xref: /openbmc/linux/drivers/gpio/gpiolib-cdev.c (revision 883e4bbf06eb5fb7482679e4edb201093e9f55a2)
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>
8309364bcSKent Gibson #include <linux/cleanup.h>
9d189f627SKent Gibson #include <linux/compat.h>
1065cff704SKent Gibson #include <linux/compiler.h>
11925ca369SKent Gibson #include <linux/device.h>
12925ca369SKent Gibson #include <linux/err.h>
13d189f627SKent Gibson #include <linux/file.h>
14925ca369SKent Gibson #include <linux/gpio.h>
15925ca369SKent Gibson #include <linux/gpio/driver.h>
1652ee7c02SAndy Shevchenko #include <linux/hte.h>
17d189f627SKent Gibson #include <linux/interrupt.h>
18d189f627SKent Gibson #include <linux/irqreturn.h>
19d189f627SKent Gibson #include <linux/kernel.h>
20925ca369SKent Gibson #include <linux/kfifo.h>
21d189f627SKent Gibson #include <linux/module.h>
22a54756cbSKent Gibson #include <linux/mutex.h>
23d189f627SKent Gibson #include <linux/pinctrl/consumer.h>
24925ca369SKent Gibson #include <linux/poll.h>
25309364bcSKent Gibson #include <linux/rbtree.h>
2652ee7c02SAndy Shevchenko #include <linux/seq_file.h>
27d189f627SKent Gibson #include <linux/spinlock.h>
28925ca369SKent Gibson #include <linux/timekeeping.h>
29d189f627SKent Gibson #include <linux/uaccess.h>
3065cff704SKent Gibson #include <linux/workqueue.h>
3152ee7c02SAndy Shevchenko 
32925ca369SKent Gibson #include <uapi/linux/gpio.h>
33925ca369SKent Gibson 
34925ca369SKent Gibson #include "gpiolib.h"
35925ca369SKent Gibson #include "gpiolib-cdev.h"
36925ca369SKent Gibson 
373c0d9c63SKent Gibson /*
383c0d9c63SKent Gibson  * Array sizes must ensure 64-bit alignment and not create holes in the
393c0d9c63SKent Gibson  * struct packing.
403c0d9c63SKent Gibson  */
413c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2));
423c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8));
433c0d9c63SKent Gibson 
443c0d9c63SKent Gibson /*
453c0d9c63SKent Gibson  * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility
463c0d9c63SKent Gibson  */
473c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8));
483c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8));
493c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8));
503c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8));
513c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8));
523c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8));
533c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8));
543c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8));
553c0d9c63SKent Gibson 
56925ca369SKent Gibson /* Character device interface to GPIO.
57925ca369SKent Gibson  *
58925ca369SKent Gibson  * The GPIO character device, /dev/gpiochipN, provides userspace an
59925ca369SKent Gibson  * interface to gpiolib GPIOs via ioctl()s.
60925ca369SKent Gibson  */
61925ca369SKent Gibson 
62bdbbae24SBartosz Golaszewski typedef __poll_t (*poll_fn)(struct file *, struct poll_table_struct *);
63bdbbae24SBartosz Golaszewski typedef long (*ioctl_fn)(struct file *, unsigned int, unsigned long);
64bdbbae24SBartosz Golaszewski typedef ssize_t (*read_fn)(struct file *, char __user *,
65bdbbae24SBartosz Golaszewski 			   size_t count, loff_t *);
66bdbbae24SBartosz Golaszewski 
67bdbbae24SBartosz Golaszewski static __poll_t call_poll_locked(struct file *file,
68bdbbae24SBartosz Golaszewski 				 struct poll_table_struct *wait,
69bdbbae24SBartosz Golaszewski 				 struct gpio_device *gdev, poll_fn func)
70bdbbae24SBartosz Golaszewski {
71bdbbae24SBartosz Golaszewski 	__poll_t ret;
72bdbbae24SBartosz Golaszewski 
73bdbbae24SBartosz Golaszewski 	down_read(&gdev->sem);
74bdbbae24SBartosz Golaszewski 	ret = func(file, wait);
75bdbbae24SBartosz Golaszewski 	up_read(&gdev->sem);
76bdbbae24SBartosz Golaszewski 
77bdbbae24SBartosz Golaszewski 	return ret;
78bdbbae24SBartosz Golaszewski }
79bdbbae24SBartosz Golaszewski 
80bdbbae24SBartosz Golaszewski static long call_ioctl_locked(struct file *file, unsigned int cmd,
81bdbbae24SBartosz Golaszewski 			      unsigned long arg, struct gpio_device *gdev,
82bdbbae24SBartosz Golaszewski 			      ioctl_fn func)
83bdbbae24SBartosz Golaszewski {
84bdbbae24SBartosz Golaszewski 	long ret;
85bdbbae24SBartosz Golaszewski 
86bdbbae24SBartosz Golaszewski 	down_read(&gdev->sem);
87bdbbae24SBartosz Golaszewski 	ret = func(file, cmd, arg);
88bdbbae24SBartosz Golaszewski 	up_read(&gdev->sem);
89bdbbae24SBartosz Golaszewski 
90bdbbae24SBartosz Golaszewski 	return ret;
91bdbbae24SBartosz Golaszewski }
92bdbbae24SBartosz Golaszewski 
93bdbbae24SBartosz Golaszewski static ssize_t call_read_locked(struct file *file, char __user *buf,
94bdbbae24SBartosz Golaszewski 				size_t count, loff_t *f_ps,
95bdbbae24SBartosz Golaszewski 				struct gpio_device *gdev, read_fn func)
96bdbbae24SBartosz Golaszewski {
97bdbbae24SBartosz Golaszewski 	ssize_t ret;
98bdbbae24SBartosz Golaszewski 
99bdbbae24SBartosz Golaszewski 	down_read(&gdev->sem);
100bdbbae24SBartosz Golaszewski 	ret = func(file, buf, count, f_ps);
101bdbbae24SBartosz Golaszewski 	up_read(&gdev->sem);
102bdbbae24SBartosz Golaszewski 
103bdbbae24SBartosz Golaszewski 	return ret;
104bdbbae24SBartosz Golaszewski }
105bdbbae24SBartosz Golaszewski 
106925ca369SKent Gibson /*
107925ca369SKent Gibson  * GPIO line handle management
108925ca369SKent Gibson  */
109925ca369SKent Gibson 
1103c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
111925ca369SKent Gibson /**
112925ca369SKent Gibson  * struct linehandle_state - contains the state of a userspace handle
113925ca369SKent Gibson  * @gdev: the GPIO device the handle pertains to
114925ca369SKent Gibson  * @label: consumer label used to tag descriptors
115925ca369SKent Gibson  * @descs: the GPIO descriptors held by this handle
11652b7b596SKent Gibson  * @num_descs: the number of descriptors held in the descs array
117925ca369SKent Gibson  */
118925ca369SKent Gibson struct linehandle_state {
119925ca369SKent Gibson 	struct gpio_device *gdev;
120925ca369SKent Gibson 	const char *label;
121925ca369SKent Gibson 	struct gpio_desc *descs[GPIOHANDLES_MAX];
12252b7b596SKent Gibson 	u32 num_descs;
123925ca369SKent Gibson };
124925ca369SKent Gibson 
125925ca369SKent Gibson #define GPIOHANDLE_REQUEST_VALID_FLAGS \
126925ca369SKent Gibson 	(GPIOHANDLE_REQUEST_INPUT | \
127925ca369SKent Gibson 	GPIOHANDLE_REQUEST_OUTPUT | \
128925ca369SKent Gibson 	GPIOHANDLE_REQUEST_ACTIVE_LOW | \
129925ca369SKent Gibson 	GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
130925ca369SKent Gibson 	GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
131925ca369SKent Gibson 	GPIOHANDLE_REQUEST_BIAS_DISABLE | \
132925ca369SKent Gibson 	GPIOHANDLE_REQUEST_OPEN_DRAIN | \
133925ca369SKent Gibson 	GPIOHANDLE_REQUEST_OPEN_SOURCE)
134925ca369SKent Gibson 
135925ca369SKent Gibson static int linehandle_validate_flags(u32 flags)
136925ca369SKent Gibson {
137925ca369SKent Gibson 	/* Return an error if an unknown flag is set */
138925ca369SKent Gibson 	if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
139925ca369SKent Gibson 		return -EINVAL;
140925ca369SKent Gibson 
141925ca369SKent Gibson 	/*
142925ca369SKent Gibson 	 * Do not allow both INPUT & OUTPUT flags to be set as they are
143925ca369SKent Gibson 	 * contradictory.
144925ca369SKent Gibson 	 */
145925ca369SKent Gibson 	if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
146925ca369SKent Gibson 	    (flags & GPIOHANDLE_REQUEST_OUTPUT))
147925ca369SKent Gibson 		return -EINVAL;
148925ca369SKent Gibson 
149925ca369SKent Gibson 	/*
150925ca369SKent Gibson 	 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
151925ca369SKent Gibson 	 * the hardware actually supports enabling both at the same time the
152925ca369SKent Gibson 	 * electrical result would be disastrous.
153925ca369SKent Gibson 	 */
154925ca369SKent Gibson 	if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
155925ca369SKent Gibson 	    (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
156925ca369SKent Gibson 		return -EINVAL;
157925ca369SKent Gibson 
158925ca369SKent Gibson 	/* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
159925ca369SKent Gibson 	if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
160925ca369SKent Gibson 	    ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
161925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
162925ca369SKent Gibson 		return -EINVAL;
163925ca369SKent Gibson 
164925ca369SKent Gibson 	/* Bias flags only allowed for input or output mode. */
165925ca369SKent Gibson 	if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
166925ca369SKent Gibson 	      (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
167925ca369SKent Gibson 	    ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
168925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
169925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
170925ca369SKent Gibson 		return -EINVAL;
171925ca369SKent Gibson 
172925ca369SKent Gibson 	/* Only one bias flag can be set. */
173925ca369SKent Gibson 	if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
174925ca369SKent Gibson 	     (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
175925ca369SKent Gibson 		       GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
176925ca369SKent Gibson 	    ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
177925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
178925ca369SKent Gibson 		return -EINVAL;
179925ca369SKent Gibson 
180925ca369SKent Gibson 	return 0;
181925ca369SKent Gibson }
182925ca369SKent Gibson 
183c274b58aSKent Gibson static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
184c274b58aSKent Gibson {
185c274b58aSKent Gibson 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
186c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
187c274b58aSKent Gibson 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
188c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
189c274b58aSKent Gibson 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
190c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
191c274b58aSKent Gibson 	assign_bit(FLAG_PULL_UP, flagsp,
192c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
193c274b58aSKent Gibson 	assign_bit(FLAG_PULL_DOWN, flagsp,
194c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
195c274b58aSKent Gibson 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
196c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
197c274b58aSKent Gibson }
198c274b58aSKent Gibson 
199925ca369SKent Gibson static long linehandle_set_config(struct linehandle_state *lh,
200925ca369SKent Gibson 				  void __user *ip)
201925ca369SKent Gibson {
202925ca369SKent Gibson 	struct gpiohandle_config gcnf;
203925ca369SKent Gibson 	struct gpio_desc *desc;
204925ca369SKent Gibson 	int i, ret;
205925ca369SKent Gibson 	u32 lflags;
206925ca369SKent Gibson 
207925ca369SKent Gibson 	if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
208925ca369SKent Gibson 		return -EFAULT;
209925ca369SKent Gibson 
210925ca369SKent Gibson 	lflags = gcnf.flags;
211925ca369SKent Gibson 	ret = linehandle_validate_flags(lflags);
212925ca369SKent Gibson 	if (ret)
213925ca369SKent Gibson 		return ret;
214925ca369SKent Gibson 
21552b7b596SKent Gibson 	for (i = 0; i < lh->num_descs; i++) {
216925ca369SKent Gibson 		desc = lh->descs[i];
217c274b58aSKent Gibson 		linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags);
218925ca369SKent Gibson 
219925ca369SKent Gibson 		/*
220925ca369SKent Gibson 		 * Lines have to be requested explicitly for input
221925ca369SKent Gibson 		 * or output, else the line will be treated "as is".
222925ca369SKent Gibson 		 */
223925ca369SKent Gibson 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
224925ca369SKent Gibson 			int val = !!gcnf.default_values[i];
225925ca369SKent Gibson 
226925ca369SKent Gibson 			ret = gpiod_direction_output(desc, val);
227925ca369SKent Gibson 			if (ret)
228925ca369SKent Gibson 				return ret;
229925ca369SKent Gibson 		} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
230925ca369SKent Gibson 			ret = gpiod_direction_input(desc);
231925ca369SKent Gibson 			if (ret)
232925ca369SKent Gibson 				return ret;
233925ca369SKent Gibson 		}
234925ca369SKent Gibson 
2359ce4ed5bSBartosz Golaszewski 		gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
236925ca369SKent Gibson 	}
237925ca369SKent Gibson 	return 0;
238925ca369SKent Gibson }
239925ca369SKent Gibson 
240bdbbae24SBartosz Golaszewski static long linehandle_ioctl_unlocked(struct file *file, unsigned int cmd,
241925ca369SKent Gibson 				      unsigned long arg)
242925ca369SKent Gibson {
24349bc5279SKent Gibson 	struct linehandle_state *lh = file->private_data;
244925ca369SKent Gibson 	void __user *ip = (void __user *)arg;
245925ca369SKent Gibson 	struct gpiohandle_data ghd;
246925ca369SKent Gibson 	DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
2471cef8b50SAndy Shevchenko 	unsigned int i;
2481cef8b50SAndy Shevchenko 	int ret;
249925ca369SKent Gibson 
250533aae7cSBartosz Golaszewski 	if (!lh->gdev->chip)
251533aae7cSBartosz Golaszewski 		return -ENODEV;
252533aae7cSBartosz Golaszewski 
2531cef8b50SAndy Shevchenko 	switch (cmd) {
2541cef8b50SAndy Shevchenko 	case GPIOHANDLE_GET_LINE_VALUES_IOCTL:
2551cef8b50SAndy Shevchenko 		/* NOTE: It's okay to read values of output lines */
2561cef8b50SAndy Shevchenko 		ret = gpiod_get_array_value_complex(false, true,
2571cef8b50SAndy Shevchenko 						    lh->num_descs, lh->descs,
2581cef8b50SAndy Shevchenko 						    NULL, vals);
259925ca369SKent Gibson 		if (ret)
260925ca369SKent Gibson 			return ret;
261925ca369SKent Gibson 
262925ca369SKent Gibson 		memset(&ghd, 0, sizeof(ghd));
26352b7b596SKent Gibson 		for (i = 0; i < lh->num_descs; i++)
264925ca369SKent Gibson 			ghd.values[i] = test_bit(i, vals);
265925ca369SKent Gibson 
266925ca369SKent Gibson 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
267925ca369SKent Gibson 			return -EFAULT;
268925ca369SKent Gibson 
269925ca369SKent Gibson 		return 0;
2701cef8b50SAndy Shevchenko 	case GPIOHANDLE_SET_LINE_VALUES_IOCTL:
271925ca369SKent Gibson 		/*
272925ca369SKent Gibson 		 * All line descriptors were created at once with the same
273925ca369SKent Gibson 		 * flags so just check if the first one is really output.
274925ca369SKent Gibson 		 */
275925ca369SKent Gibson 		if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
276925ca369SKent Gibson 			return -EPERM;
277925ca369SKent Gibson 
278925ca369SKent Gibson 		if (copy_from_user(&ghd, ip, sizeof(ghd)))
279925ca369SKent Gibson 			return -EFAULT;
280925ca369SKent Gibson 
281925ca369SKent Gibson 		/* Clamp all values to [0,1] */
28252b7b596SKent Gibson 		for (i = 0; i < lh->num_descs; i++)
283925ca369SKent Gibson 			__assign_bit(i, vals, ghd.values[i]);
284925ca369SKent Gibson 
285925ca369SKent Gibson 		/* Reuse the array setting function */
286925ca369SKent Gibson 		return gpiod_set_array_value_complex(false,
287925ca369SKent Gibson 						     true,
28852b7b596SKent Gibson 						     lh->num_descs,
289925ca369SKent Gibson 						     lh->descs,
290925ca369SKent Gibson 						     NULL,
291925ca369SKent Gibson 						     vals);
2921cef8b50SAndy Shevchenko 	case GPIOHANDLE_SET_CONFIG_IOCTL:
293925ca369SKent Gibson 		return linehandle_set_config(lh, ip);
2941cef8b50SAndy Shevchenko 	default:
295925ca369SKent Gibson 		return -EINVAL;
296925ca369SKent Gibson 	}
2971cef8b50SAndy Shevchenko }
298925ca369SKent Gibson 
299bdbbae24SBartosz Golaszewski static long linehandle_ioctl(struct file *file, unsigned int cmd,
300bdbbae24SBartosz Golaszewski 			     unsigned long arg)
301bdbbae24SBartosz Golaszewski {
302bdbbae24SBartosz Golaszewski 	struct linehandle_state *lh = file->private_data;
303bdbbae24SBartosz Golaszewski 
304bdbbae24SBartosz Golaszewski 	return call_ioctl_locked(file, cmd, arg, lh->gdev,
305bdbbae24SBartosz Golaszewski 				 linehandle_ioctl_unlocked);
306bdbbae24SBartosz Golaszewski }
307bdbbae24SBartosz Golaszewski 
308925ca369SKent Gibson #ifdef CONFIG_COMPAT
30949bc5279SKent Gibson static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
310925ca369SKent Gibson 				    unsigned long arg)
311925ca369SKent Gibson {
31249bc5279SKent Gibson 	return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
313925ca369SKent Gibson }
314925ca369SKent Gibson #endif
315925ca369SKent Gibson 
316883f9198SKent Gibson static void linehandle_free(struct linehandle_state *lh)
317925ca369SKent Gibson {
318925ca369SKent Gibson 	int i;
319925ca369SKent Gibson 
32052b7b596SKent Gibson 	for (i = 0; i < lh->num_descs; i++)
321883f9198SKent Gibson 		if (lh->descs[i])
322925ca369SKent Gibson 			gpiod_free(lh->descs[i]);
323925ca369SKent Gibson 	kfree(lh->label);
324dc0989e3SAndy Shevchenko 	gpio_device_put(lh->gdev);
325925ca369SKent Gibson 	kfree(lh);
326883f9198SKent Gibson }
327883f9198SKent Gibson 
328883f9198SKent Gibson static int linehandle_release(struct inode *inode, struct file *file)
329883f9198SKent Gibson {
330883f9198SKent Gibson 	linehandle_free(file->private_data);
331925ca369SKent Gibson 	return 0;
332925ca369SKent Gibson }
333925ca369SKent Gibson 
334925ca369SKent Gibson static const struct file_operations linehandle_fileops = {
335925ca369SKent Gibson 	.release = linehandle_release,
336925ca369SKent Gibson 	.owner = THIS_MODULE,
337925ca369SKent Gibson 	.llseek = noop_llseek,
338925ca369SKent Gibson 	.unlocked_ioctl = linehandle_ioctl,
339925ca369SKent Gibson #ifdef CONFIG_COMPAT
340925ca369SKent Gibson 	.compat_ioctl = linehandle_ioctl_compat,
341925ca369SKent Gibson #endif
342925ca369SKent Gibson };
343925ca369SKent Gibson 
344925ca369SKent Gibson static int linehandle_create(struct gpio_device *gdev, void __user *ip)
345925ca369SKent Gibson {
346925ca369SKent Gibson 	struct gpiohandle_request handlereq;
347925ca369SKent Gibson 	struct linehandle_state *lh;
348925ca369SKent Gibson 	struct file *file;
349883f9198SKent Gibson 	int fd, i, ret;
350925ca369SKent Gibson 	u32 lflags;
351925ca369SKent Gibson 
352925ca369SKent Gibson 	if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
353925ca369SKent Gibson 		return -EFAULT;
354925ca369SKent Gibson 	if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
355925ca369SKent Gibson 		return -EINVAL;
356925ca369SKent Gibson 
357925ca369SKent Gibson 	lflags = handlereq.flags;
358925ca369SKent Gibson 
359925ca369SKent Gibson 	ret = linehandle_validate_flags(lflags);
360925ca369SKent Gibson 	if (ret)
361925ca369SKent Gibson 		return ret;
362925ca369SKent Gibson 
363925ca369SKent Gibson 	lh = kzalloc(sizeof(*lh), GFP_KERNEL);
364925ca369SKent Gibson 	if (!lh)
365925ca369SKent Gibson 		return -ENOMEM;
366dc0989e3SAndy Shevchenko 	lh->gdev = gpio_device_get(gdev);
367925ca369SKent Gibson 
368f188ac12SKent Gibson 	if (handlereq.consumer_label[0] != '\0') {
369f188ac12SKent Gibson 		/* label is only initialized if consumer_label is set */
370f188ac12SKent Gibson 		lh->label = kstrndup(handlereq.consumer_label,
371f188ac12SKent Gibson 				     sizeof(handlereq.consumer_label) - 1,
372925ca369SKent Gibson 				     GFP_KERNEL);
373925ca369SKent Gibson 		if (!lh->label) {
374925ca369SKent Gibson 			ret = -ENOMEM;
375925ca369SKent Gibson 			goto out_free_lh;
376925ca369SKent Gibson 		}
377925ca369SKent Gibson 	}
378925ca369SKent Gibson 
379883f9198SKent Gibson 	lh->num_descs = handlereq.lines;
380883f9198SKent Gibson 
381925ca369SKent Gibson 	/* Request each GPIO */
382925ca369SKent Gibson 	for (i = 0; i < handlereq.lines; i++) {
383925ca369SKent Gibson 		u32 offset = handlereq.lineoffsets[i];
384925ca369SKent Gibson 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
385925ca369SKent Gibson 
386925ca369SKent Gibson 		if (IS_ERR(desc)) {
387925ca369SKent Gibson 			ret = PTR_ERR(desc);
388883f9198SKent Gibson 			goto out_free_lh;
389925ca369SKent Gibson 		}
390925ca369SKent Gibson 
39195a4eed7SAndy Shevchenko 		ret = gpiod_request_user(desc, lh->label);
392925ca369SKent Gibson 		if (ret)
393883f9198SKent Gibson 			goto out_free_lh;
394925ca369SKent Gibson 		lh->descs[i] = desc;
395c274b58aSKent Gibson 		linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
396925ca369SKent Gibson 
397925ca369SKent Gibson 		ret = gpiod_set_transitory(desc, false);
398925ca369SKent Gibson 		if (ret < 0)
399883f9198SKent Gibson 			goto out_free_lh;
400925ca369SKent Gibson 
401925ca369SKent Gibson 		/*
402925ca369SKent Gibson 		 * Lines have to be requested explicitly for input
403925ca369SKent Gibson 		 * or output, else the line will be treated "as is".
404925ca369SKent Gibson 		 */
405925ca369SKent Gibson 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
406925ca369SKent Gibson 			int val = !!handlereq.default_values[i];
407925ca369SKent Gibson 
408925ca369SKent Gibson 			ret = gpiod_direction_output(desc, val);
409925ca369SKent Gibson 			if (ret)
410883f9198SKent Gibson 				goto out_free_lh;
411925ca369SKent Gibson 		} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
412925ca369SKent Gibson 			ret = gpiod_direction_input(desc);
413925ca369SKent Gibson 			if (ret)
414883f9198SKent Gibson 				goto out_free_lh;
415925ca369SKent Gibson 		}
416925ca369SKent Gibson 
4179ce4ed5bSBartosz Golaszewski 		gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
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
466309364bcSKent Gibson  * @node: to store the object in supinfo_tree if supplemental
4673c0d9c63SKent Gibson  * @desc: the GPIO descriptor for this line.
46873e03419SKent Gibson  * @req: the corresponding line request
46973e03419SKent Gibson  * @irq: the interrupt triggered in response to events on this GPIO
4708d259847SAndy Shevchenko  * @edflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or
47173e03419SKent Gibson  * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied
47273e03419SKent Gibson  * @timestamp_ns: cache for the timestamp storing it between hardirq and
47373e03419SKent Gibson  * IRQ thread, used to bring the timestamp close to the actual event
47473e03419SKent Gibson  * @req_seqno: the seqno for the current edge event in the sequence of
47573e03419SKent Gibson  * events for the corresponding line request. This is drawn from the @req.
47673e03419SKent Gibson  * @line_seqno: the seqno for the current edge event in the sequence of
47773e03419SKent Gibson  * events for this line.
47865cff704SKent Gibson  * @work: the worker that implements software debouncing
479309364bcSKent Gibson  * @debounce_period_us: the debounce period in microseconds
48065cff704SKent Gibson  * @sw_debounced: flag indicating if the software debouncer is active
48165cff704SKent Gibson  * @level: the current debounced physical level of the line
48285ff37e3SAndy Shevchenko  * @hdesc: the Hardware Timestamp Engine (HTE) descriptor
48385ff37e3SAndy Shevchenko  * @raw_level: the line level at the time of event
48485ff37e3SAndy Shevchenko  * @total_discard_seq: the running counter of the discarded events
48585ff37e3SAndy Shevchenko  * @last_seqno: the last sequence number before debounce period expires
4863c0d9c63SKent Gibson  */
4873c0d9c63SKent Gibson struct line {
488309364bcSKent Gibson 	struct rb_node node;
4893c0d9c63SKent Gibson 	struct gpio_desc *desc;
49073e03419SKent Gibson 	/*
49173e03419SKent Gibson 	 * -- edge detector specific fields --
49273e03419SKent Gibson 	 */
49373e03419SKent Gibson 	struct linereq *req;
49473e03419SKent Gibson 	unsigned int irq;
4953ffb7c45SKent Gibson 	/*
496b1a92e94SKent Gibson 	 * The flags for the active edge detector configuration.
497b1a92e94SKent Gibson 	 *
498b1a92e94SKent Gibson 	 * edflags is set by linereq_create(), linereq_free(), and
499b1a92e94SKent Gibson 	 * linereq_set_config_unlocked(), which are themselves mutually
500b1a92e94SKent Gibson 	 * exclusive, and is accessed by edge_irq_thread(),
501b1a92e94SKent Gibson 	 * process_hw_ts_thread() and debounce_work_func(),
502b1a92e94SKent Gibson 	 * which can all live with a slightly stale value.
5033ffb7c45SKent Gibson 	 */
504b1a92e94SKent Gibson 	u64 edflags;
50573e03419SKent Gibson 	/*
50673e03419SKent Gibson 	 * timestamp_ns and req_seqno are accessed only by
50773e03419SKent Gibson 	 * edge_irq_handler() and edge_irq_thread(), which are themselves
50873e03419SKent Gibson 	 * mutually exclusive, so no additional protection is necessary.
50973e03419SKent Gibson 	 */
51073e03419SKent Gibson 	u64 timestamp_ns;
51173e03419SKent Gibson 	u32 req_seqno;
51265cff704SKent Gibson 	/*
51365cff704SKent Gibson 	 * line_seqno is accessed by either edge_irq_thread() or
51465cff704SKent Gibson 	 * debounce_work_func(), which are themselves mutually exclusive,
51565cff704SKent Gibson 	 * so no additional protection is necessary.
51665cff704SKent Gibson 	 */
51773e03419SKent Gibson 	u32 line_seqno;
51865cff704SKent Gibson 	/*
51965cff704SKent Gibson 	 * -- debouncer specific fields --
52065cff704SKent Gibson 	 */
52165cff704SKent Gibson 	struct delayed_work work;
52265cff704SKent Gibson 	/*
523309364bcSKent Gibson 	 * debounce_period_us is accessed by debounce_irq_handler() and
524309364bcSKent Gibson 	 * process_hw_ts() which are disabled when modified by
525309364bcSKent Gibson 	 * debounce_setup(), edge_detector_setup() or edge_detector_stop()
526309364bcSKent Gibson 	 * or can live with a stale version when updated by
527309364bcSKent Gibson 	 * edge_detector_update().
528309364bcSKent Gibson 	 * The modifying functions are themselves mutually exclusive.
529309364bcSKent Gibson 	 */
530309364bcSKent Gibson 	unsigned int debounce_period_us;
531309364bcSKent Gibson 	/*
53265cff704SKent Gibson 	 * sw_debounce is accessed by linereq_set_config(), which is the
53365cff704SKent Gibson 	 * only setter, and linereq_get_values(), which can live with a
53465cff704SKent Gibson 	 * slightly stale value.
53565cff704SKent Gibson 	 */
53665cff704SKent Gibson 	unsigned int sw_debounced;
53765cff704SKent Gibson 	/*
53865cff704SKent Gibson 	 * level is accessed by debounce_work_func(), which is the only
53965cff704SKent Gibson 	 * setter, and linereq_get_values() which can live with a slightly
54065cff704SKent Gibson 	 * stale value.
54165cff704SKent Gibson 	 */
54265cff704SKent Gibson 	unsigned int level;
543272ddba0SKent Gibson #ifdef CONFIG_HTE
5442068339aSDipen Patel 	struct hte_ts_desc hdesc;
5452068339aSDipen Patel 	/*
5462068339aSDipen Patel 	 * HTE provider sets line level at the time of event. The valid
5472068339aSDipen Patel 	 * value is 0 or 1 and negative value for an error.
5482068339aSDipen Patel 	 */
5492068339aSDipen Patel 	int raw_level;
5502068339aSDipen Patel 	/*
5512068339aSDipen Patel 	 * when sw_debounce is set on HTE enabled line, this is running
5522068339aSDipen Patel 	 * counter of the discarded events.
5532068339aSDipen Patel 	 */
5542068339aSDipen Patel 	u32 total_discard_seq;
5552068339aSDipen Patel 	/*
5562068339aSDipen Patel 	 * when sw_debounce is set on HTE enabled line, this variable records
5572068339aSDipen Patel 	 * last sequence number before debounce period expires.
5582068339aSDipen Patel 	 */
5592068339aSDipen Patel 	u32 last_seqno;
560272ddba0SKent Gibson #endif /* CONFIG_HTE */
5613c0d9c63SKent Gibson };
5623c0d9c63SKent Gibson 
563309364bcSKent Gibson /*
564309364bcSKent Gibson  * a rbtree of the struct lines containing supplemental info.
565309364bcSKent Gibson  * Used to populate gpio_v2_line_info with cdev specific fields not contained
566309364bcSKent Gibson  * in the struct gpio_desc.
567309364bcSKent Gibson  * A line is determined to contain supplemental information by
568309364bcSKent Gibson  * line_has_supinfo().
569309364bcSKent Gibson  */
570309364bcSKent Gibson static struct rb_root supinfo_tree = RB_ROOT;
571309364bcSKent Gibson /* covers supinfo_tree */
572309364bcSKent Gibson static DEFINE_SPINLOCK(supinfo_lock);
573309364bcSKent Gibson 
5743c0d9c63SKent Gibson /**
5753c0d9c63SKent Gibson  * struct linereq - contains the state of a userspace line request
5763c0d9c63SKent Gibson  * @gdev: the GPIO device the line request pertains to
5773c0d9c63SKent Gibson  * @label: consumer label used to tag GPIO descriptors
5783c0d9c63SKent Gibson  * @num_lines: the number of lines in the lines array
57973e03419SKent Gibson  * @wait: wait queue that handles blocking reads of events
580a0dda508SBartosz Golaszewski  * @device_unregistered_nb: notifier block for receiving gdev unregister events
58173e03419SKent Gibson  * @event_buffer_size: the number of elements allocated in @events
58273e03419SKent Gibson  * @events: KFIFO for the GPIO events
58373e03419SKent Gibson  * @seqno: the sequence number for edge events generated on all lines in
58473e03419SKent Gibson  * this line request.  Note that this is not used when @num_lines is 1, as
58573e03419SKent Gibson  * the line_seqno is then the same and is cheaper to calculate.
586a54756cbSKent Gibson  * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
587309364bcSKent Gibson  * of configuration, particularly multi-step accesses to desc flags and
588309364bcSKent Gibson  * changes to supinfo status.
5893c0d9c63SKent Gibson  * @lines: the lines held by this line request, with @num_lines elements.
5903c0d9c63SKent Gibson  */
5913c0d9c63SKent Gibson struct linereq {
5923c0d9c63SKent Gibson 	struct gpio_device *gdev;
5933c0d9c63SKent Gibson 	const char *label;
5943c0d9c63SKent Gibson 	u32 num_lines;
59573e03419SKent Gibson 	wait_queue_head_t wait;
596a0dda508SBartosz Golaszewski 	struct notifier_block device_unregistered_nb;
59773e03419SKent Gibson 	u32 event_buffer_size;
59873e03419SKent Gibson 	DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
59973e03419SKent Gibson 	atomic_t seqno;
600a54756cbSKent Gibson 	struct mutex config_mutex;
6013c0d9c63SKent Gibson 	struct line lines[];
6023c0d9c63SKent Gibson };
6033c0d9c63SKent Gibson 
604309364bcSKent Gibson static void supinfo_insert(struct line *line)
605309364bcSKent Gibson {
606309364bcSKent Gibson 	struct rb_node **new = &(supinfo_tree.rb_node), *parent = NULL;
607309364bcSKent Gibson 	struct line *entry;
608309364bcSKent Gibson 
609309364bcSKent Gibson 	guard(spinlock)(&supinfo_lock);
610309364bcSKent Gibson 
611309364bcSKent Gibson 	while (*new) {
612309364bcSKent Gibson 		entry = container_of(*new, struct line, node);
613309364bcSKent Gibson 
614309364bcSKent Gibson 		parent = *new;
615309364bcSKent Gibson 		if (line->desc < entry->desc) {
616309364bcSKent Gibson 			new = &((*new)->rb_left);
617309364bcSKent Gibson 		} else if (line->desc > entry->desc) {
618309364bcSKent Gibson 			new = &((*new)->rb_right);
619309364bcSKent Gibson 		} else {
620309364bcSKent Gibson 			/* this should never happen */
621309364bcSKent Gibson 			WARN(1, "duplicate line inserted");
622309364bcSKent Gibson 			return;
623309364bcSKent Gibson 		}
624309364bcSKent Gibson 	}
625309364bcSKent Gibson 
626309364bcSKent Gibson 	rb_link_node(&line->node, parent, new);
627309364bcSKent Gibson 	rb_insert_color(&line->node, &supinfo_tree);
628309364bcSKent Gibson }
629309364bcSKent Gibson 
630309364bcSKent Gibson static void supinfo_erase(struct line *line)
631309364bcSKent Gibson {
632309364bcSKent Gibson 	guard(spinlock)(&supinfo_lock);
633309364bcSKent Gibson 
634309364bcSKent Gibson 	rb_erase(&line->node, &supinfo_tree);
635309364bcSKent Gibson }
636309364bcSKent Gibson 
637309364bcSKent Gibson static struct line *supinfo_find(struct gpio_desc *desc)
638309364bcSKent Gibson {
639309364bcSKent Gibson 	struct rb_node *node = supinfo_tree.rb_node;
640309364bcSKent Gibson 	struct line *line;
641309364bcSKent Gibson 
642309364bcSKent Gibson 	while (node) {
643309364bcSKent Gibson 		line = container_of(node, struct line, node);
644309364bcSKent Gibson 		if (desc < line->desc)
645309364bcSKent Gibson 			node = node->rb_left;
646309364bcSKent Gibson 		else if (desc > line->desc)
647309364bcSKent Gibson 			node = node->rb_right;
648309364bcSKent Gibson 		else
649309364bcSKent Gibson 			return line;
650309364bcSKent Gibson 	}
651309364bcSKent Gibson 	return NULL;
652309364bcSKent Gibson }
653309364bcSKent Gibson 
654309364bcSKent Gibson static void supinfo_to_lineinfo(struct gpio_desc *desc,
655309364bcSKent Gibson 				struct gpio_v2_line_info *info)
656309364bcSKent Gibson {
657309364bcSKent Gibson 	struct gpio_v2_line_attribute *attr;
658309364bcSKent Gibson 	struct line *line;
659309364bcSKent Gibson 
660309364bcSKent Gibson 	guard(spinlock)(&supinfo_lock);
661309364bcSKent Gibson 
662309364bcSKent Gibson 	line = supinfo_find(desc);
663309364bcSKent Gibson 	if (!line)
664309364bcSKent Gibson 		return;
665309364bcSKent Gibson 
666309364bcSKent Gibson 	attr = &info->attrs[info->num_attrs];
667309364bcSKent Gibson 	attr->id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
668309364bcSKent Gibson 	attr->debounce_period_us = READ_ONCE(line->debounce_period_us);
669309364bcSKent Gibson 	info->num_attrs++;
670309364bcSKent Gibson }
671309364bcSKent Gibson 
672309364bcSKent Gibson static inline bool line_has_supinfo(struct line *line)
673309364bcSKent Gibson {
674309364bcSKent Gibson 	return READ_ONCE(line->debounce_period_us);
675309364bcSKent Gibson }
676309364bcSKent Gibson 
677309364bcSKent Gibson /*
678309364bcSKent Gibson  * Checks line_has_supinfo() before and after the change to avoid unnecessary
679309364bcSKent Gibson  * supinfo_tree access.
680309364bcSKent Gibson  * Called indirectly by linereq_create() or linereq_set_config() so line
681309364bcSKent Gibson  * is already protected from concurrent changes.
682309364bcSKent Gibson  */
683309364bcSKent Gibson static void line_set_debounce_period(struct line *line,
684309364bcSKent Gibson 				     unsigned int debounce_period_us)
685309364bcSKent Gibson {
686309364bcSKent Gibson 	bool was_suppl = line_has_supinfo(line);
687309364bcSKent Gibson 
688309364bcSKent Gibson 	WRITE_ONCE(line->debounce_period_us, debounce_period_us);
689309364bcSKent Gibson 
690309364bcSKent Gibson 	/* if supinfo status is unchanged then we're done */
691309364bcSKent Gibson 	if (line_has_supinfo(line) == was_suppl)
692309364bcSKent Gibson 		return;
693309364bcSKent Gibson 
694309364bcSKent Gibson 	/* supinfo status has changed, so update the tree */
695309364bcSKent Gibson 	if (was_suppl)
696309364bcSKent Gibson 		supinfo_erase(line);
697309364bcSKent Gibson 	else
698309364bcSKent Gibson 		supinfo_insert(line);
699309364bcSKent Gibson }
700309364bcSKent Gibson 
7013c0d9c63SKent Gibson #define GPIO_V2_LINE_BIAS_FLAGS \
7023c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
7033c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
7043c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_BIAS_DISABLED)
7053c0d9c63SKent Gibson 
7063c0d9c63SKent Gibson #define GPIO_V2_LINE_DIRECTION_FLAGS \
7073c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_INPUT | \
7083c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_OUTPUT)
7093c0d9c63SKent Gibson 
7103c0d9c63SKent Gibson #define GPIO_V2_LINE_DRIVE_FLAGS \
7113c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
7123c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_OPEN_SOURCE)
7133c0d9c63SKent Gibson 
71473e03419SKent Gibson #define GPIO_V2_LINE_EDGE_FLAGS \
71573e03419SKent Gibson 	(GPIO_V2_LINE_FLAG_EDGE_RISING | \
71673e03419SKent Gibson 	 GPIO_V2_LINE_FLAG_EDGE_FALLING)
71773e03419SKent Gibson 
7185e2ca893SKent Gibson #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS
7195e2ca893SKent Gibson 
7203c0d9c63SKent Gibson #define GPIO_V2_LINE_VALID_FLAGS \
7213c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
7223c0d9c63SKent Gibson 	 GPIO_V2_LINE_DIRECTION_FLAGS | \
7233c0d9c63SKent Gibson 	 GPIO_V2_LINE_DRIVE_FLAGS | \
72473e03419SKent Gibson 	 GPIO_V2_LINE_EDGE_FLAGS | \
72526d060e4SKent Gibson 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
7262068339aSDipen Patel 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
7273c0d9c63SKent Gibson 	 GPIO_V2_LINE_BIAS_FLAGS)
7283c0d9c63SKent Gibson 
729b1a92e94SKent Gibson /* subset of flags relevant for edge detector configuration */
730b1a92e94SKent Gibson #define GPIO_V2_LINE_EDGE_DETECTOR_FLAGS \
731b1a92e94SKent Gibson 	(GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
732b1a92e94SKent Gibson 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
733b1a92e94SKent Gibson 	 GPIO_V2_LINE_EDGE_FLAGS)
734b1a92e94SKent Gibson 
735a0dda508SBartosz Golaszewski static int linereq_unregistered_notify(struct notifier_block *nb,
736a0dda508SBartosz Golaszewski 				       unsigned long action, void *data)
737a0dda508SBartosz Golaszewski {
738a0dda508SBartosz Golaszewski 	struct linereq *lr = container_of(nb, struct linereq,
739a0dda508SBartosz Golaszewski 					  device_unregistered_nb);
740a0dda508SBartosz Golaszewski 
741a0dda508SBartosz Golaszewski 	wake_up_poll(&lr->wait, EPOLLIN | EPOLLERR);
742a0dda508SBartosz Golaszewski 
743a0dda508SBartosz Golaszewski 	return NOTIFY_OK;
744a0dda508SBartosz Golaszewski }
745a0dda508SBartosz Golaszewski 
74673e03419SKent Gibson static void linereq_put_event(struct linereq *lr,
74773e03419SKent Gibson 			      struct gpio_v2_line_event *le)
74873e03419SKent Gibson {
74973e03419SKent Gibson 	bool overflow = false;
75073e03419SKent Gibson 
75173e03419SKent Gibson 	spin_lock(&lr->wait.lock);
75273e03419SKent Gibson 	if (kfifo_is_full(&lr->events)) {
75373e03419SKent Gibson 		overflow = true;
75473e03419SKent Gibson 		kfifo_skip(&lr->events);
75573e03419SKent Gibson 	}
75673e03419SKent Gibson 	kfifo_in(&lr->events, le, 1);
75773e03419SKent Gibson 	spin_unlock(&lr->wait.lock);
75873e03419SKent Gibson 	if (!overflow)
75973e03419SKent Gibson 		wake_up_poll(&lr->wait, EPOLLIN);
76073e03419SKent Gibson 	else
76173e03419SKent Gibson 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
76273e03419SKent Gibson }
76373e03419SKent Gibson 
76426d060e4SKent Gibson static u64 line_event_timestamp(struct line *line)
76526d060e4SKent Gibson {
76626d060e4SKent Gibson 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
76726d060e4SKent Gibson 		return ktime_get_real_ns();
768272ddba0SKent Gibson 	else if (IS_ENABLED(CONFIG_HTE) &&
769272ddba0SKent Gibson 		 test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))
7702068339aSDipen Patel 		return line->timestamp_ns;
77126d060e4SKent Gibson 
77226d060e4SKent Gibson 	return ktime_get_ns();
77326d060e4SKent Gibson }
77426d060e4SKent Gibson 
77524220232SKent Gibson static u32 line_event_id(int level)
77624220232SKent Gibson {
77724220232SKent Gibson 	return level ? GPIO_V2_LINE_EVENT_RISING_EDGE :
77824220232SKent Gibson 		       GPIO_V2_LINE_EVENT_FALLING_EDGE;
77924220232SKent Gibson }
78024220232SKent Gibson 
7812f0262acSKent Gibson static inline char *make_irq_label(const char *orig)
7822f0262acSKent Gibson {
7832f0262acSKent Gibson 	char *new;
7842f0262acSKent Gibson 
7852f0262acSKent Gibson 	if (!orig)
7862f0262acSKent Gibson 		return NULL;
7872f0262acSKent Gibson 
7882f0262acSKent Gibson 	new = kstrdup_and_replace(orig, '/', ':', GFP_KERNEL);
7892f0262acSKent Gibson 	if (!new)
7902f0262acSKent Gibson 		return ERR_PTR(-ENOMEM);
7912f0262acSKent Gibson 
7922f0262acSKent Gibson 	return new;
7932f0262acSKent Gibson }
7942f0262acSKent Gibson 
7952f0262acSKent Gibson static inline void free_irq_label(const char *label)
7962f0262acSKent Gibson {
7972f0262acSKent Gibson 	kfree(label);
7982f0262acSKent Gibson }
7992f0262acSKent Gibson 
800272ddba0SKent Gibson #ifdef CONFIG_HTE
801272ddba0SKent Gibson 
8022068339aSDipen Patel static enum hte_return process_hw_ts_thread(void *p)
8032068339aSDipen Patel {
8042068339aSDipen Patel 	struct line *line;
8052068339aSDipen Patel 	struct linereq *lr;
8062068339aSDipen Patel 	struct gpio_v2_line_event le;
807b1a92e94SKent Gibson 	u64 edflags;
8082068339aSDipen Patel 	int level;
8092068339aSDipen Patel 
8102068339aSDipen Patel 	if (!p)
8112068339aSDipen Patel 		return HTE_CB_HANDLED;
8122068339aSDipen Patel 
8132068339aSDipen Patel 	line = p;
8142068339aSDipen Patel 	lr = line->req;
8152068339aSDipen Patel 
8162068339aSDipen Patel 	memset(&le, 0, sizeof(le));
8172068339aSDipen Patel 
8182068339aSDipen Patel 	le.timestamp_ns = line->timestamp_ns;
819b1a92e94SKent Gibson 	edflags = READ_ONCE(line->edflags);
8202068339aSDipen Patel 
821b1a92e94SKent Gibson 	switch (edflags & GPIO_V2_LINE_EDGE_FLAGS) {
822cfa53463SKent Gibson 	case GPIO_V2_LINE_FLAG_EDGE_BOTH:
82324220232SKent Gibson 		level = (line->raw_level >= 0) ?
82424220232SKent Gibson 				line->raw_level :
82524220232SKent Gibson 				gpiod_get_raw_value_cansleep(line->desc);
8262068339aSDipen Patel 
827b1a92e94SKent Gibson 		if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
82824220232SKent Gibson 			level = !level;
82924220232SKent Gibson 
83024220232SKent Gibson 		le.id = line_event_id(level);
831cfa53463SKent Gibson 		break;
832cfa53463SKent Gibson 	case GPIO_V2_LINE_FLAG_EDGE_RISING:
8332068339aSDipen Patel 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
834cfa53463SKent Gibson 		break;
835cfa53463SKent Gibson 	case GPIO_V2_LINE_FLAG_EDGE_FALLING:
8362068339aSDipen Patel 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
837cfa53463SKent Gibson 		break;
838cfa53463SKent Gibson 	default:
8392068339aSDipen Patel 		return HTE_CB_HANDLED;
8402068339aSDipen Patel 	}
8412068339aSDipen Patel 	le.line_seqno = line->line_seqno;
8422068339aSDipen Patel 	le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
8432068339aSDipen Patel 	le.offset = gpio_chip_hwgpio(line->desc);
8442068339aSDipen Patel 
8452068339aSDipen Patel 	linereq_put_event(lr, &le);
8462068339aSDipen Patel 
8472068339aSDipen Patel 	return HTE_CB_HANDLED;
8482068339aSDipen Patel }
8492068339aSDipen Patel 
8502068339aSDipen Patel static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p)
8512068339aSDipen Patel {
8522068339aSDipen Patel 	struct line *line;
8532068339aSDipen Patel 	struct linereq *lr;
8542068339aSDipen Patel 	int diff_seqno = 0;
8552068339aSDipen Patel 
8562068339aSDipen Patel 	if (!ts || !p)
8572068339aSDipen Patel 		return HTE_CB_HANDLED;
8582068339aSDipen Patel 
8592068339aSDipen Patel 	line = p;
8602068339aSDipen Patel 	line->timestamp_ns = ts->tsc;
8612068339aSDipen Patel 	line->raw_level = ts->raw_level;
8622068339aSDipen Patel 	lr = line->req;
8632068339aSDipen Patel 
8642068339aSDipen Patel 	if (READ_ONCE(line->sw_debounced)) {
8652068339aSDipen Patel 		line->total_discard_seq++;
8662068339aSDipen Patel 		line->last_seqno = ts->seq;
8672068339aSDipen Patel 		mod_delayed_work(system_wq, &line->work,
868309364bcSKent Gibson 		  usecs_to_jiffies(READ_ONCE(line->debounce_period_us)));
8692068339aSDipen Patel 	} else {
8702068339aSDipen Patel 		if (unlikely(ts->seq < line->line_seqno))
8712068339aSDipen Patel 			return HTE_CB_HANDLED;
8722068339aSDipen Patel 
8732068339aSDipen Patel 		diff_seqno = ts->seq - line->line_seqno;
8742068339aSDipen Patel 		line->line_seqno = ts->seq;
8752068339aSDipen Patel 		if (lr->num_lines != 1)
8762068339aSDipen Patel 			line->req_seqno = atomic_add_return(diff_seqno,
8772068339aSDipen Patel 							    &lr->seqno);
8782068339aSDipen Patel 
8792068339aSDipen Patel 		return HTE_RUN_SECOND_CB;
8802068339aSDipen Patel 	}
8812068339aSDipen Patel 
8822068339aSDipen Patel 	return HTE_CB_HANDLED;
8832068339aSDipen Patel }
8842068339aSDipen Patel 
885272ddba0SKent Gibson static int hte_edge_setup(struct line *line, u64 eflags)
886272ddba0SKent Gibson {
887272ddba0SKent Gibson 	int ret;
888272ddba0SKent Gibson 	unsigned long flags = 0;
889272ddba0SKent Gibson 	struct hte_ts_desc *hdesc = &line->hdesc;
890272ddba0SKent Gibson 
891272ddba0SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
892272ddba0SKent Gibson 		flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
893272ddba0SKent Gibson 				 HTE_FALLING_EDGE_TS :
894272ddba0SKent Gibson 				 HTE_RISING_EDGE_TS;
895272ddba0SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
896272ddba0SKent Gibson 		flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
897272ddba0SKent Gibson 				 HTE_RISING_EDGE_TS :
898272ddba0SKent Gibson 				 HTE_FALLING_EDGE_TS;
899272ddba0SKent Gibson 
900272ddba0SKent Gibson 	line->total_discard_seq = 0;
901272ddba0SKent Gibson 
902272ddba0SKent Gibson 	hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags, NULL,
903272ddba0SKent Gibson 			   line->desc);
904272ddba0SKent Gibson 
905272ddba0SKent Gibson 	ret = hte_ts_get(NULL, hdesc, 0);
906272ddba0SKent Gibson 	if (ret)
907272ddba0SKent Gibson 		return ret;
908272ddba0SKent Gibson 
909272ddba0SKent Gibson 	return hte_request_ts_ns(hdesc, process_hw_ts, process_hw_ts_thread,
910272ddba0SKent Gibson 				 line);
911272ddba0SKent Gibson }
912272ddba0SKent Gibson 
913272ddba0SKent Gibson #else
914272ddba0SKent Gibson 
915272ddba0SKent Gibson static int hte_edge_setup(struct line *line, u64 eflags)
916272ddba0SKent Gibson {
917272ddba0SKent Gibson 	return 0;
918272ddba0SKent Gibson }
919272ddba0SKent Gibson #endif /* CONFIG_HTE */
920272ddba0SKent Gibson 
92173e03419SKent Gibson static irqreturn_t edge_irq_thread(int irq, void *p)
92273e03419SKent Gibson {
92373e03419SKent Gibson 	struct line *line = p;
92473e03419SKent Gibson 	struct linereq *lr = line->req;
92573e03419SKent Gibson 	struct gpio_v2_line_event le;
92673e03419SKent Gibson 
92773e03419SKent Gibson 	/* Do not leak kernel stack to userspace */
92873e03419SKent Gibson 	memset(&le, 0, sizeof(le));
92973e03419SKent Gibson 
93073e03419SKent Gibson 	if (line->timestamp_ns) {
93173e03419SKent Gibson 		le.timestamp_ns = line->timestamp_ns;
93273e03419SKent Gibson 	} else {
93373e03419SKent Gibson 		/*
93473e03419SKent Gibson 		 * We may be running from a nested threaded interrupt in
93573e03419SKent Gibson 		 * which case we didn't get the timestamp from
93673e03419SKent Gibson 		 * edge_irq_handler().
93773e03419SKent Gibson 		 */
93826d060e4SKent Gibson 		le.timestamp_ns = line_event_timestamp(line);
93973e03419SKent Gibson 		if (lr->num_lines != 1)
94073e03419SKent Gibson 			line->req_seqno = atomic_inc_return(&lr->seqno);
94173e03419SKent Gibson 	}
94273e03419SKent Gibson 	line->timestamp_ns = 0;
94373e03419SKent Gibson 
944b1a92e94SKent Gibson 	switch (READ_ONCE(line->edflags) & GPIO_V2_LINE_EDGE_FLAGS) {
945cfa53463SKent Gibson 	case GPIO_V2_LINE_FLAG_EDGE_BOTH:
94624220232SKent Gibson 		le.id = line_event_id(gpiod_get_value_cansleep(line->desc));
947cfa53463SKent Gibson 		break;
948cfa53463SKent Gibson 	case GPIO_V2_LINE_FLAG_EDGE_RISING:
94973e03419SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
950cfa53463SKent Gibson 		break;
951cfa53463SKent Gibson 	case GPIO_V2_LINE_FLAG_EDGE_FALLING:
95273e03419SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
953cfa53463SKent Gibson 		break;
954cfa53463SKent Gibson 	default:
95573e03419SKent Gibson 		return IRQ_NONE;
95673e03419SKent Gibson 	}
95773e03419SKent Gibson 	line->line_seqno++;
95873e03419SKent Gibson 	le.line_seqno = line->line_seqno;
95973e03419SKent Gibson 	le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
96073e03419SKent Gibson 	le.offset = gpio_chip_hwgpio(line->desc);
96173e03419SKent Gibson 
96273e03419SKent Gibson 	linereq_put_event(lr, &le);
96373e03419SKent Gibson 
96473e03419SKent Gibson 	return IRQ_HANDLED;
96573e03419SKent Gibson }
96673e03419SKent Gibson 
96773e03419SKent Gibson static irqreturn_t edge_irq_handler(int irq, void *p)
96873e03419SKent Gibson {
96973e03419SKent Gibson 	struct line *line = p;
97073e03419SKent Gibson 	struct linereq *lr = line->req;
97173e03419SKent Gibson 
97273e03419SKent Gibson 	/*
97373e03419SKent Gibson 	 * Just store the timestamp in hardirq context so we get it as
97473e03419SKent Gibson 	 * close in time as possible to the actual event.
97573e03419SKent Gibson 	 */
97626d060e4SKent Gibson 	line->timestamp_ns = line_event_timestamp(line);
97773e03419SKent Gibson 
97873e03419SKent Gibson 	if (lr->num_lines != 1)
97973e03419SKent Gibson 		line->req_seqno = atomic_inc_return(&lr->seqno);
98073e03419SKent Gibson 
98173e03419SKent Gibson 	return IRQ_WAKE_THREAD;
98273e03419SKent Gibson }
98373e03419SKent Gibson 
98465cff704SKent Gibson /*
98565cff704SKent Gibson  * returns the current debounced logical value.
98665cff704SKent Gibson  */
98765cff704SKent Gibson static bool debounced_value(struct line *line)
98865cff704SKent Gibson {
98965cff704SKent Gibson 	bool value;
99065cff704SKent Gibson 
99165cff704SKent Gibson 	/*
99265cff704SKent Gibson 	 * minor race - debouncer may be stopped here, so edge_detector_stop()
99365cff704SKent Gibson 	 * must leave the value unchanged so the following will read the level
99465cff704SKent Gibson 	 * from when the debouncer was last running.
99565cff704SKent Gibson 	 */
99665cff704SKent Gibson 	value = READ_ONCE(line->level);
99765cff704SKent Gibson 
99865cff704SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
99965cff704SKent Gibson 		value = !value;
100065cff704SKent Gibson 
100165cff704SKent Gibson 	return value;
100265cff704SKent Gibson }
100365cff704SKent Gibson 
100465cff704SKent Gibson static irqreturn_t debounce_irq_handler(int irq, void *p)
100565cff704SKent Gibson {
100665cff704SKent Gibson 	struct line *line = p;
100765cff704SKent Gibson 
100865cff704SKent Gibson 	mod_delayed_work(system_wq, &line->work,
1009309364bcSKent Gibson 		usecs_to_jiffies(READ_ONCE(line->debounce_period_us)));
101065cff704SKent Gibson 
101165cff704SKent Gibson 	return IRQ_HANDLED;
101265cff704SKent Gibson }
101365cff704SKent Gibson 
101465cff704SKent Gibson static void debounce_work_func(struct work_struct *work)
101565cff704SKent Gibson {
101665cff704SKent Gibson 	struct gpio_v2_line_event le;
101765cff704SKent Gibson 	struct line *line = container_of(work, struct line, work.work);
101865cff704SKent Gibson 	struct linereq *lr;
1019b1a92e94SKent Gibson 	u64 eflags, edflags = READ_ONCE(line->edflags);
1020272ddba0SKent Gibson 	int level = -1;
1021272ddba0SKent Gibson #ifdef CONFIG_HTE
1022272ddba0SKent Gibson 	int diff_seqno;
102365cff704SKent Gibson 
1024b1a92e94SKent Gibson 	if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
10252068339aSDipen Patel 		level = line->raw_level;
1026272ddba0SKent Gibson #endif
10272068339aSDipen Patel 	if (level < 0)
102865cff704SKent Gibson 		level = gpiod_get_raw_value_cansleep(line->desc);
102965cff704SKent Gibson 	if (level < 0) {
103065cff704SKent Gibson 		pr_debug_ratelimited("debouncer failed to read line value\n");
103165cff704SKent Gibson 		return;
103265cff704SKent Gibson 	}
103365cff704SKent Gibson 
103465cff704SKent Gibson 	if (READ_ONCE(line->level) == level)
103565cff704SKent Gibson 		return;
103665cff704SKent Gibson 
103765cff704SKent Gibson 	WRITE_ONCE(line->level, level);
103865cff704SKent Gibson 
103965cff704SKent Gibson 	/* -- edge detection -- */
1040b1a92e94SKent Gibson 	eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
10413ffb7c45SKent Gibson 	if (!eflags)
104265cff704SKent Gibson 		return;
104365cff704SKent Gibson 
104465cff704SKent Gibson 	/* switch from physical level to logical - if they differ */
1045b1a92e94SKent Gibson 	if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
104665cff704SKent Gibson 		level = !level;
104765cff704SKent Gibson 
104865cff704SKent Gibson 	/* ignore edges that are not being monitored */
10493ffb7c45SKent Gibson 	if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
10503ffb7c45SKent Gibson 	    ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
105165cff704SKent Gibson 		return;
105265cff704SKent Gibson 
105365cff704SKent Gibson 	/* Do not leak kernel stack to userspace */
105465cff704SKent Gibson 	memset(&le, 0, sizeof(le));
105565cff704SKent Gibson 
105665cff704SKent Gibson 	lr = line->req;
105726d060e4SKent Gibson 	le.timestamp_ns = line_event_timestamp(line);
105865cff704SKent Gibson 	le.offset = gpio_chip_hwgpio(line->desc);
1059272ddba0SKent Gibson #ifdef CONFIG_HTE
1060b1a92e94SKent Gibson 	if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) {
10612068339aSDipen Patel 		/* discard events except the last one */
10622068339aSDipen Patel 		line->total_discard_seq -= 1;
10632068339aSDipen Patel 		diff_seqno = line->last_seqno - line->total_discard_seq -
10642068339aSDipen Patel 				line->line_seqno;
10652068339aSDipen Patel 		line->line_seqno = line->last_seqno - line->total_discard_seq;
10662068339aSDipen Patel 		le.line_seqno = line->line_seqno;
10672068339aSDipen Patel 		le.seqno = (lr->num_lines == 1) ?
10682068339aSDipen Patel 			le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno);
1069272ddba0SKent Gibson 	} else
1070272ddba0SKent Gibson #endif /* CONFIG_HTE */
1071272ddba0SKent Gibson 	{
107265cff704SKent Gibson 		line->line_seqno++;
107365cff704SKent Gibson 		le.line_seqno = line->line_seqno;
107465cff704SKent Gibson 		le.seqno = (lr->num_lines == 1) ?
107565cff704SKent Gibson 			le.line_seqno : atomic_inc_return(&lr->seqno);
10762068339aSDipen Patel 	}
107765cff704SKent Gibson 
107824220232SKent Gibson 	le.id = line_event_id(level);
107965cff704SKent Gibson 
108065cff704SKent Gibson 	linereq_put_event(lr, &le);
108165cff704SKent Gibson }
108265cff704SKent Gibson 
1083b1a92e94SKent Gibson static int debounce_setup(struct line *line, unsigned int debounce_period_us)
108465cff704SKent Gibson {
108565cff704SKent Gibson 	unsigned long irqflags;
108665cff704SKent Gibson 	int ret, level, irq;
10872f0262acSKent Gibson 	char *label;
108865cff704SKent Gibson 
108965cff704SKent Gibson 	/* try hardware */
109065cff704SKent Gibson 	ret = gpiod_set_debounce(line->desc, debounce_period_us);
109165cff704SKent Gibson 	if (!ret) {
1092309364bcSKent Gibson 		line_set_debounce_period(line, debounce_period_us);
109365cff704SKent Gibson 		return ret;
109465cff704SKent Gibson 	}
109565cff704SKent Gibson 	if (ret != -ENOTSUPP)
109665cff704SKent Gibson 		return ret;
109765cff704SKent Gibson 
109865cff704SKent Gibson 	if (debounce_period_us) {
109965cff704SKent Gibson 		/* setup software debounce */
110065cff704SKent Gibson 		level = gpiod_get_raw_value_cansleep(line->desc);
110165cff704SKent Gibson 		if (level < 0)
110265cff704SKent Gibson 			return level;
110365cff704SKent Gibson 
1104272ddba0SKent Gibson 		if (!(IS_ENABLED(CONFIG_HTE) &&
1105272ddba0SKent Gibson 		      test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))) {
110665cff704SKent Gibson 			irq = gpiod_to_irq(line->desc);
110765cff704SKent Gibson 			if (irq < 0)
110865cff704SKent Gibson 				return -ENXIO;
110965cff704SKent Gibson 
11102f0262acSKent Gibson 			label = make_irq_label(line->req->label);
11112f0262acSKent Gibson 			if (IS_ERR(label))
11122f0262acSKent Gibson 				return -ENOMEM;
11132f0262acSKent Gibson 
111465cff704SKent Gibson 			irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
111565cff704SKent Gibson 			ret = request_irq(irq, debounce_irq_handler, irqflags,
11162f0262acSKent Gibson 					  label, line);
11172f0262acSKent Gibson 			if (ret) {
11182f0262acSKent Gibson 				free_irq_label(label);
111965cff704SKent Gibson 				return ret;
11202f0262acSKent Gibson 			}
112165cff704SKent Gibson 			line->irq = irq;
11222068339aSDipen Patel 		} else {
11232487a812SKent Gibson 			ret = hte_edge_setup(line, GPIO_V2_LINE_FLAG_EDGE_BOTH);
11242068339aSDipen Patel 			if (ret)
11252068339aSDipen Patel 				return ret;
11262068339aSDipen Patel 		}
11272068339aSDipen Patel 
11282068339aSDipen Patel 		WRITE_ONCE(line->level, level);
11292068339aSDipen Patel 		WRITE_ONCE(line->sw_debounced, 1);
113065cff704SKent Gibson 	}
113165cff704SKent Gibson 	return 0;
113265cff704SKent Gibson }
113365cff704SKent Gibson 
113465cff704SKent Gibson static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
113565cff704SKent Gibson 					  unsigned int line_idx)
113665cff704SKent Gibson {
113765cff704SKent Gibson 	unsigned int i;
113865cff704SKent Gibson 	u64 mask = BIT_ULL(line_idx);
113965cff704SKent Gibson 
114065cff704SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
114165cff704SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
114265cff704SKent Gibson 		    (lc->attrs[i].mask & mask))
114365cff704SKent Gibson 			return true;
114465cff704SKent Gibson 	}
114565cff704SKent Gibson 	return false;
114665cff704SKent Gibson }
114765cff704SKent Gibson 
114865cff704SKent Gibson static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
114965cff704SKent Gibson 					       unsigned int line_idx)
115065cff704SKent Gibson {
115165cff704SKent Gibson 	unsigned int i;
115265cff704SKent Gibson 	u64 mask = BIT_ULL(line_idx);
115365cff704SKent Gibson 
115465cff704SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
115565cff704SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
115665cff704SKent Gibson 		    (lc->attrs[i].mask & mask))
115765cff704SKent Gibson 			return lc->attrs[i].attr.debounce_period_us;
115865cff704SKent Gibson 	}
115965cff704SKent Gibson 	return 0;
116065cff704SKent Gibson }
116165cff704SKent Gibson 
1162b1a92e94SKent Gibson static void edge_detector_stop(struct line *line)
116373e03419SKent Gibson {
1164b1a92e94SKent Gibson 	if (line->irq) {
116521bc9b15SBartosz Golaszewski 		free_irq_label(free_irq(line->irq, line));
116673e03419SKent Gibson 		line->irq = 0;
116773e03419SKent Gibson 	}
1168a54756cbSKent Gibson 
1169272ddba0SKent Gibson #ifdef CONFIG_HTE
1170b1a92e94SKent Gibson 	if (READ_ONCE(line->edflags) & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
11712068339aSDipen Patel 		hte_ts_put(&line->hdesc);
1172272ddba0SKent Gibson #endif
11732068339aSDipen Patel 
117465cff704SKent Gibson 	cancel_delayed_work_sync(&line->work);
117565cff704SKent Gibson 	WRITE_ONCE(line->sw_debounced, 0);
1176b1a92e94SKent Gibson 	WRITE_ONCE(line->edflags, 0);
1177309364bcSKent Gibson 	line_set_debounce_period(line, 0);
117865cff704SKent Gibson 	/* do not change line->level - see comment in debounced_value() */
117973e03419SKent Gibson }
118073e03419SKent Gibson 
118173e03419SKent Gibson static int edge_detector_setup(struct line *line,
118265cff704SKent Gibson 			       struct gpio_v2_line_config *lc,
1183b1a92e94SKent Gibson 			       unsigned int line_idx, u64 edflags)
118473e03419SKent Gibson {
118565cff704SKent Gibson 	u32 debounce_period_us;
118673e03419SKent Gibson 	unsigned long irqflags = 0;
1187b1a92e94SKent Gibson 	u64 eflags;
118873e03419SKent Gibson 	int irq, ret;
118921bc9b15SBartosz Golaszewski 	char *label;
119073e03419SKent Gibson 
1191b1a92e94SKent Gibson 	eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
119273e03419SKent Gibson 	if (eflags && !kfifo_initialized(&line->req->events)) {
119373e03419SKent Gibson 		ret = kfifo_alloc(&line->req->events,
119473e03419SKent Gibson 				  line->req->event_buffer_size, GFP_KERNEL);
119573e03419SKent Gibson 		if (ret)
119673e03419SKent Gibson 			return ret;
119773e03419SKent Gibson 	}
119865cff704SKent Gibson 	if (gpio_v2_line_config_debounced(lc, line_idx)) {
119965cff704SKent Gibson 		debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
1200b1a92e94SKent Gibson 		ret = debounce_setup(line, debounce_period_us);
120165cff704SKent Gibson 		if (ret)
120265cff704SKent Gibson 			return ret;
1203309364bcSKent Gibson 		line_set_debounce_period(line, debounce_period_us);
120465cff704SKent Gibson 	}
120573e03419SKent Gibson 
120665cff704SKent Gibson 	/* detection disabled or sw debouncer will provide edge detection */
120765cff704SKent Gibson 	if (!eflags || READ_ONCE(line->sw_debounced))
120873e03419SKent Gibson 		return 0;
120973e03419SKent Gibson 
1210272ddba0SKent Gibson 	if (IS_ENABLED(CONFIG_HTE) &&
1211272ddba0SKent Gibson 	    (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1212b1a92e94SKent Gibson 		return hte_edge_setup(line, edflags);
12132068339aSDipen Patel 
121473e03419SKent Gibson 	irq = gpiod_to_irq(line->desc);
121573e03419SKent Gibson 	if (irq < 0)
121673e03419SKent Gibson 		return -ENXIO;
121773e03419SKent Gibson 
121873e03419SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
121973e03419SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
122073e03419SKent Gibson 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
122173e03419SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
122273e03419SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
122373e03419SKent Gibson 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
122473e03419SKent Gibson 	irqflags |= IRQF_ONESHOT;
122573e03419SKent Gibson 
122621bc9b15SBartosz Golaszewski 	label = make_irq_label(line->req->label);
1227d9f0804aSBartosz Golaszewski 	if (IS_ERR(label))
1228d9f0804aSBartosz Golaszewski 		return PTR_ERR(label);
122921bc9b15SBartosz Golaszewski 
123073e03419SKent Gibson 	/* Request a thread to read the events */
123173e03419SKent Gibson 	ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
123221bc9b15SBartosz Golaszewski 				   irqflags, label, line);
123321bc9b15SBartosz Golaszewski 	if (ret) {
123421bc9b15SBartosz Golaszewski 		free_irq_label(label);
123573e03419SKent Gibson 		return ret;
123621bc9b15SBartosz Golaszewski 	}
123773e03419SKent Gibson 
123873e03419SKent Gibson 	line->irq = irq;
123973e03419SKent Gibson 	return 0;
124073e03419SKent Gibson }
124173e03419SKent Gibson 
124265cff704SKent Gibson static int edge_detector_update(struct line *line,
124365cff704SKent Gibson 				struct gpio_v2_line_config *lc,
1244b1a92e94SKent Gibson 				unsigned int line_idx, u64 edflags)
1245a54756cbSKent Gibson {
1246*883e4bbfSKent Gibson 	u64 eflags;
1247*883e4bbfSKent Gibson 	int ret;
1248b1a92e94SKent Gibson 	u64 active_edflags = READ_ONCE(line->edflags);
124965cff704SKent Gibson 	unsigned int debounce_period_us =
125065cff704SKent Gibson 			gpio_v2_line_config_debounce_period(lc, line_idx);
125165cff704SKent Gibson 
1252b1a92e94SKent Gibson 	if ((active_edflags == edflags) &&
1253309364bcSKent Gibson 	    (READ_ONCE(line->debounce_period_us) == debounce_period_us))
1254a54756cbSKent Gibson 		return 0;
1255a54756cbSKent Gibson 
125665cff704SKent Gibson 	/* sw debounced and still will be...*/
125765cff704SKent Gibson 	if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
1258309364bcSKent Gibson 		line_set_debounce_period(line, debounce_period_us);
1259*883e4bbfSKent Gibson 		/*
1260*883e4bbfSKent Gibson 		 * ensure event fifo is initialised if edge detection
1261*883e4bbfSKent Gibson 		 * is now enabled.
1262*883e4bbfSKent Gibson 		 */
1263*883e4bbfSKent Gibson 		eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
1264*883e4bbfSKent Gibson 		if (eflags && !kfifo_initialized(&line->req->events)) {
1265*883e4bbfSKent Gibson 			ret = kfifo_alloc(&line->req->events,
1266*883e4bbfSKent Gibson 					  line->req->event_buffer_size,
1267*883e4bbfSKent Gibson 					  GFP_KERNEL);
1268*883e4bbfSKent Gibson 			if (ret)
1269*883e4bbfSKent Gibson 				return ret;
1270*883e4bbfSKent Gibson 		}
127165cff704SKent Gibson 		return 0;
127265cff704SKent Gibson 	}
127365cff704SKent Gibson 
127465cff704SKent Gibson 	/* reconfiguring edge detection or sw debounce being disabled */
1275b1a92e94SKent Gibson 	if ((line->irq && !READ_ONCE(line->sw_debounced)) ||
1276b1a92e94SKent Gibson 	    (active_edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) ||
127765cff704SKent Gibson 	    (!debounce_period_us && READ_ONCE(line->sw_debounced)))
1278b1a92e94SKent Gibson 		edge_detector_stop(line);
1279a54756cbSKent Gibson 
1280b1a92e94SKent Gibson 	return edge_detector_setup(line, lc, line_idx, edflags);
1281a54756cbSKent Gibson }
1282a54756cbSKent Gibson 
12833c0d9c63SKent Gibson static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
12843c0d9c63SKent Gibson 				     unsigned int line_idx)
12853c0d9c63SKent Gibson {
12863c0d9c63SKent Gibson 	unsigned int i;
12873c0d9c63SKent Gibson 	u64 mask = BIT_ULL(line_idx);
12883c0d9c63SKent Gibson 
12893c0d9c63SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
12903c0d9c63SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
12913c0d9c63SKent Gibson 		    (lc->attrs[i].mask & mask))
12923c0d9c63SKent Gibson 			return lc->attrs[i].attr.flags;
12933c0d9c63SKent Gibson 	}
12943c0d9c63SKent Gibson 	return lc->flags;
12953c0d9c63SKent Gibson }
12963c0d9c63SKent Gibson 
12973c0d9c63SKent Gibson static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
12983c0d9c63SKent Gibson 					    unsigned int line_idx)
12993c0d9c63SKent Gibson {
13003c0d9c63SKent Gibson 	unsigned int i;
13013c0d9c63SKent Gibson 	u64 mask = BIT_ULL(line_idx);
13023c0d9c63SKent Gibson 
13033c0d9c63SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
13043c0d9c63SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
13053c0d9c63SKent Gibson 		    (lc->attrs[i].mask & mask))
13063c0d9c63SKent Gibson 			return !!(lc->attrs[i].attr.values & mask);
13073c0d9c63SKent Gibson 	}
13083c0d9c63SKent Gibson 	return 0;
13093c0d9c63SKent Gibson }
13103c0d9c63SKent Gibson 
13113c0d9c63SKent Gibson static int gpio_v2_line_flags_validate(u64 flags)
13123c0d9c63SKent Gibson {
13133c0d9c63SKent Gibson 	/* Return an error if an unknown flag is set */
13143c0d9c63SKent Gibson 	if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
13153c0d9c63SKent Gibson 		return -EINVAL;
1316272ddba0SKent Gibson 
1317272ddba0SKent Gibson 	if (!IS_ENABLED(CONFIG_HTE) &&
1318272ddba0SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1319272ddba0SKent Gibson 		return -EOPNOTSUPP;
1320272ddba0SKent Gibson 
13213c0d9c63SKent Gibson 	/*
13223c0d9c63SKent Gibson 	 * Do not allow both INPUT and OUTPUT flags to be set as they are
13233c0d9c63SKent Gibson 	 * contradictory.
13243c0d9c63SKent Gibson 	 */
13253c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
13263c0d9c63SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_OUTPUT))
13273c0d9c63SKent Gibson 		return -EINVAL;
13283c0d9c63SKent Gibson 
13292068339aSDipen Patel 	/* Only allow one event clock source */
1330272ddba0SKent Gibson 	if (IS_ENABLED(CONFIG_HTE) &&
1331272ddba0SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) &&
13322068339aSDipen Patel 	    (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
13332068339aSDipen Patel 		return -EINVAL;
13342068339aSDipen Patel 
133573e03419SKent Gibson 	/* Edge detection requires explicit input. */
133673e03419SKent Gibson 	if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
133773e03419SKent Gibson 	    !(flags & GPIO_V2_LINE_FLAG_INPUT))
133873e03419SKent Gibson 		return -EINVAL;
133973e03419SKent Gibson 
13403c0d9c63SKent Gibson 	/*
13413c0d9c63SKent Gibson 	 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
13423c0d9c63SKent Gibson 	 * request. If the hardware actually supports enabling both at the
13433c0d9c63SKent Gibson 	 * same time the electrical result would be disastrous.
13443c0d9c63SKent Gibson 	 */
13453c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
13463c0d9c63SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
13473c0d9c63SKent Gibson 		return -EINVAL;
13483c0d9c63SKent Gibson 
13493c0d9c63SKent Gibson 	/* Drive requires explicit output direction. */
13503c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
13513c0d9c63SKent Gibson 	    !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
13523c0d9c63SKent Gibson 		return -EINVAL;
13533c0d9c63SKent Gibson 
13543c0d9c63SKent Gibson 	/* Bias requires explicit direction. */
13553c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
13563c0d9c63SKent Gibson 	    !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
13573c0d9c63SKent Gibson 		return -EINVAL;
13583c0d9c63SKent Gibson 
13593c0d9c63SKent Gibson 	/* Only one bias flag can be set. */
13603c0d9c63SKent Gibson 	if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
13613c0d9c63SKent Gibson 	     (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
13623c0d9c63SKent Gibson 		       GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
13633c0d9c63SKent Gibson 	    ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
13643c0d9c63SKent Gibson 	     (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
13653c0d9c63SKent Gibson 		return -EINVAL;
13663c0d9c63SKent Gibson 
13673c0d9c63SKent Gibson 	return 0;
13683c0d9c63SKent Gibson }
13693c0d9c63SKent Gibson 
13703c0d9c63SKent Gibson static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
13713c0d9c63SKent Gibson 					unsigned int num_lines)
13723c0d9c63SKent Gibson {
13733c0d9c63SKent Gibson 	unsigned int i;
13743c0d9c63SKent Gibson 	u64 flags;
13753c0d9c63SKent Gibson 	int ret;
13763c0d9c63SKent Gibson 
13773c0d9c63SKent Gibson 	if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
13783c0d9c63SKent Gibson 		return -EINVAL;
13793c0d9c63SKent Gibson 
13803c0d9c63SKent Gibson 	if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
13813c0d9c63SKent Gibson 		return -EINVAL;
13823c0d9c63SKent Gibson 
13833c0d9c63SKent Gibson 	for (i = 0; i < num_lines; i++) {
13843c0d9c63SKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
13853c0d9c63SKent Gibson 		ret = gpio_v2_line_flags_validate(flags);
13863c0d9c63SKent Gibson 		if (ret)
13873c0d9c63SKent Gibson 			return ret;
138865cff704SKent Gibson 
138965cff704SKent Gibson 		/* debounce requires explicit input */
139065cff704SKent Gibson 		if (gpio_v2_line_config_debounced(lc, i) &&
139165cff704SKent Gibson 		    !(flags & GPIO_V2_LINE_FLAG_INPUT))
139265cff704SKent Gibson 			return -EINVAL;
13933c0d9c63SKent Gibson 	}
13943c0d9c63SKent Gibson 	return 0;
13953c0d9c63SKent Gibson }
13963c0d9c63SKent Gibson 
13973c0d9c63SKent Gibson static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
13983c0d9c63SKent Gibson 						    unsigned long *flagsp)
13993c0d9c63SKent Gibson {
14003c0d9c63SKent Gibson 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
14013c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
14023c0d9c63SKent Gibson 
14033c0d9c63SKent Gibson 	if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
14043c0d9c63SKent Gibson 		set_bit(FLAG_IS_OUT, flagsp);
14053c0d9c63SKent Gibson 	else if (flags & GPIO_V2_LINE_FLAG_INPUT)
14063c0d9c63SKent Gibson 		clear_bit(FLAG_IS_OUT, flagsp);
14073c0d9c63SKent Gibson 
140873e03419SKent Gibson 	assign_bit(FLAG_EDGE_RISING, flagsp,
140973e03419SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
141073e03419SKent Gibson 	assign_bit(FLAG_EDGE_FALLING, flagsp,
141173e03419SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
141273e03419SKent Gibson 
14133c0d9c63SKent Gibson 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
14143c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
14153c0d9c63SKent Gibson 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
14163c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
14173c0d9c63SKent Gibson 
14183c0d9c63SKent Gibson 	assign_bit(FLAG_PULL_UP, flagsp,
14193c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
14203c0d9c63SKent Gibson 	assign_bit(FLAG_PULL_DOWN, flagsp,
14213c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
14223c0d9c63SKent Gibson 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
14233c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
142426d060e4SKent Gibson 
142526d060e4SKent Gibson 	assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
142626d060e4SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
14272068339aSDipen Patel 	assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp,
14282068339aSDipen Patel 		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
14293c0d9c63SKent Gibson }
14303c0d9c63SKent Gibson 
14313c0d9c63SKent Gibson static long linereq_get_values(struct linereq *lr, void __user *ip)
14323c0d9c63SKent Gibson {
14333c0d9c63SKent Gibson 	struct gpio_v2_line_values lv;
14343c0d9c63SKent Gibson 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
14353c0d9c63SKent Gibson 	struct gpio_desc **descs;
14363c0d9c63SKent Gibson 	unsigned int i, didx, num_get;
143765cff704SKent Gibson 	bool val;
14383c0d9c63SKent Gibson 	int ret;
14393c0d9c63SKent Gibson 
14403c0d9c63SKent Gibson 	/* NOTE: It's ok to read values of output lines. */
14413c0d9c63SKent Gibson 	if (copy_from_user(&lv, ip, sizeof(lv)))
14423c0d9c63SKent Gibson 		return -EFAULT;
14433c0d9c63SKent Gibson 
14443c0d9c63SKent Gibson 	for (num_get = 0, i = 0; i < lr->num_lines; i++) {
14453c0d9c63SKent Gibson 		if (lv.mask & BIT_ULL(i)) {
14463c0d9c63SKent Gibson 			num_get++;
14473c0d9c63SKent Gibson 			descs = &lr->lines[i].desc;
14483c0d9c63SKent Gibson 		}
14493c0d9c63SKent Gibson 	}
14503c0d9c63SKent Gibson 
14513c0d9c63SKent Gibson 	if (num_get == 0)
14523c0d9c63SKent Gibson 		return -EINVAL;
14533c0d9c63SKent Gibson 
14543c0d9c63SKent Gibson 	if (num_get != 1) {
14553c0d9c63SKent Gibson 		descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
14563c0d9c63SKent Gibson 		if (!descs)
14573c0d9c63SKent Gibson 			return -ENOMEM;
14583c0d9c63SKent Gibson 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
14593c0d9c63SKent Gibson 			if (lv.mask & BIT_ULL(i)) {
14603c0d9c63SKent Gibson 				descs[didx] = lr->lines[i].desc;
14613c0d9c63SKent Gibson 				didx++;
14623c0d9c63SKent Gibson 			}
14633c0d9c63SKent Gibson 		}
14643c0d9c63SKent Gibson 	}
14653c0d9c63SKent Gibson 	ret = gpiod_get_array_value_complex(false, true, num_get,
14663c0d9c63SKent Gibson 					    descs, NULL, vals);
14673c0d9c63SKent Gibson 
14683c0d9c63SKent Gibson 	if (num_get != 1)
14693c0d9c63SKent Gibson 		kfree(descs);
14703c0d9c63SKent Gibson 	if (ret)
14713c0d9c63SKent Gibson 		return ret;
14723c0d9c63SKent Gibson 
14733c0d9c63SKent Gibson 	lv.bits = 0;
14743c0d9c63SKent Gibson 	for (didx = 0, i = 0; i < lr->num_lines; i++) {
14753c0d9c63SKent Gibson 		if (lv.mask & BIT_ULL(i)) {
147665cff704SKent Gibson 			if (lr->lines[i].sw_debounced)
147765cff704SKent Gibson 				val = debounced_value(&lr->lines[i]);
147865cff704SKent Gibson 			else
147965cff704SKent Gibson 				val = test_bit(didx, vals);
148065cff704SKent Gibson 			if (val)
14813c0d9c63SKent Gibson 				lv.bits |= BIT_ULL(i);
14823c0d9c63SKent Gibson 			didx++;
14833c0d9c63SKent Gibson 		}
14843c0d9c63SKent Gibson 	}
14853c0d9c63SKent Gibson 
14863c0d9c63SKent Gibson 	if (copy_to_user(ip, &lv, sizeof(lv)))
14873c0d9c63SKent Gibson 		return -EFAULT;
14883c0d9c63SKent Gibson 
14893c0d9c63SKent Gibson 	return 0;
14903c0d9c63SKent Gibson }
14913c0d9c63SKent Gibson 
14927b8e00d9SKent Gibson static long linereq_set_values_unlocked(struct linereq *lr,
14937b8e00d9SKent Gibson 					struct gpio_v2_line_values *lv)
14947b8e00d9SKent Gibson {
14957b8e00d9SKent Gibson 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
14967b8e00d9SKent Gibson 	struct gpio_desc **descs;
14977b8e00d9SKent Gibson 	unsigned int i, didx, num_set;
14987b8e00d9SKent Gibson 	int ret;
14997b8e00d9SKent Gibson 
15007b8e00d9SKent Gibson 	bitmap_zero(vals, GPIO_V2_LINES_MAX);
15017b8e00d9SKent Gibson 	for (num_set = 0, i = 0; i < lr->num_lines; i++) {
15027b8e00d9SKent Gibson 		if (lv->mask & BIT_ULL(i)) {
15037b8e00d9SKent Gibson 			if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
15047b8e00d9SKent Gibson 				return -EPERM;
15057b8e00d9SKent Gibson 			if (lv->bits & BIT_ULL(i))
15067b8e00d9SKent Gibson 				__set_bit(num_set, vals);
15077b8e00d9SKent Gibson 			num_set++;
15087b8e00d9SKent Gibson 			descs = &lr->lines[i].desc;
15097b8e00d9SKent Gibson 		}
15107b8e00d9SKent Gibson 	}
15117b8e00d9SKent Gibson 	if (num_set == 0)
15127b8e00d9SKent Gibson 		return -EINVAL;
15137b8e00d9SKent Gibson 
15147b8e00d9SKent Gibson 	if (num_set != 1) {
15157b8e00d9SKent Gibson 		/* build compacted desc array and values */
15167b8e00d9SKent Gibson 		descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
15177b8e00d9SKent Gibson 		if (!descs)
15187b8e00d9SKent Gibson 			return -ENOMEM;
15197b8e00d9SKent Gibson 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
15207b8e00d9SKent Gibson 			if (lv->mask & BIT_ULL(i)) {
15217b8e00d9SKent Gibson 				descs[didx] = lr->lines[i].desc;
15227b8e00d9SKent Gibson 				didx++;
15237b8e00d9SKent Gibson 			}
15247b8e00d9SKent Gibson 		}
15257b8e00d9SKent Gibson 	}
15267b8e00d9SKent Gibson 	ret = gpiod_set_array_value_complex(false, true, num_set,
15277b8e00d9SKent Gibson 					    descs, NULL, vals);
15287b8e00d9SKent Gibson 
15297b8e00d9SKent Gibson 	if (num_set != 1)
15307b8e00d9SKent Gibson 		kfree(descs);
15317b8e00d9SKent Gibson 	return ret;
15327b8e00d9SKent Gibson }
15337b8e00d9SKent Gibson 
15347b8e00d9SKent Gibson static long linereq_set_values(struct linereq *lr, void __user *ip)
15357b8e00d9SKent Gibson {
15367b8e00d9SKent Gibson 	struct gpio_v2_line_values lv;
15377b8e00d9SKent Gibson 	int ret;
15387b8e00d9SKent Gibson 
15397b8e00d9SKent Gibson 	if (copy_from_user(&lv, ip, sizeof(lv)))
15407b8e00d9SKent Gibson 		return -EFAULT;
15417b8e00d9SKent Gibson 
15427b8e00d9SKent Gibson 	mutex_lock(&lr->config_mutex);
15437b8e00d9SKent Gibson 
15447b8e00d9SKent Gibson 	ret = linereq_set_values_unlocked(lr, &lv);
15457b8e00d9SKent Gibson 
15467b8e00d9SKent Gibson 	mutex_unlock(&lr->config_mutex);
15477b8e00d9SKent Gibson 
15487b8e00d9SKent Gibson 	return ret;
15497b8e00d9SKent Gibson }
15507b8e00d9SKent Gibson 
1551a54756cbSKent Gibson static long linereq_set_config_unlocked(struct linereq *lr,
1552a54756cbSKent Gibson 					struct gpio_v2_line_config *lc)
1553a54756cbSKent Gibson {
1554a54756cbSKent Gibson 	struct gpio_desc *desc;
1555b1a92e94SKent Gibson 	struct line *line;
1556a54756cbSKent Gibson 	unsigned int i;
1557b1a92e94SKent Gibson 	u64 flags, edflags;
1558a54756cbSKent Gibson 	int ret;
1559a54756cbSKent Gibson 
1560a54756cbSKent Gibson 	for (i = 0; i < lr->num_lines; i++) {
1561b1a92e94SKent Gibson 		line = &lr->lines[i];
1562a54756cbSKent Gibson 		desc = lr->lines[i].desc;
1563a54756cbSKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
1564a54756cbSKent Gibson 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1565b1a92e94SKent Gibson 		edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
1566a54756cbSKent Gibson 		/*
1567a54756cbSKent Gibson 		 * Lines have to be requested explicitly for input
1568a54756cbSKent Gibson 		 * or output, else the line will be treated "as is".
1569a54756cbSKent Gibson 		 */
1570a54756cbSKent Gibson 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1571a54756cbSKent Gibson 			int val = gpio_v2_line_config_output_value(lc, i);
1572a54756cbSKent Gibson 
1573b1a92e94SKent Gibson 			edge_detector_stop(line);
1574a54756cbSKent Gibson 			ret = gpiod_direction_output(desc, val);
1575a54756cbSKent Gibson 			if (ret)
1576a54756cbSKent Gibson 				return ret;
1577a54756cbSKent Gibson 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1578a54756cbSKent Gibson 			ret = gpiod_direction_input(desc);
1579a54756cbSKent Gibson 			if (ret)
1580a54756cbSKent Gibson 				return ret;
1581a54756cbSKent Gibson 
1582b1a92e94SKent Gibson 			ret = edge_detector_update(line, lc, i, edflags);
1583a54756cbSKent Gibson 			if (ret)
1584a54756cbSKent Gibson 				return ret;
1585a54756cbSKent Gibson 		}
1586a54756cbSKent Gibson 
1587b1a92e94SKent Gibson 		WRITE_ONCE(line->edflags, edflags);
1588b1a92e94SKent Gibson 
15899ce4ed5bSBartosz Golaszewski 		gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
1590a54756cbSKent Gibson 	}
1591a54756cbSKent Gibson 	return 0;
1592a54756cbSKent Gibson }
1593a54756cbSKent Gibson 
1594a54756cbSKent Gibson static long linereq_set_config(struct linereq *lr, void __user *ip)
1595a54756cbSKent Gibson {
1596a54756cbSKent Gibson 	struct gpio_v2_line_config lc;
1597a54756cbSKent Gibson 	int ret;
1598a54756cbSKent Gibson 
1599a54756cbSKent Gibson 	if (copy_from_user(&lc, ip, sizeof(lc)))
1600a54756cbSKent Gibson 		return -EFAULT;
1601a54756cbSKent Gibson 
1602a54756cbSKent Gibson 	ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
1603a54756cbSKent Gibson 	if (ret)
1604a54756cbSKent Gibson 		return ret;
1605a54756cbSKent Gibson 
1606a54756cbSKent Gibson 	mutex_lock(&lr->config_mutex);
1607a54756cbSKent Gibson 
1608a54756cbSKent Gibson 	ret = linereq_set_config_unlocked(lr, &lc);
1609a54756cbSKent Gibson 
1610a54756cbSKent Gibson 	mutex_unlock(&lr->config_mutex);
1611a54756cbSKent Gibson 
1612a54756cbSKent Gibson 	return ret;
1613a54756cbSKent Gibson }
1614a54756cbSKent Gibson 
1615bdbbae24SBartosz Golaszewski static long linereq_ioctl_unlocked(struct file *file, unsigned int cmd,
16163c0d9c63SKent Gibson 				   unsigned long arg)
16173c0d9c63SKent Gibson {
16183c0d9c63SKent Gibson 	struct linereq *lr = file->private_data;
16193c0d9c63SKent Gibson 	void __user *ip = (void __user *)arg;
16203c0d9c63SKent Gibson 
1621533aae7cSBartosz Golaszewski 	if (!lr->gdev->chip)
1622533aae7cSBartosz Golaszewski 		return -ENODEV;
1623533aae7cSBartosz Golaszewski 
16241cef8b50SAndy Shevchenko 	switch (cmd) {
16251cef8b50SAndy Shevchenko 	case GPIO_V2_LINE_GET_VALUES_IOCTL:
16263c0d9c63SKent Gibson 		return linereq_get_values(lr, ip);
16271cef8b50SAndy Shevchenko 	case GPIO_V2_LINE_SET_VALUES_IOCTL:
16287b8e00d9SKent Gibson 		return linereq_set_values(lr, ip);
16291cef8b50SAndy Shevchenko 	case GPIO_V2_LINE_SET_CONFIG_IOCTL:
1630a54756cbSKent Gibson 		return linereq_set_config(lr, ip);
16311cef8b50SAndy Shevchenko 	default:
16323c0d9c63SKent Gibson 		return -EINVAL;
16333c0d9c63SKent Gibson 	}
16341cef8b50SAndy Shevchenko }
16353c0d9c63SKent Gibson 
1636bdbbae24SBartosz Golaszewski static long linereq_ioctl(struct file *file, unsigned int cmd,
1637bdbbae24SBartosz Golaszewski 			  unsigned long arg)
1638bdbbae24SBartosz Golaszewski {
1639bdbbae24SBartosz Golaszewski 	struct linereq *lr = file->private_data;
1640bdbbae24SBartosz Golaszewski 
1641bdbbae24SBartosz Golaszewski 	return call_ioctl_locked(file, cmd, arg, lr->gdev,
1642bdbbae24SBartosz Golaszewski 				 linereq_ioctl_unlocked);
1643bdbbae24SBartosz Golaszewski }
1644bdbbae24SBartosz Golaszewski 
16453c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
16463c0d9c63SKent Gibson static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
16473c0d9c63SKent Gibson 				 unsigned long arg)
16483c0d9c63SKent Gibson {
16493c0d9c63SKent Gibson 	return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
16503c0d9c63SKent Gibson }
16513c0d9c63SKent Gibson #endif
16523c0d9c63SKent Gibson 
1653bdbbae24SBartosz Golaszewski static __poll_t linereq_poll_unlocked(struct file *file,
165473e03419SKent Gibson 				      struct poll_table_struct *wait)
165573e03419SKent Gibson {
165673e03419SKent Gibson 	struct linereq *lr = file->private_data;
165773e03419SKent Gibson 	__poll_t events = 0;
165873e03419SKent Gibson 
1659533aae7cSBartosz Golaszewski 	if (!lr->gdev->chip)
1660533aae7cSBartosz Golaszewski 		return EPOLLHUP | EPOLLERR;
1661533aae7cSBartosz Golaszewski 
166273e03419SKent Gibson 	poll_wait(file, &lr->wait, wait);
166373e03419SKent Gibson 
166473e03419SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
166573e03419SKent Gibson 						 &lr->wait.lock))
166673e03419SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
166773e03419SKent Gibson 
166873e03419SKent Gibson 	return events;
166973e03419SKent Gibson }
167073e03419SKent Gibson 
1671bdbbae24SBartosz Golaszewski static __poll_t linereq_poll(struct file *file,
1672bdbbae24SBartosz Golaszewski 			     struct poll_table_struct *wait)
1673bdbbae24SBartosz Golaszewski {
1674bdbbae24SBartosz Golaszewski 	struct linereq *lr = file->private_data;
1675bdbbae24SBartosz Golaszewski 
1676bdbbae24SBartosz Golaszewski 	return call_poll_locked(file, wait, lr->gdev, linereq_poll_unlocked);
1677bdbbae24SBartosz Golaszewski }
1678bdbbae24SBartosz Golaszewski 
1679bdbbae24SBartosz Golaszewski static ssize_t linereq_read_unlocked(struct file *file, char __user *buf,
1680bdbbae24SBartosz Golaszewski 				     size_t count, loff_t *f_ps)
168173e03419SKent Gibson {
168273e03419SKent Gibson 	struct linereq *lr = file->private_data;
168373e03419SKent Gibson 	struct gpio_v2_line_event le;
168473e03419SKent Gibson 	ssize_t bytes_read = 0;
168573e03419SKent Gibson 	int ret;
168673e03419SKent Gibson 
1687533aae7cSBartosz Golaszewski 	if (!lr->gdev->chip)
1688533aae7cSBartosz Golaszewski 		return -ENODEV;
1689533aae7cSBartosz Golaszewski 
169073e03419SKent Gibson 	if (count < sizeof(le))
169173e03419SKent Gibson 		return -EINVAL;
169273e03419SKent Gibson 
169373e03419SKent Gibson 	do {
169473e03419SKent Gibson 		spin_lock(&lr->wait.lock);
169573e03419SKent Gibson 		if (kfifo_is_empty(&lr->events)) {
169673e03419SKent Gibson 			if (bytes_read) {
169773e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
169873e03419SKent Gibson 				return bytes_read;
169973e03419SKent Gibson 			}
170073e03419SKent Gibson 
170173e03419SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
170273e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
170373e03419SKent Gibson 				return -EAGAIN;
170473e03419SKent Gibson 			}
170573e03419SKent Gibson 
170673e03419SKent Gibson 			ret = wait_event_interruptible_locked(lr->wait,
170773e03419SKent Gibson 					!kfifo_is_empty(&lr->events));
170873e03419SKent Gibson 			if (ret) {
170973e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
171073e03419SKent Gibson 				return ret;
171173e03419SKent Gibson 			}
171273e03419SKent Gibson 		}
171373e03419SKent Gibson 
171473e03419SKent Gibson 		ret = kfifo_out(&lr->events, &le, 1);
171573e03419SKent Gibson 		spin_unlock(&lr->wait.lock);
171673e03419SKent Gibson 		if (ret != 1) {
171773e03419SKent Gibson 			/*
171873e03419SKent Gibson 			 * This should never happen - we were holding the
171973e03419SKent Gibson 			 * lock from the moment we learned the fifo is no
172073e03419SKent Gibson 			 * longer empty until now.
172173e03419SKent Gibson 			 */
172273e03419SKent Gibson 			ret = -EIO;
172373e03419SKent Gibson 			break;
172473e03419SKent Gibson 		}
172573e03419SKent Gibson 
172673e03419SKent Gibson 		if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
172773e03419SKent Gibson 			return -EFAULT;
172873e03419SKent Gibson 		bytes_read += sizeof(le);
172973e03419SKent Gibson 	} while (count >= bytes_read + sizeof(le));
173073e03419SKent Gibson 
173173e03419SKent Gibson 	return bytes_read;
173273e03419SKent Gibson }
173373e03419SKent Gibson 
1734bdbbae24SBartosz Golaszewski static ssize_t linereq_read(struct file *file, char __user *buf,
1735bdbbae24SBartosz Golaszewski 			    size_t count, loff_t *f_ps)
1736bdbbae24SBartosz Golaszewski {
1737bdbbae24SBartosz Golaszewski 	struct linereq *lr = file->private_data;
1738bdbbae24SBartosz Golaszewski 
1739bdbbae24SBartosz Golaszewski 	return call_read_locked(file, buf, count, f_ps, lr->gdev,
1740bdbbae24SBartosz Golaszewski 				linereq_read_unlocked);
1741bdbbae24SBartosz Golaszewski }
1742bdbbae24SBartosz Golaszewski 
17433c0d9c63SKent Gibson static void linereq_free(struct linereq *lr)
17443c0d9c63SKent Gibson {
1745309364bcSKent Gibson 	struct line *line;
17463c0d9c63SKent Gibson 	unsigned int i;
17473c0d9c63SKent Gibson 
1748a0dda508SBartosz Golaszewski 	if (lr->device_unregistered_nb.notifier_call)
1749a0dda508SBartosz Golaszewski 		blocking_notifier_chain_unregister(&lr->gdev->device_notifier,
1750a0dda508SBartosz Golaszewski 						   &lr->device_unregistered_nb);
1751a0dda508SBartosz Golaszewski 
17523c0d9c63SKent Gibson 	for (i = 0; i < lr->num_lines; i++) {
1753309364bcSKent Gibson 		line = &lr->lines[i];
1754309364bcSKent Gibson 		if (!line->desc)
1755309364bcSKent Gibson 			continue;
1756309364bcSKent Gibson 
1757309364bcSKent Gibson 		edge_detector_stop(line);
1758309364bcSKent Gibson 		if (line_has_supinfo(line))
1759309364bcSKent Gibson 			supinfo_erase(line);
1760309364bcSKent Gibson 		gpiod_free(line->desc);
1761160d6e40SKent Gibson 	}
176273e03419SKent Gibson 	kfifo_free(&lr->events);
17633c0d9c63SKent Gibson 	kfree(lr->label);
1764dc0989e3SAndy Shevchenko 	gpio_device_put(lr->gdev);
17653c0d9c63SKent Gibson 	kfree(lr);
17663c0d9c63SKent Gibson }
17673c0d9c63SKent Gibson 
17683c0d9c63SKent Gibson static int linereq_release(struct inode *inode, struct file *file)
17693c0d9c63SKent Gibson {
17703c0d9c63SKent Gibson 	struct linereq *lr = file->private_data;
17713c0d9c63SKent Gibson 
17723c0d9c63SKent Gibson 	linereq_free(lr);
17733c0d9c63SKent Gibson 	return 0;
17743c0d9c63SKent Gibson }
17753c0d9c63SKent Gibson 
17760ae3109aSBartosz Golaszewski #ifdef CONFIG_PROC_FS
17770ae3109aSBartosz Golaszewski static void linereq_show_fdinfo(struct seq_file *out, struct file *file)
17780ae3109aSBartosz Golaszewski {
17790ae3109aSBartosz Golaszewski 	struct linereq *lr = file->private_data;
17800ae3109aSBartosz Golaszewski 	struct device *dev = &lr->gdev->dev;
17810ae3109aSBartosz Golaszewski 	u16 i;
17820ae3109aSBartosz Golaszewski 
17830ae3109aSBartosz Golaszewski 	seq_printf(out, "gpio-chip:\t%s\n", dev_name(dev));
17840ae3109aSBartosz Golaszewski 
17850ae3109aSBartosz Golaszewski 	for (i = 0; i < lr->num_lines; i++)
17860ae3109aSBartosz Golaszewski 		seq_printf(out, "gpio-line:\t%d\n",
17870ae3109aSBartosz Golaszewski 			   gpio_chip_hwgpio(lr->lines[i].desc));
17880ae3109aSBartosz Golaszewski }
17890ae3109aSBartosz Golaszewski #endif
17900ae3109aSBartosz Golaszewski 
17913c0d9c63SKent Gibson static const struct file_operations line_fileops = {
17923c0d9c63SKent Gibson 	.release = linereq_release,
179373e03419SKent Gibson 	.read = linereq_read,
179473e03419SKent Gibson 	.poll = linereq_poll,
17953c0d9c63SKent Gibson 	.owner = THIS_MODULE,
17963c0d9c63SKent Gibson 	.llseek = noop_llseek,
17973c0d9c63SKent Gibson 	.unlocked_ioctl = linereq_ioctl,
17983c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
17993c0d9c63SKent Gibson 	.compat_ioctl = linereq_ioctl_compat,
18003c0d9c63SKent Gibson #endif
18010ae3109aSBartosz Golaszewski #ifdef CONFIG_PROC_FS
18020ae3109aSBartosz Golaszewski 	.show_fdinfo = linereq_show_fdinfo,
18030ae3109aSBartosz Golaszewski #endif
18043c0d9c63SKent Gibson };
18053c0d9c63SKent Gibson 
18063c0d9c63SKent Gibson static int linereq_create(struct gpio_device *gdev, void __user *ip)
18073c0d9c63SKent Gibson {
18083c0d9c63SKent Gibson 	struct gpio_v2_line_request ulr;
18093c0d9c63SKent Gibson 	struct gpio_v2_line_config *lc;
18103c0d9c63SKent Gibson 	struct linereq *lr;
18113c0d9c63SKent Gibson 	struct file *file;
1812b1a92e94SKent Gibson 	u64 flags, edflags;
18133c0d9c63SKent Gibson 	unsigned int i;
18143c0d9c63SKent Gibson 	int fd, ret;
18153c0d9c63SKent Gibson 
18163c0d9c63SKent Gibson 	if (copy_from_user(&ulr, ip, sizeof(ulr)))
18173c0d9c63SKent Gibson 		return -EFAULT;
18183c0d9c63SKent Gibson 
18193c0d9c63SKent Gibson 	if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
18203c0d9c63SKent Gibson 		return -EINVAL;
18213c0d9c63SKent Gibson 
18223c0d9c63SKent Gibson 	if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
18233c0d9c63SKent Gibson 		return -EINVAL;
18243c0d9c63SKent Gibson 
18253c0d9c63SKent Gibson 	lc = &ulr.config;
18263c0d9c63SKent Gibson 	ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
18273c0d9c63SKent Gibson 	if (ret)
18283c0d9c63SKent Gibson 		return ret;
18293c0d9c63SKent Gibson 
18303c0d9c63SKent Gibson 	lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
18313c0d9c63SKent Gibson 	if (!lr)
18323c0d9c63SKent Gibson 		return -ENOMEM;
18333c0d9c63SKent Gibson 
1834dc0989e3SAndy Shevchenko 	lr->gdev = gpio_device_get(gdev);
18353c0d9c63SKent Gibson 
183665cff704SKent Gibson 	for (i = 0; i < ulr.num_lines; i++) {
183773e03419SKent Gibson 		lr->lines[i].req = lr;
183865cff704SKent Gibson 		WRITE_ONCE(lr->lines[i].sw_debounced, 0);
183965cff704SKent Gibson 		INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
184065cff704SKent Gibson 	}
184173e03419SKent Gibson 
1842f188ac12SKent Gibson 	if (ulr.consumer[0] != '\0') {
18433c0d9c63SKent Gibson 		/* label is only initialized if consumer is set */
1844f188ac12SKent Gibson 		lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1845f188ac12SKent Gibson 				     GFP_KERNEL);
18463c0d9c63SKent Gibson 		if (!lr->label) {
18473c0d9c63SKent Gibson 			ret = -ENOMEM;
18483c0d9c63SKent Gibson 			goto out_free_linereq;
18493c0d9c63SKent Gibson 		}
18503c0d9c63SKent Gibson 	}
18513c0d9c63SKent Gibson 
1852a54756cbSKent Gibson 	mutex_init(&lr->config_mutex);
185373e03419SKent Gibson 	init_waitqueue_head(&lr->wait);
185473e03419SKent Gibson 	lr->event_buffer_size = ulr.event_buffer_size;
185573e03419SKent Gibson 	if (lr->event_buffer_size == 0)
185673e03419SKent Gibson 		lr->event_buffer_size = ulr.num_lines * 16;
185773e03419SKent Gibson 	else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
185873e03419SKent Gibson 		lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
185973e03419SKent Gibson 
186073e03419SKent Gibson 	atomic_set(&lr->seqno, 0);
18613c0d9c63SKent Gibson 	lr->num_lines = ulr.num_lines;
18623c0d9c63SKent Gibson 
18633c0d9c63SKent Gibson 	/* Request each GPIO */
18643c0d9c63SKent Gibson 	for (i = 0; i < ulr.num_lines; i++) {
18653c0d9c63SKent Gibson 		u32 offset = ulr.offsets[i];
18663c0d9c63SKent Gibson 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
18673c0d9c63SKent Gibson 
18683c0d9c63SKent Gibson 		if (IS_ERR(desc)) {
18693c0d9c63SKent Gibson 			ret = PTR_ERR(desc);
18703c0d9c63SKent Gibson 			goto out_free_linereq;
18713c0d9c63SKent Gibson 		}
18723c0d9c63SKent Gibson 
187395a4eed7SAndy Shevchenko 		ret = gpiod_request_user(desc, lr->label);
18743c0d9c63SKent Gibson 		if (ret)
18753c0d9c63SKent Gibson 			goto out_free_linereq;
18763c0d9c63SKent Gibson 
18773c0d9c63SKent Gibson 		lr->lines[i].desc = desc;
18783c0d9c63SKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
18793c0d9c63SKent Gibson 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
18803c0d9c63SKent Gibson 
18813c0d9c63SKent Gibson 		ret = gpiod_set_transitory(desc, false);
18823c0d9c63SKent Gibson 		if (ret < 0)
18833c0d9c63SKent Gibson 			goto out_free_linereq;
18843c0d9c63SKent Gibson 
1885b1a92e94SKent Gibson 		edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
18863c0d9c63SKent Gibson 		/*
18873c0d9c63SKent Gibson 		 * Lines have to be requested explicitly for input
18883c0d9c63SKent Gibson 		 * or output, else the line will be treated "as is".
18893c0d9c63SKent Gibson 		 */
18903c0d9c63SKent Gibson 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
18913c0d9c63SKent Gibson 			int val = gpio_v2_line_config_output_value(lc, i);
18923c0d9c63SKent Gibson 
18933c0d9c63SKent Gibson 			ret = gpiod_direction_output(desc, val);
18943c0d9c63SKent Gibson 			if (ret)
18953c0d9c63SKent Gibson 				goto out_free_linereq;
18963c0d9c63SKent Gibson 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
18973c0d9c63SKent Gibson 			ret = gpiod_direction_input(desc);
18983c0d9c63SKent Gibson 			if (ret)
18993c0d9c63SKent Gibson 				goto out_free_linereq;
190073e03419SKent Gibson 
190165cff704SKent Gibson 			ret = edge_detector_setup(&lr->lines[i], lc, i,
1902b1a92e94SKent Gibson 						  edflags);
190373e03419SKent Gibson 			if (ret)
190473e03419SKent Gibson 				goto out_free_linereq;
19053c0d9c63SKent Gibson 		}
19063c0d9c63SKent Gibson 
1907b1a92e94SKent Gibson 		lr->lines[i].edflags = edflags;
1908b1a92e94SKent Gibson 
19099ce4ed5bSBartosz Golaszewski 		gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
19103c0d9c63SKent Gibson 
19113c0d9c63SKent Gibson 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
19123c0d9c63SKent Gibson 			offset);
19133c0d9c63SKent Gibson 	}
19143c0d9c63SKent Gibson 
1915a0dda508SBartosz Golaszewski 	lr->device_unregistered_nb.notifier_call = linereq_unregistered_notify;
1916a0dda508SBartosz Golaszewski 	ret = blocking_notifier_chain_register(&gdev->device_notifier,
1917a0dda508SBartosz Golaszewski 					       &lr->device_unregistered_nb);
1918a0dda508SBartosz Golaszewski 	if (ret)
1919a0dda508SBartosz Golaszewski 		goto out_free_linereq;
1920a0dda508SBartosz Golaszewski 
19213c0d9c63SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
19223c0d9c63SKent Gibson 	if (fd < 0) {
19233c0d9c63SKent Gibson 		ret = fd;
19243c0d9c63SKent Gibson 		goto out_free_linereq;
19253c0d9c63SKent Gibson 	}
19263c0d9c63SKent Gibson 
19273c0d9c63SKent Gibson 	file = anon_inode_getfile("gpio-line", &line_fileops, lr,
19283c0d9c63SKent Gibson 				  O_RDONLY | O_CLOEXEC);
19293c0d9c63SKent Gibson 	if (IS_ERR(file)) {
19303c0d9c63SKent Gibson 		ret = PTR_ERR(file);
19313c0d9c63SKent Gibson 		goto out_put_unused_fd;
19323c0d9c63SKent Gibson 	}
19333c0d9c63SKent Gibson 
19343c0d9c63SKent Gibson 	ulr.fd = fd;
19353c0d9c63SKent Gibson 	if (copy_to_user(ip, &ulr, sizeof(ulr))) {
19363c0d9c63SKent Gibson 		/*
19373c0d9c63SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
19383c0d9c63SKent Gibson 		 * the regular error cleanup path here.
19393c0d9c63SKent Gibson 		 */
19403c0d9c63SKent Gibson 		fput(file);
19413c0d9c63SKent Gibson 		put_unused_fd(fd);
19423c0d9c63SKent Gibson 		return -EFAULT;
19433c0d9c63SKent Gibson 	}
19443c0d9c63SKent Gibson 
19453c0d9c63SKent Gibson 	fd_install(fd, file);
19463c0d9c63SKent Gibson 
19473c0d9c63SKent Gibson 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
19483c0d9c63SKent Gibson 		lr->num_lines);
19493c0d9c63SKent Gibson 
19503c0d9c63SKent Gibson 	return 0;
19513c0d9c63SKent Gibson 
19523c0d9c63SKent Gibson out_put_unused_fd:
19533c0d9c63SKent Gibson 	put_unused_fd(fd);
19543c0d9c63SKent Gibson out_free_linereq:
19553c0d9c63SKent Gibson 	linereq_free(lr);
19563c0d9c63SKent Gibson 	return ret;
19573c0d9c63SKent Gibson }
19583c0d9c63SKent Gibson 
19593c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1960925ca369SKent Gibson 
1961925ca369SKent Gibson /*
1962925ca369SKent Gibson  * GPIO line event management
1963925ca369SKent Gibson  */
1964925ca369SKent Gibson 
1965925ca369SKent Gibson /**
1966925ca369SKent Gibson  * struct lineevent_state - contains the state of a userspace event
1967925ca369SKent Gibson  * @gdev: the GPIO device the event pertains to
1968925ca369SKent Gibson  * @label: consumer label used to tag descriptors
1969925ca369SKent Gibson  * @desc: the GPIO descriptor held by this event
1970925ca369SKent Gibson  * @eflags: the event flags this line was requested with
1971925ca369SKent Gibson  * @irq: the interrupt that trigger in response to events on this GPIO
1972925ca369SKent Gibson  * @wait: wait queue that handles blocking reads of events
197391043f55SBartosz Golaszewski  * @device_unregistered_nb: notifier block for receiving gdev unregister events
1974925ca369SKent Gibson  * @events: KFIFO for the GPIO events
1975925ca369SKent Gibson  * @timestamp: cache for the timestamp storing it between hardirq
1976925ca369SKent Gibson  * and IRQ thread, used to bring the timestamp close to the actual
1977925ca369SKent Gibson  * event
1978925ca369SKent Gibson  */
1979925ca369SKent Gibson struct lineevent_state {
1980925ca369SKent Gibson 	struct gpio_device *gdev;
1981925ca369SKent Gibson 	const char *label;
1982925ca369SKent Gibson 	struct gpio_desc *desc;
1983925ca369SKent Gibson 	u32 eflags;
1984925ca369SKent Gibson 	int irq;
1985925ca369SKent Gibson 	wait_queue_head_t wait;
198691043f55SBartosz Golaszewski 	struct notifier_block device_unregistered_nb;
1987925ca369SKent Gibson 	DECLARE_KFIFO(events, struct gpioevent_data, 16);
1988925ca369SKent Gibson 	u64 timestamp;
1989925ca369SKent Gibson };
1990925ca369SKent Gibson 
1991925ca369SKent Gibson #define GPIOEVENT_REQUEST_VALID_FLAGS \
1992925ca369SKent Gibson 	(GPIOEVENT_REQUEST_RISING_EDGE | \
1993925ca369SKent Gibson 	GPIOEVENT_REQUEST_FALLING_EDGE)
1994925ca369SKent Gibson 
1995bdbbae24SBartosz Golaszewski static __poll_t lineevent_poll_unlocked(struct file *file,
1996925ca369SKent Gibson 					struct poll_table_struct *wait)
1997925ca369SKent Gibson {
199849bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1999925ca369SKent Gibson 	__poll_t events = 0;
2000925ca369SKent Gibson 
2001533aae7cSBartosz Golaszewski 	if (!le->gdev->chip)
2002533aae7cSBartosz Golaszewski 		return EPOLLHUP | EPOLLERR;
2003533aae7cSBartosz Golaszewski 
200449bc5279SKent Gibson 	poll_wait(file, &le->wait, wait);
2005925ca369SKent Gibson 
2006925ca369SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
2007925ca369SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
2008925ca369SKent Gibson 
2009925ca369SKent Gibson 	return events;
2010925ca369SKent Gibson }
2011925ca369SKent Gibson 
2012bdbbae24SBartosz Golaszewski static __poll_t lineevent_poll(struct file *file,
2013bdbbae24SBartosz Golaszewski 			       struct poll_table_struct *wait)
2014bdbbae24SBartosz Golaszewski {
2015bdbbae24SBartosz Golaszewski 	struct lineevent_state *le = file->private_data;
2016bdbbae24SBartosz Golaszewski 
2017bdbbae24SBartosz Golaszewski 	return call_poll_locked(file, wait, le->gdev, lineevent_poll_unlocked);
2018bdbbae24SBartosz Golaszewski }
2019bdbbae24SBartosz Golaszewski 
202091043f55SBartosz Golaszewski static int lineevent_unregistered_notify(struct notifier_block *nb,
202191043f55SBartosz Golaszewski 					 unsigned long action, void *data)
202291043f55SBartosz Golaszewski {
202391043f55SBartosz Golaszewski 	struct lineevent_state *le = container_of(nb, struct lineevent_state,
202491043f55SBartosz Golaszewski 						  device_unregistered_nb);
202591043f55SBartosz Golaszewski 
202691043f55SBartosz Golaszewski 	wake_up_poll(&le->wait, EPOLLIN | EPOLLERR);
202791043f55SBartosz Golaszewski 
202891043f55SBartosz Golaszewski 	return NOTIFY_OK;
202991043f55SBartosz Golaszewski }
203091043f55SBartosz Golaszewski 
20315ad284abSAndy Shevchenko struct compat_gpioeevent_data {
20325ad284abSAndy Shevchenko 	compat_u64	timestamp;
20335ad284abSAndy Shevchenko 	u32		id;
20345ad284abSAndy Shevchenko };
20355ad284abSAndy Shevchenko 
2036bdbbae24SBartosz Golaszewski static ssize_t lineevent_read_unlocked(struct file *file, char __user *buf,
2037bdbbae24SBartosz Golaszewski 				       size_t count, loff_t *f_ps)
2038925ca369SKent Gibson {
203949bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
2040925ca369SKent Gibson 	struct gpioevent_data ge;
2041925ca369SKent Gibson 	ssize_t bytes_read = 0;
20425ad284abSAndy Shevchenko 	ssize_t ge_size;
2043925ca369SKent Gibson 	int ret;
2044925ca369SKent Gibson 
2045533aae7cSBartosz Golaszewski 	if (!le->gdev->chip)
2046533aae7cSBartosz Golaszewski 		return -ENODEV;
2047533aae7cSBartosz Golaszewski 
20485ad284abSAndy Shevchenko 	/*
20495ad284abSAndy Shevchenko 	 * When compatible system call is being used the struct gpioevent_data,
20505ad284abSAndy Shevchenko 	 * in case of at least ia32, has different size due to the alignment
20515ad284abSAndy Shevchenko 	 * differences. Because we have first member 64 bits followed by one of
20525ad284abSAndy Shevchenko 	 * 32 bits there is no gap between them. The only difference is the
20535ad284abSAndy Shevchenko 	 * padding at the end of the data structure. Hence, we calculate the
20545ad284abSAndy Shevchenko 	 * actual sizeof() and pass this as an argument to copy_to_user() to
20555ad284abSAndy Shevchenko 	 * drop unneeded bytes from the output.
20565ad284abSAndy Shevchenko 	 */
2057163d1719SAndy Shevchenko 	if (compat_need_64bit_alignment_fixup())
2058163d1719SAndy Shevchenko 		ge_size = sizeof(struct compat_gpioeevent_data);
2059163d1719SAndy Shevchenko 	else
2060163d1719SAndy Shevchenko 		ge_size = sizeof(struct gpioevent_data);
20615ad284abSAndy Shevchenko 	if (count < ge_size)
2062925ca369SKent Gibson 		return -EINVAL;
2063925ca369SKent Gibson 
2064925ca369SKent Gibson 	do {
2065925ca369SKent Gibson 		spin_lock(&le->wait.lock);
2066925ca369SKent Gibson 		if (kfifo_is_empty(&le->events)) {
2067925ca369SKent Gibson 			if (bytes_read) {
2068925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
2069925ca369SKent Gibson 				return bytes_read;
2070925ca369SKent Gibson 			}
2071925ca369SKent Gibson 
207249bc5279SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
2073925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
2074925ca369SKent Gibson 				return -EAGAIN;
2075925ca369SKent Gibson 			}
2076925ca369SKent Gibson 
2077925ca369SKent Gibson 			ret = wait_event_interruptible_locked(le->wait,
2078925ca369SKent Gibson 					!kfifo_is_empty(&le->events));
2079925ca369SKent Gibson 			if (ret) {
2080925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
2081925ca369SKent Gibson 				return ret;
2082925ca369SKent Gibson 			}
2083925ca369SKent Gibson 		}
2084925ca369SKent Gibson 
2085925ca369SKent Gibson 		ret = kfifo_out(&le->events, &ge, 1);
2086925ca369SKent Gibson 		spin_unlock(&le->wait.lock);
2087925ca369SKent Gibson 		if (ret != 1) {
2088925ca369SKent Gibson 			/*
2089925ca369SKent Gibson 			 * This should never happen - we were holding the lock
2090925ca369SKent Gibson 			 * from the moment we learned the fifo is no longer
2091925ca369SKent Gibson 			 * empty until now.
2092925ca369SKent Gibson 			 */
2093925ca369SKent Gibson 			ret = -EIO;
2094925ca369SKent Gibson 			break;
2095925ca369SKent Gibson 		}
2096925ca369SKent Gibson 
20975ad284abSAndy Shevchenko 		if (copy_to_user(buf + bytes_read, &ge, ge_size))
2098925ca369SKent Gibson 			return -EFAULT;
20995ad284abSAndy Shevchenko 		bytes_read += ge_size;
21005ad284abSAndy Shevchenko 	} while (count >= bytes_read + ge_size);
2101925ca369SKent Gibson 
2102925ca369SKent Gibson 	return bytes_read;
2103925ca369SKent Gibson }
2104925ca369SKent Gibson 
2105bdbbae24SBartosz Golaszewski static ssize_t lineevent_read(struct file *file, char __user *buf,
2106bdbbae24SBartosz Golaszewski 			      size_t count, loff_t *f_ps)
2107bdbbae24SBartosz Golaszewski {
2108bdbbae24SBartosz Golaszewski 	struct lineevent_state *le = file->private_data;
2109bdbbae24SBartosz Golaszewski 
2110bdbbae24SBartosz Golaszewski 	return call_read_locked(file, buf, count, f_ps, le->gdev,
2111bdbbae24SBartosz Golaszewski 				lineevent_read_unlocked);
2112bdbbae24SBartosz Golaszewski }
2113bdbbae24SBartosz Golaszewski 
211446824272SKent Gibson static void lineevent_free(struct lineevent_state *le)
2115925ca369SKent Gibson {
211691043f55SBartosz Golaszewski 	if (le->device_unregistered_nb.notifier_call)
211791043f55SBartosz Golaszewski 		blocking_notifier_chain_unregister(&le->gdev->device_notifier,
211891043f55SBartosz Golaszewski 						   &le->device_unregistered_nb);
211946824272SKent Gibson 	if (le->irq)
212021bc9b15SBartosz Golaszewski 		free_irq_label(free_irq(le->irq, le));
212146824272SKent Gibson 	if (le->desc)
2122925ca369SKent Gibson 		gpiod_free(le->desc);
2123925ca369SKent Gibson 	kfree(le->label);
2124dc0989e3SAndy Shevchenko 	gpio_device_put(le->gdev);
2125925ca369SKent Gibson 	kfree(le);
212646824272SKent Gibson }
212746824272SKent Gibson 
212846824272SKent Gibson static int lineevent_release(struct inode *inode, struct file *file)
212946824272SKent Gibson {
213046824272SKent Gibson 	lineevent_free(file->private_data);
2131925ca369SKent Gibson 	return 0;
2132925ca369SKent Gibson }
2133925ca369SKent Gibson 
2134bdbbae24SBartosz Golaszewski static long lineevent_ioctl_unlocked(struct file *file, unsigned int cmd,
2135925ca369SKent Gibson 				     unsigned long arg)
2136925ca369SKent Gibson {
213749bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
2138925ca369SKent Gibson 	void __user *ip = (void __user *)arg;
2139925ca369SKent Gibson 	struct gpiohandle_data ghd;
2140925ca369SKent Gibson 
2141533aae7cSBartosz Golaszewski 	if (!le->gdev->chip)
2142533aae7cSBartosz Golaszewski 		return -ENODEV;
2143533aae7cSBartosz Golaszewski 
2144925ca369SKent Gibson 	/*
2145925ca369SKent Gibson 	 * We can get the value for an event line but not set it,
2146925ca369SKent Gibson 	 * because it is input by definition.
2147925ca369SKent Gibson 	 */
2148925ca369SKent Gibson 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
2149925ca369SKent Gibson 		int val;
2150925ca369SKent Gibson 
2151925ca369SKent Gibson 		memset(&ghd, 0, sizeof(ghd));
2152925ca369SKent Gibson 
2153925ca369SKent Gibson 		val = gpiod_get_value_cansleep(le->desc);
2154925ca369SKent Gibson 		if (val < 0)
2155925ca369SKent Gibson 			return val;
2156925ca369SKent Gibson 		ghd.values[0] = val;
2157925ca369SKent Gibson 
2158925ca369SKent Gibson 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
2159925ca369SKent Gibson 			return -EFAULT;
2160925ca369SKent Gibson 
2161925ca369SKent Gibson 		return 0;
2162925ca369SKent Gibson 	}
2163925ca369SKent Gibson 	return -EINVAL;
2164925ca369SKent Gibson }
2165925ca369SKent Gibson 
2166bdbbae24SBartosz Golaszewski static long lineevent_ioctl(struct file *file, unsigned int cmd,
2167bdbbae24SBartosz Golaszewski 			    unsigned long arg)
2168bdbbae24SBartosz Golaszewski {
2169bdbbae24SBartosz Golaszewski 	struct lineevent_state *le = file->private_data;
2170bdbbae24SBartosz Golaszewski 
2171bdbbae24SBartosz Golaszewski 	return call_ioctl_locked(file, cmd, arg, le->gdev,
2172bdbbae24SBartosz Golaszewski 				 lineevent_ioctl_unlocked);
2173bdbbae24SBartosz Golaszewski }
2174bdbbae24SBartosz Golaszewski 
2175925ca369SKent Gibson #ifdef CONFIG_COMPAT
217649bc5279SKent Gibson static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
2177925ca369SKent Gibson 				   unsigned long arg)
2178925ca369SKent Gibson {
217949bc5279SKent Gibson 	return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2180925ca369SKent Gibson }
2181925ca369SKent Gibson #endif
2182925ca369SKent Gibson 
2183925ca369SKent Gibson static const struct file_operations lineevent_fileops = {
2184925ca369SKent Gibson 	.release = lineevent_release,
2185925ca369SKent Gibson 	.read = lineevent_read,
2186925ca369SKent Gibson 	.poll = lineevent_poll,
2187925ca369SKent Gibson 	.owner = THIS_MODULE,
2188925ca369SKent Gibson 	.llseek = noop_llseek,
2189925ca369SKent Gibson 	.unlocked_ioctl = lineevent_ioctl,
2190925ca369SKent Gibson #ifdef CONFIG_COMPAT
2191925ca369SKent Gibson 	.compat_ioctl = lineevent_ioctl_compat,
2192925ca369SKent Gibson #endif
2193925ca369SKent Gibson };
2194925ca369SKent Gibson 
2195925ca369SKent Gibson static irqreturn_t lineevent_irq_thread(int irq, void *p)
2196925ca369SKent Gibson {
2197925ca369SKent Gibson 	struct lineevent_state *le = p;
2198925ca369SKent Gibson 	struct gpioevent_data ge;
2199925ca369SKent Gibson 	int ret;
2200925ca369SKent Gibson 
2201925ca369SKent Gibson 	/* Do not leak kernel stack to userspace */
2202925ca369SKent Gibson 	memset(&ge, 0, sizeof(ge));
2203925ca369SKent Gibson 
2204925ca369SKent Gibson 	/*
2205925ca369SKent Gibson 	 * We may be running from a nested threaded interrupt in which case
2206925ca369SKent Gibson 	 * we didn't get the timestamp from lineevent_irq_handler().
2207925ca369SKent Gibson 	 */
2208925ca369SKent Gibson 	if (!le->timestamp)
2209925ca369SKent Gibson 		ge.timestamp = ktime_get_ns();
2210925ca369SKent Gibson 	else
2211925ca369SKent Gibson 		ge.timestamp = le->timestamp;
2212925ca369SKent Gibson 
2213925ca369SKent Gibson 	if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
2214925ca369SKent Gibson 	    && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
2215925ca369SKent Gibson 		int level = gpiod_get_value_cansleep(le->desc);
2216925ca369SKent Gibson 
2217925ca369SKent Gibson 		if (level)
2218925ca369SKent Gibson 			/* Emit low-to-high event */
2219925ca369SKent Gibson 			ge.id = GPIOEVENT_EVENT_RISING_EDGE;
2220925ca369SKent Gibson 		else
2221925ca369SKent Gibson 			/* Emit high-to-low event */
2222925ca369SKent Gibson 			ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
2223925ca369SKent Gibson 	} else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
2224925ca369SKent Gibson 		/* Emit low-to-high event */
2225925ca369SKent Gibson 		ge.id = GPIOEVENT_EVENT_RISING_EDGE;
2226925ca369SKent Gibson 	} else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
2227925ca369SKent Gibson 		/* Emit high-to-low event */
2228925ca369SKent Gibson 		ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
2229925ca369SKent Gibson 	} else {
2230925ca369SKent Gibson 		return IRQ_NONE;
2231925ca369SKent Gibson 	}
2232925ca369SKent Gibson 
2233925ca369SKent Gibson 	ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
2234925ca369SKent Gibson 					    1, &le->wait.lock);
2235925ca369SKent Gibson 	if (ret)
2236925ca369SKent Gibson 		wake_up_poll(&le->wait, EPOLLIN);
2237925ca369SKent Gibson 	else
2238925ca369SKent Gibson 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
2239925ca369SKent Gibson 
2240925ca369SKent Gibson 	return IRQ_HANDLED;
2241925ca369SKent Gibson }
2242925ca369SKent Gibson 
2243925ca369SKent Gibson static irqreturn_t lineevent_irq_handler(int irq, void *p)
2244925ca369SKent Gibson {
2245925ca369SKent Gibson 	struct lineevent_state *le = p;
2246925ca369SKent Gibson 
2247925ca369SKent Gibson 	/*
2248925ca369SKent Gibson 	 * Just store the timestamp in hardirq context so we get it as
2249925ca369SKent Gibson 	 * close in time as possible to the actual event.
2250925ca369SKent Gibson 	 */
2251925ca369SKent Gibson 	le->timestamp = ktime_get_ns();
2252925ca369SKent Gibson 
2253925ca369SKent Gibson 	return IRQ_WAKE_THREAD;
2254925ca369SKent Gibson }
2255925ca369SKent Gibson 
2256925ca369SKent Gibson static int lineevent_create(struct gpio_device *gdev, void __user *ip)
2257925ca369SKent Gibson {
2258925ca369SKent Gibson 	struct gpioevent_request eventreq;
2259925ca369SKent Gibson 	struct lineevent_state *le;
2260925ca369SKent Gibson 	struct gpio_desc *desc;
2261925ca369SKent Gibson 	struct file *file;
2262925ca369SKent Gibson 	u32 offset;
2263925ca369SKent Gibson 	u32 lflags;
2264925ca369SKent Gibson 	u32 eflags;
2265925ca369SKent Gibson 	int fd;
2266925ca369SKent Gibson 	int ret;
226746824272SKent Gibson 	int irq, irqflags = 0;
226821bc9b15SBartosz Golaszewski 	char *label;
2269925ca369SKent Gibson 
2270925ca369SKent Gibson 	if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
2271925ca369SKent Gibson 		return -EFAULT;
2272925ca369SKent Gibson 
2273925ca369SKent Gibson 	offset = eventreq.lineoffset;
2274925ca369SKent Gibson 	lflags = eventreq.handleflags;
2275925ca369SKent Gibson 	eflags = eventreq.eventflags;
2276925ca369SKent Gibson 
2277925ca369SKent Gibson 	desc = gpiochip_get_desc(gdev->chip, offset);
2278925ca369SKent Gibson 	if (IS_ERR(desc))
2279925ca369SKent Gibson 		return PTR_ERR(desc);
2280925ca369SKent Gibson 
2281925ca369SKent Gibson 	/* Return an error if a unknown flag is set */
2282925ca369SKent Gibson 	if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
2283925ca369SKent Gibson 	    (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
2284925ca369SKent Gibson 		return -EINVAL;
2285925ca369SKent Gibson 
2286925ca369SKent Gibson 	/* This is just wrong: we don't look for events on output lines */
2287925ca369SKent Gibson 	if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
2288925ca369SKent Gibson 	    (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
2289925ca369SKent Gibson 	    (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
2290925ca369SKent Gibson 		return -EINVAL;
2291925ca369SKent Gibson 
2292925ca369SKent Gibson 	/* Only one bias flag can be set. */
2293925ca369SKent Gibson 	if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
2294925ca369SKent Gibson 	     (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
2295925ca369SKent Gibson 			GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
2296925ca369SKent Gibson 	    ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
2297925ca369SKent Gibson 	     (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
2298925ca369SKent Gibson 		return -EINVAL;
2299925ca369SKent Gibson 
2300925ca369SKent Gibson 	le = kzalloc(sizeof(*le), GFP_KERNEL);
2301925ca369SKent Gibson 	if (!le)
2302925ca369SKent Gibson 		return -ENOMEM;
2303dc0989e3SAndy Shevchenko 	le->gdev = gpio_device_get(gdev);
2304925ca369SKent Gibson 
2305f188ac12SKent Gibson 	if (eventreq.consumer_label[0] != '\0') {
2306f188ac12SKent Gibson 		/* label is only initialized if consumer_label is set */
2307f188ac12SKent Gibson 		le->label = kstrndup(eventreq.consumer_label,
2308f188ac12SKent Gibson 				     sizeof(eventreq.consumer_label) - 1,
2309925ca369SKent Gibson 				     GFP_KERNEL);
2310925ca369SKent Gibson 		if (!le->label) {
2311925ca369SKent Gibson 			ret = -ENOMEM;
2312925ca369SKent Gibson 			goto out_free_le;
2313925ca369SKent Gibson 		}
2314925ca369SKent Gibson 	}
2315925ca369SKent Gibson 
231695a4eed7SAndy Shevchenko 	ret = gpiod_request_user(desc, le->label);
2317925ca369SKent Gibson 	if (ret)
231846824272SKent Gibson 		goto out_free_le;
2319925ca369SKent Gibson 	le->desc = desc;
2320925ca369SKent Gibson 	le->eflags = eflags;
2321925ca369SKent Gibson 
2322c274b58aSKent Gibson 	linehandle_flags_to_desc_flags(lflags, &desc->flags);
2323925ca369SKent Gibson 
2324925ca369SKent Gibson 	ret = gpiod_direction_input(desc);
2325925ca369SKent Gibson 	if (ret)
232646824272SKent Gibson 		goto out_free_le;
2327925ca369SKent Gibson 
23289ce4ed5bSBartosz Golaszewski 	gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
2329925ca369SKent Gibson 
233046824272SKent Gibson 	irq = gpiod_to_irq(desc);
233146824272SKent Gibson 	if (irq <= 0) {
2332925ca369SKent Gibson 		ret = -ENODEV;
233346824272SKent Gibson 		goto out_free_le;
2334925ca369SKent Gibson 	}
2335925ca369SKent Gibson 
2336925ca369SKent Gibson 	if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
2337925ca369SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
2338925ca369SKent Gibson 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
2339925ca369SKent Gibson 	if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
2340925ca369SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
2341925ca369SKent Gibson 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
2342925ca369SKent Gibson 	irqflags |= IRQF_ONESHOT;
2343925ca369SKent Gibson 
2344925ca369SKent Gibson 	INIT_KFIFO(le->events);
2345925ca369SKent Gibson 	init_waitqueue_head(&le->wait);
2346925ca369SKent Gibson 
234791043f55SBartosz Golaszewski 	le->device_unregistered_nb.notifier_call = lineevent_unregistered_notify;
234891043f55SBartosz Golaszewski 	ret = blocking_notifier_chain_register(&gdev->device_notifier,
234991043f55SBartosz Golaszewski 					       &le->device_unregistered_nb);
235091043f55SBartosz Golaszewski 	if (ret)
235191043f55SBartosz Golaszewski 		goto out_free_le;
235291043f55SBartosz Golaszewski 
235321bc9b15SBartosz Golaszewski 	label = make_irq_label(le->label);
2354d9f0804aSBartosz Golaszewski 	if (IS_ERR(label)) {
2355d9f0804aSBartosz Golaszewski 		ret = PTR_ERR(label);
235621bc9b15SBartosz Golaszewski 		goto out_free_le;
235721bc9b15SBartosz Golaszewski 	}
235821bc9b15SBartosz Golaszewski 
2359925ca369SKent Gibson 	/* Request a thread to read the events */
236069bef19dSMeng Li 	ret = request_threaded_irq(irq,
2361925ca369SKent Gibson 				   lineevent_irq_handler,
2362925ca369SKent Gibson 				   lineevent_irq_thread,
2363925ca369SKent Gibson 				   irqflags,
236421bc9b15SBartosz Golaszewski 				   label,
2365925ca369SKent Gibson 				   le);
236621bc9b15SBartosz Golaszewski 	if (ret) {
236721bc9b15SBartosz Golaszewski 		free_irq_label(label);
236846824272SKent Gibson 		goto out_free_le;
236921bc9b15SBartosz Golaszewski 	}
2370925ca369SKent Gibson 
237169bef19dSMeng Li 	le->irq = irq;
237269bef19dSMeng Li 
2373925ca369SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
2374925ca369SKent Gibson 	if (fd < 0) {
2375925ca369SKent Gibson 		ret = fd;
237646824272SKent Gibson 		goto out_free_le;
2377925ca369SKent Gibson 	}
2378925ca369SKent Gibson 
2379925ca369SKent Gibson 	file = anon_inode_getfile("gpio-event",
2380925ca369SKent Gibson 				  &lineevent_fileops,
2381925ca369SKent Gibson 				  le,
2382925ca369SKent Gibson 				  O_RDONLY | O_CLOEXEC);
2383925ca369SKent Gibson 	if (IS_ERR(file)) {
2384925ca369SKent Gibson 		ret = PTR_ERR(file);
2385925ca369SKent Gibson 		goto out_put_unused_fd;
2386925ca369SKent Gibson 	}
2387925ca369SKent Gibson 
2388925ca369SKent Gibson 	eventreq.fd = fd;
2389925ca369SKent Gibson 	if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
2390925ca369SKent Gibson 		/*
2391925ca369SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
2392925ca369SKent Gibson 		 * the regular error cleanup path here.
2393925ca369SKent Gibson 		 */
2394925ca369SKent Gibson 		fput(file);
2395925ca369SKent Gibson 		put_unused_fd(fd);
2396925ca369SKent Gibson 		return -EFAULT;
2397925ca369SKent Gibson 	}
2398925ca369SKent Gibson 
2399925ca369SKent Gibson 	fd_install(fd, file);
2400925ca369SKent Gibson 
2401925ca369SKent Gibson 	return 0;
2402925ca369SKent Gibson 
2403925ca369SKent Gibson out_put_unused_fd:
2404925ca369SKent Gibson 	put_unused_fd(fd);
2405925ca369SKent Gibson out_free_le:
240646824272SKent Gibson 	lineevent_free(le);
2407925ca369SKent Gibson 	return ret;
2408925ca369SKent Gibson }
2409925ca369SKent Gibson 
2410aad95584SKent Gibson static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
2411aad95584SKent Gibson 				    struct gpioline_info *info_v1)
2412aad95584SKent Gibson {
2413aad95584SKent Gibson 	u64 flagsv2 = info_v2->flags;
2414aad95584SKent Gibson 
2415aad95584SKent Gibson 	memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
2416aad95584SKent Gibson 	memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
2417aad95584SKent Gibson 	info_v1->line_offset = info_v2->offset;
2418aad95584SKent Gibson 	info_v1->flags = 0;
2419aad95584SKent Gibson 
2420aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
2421aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_KERNEL;
2422aad95584SKent Gibson 
2423aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
2424aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
2425aad95584SKent Gibson 
2426aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
2427aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
2428aad95584SKent Gibson 
2429aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
2430aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
2431aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
2432aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
2433aad95584SKent Gibson 
2434aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
2435aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
2436aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
2437aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
2438aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
2439aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
2440aad95584SKent Gibson }
2441aad95584SKent Gibson 
2442aad95584SKent Gibson static void gpio_v2_line_info_changed_to_v1(
2443aad95584SKent Gibson 		struct gpio_v2_line_info_changed *lic_v2,
2444aad95584SKent Gibson 		struct gpioline_info_changed *lic_v1)
2445aad95584SKent Gibson {
2446cb8f63b8SGabriel Knezek 	memset(lic_v1, 0, sizeof(*lic_v1));
2447aad95584SKent Gibson 	gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
2448aad95584SKent Gibson 	lic_v1->timestamp = lic_v2->timestamp_ns;
2449aad95584SKent Gibson 	lic_v1->event_type = lic_v2->event_type;
2450aad95584SKent Gibson }
2451aad95584SKent Gibson 
24523c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
24533c0d9c63SKent Gibson 
2454925ca369SKent Gibson static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
2455aad95584SKent Gibson 				  struct gpio_v2_line_info *info)
2456925ca369SKent Gibson {
2457925ca369SKent Gibson 	struct gpio_chip *gc = desc->gdev->chip;
2458925ca369SKent Gibson 	bool ok_for_pinctrl;
2459925ca369SKent Gibson 	unsigned long flags;
2460925ca369SKent Gibson 
246169e4e136SKent Gibson 	memset(info, 0, sizeof(*info));
2462aad95584SKent Gibson 	info->offset = gpio_chip_hwgpio(desc);
2463925ca369SKent Gibson 
2464925ca369SKent Gibson 	/*
2465925ca369SKent Gibson 	 * This function takes a mutex so we must check this before taking
2466925ca369SKent Gibson 	 * the spinlock.
2467925ca369SKent Gibson 	 *
2468925ca369SKent Gibson 	 * FIXME: find a non-racy way to retrieve this information. Maybe a
2469925ca369SKent Gibson 	 * lock common to both frameworks?
2470925ca369SKent Gibson 	 */
2471925ca369SKent Gibson 	ok_for_pinctrl =
2472aad95584SKent Gibson 		pinctrl_gpio_can_use_line(gc->base + info->offset);
2473925ca369SKent Gibson 
2474925ca369SKent Gibson 	spin_lock_irqsave(&gpio_lock, flags);
2475925ca369SKent Gibson 
247669e4e136SKent Gibson 	if (desc->name)
247769e4e136SKent Gibson 		strscpy(info->name, desc->name, sizeof(info->name));
2478925ca369SKent Gibson 
247969e4e136SKent Gibson 	if (desc->label)
248069e4e136SKent Gibson 		strscpy(info->consumer, desc->label, sizeof(info->consumer));
2481925ca369SKent Gibson 
2482925ca369SKent Gibson 	/*
2483925ca369SKent Gibson 	 * Userspace only need to know that the kernel is using this GPIO so
2484925ca369SKent Gibson 	 * it can't use it.
2485925ca369SKent Gibson 	 */
2486925ca369SKent Gibson 	info->flags = 0;
2487925ca369SKent Gibson 	if (test_bit(FLAG_REQUESTED, &desc->flags) ||
2488925ca369SKent Gibson 	    test_bit(FLAG_IS_HOGGED, &desc->flags) ||
2489925ca369SKent Gibson 	    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
2490925ca369SKent Gibson 	    test_bit(FLAG_EXPORT, &desc->flags) ||
2491925ca369SKent Gibson 	    test_bit(FLAG_SYSFS, &desc->flags) ||
2492a0db197fSMarc Zyngier 	    !gpiochip_line_is_valid(gc, info->offset) ||
2493925ca369SKent Gibson 	    !ok_for_pinctrl)
2494aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_USED;
2495aad95584SKent Gibson 
2496925ca369SKent Gibson 	if (test_bit(FLAG_IS_OUT, &desc->flags))
2497aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
2498aad95584SKent Gibson 	else
2499aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_INPUT;
2500aad95584SKent Gibson 
2501925ca369SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2502aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
2503aad95584SKent Gibson 
2504925ca369SKent Gibson 	if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2505aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
2506925ca369SKent Gibson 	if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2507aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
2508aad95584SKent Gibson 
2509925ca369SKent Gibson 	if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2510aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
2511925ca369SKent Gibson 	if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2512aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
2513925ca369SKent Gibson 	if (test_bit(FLAG_PULL_UP, &desc->flags))
2514aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
2515925ca369SKent Gibson 
251673e03419SKent Gibson 	if (test_bit(FLAG_EDGE_RISING, &desc->flags))
251773e03419SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
251873e03419SKent Gibson 	if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
251973e03419SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
252073e03419SKent Gibson 
252126d060e4SKent Gibson 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
252226d060e4SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
25232068339aSDipen Patel 	else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags))
25242068339aSDipen Patel 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
252526d060e4SKent Gibson 
2526925ca369SKent Gibson 	spin_unlock_irqrestore(&gpio_lock, flags);
2527925ca369SKent Gibson }
2528925ca369SKent Gibson 
2529925ca369SKent Gibson struct gpio_chardev_data {
2530925ca369SKent Gibson 	struct gpio_device *gdev;
2531925ca369SKent Gibson 	wait_queue_head_t wait;
2532aad95584SKent Gibson 	DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
2533925ca369SKent Gibson 	struct notifier_block lineinfo_changed_nb;
2534d2e2586aSBartosz Golaszewski 	struct notifier_block device_unregistered_nb;
2535925ca369SKent Gibson 	unsigned long *watched_lines;
2536aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2537aad95584SKent Gibson 	atomic_t watch_abi_version;
2538aad95584SKent Gibson #endif
2539925ca369SKent Gibson };
2540925ca369SKent Gibson 
25412e202ad8SKent Gibson static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
25422e202ad8SKent Gibson {
25432e202ad8SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
25442e202ad8SKent Gibson 	struct gpiochip_info chipinfo;
25452e202ad8SKent Gibson 
25462e202ad8SKent Gibson 	memset(&chipinfo, 0, sizeof(chipinfo));
25472e202ad8SKent Gibson 
25482e202ad8SKent Gibson 	strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
25492e202ad8SKent Gibson 	strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
25502e202ad8SKent Gibson 	chipinfo.lines = gdev->ngpio;
25512e202ad8SKent Gibson 	if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
25522e202ad8SKent Gibson 		return -EFAULT;
25532e202ad8SKent Gibson 	return 0;
25542e202ad8SKent Gibson }
25552e202ad8SKent Gibson 
2556aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2557aad95584SKent Gibson /*
2558aad95584SKent Gibson  * returns 0 if the versions match, else the previously selected ABI version
2559aad95584SKent Gibson  */
2560aad95584SKent Gibson static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
2561aad95584SKent Gibson 				       unsigned int version)
2562aad95584SKent Gibson {
2563aad95584SKent Gibson 	int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
2564aad95584SKent Gibson 
2565aad95584SKent Gibson 	if (abiv == version)
2566aad95584SKent Gibson 		return 0;
2567aad95584SKent Gibson 
2568aad95584SKent Gibson 	return abiv;
2569aad95584SKent Gibson }
25702e202ad8SKent Gibson 
25712e202ad8SKent Gibson static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
25722e202ad8SKent Gibson 			   bool watch)
25732e202ad8SKent Gibson {
25742e202ad8SKent Gibson 	struct gpio_desc *desc;
25752e202ad8SKent Gibson 	struct gpioline_info lineinfo;
25762e202ad8SKent Gibson 	struct gpio_v2_line_info lineinfo_v2;
25772e202ad8SKent Gibson 
25782e202ad8SKent Gibson 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
25792e202ad8SKent Gibson 		return -EFAULT;
25802e202ad8SKent Gibson 
25812e202ad8SKent Gibson 	/* this doubles as a range check on line_offset */
25822e202ad8SKent Gibson 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset);
25832e202ad8SKent Gibson 	if (IS_ERR(desc))
25842e202ad8SKent Gibson 		return PTR_ERR(desc);
25852e202ad8SKent Gibson 
25862e202ad8SKent Gibson 	if (watch) {
25872e202ad8SKent Gibson 		if (lineinfo_ensure_abi_version(cdev, 1))
25882e202ad8SKent Gibson 			return -EPERM;
25892e202ad8SKent Gibson 
25902e202ad8SKent Gibson 		if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
25912e202ad8SKent Gibson 			return -EBUSY;
25922e202ad8SKent Gibson 	}
25932e202ad8SKent Gibson 
25942e202ad8SKent Gibson 	gpio_desc_to_lineinfo(desc, &lineinfo_v2);
25952e202ad8SKent Gibson 	gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
25962e202ad8SKent Gibson 
25972e202ad8SKent Gibson 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
25982e202ad8SKent Gibson 		if (watch)
25992e202ad8SKent Gibson 			clear_bit(lineinfo.line_offset, cdev->watched_lines);
26002e202ad8SKent Gibson 		return -EFAULT;
26012e202ad8SKent Gibson 	}
26022e202ad8SKent Gibson 
26032e202ad8SKent Gibson 	return 0;
26042e202ad8SKent Gibson }
2605aad95584SKent Gibson #endif
2606aad95584SKent Gibson 
2607aad95584SKent Gibson static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
2608aad95584SKent Gibson 			bool watch)
2609aad95584SKent Gibson {
2610aad95584SKent Gibson 	struct gpio_desc *desc;
2611aad95584SKent Gibson 	struct gpio_v2_line_info lineinfo;
2612aad95584SKent Gibson 
2613aad95584SKent Gibson 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2614aad95584SKent Gibson 		return -EFAULT;
2615aad95584SKent Gibson 
2616aad95584SKent Gibson 	if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2617aad95584SKent Gibson 		return -EINVAL;
2618aad95584SKent Gibson 
2619aad95584SKent Gibson 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2620aad95584SKent Gibson 	if (IS_ERR(desc))
2621aad95584SKent Gibson 		return PTR_ERR(desc);
2622aad95584SKent Gibson 
2623aad95584SKent Gibson 	if (watch) {
2624aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2625aad95584SKent Gibson 		if (lineinfo_ensure_abi_version(cdev, 2))
2626aad95584SKent Gibson 			return -EPERM;
2627aad95584SKent Gibson #endif
2628aad95584SKent Gibson 		if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2629aad95584SKent Gibson 			return -EBUSY;
2630aad95584SKent Gibson 	}
2631aad95584SKent Gibson 	gpio_desc_to_lineinfo(desc, &lineinfo);
2632309364bcSKent Gibson 	supinfo_to_lineinfo(desc, &lineinfo);
2633aad95584SKent Gibson 
2634aad95584SKent Gibson 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2635aad95584SKent Gibson 		if (watch)
2636aad95584SKent Gibson 			clear_bit(lineinfo.offset, cdev->watched_lines);
2637aad95584SKent Gibson 		return -EFAULT;
2638aad95584SKent Gibson 	}
2639aad95584SKent Gibson 
2640aad95584SKent Gibson 	return 0;
2641aad95584SKent Gibson }
2642aad95584SKent Gibson 
26432e202ad8SKent Gibson static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
2644925ca369SKent Gibson {
2645925ca369SKent Gibson 	__u32 offset;
2646925ca369SKent Gibson 
2647925ca369SKent Gibson 	if (copy_from_user(&offset, ip, sizeof(offset)))
2648925ca369SKent Gibson 		return -EFAULT;
2649925ca369SKent Gibson 
26501bf7ba40SKent Gibson 	if (offset >= cdev->gdev->ngpio)
26511bf7ba40SKent Gibson 		return -EINVAL;
2652925ca369SKent Gibson 
26531bf7ba40SKent Gibson 	if (!test_and_clear_bit(offset, cdev->watched_lines))
2654925ca369SKent Gibson 		return -EBUSY;
2655925ca369SKent Gibson 
2656925ca369SKent Gibson 	return 0;
2657925ca369SKent Gibson }
26582e202ad8SKent Gibson 
2659249cbac4SKent Gibson static long gpio_ioctl_unlocked(struct file *file, unsigned int cmd, unsigned long arg)
26602e202ad8SKent Gibson {
26612e202ad8SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
26622e202ad8SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
26632e202ad8SKent Gibson 	void __user *ip = (void __user *)arg;
26642e202ad8SKent Gibson 
26652e202ad8SKent Gibson 	/* We fail any subsequent ioctl():s when the chip is gone */
26662e202ad8SKent Gibson 	if (!gdev->chip)
26672e202ad8SKent Gibson 		return -ENODEV;
26682e202ad8SKent Gibson 
26692e202ad8SKent Gibson 	/* Fill in the struct and pass to userspace */
26701cef8b50SAndy Shevchenko 	switch (cmd) {
26711cef8b50SAndy Shevchenko 	case GPIO_GET_CHIPINFO_IOCTL:
26722e202ad8SKent Gibson 		return chipinfo_get(cdev, ip);
26732e202ad8SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
26741cef8b50SAndy Shevchenko 	case GPIO_GET_LINEHANDLE_IOCTL:
26752e202ad8SKent Gibson 		return linehandle_create(gdev, ip);
26761cef8b50SAndy Shevchenko 	case GPIO_GET_LINEEVENT_IOCTL:
26772e202ad8SKent Gibson 		return lineevent_create(gdev, ip);
26781cef8b50SAndy Shevchenko 	case GPIO_GET_LINEINFO_IOCTL:
26791cef8b50SAndy Shevchenko 		return lineinfo_get_v1(cdev, ip, false);
26801cef8b50SAndy Shevchenko 	case GPIO_GET_LINEINFO_WATCH_IOCTL:
26811cef8b50SAndy Shevchenko 		return lineinfo_get_v1(cdev, ip, true);
26822e202ad8SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
26831cef8b50SAndy Shevchenko 	case GPIO_V2_GET_LINEINFO_IOCTL:
26841cef8b50SAndy Shevchenko 		return lineinfo_get(cdev, ip, false);
26851cef8b50SAndy Shevchenko 	case GPIO_V2_GET_LINEINFO_WATCH_IOCTL:
26861cef8b50SAndy Shevchenko 		return lineinfo_get(cdev, ip, true);
26871cef8b50SAndy Shevchenko 	case GPIO_V2_GET_LINE_IOCTL:
26882e202ad8SKent Gibson 		return linereq_create(gdev, ip);
26891cef8b50SAndy Shevchenko 	case GPIO_GET_LINEINFO_UNWATCH_IOCTL:
26902e202ad8SKent Gibson 		return lineinfo_unwatch(cdev, ip);
26911cef8b50SAndy Shevchenko 	default:
2692925ca369SKent Gibson 		return -EINVAL;
2693925ca369SKent Gibson 	}
26941cef8b50SAndy Shevchenko }
2695925ca369SKent Gibson 
2696249cbac4SKent Gibson /*
2697249cbac4SKent Gibson  * gpio_ioctl() - ioctl handler for the GPIO chardev
2698249cbac4SKent Gibson  */
2699249cbac4SKent Gibson static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2700249cbac4SKent Gibson {
2701249cbac4SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2702249cbac4SKent Gibson 
2703249cbac4SKent Gibson 	return call_ioctl_locked(file, cmd, arg, cdev->gdev,
2704249cbac4SKent Gibson 				 gpio_ioctl_unlocked);
2705249cbac4SKent Gibson }
2706249cbac4SKent Gibson 
2707925ca369SKent Gibson #ifdef CONFIG_COMPAT
270849bc5279SKent Gibson static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2709925ca369SKent Gibson 			      unsigned long arg)
2710925ca369SKent Gibson {
271149bc5279SKent Gibson 	return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2712925ca369SKent Gibson }
2713925ca369SKent Gibson #endif
2714925ca369SKent Gibson 
2715925ca369SKent Gibson static int lineinfo_changed_notify(struct notifier_block *nb,
2716925ca369SKent Gibson 				   unsigned long action, void *data)
2717925ca369SKent Gibson {
2718e82bbd67SBartosz Golaszewski 	struct gpio_chardev_data *cdev =
2719e82bbd67SBartosz Golaszewski 		container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2720aad95584SKent Gibson 	struct gpio_v2_line_info_changed chg;
2721925ca369SKent Gibson 	struct gpio_desc *desc = data;
2722925ca369SKent Gibson 	int ret;
2723925ca369SKent Gibson 
2724e2b781c5SKent Gibson 	if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2725925ca369SKent Gibson 		return NOTIFY_DONE;
2726925ca369SKent Gibson 
2727925ca369SKent Gibson 	memset(&chg, 0, sizeof(chg));
2728925ca369SKent Gibson 	chg.event_type = action;
2729aad95584SKent Gibson 	chg.timestamp_ns = ktime_get_ns();
2730925ca369SKent Gibson 	gpio_desc_to_lineinfo(desc, &chg.info);
2731309364bcSKent Gibson 	supinfo_to_lineinfo(desc, &chg.info);
2732925ca369SKent Gibson 
2733e2b781c5SKent Gibson 	ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2734925ca369SKent Gibson 	if (ret)
2735e2b781c5SKent Gibson 		wake_up_poll(&cdev->wait, EPOLLIN);
2736925ca369SKent Gibson 	else
2737925ca369SKent Gibson 		pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2738925ca369SKent Gibson 
2739925ca369SKent Gibson 	return NOTIFY_OK;
2740925ca369SKent Gibson }
2741925ca369SKent Gibson 
2742d2e2586aSBartosz Golaszewski static int gpio_device_unregistered_notify(struct notifier_block *nb,
2743d2e2586aSBartosz Golaszewski 					   unsigned long action, void *data)
2744d2e2586aSBartosz Golaszewski {
2745d2e2586aSBartosz Golaszewski 	struct gpio_chardev_data *cdev = container_of(nb,
2746d2e2586aSBartosz Golaszewski 						      struct gpio_chardev_data,
2747d2e2586aSBartosz Golaszewski 						      device_unregistered_nb);
2748d2e2586aSBartosz Golaszewski 
2749d2e2586aSBartosz Golaszewski 	wake_up_poll(&cdev->wait, EPOLLIN | EPOLLERR);
2750d2e2586aSBartosz Golaszewski 
2751d2e2586aSBartosz Golaszewski 	return NOTIFY_OK;
2752d2e2586aSBartosz Golaszewski }
2753d2e2586aSBartosz Golaszewski 
2754bdbbae24SBartosz Golaszewski static __poll_t lineinfo_watch_poll_unlocked(struct file *file,
2755925ca369SKent Gibson 					     struct poll_table_struct *pollt)
2756925ca369SKent Gibson {
2757e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2758925ca369SKent Gibson 	__poll_t events = 0;
2759925ca369SKent Gibson 
2760533aae7cSBartosz Golaszewski 	if (!cdev->gdev->chip)
2761533aae7cSBartosz Golaszewski 		return EPOLLHUP | EPOLLERR;
2762533aae7cSBartosz Golaszewski 
2763e2b781c5SKent Gibson 	poll_wait(file, &cdev->wait, pollt);
2764925ca369SKent Gibson 
2765e2b781c5SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2766e2b781c5SKent Gibson 						 &cdev->wait.lock))
2767925ca369SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
2768925ca369SKent Gibson 
2769925ca369SKent Gibson 	return events;
2770925ca369SKent Gibson }
2771925ca369SKent Gibson 
2772bdbbae24SBartosz Golaszewski static __poll_t lineinfo_watch_poll(struct file *file,
2773bdbbae24SBartosz Golaszewski 				    struct poll_table_struct *pollt)
2774bdbbae24SBartosz Golaszewski {
2775bdbbae24SBartosz Golaszewski 	struct gpio_chardev_data *cdev = file->private_data;
2776bdbbae24SBartosz Golaszewski 
2777bdbbae24SBartosz Golaszewski 	return call_poll_locked(file, pollt, cdev->gdev,
2778bdbbae24SBartosz Golaszewski 				lineinfo_watch_poll_unlocked);
2779bdbbae24SBartosz Golaszewski }
2780bdbbae24SBartosz Golaszewski 
2781bdbbae24SBartosz Golaszewski static ssize_t lineinfo_watch_read_unlocked(struct file *file, char __user *buf,
2782925ca369SKent Gibson 					    size_t count, loff_t *off)
2783925ca369SKent Gibson {
2784e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2785aad95584SKent Gibson 	struct gpio_v2_line_info_changed event;
2786925ca369SKent Gibson 	ssize_t bytes_read = 0;
2787925ca369SKent Gibson 	int ret;
2788aad95584SKent Gibson 	size_t event_size;
2789925ca369SKent Gibson 
2790533aae7cSBartosz Golaszewski 	if (!cdev->gdev->chip)
2791533aae7cSBartosz Golaszewski 		return -ENODEV;
2792533aae7cSBartosz Golaszewski 
2793aad95584SKent Gibson #ifndef CONFIG_GPIO_CDEV_V1
2794aad95584SKent Gibson 	event_size = sizeof(struct gpio_v2_line_info_changed);
2795aad95584SKent Gibson 	if (count < event_size)
2796925ca369SKent Gibson 		return -EINVAL;
2797aad95584SKent Gibson #endif
2798925ca369SKent Gibson 
2799925ca369SKent Gibson 	do {
2800e2b781c5SKent Gibson 		spin_lock(&cdev->wait.lock);
2801e2b781c5SKent Gibson 		if (kfifo_is_empty(&cdev->events)) {
2802925ca369SKent Gibson 			if (bytes_read) {
2803e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2804925ca369SKent Gibson 				return bytes_read;
2805925ca369SKent Gibson 			}
2806925ca369SKent Gibson 
280749bc5279SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
2808e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2809925ca369SKent Gibson 				return -EAGAIN;
2810925ca369SKent Gibson 			}
2811925ca369SKent Gibson 
2812e2b781c5SKent Gibson 			ret = wait_event_interruptible_locked(cdev->wait,
2813e2b781c5SKent Gibson 					!kfifo_is_empty(&cdev->events));
2814925ca369SKent Gibson 			if (ret) {
2815e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
2816925ca369SKent Gibson 				return ret;
2817925ca369SKent Gibson 			}
2818925ca369SKent Gibson 		}
2819aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2820aad95584SKent Gibson 		/* must be after kfifo check so watch_abi_version is set */
2821aad95584SKent Gibson 		if (atomic_read(&cdev->watch_abi_version) == 2)
2822aad95584SKent Gibson 			event_size = sizeof(struct gpio_v2_line_info_changed);
2823aad95584SKent Gibson 		else
2824aad95584SKent Gibson 			event_size = sizeof(struct gpioline_info_changed);
2825aad95584SKent Gibson 		if (count < event_size) {
2826aad95584SKent Gibson 			spin_unlock(&cdev->wait.lock);
2827aad95584SKent Gibson 			return -EINVAL;
2828aad95584SKent Gibson 		}
2829aad95584SKent Gibson #endif
2830e2b781c5SKent Gibson 		ret = kfifo_out(&cdev->events, &event, 1);
2831e2b781c5SKent Gibson 		spin_unlock(&cdev->wait.lock);
2832925ca369SKent Gibson 		if (ret != 1) {
2833925ca369SKent Gibson 			ret = -EIO;
2834925ca369SKent Gibson 			break;
2835925ca369SKent Gibson 			/* We should never get here. See lineevent_read(). */
2836925ca369SKent Gibson 		}
2837925ca369SKent Gibson 
2838aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2839aad95584SKent Gibson 		if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2840aad95584SKent Gibson 			if (copy_to_user(buf + bytes_read, &event, event_size))
2841925ca369SKent Gibson 				return -EFAULT;
2842aad95584SKent Gibson 		} else {
2843aad95584SKent Gibson 			struct gpioline_info_changed event_v1;
2844aad95584SKent Gibson 
2845aad95584SKent Gibson 			gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2846aad95584SKent Gibson 			if (copy_to_user(buf + bytes_read, &event_v1,
2847aad95584SKent Gibson 					 event_size))
2848aad95584SKent Gibson 				return -EFAULT;
2849aad95584SKent Gibson 		}
2850aad95584SKent Gibson #else
2851aad95584SKent Gibson 		if (copy_to_user(buf + bytes_read, &event, event_size))
2852aad95584SKent Gibson 			return -EFAULT;
2853aad95584SKent Gibson #endif
2854aad95584SKent Gibson 		bytes_read += event_size;
2855925ca369SKent Gibson 	} while (count >= bytes_read + sizeof(event));
2856925ca369SKent Gibson 
2857925ca369SKent Gibson 	return bytes_read;
2858925ca369SKent Gibson }
2859925ca369SKent Gibson 
2860bdbbae24SBartosz Golaszewski static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2861bdbbae24SBartosz Golaszewski 				   size_t count, loff_t *off)
2862bdbbae24SBartosz Golaszewski {
2863bdbbae24SBartosz Golaszewski 	struct gpio_chardev_data *cdev = file->private_data;
2864bdbbae24SBartosz Golaszewski 
2865bdbbae24SBartosz Golaszewski 	return call_read_locked(file, buf, count, off, cdev->gdev,
2866bdbbae24SBartosz Golaszewski 				lineinfo_watch_read_unlocked);
2867bdbbae24SBartosz Golaszewski }
2868bdbbae24SBartosz Golaszewski 
2869925ca369SKent Gibson /**
2870925ca369SKent Gibson  * gpio_chrdev_open() - open the chardev for ioctl operations
2871925ca369SKent Gibson  * @inode: inode for this chardev
287249bc5279SKent Gibson  * @file: file struct for storing private data
2873925ca369SKent Gibson  * Returns 0 on success
2874925ca369SKent Gibson  */
287549bc5279SKent Gibson static int gpio_chrdev_open(struct inode *inode, struct file *file)
2876925ca369SKent Gibson {
2877925ca369SKent Gibson 	struct gpio_device *gdev = container_of(inode->i_cdev,
2878925ca369SKent Gibson 						struct gpio_device, chrdev);
2879e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev;
2880925ca369SKent Gibson 	int ret = -ENOMEM;
2881925ca369SKent Gibson 
2882bdbbae24SBartosz Golaszewski 	down_read(&gdev->sem);
2883bdbbae24SBartosz Golaszewski 
2884925ca369SKent Gibson 	/* Fail on open if the backing gpiochip is gone */
2885bdbbae24SBartosz Golaszewski 	if (!gdev->chip) {
2886bdbbae24SBartosz Golaszewski 		ret = -ENODEV;
2887bdbbae24SBartosz Golaszewski 		goto out_unlock;
2888bdbbae24SBartosz Golaszewski 	}
2889925ca369SKent Gibson 
2890e2b781c5SKent Gibson 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2891e2b781c5SKent Gibson 	if (!cdev)
2892bdbbae24SBartosz Golaszewski 		goto out_unlock;
2893925ca369SKent Gibson 
2894e2b781c5SKent Gibson 	cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2895e2b781c5SKent Gibson 	if (!cdev->watched_lines)
2896e2b781c5SKent Gibson 		goto out_free_cdev;
2897925ca369SKent Gibson 
2898e2b781c5SKent Gibson 	init_waitqueue_head(&cdev->wait);
2899e2b781c5SKent Gibson 	INIT_KFIFO(cdev->events);
2900dc0989e3SAndy Shevchenko 	cdev->gdev = gpio_device_get(gdev);
2901925ca369SKent Gibson 
2902e2b781c5SKent Gibson 	cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
290317a7ca35SBartosz Golaszewski 	ret = blocking_notifier_chain_register(&gdev->line_state_notifier,
2904e2b781c5SKent Gibson 					       &cdev->lineinfo_changed_nb);
2905925ca369SKent Gibson 	if (ret)
2906925ca369SKent Gibson 		goto out_free_bitmap;
2907925ca369SKent Gibson 
2908d2e2586aSBartosz Golaszewski 	cdev->device_unregistered_nb.notifier_call =
2909d2e2586aSBartosz Golaszewski 					gpio_device_unregistered_notify;
2910d2e2586aSBartosz Golaszewski 	ret = blocking_notifier_chain_register(&gdev->device_notifier,
2911d2e2586aSBartosz Golaszewski 					       &cdev->device_unregistered_nb);
2912d2e2586aSBartosz Golaszewski 	if (ret)
2913d2e2586aSBartosz Golaszewski 		goto out_unregister_line_notifier;
2914d2e2586aSBartosz Golaszewski 
2915e2b781c5SKent Gibson 	file->private_data = cdev;
2916925ca369SKent Gibson 
291749bc5279SKent Gibson 	ret = nonseekable_open(inode, file);
2918925ca369SKent Gibson 	if (ret)
2919d2e2586aSBartosz Golaszewski 		goto out_unregister_device_notifier;
2920925ca369SKent Gibson 
2921bdbbae24SBartosz Golaszewski 	up_read(&gdev->sem);
2922bdbbae24SBartosz Golaszewski 
2923925ca369SKent Gibson 	return ret;
2924925ca369SKent Gibson 
2925d2e2586aSBartosz Golaszewski out_unregister_device_notifier:
2926d2e2586aSBartosz Golaszewski 	blocking_notifier_chain_unregister(&gdev->device_notifier,
2927d2e2586aSBartosz Golaszewski 					   &cdev->device_unregistered_nb);
2928d2e2586aSBartosz Golaszewski out_unregister_line_notifier:
292917a7ca35SBartosz Golaszewski 	blocking_notifier_chain_unregister(&gdev->line_state_notifier,
2930e2b781c5SKent Gibson 					   &cdev->lineinfo_changed_nb);
2931925ca369SKent Gibson out_free_bitmap:
2932dc0989e3SAndy Shevchenko 	gpio_device_put(gdev);
2933e2b781c5SKent Gibson 	bitmap_free(cdev->watched_lines);
2934e2b781c5SKent Gibson out_free_cdev:
2935e2b781c5SKent Gibson 	kfree(cdev);
2936bdbbae24SBartosz Golaszewski out_unlock:
2937bdbbae24SBartosz Golaszewski 	up_read(&gdev->sem);
2938925ca369SKent Gibson 	return ret;
2939925ca369SKent Gibson }
2940925ca369SKent Gibson 
2941925ca369SKent Gibson /**
2942925ca369SKent Gibson  * gpio_chrdev_release() - close chardev after ioctl operations
2943925ca369SKent Gibson  * @inode: inode for this chardev
294449bc5279SKent Gibson  * @file: file struct for storing private data
2945925ca369SKent Gibson  * Returns 0 on success
2946925ca369SKent Gibson  */
294749bc5279SKent Gibson static int gpio_chrdev_release(struct inode *inode, struct file *file)
2948925ca369SKent Gibson {
2949e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
2950e2b781c5SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
2951925ca369SKent Gibson 
2952d2e2586aSBartosz Golaszewski 	blocking_notifier_chain_unregister(&gdev->device_notifier,
2953d2e2586aSBartosz Golaszewski 					   &cdev->device_unregistered_nb);
295417a7ca35SBartosz Golaszewski 	blocking_notifier_chain_unregister(&gdev->line_state_notifier,
2955e2b781c5SKent Gibson 					   &cdev->lineinfo_changed_nb);
295695ca7c90SZhongqiu Han 	bitmap_free(cdev->watched_lines);
2957dc0989e3SAndy Shevchenko 	gpio_device_put(gdev);
2958e2b781c5SKent Gibson 	kfree(cdev);
2959925ca369SKent Gibson 
2960925ca369SKent Gibson 	return 0;
2961925ca369SKent Gibson }
2962925ca369SKent Gibson 
2963925ca369SKent Gibson static const struct file_operations gpio_fileops = {
2964925ca369SKent Gibson 	.release = gpio_chrdev_release,
2965925ca369SKent Gibson 	.open = gpio_chrdev_open,
2966925ca369SKent Gibson 	.poll = lineinfo_watch_poll,
2967925ca369SKent Gibson 	.read = lineinfo_watch_read,
2968925ca369SKent Gibson 	.owner = THIS_MODULE,
2969925ca369SKent Gibson 	.llseek = no_llseek,
2970925ca369SKent Gibson 	.unlocked_ioctl = gpio_ioctl,
2971925ca369SKent Gibson #ifdef CONFIG_COMPAT
2972925ca369SKent Gibson 	.compat_ioctl = gpio_ioctl_compat,
2973925ca369SKent Gibson #endif
2974925ca369SKent Gibson };
2975925ca369SKent Gibson 
2976925ca369SKent Gibson int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2977925ca369SKent Gibson {
2978925ca369SKent Gibson 	int ret;
2979925ca369SKent Gibson 
2980925ca369SKent Gibson 	cdev_init(&gdev->chrdev, &gpio_fileops);
2981925ca369SKent Gibson 	gdev->chrdev.owner = THIS_MODULE;
2982925ca369SKent Gibson 	gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2983925ca369SKent Gibson 
2984925ca369SKent Gibson 	ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2985925ca369SKent Gibson 	if (ret)
2986925ca369SKent Gibson 		return ret;
2987925ca369SKent Gibson 
2988925ca369SKent Gibson 	chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2989925ca369SKent Gibson 		 MAJOR(devt), gdev->id);
2990925ca369SKent Gibson 
2991925ca369SKent Gibson 	return 0;
2992925ca369SKent Gibson }
2993925ca369SKent Gibson 
2994925ca369SKent Gibson void gpiolib_cdev_unregister(struct gpio_device *gdev)
2995925ca369SKent Gibson {
2996925ca369SKent Gibson 	cdev_device_del(&gdev->chrdev, &gdev->dev);
2997a067419bSBartosz Golaszewski 	blocking_notifier_call_chain(&gdev->device_notifier, 0, NULL);
2998925ca369SKent Gibson }
2999