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