xref: /openbmc/linux/drivers/gpio/gpiolib-cdev.c (revision 2068339a6c35147847ba433fd0da67b313779059)
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>
27*2068339aSDipen 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);
201925ca369SKent Gibson 	int i;
202925ca369SKent Gibson 
203925ca369SKent Gibson 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
204925ca369SKent Gibson 		/* NOTE: It's ok to read values of output lines. */
205925ca369SKent Gibson 		int ret = gpiod_get_array_value_complex(false,
206925ca369SKent Gibson 							true,
20752b7b596SKent Gibson 							lh->num_descs,
208925ca369SKent Gibson 							lh->descs,
209925ca369SKent Gibson 							NULL,
210925ca369SKent Gibson 							vals);
211925ca369SKent Gibson 		if (ret)
212925ca369SKent Gibson 			return ret;
213925ca369SKent Gibson 
214925ca369SKent Gibson 		memset(&ghd, 0, sizeof(ghd));
21552b7b596SKent Gibson 		for (i = 0; i < lh->num_descs; i++)
216925ca369SKent Gibson 			ghd.values[i] = test_bit(i, vals);
217925ca369SKent Gibson 
218925ca369SKent Gibson 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
219925ca369SKent Gibson 			return -EFAULT;
220925ca369SKent Gibson 
221925ca369SKent Gibson 		return 0;
222925ca369SKent Gibson 	} else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
223925ca369SKent Gibson 		/*
224925ca369SKent Gibson 		 * All line descriptors were created at once with the same
225925ca369SKent Gibson 		 * flags so just check if the first one is really output.
226925ca369SKent Gibson 		 */
227925ca369SKent Gibson 		if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
228925ca369SKent Gibson 			return -EPERM;
229925ca369SKent Gibson 
230925ca369SKent Gibson 		if (copy_from_user(&ghd, ip, sizeof(ghd)))
231925ca369SKent Gibson 			return -EFAULT;
232925ca369SKent Gibson 
233925ca369SKent Gibson 		/* Clamp all values to [0,1] */
23452b7b596SKent Gibson 		for (i = 0; i < lh->num_descs; i++)
235925ca369SKent Gibson 			__assign_bit(i, vals, ghd.values[i]);
236925ca369SKent Gibson 
237925ca369SKent Gibson 		/* Reuse the array setting function */
238925ca369SKent Gibson 		return gpiod_set_array_value_complex(false,
239925ca369SKent Gibson 						     true,
24052b7b596SKent Gibson 						     lh->num_descs,
241925ca369SKent Gibson 						     lh->descs,
242925ca369SKent Gibson 						     NULL,
243925ca369SKent Gibson 						     vals);
244925ca369SKent Gibson 	} else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) {
245925ca369SKent Gibson 		return linehandle_set_config(lh, ip);
246925ca369SKent Gibson 	}
247925ca369SKent Gibson 	return -EINVAL;
248925ca369SKent Gibson }
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;
468*2068339aSDipen Patel 	/*
469*2068339aSDipen Patel 	 * -- hte specific fields --
470*2068339aSDipen Patel 	 */
471*2068339aSDipen Patel 	struct hte_ts_desc hdesc;
472*2068339aSDipen Patel 	/*
473*2068339aSDipen Patel 	 * HTE provider sets line level at the time of event. The valid
474*2068339aSDipen Patel 	 * value is 0 or 1 and negative value for an error.
475*2068339aSDipen Patel 	 */
476*2068339aSDipen Patel 	int raw_level;
477*2068339aSDipen Patel 	/*
478*2068339aSDipen Patel 	 * when sw_debounce is set on HTE enabled line, this is running
479*2068339aSDipen Patel 	 * counter of the discarded events.
480*2068339aSDipen Patel 	 */
481*2068339aSDipen Patel 	u32 total_discard_seq;
482*2068339aSDipen Patel 	/*
483*2068339aSDipen Patel 	 * when sw_debounce is set on HTE enabled line, this variable records
484*2068339aSDipen Patel 	 * last sequence number before debounce period expires.
485*2068339aSDipen Patel 	 */
486*2068339aSDipen 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 | \
541*2068339aSDipen 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();
566*2068339aSDipen Patel 	else if (test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))
567*2068339aSDipen Patel 		return line->timestamp_ns;
56826d060e4SKent Gibson 
56926d060e4SKent Gibson 	return ktime_get_ns();
57026d060e4SKent Gibson }
57126d060e4SKent Gibson 
572*2068339aSDipen Patel static enum hte_return process_hw_ts_thread(void *p)
573*2068339aSDipen Patel {
574*2068339aSDipen Patel 	struct line *line;
575*2068339aSDipen Patel 	struct linereq *lr;
576*2068339aSDipen Patel 	struct gpio_v2_line_event le;
577*2068339aSDipen Patel 	int level;
578*2068339aSDipen Patel 	u64 eflags;
579*2068339aSDipen Patel 
580*2068339aSDipen Patel 	if (!p)
581*2068339aSDipen Patel 		return HTE_CB_HANDLED;
582*2068339aSDipen Patel 
583*2068339aSDipen Patel 	line = p;
584*2068339aSDipen Patel 	lr = line->req;
585*2068339aSDipen Patel 
586*2068339aSDipen Patel 	memset(&le, 0, sizeof(le));
587*2068339aSDipen Patel 
588*2068339aSDipen Patel 	le.timestamp_ns = line->timestamp_ns;
589*2068339aSDipen Patel 	eflags = READ_ONCE(line->eflags);
590*2068339aSDipen Patel 
591*2068339aSDipen Patel 	if (eflags == GPIO_V2_LINE_FLAG_EDGE_BOTH) {
592*2068339aSDipen Patel 		if (line->raw_level >= 0) {
593*2068339aSDipen Patel 			if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
594*2068339aSDipen Patel 				level = !line->raw_level;
595*2068339aSDipen Patel 			else
596*2068339aSDipen Patel 				level = line->raw_level;
597*2068339aSDipen Patel 		} else {
598*2068339aSDipen Patel 			level = gpiod_get_value_cansleep(line->desc);
599*2068339aSDipen Patel 		}
600*2068339aSDipen Patel 
601*2068339aSDipen Patel 		if (level)
602*2068339aSDipen Patel 			le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
603*2068339aSDipen Patel 		else
604*2068339aSDipen Patel 			le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
605*2068339aSDipen Patel 	} else if (eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) {
606*2068339aSDipen Patel 		/* Emit low-to-high event */
607*2068339aSDipen Patel 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
608*2068339aSDipen Patel 	} else if (eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) {
609*2068339aSDipen Patel 		/* Emit high-to-low event */
610*2068339aSDipen Patel 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
611*2068339aSDipen Patel 	} else {
612*2068339aSDipen Patel 		return HTE_CB_HANDLED;
613*2068339aSDipen Patel 	}
614*2068339aSDipen Patel 	le.line_seqno = line->line_seqno;
615*2068339aSDipen Patel 	le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
616*2068339aSDipen Patel 	le.offset = gpio_chip_hwgpio(line->desc);
617*2068339aSDipen Patel 
618*2068339aSDipen Patel 	linereq_put_event(lr, &le);
619*2068339aSDipen Patel 
620*2068339aSDipen Patel 	return HTE_CB_HANDLED;
621*2068339aSDipen Patel }
622*2068339aSDipen Patel 
623*2068339aSDipen Patel static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p)
624*2068339aSDipen Patel {
625*2068339aSDipen Patel 	struct line *line;
626*2068339aSDipen Patel 	struct linereq *lr;
627*2068339aSDipen Patel 	int diff_seqno = 0;
628*2068339aSDipen Patel 
629*2068339aSDipen Patel 	if (!ts || !p)
630*2068339aSDipen Patel 		return HTE_CB_HANDLED;
631*2068339aSDipen Patel 
632*2068339aSDipen Patel 	line = p;
633*2068339aSDipen Patel 	line->timestamp_ns = ts->tsc;
634*2068339aSDipen Patel 	line->raw_level = ts->raw_level;
635*2068339aSDipen Patel 	lr = line->req;
636*2068339aSDipen Patel 
637*2068339aSDipen Patel 	if (READ_ONCE(line->sw_debounced)) {
638*2068339aSDipen Patel 		line->total_discard_seq++;
639*2068339aSDipen Patel 		line->last_seqno = ts->seq;
640*2068339aSDipen Patel 		mod_delayed_work(system_wq, &line->work,
641*2068339aSDipen Patel 		  usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
642*2068339aSDipen Patel 	} else {
643*2068339aSDipen Patel 		if (unlikely(ts->seq < line->line_seqno))
644*2068339aSDipen Patel 			return HTE_CB_HANDLED;
645*2068339aSDipen Patel 
646*2068339aSDipen Patel 		diff_seqno = ts->seq - line->line_seqno;
647*2068339aSDipen Patel 		line->line_seqno = ts->seq;
648*2068339aSDipen Patel 		if (lr->num_lines != 1)
649*2068339aSDipen Patel 			line->req_seqno = atomic_add_return(diff_seqno,
650*2068339aSDipen Patel 							    &lr->seqno);
651*2068339aSDipen Patel 
652*2068339aSDipen Patel 		return HTE_RUN_SECOND_CB;
653*2068339aSDipen Patel 	}
654*2068339aSDipen Patel 
655*2068339aSDipen Patel 	return HTE_CB_HANDLED;
656*2068339aSDipen Patel }
657*2068339aSDipen Patel 
65873e03419SKent Gibson static irqreturn_t edge_irq_thread(int irq, void *p)
65973e03419SKent Gibson {
66073e03419SKent Gibson 	struct line *line = p;
66173e03419SKent Gibson 	struct linereq *lr = line->req;
66273e03419SKent Gibson 	struct gpio_v2_line_event le;
6633ffb7c45SKent Gibson 	u64 eflags;
66473e03419SKent Gibson 
66573e03419SKent Gibson 	/* Do not leak kernel stack to userspace */
66673e03419SKent Gibson 	memset(&le, 0, sizeof(le));
66773e03419SKent Gibson 
66873e03419SKent Gibson 	if (line->timestamp_ns) {
66973e03419SKent Gibson 		le.timestamp_ns = line->timestamp_ns;
67073e03419SKent Gibson 	} else {
67173e03419SKent Gibson 		/*
67273e03419SKent Gibson 		 * We may be running from a nested threaded interrupt in
67373e03419SKent Gibson 		 * which case we didn't get the timestamp from
67473e03419SKent Gibson 		 * edge_irq_handler().
67573e03419SKent Gibson 		 */
67626d060e4SKent Gibson 		le.timestamp_ns = line_event_timestamp(line);
67773e03419SKent Gibson 		if (lr->num_lines != 1)
67873e03419SKent Gibson 			line->req_seqno = atomic_inc_return(&lr->seqno);
67973e03419SKent Gibson 	}
68073e03419SKent Gibson 	line->timestamp_ns = 0;
68173e03419SKent Gibson 
6823ffb7c45SKent Gibson 	eflags = READ_ONCE(line->eflags);
6835e2ca893SKent Gibson 	if (eflags == GPIO_V2_LINE_FLAG_EDGE_BOTH) {
68473e03419SKent Gibson 		int level = gpiod_get_value_cansleep(line->desc);
68573e03419SKent Gibson 
68673e03419SKent Gibson 		if (level)
68773e03419SKent Gibson 			/* Emit low-to-high event */
68873e03419SKent Gibson 			le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
68973e03419SKent Gibson 		else
69073e03419SKent Gibson 			/* Emit high-to-low event */
69173e03419SKent Gibson 			le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
6923ffb7c45SKent Gibson 	} else if (eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) {
69373e03419SKent Gibson 		/* Emit low-to-high event */
69473e03419SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
6953ffb7c45SKent Gibson 	} else if (eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) {
69673e03419SKent Gibson 		/* Emit high-to-low event */
69773e03419SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
69873e03419SKent Gibson 	} else {
69973e03419SKent Gibson 		return IRQ_NONE;
70073e03419SKent Gibson 	}
70173e03419SKent Gibson 	line->line_seqno++;
70273e03419SKent Gibson 	le.line_seqno = line->line_seqno;
70373e03419SKent Gibson 	le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
70473e03419SKent Gibson 	le.offset = gpio_chip_hwgpio(line->desc);
70573e03419SKent Gibson 
70673e03419SKent Gibson 	linereq_put_event(lr, &le);
70773e03419SKent Gibson 
70873e03419SKent Gibson 	return IRQ_HANDLED;
70973e03419SKent Gibson }
71073e03419SKent Gibson 
71173e03419SKent Gibson static irqreturn_t edge_irq_handler(int irq, void *p)
71273e03419SKent Gibson {
71373e03419SKent Gibson 	struct line *line = p;
71473e03419SKent Gibson 	struct linereq *lr = line->req;
71573e03419SKent Gibson 
71673e03419SKent Gibson 	/*
71773e03419SKent Gibson 	 * Just store the timestamp in hardirq context so we get it as
71873e03419SKent Gibson 	 * close in time as possible to the actual event.
71973e03419SKent Gibson 	 */
72026d060e4SKent Gibson 	line->timestamp_ns = line_event_timestamp(line);
72173e03419SKent Gibson 
72273e03419SKent Gibson 	if (lr->num_lines != 1)
72373e03419SKent Gibson 		line->req_seqno = atomic_inc_return(&lr->seqno);
72473e03419SKent Gibson 
72573e03419SKent Gibson 	return IRQ_WAKE_THREAD;
72673e03419SKent Gibson }
72773e03419SKent Gibson 
72865cff704SKent Gibson /*
72965cff704SKent Gibson  * returns the current debounced logical value.
73065cff704SKent Gibson  */
73165cff704SKent Gibson static bool debounced_value(struct line *line)
73265cff704SKent Gibson {
73365cff704SKent Gibson 	bool value;
73465cff704SKent Gibson 
73565cff704SKent Gibson 	/*
73665cff704SKent Gibson 	 * minor race - debouncer may be stopped here, so edge_detector_stop()
73765cff704SKent Gibson 	 * must leave the value unchanged so the following will read the level
73865cff704SKent Gibson 	 * from when the debouncer was last running.
73965cff704SKent Gibson 	 */
74065cff704SKent Gibson 	value = READ_ONCE(line->level);
74165cff704SKent Gibson 
74265cff704SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
74365cff704SKent Gibson 		value = !value;
74465cff704SKent Gibson 
74565cff704SKent Gibson 	return value;
74665cff704SKent Gibson }
74765cff704SKent Gibson 
74865cff704SKent Gibson static irqreturn_t debounce_irq_handler(int irq, void *p)
74965cff704SKent Gibson {
75065cff704SKent Gibson 	struct line *line = p;
75165cff704SKent Gibson 
75265cff704SKent Gibson 	mod_delayed_work(system_wq, &line->work,
75365cff704SKent Gibson 		usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
75465cff704SKent Gibson 
75565cff704SKent Gibson 	return IRQ_HANDLED;
75665cff704SKent Gibson }
75765cff704SKent Gibson 
75865cff704SKent Gibson static void debounce_work_func(struct work_struct *work)
75965cff704SKent Gibson {
76065cff704SKent Gibson 	struct gpio_v2_line_event le;
76165cff704SKent Gibson 	struct line *line = container_of(work, struct line, work.work);
76265cff704SKent Gibson 	struct linereq *lr;
763*2068339aSDipen Patel 	int level, diff_seqno;
7643ffb7c45SKent Gibson 	u64 eflags;
76565cff704SKent Gibson 
766*2068339aSDipen Patel 	if (test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags)) {
767*2068339aSDipen Patel 		level = line->raw_level;
768*2068339aSDipen Patel 		if (level < 0)
76965cff704SKent Gibson 			level = gpiod_get_raw_value_cansleep(line->desc);
770*2068339aSDipen Patel 	} else {
771*2068339aSDipen Patel 		level = gpiod_get_raw_value_cansleep(line->desc);
772*2068339aSDipen Patel 	}
77365cff704SKent Gibson 	if (level < 0) {
77465cff704SKent Gibson 		pr_debug_ratelimited("debouncer failed to read line value\n");
77565cff704SKent Gibson 		return;
77665cff704SKent Gibson 	}
77765cff704SKent Gibson 
77865cff704SKent Gibson 	if (READ_ONCE(line->level) == level)
77965cff704SKent Gibson 		return;
78065cff704SKent Gibson 
78165cff704SKent Gibson 	WRITE_ONCE(line->level, level);
78265cff704SKent Gibson 
78365cff704SKent Gibson 	/* -- edge detection -- */
7843ffb7c45SKent Gibson 	eflags = READ_ONCE(line->eflags);
7853ffb7c45SKent Gibson 	if (!eflags)
78665cff704SKent Gibson 		return;
78765cff704SKent Gibson 
78865cff704SKent Gibson 	/* switch from physical level to logical - if they differ */
78965cff704SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
79065cff704SKent Gibson 		level = !level;
79165cff704SKent Gibson 
79265cff704SKent Gibson 	/* ignore edges that are not being monitored */
7933ffb7c45SKent Gibson 	if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
7943ffb7c45SKent Gibson 	    ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
79565cff704SKent Gibson 		return;
79665cff704SKent Gibson 
79765cff704SKent Gibson 	/* Do not leak kernel stack to userspace */
79865cff704SKent Gibson 	memset(&le, 0, sizeof(le));
79965cff704SKent Gibson 
80065cff704SKent Gibson 	lr = line->req;
80126d060e4SKent Gibson 	le.timestamp_ns = line_event_timestamp(line);
80265cff704SKent Gibson 	le.offset = gpio_chip_hwgpio(line->desc);
803*2068339aSDipen Patel 	if (test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags)) {
804*2068339aSDipen Patel 		/* discard events except the last one */
805*2068339aSDipen Patel 		line->total_discard_seq -= 1;
806*2068339aSDipen Patel 		diff_seqno = line->last_seqno - line->total_discard_seq -
807*2068339aSDipen Patel 				line->line_seqno;
808*2068339aSDipen Patel 		line->line_seqno = line->last_seqno - line->total_discard_seq;
809*2068339aSDipen Patel 		le.line_seqno = line->line_seqno;
810*2068339aSDipen Patel 		le.seqno = (lr->num_lines == 1) ?
811*2068339aSDipen Patel 			le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno);
812*2068339aSDipen Patel 	} else {
81365cff704SKent Gibson 		line->line_seqno++;
81465cff704SKent Gibson 		le.line_seqno = line->line_seqno;
81565cff704SKent Gibson 		le.seqno = (lr->num_lines == 1) ?
81665cff704SKent Gibson 			le.line_seqno : atomic_inc_return(&lr->seqno);
817*2068339aSDipen Patel 	}
81865cff704SKent Gibson 
81965cff704SKent Gibson 	if (level)
82065cff704SKent Gibson 		/* Emit low-to-high event */
82165cff704SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
82265cff704SKent Gibson 	else
82365cff704SKent Gibson 		/* Emit high-to-low event */
82465cff704SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
82565cff704SKent Gibson 
82665cff704SKent Gibson 	linereq_put_event(lr, &le);
82765cff704SKent Gibson }
82865cff704SKent Gibson 
829*2068339aSDipen Patel static int hte_edge_setup(struct line *line, u64 eflags)
830*2068339aSDipen Patel {
831*2068339aSDipen Patel 	int ret;
832*2068339aSDipen Patel 	unsigned long flags = 0;
833*2068339aSDipen Patel 	struct hte_ts_desc *hdesc = &line->hdesc;
834*2068339aSDipen Patel 
835*2068339aSDipen Patel 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
836*2068339aSDipen Patel 		flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
837*2068339aSDipen Patel 				  HTE_FALLING_EDGE_TS : HTE_RISING_EDGE_TS;
838*2068339aSDipen Patel 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
839*2068339aSDipen Patel 		flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
840*2068339aSDipen Patel 				  HTE_RISING_EDGE_TS : HTE_FALLING_EDGE_TS;
841*2068339aSDipen Patel 
842*2068339aSDipen Patel 	line->total_discard_seq = 0;
843*2068339aSDipen Patel 
844*2068339aSDipen Patel 	hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags,
845*2068339aSDipen Patel 			   NULL, line->desc);
846*2068339aSDipen Patel 
847*2068339aSDipen Patel 	ret = hte_ts_get(NULL, hdesc, 0);
848*2068339aSDipen Patel 	if (ret)
849*2068339aSDipen Patel 		return ret;
850*2068339aSDipen Patel 
851*2068339aSDipen Patel 	return hte_request_ts_ns(hdesc, process_hw_ts,
852*2068339aSDipen Patel 				 process_hw_ts_thread, line);
853*2068339aSDipen Patel }
854*2068339aSDipen Patel 
85565cff704SKent Gibson static int debounce_setup(struct line *line,
856*2068339aSDipen Patel 			  unsigned int debounce_period_us, bool hte_req)
85765cff704SKent Gibson {
85865cff704SKent Gibson 	unsigned long irqflags;
85965cff704SKent Gibson 	int ret, level, irq;
86065cff704SKent Gibson 
86165cff704SKent Gibson 	/* try hardware */
86265cff704SKent Gibson 	ret = gpiod_set_debounce(line->desc, debounce_period_us);
86365cff704SKent Gibson 	if (!ret) {
86465cff704SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
86565cff704SKent Gibson 		return ret;
86665cff704SKent Gibson 	}
86765cff704SKent Gibson 	if (ret != -ENOTSUPP)
86865cff704SKent Gibson 		return ret;
86965cff704SKent Gibson 
87065cff704SKent Gibson 	if (debounce_period_us) {
87165cff704SKent Gibson 		/* setup software debounce */
87265cff704SKent Gibson 		level = gpiod_get_raw_value_cansleep(line->desc);
87365cff704SKent Gibson 		if (level < 0)
87465cff704SKent Gibson 			return level;
87565cff704SKent Gibson 
876*2068339aSDipen Patel 		if (!hte_req) {
87765cff704SKent Gibson 			irq = gpiod_to_irq(line->desc);
87865cff704SKent Gibson 			if (irq < 0)
87965cff704SKent Gibson 				return -ENXIO;
88065cff704SKent Gibson 
88165cff704SKent Gibson 			irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
88265cff704SKent Gibson 			ret = request_irq(irq, debounce_irq_handler, irqflags,
88365cff704SKent Gibson 					  line->req->label, line);
88465cff704SKent Gibson 			if (ret)
88565cff704SKent Gibson 				return ret;
88665cff704SKent Gibson 			line->irq = irq;
887*2068339aSDipen Patel 		} else {
888*2068339aSDipen Patel 			ret = hte_edge_setup(line,
889*2068339aSDipen Patel 					     GPIO_V2_LINE_FLAG_EDGE_RISING |
890*2068339aSDipen Patel 					     GPIO_V2_LINE_FLAG_EDGE_FALLING);
891*2068339aSDipen Patel 			if (ret)
892*2068339aSDipen Patel 				return ret;
893*2068339aSDipen Patel 		}
894*2068339aSDipen Patel 
895*2068339aSDipen Patel 		WRITE_ONCE(line->level, level);
896*2068339aSDipen Patel 		WRITE_ONCE(line->sw_debounced, 1);
89765cff704SKent Gibson 	}
89865cff704SKent Gibson 	return 0;
89965cff704SKent Gibson }
90065cff704SKent Gibson 
90165cff704SKent Gibson static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
90265cff704SKent Gibson 					  unsigned int line_idx)
90365cff704SKent Gibson {
90465cff704SKent Gibson 	unsigned int i;
90565cff704SKent Gibson 	u64 mask = BIT_ULL(line_idx);
90665cff704SKent Gibson 
90765cff704SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
90865cff704SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
90965cff704SKent Gibson 		    (lc->attrs[i].mask & mask))
91065cff704SKent Gibson 			return true;
91165cff704SKent Gibson 	}
91265cff704SKent Gibson 	return false;
91365cff704SKent Gibson }
91465cff704SKent Gibson 
91565cff704SKent Gibson static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
91665cff704SKent Gibson 					       unsigned int line_idx)
91765cff704SKent Gibson {
91865cff704SKent Gibson 	unsigned int i;
91965cff704SKent Gibson 	u64 mask = BIT_ULL(line_idx);
92065cff704SKent Gibson 
92165cff704SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
92265cff704SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
92365cff704SKent Gibson 		    (lc->attrs[i].mask & mask))
92465cff704SKent Gibson 			return lc->attrs[i].attr.debounce_period_us;
92565cff704SKent Gibson 	}
92665cff704SKent Gibson 	return 0;
92765cff704SKent Gibson }
92865cff704SKent Gibson 
929*2068339aSDipen Patel static void edge_detector_stop(struct line *line, bool hte_en)
93073e03419SKent Gibson {
931*2068339aSDipen Patel 	if (line->irq && !hte_en) {
93273e03419SKent Gibson 		free_irq(line->irq, line);
93373e03419SKent Gibson 		line->irq = 0;
93473e03419SKent Gibson 	}
935a54756cbSKent Gibson 
936*2068339aSDipen Patel 	if (hte_en)
937*2068339aSDipen Patel 		hte_ts_put(&line->hdesc);
938*2068339aSDipen Patel 
93965cff704SKent Gibson 	cancel_delayed_work_sync(&line->work);
94065cff704SKent Gibson 	WRITE_ONCE(line->sw_debounced, 0);
9413ffb7c45SKent Gibson 	WRITE_ONCE(line->eflags, 0);
94203a58ea5SKent Gibson 	if (line->desc)
94303a58ea5SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, 0);
94465cff704SKent Gibson 	/* do not change line->level - see comment in debounced_value() */
94573e03419SKent Gibson }
94673e03419SKent Gibson 
94773e03419SKent Gibson static int edge_detector_setup(struct line *line,
94865cff704SKent Gibson 			       struct gpio_v2_line_config *lc,
94965cff704SKent Gibson 			       unsigned int line_idx,
950*2068339aSDipen Patel 			       u64 eflags, bool hte_req)
95173e03419SKent Gibson {
95265cff704SKent Gibson 	u32 debounce_period_us;
95373e03419SKent Gibson 	unsigned long irqflags = 0;
95473e03419SKent Gibson 	int irq, ret;
95573e03419SKent Gibson 
95673e03419SKent Gibson 	if (eflags && !kfifo_initialized(&line->req->events)) {
95773e03419SKent Gibson 		ret = kfifo_alloc(&line->req->events,
95873e03419SKent Gibson 				  line->req->event_buffer_size, GFP_KERNEL);
95973e03419SKent Gibson 		if (ret)
96073e03419SKent Gibson 			return ret;
96173e03419SKent Gibson 	}
9623ffb7c45SKent Gibson 	WRITE_ONCE(line->eflags, eflags);
96365cff704SKent Gibson 	if (gpio_v2_line_config_debounced(lc, line_idx)) {
96465cff704SKent Gibson 		debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
965*2068339aSDipen Patel 		ret = debounce_setup(line, debounce_period_us, hte_req);
96665cff704SKent Gibson 		if (ret)
96765cff704SKent Gibson 			return ret;
96865cff704SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
96965cff704SKent Gibson 	}
97073e03419SKent Gibson 
97165cff704SKent Gibson 	/* detection disabled or sw debouncer will provide edge detection */
97265cff704SKent Gibson 	if (!eflags || READ_ONCE(line->sw_debounced))
97373e03419SKent Gibson 		return 0;
97473e03419SKent Gibson 
975*2068339aSDipen Patel 	if (hte_req)
976*2068339aSDipen Patel 		return hte_edge_setup(line, eflags);
977*2068339aSDipen Patel 
97873e03419SKent Gibson 	irq = gpiod_to_irq(line->desc);
97973e03419SKent Gibson 	if (irq < 0)
98073e03419SKent Gibson 		return -ENXIO;
98173e03419SKent Gibson 
98273e03419SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
98373e03419SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
98473e03419SKent Gibson 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
98573e03419SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
98673e03419SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
98773e03419SKent Gibson 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
98873e03419SKent Gibson 	irqflags |= IRQF_ONESHOT;
98973e03419SKent Gibson 
99073e03419SKent Gibson 	/* Request a thread to read the events */
99173e03419SKent Gibson 	ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
99273e03419SKent Gibson 				   irqflags, line->req->label, line);
99373e03419SKent Gibson 	if (ret)
99473e03419SKent Gibson 		return ret;
99573e03419SKent Gibson 
99673e03419SKent Gibson 	line->irq = irq;
99773e03419SKent Gibson 	return 0;
99873e03419SKent Gibson }
99973e03419SKent Gibson 
100065cff704SKent Gibson static int edge_detector_update(struct line *line,
100165cff704SKent Gibson 				struct gpio_v2_line_config *lc,
100265cff704SKent Gibson 				unsigned int line_idx,
1003*2068339aSDipen Patel 				u64 flags, bool polarity_change,
1004*2068339aSDipen Patel 				bool prev_hte_flag)
1005a54756cbSKent Gibson {
1006*2068339aSDipen Patel 	u64 eflags = flags & GPIO_V2_LINE_EDGE_FLAGS;
100765cff704SKent Gibson 	unsigned int debounce_period_us =
100865cff704SKent Gibson 			gpio_v2_line_config_debounce_period(lc, line_idx);
1009*2068339aSDipen Patel 	bool hte_change = (prev_hte_flag !=
1010*2068339aSDipen Patel 		      ((flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) != 0));
101165cff704SKent Gibson 
10123ffb7c45SKent Gibson 	if ((READ_ONCE(line->eflags) == eflags) && !polarity_change &&
1013*2068339aSDipen Patel 	    (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us)
1014*2068339aSDipen Patel 	    && !hte_change)
1015a54756cbSKent Gibson 		return 0;
1016a54756cbSKent Gibson 
101765cff704SKent Gibson 	/* sw debounced and still will be...*/
101865cff704SKent Gibson 	if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
10193ffb7c45SKent Gibson 		WRITE_ONCE(line->eflags, eflags);
102065cff704SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
102165cff704SKent Gibson 		return 0;
102265cff704SKent Gibson 	}
102365cff704SKent Gibson 
102465cff704SKent Gibson 	/* reconfiguring edge detection or sw debounce being disabled */
1025*2068339aSDipen Patel 	if ((line->irq && !READ_ONCE(line->sw_debounced)) || prev_hte_flag ||
102665cff704SKent Gibson 	    (!debounce_period_us && READ_ONCE(line->sw_debounced)))
1027*2068339aSDipen Patel 		edge_detector_stop(line, prev_hte_flag);
1028a54756cbSKent Gibson 
1029*2068339aSDipen Patel 	return edge_detector_setup(line, lc, line_idx, eflags,
1030*2068339aSDipen Patel 				   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
1031a54756cbSKent Gibson }
1032a54756cbSKent Gibson 
10333c0d9c63SKent Gibson static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
10343c0d9c63SKent Gibson 				     unsigned int line_idx)
10353c0d9c63SKent Gibson {
10363c0d9c63SKent Gibson 	unsigned int i;
10373c0d9c63SKent Gibson 	u64 mask = BIT_ULL(line_idx);
10383c0d9c63SKent Gibson 
10393c0d9c63SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
10403c0d9c63SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
10413c0d9c63SKent Gibson 		    (lc->attrs[i].mask & mask))
10423c0d9c63SKent Gibson 			return lc->attrs[i].attr.flags;
10433c0d9c63SKent Gibson 	}
10443c0d9c63SKent Gibson 	return lc->flags;
10453c0d9c63SKent Gibson }
10463c0d9c63SKent Gibson 
10473c0d9c63SKent Gibson static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
10483c0d9c63SKent Gibson 					    unsigned int line_idx)
10493c0d9c63SKent Gibson {
10503c0d9c63SKent Gibson 	unsigned int i;
10513c0d9c63SKent Gibson 	u64 mask = BIT_ULL(line_idx);
10523c0d9c63SKent Gibson 
10533c0d9c63SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
10543c0d9c63SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
10553c0d9c63SKent Gibson 		    (lc->attrs[i].mask & mask))
10563c0d9c63SKent Gibson 			return !!(lc->attrs[i].attr.values & mask);
10573c0d9c63SKent Gibson 	}
10583c0d9c63SKent Gibson 	return 0;
10593c0d9c63SKent Gibson }
10603c0d9c63SKent Gibson 
10613c0d9c63SKent Gibson static int gpio_v2_line_flags_validate(u64 flags)
10623c0d9c63SKent Gibson {
10633c0d9c63SKent Gibson 	/* Return an error if an unknown flag is set */
10643c0d9c63SKent Gibson 	if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
10653c0d9c63SKent Gibson 		return -EINVAL;
10663c0d9c63SKent Gibson 	/*
10673c0d9c63SKent Gibson 	 * Do not allow both INPUT and OUTPUT flags to be set as they are
10683c0d9c63SKent Gibson 	 * contradictory.
10693c0d9c63SKent Gibson 	 */
10703c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
10713c0d9c63SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_OUTPUT))
10723c0d9c63SKent Gibson 		return -EINVAL;
10733c0d9c63SKent Gibson 
1074*2068339aSDipen Patel 	/* Only allow one event clock source */
1075*2068339aSDipen Patel 	if ((flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) &&
1076*2068339aSDipen Patel 	    (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1077*2068339aSDipen Patel 		return -EINVAL;
1078*2068339aSDipen Patel 
107973e03419SKent Gibson 	/* Edge detection requires explicit input. */
108073e03419SKent Gibson 	if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
108173e03419SKent Gibson 	    !(flags & GPIO_V2_LINE_FLAG_INPUT))
108273e03419SKent Gibson 		return -EINVAL;
108373e03419SKent Gibson 
10843c0d9c63SKent Gibson 	/*
10853c0d9c63SKent Gibson 	 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
10863c0d9c63SKent Gibson 	 * request. If the hardware actually supports enabling both at the
10873c0d9c63SKent Gibson 	 * same time the electrical result would be disastrous.
10883c0d9c63SKent Gibson 	 */
10893c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
10903c0d9c63SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
10913c0d9c63SKent Gibson 		return -EINVAL;
10923c0d9c63SKent Gibson 
10933c0d9c63SKent Gibson 	/* Drive requires explicit output direction. */
10943c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
10953c0d9c63SKent Gibson 	    !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
10963c0d9c63SKent Gibson 		return -EINVAL;
10973c0d9c63SKent Gibson 
10983c0d9c63SKent Gibson 	/* Bias requires explicit direction. */
10993c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
11003c0d9c63SKent Gibson 	    !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
11013c0d9c63SKent Gibson 		return -EINVAL;
11023c0d9c63SKent Gibson 
11033c0d9c63SKent Gibson 	/* Only one bias flag can be set. */
11043c0d9c63SKent Gibson 	if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
11053c0d9c63SKent Gibson 	     (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
11063c0d9c63SKent Gibson 		       GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
11073c0d9c63SKent Gibson 	    ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
11083c0d9c63SKent Gibson 	     (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
11093c0d9c63SKent Gibson 		return -EINVAL;
11103c0d9c63SKent Gibson 
11113c0d9c63SKent Gibson 	return 0;
11123c0d9c63SKent Gibson }
11133c0d9c63SKent Gibson 
11143c0d9c63SKent Gibson static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
11153c0d9c63SKent Gibson 					unsigned int num_lines)
11163c0d9c63SKent Gibson {
11173c0d9c63SKent Gibson 	unsigned int i;
11183c0d9c63SKent Gibson 	u64 flags;
11193c0d9c63SKent Gibson 	int ret;
11203c0d9c63SKent Gibson 
11213c0d9c63SKent Gibson 	if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
11223c0d9c63SKent Gibson 		return -EINVAL;
11233c0d9c63SKent Gibson 
11243c0d9c63SKent Gibson 	if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
11253c0d9c63SKent Gibson 		return -EINVAL;
11263c0d9c63SKent Gibson 
11273c0d9c63SKent Gibson 	for (i = 0; i < num_lines; i++) {
11283c0d9c63SKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
11293c0d9c63SKent Gibson 		ret = gpio_v2_line_flags_validate(flags);
11303c0d9c63SKent Gibson 		if (ret)
11313c0d9c63SKent Gibson 			return ret;
113265cff704SKent Gibson 
113365cff704SKent Gibson 		/* debounce requires explicit input */
113465cff704SKent Gibson 		if (gpio_v2_line_config_debounced(lc, i) &&
113565cff704SKent Gibson 		    !(flags & GPIO_V2_LINE_FLAG_INPUT))
113665cff704SKent Gibson 			return -EINVAL;
11373c0d9c63SKent Gibson 	}
11383c0d9c63SKent Gibson 	return 0;
11393c0d9c63SKent Gibson }
11403c0d9c63SKent Gibson 
11413c0d9c63SKent Gibson static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
11423c0d9c63SKent Gibson 						    unsigned long *flagsp)
11433c0d9c63SKent Gibson {
11443c0d9c63SKent Gibson 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
11453c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
11463c0d9c63SKent Gibson 
11473c0d9c63SKent Gibson 	if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
11483c0d9c63SKent Gibson 		set_bit(FLAG_IS_OUT, flagsp);
11493c0d9c63SKent Gibson 	else if (flags & GPIO_V2_LINE_FLAG_INPUT)
11503c0d9c63SKent Gibson 		clear_bit(FLAG_IS_OUT, flagsp);
11513c0d9c63SKent Gibson 
115273e03419SKent Gibson 	assign_bit(FLAG_EDGE_RISING, flagsp,
115373e03419SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
115473e03419SKent Gibson 	assign_bit(FLAG_EDGE_FALLING, flagsp,
115573e03419SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
115673e03419SKent Gibson 
11573c0d9c63SKent Gibson 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
11583c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
11593c0d9c63SKent Gibson 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
11603c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
11613c0d9c63SKent Gibson 
11623c0d9c63SKent Gibson 	assign_bit(FLAG_PULL_UP, flagsp,
11633c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
11643c0d9c63SKent Gibson 	assign_bit(FLAG_PULL_DOWN, flagsp,
11653c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
11663c0d9c63SKent Gibson 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
11673c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
116826d060e4SKent Gibson 
116926d060e4SKent Gibson 	assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
117026d060e4SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
1171*2068339aSDipen Patel 	assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp,
1172*2068339aSDipen Patel 		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
11733c0d9c63SKent Gibson }
11743c0d9c63SKent Gibson 
11753c0d9c63SKent Gibson static long linereq_get_values(struct linereq *lr, void __user *ip)
11763c0d9c63SKent Gibson {
11773c0d9c63SKent Gibson 	struct gpio_v2_line_values lv;
11783c0d9c63SKent Gibson 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
11793c0d9c63SKent Gibson 	struct gpio_desc **descs;
11803c0d9c63SKent Gibson 	unsigned int i, didx, num_get;
118165cff704SKent Gibson 	bool val;
11823c0d9c63SKent Gibson 	int ret;
11833c0d9c63SKent Gibson 
11843c0d9c63SKent Gibson 	/* NOTE: It's ok to read values of output lines. */
11853c0d9c63SKent Gibson 	if (copy_from_user(&lv, ip, sizeof(lv)))
11863c0d9c63SKent Gibson 		return -EFAULT;
11873c0d9c63SKent Gibson 
11883c0d9c63SKent Gibson 	for (num_get = 0, i = 0; i < lr->num_lines; i++) {
11893c0d9c63SKent Gibson 		if (lv.mask & BIT_ULL(i)) {
11903c0d9c63SKent Gibson 			num_get++;
11913c0d9c63SKent Gibson 			descs = &lr->lines[i].desc;
11923c0d9c63SKent Gibson 		}
11933c0d9c63SKent Gibson 	}
11943c0d9c63SKent Gibson 
11953c0d9c63SKent Gibson 	if (num_get == 0)
11963c0d9c63SKent Gibson 		return -EINVAL;
11973c0d9c63SKent Gibson 
11983c0d9c63SKent Gibson 	if (num_get != 1) {
11993c0d9c63SKent Gibson 		descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
12003c0d9c63SKent Gibson 		if (!descs)
12013c0d9c63SKent Gibson 			return -ENOMEM;
12023c0d9c63SKent Gibson 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
12033c0d9c63SKent Gibson 			if (lv.mask & BIT_ULL(i)) {
12043c0d9c63SKent Gibson 				descs[didx] = lr->lines[i].desc;
12053c0d9c63SKent Gibson 				didx++;
12063c0d9c63SKent Gibson 			}
12073c0d9c63SKent Gibson 		}
12083c0d9c63SKent Gibson 	}
12093c0d9c63SKent Gibson 	ret = gpiod_get_array_value_complex(false, true, num_get,
12103c0d9c63SKent Gibson 					    descs, NULL, vals);
12113c0d9c63SKent Gibson 
12123c0d9c63SKent Gibson 	if (num_get != 1)
12133c0d9c63SKent Gibson 		kfree(descs);
12143c0d9c63SKent Gibson 	if (ret)
12153c0d9c63SKent Gibson 		return ret;
12163c0d9c63SKent Gibson 
12173c0d9c63SKent Gibson 	lv.bits = 0;
12183c0d9c63SKent Gibson 	for (didx = 0, i = 0; i < lr->num_lines; i++) {
12193c0d9c63SKent Gibson 		if (lv.mask & BIT_ULL(i)) {
122065cff704SKent Gibson 			if (lr->lines[i].sw_debounced)
122165cff704SKent Gibson 				val = debounced_value(&lr->lines[i]);
122265cff704SKent Gibson 			else
122365cff704SKent Gibson 				val = test_bit(didx, vals);
122465cff704SKent Gibson 			if (val)
12253c0d9c63SKent Gibson 				lv.bits |= BIT_ULL(i);
12263c0d9c63SKent Gibson 			didx++;
12273c0d9c63SKent Gibson 		}
12283c0d9c63SKent Gibson 	}
12293c0d9c63SKent Gibson 
12303c0d9c63SKent Gibson 	if (copy_to_user(ip, &lv, sizeof(lv)))
12313c0d9c63SKent Gibson 		return -EFAULT;
12323c0d9c63SKent Gibson 
12333c0d9c63SKent Gibson 	return 0;
12343c0d9c63SKent Gibson }
12353c0d9c63SKent Gibson 
12367b8e00d9SKent Gibson static long linereq_set_values_unlocked(struct linereq *lr,
12377b8e00d9SKent Gibson 					struct gpio_v2_line_values *lv)
12387b8e00d9SKent Gibson {
12397b8e00d9SKent Gibson 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
12407b8e00d9SKent Gibson 	struct gpio_desc **descs;
12417b8e00d9SKent Gibson 	unsigned int i, didx, num_set;
12427b8e00d9SKent Gibson 	int ret;
12437b8e00d9SKent Gibson 
12447b8e00d9SKent Gibson 	bitmap_zero(vals, GPIO_V2_LINES_MAX);
12457b8e00d9SKent Gibson 	for (num_set = 0, i = 0; i < lr->num_lines; i++) {
12467b8e00d9SKent Gibson 		if (lv->mask & BIT_ULL(i)) {
12477b8e00d9SKent Gibson 			if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
12487b8e00d9SKent Gibson 				return -EPERM;
12497b8e00d9SKent Gibson 			if (lv->bits & BIT_ULL(i))
12507b8e00d9SKent Gibson 				__set_bit(num_set, vals);
12517b8e00d9SKent Gibson 			num_set++;
12527b8e00d9SKent Gibson 			descs = &lr->lines[i].desc;
12537b8e00d9SKent Gibson 		}
12547b8e00d9SKent Gibson 	}
12557b8e00d9SKent Gibson 	if (num_set == 0)
12567b8e00d9SKent Gibson 		return -EINVAL;
12577b8e00d9SKent Gibson 
12587b8e00d9SKent Gibson 	if (num_set != 1) {
12597b8e00d9SKent Gibson 		/* build compacted desc array and values */
12607b8e00d9SKent Gibson 		descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
12617b8e00d9SKent Gibson 		if (!descs)
12627b8e00d9SKent Gibson 			return -ENOMEM;
12637b8e00d9SKent Gibson 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
12647b8e00d9SKent Gibson 			if (lv->mask & BIT_ULL(i)) {
12657b8e00d9SKent Gibson 				descs[didx] = lr->lines[i].desc;
12667b8e00d9SKent Gibson 				didx++;
12677b8e00d9SKent Gibson 			}
12687b8e00d9SKent Gibson 		}
12697b8e00d9SKent Gibson 	}
12707b8e00d9SKent Gibson 	ret = gpiod_set_array_value_complex(false, true, num_set,
12717b8e00d9SKent Gibson 					    descs, NULL, vals);
12727b8e00d9SKent Gibson 
12737b8e00d9SKent Gibson 	if (num_set != 1)
12747b8e00d9SKent Gibson 		kfree(descs);
12757b8e00d9SKent Gibson 	return ret;
12767b8e00d9SKent Gibson }
12777b8e00d9SKent Gibson 
12787b8e00d9SKent Gibson static long linereq_set_values(struct linereq *lr, void __user *ip)
12797b8e00d9SKent Gibson {
12807b8e00d9SKent Gibson 	struct gpio_v2_line_values lv;
12817b8e00d9SKent Gibson 	int ret;
12827b8e00d9SKent Gibson 
12837b8e00d9SKent Gibson 	if (copy_from_user(&lv, ip, sizeof(lv)))
12847b8e00d9SKent Gibson 		return -EFAULT;
12857b8e00d9SKent Gibson 
12867b8e00d9SKent Gibson 	mutex_lock(&lr->config_mutex);
12877b8e00d9SKent Gibson 
12887b8e00d9SKent Gibson 	ret = linereq_set_values_unlocked(lr, &lv);
12897b8e00d9SKent Gibson 
12907b8e00d9SKent Gibson 	mutex_unlock(&lr->config_mutex);
12917b8e00d9SKent Gibson 
12927b8e00d9SKent Gibson 	return ret;
12937b8e00d9SKent Gibson }
12947b8e00d9SKent Gibson 
1295a54756cbSKent Gibson static long linereq_set_config_unlocked(struct linereq *lr,
1296a54756cbSKent Gibson 					struct gpio_v2_line_config *lc)
1297a54756cbSKent Gibson {
1298a54756cbSKent Gibson 	struct gpio_desc *desc;
1299a54756cbSKent Gibson 	unsigned int i;
1300a54756cbSKent Gibson 	u64 flags;
1301a54756cbSKent Gibson 	bool polarity_change;
1302*2068339aSDipen Patel 	bool prev_hte_flag;
1303a54756cbSKent Gibson 	int ret;
1304a54756cbSKent Gibson 
1305a54756cbSKent Gibson 	for (i = 0; i < lr->num_lines; i++) {
1306a54756cbSKent Gibson 		desc = lr->lines[i].desc;
1307a54756cbSKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
1308a54756cbSKent Gibson 		polarity_change =
1309a54756cbSKent Gibson 			(!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) !=
1310a54756cbSKent Gibson 			 ((flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) != 0));
1311a54756cbSKent Gibson 
1312*2068339aSDipen Patel 		prev_hte_flag = !!test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags);
1313*2068339aSDipen Patel 
1314a54756cbSKent Gibson 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1315a54756cbSKent Gibson 		/*
1316a54756cbSKent Gibson 		 * Lines have to be requested explicitly for input
1317a54756cbSKent Gibson 		 * or output, else the line will be treated "as is".
1318a54756cbSKent Gibson 		 */
1319a54756cbSKent Gibson 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1320a54756cbSKent Gibson 			int val = gpio_v2_line_config_output_value(lc, i);
1321a54756cbSKent Gibson 
1322*2068339aSDipen Patel 			edge_detector_stop(&lr->lines[i], prev_hte_flag);
1323a54756cbSKent Gibson 			ret = gpiod_direction_output(desc, val);
1324a54756cbSKent Gibson 			if (ret)
1325a54756cbSKent Gibson 				return ret;
1326a54756cbSKent Gibson 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1327a54756cbSKent Gibson 			ret = gpiod_direction_input(desc);
1328a54756cbSKent Gibson 			if (ret)
1329a54756cbSKent Gibson 				return ret;
1330a54756cbSKent Gibson 
133165cff704SKent Gibson 			ret = edge_detector_update(&lr->lines[i], lc, i,
1332*2068339aSDipen Patel 					flags, polarity_change, prev_hte_flag);
1333a54756cbSKent Gibson 			if (ret)
1334a54756cbSKent Gibson 				return ret;
1335a54756cbSKent Gibson 		}
1336a54756cbSKent Gibson 
1337a54756cbSKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
1338a54756cbSKent Gibson 					     GPIO_V2_LINE_CHANGED_CONFIG,
1339a54756cbSKent Gibson 					     desc);
1340a54756cbSKent Gibson 	}
1341a54756cbSKent Gibson 	return 0;
1342a54756cbSKent Gibson }
1343a54756cbSKent Gibson 
1344a54756cbSKent Gibson static long linereq_set_config(struct linereq *lr, void __user *ip)
1345a54756cbSKent Gibson {
1346a54756cbSKent Gibson 	struct gpio_v2_line_config lc;
1347a54756cbSKent Gibson 	int ret;
1348a54756cbSKent Gibson 
1349a54756cbSKent Gibson 	if (copy_from_user(&lc, ip, sizeof(lc)))
1350a54756cbSKent Gibson 		return -EFAULT;
1351a54756cbSKent Gibson 
1352a54756cbSKent Gibson 	ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
1353a54756cbSKent Gibson 	if (ret)
1354a54756cbSKent Gibson 		return ret;
1355a54756cbSKent Gibson 
1356a54756cbSKent Gibson 	mutex_lock(&lr->config_mutex);
1357a54756cbSKent Gibson 
1358a54756cbSKent Gibson 	ret = linereq_set_config_unlocked(lr, &lc);
1359a54756cbSKent Gibson 
1360a54756cbSKent Gibson 	mutex_unlock(&lr->config_mutex);
1361a54756cbSKent Gibson 
1362a54756cbSKent Gibson 	return ret;
1363a54756cbSKent Gibson }
1364a54756cbSKent Gibson 
13653c0d9c63SKent Gibson static long linereq_ioctl(struct file *file, unsigned int cmd,
13663c0d9c63SKent Gibson 			  unsigned long arg)
13673c0d9c63SKent Gibson {
13683c0d9c63SKent Gibson 	struct linereq *lr = file->private_data;
13693c0d9c63SKent Gibson 	void __user *ip = (void __user *)arg;
13703c0d9c63SKent Gibson 
13713c0d9c63SKent Gibson 	if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL)
13723c0d9c63SKent Gibson 		return linereq_get_values(lr, ip);
13737b8e00d9SKent Gibson 	else if (cmd == GPIO_V2_LINE_SET_VALUES_IOCTL)
13747b8e00d9SKent Gibson 		return linereq_set_values(lr, ip);
1375a54756cbSKent Gibson 	else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL)
1376a54756cbSKent Gibson 		return linereq_set_config(lr, ip);
13773c0d9c63SKent Gibson 
13783c0d9c63SKent Gibson 	return -EINVAL;
13793c0d9c63SKent Gibson }
13803c0d9c63SKent Gibson 
13813c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
13823c0d9c63SKent Gibson static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
13833c0d9c63SKent Gibson 				 unsigned long arg)
13843c0d9c63SKent Gibson {
13853c0d9c63SKent Gibson 	return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
13863c0d9c63SKent Gibson }
13873c0d9c63SKent Gibson #endif
13883c0d9c63SKent Gibson 
138973e03419SKent Gibson static __poll_t linereq_poll(struct file *file,
139073e03419SKent Gibson 			    struct poll_table_struct *wait)
139173e03419SKent Gibson {
139273e03419SKent Gibson 	struct linereq *lr = file->private_data;
139373e03419SKent Gibson 	__poll_t events = 0;
139473e03419SKent Gibson 
139573e03419SKent Gibson 	poll_wait(file, &lr->wait, wait);
139673e03419SKent Gibson 
139773e03419SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
139873e03419SKent Gibson 						 &lr->wait.lock))
139973e03419SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
140073e03419SKent Gibson 
140173e03419SKent Gibson 	return events;
140273e03419SKent Gibson }
140373e03419SKent Gibson 
140473e03419SKent Gibson static ssize_t linereq_read(struct file *file,
140573e03419SKent Gibson 			    char __user *buf,
140673e03419SKent Gibson 			    size_t count,
140773e03419SKent Gibson 			    loff_t *f_ps)
140873e03419SKent Gibson {
140973e03419SKent Gibson 	struct linereq *lr = file->private_data;
141073e03419SKent Gibson 	struct gpio_v2_line_event le;
141173e03419SKent Gibson 	ssize_t bytes_read = 0;
141273e03419SKent Gibson 	int ret;
141373e03419SKent Gibson 
141473e03419SKent Gibson 	if (count < sizeof(le))
141573e03419SKent Gibson 		return -EINVAL;
141673e03419SKent Gibson 
141773e03419SKent Gibson 	do {
141873e03419SKent Gibson 		spin_lock(&lr->wait.lock);
141973e03419SKent Gibson 		if (kfifo_is_empty(&lr->events)) {
142073e03419SKent Gibson 			if (bytes_read) {
142173e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
142273e03419SKent Gibson 				return bytes_read;
142373e03419SKent Gibson 			}
142473e03419SKent Gibson 
142573e03419SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
142673e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
142773e03419SKent Gibson 				return -EAGAIN;
142873e03419SKent Gibson 			}
142973e03419SKent Gibson 
143073e03419SKent Gibson 			ret = wait_event_interruptible_locked(lr->wait,
143173e03419SKent Gibson 					!kfifo_is_empty(&lr->events));
143273e03419SKent Gibson 			if (ret) {
143373e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
143473e03419SKent Gibson 				return ret;
143573e03419SKent Gibson 			}
143673e03419SKent Gibson 		}
143773e03419SKent Gibson 
143873e03419SKent Gibson 		ret = kfifo_out(&lr->events, &le, 1);
143973e03419SKent Gibson 		spin_unlock(&lr->wait.lock);
144073e03419SKent Gibson 		if (ret != 1) {
144173e03419SKent Gibson 			/*
144273e03419SKent Gibson 			 * This should never happen - we were holding the
144373e03419SKent Gibson 			 * lock from the moment we learned the fifo is no
144473e03419SKent Gibson 			 * longer empty until now.
144573e03419SKent Gibson 			 */
144673e03419SKent Gibson 			ret = -EIO;
144773e03419SKent Gibson 			break;
144873e03419SKent Gibson 		}
144973e03419SKent Gibson 
145073e03419SKent Gibson 		if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
145173e03419SKent Gibson 			return -EFAULT;
145273e03419SKent Gibson 		bytes_read += sizeof(le);
145373e03419SKent Gibson 	} while (count >= bytes_read + sizeof(le));
145473e03419SKent Gibson 
145573e03419SKent Gibson 	return bytes_read;
145673e03419SKent Gibson }
145773e03419SKent Gibson 
14583c0d9c63SKent Gibson static void linereq_free(struct linereq *lr)
14593c0d9c63SKent Gibson {
14603c0d9c63SKent Gibson 	unsigned int i;
1461*2068339aSDipen Patel 	bool hte;
14623c0d9c63SKent Gibson 
14633c0d9c63SKent Gibson 	for (i = 0; i < lr->num_lines; i++) {
1464*2068339aSDipen Patel 		hte = !!test_bit(FLAG_EVENT_CLOCK_HTE,
1465*2068339aSDipen Patel 				 &lr->lines[i].desc->flags);
1466*2068339aSDipen Patel 		edge_detector_stop(&lr->lines[i], hte);
14673c0d9c63SKent Gibson 		if (lr->lines[i].desc)
14683c0d9c63SKent Gibson 			gpiod_free(lr->lines[i].desc);
14693c0d9c63SKent Gibson 	}
147073e03419SKent Gibson 	kfifo_free(&lr->events);
14713c0d9c63SKent Gibson 	kfree(lr->label);
14723c0d9c63SKent Gibson 	put_device(&lr->gdev->dev);
14733c0d9c63SKent Gibson 	kfree(lr);
14743c0d9c63SKent Gibson }
14753c0d9c63SKent Gibson 
14763c0d9c63SKent Gibson static int linereq_release(struct inode *inode, struct file *file)
14773c0d9c63SKent Gibson {
14783c0d9c63SKent Gibson 	struct linereq *lr = file->private_data;
14793c0d9c63SKent Gibson 
14803c0d9c63SKent Gibson 	linereq_free(lr);
14813c0d9c63SKent Gibson 	return 0;
14823c0d9c63SKent Gibson }
14833c0d9c63SKent Gibson 
14843c0d9c63SKent Gibson static const struct file_operations line_fileops = {
14853c0d9c63SKent Gibson 	.release = linereq_release,
148673e03419SKent Gibson 	.read = linereq_read,
148773e03419SKent Gibson 	.poll = linereq_poll,
14883c0d9c63SKent Gibson 	.owner = THIS_MODULE,
14893c0d9c63SKent Gibson 	.llseek = noop_llseek,
14903c0d9c63SKent Gibson 	.unlocked_ioctl = linereq_ioctl,
14913c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
14923c0d9c63SKent Gibson 	.compat_ioctl = linereq_ioctl_compat,
14933c0d9c63SKent Gibson #endif
14943c0d9c63SKent Gibson };
14953c0d9c63SKent Gibson 
14963c0d9c63SKent Gibson static int linereq_create(struct gpio_device *gdev, void __user *ip)
14973c0d9c63SKent Gibson {
14983c0d9c63SKent Gibson 	struct gpio_v2_line_request ulr;
14993c0d9c63SKent Gibson 	struct gpio_v2_line_config *lc;
15003c0d9c63SKent Gibson 	struct linereq *lr;
15013c0d9c63SKent Gibson 	struct file *file;
15023c0d9c63SKent Gibson 	u64 flags;
15033c0d9c63SKent Gibson 	unsigned int i;
15043c0d9c63SKent Gibson 	int fd, ret;
15053c0d9c63SKent Gibson 
15063c0d9c63SKent Gibson 	if (copy_from_user(&ulr, ip, sizeof(ulr)))
15073c0d9c63SKent Gibson 		return -EFAULT;
15083c0d9c63SKent Gibson 
15093c0d9c63SKent Gibson 	if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
15103c0d9c63SKent Gibson 		return -EINVAL;
15113c0d9c63SKent Gibson 
15123c0d9c63SKent Gibson 	if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
15133c0d9c63SKent Gibson 		return -EINVAL;
15143c0d9c63SKent Gibson 
15153c0d9c63SKent Gibson 	lc = &ulr.config;
15163c0d9c63SKent Gibson 	ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
15173c0d9c63SKent Gibson 	if (ret)
15183c0d9c63SKent Gibson 		return ret;
15193c0d9c63SKent Gibson 
15203c0d9c63SKent Gibson 	lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
15213c0d9c63SKent Gibson 	if (!lr)
15223c0d9c63SKent Gibson 		return -ENOMEM;
15233c0d9c63SKent Gibson 
15243c0d9c63SKent Gibson 	lr->gdev = gdev;
15253c0d9c63SKent Gibson 	get_device(&gdev->dev);
15263c0d9c63SKent Gibson 
152765cff704SKent Gibson 	for (i = 0; i < ulr.num_lines; i++) {
152873e03419SKent Gibson 		lr->lines[i].req = lr;
152965cff704SKent Gibson 		WRITE_ONCE(lr->lines[i].sw_debounced, 0);
153065cff704SKent Gibson 		INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
153165cff704SKent Gibson 	}
153273e03419SKent Gibson 
1533f188ac12SKent Gibson 	if (ulr.consumer[0] != '\0') {
15343c0d9c63SKent Gibson 		/* label is only initialized if consumer is set */
1535f188ac12SKent Gibson 		lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1536f188ac12SKent Gibson 				     GFP_KERNEL);
15373c0d9c63SKent Gibson 		if (!lr->label) {
15383c0d9c63SKent Gibson 			ret = -ENOMEM;
15393c0d9c63SKent Gibson 			goto out_free_linereq;
15403c0d9c63SKent Gibson 		}
15413c0d9c63SKent Gibson 	}
15423c0d9c63SKent Gibson 
1543a54756cbSKent Gibson 	mutex_init(&lr->config_mutex);
154473e03419SKent Gibson 	init_waitqueue_head(&lr->wait);
154573e03419SKent Gibson 	lr->event_buffer_size = ulr.event_buffer_size;
154673e03419SKent Gibson 	if (lr->event_buffer_size == 0)
154773e03419SKent Gibson 		lr->event_buffer_size = ulr.num_lines * 16;
154873e03419SKent Gibson 	else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
154973e03419SKent Gibson 		lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
155073e03419SKent Gibson 
155173e03419SKent Gibson 	atomic_set(&lr->seqno, 0);
15523c0d9c63SKent Gibson 	lr->num_lines = ulr.num_lines;
15533c0d9c63SKent Gibson 
15543c0d9c63SKent Gibson 	/* Request each GPIO */
15553c0d9c63SKent Gibson 	for (i = 0; i < ulr.num_lines; i++) {
15563c0d9c63SKent Gibson 		u32 offset = ulr.offsets[i];
15573c0d9c63SKent Gibson 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
15583c0d9c63SKent Gibson 
15593c0d9c63SKent Gibson 		if (IS_ERR(desc)) {
15603c0d9c63SKent Gibson 			ret = PTR_ERR(desc);
15613c0d9c63SKent Gibson 			goto out_free_linereq;
15623c0d9c63SKent Gibson 		}
15633c0d9c63SKent Gibson 
156495a4eed7SAndy Shevchenko 		ret = gpiod_request_user(desc, lr->label);
15653c0d9c63SKent Gibson 		if (ret)
15663c0d9c63SKent Gibson 			goto out_free_linereq;
15673c0d9c63SKent Gibson 
15683c0d9c63SKent Gibson 		lr->lines[i].desc = desc;
15693c0d9c63SKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
15703c0d9c63SKent Gibson 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
15713c0d9c63SKent Gibson 
15723c0d9c63SKent Gibson 		ret = gpiod_set_transitory(desc, false);
15733c0d9c63SKent Gibson 		if (ret < 0)
15743c0d9c63SKent Gibson 			goto out_free_linereq;
15753c0d9c63SKent Gibson 
15763c0d9c63SKent Gibson 		/*
15773c0d9c63SKent Gibson 		 * Lines have to be requested explicitly for input
15783c0d9c63SKent Gibson 		 * or output, else the line will be treated "as is".
15793c0d9c63SKent Gibson 		 */
15803c0d9c63SKent Gibson 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
15813c0d9c63SKent Gibson 			int val = gpio_v2_line_config_output_value(lc, i);
15823c0d9c63SKent Gibson 
15833c0d9c63SKent Gibson 			ret = gpiod_direction_output(desc, val);
15843c0d9c63SKent Gibson 			if (ret)
15853c0d9c63SKent Gibson 				goto out_free_linereq;
15863c0d9c63SKent Gibson 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
15873c0d9c63SKent Gibson 			ret = gpiod_direction_input(desc);
15883c0d9c63SKent Gibson 			if (ret)
15893c0d9c63SKent Gibson 				goto out_free_linereq;
159073e03419SKent Gibson 
159165cff704SKent Gibson 			ret = edge_detector_setup(&lr->lines[i], lc, i,
1592*2068339aSDipen Patel 				flags & GPIO_V2_LINE_EDGE_FLAGS,
1593*2068339aSDipen Patel 				flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
159473e03419SKent Gibson 			if (ret)
159573e03419SKent Gibson 				goto out_free_linereq;
15963c0d9c63SKent Gibson 		}
15973c0d9c63SKent Gibson 
15983c0d9c63SKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
1599aad95584SKent Gibson 					     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
16003c0d9c63SKent Gibson 
16013c0d9c63SKent Gibson 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
16023c0d9c63SKent Gibson 			offset);
16033c0d9c63SKent Gibson 	}
16043c0d9c63SKent Gibson 
16053c0d9c63SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
16063c0d9c63SKent Gibson 	if (fd < 0) {
16073c0d9c63SKent Gibson 		ret = fd;
16083c0d9c63SKent Gibson 		goto out_free_linereq;
16093c0d9c63SKent Gibson 	}
16103c0d9c63SKent Gibson 
16113c0d9c63SKent Gibson 	file = anon_inode_getfile("gpio-line", &line_fileops, lr,
16123c0d9c63SKent Gibson 				  O_RDONLY | O_CLOEXEC);
16133c0d9c63SKent Gibson 	if (IS_ERR(file)) {
16143c0d9c63SKent Gibson 		ret = PTR_ERR(file);
16153c0d9c63SKent Gibson 		goto out_put_unused_fd;
16163c0d9c63SKent Gibson 	}
16173c0d9c63SKent Gibson 
16183c0d9c63SKent Gibson 	ulr.fd = fd;
16193c0d9c63SKent Gibson 	if (copy_to_user(ip, &ulr, sizeof(ulr))) {
16203c0d9c63SKent Gibson 		/*
16213c0d9c63SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
16223c0d9c63SKent Gibson 		 * the regular error cleanup path here.
16233c0d9c63SKent Gibson 		 */
16243c0d9c63SKent Gibson 		fput(file);
16253c0d9c63SKent Gibson 		put_unused_fd(fd);
16263c0d9c63SKent Gibson 		return -EFAULT;
16273c0d9c63SKent Gibson 	}
16283c0d9c63SKent Gibson 
16293c0d9c63SKent Gibson 	fd_install(fd, file);
16303c0d9c63SKent Gibson 
16313c0d9c63SKent Gibson 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
16323c0d9c63SKent Gibson 		lr->num_lines);
16333c0d9c63SKent Gibson 
16343c0d9c63SKent Gibson 	return 0;
16353c0d9c63SKent Gibson 
16363c0d9c63SKent Gibson out_put_unused_fd:
16373c0d9c63SKent Gibson 	put_unused_fd(fd);
16383c0d9c63SKent Gibson out_free_linereq:
16393c0d9c63SKent Gibson 	linereq_free(lr);
16403c0d9c63SKent Gibson 	return ret;
16413c0d9c63SKent Gibson }
16423c0d9c63SKent Gibson 
16433c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1644925ca369SKent Gibson 
1645925ca369SKent Gibson /*
1646925ca369SKent Gibson  * GPIO line event management
1647925ca369SKent Gibson  */
1648925ca369SKent Gibson 
1649925ca369SKent Gibson /**
1650925ca369SKent Gibson  * struct lineevent_state - contains the state of a userspace event
1651925ca369SKent Gibson  * @gdev: the GPIO device the event pertains to
1652925ca369SKent Gibson  * @label: consumer label used to tag descriptors
1653925ca369SKent Gibson  * @desc: the GPIO descriptor held by this event
1654925ca369SKent Gibson  * @eflags: the event flags this line was requested with
1655925ca369SKent Gibson  * @irq: the interrupt that trigger in response to events on this GPIO
1656925ca369SKent Gibson  * @wait: wait queue that handles blocking reads of events
1657925ca369SKent Gibson  * @events: KFIFO for the GPIO events
1658925ca369SKent Gibson  * @timestamp: cache for the timestamp storing it between hardirq
1659925ca369SKent Gibson  * and IRQ thread, used to bring the timestamp close to the actual
1660925ca369SKent Gibson  * event
1661925ca369SKent Gibson  */
1662925ca369SKent Gibson struct lineevent_state {
1663925ca369SKent Gibson 	struct gpio_device *gdev;
1664925ca369SKent Gibson 	const char *label;
1665925ca369SKent Gibson 	struct gpio_desc *desc;
1666925ca369SKent Gibson 	u32 eflags;
1667925ca369SKent Gibson 	int irq;
1668925ca369SKent Gibson 	wait_queue_head_t wait;
1669925ca369SKent Gibson 	DECLARE_KFIFO(events, struct gpioevent_data, 16);
1670925ca369SKent Gibson 	u64 timestamp;
1671925ca369SKent Gibson };
1672925ca369SKent Gibson 
1673925ca369SKent Gibson #define GPIOEVENT_REQUEST_VALID_FLAGS \
1674925ca369SKent Gibson 	(GPIOEVENT_REQUEST_RISING_EDGE | \
1675925ca369SKent Gibson 	GPIOEVENT_REQUEST_FALLING_EDGE)
1676925ca369SKent Gibson 
167749bc5279SKent Gibson static __poll_t lineevent_poll(struct file *file,
1678925ca369SKent Gibson 			       struct poll_table_struct *wait)
1679925ca369SKent Gibson {
168049bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1681925ca369SKent Gibson 	__poll_t events = 0;
1682925ca369SKent Gibson 
168349bc5279SKent Gibson 	poll_wait(file, &le->wait, wait);
1684925ca369SKent Gibson 
1685925ca369SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1686925ca369SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
1687925ca369SKent Gibson 
1688925ca369SKent Gibson 	return events;
1689925ca369SKent Gibson }
1690925ca369SKent Gibson 
16915ad284abSAndy Shevchenko struct compat_gpioeevent_data {
16925ad284abSAndy Shevchenko 	compat_u64	timestamp;
16935ad284abSAndy Shevchenko 	u32		id;
16945ad284abSAndy Shevchenko };
16955ad284abSAndy Shevchenko 
169649bc5279SKent Gibson static ssize_t lineevent_read(struct file *file,
1697925ca369SKent Gibson 			      char __user *buf,
1698925ca369SKent Gibson 			      size_t count,
1699925ca369SKent Gibson 			      loff_t *f_ps)
1700925ca369SKent Gibson {
170149bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1702925ca369SKent Gibson 	struct gpioevent_data ge;
1703925ca369SKent Gibson 	ssize_t bytes_read = 0;
17045ad284abSAndy Shevchenko 	ssize_t ge_size;
1705925ca369SKent Gibson 	int ret;
1706925ca369SKent Gibson 
17075ad284abSAndy Shevchenko 	/*
17085ad284abSAndy Shevchenko 	 * When compatible system call is being used the struct gpioevent_data,
17095ad284abSAndy Shevchenko 	 * in case of at least ia32, has different size due to the alignment
17105ad284abSAndy Shevchenko 	 * differences. Because we have first member 64 bits followed by one of
17115ad284abSAndy Shevchenko 	 * 32 bits there is no gap between them. The only difference is the
17125ad284abSAndy Shevchenko 	 * padding at the end of the data structure. Hence, we calculate the
17135ad284abSAndy Shevchenko 	 * actual sizeof() and pass this as an argument to copy_to_user() to
17145ad284abSAndy Shevchenko 	 * drop unneeded bytes from the output.
17155ad284abSAndy Shevchenko 	 */
1716163d1719SAndy Shevchenko 	if (compat_need_64bit_alignment_fixup())
1717163d1719SAndy Shevchenko 		ge_size = sizeof(struct compat_gpioeevent_data);
1718163d1719SAndy Shevchenko 	else
1719163d1719SAndy Shevchenko 		ge_size = sizeof(struct gpioevent_data);
17205ad284abSAndy Shevchenko 	if (count < ge_size)
1721925ca369SKent Gibson 		return -EINVAL;
1722925ca369SKent Gibson 
1723925ca369SKent Gibson 	do {
1724925ca369SKent Gibson 		spin_lock(&le->wait.lock);
1725925ca369SKent Gibson 		if (kfifo_is_empty(&le->events)) {
1726925ca369SKent Gibson 			if (bytes_read) {
1727925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1728925ca369SKent Gibson 				return bytes_read;
1729925ca369SKent Gibson 			}
1730925ca369SKent Gibson 
173149bc5279SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
1732925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1733925ca369SKent Gibson 				return -EAGAIN;
1734925ca369SKent Gibson 			}
1735925ca369SKent Gibson 
1736925ca369SKent Gibson 			ret = wait_event_interruptible_locked(le->wait,
1737925ca369SKent Gibson 					!kfifo_is_empty(&le->events));
1738925ca369SKent Gibson 			if (ret) {
1739925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1740925ca369SKent Gibson 				return ret;
1741925ca369SKent Gibson 			}
1742925ca369SKent Gibson 		}
1743925ca369SKent Gibson 
1744925ca369SKent Gibson 		ret = kfifo_out(&le->events, &ge, 1);
1745925ca369SKent Gibson 		spin_unlock(&le->wait.lock);
1746925ca369SKent Gibson 		if (ret != 1) {
1747925ca369SKent Gibson 			/*
1748925ca369SKent Gibson 			 * This should never happen - we were holding the lock
1749925ca369SKent Gibson 			 * from the moment we learned the fifo is no longer
1750925ca369SKent Gibson 			 * empty until now.
1751925ca369SKent Gibson 			 */
1752925ca369SKent Gibson 			ret = -EIO;
1753925ca369SKent Gibson 			break;
1754925ca369SKent Gibson 		}
1755925ca369SKent Gibson 
17565ad284abSAndy Shevchenko 		if (copy_to_user(buf + bytes_read, &ge, ge_size))
1757925ca369SKent Gibson 			return -EFAULT;
17585ad284abSAndy Shevchenko 		bytes_read += ge_size;
17595ad284abSAndy Shevchenko 	} while (count >= bytes_read + ge_size);
1760925ca369SKent Gibson 
1761925ca369SKent Gibson 	return bytes_read;
1762925ca369SKent Gibson }
1763925ca369SKent Gibson 
176446824272SKent Gibson static void lineevent_free(struct lineevent_state *le)
1765925ca369SKent Gibson {
176646824272SKent Gibson 	if (le->irq)
1767925ca369SKent Gibson 		free_irq(le->irq, le);
176846824272SKent Gibson 	if (le->desc)
1769925ca369SKent Gibson 		gpiod_free(le->desc);
1770925ca369SKent Gibson 	kfree(le->label);
177146824272SKent Gibson 	put_device(&le->gdev->dev);
1772925ca369SKent Gibson 	kfree(le);
177346824272SKent Gibson }
177446824272SKent Gibson 
177546824272SKent Gibson static int lineevent_release(struct inode *inode, struct file *file)
177646824272SKent Gibson {
177746824272SKent Gibson 	lineevent_free(file->private_data);
1778925ca369SKent Gibson 	return 0;
1779925ca369SKent Gibson }
1780925ca369SKent Gibson 
178149bc5279SKent Gibson static long lineevent_ioctl(struct file *file, unsigned int cmd,
1782925ca369SKent Gibson 			    unsigned long arg)
1783925ca369SKent Gibson {
178449bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1785925ca369SKent Gibson 	void __user *ip = (void __user *)arg;
1786925ca369SKent Gibson 	struct gpiohandle_data ghd;
1787925ca369SKent Gibson 
1788925ca369SKent Gibson 	/*
1789925ca369SKent Gibson 	 * We can get the value for an event line but not set it,
1790925ca369SKent Gibson 	 * because it is input by definition.
1791925ca369SKent Gibson 	 */
1792925ca369SKent Gibson 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
1793925ca369SKent Gibson 		int val;
1794925ca369SKent Gibson 
1795925ca369SKent Gibson 		memset(&ghd, 0, sizeof(ghd));
1796925ca369SKent Gibson 
1797925ca369SKent Gibson 		val = gpiod_get_value_cansleep(le->desc);
1798925ca369SKent Gibson 		if (val < 0)
1799925ca369SKent Gibson 			return val;
1800925ca369SKent Gibson 		ghd.values[0] = val;
1801925ca369SKent Gibson 
1802925ca369SKent Gibson 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
1803925ca369SKent Gibson 			return -EFAULT;
1804925ca369SKent Gibson 
1805925ca369SKent Gibson 		return 0;
1806925ca369SKent Gibson 	}
1807925ca369SKent Gibson 	return -EINVAL;
1808925ca369SKent Gibson }
1809925ca369SKent Gibson 
1810925ca369SKent Gibson #ifdef CONFIG_COMPAT
181149bc5279SKent Gibson static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
1812925ca369SKent Gibson 				   unsigned long arg)
1813925ca369SKent Gibson {
181449bc5279SKent Gibson 	return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1815925ca369SKent Gibson }
1816925ca369SKent Gibson #endif
1817925ca369SKent Gibson 
1818925ca369SKent Gibson static const struct file_operations lineevent_fileops = {
1819925ca369SKent Gibson 	.release = lineevent_release,
1820925ca369SKent Gibson 	.read = lineevent_read,
1821925ca369SKent Gibson 	.poll = lineevent_poll,
1822925ca369SKent Gibson 	.owner = THIS_MODULE,
1823925ca369SKent Gibson 	.llseek = noop_llseek,
1824925ca369SKent Gibson 	.unlocked_ioctl = lineevent_ioctl,
1825925ca369SKent Gibson #ifdef CONFIG_COMPAT
1826925ca369SKent Gibson 	.compat_ioctl = lineevent_ioctl_compat,
1827925ca369SKent Gibson #endif
1828925ca369SKent Gibson };
1829925ca369SKent Gibson 
1830925ca369SKent Gibson static irqreturn_t lineevent_irq_thread(int irq, void *p)
1831925ca369SKent Gibson {
1832925ca369SKent Gibson 	struct lineevent_state *le = p;
1833925ca369SKent Gibson 	struct gpioevent_data ge;
1834925ca369SKent Gibson 	int ret;
1835925ca369SKent Gibson 
1836925ca369SKent Gibson 	/* Do not leak kernel stack to userspace */
1837925ca369SKent Gibson 	memset(&ge, 0, sizeof(ge));
1838925ca369SKent Gibson 
1839925ca369SKent Gibson 	/*
1840925ca369SKent Gibson 	 * We may be running from a nested threaded interrupt in which case
1841925ca369SKent Gibson 	 * we didn't get the timestamp from lineevent_irq_handler().
1842925ca369SKent Gibson 	 */
1843925ca369SKent Gibson 	if (!le->timestamp)
1844925ca369SKent Gibson 		ge.timestamp = ktime_get_ns();
1845925ca369SKent Gibson 	else
1846925ca369SKent Gibson 		ge.timestamp = le->timestamp;
1847925ca369SKent Gibson 
1848925ca369SKent Gibson 	if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
1849925ca369SKent Gibson 	    && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1850925ca369SKent Gibson 		int level = gpiod_get_value_cansleep(le->desc);
1851925ca369SKent Gibson 
1852925ca369SKent Gibson 		if (level)
1853925ca369SKent Gibson 			/* Emit low-to-high event */
1854925ca369SKent Gibson 			ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1855925ca369SKent Gibson 		else
1856925ca369SKent Gibson 			/* Emit high-to-low event */
1857925ca369SKent Gibson 			ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1858925ca369SKent Gibson 	} else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
1859925ca369SKent Gibson 		/* Emit low-to-high event */
1860925ca369SKent Gibson 		ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1861925ca369SKent Gibson 	} else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1862925ca369SKent Gibson 		/* Emit high-to-low event */
1863925ca369SKent Gibson 		ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1864925ca369SKent Gibson 	} else {
1865925ca369SKent Gibson 		return IRQ_NONE;
1866925ca369SKent Gibson 	}
1867925ca369SKent Gibson 
1868925ca369SKent Gibson 	ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
1869925ca369SKent Gibson 					    1, &le->wait.lock);
1870925ca369SKent Gibson 	if (ret)
1871925ca369SKent Gibson 		wake_up_poll(&le->wait, EPOLLIN);
1872925ca369SKent Gibson 	else
1873925ca369SKent Gibson 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
1874925ca369SKent Gibson 
1875925ca369SKent Gibson 	return IRQ_HANDLED;
1876925ca369SKent Gibson }
1877925ca369SKent Gibson 
1878925ca369SKent Gibson static irqreturn_t lineevent_irq_handler(int irq, void *p)
1879925ca369SKent Gibson {
1880925ca369SKent Gibson 	struct lineevent_state *le = p;
1881925ca369SKent Gibson 
1882925ca369SKent Gibson 	/*
1883925ca369SKent Gibson 	 * Just store the timestamp in hardirq context so we get it as
1884925ca369SKent Gibson 	 * close in time as possible to the actual event.
1885925ca369SKent Gibson 	 */
1886925ca369SKent Gibson 	le->timestamp = ktime_get_ns();
1887925ca369SKent Gibson 
1888925ca369SKent Gibson 	return IRQ_WAKE_THREAD;
1889925ca369SKent Gibson }
1890925ca369SKent Gibson 
1891925ca369SKent Gibson static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1892925ca369SKent Gibson {
1893925ca369SKent Gibson 	struct gpioevent_request eventreq;
1894925ca369SKent Gibson 	struct lineevent_state *le;
1895925ca369SKent Gibson 	struct gpio_desc *desc;
1896925ca369SKent Gibson 	struct file *file;
1897925ca369SKent Gibson 	u32 offset;
1898925ca369SKent Gibson 	u32 lflags;
1899925ca369SKent Gibson 	u32 eflags;
1900925ca369SKent Gibson 	int fd;
1901925ca369SKent Gibson 	int ret;
190246824272SKent Gibson 	int irq, irqflags = 0;
1903925ca369SKent Gibson 
1904925ca369SKent Gibson 	if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1905925ca369SKent Gibson 		return -EFAULT;
1906925ca369SKent Gibson 
1907925ca369SKent Gibson 	offset = eventreq.lineoffset;
1908925ca369SKent Gibson 	lflags = eventreq.handleflags;
1909925ca369SKent Gibson 	eflags = eventreq.eventflags;
1910925ca369SKent Gibson 
1911925ca369SKent Gibson 	desc = gpiochip_get_desc(gdev->chip, offset);
1912925ca369SKent Gibson 	if (IS_ERR(desc))
1913925ca369SKent Gibson 		return PTR_ERR(desc);
1914925ca369SKent Gibson 
1915925ca369SKent Gibson 	/* Return an error if a unknown flag is set */
1916925ca369SKent Gibson 	if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1917925ca369SKent Gibson 	    (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1918925ca369SKent Gibson 		return -EINVAL;
1919925ca369SKent Gibson 
1920925ca369SKent Gibson 	/* This is just wrong: we don't look for events on output lines */
1921925ca369SKent Gibson 	if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1922925ca369SKent Gibson 	    (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1923925ca369SKent Gibson 	    (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1924925ca369SKent Gibson 		return -EINVAL;
1925925ca369SKent Gibson 
1926925ca369SKent Gibson 	/* Only one bias flag can be set. */
1927925ca369SKent Gibson 	if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1928925ca369SKent Gibson 	     (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1929925ca369SKent Gibson 			GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1930925ca369SKent Gibson 	    ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1931925ca369SKent Gibson 	     (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1932925ca369SKent Gibson 		return -EINVAL;
1933925ca369SKent Gibson 
1934925ca369SKent Gibson 	le = kzalloc(sizeof(*le), GFP_KERNEL);
1935925ca369SKent Gibson 	if (!le)
1936925ca369SKent Gibson 		return -ENOMEM;
1937925ca369SKent Gibson 	le->gdev = gdev;
1938925ca369SKent Gibson 	get_device(&gdev->dev);
1939925ca369SKent Gibson 
1940f188ac12SKent Gibson 	if (eventreq.consumer_label[0] != '\0') {
1941f188ac12SKent Gibson 		/* label is only initialized if consumer_label is set */
1942f188ac12SKent Gibson 		le->label = kstrndup(eventreq.consumer_label,
1943f188ac12SKent Gibson 				     sizeof(eventreq.consumer_label) - 1,
1944925ca369SKent Gibson 				     GFP_KERNEL);
1945925ca369SKent Gibson 		if (!le->label) {
1946925ca369SKent Gibson 			ret = -ENOMEM;
1947925ca369SKent Gibson 			goto out_free_le;
1948925ca369SKent Gibson 		}
1949925ca369SKent Gibson 	}
1950925ca369SKent Gibson 
195195a4eed7SAndy Shevchenko 	ret = gpiod_request_user(desc, le->label);
1952925ca369SKent Gibson 	if (ret)
195346824272SKent Gibson 		goto out_free_le;
1954925ca369SKent Gibson 	le->desc = desc;
1955925ca369SKent Gibson 	le->eflags = eflags;
1956925ca369SKent Gibson 
1957c274b58aSKent Gibson 	linehandle_flags_to_desc_flags(lflags, &desc->flags);
1958925ca369SKent Gibson 
1959925ca369SKent Gibson 	ret = gpiod_direction_input(desc);
1960925ca369SKent Gibson 	if (ret)
196146824272SKent Gibson 		goto out_free_le;
1962925ca369SKent Gibson 
19636accc376SKent Gibson 	blocking_notifier_call_chain(&desc->gdev->notifier,
1964aad95584SKent Gibson 				     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1965925ca369SKent Gibson 
196646824272SKent Gibson 	irq = gpiod_to_irq(desc);
196746824272SKent Gibson 	if (irq <= 0) {
1968925ca369SKent Gibson 		ret = -ENODEV;
196946824272SKent Gibson 		goto out_free_le;
1970925ca369SKent Gibson 	}
197146824272SKent Gibson 	le->irq = irq;
1972925ca369SKent Gibson 
1973925ca369SKent Gibson 	if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
1974925ca369SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1975925ca369SKent Gibson 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1976925ca369SKent Gibson 	if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
1977925ca369SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1978925ca369SKent Gibson 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1979925ca369SKent Gibson 	irqflags |= IRQF_ONESHOT;
1980925ca369SKent Gibson 
1981925ca369SKent Gibson 	INIT_KFIFO(le->events);
1982925ca369SKent Gibson 	init_waitqueue_head(&le->wait);
1983925ca369SKent Gibson 
1984925ca369SKent Gibson 	/* Request a thread to read the events */
1985925ca369SKent Gibson 	ret = request_threaded_irq(le->irq,
1986925ca369SKent Gibson 				   lineevent_irq_handler,
1987925ca369SKent Gibson 				   lineevent_irq_thread,
1988925ca369SKent Gibson 				   irqflags,
1989925ca369SKent Gibson 				   le->label,
1990925ca369SKent Gibson 				   le);
1991925ca369SKent Gibson 	if (ret)
199246824272SKent Gibson 		goto out_free_le;
1993925ca369SKent Gibson 
1994925ca369SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1995925ca369SKent Gibson 	if (fd < 0) {
1996925ca369SKent Gibson 		ret = fd;
199746824272SKent Gibson 		goto out_free_le;
1998925ca369SKent Gibson 	}
1999925ca369SKent Gibson 
2000925ca369SKent Gibson 	file = anon_inode_getfile("gpio-event",
2001925ca369SKent Gibson 				  &lineevent_fileops,
2002925ca369SKent Gibson 				  le,
2003925ca369SKent Gibson 				  O_RDONLY | O_CLOEXEC);
2004925ca369SKent Gibson 	if (IS_ERR(file)) {
2005925ca369SKent Gibson 		ret = PTR_ERR(file);
2006925ca369SKent Gibson 		goto out_put_unused_fd;
2007925ca369SKent Gibson 	}
2008925ca369SKent Gibson 
2009925ca369SKent Gibson 	eventreq.fd = fd;
2010925ca369SKent Gibson 	if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
2011925ca369SKent Gibson 		/*
2012925ca369SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
2013925ca369SKent Gibson 		 * the regular error cleanup path here.
2014925ca369SKent Gibson 		 */
2015925ca369SKent Gibson 		fput(file);
2016925ca369SKent Gibson 		put_unused_fd(fd);
2017925ca369SKent Gibson 		return -EFAULT;
2018925ca369SKent Gibson 	}
2019925ca369SKent Gibson 
2020925ca369SKent Gibson 	fd_install(fd, file);
2021925ca369SKent Gibson 
2022925ca369SKent Gibson 	return 0;
2023925ca369SKent Gibson 
2024925ca369SKent Gibson out_put_unused_fd:
2025925ca369SKent Gibson 	put_unused_fd(fd);
2026925ca369SKent Gibson out_free_le:
202746824272SKent Gibson 	lineevent_free(le);
2028925ca369SKent Gibson 	return ret;
2029925ca369SKent Gibson }
2030925ca369SKent Gibson 
2031aad95584SKent Gibson static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
2032aad95584SKent Gibson 				    struct gpioline_info *info_v1)
2033aad95584SKent Gibson {
2034aad95584SKent Gibson 	u64 flagsv2 = info_v2->flags;
2035aad95584SKent Gibson 
2036aad95584SKent Gibson 	memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
2037aad95584SKent Gibson 	memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
2038aad95584SKent Gibson 	info_v1->line_offset = info_v2->offset;
2039aad95584SKent Gibson 	info_v1->flags = 0;
2040aad95584SKent Gibson 
2041aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
2042aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_KERNEL;
2043aad95584SKent Gibson 
2044aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
2045aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
2046aad95584SKent Gibson 
2047aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
2048aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
2049aad95584SKent Gibson 
2050aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
2051aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
2052aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
2053aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
2054aad95584SKent Gibson 
2055aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
2056aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
2057aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
2058aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
2059aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
2060aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
2061aad95584SKent Gibson }
2062aad95584SKent Gibson 
2063aad95584SKent Gibson static void gpio_v2_line_info_changed_to_v1(
2064aad95584SKent Gibson 		struct gpio_v2_line_info_changed *lic_v2,
2065aad95584SKent Gibson 		struct gpioline_info_changed *lic_v1)
2066aad95584SKent Gibson {
2067cb8f63b8SGabriel Knezek 	memset(lic_v1, 0, sizeof(*lic_v1));
2068aad95584SKent Gibson 	gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
2069aad95584SKent Gibson 	lic_v1->timestamp = lic_v2->timestamp_ns;
2070aad95584SKent Gibson 	lic_v1->event_type = lic_v2->event_type;
2071aad95584SKent Gibson }
2072aad95584SKent Gibson 
20733c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
20743c0d9c63SKent Gibson 
2075925ca369SKent Gibson static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
2076aad95584SKent Gibson 				  struct gpio_v2_line_info *info)
2077925ca369SKent Gibson {
2078925ca369SKent Gibson 	struct gpio_chip *gc = desc->gdev->chip;
2079925ca369SKent Gibson 	bool ok_for_pinctrl;
2080925ca369SKent Gibson 	unsigned long flags;
208165cff704SKent Gibson 	u32 debounce_period_us;
208265cff704SKent Gibson 	unsigned int num_attrs = 0;
2083925ca369SKent Gibson 
208469e4e136SKent Gibson 	memset(info, 0, sizeof(*info));
2085aad95584SKent Gibson 	info->offset = gpio_chip_hwgpio(desc);
2086925ca369SKent Gibson 
2087925ca369SKent Gibson 	/*
2088925ca369SKent Gibson 	 * This function takes a mutex so we must check this before taking
2089925ca369SKent Gibson 	 * the spinlock.
2090925ca369SKent Gibson 	 *
2091925ca369SKent Gibson 	 * FIXME: find a non-racy way to retrieve this information. Maybe a
2092925ca369SKent Gibson 	 * lock common to both frameworks?
2093925ca369SKent Gibson 	 */
2094925ca369SKent Gibson 	ok_for_pinctrl =
2095aad95584SKent Gibson 		pinctrl_gpio_can_use_line(gc->base + info->offset);
2096925ca369SKent Gibson 
2097925ca369SKent Gibson 	spin_lock_irqsave(&gpio_lock, flags);
2098925ca369SKent Gibson 
209969e4e136SKent Gibson 	if (desc->name)
210069e4e136SKent Gibson 		strscpy(info->name, desc->name, sizeof(info->name));
2101925ca369SKent Gibson 
210269e4e136SKent Gibson 	if (desc->label)
210369e4e136SKent Gibson 		strscpy(info->consumer, desc->label, sizeof(info->consumer));
2104925ca369SKent Gibson 
2105925ca369SKent Gibson 	/*
2106925ca369SKent Gibson 	 * Userspace only need to know that the kernel is using this GPIO so
2107925ca369SKent Gibson 	 * it can't use it.
2108925ca369SKent Gibson 	 */
2109925ca369SKent Gibson 	info->flags = 0;
2110925ca369SKent Gibson 	if (test_bit(FLAG_REQUESTED, &desc->flags) ||
2111925ca369SKent Gibson 	    test_bit(FLAG_IS_HOGGED, &desc->flags) ||
2112925ca369SKent Gibson 	    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
2113925ca369SKent Gibson 	    test_bit(FLAG_EXPORT, &desc->flags) ||
2114925ca369SKent Gibson 	    test_bit(FLAG_SYSFS, &desc->flags) ||
2115a0db197fSMarc Zyngier 	    !gpiochip_line_is_valid(gc, info->offset) ||
2116925ca369SKent Gibson 	    !ok_for_pinctrl)
2117aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_USED;
2118aad95584SKent Gibson 
2119925ca369SKent Gibson 	if (test_bit(FLAG_IS_OUT, &desc->flags))
2120aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
2121aad95584SKent Gibson 	else
2122aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_INPUT;
2123aad95584SKent Gibson 
2124925ca369SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2125aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
2126aad95584SKent Gibson 
2127925ca369SKent Gibson 	if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2128aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
2129925ca369SKent Gibson 	if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2130aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
2131aad95584SKent Gibson 
2132925ca369SKent Gibson 	if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2133aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
2134925ca369SKent Gibson 	if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2135aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
2136925ca369SKent Gibson 	if (test_bit(FLAG_PULL_UP, &desc->flags))
2137aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
2138925ca369SKent Gibson 
213973e03419SKent Gibson 	if (test_bit(FLAG_EDGE_RISING, &desc->flags))
214073e03419SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
214173e03419SKent Gibson 	if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
214273e03419SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
214373e03419SKent Gibson 
214426d060e4SKent Gibson 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
214526d060e4SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
2146*2068339aSDipen Patel 	else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags))
2147*2068339aSDipen Patel 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
214826d060e4SKent Gibson 
214965cff704SKent Gibson 	debounce_period_us = READ_ONCE(desc->debounce_period_us);
215065cff704SKent Gibson 	if (debounce_period_us) {
215165cff704SKent Gibson 		info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
215265cff704SKent Gibson 		info->attrs[num_attrs].debounce_period_us = debounce_period_us;
215365cff704SKent Gibson 		num_attrs++;
215465cff704SKent Gibson 	}
215565cff704SKent Gibson 	info->num_attrs = num_attrs;
2156925ca369SKent Gibson 
2157925ca369SKent Gibson 	spin_unlock_irqrestore(&gpio_lock, flags);
2158925ca369SKent Gibson }
2159925ca369SKent Gibson 
2160925ca369SKent Gibson struct gpio_chardev_data {
2161925ca369SKent Gibson 	struct gpio_device *gdev;
2162925ca369SKent Gibson 	wait_queue_head_t wait;
2163aad95584SKent Gibson 	DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
2164925ca369SKent Gibson 	struct notifier_block lineinfo_changed_nb;
2165925ca369SKent Gibson 	unsigned long *watched_lines;
2166aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2167aad95584SKent Gibson 	atomic_t watch_abi_version;
2168aad95584SKent Gibson #endif
2169925ca369SKent Gibson };
2170925ca369SKent Gibson 
21712e202ad8SKent Gibson static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
21722e202ad8SKent Gibson {
21732e202ad8SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
21742e202ad8SKent Gibson 	struct gpiochip_info chipinfo;
21752e202ad8SKent Gibson 
21762e202ad8SKent Gibson 	memset(&chipinfo, 0, sizeof(chipinfo));
21772e202ad8SKent Gibson 
21782e202ad8SKent Gibson 	strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
21792e202ad8SKent Gibson 	strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
21802e202ad8SKent Gibson 	chipinfo.lines = gdev->ngpio;
21812e202ad8SKent Gibson 	if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
21822e202ad8SKent Gibson 		return -EFAULT;
21832e202ad8SKent Gibson 	return 0;
21842e202ad8SKent Gibson }
21852e202ad8SKent Gibson 
2186aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2187aad95584SKent Gibson /*
2188aad95584SKent Gibson  * returns 0 if the versions match, else the previously selected ABI version
2189aad95584SKent Gibson  */
2190aad95584SKent Gibson static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
2191aad95584SKent Gibson 				       unsigned int version)
2192aad95584SKent Gibson {
2193aad95584SKent Gibson 	int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
2194aad95584SKent Gibson 
2195aad95584SKent Gibson 	if (abiv == version)
2196aad95584SKent Gibson 		return 0;
2197aad95584SKent Gibson 
2198aad95584SKent Gibson 	return abiv;
2199aad95584SKent Gibson }
22002e202ad8SKent Gibson 
22012e202ad8SKent Gibson static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
22022e202ad8SKent Gibson 			   bool watch)
22032e202ad8SKent Gibson {
22042e202ad8SKent Gibson 	struct gpio_desc *desc;
22052e202ad8SKent Gibson 	struct gpioline_info lineinfo;
22062e202ad8SKent Gibson 	struct gpio_v2_line_info lineinfo_v2;
22072e202ad8SKent Gibson 
22082e202ad8SKent Gibson 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
22092e202ad8SKent Gibson 		return -EFAULT;
22102e202ad8SKent Gibson 
22112e202ad8SKent Gibson 	/* this doubles as a range check on line_offset */
22122e202ad8SKent Gibson 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset);
22132e202ad8SKent Gibson 	if (IS_ERR(desc))
22142e202ad8SKent Gibson 		return PTR_ERR(desc);
22152e202ad8SKent Gibson 
22162e202ad8SKent Gibson 	if (watch) {
22172e202ad8SKent Gibson 		if (lineinfo_ensure_abi_version(cdev, 1))
22182e202ad8SKent Gibson 			return -EPERM;
22192e202ad8SKent Gibson 
22202e202ad8SKent Gibson 		if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
22212e202ad8SKent Gibson 			return -EBUSY;
22222e202ad8SKent Gibson 	}
22232e202ad8SKent Gibson 
22242e202ad8SKent Gibson 	gpio_desc_to_lineinfo(desc, &lineinfo_v2);
22252e202ad8SKent Gibson 	gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
22262e202ad8SKent Gibson 
22272e202ad8SKent Gibson 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
22282e202ad8SKent Gibson 		if (watch)
22292e202ad8SKent Gibson 			clear_bit(lineinfo.line_offset, cdev->watched_lines);
22302e202ad8SKent Gibson 		return -EFAULT;
22312e202ad8SKent Gibson 	}
22322e202ad8SKent Gibson 
22332e202ad8SKent Gibson 	return 0;
22342e202ad8SKent Gibson }
2235aad95584SKent Gibson #endif
2236aad95584SKent Gibson 
2237aad95584SKent Gibson static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
2238aad95584SKent Gibson 			bool watch)
2239aad95584SKent Gibson {
2240aad95584SKent Gibson 	struct gpio_desc *desc;
2241aad95584SKent Gibson 	struct gpio_v2_line_info lineinfo;
2242aad95584SKent Gibson 
2243aad95584SKent Gibson 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2244aad95584SKent Gibson 		return -EFAULT;
2245aad95584SKent Gibson 
2246aad95584SKent Gibson 	if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2247aad95584SKent Gibson 		return -EINVAL;
2248aad95584SKent Gibson 
2249aad95584SKent Gibson 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2250aad95584SKent Gibson 	if (IS_ERR(desc))
2251aad95584SKent Gibson 		return PTR_ERR(desc);
2252aad95584SKent Gibson 
2253aad95584SKent Gibson 	if (watch) {
2254aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2255aad95584SKent Gibson 		if (lineinfo_ensure_abi_version(cdev, 2))
2256aad95584SKent Gibson 			return -EPERM;
2257aad95584SKent Gibson #endif
2258aad95584SKent Gibson 		if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2259aad95584SKent Gibson 			return -EBUSY;
2260aad95584SKent Gibson 	}
2261aad95584SKent Gibson 	gpio_desc_to_lineinfo(desc, &lineinfo);
2262aad95584SKent Gibson 
2263aad95584SKent Gibson 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2264aad95584SKent Gibson 		if (watch)
2265aad95584SKent Gibson 			clear_bit(lineinfo.offset, cdev->watched_lines);
2266aad95584SKent Gibson 		return -EFAULT;
2267aad95584SKent Gibson 	}
2268aad95584SKent Gibson 
2269aad95584SKent Gibson 	return 0;
2270aad95584SKent Gibson }
2271aad95584SKent Gibson 
22722e202ad8SKent Gibson static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
2273925ca369SKent Gibson {
2274925ca369SKent Gibson 	__u32 offset;
2275925ca369SKent Gibson 
2276925ca369SKent Gibson 	if (copy_from_user(&offset, ip, sizeof(offset)))
2277925ca369SKent Gibson 		return -EFAULT;
2278925ca369SKent Gibson 
22791bf7ba40SKent Gibson 	if (offset >= cdev->gdev->ngpio)
22801bf7ba40SKent Gibson 		return -EINVAL;
2281925ca369SKent Gibson 
22821bf7ba40SKent Gibson 	if (!test_and_clear_bit(offset, cdev->watched_lines))
2283925ca369SKent Gibson 		return -EBUSY;
2284925ca369SKent Gibson 
2285925ca369SKent Gibson 	return 0;
2286925ca369SKent Gibson }
22872e202ad8SKent Gibson 
22882e202ad8SKent Gibson /*
22892e202ad8SKent Gibson  * gpio_ioctl() - ioctl handler for the GPIO chardev
22902e202ad8SKent Gibson  */
22912e202ad8SKent Gibson static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
22922e202ad8SKent Gibson {
22932e202ad8SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
22942e202ad8SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
22952e202ad8SKent Gibson 	void __user *ip = (void __user *)arg;
22962e202ad8SKent Gibson 
22972e202ad8SKent Gibson 	/* We fail any subsequent ioctl():s when the chip is gone */
22982e202ad8SKent Gibson 	if (!gdev->chip)
22992e202ad8SKent Gibson 		return -ENODEV;
23002e202ad8SKent Gibson 
23012e202ad8SKent Gibson 	/* Fill in the struct and pass to userspace */
23022e202ad8SKent Gibson 	if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
23032e202ad8SKent Gibson 		return chipinfo_get(cdev, ip);
23042e202ad8SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
23052e202ad8SKent Gibson 	} else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
23062e202ad8SKent Gibson 		return linehandle_create(gdev, ip);
23072e202ad8SKent Gibson 	} else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
23082e202ad8SKent Gibson 		return lineevent_create(gdev, ip);
23092e202ad8SKent Gibson 	} else if (cmd == GPIO_GET_LINEINFO_IOCTL ||
23102e202ad8SKent Gibson 		   cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
23112e202ad8SKent Gibson 		return lineinfo_get_v1(cdev, ip,
23122e202ad8SKent Gibson 				       cmd == GPIO_GET_LINEINFO_WATCH_IOCTL);
23132e202ad8SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
23142e202ad8SKent Gibson 	} else if (cmd == GPIO_V2_GET_LINEINFO_IOCTL ||
23152e202ad8SKent Gibson 		   cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL) {
23162e202ad8SKent Gibson 		return lineinfo_get(cdev, ip,
23172e202ad8SKent Gibson 				    cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL);
23182e202ad8SKent Gibson 	} else if (cmd == GPIO_V2_GET_LINE_IOCTL) {
23192e202ad8SKent Gibson 		return linereq_create(gdev, ip);
23202e202ad8SKent Gibson 	} else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) {
23212e202ad8SKent Gibson 		return lineinfo_unwatch(cdev, ip);
23222e202ad8SKent Gibson 	}
2323925ca369SKent Gibson 	return -EINVAL;
2324925ca369SKent Gibson }
2325925ca369SKent Gibson 
2326925ca369SKent Gibson #ifdef CONFIG_COMPAT
232749bc5279SKent Gibson static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2328925ca369SKent Gibson 			      unsigned long arg)
2329925ca369SKent Gibson {
233049bc5279SKent Gibson 	return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2331925ca369SKent Gibson }
2332925ca369SKent Gibson #endif
2333925ca369SKent Gibson 
2334925ca369SKent Gibson static struct gpio_chardev_data *
2335925ca369SKent Gibson to_gpio_chardev_data(struct notifier_block *nb)
2336925ca369SKent Gibson {
2337925ca369SKent Gibson 	return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2338925ca369SKent Gibson }
2339925ca369SKent Gibson 
2340925ca369SKent Gibson static int lineinfo_changed_notify(struct notifier_block *nb,
2341925ca369SKent Gibson 				   unsigned long action, void *data)
2342925ca369SKent Gibson {
2343e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
2344aad95584SKent Gibson 	struct gpio_v2_line_info_changed chg;
2345925ca369SKent Gibson 	struct gpio_desc *desc = data;
2346925ca369SKent Gibson 	int ret;
2347925ca369SKent Gibson 
2348e2b781c5SKent Gibson 	if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2349925ca369SKent Gibson 		return NOTIFY_DONE;
2350925ca369SKent Gibson 
2351925ca369SKent Gibson 	memset(&chg, 0, sizeof(chg));
2352925ca369SKent Gibson 	chg.event_type = action;
2353aad95584SKent Gibson 	chg.timestamp_ns = ktime_get_ns();
2354925ca369SKent Gibson 	gpio_desc_to_lineinfo(desc, &chg.info);
2355925ca369SKent Gibson 
2356e2b781c5SKent Gibson 	ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2357925ca369SKent Gibson 	if (ret)
2358e2b781c5SKent Gibson 		wake_up_poll(&cdev->wait, EPOLLIN);
2359925ca369SKent Gibson 	else
2360925ca369SKent Gibson 		pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2361925ca369SKent Gibson 
2362925ca369SKent Gibson 	return NOTIFY_OK;
2363925ca369SKent Gibson }
2364925ca369SKent Gibson 
236549bc5279SKent Gibson static __poll_t lineinfo_watch_poll(struct file *file,
2366925ca369SKent Gibson 				    struct poll_table_struct *pollt)
2367925ca369SKent Gibson {
2368e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2369925ca369SKent Gibson 	__poll_t events = 0;
2370925ca369SKent Gibson 
2371e2b781c5SKent Gibson 	poll_wait(file, &cdev->wait, pollt);
2372925ca369SKent Gibson 
2373e2b781c5SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2374e2b781c5SKent Gibson 						 &cdev->wait.lock))
2375925ca369SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
2376925ca369SKent Gibson 
2377925ca369SKent Gibson 	return events;
2378925ca369SKent Gibson }
2379925ca369SKent Gibson 
238049bc5279SKent Gibson static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2381925ca369SKent Gibson 				   size_t count, loff_t *off)
2382925ca369SKent Gibson {
2383e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2384aad95584SKent Gibson 	struct gpio_v2_line_info_changed event;
2385925ca369SKent Gibson 	ssize_t bytes_read = 0;
2386925ca369SKent Gibson 	int ret;
2387aad95584SKent Gibson 	size_t event_size;
2388925ca369SKent Gibson 
2389aad95584SKent Gibson #ifndef CONFIG_GPIO_CDEV_V1
2390aad95584SKent Gibson 	event_size = sizeof(struct gpio_v2_line_info_changed);
2391aad95584SKent Gibson 	if (count < event_size)
2392925ca369SKent Gibson 		return -EINVAL;
2393aad95584SKent Gibson #endif
2394925ca369SKent Gibson 
2395925ca369SKent Gibson 	do {
2396e2b781c5SKent Gibson 		spin_lock(&cdev->wait.lock);
2397e2b781c5SKent Gibson 		if (kfifo_is_empty(&cdev->events)) {
2398925ca369SKent Gibson 			if (bytes_read) {
2399e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2400925ca369SKent Gibson 				return bytes_read;
2401925ca369SKent Gibson 			}
2402925ca369SKent Gibson 
240349bc5279SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
2404e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2405925ca369SKent Gibson 				return -EAGAIN;
2406925ca369SKent Gibson 			}
2407925ca369SKent Gibson 
2408e2b781c5SKent Gibson 			ret = wait_event_interruptible_locked(cdev->wait,
2409e2b781c5SKent Gibson 					!kfifo_is_empty(&cdev->events));
2410925ca369SKent Gibson 			if (ret) {
2411e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2412925ca369SKent Gibson 				return ret;
2413925ca369SKent Gibson 			}
2414925ca369SKent Gibson 		}
2415aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2416aad95584SKent Gibson 		/* must be after kfifo check so watch_abi_version is set */
2417aad95584SKent Gibson 		if (atomic_read(&cdev->watch_abi_version) == 2)
2418aad95584SKent Gibson 			event_size = sizeof(struct gpio_v2_line_info_changed);
2419aad95584SKent Gibson 		else
2420aad95584SKent Gibson 			event_size = sizeof(struct gpioline_info_changed);
2421aad95584SKent Gibson 		if (count < event_size) {
2422aad95584SKent Gibson 			spin_unlock(&cdev->wait.lock);
2423aad95584SKent Gibson 			return -EINVAL;
2424aad95584SKent Gibson 		}
2425aad95584SKent Gibson #endif
2426e2b781c5SKent Gibson 		ret = kfifo_out(&cdev->events, &event, 1);
2427e2b781c5SKent Gibson 		spin_unlock(&cdev->wait.lock);
2428925ca369SKent Gibson 		if (ret != 1) {
2429925ca369SKent Gibson 			ret = -EIO;
2430925ca369SKent Gibson 			break;
2431925ca369SKent Gibson 			/* We should never get here. See lineevent_read(). */
2432925ca369SKent Gibson 		}
2433925ca369SKent Gibson 
2434aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2435aad95584SKent Gibson 		if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2436aad95584SKent Gibson 			if (copy_to_user(buf + bytes_read, &event, event_size))
2437925ca369SKent Gibson 				return -EFAULT;
2438aad95584SKent Gibson 		} else {
2439aad95584SKent Gibson 			struct gpioline_info_changed event_v1;
2440aad95584SKent Gibson 
2441aad95584SKent Gibson 			gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2442aad95584SKent Gibson 			if (copy_to_user(buf + bytes_read, &event_v1,
2443aad95584SKent Gibson 					 event_size))
2444aad95584SKent Gibson 				return -EFAULT;
2445aad95584SKent Gibson 		}
2446aad95584SKent Gibson #else
2447aad95584SKent Gibson 		if (copy_to_user(buf + bytes_read, &event, event_size))
2448aad95584SKent Gibson 			return -EFAULT;
2449aad95584SKent Gibson #endif
2450aad95584SKent Gibson 		bytes_read += event_size;
2451925ca369SKent Gibson 	} while (count >= bytes_read + sizeof(event));
2452925ca369SKent Gibson 
2453925ca369SKent Gibson 	return bytes_read;
2454925ca369SKent Gibson }
2455925ca369SKent Gibson 
2456925ca369SKent Gibson /**
2457925ca369SKent Gibson  * gpio_chrdev_open() - open the chardev for ioctl operations
2458925ca369SKent Gibson  * @inode: inode for this chardev
245949bc5279SKent Gibson  * @file: file struct for storing private data
2460925ca369SKent Gibson  * Returns 0 on success
2461925ca369SKent Gibson  */
246249bc5279SKent Gibson static int gpio_chrdev_open(struct inode *inode, struct file *file)
2463925ca369SKent Gibson {
2464925ca369SKent Gibson 	struct gpio_device *gdev = container_of(inode->i_cdev,
2465925ca369SKent Gibson 						struct gpio_device, chrdev);
2466e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev;
2467925ca369SKent Gibson 	int ret = -ENOMEM;
2468925ca369SKent Gibson 
2469925ca369SKent Gibson 	/* Fail on open if the backing gpiochip is gone */
2470925ca369SKent Gibson 	if (!gdev->chip)
2471925ca369SKent Gibson 		return -ENODEV;
2472925ca369SKent Gibson 
2473e2b781c5SKent Gibson 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2474e2b781c5SKent Gibson 	if (!cdev)
2475925ca369SKent Gibson 		return -ENOMEM;
2476925ca369SKent Gibson 
2477e2b781c5SKent Gibson 	cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2478e2b781c5SKent Gibson 	if (!cdev->watched_lines)
2479e2b781c5SKent Gibson 		goto out_free_cdev;
2480925ca369SKent Gibson 
2481e2b781c5SKent Gibson 	init_waitqueue_head(&cdev->wait);
2482e2b781c5SKent Gibson 	INIT_KFIFO(cdev->events);
2483e2b781c5SKent Gibson 	cdev->gdev = gdev;
2484925ca369SKent Gibson 
2485e2b781c5SKent Gibson 	cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
24866accc376SKent Gibson 	ret = blocking_notifier_chain_register(&gdev->notifier,
2487e2b781c5SKent Gibson 					       &cdev->lineinfo_changed_nb);
2488925ca369SKent Gibson 	if (ret)
2489925ca369SKent Gibson 		goto out_free_bitmap;
2490925ca369SKent Gibson 
2491925ca369SKent Gibson 	get_device(&gdev->dev);
2492e2b781c5SKent Gibson 	file->private_data = cdev;
2493925ca369SKent Gibson 
249449bc5279SKent Gibson 	ret = nonseekable_open(inode, file);
2495925ca369SKent Gibson 	if (ret)
2496925ca369SKent Gibson 		goto out_unregister_notifier;
2497925ca369SKent Gibson 
2498925ca369SKent Gibson 	return ret;
2499925ca369SKent Gibson 
2500925ca369SKent Gibson out_unregister_notifier:
25016accc376SKent Gibson 	blocking_notifier_chain_unregister(&gdev->notifier,
2502e2b781c5SKent Gibson 					   &cdev->lineinfo_changed_nb);
2503925ca369SKent Gibson out_free_bitmap:
2504e2b781c5SKent Gibson 	bitmap_free(cdev->watched_lines);
2505e2b781c5SKent Gibson out_free_cdev:
2506e2b781c5SKent Gibson 	kfree(cdev);
2507925ca369SKent Gibson 	return ret;
2508925ca369SKent Gibson }
2509925ca369SKent Gibson 
2510925ca369SKent Gibson /**
2511925ca369SKent Gibson  * gpio_chrdev_release() - close chardev after ioctl operations
2512925ca369SKent Gibson  * @inode: inode for this chardev
251349bc5279SKent Gibson  * @file: file struct for storing private data
2514925ca369SKent Gibson  * Returns 0 on success
2515925ca369SKent Gibson  */
251649bc5279SKent Gibson static int gpio_chrdev_release(struct inode *inode, struct file *file)
2517925ca369SKent Gibson {
2518e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2519e2b781c5SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
2520925ca369SKent Gibson 
2521e2b781c5SKent Gibson 	bitmap_free(cdev->watched_lines);
25226accc376SKent Gibson 	blocking_notifier_chain_unregister(&gdev->notifier,
2523e2b781c5SKent Gibson 					   &cdev->lineinfo_changed_nb);
2524925ca369SKent Gibson 	put_device(&gdev->dev);
2525e2b781c5SKent Gibson 	kfree(cdev);
2526925ca369SKent Gibson 
2527925ca369SKent Gibson 	return 0;
2528925ca369SKent Gibson }
2529925ca369SKent Gibson 
2530925ca369SKent Gibson static const struct file_operations gpio_fileops = {
2531925ca369SKent Gibson 	.release = gpio_chrdev_release,
2532925ca369SKent Gibson 	.open = gpio_chrdev_open,
2533925ca369SKent Gibson 	.poll = lineinfo_watch_poll,
2534925ca369SKent Gibson 	.read = lineinfo_watch_read,
2535925ca369SKent Gibson 	.owner = THIS_MODULE,
2536925ca369SKent Gibson 	.llseek = no_llseek,
2537925ca369SKent Gibson 	.unlocked_ioctl = gpio_ioctl,
2538925ca369SKent Gibson #ifdef CONFIG_COMPAT
2539925ca369SKent Gibson 	.compat_ioctl = gpio_ioctl_compat,
2540925ca369SKent Gibson #endif
2541925ca369SKent Gibson };
2542925ca369SKent Gibson 
2543925ca369SKent Gibson int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2544925ca369SKent Gibson {
2545925ca369SKent Gibson 	int ret;
2546925ca369SKent Gibson 
2547925ca369SKent Gibson 	cdev_init(&gdev->chrdev, &gpio_fileops);
2548925ca369SKent Gibson 	gdev->chrdev.owner = THIS_MODULE;
2549925ca369SKent Gibson 	gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2550925ca369SKent Gibson 
2551925ca369SKent Gibson 	ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2552925ca369SKent Gibson 	if (ret)
2553925ca369SKent Gibson 		return ret;
2554925ca369SKent Gibson 
2555925ca369SKent Gibson 	chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2556925ca369SKent Gibson 		 MAJOR(devt), gdev->id);
2557925ca369SKent Gibson 
2558925ca369SKent Gibson 	return 0;
2559925ca369SKent Gibson }
2560925ca369SKent Gibson 
2561925ca369SKent Gibson void gpiolib_cdev_unregister(struct gpio_device *gdev)
2562925ca369SKent Gibson {
2563925ca369SKent Gibson 	cdev_device_del(&gdev->chrdev, &gdev->dev);
2564925ca369SKent Gibson }
2565