xref: /openbmc/linux/drivers/gpio/gpiolib-cdev.c (revision aad955842d1cdf56d31e600112137d82fd431140)
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/device.h>
10 #include <linux/err.h>
11 #include <linux/file.h>
12 #include <linux/gpio.h>
13 #include <linux/gpio/driver.h>
14 #include <linux/interrupt.h>
15 #include <linux/irqreturn.h>
16 #include <linux/kernel.h>
17 #include <linux/kfifo.h>
18 #include <linux/module.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/poll.h>
21 #include <linux/spinlock.h>
22 #include <linux/timekeeping.h>
23 #include <linux/uaccess.h>
24 #include <uapi/linux/gpio.h>
25 
26 #include "gpiolib.h"
27 #include "gpiolib-cdev.h"
28 
29 /*
30  * Array sizes must ensure 64-bit alignment and not create holes in the
31  * struct packing.
32  */
33 static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2));
34 static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8));
35 
36 /*
37  * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility
38  */
39 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8));
40 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8));
41 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8));
42 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8));
43 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8));
44 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8));
45 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8));
46 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8));
47 
48 /* Character device interface to GPIO.
49  *
50  * The GPIO character device, /dev/gpiochipN, provides userspace an
51  * interface to gpiolib GPIOs via ioctl()s.
52  */
53 
54 /*
55  * GPIO line handle management
56  */
57 
58 #ifdef CONFIG_GPIO_CDEV_V1
59 /**
60  * struct linehandle_state - contains the state of a userspace handle
61  * @gdev: the GPIO device the handle pertains to
62  * @label: consumer label used to tag descriptors
63  * @descs: the GPIO descriptors held by this handle
64  * @num_descs: the number of descriptors held in the descs array
65  */
66 struct linehandle_state {
67 	struct gpio_device *gdev;
68 	const char *label;
69 	struct gpio_desc *descs[GPIOHANDLES_MAX];
70 	u32 num_descs;
71 };
72 
73 #define GPIOHANDLE_REQUEST_VALID_FLAGS \
74 	(GPIOHANDLE_REQUEST_INPUT | \
75 	GPIOHANDLE_REQUEST_OUTPUT | \
76 	GPIOHANDLE_REQUEST_ACTIVE_LOW | \
77 	GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
78 	GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
79 	GPIOHANDLE_REQUEST_BIAS_DISABLE | \
80 	GPIOHANDLE_REQUEST_OPEN_DRAIN | \
81 	GPIOHANDLE_REQUEST_OPEN_SOURCE)
82 
83 static int linehandle_validate_flags(u32 flags)
84 {
85 	/* Return an error if an unknown flag is set */
86 	if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
87 		return -EINVAL;
88 
89 	/*
90 	 * Do not allow both INPUT & OUTPUT flags to be set as they are
91 	 * contradictory.
92 	 */
93 	if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
94 	    (flags & GPIOHANDLE_REQUEST_OUTPUT))
95 		return -EINVAL;
96 
97 	/*
98 	 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
99 	 * the hardware actually supports enabling both at the same time the
100 	 * electrical result would be disastrous.
101 	 */
102 	if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
103 	    (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
104 		return -EINVAL;
105 
106 	/* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
107 	if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
108 	    ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
109 	     (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
110 		return -EINVAL;
111 
112 	/* Bias flags only allowed for input or output mode. */
113 	if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
114 	      (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
115 	    ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
116 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
117 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
118 		return -EINVAL;
119 
120 	/* Only one bias flag can be set. */
121 	if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
122 	     (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
123 		       GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
124 	    ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
125 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
126 		return -EINVAL;
127 
128 	return 0;
129 }
130 
131 static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
132 {
133 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
134 		   lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
135 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
136 		   lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
137 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
138 		   lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
139 	assign_bit(FLAG_PULL_UP, flagsp,
140 		   lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
141 	assign_bit(FLAG_PULL_DOWN, flagsp,
142 		   lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
143 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
144 		   lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
145 }
146 
147 static long linehandle_set_config(struct linehandle_state *lh,
148 				  void __user *ip)
149 {
150 	struct gpiohandle_config gcnf;
151 	struct gpio_desc *desc;
152 	int i, ret;
153 	u32 lflags;
154 
155 	if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
156 		return -EFAULT;
157 
158 	lflags = gcnf.flags;
159 	ret = linehandle_validate_flags(lflags);
160 	if (ret)
161 		return ret;
162 
163 	for (i = 0; i < lh->num_descs; i++) {
164 		desc = lh->descs[i];
165 		linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags);
166 
167 		/*
168 		 * Lines have to be requested explicitly for input
169 		 * or output, else the line will be treated "as is".
170 		 */
171 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
172 			int val = !!gcnf.default_values[i];
173 
174 			ret = gpiod_direction_output(desc, val);
175 			if (ret)
176 				return ret;
177 		} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
178 			ret = gpiod_direction_input(desc);
179 			if (ret)
180 				return ret;
181 		}
182 
183 		blocking_notifier_call_chain(&desc->gdev->notifier,
184 					     GPIO_V2_LINE_CHANGED_CONFIG,
185 					     desc);
186 	}
187 	return 0;
188 }
189 
190 static long linehandle_ioctl(struct file *file, unsigned int cmd,
191 			     unsigned long arg)
192 {
193 	struct linehandle_state *lh = file->private_data;
194 	void __user *ip = (void __user *)arg;
195 	struct gpiohandle_data ghd;
196 	DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
197 	int i;
198 
199 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
200 		/* NOTE: It's ok to read values of output lines. */
201 		int ret = gpiod_get_array_value_complex(false,
202 							true,
203 							lh->num_descs,
204 							lh->descs,
205 							NULL,
206 							vals);
207 		if (ret)
208 			return ret;
209 
210 		memset(&ghd, 0, sizeof(ghd));
211 		for (i = 0; i < lh->num_descs; i++)
212 			ghd.values[i] = test_bit(i, vals);
213 
214 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
215 			return -EFAULT;
216 
217 		return 0;
218 	} else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
219 		/*
220 		 * All line descriptors were created at once with the same
221 		 * flags so just check if the first one is really output.
222 		 */
223 		if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
224 			return -EPERM;
225 
226 		if (copy_from_user(&ghd, ip, sizeof(ghd)))
227 			return -EFAULT;
228 
229 		/* Clamp all values to [0,1] */
230 		for (i = 0; i < lh->num_descs; i++)
231 			__assign_bit(i, vals, ghd.values[i]);
232 
233 		/* Reuse the array setting function */
234 		return gpiod_set_array_value_complex(false,
235 						     true,
236 						     lh->num_descs,
237 						     lh->descs,
238 						     NULL,
239 						     vals);
240 	} else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) {
241 		return linehandle_set_config(lh, ip);
242 	}
243 	return -EINVAL;
244 }
245 
246 #ifdef CONFIG_COMPAT
247 static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
248 				    unsigned long arg)
249 {
250 	return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
251 }
252 #endif
253 
254 static void linehandle_free(struct linehandle_state *lh)
255 {
256 	int i;
257 
258 	for (i = 0; i < lh->num_descs; i++)
259 		if (lh->descs[i])
260 			gpiod_free(lh->descs[i]);
261 	kfree(lh->label);
262 	put_device(&lh->gdev->dev);
263 	kfree(lh);
264 }
265 
266 static int linehandle_release(struct inode *inode, struct file *file)
267 {
268 	linehandle_free(file->private_data);
269 	return 0;
270 }
271 
272 static const struct file_operations linehandle_fileops = {
273 	.release = linehandle_release,
274 	.owner = THIS_MODULE,
275 	.llseek = noop_llseek,
276 	.unlocked_ioctl = linehandle_ioctl,
277 #ifdef CONFIG_COMPAT
278 	.compat_ioctl = linehandle_ioctl_compat,
279 #endif
280 };
281 
282 static int linehandle_create(struct gpio_device *gdev, void __user *ip)
283 {
284 	struct gpiohandle_request handlereq;
285 	struct linehandle_state *lh;
286 	struct file *file;
287 	int fd, i, ret;
288 	u32 lflags;
289 
290 	if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
291 		return -EFAULT;
292 	if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
293 		return -EINVAL;
294 
295 	lflags = handlereq.flags;
296 
297 	ret = linehandle_validate_flags(lflags);
298 	if (ret)
299 		return ret;
300 
301 	lh = kzalloc(sizeof(*lh), GFP_KERNEL);
302 	if (!lh)
303 		return -ENOMEM;
304 	lh->gdev = gdev;
305 	get_device(&gdev->dev);
306 
307 	/* Make sure this is terminated */
308 	handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
309 	if (strlen(handlereq.consumer_label)) {
310 		lh->label = kstrdup(handlereq.consumer_label,
311 				    GFP_KERNEL);
312 		if (!lh->label) {
313 			ret = -ENOMEM;
314 			goto out_free_lh;
315 		}
316 	}
317 
318 	lh->num_descs = handlereq.lines;
319 
320 	/* Request each GPIO */
321 	for (i = 0; i < handlereq.lines; i++) {
322 		u32 offset = handlereq.lineoffsets[i];
323 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
324 
325 		if (IS_ERR(desc)) {
326 			ret = PTR_ERR(desc);
327 			goto out_free_lh;
328 		}
329 
330 		ret = gpiod_request(desc, lh->label);
331 		if (ret)
332 			goto out_free_lh;
333 		lh->descs[i] = desc;
334 		linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
335 
336 		ret = gpiod_set_transitory(desc, false);
337 		if (ret < 0)
338 			goto out_free_lh;
339 
340 		/*
341 		 * Lines have to be requested explicitly for input
342 		 * or output, else the line will be treated "as is".
343 		 */
344 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
345 			int val = !!handlereq.default_values[i];
346 
347 			ret = gpiod_direction_output(desc, val);
348 			if (ret)
349 				goto out_free_lh;
350 		} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
351 			ret = gpiod_direction_input(desc);
352 			if (ret)
353 				goto out_free_lh;
354 		}
355 
356 		blocking_notifier_call_chain(&desc->gdev->notifier,
357 					     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
358 
359 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
360 			offset);
361 	}
362 
363 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
364 	if (fd < 0) {
365 		ret = fd;
366 		goto out_free_lh;
367 	}
368 
369 	file = anon_inode_getfile("gpio-linehandle",
370 				  &linehandle_fileops,
371 				  lh,
372 				  O_RDONLY | O_CLOEXEC);
373 	if (IS_ERR(file)) {
374 		ret = PTR_ERR(file);
375 		goto out_put_unused_fd;
376 	}
377 
378 	handlereq.fd = fd;
379 	if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
380 		/*
381 		 * fput() will trigger the release() callback, so do not go onto
382 		 * the regular error cleanup path here.
383 		 */
384 		fput(file);
385 		put_unused_fd(fd);
386 		return -EFAULT;
387 	}
388 
389 	fd_install(fd, file);
390 
391 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
392 		lh->num_descs);
393 
394 	return 0;
395 
396 out_put_unused_fd:
397 	put_unused_fd(fd);
398 out_free_lh:
399 	linehandle_free(lh);
400 	return ret;
401 }
402 #endif /* CONFIG_GPIO_CDEV_V1 */
403 
404 /**
405  * struct line - contains the state of a requested line
406  * @desc: the GPIO descriptor for this line.
407  */
408 struct line {
409 	struct gpio_desc *desc;
410 };
411 
412 /**
413  * struct linereq - contains the state of a userspace line request
414  * @gdev: the GPIO device the line request pertains to
415  * @label: consumer label used to tag GPIO descriptors
416  * @num_lines: the number of lines in the lines array
417  * @lines: the lines held by this line request, with @num_lines elements.
418  */
419 struct linereq {
420 	struct gpio_device *gdev;
421 	const char *label;
422 	u32 num_lines;
423 	struct line lines[];
424 };
425 
426 #define GPIO_V2_LINE_BIAS_FLAGS \
427 	(GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
428 	 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
429 	 GPIO_V2_LINE_FLAG_BIAS_DISABLED)
430 
431 #define GPIO_V2_LINE_DIRECTION_FLAGS \
432 	(GPIO_V2_LINE_FLAG_INPUT | \
433 	 GPIO_V2_LINE_FLAG_OUTPUT)
434 
435 #define GPIO_V2_LINE_DRIVE_FLAGS \
436 	(GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
437 	 GPIO_V2_LINE_FLAG_OPEN_SOURCE)
438 
439 #define GPIO_V2_LINE_VALID_FLAGS \
440 	(GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
441 	 GPIO_V2_LINE_DIRECTION_FLAGS | \
442 	 GPIO_V2_LINE_DRIVE_FLAGS | \
443 	 GPIO_V2_LINE_BIAS_FLAGS)
444 
445 static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
446 				     unsigned int line_idx)
447 {
448 	unsigned int i;
449 	u64 mask = BIT_ULL(line_idx);
450 
451 	for (i = 0; i < lc->num_attrs; i++) {
452 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
453 		    (lc->attrs[i].mask & mask))
454 			return lc->attrs[i].attr.flags;
455 	}
456 	return lc->flags;
457 }
458 
459 static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
460 					    unsigned int line_idx)
461 {
462 	unsigned int i;
463 	u64 mask = BIT_ULL(line_idx);
464 
465 	for (i = 0; i < lc->num_attrs; i++) {
466 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
467 		    (lc->attrs[i].mask & mask))
468 			return !!(lc->attrs[i].attr.values & mask);
469 	}
470 	return 0;
471 }
472 
473 static int gpio_v2_line_flags_validate(u64 flags)
474 {
475 	/* Return an error if an unknown flag is set */
476 	if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
477 		return -EINVAL;
478 
479 	/*
480 	 * Do not allow both INPUT and OUTPUT flags to be set as they are
481 	 * contradictory.
482 	 */
483 	if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
484 	    (flags & GPIO_V2_LINE_FLAG_OUTPUT))
485 		return -EINVAL;
486 
487 	/*
488 	 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
489 	 * request. If the hardware actually supports enabling both at the
490 	 * same time the electrical result would be disastrous.
491 	 */
492 	if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
493 	    (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
494 		return -EINVAL;
495 
496 	/* Drive requires explicit output direction. */
497 	if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
498 	    !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
499 		return -EINVAL;
500 
501 	/* Bias requires explicit direction. */
502 	if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
503 	    !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
504 		return -EINVAL;
505 
506 	/* Only one bias flag can be set. */
507 	if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
508 	     (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
509 		       GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
510 	    ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
511 	     (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
512 		return -EINVAL;
513 
514 	return 0;
515 }
516 
517 static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
518 					unsigned int num_lines)
519 {
520 	unsigned int i;
521 	u64 flags;
522 	int ret;
523 
524 	if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
525 		return -EINVAL;
526 
527 	if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
528 		return -EINVAL;
529 
530 	for (i = 0; i < num_lines; i++) {
531 		flags = gpio_v2_line_config_flags(lc, i);
532 		ret = gpio_v2_line_flags_validate(flags);
533 		if (ret)
534 			return ret;
535 	}
536 	return 0;
537 }
538 
539 static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
540 						    unsigned long *flagsp)
541 {
542 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
543 		   flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
544 
545 	if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
546 		set_bit(FLAG_IS_OUT, flagsp);
547 	else if (flags & GPIO_V2_LINE_FLAG_INPUT)
548 		clear_bit(FLAG_IS_OUT, flagsp);
549 
550 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
551 		   flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
552 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
553 		   flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
554 
555 	assign_bit(FLAG_PULL_UP, flagsp,
556 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
557 	assign_bit(FLAG_PULL_DOWN, flagsp,
558 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
559 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
560 		   flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
561 }
562 
563 static long linereq_get_values(struct linereq *lr, void __user *ip)
564 {
565 	struct gpio_v2_line_values lv;
566 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
567 	struct gpio_desc **descs;
568 	unsigned int i, didx, num_get;
569 	int ret;
570 
571 	/* NOTE: It's ok to read values of output lines. */
572 	if (copy_from_user(&lv, ip, sizeof(lv)))
573 		return -EFAULT;
574 
575 	for (num_get = 0, i = 0; i < lr->num_lines; i++) {
576 		if (lv.mask & BIT_ULL(i)) {
577 			num_get++;
578 			descs = &lr->lines[i].desc;
579 		}
580 	}
581 
582 	if (num_get == 0)
583 		return -EINVAL;
584 
585 	if (num_get != 1) {
586 		descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
587 		if (!descs)
588 			return -ENOMEM;
589 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
590 			if (lv.mask & BIT_ULL(i)) {
591 				descs[didx] = lr->lines[i].desc;
592 				didx++;
593 			}
594 		}
595 	}
596 	ret = gpiod_get_array_value_complex(false, true, num_get,
597 					    descs, NULL, vals);
598 
599 	if (num_get != 1)
600 		kfree(descs);
601 	if (ret)
602 		return ret;
603 
604 	lv.bits = 0;
605 	for (didx = 0, i = 0; i < lr->num_lines; i++) {
606 		if (lv.mask & BIT_ULL(i)) {
607 			if (test_bit(didx, vals))
608 				lv.bits |= BIT_ULL(i);
609 			didx++;
610 		}
611 	}
612 
613 	if (copy_to_user(ip, &lv, sizeof(lv)))
614 		return -EFAULT;
615 
616 	return 0;
617 }
618 
619 static long linereq_ioctl(struct file *file, unsigned int cmd,
620 			  unsigned long arg)
621 {
622 	struct linereq *lr = file->private_data;
623 	void __user *ip = (void __user *)arg;
624 
625 	if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL)
626 		return linereq_get_values(lr, ip);
627 
628 	return -EINVAL;
629 }
630 
631 #ifdef CONFIG_COMPAT
632 static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
633 				 unsigned long arg)
634 {
635 	return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
636 }
637 #endif
638 
639 static void linereq_free(struct linereq *lr)
640 {
641 	unsigned int i;
642 
643 	for (i = 0; i < lr->num_lines; i++) {
644 		if (lr->lines[i].desc)
645 			gpiod_free(lr->lines[i].desc);
646 	}
647 	kfree(lr->label);
648 	put_device(&lr->gdev->dev);
649 	kfree(lr);
650 }
651 
652 static int linereq_release(struct inode *inode, struct file *file)
653 {
654 	struct linereq *lr = file->private_data;
655 
656 	linereq_free(lr);
657 	return 0;
658 }
659 
660 static const struct file_operations line_fileops = {
661 	.release = linereq_release,
662 	.owner = THIS_MODULE,
663 	.llseek = noop_llseek,
664 	.unlocked_ioctl = linereq_ioctl,
665 #ifdef CONFIG_COMPAT
666 	.compat_ioctl = linereq_ioctl_compat,
667 #endif
668 };
669 
670 static int linereq_create(struct gpio_device *gdev, void __user *ip)
671 {
672 	struct gpio_v2_line_request ulr;
673 	struct gpio_v2_line_config *lc;
674 	struct linereq *lr;
675 	struct file *file;
676 	u64 flags;
677 	unsigned int i;
678 	int fd, ret;
679 
680 	if (copy_from_user(&ulr, ip, sizeof(ulr)))
681 		return -EFAULT;
682 
683 	if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
684 		return -EINVAL;
685 
686 	if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
687 		return -EINVAL;
688 
689 	lc = &ulr.config;
690 	ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
691 	if (ret)
692 		return ret;
693 
694 	lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
695 	if (!lr)
696 		return -ENOMEM;
697 
698 	lr->gdev = gdev;
699 	get_device(&gdev->dev);
700 
701 	/* Make sure this is terminated */
702 	ulr.consumer[sizeof(ulr.consumer)-1] = '\0';
703 	if (strlen(ulr.consumer)) {
704 		/* label is only initialized if consumer is set */
705 		lr->label = kstrdup(ulr.consumer, GFP_KERNEL);
706 		if (!lr->label) {
707 			ret = -ENOMEM;
708 			goto out_free_linereq;
709 		}
710 	}
711 
712 	lr->num_lines = ulr.num_lines;
713 
714 	/* Request each GPIO */
715 	for (i = 0; i < ulr.num_lines; i++) {
716 		u32 offset = ulr.offsets[i];
717 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
718 
719 		if (IS_ERR(desc)) {
720 			ret = PTR_ERR(desc);
721 			goto out_free_linereq;
722 		}
723 
724 		ret = gpiod_request(desc, lr->label);
725 		if (ret)
726 			goto out_free_linereq;
727 
728 		lr->lines[i].desc = desc;
729 		flags = gpio_v2_line_config_flags(lc, i);
730 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
731 
732 		ret = gpiod_set_transitory(desc, false);
733 		if (ret < 0)
734 			goto out_free_linereq;
735 
736 		/*
737 		 * Lines have to be requested explicitly for input
738 		 * or output, else the line will be treated "as is".
739 		 */
740 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
741 			int val = gpio_v2_line_config_output_value(lc, i);
742 
743 			ret = gpiod_direction_output(desc, val);
744 			if (ret)
745 				goto out_free_linereq;
746 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
747 			ret = gpiod_direction_input(desc);
748 			if (ret)
749 				goto out_free_linereq;
750 		}
751 
752 		blocking_notifier_call_chain(&desc->gdev->notifier,
753 					     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
754 
755 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
756 			offset);
757 	}
758 
759 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
760 	if (fd < 0) {
761 		ret = fd;
762 		goto out_free_linereq;
763 	}
764 
765 	file = anon_inode_getfile("gpio-line", &line_fileops, lr,
766 				  O_RDONLY | O_CLOEXEC);
767 	if (IS_ERR(file)) {
768 		ret = PTR_ERR(file);
769 		goto out_put_unused_fd;
770 	}
771 
772 	ulr.fd = fd;
773 	if (copy_to_user(ip, &ulr, sizeof(ulr))) {
774 		/*
775 		 * fput() will trigger the release() callback, so do not go onto
776 		 * the regular error cleanup path here.
777 		 */
778 		fput(file);
779 		put_unused_fd(fd);
780 		return -EFAULT;
781 	}
782 
783 	fd_install(fd, file);
784 
785 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
786 		lr->num_lines);
787 
788 	return 0;
789 
790 out_put_unused_fd:
791 	put_unused_fd(fd);
792 out_free_linereq:
793 	linereq_free(lr);
794 	return ret;
795 }
796 
797 #ifdef CONFIG_GPIO_CDEV_V1
798 
799 /*
800  * GPIO line event management
801  */
802 
803 /**
804  * struct lineevent_state - contains the state of a userspace event
805  * @gdev: the GPIO device the event pertains to
806  * @label: consumer label used to tag descriptors
807  * @desc: the GPIO descriptor held by this event
808  * @eflags: the event flags this line was requested with
809  * @irq: the interrupt that trigger in response to events on this GPIO
810  * @wait: wait queue that handles blocking reads of events
811  * @events: KFIFO for the GPIO events
812  * @timestamp: cache for the timestamp storing it between hardirq
813  * and IRQ thread, used to bring the timestamp close to the actual
814  * event
815  */
816 struct lineevent_state {
817 	struct gpio_device *gdev;
818 	const char *label;
819 	struct gpio_desc *desc;
820 	u32 eflags;
821 	int irq;
822 	wait_queue_head_t wait;
823 	DECLARE_KFIFO(events, struct gpioevent_data, 16);
824 	u64 timestamp;
825 };
826 
827 #define GPIOEVENT_REQUEST_VALID_FLAGS \
828 	(GPIOEVENT_REQUEST_RISING_EDGE | \
829 	GPIOEVENT_REQUEST_FALLING_EDGE)
830 
831 static __poll_t lineevent_poll(struct file *file,
832 			       struct poll_table_struct *wait)
833 {
834 	struct lineevent_state *le = file->private_data;
835 	__poll_t events = 0;
836 
837 	poll_wait(file, &le->wait, wait);
838 
839 	if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
840 		events = EPOLLIN | EPOLLRDNORM;
841 
842 	return events;
843 }
844 
845 
846 static ssize_t lineevent_read(struct file *file,
847 			      char __user *buf,
848 			      size_t count,
849 			      loff_t *f_ps)
850 {
851 	struct lineevent_state *le = file->private_data;
852 	struct gpioevent_data ge;
853 	ssize_t bytes_read = 0;
854 	int ret;
855 
856 	if (count < sizeof(ge))
857 		return -EINVAL;
858 
859 	do {
860 		spin_lock(&le->wait.lock);
861 		if (kfifo_is_empty(&le->events)) {
862 			if (bytes_read) {
863 				spin_unlock(&le->wait.lock);
864 				return bytes_read;
865 			}
866 
867 			if (file->f_flags & O_NONBLOCK) {
868 				spin_unlock(&le->wait.lock);
869 				return -EAGAIN;
870 			}
871 
872 			ret = wait_event_interruptible_locked(le->wait,
873 					!kfifo_is_empty(&le->events));
874 			if (ret) {
875 				spin_unlock(&le->wait.lock);
876 				return ret;
877 			}
878 		}
879 
880 		ret = kfifo_out(&le->events, &ge, 1);
881 		spin_unlock(&le->wait.lock);
882 		if (ret != 1) {
883 			/*
884 			 * This should never happen - we were holding the lock
885 			 * from the moment we learned the fifo is no longer
886 			 * empty until now.
887 			 */
888 			ret = -EIO;
889 			break;
890 		}
891 
892 		if (copy_to_user(buf + bytes_read, &ge, sizeof(ge)))
893 			return -EFAULT;
894 		bytes_read += sizeof(ge);
895 	} while (count >= bytes_read + sizeof(ge));
896 
897 	return bytes_read;
898 }
899 
900 static void lineevent_free(struct lineevent_state *le)
901 {
902 	if (le->irq)
903 		free_irq(le->irq, le);
904 	if (le->desc)
905 		gpiod_free(le->desc);
906 	kfree(le->label);
907 	put_device(&le->gdev->dev);
908 	kfree(le);
909 }
910 
911 static int lineevent_release(struct inode *inode, struct file *file)
912 {
913 	lineevent_free(file->private_data);
914 	return 0;
915 }
916 
917 static long lineevent_ioctl(struct file *file, unsigned int cmd,
918 			    unsigned long arg)
919 {
920 	struct lineevent_state *le = file->private_data;
921 	void __user *ip = (void __user *)arg;
922 	struct gpiohandle_data ghd;
923 
924 	/*
925 	 * We can get the value for an event line but not set it,
926 	 * because it is input by definition.
927 	 */
928 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
929 		int val;
930 
931 		memset(&ghd, 0, sizeof(ghd));
932 
933 		val = gpiod_get_value_cansleep(le->desc);
934 		if (val < 0)
935 			return val;
936 		ghd.values[0] = val;
937 
938 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
939 			return -EFAULT;
940 
941 		return 0;
942 	}
943 	return -EINVAL;
944 }
945 
946 #ifdef CONFIG_COMPAT
947 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
948 				   unsigned long arg)
949 {
950 	return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
951 }
952 #endif
953 
954 static const struct file_operations lineevent_fileops = {
955 	.release = lineevent_release,
956 	.read = lineevent_read,
957 	.poll = lineevent_poll,
958 	.owner = THIS_MODULE,
959 	.llseek = noop_llseek,
960 	.unlocked_ioctl = lineevent_ioctl,
961 #ifdef CONFIG_COMPAT
962 	.compat_ioctl = lineevent_ioctl_compat,
963 #endif
964 };
965 
966 static irqreturn_t lineevent_irq_thread(int irq, void *p)
967 {
968 	struct lineevent_state *le = p;
969 	struct gpioevent_data ge;
970 	int ret;
971 
972 	/* Do not leak kernel stack to userspace */
973 	memset(&ge, 0, sizeof(ge));
974 
975 	/*
976 	 * We may be running from a nested threaded interrupt in which case
977 	 * we didn't get the timestamp from lineevent_irq_handler().
978 	 */
979 	if (!le->timestamp)
980 		ge.timestamp = ktime_get_ns();
981 	else
982 		ge.timestamp = le->timestamp;
983 
984 	if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
985 	    && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
986 		int level = gpiod_get_value_cansleep(le->desc);
987 
988 		if (level)
989 			/* Emit low-to-high event */
990 			ge.id = GPIOEVENT_EVENT_RISING_EDGE;
991 		else
992 			/* Emit high-to-low event */
993 			ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
994 	} else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
995 		/* Emit low-to-high event */
996 		ge.id = GPIOEVENT_EVENT_RISING_EDGE;
997 	} else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
998 		/* Emit high-to-low event */
999 		ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1000 	} else {
1001 		return IRQ_NONE;
1002 	}
1003 
1004 	ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
1005 					    1, &le->wait.lock);
1006 	if (ret)
1007 		wake_up_poll(&le->wait, EPOLLIN);
1008 	else
1009 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
1010 
1011 	return IRQ_HANDLED;
1012 }
1013 
1014 static irqreturn_t lineevent_irq_handler(int irq, void *p)
1015 {
1016 	struct lineevent_state *le = p;
1017 
1018 	/*
1019 	 * Just store the timestamp in hardirq context so we get it as
1020 	 * close in time as possible to the actual event.
1021 	 */
1022 	le->timestamp = ktime_get_ns();
1023 
1024 	return IRQ_WAKE_THREAD;
1025 }
1026 
1027 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1028 {
1029 	struct gpioevent_request eventreq;
1030 	struct lineevent_state *le;
1031 	struct gpio_desc *desc;
1032 	struct file *file;
1033 	u32 offset;
1034 	u32 lflags;
1035 	u32 eflags;
1036 	int fd;
1037 	int ret;
1038 	int irq, irqflags = 0;
1039 
1040 	if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1041 		return -EFAULT;
1042 
1043 	offset = eventreq.lineoffset;
1044 	lflags = eventreq.handleflags;
1045 	eflags = eventreq.eventflags;
1046 
1047 	desc = gpiochip_get_desc(gdev->chip, offset);
1048 	if (IS_ERR(desc))
1049 		return PTR_ERR(desc);
1050 
1051 	/* Return an error if a unknown flag is set */
1052 	if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1053 	    (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1054 		return -EINVAL;
1055 
1056 	/* This is just wrong: we don't look for events on output lines */
1057 	if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1058 	    (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1059 	    (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1060 		return -EINVAL;
1061 
1062 	/* Only one bias flag can be set. */
1063 	if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1064 	     (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1065 			GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1066 	    ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1067 	     (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1068 		return -EINVAL;
1069 
1070 	le = kzalloc(sizeof(*le), GFP_KERNEL);
1071 	if (!le)
1072 		return -ENOMEM;
1073 	le->gdev = gdev;
1074 	get_device(&gdev->dev);
1075 
1076 	/* Make sure this is terminated */
1077 	eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
1078 	if (strlen(eventreq.consumer_label)) {
1079 		le->label = kstrdup(eventreq.consumer_label,
1080 				    GFP_KERNEL);
1081 		if (!le->label) {
1082 			ret = -ENOMEM;
1083 			goto out_free_le;
1084 		}
1085 	}
1086 
1087 	ret = gpiod_request(desc, le->label);
1088 	if (ret)
1089 		goto out_free_le;
1090 	le->desc = desc;
1091 	le->eflags = eflags;
1092 
1093 	linehandle_flags_to_desc_flags(lflags, &desc->flags);
1094 
1095 	ret = gpiod_direction_input(desc);
1096 	if (ret)
1097 		goto out_free_le;
1098 
1099 	blocking_notifier_call_chain(&desc->gdev->notifier,
1100 				     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1101 
1102 	irq = gpiod_to_irq(desc);
1103 	if (irq <= 0) {
1104 		ret = -ENODEV;
1105 		goto out_free_le;
1106 	}
1107 	le->irq = irq;
1108 
1109 	if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
1110 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1111 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1112 	if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
1113 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1114 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1115 	irqflags |= IRQF_ONESHOT;
1116 
1117 	INIT_KFIFO(le->events);
1118 	init_waitqueue_head(&le->wait);
1119 
1120 	/* Request a thread to read the events */
1121 	ret = request_threaded_irq(le->irq,
1122 				   lineevent_irq_handler,
1123 				   lineevent_irq_thread,
1124 				   irqflags,
1125 				   le->label,
1126 				   le);
1127 	if (ret)
1128 		goto out_free_le;
1129 
1130 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1131 	if (fd < 0) {
1132 		ret = fd;
1133 		goto out_free_le;
1134 	}
1135 
1136 	file = anon_inode_getfile("gpio-event",
1137 				  &lineevent_fileops,
1138 				  le,
1139 				  O_RDONLY | O_CLOEXEC);
1140 	if (IS_ERR(file)) {
1141 		ret = PTR_ERR(file);
1142 		goto out_put_unused_fd;
1143 	}
1144 
1145 	eventreq.fd = fd;
1146 	if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
1147 		/*
1148 		 * fput() will trigger the release() callback, so do not go onto
1149 		 * the regular error cleanup path here.
1150 		 */
1151 		fput(file);
1152 		put_unused_fd(fd);
1153 		return -EFAULT;
1154 	}
1155 
1156 	fd_install(fd, file);
1157 
1158 	return 0;
1159 
1160 out_put_unused_fd:
1161 	put_unused_fd(fd);
1162 out_free_le:
1163 	lineevent_free(le);
1164 	return ret;
1165 }
1166 
1167 static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
1168 				    struct gpioline_info *info_v1)
1169 {
1170 	u64 flagsv2 = info_v2->flags;
1171 
1172 	memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
1173 	memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
1174 	info_v1->line_offset = info_v2->offset;
1175 	info_v1->flags = 0;
1176 
1177 	if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
1178 		info_v1->flags |= GPIOLINE_FLAG_KERNEL;
1179 
1180 	if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
1181 		info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
1182 
1183 	if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
1184 		info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
1185 
1186 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
1187 		info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
1188 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
1189 		info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
1190 
1191 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
1192 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
1193 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
1194 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
1195 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
1196 		info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
1197 }
1198 
1199 static void gpio_v2_line_info_changed_to_v1(
1200 		struct gpio_v2_line_info_changed *lic_v2,
1201 		struct gpioline_info_changed *lic_v1)
1202 {
1203 	gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
1204 	lic_v1->timestamp = lic_v2->timestamp_ns;
1205 	lic_v1->event_type = lic_v2->event_type;
1206 }
1207 
1208 #endif /* CONFIG_GPIO_CDEV_V1 */
1209 
1210 static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
1211 				  struct gpio_v2_line_info *info)
1212 {
1213 	struct gpio_chip *gc = desc->gdev->chip;
1214 	bool ok_for_pinctrl;
1215 	unsigned long flags;
1216 
1217 	memset(info, 0, sizeof(*info));
1218 	info->offset = gpio_chip_hwgpio(desc);
1219 
1220 	/*
1221 	 * This function takes a mutex so we must check this before taking
1222 	 * the spinlock.
1223 	 *
1224 	 * FIXME: find a non-racy way to retrieve this information. Maybe a
1225 	 * lock common to both frameworks?
1226 	 */
1227 	ok_for_pinctrl =
1228 		pinctrl_gpio_can_use_line(gc->base + info->offset);
1229 
1230 	spin_lock_irqsave(&gpio_lock, flags);
1231 
1232 	if (desc->name)
1233 		strscpy(info->name, desc->name, sizeof(info->name));
1234 
1235 	if (desc->label)
1236 		strscpy(info->consumer, desc->label, sizeof(info->consumer));
1237 
1238 	/*
1239 	 * Userspace only need to know that the kernel is using this GPIO so
1240 	 * it can't use it.
1241 	 */
1242 	info->flags = 0;
1243 	if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1244 	    test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1245 	    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1246 	    test_bit(FLAG_EXPORT, &desc->flags) ||
1247 	    test_bit(FLAG_SYSFS, &desc->flags) ||
1248 	    !ok_for_pinctrl)
1249 		info->flags |= GPIO_V2_LINE_FLAG_USED;
1250 
1251 	if (test_bit(FLAG_IS_OUT, &desc->flags))
1252 		info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
1253 	else
1254 		info->flags |= GPIO_V2_LINE_FLAG_INPUT;
1255 
1256 	if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1257 		info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
1258 
1259 	if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1260 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
1261 	if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1262 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
1263 
1264 	if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
1265 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
1266 	if (test_bit(FLAG_PULL_DOWN, &desc->flags))
1267 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
1268 	if (test_bit(FLAG_PULL_UP, &desc->flags))
1269 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
1270 
1271 	spin_unlock_irqrestore(&gpio_lock, flags);
1272 }
1273 
1274 struct gpio_chardev_data {
1275 	struct gpio_device *gdev;
1276 	wait_queue_head_t wait;
1277 	DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
1278 	struct notifier_block lineinfo_changed_nb;
1279 	unsigned long *watched_lines;
1280 #ifdef CONFIG_GPIO_CDEV_V1
1281 	atomic_t watch_abi_version;
1282 #endif
1283 };
1284 
1285 #ifdef CONFIG_GPIO_CDEV_V1
1286 /*
1287  * returns 0 if the versions match, else the previously selected ABI version
1288  */
1289 static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
1290 				       unsigned int version)
1291 {
1292 	int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
1293 
1294 	if (abiv == version)
1295 		return 0;
1296 
1297 	return abiv;
1298 }
1299 #endif
1300 
1301 static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
1302 			bool watch)
1303 {
1304 	struct gpio_desc *desc;
1305 	struct gpio_v2_line_info lineinfo;
1306 
1307 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1308 		return -EFAULT;
1309 
1310 	if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
1311 		return -EINVAL;
1312 
1313 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
1314 	if (IS_ERR(desc))
1315 		return PTR_ERR(desc);
1316 
1317 	if (watch) {
1318 #ifdef CONFIG_GPIO_CDEV_V1
1319 		if (lineinfo_ensure_abi_version(cdev, 2))
1320 			return -EPERM;
1321 #endif
1322 		if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
1323 			return -EBUSY;
1324 	}
1325 	gpio_desc_to_lineinfo(desc, &lineinfo);
1326 
1327 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
1328 		if (watch)
1329 			clear_bit(lineinfo.offset, cdev->watched_lines);
1330 		return -EFAULT;
1331 	}
1332 
1333 	return 0;
1334 }
1335 
1336 /*
1337  * gpio_ioctl() - ioctl handler for the GPIO chardev
1338  */
1339 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1340 {
1341 	struct gpio_chardev_data *cdev = file->private_data;
1342 	struct gpio_device *gdev = cdev->gdev;
1343 	struct gpio_chip *gc = gdev->chip;
1344 	void __user *ip = (void __user *)arg;
1345 	__u32 offset;
1346 
1347 	/* We fail any subsequent ioctl():s when the chip is gone */
1348 	if (!gc)
1349 		return -ENODEV;
1350 
1351 	/* Fill in the struct and pass to userspace */
1352 	if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
1353 		struct gpiochip_info chipinfo;
1354 
1355 		memset(&chipinfo, 0, sizeof(chipinfo));
1356 
1357 		strscpy(chipinfo.name, dev_name(&gdev->dev),
1358 			sizeof(chipinfo.name));
1359 		strscpy(chipinfo.label, gdev->label,
1360 			sizeof(chipinfo.label));
1361 		chipinfo.lines = gdev->ngpio;
1362 		if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1363 			return -EFAULT;
1364 		return 0;
1365 #ifdef CONFIG_GPIO_CDEV_V1
1366 	} else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
1367 		struct gpio_desc *desc;
1368 		struct gpioline_info lineinfo;
1369 		struct gpio_v2_line_info lineinfo_v2;
1370 
1371 		if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1372 			return -EFAULT;
1373 
1374 		/* this doubles as a range check on line_offset */
1375 		desc = gpiochip_get_desc(gc, lineinfo.line_offset);
1376 		if (IS_ERR(desc))
1377 			return PTR_ERR(desc);
1378 
1379 		gpio_desc_to_lineinfo(desc, &lineinfo_v2);
1380 		gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
1381 
1382 		if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1383 			return -EFAULT;
1384 		return 0;
1385 	} else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1386 		return linehandle_create(gdev, ip);
1387 	} else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1388 		return lineevent_create(gdev, ip);
1389 	} else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
1390 		struct gpio_desc *desc;
1391 		struct gpioline_info lineinfo;
1392 		struct gpio_v2_line_info lineinfo_v2;
1393 
1394 		if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1395 			return -EFAULT;
1396 
1397 		/* this doubles as a range check on line_offset */
1398 		desc = gpiochip_get_desc(gc, lineinfo.line_offset);
1399 		if (IS_ERR(desc))
1400 			return PTR_ERR(desc);
1401 
1402 		if (lineinfo_ensure_abi_version(cdev, 1))
1403 			return -EPERM;
1404 
1405 		if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
1406 			return -EBUSY;
1407 
1408 		gpio_desc_to_lineinfo(desc, &lineinfo_v2);
1409 		gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
1410 
1411 		if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
1412 			clear_bit(lineinfo.line_offset, cdev->watched_lines);
1413 			return -EFAULT;
1414 		}
1415 
1416 		return 0;
1417 #endif /* CONFIG_GPIO_CDEV_V1 */
1418 	} else if (cmd == GPIO_V2_GET_LINEINFO_IOCTL ||
1419 		   cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL) {
1420 		return lineinfo_get(cdev, ip,
1421 				    cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL);
1422 	} else if (cmd == GPIO_V2_GET_LINE_IOCTL) {
1423 		return linereq_create(gdev, ip);
1424 	} else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) {
1425 		if (copy_from_user(&offset, ip, sizeof(offset)))
1426 			return -EFAULT;
1427 
1428 		if (offset >= cdev->gdev->ngpio)
1429 			return -EINVAL;
1430 
1431 		if (!test_and_clear_bit(offset, cdev->watched_lines))
1432 			return -EBUSY;
1433 
1434 		return 0;
1435 	}
1436 	return -EINVAL;
1437 }
1438 
1439 #ifdef CONFIG_COMPAT
1440 static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
1441 			      unsigned long arg)
1442 {
1443 	return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1444 }
1445 #endif
1446 
1447 static struct gpio_chardev_data *
1448 to_gpio_chardev_data(struct notifier_block *nb)
1449 {
1450 	return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
1451 }
1452 
1453 static int lineinfo_changed_notify(struct notifier_block *nb,
1454 				   unsigned long action, void *data)
1455 {
1456 	struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
1457 	struct gpio_v2_line_info_changed chg;
1458 	struct gpio_desc *desc = data;
1459 	int ret;
1460 
1461 	if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
1462 		return NOTIFY_DONE;
1463 
1464 	memset(&chg, 0, sizeof(chg));
1465 	chg.event_type = action;
1466 	chg.timestamp_ns = ktime_get_ns();
1467 	gpio_desc_to_lineinfo(desc, &chg.info);
1468 
1469 	ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
1470 	if (ret)
1471 		wake_up_poll(&cdev->wait, EPOLLIN);
1472 	else
1473 		pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
1474 
1475 	return NOTIFY_OK;
1476 }
1477 
1478 static __poll_t lineinfo_watch_poll(struct file *file,
1479 				    struct poll_table_struct *pollt)
1480 {
1481 	struct gpio_chardev_data *cdev = file->private_data;
1482 	__poll_t events = 0;
1483 
1484 	poll_wait(file, &cdev->wait, pollt);
1485 
1486 	if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
1487 						 &cdev->wait.lock))
1488 		events = EPOLLIN | EPOLLRDNORM;
1489 
1490 	return events;
1491 }
1492 
1493 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
1494 				   size_t count, loff_t *off)
1495 {
1496 	struct gpio_chardev_data *cdev = file->private_data;
1497 	struct gpio_v2_line_info_changed event;
1498 	ssize_t bytes_read = 0;
1499 	int ret;
1500 	size_t event_size;
1501 
1502 #ifndef CONFIG_GPIO_CDEV_V1
1503 	event_size = sizeof(struct gpio_v2_line_info_changed);
1504 	if (count < event_size)
1505 		return -EINVAL;
1506 #endif
1507 
1508 	do {
1509 		spin_lock(&cdev->wait.lock);
1510 		if (kfifo_is_empty(&cdev->events)) {
1511 			if (bytes_read) {
1512 				spin_unlock(&cdev->wait.lock);
1513 				return bytes_read;
1514 			}
1515 
1516 			if (file->f_flags & O_NONBLOCK) {
1517 				spin_unlock(&cdev->wait.lock);
1518 				return -EAGAIN;
1519 			}
1520 
1521 			ret = wait_event_interruptible_locked(cdev->wait,
1522 					!kfifo_is_empty(&cdev->events));
1523 			if (ret) {
1524 				spin_unlock(&cdev->wait.lock);
1525 				return ret;
1526 			}
1527 		}
1528 #ifdef CONFIG_GPIO_CDEV_V1
1529 		/* must be after kfifo check so watch_abi_version is set */
1530 		if (atomic_read(&cdev->watch_abi_version) == 2)
1531 			event_size = sizeof(struct gpio_v2_line_info_changed);
1532 		else
1533 			event_size = sizeof(struct gpioline_info_changed);
1534 		if (count < event_size) {
1535 			spin_unlock(&cdev->wait.lock);
1536 			return -EINVAL;
1537 		}
1538 #endif
1539 		ret = kfifo_out(&cdev->events, &event, 1);
1540 		spin_unlock(&cdev->wait.lock);
1541 		if (ret != 1) {
1542 			ret = -EIO;
1543 			break;
1544 			/* We should never get here. See lineevent_read(). */
1545 		}
1546 
1547 #ifdef CONFIG_GPIO_CDEV_V1
1548 		if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
1549 			if (copy_to_user(buf + bytes_read, &event, event_size))
1550 				return -EFAULT;
1551 		} else {
1552 			struct gpioline_info_changed event_v1;
1553 
1554 			gpio_v2_line_info_changed_to_v1(&event, &event_v1);
1555 			if (copy_to_user(buf + bytes_read, &event_v1,
1556 					 event_size))
1557 				return -EFAULT;
1558 		}
1559 #else
1560 		if (copy_to_user(buf + bytes_read, &event, event_size))
1561 			return -EFAULT;
1562 #endif
1563 		bytes_read += event_size;
1564 	} while (count >= bytes_read + sizeof(event));
1565 
1566 	return bytes_read;
1567 }
1568 
1569 /**
1570  * gpio_chrdev_open() - open the chardev for ioctl operations
1571  * @inode: inode for this chardev
1572  * @file: file struct for storing private data
1573  * Returns 0 on success
1574  */
1575 static int gpio_chrdev_open(struct inode *inode, struct file *file)
1576 {
1577 	struct gpio_device *gdev = container_of(inode->i_cdev,
1578 						struct gpio_device, chrdev);
1579 	struct gpio_chardev_data *cdev;
1580 	int ret = -ENOMEM;
1581 
1582 	/* Fail on open if the backing gpiochip is gone */
1583 	if (!gdev->chip)
1584 		return -ENODEV;
1585 
1586 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
1587 	if (!cdev)
1588 		return -ENOMEM;
1589 
1590 	cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
1591 	if (!cdev->watched_lines)
1592 		goto out_free_cdev;
1593 
1594 	init_waitqueue_head(&cdev->wait);
1595 	INIT_KFIFO(cdev->events);
1596 	cdev->gdev = gdev;
1597 
1598 	cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
1599 	ret = blocking_notifier_chain_register(&gdev->notifier,
1600 					       &cdev->lineinfo_changed_nb);
1601 	if (ret)
1602 		goto out_free_bitmap;
1603 
1604 	get_device(&gdev->dev);
1605 	file->private_data = cdev;
1606 
1607 	ret = nonseekable_open(inode, file);
1608 	if (ret)
1609 		goto out_unregister_notifier;
1610 
1611 	return ret;
1612 
1613 out_unregister_notifier:
1614 	blocking_notifier_chain_unregister(&gdev->notifier,
1615 					   &cdev->lineinfo_changed_nb);
1616 out_free_bitmap:
1617 	bitmap_free(cdev->watched_lines);
1618 out_free_cdev:
1619 	kfree(cdev);
1620 	return ret;
1621 }
1622 
1623 /**
1624  * gpio_chrdev_release() - close chardev after ioctl operations
1625  * @inode: inode for this chardev
1626  * @file: file struct for storing private data
1627  * Returns 0 on success
1628  */
1629 static int gpio_chrdev_release(struct inode *inode, struct file *file)
1630 {
1631 	struct gpio_chardev_data *cdev = file->private_data;
1632 	struct gpio_device *gdev = cdev->gdev;
1633 
1634 	bitmap_free(cdev->watched_lines);
1635 	blocking_notifier_chain_unregister(&gdev->notifier,
1636 					   &cdev->lineinfo_changed_nb);
1637 	put_device(&gdev->dev);
1638 	kfree(cdev);
1639 
1640 	return 0;
1641 }
1642 
1643 static const struct file_operations gpio_fileops = {
1644 	.release = gpio_chrdev_release,
1645 	.open = gpio_chrdev_open,
1646 	.poll = lineinfo_watch_poll,
1647 	.read = lineinfo_watch_read,
1648 	.owner = THIS_MODULE,
1649 	.llseek = no_llseek,
1650 	.unlocked_ioctl = gpio_ioctl,
1651 #ifdef CONFIG_COMPAT
1652 	.compat_ioctl = gpio_ioctl_compat,
1653 #endif
1654 };
1655 
1656 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
1657 {
1658 	int ret;
1659 
1660 	cdev_init(&gdev->chrdev, &gpio_fileops);
1661 	gdev->chrdev.owner = THIS_MODULE;
1662 	gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
1663 
1664 	ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
1665 	if (ret)
1666 		return ret;
1667 
1668 	chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1669 		 MAJOR(devt), gdev->id);
1670 
1671 	return 0;
1672 }
1673 
1674 void gpiolib_cdev_unregister(struct gpio_device *gdev)
1675 {
1676 	cdev_device_del(&gdev->chrdev, &gdev->dev);
1677 }
1678