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