1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/anon_inodes.h>
4 #include <linux/atomic.h>
5 #include <linux/bitmap.h>
6 #include <linux/build_bug.h>
7 #include <linux/cdev.h>
8 #include <linux/compat.h>
9 #include <linux/compiler.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/file.h>
13 #include <linux/gpio.h>
14 #include <linux/gpio/driver.h>
15 #include <linux/hte.h>
16 #include <linux/interrupt.h>
17 #include <linux/irqreturn.h>
18 #include <linux/kernel.h>
19 #include <linux/kfifo.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/poll.h>
24 #include <linux/seq_file.h>
25 #include <linux/spinlock.h>
26 #include <linux/timekeeping.h>
27 #include <linux/uaccess.h>
28 #include <linux/workqueue.h>
30 #include <uapi/linux/gpio.h>
33 #include "gpiolib-cdev.h"
36 * Array sizes must ensure 64-bit alignment and not create holes in the
39 static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2));
40 static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8));
43 * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility
45 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8));
46 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8));
47 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8));
48 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8));
49 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8));
50 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8));
51 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8));
52 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8));
54 /* Character device interface to GPIO.
56 * The GPIO character device, /dev/gpiochipN, provides userspace an
57 * interface to gpiolib GPIOs via ioctl()s.
61 * GPIO line handle management
64 #ifdef CONFIG_GPIO_CDEV_V1
66 * struct linehandle_state - contains the state of a userspace handle
67 * @gdev: the GPIO device the handle pertains to
68 * @label: consumer label used to tag descriptors
69 * @descs: the GPIO descriptors held by this handle
70 * @num_descs: the number of descriptors held in the descs array
72 struct linehandle_state {
73 struct gpio_device *gdev;
75 struct gpio_desc *descs[GPIOHANDLES_MAX];
79 #define GPIOHANDLE_REQUEST_VALID_FLAGS \
80 (GPIOHANDLE_REQUEST_INPUT | \
81 GPIOHANDLE_REQUEST_OUTPUT | \
82 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
83 GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
84 GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
85 GPIOHANDLE_REQUEST_BIAS_DISABLE | \
86 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
87 GPIOHANDLE_REQUEST_OPEN_SOURCE)
89 static int linehandle_validate_flags(u32 flags)
91 /* Return an error if an unknown flag is set */
92 if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
96 * Do not allow both INPUT & OUTPUT flags to be set as they are
99 if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
100 (flags & GPIOHANDLE_REQUEST_OUTPUT))
104 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
105 * the hardware actually supports enabling both at the same time the
106 * electrical result would be disastrous.
108 if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
109 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
112 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
113 if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
114 ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
115 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
118 /* Bias flags only allowed for input or output mode. */
119 if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
120 (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
121 ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
122 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
123 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
126 /* Only one bias flag can be set. */
127 if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
128 (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
129 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
130 ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
131 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
137 static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
139 assign_bit(FLAG_ACTIVE_LOW, flagsp,
140 lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
141 assign_bit(FLAG_OPEN_DRAIN, flagsp,
142 lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
143 assign_bit(FLAG_OPEN_SOURCE, flagsp,
144 lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
145 assign_bit(FLAG_PULL_UP, flagsp,
146 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
147 assign_bit(FLAG_PULL_DOWN, flagsp,
148 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
149 assign_bit(FLAG_BIAS_DISABLE, flagsp,
150 lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
153 static long linehandle_set_config(struct linehandle_state *lh,
156 struct gpiohandle_config gcnf;
157 struct gpio_desc *desc;
161 if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
165 ret = linehandle_validate_flags(lflags);
169 for (i = 0; i < lh->num_descs; i++) {
171 linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags);
174 * Lines have to be requested explicitly for input
175 * or output, else the line will be treated "as is".
177 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
178 int val = !!gcnf.default_values[i];
180 ret = gpiod_direction_output(desc, val);
183 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
184 ret = gpiod_direction_input(desc);
189 blocking_notifier_call_chain(&desc->gdev->notifier,
190 GPIO_V2_LINE_CHANGED_CONFIG,
196 static long linehandle_ioctl(struct file *file, unsigned int cmd,
199 struct linehandle_state *lh = file->private_data;
200 void __user *ip = (void __user *)arg;
201 struct gpiohandle_data ghd;
202 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
207 case GPIOHANDLE_GET_LINE_VALUES_IOCTL:
208 /* NOTE: It's okay to read values of output lines */
209 ret = gpiod_get_array_value_complex(false, true,
210 lh->num_descs, lh->descs,
215 memset(&ghd, 0, sizeof(ghd));
216 for (i = 0; i < lh->num_descs; i++)
217 ghd.values[i] = test_bit(i, vals);
219 if (copy_to_user(ip, &ghd, sizeof(ghd)))
223 case GPIOHANDLE_SET_LINE_VALUES_IOCTL:
225 * All line descriptors were created at once with the same
226 * flags so just check if the first one is really output.
228 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
231 if (copy_from_user(&ghd, ip, sizeof(ghd)))
234 /* Clamp all values to [0,1] */
235 for (i = 0; i < lh->num_descs; i++)
236 __assign_bit(i, vals, ghd.values[i]);
238 /* Reuse the array setting function */
239 return gpiod_set_array_value_complex(false,
245 case GPIOHANDLE_SET_CONFIG_IOCTL:
246 return linehandle_set_config(lh, ip);
253 static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
256 return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
260 static void linehandle_free(struct linehandle_state *lh)
264 for (i = 0; i < lh->num_descs; i++)
266 gpiod_free(lh->descs[i]);
268 put_device(&lh->gdev->dev);
272 static int linehandle_release(struct inode *inode, struct file *file)
274 linehandle_free(file->private_data);
278 static const struct file_operations linehandle_fileops = {
279 .release = linehandle_release,
280 .owner = THIS_MODULE,
281 .llseek = noop_llseek,
282 .unlocked_ioctl = linehandle_ioctl,
284 .compat_ioctl = linehandle_ioctl_compat,
288 static int linehandle_create(struct gpio_device *gdev, void __user *ip)
290 struct gpiohandle_request handlereq;
291 struct linehandle_state *lh;
296 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
298 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
301 lflags = handlereq.flags;
303 ret = linehandle_validate_flags(lflags);
307 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
311 get_device(&gdev->dev);
313 if (handlereq.consumer_label[0] != '\0') {
314 /* label is only initialized if consumer_label is set */
315 lh->label = kstrndup(handlereq.consumer_label,
316 sizeof(handlereq.consumer_label) - 1,
324 lh->num_descs = handlereq.lines;
326 /* Request each GPIO */
327 for (i = 0; i < handlereq.lines; i++) {
328 u32 offset = handlereq.lineoffsets[i];
329 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
336 ret = gpiod_request_user(desc, lh->label);
340 linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
342 ret = gpiod_set_transitory(desc, false);
347 * Lines have to be requested explicitly for input
348 * or output, else the line will be treated "as is".
350 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
351 int val = !!handlereq.default_values[i];
353 ret = gpiod_direction_output(desc, val);
356 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
357 ret = gpiod_direction_input(desc);
362 blocking_notifier_call_chain(&desc->gdev->notifier,
363 GPIO_V2_LINE_CHANGED_REQUESTED, desc);
365 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
369 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
375 file = anon_inode_getfile("gpio-linehandle",
378 O_RDONLY | O_CLOEXEC);
381 goto out_put_unused_fd;
385 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
387 * fput() will trigger the release() callback, so do not go onto
388 * the regular error cleanup path here.
395 fd_install(fd, file);
397 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
408 #endif /* CONFIG_GPIO_CDEV_V1 */
411 * struct line - contains the state of a requested line
412 * @desc: the GPIO descriptor for this line.
413 * @req: the corresponding line request
414 * @irq: the interrupt triggered in response to events on this GPIO
415 * @eflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or
416 * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied
417 * @timestamp_ns: cache for the timestamp storing it between hardirq and
418 * IRQ thread, used to bring the timestamp close to the actual event
419 * @req_seqno: the seqno for the current edge event in the sequence of
420 * events for the corresponding line request. This is drawn from the @req.
421 * @line_seqno: the seqno for the current edge event in the sequence of
422 * events for this line.
423 * @work: the worker that implements software debouncing
424 * @sw_debounced: flag indicating if the software debouncer is active
425 * @level: the current debounced physical level of the line
426 * @hdesc: the Hardware Timestamp Engine (HTE) descriptor
427 * @raw_level: the line level at the time of event
428 * @total_discard_seq: the running counter of the discarded events
429 * @last_seqno: the last sequence number before debounce period expires
432 struct gpio_desc *desc;
434 * -- edge detector specific fields --
439 * The flags for the active edge detector configuration.
441 * edflags is set by linereq_create(), linereq_free(), and
442 * linereq_set_config_unlocked(), which are themselves mutually
443 * exclusive, and is accessed by edge_irq_thread(),
444 * process_hw_ts_thread() and debounce_work_func(),
445 * which can all live with a slightly stale value.
449 * timestamp_ns and req_seqno are accessed only by
450 * edge_irq_handler() and edge_irq_thread(), which are themselves
451 * mutually exclusive, so no additional protection is necessary.
456 * line_seqno is accessed by either edge_irq_thread() or
457 * debounce_work_func(), which are themselves mutually exclusive,
458 * so no additional protection is necessary.
462 * -- debouncer specific fields --
464 struct delayed_work work;
466 * sw_debounce is accessed by linereq_set_config(), which is the
467 * only setter, and linereq_get_values(), which can live with a
468 * slightly stale value.
470 unsigned int sw_debounced;
472 * level is accessed by debounce_work_func(), which is the only
473 * setter, and linereq_get_values() which can live with a slightly
478 struct hte_ts_desc hdesc;
480 * HTE provider sets line level at the time of event. The valid
481 * value is 0 or 1 and negative value for an error.
485 * when sw_debounce is set on HTE enabled line, this is running
486 * counter of the discarded events.
488 u32 total_discard_seq;
490 * when sw_debounce is set on HTE enabled line, this variable records
491 * last sequence number before debounce period expires.
494 #endif /* CONFIG_HTE */
498 * struct linereq - contains the state of a userspace line request
499 * @gdev: the GPIO device the line request pertains to
500 * @label: consumer label used to tag GPIO descriptors
501 * @num_lines: the number of lines in the lines array
502 * @wait: wait queue that handles blocking reads of events
503 * @event_buffer_size: the number of elements allocated in @events
504 * @events: KFIFO for the GPIO events
505 * @seqno: the sequence number for edge events generated on all lines in
506 * this line request. Note that this is not used when @num_lines is 1, as
507 * the line_seqno is then the same and is cheaper to calculate.
508 * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
509 * of configuration, particularly multi-step accesses to desc flags.
510 * @lines: the lines held by this line request, with @num_lines elements.
513 struct gpio_device *gdev;
516 wait_queue_head_t wait;
517 u32 event_buffer_size;
518 DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
520 struct mutex config_mutex;
524 #define GPIO_V2_LINE_BIAS_FLAGS \
525 (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
526 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
527 GPIO_V2_LINE_FLAG_BIAS_DISABLED)
529 #define GPIO_V2_LINE_DIRECTION_FLAGS \
530 (GPIO_V2_LINE_FLAG_INPUT | \
531 GPIO_V2_LINE_FLAG_OUTPUT)
533 #define GPIO_V2_LINE_DRIVE_FLAGS \
534 (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
535 GPIO_V2_LINE_FLAG_OPEN_SOURCE)
537 #define GPIO_V2_LINE_EDGE_FLAGS \
538 (GPIO_V2_LINE_FLAG_EDGE_RISING | \
539 GPIO_V2_LINE_FLAG_EDGE_FALLING)
541 #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS
543 #define GPIO_V2_LINE_VALID_FLAGS \
544 (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
545 GPIO_V2_LINE_DIRECTION_FLAGS | \
546 GPIO_V2_LINE_DRIVE_FLAGS | \
547 GPIO_V2_LINE_EDGE_FLAGS | \
548 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
549 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
550 GPIO_V2_LINE_BIAS_FLAGS)
552 /* subset of flags relevant for edge detector configuration */
553 #define GPIO_V2_LINE_EDGE_DETECTOR_FLAGS \
554 (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
555 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
556 GPIO_V2_LINE_EDGE_FLAGS)
558 static void linereq_put_event(struct linereq *lr,
559 struct gpio_v2_line_event *le)
561 bool overflow = false;
563 spin_lock(&lr->wait.lock);
564 if (kfifo_is_full(&lr->events)) {
566 kfifo_skip(&lr->events);
568 kfifo_in(&lr->events, le, 1);
569 spin_unlock(&lr->wait.lock);
571 wake_up_poll(&lr->wait, EPOLLIN);
573 pr_debug_ratelimited("event FIFO is full - event dropped\n");
576 static u64 line_event_timestamp(struct line *line)
578 if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
579 return ktime_get_real_ns();
580 else if (IS_ENABLED(CONFIG_HTE) &&
581 test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))
582 return line->timestamp_ns;
584 return ktime_get_ns();
587 static u32 line_event_id(int level)
589 return level ? GPIO_V2_LINE_EVENT_RISING_EDGE :
590 GPIO_V2_LINE_EVENT_FALLING_EDGE;
595 static enum hte_return process_hw_ts_thread(void *p)
599 struct gpio_v2_line_event le;
604 return HTE_CB_HANDLED;
609 memset(&le, 0, sizeof(le));
611 le.timestamp_ns = line->timestamp_ns;
612 edflags = READ_ONCE(line->edflags);
614 switch (edflags & GPIO_V2_LINE_EDGE_FLAGS) {
615 case GPIO_V2_LINE_FLAG_EDGE_BOTH:
616 level = (line->raw_level >= 0) ?
618 gpiod_get_raw_value_cansleep(line->desc);
620 if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
623 le.id = line_event_id(level);
625 case GPIO_V2_LINE_FLAG_EDGE_RISING:
626 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
628 case GPIO_V2_LINE_FLAG_EDGE_FALLING:
629 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
632 return HTE_CB_HANDLED;
634 le.line_seqno = line->line_seqno;
635 le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
636 le.offset = gpio_chip_hwgpio(line->desc);
638 linereq_put_event(lr, &le);
640 return HTE_CB_HANDLED;
643 static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p)
650 return HTE_CB_HANDLED;
653 line->timestamp_ns = ts->tsc;
654 line->raw_level = ts->raw_level;
657 if (READ_ONCE(line->sw_debounced)) {
658 line->total_discard_seq++;
659 line->last_seqno = ts->seq;
660 mod_delayed_work(system_wq, &line->work,
661 usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
663 if (unlikely(ts->seq < line->line_seqno))
664 return HTE_CB_HANDLED;
666 diff_seqno = ts->seq - line->line_seqno;
667 line->line_seqno = ts->seq;
668 if (lr->num_lines != 1)
669 line->req_seqno = atomic_add_return(diff_seqno,
672 return HTE_RUN_SECOND_CB;
675 return HTE_CB_HANDLED;
678 static int hte_edge_setup(struct line *line, u64 eflags)
681 unsigned long flags = 0;
682 struct hte_ts_desc *hdesc = &line->hdesc;
684 if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
685 flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
686 HTE_FALLING_EDGE_TS :
688 if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
689 flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
693 line->total_discard_seq = 0;
695 hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags, NULL,
698 ret = hte_ts_get(NULL, hdesc, 0);
702 return hte_request_ts_ns(hdesc, process_hw_ts, process_hw_ts_thread,
708 static int hte_edge_setup(struct line *line, u64 eflags)
712 #endif /* CONFIG_HTE */
714 static irqreturn_t edge_irq_thread(int irq, void *p)
716 struct line *line = p;
717 struct linereq *lr = line->req;
718 struct gpio_v2_line_event le;
720 /* Do not leak kernel stack to userspace */
721 memset(&le, 0, sizeof(le));
723 if (line->timestamp_ns) {
724 le.timestamp_ns = line->timestamp_ns;
727 * We may be running from a nested threaded interrupt in
728 * which case we didn't get the timestamp from
729 * edge_irq_handler().
731 le.timestamp_ns = line_event_timestamp(line);
732 if (lr->num_lines != 1)
733 line->req_seqno = atomic_inc_return(&lr->seqno);
735 line->timestamp_ns = 0;
737 switch (READ_ONCE(line->edflags) & GPIO_V2_LINE_EDGE_FLAGS) {
738 case GPIO_V2_LINE_FLAG_EDGE_BOTH:
739 le.id = line_event_id(gpiod_get_value_cansleep(line->desc));
741 case GPIO_V2_LINE_FLAG_EDGE_RISING:
742 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
744 case GPIO_V2_LINE_FLAG_EDGE_FALLING:
745 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
751 le.line_seqno = line->line_seqno;
752 le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
753 le.offset = gpio_chip_hwgpio(line->desc);
755 linereq_put_event(lr, &le);
760 static irqreturn_t edge_irq_handler(int irq, void *p)
762 struct line *line = p;
763 struct linereq *lr = line->req;
766 * Just store the timestamp in hardirq context so we get it as
767 * close in time as possible to the actual event.
769 line->timestamp_ns = line_event_timestamp(line);
771 if (lr->num_lines != 1)
772 line->req_seqno = atomic_inc_return(&lr->seqno);
774 return IRQ_WAKE_THREAD;
778 * returns the current debounced logical value.
780 static bool debounced_value(struct line *line)
785 * minor race - debouncer may be stopped here, so edge_detector_stop()
786 * must leave the value unchanged so the following will read the level
787 * from when the debouncer was last running.
789 value = READ_ONCE(line->level);
791 if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
797 static irqreturn_t debounce_irq_handler(int irq, void *p)
799 struct line *line = p;
801 mod_delayed_work(system_wq, &line->work,
802 usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
807 static void debounce_work_func(struct work_struct *work)
809 struct gpio_v2_line_event le;
810 struct line *line = container_of(work, struct line, work.work);
812 u64 eflags, edflags = READ_ONCE(line->edflags);
817 if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
818 level = line->raw_level;
821 level = gpiod_get_raw_value_cansleep(line->desc);
823 pr_debug_ratelimited("debouncer failed to read line value\n");
827 if (READ_ONCE(line->level) == level)
830 WRITE_ONCE(line->level, level);
832 /* -- edge detection -- */
833 eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
837 /* switch from physical level to logical - if they differ */
838 if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
841 /* ignore edges that are not being monitored */
842 if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
843 ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
846 /* Do not leak kernel stack to userspace */
847 memset(&le, 0, sizeof(le));
850 le.timestamp_ns = line_event_timestamp(line);
851 le.offset = gpio_chip_hwgpio(line->desc);
853 if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) {
854 /* discard events except the last one */
855 line->total_discard_seq -= 1;
856 diff_seqno = line->last_seqno - line->total_discard_seq -
858 line->line_seqno = line->last_seqno - line->total_discard_seq;
859 le.line_seqno = line->line_seqno;
860 le.seqno = (lr->num_lines == 1) ?
861 le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno);
863 #endif /* CONFIG_HTE */
866 le.line_seqno = line->line_seqno;
867 le.seqno = (lr->num_lines == 1) ?
868 le.line_seqno : atomic_inc_return(&lr->seqno);
871 le.id = line_event_id(level);
873 linereq_put_event(lr, &le);
876 static int debounce_setup(struct line *line, unsigned int debounce_period_us)
878 unsigned long irqflags;
882 ret = gpiod_set_debounce(line->desc, debounce_period_us);
884 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
887 if (ret != -ENOTSUPP)
890 if (debounce_period_us) {
891 /* setup software debounce */
892 level = gpiod_get_raw_value_cansleep(line->desc);
896 if (!(IS_ENABLED(CONFIG_HTE) &&
897 test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))) {
898 irq = gpiod_to_irq(line->desc);
902 irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
903 ret = request_irq(irq, debounce_irq_handler, irqflags,
904 line->req->label, line);
909 ret = hte_edge_setup(line, GPIO_V2_LINE_FLAG_EDGE_BOTH);
914 WRITE_ONCE(line->level, level);
915 WRITE_ONCE(line->sw_debounced, 1);
920 static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
921 unsigned int line_idx)
924 u64 mask = BIT_ULL(line_idx);
926 for (i = 0; i < lc->num_attrs; i++) {
927 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
928 (lc->attrs[i].mask & mask))
934 static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
935 unsigned int line_idx)
938 u64 mask = BIT_ULL(line_idx);
940 for (i = 0; i < lc->num_attrs; i++) {
941 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
942 (lc->attrs[i].mask & mask))
943 return lc->attrs[i].attr.debounce_period_us;
948 static void edge_detector_stop(struct line *line)
951 free_irq(line->irq, line);
956 if (READ_ONCE(line->edflags) & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
957 hte_ts_put(&line->hdesc);
960 cancel_delayed_work_sync(&line->work);
961 WRITE_ONCE(line->sw_debounced, 0);
962 WRITE_ONCE(line->edflags, 0);
964 WRITE_ONCE(line->desc->debounce_period_us, 0);
965 /* do not change line->level - see comment in debounced_value() */
968 static int edge_detector_setup(struct line *line,
969 struct gpio_v2_line_config *lc,
970 unsigned int line_idx, u64 edflags)
972 u32 debounce_period_us;
973 unsigned long irqflags = 0;
977 eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
978 if (eflags && !kfifo_initialized(&line->req->events)) {
979 ret = kfifo_alloc(&line->req->events,
980 line->req->event_buffer_size, GFP_KERNEL);
984 if (gpio_v2_line_config_debounced(lc, line_idx)) {
985 debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
986 ret = debounce_setup(line, debounce_period_us);
989 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
992 /* detection disabled or sw debouncer will provide edge detection */
993 if (!eflags || READ_ONCE(line->sw_debounced))
996 if (IS_ENABLED(CONFIG_HTE) &&
997 (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
998 return hte_edge_setup(line, edflags);
1000 irq = gpiod_to_irq(line->desc);
1004 if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
1005 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
1006 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1007 if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
1008 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
1009 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1010 irqflags |= IRQF_ONESHOT;
1012 /* Request a thread to read the events */
1013 ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
1014 irqflags, line->req->label, line);
1022 static int edge_detector_update(struct line *line,
1023 struct gpio_v2_line_config *lc,
1024 unsigned int line_idx, u64 edflags)
1026 u64 active_edflags = READ_ONCE(line->edflags);
1027 unsigned int debounce_period_us =
1028 gpio_v2_line_config_debounce_period(lc, line_idx);
1030 if ((active_edflags == edflags) &&
1031 (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us))
1034 /* sw debounced and still will be...*/
1035 if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
1036 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
1040 /* reconfiguring edge detection or sw debounce being disabled */
1041 if ((line->irq && !READ_ONCE(line->sw_debounced)) ||
1042 (active_edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) ||
1043 (!debounce_period_us && READ_ONCE(line->sw_debounced)))
1044 edge_detector_stop(line);
1046 return edge_detector_setup(line, lc, line_idx, edflags);
1049 static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
1050 unsigned int line_idx)
1053 u64 mask = BIT_ULL(line_idx);
1055 for (i = 0; i < lc->num_attrs; i++) {
1056 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
1057 (lc->attrs[i].mask & mask))
1058 return lc->attrs[i].attr.flags;
1063 static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
1064 unsigned int line_idx)
1067 u64 mask = BIT_ULL(line_idx);
1069 for (i = 0; i < lc->num_attrs; i++) {
1070 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
1071 (lc->attrs[i].mask & mask))
1072 return !!(lc->attrs[i].attr.values & mask);
1077 static int gpio_v2_line_flags_validate(u64 flags)
1079 /* Return an error if an unknown flag is set */
1080 if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
1083 if (!IS_ENABLED(CONFIG_HTE) &&
1084 (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1088 * Do not allow both INPUT and OUTPUT flags to be set as they are
1091 if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
1092 (flags & GPIO_V2_LINE_FLAG_OUTPUT))
1095 /* Only allow one event clock source */
1096 if (IS_ENABLED(CONFIG_HTE) &&
1097 (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) &&
1098 (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1101 /* Edge detection requires explicit input. */
1102 if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
1103 !(flags & GPIO_V2_LINE_FLAG_INPUT))
1107 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
1108 * request. If the hardware actually supports enabling both at the
1109 * same time the electrical result would be disastrous.
1111 if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
1112 (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
1115 /* Drive requires explicit output direction. */
1116 if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
1117 !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
1120 /* Bias requires explicit direction. */
1121 if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
1122 !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
1125 /* Only one bias flag can be set. */
1126 if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
1127 (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
1128 GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
1129 ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
1130 (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
1136 static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
1137 unsigned int num_lines)
1143 if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
1146 if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
1149 for (i = 0; i < num_lines; i++) {
1150 flags = gpio_v2_line_config_flags(lc, i);
1151 ret = gpio_v2_line_flags_validate(flags);
1155 /* debounce requires explicit input */
1156 if (gpio_v2_line_config_debounced(lc, i) &&
1157 !(flags & GPIO_V2_LINE_FLAG_INPUT))
1163 static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
1164 unsigned long *flagsp)
1166 assign_bit(FLAG_ACTIVE_LOW, flagsp,
1167 flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
1169 if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
1170 set_bit(FLAG_IS_OUT, flagsp);
1171 else if (flags & GPIO_V2_LINE_FLAG_INPUT)
1172 clear_bit(FLAG_IS_OUT, flagsp);
1174 assign_bit(FLAG_EDGE_RISING, flagsp,
1175 flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
1176 assign_bit(FLAG_EDGE_FALLING, flagsp,
1177 flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
1179 assign_bit(FLAG_OPEN_DRAIN, flagsp,
1180 flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
1181 assign_bit(FLAG_OPEN_SOURCE, flagsp,
1182 flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
1184 assign_bit(FLAG_PULL_UP, flagsp,
1185 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
1186 assign_bit(FLAG_PULL_DOWN, flagsp,
1187 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
1188 assign_bit(FLAG_BIAS_DISABLE, flagsp,
1189 flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
1191 assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
1192 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
1193 assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp,
1194 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
1197 static long linereq_get_values(struct linereq *lr, void __user *ip)
1199 struct gpio_v2_line_values lv;
1200 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
1201 struct gpio_desc **descs;
1202 unsigned int i, didx, num_get;
1206 /* NOTE: It's ok to read values of output lines. */
1207 if (copy_from_user(&lv, ip, sizeof(lv)))
1210 for (num_get = 0, i = 0; i < lr->num_lines; i++) {
1211 if (lv.mask & BIT_ULL(i)) {
1213 descs = &lr->lines[i].desc;
1221 descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
1224 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1225 if (lv.mask & BIT_ULL(i)) {
1226 descs[didx] = lr->lines[i].desc;
1231 ret = gpiod_get_array_value_complex(false, true, num_get,
1240 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1241 if (lv.mask & BIT_ULL(i)) {
1242 if (lr->lines[i].sw_debounced)
1243 val = debounced_value(&lr->lines[i]);
1245 val = test_bit(didx, vals);
1247 lv.bits |= BIT_ULL(i);
1252 if (copy_to_user(ip, &lv, sizeof(lv)))
1258 static long linereq_set_values_unlocked(struct linereq *lr,
1259 struct gpio_v2_line_values *lv)
1261 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
1262 struct gpio_desc **descs;
1263 unsigned int i, didx, num_set;
1266 bitmap_zero(vals, GPIO_V2_LINES_MAX);
1267 for (num_set = 0, i = 0; i < lr->num_lines; i++) {
1268 if (lv->mask & BIT_ULL(i)) {
1269 if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
1271 if (lv->bits & BIT_ULL(i))
1272 __set_bit(num_set, vals);
1274 descs = &lr->lines[i].desc;
1281 /* build compacted desc array and values */
1282 descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
1285 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1286 if (lv->mask & BIT_ULL(i)) {
1287 descs[didx] = lr->lines[i].desc;
1292 ret = gpiod_set_array_value_complex(false, true, num_set,
1300 static long linereq_set_values(struct linereq *lr, void __user *ip)
1302 struct gpio_v2_line_values lv;
1305 if (copy_from_user(&lv, ip, sizeof(lv)))
1308 mutex_lock(&lr->config_mutex);
1310 ret = linereq_set_values_unlocked(lr, &lv);
1312 mutex_unlock(&lr->config_mutex);
1317 static long linereq_set_config_unlocked(struct linereq *lr,
1318 struct gpio_v2_line_config *lc)
1320 struct gpio_desc *desc;
1326 for (i = 0; i < lr->num_lines; i++) {
1327 line = &lr->lines[i];
1328 desc = lr->lines[i].desc;
1329 flags = gpio_v2_line_config_flags(lc, i);
1330 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1331 edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
1333 * Lines have to be requested explicitly for input
1334 * or output, else the line will be treated "as is".
1336 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1337 int val = gpio_v2_line_config_output_value(lc, i);
1339 edge_detector_stop(line);
1340 ret = gpiod_direction_output(desc, val);
1343 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1344 ret = gpiod_direction_input(desc);
1348 ret = edge_detector_update(line, lc, i, edflags);
1353 WRITE_ONCE(line->edflags, edflags);
1355 blocking_notifier_call_chain(&desc->gdev->notifier,
1356 GPIO_V2_LINE_CHANGED_CONFIG,
1362 static long linereq_set_config(struct linereq *lr, void __user *ip)
1364 struct gpio_v2_line_config lc;
1367 if (copy_from_user(&lc, ip, sizeof(lc)))
1370 ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
1374 mutex_lock(&lr->config_mutex);
1376 ret = linereq_set_config_unlocked(lr, &lc);
1378 mutex_unlock(&lr->config_mutex);
1383 static long linereq_ioctl(struct file *file, unsigned int cmd,
1386 struct linereq *lr = file->private_data;
1387 void __user *ip = (void __user *)arg;
1390 case GPIO_V2_LINE_GET_VALUES_IOCTL:
1391 return linereq_get_values(lr, ip);
1392 case GPIO_V2_LINE_SET_VALUES_IOCTL:
1393 return linereq_set_values(lr, ip);
1394 case GPIO_V2_LINE_SET_CONFIG_IOCTL:
1395 return linereq_set_config(lr, ip);
1401 #ifdef CONFIG_COMPAT
1402 static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
1405 return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1409 static __poll_t linereq_poll(struct file *file,
1410 struct poll_table_struct *wait)
1412 struct linereq *lr = file->private_data;
1413 __poll_t events = 0;
1415 poll_wait(file, &lr->wait, wait);
1417 if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
1419 events = EPOLLIN | EPOLLRDNORM;
1424 static ssize_t linereq_read(struct file *file,
1429 struct linereq *lr = file->private_data;
1430 struct gpio_v2_line_event le;
1431 ssize_t bytes_read = 0;
1434 if (count < sizeof(le))
1438 spin_lock(&lr->wait.lock);
1439 if (kfifo_is_empty(&lr->events)) {
1441 spin_unlock(&lr->wait.lock);
1445 if (file->f_flags & O_NONBLOCK) {
1446 spin_unlock(&lr->wait.lock);
1450 ret = wait_event_interruptible_locked(lr->wait,
1451 !kfifo_is_empty(&lr->events));
1453 spin_unlock(&lr->wait.lock);
1458 ret = kfifo_out(&lr->events, &le, 1);
1459 spin_unlock(&lr->wait.lock);
1462 * This should never happen - we were holding the
1463 * lock from the moment we learned the fifo is no
1464 * longer empty until now.
1470 if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
1472 bytes_read += sizeof(le);
1473 } while (count >= bytes_read + sizeof(le));
1478 static void linereq_free(struct linereq *lr)
1482 for (i = 0; i < lr->num_lines; i++) {
1483 if (lr->lines[i].desc) {
1484 edge_detector_stop(&lr->lines[i]);
1485 gpiod_free(lr->lines[i].desc);
1488 kfifo_free(&lr->events);
1490 put_device(&lr->gdev->dev);
1494 static int linereq_release(struct inode *inode, struct file *file)
1496 struct linereq *lr = file->private_data;
1502 #ifdef CONFIG_PROC_FS
1503 static void linereq_show_fdinfo(struct seq_file *out, struct file *file)
1505 struct linereq *lr = file->private_data;
1506 struct device *dev = &lr->gdev->dev;
1509 seq_printf(out, "gpio-chip:\t%s\n", dev_name(dev));
1511 for (i = 0; i < lr->num_lines; i++)
1512 seq_printf(out, "gpio-line:\t%d\n",
1513 gpio_chip_hwgpio(lr->lines[i].desc));
1517 static const struct file_operations line_fileops = {
1518 .release = linereq_release,
1519 .read = linereq_read,
1520 .poll = linereq_poll,
1521 .owner = THIS_MODULE,
1522 .llseek = noop_llseek,
1523 .unlocked_ioctl = linereq_ioctl,
1524 #ifdef CONFIG_COMPAT
1525 .compat_ioctl = linereq_ioctl_compat,
1527 #ifdef CONFIG_PROC_FS
1528 .show_fdinfo = linereq_show_fdinfo,
1532 static int linereq_create(struct gpio_device *gdev, void __user *ip)
1534 struct gpio_v2_line_request ulr;
1535 struct gpio_v2_line_config *lc;
1542 if (copy_from_user(&ulr, ip, sizeof(ulr)))
1545 if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
1548 if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
1552 ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
1556 lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
1561 get_device(&gdev->dev);
1563 for (i = 0; i < ulr.num_lines; i++) {
1564 lr->lines[i].req = lr;
1565 WRITE_ONCE(lr->lines[i].sw_debounced, 0);
1566 INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
1569 if (ulr.consumer[0] != '\0') {
1570 /* label is only initialized if consumer is set */
1571 lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1575 goto out_free_linereq;
1579 mutex_init(&lr->config_mutex);
1580 init_waitqueue_head(&lr->wait);
1581 lr->event_buffer_size = ulr.event_buffer_size;
1582 if (lr->event_buffer_size == 0)
1583 lr->event_buffer_size = ulr.num_lines * 16;
1584 else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
1585 lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
1587 atomic_set(&lr->seqno, 0);
1588 lr->num_lines = ulr.num_lines;
1590 /* Request each GPIO */
1591 for (i = 0; i < ulr.num_lines; i++) {
1592 u32 offset = ulr.offsets[i];
1593 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
1596 ret = PTR_ERR(desc);
1597 goto out_free_linereq;
1600 ret = gpiod_request_user(desc, lr->label);
1602 goto out_free_linereq;
1604 lr->lines[i].desc = desc;
1605 flags = gpio_v2_line_config_flags(lc, i);
1606 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1608 ret = gpiod_set_transitory(desc, false);
1610 goto out_free_linereq;
1612 edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
1614 * Lines have to be requested explicitly for input
1615 * or output, else the line will be treated "as is".
1617 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1618 int val = gpio_v2_line_config_output_value(lc, i);
1620 ret = gpiod_direction_output(desc, val);
1622 goto out_free_linereq;
1623 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1624 ret = gpiod_direction_input(desc);
1626 goto out_free_linereq;
1628 ret = edge_detector_setup(&lr->lines[i], lc, i,
1631 goto out_free_linereq;
1634 lr->lines[i].edflags = edflags;
1636 blocking_notifier_call_chain(&desc->gdev->notifier,
1637 GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1639 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
1643 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1646 goto out_free_linereq;
1649 file = anon_inode_getfile("gpio-line", &line_fileops, lr,
1650 O_RDONLY | O_CLOEXEC);
1652 ret = PTR_ERR(file);
1653 goto out_put_unused_fd;
1657 if (copy_to_user(ip, &ulr, sizeof(ulr))) {
1659 * fput() will trigger the release() callback, so do not go onto
1660 * the regular error cleanup path here.
1667 fd_install(fd, file);
1669 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
1681 #ifdef CONFIG_GPIO_CDEV_V1
1684 * GPIO line event management
1688 * struct lineevent_state - contains the state of a userspace event
1689 * @gdev: the GPIO device the event pertains to
1690 * @label: consumer label used to tag descriptors
1691 * @desc: the GPIO descriptor held by this event
1692 * @eflags: the event flags this line was requested with
1693 * @irq: the interrupt that trigger in response to events on this GPIO
1694 * @wait: wait queue that handles blocking reads of events
1695 * @events: KFIFO for the GPIO events
1696 * @timestamp: cache for the timestamp storing it between hardirq
1697 * and IRQ thread, used to bring the timestamp close to the actual
1700 struct lineevent_state {
1701 struct gpio_device *gdev;
1703 struct gpio_desc *desc;
1706 wait_queue_head_t wait;
1707 DECLARE_KFIFO(events, struct gpioevent_data, 16);
1711 #define GPIOEVENT_REQUEST_VALID_FLAGS \
1712 (GPIOEVENT_REQUEST_RISING_EDGE | \
1713 GPIOEVENT_REQUEST_FALLING_EDGE)
1715 static __poll_t lineevent_poll(struct file *file,
1716 struct poll_table_struct *wait)
1718 struct lineevent_state *le = file->private_data;
1719 __poll_t events = 0;
1721 poll_wait(file, &le->wait, wait);
1723 if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1724 events = EPOLLIN | EPOLLRDNORM;
1729 struct compat_gpioeevent_data {
1730 compat_u64 timestamp;
1734 static ssize_t lineevent_read(struct file *file,
1739 struct lineevent_state *le = file->private_data;
1740 struct gpioevent_data ge;
1741 ssize_t bytes_read = 0;
1746 * When compatible system call is being used the struct gpioevent_data,
1747 * in case of at least ia32, has different size due to the alignment
1748 * differences. Because we have first member 64 bits followed by one of
1749 * 32 bits there is no gap between them. The only difference is the
1750 * padding at the end of the data structure. Hence, we calculate the
1751 * actual sizeof() and pass this as an argument to copy_to_user() to
1752 * drop unneeded bytes from the output.
1754 if (compat_need_64bit_alignment_fixup())
1755 ge_size = sizeof(struct compat_gpioeevent_data);
1757 ge_size = sizeof(struct gpioevent_data);
1758 if (count < ge_size)
1762 spin_lock(&le->wait.lock);
1763 if (kfifo_is_empty(&le->events)) {
1765 spin_unlock(&le->wait.lock);
1769 if (file->f_flags & O_NONBLOCK) {
1770 spin_unlock(&le->wait.lock);
1774 ret = wait_event_interruptible_locked(le->wait,
1775 !kfifo_is_empty(&le->events));
1777 spin_unlock(&le->wait.lock);
1782 ret = kfifo_out(&le->events, &ge, 1);
1783 spin_unlock(&le->wait.lock);
1786 * This should never happen - we were holding the lock
1787 * from the moment we learned the fifo is no longer
1794 if (copy_to_user(buf + bytes_read, &ge, ge_size))
1796 bytes_read += ge_size;
1797 } while (count >= bytes_read + ge_size);
1802 static void lineevent_free(struct lineevent_state *le)
1805 free_irq(le->irq, le);
1807 gpiod_free(le->desc);
1809 put_device(&le->gdev->dev);
1813 static int lineevent_release(struct inode *inode, struct file *file)
1815 lineevent_free(file->private_data);
1819 static long lineevent_ioctl(struct file *file, unsigned int cmd,
1822 struct lineevent_state *le = file->private_data;
1823 void __user *ip = (void __user *)arg;
1824 struct gpiohandle_data ghd;
1827 * We can get the value for an event line but not set it,
1828 * because it is input by definition.
1830 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
1833 memset(&ghd, 0, sizeof(ghd));
1835 val = gpiod_get_value_cansleep(le->desc);
1838 ghd.values[0] = val;
1840 if (copy_to_user(ip, &ghd, sizeof(ghd)))
1848 #ifdef CONFIG_COMPAT
1849 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
1852 return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1856 static const struct file_operations lineevent_fileops = {
1857 .release = lineevent_release,
1858 .read = lineevent_read,
1859 .poll = lineevent_poll,
1860 .owner = THIS_MODULE,
1861 .llseek = noop_llseek,
1862 .unlocked_ioctl = lineevent_ioctl,
1863 #ifdef CONFIG_COMPAT
1864 .compat_ioctl = lineevent_ioctl_compat,
1868 static irqreturn_t lineevent_irq_thread(int irq, void *p)
1870 struct lineevent_state *le = p;
1871 struct gpioevent_data ge;
1874 /* Do not leak kernel stack to userspace */
1875 memset(&ge, 0, sizeof(ge));
1878 * We may be running from a nested threaded interrupt in which case
1879 * we didn't get the timestamp from lineevent_irq_handler().
1882 ge.timestamp = ktime_get_ns();
1884 ge.timestamp = le->timestamp;
1886 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
1887 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1888 int level = gpiod_get_value_cansleep(le->desc);
1891 /* Emit low-to-high event */
1892 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1894 /* Emit high-to-low event */
1895 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1896 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
1897 /* Emit low-to-high event */
1898 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1899 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1900 /* Emit high-to-low event */
1901 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1906 ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
1909 wake_up_poll(&le->wait, EPOLLIN);
1911 pr_debug_ratelimited("event FIFO is full - event dropped\n");
1916 static irqreturn_t lineevent_irq_handler(int irq, void *p)
1918 struct lineevent_state *le = p;
1921 * Just store the timestamp in hardirq context so we get it as
1922 * close in time as possible to the actual event.
1924 le->timestamp = ktime_get_ns();
1926 return IRQ_WAKE_THREAD;
1929 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1931 struct gpioevent_request eventreq;
1932 struct lineevent_state *le;
1933 struct gpio_desc *desc;
1940 int irq, irqflags = 0;
1942 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1945 offset = eventreq.lineoffset;
1946 lflags = eventreq.handleflags;
1947 eflags = eventreq.eventflags;
1949 desc = gpiochip_get_desc(gdev->chip, offset);
1951 return PTR_ERR(desc);
1953 /* Return an error if a unknown flag is set */
1954 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1955 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1958 /* This is just wrong: we don't look for events on output lines */
1959 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1960 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1961 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1964 /* Only one bias flag can be set. */
1965 if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1966 (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1967 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1968 ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1969 (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1972 le = kzalloc(sizeof(*le), GFP_KERNEL);
1976 get_device(&gdev->dev);
1978 if (eventreq.consumer_label[0] != '\0') {
1979 /* label is only initialized if consumer_label is set */
1980 le->label = kstrndup(eventreq.consumer_label,
1981 sizeof(eventreq.consumer_label) - 1,
1989 ret = gpiod_request_user(desc, le->label);
1993 le->eflags = eflags;
1995 linehandle_flags_to_desc_flags(lflags, &desc->flags);
1997 ret = gpiod_direction_input(desc);
2001 blocking_notifier_call_chain(&desc->gdev->notifier,
2002 GPIO_V2_LINE_CHANGED_REQUESTED, desc);
2004 irq = gpiod_to_irq(desc);
2010 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
2011 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
2012 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
2013 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
2014 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
2015 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
2016 irqflags |= IRQF_ONESHOT;
2018 INIT_KFIFO(le->events);
2019 init_waitqueue_head(&le->wait);
2021 /* Request a thread to read the events */
2022 ret = request_threaded_irq(irq,
2023 lineevent_irq_handler,
2024 lineevent_irq_thread,
2033 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
2039 file = anon_inode_getfile("gpio-event",
2042 O_RDONLY | O_CLOEXEC);
2044 ret = PTR_ERR(file);
2045 goto out_put_unused_fd;
2049 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
2051 * fput() will trigger the release() callback, so do not go onto
2052 * the regular error cleanup path here.
2059 fd_install(fd, file);
2070 static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
2071 struct gpioline_info *info_v1)
2073 u64 flagsv2 = info_v2->flags;
2075 memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
2076 memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
2077 info_v1->line_offset = info_v2->offset;
2080 if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
2081 info_v1->flags |= GPIOLINE_FLAG_KERNEL;
2083 if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
2084 info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
2086 if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
2087 info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
2089 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
2090 info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
2091 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
2092 info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
2094 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
2095 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
2096 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
2097 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
2098 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
2099 info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
2102 static void gpio_v2_line_info_changed_to_v1(
2103 struct gpio_v2_line_info_changed *lic_v2,
2104 struct gpioline_info_changed *lic_v1)
2106 memset(lic_v1, 0, sizeof(*lic_v1));
2107 gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
2108 lic_v1->timestamp = lic_v2->timestamp_ns;
2109 lic_v1->event_type = lic_v2->event_type;
2112 #endif /* CONFIG_GPIO_CDEV_V1 */
2114 static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
2115 struct gpio_v2_line_info *info)
2117 struct gpio_chip *gc = desc->gdev->chip;
2118 bool ok_for_pinctrl;
2119 unsigned long flags;
2120 u32 debounce_period_us;
2121 unsigned int num_attrs = 0;
2123 memset(info, 0, sizeof(*info));
2124 info->offset = gpio_chip_hwgpio(desc);
2127 * This function takes a mutex so we must check this before taking
2130 * FIXME: find a non-racy way to retrieve this information. Maybe a
2131 * lock common to both frameworks?
2134 pinctrl_gpio_can_use_line(gc->base + info->offset);
2136 spin_lock_irqsave(&gpio_lock, flags);
2139 strscpy(info->name, desc->name, sizeof(info->name));
2142 strscpy(info->consumer, desc->label, sizeof(info->consumer));
2145 * Userspace only need to know that the kernel is using this GPIO so
2149 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
2150 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
2151 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
2152 test_bit(FLAG_EXPORT, &desc->flags) ||
2153 test_bit(FLAG_SYSFS, &desc->flags) ||
2154 !gpiochip_line_is_valid(gc, info->offset) ||
2156 info->flags |= GPIO_V2_LINE_FLAG_USED;
2158 if (test_bit(FLAG_IS_OUT, &desc->flags))
2159 info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
2161 info->flags |= GPIO_V2_LINE_FLAG_INPUT;
2163 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2164 info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
2166 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2167 info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
2168 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2169 info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
2171 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2172 info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
2173 if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2174 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
2175 if (test_bit(FLAG_PULL_UP, &desc->flags))
2176 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
2178 if (test_bit(FLAG_EDGE_RISING, &desc->flags))
2179 info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
2180 if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
2181 info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
2183 if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
2184 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
2185 else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags))
2186 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
2188 debounce_period_us = READ_ONCE(desc->debounce_period_us);
2189 if (debounce_period_us) {
2190 info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
2191 info->attrs[num_attrs].debounce_period_us = debounce_period_us;
2194 info->num_attrs = num_attrs;
2196 spin_unlock_irqrestore(&gpio_lock, flags);
2199 struct gpio_chardev_data {
2200 struct gpio_device *gdev;
2201 wait_queue_head_t wait;
2202 DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
2203 struct notifier_block lineinfo_changed_nb;
2204 unsigned long *watched_lines;
2205 #ifdef CONFIG_GPIO_CDEV_V1
2206 atomic_t watch_abi_version;
2210 static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
2212 struct gpio_device *gdev = cdev->gdev;
2213 struct gpiochip_info chipinfo;
2215 memset(&chipinfo, 0, sizeof(chipinfo));
2217 strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
2218 strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
2219 chipinfo.lines = gdev->ngpio;
2220 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
2225 #ifdef CONFIG_GPIO_CDEV_V1
2227 * returns 0 if the versions match, else the previously selected ABI version
2229 static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
2230 unsigned int version)
2232 int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
2234 if (abiv == version)
2240 static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
2243 struct gpio_desc *desc;
2244 struct gpioline_info lineinfo;
2245 struct gpio_v2_line_info lineinfo_v2;
2247 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2250 /* this doubles as a range check on line_offset */
2251 desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset);
2253 return PTR_ERR(desc);
2256 if (lineinfo_ensure_abi_version(cdev, 1))
2259 if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
2263 gpio_desc_to_lineinfo(desc, &lineinfo_v2);
2264 gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
2266 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2268 clear_bit(lineinfo.line_offset, cdev->watched_lines);
2276 static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
2279 struct gpio_desc *desc;
2280 struct gpio_v2_line_info lineinfo;
2282 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2285 if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2288 desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2290 return PTR_ERR(desc);
2293 #ifdef CONFIG_GPIO_CDEV_V1
2294 if (lineinfo_ensure_abi_version(cdev, 2))
2297 if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2300 gpio_desc_to_lineinfo(desc, &lineinfo);
2302 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2304 clear_bit(lineinfo.offset, cdev->watched_lines);
2311 static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
2315 if (copy_from_user(&offset, ip, sizeof(offset)))
2318 if (offset >= cdev->gdev->ngpio)
2321 if (!test_and_clear_bit(offset, cdev->watched_lines))
2328 * gpio_ioctl() - ioctl handler for the GPIO chardev
2330 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2332 struct gpio_chardev_data *cdev = file->private_data;
2333 struct gpio_device *gdev = cdev->gdev;
2334 void __user *ip = (void __user *)arg;
2336 /* We fail any subsequent ioctl():s when the chip is gone */
2340 /* Fill in the struct and pass to userspace */
2342 case GPIO_GET_CHIPINFO_IOCTL:
2343 return chipinfo_get(cdev, ip);
2344 #ifdef CONFIG_GPIO_CDEV_V1
2345 case GPIO_GET_LINEHANDLE_IOCTL:
2346 return linehandle_create(gdev, ip);
2347 case GPIO_GET_LINEEVENT_IOCTL:
2348 return lineevent_create(gdev, ip);
2349 case GPIO_GET_LINEINFO_IOCTL:
2350 return lineinfo_get_v1(cdev, ip, false);
2351 case GPIO_GET_LINEINFO_WATCH_IOCTL:
2352 return lineinfo_get_v1(cdev, ip, true);
2353 #endif /* CONFIG_GPIO_CDEV_V1 */
2354 case GPIO_V2_GET_LINEINFO_IOCTL:
2355 return lineinfo_get(cdev, ip, false);
2356 case GPIO_V2_GET_LINEINFO_WATCH_IOCTL:
2357 return lineinfo_get(cdev, ip, true);
2358 case GPIO_V2_GET_LINE_IOCTL:
2359 return linereq_create(gdev, ip);
2360 case GPIO_GET_LINEINFO_UNWATCH_IOCTL:
2361 return lineinfo_unwatch(cdev, ip);
2367 #ifdef CONFIG_COMPAT
2368 static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2371 return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2375 static struct gpio_chardev_data *
2376 to_gpio_chardev_data(struct notifier_block *nb)
2378 return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2381 static int lineinfo_changed_notify(struct notifier_block *nb,
2382 unsigned long action, void *data)
2384 struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
2385 struct gpio_v2_line_info_changed chg;
2386 struct gpio_desc *desc = data;
2389 if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2392 memset(&chg, 0, sizeof(chg));
2393 chg.event_type = action;
2394 chg.timestamp_ns = ktime_get_ns();
2395 gpio_desc_to_lineinfo(desc, &chg.info);
2397 ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2399 wake_up_poll(&cdev->wait, EPOLLIN);
2401 pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2406 static __poll_t lineinfo_watch_poll(struct file *file,
2407 struct poll_table_struct *pollt)
2409 struct gpio_chardev_data *cdev = file->private_data;
2410 __poll_t events = 0;
2412 poll_wait(file, &cdev->wait, pollt);
2414 if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2416 events = EPOLLIN | EPOLLRDNORM;
2421 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2422 size_t count, loff_t *off)
2424 struct gpio_chardev_data *cdev = file->private_data;
2425 struct gpio_v2_line_info_changed event;
2426 ssize_t bytes_read = 0;
2430 #ifndef CONFIG_GPIO_CDEV_V1
2431 event_size = sizeof(struct gpio_v2_line_info_changed);
2432 if (count < event_size)
2437 spin_lock(&cdev->wait.lock);
2438 if (kfifo_is_empty(&cdev->events)) {
2440 spin_unlock(&cdev->wait.lock);
2444 if (file->f_flags & O_NONBLOCK) {
2445 spin_unlock(&cdev->wait.lock);
2449 ret = wait_event_interruptible_locked(cdev->wait,
2450 !kfifo_is_empty(&cdev->events));
2452 spin_unlock(&cdev->wait.lock);
2456 #ifdef CONFIG_GPIO_CDEV_V1
2457 /* must be after kfifo check so watch_abi_version is set */
2458 if (atomic_read(&cdev->watch_abi_version) == 2)
2459 event_size = sizeof(struct gpio_v2_line_info_changed);
2461 event_size = sizeof(struct gpioline_info_changed);
2462 if (count < event_size) {
2463 spin_unlock(&cdev->wait.lock);
2467 ret = kfifo_out(&cdev->events, &event, 1);
2468 spin_unlock(&cdev->wait.lock);
2472 /* We should never get here. See lineevent_read(). */
2475 #ifdef CONFIG_GPIO_CDEV_V1
2476 if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2477 if (copy_to_user(buf + bytes_read, &event, event_size))
2480 struct gpioline_info_changed event_v1;
2482 gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2483 if (copy_to_user(buf + bytes_read, &event_v1,
2488 if (copy_to_user(buf + bytes_read, &event, event_size))
2491 bytes_read += event_size;
2492 } while (count >= bytes_read + sizeof(event));
2498 * gpio_chrdev_open() - open the chardev for ioctl operations
2499 * @inode: inode for this chardev
2500 * @file: file struct for storing private data
2501 * Returns 0 on success
2503 static int gpio_chrdev_open(struct inode *inode, struct file *file)
2505 struct gpio_device *gdev = container_of(inode->i_cdev,
2506 struct gpio_device, chrdev);
2507 struct gpio_chardev_data *cdev;
2510 /* Fail on open if the backing gpiochip is gone */
2514 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2518 cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2519 if (!cdev->watched_lines)
2522 init_waitqueue_head(&cdev->wait);
2523 INIT_KFIFO(cdev->events);
2526 cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
2527 ret = blocking_notifier_chain_register(&gdev->notifier,
2528 &cdev->lineinfo_changed_nb);
2530 goto out_free_bitmap;
2532 get_device(&gdev->dev);
2533 file->private_data = cdev;
2535 ret = nonseekable_open(inode, file);
2537 goto out_unregister_notifier;
2541 out_unregister_notifier:
2542 blocking_notifier_chain_unregister(&gdev->notifier,
2543 &cdev->lineinfo_changed_nb);
2545 bitmap_free(cdev->watched_lines);
2552 * gpio_chrdev_release() - close chardev after ioctl operations
2553 * @inode: inode for this chardev
2554 * @file: file struct for storing private data
2555 * Returns 0 on success
2557 static int gpio_chrdev_release(struct inode *inode, struct file *file)
2559 struct gpio_chardev_data *cdev = file->private_data;
2560 struct gpio_device *gdev = cdev->gdev;
2562 bitmap_free(cdev->watched_lines);
2563 blocking_notifier_chain_unregister(&gdev->notifier,
2564 &cdev->lineinfo_changed_nb);
2565 put_device(&gdev->dev);
2571 static const struct file_operations gpio_fileops = {
2572 .release = gpio_chrdev_release,
2573 .open = gpio_chrdev_open,
2574 .poll = lineinfo_watch_poll,
2575 .read = lineinfo_watch_read,
2576 .owner = THIS_MODULE,
2577 .llseek = no_llseek,
2578 .unlocked_ioctl = gpio_ioctl,
2579 #ifdef CONFIG_COMPAT
2580 .compat_ioctl = gpio_ioctl_compat,
2584 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2588 cdev_init(&gdev->chrdev, &gpio_fileops);
2589 gdev->chrdev.owner = THIS_MODULE;
2590 gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2592 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2596 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2597 MAJOR(devt), gdev->id);
2602 void gpiolib_cdev_unregister(struct gpio_device *gdev)
2604 cdev_device_del(&gdev->chrdev, &gdev->dev);