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/cleanup.h>
9 #include <linux/compat.h>
10 #include <linux/compiler.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/file.h>
14 #include <linux/gpio.h>
15 #include <linux/gpio/driver.h>
16 #include <linux/hte.h>
17 #include <linux/interrupt.h>
18 #include <linux/irqreturn.h>
19 #include <linux/kernel.h>
20 #include <linux/kfifo.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/overflow.h>
24 #include <linux/pinctrl/consumer.h>
25 #include <linux/poll.h>
26 #include <linux/rbtree.h>
27 #include <linux/seq_file.h>
28 #include <linux/spinlock.h>
29 #include <linux/timekeeping.h>
30 #include <linux/uaccess.h>
31 #include <linux/workqueue.h>
33 #include <uapi/linux/gpio.h>
36 #include "gpiolib-cdev.h"
39 * Array sizes must ensure 64-bit alignment and not create holes in the
42 static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2));
43 static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8));
46 * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility
48 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8));
49 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8));
50 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8));
51 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8));
52 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8));
53 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8));
54 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8));
55 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8));
57 /* Character device interface to GPIO.
59 * The GPIO character device, /dev/gpiochipN, provides userspace an
60 * interface to gpiolib GPIOs via ioctl()s.
64 * GPIO line handle management
67 #ifdef CONFIG_GPIO_CDEV_V1
69 * struct linehandle_state - contains the state of a userspace handle
70 * @gdev: the GPIO device the handle pertains to
71 * @label: consumer label used to tag descriptors
72 * @descs: the GPIO descriptors held by this handle
73 * @num_descs: the number of descriptors held in the descs array
75 struct linehandle_state {
76 struct gpio_device *gdev;
78 struct gpio_desc *descs[GPIOHANDLES_MAX];
82 #define GPIOHANDLE_REQUEST_VALID_FLAGS \
83 (GPIOHANDLE_REQUEST_INPUT | \
84 GPIOHANDLE_REQUEST_OUTPUT | \
85 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
86 GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
87 GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
88 GPIOHANDLE_REQUEST_BIAS_DISABLE | \
89 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
90 GPIOHANDLE_REQUEST_OPEN_SOURCE)
92 #define GPIOHANDLE_REQUEST_DIRECTION_FLAGS \
93 (GPIOHANDLE_REQUEST_INPUT | \
94 GPIOHANDLE_REQUEST_OUTPUT)
96 static int linehandle_validate_flags(u32 flags)
98 /* Return an error if an unknown flag is set */
99 if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
103 * Do not allow both INPUT & OUTPUT flags to be set as they are
106 if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
107 (flags & GPIOHANDLE_REQUEST_OUTPUT))
111 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
112 * the hardware actually supports enabling both at the same time the
113 * electrical result would be disastrous.
115 if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
116 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
119 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
120 if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
121 ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
122 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
125 /* Bias flags only allowed for input or output mode. */
126 if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
127 (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
128 ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
129 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
130 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
133 /* Only one bias flag can be set. */
134 if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
135 (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
136 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
137 ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
138 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
144 static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
146 assign_bit(FLAG_ACTIVE_LOW, flagsp,
147 lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
148 assign_bit(FLAG_OPEN_DRAIN, flagsp,
149 lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
150 assign_bit(FLAG_OPEN_SOURCE, flagsp,
151 lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
152 assign_bit(FLAG_PULL_UP, flagsp,
153 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
154 assign_bit(FLAG_PULL_DOWN, flagsp,
155 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
156 assign_bit(FLAG_BIAS_DISABLE, flagsp,
157 lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
160 static long linehandle_set_config(struct linehandle_state *lh,
163 struct gpiohandle_config gcnf;
164 struct gpio_desc *desc;
168 if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
172 ret = linehandle_validate_flags(lflags);
176 /* Lines must be reconfigured explicitly as input or output. */
177 if (!(lflags & GPIOHANDLE_REQUEST_DIRECTION_FLAGS))
180 for (i = 0; i < lh->num_descs; i++) {
182 linehandle_flags_to_desc_flags(lflags, &desc->flags);
184 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
185 int val = !!gcnf.default_values[i];
187 ret = gpiod_direction_output(desc, val);
191 ret = gpiod_direction_input(desc);
196 gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
201 static long linehandle_ioctl(struct file *file, unsigned int cmd,
204 struct linehandle_state *lh = file->private_data;
205 void __user *ip = (void __user *)arg;
206 struct gpiohandle_data ghd;
207 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
211 guard(srcu)(&lh->gdev->srcu);
213 if (!rcu_access_pointer(lh->gdev->chip))
217 case GPIOHANDLE_GET_LINE_VALUES_IOCTL:
218 /* NOTE: It's okay to read values of output lines */
219 ret = gpiod_get_array_value_complex(false, true,
220 lh->num_descs, lh->descs,
225 memset(&ghd, 0, sizeof(ghd));
226 for (i = 0; i < lh->num_descs; i++)
227 ghd.values[i] = test_bit(i, vals);
229 if (copy_to_user(ip, &ghd, sizeof(ghd)))
233 case GPIOHANDLE_SET_LINE_VALUES_IOCTL:
235 * All line descriptors were created at once with the same
236 * flags so just check if the first one is really output.
238 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
241 if (copy_from_user(&ghd, ip, sizeof(ghd)))
244 /* Clamp all values to [0,1] */
245 for (i = 0; i < lh->num_descs; i++)
246 __assign_bit(i, vals, ghd.values[i]);
248 /* Reuse the array setting function */
249 return gpiod_set_array_value_complex(false,
255 case GPIOHANDLE_SET_CONFIG_IOCTL:
256 return linehandle_set_config(lh, ip);
263 static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
266 return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
270 static void linehandle_free(struct linehandle_state *lh)
274 for (i = 0; i < lh->num_descs; i++)
276 gpiod_free(lh->descs[i]);
278 gpio_device_put(lh->gdev);
282 static int linehandle_release(struct inode *inode, struct file *file)
284 linehandle_free(file->private_data);
288 static const struct file_operations linehandle_fileops = {
289 .release = linehandle_release,
290 .owner = THIS_MODULE,
291 .llseek = noop_llseek,
292 .unlocked_ioctl = linehandle_ioctl,
294 .compat_ioctl = linehandle_ioctl_compat,
298 static int linehandle_create(struct gpio_device *gdev, void __user *ip)
300 struct gpiohandle_request handlereq;
301 struct linehandle_state *lh;
306 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
308 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
311 lflags = handlereq.flags;
313 ret = linehandle_validate_flags(lflags);
317 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
320 lh->gdev = gpio_device_get(gdev);
322 if (handlereq.consumer_label[0] != '\0') {
323 /* label is only initialized if consumer_label is set */
324 lh->label = kstrndup(handlereq.consumer_label,
325 sizeof(handlereq.consumer_label) - 1,
333 lh->num_descs = handlereq.lines;
335 /* Request each GPIO */
336 for (i = 0; i < handlereq.lines; i++) {
337 u32 offset = handlereq.lineoffsets[i];
338 struct gpio_desc *desc = gpio_device_get_desc(gdev, offset);
345 ret = gpiod_request_user(desc, lh->label);
349 linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
351 ret = gpiod_set_transitory(desc, false);
356 * Lines have to be requested explicitly for input
357 * or output, else the line will be treated "as is".
359 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
360 int val = !!handlereq.default_values[i];
362 ret = gpiod_direction_output(desc, val);
365 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
366 ret = gpiod_direction_input(desc);
371 gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
373 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
377 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
383 file = anon_inode_getfile("gpio-linehandle",
386 O_RDONLY | O_CLOEXEC);
389 goto out_put_unused_fd;
393 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
395 * fput() will trigger the release() callback, so do not go onto
396 * the regular error cleanup path here.
403 fd_install(fd, file);
405 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
416 #endif /* CONFIG_GPIO_CDEV_V1 */
419 * struct line - contains the state of a requested line
420 * @node: to store the object in supinfo_tree if supplemental
421 * @desc: the GPIO descriptor for this line.
422 * @req: the corresponding line request
423 * @irq: the interrupt triggered in response to events on this GPIO
424 * @edflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or
425 * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied
426 * @timestamp_ns: cache for the timestamp storing it between hardirq and
427 * IRQ thread, used to bring the timestamp close to the actual event
428 * @req_seqno: the seqno for the current edge event in the sequence of
429 * events for the corresponding line request. This is drawn from the @req.
430 * @line_seqno: the seqno for the current edge event in the sequence of
431 * events for this line.
432 * @work: the worker that implements software debouncing
433 * @debounce_period_us: the debounce period in microseconds
434 * @sw_debounced: flag indicating if the software debouncer is active
435 * @level: the current debounced physical level of the line
436 * @hdesc: the Hardware Timestamp Engine (HTE) descriptor
437 * @raw_level: the line level at the time of event
438 * @total_discard_seq: the running counter of the discarded events
439 * @last_seqno: the last sequence number before debounce period expires
443 struct gpio_desc *desc;
445 * -- edge detector specific fields --
450 * The flags for the active edge detector configuration.
452 * edflags is set by linereq_create(), linereq_free(), and
453 * linereq_set_config_unlocked(), which are themselves mutually
454 * exclusive, and is accessed by edge_irq_thread(),
455 * process_hw_ts_thread() and debounce_work_func(),
456 * which can all live with a slightly stale value.
460 * timestamp_ns and req_seqno are accessed only by
461 * edge_irq_handler() and edge_irq_thread(), which are themselves
462 * mutually exclusive, so no additional protection is necessary.
467 * line_seqno is accessed by either edge_irq_thread() or
468 * debounce_work_func(), which are themselves mutually exclusive,
469 * so no additional protection is necessary.
473 * -- debouncer specific fields --
475 struct delayed_work work;
477 * debounce_period_us is accessed by debounce_irq_handler() and
478 * process_hw_ts() which are disabled when modified by
479 * debounce_setup(), edge_detector_setup() or edge_detector_stop()
480 * or can live with a stale version when updated by
481 * edge_detector_update().
482 * The modifying functions are themselves mutually exclusive.
484 unsigned int debounce_period_us;
486 * sw_debounce is accessed by linereq_set_config(), which is the
487 * only setter, and linereq_get_values(), which can live with a
488 * slightly stale value.
490 unsigned int sw_debounced;
492 * level is accessed by debounce_work_func(), which is the only
493 * setter, and linereq_get_values() which can live with a slightly
498 struct hte_ts_desc hdesc;
500 * HTE provider sets line level at the time of event. The valid
501 * value is 0 or 1 and negative value for an error.
505 * when sw_debounce is set on HTE enabled line, this is running
506 * counter of the discarded events.
508 u32 total_discard_seq;
510 * when sw_debounce is set on HTE enabled line, this variable records
511 * last sequence number before debounce period expires.
514 #endif /* CONFIG_HTE */
518 * a rbtree of the struct lines containing supplemental info.
519 * Used to populate gpio_v2_line_info with cdev specific fields not contained
520 * in the struct gpio_desc.
521 * A line is determined to contain supplemental information by
522 * line_has_supinfo().
524 static struct rb_root supinfo_tree = RB_ROOT;
525 /* covers supinfo_tree */
526 static DEFINE_SPINLOCK(supinfo_lock);
529 * struct linereq - contains the state of a userspace line request
530 * @gdev: the GPIO device the line request pertains to
531 * @label: consumer label used to tag GPIO descriptors
532 * @num_lines: the number of lines in the lines array
533 * @wait: wait queue that handles blocking reads of events
534 * @device_unregistered_nb: notifier block for receiving gdev unregister events
535 * @event_buffer_size: the number of elements allocated in @events
536 * @events: KFIFO for the GPIO events
537 * @seqno: the sequence number for edge events generated on all lines in
538 * this line request. Note that this is not used when @num_lines is 1, as
539 * the line_seqno is then the same and is cheaper to calculate.
540 * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
541 * of configuration, particularly multi-step accesses to desc flags and
542 * changes to supinfo status.
543 * @lines: the lines held by this line request, with @num_lines elements.
546 struct gpio_device *gdev;
549 wait_queue_head_t wait;
550 struct notifier_block device_unregistered_nb;
551 u32 event_buffer_size;
552 DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
554 struct mutex config_mutex;
555 struct line lines[] __counted_by(num_lines);
558 static void supinfo_insert(struct line *line)
560 struct rb_node **new = &(supinfo_tree.rb_node), *parent = NULL;
563 guard(spinlock)(&supinfo_lock);
566 entry = container_of(*new, struct line, node);
569 if (line->desc < entry->desc) {
570 new = &((*new)->rb_left);
571 } else if (line->desc > entry->desc) {
572 new = &((*new)->rb_right);
574 /* this should never happen */
575 WARN(1, "duplicate line inserted");
580 rb_link_node(&line->node, parent, new);
581 rb_insert_color(&line->node, &supinfo_tree);
584 static void supinfo_erase(struct line *line)
586 guard(spinlock)(&supinfo_lock);
588 rb_erase(&line->node, &supinfo_tree);
591 static struct line *supinfo_find(struct gpio_desc *desc)
593 struct rb_node *node = supinfo_tree.rb_node;
597 line = container_of(node, struct line, node);
598 if (desc < line->desc)
599 node = node->rb_left;
600 else if (desc > line->desc)
601 node = node->rb_right;
608 static void supinfo_to_lineinfo(struct gpio_desc *desc,
609 struct gpio_v2_line_info *info)
611 struct gpio_v2_line_attribute *attr;
614 guard(spinlock)(&supinfo_lock);
616 line = supinfo_find(desc);
620 attr = &info->attrs[info->num_attrs];
621 attr->id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
622 attr->debounce_period_us = READ_ONCE(line->debounce_period_us);
626 static inline bool line_has_supinfo(struct line *line)
628 return READ_ONCE(line->debounce_period_us);
632 * Checks line_has_supinfo() before and after the change to avoid unnecessary
633 * supinfo_tree access.
634 * Called indirectly by linereq_create() or linereq_set_config() so line
635 * is already protected from concurrent changes.
637 static void line_set_debounce_period(struct line *line,
638 unsigned int debounce_period_us)
640 bool was_suppl = line_has_supinfo(line);
642 WRITE_ONCE(line->debounce_period_us, debounce_period_us);
644 /* if supinfo status is unchanged then we're done */
645 if (line_has_supinfo(line) == was_suppl)
648 /* supinfo status has changed, so update the tree */
652 supinfo_insert(line);
655 #define GPIO_V2_LINE_BIAS_FLAGS \
656 (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
657 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
658 GPIO_V2_LINE_FLAG_BIAS_DISABLED)
660 #define GPIO_V2_LINE_DIRECTION_FLAGS \
661 (GPIO_V2_LINE_FLAG_INPUT | \
662 GPIO_V2_LINE_FLAG_OUTPUT)
664 #define GPIO_V2_LINE_DRIVE_FLAGS \
665 (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
666 GPIO_V2_LINE_FLAG_OPEN_SOURCE)
668 #define GPIO_V2_LINE_EDGE_FLAGS \
669 (GPIO_V2_LINE_FLAG_EDGE_RISING | \
670 GPIO_V2_LINE_FLAG_EDGE_FALLING)
672 #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS
674 #define GPIO_V2_LINE_VALID_FLAGS \
675 (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
676 GPIO_V2_LINE_DIRECTION_FLAGS | \
677 GPIO_V2_LINE_DRIVE_FLAGS | \
678 GPIO_V2_LINE_EDGE_FLAGS | \
679 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
680 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
681 GPIO_V2_LINE_BIAS_FLAGS)
683 /* subset of flags relevant for edge detector configuration */
684 #define GPIO_V2_LINE_EDGE_DETECTOR_FLAGS \
685 (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
686 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
687 GPIO_V2_LINE_EDGE_FLAGS)
689 static int linereq_unregistered_notify(struct notifier_block *nb,
690 unsigned long action, void *data)
692 struct linereq *lr = container_of(nb, struct linereq,
693 device_unregistered_nb);
695 wake_up_poll(&lr->wait, EPOLLIN | EPOLLERR);
700 static void linereq_put_event(struct linereq *lr,
701 struct gpio_v2_line_event *le)
703 bool overflow = false;
705 scoped_guard(spinlock, &lr->wait.lock) {
706 if (kfifo_is_full(&lr->events)) {
708 kfifo_skip(&lr->events);
710 kfifo_in(&lr->events, le, 1);
713 wake_up_poll(&lr->wait, EPOLLIN);
715 pr_debug_ratelimited("event FIFO is full - event dropped\n");
718 static u64 line_event_timestamp(struct line *line)
720 if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
721 return ktime_get_real_ns();
722 else if (IS_ENABLED(CONFIG_HTE) &&
723 test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))
724 return line->timestamp_ns;
726 return ktime_get_ns();
729 static u32 line_event_id(int level)
731 return level ? GPIO_V2_LINE_EVENT_RISING_EDGE :
732 GPIO_V2_LINE_EVENT_FALLING_EDGE;
735 static inline char *make_irq_label(const char *orig)
742 new = kstrdup_and_replace(orig, '/', ':', GFP_KERNEL);
744 return ERR_PTR(-ENOMEM);
749 static inline void free_irq_label(const char *label)
756 static enum hte_return process_hw_ts_thread(void *p)
760 struct gpio_v2_line_event le;
765 return HTE_CB_HANDLED;
770 memset(&le, 0, sizeof(le));
772 le.timestamp_ns = line->timestamp_ns;
773 edflags = READ_ONCE(line->edflags);
775 switch (edflags & GPIO_V2_LINE_EDGE_FLAGS) {
776 case GPIO_V2_LINE_FLAG_EDGE_BOTH:
777 level = (line->raw_level >= 0) ?
779 gpiod_get_raw_value_cansleep(line->desc);
781 if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
784 le.id = line_event_id(level);
786 case GPIO_V2_LINE_FLAG_EDGE_RISING:
787 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
789 case GPIO_V2_LINE_FLAG_EDGE_FALLING:
790 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
793 return HTE_CB_HANDLED;
795 le.line_seqno = line->line_seqno;
796 le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
797 le.offset = gpio_chip_hwgpio(line->desc);
799 linereq_put_event(lr, &le);
801 return HTE_CB_HANDLED;
804 static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p)
811 return HTE_CB_HANDLED;
814 line->timestamp_ns = ts->tsc;
815 line->raw_level = ts->raw_level;
818 if (READ_ONCE(line->sw_debounced)) {
819 line->total_discard_seq++;
820 line->last_seqno = ts->seq;
821 mod_delayed_work(system_wq, &line->work,
822 usecs_to_jiffies(READ_ONCE(line->debounce_period_us)));
824 if (unlikely(ts->seq < line->line_seqno))
825 return HTE_CB_HANDLED;
827 diff_seqno = ts->seq - line->line_seqno;
828 line->line_seqno = ts->seq;
829 if (lr->num_lines != 1)
830 line->req_seqno = atomic_add_return(diff_seqno,
833 return HTE_RUN_SECOND_CB;
836 return HTE_CB_HANDLED;
839 static int hte_edge_setup(struct line *line, u64 eflags)
842 unsigned long flags = 0;
843 struct hte_ts_desc *hdesc = &line->hdesc;
845 if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
846 flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
847 HTE_FALLING_EDGE_TS :
849 if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
850 flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
854 line->total_discard_seq = 0;
856 hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags, NULL,
859 ret = hte_ts_get(NULL, hdesc, 0);
863 return hte_request_ts_ns(hdesc, process_hw_ts, process_hw_ts_thread,
869 static int hte_edge_setup(struct line *line, u64 eflags)
873 #endif /* CONFIG_HTE */
875 static irqreturn_t edge_irq_thread(int irq, void *p)
877 struct line *line = p;
878 struct linereq *lr = line->req;
879 struct gpio_v2_line_event le;
881 /* Do not leak kernel stack to userspace */
882 memset(&le, 0, sizeof(le));
884 if (line->timestamp_ns) {
885 le.timestamp_ns = line->timestamp_ns;
888 * We may be running from a nested threaded interrupt in
889 * which case we didn't get the timestamp from
890 * edge_irq_handler().
892 le.timestamp_ns = line_event_timestamp(line);
893 if (lr->num_lines != 1)
894 line->req_seqno = atomic_inc_return(&lr->seqno);
896 line->timestamp_ns = 0;
898 switch (READ_ONCE(line->edflags) & GPIO_V2_LINE_EDGE_FLAGS) {
899 case GPIO_V2_LINE_FLAG_EDGE_BOTH:
900 le.id = line_event_id(gpiod_get_value_cansleep(line->desc));
902 case GPIO_V2_LINE_FLAG_EDGE_RISING:
903 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
905 case GPIO_V2_LINE_FLAG_EDGE_FALLING:
906 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
912 le.line_seqno = line->line_seqno;
913 le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
914 le.offset = gpio_chip_hwgpio(line->desc);
916 linereq_put_event(lr, &le);
921 static irqreturn_t edge_irq_handler(int irq, void *p)
923 struct line *line = p;
924 struct linereq *lr = line->req;
927 * Just store the timestamp in hardirq context so we get it as
928 * close in time as possible to the actual event.
930 line->timestamp_ns = line_event_timestamp(line);
932 if (lr->num_lines != 1)
933 line->req_seqno = atomic_inc_return(&lr->seqno);
935 return IRQ_WAKE_THREAD;
939 * returns the current debounced logical value.
941 static bool debounced_value(struct line *line)
946 * minor race - debouncer may be stopped here, so edge_detector_stop()
947 * must leave the value unchanged so the following will read the level
948 * from when the debouncer was last running.
950 value = READ_ONCE(line->level);
952 if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
958 static irqreturn_t debounce_irq_handler(int irq, void *p)
960 struct line *line = p;
962 mod_delayed_work(system_wq, &line->work,
963 usecs_to_jiffies(READ_ONCE(line->debounce_period_us)));
968 static void debounce_work_func(struct work_struct *work)
970 struct gpio_v2_line_event le;
971 struct line *line = container_of(work, struct line, work.work);
973 u64 eflags, edflags = READ_ONCE(line->edflags);
978 if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
979 level = line->raw_level;
982 level = gpiod_get_raw_value_cansleep(line->desc);
984 pr_debug_ratelimited("debouncer failed to read line value\n");
988 if (READ_ONCE(line->level) == level)
991 WRITE_ONCE(line->level, level);
993 /* -- edge detection -- */
994 eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
998 /* switch from physical level to logical - if they differ */
999 if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
1002 /* ignore edges that are not being monitored */
1003 if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
1004 ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
1007 /* Do not leak kernel stack to userspace */
1008 memset(&le, 0, sizeof(le));
1011 le.timestamp_ns = line_event_timestamp(line);
1012 le.offset = gpio_chip_hwgpio(line->desc);
1014 if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) {
1015 /* discard events except the last one */
1016 line->total_discard_seq -= 1;
1017 diff_seqno = line->last_seqno - line->total_discard_seq -
1019 line->line_seqno = line->last_seqno - line->total_discard_seq;
1020 le.line_seqno = line->line_seqno;
1021 le.seqno = (lr->num_lines == 1) ?
1022 le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno);
1024 #endif /* CONFIG_HTE */
1027 le.line_seqno = line->line_seqno;
1028 le.seqno = (lr->num_lines == 1) ?
1029 le.line_seqno : atomic_inc_return(&lr->seqno);
1032 le.id = line_event_id(level);
1034 linereq_put_event(lr, &le);
1037 static int debounce_setup(struct line *line, unsigned int debounce_period_us)
1039 unsigned long irqflags;
1040 int ret, level, irq;
1044 ret = gpiod_set_debounce(line->desc, debounce_period_us);
1046 line_set_debounce_period(line, debounce_period_us);
1049 if (ret != -ENOTSUPP)
1052 if (debounce_period_us) {
1053 /* setup software debounce */
1054 level = gpiod_get_raw_value_cansleep(line->desc);
1058 if (!(IS_ENABLED(CONFIG_HTE) &&
1059 test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))) {
1060 irq = gpiod_to_irq(line->desc);
1064 label = make_irq_label(line->req->label);
1068 irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
1069 ret = request_irq(irq, debounce_irq_handler, irqflags,
1072 free_irq_label(label);
1077 ret = hte_edge_setup(line, GPIO_V2_LINE_FLAG_EDGE_BOTH);
1082 WRITE_ONCE(line->level, level);
1083 WRITE_ONCE(line->sw_debounced, 1);
1088 static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
1089 unsigned int line_idx)
1092 u64 mask = BIT_ULL(line_idx);
1094 for (i = 0; i < lc->num_attrs; i++) {
1095 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
1096 (lc->attrs[i].mask & mask))
1102 static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
1103 unsigned int line_idx)
1106 u64 mask = BIT_ULL(line_idx);
1108 for (i = 0; i < lc->num_attrs; i++) {
1109 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
1110 (lc->attrs[i].mask & mask))
1111 return lc->attrs[i].attr.debounce_period_us;
1116 static void edge_detector_stop(struct line *line)
1119 free_irq_label(free_irq(line->irq, line));
1124 if (READ_ONCE(line->edflags) & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
1125 hte_ts_put(&line->hdesc);
1128 cancel_delayed_work_sync(&line->work);
1129 WRITE_ONCE(line->sw_debounced, 0);
1130 WRITE_ONCE(line->edflags, 0);
1131 line_set_debounce_period(line, 0);
1132 /* do not change line->level - see comment in debounced_value() */
1135 static int edge_detector_fifo_init(struct linereq *req)
1137 if (kfifo_initialized(&req->events))
1140 return kfifo_alloc(&req->events, req->event_buffer_size, GFP_KERNEL);
1143 static int edge_detector_setup(struct line *line,
1144 struct gpio_v2_line_config *lc,
1145 unsigned int line_idx, u64 edflags)
1147 u32 debounce_period_us;
1148 unsigned long irqflags = 0;
1153 eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
1155 ret = edge_detector_fifo_init(line->req);
1159 if (gpio_v2_line_config_debounced(lc, line_idx)) {
1160 debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
1161 ret = debounce_setup(line, debounce_period_us);
1164 line_set_debounce_period(line, debounce_period_us);
1167 /* detection disabled or sw debouncer will provide edge detection */
1168 if (!eflags || READ_ONCE(line->sw_debounced))
1171 if (IS_ENABLED(CONFIG_HTE) &&
1172 (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1173 return hte_edge_setup(line, edflags);
1175 irq = gpiod_to_irq(line->desc);
1179 if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
1180 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
1181 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1182 if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
1183 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
1184 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1185 irqflags |= IRQF_ONESHOT;
1187 label = make_irq_label(line->req->label);
1189 return PTR_ERR(label);
1191 /* Request a thread to read the events */
1192 ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
1193 irqflags, label, line);
1195 free_irq_label(label);
1203 static int edge_detector_update(struct line *line,
1204 struct gpio_v2_line_config *lc,
1205 unsigned int line_idx, u64 edflags)
1207 u64 active_edflags = READ_ONCE(line->edflags);
1208 unsigned int debounce_period_us =
1209 gpio_v2_line_config_debounce_period(lc, line_idx);
1211 if ((active_edflags == edflags) &&
1212 (READ_ONCE(line->debounce_period_us) == debounce_period_us))
1215 /* sw debounced and still will be...*/
1216 if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
1217 line_set_debounce_period(line, debounce_period_us);
1219 * ensure event fifo is initialised if edge detection
1222 if (edflags & GPIO_V2_LINE_EDGE_FLAGS)
1223 return edge_detector_fifo_init(line->req);
1228 /* reconfiguring edge detection or sw debounce being disabled */
1229 if ((line->irq && !READ_ONCE(line->sw_debounced)) ||
1230 (active_edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) ||
1231 (!debounce_period_us && READ_ONCE(line->sw_debounced)))
1232 edge_detector_stop(line);
1234 return edge_detector_setup(line, lc, line_idx, edflags);
1237 static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
1238 unsigned int line_idx)
1241 u64 mask = BIT_ULL(line_idx);
1243 for (i = 0; i < lc->num_attrs; i++) {
1244 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
1245 (lc->attrs[i].mask & mask))
1246 return lc->attrs[i].attr.flags;
1251 static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
1252 unsigned int line_idx)
1255 u64 mask = BIT_ULL(line_idx);
1257 for (i = 0; i < lc->num_attrs; i++) {
1258 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
1259 (lc->attrs[i].mask & mask))
1260 return !!(lc->attrs[i].attr.values & mask);
1265 static int gpio_v2_line_flags_validate(u64 flags)
1267 /* Return an error if an unknown flag is set */
1268 if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
1271 if (!IS_ENABLED(CONFIG_HTE) &&
1272 (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1276 * Do not allow both INPUT and OUTPUT flags to be set as they are
1279 if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
1280 (flags & GPIO_V2_LINE_FLAG_OUTPUT))
1283 /* Only allow one event clock source */
1284 if (IS_ENABLED(CONFIG_HTE) &&
1285 (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) &&
1286 (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1289 /* Edge detection requires explicit input. */
1290 if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
1291 !(flags & GPIO_V2_LINE_FLAG_INPUT))
1295 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
1296 * request. If the hardware actually supports enabling both at the
1297 * same time the electrical result would be disastrous.
1299 if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
1300 (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
1303 /* Drive requires explicit output direction. */
1304 if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
1305 !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
1308 /* Bias requires explicit direction. */
1309 if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
1310 !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
1313 /* Only one bias flag can be set. */
1314 if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
1315 (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
1316 GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
1317 ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
1318 (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
1324 static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
1325 unsigned int num_lines)
1331 if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
1334 if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
1337 for (i = 0; i < num_lines; i++) {
1338 flags = gpio_v2_line_config_flags(lc, i);
1339 ret = gpio_v2_line_flags_validate(flags);
1343 /* debounce requires explicit input */
1344 if (gpio_v2_line_config_debounced(lc, i) &&
1345 !(flags & GPIO_V2_LINE_FLAG_INPUT))
1351 static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
1352 unsigned long *flagsp)
1354 assign_bit(FLAG_ACTIVE_LOW, flagsp,
1355 flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
1357 if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
1358 set_bit(FLAG_IS_OUT, flagsp);
1359 else if (flags & GPIO_V2_LINE_FLAG_INPUT)
1360 clear_bit(FLAG_IS_OUT, flagsp);
1362 assign_bit(FLAG_EDGE_RISING, flagsp,
1363 flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
1364 assign_bit(FLAG_EDGE_FALLING, flagsp,
1365 flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
1367 assign_bit(FLAG_OPEN_DRAIN, flagsp,
1368 flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
1369 assign_bit(FLAG_OPEN_SOURCE, flagsp,
1370 flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
1372 assign_bit(FLAG_PULL_UP, flagsp,
1373 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
1374 assign_bit(FLAG_PULL_DOWN, flagsp,
1375 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
1376 assign_bit(FLAG_BIAS_DISABLE, flagsp,
1377 flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
1379 assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
1380 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
1381 assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp,
1382 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
1385 static long linereq_get_values(struct linereq *lr, void __user *ip)
1387 struct gpio_v2_line_values lv;
1388 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
1389 struct gpio_desc **descs;
1390 unsigned int i, didx, num_get;
1394 /* NOTE: It's ok to read values of output lines. */
1395 if (copy_from_user(&lv, ip, sizeof(lv)))
1399 * gpiod_get_array_value_complex() requires compacted desc and val
1400 * arrays, rather than the sparse ones in lv.
1401 * Calculation of num_get and construction of the desc array is
1402 * optimized to avoid allocation for the desc array for the common
1403 * num_get == 1 case.
1405 /* scan requested lines to calculate the subset to get */
1406 for (num_get = 0, i = 0; i < lr->num_lines; i++) {
1407 if (lv.mask & BIT_ULL(i)) {
1409 /* capture desc for the num_get == 1 case */
1410 descs = &lr->lines[i].desc;
1418 /* build compacted desc array */
1419 descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
1422 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1423 if (lv.mask & BIT_ULL(i)) {
1424 descs[didx] = lr->lines[i].desc;
1429 ret = gpiod_get_array_value_complex(false, true, num_get,
1438 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1439 /* unpack compacted vals for the response */
1440 if (lv.mask & BIT_ULL(i)) {
1441 if (lr->lines[i].sw_debounced)
1442 val = debounced_value(&lr->lines[i]);
1444 val = test_bit(didx, vals);
1446 lv.bits |= BIT_ULL(i);
1451 if (copy_to_user(ip, &lv, sizeof(lv)))
1457 static long linereq_set_values(struct linereq *lr, void __user *ip)
1459 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
1460 struct gpio_v2_line_values lv;
1461 struct gpio_desc **descs;
1462 unsigned int i, didx, num_set;
1465 if (copy_from_user(&lv, ip, sizeof(lv)))
1468 guard(mutex)(&lr->config_mutex);
1471 * gpiod_set_array_value_complex() requires compacted desc and val
1472 * arrays, rather than the sparse ones in lv.
1473 * Calculation of num_set and construction of the descs and vals arrays
1474 * is optimized to minimize scanning the lv->mask, and to avoid
1475 * allocation for the desc array for the common num_set == 1 case.
1477 bitmap_zero(vals, GPIO_V2_LINES_MAX);
1478 /* scan requested lines to determine the subset to be set */
1479 for (num_set = 0, i = 0; i < lr->num_lines; i++) {
1480 if (lv.mask & BIT_ULL(i)) {
1481 /* setting inputs is not allowed */
1482 if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
1484 /* add to compacted values */
1485 if (lv.bits & BIT_ULL(i))
1486 __set_bit(num_set, vals);
1488 /* capture desc for the num_set == 1 case */
1489 descs = &lr->lines[i].desc;
1496 /* build compacted desc array */
1497 descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
1500 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1501 if (lv.mask & BIT_ULL(i)) {
1502 descs[didx] = lr->lines[i].desc;
1507 ret = gpiod_set_array_value_complex(false, true, num_set,
1515 static long linereq_set_config(struct linereq *lr, void __user *ip)
1517 struct gpio_v2_line_config lc;
1518 struct gpio_desc *desc;
1524 if (copy_from_user(&lc, ip, sizeof(lc)))
1527 ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
1531 guard(mutex)(&lr->config_mutex);
1533 for (i = 0; i < lr->num_lines; i++) {
1534 line = &lr->lines[i];
1535 desc = lr->lines[i].desc;
1536 flags = gpio_v2_line_config_flags(&lc, i);
1538 * Lines not explicitly reconfigured as input or output
1539 * are left unchanged.
1541 if (!(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
1543 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1544 edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
1545 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1546 int val = gpio_v2_line_config_output_value(&lc, i);
1548 edge_detector_stop(line);
1549 ret = gpiod_direction_output(desc, val);
1553 ret = gpiod_direction_input(desc);
1557 ret = edge_detector_update(line, &lc, i, edflags);
1562 WRITE_ONCE(line->edflags, edflags);
1564 gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
1569 static long linereq_ioctl(struct file *file, unsigned int cmd,
1572 struct linereq *lr = file->private_data;
1573 void __user *ip = (void __user *)arg;
1575 guard(srcu)(&lr->gdev->srcu);
1577 if (!rcu_access_pointer(lr->gdev->chip))
1581 case GPIO_V2_LINE_GET_VALUES_IOCTL:
1582 return linereq_get_values(lr, ip);
1583 case GPIO_V2_LINE_SET_VALUES_IOCTL:
1584 return linereq_set_values(lr, ip);
1585 case GPIO_V2_LINE_SET_CONFIG_IOCTL:
1586 return linereq_set_config(lr, ip);
1592 #ifdef CONFIG_COMPAT
1593 static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
1596 return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1600 static __poll_t linereq_poll(struct file *file,
1601 struct poll_table_struct *wait)
1603 struct linereq *lr = file->private_data;
1604 __poll_t events = 0;
1606 guard(srcu)(&lr->gdev->srcu);
1608 if (!rcu_access_pointer(lr->gdev->chip))
1609 return EPOLLHUP | EPOLLERR;
1611 poll_wait(file, &lr->wait, wait);
1613 if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
1615 events = EPOLLIN | EPOLLRDNORM;
1620 static ssize_t linereq_read(struct file *file, char __user *buf,
1621 size_t count, loff_t *f_ps)
1623 struct linereq *lr = file->private_data;
1624 struct gpio_v2_line_event le;
1625 ssize_t bytes_read = 0;
1628 guard(srcu)(&lr->gdev->srcu);
1630 if (!rcu_access_pointer(lr->gdev->chip))
1633 if (count < sizeof(le))
1637 scoped_guard(spinlock, &lr->wait.lock) {
1638 if (kfifo_is_empty(&lr->events)) {
1642 if (file->f_flags & O_NONBLOCK)
1645 ret = wait_event_interruptible_locked(lr->wait,
1646 !kfifo_is_empty(&lr->events));
1651 if (kfifo_out(&lr->events, &le, 1) != 1) {
1653 * This should never happen - we hold the
1654 * lock from the moment we learned the fifo
1655 * is no longer empty until now.
1657 WARN(1, "failed to read from non-empty kfifo");
1662 if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
1664 bytes_read += sizeof(le);
1665 } while (count >= bytes_read + sizeof(le));
1670 static void linereq_free(struct linereq *lr)
1675 if (lr->device_unregistered_nb.notifier_call)
1676 blocking_notifier_chain_unregister(&lr->gdev->device_notifier,
1677 &lr->device_unregistered_nb);
1679 for (i = 0; i < lr->num_lines; i++) {
1680 line = &lr->lines[i];
1684 edge_detector_stop(line);
1685 if (line_has_supinfo(line))
1686 supinfo_erase(line);
1687 gpiod_free(line->desc);
1689 kfifo_free(&lr->events);
1691 gpio_device_put(lr->gdev);
1695 static int linereq_release(struct inode *inode, struct file *file)
1697 struct linereq *lr = file->private_data;
1703 #ifdef CONFIG_PROC_FS
1704 static void linereq_show_fdinfo(struct seq_file *out, struct file *file)
1706 struct linereq *lr = file->private_data;
1707 struct device *dev = &lr->gdev->dev;
1710 seq_printf(out, "gpio-chip:\t%s\n", dev_name(dev));
1712 for (i = 0; i < lr->num_lines; i++)
1713 seq_printf(out, "gpio-line:\t%d\n",
1714 gpio_chip_hwgpio(lr->lines[i].desc));
1718 static const struct file_operations line_fileops = {
1719 .release = linereq_release,
1720 .read = linereq_read,
1721 .poll = linereq_poll,
1722 .owner = THIS_MODULE,
1723 .llseek = noop_llseek,
1724 .unlocked_ioctl = linereq_ioctl,
1725 #ifdef CONFIG_COMPAT
1726 .compat_ioctl = linereq_ioctl_compat,
1728 #ifdef CONFIG_PROC_FS
1729 .show_fdinfo = linereq_show_fdinfo,
1733 static int linereq_create(struct gpio_device *gdev, void __user *ip)
1735 struct gpio_v2_line_request ulr;
1736 struct gpio_v2_line_config *lc;
1743 if (copy_from_user(&ulr, ip, sizeof(ulr)))
1746 if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
1749 if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
1753 ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
1757 lr = kvzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
1760 lr->num_lines = ulr.num_lines;
1762 lr->gdev = gpio_device_get(gdev);
1764 for (i = 0; i < ulr.num_lines; i++) {
1765 lr->lines[i].req = lr;
1766 WRITE_ONCE(lr->lines[i].sw_debounced, 0);
1767 INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
1770 if (ulr.consumer[0] != '\0') {
1771 /* label is only initialized if consumer is set */
1772 lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1776 goto out_free_linereq;
1780 mutex_init(&lr->config_mutex);
1781 init_waitqueue_head(&lr->wait);
1782 INIT_KFIFO(lr->events);
1783 lr->event_buffer_size = ulr.event_buffer_size;
1784 if (lr->event_buffer_size == 0)
1785 lr->event_buffer_size = ulr.num_lines * 16;
1786 else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
1787 lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
1789 atomic_set(&lr->seqno, 0);
1791 /* Request each GPIO */
1792 for (i = 0; i < ulr.num_lines; i++) {
1793 u32 offset = ulr.offsets[i];
1794 struct gpio_desc *desc = gpio_device_get_desc(gdev, offset);
1797 ret = PTR_ERR(desc);
1798 goto out_free_linereq;
1801 ret = gpiod_request_user(desc, lr->label);
1803 goto out_free_linereq;
1805 lr->lines[i].desc = desc;
1806 flags = gpio_v2_line_config_flags(lc, i);
1807 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1809 ret = gpiod_set_transitory(desc, false);
1811 goto out_free_linereq;
1813 edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
1815 * Lines have to be requested explicitly for input
1816 * or output, else the line will be treated "as is".
1818 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1819 int val = gpio_v2_line_config_output_value(lc, i);
1821 ret = gpiod_direction_output(desc, val);
1823 goto out_free_linereq;
1824 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1825 ret = gpiod_direction_input(desc);
1827 goto out_free_linereq;
1829 ret = edge_detector_setup(&lr->lines[i], lc, i,
1832 goto out_free_linereq;
1835 lr->lines[i].edflags = edflags;
1837 gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
1839 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
1843 lr->device_unregistered_nb.notifier_call = linereq_unregistered_notify;
1844 ret = blocking_notifier_chain_register(&gdev->device_notifier,
1845 &lr->device_unregistered_nb);
1847 goto out_free_linereq;
1849 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1852 goto out_free_linereq;
1855 file = anon_inode_getfile("gpio-line", &line_fileops, lr,
1856 O_RDONLY | O_CLOEXEC);
1858 ret = PTR_ERR(file);
1859 goto out_put_unused_fd;
1863 if (copy_to_user(ip, &ulr, sizeof(ulr))) {
1865 * fput() will trigger the release() callback, so do not go onto
1866 * the regular error cleanup path here.
1873 fd_install(fd, file);
1875 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
1887 #ifdef CONFIG_GPIO_CDEV_V1
1890 * GPIO line event management
1894 * struct lineevent_state - contains the state of a userspace event
1895 * @gdev: the GPIO device the event pertains to
1896 * @label: consumer label used to tag descriptors
1897 * @desc: the GPIO descriptor held by this event
1898 * @eflags: the event flags this line was requested with
1899 * @irq: the interrupt that trigger in response to events on this GPIO
1900 * @wait: wait queue that handles blocking reads of events
1901 * @device_unregistered_nb: notifier block for receiving gdev unregister events
1902 * @events: KFIFO for the GPIO events
1903 * @timestamp: cache for the timestamp storing it between hardirq
1904 * and IRQ thread, used to bring the timestamp close to the actual
1907 struct lineevent_state {
1908 struct gpio_device *gdev;
1910 struct gpio_desc *desc;
1913 wait_queue_head_t wait;
1914 struct notifier_block device_unregistered_nb;
1915 DECLARE_KFIFO(events, struct gpioevent_data, 16);
1919 #define GPIOEVENT_REQUEST_VALID_FLAGS \
1920 (GPIOEVENT_REQUEST_RISING_EDGE | \
1921 GPIOEVENT_REQUEST_FALLING_EDGE)
1923 static __poll_t lineevent_poll(struct file *file,
1924 struct poll_table_struct *wait)
1926 struct lineevent_state *le = file->private_data;
1927 __poll_t events = 0;
1929 guard(srcu)(&le->gdev->srcu);
1931 if (!rcu_access_pointer(le->gdev->chip))
1932 return EPOLLHUP | EPOLLERR;
1934 poll_wait(file, &le->wait, wait);
1936 if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1937 events = EPOLLIN | EPOLLRDNORM;
1942 static int lineevent_unregistered_notify(struct notifier_block *nb,
1943 unsigned long action, void *data)
1945 struct lineevent_state *le = container_of(nb, struct lineevent_state,
1946 device_unregistered_nb);
1948 wake_up_poll(&le->wait, EPOLLIN | EPOLLERR);
1953 struct compat_gpioeevent_data {
1954 compat_u64 timestamp;
1958 static ssize_t lineevent_read(struct file *file, char __user *buf,
1959 size_t count, loff_t *f_ps)
1961 struct lineevent_state *le = file->private_data;
1962 struct gpioevent_data ge;
1963 ssize_t bytes_read = 0;
1967 guard(srcu)(&le->gdev->srcu);
1969 if (!rcu_access_pointer(le->gdev->chip))
1973 * When compatible system call is being used the struct gpioevent_data,
1974 * in case of at least ia32, has different size due to the alignment
1975 * differences. Because we have first member 64 bits followed by one of
1976 * 32 bits there is no gap between them. The only difference is the
1977 * padding at the end of the data structure. Hence, we calculate the
1978 * actual sizeof() and pass this as an argument to copy_to_user() to
1979 * drop unneeded bytes from the output.
1981 if (compat_need_64bit_alignment_fixup())
1982 ge_size = sizeof(struct compat_gpioeevent_data);
1984 ge_size = sizeof(struct gpioevent_data);
1985 if (count < ge_size)
1989 scoped_guard(spinlock, &le->wait.lock) {
1990 if (kfifo_is_empty(&le->events)) {
1994 if (file->f_flags & O_NONBLOCK)
1997 ret = wait_event_interruptible_locked(le->wait,
1998 !kfifo_is_empty(&le->events));
2003 if (kfifo_out(&le->events, &ge, 1) != 1) {
2005 * This should never happen - we hold the
2006 * lock from the moment we learned the fifo
2007 * is no longer empty until now.
2009 WARN(1, "failed to read from non-empty kfifo");
2014 if (copy_to_user(buf + bytes_read, &ge, ge_size))
2016 bytes_read += ge_size;
2017 } while (count >= bytes_read + ge_size);
2022 static void lineevent_free(struct lineevent_state *le)
2024 if (le->device_unregistered_nb.notifier_call)
2025 blocking_notifier_chain_unregister(&le->gdev->device_notifier,
2026 &le->device_unregistered_nb);
2028 free_irq_label(free_irq(le->irq, le));
2030 gpiod_free(le->desc);
2032 gpio_device_put(le->gdev);
2036 static int lineevent_release(struct inode *inode, struct file *file)
2038 lineevent_free(file->private_data);
2042 static long lineevent_ioctl(struct file *file, unsigned int cmd,
2045 struct lineevent_state *le = file->private_data;
2046 void __user *ip = (void __user *)arg;
2047 struct gpiohandle_data ghd;
2049 guard(srcu)(&le->gdev->srcu);
2051 if (!rcu_access_pointer(le->gdev->chip))
2055 * We can get the value for an event line but not set it,
2056 * because it is input by definition.
2058 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
2061 memset(&ghd, 0, sizeof(ghd));
2063 val = gpiod_get_value_cansleep(le->desc);
2066 ghd.values[0] = val;
2068 if (copy_to_user(ip, &ghd, sizeof(ghd)))
2076 #ifdef CONFIG_COMPAT
2077 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
2080 return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2084 static const struct file_operations lineevent_fileops = {
2085 .release = lineevent_release,
2086 .read = lineevent_read,
2087 .poll = lineevent_poll,
2088 .owner = THIS_MODULE,
2089 .llseek = noop_llseek,
2090 .unlocked_ioctl = lineevent_ioctl,
2091 #ifdef CONFIG_COMPAT
2092 .compat_ioctl = lineevent_ioctl_compat,
2096 static irqreturn_t lineevent_irq_thread(int irq, void *p)
2098 struct lineevent_state *le = p;
2099 struct gpioevent_data ge;
2102 /* Do not leak kernel stack to userspace */
2103 memset(&ge, 0, sizeof(ge));
2106 * We may be running from a nested threaded interrupt in which case
2107 * we didn't get the timestamp from lineevent_irq_handler().
2110 ge.timestamp = ktime_get_ns();
2112 ge.timestamp = le->timestamp;
2114 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
2115 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
2116 int level = gpiod_get_value_cansleep(le->desc);
2119 /* Emit low-to-high event */
2120 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
2122 /* Emit high-to-low event */
2123 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
2124 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
2125 /* Emit low-to-high event */
2126 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
2127 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
2128 /* Emit high-to-low event */
2129 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
2134 ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
2137 wake_up_poll(&le->wait, EPOLLIN);
2139 pr_debug_ratelimited("event FIFO is full - event dropped\n");
2144 static irqreturn_t lineevent_irq_handler(int irq, void *p)
2146 struct lineevent_state *le = p;
2149 * Just store the timestamp in hardirq context so we get it as
2150 * close in time as possible to the actual event.
2152 le->timestamp = ktime_get_ns();
2154 return IRQ_WAKE_THREAD;
2157 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
2159 struct gpioevent_request eventreq;
2160 struct lineevent_state *le;
2161 struct gpio_desc *desc;
2168 int irq, irqflags = 0;
2171 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
2174 offset = eventreq.lineoffset;
2175 lflags = eventreq.handleflags;
2176 eflags = eventreq.eventflags;
2178 desc = gpio_device_get_desc(gdev, offset);
2180 return PTR_ERR(desc);
2182 /* Return an error if a unknown flag is set */
2183 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
2184 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
2187 /* This is just wrong: we don't look for events on output lines */
2188 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
2189 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
2190 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
2193 /* Only one bias flag can be set. */
2194 if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
2195 (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
2196 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
2197 ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
2198 (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
2201 le = kzalloc(sizeof(*le), GFP_KERNEL);
2204 le->gdev = gpio_device_get(gdev);
2206 if (eventreq.consumer_label[0] != '\0') {
2207 /* label is only initialized if consumer_label is set */
2208 le->label = kstrndup(eventreq.consumer_label,
2209 sizeof(eventreq.consumer_label) - 1,
2217 ret = gpiod_request_user(desc, le->label);
2221 le->eflags = eflags;
2223 linehandle_flags_to_desc_flags(lflags, &desc->flags);
2225 ret = gpiod_direction_input(desc);
2229 gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
2231 irq = gpiod_to_irq(desc);
2237 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
2238 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
2239 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
2240 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
2241 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
2242 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
2243 irqflags |= IRQF_ONESHOT;
2245 INIT_KFIFO(le->events);
2246 init_waitqueue_head(&le->wait);
2248 le->device_unregistered_nb.notifier_call = lineevent_unregistered_notify;
2249 ret = blocking_notifier_chain_register(&gdev->device_notifier,
2250 &le->device_unregistered_nb);
2254 label = make_irq_label(le->label);
2255 if (IS_ERR(label)) {
2256 ret = PTR_ERR(label);
2260 /* Request a thread to read the events */
2261 ret = request_threaded_irq(irq,
2262 lineevent_irq_handler,
2263 lineevent_irq_thread,
2268 free_irq_label(label);
2274 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
2280 file = anon_inode_getfile("gpio-event",
2283 O_RDONLY | O_CLOEXEC);
2285 ret = PTR_ERR(file);
2286 goto out_put_unused_fd;
2290 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
2292 * fput() will trigger the release() callback, so do not go onto
2293 * the regular error cleanup path here.
2300 fd_install(fd, file);
2311 static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
2312 struct gpioline_info *info_v1)
2314 u64 flagsv2 = info_v2->flags;
2316 memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
2317 memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
2318 info_v1->line_offset = info_v2->offset;
2321 if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
2322 info_v1->flags |= GPIOLINE_FLAG_KERNEL;
2324 if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
2325 info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
2327 if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
2328 info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
2330 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
2331 info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
2332 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
2333 info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
2335 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
2336 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
2337 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
2338 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
2339 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
2340 info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
2343 static void gpio_v2_line_info_changed_to_v1(
2344 struct gpio_v2_line_info_changed *lic_v2,
2345 struct gpioline_info_changed *lic_v1)
2347 memset(lic_v1, 0, sizeof(*lic_v1));
2348 gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
2349 lic_v1->timestamp = lic_v2->timestamp_ns;
2350 lic_v1->event_type = lic_v2->event_type;
2353 #endif /* CONFIG_GPIO_CDEV_V1 */
2355 static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
2356 struct gpio_v2_line_info *info)
2358 unsigned long dflags;
2361 CLASS(gpio_chip_guard, guard)(desc);
2365 memset(info, 0, sizeof(*info));
2366 info->offset = gpio_chip_hwgpio(desc);
2369 strscpy(info->name, desc->name, sizeof(info->name));
2371 dflags = READ_ONCE(desc->flags);
2373 scoped_guard(srcu, &desc->gdev->desc_srcu) {
2374 label = gpiod_get_label(desc);
2375 if (label && test_bit(FLAG_REQUESTED, &dflags))
2376 strscpy(info->consumer, label,
2377 sizeof(info->consumer));
2381 * Userspace only need know that the kernel is using this GPIO so it
2383 * The calculation of the used flag is slightly racy, as it may read
2384 * desc, gc and pinctrl state without a lock covering all three at
2385 * once. Worst case if the line is in transition and the calculation
2386 * is inconsistent then it looks to the user like they performed the
2387 * read on the other side of the transition - but that can always
2389 * The definitive test that a line is available to userspace is to
2392 if (test_bit(FLAG_REQUESTED, &dflags) ||
2393 test_bit(FLAG_IS_HOGGED, &dflags) ||
2394 test_bit(FLAG_USED_AS_IRQ, &dflags) ||
2395 test_bit(FLAG_EXPORT, &dflags) ||
2396 test_bit(FLAG_SYSFS, &dflags) ||
2397 !gpiochip_line_is_valid(guard.gc, info->offset) ||
2398 !pinctrl_gpio_can_use_line(guard.gc, info->offset))
2399 info->flags |= GPIO_V2_LINE_FLAG_USED;
2401 if (test_bit(FLAG_IS_OUT, &dflags))
2402 info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
2404 info->flags |= GPIO_V2_LINE_FLAG_INPUT;
2406 if (test_bit(FLAG_ACTIVE_LOW, &dflags))
2407 info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
2409 if (test_bit(FLAG_OPEN_DRAIN, &dflags))
2410 info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
2411 if (test_bit(FLAG_OPEN_SOURCE, &dflags))
2412 info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
2414 if (test_bit(FLAG_BIAS_DISABLE, &dflags))
2415 info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
2416 if (test_bit(FLAG_PULL_DOWN, &dflags))
2417 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
2418 if (test_bit(FLAG_PULL_UP, &dflags))
2419 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
2421 if (test_bit(FLAG_EDGE_RISING, &dflags))
2422 info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
2423 if (test_bit(FLAG_EDGE_FALLING, &dflags))
2424 info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
2426 if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &dflags))
2427 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
2428 else if (test_bit(FLAG_EVENT_CLOCK_HTE, &dflags))
2429 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
2432 struct gpio_chardev_data {
2433 struct gpio_device *gdev;
2434 wait_queue_head_t wait;
2435 DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
2436 struct notifier_block lineinfo_changed_nb;
2437 struct notifier_block device_unregistered_nb;
2438 unsigned long *watched_lines;
2439 #ifdef CONFIG_GPIO_CDEV_V1
2440 atomic_t watch_abi_version;
2444 static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
2446 struct gpio_device *gdev = cdev->gdev;
2447 struct gpiochip_info chipinfo;
2449 memset(&chipinfo, 0, sizeof(chipinfo));
2451 strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
2452 strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
2453 chipinfo.lines = gdev->ngpio;
2454 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
2459 #ifdef CONFIG_GPIO_CDEV_V1
2461 * returns 0 if the versions match, else the previously selected ABI version
2463 static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
2464 unsigned int version)
2466 int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
2468 if (abiv == version)
2474 static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
2477 struct gpio_desc *desc;
2478 struct gpioline_info lineinfo;
2479 struct gpio_v2_line_info lineinfo_v2;
2481 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2484 /* this doubles as a range check on line_offset */
2485 desc = gpio_device_get_desc(cdev->gdev, lineinfo.line_offset);
2487 return PTR_ERR(desc);
2490 if (lineinfo_ensure_abi_version(cdev, 1))
2493 if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
2497 gpio_desc_to_lineinfo(desc, &lineinfo_v2);
2498 gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
2500 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2502 clear_bit(lineinfo.line_offset, cdev->watched_lines);
2510 static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
2513 struct gpio_desc *desc;
2514 struct gpio_v2_line_info lineinfo;
2516 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2519 if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2522 desc = gpio_device_get_desc(cdev->gdev, lineinfo.offset);
2524 return PTR_ERR(desc);
2527 #ifdef CONFIG_GPIO_CDEV_V1
2528 if (lineinfo_ensure_abi_version(cdev, 2))
2531 if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2534 gpio_desc_to_lineinfo(desc, &lineinfo);
2535 supinfo_to_lineinfo(desc, &lineinfo);
2537 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2539 clear_bit(lineinfo.offset, cdev->watched_lines);
2546 static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
2550 if (copy_from_user(&offset, ip, sizeof(offset)))
2553 if (offset >= cdev->gdev->ngpio)
2556 if (!test_and_clear_bit(offset, cdev->watched_lines))
2563 * gpio_ioctl() - ioctl handler for the GPIO chardev
2565 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2567 struct gpio_chardev_data *cdev = file->private_data;
2568 struct gpio_device *gdev = cdev->gdev;
2569 void __user *ip = (void __user *)arg;
2571 guard(srcu)(&gdev->srcu);
2573 /* We fail any subsequent ioctl():s when the chip is gone */
2574 if (!rcu_access_pointer(gdev->chip))
2577 /* Fill in the struct and pass to userspace */
2579 case GPIO_GET_CHIPINFO_IOCTL:
2580 return chipinfo_get(cdev, ip);
2581 #ifdef CONFIG_GPIO_CDEV_V1
2582 case GPIO_GET_LINEHANDLE_IOCTL:
2583 return linehandle_create(gdev, ip);
2584 case GPIO_GET_LINEEVENT_IOCTL:
2585 return lineevent_create(gdev, ip);
2586 case GPIO_GET_LINEINFO_IOCTL:
2587 return lineinfo_get_v1(cdev, ip, false);
2588 case GPIO_GET_LINEINFO_WATCH_IOCTL:
2589 return lineinfo_get_v1(cdev, ip, true);
2590 #endif /* CONFIG_GPIO_CDEV_V1 */
2591 case GPIO_V2_GET_LINEINFO_IOCTL:
2592 return lineinfo_get(cdev, ip, false);
2593 case GPIO_V2_GET_LINEINFO_WATCH_IOCTL:
2594 return lineinfo_get(cdev, ip, true);
2595 case GPIO_V2_GET_LINE_IOCTL:
2596 return linereq_create(gdev, ip);
2597 case GPIO_GET_LINEINFO_UNWATCH_IOCTL:
2598 return lineinfo_unwatch(cdev, ip);
2604 #ifdef CONFIG_COMPAT
2605 static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2608 return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2612 static int lineinfo_changed_notify(struct notifier_block *nb,
2613 unsigned long action, void *data)
2615 struct gpio_chardev_data *cdev =
2616 container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2617 struct gpio_v2_line_info_changed chg;
2618 struct gpio_desc *desc = data;
2621 if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2624 memset(&chg, 0, sizeof(chg));
2625 chg.event_type = action;
2626 chg.timestamp_ns = ktime_get_ns();
2627 gpio_desc_to_lineinfo(desc, &chg.info);
2628 supinfo_to_lineinfo(desc, &chg.info);
2630 ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2632 wake_up_poll(&cdev->wait, EPOLLIN);
2634 pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2639 static int gpio_device_unregistered_notify(struct notifier_block *nb,
2640 unsigned long action, void *data)
2642 struct gpio_chardev_data *cdev = container_of(nb,
2643 struct gpio_chardev_data,
2644 device_unregistered_nb);
2646 wake_up_poll(&cdev->wait, EPOLLIN | EPOLLERR);
2651 static __poll_t lineinfo_watch_poll(struct file *file,
2652 struct poll_table_struct *pollt)
2654 struct gpio_chardev_data *cdev = file->private_data;
2655 __poll_t events = 0;
2657 guard(srcu)(&cdev->gdev->srcu);
2659 if (!rcu_access_pointer(cdev->gdev->chip))
2660 return EPOLLHUP | EPOLLERR;
2662 poll_wait(file, &cdev->wait, pollt);
2664 if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2666 events = EPOLLIN | EPOLLRDNORM;
2671 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2672 size_t count, loff_t *off)
2674 struct gpio_chardev_data *cdev = file->private_data;
2675 struct gpio_v2_line_info_changed event;
2676 ssize_t bytes_read = 0;
2680 guard(srcu)(&cdev->gdev->srcu);
2682 if (!rcu_access_pointer(cdev->gdev->chip))
2685 #ifndef CONFIG_GPIO_CDEV_V1
2686 event_size = sizeof(struct gpio_v2_line_info_changed);
2687 if (count < event_size)
2692 scoped_guard(spinlock, &cdev->wait.lock) {
2693 if (kfifo_is_empty(&cdev->events)) {
2697 if (file->f_flags & O_NONBLOCK)
2700 ret = wait_event_interruptible_locked(cdev->wait,
2701 !kfifo_is_empty(&cdev->events));
2705 #ifdef CONFIG_GPIO_CDEV_V1
2706 /* must be after kfifo check so watch_abi_version is set */
2707 if (atomic_read(&cdev->watch_abi_version) == 2)
2708 event_size = sizeof(struct gpio_v2_line_info_changed);
2710 event_size = sizeof(struct gpioline_info_changed);
2711 if (count < event_size)
2714 if (kfifo_out(&cdev->events, &event, 1) != 1) {
2716 * This should never happen - we hold the
2717 * lock from the moment we learned the fifo
2718 * is no longer empty until now.
2720 WARN(1, "failed to read from non-empty kfifo");
2725 #ifdef CONFIG_GPIO_CDEV_V1
2726 if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2727 if (copy_to_user(buf + bytes_read, &event, event_size))
2730 struct gpioline_info_changed event_v1;
2732 gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2733 if (copy_to_user(buf + bytes_read, &event_v1,
2738 if (copy_to_user(buf + bytes_read, &event, event_size))
2741 bytes_read += event_size;
2742 } while (count >= bytes_read + sizeof(event));
2748 * gpio_chrdev_open() - open the chardev for ioctl operations
2749 * @inode: inode for this chardev
2750 * @file: file struct for storing private data
2751 * Returns 0 on success
2753 static int gpio_chrdev_open(struct inode *inode, struct file *file)
2755 struct gpio_device *gdev = container_of(inode->i_cdev,
2756 struct gpio_device, chrdev);
2757 struct gpio_chardev_data *cdev;
2760 guard(srcu)(&gdev->srcu);
2762 /* Fail on open if the backing gpiochip is gone */
2763 if (!rcu_access_pointer(gdev->chip))
2766 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2770 cdev->watched_lines = bitmap_zalloc(gdev->ngpio, GFP_KERNEL);
2771 if (!cdev->watched_lines)
2774 init_waitqueue_head(&cdev->wait);
2775 INIT_KFIFO(cdev->events);
2776 cdev->gdev = gpio_device_get(gdev);
2778 cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
2779 ret = blocking_notifier_chain_register(&gdev->line_state_notifier,
2780 &cdev->lineinfo_changed_nb);
2782 goto out_free_bitmap;
2784 cdev->device_unregistered_nb.notifier_call =
2785 gpio_device_unregistered_notify;
2786 ret = blocking_notifier_chain_register(&gdev->device_notifier,
2787 &cdev->device_unregistered_nb);
2789 goto out_unregister_line_notifier;
2791 file->private_data = cdev;
2793 ret = nonseekable_open(inode, file);
2795 goto out_unregister_device_notifier;
2799 out_unregister_device_notifier:
2800 blocking_notifier_chain_unregister(&gdev->device_notifier,
2801 &cdev->device_unregistered_nb);
2802 out_unregister_line_notifier:
2803 blocking_notifier_chain_unregister(&gdev->line_state_notifier,
2804 &cdev->lineinfo_changed_nb);
2806 gpio_device_put(gdev);
2807 bitmap_free(cdev->watched_lines);
2814 * gpio_chrdev_release() - close chardev after ioctl operations
2815 * @inode: inode for this chardev
2816 * @file: file struct for storing private data
2817 * Returns 0 on success
2819 static int gpio_chrdev_release(struct inode *inode, struct file *file)
2821 struct gpio_chardev_data *cdev = file->private_data;
2822 struct gpio_device *gdev = cdev->gdev;
2824 blocking_notifier_chain_unregister(&gdev->device_notifier,
2825 &cdev->device_unregistered_nb);
2826 blocking_notifier_chain_unregister(&gdev->line_state_notifier,
2827 &cdev->lineinfo_changed_nb);
2828 bitmap_free(cdev->watched_lines);
2829 gpio_device_put(gdev);
2835 static const struct file_operations gpio_fileops = {
2836 .release = gpio_chrdev_release,
2837 .open = gpio_chrdev_open,
2838 .poll = lineinfo_watch_poll,
2839 .read = lineinfo_watch_read,
2840 .owner = THIS_MODULE,
2841 .llseek = no_llseek,
2842 .unlocked_ioctl = gpio_ioctl,
2843 #ifdef CONFIG_COMPAT
2844 .compat_ioctl = gpio_ioctl_compat,
2848 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2850 struct gpio_chip *gc;
2853 cdev_init(&gdev->chrdev, &gpio_fileops);
2854 gdev->chrdev.owner = THIS_MODULE;
2855 gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2857 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2861 guard(srcu)(&gdev->srcu);
2862 gc = srcu_dereference(gdev->chip, &gdev->srcu);
2866 chip_dbg(gc, "added GPIO chardev (%d:%d)\n", MAJOR(devt), gdev->id);
2871 void gpiolib_cdev_unregister(struct gpio_device *gdev)
2873 cdev_device_del(&gdev->chrdev, &gdev->dev);
2874 blocking_notifier_call_chain(&gdev->device_notifier, 0, NULL);