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