xref: /openbmc/linux/drivers/gpio/gpiolib-cdev.c (revision 73e0341992b68bb3e748134ea8c7473f9d4e279f)
1925ca369SKent Gibson // SPDX-License-Identifier: GPL-2.0
2925ca369SKent Gibson 
3d189f627SKent Gibson #include <linux/anon_inodes.h>
43c0d9c63SKent Gibson #include <linux/atomic.h>
5925ca369SKent Gibson #include <linux/bitmap.h>
63c0d9c63SKent Gibson #include <linux/build_bug.h>
7d189f627SKent Gibson #include <linux/cdev.h>
8d189f627SKent Gibson #include <linux/compat.h>
9925ca369SKent Gibson #include <linux/device.h>
10925ca369SKent Gibson #include <linux/err.h>
11d189f627SKent Gibson #include <linux/file.h>
12925ca369SKent Gibson #include <linux/gpio.h>
13925ca369SKent Gibson #include <linux/gpio/driver.h>
14d189f627SKent Gibson #include <linux/interrupt.h>
15d189f627SKent Gibson #include <linux/irqreturn.h>
16d189f627SKent Gibson #include <linux/kernel.h>
17925ca369SKent Gibson #include <linux/kfifo.h>
18d189f627SKent Gibson #include <linux/module.h>
19d189f627SKent Gibson #include <linux/pinctrl/consumer.h>
20925ca369SKent Gibson #include <linux/poll.h>
21d189f627SKent Gibson #include <linux/spinlock.h>
22925ca369SKent Gibson #include <linux/timekeeping.h>
23d189f627SKent Gibson #include <linux/uaccess.h>
24925ca369SKent Gibson #include <uapi/linux/gpio.h>
25925ca369SKent Gibson 
26925ca369SKent Gibson #include "gpiolib.h"
27925ca369SKent Gibson #include "gpiolib-cdev.h"
28925ca369SKent Gibson 
293c0d9c63SKent Gibson /*
303c0d9c63SKent Gibson  * Array sizes must ensure 64-bit alignment and not create holes in the
313c0d9c63SKent Gibson  * struct packing.
323c0d9c63SKent Gibson  */
333c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2));
343c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8));
353c0d9c63SKent Gibson 
363c0d9c63SKent Gibson /*
373c0d9c63SKent Gibson  * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility
383c0d9c63SKent Gibson  */
393c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8));
403c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8));
413c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8));
423c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8));
433c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8));
443c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8));
453c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8));
463c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8));
473c0d9c63SKent Gibson 
48925ca369SKent Gibson /* Character device interface to GPIO.
49925ca369SKent Gibson  *
50925ca369SKent Gibson  * The GPIO character device, /dev/gpiochipN, provides userspace an
51925ca369SKent Gibson  * interface to gpiolib GPIOs via ioctl()s.
52925ca369SKent Gibson  */
53925ca369SKent Gibson 
54925ca369SKent Gibson /*
55925ca369SKent Gibson  * GPIO line handle management
56925ca369SKent Gibson  */
57925ca369SKent Gibson 
583c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
59925ca369SKent Gibson /**
60925ca369SKent Gibson  * struct linehandle_state - contains the state of a userspace handle
61925ca369SKent Gibson  * @gdev: the GPIO device the handle pertains to
62925ca369SKent Gibson  * @label: consumer label used to tag descriptors
63925ca369SKent Gibson  * @descs: the GPIO descriptors held by this handle
6452b7b596SKent Gibson  * @num_descs: the number of descriptors held in the descs array
65925ca369SKent Gibson  */
66925ca369SKent Gibson struct linehandle_state {
67925ca369SKent Gibson 	struct gpio_device *gdev;
68925ca369SKent Gibson 	const char *label;
69925ca369SKent Gibson 	struct gpio_desc *descs[GPIOHANDLES_MAX];
7052b7b596SKent Gibson 	u32 num_descs;
71925ca369SKent Gibson };
72925ca369SKent Gibson 
73925ca369SKent Gibson #define GPIOHANDLE_REQUEST_VALID_FLAGS \
74925ca369SKent Gibson 	(GPIOHANDLE_REQUEST_INPUT | \
75925ca369SKent Gibson 	GPIOHANDLE_REQUEST_OUTPUT | \
76925ca369SKent Gibson 	GPIOHANDLE_REQUEST_ACTIVE_LOW | \
77925ca369SKent Gibson 	GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
78925ca369SKent Gibson 	GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
79925ca369SKent Gibson 	GPIOHANDLE_REQUEST_BIAS_DISABLE | \
80925ca369SKent Gibson 	GPIOHANDLE_REQUEST_OPEN_DRAIN | \
81925ca369SKent Gibson 	GPIOHANDLE_REQUEST_OPEN_SOURCE)
82925ca369SKent Gibson 
83925ca369SKent Gibson static int linehandle_validate_flags(u32 flags)
84925ca369SKent Gibson {
85925ca369SKent Gibson 	/* Return an error if an unknown flag is set */
86925ca369SKent Gibson 	if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
87925ca369SKent Gibson 		return -EINVAL;
88925ca369SKent Gibson 
89925ca369SKent Gibson 	/*
90925ca369SKent Gibson 	 * Do not allow both INPUT & OUTPUT flags to be set as they are
91925ca369SKent Gibson 	 * contradictory.
92925ca369SKent Gibson 	 */
93925ca369SKent Gibson 	if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
94925ca369SKent Gibson 	    (flags & GPIOHANDLE_REQUEST_OUTPUT))
95925ca369SKent Gibson 		return -EINVAL;
96925ca369SKent Gibson 
97925ca369SKent Gibson 	/*
98925ca369SKent Gibson 	 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
99925ca369SKent Gibson 	 * the hardware actually supports enabling both at the same time the
100925ca369SKent Gibson 	 * electrical result would be disastrous.
101925ca369SKent Gibson 	 */
102925ca369SKent Gibson 	if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
103925ca369SKent Gibson 	    (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
104925ca369SKent Gibson 		return -EINVAL;
105925ca369SKent Gibson 
106925ca369SKent Gibson 	/* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
107925ca369SKent Gibson 	if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
108925ca369SKent Gibson 	    ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
109925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
110925ca369SKent Gibson 		return -EINVAL;
111925ca369SKent Gibson 
112925ca369SKent Gibson 	/* Bias flags only allowed for input or output mode. */
113925ca369SKent Gibson 	if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
114925ca369SKent Gibson 	      (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
115925ca369SKent Gibson 	    ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
116925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
117925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
118925ca369SKent Gibson 		return -EINVAL;
119925ca369SKent Gibson 
120925ca369SKent Gibson 	/* Only one bias flag can be set. */
121925ca369SKent Gibson 	if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
122925ca369SKent Gibson 	     (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
123925ca369SKent Gibson 		       GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
124925ca369SKent Gibson 	    ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
125925ca369SKent Gibson 	     (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
126925ca369SKent Gibson 		return -EINVAL;
127925ca369SKent Gibson 
128925ca369SKent Gibson 	return 0;
129925ca369SKent Gibson }
130925ca369SKent Gibson 
131c274b58aSKent Gibson static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
132c274b58aSKent Gibson {
133c274b58aSKent Gibson 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
134c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
135c274b58aSKent Gibson 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
136c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
137c274b58aSKent Gibson 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
138c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
139c274b58aSKent Gibson 	assign_bit(FLAG_PULL_UP, flagsp,
140c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
141c274b58aSKent Gibson 	assign_bit(FLAG_PULL_DOWN, flagsp,
142c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
143c274b58aSKent Gibson 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
144c274b58aSKent Gibson 		   lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
145c274b58aSKent Gibson }
146c274b58aSKent Gibson 
147925ca369SKent Gibson static long linehandle_set_config(struct linehandle_state *lh,
148925ca369SKent Gibson 				  void __user *ip)
149925ca369SKent Gibson {
150925ca369SKent Gibson 	struct gpiohandle_config gcnf;
151925ca369SKent Gibson 	struct gpio_desc *desc;
152925ca369SKent Gibson 	int i, ret;
153925ca369SKent Gibson 	u32 lflags;
154925ca369SKent Gibson 
155925ca369SKent Gibson 	if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
156925ca369SKent Gibson 		return -EFAULT;
157925ca369SKent Gibson 
158925ca369SKent Gibson 	lflags = gcnf.flags;
159925ca369SKent Gibson 	ret = linehandle_validate_flags(lflags);
160925ca369SKent Gibson 	if (ret)
161925ca369SKent Gibson 		return ret;
162925ca369SKent Gibson 
16352b7b596SKent Gibson 	for (i = 0; i < lh->num_descs; i++) {
164925ca369SKent Gibson 		desc = lh->descs[i];
165c274b58aSKent Gibson 		linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags);
166925ca369SKent Gibson 
167925ca369SKent Gibson 		/*
168925ca369SKent Gibson 		 * Lines have to be requested explicitly for input
169925ca369SKent Gibson 		 * or output, else the line will be treated "as is".
170925ca369SKent Gibson 		 */
171925ca369SKent Gibson 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
172925ca369SKent Gibson 			int val = !!gcnf.default_values[i];
173925ca369SKent Gibson 
174925ca369SKent Gibson 			ret = gpiod_direction_output(desc, val);
175925ca369SKent Gibson 			if (ret)
176925ca369SKent Gibson 				return ret;
177925ca369SKent Gibson 		} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
178925ca369SKent Gibson 			ret = gpiod_direction_input(desc);
179925ca369SKent Gibson 			if (ret)
180925ca369SKent Gibson 				return ret;
181925ca369SKent Gibson 		}
182925ca369SKent Gibson 
1836accc376SKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
184aad95584SKent Gibson 					     GPIO_V2_LINE_CHANGED_CONFIG,
185aad95584SKent Gibson 					     desc);
186925ca369SKent Gibson 	}
187925ca369SKent Gibson 	return 0;
188925ca369SKent Gibson }
189925ca369SKent Gibson 
19049bc5279SKent Gibson static long linehandle_ioctl(struct file *file, unsigned int cmd,
191925ca369SKent Gibson 			     unsigned long arg)
192925ca369SKent Gibson {
19349bc5279SKent Gibson 	struct linehandle_state *lh = file->private_data;
194925ca369SKent Gibson 	void __user *ip = (void __user *)arg;
195925ca369SKent Gibson 	struct gpiohandle_data ghd;
196925ca369SKent Gibson 	DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
197925ca369SKent Gibson 	int i;
198925ca369SKent Gibson 
199925ca369SKent Gibson 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
200925ca369SKent Gibson 		/* NOTE: It's ok to read values of output lines. */
201925ca369SKent Gibson 		int ret = gpiod_get_array_value_complex(false,
202925ca369SKent Gibson 							true,
20352b7b596SKent Gibson 							lh->num_descs,
204925ca369SKent Gibson 							lh->descs,
205925ca369SKent Gibson 							NULL,
206925ca369SKent Gibson 							vals);
207925ca369SKent Gibson 		if (ret)
208925ca369SKent Gibson 			return ret;
209925ca369SKent Gibson 
210925ca369SKent Gibson 		memset(&ghd, 0, sizeof(ghd));
21152b7b596SKent Gibson 		for (i = 0; i < lh->num_descs; i++)
212925ca369SKent Gibson 			ghd.values[i] = test_bit(i, vals);
213925ca369SKent Gibson 
214925ca369SKent Gibson 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
215925ca369SKent Gibson 			return -EFAULT;
216925ca369SKent Gibson 
217925ca369SKent Gibson 		return 0;
218925ca369SKent Gibson 	} else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
219925ca369SKent Gibson 		/*
220925ca369SKent Gibson 		 * All line descriptors were created at once with the same
221925ca369SKent Gibson 		 * flags so just check if the first one is really output.
222925ca369SKent Gibson 		 */
223925ca369SKent Gibson 		if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
224925ca369SKent Gibson 			return -EPERM;
225925ca369SKent Gibson 
226925ca369SKent Gibson 		if (copy_from_user(&ghd, ip, sizeof(ghd)))
227925ca369SKent Gibson 			return -EFAULT;
228925ca369SKent Gibson 
229925ca369SKent Gibson 		/* Clamp all values to [0,1] */
23052b7b596SKent Gibson 		for (i = 0; i < lh->num_descs; i++)
231925ca369SKent Gibson 			__assign_bit(i, vals, ghd.values[i]);
232925ca369SKent Gibson 
233925ca369SKent Gibson 		/* Reuse the array setting function */
234925ca369SKent Gibson 		return gpiod_set_array_value_complex(false,
235925ca369SKent Gibson 						     true,
23652b7b596SKent Gibson 						     lh->num_descs,
237925ca369SKent Gibson 						     lh->descs,
238925ca369SKent Gibson 						     NULL,
239925ca369SKent Gibson 						     vals);
240925ca369SKent Gibson 	} else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) {
241925ca369SKent Gibson 		return linehandle_set_config(lh, ip);
242925ca369SKent Gibson 	}
243925ca369SKent Gibson 	return -EINVAL;
244925ca369SKent Gibson }
245925ca369SKent Gibson 
246925ca369SKent Gibson #ifdef CONFIG_COMPAT
24749bc5279SKent Gibson static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
248925ca369SKent Gibson 				    unsigned long arg)
249925ca369SKent Gibson {
25049bc5279SKent Gibson 	return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
251925ca369SKent Gibson }
252925ca369SKent Gibson #endif
253925ca369SKent Gibson 
254883f9198SKent Gibson static void linehandle_free(struct linehandle_state *lh)
255925ca369SKent Gibson {
256925ca369SKent Gibson 	int i;
257925ca369SKent Gibson 
25852b7b596SKent Gibson 	for (i = 0; i < lh->num_descs; i++)
259883f9198SKent Gibson 		if (lh->descs[i])
260925ca369SKent Gibson 			gpiod_free(lh->descs[i]);
261925ca369SKent Gibson 	kfree(lh->label);
262883f9198SKent Gibson 	put_device(&lh->gdev->dev);
263925ca369SKent Gibson 	kfree(lh);
264883f9198SKent Gibson }
265883f9198SKent Gibson 
266883f9198SKent Gibson static int linehandle_release(struct inode *inode, struct file *file)
267883f9198SKent Gibson {
268883f9198SKent Gibson 	linehandle_free(file->private_data);
269925ca369SKent Gibson 	return 0;
270925ca369SKent Gibson }
271925ca369SKent Gibson 
272925ca369SKent Gibson static const struct file_operations linehandle_fileops = {
273925ca369SKent Gibson 	.release = linehandle_release,
274925ca369SKent Gibson 	.owner = THIS_MODULE,
275925ca369SKent Gibson 	.llseek = noop_llseek,
276925ca369SKent Gibson 	.unlocked_ioctl = linehandle_ioctl,
277925ca369SKent Gibson #ifdef CONFIG_COMPAT
278925ca369SKent Gibson 	.compat_ioctl = linehandle_ioctl_compat,
279925ca369SKent Gibson #endif
280925ca369SKent Gibson };
281925ca369SKent Gibson 
282925ca369SKent Gibson static int linehandle_create(struct gpio_device *gdev, void __user *ip)
283925ca369SKent Gibson {
284925ca369SKent Gibson 	struct gpiohandle_request handlereq;
285925ca369SKent Gibson 	struct linehandle_state *lh;
286925ca369SKent Gibson 	struct file *file;
287883f9198SKent Gibson 	int fd, i, ret;
288925ca369SKent Gibson 	u32 lflags;
289925ca369SKent Gibson 
290925ca369SKent Gibson 	if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
291925ca369SKent Gibson 		return -EFAULT;
292925ca369SKent Gibson 	if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
293925ca369SKent Gibson 		return -EINVAL;
294925ca369SKent Gibson 
295925ca369SKent Gibson 	lflags = handlereq.flags;
296925ca369SKent Gibson 
297925ca369SKent Gibson 	ret = linehandle_validate_flags(lflags);
298925ca369SKent Gibson 	if (ret)
299925ca369SKent Gibson 		return ret;
300925ca369SKent Gibson 
301925ca369SKent Gibson 	lh = kzalloc(sizeof(*lh), GFP_KERNEL);
302925ca369SKent Gibson 	if (!lh)
303925ca369SKent Gibson 		return -ENOMEM;
304925ca369SKent Gibson 	lh->gdev = gdev;
305925ca369SKent Gibson 	get_device(&gdev->dev);
306925ca369SKent Gibson 
307925ca369SKent Gibson 	/* Make sure this is terminated */
308925ca369SKent Gibson 	handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
309925ca369SKent Gibson 	if (strlen(handlereq.consumer_label)) {
310925ca369SKent Gibson 		lh->label = kstrdup(handlereq.consumer_label,
311925ca369SKent Gibson 				    GFP_KERNEL);
312925ca369SKent Gibson 		if (!lh->label) {
313925ca369SKent Gibson 			ret = -ENOMEM;
314925ca369SKent Gibson 			goto out_free_lh;
315925ca369SKent Gibson 		}
316925ca369SKent Gibson 	}
317925ca369SKent Gibson 
318883f9198SKent Gibson 	lh->num_descs = handlereq.lines;
319883f9198SKent Gibson 
320925ca369SKent Gibson 	/* Request each GPIO */
321925ca369SKent Gibson 	for (i = 0; i < handlereq.lines; i++) {
322925ca369SKent Gibson 		u32 offset = handlereq.lineoffsets[i];
323925ca369SKent Gibson 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
324925ca369SKent Gibson 
325925ca369SKent Gibson 		if (IS_ERR(desc)) {
326925ca369SKent Gibson 			ret = PTR_ERR(desc);
327883f9198SKent Gibson 			goto out_free_lh;
328925ca369SKent Gibson 		}
329925ca369SKent Gibson 
330925ca369SKent Gibson 		ret = gpiod_request(desc, lh->label);
331925ca369SKent Gibson 		if (ret)
332883f9198SKent Gibson 			goto out_free_lh;
333925ca369SKent Gibson 		lh->descs[i] = desc;
334c274b58aSKent Gibson 		linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
335925ca369SKent Gibson 
336925ca369SKent Gibson 		ret = gpiod_set_transitory(desc, false);
337925ca369SKent Gibson 		if (ret < 0)
338883f9198SKent Gibson 			goto out_free_lh;
339925ca369SKent Gibson 
340925ca369SKent Gibson 		/*
341925ca369SKent Gibson 		 * Lines have to be requested explicitly for input
342925ca369SKent Gibson 		 * or output, else the line will be treated "as is".
343925ca369SKent Gibson 		 */
344925ca369SKent Gibson 		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
345925ca369SKent Gibson 			int val = !!handlereq.default_values[i];
346925ca369SKent Gibson 
347925ca369SKent Gibson 			ret = gpiod_direction_output(desc, val);
348925ca369SKent Gibson 			if (ret)
349883f9198SKent Gibson 				goto out_free_lh;
350925ca369SKent Gibson 		} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
351925ca369SKent Gibson 			ret = gpiod_direction_input(desc);
352925ca369SKent Gibson 			if (ret)
353883f9198SKent Gibson 				goto out_free_lh;
354925ca369SKent Gibson 		}
355925ca369SKent Gibson 
3566accc376SKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
357aad95584SKent Gibson 					     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
358925ca369SKent Gibson 
359925ca369SKent Gibson 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
360925ca369SKent Gibson 			offset);
361925ca369SKent Gibson 	}
362925ca369SKent Gibson 
363925ca369SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
364925ca369SKent Gibson 	if (fd < 0) {
365925ca369SKent Gibson 		ret = fd;
366883f9198SKent Gibson 		goto out_free_lh;
367925ca369SKent Gibson 	}
368925ca369SKent Gibson 
369925ca369SKent Gibson 	file = anon_inode_getfile("gpio-linehandle",
370925ca369SKent Gibson 				  &linehandle_fileops,
371925ca369SKent Gibson 				  lh,
372925ca369SKent Gibson 				  O_RDONLY | O_CLOEXEC);
373925ca369SKent Gibson 	if (IS_ERR(file)) {
374925ca369SKent Gibson 		ret = PTR_ERR(file);
375925ca369SKent Gibson 		goto out_put_unused_fd;
376925ca369SKent Gibson 	}
377925ca369SKent Gibson 
378925ca369SKent Gibson 	handlereq.fd = fd;
379925ca369SKent Gibson 	if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
380925ca369SKent Gibson 		/*
381925ca369SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
382925ca369SKent Gibson 		 * the regular error cleanup path here.
383925ca369SKent Gibson 		 */
384925ca369SKent Gibson 		fput(file);
385925ca369SKent Gibson 		put_unused_fd(fd);
386925ca369SKent Gibson 		return -EFAULT;
387925ca369SKent Gibson 	}
388925ca369SKent Gibson 
389925ca369SKent Gibson 	fd_install(fd, file);
390925ca369SKent Gibson 
391925ca369SKent Gibson 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
39252b7b596SKent Gibson 		lh->num_descs);
393925ca369SKent Gibson 
394925ca369SKent Gibson 	return 0;
395925ca369SKent Gibson 
396925ca369SKent Gibson out_put_unused_fd:
397925ca369SKent Gibson 	put_unused_fd(fd);
398925ca369SKent Gibson out_free_lh:
399883f9198SKent Gibson 	linehandle_free(lh);
400925ca369SKent Gibson 	return ret;
401925ca369SKent Gibson }
4023c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
4033c0d9c63SKent Gibson 
4043c0d9c63SKent Gibson /**
4053c0d9c63SKent Gibson  * struct line - contains the state of a requested line
4063c0d9c63SKent Gibson  * @desc: the GPIO descriptor for this line.
407*73e03419SKent Gibson  * @req: the corresponding line request
408*73e03419SKent Gibson  * @irq: the interrupt triggered in response to events on this GPIO
409*73e03419SKent Gibson  * @eflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or
410*73e03419SKent Gibson  * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied
411*73e03419SKent Gibson  * @timestamp_ns: cache for the timestamp storing it between hardirq and
412*73e03419SKent Gibson  * IRQ thread, used to bring the timestamp close to the actual event
413*73e03419SKent Gibson  * @req_seqno: the seqno for the current edge event in the sequence of
414*73e03419SKent Gibson  * events for the corresponding line request. This is drawn from the @req.
415*73e03419SKent Gibson  * @line_seqno: the seqno for the current edge event in the sequence of
416*73e03419SKent Gibson  * events for this line.
4173c0d9c63SKent Gibson  */
4183c0d9c63SKent Gibson struct line {
4193c0d9c63SKent Gibson 	struct gpio_desc *desc;
420*73e03419SKent Gibson 	/*
421*73e03419SKent Gibson 	 * -- edge detector specific fields --
422*73e03419SKent Gibson 	 */
423*73e03419SKent Gibson 	struct linereq *req;
424*73e03419SKent Gibson 	unsigned int irq;
425*73e03419SKent Gibson 	u64 eflags;
426*73e03419SKent Gibson 	/*
427*73e03419SKent Gibson 	 * timestamp_ns and req_seqno are accessed only by
428*73e03419SKent Gibson 	 * edge_irq_handler() and edge_irq_thread(), which are themselves
429*73e03419SKent Gibson 	 * mutually exclusive, so no additional protection is necessary.
430*73e03419SKent Gibson 	 */
431*73e03419SKent Gibson 	u64 timestamp_ns;
432*73e03419SKent Gibson 	u32 req_seqno;
433*73e03419SKent Gibson 	u32 line_seqno;
4343c0d9c63SKent Gibson };
4353c0d9c63SKent Gibson 
4363c0d9c63SKent Gibson /**
4373c0d9c63SKent Gibson  * struct linereq - contains the state of a userspace line request
4383c0d9c63SKent Gibson  * @gdev: the GPIO device the line request pertains to
4393c0d9c63SKent Gibson  * @label: consumer label used to tag GPIO descriptors
4403c0d9c63SKent Gibson  * @num_lines: the number of lines in the lines array
441*73e03419SKent Gibson  * @wait: wait queue that handles blocking reads of events
442*73e03419SKent Gibson  * @event_buffer_size: the number of elements allocated in @events
443*73e03419SKent Gibson  * @events: KFIFO for the GPIO events
444*73e03419SKent Gibson  * @seqno: the sequence number for edge events generated on all lines in
445*73e03419SKent Gibson  * this line request.  Note that this is not used when @num_lines is 1, as
446*73e03419SKent Gibson  * the line_seqno is then the same and is cheaper to calculate.
4473c0d9c63SKent Gibson  * @lines: the lines held by this line request, with @num_lines elements.
4483c0d9c63SKent Gibson  */
4493c0d9c63SKent Gibson struct linereq {
4503c0d9c63SKent Gibson 	struct gpio_device *gdev;
4513c0d9c63SKent Gibson 	const char *label;
4523c0d9c63SKent Gibson 	u32 num_lines;
453*73e03419SKent Gibson 	wait_queue_head_t wait;
454*73e03419SKent Gibson 	u32 event_buffer_size;
455*73e03419SKent Gibson 	DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
456*73e03419SKent Gibson 	atomic_t seqno;
4573c0d9c63SKent Gibson 	struct line lines[];
4583c0d9c63SKent Gibson };
4593c0d9c63SKent Gibson 
4603c0d9c63SKent Gibson #define GPIO_V2_LINE_BIAS_FLAGS \
4613c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
4623c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
4633c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_BIAS_DISABLED)
4643c0d9c63SKent Gibson 
4653c0d9c63SKent Gibson #define GPIO_V2_LINE_DIRECTION_FLAGS \
4663c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_INPUT | \
4673c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_OUTPUT)
4683c0d9c63SKent Gibson 
4693c0d9c63SKent Gibson #define GPIO_V2_LINE_DRIVE_FLAGS \
4703c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
4713c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_OPEN_SOURCE)
4723c0d9c63SKent Gibson 
473*73e03419SKent Gibson #define GPIO_V2_LINE_EDGE_FLAGS \
474*73e03419SKent Gibson 	(GPIO_V2_LINE_FLAG_EDGE_RISING | \
475*73e03419SKent Gibson 	 GPIO_V2_LINE_FLAG_EDGE_FALLING)
476*73e03419SKent Gibson 
4773c0d9c63SKent Gibson #define GPIO_V2_LINE_VALID_FLAGS \
4783c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
4793c0d9c63SKent Gibson 	 GPIO_V2_LINE_DIRECTION_FLAGS | \
4803c0d9c63SKent Gibson 	 GPIO_V2_LINE_DRIVE_FLAGS | \
481*73e03419SKent Gibson 	 GPIO_V2_LINE_EDGE_FLAGS | \
4823c0d9c63SKent Gibson 	 GPIO_V2_LINE_BIAS_FLAGS)
4833c0d9c63SKent Gibson 
484*73e03419SKent Gibson static void linereq_put_event(struct linereq *lr,
485*73e03419SKent Gibson 			      struct gpio_v2_line_event *le)
486*73e03419SKent Gibson {
487*73e03419SKent Gibson 	bool overflow = false;
488*73e03419SKent Gibson 
489*73e03419SKent Gibson 	spin_lock(&lr->wait.lock);
490*73e03419SKent Gibson 	if (kfifo_is_full(&lr->events)) {
491*73e03419SKent Gibson 		overflow = true;
492*73e03419SKent Gibson 		kfifo_skip(&lr->events);
493*73e03419SKent Gibson 	}
494*73e03419SKent Gibson 	kfifo_in(&lr->events, le, 1);
495*73e03419SKent Gibson 	spin_unlock(&lr->wait.lock);
496*73e03419SKent Gibson 	if (!overflow)
497*73e03419SKent Gibson 		wake_up_poll(&lr->wait, EPOLLIN);
498*73e03419SKent Gibson 	else
499*73e03419SKent Gibson 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
500*73e03419SKent Gibson }
501*73e03419SKent Gibson 
502*73e03419SKent Gibson static irqreturn_t edge_irq_thread(int irq, void *p)
503*73e03419SKent Gibson {
504*73e03419SKent Gibson 	struct line *line = p;
505*73e03419SKent Gibson 	struct linereq *lr = line->req;
506*73e03419SKent Gibson 	struct gpio_v2_line_event le;
507*73e03419SKent Gibson 
508*73e03419SKent Gibson 	/* Do not leak kernel stack to userspace */
509*73e03419SKent Gibson 	memset(&le, 0, sizeof(le));
510*73e03419SKent Gibson 
511*73e03419SKent Gibson 	if (line->timestamp_ns) {
512*73e03419SKent Gibson 		le.timestamp_ns = line->timestamp_ns;
513*73e03419SKent Gibson 	} else {
514*73e03419SKent Gibson 		/*
515*73e03419SKent Gibson 		 * We may be running from a nested threaded interrupt in
516*73e03419SKent Gibson 		 * which case we didn't get the timestamp from
517*73e03419SKent Gibson 		 * edge_irq_handler().
518*73e03419SKent Gibson 		 */
519*73e03419SKent Gibson 		le.timestamp_ns = ktime_get_ns();
520*73e03419SKent Gibson 		if (lr->num_lines != 1)
521*73e03419SKent Gibson 			line->req_seqno = atomic_inc_return(&lr->seqno);
522*73e03419SKent Gibson 	}
523*73e03419SKent Gibson 	line->timestamp_ns = 0;
524*73e03419SKent Gibson 
525*73e03419SKent Gibson 	if (line->eflags == (GPIO_V2_LINE_FLAG_EDGE_RISING |
526*73e03419SKent Gibson 			     GPIO_V2_LINE_FLAG_EDGE_FALLING)) {
527*73e03419SKent Gibson 		int level = gpiod_get_value_cansleep(line->desc);
528*73e03419SKent Gibson 
529*73e03419SKent Gibson 		if (level)
530*73e03419SKent Gibson 			/* Emit low-to-high event */
531*73e03419SKent Gibson 			le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
532*73e03419SKent Gibson 		else
533*73e03419SKent Gibson 			/* Emit high-to-low event */
534*73e03419SKent Gibson 			le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
535*73e03419SKent Gibson 	} else if (line->eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) {
536*73e03419SKent Gibson 		/* Emit low-to-high event */
537*73e03419SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
538*73e03419SKent Gibson 	} else if (line->eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) {
539*73e03419SKent Gibson 		/* Emit high-to-low event */
540*73e03419SKent Gibson 		le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
541*73e03419SKent Gibson 	} else {
542*73e03419SKent Gibson 		return IRQ_NONE;
543*73e03419SKent Gibson 	}
544*73e03419SKent Gibson 	line->line_seqno++;
545*73e03419SKent Gibson 	le.line_seqno = line->line_seqno;
546*73e03419SKent Gibson 	le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
547*73e03419SKent Gibson 	le.offset = gpio_chip_hwgpio(line->desc);
548*73e03419SKent Gibson 
549*73e03419SKent Gibson 	linereq_put_event(lr, &le);
550*73e03419SKent Gibson 
551*73e03419SKent Gibson 	return IRQ_HANDLED;
552*73e03419SKent Gibson }
553*73e03419SKent Gibson 
554*73e03419SKent Gibson static irqreturn_t edge_irq_handler(int irq, void *p)
555*73e03419SKent Gibson {
556*73e03419SKent Gibson 	struct line *line = p;
557*73e03419SKent Gibson 	struct linereq *lr = line->req;
558*73e03419SKent Gibson 
559*73e03419SKent Gibson 	/*
560*73e03419SKent Gibson 	 * Just store the timestamp in hardirq context so we get it as
561*73e03419SKent Gibson 	 * close in time as possible to the actual event.
562*73e03419SKent Gibson 	 */
563*73e03419SKent Gibson 	line->timestamp_ns = ktime_get_ns();
564*73e03419SKent Gibson 
565*73e03419SKent Gibson 	if (lr->num_lines != 1)
566*73e03419SKent Gibson 		line->req_seqno = atomic_inc_return(&lr->seqno);
567*73e03419SKent Gibson 
568*73e03419SKent Gibson 	return IRQ_WAKE_THREAD;
569*73e03419SKent Gibson }
570*73e03419SKent Gibson 
571*73e03419SKent Gibson static void edge_detector_stop(struct line *line)
572*73e03419SKent Gibson {
573*73e03419SKent Gibson 	if (line->irq) {
574*73e03419SKent Gibson 		free_irq(line->irq, line);
575*73e03419SKent Gibson 		line->irq = 0;
576*73e03419SKent Gibson 	}
577*73e03419SKent Gibson }
578*73e03419SKent Gibson 
579*73e03419SKent Gibson static int edge_detector_setup(struct line *line,
580*73e03419SKent Gibson 			       u64 eflags)
581*73e03419SKent Gibson {
582*73e03419SKent Gibson 	unsigned long irqflags = 0;
583*73e03419SKent Gibson 	int irq, ret;
584*73e03419SKent Gibson 
585*73e03419SKent Gibson 	if (eflags && !kfifo_initialized(&line->req->events)) {
586*73e03419SKent Gibson 		ret = kfifo_alloc(&line->req->events,
587*73e03419SKent Gibson 				  line->req->event_buffer_size, GFP_KERNEL);
588*73e03419SKent Gibson 		if (ret)
589*73e03419SKent Gibson 			return ret;
590*73e03419SKent Gibson 	}
591*73e03419SKent Gibson 	line->eflags = eflags;
592*73e03419SKent Gibson 
593*73e03419SKent Gibson 	if (!eflags)
594*73e03419SKent Gibson 		return 0;
595*73e03419SKent Gibson 
596*73e03419SKent Gibson 	irq = gpiod_to_irq(line->desc);
597*73e03419SKent Gibson 	if (irq < 0)
598*73e03419SKent Gibson 		return -ENXIO;
599*73e03419SKent Gibson 
600*73e03419SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
601*73e03419SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
602*73e03419SKent Gibson 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
603*73e03419SKent Gibson 	if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
604*73e03419SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
605*73e03419SKent Gibson 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
606*73e03419SKent Gibson 	irqflags |= IRQF_ONESHOT;
607*73e03419SKent Gibson 
608*73e03419SKent Gibson 	/* Request a thread to read the events */
609*73e03419SKent Gibson 	ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
610*73e03419SKent Gibson 				   irqflags, line->req->label, line);
611*73e03419SKent Gibson 	if (ret)
612*73e03419SKent Gibson 		return ret;
613*73e03419SKent Gibson 
614*73e03419SKent Gibson 	line->irq = irq;
615*73e03419SKent Gibson 	return 0;
616*73e03419SKent Gibson }
617*73e03419SKent Gibson 
6183c0d9c63SKent Gibson static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
6193c0d9c63SKent Gibson 				     unsigned int line_idx)
6203c0d9c63SKent Gibson {
6213c0d9c63SKent Gibson 	unsigned int i;
6223c0d9c63SKent Gibson 	u64 mask = BIT_ULL(line_idx);
6233c0d9c63SKent Gibson 
6243c0d9c63SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
6253c0d9c63SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
6263c0d9c63SKent Gibson 		    (lc->attrs[i].mask & mask))
6273c0d9c63SKent Gibson 			return lc->attrs[i].attr.flags;
6283c0d9c63SKent Gibson 	}
6293c0d9c63SKent Gibson 	return lc->flags;
6303c0d9c63SKent Gibson }
6313c0d9c63SKent Gibson 
6323c0d9c63SKent Gibson static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
6333c0d9c63SKent Gibson 					    unsigned int line_idx)
6343c0d9c63SKent Gibson {
6353c0d9c63SKent Gibson 	unsigned int i;
6363c0d9c63SKent Gibson 	u64 mask = BIT_ULL(line_idx);
6373c0d9c63SKent Gibson 
6383c0d9c63SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
6393c0d9c63SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
6403c0d9c63SKent Gibson 		    (lc->attrs[i].mask & mask))
6413c0d9c63SKent Gibson 			return !!(lc->attrs[i].attr.values & mask);
6423c0d9c63SKent Gibson 	}
6433c0d9c63SKent Gibson 	return 0;
6443c0d9c63SKent Gibson }
6453c0d9c63SKent Gibson 
6463c0d9c63SKent Gibson static int gpio_v2_line_flags_validate(u64 flags)
6473c0d9c63SKent Gibson {
6483c0d9c63SKent Gibson 	/* Return an error if an unknown flag is set */
6493c0d9c63SKent Gibson 	if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
6503c0d9c63SKent Gibson 		return -EINVAL;
6513c0d9c63SKent Gibson 
6523c0d9c63SKent Gibson 	/*
6533c0d9c63SKent Gibson 	 * Do not allow both INPUT and OUTPUT flags to be set as they are
6543c0d9c63SKent Gibson 	 * contradictory.
6553c0d9c63SKent Gibson 	 */
6563c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
6573c0d9c63SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_OUTPUT))
6583c0d9c63SKent Gibson 		return -EINVAL;
6593c0d9c63SKent Gibson 
660*73e03419SKent Gibson 	/* Edge detection requires explicit input. */
661*73e03419SKent Gibson 	if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
662*73e03419SKent Gibson 	    !(flags & GPIO_V2_LINE_FLAG_INPUT))
663*73e03419SKent Gibson 		return -EINVAL;
664*73e03419SKent Gibson 
6653c0d9c63SKent Gibson 	/*
6663c0d9c63SKent Gibson 	 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
6673c0d9c63SKent Gibson 	 * request. If the hardware actually supports enabling both at the
6683c0d9c63SKent Gibson 	 * same time the electrical result would be disastrous.
6693c0d9c63SKent Gibson 	 */
6703c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
6713c0d9c63SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
6723c0d9c63SKent Gibson 		return -EINVAL;
6733c0d9c63SKent Gibson 
6743c0d9c63SKent Gibson 	/* Drive requires explicit output direction. */
6753c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
6763c0d9c63SKent Gibson 	    !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
6773c0d9c63SKent Gibson 		return -EINVAL;
6783c0d9c63SKent Gibson 
6793c0d9c63SKent Gibson 	/* Bias requires explicit direction. */
6803c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
6813c0d9c63SKent Gibson 	    !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
6823c0d9c63SKent Gibson 		return -EINVAL;
6833c0d9c63SKent Gibson 
6843c0d9c63SKent Gibson 	/* Only one bias flag can be set. */
6853c0d9c63SKent Gibson 	if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
6863c0d9c63SKent Gibson 	     (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
6873c0d9c63SKent Gibson 		       GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
6883c0d9c63SKent Gibson 	    ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
6893c0d9c63SKent Gibson 	     (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
6903c0d9c63SKent Gibson 		return -EINVAL;
6913c0d9c63SKent Gibson 
6923c0d9c63SKent Gibson 	return 0;
6933c0d9c63SKent Gibson }
6943c0d9c63SKent Gibson 
6953c0d9c63SKent Gibson static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
6963c0d9c63SKent Gibson 					unsigned int num_lines)
6973c0d9c63SKent Gibson {
6983c0d9c63SKent Gibson 	unsigned int i;
6993c0d9c63SKent Gibson 	u64 flags;
7003c0d9c63SKent Gibson 	int ret;
7013c0d9c63SKent Gibson 
7023c0d9c63SKent Gibson 	if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
7033c0d9c63SKent Gibson 		return -EINVAL;
7043c0d9c63SKent Gibson 
7053c0d9c63SKent Gibson 	if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
7063c0d9c63SKent Gibson 		return -EINVAL;
7073c0d9c63SKent Gibson 
7083c0d9c63SKent Gibson 	for (i = 0; i < num_lines; i++) {
7093c0d9c63SKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
7103c0d9c63SKent Gibson 		ret = gpio_v2_line_flags_validate(flags);
7113c0d9c63SKent Gibson 		if (ret)
7123c0d9c63SKent Gibson 			return ret;
7133c0d9c63SKent Gibson 	}
7143c0d9c63SKent Gibson 	return 0;
7153c0d9c63SKent Gibson }
7163c0d9c63SKent Gibson 
7173c0d9c63SKent Gibson static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
7183c0d9c63SKent Gibson 						    unsigned long *flagsp)
7193c0d9c63SKent Gibson {
7203c0d9c63SKent Gibson 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
7213c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
7223c0d9c63SKent Gibson 
7233c0d9c63SKent Gibson 	if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
7243c0d9c63SKent Gibson 		set_bit(FLAG_IS_OUT, flagsp);
7253c0d9c63SKent Gibson 	else if (flags & GPIO_V2_LINE_FLAG_INPUT)
7263c0d9c63SKent Gibson 		clear_bit(FLAG_IS_OUT, flagsp);
7273c0d9c63SKent Gibson 
728*73e03419SKent Gibson 	assign_bit(FLAG_EDGE_RISING, flagsp,
729*73e03419SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
730*73e03419SKent Gibson 	assign_bit(FLAG_EDGE_FALLING, flagsp,
731*73e03419SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
732*73e03419SKent Gibson 
7333c0d9c63SKent Gibson 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
7343c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
7353c0d9c63SKent Gibson 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
7363c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
7373c0d9c63SKent Gibson 
7383c0d9c63SKent Gibson 	assign_bit(FLAG_PULL_UP, flagsp,
7393c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
7403c0d9c63SKent Gibson 	assign_bit(FLAG_PULL_DOWN, flagsp,
7413c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
7423c0d9c63SKent Gibson 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
7433c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
7443c0d9c63SKent Gibson }
7453c0d9c63SKent Gibson 
7463c0d9c63SKent Gibson static long linereq_get_values(struct linereq *lr, void __user *ip)
7473c0d9c63SKent Gibson {
7483c0d9c63SKent Gibson 	struct gpio_v2_line_values lv;
7493c0d9c63SKent Gibson 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
7503c0d9c63SKent Gibson 	struct gpio_desc **descs;
7513c0d9c63SKent Gibson 	unsigned int i, didx, num_get;
7523c0d9c63SKent Gibson 	int ret;
7533c0d9c63SKent Gibson 
7543c0d9c63SKent Gibson 	/* NOTE: It's ok to read values of output lines. */
7553c0d9c63SKent Gibson 	if (copy_from_user(&lv, ip, sizeof(lv)))
7563c0d9c63SKent Gibson 		return -EFAULT;
7573c0d9c63SKent Gibson 
7583c0d9c63SKent Gibson 	for (num_get = 0, i = 0; i < lr->num_lines; i++) {
7593c0d9c63SKent Gibson 		if (lv.mask & BIT_ULL(i)) {
7603c0d9c63SKent Gibson 			num_get++;
7613c0d9c63SKent Gibson 			descs = &lr->lines[i].desc;
7623c0d9c63SKent Gibson 		}
7633c0d9c63SKent Gibson 	}
7643c0d9c63SKent Gibson 
7653c0d9c63SKent Gibson 	if (num_get == 0)
7663c0d9c63SKent Gibson 		return -EINVAL;
7673c0d9c63SKent Gibson 
7683c0d9c63SKent Gibson 	if (num_get != 1) {
7693c0d9c63SKent Gibson 		descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
7703c0d9c63SKent Gibson 		if (!descs)
7713c0d9c63SKent Gibson 			return -ENOMEM;
7723c0d9c63SKent Gibson 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
7733c0d9c63SKent Gibson 			if (lv.mask & BIT_ULL(i)) {
7743c0d9c63SKent Gibson 				descs[didx] = lr->lines[i].desc;
7753c0d9c63SKent Gibson 				didx++;
7763c0d9c63SKent Gibson 			}
7773c0d9c63SKent Gibson 		}
7783c0d9c63SKent Gibson 	}
7793c0d9c63SKent Gibson 	ret = gpiod_get_array_value_complex(false, true, num_get,
7803c0d9c63SKent Gibson 					    descs, NULL, vals);
7813c0d9c63SKent Gibson 
7823c0d9c63SKent Gibson 	if (num_get != 1)
7833c0d9c63SKent Gibson 		kfree(descs);
7843c0d9c63SKent Gibson 	if (ret)
7853c0d9c63SKent Gibson 		return ret;
7863c0d9c63SKent Gibson 
7873c0d9c63SKent Gibson 	lv.bits = 0;
7883c0d9c63SKent Gibson 	for (didx = 0, i = 0; i < lr->num_lines; i++) {
7893c0d9c63SKent Gibson 		if (lv.mask & BIT_ULL(i)) {
7903c0d9c63SKent Gibson 			if (test_bit(didx, vals))
7913c0d9c63SKent Gibson 				lv.bits |= BIT_ULL(i);
7923c0d9c63SKent Gibson 			didx++;
7933c0d9c63SKent Gibson 		}
7943c0d9c63SKent Gibson 	}
7953c0d9c63SKent Gibson 
7963c0d9c63SKent Gibson 	if (copy_to_user(ip, &lv, sizeof(lv)))
7973c0d9c63SKent Gibson 		return -EFAULT;
7983c0d9c63SKent Gibson 
7993c0d9c63SKent Gibson 	return 0;
8003c0d9c63SKent Gibson }
8013c0d9c63SKent Gibson 
8023c0d9c63SKent Gibson static long linereq_ioctl(struct file *file, unsigned int cmd,
8033c0d9c63SKent Gibson 			  unsigned long arg)
8043c0d9c63SKent Gibson {
8053c0d9c63SKent Gibson 	struct linereq *lr = file->private_data;
8063c0d9c63SKent Gibson 	void __user *ip = (void __user *)arg;
8073c0d9c63SKent Gibson 
8083c0d9c63SKent Gibson 	if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL)
8093c0d9c63SKent Gibson 		return linereq_get_values(lr, ip);
8103c0d9c63SKent Gibson 
8113c0d9c63SKent Gibson 	return -EINVAL;
8123c0d9c63SKent Gibson }
8133c0d9c63SKent Gibson 
8143c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
8153c0d9c63SKent Gibson static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
8163c0d9c63SKent Gibson 				 unsigned long arg)
8173c0d9c63SKent Gibson {
8183c0d9c63SKent Gibson 	return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
8193c0d9c63SKent Gibson }
8203c0d9c63SKent Gibson #endif
8213c0d9c63SKent Gibson 
822*73e03419SKent Gibson static __poll_t linereq_poll(struct file *file,
823*73e03419SKent Gibson 			    struct poll_table_struct *wait)
824*73e03419SKent Gibson {
825*73e03419SKent Gibson 	struct linereq *lr = file->private_data;
826*73e03419SKent Gibson 	__poll_t events = 0;
827*73e03419SKent Gibson 
828*73e03419SKent Gibson 	poll_wait(file, &lr->wait, wait);
829*73e03419SKent Gibson 
830*73e03419SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
831*73e03419SKent Gibson 						 &lr->wait.lock))
832*73e03419SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
833*73e03419SKent Gibson 
834*73e03419SKent Gibson 	return events;
835*73e03419SKent Gibson }
836*73e03419SKent Gibson 
837*73e03419SKent Gibson static ssize_t linereq_read(struct file *file,
838*73e03419SKent Gibson 			    char __user *buf,
839*73e03419SKent Gibson 			    size_t count,
840*73e03419SKent Gibson 			    loff_t *f_ps)
841*73e03419SKent Gibson {
842*73e03419SKent Gibson 	struct linereq *lr = file->private_data;
843*73e03419SKent Gibson 	struct gpio_v2_line_event le;
844*73e03419SKent Gibson 	ssize_t bytes_read = 0;
845*73e03419SKent Gibson 	int ret;
846*73e03419SKent Gibson 
847*73e03419SKent Gibson 	if (count < sizeof(le))
848*73e03419SKent Gibson 		return -EINVAL;
849*73e03419SKent Gibson 
850*73e03419SKent Gibson 	do {
851*73e03419SKent Gibson 		spin_lock(&lr->wait.lock);
852*73e03419SKent Gibson 		if (kfifo_is_empty(&lr->events)) {
853*73e03419SKent Gibson 			if (bytes_read) {
854*73e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
855*73e03419SKent Gibson 				return bytes_read;
856*73e03419SKent Gibson 			}
857*73e03419SKent Gibson 
858*73e03419SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
859*73e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
860*73e03419SKent Gibson 				return -EAGAIN;
861*73e03419SKent Gibson 			}
862*73e03419SKent Gibson 
863*73e03419SKent Gibson 			ret = wait_event_interruptible_locked(lr->wait,
864*73e03419SKent Gibson 					!kfifo_is_empty(&lr->events));
865*73e03419SKent Gibson 			if (ret) {
866*73e03419SKent Gibson 				spin_unlock(&lr->wait.lock);
867*73e03419SKent Gibson 				return ret;
868*73e03419SKent Gibson 			}
869*73e03419SKent Gibson 		}
870*73e03419SKent Gibson 
871*73e03419SKent Gibson 		ret = kfifo_out(&lr->events, &le, 1);
872*73e03419SKent Gibson 		spin_unlock(&lr->wait.lock);
873*73e03419SKent Gibson 		if (ret != 1) {
874*73e03419SKent Gibson 			/*
875*73e03419SKent Gibson 			 * This should never happen - we were holding the
876*73e03419SKent Gibson 			 * lock from the moment we learned the fifo is no
877*73e03419SKent Gibson 			 * longer empty until now.
878*73e03419SKent Gibson 			 */
879*73e03419SKent Gibson 			ret = -EIO;
880*73e03419SKent Gibson 			break;
881*73e03419SKent Gibson 		}
882*73e03419SKent Gibson 
883*73e03419SKent Gibson 		if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
884*73e03419SKent Gibson 			return -EFAULT;
885*73e03419SKent Gibson 		bytes_read += sizeof(le);
886*73e03419SKent Gibson 	} while (count >= bytes_read + sizeof(le));
887*73e03419SKent Gibson 
888*73e03419SKent Gibson 	return bytes_read;
889*73e03419SKent Gibson }
890*73e03419SKent Gibson 
8913c0d9c63SKent Gibson static void linereq_free(struct linereq *lr)
8923c0d9c63SKent Gibson {
8933c0d9c63SKent Gibson 	unsigned int i;
8943c0d9c63SKent Gibson 
8953c0d9c63SKent Gibson 	for (i = 0; i < lr->num_lines; i++) {
896*73e03419SKent Gibson 		edge_detector_stop(&lr->lines[i]);
8973c0d9c63SKent Gibson 		if (lr->lines[i].desc)
8983c0d9c63SKent Gibson 			gpiod_free(lr->lines[i].desc);
8993c0d9c63SKent Gibson 	}
900*73e03419SKent Gibson 	kfifo_free(&lr->events);
9013c0d9c63SKent Gibson 	kfree(lr->label);
9023c0d9c63SKent Gibson 	put_device(&lr->gdev->dev);
9033c0d9c63SKent Gibson 	kfree(lr);
9043c0d9c63SKent Gibson }
9053c0d9c63SKent Gibson 
9063c0d9c63SKent Gibson static int linereq_release(struct inode *inode, struct file *file)
9073c0d9c63SKent Gibson {
9083c0d9c63SKent Gibson 	struct linereq *lr = file->private_data;
9093c0d9c63SKent Gibson 
9103c0d9c63SKent Gibson 	linereq_free(lr);
9113c0d9c63SKent Gibson 	return 0;
9123c0d9c63SKent Gibson }
9133c0d9c63SKent Gibson 
9143c0d9c63SKent Gibson static const struct file_operations line_fileops = {
9153c0d9c63SKent Gibson 	.release = linereq_release,
916*73e03419SKent Gibson 	.read = linereq_read,
917*73e03419SKent Gibson 	.poll = linereq_poll,
9183c0d9c63SKent Gibson 	.owner = THIS_MODULE,
9193c0d9c63SKent Gibson 	.llseek = noop_llseek,
9203c0d9c63SKent Gibson 	.unlocked_ioctl = linereq_ioctl,
9213c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
9223c0d9c63SKent Gibson 	.compat_ioctl = linereq_ioctl_compat,
9233c0d9c63SKent Gibson #endif
9243c0d9c63SKent Gibson };
9253c0d9c63SKent Gibson 
9263c0d9c63SKent Gibson static int linereq_create(struct gpio_device *gdev, void __user *ip)
9273c0d9c63SKent Gibson {
9283c0d9c63SKent Gibson 	struct gpio_v2_line_request ulr;
9293c0d9c63SKent Gibson 	struct gpio_v2_line_config *lc;
9303c0d9c63SKent Gibson 	struct linereq *lr;
9313c0d9c63SKent Gibson 	struct file *file;
9323c0d9c63SKent Gibson 	u64 flags;
9333c0d9c63SKent Gibson 	unsigned int i;
9343c0d9c63SKent Gibson 	int fd, ret;
9353c0d9c63SKent Gibson 
9363c0d9c63SKent Gibson 	if (copy_from_user(&ulr, ip, sizeof(ulr)))
9373c0d9c63SKent Gibson 		return -EFAULT;
9383c0d9c63SKent Gibson 
9393c0d9c63SKent Gibson 	if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
9403c0d9c63SKent Gibson 		return -EINVAL;
9413c0d9c63SKent Gibson 
9423c0d9c63SKent Gibson 	if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
9433c0d9c63SKent Gibson 		return -EINVAL;
9443c0d9c63SKent Gibson 
9453c0d9c63SKent Gibson 	lc = &ulr.config;
9463c0d9c63SKent Gibson 	ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
9473c0d9c63SKent Gibson 	if (ret)
9483c0d9c63SKent Gibson 		return ret;
9493c0d9c63SKent Gibson 
9503c0d9c63SKent Gibson 	lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
9513c0d9c63SKent Gibson 	if (!lr)
9523c0d9c63SKent Gibson 		return -ENOMEM;
9533c0d9c63SKent Gibson 
9543c0d9c63SKent Gibson 	lr->gdev = gdev;
9553c0d9c63SKent Gibson 	get_device(&gdev->dev);
9563c0d9c63SKent Gibson 
957*73e03419SKent Gibson 	for (i = 0; i < ulr.num_lines; i++)
958*73e03419SKent Gibson 		lr->lines[i].req = lr;
959*73e03419SKent Gibson 
9603c0d9c63SKent Gibson 	/* Make sure this is terminated */
9613c0d9c63SKent Gibson 	ulr.consumer[sizeof(ulr.consumer)-1] = '\0';
9623c0d9c63SKent Gibson 	if (strlen(ulr.consumer)) {
9633c0d9c63SKent Gibson 		/* label is only initialized if consumer is set */
9643c0d9c63SKent Gibson 		lr->label = kstrdup(ulr.consumer, GFP_KERNEL);
9653c0d9c63SKent Gibson 		if (!lr->label) {
9663c0d9c63SKent Gibson 			ret = -ENOMEM;
9673c0d9c63SKent Gibson 			goto out_free_linereq;
9683c0d9c63SKent Gibson 		}
9693c0d9c63SKent Gibson 	}
9703c0d9c63SKent Gibson 
971*73e03419SKent Gibson 	init_waitqueue_head(&lr->wait);
972*73e03419SKent Gibson 	lr->event_buffer_size = ulr.event_buffer_size;
973*73e03419SKent Gibson 	if (lr->event_buffer_size == 0)
974*73e03419SKent Gibson 		lr->event_buffer_size = ulr.num_lines * 16;
975*73e03419SKent Gibson 	else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
976*73e03419SKent Gibson 		lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
977*73e03419SKent Gibson 
978*73e03419SKent Gibson 	atomic_set(&lr->seqno, 0);
9793c0d9c63SKent Gibson 	lr->num_lines = ulr.num_lines;
9803c0d9c63SKent Gibson 
9813c0d9c63SKent Gibson 	/* Request each GPIO */
9823c0d9c63SKent Gibson 	for (i = 0; i < ulr.num_lines; i++) {
9833c0d9c63SKent Gibson 		u32 offset = ulr.offsets[i];
9843c0d9c63SKent Gibson 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
9853c0d9c63SKent Gibson 
9863c0d9c63SKent Gibson 		if (IS_ERR(desc)) {
9873c0d9c63SKent Gibson 			ret = PTR_ERR(desc);
9883c0d9c63SKent Gibson 			goto out_free_linereq;
9893c0d9c63SKent Gibson 		}
9903c0d9c63SKent Gibson 
9913c0d9c63SKent Gibson 		ret = gpiod_request(desc, lr->label);
9923c0d9c63SKent Gibson 		if (ret)
9933c0d9c63SKent Gibson 			goto out_free_linereq;
9943c0d9c63SKent Gibson 
9953c0d9c63SKent Gibson 		lr->lines[i].desc = desc;
9963c0d9c63SKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
9973c0d9c63SKent Gibson 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
9983c0d9c63SKent Gibson 
9993c0d9c63SKent Gibson 		ret = gpiod_set_transitory(desc, false);
10003c0d9c63SKent Gibson 		if (ret < 0)
10013c0d9c63SKent Gibson 			goto out_free_linereq;
10023c0d9c63SKent Gibson 
10033c0d9c63SKent Gibson 		/*
10043c0d9c63SKent Gibson 		 * Lines have to be requested explicitly for input
10053c0d9c63SKent Gibson 		 * or output, else the line will be treated "as is".
10063c0d9c63SKent Gibson 		 */
10073c0d9c63SKent Gibson 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
10083c0d9c63SKent Gibson 			int val = gpio_v2_line_config_output_value(lc, i);
10093c0d9c63SKent Gibson 
10103c0d9c63SKent Gibson 			ret = gpiod_direction_output(desc, val);
10113c0d9c63SKent Gibson 			if (ret)
10123c0d9c63SKent Gibson 				goto out_free_linereq;
10133c0d9c63SKent Gibson 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
10143c0d9c63SKent Gibson 			ret = gpiod_direction_input(desc);
10153c0d9c63SKent Gibson 			if (ret)
10163c0d9c63SKent Gibson 				goto out_free_linereq;
1017*73e03419SKent Gibson 
1018*73e03419SKent Gibson 			ret = edge_detector_setup(&lr->lines[i],
1019*73e03419SKent Gibson 					flags & GPIO_V2_LINE_EDGE_FLAGS);
1020*73e03419SKent Gibson 			if (ret)
1021*73e03419SKent Gibson 				goto out_free_linereq;
10223c0d9c63SKent Gibson 		}
10233c0d9c63SKent Gibson 
10243c0d9c63SKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
1025aad95584SKent Gibson 					     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
10263c0d9c63SKent Gibson 
10273c0d9c63SKent Gibson 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
10283c0d9c63SKent Gibson 			offset);
10293c0d9c63SKent Gibson 	}
10303c0d9c63SKent Gibson 
10313c0d9c63SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
10323c0d9c63SKent Gibson 	if (fd < 0) {
10333c0d9c63SKent Gibson 		ret = fd;
10343c0d9c63SKent Gibson 		goto out_free_linereq;
10353c0d9c63SKent Gibson 	}
10363c0d9c63SKent Gibson 
10373c0d9c63SKent Gibson 	file = anon_inode_getfile("gpio-line", &line_fileops, lr,
10383c0d9c63SKent Gibson 				  O_RDONLY | O_CLOEXEC);
10393c0d9c63SKent Gibson 	if (IS_ERR(file)) {
10403c0d9c63SKent Gibson 		ret = PTR_ERR(file);
10413c0d9c63SKent Gibson 		goto out_put_unused_fd;
10423c0d9c63SKent Gibson 	}
10433c0d9c63SKent Gibson 
10443c0d9c63SKent Gibson 	ulr.fd = fd;
10453c0d9c63SKent Gibson 	if (copy_to_user(ip, &ulr, sizeof(ulr))) {
10463c0d9c63SKent Gibson 		/*
10473c0d9c63SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
10483c0d9c63SKent Gibson 		 * the regular error cleanup path here.
10493c0d9c63SKent Gibson 		 */
10503c0d9c63SKent Gibson 		fput(file);
10513c0d9c63SKent Gibson 		put_unused_fd(fd);
10523c0d9c63SKent Gibson 		return -EFAULT;
10533c0d9c63SKent Gibson 	}
10543c0d9c63SKent Gibson 
10553c0d9c63SKent Gibson 	fd_install(fd, file);
10563c0d9c63SKent Gibson 
10573c0d9c63SKent Gibson 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
10583c0d9c63SKent Gibson 		lr->num_lines);
10593c0d9c63SKent Gibson 
10603c0d9c63SKent Gibson 	return 0;
10613c0d9c63SKent Gibson 
10623c0d9c63SKent Gibson out_put_unused_fd:
10633c0d9c63SKent Gibson 	put_unused_fd(fd);
10643c0d9c63SKent Gibson out_free_linereq:
10653c0d9c63SKent Gibson 	linereq_free(lr);
10663c0d9c63SKent Gibson 	return ret;
10673c0d9c63SKent Gibson }
10683c0d9c63SKent Gibson 
10693c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1070925ca369SKent Gibson 
1071925ca369SKent Gibson /*
1072925ca369SKent Gibson  * GPIO line event management
1073925ca369SKent Gibson  */
1074925ca369SKent Gibson 
1075925ca369SKent Gibson /**
1076925ca369SKent Gibson  * struct lineevent_state - contains the state of a userspace event
1077925ca369SKent Gibson  * @gdev: the GPIO device the event pertains to
1078925ca369SKent Gibson  * @label: consumer label used to tag descriptors
1079925ca369SKent Gibson  * @desc: the GPIO descriptor held by this event
1080925ca369SKent Gibson  * @eflags: the event flags this line was requested with
1081925ca369SKent Gibson  * @irq: the interrupt that trigger in response to events on this GPIO
1082925ca369SKent Gibson  * @wait: wait queue that handles blocking reads of events
1083925ca369SKent Gibson  * @events: KFIFO for the GPIO events
1084925ca369SKent Gibson  * @timestamp: cache for the timestamp storing it between hardirq
1085925ca369SKent Gibson  * and IRQ thread, used to bring the timestamp close to the actual
1086925ca369SKent Gibson  * event
1087925ca369SKent Gibson  */
1088925ca369SKent Gibson struct lineevent_state {
1089925ca369SKent Gibson 	struct gpio_device *gdev;
1090925ca369SKent Gibson 	const char *label;
1091925ca369SKent Gibson 	struct gpio_desc *desc;
1092925ca369SKent Gibson 	u32 eflags;
1093925ca369SKent Gibson 	int irq;
1094925ca369SKent Gibson 	wait_queue_head_t wait;
1095925ca369SKent Gibson 	DECLARE_KFIFO(events, struct gpioevent_data, 16);
1096925ca369SKent Gibson 	u64 timestamp;
1097925ca369SKent Gibson };
1098925ca369SKent Gibson 
1099925ca369SKent Gibson #define GPIOEVENT_REQUEST_VALID_FLAGS \
1100925ca369SKent Gibson 	(GPIOEVENT_REQUEST_RISING_EDGE | \
1101925ca369SKent Gibson 	GPIOEVENT_REQUEST_FALLING_EDGE)
1102925ca369SKent Gibson 
110349bc5279SKent Gibson static __poll_t lineevent_poll(struct file *file,
1104925ca369SKent Gibson 			       struct poll_table_struct *wait)
1105925ca369SKent Gibson {
110649bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1107925ca369SKent Gibson 	__poll_t events = 0;
1108925ca369SKent Gibson 
110949bc5279SKent Gibson 	poll_wait(file, &le->wait, wait);
1110925ca369SKent Gibson 
1111925ca369SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1112925ca369SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
1113925ca369SKent Gibson 
1114925ca369SKent Gibson 	return events;
1115925ca369SKent Gibson }
1116925ca369SKent Gibson 
1117925ca369SKent Gibson 
111849bc5279SKent Gibson static ssize_t lineevent_read(struct file *file,
1119925ca369SKent Gibson 			      char __user *buf,
1120925ca369SKent Gibson 			      size_t count,
1121925ca369SKent Gibson 			      loff_t *f_ps)
1122925ca369SKent Gibson {
112349bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1124925ca369SKent Gibson 	struct gpioevent_data ge;
1125925ca369SKent Gibson 	ssize_t bytes_read = 0;
1126925ca369SKent Gibson 	int ret;
1127925ca369SKent Gibson 
1128925ca369SKent Gibson 	if (count < sizeof(ge))
1129925ca369SKent Gibson 		return -EINVAL;
1130925ca369SKent Gibson 
1131925ca369SKent Gibson 	do {
1132925ca369SKent Gibson 		spin_lock(&le->wait.lock);
1133925ca369SKent Gibson 		if (kfifo_is_empty(&le->events)) {
1134925ca369SKent Gibson 			if (bytes_read) {
1135925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1136925ca369SKent Gibson 				return bytes_read;
1137925ca369SKent Gibson 			}
1138925ca369SKent Gibson 
113949bc5279SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
1140925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1141925ca369SKent Gibson 				return -EAGAIN;
1142925ca369SKent Gibson 			}
1143925ca369SKent Gibson 
1144925ca369SKent Gibson 			ret = wait_event_interruptible_locked(le->wait,
1145925ca369SKent Gibson 					!kfifo_is_empty(&le->events));
1146925ca369SKent Gibson 			if (ret) {
1147925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
1148925ca369SKent Gibson 				return ret;
1149925ca369SKent Gibson 			}
1150925ca369SKent Gibson 		}
1151925ca369SKent Gibson 
1152925ca369SKent Gibson 		ret = kfifo_out(&le->events, &ge, 1);
1153925ca369SKent Gibson 		spin_unlock(&le->wait.lock);
1154925ca369SKent Gibson 		if (ret != 1) {
1155925ca369SKent Gibson 			/*
1156925ca369SKent Gibson 			 * This should never happen - we were holding the lock
1157925ca369SKent Gibson 			 * from the moment we learned the fifo is no longer
1158925ca369SKent Gibson 			 * empty until now.
1159925ca369SKent Gibson 			 */
1160925ca369SKent Gibson 			ret = -EIO;
1161925ca369SKent Gibson 			break;
1162925ca369SKent Gibson 		}
1163925ca369SKent Gibson 
1164925ca369SKent Gibson 		if (copy_to_user(buf + bytes_read, &ge, sizeof(ge)))
1165925ca369SKent Gibson 			return -EFAULT;
1166925ca369SKent Gibson 		bytes_read += sizeof(ge);
1167925ca369SKent Gibson 	} while (count >= bytes_read + sizeof(ge));
1168925ca369SKent Gibson 
1169925ca369SKent Gibson 	return bytes_read;
1170925ca369SKent Gibson }
1171925ca369SKent Gibson 
117246824272SKent Gibson static void lineevent_free(struct lineevent_state *le)
1173925ca369SKent Gibson {
117446824272SKent Gibson 	if (le->irq)
1175925ca369SKent Gibson 		free_irq(le->irq, le);
117646824272SKent Gibson 	if (le->desc)
1177925ca369SKent Gibson 		gpiod_free(le->desc);
1178925ca369SKent Gibson 	kfree(le->label);
117946824272SKent Gibson 	put_device(&le->gdev->dev);
1180925ca369SKent Gibson 	kfree(le);
118146824272SKent Gibson }
118246824272SKent Gibson 
118346824272SKent Gibson static int lineevent_release(struct inode *inode, struct file *file)
118446824272SKent Gibson {
118546824272SKent Gibson 	lineevent_free(file->private_data);
1186925ca369SKent Gibson 	return 0;
1187925ca369SKent Gibson }
1188925ca369SKent Gibson 
118949bc5279SKent Gibson static long lineevent_ioctl(struct file *file, unsigned int cmd,
1190925ca369SKent Gibson 			    unsigned long arg)
1191925ca369SKent Gibson {
119249bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
1193925ca369SKent Gibson 	void __user *ip = (void __user *)arg;
1194925ca369SKent Gibson 	struct gpiohandle_data ghd;
1195925ca369SKent Gibson 
1196925ca369SKent Gibson 	/*
1197925ca369SKent Gibson 	 * We can get the value for an event line but not set it,
1198925ca369SKent Gibson 	 * because it is input by definition.
1199925ca369SKent Gibson 	 */
1200925ca369SKent Gibson 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
1201925ca369SKent Gibson 		int val;
1202925ca369SKent Gibson 
1203925ca369SKent Gibson 		memset(&ghd, 0, sizeof(ghd));
1204925ca369SKent Gibson 
1205925ca369SKent Gibson 		val = gpiod_get_value_cansleep(le->desc);
1206925ca369SKent Gibson 		if (val < 0)
1207925ca369SKent Gibson 			return val;
1208925ca369SKent Gibson 		ghd.values[0] = val;
1209925ca369SKent Gibson 
1210925ca369SKent Gibson 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
1211925ca369SKent Gibson 			return -EFAULT;
1212925ca369SKent Gibson 
1213925ca369SKent Gibson 		return 0;
1214925ca369SKent Gibson 	}
1215925ca369SKent Gibson 	return -EINVAL;
1216925ca369SKent Gibson }
1217925ca369SKent Gibson 
1218925ca369SKent Gibson #ifdef CONFIG_COMPAT
121949bc5279SKent Gibson static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
1220925ca369SKent Gibson 				   unsigned long arg)
1221925ca369SKent Gibson {
122249bc5279SKent Gibson 	return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1223925ca369SKent Gibson }
1224925ca369SKent Gibson #endif
1225925ca369SKent Gibson 
1226925ca369SKent Gibson static const struct file_operations lineevent_fileops = {
1227925ca369SKent Gibson 	.release = lineevent_release,
1228925ca369SKent Gibson 	.read = lineevent_read,
1229925ca369SKent Gibson 	.poll = lineevent_poll,
1230925ca369SKent Gibson 	.owner = THIS_MODULE,
1231925ca369SKent Gibson 	.llseek = noop_llseek,
1232925ca369SKent Gibson 	.unlocked_ioctl = lineevent_ioctl,
1233925ca369SKent Gibson #ifdef CONFIG_COMPAT
1234925ca369SKent Gibson 	.compat_ioctl = lineevent_ioctl_compat,
1235925ca369SKent Gibson #endif
1236925ca369SKent Gibson };
1237925ca369SKent Gibson 
1238925ca369SKent Gibson static irqreturn_t lineevent_irq_thread(int irq, void *p)
1239925ca369SKent Gibson {
1240925ca369SKent Gibson 	struct lineevent_state *le = p;
1241925ca369SKent Gibson 	struct gpioevent_data ge;
1242925ca369SKent Gibson 	int ret;
1243925ca369SKent Gibson 
1244925ca369SKent Gibson 	/* Do not leak kernel stack to userspace */
1245925ca369SKent Gibson 	memset(&ge, 0, sizeof(ge));
1246925ca369SKent Gibson 
1247925ca369SKent Gibson 	/*
1248925ca369SKent Gibson 	 * We may be running from a nested threaded interrupt in which case
1249925ca369SKent Gibson 	 * we didn't get the timestamp from lineevent_irq_handler().
1250925ca369SKent Gibson 	 */
1251925ca369SKent Gibson 	if (!le->timestamp)
1252925ca369SKent Gibson 		ge.timestamp = ktime_get_ns();
1253925ca369SKent Gibson 	else
1254925ca369SKent Gibson 		ge.timestamp = le->timestamp;
1255925ca369SKent Gibson 
1256925ca369SKent Gibson 	if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
1257925ca369SKent Gibson 	    && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1258925ca369SKent Gibson 		int level = gpiod_get_value_cansleep(le->desc);
1259925ca369SKent Gibson 
1260925ca369SKent Gibson 		if (level)
1261925ca369SKent Gibson 			/* Emit low-to-high event */
1262925ca369SKent Gibson 			ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1263925ca369SKent Gibson 		else
1264925ca369SKent Gibson 			/* Emit high-to-low event */
1265925ca369SKent Gibson 			ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1266925ca369SKent Gibson 	} else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
1267925ca369SKent Gibson 		/* Emit low-to-high event */
1268925ca369SKent Gibson 		ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1269925ca369SKent Gibson 	} else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1270925ca369SKent Gibson 		/* Emit high-to-low event */
1271925ca369SKent Gibson 		ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1272925ca369SKent Gibson 	} else {
1273925ca369SKent Gibson 		return IRQ_NONE;
1274925ca369SKent Gibson 	}
1275925ca369SKent Gibson 
1276925ca369SKent Gibson 	ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
1277925ca369SKent Gibson 					    1, &le->wait.lock);
1278925ca369SKent Gibson 	if (ret)
1279925ca369SKent Gibson 		wake_up_poll(&le->wait, EPOLLIN);
1280925ca369SKent Gibson 	else
1281925ca369SKent Gibson 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
1282925ca369SKent Gibson 
1283925ca369SKent Gibson 	return IRQ_HANDLED;
1284925ca369SKent Gibson }
1285925ca369SKent Gibson 
1286925ca369SKent Gibson static irqreturn_t lineevent_irq_handler(int irq, void *p)
1287925ca369SKent Gibson {
1288925ca369SKent Gibson 	struct lineevent_state *le = p;
1289925ca369SKent Gibson 
1290925ca369SKent Gibson 	/*
1291925ca369SKent Gibson 	 * Just store the timestamp in hardirq context so we get it as
1292925ca369SKent Gibson 	 * close in time as possible to the actual event.
1293925ca369SKent Gibson 	 */
1294925ca369SKent Gibson 	le->timestamp = ktime_get_ns();
1295925ca369SKent Gibson 
1296925ca369SKent Gibson 	return IRQ_WAKE_THREAD;
1297925ca369SKent Gibson }
1298925ca369SKent Gibson 
1299925ca369SKent Gibson static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1300925ca369SKent Gibson {
1301925ca369SKent Gibson 	struct gpioevent_request eventreq;
1302925ca369SKent Gibson 	struct lineevent_state *le;
1303925ca369SKent Gibson 	struct gpio_desc *desc;
1304925ca369SKent Gibson 	struct file *file;
1305925ca369SKent Gibson 	u32 offset;
1306925ca369SKent Gibson 	u32 lflags;
1307925ca369SKent Gibson 	u32 eflags;
1308925ca369SKent Gibson 	int fd;
1309925ca369SKent Gibson 	int ret;
131046824272SKent Gibson 	int irq, irqflags = 0;
1311925ca369SKent Gibson 
1312925ca369SKent Gibson 	if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1313925ca369SKent Gibson 		return -EFAULT;
1314925ca369SKent Gibson 
1315925ca369SKent Gibson 	offset = eventreq.lineoffset;
1316925ca369SKent Gibson 	lflags = eventreq.handleflags;
1317925ca369SKent Gibson 	eflags = eventreq.eventflags;
1318925ca369SKent Gibson 
1319925ca369SKent Gibson 	desc = gpiochip_get_desc(gdev->chip, offset);
1320925ca369SKent Gibson 	if (IS_ERR(desc))
1321925ca369SKent Gibson 		return PTR_ERR(desc);
1322925ca369SKent Gibson 
1323925ca369SKent Gibson 	/* Return an error if a unknown flag is set */
1324925ca369SKent Gibson 	if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1325925ca369SKent Gibson 	    (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1326925ca369SKent Gibson 		return -EINVAL;
1327925ca369SKent Gibson 
1328925ca369SKent Gibson 	/* This is just wrong: we don't look for events on output lines */
1329925ca369SKent Gibson 	if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1330925ca369SKent Gibson 	    (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1331925ca369SKent Gibson 	    (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1332925ca369SKent Gibson 		return -EINVAL;
1333925ca369SKent Gibson 
1334925ca369SKent Gibson 	/* Only one bias flag can be set. */
1335925ca369SKent Gibson 	if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1336925ca369SKent Gibson 	     (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1337925ca369SKent Gibson 			GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1338925ca369SKent Gibson 	    ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1339925ca369SKent Gibson 	     (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1340925ca369SKent Gibson 		return -EINVAL;
1341925ca369SKent Gibson 
1342925ca369SKent Gibson 	le = kzalloc(sizeof(*le), GFP_KERNEL);
1343925ca369SKent Gibson 	if (!le)
1344925ca369SKent Gibson 		return -ENOMEM;
1345925ca369SKent Gibson 	le->gdev = gdev;
1346925ca369SKent Gibson 	get_device(&gdev->dev);
1347925ca369SKent Gibson 
1348925ca369SKent Gibson 	/* Make sure this is terminated */
1349925ca369SKent Gibson 	eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
1350925ca369SKent Gibson 	if (strlen(eventreq.consumer_label)) {
1351925ca369SKent Gibson 		le->label = kstrdup(eventreq.consumer_label,
1352925ca369SKent Gibson 				    GFP_KERNEL);
1353925ca369SKent Gibson 		if (!le->label) {
1354925ca369SKent Gibson 			ret = -ENOMEM;
1355925ca369SKent Gibson 			goto out_free_le;
1356925ca369SKent Gibson 		}
1357925ca369SKent Gibson 	}
1358925ca369SKent Gibson 
1359925ca369SKent Gibson 	ret = gpiod_request(desc, le->label);
1360925ca369SKent Gibson 	if (ret)
136146824272SKent Gibson 		goto out_free_le;
1362925ca369SKent Gibson 	le->desc = desc;
1363925ca369SKent Gibson 	le->eflags = eflags;
1364925ca369SKent Gibson 
1365c274b58aSKent Gibson 	linehandle_flags_to_desc_flags(lflags, &desc->flags);
1366925ca369SKent Gibson 
1367925ca369SKent Gibson 	ret = gpiod_direction_input(desc);
1368925ca369SKent Gibson 	if (ret)
136946824272SKent Gibson 		goto out_free_le;
1370925ca369SKent Gibson 
13716accc376SKent Gibson 	blocking_notifier_call_chain(&desc->gdev->notifier,
1372aad95584SKent Gibson 				     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1373925ca369SKent Gibson 
137446824272SKent Gibson 	irq = gpiod_to_irq(desc);
137546824272SKent Gibson 	if (irq <= 0) {
1376925ca369SKent Gibson 		ret = -ENODEV;
137746824272SKent Gibson 		goto out_free_le;
1378925ca369SKent Gibson 	}
137946824272SKent Gibson 	le->irq = irq;
1380925ca369SKent Gibson 
1381925ca369SKent Gibson 	if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
1382925ca369SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1383925ca369SKent Gibson 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1384925ca369SKent Gibson 	if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
1385925ca369SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1386925ca369SKent Gibson 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1387925ca369SKent Gibson 	irqflags |= IRQF_ONESHOT;
1388925ca369SKent Gibson 
1389925ca369SKent Gibson 	INIT_KFIFO(le->events);
1390925ca369SKent Gibson 	init_waitqueue_head(&le->wait);
1391925ca369SKent Gibson 
1392925ca369SKent Gibson 	/* Request a thread to read the events */
1393925ca369SKent Gibson 	ret = request_threaded_irq(le->irq,
1394925ca369SKent Gibson 				   lineevent_irq_handler,
1395925ca369SKent Gibson 				   lineevent_irq_thread,
1396925ca369SKent Gibson 				   irqflags,
1397925ca369SKent Gibson 				   le->label,
1398925ca369SKent Gibson 				   le);
1399925ca369SKent Gibson 	if (ret)
140046824272SKent Gibson 		goto out_free_le;
1401925ca369SKent Gibson 
1402925ca369SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1403925ca369SKent Gibson 	if (fd < 0) {
1404925ca369SKent Gibson 		ret = fd;
140546824272SKent Gibson 		goto out_free_le;
1406925ca369SKent Gibson 	}
1407925ca369SKent Gibson 
1408925ca369SKent Gibson 	file = anon_inode_getfile("gpio-event",
1409925ca369SKent Gibson 				  &lineevent_fileops,
1410925ca369SKent Gibson 				  le,
1411925ca369SKent Gibson 				  O_RDONLY | O_CLOEXEC);
1412925ca369SKent Gibson 	if (IS_ERR(file)) {
1413925ca369SKent Gibson 		ret = PTR_ERR(file);
1414925ca369SKent Gibson 		goto out_put_unused_fd;
1415925ca369SKent Gibson 	}
1416925ca369SKent Gibson 
1417925ca369SKent Gibson 	eventreq.fd = fd;
1418925ca369SKent Gibson 	if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
1419925ca369SKent Gibson 		/*
1420925ca369SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
1421925ca369SKent Gibson 		 * the regular error cleanup path here.
1422925ca369SKent Gibson 		 */
1423925ca369SKent Gibson 		fput(file);
1424925ca369SKent Gibson 		put_unused_fd(fd);
1425925ca369SKent Gibson 		return -EFAULT;
1426925ca369SKent Gibson 	}
1427925ca369SKent Gibson 
1428925ca369SKent Gibson 	fd_install(fd, file);
1429925ca369SKent Gibson 
1430925ca369SKent Gibson 	return 0;
1431925ca369SKent Gibson 
1432925ca369SKent Gibson out_put_unused_fd:
1433925ca369SKent Gibson 	put_unused_fd(fd);
1434925ca369SKent Gibson out_free_le:
143546824272SKent Gibson 	lineevent_free(le);
1436925ca369SKent Gibson 	return ret;
1437925ca369SKent Gibson }
1438925ca369SKent Gibson 
1439aad95584SKent Gibson static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
1440aad95584SKent Gibson 				    struct gpioline_info *info_v1)
1441aad95584SKent Gibson {
1442aad95584SKent Gibson 	u64 flagsv2 = info_v2->flags;
1443aad95584SKent Gibson 
1444aad95584SKent Gibson 	memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
1445aad95584SKent Gibson 	memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
1446aad95584SKent Gibson 	info_v1->line_offset = info_v2->offset;
1447aad95584SKent Gibson 	info_v1->flags = 0;
1448aad95584SKent Gibson 
1449aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
1450aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_KERNEL;
1451aad95584SKent Gibson 
1452aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
1453aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
1454aad95584SKent Gibson 
1455aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
1456aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
1457aad95584SKent Gibson 
1458aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
1459aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
1460aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
1461aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
1462aad95584SKent Gibson 
1463aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
1464aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
1465aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
1466aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
1467aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
1468aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
1469aad95584SKent Gibson }
1470aad95584SKent Gibson 
1471aad95584SKent Gibson static void gpio_v2_line_info_changed_to_v1(
1472aad95584SKent Gibson 		struct gpio_v2_line_info_changed *lic_v2,
1473aad95584SKent Gibson 		struct gpioline_info_changed *lic_v1)
1474aad95584SKent Gibson {
1475aad95584SKent Gibson 	gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
1476aad95584SKent Gibson 	lic_v1->timestamp = lic_v2->timestamp_ns;
1477aad95584SKent Gibson 	lic_v1->event_type = lic_v2->event_type;
1478aad95584SKent Gibson }
1479aad95584SKent Gibson 
14803c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
14813c0d9c63SKent Gibson 
1482925ca369SKent Gibson static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
1483aad95584SKent Gibson 				  struct gpio_v2_line_info *info)
1484925ca369SKent Gibson {
1485925ca369SKent Gibson 	struct gpio_chip *gc = desc->gdev->chip;
1486925ca369SKent Gibson 	bool ok_for_pinctrl;
1487925ca369SKent Gibson 	unsigned long flags;
1488925ca369SKent Gibson 
148969e4e136SKent Gibson 	memset(info, 0, sizeof(*info));
1490aad95584SKent Gibson 	info->offset = gpio_chip_hwgpio(desc);
14910dc11e3aSKent Gibson 
1492925ca369SKent Gibson 	/*
1493925ca369SKent Gibson 	 * This function takes a mutex so we must check this before taking
1494925ca369SKent Gibson 	 * the spinlock.
1495925ca369SKent Gibson 	 *
1496925ca369SKent Gibson 	 * FIXME: find a non-racy way to retrieve this information. Maybe a
1497925ca369SKent Gibson 	 * lock common to both frameworks?
1498925ca369SKent Gibson 	 */
1499925ca369SKent Gibson 	ok_for_pinctrl =
1500aad95584SKent Gibson 		pinctrl_gpio_can_use_line(gc->base + info->offset);
1501925ca369SKent Gibson 
1502925ca369SKent Gibson 	spin_lock_irqsave(&gpio_lock, flags);
1503925ca369SKent Gibson 
150469e4e136SKent Gibson 	if (desc->name)
150569e4e136SKent Gibson 		strscpy(info->name, desc->name, sizeof(info->name));
1506925ca369SKent Gibson 
150769e4e136SKent Gibson 	if (desc->label)
150869e4e136SKent Gibson 		strscpy(info->consumer, desc->label, sizeof(info->consumer));
1509925ca369SKent Gibson 
1510925ca369SKent Gibson 	/*
1511925ca369SKent Gibson 	 * Userspace only need to know that the kernel is using this GPIO so
1512925ca369SKent Gibson 	 * it can't use it.
1513925ca369SKent Gibson 	 */
1514925ca369SKent Gibson 	info->flags = 0;
1515925ca369SKent Gibson 	if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1516925ca369SKent Gibson 	    test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1517925ca369SKent Gibson 	    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1518925ca369SKent Gibson 	    test_bit(FLAG_EXPORT, &desc->flags) ||
1519925ca369SKent Gibson 	    test_bit(FLAG_SYSFS, &desc->flags) ||
1520925ca369SKent Gibson 	    !ok_for_pinctrl)
1521aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_USED;
1522aad95584SKent Gibson 
1523925ca369SKent Gibson 	if (test_bit(FLAG_IS_OUT, &desc->flags))
1524aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
1525aad95584SKent Gibson 	else
1526aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_INPUT;
1527aad95584SKent Gibson 
1528925ca369SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1529aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
1530aad95584SKent Gibson 
1531925ca369SKent Gibson 	if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1532aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
1533925ca369SKent Gibson 	if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1534aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
1535aad95584SKent Gibson 
1536925ca369SKent Gibson 	if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
1537aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
1538925ca369SKent Gibson 	if (test_bit(FLAG_PULL_DOWN, &desc->flags))
1539aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
1540925ca369SKent Gibson 	if (test_bit(FLAG_PULL_UP, &desc->flags))
1541aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
1542925ca369SKent Gibson 
1543*73e03419SKent Gibson 	if (test_bit(FLAG_EDGE_RISING, &desc->flags))
1544*73e03419SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
1545*73e03419SKent Gibson 	if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
1546*73e03419SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
1547*73e03419SKent Gibson 
1548925ca369SKent Gibson 	spin_unlock_irqrestore(&gpio_lock, flags);
1549925ca369SKent Gibson }
1550925ca369SKent Gibson 
1551925ca369SKent Gibson struct gpio_chardev_data {
1552925ca369SKent Gibson 	struct gpio_device *gdev;
1553925ca369SKent Gibson 	wait_queue_head_t wait;
1554aad95584SKent Gibson 	DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
1555925ca369SKent Gibson 	struct notifier_block lineinfo_changed_nb;
1556925ca369SKent Gibson 	unsigned long *watched_lines;
1557aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1558aad95584SKent Gibson 	atomic_t watch_abi_version;
1559aad95584SKent Gibson #endif
1560925ca369SKent Gibson };
1561925ca369SKent Gibson 
1562aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1563aad95584SKent Gibson /*
1564aad95584SKent Gibson  * returns 0 if the versions match, else the previously selected ABI version
1565aad95584SKent Gibson  */
1566aad95584SKent Gibson static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
1567aad95584SKent Gibson 				       unsigned int version)
1568aad95584SKent Gibson {
1569aad95584SKent Gibson 	int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
1570aad95584SKent Gibson 
1571aad95584SKent Gibson 	if (abiv == version)
1572aad95584SKent Gibson 		return 0;
1573aad95584SKent Gibson 
1574aad95584SKent Gibson 	return abiv;
1575aad95584SKent Gibson }
1576aad95584SKent Gibson #endif
1577aad95584SKent Gibson 
1578aad95584SKent Gibson static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
1579aad95584SKent Gibson 			bool watch)
1580aad95584SKent Gibson {
1581aad95584SKent Gibson 	struct gpio_desc *desc;
1582aad95584SKent Gibson 	struct gpio_v2_line_info lineinfo;
1583aad95584SKent Gibson 
1584aad95584SKent Gibson 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1585aad95584SKent Gibson 		return -EFAULT;
1586aad95584SKent Gibson 
1587aad95584SKent Gibson 	if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
1588aad95584SKent Gibson 		return -EINVAL;
1589aad95584SKent Gibson 
1590aad95584SKent Gibson 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
1591aad95584SKent Gibson 	if (IS_ERR(desc))
1592aad95584SKent Gibson 		return PTR_ERR(desc);
1593aad95584SKent Gibson 
1594aad95584SKent Gibson 	if (watch) {
1595aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1596aad95584SKent Gibson 		if (lineinfo_ensure_abi_version(cdev, 2))
1597aad95584SKent Gibson 			return -EPERM;
1598aad95584SKent Gibson #endif
1599aad95584SKent Gibson 		if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
1600aad95584SKent Gibson 			return -EBUSY;
1601aad95584SKent Gibson 	}
1602aad95584SKent Gibson 	gpio_desc_to_lineinfo(desc, &lineinfo);
1603aad95584SKent Gibson 
1604aad95584SKent Gibson 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
1605aad95584SKent Gibson 		if (watch)
1606aad95584SKent Gibson 			clear_bit(lineinfo.offset, cdev->watched_lines);
1607aad95584SKent Gibson 		return -EFAULT;
1608aad95584SKent Gibson 	}
1609aad95584SKent Gibson 
1610aad95584SKent Gibson 	return 0;
1611aad95584SKent Gibson }
1612aad95584SKent Gibson 
1613925ca369SKent Gibson /*
1614925ca369SKent Gibson  * gpio_ioctl() - ioctl handler for the GPIO chardev
1615925ca369SKent Gibson  */
161649bc5279SKent Gibson static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1617925ca369SKent Gibson {
1618e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
1619e2b781c5SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
1620925ca369SKent Gibson 	struct gpio_chip *gc = gdev->chip;
1621925ca369SKent Gibson 	void __user *ip = (void __user *)arg;
1622925ca369SKent Gibson 	__u32 offset;
1623925ca369SKent Gibson 
1624925ca369SKent Gibson 	/* We fail any subsequent ioctl():s when the chip is gone */
1625925ca369SKent Gibson 	if (!gc)
1626925ca369SKent Gibson 		return -ENODEV;
1627925ca369SKent Gibson 
1628925ca369SKent Gibson 	/* Fill in the struct and pass to userspace */
1629925ca369SKent Gibson 	if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
1630925ca369SKent Gibson 		struct gpiochip_info chipinfo;
1631925ca369SKent Gibson 
1632925ca369SKent Gibson 		memset(&chipinfo, 0, sizeof(chipinfo));
1633925ca369SKent Gibson 
163469e4e136SKent Gibson 		strscpy(chipinfo.name, dev_name(&gdev->dev),
1635925ca369SKent Gibson 			sizeof(chipinfo.name));
163669e4e136SKent Gibson 		strscpy(chipinfo.label, gdev->label,
1637925ca369SKent Gibson 			sizeof(chipinfo.label));
1638925ca369SKent Gibson 		chipinfo.lines = gdev->ngpio;
1639925ca369SKent Gibson 		if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1640925ca369SKent Gibson 			return -EFAULT;
1641925ca369SKent Gibson 		return 0;
16423c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1643925ca369SKent Gibson 	} else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
1644aad95584SKent Gibson 		struct gpio_desc *desc;
1645925ca369SKent Gibson 		struct gpioline_info lineinfo;
1646aad95584SKent Gibson 		struct gpio_v2_line_info lineinfo_v2;
1647925ca369SKent Gibson 
1648925ca369SKent Gibson 		if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1649925ca369SKent Gibson 			return -EFAULT;
1650925ca369SKent Gibson 
16511bf7ba40SKent Gibson 		/* this doubles as a range check on line_offset */
1652925ca369SKent Gibson 		desc = gpiochip_get_desc(gc, lineinfo.line_offset);
1653925ca369SKent Gibson 		if (IS_ERR(desc))
1654925ca369SKent Gibson 			return PTR_ERR(desc);
1655925ca369SKent Gibson 
1656aad95584SKent Gibson 		gpio_desc_to_lineinfo(desc, &lineinfo_v2);
1657aad95584SKent Gibson 		gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
1658925ca369SKent Gibson 
1659925ca369SKent Gibson 		if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1660925ca369SKent Gibson 			return -EFAULT;
1661925ca369SKent Gibson 		return 0;
1662925ca369SKent Gibson 	} else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1663925ca369SKent Gibson 		return linehandle_create(gdev, ip);
1664925ca369SKent Gibson 	} else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1665925ca369SKent Gibson 		return lineevent_create(gdev, ip);
1666925ca369SKent Gibson 	} else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
1667aad95584SKent Gibson 		struct gpio_desc *desc;
1668925ca369SKent Gibson 		struct gpioline_info lineinfo;
1669aad95584SKent Gibson 		struct gpio_v2_line_info lineinfo_v2;
1670925ca369SKent Gibson 
1671925ca369SKent Gibson 		if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1672925ca369SKent Gibson 			return -EFAULT;
1673925ca369SKent Gibson 
16741bf7ba40SKent Gibson 		/* this doubles as a range check on line_offset */
1675925ca369SKent Gibson 		desc = gpiochip_get_desc(gc, lineinfo.line_offset);
1676925ca369SKent Gibson 		if (IS_ERR(desc))
1677925ca369SKent Gibson 			return PTR_ERR(desc);
1678925ca369SKent Gibson 
1679aad95584SKent Gibson 		if (lineinfo_ensure_abi_version(cdev, 1))
1680aad95584SKent Gibson 			return -EPERM;
1681aad95584SKent Gibson 
16821bf7ba40SKent Gibson 		if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
1683925ca369SKent Gibson 			return -EBUSY;
1684925ca369SKent Gibson 
1685aad95584SKent Gibson 		gpio_desc_to_lineinfo(desc, &lineinfo_v2);
1686aad95584SKent Gibson 		gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
1687925ca369SKent Gibson 
1688f30ef3e8SKent Gibson 		if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
16891bf7ba40SKent Gibson 			clear_bit(lineinfo.line_offset, cdev->watched_lines);
1690925ca369SKent Gibson 			return -EFAULT;
1691f30ef3e8SKent Gibson 		}
1692925ca369SKent Gibson 
1693925ca369SKent Gibson 		return 0;
16943c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
1695aad95584SKent Gibson 	} else if (cmd == GPIO_V2_GET_LINEINFO_IOCTL ||
1696aad95584SKent Gibson 		   cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL) {
1697aad95584SKent Gibson 		return lineinfo_get(cdev, ip,
1698aad95584SKent Gibson 				    cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL);
16993c0d9c63SKent Gibson 	} else if (cmd == GPIO_V2_GET_LINE_IOCTL) {
17003c0d9c63SKent Gibson 		return linereq_create(gdev, ip);
1701925ca369SKent Gibson 	} else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) {
1702925ca369SKent Gibson 		if (copy_from_user(&offset, ip, sizeof(offset)))
1703925ca369SKent Gibson 			return -EFAULT;
1704925ca369SKent Gibson 
17051bf7ba40SKent Gibson 		if (offset >= cdev->gdev->ngpio)
17061bf7ba40SKent Gibson 			return -EINVAL;
1707925ca369SKent Gibson 
17081bf7ba40SKent Gibson 		if (!test_and_clear_bit(offset, cdev->watched_lines))
1709925ca369SKent Gibson 			return -EBUSY;
1710925ca369SKent Gibson 
1711925ca369SKent Gibson 		return 0;
1712925ca369SKent Gibson 	}
1713925ca369SKent Gibson 	return -EINVAL;
1714925ca369SKent Gibson }
1715925ca369SKent Gibson 
1716925ca369SKent Gibson #ifdef CONFIG_COMPAT
171749bc5279SKent Gibson static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
1718925ca369SKent Gibson 			      unsigned long arg)
1719925ca369SKent Gibson {
172049bc5279SKent Gibson 	return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1721925ca369SKent Gibson }
1722925ca369SKent Gibson #endif
1723925ca369SKent Gibson 
1724925ca369SKent Gibson static struct gpio_chardev_data *
1725925ca369SKent Gibson to_gpio_chardev_data(struct notifier_block *nb)
1726925ca369SKent Gibson {
1727925ca369SKent Gibson 	return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
1728925ca369SKent Gibson }
1729925ca369SKent Gibson 
1730925ca369SKent Gibson static int lineinfo_changed_notify(struct notifier_block *nb,
1731925ca369SKent Gibson 				   unsigned long action, void *data)
1732925ca369SKent Gibson {
1733e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
1734aad95584SKent Gibson 	struct gpio_v2_line_info_changed chg;
1735925ca369SKent Gibson 	struct gpio_desc *desc = data;
1736925ca369SKent Gibson 	int ret;
1737925ca369SKent Gibson 
1738e2b781c5SKent Gibson 	if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
1739925ca369SKent Gibson 		return NOTIFY_DONE;
1740925ca369SKent Gibson 
1741925ca369SKent Gibson 	memset(&chg, 0, sizeof(chg));
1742925ca369SKent Gibson 	chg.event_type = action;
1743aad95584SKent Gibson 	chg.timestamp_ns = ktime_get_ns();
1744925ca369SKent Gibson 	gpio_desc_to_lineinfo(desc, &chg.info);
1745925ca369SKent Gibson 
1746e2b781c5SKent Gibson 	ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
1747925ca369SKent Gibson 	if (ret)
1748e2b781c5SKent Gibson 		wake_up_poll(&cdev->wait, EPOLLIN);
1749925ca369SKent Gibson 	else
1750925ca369SKent Gibson 		pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
1751925ca369SKent Gibson 
1752925ca369SKent Gibson 	return NOTIFY_OK;
1753925ca369SKent Gibson }
1754925ca369SKent Gibson 
175549bc5279SKent Gibson static __poll_t lineinfo_watch_poll(struct file *file,
1756925ca369SKent Gibson 				    struct poll_table_struct *pollt)
1757925ca369SKent Gibson {
1758e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
1759925ca369SKent Gibson 	__poll_t events = 0;
1760925ca369SKent Gibson 
1761e2b781c5SKent Gibson 	poll_wait(file, &cdev->wait, pollt);
1762925ca369SKent Gibson 
1763e2b781c5SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
1764e2b781c5SKent Gibson 						 &cdev->wait.lock))
1765925ca369SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
1766925ca369SKent Gibson 
1767925ca369SKent Gibson 	return events;
1768925ca369SKent Gibson }
1769925ca369SKent Gibson 
177049bc5279SKent Gibson static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
1771925ca369SKent Gibson 				   size_t count, loff_t *off)
1772925ca369SKent Gibson {
1773e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
1774aad95584SKent Gibson 	struct gpio_v2_line_info_changed event;
1775925ca369SKent Gibson 	ssize_t bytes_read = 0;
1776925ca369SKent Gibson 	int ret;
1777aad95584SKent Gibson 	size_t event_size;
1778925ca369SKent Gibson 
1779aad95584SKent Gibson #ifndef CONFIG_GPIO_CDEV_V1
1780aad95584SKent Gibson 	event_size = sizeof(struct gpio_v2_line_info_changed);
1781aad95584SKent Gibson 	if (count < event_size)
1782925ca369SKent Gibson 		return -EINVAL;
1783aad95584SKent Gibson #endif
1784925ca369SKent Gibson 
1785925ca369SKent Gibson 	do {
1786e2b781c5SKent Gibson 		spin_lock(&cdev->wait.lock);
1787e2b781c5SKent Gibson 		if (kfifo_is_empty(&cdev->events)) {
1788925ca369SKent Gibson 			if (bytes_read) {
1789e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
1790925ca369SKent Gibson 				return bytes_read;
1791925ca369SKent Gibson 			}
1792925ca369SKent Gibson 
179349bc5279SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
1794e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
1795925ca369SKent Gibson 				return -EAGAIN;
1796925ca369SKent Gibson 			}
1797925ca369SKent Gibson 
1798e2b781c5SKent Gibson 			ret = wait_event_interruptible_locked(cdev->wait,
1799e2b781c5SKent Gibson 					!kfifo_is_empty(&cdev->events));
1800925ca369SKent Gibson 			if (ret) {
1801e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
1802925ca369SKent Gibson 				return ret;
1803925ca369SKent Gibson 			}
1804925ca369SKent Gibson 		}
1805aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1806aad95584SKent Gibson 		/* must be after kfifo check so watch_abi_version is set */
1807aad95584SKent Gibson 		if (atomic_read(&cdev->watch_abi_version) == 2)
1808aad95584SKent Gibson 			event_size = sizeof(struct gpio_v2_line_info_changed);
1809aad95584SKent Gibson 		else
1810aad95584SKent Gibson 			event_size = sizeof(struct gpioline_info_changed);
1811aad95584SKent Gibson 		if (count < event_size) {
1812aad95584SKent Gibson 			spin_unlock(&cdev->wait.lock);
1813aad95584SKent Gibson 			return -EINVAL;
1814aad95584SKent Gibson 		}
1815aad95584SKent Gibson #endif
1816e2b781c5SKent Gibson 		ret = kfifo_out(&cdev->events, &event, 1);
1817e2b781c5SKent Gibson 		spin_unlock(&cdev->wait.lock);
1818925ca369SKent Gibson 		if (ret != 1) {
1819925ca369SKent Gibson 			ret = -EIO;
1820925ca369SKent Gibson 			break;
1821925ca369SKent Gibson 			/* We should never get here. See lineevent_read(). */
1822925ca369SKent Gibson 		}
1823925ca369SKent Gibson 
1824aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1825aad95584SKent Gibson 		if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
1826aad95584SKent Gibson 			if (copy_to_user(buf + bytes_read, &event, event_size))
1827925ca369SKent Gibson 				return -EFAULT;
1828aad95584SKent Gibson 		} else {
1829aad95584SKent Gibson 			struct gpioline_info_changed event_v1;
1830aad95584SKent Gibson 
1831aad95584SKent Gibson 			gpio_v2_line_info_changed_to_v1(&event, &event_v1);
1832aad95584SKent Gibson 			if (copy_to_user(buf + bytes_read, &event_v1,
1833aad95584SKent Gibson 					 event_size))
1834aad95584SKent Gibson 				return -EFAULT;
1835aad95584SKent Gibson 		}
1836aad95584SKent Gibson #else
1837aad95584SKent Gibson 		if (copy_to_user(buf + bytes_read, &event, event_size))
1838aad95584SKent Gibson 			return -EFAULT;
1839aad95584SKent Gibson #endif
1840aad95584SKent Gibson 		bytes_read += event_size;
1841925ca369SKent Gibson 	} while (count >= bytes_read + sizeof(event));
1842925ca369SKent Gibson 
1843925ca369SKent Gibson 	return bytes_read;
1844925ca369SKent Gibson }
1845925ca369SKent Gibson 
1846925ca369SKent Gibson /**
1847925ca369SKent Gibson  * gpio_chrdev_open() - open the chardev for ioctl operations
1848925ca369SKent Gibson  * @inode: inode for this chardev
184949bc5279SKent Gibson  * @file: file struct for storing private data
1850925ca369SKent Gibson  * Returns 0 on success
1851925ca369SKent Gibson  */
185249bc5279SKent Gibson static int gpio_chrdev_open(struct inode *inode, struct file *file)
1853925ca369SKent Gibson {
1854925ca369SKent Gibson 	struct gpio_device *gdev = container_of(inode->i_cdev,
1855925ca369SKent Gibson 						struct gpio_device, chrdev);
1856e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev;
1857925ca369SKent Gibson 	int ret = -ENOMEM;
1858925ca369SKent Gibson 
1859925ca369SKent Gibson 	/* Fail on open if the backing gpiochip is gone */
1860925ca369SKent Gibson 	if (!gdev->chip)
1861925ca369SKent Gibson 		return -ENODEV;
1862925ca369SKent Gibson 
1863e2b781c5SKent Gibson 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
1864e2b781c5SKent Gibson 	if (!cdev)
1865925ca369SKent Gibson 		return -ENOMEM;
1866925ca369SKent Gibson 
1867e2b781c5SKent Gibson 	cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
1868e2b781c5SKent Gibson 	if (!cdev->watched_lines)
1869e2b781c5SKent Gibson 		goto out_free_cdev;
1870925ca369SKent Gibson 
1871e2b781c5SKent Gibson 	init_waitqueue_head(&cdev->wait);
1872e2b781c5SKent Gibson 	INIT_KFIFO(cdev->events);
1873e2b781c5SKent Gibson 	cdev->gdev = gdev;
1874925ca369SKent Gibson 
1875e2b781c5SKent Gibson 	cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
18766accc376SKent Gibson 	ret = blocking_notifier_chain_register(&gdev->notifier,
1877e2b781c5SKent Gibson 					       &cdev->lineinfo_changed_nb);
1878925ca369SKent Gibson 	if (ret)
1879925ca369SKent Gibson 		goto out_free_bitmap;
1880925ca369SKent Gibson 
1881925ca369SKent Gibson 	get_device(&gdev->dev);
1882e2b781c5SKent Gibson 	file->private_data = cdev;
1883925ca369SKent Gibson 
188449bc5279SKent Gibson 	ret = nonseekable_open(inode, file);
1885925ca369SKent Gibson 	if (ret)
1886925ca369SKent Gibson 		goto out_unregister_notifier;
1887925ca369SKent Gibson 
1888925ca369SKent Gibson 	return ret;
1889925ca369SKent Gibson 
1890925ca369SKent Gibson out_unregister_notifier:
18916accc376SKent Gibson 	blocking_notifier_chain_unregister(&gdev->notifier,
1892e2b781c5SKent Gibson 					   &cdev->lineinfo_changed_nb);
1893925ca369SKent Gibson out_free_bitmap:
1894e2b781c5SKent Gibson 	bitmap_free(cdev->watched_lines);
1895e2b781c5SKent Gibson out_free_cdev:
1896e2b781c5SKent Gibson 	kfree(cdev);
1897925ca369SKent Gibson 	return ret;
1898925ca369SKent Gibson }
1899925ca369SKent Gibson 
1900925ca369SKent Gibson /**
1901925ca369SKent Gibson  * gpio_chrdev_release() - close chardev after ioctl operations
1902925ca369SKent Gibson  * @inode: inode for this chardev
190349bc5279SKent Gibson  * @file: file struct for storing private data
1904925ca369SKent Gibson  * Returns 0 on success
1905925ca369SKent Gibson  */
190649bc5279SKent Gibson static int gpio_chrdev_release(struct inode *inode, struct file *file)
1907925ca369SKent Gibson {
1908e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
1909e2b781c5SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
1910925ca369SKent Gibson 
1911e2b781c5SKent Gibson 	bitmap_free(cdev->watched_lines);
19126accc376SKent Gibson 	blocking_notifier_chain_unregister(&gdev->notifier,
1913e2b781c5SKent Gibson 					   &cdev->lineinfo_changed_nb);
1914925ca369SKent Gibson 	put_device(&gdev->dev);
1915e2b781c5SKent Gibson 	kfree(cdev);
1916925ca369SKent Gibson 
1917925ca369SKent Gibson 	return 0;
1918925ca369SKent Gibson }
1919925ca369SKent Gibson 
1920925ca369SKent Gibson static const struct file_operations gpio_fileops = {
1921925ca369SKent Gibson 	.release = gpio_chrdev_release,
1922925ca369SKent Gibson 	.open = gpio_chrdev_open,
1923925ca369SKent Gibson 	.poll = lineinfo_watch_poll,
1924925ca369SKent Gibson 	.read = lineinfo_watch_read,
1925925ca369SKent Gibson 	.owner = THIS_MODULE,
1926925ca369SKent Gibson 	.llseek = no_llseek,
1927925ca369SKent Gibson 	.unlocked_ioctl = gpio_ioctl,
1928925ca369SKent Gibson #ifdef CONFIG_COMPAT
1929925ca369SKent Gibson 	.compat_ioctl = gpio_ioctl_compat,
1930925ca369SKent Gibson #endif
1931925ca369SKent Gibson };
1932925ca369SKent Gibson 
1933925ca369SKent Gibson int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
1934925ca369SKent Gibson {
1935925ca369SKent Gibson 	int ret;
1936925ca369SKent Gibson 
1937925ca369SKent Gibson 	cdev_init(&gdev->chrdev, &gpio_fileops);
1938925ca369SKent Gibson 	gdev->chrdev.owner = THIS_MODULE;
1939925ca369SKent Gibson 	gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
1940925ca369SKent Gibson 
1941925ca369SKent Gibson 	ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
1942925ca369SKent Gibson 	if (ret)
1943925ca369SKent Gibson 		return ret;
1944925ca369SKent Gibson 
1945925ca369SKent Gibson 	chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1946925ca369SKent Gibson 		 MAJOR(devt), gdev->id);
1947925ca369SKent Gibson 
1948925ca369SKent Gibson 	return 0;
1949925ca369SKent Gibson }
1950925ca369SKent Gibson 
1951925ca369SKent Gibson void gpiolib_cdev_unregister(struct gpio_device *gdev)
1952925ca369SKent Gibson {
1953925ca369SKent Gibson 	cdev_device_del(&gdev->chrdev, &gdev->dev);
1954925ca369SKent Gibson }
1955