xref: /openbmc/linux/drivers/gpio/gpiolib-cdev.c (revision 5e2ca893d772560a0926a31b88f8c83efbc6b058)
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 
513*5e2ca893SKent Gibson #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS
514*5e2ca893SKent 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 | \
5203c0d9c63SKent Gibson 	 GPIO_V2_LINE_BIAS_FLAGS)
5213c0d9c63SKent Gibson 
52273e03419SKent Gibson static void linereq_put_event(struct linereq *lr,
52373e03419SKent Gibson 			      struct gpio_v2_line_event *le)
52473e03419SKent Gibson {
52573e03419SKent Gibson 	bool overflow = false;
52673e03419SKent Gibson 
52773e03419SKent Gibson 	spin_lock(&lr->wait.lock);
52873e03419SKent Gibson 	if (kfifo_is_full(&lr->events)) {
52973e03419SKent Gibson 		overflow = true;
53073e03419SKent Gibson 		kfifo_skip(&lr->events);
53173e03419SKent Gibson 	}
53273e03419SKent Gibson 	kfifo_in(&lr->events, le, 1);
53373e03419SKent Gibson 	spin_unlock(&lr->wait.lock);
53473e03419SKent Gibson 	if (!overflow)
53573e03419SKent Gibson 		wake_up_poll(&lr->wait, EPOLLIN);
53673e03419SKent Gibson 	else
53773e03419SKent Gibson 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
53873e03419SKent Gibson }
53973e03419SKent Gibson 
54073e03419SKent Gibson static irqreturn_t edge_irq_thread(int irq, void *p)
54173e03419SKent Gibson {
54273e03419SKent Gibson 	struct line *line = p;
54373e03419SKent Gibson 	struct linereq *lr = line->req;
54473e03419SKent Gibson 	struct gpio_v2_line_event le;
5453ffb7c45SKent Gibson 	u64 eflags;
54673e03419SKent Gibson 
54773e03419SKent Gibson 	/* Do not leak kernel stack to userspace */
54873e03419SKent Gibson 	memset(&le, 0, sizeof(le));
54973e03419SKent Gibson 
55073e03419SKent Gibson 	if (line->timestamp_ns) {
55173e03419SKent Gibson 		le.timestamp_ns = line->timestamp_ns;
55273e03419SKent Gibson 	} else {
55373e03419SKent Gibson 		/*
55473e03419SKent Gibson 		 * We may be running from a nested threaded interrupt in
55573e03419SKent Gibson 		 * which case we didn't get the timestamp from
55673e03419SKent Gibson 		 * edge_irq_handler().
55773e03419SKent Gibson 		 */
55873e03419SKent Gibson 		le.timestamp_ns = ktime_get_ns();
55973e03419SKent Gibson 		if (lr->num_lines != 1)
56073e03419SKent Gibson 			line->req_seqno = atomic_inc_return(&lr->seqno);
56173e03419SKent Gibson 	}
56273e03419SKent Gibson 	line->timestamp_ns = 0;
56373e03419SKent Gibson 
5643ffb7c45SKent Gibson 	eflags = READ_ONCE(line->eflags);
565*5e2ca893SKent Gibson 	if (eflags == GPIO_V2_LINE_FLAG_EDGE_BOTH) {
56673e03419SKent Gibson 		int level = gpiod_get_value_cansleep(line->desc);
56773e03419SKent Gibson 
56873e03419SKent Gibson 		if (level)
56973e03419SKent Gibson 			/* Emit low-to-high event */
57073e03419SKent Gibson 			le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
57173e03419SKent Gibson 		else
57273e03419SKent Gibson 			/* Emit high-to-low event */
57373e03419SKent Gibson 			le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
5743ffb7c45SKent Gibson 	} else if (eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) {
57573e03419SKent Gibson 		/* Emit low-to-high event */
57673e03419SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
5773ffb7c45SKent Gibson 	} else if (eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) {
57873e03419SKent Gibson 		/* Emit high-to-low event */
57973e03419SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
58073e03419SKent Gibson 	} else {
58173e03419SKent Gibson 		return IRQ_NONE;
58273e03419SKent Gibson 	}
58373e03419SKent Gibson 	line->line_seqno++;
58473e03419SKent Gibson 	le.line_seqno = line->line_seqno;
58573e03419SKent Gibson 	le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
58673e03419SKent Gibson 	le.offset = gpio_chip_hwgpio(line->desc);
58773e03419SKent Gibson 
58873e03419SKent Gibson 	linereq_put_event(lr, &le);
58973e03419SKent Gibson 
59073e03419SKent Gibson 	return IRQ_HANDLED;
59173e03419SKent Gibson }
59273e03419SKent Gibson 
59373e03419SKent Gibson static irqreturn_t edge_irq_handler(int irq, void *p)
59473e03419SKent Gibson {
59573e03419SKent Gibson 	struct line *line = p;
59673e03419SKent Gibson 	struct linereq *lr = line->req;
59773e03419SKent Gibson 
59873e03419SKent Gibson 	/*
59973e03419SKent Gibson 	 * Just store the timestamp in hardirq context so we get it as
60073e03419SKent Gibson 	 * close in time as possible to the actual event.
60173e03419SKent Gibson 	 */
60273e03419SKent Gibson 	line->timestamp_ns = ktime_get_ns();
60373e03419SKent Gibson 
60473e03419SKent Gibson 	if (lr->num_lines != 1)
60573e03419SKent Gibson 		line->req_seqno = atomic_inc_return(&lr->seqno);
60673e03419SKent Gibson 
60773e03419SKent Gibson 	return IRQ_WAKE_THREAD;
60873e03419SKent Gibson }
60973e03419SKent Gibson 
61065cff704SKent Gibson /*
61165cff704SKent Gibson  * returns the current debounced logical value.
61265cff704SKent Gibson  */
61365cff704SKent Gibson static bool debounced_value(struct line *line)
61465cff704SKent Gibson {
61565cff704SKent Gibson 	bool value;
61665cff704SKent Gibson 
61765cff704SKent Gibson 	/*
61865cff704SKent Gibson 	 * minor race - debouncer may be stopped here, so edge_detector_stop()
61965cff704SKent Gibson 	 * must leave the value unchanged so the following will read the level
62065cff704SKent Gibson 	 * from when the debouncer was last running.
62165cff704SKent Gibson 	 */
62265cff704SKent Gibson 	value = READ_ONCE(line->level);
62365cff704SKent Gibson 
62465cff704SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
62565cff704SKent Gibson 		value = !value;
62665cff704SKent Gibson 
62765cff704SKent Gibson 	return value;
62865cff704SKent Gibson }
62965cff704SKent Gibson 
63065cff704SKent Gibson static irqreturn_t debounce_irq_handler(int irq, void *p)
63165cff704SKent Gibson {
63265cff704SKent Gibson 	struct line *line = p;
63365cff704SKent Gibson 
63465cff704SKent Gibson 	mod_delayed_work(system_wq, &line->work,
63565cff704SKent Gibson 		usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
63665cff704SKent Gibson 
63765cff704SKent Gibson 	return IRQ_HANDLED;
63865cff704SKent Gibson }
63965cff704SKent Gibson 
64065cff704SKent Gibson static void debounce_work_func(struct work_struct *work)
64165cff704SKent Gibson {
64265cff704SKent Gibson 	struct gpio_v2_line_event le;
64365cff704SKent Gibson 	struct line *line = container_of(work, struct line, work.work);
64465cff704SKent Gibson 	struct linereq *lr;
64565cff704SKent Gibson 	int level;
6463ffb7c45SKent Gibson 	u64 eflags;
64765cff704SKent Gibson 
64865cff704SKent Gibson 	level = gpiod_get_raw_value_cansleep(line->desc);
64965cff704SKent Gibson 	if (level < 0) {
65065cff704SKent Gibson 		pr_debug_ratelimited("debouncer failed to read line value\n");
65165cff704SKent Gibson 		return;
65265cff704SKent Gibson 	}
65365cff704SKent Gibson 
65465cff704SKent Gibson 	if (READ_ONCE(line->level) == level)
65565cff704SKent Gibson 		return;
65665cff704SKent Gibson 
65765cff704SKent Gibson 	WRITE_ONCE(line->level, level);
65865cff704SKent Gibson 
65965cff704SKent Gibson 	/* -- edge detection -- */
6603ffb7c45SKent Gibson 	eflags = READ_ONCE(line->eflags);
6613ffb7c45SKent Gibson 	if (!eflags)
66265cff704SKent Gibson 		return;
66365cff704SKent Gibson 
66465cff704SKent Gibson 	/* switch from physical level to logical - if they differ */
66565cff704SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
66665cff704SKent Gibson 		level = !level;
66765cff704SKent Gibson 
66865cff704SKent Gibson 	/* ignore edges that are not being monitored */
6693ffb7c45SKent Gibson 	if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
6703ffb7c45SKent Gibson 	    ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
67165cff704SKent Gibson 		return;
67265cff704SKent Gibson 
67365cff704SKent Gibson 	/* Do not leak kernel stack to userspace */
67465cff704SKent Gibson 	memset(&le, 0, sizeof(le));
67565cff704SKent Gibson 
67665cff704SKent Gibson 	lr = line->req;
67765cff704SKent Gibson 	le.timestamp_ns = ktime_get_ns();
67865cff704SKent Gibson 	le.offset = gpio_chip_hwgpio(line->desc);
67965cff704SKent Gibson 	line->line_seqno++;
68065cff704SKent Gibson 	le.line_seqno = line->line_seqno;
68165cff704SKent Gibson 	le.seqno = (lr->num_lines == 1) ?
68265cff704SKent Gibson 		le.line_seqno : atomic_inc_return(&lr->seqno);
68365cff704SKent Gibson 
68465cff704SKent Gibson 	if (level)
68565cff704SKent Gibson 		/* Emit low-to-high event */
68665cff704SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
68765cff704SKent Gibson 	else
68865cff704SKent Gibson 		/* Emit high-to-low event */
68965cff704SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
69065cff704SKent Gibson 
69165cff704SKent Gibson 	linereq_put_event(lr, &le);
69265cff704SKent Gibson }
69365cff704SKent Gibson 
69465cff704SKent Gibson static int debounce_setup(struct line *line,
69565cff704SKent Gibson 			  unsigned int debounce_period_us)
69665cff704SKent Gibson {
69765cff704SKent Gibson 	unsigned long irqflags;
69865cff704SKent Gibson 	int ret, level, irq;
69965cff704SKent Gibson 
70065cff704SKent Gibson 	/* try hardware */
70165cff704SKent Gibson 	ret = gpiod_set_debounce(line->desc, debounce_period_us);
70265cff704SKent Gibson 	if (!ret) {
70365cff704SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
70465cff704SKent Gibson 		return ret;
70565cff704SKent Gibson 	}
70665cff704SKent Gibson 	if (ret != -ENOTSUPP)
70765cff704SKent Gibson 		return ret;
70865cff704SKent Gibson 
70965cff704SKent Gibson 	if (debounce_period_us) {
71065cff704SKent Gibson 		/* setup software debounce */
71165cff704SKent Gibson 		level = gpiod_get_raw_value_cansleep(line->desc);
71265cff704SKent Gibson 		if (level < 0)
71365cff704SKent Gibson 			return level;
71465cff704SKent Gibson 
71565cff704SKent Gibson 		irq = gpiod_to_irq(line->desc);
71665cff704SKent Gibson 		if (irq < 0)
71765cff704SKent Gibson 			return -ENXIO;
71865cff704SKent Gibson 
71965cff704SKent Gibson 		WRITE_ONCE(line->level, level);
72065cff704SKent Gibson 		irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
72165cff704SKent Gibson 		ret = request_irq(irq, debounce_irq_handler, irqflags,
72265cff704SKent Gibson 				  line->req->label, line);
72365cff704SKent Gibson 		if (ret)
72465cff704SKent Gibson 			return ret;
72565cff704SKent Gibson 
72665cff704SKent Gibson 		WRITE_ONCE(line->sw_debounced, 1);
72765cff704SKent Gibson 		line->irq = irq;
72865cff704SKent Gibson 	}
72965cff704SKent Gibson 	return 0;
73065cff704SKent Gibson }
73165cff704SKent Gibson 
73265cff704SKent Gibson static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
73365cff704SKent Gibson 					  unsigned int line_idx)
73465cff704SKent Gibson {
73565cff704SKent Gibson 	unsigned int i;
73665cff704SKent Gibson 	u64 mask = BIT_ULL(line_idx);
73765cff704SKent Gibson 
73865cff704SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
73965cff704SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
74065cff704SKent Gibson 		    (lc->attrs[i].mask & mask))
74165cff704SKent Gibson 			return true;
74265cff704SKent Gibson 	}
74365cff704SKent Gibson 	return false;
74465cff704SKent Gibson }
74565cff704SKent Gibson 
74665cff704SKent Gibson static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
74765cff704SKent Gibson 					       unsigned int line_idx)
74865cff704SKent Gibson {
74965cff704SKent Gibson 	unsigned int i;
75065cff704SKent Gibson 	u64 mask = BIT_ULL(line_idx);
75165cff704SKent Gibson 
75265cff704SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
75365cff704SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
75465cff704SKent Gibson 		    (lc->attrs[i].mask & mask))
75565cff704SKent Gibson 			return lc->attrs[i].attr.debounce_period_us;
75665cff704SKent Gibson 	}
75765cff704SKent Gibson 	return 0;
75865cff704SKent Gibson }
75965cff704SKent Gibson 
76073e03419SKent Gibson static void edge_detector_stop(struct line *line)
76173e03419SKent Gibson {
76273e03419SKent Gibson 	if (line->irq) {
76373e03419SKent Gibson 		free_irq(line->irq, line);
76473e03419SKent Gibson 		line->irq = 0;
76573e03419SKent Gibson 	}
766a54756cbSKent Gibson 
76765cff704SKent Gibson 	cancel_delayed_work_sync(&line->work);
76865cff704SKent Gibson 	WRITE_ONCE(line->sw_debounced, 0);
7693ffb7c45SKent Gibson 	WRITE_ONCE(line->eflags, 0);
77065cff704SKent Gibson 	/* do not change line->level - see comment in debounced_value() */
77173e03419SKent Gibson }
77273e03419SKent Gibson 
77373e03419SKent Gibson static int edge_detector_setup(struct line *line,
77465cff704SKent Gibson 			       struct gpio_v2_line_config *lc,
77565cff704SKent Gibson 			       unsigned int line_idx,
77673e03419SKent Gibson 			       u64 eflags)
77773e03419SKent Gibson {
77865cff704SKent Gibson 	u32 debounce_period_us;
77973e03419SKent Gibson 	unsigned long irqflags = 0;
78073e03419SKent Gibson 	int irq, ret;
78173e03419SKent Gibson 
78273e03419SKent Gibson 	if (eflags && !kfifo_initialized(&line->req->events)) {
78373e03419SKent Gibson 		ret = kfifo_alloc(&line->req->events,
78473e03419SKent Gibson 				  line->req->event_buffer_size, GFP_KERNEL);
78573e03419SKent Gibson 		if (ret)
78673e03419SKent Gibson 			return ret;
78773e03419SKent Gibson 	}
7883ffb7c45SKent Gibson 	WRITE_ONCE(line->eflags, eflags);
78965cff704SKent Gibson 	if (gpio_v2_line_config_debounced(lc, line_idx)) {
79065cff704SKent Gibson 		debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
79165cff704SKent Gibson 		ret = debounce_setup(line, debounce_period_us);
79265cff704SKent Gibson 		if (ret)
79365cff704SKent Gibson 			return ret;
79465cff704SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
79565cff704SKent Gibson 	}
79673e03419SKent Gibson 
79765cff704SKent Gibson 	/* detection disabled or sw debouncer will provide edge detection */
79865cff704SKent Gibson 	if (!eflags || READ_ONCE(line->sw_debounced))
79973e03419SKent Gibson 		return 0;
80073e03419SKent Gibson 
80173e03419SKent Gibson 	irq = gpiod_to_irq(line->desc);
80273e03419SKent Gibson 	if (irq < 0)
80373e03419SKent Gibson 		return -ENXIO;
80473e03419SKent Gibson 
80573e03419SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
80673e03419SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
80773e03419SKent Gibson 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
80873e03419SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
80973e03419SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
81073e03419SKent Gibson 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
81173e03419SKent Gibson 	irqflags |= IRQF_ONESHOT;
81273e03419SKent Gibson 
81373e03419SKent Gibson 	/* Request a thread to read the events */
81473e03419SKent Gibson 	ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
81573e03419SKent Gibson 				   irqflags, line->req->label, line);
81673e03419SKent Gibson 	if (ret)
81773e03419SKent Gibson 		return ret;
81873e03419SKent Gibson 
81973e03419SKent Gibson 	line->irq = irq;
82073e03419SKent Gibson 	return 0;
82173e03419SKent Gibson }
82273e03419SKent Gibson 
82365cff704SKent Gibson static int edge_detector_update(struct line *line,
82465cff704SKent Gibson 				struct gpio_v2_line_config *lc,
82565cff704SKent Gibson 				unsigned int line_idx,
82665cff704SKent Gibson 				u64 eflags, bool polarity_change)
827a54756cbSKent Gibson {
82865cff704SKent Gibson 	unsigned int debounce_period_us =
82965cff704SKent Gibson 		gpio_v2_line_config_debounce_period(lc, line_idx);
83065cff704SKent Gibson 
8313ffb7c45SKent Gibson 	if ((READ_ONCE(line->eflags) == eflags) && !polarity_change &&
83265cff704SKent Gibson 	    (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us))
833a54756cbSKent Gibson 		return 0;
834a54756cbSKent Gibson 
83565cff704SKent Gibson 	/* sw debounced and still will be...*/
83665cff704SKent Gibson 	if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
8373ffb7c45SKent Gibson 		WRITE_ONCE(line->eflags, eflags);
83865cff704SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
83965cff704SKent Gibson 		return 0;
84065cff704SKent Gibson 	}
84165cff704SKent Gibson 
84265cff704SKent Gibson 	/* reconfiguring edge detection or sw debounce being disabled */
84365cff704SKent Gibson 	if ((line->irq && !READ_ONCE(line->sw_debounced)) ||
84465cff704SKent Gibson 	    (!debounce_period_us && READ_ONCE(line->sw_debounced)))
845a54756cbSKent Gibson 		edge_detector_stop(line);
846a54756cbSKent Gibson 
84765cff704SKent Gibson 	return edge_detector_setup(line, lc, line_idx, eflags);
848a54756cbSKent Gibson }
849a54756cbSKent Gibson 
8503c0d9c63SKent Gibson static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
8513c0d9c63SKent Gibson 				     unsigned int line_idx)
8523c0d9c63SKent Gibson {
8533c0d9c63SKent Gibson 	unsigned int i;
8543c0d9c63SKent Gibson 	u64 mask = BIT_ULL(line_idx);
8553c0d9c63SKent Gibson 
8563c0d9c63SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
8573c0d9c63SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
8583c0d9c63SKent Gibson 		    (lc->attrs[i].mask & mask))
8593c0d9c63SKent Gibson 			return lc->attrs[i].attr.flags;
8603c0d9c63SKent Gibson 	}
8613c0d9c63SKent Gibson 	return lc->flags;
8623c0d9c63SKent Gibson }
8633c0d9c63SKent Gibson 
8643c0d9c63SKent Gibson static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
8653c0d9c63SKent Gibson 					    unsigned int line_idx)
8663c0d9c63SKent Gibson {
8673c0d9c63SKent Gibson 	unsigned int i;
8683c0d9c63SKent Gibson 	u64 mask = BIT_ULL(line_idx);
8693c0d9c63SKent Gibson 
8703c0d9c63SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
8713c0d9c63SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
8723c0d9c63SKent Gibson 		    (lc->attrs[i].mask & mask))
8733c0d9c63SKent Gibson 			return !!(lc->attrs[i].attr.values & mask);
8743c0d9c63SKent Gibson 	}
8753c0d9c63SKent Gibson 	return 0;
8763c0d9c63SKent Gibson }
8773c0d9c63SKent Gibson 
8783c0d9c63SKent Gibson static int gpio_v2_line_flags_validate(u64 flags)
8793c0d9c63SKent Gibson {
8803c0d9c63SKent Gibson 	/* Return an error if an unknown flag is set */
8813c0d9c63SKent Gibson 	if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
8823c0d9c63SKent Gibson 		return -EINVAL;
8833c0d9c63SKent Gibson 
8843c0d9c63SKent Gibson 	/*
8853c0d9c63SKent Gibson 	 * Do not allow both INPUT and OUTPUT flags to be set as they are
8863c0d9c63SKent Gibson 	 * contradictory.
8873c0d9c63SKent Gibson 	 */
8883c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
8893c0d9c63SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_OUTPUT))
8903c0d9c63SKent Gibson 		return -EINVAL;
8913c0d9c63SKent Gibson 
89273e03419SKent Gibson 	/* Edge detection requires explicit input. */
89373e03419SKent Gibson 	if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
89473e03419SKent Gibson 	    !(flags & GPIO_V2_LINE_FLAG_INPUT))
89573e03419SKent Gibson 		return -EINVAL;
89673e03419SKent Gibson 
8973c0d9c63SKent Gibson 	/*
8983c0d9c63SKent Gibson 	 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
8993c0d9c63SKent Gibson 	 * request. If the hardware actually supports enabling both at the
9003c0d9c63SKent Gibson 	 * same time the electrical result would be disastrous.
9013c0d9c63SKent Gibson 	 */
9023c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
9033c0d9c63SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
9043c0d9c63SKent Gibson 		return -EINVAL;
9053c0d9c63SKent Gibson 
9063c0d9c63SKent Gibson 	/* Drive requires explicit output direction. */
9073c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
9083c0d9c63SKent Gibson 	    !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
9093c0d9c63SKent Gibson 		return -EINVAL;
9103c0d9c63SKent Gibson 
9113c0d9c63SKent Gibson 	/* Bias requires explicit direction. */
9123c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
9133c0d9c63SKent Gibson 	    !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
9143c0d9c63SKent Gibson 		return -EINVAL;
9153c0d9c63SKent Gibson 
9163c0d9c63SKent Gibson 	/* Only one bias flag can be set. */
9173c0d9c63SKent Gibson 	if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
9183c0d9c63SKent Gibson 	     (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
9193c0d9c63SKent Gibson 		       GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
9203c0d9c63SKent Gibson 	    ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
9213c0d9c63SKent Gibson 	     (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
9223c0d9c63SKent Gibson 		return -EINVAL;
9233c0d9c63SKent Gibson 
9243c0d9c63SKent Gibson 	return 0;
9253c0d9c63SKent Gibson }
9263c0d9c63SKent Gibson 
9273c0d9c63SKent Gibson static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
9283c0d9c63SKent Gibson 					unsigned int num_lines)
9293c0d9c63SKent Gibson {
9303c0d9c63SKent Gibson 	unsigned int i;
9313c0d9c63SKent Gibson 	u64 flags;
9323c0d9c63SKent Gibson 	int ret;
9333c0d9c63SKent Gibson 
9343c0d9c63SKent Gibson 	if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
9353c0d9c63SKent Gibson 		return -EINVAL;
9363c0d9c63SKent Gibson 
9373c0d9c63SKent Gibson 	if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
9383c0d9c63SKent Gibson 		return -EINVAL;
9393c0d9c63SKent Gibson 
9403c0d9c63SKent Gibson 	for (i = 0; i < num_lines; i++) {
9413c0d9c63SKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
9423c0d9c63SKent Gibson 		ret = gpio_v2_line_flags_validate(flags);
9433c0d9c63SKent Gibson 		if (ret)
9443c0d9c63SKent Gibson 			return ret;
94565cff704SKent Gibson 
94665cff704SKent Gibson 		/* debounce requires explicit input */
94765cff704SKent Gibson 		if (gpio_v2_line_config_debounced(lc, i) &&
94865cff704SKent Gibson 		    !(flags & GPIO_V2_LINE_FLAG_INPUT))
94965cff704SKent Gibson 			return -EINVAL;
9503c0d9c63SKent Gibson 	}
9513c0d9c63SKent Gibson 	return 0;
9523c0d9c63SKent Gibson }
9533c0d9c63SKent Gibson 
9543c0d9c63SKent Gibson static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
9553c0d9c63SKent Gibson 						    unsigned long *flagsp)
9563c0d9c63SKent Gibson {
9573c0d9c63SKent Gibson 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
9583c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
9593c0d9c63SKent Gibson 
9603c0d9c63SKent Gibson 	if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
9613c0d9c63SKent Gibson 		set_bit(FLAG_IS_OUT, flagsp);
9623c0d9c63SKent Gibson 	else if (flags & GPIO_V2_LINE_FLAG_INPUT)
9633c0d9c63SKent Gibson 		clear_bit(FLAG_IS_OUT, flagsp);
9643c0d9c63SKent Gibson 
96573e03419SKent Gibson 	assign_bit(FLAG_EDGE_RISING, flagsp,
96673e03419SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
96773e03419SKent Gibson 	assign_bit(FLAG_EDGE_FALLING, flagsp,
96873e03419SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
96973e03419SKent Gibson 
9703c0d9c63SKent Gibson 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
9713c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
9723c0d9c63SKent Gibson 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
9733c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
9743c0d9c63SKent Gibson 
9753c0d9c63SKent Gibson 	assign_bit(FLAG_PULL_UP, flagsp,
9763c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
9773c0d9c63SKent Gibson 	assign_bit(FLAG_PULL_DOWN, flagsp,
9783c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
9793c0d9c63SKent Gibson 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
9803c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
9813c0d9c63SKent Gibson }
9823c0d9c63SKent Gibson 
9833c0d9c63SKent Gibson static long linereq_get_values(struct linereq *lr, void __user *ip)
9843c0d9c63SKent Gibson {
9853c0d9c63SKent Gibson 	struct gpio_v2_line_values lv;
9863c0d9c63SKent Gibson 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
9873c0d9c63SKent Gibson 	struct gpio_desc **descs;
9883c0d9c63SKent Gibson 	unsigned int i, didx, num_get;
98965cff704SKent Gibson 	bool val;
9903c0d9c63SKent Gibson 	int ret;
9913c0d9c63SKent Gibson 
9923c0d9c63SKent Gibson 	/* NOTE: It's ok to read values of output lines. */
9933c0d9c63SKent Gibson 	if (copy_from_user(&lv, ip, sizeof(lv)))
9943c0d9c63SKent Gibson 		return -EFAULT;
9953c0d9c63SKent Gibson 
9963c0d9c63SKent Gibson 	for (num_get = 0, i = 0; i < lr->num_lines; i++) {
9973c0d9c63SKent Gibson 		if (lv.mask & BIT_ULL(i)) {
9983c0d9c63SKent Gibson 			num_get++;
9993c0d9c63SKent Gibson 			descs = &lr->lines[i].desc;
10003c0d9c63SKent Gibson 		}
10013c0d9c63SKent Gibson 	}
10023c0d9c63SKent Gibson 
10033c0d9c63SKent Gibson 	if (num_get == 0)
10043c0d9c63SKent Gibson 		return -EINVAL;
10053c0d9c63SKent Gibson 
10063c0d9c63SKent Gibson 	if (num_get != 1) {
10073c0d9c63SKent Gibson 		descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
10083c0d9c63SKent Gibson 		if (!descs)
10093c0d9c63SKent Gibson 			return -ENOMEM;
10103c0d9c63SKent Gibson 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
10113c0d9c63SKent Gibson 			if (lv.mask & BIT_ULL(i)) {
10123c0d9c63SKent Gibson 				descs[didx] = lr->lines[i].desc;
10133c0d9c63SKent Gibson 				didx++;
10143c0d9c63SKent Gibson 			}
10153c0d9c63SKent Gibson 		}
10163c0d9c63SKent Gibson 	}
10173c0d9c63SKent Gibson 	ret = gpiod_get_array_value_complex(false, true, num_get,
10183c0d9c63SKent Gibson 					    descs, NULL, vals);
10193c0d9c63SKent Gibson 
10203c0d9c63SKent Gibson 	if (num_get != 1)
10213c0d9c63SKent Gibson 		kfree(descs);
10223c0d9c63SKent Gibson 	if (ret)
10233c0d9c63SKent Gibson 		return ret;
10243c0d9c63SKent Gibson 
10253c0d9c63SKent Gibson 	lv.bits = 0;
10263c0d9c63SKent Gibson 	for (didx = 0, i = 0; i < lr->num_lines; i++) {
10273c0d9c63SKent Gibson 		if (lv.mask & BIT_ULL(i)) {
102865cff704SKent Gibson 			if (lr->lines[i].sw_debounced)
102965cff704SKent Gibson 				val = debounced_value(&lr->lines[i]);
103065cff704SKent Gibson 			else
103165cff704SKent Gibson 				val = test_bit(didx, vals);
103265cff704SKent Gibson 			if (val)
10333c0d9c63SKent Gibson 				lv.bits |= BIT_ULL(i);
10343c0d9c63SKent Gibson 			didx++;
10353c0d9c63SKent Gibson 		}
10363c0d9c63SKent Gibson 	}
10373c0d9c63SKent Gibson 
10383c0d9c63SKent Gibson 	if (copy_to_user(ip, &lv, sizeof(lv)))
10393c0d9c63SKent Gibson 		return -EFAULT;
10403c0d9c63SKent Gibson 
10413c0d9c63SKent Gibson 	return 0;
10423c0d9c63SKent Gibson }
10433c0d9c63SKent Gibson 
10447b8e00d9SKent Gibson static long linereq_set_values_unlocked(struct linereq *lr,
10457b8e00d9SKent Gibson 					struct gpio_v2_line_values *lv)
10467b8e00d9SKent Gibson {
10477b8e00d9SKent Gibson 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
10487b8e00d9SKent Gibson 	struct gpio_desc **descs;
10497b8e00d9SKent Gibson 	unsigned int i, didx, num_set;
10507b8e00d9SKent Gibson 	int ret;
10517b8e00d9SKent Gibson 
10527b8e00d9SKent Gibson 	bitmap_zero(vals, GPIO_V2_LINES_MAX);
10537b8e00d9SKent Gibson 	for (num_set = 0, i = 0; i < lr->num_lines; i++) {
10547b8e00d9SKent Gibson 		if (lv->mask & BIT_ULL(i)) {
10557b8e00d9SKent Gibson 			if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
10567b8e00d9SKent Gibson 				return -EPERM;
10577b8e00d9SKent Gibson 			if (lv->bits & BIT_ULL(i))
10587b8e00d9SKent Gibson 				__set_bit(num_set, vals);
10597b8e00d9SKent Gibson 			num_set++;
10607b8e00d9SKent Gibson 			descs = &lr->lines[i].desc;
10617b8e00d9SKent Gibson 		}
10627b8e00d9SKent Gibson 	}
10637b8e00d9SKent Gibson 	if (num_set == 0)
10647b8e00d9SKent Gibson 		return -EINVAL;
10657b8e00d9SKent Gibson 
10667b8e00d9SKent Gibson 	if (num_set != 1) {
10677b8e00d9SKent Gibson 		/* build compacted desc array and values */
10687b8e00d9SKent Gibson 		descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
10697b8e00d9SKent Gibson 		if (!descs)
10707b8e00d9SKent Gibson 			return -ENOMEM;
10717b8e00d9SKent Gibson 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
10727b8e00d9SKent Gibson 			if (lv->mask & BIT_ULL(i)) {
10737b8e00d9SKent Gibson 				descs[didx] = lr->lines[i].desc;
10747b8e00d9SKent Gibson 				didx++;
10757b8e00d9SKent Gibson 			}
10767b8e00d9SKent Gibson 		}
10777b8e00d9SKent Gibson 	}
10787b8e00d9SKent Gibson 	ret = gpiod_set_array_value_complex(false, true, num_set,
10797b8e00d9SKent Gibson 					    descs, NULL, vals);
10807b8e00d9SKent Gibson 
10817b8e00d9SKent Gibson 	if (num_set != 1)
10827b8e00d9SKent Gibson 		kfree(descs);
10837b8e00d9SKent Gibson 	return ret;
10847b8e00d9SKent Gibson }
10857b8e00d9SKent Gibson 
10867b8e00d9SKent Gibson static long linereq_set_values(struct linereq *lr, void __user *ip)
10877b8e00d9SKent Gibson {
10887b8e00d9SKent Gibson 	struct gpio_v2_line_values lv;
10897b8e00d9SKent Gibson 	int ret;
10907b8e00d9SKent Gibson 
10917b8e00d9SKent Gibson 	if (copy_from_user(&lv, ip, sizeof(lv)))
10927b8e00d9SKent Gibson 		return -EFAULT;
10937b8e00d9SKent Gibson 
10947b8e00d9SKent Gibson 	mutex_lock(&lr->config_mutex);
10957b8e00d9SKent Gibson 
10967b8e00d9SKent Gibson 	ret = linereq_set_values_unlocked(lr, &lv);
10977b8e00d9SKent Gibson 
10987b8e00d9SKent Gibson 	mutex_unlock(&lr->config_mutex);
10997b8e00d9SKent Gibson 
11007b8e00d9SKent Gibson 	return ret;
11017b8e00d9SKent Gibson }
11027b8e00d9SKent Gibson 
1103a54756cbSKent Gibson static long linereq_set_config_unlocked(struct linereq *lr,
1104a54756cbSKent Gibson 					struct gpio_v2_line_config *lc)
1105a54756cbSKent Gibson {
1106a54756cbSKent Gibson 	struct gpio_desc *desc;
1107a54756cbSKent Gibson 	unsigned int i;
1108a54756cbSKent Gibson 	u64 flags;
1109a54756cbSKent Gibson 	bool polarity_change;
1110a54756cbSKent Gibson 	int ret;
1111a54756cbSKent Gibson 
1112a54756cbSKent Gibson 	for (i = 0; i < lr->num_lines; i++) {
1113a54756cbSKent Gibson 		desc = lr->lines[i].desc;
1114a54756cbSKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
1115a54756cbSKent Gibson 		polarity_change =
1116a54756cbSKent Gibson 			(!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) !=
1117a54756cbSKent Gibson 			 ((flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) != 0));
1118a54756cbSKent Gibson 
1119a54756cbSKent Gibson 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1120a54756cbSKent Gibson 		/*
1121a54756cbSKent Gibson 		 * Lines have to be requested explicitly for input
1122a54756cbSKent Gibson 		 * or output, else the line will be treated "as is".
1123a54756cbSKent Gibson 		 */
1124a54756cbSKent Gibson 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1125a54756cbSKent Gibson 			int val = gpio_v2_line_config_output_value(lc, i);
1126a54756cbSKent Gibson 
1127a54756cbSKent Gibson 			edge_detector_stop(&lr->lines[i]);
1128a54756cbSKent Gibson 			ret = gpiod_direction_output(desc, val);
1129a54756cbSKent Gibson 			if (ret)
1130a54756cbSKent Gibson 				return ret;
1131a54756cbSKent Gibson 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1132a54756cbSKent Gibson 			ret = gpiod_direction_input(desc);
1133a54756cbSKent Gibson 			if (ret)
1134a54756cbSKent Gibson 				return ret;
1135a54756cbSKent Gibson 
113665cff704SKent Gibson 			ret = edge_detector_update(&lr->lines[i], lc, i,
1137a54756cbSKent Gibson 					flags & GPIO_V2_LINE_EDGE_FLAGS,
1138a54756cbSKent Gibson 					polarity_change);
1139a54756cbSKent Gibson 			if (ret)
1140a54756cbSKent Gibson 				return ret;
1141a54756cbSKent Gibson 		}
1142a54756cbSKent Gibson 
1143a54756cbSKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
1144a54756cbSKent Gibson 					     GPIO_V2_LINE_CHANGED_CONFIG,
1145a54756cbSKent Gibson 					     desc);
1146a54756cbSKent Gibson 	}
1147a54756cbSKent Gibson 	return 0;
1148a54756cbSKent Gibson }
1149a54756cbSKent Gibson 
1150a54756cbSKent Gibson static long linereq_set_config(struct linereq *lr, void __user *ip)
1151a54756cbSKent Gibson {
1152a54756cbSKent Gibson 	struct gpio_v2_line_config lc;
1153a54756cbSKent Gibson 	int ret;
1154a54756cbSKent Gibson 
1155a54756cbSKent Gibson 	if (copy_from_user(&lc, ip, sizeof(lc)))
1156a54756cbSKent Gibson 		return -EFAULT;
1157a54756cbSKent Gibson 
1158a54756cbSKent Gibson 	ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
1159a54756cbSKent Gibson 	if (ret)
1160a54756cbSKent Gibson 		return ret;
1161a54756cbSKent Gibson 
1162a54756cbSKent Gibson 	mutex_lock(&lr->config_mutex);
1163a54756cbSKent Gibson 
1164a54756cbSKent Gibson 	ret = linereq_set_config_unlocked(lr, &lc);
1165a54756cbSKent Gibson 
1166a54756cbSKent Gibson 	mutex_unlock(&lr->config_mutex);
1167a54756cbSKent Gibson 
1168a54756cbSKent Gibson 	return ret;
1169a54756cbSKent Gibson }
1170a54756cbSKent Gibson 
11713c0d9c63SKent Gibson static long linereq_ioctl(struct file *file, unsigned int cmd,
11723c0d9c63SKent Gibson 			  unsigned long arg)
11733c0d9c63SKent Gibson {
11743c0d9c63SKent Gibson 	struct linereq *lr = file->private_data;
11753c0d9c63SKent Gibson 	void __user *ip = (void __user *)arg;
11763c0d9c63SKent Gibson 
11773c0d9c63SKent Gibson 	if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL)
11783c0d9c63SKent Gibson 		return linereq_get_values(lr, ip);
11797b8e00d9SKent Gibson 	else if (cmd == GPIO_V2_LINE_SET_VALUES_IOCTL)
11807b8e00d9SKent Gibson 		return linereq_set_values(lr, ip);
1181a54756cbSKent Gibson 	else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL)
1182a54756cbSKent Gibson 		return linereq_set_config(lr, ip);
11833c0d9c63SKent Gibson 
11843c0d9c63SKent Gibson 	return -EINVAL;
11853c0d9c63SKent Gibson }
11863c0d9c63SKent Gibson 
11873c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
11883c0d9c63SKent Gibson static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
11893c0d9c63SKent Gibson 				 unsigned long arg)
11903c0d9c63SKent Gibson {
11913c0d9c63SKent Gibson 	return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
11923c0d9c63SKent Gibson }
11933c0d9c63SKent Gibson #endif
11943c0d9c63SKent Gibson 
119573e03419SKent Gibson static __poll_t linereq_poll(struct file *file,
119673e03419SKent Gibson 			    struct poll_table_struct *wait)
119773e03419SKent Gibson {
119873e03419SKent Gibson 	struct linereq *lr = file->private_data;
119973e03419SKent Gibson 	__poll_t events = 0;
120073e03419SKent Gibson 
120173e03419SKent Gibson 	poll_wait(file, &lr->wait, wait);
120273e03419SKent Gibson 
120373e03419SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
120473e03419SKent Gibson 						 &lr->wait.lock))
120573e03419SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
120673e03419SKent Gibson 
120773e03419SKent Gibson 	return events;
120873e03419SKent Gibson }
120973e03419SKent Gibson 
121073e03419SKent Gibson static ssize_t linereq_read(struct file *file,
121173e03419SKent Gibson 			    char __user *buf,
121273e03419SKent Gibson 			    size_t count,
121373e03419SKent Gibson 			    loff_t *f_ps)
121473e03419SKent Gibson {
121573e03419SKent Gibson 	struct linereq *lr = file->private_data;
121673e03419SKent Gibson 	struct gpio_v2_line_event le;
121773e03419SKent Gibson 	ssize_t bytes_read = 0;
121873e03419SKent Gibson 	int ret;
121973e03419SKent Gibson 
122073e03419SKent Gibson 	if (count < sizeof(le))
122173e03419SKent Gibson 		return -EINVAL;
122273e03419SKent Gibson 
122373e03419SKent Gibson 	do {
122473e03419SKent Gibson 		spin_lock(&lr->wait.lock);
122573e03419SKent Gibson 		if (kfifo_is_empty(&lr->events)) {
122673e03419SKent Gibson 			if (bytes_read) {
122773e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
122873e03419SKent Gibson 				return bytes_read;
122973e03419SKent Gibson 			}
123073e03419SKent Gibson 
123173e03419SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
123273e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
123373e03419SKent Gibson 				return -EAGAIN;
123473e03419SKent Gibson 			}
123573e03419SKent Gibson 
123673e03419SKent Gibson 			ret = wait_event_interruptible_locked(lr->wait,
123773e03419SKent Gibson 					!kfifo_is_empty(&lr->events));
123873e03419SKent Gibson 			if (ret) {
123973e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
124073e03419SKent Gibson 				return ret;
124173e03419SKent Gibson 			}
124273e03419SKent Gibson 		}
124373e03419SKent Gibson 
124473e03419SKent Gibson 		ret = kfifo_out(&lr->events, &le, 1);
124573e03419SKent Gibson 		spin_unlock(&lr->wait.lock);
124673e03419SKent Gibson 		if (ret != 1) {
124773e03419SKent Gibson 			/*
124873e03419SKent Gibson 			 * This should never happen - we were holding the
124973e03419SKent Gibson 			 * lock from the moment we learned the fifo is no
125073e03419SKent Gibson 			 * longer empty until now.
125173e03419SKent Gibson 			 */
125273e03419SKent Gibson 			ret = -EIO;
125373e03419SKent Gibson 			break;
125473e03419SKent Gibson 		}
125573e03419SKent Gibson 
125673e03419SKent Gibson 		if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
125773e03419SKent Gibson 			return -EFAULT;
125873e03419SKent Gibson 		bytes_read += sizeof(le);
125973e03419SKent Gibson 	} while (count >= bytes_read + sizeof(le));
126073e03419SKent Gibson 
126173e03419SKent Gibson 	return bytes_read;
126273e03419SKent Gibson }
126373e03419SKent Gibson 
12643c0d9c63SKent Gibson static void linereq_free(struct linereq *lr)
12653c0d9c63SKent Gibson {
12663c0d9c63SKent Gibson 	unsigned int i;
12673c0d9c63SKent Gibson 
12683c0d9c63SKent Gibson 	for (i = 0; i < lr->num_lines; i++) {
126973e03419SKent Gibson 		edge_detector_stop(&lr->lines[i]);
12703c0d9c63SKent Gibson 		if (lr->lines[i].desc)
12713c0d9c63SKent Gibson 			gpiod_free(lr->lines[i].desc);
12723c0d9c63SKent Gibson 	}
127373e03419SKent Gibson 	kfifo_free(&lr->events);
12743c0d9c63SKent Gibson 	kfree(lr->label);
12753c0d9c63SKent Gibson 	put_device(&lr->gdev->dev);
12763c0d9c63SKent Gibson 	kfree(lr);
12773c0d9c63SKent Gibson }
12783c0d9c63SKent Gibson 
12793c0d9c63SKent Gibson static int linereq_release(struct inode *inode, struct file *file)
12803c0d9c63SKent Gibson {
12813c0d9c63SKent Gibson 	struct linereq *lr = file->private_data;
12823c0d9c63SKent Gibson 
12833c0d9c63SKent Gibson 	linereq_free(lr);
12843c0d9c63SKent Gibson 	return 0;
12853c0d9c63SKent Gibson }
12863c0d9c63SKent Gibson 
12873c0d9c63SKent Gibson static const struct file_operations line_fileops = {
12883c0d9c63SKent Gibson 	.release = linereq_release,
128973e03419SKent Gibson 	.read = linereq_read,
129073e03419SKent Gibson 	.poll = linereq_poll,
12913c0d9c63SKent Gibson 	.owner = THIS_MODULE,
12923c0d9c63SKent Gibson 	.llseek = noop_llseek,
12933c0d9c63SKent Gibson 	.unlocked_ioctl = linereq_ioctl,
12943c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
12953c0d9c63SKent Gibson 	.compat_ioctl = linereq_ioctl_compat,
12963c0d9c63SKent Gibson #endif
12973c0d9c63SKent Gibson };
12983c0d9c63SKent Gibson 
12993c0d9c63SKent Gibson static int linereq_create(struct gpio_device *gdev, void __user *ip)
13003c0d9c63SKent Gibson {
13013c0d9c63SKent Gibson 	struct gpio_v2_line_request ulr;
13023c0d9c63SKent Gibson 	struct gpio_v2_line_config *lc;
13033c0d9c63SKent Gibson 	struct linereq *lr;
13043c0d9c63SKent Gibson 	struct file *file;
13053c0d9c63SKent Gibson 	u64 flags;
13063c0d9c63SKent Gibson 	unsigned int i;
13073c0d9c63SKent Gibson 	int fd, ret;
13083c0d9c63SKent Gibson 
13093c0d9c63SKent Gibson 	if (copy_from_user(&ulr, ip, sizeof(ulr)))
13103c0d9c63SKent Gibson 		return -EFAULT;
13113c0d9c63SKent Gibson 
13123c0d9c63SKent Gibson 	if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
13133c0d9c63SKent Gibson 		return -EINVAL;
13143c0d9c63SKent Gibson 
13153c0d9c63SKent Gibson 	if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
13163c0d9c63SKent Gibson 		return -EINVAL;
13173c0d9c63SKent Gibson 
13183c0d9c63SKent Gibson 	lc = &ulr.config;
13193c0d9c63SKent Gibson 	ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
13203c0d9c63SKent Gibson 	if (ret)
13213c0d9c63SKent Gibson 		return ret;
13223c0d9c63SKent Gibson 
13233c0d9c63SKent Gibson 	lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
13243c0d9c63SKent Gibson 	if (!lr)
13253c0d9c63SKent Gibson 		return -ENOMEM;
13263c0d9c63SKent Gibson 
13273c0d9c63SKent Gibson 	lr->gdev = gdev;
13283c0d9c63SKent Gibson 	get_device(&gdev->dev);
13293c0d9c63SKent Gibson 
133065cff704SKent Gibson 	for (i = 0; i < ulr.num_lines; i++) {
133173e03419SKent Gibson 		lr->lines[i].req = lr;
133265cff704SKent Gibson 		WRITE_ONCE(lr->lines[i].sw_debounced, 0);
133365cff704SKent Gibson 		INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
133465cff704SKent Gibson 	}
133573e03419SKent Gibson 
1336f188ac12SKent Gibson 	if (ulr.consumer[0] != '\0') {
13373c0d9c63SKent Gibson 		/* label is only initialized if consumer is set */
1338f188ac12SKent Gibson 		lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1339f188ac12SKent Gibson 				     GFP_KERNEL);
13403c0d9c63SKent Gibson 		if (!lr->label) {
13413c0d9c63SKent Gibson 			ret = -ENOMEM;
13423c0d9c63SKent Gibson 			goto out_free_linereq;
13433c0d9c63SKent Gibson 		}
13443c0d9c63SKent Gibson 	}
13453c0d9c63SKent Gibson 
1346a54756cbSKent Gibson 	mutex_init(&lr->config_mutex);
134773e03419SKent Gibson 	init_waitqueue_head(&lr->wait);
134873e03419SKent Gibson 	lr->event_buffer_size = ulr.event_buffer_size;
134973e03419SKent Gibson 	if (lr->event_buffer_size == 0)
135073e03419SKent Gibson 		lr->event_buffer_size = ulr.num_lines * 16;
135173e03419SKent Gibson 	else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
135273e03419SKent Gibson 		lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
135373e03419SKent Gibson 
135473e03419SKent Gibson 	atomic_set(&lr->seqno, 0);
13553c0d9c63SKent Gibson 	lr->num_lines = ulr.num_lines;
13563c0d9c63SKent Gibson 
13573c0d9c63SKent Gibson 	/* Request each GPIO */
13583c0d9c63SKent Gibson 	for (i = 0; i < ulr.num_lines; i++) {
13593c0d9c63SKent Gibson 		u32 offset = ulr.offsets[i];
13603c0d9c63SKent Gibson 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
13613c0d9c63SKent Gibson 
13623c0d9c63SKent Gibson 		if (IS_ERR(desc)) {
13633c0d9c63SKent Gibson 			ret = PTR_ERR(desc);
13643c0d9c63SKent Gibson 			goto out_free_linereq;
13653c0d9c63SKent Gibson 		}
13663c0d9c63SKent Gibson 
13673c0d9c63SKent Gibson 		ret = gpiod_request(desc, lr->label);
13683c0d9c63SKent Gibson 		if (ret)
13693c0d9c63SKent Gibson 			goto out_free_linereq;
13703c0d9c63SKent Gibson 
13713c0d9c63SKent Gibson 		lr->lines[i].desc = desc;
13723c0d9c63SKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
13733c0d9c63SKent Gibson 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
13743c0d9c63SKent Gibson 
13753c0d9c63SKent Gibson 		ret = gpiod_set_transitory(desc, false);
13763c0d9c63SKent Gibson 		if (ret < 0)
13773c0d9c63SKent Gibson 			goto out_free_linereq;
13783c0d9c63SKent Gibson 
13793c0d9c63SKent Gibson 		/*
13803c0d9c63SKent Gibson 		 * Lines have to be requested explicitly for input
13813c0d9c63SKent Gibson 		 * or output, else the line will be treated "as is".
13823c0d9c63SKent Gibson 		 */
13833c0d9c63SKent Gibson 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
13843c0d9c63SKent Gibson 			int val = gpio_v2_line_config_output_value(lc, i);
13853c0d9c63SKent Gibson 
13863c0d9c63SKent Gibson 			ret = gpiod_direction_output(desc, val);
13873c0d9c63SKent Gibson 			if (ret)
13883c0d9c63SKent Gibson 				goto out_free_linereq;
13893c0d9c63SKent Gibson 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
13903c0d9c63SKent Gibson 			ret = gpiod_direction_input(desc);
13913c0d9c63SKent Gibson 			if (ret)
13923c0d9c63SKent Gibson 				goto out_free_linereq;
139373e03419SKent Gibson 
139465cff704SKent Gibson 			ret = edge_detector_setup(&lr->lines[i], lc, i,
139573e03419SKent Gibson 					flags & GPIO_V2_LINE_EDGE_FLAGS);
139673e03419SKent Gibson 			if (ret)
139773e03419SKent Gibson 				goto out_free_linereq;
13983c0d9c63SKent Gibson 		}
13993c0d9c63SKent Gibson 
14003c0d9c63SKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
1401aad95584SKent Gibson 					     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
14023c0d9c63SKent Gibson 
14033c0d9c63SKent Gibson 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
14043c0d9c63SKent Gibson 			offset);
14053c0d9c63SKent Gibson 	}
14063c0d9c63SKent Gibson 
14073c0d9c63SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
14083c0d9c63SKent Gibson 	if (fd < 0) {
14093c0d9c63SKent Gibson 		ret = fd;
14103c0d9c63SKent Gibson 		goto out_free_linereq;
14113c0d9c63SKent Gibson 	}
14123c0d9c63SKent Gibson 
14133c0d9c63SKent Gibson 	file = anon_inode_getfile("gpio-line", &line_fileops, lr,
14143c0d9c63SKent Gibson 				  O_RDONLY | O_CLOEXEC);
14153c0d9c63SKent Gibson 	if (IS_ERR(file)) {
14163c0d9c63SKent Gibson 		ret = PTR_ERR(file);
14173c0d9c63SKent Gibson 		goto out_put_unused_fd;
14183c0d9c63SKent Gibson 	}
14193c0d9c63SKent Gibson 
14203c0d9c63SKent Gibson 	ulr.fd = fd;
14213c0d9c63SKent Gibson 	if (copy_to_user(ip, &ulr, sizeof(ulr))) {
14223c0d9c63SKent Gibson 		/*
14233c0d9c63SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
14243c0d9c63SKent Gibson 		 * the regular error cleanup path here.
14253c0d9c63SKent Gibson 		 */
14263c0d9c63SKent Gibson 		fput(file);
14273c0d9c63SKent Gibson 		put_unused_fd(fd);
14283c0d9c63SKent Gibson 		return -EFAULT;
14293c0d9c63SKent Gibson 	}
14303c0d9c63SKent Gibson 
14313c0d9c63SKent Gibson 	fd_install(fd, file);
14323c0d9c63SKent Gibson 
14333c0d9c63SKent Gibson 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
14343c0d9c63SKent Gibson 		lr->num_lines);
14353c0d9c63SKent Gibson 
14363c0d9c63SKent Gibson 	return 0;
14373c0d9c63SKent Gibson 
14383c0d9c63SKent Gibson out_put_unused_fd:
14393c0d9c63SKent Gibson 	put_unused_fd(fd);
14403c0d9c63SKent Gibson out_free_linereq:
14413c0d9c63SKent Gibson 	linereq_free(lr);
14423c0d9c63SKent Gibson 	return ret;
14433c0d9c63SKent Gibson }
14443c0d9c63SKent Gibson 
14453c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1446925ca369SKent Gibson 
1447925ca369SKent Gibson /*
1448925ca369SKent Gibson  * GPIO line event management
1449925ca369SKent Gibson  */
1450925ca369SKent Gibson 
1451925ca369SKent Gibson /**
1452925ca369SKent Gibson  * struct lineevent_state - contains the state of a userspace event
1453925ca369SKent Gibson  * @gdev: the GPIO device the event pertains to
1454925ca369SKent Gibson  * @label: consumer label used to tag descriptors
1455925ca369SKent Gibson  * @desc: the GPIO descriptor held by this event
1456925ca369SKent Gibson  * @eflags: the event flags this line was requested with
1457925ca369SKent Gibson  * @irq: the interrupt that trigger in response to events on this GPIO
1458925ca369SKent Gibson  * @wait: wait queue that handles blocking reads of events
1459925ca369SKent Gibson  * @events: KFIFO for the GPIO events
1460925ca369SKent Gibson  * @timestamp: cache for the timestamp storing it between hardirq
1461925ca369SKent Gibson  * and IRQ thread, used to bring the timestamp close to the actual
1462925ca369SKent Gibson  * event
1463925ca369SKent Gibson  */
1464925ca369SKent Gibson struct lineevent_state {
1465925ca369SKent Gibson 	struct gpio_device *gdev;
1466925ca369SKent Gibson 	const char *label;
1467925ca369SKent Gibson 	struct gpio_desc *desc;
1468925ca369SKent Gibson 	u32 eflags;
1469925ca369SKent Gibson 	int irq;
1470925ca369SKent Gibson 	wait_queue_head_t wait;
1471925ca369SKent Gibson 	DECLARE_KFIFO(events, struct gpioevent_data, 16);
1472925ca369SKent Gibson 	u64 timestamp;
1473925ca369SKent Gibson };
1474925ca369SKent Gibson 
1475925ca369SKent Gibson #define GPIOEVENT_REQUEST_VALID_FLAGS \
1476925ca369SKent Gibson 	(GPIOEVENT_REQUEST_RISING_EDGE | \
1477925ca369SKent Gibson 	GPIOEVENT_REQUEST_FALLING_EDGE)
1478925ca369SKent Gibson 
147949bc5279SKent Gibson static __poll_t lineevent_poll(struct file *file,
1480925ca369SKent Gibson 			       struct poll_table_struct *wait)
1481925ca369SKent Gibson {
148249bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1483925ca369SKent Gibson 	__poll_t events = 0;
1484925ca369SKent Gibson 
148549bc5279SKent Gibson 	poll_wait(file, &le->wait, wait);
1486925ca369SKent Gibson 
1487925ca369SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1488925ca369SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
1489925ca369SKent Gibson 
1490925ca369SKent Gibson 	return events;
1491925ca369SKent Gibson }
1492925ca369SKent Gibson 
14935ad284abSAndy Shevchenko static ssize_t lineevent_get_size(void)
14945ad284abSAndy Shevchenko {
149547e538d8SAndy Shevchenko #if defined(CONFIG_X86_64) && !defined(CONFIG_UML)
14965ad284abSAndy Shevchenko 	/* i386 has no padding after 'id' */
14975ad284abSAndy Shevchenko 	if (in_ia32_syscall()) {
14985ad284abSAndy Shevchenko 		struct compat_gpioeevent_data {
14995ad284abSAndy Shevchenko 			compat_u64	timestamp;
15005ad284abSAndy Shevchenko 			u32		id;
15015ad284abSAndy Shevchenko 		};
15025ad284abSAndy Shevchenko 
15035ad284abSAndy Shevchenko 		return sizeof(struct compat_gpioeevent_data);
15045ad284abSAndy Shevchenko 	}
15055ad284abSAndy Shevchenko #endif
15065ad284abSAndy Shevchenko 	return sizeof(struct gpioevent_data);
15075ad284abSAndy Shevchenko }
1508925ca369SKent Gibson 
150949bc5279SKent Gibson static ssize_t lineevent_read(struct file *file,
1510925ca369SKent Gibson 			      char __user *buf,
1511925ca369SKent Gibson 			      size_t count,
1512925ca369SKent Gibson 			      loff_t *f_ps)
1513925ca369SKent Gibson {
151449bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1515925ca369SKent Gibson 	struct gpioevent_data ge;
1516925ca369SKent Gibson 	ssize_t bytes_read = 0;
15175ad284abSAndy Shevchenko 	ssize_t ge_size;
1518925ca369SKent Gibson 	int ret;
1519925ca369SKent Gibson 
15205ad284abSAndy Shevchenko 	/*
15215ad284abSAndy Shevchenko 	 * When compatible system call is being used the struct gpioevent_data,
15225ad284abSAndy Shevchenko 	 * in case of at least ia32, has different size due to the alignment
15235ad284abSAndy Shevchenko 	 * differences. Because we have first member 64 bits followed by one of
15245ad284abSAndy Shevchenko 	 * 32 bits there is no gap between them. The only difference is the
15255ad284abSAndy Shevchenko 	 * padding at the end of the data structure. Hence, we calculate the
15265ad284abSAndy Shevchenko 	 * actual sizeof() and pass this as an argument to copy_to_user() to
15275ad284abSAndy Shevchenko 	 * drop unneeded bytes from the output.
15285ad284abSAndy Shevchenko 	 */
15295ad284abSAndy Shevchenko 	ge_size = lineevent_get_size();
15305ad284abSAndy Shevchenko 	if (count < ge_size)
1531925ca369SKent Gibson 		return -EINVAL;
1532925ca369SKent Gibson 
1533925ca369SKent Gibson 	do {
1534925ca369SKent Gibson 		spin_lock(&le->wait.lock);
1535925ca369SKent Gibson 		if (kfifo_is_empty(&le->events)) {
1536925ca369SKent Gibson 			if (bytes_read) {
1537925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1538925ca369SKent Gibson 				return bytes_read;
1539925ca369SKent Gibson 			}
1540925ca369SKent Gibson 
154149bc5279SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
1542925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1543925ca369SKent Gibson 				return -EAGAIN;
1544925ca369SKent Gibson 			}
1545925ca369SKent Gibson 
1546925ca369SKent Gibson 			ret = wait_event_interruptible_locked(le->wait,
1547925ca369SKent Gibson 					!kfifo_is_empty(&le->events));
1548925ca369SKent Gibson 			if (ret) {
1549925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1550925ca369SKent Gibson 				return ret;
1551925ca369SKent Gibson 			}
1552925ca369SKent Gibson 		}
1553925ca369SKent Gibson 
1554925ca369SKent Gibson 		ret = kfifo_out(&le->events, &ge, 1);
1555925ca369SKent Gibson 		spin_unlock(&le->wait.lock);
1556925ca369SKent Gibson 		if (ret != 1) {
1557925ca369SKent Gibson 			/*
1558925ca369SKent Gibson 			 * This should never happen - we were holding the lock
1559925ca369SKent Gibson 			 * from the moment we learned the fifo is no longer
1560925ca369SKent Gibson 			 * empty until now.
1561925ca369SKent Gibson 			 */
1562925ca369SKent Gibson 			ret = -EIO;
1563925ca369SKent Gibson 			break;
1564925ca369SKent Gibson 		}
1565925ca369SKent Gibson 
15665ad284abSAndy Shevchenko 		if (copy_to_user(buf + bytes_read, &ge, ge_size))
1567925ca369SKent Gibson 			return -EFAULT;
15685ad284abSAndy Shevchenko 		bytes_read += ge_size;
15695ad284abSAndy Shevchenko 	} while (count >= bytes_read + ge_size);
1570925ca369SKent Gibson 
1571925ca369SKent Gibson 	return bytes_read;
1572925ca369SKent Gibson }
1573925ca369SKent Gibson 
157446824272SKent Gibson static void lineevent_free(struct lineevent_state *le)
1575925ca369SKent Gibson {
157646824272SKent Gibson 	if (le->irq)
1577925ca369SKent Gibson 		free_irq(le->irq, le);
157846824272SKent Gibson 	if (le->desc)
1579925ca369SKent Gibson 		gpiod_free(le->desc);
1580925ca369SKent Gibson 	kfree(le->label);
158146824272SKent Gibson 	put_device(&le->gdev->dev);
1582925ca369SKent Gibson 	kfree(le);
158346824272SKent Gibson }
158446824272SKent Gibson 
158546824272SKent Gibson static int lineevent_release(struct inode *inode, struct file *file)
158646824272SKent Gibson {
158746824272SKent Gibson 	lineevent_free(file->private_data);
1588925ca369SKent Gibson 	return 0;
1589925ca369SKent Gibson }
1590925ca369SKent Gibson 
159149bc5279SKent Gibson static long lineevent_ioctl(struct file *file, unsigned int cmd,
1592925ca369SKent Gibson 			    unsigned long arg)
1593925ca369SKent Gibson {
159449bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1595925ca369SKent Gibson 	void __user *ip = (void __user *)arg;
1596925ca369SKent Gibson 	struct gpiohandle_data ghd;
1597925ca369SKent Gibson 
1598925ca369SKent Gibson 	/*
1599925ca369SKent Gibson 	 * We can get the value for an event line but not set it,
1600925ca369SKent Gibson 	 * because it is input by definition.
1601925ca369SKent Gibson 	 */
1602925ca369SKent Gibson 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
1603925ca369SKent Gibson 		int val;
1604925ca369SKent Gibson 
1605925ca369SKent Gibson 		memset(&ghd, 0, sizeof(ghd));
1606925ca369SKent Gibson 
1607925ca369SKent Gibson 		val = gpiod_get_value_cansleep(le->desc);
1608925ca369SKent Gibson 		if (val < 0)
1609925ca369SKent Gibson 			return val;
1610925ca369SKent Gibson 		ghd.values[0] = val;
1611925ca369SKent Gibson 
1612925ca369SKent Gibson 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
1613925ca369SKent Gibson 			return -EFAULT;
1614925ca369SKent Gibson 
1615925ca369SKent Gibson 		return 0;
1616925ca369SKent Gibson 	}
1617925ca369SKent Gibson 	return -EINVAL;
1618925ca369SKent Gibson }
1619925ca369SKent Gibson 
1620925ca369SKent Gibson #ifdef CONFIG_COMPAT
162149bc5279SKent Gibson static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
1622925ca369SKent Gibson 				   unsigned long arg)
1623925ca369SKent Gibson {
162449bc5279SKent Gibson 	return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1625925ca369SKent Gibson }
1626925ca369SKent Gibson #endif
1627925ca369SKent Gibson 
1628925ca369SKent Gibson static const struct file_operations lineevent_fileops = {
1629925ca369SKent Gibson 	.release = lineevent_release,
1630925ca369SKent Gibson 	.read = lineevent_read,
1631925ca369SKent Gibson 	.poll = lineevent_poll,
1632925ca369SKent Gibson 	.owner = THIS_MODULE,
1633925ca369SKent Gibson 	.llseek = noop_llseek,
1634925ca369SKent Gibson 	.unlocked_ioctl = lineevent_ioctl,
1635925ca369SKent Gibson #ifdef CONFIG_COMPAT
1636925ca369SKent Gibson 	.compat_ioctl = lineevent_ioctl_compat,
1637925ca369SKent Gibson #endif
1638925ca369SKent Gibson };
1639925ca369SKent Gibson 
1640925ca369SKent Gibson static irqreturn_t lineevent_irq_thread(int irq, void *p)
1641925ca369SKent Gibson {
1642925ca369SKent Gibson 	struct lineevent_state *le = p;
1643925ca369SKent Gibson 	struct gpioevent_data ge;
1644925ca369SKent Gibson 	int ret;
1645925ca369SKent Gibson 
1646925ca369SKent Gibson 	/* Do not leak kernel stack to userspace */
1647925ca369SKent Gibson 	memset(&ge, 0, sizeof(ge));
1648925ca369SKent Gibson 
1649925ca369SKent Gibson 	/*
1650925ca369SKent Gibson 	 * We may be running from a nested threaded interrupt in which case
1651925ca369SKent Gibson 	 * we didn't get the timestamp from lineevent_irq_handler().
1652925ca369SKent Gibson 	 */
1653925ca369SKent Gibson 	if (!le->timestamp)
1654925ca369SKent Gibson 		ge.timestamp = ktime_get_ns();
1655925ca369SKent Gibson 	else
1656925ca369SKent Gibson 		ge.timestamp = le->timestamp;
1657925ca369SKent Gibson 
1658925ca369SKent Gibson 	if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
1659925ca369SKent Gibson 	    && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1660925ca369SKent Gibson 		int level = gpiod_get_value_cansleep(le->desc);
1661925ca369SKent Gibson 
1662925ca369SKent Gibson 		if (level)
1663925ca369SKent Gibson 			/* Emit low-to-high event */
1664925ca369SKent Gibson 			ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1665925ca369SKent Gibson 		else
1666925ca369SKent Gibson 			/* Emit high-to-low event */
1667925ca369SKent Gibson 			ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1668925ca369SKent Gibson 	} else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
1669925ca369SKent Gibson 		/* Emit low-to-high event */
1670925ca369SKent Gibson 		ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1671925ca369SKent Gibson 	} else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1672925ca369SKent Gibson 		/* Emit high-to-low event */
1673925ca369SKent Gibson 		ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1674925ca369SKent Gibson 	} else {
1675925ca369SKent Gibson 		return IRQ_NONE;
1676925ca369SKent Gibson 	}
1677925ca369SKent Gibson 
1678925ca369SKent Gibson 	ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
1679925ca369SKent Gibson 					    1, &le->wait.lock);
1680925ca369SKent Gibson 	if (ret)
1681925ca369SKent Gibson 		wake_up_poll(&le->wait, EPOLLIN);
1682925ca369SKent Gibson 	else
1683925ca369SKent Gibson 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
1684925ca369SKent Gibson 
1685925ca369SKent Gibson 	return IRQ_HANDLED;
1686925ca369SKent Gibson }
1687925ca369SKent Gibson 
1688925ca369SKent Gibson static irqreturn_t lineevent_irq_handler(int irq, void *p)
1689925ca369SKent Gibson {
1690925ca369SKent Gibson 	struct lineevent_state *le = p;
1691925ca369SKent Gibson 
1692925ca369SKent Gibson 	/*
1693925ca369SKent Gibson 	 * Just store the timestamp in hardirq context so we get it as
1694925ca369SKent Gibson 	 * close in time as possible to the actual event.
1695925ca369SKent Gibson 	 */
1696925ca369SKent Gibson 	le->timestamp = ktime_get_ns();
1697925ca369SKent Gibson 
1698925ca369SKent Gibson 	return IRQ_WAKE_THREAD;
1699925ca369SKent Gibson }
1700925ca369SKent Gibson 
1701925ca369SKent Gibson static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1702925ca369SKent Gibson {
1703925ca369SKent Gibson 	struct gpioevent_request eventreq;
1704925ca369SKent Gibson 	struct lineevent_state *le;
1705925ca369SKent Gibson 	struct gpio_desc *desc;
1706925ca369SKent Gibson 	struct file *file;
1707925ca369SKent Gibson 	u32 offset;
1708925ca369SKent Gibson 	u32 lflags;
1709925ca369SKent Gibson 	u32 eflags;
1710925ca369SKent Gibson 	int fd;
1711925ca369SKent Gibson 	int ret;
171246824272SKent Gibson 	int irq, irqflags = 0;
1713925ca369SKent Gibson 
1714925ca369SKent Gibson 	if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1715925ca369SKent Gibson 		return -EFAULT;
1716925ca369SKent Gibson 
1717925ca369SKent Gibson 	offset = eventreq.lineoffset;
1718925ca369SKent Gibson 	lflags = eventreq.handleflags;
1719925ca369SKent Gibson 	eflags = eventreq.eventflags;
1720925ca369SKent Gibson 
1721925ca369SKent Gibson 	desc = gpiochip_get_desc(gdev->chip, offset);
1722925ca369SKent Gibson 	if (IS_ERR(desc))
1723925ca369SKent Gibson 		return PTR_ERR(desc);
1724925ca369SKent Gibson 
1725925ca369SKent Gibson 	/* Return an error if a unknown flag is set */
1726925ca369SKent Gibson 	if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1727925ca369SKent Gibson 	    (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1728925ca369SKent Gibson 		return -EINVAL;
1729925ca369SKent Gibson 
1730925ca369SKent Gibson 	/* This is just wrong: we don't look for events on output lines */
1731925ca369SKent Gibson 	if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1732925ca369SKent Gibson 	    (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1733925ca369SKent Gibson 	    (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1734925ca369SKent Gibson 		return -EINVAL;
1735925ca369SKent Gibson 
1736925ca369SKent Gibson 	/* Only one bias flag can be set. */
1737925ca369SKent Gibson 	if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1738925ca369SKent Gibson 	     (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1739925ca369SKent Gibson 			GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1740925ca369SKent Gibson 	    ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1741925ca369SKent Gibson 	     (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1742925ca369SKent Gibson 		return -EINVAL;
1743925ca369SKent Gibson 
1744925ca369SKent Gibson 	le = kzalloc(sizeof(*le), GFP_KERNEL);
1745925ca369SKent Gibson 	if (!le)
1746925ca369SKent Gibson 		return -ENOMEM;
1747925ca369SKent Gibson 	le->gdev = gdev;
1748925ca369SKent Gibson 	get_device(&gdev->dev);
1749925ca369SKent Gibson 
1750f188ac12SKent Gibson 	if (eventreq.consumer_label[0] != '\0') {
1751f188ac12SKent Gibson 		/* label is only initialized if consumer_label is set */
1752f188ac12SKent Gibson 		le->label = kstrndup(eventreq.consumer_label,
1753f188ac12SKent Gibson 				     sizeof(eventreq.consumer_label) - 1,
1754925ca369SKent Gibson 				     GFP_KERNEL);
1755925ca369SKent Gibson 		if (!le->label) {
1756925ca369SKent Gibson 			ret = -ENOMEM;
1757925ca369SKent Gibson 			goto out_free_le;
1758925ca369SKent Gibson 		}
1759925ca369SKent Gibson 	}
1760925ca369SKent Gibson 
1761925ca369SKent Gibson 	ret = gpiod_request(desc, le->label);
1762925ca369SKent Gibson 	if (ret)
176346824272SKent Gibson 		goto out_free_le;
1764925ca369SKent Gibson 	le->desc = desc;
1765925ca369SKent Gibson 	le->eflags = eflags;
1766925ca369SKent Gibson 
1767c274b58aSKent Gibson 	linehandle_flags_to_desc_flags(lflags, &desc->flags);
1768925ca369SKent Gibson 
1769925ca369SKent Gibson 	ret = gpiod_direction_input(desc);
1770925ca369SKent Gibson 	if (ret)
177146824272SKent Gibson 		goto out_free_le;
1772925ca369SKent Gibson 
17736accc376SKent Gibson 	blocking_notifier_call_chain(&desc->gdev->notifier,
1774aad95584SKent Gibson 				     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1775925ca369SKent Gibson 
177646824272SKent Gibson 	irq = gpiod_to_irq(desc);
177746824272SKent Gibson 	if (irq <= 0) {
1778925ca369SKent Gibson 		ret = -ENODEV;
177946824272SKent Gibson 		goto out_free_le;
1780925ca369SKent Gibson 	}
178146824272SKent Gibson 	le->irq = irq;
1782925ca369SKent Gibson 
1783925ca369SKent Gibson 	if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
1784925ca369SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1785925ca369SKent Gibson 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1786925ca369SKent Gibson 	if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
1787925ca369SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1788925ca369SKent Gibson 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1789925ca369SKent Gibson 	irqflags |= IRQF_ONESHOT;
1790925ca369SKent Gibson 
1791925ca369SKent Gibson 	INIT_KFIFO(le->events);
1792925ca369SKent Gibson 	init_waitqueue_head(&le->wait);
1793925ca369SKent Gibson 
1794925ca369SKent Gibson 	/* Request a thread to read the events */
1795925ca369SKent Gibson 	ret = request_threaded_irq(le->irq,
1796925ca369SKent Gibson 				   lineevent_irq_handler,
1797925ca369SKent Gibson 				   lineevent_irq_thread,
1798925ca369SKent Gibson 				   irqflags,
1799925ca369SKent Gibson 				   le->label,
1800925ca369SKent Gibson 				   le);
1801925ca369SKent Gibson 	if (ret)
180246824272SKent Gibson 		goto out_free_le;
1803925ca369SKent Gibson 
1804925ca369SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1805925ca369SKent Gibson 	if (fd < 0) {
1806925ca369SKent Gibson 		ret = fd;
180746824272SKent Gibson 		goto out_free_le;
1808925ca369SKent Gibson 	}
1809925ca369SKent Gibson 
1810925ca369SKent Gibson 	file = anon_inode_getfile("gpio-event",
1811925ca369SKent Gibson 				  &lineevent_fileops,
1812925ca369SKent Gibson 				  le,
1813925ca369SKent Gibson 				  O_RDONLY | O_CLOEXEC);
1814925ca369SKent Gibson 	if (IS_ERR(file)) {
1815925ca369SKent Gibson 		ret = PTR_ERR(file);
1816925ca369SKent Gibson 		goto out_put_unused_fd;
1817925ca369SKent Gibson 	}
1818925ca369SKent Gibson 
1819925ca369SKent Gibson 	eventreq.fd = fd;
1820925ca369SKent Gibson 	if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
1821925ca369SKent Gibson 		/*
1822925ca369SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
1823925ca369SKent Gibson 		 * the regular error cleanup path here.
1824925ca369SKent Gibson 		 */
1825925ca369SKent Gibson 		fput(file);
1826925ca369SKent Gibson 		put_unused_fd(fd);
1827925ca369SKent Gibson 		return -EFAULT;
1828925ca369SKent Gibson 	}
1829925ca369SKent Gibson 
1830925ca369SKent Gibson 	fd_install(fd, file);
1831925ca369SKent Gibson 
1832925ca369SKent Gibson 	return 0;
1833925ca369SKent Gibson 
1834925ca369SKent Gibson out_put_unused_fd:
1835925ca369SKent Gibson 	put_unused_fd(fd);
1836925ca369SKent Gibson out_free_le:
183746824272SKent Gibson 	lineevent_free(le);
1838925ca369SKent Gibson 	return ret;
1839925ca369SKent Gibson }
1840925ca369SKent Gibson 
1841aad95584SKent Gibson static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
1842aad95584SKent Gibson 				    struct gpioline_info *info_v1)
1843aad95584SKent Gibson {
1844aad95584SKent Gibson 	u64 flagsv2 = info_v2->flags;
1845aad95584SKent Gibson 
1846aad95584SKent Gibson 	memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
1847aad95584SKent Gibson 	memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
1848aad95584SKent Gibson 	info_v1->line_offset = info_v2->offset;
1849aad95584SKent Gibson 	info_v1->flags = 0;
1850aad95584SKent Gibson 
1851aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
1852aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_KERNEL;
1853aad95584SKent Gibson 
1854aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
1855aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
1856aad95584SKent Gibson 
1857aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
1858aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
1859aad95584SKent Gibson 
1860aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
1861aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
1862aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
1863aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
1864aad95584SKent Gibson 
1865aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
1866aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
1867aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
1868aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
1869aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
1870aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
1871aad95584SKent Gibson }
1872aad95584SKent Gibson 
1873aad95584SKent Gibson static void gpio_v2_line_info_changed_to_v1(
1874aad95584SKent Gibson 		struct gpio_v2_line_info_changed *lic_v2,
1875aad95584SKent Gibson 		struct gpioline_info_changed *lic_v1)
1876aad95584SKent Gibson {
1877aad95584SKent Gibson 	gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
1878aad95584SKent Gibson 	lic_v1->timestamp = lic_v2->timestamp_ns;
1879aad95584SKent Gibson 	lic_v1->event_type = lic_v2->event_type;
1880aad95584SKent Gibson }
1881aad95584SKent Gibson 
18823c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
18833c0d9c63SKent Gibson 
1884925ca369SKent Gibson static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
1885aad95584SKent Gibson 				  struct gpio_v2_line_info *info)
1886925ca369SKent Gibson {
1887925ca369SKent Gibson 	struct gpio_chip *gc = desc->gdev->chip;
1888925ca369SKent Gibson 	bool ok_for_pinctrl;
1889925ca369SKent Gibson 	unsigned long flags;
189065cff704SKent Gibson 	u32 debounce_period_us;
189165cff704SKent Gibson 	unsigned int num_attrs = 0;
1892925ca369SKent Gibson 
189369e4e136SKent Gibson 	memset(info, 0, sizeof(*info));
1894aad95584SKent Gibson 	info->offset = gpio_chip_hwgpio(desc);
1895925ca369SKent Gibson 
1896925ca369SKent Gibson 	/*
1897925ca369SKent Gibson 	 * This function takes a mutex so we must check this before taking
1898925ca369SKent Gibson 	 * the spinlock.
1899925ca369SKent Gibson 	 *
1900925ca369SKent Gibson 	 * FIXME: find a non-racy way to retrieve this information. Maybe a
1901925ca369SKent Gibson 	 * lock common to both frameworks?
1902925ca369SKent Gibson 	 */
1903925ca369SKent Gibson 	ok_for_pinctrl =
1904aad95584SKent Gibson 		pinctrl_gpio_can_use_line(gc->base + info->offset);
1905925ca369SKent Gibson 
1906925ca369SKent Gibson 	spin_lock_irqsave(&gpio_lock, flags);
1907925ca369SKent Gibson 
190869e4e136SKent Gibson 	if (desc->name)
190969e4e136SKent Gibson 		strscpy(info->name, desc->name, sizeof(info->name));
1910925ca369SKent Gibson 
191169e4e136SKent Gibson 	if (desc->label)
191269e4e136SKent Gibson 		strscpy(info->consumer, desc->label, sizeof(info->consumer));
1913925ca369SKent Gibson 
1914925ca369SKent Gibson 	/*
1915925ca369SKent Gibson 	 * Userspace only need to know that the kernel is using this GPIO so
1916925ca369SKent Gibson 	 * it can't use it.
1917925ca369SKent Gibson 	 */
1918925ca369SKent Gibson 	info->flags = 0;
1919925ca369SKent Gibson 	if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1920925ca369SKent Gibson 	    test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1921925ca369SKent Gibson 	    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1922925ca369SKent Gibson 	    test_bit(FLAG_EXPORT, &desc->flags) ||
1923925ca369SKent Gibson 	    test_bit(FLAG_SYSFS, &desc->flags) ||
1924925ca369SKent Gibson 	    !ok_for_pinctrl)
1925aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_USED;
1926aad95584SKent Gibson 
1927925ca369SKent Gibson 	if (test_bit(FLAG_IS_OUT, &desc->flags))
1928aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
1929aad95584SKent Gibson 	else
1930aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_INPUT;
1931aad95584SKent Gibson 
1932925ca369SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1933aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
1934aad95584SKent Gibson 
1935925ca369SKent Gibson 	if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1936aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
1937925ca369SKent Gibson 	if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1938aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
1939aad95584SKent Gibson 
1940925ca369SKent Gibson 	if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
1941aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
1942925ca369SKent Gibson 	if (test_bit(FLAG_PULL_DOWN, &desc->flags))
1943aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
1944925ca369SKent Gibson 	if (test_bit(FLAG_PULL_UP, &desc->flags))
1945aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
1946925ca369SKent Gibson 
194773e03419SKent Gibson 	if (test_bit(FLAG_EDGE_RISING, &desc->flags))
194873e03419SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
194973e03419SKent Gibson 	if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
195073e03419SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
195173e03419SKent Gibson 
195265cff704SKent Gibson 	debounce_period_us = READ_ONCE(desc->debounce_period_us);
195365cff704SKent Gibson 	if (debounce_period_us) {
195465cff704SKent Gibson 		info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
195565cff704SKent Gibson 		info->attrs[num_attrs].debounce_period_us = debounce_period_us;
195665cff704SKent Gibson 		num_attrs++;
195765cff704SKent Gibson 	}
195865cff704SKent Gibson 	info->num_attrs = num_attrs;
1959925ca369SKent Gibson 
1960925ca369SKent Gibson 	spin_unlock_irqrestore(&gpio_lock, flags);
1961925ca369SKent Gibson }
1962925ca369SKent Gibson 
1963925ca369SKent Gibson struct gpio_chardev_data {
1964925ca369SKent Gibson 	struct gpio_device *gdev;
1965925ca369SKent Gibson 	wait_queue_head_t wait;
1966aad95584SKent Gibson 	DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
1967925ca369SKent Gibson 	struct notifier_block lineinfo_changed_nb;
1968925ca369SKent Gibson 	unsigned long *watched_lines;
1969aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1970aad95584SKent Gibson 	atomic_t watch_abi_version;
1971aad95584SKent Gibson #endif
1972925ca369SKent Gibson };
1973925ca369SKent Gibson 
1974aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1975aad95584SKent Gibson /*
1976aad95584SKent Gibson  * returns 0 if the versions match, else the previously selected ABI version
1977aad95584SKent Gibson  */
1978aad95584SKent Gibson static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
1979aad95584SKent Gibson 				       unsigned int version)
1980aad95584SKent Gibson {
1981aad95584SKent Gibson 	int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
1982aad95584SKent Gibson 
1983aad95584SKent Gibson 	if (abiv == version)
1984aad95584SKent Gibson 		return 0;
1985aad95584SKent Gibson 
1986aad95584SKent Gibson 	return abiv;
1987aad95584SKent Gibson }
1988aad95584SKent Gibson #endif
1989aad95584SKent Gibson 
1990aad95584SKent Gibson static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
1991aad95584SKent Gibson 			bool watch)
1992aad95584SKent Gibson {
1993aad95584SKent Gibson 	struct gpio_desc *desc;
1994aad95584SKent Gibson 	struct gpio_v2_line_info lineinfo;
1995aad95584SKent Gibson 
1996aad95584SKent Gibson 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1997aad95584SKent Gibson 		return -EFAULT;
1998aad95584SKent Gibson 
1999aad95584SKent Gibson 	if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2000aad95584SKent Gibson 		return -EINVAL;
2001aad95584SKent Gibson 
2002aad95584SKent Gibson 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2003aad95584SKent Gibson 	if (IS_ERR(desc))
2004aad95584SKent Gibson 		return PTR_ERR(desc);
2005aad95584SKent Gibson 
2006aad95584SKent Gibson 	if (watch) {
2007aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2008aad95584SKent Gibson 		if (lineinfo_ensure_abi_version(cdev, 2))
2009aad95584SKent Gibson 			return -EPERM;
2010aad95584SKent Gibson #endif
2011aad95584SKent Gibson 		if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2012aad95584SKent Gibson 			return -EBUSY;
2013aad95584SKent Gibson 	}
2014aad95584SKent Gibson 	gpio_desc_to_lineinfo(desc, &lineinfo);
2015aad95584SKent Gibson 
2016aad95584SKent Gibson 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2017aad95584SKent Gibson 		if (watch)
2018aad95584SKent Gibson 			clear_bit(lineinfo.offset, cdev->watched_lines);
2019aad95584SKent Gibson 		return -EFAULT;
2020aad95584SKent Gibson 	}
2021aad95584SKent Gibson 
2022aad95584SKent Gibson 	return 0;
2023aad95584SKent Gibson }
2024aad95584SKent Gibson 
2025925ca369SKent Gibson /*
2026925ca369SKent Gibson  * gpio_ioctl() - ioctl handler for the GPIO chardev
2027925ca369SKent Gibson  */
202849bc5279SKent Gibson static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2029925ca369SKent Gibson {
2030e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2031e2b781c5SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
2032925ca369SKent Gibson 	struct gpio_chip *gc = gdev->chip;
2033925ca369SKent Gibson 	void __user *ip = (void __user *)arg;
2034925ca369SKent Gibson 	__u32 offset;
2035925ca369SKent Gibson 
2036925ca369SKent Gibson 	/* We fail any subsequent ioctl():s when the chip is gone */
2037925ca369SKent Gibson 	if (!gc)
2038925ca369SKent Gibson 		return -ENODEV;
2039925ca369SKent Gibson 
2040925ca369SKent Gibson 	/* Fill in the struct and pass to userspace */
2041925ca369SKent Gibson 	if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
2042925ca369SKent Gibson 		struct gpiochip_info chipinfo;
2043925ca369SKent Gibson 
2044925ca369SKent Gibson 		memset(&chipinfo, 0, sizeof(chipinfo));
2045925ca369SKent Gibson 
204669e4e136SKent Gibson 		strscpy(chipinfo.name, dev_name(&gdev->dev),
2047925ca369SKent Gibson 			sizeof(chipinfo.name));
204869e4e136SKent Gibson 		strscpy(chipinfo.label, gdev->label,
2049925ca369SKent Gibson 			sizeof(chipinfo.label));
2050925ca369SKent Gibson 		chipinfo.lines = gdev->ngpio;
2051925ca369SKent Gibson 		if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
2052925ca369SKent Gibson 			return -EFAULT;
2053925ca369SKent Gibson 		return 0;
20543c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2055925ca369SKent Gibson 	} else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
2056aad95584SKent Gibson 		struct gpio_desc *desc;
2057925ca369SKent Gibson 		struct gpioline_info lineinfo;
2058aad95584SKent Gibson 		struct gpio_v2_line_info lineinfo_v2;
2059925ca369SKent Gibson 
2060925ca369SKent Gibson 		if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2061925ca369SKent Gibson 			return -EFAULT;
2062925ca369SKent Gibson 
20631bf7ba40SKent Gibson 		/* this doubles as a range check on line_offset */
2064925ca369SKent Gibson 		desc = gpiochip_get_desc(gc, lineinfo.line_offset);
2065925ca369SKent Gibson 		if (IS_ERR(desc))
2066925ca369SKent Gibson 			return PTR_ERR(desc);
2067925ca369SKent Gibson 
2068aad95584SKent Gibson 		gpio_desc_to_lineinfo(desc, &lineinfo_v2);
2069aad95584SKent Gibson 		gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
2070925ca369SKent Gibson 
2071925ca369SKent Gibson 		if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
2072925ca369SKent Gibson 			return -EFAULT;
2073925ca369SKent Gibson 		return 0;
2074925ca369SKent Gibson 	} else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
2075925ca369SKent Gibson 		return linehandle_create(gdev, ip);
2076925ca369SKent Gibson 	} else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
2077925ca369SKent Gibson 		return lineevent_create(gdev, ip);
2078925ca369SKent Gibson 	} else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
2079aad95584SKent Gibson 		struct gpio_desc *desc;
2080925ca369SKent Gibson 		struct gpioline_info lineinfo;
2081aad95584SKent Gibson 		struct gpio_v2_line_info lineinfo_v2;
2082925ca369SKent Gibson 
2083925ca369SKent Gibson 		if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2084925ca369SKent Gibson 			return -EFAULT;
2085925ca369SKent Gibson 
20861bf7ba40SKent Gibson 		/* this doubles as a range check on line_offset */
2087925ca369SKent Gibson 		desc = gpiochip_get_desc(gc, lineinfo.line_offset);
2088925ca369SKent Gibson 		if (IS_ERR(desc))
2089925ca369SKent Gibson 			return PTR_ERR(desc);
2090925ca369SKent Gibson 
2091aad95584SKent Gibson 		if (lineinfo_ensure_abi_version(cdev, 1))
2092aad95584SKent Gibson 			return -EPERM;
2093aad95584SKent Gibson 
20941bf7ba40SKent Gibson 		if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
2095925ca369SKent Gibson 			return -EBUSY;
2096925ca369SKent Gibson 
2097aad95584SKent Gibson 		gpio_desc_to_lineinfo(desc, &lineinfo_v2);
2098aad95584SKent Gibson 		gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
2099925ca369SKent Gibson 
2100f30ef3e8SKent Gibson 		if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
21011bf7ba40SKent Gibson 			clear_bit(lineinfo.line_offset, cdev->watched_lines);
2102925ca369SKent Gibson 			return -EFAULT;
2103f30ef3e8SKent Gibson 		}
2104925ca369SKent Gibson 
2105925ca369SKent Gibson 		return 0;
21063c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
2107aad95584SKent Gibson 	} else if (cmd == GPIO_V2_GET_LINEINFO_IOCTL ||
2108aad95584SKent Gibson 		   cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL) {
2109aad95584SKent Gibson 		return lineinfo_get(cdev, ip,
2110aad95584SKent Gibson 				    cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL);
21113c0d9c63SKent Gibson 	} else if (cmd == GPIO_V2_GET_LINE_IOCTL) {
21123c0d9c63SKent Gibson 		return linereq_create(gdev, ip);
2113925ca369SKent Gibson 	} else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) {
2114925ca369SKent Gibson 		if (copy_from_user(&offset, ip, sizeof(offset)))
2115925ca369SKent Gibson 			return -EFAULT;
2116925ca369SKent Gibson 
21171bf7ba40SKent Gibson 		if (offset >= cdev->gdev->ngpio)
21181bf7ba40SKent Gibson 			return -EINVAL;
2119925ca369SKent Gibson 
21201bf7ba40SKent Gibson 		if (!test_and_clear_bit(offset, cdev->watched_lines))
2121925ca369SKent Gibson 			return -EBUSY;
2122925ca369SKent Gibson 
2123925ca369SKent Gibson 		return 0;
2124925ca369SKent Gibson 	}
2125925ca369SKent Gibson 	return -EINVAL;
2126925ca369SKent Gibson }
2127925ca369SKent Gibson 
2128925ca369SKent Gibson #ifdef CONFIG_COMPAT
212949bc5279SKent Gibson static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2130925ca369SKent Gibson 			      unsigned long arg)
2131925ca369SKent Gibson {
213249bc5279SKent Gibson 	return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2133925ca369SKent Gibson }
2134925ca369SKent Gibson #endif
2135925ca369SKent Gibson 
2136925ca369SKent Gibson static struct gpio_chardev_data *
2137925ca369SKent Gibson to_gpio_chardev_data(struct notifier_block *nb)
2138925ca369SKent Gibson {
2139925ca369SKent Gibson 	return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2140925ca369SKent Gibson }
2141925ca369SKent Gibson 
2142925ca369SKent Gibson static int lineinfo_changed_notify(struct notifier_block *nb,
2143925ca369SKent Gibson 				   unsigned long action, void *data)
2144925ca369SKent Gibson {
2145e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
2146aad95584SKent Gibson 	struct gpio_v2_line_info_changed chg;
2147925ca369SKent Gibson 	struct gpio_desc *desc = data;
2148925ca369SKent Gibson 	int ret;
2149925ca369SKent Gibson 
2150e2b781c5SKent Gibson 	if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2151925ca369SKent Gibson 		return NOTIFY_DONE;
2152925ca369SKent Gibson 
2153925ca369SKent Gibson 	memset(&chg, 0, sizeof(chg));
2154925ca369SKent Gibson 	chg.event_type = action;
2155aad95584SKent Gibson 	chg.timestamp_ns = ktime_get_ns();
2156925ca369SKent Gibson 	gpio_desc_to_lineinfo(desc, &chg.info);
2157925ca369SKent Gibson 
2158e2b781c5SKent Gibson 	ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2159925ca369SKent Gibson 	if (ret)
2160e2b781c5SKent Gibson 		wake_up_poll(&cdev->wait, EPOLLIN);
2161925ca369SKent Gibson 	else
2162925ca369SKent Gibson 		pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2163925ca369SKent Gibson 
2164925ca369SKent Gibson 	return NOTIFY_OK;
2165925ca369SKent Gibson }
2166925ca369SKent Gibson 
216749bc5279SKent Gibson static __poll_t lineinfo_watch_poll(struct file *file,
2168925ca369SKent Gibson 				    struct poll_table_struct *pollt)
2169925ca369SKent Gibson {
2170e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2171925ca369SKent Gibson 	__poll_t events = 0;
2172925ca369SKent Gibson 
2173e2b781c5SKent Gibson 	poll_wait(file, &cdev->wait, pollt);
2174925ca369SKent Gibson 
2175e2b781c5SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2176e2b781c5SKent Gibson 						 &cdev->wait.lock))
2177925ca369SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
2178925ca369SKent Gibson 
2179925ca369SKent Gibson 	return events;
2180925ca369SKent Gibson }
2181925ca369SKent Gibson 
218249bc5279SKent Gibson static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2183925ca369SKent Gibson 				   size_t count, loff_t *off)
2184925ca369SKent Gibson {
2185e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2186aad95584SKent Gibson 	struct gpio_v2_line_info_changed event;
2187925ca369SKent Gibson 	ssize_t bytes_read = 0;
2188925ca369SKent Gibson 	int ret;
2189aad95584SKent Gibson 	size_t event_size;
2190925ca369SKent Gibson 
2191aad95584SKent Gibson #ifndef CONFIG_GPIO_CDEV_V1
2192aad95584SKent Gibson 	event_size = sizeof(struct gpio_v2_line_info_changed);
2193aad95584SKent Gibson 	if (count < event_size)
2194925ca369SKent Gibson 		return -EINVAL;
2195aad95584SKent Gibson #endif
2196925ca369SKent Gibson 
2197925ca369SKent Gibson 	do {
2198e2b781c5SKent Gibson 		spin_lock(&cdev->wait.lock);
2199e2b781c5SKent Gibson 		if (kfifo_is_empty(&cdev->events)) {
2200925ca369SKent Gibson 			if (bytes_read) {
2201e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2202925ca369SKent Gibson 				return bytes_read;
2203925ca369SKent Gibson 			}
2204925ca369SKent Gibson 
220549bc5279SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
2206e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2207925ca369SKent Gibson 				return -EAGAIN;
2208925ca369SKent Gibson 			}
2209925ca369SKent Gibson 
2210e2b781c5SKent Gibson 			ret = wait_event_interruptible_locked(cdev->wait,
2211e2b781c5SKent Gibson 					!kfifo_is_empty(&cdev->events));
2212925ca369SKent Gibson 			if (ret) {
2213e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2214925ca369SKent Gibson 				return ret;
2215925ca369SKent Gibson 			}
2216925ca369SKent Gibson 		}
2217aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2218aad95584SKent Gibson 		/* must be after kfifo check so watch_abi_version is set */
2219aad95584SKent Gibson 		if (atomic_read(&cdev->watch_abi_version) == 2)
2220aad95584SKent Gibson 			event_size = sizeof(struct gpio_v2_line_info_changed);
2221aad95584SKent Gibson 		else
2222aad95584SKent Gibson 			event_size = sizeof(struct gpioline_info_changed);
2223aad95584SKent Gibson 		if (count < event_size) {
2224aad95584SKent Gibson 			spin_unlock(&cdev->wait.lock);
2225aad95584SKent Gibson 			return -EINVAL;
2226aad95584SKent Gibson 		}
2227aad95584SKent Gibson #endif
2228e2b781c5SKent Gibson 		ret = kfifo_out(&cdev->events, &event, 1);
2229e2b781c5SKent Gibson 		spin_unlock(&cdev->wait.lock);
2230925ca369SKent Gibson 		if (ret != 1) {
2231925ca369SKent Gibson 			ret = -EIO;
2232925ca369SKent Gibson 			break;
2233925ca369SKent Gibson 			/* We should never get here. See lineevent_read(). */
2234925ca369SKent Gibson 		}
2235925ca369SKent Gibson 
2236aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2237aad95584SKent Gibson 		if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2238aad95584SKent Gibson 			if (copy_to_user(buf + bytes_read, &event, event_size))
2239925ca369SKent Gibson 				return -EFAULT;
2240aad95584SKent Gibson 		} else {
2241aad95584SKent Gibson 			struct gpioline_info_changed event_v1;
2242aad95584SKent Gibson 
2243aad95584SKent Gibson 			gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2244aad95584SKent Gibson 			if (copy_to_user(buf + bytes_read, &event_v1,
2245aad95584SKent Gibson 					 event_size))
2246aad95584SKent Gibson 				return -EFAULT;
2247aad95584SKent Gibson 		}
2248aad95584SKent Gibson #else
2249aad95584SKent Gibson 		if (copy_to_user(buf + bytes_read, &event, event_size))
2250aad95584SKent Gibson 			return -EFAULT;
2251aad95584SKent Gibson #endif
2252aad95584SKent Gibson 		bytes_read += event_size;
2253925ca369SKent Gibson 	} while (count >= bytes_read + sizeof(event));
2254925ca369SKent Gibson 
2255925ca369SKent Gibson 	return bytes_read;
2256925ca369SKent Gibson }
2257925ca369SKent Gibson 
2258925ca369SKent Gibson /**
2259925ca369SKent Gibson  * gpio_chrdev_open() - open the chardev for ioctl operations
2260925ca369SKent Gibson  * @inode: inode for this chardev
226149bc5279SKent Gibson  * @file: file struct for storing private data
2262925ca369SKent Gibson  * Returns 0 on success
2263925ca369SKent Gibson  */
226449bc5279SKent Gibson static int gpio_chrdev_open(struct inode *inode, struct file *file)
2265925ca369SKent Gibson {
2266925ca369SKent Gibson 	struct gpio_device *gdev = container_of(inode->i_cdev,
2267925ca369SKent Gibson 						struct gpio_device, chrdev);
2268e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev;
2269925ca369SKent Gibson 	int ret = -ENOMEM;
2270925ca369SKent Gibson 
2271925ca369SKent Gibson 	/* Fail on open if the backing gpiochip is gone */
2272925ca369SKent Gibson 	if (!gdev->chip)
2273925ca369SKent Gibson 		return -ENODEV;
2274925ca369SKent Gibson 
2275e2b781c5SKent Gibson 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2276e2b781c5SKent Gibson 	if (!cdev)
2277925ca369SKent Gibson 		return -ENOMEM;
2278925ca369SKent Gibson 
2279e2b781c5SKent Gibson 	cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2280e2b781c5SKent Gibson 	if (!cdev->watched_lines)
2281e2b781c5SKent Gibson 		goto out_free_cdev;
2282925ca369SKent Gibson 
2283e2b781c5SKent Gibson 	init_waitqueue_head(&cdev->wait);
2284e2b781c5SKent Gibson 	INIT_KFIFO(cdev->events);
2285e2b781c5SKent Gibson 	cdev->gdev = gdev;
2286925ca369SKent Gibson 
2287e2b781c5SKent Gibson 	cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
22886accc376SKent Gibson 	ret = blocking_notifier_chain_register(&gdev->notifier,
2289e2b781c5SKent Gibson 					       &cdev->lineinfo_changed_nb);
2290925ca369SKent Gibson 	if (ret)
2291925ca369SKent Gibson 		goto out_free_bitmap;
2292925ca369SKent Gibson 
2293925ca369SKent Gibson 	get_device(&gdev->dev);
2294e2b781c5SKent Gibson 	file->private_data = cdev;
2295925ca369SKent Gibson 
229649bc5279SKent Gibson 	ret = nonseekable_open(inode, file);
2297925ca369SKent Gibson 	if (ret)
2298925ca369SKent Gibson 		goto out_unregister_notifier;
2299925ca369SKent Gibson 
2300925ca369SKent Gibson 	return ret;
2301925ca369SKent Gibson 
2302925ca369SKent Gibson out_unregister_notifier:
23036accc376SKent Gibson 	blocking_notifier_chain_unregister(&gdev->notifier,
2304e2b781c5SKent Gibson 					   &cdev->lineinfo_changed_nb);
2305925ca369SKent Gibson out_free_bitmap:
2306e2b781c5SKent Gibson 	bitmap_free(cdev->watched_lines);
2307e2b781c5SKent Gibson out_free_cdev:
2308e2b781c5SKent Gibson 	kfree(cdev);
2309925ca369SKent Gibson 	return ret;
2310925ca369SKent Gibson }
2311925ca369SKent Gibson 
2312925ca369SKent Gibson /**
2313925ca369SKent Gibson  * gpio_chrdev_release() - close chardev after ioctl operations
2314925ca369SKent Gibson  * @inode: inode for this chardev
231549bc5279SKent Gibson  * @file: file struct for storing private data
2316925ca369SKent Gibson  * Returns 0 on success
2317925ca369SKent Gibson  */
231849bc5279SKent Gibson static int gpio_chrdev_release(struct inode *inode, struct file *file)
2319925ca369SKent Gibson {
2320e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2321e2b781c5SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
2322925ca369SKent Gibson 
2323e2b781c5SKent Gibson 	bitmap_free(cdev->watched_lines);
23246accc376SKent Gibson 	blocking_notifier_chain_unregister(&gdev->notifier,
2325e2b781c5SKent Gibson 					   &cdev->lineinfo_changed_nb);
2326925ca369SKent Gibson 	put_device(&gdev->dev);
2327e2b781c5SKent Gibson 	kfree(cdev);
2328925ca369SKent Gibson 
2329925ca369SKent Gibson 	return 0;
2330925ca369SKent Gibson }
2331925ca369SKent Gibson 
2332925ca369SKent Gibson static const struct file_operations gpio_fileops = {
2333925ca369SKent Gibson 	.release = gpio_chrdev_release,
2334925ca369SKent Gibson 	.open = gpio_chrdev_open,
2335925ca369SKent Gibson 	.poll = lineinfo_watch_poll,
2336925ca369SKent Gibson 	.read = lineinfo_watch_read,
2337925ca369SKent Gibson 	.owner = THIS_MODULE,
2338925ca369SKent Gibson 	.llseek = no_llseek,
2339925ca369SKent Gibson 	.unlocked_ioctl = gpio_ioctl,
2340925ca369SKent Gibson #ifdef CONFIG_COMPAT
2341925ca369SKent Gibson 	.compat_ioctl = gpio_ioctl_compat,
2342925ca369SKent Gibson #endif
2343925ca369SKent Gibson };
2344925ca369SKent Gibson 
2345925ca369SKent Gibson int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2346925ca369SKent Gibson {
2347925ca369SKent Gibson 	int ret;
2348925ca369SKent Gibson 
2349925ca369SKent Gibson 	cdev_init(&gdev->chrdev, &gpio_fileops);
2350925ca369SKent Gibson 	gdev->chrdev.owner = THIS_MODULE;
2351925ca369SKent Gibson 	gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2352925ca369SKent Gibson 
2353925ca369SKent Gibson 	ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2354925ca369SKent Gibson 	if (ret)
2355925ca369SKent Gibson 		return ret;
2356925ca369SKent Gibson 
2357925ca369SKent Gibson 	chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2358925ca369SKent Gibson 		 MAJOR(devt), gdev->id);
2359925ca369SKent Gibson 
2360925ca369SKent Gibson 	return 0;
2361925ca369SKent Gibson }
2362925ca369SKent Gibson 
2363925ca369SKent Gibson void gpiolib_cdev_unregister(struct gpio_device *gdev)
2364925ca369SKent Gibson {
2365925ca369SKent Gibson 	cdev_device_del(&gdev->chrdev, &gdev->dev);
2366925ca369SKent Gibson }
2367