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
424 * @hdesc: the Hardware Timestamp Engine (HTE) descriptor
425 * @raw_level: the line level at the time of event
426 * @total_discard_seq: the running counter of the discarded events
427 * @last_seqno: the last sequence number before debounce period expires
430 struct gpio_desc *desc;
432 * -- edge detector specific fields --
437 * eflags is set by edge_detector_setup(), edge_detector_stop() and
438 * edge_detector_update(), which are themselves mutually exclusive,
439 * and is accessed by edge_irq_thread() and debounce_work_func(),
440 * which can both live with a slightly stale value.
444 * timestamp_ns and req_seqno are accessed only by
445 * edge_irq_handler() and edge_irq_thread(), which are themselves
446 * mutually exclusive, so no additional protection is necessary.
451 * line_seqno is accessed by either edge_irq_thread() or
452 * debounce_work_func(), which are themselves mutually exclusive,
453 * so no additional protection is necessary.
457 * -- debouncer specific fields --
459 struct delayed_work work;
461 * sw_debounce is accessed by linereq_set_config(), which is the
462 * only setter, and linereq_get_values(), which can live with a
463 * slightly stale value.
465 unsigned int sw_debounced;
467 * level is accessed by debounce_work_func(), which is the only
468 * setter, and linereq_get_values() which can live with a slightly
473 * -- hte specific fields --
475 struct hte_ts_desc hdesc;
477 * HTE provider sets line level at the time of event. The valid
478 * value is 0 or 1 and negative value for an error.
482 * when sw_debounce is set on HTE enabled line, this is running
483 * counter of the discarded events.
485 u32 total_discard_seq;
487 * when sw_debounce is set on HTE enabled line, this variable records
488 * last sequence number before debounce period expires.
494 * struct linereq - contains the state of a userspace line request
495 * @gdev: the GPIO device the line request pertains to
496 * @label: consumer label used to tag GPIO descriptors
497 * @num_lines: the number of lines in the lines array
498 * @wait: wait queue that handles blocking reads of events
499 * @event_buffer_size: the number of elements allocated in @events
500 * @events: KFIFO for the GPIO events
501 * @seqno: the sequence number for edge events generated on all lines in
502 * this line request. Note that this is not used when @num_lines is 1, as
503 * the line_seqno is then the same and is cheaper to calculate.
504 * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
505 * of configuration, particularly multi-step accesses to desc flags.
506 * @lines: the lines held by this line request, with @num_lines elements.
509 struct gpio_device *gdev;
512 wait_queue_head_t wait;
513 u32 event_buffer_size;
514 DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
516 struct mutex config_mutex;
520 #define GPIO_V2_LINE_BIAS_FLAGS \
521 (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
522 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
523 GPIO_V2_LINE_FLAG_BIAS_DISABLED)
525 #define GPIO_V2_LINE_DIRECTION_FLAGS \
526 (GPIO_V2_LINE_FLAG_INPUT | \
527 GPIO_V2_LINE_FLAG_OUTPUT)
529 #define GPIO_V2_LINE_DRIVE_FLAGS \
530 (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
531 GPIO_V2_LINE_FLAG_OPEN_SOURCE)
533 #define GPIO_V2_LINE_EDGE_FLAGS \
534 (GPIO_V2_LINE_FLAG_EDGE_RISING | \
535 GPIO_V2_LINE_FLAG_EDGE_FALLING)
537 #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS
539 #define GPIO_V2_LINE_VALID_FLAGS \
540 (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
541 GPIO_V2_LINE_DIRECTION_FLAGS | \
542 GPIO_V2_LINE_DRIVE_FLAGS | \
543 GPIO_V2_LINE_EDGE_FLAGS | \
544 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
545 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
546 GPIO_V2_LINE_BIAS_FLAGS)
548 static void linereq_put_event(struct linereq *lr,
549 struct gpio_v2_line_event *le)
551 bool overflow = false;
553 spin_lock(&lr->wait.lock);
554 if (kfifo_is_full(&lr->events)) {
556 kfifo_skip(&lr->events);
558 kfifo_in(&lr->events, le, 1);
559 spin_unlock(&lr->wait.lock);
561 wake_up_poll(&lr->wait, EPOLLIN);
563 pr_debug_ratelimited("event FIFO is full - event dropped\n");
566 static u64 line_event_timestamp(struct line *line)
568 if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
569 return ktime_get_real_ns();
570 else if (test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))
571 return line->timestamp_ns;
573 return ktime_get_ns();
576 static enum hte_return process_hw_ts_thread(void *p)
580 struct gpio_v2_line_event le;
585 return HTE_CB_HANDLED;
590 memset(&le, 0, sizeof(le));
592 le.timestamp_ns = line->timestamp_ns;
593 eflags = READ_ONCE(line->eflags);
595 if (eflags == GPIO_V2_LINE_FLAG_EDGE_BOTH) {
596 if (line->raw_level >= 0) {
597 if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
598 level = !line->raw_level;
600 level = line->raw_level;
602 level = gpiod_get_value_cansleep(line->desc);
606 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
608 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
609 } else if (eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) {
610 /* Emit low-to-high event */
611 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
612 } else if (eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) {
613 /* Emit high-to-low event */
614 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
616 return HTE_CB_HANDLED;
618 le.line_seqno = line->line_seqno;
619 le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
620 le.offset = gpio_chip_hwgpio(line->desc);
622 linereq_put_event(lr, &le);
624 return HTE_CB_HANDLED;
627 static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p)
634 return HTE_CB_HANDLED;
637 line->timestamp_ns = ts->tsc;
638 line->raw_level = ts->raw_level;
641 if (READ_ONCE(line->sw_debounced)) {
642 line->total_discard_seq++;
643 line->last_seqno = ts->seq;
644 mod_delayed_work(system_wq, &line->work,
645 usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
647 if (unlikely(ts->seq < line->line_seqno))
648 return HTE_CB_HANDLED;
650 diff_seqno = ts->seq - line->line_seqno;
651 line->line_seqno = ts->seq;
652 if (lr->num_lines != 1)
653 line->req_seqno = atomic_add_return(diff_seqno,
656 return HTE_RUN_SECOND_CB;
659 return HTE_CB_HANDLED;
662 static irqreturn_t edge_irq_thread(int irq, void *p)
664 struct line *line = p;
665 struct linereq *lr = line->req;
666 struct gpio_v2_line_event le;
669 /* Do not leak kernel stack to userspace */
670 memset(&le, 0, sizeof(le));
672 if (line->timestamp_ns) {
673 le.timestamp_ns = line->timestamp_ns;
676 * We may be running from a nested threaded interrupt in
677 * which case we didn't get the timestamp from
678 * edge_irq_handler().
680 le.timestamp_ns = line_event_timestamp(line);
681 if (lr->num_lines != 1)
682 line->req_seqno = atomic_inc_return(&lr->seqno);
684 line->timestamp_ns = 0;
686 eflags = READ_ONCE(line->eflags);
687 if (eflags == GPIO_V2_LINE_FLAG_EDGE_BOTH) {
688 int level = gpiod_get_value_cansleep(line->desc);
691 /* Emit low-to-high event */
692 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
694 /* Emit high-to-low event */
695 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
696 } else if (eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) {
697 /* Emit low-to-high event */
698 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
699 } else if (eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) {
700 /* Emit high-to-low event */
701 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
706 le.line_seqno = line->line_seqno;
707 le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
708 le.offset = gpio_chip_hwgpio(line->desc);
710 linereq_put_event(lr, &le);
715 static irqreturn_t edge_irq_handler(int irq, void *p)
717 struct line *line = p;
718 struct linereq *lr = line->req;
721 * Just store the timestamp in hardirq context so we get it as
722 * close in time as possible to the actual event.
724 line->timestamp_ns = line_event_timestamp(line);
726 if (lr->num_lines != 1)
727 line->req_seqno = atomic_inc_return(&lr->seqno);
729 return IRQ_WAKE_THREAD;
733 * returns the current debounced logical value.
735 static bool debounced_value(struct line *line)
740 * minor race - debouncer may be stopped here, so edge_detector_stop()
741 * must leave the value unchanged so the following will read the level
742 * from when the debouncer was last running.
744 value = READ_ONCE(line->level);
746 if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
752 static irqreturn_t debounce_irq_handler(int irq, void *p)
754 struct line *line = p;
756 mod_delayed_work(system_wq, &line->work,
757 usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
762 static void debounce_work_func(struct work_struct *work)
764 struct gpio_v2_line_event le;
765 struct line *line = container_of(work, struct line, work.work);
767 int level, diff_seqno;
770 if (test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags)) {
771 level = line->raw_level;
773 level = gpiod_get_raw_value_cansleep(line->desc);
775 level = gpiod_get_raw_value_cansleep(line->desc);
778 pr_debug_ratelimited("debouncer failed to read line value\n");
782 if (READ_ONCE(line->level) == level)
785 WRITE_ONCE(line->level, level);
787 /* -- edge detection -- */
788 eflags = READ_ONCE(line->eflags);
792 /* switch from physical level to logical - if they differ */
793 if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
796 /* ignore edges that are not being monitored */
797 if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
798 ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
801 /* Do not leak kernel stack to userspace */
802 memset(&le, 0, sizeof(le));
805 le.timestamp_ns = line_event_timestamp(line);
806 le.offset = gpio_chip_hwgpio(line->desc);
807 if (test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags)) {
808 /* discard events except the last one */
809 line->total_discard_seq -= 1;
810 diff_seqno = line->last_seqno - line->total_discard_seq -
812 line->line_seqno = line->last_seqno - line->total_discard_seq;
813 le.line_seqno = line->line_seqno;
814 le.seqno = (lr->num_lines == 1) ?
815 le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno);
818 le.line_seqno = line->line_seqno;
819 le.seqno = (lr->num_lines == 1) ?
820 le.line_seqno : atomic_inc_return(&lr->seqno);
824 /* Emit low-to-high event */
825 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
827 /* Emit high-to-low event */
828 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
830 linereq_put_event(lr, &le);
833 static int hte_edge_setup(struct line *line, u64 eflags)
836 unsigned long flags = 0;
837 struct hte_ts_desc *hdesc = &line->hdesc;
839 if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
840 flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
841 HTE_FALLING_EDGE_TS : HTE_RISING_EDGE_TS;
842 if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
843 flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
844 HTE_RISING_EDGE_TS : HTE_FALLING_EDGE_TS;
846 line->total_discard_seq = 0;
848 hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags,
851 ret = hte_ts_get(NULL, hdesc, 0);
855 return hte_request_ts_ns(hdesc, process_hw_ts,
856 process_hw_ts_thread, line);
859 static int debounce_setup(struct line *line,
860 unsigned int debounce_period_us, bool hte_req)
862 unsigned long irqflags;
866 ret = gpiod_set_debounce(line->desc, debounce_period_us);
868 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
871 if (ret != -ENOTSUPP)
874 if (debounce_period_us) {
875 /* setup software debounce */
876 level = gpiod_get_raw_value_cansleep(line->desc);
881 irq = gpiod_to_irq(line->desc);
885 irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
886 ret = request_irq(irq, debounce_irq_handler, irqflags,
887 line->req->label, line);
892 ret = hte_edge_setup(line,
893 GPIO_V2_LINE_FLAG_EDGE_RISING |
894 GPIO_V2_LINE_FLAG_EDGE_FALLING);
899 WRITE_ONCE(line->level, level);
900 WRITE_ONCE(line->sw_debounced, 1);
905 static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
906 unsigned int line_idx)
909 u64 mask = BIT_ULL(line_idx);
911 for (i = 0; i < lc->num_attrs; i++) {
912 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
913 (lc->attrs[i].mask & mask))
919 static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
920 unsigned int line_idx)
923 u64 mask = BIT_ULL(line_idx);
925 for (i = 0; i < lc->num_attrs; i++) {
926 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
927 (lc->attrs[i].mask & mask))
928 return lc->attrs[i].attr.debounce_period_us;
933 static void edge_detector_stop(struct line *line, bool hte_en)
935 if (line->irq && !hte_en) {
936 free_irq(line->irq, line);
941 hte_ts_put(&line->hdesc);
943 cancel_delayed_work_sync(&line->work);
944 WRITE_ONCE(line->sw_debounced, 0);
945 WRITE_ONCE(line->eflags, 0);
947 WRITE_ONCE(line->desc->debounce_period_us, 0);
948 /* do not change line->level - see comment in debounced_value() */
951 static int edge_detector_setup(struct line *line,
952 struct gpio_v2_line_config *lc,
953 unsigned int line_idx,
954 u64 eflags, bool hte_req)
956 u32 debounce_period_us;
957 unsigned long irqflags = 0;
960 if (eflags && !kfifo_initialized(&line->req->events)) {
961 ret = kfifo_alloc(&line->req->events,
962 line->req->event_buffer_size, GFP_KERNEL);
966 WRITE_ONCE(line->eflags, eflags);
967 if (gpio_v2_line_config_debounced(lc, line_idx)) {
968 debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
969 ret = debounce_setup(line, debounce_period_us, hte_req);
972 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
975 /* detection disabled or sw debouncer will provide edge detection */
976 if (!eflags || READ_ONCE(line->sw_debounced))
980 return hte_edge_setup(line, eflags);
982 irq = gpiod_to_irq(line->desc);
986 if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
987 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
988 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
989 if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
990 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
991 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
992 irqflags |= IRQF_ONESHOT;
994 /* Request a thread to read the events */
995 ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
996 irqflags, line->req->label, line);
1004 static int edge_detector_update(struct line *line,
1005 struct gpio_v2_line_config *lc,
1006 unsigned int line_idx,
1007 u64 flags, bool polarity_change,
1010 u64 eflags = flags & GPIO_V2_LINE_EDGE_FLAGS;
1011 unsigned int debounce_period_us =
1012 gpio_v2_line_config_debounce_period(lc, line_idx);
1013 bool hte_change = (prev_hte_flag !=
1014 ((flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) != 0));
1016 if ((READ_ONCE(line->eflags) == eflags) && !polarity_change &&
1017 (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us)
1021 /* sw debounced and still will be...*/
1022 if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
1023 WRITE_ONCE(line->eflags, eflags);
1024 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
1028 /* reconfiguring edge detection or sw debounce being disabled */
1029 if ((line->irq && !READ_ONCE(line->sw_debounced)) || prev_hte_flag ||
1030 (!debounce_period_us && READ_ONCE(line->sw_debounced)))
1031 edge_detector_stop(line, prev_hte_flag);
1033 return edge_detector_setup(line, lc, line_idx, eflags,
1034 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
1037 static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
1038 unsigned int line_idx)
1041 u64 mask = BIT_ULL(line_idx);
1043 for (i = 0; i < lc->num_attrs; i++) {
1044 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
1045 (lc->attrs[i].mask & mask))
1046 return lc->attrs[i].attr.flags;
1051 static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
1052 unsigned int line_idx)
1055 u64 mask = BIT_ULL(line_idx);
1057 for (i = 0; i < lc->num_attrs; i++) {
1058 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
1059 (lc->attrs[i].mask & mask))
1060 return !!(lc->attrs[i].attr.values & mask);
1065 static int gpio_v2_line_flags_validate(u64 flags)
1067 /* Return an error if an unknown flag is set */
1068 if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
1071 * Do not allow both INPUT and OUTPUT flags to be set as they are
1074 if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
1075 (flags & GPIO_V2_LINE_FLAG_OUTPUT))
1078 /* Only allow one event clock source */
1079 if ((flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) &&
1080 (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1083 /* Edge detection requires explicit input. */
1084 if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
1085 !(flags & GPIO_V2_LINE_FLAG_INPUT))
1089 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
1090 * request. If the hardware actually supports enabling both at the
1091 * same time the electrical result would be disastrous.
1093 if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
1094 (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
1097 /* Drive requires explicit output direction. */
1098 if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
1099 !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
1102 /* Bias requires explicit direction. */
1103 if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
1104 !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
1107 /* Only one bias flag can be set. */
1108 if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
1109 (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
1110 GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
1111 ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
1112 (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
1118 static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
1119 unsigned int num_lines)
1125 if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
1128 if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
1131 for (i = 0; i < num_lines; i++) {
1132 flags = gpio_v2_line_config_flags(lc, i);
1133 ret = gpio_v2_line_flags_validate(flags);
1137 /* debounce requires explicit input */
1138 if (gpio_v2_line_config_debounced(lc, i) &&
1139 !(flags & GPIO_V2_LINE_FLAG_INPUT))
1145 static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
1146 unsigned long *flagsp)
1148 assign_bit(FLAG_ACTIVE_LOW, flagsp,
1149 flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
1151 if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
1152 set_bit(FLAG_IS_OUT, flagsp);
1153 else if (flags & GPIO_V2_LINE_FLAG_INPUT)
1154 clear_bit(FLAG_IS_OUT, flagsp);
1156 assign_bit(FLAG_EDGE_RISING, flagsp,
1157 flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
1158 assign_bit(FLAG_EDGE_FALLING, flagsp,
1159 flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
1161 assign_bit(FLAG_OPEN_DRAIN, flagsp,
1162 flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
1163 assign_bit(FLAG_OPEN_SOURCE, flagsp,
1164 flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
1166 assign_bit(FLAG_PULL_UP, flagsp,
1167 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
1168 assign_bit(FLAG_PULL_DOWN, flagsp,
1169 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
1170 assign_bit(FLAG_BIAS_DISABLE, flagsp,
1171 flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
1173 assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
1174 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
1175 assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp,
1176 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
1179 static long linereq_get_values(struct linereq *lr, void __user *ip)
1181 struct gpio_v2_line_values lv;
1182 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
1183 struct gpio_desc **descs;
1184 unsigned int i, didx, num_get;
1188 /* NOTE: It's ok to read values of output lines. */
1189 if (copy_from_user(&lv, ip, sizeof(lv)))
1192 for (num_get = 0, i = 0; i < lr->num_lines; i++) {
1193 if (lv.mask & BIT_ULL(i)) {
1195 descs = &lr->lines[i].desc;
1203 descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
1206 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1207 if (lv.mask & BIT_ULL(i)) {
1208 descs[didx] = lr->lines[i].desc;
1213 ret = gpiod_get_array_value_complex(false, true, num_get,
1222 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1223 if (lv.mask & BIT_ULL(i)) {
1224 if (lr->lines[i].sw_debounced)
1225 val = debounced_value(&lr->lines[i]);
1227 val = test_bit(didx, vals);
1229 lv.bits |= BIT_ULL(i);
1234 if (copy_to_user(ip, &lv, sizeof(lv)))
1240 static long linereq_set_values_unlocked(struct linereq *lr,
1241 struct gpio_v2_line_values *lv)
1243 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
1244 struct gpio_desc **descs;
1245 unsigned int i, didx, num_set;
1248 bitmap_zero(vals, GPIO_V2_LINES_MAX);
1249 for (num_set = 0, i = 0; i < lr->num_lines; i++) {
1250 if (lv->mask & BIT_ULL(i)) {
1251 if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
1253 if (lv->bits & BIT_ULL(i))
1254 __set_bit(num_set, vals);
1256 descs = &lr->lines[i].desc;
1263 /* build compacted desc array and values */
1264 descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
1267 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1268 if (lv->mask & BIT_ULL(i)) {
1269 descs[didx] = lr->lines[i].desc;
1274 ret = gpiod_set_array_value_complex(false, true, num_set,
1282 static long linereq_set_values(struct linereq *lr, void __user *ip)
1284 struct gpio_v2_line_values lv;
1287 if (copy_from_user(&lv, ip, sizeof(lv)))
1290 mutex_lock(&lr->config_mutex);
1292 ret = linereq_set_values_unlocked(lr, &lv);
1294 mutex_unlock(&lr->config_mutex);
1299 static long linereq_set_config_unlocked(struct linereq *lr,
1300 struct gpio_v2_line_config *lc)
1302 struct gpio_desc *desc;
1305 bool polarity_change;
1309 for (i = 0; i < lr->num_lines; i++) {
1310 desc = lr->lines[i].desc;
1311 flags = gpio_v2_line_config_flags(lc, i);
1313 (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) !=
1314 ((flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) != 0));
1316 prev_hte_flag = !!test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags);
1318 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1320 * Lines have to be requested explicitly for input
1321 * or output, else the line will be treated "as is".
1323 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1324 int val = gpio_v2_line_config_output_value(lc, i);
1326 edge_detector_stop(&lr->lines[i], prev_hte_flag);
1327 ret = gpiod_direction_output(desc, val);
1330 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1331 ret = gpiod_direction_input(desc);
1335 ret = edge_detector_update(&lr->lines[i], lc, i,
1336 flags, polarity_change, prev_hte_flag);
1341 blocking_notifier_call_chain(&desc->gdev->notifier,
1342 GPIO_V2_LINE_CHANGED_CONFIG,
1348 static long linereq_set_config(struct linereq *lr, void __user *ip)
1350 struct gpio_v2_line_config lc;
1353 if (copy_from_user(&lc, ip, sizeof(lc)))
1356 ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
1360 mutex_lock(&lr->config_mutex);
1362 ret = linereq_set_config_unlocked(lr, &lc);
1364 mutex_unlock(&lr->config_mutex);
1369 static long linereq_ioctl(struct file *file, unsigned int cmd,
1372 struct linereq *lr = file->private_data;
1373 void __user *ip = (void __user *)arg;
1376 case GPIO_V2_LINE_GET_VALUES_IOCTL:
1377 return linereq_get_values(lr, ip);
1378 case GPIO_V2_LINE_SET_VALUES_IOCTL:
1379 return linereq_set_values(lr, ip);
1380 case GPIO_V2_LINE_SET_CONFIG_IOCTL:
1381 return linereq_set_config(lr, ip);
1387 #ifdef CONFIG_COMPAT
1388 static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
1391 return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1395 static __poll_t linereq_poll(struct file *file,
1396 struct poll_table_struct *wait)
1398 struct linereq *lr = file->private_data;
1399 __poll_t events = 0;
1401 poll_wait(file, &lr->wait, wait);
1403 if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
1405 events = EPOLLIN | EPOLLRDNORM;
1410 static ssize_t linereq_read(struct file *file,
1415 struct linereq *lr = file->private_data;
1416 struct gpio_v2_line_event le;
1417 ssize_t bytes_read = 0;
1420 if (count < sizeof(le))
1424 spin_lock(&lr->wait.lock);
1425 if (kfifo_is_empty(&lr->events)) {
1427 spin_unlock(&lr->wait.lock);
1431 if (file->f_flags & O_NONBLOCK) {
1432 spin_unlock(&lr->wait.lock);
1436 ret = wait_event_interruptible_locked(lr->wait,
1437 !kfifo_is_empty(&lr->events));
1439 spin_unlock(&lr->wait.lock);
1444 ret = kfifo_out(&lr->events, &le, 1);
1445 spin_unlock(&lr->wait.lock);
1448 * This should never happen - we were holding the
1449 * lock from the moment we learned the fifo is no
1450 * longer empty until now.
1456 if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
1458 bytes_read += sizeof(le);
1459 } while (count >= bytes_read + sizeof(le));
1464 static void linereq_free(struct linereq *lr)
1469 for (i = 0; i < lr->num_lines; i++) {
1470 if (lr->lines[i].desc)
1471 hte = !!test_bit(FLAG_EVENT_CLOCK_HTE,
1472 &lr->lines[i].desc->flags);
1473 edge_detector_stop(&lr->lines[i], hte);
1474 if (lr->lines[i].desc)
1475 gpiod_free(lr->lines[i].desc);
1477 kfifo_free(&lr->events);
1479 put_device(&lr->gdev->dev);
1483 static int linereq_release(struct inode *inode, struct file *file)
1485 struct linereq *lr = file->private_data;
1491 static const struct file_operations line_fileops = {
1492 .release = linereq_release,
1493 .read = linereq_read,
1494 .poll = linereq_poll,
1495 .owner = THIS_MODULE,
1496 .llseek = noop_llseek,
1497 .unlocked_ioctl = linereq_ioctl,
1498 #ifdef CONFIG_COMPAT
1499 .compat_ioctl = linereq_ioctl_compat,
1503 static int linereq_create(struct gpio_device *gdev, void __user *ip)
1505 struct gpio_v2_line_request ulr;
1506 struct gpio_v2_line_config *lc;
1513 if (copy_from_user(&ulr, ip, sizeof(ulr)))
1516 if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
1519 if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
1523 ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
1527 lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
1532 get_device(&gdev->dev);
1534 for (i = 0; i < ulr.num_lines; i++) {
1535 lr->lines[i].req = lr;
1536 WRITE_ONCE(lr->lines[i].sw_debounced, 0);
1537 INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
1540 if (ulr.consumer[0] != '\0') {
1541 /* label is only initialized if consumer is set */
1542 lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1546 goto out_free_linereq;
1550 mutex_init(&lr->config_mutex);
1551 init_waitqueue_head(&lr->wait);
1552 lr->event_buffer_size = ulr.event_buffer_size;
1553 if (lr->event_buffer_size == 0)
1554 lr->event_buffer_size = ulr.num_lines * 16;
1555 else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
1556 lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
1558 atomic_set(&lr->seqno, 0);
1559 lr->num_lines = ulr.num_lines;
1561 /* Request each GPIO */
1562 for (i = 0; i < ulr.num_lines; i++) {
1563 u32 offset = ulr.offsets[i];
1564 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
1567 ret = PTR_ERR(desc);
1568 goto out_free_linereq;
1571 ret = gpiod_request_user(desc, lr->label);
1573 goto out_free_linereq;
1575 lr->lines[i].desc = desc;
1576 flags = gpio_v2_line_config_flags(lc, i);
1577 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1579 ret = gpiod_set_transitory(desc, false);
1581 goto out_free_linereq;
1584 * Lines have to be requested explicitly for input
1585 * or output, else the line will be treated "as is".
1587 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1588 int val = gpio_v2_line_config_output_value(lc, i);
1590 ret = gpiod_direction_output(desc, val);
1592 goto out_free_linereq;
1593 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1594 ret = gpiod_direction_input(desc);
1596 goto out_free_linereq;
1598 ret = edge_detector_setup(&lr->lines[i], lc, i,
1599 flags & GPIO_V2_LINE_EDGE_FLAGS,
1600 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
1602 goto out_free_linereq;
1605 blocking_notifier_call_chain(&desc->gdev->notifier,
1606 GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1608 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
1612 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1615 goto out_free_linereq;
1618 file = anon_inode_getfile("gpio-line", &line_fileops, lr,
1619 O_RDONLY | O_CLOEXEC);
1621 ret = PTR_ERR(file);
1622 goto out_put_unused_fd;
1626 if (copy_to_user(ip, &ulr, sizeof(ulr))) {
1628 * fput() will trigger the release() callback, so do not go onto
1629 * the regular error cleanup path here.
1636 fd_install(fd, file);
1638 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
1650 #ifdef CONFIG_GPIO_CDEV_V1
1653 * GPIO line event management
1657 * struct lineevent_state - contains the state of a userspace event
1658 * @gdev: the GPIO device the event pertains to
1659 * @label: consumer label used to tag descriptors
1660 * @desc: the GPIO descriptor held by this event
1661 * @eflags: the event flags this line was requested with
1662 * @irq: the interrupt that trigger in response to events on this GPIO
1663 * @wait: wait queue that handles blocking reads of events
1664 * @events: KFIFO for the GPIO events
1665 * @timestamp: cache for the timestamp storing it between hardirq
1666 * and IRQ thread, used to bring the timestamp close to the actual
1669 struct lineevent_state {
1670 struct gpio_device *gdev;
1672 struct gpio_desc *desc;
1675 wait_queue_head_t wait;
1676 DECLARE_KFIFO(events, struct gpioevent_data, 16);
1680 #define GPIOEVENT_REQUEST_VALID_FLAGS \
1681 (GPIOEVENT_REQUEST_RISING_EDGE | \
1682 GPIOEVENT_REQUEST_FALLING_EDGE)
1684 static __poll_t lineevent_poll(struct file *file,
1685 struct poll_table_struct *wait)
1687 struct lineevent_state *le = file->private_data;
1688 __poll_t events = 0;
1690 poll_wait(file, &le->wait, wait);
1692 if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1693 events = EPOLLIN | EPOLLRDNORM;
1698 struct compat_gpioeevent_data {
1699 compat_u64 timestamp;
1703 static ssize_t lineevent_read(struct file *file,
1708 struct lineevent_state *le = file->private_data;
1709 struct gpioevent_data ge;
1710 ssize_t bytes_read = 0;
1715 * When compatible system call is being used the struct gpioevent_data,
1716 * in case of at least ia32, has different size due to the alignment
1717 * differences. Because we have first member 64 bits followed by one of
1718 * 32 bits there is no gap between them. The only difference is the
1719 * padding at the end of the data structure. Hence, we calculate the
1720 * actual sizeof() and pass this as an argument to copy_to_user() to
1721 * drop unneeded bytes from the output.
1723 if (compat_need_64bit_alignment_fixup())
1724 ge_size = sizeof(struct compat_gpioeevent_data);
1726 ge_size = sizeof(struct gpioevent_data);
1727 if (count < ge_size)
1731 spin_lock(&le->wait.lock);
1732 if (kfifo_is_empty(&le->events)) {
1734 spin_unlock(&le->wait.lock);
1738 if (file->f_flags & O_NONBLOCK) {
1739 spin_unlock(&le->wait.lock);
1743 ret = wait_event_interruptible_locked(le->wait,
1744 !kfifo_is_empty(&le->events));
1746 spin_unlock(&le->wait.lock);
1751 ret = kfifo_out(&le->events, &ge, 1);
1752 spin_unlock(&le->wait.lock);
1755 * This should never happen - we were holding the lock
1756 * from the moment we learned the fifo is no longer
1763 if (copy_to_user(buf + bytes_read, &ge, ge_size))
1765 bytes_read += ge_size;
1766 } while (count >= bytes_read + ge_size);
1771 static void lineevent_free(struct lineevent_state *le)
1774 free_irq(le->irq, le);
1776 gpiod_free(le->desc);
1778 put_device(&le->gdev->dev);
1782 static int lineevent_release(struct inode *inode, struct file *file)
1784 lineevent_free(file->private_data);
1788 static long lineevent_ioctl(struct file *file, unsigned int cmd,
1791 struct lineevent_state *le = file->private_data;
1792 void __user *ip = (void __user *)arg;
1793 struct gpiohandle_data ghd;
1796 * We can get the value for an event line but not set it,
1797 * because it is input by definition.
1799 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
1802 memset(&ghd, 0, sizeof(ghd));
1804 val = gpiod_get_value_cansleep(le->desc);
1807 ghd.values[0] = val;
1809 if (copy_to_user(ip, &ghd, sizeof(ghd)))
1817 #ifdef CONFIG_COMPAT
1818 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
1821 return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1825 static const struct file_operations lineevent_fileops = {
1826 .release = lineevent_release,
1827 .read = lineevent_read,
1828 .poll = lineevent_poll,
1829 .owner = THIS_MODULE,
1830 .llseek = noop_llseek,
1831 .unlocked_ioctl = lineevent_ioctl,
1832 #ifdef CONFIG_COMPAT
1833 .compat_ioctl = lineevent_ioctl_compat,
1837 static irqreturn_t lineevent_irq_thread(int irq, void *p)
1839 struct lineevent_state *le = p;
1840 struct gpioevent_data ge;
1843 /* Do not leak kernel stack to userspace */
1844 memset(&ge, 0, sizeof(ge));
1847 * We may be running from a nested threaded interrupt in which case
1848 * we didn't get the timestamp from lineevent_irq_handler().
1851 ge.timestamp = ktime_get_ns();
1853 ge.timestamp = le->timestamp;
1855 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
1856 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1857 int level = gpiod_get_value_cansleep(le->desc);
1860 /* Emit low-to-high event */
1861 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1863 /* Emit high-to-low event */
1864 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1865 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
1866 /* Emit low-to-high event */
1867 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1868 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1869 /* Emit high-to-low event */
1870 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1875 ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
1878 wake_up_poll(&le->wait, EPOLLIN);
1880 pr_debug_ratelimited("event FIFO is full - event dropped\n");
1885 static irqreturn_t lineevent_irq_handler(int irq, void *p)
1887 struct lineevent_state *le = p;
1890 * Just store the timestamp in hardirq context so we get it as
1891 * close in time as possible to the actual event.
1893 le->timestamp = ktime_get_ns();
1895 return IRQ_WAKE_THREAD;
1898 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1900 struct gpioevent_request eventreq;
1901 struct lineevent_state *le;
1902 struct gpio_desc *desc;
1909 int irq, irqflags = 0;
1911 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1914 offset = eventreq.lineoffset;
1915 lflags = eventreq.handleflags;
1916 eflags = eventreq.eventflags;
1918 desc = gpiochip_get_desc(gdev->chip, offset);
1920 return PTR_ERR(desc);
1922 /* Return an error if a unknown flag is set */
1923 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1924 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1927 /* This is just wrong: we don't look for events on output lines */
1928 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1929 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1930 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1933 /* Only one bias flag can be set. */
1934 if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1935 (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1936 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1937 ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1938 (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1941 le = kzalloc(sizeof(*le), GFP_KERNEL);
1945 get_device(&gdev->dev);
1947 if (eventreq.consumer_label[0] != '\0') {
1948 /* label is only initialized if consumer_label is set */
1949 le->label = kstrndup(eventreq.consumer_label,
1950 sizeof(eventreq.consumer_label) - 1,
1958 ret = gpiod_request_user(desc, le->label);
1962 le->eflags = eflags;
1964 linehandle_flags_to_desc_flags(lflags, &desc->flags);
1966 ret = gpiod_direction_input(desc);
1970 blocking_notifier_call_chain(&desc->gdev->notifier,
1971 GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1973 irq = gpiod_to_irq(desc);
1980 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
1981 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1982 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1983 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
1984 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1985 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1986 irqflags |= IRQF_ONESHOT;
1988 INIT_KFIFO(le->events);
1989 init_waitqueue_head(&le->wait);
1991 /* Request a thread to read the events */
1992 ret = request_threaded_irq(le->irq,
1993 lineevent_irq_handler,
1994 lineevent_irq_thread,
2001 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
2007 file = anon_inode_getfile("gpio-event",
2010 O_RDONLY | O_CLOEXEC);
2012 ret = PTR_ERR(file);
2013 goto out_put_unused_fd;
2017 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
2019 * fput() will trigger the release() callback, so do not go onto
2020 * the regular error cleanup path here.
2027 fd_install(fd, file);
2038 static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
2039 struct gpioline_info *info_v1)
2041 u64 flagsv2 = info_v2->flags;
2043 memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
2044 memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
2045 info_v1->line_offset = info_v2->offset;
2048 if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
2049 info_v1->flags |= GPIOLINE_FLAG_KERNEL;
2051 if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
2052 info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
2054 if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
2055 info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
2057 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
2058 info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
2059 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
2060 info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
2062 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
2063 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
2064 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
2065 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
2066 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
2067 info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
2070 static void gpio_v2_line_info_changed_to_v1(
2071 struct gpio_v2_line_info_changed *lic_v2,
2072 struct gpioline_info_changed *lic_v1)
2074 memset(lic_v1, 0, sizeof(*lic_v1));
2075 gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
2076 lic_v1->timestamp = lic_v2->timestamp_ns;
2077 lic_v1->event_type = lic_v2->event_type;
2080 #endif /* CONFIG_GPIO_CDEV_V1 */
2082 static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
2083 struct gpio_v2_line_info *info)
2085 struct gpio_chip *gc = desc->gdev->chip;
2086 bool ok_for_pinctrl;
2087 unsigned long flags;
2088 u32 debounce_period_us;
2089 unsigned int num_attrs = 0;
2091 memset(info, 0, sizeof(*info));
2092 info->offset = gpio_chip_hwgpio(desc);
2095 * This function takes a mutex so we must check this before taking
2098 * FIXME: find a non-racy way to retrieve this information. Maybe a
2099 * lock common to both frameworks?
2102 pinctrl_gpio_can_use_line(gc->base + info->offset);
2104 spin_lock_irqsave(&gpio_lock, flags);
2107 strscpy(info->name, desc->name, sizeof(info->name));
2110 strscpy(info->consumer, desc->label, sizeof(info->consumer));
2113 * Userspace only need to know that the kernel is using this GPIO so
2117 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
2118 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
2119 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
2120 test_bit(FLAG_EXPORT, &desc->flags) ||
2121 test_bit(FLAG_SYSFS, &desc->flags) ||
2122 !gpiochip_line_is_valid(gc, info->offset) ||
2124 info->flags |= GPIO_V2_LINE_FLAG_USED;
2126 if (test_bit(FLAG_IS_OUT, &desc->flags))
2127 info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
2129 info->flags |= GPIO_V2_LINE_FLAG_INPUT;
2131 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2132 info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
2134 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2135 info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
2136 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2137 info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
2139 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2140 info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
2141 if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2142 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
2143 if (test_bit(FLAG_PULL_UP, &desc->flags))
2144 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
2146 if (test_bit(FLAG_EDGE_RISING, &desc->flags))
2147 info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
2148 if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
2149 info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
2151 if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
2152 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
2153 else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags))
2154 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
2156 debounce_period_us = READ_ONCE(desc->debounce_period_us);
2157 if (debounce_period_us) {
2158 info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
2159 info->attrs[num_attrs].debounce_period_us = debounce_period_us;
2162 info->num_attrs = num_attrs;
2164 spin_unlock_irqrestore(&gpio_lock, flags);
2167 struct gpio_chardev_data {
2168 struct gpio_device *gdev;
2169 wait_queue_head_t wait;
2170 DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
2171 struct notifier_block lineinfo_changed_nb;
2172 unsigned long *watched_lines;
2173 #ifdef CONFIG_GPIO_CDEV_V1
2174 atomic_t watch_abi_version;
2178 static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
2180 struct gpio_device *gdev = cdev->gdev;
2181 struct gpiochip_info chipinfo;
2183 memset(&chipinfo, 0, sizeof(chipinfo));
2185 strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
2186 strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
2187 chipinfo.lines = gdev->ngpio;
2188 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
2193 #ifdef CONFIG_GPIO_CDEV_V1
2195 * returns 0 if the versions match, else the previously selected ABI version
2197 static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
2198 unsigned int version)
2200 int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
2202 if (abiv == version)
2208 static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
2211 struct gpio_desc *desc;
2212 struct gpioline_info lineinfo;
2213 struct gpio_v2_line_info lineinfo_v2;
2215 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2218 /* this doubles as a range check on line_offset */
2219 desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset);
2221 return PTR_ERR(desc);
2224 if (lineinfo_ensure_abi_version(cdev, 1))
2227 if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
2231 gpio_desc_to_lineinfo(desc, &lineinfo_v2);
2232 gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
2234 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2236 clear_bit(lineinfo.line_offset, cdev->watched_lines);
2244 static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
2247 struct gpio_desc *desc;
2248 struct gpio_v2_line_info lineinfo;
2250 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2253 if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2256 desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2258 return PTR_ERR(desc);
2261 #ifdef CONFIG_GPIO_CDEV_V1
2262 if (lineinfo_ensure_abi_version(cdev, 2))
2265 if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2268 gpio_desc_to_lineinfo(desc, &lineinfo);
2270 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2272 clear_bit(lineinfo.offset, cdev->watched_lines);
2279 static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
2283 if (copy_from_user(&offset, ip, sizeof(offset)))
2286 if (offset >= cdev->gdev->ngpio)
2289 if (!test_and_clear_bit(offset, cdev->watched_lines))
2296 * gpio_ioctl() - ioctl handler for the GPIO chardev
2298 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2300 struct gpio_chardev_data *cdev = file->private_data;
2301 struct gpio_device *gdev = cdev->gdev;
2302 void __user *ip = (void __user *)arg;
2304 /* We fail any subsequent ioctl():s when the chip is gone */
2308 /* Fill in the struct and pass to userspace */
2310 case GPIO_GET_CHIPINFO_IOCTL:
2311 return chipinfo_get(cdev, ip);
2312 #ifdef CONFIG_GPIO_CDEV_V1
2313 case GPIO_GET_LINEHANDLE_IOCTL:
2314 return linehandle_create(gdev, ip);
2315 case GPIO_GET_LINEEVENT_IOCTL:
2316 return lineevent_create(gdev, ip);
2317 case GPIO_GET_LINEINFO_IOCTL:
2318 return lineinfo_get_v1(cdev, ip, false);
2319 case GPIO_GET_LINEINFO_WATCH_IOCTL:
2320 return lineinfo_get_v1(cdev, ip, true);
2321 #endif /* CONFIG_GPIO_CDEV_V1 */
2322 case GPIO_V2_GET_LINEINFO_IOCTL:
2323 return lineinfo_get(cdev, ip, false);
2324 case GPIO_V2_GET_LINEINFO_WATCH_IOCTL:
2325 return lineinfo_get(cdev, ip, true);
2326 case GPIO_V2_GET_LINE_IOCTL:
2327 return linereq_create(gdev, ip);
2328 case GPIO_GET_LINEINFO_UNWATCH_IOCTL:
2329 return lineinfo_unwatch(cdev, ip);
2335 #ifdef CONFIG_COMPAT
2336 static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2339 return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2343 static struct gpio_chardev_data *
2344 to_gpio_chardev_data(struct notifier_block *nb)
2346 return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2349 static int lineinfo_changed_notify(struct notifier_block *nb,
2350 unsigned long action, void *data)
2352 struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
2353 struct gpio_v2_line_info_changed chg;
2354 struct gpio_desc *desc = data;
2357 if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2360 memset(&chg, 0, sizeof(chg));
2361 chg.event_type = action;
2362 chg.timestamp_ns = ktime_get_ns();
2363 gpio_desc_to_lineinfo(desc, &chg.info);
2365 ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2367 wake_up_poll(&cdev->wait, EPOLLIN);
2369 pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2374 static __poll_t lineinfo_watch_poll(struct file *file,
2375 struct poll_table_struct *pollt)
2377 struct gpio_chardev_data *cdev = file->private_data;
2378 __poll_t events = 0;
2380 poll_wait(file, &cdev->wait, pollt);
2382 if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2384 events = EPOLLIN | EPOLLRDNORM;
2389 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2390 size_t count, loff_t *off)
2392 struct gpio_chardev_data *cdev = file->private_data;
2393 struct gpio_v2_line_info_changed event;
2394 ssize_t bytes_read = 0;
2398 #ifndef CONFIG_GPIO_CDEV_V1
2399 event_size = sizeof(struct gpio_v2_line_info_changed);
2400 if (count < event_size)
2405 spin_lock(&cdev->wait.lock);
2406 if (kfifo_is_empty(&cdev->events)) {
2408 spin_unlock(&cdev->wait.lock);
2412 if (file->f_flags & O_NONBLOCK) {
2413 spin_unlock(&cdev->wait.lock);
2417 ret = wait_event_interruptible_locked(cdev->wait,
2418 !kfifo_is_empty(&cdev->events));
2420 spin_unlock(&cdev->wait.lock);
2424 #ifdef CONFIG_GPIO_CDEV_V1
2425 /* must be after kfifo check so watch_abi_version is set */
2426 if (atomic_read(&cdev->watch_abi_version) == 2)
2427 event_size = sizeof(struct gpio_v2_line_info_changed);
2429 event_size = sizeof(struct gpioline_info_changed);
2430 if (count < event_size) {
2431 spin_unlock(&cdev->wait.lock);
2435 ret = kfifo_out(&cdev->events, &event, 1);
2436 spin_unlock(&cdev->wait.lock);
2440 /* We should never get here. See lineevent_read(). */
2443 #ifdef CONFIG_GPIO_CDEV_V1
2444 if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2445 if (copy_to_user(buf + bytes_read, &event, event_size))
2448 struct gpioline_info_changed event_v1;
2450 gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2451 if (copy_to_user(buf + bytes_read, &event_v1,
2456 if (copy_to_user(buf + bytes_read, &event, event_size))
2459 bytes_read += event_size;
2460 } while (count >= bytes_read + sizeof(event));
2466 * gpio_chrdev_open() - open the chardev for ioctl operations
2467 * @inode: inode for this chardev
2468 * @file: file struct for storing private data
2469 * Returns 0 on success
2471 static int gpio_chrdev_open(struct inode *inode, struct file *file)
2473 struct gpio_device *gdev = container_of(inode->i_cdev,
2474 struct gpio_device, chrdev);
2475 struct gpio_chardev_data *cdev;
2478 /* Fail on open if the backing gpiochip is gone */
2482 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2486 cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2487 if (!cdev->watched_lines)
2490 init_waitqueue_head(&cdev->wait);
2491 INIT_KFIFO(cdev->events);
2494 cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
2495 ret = blocking_notifier_chain_register(&gdev->notifier,
2496 &cdev->lineinfo_changed_nb);
2498 goto out_free_bitmap;
2500 get_device(&gdev->dev);
2501 file->private_data = cdev;
2503 ret = nonseekable_open(inode, file);
2505 goto out_unregister_notifier;
2509 out_unregister_notifier:
2510 blocking_notifier_chain_unregister(&gdev->notifier,
2511 &cdev->lineinfo_changed_nb);
2513 bitmap_free(cdev->watched_lines);
2520 * gpio_chrdev_release() - close chardev after ioctl operations
2521 * @inode: inode for this chardev
2522 * @file: file struct for storing private data
2523 * Returns 0 on success
2525 static int gpio_chrdev_release(struct inode *inode, struct file *file)
2527 struct gpio_chardev_data *cdev = file->private_data;
2528 struct gpio_device *gdev = cdev->gdev;
2530 bitmap_free(cdev->watched_lines);
2531 blocking_notifier_chain_unregister(&gdev->notifier,
2532 &cdev->lineinfo_changed_nb);
2533 put_device(&gdev->dev);
2539 static const struct file_operations gpio_fileops = {
2540 .release = gpio_chrdev_release,
2541 .open = gpio_chrdev_open,
2542 .poll = lineinfo_watch_poll,
2543 .read = lineinfo_watch_read,
2544 .owner = THIS_MODULE,
2545 .llseek = no_llseek,
2546 .unlocked_ioctl = gpio_ioctl,
2547 #ifdef CONFIG_COMPAT
2548 .compat_ioctl = gpio_ioctl_compat,
2552 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2556 cdev_init(&gdev->chrdev, &gpio_fileops);
2557 gdev->chrdev.owner = THIS_MODULE;
2558 gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2560 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2564 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2565 MAJOR(devt), gdev->id);
2570 void gpiolib_cdev_unregister(struct gpio_device *gdev)
2572 cdev_device_del(&gdev->chrdev, &gdev->dev);