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;
426 static ssize_t lineevent_get_size(void)
428 #if defined(CONFIG_X86_64) && !defined(CONFIG_UML)
429 /* i386 has no padding after 'id' */
430 if (in_ia32_syscall()) {
431 struct compat_gpioeevent_data {
432 compat_u64 timestamp;
436 return sizeof(struct compat_gpioeevent_data);
439 return sizeof(struct gpioevent_data);
442 static ssize_t lineevent_read(struct file *file,
447 struct lineevent_state *le = file->private_data;
448 struct gpioevent_data ge;
449 ssize_t bytes_read = 0;
454 * When compatible system call is being used the struct gpioevent_data,
455 * in case of at least ia32, has different size due to the alignment
456 * differences. Because we have first member 64 bits followed by one of
457 * 32 bits there is no gap between them. The only difference is the
458 * padding at the end of the data structure. Hence, we calculate the
459 * actual sizeof() and pass this as an argument to copy_to_user() to
460 * drop unneeded bytes from the output.
462 ge_size = lineevent_get_size();
467 spin_lock(&le->wait.lock);
468 if (kfifo_is_empty(&le->events)) {
470 spin_unlock(&le->wait.lock);
474 if (file->f_flags & O_NONBLOCK) {
475 spin_unlock(&le->wait.lock);
479 ret = wait_event_interruptible_locked(le->wait,
480 !kfifo_is_empty(&le->events));
482 spin_unlock(&le->wait.lock);
487 ret = kfifo_out(&le->events, &ge, 1);
488 spin_unlock(&le->wait.lock);
491 * This should never happen - we were holding the lock
492 * from the moment we learned the fifo is no longer
499 if (copy_to_user(buf + bytes_read, &ge, ge_size))
501 bytes_read += ge_size;
502 } while (count >= bytes_read + ge_size);
507 static void lineevent_free(struct lineevent_state *le)
510 free_irq(le->irq, le);
512 gpiod_free(le->desc);
514 put_device(&le->gdev->dev);
518 static int lineevent_release(struct inode *inode, struct file *file)
520 lineevent_free(file->private_data);
524 static long lineevent_ioctl(struct file *file, unsigned int cmd,
527 struct lineevent_state *le = file->private_data;
528 void __user *ip = (void __user *)arg;
529 struct gpiohandle_data ghd;
532 * We can get the value for an event line but not set it,
533 * because it is input by definition.
535 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
538 memset(&ghd, 0, sizeof(ghd));
540 val = gpiod_get_value_cansleep(le->desc);
545 if (copy_to_user(ip, &ghd, sizeof(ghd)))
554 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
557 return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
561 static const struct file_operations lineevent_fileops = {
562 .release = lineevent_release,
563 .read = lineevent_read,
564 .poll = lineevent_poll,
565 .owner = THIS_MODULE,
566 .llseek = noop_llseek,
567 .unlocked_ioctl = lineevent_ioctl,
569 .compat_ioctl = lineevent_ioctl_compat,
573 static irqreturn_t lineevent_irq_thread(int irq, void *p)
575 struct lineevent_state *le = p;
576 struct gpioevent_data ge;
579 /* Do not leak kernel stack to userspace */
580 memset(&ge, 0, sizeof(ge));
583 * We may be running from a nested threaded interrupt in which case
584 * we didn't get the timestamp from lineevent_irq_handler().
587 ge.timestamp = ktime_get_ns();
589 ge.timestamp = le->timestamp;
591 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
592 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
593 int level = gpiod_get_value_cansleep(le->desc);
596 /* Emit low-to-high event */
597 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
599 /* Emit high-to-low event */
600 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
601 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
602 /* Emit low-to-high event */
603 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
604 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
605 /* Emit high-to-low event */
606 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
611 ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
614 wake_up_poll(&le->wait, EPOLLIN);
616 pr_debug_ratelimited("event FIFO is full - event dropped\n");
621 static irqreturn_t lineevent_irq_handler(int irq, void *p)
623 struct lineevent_state *le = p;
626 * Just store the timestamp in hardirq context so we get it as
627 * close in time as possible to the actual event.
629 le->timestamp = ktime_get_ns();
631 return IRQ_WAKE_THREAD;
634 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
636 struct gpioevent_request eventreq;
637 struct lineevent_state *le;
638 struct gpio_desc *desc;
645 int irq, irqflags = 0;
647 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
650 offset = eventreq.lineoffset;
651 lflags = eventreq.handleflags;
652 eflags = eventreq.eventflags;
654 desc = gpiochip_get_desc(gdev->chip, offset);
656 return PTR_ERR(desc);
658 /* Return an error if a unknown flag is set */
659 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
660 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
663 /* This is just wrong: we don't look for events on output lines */
664 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
665 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
666 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
669 /* Only one bias flag can be set. */
670 if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
671 (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
672 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
673 ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
674 (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
677 le = kzalloc(sizeof(*le), GFP_KERNEL);
681 get_device(&gdev->dev);
683 /* Make sure this is terminated */
684 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
685 if (strlen(eventreq.consumer_label)) {
686 le->label = kstrdup(eventreq.consumer_label,
694 ret = gpiod_request(desc, le->label);
700 linehandle_flags_to_desc_flags(lflags, &desc->flags);
702 ret = gpiod_direction_input(desc);
706 blocking_notifier_call_chain(&desc->gdev->notifier,
707 GPIOLINE_CHANGED_REQUESTED, desc);
709 irq = gpiod_to_irq(desc);
716 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
717 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
718 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
719 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
720 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
721 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
722 irqflags |= IRQF_ONESHOT;
724 INIT_KFIFO(le->events);
725 init_waitqueue_head(&le->wait);
727 /* Request a thread to read the events */
728 ret = request_threaded_irq(le->irq,
729 lineevent_irq_handler,
730 lineevent_irq_thread,
737 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
743 file = anon_inode_getfile("gpio-event",
746 O_RDONLY | O_CLOEXEC);
749 goto out_put_unused_fd;
753 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
755 * fput() will trigger the release() callback, so do not go onto
756 * the regular error cleanup path here.
763 fd_install(fd, file);
774 static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
775 struct gpioline_info *info)
777 struct gpio_chip *gc = desc->gdev->chip;
782 * This function takes a mutex so we must check this before taking
785 * FIXME: find a non-racy way to retrieve this information. Maybe a
786 * lock common to both frameworks?
789 pinctrl_gpio_can_use_line(gc->base + info->line_offset);
791 spin_lock_irqsave(&gpio_lock, flags);
794 strncpy(info->name, desc->name, sizeof(info->name));
795 info->name[sizeof(info->name) - 1] = '\0';
797 info->name[0] = '\0';
801 strncpy(info->consumer, desc->label, sizeof(info->consumer));
802 info->consumer[sizeof(info->consumer) - 1] = '\0';
804 info->consumer[0] = '\0';
808 * Userspace only need to know that the kernel is using this GPIO so
812 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
813 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
814 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
815 test_bit(FLAG_EXPORT, &desc->flags) ||
816 test_bit(FLAG_SYSFS, &desc->flags) ||
818 info->flags |= GPIOLINE_FLAG_KERNEL;
819 if (test_bit(FLAG_IS_OUT, &desc->flags))
820 info->flags |= GPIOLINE_FLAG_IS_OUT;
821 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
822 info->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
823 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
824 info->flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
825 GPIOLINE_FLAG_IS_OUT);
826 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
827 info->flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
828 GPIOLINE_FLAG_IS_OUT);
829 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
830 info->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
831 if (test_bit(FLAG_PULL_DOWN, &desc->flags))
832 info->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
833 if (test_bit(FLAG_PULL_UP, &desc->flags))
834 info->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
836 spin_unlock_irqrestore(&gpio_lock, flags);
839 struct gpio_chardev_data {
840 struct gpio_device *gdev;
841 wait_queue_head_t wait;
842 DECLARE_KFIFO(events, struct gpioline_info_changed, 32);
843 struct notifier_block lineinfo_changed_nb;
844 unsigned long *watched_lines;
848 * gpio_ioctl() - ioctl handler for the GPIO chardev
850 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
852 struct gpio_chardev_data *cdev = file->private_data;
853 struct gpio_device *gdev = cdev->gdev;
854 struct gpio_chip *gc = gdev->chip;
855 void __user *ip = (void __user *)arg;
856 struct gpio_desc *desc;
859 /* We fail any subsequent ioctl():s when the chip is gone */
863 /* Fill in the struct and pass to userspace */
864 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
865 struct gpiochip_info chipinfo;
867 memset(&chipinfo, 0, sizeof(chipinfo));
869 strncpy(chipinfo.name, dev_name(&gdev->dev),
870 sizeof(chipinfo.name));
871 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
872 strncpy(chipinfo.label, gdev->label,
873 sizeof(chipinfo.label));
874 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
875 chipinfo.lines = gdev->ngpio;
876 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
879 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
880 struct gpioline_info lineinfo;
882 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
885 /* this doubles as a range check on line_offset */
886 desc = gpiochip_get_desc(gc, lineinfo.line_offset);
888 return PTR_ERR(desc);
890 gpio_desc_to_lineinfo(desc, &lineinfo);
892 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
895 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
896 return linehandle_create(gdev, ip);
897 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
898 return lineevent_create(gdev, ip);
899 } else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
900 struct gpioline_info lineinfo;
902 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
905 /* this doubles as a range check on line_offset */
906 desc = gpiochip_get_desc(gc, lineinfo.line_offset);
908 return PTR_ERR(desc);
910 if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
913 gpio_desc_to_lineinfo(desc, &lineinfo);
915 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
916 clear_bit(lineinfo.line_offset, cdev->watched_lines);
921 } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) {
922 if (copy_from_user(&offset, ip, sizeof(offset)))
925 if (offset >= cdev->gdev->ngpio)
928 if (!test_and_clear_bit(offset, cdev->watched_lines))
937 static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
940 return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
944 static struct gpio_chardev_data *
945 to_gpio_chardev_data(struct notifier_block *nb)
947 return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
950 static int lineinfo_changed_notify(struct notifier_block *nb,
951 unsigned long action, void *data)
953 struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
954 struct gpioline_info_changed chg;
955 struct gpio_desc *desc = data;
958 if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
961 memset(&chg, 0, sizeof(chg));
962 chg.info.line_offset = gpio_chip_hwgpio(desc);
963 chg.event_type = action;
964 chg.timestamp = ktime_get_ns();
965 gpio_desc_to_lineinfo(desc, &chg.info);
967 ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
969 wake_up_poll(&cdev->wait, EPOLLIN);
971 pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
976 static __poll_t lineinfo_watch_poll(struct file *file,
977 struct poll_table_struct *pollt)
979 struct gpio_chardev_data *cdev = file->private_data;
982 poll_wait(file, &cdev->wait, pollt);
984 if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
986 events = EPOLLIN | EPOLLRDNORM;
991 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
992 size_t count, loff_t *off)
994 struct gpio_chardev_data *cdev = file->private_data;
995 struct gpioline_info_changed event;
996 ssize_t bytes_read = 0;
999 if (count < sizeof(event))
1003 spin_lock(&cdev->wait.lock);
1004 if (kfifo_is_empty(&cdev->events)) {
1006 spin_unlock(&cdev->wait.lock);
1010 if (file->f_flags & O_NONBLOCK) {
1011 spin_unlock(&cdev->wait.lock);
1015 ret = wait_event_interruptible_locked(cdev->wait,
1016 !kfifo_is_empty(&cdev->events));
1018 spin_unlock(&cdev->wait.lock);
1023 ret = kfifo_out(&cdev->events, &event, 1);
1024 spin_unlock(&cdev->wait.lock);
1028 /* We should never get here. See lineevent_read(). */
1031 if (copy_to_user(buf + bytes_read, &event, sizeof(event)))
1033 bytes_read += sizeof(event);
1034 } while (count >= bytes_read + sizeof(event));
1040 * gpio_chrdev_open() - open the chardev for ioctl operations
1041 * @inode: inode for this chardev
1042 * @file: file struct for storing private data
1043 * Returns 0 on success
1045 static int gpio_chrdev_open(struct inode *inode, struct file *file)
1047 struct gpio_device *gdev = container_of(inode->i_cdev,
1048 struct gpio_device, chrdev);
1049 struct gpio_chardev_data *cdev;
1052 /* Fail on open if the backing gpiochip is gone */
1056 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
1060 cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
1061 if (!cdev->watched_lines)
1064 init_waitqueue_head(&cdev->wait);
1065 INIT_KFIFO(cdev->events);
1068 cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
1069 ret = blocking_notifier_chain_register(&gdev->notifier,
1070 &cdev->lineinfo_changed_nb);
1072 goto out_free_bitmap;
1074 get_device(&gdev->dev);
1075 file->private_data = cdev;
1077 ret = nonseekable_open(inode, file);
1079 goto out_unregister_notifier;
1083 out_unregister_notifier:
1084 blocking_notifier_chain_unregister(&gdev->notifier,
1085 &cdev->lineinfo_changed_nb);
1087 bitmap_free(cdev->watched_lines);
1094 * gpio_chrdev_release() - close chardev after ioctl operations
1095 * @inode: inode for this chardev
1096 * @file: file struct for storing private data
1097 * Returns 0 on success
1099 static int gpio_chrdev_release(struct inode *inode, struct file *file)
1101 struct gpio_chardev_data *cdev = file->private_data;
1102 struct gpio_device *gdev = cdev->gdev;
1104 bitmap_free(cdev->watched_lines);
1105 blocking_notifier_chain_unregister(&gdev->notifier,
1106 &cdev->lineinfo_changed_nb);
1107 put_device(&gdev->dev);
1113 static const struct file_operations gpio_fileops = {
1114 .release = gpio_chrdev_release,
1115 .open = gpio_chrdev_open,
1116 .poll = lineinfo_watch_poll,
1117 .read = lineinfo_watch_read,
1118 .owner = THIS_MODULE,
1119 .llseek = no_llseek,
1120 .unlocked_ioctl = gpio_ioctl,
1121 #ifdef CONFIG_COMPAT
1122 .compat_ioctl = gpio_ioctl_compat,
1126 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
1130 cdev_init(&gdev->chrdev, &gpio_fileops);
1131 gdev->chrdev.owner = THIS_MODULE;
1132 gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
1134 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
1138 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1139 MAJOR(devt), gdev->id);
1144 void gpiolib_cdev_unregister(struct gpio_device *gdev)
1146 cdev_device_del(&gdev->chrdev, &gdev->dev);