xref: /openbmc/linux/drivers/gpio/gpiolib-cdev.c (revision 67ccb6b0)
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/cleanup.h>
9 #include <linux/compat.h>
10 #include <linux/compiler.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/file.h>
14 #include <linux/gpio.h>
15 #include <linux/gpio/driver.h>
16 #include <linux/hte.h>
17 #include <linux/interrupt.h>
18 #include <linux/irqreturn.h>
19 #include <linux/kernel.h>
20 #include <linux/kfifo.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/pinctrl/consumer.h>
24 #include <linux/poll.h>
25 #include <linux/rbtree.h>
26 #include <linux/seq_file.h>
27 #include <linux/spinlock.h>
28 #include <linux/timekeeping.h>
29 #include <linux/uaccess.h>
30 #include <linux/workqueue.h>
31 
32 #include <uapi/linux/gpio.h>
33 
34 #include "gpiolib.h"
35 #include "gpiolib-cdev.h"
36 
37 /*
38  * Array sizes must ensure 64-bit alignment and not create holes in the
39  * struct packing.
40  */
41 static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2));
42 static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8));
43 
44 /*
45  * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility
46  */
47 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8));
48 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8));
49 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8));
50 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8));
51 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8));
52 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8));
53 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8));
54 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8));
55 
56 /* Character device interface to GPIO.
57  *
58  * The GPIO character device, /dev/gpiochipN, provides userspace an
59  * interface to gpiolib GPIOs via ioctl()s.
60  */
61 
62 typedef __poll_t (*poll_fn)(struct file *, struct poll_table_struct *);
63 typedef long (*ioctl_fn)(struct file *, unsigned int, unsigned long);
64 typedef ssize_t (*read_fn)(struct file *, char __user *,
65 			   size_t count, loff_t *);
66 
67 static __poll_t call_poll_locked(struct file *file,
68 				 struct poll_table_struct *wait,
69 				 struct gpio_device *gdev, poll_fn func)
70 {
71 	__poll_t ret;
72 
73 	down_read(&gdev->sem);
74 	ret = func(file, wait);
75 	up_read(&gdev->sem);
76 
77 	return ret;
78 }
79 
80 static long call_ioctl_locked(struct file *file, unsigned int cmd,
81 			      unsigned long arg, struct gpio_device *gdev,
82 			      ioctl_fn func)
83 {
84 	long ret;
85 
86 	down_read(&gdev->sem);
87 	ret = func(file, cmd, arg);
88 	up_read(&gdev->sem);
89 
90 	return ret;
91 }
92 
93 static ssize_t call_read_locked(struct file *file, char __user *buf,
94 				size_t count, loff_t *f_ps,
95 				struct gpio_device *gdev, read_fn func)
96 {
97 	ssize_t ret;
98 
99 	down_read(&gdev->sem);
100 	ret = func(file, buf, count, f_ps);
101 	up_read(&gdev->sem);
102 
103 	return ret;
104 }
105 
106 /*
107  * GPIO line handle management
108  */
109 
110 #ifdef CONFIG_GPIO_CDEV_V1
111 /**
112  * struct linehandle_state - contains the state of a userspace handle
113  * @gdev: the GPIO device the handle pertains to
114  * @label: consumer label used to tag descriptors
115  * @descs: the GPIO descriptors held by this handle
116  * @num_descs: the number of descriptors held in the descs array
117  */
118 struct linehandle_state {
119 	struct gpio_device *gdev;
120 	const char *label;
121 	struct gpio_desc *descs[GPIOHANDLES_MAX];
122 	u32 num_descs;
123 };
124 
125 #define GPIOHANDLE_REQUEST_VALID_FLAGS \
126 	(GPIOHANDLE_REQUEST_INPUT | \
127 	GPIOHANDLE_REQUEST_OUTPUT | \
128 	GPIOHANDLE_REQUEST_ACTIVE_LOW | \
129 	GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
130 	GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
131 	GPIOHANDLE_REQUEST_BIAS_DISABLE | \
132 	GPIOHANDLE_REQUEST_OPEN_DRAIN | \
133 	GPIOHANDLE_REQUEST_OPEN_SOURCE)
134 
135 #define GPIOHANDLE_REQUEST_DIRECTION_FLAGS \
136 	(GPIOHANDLE_REQUEST_INPUT | \
137 	 GPIOHANDLE_REQUEST_OUTPUT)
138 
139 static int linehandle_validate_flags(u32 flags)
140 {
141 	/* Return an error if an unknown flag is set */
142 	if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
143 		return -EINVAL;
144 
145 	/*
146 	 * Do not allow both INPUT & OUTPUT flags to be set as they are
147 	 * contradictory.
148 	 */
149 	if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
150 	    (flags & GPIOHANDLE_REQUEST_OUTPUT))
151 		return -EINVAL;
152 
153 	/*
154 	 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
155 	 * the hardware actually supports enabling both at the same time the
156 	 * electrical result would be disastrous.
157 	 */
158 	if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
159 	    (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
160 		return -EINVAL;
161 
162 	/* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
163 	if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
164 	    ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
165 	     (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
166 		return -EINVAL;
167 
168 	/* Bias flags only allowed for input or output mode. */
169 	if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
170 	      (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
171 	    ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
172 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
173 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
174 		return -EINVAL;
175 
176 	/* Only one bias flag can be set. */
177 	if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
178 	     (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
179 		       GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
180 	    ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
181 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
182 		return -EINVAL;
183 
184 	return 0;
185 }
186 
187 static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
188 {
189 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
190 		   lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
191 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
192 		   lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
193 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
194 		   lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
195 	assign_bit(FLAG_PULL_UP, flagsp,
196 		   lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
197 	assign_bit(FLAG_PULL_DOWN, flagsp,
198 		   lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
199 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
200 		   lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
201 }
202 
203 static long linehandle_set_config(struct linehandle_state *lh,
204 				  void __user *ip)
205 {
206 	struct gpiohandle_config gcnf;
207 	struct gpio_desc *desc;
208 	int i, ret;
209 	u32 lflags;
210 
211 	if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
212 		return -EFAULT;
213 
214 	lflags = gcnf.flags;
215 	ret = linehandle_validate_flags(lflags);
216 	if (ret)
217 		return ret;
218 
219 	/* Lines must be reconfigured explicitly as input or output. */
220 	if (!(lflags & GPIOHANDLE_REQUEST_DIRECTION_FLAGS))
221 		return -EINVAL;
222 
223 	for (i = 0; i < lh->num_descs; i++) {
224 		desc = lh->descs[i];
225 		linehandle_flags_to_desc_flags(lflags, &desc->flags);
226 
227 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
228 			int val = !!gcnf.default_values[i];
229 
230 			ret = gpiod_direction_output(desc, val);
231 			if (ret)
232 				return ret;
233 		} else {
234 			ret = gpiod_direction_input(desc);
235 			if (ret)
236 				return ret;
237 		}
238 
239 		gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
240 	}
241 	return 0;
242 }
243 
244 static long linehandle_ioctl_unlocked(struct file *file, unsigned int cmd,
245 				      unsigned long arg)
246 {
247 	struct linehandle_state *lh = file->private_data;
248 	void __user *ip = (void __user *)arg;
249 	struct gpiohandle_data ghd;
250 	DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
251 	unsigned int i;
252 	int ret;
253 
254 	if (!lh->gdev->chip)
255 		return -ENODEV;
256 
257 	switch (cmd) {
258 	case GPIOHANDLE_GET_LINE_VALUES_IOCTL:
259 		/* NOTE: It's okay to read values of output lines */
260 		ret = gpiod_get_array_value_complex(false, true,
261 						    lh->num_descs, lh->descs,
262 						    NULL, vals);
263 		if (ret)
264 			return ret;
265 
266 		memset(&ghd, 0, sizeof(ghd));
267 		for (i = 0; i < lh->num_descs; i++)
268 			ghd.values[i] = test_bit(i, vals);
269 
270 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
271 			return -EFAULT;
272 
273 		return 0;
274 	case GPIOHANDLE_SET_LINE_VALUES_IOCTL:
275 		/*
276 		 * All line descriptors were created at once with the same
277 		 * flags so just check if the first one is really output.
278 		 */
279 		if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
280 			return -EPERM;
281 
282 		if (copy_from_user(&ghd, ip, sizeof(ghd)))
283 			return -EFAULT;
284 
285 		/* Clamp all values to [0,1] */
286 		for (i = 0; i < lh->num_descs; i++)
287 			__assign_bit(i, vals, ghd.values[i]);
288 
289 		/* Reuse the array setting function */
290 		return gpiod_set_array_value_complex(false,
291 						     true,
292 						     lh->num_descs,
293 						     lh->descs,
294 						     NULL,
295 						     vals);
296 	case GPIOHANDLE_SET_CONFIG_IOCTL:
297 		return linehandle_set_config(lh, ip);
298 	default:
299 		return -EINVAL;
300 	}
301 }
302 
303 static long linehandle_ioctl(struct file *file, unsigned int cmd,
304 			     unsigned long arg)
305 {
306 	struct linehandle_state *lh = file->private_data;
307 
308 	return call_ioctl_locked(file, cmd, arg, lh->gdev,
309 				 linehandle_ioctl_unlocked);
310 }
311 
312 #ifdef CONFIG_COMPAT
313 static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
314 				    unsigned long arg)
315 {
316 	return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
317 }
318 #endif
319 
320 static void linehandle_free(struct linehandle_state *lh)
321 {
322 	int i;
323 
324 	for (i = 0; i < lh->num_descs; i++)
325 		if (lh->descs[i])
326 			gpiod_free(lh->descs[i]);
327 	kfree(lh->label);
328 	gpio_device_put(lh->gdev);
329 	kfree(lh);
330 }
331 
332 static int linehandle_release(struct inode *inode, struct file *file)
333 {
334 	linehandle_free(file->private_data);
335 	return 0;
336 }
337 
338 static const struct file_operations linehandle_fileops = {
339 	.release = linehandle_release,
340 	.owner = THIS_MODULE,
341 	.llseek = noop_llseek,
342 	.unlocked_ioctl = linehandle_ioctl,
343 #ifdef CONFIG_COMPAT
344 	.compat_ioctl = linehandle_ioctl_compat,
345 #endif
346 };
347 
348 static int linehandle_create(struct gpio_device *gdev, void __user *ip)
349 {
350 	struct gpiohandle_request handlereq;
351 	struct linehandle_state *lh;
352 	struct file *file;
353 	int fd, i, ret;
354 	u32 lflags;
355 
356 	if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
357 		return -EFAULT;
358 	if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
359 		return -EINVAL;
360 
361 	lflags = handlereq.flags;
362 
363 	ret = linehandle_validate_flags(lflags);
364 	if (ret)
365 		return ret;
366 
367 	lh = kzalloc(sizeof(*lh), GFP_KERNEL);
368 	if (!lh)
369 		return -ENOMEM;
370 	lh->gdev = gpio_device_get(gdev);
371 
372 	if (handlereq.consumer_label[0] != '\0') {
373 		/* label is only initialized if consumer_label is set */
374 		lh->label = kstrndup(handlereq.consumer_label,
375 				     sizeof(handlereq.consumer_label) - 1,
376 				     GFP_KERNEL);
377 		if (!lh->label) {
378 			ret = -ENOMEM;
379 			goto out_free_lh;
380 		}
381 	}
382 
383 	lh->num_descs = handlereq.lines;
384 
385 	/* Request each GPIO */
386 	for (i = 0; i < handlereq.lines; i++) {
387 		u32 offset = handlereq.lineoffsets[i];
388 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
389 
390 		if (IS_ERR(desc)) {
391 			ret = PTR_ERR(desc);
392 			goto out_free_lh;
393 		}
394 
395 		ret = gpiod_request_user(desc, lh->label);
396 		if (ret)
397 			goto out_free_lh;
398 		lh->descs[i] = desc;
399 		linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
400 
401 		ret = gpiod_set_transitory(desc, false);
402 		if (ret < 0)
403 			goto out_free_lh;
404 
405 		/*
406 		 * Lines have to be requested explicitly for input
407 		 * or output, else the line will be treated "as is".
408 		 */
409 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
410 			int val = !!handlereq.default_values[i];
411 
412 			ret = gpiod_direction_output(desc, val);
413 			if (ret)
414 				goto out_free_lh;
415 		} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
416 			ret = gpiod_direction_input(desc);
417 			if (ret)
418 				goto out_free_lh;
419 		}
420 
421 		gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
422 
423 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
424 			offset);
425 	}
426 
427 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
428 	if (fd < 0) {
429 		ret = fd;
430 		goto out_free_lh;
431 	}
432 
433 	file = anon_inode_getfile("gpio-linehandle",
434 				  &linehandle_fileops,
435 				  lh,
436 				  O_RDONLY | O_CLOEXEC);
437 	if (IS_ERR(file)) {
438 		ret = PTR_ERR(file);
439 		goto out_put_unused_fd;
440 	}
441 
442 	handlereq.fd = fd;
443 	if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
444 		/*
445 		 * fput() will trigger the release() callback, so do not go onto
446 		 * the regular error cleanup path here.
447 		 */
448 		fput(file);
449 		put_unused_fd(fd);
450 		return -EFAULT;
451 	}
452 
453 	fd_install(fd, file);
454 
455 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
456 		lh->num_descs);
457 
458 	return 0;
459 
460 out_put_unused_fd:
461 	put_unused_fd(fd);
462 out_free_lh:
463 	linehandle_free(lh);
464 	return ret;
465 }
466 #endif /* CONFIG_GPIO_CDEV_V1 */
467 
468 /**
469  * struct line - contains the state of a requested line
470  * @node: to store the object in supinfo_tree if supplemental
471  * @desc: the GPIO descriptor for this line.
472  * @req: the corresponding line request
473  * @irq: the interrupt triggered in response to events on this GPIO
474  * @edflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or
475  * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied
476  * @timestamp_ns: cache for the timestamp storing it between hardirq and
477  * IRQ thread, used to bring the timestamp close to the actual event
478  * @req_seqno: the seqno for the current edge event in the sequence of
479  * events for the corresponding line request. This is drawn from the @req.
480  * @line_seqno: the seqno for the current edge event in the sequence of
481  * events for this line.
482  * @work: the worker that implements software debouncing
483  * @debounce_period_us: the debounce period in microseconds
484  * @sw_debounced: flag indicating if the software debouncer is active
485  * @level: the current debounced physical level of the line
486  * @hdesc: the Hardware Timestamp Engine (HTE) descriptor
487  * @raw_level: the line level at the time of event
488  * @total_discard_seq: the running counter of the discarded events
489  * @last_seqno: the last sequence number before debounce period expires
490  */
491 struct line {
492 	struct rb_node node;
493 	struct gpio_desc *desc;
494 	/*
495 	 * -- edge detector specific fields --
496 	 */
497 	struct linereq *req;
498 	unsigned int irq;
499 	/*
500 	 * The flags for the active edge detector configuration.
501 	 *
502 	 * edflags is set by linereq_create(), linereq_free(), and
503 	 * linereq_set_config_unlocked(), which are themselves mutually
504 	 * exclusive, and is accessed by edge_irq_thread(),
505 	 * process_hw_ts_thread() and debounce_work_func(),
506 	 * which can all live with a slightly stale value.
507 	 */
508 	u64 edflags;
509 	/*
510 	 * timestamp_ns and req_seqno are accessed only by
511 	 * edge_irq_handler() and edge_irq_thread(), which are themselves
512 	 * mutually exclusive, so no additional protection is necessary.
513 	 */
514 	u64 timestamp_ns;
515 	u32 req_seqno;
516 	/*
517 	 * line_seqno is accessed by either edge_irq_thread() or
518 	 * debounce_work_func(), which are themselves mutually exclusive,
519 	 * so no additional protection is necessary.
520 	 */
521 	u32 line_seqno;
522 	/*
523 	 * -- debouncer specific fields --
524 	 */
525 	struct delayed_work work;
526 	/*
527 	 * debounce_period_us is accessed by debounce_irq_handler() and
528 	 * process_hw_ts() which are disabled when modified by
529 	 * debounce_setup(), edge_detector_setup() or edge_detector_stop()
530 	 * or can live with a stale version when updated by
531 	 * edge_detector_update().
532 	 * The modifying functions are themselves mutually exclusive.
533 	 */
534 	unsigned int debounce_period_us;
535 	/*
536 	 * sw_debounce is accessed by linereq_set_config(), which is the
537 	 * only setter, and linereq_get_values(), which can live with a
538 	 * slightly stale value.
539 	 */
540 	unsigned int sw_debounced;
541 	/*
542 	 * level is accessed by debounce_work_func(), which is the only
543 	 * setter, and linereq_get_values() which can live with a slightly
544 	 * stale value.
545 	 */
546 	unsigned int level;
547 #ifdef CONFIG_HTE
548 	struct hte_ts_desc hdesc;
549 	/*
550 	 * HTE provider sets line level at the time of event. The valid
551 	 * value is 0 or 1 and negative value for an error.
552 	 */
553 	int raw_level;
554 	/*
555 	 * when sw_debounce is set on HTE enabled line, this is running
556 	 * counter of the discarded events.
557 	 */
558 	u32 total_discard_seq;
559 	/*
560 	 * when sw_debounce is set on HTE enabled line, this variable records
561 	 * last sequence number before debounce period expires.
562 	 */
563 	u32 last_seqno;
564 #endif /* CONFIG_HTE */
565 };
566 
567 /*
568  * a rbtree of the struct lines containing supplemental info.
569  * Used to populate gpio_v2_line_info with cdev specific fields not contained
570  * in the struct gpio_desc.
571  * A line is determined to contain supplemental information by
572  * line_has_supinfo().
573  */
574 static struct rb_root supinfo_tree = RB_ROOT;
575 /* covers supinfo_tree */
576 static DEFINE_SPINLOCK(supinfo_lock);
577 
578 /**
579  * struct linereq - contains the state of a userspace line request
580  * @gdev: the GPIO device the line request pertains to
581  * @label: consumer label used to tag GPIO descriptors
582  * @num_lines: the number of lines in the lines array
583  * @wait: wait queue that handles blocking reads of events
584  * @device_unregistered_nb: notifier block for receiving gdev unregister events
585  * @event_buffer_size: the number of elements allocated in @events
586  * @events: KFIFO for the GPIO events
587  * @seqno: the sequence number for edge events generated on all lines in
588  * this line request.  Note that this is not used when @num_lines is 1, as
589  * the line_seqno is then the same and is cheaper to calculate.
590  * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
591  * of configuration, particularly multi-step accesses to desc flags and
592  * changes to supinfo status.
593  * @lines: the lines held by this line request, with @num_lines elements.
594  */
595 struct linereq {
596 	struct gpio_device *gdev;
597 	const char *label;
598 	u32 num_lines;
599 	wait_queue_head_t wait;
600 	struct notifier_block device_unregistered_nb;
601 	u32 event_buffer_size;
602 	DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
603 	atomic_t seqno;
604 	struct mutex config_mutex;
605 	struct line lines[];
606 };
607 
608 static void supinfo_insert(struct line *line)
609 {
610 	struct rb_node **new = &(supinfo_tree.rb_node), *parent = NULL;
611 	struct line *entry;
612 
613 	guard(spinlock)(&supinfo_lock);
614 
615 	while (*new) {
616 		entry = container_of(*new, struct line, node);
617 
618 		parent = *new;
619 		if (line->desc < entry->desc) {
620 			new = &((*new)->rb_left);
621 		} else if (line->desc > entry->desc) {
622 			new = &((*new)->rb_right);
623 		} else {
624 			/* this should never happen */
625 			WARN(1, "duplicate line inserted");
626 			return;
627 		}
628 	}
629 
630 	rb_link_node(&line->node, parent, new);
631 	rb_insert_color(&line->node, &supinfo_tree);
632 }
633 
634 static void supinfo_erase(struct line *line)
635 {
636 	guard(spinlock)(&supinfo_lock);
637 
638 	rb_erase(&line->node, &supinfo_tree);
639 }
640 
641 static struct line *supinfo_find(struct gpio_desc *desc)
642 {
643 	struct rb_node *node = supinfo_tree.rb_node;
644 	struct line *line;
645 
646 	while (node) {
647 		line = container_of(node, struct line, node);
648 		if (desc < line->desc)
649 			node = node->rb_left;
650 		else if (desc > line->desc)
651 			node = node->rb_right;
652 		else
653 			return line;
654 	}
655 	return NULL;
656 }
657 
658 static void supinfo_to_lineinfo(struct gpio_desc *desc,
659 				struct gpio_v2_line_info *info)
660 {
661 	struct gpio_v2_line_attribute *attr;
662 	struct line *line;
663 
664 	guard(spinlock)(&supinfo_lock);
665 
666 	line = supinfo_find(desc);
667 	if (!line)
668 		return;
669 
670 	attr = &info->attrs[info->num_attrs];
671 	attr->id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
672 	attr->debounce_period_us = READ_ONCE(line->debounce_period_us);
673 	info->num_attrs++;
674 }
675 
676 static inline bool line_has_supinfo(struct line *line)
677 {
678 	return READ_ONCE(line->debounce_period_us);
679 }
680 
681 /*
682  * Checks line_has_supinfo() before and after the change to avoid unnecessary
683  * supinfo_tree access.
684  * Called indirectly by linereq_create() or linereq_set_config() so line
685  * is already protected from concurrent changes.
686  */
687 static void line_set_debounce_period(struct line *line,
688 				     unsigned int debounce_period_us)
689 {
690 	bool was_suppl = line_has_supinfo(line);
691 
692 	WRITE_ONCE(line->debounce_period_us, debounce_period_us);
693 
694 	/* if supinfo status is unchanged then we're done */
695 	if (line_has_supinfo(line) == was_suppl)
696 		return;
697 
698 	/* supinfo status has changed, so update the tree */
699 	if (was_suppl)
700 		supinfo_erase(line);
701 	else
702 		supinfo_insert(line);
703 }
704 
705 #define GPIO_V2_LINE_BIAS_FLAGS \
706 	(GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
707 	 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
708 	 GPIO_V2_LINE_FLAG_BIAS_DISABLED)
709 
710 #define GPIO_V2_LINE_DIRECTION_FLAGS \
711 	(GPIO_V2_LINE_FLAG_INPUT | \
712 	 GPIO_V2_LINE_FLAG_OUTPUT)
713 
714 #define GPIO_V2_LINE_DRIVE_FLAGS \
715 	(GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
716 	 GPIO_V2_LINE_FLAG_OPEN_SOURCE)
717 
718 #define GPIO_V2_LINE_EDGE_FLAGS \
719 	(GPIO_V2_LINE_FLAG_EDGE_RISING | \
720 	 GPIO_V2_LINE_FLAG_EDGE_FALLING)
721 
722 #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS
723 
724 #define GPIO_V2_LINE_VALID_FLAGS \
725 	(GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
726 	 GPIO_V2_LINE_DIRECTION_FLAGS | \
727 	 GPIO_V2_LINE_DRIVE_FLAGS | \
728 	 GPIO_V2_LINE_EDGE_FLAGS | \
729 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
730 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
731 	 GPIO_V2_LINE_BIAS_FLAGS)
732 
733 /* subset of flags relevant for edge detector configuration */
734 #define GPIO_V2_LINE_EDGE_DETECTOR_FLAGS \
735 	(GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
736 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
737 	 GPIO_V2_LINE_EDGE_FLAGS)
738 
739 static int linereq_unregistered_notify(struct notifier_block *nb,
740 				       unsigned long action, void *data)
741 {
742 	struct linereq *lr = container_of(nb, struct linereq,
743 					  device_unregistered_nb);
744 
745 	wake_up_poll(&lr->wait, EPOLLIN | EPOLLERR);
746 
747 	return NOTIFY_OK;
748 }
749 
750 static void linereq_put_event(struct linereq *lr,
751 			      struct gpio_v2_line_event *le)
752 {
753 	bool overflow = false;
754 
755 	spin_lock(&lr->wait.lock);
756 	if (kfifo_is_full(&lr->events)) {
757 		overflow = true;
758 		kfifo_skip(&lr->events);
759 	}
760 	kfifo_in(&lr->events, le, 1);
761 	spin_unlock(&lr->wait.lock);
762 	if (!overflow)
763 		wake_up_poll(&lr->wait, EPOLLIN);
764 	else
765 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
766 }
767 
768 static u64 line_event_timestamp(struct line *line)
769 {
770 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
771 		return ktime_get_real_ns();
772 	else if (IS_ENABLED(CONFIG_HTE) &&
773 		 test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))
774 		return line->timestamp_ns;
775 
776 	return ktime_get_ns();
777 }
778 
779 static u32 line_event_id(int level)
780 {
781 	return level ? GPIO_V2_LINE_EVENT_RISING_EDGE :
782 		       GPIO_V2_LINE_EVENT_FALLING_EDGE;
783 }
784 
785 static inline char *make_irq_label(const char *orig)
786 {
787 	char *new;
788 
789 	if (!orig)
790 		return NULL;
791 
792 	new = kstrdup_and_replace(orig, '/', ':', GFP_KERNEL);
793 	if (!new)
794 		return ERR_PTR(-ENOMEM);
795 
796 	return new;
797 }
798 
799 static inline void free_irq_label(const char *label)
800 {
801 	kfree(label);
802 }
803 
804 #ifdef CONFIG_HTE
805 
806 static enum hte_return process_hw_ts_thread(void *p)
807 {
808 	struct line *line;
809 	struct linereq *lr;
810 	struct gpio_v2_line_event le;
811 	u64 edflags;
812 	int level;
813 
814 	if (!p)
815 		return HTE_CB_HANDLED;
816 
817 	line = p;
818 	lr = line->req;
819 
820 	memset(&le, 0, sizeof(le));
821 
822 	le.timestamp_ns = line->timestamp_ns;
823 	edflags = READ_ONCE(line->edflags);
824 
825 	switch (edflags & GPIO_V2_LINE_EDGE_FLAGS) {
826 	case GPIO_V2_LINE_FLAG_EDGE_BOTH:
827 		level = (line->raw_level >= 0) ?
828 				line->raw_level :
829 				gpiod_get_raw_value_cansleep(line->desc);
830 
831 		if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
832 			level = !level;
833 
834 		le.id = line_event_id(level);
835 		break;
836 	case GPIO_V2_LINE_FLAG_EDGE_RISING:
837 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
838 		break;
839 	case GPIO_V2_LINE_FLAG_EDGE_FALLING:
840 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
841 		break;
842 	default:
843 		return HTE_CB_HANDLED;
844 	}
845 	le.line_seqno = line->line_seqno;
846 	le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
847 	le.offset = gpio_chip_hwgpio(line->desc);
848 
849 	linereq_put_event(lr, &le);
850 
851 	return HTE_CB_HANDLED;
852 }
853 
854 static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p)
855 {
856 	struct line *line;
857 	struct linereq *lr;
858 	int diff_seqno = 0;
859 
860 	if (!ts || !p)
861 		return HTE_CB_HANDLED;
862 
863 	line = p;
864 	line->timestamp_ns = ts->tsc;
865 	line->raw_level = ts->raw_level;
866 	lr = line->req;
867 
868 	if (READ_ONCE(line->sw_debounced)) {
869 		line->total_discard_seq++;
870 		line->last_seqno = ts->seq;
871 		mod_delayed_work(system_wq, &line->work,
872 		  usecs_to_jiffies(READ_ONCE(line->debounce_period_us)));
873 	} else {
874 		if (unlikely(ts->seq < line->line_seqno))
875 			return HTE_CB_HANDLED;
876 
877 		diff_seqno = ts->seq - line->line_seqno;
878 		line->line_seqno = ts->seq;
879 		if (lr->num_lines != 1)
880 			line->req_seqno = atomic_add_return(diff_seqno,
881 							    &lr->seqno);
882 
883 		return HTE_RUN_SECOND_CB;
884 	}
885 
886 	return HTE_CB_HANDLED;
887 }
888 
889 static int hte_edge_setup(struct line *line, u64 eflags)
890 {
891 	int ret;
892 	unsigned long flags = 0;
893 	struct hte_ts_desc *hdesc = &line->hdesc;
894 
895 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
896 		flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
897 				 HTE_FALLING_EDGE_TS :
898 				 HTE_RISING_EDGE_TS;
899 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
900 		flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
901 				 HTE_RISING_EDGE_TS :
902 				 HTE_FALLING_EDGE_TS;
903 
904 	line->total_discard_seq = 0;
905 
906 	hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags, NULL,
907 			   line->desc);
908 
909 	ret = hte_ts_get(NULL, hdesc, 0);
910 	if (ret)
911 		return ret;
912 
913 	return hte_request_ts_ns(hdesc, process_hw_ts, process_hw_ts_thread,
914 				 line);
915 }
916 
917 #else
918 
919 static int hte_edge_setup(struct line *line, u64 eflags)
920 {
921 	return 0;
922 }
923 #endif /* CONFIG_HTE */
924 
925 static irqreturn_t edge_irq_thread(int irq, void *p)
926 {
927 	struct line *line = p;
928 	struct linereq *lr = line->req;
929 	struct gpio_v2_line_event le;
930 
931 	/* Do not leak kernel stack to userspace */
932 	memset(&le, 0, sizeof(le));
933 
934 	if (line->timestamp_ns) {
935 		le.timestamp_ns = line->timestamp_ns;
936 	} else {
937 		/*
938 		 * We may be running from a nested threaded interrupt in
939 		 * which case we didn't get the timestamp from
940 		 * edge_irq_handler().
941 		 */
942 		le.timestamp_ns = line_event_timestamp(line);
943 		if (lr->num_lines != 1)
944 			line->req_seqno = atomic_inc_return(&lr->seqno);
945 	}
946 	line->timestamp_ns = 0;
947 
948 	switch (READ_ONCE(line->edflags) & GPIO_V2_LINE_EDGE_FLAGS) {
949 	case GPIO_V2_LINE_FLAG_EDGE_BOTH:
950 		le.id = line_event_id(gpiod_get_value_cansleep(line->desc));
951 		break;
952 	case GPIO_V2_LINE_FLAG_EDGE_RISING:
953 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
954 		break;
955 	case GPIO_V2_LINE_FLAG_EDGE_FALLING:
956 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
957 		break;
958 	default:
959 		return IRQ_NONE;
960 	}
961 	line->line_seqno++;
962 	le.line_seqno = line->line_seqno;
963 	le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
964 	le.offset = gpio_chip_hwgpio(line->desc);
965 
966 	linereq_put_event(lr, &le);
967 
968 	return IRQ_HANDLED;
969 }
970 
971 static irqreturn_t edge_irq_handler(int irq, void *p)
972 {
973 	struct line *line = p;
974 	struct linereq *lr = line->req;
975 
976 	/*
977 	 * Just store the timestamp in hardirq context so we get it as
978 	 * close in time as possible to the actual event.
979 	 */
980 	line->timestamp_ns = line_event_timestamp(line);
981 
982 	if (lr->num_lines != 1)
983 		line->req_seqno = atomic_inc_return(&lr->seqno);
984 
985 	return IRQ_WAKE_THREAD;
986 }
987 
988 /*
989  * returns the current debounced logical value.
990  */
991 static bool debounced_value(struct line *line)
992 {
993 	bool value;
994 
995 	/*
996 	 * minor race - debouncer may be stopped here, so edge_detector_stop()
997 	 * must leave the value unchanged so the following will read the level
998 	 * from when the debouncer was last running.
999 	 */
1000 	value = READ_ONCE(line->level);
1001 
1002 	if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
1003 		value = !value;
1004 
1005 	return value;
1006 }
1007 
1008 static irqreturn_t debounce_irq_handler(int irq, void *p)
1009 {
1010 	struct line *line = p;
1011 
1012 	mod_delayed_work(system_wq, &line->work,
1013 		usecs_to_jiffies(READ_ONCE(line->debounce_period_us)));
1014 
1015 	return IRQ_HANDLED;
1016 }
1017 
1018 static void debounce_work_func(struct work_struct *work)
1019 {
1020 	struct gpio_v2_line_event le;
1021 	struct line *line = container_of(work, struct line, work.work);
1022 	struct linereq *lr;
1023 	u64 eflags, edflags = READ_ONCE(line->edflags);
1024 	int level = -1;
1025 #ifdef CONFIG_HTE
1026 	int diff_seqno;
1027 
1028 	if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
1029 		level = line->raw_level;
1030 #endif
1031 	if (level < 0)
1032 		level = gpiod_get_raw_value_cansleep(line->desc);
1033 	if (level < 0) {
1034 		pr_debug_ratelimited("debouncer failed to read line value\n");
1035 		return;
1036 	}
1037 
1038 	if (READ_ONCE(line->level) == level)
1039 		return;
1040 
1041 	WRITE_ONCE(line->level, level);
1042 
1043 	/* -- edge detection -- */
1044 	eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
1045 	if (!eflags)
1046 		return;
1047 
1048 	/* switch from physical level to logical - if they differ */
1049 	if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
1050 		level = !level;
1051 
1052 	/* ignore edges that are not being monitored */
1053 	if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
1054 	    ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
1055 		return;
1056 
1057 	/* Do not leak kernel stack to userspace */
1058 	memset(&le, 0, sizeof(le));
1059 
1060 	lr = line->req;
1061 	le.timestamp_ns = line_event_timestamp(line);
1062 	le.offset = gpio_chip_hwgpio(line->desc);
1063 #ifdef CONFIG_HTE
1064 	if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) {
1065 		/* discard events except the last one */
1066 		line->total_discard_seq -= 1;
1067 		diff_seqno = line->last_seqno - line->total_discard_seq -
1068 				line->line_seqno;
1069 		line->line_seqno = line->last_seqno - line->total_discard_seq;
1070 		le.line_seqno = line->line_seqno;
1071 		le.seqno = (lr->num_lines == 1) ?
1072 			le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno);
1073 	} else
1074 #endif /* CONFIG_HTE */
1075 	{
1076 		line->line_seqno++;
1077 		le.line_seqno = line->line_seqno;
1078 		le.seqno = (lr->num_lines == 1) ?
1079 			le.line_seqno : atomic_inc_return(&lr->seqno);
1080 	}
1081 
1082 	le.id = line_event_id(level);
1083 
1084 	linereq_put_event(lr, &le);
1085 }
1086 
1087 static int debounce_setup(struct line *line, unsigned int debounce_period_us)
1088 {
1089 	unsigned long irqflags;
1090 	int ret, level, irq;
1091 	char *label;
1092 
1093 	/* try hardware */
1094 	ret = gpiod_set_debounce(line->desc, debounce_period_us);
1095 	if (!ret) {
1096 		line_set_debounce_period(line, debounce_period_us);
1097 		return ret;
1098 	}
1099 	if (ret != -ENOTSUPP)
1100 		return ret;
1101 
1102 	if (debounce_period_us) {
1103 		/* setup software debounce */
1104 		level = gpiod_get_raw_value_cansleep(line->desc);
1105 		if (level < 0)
1106 			return level;
1107 
1108 		if (!(IS_ENABLED(CONFIG_HTE) &&
1109 		      test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))) {
1110 			irq = gpiod_to_irq(line->desc);
1111 			if (irq < 0)
1112 				return -ENXIO;
1113 
1114 			label = make_irq_label(line->req->label);
1115 			if (IS_ERR(label))
1116 				return -ENOMEM;
1117 
1118 			irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
1119 			ret = request_irq(irq, debounce_irq_handler, irqflags,
1120 					  label, line);
1121 			if (ret) {
1122 				free_irq_label(label);
1123 				return ret;
1124 			}
1125 			line->irq = irq;
1126 		} else {
1127 			ret = hte_edge_setup(line, GPIO_V2_LINE_FLAG_EDGE_BOTH);
1128 			if (ret)
1129 				return ret;
1130 		}
1131 
1132 		WRITE_ONCE(line->level, level);
1133 		WRITE_ONCE(line->sw_debounced, 1);
1134 	}
1135 	return 0;
1136 }
1137 
1138 static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
1139 					  unsigned int line_idx)
1140 {
1141 	unsigned int i;
1142 	u64 mask = BIT_ULL(line_idx);
1143 
1144 	for (i = 0; i < lc->num_attrs; i++) {
1145 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
1146 		    (lc->attrs[i].mask & mask))
1147 			return true;
1148 	}
1149 	return false;
1150 }
1151 
1152 static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
1153 					       unsigned int line_idx)
1154 {
1155 	unsigned int i;
1156 	u64 mask = BIT_ULL(line_idx);
1157 
1158 	for (i = 0; i < lc->num_attrs; i++) {
1159 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
1160 		    (lc->attrs[i].mask & mask))
1161 			return lc->attrs[i].attr.debounce_period_us;
1162 	}
1163 	return 0;
1164 }
1165 
1166 static void edge_detector_stop(struct line *line)
1167 {
1168 	if (line->irq) {
1169 		free_irq_label(free_irq(line->irq, line));
1170 		line->irq = 0;
1171 	}
1172 
1173 #ifdef CONFIG_HTE
1174 	if (READ_ONCE(line->edflags) & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
1175 		hte_ts_put(&line->hdesc);
1176 #endif
1177 
1178 	cancel_delayed_work_sync(&line->work);
1179 	WRITE_ONCE(line->sw_debounced, 0);
1180 	WRITE_ONCE(line->edflags, 0);
1181 	line_set_debounce_period(line, 0);
1182 	/* do not change line->level - see comment in debounced_value() */
1183 }
1184 
1185 static int edge_detector_setup(struct line *line,
1186 			       struct gpio_v2_line_config *lc,
1187 			       unsigned int line_idx, u64 edflags)
1188 {
1189 	u32 debounce_period_us;
1190 	unsigned long irqflags = 0;
1191 	u64 eflags;
1192 	int irq, ret;
1193 	char *label;
1194 
1195 	eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
1196 	if (eflags && !kfifo_initialized(&line->req->events)) {
1197 		ret = kfifo_alloc(&line->req->events,
1198 				  line->req->event_buffer_size, GFP_KERNEL);
1199 		if (ret)
1200 			return ret;
1201 	}
1202 	if (gpio_v2_line_config_debounced(lc, line_idx)) {
1203 		debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
1204 		ret = debounce_setup(line, debounce_period_us);
1205 		if (ret)
1206 			return ret;
1207 		line_set_debounce_period(line, debounce_period_us);
1208 	}
1209 
1210 	/* detection disabled or sw debouncer will provide edge detection */
1211 	if (!eflags || READ_ONCE(line->sw_debounced))
1212 		return 0;
1213 
1214 	if (IS_ENABLED(CONFIG_HTE) &&
1215 	    (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1216 		return hte_edge_setup(line, edflags);
1217 
1218 	irq = gpiod_to_irq(line->desc);
1219 	if (irq < 0)
1220 		return -ENXIO;
1221 
1222 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
1223 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
1224 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1225 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
1226 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
1227 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1228 	irqflags |= IRQF_ONESHOT;
1229 
1230 	label = make_irq_label(line->req->label);
1231 	if (IS_ERR(label))
1232 		return PTR_ERR(label);
1233 
1234 	/* Request a thread to read the events */
1235 	ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
1236 				   irqflags, label, line);
1237 	if (ret) {
1238 		free_irq_label(label);
1239 		return ret;
1240 	}
1241 
1242 	line->irq = irq;
1243 	return 0;
1244 }
1245 
1246 static int edge_detector_update(struct line *line,
1247 				struct gpio_v2_line_config *lc,
1248 				unsigned int line_idx, u64 edflags)
1249 {
1250 	u64 eflags;
1251 	int ret;
1252 	u64 active_edflags = READ_ONCE(line->edflags);
1253 	unsigned int debounce_period_us =
1254 			gpio_v2_line_config_debounce_period(lc, line_idx);
1255 
1256 	if ((active_edflags == edflags) &&
1257 	    (READ_ONCE(line->debounce_period_us) == debounce_period_us))
1258 		return 0;
1259 
1260 	/* sw debounced and still will be...*/
1261 	if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
1262 		line_set_debounce_period(line, debounce_period_us);
1263 		/*
1264 		 * ensure event fifo is initialised if edge detection
1265 		 * is now enabled.
1266 		 */
1267 		eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
1268 		if (eflags && !kfifo_initialized(&line->req->events)) {
1269 			ret = kfifo_alloc(&line->req->events,
1270 					  line->req->event_buffer_size,
1271 					  GFP_KERNEL);
1272 			if (ret)
1273 				return ret;
1274 		}
1275 		return 0;
1276 	}
1277 
1278 	/* reconfiguring edge detection or sw debounce being disabled */
1279 	if ((line->irq && !READ_ONCE(line->sw_debounced)) ||
1280 	    (active_edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) ||
1281 	    (!debounce_period_us && READ_ONCE(line->sw_debounced)))
1282 		edge_detector_stop(line);
1283 
1284 	return edge_detector_setup(line, lc, line_idx, edflags);
1285 }
1286 
1287 static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
1288 				     unsigned int line_idx)
1289 {
1290 	unsigned int i;
1291 	u64 mask = BIT_ULL(line_idx);
1292 
1293 	for (i = 0; i < lc->num_attrs; i++) {
1294 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
1295 		    (lc->attrs[i].mask & mask))
1296 			return lc->attrs[i].attr.flags;
1297 	}
1298 	return lc->flags;
1299 }
1300 
1301 static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
1302 					    unsigned int line_idx)
1303 {
1304 	unsigned int i;
1305 	u64 mask = BIT_ULL(line_idx);
1306 
1307 	for (i = 0; i < lc->num_attrs; i++) {
1308 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
1309 		    (lc->attrs[i].mask & mask))
1310 			return !!(lc->attrs[i].attr.values & mask);
1311 	}
1312 	return 0;
1313 }
1314 
1315 static int gpio_v2_line_flags_validate(u64 flags)
1316 {
1317 	/* Return an error if an unknown flag is set */
1318 	if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
1319 		return -EINVAL;
1320 
1321 	if (!IS_ENABLED(CONFIG_HTE) &&
1322 	    (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1323 		return -EOPNOTSUPP;
1324 
1325 	/*
1326 	 * Do not allow both INPUT and OUTPUT flags to be set as they are
1327 	 * contradictory.
1328 	 */
1329 	if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
1330 	    (flags & GPIO_V2_LINE_FLAG_OUTPUT))
1331 		return -EINVAL;
1332 
1333 	/* Only allow one event clock source */
1334 	if (IS_ENABLED(CONFIG_HTE) &&
1335 	    (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) &&
1336 	    (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1337 		return -EINVAL;
1338 
1339 	/* Edge detection requires explicit input. */
1340 	if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
1341 	    !(flags & GPIO_V2_LINE_FLAG_INPUT))
1342 		return -EINVAL;
1343 
1344 	/*
1345 	 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
1346 	 * request. If the hardware actually supports enabling both at the
1347 	 * same time the electrical result would be disastrous.
1348 	 */
1349 	if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
1350 	    (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
1351 		return -EINVAL;
1352 
1353 	/* Drive requires explicit output direction. */
1354 	if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
1355 	    !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
1356 		return -EINVAL;
1357 
1358 	/* Bias requires explicit direction. */
1359 	if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
1360 	    !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
1361 		return -EINVAL;
1362 
1363 	/* Only one bias flag can be set. */
1364 	if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
1365 	     (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
1366 		       GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
1367 	    ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
1368 	     (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
1369 		return -EINVAL;
1370 
1371 	return 0;
1372 }
1373 
1374 static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
1375 					unsigned int num_lines)
1376 {
1377 	unsigned int i;
1378 	u64 flags;
1379 	int ret;
1380 
1381 	if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
1382 		return -EINVAL;
1383 
1384 	if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
1385 		return -EINVAL;
1386 
1387 	for (i = 0; i < num_lines; i++) {
1388 		flags = gpio_v2_line_config_flags(lc, i);
1389 		ret = gpio_v2_line_flags_validate(flags);
1390 		if (ret)
1391 			return ret;
1392 
1393 		/* debounce requires explicit input */
1394 		if (gpio_v2_line_config_debounced(lc, i) &&
1395 		    !(flags & GPIO_V2_LINE_FLAG_INPUT))
1396 			return -EINVAL;
1397 	}
1398 	return 0;
1399 }
1400 
1401 static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
1402 						    unsigned long *flagsp)
1403 {
1404 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
1405 		   flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
1406 
1407 	if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
1408 		set_bit(FLAG_IS_OUT, flagsp);
1409 	else if (flags & GPIO_V2_LINE_FLAG_INPUT)
1410 		clear_bit(FLAG_IS_OUT, flagsp);
1411 
1412 	assign_bit(FLAG_EDGE_RISING, flagsp,
1413 		   flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
1414 	assign_bit(FLAG_EDGE_FALLING, flagsp,
1415 		   flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
1416 
1417 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
1418 		   flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
1419 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
1420 		   flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
1421 
1422 	assign_bit(FLAG_PULL_UP, flagsp,
1423 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
1424 	assign_bit(FLAG_PULL_DOWN, flagsp,
1425 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
1426 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
1427 		   flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
1428 
1429 	assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
1430 		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
1431 	assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp,
1432 		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
1433 }
1434 
1435 static long linereq_get_values(struct linereq *lr, void __user *ip)
1436 {
1437 	struct gpio_v2_line_values lv;
1438 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
1439 	struct gpio_desc **descs;
1440 	unsigned int i, didx, num_get;
1441 	bool val;
1442 	int ret;
1443 
1444 	/* NOTE: It's ok to read values of output lines. */
1445 	if (copy_from_user(&lv, ip, sizeof(lv)))
1446 		return -EFAULT;
1447 
1448 	for (num_get = 0, i = 0; i < lr->num_lines; i++) {
1449 		if (lv.mask & BIT_ULL(i)) {
1450 			num_get++;
1451 			descs = &lr->lines[i].desc;
1452 		}
1453 	}
1454 
1455 	if (num_get == 0)
1456 		return -EINVAL;
1457 
1458 	if (num_get != 1) {
1459 		descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
1460 		if (!descs)
1461 			return -ENOMEM;
1462 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
1463 			if (lv.mask & BIT_ULL(i)) {
1464 				descs[didx] = lr->lines[i].desc;
1465 				didx++;
1466 			}
1467 		}
1468 	}
1469 	ret = gpiod_get_array_value_complex(false, true, num_get,
1470 					    descs, NULL, vals);
1471 
1472 	if (num_get != 1)
1473 		kfree(descs);
1474 	if (ret)
1475 		return ret;
1476 
1477 	lv.bits = 0;
1478 	for (didx = 0, i = 0; i < lr->num_lines; i++) {
1479 		if (lv.mask & BIT_ULL(i)) {
1480 			if (lr->lines[i].sw_debounced)
1481 				val = debounced_value(&lr->lines[i]);
1482 			else
1483 				val = test_bit(didx, vals);
1484 			if (val)
1485 				lv.bits |= BIT_ULL(i);
1486 			didx++;
1487 		}
1488 	}
1489 
1490 	if (copy_to_user(ip, &lv, sizeof(lv)))
1491 		return -EFAULT;
1492 
1493 	return 0;
1494 }
1495 
1496 static long linereq_set_values_unlocked(struct linereq *lr,
1497 					struct gpio_v2_line_values *lv)
1498 {
1499 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
1500 	struct gpio_desc **descs;
1501 	unsigned int i, didx, num_set;
1502 	int ret;
1503 
1504 	bitmap_zero(vals, GPIO_V2_LINES_MAX);
1505 	for (num_set = 0, i = 0; i < lr->num_lines; i++) {
1506 		if (lv->mask & BIT_ULL(i)) {
1507 			if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
1508 				return -EPERM;
1509 			if (lv->bits & BIT_ULL(i))
1510 				__set_bit(num_set, vals);
1511 			num_set++;
1512 			descs = &lr->lines[i].desc;
1513 		}
1514 	}
1515 	if (num_set == 0)
1516 		return -EINVAL;
1517 
1518 	if (num_set != 1) {
1519 		/* build compacted desc array and values */
1520 		descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
1521 		if (!descs)
1522 			return -ENOMEM;
1523 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
1524 			if (lv->mask & BIT_ULL(i)) {
1525 				descs[didx] = lr->lines[i].desc;
1526 				didx++;
1527 			}
1528 		}
1529 	}
1530 	ret = gpiod_set_array_value_complex(false, true, num_set,
1531 					    descs, NULL, vals);
1532 
1533 	if (num_set != 1)
1534 		kfree(descs);
1535 	return ret;
1536 }
1537 
1538 static long linereq_set_values(struct linereq *lr, void __user *ip)
1539 {
1540 	struct gpio_v2_line_values lv;
1541 	int ret;
1542 
1543 	if (copy_from_user(&lv, ip, sizeof(lv)))
1544 		return -EFAULT;
1545 
1546 	mutex_lock(&lr->config_mutex);
1547 
1548 	ret = linereq_set_values_unlocked(lr, &lv);
1549 
1550 	mutex_unlock(&lr->config_mutex);
1551 
1552 	return ret;
1553 }
1554 
1555 static long linereq_set_config_unlocked(struct linereq *lr,
1556 					struct gpio_v2_line_config *lc)
1557 {
1558 	struct gpio_desc *desc;
1559 	struct line *line;
1560 	unsigned int i;
1561 	u64 flags, edflags;
1562 	int ret;
1563 
1564 	for (i = 0; i < lr->num_lines; i++) {
1565 		line = &lr->lines[i];
1566 		desc = lr->lines[i].desc;
1567 		flags = gpio_v2_line_config_flags(lc, i);
1568 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1569 		edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
1570 		/*
1571 		 * Lines have to be requested explicitly for input
1572 		 * or output, else the line will be treated "as is".
1573 		 */
1574 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1575 			int val = gpio_v2_line_config_output_value(lc, i);
1576 
1577 			edge_detector_stop(line);
1578 			ret = gpiod_direction_output(desc, val);
1579 			if (ret)
1580 				return ret;
1581 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1582 			ret = gpiod_direction_input(desc);
1583 			if (ret)
1584 				return ret;
1585 
1586 			ret = edge_detector_update(line, lc, i, edflags);
1587 			if (ret)
1588 				return ret;
1589 		}
1590 
1591 		WRITE_ONCE(line->edflags, edflags);
1592 
1593 		gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
1594 	}
1595 	return 0;
1596 }
1597 
1598 static long linereq_set_config(struct linereq *lr, void __user *ip)
1599 {
1600 	struct gpio_v2_line_config lc;
1601 	int ret;
1602 
1603 	if (copy_from_user(&lc, ip, sizeof(lc)))
1604 		return -EFAULT;
1605 
1606 	ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
1607 	if (ret)
1608 		return ret;
1609 
1610 	mutex_lock(&lr->config_mutex);
1611 
1612 	ret = linereq_set_config_unlocked(lr, &lc);
1613 
1614 	mutex_unlock(&lr->config_mutex);
1615 
1616 	return ret;
1617 }
1618 
1619 static long linereq_ioctl_unlocked(struct file *file, unsigned int cmd,
1620 				   unsigned long arg)
1621 {
1622 	struct linereq *lr = file->private_data;
1623 	void __user *ip = (void __user *)arg;
1624 
1625 	if (!lr->gdev->chip)
1626 		return -ENODEV;
1627 
1628 	switch (cmd) {
1629 	case GPIO_V2_LINE_GET_VALUES_IOCTL:
1630 		return linereq_get_values(lr, ip);
1631 	case GPIO_V2_LINE_SET_VALUES_IOCTL:
1632 		return linereq_set_values(lr, ip);
1633 	case GPIO_V2_LINE_SET_CONFIG_IOCTL:
1634 		return linereq_set_config(lr, ip);
1635 	default:
1636 		return -EINVAL;
1637 	}
1638 }
1639 
1640 static long linereq_ioctl(struct file *file, unsigned int cmd,
1641 			  unsigned long arg)
1642 {
1643 	struct linereq *lr = file->private_data;
1644 
1645 	return call_ioctl_locked(file, cmd, arg, lr->gdev,
1646 				 linereq_ioctl_unlocked);
1647 }
1648 
1649 #ifdef CONFIG_COMPAT
1650 static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
1651 				 unsigned long arg)
1652 {
1653 	return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1654 }
1655 #endif
1656 
1657 static __poll_t linereq_poll_unlocked(struct file *file,
1658 				      struct poll_table_struct *wait)
1659 {
1660 	struct linereq *lr = file->private_data;
1661 	__poll_t events = 0;
1662 
1663 	if (!lr->gdev->chip)
1664 		return EPOLLHUP | EPOLLERR;
1665 
1666 	poll_wait(file, &lr->wait, wait);
1667 
1668 	if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
1669 						 &lr->wait.lock))
1670 		events = EPOLLIN | EPOLLRDNORM;
1671 
1672 	return events;
1673 }
1674 
1675 static __poll_t linereq_poll(struct file *file,
1676 			     struct poll_table_struct *wait)
1677 {
1678 	struct linereq *lr = file->private_data;
1679 
1680 	return call_poll_locked(file, wait, lr->gdev, linereq_poll_unlocked);
1681 }
1682 
1683 static ssize_t linereq_read_unlocked(struct file *file, char __user *buf,
1684 				     size_t count, loff_t *f_ps)
1685 {
1686 	struct linereq *lr = file->private_data;
1687 	struct gpio_v2_line_event le;
1688 	ssize_t bytes_read = 0;
1689 	int ret;
1690 
1691 	if (!lr->gdev->chip)
1692 		return -ENODEV;
1693 
1694 	if (count < sizeof(le))
1695 		return -EINVAL;
1696 
1697 	do {
1698 		spin_lock(&lr->wait.lock);
1699 		if (kfifo_is_empty(&lr->events)) {
1700 			if (bytes_read) {
1701 				spin_unlock(&lr->wait.lock);
1702 				return bytes_read;
1703 			}
1704 
1705 			if (file->f_flags & O_NONBLOCK) {
1706 				spin_unlock(&lr->wait.lock);
1707 				return -EAGAIN;
1708 			}
1709 
1710 			ret = wait_event_interruptible_locked(lr->wait,
1711 					!kfifo_is_empty(&lr->events));
1712 			if (ret) {
1713 				spin_unlock(&lr->wait.lock);
1714 				return ret;
1715 			}
1716 		}
1717 
1718 		ret = kfifo_out(&lr->events, &le, 1);
1719 		spin_unlock(&lr->wait.lock);
1720 		if (ret != 1) {
1721 			/*
1722 			 * This should never happen - we were holding the
1723 			 * lock from the moment we learned the fifo is no
1724 			 * longer empty until now.
1725 			 */
1726 			ret = -EIO;
1727 			break;
1728 		}
1729 
1730 		if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
1731 			return -EFAULT;
1732 		bytes_read += sizeof(le);
1733 	} while (count >= bytes_read + sizeof(le));
1734 
1735 	return bytes_read;
1736 }
1737 
1738 static ssize_t linereq_read(struct file *file, char __user *buf,
1739 			    size_t count, loff_t *f_ps)
1740 {
1741 	struct linereq *lr = file->private_data;
1742 
1743 	return call_read_locked(file, buf, count, f_ps, lr->gdev,
1744 				linereq_read_unlocked);
1745 }
1746 
1747 static void linereq_free(struct linereq *lr)
1748 {
1749 	struct line *line;
1750 	unsigned int i;
1751 
1752 	if (lr->device_unregistered_nb.notifier_call)
1753 		blocking_notifier_chain_unregister(&lr->gdev->device_notifier,
1754 						   &lr->device_unregistered_nb);
1755 
1756 	for (i = 0; i < lr->num_lines; i++) {
1757 		line = &lr->lines[i];
1758 		if (!line->desc)
1759 			continue;
1760 
1761 		edge_detector_stop(line);
1762 		if (line_has_supinfo(line))
1763 			supinfo_erase(line);
1764 		gpiod_free(line->desc);
1765 	}
1766 	kfifo_free(&lr->events);
1767 	kfree(lr->label);
1768 	gpio_device_put(lr->gdev);
1769 	kfree(lr);
1770 }
1771 
1772 static int linereq_release(struct inode *inode, struct file *file)
1773 {
1774 	struct linereq *lr = file->private_data;
1775 
1776 	linereq_free(lr);
1777 	return 0;
1778 }
1779 
1780 #ifdef CONFIG_PROC_FS
1781 static void linereq_show_fdinfo(struct seq_file *out, struct file *file)
1782 {
1783 	struct linereq *lr = file->private_data;
1784 	struct device *dev = &lr->gdev->dev;
1785 	u16 i;
1786 
1787 	seq_printf(out, "gpio-chip:\t%s\n", dev_name(dev));
1788 
1789 	for (i = 0; i < lr->num_lines; i++)
1790 		seq_printf(out, "gpio-line:\t%d\n",
1791 			   gpio_chip_hwgpio(lr->lines[i].desc));
1792 }
1793 #endif
1794 
1795 static const struct file_operations line_fileops = {
1796 	.release = linereq_release,
1797 	.read = linereq_read,
1798 	.poll = linereq_poll,
1799 	.owner = THIS_MODULE,
1800 	.llseek = noop_llseek,
1801 	.unlocked_ioctl = linereq_ioctl,
1802 #ifdef CONFIG_COMPAT
1803 	.compat_ioctl = linereq_ioctl_compat,
1804 #endif
1805 #ifdef CONFIG_PROC_FS
1806 	.show_fdinfo = linereq_show_fdinfo,
1807 #endif
1808 };
1809 
1810 static int linereq_create(struct gpio_device *gdev, void __user *ip)
1811 {
1812 	struct gpio_v2_line_request ulr;
1813 	struct gpio_v2_line_config *lc;
1814 	struct linereq *lr;
1815 	struct file *file;
1816 	u64 flags, edflags;
1817 	unsigned int i;
1818 	int fd, ret;
1819 
1820 	if (copy_from_user(&ulr, ip, sizeof(ulr)))
1821 		return -EFAULT;
1822 
1823 	if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
1824 		return -EINVAL;
1825 
1826 	if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
1827 		return -EINVAL;
1828 
1829 	lc = &ulr.config;
1830 	ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
1831 	if (ret)
1832 		return ret;
1833 
1834 	lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
1835 	if (!lr)
1836 		return -ENOMEM;
1837 
1838 	lr->gdev = gpio_device_get(gdev);
1839 
1840 	for (i = 0; i < ulr.num_lines; i++) {
1841 		lr->lines[i].req = lr;
1842 		WRITE_ONCE(lr->lines[i].sw_debounced, 0);
1843 		INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
1844 	}
1845 
1846 	if (ulr.consumer[0] != '\0') {
1847 		/* label is only initialized if consumer is set */
1848 		lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1849 				     GFP_KERNEL);
1850 		if (!lr->label) {
1851 			ret = -ENOMEM;
1852 			goto out_free_linereq;
1853 		}
1854 	}
1855 
1856 	mutex_init(&lr->config_mutex);
1857 	init_waitqueue_head(&lr->wait);
1858 	lr->event_buffer_size = ulr.event_buffer_size;
1859 	if (lr->event_buffer_size == 0)
1860 		lr->event_buffer_size = ulr.num_lines * 16;
1861 	else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
1862 		lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
1863 
1864 	atomic_set(&lr->seqno, 0);
1865 	lr->num_lines = ulr.num_lines;
1866 
1867 	/* Request each GPIO */
1868 	for (i = 0; i < ulr.num_lines; i++) {
1869 		u32 offset = ulr.offsets[i];
1870 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
1871 
1872 		if (IS_ERR(desc)) {
1873 			ret = PTR_ERR(desc);
1874 			goto out_free_linereq;
1875 		}
1876 
1877 		ret = gpiod_request_user(desc, lr->label);
1878 		if (ret)
1879 			goto out_free_linereq;
1880 
1881 		lr->lines[i].desc = desc;
1882 		flags = gpio_v2_line_config_flags(lc, i);
1883 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1884 
1885 		ret = gpiod_set_transitory(desc, false);
1886 		if (ret < 0)
1887 			goto out_free_linereq;
1888 
1889 		edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
1890 		/*
1891 		 * Lines have to be requested explicitly for input
1892 		 * or output, else the line will be treated "as is".
1893 		 */
1894 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1895 			int val = gpio_v2_line_config_output_value(lc, i);
1896 
1897 			ret = gpiod_direction_output(desc, val);
1898 			if (ret)
1899 				goto out_free_linereq;
1900 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1901 			ret = gpiod_direction_input(desc);
1902 			if (ret)
1903 				goto out_free_linereq;
1904 
1905 			ret = edge_detector_setup(&lr->lines[i], lc, i,
1906 						  edflags);
1907 			if (ret)
1908 				goto out_free_linereq;
1909 		}
1910 
1911 		lr->lines[i].edflags = edflags;
1912 
1913 		gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
1914 
1915 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
1916 			offset);
1917 	}
1918 
1919 	lr->device_unregistered_nb.notifier_call = linereq_unregistered_notify;
1920 	ret = blocking_notifier_chain_register(&gdev->device_notifier,
1921 					       &lr->device_unregistered_nb);
1922 	if (ret)
1923 		goto out_free_linereq;
1924 
1925 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1926 	if (fd < 0) {
1927 		ret = fd;
1928 		goto out_free_linereq;
1929 	}
1930 
1931 	file = anon_inode_getfile("gpio-line", &line_fileops, lr,
1932 				  O_RDONLY | O_CLOEXEC);
1933 	if (IS_ERR(file)) {
1934 		ret = PTR_ERR(file);
1935 		goto out_put_unused_fd;
1936 	}
1937 
1938 	ulr.fd = fd;
1939 	if (copy_to_user(ip, &ulr, sizeof(ulr))) {
1940 		/*
1941 		 * fput() will trigger the release() callback, so do not go onto
1942 		 * the regular error cleanup path here.
1943 		 */
1944 		fput(file);
1945 		put_unused_fd(fd);
1946 		return -EFAULT;
1947 	}
1948 
1949 	fd_install(fd, file);
1950 
1951 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
1952 		lr->num_lines);
1953 
1954 	return 0;
1955 
1956 out_put_unused_fd:
1957 	put_unused_fd(fd);
1958 out_free_linereq:
1959 	linereq_free(lr);
1960 	return ret;
1961 }
1962 
1963 #ifdef CONFIG_GPIO_CDEV_V1
1964 
1965 /*
1966  * GPIO line event management
1967  */
1968 
1969 /**
1970  * struct lineevent_state - contains the state of a userspace event
1971  * @gdev: the GPIO device the event pertains to
1972  * @label: consumer label used to tag descriptors
1973  * @desc: the GPIO descriptor held by this event
1974  * @eflags: the event flags this line was requested with
1975  * @irq: the interrupt that trigger in response to events on this GPIO
1976  * @wait: wait queue that handles blocking reads of events
1977  * @device_unregistered_nb: notifier block for receiving gdev unregister events
1978  * @events: KFIFO for the GPIO events
1979  * @timestamp: cache for the timestamp storing it between hardirq
1980  * and IRQ thread, used to bring the timestamp close to the actual
1981  * event
1982  */
1983 struct lineevent_state {
1984 	struct gpio_device *gdev;
1985 	const char *label;
1986 	struct gpio_desc *desc;
1987 	u32 eflags;
1988 	int irq;
1989 	wait_queue_head_t wait;
1990 	struct notifier_block device_unregistered_nb;
1991 	DECLARE_KFIFO(events, struct gpioevent_data, 16);
1992 	u64 timestamp;
1993 };
1994 
1995 #define GPIOEVENT_REQUEST_VALID_FLAGS \
1996 	(GPIOEVENT_REQUEST_RISING_EDGE | \
1997 	GPIOEVENT_REQUEST_FALLING_EDGE)
1998 
1999 static __poll_t lineevent_poll_unlocked(struct file *file,
2000 					struct poll_table_struct *wait)
2001 {
2002 	struct lineevent_state *le = file->private_data;
2003 	__poll_t events = 0;
2004 
2005 	if (!le->gdev->chip)
2006 		return EPOLLHUP | EPOLLERR;
2007 
2008 	poll_wait(file, &le->wait, wait);
2009 
2010 	if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
2011 		events = EPOLLIN | EPOLLRDNORM;
2012 
2013 	return events;
2014 }
2015 
2016 static __poll_t lineevent_poll(struct file *file,
2017 			       struct poll_table_struct *wait)
2018 {
2019 	struct lineevent_state *le = file->private_data;
2020 
2021 	return call_poll_locked(file, wait, le->gdev, lineevent_poll_unlocked);
2022 }
2023 
2024 static int lineevent_unregistered_notify(struct notifier_block *nb,
2025 					 unsigned long action, void *data)
2026 {
2027 	struct lineevent_state *le = container_of(nb, struct lineevent_state,
2028 						  device_unregistered_nb);
2029 
2030 	wake_up_poll(&le->wait, EPOLLIN | EPOLLERR);
2031 
2032 	return NOTIFY_OK;
2033 }
2034 
2035 struct compat_gpioeevent_data {
2036 	compat_u64	timestamp;
2037 	u32		id;
2038 };
2039 
2040 static ssize_t lineevent_read_unlocked(struct file *file, char __user *buf,
2041 				       size_t count, loff_t *f_ps)
2042 {
2043 	struct lineevent_state *le = file->private_data;
2044 	struct gpioevent_data ge;
2045 	ssize_t bytes_read = 0;
2046 	ssize_t ge_size;
2047 	int ret;
2048 
2049 	if (!le->gdev->chip)
2050 		return -ENODEV;
2051 
2052 	/*
2053 	 * When compatible system call is being used the struct gpioevent_data,
2054 	 * in case of at least ia32, has different size due to the alignment
2055 	 * differences. Because we have first member 64 bits followed by one of
2056 	 * 32 bits there is no gap between them. The only difference is the
2057 	 * padding at the end of the data structure. Hence, we calculate the
2058 	 * actual sizeof() and pass this as an argument to copy_to_user() to
2059 	 * drop unneeded bytes from the output.
2060 	 */
2061 	if (compat_need_64bit_alignment_fixup())
2062 		ge_size = sizeof(struct compat_gpioeevent_data);
2063 	else
2064 		ge_size = sizeof(struct gpioevent_data);
2065 	if (count < ge_size)
2066 		return -EINVAL;
2067 
2068 	do {
2069 		spin_lock(&le->wait.lock);
2070 		if (kfifo_is_empty(&le->events)) {
2071 			if (bytes_read) {
2072 				spin_unlock(&le->wait.lock);
2073 				return bytes_read;
2074 			}
2075 
2076 			if (file->f_flags & O_NONBLOCK) {
2077 				spin_unlock(&le->wait.lock);
2078 				return -EAGAIN;
2079 			}
2080 
2081 			ret = wait_event_interruptible_locked(le->wait,
2082 					!kfifo_is_empty(&le->events));
2083 			if (ret) {
2084 				spin_unlock(&le->wait.lock);
2085 				return ret;
2086 			}
2087 		}
2088 
2089 		ret = kfifo_out(&le->events, &ge, 1);
2090 		spin_unlock(&le->wait.lock);
2091 		if (ret != 1) {
2092 			/*
2093 			 * This should never happen - we were holding the lock
2094 			 * from the moment we learned the fifo is no longer
2095 			 * empty until now.
2096 			 */
2097 			ret = -EIO;
2098 			break;
2099 		}
2100 
2101 		if (copy_to_user(buf + bytes_read, &ge, ge_size))
2102 			return -EFAULT;
2103 		bytes_read += ge_size;
2104 	} while (count >= bytes_read + ge_size);
2105 
2106 	return bytes_read;
2107 }
2108 
2109 static ssize_t lineevent_read(struct file *file, char __user *buf,
2110 			      size_t count, loff_t *f_ps)
2111 {
2112 	struct lineevent_state *le = file->private_data;
2113 
2114 	return call_read_locked(file, buf, count, f_ps, le->gdev,
2115 				lineevent_read_unlocked);
2116 }
2117 
2118 static void lineevent_free(struct lineevent_state *le)
2119 {
2120 	if (le->device_unregistered_nb.notifier_call)
2121 		blocking_notifier_chain_unregister(&le->gdev->device_notifier,
2122 						   &le->device_unregistered_nb);
2123 	if (le->irq)
2124 		free_irq_label(free_irq(le->irq, le));
2125 	if (le->desc)
2126 		gpiod_free(le->desc);
2127 	kfree(le->label);
2128 	gpio_device_put(le->gdev);
2129 	kfree(le);
2130 }
2131 
2132 static int lineevent_release(struct inode *inode, struct file *file)
2133 {
2134 	lineevent_free(file->private_data);
2135 	return 0;
2136 }
2137 
2138 static long lineevent_ioctl_unlocked(struct file *file, unsigned int cmd,
2139 				     unsigned long arg)
2140 {
2141 	struct lineevent_state *le = file->private_data;
2142 	void __user *ip = (void __user *)arg;
2143 	struct gpiohandle_data ghd;
2144 
2145 	if (!le->gdev->chip)
2146 		return -ENODEV;
2147 
2148 	/*
2149 	 * We can get the value for an event line but not set it,
2150 	 * because it is input by definition.
2151 	 */
2152 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
2153 		int val;
2154 
2155 		memset(&ghd, 0, sizeof(ghd));
2156 
2157 		val = gpiod_get_value_cansleep(le->desc);
2158 		if (val < 0)
2159 			return val;
2160 		ghd.values[0] = val;
2161 
2162 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
2163 			return -EFAULT;
2164 
2165 		return 0;
2166 	}
2167 	return -EINVAL;
2168 }
2169 
2170 static long lineevent_ioctl(struct file *file, unsigned int cmd,
2171 			    unsigned long arg)
2172 {
2173 	struct lineevent_state *le = file->private_data;
2174 
2175 	return call_ioctl_locked(file, cmd, arg, le->gdev,
2176 				 lineevent_ioctl_unlocked);
2177 }
2178 
2179 #ifdef CONFIG_COMPAT
2180 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
2181 				   unsigned long arg)
2182 {
2183 	return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2184 }
2185 #endif
2186 
2187 static const struct file_operations lineevent_fileops = {
2188 	.release = lineevent_release,
2189 	.read = lineevent_read,
2190 	.poll = lineevent_poll,
2191 	.owner = THIS_MODULE,
2192 	.llseek = noop_llseek,
2193 	.unlocked_ioctl = lineevent_ioctl,
2194 #ifdef CONFIG_COMPAT
2195 	.compat_ioctl = lineevent_ioctl_compat,
2196 #endif
2197 };
2198 
2199 static irqreturn_t lineevent_irq_thread(int irq, void *p)
2200 {
2201 	struct lineevent_state *le = p;
2202 	struct gpioevent_data ge;
2203 	int ret;
2204 
2205 	/* Do not leak kernel stack to userspace */
2206 	memset(&ge, 0, sizeof(ge));
2207 
2208 	/*
2209 	 * We may be running from a nested threaded interrupt in which case
2210 	 * we didn't get the timestamp from lineevent_irq_handler().
2211 	 */
2212 	if (!le->timestamp)
2213 		ge.timestamp = ktime_get_ns();
2214 	else
2215 		ge.timestamp = le->timestamp;
2216 
2217 	if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
2218 	    && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
2219 		int level = gpiod_get_value_cansleep(le->desc);
2220 
2221 		if (level)
2222 			/* Emit low-to-high event */
2223 			ge.id = GPIOEVENT_EVENT_RISING_EDGE;
2224 		else
2225 			/* Emit high-to-low event */
2226 			ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
2227 	} else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
2228 		/* Emit low-to-high event */
2229 		ge.id = GPIOEVENT_EVENT_RISING_EDGE;
2230 	} else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
2231 		/* Emit high-to-low event */
2232 		ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
2233 	} else {
2234 		return IRQ_NONE;
2235 	}
2236 
2237 	ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
2238 					    1, &le->wait.lock);
2239 	if (ret)
2240 		wake_up_poll(&le->wait, EPOLLIN);
2241 	else
2242 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
2243 
2244 	return IRQ_HANDLED;
2245 }
2246 
2247 static irqreturn_t lineevent_irq_handler(int irq, void *p)
2248 {
2249 	struct lineevent_state *le = p;
2250 
2251 	/*
2252 	 * Just store the timestamp in hardirq context so we get it as
2253 	 * close in time as possible to the actual event.
2254 	 */
2255 	le->timestamp = ktime_get_ns();
2256 
2257 	return IRQ_WAKE_THREAD;
2258 }
2259 
2260 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
2261 {
2262 	struct gpioevent_request eventreq;
2263 	struct lineevent_state *le;
2264 	struct gpio_desc *desc;
2265 	struct file *file;
2266 	u32 offset;
2267 	u32 lflags;
2268 	u32 eflags;
2269 	int fd;
2270 	int ret;
2271 	int irq, irqflags = 0;
2272 	char *label;
2273 
2274 	if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
2275 		return -EFAULT;
2276 
2277 	offset = eventreq.lineoffset;
2278 	lflags = eventreq.handleflags;
2279 	eflags = eventreq.eventflags;
2280 
2281 	desc = gpiochip_get_desc(gdev->chip, offset);
2282 	if (IS_ERR(desc))
2283 		return PTR_ERR(desc);
2284 
2285 	/* Return an error if a unknown flag is set */
2286 	if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
2287 	    (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
2288 		return -EINVAL;
2289 
2290 	/* This is just wrong: we don't look for events on output lines */
2291 	if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
2292 	    (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
2293 	    (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
2294 		return -EINVAL;
2295 
2296 	/* Only one bias flag can be set. */
2297 	if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
2298 	     (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
2299 			GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
2300 	    ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
2301 	     (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
2302 		return -EINVAL;
2303 
2304 	le = kzalloc(sizeof(*le), GFP_KERNEL);
2305 	if (!le)
2306 		return -ENOMEM;
2307 	le->gdev = gpio_device_get(gdev);
2308 
2309 	if (eventreq.consumer_label[0] != '\0') {
2310 		/* label is only initialized if consumer_label is set */
2311 		le->label = kstrndup(eventreq.consumer_label,
2312 				     sizeof(eventreq.consumer_label) - 1,
2313 				     GFP_KERNEL);
2314 		if (!le->label) {
2315 			ret = -ENOMEM;
2316 			goto out_free_le;
2317 		}
2318 	}
2319 
2320 	ret = gpiod_request_user(desc, le->label);
2321 	if (ret)
2322 		goto out_free_le;
2323 	le->desc = desc;
2324 	le->eflags = eflags;
2325 
2326 	linehandle_flags_to_desc_flags(lflags, &desc->flags);
2327 
2328 	ret = gpiod_direction_input(desc);
2329 	if (ret)
2330 		goto out_free_le;
2331 
2332 	gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
2333 
2334 	irq = gpiod_to_irq(desc);
2335 	if (irq <= 0) {
2336 		ret = -ENODEV;
2337 		goto out_free_le;
2338 	}
2339 
2340 	if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
2341 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
2342 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
2343 	if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
2344 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
2345 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
2346 	irqflags |= IRQF_ONESHOT;
2347 
2348 	INIT_KFIFO(le->events);
2349 	init_waitqueue_head(&le->wait);
2350 
2351 	le->device_unregistered_nb.notifier_call = lineevent_unregistered_notify;
2352 	ret = blocking_notifier_chain_register(&gdev->device_notifier,
2353 					       &le->device_unregistered_nb);
2354 	if (ret)
2355 		goto out_free_le;
2356 
2357 	label = make_irq_label(le->label);
2358 	if (IS_ERR(label)) {
2359 		ret = PTR_ERR(label);
2360 		goto out_free_le;
2361 	}
2362 
2363 	/* Request a thread to read the events */
2364 	ret = request_threaded_irq(irq,
2365 				   lineevent_irq_handler,
2366 				   lineevent_irq_thread,
2367 				   irqflags,
2368 				   label,
2369 				   le);
2370 	if (ret) {
2371 		free_irq_label(label);
2372 		goto out_free_le;
2373 	}
2374 
2375 	le->irq = irq;
2376 
2377 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
2378 	if (fd < 0) {
2379 		ret = fd;
2380 		goto out_free_le;
2381 	}
2382 
2383 	file = anon_inode_getfile("gpio-event",
2384 				  &lineevent_fileops,
2385 				  le,
2386 				  O_RDONLY | O_CLOEXEC);
2387 	if (IS_ERR(file)) {
2388 		ret = PTR_ERR(file);
2389 		goto out_put_unused_fd;
2390 	}
2391 
2392 	eventreq.fd = fd;
2393 	if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
2394 		/*
2395 		 * fput() will trigger the release() callback, so do not go onto
2396 		 * the regular error cleanup path here.
2397 		 */
2398 		fput(file);
2399 		put_unused_fd(fd);
2400 		return -EFAULT;
2401 	}
2402 
2403 	fd_install(fd, file);
2404 
2405 	return 0;
2406 
2407 out_put_unused_fd:
2408 	put_unused_fd(fd);
2409 out_free_le:
2410 	lineevent_free(le);
2411 	return ret;
2412 }
2413 
2414 static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
2415 				    struct gpioline_info *info_v1)
2416 {
2417 	u64 flagsv2 = info_v2->flags;
2418 
2419 	memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
2420 	memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
2421 	info_v1->line_offset = info_v2->offset;
2422 	info_v1->flags = 0;
2423 
2424 	if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
2425 		info_v1->flags |= GPIOLINE_FLAG_KERNEL;
2426 
2427 	if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
2428 		info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
2429 
2430 	if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
2431 		info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
2432 
2433 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
2434 		info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
2435 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
2436 		info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
2437 
2438 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
2439 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
2440 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
2441 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
2442 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
2443 		info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
2444 }
2445 
2446 static void gpio_v2_line_info_changed_to_v1(
2447 		struct gpio_v2_line_info_changed *lic_v2,
2448 		struct gpioline_info_changed *lic_v1)
2449 {
2450 	memset(lic_v1, 0, sizeof(*lic_v1));
2451 	gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
2452 	lic_v1->timestamp = lic_v2->timestamp_ns;
2453 	lic_v1->event_type = lic_v2->event_type;
2454 }
2455 
2456 #endif /* CONFIG_GPIO_CDEV_V1 */
2457 
2458 static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
2459 				  struct gpio_v2_line_info *info)
2460 {
2461 	struct gpio_chip *gc = desc->gdev->chip;
2462 	bool ok_for_pinctrl;
2463 	unsigned long flags;
2464 
2465 	memset(info, 0, sizeof(*info));
2466 	info->offset = gpio_chip_hwgpio(desc);
2467 
2468 	/*
2469 	 * This function takes a mutex so we must check this before taking
2470 	 * the spinlock.
2471 	 *
2472 	 * FIXME: find a non-racy way to retrieve this information. Maybe a
2473 	 * lock common to both frameworks?
2474 	 */
2475 	ok_for_pinctrl =
2476 		pinctrl_gpio_can_use_line(gc->base + info->offset);
2477 
2478 	spin_lock_irqsave(&gpio_lock, flags);
2479 
2480 	if (desc->name)
2481 		strscpy(info->name, desc->name, sizeof(info->name));
2482 
2483 	if (desc->label)
2484 		strscpy(info->consumer, desc->label, sizeof(info->consumer));
2485 
2486 	/*
2487 	 * Userspace only need to know that the kernel is using this GPIO so
2488 	 * it can't use it.
2489 	 */
2490 	info->flags = 0;
2491 	if (test_bit(FLAG_REQUESTED, &desc->flags) ||
2492 	    test_bit(FLAG_IS_HOGGED, &desc->flags) ||
2493 	    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
2494 	    test_bit(FLAG_EXPORT, &desc->flags) ||
2495 	    test_bit(FLAG_SYSFS, &desc->flags) ||
2496 	    !gpiochip_line_is_valid(gc, info->offset) ||
2497 	    !ok_for_pinctrl)
2498 		info->flags |= GPIO_V2_LINE_FLAG_USED;
2499 
2500 	if (test_bit(FLAG_IS_OUT, &desc->flags))
2501 		info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
2502 	else
2503 		info->flags |= GPIO_V2_LINE_FLAG_INPUT;
2504 
2505 	if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2506 		info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
2507 
2508 	if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2509 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
2510 	if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2511 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
2512 
2513 	if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2514 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
2515 	if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2516 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
2517 	if (test_bit(FLAG_PULL_UP, &desc->flags))
2518 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
2519 
2520 	if (test_bit(FLAG_EDGE_RISING, &desc->flags))
2521 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
2522 	if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
2523 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
2524 
2525 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
2526 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
2527 	else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags))
2528 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
2529 
2530 	spin_unlock_irqrestore(&gpio_lock, flags);
2531 }
2532 
2533 struct gpio_chardev_data {
2534 	struct gpio_device *gdev;
2535 	wait_queue_head_t wait;
2536 	DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
2537 	struct notifier_block lineinfo_changed_nb;
2538 	struct notifier_block device_unregistered_nb;
2539 	unsigned long *watched_lines;
2540 #ifdef CONFIG_GPIO_CDEV_V1
2541 	atomic_t watch_abi_version;
2542 #endif
2543 };
2544 
2545 static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
2546 {
2547 	struct gpio_device *gdev = cdev->gdev;
2548 	struct gpiochip_info chipinfo;
2549 
2550 	memset(&chipinfo, 0, sizeof(chipinfo));
2551 
2552 	strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
2553 	strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
2554 	chipinfo.lines = gdev->ngpio;
2555 	if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
2556 		return -EFAULT;
2557 	return 0;
2558 }
2559 
2560 #ifdef CONFIG_GPIO_CDEV_V1
2561 /*
2562  * returns 0 if the versions match, else the previously selected ABI version
2563  */
2564 static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
2565 				       unsigned int version)
2566 {
2567 	int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
2568 
2569 	if (abiv == version)
2570 		return 0;
2571 
2572 	return abiv;
2573 }
2574 
2575 static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
2576 			   bool watch)
2577 {
2578 	struct gpio_desc *desc;
2579 	struct gpioline_info lineinfo;
2580 	struct gpio_v2_line_info lineinfo_v2;
2581 
2582 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2583 		return -EFAULT;
2584 
2585 	/* this doubles as a range check on line_offset */
2586 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset);
2587 	if (IS_ERR(desc))
2588 		return PTR_ERR(desc);
2589 
2590 	if (watch) {
2591 		if (lineinfo_ensure_abi_version(cdev, 1))
2592 			return -EPERM;
2593 
2594 		if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
2595 			return -EBUSY;
2596 	}
2597 
2598 	gpio_desc_to_lineinfo(desc, &lineinfo_v2);
2599 	gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
2600 
2601 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2602 		if (watch)
2603 			clear_bit(lineinfo.line_offset, cdev->watched_lines);
2604 		return -EFAULT;
2605 	}
2606 
2607 	return 0;
2608 }
2609 #endif
2610 
2611 static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
2612 			bool watch)
2613 {
2614 	struct gpio_desc *desc;
2615 	struct gpio_v2_line_info lineinfo;
2616 
2617 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2618 		return -EFAULT;
2619 
2620 	if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2621 		return -EINVAL;
2622 
2623 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2624 	if (IS_ERR(desc))
2625 		return PTR_ERR(desc);
2626 
2627 	if (watch) {
2628 #ifdef CONFIG_GPIO_CDEV_V1
2629 		if (lineinfo_ensure_abi_version(cdev, 2))
2630 			return -EPERM;
2631 #endif
2632 		if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2633 			return -EBUSY;
2634 	}
2635 	gpio_desc_to_lineinfo(desc, &lineinfo);
2636 	supinfo_to_lineinfo(desc, &lineinfo);
2637 
2638 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2639 		if (watch)
2640 			clear_bit(lineinfo.offset, cdev->watched_lines);
2641 		return -EFAULT;
2642 	}
2643 
2644 	return 0;
2645 }
2646 
2647 static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
2648 {
2649 	__u32 offset;
2650 
2651 	if (copy_from_user(&offset, ip, sizeof(offset)))
2652 		return -EFAULT;
2653 
2654 	if (offset >= cdev->gdev->ngpio)
2655 		return -EINVAL;
2656 
2657 	if (!test_and_clear_bit(offset, cdev->watched_lines))
2658 		return -EBUSY;
2659 
2660 	return 0;
2661 }
2662 
2663 static long gpio_ioctl_unlocked(struct file *file, unsigned int cmd, unsigned long arg)
2664 {
2665 	struct gpio_chardev_data *cdev = file->private_data;
2666 	struct gpio_device *gdev = cdev->gdev;
2667 	void __user *ip = (void __user *)arg;
2668 
2669 	/* We fail any subsequent ioctl():s when the chip is gone */
2670 	if (!gdev->chip)
2671 		return -ENODEV;
2672 
2673 	/* Fill in the struct and pass to userspace */
2674 	switch (cmd) {
2675 	case GPIO_GET_CHIPINFO_IOCTL:
2676 		return chipinfo_get(cdev, ip);
2677 #ifdef CONFIG_GPIO_CDEV_V1
2678 	case GPIO_GET_LINEHANDLE_IOCTL:
2679 		return linehandle_create(gdev, ip);
2680 	case GPIO_GET_LINEEVENT_IOCTL:
2681 		return lineevent_create(gdev, ip);
2682 	case GPIO_GET_LINEINFO_IOCTL:
2683 		return lineinfo_get_v1(cdev, ip, false);
2684 	case GPIO_GET_LINEINFO_WATCH_IOCTL:
2685 		return lineinfo_get_v1(cdev, ip, true);
2686 #endif /* CONFIG_GPIO_CDEV_V1 */
2687 	case GPIO_V2_GET_LINEINFO_IOCTL:
2688 		return lineinfo_get(cdev, ip, false);
2689 	case GPIO_V2_GET_LINEINFO_WATCH_IOCTL:
2690 		return lineinfo_get(cdev, ip, true);
2691 	case GPIO_V2_GET_LINE_IOCTL:
2692 		return linereq_create(gdev, ip);
2693 	case GPIO_GET_LINEINFO_UNWATCH_IOCTL:
2694 		return lineinfo_unwatch(cdev, ip);
2695 	default:
2696 		return -EINVAL;
2697 	}
2698 }
2699 
2700 /*
2701  * gpio_ioctl() - ioctl handler for the GPIO chardev
2702  */
2703 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2704 {
2705 	struct gpio_chardev_data *cdev = file->private_data;
2706 
2707 	return call_ioctl_locked(file, cmd, arg, cdev->gdev,
2708 				 gpio_ioctl_unlocked);
2709 }
2710 
2711 #ifdef CONFIG_COMPAT
2712 static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2713 			      unsigned long arg)
2714 {
2715 	return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2716 }
2717 #endif
2718 
2719 static int lineinfo_changed_notify(struct notifier_block *nb,
2720 				   unsigned long action, void *data)
2721 {
2722 	struct gpio_chardev_data *cdev =
2723 		container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2724 	struct gpio_v2_line_info_changed chg;
2725 	struct gpio_desc *desc = data;
2726 	int ret;
2727 
2728 	if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2729 		return NOTIFY_DONE;
2730 
2731 	memset(&chg, 0, sizeof(chg));
2732 	chg.event_type = action;
2733 	chg.timestamp_ns = ktime_get_ns();
2734 	gpio_desc_to_lineinfo(desc, &chg.info);
2735 	supinfo_to_lineinfo(desc, &chg.info);
2736 
2737 	ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2738 	if (ret)
2739 		wake_up_poll(&cdev->wait, EPOLLIN);
2740 	else
2741 		pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2742 
2743 	return NOTIFY_OK;
2744 }
2745 
2746 static int gpio_device_unregistered_notify(struct notifier_block *nb,
2747 					   unsigned long action, void *data)
2748 {
2749 	struct gpio_chardev_data *cdev = container_of(nb,
2750 						      struct gpio_chardev_data,
2751 						      device_unregistered_nb);
2752 
2753 	wake_up_poll(&cdev->wait, EPOLLIN | EPOLLERR);
2754 
2755 	return NOTIFY_OK;
2756 }
2757 
2758 static __poll_t lineinfo_watch_poll_unlocked(struct file *file,
2759 					     struct poll_table_struct *pollt)
2760 {
2761 	struct gpio_chardev_data *cdev = file->private_data;
2762 	__poll_t events = 0;
2763 
2764 	if (!cdev->gdev->chip)
2765 		return EPOLLHUP | EPOLLERR;
2766 
2767 	poll_wait(file, &cdev->wait, pollt);
2768 
2769 	if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2770 						 &cdev->wait.lock))
2771 		events = EPOLLIN | EPOLLRDNORM;
2772 
2773 	return events;
2774 }
2775 
2776 static __poll_t lineinfo_watch_poll(struct file *file,
2777 				    struct poll_table_struct *pollt)
2778 {
2779 	struct gpio_chardev_data *cdev = file->private_data;
2780 
2781 	return call_poll_locked(file, pollt, cdev->gdev,
2782 				lineinfo_watch_poll_unlocked);
2783 }
2784 
2785 static ssize_t lineinfo_watch_read_unlocked(struct file *file, char __user *buf,
2786 					    size_t count, loff_t *off)
2787 {
2788 	struct gpio_chardev_data *cdev = file->private_data;
2789 	struct gpio_v2_line_info_changed event;
2790 	ssize_t bytes_read = 0;
2791 	int ret;
2792 	size_t event_size;
2793 
2794 	if (!cdev->gdev->chip)
2795 		return -ENODEV;
2796 
2797 #ifndef CONFIG_GPIO_CDEV_V1
2798 	event_size = sizeof(struct gpio_v2_line_info_changed);
2799 	if (count < event_size)
2800 		return -EINVAL;
2801 #endif
2802 
2803 	do {
2804 		spin_lock(&cdev->wait.lock);
2805 		if (kfifo_is_empty(&cdev->events)) {
2806 			if (bytes_read) {
2807 				spin_unlock(&cdev->wait.lock);
2808 				return bytes_read;
2809 			}
2810 
2811 			if (file->f_flags & O_NONBLOCK) {
2812 				spin_unlock(&cdev->wait.lock);
2813 				return -EAGAIN;
2814 			}
2815 
2816 			ret = wait_event_interruptible_locked(cdev->wait,
2817 					!kfifo_is_empty(&cdev->events));
2818 			if (ret) {
2819 				spin_unlock(&cdev->wait.lock);
2820 				return ret;
2821 			}
2822 		}
2823 #ifdef CONFIG_GPIO_CDEV_V1
2824 		/* must be after kfifo check so watch_abi_version is set */
2825 		if (atomic_read(&cdev->watch_abi_version) == 2)
2826 			event_size = sizeof(struct gpio_v2_line_info_changed);
2827 		else
2828 			event_size = sizeof(struct gpioline_info_changed);
2829 		if (count < event_size) {
2830 			spin_unlock(&cdev->wait.lock);
2831 			return -EINVAL;
2832 		}
2833 #endif
2834 		ret = kfifo_out(&cdev->events, &event, 1);
2835 		spin_unlock(&cdev->wait.lock);
2836 		if (ret != 1) {
2837 			ret = -EIO;
2838 			break;
2839 			/* We should never get here. See lineevent_read(). */
2840 		}
2841 
2842 #ifdef CONFIG_GPIO_CDEV_V1
2843 		if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2844 			if (copy_to_user(buf + bytes_read, &event, event_size))
2845 				return -EFAULT;
2846 		} else {
2847 			struct gpioline_info_changed event_v1;
2848 
2849 			gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2850 			if (copy_to_user(buf + bytes_read, &event_v1,
2851 					 event_size))
2852 				return -EFAULT;
2853 		}
2854 #else
2855 		if (copy_to_user(buf + bytes_read, &event, event_size))
2856 			return -EFAULT;
2857 #endif
2858 		bytes_read += event_size;
2859 	} while (count >= bytes_read + sizeof(event));
2860 
2861 	return bytes_read;
2862 }
2863 
2864 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2865 				   size_t count, loff_t *off)
2866 {
2867 	struct gpio_chardev_data *cdev = file->private_data;
2868 
2869 	return call_read_locked(file, buf, count, off, cdev->gdev,
2870 				lineinfo_watch_read_unlocked);
2871 }
2872 
2873 /**
2874  * gpio_chrdev_open() - open the chardev for ioctl operations
2875  * @inode: inode for this chardev
2876  * @file: file struct for storing private data
2877  * Returns 0 on success
2878  */
2879 static int gpio_chrdev_open(struct inode *inode, struct file *file)
2880 {
2881 	struct gpio_device *gdev = container_of(inode->i_cdev,
2882 						struct gpio_device, chrdev);
2883 	struct gpio_chardev_data *cdev;
2884 	int ret = -ENOMEM;
2885 
2886 	down_read(&gdev->sem);
2887 
2888 	/* Fail on open if the backing gpiochip is gone */
2889 	if (!gdev->chip) {
2890 		ret = -ENODEV;
2891 		goto out_unlock;
2892 	}
2893 
2894 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2895 	if (!cdev)
2896 		goto out_unlock;
2897 
2898 	cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2899 	if (!cdev->watched_lines)
2900 		goto out_free_cdev;
2901 
2902 	init_waitqueue_head(&cdev->wait);
2903 	INIT_KFIFO(cdev->events);
2904 	cdev->gdev = gpio_device_get(gdev);
2905 
2906 	cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
2907 	ret = blocking_notifier_chain_register(&gdev->line_state_notifier,
2908 					       &cdev->lineinfo_changed_nb);
2909 	if (ret)
2910 		goto out_free_bitmap;
2911 
2912 	cdev->device_unregistered_nb.notifier_call =
2913 					gpio_device_unregistered_notify;
2914 	ret = blocking_notifier_chain_register(&gdev->device_notifier,
2915 					       &cdev->device_unregistered_nb);
2916 	if (ret)
2917 		goto out_unregister_line_notifier;
2918 
2919 	file->private_data = cdev;
2920 
2921 	ret = nonseekable_open(inode, file);
2922 	if (ret)
2923 		goto out_unregister_device_notifier;
2924 
2925 	up_read(&gdev->sem);
2926 
2927 	return ret;
2928 
2929 out_unregister_device_notifier:
2930 	blocking_notifier_chain_unregister(&gdev->device_notifier,
2931 					   &cdev->device_unregistered_nb);
2932 out_unregister_line_notifier:
2933 	blocking_notifier_chain_unregister(&gdev->line_state_notifier,
2934 					   &cdev->lineinfo_changed_nb);
2935 out_free_bitmap:
2936 	gpio_device_put(gdev);
2937 	bitmap_free(cdev->watched_lines);
2938 out_free_cdev:
2939 	kfree(cdev);
2940 out_unlock:
2941 	up_read(&gdev->sem);
2942 	return ret;
2943 }
2944 
2945 /**
2946  * gpio_chrdev_release() - close chardev after ioctl operations
2947  * @inode: inode for this chardev
2948  * @file: file struct for storing private data
2949  * Returns 0 on success
2950  */
2951 static int gpio_chrdev_release(struct inode *inode, struct file *file)
2952 {
2953 	struct gpio_chardev_data *cdev = file->private_data;
2954 	struct gpio_device *gdev = cdev->gdev;
2955 
2956 	blocking_notifier_chain_unregister(&gdev->device_notifier,
2957 					   &cdev->device_unregistered_nb);
2958 	blocking_notifier_chain_unregister(&gdev->line_state_notifier,
2959 					   &cdev->lineinfo_changed_nb);
2960 	bitmap_free(cdev->watched_lines);
2961 	gpio_device_put(gdev);
2962 	kfree(cdev);
2963 
2964 	return 0;
2965 }
2966 
2967 static const struct file_operations gpio_fileops = {
2968 	.release = gpio_chrdev_release,
2969 	.open = gpio_chrdev_open,
2970 	.poll = lineinfo_watch_poll,
2971 	.read = lineinfo_watch_read,
2972 	.owner = THIS_MODULE,
2973 	.llseek = no_llseek,
2974 	.unlocked_ioctl = gpio_ioctl,
2975 #ifdef CONFIG_COMPAT
2976 	.compat_ioctl = gpio_ioctl_compat,
2977 #endif
2978 };
2979 
2980 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2981 {
2982 	int ret;
2983 
2984 	cdev_init(&gdev->chrdev, &gpio_fileops);
2985 	gdev->chrdev.owner = THIS_MODULE;
2986 	gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2987 
2988 	ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2989 	if (ret)
2990 		return ret;
2991 
2992 	chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2993 		 MAJOR(devt), gdev->id);
2994 
2995 	return 0;
2996 }
2997 
2998 void gpiolib_cdev_unregister(struct gpio_device *gdev)
2999 {
3000 	cdev_device_del(&gdev->chrdev, &gdev->dev);
3001 	blocking_notifier_call_chain(&gdev->device_notifier, 0, NULL);
3002 }
3003