xref: /openbmc/linux/drivers/gpio/gpiolib-cdev.c (revision 85ff37e302efdf173cff6d1a310c2f7f38f1d069)
1925ca369SKent Gibson // SPDX-License-Identifier: GPL-2.0
2925ca369SKent Gibson 
3d189f627SKent Gibson #include <linux/anon_inodes.h>
43c0d9c63SKent Gibson #include <linux/atomic.h>
5925ca369SKent Gibson #include <linux/bitmap.h>
63c0d9c63SKent Gibson #include <linux/build_bug.h>
7d189f627SKent Gibson #include <linux/cdev.h>
8d189f627SKent Gibson #include <linux/compat.h>
965cff704SKent Gibson #include <linux/compiler.h>
10925ca369SKent Gibson #include <linux/device.h>
11925ca369SKent Gibson #include <linux/err.h>
12d189f627SKent Gibson #include <linux/file.h>
13925ca369SKent Gibson #include <linux/gpio.h>
14925ca369SKent Gibson #include <linux/gpio/driver.h>
15d189f627SKent Gibson #include <linux/interrupt.h>
16d189f627SKent Gibson #include <linux/irqreturn.h>
17d189f627SKent Gibson #include <linux/kernel.h>
18925ca369SKent Gibson #include <linux/kfifo.h>
19d189f627SKent Gibson #include <linux/module.h>
20a54756cbSKent Gibson #include <linux/mutex.h>
21d189f627SKent Gibson #include <linux/pinctrl/consumer.h>
22925ca369SKent Gibson #include <linux/poll.h>
23d189f627SKent Gibson #include <linux/spinlock.h>
24925ca369SKent Gibson #include <linux/timekeeping.h>
25d189f627SKent Gibson #include <linux/uaccess.h>
2665cff704SKent Gibson #include <linux/workqueue.h>
272068339aSDipen Patel #include <linux/hte.h>
28925ca369SKent Gibson #include <uapi/linux/gpio.h>
29925ca369SKent Gibson 
30925ca369SKent Gibson #include "gpiolib.h"
31925ca369SKent Gibson #include "gpiolib-cdev.h"
32925ca369SKent Gibson 
333c0d9c63SKent Gibson /*
343c0d9c63SKent Gibson  * Array sizes must ensure 64-bit alignment and not create holes in the
353c0d9c63SKent Gibson  * struct packing.
363c0d9c63SKent Gibson  */
373c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2));
383c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8));
393c0d9c63SKent Gibson 
403c0d9c63SKent Gibson /*
413c0d9c63SKent Gibson  * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility
423c0d9c63SKent Gibson  */
433c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8));
443c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8));
453c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8));
463c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8));
473c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8));
483c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8));
493c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8));
503c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8));
513c0d9c63SKent Gibson 
52925ca369SKent Gibson /* Character device interface to GPIO.
53925ca369SKent Gibson  *
54925ca369SKent Gibson  * The GPIO character device, /dev/gpiochipN, provides userspace an
55925ca369SKent Gibson  * interface to gpiolib GPIOs via ioctl()s.
56925ca369SKent Gibson  */
57925ca369SKent Gibson 
58925ca369SKent Gibson /*
59925ca369SKent Gibson  * GPIO line handle management
60925ca369SKent Gibson  */
61925ca369SKent Gibson 
623c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
63925ca369SKent Gibson /**
64925ca369SKent Gibson  * struct linehandle_state - contains the state of a userspace handle
65925ca369SKent Gibson  * @gdev: the GPIO device the handle pertains to
66925ca369SKent Gibson  * @label: consumer label used to tag descriptors
67925ca369SKent Gibson  * @descs: the GPIO descriptors held by this handle
6852b7b596SKent Gibson  * @num_descs: the number of descriptors held in the descs array
69925ca369SKent Gibson  */
70925ca369SKent Gibson struct linehandle_state {
71925ca369SKent Gibson 	struct gpio_device *gdev;
72925ca369SKent Gibson 	const char *label;
73925ca369SKent Gibson 	struct gpio_desc *descs[GPIOHANDLES_MAX];
7452b7b596SKent Gibson 	u32 num_descs;
75925ca369SKent Gibson };
76925ca369SKent Gibson 
77925ca369SKent Gibson #define GPIOHANDLE_REQUEST_VALID_FLAGS \
78925ca369SKent Gibson 	(GPIOHANDLE_REQUEST_INPUT | \
79925ca369SKent Gibson 	GPIOHANDLE_REQUEST_OUTPUT | \
80925ca369SKent Gibson 	GPIOHANDLE_REQUEST_ACTIVE_LOW | \
81925ca369SKent Gibson 	GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
82925ca369SKent Gibson 	GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
83925ca369SKent Gibson 	GPIOHANDLE_REQUEST_BIAS_DISABLE | \
84925ca369SKent Gibson 	GPIOHANDLE_REQUEST_OPEN_DRAIN | \
85925ca369SKent Gibson 	GPIOHANDLE_REQUEST_OPEN_SOURCE)
86925ca369SKent Gibson 
87925ca369SKent Gibson static int linehandle_validate_flags(u32 flags)
88925ca369SKent Gibson {
89925ca369SKent Gibson 	/* Return an error if an unknown flag is set */
90925ca369SKent Gibson 	if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
91925ca369SKent Gibson 		return -EINVAL;
92925ca369SKent Gibson 
93925ca369SKent Gibson 	/*
94925ca369SKent Gibson 	 * Do not allow both INPUT & OUTPUT flags to be set as they are
95925ca369SKent Gibson 	 * contradictory.
96925ca369SKent Gibson 	 */
97925ca369SKent Gibson 	if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
98925ca369SKent Gibson 	    (flags & GPIOHANDLE_REQUEST_OUTPUT))
99925ca369SKent Gibson 		return -EINVAL;
100925ca369SKent Gibson 
101925ca369SKent Gibson 	/*
102925ca369SKent Gibson 	 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
103925ca369SKent Gibson 	 * the hardware actually supports enabling both at the same time the
104925ca369SKent Gibson 	 * electrical result would be disastrous.
105925ca369SKent Gibson 	 */
106925ca369SKent Gibson 	if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
107925ca369SKent Gibson 	    (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
108925ca369SKent Gibson 		return -EINVAL;
109925ca369SKent Gibson 
110925ca369SKent Gibson 	/* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
111925ca369SKent Gibson 	if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
112925ca369SKent Gibson 	    ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
113925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
114925ca369SKent Gibson 		return -EINVAL;
115925ca369SKent Gibson 
116925ca369SKent Gibson 	/* Bias flags only allowed for input or output mode. */
117925ca369SKent Gibson 	if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
118925ca369SKent Gibson 	      (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
119925ca369SKent Gibson 	    ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
120925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
121925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
122925ca369SKent Gibson 		return -EINVAL;
123925ca369SKent Gibson 
124925ca369SKent Gibson 	/* Only one bias flag can be set. */
125925ca369SKent Gibson 	if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
126925ca369SKent Gibson 	     (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
127925ca369SKent Gibson 		       GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
128925ca369SKent Gibson 	    ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
129925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
130925ca369SKent Gibson 		return -EINVAL;
131925ca369SKent Gibson 
132925ca369SKent Gibson 	return 0;
133925ca369SKent Gibson }
134925ca369SKent Gibson 
135c274b58aSKent Gibson static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
136c274b58aSKent Gibson {
137c274b58aSKent Gibson 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
138c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
139c274b58aSKent Gibson 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
140c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
141c274b58aSKent Gibson 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
142c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
143c274b58aSKent Gibson 	assign_bit(FLAG_PULL_UP, flagsp,
144c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
145c274b58aSKent Gibson 	assign_bit(FLAG_PULL_DOWN, flagsp,
146c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
147c274b58aSKent Gibson 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
148c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
149c274b58aSKent Gibson }
150c274b58aSKent Gibson 
151925ca369SKent Gibson static long linehandle_set_config(struct linehandle_state *lh,
152925ca369SKent Gibson 				  void __user *ip)
153925ca369SKent Gibson {
154925ca369SKent Gibson 	struct gpiohandle_config gcnf;
155925ca369SKent Gibson 	struct gpio_desc *desc;
156925ca369SKent Gibson 	int i, ret;
157925ca369SKent Gibson 	u32 lflags;
158925ca369SKent Gibson 
159925ca369SKent Gibson 	if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
160925ca369SKent Gibson 		return -EFAULT;
161925ca369SKent Gibson 
162925ca369SKent Gibson 	lflags = gcnf.flags;
163925ca369SKent Gibson 	ret = linehandle_validate_flags(lflags);
164925ca369SKent Gibson 	if (ret)
165925ca369SKent Gibson 		return ret;
166925ca369SKent Gibson 
16752b7b596SKent Gibson 	for (i = 0; i < lh->num_descs; i++) {
168925ca369SKent Gibson 		desc = lh->descs[i];
169c274b58aSKent Gibson 		linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags);
170925ca369SKent Gibson 
171925ca369SKent Gibson 		/*
172925ca369SKent Gibson 		 * Lines have to be requested explicitly for input
173925ca369SKent Gibson 		 * or output, else the line will be treated "as is".
174925ca369SKent Gibson 		 */
175925ca369SKent Gibson 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
176925ca369SKent Gibson 			int val = !!gcnf.default_values[i];
177925ca369SKent Gibson 
178925ca369SKent Gibson 			ret = gpiod_direction_output(desc, val);
179925ca369SKent Gibson 			if (ret)
180925ca369SKent Gibson 				return ret;
181925ca369SKent Gibson 		} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
182925ca369SKent Gibson 			ret = gpiod_direction_input(desc);
183925ca369SKent Gibson 			if (ret)
184925ca369SKent Gibson 				return ret;
185925ca369SKent Gibson 		}
186925ca369SKent Gibson 
1876accc376SKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
188aad95584SKent Gibson 					     GPIO_V2_LINE_CHANGED_CONFIG,
189aad95584SKent Gibson 					     desc);
190925ca369SKent Gibson 	}
191925ca369SKent Gibson 	return 0;
192925ca369SKent Gibson }
193925ca369SKent Gibson 
19449bc5279SKent Gibson static long linehandle_ioctl(struct file *file, unsigned int cmd,
195925ca369SKent Gibson 			     unsigned long arg)
196925ca369SKent Gibson {
19749bc5279SKent Gibson 	struct linehandle_state *lh = file->private_data;
198925ca369SKent Gibson 	void __user *ip = (void __user *)arg;
199925ca369SKent Gibson 	struct gpiohandle_data ghd;
200925ca369SKent Gibson 	DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
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
424*85ff37e3SAndy Shevchenko  * @hdesc: the Hardware Timestamp Engine (HTE) descriptor
425*85ff37e3SAndy Shevchenko  * @raw_level: the line level at the time of event
426*85ff37e3SAndy Shevchenko  * @total_discard_seq: the running counter of the discarded events
427*85ff37e3SAndy Shevchenko  * @last_seqno: the last sequence number before debounce period expires
4283c0d9c63SKent Gibson  */
4293c0d9c63SKent Gibson struct line {
4303c0d9c63SKent Gibson 	struct gpio_desc *desc;
43173e03419SKent Gibson 	/*
43273e03419SKent Gibson 	 * -- edge detector specific fields --
43373e03419SKent Gibson 	 */
43473e03419SKent Gibson 	struct linereq *req;
43573e03419SKent Gibson 	unsigned int irq;
4363ffb7c45SKent Gibson 	/*
4373ffb7c45SKent Gibson 	 * eflags is set by edge_detector_setup(), edge_detector_stop() and
4383ffb7c45SKent Gibson 	 * edge_detector_update(), which are themselves mutually exclusive,
4393ffb7c45SKent Gibson 	 * and is accessed by edge_irq_thread() and debounce_work_func(),
4403ffb7c45SKent Gibson 	 * which can both live with a slightly stale value.
4413ffb7c45SKent Gibson 	 */
44273e03419SKent Gibson 	u64 eflags;
44373e03419SKent Gibson 	/*
44473e03419SKent Gibson 	 * timestamp_ns and req_seqno are accessed only by
44573e03419SKent Gibson 	 * edge_irq_handler() and edge_irq_thread(), which are themselves
44673e03419SKent Gibson 	 * mutually exclusive, so no additional protection is necessary.
44773e03419SKent Gibson 	 */
44873e03419SKent Gibson 	u64 timestamp_ns;
44973e03419SKent Gibson 	u32 req_seqno;
45065cff704SKent Gibson 	/*
45165cff704SKent Gibson 	 * line_seqno is accessed by either edge_irq_thread() or
45265cff704SKent Gibson 	 * debounce_work_func(), which are themselves mutually exclusive,
45365cff704SKent Gibson 	 * so no additional protection is necessary.
45465cff704SKent Gibson 	 */
45573e03419SKent Gibson 	u32 line_seqno;
45665cff704SKent Gibson 	/*
45765cff704SKent Gibson 	 * -- debouncer specific fields --
45865cff704SKent Gibson 	 */
45965cff704SKent Gibson 	struct delayed_work work;
46065cff704SKent Gibson 	/*
46165cff704SKent Gibson 	 * sw_debounce is accessed by linereq_set_config(), which is the
46265cff704SKent Gibson 	 * only setter, and linereq_get_values(), which can live with a
46365cff704SKent Gibson 	 * slightly stale value.
46465cff704SKent Gibson 	 */
46565cff704SKent Gibson 	unsigned int sw_debounced;
46665cff704SKent Gibson 	/*
46765cff704SKent Gibson 	 * level is accessed by debounce_work_func(), which is the only
46865cff704SKent Gibson 	 * setter, and linereq_get_values() which can live with a slightly
46965cff704SKent Gibson 	 * stale value.
47065cff704SKent Gibson 	 */
47165cff704SKent Gibson 	unsigned int level;
4722068339aSDipen Patel 	/*
4732068339aSDipen Patel 	 * -- hte specific fields --
4742068339aSDipen Patel 	 */
4752068339aSDipen Patel 	struct hte_ts_desc hdesc;
4762068339aSDipen Patel 	/*
4772068339aSDipen Patel 	 * HTE provider sets line level at the time of event. The valid
4782068339aSDipen Patel 	 * value is 0 or 1 and negative value for an error.
4792068339aSDipen Patel 	 */
4802068339aSDipen Patel 	int raw_level;
4812068339aSDipen Patel 	/*
4822068339aSDipen Patel 	 * when sw_debounce is set on HTE enabled line, this is running
4832068339aSDipen Patel 	 * counter of the discarded events.
4842068339aSDipen Patel 	 */
4852068339aSDipen Patel 	u32 total_discard_seq;
4862068339aSDipen Patel 	/*
4872068339aSDipen Patel 	 * when sw_debounce is set on HTE enabled line, this variable records
4882068339aSDipen Patel 	 * last sequence number before debounce period expires.
4892068339aSDipen Patel 	 */
4902068339aSDipen Patel 	u32 last_seqno;
4913c0d9c63SKent Gibson };
4923c0d9c63SKent Gibson 
4933c0d9c63SKent Gibson /**
4943c0d9c63SKent Gibson  * struct linereq - contains the state of a userspace line request
4953c0d9c63SKent Gibson  * @gdev: the GPIO device the line request pertains to
4963c0d9c63SKent Gibson  * @label: consumer label used to tag GPIO descriptors
4973c0d9c63SKent Gibson  * @num_lines: the number of lines in the lines array
49873e03419SKent Gibson  * @wait: wait queue that handles blocking reads of events
49973e03419SKent Gibson  * @event_buffer_size: the number of elements allocated in @events
50073e03419SKent Gibson  * @events: KFIFO for the GPIO events
50173e03419SKent Gibson  * @seqno: the sequence number for edge events generated on all lines in
50273e03419SKent Gibson  * this line request.  Note that this is not used when @num_lines is 1, as
50373e03419SKent Gibson  * the line_seqno is then the same and is cheaper to calculate.
504a54756cbSKent Gibson  * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
505a54756cbSKent Gibson  * of configuration, particularly multi-step accesses to desc flags.
5063c0d9c63SKent Gibson  * @lines: the lines held by this line request, with @num_lines elements.
5073c0d9c63SKent Gibson  */
5083c0d9c63SKent Gibson struct linereq {
5093c0d9c63SKent Gibson 	struct gpio_device *gdev;
5103c0d9c63SKent Gibson 	const char *label;
5113c0d9c63SKent Gibson 	u32 num_lines;
51273e03419SKent Gibson 	wait_queue_head_t wait;
51373e03419SKent Gibson 	u32 event_buffer_size;
51473e03419SKent Gibson 	DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
51573e03419SKent Gibson 	atomic_t seqno;
516a54756cbSKent Gibson 	struct mutex config_mutex;
5173c0d9c63SKent Gibson 	struct line lines[];
5183c0d9c63SKent Gibson };
5193c0d9c63SKent Gibson 
5203c0d9c63SKent Gibson #define GPIO_V2_LINE_BIAS_FLAGS \
5213c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
5223c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
5233c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_BIAS_DISABLED)
5243c0d9c63SKent Gibson 
5253c0d9c63SKent Gibson #define GPIO_V2_LINE_DIRECTION_FLAGS \
5263c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_INPUT | \
5273c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_OUTPUT)
5283c0d9c63SKent Gibson 
5293c0d9c63SKent Gibson #define GPIO_V2_LINE_DRIVE_FLAGS \
5303c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
5313c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_OPEN_SOURCE)
5323c0d9c63SKent Gibson 
53373e03419SKent Gibson #define GPIO_V2_LINE_EDGE_FLAGS \
53473e03419SKent Gibson 	(GPIO_V2_LINE_FLAG_EDGE_RISING | \
53573e03419SKent Gibson 	 GPIO_V2_LINE_FLAG_EDGE_FALLING)
53673e03419SKent Gibson 
5375e2ca893SKent Gibson #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS
5385e2ca893SKent Gibson 
5393c0d9c63SKent Gibson #define GPIO_V2_LINE_VALID_FLAGS \
5403c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
5413c0d9c63SKent Gibson 	 GPIO_V2_LINE_DIRECTION_FLAGS | \
5423c0d9c63SKent Gibson 	 GPIO_V2_LINE_DRIVE_FLAGS | \
54373e03419SKent Gibson 	 GPIO_V2_LINE_EDGE_FLAGS | \
54426d060e4SKent Gibson 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
5452068339aSDipen Patel 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
5463c0d9c63SKent Gibson 	 GPIO_V2_LINE_BIAS_FLAGS)
5473c0d9c63SKent Gibson 
54873e03419SKent Gibson static void linereq_put_event(struct linereq *lr,
54973e03419SKent Gibson 			      struct gpio_v2_line_event *le)
55073e03419SKent Gibson {
55173e03419SKent Gibson 	bool overflow = false;
55273e03419SKent Gibson 
55373e03419SKent Gibson 	spin_lock(&lr->wait.lock);
55473e03419SKent Gibson 	if (kfifo_is_full(&lr->events)) {
55573e03419SKent Gibson 		overflow = true;
55673e03419SKent Gibson 		kfifo_skip(&lr->events);
55773e03419SKent Gibson 	}
55873e03419SKent Gibson 	kfifo_in(&lr->events, le, 1);
55973e03419SKent Gibson 	spin_unlock(&lr->wait.lock);
56073e03419SKent Gibson 	if (!overflow)
56173e03419SKent Gibson 		wake_up_poll(&lr->wait, EPOLLIN);
56273e03419SKent Gibson 	else
56373e03419SKent Gibson 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
56473e03419SKent Gibson }
56573e03419SKent Gibson 
56626d060e4SKent Gibson static u64 line_event_timestamp(struct line *line)
56726d060e4SKent Gibson {
56826d060e4SKent Gibson 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
56926d060e4SKent Gibson 		return ktime_get_real_ns();
5702068339aSDipen Patel 	else if (test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))
5712068339aSDipen Patel 		return line->timestamp_ns;
57226d060e4SKent Gibson 
57326d060e4SKent Gibson 	return ktime_get_ns();
57426d060e4SKent Gibson }
57526d060e4SKent Gibson 
5762068339aSDipen Patel static enum hte_return process_hw_ts_thread(void *p)
5772068339aSDipen Patel {
5782068339aSDipen Patel 	struct line *line;
5792068339aSDipen Patel 	struct linereq *lr;
5802068339aSDipen Patel 	struct gpio_v2_line_event le;
5812068339aSDipen Patel 	int level;
5822068339aSDipen Patel 	u64 eflags;
5832068339aSDipen Patel 
5842068339aSDipen Patel 	if (!p)
5852068339aSDipen Patel 		return HTE_CB_HANDLED;
5862068339aSDipen Patel 
5872068339aSDipen Patel 	line = p;
5882068339aSDipen Patel 	lr = line->req;
5892068339aSDipen Patel 
5902068339aSDipen Patel 	memset(&le, 0, sizeof(le));
5912068339aSDipen Patel 
5922068339aSDipen Patel 	le.timestamp_ns = line->timestamp_ns;
5932068339aSDipen Patel 	eflags = READ_ONCE(line->eflags);
5942068339aSDipen Patel 
5952068339aSDipen Patel 	if (eflags == GPIO_V2_LINE_FLAG_EDGE_BOTH) {
5962068339aSDipen Patel 		if (line->raw_level >= 0) {
5972068339aSDipen Patel 			if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
5982068339aSDipen Patel 				level = !line->raw_level;
5992068339aSDipen Patel 			else
6002068339aSDipen Patel 				level = line->raw_level;
6012068339aSDipen Patel 		} else {
6022068339aSDipen Patel 			level = gpiod_get_value_cansleep(line->desc);
6032068339aSDipen Patel 		}
6042068339aSDipen Patel 
6052068339aSDipen Patel 		if (level)
6062068339aSDipen Patel 			le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
6072068339aSDipen Patel 		else
6082068339aSDipen Patel 			le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
6092068339aSDipen Patel 	} else if (eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) {
6102068339aSDipen Patel 		/* Emit low-to-high event */
6112068339aSDipen Patel 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
6122068339aSDipen Patel 	} else if (eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) {
6132068339aSDipen Patel 		/* Emit high-to-low event */
6142068339aSDipen Patel 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
6152068339aSDipen Patel 	} else {
6162068339aSDipen Patel 		return HTE_CB_HANDLED;
6172068339aSDipen Patel 	}
6182068339aSDipen Patel 	le.line_seqno = line->line_seqno;
6192068339aSDipen Patel 	le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
6202068339aSDipen Patel 	le.offset = gpio_chip_hwgpio(line->desc);
6212068339aSDipen Patel 
6222068339aSDipen Patel 	linereq_put_event(lr, &le);
6232068339aSDipen Patel 
6242068339aSDipen Patel 	return HTE_CB_HANDLED;
6252068339aSDipen Patel }
6262068339aSDipen Patel 
6272068339aSDipen Patel static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p)
6282068339aSDipen Patel {
6292068339aSDipen Patel 	struct line *line;
6302068339aSDipen Patel 	struct linereq *lr;
6312068339aSDipen Patel 	int diff_seqno = 0;
6322068339aSDipen Patel 
6332068339aSDipen Patel 	if (!ts || !p)
6342068339aSDipen Patel 		return HTE_CB_HANDLED;
6352068339aSDipen Patel 
6362068339aSDipen Patel 	line = p;
6372068339aSDipen Patel 	line->timestamp_ns = ts->tsc;
6382068339aSDipen Patel 	line->raw_level = ts->raw_level;
6392068339aSDipen Patel 	lr = line->req;
6402068339aSDipen Patel 
6412068339aSDipen Patel 	if (READ_ONCE(line->sw_debounced)) {
6422068339aSDipen Patel 		line->total_discard_seq++;
6432068339aSDipen Patel 		line->last_seqno = ts->seq;
6442068339aSDipen Patel 		mod_delayed_work(system_wq, &line->work,
6452068339aSDipen Patel 		  usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
6462068339aSDipen Patel 	} else {
6472068339aSDipen Patel 		if (unlikely(ts->seq < line->line_seqno))
6482068339aSDipen Patel 			return HTE_CB_HANDLED;
6492068339aSDipen Patel 
6502068339aSDipen Patel 		diff_seqno = ts->seq - line->line_seqno;
6512068339aSDipen Patel 		line->line_seqno = ts->seq;
6522068339aSDipen Patel 		if (lr->num_lines != 1)
6532068339aSDipen Patel 			line->req_seqno = atomic_add_return(diff_seqno,
6542068339aSDipen Patel 							    &lr->seqno);
6552068339aSDipen Patel 
6562068339aSDipen Patel 		return HTE_RUN_SECOND_CB;
6572068339aSDipen Patel 	}
6582068339aSDipen Patel 
6592068339aSDipen Patel 	return HTE_CB_HANDLED;
6602068339aSDipen Patel }
6612068339aSDipen Patel 
66273e03419SKent Gibson static irqreturn_t edge_irq_thread(int irq, void *p)
66373e03419SKent Gibson {
66473e03419SKent Gibson 	struct line *line = p;
66573e03419SKent Gibson 	struct linereq *lr = line->req;
66673e03419SKent Gibson 	struct gpio_v2_line_event le;
6673ffb7c45SKent Gibson 	u64 eflags;
66873e03419SKent Gibson 
66973e03419SKent Gibson 	/* Do not leak kernel stack to userspace */
67073e03419SKent Gibson 	memset(&le, 0, sizeof(le));
67173e03419SKent Gibson 
67273e03419SKent Gibson 	if (line->timestamp_ns) {
67373e03419SKent Gibson 		le.timestamp_ns = line->timestamp_ns;
67473e03419SKent Gibson 	} else {
67573e03419SKent Gibson 		/*
67673e03419SKent Gibson 		 * We may be running from a nested threaded interrupt in
67773e03419SKent Gibson 		 * which case we didn't get the timestamp from
67873e03419SKent Gibson 		 * edge_irq_handler().
67973e03419SKent Gibson 		 */
68026d060e4SKent Gibson 		le.timestamp_ns = line_event_timestamp(line);
68173e03419SKent Gibson 		if (lr->num_lines != 1)
68273e03419SKent Gibson 			line->req_seqno = atomic_inc_return(&lr->seqno);
68373e03419SKent Gibson 	}
68473e03419SKent Gibson 	line->timestamp_ns = 0;
68573e03419SKent Gibson 
6863ffb7c45SKent Gibson 	eflags = READ_ONCE(line->eflags);
6875e2ca893SKent Gibson 	if (eflags == GPIO_V2_LINE_FLAG_EDGE_BOTH) {
68873e03419SKent Gibson 		int level = gpiod_get_value_cansleep(line->desc);
68973e03419SKent Gibson 
69073e03419SKent Gibson 		if (level)
69173e03419SKent Gibson 			/* Emit low-to-high event */
69273e03419SKent Gibson 			le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
69373e03419SKent Gibson 		else
69473e03419SKent Gibson 			/* Emit high-to-low event */
69573e03419SKent Gibson 			le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
6963ffb7c45SKent Gibson 	} else if (eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) {
69773e03419SKent Gibson 		/* Emit low-to-high event */
69873e03419SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
6993ffb7c45SKent Gibson 	} else if (eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) {
70073e03419SKent Gibson 		/* Emit high-to-low event */
70173e03419SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
70273e03419SKent Gibson 	} else {
70373e03419SKent Gibson 		return IRQ_NONE;
70473e03419SKent Gibson 	}
70573e03419SKent Gibson 	line->line_seqno++;
70673e03419SKent Gibson 	le.line_seqno = line->line_seqno;
70773e03419SKent Gibson 	le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
70873e03419SKent Gibson 	le.offset = gpio_chip_hwgpio(line->desc);
70973e03419SKent Gibson 
71073e03419SKent Gibson 	linereq_put_event(lr, &le);
71173e03419SKent Gibson 
71273e03419SKent Gibson 	return IRQ_HANDLED;
71373e03419SKent Gibson }
71473e03419SKent Gibson 
71573e03419SKent Gibson static irqreturn_t edge_irq_handler(int irq, void *p)
71673e03419SKent Gibson {
71773e03419SKent Gibson 	struct line *line = p;
71873e03419SKent Gibson 	struct linereq *lr = line->req;
71973e03419SKent Gibson 
72073e03419SKent Gibson 	/*
72173e03419SKent Gibson 	 * Just store the timestamp in hardirq context so we get it as
72273e03419SKent Gibson 	 * close in time as possible to the actual event.
72373e03419SKent Gibson 	 */
72426d060e4SKent Gibson 	line->timestamp_ns = line_event_timestamp(line);
72573e03419SKent Gibson 
72673e03419SKent Gibson 	if (lr->num_lines != 1)
72773e03419SKent Gibson 		line->req_seqno = atomic_inc_return(&lr->seqno);
72873e03419SKent Gibson 
72973e03419SKent Gibson 	return IRQ_WAKE_THREAD;
73073e03419SKent Gibson }
73173e03419SKent Gibson 
73265cff704SKent Gibson /*
73365cff704SKent Gibson  * returns the current debounced logical value.
73465cff704SKent Gibson  */
73565cff704SKent Gibson static bool debounced_value(struct line *line)
73665cff704SKent Gibson {
73765cff704SKent Gibson 	bool value;
73865cff704SKent Gibson 
73965cff704SKent Gibson 	/*
74065cff704SKent Gibson 	 * minor race - debouncer may be stopped here, so edge_detector_stop()
74165cff704SKent Gibson 	 * must leave the value unchanged so the following will read the level
74265cff704SKent Gibson 	 * from when the debouncer was last running.
74365cff704SKent Gibson 	 */
74465cff704SKent Gibson 	value = READ_ONCE(line->level);
74565cff704SKent Gibson 
74665cff704SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
74765cff704SKent Gibson 		value = !value;
74865cff704SKent Gibson 
74965cff704SKent Gibson 	return value;
75065cff704SKent Gibson }
75165cff704SKent Gibson 
75265cff704SKent Gibson static irqreturn_t debounce_irq_handler(int irq, void *p)
75365cff704SKent Gibson {
75465cff704SKent Gibson 	struct line *line = p;
75565cff704SKent Gibson 
75665cff704SKent Gibson 	mod_delayed_work(system_wq, &line->work,
75765cff704SKent Gibson 		usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
75865cff704SKent Gibson 
75965cff704SKent Gibson 	return IRQ_HANDLED;
76065cff704SKent Gibson }
76165cff704SKent Gibson 
76265cff704SKent Gibson static void debounce_work_func(struct work_struct *work)
76365cff704SKent Gibson {
76465cff704SKent Gibson 	struct gpio_v2_line_event le;
76565cff704SKent Gibson 	struct line *line = container_of(work, struct line, work.work);
76665cff704SKent Gibson 	struct linereq *lr;
7672068339aSDipen Patel 	int level, diff_seqno;
7683ffb7c45SKent Gibson 	u64 eflags;
76965cff704SKent Gibson 
7702068339aSDipen Patel 	if (test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags)) {
7712068339aSDipen Patel 		level = line->raw_level;
7722068339aSDipen Patel 		if (level < 0)
77365cff704SKent Gibson 			level = gpiod_get_raw_value_cansleep(line->desc);
7742068339aSDipen Patel 	} else {
7752068339aSDipen Patel 		level = gpiod_get_raw_value_cansleep(line->desc);
7762068339aSDipen Patel 	}
77765cff704SKent Gibson 	if (level < 0) {
77865cff704SKent Gibson 		pr_debug_ratelimited("debouncer failed to read line value\n");
77965cff704SKent Gibson 		return;
78065cff704SKent Gibson 	}
78165cff704SKent Gibson 
78265cff704SKent Gibson 	if (READ_ONCE(line->level) == level)
78365cff704SKent Gibson 		return;
78465cff704SKent Gibson 
78565cff704SKent Gibson 	WRITE_ONCE(line->level, level);
78665cff704SKent Gibson 
78765cff704SKent Gibson 	/* -- edge detection -- */
7883ffb7c45SKent Gibson 	eflags = READ_ONCE(line->eflags);
7893ffb7c45SKent Gibson 	if (!eflags)
79065cff704SKent Gibson 		return;
79165cff704SKent Gibson 
79265cff704SKent Gibson 	/* switch from physical level to logical - if they differ */
79365cff704SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
79465cff704SKent Gibson 		level = !level;
79565cff704SKent Gibson 
79665cff704SKent Gibson 	/* ignore edges that are not being monitored */
7973ffb7c45SKent Gibson 	if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
7983ffb7c45SKent Gibson 	    ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
79965cff704SKent Gibson 		return;
80065cff704SKent Gibson 
80165cff704SKent Gibson 	/* Do not leak kernel stack to userspace */
80265cff704SKent Gibson 	memset(&le, 0, sizeof(le));
80365cff704SKent Gibson 
80465cff704SKent Gibson 	lr = line->req;
80526d060e4SKent Gibson 	le.timestamp_ns = line_event_timestamp(line);
80665cff704SKent Gibson 	le.offset = gpio_chip_hwgpio(line->desc);
8072068339aSDipen Patel 	if (test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags)) {
8082068339aSDipen Patel 		/* discard events except the last one */
8092068339aSDipen Patel 		line->total_discard_seq -= 1;
8102068339aSDipen Patel 		diff_seqno = line->last_seqno - line->total_discard_seq -
8112068339aSDipen Patel 				line->line_seqno;
8122068339aSDipen Patel 		line->line_seqno = line->last_seqno - line->total_discard_seq;
8132068339aSDipen Patel 		le.line_seqno = line->line_seqno;
8142068339aSDipen Patel 		le.seqno = (lr->num_lines == 1) ?
8152068339aSDipen Patel 			le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno);
8162068339aSDipen Patel 	} else {
81765cff704SKent Gibson 		line->line_seqno++;
81865cff704SKent Gibson 		le.line_seqno = line->line_seqno;
81965cff704SKent Gibson 		le.seqno = (lr->num_lines == 1) ?
82065cff704SKent Gibson 			le.line_seqno : atomic_inc_return(&lr->seqno);
8212068339aSDipen Patel 	}
82265cff704SKent Gibson 
82365cff704SKent Gibson 	if (level)
82465cff704SKent Gibson 		/* Emit low-to-high event */
82565cff704SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
82665cff704SKent Gibson 	else
82765cff704SKent Gibson 		/* Emit high-to-low event */
82865cff704SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
82965cff704SKent Gibson 
83065cff704SKent Gibson 	linereq_put_event(lr, &le);
83165cff704SKent Gibson }
83265cff704SKent Gibson 
8332068339aSDipen Patel static int hte_edge_setup(struct line *line, u64 eflags)
8342068339aSDipen Patel {
8352068339aSDipen Patel 	int ret;
8362068339aSDipen Patel 	unsigned long flags = 0;
8372068339aSDipen Patel 	struct hte_ts_desc *hdesc = &line->hdesc;
8382068339aSDipen Patel 
8392068339aSDipen Patel 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
8402068339aSDipen Patel 		flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
8412068339aSDipen Patel 				  HTE_FALLING_EDGE_TS : HTE_RISING_EDGE_TS;
8422068339aSDipen Patel 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
8432068339aSDipen Patel 		flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
8442068339aSDipen Patel 				  HTE_RISING_EDGE_TS : HTE_FALLING_EDGE_TS;
8452068339aSDipen Patel 
8462068339aSDipen Patel 	line->total_discard_seq = 0;
8472068339aSDipen Patel 
8482068339aSDipen Patel 	hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags,
8492068339aSDipen Patel 			   NULL, line->desc);
8502068339aSDipen Patel 
8512068339aSDipen Patel 	ret = hte_ts_get(NULL, hdesc, 0);
8522068339aSDipen Patel 	if (ret)
8532068339aSDipen Patel 		return ret;
8542068339aSDipen Patel 
8552068339aSDipen Patel 	return hte_request_ts_ns(hdesc, process_hw_ts,
8562068339aSDipen Patel 				 process_hw_ts_thread, line);
8572068339aSDipen Patel }
8582068339aSDipen Patel 
85965cff704SKent Gibson static int debounce_setup(struct line *line,
8602068339aSDipen Patel 			  unsigned int debounce_period_us, bool hte_req)
86165cff704SKent Gibson {
86265cff704SKent Gibson 	unsigned long irqflags;
86365cff704SKent Gibson 	int ret, level, irq;
86465cff704SKent Gibson 
86565cff704SKent Gibson 	/* try hardware */
86665cff704SKent Gibson 	ret = gpiod_set_debounce(line->desc, debounce_period_us);
86765cff704SKent Gibson 	if (!ret) {
86865cff704SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
86965cff704SKent Gibson 		return ret;
87065cff704SKent Gibson 	}
87165cff704SKent Gibson 	if (ret != -ENOTSUPP)
87265cff704SKent Gibson 		return ret;
87365cff704SKent Gibson 
87465cff704SKent Gibson 	if (debounce_period_us) {
87565cff704SKent Gibson 		/* setup software debounce */
87665cff704SKent Gibson 		level = gpiod_get_raw_value_cansleep(line->desc);
87765cff704SKent Gibson 		if (level < 0)
87865cff704SKent Gibson 			return level;
87965cff704SKent Gibson 
8802068339aSDipen Patel 		if (!hte_req) {
88165cff704SKent Gibson 			irq = gpiod_to_irq(line->desc);
88265cff704SKent Gibson 			if (irq < 0)
88365cff704SKent Gibson 				return -ENXIO;
88465cff704SKent Gibson 
88565cff704SKent Gibson 			irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
88665cff704SKent Gibson 			ret = request_irq(irq, debounce_irq_handler, irqflags,
88765cff704SKent Gibson 					  line->req->label, line);
88865cff704SKent Gibson 			if (ret)
88965cff704SKent Gibson 				return ret;
89065cff704SKent Gibson 			line->irq = irq;
8912068339aSDipen Patel 		} else {
8922068339aSDipen Patel 			ret = hte_edge_setup(line,
8932068339aSDipen Patel 					     GPIO_V2_LINE_FLAG_EDGE_RISING |
8942068339aSDipen Patel 					     GPIO_V2_LINE_FLAG_EDGE_FALLING);
8952068339aSDipen Patel 			if (ret)
8962068339aSDipen Patel 				return ret;
8972068339aSDipen Patel 		}
8982068339aSDipen Patel 
8992068339aSDipen Patel 		WRITE_ONCE(line->level, level);
9002068339aSDipen Patel 		WRITE_ONCE(line->sw_debounced, 1);
90165cff704SKent Gibson 	}
90265cff704SKent Gibson 	return 0;
90365cff704SKent Gibson }
90465cff704SKent Gibson 
90565cff704SKent Gibson static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
90665cff704SKent Gibson 					  unsigned int line_idx)
90765cff704SKent Gibson {
90865cff704SKent Gibson 	unsigned int i;
90965cff704SKent Gibson 	u64 mask = BIT_ULL(line_idx);
91065cff704SKent Gibson 
91165cff704SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
91265cff704SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
91365cff704SKent Gibson 		    (lc->attrs[i].mask & mask))
91465cff704SKent Gibson 			return true;
91565cff704SKent Gibson 	}
91665cff704SKent Gibson 	return false;
91765cff704SKent Gibson }
91865cff704SKent Gibson 
91965cff704SKent Gibson static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
92065cff704SKent Gibson 					       unsigned int line_idx)
92165cff704SKent Gibson {
92265cff704SKent Gibson 	unsigned int i;
92365cff704SKent Gibson 	u64 mask = BIT_ULL(line_idx);
92465cff704SKent Gibson 
92565cff704SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
92665cff704SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
92765cff704SKent Gibson 		    (lc->attrs[i].mask & mask))
92865cff704SKent Gibson 			return lc->attrs[i].attr.debounce_period_us;
92965cff704SKent Gibson 	}
93065cff704SKent Gibson 	return 0;
93165cff704SKent Gibson }
93265cff704SKent Gibson 
9332068339aSDipen Patel static void edge_detector_stop(struct line *line, bool hte_en)
93473e03419SKent Gibson {
9352068339aSDipen Patel 	if (line->irq && !hte_en) {
93673e03419SKent Gibson 		free_irq(line->irq, line);
93773e03419SKent Gibson 		line->irq = 0;
93873e03419SKent Gibson 	}
939a54756cbSKent Gibson 
9402068339aSDipen Patel 	if (hte_en)
9412068339aSDipen Patel 		hte_ts_put(&line->hdesc);
9422068339aSDipen Patel 
94365cff704SKent Gibson 	cancel_delayed_work_sync(&line->work);
94465cff704SKent Gibson 	WRITE_ONCE(line->sw_debounced, 0);
9453ffb7c45SKent Gibson 	WRITE_ONCE(line->eflags, 0);
94603a58ea5SKent Gibson 	if (line->desc)
94703a58ea5SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, 0);
94865cff704SKent Gibson 	/* do not change line->level - see comment in debounced_value() */
94973e03419SKent Gibson }
95073e03419SKent Gibson 
95173e03419SKent Gibson static int edge_detector_setup(struct line *line,
95265cff704SKent Gibson 			       struct gpio_v2_line_config *lc,
95365cff704SKent Gibson 			       unsigned int line_idx,
9542068339aSDipen Patel 			       u64 eflags, bool hte_req)
95573e03419SKent Gibson {
95665cff704SKent Gibson 	u32 debounce_period_us;
95773e03419SKent Gibson 	unsigned long irqflags = 0;
95873e03419SKent Gibson 	int irq, ret;
95973e03419SKent Gibson 
96073e03419SKent Gibson 	if (eflags && !kfifo_initialized(&line->req->events)) {
96173e03419SKent Gibson 		ret = kfifo_alloc(&line->req->events,
96273e03419SKent Gibson 				  line->req->event_buffer_size, GFP_KERNEL);
96373e03419SKent Gibson 		if (ret)
96473e03419SKent Gibson 			return ret;
96573e03419SKent Gibson 	}
9663ffb7c45SKent Gibson 	WRITE_ONCE(line->eflags, eflags);
96765cff704SKent Gibson 	if (gpio_v2_line_config_debounced(lc, line_idx)) {
96865cff704SKent Gibson 		debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
9692068339aSDipen Patel 		ret = debounce_setup(line, debounce_period_us, hte_req);
97065cff704SKent Gibson 		if (ret)
97165cff704SKent Gibson 			return ret;
97265cff704SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
97365cff704SKent Gibson 	}
97473e03419SKent Gibson 
97565cff704SKent Gibson 	/* detection disabled or sw debouncer will provide edge detection */
97665cff704SKent Gibson 	if (!eflags || READ_ONCE(line->sw_debounced))
97773e03419SKent Gibson 		return 0;
97873e03419SKent Gibson 
9792068339aSDipen Patel 	if (hte_req)
9802068339aSDipen Patel 		return hte_edge_setup(line, eflags);
9812068339aSDipen Patel 
98273e03419SKent Gibson 	irq = gpiod_to_irq(line->desc);
98373e03419SKent Gibson 	if (irq < 0)
98473e03419SKent Gibson 		return -ENXIO;
98573e03419SKent Gibson 
98673e03419SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
98773e03419SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
98873e03419SKent Gibson 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
98973e03419SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
99073e03419SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
99173e03419SKent Gibson 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
99273e03419SKent Gibson 	irqflags |= IRQF_ONESHOT;
99373e03419SKent Gibson 
99473e03419SKent Gibson 	/* Request a thread to read the events */
99573e03419SKent Gibson 	ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
99673e03419SKent Gibson 				   irqflags, line->req->label, line);
99773e03419SKent Gibson 	if (ret)
99873e03419SKent Gibson 		return ret;
99973e03419SKent Gibson 
100073e03419SKent Gibson 	line->irq = irq;
100173e03419SKent Gibson 	return 0;
100273e03419SKent Gibson }
100373e03419SKent Gibson 
100465cff704SKent Gibson static int edge_detector_update(struct line *line,
100565cff704SKent Gibson 				struct gpio_v2_line_config *lc,
100665cff704SKent Gibson 				unsigned int line_idx,
10072068339aSDipen Patel 				u64 flags, bool polarity_change,
10082068339aSDipen Patel 				bool prev_hte_flag)
1009a54756cbSKent Gibson {
10102068339aSDipen Patel 	u64 eflags = flags & GPIO_V2_LINE_EDGE_FLAGS;
101165cff704SKent Gibson 	unsigned int debounce_period_us =
101265cff704SKent Gibson 			gpio_v2_line_config_debounce_period(lc, line_idx);
10132068339aSDipen Patel 	bool hte_change = (prev_hte_flag !=
10142068339aSDipen Patel 		      ((flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) != 0));
101565cff704SKent Gibson 
10163ffb7c45SKent Gibson 	if ((READ_ONCE(line->eflags) == eflags) && !polarity_change &&
10172068339aSDipen Patel 	    (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us)
10182068339aSDipen Patel 	    && !hte_change)
1019a54756cbSKent Gibson 		return 0;
1020a54756cbSKent Gibson 
102165cff704SKent Gibson 	/* sw debounced and still will be...*/
102265cff704SKent Gibson 	if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
10233ffb7c45SKent Gibson 		WRITE_ONCE(line->eflags, eflags);
102465cff704SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
102565cff704SKent Gibson 		return 0;
102665cff704SKent Gibson 	}
102765cff704SKent Gibson 
102865cff704SKent Gibson 	/* reconfiguring edge detection or sw debounce being disabled */
10292068339aSDipen Patel 	if ((line->irq && !READ_ONCE(line->sw_debounced)) || prev_hte_flag ||
103065cff704SKent Gibson 	    (!debounce_period_us && READ_ONCE(line->sw_debounced)))
10312068339aSDipen Patel 		edge_detector_stop(line, prev_hte_flag);
1032a54756cbSKent Gibson 
10332068339aSDipen Patel 	return edge_detector_setup(line, lc, line_idx, eflags,
10342068339aSDipen Patel 				   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
1035a54756cbSKent Gibson }
1036a54756cbSKent Gibson 
10373c0d9c63SKent Gibson static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
10383c0d9c63SKent Gibson 				     unsigned int line_idx)
10393c0d9c63SKent Gibson {
10403c0d9c63SKent Gibson 	unsigned int i;
10413c0d9c63SKent Gibson 	u64 mask = BIT_ULL(line_idx);
10423c0d9c63SKent Gibson 
10433c0d9c63SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
10443c0d9c63SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
10453c0d9c63SKent Gibson 		    (lc->attrs[i].mask & mask))
10463c0d9c63SKent Gibson 			return lc->attrs[i].attr.flags;
10473c0d9c63SKent Gibson 	}
10483c0d9c63SKent Gibson 	return lc->flags;
10493c0d9c63SKent Gibson }
10503c0d9c63SKent Gibson 
10513c0d9c63SKent Gibson static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
10523c0d9c63SKent Gibson 					    unsigned int line_idx)
10533c0d9c63SKent Gibson {
10543c0d9c63SKent Gibson 	unsigned int i;
10553c0d9c63SKent Gibson 	u64 mask = BIT_ULL(line_idx);
10563c0d9c63SKent Gibson 
10573c0d9c63SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
10583c0d9c63SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
10593c0d9c63SKent Gibson 		    (lc->attrs[i].mask & mask))
10603c0d9c63SKent Gibson 			return !!(lc->attrs[i].attr.values & mask);
10613c0d9c63SKent Gibson 	}
10623c0d9c63SKent Gibson 	return 0;
10633c0d9c63SKent Gibson }
10643c0d9c63SKent Gibson 
10653c0d9c63SKent Gibson static int gpio_v2_line_flags_validate(u64 flags)
10663c0d9c63SKent Gibson {
10673c0d9c63SKent Gibson 	/* Return an error if an unknown flag is set */
10683c0d9c63SKent Gibson 	if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
10693c0d9c63SKent Gibson 		return -EINVAL;
10703c0d9c63SKent Gibson 	/*
10713c0d9c63SKent Gibson 	 * Do not allow both INPUT and OUTPUT flags to be set as they are
10723c0d9c63SKent Gibson 	 * contradictory.
10733c0d9c63SKent Gibson 	 */
10743c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
10753c0d9c63SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_OUTPUT))
10763c0d9c63SKent Gibson 		return -EINVAL;
10773c0d9c63SKent Gibson 
10782068339aSDipen Patel 	/* Only allow one event clock source */
10792068339aSDipen Patel 	if ((flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) &&
10802068339aSDipen Patel 	    (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
10812068339aSDipen Patel 		return -EINVAL;
10822068339aSDipen Patel 
108373e03419SKent Gibson 	/* Edge detection requires explicit input. */
108473e03419SKent Gibson 	if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
108573e03419SKent Gibson 	    !(flags & GPIO_V2_LINE_FLAG_INPUT))
108673e03419SKent Gibson 		return -EINVAL;
108773e03419SKent Gibson 
10883c0d9c63SKent Gibson 	/*
10893c0d9c63SKent Gibson 	 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
10903c0d9c63SKent Gibson 	 * request. If the hardware actually supports enabling both at the
10913c0d9c63SKent Gibson 	 * same time the electrical result would be disastrous.
10923c0d9c63SKent Gibson 	 */
10933c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
10943c0d9c63SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
10953c0d9c63SKent Gibson 		return -EINVAL;
10963c0d9c63SKent Gibson 
10973c0d9c63SKent Gibson 	/* Drive requires explicit output direction. */
10983c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
10993c0d9c63SKent Gibson 	    !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
11003c0d9c63SKent Gibson 		return -EINVAL;
11013c0d9c63SKent Gibson 
11023c0d9c63SKent Gibson 	/* Bias requires explicit direction. */
11033c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
11043c0d9c63SKent Gibson 	    !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
11053c0d9c63SKent Gibson 		return -EINVAL;
11063c0d9c63SKent Gibson 
11073c0d9c63SKent Gibson 	/* Only one bias flag can be set. */
11083c0d9c63SKent Gibson 	if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
11093c0d9c63SKent Gibson 	     (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
11103c0d9c63SKent Gibson 		       GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
11113c0d9c63SKent Gibson 	    ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
11123c0d9c63SKent Gibson 	     (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
11133c0d9c63SKent Gibson 		return -EINVAL;
11143c0d9c63SKent Gibson 
11153c0d9c63SKent Gibson 	return 0;
11163c0d9c63SKent Gibson }
11173c0d9c63SKent Gibson 
11183c0d9c63SKent Gibson static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
11193c0d9c63SKent Gibson 					unsigned int num_lines)
11203c0d9c63SKent Gibson {
11213c0d9c63SKent Gibson 	unsigned int i;
11223c0d9c63SKent Gibson 	u64 flags;
11233c0d9c63SKent Gibson 	int ret;
11243c0d9c63SKent Gibson 
11253c0d9c63SKent Gibson 	if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
11263c0d9c63SKent Gibson 		return -EINVAL;
11273c0d9c63SKent Gibson 
11283c0d9c63SKent Gibson 	if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
11293c0d9c63SKent Gibson 		return -EINVAL;
11303c0d9c63SKent Gibson 
11313c0d9c63SKent Gibson 	for (i = 0; i < num_lines; i++) {
11323c0d9c63SKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
11333c0d9c63SKent Gibson 		ret = gpio_v2_line_flags_validate(flags);
11343c0d9c63SKent Gibson 		if (ret)
11353c0d9c63SKent Gibson 			return ret;
113665cff704SKent Gibson 
113765cff704SKent Gibson 		/* debounce requires explicit input */
113865cff704SKent Gibson 		if (gpio_v2_line_config_debounced(lc, i) &&
113965cff704SKent Gibson 		    !(flags & GPIO_V2_LINE_FLAG_INPUT))
114065cff704SKent Gibson 			return -EINVAL;
11413c0d9c63SKent Gibson 	}
11423c0d9c63SKent Gibson 	return 0;
11433c0d9c63SKent Gibson }
11443c0d9c63SKent Gibson 
11453c0d9c63SKent Gibson static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
11463c0d9c63SKent Gibson 						    unsigned long *flagsp)
11473c0d9c63SKent Gibson {
11483c0d9c63SKent Gibson 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
11493c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
11503c0d9c63SKent Gibson 
11513c0d9c63SKent Gibson 	if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
11523c0d9c63SKent Gibson 		set_bit(FLAG_IS_OUT, flagsp);
11533c0d9c63SKent Gibson 	else if (flags & GPIO_V2_LINE_FLAG_INPUT)
11543c0d9c63SKent Gibson 		clear_bit(FLAG_IS_OUT, flagsp);
11553c0d9c63SKent Gibson 
115673e03419SKent Gibson 	assign_bit(FLAG_EDGE_RISING, flagsp,
115773e03419SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
115873e03419SKent Gibson 	assign_bit(FLAG_EDGE_FALLING, flagsp,
115973e03419SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
116073e03419SKent Gibson 
11613c0d9c63SKent Gibson 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
11623c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
11633c0d9c63SKent Gibson 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
11643c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
11653c0d9c63SKent Gibson 
11663c0d9c63SKent Gibson 	assign_bit(FLAG_PULL_UP, flagsp,
11673c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
11683c0d9c63SKent Gibson 	assign_bit(FLAG_PULL_DOWN, flagsp,
11693c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
11703c0d9c63SKent Gibson 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
11713c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
117226d060e4SKent Gibson 
117326d060e4SKent Gibson 	assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
117426d060e4SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
11752068339aSDipen Patel 	assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp,
11762068339aSDipen Patel 		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
11773c0d9c63SKent Gibson }
11783c0d9c63SKent Gibson 
11793c0d9c63SKent Gibson static long linereq_get_values(struct linereq *lr, void __user *ip)
11803c0d9c63SKent Gibson {
11813c0d9c63SKent Gibson 	struct gpio_v2_line_values lv;
11823c0d9c63SKent Gibson 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
11833c0d9c63SKent Gibson 	struct gpio_desc **descs;
11843c0d9c63SKent Gibson 	unsigned int i, didx, num_get;
118565cff704SKent Gibson 	bool val;
11863c0d9c63SKent Gibson 	int ret;
11873c0d9c63SKent Gibson 
11883c0d9c63SKent Gibson 	/* NOTE: It's ok to read values of output lines. */
11893c0d9c63SKent Gibson 	if (copy_from_user(&lv, ip, sizeof(lv)))
11903c0d9c63SKent Gibson 		return -EFAULT;
11913c0d9c63SKent Gibson 
11923c0d9c63SKent Gibson 	for (num_get = 0, i = 0; i < lr->num_lines; i++) {
11933c0d9c63SKent Gibson 		if (lv.mask & BIT_ULL(i)) {
11943c0d9c63SKent Gibson 			num_get++;
11953c0d9c63SKent Gibson 			descs = &lr->lines[i].desc;
11963c0d9c63SKent Gibson 		}
11973c0d9c63SKent Gibson 	}
11983c0d9c63SKent Gibson 
11993c0d9c63SKent Gibson 	if (num_get == 0)
12003c0d9c63SKent Gibson 		return -EINVAL;
12013c0d9c63SKent Gibson 
12023c0d9c63SKent Gibson 	if (num_get != 1) {
12033c0d9c63SKent Gibson 		descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
12043c0d9c63SKent Gibson 		if (!descs)
12053c0d9c63SKent Gibson 			return -ENOMEM;
12063c0d9c63SKent Gibson 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
12073c0d9c63SKent Gibson 			if (lv.mask & BIT_ULL(i)) {
12083c0d9c63SKent Gibson 				descs[didx] = lr->lines[i].desc;
12093c0d9c63SKent Gibson 				didx++;
12103c0d9c63SKent Gibson 			}
12113c0d9c63SKent Gibson 		}
12123c0d9c63SKent Gibson 	}
12133c0d9c63SKent Gibson 	ret = gpiod_get_array_value_complex(false, true, num_get,
12143c0d9c63SKent Gibson 					    descs, NULL, vals);
12153c0d9c63SKent Gibson 
12163c0d9c63SKent Gibson 	if (num_get != 1)
12173c0d9c63SKent Gibson 		kfree(descs);
12183c0d9c63SKent Gibson 	if (ret)
12193c0d9c63SKent Gibson 		return ret;
12203c0d9c63SKent Gibson 
12213c0d9c63SKent Gibson 	lv.bits = 0;
12223c0d9c63SKent Gibson 	for (didx = 0, i = 0; i < lr->num_lines; i++) {
12233c0d9c63SKent Gibson 		if (lv.mask & BIT_ULL(i)) {
122465cff704SKent Gibson 			if (lr->lines[i].sw_debounced)
122565cff704SKent Gibson 				val = debounced_value(&lr->lines[i]);
122665cff704SKent Gibson 			else
122765cff704SKent Gibson 				val = test_bit(didx, vals);
122865cff704SKent Gibson 			if (val)
12293c0d9c63SKent Gibson 				lv.bits |= BIT_ULL(i);
12303c0d9c63SKent Gibson 			didx++;
12313c0d9c63SKent Gibson 		}
12323c0d9c63SKent Gibson 	}
12333c0d9c63SKent Gibson 
12343c0d9c63SKent Gibson 	if (copy_to_user(ip, &lv, sizeof(lv)))
12353c0d9c63SKent Gibson 		return -EFAULT;
12363c0d9c63SKent Gibson 
12373c0d9c63SKent Gibson 	return 0;
12383c0d9c63SKent Gibson }
12393c0d9c63SKent Gibson 
12407b8e00d9SKent Gibson static long linereq_set_values_unlocked(struct linereq *lr,
12417b8e00d9SKent Gibson 					struct gpio_v2_line_values *lv)
12427b8e00d9SKent Gibson {
12437b8e00d9SKent Gibson 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
12447b8e00d9SKent Gibson 	struct gpio_desc **descs;
12457b8e00d9SKent Gibson 	unsigned int i, didx, num_set;
12467b8e00d9SKent Gibson 	int ret;
12477b8e00d9SKent Gibson 
12487b8e00d9SKent Gibson 	bitmap_zero(vals, GPIO_V2_LINES_MAX);
12497b8e00d9SKent Gibson 	for (num_set = 0, i = 0; i < lr->num_lines; i++) {
12507b8e00d9SKent Gibson 		if (lv->mask & BIT_ULL(i)) {
12517b8e00d9SKent Gibson 			if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
12527b8e00d9SKent Gibson 				return -EPERM;
12537b8e00d9SKent Gibson 			if (lv->bits & BIT_ULL(i))
12547b8e00d9SKent Gibson 				__set_bit(num_set, vals);
12557b8e00d9SKent Gibson 			num_set++;
12567b8e00d9SKent Gibson 			descs = &lr->lines[i].desc;
12577b8e00d9SKent Gibson 		}
12587b8e00d9SKent Gibson 	}
12597b8e00d9SKent Gibson 	if (num_set == 0)
12607b8e00d9SKent Gibson 		return -EINVAL;
12617b8e00d9SKent Gibson 
12627b8e00d9SKent Gibson 	if (num_set != 1) {
12637b8e00d9SKent Gibson 		/* build compacted desc array and values */
12647b8e00d9SKent Gibson 		descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
12657b8e00d9SKent Gibson 		if (!descs)
12667b8e00d9SKent Gibson 			return -ENOMEM;
12677b8e00d9SKent Gibson 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
12687b8e00d9SKent Gibson 			if (lv->mask & BIT_ULL(i)) {
12697b8e00d9SKent Gibson 				descs[didx] = lr->lines[i].desc;
12707b8e00d9SKent Gibson 				didx++;
12717b8e00d9SKent Gibson 			}
12727b8e00d9SKent Gibson 		}
12737b8e00d9SKent Gibson 	}
12747b8e00d9SKent Gibson 	ret = gpiod_set_array_value_complex(false, true, num_set,
12757b8e00d9SKent Gibson 					    descs, NULL, vals);
12767b8e00d9SKent Gibson 
12777b8e00d9SKent Gibson 	if (num_set != 1)
12787b8e00d9SKent Gibson 		kfree(descs);
12797b8e00d9SKent Gibson 	return ret;
12807b8e00d9SKent Gibson }
12817b8e00d9SKent Gibson 
12827b8e00d9SKent Gibson static long linereq_set_values(struct linereq *lr, void __user *ip)
12837b8e00d9SKent Gibson {
12847b8e00d9SKent Gibson 	struct gpio_v2_line_values lv;
12857b8e00d9SKent Gibson 	int ret;
12867b8e00d9SKent Gibson 
12877b8e00d9SKent Gibson 	if (copy_from_user(&lv, ip, sizeof(lv)))
12887b8e00d9SKent Gibson 		return -EFAULT;
12897b8e00d9SKent Gibson 
12907b8e00d9SKent Gibson 	mutex_lock(&lr->config_mutex);
12917b8e00d9SKent Gibson 
12927b8e00d9SKent Gibson 	ret = linereq_set_values_unlocked(lr, &lv);
12937b8e00d9SKent Gibson 
12947b8e00d9SKent Gibson 	mutex_unlock(&lr->config_mutex);
12957b8e00d9SKent Gibson 
12967b8e00d9SKent Gibson 	return ret;
12977b8e00d9SKent Gibson }
12987b8e00d9SKent Gibson 
1299a54756cbSKent Gibson static long linereq_set_config_unlocked(struct linereq *lr,
1300a54756cbSKent Gibson 					struct gpio_v2_line_config *lc)
1301a54756cbSKent Gibson {
1302a54756cbSKent Gibson 	struct gpio_desc *desc;
1303a54756cbSKent Gibson 	unsigned int i;
1304a54756cbSKent Gibson 	u64 flags;
1305a54756cbSKent Gibson 	bool polarity_change;
13062068339aSDipen Patel 	bool prev_hte_flag;
1307a54756cbSKent Gibson 	int ret;
1308a54756cbSKent Gibson 
1309a54756cbSKent Gibson 	for (i = 0; i < lr->num_lines; i++) {
1310a54756cbSKent Gibson 		desc = lr->lines[i].desc;
1311a54756cbSKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
1312a54756cbSKent Gibson 		polarity_change =
1313a54756cbSKent Gibson 			(!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) !=
1314a54756cbSKent Gibson 			 ((flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) != 0));
1315a54756cbSKent Gibson 
13162068339aSDipen Patel 		prev_hte_flag = !!test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags);
13172068339aSDipen Patel 
1318a54756cbSKent Gibson 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1319a54756cbSKent Gibson 		/*
1320a54756cbSKent Gibson 		 * Lines have to be requested explicitly for input
1321a54756cbSKent Gibson 		 * or output, else the line will be treated "as is".
1322a54756cbSKent Gibson 		 */
1323a54756cbSKent Gibson 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1324a54756cbSKent Gibson 			int val = gpio_v2_line_config_output_value(lc, i);
1325a54756cbSKent Gibson 
13262068339aSDipen Patel 			edge_detector_stop(&lr->lines[i], prev_hte_flag);
1327a54756cbSKent Gibson 			ret = gpiod_direction_output(desc, val);
1328a54756cbSKent Gibson 			if (ret)
1329a54756cbSKent Gibson 				return ret;
1330a54756cbSKent Gibson 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1331a54756cbSKent Gibson 			ret = gpiod_direction_input(desc);
1332a54756cbSKent Gibson 			if (ret)
1333a54756cbSKent Gibson 				return ret;
1334a54756cbSKent Gibson 
133565cff704SKent Gibson 			ret = edge_detector_update(&lr->lines[i], lc, i,
13362068339aSDipen Patel 					flags, polarity_change, prev_hte_flag);
1337a54756cbSKent Gibson 			if (ret)
1338a54756cbSKent Gibson 				return ret;
1339a54756cbSKent Gibson 		}
1340a54756cbSKent Gibson 
1341a54756cbSKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
1342a54756cbSKent Gibson 					     GPIO_V2_LINE_CHANGED_CONFIG,
1343a54756cbSKent Gibson 					     desc);
1344a54756cbSKent Gibson 	}
1345a54756cbSKent Gibson 	return 0;
1346a54756cbSKent Gibson }
1347a54756cbSKent Gibson 
1348a54756cbSKent Gibson static long linereq_set_config(struct linereq *lr, void __user *ip)
1349a54756cbSKent Gibson {
1350a54756cbSKent Gibson 	struct gpio_v2_line_config lc;
1351a54756cbSKent Gibson 	int ret;
1352a54756cbSKent Gibson 
1353a54756cbSKent Gibson 	if (copy_from_user(&lc, ip, sizeof(lc)))
1354a54756cbSKent Gibson 		return -EFAULT;
1355a54756cbSKent Gibson 
1356a54756cbSKent Gibson 	ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
1357a54756cbSKent Gibson 	if (ret)
1358a54756cbSKent Gibson 		return ret;
1359a54756cbSKent Gibson 
1360a54756cbSKent Gibson 	mutex_lock(&lr->config_mutex);
1361a54756cbSKent Gibson 
1362a54756cbSKent Gibson 	ret = linereq_set_config_unlocked(lr, &lc);
1363a54756cbSKent Gibson 
1364a54756cbSKent Gibson 	mutex_unlock(&lr->config_mutex);
1365a54756cbSKent Gibson 
1366a54756cbSKent Gibson 	return ret;
1367a54756cbSKent Gibson }
1368a54756cbSKent Gibson 
13693c0d9c63SKent Gibson static long linereq_ioctl(struct file *file, unsigned int cmd,
13703c0d9c63SKent Gibson 			  unsigned long arg)
13713c0d9c63SKent Gibson {
13723c0d9c63SKent Gibson 	struct linereq *lr = file->private_data;
13733c0d9c63SKent Gibson 	void __user *ip = (void __user *)arg;
13743c0d9c63SKent Gibson 
13753c0d9c63SKent Gibson 	if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL)
13763c0d9c63SKent Gibson 		return linereq_get_values(lr, ip);
13777b8e00d9SKent Gibson 	else if (cmd == GPIO_V2_LINE_SET_VALUES_IOCTL)
13787b8e00d9SKent Gibson 		return linereq_set_values(lr, ip);
1379a54756cbSKent Gibson 	else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL)
1380a54756cbSKent Gibson 		return linereq_set_config(lr, ip);
13813c0d9c63SKent Gibson 
13823c0d9c63SKent Gibson 	return -EINVAL;
13833c0d9c63SKent Gibson }
13843c0d9c63SKent Gibson 
13853c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
13863c0d9c63SKent Gibson static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
13873c0d9c63SKent Gibson 				 unsigned long arg)
13883c0d9c63SKent Gibson {
13893c0d9c63SKent Gibson 	return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
13903c0d9c63SKent Gibson }
13913c0d9c63SKent Gibson #endif
13923c0d9c63SKent Gibson 
139373e03419SKent Gibson static __poll_t linereq_poll(struct file *file,
139473e03419SKent Gibson 			    struct poll_table_struct *wait)
139573e03419SKent Gibson {
139673e03419SKent Gibson 	struct linereq *lr = file->private_data;
139773e03419SKent Gibson 	__poll_t events = 0;
139873e03419SKent Gibson 
139973e03419SKent Gibson 	poll_wait(file, &lr->wait, wait);
140073e03419SKent Gibson 
140173e03419SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
140273e03419SKent Gibson 						 &lr->wait.lock))
140373e03419SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
140473e03419SKent Gibson 
140573e03419SKent Gibson 	return events;
140673e03419SKent Gibson }
140773e03419SKent Gibson 
140873e03419SKent Gibson static ssize_t linereq_read(struct file *file,
140973e03419SKent Gibson 			    char __user *buf,
141073e03419SKent Gibson 			    size_t count,
141173e03419SKent Gibson 			    loff_t *f_ps)
141273e03419SKent Gibson {
141373e03419SKent Gibson 	struct linereq *lr = file->private_data;
141473e03419SKent Gibson 	struct gpio_v2_line_event le;
141573e03419SKent Gibson 	ssize_t bytes_read = 0;
141673e03419SKent Gibson 	int ret;
141773e03419SKent Gibson 
141873e03419SKent Gibson 	if (count < sizeof(le))
141973e03419SKent Gibson 		return -EINVAL;
142073e03419SKent Gibson 
142173e03419SKent Gibson 	do {
142273e03419SKent Gibson 		spin_lock(&lr->wait.lock);
142373e03419SKent Gibson 		if (kfifo_is_empty(&lr->events)) {
142473e03419SKent Gibson 			if (bytes_read) {
142573e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
142673e03419SKent Gibson 				return bytes_read;
142773e03419SKent Gibson 			}
142873e03419SKent Gibson 
142973e03419SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
143073e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
143173e03419SKent Gibson 				return -EAGAIN;
143273e03419SKent Gibson 			}
143373e03419SKent Gibson 
143473e03419SKent Gibson 			ret = wait_event_interruptible_locked(lr->wait,
143573e03419SKent Gibson 					!kfifo_is_empty(&lr->events));
143673e03419SKent Gibson 			if (ret) {
143773e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
143873e03419SKent Gibson 				return ret;
143973e03419SKent Gibson 			}
144073e03419SKent Gibson 		}
144173e03419SKent Gibson 
144273e03419SKent Gibson 		ret = kfifo_out(&lr->events, &le, 1);
144373e03419SKent Gibson 		spin_unlock(&lr->wait.lock);
144473e03419SKent Gibson 		if (ret != 1) {
144573e03419SKent Gibson 			/*
144673e03419SKent Gibson 			 * This should never happen - we were holding the
144773e03419SKent Gibson 			 * lock from the moment we learned the fifo is no
144873e03419SKent Gibson 			 * longer empty until now.
144973e03419SKent Gibson 			 */
145073e03419SKent Gibson 			ret = -EIO;
145173e03419SKent Gibson 			break;
145273e03419SKent Gibson 		}
145373e03419SKent Gibson 
145473e03419SKent Gibson 		if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
145573e03419SKent Gibson 			return -EFAULT;
145673e03419SKent Gibson 		bytes_read += sizeof(le);
145773e03419SKent Gibson 	} while (count >= bytes_read + sizeof(le));
145873e03419SKent Gibson 
145973e03419SKent Gibson 	return bytes_read;
146073e03419SKent Gibson }
146173e03419SKent Gibson 
14623c0d9c63SKent Gibson static void linereq_free(struct linereq *lr)
14633c0d9c63SKent Gibson {
14643c0d9c63SKent Gibson 	unsigned int i;
14652068339aSDipen Patel 	bool hte;
14663c0d9c63SKent Gibson 
14673c0d9c63SKent Gibson 	for (i = 0; i < lr->num_lines; i++) {
14682068339aSDipen Patel 		hte = !!test_bit(FLAG_EVENT_CLOCK_HTE,
14692068339aSDipen Patel 				 &lr->lines[i].desc->flags);
14702068339aSDipen Patel 		edge_detector_stop(&lr->lines[i], hte);
14713c0d9c63SKent Gibson 		if (lr->lines[i].desc)
14723c0d9c63SKent Gibson 			gpiod_free(lr->lines[i].desc);
14733c0d9c63SKent Gibson 	}
147473e03419SKent Gibson 	kfifo_free(&lr->events);
14753c0d9c63SKent Gibson 	kfree(lr->label);
14763c0d9c63SKent Gibson 	put_device(&lr->gdev->dev);
14773c0d9c63SKent Gibson 	kfree(lr);
14783c0d9c63SKent Gibson }
14793c0d9c63SKent Gibson 
14803c0d9c63SKent Gibson static int linereq_release(struct inode *inode, struct file *file)
14813c0d9c63SKent Gibson {
14823c0d9c63SKent Gibson 	struct linereq *lr = file->private_data;
14833c0d9c63SKent Gibson 
14843c0d9c63SKent Gibson 	linereq_free(lr);
14853c0d9c63SKent Gibson 	return 0;
14863c0d9c63SKent Gibson }
14873c0d9c63SKent Gibson 
14883c0d9c63SKent Gibson static const struct file_operations line_fileops = {
14893c0d9c63SKent Gibson 	.release = linereq_release,
149073e03419SKent Gibson 	.read = linereq_read,
149173e03419SKent Gibson 	.poll = linereq_poll,
14923c0d9c63SKent Gibson 	.owner = THIS_MODULE,
14933c0d9c63SKent Gibson 	.llseek = noop_llseek,
14943c0d9c63SKent Gibson 	.unlocked_ioctl = linereq_ioctl,
14953c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
14963c0d9c63SKent Gibson 	.compat_ioctl = linereq_ioctl_compat,
14973c0d9c63SKent Gibson #endif
14983c0d9c63SKent Gibson };
14993c0d9c63SKent Gibson 
15003c0d9c63SKent Gibson static int linereq_create(struct gpio_device *gdev, void __user *ip)
15013c0d9c63SKent Gibson {
15023c0d9c63SKent Gibson 	struct gpio_v2_line_request ulr;
15033c0d9c63SKent Gibson 	struct gpio_v2_line_config *lc;
15043c0d9c63SKent Gibson 	struct linereq *lr;
15053c0d9c63SKent Gibson 	struct file *file;
15063c0d9c63SKent Gibson 	u64 flags;
15073c0d9c63SKent Gibson 	unsigned int i;
15083c0d9c63SKent Gibson 	int fd, ret;
15093c0d9c63SKent Gibson 
15103c0d9c63SKent Gibson 	if (copy_from_user(&ulr, ip, sizeof(ulr)))
15113c0d9c63SKent Gibson 		return -EFAULT;
15123c0d9c63SKent Gibson 
15133c0d9c63SKent Gibson 	if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
15143c0d9c63SKent Gibson 		return -EINVAL;
15153c0d9c63SKent Gibson 
15163c0d9c63SKent Gibson 	if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
15173c0d9c63SKent Gibson 		return -EINVAL;
15183c0d9c63SKent Gibson 
15193c0d9c63SKent Gibson 	lc = &ulr.config;
15203c0d9c63SKent Gibson 	ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
15213c0d9c63SKent Gibson 	if (ret)
15223c0d9c63SKent Gibson 		return ret;
15233c0d9c63SKent Gibson 
15243c0d9c63SKent Gibson 	lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
15253c0d9c63SKent Gibson 	if (!lr)
15263c0d9c63SKent Gibson 		return -ENOMEM;
15273c0d9c63SKent Gibson 
15283c0d9c63SKent Gibson 	lr->gdev = gdev;
15293c0d9c63SKent Gibson 	get_device(&gdev->dev);
15303c0d9c63SKent Gibson 
153165cff704SKent Gibson 	for (i = 0; i < ulr.num_lines; i++) {
153273e03419SKent Gibson 		lr->lines[i].req = lr;
153365cff704SKent Gibson 		WRITE_ONCE(lr->lines[i].sw_debounced, 0);
153465cff704SKent Gibson 		INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
153565cff704SKent Gibson 	}
153673e03419SKent Gibson 
1537f188ac12SKent Gibson 	if (ulr.consumer[0] != '\0') {
15383c0d9c63SKent Gibson 		/* label is only initialized if consumer is set */
1539f188ac12SKent Gibson 		lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1540f188ac12SKent Gibson 				     GFP_KERNEL);
15413c0d9c63SKent Gibson 		if (!lr->label) {
15423c0d9c63SKent Gibson 			ret = -ENOMEM;
15433c0d9c63SKent Gibson 			goto out_free_linereq;
15443c0d9c63SKent Gibson 		}
15453c0d9c63SKent Gibson 	}
15463c0d9c63SKent Gibson 
1547a54756cbSKent Gibson 	mutex_init(&lr->config_mutex);
154873e03419SKent Gibson 	init_waitqueue_head(&lr->wait);
154973e03419SKent Gibson 	lr->event_buffer_size = ulr.event_buffer_size;
155073e03419SKent Gibson 	if (lr->event_buffer_size == 0)
155173e03419SKent Gibson 		lr->event_buffer_size = ulr.num_lines * 16;
155273e03419SKent Gibson 	else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
155373e03419SKent Gibson 		lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
155473e03419SKent Gibson 
155573e03419SKent Gibson 	atomic_set(&lr->seqno, 0);
15563c0d9c63SKent Gibson 	lr->num_lines = ulr.num_lines;
15573c0d9c63SKent Gibson 
15583c0d9c63SKent Gibson 	/* Request each GPIO */
15593c0d9c63SKent Gibson 	for (i = 0; i < ulr.num_lines; i++) {
15603c0d9c63SKent Gibson 		u32 offset = ulr.offsets[i];
15613c0d9c63SKent Gibson 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
15623c0d9c63SKent Gibson 
15633c0d9c63SKent Gibson 		if (IS_ERR(desc)) {
15643c0d9c63SKent Gibson 			ret = PTR_ERR(desc);
15653c0d9c63SKent Gibson 			goto out_free_linereq;
15663c0d9c63SKent Gibson 		}
15673c0d9c63SKent Gibson 
156895a4eed7SAndy Shevchenko 		ret = gpiod_request_user(desc, lr->label);
15693c0d9c63SKent Gibson 		if (ret)
15703c0d9c63SKent Gibson 			goto out_free_linereq;
15713c0d9c63SKent Gibson 
15723c0d9c63SKent Gibson 		lr->lines[i].desc = desc;
15733c0d9c63SKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
15743c0d9c63SKent Gibson 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
15753c0d9c63SKent Gibson 
15763c0d9c63SKent Gibson 		ret = gpiod_set_transitory(desc, false);
15773c0d9c63SKent Gibson 		if (ret < 0)
15783c0d9c63SKent Gibson 			goto out_free_linereq;
15793c0d9c63SKent Gibson 
15803c0d9c63SKent Gibson 		/*
15813c0d9c63SKent Gibson 		 * Lines have to be requested explicitly for input
15823c0d9c63SKent Gibson 		 * or output, else the line will be treated "as is".
15833c0d9c63SKent Gibson 		 */
15843c0d9c63SKent Gibson 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
15853c0d9c63SKent Gibson 			int val = gpio_v2_line_config_output_value(lc, i);
15863c0d9c63SKent Gibson 
15873c0d9c63SKent Gibson 			ret = gpiod_direction_output(desc, val);
15883c0d9c63SKent Gibson 			if (ret)
15893c0d9c63SKent Gibson 				goto out_free_linereq;
15903c0d9c63SKent Gibson 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
15913c0d9c63SKent Gibson 			ret = gpiod_direction_input(desc);
15923c0d9c63SKent Gibson 			if (ret)
15933c0d9c63SKent Gibson 				goto out_free_linereq;
159473e03419SKent Gibson 
159565cff704SKent Gibson 			ret = edge_detector_setup(&lr->lines[i], lc, i,
15962068339aSDipen Patel 				flags & GPIO_V2_LINE_EDGE_FLAGS,
15972068339aSDipen Patel 				flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
159873e03419SKent Gibson 			if (ret)
159973e03419SKent Gibson 				goto out_free_linereq;
16003c0d9c63SKent Gibson 		}
16013c0d9c63SKent Gibson 
16023c0d9c63SKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
1603aad95584SKent Gibson 					     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
16043c0d9c63SKent Gibson 
16053c0d9c63SKent Gibson 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
16063c0d9c63SKent Gibson 			offset);
16073c0d9c63SKent Gibson 	}
16083c0d9c63SKent Gibson 
16093c0d9c63SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
16103c0d9c63SKent Gibson 	if (fd < 0) {
16113c0d9c63SKent Gibson 		ret = fd;
16123c0d9c63SKent Gibson 		goto out_free_linereq;
16133c0d9c63SKent Gibson 	}
16143c0d9c63SKent Gibson 
16153c0d9c63SKent Gibson 	file = anon_inode_getfile("gpio-line", &line_fileops, lr,
16163c0d9c63SKent Gibson 				  O_RDONLY | O_CLOEXEC);
16173c0d9c63SKent Gibson 	if (IS_ERR(file)) {
16183c0d9c63SKent Gibson 		ret = PTR_ERR(file);
16193c0d9c63SKent Gibson 		goto out_put_unused_fd;
16203c0d9c63SKent Gibson 	}
16213c0d9c63SKent Gibson 
16223c0d9c63SKent Gibson 	ulr.fd = fd;
16233c0d9c63SKent Gibson 	if (copy_to_user(ip, &ulr, sizeof(ulr))) {
16243c0d9c63SKent Gibson 		/*
16253c0d9c63SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
16263c0d9c63SKent Gibson 		 * the regular error cleanup path here.
16273c0d9c63SKent Gibson 		 */
16283c0d9c63SKent Gibson 		fput(file);
16293c0d9c63SKent Gibson 		put_unused_fd(fd);
16303c0d9c63SKent Gibson 		return -EFAULT;
16313c0d9c63SKent Gibson 	}
16323c0d9c63SKent Gibson 
16333c0d9c63SKent Gibson 	fd_install(fd, file);
16343c0d9c63SKent Gibson 
16353c0d9c63SKent Gibson 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
16363c0d9c63SKent Gibson 		lr->num_lines);
16373c0d9c63SKent Gibson 
16383c0d9c63SKent Gibson 	return 0;
16393c0d9c63SKent Gibson 
16403c0d9c63SKent Gibson out_put_unused_fd:
16413c0d9c63SKent Gibson 	put_unused_fd(fd);
16423c0d9c63SKent Gibson out_free_linereq:
16433c0d9c63SKent Gibson 	linereq_free(lr);
16443c0d9c63SKent Gibson 	return ret;
16453c0d9c63SKent Gibson }
16463c0d9c63SKent Gibson 
16473c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1648925ca369SKent Gibson 
1649925ca369SKent Gibson /*
1650925ca369SKent Gibson  * GPIO line event management
1651925ca369SKent Gibson  */
1652925ca369SKent Gibson 
1653925ca369SKent Gibson /**
1654925ca369SKent Gibson  * struct lineevent_state - contains the state of a userspace event
1655925ca369SKent Gibson  * @gdev: the GPIO device the event pertains to
1656925ca369SKent Gibson  * @label: consumer label used to tag descriptors
1657925ca369SKent Gibson  * @desc: the GPIO descriptor held by this event
1658925ca369SKent Gibson  * @eflags: the event flags this line was requested with
1659925ca369SKent Gibson  * @irq: the interrupt that trigger in response to events on this GPIO
1660925ca369SKent Gibson  * @wait: wait queue that handles blocking reads of events
1661925ca369SKent Gibson  * @events: KFIFO for the GPIO events
1662925ca369SKent Gibson  * @timestamp: cache for the timestamp storing it between hardirq
1663925ca369SKent Gibson  * and IRQ thread, used to bring the timestamp close to the actual
1664925ca369SKent Gibson  * event
1665925ca369SKent Gibson  */
1666925ca369SKent Gibson struct lineevent_state {
1667925ca369SKent Gibson 	struct gpio_device *gdev;
1668925ca369SKent Gibson 	const char *label;
1669925ca369SKent Gibson 	struct gpio_desc *desc;
1670925ca369SKent Gibson 	u32 eflags;
1671925ca369SKent Gibson 	int irq;
1672925ca369SKent Gibson 	wait_queue_head_t wait;
1673925ca369SKent Gibson 	DECLARE_KFIFO(events, struct gpioevent_data, 16);
1674925ca369SKent Gibson 	u64 timestamp;
1675925ca369SKent Gibson };
1676925ca369SKent Gibson 
1677925ca369SKent Gibson #define GPIOEVENT_REQUEST_VALID_FLAGS \
1678925ca369SKent Gibson 	(GPIOEVENT_REQUEST_RISING_EDGE | \
1679925ca369SKent Gibson 	GPIOEVENT_REQUEST_FALLING_EDGE)
1680925ca369SKent Gibson 
168149bc5279SKent Gibson static __poll_t lineevent_poll(struct file *file,
1682925ca369SKent Gibson 			       struct poll_table_struct *wait)
1683925ca369SKent Gibson {
168449bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1685925ca369SKent Gibson 	__poll_t events = 0;
1686925ca369SKent Gibson 
168749bc5279SKent Gibson 	poll_wait(file, &le->wait, wait);
1688925ca369SKent Gibson 
1689925ca369SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1690925ca369SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
1691925ca369SKent Gibson 
1692925ca369SKent Gibson 	return events;
1693925ca369SKent Gibson }
1694925ca369SKent Gibson 
16955ad284abSAndy Shevchenko struct compat_gpioeevent_data {
16965ad284abSAndy Shevchenko 	compat_u64	timestamp;
16975ad284abSAndy Shevchenko 	u32		id;
16985ad284abSAndy Shevchenko };
16995ad284abSAndy Shevchenko 
170049bc5279SKent Gibson static ssize_t lineevent_read(struct file *file,
1701925ca369SKent Gibson 			      char __user *buf,
1702925ca369SKent Gibson 			      size_t count,
1703925ca369SKent Gibson 			      loff_t *f_ps)
1704925ca369SKent Gibson {
170549bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1706925ca369SKent Gibson 	struct gpioevent_data ge;
1707925ca369SKent Gibson 	ssize_t bytes_read = 0;
17085ad284abSAndy Shevchenko 	ssize_t ge_size;
1709925ca369SKent Gibson 	int ret;
1710925ca369SKent Gibson 
17115ad284abSAndy Shevchenko 	/*
17125ad284abSAndy Shevchenko 	 * When compatible system call is being used the struct gpioevent_data,
17135ad284abSAndy Shevchenko 	 * in case of at least ia32, has different size due to the alignment
17145ad284abSAndy Shevchenko 	 * differences. Because we have first member 64 bits followed by one of
17155ad284abSAndy Shevchenko 	 * 32 bits there is no gap between them. The only difference is the
17165ad284abSAndy Shevchenko 	 * padding at the end of the data structure. Hence, we calculate the
17175ad284abSAndy Shevchenko 	 * actual sizeof() and pass this as an argument to copy_to_user() to
17185ad284abSAndy Shevchenko 	 * drop unneeded bytes from the output.
17195ad284abSAndy Shevchenko 	 */
1720163d1719SAndy Shevchenko 	if (compat_need_64bit_alignment_fixup())
1721163d1719SAndy Shevchenko 		ge_size = sizeof(struct compat_gpioeevent_data);
1722163d1719SAndy Shevchenko 	else
1723163d1719SAndy Shevchenko 		ge_size = sizeof(struct gpioevent_data);
17245ad284abSAndy Shevchenko 	if (count < ge_size)
1725925ca369SKent Gibson 		return -EINVAL;
1726925ca369SKent Gibson 
1727925ca369SKent Gibson 	do {
1728925ca369SKent Gibson 		spin_lock(&le->wait.lock);
1729925ca369SKent Gibson 		if (kfifo_is_empty(&le->events)) {
1730925ca369SKent Gibson 			if (bytes_read) {
1731925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1732925ca369SKent Gibson 				return bytes_read;
1733925ca369SKent Gibson 			}
1734925ca369SKent Gibson 
173549bc5279SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
1736925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1737925ca369SKent Gibson 				return -EAGAIN;
1738925ca369SKent Gibson 			}
1739925ca369SKent Gibson 
1740925ca369SKent Gibson 			ret = wait_event_interruptible_locked(le->wait,
1741925ca369SKent Gibson 					!kfifo_is_empty(&le->events));
1742925ca369SKent Gibson 			if (ret) {
1743925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1744925ca369SKent Gibson 				return ret;
1745925ca369SKent Gibson 			}
1746925ca369SKent Gibson 		}
1747925ca369SKent Gibson 
1748925ca369SKent Gibson 		ret = kfifo_out(&le->events, &ge, 1);
1749925ca369SKent Gibson 		spin_unlock(&le->wait.lock);
1750925ca369SKent Gibson 		if (ret != 1) {
1751925ca369SKent Gibson 			/*
1752925ca369SKent Gibson 			 * This should never happen - we were holding the lock
1753925ca369SKent Gibson 			 * from the moment we learned the fifo is no longer
1754925ca369SKent Gibson 			 * empty until now.
1755925ca369SKent Gibson 			 */
1756925ca369SKent Gibson 			ret = -EIO;
1757925ca369SKent Gibson 			break;
1758925ca369SKent Gibson 		}
1759925ca369SKent Gibson 
17605ad284abSAndy Shevchenko 		if (copy_to_user(buf + bytes_read, &ge, ge_size))
1761925ca369SKent Gibson 			return -EFAULT;
17625ad284abSAndy Shevchenko 		bytes_read += ge_size;
17635ad284abSAndy Shevchenko 	} while (count >= bytes_read + ge_size);
1764925ca369SKent Gibson 
1765925ca369SKent Gibson 	return bytes_read;
1766925ca369SKent Gibson }
1767925ca369SKent Gibson 
176846824272SKent Gibson static void lineevent_free(struct lineevent_state *le)
1769925ca369SKent Gibson {
177046824272SKent Gibson 	if (le->irq)
1771925ca369SKent Gibson 		free_irq(le->irq, le);
177246824272SKent Gibson 	if (le->desc)
1773925ca369SKent Gibson 		gpiod_free(le->desc);
1774925ca369SKent Gibson 	kfree(le->label);
177546824272SKent Gibson 	put_device(&le->gdev->dev);
1776925ca369SKent Gibson 	kfree(le);
177746824272SKent Gibson }
177846824272SKent Gibson 
177946824272SKent Gibson static int lineevent_release(struct inode *inode, struct file *file)
178046824272SKent Gibson {
178146824272SKent Gibson 	lineevent_free(file->private_data);
1782925ca369SKent Gibson 	return 0;
1783925ca369SKent Gibson }
1784925ca369SKent Gibson 
178549bc5279SKent Gibson static long lineevent_ioctl(struct file *file, unsigned int cmd,
1786925ca369SKent Gibson 			    unsigned long arg)
1787925ca369SKent Gibson {
178849bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1789925ca369SKent Gibson 	void __user *ip = (void __user *)arg;
1790925ca369SKent Gibson 	struct gpiohandle_data ghd;
1791925ca369SKent Gibson 
1792925ca369SKent Gibson 	/*
1793925ca369SKent Gibson 	 * We can get the value for an event line but not set it,
1794925ca369SKent Gibson 	 * because it is input by definition.
1795925ca369SKent Gibson 	 */
1796925ca369SKent Gibson 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
1797925ca369SKent Gibson 		int val;
1798925ca369SKent Gibson 
1799925ca369SKent Gibson 		memset(&ghd, 0, sizeof(ghd));
1800925ca369SKent Gibson 
1801925ca369SKent Gibson 		val = gpiod_get_value_cansleep(le->desc);
1802925ca369SKent Gibson 		if (val < 0)
1803925ca369SKent Gibson 			return val;
1804925ca369SKent Gibson 		ghd.values[0] = val;
1805925ca369SKent Gibson 
1806925ca369SKent Gibson 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
1807925ca369SKent Gibson 			return -EFAULT;
1808925ca369SKent Gibson 
1809925ca369SKent Gibson 		return 0;
1810925ca369SKent Gibson 	}
1811925ca369SKent Gibson 	return -EINVAL;
1812925ca369SKent Gibson }
1813925ca369SKent Gibson 
1814925ca369SKent Gibson #ifdef CONFIG_COMPAT
181549bc5279SKent Gibson static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
1816925ca369SKent Gibson 				   unsigned long arg)
1817925ca369SKent Gibson {
181849bc5279SKent Gibson 	return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1819925ca369SKent Gibson }
1820925ca369SKent Gibson #endif
1821925ca369SKent Gibson 
1822925ca369SKent Gibson static const struct file_operations lineevent_fileops = {
1823925ca369SKent Gibson 	.release = lineevent_release,
1824925ca369SKent Gibson 	.read = lineevent_read,
1825925ca369SKent Gibson 	.poll = lineevent_poll,
1826925ca369SKent Gibson 	.owner = THIS_MODULE,
1827925ca369SKent Gibson 	.llseek = noop_llseek,
1828925ca369SKent Gibson 	.unlocked_ioctl = lineevent_ioctl,
1829925ca369SKent Gibson #ifdef CONFIG_COMPAT
1830925ca369SKent Gibson 	.compat_ioctl = lineevent_ioctl_compat,
1831925ca369SKent Gibson #endif
1832925ca369SKent Gibson };
1833925ca369SKent Gibson 
1834925ca369SKent Gibson static irqreturn_t lineevent_irq_thread(int irq, void *p)
1835925ca369SKent Gibson {
1836925ca369SKent Gibson 	struct lineevent_state *le = p;
1837925ca369SKent Gibson 	struct gpioevent_data ge;
1838925ca369SKent Gibson 	int ret;
1839925ca369SKent Gibson 
1840925ca369SKent Gibson 	/* Do not leak kernel stack to userspace */
1841925ca369SKent Gibson 	memset(&ge, 0, sizeof(ge));
1842925ca369SKent Gibson 
1843925ca369SKent Gibson 	/*
1844925ca369SKent Gibson 	 * We may be running from a nested threaded interrupt in which case
1845925ca369SKent Gibson 	 * we didn't get the timestamp from lineevent_irq_handler().
1846925ca369SKent Gibson 	 */
1847925ca369SKent Gibson 	if (!le->timestamp)
1848925ca369SKent Gibson 		ge.timestamp = ktime_get_ns();
1849925ca369SKent Gibson 	else
1850925ca369SKent Gibson 		ge.timestamp = le->timestamp;
1851925ca369SKent Gibson 
1852925ca369SKent Gibson 	if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
1853925ca369SKent Gibson 	    && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1854925ca369SKent Gibson 		int level = gpiod_get_value_cansleep(le->desc);
1855925ca369SKent Gibson 
1856925ca369SKent Gibson 		if (level)
1857925ca369SKent Gibson 			/* Emit low-to-high event */
1858925ca369SKent Gibson 			ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1859925ca369SKent Gibson 		else
1860925ca369SKent Gibson 			/* Emit high-to-low event */
1861925ca369SKent Gibson 			ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1862925ca369SKent Gibson 	} else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
1863925ca369SKent Gibson 		/* Emit low-to-high event */
1864925ca369SKent Gibson 		ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1865925ca369SKent Gibson 	} else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1866925ca369SKent Gibson 		/* Emit high-to-low event */
1867925ca369SKent Gibson 		ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1868925ca369SKent Gibson 	} else {
1869925ca369SKent Gibson 		return IRQ_NONE;
1870925ca369SKent Gibson 	}
1871925ca369SKent Gibson 
1872925ca369SKent Gibson 	ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
1873925ca369SKent Gibson 					    1, &le->wait.lock);
1874925ca369SKent Gibson 	if (ret)
1875925ca369SKent Gibson 		wake_up_poll(&le->wait, EPOLLIN);
1876925ca369SKent Gibson 	else
1877925ca369SKent Gibson 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
1878925ca369SKent Gibson 
1879925ca369SKent Gibson 	return IRQ_HANDLED;
1880925ca369SKent Gibson }
1881925ca369SKent Gibson 
1882925ca369SKent Gibson static irqreturn_t lineevent_irq_handler(int irq, void *p)
1883925ca369SKent Gibson {
1884925ca369SKent Gibson 	struct lineevent_state *le = p;
1885925ca369SKent Gibson 
1886925ca369SKent Gibson 	/*
1887925ca369SKent Gibson 	 * Just store the timestamp in hardirq context so we get it as
1888925ca369SKent Gibson 	 * close in time as possible to the actual event.
1889925ca369SKent Gibson 	 */
1890925ca369SKent Gibson 	le->timestamp = ktime_get_ns();
1891925ca369SKent Gibson 
1892925ca369SKent Gibson 	return IRQ_WAKE_THREAD;
1893925ca369SKent Gibson }
1894925ca369SKent Gibson 
1895925ca369SKent Gibson static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1896925ca369SKent Gibson {
1897925ca369SKent Gibson 	struct gpioevent_request eventreq;
1898925ca369SKent Gibson 	struct lineevent_state *le;
1899925ca369SKent Gibson 	struct gpio_desc *desc;
1900925ca369SKent Gibson 	struct file *file;
1901925ca369SKent Gibson 	u32 offset;
1902925ca369SKent Gibson 	u32 lflags;
1903925ca369SKent Gibson 	u32 eflags;
1904925ca369SKent Gibson 	int fd;
1905925ca369SKent Gibson 	int ret;
190646824272SKent Gibson 	int irq, irqflags = 0;
1907925ca369SKent Gibson 
1908925ca369SKent Gibson 	if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1909925ca369SKent Gibson 		return -EFAULT;
1910925ca369SKent Gibson 
1911925ca369SKent Gibson 	offset = eventreq.lineoffset;
1912925ca369SKent Gibson 	lflags = eventreq.handleflags;
1913925ca369SKent Gibson 	eflags = eventreq.eventflags;
1914925ca369SKent Gibson 
1915925ca369SKent Gibson 	desc = gpiochip_get_desc(gdev->chip, offset);
1916925ca369SKent Gibson 	if (IS_ERR(desc))
1917925ca369SKent Gibson 		return PTR_ERR(desc);
1918925ca369SKent Gibson 
1919925ca369SKent Gibson 	/* Return an error if a unknown flag is set */
1920925ca369SKent Gibson 	if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1921925ca369SKent Gibson 	    (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1922925ca369SKent Gibson 		return -EINVAL;
1923925ca369SKent Gibson 
1924925ca369SKent Gibson 	/* This is just wrong: we don't look for events on output lines */
1925925ca369SKent Gibson 	if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1926925ca369SKent Gibson 	    (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1927925ca369SKent Gibson 	    (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1928925ca369SKent Gibson 		return -EINVAL;
1929925ca369SKent Gibson 
1930925ca369SKent Gibson 	/* Only one bias flag can be set. */
1931925ca369SKent Gibson 	if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1932925ca369SKent Gibson 	     (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1933925ca369SKent Gibson 			GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1934925ca369SKent Gibson 	    ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1935925ca369SKent Gibson 	     (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1936925ca369SKent Gibson 		return -EINVAL;
1937925ca369SKent Gibson 
1938925ca369SKent Gibson 	le = kzalloc(sizeof(*le), GFP_KERNEL);
1939925ca369SKent Gibson 	if (!le)
1940925ca369SKent Gibson 		return -ENOMEM;
1941925ca369SKent Gibson 	le->gdev = gdev;
1942925ca369SKent Gibson 	get_device(&gdev->dev);
1943925ca369SKent Gibson 
1944f188ac12SKent Gibson 	if (eventreq.consumer_label[0] != '\0') {
1945f188ac12SKent Gibson 		/* label is only initialized if consumer_label is set */
1946f188ac12SKent Gibson 		le->label = kstrndup(eventreq.consumer_label,
1947f188ac12SKent Gibson 				     sizeof(eventreq.consumer_label) - 1,
1948925ca369SKent Gibson 				     GFP_KERNEL);
1949925ca369SKent Gibson 		if (!le->label) {
1950925ca369SKent Gibson 			ret = -ENOMEM;
1951925ca369SKent Gibson 			goto out_free_le;
1952925ca369SKent Gibson 		}
1953925ca369SKent Gibson 	}
1954925ca369SKent Gibson 
195595a4eed7SAndy Shevchenko 	ret = gpiod_request_user(desc, le->label);
1956925ca369SKent Gibson 	if (ret)
195746824272SKent Gibson 		goto out_free_le;
1958925ca369SKent Gibson 	le->desc = desc;
1959925ca369SKent Gibson 	le->eflags = eflags;
1960925ca369SKent Gibson 
1961c274b58aSKent Gibson 	linehandle_flags_to_desc_flags(lflags, &desc->flags);
1962925ca369SKent Gibson 
1963925ca369SKent Gibson 	ret = gpiod_direction_input(desc);
1964925ca369SKent Gibson 	if (ret)
196546824272SKent Gibson 		goto out_free_le;
1966925ca369SKent Gibson 
19676accc376SKent Gibson 	blocking_notifier_call_chain(&desc->gdev->notifier,
1968aad95584SKent Gibson 				     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1969925ca369SKent Gibson 
197046824272SKent Gibson 	irq = gpiod_to_irq(desc);
197146824272SKent Gibson 	if (irq <= 0) {
1972925ca369SKent Gibson 		ret = -ENODEV;
197346824272SKent Gibson 		goto out_free_le;
1974925ca369SKent Gibson 	}
197546824272SKent Gibson 	le->irq = irq;
1976925ca369SKent Gibson 
1977925ca369SKent Gibson 	if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
1978925ca369SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1979925ca369SKent Gibson 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1980925ca369SKent Gibson 	if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
1981925ca369SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1982925ca369SKent Gibson 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1983925ca369SKent Gibson 	irqflags |= IRQF_ONESHOT;
1984925ca369SKent Gibson 
1985925ca369SKent Gibson 	INIT_KFIFO(le->events);
1986925ca369SKent Gibson 	init_waitqueue_head(&le->wait);
1987925ca369SKent Gibson 
1988925ca369SKent Gibson 	/* Request a thread to read the events */
1989925ca369SKent Gibson 	ret = request_threaded_irq(le->irq,
1990925ca369SKent Gibson 				   lineevent_irq_handler,
1991925ca369SKent Gibson 				   lineevent_irq_thread,
1992925ca369SKent Gibson 				   irqflags,
1993925ca369SKent Gibson 				   le->label,
1994925ca369SKent Gibson 				   le);
1995925ca369SKent Gibson 	if (ret)
199646824272SKent Gibson 		goto out_free_le;
1997925ca369SKent Gibson 
1998925ca369SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1999925ca369SKent Gibson 	if (fd < 0) {
2000925ca369SKent Gibson 		ret = fd;
200146824272SKent Gibson 		goto out_free_le;
2002925ca369SKent Gibson 	}
2003925ca369SKent Gibson 
2004925ca369SKent Gibson 	file = anon_inode_getfile("gpio-event",
2005925ca369SKent Gibson 				  &lineevent_fileops,
2006925ca369SKent Gibson 				  le,
2007925ca369SKent Gibson 				  O_RDONLY | O_CLOEXEC);
2008925ca369SKent Gibson 	if (IS_ERR(file)) {
2009925ca369SKent Gibson 		ret = PTR_ERR(file);
2010925ca369SKent Gibson 		goto out_put_unused_fd;
2011925ca369SKent Gibson 	}
2012925ca369SKent Gibson 
2013925ca369SKent Gibson 	eventreq.fd = fd;
2014925ca369SKent Gibson 	if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
2015925ca369SKent Gibson 		/*
2016925ca369SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
2017925ca369SKent Gibson 		 * the regular error cleanup path here.
2018925ca369SKent Gibson 		 */
2019925ca369SKent Gibson 		fput(file);
2020925ca369SKent Gibson 		put_unused_fd(fd);
2021925ca369SKent Gibson 		return -EFAULT;
2022925ca369SKent Gibson 	}
2023925ca369SKent Gibson 
2024925ca369SKent Gibson 	fd_install(fd, file);
2025925ca369SKent Gibson 
2026925ca369SKent Gibson 	return 0;
2027925ca369SKent Gibson 
2028925ca369SKent Gibson out_put_unused_fd:
2029925ca369SKent Gibson 	put_unused_fd(fd);
2030925ca369SKent Gibson out_free_le:
203146824272SKent Gibson 	lineevent_free(le);
2032925ca369SKent Gibson 	return ret;
2033925ca369SKent Gibson }
2034925ca369SKent Gibson 
2035aad95584SKent Gibson static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
2036aad95584SKent Gibson 				    struct gpioline_info *info_v1)
2037aad95584SKent Gibson {
2038aad95584SKent Gibson 	u64 flagsv2 = info_v2->flags;
2039aad95584SKent Gibson 
2040aad95584SKent Gibson 	memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
2041aad95584SKent Gibson 	memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
2042aad95584SKent Gibson 	info_v1->line_offset = info_v2->offset;
2043aad95584SKent Gibson 	info_v1->flags = 0;
2044aad95584SKent Gibson 
2045aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
2046aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_KERNEL;
2047aad95584SKent Gibson 
2048aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
2049aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
2050aad95584SKent Gibson 
2051aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
2052aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
2053aad95584SKent Gibson 
2054aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
2055aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
2056aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
2057aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
2058aad95584SKent Gibson 
2059aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
2060aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
2061aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
2062aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
2063aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
2064aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
2065aad95584SKent Gibson }
2066aad95584SKent Gibson 
2067aad95584SKent Gibson static void gpio_v2_line_info_changed_to_v1(
2068aad95584SKent Gibson 		struct gpio_v2_line_info_changed *lic_v2,
2069aad95584SKent Gibson 		struct gpioline_info_changed *lic_v1)
2070aad95584SKent Gibson {
2071cb8f63b8SGabriel Knezek 	memset(lic_v1, 0, sizeof(*lic_v1));
2072aad95584SKent Gibson 	gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
2073aad95584SKent Gibson 	lic_v1->timestamp = lic_v2->timestamp_ns;
2074aad95584SKent Gibson 	lic_v1->event_type = lic_v2->event_type;
2075aad95584SKent Gibson }
2076aad95584SKent Gibson 
20773c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
20783c0d9c63SKent Gibson 
2079925ca369SKent Gibson static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
2080aad95584SKent Gibson 				  struct gpio_v2_line_info *info)
2081925ca369SKent Gibson {
2082925ca369SKent Gibson 	struct gpio_chip *gc = desc->gdev->chip;
2083925ca369SKent Gibson 	bool ok_for_pinctrl;
2084925ca369SKent Gibson 	unsigned long flags;
208565cff704SKent Gibson 	u32 debounce_period_us;
208665cff704SKent Gibson 	unsigned int num_attrs = 0;
2087925ca369SKent Gibson 
208869e4e136SKent Gibson 	memset(info, 0, sizeof(*info));
2089aad95584SKent Gibson 	info->offset = gpio_chip_hwgpio(desc);
2090925ca369SKent Gibson 
2091925ca369SKent Gibson 	/*
2092925ca369SKent Gibson 	 * This function takes a mutex so we must check this before taking
2093925ca369SKent Gibson 	 * the spinlock.
2094925ca369SKent Gibson 	 *
2095925ca369SKent Gibson 	 * FIXME: find a non-racy way to retrieve this information. Maybe a
2096925ca369SKent Gibson 	 * lock common to both frameworks?
2097925ca369SKent Gibson 	 */
2098925ca369SKent Gibson 	ok_for_pinctrl =
2099aad95584SKent Gibson 		pinctrl_gpio_can_use_line(gc->base + info->offset);
2100925ca369SKent Gibson 
2101925ca369SKent Gibson 	spin_lock_irqsave(&gpio_lock, flags);
2102925ca369SKent Gibson 
210369e4e136SKent Gibson 	if (desc->name)
210469e4e136SKent Gibson 		strscpy(info->name, desc->name, sizeof(info->name));
2105925ca369SKent Gibson 
210669e4e136SKent Gibson 	if (desc->label)
210769e4e136SKent Gibson 		strscpy(info->consumer, desc->label, sizeof(info->consumer));
2108925ca369SKent Gibson 
2109925ca369SKent Gibson 	/*
2110925ca369SKent Gibson 	 * Userspace only need to know that the kernel is using this GPIO so
2111925ca369SKent Gibson 	 * it can't use it.
2112925ca369SKent Gibson 	 */
2113925ca369SKent Gibson 	info->flags = 0;
2114925ca369SKent Gibson 	if (test_bit(FLAG_REQUESTED, &desc->flags) ||
2115925ca369SKent Gibson 	    test_bit(FLAG_IS_HOGGED, &desc->flags) ||
2116925ca369SKent Gibson 	    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
2117925ca369SKent Gibson 	    test_bit(FLAG_EXPORT, &desc->flags) ||
2118925ca369SKent Gibson 	    test_bit(FLAG_SYSFS, &desc->flags) ||
2119a0db197fSMarc Zyngier 	    !gpiochip_line_is_valid(gc, info->offset) ||
2120925ca369SKent Gibson 	    !ok_for_pinctrl)
2121aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_USED;
2122aad95584SKent Gibson 
2123925ca369SKent Gibson 	if (test_bit(FLAG_IS_OUT, &desc->flags))
2124aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
2125aad95584SKent Gibson 	else
2126aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_INPUT;
2127aad95584SKent Gibson 
2128925ca369SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2129aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
2130aad95584SKent Gibson 
2131925ca369SKent Gibson 	if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2132aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
2133925ca369SKent Gibson 	if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2134aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
2135aad95584SKent Gibson 
2136925ca369SKent Gibson 	if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2137aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
2138925ca369SKent Gibson 	if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2139aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
2140925ca369SKent Gibson 	if (test_bit(FLAG_PULL_UP, &desc->flags))
2141aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
2142925ca369SKent Gibson 
214373e03419SKent Gibson 	if (test_bit(FLAG_EDGE_RISING, &desc->flags))
214473e03419SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
214573e03419SKent Gibson 	if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
214673e03419SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
214773e03419SKent Gibson 
214826d060e4SKent Gibson 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
214926d060e4SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
21502068339aSDipen Patel 	else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags))
21512068339aSDipen Patel 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
215226d060e4SKent Gibson 
215365cff704SKent Gibson 	debounce_period_us = READ_ONCE(desc->debounce_period_us);
215465cff704SKent Gibson 	if (debounce_period_us) {
215565cff704SKent Gibson 		info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
215665cff704SKent Gibson 		info->attrs[num_attrs].debounce_period_us = debounce_period_us;
215765cff704SKent Gibson 		num_attrs++;
215865cff704SKent Gibson 	}
215965cff704SKent Gibson 	info->num_attrs = num_attrs;
2160925ca369SKent Gibson 
2161925ca369SKent Gibson 	spin_unlock_irqrestore(&gpio_lock, flags);
2162925ca369SKent Gibson }
2163925ca369SKent Gibson 
2164925ca369SKent Gibson struct gpio_chardev_data {
2165925ca369SKent Gibson 	struct gpio_device *gdev;
2166925ca369SKent Gibson 	wait_queue_head_t wait;
2167aad95584SKent Gibson 	DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
2168925ca369SKent Gibson 	struct notifier_block lineinfo_changed_nb;
2169925ca369SKent Gibson 	unsigned long *watched_lines;
2170aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2171aad95584SKent Gibson 	atomic_t watch_abi_version;
2172aad95584SKent Gibson #endif
2173925ca369SKent Gibson };
2174925ca369SKent Gibson 
21752e202ad8SKent Gibson static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
21762e202ad8SKent Gibson {
21772e202ad8SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
21782e202ad8SKent Gibson 	struct gpiochip_info chipinfo;
21792e202ad8SKent Gibson 
21802e202ad8SKent Gibson 	memset(&chipinfo, 0, sizeof(chipinfo));
21812e202ad8SKent Gibson 
21822e202ad8SKent Gibson 	strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
21832e202ad8SKent Gibson 	strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
21842e202ad8SKent Gibson 	chipinfo.lines = gdev->ngpio;
21852e202ad8SKent Gibson 	if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
21862e202ad8SKent Gibson 		return -EFAULT;
21872e202ad8SKent Gibson 	return 0;
21882e202ad8SKent Gibson }
21892e202ad8SKent Gibson 
2190aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2191aad95584SKent Gibson /*
2192aad95584SKent Gibson  * returns 0 if the versions match, else the previously selected ABI version
2193aad95584SKent Gibson  */
2194aad95584SKent Gibson static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
2195aad95584SKent Gibson 				       unsigned int version)
2196aad95584SKent Gibson {
2197aad95584SKent Gibson 	int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
2198aad95584SKent Gibson 
2199aad95584SKent Gibson 	if (abiv == version)
2200aad95584SKent Gibson 		return 0;
2201aad95584SKent Gibson 
2202aad95584SKent Gibson 	return abiv;
2203aad95584SKent Gibson }
22042e202ad8SKent Gibson 
22052e202ad8SKent Gibson static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
22062e202ad8SKent Gibson 			   bool watch)
22072e202ad8SKent Gibson {
22082e202ad8SKent Gibson 	struct gpio_desc *desc;
22092e202ad8SKent Gibson 	struct gpioline_info lineinfo;
22102e202ad8SKent Gibson 	struct gpio_v2_line_info lineinfo_v2;
22112e202ad8SKent Gibson 
22122e202ad8SKent Gibson 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
22132e202ad8SKent Gibson 		return -EFAULT;
22142e202ad8SKent Gibson 
22152e202ad8SKent Gibson 	/* this doubles as a range check on line_offset */
22162e202ad8SKent Gibson 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset);
22172e202ad8SKent Gibson 	if (IS_ERR(desc))
22182e202ad8SKent Gibson 		return PTR_ERR(desc);
22192e202ad8SKent Gibson 
22202e202ad8SKent Gibson 	if (watch) {
22212e202ad8SKent Gibson 		if (lineinfo_ensure_abi_version(cdev, 1))
22222e202ad8SKent Gibson 			return -EPERM;
22232e202ad8SKent Gibson 
22242e202ad8SKent Gibson 		if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
22252e202ad8SKent Gibson 			return -EBUSY;
22262e202ad8SKent Gibson 	}
22272e202ad8SKent Gibson 
22282e202ad8SKent Gibson 	gpio_desc_to_lineinfo(desc, &lineinfo_v2);
22292e202ad8SKent Gibson 	gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
22302e202ad8SKent Gibson 
22312e202ad8SKent Gibson 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
22322e202ad8SKent Gibson 		if (watch)
22332e202ad8SKent Gibson 			clear_bit(lineinfo.line_offset, cdev->watched_lines);
22342e202ad8SKent Gibson 		return -EFAULT;
22352e202ad8SKent Gibson 	}
22362e202ad8SKent Gibson 
22372e202ad8SKent Gibson 	return 0;
22382e202ad8SKent Gibson }
2239aad95584SKent Gibson #endif
2240aad95584SKent Gibson 
2241aad95584SKent Gibson static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
2242aad95584SKent Gibson 			bool watch)
2243aad95584SKent Gibson {
2244aad95584SKent Gibson 	struct gpio_desc *desc;
2245aad95584SKent Gibson 	struct gpio_v2_line_info lineinfo;
2246aad95584SKent Gibson 
2247aad95584SKent Gibson 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2248aad95584SKent Gibson 		return -EFAULT;
2249aad95584SKent Gibson 
2250aad95584SKent Gibson 	if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2251aad95584SKent Gibson 		return -EINVAL;
2252aad95584SKent Gibson 
2253aad95584SKent Gibson 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2254aad95584SKent Gibson 	if (IS_ERR(desc))
2255aad95584SKent Gibson 		return PTR_ERR(desc);
2256aad95584SKent Gibson 
2257aad95584SKent Gibson 	if (watch) {
2258aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2259aad95584SKent Gibson 		if (lineinfo_ensure_abi_version(cdev, 2))
2260aad95584SKent Gibson 			return -EPERM;
2261aad95584SKent Gibson #endif
2262aad95584SKent Gibson 		if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2263aad95584SKent Gibson 			return -EBUSY;
2264aad95584SKent Gibson 	}
2265aad95584SKent Gibson 	gpio_desc_to_lineinfo(desc, &lineinfo);
2266aad95584SKent Gibson 
2267aad95584SKent Gibson 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2268aad95584SKent Gibson 		if (watch)
2269aad95584SKent Gibson 			clear_bit(lineinfo.offset, cdev->watched_lines);
2270aad95584SKent Gibson 		return -EFAULT;
2271aad95584SKent Gibson 	}
2272aad95584SKent Gibson 
2273aad95584SKent Gibson 	return 0;
2274aad95584SKent Gibson }
2275aad95584SKent Gibson 
22762e202ad8SKent Gibson static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
2277925ca369SKent Gibson {
2278925ca369SKent Gibson 	__u32 offset;
2279925ca369SKent Gibson 
2280925ca369SKent Gibson 	if (copy_from_user(&offset, ip, sizeof(offset)))
2281925ca369SKent Gibson 		return -EFAULT;
2282925ca369SKent Gibson 
22831bf7ba40SKent Gibson 	if (offset >= cdev->gdev->ngpio)
22841bf7ba40SKent Gibson 		return -EINVAL;
2285925ca369SKent Gibson 
22861bf7ba40SKent Gibson 	if (!test_and_clear_bit(offset, cdev->watched_lines))
2287925ca369SKent Gibson 		return -EBUSY;
2288925ca369SKent Gibson 
2289925ca369SKent Gibson 	return 0;
2290925ca369SKent Gibson }
22912e202ad8SKent Gibson 
22922e202ad8SKent Gibson /*
22932e202ad8SKent Gibson  * gpio_ioctl() - ioctl handler for the GPIO chardev
22942e202ad8SKent Gibson  */
22952e202ad8SKent Gibson static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
22962e202ad8SKent Gibson {
22972e202ad8SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
22982e202ad8SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
22992e202ad8SKent Gibson 	void __user *ip = (void __user *)arg;
23002e202ad8SKent Gibson 
23012e202ad8SKent Gibson 	/* We fail any subsequent ioctl():s when the chip is gone */
23022e202ad8SKent Gibson 	if (!gdev->chip)
23032e202ad8SKent Gibson 		return -ENODEV;
23042e202ad8SKent Gibson 
23052e202ad8SKent Gibson 	/* Fill in the struct and pass to userspace */
23062e202ad8SKent Gibson 	if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
23072e202ad8SKent Gibson 		return chipinfo_get(cdev, ip);
23082e202ad8SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
23092e202ad8SKent Gibson 	} else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
23102e202ad8SKent Gibson 		return linehandle_create(gdev, ip);
23112e202ad8SKent Gibson 	} else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
23122e202ad8SKent Gibson 		return lineevent_create(gdev, ip);
23132e202ad8SKent Gibson 	} else if (cmd == GPIO_GET_LINEINFO_IOCTL ||
23142e202ad8SKent Gibson 		   cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
23152e202ad8SKent Gibson 		return lineinfo_get_v1(cdev, ip,
23162e202ad8SKent Gibson 				       cmd == GPIO_GET_LINEINFO_WATCH_IOCTL);
23172e202ad8SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
23182e202ad8SKent Gibson 	} else if (cmd == GPIO_V2_GET_LINEINFO_IOCTL ||
23192e202ad8SKent Gibson 		   cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL) {
23202e202ad8SKent Gibson 		return lineinfo_get(cdev, ip,
23212e202ad8SKent Gibson 				    cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL);
23222e202ad8SKent Gibson 	} else if (cmd == GPIO_V2_GET_LINE_IOCTL) {
23232e202ad8SKent Gibson 		return linereq_create(gdev, ip);
23242e202ad8SKent Gibson 	} else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) {
23252e202ad8SKent Gibson 		return lineinfo_unwatch(cdev, ip);
23262e202ad8SKent Gibson 	}
2327925ca369SKent Gibson 	return -EINVAL;
2328925ca369SKent Gibson }
2329925ca369SKent Gibson 
2330925ca369SKent Gibson #ifdef CONFIG_COMPAT
233149bc5279SKent Gibson static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2332925ca369SKent Gibson 			      unsigned long arg)
2333925ca369SKent Gibson {
233449bc5279SKent Gibson 	return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2335925ca369SKent Gibson }
2336925ca369SKent Gibson #endif
2337925ca369SKent Gibson 
2338925ca369SKent Gibson static struct gpio_chardev_data *
2339925ca369SKent Gibson to_gpio_chardev_data(struct notifier_block *nb)
2340925ca369SKent Gibson {
2341925ca369SKent Gibson 	return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2342925ca369SKent Gibson }
2343925ca369SKent Gibson 
2344925ca369SKent Gibson static int lineinfo_changed_notify(struct notifier_block *nb,
2345925ca369SKent Gibson 				   unsigned long action, void *data)
2346925ca369SKent Gibson {
2347e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
2348aad95584SKent Gibson 	struct gpio_v2_line_info_changed chg;
2349925ca369SKent Gibson 	struct gpio_desc *desc = data;
2350925ca369SKent Gibson 	int ret;
2351925ca369SKent Gibson 
2352e2b781c5SKent Gibson 	if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2353925ca369SKent Gibson 		return NOTIFY_DONE;
2354925ca369SKent Gibson 
2355925ca369SKent Gibson 	memset(&chg, 0, sizeof(chg));
2356925ca369SKent Gibson 	chg.event_type = action;
2357aad95584SKent Gibson 	chg.timestamp_ns = ktime_get_ns();
2358925ca369SKent Gibson 	gpio_desc_to_lineinfo(desc, &chg.info);
2359925ca369SKent Gibson 
2360e2b781c5SKent Gibson 	ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2361925ca369SKent Gibson 	if (ret)
2362e2b781c5SKent Gibson 		wake_up_poll(&cdev->wait, EPOLLIN);
2363925ca369SKent Gibson 	else
2364925ca369SKent Gibson 		pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2365925ca369SKent Gibson 
2366925ca369SKent Gibson 	return NOTIFY_OK;
2367925ca369SKent Gibson }
2368925ca369SKent Gibson 
236949bc5279SKent Gibson static __poll_t lineinfo_watch_poll(struct file *file,
2370925ca369SKent Gibson 				    struct poll_table_struct *pollt)
2371925ca369SKent Gibson {
2372e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2373925ca369SKent Gibson 	__poll_t events = 0;
2374925ca369SKent Gibson 
2375e2b781c5SKent Gibson 	poll_wait(file, &cdev->wait, pollt);
2376925ca369SKent Gibson 
2377e2b781c5SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2378e2b781c5SKent Gibson 						 &cdev->wait.lock))
2379925ca369SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
2380925ca369SKent Gibson 
2381925ca369SKent Gibson 	return events;
2382925ca369SKent Gibson }
2383925ca369SKent Gibson 
238449bc5279SKent Gibson static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2385925ca369SKent Gibson 				   size_t count, loff_t *off)
2386925ca369SKent Gibson {
2387e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2388aad95584SKent Gibson 	struct gpio_v2_line_info_changed event;
2389925ca369SKent Gibson 	ssize_t bytes_read = 0;
2390925ca369SKent Gibson 	int ret;
2391aad95584SKent Gibson 	size_t event_size;
2392925ca369SKent Gibson 
2393aad95584SKent Gibson #ifndef CONFIG_GPIO_CDEV_V1
2394aad95584SKent Gibson 	event_size = sizeof(struct gpio_v2_line_info_changed);
2395aad95584SKent Gibson 	if (count < event_size)
2396925ca369SKent Gibson 		return -EINVAL;
2397aad95584SKent Gibson #endif
2398925ca369SKent Gibson 
2399925ca369SKent Gibson 	do {
2400e2b781c5SKent Gibson 		spin_lock(&cdev->wait.lock);
2401e2b781c5SKent Gibson 		if (kfifo_is_empty(&cdev->events)) {
2402925ca369SKent Gibson 			if (bytes_read) {
2403e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2404925ca369SKent Gibson 				return bytes_read;
2405925ca369SKent Gibson 			}
2406925ca369SKent Gibson 
240749bc5279SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
2408e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2409925ca369SKent Gibson 				return -EAGAIN;
2410925ca369SKent Gibson 			}
2411925ca369SKent Gibson 
2412e2b781c5SKent Gibson 			ret = wait_event_interruptible_locked(cdev->wait,
2413e2b781c5SKent Gibson 					!kfifo_is_empty(&cdev->events));
2414925ca369SKent Gibson 			if (ret) {
2415e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2416925ca369SKent Gibson 				return ret;
2417925ca369SKent Gibson 			}
2418925ca369SKent Gibson 		}
2419aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2420aad95584SKent Gibson 		/* must be after kfifo check so watch_abi_version is set */
2421aad95584SKent Gibson 		if (atomic_read(&cdev->watch_abi_version) == 2)
2422aad95584SKent Gibson 			event_size = sizeof(struct gpio_v2_line_info_changed);
2423aad95584SKent Gibson 		else
2424aad95584SKent Gibson 			event_size = sizeof(struct gpioline_info_changed);
2425aad95584SKent Gibson 		if (count < event_size) {
2426aad95584SKent Gibson 			spin_unlock(&cdev->wait.lock);
2427aad95584SKent Gibson 			return -EINVAL;
2428aad95584SKent Gibson 		}
2429aad95584SKent Gibson #endif
2430e2b781c5SKent Gibson 		ret = kfifo_out(&cdev->events, &event, 1);
2431e2b781c5SKent Gibson 		spin_unlock(&cdev->wait.lock);
2432925ca369SKent Gibson 		if (ret != 1) {
2433925ca369SKent Gibson 			ret = -EIO;
2434925ca369SKent Gibson 			break;
2435925ca369SKent Gibson 			/* We should never get here. See lineevent_read(). */
2436925ca369SKent Gibson 		}
2437925ca369SKent Gibson 
2438aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2439aad95584SKent Gibson 		if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2440aad95584SKent Gibson 			if (copy_to_user(buf + bytes_read, &event, event_size))
2441925ca369SKent Gibson 				return -EFAULT;
2442aad95584SKent Gibson 		} else {
2443aad95584SKent Gibson 			struct gpioline_info_changed event_v1;
2444aad95584SKent Gibson 
2445aad95584SKent Gibson 			gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2446aad95584SKent Gibson 			if (copy_to_user(buf + bytes_read, &event_v1,
2447aad95584SKent Gibson 					 event_size))
2448aad95584SKent Gibson 				return -EFAULT;
2449aad95584SKent Gibson 		}
2450aad95584SKent Gibson #else
2451aad95584SKent Gibson 		if (copy_to_user(buf + bytes_read, &event, event_size))
2452aad95584SKent Gibson 			return -EFAULT;
2453aad95584SKent Gibson #endif
2454aad95584SKent Gibson 		bytes_read += event_size;
2455925ca369SKent Gibson 	} while (count >= bytes_read + sizeof(event));
2456925ca369SKent Gibson 
2457925ca369SKent Gibson 	return bytes_read;
2458925ca369SKent Gibson }
2459925ca369SKent Gibson 
2460925ca369SKent Gibson /**
2461925ca369SKent Gibson  * gpio_chrdev_open() - open the chardev for ioctl operations
2462925ca369SKent Gibson  * @inode: inode for this chardev
246349bc5279SKent Gibson  * @file: file struct for storing private data
2464925ca369SKent Gibson  * Returns 0 on success
2465925ca369SKent Gibson  */
246649bc5279SKent Gibson static int gpio_chrdev_open(struct inode *inode, struct file *file)
2467925ca369SKent Gibson {
2468925ca369SKent Gibson 	struct gpio_device *gdev = container_of(inode->i_cdev,
2469925ca369SKent Gibson 						struct gpio_device, chrdev);
2470e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev;
2471925ca369SKent Gibson 	int ret = -ENOMEM;
2472925ca369SKent Gibson 
2473925ca369SKent Gibson 	/* Fail on open if the backing gpiochip is gone */
2474925ca369SKent Gibson 	if (!gdev->chip)
2475925ca369SKent Gibson 		return -ENODEV;
2476925ca369SKent Gibson 
2477e2b781c5SKent Gibson 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2478e2b781c5SKent Gibson 	if (!cdev)
2479925ca369SKent Gibson 		return -ENOMEM;
2480925ca369SKent Gibson 
2481e2b781c5SKent Gibson 	cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2482e2b781c5SKent Gibson 	if (!cdev->watched_lines)
2483e2b781c5SKent Gibson 		goto out_free_cdev;
2484925ca369SKent Gibson 
2485e2b781c5SKent Gibson 	init_waitqueue_head(&cdev->wait);
2486e2b781c5SKent Gibson 	INIT_KFIFO(cdev->events);
2487e2b781c5SKent Gibson 	cdev->gdev = gdev;
2488925ca369SKent Gibson 
2489e2b781c5SKent Gibson 	cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
24906accc376SKent Gibson 	ret = blocking_notifier_chain_register(&gdev->notifier,
2491e2b781c5SKent Gibson 					       &cdev->lineinfo_changed_nb);
2492925ca369SKent Gibson 	if (ret)
2493925ca369SKent Gibson 		goto out_free_bitmap;
2494925ca369SKent Gibson 
2495925ca369SKent Gibson 	get_device(&gdev->dev);
2496e2b781c5SKent Gibson 	file->private_data = cdev;
2497925ca369SKent Gibson 
249849bc5279SKent Gibson 	ret = nonseekable_open(inode, file);
2499925ca369SKent Gibson 	if (ret)
2500925ca369SKent Gibson 		goto out_unregister_notifier;
2501925ca369SKent Gibson 
2502925ca369SKent Gibson 	return ret;
2503925ca369SKent Gibson 
2504925ca369SKent Gibson out_unregister_notifier:
25056accc376SKent Gibson 	blocking_notifier_chain_unregister(&gdev->notifier,
2506e2b781c5SKent Gibson 					   &cdev->lineinfo_changed_nb);
2507925ca369SKent Gibson out_free_bitmap:
2508e2b781c5SKent Gibson 	bitmap_free(cdev->watched_lines);
2509e2b781c5SKent Gibson out_free_cdev:
2510e2b781c5SKent Gibson 	kfree(cdev);
2511925ca369SKent Gibson 	return ret;
2512925ca369SKent Gibson }
2513925ca369SKent Gibson 
2514925ca369SKent Gibson /**
2515925ca369SKent Gibson  * gpio_chrdev_release() - close chardev after ioctl operations
2516925ca369SKent Gibson  * @inode: inode for this chardev
251749bc5279SKent Gibson  * @file: file struct for storing private data
2518925ca369SKent Gibson  * Returns 0 on success
2519925ca369SKent Gibson  */
252049bc5279SKent Gibson static int gpio_chrdev_release(struct inode *inode, struct file *file)
2521925ca369SKent Gibson {
2522e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2523e2b781c5SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
2524925ca369SKent Gibson 
2525e2b781c5SKent Gibson 	bitmap_free(cdev->watched_lines);
25266accc376SKent Gibson 	blocking_notifier_chain_unregister(&gdev->notifier,
2527e2b781c5SKent Gibson 					   &cdev->lineinfo_changed_nb);
2528925ca369SKent Gibson 	put_device(&gdev->dev);
2529e2b781c5SKent Gibson 	kfree(cdev);
2530925ca369SKent Gibson 
2531925ca369SKent Gibson 	return 0;
2532925ca369SKent Gibson }
2533925ca369SKent Gibson 
2534925ca369SKent Gibson static const struct file_operations gpio_fileops = {
2535925ca369SKent Gibson 	.release = gpio_chrdev_release,
2536925ca369SKent Gibson 	.open = gpio_chrdev_open,
2537925ca369SKent Gibson 	.poll = lineinfo_watch_poll,
2538925ca369SKent Gibson 	.read = lineinfo_watch_read,
2539925ca369SKent Gibson 	.owner = THIS_MODULE,
2540925ca369SKent Gibson 	.llseek = no_llseek,
2541925ca369SKent Gibson 	.unlocked_ioctl = gpio_ioctl,
2542925ca369SKent Gibson #ifdef CONFIG_COMPAT
2543925ca369SKent Gibson 	.compat_ioctl = gpio_ioctl_compat,
2544925ca369SKent Gibson #endif
2545925ca369SKent Gibson };
2546925ca369SKent Gibson 
2547925ca369SKent Gibson int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2548925ca369SKent Gibson {
2549925ca369SKent Gibson 	int ret;
2550925ca369SKent Gibson 
2551925ca369SKent Gibson 	cdev_init(&gdev->chrdev, &gpio_fileops);
2552925ca369SKent Gibson 	gdev->chrdev.owner = THIS_MODULE;
2553925ca369SKent Gibson 	gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2554925ca369SKent Gibson 
2555925ca369SKent Gibson 	ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2556925ca369SKent Gibson 	if (ret)
2557925ca369SKent Gibson 		return ret;
2558925ca369SKent Gibson 
2559925ca369SKent Gibson 	chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2560925ca369SKent Gibson 		 MAJOR(devt), gdev->id);
2561925ca369SKent Gibson 
2562925ca369SKent Gibson 	return 0;
2563925ca369SKent Gibson }
2564925ca369SKent Gibson 
2565925ca369SKent Gibson void gpiolib_cdev_unregister(struct gpio_device *gdev)
2566925ca369SKent Gibson {
2567925ca369SKent Gibson 	cdev_device_del(&gdev->chrdev, &gdev->dev);
2568925ca369SKent Gibson }
2569