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 * The flags for the active edge detector configuration.
439 * edflags is set by linereq_create(), linereq_free(), and
440 * linereq_set_config_unlocked(), which are themselves mutually
441 * exclusive, and is accessed by edge_irq_thread(),
442 * process_hw_ts_thread() and debounce_work_func(),
443 * which can all live with a slightly stale value.
447 * timestamp_ns and req_seqno are accessed only by
448 * edge_irq_handler() and edge_irq_thread(), which are themselves
449 * mutually exclusive, so no additional protection is necessary.
454 * line_seqno is accessed by either edge_irq_thread() or
455 * debounce_work_func(), which are themselves mutually exclusive,
456 * so no additional protection is necessary.
460 * -- debouncer specific fields --
462 struct delayed_work work;
464 * sw_debounce is accessed by linereq_set_config(), which is the
465 * only setter, and linereq_get_values(), which can live with a
466 * slightly stale value.
468 unsigned int sw_debounced;
470 * level is accessed by debounce_work_func(), which is the only
471 * setter, and linereq_get_values() which can live with a slightly
476 struct hte_ts_desc hdesc;
478 * HTE provider sets line level at the time of event. The valid
479 * value is 0 or 1 and negative value for an error.
483 * when sw_debounce is set on HTE enabled line, this is running
484 * counter of the discarded events.
486 u32 total_discard_seq;
488 * when sw_debounce is set on HTE enabled line, this variable records
489 * last sequence number before debounce period expires.
492 #endif /* CONFIG_HTE */
496 * struct linereq - contains the state of a userspace line request
497 * @gdev: the GPIO device the line request pertains to
498 * @label: consumer label used to tag GPIO descriptors
499 * @num_lines: the number of lines in the lines array
500 * @wait: wait queue that handles blocking reads of events
501 * @event_buffer_size: the number of elements allocated in @events
502 * @events: KFIFO for the GPIO events
503 * @seqno: the sequence number for edge events generated on all lines in
504 * this line request. Note that this is not used when @num_lines is 1, as
505 * the line_seqno is then the same and is cheaper to calculate.
506 * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
507 * of configuration, particularly multi-step accesses to desc flags.
508 * @lines: the lines held by this line request, with @num_lines elements.
511 struct gpio_device *gdev;
514 wait_queue_head_t wait;
515 u32 event_buffer_size;
516 DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
518 struct mutex config_mutex;
522 #define GPIO_V2_LINE_BIAS_FLAGS \
523 (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
524 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
525 GPIO_V2_LINE_FLAG_BIAS_DISABLED)
527 #define GPIO_V2_LINE_DIRECTION_FLAGS \
528 (GPIO_V2_LINE_FLAG_INPUT | \
529 GPIO_V2_LINE_FLAG_OUTPUT)
531 #define GPIO_V2_LINE_DRIVE_FLAGS \
532 (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
533 GPIO_V2_LINE_FLAG_OPEN_SOURCE)
535 #define GPIO_V2_LINE_EDGE_FLAGS \
536 (GPIO_V2_LINE_FLAG_EDGE_RISING | \
537 GPIO_V2_LINE_FLAG_EDGE_FALLING)
539 #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS
541 #define GPIO_V2_LINE_VALID_FLAGS \
542 (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
543 GPIO_V2_LINE_DIRECTION_FLAGS | \
544 GPIO_V2_LINE_DRIVE_FLAGS | \
545 GPIO_V2_LINE_EDGE_FLAGS | \
546 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
547 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
548 GPIO_V2_LINE_BIAS_FLAGS)
550 /* subset of flags relevant for edge detector configuration */
551 #define GPIO_V2_LINE_EDGE_DETECTOR_FLAGS \
552 (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
553 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
554 GPIO_V2_LINE_EDGE_FLAGS)
556 static void linereq_put_event(struct linereq *lr,
557 struct gpio_v2_line_event *le)
559 bool overflow = false;
561 spin_lock(&lr->wait.lock);
562 if (kfifo_is_full(&lr->events)) {
564 kfifo_skip(&lr->events);
566 kfifo_in(&lr->events, le, 1);
567 spin_unlock(&lr->wait.lock);
569 wake_up_poll(&lr->wait, EPOLLIN);
571 pr_debug_ratelimited("event FIFO is full - event dropped\n");
574 static u64 line_event_timestamp(struct line *line)
576 if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
577 return ktime_get_real_ns();
578 else if (IS_ENABLED(CONFIG_HTE) &&
579 test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))
580 return line->timestamp_ns;
582 return ktime_get_ns();
585 static u32 line_event_id(int level)
587 return level ? GPIO_V2_LINE_EVENT_RISING_EDGE :
588 GPIO_V2_LINE_EVENT_FALLING_EDGE;
593 static enum hte_return process_hw_ts_thread(void *p)
597 struct gpio_v2_line_event le;
602 return HTE_CB_HANDLED;
607 memset(&le, 0, sizeof(le));
609 le.timestamp_ns = line->timestamp_ns;
610 edflags = READ_ONCE(line->edflags);
612 switch (edflags & GPIO_V2_LINE_EDGE_FLAGS) {
613 case GPIO_V2_LINE_FLAG_EDGE_BOTH:
614 level = (line->raw_level >= 0) ?
616 gpiod_get_raw_value_cansleep(line->desc);
618 if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
621 le.id = line_event_id(level);
623 case GPIO_V2_LINE_FLAG_EDGE_RISING:
624 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
626 case GPIO_V2_LINE_FLAG_EDGE_FALLING:
627 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
630 return HTE_CB_HANDLED;
632 le.line_seqno = line->line_seqno;
633 le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
634 le.offset = gpio_chip_hwgpio(line->desc);
636 linereq_put_event(lr, &le);
638 return HTE_CB_HANDLED;
641 static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p)
648 return HTE_CB_HANDLED;
651 line->timestamp_ns = ts->tsc;
652 line->raw_level = ts->raw_level;
655 if (READ_ONCE(line->sw_debounced)) {
656 line->total_discard_seq++;
657 line->last_seqno = ts->seq;
658 mod_delayed_work(system_wq, &line->work,
659 usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
661 if (unlikely(ts->seq < line->line_seqno))
662 return HTE_CB_HANDLED;
664 diff_seqno = ts->seq - line->line_seqno;
665 line->line_seqno = ts->seq;
666 if (lr->num_lines != 1)
667 line->req_seqno = atomic_add_return(diff_seqno,
670 return HTE_RUN_SECOND_CB;
673 return HTE_CB_HANDLED;
676 static int hte_edge_setup(struct line *line, u64 eflags)
679 unsigned long flags = 0;
680 struct hte_ts_desc *hdesc = &line->hdesc;
682 if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
683 flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
684 HTE_FALLING_EDGE_TS :
686 if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
687 flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
691 line->total_discard_seq = 0;
693 hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags, NULL,
696 ret = hte_ts_get(NULL, hdesc, 0);
700 return hte_request_ts_ns(hdesc, process_hw_ts, process_hw_ts_thread,
706 static int hte_edge_setup(struct line *line, u64 eflags)
710 #endif /* CONFIG_HTE */
712 static irqreturn_t edge_irq_thread(int irq, void *p)
714 struct line *line = p;
715 struct linereq *lr = line->req;
716 struct gpio_v2_line_event le;
718 /* Do not leak kernel stack to userspace */
719 memset(&le, 0, sizeof(le));
721 if (line->timestamp_ns) {
722 le.timestamp_ns = line->timestamp_ns;
725 * We may be running from a nested threaded interrupt in
726 * which case we didn't get the timestamp from
727 * edge_irq_handler().
729 le.timestamp_ns = line_event_timestamp(line);
730 if (lr->num_lines != 1)
731 line->req_seqno = atomic_inc_return(&lr->seqno);
733 line->timestamp_ns = 0;
735 switch (READ_ONCE(line->edflags) & GPIO_V2_LINE_EDGE_FLAGS) {
736 case GPIO_V2_LINE_FLAG_EDGE_BOTH:
737 le.id = line_event_id(gpiod_get_value_cansleep(line->desc));
739 case GPIO_V2_LINE_FLAG_EDGE_RISING:
740 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
742 case GPIO_V2_LINE_FLAG_EDGE_FALLING:
743 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
749 le.line_seqno = line->line_seqno;
750 le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
751 le.offset = gpio_chip_hwgpio(line->desc);
753 linereq_put_event(lr, &le);
758 static irqreturn_t edge_irq_handler(int irq, void *p)
760 struct line *line = p;
761 struct linereq *lr = line->req;
764 * Just store the timestamp in hardirq context so we get it as
765 * close in time as possible to the actual event.
767 line->timestamp_ns = line_event_timestamp(line);
769 if (lr->num_lines != 1)
770 line->req_seqno = atomic_inc_return(&lr->seqno);
772 return IRQ_WAKE_THREAD;
776 * returns the current debounced logical value.
778 static bool debounced_value(struct line *line)
783 * minor race - debouncer may be stopped here, so edge_detector_stop()
784 * must leave the value unchanged so the following will read the level
785 * from when the debouncer was last running.
787 value = READ_ONCE(line->level);
789 if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
795 static irqreturn_t debounce_irq_handler(int irq, void *p)
797 struct line *line = p;
799 mod_delayed_work(system_wq, &line->work,
800 usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
805 static void debounce_work_func(struct work_struct *work)
807 struct gpio_v2_line_event le;
808 struct line *line = container_of(work, struct line, work.work);
810 u64 eflags, edflags = READ_ONCE(line->edflags);
815 if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
816 level = line->raw_level;
819 level = gpiod_get_raw_value_cansleep(line->desc);
821 pr_debug_ratelimited("debouncer failed to read line value\n");
825 if (READ_ONCE(line->level) == level)
828 WRITE_ONCE(line->level, level);
830 /* -- edge detection -- */
831 eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
835 /* switch from physical level to logical - if they differ */
836 if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
839 /* ignore edges that are not being monitored */
840 if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
841 ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
844 /* Do not leak kernel stack to userspace */
845 memset(&le, 0, sizeof(le));
848 le.timestamp_ns = line_event_timestamp(line);
849 le.offset = gpio_chip_hwgpio(line->desc);
851 if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) {
852 /* discard events except the last one */
853 line->total_discard_seq -= 1;
854 diff_seqno = line->last_seqno - line->total_discard_seq -
856 line->line_seqno = line->last_seqno - line->total_discard_seq;
857 le.line_seqno = line->line_seqno;
858 le.seqno = (lr->num_lines == 1) ?
859 le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno);
861 #endif /* CONFIG_HTE */
864 le.line_seqno = line->line_seqno;
865 le.seqno = (lr->num_lines == 1) ?
866 le.line_seqno : atomic_inc_return(&lr->seqno);
869 le.id = line_event_id(level);
871 linereq_put_event(lr, &le);
874 static int debounce_setup(struct line *line, unsigned int debounce_period_us)
876 unsigned long irqflags;
880 ret = gpiod_set_debounce(line->desc, debounce_period_us);
882 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
885 if (ret != -ENOTSUPP)
888 if (debounce_period_us) {
889 /* setup software debounce */
890 level = gpiod_get_raw_value_cansleep(line->desc);
894 if (!(IS_ENABLED(CONFIG_HTE) &&
895 test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))) {
896 irq = gpiod_to_irq(line->desc);
900 irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
901 ret = request_irq(irq, debounce_irq_handler, irqflags,
902 line->req->label, line);
907 ret = hte_edge_setup(line, GPIO_V2_LINE_FLAG_EDGE_BOTH);
912 WRITE_ONCE(line->level, level);
913 WRITE_ONCE(line->sw_debounced, 1);
918 static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
919 unsigned int line_idx)
922 u64 mask = BIT_ULL(line_idx);
924 for (i = 0; i < lc->num_attrs; i++) {
925 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
926 (lc->attrs[i].mask & mask))
932 static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
933 unsigned int line_idx)
936 u64 mask = BIT_ULL(line_idx);
938 for (i = 0; i < lc->num_attrs; i++) {
939 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
940 (lc->attrs[i].mask & mask))
941 return lc->attrs[i].attr.debounce_period_us;
946 static void edge_detector_stop(struct line *line)
949 free_irq(line->irq, line);
954 if (READ_ONCE(line->edflags) & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
955 hte_ts_put(&line->hdesc);
958 cancel_delayed_work_sync(&line->work);
959 WRITE_ONCE(line->sw_debounced, 0);
960 WRITE_ONCE(line->edflags, 0);
962 WRITE_ONCE(line->desc->debounce_period_us, 0);
963 /* do not change line->level - see comment in debounced_value() */
966 static int edge_detector_setup(struct line *line,
967 struct gpio_v2_line_config *lc,
968 unsigned int line_idx, u64 edflags)
970 u32 debounce_period_us;
971 unsigned long irqflags = 0;
975 eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
976 if (eflags && !kfifo_initialized(&line->req->events)) {
977 ret = kfifo_alloc(&line->req->events,
978 line->req->event_buffer_size, GFP_KERNEL);
982 if (gpio_v2_line_config_debounced(lc, line_idx)) {
983 debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
984 ret = debounce_setup(line, debounce_period_us);
987 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
990 /* detection disabled or sw debouncer will provide edge detection */
991 if (!eflags || READ_ONCE(line->sw_debounced))
994 if (IS_ENABLED(CONFIG_HTE) &&
995 (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
996 return hte_edge_setup(line, edflags);
998 irq = gpiod_to_irq(line->desc);
1002 if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
1003 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
1004 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1005 if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
1006 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
1007 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1008 irqflags |= IRQF_ONESHOT;
1010 /* Request a thread to read the events */
1011 ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
1012 irqflags, line->req->label, line);
1020 static int edge_detector_update(struct line *line,
1021 struct gpio_v2_line_config *lc,
1022 unsigned int line_idx, u64 edflags)
1024 u64 active_edflags = READ_ONCE(line->edflags);
1025 unsigned int debounce_period_us =
1026 gpio_v2_line_config_debounce_period(lc, line_idx);
1028 if ((active_edflags == edflags) &&
1029 (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us))
1032 /* sw debounced and still will be...*/
1033 if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
1034 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
1038 /* reconfiguring edge detection or sw debounce being disabled */
1039 if ((line->irq && !READ_ONCE(line->sw_debounced)) ||
1040 (active_edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) ||
1041 (!debounce_period_us && READ_ONCE(line->sw_debounced)))
1042 edge_detector_stop(line);
1044 return edge_detector_setup(line, lc, line_idx, edflags);
1047 static u64 gpio_v2_line_config_flags(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_FLAGS) &&
1055 (lc->attrs[i].mask & mask))
1056 return lc->attrs[i].attr.flags;
1061 static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
1062 unsigned int line_idx)
1065 u64 mask = BIT_ULL(line_idx);
1067 for (i = 0; i < lc->num_attrs; i++) {
1068 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
1069 (lc->attrs[i].mask & mask))
1070 return !!(lc->attrs[i].attr.values & mask);
1075 static int gpio_v2_line_flags_validate(u64 flags)
1077 /* Return an error if an unknown flag is set */
1078 if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
1081 if (!IS_ENABLED(CONFIG_HTE) &&
1082 (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1086 * Do not allow both INPUT and OUTPUT flags to be set as they are
1089 if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
1090 (flags & GPIO_V2_LINE_FLAG_OUTPUT))
1093 /* Only allow one event clock source */
1094 if (IS_ENABLED(CONFIG_HTE) &&
1095 (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) &&
1096 (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1099 /* Edge detection requires explicit input. */
1100 if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
1101 !(flags & GPIO_V2_LINE_FLAG_INPUT))
1105 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
1106 * request. If the hardware actually supports enabling both at the
1107 * same time the electrical result would be disastrous.
1109 if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
1110 (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
1113 /* Drive requires explicit output direction. */
1114 if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
1115 !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
1118 /* Bias requires explicit direction. */
1119 if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
1120 !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
1123 /* Only one bias flag can be set. */
1124 if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
1125 (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
1126 GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
1127 ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
1128 (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
1134 static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
1135 unsigned int num_lines)
1141 if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
1144 if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
1147 for (i = 0; i < num_lines; i++) {
1148 flags = gpio_v2_line_config_flags(lc, i);
1149 ret = gpio_v2_line_flags_validate(flags);
1153 /* debounce requires explicit input */
1154 if (gpio_v2_line_config_debounced(lc, i) &&
1155 !(flags & GPIO_V2_LINE_FLAG_INPUT))
1161 static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
1162 unsigned long *flagsp)
1164 assign_bit(FLAG_ACTIVE_LOW, flagsp,
1165 flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
1167 if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
1168 set_bit(FLAG_IS_OUT, flagsp);
1169 else if (flags & GPIO_V2_LINE_FLAG_INPUT)
1170 clear_bit(FLAG_IS_OUT, flagsp);
1172 assign_bit(FLAG_EDGE_RISING, flagsp,
1173 flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
1174 assign_bit(FLAG_EDGE_FALLING, flagsp,
1175 flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
1177 assign_bit(FLAG_OPEN_DRAIN, flagsp,
1178 flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
1179 assign_bit(FLAG_OPEN_SOURCE, flagsp,
1180 flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
1182 assign_bit(FLAG_PULL_UP, flagsp,
1183 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
1184 assign_bit(FLAG_PULL_DOWN, flagsp,
1185 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
1186 assign_bit(FLAG_BIAS_DISABLE, flagsp,
1187 flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
1189 assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
1190 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
1191 assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp,
1192 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
1195 static long linereq_get_values(struct linereq *lr, void __user *ip)
1197 struct gpio_v2_line_values lv;
1198 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
1199 struct gpio_desc **descs;
1200 unsigned int i, didx, num_get;
1204 /* NOTE: It's ok to read values of output lines. */
1205 if (copy_from_user(&lv, ip, sizeof(lv)))
1208 for (num_get = 0, i = 0; i < lr->num_lines; i++) {
1209 if (lv.mask & BIT_ULL(i)) {
1211 descs = &lr->lines[i].desc;
1219 descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
1222 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1223 if (lv.mask & BIT_ULL(i)) {
1224 descs[didx] = lr->lines[i].desc;
1229 ret = gpiod_get_array_value_complex(false, true, num_get,
1238 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1239 if (lv.mask & BIT_ULL(i)) {
1240 if (lr->lines[i].sw_debounced)
1241 val = debounced_value(&lr->lines[i]);
1243 val = test_bit(didx, vals);
1245 lv.bits |= BIT_ULL(i);
1250 if (copy_to_user(ip, &lv, sizeof(lv)))
1256 static long linereq_set_values_unlocked(struct linereq *lr,
1257 struct gpio_v2_line_values *lv)
1259 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
1260 struct gpio_desc **descs;
1261 unsigned int i, didx, num_set;
1264 bitmap_zero(vals, GPIO_V2_LINES_MAX);
1265 for (num_set = 0, i = 0; i < lr->num_lines; i++) {
1266 if (lv->mask & BIT_ULL(i)) {
1267 if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
1269 if (lv->bits & BIT_ULL(i))
1270 __set_bit(num_set, vals);
1272 descs = &lr->lines[i].desc;
1279 /* build compacted desc array and values */
1280 descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
1283 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1284 if (lv->mask & BIT_ULL(i)) {
1285 descs[didx] = lr->lines[i].desc;
1290 ret = gpiod_set_array_value_complex(false, true, num_set,
1298 static long linereq_set_values(struct linereq *lr, void __user *ip)
1300 struct gpio_v2_line_values lv;
1303 if (copy_from_user(&lv, ip, sizeof(lv)))
1306 mutex_lock(&lr->config_mutex);
1308 ret = linereq_set_values_unlocked(lr, &lv);
1310 mutex_unlock(&lr->config_mutex);
1315 static long linereq_set_config_unlocked(struct linereq *lr,
1316 struct gpio_v2_line_config *lc)
1318 struct gpio_desc *desc;
1324 for (i = 0; i < lr->num_lines; i++) {
1325 line = &lr->lines[i];
1326 desc = lr->lines[i].desc;
1327 flags = gpio_v2_line_config_flags(lc, i);
1328 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1329 edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
1331 * Lines have to be requested explicitly for input
1332 * or output, else the line will be treated "as is".
1334 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1335 int val = gpio_v2_line_config_output_value(lc, i);
1337 edge_detector_stop(line);
1338 ret = gpiod_direction_output(desc, val);
1341 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1342 ret = gpiod_direction_input(desc);
1346 ret = edge_detector_update(line, lc, i, edflags);
1351 WRITE_ONCE(line->edflags, edflags);
1353 blocking_notifier_call_chain(&desc->gdev->notifier,
1354 GPIO_V2_LINE_CHANGED_CONFIG,
1360 static long linereq_set_config(struct linereq *lr, void __user *ip)
1362 struct gpio_v2_line_config lc;
1365 if (copy_from_user(&lc, ip, sizeof(lc)))
1368 ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
1372 mutex_lock(&lr->config_mutex);
1374 ret = linereq_set_config_unlocked(lr, &lc);
1376 mutex_unlock(&lr->config_mutex);
1381 static long linereq_ioctl(struct file *file, unsigned int cmd,
1384 struct linereq *lr = file->private_data;
1385 void __user *ip = (void __user *)arg;
1388 case GPIO_V2_LINE_GET_VALUES_IOCTL:
1389 return linereq_get_values(lr, ip);
1390 case GPIO_V2_LINE_SET_VALUES_IOCTL:
1391 return linereq_set_values(lr, ip);
1392 case GPIO_V2_LINE_SET_CONFIG_IOCTL:
1393 return linereq_set_config(lr, ip);
1399 #ifdef CONFIG_COMPAT
1400 static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
1403 return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1407 static __poll_t linereq_poll(struct file *file,
1408 struct poll_table_struct *wait)
1410 struct linereq *lr = file->private_data;
1411 __poll_t events = 0;
1413 poll_wait(file, &lr->wait, wait);
1415 if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
1417 events = EPOLLIN | EPOLLRDNORM;
1422 static ssize_t linereq_read(struct file *file,
1427 struct linereq *lr = file->private_data;
1428 struct gpio_v2_line_event le;
1429 ssize_t bytes_read = 0;
1432 if (count < sizeof(le))
1436 spin_lock(&lr->wait.lock);
1437 if (kfifo_is_empty(&lr->events)) {
1439 spin_unlock(&lr->wait.lock);
1443 if (file->f_flags & O_NONBLOCK) {
1444 spin_unlock(&lr->wait.lock);
1448 ret = wait_event_interruptible_locked(lr->wait,
1449 !kfifo_is_empty(&lr->events));
1451 spin_unlock(&lr->wait.lock);
1456 ret = kfifo_out(&lr->events, &le, 1);
1457 spin_unlock(&lr->wait.lock);
1460 * This should never happen - we were holding the
1461 * lock from the moment we learned the fifo is no
1462 * longer empty until now.
1468 if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
1470 bytes_read += sizeof(le);
1471 } while (count >= bytes_read + sizeof(le));
1476 static void linereq_free(struct linereq *lr)
1480 for (i = 0; i < lr->num_lines; i++) {
1481 if (lr->lines[i].desc) {
1482 edge_detector_stop(&lr->lines[i]);
1483 gpiod_free(lr->lines[i].desc);
1486 kfifo_free(&lr->events);
1488 put_device(&lr->gdev->dev);
1492 static int linereq_release(struct inode *inode, struct file *file)
1494 struct linereq *lr = file->private_data;
1500 #ifdef CONFIG_PROC_FS
1501 static void linereq_show_fdinfo(struct seq_file *out, struct file *file)
1503 struct linereq *lr = file->private_data;
1504 struct device *dev = &lr->gdev->dev;
1507 seq_printf(out, "gpio-chip:\t%s\n", dev_name(dev));
1509 for (i = 0; i < lr->num_lines; i++)
1510 seq_printf(out, "gpio-line:\t%d\n",
1511 gpio_chip_hwgpio(lr->lines[i].desc));
1515 static const struct file_operations line_fileops = {
1516 .release = linereq_release,
1517 .read = linereq_read,
1518 .poll = linereq_poll,
1519 .owner = THIS_MODULE,
1520 .llseek = noop_llseek,
1521 .unlocked_ioctl = linereq_ioctl,
1522 #ifdef CONFIG_COMPAT
1523 .compat_ioctl = linereq_ioctl_compat,
1525 #ifdef CONFIG_PROC_FS
1526 .show_fdinfo = linereq_show_fdinfo,
1530 static int linereq_create(struct gpio_device *gdev, void __user *ip)
1532 struct gpio_v2_line_request ulr;
1533 struct gpio_v2_line_config *lc;
1540 if (copy_from_user(&ulr, ip, sizeof(ulr)))
1543 if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
1546 if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
1550 ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
1554 lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
1559 get_device(&gdev->dev);
1561 for (i = 0; i < ulr.num_lines; i++) {
1562 lr->lines[i].req = lr;
1563 WRITE_ONCE(lr->lines[i].sw_debounced, 0);
1564 INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
1567 if (ulr.consumer[0] != '\0') {
1568 /* label is only initialized if consumer is set */
1569 lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1573 goto out_free_linereq;
1577 mutex_init(&lr->config_mutex);
1578 init_waitqueue_head(&lr->wait);
1579 lr->event_buffer_size = ulr.event_buffer_size;
1580 if (lr->event_buffer_size == 0)
1581 lr->event_buffer_size = ulr.num_lines * 16;
1582 else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
1583 lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
1585 atomic_set(&lr->seqno, 0);
1586 lr->num_lines = ulr.num_lines;
1588 /* Request each GPIO */
1589 for (i = 0; i < ulr.num_lines; i++) {
1590 u32 offset = ulr.offsets[i];
1591 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
1594 ret = PTR_ERR(desc);
1595 goto out_free_linereq;
1598 ret = gpiod_request_user(desc, lr->label);
1600 goto out_free_linereq;
1602 lr->lines[i].desc = desc;
1603 flags = gpio_v2_line_config_flags(lc, i);
1604 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1606 ret = gpiod_set_transitory(desc, false);
1608 goto out_free_linereq;
1610 edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
1612 * Lines have to be requested explicitly for input
1613 * or output, else the line will be treated "as is".
1615 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1616 int val = gpio_v2_line_config_output_value(lc, i);
1618 ret = gpiod_direction_output(desc, val);
1620 goto out_free_linereq;
1621 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1622 ret = gpiod_direction_input(desc);
1624 goto out_free_linereq;
1626 ret = edge_detector_setup(&lr->lines[i], lc, i,
1629 goto out_free_linereq;
1632 lr->lines[i].edflags = edflags;
1634 blocking_notifier_call_chain(&desc->gdev->notifier,
1635 GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1637 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
1641 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1644 goto out_free_linereq;
1647 file = anon_inode_getfile("gpio-line", &line_fileops, lr,
1648 O_RDONLY | O_CLOEXEC);
1650 ret = PTR_ERR(file);
1651 goto out_put_unused_fd;
1655 if (copy_to_user(ip, &ulr, sizeof(ulr))) {
1657 * fput() will trigger the release() callback, so do not go onto
1658 * the regular error cleanup path here.
1665 fd_install(fd, file);
1667 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
1679 #ifdef CONFIG_GPIO_CDEV_V1
1682 * GPIO line event management
1686 * struct lineevent_state - contains the state of a userspace event
1687 * @gdev: the GPIO device the event pertains to
1688 * @label: consumer label used to tag descriptors
1689 * @desc: the GPIO descriptor held by this event
1690 * @eflags: the event flags this line was requested with
1691 * @irq: the interrupt that trigger in response to events on this GPIO
1692 * @wait: wait queue that handles blocking reads of events
1693 * @events: KFIFO for the GPIO events
1694 * @timestamp: cache for the timestamp storing it between hardirq
1695 * and IRQ thread, used to bring the timestamp close to the actual
1698 struct lineevent_state {
1699 struct gpio_device *gdev;
1701 struct gpio_desc *desc;
1704 wait_queue_head_t wait;
1705 DECLARE_KFIFO(events, struct gpioevent_data, 16);
1709 #define GPIOEVENT_REQUEST_VALID_FLAGS \
1710 (GPIOEVENT_REQUEST_RISING_EDGE | \
1711 GPIOEVENT_REQUEST_FALLING_EDGE)
1713 static __poll_t lineevent_poll(struct file *file,
1714 struct poll_table_struct *wait)
1716 struct lineevent_state *le = file->private_data;
1717 __poll_t events = 0;
1719 poll_wait(file, &le->wait, wait);
1721 if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1722 events = EPOLLIN | EPOLLRDNORM;
1727 struct compat_gpioeevent_data {
1728 compat_u64 timestamp;
1732 static ssize_t lineevent_read(struct file *file,
1737 struct lineevent_state *le = file->private_data;
1738 struct gpioevent_data ge;
1739 ssize_t bytes_read = 0;
1744 * When compatible system call is being used the struct gpioevent_data,
1745 * in case of at least ia32, has different size due to the alignment
1746 * differences. Because we have first member 64 bits followed by one of
1747 * 32 bits there is no gap between them. The only difference is the
1748 * padding at the end of the data structure. Hence, we calculate the
1749 * actual sizeof() and pass this as an argument to copy_to_user() to
1750 * drop unneeded bytes from the output.
1752 if (compat_need_64bit_alignment_fixup())
1753 ge_size = sizeof(struct compat_gpioeevent_data);
1755 ge_size = sizeof(struct gpioevent_data);
1756 if (count < ge_size)
1760 spin_lock(&le->wait.lock);
1761 if (kfifo_is_empty(&le->events)) {
1763 spin_unlock(&le->wait.lock);
1767 if (file->f_flags & O_NONBLOCK) {
1768 spin_unlock(&le->wait.lock);
1772 ret = wait_event_interruptible_locked(le->wait,
1773 !kfifo_is_empty(&le->events));
1775 spin_unlock(&le->wait.lock);
1780 ret = kfifo_out(&le->events, &ge, 1);
1781 spin_unlock(&le->wait.lock);
1784 * This should never happen - we were holding the lock
1785 * from the moment we learned the fifo is no longer
1792 if (copy_to_user(buf + bytes_read, &ge, ge_size))
1794 bytes_read += ge_size;
1795 } while (count >= bytes_read + ge_size);
1800 static void lineevent_free(struct lineevent_state *le)
1803 free_irq(le->irq, le);
1805 gpiod_free(le->desc);
1807 put_device(&le->gdev->dev);
1811 static int lineevent_release(struct inode *inode, struct file *file)
1813 lineevent_free(file->private_data);
1817 static long lineevent_ioctl(struct file *file, unsigned int cmd,
1820 struct lineevent_state *le = file->private_data;
1821 void __user *ip = (void __user *)arg;
1822 struct gpiohandle_data ghd;
1825 * We can get the value for an event line but not set it,
1826 * because it is input by definition.
1828 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
1831 memset(&ghd, 0, sizeof(ghd));
1833 val = gpiod_get_value_cansleep(le->desc);
1836 ghd.values[0] = val;
1838 if (copy_to_user(ip, &ghd, sizeof(ghd)))
1846 #ifdef CONFIG_COMPAT
1847 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
1850 return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1854 static const struct file_operations lineevent_fileops = {
1855 .release = lineevent_release,
1856 .read = lineevent_read,
1857 .poll = lineevent_poll,
1858 .owner = THIS_MODULE,
1859 .llseek = noop_llseek,
1860 .unlocked_ioctl = lineevent_ioctl,
1861 #ifdef CONFIG_COMPAT
1862 .compat_ioctl = lineevent_ioctl_compat,
1866 static irqreturn_t lineevent_irq_thread(int irq, void *p)
1868 struct lineevent_state *le = p;
1869 struct gpioevent_data ge;
1872 /* Do not leak kernel stack to userspace */
1873 memset(&ge, 0, sizeof(ge));
1876 * We may be running from a nested threaded interrupt in which case
1877 * we didn't get the timestamp from lineevent_irq_handler().
1880 ge.timestamp = ktime_get_ns();
1882 ge.timestamp = le->timestamp;
1884 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
1885 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1886 int level = gpiod_get_value_cansleep(le->desc);
1889 /* Emit low-to-high event */
1890 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1892 /* Emit high-to-low event */
1893 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1894 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
1895 /* Emit low-to-high event */
1896 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1897 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1898 /* Emit high-to-low event */
1899 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1904 ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
1907 wake_up_poll(&le->wait, EPOLLIN);
1909 pr_debug_ratelimited("event FIFO is full - event dropped\n");
1914 static irqreturn_t lineevent_irq_handler(int irq, void *p)
1916 struct lineevent_state *le = p;
1919 * Just store the timestamp in hardirq context so we get it as
1920 * close in time as possible to the actual event.
1922 le->timestamp = ktime_get_ns();
1924 return IRQ_WAKE_THREAD;
1927 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1929 struct gpioevent_request eventreq;
1930 struct lineevent_state *le;
1931 struct gpio_desc *desc;
1938 int irq, irqflags = 0;
1940 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1943 offset = eventreq.lineoffset;
1944 lflags = eventreq.handleflags;
1945 eflags = eventreq.eventflags;
1947 desc = gpiochip_get_desc(gdev->chip, offset);
1949 return PTR_ERR(desc);
1951 /* Return an error if a unknown flag is set */
1952 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1953 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1956 /* This is just wrong: we don't look for events on output lines */
1957 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1958 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1959 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1962 /* Only one bias flag can be set. */
1963 if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1964 (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1965 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1966 ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1967 (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1970 le = kzalloc(sizeof(*le), GFP_KERNEL);
1974 get_device(&gdev->dev);
1976 if (eventreq.consumer_label[0] != '\0') {
1977 /* label is only initialized if consumer_label is set */
1978 le->label = kstrndup(eventreq.consumer_label,
1979 sizeof(eventreq.consumer_label) - 1,
1987 ret = gpiod_request_user(desc, le->label);
1991 le->eflags = eflags;
1993 linehandle_flags_to_desc_flags(lflags, &desc->flags);
1995 ret = gpiod_direction_input(desc);
1999 blocking_notifier_call_chain(&desc->gdev->notifier,
2000 GPIO_V2_LINE_CHANGED_REQUESTED, desc);
2002 irq = gpiod_to_irq(desc);
2008 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
2009 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
2010 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
2011 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
2012 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
2013 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
2014 irqflags |= IRQF_ONESHOT;
2016 INIT_KFIFO(le->events);
2017 init_waitqueue_head(&le->wait);
2019 /* Request a thread to read the events */
2020 ret = request_threaded_irq(irq,
2021 lineevent_irq_handler,
2022 lineevent_irq_thread,
2031 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
2037 file = anon_inode_getfile("gpio-event",
2040 O_RDONLY | O_CLOEXEC);
2042 ret = PTR_ERR(file);
2043 goto out_put_unused_fd;
2047 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
2049 * fput() will trigger the release() callback, so do not go onto
2050 * the regular error cleanup path here.
2057 fd_install(fd, file);
2068 static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
2069 struct gpioline_info *info_v1)
2071 u64 flagsv2 = info_v2->flags;
2073 memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
2074 memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
2075 info_v1->line_offset = info_v2->offset;
2078 if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
2079 info_v1->flags |= GPIOLINE_FLAG_KERNEL;
2081 if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
2082 info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
2084 if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
2085 info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
2087 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
2088 info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
2089 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
2090 info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
2092 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
2093 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
2094 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
2095 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
2096 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
2097 info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
2100 static void gpio_v2_line_info_changed_to_v1(
2101 struct gpio_v2_line_info_changed *lic_v2,
2102 struct gpioline_info_changed *lic_v1)
2104 memset(lic_v1, 0, sizeof(*lic_v1));
2105 gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
2106 lic_v1->timestamp = lic_v2->timestamp_ns;
2107 lic_v1->event_type = lic_v2->event_type;
2110 #endif /* CONFIG_GPIO_CDEV_V1 */
2112 static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
2113 struct gpio_v2_line_info *info)
2115 struct gpio_chip *gc = desc->gdev->chip;
2116 bool ok_for_pinctrl;
2117 unsigned long flags;
2118 u32 debounce_period_us;
2119 unsigned int num_attrs = 0;
2121 memset(info, 0, sizeof(*info));
2122 info->offset = gpio_chip_hwgpio(desc);
2125 * This function takes a mutex so we must check this before taking
2128 * FIXME: find a non-racy way to retrieve this information. Maybe a
2129 * lock common to both frameworks?
2132 pinctrl_gpio_can_use_line(gc->base + info->offset);
2134 spin_lock_irqsave(&gpio_lock, flags);
2137 strscpy(info->name, desc->name, sizeof(info->name));
2140 strscpy(info->consumer, desc->label, sizeof(info->consumer));
2143 * Userspace only need to know that the kernel is using this GPIO so
2147 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
2148 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
2149 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
2150 test_bit(FLAG_EXPORT, &desc->flags) ||
2151 test_bit(FLAG_SYSFS, &desc->flags) ||
2152 !gpiochip_line_is_valid(gc, info->offset) ||
2154 info->flags |= GPIO_V2_LINE_FLAG_USED;
2156 if (test_bit(FLAG_IS_OUT, &desc->flags))
2157 info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
2159 info->flags |= GPIO_V2_LINE_FLAG_INPUT;
2161 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2162 info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
2164 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2165 info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
2166 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2167 info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
2169 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2170 info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
2171 if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2172 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
2173 if (test_bit(FLAG_PULL_UP, &desc->flags))
2174 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
2176 if (test_bit(FLAG_EDGE_RISING, &desc->flags))
2177 info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
2178 if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
2179 info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
2181 if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
2182 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
2183 else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags))
2184 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
2186 debounce_period_us = READ_ONCE(desc->debounce_period_us);
2187 if (debounce_period_us) {
2188 info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
2189 info->attrs[num_attrs].debounce_period_us = debounce_period_us;
2192 info->num_attrs = num_attrs;
2194 spin_unlock_irqrestore(&gpio_lock, flags);
2197 struct gpio_chardev_data {
2198 struct gpio_device *gdev;
2199 wait_queue_head_t wait;
2200 DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
2201 struct notifier_block lineinfo_changed_nb;
2202 unsigned long *watched_lines;
2203 #ifdef CONFIG_GPIO_CDEV_V1
2204 atomic_t watch_abi_version;
2208 static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
2210 struct gpio_device *gdev = cdev->gdev;
2211 struct gpiochip_info chipinfo;
2213 memset(&chipinfo, 0, sizeof(chipinfo));
2215 strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
2216 strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
2217 chipinfo.lines = gdev->ngpio;
2218 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
2223 #ifdef CONFIG_GPIO_CDEV_V1
2225 * returns 0 if the versions match, else the previously selected ABI version
2227 static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
2228 unsigned int version)
2230 int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
2232 if (abiv == version)
2238 static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
2241 struct gpio_desc *desc;
2242 struct gpioline_info lineinfo;
2243 struct gpio_v2_line_info lineinfo_v2;
2245 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2248 /* this doubles as a range check on line_offset */
2249 desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset);
2251 return PTR_ERR(desc);
2254 if (lineinfo_ensure_abi_version(cdev, 1))
2257 if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
2261 gpio_desc_to_lineinfo(desc, &lineinfo_v2);
2262 gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
2264 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2266 clear_bit(lineinfo.line_offset, cdev->watched_lines);
2274 static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
2277 struct gpio_desc *desc;
2278 struct gpio_v2_line_info lineinfo;
2280 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2283 if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2286 desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2288 return PTR_ERR(desc);
2291 #ifdef CONFIG_GPIO_CDEV_V1
2292 if (lineinfo_ensure_abi_version(cdev, 2))
2295 if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2298 gpio_desc_to_lineinfo(desc, &lineinfo);
2300 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2302 clear_bit(lineinfo.offset, cdev->watched_lines);
2309 static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
2313 if (copy_from_user(&offset, ip, sizeof(offset)))
2316 if (offset >= cdev->gdev->ngpio)
2319 if (!test_and_clear_bit(offset, cdev->watched_lines))
2326 * gpio_ioctl() - ioctl handler for the GPIO chardev
2328 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2330 struct gpio_chardev_data *cdev = file->private_data;
2331 struct gpio_device *gdev = cdev->gdev;
2332 void __user *ip = (void __user *)arg;
2334 /* We fail any subsequent ioctl():s when the chip is gone */
2338 /* Fill in the struct and pass to userspace */
2340 case GPIO_GET_CHIPINFO_IOCTL:
2341 return chipinfo_get(cdev, ip);
2342 #ifdef CONFIG_GPIO_CDEV_V1
2343 case GPIO_GET_LINEHANDLE_IOCTL:
2344 return linehandle_create(gdev, ip);
2345 case GPIO_GET_LINEEVENT_IOCTL:
2346 return lineevent_create(gdev, ip);
2347 case GPIO_GET_LINEINFO_IOCTL:
2348 return lineinfo_get_v1(cdev, ip, false);
2349 case GPIO_GET_LINEINFO_WATCH_IOCTL:
2350 return lineinfo_get_v1(cdev, ip, true);
2351 #endif /* CONFIG_GPIO_CDEV_V1 */
2352 case GPIO_V2_GET_LINEINFO_IOCTL:
2353 return lineinfo_get(cdev, ip, false);
2354 case GPIO_V2_GET_LINEINFO_WATCH_IOCTL:
2355 return lineinfo_get(cdev, ip, true);
2356 case GPIO_V2_GET_LINE_IOCTL:
2357 return linereq_create(gdev, ip);
2358 case GPIO_GET_LINEINFO_UNWATCH_IOCTL:
2359 return lineinfo_unwatch(cdev, ip);
2365 #ifdef CONFIG_COMPAT
2366 static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2369 return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2373 static struct gpio_chardev_data *
2374 to_gpio_chardev_data(struct notifier_block *nb)
2376 return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2379 static int lineinfo_changed_notify(struct notifier_block *nb,
2380 unsigned long action, void *data)
2382 struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
2383 struct gpio_v2_line_info_changed chg;
2384 struct gpio_desc *desc = data;
2387 if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2390 memset(&chg, 0, sizeof(chg));
2391 chg.event_type = action;
2392 chg.timestamp_ns = ktime_get_ns();
2393 gpio_desc_to_lineinfo(desc, &chg.info);
2395 ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2397 wake_up_poll(&cdev->wait, EPOLLIN);
2399 pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2404 static __poll_t lineinfo_watch_poll(struct file *file,
2405 struct poll_table_struct *pollt)
2407 struct gpio_chardev_data *cdev = file->private_data;
2408 __poll_t events = 0;
2410 poll_wait(file, &cdev->wait, pollt);
2412 if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2414 events = EPOLLIN | EPOLLRDNORM;
2419 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2420 size_t count, loff_t *off)
2422 struct gpio_chardev_data *cdev = file->private_data;
2423 struct gpio_v2_line_info_changed event;
2424 ssize_t bytes_read = 0;
2428 #ifndef CONFIG_GPIO_CDEV_V1
2429 event_size = sizeof(struct gpio_v2_line_info_changed);
2430 if (count < event_size)
2435 spin_lock(&cdev->wait.lock);
2436 if (kfifo_is_empty(&cdev->events)) {
2438 spin_unlock(&cdev->wait.lock);
2442 if (file->f_flags & O_NONBLOCK) {
2443 spin_unlock(&cdev->wait.lock);
2447 ret = wait_event_interruptible_locked(cdev->wait,
2448 !kfifo_is_empty(&cdev->events));
2450 spin_unlock(&cdev->wait.lock);
2454 #ifdef CONFIG_GPIO_CDEV_V1
2455 /* must be after kfifo check so watch_abi_version is set */
2456 if (atomic_read(&cdev->watch_abi_version) == 2)
2457 event_size = sizeof(struct gpio_v2_line_info_changed);
2459 event_size = sizeof(struct gpioline_info_changed);
2460 if (count < event_size) {
2461 spin_unlock(&cdev->wait.lock);
2465 ret = kfifo_out(&cdev->events, &event, 1);
2466 spin_unlock(&cdev->wait.lock);
2470 /* We should never get here. See lineevent_read(). */
2473 #ifdef CONFIG_GPIO_CDEV_V1
2474 if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2475 if (copy_to_user(buf + bytes_read, &event, event_size))
2478 struct gpioline_info_changed event_v1;
2480 gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2481 if (copy_to_user(buf + bytes_read, &event_v1,
2486 if (copy_to_user(buf + bytes_read, &event, event_size))
2489 bytes_read += event_size;
2490 } while (count >= bytes_read + sizeof(event));
2496 * gpio_chrdev_open() - open the chardev for ioctl operations
2497 * @inode: inode for this chardev
2498 * @file: file struct for storing private data
2499 * Returns 0 on success
2501 static int gpio_chrdev_open(struct inode *inode, struct file *file)
2503 struct gpio_device *gdev = container_of(inode->i_cdev,
2504 struct gpio_device, chrdev);
2505 struct gpio_chardev_data *cdev;
2508 /* Fail on open if the backing gpiochip is gone */
2512 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2516 cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2517 if (!cdev->watched_lines)
2520 init_waitqueue_head(&cdev->wait);
2521 INIT_KFIFO(cdev->events);
2524 cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
2525 ret = blocking_notifier_chain_register(&gdev->notifier,
2526 &cdev->lineinfo_changed_nb);
2528 goto out_free_bitmap;
2530 get_device(&gdev->dev);
2531 file->private_data = cdev;
2533 ret = nonseekable_open(inode, file);
2535 goto out_unregister_notifier;
2539 out_unregister_notifier:
2540 blocking_notifier_chain_unregister(&gdev->notifier,
2541 &cdev->lineinfo_changed_nb);
2543 bitmap_free(cdev->watched_lines);
2550 * gpio_chrdev_release() - close chardev after ioctl operations
2551 * @inode: inode for this chardev
2552 * @file: file struct for storing private data
2553 * Returns 0 on success
2555 static int gpio_chrdev_release(struct inode *inode, struct file *file)
2557 struct gpio_chardev_data *cdev = file->private_data;
2558 struct gpio_device *gdev = cdev->gdev;
2560 bitmap_free(cdev->watched_lines);
2561 blocking_notifier_chain_unregister(&gdev->notifier,
2562 &cdev->lineinfo_changed_nb);
2563 put_device(&gdev->dev);
2569 static const struct file_operations gpio_fileops = {
2570 .release = gpio_chrdev_release,
2571 .open = gpio_chrdev_open,
2572 .poll = lineinfo_watch_poll,
2573 .read = lineinfo_watch_read,
2574 .owner = THIS_MODULE,
2575 .llseek = no_llseek,
2576 .unlocked_ioctl = gpio_ioctl,
2577 #ifdef CONFIG_COMPAT
2578 .compat_ioctl = gpio_ioctl_compat,
2582 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2586 cdev_init(&gdev->chrdev, &gpio_fileops);
2587 gdev->chrdev.owner = THIS_MODULE;
2588 gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2590 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2594 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2595 MAJOR(devt), gdev->id);
2600 void gpiolib_cdev_unregister(struct gpio_device *gdev)
2602 cdev_device_del(&gdev->chrdev, &gdev->dev);