xref: /openbmc/linux/drivers/gpio/gpiolib-cdev.c (revision cfa53463ac62d30e4f30ec6e171b9228689d5a83)
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>
272068339aSDipen Patel #include <linux/hte.h>
28925ca369SKent Gibson #include <uapi/linux/gpio.h>
29925ca369SKent Gibson 
30925ca369SKent Gibson #include "gpiolib.h"
31925ca369SKent Gibson #include "gpiolib-cdev.h"
32925ca369SKent Gibson 
333c0d9c63SKent Gibson /*
343c0d9c63SKent Gibson  * Array sizes must ensure 64-bit alignment and not create holes in the
353c0d9c63SKent Gibson  * struct packing.
363c0d9c63SKent Gibson  */
373c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2));
383c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8));
393c0d9c63SKent Gibson 
403c0d9c63SKent Gibson /*
413c0d9c63SKent Gibson  * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility
423c0d9c63SKent Gibson  */
433c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8));
443c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8));
453c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8));
463c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8));
473c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8));
483c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8));
493c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8));
503c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8));
513c0d9c63SKent Gibson 
52925ca369SKent Gibson /* Character device interface to GPIO.
53925ca369SKent Gibson  *
54925ca369SKent Gibson  * The GPIO character device, /dev/gpiochipN, provides userspace an
55925ca369SKent Gibson  * interface to gpiolib GPIOs via ioctl()s.
56925ca369SKent Gibson  */
57925ca369SKent Gibson 
58925ca369SKent Gibson /*
59925ca369SKent Gibson  * GPIO line handle management
60925ca369SKent Gibson  */
61925ca369SKent Gibson 
623c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
63925ca369SKent Gibson /**
64925ca369SKent Gibson  * struct linehandle_state - contains the state of a userspace handle
65925ca369SKent Gibson  * @gdev: the GPIO device the handle pertains to
66925ca369SKent Gibson  * @label: consumer label used to tag descriptors
67925ca369SKent Gibson  * @descs: the GPIO descriptors held by this handle
6852b7b596SKent Gibson  * @num_descs: the number of descriptors held in the descs array
69925ca369SKent Gibson  */
70925ca369SKent Gibson struct linehandle_state {
71925ca369SKent Gibson 	struct gpio_device *gdev;
72925ca369SKent Gibson 	const char *label;
73925ca369SKent Gibson 	struct gpio_desc *descs[GPIOHANDLES_MAX];
7452b7b596SKent Gibson 	u32 num_descs;
75925ca369SKent Gibson };
76925ca369SKent Gibson 
77925ca369SKent Gibson #define GPIOHANDLE_REQUEST_VALID_FLAGS \
78925ca369SKent Gibson 	(GPIOHANDLE_REQUEST_INPUT | \
79925ca369SKent Gibson 	GPIOHANDLE_REQUEST_OUTPUT | \
80925ca369SKent Gibson 	GPIOHANDLE_REQUEST_ACTIVE_LOW | \
81925ca369SKent Gibson 	GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
82925ca369SKent Gibson 	GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
83925ca369SKent Gibson 	GPIOHANDLE_REQUEST_BIAS_DISABLE | \
84925ca369SKent Gibson 	GPIOHANDLE_REQUEST_OPEN_DRAIN | \
85925ca369SKent Gibson 	GPIOHANDLE_REQUEST_OPEN_SOURCE)
86925ca369SKent Gibson 
87925ca369SKent Gibson static int linehandle_validate_flags(u32 flags)
88925ca369SKent Gibson {
89925ca369SKent Gibson 	/* Return an error if an unknown flag is set */
90925ca369SKent Gibson 	if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
91925ca369SKent Gibson 		return -EINVAL;
92925ca369SKent Gibson 
93925ca369SKent Gibson 	/*
94925ca369SKent Gibson 	 * Do not allow both INPUT & OUTPUT flags to be set as they are
95925ca369SKent Gibson 	 * contradictory.
96925ca369SKent Gibson 	 */
97925ca369SKent Gibson 	if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
98925ca369SKent Gibson 	    (flags & GPIOHANDLE_REQUEST_OUTPUT))
99925ca369SKent Gibson 		return -EINVAL;
100925ca369SKent Gibson 
101925ca369SKent Gibson 	/*
102925ca369SKent Gibson 	 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
103925ca369SKent Gibson 	 * the hardware actually supports enabling both at the same time the
104925ca369SKent Gibson 	 * electrical result would be disastrous.
105925ca369SKent Gibson 	 */
106925ca369SKent Gibson 	if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
107925ca369SKent Gibson 	    (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
108925ca369SKent Gibson 		return -EINVAL;
109925ca369SKent Gibson 
110925ca369SKent Gibson 	/* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
111925ca369SKent Gibson 	if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
112925ca369SKent Gibson 	    ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
113925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
114925ca369SKent Gibson 		return -EINVAL;
115925ca369SKent Gibson 
116925ca369SKent Gibson 	/* Bias flags only allowed for input or output mode. */
117925ca369SKent Gibson 	if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
118925ca369SKent Gibson 	      (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
119925ca369SKent Gibson 	    ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
120925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
121925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
122925ca369SKent Gibson 		return -EINVAL;
123925ca369SKent Gibson 
124925ca369SKent Gibson 	/* Only one bias flag can be set. */
125925ca369SKent Gibson 	if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
126925ca369SKent Gibson 	     (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
127925ca369SKent Gibson 		       GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
128925ca369SKent Gibson 	    ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
129925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
130925ca369SKent Gibson 		return -EINVAL;
131925ca369SKent Gibson 
132925ca369SKent Gibson 	return 0;
133925ca369SKent Gibson }
134925ca369SKent Gibson 
135c274b58aSKent Gibson static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
136c274b58aSKent Gibson {
137c274b58aSKent Gibson 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
138c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
139c274b58aSKent Gibson 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
140c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
141c274b58aSKent Gibson 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
142c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
143c274b58aSKent Gibson 	assign_bit(FLAG_PULL_UP, flagsp,
144c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
145c274b58aSKent Gibson 	assign_bit(FLAG_PULL_DOWN, flagsp,
146c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
147c274b58aSKent Gibson 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
148c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
149c274b58aSKent Gibson }
150c274b58aSKent Gibson 
151925ca369SKent Gibson static long linehandle_set_config(struct linehandle_state *lh,
152925ca369SKent Gibson 				  void __user *ip)
153925ca369SKent Gibson {
154925ca369SKent Gibson 	struct gpiohandle_config gcnf;
155925ca369SKent Gibson 	struct gpio_desc *desc;
156925ca369SKent Gibson 	int i, ret;
157925ca369SKent Gibson 	u32 lflags;
158925ca369SKent Gibson 
159925ca369SKent Gibson 	if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
160925ca369SKent Gibson 		return -EFAULT;
161925ca369SKent Gibson 
162925ca369SKent Gibson 	lflags = gcnf.flags;
163925ca369SKent Gibson 	ret = linehandle_validate_flags(lflags);
164925ca369SKent Gibson 	if (ret)
165925ca369SKent Gibson 		return ret;
166925ca369SKent Gibson 
16752b7b596SKent Gibson 	for (i = 0; i < lh->num_descs; i++) {
168925ca369SKent Gibson 		desc = lh->descs[i];
169c274b58aSKent Gibson 		linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags);
170925ca369SKent Gibson 
171925ca369SKent Gibson 		/*
172925ca369SKent Gibson 		 * Lines have to be requested explicitly for input
173925ca369SKent Gibson 		 * or output, else the line will be treated "as is".
174925ca369SKent Gibson 		 */
175925ca369SKent Gibson 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
176925ca369SKent Gibson 			int val = !!gcnf.default_values[i];
177925ca369SKent Gibson 
178925ca369SKent Gibson 			ret = gpiod_direction_output(desc, val);
179925ca369SKent Gibson 			if (ret)
180925ca369SKent Gibson 				return ret;
181925ca369SKent Gibson 		} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
182925ca369SKent Gibson 			ret = gpiod_direction_input(desc);
183925ca369SKent Gibson 			if (ret)
184925ca369SKent Gibson 				return ret;
185925ca369SKent Gibson 		}
186925ca369SKent Gibson 
1876accc376SKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
188aad95584SKent Gibson 					     GPIO_V2_LINE_CHANGED_CONFIG,
189aad95584SKent Gibson 					     desc);
190925ca369SKent Gibson 	}
191925ca369SKent Gibson 	return 0;
192925ca369SKent Gibson }
193925ca369SKent Gibson 
19449bc5279SKent Gibson static long linehandle_ioctl(struct file *file, unsigned int cmd,
195925ca369SKent Gibson 			     unsigned long arg)
196925ca369SKent Gibson {
19749bc5279SKent Gibson 	struct linehandle_state *lh = file->private_data;
198925ca369SKent Gibson 	void __user *ip = (void __user *)arg;
199925ca369SKent Gibson 	struct gpiohandle_data ghd;
200925ca369SKent Gibson 	DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
2011cef8b50SAndy Shevchenko 	unsigned int i;
2021cef8b50SAndy Shevchenko 	int ret;
203925ca369SKent Gibson 
2041cef8b50SAndy Shevchenko 	switch (cmd) {
2051cef8b50SAndy Shevchenko 	case GPIOHANDLE_GET_LINE_VALUES_IOCTL:
2061cef8b50SAndy Shevchenko 		/* NOTE: It's okay to read values of output lines */
2071cef8b50SAndy Shevchenko 		ret = gpiod_get_array_value_complex(false, true,
2081cef8b50SAndy Shevchenko 						    lh->num_descs, lh->descs,
2091cef8b50SAndy Shevchenko 						    NULL, 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;
2211cef8b50SAndy Shevchenko 	case 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);
2431cef8b50SAndy Shevchenko 	case GPIOHANDLE_SET_CONFIG_IOCTL:
244925ca369SKent Gibson 		return linehandle_set_config(lh, ip);
2451cef8b50SAndy Shevchenko 	default:
246925ca369SKent Gibson 		return -EINVAL;
247925ca369SKent Gibson 	}
2481cef8b50SAndy Shevchenko }
249925ca369SKent Gibson 
250925ca369SKent Gibson #ifdef CONFIG_COMPAT
25149bc5279SKent Gibson static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
252925ca369SKent Gibson 				    unsigned long arg)
253925ca369SKent Gibson {
25449bc5279SKent Gibson 	return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
255925ca369SKent Gibson }
256925ca369SKent Gibson #endif
257925ca369SKent Gibson 
258883f9198SKent Gibson static void linehandle_free(struct linehandle_state *lh)
259925ca369SKent Gibson {
260925ca369SKent Gibson 	int i;
261925ca369SKent Gibson 
26252b7b596SKent Gibson 	for (i = 0; i < lh->num_descs; i++)
263883f9198SKent Gibson 		if (lh->descs[i])
264925ca369SKent Gibson 			gpiod_free(lh->descs[i]);
265925ca369SKent Gibson 	kfree(lh->label);
266883f9198SKent Gibson 	put_device(&lh->gdev->dev);
267925ca369SKent Gibson 	kfree(lh);
268883f9198SKent Gibson }
269883f9198SKent Gibson 
270883f9198SKent Gibson static int linehandle_release(struct inode *inode, struct file *file)
271883f9198SKent Gibson {
272883f9198SKent Gibson 	linehandle_free(file->private_data);
273925ca369SKent Gibson 	return 0;
274925ca369SKent Gibson }
275925ca369SKent Gibson 
276925ca369SKent Gibson static const struct file_operations linehandle_fileops = {
277925ca369SKent Gibson 	.release = linehandle_release,
278925ca369SKent Gibson 	.owner = THIS_MODULE,
279925ca369SKent Gibson 	.llseek = noop_llseek,
280925ca369SKent Gibson 	.unlocked_ioctl = linehandle_ioctl,
281925ca369SKent Gibson #ifdef CONFIG_COMPAT
282925ca369SKent Gibson 	.compat_ioctl = linehandle_ioctl_compat,
283925ca369SKent Gibson #endif
284925ca369SKent Gibson };
285925ca369SKent Gibson 
286925ca369SKent Gibson static int linehandle_create(struct gpio_device *gdev, void __user *ip)
287925ca369SKent Gibson {
288925ca369SKent Gibson 	struct gpiohandle_request handlereq;
289925ca369SKent Gibson 	struct linehandle_state *lh;
290925ca369SKent Gibson 	struct file *file;
291883f9198SKent Gibson 	int fd, i, ret;
292925ca369SKent Gibson 	u32 lflags;
293925ca369SKent Gibson 
294925ca369SKent Gibson 	if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
295925ca369SKent Gibson 		return -EFAULT;
296925ca369SKent Gibson 	if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
297925ca369SKent Gibson 		return -EINVAL;
298925ca369SKent Gibson 
299925ca369SKent Gibson 	lflags = handlereq.flags;
300925ca369SKent Gibson 
301925ca369SKent Gibson 	ret = linehandle_validate_flags(lflags);
302925ca369SKent Gibson 	if (ret)
303925ca369SKent Gibson 		return ret;
304925ca369SKent Gibson 
305925ca369SKent Gibson 	lh = kzalloc(sizeof(*lh), GFP_KERNEL);
306925ca369SKent Gibson 	if (!lh)
307925ca369SKent Gibson 		return -ENOMEM;
308925ca369SKent Gibson 	lh->gdev = gdev;
309925ca369SKent Gibson 	get_device(&gdev->dev);
310925ca369SKent Gibson 
311f188ac12SKent Gibson 	if (handlereq.consumer_label[0] != '\0') {
312f188ac12SKent Gibson 		/* label is only initialized if consumer_label is set */
313f188ac12SKent Gibson 		lh->label = kstrndup(handlereq.consumer_label,
314f188ac12SKent Gibson 				     sizeof(handlereq.consumer_label) - 1,
315925ca369SKent Gibson 				     GFP_KERNEL);
316925ca369SKent Gibson 		if (!lh->label) {
317925ca369SKent Gibson 			ret = -ENOMEM;
318925ca369SKent Gibson 			goto out_free_lh;
319925ca369SKent Gibson 		}
320925ca369SKent Gibson 	}
321925ca369SKent Gibson 
322883f9198SKent Gibson 	lh->num_descs = handlereq.lines;
323883f9198SKent Gibson 
324925ca369SKent Gibson 	/* Request each GPIO */
325925ca369SKent Gibson 	for (i = 0; i < handlereq.lines; i++) {
326925ca369SKent Gibson 		u32 offset = handlereq.lineoffsets[i];
327925ca369SKent Gibson 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
328925ca369SKent Gibson 
329925ca369SKent Gibson 		if (IS_ERR(desc)) {
330925ca369SKent Gibson 			ret = PTR_ERR(desc);
331883f9198SKent Gibson 			goto out_free_lh;
332925ca369SKent Gibson 		}
333925ca369SKent Gibson 
33495a4eed7SAndy Shevchenko 		ret = gpiod_request_user(desc, lh->label);
335925ca369SKent Gibson 		if (ret)
336883f9198SKent Gibson 			goto out_free_lh;
337925ca369SKent Gibson 		lh->descs[i] = desc;
338c274b58aSKent Gibson 		linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
339925ca369SKent Gibson 
340925ca369SKent Gibson 		ret = gpiod_set_transitory(desc, false);
341925ca369SKent Gibson 		if (ret < 0)
342883f9198SKent Gibson 			goto out_free_lh;
343925ca369SKent Gibson 
344925ca369SKent Gibson 		/*
345925ca369SKent Gibson 		 * Lines have to be requested explicitly for input
346925ca369SKent Gibson 		 * or output, else the line will be treated "as is".
347925ca369SKent Gibson 		 */
348925ca369SKent Gibson 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
349925ca369SKent Gibson 			int val = !!handlereq.default_values[i];
350925ca369SKent Gibson 
351925ca369SKent Gibson 			ret = gpiod_direction_output(desc, val);
352925ca369SKent Gibson 			if (ret)
353883f9198SKent Gibson 				goto out_free_lh;
354925ca369SKent Gibson 		} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
355925ca369SKent Gibson 			ret = gpiod_direction_input(desc);
356925ca369SKent Gibson 			if (ret)
357883f9198SKent Gibson 				goto out_free_lh;
358925ca369SKent Gibson 		}
359925ca369SKent Gibson 
3606accc376SKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
361aad95584SKent Gibson 					     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
362925ca369SKent Gibson 
363925ca369SKent Gibson 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
364925ca369SKent Gibson 			offset);
365925ca369SKent Gibson 	}
366925ca369SKent Gibson 
367925ca369SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
368925ca369SKent Gibson 	if (fd < 0) {
369925ca369SKent Gibson 		ret = fd;
370883f9198SKent Gibson 		goto out_free_lh;
371925ca369SKent Gibson 	}
372925ca369SKent Gibson 
373925ca369SKent Gibson 	file = anon_inode_getfile("gpio-linehandle",
374925ca369SKent Gibson 				  &linehandle_fileops,
375925ca369SKent Gibson 				  lh,
376925ca369SKent Gibson 				  O_RDONLY | O_CLOEXEC);
377925ca369SKent Gibson 	if (IS_ERR(file)) {
378925ca369SKent Gibson 		ret = PTR_ERR(file);
379925ca369SKent Gibson 		goto out_put_unused_fd;
380925ca369SKent Gibson 	}
381925ca369SKent Gibson 
382925ca369SKent Gibson 	handlereq.fd = fd;
383925ca369SKent Gibson 	if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
384925ca369SKent Gibson 		/*
385925ca369SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
386925ca369SKent Gibson 		 * the regular error cleanup path here.
387925ca369SKent Gibson 		 */
388925ca369SKent Gibson 		fput(file);
389925ca369SKent Gibson 		put_unused_fd(fd);
390925ca369SKent Gibson 		return -EFAULT;
391925ca369SKent Gibson 	}
392925ca369SKent Gibson 
393925ca369SKent Gibson 	fd_install(fd, file);
394925ca369SKent Gibson 
395925ca369SKent Gibson 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
39652b7b596SKent Gibson 		lh->num_descs);
397925ca369SKent Gibson 
398925ca369SKent Gibson 	return 0;
399925ca369SKent Gibson 
400925ca369SKent Gibson out_put_unused_fd:
401925ca369SKent Gibson 	put_unused_fd(fd);
402925ca369SKent Gibson out_free_lh:
403883f9198SKent Gibson 	linehandle_free(lh);
404925ca369SKent Gibson 	return ret;
405925ca369SKent Gibson }
4063c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
4073c0d9c63SKent Gibson 
4083c0d9c63SKent Gibson /**
4093c0d9c63SKent Gibson  * struct line - contains the state of a requested line
4103c0d9c63SKent Gibson  * @desc: the GPIO descriptor for this line.
41173e03419SKent Gibson  * @req: the corresponding line request
41273e03419SKent Gibson  * @irq: the interrupt triggered in response to events on this GPIO
41373e03419SKent Gibson  * @eflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or
41473e03419SKent Gibson  * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied
41573e03419SKent Gibson  * @timestamp_ns: cache for the timestamp storing it between hardirq and
41673e03419SKent Gibson  * IRQ thread, used to bring the timestamp close to the actual event
41773e03419SKent Gibson  * @req_seqno: the seqno for the current edge event in the sequence of
41873e03419SKent Gibson  * events for the corresponding line request. This is drawn from the @req.
41973e03419SKent Gibson  * @line_seqno: the seqno for the current edge event in the sequence of
42073e03419SKent Gibson  * events for this line.
42165cff704SKent Gibson  * @work: the worker that implements software debouncing
42265cff704SKent Gibson  * @sw_debounced: flag indicating if the software debouncer is active
42365cff704SKent Gibson  * @level: the current debounced physical level of the line
4243c0d9c63SKent Gibson  */
4253c0d9c63SKent Gibson struct line {
4263c0d9c63SKent Gibson 	struct gpio_desc *desc;
42773e03419SKent Gibson 	/*
42873e03419SKent Gibson 	 * -- edge detector specific fields --
42973e03419SKent Gibson 	 */
43073e03419SKent Gibson 	struct linereq *req;
43173e03419SKent Gibson 	unsigned int irq;
4323ffb7c45SKent Gibson 	/*
4333ffb7c45SKent Gibson 	 * eflags is set by edge_detector_setup(), edge_detector_stop() and
4343ffb7c45SKent Gibson 	 * edge_detector_update(), which are themselves mutually exclusive,
4353ffb7c45SKent Gibson 	 * and is accessed by edge_irq_thread() and debounce_work_func(),
4363ffb7c45SKent Gibson 	 * which can both live with a slightly stale value.
4373ffb7c45SKent Gibson 	 */
43873e03419SKent Gibson 	u64 eflags;
43973e03419SKent Gibson 	/*
44073e03419SKent Gibson 	 * timestamp_ns and req_seqno are accessed only by
44173e03419SKent Gibson 	 * edge_irq_handler() and edge_irq_thread(), which are themselves
44273e03419SKent Gibson 	 * mutually exclusive, so no additional protection is necessary.
44373e03419SKent Gibson 	 */
44473e03419SKent Gibson 	u64 timestamp_ns;
44573e03419SKent Gibson 	u32 req_seqno;
44665cff704SKent Gibson 	/*
44765cff704SKent Gibson 	 * line_seqno is accessed by either edge_irq_thread() or
44865cff704SKent Gibson 	 * debounce_work_func(), which are themselves mutually exclusive,
44965cff704SKent Gibson 	 * so no additional protection is necessary.
45065cff704SKent Gibson 	 */
45173e03419SKent Gibson 	u32 line_seqno;
45265cff704SKent Gibson 	/*
45365cff704SKent Gibson 	 * -- debouncer specific fields --
45465cff704SKent Gibson 	 */
45565cff704SKent Gibson 	struct delayed_work work;
45665cff704SKent Gibson 	/*
45765cff704SKent Gibson 	 * sw_debounce is accessed by linereq_set_config(), which is the
45865cff704SKent Gibson 	 * only setter, and linereq_get_values(), which can live with a
45965cff704SKent Gibson 	 * slightly stale value.
46065cff704SKent Gibson 	 */
46165cff704SKent Gibson 	unsigned int sw_debounced;
46265cff704SKent Gibson 	/*
46365cff704SKent Gibson 	 * level is accessed by debounce_work_func(), which is the only
46465cff704SKent Gibson 	 * setter, and linereq_get_values() which can live with a slightly
46565cff704SKent Gibson 	 * stale value.
46665cff704SKent Gibson 	 */
46765cff704SKent Gibson 	unsigned int level;
4682068339aSDipen Patel 	/*
4692068339aSDipen Patel 	 * -- hte specific fields --
4702068339aSDipen Patel 	 */
4712068339aSDipen Patel 	struct hte_ts_desc hdesc;
4722068339aSDipen Patel 	/*
4732068339aSDipen Patel 	 * HTE provider sets line level at the time of event. The valid
4742068339aSDipen Patel 	 * value is 0 or 1 and negative value for an error.
4752068339aSDipen Patel 	 */
4762068339aSDipen Patel 	int raw_level;
4772068339aSDipen Patel 	/*
4782068339aSDipen Patel 	 * when sw_debounce is set on HTE enabled line, this is running
4792068339aSDipen Patel 	 * counter of the discarded events.
4802068339aSDipen Patel 	 */
4812068339aSDipen Patel 	u32 total_discard_seq;
4822068339aSDipen Patel 	/*
4832068339aSDipen Patel 	 * when sw_debounce is set on HTE enabled line, this variable records
4842068339aSDipen Patel 	 * last sequence number before debounce period expires.
4852068339aSDipen Patel 	 */
4862068339aSDipen Patel 	u32 last_seqno;
4873c0d9c63SKent Gibson };
4883c0d9c63SKent Gibson 
4893c0d9c63SKent Gibson /**
4903c0d9c63SKent Gibson  * struct linereq - contains the state of a userspace line request
4913c0d9c63SKent Gibson  * @gdev: the GPIO device the line request pertains to
4923c0d9c63SKent Gibson  * @label: consumer label used to tag GPIO descriptors
4933c0d9c63SKent Gibson  * @num_lines: the number of lines in the lines array
49473e03419SKent Gibson  * @wait: wait queue that handles blocking reads of events
49573e03419SKent Gibson  * @event_buffer_size: the number of elements allocated in @events
49673e03419SKent Gibson  * @events: KFIFO for the GPIO events
49773e03419SKent Gibson  * @seqno: the sequence number for edge events generated on all lines in
49873e03419SKent Gibson  * this line request.  Note that this is not used when @num_lines is 1, as
49973e03419SKent Gibson  * the line_seqno is then the same and is cheaper to calculate.
500a54756cbSKent Gibson  * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
501a54756cbSKent Gibson  * of configuration, particularly multi-step accesses to desc flags.
5023c0d9c63SKent Gibson  * @lines: the lines held by this line request, with @num_lines elements.
5033c0d9c63SKent Gibson  */
5043c0d9c63SKent Gibson struct linereq {
5053c0d9c63SKent Gibson 	struct gpio_device *gdev;
5063c0d9c63SKent Gibson 	const char *label;
5073c0d9c63SKent Gibson 	u32 num_lines;
50873e03419SKent Gibson 	wait_queue_head_t wait;
50973e03419SKent Gibson 	u32 event_buffer_size;
51073e03419SKent Gibson 	DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
51173e03419SKent Gibson 	atomic_t seqno;
512a54756cbSKent Gibson 	struct mutex config_mutex;
5133c0d9c63SKent Gibson 	struct line lines[];
5143c0d9c63SKent Gibson };
5153c0d9c63SKent Gibson 
5163c0d9c63SKent Gibson #define GPIO_V2_LINE_BIAS_FLAGS \
5173c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
5183c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
5193c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_BIAS_DISABLED)
5203c0d9c63SKent Gibson 
5213c0d9c63SKent Gibson #define GPIO_V2_LINE_DIRECTION_FLAGS \
5223c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_INPUT | \
5233c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_OUTPUT)
5243c0d9c63SKent Gibson 
5253c0d9c63SKent Gibson #define GPIO_V2_LINE_DRIVE_FLAGS \
5263c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
5273c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_OPEN_SOURCE)
5283c0d9c63SKent Gibson 
52973e03419SKent Gibson #define GPIO_V2_LINE_EDGE_FLAGS \
53073e03419SKent Gibson 	(GPIO_V2_LINE_FLAG_EDGE_RISING | \
53173e03419SKent Gibson 	 GPIO_V2_LINE_FLAG_EDGE_FALLING)
53273e03419SKent Gibson 
5335e2ca893SKent Gibson #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS
5345e2ca893SKent Gibson 
5353c0d9c63SKent Gibson #define GPIO_V2_LINE_VALID_FLAGS \
5363c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
5373c0d9c63SKent Gibson 	 GPIO_V2_LINE_DIRECTION_FLAGS | \
5383c0d9c63SKent Gibson 	 GPIO_V2_LINE_DRIVE_FLAGS | \
53973e03419SKent Gibson 	 GPIO_V2_LINE_EDGE_FLAGS | \
54026d060e4SKent Gibson 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
5412068339aSDipen Patel 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
5423c0d9c63SKent Gibson 	 GPIO_V2_LINE_BIAS_FLAGS)
5433c0d9c63SKent Gibson 
54473e03419SKent Gibson static void linereq_put_event(struct linereq *lr,
54573e03419SKent Gibson 			      struct gpio_v2_line_event *le)
54673e03419SKent Gibson {
54773e03419SKent Gibson 	bool overflow = false;
54873e03419SKent Gibson 
54973e03419SKent Gibson 	spin_lock(&lr->wait.lock);
55073e03419SKent Gibson 	if (kfifo_is_full(&lr->events)) {
55173e03419SKent Gibson 		overflow = true;
55273e03419SKent Gibson 		kfifo_skip(&lr->events);
55373e03419SKent Gibson 	}
55473e03419SKent Gibson 	kfifo_in(&lr->events, le, 1);
55573e03419SKent Gibson 	spin_unlock(&lr->wait.lock);
55673e03419SKent Gibson 	if (!overflow)
55773e03419SKent Gibson 		wake_up_poll(&lr->wait, EPOLLIN);
55873e03419SKent Gibson 	else
55973e03419SKent Gibson 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
56073e03419SKent Gibson }
56173e03419SKent Gibson 
56226d060e4SKent Gibson static u64 line_event_timestamp(struct line *line)
56326d060e4SKent Gibson {
56426d060e4SKent Gibson 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
56526d060e4SKent Gibson 		return ktime_get_real_ns();
5662068339aSDipen Patel 	else if (test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))
5672068339aSDipen Patel 		return line->timestamp_ns;
56826d060e4SKent Gibson 
56926d060e4SKent Gibson 	return ktime_get_ns();
57026d060e4SKent Gibson }
57126d060e4SKent Gibson 
5722068339aSDipen Patel static enum hte_return process_hw_ts_thread(void *p)
5732068339aSDipen Patel {
5742068339aSDipen Patel 	struct line *line;
5752068339aSDipen Patel 	struct linereq *lr;
5762068339aSDipen Patel 	struct gpio_v2_line_event le;
5772068339aSDipen Patel 	int level;
5782068339aSDipen Patel 	u64 eflags;
5792068339aSDipen Patel 
5802068339aSDipen Patel 	if (!p)
5812068339aSDipen Patel 		return HTE_CB_HANDLED;
5822068339aSDipen Patel 
5832068339aSDipen Patel 	line = p;
5842068339aSDipen Patel 	lr = line->req;
5852068339aSDipen Patel 
5862068339aSDipen Patel 	memset(&le, 0, sizeof(le));
5872068339aSDipen Patel 
5882068339aSDipen Patel 	le.timestamp_ns = line->timestamp_ns;
5892068339aSDipen Patel 	eflags = READ_ONCE(line->eflags);
5902068339aSDipen Patel 
591*cfa53463SKent Gibson 	switch (eflags) {
592*cfa53463SKent Gibson 	case GPIO_V2_LINE_FLAG_EDGE_BOTH:
5932068339aSDipen Patel 		if (line->raw_level >= 0) {
5942068339aSDipen Patel 			if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
5952068339aSDipen Patel 				level = !line->raw_level;
5962068339aSDipen Patel 			else
5972068339aSDipen Patel 				level = line->raw_level;
5982068339aSDipen Patel 		} else {
5992068339aSDipen Patel 			level = gpiod_get_value_cansleep(line->desc);
6002068339aSDipen Patel 		}
6012068339aSDipen Patel 
6022068339aSDipen Patel 		if (level)
6032068339aSDipen Patel 			le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
6042068339aSDipen Patel 		else
6052068339aSDipen Patel 			le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
606*cfa53463SKent Gibson 		break;
607*cfa53463SKent Gibson 	case GPIO_V2_LINE_FLAG_EDGE_RISING:
6082068339aSDipen Patel 		/* Emit low-to-high event */
6092068339aSDipen Patel 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
610*cfa53463SKent Gibson 		break;
611*cfa53463SKent Gibson 	case GPIO_V2_LINE_FLAG_EDGE_FALLING:
6122068339aSDipen Patel 		/* Emit high-to-low event */
6132068339aSDipen Patel 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
614*cfa53463SKent Gibson 		break;
615*cfa53463SKent Gibson 	default:
6162068339aSDipen Patel 		return HTE_CB_HANDLED;
6172068339aSDipen Patel 	}
6182068339aSDipen Patel 	le.line_seqno = line->line_seqno;
6192068339aSDipen Patel 	le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
6202068339aSDipen Patel 	le.offset = gpio_chip_hwgpio(line->desc);
6212068339aSDipen Patel 
6222068339aSDipen Patel 	linereq_put_event(lr, &le);
6232068339aSDipen Patel 
6242068339aSDipen Patel 	return HTE_CB_HANDLED;
6252068339aSDipen Patel }
6262068339aSDipen Patel 
6272068339aSDipen Patel static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p)
6282068339aSDipen Patel {
6292068339aSDipen Patel 	struct line *line;
6302068339aSDipen Patel 	struct linereq *lr;
6312068339aSDipen Patel 	int diff_seqno = 0;
6322068339aSDipen Patel 
6332068339aSDipen Patel 	if (!ts || !p)
6342068339aSDipen Patel 		return HTE_CB_HANDLED;
6352068339aSDipen Patel 
6362068339aSDipen Patel 	line = p;
6372068339aSDipen Patel 	line->timestamp_ns = ts->tsc;
6382068339aSDipen Patel 	line->raw_level = ts->raw_level;
6392068339aSDipen Patel 	lr = line->req;
6402068339aSDipen Patel 
6412068339aSDipen Patel 	if (READ_ONCE(line->sw_debounced)) {
6422068339aSDipen Patel 		line->total_discard_seq++;
6432068339aSDipen Patel 		line->last_seqno = ts->seq;
6442068339aSDipen Patel 		mod_delayed_work(system_wq, &line->work,
6452068339aSDipen Patel 		  usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
6462068339aSDipen Patel 	} else {
6472068339aSDipen Patel 		if (unlikely(ts->seq < line->line_seqno))
6482068339aSDipen Patel 			return HTE_CB_HANDLED;
6492068339aSDipen Patel 
6502068339aSDipen Patel 		diff_seqno = ts->seq - line->line_seqno;
6512068339aSDipen Patel 		line->line_seqno = ts->seq;
6522068339aSDipen Patel 		if (lr->num_lines != 1)
6532068339aSDipen Patel 			line->req_seqno = atomic_add_return(diff_seqno,
6542068339aSDipen Patel 							    &lr->seqno);
6552068339aSDipen Patel 
6562068339aSDipen Patel 		return HTE_RUN_SECOND_CB;
6572068339aSDipen Patel 	}
6582068339aSDipen Patel 
6592068339aSDipen Patel 	return HTE_CB_HANDLED;
6602068339aSDipen Patel }
6612068339aSDipen Patel 
66273e03419SKent Gibson static irqreturn_t edge_irq_thread(int irq, void *p)
66373e03419SKent Gibson {
66473e03419SKent Gibson 	struct line *line = p;
66573e03419SKent Gibson 	struct linereq *lr = line->req;
66673e03419SKent Gibson 	struct gpio_v2_line_event le;
66773e03419SKent Gibson 
66873e03419SKent Gibson 	/* Do not leak kernel stack to userspace */
66973e03419SKent Gibson 	memset(&le, 0, sizeof(le));
67073e03419SKent Gibson 
67173e03419SKent Gibson 	if (line->timestamp_ns) {
67273e03419SKent Gibson 		le.timestamp_ns = line->timestamp_ns;
67373e03419SKent Gibson 	} else {
67473e03419SKent Gibson 		/*
67573e03419SKent Gibson 		 * We may be running from a nested threaded interrupt in
67673e03419SKent Gibson 		 * which case we didn't get the timestamp from
67773e03419SKent Gibson 		 * edge_irq_handler().
67873e03419SKent Gibson 		 */
67926d060e4SKent Gibson 		le.timestamp_ns = line_event_timestamp(line);
68073e03419SKent Gibson 		if (lr->num_lines != 1)
68173e03419SKent Gibson 			line->req_seqno = atomic_inc_return(&lr->seqno);
68273e03419SKent Gibson 	}
68373e03419SKent Gibson 	line->timestamp_ns = 0;
68473e03419SKent Gibson 
685*cfa53463SKent Gibson 	switch (READ_ONCE(line->eflags)) {
686*cfa53463SKent Gibson 	case GPIO_V2_LINE_FLAG_EDGE_BOTH:
687*cfa53463SKent Gibson 		if (gpiod_get_value_cansleep(line->desc))
68873e03419SKent Gibson 			/* Emit low-to-high event */
68973e03419SKent Gibson 			le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
69073e03419SKent Gibson 		else
69173e03419SKent Gibson 			/* Emit high-to-low event */
69273e03419SKent Gibson 			le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
693*cfa53463SKent Gibson 
694*cfa53463SKent Gibson 		break;
695*cfa53463SKent Gibson 	case GPIO_V2_LINE_FLAG_EDGE_RISING:
69673e03419SKent Gibson 		/* Emit low-to-high event */
69773e03419SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
698*cfa53463SKent Gibson 		break;
699*cfa53463SKent Gibson 	case GPIO_V2_LINE_FLAG_EDGE_FALLING:
70073e03419SKent Gibson 		/* Emit high-to-low event */
70173e03419SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
702*cfa53463SKent Gibson 		break;
703*cfa53463SKent Gibson 	default:
70473e03419SKent Gibson 		return IRQ_NONE;
70573e03419SKent Gibson 	}
70673e03419SKent Gibson 	line->line_seqno++;
70773e03419SKent Gibson 	le.line_seqno = line->line_seqno;
70873e03419SKent Gibson 	le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
70973e03419SKent Gibson 	le.offset = gpio_chip_hwgpio(line->desc);
71073e03419SKent Gibson 
71173e03419SKent Gibson 	linereq_put_event(lr, &le);
71273e03419SKent Gibson 
71373e03419SKent Gibson 	return IRQ_HANDLED;
71473e03419SKent Gibson }
71573e03419SKent Gibson 
71673e03419SKent Gibson static irqreturn_t edge_irq_handler(int irq, void *p)
71773e03419SKent Gibson {
71873e03419SKent Gibson 	struct line *line = p;
71973e03419SKent Gibson 	struct linereq *lr = line->req;
72073e03419SKent Gibson 
72173e03419SKent Gibson 	/*
72273e03419SKent Gibson 	 * Just store the timestamp in hardirq context so we get it as
72373e03419SKent Gibson 	 * close in time as possible to the actual event.
72473e03419SKent Gibson 	 */
72526d060e4SKent Gibson 	line->timestamp_ns = line_event_timestamp(line);
72673e03419SKent Gibson 
72773e03419SKent Gibson 	if (lr->num_lines != 1)
72873e03419SKent Gibson 		line->req_seqno = atomic_inc_return(&lr->seqno);
72973e03419SKent Gibson 
73073e03419SKent Gibson 	return IRQ_WAKE_THREAD;
73173e03419SKent Gibson }
73273e03419SKent Gibson 
73365cff704SKent Gibson /*
73465cff704SKent Gibson  * returns the current debounced logical value.
73565cff704SKent Gibson  */
73665cff704SKent Gibson static bool debounced_value(struct line *line)
73765cff704SKent Gibson {
73865cff704SKent Gibson 	bool value;
73965cff704SKent Gibson 
74065cff704SKent Gibson 	/*
74165cff704SKent Gibson 	 * minor race - debouncer may be stopped here, so edge_detector_stop()
74265cff704SKent Gibson 	 * must leave the value unchanged so the following will read the level
74365cff704SKent Gibson 	 * from when the debouncer was last running.
74465cff704SKent Gibson 	 */
74565cff704SKent Gibson 	value = READ_ONCE(line->level);
74665cff704SKent Gibson 
74765cff704SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
74865cff704SKent Gibson 		value = !value;
74965cff704SKent Gibson 
75065cff704SKent Gibson 	return value;
75165cff704SKent Gibson }
75265cff704SKent Gibson 
75365cff704SKent Gibson static irqreturn_t debounce_irq_handler(int irq, void *p)
75465cff704SKent Gibson {
75565cff704SKent Gibson 	struct line *line = p;
75665cff704SKent Gibson 
75765cff704SKent Gibson 	mod_delayed_work(system_wq, &line->work,
75865cff704SKent Gibson 		usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
75965cff704SKent Gibson 
76065cff704SKent Gibson 	return IRQ_HANDLED;
76165cff704SKent Gibson }
76265cff704SKent Gibson 
76365cff704SKent Gibson static void debounce_work_func(struct work_struct *work)
76465cff704SKent Gibson {
76565cff704SKent Gibson 	struct gpio_v2_line_event le;
76665cff704SKent Gibson 	struct line *line = container_of(work, struct line, work.work);
76765cff704SKent Gibson 	struct linereq *lr;
7682068339aSDipen Patel 	int level, diff_seqno;
7693ffb7c45SKent Gibson 	u64 eflags;
77065cff704SKent Gibson 
7712068339aSDipen Patel 	if (test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags)) {
7722068339aSDipen Patel 		level = line->raw_level;
7732068339aSDipen Patel 		if (level < 0)
77465cff704SKent Gibson 			level = gpiod_get_raw_value_cansleep(line->desc);
7752068339aSDipen Patel 	} else {
7762068339aSDipen Patel 		level = gpiod_get_raw_value_cansleep(line->desc);
7772068339aSDipen Patel 	}
77865cff704SKent Gibson 	if (level < 0) {
77965cff704SKent Gibson 		pr_debug_ratelimited("debouncer failed to read line value\n");
78065cff704SKent Gibson 		return;
78165cff704SKent Gibson 	}
78265cff704SKent Gibson 
78365cff704SKent Gibson 	if (READ_ONCE(line->level) == level)
78465cff704SKent Gibson 		return;
78565cff704SKent Gibson 
78665cff704SKent Gibson 	WRITE_ONCE(line->level, level);
78765cff704SKent Gibson 
78865cff704SKent Gibson 	/* -- edge detection -- */
7893ffb7c45SKent Gibson 	eflags = READ_ONCE(line->eflags);
7903ffb7c45SKent Gibson 	if (!eflags)
79165cff704SKent Gibson 		return;
79265cff704SKent Gibson 
79365cff704SKent Gibson 	/* switch from physical level to logical - if they differ */
79465cff704SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
79565cff704SKent Gibson 		level = !level;
79665cff704SKent Gibson 
79765cff704SKent Gibson 	/* ignore edges that are not being monitored */
7983ffb7c45SKent Gibson 	if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
7993ffb7c45SKent Gibson 	    ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
80065cff704SKent Gibson 		return;
80165cff704SKent Gibson 
80265cff704SKent Gibson 	/* Do not leak kernel stack to userspace */
80365cff704SKent Gibson 	memset(&le, 0, sizeof(le));
80465cff704SKent Gibson 
80565cff704SKent Gibson 	lr = line->req;
80626d060e4SKent Gibson 	le.timestamp_ns = line_event_timestamp(line);
80765cff704SKent Gibson 	le.offset = gpio_chip_hwgpio(line->desc);
8082068339aSDipen Patel 	if (test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags)) {
8092068339aSDipen Patel 		/* discard events except the last one */
8102068339aSDipen Patel 		line->total_discard_seq -= 1;
8112068339aSDipen Patel 		diff_seqno = line->last_seqno - line->total_discard_seq -
8122068339aSDipen Patel 				line->line_seqno;
8132068339aSDipen Patel 		line->line_seqno = line->last_seqno - line->total_discard_seq;
8142068339aSDipen Patel 		le.line_seqno = line->line_seqno;
8152068339aSDipen Patel 		le.seqno = (lr->num_lines == 1) ?
8162068339aSDipen Patel 			le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno);
8172068339aSDipen Patel 	} else {
81865cff704SKent Gibson 		line->line_seqno++;
81965cff704SKent Gibson 		le.line_seqno = line->line_seqno;
82065cff704SKent Gibson 		le.seqno = (lr->num_lines == 1) ?
82165cff704SKent Gibson 			le.line_seqno : atomic_inc_return(&lr->seqno);
8222068339aSDipen Patel 	}
82365cff704SKent Gibson 
82465cff704SKent Gibson 	if (level)
82565cff704SKent Gibson 		/* Emit low-to-high event */
82665cff704SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
82765cff704SKent Gibson 	else
82865cff704SKent Gibson 		/* Emit high-to-low event */
82965cff704SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
83065cff704SKent Gibson 
83165cff704SKent Gibson 	linereq_put_event(lr, &le);
83265cff704SKent Gibson }
83365cff704SKent Gibson 
8342068339aSDipen Patel static int hte_edge_setup(struct line *line, u64 eflags)
8352068339aSDipen Patel {
8362068339aSDipen Patel 	int ret;
8372068339aSDipen Patel 	unsigned long flags = 0;
8382068339aSDipen Patel 	struct hte_ts_desc *hdesc = &line->hdesc;
8392068339aSDipen Patel 
8402068339aSDipen Patel 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
8412068339aSDipen Patel 		flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
8422068339aSDipen Patel 				  HTE_FALLING_EDGE_TS : HTE_RISING_EDGE_TS;
8432068339aSDipen Patel 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
8442068339aSDipen Patel 		flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
8452068339aSDipen Patel 				  HTE_RISING_EDGE_TS : HTE_FALLING_EDGE_TS;
8462068339aSDipen Patel 
8472068339aSDipen Patel 	line->total_discard_seq = 0;
8482068339aSDipen Patel 
8492068339aSDipen Patel 	hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags,
8502068339aSDipen Patel 			   NULL, line->desc);
8512068339aSDipen Patel 
8522068339aSDipen Patel 	ret = hte_ts_get(NULL, hdesc, 0);
8532068339aSDipen Patel 	if (ret)
8542068339aSDipen Patel 		return ret;
8552068339aSDipen Patel 
8562068339aSDipen Patel 	return hte_request_ts_ns(hdesc, process_hw_ts,
8572068339aSDipen Patel 				 process_hw_ts_thread, line);
8582068339aSDipen Patel }
8592068339aSDipen Patel 
86065cff704SKent Gibson static int debounce_setup(struct line *line,
8612068339aSDipen Patel 			  unsigned int debounce_period_us, bool hte_req)
86265cff704SKent Gibson {
86365cff704SKent Gibson 	unsigned long irqflags;
86465cff704SKent Gibson 	int ret, level, irq;
86565cff704SKent Gibson 
86665cff704SKent Gibson 	/* try hardware */
86765cff704SKent Gibson 	ret = gpiod_set_debounce(line->desc, debounce_period_us);
86865cff704SKent Gibson 	if (!ret) {
86965cff704SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
87065cff704SKent Gibson 		return ret;
87165cff704SKent Gibson 	}
87265cff704SKent Gibson 	if (ret != -ENOTSUPP)
87365cff704SKent Gibson 		return ret;
87465cff704SKent Gibson 
87565cff704SKent Gibson 	if (debounce_period_us) {
87665cff704SKent Gibson 		/* setup software debounce */
87765cff704SKent Gibson 		level = gpiod_get_raw_value_cansleep(line->desc);
87865cff704SKent Gibson 		if (level < 0)
87965cff704SKent Gibson 			return level;
88065cff704SKent Gibson 
8812068339aSDipen Patel 		if (!hte_req) {
88265cff704SKent Gibson 			irq = gpiod_to_irq(line->desc);
88365cff704SKent Gibson 			if (irq < 0)
88465cff704SKent Gibson 				return -ENXIO;
88565cff704SKent Gibson 
88665cff704SKent Gibson 			irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
88765cff704SKent Gibson 			ret = request_irq(irq, debounce_irq_handler, irqflags,
88865cff704SKent Gibson 					  line->req->label, line);
88965cff704SKent Gibson 			if (ret)
89065cff704SKent Gibson 				return ret;
89165cff704SKent Gibson 			line->irq = irq;
8922068339aSDipen Patel 		} else {
8932487a812SKent Gibson 			ret = hte_edge_setup(line, GPIO_V2_LINE_FLAG_EDGE_BOTH);
8942068339aSDipen Patel 			if (ret)
8952068339aSDipen Patel 				return ret;
8962068339aSDipen Patel 		}
8972068339aSDipen Patel 
8982068339aSDipen Patel 		WRITE_ONCE(line->level, level);
8992068339aSDipen Patel 		WRITE_ONCE(line->sw_debounced, 1);
90065cff704SKent Gibson 	}
90165cff704SKent Gibson 	return 0;
90265cff704SKent Gibson }
90365cff704SKent Gibson 
90465cff704SKent Gibson static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
90565cff704SKent Gibson 					  unsigned int line_idx)
90665cff704SKent Gibson {
90765cff704SKent Gibson 	unsigned int i;
90865cff704SKent Gibson 	u64 mask = BIT_ULL(line_idx);
90965cff704SKent Gibson 
91065cff704SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
91165cff704SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
91265cff704SKent Gibson 		    (lc->attrs[i].mask & mask))
91365cff704SKent Gibson 			return true;
91465cff704SKent Gibson 	}
91565cff704SKent Gibson 	return false;
91665cff704SKent Gibson }
91765cff704SKent Gibson 
91865cff704SKent Gibson static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
91965cff704SKent Gibson 					       unsigned int line_idx)
92065cff704SKent Gibson {
92165cff704SKent Gibson 	unsigned int i;
92265cff704SKent Gibson 	u64 mask = BIT_ULL(line_idx);
92365cff704SKent Gibson 
92465cff704SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
92565cff704SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
92665cff704SKent Gibson 		    (lc->attrs[i].mask & mask))
92765cff704SKent Gibson 			return lc->attrs[i].attr.debounce_period_us;
92865cff704SKent Gibson 	}
92965cff704SKent Gibson 	return 0;
93065cff704SKent Gibson }
93165cff704SKent Gibson 
9322068339aSDipen Patel static void edge_detector_stop(struct line *line, bool hte_en)
93373e03419SKent Gibson {
9342068339aSDipen Patel 	if (line->irq && !hte_en) {
93573e03419SKent Gibson 		free_irq(line->irq, line);
93673e03419SKent Gibson 		line->irq = 0;
93773e03419SKent Gibson 	}
938a54756cbSKent Gibson 
9392068339aSDipen Patel 	if (hte_en)
9402068339aSDipen Patel 		hte_ts_put(&line->hdesc);
9412068339aSDipen Patel 
94265cff704SKent Gibson 	cancel_delayed_work_sync(&line->work);
94365cff704SKent Gibson 	WRITE_ONCE(line->sw_debounced, 0);
9443ffb7c45SKent Gibson 	WRITE_ONCE(line->eflags, 0);
94503a58ea5SKent Gibson 	if (line->desc)
94603a58ea5SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, 0);
94765cff704SKent Gibson 	/* do not change line->level - see comment in debounced_value() */
94873e03419SKent Gibson }
94973e03419SKent Gibson 
95073e03419SKent Gibson static int edge_detector_setup(struct line *line,
95165cff704SKent Gibson 			       struct gpio_v2_line_config *lc,
95265cff704SKent Gibson 			       unsigned int line_idx,
9532068339aSDipen Patel 			       u64 eflags, bool hte_req)
95473e03419SKent Gibson {
95565cff704SKent Gibson 	u32 debounce_period_us;
95673e03419SKent Gibson 	unsigned long irqflags = 0;
95773e03419SKent Gibson 	int irq, ret;
95873e03419SKent Gibson 
95973e03419SKent Gibson 	if (eflags && !kfifo_initialized(&line->req->events)) {
96073e03419SKent Gibson 		ret = kfifo_alloc(&line->req->events,
96173e03419SKent Gibson 				  line->req->event_buffer_size, GFP_KERNEL);
96273e03419SKent Gibson 		if (ret)
96373e03419SKent Gibson 			return ret;
96473e03419SKent Gibson 	}
9653ffb7c45SKent Gibson 	WRITE_ONCE(line->eflags, eflags);
96665cff704SKent Gibson 	if (gpio_v2_line_config_debounced(lc, line_idx)) {
96765cff704SKent Gibson 		debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
9682068339aSDipen Patel 		ret = debounce_setup(line, debounce_period_us, hte_req);
96965cff704SKent Gibson 		if (ret)
97065cff704SKent Gibson 			return ret;
97165cff704SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
97265cff704SKent Gibson 	}
97373e03419SKent Gibson 
97465cff704SKent Gibson 	/* detection disabled or sw debouncer will provide edge detection */
97565cff704SKent Gibson 	if (!eflags || READ_ONCE(line->sw_debounced))
97673e03419SKent Gibson 		return 0;
97773e03419SKent Gibson 
9782068339aSDipen Patel 	if (hte_req)
9792068339aSDipen Patel 		return hte_edge_setup(line, eflags);
9802068339aSDipen Patel 
98173e03419SKent Gibson 	irq = gpiod_to_irq(line->desc);
98273e03419SKent Gibson 	if (irq < 0)
98373e03419SKent Gibson 		return -ENXIO;
98473e03419SKent Gibson 
98573e03419SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
98673e03419SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
98773e03419SKent Gibson 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
98873e03419SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
98973e03419SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
99073e03419SKent Gibson 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
99173e03419SKent Gibson 	irqflags |= IRQF_ONESHOT;
99273e03419SKent Gibson 
99373e03419SKent Gibson 	/* Request a thread to read the events */
99473e03419SKent Gibson 	ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
99573e03419SKent Gibson 				   irqflags, line->req->label, line);
99673e03419SKent Gibson 	if (ret)
99773e03419SKent Gibson 		return ret;
99873e03419SKent Gibson 
99973e03419SKent Gibson 	line->irq = irq;
100073e03419SKent Gibson 	return 0;
100173e03419SKent Gibson }
100273e03419SKent Gibson 
100365cff704SKent Gibson static int edge_detector_update(struct line *line,
100465cff704SKent Gibson 				struct gpio_v2_line_config *lc,
100565cff704SKent Gibson 				unsigned int line_idx,
10062068339aSDipen Patel 				u64 flags, bool polarity_change,
10072068339aSDipen Patel 				bool prev_hte_flag)
1008a54756cbSKent Gibson {
10092068339aSDipen Patel 	u64 eflags = flags & GPIO_V2_LINE_EDGE_FLAGS;
101065cff704SKent Gibson 	unsigned int debounce_period_us =
101165cff704SKent Gibson 			gpio_v2_line_config_debounce_period(lc, line_idx);
10122068339aSDipen Patel 	bool hte_change = (prev_hte_flag !=
10132068339aSDipen Patel 		      ((flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) != 0));
101465cff704SKent Gibson 
10153ffb7c45SKent Gibson 	if ((READ_ONCE(line->eflags) == eflags) && !polarity_change &&
10162068339aSDipen Patel 	    (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us)
10172068339aSDipen Patel 	    && !hte_change)
1018a54756cbSKent Gibson 		return 0;
1019a54756cbSKent Gibson 
102065cff704SKent Gibson 	/* sw debounced and still will be...*/
102165cff704SKent Gibson 	if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
10223ffb7c45SKent Gibson 		WRITE_ONCE(line->eflags, eflags);
102365cff704SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
102465cff704SKent Gibson 		return 0;
102565cff704SKent Gibson 	}
102665cff704SKent Gibson 
102765cff704SKent Gibson 	/* reconfiguring edge detection or sw debounce being disabled */
10282068339aSDipen Patel 	if ((line->irq && !READ_ONCE(line->sw_debounced)) || prev_hte_flag ||
102965cff704SKent Gibson 	    (!debounce_period_us && READ_ONCE(line->sw_debounced)))
10302068339aSDipen Patel 		edge_detector_stop(line, prev_hte_flag);
1031a54756cbSKent Gibson 
10322068339aSDipen Patel 	return edge_detector_setup(line, lc, line_idx, eflags,
10332068339aSDipen Patel 				   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
1034a54756cbSKent Gibson }
1035a54756cbSKent Gibson 
10363c0d9c63SKent Gibson static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
10373c0d9c63SKent Gibson 				     unsigned int line_idx)
10383c0d9c63SKent Gibson {
10393c0d9c63SKent Gibson 	unsigned int i;
10403c0d9c63SKent Gibson 	u64 mask = BIT_ULL(line_idx);
10413c0d9c63SKent Gibson 
10423c0d9c63SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
10433c0d9c63SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
10443c0d9c63SKent Gibson 		    (lc->attrs[i].mask & mask))
10453c0d9c63SKent Gibson 			return lc->attrs[i].attr.flags;
10463c0d9c63SKent Gibson 	}
10473c0d9c63SKent Gibson 	return lc->flags;
10483c0d9c63SKent Gibson }
10493c0d9c63SKent Gibson 
10503c0d9c63SKent Gibson static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
10513c0d9c63SKent Gibson 					    unsigned int line_idx)
10523c0d9c63SKent Gibson {
10533c0d9c63SKent Gibson 	unsigned int i;
10543c0d9c63SKent Gibson 	u64 mask = BIT_ULL(line_idx);
10553c0d9c63SKent Gibson 
10563c0d9c63SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
10573c0d9c63SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
10583c0d9c63SKent Gibson 		    (lc->attrs[i].mask & mask))
10593c0d9c63SKent Gibson 			return !!(lc->attrs[i].attr.values & mask);
10603c0d9c63SKent Gibson 	}
10613c0d9c63SKent Gibson 	return 0;
10623c0d9c63SKent Gibson }
10633c0d9c63SKent Gibson 
10643c0d9c63SKent Gibson static int gpio_v2_line_flags_validate(u64 flags)
10653c0d9c63SKent Gibson {
10663c0d9c63SKent Gibson 	/* Return an error if an unknown flag is set */
10673c0d9c63SKent Gibson 	if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
10683c0d9c63SKent Gibson 		return -EINVAL;
10693c0d9c63SKent Gibson 	/*
10703c0d9c63SKent Gibson 	 * Do not allow both INPUT and OUTPUT flags to be set as they are
10713c0d9c63SKent Gibson 	 * contradictory.
10723c0d9c63SKent Gibson 	 */
10733c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
10743c0d9c63SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_OUTPUT))
10753c0d9c63SKent Gibson 		return -EINVAL;
10763c0d9c63SKent Gibson 
10772068339aSDipen Patel 	/* Only allow one event clock source */
10782068339aSDipen Patel 	if ((flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) &&
10792068339aSDipen Patel 	    (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
10802068339aSDipen Patel 		return -EINVAL;
10812068339aSDipen Patel 
108273e03419SKent Gibson 	/* Edge detection requires explicit input. */
108373e03419SKent Gibson 	if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
108473e03419SKent Gibson 	    !(flags & GPIO_V2_LINE_FLAG_INPUT))
108573e03419SKent Gibson 		return -EINVAL;
108673e03419SKent Gibson 
10873c0d9c63SKent Gibson 	/*
10883c0d9c63SKent Gibson 	 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
10893c0d9c63SKent Gibson 	 * request. If the hardware actually supports enabling both at the
10903c0d9c63SKent Gibson 	 * same time the electrical result would be disastrous.
10913c0d9c63SKent Gibson 	 */
10923c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
10933c0d9c63SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
10943c0d9c63SKent Gibson 		return -EINVAL;
10953c0d9c63SKent Gibson 
10963c0d9c63SKent Gibson 	/* Drive requires explicit output direction. */
10973c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
10983c0d9c63SKent Gibson 	    !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
10993c0d9c63SKent Gibson 		return -EINVAL;
11003c0d9c63SKent Gibson 
11013c0d9c63SKent Gibson 	/* Bias requires explicit direction. */
11023c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
11033c0d9c63SKent Gibson 	    !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
11043c0d9c63SKent Gibson 		return -EINVAL;
11053c0d9c63SKent Gibson 
11063c0d9c63SKent Gibson 	/* Only one bias flag can be set. */
11073c0d9c63SKent Gibson 	if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
11083c0d9c63SKent Gibson 	     (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
11093c0d9c63SKent Gibson 		       GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
11103c0d9c63SKent Gibson 	    ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
11113c0d9c63SKent Gibson 	     (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
11123c0d9c63SKent Gibson 		return -EINVAL;
11133c0d9c63SKent Gibson 
11143c0d9c63SKent Gibson 	return 0;
11153c0d9c63SKent Gibson }
11163c0d9c63SKent Gibson 
11173c0d9c63SKent Gibson static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
11183c0d9c63SKent Gibson 					unsigned int num_lines)
11193c0d9c63SKent Gibson {
11203c0d9c63SKent Gibson 	unsigned int i;
11213c0d9c63SKent Gibson 	u64 flags;
11223c0d9c63SKent Gibson 	int ret;
11233c0d9c63SKent Gibson 
11243c0d9c63SKent Gibson 	if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
11253c0d9c63SKent Gibson 		return -EINVAL;
11263c0d9c63SKent Gibson 
11273c0d9c63SKent Gibson 	if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
11283c0d9c63SKent Gibson 		return -EINVAL;
11293c0d9c63SKent Gibson 
11303c0d9c63SKent Gibson 	for (i = 0; i < num_lines; i++) {
11313c0d9c63SKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
11323c0d9c63SKent Gibson 		ret = gpio_v2_line_flags_validate(flags);
11333c0d9c63SKent Gibson 		if (ret)
11343c0d9c63SKent Gibson 			return ret;
113565cff704SKent Gibson 
113665cff704SKent Gibson 		/* debounce requires explicit input */
113765cff704SKent Gibson 		if (gpio_v2_line_config_debounced(lc, i) &&
113865cff704SKent Gibson 		    !(flags & GPIO_V2_LINE_FLAG_INPUT))
113965cff704SKent Gibson 			return -EINVAL;
11403c0d9c63SKent Gibson 	}
11413c0d9c63SKent Gibson 	return 0;
11423c0d9c63SKent Gibson }
11433c0d9c63SKent Gibson 
11443c0d9c63SKent Gibson static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
11453c0d9c63SKent Gibson 						    unsigned long *flagsp)
11463c0d9c63SKent Gibson {
11473c0d9c63SKent Gibson 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
11483c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
11493c0d9c63SKent Gibson 
11503c0d9c63SKent Gibson 	if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
11513c0d9c63SKent Gibson 		set_bit(FLAG_IS_OUT, flagsp);
11523c0d9c63SKent Gibson 	else if (flags & GPIO_V2_LINE_FLAG_INPUT)
11533c0d9c63SKent Gibson 		clear_bit(FLAG_IS_OUT, flagsp);
11543c0d9c63SKent Gibson 
115573e03419SKent Gibson 	assign_bit(FLAG_EDGE_RISING, flagsp,
115673e03419SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
115773e03419SKent Gibson 	assign_bit(FLAG_EDGE_FALLING, flagsp,
115873e03419SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
115973e03419SKent Gibson 
11603c0d9c63SKent Gibson 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
11613c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
11623c0d9c63SKent Gibson 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
11633c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
11643c0d9c63SKent Gibson 
11653c0d9c63SKent Gibson 	assign_bit(FLAG_PULL_UP, flagsp,
11663c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
11673c0d9c63SKent Gibson 	assign_bit(FLAG_PULL_DOWN, flagsp,
11683c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
11693c0d9c63SKent Gibson 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
11703c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
117126d060e4SKent Gibson 
117226d060e4SKent Gibson 	assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
117326d060e4SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
11742068339aSDipen Patel 	assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp,
11752068339aSDipen Patel 		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
11763c0d9c63SKent Gibson }
11773c0d9c63SKent Gibson 
11783c0d9c63SKent Gibson static long linereq_get_values(struct linereq *lr, void __user *ip)
11793c0d9c63SKent Gibson {
11803c0d9c63SKent Gibson 	struct gpio_v2_line_values lv;
11813c0d9c63SKent Gibson 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
11823c0d9c63SKent Gibson 	struct gpio_desc **descs;
11833c0d9c63SKent Gibson 	unsigned int i, didx, num_get;
118465cff704SKent Gibson 	bool val;
11853c0d9c63SKent Gibson 	int ret;
11863c0d9c63SKent Gibson 
11873c0d9c63SKent Gibson 	/* NOTE: It's ok to read values of output lines. */
11883c0d9c63SKent Gibson 	if (copy_from_user(&lv, ip, sizeof(lv)))
11893c0d9c63SKent Gibson 		return -EFAULT;
11903c0d9c63SKent Gibson 
11913c0d9c63SKent Gibson 	for (num_get = 0, i = 0; i < lr->num_lines; i++) {
11923c0d9c63SKent Gibson 		if (lv.mask & BIT_ULL(i)) {
11933c0d9c63SKent Gibson 			num_get++;
11943c0d9c63SKent Gibson 			descs = &lr->lines[i].desc;
11953c0d9c63SKent Gibson 		}
11963c0d9c63SKent Gibson 	}
11973c0d9c63SKent Gibson 
11983c0d9c63SKent Gibson 	if (num_get == 0)
11993c0d9c63SKent Gibson 		return -EINVAL;
12003c0d9c63SKent Gibson 
12013c0d9c63SKent Gibson 	if (num_get != 1) {
12023c0d9c63SKent Gibson 		descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
12033c0d9c63SKent Gibson 		if (!descs)
12043c0d9c63SKent Gibson 			return -ENOMEM;
12053c0d9c63SKent Gibson 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
12063c0d9c63SKent Gibson 			if (lv.mask & BIT_ULL(i)) {
12073c0d9c63SKent Gibson 				descs[didx] = lr->lines[i].desc;
12083c0d9c63SKent Gibson 				didx++;
12093c0d9c63SKent Gibson 			}
12103c0d9c63SKent Gibson 		}
12113c0d9c63SKent Gibson 	}
12123c0d9c63SKent Gibson 	ret = gpiod_get_array_value_complex(false, true, num_get,
12133c0d9c63SKent Gibson 					    descs, NULL, vals);
12143c0d9c63SKent Gibson 
12153c0d9c63SKent Gibson 	if (num_get != 1)
12163c0d9c63SKent Gibson 		kfree(descs);
12173c0d9c63SKent Gibson 	if (ret)
12183c0d9c63SKent Gibson 		return ret;
12193c0d9c63SKent Gibson 
12203c0d9c63SKent Gibson 	lv.bits = 0;
12213c0d9c63SKent Gibson 	for (didx = 0, i = 0; i < lr->num_lines; i++) {
12223c0d9c63SKent Gibson 		if (lv.mask & BIT_ULL(i)) {
122365cff704SKent Gibson 			if (lr->lines[i].sw_debounced)
122465cff704SKent Gibson 				val = debounced_value(&lr->lines[i]);
122565cff704SKent Gibson 			else
122665cff704SKent Gibson 				val = test_bit(didx, vals);
122765cff704SKent Gibson 			if (val)
12283c0d9c63SKent Gibson 				lv.bits |= BIT_ULL(i);
12293c0d9c63SKent Gibson 			didx++;
12303c0d9c63SKent Gibson 		}
12313c0d9c63SKent Gibson 	}
12323c0d9c63SKent Gibson 
12333c0d9c63SKent Gibson 	if (copy_to_user(ip, &lv, sizeof(lv)))
12343c0d9c63SKent Gibson 		return -EFAULT;
12353c0d9c63SKent Gibson 
12363c0d9c63SKent Gibson 	return 0;
12373c0d9c63SKent Gibson }
12383c0d9c63SKent Gibson 
12397b8e00d9SKent Gibson static long linereq_set_values_unlocked(struct linereq *lr,
12407b8e00d9SKent Gibson 					struct gpio_v2_line_values *lv)
12417b8e00d9SKent Gibson {
12427b8e00d9SKent Gibson 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
12437b8e00d9SKent Gibson 	struct gpio_desc **descs;
12447b8e00d9SKent Gibson 	unsigned int i, didx, num_set;
12457b8e00d9SKent Gibson 	int ret;
12467b8e00d9SKent Gibson 
12477b8e00d9SKent Gibson 	bitmap_zero(vals, GPIO_V2_LINES_MAX);
12487b8e00d9SKent Gibson 	for (num_set = 0, i = 0; i < lr->num_lines; i++) {
12497b8e00d9SKent Gibson 		if (lv->mask & BIT_ULL(i)) {
12507b8e00d9SKent Gibson 			if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
12517b8e00d9SKent Gibson 				return -EPERM;
12527b8e00d9SKent Gibson 			if (lv->bits & BIT_ULL(i))
12537b8e00d9SKent Gibson 				__set_bit(num_set, vals);
12547b8e00d9SKent Gibson 			num_set++;
12557b8e00d9SKent Gibson 			descs = &lr->lines[i].desc;
12567b8e00d9SKent Gibson 		}
12577b8e00d9SKent Gibson 	}
12587b8e00d9SKent Gibson 	if (num_set == 0)
12597b8e00d9SKent Gibson 		return -EINVAL;
12607b8e00d9SKent Gibson 
12617b8e00d9SKent Gibson 	if (num_set != 1) {
12627b8e00d9SKent Gibson 		/* build compacted desc array and values */
12637b8e00d9SKent Gibson 		descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
12647b8e00d9SKent Gibson 		if (!descs)
12657b8e00d9SKent Gibson 			return -ENOMEM;
12667b8e00d9SKent Gibson 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
12677b8e00d9SKent Gibson 			if (lv->mask & BIT_ULL(i)) {
12687b8e00d9SKent Gibson 				descs[didx] = lr->lines[i].desc;
12697b8e00d9SKent Gibson 				didx++;
12707b8e00d9SKent Gibson 			}
12717b8e00d9SKent Gibson 		}
12727b8e00d9SKent Gibson 	}
12737b8e00d9SKent Gibson 	ret = gpiod_set_array_value_complex(false, true, num_set,
12747b8e00d9SKent Gibson 					    descs, NULL, vals);
12757b8e00d9SKent Gibson 
12767b8e00d9SKent Gibson 	if (num_set != 1)
12777b8e00d9SKent Gibson 		kfree(descs);
12787b8e00d9SKent Gibson 	return ret;
12797b8e00d9SKent Gibson }
12807b8e00d9SKent Gibson 
12817b8e00d9SKent Gibson static long linereq_set_values(struct linereq *lr, void __user *ip)
12827b8e00d9SKent Gibson {
12837b8e00d9SKent Gibson 	struct gpio_v2_line_values lv;
12847b8e00d9SKent Gibson 	int ret;
12857b8e00d9SKent Gibson 
12867b8e00d9SKent Gibson 	if (copy_from_user(&lv, ip, sizeof(lv)))
12877b8e00d9SKent Gibson 		return -EFAULT;
12887b8e00d9SKent Gibson 
12897b8e00d9SKent Gibson 	mutex_lock(&lr->config_mutex);
12907b8e00d9SKent Gibson 
12917b8e00d9SKent Gibson 	ret = linereq_set_values_unlocked(lr, &lv);
12927b8e00d9SKent Gibson 
12937b8e00d9SKent Gibson 	mutex_unlock(&lr->config_mutex);
12947b8e00d9SKent Gibson 
12957b8e00d9SKent Gibson 	return ret;
12967b8e00d9SKent Gibson }
12977b8e00d9SKent Gibson 
1298a54756cbSKent Gibson static long linereq_set_config_unlocked(struct linereq *lr,
1299a54756cbSKent Gibson 					struct gpio_v2_line_config *lc)
1300a54756cbSKent Gibson {
1301a54756cbSKent Gibson 	struct gpio_desc *desc;
1302a54756cbSKent Gibson 	unsigned int i;
1303a54756cbSKent Gibson 	u64 flags;
1304a54756cbSKent Gibson 	bool polarity_change;
13052068339aSDipen Patel 	bool prev_hte_flag;
1306a54756cbSKent Gibson 	int ret;
1307a54756cbSKent Gibson 
1308a54756cbSKent Gibson 	for (i = 0; i < lr->num_lines; i++) {
1309a54756cbSKent Gibson 		desc = lr->lines[i].desc;
1310a54756cbSKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
1311a54756cbSKent Gibson 		polarity_change =
1312a54756cbSKent Gibson 			(!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) !=
1313a54756cbSKent Gibson 			 ((flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) != 0));
1314a54756cbSKent Gibson 
13152068339aSDipen Patel 		prev_hte_flag = !!test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags);
13162068339aSDipen Patel 
1317a54756cbSKent Gibson 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1318a54756cbSKent Gibson 		/*
1319a54756cbSKent Gibson 		 * Lines have to be requested explicitly for input
1320a54756cbSKent Gibson 		 * or output, else the line will be treated "as is".
1321a54756cbSKent Gibson 		 */
1322a54756cbSKent Gibson 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1323a54756cbSKent Gibson 			int val = gpio_v2_line_config_output_value(lc, i);
1324a54756cbSKent Gibson 
13252068339aSDipen Patel 			edge_detector_stop(&lr->lines[i], prev_hte_flag);
1326a54756cbSKent Gibson 			ret = gpiod_direction_output(desc, val);
1327a54756cbSKent Gibson 			if (ret)
1328a54756cbSKent Gibson 				return ret;
1329a54756cbSKent Gibson 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1330a54756cbSKent Gibson 			ret = gpiod_direction_input(desc);
1331a54756cbSKent Gibson 			if (ret)
1332a54756cbSKent Gibson 				return ret;
1333a54756cbSKent Gibson 
133465cff704SKent Gibson 			ret = edge_detector_update(&lr->lines[i], lc, i,
13352068339aSDipen Patel 					flags, polarity_change, prev_hte_flag);
1336a54756cbSKent Gibson 			if (ret)
1337a54756cbSKent Gibson 				return ret;
1338a54756cbSKent Gibson 		}
1339a54756cbSKent Gibson 
1340a54756cbSKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
1341a54756cbSKent Gibson 					     GPIO_V2_LINE_CHANGED_CONFIG,
1342a54756cbSKent Gibson 					     desc);
1343a54756cbSKent Gibson 	}
1344a54756cbSKent Gibson 	return 0;
1345a54756cbSKent Gibson }
1346a54756cbSKent Gibson 
1347a54756cbSKent Gibson static long linereq_set_config(struct linereq *lr, void __user *ip)
1348a54756cbSKent Gibson {
1349a54756cbSKent Gibson 	struct gpio_v2_line_config lc;
1350a54756cbSKent Gibson 	int ret;
1351a54756cbSKent Gibson 
1352a54756cbSKent Gibson 	if (copy_from_user(&lc, ip, sizeof(lc)))
1353a54756cbSKent Gibson 		return -EFAULT;
1354a54756cbSKent Gibson 
1355a54756cbSKent Gibson 	ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
1356a54756cbSKent Gibson 	if (ret)
1357a54756cbSKent Gibson 		return ret;
1358a54756cbSKent Gibson 
1359a54756cbSKent Gibson 	mutex_lock(&lr->config_mutex);
1360a54756cbSKent Gibson 
1361a54756cbSKent Gibson 	ret = linereq_set_config_unlocked(lr, &lc);
1362a54756cbSKent Gibson 
1363a54756cbSKent Gibson 	mutex_unlock(&lr->config_mutex);
1364a54756cbSKent Gibson 
1365a54756cbSKent Gibson 	return ret;
1366a54756cbSKent Gibson }
1367a54756cbSKent Gibson 
13683c0d9c63SKent Gibson static long linereq_ioctl(struct file *file, unsigned int cmd,
13693c0d9c63SKent Gibson 			  unsigned long arg)
13703c0d9c63SKent Gibson {
13713c0d9c63SKent Gibson 	struct linereq *lr = file->private_data;
13723c0d9c63SKent Gibson 	void __user *ip = (void __user *)arg;
13733c0d9c63SKent Gibson 
13741cef8b50SAndy Shevchenko 	switch (cmd) {
13751cef8b50SAndy Shevchenko 	case GPIO_V2_LINE_GET_VALUES_IOCTL:
13763c0d9c63SKent Gibson 		return linereq_get_values(lr, ip);
13771cef8b50SAndy Shevchenko 	case GPIO_V2_LINE_SET_VALUES_IOCTL:
13787b8e00d9SKent Gibson 		return linereq_set_values(lr, ip);
13791cef8b50SAndy Shevchenko 	case GPIO_V2_LINE_SET_CONFIG_IOCTL:
1380a54756cbSKent Gibson 		return linereq_set_config(lr, ip);
13811cef8b50SAndy Shevchenko 	default:
13823c0d9c63SKent Gibson 		return -EINVAL;
13833c0d9c63SKent Gibson 	}
13841cef8b50SAndy Shevchenko }
13853c0d9c63SKent Gibson 
13863c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
13873c0d9c63SKent Gibson static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
13883c0d9c63SKent Gibson 				 unsigned long arg)
13893c0d9c63SKent Gibson {
13903c0d9c63SKent Gibson 	return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
13913c0d9c63SKent Gibson }
13923c0d9c63SKent Gibson #endif
13933c0d9c63SKent Gibson 
139473e03419SKent Gibson static __poll_t linereq_poll(struct file *file,
139573e03419SKent Gibson 			    struct poll_table_struct *wait)
139673e03419SKent Gibson {
139773e03419SKent Gibson 	struct linereq *lr = file->private_data;
139873e03419SKent Gibson 	__poll_t events = 0;
139973e03419SKent Gibson 
140073e03419SKent Gibson 	poll_wait(file, &lr->wait, wait);
140173e03419SKent Gibson 
140273e03419SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
140373e03419SKent Gibson 						 &lr->wait.lock))
140473e03419SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
140573e03419SKent Gibson 
140673e03419SKent Gibson 	return events;
140773e03419SKent Gibson }
140873e03419SKent Gibson 
140973e03419SKent Gibson static ssize_t linereq_read(struct file *file,
141073e03419SKent Gibson 			    char __user *buf,
141173e03419SKent Gibson 			    size_t count,
141273e03419SKent Gibson 			    loff_t *f_ps)
141373e03419SKent Gibson {
141473e03419SKent Gibson 	struct linereq *lr = file->private_data;
141573e03419SKent Gibson 	struct gpio_v2_line_event le;
141673e03419SKent Gibson 	ssize_t bytes_read = 0;
141773e03419SKent Gibson 	int ret;
141873e03419SKent Gibson 
141973e03419SKent Gibson 	if (count < sizeof(le))
142073e03419SKent Gibson 		return -EINVAL;
142173e03419SKent Gibson 
142273e03419SKent Gibson 	do {
142373e03419SKent Gibson 		spin_lock(&lr->wait.lock);
142473e03419SKent Gibson 		if (kfifo_is_empty(&lr->events)) {
142573e03419SKent Gibson 			if (bytes_read) {
142673e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
142773e03419SKent Gibson 				return bytes_read;
142873e03419SKent Gibson 			}
142973e03419SKent Gibson 
143073e03419SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
143173e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
143273e03419SKent Gibson 				return -EAGAIN;
143373e03419SKent Gibson 			}
143473e03419SKent Gibson 
143573e03419SKent Gibson 			ret = wait_event_interruptible_locked(lr->wait,
143673e03419SKent Gibson 					!kfifo_is_empty(&lr->events));
143773e03419SKent Gibson 			if (ret) {
143873e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
143973e03419SKent Gibson 				return ret;
144073e03419SKent Gibson 			}
144173e03419SKent Gibson 		}
144273e03419SKent Gibson 
144373e03419SKent Gibson 		ret = kfifo_out(&lr->events, &le, 1);
144473e03419SKent Gibson 		spin_unlock(&lr->wait.lock);
144573e03419SKent Gibson 		if (ret != 1) {
144673e03419SKent Gibson 			/*
144773e03419SKent Gibson 			 * This should never happen - we were holding the
144873e03419SKent Gibson 			 * lock from the moment we learned the fifo is no
144973e03419SKent Gibson 			 * longer empty until now.
145073e03419SKent Gibson 			 */
145173e03419SKent Gibson 			ret = -EIO;
145273e03419SKent Gibson 			break;
145373e03419SKent Gibson 		}
145473e03419SKent Gibson 
145573e03419SKent Gibson 		if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
145673e03419SKent Gibson 			return -EFAULT;
145773e03419SKent Gibson 		bytes_read += sizeof(le);
145873e03419SKent Gibson 	} while (count >= bytes_read + sizeof(le));
145973e03419SKent Gibson 
146073e03419SKent Gibson 	return bytes_read;
146173e03419SKent Gibson }
146273e03419SKent Gibson 
14633c0d9c63SKent Gibson static void linereq_free(struct linereq *lr)
14643c0d9c63SKent Gibson {
14653c0d9c63SKent Gibson 	unsigned int i;
1466160d6e40SKent Gibson 	bool hte;
14673c0d9c63SKent Gibson 
14683c0d9c63SKent Gibson 	for (i = 0; i < lr->num_lines; i++) {
1469160d6e40SKent Gibson 		if (lr->lines[i].desc) {
14702068339aSDipen Patel 			hte = !!test_bit(FLAG_EVENT_CLOCK_HTE,
14712068339aSDipen Patel 					 &lr->lines[i].desc->flags);
14722068339aSDipen Patel 			edge_detector_stop(&lr->lines[i], hte);
14733c0d9c63SKent Gibson 			gpiod_free(lr->lines[i].desc);
14743c0d9c63SKent Gibson 		}
1475160d6e40SKent Gibson 	}
147673e03419SKent Gibson 	kfifo_free(&lr->events);
14773c0d9c63SKent Gibson 	kfree(lr->label);
14783c0d9c63SKent Gibson 	put_device(&lr->gdev->dev);
14793c0d9c63SKent Gibson 	kfree(lr);
14803c0d9c63SKent Gibson }
14813c0d9c63SKent Gibson 
14823c0d9c63SKent Gibson static int linereq_release(struct inode *inode, struct file *file)
14833c0d9c63SKent Gibson {
14843c0d9c63SKent Gibson 	struct linereq *lr = file->private_data;
14853c0d9c63SKent Gibson 
14863c0d9c63SKent Gibson 	linereq_free(lr);
14873c0d9c63SKent Gibson 	return 0;
14883c0d9c63SKent Gibson }
14893c0d9c63SKent Gibson 
14903c0d9c63SKent Gibson static const struct file_operations line_fileops = {
14913c0d9c63SKent Gibson 	.release = linereq_release,
149273e03419SKent Gibson 	.read = linereq_read,
149373e03419SKent Gibson 	.poll = linereq_poll,
14943c0d9c63SKent Gibson 	.owner = THIS_MODULE,
14953c0d9c63SKent Gibson 	.llseek = noop_llseek,
14963c0d9c63SKent Gibson 	.unlocked_ioctl = linereq_ioctl,
14973c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
14983c0d9c63SKent Gibson 	.compat_ioctl = linereq_ioctl_compat,
14993c0d9c63SKent Gibson #endif
15003c0d9c63SKent Gibson };
15013c0d9c63SKent Gibson 
15023c0d9c63SKent Gibson static int linereq_create(struct gpio_device *gdev, void __user *ip)
15033c0d9c63SKent Gibson {
15043c0d9c63SKent Gibson 	struct gpio_v2_line_request ulr;
15053c0d9c63SKent Gibson 	struct gpio_v2_line_config *lc;
15063c0d9c63SKent Gibson 	struct linereq *lr;
15073c0d9c63SKent Gibson 	struct file *file;
15083c0d9c63SKent Gibson 	u64 flags;
15093c0d9c63SKent Gibson 	unsigned int i;
15103c0d9c63SKent Gibson 	int fd, ret;
15113c0d9c63SKent Gibson 
15123c0d9c63SKent Gibson 	if (copy_from_user(&ulr, ip, sizeof(ulr)))
15133c0d9c63SKent Gibson 		return -EFAULT;
15143c0d9c63SKent Gibson 
15153c0d9c63SKent Gibson 	if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
15163c0d9c63SKent Gibson 		return -EINVAL;
15173c0d9c63SKent Gibson 
15183c0d9c63SKent Gibson 	if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
15193c0d9c63SKent Gibson 		return -EINVAL;
15203c0d9c63SKent Gibson 
15213c0d9c63SKent Gibson 	lc = &ulr.config;
15223c0d9c63SKent Gibson 	ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
15233c0d9c63SKent Gibson 	if (ret)
15243c0d9c63SKent Gibson 		return ret;
15253c0d9c63SKent Gibson 
15263c0d9c63SKent Gibson 	lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
15273c0d9c63SKent Gibson 	if (!lr)
15283c0d9c63SKent Gibson 		return -ENOMEM;
15293c0d9c63SKent Gibson 
15303c0d9c63SKent Gibson 	lr->gdev = gdev;
15313c0d9c63SKent Gibson 	get_device(&gdev->dev);
15323c0d9c63SKent Gibson 
153365cff704SKent Gibson 	for (i = 0; i < ulr.num_lines; i++) {
153473e03419SKent Gibson 		lr->lines[i].req = lr;
153565cff704SKent Gibson 		WRITE_ONCE(lr->lines[i].sw_debounced, 0);
153665cff704SKent Gibson 		INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
153765cff704SKent Gibson 	}
153873e03419SKent Gibson 
1539f188ac12SKent Gibson 	if (ulr.consumer[0] != '\0') {
15403c0d9c63SKent Gibson 		/* label is only initialized if consumer is set */
1541f188ac12SKent Gibson 		lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1542f188ac12SKent Gibson 				     GFP_KERNEL);
15433c0d9c63SKent Gibson 		if (!lr->label) {
15443c0d9c63SKent Gibson 			ret = -ENOMEM;
15453c0d9c63SKent Gibson 			goto out_free_linereq;
15463c0d9c63SKent Gibson 		}
15473c0d9c63SKent Gibson 	}
15483c0d9c63SKent Gibson 
1549a54756cbSKent Gibson 	mutex_init(&lr->config_mutex);
155073e03419SKent Gibson 	init_waitqueue_head(&lr->wait);
155173e03419SKent Gibson 	lr->event_buffer_size = ulr.event_buffer_size;
155273e03419SKent Gibson 	if (lr->event_buffer_size == 0)
155373e03419SKent Gibson 		lr->event_buffer_size = ulr.num_lines * 16;
155473e03419SKent Gibson 	else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
155573e03419SKent Gibson 		lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
155673e03419SKent Gibson 
155773e03419SKent Gibson 	atomic_set(&lr->seqno, 0);
15583c0d9c63SKent Gibson 	lr->num_lines = ulr.num_lines;
15593c0d9c63SKent Gibson 
15603c0d9c63SKent Gibson 	/* Request each GPIO */
15613c0d9c63SKent Gibson 	for (i = 0; i < ulr.num_lines; i++) {
15623c0d9c63SKent Gibson 		u32 offset = ulr.offsets[i];
15633c0d9c63SKent Gibson 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
15643c0d9c63SKent Gibson 
15653c0d9c63SKent Gibson 		if (IS_ERR(desc)) {
15663c0d9c63SKent Gibson 			ret = PTR_ERR(desc);
15673c0d9c63SKent Gibson 			goto out_free_linereq;
15683c0d9c63SKent Gibson 		}
15693c0d9c63SKent Gibson 
157095a4eed7SAndy Shevchenko 		ret = gpiod_request_user(desc, lr->label);
15713c0d9c63SKent Gibson 		if (ret)
15723c0d9c63SKent Gibson 			goto out_free_linereq;
15733c0d9c63SKent Gibson 
15743c0d9c63SKent Gibson 		lr->lines[i].desc = desc;
15753c0d9c63SKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
15763c0d9c63SKent Gibson 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
15773c0d9c63SKent Gibson 
15783c0d9c63SKent Gibson 		ret = gpiod_set_transitory(desc, false);
15793c0d9c63SKent Gibson 		if (ret < 0)
15803c0d9c63SKent Gibson 			goto out_free_linereq;
15813c0d9c63SKent Gibson 
15823c0d9c63SKent Gibson 		/*
15833c0d9c63SKent Gibson 		 * Lines have to be requested explicitly for input
15843c0d9c63SKent Gibson 		 * or output, else the line will be treated "as is".
15853c0d9c63SKent Gibson 		 */
15863c0d9c63SKent Gibson 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
15873c0d9c63SKent Gibson 			int val = gpio_v2_line_config_output_value(lc, i);
15883c0d9c63SKent Gibson 
15893c0d9c63SKent Gibson 			ret = gpiod_direction_output(desc, val);
15903c0d9c63SKent Gibson 			if (ret)
15913c0d9c63SKent Gibson 				goto out_free_linereq;
15923c0d9c63SKent Gibson 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
15933c0d9c63SKent Gibson 			ret = gpiod_direction_input(desc);
15943c0d9c63SKent Gibson 			if (ret)
15953c0d9c63SKent Gibson 				goto out_free_linereq;
159673e03419SKent Gibson 
159765cff704SKent Gibson 			ret = edge_detector_setup(&lr->lines[i], lc, i,
15982068339aSDipen Patel 				flags & GPIO_V2_LINE_EDGE_FLAGS,
15992068339aSDipen Patel 				flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
160073e03419SKent Gibson 			if (ret)
160173e03419SKent Gibson 				goto out_free_linereq;
16023c0d9c63SKent Gibson 		}
16033c0d9c63SKent Gibson 
16043c0d9c63SKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
1605aad95584SKent Gibson 					     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
16063c0d9c63SKent Gibson 
16073c0d9c63SKent Gibson 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
16083c0d9c63SKent Gibson 			offset);
16093c0d9c63SKent Gibson 	}
16103c0d9c63SKent Gibson 
16113c0d9c63SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
16123c0d9c63SKent Gibson 	if (fd < 0) {
16133c0d9c63SKent Gibson 		ret = fd;
16143c0d9c63SKent Gibson 		goto out_free_linereq;
16153c0d9c63SKent Gibson 	}
16163c0d9c63SKent Gibson 
16173c0d9c63SKent Gibson 	file = anon_inode_getfile("gpio-line", &line_fileops, lr,
16183c0d9c63SKent Gibson 				  O_RDONLY | O_CLOEXEC);
16193c0d9c63SKent Gibson 	if (IS_ERR(file)) {
16203c0d9c63SKent Gibson 		ret = PTR_ERR(file);
16213c0d9c63SKent Gibson 		goto out_put_unused_fd;
16223c0d9c63SKent Gibson 	}
16233c0d9c63SKent Gibson 
16243c0d9c63SKent Gibson 	ulr.fd = fd;
16253c0d9c63SKent Gibson 	if (copy_to_user(ip, &ulr, sizeof(ulr))) {
16263c0d9c63SKent Gibson 		/*
16273c0d9c63SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
16283c0d9c63SKent Gibson 		 * the regular error cleanup path here.
16293c0d9c63SKent Gibson 		 */
16303c0d9c63SKent Gibson 		fput(file);
16313c0d9c63SKent Gibson 		put_unused_fd(fd);
16323c0d9c63SKent Gibson 		return -EFAULT;
16333c0d9c63SKent Gibson 	}
16343c0d9c63SKent Gibson 
16353c0d9c63SKent Gibson 	fd_install(fd, file);
16363c0d9c63SKent Gibson 
16373c0d9c63SKent Gibson 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
16383c0d9c63SKent Gibson 		lr->num_lines);
16393c0d9c63SKent Gibson 
16403c0d9c63SKent Gibson 	return 0;
16413c0d9c63SKent Gibson 
16423c0d9c63SKent Gibson out_put_unused_fd:
16433c0d9c63SKent Gibson 	put_unused_fd(fd);
16443c0d9c63SKent Gibson out_free_linereq:
16453c0d9c63SKent Gibson 	linereq_free(lr);
16463c0d9c63SKent Gibson 	return ret;
16473c0d9c63SKent Gibson }
16483c0d9c63SKent Gibson 
16493c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1650925ca369SKent Gibson 
1651925ca369SKent Gibson /*
1652925ca369SKent Gibson  * GPIO line event management
1653925ca369SKent Gibson  */
1654925ca369SKent Gibson 
1655925ca369SKent Gibson /**
1656925ca369SKent Gibson  * struct lineevent_state - contains the state of a userspace event
1657925ca369SKent Gibson  * @gdev: the GPIO device the event pertains to
1658925ca369SKent Gibson  * @label: consumer label used to tag descriptors
1659925ca369SKent Gibson  * @desc: the GPIO descriptor held by this event
1660925ca369SKent Gibson  * @eflags: the event flags this line was requested with
1661925ca369SKent Gibson  * @irq: the interrupt that trigger in response to events on this GPIO
1662925ca369SKent Gibson  * @wait: wait queue that handles blocking reads of events
1663925ca369SKent Gibson  * @events: KFIFO for the GPIO events
1664925ca369SKent Gibson  * @timestamp: cache for the timestamp storing it between hardirq
1665925ca369SKent Gibson  * and IRQ thread, used to bring the timestamp close to the actual
1666925ca369SKent Gibson  * event
1667925ca369SKent Gibson  */
1668925ca369SKent Gibson struct lineevent_state {
1669925ca369SKent Gibson 	struct gpio_device *gdev;
1670925ca369SKent Gibson 	const char *label;
1671925ca369SKent Gibson 	struct gpio_desc *desc;
1672925ca369SKent Gibson 	u32 eflags;
1673925ca369SKent Gibson 	int irq;
1674925ca369SKent Gibson 	wait_queue_head_t wait;
1675925ca369SKent Gibson 	DECLARE_KFIFO(events, struct gpioevent_data, 16);
1676925ca369SKent Gibson 	u64 timestamp;
1677925ca369SKent Gibson };
1678925ca369SKent Gibson 
1679925ca369SKent Gibson #define GPIOEVENT_REQUEST_VALID_FLAGS \
1680925ca369SKent Gibson 	(GPIOEVENT_REQUEST_RISING_EDGE | \
1681925ca369SKent Gibson 	GPIOEVENT_REQUEST_FALLING_EDGE)
1682925ca369SKent Gibson 
168349bc5279SKent Gibson static __poll_t lineevent_poll(struct file *file,
1684925ca369SKent Gibson 			       struct poll_table_struct *wait)
1685925ca369SKent Gibson {
168649bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1687925ca369SKent Gibson 	__poll_t events = 0;
1688925ca369SKent Gibson 
168949bc5279SKent Gibson 	poll_wait(file, &le->wait, wait);
1690925ca369SKent Gibson 
1691925ca369SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1692925ca369SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
1693925ca369SKent Gibson 
1694925ca369SKent Gibson 	return events;
1695925ca369SKent Gibson }
1696925ca369SKent Gibson 
16975ad284abSAndy Shevchenko struct compat_gpioeevent_data {
16985ad284abSAndy Shevchenko 	compat_u64	timestamp;
16995ad284abSAndy Shevchenko 	u32		id;
17005ad284abSAndy Shevchenko };
17015ad284abSAndy Shevchenko 
170249bc5279SKent Gibson static ssize_t lineevent_read(struct file *file,
1703925ca369SKent Gibson 			      char __user *buf,
1704925ca369SKent Gibson 			      size_t count,
1705925ca369SKent Gibson 			      loff_t *f_ps)
1706925ca369SKent Gibson {
170749bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1708925ca369SKent Gibson 	struct gpioevent_data ge;
1709925ca369SKent Gibson 	ssize_t bytes_read = 0;
17105ad284abSAndy Shevchenko 	ssize_t ge_size;
1711925ca369SKent Gibson 	int ret;
1712925ca369SKent Gibson 
17135ad284abSAndy Shevchenko 	/*
17145ad284abSAndy Shevchenko 	 * When compatible system call is being used the struct gpioevent_data,
17155ad284abSAndy Shevchenko 	 * in case of at least ia32, has different size due to the alignment
17165ad284abSAndy Shevchenko 	 * differences. Because we have first member 64 bits followed by one of
17175ad284abSAndy Shevchenko 	 * 32 bits there is no gap between them. The only difference is the
17185ad284abSAndy Shevchenko 	 * padding at the end of the data structure. Hence, we calculate the
17195ad284abSAndy Shevchenko 	 * actual sizeof() and pass this as an argument to copy_to_user() to
17205ad284abSAndy Shevchenko 	 * drop unneeded bytes from the output.
17215ad284abSAndy Shevchenko 	 */
1722163d1719SAndy Shevchenko 	if (compat_need_64bit_alignment_fixup())
1723163d1719SAndy Shevchenko 		ge_size = sizeof(struct compat_gpioeevent_data);
1724163d1719SAndy Shevchenko 	else
1725163d1719SAndy Shevchenko 		ge_size = sizeof(struct gpioevent_data);
17265ad284abSAndy Shevchenko 	if (count < ge_size)
1727925ca369SKent Gibson 		return -EINVAL;
1728925ca369SKent Gibson 
1729925ca369SKent Gibson 	do {
1730925ca369SKent Gibson 		spin_lock(&le->wait.lock);
1731925ca369SKent Gibson 		if (kfifo_is_empty(&le->events)) {
1732925ca369SKent Gibson 			if (bytes_read) {
1733925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1734925ca369SKent Gibson 				return bytes_read;
1735925ca369SKent Gibson 			}
1736925ca369SKent Gibson 
173749bc5279SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
1738925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1739925ca369SKent Gibson 				return -EAGAIN;
1740925ca369SKent Gibson 			}
1741925ca369SKent Gibson 
1742925ca369SKent Gibson 			ret = wait_event_interruptible_locked(le->wait,
1743925ca369SKent Gibson 					!kfifo_is_empty(&le->events));
1744925ca369SKent Gibson 			if (ret) {
1745925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1746925ca369SKent Gibson 				return ret;
1747925ca369SKent Gibson 			}
1748925ca369SKent Gibson 		}
1749925ca369SKent Gibson 
1750925ca369SKent Gibson 		ret = kfifo_out(&le->events, &ge, 1);
1751925ca369SKent Gibson 		spin_unlock(&le->wait.lock);
1752925ca369SKent Gibson 		if (ret != 1) {
1753925ca369SKent Gibson 			/*
1754925ca369SKent Gibson 			 * This should never happen - we were holding the lock
1755925ca369SKent Gibson 			 * from the moment we learned the fifo is no longer
1756925ca369SKent Gibson 			 * empty until now.
1757925ca369SKent Gibson 			 */
1758925ca369SKent Gibson 			ret = -EIO;
1759925ca369SKent Gibson 			break;
1760925ca369SKent Gibson 		}
1761925ca369SKent Gibson 
17625ad284abSAndy Shevchenko 		if (copy_to_user(buf + bytes_read, &ge, ge_size))
1763925ca369SKent Gibson 			return -EFAULT;
17645ad284abSAndy Shevchenko 		bytes_read += ge_size;
17655ad284abSAndy Shevchenko 	} while (count >= bytes_read + ge_size);
1766925ca369SKent Gibson 
1767925ca369SKent Gibson 	return bytes_read;
1768925ca369SKent Gibson }
1769925ca369SKent Gibson 
177046824272SKent Gibson static void lineevent_free(struct lineevent_state *le)
1771925ca369SKent Gibson {
177246824272SKent Gibson 	if (le->irq)
1773925ca369SKent Gibson 		free_irq(le->irq, le);
177446824272SKent Gibson 	if (le->desc)
1775925ca369SKent Gibson 		gpiod_free(le->desc);
1776925ca369SKent Gibson 	kfree(le->label);
177746824272SKent Gibson 	put_device(&le->gdev->dev);
1778925ca369SKent Gibson 	kfree(le);
177946824272SKent Gibson }
178046824272SKent Gibson 
178146824272SKent Gibson static int lineevent_release(struct inode *inode, struct file *file)
178246824272SKent Gibson {
178346824272SKent Gibson 	lineevent_free(file->private_data);
1784925ca369SKent Gibson 	return 0;
1785925ca369SKent Gibson }
1786925ca369SKent Gibson 
178749bc5279SKent Gibson static long lineevent_ioctl(struct file *file, unsigned int cmd,
1788925ca369SKent Gibson 			    unsigned long arg)
1789925ca369SKent Gibson {
179049bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1791925ca369SKent Gibson 	void __user *ip = (void __user *)arg;
1792925ca369SKent Gibson 	struct gpiohandle_data ghd;
1793925ca369SKent Gibson 
1794925ca369SKent Gibson 	/*
1795925ca369SKent Gibson 	 * We can get the value for an event line but not set it,
1796925ca369SKent Gibson 	 * because it is input by definition.
1797925ca369SKent Gibson 	 */
1798925ca369SKent Gibson 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
1799925ca369SKent Gibson 		int val;
1800925ca369SKent Gibson 
1801925ca369SKent Gibson 		memset(&ghd, 0, sizeof(ghd));
1802925ca369SKent Gibson 
1803925ca369SKent Gibson 		val = gpiod_get_value_cansleep(le->desc);
1804925ca369SKent Gibson 		if (val < 0)
1805925ca369SKent Gibson 			return val;
1806925ca369SKent Gibson 		ghd.values[0] = val;
1807925ca369SKent Gibson 
1808925ca369SKent Gibson 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
1809925ca369SKent Gibson 			return -EFAULT;
1810925ca369SKent Gibson 
1811925ca369SKent Gibson 		return 0;
1812925ca369SKent Gibson 	}
1813925ca369SKent Gibson 	return -EINVAL;
1814925ca369SKent Gibson }
1815925ca369SKent Gibson 
1816925ca369SKent Gibson #ifdef CONFIG_COMPAT
181749bc5279SKent Gibson static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
1818925ca369SKent Gibson 				   unsigned long arg)
1819925ca369SKent Gibson {
182049bc5279SKent Gibson 	return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1821925ca369SKent Gibson }
1822925ca369SKent Gibson #endif
1823925ca369SKent Gibson 
1824925ca369SKent Gibson static const struct file_operations lineevent_fileops = {
1825925ca369SKent Gibson 	.release = lineevent_release,
1826925ca369SKent Gibson 	.read = lineevent_read,
1827925ca369SKent Gibson 	.poll = lineevent_poll,
1828925ca369SKent Gibson 	.owner = THIS_MODULE,
1829925ca369SKent Gibson 	.llseek = noop_llseek,
1830925ca369SKent Gibson 	.unlocked_ioctl = lineevent_ioctl,
1831925ca369SKent Gibson #ifdef CONFIG_COMPAT
1832925ca369SKent Gibson 	.compat_ioctl = lineevent_ioctl_compat,
1833925ca369SKent Gibson #endif
1834925ca369SKent Gibson };
1835925ca369SKent Gibson 
1836925ca369SKent Gibson static irqreturn_t lineevent_irq_thread(int irq, void *p)
1837925ca369SKent Gibson {
1838925ca369SKent Gibson 	struct lineevent_state *le = p;
1839925ca369SKent Gibson 	struct gpioevent_data ge;
1840925ca369SKent Gibson 	int ret;
1841925ca369SKent Gibson 
1842925ca369SKent Gibson 	/* Do not leak kernel stack to userspace */
1843925ca369SKent Gibson 	memset(&ge, 0, sizeof(ge));
1844925ca369SKent Gibson 
1845925ca369SKent Gibson 	/*
1846925ca369SKent Gibson 	 * We may be running from a nested threaded interrupt in which case
1847925ca369SKent Gibson 	 * we didn't get the timestamp from lineevent_irq_handler().
1848925ca369SKent Gibson 	 */
1849925ca369SKent Gibson 	if (!le->timestamp)
1850925ca369SKent Gibson 		ge.timestamp = ktime_get_ns();
1851925ca369SKent Gibson 	else
1852925ca369SKent Gibson 		ge.timestamp = le->timestamp;
1853925ca369SKent Gibson 
1854925ca369SKent Gibson 	if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
1855925ca369SKent Gibson 	    && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1856925ca369SKent Gibson 		int level = gpiod_get_value_cansleep(le->desc);
1857925ca369SKent Gibson 
1858925ca369SKent Gibson 		if (level)
1859925ca369SKent Gibson 			/* Emit low-to-high event */
1860925ca369SKent Gibson 			ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1861925ca369SKent Gibson 		else
1862925ca369SKent Gibson 			/* Emit high-to-low event */
1863925ca369SKent Gibson 			ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1864925ca369SKent Gibson 	} else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
1865925ca369SKent Gibson 		/* Emit low-to-high event */
1866925ca369SKent Gibson 		ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1867925ca369SKent Gibson 	} else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1868925ca369SKent Gibson 		/* Emit high-to-low event */
1869925ca369SKent Gibson 		ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1870925ca369SKent Gibson 	} else {
1871925ca369SKent Gibson 		return IRQ_NONE;
1872925ca369SKent Gibson 	}
1873925ca369SKent Gibson 
1874925ca369SKent Gibson 	ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
1875925ca369SKent Gibson 					    1, &le->wait.lock);
1876925ca369SKent Gibson 	if (ret)
1877925ca369SKent Gibson 		wake_up_poll(&le->wait, EPOLLIN);
1878925ca369SKent Gibson 	else
1879925ca369SKent Gibson 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
1880925ca369SKent Gibson 
1881925ca369SKent Gibson 	return IRQ_HANDLED;
1882925ca369SKent Gibson }
1883925ca369SKent Gibson 
1884925ca369SKent Gibson static irqreturn_t lineevent_irq_handler(int irq, void *p)
1885925ca369SKent Gibson {
1886925ca369SKent Gibson 	struct lineevent_state *le = p;
1887925ca369SKent Gibson 
1888925ca369SKent Gibson 	/*
1889925ca369SKent Gibson 	 * Just store the timestamp in hardirq context so we get it as
1890925ca369SKent Gibson 	 * close in time as possible to the actual event.
1891925ca369SKent Gibson 	 */
1892925ca369SKent Gibson 	le->timestamp = ktime_get_ns();
1893925ca369SKent Gibson 
1894925ca369SKent Gibson 	return IRQ_WAKE_THREAD;
1895925ca369SKent Gibson }
1896925ca369SKent Gibson 
1897925ca369SKent Gibson static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1898925ca369SKent Gibson {
1899925ca369SKent Gibson 	struct gpioevent_request eventreq;
1900925ca369SKent Gibson 	struct lineevent_state *le;
1901925ca369SKent Gibson 	struct gpio_desc *desc;
1902925ca369SKent Gibson 	struct file *file;
1903925ca369SKent Gibson 	u32 offset;
1904925ca369SKent Gibson 	u32 lflags;
1905925ca369SKent Gibson 	u32 eflags;
1906925ca369SKent Gibson 	int fd;
1907925ca369SKent Gibson 	int ret;
190846824272SKent Gibson 	int irq, irqflags = 0;
1909925ca369SKent Gibson 
1910925ca369SKent Gibson 	if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1911925ca369SKent Gibson 		return -EFAULT;
1912925ca369SKent Gibson 
1913925ca369SKent Gibson 	offset = eventreq.lineoffset;
1914925ca369SKent Gibson 	lflags = eventreq.handleflags;
1915925ca369SKent Gibson 	eflags = eventreq.eventflags;
1916925ca369SKent Gibson 
1917925ca369SKent Gibson 	desc = gpiochip_get_desc(gdev->chip, offset);
1918925ca369SKent Gibson 	if (IS_ERR(desc))
1919925ca369SKent Gibson 		return PTR_ERR(desc);
1920925ca369SKent Gibson 
1921925ca369SKent Gibson 	/* Return an error if a unknown flag is set */
1922925ca369SKent Gibson 	if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1923925ca369SKent Gibson 	    (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1924925ca369SKent Gibson 		return -EINVAL;
1925925ca369SKent Gibson 
1926925ca369SKent Gibson 	/* This is just wrong: we don't look for events on output lines */
1927925ca369SKent Gibson 	if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1928925ca369SKent Gibson 	    (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1929925ca369SKent Gibson 	    (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1930925ca369SKent Gibson 		return -EINVAL;
1931925ca369SKent Gibson 
1932925ca369SKent Gibson 	/* Only one bias flag can be set. */
1933925ca369SKent Gibson 	if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1934925ca369SKent Gibson 	     (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1935925ca369SKent Gibson 			GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1936925ca369SKent Gibson 	    ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1937925ca369SKent Gibson 	     (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1938925ca369SKent Gibson 		return -EINVAL;
1939925ca369SKent Gibson 
1940925ca369SKent Gibson 	le = kzalloc(sizeof(*le), GFP_KERNEL);
1941925ca369SKent Gibson 	if (!le)
1942925ca369SKent Gibson 		return -ENOMEM;
1943925ca369SKent Gibson 	le->gdev = gdev;
1944925ca369SKent Gibson 	get_device(&gdev->dev);
1945925ca369SKent Gibson 
1946f188ac12SKent Gibson 	if (eventreq.consumer_label[0] != '\0') {
1947f188ac12SKent Gibson 		/* label is only initialized if consumer_label is set */
1948f188ac12SKent Gibson 		le->label = kstrndup(eventreq.consumer_label,
1949f188ac12SKent Gibson 				     sizeof(eventreq.consumer_label) - 1,
1950925ca369SKent Gibson 				     GFP_KERNEL);
1951925ca369SKent Gibson 		if (!le->label) {
1952925ca369SKent Gibson 			ret = -ENOMEM;
1953925ca369SKent Gibson 			goto out_free_le;
1954925ca369SKent Gibson 		}
1955925ca369SKent Gibson 	}
1956925ca369SKent Gibson 
195795a4eed7SAndy Shevchenko 	ret = gpiod_request_user(desc, le->label);
1958925ca369SKent Gibson 	if (ret)
195946824272SKent Gibson 		goto out_free_le;
1960925ca369SKent Gibson 	le->desc = desc;
1961925ca369SKent Gibson 	le->eflags = eflags;
1962925ca369SKent Gibson 
1963c274b58aSKent Gibson 	linehandle_flags_to_desc_flags(lflags, &desc->flags);
1964925ca369SKent Gibson 
1965925ca369SKent Gibson 	ret = gpiod_direction_input(desc);
1966925ca369SKent Gibson 	if (ret)
196746824272SKent Gibson 		goto out_free_le;
1968925ca369SKent Gibson 
19696accc376SKent Gibson 	blocking_notifier_call_chain(&desc->gdev->notifier,
1970aad95584SKent Gibson 				     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1971925ca369SKent Gibson 
197246824272SKent Gibson 	irq = gpiod_to_irq(desc);
197346824272SKent Gibson 	if (irq <= 0) {
1974925ca369SKent Gibson 		ret = -ENODEV;
197546824272SKent Gibson 		goto out_free_le;
1976925ca369SKent Gibson 	}
197746824272SKent Gibson 	le->irq = irq;
1978925ca369SKent Gibson 
1979925ca369SKent Gibson 	if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
1980925ca369SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1981925ca369SKent Gibson 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1982925ca369SKent Gibson 	if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
1983925ca369SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1984925ca369SKent Gibson 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1985925ca369SKent Gibson 	irqflags |= IRQF_ONESHOT;
1986925ca369SKent Gibson 
1987925ca369SKent Gibson 	INIT_KFIFO(le->events);
1988925ca369SKent Gibson 	init_waitqueue_head(&le->wait);
1989925ca369SKent Gibson 
1990925ca369SKent Gibson 	/* Request a thread to read the events */
1991925ca369SKent Gibson 	ret = request_threaded_irq(le->irq,
1992925ca369SKent Gibson 				   lineevent_irq_handler,
1993925ca369SKent Gibson 				   lineevent_irq_thread,
1994925ca369SKent Gibson 				   irqflags,
1995925ca369SKent Gibson 				   le->label,
1996925ca369SKent Gibson 				   le);
1997925ca369SKent Gibson 	if (ret)
199846824272SKent Gibson 		goto out_free_le;
1999925ca369SKent Gibson 
2000925ca369SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
2001925ca369SKent Gibson 	if (fd < 0) {
2002925ca369SKent Gibson 		ret = fd;
200346824272SKent Gibson 		goto out_free_le;
2004925ca369SKent Gibson 	}
2005925ca369SKent Gibson 
2006925ca369SKent Gibson 	file = anon_inode_getfile("gpio-event",
2007925ca369SKent Gibson 				  &lineevent_fileops,
2008925ca369SKent Gibson 				  le,
2009925ca369SKent Gibson 				  O_RDONLY | O_CLOEXEC);
2010925ca369SKent Gibson 	if (IS_ERR(file)) {
2011925ca369SKent Gibson 		ret = PTR_ERR(file);
2012925ca369SKent Gibson 		goto out_put_unused_fd;
2013925ca369SKent Gibson 	}
2014925ca369SKent Gibson 
2015925ca369SKent Gibson 	eventreq.fd = fd;
2016925ca369SKent Gibson 	if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
2017925ca369SKent Gibson 		/*
2018925ca369SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
2019925ca369SKent Gibson 		 * the regular error cleanup path here.
2020925ca369SKent Gibson 		 */
2021925ca369SKent Gibson 		fput(file);
2022925ca369SKent Gibson 		put_unused_fd(fd);
2023925ca369SKent Gibson 		return -EFAULT;
2024925ca369SKent Gibson 	}
2025925ca369SKent Gibson 
2026925ca369SKent Gibson 	fd_install(fd, file);
2027925ca369SKent Gibson 
2028925ca369SKent Gibson 	return 0;
2029925ca369SKent Gibson 
2030925ca369SKent Gibson out_put_unused_fd:
2031925ca369SKent Gibson 	put_unused_fd(fd);
2032925ca369SKent Gibson out_free_le:
203346824272SKent Gibson 	lineevent_free(le);
2034925ca369SKent Gibson 	return ret;
2035925ca369SKent Gibson }
2036925ca369SKent Gibson 
2037aad95584SKent Gibson static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
2038aad95584SKent Gibson 				    struct gpioline_info *info_v1)
2039aad95584SKent Gibson {
2040aad95584SKent Gibson 	u64 flagsv2 = info_v2->flags;
2041aad95584SKent Gibson 
2042aad95584SKent Gibson 	memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
2043aad95584SKent Gibson 	memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
2044aad95584SKent Gibson 	info_v1->line_offset = info_v2->offset;
2045aad95584SKent Gibson 	info_v1->flags = 0;
2046aad95584SKent Gibson 
2047aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
2048aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_KERNEL;
2049aad95584SKent Gibson 
2050aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
2051aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
2052aad95584SKent Gibson 
2053aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
2054aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
2055aad95584SKent Gibson 
2056aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
2057aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
2058aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
2059aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
2060aad95584SKent Gibson 
2061aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
2062aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
2063aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
2064aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
2065aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
2066aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
2067aad95584SKent Gibson }
2068aad95584SKent Gibson 
2069aad95584SKent Gibson static void gpio_v2_line_info_changed_to_v1(
2070aad95584SKent Gibson 		struct gpio_v2_line_info_changed *lic_v2,
2071aad95584SKent Gibson 		struct gpioline_info_changed *lic_v1)
2072aad95584SKent Gibson {
2073cb8f63b8SGabriel Knezek 	memset(lic_v1, 0, sizeof(*lic_v1));
2074aad95584SKent Gibson 	gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
2075aad95584SKent Gibson 	lic_v1->timestamp = lic_v2->timestamp_ns;
2076aad95584SKent Gibson 	lic_v1->event_type = lic_v2->event_type;
2077aad95584SKent Gibson }
2078aad95584SKent Gibson 
20793c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
20803c0d9c63SKent Gibson 
2081925ca369SKent Gibson static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
2082aad95584SKent Gibson 				  struct gpio_v2_line_info *info)
2083925ca369SKent Gibson {
2084925ca369SKent Gibson 	struct gpio_chip *gc = desc->gdev->chip;
2085925ca369SKent Gibson 	bool ok_for_pinctrl;
2086925ca369SKent Gibson 	unsigned long flags;
208765cff704SKent Gibson 	u32 debounce_period_us;
208865cff704SKent Gibson 	unsigned int num_attrs = 0;
2089925ca369SKent Gibson 
209069e4e136SKent Gibson 	memset(info, 0, sizeof(*info));
2091aad95584SKent Gibson 	info->offset = gpio_chip_hwgpio(desc);
2092925ca369SKent Gibson 
2093925ca369SKent Gibson 	/*
2094925ca369SKent Gibson 	 * This function takes a mutex so we must check this before taking
2095925ca369SKent Gibson 	 * the spinlock.
2096925ca369SKent Gibson 	 *
2097925ca369SKent Gibson 	 * FIXME: find a non-racy way to retrieve this information. Maybe a
2098925ca369SKent Gibson 	 * lock common to both frameworks?
2099925ca369SKent Gibson 	 */
2100925ca369SKent Gibson 	ok_for_pinctrl =
2101aad95584SKent Gibson 		pinctrl_gpio_can_use_line(gc->base + info->offset);
2102925ca369SKent Gibson 
2103925ca369SKent Gibson 	spin_lock_irqsave(&gpio_lock, flags);
2104925ca369SKent Gibson 
210569e4e136SKent Gibson 	if (desc->name)
210669e4e136SKent Gibson 		strscpy(info->name, desc->name, sizeof(info->name));
2107925ca369SKent Gibson 
210869e4e136SKent Gibson 	if (desc->label)
210969e4e136SKent Gibson 		strscpy(info->consumer, desc->label, sizeof(info->consumer));
2110925ca369SKent Gibson 
2111925ca369SKent Gibson 	/*
2112925ca369SKent Gibson 	 * Userspace only need to know that the kernel is using this GPIO so
2113925ca369SKent Gibson 	 * it can't use it.
2114925ca369SKent Gibson 	 */
2115925ca369SKent Gibson 	info->flags = 0;
2116925ca369SKent Gibson 	if (test_bit(FLAG_REQUESTED, &desc->flags) ||
2117925ca369SKent Gibson 	    test_bit(FLAG_IS_HOGGED, &desc->flags) ||
2118925ca369SKent Gibson 	    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
2119925ca369SKent Gibson 	    test_bit(FLAG_EXPORT, &desc->flags) ||
2120925ca369SKent Gibson 	    test_bit(FLAG_SYSFS, &desc->flags) ||
2121a0db197fSMarc Zyngier 	    !gpiochip_line_is_valid(gc, info->offset) ||
2122925ca369SKent Gibson 	    !ok_for_pinctrl)
2123aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_USED;
2124aad95584SKent Gibson 
2125925ca369SKent Gibson 	if (test_bit(FLAG_IS_OUT, &desc->flags))
2126aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
2127aad95584SKent Gibson 	else
2128aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_INPUT;
2129aad95584SKent Gibson 
2130925ca369SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2131aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
2132aad95584SKent Gibson 
2133925ca369SKent Gibson 	if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2134aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
2135925ca369SKent Gibson 	if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2136aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
2137aad95584SKent Gibson 
2138925ca369SKent Gibson 	if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2139aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
2140925ca369SKent Gibson 	if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2141aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
2142925ca369SKent Gibson 	if (test_bit(FLAG_PULL_UP, &desc->flags))
2143aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
2144925ca369SKent Gibson 
214573e03419SKent Gibson 	if (test_bit(FLAG_EDGE_RISING, &desc->flags))
214673e03419SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
214773e03419SKent Gibson 	if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
214873e03419SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
214973e03419SKent Gibson 
215026d060e4SKent Gibson 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
215126d060e4SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
21522068339aSDipen Patel 	else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags))
21532068339aSDipen Patel 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
215426d060e4SKent Gibson 
215565cff704SKent Gibson 	debounce_period_us = READ_ONCE(desc->debounce_period_us);
215665cff704SKent Gibson 	if (debounce_period_us) {
215765cff704SKent Gibson 		info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
215865cff704SKent Gibson 		info->attrs[num_attrs].debounce_period_us = debounce_period_us;
215965cff704SKent Gibson 		num_attrs++;
216065cff704SKent Gibson 	}
216165cff704SKent Gibson 	info->num_attrs = num_attrs;
2162925ca369SKent Gibson 
2163925ca369SKent Gibson 	spin_unlock_irqrestore(&gpio_lock, flags);
2164925ca369SKent Gibson }
2165925ca369SKent Gibson 
2166925ca369SKent Gibson struct gpio_chardev_data {
2167925ca369SKent Gibson 	struct gpio_device *gdev;
2168925ca369SKent Gibson 	wait_queue_head_t wait;
2169aad95584SKent Gibson 	DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
2170925ca369SKent Gibson 	struct notifier_block lineinfo_changed_nb;
2171925ca369SKent Gibson 	unsigned long *watched_lines;
2172aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2173aad95584SKent Gibson 	atomic_t watch_abi_version;
2174aad95584SKent Gibson #endif
2175925ca369SKent Gibson };
2176925ca369SKent Gibson 
21772e202ad8SKent Gibson static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
21782e202ad8SKent Gibson {
21792e202ad8SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
21802e202ad8SKent Gibson 	struct gpiochip_info chipinfo;
21812e202ad8SKent Gibson 
21822e202ad8SKent Gibson 	memset(&chipinfo, 0, sizeof(chipinfo));
21832e202ad8SKent Gibson 
21842e202ad8SKent Gibson 	strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
21852e202ad8SKent Gibson 	strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
21862e202ad8SKent Gibson 	chipinfo.lines = gdev->ngpio;
21872e202ad8SKent Gibson 	if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
21882e202ad8SKent Gibson 		return -EFAULT;
21892e202ad8SKent Gibson 	return 0;
21902e202ad8SKent Gibson }
21912e202ad8SKent Gibson 
2192aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2193aad95584SKent Gibson /*
2194aad95584SKent Gibson  * returns 0 if the versions match, else the previously selected ABI version
2195aad95584SKent Gibson  */
2196aad95584SKent Gibson static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
2197aad95584SKent Gibson 				       unsigned int version)
2198aad95584SKent Gibson {
2199aad95584SKent Gibson 	int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
2200aad95584SKent Gibson 
2201aad95584SKent Gibson 	if (abiv == version)
2202aad95584SKent Gibson 		return 0;
2203aad95584SKent Gibson 
2204aad95584SKent Gibson 	return abiv;
2205aad95584SKent Gibson }
22062e202ad8SKent Gibson 
22072e202ad8SKent Gibson static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
22082e202ad8SKent Gibson 			   bool watch)
22092e202ad8SKent Gibson {
22102e202ad8SKent Gibson 	struct gpio_desc *desc;
22112e202ad8SKent Gibson 	struct gpioline_info lineinfo;
22122e202ad8SKent Gibson 	struct gpio_v2_line_info lineinfo_v2;
22132e202ad8SKent Gibson 
22142e202ad8SKent Gibson 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
22152e202ad8SKent Gibson 		return -EFAULT;
22162e202ad8SKent Gibson 
22172e202ad8SKent Gibson 	/* this doubles as a range check on line_offset */
22182e202ad8SKent Gibson 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset);
22192e202ad8SKent Gibson 	if (IS_ERR(desc))
22202e202ad8SKent Gibson 		return PTR_ERR(desc);
22212e202ad8SKent Gibson 
22222e202ad8SKent Gibson 	if (watch) {
22232e202ad8SKent Gibson 		if (lineinfo_ensure_abi_version(cdev, 1))
22242e202ad8SKent Gibson 			return -EPERM;
22252e202ad8SKent Gibson 
22262e202ad8SKent Gibson 		if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
22272e202ad8SKent Gibson 			return -EBUSY;
22282e202ad8SKent Gibson 	}
22292e202ad8SKent Gibson 
22302e202ad8SKent Gibson 	gpio_desc_to_lineinfo(desc, &lineinfo_v2);
22312e202ad8SKent Gibson 	gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
22322e202ad8SKent Gibson 
22332e202ad8SKent Gibson 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
22342e202ad8SKent Gibson 		if (watch)
22352e202ad8SKent Gibson 			clear_bit(lineinfo.line_offset, cdev->watched_lines);
22362e202ad8SKent Gibson 		return -EFAULT;
22372e202ad8SKent Gibson 	}
22382e202ad8SKent Gibson 
22392e202ad8SKent Gibson 	return 0;
22402e202ad8SKent Gibson }
2241aad95584SKent Gibson #endif
2242aad95584SKent Gibson 
2243aad95584SKent Gibson static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
2244aad95584SKent Gibson 			bool watch)
2245aad95584SKent Gibson {
2246aad95584SKent Gibson 	struct gpio_desc *desc;
2247aad95584SKent Gibson 	struct gpio_v2_line_info lineinfo;
2248aad95584SKent Gibson 
2249aad95584SKent Gibson 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2250aad95584SKent Gibson 		return -EFAULT;
2251aad95584SKent Gibson 
2252aad95584SKent Gibson 	if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2253aad95584SKent Gibson 		return -EINVAL;
2254aad95584SKent Gibson 
2255aad95584SKent Gibson 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2256aad95584SKent Gibson 	if (IS_ERR(desc))
2257aad95584SKent Gibson 		return PTR_ERR(desc);
2258aad95584SKent Gibson 
2259aad95584SKent Gibson 	if (watch) {
2260aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2261aad95584SKent Gibson 		if (lineinfo_ensure_abi_version(cdev, 2))
2262aad95584SKent Gibson 			return -EPERM;
2263aad95584SKent Gibson #endif
2264aad95584SKent Gibson 		if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2265aad95584SKent Gibson 			return -EBUSY;
2266aad95584SKent Gibson 	}
2267aad95584SKent Gibson 	gpio_desc_to_lineinfo(desc, &lineinfo);
2268aad95584SKent Gibson 
2269aad95584SKent Gibson 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2270aad95584SKent Gibson 		if (watch)
2271aad95584SKent Gibson 			clear_bit(lineinfo.offset, cdev->watched_lines);
2272aad95584SKent Gibson 		return -EFAULT;
2273aad95584SKent Gibson 	}
2274aad95584SKent Gibson 
2275aad95584SKent Gibson 	return 0;
2276aad95584SKent Gibson }
2277aad95584SKent Gibson 
22782e202ad8SKent Gibson static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
2279925ca369SKent Gibson {
2280925ca369SKent Gibson 	__u32 offset;
2281925ca369SKent Gibson 
2282925ca369SKent Gibson 	if (copy_from_user(&offset, ip, sizeof(offset)))
2283925ca369SKent Gibson 		return -EFAULT;
2284925ca369SKent Gibson 
22851bf7ba40SKent Gibson 	if (offset >= cdev->gdev->ngpio)
22861bf7ba40SKent Gibson 		return -EINVAL;
2287925ca369SKent Gibson 
22881bf7ba40SKent Gibson 	if (!test_and_clear_bit(offset, cdev->watched_lines))
2289925ca369SKent Gibson 		return -EBUSY;
2290925ca369SKent Gibson 
2291925ca369SKent Gibson 	return 0;
2292925ca369SKent Gibson }
22932e202ad8SKent Gibson 
22942e202ad8SKent Gibson /*
22952e202ad8SKent Gibson  * gpio_ioctl() - ioctl handler for the GPIO chardev
22962e202ad8SKent Gibson  */
22972e202ad8SKent Gibson static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
22982e202ad8SKent Gibson {
22992e202ad8SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
23002e202ad8SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
23012e202ad8SKent Gibson 	void __user *ip = (void __user *)arg;
23022e202ad8SKent Gibson 
23032e202ad8SKent Gibson 	/* We fail any subsequent ioctl():s when the chip is gone */
23042e202ad8SKent Gibson 	if (!gdev->chip)
23052e202ad8SKent Gibson 		return -ENODEV;
23062e202ad8SKent Gibson 
23072e202ad8SKent Gibson 	/* Fill in the struct and pass to userspace */
23081cef8b50SAndy Shevchenko 	switch (cmd) {
23091cef8b50SAndy Shevchenko 	case GPIO_GET_CHIPINFO_IOCTL:
23102e202ad8SKent Gibson 		return chipinfo_get(cdev, ip);
23112e202ad8SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
23121cef8b50SAndy Shevchenko 	case GPIO_GET_LINEHANDLE_IOCTL:
23132e202ad8SKent Gibson 		return linehandle_create(gdev, ip);
23141cef8b50SAndy Shevchenko 	case GPIO_GET_LINEEVENT_IOCTL:
23152e202ad8SKent Gibson 		return lineevent_create(gdev, ip);
23161cef8b50SAndy Shevchenko 	case GPIO_GET_LINEINFO_IOCTL:
23171cef8b50SAndy Shevchenko 		return lineinfo_get_v1(cdev, ip, false);
23181cef8b50SAndy Shevchenko 	case GPIO_GET_LINEINFO_WATCH_IOCTL:
23191cef8b50SAndy Shevchenko 		return lineinfo_get_v1(cdev, ip, true);
23202e202ad8SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
23211cef8b50SAndy Shevchenko 	case GPIO_V2_GET_LINEINFO_IOCTL:
23221cef8b50SAndy Shevchenko 		return lineinfo_get(cdev, ip, false);
23231cef8b50SAndy Shevchenko 	case GPIO_V2_GET_LINEINFO_WATCH_IOCTL:
23241cef8b50SAndy Shevchenko 		return lineinfo_get(cdev, ip, true);
23251cef8b50SAndy Shevchenko 	case GPIO_V2_GET_LINE_IOCTL:
23262e202ad8SKent Gibson 		return linereq_create(gdev, ip);
23271cef8b50SAndy Shevchenko 	case GPIO_GET_LINEINFO_UNWATCH_IOCTL:
23282e202ad8SKent Gibson 		return lineinfo_unwatch(cdev, ip);
23291cef8b50SAndy Shevchenko 	default:
2330925ca369SKent Gibson 		return -EINVAL;
2331925ca369SKent Gibson 	}
23321cef8b50SAndy Shevchenko }
2333925ca369SKent Gibson 
2334925ca369SKent Gibson #ifdef CONFIG_COMPAT
233549bc5279SKent Gibson static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2336925ca369SKent Gibson 			      unsigned long arg)
2337925ca369SKent Gibson {
233849bc5279SKent Gibson 	return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2339925ca369SKent Gibson }
2340925ca369SKent Gibson #endif
2341925ca369SKent Gibson 
2342925ca369SKent Gibson static struct gpio_chardev_data *
2343925ca369SKent Gibson to_gpio_chardev_data(struct notifier_block *nb)
2344925ca369SKent Gibson {
2345925ca369SKent Gibson 	return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2346925ca369SKent Gibson }
2347925ca369SKent Gibson 
2348925ca369SKent Gibson static int lineinfo_changed_notify(struct notifier_block *nb,
2349925ca369SKent Gibson 				   unsigned long action, void *data)
2350925ca369SKent Gibson {
2351e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
2352aad95584SKent Gibson 	struct gpio_v2_line_info_changed chg;
2353925ca369SKent Gibson 	struct gpio_desc *desc = data;
2354925ca369SKent Gibson 	int ret;
2355925ca369SKent Gibson 
2356e2b781c5SKent Gibson 	if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2357925ca369SKent Gibson 		return NOTIFY_DONE;
2358925ca369SKent Gibson 
2359925ca369SKent Gibson 	memset(&chg, 0, sizeof(chg));
2360925ca369SKent Gibson 	chg.event_type = action;
2361aad95584SKent Gibson 	chg.timestamp_ns = ktime_get_ns();
2362925ca369SKent Gibson 	gpio_desc_to_lineinfo(desc, &chg.info);
2363925ca369SKent Gibson 
2364e2b781c5SKent Gibson 	ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2365925ca369SKent Gibson 	if (ret)
2366e2b781c5SKent Gibson 		wake_up_poll(&cdev->wait, EPOLLIN);
2367925ca369SKent Gibson 	else
2368925ca369SKent Gibson 		pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2369925ca369SKent Gibson 
2370925ca369SKent Gibson 	return NOTIFY_OK;
2371925ca369SKent Gibson }
2372925ca369SKent Gibson 
237349bc5279SKent Gibson static __poll_t lineinfo_watch_poll(struct file *file,
2374925ca369SKent Gibson 				    struct poll_table_struct *pollt)
2375925ca369SKent Gibson {
2376e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2377925ca369SKent Gibson 	__poll_t events = 0;
2378925ca369SKent Gibson 
2379e2b781c5SKent Gibson 	poll_wait(file, &cdev->wait, pollt);
2380925ca369SKent Gibson 
2381e2b781c5SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2382e2b781c5SKent Gibson 						 &cdev->wait.lock))
2383925ca369SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
2384925ca369SKent Gibson 
2385925ca369SKent Gibson 	return events;
2386925ca369SKent Gibson }
2387925ca369SKent Gibson 
238849bc5279SKent Gibson static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2389925ca369SKent Gibson 				   size_t count, loff_t *off)
2390925ca369SKent Gibson {
2391e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2392aad95584SKent Gibson 	struct gpio_v2_line_info_changed event;
2393925ca369SKent Gibson 	ssize_t bytes_read = 0;
2394925ca369SKent Gibson 	int ret;
2395aad95584SKent Gibson 	size_t event_size;
2396925ca369SKent Gibson 
2397aad95584SKent Gibson #ifndef CONFIG_GPIO_CDEV_V1
2398aad95584SKent Gibson 	event_size = sizeof(struct gpio_v2_line_info_changed);
2399aad95584SKent Gibson 	if (count < event_size)
2400925ca369SKent Gibson 		return -EINVAL;
2401aad95584SKent Gibson #endif
2402925ca369SKent Gibson 
2403925ca369SKent Gibson 	do {
2404e2b781c5SKent Gibson 		spin_lock(&cdev->wait.lock);
2405e2b781c5SKent Gibson 		if (kfifo_is_empty(&cdev->events)) {
2406925ca369SKent Gibson 			if (bytes_read) {
2407e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2408925ca369SKent Gibson 				return bytes_read;
2409925ca369SKent Gibson 			}
2410925ca369SKent Gibson 
241149bc5279SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
2412e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2413925ca369SKent Gibson 				return -EAGAIN;
2414925ca369SKent Gibson 			}
2415925ca369SKent Gibson 
2416e2b781c5SKent Gibson 			ret = wait_event_interruptible_locked(cdev->wait,
2417e2b781c5SKent Gibson 					!kfifo_is_empty(&cdev->events));
2418925ca369SKent Gibson 			if (ret) {
2419e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2420925ca369SKent Gibson 				return ret;
2421925ca369SKent Gibson 			}
2422925ca369SKent Gibson 		}
2423aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2424aad95584SKent Gibson 		/* must be after kfifo check so watch_abi_version is set */
2425aad95584SKent Gibson 		if (atomic_read(&cdev->watch_abi_version) == 2)
2426aad95584SKent Gibson 			event_size = sizeof(struct gpio_v2_line_info_changed);
2427aad95584SKent Gibson 		else
2428aad95584SKent Gibson 			event_size = sizeof(struct gpioline_info_changed);
2429aad95584SKent Gibson 		if (count < event_size) {
2430aad95584SKent Gibson 			spin_unlock(&cdev->wait.lock);
2431aad95584SKent Gibson 			return -EINVAL;
2432aad95584SKent Gibson 		}
2433aad95584SKent Gibson #endif
2434e2b781c5SKent Gibson 		ret = kfifo_out(&cdev->events, &event, 1);
2435e2b781c5SKent Gibson 		spin_unlock(&cdev->wait.lock);
2436925ca369SKent Gibson 		if (ret != 1) {
2437925ca369SKent Gibson 			ret = -EIO;
2438925ca369SKent Gibson 			break;
2439925ca369SKent Gibson 			/* We should never get here. See lineevent_read(). */
2440925ca369SKent Gibson 		}
2441925ca369SKent Gibson 
2442aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2443aad95584SKent Gibson 		if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2444aad95584SKent Gibson 			if (copy_to_user(buf + bytes_read, &event, event_size))
2445925ca369SKent Gibson 				return -EFAULT;
2446aad95584SKent Gibson 		} else {
2447aad95584SKent Gibson 			struct gpioline_info_changed event_v1;
2448aad95584SKent Gibson 
2449aad95584SKent Gibson 			gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2450aad95584SKent Gibson 			if (copy_to_user(buf + bytes_read, &event_v1,
2451aad95584SKent Gibson 					 event_size))
2452aad95584SKent Gibson 				return -EFAULT;
2453aad95584SKent Gibson 		}
2454aad95584SKent Gibson #else
2455aad95584SKent Gibson 		if (copy_to_user(buf + bytes_read, &event, event_size))
2456aad95584SKent Gibson 			return -EFAULT;
2457aad95584SKent Gibson #endif
2458aad95584SKent Gibson 		bytes_read += event_size;
2459925ca369SKent Gibson 	} while (count >= bytes_read + sizeof(event));
2460925ca369SKent Gibson 
2461925ca369SKent Gibson 	return bytes_read;
2462925ca369SKent Gibson }
2463925ca369SKent Gibson 
2464925ca369SKent Gibson /**
2465925ca369SKent Gibson  * gpio_chrdev_open() - open the chardev for ioctl operations
2466925ca369SKent Gibson  * @inode: inode for this chardev
246749bc5279SKent Gibson  * @file: file struct for storing private data
2468925ca369SKent Gibson  * Returns 0 on success
2469925ca369SKent Gibson  */
247049bc5279SKent Gibson static int gpio_chrdev_open(struct inode *inode, struct file *file)
2471925ca369SKent Gibson {
2472925ca369SKent Gibson 	struct gpio_device *gdev = container_of(inode->i_cdev,
2473925ca369SKent Gibson 						struct gpio_device, chrdev);
2474e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev;
2475925ca369SKent Gibson 	int ret = -ENOMEM;
2476925ca369SKent Gibson 
2477925ca369SKent Gibson 	/* Fail on open if the backing gpiochip is gone */
2478925ca369SKent Gibson 	if (!gdev->chip)
2479925ca369SKent Gibson 		return -ENODEV;
2480925ca369SKent Gibson 
2481e2b781c5SKent Gibson 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2482e2b781c5SKent Gibson 	if (!cdev)
2483925ca369SKent Gibson 		return -ENOMEM;
2484925ca369SKent Gibson 
2485e2b781c5SKent Gibson 	cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2486e2b781c5SKent Gibson 	if (!cdev->watched_lines)
2487e2b781c5SKent Gibson 		goto out_free_cdev;
2488925ca369SKent Gibson 
2489e2b781c5SKent Gibson 	init_waitqueue_head(&cdev->wait);
2490e2b781c5SKent Gibson 	INIT_KFIFO(cdev->events);
2491e2b781c5SKent Gibson 	cdev->gdev = gdev;
2492925ca369SKent Gibson 
2493e2b781c5SKent Gibson 	cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
24946accc376SKent Gibson 	ret = blocking_notifier_chain_register(&gdev->notifier,
2495e2b781c5SKent Gibson 					       &cdev->lineinfo_changed_nb);
2496925ca369SKent Gibson 	if (ret)
2497925ca369SKent Gibson 		goto out_free_bitmap;
2498925ca369SKent Gibson 
2499925ca369SKent Gibson 	get_device(&gdev->dev);
2500e2b781c5SKent Gibson 	file->private_data = cdev;
2501925ca369SKent Gibson 
250249bc5279SKent Gibson 	ret = nonseekable_open(inode, file);
2503925ca369SKent Gibson 	if (ret)
2504925ca369SKent Gibson 		goto out_unregister_notifier;
2505925ca369SKent Gibson 
2506925ca369SKent Gibson 	return ret;
2507925ca369SKent Gibson 
2508925ca369SKent Gibson out_unregister_notifier:
25096accc376SKent Gibson 	blocking_notifier_chain_unregister(&gdev->notifier,
2510e2b781c5SKent Gibson 					   &cdev->lineinfo_changed_nb);
2511925ca369SKent Gibson out_free_bitmap:
2512e2b781c5SKent Gibson 	bitmap_free(cdev->watched_lines);
2513e2b781c5SKent Gibson out_free_cdev:
2514e2b781c5SKent Gibson 	kfree(cdev);
2515925ca369SKent Gibson 	return ret;
2516925ca369SKent Gibson }
2517925ca369SKent Gibson 
2518925ca369SKent Gibson /**
2519925ca369SKent Gibson  * gpio_chrdev_release() - close chardev after ioctl operations
2520925ca369SKent Gibson  * @inode: inode for this chardev
252149bc5279SKent Gibson  * @file: file struct for storing private data
2522925ca369SKent Gibson  * Returns 0 on success
2523925ca369SKent Gibson  */
252449bc5279SKent Gibson static int gpio_chrdev_release(struct inode *inode, struct file *file)
2525925ca369SKent Gibson {
2526e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2527e2b781c5SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
2528925ca369SKent Gibson 
2529e2b781c5SKent Gibson 	bitmap_free(cdev->watched_lines);
25306accc376SKent Gibson 	blocking_notifier_chain_unregister(&gdev->notifier,
2531e2b781c5SKent Gibson 					   &cdev->lineinfo_changed_nb);
2532925ca369SKent Gibson 	put_device(&gdev->dev);
2533e2b781c5SKent Gibson 	kfree(cdev);
2534925ca369SKent Gibson 
2535925ca369SKent Gibson 	return 0;
2536925ca369SKent Gibson }
2537925ca369SKent Gibson 
2538925ca369SKent Gibson static const struct file_operations gpio_fileops = {
2539925ca369SKent Gibson 	.release = gpio_chrdev_release,
2540925ca369SKent Gibson 	.open = gpio_chrdev_open,
2541925ca369SKent Gibson 	.poll = lineinfo_watch_poll,
2542925ca369SKent Gibson 	.read = lineinfo_watch_read,
2543925ca369SKent Gibson 	.owner = THIS_MODULE,
2544925ca369SKent Gibson 	.llseek = no_llseek,
2545925ca369SKent Gibson 	.unlocked_ioctl = gpio_ioctl,
2546925ca369SKent Gibson #ifdef CONFIG_COMPAT
2547925ca369SKent Gibson 	.compat_ioctl = gpio_ioctl_compat,
2548925ca369SKent Gibson #endif
2549925ca369SKent Gibson };
2550925ca369SKent Gibson 
2551925ca369SKent Gibson int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2552925ca369SKent Gibson {
2553925ca369SKent Gibson 	int ret;
2554925ca369SKent Gibson 
2555925ca369SKent Gibson 	cdev_init(&gdev->chrdev, &gpio_fileops);
2556925ca369SKent Gibson 	gdev->chrdev.owner = THIS_MODULE;
2557925ca369SKent Gibson 	gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2558925ca369SKent Gibson 
2559925ca369SKent Gibson 	ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2560925ca369SKent Gibson 	if (ret)
2561925ca369SKent Gibson 		return ret;
2562925ca369SKent Gibson 
2563925ca369SKent Gibson 	chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2564925ca369SKent Gibson 		 MAJOR(devt), gdev->id);
2565925ca369SKent Gibson 
2566925ca369SKent Gibson 	return 0;
2567925ca369SKent Gibson }
2568925ca369SKent Gibson 
2569925ca369SKent Gibson void gpiolib_cdev_unregister(struct gpio_device *gdev)
2570925ca369SKent Gibson {
2571925ca369SKent Gibson 	cdev_device_del(&gdev->chrdev, &gdev->dev);
2572925ca369SKent Gibson }
2573