xref: /openbmc/linux/drivers/gpio/gpiolib-cdev.c (revision cb8f63b8cbf39845244f3ccae43bb7e63bd70543)
1925ca369SKent Gibson // SPDX-License-Identifier: GPL-2.0
2925ca369SKent Gibson 
3d189f627SKent Gibson #include <linux/anon_inodes.h>
43c0d9c63SKent Gibson #include <linux/atomic.h>
5925ca369SKent Gibson #include <linux/bitmap.h>
63c0d9c63SKent Gibson #include <linux/build_bug.h>
7d189f627SKent Gibson #include <linux/cdev.h>
8d189f627SKent Gibson #include <linux/compat.h>
965cff704SKent Gibson #include <linux/compiler.h>
10925ca369SKent Gibson #include <linux/device.h>
11925ca369SKent Gibson #include <linux/err.h>
12d189f627SKent Gibson #include <linux/file.h>
13925ca369SKent Gibson #include <linux/gpio.h>
14925ca369SKent Gibson #include <linux/gpio/driver.h>
15d189f627SKent Gibson #include <linux/interrupt.h>
16d189f627SKent Gibson #include <linux/irqreturn.h>
17d189f627SKent Gibson #include <linux/kernel.h>
18925ca369SKent Gibson #include <linux/kfifo.h>
19d189f627SKent Gibson #include <linux/module.h>
20a54756cbSKent Gibson #include <linux/mutex.h>
21d189f627SKent Gibson #include <linux/pinctrl/consumer.h>
22925ca369SKent Gibson #include <linux/poll.h>
23d189f627SKent Gibson #include <linux/spinlock.h>
24925ca369SKent Gibson #include <linux/timekeeping.h>
25d189f627SKent Gibson #include <linux/uaccess.h>
2665cff704SKent Gibson #include <linux/workqueue.h>
27925ca369SKent Gibson #include <uapi/linux/gpio.h>
28925ca369SKent Gibson 
29925ca369SKent Gibson #include "gpiolib.h"
30925ca369SKent Gibson #include "gpiolib-cdev.h"
31925ca369SKent Gibson 
323c0d9c63SKent Gibson /*
333c0d9c63SKent Gibson  * Array sizes must ensure 64-bit alignment and not create holes in the
343c0d9c63SKent Gibson  * struct packing.
353c0d9c63SKent Gibson  */
363c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2));
373c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8));
383c0d9c63SKent Gibson 
393c0d9c63SKent Gibson /*
403c0d9c63SKent Gibson  * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility
413c0d9c63SKent Gibson  */
423c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8));
433c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8));
443c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8));
453c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8));
463c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8));
473c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8));
483c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8));
493c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8));
503c0d9c63SKent Gibson 
51925ca369SKent Gibson /* Character device interface to GPIO.
52925ca369SKent Gibson  *
53925ca369SKent Gibson  * The GPIO character device, /dev/gpiochipN, provides userspace an
54925ca369SKent Gibson  * interface to gpiolib GPIOs via ioctl()s.
55925ca369SKent Gibson  */
56925ca369SKent Gibson 
57925ca369SKent Gibson /*
58925ca369SKent Gibson  * GPIO line handle management
59925ca369SKent Gibson  */
60925ca369SKent Gibson 
613c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
62925ca369SKent Gibson /**
63925ca369SKent Gibson  * struct linehandle_state - contains the state of a userspace handle
64925ca369SKent Gibson  * @gdev: the GPIO device the handle pertains to
65925ca369SKent Gibson  * @label: consumer label used to tag descriptors
66925ca369SKent Gibson  * @descs: the GPIO descriptors held by this handle
6752b7b596SKent Gibson  * @num_descs: the number of descriptors held in the descs array
68925ca369SKent Gibson  */
69925ca369SKent Gibson struct linehandle_state {
70925ca369SKent Gibson 	struct gpio_device *gdev;
71925ca369SKent Gibson 	const char *label;
72925ca369SKent Gibson 	struct gpio_desc *descs[GPIOHANDLES_MAX];
7352b7b596SKent Gibson 	u32 num_descs;
74925ca369SKent Gibson };
75925ca369SKent Gibson 
76925ca369SKent Gibson #define GPIOHANDLE_REQUEST_VALID_FLAGS \
77925ca369SKent Gibson 	(GPIOHANDLE_REQUEST_INPUT | \
78925ca369SKent Gibson 	GPIOHANDLE_REQUEST_OUTPUT | \
79925ca369SKent Gibson 	GPIOHANDLE_REQUEST_ACTIVE_LOW | \
80925ca369SKent Gibson 	GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
81925ca369SKent Gibson 	GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
82925ca369SKent Gibson 	GPIOHANDLE_REQUEST_BIAS_DISABLE | \
83925ca369SKent Gibson 	GPIOHANDLE_REQUEST_OPEN_DRAIN | \
84925ca369SKent Gibson 	GPIOHANDLE_REQUEST_OPEN_SOURCE)
85925ca369SKent Gibson 
86925ca369SKent Gibson static int linehandle_validate_flags(u32 flags)
87925ca369SKent Gibson {
88925ca369SKent Gibson 	/* Return an error if an unknown flag is set */
89925ca369SKent Gibson 	if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
90925ca369SKent Gibson 		return -EINVAL;
91925ca369SKent Gibson 
92925ca369SKent Gibson 	/*
93925ca369SKent Gibson 	 * Do not allow both INPUT & OUTPUT flags to be set as they are
94925ca369SKent Gibson 	 * contradictory.
95925ca369SKent Gibson 	 */
96925ca369SKent Gibson 	if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
97925ca369SKent Gibson 	    (flags & GPIOHANDLE_REQUEST_OUTPUT))
98925ca369SKent Gibson 		return -EINVAL;
99925ca369SKent Gibson 
100925ca369SKent Gibson 	/*
101925ca369SKent Gibson 	 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
102925ca369SKent Gibson 	 * the hardware actually supports enabling both at the same time the
103925ca369SKent Gibson 	 * electrical result would be disastrous.
104925ca369SKent Gibson 	 */
105925ca369SKent Gibson 	if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
106925ca369SKent Gibson 	    (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
107925ca369SKent Gibson 		return -EINVAL;
108925ca369SKent Gibson 
109925ca369SKent Gibson 	/* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
110925ca369SKent Gibson 	if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
111925ca369SKent Gibson 	    ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
112925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
113925ca369SKent Gibson 		return -EINVAL;
114925ca369SKent Gibson 
115925ca369SKent Gibson 	/* Bias flags only allowed for input or output mode. */
116925ca369SKent Gibson 	if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
117925ca369SKent Gibson 	      (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
118925ca369SKent Gibson 	    ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
119925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
120925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
121925ca369SKent Gibson 		return -EINVAL;
122925ca369SKent Gibson 
123925ca369SKent Gibson 	/* Only one bias flag can be set. */
124925ca369SKent Gibson 	if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
125925ca369SKent Gibson 	     (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
126925ca369SKent Gibson 		       GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
127925ca369SKent Gibson 	    ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
128925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
129925ca369SKent Gibson 		return -EINVAL;
130925ca369SKent Gibson 
131925ca369SKent Gibson 	return 0;
132925ca369SKent Gibson }
133925ca369SKent Gibson 
134c274b58aSKent Gibson static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
135c274b58aSKent Gibson {
136c274b58aSKent Gibson 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
137c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
138c274b58aSKent Gibson 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
139c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
140c274b58aSKent Gibson 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
141c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
142c274b58aSKent Gibson 	assign_bit(FLAG_PULL_UP, flagsp,
143c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
144c274b58aSKent Gibson 	assign_bit(FLAG_PULL_DOWN, flagsp,
145c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
146c274b58aSKent Gibson 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
147c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
148c274b58aSKent Gibson }
149c274b58aSKent Gibson 
150925ca369SKent Gibson static long linehandle_set_config(struct linehandle_state *lh,
151925ca369SKent Gibson 				  void __user *ip)
152925ca369SKent Gibson {
153925ca369SKent Gibson 	struct gpiohandle_config gcnf;
154925ca369SKent Gibson 	struct gpio_desc *desc;
155925ca369SKent Gibson 	int i, ret;
156925ca369SKent Gibson 	u32 lflags;
157925ca369SKent Gibson 
158925ca369SKent Gibson 	if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
159925ca369SKent Gibson 		return -EFAULT;
160925ca369SKent Gibson 
161925ca369SKent Gibson 	lflags = gcnf.flags;
162925ca369SKent Gibson 	ret = linehandle_validate_flags(lflags);
163925ca369SKent Gibson 	if (ret)
164925ca369SKent Gibson 		return ret;
165925ca369SKent Gibson 
16652b7b596SKent Gibson 	for (i = 0; i < lh->num_descs; i++) {
167925ca369SKent Gibson 		desc = lh->descs[i];
168c274b58aSKent Gibson 		linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags);
169925ca369SKent Gibson 
170925ca369SKent Gibson 		/*
171925ca369SKent Gibson 		 * Lines have to be requested explicitly for input
172925ca369SKent Gibson 		 * or output, else the line will be treated "as is".
173925ca369SKent Gibson 		 */
174925ca369SKent Gibson 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
175925ca369SKent Gibson 			int val = !!gcnf.default_values[i];
176925ca369SKent Gibson 
177925ca369SKent Gibson 			ret = gpiod_direction_output(desc, val);
178925ca369SKent Gibson 			if (ret)
179925ca369SKent Gibson 				return ret;
180925ca369SKent Gibson 		} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
181925ca369SKent Gibson 			ret = gpiod_direction_input(desc);
182925ca369SKent Gibson 			if (ret)
183925ca369SKent Gibson 				return ret;
184925ca369SKent Gibson 		}
185925ca369SKent Gibson 
1866accc376SKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
187aad95584SKent Gibson 					     GPIO_V2_LINE_CHANGED_CONFIG,
188aad95584SKent Gibson 					     desc);
189925ca369SKent Gibson 	}
190925ca369SKent Gibson 	return 0;
191925ca369SKent Gibson }
192925ca369SKent Gibson 
19349bc5279SKent Gibson static long linehandle_ioctl(struct file *file, unsigned int cmd,
194925ca369SKent Gibson 			     unsigned long arg)
195925ca369SKent Gibson {
19649bc5279SKent Gibson 	struct linehandle_state *lh = file->private_data;
197925ca369SKent Gibson 	void __user *ip = (void __user *)arg;
198925ca369SKent Gibson 	struct gpiohandle_data ghd;
199925ca369SKent Gibson 	DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
200925ca369SKent Gibson 	int i;
201925ca369SKent Gibson 
202925ca369SKent Gibson 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
203925ca369SKent Gibson 		/* NOTE: It's ok to read values of output lines. */
204925ca369SKent Gibson 		int ret = gpiod_get_array_value_complex(false,
205925ca369SKent Gibson 							true,
20652b7b596SKent Gibson 							lh->num_descs,
207925ca369SKent Gibson 							lh->descs,
208925ca369SKent Gibson 							NULL,
209925ca369SKent Gibson 							vals);
210925ca369SKent Gibson 		if (ret)
211925ca369SKent Gibson 			return ret;
212925ca369SKent Gibson 
213925ca369SKent Gibson 		memset(&ghd, 0, sizeof(ghd));
21452b7b596SKent Gibson 		for (i = 0; i < lh->num_descs; i++)
215925ca369SKent Gibson 			ghd.values[i] = test_bit(i, vals);
216925ca369SKent Gibson 
217925ca369SKent Gibson 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
218925ca369SKent Gibson 			return -EFAULT;
219925ca369SKent Gibson 
220925ca369SKent Gibson 		return 0;
221925ca369SKent Gibson 	} else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
222925ca369SKent Gibson 		/*
223925ca369SKent Gibson 		 * All line descriptors were created at once with the same
224925ca369SKent Gibson 		 * flags so just check if the first one is really output.
225925ca369SKent Gibson 		 */
226925ca369SKent Gibson 		if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
227925ca369SKent Gibson 			return -EPERM;
228925ca369SKent Gibson 
229925ca369SKent Gibson 		if (copy_from_user(&ghd, ip, sizeof(ghd)))
230925ca369SKent Gibson 			return -EFAULT;
231925ca369SKent Gibson 
232925ca369SKent Gibson 		/* Clamp all values to [0,1] */
23352b7b596SKent Gibson 		for (i = 0; i < lh->num_descs; i++)
234925ca369SKent Gibson 			__assign_bit(i, vals, ghd.values[i]);
235925ca369SKent Gibson 
236925ca369SKent Gibson 		/* Reuse the array setting function */
237925ca369SKent Gibson 		return gpiod_set_array_value_complex(false,
238925ca369SKent Gibson 						     true,
23952b7b596SKent Gibson 						     lh->num_descs,
240925ca369SKent Gibson 						     lh->descs,
241925ca369SKent Gibson 						     NULL,
242925ca369SKent Gibson 						     vals);
243925ca369SKent Gibson 	} else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) {
244925ca369SKent Gibson 		return linehandle_set_config(lh, ip);
245925ca369SKent Gibson 	}
246925ca369SKent Gibson 	return -EINVAL;
247925ca369SKent Gibson }
248925ca369SKent Gibson 
249925ca369SKent Gibson #ifdef CONFIG_COMPAT
25049bc5279SKent Gibson static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
251925ca369SKent Gibson 				    unsigned long arg)
252925ca369SKent Gibson {
25349bc5279SKent Gibson 	return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
254925ca369SKent Gibson }
255925ca369SKent Gibson #endif
256925ca369SKent Gibson 
257883f9198SKent Gibson static void linehandle_free(struct linehandle_state *lh)
258925ca369SKent Gibson {
259925ca369SKent Gibson 	int i;
260925ca369SKent Gibson 
26152b7b596SKent Gibson 	for (i = 0; i < lh->num_descs; i++)
262883f9198SKent Gibson 		if (lh->descs[i])
263925ca369SKent Gibson 			gpiod_free(lh->descs[i]);
264925ca369SKent Gibson 	kfree(lh->label);
265883f9198SKent Gibson 	put_device(&lh->gdev->dev);
266925ca369SKent Gibson 	kfree(lh);
267883f9198SKent Gibson }
268883f9198SKent Gibson 
269883f9198SKent Gibson static int linehandle_release(struct inode *inode, struct file *file)
270883f9198SKent Gibson {
271883f9198SKent Gibson 	linehandle_free(file->private_data);
272925ca369SKent Gibson 	return 0;
273925ca369SKent Gibson }
274925ca369SKent Gibson 
275925ca369SKent Gibson static const struct file_operations linehandle_fileops = {
276925ca369SKent Gibson 	.release = linehandle_release,
277925ca369SKent Gibson 	.owner = THIS_MODULE,
278925ca369SKent Gibson 	.llseek = noop_llseek,
279925ca369SKent Gibson 	.unlocked_ioctl = linehandle_ioctl,
280925ca369SKent Gibson #ifdef CONFIG_COMPAT
281925ca369SKent Gibson 	.compat_ioctl = linehandle_ioctl_compat,
282925ca369SKent Gibson #endif
283925ca369SKent Gibson };
284925ca369SKent Gibson 
285925ca369SKent Gibson static int linehandle_create(struct gpio_device *gdev, void __user *ip)
286925ca369SKent Gibson {
287925ca369SKent Gibson 	struct gpiohandle_request handlereq;
288925ca369SKent Gibson 	struct linehandle_state *lh;
289925ca369SKent Gibson 	struct file *file;
290883f9198SKent Gibson 	int fd, i, ret;
291925ca369SKent Gibson 	u32 lflags;
292925ca369SKent Gibson 
293925ca369SKent Gibson 	if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
294925ca369SKent Gibson 		return -EFAULT;
295925ca369SKent Gibson 	if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
296925ca369SKent Gibson 		return -EINVAL;
297925ca369SKent Gibson 
298925ca369SKent Gibson 	lflags = handlereq.flags;
299925ca369SKent Gibson 
300925ca369SKent Gibson 	ret = linehandle_validate_flags(lflags);
301925ca369SKent Gibson 	if (ret)
302925ca369SKent Gibson 		return ret;
303925ca369SKent Gibson 
304925ca369SKent Gibson 	lh = kzalloc(sizeof(*lh), GFP_KERNEL);
305925ca369SKent Gibson 	if (!lh)
306925ca369SKent Gibson 		return -ENOMEM;
307925ca369SKent Gibson 	lh->gdev = gdev;
308925ca369SKent Gibson 	get_device(&gdev->dev);
309925ca369SKent Gibson 
310f188ac12SKent Gibson 	if (handlereq.consumer_label[0] != '\0') {
311f188ac12SKent Gibson 		/* label is only initialized if consumer_label is set */
312f188ac12SKent Gibson 		lh->label = kstrndup(handlereq.consumer_label,
313f188ac12SKent Gibson 				     sizeof(handlereq.consumer_label) - 1,
314925ca369SKent Gibson 				     GFP_KERNEL);
315925ca369SKent Gibson 		if (!lh->label) {
316925ca369SKent Gibson 			ret = -ENOMEM;
317925ca369SKent Gibson 			goto out_free_lh;
318925ca369SKent Gibson 		}
319925ca369SKent Gibson 	}
320925ca369SKent Gibson 
321883f9198SKent Gibson 	lh->num_descs = handlereq.lines;
322883f9198SKent Gibson 
323925ca369SKent Gibson 	/* Request each GPIO */
324925ca369SKent Gibson 	for (i = 0; i < handlereq.lines; i++) {
325925ca369SKent Gibson 		u32 offset = handlereq.lineoffsets[i];
326925ca369SKent Gibson 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
327925ca369SKent Gibson 
328925ca369SKent Gibson 		if (IS_ERR(desc)) {
329925ca369SKent Gibson 			ret = PTR_ERR(desc);
330883f9198SKent Gibson 			goto out_free_lh;
331925ca369SKent Gibson 		}
332925ca369SKent Gibson 
333925ca369SKent Gibson 		ret = gpiod_request(desc, lh->label);
334925ca369SKent Gibson 		if (ret)
335883f9198SKent Gibson 			goto out_free_lh;
336925ca369SKent Gibson 		lh->descs[i] = desc;
337c274b58aSKent Gibson 		linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
338925ca369SKent Gibson 
339925ca369SKent Gibson 		ret = gpiod_set_transitory(desc, false);
340925ca369SKent Gibson 		if (ret < 0)
341883f9198SKent Gibson 			goto out_free_lh;
342925ca369SKent Gibson 
343925ca369SKent Gibson 		/*
344925ca369SKent Gibson 		 * Lines have to be requested explicitly for input
345925ca369SKent Gibson 		 * or output, else the line will be treated "as is".
346925ca369SKent Gibson 		 */
347925ca369SKent Gibson 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
348925ca369SKent Gibson 			int val = !!handlereq.default_values[i];
349925ca369SKent Gibson 
350925ca369SKent Gibson 			ret = gpiod_direction_output(desc, val);
351925ca369SKent Gibson 			if (ret)
352883f9198SKent Gibson 				goto out_free_lh;
353925ca369SKent Gibson 		} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
354925ca369SKent Gibson 			ret = gpiod_direction_input(desc);
355925ca369SKent Gibson 			if (ret)
356883f9198SKent Gibson 				goto out_free_lh;
357925ca369SKent Gibson 		}
358925ca369SKent Gibson 
3596accc376SKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
360aad95584SKent Gibson 					     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
361925ca369SKent Gibson 
362925ca369SKent Gibson 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
363925ca369SKent Gibson 			offset);
364925ca369SKent Gibson 	}
365925ca369SKent Gibson 
366925ca369SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
367925ca369SKent Gibson 	if (fd < 0) {
368925ca369SKent Gibson 		ret = fd;
369883f9198SKent Gibson 		goto out_free_lh;
370925ca369SKent Gibson 	}
371925ca369SKent Gibson 
372925ca369SKent Gibson 	file = anon_inode_getfile("gpio-linehandle",
373925ca369SKent Gibson 				  &linehandle_fileops,
374925ca369SKent Gibson 				  lh,
375925ca369SKent Gibson 				  O_RDONLY | O_CLOEXEC);
376925ca369SKent Gibson 	if (IS_ERR(file)) {
377925ca369SKent Gibson 		ret = PTR_ERR(file);
378925ca369SKent Gibson 		goto out_put_unused_fd;
379925ca369SKent Gibson 	}
380925ca369SKent Gibson 
381925ca369SKent Gibson 	handlereq.fd = fd;
382925ca369SKent Gibson 	if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
383925ca369SKent Gibson 		/*
384925ca369SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
385925ca369SKent Gibson 		 * the regular error cleanup path here.
386925ca369SKent Gibson 		 */
387925ca369SKent Gibson 		fput(file);
388925ca369SKent Gibson 		put_unused_fd(fd);
389925ca369SKent Gibson 		return -EFAULT;
390925ca369SKent Gibson 	}
391925ca369SKent Gibson 
392925ca369SKent Gibson 	fd_install(fd, file);
393925ca369SKent Gibson 
394925ca369SKent Gibson 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
39552b7b596SKent Gibson 		lh->num_descs);
396925ca369SKent Gibson 
397925ca369SKent Gibson 	return 0;
398925ca369SKent Gibson 
399925ca369SKent Gibson out_put_unused_fd:
400925ca369SKent Gibson 	put_unused_fd(fd);
401925ca369SKent Gibson out_free_lh:
402883f9198SKent Gibson 	linehandle_free(lh);
403925ca369SKent Gibson 	return ret;
404925ca369SKent Gibson }
4053c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
4063c0d9c63SKent Gibson 
4073c0d9c63SKent Gibson /**
4083c0d9c63SKent Gibson  * struct line - contains the state of a requested line
4093c0d9c63SKent Gibson  * @desc: the GPIO descriptor for this line.
41073e03419SKent Gibson  * @req: the corresponding line request
41173e03419SKent Gibson  * @irq: the interrupt triggered in response to events on this GPIO
41273e03419SKent Gibson  * @eflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or
41373e03419SKent Gibson  * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied
41473e03419SKent Gibson  * @timestamp_ns: cache for the timestamp storing it between hardirq and
41573e03419SKent Gibson  * IRQ thread, used to bring the timestamp close to the actual event
41673e03419SKent Gibson  * @req_seqno: the seqno for the current edge event in the sequence of
41773e03419SKent Gibson  * events for the corresponding line request. This is drawn from the @req.
41873e03419SKent Gibson  * @line_seqno: the seqno for the current edge event in the sequence of
41973e03419SKent Gibson  * events for this line.
42065cff704SKent Gibson  * @work: the worker that implements software debouncing
42165cff704SKent Gibson  * @sw_debounced: flag indicating if the software debouncer is active
42265cff704SKent Gibson  * @level: the current debounced physical level of the line
4233c0d9c63SKent Gibson  */
4243c0d9c63SKent Gibson struct line {
4253c0d9c63SKent Gibson 	struct gpio_desc *desc;
42673e03419SKent Gibson 	/*
42773e03419SKent Gibson 	 * -- edge detector specific fields --
42873e03419SKent Gibson 	 */
42973e03419SKent Gibson 	struct linereq *req;
43073e03419SKent Gibson 	unsigned int irq;
4313ffb7c45SKent Gibson 	/*
4323ffb7c45SKent Gibson 	 * eflags is set by edge_detector_setup(), edge_detector_stop() and
4333ffb7c45SKent Gibson 	 * edge_detector_update(), which are themselves mutually exclusive,
4343ffb7c45SKent Gibson 	 * and is accessed by edge_irq_thread() and debounce_work_func(),
4353ffb7c45SKent Gibson 	 * which can both live with a slightly stale value.
4363ffb7c45SKent Gibson 	 */
43773e03419SKent Gibson 	u64 eflags;
43873e03419SKent Gibson 	/*
43973e03419SKent Gibson 	 * timestamp_ns and req_seqno are accessed only by
44073e03419SKent Gibson 	 * edge_irq_handler() and edge_irq_thread(), which are themselves
44173e03419SKent Gibson 	 * mutually exclusive, so no additional protection is necessary.
44273e03419SKent Gibson 	 */
44373e03419SKent Gibson 	u64 timestamp_ns;
44473e03419SKent Gibson 	u32 req_seqno;
44565cff704SKent Gibson 	/*
44665cff704SKent Gibson 	 * line_seqno is accessed by either edge_irq_thread() or
44765cff704SKent Gibson 	 * debounce_work_func(), which are themselves mutually exclusive,
44865cff704SKent Gibson 	 * so no additional protection is necessary.
44965cff704SKent Gibson 	 */
45073e03419SKent Gibson 	u32 line_seqno;
45165cff704SKent Gibson 	/*
45265cff704SKent Gibson 	 * -- debouncer specific fields --
45365cff704SKent Gibson 	 */
45465cff704SKent Gibson 	struct delayed_work work;
45565cff704SKent Gibson 	/*
45665cff704SKent Gibson 	 * sw_debounce is accessed by linereq_set_config(), which is the
45765cff704SKent Gibson 	 * only setter, and linereq_get_values(), which can live with a
45865cff704SKent Gibson 	 * slightly stale value.
45965cff704SKent Gibson 	 */
46065cff704SKent Gibson 	unsigned int sw_debounced;
46165cff704SKent Gibson 	/*
46265cff704SKent Gibson 	 * level is accessed by debounce_work_func(), which is the only
46365cff704SKent Gibson 	 * setter, and linereq_get_values() which can live with a slightly
46465cff704SKent Gibson 	 * stale value.
46565cff704SKent Gibson 	 */
46665cff704SKent Gibson 	unsigned int level;
4673c0d9c63SKent Gibson };
4683c0d9c63SKent Gibson 
4693c0d9c63SKent Gibson /**
4703c0d9c63SKent Gibson  * struct linereq - contains the state of a userspace line request
4713c0d9c63SKent Gibson  * @gdev: the GPIO device the line request pertains to
4723c0d9c63SKent Gibson  * @label: consumer label used to tag GPIO descriptors
4733c0d9c63SKent Gibson  * @num_lines: the number of lines in the lines array
47473e03419SKent Gibson  * @wait: wait queue that handles blocking reads of events
47573e03419SKent Gibson  * @event_buffer_size: the number of elements allocated in @events
47673e03419SKent Gibson  * @events: KFIFO for the GPIO events
47773e03419SKent Gibson  * @seqno: the sequence number for edge events generated on all lines in
47873e03419SKent Gibson  * this line request.  Note that this is not used when @num_lines is 1, as
47973e03419SKent Gibson  * the line_seqno is then the same and is cheaper to calculate.
480a54756cbSKent Gibson  * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
481a54756cbSKent Gibson  * of configuration, particularly multi-step accesses to desc flags.
4823c0d9c63SKent Gibson  * @lines: the lines held by this line request, with @num_lines elements.
4833c0d9c63SKent Gibson  */
4843c0d9c63SKent Gibson struct linereq {
4853c0d9c63SKent Gibson 	struct gpio_device *gdev;
4863c0d9c63SKent Gibson 	const char *label;
4873c0d9c63SKent Gibson 	u32 num_lines;
48873e03419SKent Gibson 	wait_queue_head_t wait;
48973e03419SKent Gibson 	u32 event_buffer_size;
49073e03419SKent Gibson 	DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
49173e03419SKent Gibson 	atomic_t seqno;
492a54756cbSKent Gibson 	struct mutex config_mutex;
4933c0d9c63SKent Gibson 	struct line lines[];
4943c0d9c63SKent Gibson };
4953c0d9c63SKent Gibson 
4963c0d9c63SKent Gibson #define GPIO_V2_LINE_BIAS_FLAGS \
4973c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
4983c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
4993c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_BIAS_DISABLED)
5003c0d9c63SKent Gibson 
5013c0d9c63SKent Gibson #define GPIO_V2_LINE_DIRECTION_FLAGS \
5023c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_INPUT | \
5033c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_OUTPUT)
5043c0d9c63SKent Gibson 
5053c0d9c63SKent Gibson #define GPIO_V2_LINE_DRIVE_FLAGS \
5063c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
5073c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_OPEN_SOURCE)
5083c0d9c63SKent Gibson 
50973e03419SKent Gibson #define GPIO_V2_LINE_EDGE_FLAGS \
51073e03419SKent Gibson 	(GPIO_V2_LINE_FLAG_EDGE_RISING | \
51173e03419SKent Gibson 	 GPIO_V2_LINE_FLAG_EDGE_FALLING)
51273e03419SKent Gibson 
5135e2ca893SKent Gibson #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS
5145e2ca893SKent Gibson 
5153c0d9c63SKent Gibson #define GPIO_V2_LINE_VALID_FLAGS \
5163c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
5173c0d9c63SKent Gibson 	 GPIO_V2_LINE_DIRECTION_FLAGS | \
5183c0d9c63SKent Gibson 	 GPIO_V2_LINE_DRIVE_FLAGS | \
51973e03419SKent Gibson 	 GPIO_V2_LINE_EDGE_FLAGS | \
52026d060e4SKent Gibson 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
5213c0d9c63SKent Gibson 	 GPIO_V2_LINE_BIAS_FLAGS)
5223c0d9c63SKent Gibson 
52373e03419SKent Gibson static void linereq_put_event(struct linereq *lr,
52473e03419SKent Gibson 			      struct gpio_v2_line_event *le)
52573e03419SKent Gibson {
52673e03419SKent Gibson 	bool overflow = false;
52773e03419SKent Gibson 
52873e03419SKent Gibson 	spin_lock(&lr->wait.lock);
52973e03419SKent Gibson 	if (kfifo_is_full(&lr->events)) {
53073e03419SKent Gibson 		overflow = true;
53173e03419SKent Gibson 		kfifo_skip(&lr->events);
53273e03419SKent Gibson 	}
53373e03419SKent Gibson 	kfifo_in(&lr->events, le, 1);
53473e03419SKent Gibson 	spin_unlock(&lr->wait.lock);
53573e03419SKent Gibson 	if (!overflow)
53673e03419SKent Gibson 		wake_up_poll(&lr->wait, EPOLLIN);
53773e03419SKent Gibson 	else
53873e03419SKent Gibson 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
53973e03419SKent Gibson }
54073e03419SKent Gibson 
54126d060e4SKent Gibson static u64 line_event_timestamp(struct line *line)
54226d060e4SKent Gibson {
54326d060e4SKent Gibson 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
54426d060e4SKent Gibson 		return ktime_get_real_ns();
54526d060e4SKent Gibson 
54626d060e4SKent Gibson 	return ktime_get_ns();
54726d060e4SKent Gibson }
54826d060e4SKent Gibson 
54973e03419SKent Gibson static irqreturn_t edge_irq_thread(int irq, void *p)
55073e03419SKent Gibson {
55173e03419SKent Gibson 	struct line *line = p;
55273e03419SKent Gibson 	struct linereq *lr = line->req;
55373e03419SKent Gibson 	struct gpio_v2_line_event le;
5543ffb7c45SKent Gibson 	u64 eflags;
55573e03419SKent Gibson 
55673e03419SKent Gibson 	/* Do not leak kernel stack to userspace */
55773e03419SKent Gibson 	memset(&le, 0, sizeof(le));
55873e03419SKent Gibson 
55973e03419SKent Gibson 	if (line->timestamp_ns) {
56073e03419SKent Gibson 		le.timestamp_ns = line->timestamp_ns;
56173e03419SKent Gibson 	} else {
56273e03419SKent Gibson 		/*
56373e03419SKent Gibson 		 * We may be running from a nested threaded interrupt in
56473e03419SKent Gibson 		 * which case we didn't get the timestamp from
56573e03419SKent Gibson 		 * edge_irq_handler().
56673e03419SKent Gibson 		 */
56726d060e4SKent Gibson 		le.timestamp_ns = line_event_timestamp(line);
56873e03419SKent Gibson 		if (lr->num_lines != 1)
56973e03419SKent Gibson 			line->req_seqno = atomic_inc_return(&lr->seqno);
57073e03419SKent Gibson 	}
57173e03419SKent Gibson 	line->timestamp_ns = 0;
57273e03419SKent Gibson 
5733ffb7c45SKent Gibson 	eflags = READ_ONCE(line->eflags);
5745e2ca893SKent Gibson 	if (eflags == GPIO_V2_LINE_FLAG_EDGE_BOTH) {
57573e03419SKent Gibson 		int level = gpiod_get_value_cansleep(line->desc);
57673e03419SKent Gibson 
57773e03419SKent Gibson 		if (level)
57873e03419SKent Gibson 			/* Emit low-to-high event */
57973e03419SKent Gibson 			le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
58073e03419SKent Gibson 		else
58173e03419SKent Gibson 			/* Emit high-to-low event */
58273e03419SKent Gibson 			le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
5833ffb7c45SKent Gibson 	} else if (eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) {
58473e03419SKent Gibson 		/* Emit low-to-high event */
58573e03419SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
5863ffb7c45SKent Gibson 	} else if (eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) {
58773e03419SKent Gibson 		/* Emit high-to-low event */
58873e03419SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
58973e03419SKent Gibson 	} else {
59073e03419SKent Gibson 		return IRQ_NONE;
59173e03419SKent Gibson 	}
59273e03419SKent Gibson 	line->line_seqno++;
59373e03419SKent Gibson 	le.line_seqno = line->line_seqno;
59473e03419SKent Gibson 	le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
59573e03419SKent Gibson 	le.offset = gpio_chip_hwgpio(line->desc);
59673e03419SKent Gibson 
59773e03419SKent Gibson 	linereq_put_event(lr, &le);
59873e03419SKent Gibson 
59973e03419SKent Gibson 	return IRQ_HANDLED;
60073e03419SKent Gibson }
60173e03419SKent Gibson 
60273e03419SKent Gibson static irqreturn_t edge_irq_handler(int irq, void *p)
60373e03419SKent Gibson {
60473e03419SKent Gibson 	struct line *line = p;
60573e03419SKent Gibson 	struct linereq *lr = line->req;
60673e03419SKent Gibson 
60773e03419SKent Gibson 	/*
60873e03419SKent Gibson 	 * Just store the timestamp in hardirq context so we get it as
60973e03419SKent Gibson 	 * close in time as possible to the actual event.
61073e03419SKent Gibson 	 */
61126d060e4SKent Gibson 	line->timestamp_ns = line_event_timestamp(line);
61273e03419SKent Gibson 
61373e03419SKent Gibson 	if (lr->num_lines != 1)
61473e03419SKent Gibson 		line->req_seqno = atomic_inc_return(&lr->seqno);
61573e03419SKent Gibson 
61673e03419SKent Gibson 	return IRQ_WAKE_THREAD;
61773e03419SKent Gibson }
61873e03419SKent Gibson 
61965cff704SKent Gibson /*
62065cff704SKent Gibson  * returns the current debounced logical value.
62165cff704SKent Gibson  */
62265cff704SKent Gibson static bool debounced_value(struct line *line)
62365cff704SKent Gibson {
62465cff704SKent Gibson 	bool value;
62565cff704SKent Gibson 
62665cff704SKent Gibson 	/*
62765cff704SKent Gibson 	 * minor race - debouncer may be stopped here, so edge_detector_stop()
62865cff704SKent Gibson 	 * must leave the value unchanged so the following will read the level
62965cff704SKent Gibson 	 * from when the debouncer was last running.
63065cff704SKent Gibson 	 */
63165cff704SKent Gibson 	value = READ_ONCE(line->level);
63265cff704SKent Gibson 
63365cff704SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
63465cff704SKent Gibson 		value = !value;
63565cff704SKent Gibson 
63665cff704SKent Gibson 	return value;
63765cff704SKent Gibson }
63865cff704SKent Gibson 
63965cff704SKent Gibson static irqreturn_t debounce_irq_handler(int irq, void *p)
64065cff704SKent Gibson {
64165cff704SKent Gibson 	struct line *line = p;
64265cff704SKent Gibson 
64365cff704SKent Gibson 	mod_delayed_work(system_wq, &line->work,
64465cff704SKent Gibson 		usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
64565cff704SKent Gibson 
64665cff704SKent Gibson 	return IRQ_HANDLED;
64765cff704SKent Gibson }
64865cff704SKent Gibson 
64965cff704SKent Gibson static void debounce_work_func(struct work_struct *work)
65065cff704SKent Gibson {
65165cff704SKent Gibson 	struct gpio_v2_line_event le;
65265cff704SKent Gibson 	struct line *line = container_of(work, struct line, work.work);
65365cff704SKent Gibson 	struct linereq *lr;
65465cff704SKent Gibson 	int level;
6553ffb7c45SKent Gibson 	u64 eflags;
65665cff704SKent Gibson 
65765cff704SKent Gibson 	level = gpiod_get_raw_value_cansleep(line->desc);
65865cff704SKent Gibson 	if (level < 0) {
65965cff704SKent Gibson 		pr_debug_ratelimited("debouncer failed to read line value\n");
66065cff704SKent Gibson 		return;
66165cff704SKent Gibson 	}
66265cff704SKent Gibson 
66365cff704SKent Gibson 	if (READ_ONCE(line->level) == level)
66465cff704SKent Gibson 		return;
66565cff704SKent Gibson 
66665cff704SKent Gibson 	WRITE_ONCE(line->level, level);
66765cff704SKent Gibson 
66865cff704SKent Gibson 	/* -- edge detection -- */
6693ffb7c45SKent Gibson 	eflags = READ_ONCE(line->eflags);
6703ffb7c45SKent Gibson 	if (!eflags)
67165cff704SKent Gibson 		return;
67265cff704SKent Gibson 
67365cff704SKent Gibson 	/* switch from physical level to logical - if they differ */
67465cff704SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
67565cff704SKent Gibson 		level = !level;
67665cff704SKent Gibson 
67765cff704SKent Gibson 	/* ignore edges that are not being monitored */
6783ffb7c45SKent Gibson 	if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
6793ffb7c45SKent Gibson 	    ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
68065cff704SKent Gibson 		return;
68165cff704SKent Gibson 
68265cff704SKent Gibson 	/* Do not leak kernel stack to userspace */
68365cff704SKent Gibson 	memset(&le, 0, sizeof(le));
68465cff704SKent Gibson 
68565cff704SKent Gibson 	lr = line->req;
68626d060e4SKent Gibson 	le.timestamp_ns = line_event_timestamp(line);
68765cff704SKent Gibson 	le.offset = gpio_chip_hwgpio(line->desc);
68865cff704SKent Gibson 	line->line_seqno++;
68965cff704SKent Gibson 	le.line_seqno = line->line_seqno;
69065cff704SKent Gibson 	le.seqno = (lr->num_lines == 1) ?
69165cff704SKent Gibson 		le.line_seqno : atomic_inc_return(&lr->seqno);
69265cff704SKent Gibson 
69365cff704SKent Gibson 	if (level)
69465cff704SKent Gibson 		/* Emit low-to-high event */
69565cff704SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
69665cff704SKent Gibson 	else
69765cff704SKent Gibson 		/* Emit high-to-low event */
69865cff704SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
69965cff704SKent Gibson 
70065cff704SKent Gibson 	linereq_put_event(lr, &le);
70165cff704SKent Gibson }
70265cff704SKent Gibson 
70365cff704SKent Gibson static int debounce_setup(struct line *line,
70465cff704SKent Gibson 			  unsigned int debounce_period_us)
70565cff704SKent Gibson {
70665cff704SKent Gibson 	unsigned long irqflags;
70765cff704SKent Gibson 	int ret, level, irq;
70865cff704SKent Gibson 
70965cff704SKent Gibson 	/* try hardware */
71065cff704SKent Gibson 	ret = gpiod_set_debounce(line->desc, debounce_period_us);
71165cff704SKent Gibson 	if (!ret) {
71265cff704SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
71365cff704SKent Gibson 		return ret;
71465cff704SKent Gibson 	}
71565cff704SKent Gibson 	if (ret != -ENOTSUPP)
71665cff704SKent Gibson 		return ret;
71765cff704SKent Gibson 
71865cff704SKent Gibson 	if (debounce_period_us) {
71965cff704SKent Gibson 		/* setup software debounce */
72065cff704SKent Gibson 		level = gpiod_get_raw_value_cansleep(line->desc);
72165cff704SKent Gibson 		if (level < 0)
72265cff704SKent Gibson 			return level;
72365cff704SKent Gibson 
72465cff704SKent Gibson 		irq = gpiod_to_irq(line->desc);
72565cff704SKent Gibson 		if (irq < 0)
72665cff704SKent Gibson 			return -ENXIO;
72765cff704SKent Gibson 
72865cff704SKent Gibson 		WRITE_ONCE(line->level, level);
72965cff704SKent Gibson 		irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
73065cff704SKent Gibson 		ret = request_irq(irq, debounce_irq_handler, irqflags,
73165cff704SKent Gibson 				  line->req->label, line);
73265cff704SKent Gibson 		if (ret)
73365cff704SKent Gibson 			return ret;
73465cff704SKent Gibson 
73565cff704SKent Gibson 		WRITE_ONCE(line->sw_debounced, 1);
73665cff704SKent Gibson 		line->irq = irq;
73765cff704SKent Gibson 	}
73865cff704SKent Gibson 	return 0;
73965cff704SKent Gibson }
74065cff704SKent Gibson 
74165cff704SKent Gibson static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
74265cff704SKent Gibson 					  unsigned int line_idx)
74365cff704SKent Gibson {
74465cff704SKent Gibson 	unsigned int i;
74565cff704SKent Gibson 	u64 mask = BIT_ULL(line_idx);
74665cff704SKent Gibson 
74765cff704SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
74865cff704SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
74965cff704SKent Gibson 		    (lc->attrs[i].mask & mask))
75065cff704SKent Gibson 			return true;
75165cff704SKent Gibson 	}
75265cff704SKent Gibson 	return false;
75365cff704SKent Gibson }
75465cff704SKent Gibson 
75565cff704SKent Gibson static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
75665cff704SKent Gibson 					       unsigned int line_idx)
75765cff704SKent Gibson {
75865cff704SKent Gibson 	unsigned int i;
75965cff704SKent Gibson 	u64 mask = BIT_ULL(line_idx);
76065cff704SKent Gibson 
76165cff704SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
76265cff704SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
76365cff704SKent Gibson 		    (lc->attrs[i].mask & mask))
76465cff704SKent Gibson 			return lc->attrs[i].attr.debounce_period_us;
76565cff704SKent Gibson 	}
76665cff704SKent Gibson 	return 0;
76765cff704SKent Gibson }
76865cff704SKent Gibson 
76973e03419SKent Gibson static void edge_detector_stop(struct line *line)
77073e03419SKent Gibson {
77173e03419SKent Gibson 	if (line->irq) {
77273e03419SKent Gibson 		free_irq(line->irq, line);
77373e03419SKent Gibson 		line->irq = 0;
77473e03419SKent Gibson 	}
775a54756cbSKent Gibson 
77665cff704SKent Gibson 	cancel_delayed_work_sync(&line->work);
77765cff704SKent Gibson 	WRITE_ONCE(line->sw_debounced, 0);
7783ffb7c45SKent Gibson 	WRITE_ONCE(line->eflags, 0);
77903a58ea5SKent Gibson 	if (line->desc)
78003a58ea5SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, 0);
78165cff704SKent Gibson 	/* do not change line->level - see comment in debounced_value() */
78273e03419SKent Gibson }
78373e03419SKent Gibson 
78473e03419SKent Gibson static int edge_detector_setup(struct line *line,
78565cff704SKent Gibson 			       struct gpio_v2_line_config *lc,
78665cff704SKent Gibson 			       unsigned int line_idx,
78773e03419SKent Gibson 			       u64 eflags)
78873e03419SKent Gibson {
78965cff704SKent Gibson 	u32 debounce_period_us;
79073e03419SKent Gibson 	unsigned long irqflags = 0;
79173e03419SKent Gibson 	int irq, ret;
79273e03419SKent Gibson 
79373e03419SKent Gibson 	if (eflags && !kfifo_initialized(&line->req->events)) {
79473e03419SKent Gibson 		ret = kfifo_alloc(&line->req->events,
79573e03419SKent Gibson 				  line->req->event_buffer_size, GFP_KERNEL);
79673e03419SKent Gibson 		if (ret)
79773e03419SKent Gibson 			return ret;
79873e03419SKent Gibson 	}
7993ffb7c45SKent Gibson 	WRITE_ONCE(line->eflags, eflags);
80065cff704SKent Gibson 	if (gpio_v2_line_config_debounced(lc, line_idx)) {
80165cff704SKent Gibson 		debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
80265cff704SKent Gibson 		ret = debounce_setup(line, debounce_period_us);
80365cff704SKent Gibson 		if (ret)
80465cff704SKent Gibson 			return ret;
80565cff704SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
80665cff704SKent Gibson 	}
80773e03419SKent Gibson 
80865cff704SKent Gibson 	/* detection disabled or sw debouncer will provide edge detection */
80965cff704SKent Gibson 	if (!eflags || READ_ONCE(line->sw_debounced))
81073e03419SKent Gibson 		return 0;
81173e03419SKent Gibson 
81273e03419SKent Gibson 	irq = gpiod_to_irq(line->desc);
81373e03419SKent Gibson 	if (irq < 0)
81473e03419SKent Gibson 		return -ENXIO;
81573e03419SKent Gibson 
81673e03419SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
81773e03419SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
81873e03419SKent Gibson 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
81973e03419SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
82073e03419SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
82173e03419SKent Gibson 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
82273e03419SKent Gibson 	irqflags |= IRQF_ONESHOT;
82373e03419SKent Gibson 
82473e03419SKent Gibson 	/* Request a thread to read the events */
82573e03419SKent Gibson 	ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
82673e03419SKent Gibson 				   irqflags, line->req->label, line);
82773e03419SKent Gibson 	if (ret)
82873e03419SKent Gibson 		return ret;
82973e03419SKent Gibson 
83073e03419SKent Gibson 	line->irq = irq;
83173e03419SKent Gibson 	return 0;
83273e03419SKent Gibson }
83373e03419SKent Gibson 
83465cff704SKent Gibson static int edge_detector_update(struct line *line,
83565cff704SKent Gibson 				struct gpio_v2_line_config *lc,
83665cff704SKent Gibson 				unsigned int line_idx,
83765cff704SKent Gibson 				u64 eflags, bool polarity_change)
838a54756cbSKent Gibson {
83965cff704SKent Gibson 	unsigned int debounce_period_us =
84065cff704SKent Gibson 		gpio_v2_line_config_debounce_period(lc, line_idx);
84165cff704SKent Gibson 
8423ffb7c45SKent Gibson 	if ((READ_ONCE(line->eflags) == eflags) && !polarity_change &&
84365cff704SKent Gibson 	    (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us))
844a54756cbSKent Gibson 		return 0;
845a54756cbSKent Gibson 
84665cff704SKent Gibson 	/* sw debounced and still will be...*/
84765cff704SKent Gibson 	if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
8483ffb7c45SKent Gibson 		WRITE_ONCE(line->eflags, eflags);
84965cff704SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
85065cff704SKent Gibson 		return 0;
85165cff704SKent Gibson 	}
85265cff704SKent Gibson 
85365cff704SKent Gibson 	/* reconfiguring edge detection or sw debounce being disabled */
85465cff704SKent Gibson 	if ((line->irq && !READ_ONCE(line->sw_debounced)) ||
85565cff704SKent Gibson 	    (!debounce_period_us && READ_ONCE(line->sw_debounced)))
856a54756cbSKent Gibson 		edge_detector_stop(line);
857a54756cbSKent Gibson 
85865cff704SKent Gibson 	return edge_detector_setup(line, lc, line_idx, eflags);
859a54756cbSKent Gibson }
860a54756cbSKent Gibson 
8613c0d9c63SKent Gibson static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
8623c0d9c63SKent Gibson 				     unsigned int line_idx)
8633c0d9c63SKent Gibson {
8643c0d9c63SKent Gibson 	unsigned int i;
8653c0d9c63SKent Gibson 	u64 mask = BIT_ULL(line_idx);
8663c0d9c63SKent Gibson 
8673c0d9c63SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
8683c0d9c63SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
8693c0d9c63SKent Gibson 		    (lc->attrs[i].mask & mask))
8703c0d9c63SKent Gibson 			return lc->attrs[i].attr.flags;
8713c0d9c63SKent Gibson 	}
8723c0d9c63SKent Gibson 	return lc->flags;
8733c0d9c63SKent Gibson }
8743c0d9c63SKent Gibson 
8753c0d9c63SKent Gibson static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
8763c0d9c63SKent Gibson 					    unsigned int line_idx)
8773c0d9c63SKent Gibson {
8783c0d9c63SKent Gibson 	unsigned int i;
8793c0d9c63SKent Gibson 	u64 mask = BIT_ULL(line_idx);
8803c0d9c63SKent Gibson 
8813c0d9c63SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
8823c0d9c63SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
8833c0d9c63SKent Gibson 		    (lc->attrs[i].mask & mask))
8843c0d9c63SKent Gibson 			return !!(lc->attrs[i].attr.values & mask);
8853c0d9c63SKent Gibson 	}
8863c0d9c63SKent Gibson 	return 0;
8873c0d9c63SKent Gibson }
8883c0d9c63SKent Gibson 
8893c0d9c63SKent Gibson static int gpio_v2_line_flags_validate(u64 flags)
8903c0d9c63SKent Gibson {
8913c0d9c63SKent Gibson 	/* Return an error if an unknown flag is set */
8923c0d9c63SKent Gibson 	if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
8933c0d9c63SKent Gibson 		return -EINVAL;
8943c0d9c63SKent Gibson 
8953c0d9c63SKent Gibson 	/*
8963c0d9c63SKent Gibson 	 * Do not allow both INPUT and OUTPUT flags to be set as they are
8973c0d9c63SKent Gibson 	 * contradictory.
8983c0d9c63SKent Gibson 	 */
8993c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
9003c0d9c63SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_OUTPUT))
9013c0d9c63SKent Gibson 		return -EINVAL;
9023c0d9c63SKent Gibson 
90373e03419SKent Gibson 	/* Edge detection requires explicit input. */
90473e03419SKent Gibson 	if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
90573e03419SKent Gibson 	    !(flags & GPIO_V2_LINE_FLAG_INPUT))
90673e03419SKent Gibson 		return -EINVAL;
90773e03419SKent Gibson 
9083c0d9c63SKent Gibson 	/*
9093c0d9c63SKent Gibson 	 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
9103c0d9c63SKent Gibson 	 * request. If the hardware actually supports enabling both at the
9113c0d9c63SKent Gibson 	 * same time the electrical result would be disastrous.
9123c0d9c63SKent Gibson 	 */
9133c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
9143c0d9c63SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
9153c0d9c63SKent Gibson 		return -EINVAL;
9163c0d9c63SKent Gibson 
9173c0d9c63SKent Gibson 	/* Drive requires explicit output direction. */
9183c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
9193c0d9c63SKent Gibson 	    !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
9203c0d9c63SKent Gibson 		return -EINVAL;
9213c0d9c63SKent Gibson 
9223c0d9c63SKent Gibson 	/* Bias requires explicit direction. */
9233c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
9243c0d9c63SKent Gibson 	    !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
9253c0d9c63SKent Gibson 		return -EINVAL;
9263c0d9c63SKent Gibson 
9273c0d9c63SKent Gibson 	/* Only one bias flag can be set. */
9283c0d9c63SKent Gibson 	if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
9293c0d9c63SKent Gibson 	     (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
9303c0d9c63SKent Gibson 		       GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
9313c0d9c63SKent Gibson 	    ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
9323c0d9c63SKent Gibson 	     (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
9333c0d9c63SKent Gibson 		return -EINVAL;
9343c0d9c63SKent Gibson 
9353c0d9c63SKent Gibson 	return 0;
9363c0d9c63SKent Gibson }
9373c0d9c63SKent Gibson 
9383c0d9c63SKent Gibson static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
9393c0d9c63SKent Gibson 					unsigned int num_lines)
9403c0d9c63SKent Gibson {
9413c0d9c63SKent Gibson 	unsigned int i;
9423c0d9c63SKent Gibson 	u64 flags;
9433c0d9c63SKent Gibson 	int ret;
9443c0d9c63SKent Gibson 
9453c0d9c63SKent Gibson 	if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
9463c0d9c63SKent Gibson 		return -EINVAL;
9473c0d9c63SKent Gibson 
9483c0d9c63SKent Gibson 	if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
9493c0d9c63SKent Gibson 		return -EINVAL;
9503c0d9c63SKent Gibson 
9513c0d9c63SKent Gibson 	for (i = 0; i < num_lines; i++) {
9523c0d9c63SKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
9533c0d9c63SKent Gibson 		ret = gpio_v2_line_flags_validate(flags);
9543c0d9c63SKent Gibson 		if (ret)
9553c0d9c63SKent Gibson 			return ret;
95665cff704SKent Gibson 
95765cff704SKent Gibson 		/* debounce requires explicit input */
95865cff704SKent Gibson 		if (gpio_v2_line_config_debounced(lc, i) &&
95965cff704SKent Gibson 		    !(flags & GPIO_V2_LINE_FLAG_INPUT))
96065cff704SKent Gibson 			return -EINVAL;
9613c0d9c63SKent Gibson 	}
9623c0d9c63SKent Gibson 	return 0;
9633c0d9c63SKent Gibson }
9643c0d9c63SKent Gibson 
9653c0d9c63SKent Gibson static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
9663c0d9c63SKent Gibson 						    unsigned long *flagsp)
9673c0d9c63SKent Gibson {
9683c0d9c63SKent Gibson 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
9693c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
9703c0d9c63SKent Gibson 
9713c0d9c63SKent Gibson 	if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
9723c0d9c63SKent Gibson 		set_bit(FLAG_IS_OUT, flagsp);
9733c0d9c63SKent Gibson 	else if (flags & GPIO_V2_LINE_FLAG_INPUT)
9743c0d9c63SKent Gibson 		clear_bit(FLAG_IS_OUT, flagsp);
9753c0d9c63SKent Gibson 
97673e03419SKent Gibson 	assign_bit(FLAG_EDGE_RISING, flagsp,
97773e03419SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
97873e03419SKent Gibson 	assign_bit(FLAG_EDGE_FALLING, flagsp,
97973e03419SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
98073e03419SKent Gibson 
9813c0d9c63SKent Gibson 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
9823c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
9833c0d9c63SKent Gibson 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
9843c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
9853c0d9c63SKent Gibson 
9863c0d9c63SKent Gibson 	assign_bit(FLAG_PULL_UP, flagsp,
9873c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
9883c0d9c63SKent Gibson 	assign_bit(FLAG_PULL_DOWN, flagsp,
9893c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
9903c0d9c63SKent Gibson 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
9913c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
99226d060e4SKent Gibson 
99326d060e4SKent Gibson 	assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
99426d060e4SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
9953c0d9c63SKent Gibson }
9963c0d9c63SKent Gibson 
9973c0d9c63SKent Gibson static long linereq_get_values(struct linereq *lr, void __user *ip)
9983c0d9c63SKent Gibson {
9993c0d9c63SKent Gibson 	struct gpio_v2_line_values lv;
10003c0d9c63SKent Gibson 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
10013c0d9c63SKent Gibson 	struct gpio_desc **descs;
10023c0d9c63SKent Gibson 	unsigned int i, didx, num_get;
100365cff704SKent Gibson 	bool val;
10043c0d9c63SKent Gibson 	int ret;
10053c0d9c63SKent Gibson 
10063c0d9c63SKent Gibson 	/* NOTE: It's ok to read values of output lines. */
10073c0d9c63SKent Gibson 	if (copy_from_user(&lv, ip, sizeof(lv)))
10083c0d9c63SKent Gibson 		return -EFAULT;
10093c0d9c63SKent Gibson 
10103c0d9c63SKent Gibson 	for (num_get = 0, i = 0; i < lr->num_lines; i++) {
10113c0d9c63SKent Gibson 		if (lv.mask & BIT_ULL(i)) {
10123c0d9c63SKent Gibson 			num_get++;
10133c0d9c63SKent Gibson 			descs = &lr->lines[i].desc;
10143c0d9c63SKent Gibson 		}
10153c0d9c63SKent Gibson 	}
10163c0d9c63SKent Gibson 
10173c0d9c63SKent Gibson 	if (num_get == 0)
10183c0d9c63SKent Gibson 		return -EINVAL;
10193c0d9c63SKent Gibson 
10203c0d9c63SKent Gibson 	if (num_get != 1) {
10213c0d9c63SKent Gibson 		descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
10223c0d9c63SKent Gibson 		if (!descs)
10233c0d9c63SKent Gibson 			return -ENOMEM;
10243c0d9c63SKent Gibson 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
10253c0d9c63SKent Gibson 			if (lv.mask & BIT_ULL(i)) {
10263c0d9c63SKent Gibson 				descs[didx] = lr->lines[i].desc;
10273c0d9c63SKent Gibson 				didx++;
10283c0d9c63SKent Gibson 			}
10293c0d9c63SKent Gibson 		}
10303c0d9c63SKent Gibson 	}
10313c0d9c63SKent Gibson 	ret = gpiod_get_array_value_complex(false, true, num_get,
10323c0d9c63SKent Gibson 					    descs, NULL, vals);
10333c0d9c63SKent Gibson 
10343c0d9c63SKent Gibson 	if (num_get != 1)
10353c0d9c63SKent Gibson 		kfree(descs);
10363c0d9c63SKent Gibson 	if (ret)
10373c0d9c63SKent Gibson 		return ret;
10383c0d9c63SKent Gibson 
10393c0d9c63SKent Gibson 	lv.bits = 0;
10403c0d9c63SKent Gibson 	for (didx = 0, i = 0; i < lr->num_lines; i++) {
10413c0d9c63SKent Gibson 		if (lv.mask & BIT_ULL(i)) {
104265cff704SKent Gibson 			if (lr->lines[i].sw_debounced)
104365cff704SKent Gibson 				val = debounced_value(&lr->lines[i]);
104465cff704SKent Gibson 			else
104565cff704SKent Gibson 				val = test_bit(didx, vals);
104665cff704SKent Gibson 			if (val)
10473c0d9c63SKent Gibson 				lv.bits |= BIT_ULL(i);
10483c0d9c63SKent Gibson 			didx++;
10493c0d9c63SKent Gibson 		}
10503c0d9c63SKent Gibson 	}
10513c0d9c63SKent Gibson 
10523c0d9c63SKent Gibson 	if (copy_to_user(ip, &lv, sizeof(lv)))
10533c0d9c63SKent Gibson 		return -EFAULT;
10543c0d9c63SKent Gibson 
10553c0d9c63SKent Gibson 	return 0;
10563c0d9c63SKent Gibson }
10573c0d9c63SKent Gibson 
10587b8e00d9SKent Gibson static long linereq_set_values_unlocked(struct linereq *lr,
10597b8e00d9SKent Gibson 					struct gpio_v2_line_values *lv)
10607b8e00d9SKent Gibson {
10617b8e00d9SKent Gibson 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
10627b8e00d9SKent Gibson 	struct gpio_desc **descs;
10637b8e00d9SKent Gibson 	unsigned int i, didx, num_set;
10647b8e00d9SKent Gibson 	int ret;
10657b8e00d9SKent Gibson 
10667b8e00d9SKent Gibson 	bitmap_zero(vals, GPIO_V2_LINES_MAX);
10677b8e00d9SKent Gibson 	for (num_set = 0, i = 0; i < lr->num_lines; i++) {
10687b8e00d9SKent Gibson 		if (lv->mask & BIT_ULL(i)) {
10697b8e00d9SKent Gibson 			if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
10707b8e00d9SKent Gibson 				return -EPERM;
10717b8e00d9SKent Gibson 			if (lv->bits & BIT_ULL(i))
10727b8e00d9SKent Gibson 				__set_bit(num_set, vals);
10737b8e00d9SKent Gibson 			num_set++;
10747b8e00d9SKent Gibson 			descs = &lr->lines[i].desc;
10757b8e00d9SKent Gibson 		}
10767b8e00d9SKent Gibson 	}
10777b8e00d9SKent Gibson 	if (num_set == 0)
10787b8e00d9SKent Gibson 		return -EINVAL;
10797b8e00d9SKent Gibson 
10807b8e00d9SKent Gibson 	if (num_set != 1) {
10817b8e00d9SKent Gibson 		/* build compacted desc array and values */
10827b8e00d9SKent Gibson 		descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
10837b8e00d9SKent Gibson 		if (!descs)
10847b8e00d9SKent Gibson 			return -ENOMEM;
10857b8e00d9SKent Gibson 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
10867b8e00d9SKent Gibson 			if (lv->mask & BIT_ULL(i)) {
10877b8e00d9SKent Gibson 				descs[didx] = lr->lines[i].desc;
10887b8e00d9SKent Gibson 				didx++;
10897b8e00d9SKent Gibson 			}
10907b8e00d9SKent Gibson 		}
10917b8e00d9SKent Gibson 	}
10927b8e00d9SKent Gibson 	ret = gpiod_set_array_value_complex(false, true, num_set,
10937b8e00d9SKent Gibson 					    descs, NULL, vals);
10947b8e00d9SKent Gibson 
10957b8e00d9SKent Gibson 	if (num_set != 1)
10967b8e00d9SKent Gibson 		kfree(descs);
10977b8e00d9SKent Gibson 	return ret;
10987b8e00d9SKent Gibson }
10997b8e00d9SKent Gibson 
11007b8e00d9SKent Gibson static long linereq_set_values(struct linereq *lr, void __user *ip)
11017b8e00d9SKent Gibson {
11027b8e00d9SKent Gibson 	struct gpio_v2_line_values lv;
11037b8e00d9SKent Gibson 	int ret;
11047b8e00d9SKent Gibson 
11057b8e00d9SKent Gibson 	if (copy_from_user(&lv, ip, sizeof(lv)))
11067b8e00d9SKent Gibson 		return -EFAULT;
11077b8e00d9SKent Gibson 
11087b8e00d9SKent Gibson 	mutex_lock(&lr->config_mutex);
11097b8e00d9SKent Gibson 
11107b8e00d9SKent Gibson 	ret = linereq_set_values_unlocked(lr, &lv);
11117b8e00d9SKent Gibson 
11127b8e00d9SKent Gibson 	mutex_unlock(&lr->config_mutex);
11137b8e00d9SKent Gibson 
11147b8e00d9SKent Gibson 	return ret;
11157b8e00d9SKent Gibson }
11167b8e00d9SKent Gibson 
1117a54756cbSKent Gibson static long linereq_set_config_unlocked(struct linereq *lr,
1118a54756cbSKent Gibson 					struct gpio_v2_line_config *lc)
1119a54756cbSKent Gibson {
1120a54756cbSKent Gibson 	struct gpio_desc *desc;
1121a54756cbSKent Gibson 	unsigned int i;
1122a54756cbSKent Gibson 	u64 flags;
1123a54756cbSKent Gibson 	bool polarity_change;
1124a54756cbSKent Gibson 	int ret;
1125a54756cbSKent Gibson 
1126a54756cbSKent Gibson 	for (i = 0; i < lr->num_lines; i++) {
1127a54756cbSKent Gibson 		desc = lr->lines[i].desc;
1128a54756cbSKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
1129a54756cbSKent Gibson 		polarity_change =
1130a54756cbSKent Gibson 			(!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) !=
1131a54756cbSKent Gibson 			 ((flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) != 0));
1132a54756cbSKent Gibson 
1133a54756cbSKent Gibson 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1134a54756cbSKent Gibson 		/*
1135a54756cbSKent Gibson 		 * Lines have to be requested explicitly for input
1136a54756cbSKent Gibson 		 * or output, else the line will be treated "as is".
1137a54756cbSKent Gibson 		 */
1138a54756cbSKent Gibson 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1139a54756cbSKent Gibson 			int val = gpio_v2_line_config_output_value(lc, i);
1140a54756cbSKent Gibson 
1141a54756cbSKent Gibson 			edge_detector_stop(&lr->lines[i]);
1142a54756cbSKent Gibson 			ret = gpiod_direction_output(desc, val);
1143a54756cbSKent Gibson 			if (ret)
1144a54756cbSKent Gibson 				return ret;
1145a54756cbSKent Gibson 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1146a54756cbSKent Gibson 			ret = gpiod_direction_input(desc);
1147a54756cbSKent Gibson 			if (ret)
1148a54756cbSKent Gibson 				return ret;
1149a54756cbSKent Gibson 
115065cff704SKent Gibson 			ret = edge_detector_update(&lr->lines[i], lc, i,
1151a54756cbSKent Gibson 					flags & GPIO_V2_LINE_EDGE_FLAGS,
1152a54756cbSKent Gibson 					polarity_change);
1153a54756cbSKent Gibson 			if (ret)
1154a54756cbSKent Gibson 				return ret;
1155a54756cbSKent Gibson 		}
1156a54756cbSKent Gibson 
1157a54756cbSKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
1158a54756cbSKent Gibson 					     GPIO_V2_LINE_CHANGED_CONFIG,
1159a54756cbSKent Gibson 					     desc);
1160a54756cbSKent Gibson 	}
1161a54756cbSKent Gibson 	return 0;
1162a54756cbSKent Gibson }
1163a54756cbSKent Gibson 
1164a54756cbSKent Gibson static long linereq_set_config(struct linereq *lr, void __user *ip)
1165a54756cbSKent Gibson {
1166a54756cbSKent Gibson 	struct gpio_v2_line_config lc;
1167a54756cbSKent Gibson 	int ret;
1168a54756cbSKent Gibson 
1169a54756cbSKent Gibson 	if (copy_from_user(&lc, ip, sizeof(lc)))
1170a54756cbSKent Gibson 		return -EFAULT;
1171a54756cbSKent Gibson 
1172a54756cbSKent Gibson 	ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
1173a54756cbSKent Gibson 	if (ret)
1174a54756cbSKent Gibson 		return ret;
1175a54756cbSKent Gibson 
1176a54756cbSKent Gibson 	mutex_lock(&lr->config_mutex);
1177a54756cbSKent Gibson 
1178a54756cbSKent Gibson 	ret = linereq_set_config_unlocked(lr, &lc);
1179a54756cbSKent Gibson 
1180a54756cbSKent Gibson 	mutex_unlock(&lr->config_mutex);
1181a54756cbSKent Gibson 
1182a54756cbSKent Gibson 	return ret;
1183a54756cbSKent Gibson }
1184a54756cbSKent Gibson 
11853c0d9c63SKent Gibson static long linereq_ioctl(struct file *file, unsigned int cmd,
11863c0d9c63SKent Gibson 			  unsigned long arg)
11873c0d9c63SKent Gibson {
11883c0d9c63SKent Gibson 	struct linereq *lr = file->private_data;
11893c0d9c63SKent Gibson 	void __user *ip = (void __user *)arg;
11903c0d9c63SKent Gibson 
11913c0d9c63SKent Gibson 	if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL)
11923c0d9c63SKent Gibson 		return linereq_get_values(lr, ip);
11937b8e00d9SKent Gibson 	else if (cmd == GPIO_V2_LINE_SET_VALUES_IOCTL)
11947b8e00d9SKent Gibson 		return linereq_set_values(lr, ip);
1195a54756cbSKent Gibson 	else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL)
1196a54756cbSKent Gibson 		return linereq_set_config(lr, ip);
11973c0d9c63SKent Gibson 
11983c0d9c63SKent Gibson 	return -EINVAL;
11993c0d9c63SKent Gibson }
12003c0d9c63SKent Gibson 
12013c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
12023c0d9c63SKent Gibson static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
12033c0d9c63SKent Gibson 				 unsigned long arg)
12043c0d9c63SKent Gibson {
12053c0d9c63SKent Gibson 	return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
12063c0d9c63SKent Gibson }
12073c0d9c63SKent Gibson #endif
12083c0d9c63SKent Gibson 
120973e03419SKent Gibson static __poll_t linereq_poll(struct file *file,
121073e03419SKent Gibson 			    struct poll_table_struct *wait)
121173e03419SKent Gibson {
121273e03419SKent Gibson 	struct linereq *lr = file->private_data;
121373e03419SKent Gibson 	__poll_t events = 0;
121473e03419SKent Gibson 
121573e03419SKent Gibson 	poll_wait(file, &lr->wait, wait);
121673e03419SKent Gibson 
121773e03419SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
121873e03419SKent Gibson 						 &lr->wait.lock))
121973e03419SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
122073e03419SKent Gibson 
122173e03419SKent Gibson 	return events;
122273e03419SKent Gibson }
122373e03419SKent Gibson 
122473e03419SKent Gibson static ssize_t linereq_read(struct file *file,
122573e03419SKent Gibson 			    char __user *buf,
122673e03419SKent Gibson 			    size_t count,
122773e03419SKent Gibson 			    loff_t *f_ps)
122873e03419SKent Gibson {
122973e03419SKent Gibson 	struct linereq *lr = file->private_data;
123073e03419SKent Gibson 	struct gpio_v2_line_event le;
123173e03419SKent Gibson 	ssize_t bytes_read = 0;
123273e03419SKent Gibson 	int ret;
123373e03419SKent Gibson 
123473e03419SKent Gibson 	if (count < sizeof(le))
123573e03419SKent Gibson 		return -EINVAL;
123673e03419SKent Gibson 
123773e03419SKent Gibson 	do {
123873e03419SKent Gibson 		spin_lock(&lr->wait.lock);
123973e03419SKent Gibson 		if (kfifo_is_empty(&lr->events)) {
124073e03419SKent Gibson 			if (bytes_read) {
124173e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
124273e03419SKent Gibson 				return bytes_read;
124373e03419SKent Gibson 			}
124473e03419SKent Gibson 
124573e03419SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
124673e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
124773e03419SKent Gibson 				return -EAGAIN;
124873e03419SKent Gibson 			}
124973e03419SKent Gibson 
125073e03419SKent Gibson 			ret = wait_event_interruptible_locked(lr->wait,
125173e03419SKent Gibson 					!kfifo_is_empty(&lr->events));
125273e03419SKent Gibson 			if (ret) {
125373e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
125473e03419SKent Gibson 				return ret;
125573e03419SKent Gibson 			}
125673e03419SKent Gibson 		}
125773e03419SKent Gibson 
125873e03419SKent Gibson 		ret = kfifo_out(&lr->events, &le, 1);
125973e03419SKent Gibson 		spin_unlock(&lr->wait.lock);
126073e03419SKent Gibson 		if (ret != 1) {
126173e03419SKent Gibson 			/*
126273e03419SKent Gibson 			 * This should never happen - we were holding the
126373e03419SKent Gibson 			 * lock from the moment we learned the fifo is no
126473e03419SKent Gibson 			 * longer empty until now.
126573e03419SKent Gibson 			 */
126673e03419SKent Gibson 			ret = -EIO;
126773e03419SKent Gibson 			break;
126873e03419SKent Gibson 		}
126973e03419SKent Gibson 
127073e03419SKent Gibson 		if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
127173e03419SKent Gibson 			return -EFAULT;
127273e03419SKent Gibson 		bytes_read += sizeof(le);
127373e03419SKent Gibson 	} while (count >= bytes_read + sizeof(le));
127473e03419SKent Gibson 
127573e03419SKent Gibson 	return bytes_read;
127673e03419SKent Gibson }
127773e03419SKent Gibson 
12783c0d9c63SKent Gibson static void linereq_free(struct linereq *lr)
12793c0d9c63SKent Gibson {
12803c0d9c63SKent Gibson 	unsigned int i;
12813c0d9c63SKent Gibson 
12823c0d9c63SKent Gibson 	for (i = 0; i < lr->num_lines; i++) {
128373e03419SKent Gibson 		edge_detector_stop(&lr->lines[i]);
12843c0d9c63SKent Gibson 		if (lr->lines[i].desc)
12853c0d9c63SKent Gibson 			gpiod_free(lr->lines[i].desc);
12863c0d9c63SKent Gibson 	}
128773e03419SKent Gibson 	kfifo_free(&lr->events);
12883c0d9c63SKent Gibson 	kfree(lr->label);
12893c0d9c63SKent Gibson 	put_device(&lr->gdev->dev);
12903c0d9c63SKent Gibson 	kfree(lr);
12913c0d9c63SKent Gibson }
12923c0d9c63SKent Gibson 
12933c0d9c63SKent Gibson static int linereq_release(struct inode *inode, struct file *file)
12943c0d9c63SKent Gibson {
12953c0d9c63SKent Gibson 	struct linereq *lr = file->private_data;
12963c0d9c63SKent Gibson 
12973c0d9c63SKent Gibson 	linereq_free(lr);
12983c0d9c63SKent Gibson 	return 0;
12993c0d9c63SKent Gibson }
13003c0d9c63SKent Gibson 
13013c0d9c63SKent Gibson static const struct file_operations line_fileops = {
13023c0d9c63SKent Gibson 	.release = linereq_release,
130373e03419SKent Gibson 	.read = linereq_read,
130473e03419SKent Gibson 	.poll = linereq_poll,
13053c0d9c63SKent Gibson 	.owner = THIS_MODULE,
13063c0d9c63SKent Gibson 	.llseek = noop_llseek,
13073c0d9c63SKent Gibson 	.unlocked_ioctl = linereq_ioctl,
13083c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
13093c0d9c63SKent Gibson 	.compat_ioctl = linereq_ioctl_compat,
13103c0d9c63SKent Gibson #endif
13113c0d9c63SKent Gibson };
13123c0d9c63SKent Gibson 
13133c0d9c63SKent Gibson static int linereq_create(struct gpio_device *gdev, void __user *ip)
13143c0d9c63SKent Gibson {
13153c0d9c63SKent Gibson 	struct gpio_v2_line_request ulr;
13163c0d9c63SKent Gibson 	struct gpio_v2_line_config *lc;
13173c0d9c63SKent Gibson 	struct linereq *lr;
13183c0d9c63SKent Gibson 	struct file *file;
13193c0d9c63SKent Gibson 	u64 flags;
13203c0d9c63SKent Gibson 	unsigned int i;
13213c0d9c63SKent Gibson 	int fd, ret;
13223c0d9c63SKent Gibson 
13233c0d9c63SKent Gibson 	if (copy_from_user(&ulr, ip, sizeof(ulr)))
13243c0d9c63SKent Gibson 		return -EFAULT;
13253c0d9c63SKent Gibson 
13263c0d9c63SKent Gibson 	if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
13273c0d9c63SKent Gibson 		return -EINVAL;
13283c0d9c63SKent Gibson 
13293c0d9c63SKent Gibson 	if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
13303c0d9c63SKent Gibson 		return -EINVAL;
13313c0d9c63SKent Gibson 
13323c0d9c63SKent Gibson 	lc = &ulr.config;
13333c0d9c63SKent Gibson 	ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
13343c0d9c63SKent Gibson 	if (ret)
13353c0d9c63SKent Gibson 		return ret;
13363c0d9c63SKent Gibson 
13373c0d9c63SKent Gibson 	lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
13383c0d9c63SKent Gibson 	if (!lr)
13393c0d9c63SKent Gibson 		return -ENOMEM;
13403c0d9c63SKent Gibson 
13413c0d9c63SKent Gibson 	lr->gdev = gdev;
13423c0d9c63SKent Gibson 	get_device(&gdev->dev);
13433c0d9c63SKent Gibson 
134465cff704SKent Gibson 	for (i = 0; i < ulr.num_lines; i++) {
134573e03419SKent Gibson 		lr->lines[i].req = lr;
134665cff704SKent Gibson 		WRITE_ONCE(lr->lines[i].sw_debounced, 0);
134765cff704SKent Gibson 		INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
134865cff704SKent Gibson 	}
134973e03419SKent Gibson 
1350f188ac12SKent Gibson 	if (ulr.consumer[0] != '\0') {
13513c0d9c63SKent Gibson 		/* label is only initialized if consumer is set */
1352f188ac12SKent Gibson 		lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1353f188ac12SKent Gibson 				     GFP_KERNEL);
13543c0d9c63SKent Gibson 		if (!lr->label) {
13553c0d9c63SKent Gibson 			ret = -ENOMEM;
13563c0d9c63SKent Gibson 			goto out_free_linereq;
13573c0d9c63SKent Gibson 		}
13583c0d9c63SKent Gibson 	}
13593c0d9c63SKent Gibson 
1360a54756cbSKent Gibson 	mutex_init(&lr->config_mutex);
136173e03419SKent Gibson 	init_waitqueue_head(&lr->wait);
136273e03419SKent Gibson 	lr->event_buffer_size = ulr.event_buffer_size;
136373e03419SKent Gibson 	if (lr->event_buffer_size == 0)
136473e03419SKent Gibson 		lr->event_buffer_size = ulr.num_lines * 16;
136573e03419SKent Gibson 	else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
136673e03419SKent Gibson 		lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
136773e03419SKent Gibson 
136873e03419SKent Gibson 	atomic_set(&lr->seqno, 0);
13693c0d9c63SKent Gibson 	lr->num_lines = ulr.num_lines;
13703c0d9c63SKent Gibson 
13713c0d9c63SKent Gibson 	/* Request each GPIO */
13723c0d9c63SKent Gibson 	for (i = 0; i < ulr.num_lines; i++) {
13733c0d9c63SKent Gibson 		u32 offset = ulr.offsets[i];
13743c0d9c63SKent Gibson 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
13753c0d9c63SKent Gibson 
13763c0d9c63SKent Gibson 		if (IS_ERR(desc)) {
13773c0d9c63SKent Gibson 			ret = PTR_ERR(desc);
13783c0d9c63SKent Gibson 			goto out_free_linereq;
13793c0d9c63SKent Gibson 		}
13803c0d9c63SKent Gibson 
13813c0d9c63SKent Gibson 		ret = gpiod_request(desc, lr->label);
13823c0d9c63SKent Gibson 		if (ret)
13833c0d9c63SKent Gibson 			goto out_free_linereq;
13843c0d9c63SKent Gibson 
13853c0d9c63SKent Gibson 		lr->lines[i].desc = desc;
13863c0d9c63SKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
13873c0d9c63SKent Gibson 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
13883c0d9c63SKent Gibson 
13893c0d9c63SKent Gibson 		ret = gpiod_set_transitory(desc, false);
13903c0d9c63SKent Gibson 		if (ret < 0)
13913c0d9c63SKent Gibson 			goto out_free_linereq;
13923c0d9c63SKent Gibson 
13933c0d9c63SKent Gibson 		/*
13943c0d9c63SKent Gibson 		 * Lines have to be requested explicitly for input
13953c0d9c63SKent Gibson 		 * or output, else the line will be treated "as is".
13963c0d9c63SKent Gibson 		 */
13973c0d9c63SKent Gibson 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
13983c0d9c63SKent Gibson 			int val = gpio_v2_line_config_output_value(lc, i);
13993c0d9c63SKent Gibson 
14003c0d9c63SKent Gibson 			ret = gpiod_direction_output(desc, val);
14013c0d9c63SKent Gibson 			if (ret)
14023c0d9c63SKent Gibson 				goto out_free_linereq;
14033c0d9c63SKent Gibson 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
14043c0d9c63SKent Gibson 			ret = gpiod_direction_input(desc);
14053c0d9c63SKent Gibson 			if (ret)
14063c0d9c63SKent Gibson 				goto out_free_linereq;
140773e03419SKent Gibson 
140865cff704SKent Gibson 			ret = edge_detector_setup(&lr->lines[i], lc, i,
140973e03419SKent Gibson 					flags & GPIO_V2_LINE_EDGE_FLAGS);
141073e03419SKent Gibson 			if (ret)
141173e03419SKent Gibson 				goto out_free_linereq;
14123c0d9c63SKent Gibson 		}
14133c0d9c63SKent Gibson 
14143c0d9c63SKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
1415aad95584SKent Gibson 					     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
14163c0d9c63SKent Gibson 
14173c0d9c63SKent Gibson 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
14183c0d9c63SKent Gibson 			offset);
14193c0d9c63SKent Gibson 	}
14203c0d9c63SKent Gibson 
14213c0d9c63SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
14223c0d9c63SKent Gibson 	if (fd < 0) {
14233c0d9c63SKent Gibson 		ret = fd;
14243c0d9c63SKent Gibson 		goto out_free_linereq;
14253c0d9c63SKent Gibson 	}
14263c0d9c63SKent Gibson 
14273c0d9c63SKent Gibson 	file = anon_inode_getfile("gpio-line", &line_fileops, lr,
14283c0d9c63SKent Gibson 				  O_RDONLY | O_CLOEXEC);
14293c0d9c63SKent Gibson 	if (IS_ERR(file)) {
14303c0d9c63SKent Gibson 		ret = PTR_ERR(file);
14313c0d9c63SKent Gibson 		goto out_put_unused_fd;
14323c0d9c63SKent Gibson 	}
14333c0d9c63SKent Gibson 
14343c0d9c63SKent Gibson 	ulr.fd = fd;
14353c0d9c63SKent Gibson 	if (copy_to_user(ip, &ulr, sizeof(ulr))) {
14363c0d9c63SKent Gibson 		/*
14373c0d9c63SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
14383c0d9c63SKent Gibson 		 * the regular error cleanup path here.
14393c0d9c63SKent Gibson 		 */
14403c0d9c63SKent Gibson 		fput(file);
14413c0d9c63SKent Gibson 		put_unused_fd(fd);
14423c0d9c63SKent Gibson 		return -EFAULT;
14433c0d9c63SKent Gibson 	}
14443c0d9c63SKent Gibson 
14453c0d9c63SKent Gibson 	fd_install(fd, file);
14463c0d9c63SKent Gibson 
14473c0d9c63SKent Gibson 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
14483c0d9c63SKent Gibson 		lr->num_lines);
14493c0d9c63SKent Gibson 
14503c0d9c63SKent Gibson 	return 0;
14513c0d9c63SKent Gibson 
14523c0d9c63SKent Gibson out_put_unused_fd:
14533c0d9c63SKent Gibson 	put_unused_fd(fd);
14543c0d9c63SKent Gibson out_free_linereq:
14553c0d9c63SKent Gibson 	linereq_free(lr);
14563c0d9c63SKent Gibson 	return ret;
14573c0d9c63SKent Gibson }
14583c0d9c63SKent Gibson 
14593c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1460925ca369SKent Gibson 
1461925ca369SKent Gibson /*
1462925ca369SKent Gibson  * GPIO line event management
1463925ca369SKent Gibson  */
1464925ca369SKent Gibson 
1465925ca369SKent Gibson /**
1466925ca369SKent Gibson  * struct lineevent_state - contains the state of a userspace event
1467925ca369SKent Gibson  * @gdev: the GPIO device the event pertains to
1468925ca369SKent Gibson  * @label: consumer label used to tag descriptors
1469925ca369SKent Gibson  * @desc: the GPIO descriptor held by this event
1470925ca369SKent Gibson  * @eflags: the event flags this line was requested with
1471925ca369SKent Gibson  * @irq: the interrupt that trigger in response to events on this GPIO
1472925ca369SKent Gibson  * @wait: wait queue that handles blocking reads of events
1473925ca369SKent Gibson  * @events: KFIFO for the GPIO events
1474925ca369SKent Gibson  * @timestamp: cache for the timestamp storing it between hardirq
1475925ca369SKent Gibson  * and IRQ thread, used to bring the timestamp close to the actual
1476925ca369SKent Gibson  * event
1477925ca369SKent Gibson  */
1478925ca369SKent Gibson struct lineevent_state {
1479925ca369SKent Gibson 	struct gpio_device *gdev;
1480925ca369SKent Gibson 	const char *label;
1481925ca369SKent Gibson 	struct gpio_desc *desc;
1482925ca369SKent Gibson 	u32 eflags;
1483925ca369SKent Gibson 	int irq;
1484925ca369SKent Gibson 	wait_queue_head_t wait;
1485925ca369SKent Gibson 	DECLARE_KFIFO(events, struct gpioevent_data, 16);
1486925ca369SKent Gibson 	u64 timestamp;
1487925ca369SKent Gibson };
1488925ca369SKent Gibson 
1489925ca369SKent Gibson #define GPIOEVENT_REQUEST_VALID_FLAGS \
1490925ca369SKent Gibson 	(GPIOEVENT_REQUEST_RISING_EDGE | \
1491925ca369SKent Gibson 	GPIOEVENT_REQUEST_FALLING_EDGE)
1492925ca369SKent Gibson 
149349bc5279SKent Gibson static __poll_t lineevent_poll(struct file *file,
1494925ca369SKent Gibson 			       struct poll_table_struct *wait)
1495925ca369SKent Gibson {
149649bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1497925ca369SKent Gibson 	__poll_t events = 0;
1498925ca369SKent Gibson 
149949bc5279SKent Gibson 	poll_wait(file, &le->wait, wait);
1500925ca369SKent Gibson 
1501925ca369SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1502925ca369SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
1503925ca369SKent Gibson 
1504925ca369SKent Gibson 	return events;
1505925ca369SKent Gibson }
1506925ca369SKent Gibson 
15075ad284abSAndy Shevchenko struct compat_gpioeevent_data {
15085ad284abSAndy Shevchenko 	compat_u64	timestamp;
15095ad284abSAndy Shevchenko 	u32		id;
15105ad284abSAndy Shevchenko };
15115ad284abSAndy Shevchenko 
151249bc5279SKent Gibson static ssize_t lineevent_read(struct file *file,
1513925ca369SKent Gibson 			      char __user *buf,
1514925ca369SKent Gibson 			      size_t count,
1515925ca369SKent Gibson 			      loff_t *f_ps)
1516925ca369SKent Gibson {
151749bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1518925ca369SKent Gibson 	struct gpioevent_data ge;
1519925ca369SKent Gibson 	ssize_t bytes_read = 0;
15205ad284abSAndy Shevchenko 	ssize_t ge_size;
1521925ca369SKent Gibson 	int ret;
1522925ca369SKent Gibson 
15235ad284abSAndy Shevchenko 	/*
15245ad284abSAndy Shevchenko 	 * When compatible system call is being used the struct gpioevent_data,
15255ad284abSAndy Shevchenko 	 * in case of at least ia32, has different size due to the alignment
15265ad284abSAndy Shevchenko 	 * differences. Because we have first member 64 bits followed by one of
15275ad284abSAndy Shevchenko 	 * 32 bits there is no gap between them. The only difference is the
15285ad284abSAndy Shevchenko 	 * padding at the end of the data structure. Hence, we calculate the
15295ad284abSAndy Shevchenko 	 * actual sizeof() and pass this as an argument to copy_to_user() to
15305ad284abSAndy Shevchenko 	 * drop unneeded bytes from the output.
15315ad284abSAndy Shevchenko 	 */
1532163d1719SAndy Shevchenko 	if (compat_need_64bit_alignment_fixup())
1533163d1719SAndy Shevchenko 		ge_size = sizeof(struct compat_gpioeevent_data);
1534163d1719SAndy Shevchenko 	else
1535163d1719SAndy Shevchenko 		ge_size = sizeof(struct gpioevent_data);
15365ad284abSAndy Shevchenko 	if (count < ge_size)
1537925ca369SKent Gibson 		return -EINVAL;
1538925ca369SKent Gibson 
1539925ca369SKent Gibson 	do {
1540925ca369SKent Gibson 		spin_lock(&le->wait.lock);
1541925ca369SKent Gibson 		if (kfifo_is_empty(&le->events)) {
1542925ca369SKent Gibson 			if (bytes_read) {
1543925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1544925ca369SKent Gibson 				return bytes_read;
1545925ca369SKent Gibson 			}
1546925ca369SKent Gibson 
154749bc5279SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
1548925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1549925ca369SKent Gibson 				return -EAGAIN;
1550925ca369SKent Gibson 			}
1551925ca369SKent Gibson 
1552925ca369SKent Gibson 			ret = wait_event_interruptible_locked(le->wait,
1553925ca369SKent Gibson 					!kfifo_is_empty(&le->events));
1554925ca369SKent Gibson 			if (ret) {
1555925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1556925ca369SKent Gibson 				return ret;
1557925ca369SKent Gibson 			}
1558925ca369SKent Gibson 		}
1559925ca369SKent Gibson 
1560925ca369SKent Gibson 		ret = kfifo_out(&le->events, &ge, 1);
1561925ca369SKent Gibson 		spin_unlock(&le->wait.lock);
1562925ca369SKent Gibson 		if (ret != 1) {
1563925ca369SKent Gibson 			/*
1564925ca369SKent Gibson 			 * This should never happen - we were holding the lock
1565925ca369SKent Gibson 			 * from the moment we learned the fifo is no longer
1566925ca369SKent Gibson 			 * empty until now.
1567925ca369SKent Gibson 			 */
1568925ca369SKent Gibson 			ret = -EIO;
1569925ca369SKent Gibson 			break;
1570925ca369SKent Gibson 		}
1571925ca369SKent Gibson 
15725ad284abSAndy Shevchenko 		if (copy_to_user(buf + bytes_read, &ge, ge_size))
1573925ca369SKent Gibson 			return -EFAULT;
15745ad284abSAndy Shevchenko 		bytes_read += ge_size;
15755ad284abSAndy Shevchenko 	} while (count >= bytes_read + ge_size);
1576925ca369SKent Gibson 
1577925ca369SKent Gibson 	return bytes_read;
1578925ca369SKent Gibson }
1579925ca369SKent Gibson 
158046824272SKent Gibson static void lineevent_free(struct lineevent_state *le)
1581925ca369SKent Gibson {
158246824272SKent Gibson 	if (le->irq)
1583925ca369SKent Gibson 		free_irq(le->irq, le);
158446824272SKent Gibson 	if (le->desc)
1585925ca369SKent Gibson 		gpiod_free(le->desc);
1586925ca369SKent Gibson 	kfree(le->label);
158746824272SKent Gibson 	put_device(&le->gdev->dev);
1588925ca369SKent Gibson 	kfree(le);
158946824272SKent Gibson }
159046824272SKent Gibson 
159146824272SKent Gibson static int lineevent_release(struct inode *inode, struct file *file)
159246824272SKent Gibson {
159346824272SKent Gibson 	lineevent_free(file->private_data);
1594925ca369SKent Gibson 	return 0;
1595925ca369SKent Gibson }
1596925ca369SKent Gibson 
159749bc5279SKent Gibson static long lineevent_ioctl(struct file *file, unsigned int cmd,
1598925ca369SKent Gibson 			    unsigned long arg)
1599925ca369SKent Gibson {
160049bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1601925ca369SKent Gibson 	void __user *ip = (void __user *)arg;
1602925ca369SKent Gibson 	struct gpiohandle_data ghd;
1603925ca369SKent Gibson 
1604925ca369SKent Gibson 	/*
1605925ca369SKent Gibson 	 * We can get the value for an event line but not set it,
1606925ca369SKent Gibson 	 * because it is input by definition.
1607925ca369SKent Gibson 	 */
1608925ca369SKent Gibson 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
1609925ca369SKent Gibson 		int val;
1610925ca369SKent Gibson 
1611925ca369SKent Gibson 		memset(&ghd, 0, sizeof(ghd));
1612925ca369SKent Gibson 
1613925ca369SKent Gibson 		val = gpiod_get_value_cansleep(le->desc);
1614925ca369SKent Gibson 		if (val < 0)
1615925ca369SKent Gibson 			return val;
1616925ca369SKent Gibson 		ghd.values[0] = val;
1617925ca369SKent Gibson 
1618925ca369SKent Gibson 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
1619925ca369SKent Gibson 			return -EFAULT;
1620925ca369SKent Gibson 
1621925ca369SKent Gibson 		return 0;
1622925ca369SKent Gibson 	}
1623925ca369SKent Gibson 	return -EINVAL;
1624925ca369SKent Gibson }
1625925ca369SKent Gibson 
1626925ca369SKent Gibson #ifdef CONFIG_COMPAT
162749bc5279SKent Gibson static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
1628925ca369SKent Gibson 				   unsigned long arg)
1629925ca369SKent Gibson {
163049bc5279SKent Gibson 	return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1631925ca369SKent Gibson }
1632925ca369SKent Gibson #endif
1633925ca369SKent Gibson 
1634925ca369SKent Gibson static const struct file_operations lineevent_fileops = {
1635925ca369SKent Gibson 	.release = lineevent_release,
1636925ca369SKent Gibson 	.read = lineevent_read,
1637925ca369SKent Gibson 	.poll = lineevent_poll,
1638925ca369SKent Gibson 	.owner = THIS_MODULE,
1639925ca369SKent Gibson 	.llseek = noop_llseek,
1640925ca369SKent Gibson 	.unlocked_ioctl = lineevent_ioctl,
1641925ca369SKent Gibson #ifdef CONFIG_COMPAT
1642925ca369SKent Gibson 	.compat_ioctl = lineevent_ioctl_compat,
1643925ca369SKent Gibson #endif
1644925ca369SKent Gibson };
1645925ca369SKent Gibson 
1646925ca369SKent Gibson static irqreturn_t lineevent_irq_thread(int irq, void *p)
1647925ca369SKent Gibson {
1648925ca369SKent Gibson 	struct lineevent_state *le = p;
1649925ca369SKent Gibson 	struct gpioevent_data ge;
1650925ca369SKent Gibson 	int ret;
1651925ca369SKent Gibson 
1652925ca369SKent Gibson 	/* Do not leak kernel stack to userspace */
1653925ca369SKent Gibson 	memset(&ge, 0, sizeof(ge));
1654925ca369SKent Gibson 
1655925ca369SKent Gibson 	/*
1656925ca369SKent Gibson 	 * We may be running from a nested threaded interrupt in which case
1657925ca369SKent Gibson 	 * we didn't get the timestamp from lineevent_irq_handler().
1658925ca369SKent Gibson 	 */
1659925ca369SKent Gibson 	if (!le->timestamp)
1660925ca369SKent Gibson 		ge.timestamp = ktime_get_ns();
1661925ca369SKent Gibson 	else
1662925ca369SKent Gibson 		ge.timestamp = le->timestamp;
1663925ca369SKent Gibson 
1664925ca369SKent Gibson 	if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
1665925ca369SKent Gibson 	    && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1666925ca369SKent Gibson 		int level = gpiod_get_value_cansleep(le->desc);
1667925ca369SKent Gibson 
1668925ca369SKent Gibson 		if (level)
1669925ca369SKent Gibson 			/* Emit low-to-high event */
1670925ca369SKent Gibson 			ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1671925ca369SKent Gibson 		else
1672925ca369SKent Gibson 			/* Emit high-to-low event */
1673925ca369SKent Gibson 			ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1674925ca369SKent Gibson 	} else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
1675925ca369SKent Gibson 		/* Emit low-to-high event */
1676925ca369SKent Gibson 		ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1677925ca369SKent Gibson 	} else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1678925ca369SKent Gibson 		/* Emit high-to-low event */
1679925ca369SKent Gibson 		ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1680925ca369SKent Gibson 	} else {
1681925ca369SKent Gibson 		return IRQ_NONE;
1682925ca369SKent Gibson 	}
1683925ca369SKent Gibson 
1684925ca369SKent Gibson 	ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
1685925ca369SKent Gibson 					    1, &le->wait.lock);
1686925ca369SKent Gibson 	if (ret)
1687925ca369SKent Gibson 		wake_up_poll(&le->wait, EPOLLIN);
1688925ca369SKent Gibson 	else
1689925ca369SKent Gibson 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
1690925ca369SKent Gibson 
1691925ca369SKent Gibson 	return IRQ_HANDLED;
1692925ca369SKent Gibson }
1693925ca369SKent Gibson 
1694925ca369SKent Gibson static irqreturn_t lineevent_irq_handler(int irq, void *p)
1695925ca369SKent Gibson {
1696925ca369SKent Gibson 	struct lineevent_state *le = p;
1697925ca369SKent Gibson 
1698925ca369SKent Gibson 	/*
1699925ca369SKent Gibson 	 * Just store the timestamp in hardirq context so we get it as
1700925ca369SKent Gibson 	 * close in time as possible to the actual event.
1701925ca369SKent Gibson 	 */
1702925ca369SKent Gibson 	le->timestamp = ktime_get_ns();
1703925ca369SKent Gibson 
1704925ca369SKent Gibson 	return IRQ_WAKE_THREAD;
1705925ca369SKent Gibson }
1706925ca369SKent Gibson 
1707925ca369SKent Gibson static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1708925ca369SKent Gibson {
1709925ca369SKent Gibson 	struct gpioevent_request eventreq;
1710925ca369SKent Gibson 	struct lineevent_state *le;
1711925ca369SKent Gibson 	struct gpio_desc *desc;
1712925ca369SKent Gibson 	struct file *file;
1713925ca369SKent Gibson 	u32 offset;
1714925ca369SKent Gibson 	u32 lflags;
1715925ca369SKent Gibson 	u32 eflags;
1716925ca369SKent Gibson 	int fd;
1717925ca369SKent Gibson 	int ret;
171846824272SKent Gibson 	int irq, irqflags = 0;
1719925ca369SKent Gibson 
1720925ca369SKent Gibson 	if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1721925ca369SKent Gibson 		return -EFAULT;
1722925ca369SKent Gibson 
1723925ca369SKent Gibson 	offset = eventreq.lineoffset;
1724925ca369SKent Gibson 	lflags = eventreq.handleflags;
1725925ca369SKent Gibson 	eflags = eventreq.eventflags;
1726925ca369SKent Gibson 
1727925ca369SKent Gibson 	desc = gpiochip_get_desc(gdev->chip, offset);
1728925ca369SKent Gibson 	if (IS_ERR(desc))
1729925ca369SKent Gibson 		return PTR_ERR(desc);
1730925ca369SKent Gibson 
1731925ca369SKent Gibson 	/* Return an error if a unknown flag is set */
1732925ca369SKent Gibson 	if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1733925ca369SKent Gibson 	    (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1734925ca369SKent Gibson 		return -EINVAL;
1735925ca369SKent Gibson 
1736925ca369SKent Gibson 	/* This is just wrong: we don't look for events on output lines */
1737925ca369SKent Gibson 	if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1738925ca369SKent Gibson 	    (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1739925ca369SKent Gibson 	    (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1740925ca369SKent Gibson 		return -EINVAL;
1741925ca369SKent Gibson 
1742925ca369SKent Gibson 	/* Only one bias flag can be set. */
1743925ca369SKent Gibson 	if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1744925ca369SKent Gibson 	     (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1745925ca369SKent Gibson 			GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1746925ca369SKent Gibson 	    ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1747925ca369SKent Gibson 	     (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1748925ca369SKent Gibson 		return -EINVAL;
1749925ca369SKent Gibson 
1750925ca369SKent Gibson 	le = kzalloc(sizeof(*le), GFP_KERNEL);
1751925ca369SKent Gibson 	if (!le)
1752925ca369SKent Gibson 		return -ENOMEM;
1753925ca369SKent Gibson 	le->gdev = gdev;
1754925ca369SKent Gibson 	get_device(&gdev->dev);
1755925ca369SKent Gibson 
1756f188ac12SKent Gibson 	if (eventreq.consumer_label[0] != '\0') {
1757f188ac12SKent Gibson 		/* label is only initialized if consumer_label is set */
1758f188ac12SKent Gibson 		le->label = kstrndup(eventreq.consumer_label,
1759f188ac12SKent Gibson 				     sizeof(eventreq.consumer_label) - 1,
1760925ca369SKent Gibson 				     GFP_KERNEL);
1761925ca369SKent Gibson 		if (!le->label) {
1762925ca369SKent Gibson 			ret = -ENOMEM;
1763925ca369SKent Gibson 			goto out_free_le;
1764925ca369SKent Gibson 		}
1765925ca369SKent Gibson 	}
1766925ca369SKent Gibson 
1767925ca369SKent Gibson 	ret = gpiod_request(desc, le->label);
1768925ca369SKent Gibson 	if (ret)
176946824272SKent Gibson 		goto out_free_le;
1770925ca369SKent Gibson 	le->desc = desc;
1771925ca369SKent Gibson 	le->eflags = eflags;
1772925ca369SKent Gibson 
1773c274b58aSKent Gibson 	linehandle_flags_to_desc_flags(lflags, &desc->flags);
1774925ca369SKent Gibson 
1775925ca369SKent Gibson 	ret = gpiod_direction_input(desc);
1776925ca369SKent Gibson 	if (ret)
177746824272SKent Gibson 		goto out_free_le;
1778925ca369SKent Gibson 
17796accc376SKent Gibson 	blocking_notifier_call_chain(&desc->gdev->notifier,
1780aad95584SKent Gibson 				     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1781925ca369SKent Gibson 
178246824272SKent Gibson 	irq = gpiod_to_irq(desc);
178346824272SKent Gibson 	if (irq <= 0) {
1784925ca369SKent Gibson 		ret = -ENODEV;
178546824272SKent Gibson 		goto out_free_le;
1786925ca369SKent Gibson 	}
178746824272SKent Gibson 	le->irq = irq;
1788925ca369SKent Gibson 
1789925ca369SKent Gibson 	if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
1790925ca369SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1791925ca369SKent Gibson 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1792925ca369SKent Gibson 	if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
1793925ca369SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1794925ca369SKent Gibson 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1795925ca369SKent Gibson 	irqflags |= IRQF_ONESHOT;
1796925ca369SKent Gibson 
1797925ca369SKent Gibson 	INIT_KFIFO(le->events);
1798925ca369SKent Gibson 	init_waitqueue_head(&le->wait);
1799925ca369SKent Gibson 
1800925ca369SKent Gibson 	/* Request a thread to read the events */
1801925ca369SKent Gibson 	ret = request_threaded_irq(le->irq,
1802925ca369SKent Gibson 				   lineevent_irq_handler,
1803925ca369SKent Gibson 				   lineevent_irq_thread,
1804925ca369SKent Gibson 				   irqflags,
1805925ca369SKent Gibson 				   le->label,
1806925ca369SKent Gibson 				   le);
1807925ca369SKent Gibson 	if (ret)
180846824272SKent Gibson 		goto out_free_le;
1809925ca369SKent Gibson 
1810925ca369SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1811925ca369SKent Gibson 	if (fd < 0) {
1812925ca369SKent Gibson 		ret = fd;
181346824272SKent Gibson 		goto out_free_le;
1814925ca369SKent Gibson 	}
1815925ca369SKent Gibson 
1816925ca369SKent Gibson 	file = anon_inode_getfile("gpio-event",
1817925ca369SKent Gibson 				  &lineevent_fileops,
1818925ca369SKent Gibson 				  le,
1819925ca369SKent Gibson 				  O_RDONLY | O_CLOEXEC);
1820925ca369SKent Gibson 	if (IS_ERR(file)) {
1821925ca369SKent Gibson 		ret = PTR_ERR(file);
1822925ca369SKent Gibson 		goto out_put_unused_fd;
1823925ca369SKent Gibson 	}
1824925ca369SKent Gibson 
1825925ca369SKent Gibson 	eventreq.fd = fd;
1826925ca369SKent Gibson 	if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
1827925ca369SKent Gibson 		/*
1828925ca369SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
1829925ca369SKent Gibson 		 * the regular error cleanup path here.
1830925ca369SKent Gibson 		 */
1831925ca369SKent Gibson 		fput(file);
1832925ca369SKent Gibson 		put_unused_fd(fd);
1833925ca369SKent Gibson 		return -EFAULT;
1834925ca369SKent Gibson 	}
1835925ca369SKent Gibson 
1836925ca369SKent Gibson 	fd_install(fd, file);
1837925ca369SKent Gibson 
1838925ca369SKent Gibson 	return 0;
1839925ca369SKent Gibson 
1840925ca369SKent Gibson out_put_unused_fd:
1841925ca369SKent Gibson 	put_unused_fd(fd);
1842925ca369SKent Gibson out_free_le:
184346824272SKent Gibson 	lineevent_free(le);
1844925ca369SKent Gibson 	return ret;
1845925ca369SKent Gibson }
1846925ca369SKent Gibson 
1847aad95584SKent Gibson static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
1848aad95584SKent Gibson 				    struct gpioline_info *info_v1)
1849aad95584SKent Gibson {
1850aad95584SKent Gibson 	u64 flagsv2 = info_v2->flags;
1851aad95584SKent Gibson 
1852aad95584SKent Gibson 	memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
1853aad95584SKent Gibson 	memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
1854aad95584SKent Gibson 	info_v1->line_offset = info_v2->offset;
1855aad95584SKent Gibson 	info_v1->flags = 0;
1856aad95584SKent Gibson 
1857aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
1858aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_KERNEL;
1859aad95584SKent Gibson 
1860aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
1861aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
1862aad95584SKent Gibson 
1863aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
1864aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
1865aad95584SKent Gibson 
1866aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
1867aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
1868aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
1869aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
1870aad95584SKent Gibson 
1871aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
1872aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
1873aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
1874aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
1875aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
1876aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
1877aad95584SKent Gibson }
1878aad95584SKent Gibson 
1879aad95584SKent Gibson static void gpio_v2_line_info_changed_to_v1(
1880aad95584SKent Gibson 		struct gpio_v2_line_info_changed *lic_v2,
1881aad95584SKent Gibson 		struct gpioline_info_changed *lic_v1)
1882aad95584SKent Gibson {
1883*cb8f63b8SGabriel Knezek 	memset(lic_v1, 0, sizeof(*lic_v1));
1884aad95584SKent Gibson 	gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
1885aad95584SKent Gibson 	lic_v1->timestamp = lic_v2->timestamp_ns;
1886aad95584SKent Gibson 	lic_v1->event_type = lic_v2->event_type;
1887aad95584SKent Gibson }
1888aad95584SKent Gibson 
18893c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
18903c0d9c63SKent Gibson 
1891925ca369SKent Gibson static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
1892aad95584SKent Gibson 				  struct gpio_v2_line_info *info)
1893925ca369SKent Gibson {
1894925ca369SKent Gibson 	struct gpio_chip *gc = desc->gdev->chip;
1895925ca369SKent Gibson 	bool ok_for_pinctrl;
1896925ca369SKent Gibson 	unsigned long flags;
189765cff704SKent Gibson 	u32 debounce_period_us;
189865cff704SKent Gibson 	unsigned int num_attrs = 0;
1899925ca369SKent Gibson 
190069e4e136SKent Gibson 	memset(info, 0, sizeof(*info));
1901aad95584SKent Gibson 	info->offset = gpio_chip_hwgpio(desc);
1902925ca369SKent Gibson 
1903925ca369SKent Gibson 	/*
1904925ca369SKent Gibson 	 * This function takes a mutex so we must check this before taking
1905925ca369SKent Gibson 	 * the spinlock.
1906925ca369SKent Gibson 	 *
1907925ca369SKent Gibson 	 * FIXME: find a non-racy way to retrieve this information. Maybe a
1908925ca369SKent Gibson 	 * lock common to both frameworks?
1909925ca369SKent Gibson 	 */
1910925ca369SKent Gibson 	ok_for_pinctrl =
1911aad95584SKent Gibson 		pinctrl_gpio_can_use_line(gc->base + info->offset);
1912925ca369SKent Gibson 
1913925ca369SKent Gibson 	spin_lock_irqsave(&gpio_lock, flags);
1914925ca369SKent Gibson 
191569e4e136SKent Gibson 	if (desc->name)
191669e4e136SKent Gibson 		strscpy(info->name, desc->name, sizeof(info->name));
1917925ca369SKent Gibson 
191869e4e136SKent Gibson 	if (desc->label)
191969e4e136SKent Gibson 		strscpy(info->consumer, desc->label, sizeof(info->consumer));
1920925ca369SKent Gibson 
1921925ca369SKent Gibson 	/*
1922925ca369SKent Gibson 	 * Userspace only need to know that the kernel is using this GPIO so
1923925ca369SKent Gibson 	 * it can't use it.
1924925ca369SKent Gibson 	 */
1925925ca369SKent Gibson 	info->flags = 0;
1926925ca369SKent Gibson 	if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1927925ca369SKent Gibson 	    test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1928925ca369SKent Gibson 	    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1929925ca369SKent Gibson 	    test_bit(FLAG_EXPORT, &desc->flags) ||
1930925ca369SKent Gibson 	    test_bit(FLAG_SYSFS, &desc->flags) ||
1931a0db197fSMarc Zyngier 	    !gpiochip_line_is_valid(gc, info->offset) ||
1932925ca369SKent Gibson 	    !ok_for_pinctrl)
1933aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_USED;
1934aad95584SKent Gibson 
1935925ca369SKent Gibson 	if (test_bit(FLAG_IS_OUT, &desc->flags))
1936aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
1937aad95584SKent Gibson 	else
1938aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_INPUT;
1939aad95584SKent Gibson 
1940925ca369SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1941aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
1942aad95584SKent Gibson 
1943925ca369SKent Gibson 	if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1944aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
1945925ca369SKent Gibson 	if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1946aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
1947aad95584SKent Gibson 
1948925ca369SKent Gibson 	if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
1949aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
1950925ca369SKent Gibson 	if (test_bit(FLAG_PULL_DOWN, &desc->flags))
1951aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
1952925ca369SKent Gibson 	if (test_bit(FLAG_PULL_UP, &desc->flags))
1953aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
1954925ca369SKent Gibson 
195573e03419SKent Gibson 	if (test_bit(FLAG_EDGE_RISING, &desc->flags))
195673e03419SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
195773e03419SKent Gibson 	if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
195873e03419SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
195973e03419SKent Gibson 
196026d060e4SKent Gibson 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
196126d060e4SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
196226d060e4SKent Gibson 
196365cff704SKent Gibson 	debounce_period_us = READ_ONCE(desc->debounce_period_us);
196465cff704SKent Gibson 	if (debounce_period_us) {
196565cff704SKent Gibson 		info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
196665cff704SKent Gibson 		info->attrs[num_attrs].debounce_period_us = debounce_period_us;
196765cff704SKent Gibson 		num_attrs++;
196865cff704SKent Gibson 	}
196965cff704SKent Gibson 	info->num_attrs = num_attrs;
1970925ca369SKent Gibson 
1971925ca369SKent Gibson 	spin_unlock_irqrestore(&gpio_lock, flags);
1972925ca369SKent Gibson }
1973925ca369SKent Gibson 
1974925ca369SKent Gibson struct gpio_chardev_data {
1975925ca369SKent Gibson 	struct gpio_device *gdev;
1976925ca369SKent Gibson 	wait_queue_head_t wait;
1977aad95584SKent Gibson 	DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
1978925ca369SKent Gibson 	struct notifier_block lineinfo_changed_nb;
1979925ca369SKent Gibson 	unsigned long *watched_lines;
1980aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1981aad95584SKent Gibson 	atomic_t watch_abi_version;
1982aad95584SKent Gibson #endif
1983925ca369SKent Gibson };
1984925ca369SKent Gibson 
19852e202ad8SKent Gibson static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
19862e202ad8SKent Gibson {
19872e202ad8SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
19882e202ad8SKent Gibson 	struct gpiochip_info chipinfo;
19892e202ad8SKent Gibson 
19902e202ad8SKent Gibson 	memset(&chipinfo, 0, sizeof(chipinfo));
19912e202ad8SKent Gibson 
19922e202ad8SKent Gibson 	strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
19932e202ad8SKent Gibson 	strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
19942e202ad8SKent Gibson 	chipinfo.lines = gdev->ngpio;
19952e202ad8SKent Gibson 	if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
19962e202ad8SKent Gibson 		return -EFAULT;
19972e202ad8SKent Gibson 	return 0;
19982e202ad8SKent Gibson }
19992e202ad8SKent Gibson 
2000aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2001aad95584SKent Gibson /*
2002aad95584SKent Gibson  * returns 0 if the versions match, else the previously selected ABI version
2003aad95584SKent Gibson  */
2004aad95584SKent Gibson static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
2005aad95584SKent Gibson 				       unsigned int version)
2006aad95584SKent Gibson {
2007aad95584SKent Gibson 	int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
2008aad95584SKent Gibson 
2009aad95584SKent Gibson 	if (abiv == version)
2010aad95584SKent Gibson 		return 0;
2011aad95584SKent Gibson 
2012aad95584SKent Gibson 	return abiv;
2013aad95584SKent Gibson }
20142e202ad8SKent Gibson 
20152e202ad8SKent Gibson static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
20162e202ad8SKent Gibson 			   bool watch)
20172e202ad8SKent Gibson {
20182e202ad8SKent Gibson 	struct gpio_desc *desc;
20192e202ad8SKent Gibson 	struct gpioline_info lineinfo;
20202e202ad8SKent Gibson 	struct gpio_v2_line_info lineinfo_v2;
20212e202ad8SKent Gibson 
20222e202ad8SKent Gibson 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
20232e202ad8SKent Gibson 		return -EFAULT;
20242e202ad8SKent Gibson 
20252e202ad8SKent Gibson 	/* this doubles as a range check on line_offset */
20262e202ad8SKent Gibson 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset);
20272e202ad8SKent Gibson 	if (IS_ERR(desc))
20282e202ad8SKent Gibson 		return PTR_ERR(desc);
20292e202ad8SKent Gibson 
20302e202ad8SKent Gibson 	if (watch) {
20312e202ad8SKent Gibson 		if (lineinfo_ensure_abi_version(cdev, 1))
20322e202ad8SKent Gibson 			return -EPERM;
20332e202ad8SKent Gibson 
20342e202ad8SKent Gibson 		if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
20352e202ad8SKent Gibson 			return -EBUSY;
20362e202ad8SKent Gibson 	}
20372e202ad8SKent Gibson 
20382e202ad8SKent Gibson 	gpio_desc_to_lineinfo(desc, &lineinfo_v2);
20392e202ad8SKent Gibson 	gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
20402e202ad8SKent Gibson 
20412e202ad8SKent Gibson 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
20422e202ad8SKent Gibson 		if (watch)
20432e202ad8SKent Gibson 			clear_bit(lineinfo.line_offset, cdev->watched_lines);
20442e202ad8SKent Gibson 		return -EFAULT;
20452e202ad8SKent Gibson 	}
20462e202ad8SKent Gibson 
20472e202ad8SKent Gibson 	return 0;
20482e202ad8SKent Gibson }
2049aad95584SKent Gibson #endif
2050aad95584SKent Gibson 
2051aad95584SKent Gibson static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
2052aad95584SKent Gibson 			bool watch)
2053aad95584SKent Gibson {
2054aad95584SKent Gibson 	struct gpio_desc *desc;
2055aad95584SKent Gibson 	struct gpio_v2_line_info lineinfo;
2056aad95584SKent Gibson 
2057aad95584SKent Gibson 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2058aad95584SKent Gibson 		return -EFAULT;
2059aad95584SKent Gibson 
2060aad95584SKent Gibson 	if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2061aad95584SKent Gibson 		return -EINVAL;
2062aad95584SKent Gibson 
2063aad95584SKent Gibson 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2064aad95584SKent Gibson 	if (IS_ERR(desc))
2065aad95584SKent Gibson 		return PTR_ERR(desc);
2066aad95584SKent Gibson 
2067aad95584SKent Gibson 	if (watch) {
2068aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2069aad95584SKent Gibson 		if (lineinfo_ensure_abi_version(cdev, 2))
2070aad95584SKent Gibson 			return -EPERM;
2071aad95584SKent Gibson #endif
2072aad95584SKent Gibson 		if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2073aad95584SKent Gibson 			return -EBUSY;
2074aad95584SKent Gibson 	}
2075aad95584SKent Gibson 	gpio_desc_to_lineinfo(desc, &lineinfo);
2076aad95584SKent Gibson 
2077aad95584SKent Gibson 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2078aad95584SKent Gibson 		if (watch)
2079aad95584SKent Gibson 			clear_bit(lineinfo.offset, cdev->watched_lines);
2080aad95584SKent Gibson 		return -EFAULT;
2081aad95584SKent Gibson 	}
2082aad95584SKent Gibson 
2083aad95584SKent Gibson 	return 0;
2084aad95584SKent Gibson }
2085aad95584SKent Gibson 
20862e202ad8SKent Gibson static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
2087925ca369SKent Gibson {
2088925ca369SKent Gibson 	__u32 offset;
2089925ca369SKent Gibson 
2090925ca369SKent Gibson 	if (copy_from_user(&offset, ip, sizeof(offset)))
2091925ca369SKent Gibson 		return -EFAULT;
2092925ca369SKent Gibson 
20931bf7ba40SKent Gibson 	if (offset >= cdev->gdev->ngpio)
20941bf7ba40SKent Gibson 		return -EINVAL;
2095925ca369SKent Gibson 
20961bf7ba40SKent Gibson 	if (!test_and_clear_bit(offset, cdev->watched_lines))
2097925ca369SKent Gibson 		return -EBUSY;
2098925ca369SKent Gibson 
2099925ca369SKent Gibson 	return 0;
2100925ca369SKent Gibson }
21012e202ad8SKent Gibson 
21022e202ad8SKent Gibson /*
21032e202ad8SKent Gibson  * gpio_ioctl() - ioctl handler for the GPIO chardev
21042e202ad8SKent Gibson  */
21052e202ad8SKent Gibson static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
21062e202ad8SKent Gibson {
21072e202ad8SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
21082e202ad8SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
21092e202ad8SKent Gibson 	void __user *ip = (void __user *)arg;
21102e202ad8SKent Gibson 
21112e202ad8SKent Gibson 	/* We fail any subsequent ioctl():s when the chip is gone */
21122e202ad8SKent Gibson 	if (!gdev->chip)
21132e202ad8SKent Gibson 		return -ENODEV;
21142e202ad8SKent Gibson 
21152e202ad8SKent Gibson 	/* Fill in the struct and pass to userspace */
21162e202ad8SKent Gibson 	if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
21172e202ad8SKent Gibson 		return chipinfo_get(cdev, ip);
21182e202ad8SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
21192e202ad8SKent Gibson 	} else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
21202e202ad8SKent Gibson 		return linehandle_create(gdev, ip);
21212e202ad8SKent Gibson 	} else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
21222e202ad8SKent Gibson 		return lineevent_create(gdev, ip);
21232e202ad8SKent Gibson 	} else if (cmd == GPIO_GET_LINEINFO_IOCTL ||
21242e202ad8SKent Gibson 		   cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
21252e202ad8SKent Gibson 		return lineinfo_get_v1(cdev, ip,
21262e202ad8SKent Gibson 				       cmd == GPIO_GET_LINEINFO_WATCH_IOCTL);
21272e202ad8SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
21282e202ad8SKent Gibson 	} else if (cmd == GPIO_V2_GET_LINEINFO_IOCTL ||
21292e202ad8SKent Gibson 		   cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL) {
21302e202ad8SKent Gibson 		return lineinfo_get(cdev, ip,
21312e202ad8SKent Gibson 				    cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL);
21322e202ad8SKent Gibson 	} else if (cmd == GPIO_V2_GET_LINE_IOCTL) {
21332e202ad8SKent Gibson 		return linereq_create(gdev, ip);
21342e202ad8SKent Gibson 	} else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) {
21352e202ad8SKent Gibson 		return lineinfo_unwatch(cdev, ip);
21362e202ad8SKent Gibson 	}
2137925ca369SKent Gibson 	return -EINVAL;
2138925ca369SKent Gibson }
2139925ca369SKent Gibson 
2140925ca369SKent Gibson #ifdef CONFIG_COMPAT
214149bc5279SKent Gibson static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2142925ca369SKent Gibson 			      unsigned long arg)
2143925ca369SKent Gibson {
214449bc5279SKent Gibson 	return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2145925ca369SKent Gibson }
2146925ca369SKent Gibson #endif
2147925ca369SKent Gibson 
2148925ca369SKent Gibson static struct gpio_chardev_data *
2149925ca369SKent Gibson to_gpio_chardev_data(struct notifier_block *nb)
2150925ca369SKent Gibson {
2151925ca369SKent Gibson 	return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2152925ca369SKent Gibson }
2153925ca369SKent Gibson 
2154925ca369SKent Gibson static int lineinfo_changed_notify(struct notifier_block *nb,
2155925ca369SKent Gibson 				   unsigned long action, void *data)
2156925ca369SKent Gibson {
2157e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
2158aad95584SKent Gibson 	struct gpio_v2_line_info_changed chg;
2159925ca369SKent Gibson 	struct gpio_desc *desc = data;
2160925ca369SKent Gibson 	int ret;
2161925ca369SKent Gibson 
2162e2b781c5SKent Gibson 	if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2163925ca369SKent Gibson 		return NOTIFY_DONE;
2164925ca369SKent Gibson 
2165925ca369SKent Gibson 	memset(&chg, 0, sizeof(chg));
2166925ca369SKent Gibson 	chg.event_type = action;
2167aad95584SKent Gibson 	chg.timestamp_ns = ktime_get_ns();
2168925ca369SKent Gibson 	gpio_desc_to_lineinfo(desc, &chg.info);
2169925ca369SKent Gibson 
2170e2b781c5SKent Gibson 	ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2171925ca369SKent Gibson 	if (ret)
2172e2b781c5SKent Gibson 		wake_up_poll(&cdev->wait, EPOLLIN);
2173925ca369SKent Gibson 	else
2174925ca369SKent Gibson 		pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2175925ca369SKent Gibson 
2176925ca369SKent Gibson 	return NOTIFY_OK;
2177925ca369SKent Gibson }
2178925ca369SKent Gibson 
217949bc5279SKent Gibson static __poll_t lineinfo_watch_poll(struct file *file,
2180925ca369SKent Gibson 				    struct poll_table_struct *pollt)
2181925ca369SKent Gibson {
2182e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2183925ca369SKent Gibson 	__poll_t events = 0;
2184925ca369SKent Gibson 
2185e2b781c5SKent Gibson 	poll_wait(file, &cdev->wait, pollt);
2186925ca369SKent Gibson 
2187e2b781c5SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2188e2b781c5SKent Gibson 						 &cdev->wait.lock))
2189925ca369SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
2190925ca369SKent Gibson 
2191925ca369SKent Gibson 	return events;
2192925ca369SKent Gibson }
2193925ca369SKent Gibson 
219449bc5279SKent Gibson static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2195925ca369SKent Gibson 				   size_t count, loff_t *off)
2196925ca369SKent Gibson {
2197e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2198aad95584SKent Gibson 	struct gpio_v2_line_info_changed event;
2199925ca369SKent Gibson 	ssize_t bytes_read = 0;
2200925ca369SKent Gibson 	int ret;
2201aad95584SKent Gibson 	size_t event_size;
2202925ca369SKent Gibson 
2203aad95584SKent Gibson #ifndef CONFIG_GPIO_CDEV_V1
2204aad95584SKent Gibson 	event_size = sizeof(struct gpio_v2_line_info_changed);
2205aad95584SKent Gibson 	if (count < event_size)
2206925ca369SKent Gibson 		return -EINVAL;
2207aad95584SKent Gibson #endif
2208925ca369SKent Gibson 
2209925ca369SKent Gibson 	do {
2210e2b781c5SKent Gibson 		spin_lock(&cdev->wait.lock);
2211e2b781c5SKent Gibson 		if (kfifo_is_empty(&cdev->events)) {
2212925ca369SKent Gibson 			if (bytes_read) {
2213e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2214925ca369SKent Gibson 				return bytes_read;
2215925ca369SKent Gibson 			}
2216925ca369SKent Gibson 
221749bc5279SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
2218e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2219925ca369SKent Gibson 				return -EAGAIN;
2220925ca369SKent Gibson 			}
2221925ca369SKent Gibson 
2222e2b781c5SKent Gibson 			ret = wait_event_interruptible_locked(cdev->wait,
2223e2b781c5SKent Gibson 					!kfifo_is_empty(&cdev->events));
2224925ca369SKent Gibson 			if (ret) {
2225e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2226925ca369SKent Gibson 				return ret;
2227925ca369SKent Gibson 			}
2228925ca369SKent Gibson 		}
2229aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2230aad95584SKent Gibson 		/* must be after kfifo check so watch_abi_version is set */
2231aad95584SKent Gibson 		if (atomic_read(&cdev->watch_abi_version) == 2)
2232aad95584SKent Gibson 			event_size = sizeof(struct gpio_v2_line_info_changed);
2233aad95584SKent Gibson 		else
2234aad95584SKent Gibson 			event_size = sizeof(struct gpioline_info_changed);
2235aad95584SKent Gibson 		if (count < event_size) {
2236aad95584SKent Gibson 			spin_unlock(&cdev->wait.lock);
2237aad95584SKent Gibson 			return -EINVAL;
2238aad95584SKent Gibson 		}
2239aad95584SKent Gibson #endif
2240e2b781c5SKent Gibson 		ret = kfifo_out(&cdev->events, &event, 1);
2241e2b781c5SKent Gibson 		spin_unlock(&cdev->wait.lock);
2242925ca369SKent Gibson 		if (ret != 1) {
2243925ca369SKent Gibson 			ret = -EIO;
2244925ca369SKent Gibson 			break;
2245925ca369SKent Gibson 			/* We should never get here. See lineevent_read(). */
2246925ca369SKent Gibson 		}
2247925ca369SKent Gibson 
2248aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2249aad95584SKent Gibson 		if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2250aad95584SKent Gibson 			if (copy_to_user(buf + bytes_read, &event, event_size))
2251925ca369SKent Gibson 				return -EFAULT;
2252aad95584SKent Gibson 		} else {
2253aad95584SKent Gibson 			struct gpioline_info_changed event_v1;
2254aad95584SKent Gibson 
2255aad95584SKent Gibson 			gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2256aad95584SKent Gibson 			if (copy_to_user(buf + bytes_read, &event_v1,
2257aad95584SKent Gibson 					 event_size))
2258aad95584SKent Gibson 				return -EFAULT;
2259aad95584SKent Gibson 		}
2260aad95584SKent Gibson #else
2261aad95584SKent Gibson 		if (copy_to_user(buf + bytes_read, &event, event_size))
2262aad95584SKent Gibson 			return -EFAULT;
2263aad95584SKent Gibson #endif
2264aad95584SKent Gibson 		bytes_read += event_size;
2265925ca369SKent Gibson 	} while (count >= bytes_read + sizeof(event));
2266925ca369SKent Gibson 
2267925ca369SKent Gibson 	return bytes_read;
2268925ca369SKent Gibson }
2269925ca369SKent Gibson 
2270925ca369SKent Gibson /**
2271925ca369SKent Gibson  * gpio_chrdev_open() - open the chardev for ioctl operations
2272925ca369SKent Gibson  * @inode: inode for this chardev
227349bc5279SKent Gibson  * @file: file struct for storing private data
2274925ca369SKent Gibson  * Returns 0 on success
2275925ca369SKent Gibson  */
227649bc5279SKent Gibson static int gpio_chrdev_open(struct inode *inode, struct file *file)
2277925ca369SKent Gibson {
2278925ca369SKent Gibson 	struct gpio_device *gdev = container_of(inode->i_cdev,
2279925ca369SKent Gibson 						struct gpio_device, chrdev);
2280e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev;
2281925ca369SKent Gibson 	int ret = -ENOMEM;
2282925ca369SKent Gibson 
2283925ca369SKent Gibson 	/* Fail on open if the backing gpiochip is gone */
2284925ca369SKent Gibson 	if (!gdev->chip)
2285925ca369SKent Gibson 		return -ENODEV;
2286925ca369SKent Gibson 
2287e2b781c5SKent Gibson 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2288e2b781c5SKent Gibson 	if (!cdev)
2289925ca369SKent Gibson 		return -ENOMEM;
2290925ca369SKent Gibson 
2291e2b781c5SKent Gibson 	cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2292e2b781c5SKent Gibson 	if (!cdev->watched_lines)
2293e2b781c5SKent Gibson 		goto out_free_cdev;
2294925ca369SKent Gibson 
2295e2b781c5SKent Gibson 	init_waitqueue_head(&cdev->wait);
2296e2b781c5SKent Gibson 	INIT_KFIFO(cdev->events);
2297e2b781c5SKent Gibson 	cdev->gdev = gdev;
2298925ca369SKent Gibson 
2299e2b781c5SKent Gibson 	cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
23006accc376SKent Gibson 	ret = blocking_notifier_chain_register(&gdev->notifier,
2301e2b781c5SKent Gibson 					       &cdev->lineinfo_changed_nb);
2302925ca369SKent Gibson 	if (ret)
2303925ca369SKent Gibson 		goto out_free_bitmap;
2304925ca369SKent Gibson 
2305925ca369SKent Gibson 	get_device(&gdev->dev);
2306e2b781c5SKent Gibson 	file->private_data = cdev;
2307925ca369SKent Gibson 
230849bc5279SKent Gibson 	ret = nonseekable_open(inode, file);
2309925ca369SKent Gibson 	if (ret)
2310925ca369SKent Gibson 		goto out_unregister_notifier;
2311925ca369SKent Gibson 
2312925ca369SKent Gibson 	return ret;
2313925ca369SKent Gibson 
2314925ca369SKent Gibson out_unregister_notifier:
23156accc376SKent Gibson 	blocking_notifier_chain_unregister(&gdev->notifier,
2316e2b781c5SKent Gibson 					   &cdev->lineinfo_changed_nb);
2317925ca369SKent Gibson out_free_bitmap:
2318e2b781c5SKent Gibson 	bitmap_free(cdev->watched_lines);
2319e2b781c5SKent Gibson out_free_cdev:
2320e2b781c5SKent Gibson 	kfree(cdev);
2321925ca369SKent Gibson 	return ret;
2322925ca369SKent Gibson }
2323925ca369SKent Gibson 
2324925ca369SKent Gibson /**
2325925ca369SKent Gibson  * gpio_chrdev_release() - close chardev after ioctl operations
2326925ca369SKent Gibson  * @inode: inode for this chardev
232749bc5279SKent Gibson  * @file: file struct for storing private data
2328925ca369SKent Gibson  * Returns 0 on success
2329925ca369SKent Gibson  */
233049bc5279SKent Gibson static int gpio_chrdev_release(struct inode *inode, struct file *file)
2331925ca369SKent Gibson {
2332e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2333e2b781c5SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
2334925ca369SKent Gibson 
2335e2b781c5SKent Gibson 	bitmap_free(cdev->watched_lines);
23366accc376SKent Gibson 	blocking_notifier_chain_unregister(&gdev->notifier,
2337e2b781c5SKent Gibson 					   &cdev->lineinfo_changed_nb);
2338925ca369SKent Gibson 	put_device(&gdev->dev);
2339e2b781c5SKent Gibson 	kfree(cdev);
2340925ca369SKent Gibson 
2341925ca369SKent Gibson 	return 0;
2342925ca369SKent Gibson }
2343925ca369SKent Gibson 
2344925ca369SKent Gibson static const struct file_operations gpio_fileops = {
2345925ca369SKent Gibson 	.release = gpio_chrdev_release,
2346925ca369SKent Gibson 	.open = gpio_chrdev_open,
2347925ca369SKent Gibson 	.poll = lineinfo_watch_poll,
2348925ca369SKent Gibson 	.read = lineinfo_watch_read,
2349925ca369SKent Gibson 	.owner = THIS_MODULE,
2350925ca369SKent Gibson 	.llseek = no_llseek,
2351925ca369SKent Gibson 	.unlocked_ioctl = gpio_ioctl,
2352925ca369SKent Gibson #ifdef CONFIG_COMPAT
2353925ca369SKent Gibson 	.compat_ioctl = gpio_ioctl_compat,
2354925ca369SKent Gibson #endif
2355925ca369SKent Gibson };
2356925ca369SKent Gibson 
2357925ca369SKent Gibson int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2358925ca369SKent Gibson {
2359925ca369SKent Gibson 	int ret;
2360925ca369SKent Gibson 
2361925ca369SKent Gibson 	cdev_init(&gdev->chrdev, &gpio_fileops);
2362925ca369SKent Gibson 	gdev->chrdev.owner = THIS_MODULE;
2363925ca369SKent Gibson 	gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2364925ca369SKent Gibson 
2365925ca369SKent Gibson 	ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2366925ca369SKent Gibson 	if (ret)
2367925ca369SKent Gibson 		return ret;
2368925ca369SKent Gibson 
2369925ca369SKent Gibson 	chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2370925ca369SKent Gibson 		 MAJOR(devt), gdev->id);
2371925ca369SKent Gibson 
2372925ca369SKent Gibson 	return 0;
2373925ca369SKent Gibson }
2374925ca369SKent Gibson 
2375925ca369SKent Gibson void gpiolib_cdev_unregister(struct gpio_device *gdev)
2376925ca369SKent Gibson {
2377925ca369SKent Gibson 	cdev_device_del(&gdev->chrdev, &gdev->dev);
2378925ca369SKent Gibson }
2379