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 if (lr->lines[i].desc)
1467 hte = !!test_bit(FLAG_EVENT_CLOCK_HTE,
1468 &lr->lines[i].desc->flags);
1469 edge_detector_stop(&lr->lines[i], hte);
1470 if (lr->lines[i].desc)
1471 gpiod_free(lr->lines[i].desc);
1473 kfifo_free(&lr->events);
1475 put_device(&lr->gdev->dev);
1479 static int linereq_release(struct inode *inode, struct file *file)
1481 struct linereq *lr = file->private_data;
1487 static const struct file_operations line_fileops = {
1488 .release = linereq_release,
1489 .read = linereq_read,
1490 .poll = linereq_poll,
1491 .owner = THIS_MODULE,
1492 .llseek = noop_llseek,
1493 .unlocked_ioctl = linereq_ioctl,
1494 #ifdef CONFIG_COMPAT
1495 .compat_ioctl = linereq_ioctl_compat,
1499 static int linereq_create(struct gpio_device *gdev, void __user *ip)
1501 struct gpio_v2_line_request ulr;
1502 struct gpio_v2_line_config *lc;
1509 if (copy_from_user(&ulr, ip, sizeof(ulr)))
1512 if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
1515 if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
1519 ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
1523 lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
1528 get_device(&gdev->dev);
1530 for (i = 0; i < ulr.num_lines; i++) {
1531 lr->lines[i].req = lr;
1532 WRITE_ONCE(lr->lines[i].sw_debounced, 0);
1533 INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
1536 if (ulr.consumer[0] != '\0') {
1537 /* label is only initialized if consumer is set */
1538 lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1542 goto out_free_linereq;
1546 mutex_init(&lr->config_mutex);
1547 init_waitqueue_head(&lr->wait);
1548 lr->event_buffer_size = ulr.event_buffer_size;
1549 if (lr->event_buffer_size == 0)
1550 lr->event_buffer_size = ulr.num_lines * 16;
1551 else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
1552 lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
1554 atomic_set(&lr->seqno, 0);
1555 lr->num_lines = ulr.num_lines;
1557 /* Request each GPIO */
1558 for (i = 0; i < ulr.num_lines; i++) {
1559 u32 offset = ulr.offsets[i];
1560 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
1563 ret = PTR_ERR(desc);
1564 goto out_free_linereq;
1567 ret = gpiod_request_user(desc, lr->label);
1569 goto out_free_linereq;
1571 lr->lines[i].desc = desc;
1572 flags = gpio_v2_line_config_flags(lc, i);
1573 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1575 ret = gpiod_set_transitory(desc, false);
1577 goto out_free_linereq;
1580 * Lines have to be requested explicitly for input
1581 * or output, else the line will be treated "as is".
1583 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1584 int val = gpio_v2_line_config_output_value(lc, i);
1586 ret = gpiod_direction_output(desc, val);
1588 goto out_free_linereq;
1589 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1590 ret = gpiod_direction_input(desc);
1592 goto out_free_linereq;
1594 ret = edge_detector_setup(&lr->lines[i], lc, i,
1595 flags & GPIO_V2_LINE_EDGE_FLAGS,
1596 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
1598 goto out_free_linereq;
1601 blocking_notifier_call_chain(&desc->gdev->notifier,
1602 GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1604 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
1608 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1611 goto out_free_linereq;
1614 file = anon_inode_getfile("gpio-line", &line_fileops, lr,
1615 O_RDONLY | O_CLOEXEC);
1617 ret = PTR_ERR(file);
1618 goto out_put_unused_fd;
1622 if (copy_to_user(ip, &ulr, sizeof(ulr))) {
1624 * fput() will trigger the release() callback, so do not go onto
1625 * the regular error cleanup path here.
1632 fd_install(fd, file);
1634 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
1646 #ifdef CONFIG_GPIO_CDEV_V1
1649 * GPIO line event management
1653 * struct lineevent_state - contains the state of a userspace event
1654 * @gdev: the GPIO device the event pertains to
1655 * @label: consumer label used to tag descriptors
1656 * @desc: the GPIO descriptor held by this event
1657 * @eflags: the event flags this line was requested with
1658 * @irq: the interrupt that trigger in response to events on this GPIO
1659 * @wait: wait queue that handles blocking reads of events
1660 * @events: KFIFO for the GPIO events
1661 * @timestamp: cache for the timestamp storing it between hardirq
1662 * and IRQ thread, used to bring the timestamp close to the actual
1665 struct lineevent_state {
1666 struct gpio_device *gdev;
1668 struct gpio_desc *desc;
1671 wait_queue_head_t wait;
1672 DECLARE_KFIFO(events, struct gpioevent_data, 16);
1676 #define GPIOEVENT_REQUEST_VALID_FLAGS \
1677 (GPIOEVENT_REQUEST_RISING_EDGE | \
1678 GPIOEVENT_REQUEST_FALLING_EDGE)
1680 static __poll_t lineevent_poll(struct file *file,
1681 struct poll_table_struct *wait)
1683 struct lineevent_state *le = file->private_data;
1684 __poll_t events = 0;
1686 poll_wait(file, &le->wait, wait);
1688 if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1689 events = EPOLLIN | EPOLLRDNORM;
1694 struct compat_gpioeevent_data {
1695 compat_u64 timestamp;
1699 static ssize_t lineevent_read(struct file *file,
1704 struct lineevent_state *le = file->private_data;
1705 struct gpioevent_data ge;
1706 ssize_t bytes_read = 0;
1711 * When compatible system call is being used the struct gpioevent_data,
1712 * in case of at least ia32, has different size due to the alignment
1713 * differences. Because we have first member 64 bits followed by one of
1714 * 32 bits there is no gap between them. The only difference is the
1715 * padding at the end of the data structure. Hence, we calculate the
1716 * actual sizeof() and pass this as an argument to copy_to_user() to
1717 * drop unneeded bytes from the output.
1719 if (compat_need_64bit_alignment_fixup())
1720 ge_size = sizeof(struct compat_gpioeevent_data);
1722 ge_size = sizeof(struct gpioevent_data);
1723 if (count < ge_size)
1727 spin_lock(&le->wait.lock);
1728 if (kfifo_is_empty(&le->events)) {
1730 spin_unlock(&le->wait.lock);
1734 if (file->f_flags & O_NONBLOCK) {
1735 spin_unlock(&le->wait.lock);
1739 ret = wait_event_interruptible_locked(le->wait,
1740 !kfifo_is_empty(&le->events));
1742 spin_unlock(&le->wait.lock);
1747 ret = kfifo_out(&le->events, &ge, 1);
1748 spin_unlock(&le->wait.lock);
1751 * This should never happen - we were holding the lock
1752 * from the moment we learned the fifo is no longer
1759 if (copy_to_user(buf + bytes_read, &ge, ge_size))
1761 bytes_read += ge_size;
1762 } while (count >= bytes_read + ge_size);
1767 static void lineevent_free(struct lineevent_state *le)
1770 free_irq(le->irq, le);
1772 gpiod_free(le->desc);
1774 put_device(&le->gdev->dev);
1778 static int lineevent_release(struct inode *inode, struct file *file)
1780 lineevent_free(file->private_data);
1784 static long lineevent_ioctl(struct file *file, unsigned int cmd,
1787 struct lineevent_state *le = file->private_data;
1788 void __user *ip = (void __user *)arg;
1789 struct gpiohandle_data ghd;
1792 * We can get the value for an event line but not set it,
1793 * because it is input by definition.
1795 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
1798 memset(&ghd, 0, sizeof(ghd));
1800 val = gpiod_get_value_cansleep(le->desc);
1803 ghd.values[0] = val;
1805 if (copy_to_user(ip, &ghd, sizeof(ghd)))
1813 #ifdef CONFIG_COMPAT
1814 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
1817 return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1821 static const struct file_operations lineevent_fileops = {
1822 .release = lineevent_release,
1823 .read = lineevent_read,
1824 .poll = lineevent_poll,
1825 .owner = THIS_MODULE,
1826 .llseek = noop_llseek,
1827 .unlocked_ioctl = lineevent_ioctl,
1828 #ifdef CONFIG_COMPAT
1829 .compat_ioctl = lineevent_ioctl_compat,
1833 static irqreturn_t lineevent_irq_thread(int irq, void *p)
1835 struct lineevent_state *le = p;
1836 struct gpioevent_data ge;
1839 /* Do not leak kernel stack to userspace */
1840 memset(&ge, 0, sizeof(ge));
1843 * We may be running from a nested threaded interrupt in which case
1844 * we didn't get the timestamp from lineevent_irq_handler().
1847 ge.timestamp = ktime_get_ns();
1849 ge.timestamp = le->timestamp;
1851 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
1852 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1853 int level = gpiod_get_value_cansleep(le->desc);
1856 /* Emit low-to-high event */
1857 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1859 /* Emit high-to-low event */
1860 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1861 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
1862 /* Emit low-to-high event */
1863 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1864 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1865 /* Emit high-to-low event */
1866 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1871 ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
1874 wake_up_poll(&le->wait, EPOLLIN);
1876 pr_debug_ratelimited("event FIFO is full - event dropped\n");
1881 static irqreturn_t lineevent_irq_handler(int irq, void *p)
1883 struct lineevent_state *le = p;
1886 * Just store the timestamp in hardirq context so we get it as
1887 * close in time as possible to the actual event.
1889 le->timestamp = ktime_get_ns();
1891 return IRQ_WAKE_THREAD;
1894 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1896 struct gpioevent_request eventreq;
1897 struct lineevent_state *le;
1898 struct gpio_desc *desc;
1905 int irq, irqflags = 0;
1907 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1910 offset = eventreq.lineoffset;
1911 lflags = eventreq.handleflags;
1912 eflags = eventreq.eventflags;
1914 desc = gpiochip_get_desc(gdev->chip, offset);
1916 return PTR_ERR(desc);
1918 /* Return an error if a unknown flag is set */
1919 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1920 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1923 /* This is just wrong: we don't look for events on output lines */
1924 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1925 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1926 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1929 /* Only one bias flag can be set. */
1930 if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1931 (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1932 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1933 ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1934 (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1937 le = kzalloc(sizeof(*le), GFP_KERNEL);
1941 get_device(&gdev->dev);
1943 if (eventreq.consumer_label[0] != '\0') {
1944 /* label is only initialized if consumer_label is set */
1945 le->label = kstrndup(eventreq.consumer_label,
1946 sizeof(eventreq.consumer_label) - 1,
1954 ret = gpiod_request_user(desc, le->label);
1958 le->eflags = eflags;
1960 linehandle_flags_to_desc_flags(lflags, &desc->flags);
1962 ret = gpiod_direction_input(desc);
1966 blocking_notifier_call_chain(&desc->gdev->notifier,
1967 GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1969 irq = gpiod_to_irq(desc);
1976 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
1977 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1978 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1979 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
1980 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1981 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1982 irqflags |= IRQF_ONESHOT;
1984 INIT_KFIFO(le->events);
1985 init_waitqueue_head(&le->wait);
1987 /* Request a thread to read the events */
1988 ret = request_threaded_irq(le->irq,
1989 lineevent_irq_handler,
1990 lineevent_irq_thread,
1997 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
2003 file = anon_inode_getfile("gpio-event",
2006 O_RDONLY | O_CLOEXEC);
2008 ret = PTR_ERR(file);
2009 goto out_put_unused_fd;
2013 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
2015 * fput() will trigger the release() callback, so do not go onto
2016 * the regular error cleanup path here.
2023 fd_install(fd, file);
2034 static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
2035 struct gpioline_info *info_v1)
2037 u64 flagsv2 = info_v2->flags;
2039 memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
2040 memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
2041 info_v1->line_offset = info_v2->offset;
2044 if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
2045 info_v1->flags |= GPIOLINE_FLAG_KERNEL;
2047 if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
2048 info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
2050 if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
2051 info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
2053 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
2054 info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
2055 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
2056 info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
2058 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
2059 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
2060 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
2061 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
2062 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
2063 info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
2066 static void gpio_v2_line_info_changed_to_v1(
2067 struct gpio_v2_line_info_changed *lic_v2,
2068 struct gpioline_info_changed *lic_v1)
2070 memset(lic_v1, 0, sizeof(*lic_v1));
2071 gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
2072 lic_v1->timestamp = lic_v2->timestamp_ns;
2073 lic_v1->event_type = lic_v2->event_type;
2076 #endif /* CONFIG_GPIO_CDEV_V1 */
2078 static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
2079 struct gpio_v2_line_info *info)
2081 struct gpio_chip *gc = desc->gdev->chip;
2082 bool ok_for_pinctrl;
2083 unsigned long flags;
2084 u32 debounce_period_us;
2085 unsigned int num_attrs = 0;
2087 memset(info, 0, sizeof(*info));
2088 info->offset = gpio_chip_hwgpio(desc);
2091 * This function takes a mutex so we must check this before taking
2094 * FIXME: find a non-racy way to retrieve this information. Maybe a
2095 * lock common to both frameworks?
2098 pinctrl_gpio_can_use_line(gc->base + info->offset);
2100 spin_lock_irqsave(&gpio_lock, flags);
2103 strscpy(info->name, desc->name, sizeof(info->name));
2106 strscpy(info->consumer, desc->label, sizeof(info->consumer));
2109 * Userspace only need to know that the kernel is using this GPIO so
2113 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
2114 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
2115 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
2116 test_bit(FLAG_EXPORT, &desc->flags) ||
2117 test_bit(FLAG_SYSFS, &desc->flags) ||
2118 !gpiochip_line_is_valid(gc, info->offset) ||
2120 info->flags |= GPIO_V2_LINE_FLAG_USED;
2122 if (test_bit(FLAG_IS_OUT, &desc->flags))
2123 info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
2125 info->flags |= GPIO_V2_LINE_FLAG_INPUT;
2127 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2128 info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
2130 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2131 info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
2132 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2133 info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
2135 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2136 info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
2137 if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2138 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
2139 if (test_bit(FLAG_PULL_UP, &desc->flags))
2140 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
2142 if (test_bit(FLAG_EDGE_RISING, &desc->flags))
2143 info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
2144 if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
2145 info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
2147 if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
2148 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
2149 else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags))
2150 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
2152 debounce_period_us = READ_ONCE(desc->debounce_period_us);
2153 if (debounce_period_us) {
2154 info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
2155 info->attrs[num_attrs].debounce_period_us = debounce_period_us;
2158 info->num_attrs = num_attrs;
2160 spin_unlock_irqrestore(&gpio_lock, flags);
2163 struct gpio_chardev_data {
2164 struct gpio_device *gdev;
2165 wait_queue_head_t wait;
2166 DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
2167 struct notifier_block lineinfo_changed_nb;
2168 unsigned long *watched_lines;
2169 #ifdef CONFIG_GPIO_CDEV_V1
2170 atomic_t watch_abi_version;
2174 static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
2176 struct gpio_device *gdev = cdev->gdev;
2177 struct gpiochip_info chipinfo;
2179 memset(&chipinfo, 0, sizeof(chipinfo));
2181 strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
2182 strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
2183 chipinfo.lines = gdev->ngpio;
2184 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
2189 #ifdef CONFIG_GPIO_CDEV_V1
2191 * returns 0 if the versions match, else the previously selected ABI version
2193 static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
2194 unsigned int version)
2196 int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
2198 if (abiv == version)
2204 static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
2207 struct gpio_desc *desc;
2208 struct gpioline_info lineinfo;
2209 struct gpio_v2_line_info lineinfo_v2;
2211 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2214 /* this doubles as a range check on line_offset */
2215 desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset);
2217 return PTR_ERR(desc);
2220 if (lineinfo_ensure_abi_version(cdev, 1))
2223 if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
2227 gpio_desc_to_lineinfo(desc, &lineinfo_v2);
2228 gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
2230 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2232 clear_bit(lineinfo.line_offset, cdev->watched_lines);
2240 static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
2243 struct gpio_desc *desc;
2244 struct gpio_v2_line_info lineinfo;
2246 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2249 if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2252 desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2254 return PTR_ERR(desc);
2257 #ifdef CONFIG_GPIO_CDEV_V1
2258 if (lineinfo_ensure_abi_version(cdev, 2))
2261 if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2264 gpio_desc_to_lineinfo(desc, &lineinfo);
2266 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2268 clear_bit(lineinfo.offset, cdev->watched_lines);
2275 static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
2279 if (copy_from_user(&offset, ip, sizeof(offset)))
2282 if (offset >= cdev->gdev->ngpio)
2285 if (!test_and_clear_bit(offset, cdev->watched_lines))
2292 * gpio_ioctl() - ioctl handler for the GPIO chardev
2294 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2296 struct gpio_chardev_data *cdev = file->private_data;
2297 struct gpio_device *gdev = cdev->gdev;
2298 void __user *ip = (void __user *)arg;
2300 /* We fail any subsequent ioctl():s when the chip is gone */
2304 /* Fill in the struct and pass to userspace */
2306 case GPIO_GET_CHIPINFO_IOCTL:
2307 return chipinfo_get(cdev, ip);
2308 #ifdef CONFIG_GPIO_CDEV_V1
2309 case GPIO_GET_LINEHANDLE_IOCTL:
2310 return linehandle_create(gdev, ip);
2311 case GPIO_GET_LINEEVENT_IOCTL:
2312 return lineevent_create(gdev, ip);
2313 case GPIO_GET_LINEINFO_IOCTL:
2314 return lineinfo_get_v1(cdev, ip, false);
2315 case GPIO_GET_LINEINFO_WATCH_IOCTL:
2316 return lineinfo_get_v1(cdev, ip, true);
2317 #endif /* CONFIG_GPIO_CDEV_V1 */
2318 case GPIO_V2_GET_LINEINFO_IOCTL:
2319 return lineinfo_get(cdev, ip, false);
2320 case GPIO_V2_GET_LINEINFO_WATCH_IOCTL:
2321 return lineinfo_get(cdev, ip, true);
2322 case GPIO_V2_GET_LINE_IOCTL:
2323 return linereq_create(gdev, ip);
2324 case GPIO_GET_LINEINFO_UNWATCH_IOCTL:
2325 return lineinfo_unwatch(cdev, ip);
2331 #ifdef CONFIG_COMPAT
2332 static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2335 return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2339 static struct gpio_chardev_data *
2340 to_gpio_chardev_data(struct notifier_block *nb)
2342 return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2345 static int lineinfo_changed_notify(struct notifier_block *nb,
2346 unsigned long action, void *data)
2348 struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
2349 struct gpio_v2_line_info_changed chg;
2350 struct gpio_desc *desc = data;
2353 if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2356 memset(&chg, 0, sizeof(chg));
2357 chg.event_type = action;
2358 chg.timestamp_ns = ktime_get_ns();
2359 gpio_desc_to_lineinfo(desc, &chg.info);
2361 ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2363 wake_up_poll(&cdev->wait, EPOLLIN);
2365 pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2370 static __poll_t lineinfo_watch_poll(struct file *file,
2371 struct poll_table_struct *pollt)
2373 struct gpio_chardev_data *cdev = file->private_data;
2374 __poll_t events = 0;
2376 poll_wait(file, &cdev->wait, pollt);
2378 if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2380 events = EPOLLIN | EPOLLRDNORM;
2385 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2386 size_t count, loff_t *off)
2388 struct gpio_chardev_data *cdev = file->private_data;
2389 struct gpio_v2_line_info_changed event;
2390 ssize_t bytes_read = 0;
2394 #ifndef CONFIG_GPIO_CDEV_V1
2395 event_size = sizeof(struct gpio_v2_line_info_changed);
2396 if (count < event_size)
2401 spin_lock(&cdev->wait.lock);
2402 if (kfifo_is_empty(&cdev->events)) {
2404 spin_unlock(&cdev->wait.lock);
2408 if (file->f_flags & O_NONBLOCK) {
2409 spin_unlock(&cdev->wait.lock);
2413 ret = wait_event_interruptible_locked(cdev->wait,
2414 !kfifo_is_empty(&cdev->events));
2416 spin_unlock(&cdev->wait.lock);
2420 #ifdef CONFIG_GPIO_CDEV_V1
2421 /* must be after kfifo check so watch_abi_version is set */
2422 if (atomic_read(&cdev->watch_abi_version) == 2)
2423 event_size = sizeof(struct gpio_v2_line_info_changed);
2425 event_size = sizeof(struct gpioline_info_changed);
2426 if (count < event_size) {
2427 spin_unlock(&cdev->wait.lock);
2431 ret = kfifo_out(&cdev->events, &event, 1);
2432 spin_unlock(&cdev->wait.lock);
2436 /* We should never get here. See lineevent_read(). */
2439 #ifdef CONFIG_GPIO_CDEV_V1
2440 if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2441 if (copy_to_user(buf + bytes_read, &event, event_size))
2444 struct gpioline_info_changed event_v1;
2446 gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2447 if (copy_to_user(buf + bytes_read, &event_v1,
2452 if (copy_to_user(buf + bytes_read, &event, event_size))
2455 bytes_read += event_size;
2456 } while (count >= bytes_read + sizeof(event));
2462 * gpio_chrdev_open() - open the chardev for ioctl operations
2463 * @inode: inode for this chardev
2464 * @file: file struct for storing private data
2465 * Returns 0 on success
2467 static int gpio_chrdev_open(struct inode *inode, struct file *file)
2469 struct gpio_device *gdev = container_of(inode->i_cdev,
2470 struct gpio_device, chrdev);
2471 struct gpio_chardev_data *cdev;
2474 /* Fail on open if the backing gpiochip is gone */
2478 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2482 cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2483 if (!cdev->watched_lines)
2486 init_waitqueue_head(&cdev->wait);
2487 INIT_KFIFO(cdev->events);
2490 cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
2491 ret = blocking_notifier_chain_register(&gdev->notifier,
2492 &cdev->lineinfo_changed_nb);
2494 goto out_free_bitmap;
2496 get_device(&gdev->dev);
2497 file->private_data = cdev;
2499 ret = nonseekable_open(inode, file);
2501 goto out_unregister_notifier;
2505 out_unregister_notifier:
2506 blocking_notifier_chain_unregister(&gdev->notifier,
2507 &cdev->lineinfo_changed_nb);
2509 bitmap_free(cdev->watched_lines);
2516 * gpio_chrdev_release() - close chardev after ioctl operations
2517 * @inode: inode for this chardev
2518 * @file: file struct for storing private data
2519 * Returns 0 on success
2521 static int gpio_chrdev_release(struct inode *inode, struct file *file)
2523 struct gpio_chardev_data *cdev = file->private_data;
2524 struct gpio_device *gdev = cdev->gdev;
2526 bitmap_free(cdev->watched_lines);
2527 blocking_notifier_chain_unregister(&gdev->notifier,
2528 &cdev->lineinfo_changed_nb);
2529 put_device(&gdev->dev);
2535 static const struct file_operations gpio_fileops = {
2536 .release = gpio_chrdev_release,
2537 .open = gpio_chrdev_open,
2538 .poll = lineinfo_watch_poll,
2539 .read = lineinfo_watch_read,
2540 .owner = THIS_MODULE,
2541 .llseek = no_llseek,
2542 .unlocked_ioctl = gpio_ioctl,
2543 #ifdef CONFIG_COMPAT
2544 .compat_ioctl = gpio_ioctl_compat,
2548 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2552 cdev_init(&gdev->chrdev, &gpio_fileops);
2553 gdev->chrdev.owner = THIS_MODULE;
2554 gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2556 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2560 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2561 MAJOR(devt), gdev->id);
2566 void gpiolib_cdev_unregister(struct gpio_device *gdev)
2568 cdev_device_del(&gdev->chrdev, &gdev->dev);