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