xref: /openbmc/linux/drivers/gpio/gpiolib-cdev.c (revision 3a83e4e6)
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 
427 static ssize_t lineevent_read(struct file *file,
428 			      char __user *buf,
429 			      size_t count,
430 			      loff_t *f_ps)
431 {
432 	struct lineevent_state *le = file->private_data;
433 	struct gpioevent_data ge;
434 	ssize_t bytes_read = 0;
435 	int ret;
436 
437 	if (count < sizeof(ge))
438 		return -EINVAL;
439 
440 	do {
441 		spin_lock(&le->wait.lock);
442 		if (kfifo_is_empty(&le->events)) {
443 			if (bytes_read) {
444 				spin_unlock(&le->wait.lock);
445 				return bytes_read;
446 			}
447 
448 			if (file->f_flags & O_NONBLOCK) {
449 				spin_unlock(&le->wait.lock);
450 				return -EAGAIN;
451 			}
452 
453 			ret = wait_event_interruptible_locked(le->wait,
454 					!kfifo_is_empty(&le->events));
455 			if (ret) {
456 				spin_unlock(&le->wait.lock);
457 				return ret;
458 			}
459 		}
460 
461 		ret = kfifo_out(&le->events, &ge, 1);
462 		spin_unlock(&le->wait.lock);
463 		if (ret != 1) {
464 			/*
465 			 * This should never happen - we were holding the lock
466 			 * from the moment we learned the fifo is no longer
467 			 * empty until now.
468 			 */
469 			ret = -EIO;
470 			break;
471 		}
472 
473 		if (copy_to_user(buf + bytes_read, &ge, sizeof(ge)))
474 			return -EFAULT;
475 		bytes_read += sizeof(ge);
476 	} while (count >= bytes_read + sizeof(ge));
477 
478 	return bytes_read;
479 }
480 
481 static void lineevent_free(struct lineevent_state *le)
482 {
483 	if (le->irq)
484 		free_irq(le->irq, le);
485 	if (le->desc)
486 		gpiod_free(le->desc);
487 	kfree(le->label);
488 	put_device(&le->gdev->dev);
489 	kfree(le);
490 }
491 
492 static int lineevent_release(struct inode *inode, struct file *file)
493 {
494 	lineevent_free(file->private_data);
495 	return 0;
496 }
497 
498 static long lineevent_ioctl(struct file *file, unsigned int cmd,
499 			    unsigned long arg)
500 {
501 	struct lineevent_state *le = file->private_data;
502 	void __user *ip = (void __user *)arg;
503 	struct gpiohandle_data ghd;
504 
505 	/*
506 	 * We can get the value for an event line but not set it,
507 	 * because it is input by definition.
508 	 */
509 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
510 		int val;
511 
512 		memset(&ghd, 0, sizeof(ghd));
513 
514 		val = gpiod_get_value_cansleep(le->desc);
515 		if (val < 0)
516 			return val;
517 		ghd.values[0] = val;
518 
519 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
520 			return -EFAULT;
521 
522 		return 0;
523 	}
524 	return -EINVAL;
525 }
526 
527 #ifdef CONFIG_COMPAT
528 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
529 				   unsigned long arg)
530 {
531 	return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
532 }
533 #endif
534 
535 static const struct file_operations lineevent_fileops = {
536 	.release = lineevent_release,
537 	.read = lineevent_read,
538 	.poll = lineevent_poll,
539 	.owner = THIS_MODULE,
540 	.llseek = noop_llseek,
541 	.unlocked_ioctl = lineevent_ioctl,
542 #ifdef CONFIG_COMPAT
543 	.compat_ioctl = lineevent_ioctl_compat,
544 #endif
545 };
546 
547 static irqreturn_t lineevent_irq_thread(int irq, void *p)
548 {
549 	struct lineevent_state *le = p;
550 	struct gpioevent_data ge;
551 	int ret;
552 
553 	/* Do not leak kernel stack to userspace */
554 	memset(&ge, 0, sizeof(ge));
555 
556 	/*
557 	 * We may be running from a nested threaded interrupt in which case
558 	 * we didn't get the timestamp from lineevent_irq_handler().
559 	 */
560 	if (!le->timestamp)
561 		ge.timestamp = ktime_get_ns();
562 	else
563 		ge.timestamp = le->timestamp;
564 
565 	if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
566 	    && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
567 		int level = gpiod_get_value_cansleep(le->desc);
568 
569 		if (level)
570 			/* Emit low-to-high event */
571 			ge.id = GPIOEVENT_EVENT_RISING_EDGE;
572 		else
573 			/* Emit high-to-low event */
574 			ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
575 	} else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
576 		/* Emit low-to-high event */
577 		ge.id = GPIOEVENT_EVENT_RISING_EDGE;
578 	} else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
579 		/* Emit high-to-low event */
580 		ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
581 	} else {
582 		return IRQ_NONE;
583 	}
584 
585 	ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
586 					    1, &le->wait.lock);
587 	if (ret)
588 		wake_up_poll(&le->wait, EPOLLIN);
589 	else
590 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
591 
592 	return IRQ_HANDLED;
593 }
594 
595 static irqreturn_t lineevent_irq_handler(int irq, void *p)
596 {
597 	struct lineevent_state *le = p;
598 
599 	/*
600 	 * Just store the timestamp in hardirq context so we get it as
601 	 * close in time as possible to the actual event.
602 	 */
603 	le->timestamp = ktime_get_ns();
604 
605 	return IRQ_WAKE_THREAD;
606 }
607 
608 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
609 {
610 	struct gpioevent_request eventreq;
611 	struct lineevent_state *le;
612 	struct gpio_desc *desc;
613 	struct file *file;
614 	u32 offset;
615 	u32 lflags;
616 	u32 eflags;
617 	int fd;
618 	int ret;
619 	int irq, irqflags = 0;
620 
621 	if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
622 		return -EFAULT;
623 
624 	offset = eventreq.lineoffset;
625 	lflags = eventreq.handleflags;
626 	eflags = eventreq.eventflags;
627 
628 	desc = gpiochip_get_desc(gdev->chip, offset);
629 	if (IS_ERR(desc))
630 		return PTR_ERR(desc);
631 
632 	/* Return an error if a unknown flag is set */
633 	if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
634 	    (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
635 		return -EINVAL;
636 
637 	/* This is just wrong: we don't look for events on output lines */
638 	if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
639 	    (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
640 	    (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
641 		return -EINVAL;
642 
643 	/* Only one bias flag can be set. */
644 	if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
645 	     (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
646 			GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
647 	    ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
648 	     (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
649 		return -EINVAL;
650 
651 	le = kzalloc(sizeof(*le), GFP_KERNEL);
652 	if (!le)
653 		return -ENOMEM;
654 	le->gdev = gdev;
655 	get_device(&gdev->dev);
656 
657 	/* Make sure this is terminated */
658 	eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
659 	if (strlen(eventreq.consumer_label)) {
660 		le->label = kstrdup(eventreq.consumer_label,
661 				    GFP_KERNEL);
662 		if (!le->label) {
663 			ret = -ENOMEM;
664 			goto out_free_le;
665 		}
666 	}
667 
668 	ret = gpiod_request(desc, le->label);
669 	if (ret)
670 		goto out_free_le;
671 	le->desc = desc;
672 	le->eflags = eflags;
673 
674 	linehandle_flags_to_desc_flags(lflags, &desc->flags);
675 
676 	ret = gpiod_direction_input(desc);
677 	if (ret)
678 		goto out_free_le;
679 
680 	blocking_notifier_call_chain(&desc->gdev->notifier,
681 				     GPIOLINE_CHANGED_REQUESTED, desc);
682 
683 	irq = gpiod_to_irq(desc);
684 	if (irq <= 0) {
685 		ret = -ENODEV;
686 		goto out_free_le;
687 	}
688 	le->irq = irq;
689 
690 	if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
691 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
692 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
693 	if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
694 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
695 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
696 	irqflags |= IRQF_ONESHOT;
697 
698 	INIT_KFIFO(le->events);
699 	init_waitqueue_head(&le->wait);
700 
701 	/* Request a thread to read the events */
702 	ret = request_threaded_irq(le->irq,
703 				   lineevent_irq_handler,
704 				   lineevent_irq_thread,
705 				   irqflags,
706 				   le->label,
707 				   le);
708 	if (ret)
709 		goto out_free_le;
710 
711 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
712 	if (fd < 0) {
713 		ret = fd;
714 		goto out_free_le;
715 	}
716 
717 	file = anon_inode_getfile("gpio-event",
718 				  &lineevent_fileops,
719 				  le,
720 				  O_RDONLY | O_CLOEXEC);
721 	if (IS_ERR(file)) {
722 		ret = PTR_ERR(file);
723 		goto out_put_unused_fd;
724 	}
725 
726 	eventreq.fd = fd;
727 	if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
728 		/*
729 		 * fput() will trigger the release() callback, so do not go onto
730 		 * the regular error cleanup path here.
731 		 */
732 		fput(file);
733 		put_unused_fd(fd);
734 		return -EFAULT;
735 	}
736 
737 	fd_install(fd, file);
738 
739 	return 0;
740 
741 out_put_unused_fd:
742 	put_unused_fd(fd);
743 out_free_le:
744 	lineevent_free(le);
745 	return ret;
746 }
747 
748 static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
749 				  struct gpioline_info *info)
750 {
751 	struct gpio_chip *gc = desc->gdev->chip;
752 	bool ok_for_pinctrl;
753 	unsigned long flags;
754 
755 	/*
756 	 * This function takes a mutex so we must check this before taking
757 	 * the spinlock.
758 	 *
759 	 * FIXME: find a non-racy way to retrieve this information. Maybe a
760 	 * lock common to both frameworks?
761 	 */
762 	ok_for_pinctrl =
763 		pinctrl_gpio_can_use_line(gc->base + info->line_offset);
764 
765 	spin_lock_irqsave(&gpio_lock, flags);
766 
767 	if (desc->name) {
768 		strncpy(info->name, desc->name, sizeof(info->name));
769 		info->name[sizeof(info->name) - 1] = '\0';
770 	} else {
771 		info->name[0] = '\0';
772 	}
773 
774 	if (desc->label) {
775 		strncpy(info->consumer, desc->label, sizeof(info->consumer));
776 		info->consumer[sizeof(info->consumer) - 1] = '\0';
777 	} else {
778 		info->consumer[0] = '\0';
779 	}
780 
781 	/*
782 	 * Userspace only need to know that the kernel is using this GPIO so
783 	 * it can't use it.
784 	 */
785 	info->flags = 0;
786 	if (test_bit(FLAG_REQUESTED, &desc->flags) ||
787 	    test_bit(FLAG_IS_HOGGED, &desc->flags) ||
788 	    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
789 	    test_bit(FLAG_EXPORT, &desc->flags) ||
790 	    test_bit(FLAG_SYSFS, &desc->flags) ||
791 	    !ok_for_pinctrl)
792 		info->flags |= GPIOLINE_FLAG_KERNEL;
793 	if (test_bit(FLAG_IS_OUT, &desc->flags))
794 		info->flags |= GPIOLINE_FLAG_IS_OUT;
795 	if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
796 		info->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
797 	if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
798 		info->flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
799 				GPIOLINE_FLAG_IS_OUT);
800 	if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
801 		info->flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
802 				GPIOLINE_FLAG_IS_OUT);
803 	if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
804 		info->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
805 	if (test_bit(FLAG_PULL_DOWN, &desc->flags))
806 		info->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
807 	if (test_bit(FLAG_PULL_UP, &desc->flags))
808 		info->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
809 
810 	spin_unlock_irqrestore(&gpio_lock, flags);
811 }
812 
813 struct gpio_chardev_data {
814 	struct gpio_device *gdev;
815 	wait_queue_head_t wait;
816 	DECLARE_KFIFO(events, struct gpioline_info_changed, 32);
817 	struct notifier_block lineinfo_changed_nb;
818 	unsigned long *watched_lines;
819 };
820 
821 /*
822  * gpio_ioctl() - ioctl handler for the GPIO chardev
823  */
824 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
825 {
826 	struct gpio_chardev_data *cdev = file->private_data;
827 	struct gpio_device *gdev = cdev->gdev;
828 	struct gpio_chip *gc = gdev->chip;
829 	void __user *ip = (void __user *)arg;
830 	struct gpio_desc *desc;
831 	__u32 offset;
832 
833 	/* We fail any subsequent ioctl():s when the chip is gone */
834 	if (!gc)
835 		return -ENODEV;
836 
837 	/* Fill in the struct and pass to userspace */
838 	if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
839 		struct gpiochip_info chipinfo;
840 
841 		memset(&chipinfo, 0, sizeof(chipinfo));
842 
843 		strncpy(chipinfo.name, dev_name(&gdev->dev),
844 			sizeof(chipinfo.name));
845 		chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
846 		strncpy(chipinfo.label, gdev->label,
847 			sizeof(chipinfo.label));
848 		chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
849 		chipinfo.lines = gdev->ngpio;
850 		if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
851 			return -EFAULT;
852 		return 0;
853 	} else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
854 		struct gpioline_info lineinfo;
855 
856 		if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
857 			return -EFAULT;
858 
859 		/* this doubles as a range check on line_offset */
860 		desc = gpiochip_get_desc(gc, lineinfo.line_offset);
861 		if (IS_ERR(desc))
862 			return PTR_ERR(desc);
863 
864 		gpio_desc_to_lineinfo(desc, &lineinfo);
865 
866 		if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
867 			return -EFAULT;
868 		return 0;
869 	} else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
870 		return linehandle_create(gdev, ip);
871 	} else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
872 		return lineevent_create(gdev, ip);
873 	} else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
874 		struct gpioline_info lineinfo;
875 
876 		if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
877 			return -EFAULT;
878 
879 		/* this doubles as a range check on line_offset */
880 		desc = gpiochip_get_desc(gc, lineinfo.line_offset);
881 		if (IS_ERR(desc))
882 			return PTR_ERR(desc);
883 
884 		if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
885 			return -EBUSY;
886 
887 		gpio_desc_to_lineinfo(desc, &lineinfo);
888 
889 		if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
890 			clear_bit(lineinfo.line_offset, cdev->watched_lines);
891 			return -EFAULT;
892 		}
893 
894 		return 0;
895 	} else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) {
896 		if (copy_from_user(&offset, ip, sizeof(offset)))
897 			return -EFAULT;
898 
899 		if (offset >= cdev->gdev->ngpio)
900 			return -EINVAL;
901 
902 		if (!test_and_clear_bit(offset, cdev->watched_lines))
903 			return -EBUSY;
904 
905 		return 0;
906 	}
907 	return -EINVAL;
908 }
909 
910 #ifdef CONFIG_COMPAT
911 static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
912 			      unsigned long arg)
913 {
914 	return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
915 }
916 #endif
917 
918 static struct gpio_chardev_data *
919 to_gpio_chardev_data(struct notifier_block *nb)
920 {
921 	return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
922 }
923 
924 static int lineinfo_changed_notify(struct notifier_block *nb,
925 				   unsigned long action, void *data)
926 {
927 	struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
928 	struct gpioline_info_changed chg;
929 	struct gpio_desc *desc = data;
930 	int ret;
931 
932 	if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
933 		return NOTIFY_DONE;
934 
935 	memset(&chg, 0, sizeof(chg));
936 	chg.info.line_offset = gpio_chip_hwgpio(desc);
937 	chg.event_type = action;
938 	chg.timestamp = ktime_get_ns();
939 	gpio_desc_to_lineinfo(desc, &chg.info);
940 
941 	ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
942 	if (ret)
943 		wake_up_poll(&cdev->wait, EPOLLIN);
944 	else
945 		pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
946 
947 	return NOTIFY_OK;
948 }
949 
950 static __poll_t lineinfo_watch_poll(struct file *file,
951 				    struct poll_table_struct *pollt)
952 {
953 	struct gpio_chardev_data *cdev = file->private_data;
954 	__poll_t events = 0;
955 
956 	poll_wait(file, &cdev->wait, pollt);
957 
958 	if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
959 						 &cdev->wait.lock))
960 		events = EPOLLIN | EPOLLRDNORM;
961 
962 	return events;
963 }
964 
965 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
966 				   size_t count, loff_t *off)
967 {
968 	struct gpio_chardev_data *cdev = file->private_data;
969 	struct gpioline_info_changed event;
970 	ssize_t bytes_read = 0;
971 	int ret;
972 
973 	if (count < sizeof(event))
974 		return -EINVAL;
975 
976 	do {
977 		spin_lock(&cdev->wait.lock);
978 		if (kfifo_is_empty(&cdev->events)) {
979 			if (bytes_read) {
980 				spin_unlock(&cdev->wait.lock);
981 				return bytes_read;
982 			}
983 
984 			if (file->f_flags & O_NONBLOCK) {
985 				spin_unlock(&cdev->wait.lock);
986 				return -EAGAIN;
987 			}
988 
989 			ret = wait_event_interruptible_locked(cdev->wait,
990 					!kfifo_is_empty(&cdev->events));
991 			if (ret) {
992 				spin_unlock(&cdev->wait.lock);
993 				return ret;
994 			}
995 		}
996 
997 		ret = kfifo_out(&cdev->events, &event, 1);
998 		spin_unlock(&cdev->wait.lock);
999 		if (ret != 1) {
1000 			ret = -EIO;
1001 			break;
1002 			/* We should never get here. See lineevent_read(). */
1003 		}
1004 
1005 		if (copy_to_user(buf + bytes_read, &event, sizeof(event)))
1006 			return -EFAULT;
1007 		bytes_read += sizeof(event);
1008 	} while (count >= bytes_read + sizeof(event));
1009 
1010 	return bytes_read;
1011 }
1012 
1013 /**
1014  * gpio_chrdev_open() - open the chardev for ioctl operations
1015  * @inode: inode for this chardev
1016  * @file: file struct for storing private data
1017  * Returns 0 on success
1018  */
1019 static int gpio_chrdev_open(struct inode *inode, struct file *file)
1020 {
1021 	struct gpio_device *gdev = container_of(inode->i_cdev,
1022 						struct gpio_device, chrdev);
1023 	struct gpio_chardev_data *cdev;
1024 	int ret = -ENOMEM;
1025 
1026 	/* Fail on open if the backing gpiochip is gone */
1027 	if (!gdev->chip)
1028 		return -ENODEV;
1029 
1030 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
1031 	if (!cdev)
1032 		return -ENOMEM;
1033 
1034 	cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
1035 	if (!cdev->watched_lines)
1036 		goto out_free_cdev;
1037 
1038 	init_waitqueue_head(&cdev->wait);
1039 	INIT_KFIFO(cdev->events);
1040 	cdev->gdev = gdev;
1041 
1042 	cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
1043 	ret = blocking_notifier_chain_register(&gdev->notifier,
1044 					       &cdev->lineinfo_changed_nb);
1045 	if (ret)
1046 		goto out_free_bitmap;
1047 
1048 	get_device(&gdev->dev);
1049 	file->private_data = cdev;
1050 
1051 	ret = nonseekable_open(inode, file);
1052 	if (ret)
1053 		goto out_unregister_notifier;
1054 
1055 	return ret;
1056 
1057 out_unregister_notifier:
1058 	blocking_notifier_chain_unregister(&gdev->notifier,
1059 					   &cdev->lineinfo_changed_nb);
1060 out_free_bitmap:
1061 	bitmap_free(cdev->watched_lines);
1062 out_free_cdev:
1063 	kfree(cdev);
1064 	return ret;
1065 }
1066 
1067 /**
1068  * gpio_chrdev_release() - close chardev after ioctl operations
1069  * @inode: inode for this chardev
1070  * @file: file struct for storing private data
1071  * Returns 0 on success
1072  */
1073 static int gpio_chrdev_release(struct inode *inode, struct file *file)
1074 {
1075 	struct gpio_chardev_data *cdev = file->private_data;
1076 	struct gpio_device *gdev = cdev->gdev;
1077 
1078 	bitmap_free(cdev->watched_lines);
1079 	blocking_notifier_chain_unregister(&gdev->notifier,
1080 					   &cdev->lineinfo_changed_nb);
1081 	put_device(&gdev->dev);
1082 	kfree(cdev);
1083 
1084 	return 0;
1085 }
1086 
1087 static const struct file_operations gpio_fileops = {
1088 	.release = gpio_chrdev_release,
1089 	.open = gpio_chrdev_open,
1090 	.poll = lineinfo_watch_poll,
1091 	.read = lineinfo_watch_read,
1092 	.owner = THIS_MODULE,
1093 	.llseek = no_llseek,
1094 	.unlocked_ioctl = gpio_ioctl,
1095 #ifdef CONFIG_COMPAT
1096 	.compat_ioctl = gpio_ioctl_compat,
1097 #endif
1098 };
1099 
1100 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
1101 {
1102 	int ret;
1103 
1104 	cdev_init(&gdev->chrdev, &gpio_fileops);
1105 	gdev->chrdev.owner = THIS_MODULE;
1106 	gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
1107 
1108 	ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
1109 	if (ret)
1110 		return ret;
1111 
1112 	chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1113 		 MAJOR(devt), gdev->id);
1114 
1115 	return 0;
1116 }
1117 
1118 void gpiolib_cdev_unregister(struct gpio_device *gdev)
1119 {
1120 	cdev_device_del(&gdev->chrdev, &gdev->dev);
1121 }
1122