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