WIP FPC-III support
[linux/fpc-iii.git] / drivers / gpio / gpiolib-cdev.c
blob12b679ca552cccea423e04874e1f2e3c11bef7a6
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/interrupt.h>
16 #include <linux/irqreturn.h>
17 #include <linux/kernel.h>
18 #include <linux/kfifo.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/poll.h>
23 #include <linux/spinlock.h>
24 #include <linux/timekeeping.h>
25 #include <linux/uaccess.h>
26 #include <linux/workqueue.h>
27 #include <uapi/linux/gpio.h>
29 #include "gpiolib.h"
30 #include "gpiolib-cdev.h"
33 * Array sizes must ensure 64-bit alignment and not create holes in the
34 * struct packing.
36 static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2));
37 static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8));
40 * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility
42 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8));
43 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8));
44 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8));
45 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8));
46 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8));
47 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8));
48 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8));
49 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8));
51 /* Character device interface to GPIO.
53 * The GPIO character device, /dev/gpiochipN, provides userspace an
54 * interface to gpiolib GPIOs via ioctl()s.
58 * GPIO line handle management
61 #ifdef CONFIG_GPIO_CDEV_V1
62 /**
63 * struct linehandle_state - contains the state of a userspace handle
64 * @gdev: the GPIO device the handle pertains to
65 * @label: consumer label used to tag descriptors
66 * @descs: the GPIO descriptors held by this handle
67 * @num_descs: the number of descriptors held in the descs array
69 struct linehandle_state {
70 struct gpio_device *gdev;
71 const char *label;
72 struct gpio_desc *descs[GPIOHANDLES_MAX];
73 u32 num_descs;
76 #define GPIOHANDLE_REQUEST_VALID_FLAGS \
77 (GPIOHANDLE_REQUEST_INPUT | \
78 GPIOHANDLE_REQUEST_OUTPUT | \
79 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
80 GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
81 GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
82 GPIOHANDLE_REQUEST_BIAS_DISABLE | \
83 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
84 GPIOHANDLE_REQUEST_OPEN_SOURCE)
86 static int linehandle_validate_flags(u32 flags)
88 /* Return an error if an unknown flag is set */
89 if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
90 return -EINVAL;
93 * Do not allow both INPUT & OUTPUT flags to be set as they are
94 * contradictory.
96 if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
97 (flags & GPIOHANDLE_REQUEST_OUTPUT))
98 return -EINVAL;
101 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
102 * the hardware actually supports enabling both at the same time the
103 * electrical result would be disastrous.
105 if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
106 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
107 return -EINVAL;
109 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
110 if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
111 ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
112 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
113 return -EINVAL;
115 /* Bias flags only allowed for input or output mode. */
116 if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
117 (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
118 ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
119 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
120 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
121 return -EINVAL;
123 /* Only one bias flag can be set. */
124 if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
125 (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
126 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
127 ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
128 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
129 return -EINVAL;
131 return 0;
134 static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
136 assign_bit(FLAG_ACTIVE_LOW, flagsp,
137 lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
138 assign_bit(FLAG_OPEN_DRAIN, flagsp,
139 lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
140 assign_bit(FLAG_OPEN_SOURCE, flagsp,
141 lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
142 assign_bit(FLAG_PULL_UP, flagsp,
143 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
144 assign_bit(FLAG_PULL_DOWN, flagsp,
145 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
146 assign_bit(FLAG_BIAS_DISABLE, flagsp,
147 lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
150 static long linehandle_set_config(struct linehandle_state *lh,
151 void __user *ip)
153 struct gpiohandle_config gcnf;
154 struct gpio_desc *desc;
155 int i, ret;
156 u32 lflags;
158 if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
159 return -EFAULT;
161 lflags = gcnf.flags;
162 ret = linehandle_validate_flags(lflags);
163 if (ret)
164 return ret;
166 for (i = 0; i < lh->num_descs; i++) {
167 desc = lh->descs[i];
168 linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags);
171 * Lines have to be requested explicitly for input
172 * or output, else the line will be treated "as is".
174 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
175 int val = !!gcnf.default_values[i];
177 ret = gpiod_direction_output(desc, val);
178 if (ret)
179 return ret;
180 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
181 ret = gpiod_direction_input(desc);
182 if (ret)
183 return ret;
186 blocking_notifier_call_chain(&desc->gdev->notifier,
187 GPIO_V2_LINE_CHANGED_CONFIG,
188 desc);
190 return 0;
193 static long linehandle_ioctl(struct file *file, unsigned int cmd,
194 unsigned long arg)
196 struct linehandle_state *lh = file->private_data;
197 void __user *ip = (void __user *)arg;
198 struct gpiohandle_data ghd;
199 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
200 int i;
202 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
203 /* NOTE: It's ok to read values of output lines. */
204 int ret = gpiod_get_array_value_complex(false,
205 true,
206 lh->num_descs,
207 lh->descs,
208 NULL,
209 vals);
210 if (ret)
211 return ret;
213 memset(&ghd, 0, sizeof(ghd));
214 for (i = 0; i < lh->num_descs; i++)
215 ghd.values[i] = test_bit(i, vals);
217 if (copy_to_user(ip, &ghd, sizeof(ghd)))
218 return -EFAULT;
220 return 0;
221 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
223 * All line descriptors were created at once with the same
224 * flags so just check if the first one is really output.
226 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
227 return -EPERM;
229 if (copy_from_user(&ghd, ip, sizeof(ghd)))
230 return -EFAULT;
232 /* Clamp all values to [0,1] */
233 for (i = 0; i < lh->num_descs; i++)
234 __assign_bit(i, vals, ghd.values[i]);
236 /* Reuse the array setting function */
237 return gpiod_set_array_value_complex(false,
238 true,
239 lh->num_descs,
240 lh->descs,
241 NULL,
242 vals);
243 } else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) {
244 return linehandle_set_config(lh, ip);
246 return -EINVAL;
249 #ifdef CONFIG_COMPAT
250 static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
251 unsigned long arg)
253 return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
255 #endif
257 static void linehandle_free(struct linehandle_state *lh)
259 int i;
261 for (i = 0; i < lh->num_descs; i++)
262 if (lh->descs[i])
263 gpiod_free(lh->descs[i]);
264 kfree(lh->label);
265 put_device(&lh->gdev->dev);
266 kfree(lh);
269 static int linehandle_release(struct inode *inode, struct file *file)
271 linehandle_free(file->private_data);
272 return 0;
275 static const struct file_operations linehandle_fileops = {
276 .release = linehandle_release,
277 .owner = THIS_MODULE,
278 .llseek = noop_llseek,
279 .unlocked_ioctl = linehandle_ioctl,
280 #ifdef CONFIG_COMPAT
281 .compat_ioctl = linehandle_ioctl_compat,
282 #endif
285 static int linehandle_create(struct gpio_device *gdev, void __user *ip)
287 struct gpiohandle_request handlereq;
288 struct linehandle_state *lh;
289 struct file *file;
290 int fd, i, ret;
291 u32 lflags;
293 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
294 return -EFAULT;
295 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
296 return -EINVAL;
298 lflags = handlereq.flags;
300 ret = linehandle_validate_flags(lflags);
301 if (ret)
302 return ret;
304 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
305 if (!lh)
306 return -ENOMEM;
307 lh->gdev = gdev;
308 get_device(&gdev->dev);
310 if (handlereq.consumer_label[0] != '\0') {
311 /* label is only initialized if consumer_label is set */
312 lh->label = kstrndup(handlereq.consumer_label,
313 sizeof(handlereq.consumer_label) - 1,
314 GFP_KERNEL);
315 if (!lh->label) {
316 ret = -ENOMEM;
317 goto out_free_lh;
321 lh->num_descs = handlereq.lines;
323 /* Request each GPIO */
324 for (i = 0; i < handlereq.lines; i++) {
325 u32 offset = handlereq.lineoffsets[i];
326 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
328 if (IS_ERR(desc)) {
329 ret = PTR_ERR(desc);
330 goto out_free_lh;
333 ret = gpiod_request(desc, lh->label);
334 if (ret)
335 goto out_free_lh;
336 lh->descs[i] = desc;
337 linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
339 ret = gpiod_set_transitory(desc, false);
340 if (ret < 0)
341 goto out_free_lh;
344 * Lines have to be requested explicitly for input
345 * or output, else the line will be treated "as is".
347 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
348 int val = !!handlereq.default_values[i];
350 ret = gpiod_direction_output(desc, val);
351 if (ret)
352 goto out_free_lh;
353 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
354 ret = gpiod_direction_input(desc);
355 if (ret)
356 goto out_free_lh;
359 blocking_notifier_call_chain(&desc->gdev->notifier,
360 GPIO_V2_LINE_CHANGED_REQUESTED, desc);
362 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
363 offset);
366 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
367 if (fd < 0) {
368 ret = fd;
369 goto out_free_lh;
372 file = anon_inode_getfile("gpio-linehandle",
373 &linehandle_fileops,
375 O_RDONLY | O_CLOEXEC);
376 if (IS_ERR(file)) {
377 ret = PTR_ERR(file);
378 goto out_put_unused_fd;
381 handlereq.fd = fd;
382 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
384 * fput() will trigger the release() callback, so do not go onto
385 * the regular error cleanup path here.
387 fput(file);
388 put_unused_fd(fd);
389 return -EFAULT;
392 fd_install(fd, file);
394 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
395 lh->num_descs);
397 return 0;
399 out_put_unused_fd:
400 put_unused_fd(fd);
401 out_free_lh:
402 linehandle_free(lh);
403 return ret;
405 #endif /* CONFIG_GPIO_CDEV_V1 */
408 * struct line - contains the state of a requested line
409 * @desc: the GPIO descriptor for this line.
410 * @req: the corresponding line request
411 * @irq: the interrupt triggered in response to events on this GPIO
412 * @eflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or
413 * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied
414 * @timestamp_ns: cache for the timestamp storing it between hardirq and
415 * IRQ thread, used to bring the timestamp close to the actual event
416 * @req_seqno: the seqno for the current edge event in the sequence of
417 * events for the corresponding line request. This is drawn from the @req.
418 * @line_seqno: the seqno for the current edge event in the sequence of
419 * events for this line.
420 * @work: the worker that implements software debouncing
421 * @sw_debounced: flag indicating if the software debouncer is active
422 * @level: the current debounced physical level of the line
424 struct line {
425 struct gpio_desc *desc;
427 * -- edge detector specific fields --
429 struct linereq *req;
430 unsigned int irq;
432 * eflags is set by edge_detector_setup(), edge_detector_stop() and
433 * edge_detector_update(), which are themselves mutually exclusive,
434 * and is accessed by edge_irq_thread() and debounce_work_func(),
435 * which can both live with a slightly stale value.
437 u64 eflags;
439 * timestamp_ns and req_seqno are accessed only by
440 * edge_irq_handler() and edge_irq_thread(), which are themselves
441 * mutually exclusive, so no additional protection is necessary.
443 u64 timestamp_ns;
444 u32 req_seqno;
446 * line_seqno is accessed by either edge_irq_thread() or
447 * debounce_work_func(), which are themselves mutually exclusive,
448 * so no additional protection is necessary.
450 u32 line_seqno;
452 * -- debouncer specific fields --
454 struct delayed_work work;
456 * sw_debounce is accessed by linereq_set_config(), which is the
457 * only setter, and linereq_get_values(), which can live with a
458 * slightly stale value.
460 unsigned int sw_debounced;
462 * level is accessed by debounce_work_func(), which is the only
463 * setter, and linereq_get_values() which can live with a slightly
464 * stale value.
466 unsigned int level;
470 * struct linereq - contains the state of a userspace line request
471 * @gdev: the GPIO device the line request pertains to
472 * @label: consumer label used to tag GPIO descriptors
473 * @num_lines: the number of lines in the lines array
474 * @wait: wait queue that handles blocking reads of events
475 * @event_buffer_size: the number of elements allocated in @events
476 * @events: KFIFO for the GPIO events
477 * @seqno: the sequence number for edge events generated on all lines in
478 * this line request. Note that this is not used when @num_lines is 1, as
479 * the line_seqno is then the same and is cheaper to calculate.
480 * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
481 * of configuration, particularly multi-step accesses to desc flags.
482 * @lines: the lines held by this line request, with @num_lines elements.
484 struct linereq {
485 struct gpio_device *gdev;
486 const char *label;
487 u32 num_lines;
488 wait_queue_head_t wait;
489 u32 event_buffer_size;
490 DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
491 atomic_t seqno;
492 struct mutex config_mutex;
493 struct line lines[];
496 #define GPIO_V2_LINE_BIAS_FLAGS \
497 (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
498 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
499 GPIO_V2_LINE_FLAG_BIAS_DISABLED)
501 #define GPIO_V2_LINE_DIRECTION_FLAGS \
502 (GPIO_V2_LINE_FLAG_INPUT | \
503 GPIO_V2_LINE_FLAG_OUTPUT)
505 #define GPIO_V2_LINE_DRIVE_FLAGS \
506 (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
507 GPIO_V2_LINE_FLAG_OPEN_SOURCE)
509 #define GPIO_V2_LINE_EDGE_FLAGS \
510 (GPIO_V2_LINE_FLAG_EDGE_RISING | \
511 GPIO_V2_LINE_FLAG_EDGE_FALLING)
513 #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS
515 #define GPIO_V2_LINE_VALID_FLAGS \
516 (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
517 GPIO_V2_LINE_DIRECTION_FLAGS | \
518 GPIO_V2_LINE_DRIVE_FLAGS | \
519 GPIO_V2_LINE_EDGE_FLAGS | \
520 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
521 GPIO_V2_LINE_BIAS_FLAGS)
523 static void linereq_put_event(struct linereq *lr,
524 struct gpio_v2_line_event *le)
526 bool overflow = false;
528 spin_lock(&lr->wait.lock);
529 if (kfifo_is_full(&lr->events)) {
530 overflow = true;
531 kfifo_skip(&lr->events);
533 kfifo_in(&lr->events, le, 1);
534 spin_unlock(&lr->wait.lock);
535 if (!overflow)
536 wake_up_poll(&lr->wait, EPOLLIN);
537 else
538 pr_debug_ratelimited("event FIFO is full - event dropped\n");
541 static u64 line_event_timestamp(struct line *line)
543 if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
544 return ktime_get_real_ns();
546 return ktime_get_ns();
549 static irqreturn_t edge_irq_thread(int irq, void *p)
551 struct line *line = p;
552 struct linereq *lr = line->req;
553 struct gpio_v2_line_event le;
554 u64 eflags;
556 /* Do not leak kernel stack to userspace */
557 memset(&le, 0, sizeof(le));
559 if (line->timestamp_ns) {
560 le.timestamp_ns = line->timestamp_ns;
561 } else {
563 * We may be running from a nested threaded interrupt in
564 * which case we didn't get the timestamp from
565 * edge_irq_handler().
567 le.timestamp_ns = line_event_timestamp(line);
568 if (lr->num_lines != 1)
569 line->req_seqno = atomic_inc_return(&lr->seqno);
571 line->timestamp_ns = 0;
573 eflags = READ_ONCE(line->eflags);
574 if (eflags == GPIO_V2_LINE_FLAG_EDGE_BOTH) {
575 int level = gpiod_get_value_cansleep(line->desc);
577 if (level)
578 /* Emit low-to-high event */
579 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
580 else
581 /* Emit high-to-low event */
582 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
583 } else if (eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) {
584 /* Emit low-to-high event */
585 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
586 } else if (eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) {
587 /* Emit high-to-low event */
588 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
589 } else {
590 return IRQ_NONE;
592 line->line_seqno++;
593 le.line_seqno = line->line_seqno;
594 le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
595 le.offset = gpio_chip_hwgpio(line->desc);
597 linereq_put_event(lr, &le);
599 return IRQ_HANDLED;
602 static irqreturn_t edge_irq_handler(int irq, void *p)
604 struct line *line = p;
605 struct linereq *lr = line->req;
608 * Just store the timestamp in hardirq context so we get it as
609 * close in time as possible to the actual event.
611 line->timestamp_ns = line_event_timestamp(line);
613 if (lr->num_lines != 1)
614 line->req_seqno = atomic_inc_return(&lr->seqno);
616 return IRQ_WAKE_THREAD;
620 * returns the current debounced logical value.
622 static bool debounced_value(struct line *line)
624 bool value;
627 * minor race - debouncer may be stopped here, so edge_detector_stop()
628 * must leave the value unchanged so the following will read the level
629 * from when the debouncer was last running.
631 value = READ_ONCE(line->level);
633 if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
634 value = !value;
636 return value;
639 static irqreturn_t debounce_irq_handler(int irq, void *p)
641 struct line *line = p;
643 mod_delayed_work(system_wq, &line->work,
644 usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
646 return IRQ_HANDLED;
649 static void debounce_work_func(struct work_struct *work)
651 struct gpio_v2_line_event le;
652 struct line *line = container_of(work, struct line, work.work);
653 struct linereq *lr;
654 int level;
655 u64 eflags;
657 level = gpiod_get_raw_value_cansleep(line->desc);
658 if (level < 0) {
659 pr_debug_ratelimited("debouncer failed to read line value\n");
660 return;
663 if (READ_ONCE(line->level) == level)
664 return;
666 WRITE_ONCE(line->level, level);
668 /* -- edge detection -- */
669 eflags = READ_ONCE(line->eflags);
670 if (!eflags)
671 return;
673 /* switch from physical level to logical - if they differ */
674 if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
675 level = !level;
677 /* ignore edges that are not being monitored */
678 if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
679 ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
680 return;
682 /* Do not leak kernel stack to userspace */
683 memset(&le, 0, sizeof(le));
685 lr = line->req;
686 le.timestamp_ns = line_event_timestamp(line);
687 le.offset = gpio_chip_hwgpio(line->desc);
688 line->line_seqno++;
689 le.line_seqno = line->line_seqno;
690 le.seqno = (lr->num_lines == 1) ?
691 le.line_seqno : atomic_inc_return(&lr->seqno);
693 if (level)
694 /* Emit low-to-high event */
695 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
696 else
697 /* Emit high-to-low event */
698 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
700 linereq_put_event(lr, &le);
703 static int debounce_setup(struct line *line,
704 unsigned int debounce_period_us)
706 unsigned long irqflags;
707 int ret, level, irq;
709 /* try hardware */
710 ret = gpiod_set_debounce(line->desc, debounce_period_us);
711 if (!ret) {
712 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
713 return ret;
715 if (ret != -ENOTSUPP)
716 return ret;
718 if (debounce_period_us) {
719 /* setup software debounce */
720 level = gpiod_get_raw_value_cansleep(line->desc);
721 if (level < 0)
722 return level;
724 irq = gpiod_to_irq(line->desc);
725 if (irq < 0)
726 return -ENXIO;
728 WRITE_ONCE(line->level, level);
729 irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
730 ret = request_irq(irq, debounce_irq_handler, irqflags,
731 line->req->label, line);
732 if (ret)
733 return ret;
735 WRITE_ONCE(line->sw_debounced, 1);
736 line->irq = irq;
738 return 0;
741 static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
742 unsigned int line_idx)
744 unsigned int i;
745 u64 mask = BIT_ULL(line_idx);
747 for (i = 0; i < lc->num_attrs; i++) {
748 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
749 (lc->attrs[i].mask & mask))
750 return true;
752 return false;
755 static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
756 unsigned int line_idx)
758 unsigned int i;
759 u64 mask = BIT_ULL(line_idx);
761 for (i = 0; i < lc->num_attrs; i++) {
762 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
763 (lc->attrs[i].mask & mask))
764 return lc->attrs[i].attr.debounce_period_us;
766 return 0;
769 static void edge_detector_stop(struct line *line)
771 if (line->irq) {
772 free_irq(line->irq, line);
773 line->irq = 0;
776 cancel_delayed_work_sync(&line->work);
777 WRITE_ONCE(line->sw_debounced, 0);
778 WRITE_ONCE(line->eflags, 0);
779 /* do not change line->level - see comment in debounced_value() */
782 static int edge_detector_setup(struct line *line,
783 struct gpio_v2_line_config *lc,
784 unsigned int line_idx,
785 u64 eflags)
787 u32 debounce_period_us;
788 unsigned long irqflags = 0;
789 int irq, ret;
791 if (eflags && !kfifo_initialized(&line->req->events)) {
792 ret = kfifo_alloc(&line->req->events,
793 line->req->event_buffer_size, GFP_KERNEL);
794 if (ret)
795 return ret;
797 WRITE_ONCE(line->eflags, eflags);
798 if (gpio_v2_line_config_debounced(lc, line_idx)) {
799 debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
800 ret = debounce_setup(line, debounce_period_us);
801 if (ret)
802 return ret;
803 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
806 /* detection disabled or sw debouncer will provide edge detection */
807 if (!eflags || READ_ONCE(line->sw_debounced))
808 return 0;
810 irq = gpiod_to_irq(line->desc);
811 if (irq < 0)
812 return -ENXIO;
814 if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
815 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
816 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
817 if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
818 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
819 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
820 irqflags |= IRQF_ONESHOT;
822 /* Request a thread to read the events */
823 ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
824 irqflags, line->req->label, line);
825 if (ret)
826 return ret;
828 line->irq = irq;
829 return 0;
832 static int edge_detector_update(struct line *line,
833 struct gpio_v2_line_config *lc,
834 unsigned int line_idx,
835 u64 eflags, bool polarity_change)
837 unsigned int debounce_period_us =
838 gpio_v2_line_config_debounce_period(lc, line_idx);
840 if ((READ_ONCE(line->eflags) == eflags) && !polarity_change &&
841 (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us))
842 return 0;
844 /* sw debounced and still will be...*/
845 if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
846 WRITE_ONCE(line->eflags, eflags);
847 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
848 return 0;
851 /* reconfiguring edge detection or sw debounce being disabled */
852 if ((line->irq && !READ_ONCE(line->sw_debounced)) ||
853 (!debounce_period_us && READ_ONCE(line->sw_debounced)))
854 edge_detector_stop(line);
856 return edge_detector_setup(line, lc, line_idx, eflags);
859 static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
860 unsigned int line_idx)
862 unsigned int i;
863 u64 mask = BIT_ULL(line_idx);
865 for (i = 0; i < lc->num_attrs; i++) {
866 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
867 (lc->attrs[i].mask & mask))
868 return lc->attrs[i].attr.flags;
870 return lc->flags;
873 static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
874 unsigned int line_idx)
876 unsigned int i;
877 u64 mask = BIT_ULL(line_idx);
879 for (i = 0; i < lc->num_attrs; i++) {
880 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
881 (lc->attrs[i].mask & mask))
882 return !!(lc->attrs[i].attr.values & mask);
884 return 0;
887 static int gpio_v2_line_flags_validate(u64 flags)
889 /* Return an error if an unknown flag is set */
890 if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
891 return -EINVAL;
894 * Do not allow both INPUT and OUTPUT flags to be set as they are
895 * contradictory.
897 if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
898 (flags & GPIO_V2_LINE_FLAG_OUTPUT))
899 return -EINVAL;
901 /* Edge detection requires explicit input. */
902 if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
903 !(flags & GPIO_V2_LINE_FLAG_INPUT))
904 return -EINVAL;
907 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
908 * request. If the hardware actually supports enabling both at the
909 * same time the electrical result would be disastrous.
911 if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
912 (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
913 return -EINVAL;
915 /* Drive requires explicit output direction. */
916 if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
917 !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
918 return -EINVAL;
920 /* Bias requires explicit direction. */
921 if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
922 !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
923 return -EINVAL;
925 /* Only one bias flag can be set. */
926 if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
927 (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
928 GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
929 ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
930 (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
931 return -EINVAL;
933 return 0;
936 static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
937 unsigned int num_lines)
939 unsigned int i;
940 u64 flags;
941 int ret;
943 if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
944 return -EINVAL;
946 if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
947 return -EINVAL;
949 for (i = 0; i < num_lines; i++) {
950 flags = gpio_v2_line_config_flags(lc, i);
951 ret = gpio_v2_line_flags_validate(flags);
952 if (ret)
953 return ret;
955 /* debounce requires explicit input */
956 if (gpio_v2_line_config_debounced(lc, i) &&
957 !(flags & GPIO_V2_LINE_FLAG_INPUT))
958 return -EINVAL;
960 return 0;
963 static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
964 unsigned long *flagsp)
966 assign_bit(FLAG_ACTIVE_LOW, flagsp,
967 flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
969 if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
970 set_bit(FLAG_IS_OUT, flagsp);
971 else if (flags & GPIO_V2_LINE_FLAG_INPUT)
972 clear_bit(FLAG_IS_OUT, flagsp);
974 assign_bit(FLAG_EDGE_RISING, flagsp,
975 flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
976 assign_bit(FLAG_EDGE_FALLING, flagsp,
977 flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
979 assign_bit(FLAG_OPEN_DRAIN, flagsp,
980 flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
981 assign_bit(FLAG_OPEN_SOURCE, flagsp,
982 flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
984 assign_bit(FLAG_PULL_UP, flagsp,
985 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
986 assign_bit(FLAG_PULL_DOWN, flagsp,
987 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
988 assign_bit(FLAG_BIAS_DISABLE, flagsp,
989 flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
991 assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
992 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
995 static long linereq_get_values(struct linereq *lr, void __user *ip)
997 struct gpio_v2_line_values lv;
998 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
999 struct gpio_desc **descs;
1000 unsigned int i, didx, num_get;
1001 bool val;
1002 int ret;
1004 /* NOTE: It's ok to read values of output lines. */
1005 if (copy_from_user(&lv, ip, sizeof(lv)))
1006 return -EFAULT;
1008 for (num_get = 0, i = 0; i < lr->num_lines; i++) {
1009 if (lv.mask & BIT_ULL(i)) {
1010 num_get++;
1011 descs = &lr->lines[i].desc;
1015 if (num_get == 0)
1016 return -EINVAL;
1018 if (num_get != 1) {
1019 descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
1020 if (!descs)
1021 return -ENOMEM;
1022 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1023 if (lv.mask & BIT_ULL(i)) {
1024 descs[didx] = lr->lines[i].desc;
1025 didx++;
1029 ret = gpiod_get_array_value_complex(false, true, num_get,
1030 descs, NULL, vals);
1032 if (num_get != 1)
1033 kfree(descs);
1034 if (ret)
1035 return ret;
1037 lv.bits = 0;
1038 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1039 if (lv.mask & BIT_ULL(i)) {
1040 if (lr->lines[i].sw_debounced)
1041 val = debounced_value(&lr->lines[i]);
1042 else
1043 val = test_bit(didx, vals);
1044 if (val)
1045 lv.bits |= BIT_ULL(i);
1046 didx++;
1050 if (copy_to_user(ip, &lv, sizeof(lv)))
1051 return -EFAULT;
1053 return 0;
1056 static long linereq_set_values_unlocked(struct linereq *lr,
1057 struct gpio_v2_line_values *lv)
1059 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
1060 struct gpio_desc **descs;
1061 unsigned int i, didx, num_set;
1062 int ret;
1064 bitmap_zero(vals, GPIO_V2_LINES_MAX);
1065 for (num_set = 0, i = 0; i < lr->num_lines; i++) {
1066 if (lv->mask & BIT_ULL(i)) {
1067 if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
1068 return -EPERM;
1069 if (lv->bits & BIT_ULL(i))
1070 __set_bit(num_set, vals);
1071 num_set++;
1072 descs = &lr->lines[i].desc;
1075 if (num_set == 0)
1076 return -EINVAL;
1078 if (num_set != 1) {
1079 /* build compacted desc array and values */
1080 descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
1081 if (!descs)
1082 return -ENOMEM;
1083 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1084 if (lv->mask & BIT_ULL(i)) {
1085 descs[didx] = lr->lines[i].desc;
1086 didx++;
1090 ret = gpiod_set_array_value_complex(false, true, num_set,
1091 descs, NULL, vals);
1093 if (num_set != 1)
1094 kfree(descs);
1095 return ret;
1098 static long linereq_set_values(struct linereq *lr, void __user *ip)
1100 struct gpio_v2_line_values lv;
1101 int ret;
1103 if (copy_from_user(&lv, ip, sizeof(lv)))
1104 return -EFAULT;
1106 mutex_lock(&lr->config_mutex);
1108 ret = linereq_set_values_unlocked(lr, &lv);
1110 mutex_unlock(&lr->config_mutex);
1112 return ret;
1115 static long linereq_set_config_unlocked(struct linereq *lr,
1116 struct gpio_v2_line_config *lc)
1118 struct gpio_desc *desc;
1119 unsigned int i;
1120 u64 flags;
1121 bool polarity_change;
1122 int ret;
1124 for (i = 0; i < lr->num_lines; i++) {
1125 desc = lr->lines[i].desc;
1126 flags = gpio_v2_line_config_flags(lc, i);
1127 polarity_change =
1128 (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) !=
1129 ((flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) != 0));
1131 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1133 * Lines have to be requested explicitly for input
1134 * or output, else the line will be treated "as is".
1136 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1137 int val = gpio_v2_line_config_output_value(lc, i);
1139 edge_detector_stop(&lr->lines[i]);
1140 ret = gpiod_direction_output(desc, val);
1141 if (ret)
1142 return ret;
1143 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1144 ret = gpiod_direction_input(desc);
1145 if (ret)
1146 return ret;
1148 ret = edge_detector_update(&lr->lines[i], lc, i,
1149 flags & GPIO_V2_LINE_EDGE_FLAGS,
1150 polarity_change);
1151 if (ret)
1152 return ret;
1155 blocking_notifier_call_chain(&desc->gdev->notifier,
1156 GPIO_V2_LINE_CHANGED_CONFIG,
1157 desc);
1159 return 0;
1162 static long linereq_set_config(struct linereq *lr, void __user *ip)
1164 struct gpio_v2_line_config lc;
1165 int ret;
1167 if (copy_from_user(&lc, ip, sizeof(lc)))
1168 return -EFAULT;
1170 ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
1171 if (ret)
1172 return ret;
1174 mutex_lock(&lr->config_mutex);
1176 ret = linereq_set_config_unlocked(lr, &lc);
1178 mutex_unlock(&lr->config_mutex);
1180 return ret;
1183 static long linereq_ioctl(struct file *file, unsigned int cmd,
1184 unsigned long arg)
1186 struct linereq *lr = file->private_data;
1187 void __user *ip = (void __user *)arg;
1189 if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL)
1190 return linereq_get_values(lr, ip);
1191 else if (cmd == GPIO_V2_LINE_SET_VALUES_IOCTL)
1192 return linereq_set_values(lr, ip);
1193 else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL)
1194 return linereq_set_config(lr, ip);
1196 return -EINVAL;
1199 #ifdef CONFIG_COMPAT
1200 static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
1201 unsigned long arg)
1203 return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1205 #endif
1207 static __poll_t linereq_poll(struct file *file,
1208 struct poll_table_struct *wait)
1210 struct linereq *lr = file->private_data;
1211 __poll_t events = 0;
1213 poll_wait(file, &lr->wait, wait);
1215 if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
1216 &lr->wait.lock))
1217 events = EPOLLIN | EPOLLRDNORM;
1219 return events;
1222 static ssize_t linereq_read(struct file *file,
1223 char __user *buf,
1224 size_t count,
1225 loff_t *f_ps)
1227 struct linereq *lr = file->private_data;
1228 struct gpio_v2_line_event le;
1229 ssize_t bytes_read = 0;
1230 int ret;
1232 if (count < sizeof(le))
1233 return -EINVAL;
1235 do {
1236 spin_lock(&lr->wait.lock);
1237 if (kfifo_is_empty(&lr->events)) {
1238 if (bytes_read) {
1239 spin_unlock(&lr->wait.lock);
1240 return bytes_read;
1243 if (file->f_flags & O_NONBLOCK) {
1244 spin_unlock(&lr->wait.lock);
1245 return -EAGAIN;
1248 ret = wait_event_interruptible_locked(lr->wait,
1249 !kfifo_is_empty(&lr->events));
1250 if (ret) {
1251 spin_unlock(&lr->wait.lock);
1252 return ret;
1256 ret = kfifo_out(&lr->events, &le, 1);
1257 spin_unlock(&lr->wait.lock);
1258 if (ret != 1) {
1260 * This should never happen - we were holding the
1261 * lock from the moment we learned the fifo is no
1262 * longer empty until now.
1264 ret = -EIO;
1265 break;
1268 if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
1269 return -EFAULT;
1270 bytes_read += sizeof(le);
1271 } while (count >= bytes_read + sizeof(le));
1273 return bytes_read;
1276 static void linereq_free(struct linereq *lr)
1278 unsigned int i;
1280 for (i = 0; i < lr->num_lines; i++) {
1281 edge_detector_stop(&lr->lines[i]);
1282 if (lr->lines[i].desc)
1283 gpiod_free(lr->lines[i].desc);
1285 kfifo_free(&lr->events);
1286 kfree(lr->label);
1287 put_device(&lr->gdev->dev);
1288 kfree(lr);
1291 static int linereq_release(struct inode *inode, struct file *file)
1293 struct linereq *lr = file->private_data;
1295 linereq_free(lr);
1296 return 0;
1299 static const struct file_operations line_fileops = {
1300 .release = linereq_release,
1301 .read = linereq_read,
1302 .poll = linereq_poll,
1303 .owner = THIS_MODULE,
1304 .llseek = noop_llseek,
1305 .unlocked_ioctl = linereq_ioctl,
1306 #ifdef CONFIG_COMPAT
1307 .compat_ioctl = linereq_ioctl_compat,
1308 #endif
1311 static int linereq_create(struct gpio_device *gdev, void __user *ip)
1313 struct gpio_v2_line_request ulr;
1314 struct gpio_v2_line_config *lc;
1315 struct linereq *lr;
1316 struct file *file;
1317 u64 flags;
1318 unsigned int i;
1319 int fd, ret;
1321 if (copy_from_user(&ulr, ip, sizeof(ulr)))
1322 return -EFAULT;
1324 if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
1325 return -EINVAL;
1327 if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
1328 return -EINVAL;
1330 lc = &ulr.config;
1331 ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
1332 if (ret)
1333 return ret;
1335 lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
1336 if (!lr)
1337 return -ENOMEM;
1339 lr->gdev = gdev;
1340 get_device(&gdev->dev);
1342 for (i = 0; i < ulr.num_lines; i++) {
1343 lr->lines[i].req = lr;
1344 WRITE_ONCE(lr->lines[i].sw_debounced, 0);
1345 INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
1348 if (ulr.consumer[0] != '\0') {
1349 /* label is only initialized if consumer is set */
1350 lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1351 GFP_KERNEL);
1352 if (!lr->label) {
1353 ret = -ENOMEM;
1354 goto out_free_linereq;
1358 mutex_init(&lr->config_mutex);
1359 init_waitqueue_head(&lr->wait);
1360 lr->event_buffer_size = ulr.event_buffer_size;
1361 if (lr->event_buffer_size == 0)
1362 lr->event_buffer_size = ulr.num_lines * 16;
1363 else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
1364 lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
1366 atomic_set(&lr->seqno, 0);
1367 lr->num_lines = ulr.num_lines;
1369 /* Request each GPIO */
1370 for (i = 0; i < ulr.num_lines; i++) {
1371 u32 offset = ulr.offsets[i];
1372 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
1374 if (IS_ERR(desc)) {
1375 ret = PTR_ERR(desc);
1376 goto out_free_linereq;
1379 ret = gpiod_request(desc, lr->label);
1380 if (ret)
1381 goto out_free_linereq;
1383 lr->lines[i].desc = desc;
1384 flags = gpio_v2_line_config_flags(lc, i);
1385 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1387 ret = gpiod_set_transitory(desc, false);
1388 if (ret < 0)
1389 goto out_free_linereq;
1392 * Lines have to be requested explicitly for input
1393 * or output, else the line will be treated "as is".
1395 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1396 int val = gpio_v2_line_config_output_value(lc, i);
1398 ret = gpiod_direction_output(desc, val);
1399 if (ret)
1400 goto out_free_linereq;
1401 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1402 ret = gpiod_direction_input(desc);
1403 if (ret)
1404 goto out_free_linereq;
1406 ret = edge_detector_setup(&lr->lines[i], lc, i,
1407 flags & GPIO_V2_LINE_EDGE_FLAGS);
1408 if (ret)
1409 goto out_free_linereq;
1412 blocking_notifier_call_chain(&desc->gdev->notifier,
1413 GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1415 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
1416 offset);
1419 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1420 if (fd < 0) {
1421 ret = fd;
1422 goto out_free_linereq;
1425 file = anon_inode_getfile("gpio-line", &line_fileops, lr,
1426 O_RDONLY | O_CLOEXEC);
1427 if (IS_ERR(file)) {
1428 ret = PTR_ERR(file);
1429 goto out_put_unused_fd;
1432 ulr.fd = fd;
1433 if (copy_to_user(ip, &ulr, sizeof(ulr))) {
1435 * fput() will trigger the release() callback, so do not go onto
1436 * the regular error cleanup path here.
1438 fput(file);
1439 put_unused_fd(fd);
1440 return -EFAULT;
1443 fd_install(fd, file);
1445 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
1446 lr->num_lines);
1448 return 0;
1450 out_put_unused_fd:
1451 put_unused_fd(fd);
1452 out_free_linereq:
1453 linereq_free(lr);
1454 return ret;
1457 #ifdef CONFIG_GPIO_CDEV_V1
1460 * GPIO line event management
1464 * struct lineevent_state - contains the state of a userspace event
1465 * @gdev: the GPIO device the event pertains to
1466 * @label: consumer label used to tag descriptors
1467 * @desc: the GPIO descriptor held by this event
1468 * @eflags: the event flags this line was requested with
1469 * @irq: the interrupt that trigger in response to events on this GPIO
1470 * @wait: wait queue that handles blocking reads of events
1471 * @events: KFIFO for the GPIO events
1472 * @timestamp: cache for the timestamp storing it between hardirq
1473 * and IRQ thread, used to bring the timestamp close to the actual
1474 * event
1476 struct lineevent_state {
1477 struct gpio_device *gdev;
1478 const char *label;
1479 struct gpio_desc *desc;
1480 u32 eflags;
1481 int irq;
1482 wait_queue_head_t wait;
1483 DECLARE_KFIFO(events, struct gpioevent_data, 16);
1484 u64 timestamp;
1487 #define GPIOEVENT_REQUEST_VALID_FLAGS \
1488 (GPIOEVENT_REQUEST_RISING_EDGE | \
1489 GPIOEVENT_REQUEST_FALLING_EDGE)
1491 static __poll_t lineevent_poll(struct file *file,
1492 struct poll_table_struct *wait)
1494 struct lineevent_state *le = file->private_data;
1495 __poll_t events = 0;
1497 poll_wait(file, &le->wait, wait);
1499 if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1500 events = EPOLLIN | EPOLLRDNORM;
1502 return events;
1505 struct compat_gpioeevent_data {
1506 compat_u64 timestamp;
1507 u32 id;
1510 static ssize_t lineevent_read(struct file *file,
1511 char __user *buf,
1512 size_t count,
1513 loff_t *f_ps)
1515 struct lineevent_state *le = file->private_data;
1516 struct gpioevent_data ge;
1517 ssize_t bytes_read = 0;
1518 ssize_t ge_size;
1519 int ret;
1522 * When compatible system call is being used the struct gpioevent_data,
1523 * in case of at least ia32, has different size due to the alignment
1524 * differences. Because we have first member 64 bits followed by one of
1525 * 32 bits there is no gap between them. The only difference is the
1526 * padding at the end of the data structure. Hence, we calculate the
1527 * actual sizeof() and pass this as an argument to copy_to_user() to
1528 * drop unneeded bytes from the output.
1530 if (compat_need_64bit_alignment_fixup())
1531 ge_size = sizeof(struct compat_gpioeevent_data);
1532 else
1533 ge_size = sizeof(struct gpioevent_data);
1534 if (count < ge_size)
1535 return -EINVAL;
1537 do {
1538 spin_lock(&le->wait.lock);
1539 if (kfifo_is_empty(&le->events)) {
1540 if (bytes_read) {
1541 spin_unlock(&le->wait.lock);
1542 return bytes_read;
1545 if (file->f_flags & O_NONBLOCK) {
1546 spin_unlock(&le->wait.lock);
1547 return -EAGAIN;
1550 ret = wait_event_interruptible_locked(le->wait,
1551 !kfifo_is_empty(&le->events));
1552 if (ret) {
1553 spin_unlock(&le->wait.lock);
1554 return ret;
1558 ret = kfifo_out(&le->events, &ge, 1);
1559 spin_unlock(&le->wait.lock);
1560 if (ret != 1) {
1562 * This should never happen - we were holding the lock
1563 * from the moment we learned the fifo is no longer
1564 * empty until now.
1566 ret = -EIO;
1567 break;
1570 if (copy_to_user(buf + bytes_read, &ge, ge_size))
1571 return -EFAULT;
1572 bytes_read += ge_size;
1573 } while (count >= bytes_read + ge_size);
1575 return bytes_read;
1578 static void lineevent_free(struct lineevent_state *le)
1580 if (le->irq)
1581 free_irq(le->irq, le);
1582 if (le->desc)
1583 gpiod_free(le->desc);
1584 kfree(le->label);
1585 put_device(&le->gdev->dev);
1586 kfree(le);
1589 static int lineevent_release(struct inode *inode, struct file *file)
1591 lineevent_free(file->private_data);
1592 return 0;
1595 static long lineevent_ioctl(struct file *file, unsigned int cmd,
1596 unsigned long arg)
1598 struct lineevent_state *le = file->private_data;
1599 void __user *ip = (void __user *)arg;
1600 struct gpiohandle_data ghd;
1603 * We can get the value for an event line but not set it,
1604 * because it is input by definition.
1606 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
1607 int val;
1609 memset(&ghd, 0, sizeof(ghd));
1611 val = gpiod_get_value_cansleep(le->desc);
1612 if (val < 0)
1613 return val;
1614 ghd.values[0] = val;
1616 if (copy_to_user(ip, &ghd, sizeof(ghd)))
1617 return -EFAULT;
1619 return 0;
1621 return -EINVAL;
1624 #ifdef CONFIG_COMPAT
1625 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
1626 unsigned long arg)
1628 return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1630 #endif
1632 static const struct file_operations lineevent_fileops = {
1633 .release = lineevent_release,
1634 .read = lineevent_read,
1635 .poll = lineevent_poll,
1636 .owner = THIS_MODULE,
1637 .llseek = noop_llseek,
1638 .unlocked_ioctl = lineevent_ioctl,
1639 #ifdef CONFIG_COMPAT
1640 .compat_ioctl = lineevent_ioctl_compat,
1641 #endif
1644 static irqreturn_t lineevent_irq_thread(int irq, void *p)
1646 struct lineevent_state *le = p;
1647 struct gpioevent_data ge;
1648 int ret;
1650 /* Do not leak kernel stack to userspace */
1651 memset(&ge, 0, sizeof(ge));
1654 * We may be running from a nested threaded interrupt in which case
1655 * we didn't get the timestamp from lineevent_irq_handler().
1657 if (!le->timestamp)
1658 ge.timestamp = ktime_get_ns();
1659 else
1660 ge.timestamp = le->timestamp;
1662 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
1663 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1664 int level = gpiod_get_value_cansleep(le->desc);
1666 if (level)
1667 /* Emit low-to-high event */
1668 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1669 else
1670 /* Emit high-to-low event */
1671 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1672 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
1673 /* Emit low-to-high event */
1674 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1675 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1676 /* Emit high-to-low event */
1677 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1678 } else {
1679 return IRQ_NONE;
1682 ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
1683 1, &le->wait.lock);
1684 if (ret)
1685 wake_up_poll(&le->wait, EPOLLIN);
1686 else
1687 pr_debug_ratelimited("event FIFO is full - event dropped\n");
1689 return IRQ_HANDLED;
1692 static irqreturn_t lineevent_irq_handler(int irq, void *p)
1694 struct lineevent_state *le = p;
1697 * Just store the timestamp in hardirq context so we get it as
1698 * close in time as possible to the actual event.
1700 le->timestamp = ktime_get_ns();
1702 return IRQ_WAKE_THREAD;
1705 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1707 struct gpioevent_request eventreq;
1708 struct lineevent_state *le;
1709 struct gpio_desc *desc;
1710 struct file *file;
1711 u32 offset;
1712 u32 lflags;
1713 u32 eflags;
1714 int fd;
1715 int ret;
1716 int irq, irqflags = 0;
1718 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1719 return -EFAULT;
1721 offset = eventreq.lineoffset;
1722 lflags = eventreq.handleflags;
1723 eflags = eventreq.eventflags;
1725 desc = gpiochip_get_desc(gdev->chip, offset);
1726 if (IS_ERR(desc))
1727 return PTR_ERR(desc);
1729 /* Return an error if a unknown flag is set */
1730 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1731 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1732 return -EINVAL;
1734 /* This is just wrong: we don't look for events on output lines */
1735 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1736 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1737 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1738 return -EINVAL;
1740 /* Only one bias flag can be set. */
1741 if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1742 (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1743 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1744 ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1745 (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1746 return -EINVAL;
1748 le = kzalloc(sizeof(*le), GFP_KERNEL);
1749 if (!le)
1750 return -ENOMEM;
1751 le->gdev = gdev;
1752 get_device(&gdev->dev);
1754 if (eventreq.consumer_label[0] != '\0') {
1755 /* label is only initialized if consumer_label is set */
1756 le->label = kstrndup(eventreq.consumer_label,
1757 sizeof(eventreq.consumer_label) - 1,
1758 GFP_KERNEL);
1759 if (!le->label) {
1760 ret = -ENOMEM;
1761 goto out_free_le;
1765 ret = gpiod_request(desc, le->label);
1766 if (ret)
1767 goto out_free_le;
1768 le->desc = desc;
1769 le->eflags = eflags;
1771 linehandle_flags_to_desc_flags(lflags, &desc->flags);
1773 ret = gpiod_direction_input(desc);
1774 if (ret)
1775 goto out_free_le;
1777 blocking_notifier_call_chain(&desc->gdev->notifier,
1778 GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1780 irq = gpiod_to_irq(desc);
1781 if (irq <= 0) {
1782 ret = -ENODEV;
1783 goto out_free_le;
1785 le->irq = irq;
1787 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
1788 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1789 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1790 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
1791 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1792 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1793 irqflags |= IRQF_ONESHOT;
1795 INIT_KFIFO(le->events);
1796 init_waitqueue_head(&le->wait);
1798 /* Request a thread to read the events */
1799 ret = request_threaded_irq(le->irq,
1800 lineevent_irq_handler,
1801 lineevent_irq_thread,
1802 irqflags,
1803 le->label,
1804 le);
1805 if (ret)
1806 goto out_free_le;
1808 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1809 if (fd < 0) {
1810 ret = fd;
1811 goto out_free_le;
1814 file = anon_inode_getfile("gpio-event",
1815 &lineevent_fileops,
1817 O_RDONLY | O_CLOEXEC);
1818 if (IS_ERR(file)) {
1819 ret = PTR_ERR(file);
1820 goto out_put_unused_fd;
1823 eventreq.fd = fd;
1824 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
1826 * fput() will trigger the release() callback, so do not go onto
1827 * the regular error cleanup path here.
1829 fput(file);
1830 put_unused_fd(fd);
1831 return -EFAULT;
1834 fd_install(fd, file);
1836 return 0;
1838 out_put_unused_fd:
1839 put_unused_fd(fd);
1840 out_free_le:
1841 lineevent_free(le);
1842 return ret;
1845 static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
1846 struct gpioline_info *info_v1)
1848 u64 flagsv2 = info_v2->flags;
1850 memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
1851 memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
1852 info_v1->line_offset = info_v2->offset;
1853 info_v1->flags = 0;
1855 if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
1856 info_v1->flags |= GPIOLINE_FLAG_KERNEL;
1858 if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
1859 info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
1861 if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
1862 info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
1864 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
1865 info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
1866 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
1867 info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
1869 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
1870 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
1871 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
1872 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
1873 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
1874 info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
1877 static void gpio_v2_line_info_changed_to_v1(
1878 struct gpio_v2_line_info_changed *lic_v2,
1879 struct gpioline_info_changed *lic_v1)
1881 gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
1882 lic_v1->timestamp = lic_v2->timestamp_ns;
1883 lic_v1->event_type = lic_v2->event_type;
1886 #endif /* CONFIG_GPIO_CDEV_V1 */
1888 static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
1889 struct gpio_v2_line_info *info)
1891 struct gpio_chip *gc = desc->gdev->chip;
1892 bool ok_for_pinctrl;
1893 unsigned long flags;
1894 u32 debounce_period_us;
1895 unsigned int num_attrs = 0;
1897 memset(info, 0, sizeof(*info));
1898 info->offset = gpio_chip_hwgpio(desc);
1901 * This function takes a mutex so we must check this before taking
1902 * the spinlock.
1904 * FIXME: find a non-racy way to retrieve this information. Maybe a
1905 * lock common to both frameworks?
1907 ok_for_pinctrl =
1908 pinctrl_gpio_can_use_line(gc->base + info->offset);
1910 spin_lock_irqsave(&gpio_lock, flags);
1912 if (desc->name)
1913 strscpy(info->name, desc->name, sizeof(info->name));
1915 if (desc->label)
1916 strscpy(info->consumer, desc->label, sizeof(info->consumer));
1919 * Userspace only need to know that the kernel is using this GPIO so
1920 * it can't use it.
1922 info->flags = 0;
1923 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1924 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1925 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1926 test_bit(FLAG_EXPORT, &desc->flags) ||
1927 test_bit(FLAG_SYSFS, &desc->flags) ||
1928 !gpiochip_line_is_valid(gc, info->offset) ||
1929 !ok_for_pinctrl)
1930 info->flags |= GPIO_V2_LINE_FLAG_USED;
1932 if (test_bit(FLAG_IS_OUT, &desc->flags))
1933 info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
1934 else
1935 info->flags |= GPIO_V2_LINE_FLAG_INPUT;
1937 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1938 info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
1940 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1941 info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
1942 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1943 info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
1945 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
1946 info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
1947 if (test_bit(FLAG_PULL_DOWN, &desc->flags))
1948 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
1949 if (test_bit(FLAG_PULL_UP, &desc->flags))
1950 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
1952 if (test_bit(FLAG_EDGE_RISING, &desc->flags))
1953 info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
1954 if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
1955 info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
1957 if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
1958 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
1960 debounce_period_us = READ_ONCE(desc->debounce_period_us);
1961 if (debounce_period_us) {
1962 info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
1963 info->attrs[num_attrs].debounce_period_us = debounce_period_us;
1964 num_attrs++;
1966 info->num_attrs = num_attrs;
1968 spin_unlock_irqrestore(&gpio_lock, flags);
1971 struct gpio_chardev_data {
1972 struct gpio_device *gdev;
1973 wait_queue_head_t wait;
1974 DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
1975 struct notifier_block lineinfo_changed_nb;
1976 unsigned long *watched_lines;
1977 #ifdef CONFIG_GPIO_CDEV_V1
1978 atomic_t watch_abi_version;
1979 #endif
1982 #ifdef CONFIG_GPIO_CDEV_V1
1984 * returns 0 if the versions match, else the previously selected ABI version
1986 static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
1987 unsigned int version)
1989 int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
1991 if (abiv == version)
1992 return 0;
1994 return abiv;
1996 #endif
1998 static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
1999 bool watch)
2001 struct gpio_desc *desc;
2002 struct gpio_v2_line_info lineinfo;
2004 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2005 return -EFAULT;
2007 if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2008 return -EINVAL;
2010 desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2011 if (IS_ERR(desc))
2012 return PTR_ERR(desc);
2014 if (watch) {
2015 #ifdef CONFIG_GPIO_CDEV_V1
2016 if (lineinfo_ensure_abi_version(cdev, 2))
2017 return -EPERM;
2018 #endif
2019 if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2020 return -EBUSY;
2022 gpio_desc_to_lineinfo(desc, &lineinfo);
2024 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2025 if (watch)
2026 clear_bit(lineinfo.offset, cdev->watched_lines);
2027 return -EFAULT;
2030 return 0;
2034 * gpio_ioctl() - ioctl handler for the GPIO chardev
2036 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2038 struct gpio_chardev_data *cdev = file->private_data;
2039 struct gpio_device *gdev = cdev->gdev;
2040 struct gpio_chip *gc = gdev->chip;
2041 void __user *ip = (void __user *)arg;
2042 __u32 offset;
2044 /* We fail any subsequent ioctl():s when the chip is gone */
2045 if (!gc)
2046 return -ENODEV;
2048 /* Fill in the struct and pass to userspace */
2049 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
2050 struct gpiochip_info chipinfo;
2052 memset(&chipinfo, 0, sizeof(chipinfo));
2054 strscpy(chipinfo.name, dev_name(&gdev->dev),
2055 sizeof(chipinfo.name));
2056 strscpy(chipinfo.label, gdev->label,
2057 sizeof(chipinfo.label));
2058 chipinfo.lines = gdev->ngpio;
2059 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
2060 return -EFAULT;
2061 return 0;
2062 #ifdef CONFIG_GPIO_CDEV_V1
2063 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
2064 struct gpio_desc *desc;
2065 struct gpioline_info lineinfo;
2066 struct gpio_v2_line_info lineinfo_v2;
2068 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2069 return -EFAULT;
2071 /* this doubles as a range check on line_offset */
2072 desc = gpiochip_get_desc(gc, lineinfo.line_offset);
2073 if (IS_ERR(desc))
2074 return PTR_ERR(desc);
2076 gpio_desc_to_lineinfo(desc, &lineinfo_v2);
2077 gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
2079 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
2080 return -EFAULT;
2081 return 0;
2082 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
2083 return linehandle_create(gdev, ip);
2084 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
2085 return lineevent_create(gdev, ip);
2086 } else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
2087 struct gpio_desc *desc;
2088 struct gpioline_info lineinfo;
2089 struct gpio_v2_line_info lineinfo_v2;
2091 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2092 return -EFAULT;
2094 /* this doubles as a range check on line_offset */
2095 desc = gpiochip_get_desc(gc, lineinfo.line_offset);
2096 if (IS_ERR(desc))
2097 return PTR_ERR(desc);
2099 if (lineinfo_ensure_abi_version(cdev, 1))
2100 return -EPERM;
2102 if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
2103 return -EBUSY;
2105 gpio_desc_to_lineinfo(desc, &lineinfo_v2);
2106 gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
2108 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2109 clear_bit(lineinfo.line_offset, cdev->watched_lines);
2110 return -EFAULT;
2113 return 0;
2114 #endif /* CONFIG_GPIO_CDEV_V1 */
2115 } else if (cmd == GPIO_V2_GET_LINEINFO_IOCTL ||
2116 cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL) {
2117 return lineinfo_get(cdev, ip,
2118 cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL);
2119 } else if (cmd == GPIO_V2_GET_LINE_IOCTL) {
2120 return linereq_create(gdev, ip);
2121 } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) {
2122 if (copy_from_user(&offset, ip, sizeof(offset)))
2123 return -EFAULT;
2125 if (offset >= cdev->gdev->ngpio)
2126 return -EINVAL;
2128 if (!test_and_clear_bit(offset, cdev->watched_lines))
2129 return -EBUSY;
2131 return 0;
2133 return -EINVAL;
2136 #ifdef CONFIG_COMPAT
2137 static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2138 unsigned long arg)
2140 return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2142 #endif
2144 static struct gpio_chardev_data *
2145 to_gpio_chardev_data(struct notifier_block *nb)
2147 return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2150 static int lineinfo_changed_notify(struct notifier_block *nb,
2151 unsigned long action, void *data)
2153 struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
2154 struct gpio_v2_line_info_changed chg;
2155 struct gpio_desc *desc = data;
2156 int ret;
2158 if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2159 return NOTIFY_DONE;
2161 memset(&chg, 0, sizeof(chg));
2162 chg.event_type = action;
2163 chg.timestamp_ns = ktime_get_ns();
2164 gpio_desc_to_lineinfo(desc, &chg.info);
2166 ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2167 if (ret)
2168 wake_up_poll(&cdev->wait, EPOLLIN);
2169 else
2170 pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2172 return NOTIFY_OK;
2175 static __poll_t lineinfo_watch_poll(struct file *file,
2176 struct poll_table_struct *pollt)
2178 struct gpio_chardev_data *cdev = file->private_data;
2179 __poll_t events = 0;
2181 poll_wait(file, &cdev->wait, pollt);
2183 if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2184 &cdev->wait.lock))
2185 events = EPOLLIN | EPOLLRDNORM;
2187 return events;
2190 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2191 size_t count, loff_t *off)
2193 struct gpio_chardev_data *cdev = file->private_data;
2194 struct gpio_v2_line_info_changed event;
2195 ssize_t bytes_read = 0;
2196 int ret;
2197 size_t event_size;
2199 #ifndef CONFIG_GPIO_CDEV_V1
2200 event_size = sizeof(struct gpio_v2_line_info_changed);
2201 if (count < event_size)
2202 return -EINVAL;
2203 #endif
2205 do {
2206 spin_lock(&cdev->wait.lock);
2207 if (kfifo_is_empty(&cdev->events)) {
2208 if (bytes_read) {
2209 spin_unlock(&cdev->wait.lock);
2210 return bytes_read;
2213 if (file->f_flags & O_NONBLOCK) {
2214 spin_unlock(&cdev->wait.lock);
2215 return -EAGAIN;
2218 ret = wait_event_interruptible_locked(cdev->wait,
2219 !kfifo_is_empty(&cdev->events));
2220 if (ret) {
2221 spin_unlock(&cdev->wait.lock);
2222 return ret;
2225 #ifdef CONFIG_GPIO_CDEV_V1
2226 /* must be after kfifo check so watch_abi_version is set */
2227 if (atomic_read(&cdev->watch_abi_version) == 2)
2228 event_size = sizeof(struct gpio_v2_line_info_changed);
2229 else
2230 event_size = sizeof(struct gpioline_info_changed);
2231 if (count < event_size) {
2232 spin_unlock(&cdev->wait.lock);
2233 return -EINVAL;
2235 #endif
2236 ret = kfifo_out(&cdev->events, &event, 1);
2237 spin_unlock(&cdev->wait.lock);
2238 if (ret != 1) {
2239 ret = -EIO;
2240 break;
2241 /* We should never get here. See lineevent_read(). */
2244 #ifdef CONFIG_GPIO_CDEV_V1
2245 if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2246 if (copy_to_user(buf + bytes_read, &event, event_size))
2247 return -EFAULT;
2248 } else {
2249 struct gpioline_info_changed event_v1;
2251 gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2252 if (copy_to_user(buf + bytes_read, &event_v1,
2253 event_size))
2254 return -EFAULT;
2256 #else
2257 if (copy_to_user(buf + bytes_read, &event, event_size))
2258 return -EFAULT;
2259 #endif
2260 bytes_read += event_size;
2261 } while (count >= bytes_read + sizeof(event));
2263 return bytes_read;
2267 * gpio_chrdev_open() - open the chardev for ioctl operations
2268 * @inode: inode for this chardev
2269 * @file: file struct for storing private data
2270 * Returns 0 on success
2272 static int gpio_chrdev_open(struct inode *inode, struct file *file)
2274 struct gpio_device *gdev = container_of(inode->i_cdev,
2275 struct gpio_device, chrdev);
2276 struct gpio_chardev_data *cdev;
2277 int ret = -ENOMEM;
2279 /* Fail on open if the backing gpiochip is gone */
2280 if (!gdev->chip)
2281 return -ENODEV;
2283 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2284 if (!cdev)
2285 return -ENOMEM;
2287 cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2288 if (!cdev->watched_lines)
2289 goto out_free_cdev;
2291 init_waitqueue_head(&cdev->wait);
2292 INIT_KFIFO(cdev->events);
2293 cdev->gdev = gdev;
2295 cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
2296 ret = blocking_notifier_chain_register(&gdev->notifier,
2297 &cdev->lineinfo_changed_nb);
2298 if (ret)
2299 goto out_free_bitmap;
2301 get_device(&gdev->dev);
2302 file->private_data = cdev;
2304 ret = nonseekable_open(inode, file);
2305 if (ret)
2306 goto out_unregister_notifier;
2308 return ret;
2310 out_unregister_notifier:
2311 blocking_notifier_chain_unregister(&gdev->notifier,
2312 &cdev->lineinfo_changed_nb);
2313 out_free_bitmap:
2314 bitmap_free(cdev->watched_lines);
2315 out_free_cdev:
2316 kfree(cdev);
2317 return ret;
2321 * gpio_chrdev_release() - close chardev after ioctl operations
2322 * @inode: inode for this chardev
2323 * @file: file struct for storing private data
2324 * Returns 0 on success
2326 static int gpio_chrdev_release(struct inode *inode, struct file *file)
2328 struct gpio_chardev_data *cdev = file->private_data;
2329 struct gpio_device *gdev = cdev->gdev;
2331 bitmap_free(cdev->watched_lines);
2332 blocking_notifier_chain_unregister(&gdev->notifier,
2333 &cdev->lineinfo_changed_nb);
2334 put_device(&gdev->dev);
2335 kfree(cdev);
2337 return 0;
2340 static const struct file_operations gpio_fileops = {
2341 .release = gpio_chrdev_release,
2342 .open = gpio_chrdev_open,
2343 .poll = lineinfo_watch_poll,
2344 .read = lineinfo_watch_read,
2345 .owner = THIS_MODULE,
2346 .llseek = no_llseek,
2347 .unlocked_ioctl = gpio_ioctl,
2348 #ifdef CONFIG_COMPAT
2349 .compat_ioctl = gpio_ioctl_compat,
2350 #endif
2353 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2355 int ret;
2357 cdev_init(&gdev->chrdev, &gpio_fileops);
2358 gdev->chrdev.owner = THIS_MODULE;
2359 gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2361 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2362 if (ret)
2363 return ret;
2365 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2366 MAJOR(devt), gdev->id);
2368 return 0;
2371 void gpiolib_cdev_unregister(struct gpio_device *gdev)
2373 cdev_device_del(&gdev->chrdev, &gdev->dev);