xref: /openbmc/linux/drivers/gpio/gpiolib-cdev.c (revision a0db197f534fb24d64cc8c716b5f128f2de1c898)
1 // SPDX-License-Identifier: GPL-2.0
2 
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>
28 
29 #include "gpiolib.h"
30 #include "gpiolib-cdev.h"
31 
32 /*
33  * Array sizes must ensure 64-bit alignment and not create holes in the
34  * struct packing.
35  */
36 static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2));
37 static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8));
38 
39 /*
40  * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility
41  */
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));
50 
51 /* Character device interface to GPIO.
52  *
53  * The GPIO character device, /dev/gpiochipN, provides userspace an
54  * interface to gpiolib GPIOs via ioctl()s.
55  */
56 
57 /*
58  * GPIO line handle management
59  */
60 
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
68  */
69 struct linehandle_state {
70 	struct gpio_device *gdev;
71 	const char *label;
72 	struct gpio_desc *descs[GPIOHANDLES_MAX];
73 	u32 num_descs;
74 };
75 
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)
85 
86 static int linehandle_validate_flags(u32 flags)
87 {
88 	/* Return an error if an unknown flag is set */
89 	if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
90 		return -EINVAL;
91 
92 	/*
93 	 * Do not allow both INPUT & OUTPUT flags to be set as they are
94 	 * contradictory.
95 	 */
96 	if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
97 	    (flags & GPIOHANDLE_REQUEST_OUTPUT))
98 		return -EINVAL;
99 
100 	/*
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.
104 	 */
105 	if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
106 	    (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
107 		return -EINVAL;
108 
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;
114 
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;
122 
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;
130 
131 	return 0;
132 }
133 
134 static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
135 {
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);
148 }
149 
150 static long linehandle_set_config(struct linehandle_state *lh,
151 				  void __user *ip)
152 {
153 	struct gpiohandle_config gcnf;
154 	struct gpio_desc *desc;
155 	int i, ret;
156 	u32 lflags;
157 
158 	if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
159 		return -EFAULT;
160 
161 	lflags = gcnf.flags;
162 	ret = linehandle_validate_flags(lflags);
163 	if (ret)
164 		return ret;
165 
166 	for (i = 0; i < lh->num_descs; i++) {
167 		desc = lh->descs[i];
168 		linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags);
169 
170 		/*
171 		 * Lines have to be requested explicitly for input
172 		 * or output, else the line will be treated "as is".
173 		 */
174 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
175 			int val = !!gcnf.default_values[i];
176 
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;
184 		}
185 
186 		blocking_notifier_call_chain(&desc->gdev->notifier,
187 					     GPIO_V2_LINE_CHANGED_CONFIG,
188 					     desc);
189 	}
190 	return 0;
191 }
192 
193 static long linehandle_ioctl(struct file *file, unsigned int cmd,
194 			     unsigned long arg)
195 {
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;
201 
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;
212 
213 		memset(&ghd, 0, sizeof(ghd));
214 		for (i = 0; i < lh->num_descs; i++)
215 			ghd.values[i] = test_bit(i, vals);
216 
217 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
218 			return -EFAULT;
219 
220 		return 0;
221 	} else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
222 		/*
223 		 * All line descriptors were created at once with the same
224 		 * flags so just check if the first one is really output.
225 		 */
226 		if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
227 			return -EPERM;
228 
229 		if (copy_from_user(&ghd, ip, sizeof(ghd)))
230 			return -EFAULT;
231 
232 		/* Clamp all values to [0,1] */
233 		for (i = 0; i < lh->num_descs; i++)
234 			__assign_bit(i, vals, ghd.values[i]);
235 
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);
245 	}
246 	return -EINVAL;
247 }
248 
249 #ifdef CONFIG_COMPAT
250 static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
251 				    unsigned long arg)
252 {
253 	return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
254 }
255 #endif
256 
257 static void linehandle_free(struct linehandle_state *lh)
258 {
259 	int i;
260 
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);
267 }
268 
269 static int linehandle_release(struct inode *inode, struct file *file)
270 {
271 	linehandle_free(file->private_data);
272 	return 0;
273 }
274 
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
283 };
284 
285 static int linehandle_create(struct gpio_device *gdev, void __user *ip)
286 {
287 	struct gpiohandle_request handlereq;
288 	struct linehandle_state *lh;
289 	struct file *file;
290 	int fd, i, ret;
291 	u32 lflags;
292 
293 	if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
294 		return -EFAULT;
295 	if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
296 		return -EINVAL;
297 
298 	lflags = handlereq.flags;
299 
300 	ret = linehandle_validate_flags(lflags);
301 	if (ret)
302 		return ret;
303 
304 	lh = kzalloc(sizeof(*lh), GFP_KERNEL);
305 	if (!lh)
306 		return -ENOMEM;
307 	lh->gdev = gdev;
308 	get_device(&gdev->dev);
309 
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;
318 		}
319 	}
320 
321 	lh->num_descs = handlereq.lines;
322 
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);
327 
328 		if (IS_ERR(desc)) {
329 			ret = PTR_ERR(desc);
330 			goto out_free_lh;
331 		}
332 
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);
338 
339 		ret = gpiod_set_transitory(desc, false);
340 		if (ret < 0)
341 			goto out_free_lh;
342 
343 		/*
344 		 * Lines have to be requested explicitly for input
345 		 * or output, else the line will be treated "as is".
346 		 */
347 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
348 			int val = !!handlereq.default_values[i];
349 
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;
357 		}
358 
359 		blocking_notifier_call_chain(&desc->gdev->notifier,
360 					     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
361 
362 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
363 			offset);
364 	}
365 
366 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
367 	if (fd < 0) {
368 		ret = fd;
369 		goto out_free_lh;
370 	}
371 
372 	file = anon_inode_getfile("gpio-linehandle",
373 				  &linehandle_fileops,
374 				  lh,
375 				  O_RDONLY | O_CLOEXEC);
376 	if (IS_ERR(file)) {
377 		ret = PTR_ERR(file);
378 		goto out_put_unused_fd;
379 	}
380 
381 	handlereq.fd = fd;
382 	if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
383 		/*
384 		 * fput() will trigger the release() callback, so do not go onto
385 		 * the regular error cleanup path here.
386 		 */
387 		fput(file);
388 		put_unused_fd(fd);
389 		return -EFAULT;
390 	}
391 
392 	fd_install(fd, file);
393 
394 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
395 		lh->num_descs);
396 
397 	return 0;
398 
399 out_put_unused_fd:
400 	put_unused_fd(fd);
401 out_free_lh:
402 	linehandle_free(lh);
403 	return ret;
404 }
405 #endif /* CONFIG_GPIO_CDEV_V1 */
406 
407 /**
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
423  */
424 struct line {
425 	struct gpio_desc *desc;
426 	/*
427 	 * -- edge detector specific fields --
428 	 */
429 	struct linereq *req;
430 	unsigned int irq;
431 	u64 eflags;
432 	/*
433 	 * timestamp_ns and req_seqno are accessed only by
434 	 * edge_irq_handler() and edge_irq_thread(), which are themselves
435 	 * mutually exclusive, so no additional protection is necessary.
436 	 */
437 	u64 timestamp_ns;
438 	u32 req_seqno;
439 	/*
440 	 * line_seqno is accessed by either edge_irq_thread() or
441 	 * debounce_work_func(), which are themselves mutually exclusive,
442 	 * so no additional protection is necessary.
443 	 */
444 	u32 line_seqno;
445 	/*
446 	 * -- debouncer specific fields --
447 	 */
448 	struct delayed_work work;
449 	/*
450 	 * sw_debounce is accessed by linereq_set_config(), which is the
451 	 * only setter, and linereq_get_values(), which can live with a
452 	 * slightly stale value.
453 	 */
454 	unsigned int sw_debounced;
455 	/*
456 	 * level is accessed by debounce_work_func(), which is the only
457 	 * setter, and linereq_get_values() which can live with a slightly
458 	 * stale value.
459 	 */
460 	unsigned int level;
461 };
462 
463 /**
464  * struct linereq - contains the state of a userspace line request
465  * @gdev: the GPIO device the line request pertains to
466  * @label: consumer label used to tag GPIO descriptors
467  * @num_lines: the number of lines in the lines array
468  * @wait: wait queue that handles blocking reads of events
469  * @event_buffer_size: the number of elements allocated in @events
470  * @events: KFIFO for the GPIO events
471  * @seqno: the sequence number for edge events generated on all lines in
472  * this line request.  Note that this is not used when @num_lines is 1, as
473  * the line_seqno is then the same and is cheaper to calculate.
474  * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
475  * of configuration, particularly multi-step accesses to desc flags.
476  * @lines: the lines held by this line request, with @num_lines elements.
477  */
478 struct linereq {
479 	struct gpio_device *gdev;
480 	const char *label;
481 	u32 num_lines;
482 	wait_queue_head_t wait;
483 	u32 event_buffer_size;
484 	DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
485 	atomic_t seqno;
486 	struct mutex config_mutex;
487 	struct line lines[];
488 };
489 
490 #define GPIO_V2_LINE_BIAS_FLAGS \
491 	(GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
492 	 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
493 	 GPIO_V2_LINE_FLAG_BIAS_DISABLED)
494 
495 #define GPIO_V2_LINE_DIRECTION_FLAGS \
496 	(GPIO_V2_LINE_FLAG_INPUT | \
497 	 GPIO_V2_LINE_FLAG_OUTPUT)
498 
499 #define GPIO_V2_LINE_DRIVE_FLAGS \
500 	(GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
501 	 GPIO_V2_LINE_FLAG_OPEN_SOURCE)
502 
503 #define GPIO_V2_LINE_EDGE_FLAGS \
504 	(GPIO_V2_LINE_FLAG_EDGE_RISING | \
505 	 GPIO_V2_LINE_FLAG_EDGE_FALLING)
506 
507 #define GPIO_V2_LINE_VALID_FLAGS \
508 	(GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
509 	 GPIO_V2_LINE_DIRECTION_FLAGS | \
510 	 GPIO_V2_LINE_DRIVE_FLAGS | \
511 	 GPIO_V2_LINE_EDGE_FLAGS | \
512 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
513 	 GPIO_V2_LINE_BIAS_FLAGS)
514 
515 static void linereq_put_event(struct linereq *lr,
516 			      struct gpio_v2_line_event *le)
517 {
518 	bool overflow = false;
519 
520 	spin_lock(&lr->wait.lock);
521 	if (kfifo_is_full(&lr->events)) {
522 		overflow = true;
523 		kfifo_skip(&lr->events);
524 	}
525 	kfifo_in(&lr->events, le, 1);
526 	spin_unlock(&lr->wait.lock);
527 	if (!overflow)
528 		wake_up_poll(&lr->wait, EPOLLIN);
529 	else
530 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
531 }
532 
533 static u64 line_event_timestamp(struct line *line)
534 {
535 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
536 		return ktime_get_real_ns();
537 
538 	return ktime_get_ns();
539 }
540 
541 static irqreturn_t edge_irq_thread(int irq, void *p)
542 {
543 	struct line *line = p;
544 	struct linereq *lr = line->req;
545 	struct gpio_v2_line_event le;
546 
547 	/* Do not leak kernel stack to userspace */
548 	memset(&le, 0, sizeof(le));
549 
550 	if (line->timestamp_ns) {
551 		le.timestamp_ns = line->timestamp_ns;
552 	} else {
553 		/*
554 		 * We may be running from a nested threaded interrupt in
555 		 * which case we didn't get the timestamp from
556 		 * edge_irq_handler().
557 		 */
558 		le.timestamp_ns = line_event_timestamp(line);
559 		if (lr->num_lines != 1)
560 			line->req_seqno = atomic_inc_return(&lr->seqno);
561 	}
562 	line->timestamp_ns = 0;
563 
564 	if (line->eflags == (GPIO_V2_LINE_FLAG_EDGE_RISING |
565 			     GPIO_V2_LINE_FLAG_EDGE_FALLING)) {
566 		int level = gpiod_get_value_cansleep(line->desc);
567 
568 		if (level)
569 			/* Emit low-to-high event */
570 			le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
571 		else
572 			/* Emit high-to-low event */
573 			le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
574 	} else if (line->eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) {
575 		/* Emit low-to-high event */
576 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
577 	} else if (line->eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) {
578 		/* Emit high-to-low event */
579 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
580 	} else {
581 		return IRQ_NONE;
582 	}
583 	line->line_seqno++;
584 	le.line_seqno = line->line_seqno;
585 	le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
586 	le.offset = gpio_chip_hwgpio(line->desc);
587 
588 	linereq_put_event(lr, &le);
589 
590 	return IRQ_HANDLED;
591 }
592 
593 static irqreturn_t edge_irq_handler(int irq, void *p)
594 {
595 	struct line *line = p;
596 	struct linereq *lr = line->req;
597 
598 	/*
599 	 * Just store the timestamp in hardirq context so we get it as
600 	 * close in time as possible to the actual event.
601 	 */
602 	line->timestamp_ns = line_event_timestamp(line);
603 
604 	if (lr->num_lines != 1)
605 		line->req_seqno = atomic_inc_return(&lr->seqno);
606 
607 	return IRQ_WAKE_THREAD;
608 }
609 
610 /*
611  * returns the current debounced logical value.
612  */
613 static bool debounced_value(struct line *line)
614 {
615 	bool value;
616 
617 	/*
618 	 * minor race - debouncer may be stopped here, so edge_detector_stop()
619 	 * must leave the value unchanged so the following will read the level
620 	 * from when the debouncer was last running.
621 	 */
622 	value = READ_ONCE(line->level);
623 
624 	if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
625 		value = !value;
626 
627 	return value;
628 }
629 
630 static irqreturn_t debounce_irq_handler(int irq, void *p)
631 {
632 	struct line *line = p;
633 
634 	mod_delayed_work(system_wq, &line->work,
635 		usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
636 
637 	return IRQ_HANDLED;
638 }
639 
640 static void debounce_work_func(struct work_struct *work)
641 {
642 	struct gpio_v2_line_event le;
643 	struct line *line = container_of(work, struct line, work.work);
644 	struct linereq *lr;
645 	int level;
646 
647 	level = gpiod_get_raw_value_cansleep(line->desc);
648 	if (level < 0) {
649 		pr_debug_ratelimited("debouncer failed to read line value\n");
650 		return;
651 	}
652 
653 	if (READ_ONCE(line->level) == level)
654 		return;
655 
656 	WRITE_ONCE(line->level, level);
657 
658 	/* -- edge detection -- */
659 	if (!line->eflags)
660 		return;
661 
662 	/* switch from physical level to logical - if they differ */
663 	if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
664 		level = !level;
665 
666 	/* ignore edges that are not being monitored */
667 	if (((line->eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
668 	    ((line->eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
669 		return;
670 
671 	/* Do not leak kernel stack to userspace */
672 	memset(&le, 0, sizeof(le));
673 
674 	lr = line->req;
675 	le.timestamp_ns = line_event_timestamp(line);
676 	le.offset = gpio_chip_hwgpio(line->desc);
677 	line->line_seqno++;
678 	le.line_seqno = line->line_seqno;
679 	le.seqno = (lr->num_lines == 1) ?
680 		le.line_seqno : atomic_inc_return(&lr->seqno);
681 
682 	if (level)
683 		/* Emit low-to-high event */
684 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
685 	else
686 		/* Emit high-to-low event */
687 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
688 
689 	linereq_put_event(lr, &le);
690 }
691 
692 static int debounce_setup(struct line *line,
693 			  unsigned int debounce_period_us)
694 {
695 	unsigned long irqflags;
696 	int ret, level, irq;
697 
698 	/* try hardware */
699 	ret = gpiod_set_debounce(line->desc, debounce_period_us);
700 	if (!ret) {
701 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
702 		return ret;
703 	}
704 	if (ret != -ENOTSUPP)
705 		return ret;
706 
707 	if (debounce_period_us) {
708 		/* setup software debounce */
709 		level = gpiod_get_raw_value_cansleep(line->desc);
710 		if (level < 0)
711 			return level;
712 
713 		irq = gpiod_to_irq(line->desc);
714 		if (irq < 0)
715 			return -ENXIO;
716 
717 		WRITE_ONCE(line->level, level);
718 		irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
719 		ret = request_irq(irq, debounce_irq_handler, irqflags,
720 				  line->req->label, line);
721 		if (ret)
722 			return ret;
723 
724 		WRITE_ONCE(line->sw_debounced, 1);
725 		line->irq = irq;
726 	}
727 	return 0;
728 }
729 
730 static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
731 					  unsigned int line_idx)
732 {
733 	unsigned int i;
734 	u64 mask = BIT_ULL(line_idx);
735 
736 	for (i = 0; i < lc->num_attrs; i++) {
737 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
738 		    (lc->attrs[i].mask & mask))
739 			return true;
740 	}
741 	return false;
742 }
743 
744 static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
745 					       unsigned int line_idx)
746 {
747 	unsigned int i;
748 	u64 mask = BIT_ULL(line_idx);
749 
750 	for (i = 0; i < lc->num_attrs; i++) {
751 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
752 		    (lc->attrs[i].mask & mask))
753 			return lc->attrs[i].attr.debounce_period_us;
754 	}
755 	return 0;
756 }
757 
758 static void edge_detector_stop(struct line *line)
759 {
760 	if (line->irq) {
761 		free_irq(line->irq, line);
762 		line->irq = 0;
763 	}
764 
765 	cancel_delayed_work_sync(&line->work);
766 	WRITE_ONCE(line->sw_debounced, 0);
767 	line->eflags = 0;
768 	/* do not change line->level - see comment in debounced_value() */
769 }
770 
771 static int edge_detector_setup(struct line *line,
772 			       struct gpio_v2_line_config *lc,
773 			       unsigned int line_idx,
774 			       u64 eflags)
775 {
776 	u32 debounce_period_us;
777 	unsigned long irqflags = 0;
778 	int irq, ret;
779 
780 	if (eflags && !kfifo_initialized(&line->req->events)) {
781 		ret = kfifo_alloc(&line->req->events,
782 				  line->req->event_buffer_size, GFP_KERNEL);
783 		if (ret)
784 			return ret;
785 	}
786 	line->eflags = eflags;
787 	if (gpio_v2_line_config_debounced(lc, line_idx)) {
788 		debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
789 		ret = debounce_setup(line, debounce_period_us);
790 		if (ret)
791 			return ret;
792 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
793 	}
794 
795 	/* detection disabled or sw debouncer will provide edge detection */
796 	if (!eflags || READ_ONCE(line->sw_debounced))
797 		return 0;
798 
799 	irq = gpiod_to_irq(line->desc);
800 	if (irq < 0)
801 		return -ENXIO;
802 
803 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
804 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
805 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
806 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
807 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
808 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
809 	irqflags |= IRQF_ONESHOT;
810 
811 	/* Request a thread to read the events */
812 	ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
813 				   irqflags, line->req->label, line);
814 	if (ret)
815 		return ret;
816 
817 	line->irq = irq;
818 	return 0;
819 }
820 
821 static int edge_detector_update(struct line *line,
822 				struct gpio_v2_line_config *lc,
823 				unsigned int line_idx,
824 				u64 eflags, bool polarity_change)
825 {
826 	unsigned int debounce_period_us =
827 		gpio_v2_line_config_debounce_period(lc, line_idx);
828 
829 	if ((line->eflags == eflags) && !polarity_change &&
830 	    (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us))
831 		return 0;
832 
833 	/* sw debounced and still will be...*/
834 	if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
835 		line->eflags = eflags;
836 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
837 		return 0;
838 	}
839 
840 	/* reconfiguring edge detection or sw debounce being disabled */
841 	if ((line->irq && !READ_ONCE(line->sw_debounced)) ||
842 	    (!debounce_period_us && READ_ONCE(line->sw_debounced)))
843 		edge_detector_stop(line);
844 
845 	return edge_detector_setup(line, lc, line_idx, eflags);
846 }
847 
848 static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
849 				     unsigned int line_idx)
850 {
851 	unsigned int i;
852 	u64 mask = BIT_ULL(line_idx);
853 
854 	for (i = 0; i < lc->num_attrs; i++) {
855 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
856 		    (lc->attrs[i].mask & mask))
857 			return lc->attrs[i].attr.flags;
858 	}
859 	return lc->flags;
860 }
861 
862 static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
863 					    unsigned int line_idx)
864 {
865 	unsigned int i;
866 	u64 mask = BIT_ULL(line_idx);
867 
868 	for (i = 0; i < lc->num_attrs; i++) {
869 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
870 		    (lc->attrs[i].mask & mask))
871 			return !!(lc->attrs[i].attr.values & mask);
872 	}
873 	return 0;
874 }
875 
876 static int gpio_v2_line_flags_validate(u64 flags)
877 {
878 	/* Return an error if an unknown flag is set */
879 	if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
880 		return -EINVAL;
881 
882 	/*
883 	 * Do not allow both INPUT and OUTPUT flags to be set as they are
884 	 * contradictory.
885 	 */
886 	if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
887 	    (flags & GPIO_V2_LINE_FLAG_OUTPUT))
888 		return -EINVAL;
889 
890 	/* Edge detection requires explicit input. */
891 	if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
892 	    !(flags & GPIO_V2_LINE_FLAG_INPUT))
893 		return -EINVAL;
894 
895 	/*
896 	 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
897 	 * request. If the hardware actually supports enabling both at the
898 	 * same time the electrical result would be disastrous.
899 	 */
900 	if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
901 	    (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
902 		return -EINVAL;
903 
904 	/* Drive requires explicit output direction. */
905 	if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
906 	    !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
907 		return -EINVAL;
908 
909 	/* Bias requires explicit direction. */
910 	if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
911 	    !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
912 		return -EINVAL;
913 
914 	/* Only one bias flag can be set. */
915 	if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
916 	     (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
917 		       GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
918 	    ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
919 	     (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
920 		return -EINVAL;
921 
922 	return 0;
923 }
924 
925 static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
926 					unsigned int num_lines)
927 {
928 	unsigned int i;
929 	u64 flags;
930 	int ret;
931 
932 	if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
933 		return -EINVAL;
934 
935 	if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
936 		return -EINVAL;
937 
938 	for (i = 0; i < num_lines; i++) {
939 		flags = gpio_v2_line_config_flags(lc, i);
940 		ret = gpio_v2_line_flags_validate(flags);
941 		if (ret)
942 			return ret;
943 
944 		/* debounce requires explicit input */
945 		if (gpio_v2_line_config_debounced(lc, i) &&
946 		    !(flags & GPIO_V2_LINE_FLAG_INPUT))
947 			return -EINVAL;
948 	}
949 	return 0;
950 }
951 
952 static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
953 						    unsigned long *flagsp)
954 {
955 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
956 		   flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
957 
958 	if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
959 		set_bit(FLAG_IS_OUT, flagsp);
960 	else if (flags & GPIO_V2_LINE_FLAG_INPUT)
961 		clear_bit(FLAG_IS_OUT, flagsp);
962 
963 	assign_bit(FLAG_EDGE_RISING, flagsp,
964 		   flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
965 	assign_bit(FLAG_EDGE_FALLING, flagsp,
966 		   flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
967 
968 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
969 		   flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
970 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
971 		   flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
972 
973 	assign_bit(FLAG_PULL_UP, flagsp,
974 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
975 	assign_bit(FLAG_PULL_DOWN, flagsp,
976 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
977 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
978 		   flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
979 
980 	assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
981 		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
982 }
983 
984 static long linereq_get_values(struct linereq *lr, void __user *ip)
985 {
986 	struct gpio_v2_line_values lv;
987 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
988 	struct gpio_desc **descs;
989 	unsigned int i, didx, num_get;
990 	bool val;
991 	int ret;
992 
993 	/* NOTE: It's ok to read values of output lines. */
994 	if (copy_from_user(&lv, ip, sizeof(lv)))
995 		return -EFAULT;
996 
997 	for (num_get = 0, i = 0; i < lr->num_lines; i++) {
998 		if (lv.mask & BIT_ULL(i)) {
999 			num_get++;
1000 			descs = &lr->lines[i].desc;
1001 		}
1002 	}
1003 
1004 	if (num_get == 0)
1005 		return -EINVAL;
1006 
1007 	if (num_get != 1) {
1008 		descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
1009 		if (!descs)
1010 			return -ENOMEM;
1011 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
1012 			if (lv.mask & BIT_ULL(i)) {
1013 				descs[didx] = lr->lines[i].desc;
1014 				didx++;
1015 			}
1016 		}
1017 	}
1018 	ret = gpiod_get_array_value_complex(false, true, num_get,
1019 					    descs, NULL, vals);
1020 
1021 	if (num_get != 1)
1022 		kfree(descs);
1023 	if (ret)
1024 		return ret;
1025 
1026 	lv.bits = 0;
1027 	for (didx = 0, i = 0; i < lr->num_lines; i++) {
1028 		if (lv.mask & BIT_ULL(i)) {
1029 			if (lr->lines[i].sw_debounced)
1030 				val = debounced_value(&lr->lines[i]);
1031 			else
1032 				val = test_bit(didx, vals);
1033 			if (val)
1034 				lv.bits |= BIT_ULL(i);
1035 			didx++;
1036 		}
1037 	}
1038 
1039 	if (copy_to_user(ip, &lv, sizeof(lv)))
1040 		return -EFAULT;
1041 
1042 	return 0;
1043 }
1044 
1045 static long linereq_set_values_unlocked(struct linereq *lr,
1046 					struct gpio_v2_line_values *lv)
1047 {
1048 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
1049 	struct gpio_desc **descs;
1050 	unsigned int i, didx, num_set;
1051 	int ret;
1052 
1053 	bitmap_zero(vals, GPIO_V2_LINES_MAX);
1054 	for (num_set = 0, i = 0; i < lr->num_lines; i++) {
1055 		if (lv->mask & BIT_ULL(i)) {
1056 			if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
1057 				return -EPERM;
1058 			if (lv->bits & BIT_ULL(i))
1059 				__set_bit(num_set, vals);
1060 			num_set++;
1061 			descs = &lr->lines[i].desc;
1062 		}
1063 	}
1064 	if (num_set == 0)
1065 		return -EINVAL;
1066 
1067 	if (num_set != 1) {
1068 		/* build compacted desc array and values */
1069 		descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
1070 		if (!descs)
1071 			return -ENOMEM;
1072 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
1073 			if (lv->mask & BIT_ULL(i)) {
1074 				descs[didx] = lr->lines[i].desc;
1075 				didx++;
1076 			}
1077 		}
1078 	}
1079 	ret = gpiod_set_array_value_complex(false, true, num_set,
1080 					    descs, NULL, vals);
1081 
1082 	if (num_set != 1)
1083 		kfree(descs);
1084 	return ret;
1085 }
1086 
1087 static long linereq_set_values(struct linereq *lr, void __user *ip)
1088 {
1089 	struct gpio_v2_line_values lv;
1090 	int ret;
1091 
1092 	if (copy_from_user(&lv, ip, sizeof(lv)))
1093 		return -EFAULT;
1094 
1095 	mutex_lock(&lr->config_mutex);
1096 
1097 	ret = linereq_set_values_unlocked(lr, &lv);
1098 
1099 	mutex_unlock(&lr->config_mutex);
1100 
1101 	return ret;
1102 }
1103 
1104 static long linereq_set_config_unlocked(struct linereq *lr,
1105 					struct gpio_v2_line_config *lc)
1106 {
1107 	struct gpio_desc *desc;
1108 	unsigned int i;
1109 	u64 flags;
1110 	bool polarity_change;
1111 	int ret;
1112 
1113 	for (i = 0; i < lr->num_lines; i++) {
1114 		desc = lr->lines[i].desc;
1115 		flags = gpio_v2_line_config_flags(lc, i);
1116 		polarity_change =
1117 			(!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) !=
1118 			 ((flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) != 0));
1119 
1120 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1121 		/*
1122 		 * Lines have to be requested explicitly for input
1123 		 * or output, else the line will be treated "as is".
1124 		 */
1125 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1126 			int val = gpio_v2_line_config_output_value(lc, i);
1127 
1128 			edge_detector_stop(&lr->lines[i]);
1129 			ret = gpiod_direction_output(desc, val);
1130 			if (ret)
1131 				return ret;
1132 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1133 			ret = gpiod_direction_input(desc);
1134 			if (ret)
1135 				return ret;
1136 
1137 			ret = edge_detector_update(&lr->lines[i], lc, i,
1138 					flags & GPIO_V2_LINE_EDGE_FLAGS,
1139 					polarity_change);
1140 			if (ret)
1141 				return ret;
1142 		}
1143 
1144 		blocking_notifier_call_chain(&desc->gdev->notifier,
1145 					     GPIO_V2_LINE_CHANGED_CONFIG,
1146 					     desc);
1147 	}
1148 	return 0;
1149 }
1150 
1151 static long linereq_set_config(struct linereq *lr, void __user *ip)
1152 {
1153 	struct gpio_v2_line_config lc;
1154 	int ret;
1155 
1156 	if (copy_from_user(&lc, ip, sizeof(lc)))
1157 		return -EFAULT;
1158 
1159 	ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
1160 	if (ret)
1161 		return ret;
1162 
1163 	mutex_lock(&lr->config_mutex);
1164 
1165 	ret = linereq_set_config_unlocked(lr, &lc);
1166 
1167 	mutex_unlock(&lr->config_mutex);
1168 
1169 	return ret;
1170 }
1171 
1172 static long linereq_ioctl(struct file *file, unsigned int cmd,
1173 			  unsigned long arg)
1174 {
1175 	struct linereq *lr = file->private_data;
1176 	void __user *ip = (void __user *)arg;
1177 
1178 	if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL)
1179 		return linereq_get_values(lr, ip);
1180 	else if (cmd == GPIO_V2_LINE_SET_VALUES_IOCTL)
1181 		return linereq_set_values(lr, ip);
1182 	else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL)
1183 		return linereq_set_config(lr, ip);
1184 
1185 	return -EINVAL;
1186 }
1187 
1188 #ifdef CONFIG_COMPAT
1189 static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
1190 				 unsigned long arg)
1191 {
1192 	return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1193 }
1194 #endif
1195 
1196 static __poll_t linereq_poll(struct file *file,
1197 			    struct poll_table_struct *wait)
1198 {
1199 	struct linereq *lr = file->private_data;
1200 	__poll_t events = 0;
1201 
1202 	poll_wait(file, &lr->wait, wait);
1203 
1204 	if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
1205 						 &lr->wait.lock))
1206 		events = EPOLLIN | EPOLLRDNORM;
1207 
1208 	return events;
1209 }
1210 
1211 static ssize_t linereq_read(struct file *file,
1212 			    char __user *buf,
1213 			    size_t count,
1214 			    loff_t *f_ps)
1215 {
1216 	struct linereq *lr = file->private_data;
1217 	struct gpio_v2_line_event le;
1218 	ssize_t bytes_read = 0;
1219 	int ret;
1220 
1221 	if (count < sizeof(le))
1222 		return -EINVAL;
1223 
1224 	do {
1225 		spin_lock(&lr->wait.lock);
1226 		if (kfifo_is_empty(&lr->events)) {
1227 			if (bytes_read) {
1228 				spin_unlock(&lr->wait.lock);
1229 				return bytes_read;
1230 			}
1231 
1232 			if (file->f_flags & O_NONBLOCK) {
1233 				spin_unlock(&lr->wait.lock);
1234 				return -EAGAIN;
1235 			}
1236 
1237 			ret = wait_event_interruptible_locked(lr->wait,
1238 					!kfifo_is_empty(&lr->events));
1239 			if (ret) {
1240 				spin_unlock(&lr->wait.lock);
1241 				return ret;
1242 			}
1243 		}
1244 
1245 		ret = kfifo_out(&lr->events, &le, 1);
1246 		spin_unlock(&lr->wait.lock);
1247 		if (ret != 1) {
1248 			/*
1249 			 * This should never happen - we were holding the
1250 			 * lock from the moment we learned the fifo is no
1251 			 * longer empty until now.
1252 			 */
1253 			ret = -EIO;
1254 			break;
1255 		}
1256 
1257 		if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
1258 			return -EFAULT;
1259 		bytes_read += sizeof(le);
1260 	} while (count >= bytes_read + sizeof(le));
1261 
1262 	return bytes_read;
1263 }
1264 
1265 static void linereq_free(struct linereq *lr)
1266 {
1267 	unsigned int i;
1268 
1269 	for (i = 0; i < lr->num_lines; i++) {
1270 		edge_detector_stop(&lr->lines[i]);
1271 		if (lr->lines[i].desc)
1272 			gpiod_free(lr->lines[i].desc);
1273 	}
1274 	kfifo_free(&lr->events);
1275 	kfree(lr->label);
1276 	put_device(&lr->gdev->dev);
1277 	kfree(lr);
1278 }
1279 
1280 static int linereq_release(struct inode *inode, struct file *file)
1281 {
1282 	struct linereq *lr = file->private_data;
1283 
1284 	linereq_free(lr);
1285 	return 0;
1286 }
1287 
1288 static const struct file_operations line_fileops = {
1289 	.release = linereq_release,
1290 	.read = linereq_read,
1291 	.poll = linereq_poll,
1292 	.owner = THIS_MODULE,
1293 	.llseek = noop_llseek,
1294 	.unlocked_ioctl = linereq_ioctl,
1295 #ifdef CONFIG_COMPAT
1296 	.compat_ioctl = linereq_ioctl_compat,
1297 #endif
1298 };
1299 
1300 static int linereq_create(struct gpio_device *gdev, void __user *ip)
1301 {
1302 	struct gpio_v2_line_request ulr;
1303 	struct gpio_v2_line_config *lc;
1304 	struct linereq *lr;
1305 	struct file *file;
1306 	u64 flags;
1307 	unsigned int i;
1308 	int fd, ret;
1309 
1310 	if (copy_from_user(&ulr, ip, sizeof(ulr)))
1311 		return -EFAULT;
1312 
1313 	if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
1314 		return -EINVAL;
1315 
1316 	if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
1317 		return -EINVAL;
1318 
1319 	lc = &ulr.config;
1320 	ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
1321 	if (ret)
1322 		return ret;
1323 
1324 	lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
1325 	if (!lr)
1326 		return -ENOMEM;
1327 
1328 	lr->gdev = gdev;
1329 	get_device(&gdev->dev);
1330 
1331 	for (i = 0; i < ulr.num_lines; i++) {
1332 		lr->lines[i].req = lr;
1333 		WRITE_ONCE(lr->lines[i].sw_debounced, 0);
1334 		INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
1335 	}
1336 
1337 	if (ulr.consumer[0] != '\0') {
1338 		/* label is only initialized if consumer is set */
1339 		lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1340 				     GFP_KERNEL);
1341 		if (!lr->label) {
1342 			ret = -ENOMEM;
1343 			goto out_free_linereq;
1344 		}
1345 	}
1346 
1347 	mutex_init(&lr->config_mutex);
1348 	init_waitqueue_head(&lr->wait);
1349 	lr->event_buffer_size = ulr.event_buffer_size;
1350 	if (lr->event_buffer_size == 0)
1351 		lr->event_buffer_size = ulr.num_lines * 16;
1352 	else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
1353 		lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
1354 
1355 	atomic_set(&lr->seqno, 0);
1356 	lr->num_lines = ulr.num_lines;
1357 
1358 	/* Request each GPIO */
1359 	for (i = 0; i < ulr.num_lines; i++) {
1360 		u32 offset = ulr.offsets[i];
1361 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
1362 
1363 		if (IS_ERR(desc)) {
1364 			ret = PTR_ERR(desc);
1365 			goto out_free_linereq;
1366 		}
1367 
1368 		ret = gpiod_request(desc, lr->label);
1369 		if (ret)
1370 			goto out_free_linereq;
1371 
1372 		lr->lines[i].desc = desc;
1373 		flags = gpio_v2_line_config_flags(lc, i);
1374 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1375 
1376 		ret = gpiod_set_transitory(desc, false);
1377 		if (ret < 0)
1378 			goto out_free_linereq;
1379 
1380 		/*
1381 		 * Lines have to be requested explicitly for input
1382 		 * or output, else the line will be treated "as is".
1383 		 */
1384 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1385 			int val = gpio_v2_line_config_output_value(lc, i);
1386 
1387 			ret = gpiod_direction_output(desc, val);
1388 			if (ret)
1389 				goto out_free_linereq;
1390 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1391 			ret = gpiod_direction_input(desc);
1392 			if (ret)
1393 				goto out_free_linereq;
1394 
1395 			ret = edge_detector_setup(&lr->lines[i], lc, i,
1396 					flags & GPIO_V2_LINE_EDGE_FLAGS);
1397 			if (ret)
1398 				goto out_free_linereq;
1399 		}
1400 
1401 		blocking_notifier_call_chain(&desc->gdev->notifier,
1402 					     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1403 
1404 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
1405 			offset);
1406 	}
1407 
1408 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1409 	if (fd < 0) {
1410 		ret = fd;
1411 		goto out_free_linereq;
1412 	}
1413 
1414 	file = anon_inode_getfile("gpio-line", &line_fileops, lr,
1415 				  O_RDONLY | O_CLOEXEC);
1416 	if (IS_ERR(file)) {
1417 		ret = PTR_ERR(file);
1418 		goto out_put_unused_fd;
1419 	}
1420 
1421 	ulr.fd = fd;
1422 	if (copy_to_user(ip, &ulr, sizeof(ulr))) {
1423 		/*
1424 		 * fput() will trigger the release() callback, so do not go onto
1425 		 * the regular error cleanup path here.
1426 		 */
1427 		fput(file);
1428 		put_unused_fd(fd);
1429 		return -EFAULT;
1430 	}
1431 
1432 	fd_install(fd, file);
1433 
1434 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
1435 		lr->num_lines);
1436 
1437 	return 0;
1438 
1439 out_put_unused_fd:
1440 	put_unused_fd(fd);
1441 out_free_linereq:
1442 	linereq_free(lr);
1443 	return ret;
1444 }
1445 
1446 #ifdef CONFIG_GPIO_CDEV_V1
1447 
1448 /*
1449  * GPIO line event management
1450  */
1451 
1452 /**
1453  * struct lineevent_state - contains the state of a userspace event
1454  * @gdev: the GPIO device the event pertains to
1455  * @label: consumer label used to tag descriptors
1456  * @desc: the GPIO descriptor held by this event
1457  * @eflags: the event flags this line was requested with
1458  * @irq: the interrupt that trigger in response to events on this GPIO
1459  * @wait: wait queue that handles blocking reads of events
1460  * @events: KFIFO for the GPIO events
1461  * @timestamp: cache for the timestamp storing it between hardirq
1462  * and IRQ thread, used to bring the timestamp close to the actual
1463  * event
1464  */
1465 struct lineevent_state {
1466 	struct gpio_device *gdev;
1467 	const char *label;
1468 	struct gpio_desc *desc;
1469 	u32 eflags;
1470 	int irq;
1471 	wait_queue_head_t wait;
1472 	DECLARE_KFIFO(events, struct gpioevent_data, 16);
1473 	u64 timestamp;
1474 };
1475 
1476 #define GPIOEVENT_REQUEST_VALID_FLAGS \
1477 	(GPIOEVENT_REQUEST_RISING_EDGE | \
1478 	GPIOEVENT_REQUEST_FALLING_EDGE)
1479 
1480 static __poll_t lineevent_poll(struct file *file,
1481 			       struct poll_table_struct *wait)
1482 {
1483 	struct lineevent_state *le = file->private_data;
1484 	__poll_t events = 0;
1485 
1486 	poll_wait(file, &le->wait, wait);
1487 
1488 	if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1489 		events = EPOLLIN | EPOLLRDNORM;
1490 
1491 	return events;
1492 }
1493 
1494 struct compat_gpioeevent_data {
1495 	compat_u64	timestamp;
1496 	u32		id;
1497 };
1498 
1499 static ssize_t lineevent_read(struct file *file,
1500 			      char __user *buf,
1501 			      size_t count,
1502 			      loff_t *f_ps)
1503 {
1504 	struct lineevent_state *le = file->private_data;
1505 	struct gpioevent_data ge;
1506 	ssize_t bytes_read = 0;
1507 	ssize_t ge_size;
1508 	int ret;
1509 
1510 	/*
1511 	 * When compatible system call is being used the struct gpioevent_data,
1512 	 * in case of at least ia32, has different size due to the alignment
1513 	 * differences. Because we have first member 64 bits followed by one of
1514 	 * 32 bits there is no gap between them. The only difference is the
1515 	 * padding at the end of the data structure. Hence, we calculate the
1516 	 * actual sizeof() and pass this as an argument to copy_to_user() to
1517 	 * drop unneeded bytes from the output.
1518 	 */
1519 	if (compat_need_64bit_alignment_fixup())
1520 		ge_size = sizeof(struct compat_gpioeevent_data);
1521 	else
1522 		ge_size = sizeof(struct gpioevent_data);
1523 	if (count < ge_size)
1524 		return -EINVAL;
1525 
1526 	do {
1527 		spin_lock(&le->wait.lock);
1528 		if (kfifo_is_empty(&le->events)) {
1529 			if (bytes_read) {
1530 				spin_unlock(&le->wait.lock);
1531 				return bytes_read;
1532 			}
1533 
1534 			if (file->f_flags & O_NONBLOCK) {
1535 				spin_unlock(&le->wait.lock);
1536 				return -EAGAIN;
1537 			}
1538 
1539 			ret = wait_event_interruptible_locked(le->wait,
1540 					!kfifo_is_empty(&le->events));
1541 			if (ret) {
1542 				spin_unlock(&le->wait.lock);
1543 				return ret;
1544 			}
1545 		}
1546 
1547 		ret = kfifo_out(&le->events, &ge, 1);
1548 		spin_unlock(&le->wait.lock);
1549 		if (ret != 1) {
1550 			/*
1551 			 * This should never happen - we were holding the lock
1552 			 * from the moment we learned the fifo is no longer
1553 			 * empty until now.
1554 			 */
1555 			ret = -EIO;
1556 			break;
1557 		}
1558 
1559 		if (copy_to_user(buf + bytes_read, &ge, ge_size))
1560 			return -EFAULT;
1561 		bytes_read += ge_size;
1562 	} while (count >= bytes_read + ge_size);
1563 
1564 	return bytes_read;
1565 }
1566 
1567 static void lineevent_free(struct lineevent_state *le)
1568 {
1569 	if (le->irq)
1570 		free_irq(le->irq, le);
1571 	if (le->desc)
1572 		gpiod_free(le->desc);
1573 	kfree(le->label);
1574 	put_device(&le->gdev->dev);
1575 	kfree(le);
1576 }
1577 
1578 static int lineevent_release(struct inode *inode, struct file *file)
1579 {
1580 	lineevent_free(file->private_data);
1581 	return 0;
1582 }
1583 
1584 static long lineevent_ioctl(struct file *file, unsigned int cmd,
1585 			    unsigned long arg)
1586 {
1587 	struct lineevent_state *le = file->private_data;
1588 	void __user *ip = (void __user *)arg;
1589 	struct gpiohandle_data ghd;
1590 
1591 	/*
1592 	 * We can get the value for an event line but not set it,
1593 	 * because it is input by definition.
1594 	 */
1595 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
1596 		int val;
1597 
1598 		memset(&ghd, 0, sizeof(ghd));
1599 
1600 		val = gpiod_get_value_cansleep(le->desc);
1601 		if (val < 0)
1602 			return val;
1603 		ghd.values[0] = val;
1604 
1605 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
1606 			return -EFAULT;
1607 
1608 		return 0;
1609 	}
1610 	return -EINVAL;
1611 }
1612 
1613 #ifdef CONFIG_COMPAT
1614 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
1615 				   unsigned long arg)
1616 {
1617 	return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1618 }
1619 #endif
1620 
1621 static const struct file_operations lineevent_fileops = {
1622 	.release = lineevent_release,
1623 	.read = lineevent_read,
1624 	.poll = lineevent_poll,
1625 	.owner = THIS_MODULE,
1626 	.llseek = noop_llseek,
1627 	.unlocked_ioctl = lineevent_ioctl,
1628 #ifdef CONFIG_COMPAT
1629 	.compat_ioctl = lineevent_ioctl_compat,
1630 #endif
1631 };
1632 
1633 static irqreturn_t lineevent_irq_thread(int irq, void *p)
1634 {
1635 	struct lineevent_state *le = p;
1636 	struct gpioevent_data ge;
1637 	int ret;
1638 
1639 	/* Do not leak kernel stack to userspace */
1640 	memset(&ge, 0, sizeof(ge));
1641 
1642 	/*
1643 	 * We may be running from a nested threaded interrupt in which case
1644 	 * we didn't get the timestamp from lineevent_irq_handler().
1645 	 */
1646 	if (!le->timestamp)
1647 		ge.timestamp = ktime_get_ns();
1648 	else
1649 		ge.timestamp = le->timestamp;
1650 
1651 	if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
1652 	    && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1653 		int level = gpiod_get_value_cansleep(le->desc);
1654 
1655 		if (level)
1656 			/* Emit low-to-high event */
1657 			ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1658 		else
1659 			/* Emit high-to-low event */
1660 			ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1661 	} else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
1662 		/* Emit low-to-high event */
1663 		ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1664 	} else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1665 		/* Emit high-to-low event */
1666 		ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1667 	} else {
1668 		return IRQ_NONE;
1669 	}
1670 
1671 	ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
1672 					    1, &le->wait.lock);
1673 	if (ret)
1674 		wake_up_poll(&le->wait, EPOLLIN);
1675 	else
1676 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
1677 
1678 	return IRQ_HANDLED;
1679 }
1680 
1681 static irqreturn_t lineevent_irq_handler(int irq, void *p)
1682 {
1683 	struct lineevent_state *le = p;
1684 
1685 	/*
1686 	 * Just store the timestamp in hardirq context so we get it as
1687 	 * close in time as possible to the actual event.
1688 	 */
1689 	le->timestamp = ktime_get_ns();
1690 
1691 	return IRQ_WAKE_THREAD;
1692 }
1693 
1694 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1695 {
1696 	struct gpioevent_request eventreq;
1697 	struct lineevent_state *le;
1698 	struct gpio_desc *desc;
1699 	struct file *file;
1700 	u32 offset;
1701 	u32 lflags;
1702 	u32 eflags;
1703 	int fd;
1704 	int ret;
1705 	int irq, irqflags = 0;
1706 
1707 	if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1708 		return -EFAULT;
1709 
1710 	offset = eventreq.lineoffset;
1711 	lflags = eventreq.handleflags;
1712 	eflags = eventreq.eventflags;
1713 
1714 	desc = gpiochip_get_desc(gdev->chip, offset);
1715 	if (IS_ERR(desc))
1716 		return PTR_ERR(desc);
1717 
1718 	/* Return an error if a unknown flag is set */
1719 	if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1720 	    (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1721 		return -EINVAL;
1722 
1723 	/* This is just wrong: we don't look for events on output lines */
1724 	if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1725 	    (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1726 	    (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1727 		return -EINVAL;
1728 
1729 	/* Only one bias flag can be set. */
1730 	if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1731 	     (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1732 			GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1733 	    ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1734 	     (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1735 		return -EINVAL;
1736 
1737 	le = kzalloc(sizeof(*le), GFP_KERNEL);
1738 	if (!le)
1739 		return -ENOMEM;
1740 	le->gdev = gdev;
1741 	get_device(&gdev->dev);
1742 
1743 	if (eventreq.consumer_label[0] != '\0') {
1744 		/* label is only initialized if consumer_label is set */
1745 		le->label = kstrndup(eventreq.consumer_label,
1746 				     sizeof(eventreq.consumer_label) - 1,
1747 				     GFP_KERNEL);
1748 		if (!le->label) {
1749 			ret = -ENOMEM;
1750 			goto out_free_le;
1751 		}
1752 	}
1753 
1754 	ret = gpiod_request(desc, le->label);
1755 	if (ret)
1756 		goto out_free_le;
1757 	le->desc = desc;
1758 	le->eflags = eflags;
1759 
1760 	linehandle_flags_to_desc_flags(lflags, &desc->flags);
1761 
1762 	ret = gpiod_direction_input(desc);
1763 	if (ret)
1764 		goto out_free_le;
1765 
1766 	blocking_notifier_call_chain(&desc->gdev->notifier,
1767 				     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1768 
1769 	irq = gpiod_to_irq(desc);
1770 	if (irq <= 0) {
1771 		ret = -ENODEV;
1772 		goto out_free_le;
1773 	}
1774 	le->irq = irq;
1775 
1776 	if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
1777 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1778 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1779 	if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
1780 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1781 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1782 	irqflags |= IRQF_ONESHOT;
1783 
1784 	INIT_KFIFO(le->events);
1785 	init_waitqueue_head(&le->wait);
1786 
1787 	/* Request a thread to read the events */
1788 	ret = request_threaded_irq(le->irq,
1789 				   lineevent_irq_handler,
1790 				   lineevent_irq_thread,
1791 				   irqflags,
1792 				   le->label,
1793 				   le);
1794 	if (ret)
1795 		goto out_free_le;
1796 
1797 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1798 	if (fd < 0) {
1799 		ret = fd;
1800 		goto out_free_le;
1801 	}
1802 
1803 	file = anon_inode_getfile("gpio-event",
1804 				  &lineevent_fileops,
1805 				  le,
1806 				  O_RDONLY | O_CLOEXEC);
1807 	if (IS_ERR(file)) {
1808 		ret = PTR_ERR(file);
1809 		goto out_put_unused_fd;
1810 	}
1811 
1812 	eventreq.fd = fd;
1813 	if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
1814 		/*
1815 		 * fput() will trigger the release() callback, so do not go onto
1816 		 * the regular error cleanup path here.
1817 		 */
1818 		fput(file);
1819 		put_unused_fd(fd);
1820 		return -EFAULT;
1821 	}
1822 
1823 	fd_install(fd, file);
1824 
1825 	return 0;
1826 
1827 out_put_unused_fd:
1828 	put_unused_fd(fd);
1829 out_free_le:
1830 	lineevent_free(le);
1831 	return ret;
1832 }
1833 
1834 static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
1835 				    struct gpioline_info *info_v1)
1836 {
1837 	u64 flagsv2 = info_v2->flags;
1838 
1839 	memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
1840 	memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
1841 	info_v1->line_offset = info_v2->offset;
1842 	info_v1->flags = 0;
1843 
1844 	if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
1845 		info_v1->flags |= GPIOLINE_FLAG_KERNEL;
1846 
1847 	if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
1848 		info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
1849 
1850 	if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
1851 		info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
1852 
1853 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
1854 		info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
1855 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
1856 		info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
1857 
1858 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
1859 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
1860 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
1861 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
1862 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
1863 		info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
1864 }
1865 
1866 static void gpio_v2_line_info_changed_to_v1(
1867 		struct gpio_v2_line_info_changed *lic_v2,
1868 		struct gpioline_info_changed *lic_v1)
1869 {
1870 	gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
1871 	lic_v1->timestamp = lic_v2->timestamp_ns;
1872 	lic_v1->event_type = lic_v2->event_type;
1873 }
1874 
1875 #endif /* CONFIG_GPIO_CDEV_V1 */
1876 
1877 static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
1878 				  struct gpio_v2_line_info *info)
1879 {
1880 	struct gpio_chip *gc = desc->gdev->chip;
1881 	bool ok_for_pinctrl;
1882 	unsigned long flags;
1883 	u32 debounce_period_us;
1884 	unsigned int num_attrs = 0;
1885 
1886 	memset(info, 0, sizeof(*info));
1887 	info->offset = gpio_chip_hwgpio(desc);
1888 
1889 	/*
1890 	 * This function takes a mutex so we must check this before taking
1891 	 * the spinlock.
1892 	 *
1893 	 * FIXME: find a non-racy way to retrieve this information. Maybe a
1894 	 * lock common to both frameworks?
1895 	 */
1896 	ok_for_pinctrl =
1897 		pinctrl_gpio_can_use_line(gc->base + info->offset);
1898 
1899 	spin_lock_irqsave(&gpio_lock, flags);
1900 
1901 	if (desc->name)
1902 		strscpy(info->name, desc->name, sizeof(info->name));
1903 
1904 	if (desc->label)
1905 		strscpy(info->consumer, desc->label, sizeof(info->consumer));
1906 
1907 	/*
1908 	 * Userspace only need to know that the kernel is using this GPIO so
1909 	 * it can't use it.
1910 	 */
1911 	info->flags = 0;
1912 	if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1913 	    test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1914 	    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1915 	    test_bit(FLAG_EXPORT, &desc->flags) ||
1916 	    test_bit(FLAG_SYSFS, &desc->flags) ||
1917 	    !gpiochip_line_is_valid(gc, info->offset) ||
1918 	    !ok_for_pinctrl)
1919 		info->flags |= GPIO_V2_LINE_FLAG_USED;
1920 
1921 	if (test_bit(FLAG_IS_OUT, &desc->flags))
1922 		info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
1923 	else
1924 		info->flags |= GPIO_V2_LINE_FLAG_INPUT;
1925 
1926 	if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1927 		info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
1928 
1929 	if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1930 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
1931 	if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1932 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
1933 
1934 	if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
1935 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
1936 	if (test_bit(FLAG_PULL_DOWN, &desc->flags))
1937 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
1938 	if (test_bit(FLAG_PULL_UP, &desc->flags))
1939 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
1940 
1941 	if (test_bit(FLAG_EDGE_RISING, &desc->flags))
1942 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
1943 	if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
1944 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
1945 
1946 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
1947 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
1948 
1949 	debounce_period_us = READ_ONCE(desc->debounce_period_us);
1950 	if (debounce_period_us) {
1951 		info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
1952 		info->attrs[num_attrs].debounce_period_us = debounce_period_us;
1953 		num_attrs++;
1954 	}
1955 	info->num_attrs = num_attrs;
1956 
1957 	spin_unlock_irqrestore(&gpio_lock, flags);
1958 }
1959 
1960 struct gpio_chardev_data {
1961 	struct gpio_device *gdev;
1962 	wait_queue_head_t wait;
1963 	DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
1964 	struct notifier_block lineinfo_changed_nb;
1965 	unsigned long *watched_lines;
1966 #ifdef CONFIG_GPIO_CDEV_V1
1967 	atomic_t watch_abi_version;
1968 #endif
1969 };
1970 
1971 #ifdef CONFIG_GPIO_CDEV_V1
1972 /*
1973  * returns 0 if the versions match, else the previously selected ABI version
1974  */
1975 static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
1976 				       unsigned int version)
1977 {
1978 	int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
1979 
1980 	if (abiv == version)
1981 		return 0;
1982 
1983 	return abiv;
1984 }
1985 #endif
1986 
1987 static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
1988 			bool watch)
1989 {
1990 	struct gpio_desc *desc;
1991 	struct gpio_v2_line_info lineinfo;
1992 
1993 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1994 		return -EFAULT;
1995 
1996 	if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
1997 		return -EINVAL;
1998 
1999 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2000 	if (IS_ERR(desc))
2001 		return PTR_ERR(desc);
2002 
2003 	if (watch) {
2004 #ifdef CONFIG_GPIO_CDEV_V1
2005 		if (lineinfo_ensure_abi_version(cdev, 2))
2006 			return -EPERM;
2007 #endif
2008 		if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2009 			return -EBUSY;
2010 	}
2011 	gpio_desc_to_lineinfo(desc, &lineinfo);
2012 
2013 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2014 		if (watch)
2015 			clear_bit(lineinfo.offset, cdev->watched_lines);
2016 		return -EFAULT;
2017 	}
2018 
2019 	return 0;
2020 }
2021 
2022 /*
2023  * gpio_ioctl() - ioctl handler for the GPIO chardev
2024  */
2025 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2026 {
2027 	struct gpio_chardev_data *cdev = file->private_data;
2028 	struct gpio_device *gdev = cdev->gdev;
2029 	struct gpio_chip *gc = gdev->chip;
2030 	void __user *ip = (void __user *)arg;
2031 	__u32 offset;
2032 
2033 	/* We fail any subsequent ioctl():s when the chip is gone */
2034 	if (!gc)
2035 		return -ENODEV;
2036 
2037 	/* Fill in the struct and pass to userspace */
2038 	if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
2039 		struct gpiochip_info chipinfo;
2040 
2041 		memset(&chipinfo, 0, sizeof(chipinfo));
2042 
2043 		strscpy(chipinfo.name, dev_name(&gdev->dev),
2044 			sizeof(chipinfo.name));
2045 		strscpy(chipinfo.label, gdev->label,
2046 			sizeof(chipinfo.label));
2047 		chipinfo.lines = gdev->ngpio;
2048 		if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
2049 			return -EFAULT;
2050 		return 0;
2051 #ifdef CONFIG_GPIO_CDEV_V1
2052 	} else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
2053 		struct gpio_desc *desc;
2054 		struct gpioline_info lineinfo;
2055 		struct gpio_v2_line_info lineinfo_v2;
2056 
2057 		if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2058 			return -EFAULT;
2059 
2060 		/* this doubles as a range check on line_offset */
2061 		desc = gpiochip_get_desc(gc, lineinfo.line_offset);
2062 		if (IS_ERR(desc))
2063 			return PTR_ERR(desc);
2064 
2065 		gpio_desc_to_lineinfo(desc, &lineinfo_v2);
2066 		gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
2067 
2068 		if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
2069 			return -EFAULT;
2070 		return 0;
2071 	} else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
2072 		return linehandle_create(gdev, ip);
2073 	} else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
2074 		return lineevent_create(gdev, ip);
2075 	} else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
2076 		struct gpio_desc *desc;
2077 		struct gpioline_info lineinfo;
2078 		struct gpio_v2_line_info lineinfo_v2;
2079 
2080 		if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2081 			return -EFAULT;
2082 
2083 		/* this doubles as a range check on line_offset */
2084 		desc = gpiochip_get_desc(gc, lineinfo.line_offset);
2085 		if (IS_ERR(desc))
2086 			return PTR_ERR(desc);
2087 
2088 		if (lineinfo_ensure_abi_version(cdev, 1))
2089 			return -EPERM;
2090 
2091 		if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
2092 			return -EBUSY;
2093 
2094 		gpio_desc_to_lineinfo(desc, &lineinfo_v2);
2095 		gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
2096 
2097 		if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2098 			clear_bit(lineinfo.line_offset, cdev->watched_lines);
2099 			return -EFAULT;
2100 		}
2101 
2102 		return 0;
2103 #endif /* CONFIG_GPIO_CDEV_V1 */
2104 	} else if (cmd == GPIO_V2_GET_LINEINFO_IOCTL ||
2105 		   cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL) {
2106 		return lineinfo_get(cdev, ip,
2107 				    cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL);
2108 	} else if (cmd == GPIO_V2_GET_LINE_IOCTL) {
2109 		return linereq_create(gdev, ip);
2110 	} else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) {
2111 		if (copy_from_user(&offset, ip, sizeof(offset)))
2112 			return -EFAULT;
2113 
2114 		if (offset >= cdev->gdev->ngpio)
2115 			return -EINVAL;
2116 
2117 		if (!test_and_clear_bit(offset, cdev->watched_lines))
2118 			return -EBUSY;
2119 
2120 		return 0;
2121 	}
2122 	return -EINVAL;
2123 }
2124 
2125 #ifdef CONFIG_COMPAT
2126 static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2127 			      unsigned long arg)
2128 {
2129 	return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2130 }
2131 #endif
2132 
2133 static struct gpio_chardev_data *
2134 to_gpio_chardev_data(struct notifier_block *nb)
2135 {
2136 	return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2137 }
2138 
2139 static int lineinfo_changed_notify(struct notifier_block *nb,
2140 				   unsigned long action, void *data)
2141 {
2142 	struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
2143 	struct gpio_v2_line_info_changed chg;
2144 	struct gpio_desc *desc = data;
2145 	int ret;
2146 
2147 	if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2148 		return NOTIFY_DONE;
2149 
2150 	memset(&chg, 0, sizeof(chg));
2151 	chg.event_type = action;
2152 	chg.timestamp_ns = ktime_get_ns();
2153 	gpio_desc_to_lineinfo(desc, &chg.info);
2154 
2155 	ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2156 	if (ret)
2157 		wake_up_poll(&cdev->wait, EPOLLIN);
2158 	else
2159 		pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2160 
2161 	return NOTIFY_OK;
2162 }
2163 
2164 static __poll_t lineinfo_watch_poll(struct file *file,
2165 				    struct poll_table_struct *pollt)
2166 {
2167 	struct gpio_chardev_data *cdev = file->private_data;
2168 	__poll_t events = 0;
2169 
2170 	poll_wait(file, &cdev->wait, pollt);
2171 
2172 	if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2173 						 &cdev->wait.lock))
2174 		events = EPOLLIN | EPOLLRDNORM;
2175 
2176 	return events;
2177 }
2178 
2179 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2180 				   size_t count, loff_t *off)
2181 {
2182 	struct gpio_chardev_data *cdev = file->private_data;
2183 	struct gpio_v2_line_info_changed event;
2184 	ssize_t bytes_read = 0;
2185 	int ret;
2186 	size_t event_size;
2187 
2188 #ifndef CONFIG_GPIO_CDEV_V1
2189 	event_size = sizeof(struct gpio_v2_line_info_changed);
2190 	if (count < event_size)
2191 		return -EINVAL;
2192 #endif
2193 
2194 	do {
2195 		spin_lock(&cdev->wait.lock);
2196 		if (kfifo_is_empty(&cdev->events)) {
2197 			if (bytes_read) {
2198 				spin_unlock(&cdev->wait.lock);
2199 				return bytes_read;
2200 			}
2201 
2202 			if (file->f_flags & O_NONBLOCK) {
2203 				spin_unlock(&cdev->wait.lock);
2204 				return -EAGAIN;
2205 			}
2206 
2207 			ret = wait_event_interruptible_locked(cdev->wait,
2208 					!kfifo_is_empty(&cdev->events));
2209 			if (ret) {
2210 				spin_unlock(&cdev->wait.lock);
2211 				return ret;
2212 			}
2213 		}
2214 #ifdef CONFIG_GPIO_CDEV_V1
2215 		/* must be after kfifo check so watch_abi_version is set */
2216 		if (atomic_read(&cdev->watch_abi_version) == 2)
2217 			event_size = sizeof(struct gpio_v2_line_info_changed);
2218 		else
2219 			event_size = sizeof(struct gpioline_info_changed);
2220 		if (count < event_size) {
2221 			spin_unlock(&cdev->wait.lock);
2222 			return -EINVAL;
2223 		}
2224 #endif
2225 		ret = kfifo_out(&cdev->events, &event, 1);
2226 		spin_unlock(&cdev->wait.lock);
2227 		if (ret != 1) {
2228 			ret = -EIO;
2229 			break;
2230 			/* We should never get here. See lineevent_read(). */
2231 		}
2232 
2233 #ifdef CONFIG_GPIO_CDEV_V1
2234 		if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2235 			if (copy_to_user(buf + bytes_read, &event, event_size))
2236 				return -EFAULT;
2237 		} else {
2238 			struct gpioline_info_changed event_v1;
2239 
2240 			gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2241 			if (copy_to_user(buf + bytes_read, &event_v1,
2242 					 event_size))
2243 				return -EFAULT;
2244 		}
2245 #else
2246 		if (copy_to_user(buf + bytes_read, &event, event_size))
2247 			return -EFAULT;
2248 #endif
2249 		bytes_read += event_size;
2250 	} while (count >= bytes_read + sizeof(event));
2251 
2252 	return bytes_read;
2253 }
2254 
2255 /**
2256  * gpio_chrdev_open() - open the chardev for ioctl operations
2257  * @inode: inode for this chardev
2258  * @file: file struct for storing private data
2259  * Returns 0 on success
2260  */
2261 static int gpio_chrdev_open(struct inode *inode, struct file *file)
2262 {
2263 	struct gpio_device *gdev = container_of(inode->i_cdev,
2264 						struct gpio_device, chrdev);
2265 	struct gpio_chardev_data *cdev;
2266 	int ret = -ENOMEM;
2267 
2268 	/* Fail on open if the backing gpiochip is gone */
2269 	if (!gdev->chip)
2270 		return -ENODEV;
2271 
2272 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2273 	if (!cdev)
2274 		return -ENOMEM;
2275 
2276 	cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2277 	if (!cdev->watched_lines)
2278 		goto out_free_cdev;
2279 
2280 	init_waitqueue_head(&cdev->wait);
2281 	INIT_KFIFO(cdev->events);
2282 	cdev->gdev = gdev;
2283 
2284 	cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
2285 	ret = blocking_notifier_chain_register(&gdev->notifier,
2286 					       &cdev->lineinfo_changed_nb);
2287 	if (ret)
2288 		goto out_free_bitmap;
2289 
2290 	get_device(&gdev->dev);
2291 	file->private_data = cdev;
2292 
2293 	ret = nonseekable_open(inode, file);
2294 	if (ret)
2295 		goto out_unregister_notifier;
2296 
2297 	return ret;
2298 
2299 out_unregister_notifier:
2300 	blocking_notifier_chain_unregister(&gdev->notifier,
2301 					   &cdev->lineinfo_changed_nb);
2302 out_free_bitmap:
2303 	bitmap_free(cdev->watched_lines);
2304 out_free_cdev:
2305 	kfree(cdev);
2306 	return ret;
2307 }
2308 
2309 /**
2310  * gpio_chrdev_release() - close chardev after ioctl operations
2311  * @inode: inode for this chardev
2312  * @file: file struct for storing private data
2313  * Returns 0 on success
2314  */
2315 static int gpio_chrdev_release(struct inode *inode, struct file *file)
2316 {
2317 	struct gpio_chardev_data *cdev = file->private_data;
2318 	struct gpio_device *gdev = cdev->gdev;
2319 
2320 	bitmap_free(cdev->watched_lines);
2321 	blocking_notifier_chain_unregister(&gdev->notifier,
2322 					   &cdev->lineinfo_changed_nb);
2323 	put_device(&gdev->dev);
2324 	kfree(cdev);
2325 
2326 	return 0;
2327 }
2328 
2329 static const struct file_operations gpio_fileops = {
2330 	.release = gpio_chrdev_release,
2331 	.open = gpio_chrdev_open,
2332 	.poll = lineinfo_watch_poll,
2333 	.read = lineinfo_watch_read,
2334 	.owner = THIS_MODULE,
2335 	.llseek = no_llseek,
2336 	.unlocked_ioctl = gpio_ioctl,
2337 #ifdef CONFIG_COMPAT
2338 	.compat_ioctl = gpio_ioctl_compat,
2339 #endif
2340 };
2341 
2342 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2343 {
2344 	int ret;
2345 
2346 	cdev_init(&gdev->chrdev, &gpio_fileops);
2347 	gdev->chrdev.owner = THIS_MODULE;
2348 	gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2349 
2350 	ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2351 	if (ret)
2352 		return ret;
2353 
2354 	chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2355 		 MAJOR(devt), gdev->id);
2356 
2357 	return 0;
2358 }
2359 
2360 void gpiolib_cdev_unregister(struct gpio_device *gdev)
2361 {
2362 	cdev_device_del(&gdev->chrdev, &gdev->dev);
2363 }
2364