xref: /openbmc/linux/drivers/gpio/gpiolib-cdev.c (revision f97cee494dc92395a668445bcd24d34c89f4ff8c)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/anon_inodes.h>
4 #include <linux/bitmap.h>
5 #include <linux/cdev.h>
6 #include <linux/compat.h>
7 #include <linux/device.h>
8 #include <linux/err.h>
9 #include <linux/file.h>
10 #include <linux/gpio.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/interrupt.h>
13 #include <linux/irqreturn.h>
14 #include <linux/kernel.h>
15 #include <linux/kfifo.h>
16 #include <linux/module.h>
17 #include <linux/pinctrl/consumer.h>
18 #include <linux/poll.h>
19 #include <linux/spinlock.h>
20 #include <linux/timekeeping.h>
21 #include <linux/uaccess.h>
22 #include <uapi/linux/gpio.h>
23 
24 #include "gpiolib.h"
25 #include "gpiolib-cdev.h"
26 
27 /* Character device interface to GPIO.
28  *
29  * The GPIO character device, /dev/gpiochipN, provides userspace an
30  * interface to gpiolib GPIOs via ioctl()s.
31  */
32 
33 /*
34  * GPIO line handle management
35  */
36 
37 /**
38  * struct linehandle_state - contains the state of a userspace handle
39  * @gdev: the GPIO device the handle pertains to
40  * @label: consumer label used to tag descriptors
41  * @descs: the GPIO descriptors held by this handle
42  * @num_descs: the number of descriptors held in the descs array
43  */
44 struct linehandle_state {
45 	struct gpio_device *gdev;
46 	const char *label;
47 	struct gpio_desc *descs[GPIOHANDLES_MAX];
48 	u32 num_descs;
49 };
50 
51 #define GPIOHANDLE_REQUEST_VALID_FLAGS \
52 	(GPIOHANDLE_REQUEST_INPUT | \
53 	GPIOHANDLE_REQUEST_OUTPUT | \
54 	GPIOHANDLE_REQUEST_ACTIVE_LOW | \
55 	GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
56 	GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
57 	GPIOHANDLE_REQUEST_BIAS_DISABLE | \
58 	GPIOHANDLE_REQUEST_OPEN_DRAIN | \
59 	GPIOHANDLE_REQUEST_OPEN_SOURCE)
60 
61 static int linehandle_validate_flags(u32 flags)
62 {
63 	/* Return an error if an unknown flag is set */
64 	if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
65 		return -EINVAL;
66 
67 	/*
68 	 * Do not allow both INPUT & OUTPUT flags to be set as they are
69 	 * contradictory.
70 	 */
71 	if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
72 	    (flags & GPIOHANDLE_REQUEST_OUTPUT))
73 		return -EINVAL;
74 
75 	/*
76 	 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
77 	 * the hardware actually supports enabling both at the same time the
78 	 * electrical result would be disastrous.
79 	 */
80 	if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
81 	    (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
82 		return -EINVAL;
83 
84 	/* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
85 	if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
86 	    ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
87 	     (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
88 		return -EINVAL;
89 
90 	/* Bias flags only allowed for input or output mode. */
91 	if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
92 	      (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
93 	    ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
94 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
95 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
96 		return -EINVAL;
97 
98 	/* Only one bias flag can be set. */
99 	if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
100 	     (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
101 		       GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
102 	    ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
103 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
104 		return -EINVAL;
105 
106 	return 0;
107 }
108 
109 static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
110 {
111 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
112 		   lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
113 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
114 		   lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
115 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
116 		   lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
117 	assign_bit(FLAG_PULL_UP, flagsp,
118 		   lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
119 	assign_bit(FLAG_PULL_DOWN, flagsp,
120 		   lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
121 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
122 		   lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
123 }
124 
125 static long linehandle_set_config(struct linehandle_state *lh,
126 				  void __user *ip)
127 {
128 	struct gpiohandle_config gcnf;
129 	struct gpio_desc *desc;
130 	int i, ret;
131 	u32 lflags;
132 
133 	if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
134 		return -EFAULT;
135 
136 	lflags = gcnf.flags;
137 	ret = linehandle_validate_flags(lflags);
138 	if (ret)
139 		return ret;
140 
141 	for (i = 0; i < lh->num_descs; i++) {
142 		desc = lh->descs[i];
143 		linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags);
144 
145 		/*
146 		 * Lines have to be requested explicitly for input
147 		 * or output, else the line will be treated "as is".
148 		 */
149 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
150 			int val = !!gcnf.default_values[i];
151 
152 			ret = gpiod_direction_output(desc, val);
153 			if (ret)
154 				return ret;
155 		} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
156 			ret = gpiod_direction_input(desc);
157 			if (ret)
158 				return ret;
159 		}
160 
161 		blocking_notifier_call_chain(&desc->gdev->notifier,
162 					     GPIOLINE_CHANGED_CONFIG, desc);
163 	}
164 	return 0;
165 }
166 
167 static long linehandle_ioctl(struct file *file, unsigned int cmd,
168 			     unsigned long arg)
169 {
170 	struct linehandle_state *lh = file->private_data;
171 	void __user *ip = (void __user *)arg;
172 	struct gpiohandle_data ghd;
173 	DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
174 	int i;
175 
176 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
177 		/* NOTE: It's ok to read values of output lines. */
178 		int ret = gpiod_get_array_value_complex(false,
179 							true,
180 							lh->num_descs,
181 							lh->descs,
182 							NULL,
183 							vals);
184 		if (ret)
185 			return ret;
186 
187 		memset(&ghd, 0, sizeof(ghd));
188 		for (i = 0; i < lh->num_descs; i++)
189 			ghd.values[i] = test_bit(i, vals);
190 
191 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
192 			return -EFAULT;
193 
194 		return 0;
195 	} else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
196 		/*
197 		 * All line descriptors were created at once with the same
198 		 * flags so just check if the first one is really output.
199 		 */
200 		if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
201 			return -EPERM;
202 
203 		if (copy_from_user(&ghd, ip, sizeof(ghd)))
204 			return -EFAULT;
205 
206 		/* Clamp all values to [0,1] */
207 		for (i = 0; i < lh->num_descs; i++)
208 			__assign_bit(i, vals, ghd.values[i]);
209 
210 		/* Reuse the array setting function */
211 		return gpiod_set_array_value_complex(false,
212 						     true,
213 						     lh->num_descs,
214 						     lh->descs,
215 						     NULL,
216 						     vals);
217 	} else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) {
218 		return linehandle_set_config(lh, ip);
219 	}
220 	return -EINVAL;
221 }
222 
223 #ifdef CONFIG_COMPAT
224 static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
225 				    unsigned long arg)
226 {
227 	return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
228 }
229 #endif
230 
231 static void linehandle_free(struct linehandle_state *lh)
232 {
233 	int i;
234 
235 	for (i = 0; i < lh->num_descs; i++)
236 		if (lh->descs[i])
237 			gpiod_free(lh->descs[i]);
238 	kfree(lh->label);
239 	put_device(&lh->gdev->dev);
240 	kfree(lh);
241 }
242 
243 static int linehandle_release(struct inode *inode, struct file *file)
244 {
245 	linehandle_free(file->private_data);
246 	return 0;
247 }
248 
249 static const struct file_operations linehandle_fileops = {
250 	.release = linehandle_release,
251 	.owner = THIS_MODULE,
252 	.llseek = noop_llseek,
253 	.unlocked_ioctl = linehandle_ioctl,
254 #ifdef CONFIG_COMPAT
255 	.compat_ioctl = linehandle_ioctl_compat,
256 #endif
257 };
258 
259 static int linehandle_create(struct gpio_device *gdev, void __user *ip)
260 {
261 	struct gpiohandle_request handlereq;
262 	struct linehandle_state *lh;
263 	struct file *file;
264 	int fd, i, ret;
265 	u32 lflags;
266 
267 	if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
268 		return -EFAULT;
269 	if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
270 		return -EINVAL;
271 
272 	lflags = handlereq.flags;
273 
274 	ret = linehandle_validate_flags(lflags);
275 	if (ret)
276 		return ret;
277 
278 	lh = kzalloc(sizeof(*lh), GFP_KERNEL);
279 	if (!lh)
280 		return -ENOMEM;
281 	lh->gdev = gdev;
282 	get_device(&gdev->dev);
283 
284 	/* Make sure this is terminated */
285 	handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
286 	if (strlen(handlereq.consumer_label)) {
287 		lh->label = kstrdup(handlereq.consumer_label,
288 				    GFP_KERNEL);
289 		if (!lh->label) {
290 			ret = -ENOMEM;
291 			goto out_free_lh;
292 		}
293 	}
294 
295 	lh->num_descs = handlereq.lines;
296 
297 	/* Request each GPIO */
298 	for (i = 0; i < handlereq.lines; i++) {
299 		u32 offset = handlereq.lineoffsets[i];
300 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
301 
302 		if (IS_ERR(desc)) {
303 			ret = PTR_ERR(desc);
304 			goto out_free_lh;
305 		}
306 
307 		ret = gpiod_request(desc, lh->label);
308 		if (ret)
309 			goto out_free_lh;
310 		lh->descs[i] = desc;
311 		linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
312 
313 		ret = gpiod_set_transitory(desc, false);
314 		if (ret < 0)
315 			goto out_free_lh;
316 
317 		/*
318 		 * Lines have to be requested explicitly for input
319 		 * or output, else the line will be treated "as is".
320 		 */
321 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
322 			int val = !!handlereq.default_values[i];
323 
324 			ret = gpiod_direction_output(desc, val);
325 			if (ret)
326 				goto out_free_lh;
327 		} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
328 			ret = gpiod_direction_input(desc);
329 			if (ret)
330 				goto out_free_lh;
331 		}
332 
333 		blocking_notifier_call_chain(&desc->gdev->notifier,
334 					     GPIOLINE_CHANGED_REQUESTED, desc);
335 
336 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
337 			offset);
338 	}
339 
340 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
341 	if (fd < 0) {
342 		ret = fd;
343 		goto out_free_lh;
344 	}
345 
346 	file = anon_inode_getfile("gpio-linehandle",
347 				  &linehandle_fileops,
348 				  lh,
349 				  O_RDONLY | O_CLOEXEC);
350 	if (IS_ERR(file)) {
351 		ret = PTR_ERR(file);
352 		goto out_put_unused_fd;
353 	}
354 
355 	handlereq.fd = fd;
356 	if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
357 		/*
358 		 * fput() will trigger the release() callback, so do not go onto
359 		 * the regular error cleanup path here.
360 		 */
361 		fput(file);
362 		put_unused_fd(fd);
363 		return -EFAULT;
364 	}
365 
366 	fd_install(fd, file);
367 
368 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
369 		lh->num_descs);
370 
371 	return 0;
372 
373 out_put_unused_fd:
374 	put_unused_fd(fd);
375 out_free_lh:
376 	linehandle_free(lh);
377 	return ret;
378 }
379 
380 /*
381  * GPIO line event management
382  */
383 
384 /**
385  * struct lineevent_state - contains the state of a userspace event
386  * @gdev: the GPIO device the event pertains to
387  * @label: consumer label used to tag descriptors
388  * @desc: the GPIO descriptor held by this event
389  * @eflags: the event flags this line was requested with
390  * @irq: the interrupt that trigger in response to events on this GPIO
391  * @wait: wait queue that handles blocking reads of events
392  * @events: KFIFO for the GPIO events
393  * @timestamp: cache for the timestamp storing it between hardirq
394  * and IRQ thread, used to bring the timestamp close to the actual
395  * event
396  */
397 struct lineevent_state {
398 	struct gpio_device *gdev;
399 	const char *label;
400 	struct gpio_desc *desc;
401 	u32 eflags;
402 	int irq;
403 	wait_queue_head_t wait;
404 	DECLARE_KFIFO(events, struct gpioevent_data, 16);
405 	u64 timestamp;
406 };
407 
408 #define GPIOEVENT_REQUEST_VALID_FLAGS \
409 	(GPIOEVENT_REQUEST_RISING_EDGE | \
410 	GPIOEVENT_REQUEST_FALLING_EDGE)
411 
412 static __poll_t lineevent_poll(struct file *file,
413 			       struct poll_table_struct *wait)
414 {
415 	struct lineevent_state *le = file->private_data;
416 	__poll_t events = 0;
417 
418 	poll_wait(file, &le->wait, wait);
419 
420 	if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
421 		events = EPOLLIN | EPOLLRDNORM;
422 
423 	return events;
424 }
425 
426 static ssize_t lineevent_get_size(void)
427 {
428 #ifdef __x86_64__
429 	/* i386 has no padding after 'id' */
430 	if (in_ia32_syscall()) {
431 		struct compat_gpioeevent_data {
432 			compat_u64	timestamp;
433 			u32		id;
434 		};
435 
436 		return sizeof(struct compat_gpioeevent_data);
437 	}
438 #endif
439 	return sizeof(struct gpioevent_data);
440 }
441 
442 static ssize_t lineevent_read(struct file *file,
443 			      char __user *buf,
444 			      size_t count,
445 			      loff_t *f_ps)
446 {
447 	struct lineevent_state *le = file->private_data;
448 	struct gpioevent_data ge;
449 	ssize_t bytes_read = 0;
450 	ssize_t ge_size;
451 	int ret;
452 
453 	/*
454 	 * When compatible system call is being used the struct gpioevent_data,
455 	 * in case of at least ia32, has different size due to the alignment
456 	 * differences. Because we have first member 64 bits followed by one of
457 	 * 32 bits there is no gap between them. The only difference is the
458 	 * padding at the end of the data structure. Hence, we calculate the
459 	 * actual sizeof() and pass this as an argument to copy_to_user() to
460 	 * drop unneeded bytes from the output.
461 	 */
462 	ge_size = lineevent_get_size();
463 	if (count < ge_size)
464 		return -EINVAL;
465 
466 	do {
467 		spin_lock(&le->wait.lock);
468 		if (kfifo_is_empty(&le->events)) {
469 			if (bytes_read) {
470 				spin_unlock(&le->wait.lock);
471 				return bytes_read;
472 			}
473 
474 			if (file->f_flags & O_NONBLOCK) {
475 				spin_unlock(&le->wait.lock);
476 				return -EAGAIN;
477 			}
478 
479 			ret = wait_event_interruptible_locked(le->wait,
480 					!kfifo_is_empty(&le->events));
481 			if (ret) {
482 				spin_unlock(&le->wait.lock);
483 				return ret;
484 			}
485 		}
486 
487 		ret = kfifo_out(&le->events, &ge, 1);
488 		spin_unlock(&le->wait.lock);
489 		if (ret != 1) {
490 			/*
491 			 * This should never happen - we were holding the lock
492 			 * from the moment we learned the fifo is no longer
493 			 * empty until now.
494 			 */
495 			ret = -EIO;
496 			break;
497 		}
498 
499 		if (copy_to_user(buf + bytes_read, &ge, ge_size))
500 			return -EFAULT;
501 		bytes_read += ge_size;
502 	} while (count >= bytes_read + ge_size);
503 
504 	return bytes_read;
505 }
506 
507 static void lineevent_free(struct lineevent_state *le)
508 {
509 	if (le->irq)
510 		free_irq(le->irq, le);
511 	if (le->desc)
512 		gpiod_free(le->desc);
513 	kfree(le->label);
514 	put_device(&le->gdev->dev);
515 	kfree(le);
516 }
517 
518 static int lineevent_release(struct inode *inode, struct file *file)
519 {
520 	lineevent_free(file->private_data);
521 	return 0;
522 }
523 
524 static long lineevent_ioctl(struct file *file, unsigned int cmd,
525 			    unsigned long arg)
526 {
527 	struct lineevent_state *le = file->private_data;
528 	void __user *ip = (void __user *)arg;
529 	struct gpiohandle_data ghd;
530 
531 	/*
532 	 * We can get the value for an event line but not set it,
533 	 * because it is input by definition.
534 	 */
535 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
536 		int val;
537 
538 		memset(&ghd, 0, sizeof(ghd));
539 
540 		val = gpiod_get_value_cansleep(le->desc);
541 		if (val < 0)
542 			return val;
543 		ghd.values[0] = val;
544 
545 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
546 			return -EFAULT;
547 
548 		return 0;
549 	}
550 	return -EINVAL;
551 }
552 
553 #ifdef CONFIG_COMPAT
554 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
555 				   unsigned long arg)
556 {
557 	return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
558 }
559 #endif
560 
561 static const struct file_operations lineevent_fileops = {
562 	.release = lineevent_release,
563 	.read = lineevent_read,
564 	.poll = lineevent_poll,
565 	.owner = THIS_MODULE,
566 	.llseek = noop_llseek,
567 	.unlocked_ioctl = lineevent_ioctl,
568 #ifdef CONFIG_COMPAT
569 	.compat_ioctl = lineevent_ioctl_compat,
570 #endif
571 };
572 
573 static irqreturn_t lineevent_irq_thread(int irq, void *p)
574 {
575 	struct lineevent_state *le = p;
576 	struct gpioevent_data ge;
577 	int ret;
578 
579 	/* Do not leak kernel stack to userspace */
580 	memset(&ge, 0, sizeof(ge));
581 
582 	/*
583 	 * We may be running from a nested threaded interrupt in which case
584 	 * we didn't get the timestamp from lineevent_irq_handler().
585 	 */
586 	if (!le->timestamp)
587 		ge.timestamp = ktime_get_ns();
588 	else
589 		ge.timestamp = le->timestamp;
590 
591 	if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
592 	    && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
593 		int level = gpiod_get_value_cansleep(le->desc);
594 
595 		if (level)
596 			/* Emit low-to-high event */
597 			ge.id = GPIOEVENT_EVENT_RISING_EDGE;
598 		else
599 			/* Emit high-to-low event */
600 			ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
601 	} else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
602 		/* Emit low-to-high event */
603 		ge.id = GPIOEVENT_EVENT_RISING_EDGE;
604 	} else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
605 		/* Emit high-to-low event */
606 		ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
607 	} else {
608 		return IRQ_NONE;
609 	}
610 
611 	ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
612 					    1, &le->wait.lock);
613 	if (ret)
614 		wake_up_poll(&le->wait, EPOLLIN);
615 	else
616 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
617 
618 	return IRQ_HANDLED;
619 }
620 
621 static irqreturn_t lineevent_irq_handler(int irq, void *p)
622 {
623 	struct lineevent_state *le = p;
624 
625 	/*
626 	 * Just store the timestamp in hardirq context so we get it as
627 	 * close in time as possible to the actual event.
628 	 */
629 	le->timestamp = ktime_get_ns();
630 
631 	return IRQ_WAKE_THREAD;
632 }
633 
634 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
635 {
636 	struct gpioevent_request eventreq;
637 	struct lineevent_state *le;
638 	struct gpio_desc *desc;
639 	struct file *file;
640 	u32 offset;
641 	u32 lflags;
642 	u32 eflags;
643 	int fd;
644 	int ret;
645 	int irq, irqflags = 0;
646 
647 	if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
648 		return -EFAULT;
649 
650 	offset = eventreq.lineoffset;
651 	lflags = eventreq.handleflags;
652 	eflags = eventreq.eventflags;
653 
654 	desc = gpiochip_get_desc(gdev->chip, offset);
655 	if (IS_ERR(desc))
656 		return PTR_ERR(desc);
657 
658 	/* Return an error if a unknown flag is set */
659 	if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
660 	    (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
661 		return -EINVAL;
662 
663 	/* This is just wrong: we don't look for events on output lines */
664 	if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
665 	    (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
666 	    (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
667 		return -EINVAL;
668 
669 	/* Only one bias flag can be set. */
670 	if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
671 	     (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
672 			GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
673 	    ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
674 	     (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
675 		return -EINVAL;
676 
677 	le = kzalloc(sizeof(*le), GFP_KERNEL);
678 	if (!le)
679 		return -ENOMEM;
680 	le->gdev = gdev;
681 	get_device(&gdev->dev);
682 
683 	/* Make sure this is terminated */
684 	eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
685 	if (strlen(eventreq.consumer_label)) {
686 		le->label = kstrdup(eventreq.consumer_label,
687 				    GFP_KERNEL);
688 		if (!le->label) {
689 			ret = -ENOMEM;
690 			goto out_free_le;
691 		}
692 	}
693 
694 	ret = gpiod_request(desc, le->label);
695 	if (ret)
696 		goto out_free_le;
697 	le->desc = desc;
698 	le->eflags = eflags;
699 
700 	linehandle_flags_to_desc_flags(lflags, &desc->flags);
701 
702 	ret = gpiod_direction_input(desc);
703 	if (ret)
704 		goto out_free_le;
705 
706 	blocking_notifier_call_chain(&desc->gdev->notifier,
707 				     GPIOLINE_CHANGED_REQUESTED, desc);
708 
709 	irq = gpiod_to_irq(desc);
710 	if (irq <= 0) {
711 		ret = -ENODEV;
712 		goto out_free_le;
713 	}
714 	le->irq = irq;
715 
716 	if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
717 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
718 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
719 	if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
720 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
721 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
722 	irqflags |= IRQF_ONESHOT;
723 
724 	INIT_KFIFO(le->events);
725 	init_waitqueue_head(&le->wait);
726 
727 	/* Request a thread to read the events */
728 	ret = request_threaded_irq(le->irq,
729 				   lineevent_irq_handler,
730 				   lineevent_irq_thread,
731 				   irqflags,
732 				   le->label,
733 				   le);
734 	if (ret)
735 		goto out_free_le;
736 
737 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
738 	if (fd < 0) {
739 		ret = fd;
740 		goto out_free_le;
741 	}
742 
743 	file = anon_inode_getfile("gpio-event",
744 				  &lineevent_fileops,
745 				  le,
746 				  O_RDONLY | O_CLOEXEC);
747 	if (IS_ERR(file)) {
748 		ret = PTR_ERR(file);
749 		goto out_put_unused_fd;
750 	}
751 
752 	eventreq.fd = fd;
753 	if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
754 		/*
755 		 * fput() will trigger the release() callback, so do not go onto
756 		 * the regular error cleanup path here.
757 		 */
758 		fput(file);
759 		put_unused_fd(fd);
760 		return -EFAULT;
761 	}
762 
763 	fd_install(fd, file);
764 
765 	return 0;
766 
767 out_put_unused_fd:
768 	put_unused_fd(fd);
769 out_free_le:
770 	lineevent_free(le);
771 	return ret;
772 }
773 
774 static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
775 				  struct gpioline_info *info)
776 {
777 	struct gpio_chip *gc = desc->gdev->chip;
778 	bool ok_for_pinctrl;
779 	unsigned long flags;
780 
781 	/*
782 	 * This function takes a mutex so we must check this before taking
783 	 * the spinlock.
784 	 *
785 	 * FIXME: find a non-racy way to retrieve this information. Maybe a
786 	 * lock common to both frameworks?
787 	 */
788 	ok_for_pinctrl =
789 		pinctrl_gpio_can_use_line(gc->base + info->line_offset);
790 
791 	spin_lock_irqsave(&gpio_lock, flags);
792 
793 	if (desc->name) {
794 		strncpy(info->name, desc->name, sizeof(info->name));
795 		info->name[sizeof(info->name) - 1] = '\0';
796 	} else {
797 		info->name[0] = '\0';
798 	}
799 
800 	if (desc->label) {
801 		strncpy(info->consumer, desc->label, sizeof(info->consumer));
802 		info->consumer[sizeof(info->consumer) - 1] = '\0';
803 	} else {
804 		info->consumer[0] = '\0';
805 	}
806 
807 	/*
808 	 * Userspace only need to know that the kernel is using this GPIO so
809 	 * it can't use it.
810 	 */
811 	info->flags = 0;
812 	if (test_bit(FLAG_REQUESTED, &desc->flags) ||
813 	    test_bit(FLAG_IS_HOGGED, &desc->flags) ||
814 	    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
815 	    test_bit(FLAG_EXPORT, &desc->flags) ||
816 	    test_bit(FLAG_SYSFS, &desc->flags) ||
817 	    !ok_for_pinctrl)
818 		info->flags |= GPIOLINE_FLAG_KERNEL;
819 	if (test_bit(FLAG_IS_OUT, &desc->flags))
820 		info->flags |= GPIOLINE_FLAG_IS_OUT;
821 	if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
822 		info->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
823 	if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
824 		info->flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
825 				GPIOLINE_FLAG_IS_OUT);
826 	if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
827 		info->flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
828 				GPIOLINE_FLAG_IS_OUT);
829 	if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
830 		info->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
831 	if (test_bit(FLAG_PULL_DOWN, &desc->flags))
832 		info->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
833 	if (test_bit(FLAG_PULL_UP, &desc->flags))
834 		info->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
835 
836 	spin_unlock_irqrestore(&gpio_lock, flags);
837 }
838 
839 struct gpio_chardev_data {
840 	struct gpio_device *gdev;
841 	wait_queue_head_t wait;
842 	DECLARE_KFIFO(events, struct gpioline_info_changed, 32);
843 	struct notifier_block lineinfo_changed_nb;
844 	unsigned long *watched_lines;
845 };
846 
847 /*
848  * gpio_ioctl() - ioctl handler for the GPIO chardev
849  */
850 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
851 {
852 	struct gpio_chardev_data *cdev = file->private_data;
853 	struct gpio_device *gdev = cdev->gdev;
854 	struct gpio_chip *gc = gdev->chip;
855 	void __user *ip = (void __user *)arg;
856 	struct gpio_desc *desc;
857 	__u32 offset;
858 
859 	/* We fail any subsequent ioctl():s when the chip is gone */
860 	if (!gc)
861 		return -ENODEV;
862 
863 	/* Fill in the struct and pass to userspace */
864 	if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
865 		struct gpiochip_info chipinfo;
866 
867 		memset(&chipinfo, 0, sizeof(chipinfo));
868 
869 		strncpy(chipinfo.name, dev_name(&gdev->dev),
870 			sizeof(chipinfo.name));
871 		chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
872 		strncpy(chipinfo.label, gdev->label,
873 			sizeof(chipinfo.label));
874 		chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
875 		chipinfo.lines = gdev->ngpio;
876 		if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
877 			return -EFAULT;
878 		return 0;
879 	} else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
880 		struct gpioline_info lineinfo;
881 
882 		if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
883 			return -EFAULT;
884 
885 		/* this doubles as a range check on line_offset */
886 		desc = gpiochip_get_desc(gc, lineinfo.line_offset);
887 		if (IS_ERR(desc))
888 			return PTR_ERR(desc);
889 
890 		gpio_desc_to_lineinfo(desc, &lineinfo);
891 
892 		if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
893 			return -EFAULT;
894 		return 0;
895 	} else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
896 		return linehandle_create(gdev, ip);
897 	} else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
898 		return lineevent_create(gdev, ip);
899 	} else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
900 		struct gpioline_info lineinfo;
901 
902 		if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
903 			return -EFAULT;
904 
905 		/* this doubles as a range check on line_offset */
906 		desc = gpiochip_get_desc(gc, lineinfo.line_offset);
907 		if (IS_ERR(desc))
908 			return PTR_ERR(desc);
909 
910 		if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
911 			return -EBUSY;
912 
913 		gpio_desc_to_lineinfo(desc, &lineinfo);
914 
915 		if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
916 			clear_bit(lineinfo.line_offset, cdev->watched_lines);
917 			return -EFAULT;
918 		}
919 
920 		return 0;
921 	} else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) {
922 		if (copy_from_user(&offset, ip, sizeof(offset)))
923 			return -EFAULT;
924 
925 		if (offset >= cdev->gdev->ngpio)
926 			return -EINVAL;
927 
928 		if (!test_and_clear_bit(offset, cdev->watched_lines))
929 			return -EBUSY;
930 
931 		return 0;
932 	}
933 	return -EINVAL;
934 }
935 
936 #ifdef CONFIG_COMPAT
937 static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
938 			      unsigned long arg)
939 {
940 	return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
941 }
942 #endif
943 
944 static struct gpio_chardev_data *
945 to_gpio_chardev_data(struct notifier_block *nb)
946 {
947 	return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
948 }
949 
950 static int lineinfo_changed_notify(struct notifier_block *nb,
951 				   unsigned long action, void *data)
952 {
953 	struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
954 	struct gpioline_info_changed chg;
955 	struct gpio_desc *desc = data;
956 	int ret;
957 
958 	if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
959 		return NOTIFY_DONE;
960 
961 	memset(&chg, 0, sizeof(chg));
962 	chg.info.line_offset = gpio_chip_hwgpio(desc);
963 	chg.event_type = action;
964 	chg.timestamp = ktime_get_ns();
965 	gpio_desc_to_lineinfo(desc, &chg.info);
966 
967 	ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
968 	if (ret)
969 		wake_up_poll(&cdev->wait, EPOLLIN);
970 	else
971 		pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
972 
973 	return NOTIFY_OK;
974 }
975 
976 static __poll_t lineinfo_watch_poll(struct file *file,
977 				    struct poll_table_struct *pollt)
978 {
979 	struct gpio_chardev_data *cdev = file->private_data;
980 	__poll_t events = 0;
981 
982 	poll_wait(file, &cdev->wait, pollt);
983 
984 	if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
985 						 &cdev->wait.lock))
986 		events = EPOLLIN | EPOLLRDNORM;
987 
988 	return events;
989 }
990 
991 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
992 				   size_t count, loff_t *off)
993 {
994 	struct gpio_chardev_data *cdev = file->private_data;
995 	struct gpioline_info_changed event;
996 	ssize_t bytes_read = 0;
997 	int ret;
998 
999 	if (count < sizeof(event))
1000 		return -EINVAL;
1001 
1002 	do {
1003 		spin_lock(&cdev->wait.lock);
1004 		if (kfifo_is_empty(&cdev->events)) {
1005 			if (bytes_read) {
1006 				spin_unlock(&cdev->wait.lock);
1007 				return bytes_read;
1008 			}
1009 
1010 			if (file->f_flags & O_NONBLOCK) {
1011 				spin_unlock(&cdev->wait.lock);
1012 				return -EAGAIN;
1013 			}
1014 
1015 			ret = wait_event_interruptible_locked(cdev->wait,
1016 					!kfifo_is_empty(&cdev->events));
1017 			if (ret) {
1018 				spin_unlock(&cdev->wait.lock);
1019 				return ret;
1020 			}
1021 		}
1022 
1023 		ret = kfifo_out(&cdev->events, &event, 1);
1024 		spin_unlock(&cdev->wait.lock);
1025 		if (ret != 1) {
1026 			ret = -EIO;
1027 			break;
1028 			/* We should never get here. See lineevent_read(). */
1029 		}
1030 
1031 		if (copy_to_user(buf + bytes_read, &event, sizeof(event)))
1032 			return -EFAULT;
1033 		bytes_read += sizeof(event);
1034 	} while (count >= bytes_read + sizeof(event));
1035 
1036 	return bytes_read;
1037 }
1038 
1039 /**
1040  * gpio_chrdev_open() - open the chardev for ioctl operations
1041  * @inode: inode for this chardev
1042  * @file: file struct for storing private data
1043  * Returns 0 on success
1044  */
1045 static int gpio_chrdev_open(struct inode *inode, struct file *file)
1046 {
1047 	struct gpio_device *gdev = container_of(inode->i_cdev,
1048 						struct gpio_device, chrdev);
1049 	struct gpio_chardev_data *cdev;
1050 	int ret = -ENOMEM;
1051 
1052 	/* Fail on open if the backing gpiochip is gone */
1053 	if (!gdev->chip)
1054 		return -ENODEV;
1055 
1056 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
1057 	if (!cdev)
1058 		return -ENOMEM;
1059 
1060 	cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
1061 	if (!cdev->watched_lines)
1062 		goto out_free_cdev;
1063 
1064 	init_waitqueue_head(&cdev->wait);
1065 	INIT_KFIFO(cdev->events);
1066 	cdev->gdev = gdev;
1067 
1068 	cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
1069 	ret = blocking_notifier_chain_register(&gdev->notifier,
1070 					       &cdev->lineinfo_changed_nb);
1071 	if (ret)
1072 		goto out_free_bitmap;
1073 
1074 	get_device(&gdev->dev);
1075 	file->private_data = cdev;
1076 
1077 	ret = nonseekable_open(inode, file);
1078 	if (ret)
1079 		goto out_unregister_notifier;
1080 
1081 	return ret;
1082 
1083 out_unregister_notifier:
1084 	blocking_notifier_chain_unregister(&gdev->notifier,
1085 					   &cdev->lineinfo_changed_nb);
1086 out_free_bitmap:
1087 	bitmap_free(cdev->watched_lines);
1088 out_free_cdev:
1089 	kfree(cdev);
1090 	return ret;
1091 }
1092 
1093 /**
1094  * gpio_chrdev_release() - close chardev after ioctl operations
1095  * @inode: inode for this chardev
1096  * @file: file struct for storing private data
1097  * Returns 0 on success
1098  */
1099 static int gpio_chrdev_release(struct inode *inode, struct file *file)
1100 {
1101 	struct gpio_chardev_data *cdev = file->private_data;
1102 	struct gpio_device *gdev = cdev->gdev;
1103 
1104 	bitmap_free(cdev->watched_lines);
1105 	blocking_notifier_chain_unregister(&gdev->notifier,
1106 					   &cdev->lineinfo_changed_nb);
1107 	put_device(&gdev->dev);
1108 	kfree(cdev);
1109 
1110 	return 0;
1111 }
1112 
1113 static const struct file_operations gpio_fileops = {
1114 	.release = gpio_chrdev_release,
1115 	.open = gpio_chrdev_open,
1116 	.poll = lineinfo_watch_poll,
1117 	.read = lineinfo_watch_read,
1118 	.owner = THIS_MODULE,
1119 	.llseek = no_llseek,
1120 	.unlocked_ioctl = gpio_ioctl,
1121 #ifdef CONFIG_COMPAT
1122 	.compat_ioctl = gpio_ioctl_compat,
1123 #endif
1124 };
1125 
1126 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
1127 {
1128 	int ret;
1129 
1130 	cdev_init(&gdev->chrdev, &gpio_fileops);
1131 	gdev->chrdev.owner = THIS_MODULE;
1132 	gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
1133 
1134 	ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
1135 	if (ret)
1136 		return ret;
1137 
1138 	chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1139 		 MAJOR(devt), gdev->id);
1140 
1141 	return 0;
1142 }
1143 
1144 void gpiolib_cdev_unregister(struct gpio_device *gdev)
1145 {
1146 	cdev_device_del(&gdev->chrdev, &gdev->dev);
1147 }
1148