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