1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/anon_inodes.h>
4 #include <linux/atomic.h>
5 #include <linux/bitmap.h>
6 #include <linux/build_bug.h>
7 #include <linux/cdev.h>
8 #include <linux/compat.h>
9 #include <linux/compiler.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/file.h>
13 #include <linux/gpio.h>
14 #include <linux/gpio/driver.h>
15 #include <linux/interrupt.h>
16 #include <linux/irqreturn.h>
17 #include <linux/kernel.h>
18 #include <linux/kfifo.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/poll.h>
23 #include <linux/spinlock.h>
24 #include <linux/timekeeping.h>
25 #include <linux/uaccess.h>
26 #include <linux/workqueue.h>
27 #include <linux/hte.h>
28 #include <uapi/linux/gpio.h>
31 #include "gpiolib-cdev.h"
34 * Array sizes must ensure 64-bit alignment and not create holes in the
37 static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2));
38 static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8));
41 * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility
43 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8));
44 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8));
45 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8));
46 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8));
47 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8));
48 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8));
49 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8));
50 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8));
52 /* Character device interface to GPIO.
54 * The GPIO character device, /dev/gpiochipN, provides userspace an
55 * interface to gpiolib GPIOs via ioctl()s.
59 * GPIO line handle management
62 #ifdef CONFIG_GPIO_CDEV_V1
64 * struct linehandle_state - contains the state of a userspace handle
65 * @gdev: the GPIO device the handle pertains to
66 * @label: consumer label used to tag descriptors
67 * @descs: the GPIO descriptors held by this handle
68 * @num_descs: the number of descriptors held in the descs array
70 struct linehandle_state {
71 struct gpio_device *gdev;
73 struct gpio_desc *descs[GPIOHANDLES_MAX];
77 #define GPIOHANDLE_REQUEST_VALID_FLAGS \
78 (GPIOHANDLE_REQUEST_INPUT | \
79 GPIOHANDLE_REQUEST_OUTPUT | \
80 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
81 GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
82 GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
83 GPIOHANDLE_REQUEST_BIAS_DISABLE | \
84 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
85 GPIOHANDLE_REQUEST_OPEN_SOURCE)
87 static int linehandle_validate_flags(u32 flags)
89 /* Return an error if an unknown flag is set */
90 if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
94 * Do not allow both INPUT & OUTPUT flags to be set as they are
97 if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
98 (flags & GPIOHANDLE_REQUEST_OUTPUT))
102 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
103 * the hardware actually supports enabling both at the same time the
104 * electrical result would be disastrous.
106 if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
107 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
110 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
111 if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
112 ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
113 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
116 /* Bias flags only allowed for input or output mode. */
117 if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
118 (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
119 ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
120 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
121 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
124 /* Only one bias flag can be set. */
125 if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
126 (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
127 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
128 ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
129 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
135 static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
137 assign_bit(FLAG_ACTIVE_LOW, flagsp,
138 lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
139 assign_bit(FLAG_OPEN_DRAIN, flagsp,
140 lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
141 assign_bit(FLAG_OPEN_SOURCE, flagsp,
142 lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
143 assign_bit(FLAG_PULL_UP, flagsp,
144 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
145 assign_bit(FLAG_PULL_DOWN, flagsp,
146 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
147 assign_bit(FLAG_BIAS_DISABLE, flagsp,
148 lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
151 static long linehandle_set_config(struct linehandle_state *lh,
154 struct gpiohandle_config gcnf;
155 struct gpio_desc *desc;
159 if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
163 ret = linehandle_validate_flags(lflags);
167 for (i = 0; i < lh->num_descs; i++) {
169 linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags);
172 * Lines have to be requested explicitly for input
173 * or output, else the line will be treated "as is".
175 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
176 int val = !!gcnf.default_values[i];
178 ret = gpiod_direction_output(desc, val);
181 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
182 ret = gpiod_direction_input(desc);
187 blocking_notifier_call_chain(&desc->gdev->notifier,
188 GPIO_V2_LINE_CHANGED_CONFIG,
194 static long linehandle_ioctl(struct file *file, unsigned int cmd,
197 struct linehandle_state *lh = file->private_data;
198 void __user *ip = (void __user *)arg;
199 struct gpiohandle_data ghd;
200 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
205 case GPIOHANDLE_GET_LINE_VALUES_IOCTL:
206 /* NOTE: It's okay to read values of output lines */
207 ret = gpiod_get_array_value_complex(false, true,
208 lh->num_descs, lh->descs,
213 memset(&ghd, 0, sizeof(ghd));
214 for (i = 0; i < lh->num_descs; i++)
215 ghd.values[i] = test_bit(i, vals);
217 if (copy_to_user(ip, &ghd, sizeof(ghd)))
221 case GPIOHANDLE_SET_LINE_VALUES_IOCTL:
223 * All line descriptors were created at once with the same
224 * flags so just check if the first one is really output.
226 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
229 if (copy_from_user(&ghd, ip, sizeof(ghd)))
232 /* Clamp all values to [0,1] */
233 for (i = 0; i < lh->num_descs; i++)
234 __assign_bit(i, vals, ghd.values[i]);
236 /* Reuse the array setting function */
237 return gpiod_set_array_value_complex(false,
243 case GPIOHANDLE_SET_CONFIG_IOCTL:
244 return linehandle_set_config(lh, ip);
251 static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
254 return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
258 static void linehandle_free(struct linehandle_state *lh)
262 for (i = 0; i < lh->num_descs; i++)
264 gpiod_free(lh->descs[i]);
266 put_device(&lh->gdev->dev);
270 static int linehandle_release(struct inode *inode, struct file *file)
272 linehandle_free(file->private_data);
276 static const struct file_operations linehandle_fileops = {
277 .release = linehandle_release,
278 .owner = THIS_MODULE,
279 .llseek = noop_llseek,
280 .unlocked_ioctl = linehandle_ioctl,
282 .compat_ioctl = linehandle_ioctl_compat,
286 static int linehandle_create(struct gpio_device *gdev, void __user *ip)
288 struct gpiohandle_request handlereq;
289 struct linehandle_state *lh;
294 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
296 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
299 lflags = handlereq.flags;
301 ret = linehandle_validate_flags(lflags);
305 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
309 get_device(&gdev->dev);
311 if (handlereq.consumer_label[0] != '\0') {
312 /* label is only initialized if consumer_label is set */
313 lh->label = kstrndup(handlereq.consumer_label,
314 sizeof(handlereq.consumer_label) - 1,
322 lh->num_descs = handlereq.lines;
324 /* Request each GPIO */
325 for (i = 0; i < handlereq.lines; i++) {
326 u32 offset = handlereq.lineoffsets[i];
327 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
334 ret = gpiod_request_user(desc, lh->label);
338 linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
340 ret = gpiod_set_transitory(desc, false);
345 * Lines have to be requested explicitly for input
346 * or output, else the line will be treated "as is".
348 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
349 int val = !!handlereq.default_values[i];
351 ret = gpiod_direction_output(desc, val);
354 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
355 ret = gpiod_direction_input(desc);
360 blocking_notifier_call_chain(&desc->gdev->notifier,
361 GPIO_V2_LINE_CHANGED_REQUESTED, desc);
363 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
367 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
373 file = anon_inode_getfile("gpio-linehandle",
376 O_RDONLY | O_CLOEXEC);
379 goto out_put_unused_fd;
383 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
385 * fput() will trigger the release() callback, so do not go onto
386 * the regular error cleanup path here.
393 fd_install(fd, file);
395 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
406 #endif /* CONFIG_GPIO_CDEV_V1 */
409 * struct line - contains the state of a requested line
410 * @desc: the GPIO descriptor for this line.
411 * @req: the corresponding line request
412 * @irq: the interrupt triggered in response to events on this GPIO
413 * @eflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or
414 * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied
415 * @timestamp_ns: cache for the timestamp storing it between hardirq and
416 * IRQ thread, used to bring the timestamp close to the actual event
417 * @req_seqno: the seqno for the current edge event in the sequence of
418 * events for the corresponding line request. This is drawn from the @req.
419 * @line_seqno: the seqno for the current edge event in the sequence of
420 * events for this line.
421 * @work: the worker that implements software debouncing
422 * @sw_debounced: flag indicating if the software debouncer is active
423 * @level: the current debounced physical level of the line
426 struct gpio_desc *desc;
428 * -- edge detector specific fields --
433 * eflags is set by edge_detector_setup(), edge_detector_stop() and
434 * edge_detector_update(), which are themselves mutually exclusive,
435 * and is accessed by edge_irq_thread() and debounce_work_func(),
436 * which can both live with a slightly stale value.
440 * timestamp_ns and req_seqno are accessed only by
441 * edge_irq_handler() and edge_irq_thread(), which are themselves
442 * mutually exclusive, so no additional protection is necessary.
447 * line_seqno is accessed by either edge_irq_thread() or
448 * debounce_work_func(), which are themselves mutually exclusive,
449 * so no additional protection is necessary.
453 * -- debouncer specific fields --
455 struct delayed_work work;
457 * sw_debounce is accessed by linereq_set_config(), which is the
458 * only setter, and linereq_get_values(), which can live with a
459 * slightly stale value.
461 unsigned int sw_debounced;
463 * level is accessed by debounce_work_func(), which is the only
464 * setter, and linereq_get_values() which can live with a slightly
469 * -- hte specific fields --
471 struct hte_ts_desc hdesc;
473 * HTE provider sets line level at the time of event. The valid
474 * value is 0 or 1 and negative value for an error.
478 * when sw_debounce is set on HTE enabled line, this is running
479 * counter of the discarded events.
481 u32 total_discard_seq;
483 * when sw_debounce is set on HTE enabled line, this variable records
484 * last sequence number before debounce period expires.
490 * struct linereq - contains the state of a userspace line request
491 * @gdev: the GPIO device the line request pertains to
492 * @label: consumer label used to tag GPIO descriptors
493 * @num_lines: the number of lines in the lines array
494 * @wait: wait queue that handles blocking reads of events
495 * @event_buffer_size: the number of elements allocated in @events
496 * @events: KFIFO for the GPIO events
497 * @seqno: the sequence number for edge events generated on all lines in
498 * this line request. Note that this is not used when @num_lines is 1, as
499 * the line_seqno is then the same and is cheaper to calculate.
500 * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
501 * of configuration, particularly multi-step accesses to desc flags.
502 * @lines: the lines held by this line request, with @num_lines elements.
505 struct gpio_device *gdev;
508 wait_queue_head_t wait;
509 u32 event_buffer_size;
510 DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
512 struct mutex config_mutex;
516 #define GPIO_V2_LINE_BIAS_FLAGS \
517 (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
518 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
519 GPIO_V2_LINE_FLAG_BIAS_DISABLED)
521 #define GPIO_V2_LINE_DIRECTION_FLAGS \
522 (GPIO_V2_LINE_FLAG_INPUT | \
523 GPIO_V2_LINE_FLAG_OUTPUT)
525 #define GPIO_V2_LINE_DRIVE_FLAGS \
526 (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
527 GPIO_V2_LINE_FLAG_OPEN_SOURCE)
529 #define GPIO_V2_LINE_EDGE_FLAGS \
530 (GPIO_V2_LINE_FLAG_EDGE_RISING | \
531 GPIO_V2_LINE_FLAG_EDGE_FALLING)
533 #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS
535 #define GPIO_V2_LINE_VALID_FLAGS \
536 (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
537 GPIO_V2_LINE_DIRECTION_FLAGS | \
538 GPIO_V2_LINE_DRIVE_FLAGS | \
539 GPIO_V2_LINE_EDGE_FLAGS | \
540 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
541 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
542 GPIO_V2_LINE_BIAS_FLAGS)
544 static void linereq_put_event(struct linereq *lr,
545 struct gpio_v2_line_event *le)
547 bool overflow = false;
549 spin_lock(&lr->wait.lock);
550 if (kfifo_is_full(&lr->events)) {
552 kfifo_skip(&lr->events);
554 kfifo_in(&lr->events, le, 1);
555 spin_unlock(&lr->wait.lock);
557 wake_up_poll(&lr->wait, EPOLLIN);
559 pr_debug_ratelimited("event FIFO is full - event dropped\n");
562 static u64 line_event_timestamp(struct line *line)
564 if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
565 return ktime_get_real_ns();
566 else if (test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))
567 return line->timestamp_ns;
569 return ktime_get_ns();
572 static enum hte_return process_hw_ts_thread(void *p)
576 struct gpio_v2_line_event le;
581 return HTE_CB_HANDLED;
586 memset(&le, 0, sizeof(le));
588 le.timestamp_ns = line->timestamp_ns;
589 eflags = READ_ONCE(line->eflags);
591 if (eflags == GPIO_V2_LINE_FLAG_EDGE_BOTH) {
592 if (line->raw_level >= 0) {
593 if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
594 level = !line->raw_level;
596 level = line->raw_level;
598 level = gpiod_get_value_cansleep(line->desc);
602 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
604 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
605 } else if (eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) {
606 /* Emit low-to-high event */
607 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
608 } else if (eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) {
609 /* Emit high-to-low event */
610 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
612 return HTE_CB_HANDLED;
614 le.line_seqno = line->line_seqno;
615 le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
616 le.offset = gpio_chip_hwgpio(line->desc);
618 linereq_put_event(lr, &le);
620 return HTE_CB_HANDLED;
623 static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p)
630 return HTE_CB_HANDLED;
633 line->timestamp_ns = ts->tsc;
634 line->raw_level = ts->raw_level;
637 if (READ_ONCE(line->sw_debounced)) {
638 line->total_discard_seq++;
639 line->last_seqno = ts->seq;
640 mod_delayed_work(system_wq, &line->work,
641 usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
643 if (unlikely(ts->seq < line->line_seqno))
644 return HTE_CB_HANDLED;
646 diff_seqno = ts->seq - line->line_seqno;
647 line->line_seqno = ts->seq;
648 if (lr->num_lines != 1)
649 line->req_seqno = atomic_add_return(diff_seqno,
652 return HTE_RUN_SECOND_CB;
655 return HTE_CB_HANDLED;
658 static irqreturn_t edge_irq_thread(int irq, void *p)
660 struct line *line = p;
661 struct linereq *lr = line->req;
662 struct gpio_v2_line_event le;
665 /* Do not leak kernel stack to userspace */
666 memset(&le, 0, sizeof(le));
668 if (line->timestamp_ns) {
669 le.timestamp_ns = line->timestamp_ns;
672 * We may be running from a nested threaded interrupt in
673 * which case we didn't get the timestamp from
674 * edge_irq_handler().
676 le.timestamp_ns = line_event_timestamp(line);
677 if (lr->num_lines != 1)
678 line->req_seqno = atomic_inc_return(&lr->seqno);
680 line->timestamp_ns = 0;
682 eflags = READ_ONCE(line->eflags);
683 if (eflags == GPIO_V2_LINE_FLAG_EDGE_BOTH) {
684 int level = gpiod_get_value_cansleep(line->desc);
687 /* Emit low-to-high event */
688 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
690 /* Emit high-to-low event */
691 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
692 } else if (eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) {
693 /* Emit low-to-high event */
694 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
695 } else if (eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) {
696 /* Emit high-to-low event */
697 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
702 le.line_seqno = line->line_seqno;
703 le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
704 le.offset = gpio_chip_hwgpio(line->desc);
706 linereq_put_event(lr, &le);
711 static irqreturn_t edge_irq_handler(int irq, void *p)
713 struct line *line = p;
714 struct linereq *lr = line->req;
717 * Just store the timestamp in hardirq context so we get it as
718 * close in time as possible to the actual event.
720 line->timestamp_ns = line_event_timestamp(line);
722 if (lr->num_lines != 1)
723 line->req_seqno = atomic_inc_return(&lr->seqno);
725 return IRQ_WAKE_THREAD;
729 * returns the current debounced logical value.
731 static bool debounced_value(struct line *line)
736 * minor race - debouncer may be stopped here, so edge_detector_stop()
737 * must leave the value unchanged so the following will read the level
738 * from when the debouncer was last running.
740 value = READ_ONCE(line->level);
742 if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
748 static irqreturn_t debounce_irq_handler(int irq, void *p)
750 struct line *line = p;
752 mod_delayed_work(system_wq, &line->work,
753 usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
758 static void debounce_work_func(struct work_struct *work)
760 struct gpio_v2_line_event le;
761 struct line *line = container_of(work, struct line, work.work);
763 int level, diff_seqno;
766 if (test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags)) {
767 level = line->raw_level;
769 level = gpiod_get_raw_value_cansleep(line->desc);
771 level = gpiod_get_raw_value_cansleep(line->desc);
774 pr_debug_ratelimited("debouncer failed to read line value\n");
778 if (READ_ONCE(line->level) == level)
781 WRITE_ONCE(line->level, level);
783 /* -- edge detection -- */
784 eflags = READ_ONCE(line->eflags);
788 /* switch from physical level to logical - if they differ */
789 if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
792 /* ignore edges that are not being monitored */
793 if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
794 ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
797 /* Do not leak kernel stack to userspace */
798 memset(&le, 0, sizeof(le));
801 le.timestamp_ns = line_event_timestamp(line);
802 le.offset = gpio_chip_hwgpio(line->desc);
803 if (test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags)) {
804 /* discard events except the last one */
805 line->total_discard_seq -= 1;
806 diff_seqno = line->last_seqno - line->total_discard_seq -
808 line->line_seqno = line->last_seqno - line->total_discard_seq;
809 le.line_seqno = line->line_seqno;
810 le.seqno = (lr->num_lines == 1) ?
811 le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno);
814 le.line_seqno = line->line_seqno;
815 le.seqno = (lr->num_lines == 1) ?
816 le.line_seqno : atomic_inc_return(&lr->seqno);
820 /* Emit low-to-high event */
821 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
823 /* Emit high-to-low event */
824 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
826 linereq_put_event(lr, &le);
829 static int hte_edge_setup(struct line *line, u64 eflags)
832 unsigned long flags = 0;
833 struct hte_ts_desc *hdesc = &line->hdesc;
835 if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
836 flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
837 HTE_FALLING_EDGE_TS : HTE_RISING_EDGE_TS;
838 if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
839 flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
840 HTE_RISING_EDGE_TS : HTE_FALLING_EDGE_TS;
842 line->total_discard_seq = 0;
844 hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags,
847 ret = hte_ts_get(NULL, hdesc, 0);
851 return hte_request_ts_ns(hdesc, process_hw_ts,
852 process_hw_ts_thread, line);
855 static int debounce_setup(struct line *line,
856 unsigned int debounce_period_us, bool hte_req)
858 unsigned long irqflags;
862 ret = gpiod_set_debounce(line->desc, debounce_period_us);
864 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
867 if (ret != -ENOTSUPP)
870 if (debounce_period_us) {
871 /* setup software debounce */
872 level = gpiod_get_raw_value_cansleep(line->desc);
877 irq = gpiod_to_irq(line->desc);
881 irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
882 ret = request_irq(irq, debounce_irq_handler, irqflags,
883 line->req->label, line);
888 ret = hte_edge_setup(line,
889 GPIO_V2_LINE_FLAG_EDGE_RISING |
890 GPIO_V2_LINE_FLAG_EDGE_FALLING);
895 WRITE_ONCE(line->level, level);
896 WRITE_ONCE(line->sw_debounced, 1);
901 static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
902 unsigned int line_idx)
905 u64 mask = BIT_ULL(line_idx);
907 for (i = 0; i < lc->num_attrs; i++) {
908 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
909 (lc->attrs[i].mask & mask))
915 static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
916 unsigned int line_idx)
919 u64 mask = BIT_ULL(line_idx);
921 for (i = 0; i < lc->num_attrs; i++) {
922 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
923 (lc->attrs[i].mask & mask))
924 return lc->attrs[i].attr.debounce_period_us;
929 static void edge_detector_stop(struct line *line, bool hte_en)
931 if (line->irq && !hte_en) {
932 free_irq(line->irq, line);
937 hte_ts_put(&line->hdesc);
939 cancel_delayed_work_sync(&line->work);
940 WRITE_ONCE(line->sw_debounced, 0);
941 WRITE_ONCE(line->eflags, 0);
943 WRITE_ONCE(line->desc->debounce_period_us, 0);
944 /* do not change line->level - see comment in debounced_value() */
947 static int edge_detector_setup(struct line *line,
948 struct gpio_v2_line_config *lc,
949 unsigned int line_idx,
950 u64 eflags, bool hte_req)
952 u32 debounce_period_us;
953 unsigned long irqflags = 0;
956 if (eflags && !kfifo_initialized(&line->req->events)) {
957 ret = kfifo_alloc(&line->req->events,
958 line->req->event_buffer_size, GFP_KERNEL);
962 WRITE_ONCE(line->eflags, eflags);
963 if (gpio_v2_line_config_debounced(lc, line_idx)) {
964 debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
965 ret = debounce_setup(line, debounce_period_us, hte_req);
968 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
971 /* detection disabled or sw debouncer will provide edge detection */
972 if (!eflags || READ_ONCE(line->sw_debounced))
976 return hte_edge_setup(line, eflags);
978 irq = gpiod_to_irq(line->desc);
982 if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
983 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
984 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
985 if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
986 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
987 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
988 irqflags |= IRQF_ONESHOT;
990 /* Request a thread to read the events */
991 ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
992 irqflags, line->req->label, line);
1000 static int edge_detector_update(struct line *line,
1001 struct gpio_v2_line_config *lc,
1002 unsigned int line_idx,
1003 u64 flags, bool polarity_change,
1006 u64 eflags = flags & GPIO_V2_LINE_EDGE_FLAGS;
1007 unsigned int debounce_period_us =
1008 gpio_v2_line_config_debounce_period(lc, line_idx);
1009 bool hte_change = (prev_hte_flag !=
1010 ((flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) != 0));
1012 if ((READ_ONCE(line->eflags) == eflags) && !polarity_change &&
1013 (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us)
1017 /* sw debounced and still will be...*/
1018 if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
1019 WRITE_ONCE(line->eflags, eflags);
1020 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
1024 /* reconfiguring edge detection or sw debounce being disabled */
1025 if ((line->irq && !READ_ONCE(line->sw_debounced)) || prev_hte_flag ||
1026 (!debounce_period_us && READ_ONCE(line->sw_debounced)))
1027 edge_detector_stop(line, prev_hte_flag);
1029 return edge_detector_setup(line, lc, line_idx, eflags,
1030 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
1033 static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
1034 unsigned int line_idx)
1037 u64 mask = BIT_ULL(line_idx);
1039 for (i = 0; i < lc->num_attrs; i++) {
1040 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
1041 (lc->attrs[i].mask & mask))
1042 return lc->attrs[i].attr.flags;
1047 static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
1048 unsigned int line_idx)
1051 u64 mask = BIT_ULL(line_idx);
1053 for (i = 0; i < lc->num_attrs; i++) {
1054 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
1055 (lc->attrs[i].mask & mask))
1056 return !!(lc->attrs[i].attr.values & mask);
1061 static int gpio_v2_line_flags_validate(u64 flags)
1063 /* Return an error if an unknown flag is set */
1064 if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
1067 * Do not allow both INPUT and OUTPUT flags to be set as they are
1070 if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
1071 (flags & GPIO_V2_LINE_FLAG_OUTPUT))
1074 /* Only allow one event clock source */
1075 if ((flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) &&
1076 (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1079 /* Edge detection requires explicit input. */
1080 if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
1081 !(flags & GPIO_V2_LINE_FLAG_INPUT))
1085 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
1086 * request. If the hardware actually supports enabling both at the
1087 * same time the electrical result would be disastrous.
1089 if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
1090 (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
1093 /* Drive requires explicit output direction. */
1094 if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
1095 !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
1098 /* Bias requires explicit direction. */
1099 if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
1100 !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
1103 /* Only one bias flag can be set. */
1104 if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
1105 (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
1106 GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
1107 ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
1108 (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
1114 static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
1115 unsigned int num_lines)
1121 if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
1124 if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
1127 for (i = 0; i < num_lines; i++) {
1128 flags = gpio_v2_line_config_flags(lc, i);
1129 ret = gpio_v2_line_flags_validate(flags);
1133 /* debounce requires explicit input */
1134 if (gpio_v2_line_config_debounced(lc, i) &&
1135 !(flags & GPIO_V2_LINE_FLAG_INPUT))
1141 static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
1142 unsigned long *flagsp)
1144 assign_bit(FLAG_ACTIVE_LOW, flagsp,
1145 flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
1147 if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
1148 set_bit(FLAG_IS_OUT, flagsp);
1149 else if (flags & GPIO_V2_LINE_FLAG_INPUT)
1150 clear_bit(FLAG_IS_OUT, flagsp);
1152 assign_bit(FLAG_EDGE_RISING, flagsp,
1153 flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
1154 assign_bit(FLAG_EDGE_FALLING, flagsp,
1155 flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
1157 assign_bit(FLAG_OPEN_DRAIN, flagsp,
1158 flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
1159 assign_bit(FLAG_OPEN_SOURCE, flagsp,
1160 flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
1162 assign_bit(FLAG_PULL_UP, flagsp,
1163 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
1164 assign_bit(FLAG_PULL_DOWN, flagsp,
1165 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
1166 assign_bit(FLAG_BIAS_DISABLE, flagsp,
1167 flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
1169 assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
1170 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
1171 assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp,
1172 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
1175 static long linereq_get_values(struct linereq *lr, void __user *ip)
1177 struct gpio_v2_line_values lv;
1178 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
1179 struct gpio_desc **descs;
1180 unsigned int i, didx, num_get;
1184 /* NOTE: It's ok to read values of output lines. */
1185 if (copy_from_user(&lv, ip, sizeof(lv)))
1188 for (num_get = 0, i = 0; i < lr->num_lines; i++) {
1189 if (lv.mask & BIT_ULL(i)) {
1191 descs = &lr->lines[i].desc;
1199 descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
1202 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1203 if (lv.mask & BIT_ULL(i)) {
1204 descs[didx] = lr->lines[i].desc;
1209 ret = gpiod_get_array_value_complex(false, true, num_get,
1218 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1219 if (lv.mask & BIT_ULL(i)) {
1220 if (lr->lines[i].sw_debounced)
1221 val = debounced_value(&lr->lines[i]);
1223 val = test_bit(didx, vals);
1225 lv.bits |= BIT_ULL(i);
1230 if (copy_to_user(ip, &lv, sizeof(lv)))
1236 static long linereq_set_values_unlocked(struct linereq *lr,
1237 struct gpio_v2_line_values *lv)
1239 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
1240 struct gpio_desc **descs;
1241 unsigned int i, didx, num_set;
1244 bitmap_zero(vals, GPIO_V2_LINES_MAX);
1245 for (num_set = 0, i = 0; i < lr->num_lines; i++) {
1246 if (lv->mask & BIT_ULL(i)) {
1247 if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
1249 if (lv->bits & BIT_ULL(i))
1250 __set_bit(num_set, vals);
1252 descs = &lr->lines[i].desc;
1259 /* build compacted desc array and values */
1260 descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
1263 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1264 if (lv->mask & BIT_ULL(i)) {
1265 descs[didx] = lr->lines[i].desc;
1270 ret = gpiod_set_array_value_complex(false, true, num_set,
1278 static long linereq_set_values(struct linereq *lr, void __user *ip)
1280 struct gpio_v2_line_values lv;
1283 if (copy_from_user(&lv, ip, sizeof(lv)))
1286 mutex_lock(&lr->config_mutex);
1288 ret = linereq_set_values_unlocked(lr, &lv);
1290 mutex_unlock(&lr->config_mutex);
1295 static long linereq_set_config_unlocked(struct linereq *lr,
1296 struct gpio_v2_line_config *lc)
1298 struct gpio_desc *desc;
1301 bool polarity_change;
1305 for (i = 0; i < lr->num_lines; i++) {
1306 desc = lr->lines[i].desc;
1307 flags = gpio_v2_line_config_flags(lc, i);
1309 (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) !=
1310 ((flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) != 0));
1312 prev_hte_flag = !!test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags);
1314 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1316 * Lines have to be requested explicitly for input
1317 * or output, else the line will be treated "as is".
1319 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1320 int val = gpio_v2_line_config_output_value(lc, i);
1322 edge_detector_stop(&lr->lines[i], prev_hte_flag);
1323 ret = gpiod_direction_output(desc, val);
1326 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1327 ret = gpiod_direction_input(desc);
1331 ret = edge_detector_update(&lr->lines[i], lc, i,
1332 flags, polarity_change, prev_hte_flag);
1337 blocking_notifier_call_chain(&desc->gdev->notifier,
1338 GPIO_V2_LINE_CHANGED_CONFIG,
1344 static long linereq_set_config(struct linereq *lr, void __user *ip)
1346 struct gpio_v2_line_config lc;
1349 if (copy_from_user(&lc, ip, sizeof(lc)))
1352 ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
1356 mutex_lock(&lr->config_mutex);
1358 ret = linereq_set_config_unlocked(lr, &lc);
1360 mutex_unlock(&lr->config_mutex);
1365 static long linereq_ioctl(struct file *file, unsigned int cmd,
1368 struct linereq *lr = file->private_data;
1369 void __user *ip = (void __user *)arg;
1372 case GPIO_V2_LINE_GET_VALUES_IOCTL:
1373 return linereq_get_values(lr, ip);
1374 case GPIO_V2_LINE_SET_VALUES_IOCTL:
1375 return linereq_set_values(lr, ip);
1376 case GPIO_V2_LINE_SET_CONFIG_IOCTL:
1377 return linereq_set_config(lr, ip);
1383 #ifdef CONFIG_COMPAT
1384 static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
1387 return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1391 static __poll_t linereq_poll(struct file *file,
1392 struct poll_table_struct *wait)
1394 struct linereq *lr = file->private_data;
1395 __poll_t events = 0;
1397 poll_wait(file, &lr->wait, wait);
1399 if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
1401 events = EPOLLIN | EPOLLRDNORM;
1406 static ssize_t linereq_read(struct file *file,
1411 struct linereq *lr = file->private_data;
1412 struct gpio_v2_line_event le;
1413 ssize_t bytes_read = 0;
1416 if (count < sizeof(le))
1420 spin_lock(&lr->wait.lock);
1421 if (kfifo_is_empty(&lr->events)) {
1423 spin_unlock(&lr->wait.lock);
1427 if (file->f_flags & O_NONBLOCK) {
1428 spin_unlock(&lr->wait.lock);
1432 ret = wait_event_interruptible_locked(lr->wait,
1433 !kfifo_is_empty(&lr->events));
1435 spin_unlock(&lr->wait.lock);
1440 ret = kfifo_out(&lr->events, &le, 1);
1441 spin_unlock(&lr->wait.lock);
1444 * This should never happen - we were holding the
1445 * lock from the moment we learned the fifo is no
1446 * longer empty until now.
1452 if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
1454 bytes_read += sizeof(le);
1455 } while (count >= bytes_read + sizeof(le));
1460 static void linereq_free(struct linereq *lr)
1465 for (i = 0; i < lr->num_lines; i++) {
1466 hte = !!test_bit(FLAG_EVENT_CLOCK_HTE,
1467 &lr->lines[i].desc->flags);
1468 edge_detector_stop(&lr->lines[i], hte);
1469 if (lr->lines[i].desc)
1470 gpiod_free(lr->lines[i].desc);
1472 kfifo_free(&lr->events);
1474 put_device(&lr->gdev->dev);
1478 static int linereq_release(struct inode *inode, struct file *file)
1480 struct linereq *lr = file->private_data;
1486 static const struct file_operations line_fileops = {
1487 .release = linereq_release,
1488 .read = linereq_read,
1489 .poll = linereq_poll,
1490 .owner = THIS_MODULE,
1491 .llseek = noop_llseek,
1492 .unlocked_ioctl = linereq_ioctl,
1493 #ifdef CONFIG_COMPAT
1494 .compat_ioctl = linereq_ioctl_compat,
1498 static int linereq_create(struct gpio_device *gdev, void __user *ip)
1500 struct gpio_v2_line_request ulr;
1501 struct gpio_v2_line_config *lc;
1508 if (copy_from_user(&ulr, ip, sizeof(ulr)))
1511 if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
1514 if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
1518 ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
1522 lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
1527 get_device(&gdev->dev);
1529 for (i = 0; i < ulr.num_lines; i++) {
1530 lr->lines[i].req = lr;
1531 WRITE_ONCE(lr->lines[i].sw_debounced, 0);
1532 INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
1535 if (ulr.consumer[0] != '\0') {
1536 /* label is only initialized if consumer is set */
1537 lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1541 goto out_free_linereq;
1545 mutex_init(&lr->config_mutex);
1546 init_waitqueue_head(&lr->wait);
1547 lr->event_buffer_size = ulr.event_buffer_size;
1548 if (lr->event_buffer_size == 0)
1549 lr->event_buffer_size = ulr.num_lines * 16;
1550 else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
1551 lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
1553 atomic_set(&lr->seqno, 0);
1554 lr->num_lines = ulr.num_lines;
1556 /* Request each GPIO */
1557 for (i = 0; i < ulr.num_lines; i++) {
1558 u32 offset = ulr.offsets[i];
1559 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
1562 ret = PTR_ERR(desc);
1563 goto out_free_linereq;
1566 ret = gpiod_request_user(desc, lr->label);
1568 goto out_free_linereq;
1570 lr->lines[i].desc = desc;
1571 flags = gpio_v2_line_config_flags(lc, i);
1572 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1574 ret = gpiod_set_transitory(desc, false);
1576 goto out_free_linereq;
1579 * Lines have to be requested explicitly for input
1580 * or output, else the line will be treated "as is".
1582 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1583 int val = gpio_v2_line_config_output_value(lc, i);
1585 ret = gpiod_direction_output(desc, val);
1587 goto out_free_linereq;
1588 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1589 ret = gpiod_direction_input(desc);
1591 goto out_free_linereq;
1593 ret = edge_detector_setup(&lr->lines[i], lc, i,
1594 flags & GPIO_V2_LINE_EDGE_FLAGS,
1595 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
1597 goto out_free_linereq;
1600 blocking_notifier_call_chain(&desc->gdev->notifier,
1601 GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1603 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
1607 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1610 goto out_free_linereq;
1613 file = anon_inode_getfile("gpio-line", &line_fileops, lr,
1614 O_RDONLY | O_CLOEXEC);
1616 ret = PTR_ERR(file);
1617 goto out_put_unused_fd;
1621 if (copy_to_user(ip, &ulr, sizeof(ulr))) {
1623 * fput() will trigger the release() callback, so do not go onto
1624 * the regular error cleanup path here.
1631 fd_install(fd, file);
1633 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
1645 #ifdef CONFIG_GPIO_CDEV_V1
1648 * GPIO line event management
1652 * struct lineevent_state - contains the state of a userspace event
1653 * @gdev: the GPIO device the event pertains to
1654 * @label: consumer label used to tag descriptors
1655 * @desc: the GPIO descriptor held by this event
1656 * @eflags: the event flags this line was requested with
1657 * @irq: the interrupt that trigger in response to events on this GPIO
1658 * @wait: wait queue that handles blocking reads of events
1659 * @events: KFIFO for the GPIO events
1660 * @timestamp: cache for the timestamp storing it between hardirq
1661 * and IRQ thread, used to bring the timestamp close to the actual
1664 struct lineevent_state {
1665 struct gpio_device *gdev;
1667 struct gpio_desc *desc;
1670 wait_queue_head_t wait;
1671 DECLARE_KFIFO(events, struct gpioevent_data, 16);
1675 #define GPIOEVENT_REQUEST_VALID_FLAGS \
1676 (GPIOEVENT_REQUEST_RISING_EDGE | \
1677 GPIOEVENT_REQUEST_FALLING_EDGE)
1679 static __poll_t lineevent_poll(struct file *file,
1680 struct poll_table_struct *wait)
1682 struct lineevent_state *le = file->private_data;
1683 __poll_t events = 0;
1685 poll_wait(file, &le->wait, wait);
1687 if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1688 events = EPOLLIN | EPOLLRDNORM;
1693 struct compat_gpioeevent_data {
1694 compat_u64 timestamp;
1698 static ssize_t lineevent_read(struct file *file,
1703 struct lineevent_state *le = file->private_data;
1704 struct gpioevent_data ge;
1705 ssize_t bytes_read = 0;
1710 * When compatible system call is being used the struct gpioevent_data,
1711 * in case of at least ia32, has different size due to the alignment
1712 * differences. Because we have first member 64 bits followed by one of
1713 * 32 bits there is no gap between them. The only difference is the
1714 * padding at the end of the data structure. Hence, we calculate the
1715 * actual sizeof() and pass this as an argument to copy_to_user() to
1716 * drop unneeded bytes from the output.
1718 if (compat_need_64bit_alignment_fixup())
1719 ge_size = sizeof(struct compat_gpioeevent_data);
1721 ge_size = sizeof(struct gpioevent_data);
1722 if (count < ge_size)
1726 spin_lock(&le->wait.lock);
1727 if (kfifo_is_empty(&le->events)) {
1729 spin_unlock(&le->wait.lock);
1733 if (file->f_flags & O_NONBLOCK) {
1734 spin_unlock(&le->wait.lock);
1738 ret = wait_event_interruptible_locked(le->wait,
1739 !kfifo_is_empty(&le->events));
1741 spin_unlock(&le->wait.lock);
1746 ret = kfifo_out(&le->events, &ge, 1);
1747 spin_unlock(&le->wait.lock);
1750 * This should never happen - we were holding the lock
1751 * from the moment we learned the fifo is no longer
1758 if (copy_to_user(buf + bytes_read, &ge, ge_size))
1760 bytes_read += ge_size;
1761 } while (count >= bytes_read + ge_size);
1766 static void lineevent_free(struct lineevent_state *le)
1769 free_irq(le->irq, le);
1771 gpiod_free(le->desc);
1773 put_device(&le->gdev->dev);
1777 static int lineevent_release(struct inode *inode, struct file *file)
1779 lineevent_free(file->private_data);
1783 static long lineevent_ioctl(struct file *file, unsigned int cmd,
1786 struct lineevent_state *le = file->private_data;
1787 void __user *ip = (void __user *)arg;
1788 struct gpiohandle_data ghd;
1791 * We can get the value for an event line but not set it,
1792 * because it is input by definition.
1794 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
1797 memset(&ghd, 0, sizeof(ghd));
1799 val = gpiod_get_value_cansleep(le->desc);
1802 ghd.values[0] = val;
1804 if (copy_to_user(ip, &ghd, sizeof(ghd)))
1812 #ifdef CONFIG_COMPAT
1813 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
1816 return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1820 static const struct file_operations lineevent_fileops = {
1821 .release = lineevent_release,
1822 .read = lineevent_read,
1823 .poll = lineevent_poll,
1824 .owner = THIS_MODULE,
1825 .llseek = noop_llseek,
1826 .unlocked_ioctl = lineevent_ioctl,
1827 #ifdef CONFIG_COMPAT
1828 .compat_ioctl = lineevent_ioctl_compat,
1832 static irqreturn_t lineevent_irq_thread(int irq, void *p)
1834 struct lineevent_state *le = p;
1835 struct gpioevent_data ge;
1838 /* Do not leak kernel stack to userspace */
1839 memset(&ge, 0, sizeof(ge));
1842 * We may be running from a nested threaded interrupt in which case
1843 * we didn't get the timestamp from lineevent_irq_handler().
1846 ge.timestamp = ktime_get_ns();
1848 ge.timestamp = le->timestamp;
1850 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
1851 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1852 int level = gpiod_get_value_cansleep(le->desc);
1855 /* Emit low-to-high event */
1856 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1858 /* Emit high-to-low event */
1859 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1860 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
1861 /* Emit low-to-high event */
1862 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1863 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1864 /* Emit high-to-low event */
1865 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1870 ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
1873 wake_up_poll(&le->wait, EPOLLIN);
1875 pr_debug_ratelimited("event FIFO is full - event dropped\n");
1880 static irqreturn_t lineevent_irq_handler(int irq, void *p)
1882 struct lineevent_state *le = p;
1885 * Just store the timestamp in hardirq context so we get it as
1886 * close in time as possible to the actual event.
1888 le->timestamp = ktime_get_ns();
1890 return IRQ_WAKE_THREAD;
1893 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1895 struct gpioevent_request eventreq;
1896 struct lineevent_state *le;
1897 struct gpio_desc *desc;
1904 int irq, irqflags = 0;
1906 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1909 offset = eventreq.lineoffset;
1910 lflags = eventreq.handleflags;
1911 eflags = eventreq.eventflags;
1913 desc = gpiochip_get_desc(gdev->chip, offset);
1915 return PTR_ERR(desc);
1917 /* Return an error if a unknown flag is set */
1918 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1919 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1922 /* This is just wrong: we don't look for events on output lines */
1923 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1924 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1925 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1928 /* Only one bias flag can be set. */
1929 if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1930 (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1931 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1932 ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1933 (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1936 le = kzalloc(sizeof(*le), GFP_KERNEL);
1940 get_device(&gdev->dev);
1942 if (eventreq.consumer_label[0] != '\0') {
1943 /* label is only initialized if consumer_label is set */
1944 le->label = kstrndup(eventreq.consumer_label,
1945 sizeof(eventreq.consumer_label) - 1,
1953 ret = gpiod_request_user(desc, le->label);
1957 le->eflags = eflags;
1959 linehandle_flags_to_desc_flags(lflags, &desc->flags);
1961 ret = gpiod_direction_input(desc);
1965 blocking_notifier_call_chain(&desc->gdev->notifier,
1966 GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1968 irq = gpiod_to_irq(desc);
1975 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
1976 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1977 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1978 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
1979 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1980 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1981 irqflags |= IRQF_ONESHOT;
1983 INIT_KFIFO(le->events);
1984 init_waitqueue_head(&le->wait);
1986 /* Request a thread to read the events */
1987 ret = request_threaded_irq(le->irq,
1988 lineevent_irq_handler,
1989 lineevent_irq_thread,
1996 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
2002 file = anon_inode_getfile("gpio-event",
2005 O_RDONLY | O_CLOEXEC);
2007 ret = PTR_ERR(file);
2008 goto out_put_unused_fd;
2012 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
2014 * fput() will trigger the release() callback, so do not go onto
2015 * the regular error cleanup path here.
2022 fd_install(fd, file);
2033 static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
2034 struct gpioline_info *info_v1)
2036 u64 flagsv2 = info_v2->flags;
2038 memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
2039 memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
2040 info_v1->line_offset = info_v2->offset;
2043 if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
2044 info_v1->flags |= GPIOLINE_FLAG_KERNEL;
2046 if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
2047 info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
2049 if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
2050 info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
2052 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
2053 info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
2054 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
2055 info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
2057 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
2058 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
2059 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
2060 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
2061 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
2062 info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
2065 static void gpio_v2_line_info_changed_to_v1(
2066 struct gpio_v2_line_info_changed *lic_v2,
2067 struct gpioline_info_changed *lic_v1)
2069 memset(lic_v1, 0, sizeof(*lic_v1));
2070 gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
2071 lic_v1->timestamp = lic_v2->timestamp_ns;
2072 lic_v1->event_type = lic_v2->event_type;
2075 #endif /* CONFIG_GPIO_CDEV_V1 */
2077 static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
2078 struct gpio_v2_line_info *info)
2080 struct gpio_chip *gc = desc->gdev->chip;
2081 bool ok_for_pinctrl;
2082 unsigned long flags;
2083 u32 debounce_period_us;
2084 unsigned int num_attrs = 0;
2086 memset(info, 0, sizeof(*info));
2087 info->offset = gpio_chip_hwgpio(desc);
2090 * This function takes a mutex so we must check this before taking
2093 * FIXME: find a non-racy way to retrieve this information. Maybe a
2094 * lock common to both frameworks?
2097 pinctrl_gpio_can_use_line(gc->base + info->offset);
2099 spin_lock_irqsave(&gpio_lock, flags);
2102 strscpy(info->name, desc->name, sizeof(info->name));
2105 strscpy(info->consumer, desc->label, sizeof(info->consumer));
2108 * Userspace only need to know that the kernel is using this GPIO so
2112 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
2113 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
2114 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
2115 test_bit(FLAG_EXPORT, &desc->flags) ||
2116 test_bit(FLAG_SYSFS, &desc->flags) ||
2117 !gpiochip_line_is_valid(gc, info->offset) ||
2119 info->flags |= GPIO_V2_LINE_FLAG_USED;
2121 if (test_bit(FLAG_IS_OUT, &desc->flags))
2122 info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
2124 info->flags |= GPIO_V2_LINE_FLAG_INPUT;
2126 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2127 info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
2129 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2130 info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
2131 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2132 info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
2134 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2135 info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
2136 if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2137 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
2138 if (test_bit(FLAG_PULL_UP, &desc->flags))
2139 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
2141 if (test_bit(FLAG_EDGE_RISING, &desc->flags))
2142 info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
2143 if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
2144 info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
2146 if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
2147 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
2148 else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags))
2149 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
2151 debounce_period_us = READ_ONCE(desc->debounce_period_us);
2152 if (debounce_period_us) {
2153 info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
2154 info->attrs[num_attrs].debounce_period_us = debounce_period_us;
2157 info->num_attrs = num_attrs;
2159 spin_unlock_irqrestore(&gpio_lock, flags);
2162 struct gpio_chardev_data {
2163 struct gpio_device *gdev;
2164 wait_queue_head_t wait;
2165 DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
2166 struct notifier_block lineinfo_changed_nb;
2167 unsigned long *watched_lines;
2168 #ifdef CONFIG_GPIO_CDEV_V1
2169 atomic_t watch_abi_version;
2173 static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
2175 struct gpio_device *gdev = cdev->gdev;
2176 struct gpiochip_info chipinfo;
2178 memset(&chipinfo, 0, sizeof(chipinfo));
2180 strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
2181 strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
2182 chipinfo.lines = gdev->ngpio;
2183 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
2188 #ifdef CONFIG_GPIO_CDEV_V1
2190 * returns 0 if the versions match, else the previously selected ABI version
2192 static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
2193 unsigned int version)
2195 int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
2197 if (abiv == version)
2203 static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
2206 struct gpio_desc *desc;
2207 struct gpioline_info lineinfo;
2208 struct gpio_v2_line_info lineinfo_v2;
2210 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2213 /* this doubles as a range check on line_offset */
2214 desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset);
2216 return PTR_ERR(desc);
2219 if (lineinfo_ensure_abi_version(cdev, 1))
2222 if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
2226 gpio_desc_to_lineinfo(desc, &lineinfo_v2);
2227 gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
2229 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2231 clear_bit(lineinfo.line_offset, cdev->watched_lines);
2239 static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
2242 struct gpio_desc *desc;
2243 struct gpio_v2_line_info lineinfo;
2245 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2248 if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2251 desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2253 return PTR_ERR(desc);
2256 #ifdef CONFIG_GPIO_CDEV_V1
2257 if (lineinfo_ensure_abi_version(cdev, 2))
2260 if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2263 gpio_desc_to_lineinfo(desc, &lineinfo);
2265 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2267 clear_bit(lineinfo.offset, cdev->watched_lines);
2274 static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
2278 if (copy_from_user(&offset, ip, sizeof(offset)))
2281 if (offset >= cdev->gdev->ngpio)
2284 if (!test_and_clear_bit(offset, cdev->watched_lines))
2291 * gpio_ioctl() - ioctl handler for the GPIO chardev
2293 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2295 struct gpio_chardev_data *cdev = file->private_data;
2296 struct gpio_device *gdev = cdev->gdev;
2297 void __user *ip = (void __user *)arg;
2299 /* We fail any subsequent ioctl():s when the chip is gone */
2303 /* Fill in the struct and pass to userspace */
2305 case GPIO_GET_CHIPINFO_IOCTL:
2306 return chipinfo_get(cdev, ip);
2307 #ifdef CONFIG_GPIO_CDEV_V1
2308 case GPIO_GET_LINEHANDLE_IOCTL:
2309 return linehandle_create(gdev, ip);
2310 case GPIO_GET_LINEEVENT_IOCTL:
2311 return lineevent_create(gdev, ip);
2312 case GPIO_GET_LINEINFO_IOCTL:
2313 return lineinfo_get_v1(cdev, ip, false);
2314 case GPIO_GET_LINEINFO_WATCH_IOCTL:
2315 return lineinfo_get_v1(cdev, ip, true);
2316 #endif /* CONFIG_GPIO_CDEV_V1 */
2317 case GPIO_V2_GET_LINEINFO_IOCTL:
2318 return lineinfo_get(cdev, ip, false);
2319 case GPIO_V2_GET_LINEINFO_WATCH_IOCTL:
2320 return lineinfo_get(cdev, ip, true);
2321 case GPIO_V2_GET_LINE_IOCTL:
2322 return linereq_create(gdev, ip);
2323 case GPIO_GET_LINEINFO_UNWATCH_IOCTL:
2324 return lineinfo_unwatch(cdev, ip);
2330 #ifdef CONFIG_COMPAT
2331 static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2334 return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2338 static struct gpio_chardev_data *
2339 to_gpio_chardev_data(struct notifier_block *nb)
2341 return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2344 static int lineinfo_changed_notify(struct notifier_block *nb,
2345 unsigned long action, void *data)
2347 struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
2348 struct gpio_v2_line_info_changed chg;
2349 struct gpio_desc *desc = data;
2352 if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2355 memset(&chg, 0, sizeof(chg));
2356 chg.event_type = action;
2357 chg.timestamp_ns = ktime_get_ns();
2358 gpio_desc_to_lineinfo(desc, &chg.info);
2360 ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2362 wake_up_poll(&cdev->wait, EPOLLIN);
2364 pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2369 static __poll_t lineinfo_watch_poll(struct file *file,
2370 struct poll_table_struct *pollt)
2372 struct gpio_chardev_data *cdev = file->private_data;
2373 __poll_t events = 0;
2375 poll_wait(file, &cdev->wait, pollt);
2377 if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2379 events = EPOLLIN | EPOLLRDNORM;
2384 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2385 size_t count, loff_t *off)
2387 struct gpio_chardev_data *cdev = file->private_data;
2388 struct gpio_v2_line_info_changed event;
2389 ssize_t bytes_read = 0;
2393 #ifndef CONFIG_GPIO_CDEV_V1
2394 event_size = sizeof(struct gpio_v2_line_info_changed);
2395 if (count < event_size)
2400 spin_lock(&cdev->wait.lock);
2401 if (kfifo_is_empty(&cdev->events)) {
2403 spin_unlock(&cdev->wait.lock);
2407 if (file->f_flags & O_NONBLOCK) {
2408 spin_unlock(&cdev->wait.lock);
2412 ret = wait_event_interruptible_locked(cdev->wait,
2413 !kfifo_is_empty(&cdev->events));
2415 spin_unlock(&cdev->wait.lock);
2419 #ifdef CONFIG_GPIO_CDEV_V1
2420 /* must be after kfifo check so watch_abi_version is set */
2421 if (atomic_read(&cdev->watch_abi_version) == 2)
2422 event_size = sizeof(struct gpio_v2_line_info_changed);
2424 event_size = sizeof(struct gpioline_info_changed);
2425 if (count < event_size) {
2426 spin_unlock(&cdev->wait.lock);
2430 ret = kfifo_out(&cdev->events, &event, 1);
2431 spin_unlock(&cdev->wait.lock);
2435 /* We should never get here. See lineevent_read(). */
2438 #ifdef CONFIG_GPIO_CDEV_V1
2439 if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2440 if (copy_to_user(buf + bytes_read, &event, event_size))
2443 struct gpioline_info_changed event_v1;
2445 gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2446 if (copy_to_user(buf + bytes_read, &event_v1,
2451 if (copy_to_user(buf + bytes_read, &event, event_size))
2454 bytes_read += event_size;
2455 } while (count >= bytes_read + sizeof(event));
2461 * gpio_chrdev_open() - open the chardev for ioctl operations
2462 * @inode: inode for this chardev
2463 * @file: file struct for storing private data
2464 * Returns 0 on success
2466 static int gpio_chrdev_open(struct inode *inode, struct file *file)
2468 struct gpio_device *gdev = container_of(inode->i_cdev,
2469 struct gpio_device, chrdev);
2470 struct gpio_chardev_data *cdev;
2473 /* Fail on open if the backing gpiochip is gone */
2477 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2481 cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2482 if (!cdev->watched_lines)
2485 init_waitqueue_head(&cdev->wait);
2486 INIT_KFIFO(cdev->events);
2489 cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
2490 ret = blocking_notifier_chain_register(&gdev->notifier,
2491 &cdev->lineinfo_changed_nb);
2493 goto out_free_bitmap;
2495 get_device(&gdev->dev);
2496 file->private_data = cdev;
2498 ret = nonseekable_open(inode, file);
2500 goto out_unregister_notifier;
2504 out_unregister_notifier:
2505 blocking_notifier_chain_unregister(&gdev->notifier,
2506 &cdev->lineinfo_changed_nb);
2508 bitmap_free(cdev->watched_lines);
2515 * gpio_chrdev_release() - close chardev after ioctl operations
2516 * @inode: inode for this chardev
2517 * @file: file struct for storing private data
2518 * Returns 0 on success
2520 static int gpio_chrdev_release(struct inode *inode, struct file *file)
2522 struct gpio_chardev_data *cdev = file->private_data;
2523 struct gpio_device *gdev = cdev->gdev;
2525 bitmap_free(cdev->watched_lines);
2526 blocking_notifier_chain_unregister(&gdev->notifier,
2527 &cdev->lineinfo_changed_nb);
2528 put_device(&gdev->dev);
2534 static const struct file_operations gpio_fileops = {
2535 .release = gpio_chrdev_release,
2536 .open = gpio_chrdev_open,
2537 .poll = lineinfo_watch_poll,
2538 .read = lineinfo_watch_read,
2539 .owner = THIS_MODULE,
2540 .llseek = no_llseek,
2541 .unlocked_ioctl = gpio_ioctl,
2542 #ifdef CONFIG_COMPAT
2543 .compat_ioctl = gpio_ioctl_compat,
2547 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2551 cdev_init(&gdev->chrdev, &gpio_fileops);
2552 gdev->chrdev.owner = THIS_MODULE;
2553 gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2555 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2559 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2560 MAJOR(devt), gdev->id);
2565 void gpiolib_cdev_unregister(struct gpio_device *gdev)
2567 cdev_device_del(&gdev->chrdev, &gdev->dev);