xref: /openbmc/linux/drivers/gpio/gpiolib-cdev.c (revision ad804659)
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;
1464 
1465 	for (i = 0; i < lr->num_lines; i++) {
1466 		hte = !!test_bit(FLAG_EVENT_CLOCK_HTE,
1467 				 &lr->lines[i].desc->flags);
1468 		edge_detector_stop(&lr->lines[i], hte);
1469 		if (lr->lines[i].desc)
1470 			gpiod_free(lr->lines[i].desc);
1471 	}
1472 	kfifo_free(&lr->events);
1473 	kfree(lr->label);
1474 	put_device(&lr->gdev->dev);
1475 	kfree(lr);
1476 }
1477 
1478 static int linereq_release(struct inode *inode, struct file *file)
1479 {
1480 	struct linereq *lr = file->private_data;
1481 
1482 	linereq_free(lr);
1483 	return 0;
1484 }
1485 
1486 static const struct file_operations line_fileops = {
1487 	.release = linereq_release,
1488 	.read = linereq_read,
1489 	.poll = linereq_poll,
1490 	.owner = THIS_MODULE,
1491 	.llseek = noop_llseek,
1492 	.unlocked_ioctl = linereq_ioctl,
1493 #ifdef CONFIG_COMPAT
1494 	.compat_ioctl = linereq_ioctl_compat,
1495 #endif
1496 };
1497 
1498 static int linereq_create(struct gpio_device *gdev, void __user *ip)
1499 {
1500 	struct gpio_v2_line_request ulr;
1501 	struct gpio_v2_line_config *lc;
1502 	struct linereq *lr;
1503 	struct file *file;
1504 	u64 flags;
1505 	unsigned int i;
1506 	int fd, ret;
1507 
1508 	if (copy_from_user(&ulr, ip, sizeof(ulr)))
1509 		return -EFAULT;
1510 
1511 	if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
1512 		return -EINVAL;
1513 
1514 	if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
1515 		return -EINVAL;
1516 
1517 	lc = &ulr.config;
1518 	ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
1519 	if (ret)
1520 		return ret;
1521 
1522 	lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
1523 	if (!lr)
1524 		return -ENOMEM;
1525 
1526 	lr->gdev = gdev;
1527 	get_device(&gdev->dev);
1528 
1529 	for (i = 0; i < ulr.num_lines; i++) {
1530 		lr->lines[i].req = lr;
1531 		WRITE_ONCE(lr->lines[i].sw_debounced, 0);
1532 		INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
1533 	}
1534 
1535 	if (ulr.consumer[0] != '\0') {
1536 		/* label is only initialized if consumer is set */
1537 		lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1538 				     GFP_KERNEL);
1539 		if (!lr->label) {
1540 			ret = -ENOMEM;
1541 			goto out_free_linereq;
1542 		}
1543 	}
1544 
1545 	mutex_init(&lr->config_mutex);
1546 	init_waitqueue_head(&lr->wait);
1547 	lr->event_buffer_size = ulr.event_buffer_size;
1548 	if (lr->event_buffer_size == 0)
1549 		lr->event_buffer_size = ulr.num_lines * 16;
1550 	else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
1551 		lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
1552 
1553 	atomic_set(&lr->seqno, 0);
1554 	lr->num_lines = ulr.num_lines;
1555 
1556 	/* Request each GPIO */
1557 	for (i = 0; i < ulr.num_lines; i++) {
1558 		u32 offset = ulr.offsets[i];
1559 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
1560 
1561 		if (IS_ERR(desc)) {
1562 			ret = PTR_ERR(desc);
1563 			goto out_free_linereq;
1564 		}
1565 
1566 		ret = gpiod_request_user(desc, lr->label);
1567 		if (ret)
1568 			goto out_free_linereq;
1569 
1570 		lr->lines[i].desc = desc;
1571 		flags = gpio_v2_line_config_flags(lc, i);
1572 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1573 
1574 		ret = gpiod_set_transitory(desc, false);
1575 		if (ret < 0)
1576 			goto out_free_linereq;
1577 
1578 		/*
1579 		 * Lines have to be requested explicitly for input
1580 		 * or output, else the line will be treated "as is".
1581 		 */
1582 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1583 			int val = gpio_v2_line_config_output_value(lc, i);
1584 
1585 			ret = gpiod_direction_output(desc, val);
1586 			if (ret)
1587 				goto out_free_linereq;
1588 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1589 			ret = gpiod_direction_input(desc);
1590 			if (ret)
1591 				goto out_free_linereq;
1592 
1593 			ret = edge_detector_setup(&lr->lines[i], lc, i,
1594 				flags & GPIO_V2_LINE_EDGE_FLAGS,
1595 				flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
1596 			if (ret)
1597 				goto out_free_linereq;
1598 		}
1599 
1600 		blocking_notifier_call_chain(&desc->gdev->notifier,
1601 					     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1602 
1603 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
1604 			offset);
1605 	}
1606 
1607 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1608 	if (fd < 0) {
1609 		ret = fd;
1610 		goto out_free_linereq;
1611 	}
1612 
1613 	file = anon_inode_getfile("gpio-line", &line_fileops, lr,
1614 				  O_RDONLY | O_CLOEXEC);
1615 	if (IS_ERR(file)) {
1616 		ret = PTR_ERR(file);
1617 		goto out_put_unused_fd;
1618 	}
1619 
1620 	ulr.fd = fd;
1621 	if (copy_to_user(ip, &ulr, sizeof(ulr))) {
1622 		/*
1623 		 * fput() will trigger the release() callback, so do not go onto
1624 		 * the regular error cleanup path here.
1625 		 */
1626 		fput(file);
1627 		put_unused_fd(fd);
1628 		return -EFAULT;
1629 	}
1630 
1631 	fd_install(fd, file);
1632 
1633 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
1634 		lr->num_lines);
1635 
1636 	return 0;
1637 
1638 out_put_unused_fd:
1639 	put_unused_fd(fd);
1640 out_free_linereq:
1641 	linereq_free(lr);
1642 	return ret;
1643 }
1644 
1645 #ifdef CONFIG_GPIO_CDEV_V1
1646 
1647 /*
1648  * GPIO line event management
1649  */
1650 
1651 /**
1652  * struct lineevent_state - contains the state of a userspace event
1653  * @gdev: the GPIO device the event pertains to
1654  * @label: consumer label used to tag descriptors
1655  * @desc: the GPIO descriptor held by this event
1656  * @eflags: the event flags this line was requested with
1657  * @irq: the interrupt that trigger in response to events on this GPIO
1658  * @wait: wait queue that handles blocking reads of events
1659  * @events: KFIFO for the GPIO events
1660  * @timestamp: cache for the timestamp storing it between hardirq
1661  * and IRQ thread, used to bring the timestamp close to the actual
1662  * event
1663  */
1664 struct lineevent_state {
1665 	struct gpio_device *gdev;
1666 	const char *label;
1667 	struct gpio_desc *desc;
1668 	u32 eflags;
1669 	int irq;
1670 	wait_queue_head_t wait;
1671 	DECLARE_KFIFO(events, struct gpioevent_data, 16);
1672 	u64 timestamp;
1673 };
1674 
1675 #define GPIOEVENT_REQUEST_VALID_FLAGS \
1676 	(GPIOEVENT_REQUEST_RISING_EDGE | \
1677 	GPIOEVENT_REQUEST_FALLING_EDGE)
1678 
1679 static __poll_t lineevent_poll(struct file *file,
1680 			       struct poll_table_struct *wait)
1681 {
1682 	struct lineevent_state *le = file->private_data;
1683 	__poll_t events = 0;
1684 
1685 	poll_wait(file, &le->wait, wait);
1686 
1687 	if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1688 		events = EPOLLIN | EPOLLRDNORM;
1689 
1690 	return events;
1691 }
1692 
1693 struct compat_gpioeevent_data {
1694 	compat_u64	timestamp;
1695 	u32		id;
1696 };
1697 
1698 static ssize_t lineevent_read(struct file *file,
1699 			      char __user *buf,
1700 			      size_t count,
1701 			      loff_t *f_ps)
1702 {
1703 	struct lineevent_state *le = file->private_data;
1704 	struct gpioevent_data ge;
1705 	ssize_t bytes_read = 0;
1706 	ssize_t ge_size;
1707 	int ret;
1708 
1709 	/*
1710 	 * When compatible system call is being used the struct gpioevent_data,
1711 	 * in case of at least ia32, has different size due to the alignment
1712 	 * differences. Because we have first member 64 bits followed by one of
1713 	 * 32 bits there is no gap between them. The only difference is the
1714 	 * padding at the end of the data structure. Hence, we calculate the
1715 	 * actual sizeof() and pass this as an argument to copy_to_user() to
1716 	 * drop unneeded bytes from the output.
1717 	 */
1718 	if (compat_need_64bit_alignment_fixup())
1719 		ge_size = sizeof(struct compat_gpioeevent_data);
1720 	else
1721 		ge_size = sizeof(struct gpioevent_data);
1722 	if (count < ge_size)
1723 		return -EINVAL;
1724 
1725 	do {
1726 		spin_lock(&le->wait.lock);
1727 		if (kfifo_is_empty(&le->events)) {
1728 			if (bytes_read) {
1729 				spin_unlock(&le->wait.lock);
1730 				return bytes_read;
1731 			}
1732 
1733 			if (file->f_flags & O_NONBLOCK) {
1734 				spin_unlock(&le->wait.lock);
1735 				return -EAGAIN;
1736 			}
1737 
1738 			ret = wait_event_interruptible_locked(le->wait,
1739 					!kfifo_is_empty(&le->events));
1740 			if (ret) {
1741 				spin_unlock(&le->wait.lock);
1742 				return ret;
1743 			}
1744 		}
1745 
1746 		ret = kfifo_out(&le->events, &ge, 1);
1747 		spin_unlock(&le->wait.lock);
1748 		if (ret != 1) {
1749 			/*
1750 			 * This should never happen - we were holding the lock
1751 			 * from the moment we learned the fifo is no longer
1752 			 * empty until now.
1753 			 */
1754 			ret = -EIO;
1755 			break;
1756 		}
1757 
1758 		if (copy_to_user(buf + bytes_read, &ge, ge_size))
1759 			return -EFAULT;
1760 		bytes_read += ge_size;
1761 	} while (count >= bytes_read + ge_size);
1762 
1763 	return bytes_read;
1764 }
1765 
1766 static void lineevent_free(struct lineevent_state *le)
1767 {
1768 	if (le->irq)
1769 		free_irq(le->irq, le);
1770 	if (le->desc)
1771 		gpiod_free(le->desc);
1772 	kfree(le->label);
1773 	put_device(&le->gdev->dev);
1774 	kfree(le);
1775 }
1776 
1777 static int lineevent_release(struct inode *inode, struct file *file)
1778 {
1779 	lineevent_free(file->private_data);
1780 	return 0;
1781 }
1782 
1783 static long lineevent_ioctl(struct file *file, unsigned int cmd,
1784 			    unsigned long arg)
1785 {
1786 	struct lineevent_state *le = file->private_data;
1787 	void __user *ip = (void __user *)arg;
1788 	struct gpiohandle_data ghd;
1789 
1790 	/*
1791 	 * We can get the value for an event line but not set it,
1792 	 * because it is input by definition.
1793 	 */
1794 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
1795 		int val;
1796 
1797 		memset(&ghd, 0, sizeof(ghd));
1798 
1799 		val = gpiod_get_value_cansleep(le->desc);
1800 		if (val < 0)
1801 			return val;
1802 		ghd.values[0] = val;
1803 
1804 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
1805 			return -EFAULT;
1806 
1807 		return 0;
1808 	}
1809 	return -EINVAL;
1810 }
1811 
1812 #ifdef CONFIG_COMPAT
1813 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
1814 				   unsigned long arg)
1815 {
1816 	return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1817 }
1818 #endif
1819 
1820 static const struct file_operations lineevent_fileops = {
1821 	.release = lineevent_release,
1822 	.read = lineevent_read,
1823 	.poll = lineevent_poll,
1824 	.owner = THIS_MODULE,
1825 	.llseek = noop_llseek,
1826 	.unlocked_ioctl = lineevent_ioctl,
1827 #ifdef CONFIG_COMPAT
1828 	.compat_ioctl = lineevent_ioctl_compat,
1829 #endif
1830 };
1831 
1832 static irqreturn_t lineevent_irq_thread(int irq, void *p)
1833 {
1834 	struct lineevent_state *le = p;
1835 	struct gpioevent_data ge;
1836 	int ret;
1837 
1838 	/* Do not leak kernel stack to userspace */
1839 	memset(&ge, 0, sizeof(ge));
1840 
1841 	/*
1842 	 * We may be running from a nested threaded interrupt in which case
1843 	 * we didn't get the timestamp from lineevent_irq_handler().
1844 	 */
1845 	if (!le->timestamp)
1846 		ge.timestamp = ktime_get_ns();
1847 	else
1848 		ge.timestamp = le->timestamp;
1849 
1850 	if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
1851 	    && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1852 		int level = gpiod_get_value_cansleep(le->desc);
1853 
1854 		if (level)
1855 			/* Emit low-to-high event */
1856 			ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1857 		else
1858 			/* Emit high-to-low event */
1859 			ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1860 	} else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
1861 		/* Emit low-to-high event */
1862 		ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1863 	} else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1864 		/* Emit high-to-low event */
1865 		ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1866 	} else {
1867 		return IRQ_NONE;
1868 	}
1869 
1870 	ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
1871 					    1, &le->wait.lock);
1872 	if (ret)
1873 		wake_up_poll(&le->wait, EPOLLIN);
1874 	else
1875 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
1876 
1877 	return IRQ_HANDLED;
1878 }
1879 
1880 static irqreturn_t lineevent_irq_handler(int irq, void *p)
1881 {
1882 	struct lineevent_state *le = p;
1883 
1884 	/*
1885 	 * Just store the timestamp in hardirq context so we get it as
1886 	 * close in time as possible to the actual event.
1887 	 */
1888 	le->timestamp = ktime_get_ns();
1889 
1890 	return IRQ_WAKE_THREAD;
1891 }
1892 
1893 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1894 {
1895 	struct gpioevent_request eventreq;
1896 	struct lineevent_state *le;
1897 	struct gpio_desc *desc;
1898 	struct file *file;
1899 	u32 offset;
1900 	u32 lflags;
1901 	u32 eflags;
1902 	int fd;
1903 	int ret;
1904 	int irq, irqflags = 0;
1905 
1906 	if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1907 		return -EFAULT;
1908 
1909 	offset = eventreq.lineoffset;
1910 	lflags = eventreq.handleflags;
1911 	eflags = eventreq.eventflags;
1912 
1913 	desc = gpiochip_get_desc(gdev->chip, offset);
1914 	if (IS_ERR(desc))
1915 		return PTR_ERR(desc);
1916 
1917 	/* Return an error if a unknown flag is set */
1918 	if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1919 	    (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1920 		return -EINVAL;
1921 
1922 	/* This is just wrong: we don't look for events on output lines */
1923 	if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1924 	    (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1925 	    (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1926 		return -EINVAL;
1927 
1928 	/* Only one bias flag can be set. */
1929 	if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1930 	     (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1931 			GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1932 	    ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1933 	     (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1934 		return -EINVAL;
1935 
1936 	le = kzalloc(sizeof(*le), GFP_KERNEL);
1937 	if (!le)
1938 		return -ENOMEM;
1939 	le->gdev = gdev;
1940 	get_device(&gdev->dev);
1941 
1942 	if (eventreq.consumer_label[0] != '\0') {
1943 		/* label is only initialized if consumer_label is set */
1944 		le->label = kstrndup(eventreq.consumer_label,
1945 				     sizeof(eventreq.consumer_label) - 1,
1946 				     GFP_KERNEL);
1947 		if (!le->label) {
1948 			ret = -ENOMEM;
1949 			goto out_free_le;
1950 		}
1951 	}
1952 
1953 	ret = gpiod_request_user(desc, le->label);
1954 	if (ret)
1955 		goto out_free_le;
1956 	le->desc = desc;
1957 	le->eflags = eflags;
1958 
1959 	linehandle_flags_to_desc_flags(lflags, &desc->flags);
1960 
1961 	ret = gpiod_direction_input(desc);
1962 	if (ret)
1963 		goto out_free_le;
1964 
1965 	blocking_notifier_call_chain(&desc->gdev->notifier,
1966 				     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1967 
1968 	irq = gpiod_to_irq(desc);
1969 	if (irq <= 0) {
1970 		ret = -ENODEV;
1971 		goto out_free_le;
1972 	}
1973 	le->irq = irq;
1974 
1975 	if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
1976 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1977 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1978 	if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
1979 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1980 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1981 	irqflags |= IRQF_ONESHOT;
1982 
1983 	INIT_KFIFO(le->events);
1984 	init_waitqueue_head(&le->wait);
1985 
1986 	/* Request a thread to read the events */
1987 	ret = request_threaded_irq(le->irq,
1988 				   lineevent_irq_handler,
1989 				   lineevent_irq_thread,
1990 				   irqflags,
1991 				   le->label,
1992 				   le);
1993 	if (ret)
1994 		goto out_free_le;
1995 
1996 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1997 	if (fd < 0) {
1998 		ret = fd;
1999 		goto out_free_le;
2000 	}
2001 
2002 	file = anon_inode_getfile("gpio-event",
2003 				  &lineevent_fileops,
2004 				  le,
2005 				  O_RDONLY | O_CLOEXEC);
2006 	if (IS_ERR(file)) {
2007 		ret = PTR_ERR(file);
2008 		goto out_put_unused_fd;
2009 	}
2010 
2011 	eventreq.fd = fd;
2012 	if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
2013 		/*
2014 		 * fput() will trigger the release() callback, so do not go onto
2015 		 * the regular error cleanup path here.
2016 		 */
2017 		fput(file);
2018 		put_unused_fd(fd);
2019 		return -EFAULT;
2020 	}
2021 
2022 	fd_install(fd, file);
2023 
2024 	return 0;
2025 
2026 out_put_unused_fd:
2027 	put_unused_fd(fd);
2028 out_free_le:
2029 	lineevent_free(le);
2030 	return ret;
2031 }
2032 
2033 static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
2034 				    struct gpioline_info *info_v1)
2035 {
2036 	u64 flagsv2 = info_v2->flags;
2037 
2038 	memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
2039 	memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
2040 	info_v1->line_offset = info_v2->offset;
2041 	info_v1->flags = 0;
2042 
2043 	if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
2044 		info_v1->flags |= GPIOLINE_FLAG_KERNEL;
2045 
2046 	if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
2047 		info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
2048 
2049 	if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
2050 		info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
2051 
2052 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
2053 		info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
2054 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
2055 		info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
2056 
2057 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
2058 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
2059 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
2060 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
2061 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
2062 		info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
2063 }
2064 
2065 static void gpio_v2_line_info_changed_to_v1(
2066 		struct gpio_v2_line_info_changed *lic_v2,
2067 		struct gpioline_info_changed *lic_v1)
2068 {
2069 	memset(lic_v1, 0, sizeof(*lic_v1));
2070 	gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
2071 	lic_v1->timestamp = lic_v2->timestamp_ns;
2072 	lic_v1->event_type = lic_v2->event_type;
2073 }
2074 
2075 #endif /* CONFIG_GPIO_CDEV_V1 */
2076 
2077 static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
2078 				  struct gpio_v2_line_info *info)
2079 {
2080 	struct gpio_chip *gc = desc->gdev->chip;
2081 	bool ok_for_pinctrl;
2082 	unsigned long flags;
2083 	u32 debounce_period_us;
2084 	unsigned int num_attrs = 0;
2085 
2086 	memset(info, 0, sizeof(*info));
2087 	info->offset = gpio_chip_hwgpio(desc);
2088 
2089 	/*
2090 	 * This function takes a mutex so we must check this before taking
2091 	 * the spinlock.
2092 	 *
2093 	 * FIXME: find a non-racy way to retrieve this information. Maybe a
2094 	 * lock common to both frameworks?
2095 	 */
2096 	ok_for_pinctrl =
2097 		pinctrl_gpio_can_use_line(gc->base + info->offset);
2098 
2099 	spin_lock_irqsave(&gpio_lock, flags);
2100 
2101 	if (desc->name)
2102 		strscpy(info->name, desc->name, sizeof(info->name));
2103 
2104 	if (desc->label)
2105 		strscpy(info->consumer, desc->label, sizeof(info->consumer));
2106 
2107 	/*
2108 	 * Userspace only need to know that the kernel is using this GPIO so
2109 	 * it can't use it.
2110 	 */
2111 	info->flags = 0;
2112 	if (test_bit(FLAG_REQUESTED, &desc->flags) ||
2113 	    test_bit(FLAG_IS_HOGGED, &desc->flags) ||
2114 	    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
2115 	    test_bit(FLAG_EXPORT, &desc->flags) ||
2116 	    test_bit(FLAG_SYSFS, &desc->flags) ||
2117 	    !gpiochip_line_is_valid(gc, info->offset) ||
2118 	    !ok_for_pinctrl)
2119 		info->flags |= GPIO_V2_LINE_FLAG_USED;
2120 
2121 	if (test_bit(FLAG_IS_OUT, &desc->flags))
2122 		info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
2123 	else
2124 		info->flags |= GPIO_V2_LINE_FLAG_INPUT;
2125 
2126 	if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2127 		info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
2128 
2129 	if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2130 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
2131 	if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2132 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
2133 
2134 	if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2135 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
2136 	if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2137 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
2138 	if (test_bit(FLAG_PULL_UP, &desc->flags))
2139 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
2140 
2141 	if (test_bit(FLAG_EDGE_RISING, &desc->flags))
2142 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
2143 	if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
2144 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
2145 
2146 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
2147 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
2148 	else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags))
2149 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
2150 
2151 	debounce_period_us = READ_ONCE(desc->debounce_period_us);
2152 	if (debounce_period_us) {
2153 		info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
2154 		info->attrs[num_attrs].debounce_period_us = debounce_period_us;
2155 		num_attrs++;
2156 	}
2157 	info->num_attrs = num_attrs;
2158 
2159 	spin_unlock_irqrestore(&gpio_lock, flags);
2160 }
2161 
2162 struct gpio_chardev_data {
2163 	struct gpio_device *gdev;
2164 	wait_queue_head_t wait;
2165 	DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
2166 	struct notifier_block lineinfo_changed_nb;
2167 	unsigned long *watched_lines;
2168 #ifdef CONFIG_GPIO_CDEV_V1
2169 	atomic_t watch_abi_version;
2170 #endif
2171 };
2172 
2173 static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
2174 {
2175 	struct gpio_device *gdev = cdev->gdev;
2176 	struct gpiochip_info chipinfo;
2177 
2178 	memset(&chipinfo, 0, sizeof(chipinfo));
2179 
2180 	strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
2181 	strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
2182 	chipinfo.lines = gdev->ngpio;
2183 	if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
2184 		return -EFAULT;
2185 	return 0;
2186 }
2187 
2188 #ifdef CONFIG_GPIO_CDEV_V1
2189 /*
2190  * returns 0 if the versions match, else the previously selected ABI version
2191  */
2192 static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
2193 				       unsigned int version)
2194 {
2195 	int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
2196 
2197 	if (abiv == version)
2198 		return 0;
2199 
2200 	return abiv;
2201 }
2202 
2203 static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
2204 			   bool watch)
2205 {
2206 	struct gpio_desc *desc;
2207 	struct gpioline_info lineinfo;
2208 	struct gpio_v2_line_info lineinfo_v2;
2209 
2210 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2211 		return -EFAULT;
2212 
2213 	/* this doubles as a range check on line_offset */
2214 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset);
2215 	if (IS_ERR(desc))
2216 		return PTR_ERR(desc);
2217 
2218 	if (watch) {
2219 		if (lineinfo_ensure_abi_version(cdev, 1))
2220 			return -EPERM;
2221 
2222 		if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
2223 			return -EBUSY;
2224 	}
2225 
2226 	gpio_desc_to_lineinfo(desc, &lineinfo_v2);
2227 	gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
2228 
2229 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2230 		if (watch)
2231 			clear_bit(lineinfo.line_offset, cdev->watched_lines);
2232 		return -EFAULT;
2233 	}
2234 
2235 	return 0;
2236 }
2237 #endif
2238 
2239 static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
2240 			bool watch)
2241 {
2242 	struct gpio_desc *desc;
2243 	struct gpio_v2_line_info lineinfo;
2244 
2245 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2246 		return -EFAULT;
2247 
2248 	if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2249 		return -EINVAL;
2250 
2251 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2252 	if (IS_ERR(desc))
2253 		return PTR_ERR(desc);
2254 
2255 	if (watch) {
2256 #ifdef CONFIG_GPIO_CDEV_V1
2257 		if (lineinfo_ensure_abi_version(cdev, 2))
2258 			return -EPERM;
2259 #endif
2260 		if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2261 			return -EBUSY;
2262 	}
2263 	gpio_desc_to_lineinfo(desc, &lineinfo);
2264 
2265 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2266 		if (watch)
2267 			clear_bit(lineinfo.offset, cdev->watched_lines);
2268 		return -EFAULT;
2269 	}
2270 
2271 	return 0;
2272 }
2273 
2274 static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
2275 {
2276 	__u32 offset;
2277 
2278 	if (copy_from_user(&offset, ip, sizeof(offset)))
2279 		return -EFAULT;
2280 
2281 	if (offset >= cdev->gdev->ngpio)
2282 		return -EINVAL;
2283 
2284 	if (!test_and_clear_bit(offset, cdev->watched_lines))
2285 		return -EBUSY;
2286 
2287 	return 0;
2288 }
2289 
2290 /*
2291  * gpio_ioctl() - ioctl handler for the GPIO chardev
2292  */
2293 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2294 {
2295 	struct gpio_chardev_data *cdev = file->private_data;
2296 	struct gpio_device *gdev = cdev->gdev;
2297 	void __user *ip = (void __user *)arg;
2298 
2299 	/* We fail any subsequent ioctl():s when the chip is gone */
2300 	if (!gdev->chip)
2301 		return -ENODEV;
2302 
2303 	/* Fill in the struct and pass to userspace */
2304 	switch (cmd) {
2305 	case GPIO_GET_CHIPINFO_IOCTL:
2306 		return chipinfo_get(cdev, ip);
2307 #ifdef CONFIG_GPIO_CDEV_V1
2308 	case GPIO_GET_LINEHANDLE_IOCTL:
2309 		return linehandle_create(gdev, ip);
2310 	case GPIO_GET_LINEEVENT_IOCTL:
2311 		return lineevent_create(gdev, ip);
2312 	case GPIO_GET_LINEINFO_IOCTL:
2313 		return lineinfo_get_v1(cdev, ip, false);
2314 	case GPIO_GET_LINEINFO_WATCH_IOCTL:
2315 		return lineinfo_get_v1(cdev, ip, true);
2316 #endif /* CONFIG_GPIO_CDEV_V1 */
2317 	case GPIO_V2_GET_LINEINFO_IOCTL:
2318 		return lineinfo_get(cdev, ip, false);
2319 	case GPIO_V2_GET_LINEINFO_WATCH_IOCTL:
2320 		return lineinfo_get(cdev, ip, true);
2321 	case GPIO_V2_GET_LINE_IOCTL:
2322 		return linereq_create(gdev, ip);
2323 	case GPIO_GET_LINEINFO_UNWATCH_IOCTL:
2324 		return lineinfo_unwatch(cdev, ip);
2325 	default:
2326 		return -EINVAL;
2327 	}
2328 }
2329 
2330 #ifdef CONFIG_COMPAT
2331 static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2332 			      unsigned long arg)
2333 {
2334 	return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2335 }
2336 #endif
2337 
2338 static struct gpio_chardev_data *
2339 to_gpio_chardev_data(struct notifier_block *nb)
2340 {
2341 	return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2342 }
2343 
2344 static int lineinfo_changed_notify(struct notifier_block *nb,
2345 				   unsigned long action, void *data)
2346 {
2347 	struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
2348 	struct gpio_v2_line_info_changed chg;
2349 	struct gpio_desc *desc = data;
2350 	int ret;
2351 
2352 	if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2353 		return NOTIFY_DONE;
2354 
2355 	memset(&chg, 0, sizeof(chg));
2356 	chg.event_type = action;
2357 	chg.timestamp_ns = ktime_get_ns();
2358 	gpio_desc_to_lineinfo(desc, &chg.info);
2359 
2360 	ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2361 	if (ret)
2362 		wake_up_poll(&cdev->wait, EPOLLIN);
2363 	else
2364 		pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2365 
2366 	return NOTIFY_OK;
2367 }
2368 
2369 static __poll_t lineinfo_watch_poll(struct file *file,
2370 				    struct poll_table_struct *pollt)
2371 {
2372 	struct gpio_chardev_data *cdev = file->private_data;
2373 	__poll_t events = 0;
2374 
2375 	poll_wait(file, &cdev->wait, pollt);
2376 
2377 	if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2378 						 &cdev->wait.lock))
2379 		events = EPOLLIN | EPOLLRDNORM;
2380 
2381 	return events;
2382 }
2383 
2384 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2385 				   size_t count, loff_t *off)
2386 {
2387 	struct gpio_chardev_data *cdev = file->private_data;
2388 	struct gpio_v2_line_info_changed event;
2389 	ssize_t bytes_read = 0;
2390 	int ret;
2391 	size_t event_size;
2392 
2393 #ifndef CONFIG_GPIO_CDEV_V1
2394 	event_size = sizeof(struct gpio_v2_line_info_changed);
2395 	if (count < event_size)
2396 		return -EINVAL;
2397 #endif
2398 
2399 	do {
2400 		spin_lock(&cdev->wait.lock);
2401 		if (kfifo_is_empty(&cdev->events)) {
2402 			if (bytes_read) {
2403 				spin_unlock(&cdev->wait.lock);
2404 				return bytes_read;
2405 			}
2406 
2407 			if (file->f_flags & O_NONBLOCK) {
2408 				spin_unlock(&cdev->wait.lock);
2409 				return -EAGAIN;
2410 			}
2411 
2412 			ret = wait_event_interruptible_locked(cdev->wait,
2413 					!kfifo_is_empty(&cdev->events));
2414 			if (ret) {
2415 				spin_unlock(&cdev->wait.lock);
2416 				return ret;
2417 			}
2418 		}
2419 #ifdef CONFIG_GPIO_CDEV_V1
2420 		/* must be after kfifo check so watch_abi_version is set */
2421 		if (atomic_read(&cdev->watch_abi_version) == 2)
2422 			event_size = sizeof(struct gpio_v2_line_info_changed);
2423 		else
2424 			event_size = sizeof(struct gpioline_info_changed);
2425 		if (count < event_size) {
2426 			spin_unlock(&cdev->wait.lock);
2427 			return -EINVAL;
2428 		}
2429 #endif
2430 		ret = kfifo_out(&cdev->events, &event, 1);
2431 		spin_unlock(&cdev->wait.lock);
2432 		if (ret != 1) {
2433 			ret = -EIO;
2434 			break;
2435 			/* We should never get here. See lineevent_read(). */
2436 		}
2437 
2438 #ifdef CONFIG_GPIO_CDEV_V1
2439 		if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2440 			if (copy_to_user(buf + bytes_read, &event, event_size))
2441 				return -EFAULT;
2442 		} else {
2443 			struct gpioline_info_changed event_v1;
2444 
2445 			gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2446 			if (copy_to_user(buf + bytes_read, &event_v1,
2447 					 event_size))
2448 				return -EFAULT;
2449 		}
2450 #else
2451 		if (copy_to_user(buf + bytes_read, &event, event_size))
2452 			return -EFAULT;
2453 #endif
2454 		bytes_read += event_size;
2455 	} while (count >= bytes_read + sizeof(event));
2456 
2457 	return bytes_read;
2458 }
2459 
2460 /**
2461  * gpio_chrdev_open() - open the chardev for ioctl operations
2462  * @inode: inode for this chardev
2463  * @file: file struct for storing private data
2464  * Returns 0 on success
2465  */
2466 static int gpio_chrdev_open(struct inode *inode, struct file *file)
2467 {
2468 	struct gpio_device *gdev = container_of(inode->i_cdev,
2469 						struct gpio_device, chrdev);
2470 	struct gpio_chardev_data *cdev;
2471 	int ret = -ENOMEM;
2472 
2473 	/* Fail on open if the backing gpiochip is gone */
2474 	if (!gdev->chip)
2475 		return -ENODEV;
2476 
2477 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2478 	if (!cdev)
2479 		return -ENOMEM;
2480 
2481 	cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2482 	if (!cdev->watched_lines)
2483 		goto out_free_cdev;
2484 
2485 	init_waitqueue_head(&cdev->wait);
2486 	INIT_KFIFO(cdev->events);
2487 	cdev->gdev = gdev;
2488 
2489 	cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
2490 	ret = blocking_notifier_chain_register(&gdev->notifier,
2491 					       &cdev->lineinfo_changed_nb);
2492 	if (ret)
2493 		goto out_free_bitmap;
2494 
2495 	get_device(&gdev->dev);
2496 	file->private_data = cdev;
2497 
2498 	ret = nonseekable_open(inode, file);
2499 	if (ret)
2500 		goto out_unregister_notifier;
2501 
2502 	return ret;
2503 
2504 out_unregister_notifier:
2505 	blocking_notifier_chain_unregister(&gdev->notifier,
2506 					   &cdev->lineinfo_changed_nb);
2507 out_free_bitmap:
2508 	bitmap_free(cdev->watched_lines);
2509 out_free_cdev:
2510 	kfree(cdev);
2511 	return ret;
2512 }
2513 
2514 /**
2515  * gpio_chrdev_release() - close chardev after ioctl operations
2516  * @inode: inode for this chardev
2517  * @file: file struct for storing private data
2518  * Returns 0 on success
2519  */
2520 static int gpio_chrdev_release(struct inode *inode, struct file *file)
2521 {
2522 	struct gpio_chardev_data *cdev = file->private_data;
2523 	struct gpio_device *gdev = cdev->gdev;
2524 
2525 	bitmap_free(cdev->watched_lines);
2526 	blocking_notifier_chain_unregister(&gdev->notifier,
2527 					   &cdev->lineinfo_changed_nb);
2528 	put_device(&gdev->dev);
2529 	kfree(cdev);
2530 
2531 	return 0;
2532 }
2533 
2534 static const struct file_operations gpio_fileops = {
2535 	.release = gpio_chrdev_release,
2536 	.open = gpio_chrdev_open,
2537 	.poll = lineinfo_watch_poll,
2538 	.read = lineinfo_watch_read,
2539 	.owner = THIS_MODULE,
2540 	.llseek = no_llseek,
2541 	.unlocked_ioctl = gpio_ioctl,
2542 #ifdef CONFIG_COMPAT
2543 	.compat_ioctl = gpio_ioctl_compat,
2544 #endif
2545 };
2546 
2547 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2548 {
2549 	int ret;
2550 
2551 	cdev_init(&gdev->chrdev, &gpio_fileops);
2552 	gdev->chrdev.owner = THIS_MODULE;
2553 	gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2554 
2555 	ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2556 	if (ret)
2557 		return ret;
2558 
2559 	chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2560 		 MAJOR(devt), gdev->id);
2561 
2562 	return 0;
2563 }
2564 
2565 void gpiolib_cdev_unregister(struct gpio_device *gdev)
2566 {
2567 	cdev_device_del(&gdev->chrdev, &gdev->dev);
2568 }
2569