xref: /openbmc/linux/drivers/gpio/gpiolib-cdev.c (revision aad955842d1cdf56d31e600112137d82fd431140)
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,
184*aad95584SKent Gibson 					     GPIO_V2_LINE_CHANGED_CONFIG,
185*aad95584SKent 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,
357*aad95584SKent 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.
4073c0d9c63SKent Gibson  */
4083c0d9c63SKent Gibson struct line {
4093c0d9c63SKent Gibson 	struct gpio_desc *desc;
4103c0d9c63SKent Gibson };
4113c0d9c63SKent Gibson 
4123c0d9c63SKent Gibson /**
4133c0d9c63SKent Gibson  * struct linereq - contains the state of a userspace line request
4143c0d9c63SKent Gibson  * @gdev: the GPIO device the line request pertains to
4153c0d9c63SKent Gibson  * @label: consumer label used to tag GPIO descriptors
4163c0d9c63SKent Gibson  * @num_lines: the number of lines in the lines array
4173c0d9c63SKent Gibson  * @lines: the lines held by this line request, with @num_lines elements.
4183c0d9c63SKent Gibson  */
4193c0d9c63SKent Gibson struct linereq {
4203c0d9c63SKent Gibson 	struct gpio_device *gdev;
4213c0d9c63SKent Gibson 	const char *label;
4223c0d9c63SKent Gibson 	u32 num_lines;
4233c0d9c63SKent Gibson 	struct line lines[];
4243c0d9c63SKent Gibson };
4253c0d9c63SKent Gibson 
4263c0d9c63SKent Gibson #define GPIO_V2_LINE_BIAS_FLAGS \
4273c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
4283c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
4293c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_BIAS_DISABLED)
4303c0d9c63SKent Gibson 
4313c0d9c63SKent Gibson #define GPIO_V2_LINE_DIRECTION_FLAGS \
4323c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_INPUT | \
4333c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_OUTPUT)
4343c0d9c63SKent Gibson 
4353c0d9c63SKent Gibson #define GPIO_V2_LINE_DRIVE_FLAGS \
4363c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
4373c0d9c63SKent Gibson 	 GPIO_V2_LINE_FLAG_OPEN_SOURCE)
4383c0d9c63SKent Gibson 
4393c0d9c63SKent Gibson #define GPIO_V2_LINE_VALID_FLAGS \
4403c0d9c63SKent Gibson 	(GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
4413c0d9c63SKent Gibson 	 GPIO_V2_LINE_DIRECTION_FLAGS | \
4423c0d9c63SKent Gibson 	 GPIO_V2_LINE_DRIVE_FLAGS | \
4433c0d9c63SKent Gibson 	 GPIO_V2_LINE_BIAS_FLAGS)
4443c0d9c63SKent Gibson 
4453c0d9c63SKent Gibson static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
4463c0d9c63SKent Gibson 				     unsigned int line_idx)
4473c0d9c63SKent Gibson {
4483c0d9c63SKent Gibson 	unsigned int i;
4493c0d9c63SKent Gibson 	u64 mask = BIT_ULL(line_idx);
4503c0d9c63SKent Gibson 
4513c0d9c63SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
4523c0d9c63SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
4533c0d9c63SKent Gibson 		    (lc->attrs[i].mask & mask))
4543c0d9c63SKent Gibson 			return lc->attrs[i].attr.flags;
4553c0d9c63SKent Gibson 	}
4563c0d9c63SKent Gibson 	return lc->flags;
4573c0d9c63SKent Gibson }
4583c0d9c63SKent Gibson 
4593c0d9c63SKent Gibson static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
4603c0d9c63SKent Gibson 					    unsigned int line_idx)
4613c0d9c63SKent Gibson {
4623c0d9c63SKent Gibson 	unsigned int i;
4633c0d9c63SKent Gibson 	u64 mask = BIT_ULL(line_idx);
4643c0d9c63SKent Gibson 
4653c0d9c63SKent Gibson 	for (i = 0; i < lc->num_attrs; i++) {
4663c0d9c63SKent Gibson 		if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
4673c0d9c63SKent Gibson 		    (lc->attrs[i].mask & mask))
4683c0d9c63SKent Gibson 			return !!(lc->attrs[i].attr.values & mask);
4693c0d9c63SKent Gibson 	}
4703c0d9c63SKent Gibson 	return 0;
4713c0d9c63SKent Gibson }
4723c0d9c63SKent Gibson 
4733c0d9c63SKent Gibson static int gpio_v2_line_flags_validate(u64 flags)
4743c0d9c63SKent Gibson {
4753c0d9c63SKent Gibson 	/* Return an error if an unknown flag is set */
4763c0d9c63SKent Gibson 	if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
4773c0d9c63SKent Gibson 		return -EINVAL;
4783c0d9c63SKent Gibson 
4793c0d9c63SKent Gibson 	/*
4803c0d9c63SKent Gibson 	 * Do not allow both INPUT and OUTPUT flags to be set as they are
4813c0d9c63SKent Gibson 	 * contradictory.
4823c0d9c63SKent Gibson 	 */
4833c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
4843c0d9c63SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_OUTPUT))
4853c0d9c63SKent Gibson 		return -EINVAL;
4863c0d9c63SKent Gibson 
4873c0d9c63SKent Gibson 	/*
4883c0d9c63SKent Gibson 	 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
4893c0d9c63SKent Gibson 	 * request. If the hardware actually supports enabling both at the
4903c0d9c63SKent Gibson 	 * same time the electrical result would be disastrous.
4913c0d9c63SKent Gibson 	 */
4923c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
4933c0d9c63SKent Gibson 	    (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
4943c0d9c63SKent Gibson 		return -EINVAL;
4953c0d9c63SKent Gibson 
4963c0d9c63SKent Gibson 	/* Drive requires explicit output direction. */
4973c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
4983c0d9c63SKent Gibson 	    !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
4993c0d9c63SKent Gibson 		return -EINVAL;
5003c0d9c63SKent Gibson 
5013c0d9c63SKent Gibson 	/* Bias requires explicit direction. */
5023c0d9c63SKent Gibson 	if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
5033c0d9c63SKent Gibson 	    !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
5043c0d9c63SKent Gibson 		return -EINVAL;
5053c0d9c63SKent Gibson 
5063c0d9c63SKent Gibson 	/* Only one bias flag can be set. */
5073c0d9c63SKent Gibson 	if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
5083c0d9c63SKent Gibson 	     (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
5093c0d9c63SKent Gibson 		       GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
5103c0d9c63SKent Gibson 	    ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
5113c0d9c63SKent Gibson 	     (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
5123c0d9c63SKent Gibson 		return -EINVAL;
5133c0d9c63SKent Gibson 
5143c0d9c63SKent Gibson 	return 0;
5153c0d9c63SKent Gibson }
5163c0d9c63SKent Gibson 
5173c0d9c63SKent Gibson static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
5183c0d9c63SKent Gibson 					unsigned int num_lines)
5193c0d9c63SKent Gibson {
5203c0d9c63SKent Gibson 	unsigned int i;
5213c0d9c63SKent Gibson 	u64 flags;
5223c0d9c63SKent Gibson 	int ret;
5233c0d9c63SKent Gibson 
5243c0d9c63SKent Gibson 	if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
5253c0d9c63SKent Gibson 		return -EINVAL;
5263c0d9c63SKent Gibson 
5273c0d9c63SKent Gibson 	if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
5283c0d9c63SKent Gibson 		return -EINVAL;
5293c0d9c63SKent Gibson 
5303c0d9c63SKent Gibson 	for (i = 0; i < num_lines; i++) {
5313c0d9c63SKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
5323c0d9c63SKent Gibson 		ret = gpio_v2_line_flags_validate(flags);
5333c0d9c63SKent Gibson 		if (ret)
5343c0d9c63SKent Gibson 			return ret;
5353c0d9c63SKent Gibson 	}
5363c0d9c63SKent Gibson 	return 0;
5373c0d9c63SKent Gibson }
5383c0d9c63SKent Gibson 
5393c0d9c63SKent Gibson static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
5403c0d9c63SKent Gibson 						    unsigned long *flagsp)
5413c0d9c63SKent Gibson {
5423c0d9c63SKent Gibson 	assign_bit(FLAG_ACTIVE_LOW, flagsp,
5433c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
5443c0d9c63SKent Gibson 
5453c0d9c63SKent Gibson 	if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
5463c0d9c63SKent Gibson 		set_bit(FLAG_IS_OUT, flagsp);
5473c0d9c63SKent Gibson 	else if (flags & GPIO_V2_LINE_FLAG_INPUT)
5483c0d9c63SKent Gibson 		clear_bit(FLAG_IS_OUT, flagsp);
5493c0d9c63SKent Gibson 
5503c0d9c63SKent Gibson 	assign_bit(FLAG_OPEN_DRAIN, flagsp,
5513c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
5523c0d9c63SKent Gibson 	assign_bit(FLAG_OPEN_SOURCE, flagsp,
5533c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
5543c0d9c63SKent Gibson 
5553c0d9c63SKent Gibson 	assign_bit(FLAG_PULL_UP, flagsp,
5563c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
5573c0d9c63SKent Gibson 	assign_bit(FLAG_PULL_DOWN, flagsp,
5583c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
5593c0d9c63SKent Gibson 	assign_bit(FLAG_BIAS_DISABLE, flagsp,
5603c0d9c63SKent Gibson 		   flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
5613c0d9c63SKent Gibson }
5623c0d9c63SKent Gibson 
5633c0d9c63SKent Gibson static long linereq_get_values(struct linereq *lr, void __user *ip)
5643c0d9c63SKent Gibson {
5653c0d9c63SKent Gibson 	struct gpio_v2_line_values lv;
5663c0d9c63SKent Gibson 	DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
5673c0d9c63SKent Gibson 	struct gpio_desc **descs;
5683c0d9c63SKent Gibson 	unsigned int i, didx, num_get;
5693c0d9c63SKent Gibson 	int ret;
5703c0d9c63SKent Gibson 
5713c0d9c63SKent Gibson 	/* NOTE: It's ok to read values of output lines. */
5723c0d9c63SKent Gibson 	if (copy_from_user(&lv, ip, sizeof(lv)))
5733c0d9c63SKent Gibson 		return -EFAULT;
5743c0d9c63SKent Gibson 
5753c0d9c63SKent Gibson 	for (num_get = 0, i = 0; i < lr->num_lines; i++) {
5763c0d9c63SKent Gibson 		if (lv.mask & BIT_ULL(i)) {
5773c0d9c63SKent Gibson 			num_get++;
5783c0d9c63SKent Gibson 			descs = &lr->lines[i].desc;
5793c0d9c63SKent Gibson 		}
5803c0d9c63SKent Gibson 	}
5813c0d9c63SKent Gibson 
5823c0d9c63SKent Gibson 	if (num_get == 0)
5833c0d9c63SKent Gibson 		return -EINVAL;
5843c0d9c63SKent Gibson 
5853c0d9c63SKent Gibson 	if (num_get != 1) {
5863c0d9c63SKent Gibson 		descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
5873c0d9c63SKent Gibson 		if (!descs)
5883c0d9c63SKent Gibson 			return -ENOMEM;
5893c0d9c63SKent Gibson 		for (didx = 0, i = 0; i < lr->num_lines; i++) {
5903c0d9c63SKent Gibson 			if (lv.mask & BIT_ULL(i)) {
5913c0d9c63SKent Gibson 				descs[didx] = lr->lines[i].desc;
5923c0d9c63SKent Gibson 				didx++;
5933c0d9c63SKent Gibson 			}
5943c0d9c63SKent Gibson 		}
5953c0d9c63SKent Gibson 	}
5963c0d9c63SKent Gibson 	ret = gpiod_get_array_value_complex(false, true, num_get,
5973c0d9c63SKent Gibson 					    descs, NULL, vals);
5983c0d9c63SKent Gibson 
5993c0d9c63SKent Gibson 	if (num_get != 1)
6003c0d9c63SKent Gibson 		kfree(descs);
6013c0d9c63SKent Gibson 	if (ret)
6023c0d9c63SKent Gibson 		return ret;
6033c0d9c63SKent Gibson 
6043c0d9c63SKent Gibson 	lv.bits = 0;
6053c0d9c63SKent Gibson 	for (didx = 0, i = 0; i < lr->num_lines; i++) {
6063c0d9c63SKent Gibson 		if (lv.mask & BIT_ULL(i)) {
6073c0d9c63SKent Gibson 			if (test_bit(didx, vals))
6083c0d9c63SKent Gibson 				lv.bits |= BIT_ULL(i);
6093c0d9c63SKent Gibson 			didx++;
6103c0d9c63SKent Gibson 		}
6113c0d9c63SKent Gibson 	}
6123c0d9c63SKent Gibson 
6133c0d9c63SKent Gibson 	if (copy_to_user(ip, &lv, sizeof(lv)))
6143c0d9c63SKent Gibson 		return -EFAULT;
6153c0d9c63SKent Gibson 
6163c0d9c63SKent Gibson 	return 0;
6173c0d9c63SKent Gibson }
6183c0d9c63SKent Gibson 
6193c0d9c63SKent Gibson static long linereq_ioctl(struct file *file, unsigned int cmd,
6203c0d9c63SKent Gibson 			  unsigned long arg)
6213c0d9c63SKent Gibson {
6223c0d9c63SKent Gibson 	struct linereq *lr = file->private_data;
6233c0d9c63SKent Gibson 	void __user *ip = (void __user *)arg;
6243c0d9c63SKent Gibson 
6253c0d9c63SKent Gibson 	if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL)
6263c0d9c63SKent Gibson 		return linereq_get_values(lr, ip);
6273c0d9c63SKent Gibson 
6283c0d9c63SKent Gibson 	return -EINVAL;
6293c0d9c63SKent Gibson }
6303c0d9c63SKent Gibson 
6313c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
6323c0d9c63SKent Gibson static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
6333c0d9c63SKent Gibson 				 unsigned long arg)
6343c0d9c63SKent Gibson {
6353c0d9c63SKent Gibson 	return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
6363c0d9c63SKent Gibson }
6373c0d9c63SKent Gibson #endif
6383c0d9c63SKent Gibson 
6393c0d9c63SKent Gibson static void linereq_free(struct linereq *lr)
6403c0d9c63SKent Gibson {
6413c0d9c63SKent Gibson 	unsigned int i;
6423c0d9c63SKent Gibson 
6433c0d9c63SKent Gibson 	for (i = 0; i < lr->num_lines; i++) {
6443c0d9c63SKent Gibson 		if (lr->lines[i].desc)
6453c0d9c63SKent Gibson 			gpiod_free(lr->lines[i].desc);
6463c0d9c63SKent Gibson 	}
6473c0d9c63SKent Gibson 	kfree(lr->label);
6483c0d9c63SKent Gibson 	put_device(&lr->gdev->dev);
6493c0d9c63SKent Gibson 	kfree(lr);
6503c0d9c63SKent Gibson }
6513c0d9c63SKent Gibson 
6523c0d9c63SKent Gibson static int linereq_release(struct inode *inode, struct file *file)
6533c0d9c63SKent Gibson {
6543c0d9c63SKent Gibson 	struct linereq *lr = file->private_data;
6553c0d9c63SKent Gibson 
6563c0d9c63SKent Gibson 	linereq_free(lr);
6573c0d9c63SKent Gibson 	return 0;
6583c0d9c63SKent Gibson }
6593c0d9c63SKent Gibson 
6603c0d9c63SKent Gibson static const struct file_operations line_fileops = {
6613c0d9c63SKent Gibson 	.release = linereq_release,
6623c0d9c63SKent Gibson 	.owner = THIS_MODULE,
6633c0d9c63SKent Gibson 	.llseek = noop_llseek,
6643c0d9c63SKent Gibson 	.unlocked_ioctl = linereq_ioctl,
6653c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
6663c0d9c63SKent Gibson 	.compat_ioctl = linereq_ioctl_compat,
6673c0d9c63SKent Gibson #endif
6683c0d9c63SKent Gibson };
6693c0d9c63SKent Gibson 
6703c0d9c63SKent Gibson static int linereq_create(struct gpio_device *gdev, void __user *ip)
6713c0d9c63SKent Gibson {
6723c0d9c63SKent Gibson 	struct gpio_v2_line_request ulr;
6733c0d9c63SKent Gibson 	struct gpio_v2_line_config *lc;
6743c0d9c63SKent Gibson 	struct linereq *lr;
6753c0d9c63SKent Gibson 	struct file *file;
6763c0d9c63SKent Gibson 	u64 flags;
6773c0d9c63SKent Gibson 	unsigned int i;
6783c0d9c63SKent Gibson 	int fd, ret;
6793c0d9c63SKent Gibson 
6803c0d9c63SKent Gibson 	if (copy_from_user(&ulr, ip, sizeof(ulr)))
6813c0d9c63SKent Gibson 		return -EFAULT;
6823c0d9c63SKent Gibson 
6833c0d9c63SKent Gibson 	if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
6843c0d9c63SKent Gibson 		return -EINVAL;
6853c0d9c63SKent Gibson 
6863c0d9c63SKent Gibson 	if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
6873c0d9c63SKent Gibson 		return -EINVAL;
6883c0d9c63SKent Gibson 
6893c0d9c63SKent Gibson 	lc = &ulr.config;
6903c0d9c63SKent Gibson 	ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
6913c0d9c63SKent Gibson 	if (ret)
6923c0d9c63SKent Gibson 		return ret;
6933c0d9c63SKent Gibson 
6943c0d9c63SKent Gibson 	lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
6953c0d9c63SKent Gibson 	if (!lr)
6963c0d9c63SKent Gibson 		return -ENOMEM;
6973c0d9c63SKent Gibson 
6983c0d9c63SKent Gibson 	lr->gdev = gdev;
6993c0d9c63SKent Gibson 	get_device(&gdev->dev);
7003c0d9c63SKent Gibson 
7013c0d9c63SKent Gibson 	/* Make sure this is terminated */
7023c0d9c63SKent Gibson 	ulr.consumer[sizeof(ulr.consumer)-1] = '\0';
7033c0d9c63SKent Gibson 	if (strlen(ulr.consumer)) {
7043c0d9c63SKent Gibson 		/* label is only initialized if consumer is set */
7053c0d9c63SKent Gibson 		lr->label = kstrdup(ulr.consumer, GFP_KERNEL);
7063c0d9c63SKent Gibson 		if (!lr->label) {
7073c0d9c63SKent Gibson 			ret = -ENOMEM;
7083c0d9c63SKent Gibson 			goto out_free_linereq;
7093c0d9c63SKent Gibson 		}
7103c0d9c63SKent Gibson 	}
7113c0d9c63SKent Gibson 
7123c0d9c63SKent Gibson 	lr->num_lines = ulr.num_lines;
7133c0d9c63SKent Gibson 
7143c0d9c63SKent Gibson 	/* Request each GPIO */
7153c0d9c63SKent Gibson 	for (i = 0; i < ulr.num_lines; i++) {
7163c0d9c63SKent Gibson 		u32 offset = ulr.offsets[i];
7173c0d9c63SKent Gibson 		struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
7183c0d9c63SKent Gibson 
7193c0d9c63SKent Gibson 		if (IS_ERR(desc)) {
7203c0d9c63SKent Gibson 			ret = PTR_ERR(desc);
7213c0d9c63SKent Gibson 			goto out_free_linereq;
7223c0d9c63SKent Gibson 		}
7233c0d9c63SKent Gibson 
7243c0d9c63SKent Gibson 		ret = gpiod_request(desc, lr->label);
7253c0d9c63SKent Gibson 		if (ret)
7263c0d9c63SKent Gibson 			goto out_free_linereq;
7273c0d9c63SKent Gibson 
7283c0d9c63SKent Gibson 		lr->lines[i].desc = desc;
7293c0d9c63SKent Gibson 		flags = gpio_v2_line_config_flags(lc, i);
7303c0d9c63SKent Gibson 		gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
7313c0d9c63SKent Gibson 
7323c0d9c63SKent Gibson 		ret = gpiod_set_transitory(desc, false);
7333c0d9c63SKent Gibson 		if (ret < 0)
7343c0d9c63SKent Gibson 			goto out_free_linereq;
7353c0d9c63SKent Gibson 
7363c0d9c63SKent Gibson 		/*
7373c0d9c63SKent Gibson 		 * Lines have to be requested explicitly for input
7383c0d9c63SKent Gibson 		 * or output, else the line will be treated "as is".
7393c0d9c63SKent Gibson 		 */
7403c0d9c63SKent Gibson 		if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
7413c0d9c63SKent Gibson 			int val = gpio_v2_line_config_output_value(lc, i);
7423c0d9c63SKent Gibson 
7433c0d9c63SKent Gibson 			ret = gpiod_direction_output(desc, val);
7443c0d9c63SKent Gibson 			if (ret)
7453c0d9c63SKent Gibson 				goto out_free_linereq;
7463c0d9c63SKent Gibson 		} else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
7473c0d9c63SKent Gibson 			ret = gpiod_direction_input(desc);
7483c0d9c63SKent Gibson 			if (ret)
7493c0d9c63SKent Gibson 				goto out_free_linereq;
7503c0d9c63SKent Gibson 		}
7513c0d9c63SKent Gibson 
7523c0d9c63SKent Gibson 		blocking_notifier_call_chain(&desc->gdev->notifier,
753*aad95584SKent Gibson 					     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
7543c0d9c63SKent Gibson 
7553c0d9c63SKent Gibson 		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
7563c0d9c63SKent Gibson 			offset);
7573c0d9c63SKent Gibson 	}
7583c0d9c63SKent Gibson 
7593c0d9c63SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
7603c0d9c63SKent Gibson 	if (fd < 0) {
7613c0d9c63SKent Gibson 		ret = fd;
7623c0d9c63SKent Gibson 		goto out_free_linereq;
7633c0d9c63SKent Gibson 	}
7643c0d9c63SKent Gibson 
7653c0d9c63SKent Gibson 	file = anon_inode_getfile("gpio-line", &line_fileops, lr,
7663c0d9c63SKent Gibson 				  O_RDONLY | O_CLOEXEC);
7673c0d9c63SKent Gibson 	if (IS_ERR(file)) {
7683c0d9c63SKent Gibson 		ret = PTR_ERR(file);
7693c0d9c63SKent Gibson 		goto out_put_unused_fd;
7703c0d9c63SKent Gibson 	}
7713c0d9c63SKent Gibson 
7723c0d9c63SKent Gibson 	ulr.fd = fd;
7733c0d9c63SKent Gibson 	if (copy_to_user(ip, &ulr, sizeof(ulr))) {
7743c0d9c63SKent Gibson 		/*
7753c0d9c63SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
7763c0d9c63SKent Gibson 		 * the regular error cleanup path here.
7773c0d9c63SKent Gibson 		 */
7783c0d9c63SKent Gibson 		fput(file);
7793c0d9c63SKent Gibson 		put_unused_fd(fd);
7803c0d9c63SKent Gibson 		return -EFAULT;
7813c0d9c63SKent Gibson 	}
7823c0d9c63SKent Gibson 
7833c0d9c63SKent Gibson 	fd_install(fd, file);
7843c0d9c63SKent Gibson 
7853c0d9c63SKent Gibson 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
7863c0d9c63SKent Gibson 		lr->num_lines);
7873c0d9c63SKent Gibson 
7883c0d9c63SKent Gibson 	return 0;
7893c0d9c63SKent Gibson 
7903c0d9c63SKent Gibson out_put_unused_fd:
7913c0d9c63SKent Gibson 	put_unused_fd(fd);
7923c0d9c63SKent Gibson out_free_linereq:
7933c0d9c63SKent Gibson 	linereq_free(lr);
7943c0d9c63SKent Gibson 	return ret;
7953c0d9c63SKent Gibson }
7963c0d9c63SKent Gibson 
7973c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
798925ca369SKent Gibson 
799925ca369SKent Gibson /*
800925ca369SKent Gibson  * GPIO line event management
801925ca369SKent Gibson  */
802925ca369SKent Gibson 
803925ca369SKent Gibson /**
804925ca369SKent Gibson  * struct lineevent_state - contains the state of a userspace event
805925ca369SKent Gibson  * @gdev: the GPIO device the event pertains to
806925ca369SKent Gibson  * @label: consumer label used to tag descriptors
807925ca369SKent Gibson  * @desc: the GPIO descriptor held by this event
808925ca369SKent Gibson  * @eflags: the event flags this line was requested with
809925ca369SKent Gibson  * @irq: the interrupt that trigger in response to events on this GPIO
810925ca369SKent Gibson  * @wait: wait queue that handles blocking reads of events
811925ca369SKent Gibson  * @events: KFIFO for the GPIO events
812925ca369SKent Gibson  * @timestamp: cache for the timestamp storing it between hardirq
813925ca369SKent Gibson  * and IRQ thread, used to bring the timestamp close to the actual
814925ca369SKent Gibson  * event
815925ca369SKent Gibson  */
816925ca369SKent Gibson struct lineevent_state {
817925ca369SKent Gibson 	struct gpio_device *gdev;
818925ca369SKent Gibson 	const char *label;
819925ca369SKent Gibson 	struct gpio_desc *desc;
820925ca369SKent Gibson 	u32 eflags;
821925ca369SKent Gibson 	int irq;
822925ca369SKent Gibson 	wait_queue_head_t wait;
823925ca369SKent Gibson 	DECLARE_KFIFO(events, struct gpioevent_data, 16);
824925ca369SKent Gibson 	u64 timestamp;
825925ca369SKent Gibson };
826925ca369SKent Gibson 
827925ca369SKent Gibson #define GPIOEVENT_REQUEST_VALID_FLAGS \
828925ca369SKent Gibson 	(GPIOEVENT_REQUEST_RISING_EDGE | \
829925ca369SKent Gibson 	GPIOEVENT_REQUEST_FALLING_EDGE)
830925ca369SKent Gibson 
83149bc5279SKent Gibson static __poll_t lineevent_poll(struct file *file,
832925ca369SKent Gibson 			       struct poll_table_struct *wait)
833925ca369SKent Gibson {
83449bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
835925ca369SKent Gibson 	__poll_t events = 0;
836925ca369SKent Gibson 
83749bc5279SKent Gibson 	poll_wait(file, &le->wait, wait);
838925ca369SKent Gibson 
839925ca369SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
840925ca369SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
841925ca369SKent Gibson 
842925ca369SKent Gibson 	return events;
843925ca369SKent Gibson }
844925ca369SKent Gibson 
845925ca369SKent Gibson 
84649bc5279SKent Gibson static ssize_t lineevent_read(struct file *file,
847925ca369SKent Gibson 			      char __user *buf,
848925ca369SKent Gibson 			      size_t count,
849925ca369SKent Gibson 			      loff_t *f_ps)
850925ca369SKent Gibson {
85149bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
852925ca369SKent Gibson 	struct gpioevent_data ge;
853925ca369SKent Gibson 	ssize_t bytes_read = 0;
854925ca369SKent Gibson 	int ret;
855925ca369SKent Gibson 
856925ca369SKent Gibson 	if (count < sizeof(ge))
857925ca369SKent Gibson 		return -EINVAL;
858925ca369SKent Gibson 
859925ca369SKent Gibson 	do {
860925ca369SKent Gibson 		spin_lock(&le->wait.lock);
861925ca369SKent Gibson 		if (kfifo_is_empty(&le->events)) {
862925ca369SKent Gibson 			if (bytes_read) {
863925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
864925ca369SKent Gibson 				return bytes_read;
865925ca369SKent Gibson 			}
866925ca369SKent Gibson 
86749bc5279SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
868925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
869925ca369SKent Gibson 				return -EAGAIN;
870925ca369SKent Gibson 			}
871925ca369SKent Gibson 
872925ca369SKent Gibson 			ret = wait_event_interruptible_locked(le->wait,
873925ca369SKent Gibson 					!kfifo_is_empty(&le->events));
874925ca369SKent Gibson 			if (ret) {
875925ca369SKent Gibson 				spin_unlock(&le->wait.lock);
876925ca369SKent Gibson 				return ret;
877925ca369SKent Gibson 			}
878925ca369SKent Gibson 		}
879925ca369SKent Gibson 
880925ca369SKent Gibson 		ret = kfifo_out(&le->events, &ge, 1);
881925ca369SKent Gibson 		spin_unlock(&le->wait.lock);
882925ca369SKent Gibson 		if (ret != 1) {
883925ca369SKent Gibson 			/*
884925ca369SKent Gibson 			 * This should never happen - we were holding the lock
885925ca369SKent Gibson 			 * from the moment we learned the fifo is no longer
886925ca369SKent Gibson 			 * empty until now.
887925ca369SKent Gibson 			 */
888925ca369SKent Gibson 			ret = -EIO;
889925ca369SKent Gibson 			break;
890925ca369SKent Gibson 		}
891925ca369SKent Gibson 
892925ca369SKent Gibson 		if (copy_to_user(buf + bytes_read, &ge, sizeof(ge)))
893925ca369SKent Gibson 			return -EFAULT;
894925ca369SKent Gibson 		bytes_read += sizeof(ge);
895925ca369SKent Gibson 	} while (count >= bytes_read + sizeof(ge));
896925ca369SKent Gibson 
897925ca369SKent Gibson 	return bytes_read;
898925ca369SKent Gibson }
899925ca369SKent Gibson 
90046824272SKent Gibson static void lineevent_free(struct lineevent_state *le)
901925ca369SKent Gibson {
90246824272SKent Gibson 	if (le->irq)
903925ca369SKent Gibson 		free_irq(le->irq, le);
90446824272SKent Gibson 	if (le->desc)
905925ca369SKent Gibson 		gpiod_free(le->desc);
906925ca369SKent Gibson 	kfree(le->label);
90746824272SKent Gibson 	put_device(&le->gdev->dev);
908925ca369SKent Gibson 	kfree(le);
90946824272SKent Gibson }
91046824272SKent Gibson 
91146824272SKent Gibson static int lineevent_release(struct inode *inode, struct file *file)
91246824272SKent Gibson {
91346824272SKent Gibson 	lineevent_free(file->private_data);
914925ca369SKent Gibson 	return 0;
915925ca369SKent Gibson }
916925ca369SKent Gibson 
91749bc5279SKent Gibson static long lineevent_ioctl(struct file *file, unsigned int cmd,
918925ca369SKent Gibson 			    unsigned long arg)
919925ca369SKent Gibson {
92049bc5279SKent Gibson 	struct lineevent_state *le = file->private_data;
921925ca369SKent Gibson 	void __user *ip = (void __user *)arg;
922925ca369SKent Gibson 	struct gpiohandle_data ghd;
923925ca369SKent Gibson 
924925ca369SKent Gibson 	/*
925925ca369SKent Gibson 	 * We can get the value for an event line but not set it,
926925ca369SKent Gibson 	 * because it is input by definition.
927925ca369SKent Gibson 	 */
928925ca369SKent Gibson 	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
929925ca369SKent Gibson 		int val;
930925ca369SKent Gibson 
931925ca369SKent Gibson 		memset(&ghd, 0, sizeof(ghd));
932925ca369SKent Gibson 
933925ca369SKent Gibson 		val = gpiod_get_value_cansleep(le->desc);
934925ca369SKent Gibson 		if (val < 0)
935925ca369SKent Gibson 			return val;
936925ca369SKent Gibson 		ghd.values[0] = val;
937925ca369SKent Gibson 
938925ca369SKent Gibson 		if (copy_to_user(ip, &ghd, sizeof(ghd)))
939925ca369SKent Gibson 			return -EFAULT;
940925ca369SKent Gibson 
941925ca369SKent Gibson 		return 0;
942925ca369SKent Gibson 	}
943925ca369SKent Gibson 	return -EINVAL;
944925ca369SKent Gibson }
945925ca369SKent Gibson 
946925ca369SKent Gibson #ifdef CONFIG_COMPAT
94749bc5279SKent Gibson static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
948925ca369SKent Gibson 				   unsigned long arg)
949925ca369SKent Gibson {
95049bc5279SKent Gibson 	return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
951925ca369SKent Gibson }
952925ca369SKent Gibson #endif
953925ca369SKent Gibson 
954925ca369SKent Gibson static const struct file_operations lineevent_fileops = {
955925ca369SKent Gibson 	.release = lineevent_release,
956925ca369SKent Gibson 	.read = lineevent_read,
957925ca369SKent Gibson 	.poll = lineevent_poll,
958925ca369SKent Gibson 	.owner = THIS_MODULE,
959925ca369SKent Gibson 	.llseek = noop_llseek,
960925ca369SKent Gibson 	.unlocked_ioctl = lineevent_ioctl,
961925ca369SKent Gibson #ifdef CONFIG_COMPAT
962925ca369SKent Gibson 	.compat_ioctl = lineevent_ioctl_compat,
963925ca369SKent Gibson #endif
964925ca369SKent Gibson };
965925ca369SKent Gibson 
966925ca369SKent Gibson static irqreturn_t lineevent_irq_thread(int irq, void *p)
967925ca369SKent Gibson {
968925ca369SKent Gibson 	struct lineevent_state *le = p;
969925ca369SKent Gibson 	struct gpioevent_data ge;
970925ca369SKent Gibson 	int ret;
971925ca369SKent Gibson 
972925ca369SKent Gibson 	/* Do not leak kernel stack to userspace */
973925ca369SKent Gibson 	memset(&ge, 0, sizeof(ge));
974925ca369SKent Gibson 
975925ca369SKent Gibson 	/*
976925ca369SKent Gibson 	 * We may be running from a nested threaded interrupt in which case
977925ca369SKent Gibson 	 * we didn't get the timestamp from lineevent_irq_handler().
978925ca369SKent Gibson 	 */
979925ca369SKent Gibson 	if (!le->timestamp)
980925ca369SKent Gibson 		ge.timestamp = ktime_get_ns();
981925ca369SKent Gibson 	else
982925ca369SKent Gibson 		ge.timestamp = le->timestamp;
983925ca369SKent Gibson 
984925ca369SKent Gibson 	if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
985925ca369SKent Gibson 	    && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
986925ca369SKent Gibson 		int level = gpiod_get_value_cansleep(le->desc);
987925ca369SKent Gibson 
988925ca369SKent Gibson 		if (level)
989925ca369SKent Gibson 			/* Emit low-to-high event */
990925ca369SKent Gibson 			ge.id = GPIOEVENT_EVENT_RISING_EDGE;
991925ca369SKent Gibson 		else
992925ca369SKent Gibson 			/* Emit high-to-low event */
993925ca369SKent Gibson 			ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
994925ca369SKent Gibson 	} else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
995925ca369SKent Gibson 		/* Emit low-to-high event */
996925ca369SKent Gibson 		ge.id = GPIOEVENT_EVENT_RISING_EDGE;
997925ca369SKent Gibson 	} else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
998925ca369SKent Gibson 		/* Emit high-to-low event */
999925ca369SKent Gibson 		ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1000925ca369SKent Gibson 	} else {
1001925ca369SKent Gibson 		return IRQ_NONE;
1002925ca369SKent Gibson 	}
1003925ca369SKent Gibson 
1004925ca369SKent Gibson 	ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
1005925ca369SKent Gibson 					    1, &le->wait.lock);
1006925ca369SKent Gibson 	if (ret)
1007925ca369SKent Gibson 		wake_up_poll(&le->wait, EPOLLIN);
1008925ca369SKent Gibson 	else
1009925ca369SKent Gibson 		pr_debug_ratelimited("event FIFO is full - event dropped\n");
1010925ca369SKent Gibson 
1011925ca369SKent Gibson 	return IRQ_HANDLED;
1012925ca369SKent Gibson }
1013925ca369SKent Gibson 
1014925ca369SKent Gibson static irqreturn_t lineevent_irq_handler(int irq, void *p)
1015925ca369SKent Gibson {
1016925ca369SKent Gibson 	struct lineevent_state *le = p;
1017925ca369SKent Gibson 
1018925ca369SKent Gibson 	/*
1019925ca369SKent Gibson 	 * Just store the timestamp in hardirq context so we get it as
1020925ca369SKent Gibson 	 * close in time as possible to the actual event.
1021925ca369SKent Gibson 	 */
1022925ca369SKent Gibson 	le->timestamp = ktime_get_ns();
1023925ca369SKent Gibson 
1024925ca369SKent Gibson 	return IRQ_WAKE_THREAD;
1025925ca369SKent Gibson }
1026925ca369SKent Gibson 
1027925ca369SKent Gibson static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1028925ca369SKent Gibson {
1029925ca369SKent Gibson 	struct gpioevent_request eventreq;
1030925ca369SKent Gibson 	struct lineevent_state *le;
1031925ca369SKent Gibson 	struct gpio_desc *desc;
1032925ca369SKent Gibson 	struct file *file;
1033925ca369SKent Gibson 	u32 offset;
1034925ca369SKent Gibson 	u32 lflags;
1035925ca369SKent Gibson 	u32 eflags;
1036925ca369SKent Gibson 	int fd;
1037925ca369SKent Gibson 	int ret;
103846824272SKent Gibson 	int irq, irqflags = 0;
1039925ca369SKent Gibson 
1040925ca369SKent Gibson 	if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1041925ca369SKent Gibson 		return -EFAULT;
1042925ca369SKent Gibson 
1043925ca369SKent Gibson 	offset = eventreq.lineoffset;
1044925ca369SKent Gibson 	lflags = eventreq.handleflags;
1045925ca369SKent Gibson 	eflags = eventreq.eventflags;
1046925ca369SKent Gibson 
1047925ca369SKent Gibson 	desc = gpiochip_get_desc(gdev->chip, offset);
1048925ca369SKent Gibson 	if (IS_ERR(desc))
1049925ca369SKent Gibson 		return PTR_ERR(desc);
1050925ca369SKent Gibson 
1051925ca369SKent Gibson 	/* Return an error if a unknown flag is set */
1052925ca369SKent Gibson 	if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1053925ca369SKent Gibson 	    (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1054925ca369SKent Gibson 		return -EINVAL;
1055925ca369SKent Gibson 
1056925ca369SKent Gibson 	/* This is just wrong: we don't look for events on output lines */
1057925ca369SKent Gibson 	if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1058925ca369SKent Gibson 	    (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1059925ca369SKent Gibson 	    (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1060925ca369SKent Gibson 		return -EINVAL;
1061925ca369SKent Gibson 
1062925ca369SKent Gibson 	/* Only one bias flag can be set. */
1063925ca369SKent Gibson 	if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1064925ca369SKent Gibson 	     (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1065925ca369SKent Gibson 			GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1066925ca369SKent Gibson 	    ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1067925ca369SKent Gibson 	     (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1068925ca369SKent Gibson 		return -EINVAL;
1069925ca369SKent Gibson 
1070925ca369SKent Gibson 	le = kzalloc(sizeof(*le), GFP_KERNEL);
1071925ca369SKent Gibson 	if (!le)
1072925ca369SKent Gibson 		return -ENOMEM;
1073925ca369SKent Gibson 	le->gdev = gdev;
1074925ca369SKent Gibson 	get_device(&gdev->dev);
1075925ca369SKent Gibson 
1076925ca369SKent Gibson 	/* Make sure this is terminated */
1077925ca369SKent Gibson 	eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
1078925ca369SKent Gibson 	if (strlen(eventreq.consumer_label)) {
1079925ca369SKent Gibson 		le->label = kstrdup(eventreq.consumer_label,
1080925ca369SKent Gibson 				    GFP_KERNEL);
1081925ca369SKent Gibson 		if (!le->label) {
1082925ca369SKent Gibson 			ret = -ENOMEM;
1083925ca369SKent Gibson 			goto out_free_le;
1084925ca369SKent Gibson 		}
1085925ca369SKent Gibson 	}
1086925ca369SKent Gibson 
1087925ca369SKent Gibson 	ret = gpiod_request(desc, le->label);
1088925ca369SKent Gibson 	if (ret)
108946824272SKent Gibson 		goto out_free_le;
1090925ca369SKent Gibson 	le->desc = desc;
1091925ca369SKent Gibson 	le->eflags = eflags;
1092925ca369SKent Gibson 
1093c274b58aSKent Gibson 	linehandle_flags_to_desc_flags(lflags, &desc->flags);
1094925ca369SKent Gibson 
1095925ca369SKent Gibson 	ret = gpiod_direction_input(desc);
1096925ca369SKent Gibson 	if (ret)
109746824272SKent Gibson 		goto out_free_le;
1098925ca369SKent Gibson 
10996accc376SKent Gibson 	blocking_notifier_call_chain(&desc->gdev->notifier,
1100*aad95584SKent Gibson 				     GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1101925ca369SKent Gibson 
110246824272SKent Gibson 	irq = gpiod_to_irq(desc);
110346824272SKent Gibson 	if (irq <= 0) {
1104925ca369SKent Gibson 		ret = -ENODEV;
110546824272SKent Gibson 		goto out_free_le;
1106925ca369SKent Gibson 	}
110746824272SKent Gibson 	le->irq = irq;
1108925ca369SKent Gibson 
1109925ca369SKent Gibson 	if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
1110925ca369SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1111925ca369SKent Gibson 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1112925ca369SKent Gibson 	if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
1113925ca369SKent Gibson 		irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1114925ca369SKent Gibson 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1115925ca369SKent Gibson 	irqflags |= IRQF_ONESHOT;
1116925ca369SKent Gibson 
1117925ca369SKent Gibson 	INIT_KFIFO(le->events);
1118925ca369SKent Gibson 	init_waitqueue_head(&le->wait);
1119925ca369SKent Gibson 
1120925ca369SKent Gibson 	/* Request a thread to read the events */
1121925ca369SKent Gibson 	ret = request_threaded_irq(le->irq,
1122925ca369SKent Gibson 				   lineevent_irq_handler,
1123925ca369SKent Gibson 				   lineevent_irq_thread,
1124925ca369SKent Gibson 				   irqflags,
1125925ca369SKent Gibson 				   le->label,
1126925ca369SKent Gibson 				   le);
1127925ca369SKent Gibson 	if (ret)
112846824272SKent Gibson 		goto out_free_le;
1129925ca369SKent Gibson 
1130925ca369SKent Gibson 	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1131925ca369SKent Gibson 	if (fd < 0) {
1132925ca369SKent Gibson 		ret = fd;
113346824272SKent Gibson 		goto out_free_le;
1134925ca369SKent Gibson 	}
1135925ca369SKent Gibson 
1136925ca369SKent Gibson 	file = anon_inode_getfile("gpio-event",
1137925ca369SKent Gibson 				  &lineevent_fileops,
1138925ca369SKent Gibson 				  le,
1139925ca369SKent Gibson 				  O_RDONLY | O_CLOEXEC);
1140925ca369SKent Gibson 	if (IS_ERR(file)) {
1141925ca369SKent Gibson 		ret = PTR_ERR(file);
1142925ca369SKent Gibson 		goto out_put_unused_fd;
1143925ca369SKent Gibson 	}
1144925ca369SKent Gibson 
1145925ca369SKent Gibson 	eventreq.fd = fd;
1146925ca369SKent Gibson 	if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
1147925ca369SKent Gibson 		/*
1148925ca369SKent Gibson 		 * fput() will trigger the release() callback, so do not go onto
1149925ca369SKent Gibson 		 * the regular error cleanup path here.
1150925ca369SKent Gibson 		 */
1151925ca369SKent Gibson 		fput(file);
1152925ca369SKent Gibson 		put_unused_fd(fd);
1153925ca369SKent Gibson 		return -EFAULT;
1154925ca369SKent Gibson 	}
1155925ca369SKent Gibson 
1156925ca369SKent Gibson 	fd_install(fd, file);
1157925ca369SKent Gibson 
1158925ca369SKent Gibson 	return 0;
1159925ca369SKent Gibson 
1160925ca369SKent Gibson out_put_unused_fd:
1161925ca369SKent Gibson 	put_unused_fd(fd);
1162925ca369SKent Gibson out_free_le:
116346824272SKent Gibson 	lineevent_free(le);
1164925ca369SKent Gibson 	return ret;
1165925ca369SKent Gibson }
1166925ca369SKent Gibson 
1167*aad95584SKent Gibson static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
1168*aad95584SKent Gibson 				    struct gpioline_info *info_v1)
1169*aad95584SKent Gibson {
1170*aad95584SKent Gibson 	u64 flagsv2 = info_v2->flags;
1171*aad95584SKent Gibson 
1172*aad95584SKent Gibson 	memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
1173*aad95584SKent Gibson 	memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
1174*aad95584SKent Gibson 	info_v1->line_offset = info_v2->offset;
1175*aad95584SKent Gibson 	info_v1->flags = 0;
1176*aad95584SKent Gibson 
1177*aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
1178*aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_KERNEL;
1179*aad95584SKent Gibson 
1180*aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
1181*aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
1182*aad95584SKent Gibson 
1183*aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
1184*aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
1185*aad95584SKent Gibson 
1186*aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
1187*aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
1188*aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
1189*aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
1190*aad95584SKent Gibson 
1191*aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
1192*aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
1193*aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
1194*aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
1195*aad95584SKent Gibson 	if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
1196*aad95584SKent Gibson 		info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
1197*aad95584SKent Gibson }
1198*aad95584SKent Gibson 
1199*aad95584SKent Gibson static void gpio_v2_line_info_changed_to_v1(
1200*aad95584SKent Gibson 		struct gpio_v2_line_info_changed *lic_v2,
1201*aad95584SKent Gibson 		struct gpioline_info_changed *lic_v1)
1202*aad95584SKent Gibson {
1203*aad95584SKent Gibson 	gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
1204*aad95584SKent Gibson 	lic_v1->timestamp = lic_v2->timestamp_ns;
1205*aad95584SKent Gibson 	lic_v1->event_type = lic_v2->event_type;
1206*aad95584SKent Gibson }
1207*aad95584SKent Gibson 
12083c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
12093c0d9c63SKent Gibson 
1210925ca369SKent Gibson static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
1211*aad95584SKent Gibson 				  struct gpio_v2_line_info *info)
1212925ca369SKent Gibson {
1213925ca369SKent Gibson 	struct gpio_chip *gc = desc->gdev->chip;
1214925ca369SKent Gibson 	bool ok_for_pinctrl;
1215925ca369SKent Gibson 	unsigned long flags;
1216925ca369SKent Gibson 
121769e4e136SKent Gibson 	memset(info, 0, sizeof(*info));
1218*aad95584SKent Gibson 	info->offset = gpio_chip_hwgpio(desc);
12190dc11e3aSKent Gibson 
1220925ca369SKent Gibson 	/*
1221925ca369SKent Gibson 	 * This function takes a mutex so we must check this before taking
1222925ca369SKent Gibson 	 * the spinlock.
1223925ca369SKent Gibson 	 *
1224925ca369SKent Gibson 	 * FIXME: find a non-racy way to retrieve this information. Maybe a
1225925ca369SKent Gibson 	 * lock common to both frameworks?
1226925ca369SKent Gibson 	 */
1227925ca369SKent Gibson 	ok_for_pinctrl =
1228*aad95584SKent Gibson 		pinctrl_gpio_can_use_line(gc->base + info->offset);
1229925ca369SKent Gibson 
1230925ca369SKent Gibson 	spin_lock_irqsave(&gpio_lock, flags);
1231925ca369SKent Gibson 
123269e4e136SKent Gibson 	if (desc->name)
123369e4e136SKent Gibson 		strscpy(info->name, desc->name, sizeof(info->name));
1234925ca369SKent Gibson 
123569e4e136SKent Gibson 	if (desc->label)
123669e4e136SKent Gibson 		strscpy(info->consumer, desc->label, sizeof(info->consumer));
1237925ca369SKent Gibson 
1238925ca369SKent Gibson 	/*
1239925ca369SKent Gibson 	 * Userspace only need to know that the kernel is using this GPIO so
1240925ca369SKent Gibson 	 * it can't use it.
1241925ca369SKent Gibson 	 */
1242925ca369SKent Gibson 	info->flags = 0;
1243925ca369SKent Gibson 	if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1244925ca369SKent Gibson 	    test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1245925ca369SKent Gibson 	    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1246925ca369SKent Gibson 	    test_bit(FLAG_EXPORT, &desc->flags) ||
1247925ca369SKent Gibson 	    test_bit(FLAG_SYSFS, &desc->flags) ||
1248925ca369SKent Gibson 	    !ok_for_pinctrl)
1249*aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_USED;
1250*aad95584SKent Gibson 
1251925ca369SKent Gibson 	if (test_bit(FLAG_IS_OUT, &desc->flags))
1252*aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
1253*aad95584SKent Gibson 	else
1254*aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_INPUT;
1255*aad95584SKent Gibson 
1256925ca369SKent Gibson 	if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1257*aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
1258*aad95584SKent Gibson 
1259925ca369SKent Gibson 	if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1260*aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
1261925ca369SKent Gibson 	if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1262*aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
1263*aad95584SKent Gibson 
1264925ca369SKent Gibson 	if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
1265*aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
1266925ca369SKent Gibson 	if (test_bit(FLAG_PULL_DOWN, &desc->flags))
1267*aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
1268925ca369SKent Gibson 	if (test_bit(FLAG_PULL_UP, &desc->flags))
1269*aad95584SKent Gibson 		info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
1270925ca369SKent Gibson 
1271925ca369SKent Gibson 	spin_unlock_irqrestore(&gpio_lock, flags);
1272925ca369SKent Gibson }
1273925ca369SKent Gibson 
1274925ca369SKent Gibson struct gpio_chardev_data {
1275925ca369SKent Gibson 	struct gpio_device *gdev;
1276925ca369SKent Gibson 	wait_queue_head_t wait;
1277*aad95584SKent Gibson 	DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
1278925ca369SKent Gibson 	struct notifier_block lineinfo_changed_nb;
1279925ca369SKent Gibson 	unsigned long *watched_lines;
1280*aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1281*aad95584SKent Gibson 	atomic_t watch_abi_version;
1282*aad95584SKent Gibson #endif
1283925ca369SKent Gibson };
1284925ca369SKent Gibson 
1285*aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1286*aad95584SKent Gibson /*
1287*aad95584SKent Gibson  * returns 0 if the versions match, else the previously selected ABI version
1288*aad95584SKent Gibson  */
1289*aad95584SKent Gibson static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
1290*aad95584SKent Gibson 				       unsigned int version)
1291*aad95584SKent Gibson {
1292*aad95584SKent Gibson 	int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
1293*aad95584SKent Gibson 
1294*aad95584SKent Gibson 	if (abiv == version)
1295*aad95584SKent Gibson 		return 0;
1296*aad95584SKent Gibson 
1297*aad95584SKent Gibson 	return abiv;
1298*aad95584SKent Gibson }
1299*aad95584SKent Gibson #endif
1300*aad95584SKent Gibson 
1301*aad95584SKent Gibson static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
1302*aad95584SKent Gibson 			bool watch)
1303*aad95584SKent Gibson {
1304*aad95584SKent Gibson 	struct gpio_desc *desc;
1305*aad95584SKent Gibson 	struct gpio_v2_line_info lineinfo;
1306*aad95584SKent Gibson 
1307*aad95584SKent Gibson 	if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1308*aad95584SKent Gibson 		return -EFAULT;
1309*aad95584SKent Gibson 
1310*aad95584SKent Gibson 	if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
1311*aad95584SKent Gibson 		return -EINVAL;
1312*aad95584SKent Gibson 
1313*aad95584SKent Gibson 	desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
1314*aad95584SKent Gibson 	if (IS_ERR(desc))
1315*aad95584SKent Gibson 		return PTR_ERR(desc);
1316*aad95584SKent Gibson 
1317*aad95584SKent Gibson 	if (watch) {
1318*aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1319*aad95584SKent Gibson 		if (lineinfo_ensure_abi_version(cdev, 2))
1320*aad95584SKent Gibson 			return -EPERM;
1321*aad95584SKent Gibson #endif
1322*aad95584SKent Gibson 		if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
1323*aad95584SKent Gibson 			return -EBUSY;
1324*aad95584SKent Gibson 	}
1325*aad95584SKent Gibson 	gpio_desc_to_lineinfo(desc, &lineinfo);
1326*aad95584SKent Gibson 
1327*aad95584SKent Gibson 	if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
1328*aad95584SKent Gibson 		if (watch)
1329*aad95584SKent Gibson 			clear_bit(lineinfo.offset, cdev->watched_lines);
1330*aad95584SKent Gibson 		return -EFAULT;
1331*aad95584SKent Gibson 	}
1332*aad95584SKent Gibson 
1333*aad95584SKent Gibson 	return 0;
1334*aad95584SKent Gibson }
1335*aad95584SKent Gibson 
1336925ca369SKent Gibson /*
1337925ca369SKent Gibson  * gpio_ioctl() - ioctl handler for the GPIO chardev
1338925ca369SKent Gibson  */
133949bc5279SKent Gibson static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1340925ca369SKent Gibson {
1341e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
1342e2b781c5SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
1343925ca369SKent Gibson 	struct gpio_chip *gc = gdev->chip;
1344925ca369SKent Gibson 	void __user *ip = (void __user *)arg;
1345925ca369SKent Gibson 	__u32 offset;
1346925ca369SKent Gibson 
1347925ca369SKent Gibson 	/* We fail any subsequent ioctl():s when the chip is gone */
1348925ca369SKent Gibson 	if (!gc)
1349925ca369SKent Gibson 		return -ENODEV;
1350925ca369SKent Gibson 
1351925ca369SKent Gibson 	/* Fill in the struct and pass to userspace */
1352925ca369SKent Gibson 	if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
1353925ca369SKent Gibson 		struct gpiochip_info chipinfo;
1354925ca369SKent Gibson 
1355925ca369SKent Gibson 		memset(&chipinfo, 0, sizeof(chipinfo));
1356925ca369SKent Gibson 
135769e4e136SKent Gibson 		strscpy(chipinfo.name, dev_name(&gdev->dev),
1358925ca369SKent Gibson 			sizeof(chipinfo.name));
135969e4e136SKent Gibson 		strscpy(chipinfo.label, gdev->label,
1360925ca369SKent Gibson 			sizeof(chipinfo.label));
1361925ca369SKent Gibson 		chipinfo.lines = gdev->ngpio;
1362925ca369SKent Gibson 		if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1363925ca369SKent Gibson 			return -EFAULT;
1364925ca369SKent Gibson 		return 0;
13653c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1366925ca369SKent Gibson 	} else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
1367*aad95584SKent Gibson 		struct gpio_desc *desc;
1368925ca369SKent Gibson 		struct gpioline_info lineinfo;
1369*aad95584SKent Gibson 		struct gpio_v2_line_info lineinfo_v2;
1370925ca369SKent Gibson 
1371925ca369SKent Gibson 		if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1372925ca369SKent Gibson 			return -EFAULT;
1373925ca369SKent Gibson 
13741bf7ba40SKent Gibson 		/* this doubles as a range check on line_offset */
1375925ca369SKent Gibson 		desc = gpiochip_get_desc(gc, lineinfo.line_offset);
1376925ca369SKent Gibson 		if (IS_ERR(desc))
1377925ca369SKent Gibson 			return PTR_ERR(desc);
1378925ca369SKent Gibson 
1379*aad95584SKent Gibson 		gpio_desc_to_lineinfo(desc, &lineinfo_v2);
1380*aad95584SKent Gibson 		gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
1381925ca369SKent Gibson 
1382925ca369SKent Gibson 		if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1383925ca369SKent Gibson 			return -EFAULT;
1384925ca369SKent Gibson 		return 0;
1385925ca369SKent Gibson 	} else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1386925ca369SKent Gibson 		return linehandle_create(gdev, ip);
1387925ca369SKent Gibson 	} else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1388925ca369SKent Gibson 		return lineevent_create(gdev, ip);
1389925ca369SKent Gibson 	} else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
1390*aad95584SKent Gibson 		struct gpio_desc *desc;
1391925ca369SKent Gibson 		struct gpioline_info lineinfo;
1392*aad95584SKent Gibson 		struct gpio_v2_line_info lineinfo_v2;
1393925ca369SKent Gibson 
1394925ca369SKent Gibson 		if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1395925ca369SKent Gibson 			return -EFAULT;
1396925ca369SKent Gibson 
13971bf7ba40SKent Gibson 		/* this doubles as a range check on line_offset */
1398925ca369SKent Gibson 		desc = gpiochip_get_desc(gc, lineinfo.line_offset);
1399925ca369SKent Gibson 		if (IS_ERR(desc))
1400925ca369SKent Gibson 			return PTR_ERR(desc);
1401925ca369SKent Gibson 
1402*aad95584SKent Gibson 		if (lineinfo_ensure_abi_version(cdev, 1))
1403*aad95584SKent Gibson 			return -EPERM;
1404*aad95584SKent Gibson 
14051bf7ba40SKent Gibson 		if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
1406925ca369SKent Gibson 			return -EBUSY;
1407925ca369SKent Gibson 
1408*aad95584SKent Gibson 		gpio_desc_to_lineinfo(desc, &lineinfo_v2);
1409*aad95584SKent Gibson 		gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
1410925ca369SKent Gibson 
1411f30ef3e8SKent Gibson 		if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
14121bf7ba40SKent Gibson 			clear_bit(lineinfo.line_offset, cdev->watched_lines);
1413925ca369SKent Gibson 			return -EFAULT;
1414f30ef3e8SKent Gibson 		}
1415925ca369SKent Gibson 
1416925ca369SKent Gibson 		return 0;
14173c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
1418*aad95584SKent Gibson 	} else if (cmd == GPIO_V2_GET_LINEINFO_IOCTL ||
1419*aad95584SKent Gibson 		   cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL) {
1420*aad95584SKent Gibson 		return lineinfo_get(cdev, ip,
1421*aad95584SKent Gibson 				    cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL);
14223c0d9c63SKent Gibson 	} else if (cmd == GPIO_V2_GET_LINE_IOCTL) {
14233c0d9c63SKent Gibson 		return linereq_create(gdev, ip);
1424925ca369SKent Gibson 	} else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) {
1425925ca369SKent Gibson 		if (copy_from_user(&offset, ip, sizeof(offset)))
1426925ca369SKent Gibson 			return -EFAULT;
1427925ca369SKent Gibson 
14281bf7ba40SKent Gibson 		if (offset >= cdev->gdev->ngpio)
14291bf7ba40SKent Gibson 			return -EINVAL;
1430925ca369SKent Gibson 
14311bf7ba40SKent Gibson 		if (!test_and_clear_bit(offset, cdev->watched_lines))
1432925ca369SKent Gibson 			return -EBUSY;
1433925ca369SKent Gibson 
1434925ca369SKent Gibson 		return 0;
1435925ca369SKent Gibson 	}
1436925ca369SKent Gibson 	return -EINVAL;
1437925ca369SKent Gibson }
1438925ca369SKent Gibson 
1439925ca369SKent Gibson #ifdef CONFIG_COMPAT
144049bc5279SKent Gibson static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
1441925ca369SKent Gibson 			      unsigned long arg)
1442925ca369SKent Gibson {
144349bc5279SKent Gibson 	return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1444925ca369SKent Gibson }
1445925ca369SKent Gibson #endif
1446925ca369SKent Gibson 
1447925ca369SKent Gibson static struct gpio_chardev_data *
1448925ca369SKent Gibson to_gpio_chardev_data(struct notifier_block *nb)
1449925ca369SKent Gibson {
1450925ca369SKent Gibson 	return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
1451925ca369SKent Gibson }
1452925ca369SKent Gibson 
1453925ca369SKent Gibson static int lineinfo_changed_notify(struct notifier_block *nb,
1454925ca369SKent Gibson 				   unsigned long action, void *data)
1455925ca369SKent Gibson {
1456e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
1457*aad95584SKent Gibson 	struct gpio_v2_line_info_changed chg;
1458925ca369SKent Gibson 	struct gpio_desc *desc = data;
1459925ca369SKent Gibson 	int ret;
1460925ca369SKent Gibson 
1461e2b781c5SKent Gibson 	if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
1462925ca369SKent Gibson 		return NOTIFY_DONE;
1463925ca369SKent Gibson 
1464925ca369SKent Gibson 	memset(&chg, 0, sizeof(chg));
1465925ca369SKent Gibson 	chg.event_type = action;
1466*aad95584SKent Gibson 	chg.timestamp_ns = ktime_get_ns();
1467925ca369SKent Gibson 	gpio_desc_to_lineinfo(desc, &chg.info);
1468925ca369SKent Gibson 
1469e2b781c5SKent Gibson 	ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
1470925ca369SKent Gibson 	if (ret)
1471e2b781c5SKent Gibson 		wake_up_poll(&cdev->wait, EPOLLIN);
1472925ca369SKent Gibson 	else
1473925ca369SKent Gibson 		pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
1474925ca369SKent Gibson 
1475925ca369SKent Gibson 	return NOTIFY_OK;
1476925ca369SKent Gibson }
1477925ca369SKent Gibson 
147849bc5279SKent Gibson static __poll_t lineinfo_watch_poll(struct file *file,
1479925ca369SKent Gibson 				    struct poll_table_struct *pollt)
1480925ca369SKent Gibson {
1481e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
1482925ca369SKent Gibson 	__poll_t events = 0;
1483925ca369SKent Gibson 
1484e2b781c5SKent Gibson 	poll_wait(file, &cdev->wait, pollt);
1485925ca369SKent Gibson 
1486e2b781c5SKent Gibson 	if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
1487e2b781c5SKent Gibson 						 &cdev->wait.lock))
1488925ca369SKent Gibson 		events = EPOLLIN | EPOLLRDNORM;
1489925ca369SKent Gibson 
1490925ca369SKent Gibson 	return events;
1491925ca369SKent Gibson }
1492925ca369SKent Gibson 
149349bc5279SKent Gibson static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
1494925ca369SKent Gibson 				   size_t count, loff_t *off)
1495925ca369SKent Gibson {
1496e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
1497*aad95584SKent Gibson 	struct gpio_v2_line_info_changed event;
1498925ca369SKent Gibson 	ssize_t bytes_read = 0;
1499925ca369SKent Gibson 	int ret;
1500*aad95584SKent Gibson 	size_t event_size;
1501925ca369SKent Gibson 
1502*aad95584SKent Gibson #ifndef CONFIG_GPIO_CDEV_V1
1503*aad95584SKent Gibson 	event_size = sizeof(struct gpio_v2_line_info_changed);
1504*aad95584SKent Gibson 	if (count < event_size)
1505925ca369SKent Gibson 		return -EINVAL;
1506*aad95584SKent Gibson #endif
1507925ca369SKent Gibson 
1508925ca369SKent Gibson 	do {
1509e2b781c5SKent Gibson 		spin_lock(&cdev->wait.lock);
1510e2b781c5SKent Gibson 		if (kfifo_is_empty(&cdev->events)) {
1511925ca369SKent Gibson 			if (bytes_read) {
1512e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
1513925ca369SKent Gibson 				return bytes_read;
1514925ca369SKent Gibson 			}
1515925ca369SKent Gibson 
151649bc5279SKent Gibson 			if (file->f_flags & O_NONBLOCK) {
1517e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
1518925ca369SKent Gibson 				return -EAGAIN;
1519925ca369SKent Gibson 			}
1520925ca369SKent Gibson 
1521e2b781c5SKent Gibson 			ret = wait_event_interruptible_locked(cdev->wait,
1522e2b781c5SKent Gibson 					!kfifo_is_empty(&cdev->events));
1523925ca369SKent Gibson 			if (ret) {
1524e2b781c5SKent Gibson 				spin_unlock(&cdev->wait.lock);
1525925ca369SKent Gibson 				return ret;
1526925ca369SKent Gibson 			}
1527925ca369SKent Gibson 		}
1528*aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1529*aad95584SKent Gibson 		/* must be after kfifo check so watch_abi_version is set */
1530*aad95584SKent Gibson 		if (atomic_read(&cdev->watch_abi_version) == 2)
1531*aad95584SKent Gibson 			event_size = sizeof(struct gpio_v2_line_info_changed);
1532*aad95584SKent Gibson 		else
1533*aad95584SKent Gibson 			event_size = sizeof(struct gpioline_info_changed);
1534*aad95584SKent Gibson 		if (count < event_size) {
1535*aad95584SKent Gibson 			spin_unlock(&cdev->wait.lock);
1536*aad95584SKent Gibson 			return -EINVAL;
1537*aad95584SKent Gibson 		}
1538*aad95584SKent Gibson #endif
1539e2b781c5SKent Gibson 		ret = kfifo_out(&cdev->events, &event, 1);
1540e2b781c5SKent Gibson 		spin_unlock(&cdev->wait.lock);
1541925ca369SKent Gibson 		if (ret != 1) {
1542925ca369SKent Gibson 			ret = -EIO;
1543925ca369SKent Gibson 			break;
1544925ca369SKent Gibson 			/* We should never get here. See lineevent_read(). */
1545925ca369SKent Gibson 		}
1546925ca369SKent Gibson 
1547*aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1548*aad95584SKent Gibson 		if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
1549*aad95584SKent Gibson 			if (copy_to_user(buf + bytes_read, &event, event_size))
1550925ca369SKent Gibson 				return -EFAULT;
1551*aad95584SKent Gibson 		} else {
1552*aad95584SKent Gibson 			struct gpioline_info_changed event_v1;
1553*aad95584SKent Gibson 
1554*aad95584SKent Gibson 			gpio_v2_line_info_changed_to_v1(&event, &event_v1);
1555*aad95584SKent Gibson 			if (copy_to_user(buf + bytes_read, &event_v1,
1556*aad95584SKent Gibson 					 event_size))
1557*aad95584SKent Gibson 				return -EFAULT;
1558*aad95584SKent Gibson 		}
1559*aad95584SKent Gibson #else
1560*aad95584SKent Gibson 		if (copy_to_user(buf + bytes_read, &event, event_size))
1561*aad95584SKent Gibson 			return -EFAULT;
1562*aad95584SKent Gibson #endif
1563*aad95584SKent Gibson 		bytes_read += event_size;
1564925ca369SKent Gibson 	} while (count >= bytes_read + sizeof(event));
1565925ca369SKent Gibson 
1566925ca369SKent Gibson 	return bytes_read;
1567925ca369SKent Gibson }
1568925ca369SKent Gibson 
1569925ca369SKent Gibson /**
1570925ca369SKent Gibson  * gpio_chrdev_open() - open the chardev for ioctl operations
1571925ca369SKent Gibson  * @inode: inode for this chardev
157249bc5279SKent Gibson  * @file: file struct for storing private data
1573925ca369SKent Gibson  * Returns 0 on success
1574925ca369SKent Gibson  */
157549bc5279SKent Gibson static int gpio_chrdev_open(struct inode *inode, struct file *file)
1576925ca369SKent Gibson {
1577925ca369SKent Gibson 	struct gpio_device *gdev = container_of(inode->i_cdev,
1578925ca369SKent Gibson 						struct gpio_device, chrdev);
1579e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev;
1580925ca369SKent Gibson 	int ret = -ENOMEM;
1581925ca369SKent Gibson 
1582925ca369SKent Gibson 	/* Fail on open if the backing gpiochip is gone */
1583925ca369SKent Gibson 	if (!gdev->chip)
1584925ca369SKent Gibson 		return -ENODEV;
1585925ca369SKent Gibson 
1586e2b781c5SKent Gibson 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
1587e2b781c5SKent Gibson 	if (!cdev)
1588925ca369SKent Gibson 		return -ENOMEM;
1589925ca369SKent Gibson 
1590e2b781c5SKent Gibson 	cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
1591e2b781c5SKent Gibson 	if (!cdev->watched_lines)
1592e2b781c5SKent Gibson 		goto out_free_cdev;
1593925ca369SKent Gibson 
1594e2b781c5SKent Gibson 	init_waitqueue_head(&cdev->wait);
1595e2b781c5SKent Gibson 	INIT_KFIFO(cdev->events);
1596e2b781c5SKent Gibson 	cdev->gdev = gdev;
1597925ca369SKent Gibson 
1598e2b781c5SKent Gibson 	cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
15996accc376SKent Gibson 	ret = blocking_notifier_chain_register(&gdev->notifier,
1600e2b781c5SKent Gibson 					       &cdev->lineinfo_changed_nb);
1601925ca369SKent Gibson 	if (ret)
1602925ca369SKent Gibson 		goto out_free_bitmap;
1603925ca369SKent Gibson 
1604925ca369SKent Gibson 	get_device(&gdev->dev);
1605e2b781c5SKent Gibson 	file->private_data = cdev;
1606925ca369SKent Gibson 
160749bc5279SKent Gibson 	ret = nonseekable_open(inode, file);
1608925ca369SKent Gibson 	if (ret)
1609925ca369SKent Gibson 		goto out_unregister_notifier;
1610925ca369SKent Gibson 
1611925ca369SKent Gibson 	return ret;
1612925ca369SKent Gibson 
1613925ca369SKent Gibson out_unregister_notifier:
16146accc376SKent Gibson 	blocking_notifier_chain_unregister(&gdev->notifier,
1615e2b781c5SKent Gibson 					   &cdev->lineinfo_changed_nb);
1616925ca369SKent Gibson out_free_bitmap:
1617e2b781c5SKent Gibson 	bitmap_free(cdev->watched_lines);
1618e2b781c5SKent Gibson out_free_cdev:
1619e2b781c5SKent Gibson 	kfree(cdev);
1620925ca369SKent Gibson 	return ret;
1621925ca369SKent Gibson }
1622925ca369SKent Gibson 
1623925ca369SKent Gibson /**
1624925ca369SKent Gibson  * gpio_chrdev_release() - close chardev after ioctl operations
1625925ca369SKent Gibson  * @inode: inode for this chardev
162649bc5279SKent Gibson  * @file: file struct for storing private data
1627925ca369SKent Gibson  * Returns 0 on success
1628925ca369SKent Gibson  */
162949bc5279SKent Gibson static int gpio_chrdev_release(struct inode *inode, struct file *file)
1630925ca369SKent Gibson {
1631e2b781c5SKent Gibson 	struct gpio_chardev_data *cdev = file->private_data;
1632e2b781c5SKent Gibson 	struct gpio_device *gdev = cdev->gdev;
1633925ca369SKent Gibson 
1634e2b781c5SKent Gibson 	bitmap_free(cdev->watched_lines);
16356accc376SKent Gibson 	blocking_notifier_chain_unregister(&gdev->notifier,
1636e2b781c5SKent Gibson 					   &cdev->lineinfo_changed_nb);
1637925ca369SKent Gibson 	put_device(&gdev->dev);
1638e2b781c5SKent Gibson 	kfree(cdev);
1639925ca369SKent Gibson 
1640925ca369SKent Gibson 	return 0;
1641925ca369SKent Gibson }
1642925ca369SKent Gibson 
1643925ca369SKent Gibson static const struct file_operations gpio_fileops = {
1644925ca369SKent Gibson 	.release = gpio_chrdev_release,
1645925ca369SKent Gibson 	.open = gpio_chrdev_open,
1646925ca369SKent Gibson 	.poll = lineinfo_watch_poll,
1647925ca369SKent Gibson 	.read = lineinfo_watch_read,
1648925ca369SKent Gibson 	.owner = THIS_MODULE,
1649925ca369SKent Gibson 	.llseek = no_llseek,
1650925ca369SKent Gibson 	.unlocked_ioctl = gpio_ioctl,
1651925ca369SKent Gibson #ifdef CONFIG_COMPAT
1652925ca369SKent Gibson 	.compat_ioctl = gpio_ioctl_compat,
1653925ca369SKent Gibson #endif
1654925ca369SKent Gibson };
1655925ca369SKent Gibson 
1656925ca369SKent Gibson int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
1657925ca369SKent Gibson {
1658925ca369SKent Gibson 	int ret;
1659925ca369SKent Gibson 
1660925ca369SKent Gibson 	cdev_init(&gdev->chrdev, &gpio_fileops);
1661925ca369SKent Gibson 	gdev->chrdev.owner = THIS_MODULE;
1662925ca369SKent Gibson 	gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
1663925ca369SKent Gibson 
1664925ca369SKent Gibson 	ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
1665925ca369SKent Gibson 	if (ret)
1666925ca369SKent Gibson 		return ret;
1667925ca369SKent Gibson 
1668925ca369SKent Gibson 	chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1669925ca369SKent Gibson 		 MAJOR(devt), gdev->id);
1670925ca369SKent Gibson 
1671925ca369SKent Gibson 	return 0;
1672925ca369SKent Gibson }
1673925ca369SKent Gibson 
1674925ca369SKent Gibson void gpiolib_cdev_unregister(struct gpio_device *gdev)
1675925ca369SKent Gibson {
1676925ca369SKent Gibson 	cdev_device_del(&gdev->chrdev, &gdev->dev);
1677925ca369SKent Gibson }
1678