xref: /openbmc/linux/drivers/gpio/gpiolib-cdev.c (revision bdbbae241a04f387ba910b8609f95fad5f1470c7)
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 
58*bdbbae24SBartosz Golaszewski typedef __poll_t (*poll_fn)(struct file *, struct poll_table_struct *);
59*bdbbae24SBartosz Golaszewski typedef long (*ioctl_fn)(struct file *, unsigned int, unsigned long);
60*bdbbae24SBartosz Golaszewski typedef ssize_t (*read_fn)(struct file *, char __user *,
61*bdbbae24SBartosz Golaszewski 			   size_t count, loff_t *);
62*bdbbae24SBartosz Golaszewski 
63*bdbbae24SBartosz Golaszewski static __poll_t call_poll_locked(struct file *file,
64*bdbbae24SBartosz Golaszewski 				 struct poll_table_struct *wait,
65*bdbbae24SBartosz Golaszewski 				 struct gpio_device *gdev, poll_fn func)
66*bdbbae24SBartosz Golaszewski {
67*bdbbae24SBartosz Golaszewski 	__poll_t ret;
68*bdbbae24SBartosz Golaszewski 
69*bdbbae24SBartosz Golaszewski 	down_read(&gdev->sem);
70*bdbbae24SBartosz Golaszewski 	ret = func(file, wait);
71*bdbbae24SBartosz Golaszewski 	up_read(&gdev->sem);
72*bdbbae24SBartosz Golaszewski 
73*bdbbae24SBartosz Golaszewski 	return ret;
74*bdbbae24SBartosz Golaszewski }
75*bdbbae24SBartosz Golaszewski 
76*bdbbae24SBartosz Golaszewski static long call_ioctl_locked(struct file *file, unsigned int cmd,
77*bdbbae24SBartosz Golaszewski 			      unsigned long arg, struct gpio_device *gdev,
78*bdbbae24SBartosz Golaszewski 			      ioctl_fn func)
79*bdbbae24SBartosz Golaszewski {
80*bdbbae24SBartosz Golaszewski 	long ret;
81*bdbbae24SBartosz Golaszewski 
82*bdbbae24SBartosz Golaszewski 	down_read(&gdev->sem);
83*bdbbae24SBartosz Golaszewski 	ret = func(file, cmd, arg);
84*bdbbae24SBartosz Golaszewski 	up_read(&gdev->sem);
85*bdbbae24SBartosz Golaszewski 
86*bdbbae24SBartosz Golaszewski 	return ret;
87*bdbbae24SBartosz Golaszewski }
88*bdbbae24SBartosz Golaszewski 
89*bdbbae24SBartosz Golaszewski static ssize_t call_read_locked(struct file *file, char __user *buf,
90*bdbbae24SBartosz Golaszewski 				size_t count, loff_t *f_ps,
91*bdbbae24SBartosz Golaszewski 				struct gpio_device *gdev, read_fn func)
92*bdbbae24SBartosz Golaszewski {
93*bdbbae24SBartosz Golaszewski 	ssize_t ret;
94*bdbbae24SBartosz Golaszewski 
95*bdbbae24SBartosz Golaszewski 	down_read(&gdev->sem);
96*bdbbae24SBartosz Golaszewski 	ret = func(file, buf, count, f_ps);
97*bdbbae24SBartosz Golaszewski 	up_read(&gdev->sem);
98*bdbbae24SBartosz Golaszewski 
99*bdbbae24SBartosz Golaszewski 	return ret;
100*bdbbae24SBartosz Golaszewski }
101*bdbbae24SBartosz Golaszewski 
102925ca369SKent Gibson /*
103925ca369SKent Gibson  * GPIO line handle management
104925ca369SKent Gibson  */
105925ca369SKent Gibson 
1063c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
107925ca369SKent Gibson /**
108925ca369SKent Gibson  * struct linehandle_state - contains the state of a userspace handle
109925ca369SKent Gibson  * @gdev: the GPIO device the handle pertains to
110925ca369SKent Gibson  * @label: consumer label used to tag descriptors
111925ca369SKent Gibson  * @descs: the GPIO descriptors held by this handle
11252b7b596SKent Gibson  * @num_descs: the number of descriptors held in the descs array
113925ca369SKent Gibson  */
114925ca369SKent Gibson struct linehandle_state {
115925ca369SKent Gibson 	struct gpio_device *gdev;
116925ca369SKent Gibson 	const char *label;
117925ca369SKent Gibson 	struct gpio_desc *descs[GPIOHANDLES_MAX];
11852b7b596SKent Gibson 	u32 num_descs;
119925ca369SKent Gibson };
120925ca369SKent Gibson 
121925ca369SKent Gibson #define GPIOHANDLE_REQUEST_VALID_FLAGS \
122925ca369SKent Gibson 	(GPIOHANDLE_REQUEST_INPUT | \
123925ca369SKent Gibson 	GPIOHANDLE_REQUEST_OUTPUT | \
124925ca369SKent Gibson 	GPIOHANDLE_REQUEST_ACTIVE_LOW | \
125925ca369SKent Gibson 	GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
126925ca369SKent Gibson 	GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
127925ca369SKent Gibson 	GPIOHANDLE_REQUEST_BIAS_DISABLE | \
128925ca369SKent Gibson 	GPIOHANDLE_REQUEST_OPEN_DRAIN | \
129925ca369SKent Gibson 	GPIOHANDLE_REQUEST_OPEN_SOURCE)
130925ca369SKent Gibson 
131925ca369SKent Gibson static int linehandle_validate_flags(u32 flags)
132925ca369SKent Gibson {
133925ca369SKent Gibson 	/* Return an error if an unknown flag is set */
134925ca369SKent Gibson 	if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
135925ca369SKent Gibson 		return -EINVAL;
136925ca369SKent Gibson 
137925ca369SKent Gibson 	/*
138925ca369SKent Gibson 	 * Do not allow both INPUT & OUTPUT flags to be set as they are
139925ca369SKent Gibson 	 * contradictory.
140925ca369SKent Gibson 	 */
141925ca369SKent Gibson 	if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
142925ca369SKent Gibson 	    (flags & GPIOHANDLE_REQUEST_OUTPUT))
143925ca369SKent Gibson 		return -EINVAL;
144925ca369SKent Gibson 
145925ca369SKent Gibson 	/*
146925ca369SKent Gibson 	 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
147925ca369SKent Gibson 	 * the hardware actually supports enabling both at the same time the
148925ca369SKent Gibson 	 * electrical result would be disastrous.
149925ca369SKent Gibson 	 */
150925ca369SKent Gibson 	if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
151925ca369SKent Gibson 	    (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
152925ca369SKent Gibson 		return -EINVAL;
153925ca369SKent Gibson 
154925ca369SKent Gibson 	/* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
155925ca369SKent Gibson 	if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
156925ca369SKent Gibson 	    ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
157925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
158925ca369SKent Gibson 		return -EINVAL;
159925ca369SKent Gibson 
160925ca369SKent Gibson 	/* Bias flags only allowed for input or output mode. */
161925ca369SKent Gibson 	if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
162925ca369SKent Gibson 	      (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
163925ca369SKent Gibson 	    ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
164925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
165925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
166925ca369SKent Gibson 		return -EINVAL;
167925ca369SKent Gibson 
168925ca369SKent Gibson 	/* Only one bias flag can be set. */
169925ca369SKent Gibson 	if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
170925ca369SKent Gibson 	     (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
171925ca369SKent Gibson 		       GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
172925ca369SKent Gibson 	    ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
173925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
174925ca369SKent Gibson 		return -EINVAL;
175925ca369SKent Gibson 
176925ca369SKent Gibson 	return 0;
177925ca369SKent Gibson }
178925ca369SKent Gibson 
179c274b58aSKent Gibson static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
180c274b58aSKent Gibson {
181c274b58aSKent Gibson 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
182c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
183c274b58aSKent Gibson 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
184c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
185c274b58aSKent Gibson 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
186c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
187c274b58aSKent Gibson 	assign_bit(FLAG_PULL_UP, flagsp,
188c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
189c274b58aSKent Gibson 	assign_bit(FLAG_PULL_DOWN, flagsp,
190c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
191c274b58aSKent Gibson 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
192c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
193c274b58aSKent Gibson }
194c274b58aSKent Gibson 
195925ca369SKent Gibson static long linehandle_set_config(struct linehandle_state *lh,
196925ca369SKent Gibson 				  void __user *ip)
197925ca369SKent Gibson {
198925ca369SKent Gibson 	struct gpiohandle_config gcnf;
199925ca369SKent Gibson 	struct gpio_desc *desc;
200925ca369SKent Gibson 	int i, ret;
201925ca369SKent Gibson 	u32 lflags;
202925ca369SKent Gibson 
203925ca369SKent Gibson 	if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
204925ca369SKent Gibson 		return -EFAULT;
205925ca369SKent Gibson 
206925ca369SKent Gibson 	lflags = gcnf.flags;
207925ca369SKent Gibson 	ret = linehandle_validate_flags(lflags);
208925ca369SKent Gibson 	if (ret)
209925ca369SKent Gibson 		return ret;
210925ca369SKent Gibson 
21152b7b596SKent Gibson 	for (i = 0; i < lh->num_descs; i++) {
212925ca369SKent Gibson 		desc = lh->descs[i];
213c274b58aSKent Gibson 		linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags);
214925ca369SKent Gibson 
215925ca369SKent Gibson 		/*
216925ca369SKent Gibson 		 * Lines have to be requested explicitly for input
217925ca369SKent Gibson 		 * or output, else the line will be treated "as is".
218925ca369SKent Gibson 		 */
219925ca369SKent Gibson 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
220925ca369SKent Gibson 			int val = !!gcnf.default_values[i];
221925ca369SKent Gibson 
222925ca369SKent Gibson 			ret = gpiod_direction_output(desc, val);
223925ca369SKent Gibson 			if (ret)
224925ca369SKent Gibson 				return ret;
225925ca369SKent Gibson 		} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
226925ca369SKent Gibson 			ret = gpiod_direction_input(desc);
227925ca369SKent Gibson 			if (ret)
228925ca369SKent Gibson 				return ret;
229925ca369SKent Gibson 		}
230925ca369SKent Gibson 
2316accc376SKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
232aad95584SKent Gibson 					     GPIO_V2_LINE_CHANGED_CONFIG,
233aad95584SKent Gibson 					     desc);
234925ca369SKent Gibson 	}
235925ca369SKent Gibson 	return 0;
236925ca369SKent Gibson }
237925ca369SKent Gibson 
238*bdbbae24SBartosz Golaszewski static long linehandle_ioctl_unlocked(struct file *file, unsigned int cmd,
239925ca369SKent Gibson 				      unsigned long arg)
240925ca369SKent Gibson {
24149bc5279SKent Gibson 	struct linehandle_state *lh = file->private_data;
242925ca369SKent Gibson 	void __user *ip = (void __user *)arg;
243925ca369SKent Gibson 	struct gpiohandle_data ghd;
244925ca369SKent Gibson 	DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
2451cef8b50SAndy Shevchenko 	unsigned int i;
2461cef8b50SAndy Shevchenko 	int ret;
247925ca369SKent Gibson 
248533aae7cSBartosz Golaszewski 	if (!lh->gdev->chip)
249533aae7cSBartosz Golaszewski 		return -ENODEV;
250533aae7cSBartosz Golaszewski 
2511cef8b50SAndy Shevchenko 	switch (cmd) {
2521cef8b50SAndy Shevchenko 	case GPIOHANDLE_GET_LINE_VALUES_IOCTL:
2531cef8b50SAndy Shevchenko 		/* NOTE: It's okay to read values of output lines */
2541cef8b50SAndy Shevchenko 		ret = gpiod_get_array_value_complex(false, true,
2551cef8b50SAndy Shevchenko 						    lh->num_descs, lh->descs,
2561cef8b50SAndy Shevchenko 						    NULL, vals);
257925ca369SKent Gibson 		if (ret)
258925ca369SKent Gibson 			return ret;
259925ca369SKent Gibson 
260925ca369SKent Gibson 		memset(&ghd, 0, sizeof(ghd));
26152b7b596SKent Gibson 		for (i = 0; i < lh->num_descs; i++)
262925ca369SKent Gibson 			ghd.values[i] = test_bit(i, vals);
263925ca369SKent Gibson 
264925ca369SKent Gibson 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
265925ca369SKent Gibson 			return -EFAULT;
266925ca369SKent Gibson 
267925ca369SKent Gibson 		return 0;
2681cef8b50SAndy Shevchenko 	case GPIOHANDLE_SET_LINE_VALUES_IOCTL:
269925ca369SKent Gibson 		/*
270925ca369SKent Gibson 		 * All line descriptors were created at once with the same
271925ca369SKent Gibson 		 * flags so just check if the first one is really output.
272925ca369SKent Gibson 		 */
273925ca369SKent Gibson 		if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
274925ca369SKent Gibson 			return -EPERM;
275925ca369SKent Gibson 
276925ca369SKent Gibson 		if (copy_from_user(&ghd, ip, sizeof(ghd)))
277925ca369SKent Gibson 			return -EFAULT;
278925ca369SKent Gibson 
279925ca369SKent Gibson 		/* Clamp all values to [0,1] */
28052b7b596SKent Gibson 		for (i = 0; i < lh->num_descs; i++)
281925ca369SKent Gibson 			__assign_bit(i, vals, ghd.values[i]);
282925ca369SKent Gibson 
283925ca369SKent Gibson 		/* Reuse the array setting function */
284925ca369SKent Gibson 		return gpiod_set_array_value_complex(false,
285925ca369SKent Gibson 						     true,
28652b7b596SKent Gibson 						     lh->num_descs,
287925ca369SKent Gibson 						     lh->descs,
288925ca369SKent Gibson 						     NULL,
289925ca369SKent Gibson 						     vals);
2901cef8b50SAndy Shevchenko 	case GPIOHANDLE_SET_CONFIG_IOCTL:
291925ca369SKent Gibson 		return linehandle_set_config(lh, ip);
2921cef8b50SAndy Shevchenko 	default:
293925ca369SKent Gibson 		return -EINVAL;
294925ca369SKent Gibson 	}
2951cef8b50SAndy Shevchenko }
296925ca369SKent Gibson 
297*bdbbae24SBartosz Golaszewski static long linehandle_ioctl(struct file *file, unsigned int cmd,
298*bdbbae24SBartosz Golaszewski 			     unsigned long arg)
299*bdbbae24SBartosz Golaszewski {
300*bdbbae24SBartosz Golaszewski 	struct linehandle_state *lh = file->private_data;
301*bdbbae24SBartosz Golaszewski 
302*bdbbae24SBartosz Golaszewski 	return call_ioctl_locked(file, cmd, arg, lh->gdev,
303*bdbbae24SBartosz Golaszewski 				 linehandle_ioctl_unlocked);
304*bdbbae24SBartosz Golaszewski }
305*bdbbae24SBartosz Golaszewski 
306925ca369SKent Gibson #ifdef CONFIG_COMPAT
30749bc5279SKent Gibson static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
308925ca369SKent Gibson 				    unsigned long arg)
309925ca369SKent Gibson {
31049bc5279SKent Gibson 	return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
311925ca369SKent Gibson }
312925ca369SKent Gibson #endif
313925ca369SKent Gibson 
314883f9198SKent Gibson static void linehandle_free(struct linehandle_state *lh)
315925ca369SKent Gibson {
316925ca369SKent Gibson 	int i;
317925ca369SKent Gibson 
31852b7b596SKent Gibson 	for (i = 0; i < lh->num_descs; i++)
319883f9198SKent Gibson 		if (lh->descs[i])
320925ca369SKent Gibson 			gpiod_free(lh->descs[i]);
321925ca369SKent Gibson 	kfree(lh->label);
322883f9198SKent Gibson 	put_device(&lh->gdev->dev);
323925ca369SKent Gibson 	kfree(lh);
324883f9198SKent Gibson }
325883f9198SKent Gibson 
326883f9198SKent Gibson static int linehandle_release(struct inode *inode, struct file *file)
327883f9198SKent Gibson {
328883f9198SKent Gibson 	linehandle_free(file->private_data);
329925ca369SKent Gibson 	return 0;
330925ca369SKent Gibson }
331925ca369SKent Gibson 
332925ca369SKent Gibson static const struct file_operations linehandle_fileops = {
333925ca369SKent Gibson 	.release = linehandle_release,
334925ca369SKent Gibson 	.owner = THIS_MODULE,
335925ca369SKent Gibson 	.llseek = noop_llseek,
336925ca369SKent Gibson 	.unlocked_ioctl = linehandle_ioctl,
337925ca369SKent Gibson #ifdef CONFIG_COMPAT
338925ca369SKent Gibson 	.compat_ioctl = linehandle_ioctl_compat,
339925ca369SKent Gibson #endif
340925ca369SKent Gibson };
341925ca369SKent Gibson 
342925ca369SKent Gibson static int linehandle_create(struct gpio_device *gdev, void __user *ip)
343925ca369SKent Gibson {
344925ca369SKent Gibson 	struct gpiohandle_request handlereq;
345925ca369SKent Gibson 	struct linehandle_state *lh;
346925ca369SKent Gibson 	struct file *file;
347883f9198SKent Gibson 	int fd, i, ret;
348925ca369SKent Gibson 	u32 lflags;
349925ca369SKent Gibson 
350925ca369SKent Gibson 	if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
351925ca369SKent Gibson 		return -EFAULT;
352925ca369SKent Gibson 	if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
353925ca369SKent Gibson 		return -EINVAL;
354925ca369SKent Gibson 
355925ca369SKent Gibson 	lflags = handlereq.flags;
356925ca369SKent Gibson 
357925ca369SKent Gibson 	ret = linehandle_validate_flags(lflags);
358925ca369SKent Gibson 	if (ret)
359925ca369SKent Gibson 		return ret;
360925ca369SKent Gibson 
361925ca369SKent Gibson 	lh = kzalloc(sizeof(*lh), GFP_KERNEL);
362925ca369SKent Gibson 	if (!lh)
363925ca369SKent Gibson 		return -ENOMEM;
364925ca369SKent Gibson 	lh->gdev = gdev;
365925ca369SKent Gibson 	get_device(&gdev->dev);
366925ca369SKent Gibson 
367f188ac12SKent Gibson 	if (handlereq.consumer_label[0] != '\0') {
368f188ac12SKent Gibson 		/* label is only initialized if consumer_label is set */
369f188ac12SKent Gibson 		lh->label = kstrndup(handlereq.consumer_label,
370f188ac12SKent Gibson 				     sizeof(handlereq.consumer_label) - 1,
371925ca369SKent Gibson 				     GFP_KERNEL);
372925ca369SKent Gibson 		if (!lh->label) {
373925ca369SKent Gibson 			ret = -ENOMEM;
374925ca369SKent Gibson 			goto out_free_lh;
375925ca369SKent Gibson 		}
376925ca369SKent Gibson 	}
377925ca369SKent Gibson 
378883f9198SKent Gibson 	lh->num_descs = handlereq.lines;
379883f9198SKent Gibson 
380925ca369SKent Gibson 	/* Request each GPIO */
381925ca369SKent Gibson 	for (i = 0; i < handlereq.lines; i++) {
382925ca369SKent Gibson 		u32 offset = handlereq.lineoffsets[i];
383925ca369SKent Gibson 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
384925ca369SKent Gibson 
385925ca369SKent Gibson 		if (IS_ERR(desc)) {
386925ca369SKent Gibson 			ret = PTR_ERR(desc);
387883f9198SKent Gibson 			goto out_free_lh;
388925ca369SKent Gibson 		}
389925ca369SKent Gibson 
39095a4eed7SAndy Shevchenko 		ret = gpiod_request_user(desc, lh->label);
391925ca369SKent Gibson 		if (ret)
392883f9198SKent Gibson 			goto out_free_lh;
393925ca369SKent Gibson 		lh->descs[i] = desc;
394c274b58aSKent Gibson 		linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
395925ca369SKent Gibson 
396925ca369SKent Gibson 		ret = gpiod_set_transitory(desc, false);
397925ca369SKent Gibson 		if (ret < 0)
398883f9198SKent Gibson 			goto out_free_lh;
399925ca369SKent Gibson 
400925ca369SKent Gibson 		/*
401925ca369SKent Gibson 		 * Lines have to be requested explicitly for input
402925ca369SKent Gibson 		 * or output, else the line will be treated "as is".
403925ca369SKent Gibson 		 */
404925ca369SKent Gibson 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
405925ca369SKent Gibson 			int val = !!handlereq.default_values[i];
406925ca369SKent Gibson 
407925ca369SKent Gibson 			ret = gpiod_direction_output(desc, val);
408925ca369SKent Gibson 			if (ret)
409883f9198SKent Gibson 				goto out_free_lh;
410925ca369SKent Gibson 		} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
411925ca369SKent Gibson 			ret = gpiod_direction_input(desc);
412925ca369SKent Gibson 			if (ret)
413883f9198SKent Gibson 				goto out_free_lh;
414925ca369SKent Gibson 		}
415925ca369SKent Gibson 
4166accc376SKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
417aad95584SKent Gibson 					     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
418925ca369SKent Gibson 
419925ca369SKent Gibson 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
420925ca369SKent Gibson 			offset);
421925ca369SKent Gibson 	}
422925ca369SKent Gibson 
423925ca369SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
424925ca369SKent Gibson 	if (fd < 0) {
425925ca369SKent Gibson 		ret = fd;
426883f9198SKent Gibson 		goto out_free_lh;
427925ca369SKent Gibson 	}
428925ca369SKent Gibson 
429925ca369SKent Gibson 	file = anon_inode_getfile("gpio-linehandle",
430925ca369SKent Gibson 				  &linehandle_fileops,
431925ca369SKent Gibson 				  lh,
432925ca369SKent Gibson 				  O_RDONLY | O_CLOEXEC);
433925ca369SKent Gibson 	if (IS_ERR(file)) {
434925ca369SKent Gibson 		ret = PTR_ERR(file);
435925ca369SKent Gibson 		goto out_put_unused_fd;
436925ca369SKent Gibson 	}
437925ca369SKent Gibson 
438925ca369SKent Gibson 	handlereq.fd = fd;
439925ca369SKent Gibson 	if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
440925ca369SKent Gibson 		/*
441925ca369SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
442925ca369SKent Gibson 		 * the regular error cleanup path here.
443925ca369SKent Gibson 		 */
444925ca369SKent Gibson 		fput(file);
445925ca369SKent Gibson 		put_unused_fd(fd);
446925ca369SKent Gibson 		return -EFAULT;
447925ca369SKent Gibson 	}
448925ca369SKent Gibson 
449925ca369SKent Gibson 	fd_install(fd, file);
450925ca369SKent Gibson 
451925ca369SKent Gibson 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
45252b7b596SKent Gibson 		lh->num_descs);
453925ca369SKent Gibson 
454925ca369SKent Gibson 	return 0;
455925ca369SKent Gibson 
456925ca369SKent Gibson out_put_unused_fd:
457925ca369SKent Gibson 	put_unused_fd(fd);
458925ca369SKent Gibson out_free_lh:
459883f9198SKent Gibson 	linehandle_free(lh);
460925ca369SKent Gibson 	return ret;
461925ca369SKent Gibson }
4623c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
4633c0d9c63SKent Gibson 
4643c0d9c63SKent Gibson /**
4653c0d9c63SKent Gibson  * struct line - contains the state of a requested line
4663c0d9c63SKent Gibson  * @desc: the GPIO descriptor for this line.
46773e03419SKent Gibson  * @req: the corresponding line request
46873e03419SKent Gibson  * @irq: the interrupt triggered in response to events on this GPIO
4698d259847SAndy Shevchenko  * @edflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or
47073e03419SKent Gibson  * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied
47173e03419SKent Gibson  * @timestamp_ns: cache for the timestamp storing it between hardirq and
47273e03419SKent Gibson  * IRQ thread, used to bring the timestamp close to the actual event
47373e03419SKent Gibson  * @req_seqno: the seqno for the current edge event in the sequence of
47473e03419SKent Gibson  * events for the corresponding line request. This is drawn from the @req.
47573e03419SKent Gibson  * @line_seqno: the seqno for the current edge event in the sequence of
47673e03419SKent Gibson  * events for this line.
47765cff704SKent Gibson  * @work: the worker that implements software debouncing
47865cff704SKent Gibson  * @sw_debounced: flag indicating if the software debouncer is active
47965cff704SKent Gibson  * @level: the current debounced physical level of the line
48085ff37e3SAndy Shevchenko  * @hdesc: the Hardware Timestamp Engine (HTE) descriptor
48185ff37e3SAndy Shevchenko  * @raw_level: the line level at the time of event
48285ff37e3SAndy Shevchenko  * @total_discard_seq: the running counter of the discarded events
48385ff37e3SAndy Shevchenko  * @last_seqno: the last sequence number before debounce period expires
4843c0d9c63SKent Gibson  */
4853c0d9c63SKent Gibson struct line {
4863c0d9c63SKent Gibson 	struct gpio_desc *desc;
48773e03419SKent Gibson 	/*
48873e03419SKent Gibson 	 * -- edge detector specific fields --
48973e03419SKent Gibson 	 */
49073e03419SKent Gibson 	struct linereq *req;
49173e03419SKent Gibson 	unsigned int irq;
4923ffb7c45SKent Gibson 	/*
493b1a92e94SKent Gibson 	 * The flags for the active edge detector configuration.
494b1a92e94SKent Gibson 	 *
495b1a92e94SKent Gibson 	 * edflags is set by linereq_create(), linereq_free(), and
496b1a92e94SKent Gibson 	 * linereq_set_config_unlocked(), which are themselves mutually
497b1a92e94SKent Gibson 	 * exclusive, and is accessed by edge_irq_thread(),
498b1a92e94SKent Gibson 	 * process_hw_ts_thread() and debounce_work_func(),
499b1a92e94SKent Gibson 	 * which can all live with a slightly stale value.
5003ffb7c45SKent Gibson 	 */
501b1a92e94SKent Gibson 	u64 edflags;
50273e03419SKent Gibson 	/*
50373e03419SKent Gibson 	 * timestamp_ns and req_seqno are accessed only by
50473e03419SKent Gibson 	 * edge_irq_handler() and edge_irq_thread(), which are themselves
50573e03419SKent Gibson 	 * mutually exclusive, so no additional protection is necessary.
50673e03419SKent Gibson 	 */
50773e03419SKent Gibson 	u64 timestamp_ns;
50873e03419SKent Gibson 	u32 req_seqno;
50965cff704SKent Gibson 	/*
51065cff704SKent Gibson 	 * line_seqno is accessed by either edge_irq_thread() or
51165cff704SKent Gibson 	 * debounce_work_func(), which are themselves mutually exclusive,
51265cff704SKent Gibson 	 * so no additional protection is necessary.
51365cff704SKent Gibson 	 */
51473e03419SKent Gibson 	u32 line_seqno;
51565cff704SKent Gibson 	/*
51665cff704SKent Gibson 	 * -- debouncer specific fields --
51765cff704SKent Gibson 	 */
51865cff704SKent Gibson 	struct delayed_work work;
51965cff704SKent Gibson 	/*
52065cff704SKent Gibson 	 * sw_debounce is accessed by linereq_set_config(), which is the
52165cff704SKent Gibson 	 * only setter, and linereq_get_values(), which can live with a
52265cff704SKent Gibson 	 * slightly stale value.
52365cff704SKent Gibson 	 */
52465cff704SKent Gibson 	unsigned int sw_debounced;
52565cff704SKent Gibson 	/*
52665cff704SKent Gibson 	 * level is accessed by debounce_work_func(), which is the only
52765cff704SKent Gibson 	 * setter, and linereq_get_values() which can live with a slightly
52865cff704SKent Gibson 	 * stale value.
52965cff704SKent Gibson 	 */
53065cff704SKent Gibson 	unsigned int level;
531272ddba0SKent Gibson #ifdef CONFIG_HTE
5322068339aSDipen Patel 	struct hte_ts_desc hdesc;
5332068339aSDipen Patel 	/*
5342068339aSDipen Patel 	 * HTE provider sets line level at the time of event. The valid
5352068339aSDipen Patel 	 * value is 0 or 1 and negative value for an error.
5362068339aSDipen Patel 	 */
5372068339aSDipen Patel 	int raw_level;
5382068339aSDipen Patel 	/*
5392068339aSDipen Patel 	 * when sw_debounce is set on HTE enabled line, this is running
5402068339aSDipen Patel 	 * counter of the discarded events.
5412068339aSDipen Patel 	 */
5422068339aSDipen Patel 	u32 total_discard_seq;
5432068339aSDipen Patel 	/*
5442068339aSDipen Patel 	 * when sw_debounce is set on HTE enabled line, this variable records
5452068339aSDipen Patel 	 * last sequence number before debounce period expires.
5462068339aSDipen Patel 	 */
5472068339aSDipen Patel 	u32 last_seqno;
548272ddba0SKent Gibson #endif /* CONFIG_HTE */
5493c0d9c63SKent Gibson };
5503c0d9c63SKent Gibson 
5513c0d9c63SKent Gibson /**
5523c0d9c63SKent Gibson  * struct linereq - contains the state of a userspace line request
5533c0d9c63SKent Gibson  * @gdev: the GPIO device the line request pertains to
5543c0d9c63SKent Gibson  * @label: consumer label used to tag GPIO descriptors
5553c0d9c63SKent Gibson  * @num_lines: the number of lines in the lines array
55673e03419SKent Gibson  * @wait: wait queue that handles blocking reads of events
55773e03419SKent Gibson  * @event_buffer_size: the number of elements allocated in @events
55873e03419SKent Gibson  * @events: KFIFO for the GPIO events
55973e03419SKent Gibson  * @seqno: the sequence number for edge events generated on all lines in
56073e03419SKent Gibson  * this line request.  Note that this is not used when @num_lines is 1, as
56173e03419SKent Gibson  * the line_seqno is then the same and is cheaper to calculate.
562a54756cbSKent Gibson  * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
563a54756cbSKent Gibson  * of configuration, particularly multi-step accesses to desc flags.
5643c0d9c63SKent Gibson  * @lines: the lines held by this line request, with @num_lines elements.
5653c0d9c63SKent Gibson  */
5663c0d9c63SKent Gibson struct linereq {
5673c0d9c63SKent Gibson 	struct gpio_device *gdev;
5683c0d9c63SKent Gibson 	const char *label;
5693c0d9c63SKent Gibson 	u32 num_lines;
57073e03419SKent Gibson 	wait_queue_head_t wait;
57173e03419SKent Gibson 	u32 event_buffer_size;
57273e03419SKent Gibson 	DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
57373e03419SKent Gibson 	atomic_t seqno;
574a54756cbSKent Gibson 	struct mutex config_mutex;
5753c0d9c63SKent Gibson 	struct line lines[];
5763c0d9c63SKent Gibson };
5773c0d9c63SKent Gibson 
5783c0d9c63SKent Gibson #define GPIO_V2_LINE_BIAS_FLAGS \
5793c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
5803c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
5813c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_BIAS_DISABLED)
5823c0d9c63SKent Gibson 
5833c0d9c63SKent Gibson #define GPIO_V2_LINE_DIRECTION_FLAGS \
5843c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_INPUT | \
5853c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_OUTPUT)
5863c0d9c63SKent Gibson 
5873c0d9c63SKent Gibson #define GPIO_V2_LINE_DRIVE_FLAGS \
5883c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
5893c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_OPEN_SOURCE)
5903c0d9c63SKent Gibson 
59173e03419SKent Gibson #define GPIO_V2_LINE_EDGE_FLAGS \
59273e03419SKent Gibson 	(GPIO_V2_LINE_FLAG_EDGE_RISING | \
59373e03419SKent Gibson 	 GPIO_V2_LINE_FLAG_EDGE_FALLING)
59473e03419SKent Gibson 
5955e2ca893SKent Gibson #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS
5965e2ca893SKent Gibson 
5973c0d9c63SKent Gibson #define GPIO_V2_LINE_VALID_FLAGS \
5983c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
5993c0d9c63SKent Gibson 	 GPIO_V2_LINE_DIRECTION_FLAGS | \
6003c0d9c63SKent Gibson 	 GPIO_V2_LINE_DRIVE_FLAGS | \
60173e03419SKent Gibson 	 GPIO_V2_LINE_EDGE_FLAGS | \
60226d060e4SKent Gibson 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
6032068339aSDipen Patel 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
6043c0d9c63SKent Gibson 	 GPIO_V2_LINE_BIAS_FLAGS)
6053c0d9c63SKent Gibson 
606b1a92e94SKent Gibson /* subset of flags relevant for edge detector configuration */
607b1a92e94SKent Gibson #define GPIO_V2_LINE_EDGE_DETECTOR_FLAGS \
608b1a92e94SKent Gibson 	(GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
609b1a92e94SKent Gibson 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
610b1a92e94SKent Gibson 	 GPIO_V2_LINE_EDGE_FLAGS)
611b1a92e94SKent Gibson 
61273e03419SKent Gibson static void linereq_put_event(struct linereq *lr,
61373e03419SKent Gibson 			      struct gpio_v2_line_event *le)
61473e03419SKent Gibson {
61573e03419SKent Gibson 	bool overflow = false;
61673e03419SKent Gibson 
61773e03419SKent Gibson 	spin_lock(&lr->wait.lock);
61873e03419SKent Gibson 	if (kfifo_is_full(&lr->events)) {
61973e03419SKent Gibson 		overflow = true;
62073e03419SKent Gibson 		kfifo_skip(&lr->events);
62173e03419SKent Gibson 	}
62273e03419SKent Gibson 	kfifo_in(&lr->events, le, 1);
62373e03419SKent Gibson 	spin_unlock(&lr->wait.lock);
62473e03419SKent Gibson 	if (!overflow)
62573e03419SKent Gibson 		wake_up_poll(&lr->wait, EPOLLIN);
62673e03419SKent Gibson 	else
62773e03419SKent Gibson 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
62873e03419SKent Gibson }
62973e03419SKent Gibson 
63026d060e4SKent Gibson static u64 line_event_timestamp(struct line *line)
63126d060e4SKent Gibson {
63226d060e4SKent Gibson 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
63326d060e4SKent Gibson 		return ktime_get_real_ns();
634272ddba0SKent Gibson 	else if (IS_ENABLED(CONFIG_HTE) &&
635272ddba0SKent Gibson 		 test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))
6362068339aSDipen Patel 		return line->timestamp_ns;
63726d060e4SKent Gibson 
63826d060e4SKent Gibson 	return ktime_get_ns();
63926d060e4SKent Gibson }
64026d060e4SKent Gibson 
64124220232SKent Gibson static u32 line_event_id(int level)
64224220232SKent Gibson {
64324220232SKent Gibson 	return level ? GPIO_V2_LINE_EVENT_RISING_EDGE :
64424220232SKent Gibson 		       GPIO_V2_LINE_EVENT_FALLING_EDGE;
64524220232SKent Gibson }
64624220232SKent Gibson 
647272ddba0SKent Gibson #ifdef CONFIG_HTE
648272ddba0SKent Gibson 
6492068339aSDipen Patel static enum hte_return process_hw_ts_thread(void *p)
6502068339aSDipen Patel {
6512068339aSDipen Patel 	struct line *line;
6522068339aSDipen Patel 	struct linereq *lr;
6532068339aSDipen Patel 	struct gpio_v2_line_event le;
654b1a92e94SKent Gibson 	u64 edflags;
6552068339aSDipen Patel 	int level;
6562068339aSDipen Patel 
6572068339aSDipen Patel 	if (!p)
6582068339aSDipen Patel 		return HTE_CB_HANDLED;
6592068339aSDipen Patel 
6602068339aSDipen Patel 	line = p;
6612068339aSDipen Patel 	lr = line->req;
6622068339aSDipen Patel 
6632068339aSDipen Patel 	memset(&le, 0, sizeof(le));
6642068339aSDipen Patel 
6652068339aSDipen Patel 	le.timestamp_ns = line->timestamp_ns;
666b1a92e94SKent Gibson 	edflags = READ_ONCE(line->edflags);
6672068339aSDipen Patel 
668b1a92e94SKent Gibson 	switch (edflags & GPIO_V2_LINE_EDGE_FLAGS) {
669cfa53463SKent Gibson 	case GPIO_V2_LINE_FLAG_EDGE_BOTH:
67024220232SKent Gibson 		level = (line->raw_level >= 0) ?
67124220232SKent Gibson 				line->raw_level :
67224220232SKent Gibson 				gpiod_get_raw_value_cansleep(line->desc);
6732068339aSDipen Patel 
674b1a92e94SKent Gibson 		if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
67524220232SKent Gibson 			level = !level;
67624220232SKent Gibson 
67724220232SKent Gibson 		le.id = line_event_id(level);
678cfa53463SKent Gibson 		break;
679cfa53463SKent Gibson 	case GPIO_V2_LINE_FLAG_EDGE_RISING:
6802068339aSDipen Patel 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
681cfa53463SKent Gibson 		break;
682cfa53463SKent Gibson 	case GPIO_V2_LINE_FLAG_EDGE_FALLING:
6832068339aSDipen Patel 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
684cfa53463SKent Gibson 		break;
685cfa53463SKent Gibson 	default:
6862068339aSDipen Patel 		return HTE_CB_HANDLED;
6872068339aSDipen Patel 	}
6882068339aSDipen Patel 	le.line_seqno = line->line_seqno;
6892068339aSDipen Patel 	le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
6902068339aSDipen Patel 	le.offset = gpio_chip_hwgpio(line->desc);
6912068339aSDipen Patel 
6922068339aSDipen Patel 	linereq_put_event(lr, &le);
6932068339aSDipen Patel 
6942068339aSDipen Patel 	return HTE_CB_HANDLED;
6952068339aSDipen Patel }
6962068339aSDipen Patel 
6972068339aSDipen Patel static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p)
6982068339aSDipen Patel {
6992068339aSDipen Patel 	struct line *line;
7002068339aSDipen Patel 	struct linereq *lr;
7012068339aSDipen Patel 	int diff_seqno = 0;
7022068339aSDipen Patel 
7032068339aSDipen Patel 	if (!ts || !p)
7042068339aSDipen Patel 		return HTE_CB_HANDLED;
7052068339aSDipen Patel 
7062068339aSDipen Patel 	line = p;
7072068339aSDipen Patel 	line->timestamp_ns = ts->tsc;
7082068339aSDipen Patel 	line->raw_level = ts->raw_level;
7092068339aSDipen Patel 	lr = line->req;
7102068339aSDipen Patel 
7112068339aSDipen Patel 	if (READ_ONCE(line->sw_debounced)) {
7122068339aSDipen Patel 		line->total_discard_seq++;
7132068339aSDipen Patel 		line->last_seqno = ts->seq;
7142068339aSDipen Patel 		mod_delayed_work(system_wq, &line->work,
7152068339aSDipen Patel 		  usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
7162068339aSDipen Patel 	} else {
7172068339aSDipen Patel 		if (unlikely(ts->seq < line->line_seqno))
7182068339aSDipen Patel 			return HTE_CB_HANDLED;
7192068339aSDipen Patel 
7202068339aSDipen Patel 		diff_seqno = ts->seq - line->line_seqno;
7212068339aSDipen Patel 		line->line_seqno = ts->seq;
7222068339aSDipen Patel 		if (lr->num_lines != 1)
7232068339aSDipen Patel 			line->req_seqno = atomic_add_return(diff_seqno,
7242068339aSDipen Patel 							    &lr->seqno);
7252068339aSDipen Patel 
7262068339aSDipen Patel 		return HTE_RUN_SECOND_CB;
7272068339aSDipen Patel 	}
7282068339aSDipen Patel 
7292068339aSDipen Patel 	return HTE_CB_HANDLED;
7302068339aSDipen Patel }
7312068339aSDipen Patel 
732272ddba0SKent Gibson static int hte_edge_setup(struct line *line, u64 eflags)
733272ddba0SKent Gibson {
734272ddba0SKent Gibson 	int ret;
735272ddba0SKent Gibson 	unsigned long flags = 0;
736272ddba0SKent Gibson 	struct hte_ts_desc *hdesc = &line->hdesc;
737272ddba0SKent Gibson 
738272ddba0SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
739272ddba0SKent Gibson 		flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
740272ddba0SKent Gibson 				 HTE_FALLING_EDGE_TS :
741272ddba0SKent Gibson 				 HTE_RISING_EDGE_TS;
742272ddba0SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
743272ddba0SKent Gibson 		flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
744272ddba0SKent Gibson 				 HTE_RISING_EDGE_TS :
745272ddba0SKent Gibson 				 HTE_FALLING_EDGE_TS;
746272ddba0SKent Gibson 
747272ddba0SKent Gibson 	line->total_discard_seq = 0;
748272ddba0SKent Gibson 
749272ddba0SKent Gibson 	hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags, NULL,
750272ddba0SKent Gibson 			   line->desc);
751272ddba0SKent Gibson 
752272ddba0SKent Gibson 	ret = hte_ts_get(NULL, hdesc, 0);
753272ddba0SKent Gibson 	if (ret)
754272ddba0SKent Gibson 		return ret;
755272ddba0SKent Gibson 
756272ddba0SKent Gibson 	return hte_request_ts_ns(hdesc, process_hw_ts, process_hw_ts_thread,
757272ddba0SKent Gibson 				 line);
758272ddba0SKent Gibson }
759272ddba0SKent Gibson 
760272ddba0SKent Gibson #else
761272ddba0SKent Gibson 
762272ddba0SKent Gibson static int hte_edge_setup(struct line *line, u64 eflags)
763272ddba0SKent Gibson {
764272ddba0SKent Gibson 	return 0;
765272ddba0SKent Gibson }
766272ddba0SKent Gibson #endif /* CONFIG_HTE */
767272ddba0SKent Gibson 
76873e03419SKent Gibson static irqreturn_t edge_irq_thread(int irq, void *p)
76973e03419SKent Gibson {
77073e03419SKent Gibson 	struct line *line = p;
77173e03419SKent Gibson 	struct linereq *lr = line->req;
77273e03419SKent Gibson 	struct gpio_v2_line_event le;
77373e03419SKent Gibson 
77473e03419SKent Gibson 	/* Do not leak kernel stack to userspace */
77573e03419SKent Gibson 	memset(&le, 0, sizeof(le));
77673e03419SKent Gibson 
77773e03419SKent Gibson 	if (line->timestamp_ns) {
77873e03419SKent Gibson 		le.timestamp_ns = line->timestamp_ns;
77973e03419SKent Gibson 	} else {
78073e03419SKent Gibson 		/*
78173e03419SKent Gibson 		 * We may be running from a nested threaded interrupt in
78273e03419SKent Gibson 		 * which case we didn't get the timestamp from
78373e03419SKent Gibson 		 * edge_irq_handler().
78473e03419SKent Gibson 		 */
78526d060e4SKent Gibson 		le.timestamp_ns = line_event_timestamp(line);
78673e03419SKent Gibson 		if (lr->num_lines != 1)
78773e03419SKent Gibson 			line->req_seqno = atomic_inc_return(&lr->seqno);
78873e03419SKent Gibson 	}
78973e03419SKent Gibson 	line->timestamp_ns = 0;
79073e03419SKent Gibson 
791b1a92e94SKent Gibson 	switch (READ_ONCE(line->edflags) & GPIO_V2_LINE_EDGE_FLAGS) {
792cfa53463SKent Gibson 	case GPIO_V2_LINE_FLAG_EDGE_BOTH:
79324220232SKent Gibson 		le.id = line_event_id(gpiod_get_value_cansleep(line->desc));
794cfa53463SKent Gibson 		break;
795cfa53463SKent Gibson 	case GPIO_V2_LINE_FLAG_EDGE_RISING:
79673e03419SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
797cfa53463SKent Gibson 		break;
798cfa53463SKent Gibson 	case GPIO_V2_LINE_FLAG_EDGE_FALLING:
79973e03419SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
800cfa53463SKent Gibson 		break;
801cfa53463SKent Gibson 	default:
80273e03419SKent Gibson 		return IRQ_NONE;
80373e03419SKent Gibson 	}
80473e03419SKent Gibson 	line->line_seqno++;
80573e03419SKent Gibson 	le.line_seqno = line->line_seqno;
80673e03419SKent Gibson 	le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
80773e03419SKent Gibson 	le.offset = gpio_chip_hwgpio(line->desc);
80873e03419SKent Gibson 
80973e03419SKent Gibson 	linereq_put_event(lr, &le);
81073e03419SKent Gibson 
81173e03419SKent Gibson 	return IRQ_HANDLED;
81273e03419SKent Gibson }
81373e03419SKent Gibson 
81473e03419SKent Gibson static irqreturn_t edge_irq_handler(int irq, void *p)
81573e03419SKent Gibson {
81673e03419SKent Gibson 	struct line *line = p;
81773e03419SKent Gibson 	struct linereq *lr = line->req;
81873e03419SKent Gibson 
81973e03419SKent Gibson 	/*
82073e03419SKent Gibson 	 * Just store the timestamp in hardirq context so we get it as
82173e03419SKent Gibson 	 * close in time as possible to the actual event.
82273e03419SKent Gibson 	 */
82326d060e4SKent Gibson 	line->timestamp_ns = line_event_timestamp(line);
82473e03419SKent Gibson 
82573e03419SKent Gibson 	if (lr->num_lines != 1)
82673e03419SKent Gibson 		line->req_seqno = atomic_inc_return(&lr->seqno);
82773e03419SKent Gibson 
82873e03419SKent Gibson 	return IRQ_WAKE_THREAD;
82973e03419SKent Gibson }
83073e03419SKent Gibson 
83165cff704SKent Gibson /*
83265cff704SKent Gibson  * returns the current debounced logical value.
83365cff704SKent Gibson  */
83465cff704SKent Gibson static bool debounced_value(struct line *line)
83565cff704SKent Gibson {
83665cff704SKent Gibson 	bool value;
83765cff704SKent Gibson 
83865cff704SKent Gibson 	/*
83965cff704SKent Gibson 	 * minor race - debouncer may be stopped here, so edge_detector_stop()
84065cff704SKent Gibson 	 * must leave the value unchanged so the following will read the level
84165cff704SKent Gibson 	 * from when the debouncer was last running.
84265cff704SKent Gibson 	 */
84365cff704SKent Gibson 	value = READ_ONCE(line->level);
84465cff704SKent Gibson 
84565cff704SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
84665cff704SKent Gibson 		value = !value;
84765cff704SKent Gibson 
84865cff704SKent Gibson 	return value;
84965cff704SKent Gibson }
85065cff704SKent Gibson 
85165cff704SKent Gibson static irqreturn_t debounce_irq_handler(int irq, void *p)
85265cff704SKent Gibson {
85365cff704SKent Gibson 	struct line *line = p;
85465cff704SKent Gibson 
85565cff704SKent Gibson 	mod_delayed_work(system_wq, &line->work,
85665cff704SKent Gibson 		usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
85765cff704SKent Gibson 
85865cff704SKent Gibson 	return IRQ_HANDLED;
85965cff704SKent Gibson }
86065cff704SKent Gibson 
86165cff704SKent Gibson static void debounce_work_func(struct work_struct *work)
86265cff704SKent Gibson {
86365cff704SKent Gibson 	struct gpio_v2_line_event le;
86465cff704SKent Gibson 	struct line *line = container_of(work, struct line, work.work);
86565cff704SKent Gibson 	struct linereq *lr;
866b1a92e94SKent Gibson 	u64 eflags, edflags = READ_ONCE(line->edflags);
867272ddba0SKent Gibson 	int level = -1;
868272ddba0SKent Gibson #ifdef CONFIG_HTE
869272ddba0SKent Gibson 	int diff_seqno;
87065cff704SKent Gibson 
871b1a92e94SKent Gibson 	if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
8722068339aSDipen Patel 		level = line->raw_level;
873272ddba0SKent Gibson #endif
8742068339aSDipen Patel 	if (level < 0)
87565cff704SKent Gibson 		level = gpiod_get_raw_value_cansleep(line->desc);
87665cff704SKent Gibson 	if (level < 0) {
87765cff704SKent Gibson 		pr_debug_ratelimited("debouncer failed to read line value\n");
87865cff704SKent Gibson 		return;
87965cff704SKent Gibson 	}
88065cff704SKent Gibson 
88165cff704SKent Gibson 	if (READ_ONCE(line->level) == level)
88265cff704SKent Gibson 		return;
88365cff704SKent Gibson 
88465cff704SKent Gibson 	WRITE_ONCE(line->level, level);
88565cff704SKent Gibson 
88665cff704SKent Gibson 	/* -- edge detection -- */
887b1a92e94SKent Gibson 	eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
8883ffb7c45SKent Gibson 	if (!eflags)
88965cff704SKent Gibson 		return;
89065cff704SKent Gibson 
89165cff704SKent Gibson 	/* switch from physical level to logical - if they differ */
892b1a92e94SKent Gibson 	if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
89365cff704SKent Gibson 		level = !level;
89465cff704SKent Gibson 
89565cff704SKent Gibson 	/* ignore edges that are not being monitored */
8963ffb7c45SKent Gibson 	if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
8973ffb7c45SKent Gibson 	    ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
89865cff704SKent Gibson 		return;
89965cff704SKent Gibson 
90065cff704SKent Gibson 	/* Do not leak kernel stack to userspace */
90165cff704SKent Gibson 	memset(&le, 0, sizeof(le));
90265cff704SKent Gibson 
90365cff704SKent Gibson 	lr = line->req;
90426d060e4SKent Gibson 	le.timestamp_ns = line_event_timestamp(line);
90565cff704SKent Gibson 	le.offset = gpio_chip_hwgpio(line->desc);
906272ddba0SKent Gibson #ifdef CONFIG_HTE
907b1a92e94SKent Gibson 	if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) {
9082068339aSDipen Patel 		/* discard events except the last one */
9092068339aSDipen Patel 		line->total_discard_seq -= 1;
9102068339aSDipen Patel 		diff_seqno = line->last_seqno - line->total_discard_seq -
9112068339aSDipen Patel 				line->line_seqno;
9122068339aSDipen Patel 		line->line_seqno = line->last_seqno - line->total_discard_seq;
9132068339aSDipen Patel 		le.line_seqno = line->line_seqno;
9142068339aSDipen Patel 		le.seqno = (lr->num_lines == 1) ?
9152068339aSDipen Patel 			le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno);
916272ddba0SKent Gibson 	} else
917272ddba0SKent Gibson #endif /* CONFIG_HTE */
918272ddba0SKent Gibson 	{
91965cff704SKent Gibson 		line->line_seqno++;
92065cff704SKent Gibson 		le.line_seqno = line->line_seqno;
92165cff704SKent Gibson 		le.seqno = (lr->num_lines == 1) ?
92265cff704SKent Gibson 			le.line_seqno : atomic_inc_return(&lr->seqno);
9232068339aSDipen Patel 	}
92465cff704SKent Gibson 
92524220232SKent Gibson 	le.id = line_event_id(level);
92665cff704SKent Gibson 
92765cff704SKent Gibson 	linereq_put_event(lr, &le);
92865cff704SKent Gibson }
92965cff704SKent Gibson 
930b1a92e94SKent Gibson static int debounce_setup(struct line *line, unsigned int debounce_period_us)
93165cff704SKent Gibson {
93265cff704SKent Gibson 	unsigned long irqflags;
93365cff704SKent Gibson 	int ret, level, irq;
93465cff704SKent Gibson 
93565cff704SKent Gibson 	/* try hardware */
93665cff704SKent Gibson 	ret = gpiod_set_debounce(line->desc, debounce_period_us);
93765cff704SKent Gibson 	if (!ret) {
93865cff704SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
93965cff704SKent Gibson 		return ret;
94065cff704SKent Gibson 	}
94165cff704SKent Gibson 	if (ret != -ENOTSUPP)
94265cff704SKent Gibson 		return ret;
94365cff704SKent Gibson 
94465cff704SKent Gibson 	if (debounce_period_us) {
94565cff704SKent Gibson 		/* setup software debounce */
94665cff704SKent Gibson 		level = gpiod_get_raw_value_cansleep(line->desc);
94765cff704SKent Gibson 		if (level < 0)
94865cff704SKent Gibson 			return level;
94965cff704SKent Gibson 
950272ddba0SKent Gibson 		if (!(IS_ENABLED(CONFIG_HTE) &&
951272ddba0SKent Gibson 		      test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))) {
95265cff704SKent Gibson 			irq = gpiod_to_irq(line->desc);
95365cff704SKent Gibson 			if (irq < 0)
95465cff704SKent Gibson 				return -ENXIO;
95565cff704SKent Gibson 
95665cff704SKent Gibson 			irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
95765cff704SKent Gibson 			ret = request_irq(irq, debounce_irq_handler, irqflags,
95865cff704SKent Gibson 					  line->req->label, line);
95965cff704SKent Gibson 			if (ret)
96065cff704SKent Gibson 				return ret;
96165cff704SKent Gibson 			line->irq = irq;
9622068339aSDipen Patel 		} else {
9632487a812SKent Gibson 			ret = hte_edge_setup(line, GPIO_V2_LINE_FLAG_EDGE_BOTH);
9642068339aSDipen Patel 			if (ret)
9652068339aSDipen Patel 				return ret;
9662068339aSDipen Patel 		}
9672068339aSDipen Patel 
9682068339aSDipen Patel 		WRITE_ONCE(line->level, level);
9692068339aSDipen Patel 		WRITE_ONCE(line->sw_debounced, 1);
97065cff704SKent Gibson 	}
97165cff704SKent Gibson 	return 0;
97265cff704SKent Gibson }
97365cff704SKent Gibson 
97465cff704SKent Gibson static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
97565cff704SKent Gibson 					  unsigned int line_idx)
97665cff704SKent Gibson {
97765cff704SKent Gibson 	unsigned int i;
97865cff704SKent Gibson 	u64 mask = BIT_ULL(line_idx);
97965cff704SKent Gibson 
98065cff704SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
98165cff704SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
98265cff704SKent Gibson 		    (lc->attrs[i].mask & mask))
98365cff704SKent Gibson 			return true;
98465cff704SKent Gibson 	}
98565cff704SKent Gibson 	return false;
98665cff704SKent Gibson }
98765cff704SKent Gibson 
98865cff704SKent Gibson static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
98965cff704SKent Gibson 					       unsigned int line_idx)
99065cff704SKent Gibson {
99165cff704SKent Gibson 	unsigned int i;
99265cff704SKent Gibson 	u64 mask = BIT_ULL(line_idx);
99365cff704SKent Gibson 
99465cff704SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
99565cff704SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
99665cff704SKent Gibson 		    (lc->attrs[i].mask & mask))
99765cff704SKent Gibson 			return lc->attrs[i].attr.debounce_period_us;
99865cff704SKent Gibson 	}
99965cff704SKent Gibson 	return 0;
100065cff704SKent Gibson }
100165cff704SKent Gibson 
1002b1a92e94SKent Gibson static void edge_detector_stop(struct line *line)
100373e03419SKent Gibson {
1004b1a92e94SKent Gibson 	if (line->irq) {
100573e03419SKent Gibson 		free_irq(line->irq, line);
100673e03419SKent Gibson 		line->irq = 0;
100773e03419SKent Gibson 	}
1008a54756cbSKent Gibson 
1009272ddba0SKent Gibson #ifdef CONFIG_HTE
1010b1a92e94SKent Gibson 	if (READ_ONCE(line->edflags) & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
10112068339aSDipen Patel 		hte_ts_put(&line->hdesc);
1012272ddba0SKent Gibson #endif
10132068339aSDipen Patel 
101465cff704SKent Gibson 	cancel_delayed_work_sync(&line->work);
101565cff704SKent Gibson 	WRITE_ONCE(line->sw_debounced, 0);
1016b1a92e94SKent Gibson 	WRITE_ONCE(line->edflags, 0);
101703a58ea5SKent Gibson 	if (line->desc)
101803a58ea5SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, 0);
101965cff704SKent Gibson 	/* do not change line->level - see comment in debounced_value() */
102073e03419SKent Gibson }
102173e03419SKent Gibson 
102273e03419SKent Gibson static int edge_detector_setup(struct line *line,
102365cff704SKent Gibson 			       struct gpio_v2_line_config *lc,
1024b1a92e94SKent Gibson 			       unsigned int line_idx, u64 edflags)
102573e03419SKent Gibson {
102665cff704SKent Gibson 	u32 debounce_period_us;
102773e03419SKent Gibson 	unsigned long irqflags = 0;
1028b1a92e94SKent Gibson 	u64 eflags;
102973e03419SKent Gibson 	int irq, ret;
103073e03419SKent Gibson 
1031b1a92e94SKent Gibson 	eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
103273e03419SKent Gibson 	if (eflags && !kfifo_initialized(&line->req->events)) {
103373e03419SKent Gibson 		ret = kfifo_alloc(&line->req->events,
103473e03419SKent Gibson 				  line->req->event_buffer_size, GFP_KERNEL);
103573e03419SKent Gibson 		if (ret)
103673e03419SKent Gibson 			return ret;
103773e03419SKent Gibson 	}
103865cff704SKent Gibson 	if (gpio_v2_line_config_debounced(lc, line_idx)) {
103965cff704SKent Gibson 		debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
1040b1a92e94SKent Gibson 		ret = debounce_setup(line, debounce_period_us);
104165cff704SKent Gibson 		if (ret)
104265cff704SKent Gibson 			return ret;
104365cff704SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
104465cff704SKent Gibson 	}
104573e03419SKent Gibson 
104665cff704SKent Gibson 	/* detection disabled or sw debouncer will provide edge detection */
104765cff704SKent Gibson 	if (!eflags || READ_ONCE(line->sw_debounced))
104873e03419SKent Gibson 		return 0;
104973e03419SKent Gibson 
1050272ddba0SKent Gibson 	if (IS_ENABLED(CONFIG_HTE) &&
1051272ddba0SKent Gibson 	    (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1052b1a92e94SKent Gibson 		return hte_edge_setup(line, edflags);
10532068339aSDipen Patel 
105473e03419SKent Gibson 	irq = gpiod_to_irq(line->desc);
105573e03419SKent Gibson 	if (irq < 0)
105673e03419SKent Gibson 		return -ENXIO;
105773e03419SKent Gibson 
105873e03419SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
105973e03419SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
106073e03419SKent Gibson 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
106173e03419SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
106273e03419SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
106373e03419SKent Gibson 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
106473e03419SKent Gibson 	irqflags |= IRQF_ONESHOT;
106573e03419SKent Gibson 
106673e03419SKent Gibson 	/* Request a thread to read the events */
106773e03419SKent Gibson 	ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
106873e03419SKent Gibson 				   irqflags, line->req->label, line);
106973e03419SKent Gibson 	if (ret)
107073e03419SKent Gibson 		return ret;
107173e03419SKent Gibson 
107273e03419SKent Gibson 	line->irq = irq;
107373e03419SKent Gibson 	return 0;
107473e03419SKent Gibson }
107573e03419SKent Gibson 
107665cff704SKent Gibson static int edge_detector_update(struct line *line,
107765cff704SKent Gibson 				struct gpio_v2_line_config *lc,
1078b1a92e94SKent Gibson 				unsigned int line_idx, u64 edflags)
1079a54756cbSKent Gibson {
1080b1a92e94SKent Gibson 	u64 active_edflags = READ_ONCE(line->edflags);
108165cff704SKent Gibson 	unsigned int debounce_period_us =
108265cff704SKent Gibson 			gpio_v2_line_config_debounce_period(lc, line_idx);
108365cff704SKent Gibson 
1084b1a92e94SKent Gibson 	if ((active_edflags == edflags) &&
1085b1a92e94SKent Gibson 	    (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us))
1086a54756cbSKent Gibson 		return 0;
1087a54756cbSKent Gibson 
108865cff704SKent Gibson 	/* sw debounced and still will be...*/
108965cff704SKent Gibson 	if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
109065cff704SKent Gibson 		WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
109165cff704SKent Gibson 		return 0;
109265cff704SKent Gibson 	}
109365cff704SKent Gibson 
109465cff704SKent Gibson 	/* reconfiguring edge detection or sw debounce being disabled */
1095b1a92e94SKent Gibson 	if ((line->irq && !READ_ONCE(line->sw_debounced)) ||
1096b1a92e94SKent Gibson 	    (active_edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) ||
109765cff704SKent Gibson 	    (!debounce_period_us && READ_ONCE(line->sw_debounced)))
1098b1a92e94SKent Gibson 		edge_detector_stop(line);
1099a54756cbSKent Gibson 
1100b1a92e94SKent Gibson 	return edge_detector_setup(line, lc, line_idx, edflags);
1101a54756cbSKent Gibson }
1102a54756cbSKent Gibson 
11033c0d9c63SKent Gibson static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
11043c0d9c63SKent Gibson 				     unsigned int line_idx)
11053c0d9c63SKent Gibson {
11063c0d9c63SKent Gibson 	unsigned int i;
11073c0d9c63SKent Gibson 	u64 mask = BIT_ULL(line_idx);
11083c0d9c63SKent Gibson 
11093c0d9c63SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
11103c0d9c63SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
11113c0d9c63SKent Gibson 		    (lc->attrs[i].mask & mask))
11123c0d9c63SKent Gibson 			return lc->attrs[i].attr.flags;
11133c0d9c63SKent Gibson 	}
11143c0d9c63SKent Gibson 	return lc->flags;
11153c0d9c63SKent Gibson }
11163c0d9c63SKent Gibson 
11173c0d9c63SKent Gibson static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
11183c0d9c63SKent Gibson 					    unsigned int line_idx)
11193c0d9c63SKent Gibson {
11203c0d9c63SKent Gibson 	unsigned int i;
11213c0d9c63SKent Gibson 	u64 mask = BIT_ULL(line_idx);
11223c0d9c63SKent Gibson 
11233c0d9c63SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
11243c0d9c63SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
11253c0d9c63SKent Gibson 		    (lc->attrs[i].mask & mask))
11263c0d9c63SKent Gibson 			return !!(lc->attrs[i].attr.values & mask);
11273c0d9c63SKent Gibson 	}
11283c0d9c63SKent Gibson 	return 0;
11293c0d9c63SKent Gibson }
11303c0d9c63SKent Gibson 
11313c0d9c63SKent Gibson static int gpio_v2_line_flags_validate(u64 flags)
11323c0d9c63SKent Gibson {
11333c0d9c63SKent Gibson 	/* Return an error if an unknown flag is set */
11343c0d9c63SKent Gibson 	if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
11353c0d9c63SKent Gibson 		return -EINVAL;
1136272ddba0SKent Gibson 
1137272ddba0SKent Gibson 	if (!IS_ENABLED(CONFIG_HTE) &&
1138272ddba0SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1139272ddba0SKent Gibson 		return -EOPNOTSUPP;
1140272ddba0SKent Gibson 
11413c0d9c63SKent Gibson 	/*
11423c0d9c63SKent Gibson 	 * Do not allow both INPUT and OUTPUT flags to be set as they are
11433c0d9c63SKent Gibson 	 * contradictory.
11443c0d9c63SKent Gibson 	 */
11453c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
11463c0d9c63SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_OUTPUT))
11473c0d9c63SKent Gibson 		return -EINVAL;
11483c0d9c63SKent Gibson 
11492068339aSDipen Patel 	/* Only allow one event clock source */
1150272ddba0SKent Gibson 	if (IS_ENABLED(CONFIG_HTE) &&
1151272ddba0SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) &&
11522068339aSDipen Patel 	    (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
11532068339aSDipen Patel 		return -EINVAL;
11542068339aSDipen Patel 
115573e03419SKent Gibson 	/* Edge detection requires explicit input. */
115673e03419SKent Gibson 	if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
115773e03419SKent Gibson 	    !(flags & GPIO_V2_LINE_FLAG_INPUT))
115873e03419SKent Gibson 		return -EINVAL;
115973e03419SKent Gibson 
11603c0d9c63SKent Gibson 	/*
11613c0d9c63SKent Gibson 	 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
11623c0d9c63SKent Gibson 	 * request. If the hardware actually supports enabling both at the
11633c0d9c63SKent Gibson 	 * same time the electrical result would be disastrous.
11643c0d9c63SKent Gibson 	 */
11653c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
11663c0d9c63SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
11673c0d9c63SKent Gibson 		return -EINVAL;
11683c0d9c63SKent Gibson 
11693c0d9c63SKent Gibson 	/* Drive requires explicit output direction. */
11703c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
11713c0d9c63SKent Gibson 	    !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
11723c0d9c63SKent Gibson 		return -EINVAL;
11733c0d9c63SKent Gibson 
11743c0d9c63SKent Gibson 	/* Bias requires explicit direction. */
11753c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
11763c0d9c63SKent Gibson 	    !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
11773c0d9c63SKent Gibson 		return -EINVAL;
11783c0d9c63SKent Gibson 
11793c0d9c63SKent Gibson 	/* Only one bias flag can be set. */
11803c0d9c63SKent Gibson 	if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
11813c0d9c63SKent Gibson 	     (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
11823c0d9c63SKent Gibson 		       GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
11833c0d9c63SKent Gibson 	    ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
11843c0d9c63SKent Gibson 	     (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
11853c0d9c63SKent Gibson 		return -EINVAL;
11863c0d9c63SKent Gibson 
11873c0d9c63SKent Gibson 	return 0;
11883c0d9c63SKent Gibson }
11893c0d9c63SKent Gibson 
11903c0d9c63SKent Gibson static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
11913c0d9c63SKent Gibson 					unsigned int num_lines)
11923c0d9c63SKent Gibson {
11933c0d9c63SKent Gibson 	unsigned int i;
11943c0d9c63SKent Gibson 	u64 flags;
11953c0d9c63SKent Gibson 	int ret;
11963c0d9c63SKent Gibson 
11973c0d9c63SKent Gibson 	if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
11983c0d9c63SKent Gibson 		return -EINVAL;
11993c0d9c63SKent Gibson 
12003c0d9c63SKent Gibson 	if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
12013c0d9c63SKent Gibson 		return -EINVAL;
12023c0d9c63SKent Gibson 
12033c0d9c63SKent Gibson 	for (i = 0; i < num_lines; i++) {
12043c0d9c63SKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
12053c0d9c63SKent Gibson 		ret = gpio_v2_line_flags_validate(flags);
12063c0d9c63SKent Gibson 		if (ret)
12073c0d9c63SKent Gibson 			return ret;
120865cff704SKent Gibson 
120965cff704SKent Gibson 		/* debounce requires explicit input */
121065cff704SKent Gibson 		if (gpio_v2_line_config_debounced(lc, i) &&
121165cff704SKent Gibson 		    !(flags & GPIO_V2_LINE_FLAG_INPUT))
121265cff704SKent Gibson 			return -EINVAL;
12133c0d9c63SKent Gibson 	}
12143c0d9c63SKent Gibson 	return 0;
12153c0d9c63SKent Gibson }
12163c0d9c63SKent Gibson 
12173c0d9c63SKent Gibson static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
12183c0d9c63SKent Gibson 						    unsigned long *flagsp)
12193c0d9c63SKent Gibson {
12203c0d9c63SKent Gibson 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
12213c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
12223c0d9c63SKent Gibson 
12233c0d9c63SKent Gibson 	if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
12243c0d9c63SKent Gibson 		set_bit(FLAG_IS_OUT, flagsp);
12253c0d9c63SKent Gibson 	else if (flags & GPIO_V2_LINE_FLAG_INPUT)
12263c0d9c63SKent Gibson 		clear_bit(FLAG_IS_OUT, flagsp);
12273c0d9c63SKent Gibson 
122873e03419SKent Gibson 	assign_bit(FLAG_EDGE_RISING, flagsp,
122973e03419SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
123073e03419SKent Gibson 	assign_bit(FLAG_EDGE_FALLING, flagsp,
123173e03419SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
123273e03419SKent Gibson 
12333c0d9c63SKent Gibson 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
12343c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
12353c0d9c63SKent Gibson 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
12363c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
12373c0d9c63SKent Gibson 
12383c0d9c63SKent Gibson 	assign_bit(FLAG_PULL_UP, flagsp,
12393c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
12403c0d9c63SKent Gibson 	assign_bit(FLAG_PULL_DOWN, flagsp,
12413c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
12423c0d9c63SKent Gibson 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
12433c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
124426d060e4SKent Gibson 
124526d060e4SKent Gibson 	assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
124626d060e4SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
12472068339aSDipen Patel 	assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp,
12482068339aSDipen Patel 		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
12493c0d9c63SKent Gibson }
12503c0d9c63SKent Gibson 
12513c0d9c63SKent Gibson static long linereq_get_values(struct linereq *lr, void __user *ip)
12523c0d9c63SKent Gibson {
12533c0d9c63SKent Gibson 	struct gpio_v2_line_values lv;
12543c0d9c63SKent Gibson 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
12553c0d9c63SKent Gibson 	struct gpio_desc **descs;
12563c0d9c63SKent Gibson 	unsigned int i, didx, num_get;
125765cff704SKent Gibson 	bool val;
12583c0d9c63SKent Gibson 	int ret;
12593c0d9c63SKent Gibson 
12603c0d9c63SKent Gibson 	/* NOTE: It's ok to read values of output lines. */
12613c0d9c63SKent Gibson 	if (copy_from_user(&lv, ip, sizeof(lv)))
12623c0d9c63SKent Gibson 		return -EFAULT;
12633c0d9c63SKent Gibson 
12643c0d9c63SKent Gibson 	for (num_get = 0, i = 0; i < lr->num_lines; i++) {
12653c0d9c63SKent Gibson 		if (lv.mask & BIT_ULL(i)) {
12663c0d9c63SKent Gibson 			num_get++;
12673c0d9c63SKent Gibson 			descs = &lr->lines[i].desc;
12683c0d9c63SKent Gibson 		}
12693c0d9c63SKent Gibson 	}
12703c0d9c63SKent Gibson 
12713c0d9c63SKent Gibson 	if (num_get == 0)
12723c0d9c63SKent Gibson 		return -EINVAL;
12733c0d9c63SKent Gibson 
12743c0d9c63SKent Gibson 	if (num_get != 1) {
12753c0d9c63SKent Gibson 		descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
12763c0d9c63SKent Gibson 		if (!descs)
12773c0d9c63SKent Gibson 			return -ENOMEM;
12783c0d9c63SKent Gibson 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
12793c0d9c63SKent Gibson 			if (lv.mask & BIT_ULL(i)) {
12803c0d9c63SKent Gibson 				descs[didx] = lr->lines[i].desc;
12813c0d9c63SKent Gibson 				didx++;
12823c0d9c63SKent Gibson 			}
12833c0d9c63SKent Gibson 		}
12843c0d9c63SKent Gibson 	}
12853c0d9c63SKent Gibson 	ret = gpiod_get_array_value_complex(false, true, num_get,
12863c0d9c63SKent Gibson 					    descs, NULL, vals);
12873c0d9c63SKent Gibson 
12883c0d9c63SKent Gibson 	if (num_get != 1)
12893c0d9c63SKent Gibson 		kfree(descs);
12903c0d9c63SKent Gibson 	if (ret)
12913c0d9c63SKent Gibson 		return ret;
12923c0d9c63SKent Gibson 
12933c0d9c63SKent Gibson 	lv.bits = 0;
12943c0d9c63SKent Gibson 	for (didx = 0, i = 0; i < lr->num_lines; i++) {
12953c0d9c63SKent Gibson 		if (lv.mask & BIT_ULL(i)) {
129665cff704SKent Gibson 			if (lr->lines[i].sw_debounced)
129765cff704SKent Gibson 				val = debounced_value(&lr->lines[i]);
129865cff704SKent Gibson 			else
129965cff704SKent Gibson 				val = test_bit(didx, vals);
130065cff704SKent Gibson 			if (val)
13013c0d9c63SKent Gibson 				lv.bits |= BIT_ULL(i);
13023c0d9c63SKent Gibson 			didx++;
13033c0d9c63SKent Gibson 		}
13043c0d9c63SKent Gibson 	}
13053c0d9c63SKent Gibson 
13063c0d9c63SKent Gibson 	if (copy_to_user(ip, &lv, sizeof(lv)))
13073c0d9c63SKent Gibson 		return -EFAULT;
13083c0d9c63SKent Gibson 
13093c0d9c63SKent Gibson 	return 0;
13103c0d9c63SKent Gibson }
13113c0d9c63SKent Gibson 
13127b8e00d9SKent Gibson static long linereq_set_values_unlocked(struct linereq *lr,
13137b8e00d9SKent Gibson 					struct gpio_v2_line_values *lv)
13147b8e00d9SKent Gibson {
13157b8e00d9SKent Gibson 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
13167b8e00d9SKent Gibson 	struct gpio_desc **descs;
13177b8e00d9SKent Gibson 	unsigned int i, didx, num_set;
13187b8e00d9SKent Gibson 	int ret;
13197b8e00d9SKent Gibson 
13207b8e00d9SKent Gibson 	bitmap_zero(vals, GPIO_V2_LINES_MAX);
13217b8e00d9SKent Gibson 	for (num_set = 0, i = 0; i < lr->num_lines; i++) {
13227b8e00d9SKent Gibson 		if (lv->mask & BIT_ULL(i)) {
13237b8e00d9SKent Gibson 			if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
13247b8e00d9SKent Gibson 				return -EPERM;
13257b8e00d9SKent Gibson 			if (lv->bits & BIT_ULL(i))
13267b8e00d9SKent Gibson 				__set_bit(num_set, vals);
13277b8e00d9SKent Gibson 			num_set++;
13287b8e00d9SKent Gibson 			descs = &lr->lines[i].desc;
13297b8e00d9SKent Gibson 		}
13307b8e00d9SKent Gibson 	}
13317b8e00d9SKent Gibson 	if (num_set == 0)
13327b8e00d9SKent Gibson 		return -EINVAL;
13337b8e00d9SKent Gibson 
13347b8e00d9SKent Gibson 	if (num_set != 1) {
13357b8e00d9SKent Gibson 		/* build compacted desc array and values */
13367b8e00d9SKent Gibson 		descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
13377b8e00d9SKent Gibson 		if (!descs)
13387b8e00d9SKent Gibson 			return -ENOMEM;
13397b8e00d9SKent Gibson 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
13407b8e00d9SKent Gibson 			if (lv->mask & BIT_ULL(i)) {
13417b8e00d9SKent Gibson 				descs[didx] = lr->lines[i].desc;
13427b8e00d9SKent Gibson 				didx++;
13437b8e00d9SKent Gibson 			}
13447b8e00d9SKent Gibson 		}
13457b8e00d9SKent Gibson 	}
13467b8e00d9SKent Gibson 	ret = gpiod_set_array_value_complex(false, true, num_set,
13477b8e00d9SKent Gibson 					    descs, NULL, vals);
13487b8e00d9SKent Gibson 
13497b8e00d9SKent Gibson 	if (num_set != 1)
13507b8e00d9SKent Gibson 		kfree(descs);
13517b8e00d9SKent Gibson 	return ret;
13527b8e00d9SKent Gibson }
13537b8e00d9SKent Gibson 
13547b8e00d9SKent Gibson static long linereq_set_values(struct linereq *lr, void __user *ip)
13557b8e00d9SKent Gibson {
13567b8e00d9SKent Gibson 	struct gpio_v2_line_values lv;
13577b8e00d9SKent Gibson 	int ret;
13587b8e00d9SKent Gibson 
13597b8e00d9SKent Gibson 	if (copy_from_user(&lv, ip, sizeof(lv)))
13607b8e00d9SKent Gibson 		return -EFAULT;
13617b8e00d9SKent Gibson 
13627b8e00d9SKent Gibson 	mutex_lock(&lr->config_mutex);
13637b8e00d9SKent Gibson 
13647b8e00d9SKent Gibson 	ret = linereq_set_values_unlocked(lr, &lv);
13657b8e00d9SKent Gibson 
13667b8e00d9SKent Gibson 	mutex_unlock(&lr->config_mutex);
13677b8e00d9SKent Gibson 
13687b8e00d9SKent Gibson 	return ret;
13697b8e00d9SKent Gibson }
13707b8e00d9SKent Gibson 
1371a54756cbSKent Gibson static long linereq_set_config_unlocked(struct linereq *lr,
1372a54756cbSKent Gibson 					struct gpio_v2_line_config *lc)
1373a54756cbSKent Gibson {
1374a54756cbSKent Gibson 	struct gpio_desc *desc;
1375b1a92e94SKent Gibson 	struct line *line;
1376a54756cbSKent Gibson 	unsigned int i;
1377b1a92e94SKent Gibson 	u64 flags, edflags;
1378a54756cbSKent Gibson 	int ret;
1379a54756cbSKent Gibson 
1380a54756cbSKent Gibson 	for (i = 0; i < lr->num_lines; i++) {
1381b1a92e94SKent Gibson 		line = &lr->lines[i];
1382a54756cbSKent Gibson 		desc = lr->lines[i].desc;
1383a54756cbSKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
1384a54756cbSKent Gibson 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1385b1a92e94SKent Gibson 		edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
1386a54756cbSKent Gibson 		/*
1387a54756cbSKent Gibson 		 * Lines have to be requested explicitly for input
1388a54756cbSKent Gibson 		 * or output, else the line will be treated "as is".
1389a54756cbSKent Gibson 		 */
1390a54756cbSKent Gibson 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1391a54756cbSKent Gibson 			int val = gpio_v2_line_config_output_value(lc, i);
1392a54756cbSKent Gibson 
1393b1a92e94SKent Gibson 			edge_detector_stop(line);
1394a54756cbSKent Gibson 			ret = gpiod_direction_output(desc, val);
1395a54756cbSKent Gibson 			if (ret)
1396a54756cbSKent Gibson 				return ret;
1397a54756cbSKent Gibson 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1398a54756cbSKent Gibson 			ret = gpiod_direction_input(desc);
1399a54756cbSKent Gibson 			if (ret)
1400a54756cbSKent Gibson 				return ret;
1401a54756cbSKent Gibson 
1402b1a92e94SKent Gibson 			ret = edge_detector_update(line, lc, i, edflags);
1403a54756cbSKent Gibson 			if (ret)
1404a54756cbSKent Gibson 				return ret;
1405a54756cbSKent Gibson 		}
1406a54756cbSKent Gibson 
1407b1a92e94SKent Gibson 		WRITE_ONCE(line->edflags, edflags);
1408b1a92e94SKent Gibson 
1409a54756cbSKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
1410a54756cbSKent Gibson 					     GPIO_V2_LINE_CHANGED_CONFIG,
1411a54756cbSKent Gibson 					     desc);
1412a54756cbSKent Gibson 	}
1413a54756cbSKent Gibson 	return 0;
1414a54756cbSKent Gibson }
1415a54756cbSKent Gibson 
1416a54756cbSKent Gibson static long linereq_set_config(struct linereq *lr, void __user *ip)
1417a54756cbSKent Gibson {
1418a54756cbSKent Gibson 	struct gpio_v2_line_config lc;
1419a54756cbSKent Gibson 	int ret;
1420a54756cbSKent Gibson 
1421a54756cbSKent Gibson 	if (copy_from_user(&lc, ip, sizeof(lc)))
1422a54756cbSKent Gibson 		return -EFAULT;
1423a54756cbSKent Gibson 
1424a54756cbSKent Gibson 	ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
1425a54756cbSKent Gibson 	if (ret)
1426a54756cbSKent Gibson 		return ret;
1427a54756cbSKent Gibson 
1428a54756cbSKent Gibson 	mutex_lock(&lr->config_mutex);
1429a54756cbSKent Gibson 
1430a54756cbSKent Gibson 	ret = linereq_set_config_unlocked(lr, &lc);
1431a54756cbSKent Gibson 
1432a54756cbSKent Gibson 	mutex_unlock(&lr->config_mutex);
1433a54756cbSKent Gibson 
1434a54756cbSKent Gibson 	return ret;
1435a54756cbSKent Gibson }
1436a54756cbSKent Gibson 
1437*bdbbae24SBartosz Golaszewski static long linereq_ioctl_unlocked(struct file *file, unsigned int cmd,
14383c0d9c63SKent Gibson 				   unsigned long arg)
14393c0d9c63SKent Gibson {
14403c0d9c63SKent Gibson 	struct linereq *lr = file->private_data;
14413c0d9c63SKent Gibson 	void __user *ip = (void __user *)arg;
14423c0d9c63SKent Gibson 
1443533aae7cSBartosz Golaszewski 	if (!lr->gdev->chip)
1444533aae7cSBartosz Golaszewski 		return -ENODEV;
1445533aae7cSBartosz Golaszewski 
14461cef8b50SAndy Shevchenko 	switch (cmd) {
14471cef8b50SAndy Shevchenko 	case GPIO_V2_LINE_GET_VALUES_IOCTL:
14483c0d9c63SKent Gibson 		return linereq_get_values(lr, ip);
14491cef8b50SAndy Shevchenko 	case GPIO_V2_LINE_SET_VALUES_IOCTL:
14507b8e00d9SKent Gibson 		return linereq_set_values(lr, ip);
14511cef8b50SAndy Shevchenko 	case GPIO_V2_LINE_SET_CONFIG_IOCTL:
1452a54756cbSKent Gibson 		return linereq_set_config(lr, ip);
14531cef8b50SAndy Shevchenko 	default:
14543c0d9c63SKent Gibson 		return -EINVAL;
14553c0d9c63SKent Gibson 	}
14561cef8b50SAndy Shevchenko }
14573c0d9c63SKent Gibson 
1458*bdbbae24SBartosz Golaszewski static long linereq_ioctl(struct file *file, unsigned int cmd,
1459*bdbbae24SBartosz Golaszewski 			  unsigned long arg)
1460*bdbbae24SBartosz Golaszewski {
1461*bdbbae24SBartosz Golaszewski 	struct linereq *lr = file->private_data;
1462*bdbbae24SBartosz Golaszewski 
1463*bdbbae24SBartosz Golaszewski 	return call_ioctl_locked(file, cmd, arg, lr->gdev,
1464*bdbbae24SBartosz Golaszewski 				 linereq_ioctl_unlocked);
1465*bdbbae24SBartosz Golaszewski }
1466*bdbbae24SBartosz Golaszewski 
14673c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
14683c0d9c63SKent Gibson static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
14693c0d9c63SKent Gibson 				 unsigned long arg)
14703c0d9c63SKent Gibson {
14713c0d9c63SKent Gibson 	return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
14723c0d9c63SKent Gibson }
14733c0d9c63SKent Gibson #endif
14743c0d9c63SKent Gibson 
1475*bdbbae24SBartosz Golaszewski static __poll_t linereq_poll_unlocked(struct file *file,
147673e03419SKent Gibson 				      struct poll_table_struct *wait)
147773e03419SKent Gibson {
147873e03419SKent Gibson 	struct linereq *lr = file->private_data;
147973e03419SKent Gibson 	__poll_t events = 0;
148073e03419SKent Gibson 
1481533aae7cSBartosz Golaszewski 	if (!lr->gdev->chip)
1482533aae7cSBartosz Golaszewski 		return EPOLLHUP | EPOLLERR;
1483533aae7cSBartosz Golaszewski 
148473e03419SKent Gibson 	poll_wait(file, &lr->wait, wait);
148573e03419SKent Gibson 
148673e03419SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
148773e03419SKent Gibson 						 &lr->wait.lock))
148873e03419SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
148973e03419SKent Gibson 
149073e03419SKent Gibson 	return events;
149173e03419SKent Gibson }
149273e03419SKent Gibson 
1493*bdbbae24SBartosz Golaszewski static __poll_t linereq_poll(struct file *file,
1494*bdbbae24SBartosz Golaszewski 			     struct poll_table_struct *wait)
1495*bdbbae24SBartosz Golaszewski {
1496*bdbbae24SBartosz Golaszewski 	struct linereq *lr = file->private_data;
1497*bdbbae24SBartosz Golaszewski 
1498*bdbbae24SBartosz Golaszewski 	return call_poll_locked(file, wait, lr->gdev, linereq_poll_unlocked);
1499*bdbbae24SBartosz Golaszewski }
1500*bdbbae24SBartosz Golaszewski 
1501*bdbbae24SBartosz Golaszewski static ssize_t linereq_read_unlocked(struct file *file, char __user *buf,
1502*bdbbae24SBartosz Golaszewski 				     size_t count, loff_t *f_ps)
150373e03419SKent Gibson {
150473e03419SKent Gibson 	struct linereq *lr = file->private_data;
150573e03419SKent Gibson 	struct gpio_v2_line_event le;
150673e03419SKent Gibson 	ssize_t bytes_read = 0;
150773e03419SKent Gibson 	int ret;
150873e03419SKent Gibson 
1509533aae7cSBartosz Golaszewski 	if (!lr->gdev->chip)
1510533aae7cSBartosz Golaszewski 		return -ENODEV;
1511533aae7cSBartosz Golaszewski 
151273e03419SKent Gibson 	if (count < sizeof(le))
151373e03419SKent Gibson 		return -EINVAL;
151473e03419SKent Gibson 
151573e03419SKent Gibson 	do {
151673e03419SKent Gibson 		spin_lock(&lr->wait.lock);
151773e03419SKent Gibson 		if (kfifo_is_empty(&lr->events)) {
151873e03419SKent Gibson 			if (bytes_read) {
151973e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
152073e03419SKent Gibson 				return bytes_read;
152173e03419SKent Gibson 			}
152273e03419SKent Gibson 
152373e03419SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
152473e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
152573e03419SKent Gibson 				return -EAGAIN;
152673e03419SKent Gibson 			}
152773e03419SKent Gibson 
152873e03419SKent Gibson 			ret = wait_event_interruptible_locked(lr->wait,
152973e03419SKent Gibson 					!kfifo_is_empty(&lr->events));
153073e03419SKent Gibson 			if (ret) {
153173e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
153273e03419SKent Gibson 				return ret;
153373e03419SKent Gibson 			}
153473e03419SKent Gibson 		}
153573e03419SKent Gibson 
153673e03419SKent Gibson 		ret = kfifo_out(&lr->events, &le, 1);
153773e03419SKent Gibson 		spin_unlock(&lr->wait.lock);
153873e03419SKent Gibson 		if (ret != 1) {
153973e03419SKent Gibson 			/*
154073e03419SKent Gibson 			 * This should never happen - we were holding the
154173e03419SKent Gibson 			 * lock from the moment we learned the fifo is no
154273e03419SKent Gibson 			 * longer empty until now.
154373e03419SKent Gibson 			 */
154473e03419SKent Gibson 			ret = -EIO;
154573e03419SKent Gibson 			break;
154673e03419SKent Gibson 		}
154773e03419SKent Gibson 
154873e03419SKent Gibson 		if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
154973e03419SKent Gibson 			return -EFAULT;
155073e03419SKent Gibson 		bytes_read += sizeof(le);
155173e03419SKent Gibson 	} while (count >= bytes_read + sizeof(le));
155273e03419SKent Gibson 
155373e03419SKent Gibson 	return bytes_read;
155473e03419SKent Gibson }
155573e03419SKent Gibson 
1556*bdbbae24SBartosz Golaszewski static ssize_t linereq_read(struct file *file, char __user *buf,
1557*bdbbae24SBartosz Golaszewski 			    size_t count, loff_t *f_ps)
1558*bdbbae24SBartosz Golaszewski {
1559*bdbbae24SBartosz Golaszewski 	struct linereq *lr = file->private_data;
1560*bdbbae24SBartosz Golaszewski 
1561*bdbbae24SBartosz Golaszewski 	return call_read_locked(file, buf, count, f_ps, lr->gdev,
1562*bdbbae24SBartosz Golaszewski 				linereq_read_unlocked);
1563*bdbbae24SBartosz Golaszewski }
1564*bdbbae24SBartosz Golaszewski 
15653c0d9c63SKent Gibson static void linereq_free(struct linereq *lr)
15663c0d9c63SKent Gibson {
15673c0d9c63SKent Gibson 	unsigned int i;
15683c0d9c63SKent Gibson 
15693c0d9c63SKent Gibson 	for (i = 0; i < lr->num_lines; i++) {
1570160d6e40SKent Gibson 		if (lr->lines[i].desc) {
1571b1a92e94SKent Gibson 			edge_detector_stop(&lr->lines[i]);
15723c0d9c63SKent Gibson 			gpiod_free(lr->lines[i].desc);
15733c0d9c63SKent Gibson 		}
1574160d6e40SKent Gibson 	}
157573e03419SKent Gibson 	kfifo_free(&lr->events);
15763c0d9c63SKent Gibson 	kfree(lr->label);
15773c0d9c63SKent Gibson 	put_device(&lr->gdev->dev);
15783c0d9c63SKent Gibson 	kfree(lr);
15793c0d9c63SKent Gibson }
15803c0d9c63SKent Gibson 
15813c0d9c63SKent Gibson static int linereq_release(struct inode *inode, struct file *file)
15823c0d9c63SKent Gibson {
15833c0d9c63SKent Gibson 	struct linereq *lr = file->private_data;
15843c0d9c63SKent Gibson 
15853c0d9c63SKent Gibson 	linereq_free(lr);
15863c0d9c63SKent Gibson 	return 0;
15873c0d9c63SKent Gibson }
15883c0d9c63SKent Gibson 
15890ae3109aSBartosz Golaszewski #ifdef CONFIG_PROC_FS
15900ae3109aSBartosz Golaszewski static void linereq_show_fdinfo(struct seq_file *out, struct file *file)
15910ae3109aSBartosz Golaszewski {
15920ae3109aSBartosz Golaszewski 	struct linereq *lr = file->private_data;
15930ae3109aSBartosz Golaszewski 	struct device *dev = &lr->gdev->dev;
15940ae3109aSBartosz Golaszewski 	u16 i;
15950ae3109aSBartosz Golaszewski 
15960ae3109aSBartosz Golaszewski 	seq_printf(out, "gpio-chip:\t%s\n", dev_name(dev));
15970ae3109aSBartosz Golaszewski 
15980ae3109aSBartosz Golaszewski 	for (i = 0; i < lr->num_lines; i++)
15990ae3109aSBartosz Golaszewski 		seq_printf(out, "gpio-line:\t%d\n",
16000ae3109aSBartosz Golaszewski 			   gpio_chip_hwgpio(lr->lines[i].desc));
16010ae3109aSBartosz Golaszewski }
16020ae3109aSBartosz Golaszewski #endif
16030ae3109aSBartosz Golaszewski 
16043c0d9c63SKent Gibson static const struct file_operations line_fileops = {
16053c0d9c63SKent Gibson 	.release = linereq_release,
160673e03419SKent Gibson 	.read = linereq_read,
160773e03419SKent Gibson 	.poll = linereq_poll,
16083c0d9c63SKent Gibson 	.owner = THIS_MODULE,
16093c0d9c63SKent Gibson 	.llseek = noop_llseek,
16103c0d9c63SKent Gibson 	.unlocked_ioctl = linereq_ioctl,
16113c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
16123c0d9c63SKent Gibson 	.compat_ioctl = linereq_ioctl_compat,
16133c0d9c63SKent Gibson #endif
16140ae3109aSBartosz Golaszewski #ifdef CONFIG_PROC_FS
16150ae3109aSBartosz Golaszewski 	.show_fdinfo = linereq_show_fdinfo,
16160ae3109aSBartosz Golaszewski #endif
16173c0d9c63SKent Gibson };
16183c0d9c63SKent Gibson 
16193c0d9c63SKent Gibson static int linereq_create(struct gpio_device *gdev, void __user *ip)
16203c0d9c63SKent Gibson {
16213c0d9c63SKent Gibson 	struct gpio_v2_line_request ulr;
16223c0d9c63SKent Gibson 	struct gpio_v2_line_config *lc;
16233c0d9c63SKent Gibson 	struct linereq *lr;
16243c0d9c63SKent Gibson 	struct file *file;
1625b1a92e94SKent Gibson 	u64 flags, edflags;
16263c0d9c63SKent Gibson 	unsigned int i;
16273c0d9c63SKent Gibson 	int fd, ret;
16283c0d9c63SKent Gibson 
16293c0d9c63SKent Gibson 	if (copy_from_user(&ulr, ip, sizeof(ulr)))
16303c0d9c63SKent Gibson 		return -EFAULT;
16313c0d9c63SKent Gibson 
16323c0d9c63SKent Gibson 	if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
16333c0d9c63SKent Gibson 		return -EINVAL;
16343c0d9c63SKent Gibson 
16353c0d9c63SKent Gibson 	if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
16363c0d9c63SKent Gibson 		return -EINVAL;
16373c0d9c63SKent Gibson 
16383c0d9c63SKent Gibson 	lc = &ulr.config;
16393c0d9c63SKent Gibson 	ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
16403c0d9c63SKent Gibson 	if (ret)
16413c0d9c63SKent Gibson 		return ret;
16423c0d9c63SKent Gibson 
16433c0d9c63SKent Gibson 	lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
16443c0d9c63SKent Gibson 	if (!lr)
16453c0d9c63SKent Gibson 		return -ENOMEM;
16463c0d9c63SKent Gibson 
16473c0d9c63SKent Gibson 	lr->gdev = gdev;
16483c0d9c63SKent Gibson 	get_device(&gdev->dev);
16493c0d9c63SKent Gibson 
165065cff704SKent Gibson 	for (i = 0; i < ulr.num_lines; i++) {
165173e03419SKent Gibson 		lr->lines[i].req = lr;
165265cff704SKent Gibson 		WRITE_ONCE(lr->lines[i].sw_debounced, 0);
165365cff704SKent Gibson 		INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
165465cff704SKent Gibson 	}
165573e03419SKent Gibson 
1656f188ac12SKent Gibson 	if (ulr.consumer[0] != '\0') {
16573c0d9c63SKent Gibson 		/* label is only initialized if consumer is set */
1658f188ac12SKent Gibson 		lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1659f188ac12SKent Gibson 				     GFP_KERNEL);
16603c0d9c63SKent Gibson 		if (!lr->label) {
16613c0d9c63SKent Gibson 			ret = -ENOMEM;
16623c0d9c63SKent Gibson 			goto out_free_linereq;
16633c0d9c63SKent Gibson 		}
16643c0d9c63SKent Gibson 	}
16653c0d9c63SKent Gibson 
1666a54756cbSKent Gibson 	mutex_init(&lr->config_mutex);
166773e03419SKent Gibson 	init_waitqueue_head(&lr->wait);
166873e03419SKent Gibson 	lr->event_buffer_size = ulr.event_buffer_size;
166973e03419SKent Gibson 	if (lr->event_buffer_size == 0)
167073e03419SKent Gibson 		lr->event_buffer_size = ulr.num_lines * 16;
167173e03419SKent Gibson 	else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
167273e03419SKent Gibson 		lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
167373e03419SKent Gibson 
167473e03419SKent Gibson 	atomic_set(&lr->seqno, 0);
16753c0d9c63SKent Gibson 	lr->num_lines = ulr.num_lines;
16763c0d9c63SKent Gibson 
16773c0d9c63SKent Gibson 	/* Request each GPIO */
16783c0d9c63SKent Gibson 	for (i = 0; i < ulr.num_lines; i++) {
16793c0d9c63SKent Gibson 		u32 offset = ulr.offsets[i];
16803c0d9c63SKent Gibson 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
16813c0d9c63SKent Gibson 
16823c0d9c63SKent Gibson 		if (IS_ERR(desc)) {
16833c0d9c63SKent Gibson 			ret = PTR_ERR(desc);
16843c0d9c63SKent Gibson 			goto out_free_linereq;
16853c0d9c63SKent Gibson 		}
16863c0d9c63SKent Gibson 
168795a4eed7SAndy Shevchenko 		ret = gpiod_request_user(desc, lr->label);
16883c0d9c63SKent Gibson 		if (ret)
16893c0d9c63SKent Gibson 			goto out_free_linereq;
16903c0d9c63SKent Gibson 
16913c0d9c63SKent Gibson 		lr->lines[i].desc = desc;
16923c0d9c63SKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
16933c0d9c63SKent Gibson 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
16943c0d9c63SKent Gibson 
16953c0d9c63SKent Gibson 		ret = gpiod_set_transitory(desc, false);
16963c0d9c63SKent Gibson 		if (ret < 0)
16973c0d9c63SKent Gibson 			goto out_free_linereq;
16983c0d9c63SKent Gibson 
1699b1a92e94SKent Gibson 		edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
17003c0d9c63SKent Gibson 		/*
17013c0d9c63SKent Gibson 		 * Lines have to be requested explicitly for input
17023c0d9c63SKent Gibson 		 * or output, else the line will be treated "as is".
17033c0d9c63SKent Gibson 		 */
17043c0d9c63SKent Gibson 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
17053c0d9c63SKent Gibson 			int val = gpio_v2_line_config_output_value(lc, i);
17063c0d9c63SKent Gibson 
17073c0d9c63SKent Gibson 			ret = gpiod_direction_output(desc, val);
17083c0d9c63SKent Gibson 			if (ret)
17093c0d9c63SKent Gibson 				goto out_free_linereq;
17103c0d9c63SKent Gibson 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
17113c0d9c63SKent Gibson 			ret = gpiod_direction_input(desc);
17123c0d9c63SKent Gibson 			if (ret)
17133c0d9c63SKent Gibson 				goto out_free_linereq;
171473e03419SKent Gibson 
171565cff704SKent Gibson 			ret = edge_detector_setup(&lr->lines[i], lc, i,
1716b1a92e94SKent Gibson 						  edflags);
171773e03419SKent Gibson 			if (ret)
171873e03419SKent Gibson 				goto out_free_linereq;
17193c0d9c63SKent Gibson 		}
17203c0d9c63SKent Gibson 
1721b1a92e94SKent Gibson 		lr->lines[i].edflags = edflags;
1722b1a92e94SKent Gibson 
17233c0d9c63SKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
1724aad95584SKent Gibson 					     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
17253c0d9c63SKent Gibson 
17263c0d9c63SKent Gibson 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
17273c0d9c63SKent Gibson 			offset);
17283c0d9c63SKent Gibson 	}
17293c0d9c63SKent Gibson 
17303c0d9c63SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
17313c0d9c63SKent Gibson 	if (fd < 0) {
17323c0d9c63SKent Gibson 		ret = fd;
17333c0d9c63SKent Gibson 		goto out_free_linereq;
17343c0d9c63SKent Gibson 	}
17353c0d9c63SKent Gibson 
17363c0d9c63SKent Gibson 	file = anon_inode_getfile("gpio-line", &line_fileops, lr,
17373c0d9c63SKent Gibson 				  O_RDONLY | O_CLOEXEC);
17383c0d9c63SKent Gibson 	if (IS_ERR(file)) {
17393c0d9c63SKent Gibson 		ret = PTR_ERR(file);
17403c0d9c63SKent Gibson 		goto out_put_unused_fd;
17413c0d9c63SKent Gibson 	}
17423c0d9c63SKent Gibson 
17433c0d9c63SKent Gibson 	ulr.fd = fd;
17443c0d9c63SKent Gibson 	if (copy_to_user(ip, &ulr, sizeof(ulr))) {
17453c0d9c63SKent Gibson 		/*
17463c0d9c63SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
17473c0d9c63SKent Gibson 		 * the regular error cleanup path here.
17483c0d9c63SKent Gibson 		 */
17493c0d9c63SKent Gibson 		fput(file);
17503c0d9c63SKent Gibson 		put_unused_fd(fd);
17513c0d9c63SKent Gibson 		return -EFAULT;
17523c0d9c63SKent Gibson 	}
17533c0d9c63SKent Gibson 
17543c0d9c63SKent Gibson 	fd_install(fd, file);
17553c0d9c63SKent Gibson 
17563c0d9c63SKent Gibson 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
17573c0d9c63SKent Gibson 		lr->num_lines);
17583c0d9c63SKent Gibson 
17593c0d9c63SKent Gibson 	return 0;
17603c0d9c63SKent Gibson 
17613c0d9c63SKent Gibson out_put_unused_fd:
17623c0d9c63SKent Gibson 	put_unused_fd(fd);
17633c0d9c63SKent Gibson out_free_linereq:
17643c0d9c63SKent Gibson 	linereq_free(lr);
17653c0d9c63SKent Gibson 	return ret;
17663c0d9c63SKent Gibson }
17673c0d9c63SKent Gibson 
17683c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1769925ca369SKent Gibson 
1770925ca369SKent Gibson /*
1771925ca369SKent Gibson  * GPIO line event management
1772925ca369SKent Gibson  */
1773925ca369SKent Gibson 
1774925ca369SKent Gibson /**
1775925ca369SKent Gibson  * struct lineevent_state - contains the state of a userspace event
1776925ca369SKent Gibson  * @gdev: the GPIO device the event pertains to
1777925ca369SKent Gibson  * @label: consumer label used to tag descriptors
1778925ca369SKent Gibson  * @desc: the GPIO descriptor held by this event
1779925ca369SKent Gibson  * @eflags: the event flags this line was requested with
1780925ca369SKent Gibson  * @irq: the interrupt that trigger in response to events on this GPIO
1781925ca369SKent Gibson  * @wait: wait queue that handles blocking reads of events
1782925ca369SKent Gibson  * @events: KFIFO for the GPIO events
1783925ca369SKent Gibson  * @timestamp: cache for the timestamp storing it between hardirq
1784925ca369SKent Gibson  * and IRQ thread, used to bring the timestamp close to the actual
1785925ca369SKent Gibson  * event
1786925ca369SKent Gibson  */
1787925ca369SKent Gibson struct lineevent_state {
1788925ca369SKent Gibson 	struct gpio_device *gdev;
1789925ca369SKent Gibson 	const char *label;
1790925ca369SKent Gibson 	struct gpio_desc *desc;
1791925ca369SKent Gibson 	u32 eflags;
1792925ca369SKent Gibson 	int irq;
1793925ca369SKent Gibson 	wait_queue_head_t wait;
1794925ca369SKent Gibson 	DECLARE_KFIFO(events, struct gpioevent_data, 16);
1795925ca369SKent Gibson 	u64 timestamp;
1796925ca369SKent Gibson };
1797925ca369SKent Gibson 
1798925ca369SKent Gibson #define GPIOEVENT_REQUEST_VALID_FLAGS \
1799925ca369SKent Gibson 	(GPIOEVENT_REQUEST_RISING_EDGE | \
1800925ca369SKent Gibson 	GPIOEVENT_REQUEST_FALLING_EDGE)
1801925ca369SKent Gibson 
1802*bdbbae24SBartosz Golaszewski static __poll_t lineevent_poll_unlocked(struct file *file,
1803925ca369SKent Gibson 					struct poll_table_struct *wait)
1804925ca369SKent Gibson {
180549bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1806925ca369SKent Gibson 	__poll_t events = 0;
1807925ca369SKent Gibson 
1808533aae7cSBartosz Golaszewski 	if (!le->gdev->chip)
1809533aae7cSBartosz Golaszewski 		return EPOLLHUP | EPOLLERR;
1810533aae7cSBartosz Golaszewski 
181149bc5279SKent Gibson 	poll_wait(file, &le->wait, wait);
1812925ca369SKent Gibson 
1813925ca369SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1814925ca369SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
1815925ca369SKent Gibson 
1816925ca369SKent Gibson 	return events;
1817925ca369SKent Gibson }
1818925ca369SKent Gibson 
1819*bdbbae24SBartosz Golaszewski static __poll_t lineevent_poll(struct file *file,
1820*bdbbae24SBartosz Golaszewski 			       struct poll_table_struct *wait)
1821*bdbbae24SBartosz Golaszewski {
1822*bdbbae24SBartosz Golaszewski 	struct lineevent_state *le = file->private_data;
1823*bdbbae24SBartosz Golaszewski 
1824*bdbbae24SBartosz Golaszewski 	return call_poll_locked(file, wait, le->gdev, lineevent_poll_unlocked);
1825*bdbbae24SBartosz Golaszewski }
1826*bdbbae24SBartosz Golaszewski 
18275ad284abSAndy Shevchenko struct compat_gpioeevent_data {
18285ad284abSAndy Shevchenko 	compat_u64	timestamp;
18295ad284abSAndy Shevchenko 	u32		id;
18305ad284abSAndy Shevchenko };
18315ad284abSAndy Shevchenko 
1832*bdbbae24SBartosz Golaszewski static ssize_t lineevent_read_unlocked(struct file *file, char __user *buf,
1833*bdbbae24SBartosz Golaszewski 				       size_t count, loff_t *f_ps)
1834925ca369SKent Gibson {
183549bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1836925ca369SKent Gibson 	struct gpioevent_data ge;
1837925ca369SKent Gibson 	ssize_t bytes_read = 0;
18385ad284abSAndy Shevchenko 	ssize_t ge_size;
1839925ca369SKent Gibson 	int ret;
1840925ca369SKent Gibson 
1841533aae7cSBartosz Golaszewski 	if (!le->gdev->chip)
1842533aae7cSBartosz Golaszewski 		return -ENODEV;
1843533aae7cSBartosz Golaszewski 
18445ad284abSAndy Shevchenko 	/*
18455ad284abSAndy Shevchenko 	 * When compatible system call is being used the struct gpioevent_data,
18465ad284abSAndy Shevchenko 	 * in case of at least ia32, has different size due to the alignment
18475ad284abSAndy Shevchenko 	 * differences. Because we have first member 64 bits followed by one of
18485ad284abSAndy Shevchenko 	 * 32 bits there is no gap between them. The only difference is the
18495ad284abSAndy Shevchenko 	 * padding at the end of the data structure. Hence, we calculate the
18505ad284abSAndy Shevchenko 	 * actual sizeof() and pass this as an argument to copy_to_user() to
18515ad284abSAndy Shevchenko 	 * drop unneeded bytes from the output.
18525ad284abSAndy Shevchenko 	 */
1853163d1719SAndy Shevchenko 	if (compat_need_64bit_alignment_fixup())
1854163d1719SAndy Shevchenko 		ge_size = sizeof(struct compat_gpioeevent_data);
1855163d1719SAndy Shevchenko 	else
1856163d1719SAndy Shevchenko 		ge_size = sizeof(struct gpioevent_data);
18575ad284abSAndy Shevchenko 	if (count < ge_size)
1858925ca369SKent Gibson 		return -EINVAL;
1859925ca369SKent Gibson 
1860925ca369SKent Gibson 	do {
1861925ca369SKent Gibson 		spin_lock(&le->wait.lock);
1862925ca369SKent Gibson 		if (kfifo_is_empty(&le->events)) {
1863925ca369SKent Gibson 			if (bytes_read) {
1864925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1865925ca369SKent Gibson 				return bytes_read;
1866925ca369SKent Gibson 			}
1867925ca369SKent Gibson 
186849bc5279SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
1869925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1870925ca369SKent Gibson 				return -EAGAIN;
1871925ca369SKent Gibson 			}
1872925ca369SKent Gibson 
1873925ca369SKent Gibson 			ret = wait_event_interruptible_locked(le->wait,
1874925ca369SKent Gibson 					!kfifo_is_empty(&le->events));
1875925ca369SKent Gibson 			if (ret) {
1876925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1877925ca369SKent Gibson 				return ret;
1878925ca369SKent Gibson 			}
1879925ca369SKent Gibson 		}
1880925ca369SKent Gibson 
1881925ca369SKent Gibson 		ret = kfifo_out(&le->events, &ge, 1);
1882925ca369SKent Gibson 		spin_unlock(&le->wait.lock);
1883925ca369SKent Gibson 		if (ret != 1) {
1884925ca369SKent Gibson 			/*
1885925ca369SKent Gibson 			 * This should never happen - we were holding the lock
1886925ca369SKent Gibson 			 * from the moment we learned the fifo is no longer
1887925ca369SKent Gibson 			 * empty until now.
1888925ca369SKent Gibson 			 */
1889925ca369SKent Gibson 			ret = -EIO;
1890925ca369SKent Gibson 			break;
1891925ca369SKent Gibson 		}
1892925ca369SKent Gibson 
18935ad284abSAndy Shevchenko 		if (copy_to_user(buf + bytes_read, &ge, ge_size))
1894925ca369SKent Gibson 			return -EFAULT;
18955ad284abSAndy Shevchenko 		bytes_read += ge_size;
18965ad284abSAndy Shevchenko 	} while (count >= bytes_read + ge_size);
1897925ca369SKent Gibson 
1898925ca369SKent Gibson 	return bytes_read;
1899925ca369SKent Gibson }
1900925ca369SKent Gibson 
1901*bdbbae24SBartosz Golaszewski static ssize_t lineevent_read(struct file *file, char __user *buf,
1902*bdbbae24SBartosz Golaszewski 			      size_t count, loff_t *f_ps)
1903*bdbbae24SBartosz Golaszewski {
1904*bdbbae24SBartosz Golaszewski 	struct lineevent_state *le = file->private_data;
1905*bdbbae24SBartosz Golaszewski 
1906*bdbbae24SBartosz Golaszewski 	return call_read_locked(file, buf, count, f_ps, le->gdev,
1907*bdbbae24SBartosz Golaszewski 				lineevent_read_unlocked);
1908*bdbbae24SBartosz Golaszewski }
1909*bdbbae24SBartosz Golaszewski 
191046824272SKent Gibson static void lineevent_free(struct lineevent_state *le)
1911925ca369SKent Gibson {
191246824272SKent Gibson 	if (le->irq)
1913925ca369SKent Gibson 		free_irq(le->irq, le);
191446824272SKent Gibson 	if (le->desc)
1915925ca369SKent Gibson 		gpiod_free(le->desc);
1916925ca369SKent Gibson 	kfree(le->label);
191746824272SKent Gibson 	put_device(&le->gdev->dev);
1918925ca369SKent Gibson 	kfree(le);
191946824272SKent Gibson }
192046824272SKent Gibson 
192146824272SKent Gibson static int lineevent_release(struct inode *inode, struct file *file)
192246824272SKent Gibson {
192346824272SKent Gibson 	lineevent_free(file->private_data);
1924925ca369SKent Gibson 	return 0;
1925925ca369SKent Gibson }
1926925ca369SKent Gibson 
1927*bdbbae24SBartosz Golaszewski static long lineevent_ioctl_unlocked(struct file *file, unsigned int cmd,
1928925ca369SKent Gibson 				     unsigned long arg)
1929925ca369SKent Gibson {
193049bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1931925ca369SKent Gibson 	void __user *ip = (void __user *)arg;
1932925ca369SKent Gibson 	struct gpiohandle_data ghd;
1933925ca369SKent Gibson 
1934533aae7cSBartosz Golaszewski 	if (!le->gdev->chip)
1935533aae7cSBartosz Golaszewski 		return -ENODEV;
1936533aae7cSBartosz Golaszewski 
1937925ca369SKent Gibson 	/*
1938925ca369SKent Gibson 	 * We can get the value for an event line but not set it,
1939925ca369SKent Gibson 	 * because it is input by definition.
1940925ca369SKent Gibson 	 */
1941925ca369SKent Gibson 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
1942925ca369SKent Gibson 		int val;
1943925ca369SKent Gibson 
1944925ca369SKent Gibson 		memset(&ghd, 0, sizeof(ghd));
1945925ca369SKent Gibson 
1946925ca369SKent Gibson 		val = gpiod_get_value_cansleep(le->desc);
1947925ca369SKent Gibson 		if (val < 0)
1948925ca369SKent Gibson 			return val;
1949925ca369SKent Gibson 		ghd.values[0] = val;
1950925ca369SKent Gibson 
1951925ca369SKent Gibson 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
1952925ca369SKent Gibson 			return -EFAULT;
1953925ca369SKent Gibson 
1954925ca369SKent Gibson 		return 0;
1955925ca369SKent Gibson 	}
1956925ca369SKent Gibson 	return -EINVAL;
1957925ca369SKent Gibson }
1958925ca369SKent Gibson 
1959*bdbbae24SBartosz Golaszewski static long lineevent_ioctl(struct file *file, unsigned int cmd,
1960*bdbbae24SBartosz Golaszewski 			    unsigned long arg)
1961*bdbbae24SBartosz Golaszewski {
1962*bdbbae24SBartosz Golaszewski 	struct lineevent_state *le = file->private_data;
1963*bdbbae24SBartosz Golaszewski 
1964*bdbbae24SBartosz Golaszewski 	return call_ioctl_locked(file, cmd, arg, le->gdev,
1965*bdbbae24SBartosz Golaszewski 				 lineevent_ioctl_unlocked);
1966*bdbbae24SBartosz Golaszewski }
1967*bdbbae24SBartosz Golaszewski 
1968925ca369SKent Gibson #ifdef CONFIG_COMPAT
196949bc5279SKent Gibson static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
1970925ca369SKent Gibson 				   unsigned long arg)
1971925ca369SKent Gibson {
197249bc5279SKent Gibson 	return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1973925ca369SKent Gibson }
1974925ca369SKent Gibson #endif
1975925ca369SKent Gibson 
1976925ca369SKent Gibson static const struct file_operations lineevent_fileops = {
1977925ca369SKent Gibson 	.release = lineevent_release,
1978925ca369SKent Gibson 	.read = lineevent_read,
1979925ca369SKent Gibson 	.poll = lineevent_poll,
1980925ca369SKent Gibson 	.owner = THIS_MODULE,
1981925ca369SKent Gibson 	.llseek = noop_llseek,
1982925ca369SKent Gibson 	.unlocked_ioctl = lineevent_ioctl,
1983925ca369SKent Gibson #ifdef CONFIG_COMPAT
1984925ca369SKent Gibson 	.compat_ioctl = lineevent_ioctl_compat,
1985925ca369SKent Gibson #endif
1986925ca369SKent Gibson };
1987925ca369SKent Gibson 
1988925ca369SKent Gibson static irqreturn_t lineevent_irq_thread(int irq, void *p)
1989925ca369SKent Gibson {
1990925ca369SKent Gibson 	struct lineevent_state *le = p;
1991925ca369SKent Gibson 	struct gpioevent_data ge;
1992925ca369SKent Gibson 	int ret;
1993925ca369SKent Gibson 
1994925ca369SKent Gibson 	/* Do not leak kernel stack to userspace */
1995925ca369SKent Gibson 	memset(&ge, 0, sizeof(ge));
1996925ca369SKent Gibson 
1997925ca369SKent Gibson 	/*
1998925ca369SKent Gibson 	 * We may be running from a nested threaded interrupt in which case
1999925ca369SKent Gibson 	 * we didn't get the timestamp from lineevent_irq_handler().
2000925ca369SKent Gibson 	 */
2001925ca369SKent Gibson 	if (!le->timestamp)
2002925ca369SKent Gibson 		ge.timestamp = ktime_get_ns();
2003925ca369SKent Gibson 	else
2004925ca369SKent Gibson 		ge.timestamp = le->timestamp;
2005925ca369SKent Gibson 
2006925ca369SKent Gibson 	if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
2007925ca369SKent Gibson 	    && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
2008925ca369SKent Gibson 		int level = gpiod_get_value_cansleep(le->desc);
2009925ca369SKent Gibson 
2010925ca369SKent Gibson 		if (level)
2011925ca369SKent Gibson 			/* Emit low-to-high event */
2012925ca369SKent Gibson 			ge.id = GPIOEVENT_EVENT_RISING_EDGE;
2013925ca369SKent Gibson 		else
2014925ca369SKent Gibson 			/* Emit high-to-low event */
2015925ca369SKent Gibson 			ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
2016925ca369SKent Gibson 	} else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
2017925ca369SKent Gibson 		/* Emit low-to-high event */
2018925ca369SKent Gibson 		ge.id = GPIOEVENT_EVENT_RISING_EDGE;
2019925ca369SKent Gibson 	} else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
2020925ca369SKent Gibson 		/* Emit high-to-low event */
2021925ca369SKent Gibson 		ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
2022925ca369SKent Gibson 	} else {
2023925ca369SKent Gibson 		return IRQ_NONE;
2024925ca369SKent Gibson 	}
2025925ca369SKent Gibson 
2026925ca369SKent Gibson 	ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
2027925ca369SKent Gibson 					    1, &le->wait.lock);
2028925ca369SKent Gibson 	if (ret)
2029925ca369SKent Gibson 		wake_up_poll(&le->wait, EPOLLIN);
2030925ca369SKent Gibson 	else
2031925ca369SKent Gibson 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
2032925ca369SKent Gibson 
2033925ca369SKent Gibson 	return IRQ_HANDLED;
2034925ca369SKent Gibson }
2035925ca369SKent Gibson 
2036925ca369SKent Gibson static irqreturn_t lineevent_irq_handler(int irq, void *p)
2037925ca369SKent Gibson {
2038925ca369SKent Gibson 	struct lineevent_state *le = p;
2039925ca369SKent Gibson 
2040925ca369SKent Gibson 	/*
2041925ca369SKent Gibson 	 * Just store the timestamp in hardirq context so we get it as
2042925ca369SKent Gibson 	 * close in time as possible to the actual event.
2043925ca369SKent Gibson 	 */
2044925ca369SKent Gibson 	le->timestamp = ktime_get_ns();
2045925ca369SKent Gibson 
2046925ca369SKent Gibson 	return IRQ_WAKE_THREAD;
2047925ca369SKent Gibson }
2048925ca369SKent Gibson 
2049925ca369SKent Gibson static int lineevent_create(struct gpio_device *gdev, void __user *ip)
2050925ca369SKent Gibson {
2051925ca369SKent Gibson 	struct gpioevent_request eventreq;
2052925ca369SKent Gibson 	struct lineevent_state *le;
2053925ca369SKent Gibson 	struct gpio_desc *desc;
2054925ca369SKent Gibson 	struct file *file;
2055925ca369SKent Gibson 	u32 offset;
2056925ca369SKent Gibson 	u32 lflags;
2057925ca369SKent Gibson 	u32 eflags;
2058925ca369SKent Gibson 	int fd;
2059925ca369SKent Gibson 	int ret;
206046824272SKent Gibson 	int irq, irqflags = 0;
2061925ca369SKent Gibson 
2062925ca369SKent Gibson 	if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
2063925ca369SKent Gibson 		return -EFAULT;
2064925ca369SKent Gibson 
2065925ca369SKent Gibson 	offset = eventreq.lineoffset;
2066925ca369SKent Gibson 	lflags = eventreq.handleflags;
2067925ca369SKent Gibson 	eflags = eventreq.eventflags;
2068925ca369SKent Gibson 
2069925ca369SKent Gibson 	desc = gpiochip_get_desc(gdev->chip, offset);
2070925ca369SKent Gibson 	if (IS_ERR(desc))
2071925ca369SKent Gibson 		return PTR_ERR(desc);
2072925ca369SKent Gibson 
2073925ca369SKent Gibson 	/* Return an error if a unknown flag is set */
2074925ca369SKent Gibson 	if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
2075925ca369SKent Gibson 	    (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
2076925ca369SKent Gibson 		return -EINVAL;
2077925ca369SKent Gibson 
2078925ca369SKent Gibson 	/* This is just wrong: we don't look for events on output lines */
2079925ca369SKent Gibson 	if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
2080925ca369SKent Gibson 	    (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
2081925ca369SKent Gibson 	    (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
2082925ca369SKent Gibson 		return -EINVAL;
2083925ca369SKent Gibson 
2084925ca369SKent Gibson 	/* Only one bias flag can be set. */
2085925ca369SKent Gibson 	if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
2086925ca369SKent Gibson 	     (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
2087925ca369SKent Gibson 			GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
2088925ca369SKent Gibson 	    ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
2089925ca369SKent Gibson 	     (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
2090925ca369SKent Gibson 		return -EINVAL;
2091925ca369SKent Gibson 
2092925ca369SKent Gibson 	le = kzalloc(sizeof(*le), GFP_KERNEL);
2093925ca369SKent Gibson 	if (!le)
2094925ca369SKent Gibson 		return -ENOMEM;
2095925ca369SKent Gibson 	le->gdev = gdev;
2096925ca369SKent Gibson 	get_device(&gdev->dev);
2097925ca369SKent Gibson 
2098f188ac12SKent Gibson 	if (eventreq.consumer_label[0] != '\0') {
2099f188ac12SKent Gibson 		/* label is only initialized if consumer_label is set */
2100f188ac12SKent Gibson 		le->label = kstrndup(eventreq.consumer_label,
2101f188ac12SKent Gibson 				     sizeof(eventreq.consumer_label) - 1,
2102925ca369SKent Gibson 				     GFP_KERNEL);
2103925ca369SKent Gibson 		if (!le->label) {
2104925ca369SKent Gibson 			ret = -ENOMEM;
2105925ca369SKent Gibson 			goto out_free_le;
2106925ca369SKent Gibson 		}
2107925ca369SKent Gibson 	}
2108925ca369SKent Gibson 
210995a4eed7SAndy Shevchenko 	ret = gpiod_request_user(desc, le->label);
2110925ca369SKent Gibson 	if (ret)
211146824272SKent Gibson 		goto out_free_le;
2112925ca369SKent Gibson 	le->desc = desc;
2113925ca369SKent Gibson 	le->eflags = eflags;
2114925ca369SKent Gibson 
2115c274b58aSKent Gibson 	linehandle_flags_to_desc_flags(lflags, &desc->flags);
2116925ca369SKent Gibson 
2117925ca369SKent Gibson 	ret = gpiod_direction_input(desc);
2118925ca369SKent Gibson 	if (ret)
211946824272SKent Gibson 		goto out_free_le;
2120925ca369SKent Gibson 
21216accc376SKent Gibson 	blocking_notifier_call_chain(&desc->gdev->notifier,
2122aad95584SKent Gibson 				     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
2123925ca369SKent Gibson 
212446824272SKent Gibson 	irq = gpiod_to_irq(desc);
212546824272SKent Gibson 	if (irq <= 0) {
2126925ca369SKent Gibson 		ret = -ENODEV;
212746824272SKent Gibson 		goto out_free_le;
2128925ca369SKent Gibson 	}
2129925ca369SKent Gibson 
2130925ca369SKent Gibson 	if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
2131925ca369SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
2132925ca369SKent Gibson 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
2133925ca369SKent Gibson 	if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
2134925ca369SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
2135925ca369SKent Gibson 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
2136925ca369SKent Gibson 	irqflags |= IRQF_ONESHOT;
2137925ca369SKent Gibson 
2138925ca369SKent Gibson 	INIT_KFIFO(le->events);
2139925ca369SKent Gibson 	init_waitqueue_head(&le->wait);
2140925ca369SKent Gibson 
2141925ca369SKent Gibson 	/* Request a thread to read the events */
214269bef19dSMeng Li 	ret = request_threaded_irq(irq,
2143925ca369SKent Gibson 				   lineevent_irq_handler,
2144925ca369SKent Gibson 				   lineevent_irq_thread,
2145925ca369SKent Gibson 				   irqflags,
2146925ca369SKent Gibson 				   le->label,
2147925ca369SKent Gibson 				   le);
2148925ca369SKent Gibson 	if (ret)
214946824272SKent Gibson 		goto out_free_le;
2150925ca369SKent Gibson 
215169bef19dSMeng Li 	le->irq = irq;
215269bef19dSMeng Li 
2153925ca369SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
2154925ca369SKent Gibson 	if (fd < 0) {
2155925ca369SKent Gibson 		ret = fd;
215646824272SKent Gibson 		goto out_free_le;
2157925ca369SKent Gibson 	}
2158925ca369SKent Gibson 
2159925ca369SKent Gibson 	file = anon_inode_getfile("gpio-event",
2160925ca369SKent Gibson 				  &lineevent_fileops,
2161925ca369SKent Gibson 				  le,
2162925ca369SKent Gibson 				  O_RDONLY | O_CLOEXEC);
2163925ca369SKent Gibson 	if (IS_ERR(file)) {
2164925ca369SKent Gibson 		ret = PTR_ERR(file);
2165925ca369SKent Gibson 		goto out_put_unused_fd;
2166925ca369SKent Gibson 	}
2167925ca369SKent Gibson 
2168925ca369SKent Gibson 	eventreq.fd = fd;
2169925ca369SKent Gibson 	if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
2170925ca369SKent Gibson 		/*
2171925ca369SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
2172925ca369SKent Gibson 		 * the regular error cleanup path here.
2173925ca369SKent Gibson 		 */
2174925ca369SKent Gibson 		fput(file);
2175925ca369SKent Gibson 		put_unused_fd(fd);
2176925ca369SKent Gibson 		return -EFAULT;
2177925ca369SKent Gibson 	}
2178925ca369SKent Gibson 
2179925ca369SKent Gibson 	fd_install(fd, file);
2180925ca369SKent Gibson 
2181925ca369SKent Gibson 	return 0;
2182925ca369SKent Gibson 
2183925ca369SKent Gibson out_put_unused_fd:
2184925ca369SKent Gibson 	put_unused_fd(fd);
2185925ca369SKent Gibson out_free_le:
218646824272SKent Gibson 	lineevent_free(le);
2187925ca369SKent Gibson 	return ret;
2188925ca369SKent Gibson }
2189925ca369SKent Gibson 
2190aad95584SKent Gibson static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
2191aad95584SKent Gibson 				    struct gpioline_info *info_v1)
2192aad95584SKent Gibson {
2193aad95584SKent Gibson 	u64 flagsv2 = info_v2->flags;
2194aad95584SKent Gibson 
2195aad95584SKent Gibson 	memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
2196aad95584SKent Gibson 	memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
2197aad95584SKent Gibson 	info_v1->line_offset = info_v2->offset;
2198aad95584SKent Gibson 	info_v1->flags = 0;
2199aad95584SKent Gibson 
2200aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
2201aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_KERNEL;
2202aad95584SKent Gibson 
2203aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
2204aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
2205aad95584SKent Gibson 
2206aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
2207aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
2208aad95584SKent Gibson 
2209aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
2210aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
2211aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
2212aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
2213aad95584SKent Gibson 
2214aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
2215aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
2216aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
2217aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
2218aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
2219aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
2220aad95584SKent Gibson }
2221aad95584SKent Gibson 
2222aad95584SKent Gibson static void gpio_v2_line_info_changed_to_v1(
2223aad95584SKent Gibson 		struct gpio_v2_line_info_changed *lic_v2,
2224aad95584SKent Gibson 		struct gpioline_info_changed *lic_v1)
2225aad95584SKent Gibson {
2226cb8f63b8SGabriel Knezek 	memset(lic_v1, 0, sizeof(*lic_v1));
2227aad95584SKent Gibson 	gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
2228aad95584SKent Gibson 	lic_v1->timestamp = lic_v2->timestamp_ns;
2229aad95584SKent Gibson 	lic_v1->event_type = lic_v2->event_type;
2230aad95584SKent Gibson }
2231aad95584SKent Gibson 
22323c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
22333c0d9c63SKent Gibson 
2234925ca369SKent Gibson static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
2235aad95584SKent Gibson 				  struct gpio_v2_line_info *info)
2236925ca369SKent Gibson {
2237925ca369SKent Gibson 	struct gpio_chip *gc = desc->gdev->chip;
2238925ca369SKent Gibson 	bool ok_for_pinctrl;
2239925ca369SKent Gibson 	unsigned long flags;
224065cff704SKent Gibson 	u32 debounce_period_us;
224165cff704SKent Gibson 	unsigned int num_attrs = 0;
2242925ca369SKent Gibson 
224369e4e136SKent Gibson 	memset(info, 0, sizeof(*info));
2244aad95584SKent Gibson 	info->offset = gpio_chip_hwgpio(desc);
2245925ca369SKent Gibson 
2246925ca369SKent Gibson 	/*
2247925ca369SKent Gibson 	 * This function takes a mutex so we must check this before taking
2248925ca369SKent Gibson 	 * the spinlock.
2249925ca369SKent Gibson 	 *
2250925ca369SKent Gibson 	 * FIXME: find a non-racy way to retrieve this information. Maybe a
2251925ca369SKent Gibson 	 * lock common to both frameworks?
2252925ca369SKent Gibson 	 */
2253925ca369SKent Gibson 	ok_for_pinctrl =
2254aad95584SKent Gibson 		pinctrl_gpio_can_use_line(gc->base + info->offset);
2255925ca369SKent Gibson 
2256925ca369SKent Gibson 	spin_lock_irqsave(&gpio_lock, flags);
2257925ca369SKent Gibson 
225869e4e136SKent Gibson 	if (desc->name)
225969e4e136SKent Gibson 		strscpy(info->name, desc->name, sizeof(info->name));
2260925ca369SKent Gibson 
226169e4e136SKent Gibson 	if (desc->label)
226269e4e136SKent Gibson 		strscpy(info->consumer, desc->label, sizeof(info->consumer));
2263925ca369SKent Gibson 
2264925ca369SKent Gibson 	/*
2265925ca369SKent Gibson 	 * Userspace only need to know that the kernel is using this GPIO so
2266925ca369SKent Gibson 	 * it can't use it.
2267925ca369SKent Gibson 	 */
2268925ca369SKent Gibson 	info->flags = 0;
2269925ca369SKent Gibson 	if (test_bit(FLAG_REQUESTED, &desc->flags) ||
2270925ca369SKent Gibson 	    test_bit(FLAG_IS_HOGGED, &desc->flags) ||
2271925ca369SKent Gibson 	    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
2272925ca369SKent Gibson 	    test_bit(FLAG_EXPORT, &desc->flags) ||
2273925ca369SKent Gibson 	    test_bit(FLAG_SYSFS, &desc->flags) ||
2274a0db197fSMarc Zyngier 	    !gpiochip_line_is_valid(gc, info->offset) ||
2275925ca369SKent Gibson 	    !ok_for_pinctrl)
2276aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_USED;
2277aad95584SKent Gibson 
2278925ca369SKent Gibson 	if (test_bit(FLAG_IS_OUT, &desc->flags))
2279aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
2280aad95584SKent Gibson 	else
2281aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_INPUT;
2282aad95584SKent Gibson 
2283925ca369SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2284aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
2285aad95584SKent Gibson 
2286925ca369SKent Gibson 	if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2287aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
2288925ca369SKent Gibson 	if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2289aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
2290aad95584SKent Gibson 
2291925ca369SKent Gibson 	if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2292aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
2293925ca369SKent Gibson 	if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2294aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
2295925ca369SKent Gibson 	if (test_bit(FLAG_PULL_UP, &desc->flags))
2296aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
2297925ca369SKent Gibson 
229873e03419SKent Gibson 	if (test_bit(FLAG_EDGE_RISING, &desc->flags))
229973e03419SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
230073e03419SKent Gibson 	if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
230173e03419SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
230273e03419SKent Gibson 
230326d060e4SKent Gibson 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
230426d060e4SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
23052068339aSDipen Patel 	else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags))
23062068339aSDipen Patel 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
230726d060e4SKent Gibson 
230865cff704SKent Gibson 	debounce_period_us = READ_ONCE(desc->debounce_period_us);
230965cff704SKent Gibson 	if (debounce_period_us) {
231065cff704SKent Gibson 		info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
231165cff704SKent Gibson 		info->attrs[num_attrs].debounce_period_us = debounce_period_us;
231265cff704SKent Gibson 		num_attrs++;
231365cff704SKent Gibson 	}
231465cff704SKent Gibson 	info->num_attrs = num_attrs;
2315925ca369SKent Gibson 
2316925ca369SKent Gibson 	spin_unlock_irqrestore(&gpio_lock, flags);
2317925ca369SKent Gibson }
2318925ca369SKent Gibson 
2319925ca369SKent Gibson struct gpio_chardev_data {
2320925ca369SKent Gibson 	struct gpio_device *gdev;
2321925ca369SKent Gibson 	wait_queue_head_t wait;
2322aad95584SKent Gibson 	DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
2323925ca369SKent Gibson 	struct notifier_block lineinfo_changed_nb;
2324925ca369SKent Gibson 	unsigned long *watched_lines;
2325aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2326aad95584SKent Gibson 	atomic_t watch_abi_version;
2327aad95584SKent Gibson #endif
2328925ca369SKent Gibson };
2329925ca369SKent Gibson 
23302e202ad8SKent Gibson static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
23312e202ad8SKent Gibson {
23322e202ad8SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
23332e202ad8SKent Gibson 	struct gpiochip_info chipinfo;
23342e202ad8SKent Gibson 
23352e202ad8SKent Gibson 	memset(&chipinfo, 0, sizeof(chipinfo));
23362e202ad8SKent Gibson 
23372e202ad8SKent Gibson 	strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
23382e202ad8SKent Gibson 	strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
23392e202ad8SKent Gibson 	chipinfo.lines = gdev->ngpio;
23402e202ad8SKent Gibson 	if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
23412e202ad8SKent Gibson 		return -EFAULT;
23422e202ad8SKent Gibson 	return 0;
23432e202ad8SKent Gibson }
23442e202ad8SKent Gibson 
2345aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2346aad95584SKent Gibson /*
2347aad95584SKent Gibson  * returns 0 if the versions match, else the previously selected ABI version
2348aad95584SKent Gibson  */
2349aad95584SKent Gibson static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
2350aad95584SKent Gibson 				       unsigned int version)
2351aad95584SKent Gibson {
2352aad95584SKent Gibson 	int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
2353aad95584SKent Gibson 
2354aad95584SKent Gibson 	if (abiv == version)
2355aad95584SKent Gibson 		return 0;
2356aad95584SKent Gibson 
2357aad95584SKent Gibson 	return abiv;
2358aad95584SKent Gibson }
23592e202ad8SKent Gibson 
23602e202ad8SKent Gibson static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
23612e202ad8SKent Gibson 			   bool watch)
23622e202ad8SKent Gibson {
23632e202ad8SKent Gibson 	struct gpio_desc *desc;
23642e202ad8SKent Gibson 	struct gpioline_info lineinfo;
23652e202ad8SKent Gibson 	struct gpio_v2_line_info lineinfo_v2;
23662e202ad8SKent Gibson 
23672e202ad8SKent Gibson 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
23682e202ad8SKent Gibson 		return -EFAULT;
23692e202ad8SKent Gibson 
23702e202ad8SKent Gibson 	/* this doubles as a range check on line_offset */
23712e202ad8SKent Gibson 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset);
23722e202ad8SKent Gibson 	if (IS_ERR(desc))
23732e202ad8SKent Gibson 		return PTR_ERR(desc);
23742e202ad8SKent Gibson 
23752e202ad8SKent Gibson 	if (watch) {
23762e202ad8SKent Gibson 		if (lineinfo_ensure_abi_version(cdev, 1))
23772e202ad8SKent Gibson 			return -EPERM;
23782e202ad8SKent Gibson 
23792e202ad8SKent Gibson 		if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
23802e202ad8SKent Gibson 			return -EBUSY;
23812e202ad8SKent Gibson 	}
23822e202ad8SKent Gibson 
23832e202ad8SKent Gibson 	gpio_desc_to_lineinfo(desc, &lineinfo_v2);
23842e202ad8SKent Gibson 	gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
23852e202ad8SKent Gibson 
23862e202ad8SKent Gibson 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
23872e202ad8SKent Gibson 		if (watch)
23882e202ad8SKent Gibson 			clear_bit(lineinfo.line_offset, cdev->watched_lines);
23892e202ad8SKent Gibson 		return -EFAULT;
23902e202ad8SKent Gibson 	}
23912e202ad8SKent Gibson 
23922e202ad8SKent Gibson 	return 0;
23932e202ad8SKent Gibson }
2394aad95584SKent Gibson #endif
2395aad95584SKent Gibson 
2396aad95584SKent Gibson static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
2397aad95584SKent Gibson 			bool watch)
2398aad95584SKent Gibson {
2399aad95584SKent Gibson 	struct gpio_desc *desc;
2400aad95584SKent Gibson 	struct gpio_v2_line_info lineinfo;
2401aad95584SKent Gibson 
2402aad95584SKent Gibson 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2403aad95584SKent Gibson 		return -EFAULT;
2404aad95584SKent Gibson 
2405aad95584SKent Gibson 	if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2406aad95584SKent Gibson 		return -EINVAL;
2407aad95584SKent Gibson 
2408aad95584SKent Gibson 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2409aad95584SKent Gibson 	if (IS_ERR(desc))
2410aad95584SKent Gibson 		return PTR_ERR(desc);
2411aad95584SKent Gibson 
2412aad95584SKent Gibson 	if (watch) {
2413aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2414aad95584SKent Gibson 		if (lineinfo_ensure_abi_version(cdev, 2))
2415aad95584SKent Gibson 			return -EPERM;
2416aad95584SKent Gibson #endif
2417aad95584SKent Gibson 		if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2418aad95584SKent Gibson 			return -EBUSY;
2419aad95584SKent Gibson 	}
2420aad95584SKent Gibson 	gpio_desc_to_lineinfo(desc, &lineinfo);
2421aad95584SKent Gibson 
2422aad95584SKent Gibson 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2423aad95584SKent Gibson 		if (watch)
2424aad95584SKent Gibson 			clear_bit(lineinfo.offset, cdev->watched_lines);
2425aad95584SKent Gibson 		return -EFAULT;
2426aad95584SKent Gibson 	}
2427aad95584SKent Gibson 
2428aad95584SKent Gibson 	return 0;
2429aad95584SKent Gibson }
2430aad95584SKent Gibson 
24312e202ad8SKent Gibson static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
2432925ca369SKent Gibson {
2433925ca369SKent Gibson 	__u32 offset;
2434925ca369SKent Gibson 
2435925ca369SKent Gibson 	if (copy_from_user(&offset, ip, sizeof(offset)))
2436925ca369SKent Gibson 		return -EFAULT;
2437925ca369SKent Gibson 
24381bf7ba40SKent Gibson 	if (offset >= cdev->gdev->ngpio)
24391bf7ba40SKent Gibson 		return -EINVAL;
2440925ca369SKent Gibson 
24411bf7ba40SKent Gibson 	if (!test_and_clear_bit(offset, cdev->watched_lines))
2442925ca369SKent Gibson 		return -EBUSY;
2443925ca369SKent Gibson 
2444925ca369SKent Gibson 	return 0;
2445925ca369SKent Gibson }
24462e202ad8SKent Gibson 
24472e202ad8SKent Gibson /*
24482e202ad8SKent Gibson  * gpio_ioctl() - ioctl handler for the GPIO chardev
24492e202ad8SKent Gibson  */
24502e202ad8SKent Gibson static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
24512e202ad8SKent Gibson {
24522e202ad8SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
24532e202ad8SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
24542e202ad8SKent Gibson 	void __user *ip = (void __user *)arg;
24552e202ad8SKent Gibson 
24562e202ad8SKent Gibson 	/* We fail any subsequent ioctl():s when the chip is gone */
24572e202ad8SKent Gibson 	if (!gdev->chip)
24582e202ad8SKent Gibson 		return -ENODEV;
24592e202ad8SKent Gibson 
24602e202ad8SKent Gibson 	/* Fill in the struct and pass to userspace */
24611cef8b50SAndy Shevchenko 	switch (cmd) {
24621cef8b50SAndy Shevchenko 	case GPIO_GET_CHIPINFO_IOCTL:
24632e202ad8SKent Gibson 		return chipinfo_get(cdev, ip);
24642e202ad8SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
24651cef8b50SAndy Shevchenko 	case GPIO_GET_LINEHANDLE_IOCTL:
24662e202ad8SKent Gibson 		return linehandle_create(gdev, ip);
24671cef8b50SAndy Shevchenko 	case GPIO_GET_LINEEVENT_IOCTL:
24682e202ad8SKent Gibson 		return lineevent_create(gdev, ip);
24691cef8b50SAndy Shevchenko 	case GPIO_GET_LINEINFO_IOCTL:
24701cef8b50SAndy Shevchenko 		return lineinfo_get_v1(cdev, ip, false);
24711cef8b50SAndy Shevchenko 	case GPIO_GET_LINEINFO_WATCH_IOCTL:
24721cef8b50SAndy Shevchenko 		return lineinfo_get_v1(cdev, ip, true);
24732e202ad8SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
24741cef8b50SAndy Shevchenko 	case GPIO_V2_GET_LINEINFO_IOCTL:
24751cef8b50SAndy Shevchenko 		return lineinfo_get(cdev, ip, false);
24761cef8b50SAndy Shevchenko 	case GPIO_V2_GET_LINEINFO_WATCH_IOCTL:
24771cef8b50SAndy Shevchenko 		return lineinfo_get(cdev, ip, true);
24781cef8b50SAndy Shevchenko 	case GPIO_V2_GET_LINE_IOCTL:
24792e202ad8SKent Gibson 		return linereq_create(gdev, ip);
24801cef8b50SAndy Shevchenko 	case GPIO_GET_LINEINFO_UNWATCH_IOCTL:
24812e202ad8SKent Gibson 		return lineinfo_unwatch(cdev, ip);
24821cef8b50SAndy Shevchenko 	default:
2483925ca369SKent Gibson 		return -EINVAL;
2484925ca369SKent Gibson 	}
24851cef8b50SAndy Shevchenko }
2486925ca369SKent Gibson 
2487925ca369SKent Gibson #ifdef CONFIG_COMPAT
248849bc5279SKent Gibson static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2489925ca369SKent Gibson 			      unsigned long arg)
2490925ca369SKent Gibson {
249149bc5279SKent Gibson 	return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2492925ca369SKent Gibson }
2493925ca369SKent Gibson #endif
2494925ca369SKent Gibson 
2495925ca369SKent Gibson static struct gpio_chardev_data *
2496925ca369SKent Gibson to_gpio_chardev_data(struct notifier_block *nb)
2497925ca369SKent Gibson {
2498925ca369SKent Gibson 	return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2499925ca369SKent Gibson }
2500925ca369SKent Gibson 
2501925ca369SKent Gibson static int lineinfo_changed_notify(struct notifier_block *nb,
2502925ca369SKent Gibson 				   unsigned long action, void *data)
2503925ca369SKent Gibson {
2504e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
2505aad95584SKent Gibson 	struct gpio_v2_line_info_changed chg;
2506925ca369SKent Gibson 	struct gpio_desc *desc = data;
2507925ca369SKent Gibson 	int ret;
2508925ca369SKent Gibson 
2509e2b781c5SKent Gibson 	if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2510925ca369SKent Gibson 		return NOTIFY_DONE;
2511925ca369SKent Gibson 
2512925ca369SKent Gibson 	memset(&chg, 0, sizeof(chg));
2513925ca369SKent Gibson 	chg.event_type = action;
2514aad95584SKent Gibson 	chg.timestamp_ns = ktime_get_ns();
2515925ca369SKent Gibson 	gpio_desc_to_lineinfo(desc, &chg.info);
2516925ca369SKent Gibson 
2517e2b781c5SKent Gibson 	ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2518925ca369SKent Gibson 	if (ret)
2519e2b781c5SKent Gibson 		wake_up_poll(&cdev->wait, EPOLLIN);
2520925ca369SKent Gibson 	else
2521925ca369SKent Gibson 		pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2522925ca369SKent Gibson 
2523925ca369SKent Gibson 	return NOTIFY_OK;
2524925ca369SKent Gibson }
2525925ca369SKent Gibson 
2526*bdbbae24SBartosz Golaszewski static __poll_t lineinfo_watch_poll_unlocked(struct file *file,
2527925ca369SKent Gibson 					     struct poll_table_struct *pollt)
2528925ca369SKent Gibson {
2529e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2530925ca369SKent Gibson 	__poll_t events = 0;
2531925ca369SKent Gibson 
2532533aae7cSBartosz Golaszewski 	if (!cdev->gdev->chip)
2533533aae7cSBartosz Golaszewski 		return EPOLLHUP | EPOLLERR;
2534533aae7cSBartosz Golaszewski 
2535e2b781c5SKent Gibson 	poll_wait(file, &cdev->wait, pollt);
2536925ca369SKent Gibson 
2537e2b781c5SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2538e2b781c5SKent Gibson 						 &cdev->wait.lock))
2539925ca369SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
2540925ca369SKent Gibson 
2541925ca369SKent Gibson 	return events;
2542925ca369SKent Gibson }
2543925ca369SKent Gibson 
2544*bdbbae24SBartosz Golaszewski static __poll_t lineinfo_watch_poll(struct file *file,
2545*bdbbae24SBartosz Golaszewski 				    struct poll_table_struct *pollt)
2546*bdbbae24SBartosz Golaszewski {
2547*bdbbae24SBartosz Golaszewski 	struct gpio_chardev_data *cdev = file->private_data;
2548*bdbbae24SBartosz Golaszewski 
2549*bdbbae24SBartosz Golaszewski 	return call_poll_locked(file, pollt, cdev->gdev,
2550*bdbbae24SBartosz Golaszewski 				lineinfo_watch_poll_unlocked);
2551*bdbbae24SBartosz Golaszewski }
2552*bdbbae24SBartosz Golaszewski 
2553*bdbbae24SBartosz Golaszewski static ssize_t lineinfo_watch_read_unlocked(struct file *file, char __user *buf,
2554925ca369SKent Gibson 					    size_t count, loff_t *off)
2555925ca369SKent Gibson {
2556e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2557aad95584SKent Gibson 	struct gpio_v2_line_info_changed event;
2558925ca369SKent Gibson 	ssize_t bytes_read = 0;
2559925ca369SKent Gibson 	int ret;
2560aad95584SKent Gibson 	size_t event_size;
2561925ca369SKent Gibson 
2562533aae7cSBartosz Golaszewski 	if (!cdev->gdev->chip)
2563533aae7cSBartosz Golaszewski 		return -ENODEV;
2564533aae7cSBartosz Golaszewski 
2565aad95584SKent Gibson #ifndef CONFIG_GPIO_CDEV_V1
2566aad95584SKent Gibson 	event_size = sizeof(struct gpio_v2_line_info_changed);
2567aad95584SKent Gibson 	if (count < event_size)
2568925ca369SKent Gibson 		return -EINVAL;
2569aad95584SKent Gibson #endif
2570925ca369SKent Gibson 
2571925ca369SKent Gibson 	do {
2572e2b781c5SKent Gibson 		spin_lock(&cdev->wait.lock);
2573e2b781c5SKent Gibson 		if (kfifo_is_empty(&cdev->events)) {
2574925ca369SKent Gibson 			if (bytes_read) {
2575e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2576925ca369SKent Gibson 				return bytes_read;
2577925ca369SKent Gibson 			}
2578925ca369SKent Gibson 
257949bc5279SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
2580e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2581925ca369SKent Gibson 				return -EAGAIN;
2582925ca369SKent Gibson 			}
2583925ca369SKent Gibson 
2584e2b781c5SKent Gibson 			ret = wait_event_interruptible_locked(cdev->wait,
2585e2b781c5SKent Gibson 					!kfifo_is_empty(&cdev->events));
2586925ca369SKent Gibson 			if (ret) {
2587e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2588925ca369SKent Gibson 				return ret;
2589925ca369SKent Gibson 			}
2590925ca369SKent Gibson 		}
2591aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2592aad95584SKent Gibson 		/* must be after kfifo check so watch_abi_version is set */
2593aad95584SKent Gibson 		if (atomic_read(&cdev->watch_abi_version) == 2)
2594aad95584SKent Gibson 			event_size = sizeof(struct gpio_v2_line_info_changed);
2595aad95584SKent Gibson 		else
2596aad95584SKent Gibson 			event_size = sizeof(struct gpioline_info_changed);
2597aad95584SKent Gibson 		if (count < event_size) {
2598aad95584SKent Gibson 			spin_unlock(&cdev->wait.lock);
2599aad95584SKent Gibson 			return -EINVAL;
2600aad95584SKent Gibson 		}
2601aad95584SKent Gibson #endif
2602e2b781c5SKent Gibson 		ret = kfifo_out(&cdev->events, &event, 1);
2603e2b781c5SKent Gibson 		spin_unlock(&cdev->wait.lock);
2604925ca369SKent Gibson 		if (ret != 1) {
2605925ca369SKent Gibson 			ret = -EIO;
2606925ca369SKent Gibson 			break;
2607925ca369SKent Gibson 			/* We should never get here. See lineevent_read(). */
2608925ca369SKent Gibson 		}
2609925ca369SKent Gibson 
2610aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2611aad95584SKent Gibson 		if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2612aad95584SKent Gibson 			if (copy_to_user(buf + bytes_read, &event, event_size))
2613925ca369SKent Gibson 				return -EFAULT;
2614aad95584SKent Gibson 		} else {
2615aad95584SKent Gibson 			struct gpioline_info_changed event_v1;
2616aad95584SKent Gibson 
2617aad95584SKent Gibson 			gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2618aad95584SKent Gibson 			if (copy_to_user(buf + bytes_read, &event_v1,
2619aad95584SKent Gibson 					 event_size))
2620aad95584SKent Gibson 				return -EFAULT;
2621aad95584SKent Gibson 		}
2622aad95584SKent Gibson #else
2623aad95584SKent Gibson 		if (copy_to_user(buf + bytes_read, &event, event_size))
2624aad95584SKent Gibson 			return -EFAULT;
2625aad95584SKent Gibson #endif
2626aad95584SKent Gibson 		bytes_read += event_size;
2627925ca369SKent Gibson 	} while (count >= bytes_read + sizeof(event));
2628925ca369SKent Gibson 
2629925ca369SKent Gibson 	return bytes_read;
2630925ca369SKent Gibson }
2631925ca369SKent Gibson 
2632*bdbbae24SBartosz Golaszewski static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2633*bdbbae24SBartosz Golaszewski 				   size_t count, loff_t *off)
2634*bdbbae24SBartosz Golaszewski {
2635*bdbbae24SBartosz Golaszewski 	struct gpio_chardev_data *cdev = file->private_data;
2636*bdbbae24SBartosz Golaszewski 
2637*bdbbae24SBartosz Golaszewski 	return call_read_locked(file, buf, count, off, cdev->gdev,
2638*bdbbae24SBartosz Golaszewski 				lineinfo_watch_read_unlocked);
2639*bdbbae24SBartosz Golaszewski }
2640*bdbbae24SBartosz Golaszewski 
2641925ca369SKent Gibson /**
2642925ca369SKent Gibson  * gpio_chrdev_open() - open the chardev for ioctl operations
2643925ca369SKent Gibson  * @inode: inode for this chardev
264449bc5279SKent Gibson  * @file: file struct for storing private data
2645925ca369SKent Gibson  * Returns 0 on success
2646925ca369SKent Gibson  */
264749bc5279SKent Gibson static int gpio_chrdev_open(struct inode *inode, struct file *file)
2648925ca369SKent Gibson {
2649925ca369SKent Gibson 	struct gpio_device *gdev = container_of(inode->i_cdev,
2650925ca369SKent Gibson 						struct gpio_device, chrdev);
2651e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev;
2652925ca369SKent Gibson 	int ret = -ENOMEM;
2653925ca369SKent Gibson 
2654*bdbbae24SBartosz Golaszewski 	down_read(&gdev->sem);
2655*bdbbae24SBartosz Golaszewski 
2656925ca369SKent Gibson 	/* Fail on open if the backing gpiochip is gone */
2657*bdbbae24SBartosz Golaszewski 	if (!gdev->chip) {
2658*bdbbae24SBartosz Golaszewski 		ret = -ENODEV;
2659*bdbbae24SBartosz Golaszewski 		goto out_unlock;
2660*bdbbae24SBartosz Golaszewski 	}
2661925ca369SKent Gibson 
2662e2b781c5SKent Gibson 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2663e2b781c5SKent Gibson 	if (!cdev)
2664*bdbbae24SBartosz Golaszewski 		goto out_unlock;
2665925ca369SKent Gibson 
2666e2b781c5SKent Gibson 	cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2667e2b781c5SKent Gibson 	if (!cdev->watched_lines)
2668e2b781c5SKent Gibson 		goto out_free_cdev;
2669925ca369SKent Gibson 
2670e2b781c5SKent Gibson 	init_waitqueue_head(&cdev->wait);
2671e2b781c5SKent Gibson 	INIT_KFIFO(cdev->events);
2672e2b781c5SKent Gibson 	cdev->gdev = gdev;
2673925ca369SKent Gibson 
2674e2b781c5SKent Gibson 	cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
26756accc376SKent Gibson 	ret = blocking_notifier_chain_register(&gdev->notifier,
2676e2b781c5SKent Gibson 					       &cdev->lineinfo_changed_nb);
2677925ca369SKent Gibson 	if (ret)
2678925ca369SKent Gibson 		goto out_free_bitmap;
2679925ca369SKent Gibson 
2680925ca369SKent Gibson 	get_device(&gdev->dev);
2681e2b781c5SKent Gibson 	file->private_data = cdev;
2682925ca369SKent Gibson 
268349bc5279SKent Gibson 	ret = nonseekable_open(inode, file);
2684925ca369SKent Gibson 	if (ret)
2685925ca369SKent Gibson 		goto out_unregister_notifier;
2686925ca369SKent Gibson 
2687*bdbbae24SBartosz Golaszewski 	up_read(&gdev->sem);
2688*bdbbae24SBartosz Golaszewski 
2689925ca369SKent Gibson 	return ret;
2690925ca369SKent Gibson 
2691925ca369SKent Gibson out_unregister_notifier:
26926accc376SKent Gibson 	blocking_notifier_chain_unregister(&gdev->notifier,
2693e2b781c5SKent Gibson 					   &cdev->lineinfo_changed_nb);
2694925ca369SKent Gibson out_free_bitmap:
2695e2b781c5SKent Gibson 	bitmap_free(cdev->watched_lines);
2696e2b781c5SKent Gibson out_free_cdev:
2697e2b781c5SKent Gibson 	kfree(cdev);
2698*bdbbae24SBartosz Golaszewski out_unlock:
2699*bdbbae24SBartosz Golaszewski 	up_read(&gdev->sem);
2700925ca369SKent Gibson 	return ret;
2701925ca369SKent Gibson }
2702925ca369SKent Gibson 
2703925ca369SKent Gibson /**
2704925ca369SKent Gibson  * gpio_chrdev_release() - close chardev after ioctl operations
2705925ca369SKent Gibson  * @inode: inode for this chardev
270649bc5279SKent Gibson  * @file: file struct for storing private data
2707925ca369SKent Gibson  * Returns 0 on success
2708925ca369SKent Gibson  */
270949bc5279SKent Gibson static int gpio_chrdev_release(struct inode *inode, struct file *file)
2710925ca369SKent Gibson {
2711e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2712e2b781c5SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
2713925ca369SKent Gibson 
2714e2b781c5SKent Gibson 	bitmap_free(cdev->watched_lines);
27156accc376SKent Gibson 	blocking_notifier_chain_unregister(&gdev->notifier,
2716e2b781c5SKent Gibson 					   &cdev->lineinfo_changed_nb);
2717925ca369SKent Gibson 	put_device(&gdev->dev);
2718e2b781c5SKent Gibson 	kfree(cdev);
2719925ca369SKent Gibson 
2720925ca369SKent Gibson 	return 0;
2721925ca369SKent Gibson }
2722925ca369SKent Gibson 
2723925ca369SKent Gibson static const struct file_operations gpio_fileops = {
2724925ca369SKent Gibson 	.release = gpio_chrdev_release,
2725925ca369SKent Gibson 	.open = gpio_chrdev_open,
2726925ca369SKent Gibson 	.poll = lineinfo_watch_poll,
2727925ca369SKent Gibson 	.read = lineinfo_watch_read,
2728925ca369SKent Gibson 	.owner = THIS_MODULE,
2729925ca369SKent Gibson 	.llseek = no_llseek,
2730925ca369SKent Gibson 	.unlocked_ioctl = gpio_ioctl,
2731925ca369SKent Gibson #ifdef CONFIG_COMPAT
2732925ca369SKent Gibson 	.compat_ioctl = gpio_ioctl_compat,
2733925ca369SKent Gibson #endif
2734925ca369SKent Gibson };
2735925ca369SKent Gibson 
2736925ca369SKent Gibson int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2737925ca369SKent Gibson {
2738925ca369SKent Gibson 	int ret;
2739925ca369SKent Gibson 
2740925ca369SKent Gibson 	cdev_init(&gdev->chrdev, &gpio_fileops);
2741925ca369SKent Gibson 	gdev->chrdev.owner = THIS_MODULE;
2742925ca369SKent Gibson 	gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2743925ca369SKent Gibson 
2744925ca369SKent Gibson 	ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2745925ca369SKent Gibson 	if (ret)
2746925ca369SKent Gibson 		return ret;
2747925ca369SKent Gibson 
2748925ca369SKent Gibson 	chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2749925ca369SKent Gibson 		 MAJOR(devt), gdev->id);
2750925ca369SKent Gibson 
2751925ca369SKent Gibson 	return 0;
2752925ca369SKent Gibson }
2753925ca369SKent Gibson 
2754925ca369SKent Gibson void gpiolib_cdev_unregister(struct gpio_device *gdev)
2755925ca369SKent Gibson {
2756925ca369SKent Gibson 	cdev_device_del(&gdev->chrdev, &gdev->dev);
2757925ca369SKent Gibson }
2758