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 static int linehandle_validate_flags(u32 flags)
94 /* Return an error if an unknown flag is set */
95 if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
99 * Do not allow both INPUT & OUTPUT flags to be set as they are
102 if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
103 (flags & GPIOHANDLE_REQUEST_OUTPUT))
107 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
108 * the hardware actually supports enabling both at the same time the
109 * electrical result would be disastrous.
111 if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
112 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
115 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
116 if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
117 ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
118 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
121 /* Bias flags only allowed for input or output mode. */
122 if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
123 (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
124 ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
125 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
126 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
129 /* Only one bias flag can be set. */
130 if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
131 (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
132 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
133 ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
134 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
140 static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
142 assign_bit(FLAG_ACTIVE_LOW, flagsp,
143 lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
144 assign_bit(FLAG_OPEN_DRAIN, flagsp,
145 lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
146 assign_bit(FLAG_OPEN_SOURCE, flagsp,
147 lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
148 assign_bit(FLAG_PULL_UP, flagsp,
149 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
150 assign_bit(FLAG_PULL_DOWN, flagsp,
151 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
152 assign_bit(FLAG_BIAS_DISABLE, flagsp,
153 lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
156 static long linehandle_set_config(struct linehandle_state *lh,
159 struct gpiohandle_config gcnf;
160 struct gpio_desc *desc;
164 if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
168 ret = linehandle_validate_flags(lflags);
172 for (i = 0; i < lh->num_descs; i++) {
174 linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags);
177 * Lines have to be requested explicitly for input
178 * or output, else the line will be treated "as is".
180 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
181 int val = !!gcnf.default_values[i];
183 ret = gpiod_direction_output(desc, val);
186 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
187 ret = gpiod_direction_input(desc);
192 gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
197 static long linehandle_ioctl(struct file *file, unsigned int cmd,
200 struct linehandle_state *lh = file->private_data;
201 void __user *ip = (void __user *)arg;
202 struct gpiohandle_data ghd;
203 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
207 guard(srcu)(&lh->gdev->srcu);
209 if (!rcu_access_pointer(lh->gdev->chip))
213 case GPIOHANDLE_GET_LINE_VALUES_IOCTL:
214 /* NOTE: It's okay to read values of output lines */
215 ret = gpiod_get_array_value_complex(false, true,
216 lh->num_descs, lh->descs,
221 memset(&ghd, 0, sizeof(ghd));
222 for (i = 0; i < lh->num_descs; i++)
223 ghd.values[i] = test_bit(i, vals);
225 if (copy_to_user(ip, &ghd, sizeof(ghd)))
229 case GPIOHANDLE_SET_LINE_VALUES_IOCTL:
231 * All line descriptors were created at once with the same
232 * flags so just check if the first one is really output.
234 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
237 if (copy_from_user(&ghd, ip, sizeof(ghd)))
240 /* Clamp all values to [0,1] */
241 for (i = 0; i < lh->num_descs; i++)
242 __assign_bit(i, vals, ghd.values[i]);
244 /* Reuse the array setting function */
245 return gpiod_set_array_value_complex(false,
251 case GPIOHANDLE_SET_CONFIG_IOCTL:
252 return linehandle_set_config(lh, ip);
259 static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
262 return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
266 static void linehandle_free(struct linehandle_state *lh)
270 for (i = 0; i < lh->num_descs; i++)
272 gpiod_free(lh->descs[i]);
274 gpio_device_put(lh->gdev);
278 static int linehandle_release(struct inode *inode, struct file *file)
280 linehandle_free(file->private_data);
284 static const struct file_operations linehandle_fileops = {
285 .release = linehandle_release,
286 .owner = THIS_MODULE,
287 .llseek = noop_llseek,
288 .unlocked_ioctl = linehandle_ioctl,
290 .compat_ioctl = linehandle_ioctl_compat,
294 static int linehandle_create(struct gpio_device *gdev, void __user *ip)
296 struct gpiohandle_request handlereq;
297 struct linehandle_state *lh;
302 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
304 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
307 lflags = handlereq.flags;
309 ret = linehandle_validate_flags(lflags);
313 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
316 lh->gdev = gpio_device_get(gdev);
318 if (handlereq.consumer_label[0] != '\0') {
319 /* label is only initialized if consumer_label is set */
320 lh->label = kstrndup(handlereq.consumer_label,
321 sizeof(handlereq.consumer_label) - 1,
329 lh->num_descs = handlereq.lines;
331 /* Request each GPIO */
332 for (i = 0; i < handlereq.lines; i++) {
333 u32 offset = handlereq.lineoffsets[i];
334 struct gpio_desc *desc = gpio_device_get_desc(gdev, offset);
341 ret = gpiod_request_user(desc, lh->label);
345 linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
347 ret = gpiod_set_transitory(desc, false);
352 * Lines have to be requested explicitly for input
353 * or output, else the line will be treated "as is".
355 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
356 int val = !!handlereq.default_values[i];
358 ret = gpiod_direction_output(desc, val);
361 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
362 ret = gpiod_direction_input(desc);
367 gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
369 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
373 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
379 file = anon_inode_getfile("gpio-linehandle",
382 O_RDONLY | O_CLOEXEC);
385 goto out_put_unused_fd;
389 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
391 * fput() will trigger the release() callback, so do not go onto
392 * the regular error cleanup path here.
399 fd_install(fd, file);
401 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
412 #endif /* CONFIG_GPIO_CDEV_V1 */
415 * struct line - contains the state of a requested line
416 * @node: to store the object in supinfo_tree if supplemental
417 * @desc: the GPIO descriptor for this line.
418 * @req: the corresponding line request
419 * @irq: the interrupt triggered in response to events on this GPIO
420 * @edflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or
421 * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied
422 * @timestamp_ns: cache for the timestamp storing it between hardirq and
423 * IRQ thread, used to bring the timestamp close to the actual event
424 * @req_seqno: the seqno for the current edge event in the sequence of
425 * events for the corresponding line request. This is drawn from the @req.
426 * @line_seqno: the seqno for the current edge event in the sequence of
427 * events for this line.
428 * @work: the worker that implements software debouncing
429 * @debounce_period_us: the debounce period in microseconds
430 * @sw_debounced: flag indicating if the software debouncer is active
431 * @level: the current debounced physical level of the line
432 * @hdesc: the Hardware Timestamp Engine (HTE) descriptor
433 * @raw_level: the line level at the time of event
434 * @total_discard_seq: the running counter of the discarded events
435 * @last_seqno: the last sequence number before debounce period expires
439 struct gpio_desc *desc;
441 * -- edge detector specific fields --
446 * The flags for the active edge detector configuration.
448 * edflags is set by linereq_create(), linereq_free(), and
449 * linereq_set_config_unlocked(), which are themselves mutually
450 * exclusive, and is accessed by edge_irq_thread(),
451 * process_hw_ts_thread() and debounce_work_func(),
452 * which can all live with a slightly stale value.
456 * timestamp_ns and req_seqno are accessed only by
457 * edge_irq_handler() and edge_irq_thread(), which are themselves
458 * mutually exclusive, so no additional protection is necessary.
463 * line_seqno is accessed by either edge_irq_thread() or
464 * debounce_work_func(), which are themselves mutually exclusive,
465 * so no additional protection is necessary.
469 * -- debouncer specific fields --
471 struct delayed_work work;
473 * debounce_period_us is accessed by debounce_irq_handler() and
474 * process_hw_ts() which are disabled when modified by
475 * debounce_setup(), edge_detector_setup() or edge_detector_stop()
476 * or can live with a stale version when updated by
477 * edge_detector_update().
478 * The modifying functions are themselves mutually exclusive.
480 unsigned int debounce_period_us;
482 * sw_debounce is accessed by linereq_set_config(), which is the
483 * only setter, and linereq_get_values(), which can live with a
484 * slightly stale value.
486 unsigned int sw_debounced;
488 * level is accessed by debounce_work_func(), which is the only
489 * setter, and linereq_get_values() which can live with a slightly
494 struct hte_ts_desc hdesc;
496 * HTE provider sets line level at the time of event. The valid
497 * value is 0 or 1 and negative value for an error.
501 * when sw_debounce is set on HTE enabled line, this is running
502 * counter of the discarded events.
504 u32 total_discard_seq;
506 * when sw_debounce is set on HTE enabled line, this variable records
507 * last sequence number before debounce period expires.
510 #endif /* CONFIG_HTE */
514 * a rbtree of the struct lines containing supplemental info.
515 * Used to populate gpio_v2_line_info with cdev specific fields not contained
516 * in the struct gpio_desc.
517 * A line is determined to contain supplemental information by
518 * line_has_supinfo().
520 static struct rb_root supinfo_tree = RB_ROOT;
521 /* covers supinfo_tree */
522 static DEFINE_SPINLOCK(supinfo_lock);
525 * struct linereq - contains the state of a userspace line request
526 * @gdev: the GPIO device the line request pertains to
527 * @label: consumer label used to tag GPIO descriptors
528 * @num_lines: the number of lines in the lines array
529 * @wait: wait queue that handles blocking reads of events
530 * @device_unregistered_nb: notifier block for receiving gdev unregister events
531 * @event_buffer_size: the number of elements allocated in @events
532 * @events: KFIFO for the GPIO events
533 * @seqno: the sequence number for edge events generated on all lines in
534 * this line request. Note that this is not used when @num_lines is 1, as
535 * the line_seqno is then the same and is cheaper to calculate.
536 * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
537 * of configuration, particularly multi-step accesses to desc flags and
538 * changes to supinfo status.
539 * @lines: the lines held by this line request, with @num_lines elements.
542 struct gpio_device *gdev;
545 wait_queue_head_t wait;
546 struct notifier_block device_unregistered_nb;
547 u32 event_buffer_size;
548 DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
550 struct mutex config_mutex;
551 struct line lines[] __counted_by(num_lines);
554 static void supinfo_insert(struct line *line)
556 struct rb_node **new = &(supinfo_tree.rb_node), *parent = NULL;
559 guard(spinlock)(&supinfo_lock);
562 entry = container_of(*new, struct line, node);
565 if (line->desc < entry->desc) {
566 new = &((*new)->rb_left);
567 } else if (line->desc > entry->desc) {
568 new = &((*new)->rb_right);
570 /* this should never happen */
571 WARN(1, "duplicate line inserted");
576 rb_link_node(&line->node, parent, new);
577 rb_insert_color(&line->node, &supinfo_tree);
580 static void supinfo_erase(struct line *line)
582 guard(spinlock)(&supinfo_lock);
584 rb_erase(&line->node, &supinfo_tree);
587 static struct line *supinfo_find(struct gpio_desc *desc)
589 struct rb_node *node = supinfo_tree.rb_node;
593 line = container_of(node, struct line, node);
594 if (desc < line->desc)
595 node = node->rb_left;
596 else if (desc > line->desc)
597 node = node->rb_right;
604 static void supinfo_to_lineinfo(struct gpio_desc *desc,
605 struct gpio_v2_line_info *info)
607 struct gpio_v2_line_attribute *attr;
610 guard(spinlock)(&supinfo_lock);
612 line = supinfo_find(desc);
616 attr = &info->attrs[info->num_attrs];
617 attr->id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
618 attr->debounce_period_us = READ_ONCE(line->debounce_period_us);
622 static inline bool line_has_supinfo(struct line *line)
624 return READ_ONCE(line->debounce_period_us);
628 * Checks line_has_supinfo() before and after the change to avoid unnecessary
629 * supinfo_tree access.
630 * Called indirectly by linereq_create() or linereq_set_config() so line
631 * is already protected from concurrent changes.
633 static void line_set_debounce_period(struct line *line,
634 unsigned int debounce_period_us)
636 bool was_suppl = line_has_supinfo(line);
638 WRITE_ONCE(line->debounce_period_us, debounce_period_us);
640 /* if supinfo status is unchanged then we're done */
641 if (line_has_supinfo(line) == was_suppl)
644 /* supinfo status has changed, so update the tree */
648 supinfo_insert(line);
651 #define GPIO_V2_LINE_BIAS_FLAGS \
652 (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
653 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
654 GPIO_V2_LINE_FLAG_BIAS_DISABLED)
656 #define GPIO_V2_LINE_DIRECTION_FLAGS \
657 (GPIO_V2_LINE_FLAG_INPUT | \
658 GPIO_V2_LINE_FLAG_OUTPUT)
660 #define GPIO_V2_LINE_DRIVE_FLAGS \
661 (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
662 GPIO_V2_LINE_FLAG_OPEN_SOURCE)
664 #define GPIO_V2_LINE_EDGE_FLAGS \
665 (GPIO_V2_LINE_FLAG_EDGE_RISING | \
666 GPIO_V2_LINE_FLAG_EDGE_FALLING)
668 #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS
670 #define GPIO_V2_LINE_VALID_FLAGS \
671 (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
672 GPIO_V2_LINE_DIRECTION_FLAGS | \
673 GPIO_V2_LINE_DRIVE_FLAGS | \
674 GPIO_V2_LINE_EDGE_FLAGS | \
675 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
676 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
677 GPIO_V2_LINE_BIAS_FLAGS)
679 /* subset of flags relevant for edge detector configuration */
680 #define GPIO_V2_LINE_EDGE_DETECTOR_FLAGS \
681 (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
682 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
683 GPIO_V2_LINE_EDGE_FLAGS)
685 static int linereq_unregistered_notify(struct notifier_block *nb,
686 unsigned long action, void *data)
688 struct linereq *lr = container_of(nb, struct linereq,
689 device_unregistered_nb);
691 wake_up_poll(&lr->wait, EPOLLIN | EPOLLERR);
696 static void linereq_put_event(struct linereq *lr,
697 struct gpio_v2_line_event *le)
699 bool overflow = false;
701 scoped_guard(spinlock, &lr->wait.lock) {
702 if (kfifo_is_full(&lr->events)) {
704 kfifo_skip(&lr->events);
706 kfifo_in(&lr->events, le, 1);
709 wake_up_poll(&lr->wait, EPOLLIN);
711 pr_debug_ratelimited("event FIFO is full - event dropped\n");
714 static u64 line_event_timestamp(struct line *line)
716 if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
717 return ktime_get_real_ns();
718 else if (IS_ENABLED(CONFIG_HTE) &&
719 test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))
720 return line->timestamp_ns;
722 return ktime_get_ns();
725 static u32 line_event_id(int level)
727 return level ? GPIO_V2_LINE_EVENT_RISING_EDGE :
728 GPIO_V2_LINE_EVENT_FALLING_EDGE;
731 static inline char *make_irq_label(const char *orig)
738 new = kstrdup_and_replace(orig, '/', ':', GFP_KERNEL);
740 return ERR_PTR(-ENOMEM);
745 static inline void free_irq_label(const char *label)
752 static enum hte_return process_hw_ts_thread(void *p)
756 struct gpio_v2_line_event le;
761 return HTE_CB_HANDLED;
766 memset(&le, 0, sizeof(le));
768 le.timestamp_ns = line->timestamp_ns;
769 edflags = READ_ONCE(line->edflags);
771 switch (edflags & GPIO_V2_LINE_EDGE_FLAGS) {
772 case GPIO_V2_LINE_FLAG_EDGE_BOTH:
773 level = (line->raw_level >= 0) ?
775 gpiod_get_raw_value_cansleep(line->desc);
777 if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
780 le.id = line_event_id(level);
782 case GPIO_V2_LINE_FLAG_EDGE_RISING:
783 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
785 case GPIO_V2_LINE_FLAG_EDGE_FALLING:
786 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
789 return HTE_CB_HANDLED;
791 le.line_seqno = line->line_seqno;
792 le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
793 le.offset = gpio_chip_hwgpio(line->desc);
795 linereq_put_event(lr, &le);
797 return HTE_CB_HANDLED;
800 static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p)
807 return HTE_CB_HANDLED;
810 line->timestamp_ns = ts->tsc;
811 line->raw_level = ts->raw_level;
814 if (READ_ONCE(line->sw_debounced)) {
815 line->total_discard_seq++;
816 line->last_seqno = ts->seq;
817 mod_delayed_work(system_wq, &line->work,
818 usecs_to_jiffies(READ_ONCE(line->debounce_period_us)));
820 if (unlikely(ts->seq < line->line_seqno))
821 return HTE_CB_HANDLED;
823 diff_seqno = ts->seq - line->line_seqno;
824 line->line_seqno = ts->seq;
825 if (lr->num_lines != 1)
826 line->req_seqno = atomic_add_return(diff_seqno,
829 return HTE_RUN_SECOND_CB;
832 return HTE_CB_HANDLED;
835 static int hte_edge_setup(struct line *line, u64 eflags)
838 unsigned long flags = 0;
839 struct hte_ts_desc *hdesc = &line->hdesc;
841 if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
842 flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
843 HTE_FALLING_EDGE_TS :
845 if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
846 flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
850 line->total_discard_seq = 0;
852 hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags, NULL,
855 ret = hte_ts_get(NULL, hdesc, 0);
859 return hte_request_ts_ns(hdesc, process_hw_ts, process_hw_ts_thread,
865 static int hte_edge_setup(struct line *line, u64 eflags)
869 #endif /* CONFIG_HTE */
871 static irqreturn_t edge_irq_thread(int irq, void *p)
873 struct line *line = p;
874 struct linereq *lr = line->req;
875 struct gpio_v2_line_event le;
877 /* Do not leak kernel stack to userspace */
878 memset(&le, 0, sizeof(le));
880 if (line->timestamp_ns) {
881 le.timestamp_ns = line->timestamp_ns;
884 * We may be running from a nested threaded interrupt in
885 * which case we didn't get the timestamp from
886 * edge_irq_handler().
888 le.timestamp_ns = line_event_timestamp(line);
889 if (lr->num_lines != 1)
890 line->req_seqno = atomic_inc_return(&lr->seqno);
892 line->timestamp_ns = 0;
894 switch (READ_ONCE(line->edflags) & GPIO_V2_LINE_EDGE_FLAGS) {
895 case GPIO_V2_LINE_FLAG_EDGE_BOTH:
896 le.id = line_event_id(gpiod_get_value_cansleep(line->desc));
898 case GPIO_V2_LINE_FLAG_EDGE_RISING:
899 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
901 case GPIO_V2_LINE_FLAG_EDGE_FALLING:
902 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
908 le.line_seqno = line->line_seqno;
909 le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
910 le.offset = gpio_chip_hwgpio(line->desc);
912 linereq_put_event(lr, &le);
917 static irqreturn_t edge_irq_handler(int irq, void *p)
919 struct line *line = p;
920 struct linereq *lr = line->req;
923 * Just store the timestamp in hardirq context so we get it as
924 * close in time as possible to the actual event.
926 line->timestamp_ns = line_event_timestamp(line);
928 if (lr->num_lines != 1)
929 line->req_seqno = atomic_inc_return(&lr->seqno);
931 return IRQ_WAKE_THREAD;
935 * returns the current debounced logical value.
937 static bool debounced_value(struct line *line)
942 * minor race - debouncer may be stopped here, so edge_detector_stop()
943 * must leave the value unchanged so the following will read the level
944 * from when the debouncer was last running.
946 value = READ_ONCE(line->level);
948 if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
954 static irqreturn_t debounce_irq_handler(int irq, void *p)
956 struct line *line = p;
958 mod_delayed_work(system_wq, &line->work,
959 usecs_to_jiffies(READ_ONCE(line->debounce_period_us)));
964 static void debounce_work_func(struct work_struct *work)
966 struct gpio_v2_line_event le;
967 struct line *line = container_of(work, struct line, work.work);
969 u64 eflags, edflags = READ_ONCE(line->edflags);
974 if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
975 level = line->raw_level;
978 level = gpiod_get_raw_value_cansleep(line->desc);
980 pr_debug_ratelimited("debouncer failed to read line value\n");
984 if (READ_ONCE(line->level) == level)
987 WRITE_ONCE(line->level, level);
989 /* -- edge detection -- */
990 eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
994 /* switch from physical level to logical - if they differ */
995 if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
998 /* ignore edges that are not being monitored */
999 if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
1000 ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
1003 /* Do not leak kernel stack to userspace */
1004 memset(&le, 0, sizeof(le));
1007 le.timestamp_ns = line_event_timestamp(line);
1008 le.offset = gpio_chip_hwgpio(line->desc);
1010 if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) {
1011 /* discard events except the last one */
1012 line->total_discard_seq -= 1;
1013 diff_seqno = line->last_seqno - line->total_discard_seq -
1015 line->line_seqno = line->last_seqno - line->total_discard_seq;
1016 le.line_seqno = line->line_seqno;
1017 le.seqno = (lr->num_lines == 1) ?
1018 le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno);
1020 #endif /* CONFIG_HTE */
1023 le.line_seqno = line->line_seqno;
1024 le.seqno = (lr->num_lines == 1) ?
1025 le.line_seqno : atomic_inc_return(&lr->seqno);
1028 le.id = line_event_id(level);
1030 linereq_put_event(lr, &le);
1033 static int debounce_setup(struct line *line, unsigned int debounce_period_us)
1035 unsigned long irqflags;
1036 int ret, level, irq;
1040 ret = gpiod_set_debounce(line->desc, debounce_period_us);
1042 line_set_debounce_period(line, debounce_period_us);
1045 if (ret != -ENOTSUPP)
1048 if (debounce_period_us) {
1049 /* setup software debounce */
1050 level = gpiod_get_raw_value_cansleep(line->desc);
1054 if (!(IS_ENABLED(CONFIG_HTE) &&
1055 test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))) {
1056 irq = gpiod_to_irq(line->desc);
1060 label = make_irq_label(line->req->label);
1064 irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
1065 ret = request_irq(irq, debounce_irq_handler, irqflags,
1068 free_irq_label(label);
1073 ret = hte_edge_setup(line, GPIO_V2_LINE_FLAG_EDGE_BOTH);
1078 WRITE_ONCE(line->level, level);
1079 WRITE_ONCE(line->sw_debounced, 1);
1084 static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
1085 unsigned int line_idx)
1088 u64 mask = BIT_ULL(line_idx);
1090 for (i = 0; i < lc->num_attrs; i++) {
1091 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
1092 (lc->attrs[i].mask & mask))
1098 static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
1099 unsigned int line_idx)
1102 u64 mask = BIT_ULL(line_idx);
1104 for (i = 0; i < lc->num_attrs; i++) {
1105 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
1106 (lc->attrs[i].mask & mask))
1107 return lc->attrs[i].attr.debounce_period_us;
1112 static void edge_detector_stop(struct line *line)
1115 free_irq_label(free_irq(line->irq, line));
1120 if (READ_ONCE(line->edflags) & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
1121 hte_ts_put(&line->hdesc);
1124 cancel_delayed_work_sync(&line->work);
1125 WRITE_ONCE(line->sw_debounced, 0);
1126 WRITE_ONCE(line->edflags, 0);
1127 line_set_debounce_period(line, 0);
1128 /* do not change line->level - see comment in debounced_value() */
1131 static int edge_detector_setup(struct line *line,
1132 struct gpio_v2_line_config *lc,
1133 unsigned int line_idx, u64 edflags)
1135 u32 debounce_period_us;
1136 unsigned long irqflags = 0;
1141 eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
1142 if (eflags && !kfifo_initialized(&line->req->events)) {
1143 ret = kfifo_alloc(&line->req->events,
1144 line->req->event_buffer_size, GFP_KERNEL);
1148 if (gpio_v2_line_config_debounced(lc, line_idx)) {
1149 debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
1150 ret = debounce_setup(line, debounce_period_us);
1153 line_set_debounce_period(line, debounce_period_us);
1156 /* detection disabled or sw debouncer will provide edge detection */
1157 if (!eflags || READ_ONCE(line->sw_debounced))
1160 if (IS_ENABLED(CONFIG_HTE) &&
1161 (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1162 return hte_edge_setup(line, edflags);
1164 irq = gpiod_to_irq(line->desc);
1168 if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
1169 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
1170 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1171 if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
1172 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
1173 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1174 irqflags |= IRQF_ONESHOT;
1176 label = make_irq_label(line->req->label);
1178 return PTR_ERR(label);
1180 /* Request a thread to read the events */
1181 ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
1182 irqflags, label, line);
1184 free_irq_label(label);
1192 static int edge_detector_update(struct line *line,
1193 struct gpio_v2_line_config *lc,
1194 unsigned int line_idx, u64 edflags)
1198 u64 active_edflags = READ_ONCE(line->edflags);
1199 unsigned int debounce_period_us =
1200 gpio_v2_line_config_debounce_period(lc, line_idx);
1202 if ((active_edflags == edflags) &&
1203 (READ_ONCE(line->debounce_period_us) == debounce_period_us))
1206 /* sw debounced and still will be...*/
1207 if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
1208 line_set_debounce_period(line, debounce_period_us);
1210 * ensure event fifo is initialised if edge detection
1213 eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
1214 if (eflags && !kfifo_initialized(&line->req->events)) {
1215 ret = kfifo_alloc(&line->req->events,
1216 line->req->event_buffer_size,
1224 /* reconfiguring edge detection or sw debounce being disabled */
1225 if ((line->irq && !READ_ONCE(line->sw_debounced)) ||
1226 (active_edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) ||
1227 (!debounce_period_us && READ_ONCE(line->sw_debounced)))
1228 edge_detector_stop(line);
1230 return edge_detector_setup(line, lc, line_idx, edflags);
1233 static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
1234 unsigned int line_idx)
1237 u64 mask = BIT_ULL(line_idx);
1239 for (i = 0; i < lc->num_attrs; i++) {
1240 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
1241 (lc->attrs[i].mask & mask))
1242 return lc->attrs[i].attr.flags;
1247 static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
1248 unsigned int line_idx)
1251 u64 mask = BIT_ULL(line_idx);
1253 for (i = 0; i < lc->num_attrs; i++) {
1254 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
1255 (lc->attrs[i].mask & mask))
1256 return !!(lc->attrs[i].attr.values & mask);
1261 static int gpio_v2_line_flags_validate(u64 flags)
1263 /* Return an error if an unknown flag is set */
1264 if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
1267 if (!IS_ENABLED(CONFIG_HTE) &&
1268 (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1272 * Do not allow both INPUT and OUTPUT flags to be set as they are
1275 if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
1276 (flags & GPIO_V2_LINE_FLAG_OUTPUT))
1279 /* Only allow one event clock source */
1280 if (IS_ENABLED(CONFIG_HTE) &&
1281 (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) &&
1282 (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1285 /* Edge detection requires explicit input. */
1286 if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
1287 !(flags & GPIO_V2_LINE_FLAG_INPUT))
1291 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
1292 * request. If the hardware actually supports enabling both at the
1293 * same time the electrical result would be disastrous.
1295 if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
1296 (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
1299 /* Drive requires explicit output direction. */
1300 if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
1301 !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
1304 /* Bias requires explicit direction. */
1305 if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
1306 !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
1309 /* Only one bias flag can be set. */
1310 if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
1311 (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
1312 GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
1313 ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
1314 (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
1320 static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
1321 unsigned int num_lines)
1327 if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
1330 if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
1333 for (i = 0; i < num_lines; i++) {
1334 flags = gpio_v2_line_config_flags(lc, i);
1335 ret = gpio_v2_line_flags_validate(flags);
1339 /* debounce requires explicit input */
1340 if (gpio_v2_line_config_debounced(lc, i) &&
1341 !(flags & GPIO_V2_LINE_FLAG_INPUT))
1347 static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
1348 unsigned long *flagsp)
1350 assign_bit(FLAG_ACTIVE_LOW, flagsp,
1351 flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
1353 if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
1354 set_bit(FLAG_IS_OUT, flagsp);
1355 else if (flags & GPIO_V2_LINE_FLAG_INPUT)
1356 clear_bit(FLAG_IS_OUT, flagsp);
1358 assign_bit(FLAG_EDGE_RISING, flagsp,
1359 flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
1360 assign_bit(FLAG_EDGE_FALLING, flagsp,
1361 flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
1363 assign_bit(FLAG_OPEN_DRAIN, flagsp,
1364 flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
1365 assign_bit(FLAG_OPEN_SOURCE, flagsp,
1366 flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
1368 assign_bit(FLAG_PULL_UP, flagsp,
1369 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
1370 assign_bit(FLAG_PULL_DOWN, flagsp,
1371 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
1372 assign_bit(FLAG_BIAS_DISABLE, flagsp,
1373 flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
1375 assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
1376 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
1377 assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp,
1378 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
1381 static long linereq_get_values(struct linereq *lr, void __user *ip)
1383 struct gpio_v2_line_values lv;
1384 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
1385 struct gpio_desc **descs;
1386 unsigned int i, didx, num_get;
1390 /* NOTE: It's ok to read values of output lines. */
1391 if (copy_from_user(&lv, ip, sizeof(lv)))
1395 * gpiod_get_array_value_complex() requires compacted desc and val
1396 * arrays, rather than the sparse ones in lv.
1397 * Calculation of num_get and construction of the desc array is
1398 * optimized to avoid allocation for the desc array for the common
1399 * num_get == 1 case.
1401 /* scan requested lines to calculate the subset to get */
1402 for (num_get = 0, i = 0; i < lr->num_lines; i++) {
1403 if (lv.mask & BIT_ULL(i)) {
1405 /* capture desc for the num_get == 1 case */
1406 descs = &lr->lines[i].desc;
1414 /* build compacted desc array */
1415 descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
1418 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1419 if (lv.mask & BIT_ULL(i)) {
1420 descs[didx] = lr->lines[i].desc;
1425 ret = gpiod_get_array_value_complex(false, true, num_get,
1434 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1435 /* unpack compacted vals for the response */
1436 if (lv.mask & BIT_ULL(i)) {
1437 if (lr->lines[i].sw_debounced)
1438 val = debounced_value(&lr->lines[i]);
1440 val = test_bit(didx, vals);
1442 lv.bits |= BIT_ULL(i);
1447 if (copy_to_user(ip, &lv, sizeof(lv)))
1453 static long linereq_set_values(struct linereq *lr, void __user *ip)
1455 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
1456 struct gpio_v2_line_values lv;
1457 struct gpio_desc **descs;
1458 unsigned int i, didx, num_set;
1461 if (copy_from_user(&lv, ip, sizeof(lv)))
1464 guard(mutex)(&lr->config_mutex);
1467 * gpiod_set_array_value_complex() requires compacted desc and val
1468 * arrays, rather than the sparse ones in lv.
1469 * Calculation of num_set and construction of the descs and vals arrays
1470 * is optimized to minimize scanning the lv->mask, and to avoid
1471 * allocation for the desc array for the common num_set == 1 case.
1473 bitmap_zero(vals, GPIO_V2_LINES_MAX);
1474 /* scan requested lines to determine the subset to be set */
1475 for (num_set = 0, i = 0; i < lr->num_lines; i++) {
1476 if (lv.mask & BIT_ULL(i)) {
1477 /* setting inputs is not allowed */
1478 if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
1480 /* add to compacted values */
1481 if (lv.bits & BIT_ULL(i))
1482 __set_bit(num_set, vals);
1484 /* capture desc for the num_set == 1 case */
1485 descs = &lr->lines[i].desc;
1492 /* build compacted desc array */
1493 descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
1496 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1497 if (lv.mask & BIT_ULL(i)) {
1498 descs[didx] = lr->lines[i].desc;
1503 ret = gpiod_set_array_value_complex(false, true, num_set,
1511 static long linereq_set_config(struct linereq *lr, void __user *ip)
1513 struct gpio_v2_line_config lc;
1514 struct gpio_desc *desc;
1520 if (copy_from_user(&lc, ip, sizeof(lc)))
1523 ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
1527 guard(mutex)(&lr->config_mutex);
1529 for (i = 0; i < lr->num_lines; i++) {
1530 line = &lr->lines[i];
1531 desc = lr->lines[i].desc;
1532 flags = gpio_v2_line_config_flags(&lc, i);
1533 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1534 edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
1536 * Lines have to be requested explicitly for input
1537 * or output, else the line will be treated "as is".
1539 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1540 int val = gpio_v2_line_config_output_value(&lc, i);
1542 edge_detector_stop(line);
1543 ret = gpiod_direction_output(desc, val);
1546 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1547 ret = gpiod_direction_input(desc);
1551 ret = edge_detector_update(line, &lc, i, edflags);
1556 WRITE_ONCE(line->edflags, edflags);
1558 gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
1563 static long linereq_ioctl(struct file *file, unsigned int cmd,
1566 struct linereq *lr = file->private_data;
1567 void __user *ip = (void __user *)arg;
1569 guard(srcu)(&lr->gdev->srcu);
1571 if (!rcu_access_pointer(lr->gdev->chip))
1575 case GPIO_V2_LINE_GET_VALUES_IOCTL:
1576 return linereq_get_values(lr, ip);
1577 case GPIO_V2_LINE_SET_VALUES_IOCTL:
1578 return linereq_set_values(lr, ip);
1579 case GPIO_V2_LINE_SET_CONFIG_IOCTL:
1580 return linereq_set_config(lr, ip);
1586 #ifdef CONFIG_COMPAT
1587 static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
1590 return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1594 static __poll_t linereq_poll(struct file *file,
1595 struct poll_table_struct *wait)
1597 struct linereq *lr = file->private_data;
1598 __poll_t events = 0;
1600 guard(srcu)(&lr->gdev->srcu);
1602 if (!rcu_access_pointer(lr->gdev->chip))
1603 return EPOLLHUP | EPOLLERR;
1605 poll_wait(file, &lr->wait, wait);
1607 if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
1609 events = EPOLLIN | EPOLLRDNORM;
1614 static ssize_t linereq_read(struct file *file, char __user *buf,
1615 size_t count, loff_t *f_ps)
1617 struct linereq *lr = file->private_data;
1618 struct gpio_v2_line_event le;
1619 ssize_t bytes_read = 0;
1622 guard(srcu)(&lr->gdev->srcu);
1624 if (!rcu_access_pointer(lr->gdev->chip))
1627 if (count < sizeof(le))
1631 scoped_guard(spinlock, &lr->wait.lock) {
1632 if (kfifo_is_empty(&lr->events)) {
1636 if (file->f_flags & O_NONBLOCK)
1639 ret = wait_event_interruptible_locked(lr->wait,
1640 !kfifo_is_empty(&lr->events));
1645 ret = kfifo_out(&lr->events, &le, 1);
1649 * This should never happen - we were holding the
1650 * lock from the moment we learned the fifo is no
1651 * longer empty until now.
1657 if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
1659 bytes_read += sizeof(le);
1660 } while (count >= bytes_read + sizeof(le));
1665 static void linereq_free(struct linereq *lr)
1670 if (lr->device_unregistered_nb.notifier_call)
1671 blocking_notifier_chain_unregister(&lr->gdev->device_notifier,
1672 &lr->device_unregistered_nb);
1674 for (i = 0; i < lr->num_lines; i++) {
1675 line = &lr->lines[i];
1679 edge_detector_stop(line);
1680 if (line_has_supinfo(line))
1681 supinfo_erase(line);
1682 gpiod_free(line->desc);
1684 kfifo_free(&lr->events);
1686 gpio_device_put(lr->gdev);
1690 static int linereq_release(struct inode *inode, struct file *file)
1692 struct linereq *lr = file->private_data;
1698 #ifdef CONFIG_PROC_FS
1699 static void linereq_show_fdinfo(struct seq_file *out, struct file *file)
1701 struct linereq *lr = file->private_data;
1702 struct device *dev = &lr->gdev->dev;
1705 seq_printf(out, "gpio-chip:\t%s\n", dev_name(dev));
1707 for (i = 0; i < lr->num_lines; i++)
1708 seq_printf(out, "gpio-line:\t%d\n",
1709 gpio_chip_hwgpio(lr->lines[i].desc));
1713 static const struct file_operations line_fileops = {
1714 .release = linereq_release,
1715 .read = linereq_read,
1716 .poll = linereq_poll,
1717 .owner = THIS_MODULE,
1718 .llseek = noop_llseek,
1719 .unlocked_ioctl = linereq_ioctl,
1720 #ifdef CONFIG_COMPAT
1721 .compat_ioctl = linereq_ioctl_compat,
1723 #ifdef CONFIG_PROC_FS
1724 .show_fdinfo = linereq_show_fdinfo,
1728 static int linereq_create(struct gpio_device *gdev, void __user *ip)
1730 struct gpio_v2_line_request ulr;
1731 struct gpio_v2_line_config *lc;
1738 if (copy_from_user(&ulr, ip, sizeof(ulr)))
1741 if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
1744 if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
1748 ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
1752 lr = kvzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
1755 lr->num_lines = ulr.num_lines;
1757 lr->gdev = gpio_device_get(gdev);
1759 for (i = 0; i < ulr.num_lines; i++) {
1760 lr->lines[i].req = lr;
1761 WRITE_ONCE(lr->lines[i].sw_debounced, 0);
1762 INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
1765 if (ulr.consumer[0] != '\0') {
1766 /* label is only initialized if consumer is set */
1767 lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1771 goto out_free_linereq;
1775 mutex_init(&lr->config_mutex);
1776 init_waitqueue_head(&lr->wait);
1777 lr->event_buffer_size = ulr.event_buffer_size;
1778 if (lr->event_buffer_size == 0)
1779 lr->event_buffer_size = ulr.num_lines * 16;
1780 else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
1781 lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
1783 atomic_set(&lr->seqno, 0);
1785 /* Request each GPIO */
1786 for (i = 0; i < ulr.num_lines; i++) {
1787 u32 offset = ulr.offsets[i];
1788 struct gpio_desc *desc = gpio_device_get_desc(gdev, offset);
1791 ret = PTR_ERR(desc);
1792 goto out_free_linereq;
1795 ret = gpiod_request_user(desc, lr->label);
1797 goto out_free_linereq;
1799 lr->lines[i].desc = desc;
1800 flags = gpio_v2_line_config_flags(lc, i);
1801 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1803 ret = gpiod_set_transitory(desc, false);
1805 goto out_free_linereq;
1807 edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
1809 * Lines have to be requested explicitly for input
1810 * or output, else the line will be treated "as is".
1812 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1813 int val = gpio_v2_line_config_output_value(lc, i);
1815 ret = gpiod_direction_output(desc, val);
1817 goto out_free_linereq;
1818 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1819 ret = gpiod_direction_input(desc);
1821 goto out_free_linereq;
1823 ret = edge_detector_setup(&lr->lines[i], lc, i,
1826 goto out_free_linereq;
1829 lr->lines[i].edflags = edflags;
1831 gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
1833 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
1837 lr->device_unregistered_nb.notifier_call = linereq_unregistered_notify;
1838 ret = blocking_notifier_chain_register(&gdev->device_notifier,
1839 &lr->device_unregistered_nb);
1841 goto out_free_linereq;
1843 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1846 goto out_free_linereq;
1849 file = anon_inode_getfile("gpio-line", &line_fileops, lr,
1850 O_RDONLY | O_CLOEXEC);
1852 ret = PTR_ERR(file);
1853 goto out_put_unused_fd;
1857 if (copy_to_user(ip, &ulr, sizeof(ulr))) {
1859 * fput() will trigger the release() callback, so do not go onto
1860 * the regular error cleanup path here.
1867 fd_install(fd, file);
1869 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
1881 #ifdef CONFIG_GPIO_CDEV_V1
1884 * GPIO line event management
1888 * struct lineevent_state - contains the state of a userspace event
1889 * @gdev: the GPIO device the event pertains to
1890 * @label: consumer label used to tag descriptors
1891 * @desc: the GPIO descriptor held by this event
1892 * @eflags: the event flags this line was requested with
1893 * @irq: the interrupt that trigger in response to events on this GPIO
1894 * @wait: wait queue that handles blocking reads of events
1895 * @device_unregistered_nb: notifier block for receiving gdev unregister events
1896 * @events: KFIFO for the GPIO events
1897 * @timestamp: cache for the timestamp storing it between hardirq
1898 * and IRQ thread, used to bring the timestamp close to the actual
1901 struct lineevent_state {
1902 struct gpio_device *gdev;
1904 struct gpio_desc *desc;
1907 wait_queue_head_t wait;
1908 struct notifier_block device_unregistered_nb;
1909 DECLARE_KFIFO(events, struct gpioevent_data, 16);
1913 #define GPIOEVENT_REQUEST_VALID_FLAGS \
1914 (GPIOEVENT_REQUEST_RISING_EDGE | \
1915 GPIOEVENT_REQUEST_FALLING_EDGE)
1917 static __poll_t lineevent_poll(struct file *file,
1918 struct poll_table_struct *wait)
1920 struct lineevent_state *le = file->private_data;
1921 __poll_t events = 0;
1923 guard(srcu)(&le->gdev->srcu);
1925 if (!rcu_access_pointer(le->gdev->chip))
1926 return EPOLLHUP | EPOLLERR;
1928 poll_wait(file, &le->wait, wait);
1930 if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1931 events = EPOLLIN | EPOLLRDNORM;
1936 static int lineevent_unregistered_notify(struct notifier_block *nb,
1937 unsigned long action, void *data)
1939 struct lineevent_state *le = container_of(nb, struct lineevent_state,
1940 device_unregistered_nb);
1942 wake_up_poll(&le->wait, EPOLLIN | EPOLLERR);
1947 struct compat_gpioeevent_data {
1948 compat_u64 timestamp;
1952 static ssize_t lineevent_read(struct file *file, char __user *buf,
1953 size_t count, loff_t *f_ps)
1955 struct lineevent_state *le = file->private_data;
1956 struct gpioevent_data ge;
1957 ssize_t bytes_read = 0;
1961 guard(srcu)(&le->gdev->srcu);
1963 if (!rcu_access_pointer(le->gdev->chip))
1967 * When compatible system call is being used the struct gpioevent_data,
1968 * in case of at least ia32, has different size due to the alignment
1969 * differences. Because we have first member 64 bits followed by one of
1970 * 32 bits there is no gap between them. The only difference is the
1971 * padding at the end of the data structure. Hence, we calculate the
1972 * actual sizeof() and pass this as an argument to copy_to_user() to
1973 * drop unneeded bytes from the output.
1975 if (compat_need_64bit_alignment_fixup())
1976 ge_size = sizeof(struct compat_gpioeevent_data);
1978 ge_size = sizeof(struct gpioevent_data);
1979 if (count < ge_size)
1983 scoped_guard(spinlock, &le->wait.lock) {
1984 if (kfifo_is_empty(&le->events)) {
1988 if (file->f_flags & O_NONBLOCK)
1991 ret = wait_event_interruptible_locked(le->wait,
1992 !kfifo_is_empty(&le->events));
1997 ret = kfifo_out(&le->events, &ge, 1);
2001 * This should never happen - we were holding the lock
2002 * from the moment we learned the fifo is no longer
2009 if (copy_to_user(buf + bytes_read, &ge, ge_size))
2011 bytes_read += ge_size;
2012 } while (count >= bytes_read + ge_size);
2017 static void lineevent_free(struct lineevent_state *le)
2019 if (le->device_unregistered_nb.notifier_call)
2020 blocking_notifier_chain_unregister(&le->gdev->device_notifier,
2021 &le->device_unregistered_nb);
2023 free_irq_label(free_irq(le->irq, le));
2025 gpiod_free(le->desc);
2027 gpio_device_put(le->gdev);
2031 static int lineevent_release(struct inode *inode, struct file *file)
2033 lineevent_free(file->private_data);
2037 static long lineevent_ioctl(struct file *file, unsigned int cmd,
2040 struct lineevent_state *le = file->private_data;
2041 void __user *ip = (void __user *)arg;
2042 struct gpiohandle_data ghd;
2044 guard(srcu)(&le->gdev->srcu);
2046 if (!rcu_access_pointer(le->gdev->chip))
2050 * We can get the value for an event line but not set it,
2051 * because it is input by definition.
2053 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
2056 memset(&ghd, 0, sizeof(ghd));
2058 val = gpiod_get_value_cansleep(le->desc);
2061 ghd.values[0] = val;
2063 if (copy_to_user(ip, &ghd, sizeof(ghd)))
2071 #ifdef CONFIG_COMPAT
2072 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
2075 return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2079 static const struct file_operations lineevent_fileops = {
2080 .release = lineevent_release,
2081 .read = lineevent_read,
2082 .poll = lineevent_poll,
2083 .owner = THIS_MODULE,
2084 .llseek = noop_llseek,
2085 .unlocked_ioctl = lineevent_ioctl,
2086 #ifdef CONFIG_COMPAT
2087 .compat_ioctl = lineevent_ioctl_compat,
2091 static irqreturn_t lineevent_irq_thread(int irq, void *p)
2093 struct lineevent_state *le = p;
2094 struct gpioevent_data ge;
2097 /* Do not leak kernel stack to userspace */
2098 memset(&ge, 0, sizeof(ge));
2101 * We may be running from a nested threaded interrupt in which case
2102 * we didn't get the timestamp from lineevent_irq_handler().
2105 ge.timestamp = ktime_get_ns();
2107 ge.timestamp = le->timestamp;
2109 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
2110 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
2111 int level = gpiod_get_value_cansleep(le->desc);
2114 /* Emit low-to-high event */
2115 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
2117 /* Emit high-to-low event */
2118 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
2119 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
2120 /* Emit low-to-high event */
2121 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
2122 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
2123 /* Emit high-to-low event */
2124 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
2129 ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
2132 wake_up_poll(&le->wait, EPOLLIN);
2134 pr_debug_ratelimited("event FIFO is full - event dropped\n");
2139 static irqreturn_t lineevent_irq_handler(int irq, void *p)
2141 struct lineevent_state *le = p;
2144 * Just store the timestamp in hardirq context so we get it as
2145 * close in time as possible to the actual event.
2147 le->timestamp = ktime_get_ns();
2149 return IRQ_WAKE_THREAD;
2152 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
2154 struct gpioevent_request eventreq;
2155 struct lineevent_state *le;
2156 struct gpio_desc *desc;
2163 int irq, irqflags = 0;
2166 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
2169 offset = eventreq.lineoffset;
2170 lflags = eventreq.handleflags;
2171 eflags = eventreq.eventflags;
2173 desc = gpio_device_get_desc(gdev, offset);
2175 return PTR_ERR(desc);
2177 /* Return an error if a unknown flag is set */
2178 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
2179 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
2182 /* This is just wrong: we don't look for events on output lines */
2183 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
2184 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
2185 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
2188 /* Only one bias flag can be set. */
2189 if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
2190 (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
2191 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
2192 ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
2193 (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
2196 le = kzalloc(sizeof(*le), GFP_KERNEL);
2199 le->gdev = gpio_device_get(gdev);
2201 if (eventreq.consumer_label[0] != '\0') {
2202 /* label is only initialized if consumer_label is set */
2203 le->label = kstrndup(eventreq.consumer_label,
2204 sizeof(eventreq.consumer_label) - 1,
2212 ret = gpiod_request_user(desc, le->label);
2216 le->eflags = eflags;
2218 linehandle_flags_to_desc_flags(lflags, &desc->flags);
2220 ret = gpiod_direction_input(desc);
2224 gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
2226 irq = gpiod_to_irq(desc);
2232 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
2233 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
2234 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
2235 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
2236 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
2237 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
2238 irqflags |= IRQF_ONESHOT;
2240 INIT_KFIFO(le->events);
2241 init_waitqueue_head(&le->wait);
2243 le->device_unregistered_nb.notifier_call = lineevent_unregistered_notify;
2244 ret = blocking_notifier_chain_register(&gdev->device_notifier,
2245 &le->device_unregistered_nb);
2249 label = make_irq_label(le->label);
2250 if (IS_ERR(label)) {
2251 ret = PTR_ERR(label);
2255 /* Request a thread to read the events */
2256 ret = request_threaded_irq(irq,
2257 lineevent_irq_handler,
2258 lineevent_irq_thread,
2263 free_irq_label(label);
2269 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
2275 file = anon_inode_getfile("gpio-event",
2278 O_RDONLY | O_CLOEXEC);
2280 ret = PTR_ERR(file);
2281 goto out_put_unused_fd;
2285 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
2287 * fput() will trigger the release() callback, so do not go onto
2288 * the regular error cleanup path here.
2295 fd_install(fd, file);
2306 static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
2307 struct gpioline_info *info_v1)
2309 u64 flagsv2 = info_v2->flags;
2311 memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
2312 memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
2313 info_v1->line_offset = info_v2->offset;
2316 if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
2317 info_v1->flags |= GPIOLINE_FLAG_KERNEL;
2319 if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
2320 info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
2322 if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
2323 info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
2325 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
2326 info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
2327 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
2328 info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
2330 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
2331 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
2332 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
2333 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
2334 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
2335 info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
2338 static void gpio_v2_line_info_changed_to_v1(
2339 struct gpio_v2_line_info_changed *lic_v2,
2340 struct gpioline_info_changed *lic_v1)
2342 memset(lic_v1, 0, sizeof(*lic_v1));
2343 gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
2344 lic_v1->timestamp = lic_v2->timestamp_ns;
2345 lic_v1->event_type = lic_v2->event_type;
2348 #endif /* CONFIG_GPIO_CDEV_V1 */
2350 static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
2351 struct gpio_v2_line_info *info)
2353 unsigned long dflags;
2356 CLASS(gpio_chip_guard, guard)(desc);
2360 memset(info, 0, sizeof(*info));
2361 info->offset = gpio_chip_hwgpio(desc);
2364 strscpy(info->name, desc->name, sizeof(info->name));
2366 dflags = READ_ONCE(desc->flags);
2368 scoped_guard(srcu, &desc->gdev->desc_srcu) {
2369 label = gpiod_get_label(desc);
2370 if (label && test_bit(FLAG_REQUESTED, &dflags))
2371 strscpy(info->consumer, label,
2372 sizeof(info->consumer));
2376 * Userspace only need know that the kernel is using this GPIO so it
2378 * The calculation of the used flag is slightly racy, as it may read
2379 * desc, gc and pinctrl state without a lock covering all three at
2380 * once. Worst case if the line is in transition and the calculation
2381 * is inconsistent then it looks to the user like they performed the
2382 * read on the other side of the transition - but that can always
2384 * The definitive test that a line is available to userspace is to
2387 if (test_bit(FLAG_REQUESTED, &dflags) ||
2388 test_bit(FLAG_IS_HOGGED, &dflags) ||
2389 test_bit(FLAG_USED_AS_IRQ, &dflags) ||
2390 test_bit(FLAG_EXPORT, &dflags) ||
2391 test_bit(FLAG_SYSFS, &dflags) ||
2392 !gpiochip_line_is_valid(guard.gc, info->offset) ||
2393 !pinctrl_gpio_can_use_line(guard.gc, info->offset))
2394 info->flags |= GPIO_V2_LINE_FLAG_USED;
2396 if (test_bit(FLAG_IS_OUT, &dflags))
2397 info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
2399 info->flags |= GPIO_V2_LINE_FLAG_INPUT;
2401 if (test_bit(FLAG_ACTIVE_LOW, &dflags))
2402 info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
2404 if (test_bit(FLAG_OPEN_DRAIN, &dflags))
2405 info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
2406 if (test_bit(FLAG_OPEN_SOURCE, &dflags))
2407 info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
2409 if (test_bit(FLAG_BIAS_DISABLE, &dflags))
2410 info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
2411 if (test_bit(FLAG_PULL_DOWN, &dflags))
2412 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
2413 if (test_bit(FLAG_PULL_UP, &dflags))
2414 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
2416 if (test_bit(FLAG_EDGE_RISING, &dflags))
2417 info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
2418 if (test_bit(FLAG_EDGE_FALLING, &dflags))
2419 info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
2421 if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &dflags))
2422 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
2423 else if (test_bit(FLAG_EVENT_CLOCK_HTE, &dflags))
2424 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
2427 struct gpio_chardev_data {
2428 struct gpio_device *gdev;
2429 wait_queue_head_t wait;
2430 DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
2431 struct notifier_block lineinfo_changed_nb;
2432 struct notifier_block device_unregistered_nb;
2433 unsigned long *watched_lines;
2434 #ifdef CONFIG_GPIO_CDEV_V1
2435 atomic_t watch_abi_version;
2439 static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
2441 struct gpio_device *gdev = cdev->gdev;
2442 struct gpiochip_info chipinfo;
2444 memset(&chipinfo, 0, sizeof(chipinfo));
2446 strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
2447 strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
2448 chipinfo.lines = gdev->ngpio;
2449 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
2454 #ifdef CONFIG_GPIO_CDEV_V1
2456 * returns 0 if the versions match, else the previously selected ABI version
2458 static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
2459 unsigned int version)
2461 int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
2463 if (abiv == version)
2469 static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
2472 struct gpio_desc *desc;
2473 struct gpioline_info lineinfo;
2474 struct gpio_v2_line_info lineinfo_v2;
2476 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2479 /* this doubles as a range check on line_offset */
2480 desc = gpio_device_get_desc(cdev->gdev, lineinfo.line_offset);
2482 return PTR_ERR(desc);
2485 if (lineinfo_ensure_abi_version(cdev, 1))
2488 if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
2492 gpio_desc_to_lineinfo(desc, &lineinfo_v2);
2493 gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
2495 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2497 clear_bit(lineinfo.line_offset, cdev->watched_lines);
2505 static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
2508 struct gpio_desc *desc;
2509 struct gpio_v2_line_info lineinfo;
2511 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2514 if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2517 desc = gpio_device_get_desc(cdev->gdev, lineinfo.offset);
2519 return PTR_ERR(desc);
2522 #ifdef CONFIG_GPIO_CDEV_V1
2523 if (lineinfo_ensure_abi_version(cdev, 2))
2526 if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2529 gpio_desc_to_lineinfo(desc, &lineinfo);
2530 supinfo_to_lineinfo(desc, &lineinfo);
2532 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2534 clear_bit(lineinfo.offset, cdev->watched_lines);
2541 static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
2545 if (copy_from_user(&offset, ip, sizeof(offset)))
2548 if (offset >= cdev->gdev->ngpio)
2551 if (!test_and_clear_bit(offset, cdev->watched_lines))
2558 * gpio_ioctl() - ioctl handler for the GPIO chardev
2560 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2562 struct gpio_chardev_data *cdev = file->private_data;
2563 struct gpio_device *gdev = cdev->gdev;
2564 void __user *ip = (void __user *)arg;
2566 guard(srcu)(&gdev->srcu);
2568 /* We fail any subsequent ioctl():s when the chip is gone */
2569 if (!rcu_access_pointer(gdev->chip))
2572 /* Fill in the struct and pass to userspace */
2574 case GPIO_GET_CHIPINFO_IOCTL:
2575 return chipinfo_get(cdev, ip);
2576 #ifdef CONFIG_GPIO_CDEV_V1
2577 case GPIO_GET_LINEHANDLE_IOCTL:
2578 return linehandle_create(gdev, ip);
2579 case GPIO_GET_LINEEVENT_IOCTL:
2580 return lineevent_create(gdev, ip);
2581 case GPIO_GET_LINEINFO_IOCTL:
2582 return lineinfo_get_v1(cdev, ip, false);
2583 case GPIO_GET_LINEINFO_WATCH_IOCTL:
2584 return lineinfo_get_v1(cdev, ip, true);
2585 #endif /* CONFIG_GPIO_CDEV_V1 */
2586 case GPIO_V2_GET_LINEINFO_IOCTL:
2587 return lineinfo_get(cdev, ip, false);
2588 case GPIO_V2_GET_LINEINFO_WATCH_IOCTL:
2589 return lineinfo_get(cdev, ip, true);
2590 case GPIO_V2_GET_LINE_IOCTL:
2591 return linereq_create(gdev, ip);
2592 case GPIO_GET_LINEINFO_UNWATCH_IOCTL:
2593 return lineinfo_unwatch(cdev, ip);
2599 #ifdef CONFIG_COMPAT
2600 static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2603 return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2607 static int lineinfo_changed_notify(struct notifier_block *nb,
2608 unsigned long action, void *data)
2610 struct gpio_chardev_data *cdev =
2611 container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2612 struct gpio_v2_line_info_changed chg;
2613 struct gpio_desc *desc = data;
2616 if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2619 memset(&chg, 0, sizeof(chg));
2620 chg.event_type = action;
2621 chg.timestamp_ns = ktime_get_ns();
2622 gpio_desc_to_lineinfo(desc, &chg.info);
2623 supinfo_to_lineinfo(desc, &chg.info);
2625 ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2627 wake_up_poll(&cdev->wait, EPOLLIN);
2629 pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2634 static int gpio_device_unregistered_notify(struct notifier_block *nb,
2635 unsigned long action, void *data)
2637 struct gpio_chardev_data *cdev = container_of(nb,
2638 struct gpio_chardev_data,
2639 device_unregistered_nb);
2641 wake_up_poll(&cdev->wait, EPOLLIN | EPOLLERR);
2646 static __poll_t lineinfo_watch_poll(struct file *file,
2647 struct poll_table_struct *pollt)
2649 struct gpio_chardev_data *cdev = file->private_data;
2650 __poll_t events = 0;
2652 guard(srcu)(&cdev->gdev->srcu);
2654 if (!rcu_access_pointer(cdev->gdev->chip))
2655 return EPOLLHUP | EPOLLERR;
2657 poll_wait(file, &cdev->wait, pollt);
2659 if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2661 events = EPOLLIN | EPOLLRDNORM;
2666 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2667 size_t count, loff_t *off)
2669 struct gpio_chardev_data *cdev = file->private_data;
2670 struct gpio_v2_line_info_changed event;
2671 ssize_t bytes_read = 0;
2675 guard(srcu)(&cdev->gdev->srcu);
2677 if (!rcu_access_pointer(cdev->gdev->chip))
2680 #ifndef CONFIG_GPIO_CDEV_V1
2681 event_size = sizeof(struct gpio_v2_line_info_changed);
2682 if (count < event_size)
2687 scoped_guard(spinlock, &cdev->wait.lock) {
2688 if (kfifo_is_empty(&cdev->events)) {
2692 if (file->f_flags & O_NONBLOCK)
2695 ret = wait_event_interruptible_locked(cdev->wait,
2696 !kfifo_is_empty(&cdev->events));
2700 #ifdef CONFIG_GPIO_CDEV_V1
2701 /* must be after kfifo check so watch_abi_version is set */
2702 if (atomic_read(&cdev->watch_abi_version) == 2)
2703 event_size = sizeof(struct gpio_v2_line_info_changed);
2705 event_size = sizeof(struct gpioline_info_changed);
2706 if (count < event_size)
2709 ret = kfifo_out(&cdev->events, &event, 1);
2714 /* We should never get here. See lineevent_read(). */
2717 #ifdef CONFIG_GPIO_CDEV_V1
2718 if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2719 if (copy_to_user(buf + bytes_read, &event, event_size))
2722 struct gpioline_info_changed event_v1;
2724 gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2725 if (copy_to_user(buf + bytes_read, &event_v1,
2730 if (copy_to_user(buf + bytes_read, &event, event_size))
2733 bytes_read += event_size;
2734 } while (count >= bytes_read + sizeof(event));
2740 * gpio_chrdev_open() - open the chardev for ioctl operations
2741 * @inode: inode for this chardev
2742 * @file: file struct for storing private data
2743 * Returns 0 on success
2745 static int gpio_chrdev_open(struct inode *inode, struct file *file)
2747 struct gpio_device *gdev = container_of(inode->i_cdev,
2748 struct gpio_device, chrdev);
2749 struct gpio_chardev_data *cdev;
2752 guard(srcu)(&gdev->srcu);
2754 /* Fail on open if the backing gpiochip is gone */
2755 if (!rcu_access_pointer(gdev->chip))
2758 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2762 cdev->watched_lines = bitmap_zalloc(gdev->ngpio, GFP_KERNEL);
2763 if (!cdev->watched_lines)
2766 init_waitqueue_head(&cdev->wait);
2767 INIT_KFIFO(cdev->events);
2768 cdev->gdev = gpio_device_get(gdev);
2770 cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
2771 ret = blocking_notifier_chain_register(&gdev->line_state_notifier,
2772 &cdev->lineinfo_changed_nb);
2774 goto out_free_bitmap;
2776 cdev->device_unregistered_nb.notifier_call =
2777 gpio_device_unregistered_notify;
2778 ret = blocking_notifier_chain_register(&gdev->device_notifier,
2779 &cdev->device_unregistered_nb);
2781 goto out_unregister_line_notifier;
2783 file->private_data = cdev;
2785 ret = nonseekable_open(inode, file);
2787 goto out_unregister_device_notifier;
2791 out_unregister_device_notifier:
2792 blocking_notifier_chain_unregister(&gdev->device_notifier,
2793 &cdev->device_unregistered_nb);
2794 out_unregister_line_notifier:
2795 blocking_notifier_chain_unregister(&gdev->line_state_notifier,
2796 &cdev->lineinfo_changed_nb);
2798 gpio_device_put(gdev);
2799 bitmap_free(cdev->watched_lines);
2806 * gpio_chrdev_release() - close chardev after ioctl operations
2807 * @inode: inode for this chardev
2808 * @file: file struct for storing private data
2809 * Returns 0 on success
2811 static int gpio_chrdev_release(struct inode *inode, struct file *file)
2813 struct gpio_chardev_data *cdev = file->private_data;
2814 struct gpio_device *gdev = cdev->gdev;
2816 blocking_notifier_chain_unregister(&gdev->device_notifier,
2817 &cdev->device_unregistered_nb);
2818 blocking_notifier_chain_unregister(&gdev->line_state_notifier,
2819 &cdev->lineinfo_changed_nb);
2820 bitmap_free(cdev->watched_lines);
2821 gpio_device_put(gdev);
2827 static const struct file_operations gpio_fileops = {
2828 .release = gpio_chrdev_release,
2829 .open = gpio_chrdev_open,
2830 .poll = lineinfo_watch_poll,
2831 .read = lineinfo_watch_read,
2832 .owner = THIS_MODULE,
2833 .llseek = no_llseek,
2834 .unlocked_ioctl = gpio_ioctl,
2835 #ifdef CONFIG_COMPAT
2836 .compat_ioctl = gpio_ioctl_compat,
2840 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2842 struct gpio_chip *gc;
2845 cdev_init(&gdev->chrdev, &gpio_fileops);
2846 gdev->chrdev.owner = THIS_MODULE;
2847 gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2849 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2853 guard(srcu)(&gdev->srcu);
2854 gc = srcu_dereference(gdev->chip, &gdev->srcu);
2858 chip_dbg(gc, "added GPIO chardev (%d:%d)\n", MAJOR(devt), gdev->id);
2863 void gpiolib_cdev_unregister(struct gpio_device *gdev)
2865 cdev_device_del(&gdev->chrdev, &gdev->dev);
2866 blocking_notifier_call_chain(&gdev->device_notifier, 0, NULL);