1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/anon_inodes.h>
4 #include <linux/bitmap.h>
5 #include <linux/cdev.h>
6 #include <linux/compat.h>
7 #include <linux/device.h>
9 #include <linux/file.h>
10 #include <linux/gpio.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/interrupt.h>
13 #include <linux/irqreturn.h>
14 #include <linux/kernel.h>
15 #include <linux/kfifo.h>
16 #include <linux/module.h>
17 #include <linux/pinctrl/consumer.h>
18 #include <linux/poll.h>
19 #include <linux/spinlock.h>
20 #include <linux/timekeeping.h>
21 #include <linux/uaccess.h>
22 #include <uapi/linux/gpio.h>
25 #include "gpiolib-cdev.h"
27 /* Character device interface to GPIO.
29 * The GPIO character device, /dev/gpiochipN, provides userspace an
30 * interface to gpiolib GPIOs via ioctl()s.
34 * GPIO line handle management
38 * struct linehandle_state - contains the state of a userspace handle
39 * @gdev: the GPIO device the handle pertains to
40 * @label: consumer label used to tag descriptors
41 * @descs: the GPIO descriptors held by this handle
42 * @num_descs: the number of descriptors held in the descs array
44 struct linehandle_state {
45 struct gpio_device *gdev;
47 struct gpio_desc *descs[GPIOHANDLES_MAX];
51 #define GPIOHANDLE_REQUEST_VALID_FLAGS \
52 (GPIOHANDLE_REQUEST_INPUT | \
53 GPIOHANDLE_REQUEST_OUTPUT | \
54 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
55 GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
56 GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
57 GPIOHANDLE_REQUEST_BIAS_DISABLE | \
58 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
59 GPIOHANDLE_REQUEST_OPEN_SOURCE)
61 static int linehandle_validate_flags(u32 flags)
63 /* Return an error if an unknown flag is set */
64 if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
68 * Do not allow both INPUT & OUTPUT flags to be set as they are
71 if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
72 (flags & GPIOHANDLE_REQUEST_OUTPUT))
76 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
77 * the hardware actually supports enabling both at the same time the
78 * electrical result would be disastrous.
80 if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
81 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
84 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
85 if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
86 ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
87 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
90 /* Bias flags only allowed for input or output mode. */
91 if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
92 (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
93 ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
94 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
95 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
98 /* Only one bias flag can be set. */
99 if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
100 (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
101 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
102 ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
103 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
109 static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
111 assign_bit(FLAG_ACTIVE_LOW, flagsp,
112 lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
113 assign_bit(FLAG_OPEN_DRAIN, flagsp,
114 lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
115 assign_bit(FLAG_OPEN_SOURCE, flagsp,
116 lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
117 assign_bit(FLAG_PULL_UP, flagsp,
118 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
119 assign_bit(FLAG_PULL_DOWN, flagsp,
120 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
121 assign_bit(FLAG_BIAS_DISABLE, flagsp,
122 lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
125 static long linehandle_set_config(struct linehandle_state *lh,
128 struct gpiohandle_config gcnf;
129 struct gpio_desc *desc;
133 if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
137 ret = linehandle_validate_flags(lflags);
141 for (i = 0; i < lh->num_descs; i++) {
143 linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags);
146 * Lines have to be requested explicitly for input
147 * or output, else the line will be treated "as is".
149 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
150 int val = !!gcnf.default_values[i];
152 ret = gpiod_direction_output(desc, val);
155 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
156 ret = gpiod_direction_input(desc);
161 blocking_notifier_call_chain(&desc->gdev->notifier,
162 GPIOLINE_CHANGED_CONFIG, desc);
167 static long linehandle_ioctl(struct file *file, unsigned int cmd,
170 struct linehandle_state *lh = file->private_data;
171 void __user *ip = (void __user *)arg;
172 struct gpiohandle_data ghd;
173 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
176 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
177 /* NOTE: It's ok to read values of output lines. */
178 int ret = gpiod_get_array_value_complex(false,
187 memset(&ghd, 0, sizeof(ghd));
188 for (i = 0; i < lh->num_descs; i++)
189 ghd.values[i] = test_bit(i, vals);
191 if (copy_to_user(ip, &ghd, sizeof(ghd)))
195 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
197 * All line descriptors were created at once with the same
198 * flags so just check if the first one is really output.
200 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
203 if (copy_from_user(&ghd, ip, sizeof(ghd)))
206 /* Clamp all values to [0,1] */
207 for (i = 0; i < lh->num_descs; i++)
208 __assign_bit(i, vals, ghd.values[i]);
210 /* Reuse the array setting function */
211 return gpiod_set_array_value_complex(false,
217 } else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) {
218 return linehandle_set_config(lh, ip);
224 static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
227 return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
231 static void linehandle_free(struct linehandle_state *lh)
235 for (i = 0; i < lh->num_descs; i++)
237 gpiod_free(lh->descs[i]);
239 put_device(&lh->gdev->dev);
243 static int linehandle_release(struct inode *inode, struct file *file)
245 linehandle_free(file->private_data);
249 static const struct file_operations linehandle_fileops = {
250 .release = linehandle_release,
251 .owner = THIS_MODULE,
252 .llseek = noop_llseek,
253 .unlocked_ioctl = linehandle_ioctl,
255 .compat_ioctl = linehandle_ioctl_compat,
259 static int linehandle_create(struct gpio_device *gdev, void __user *ip)
261 struct gpiohandle_request handlereq;
262 struct linehandle_state *lh;
267 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
269 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
272 lflags = handlereq.flags;
274 ret = linehandle_validate_flags(lflags);
278 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
282 get_device(&gdev->dev);
284 /* Make sure this is terminated */
285 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
286 if (strlen(handlereq.consumer_label)) {
287 lh->label = kstrdup(handlereq.consumer_label,
295 lh->num_descs = handlereq.lines;
297 /* Request each GPIO */
298 for (i = 0; i < handlereq.lines; i++) {
299 u32 offset = handlereq.lineoffsets[i];
300 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
307 ret = gpiod_request(desc, lh->label);
311 linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
313 ret = gpiod_set_transitory(desc, false);
318 * Lines have to be requested explicitly for input
319 * or output, else the line will be treated "as is".
321 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
322 int val = !!handlereq.default_values[i];
324 ret = gpiod_direction_output(desc, val);
327 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
328 ret = gpiod_direction_input(desc);
333 blocking_notifier_call_chain(&desc->gdev->notifier,
334 GPIOLINE_CHANGED_REQUESTED, desc);
336 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
340 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
346 file = anon_inode_getfile("gpio-linehandle",
349 O_RDONLY | O_CLOEXEC);
352 goto out_put_unused_fd;
356 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
358 * fput() will trigger the release() callback, so do not go onto
359 * the regular error cleanup path here.
366 fd_install(fd, file);
368 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
381 * GPIO line event management
385 * struct lineevent_state - contains the state of a userspace event
386 * @gdev: the GPIO device the event pertains to
387 * @label: consumer label used to tag descriptors
388 * @desc: the GPIO descriptor held by this event
389 * @eflags: the event flags this line was requested with
390 * @irq: the interrupt that trigger in response to events on this GPIO
391 * @wait: wait queue that handles blocking reads of events
392 * @events: KFIFO for the GPIO events
393 * @timestamp: cache for the timestamp storing it between hardirq
394 * and IRQ thread, used to bring the timestamp close to the actual
397 struct lineevent_state {
398 struct gpio_device *gdev;
400 struct gpio_desc *desc;
403 wait_queue_head_t wait;
404 DECLARE_KFIFO(events, struct gpioevent_data, 16);
408 #define GPIOEVENT_REQUEST_VALID_FLAGS \
409 (GPIOEVENT_REQUEST_RISING_EDGE | \
410 GPIOEVENT_REQUEST_FALLING_EDGE)
412 static __poll_t lineevent_poll(struct file *file,
413 struct poll_table_struct *wait)
415 struct lineevent_state *le = file->private_data;
418 poll_wait(file, &le->wait, wait);
420 if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
421 events = EPOLLIN | EPOLLRDNORM;
427 static ssize_t lineevent_read(struct file *file,
432 struct lineevent_state *le = file->private_data;
433 struct gpioevent_data ge;
434 ssize_t bytes_read = 0;
437 if (count < sizeof(ge))
441 spin_lock(&le->wait.lock);
442 if (kfifo_is_empty(&le->events)) {
444 spin_unlock(&le->wait.lock);
448 if (file->f_flags & O_NONBLOCK) {
449 spin_unlock(&le->wait.lock);
453 ret = wait_event_interruptible_locked(le->wait,
454 !kfifo_is_empty(&le->events));
456 spin_unlock(&le->wait.lock);
461 ret = kfifo_out(&le->events, &ge, 1);
462 spin_unlock(&le->wait.lock);
465 * This should never happen - we were holding the lock
466 * from the moment we learned the fifo is no longer
473 if (copy_to_user(buf + bytes_read, &ge, sizeof(ge)))
475 bytes_read += sizeof(ge);
476 } while (count >= bytes_read + sizeof(ge));
481 static void lineevent_free(struct lineevent_state *le)
484 free_irq(le->irq, le);
486 gpiod_free(le->desc);
488 put_device(&le->gdev->dev);
492 static int lineevent_release(struct inode *inode, struct file *file)
494 lineevent_free(file->private_data);
498 static long lineevent_ioctl(struct file *file, unsigned int cmd,
501 struct lineevent_state *le = file->private_data;
502 void __user *ip = (void __user *)arg;
503 struct gpiohandle_data ghd;
506 * We can get the value for an event line but not set it,
507 * because it is input by definition.
509 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
512 memset(&ghd, 0, sizeof(ghd));
514 val = gpiod_get_value_cansleep(le->desc);
519 if (copy_to_user(ip, &ghd, sizeof(ghd)))
528 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
531 return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
535 static const struct file_operations lineevent_fileops = {
536 .release = lineevent_release,
537 .read = lineevent_read,
538 .poll = lineevent_poll,
539 .owner = THIS_MODULE,
540 .llseek = noop_llseek,
541 .unlocked_ioctl = lineevent_ioctl,
543 .compat_ioctl = lineevent_ioctl_compat,
547 static irqreturn_t lineevent_irq_thread(int irq, void *p)
549 struct lineevent_state *le = p;
550 struct gpioevent_data ge;
553 /* Do not leak kernel stack to userspace */
554 memset(&ge, 0, sizeof(ge));
557 * We may be running from a nested threaded interrupt in which case
558 * we didn't get the timestamp from lineevent_irq_handler().
561 ge.timestamp = ktime_get_ns();
563 ge.timestamp = le->timestamp;
565 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
566 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
567 int level = gpiod_get_value_cansleep(le->desc);
570 /* Emit low-to-high event */
571 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
573 /* Emit high-to-low event */
574 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
575 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
576 /* Emit low-to-high event */
577 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
578 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
579 /* Emit high-to-low event */
580 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
585 ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
588 wake_up_poll(&le->wait, EPOLLIN);
590 pr_debug_ratelimited("event FIFO is full - event dropped\n");
595 static irqreturn_t lineevent_irq_handler(int irq, void *p)
597 struct lineevent_state *le = p;
600 * Just store the timestamp in hardirq context so we get it as
601 * close in time as possible to the actual event.
603 le->timestamp = ktime_get_ns();
605 return IRQ_WAKE_THREAD;
608 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
610 struct gpioevent_request eventreq;
611 struct lineevent_state *le;
612 struct gpio_desc *desc;
619 int irq, irqflags = 0;
621 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
624 offset = eventreq.lineoffset;
625 lflags = eventreq.handleflags;
626 eflags = eventreq.eventflags;
628 desc = gpiochip_get_desc(gdev->chip, offset);
630 return PTR_ERR(desc);
632 /* Return an error if a unknown flag is set */
633 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
634 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
637 /* This is just wrong: we don't look for events on output lines */
638 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
639 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
640 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
643 /* Only one bias flag can be set. */
644 if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
645 (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
646 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
647 ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
648 (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
651 le = kzalloc(sizeof(*le), GFP_KERNEL);
655 get_device(&gdev->dev);
657 /* Make sure this is terminated */
658 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
659 if (strlen(eventreq.consumer_label)) {
660 le->label = kstrdup(eventreq.consumer_label,
668 ret = gpiod_request(desc, le->label);
674 linehandle_flags_to_desc_flags(lflags, &desc->flags);
676 ret = gpiod_direction_input(desc);
680 blocking_notifier_call_chain(&desc->gdev->notifier,
681 GPIOLINE_CHANGED_REQUESTED, desc);
683 irq = gpiod_to_irq(desc);
690 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
691 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
692 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
693 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
694 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
695 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
696 irqflags |= IRQF_ONESHOT;
698 INIT_KFIFO(le->events);
699 init_waitqueue_head(&le->wait);
701 /* Request a thread to read the events */
702 ret = request_threaded_irq(le->irq,
703 lineevent_irq_handler,
704 lineevent_irq_thread,
711 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
717 file = anon_inode_getfile("gpio-event",
720 O_RDONLY | O_CLOEXEC);
723 goto out_put_unused_fd;
727 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
729 * fput() will trigger the release() callback, so do not go onto
730 * the regular error cleanup path here.
737 fd_install(fd, file);
748 static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
749 struct gpioline_info *info)
751 struct gpio_chip *gc = desc->gdev->chip;
756 * This function takes a mutex so we must check this before taking
759 * FIXME: find a non-racy way to retrieve this information. Maybe a
760 * lock common to both frameworks?
763 pinctrl_gpio_can_use_line(gc->base + info->line_offset);
765 spin_lock_irqsave(&gpio_lock, flags);
768 strncpy(info->name, desc->name, sizeof(info->name));
769 info->name[sizeof(info->name) - 1] = '\0';
771 info->name[0] = '\0';
775 strncpy(info->consumer, desc->label, sizeof(info->consumer));
776 info->consumer[sizeof(info->consumer) - 1] = '\0';
778 info->consumer[0] = '\0';
782 * Userspace only need to know that the kernel is using this GPIO so
786 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
787 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
788 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
789 test_bit(FLAG_EXPORT, &desc->flags) ||
790 test_bit(FLAG_SYSFS, &desc->flags) ||
792 info->flags |= GPIOLINE_FLAG_KERNEL;
793 if (test_bit(FLAG_IS_OUT, &desc->flags))
794 info->flags |= GPIOLINE_FLAG_IS_OUT;
795 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
796 info->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
797 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
798 info->flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
799 GPIOLINE_FLAG_IS_OUT);
800 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
801 info->flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
802 GPIOLINE_FLAG_IS_OUT);
803 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
804 info->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
805 if (test_bit(FLAG_PULL_DOWN, &desc->flags))
806 info->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
807 if (test_bit(FLAG_PULL_UP, &desc->flags))
808 info->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
810 spin_unlock_irqrestore(&gpio_lock, flags);
813 struct gpio_chardev_data {
814 struct gpio_device *gdev;
815 wait_queue_head_t wait;
816 DECLARE_KFIFO(events, struct gpioline_info_changed, 32);
817 struct notifier_block lineinfo_changed_nb;
818 unsigned long *watched_lines;
822 * gpio_ioctl() - ioctl handler for the GPIO chardev
824 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
826 struct gpio_chardev_data *cdev = file->private_data;
827 struct gpio_device *gdev = cdev->gdev;
828 struct gpio_chip *gc = gdev->chip;
829 void __user *ip = (void __user *)arg;
830 struct gpio_desc *desc;
833 /* We fail any subsequent ioctl():s when the chip is gone */
837 /* Fill in the struct and pass to userspace */
838 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
839 struct gpiochip_info chipinfo;
841 memset(&chipinfo, 0, sizeof(chipinfo));
843 strncpy(chipinfo.name, dev_name(&gdev->dev),
844 sizeof(chipinfo.name));
845 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
846 strncpy(chipinfo.label, gdev->label,
847 sizeof(chipinfo.label));
848 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
849 chipinfo.lines = gdev->ngpio;
850 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
853 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
854 struct gpioline_info lineinfo;
856 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
859 /* this doubles as a range check on line_offset */
860 desc = gpiochip_get_desc(gc, lineinfo.line_offset);
862 return PTR_ERR(desc);
864 gpio_desc_to_lineinfo(desc, &lineinfo);
866 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
869 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
870 return linehandle_create(gdev, ip);
871 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
872 return lineevent_create(gdev, ip);
873 } else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
874 struct gpioline_info lineinfo;
876 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
879 /* this doubles as a range check on line_offset */
880 desc = gpiochip_get_desc(gc, lineinfo.line_offset);
882 return PTR_ERR(desc);
884 if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
887 gpio_desc_to_lineinfo(desc, &lineinfo);
889 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
890 clear_bit(lineinfo.line_offset, cdev->watched_lines);
895 } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) {
896 if (copy_from_user(&offset, ip, sizeof(offset)))
899 if (offset >= cdev->gdev->ngpio)
902 if (!test_and_clear_bit(offset, cdev->watched_lines))
911 static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
914 return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
918 static struct gpio_chardev_data *
919 to_gpio_chardev_data(struct notifier_block *nb)
921 return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
924 static int lineinfo_changed_notify(struct notifier_block *nb,
925 unsigned long action, void *data)
927 struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
928 struct gpioline_info_changed chg;
929 struct gpio_desc *desc = data;
932 if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
935 memset(&chg, 0, sizeof(chg));
936 chg.info.line_offset = gpio_chip_hwgpio(desc);
937 chg.event_type = action;
938 chg.timestamp = ktime_get_ns();
939 gpio_desc_to_lineinfo(desc, &chg.info);
941 ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
943 wake_up_poll(&cdev->wait, EPOLLIN);
945 pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
950 static __poll_t lineinfo_watch_poll(struct file *file,
951 struct poll_table_struct *pollt)
953 struct gpio_chardev_data *cdev = file->private_data;
956 poll_wait(file, &cdev->wait, pollt);
958 if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
960 events = EPOLLIN | EPOLLRDNORM;
965 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
966 size_t count, loff_t *off)
968 struct gpio_chardev_data *cdev = file->private_data;
969 struct gpioline_info_changed event;
970 ssize_t bytes_read = 0;
973 if (count < sizeof(event))
977 spin_lock(&cdev->wait.lock);
978 if (kfifo_is_empty(&cdev->events)) {
980 spin_unlock(&cdev->wait.lock);
984 if (file->f_flags & O_NONBLOCK) {
985 spin_unlock(&cdev->wait.lock);
989 ret = wait_event_interruptible_locked(cdev->wait,
990 !kfifo_is_empty(&cdev->events));
992 spin_unlock(&cdev->wait.lock);
997 ret = kfifo_out(&cdev->events, &event, 1);
998 spin_unlock(&cdev->wait.lock);
1002 /* We should never get here. See lineevent_read(). */
1005 if (copy_to_user(buf + bytes_read, &event, sizeof(event)))
1007 bytes_read += sizeof(event);
1008 } while (count >= bytes_read + sizeof(event));
1014 * gpio_chrdev_open() - open the chardev for ioctl operations
1015 * @inode: inode for this chardev
1016 * @file: file struct for storing private data
1017 * Returns 0 on success
1019 static int gpio_chrdev_open(struct inode *inode, struct file *file)
1021 struct gpio_device *gdev = container_of(inode->i_cdev,
1022 struct gpio_device, chrdev);
1023 struct gpio_chardev_data *cdev;
1026 /* Fail on open if the backing gpiochip is gone */
1030 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
1034 cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
1035 if (!cdev->watched_lines)
1038 init_waitqueue_head(&cdev->wait);
1039 INIT_KFIFO(cdev->events);
1042 cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
1043 ret = blocking_notifier_chain_register(&gdev->notifier,
1044 &cdev->lineinfo_changed_nb);
1046 goto out_free_bitmap;
1048 get_device(&gdev->dev);
1049 file->private_data = cdev;
1051 ret = nonseekable_open(inode, file);
1053 goto out_unregister_notifier;
1057 out_unregister_notifier:
1058 blocking_notifier_chain_unregister(&gdev->notifier,
1059 &cdev->lineinfo_changed_nb);
1061 bitmap_free(cdev->watched_lines);
1068 * gpio_chrdev_release() - close chardev after ioctl operations
1069 * @inode: inode for this chardev
1070 * @file: file struct for storing private data
1071 * Returns 0 on success
1073 static int gpio_chrdev_release(struct inode *inode, struct file *file)
1075 struct gpio_chardev_data *cdev = file->private_data;
1076 struct gpio_device *gdev = cdev->gdev;
1078 bitmap_free(cdev->watched_lines);
1079 blocking_notifier_chain_unregister(&gdev->notifier,
1080 &cdev->lineinfo_changed_nb);
1081 put_device(&gdev->dev);
1087 static const struct file_operations gpio_fileops = {
1088 .release = gpio_chrdev_release,
1089 .open = gpio_chrdev_open,
1090 .poll = lineinfo_watch_poll,
1091 .read = lineinfo_watch_read,
1092 .owner = THIS_MODULE,
1093 .llseek = no_llseek,
1094 .unlocked_ioctl = gpio_ioctl,
1095 #ifdef CONFIG_COMPAT
1096 .compat_ioctl = gpio_ioctl_compat,
1100 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
1104 cdev_init(&gdev->chrdev, &gpio_fileops);
1105 gdev->chrdev.owner = THIS_MODULE;
1106 gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
1108 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
1112 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1113 MAJOR(devt), gdev->id);
1118 void gpiolib_cdev_unregister(struct gpio_device *gdev)
1120 cdev_device_del(&gdev->chrdev, &gdev->dev);