1925ca369SKent Gibson // SPDX-License-Identifier: GPL-2.0
2925ca369SKent Gibson
3d189f627SKent Gibson #include <linux/anon_inodes.h>
43c0d9c63SKent Gibson #include <linux/atomic.h>
5925ca369SKent Gibson #include <linux/bitmap.h>
63c0d9c63SKent Gibson #include <linux/build_bug.h>
7d189f627SKent Gibson #include <linux/cdev.h>
8309364bcSKent Gibson #include <linux/cleanup.h>
9d189f627SKent Gibson #include <linux/compat.h>
1065cff704SKent Gibson #include <linux/compiler.h>
11925ca369SKent Gibson #include <linux/device.h>
12925ca369SKent Gibson #include <linux/err.h>
13d189f627SKent Gibson #include <linux/file.h>
14925ca369SKent Gibson #include <linux/gpio.h>
15925ca369SKent Gibson #include <linux/gpio/driver.h>
1652ee7c02SAndy Shevchenko #include <linux/hte.h>
17d189f627SKent Gibson #include <linux/interrupt.h>
18d189f627SKent Gibson #include <linux/irqreturn.h>
19d189f627SKent Gibson #include <linux/kernel.h>
20925ca369SKent Gibson #include <linux/kfifo.h>
21d189f627SKent Gibson #include <linux/module.h>
22a54756cbSKent Gibson #include <linux/mutex.h>
23d189f627SKent Gibson #include <linux/pinctrl/consumer.h>
24925ca369SKent Gibson #include <linux/poll.h>
25309364bcSKent Gibson #include <linux/rbtree.h>
2652ee7c02SAndy Shevchenko #include <linux/seq_file.h>
27d189f627SKent Gibson #include <linux/spinlock.h>
28925ca369SKent Gibson #include <linux/timekeeping.h>
29d189f627SKent Gibson #include <linux/uaccess.h>
3065cff704SKent Gibson #include <linux/workqueue.h>
3152ee7c02SAndy Shevchenko
32925ca369SKent Gibson #include <uapi/linux/gpio.h>
33925ca369SKent Gibson
34925ca369SKent Gibson #include "gpiolib.h"
35925ca369SKent Gibson #include "gpiolib-cdev.h"
36925ca369SKent Gibson
373c0d9c63SKent Gibson /*
383c0d9c63SKent Gibson * Array sizes must ensure 64-bit alignment and not create holes in the
393c0d9c63SKent Gibson * struct packing.
403c0d9c63SKent Gibson */
413c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2));
423c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8));
433c0d9c63SKent Gibson
443c0d9c63SKent Gibson /*
453c0d9c63SKent Gibson * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility
463c0d9c63SKent Gibson */
473c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8));
483c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8));
493c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8));
503c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8));
513c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8));
523c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8));
533c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8));
543c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8));
553c0d9c63SKent Gibson
56925ca369SKent Gibson /* Character device interface to GPIO.
57925ca369SKent Gibson *
58925ca369SKent Gibson * The GPIO character device, /dev/gpiochipN, provides userspace an
59925ca369SKent Gibson * interface to gpiolib GPIOs via ioctl()s.
60925ca369SKent Gibson */
61925ca369SKent Gibson
62bdbbae24SBartosz Golaszewski typedef __poll_t (*poll_fn)(struct file *, struct poll_table_struct *);
63bdbbae24SBartosz Golaszewski typedef long (*ioctl_fn)(struct file *, unsigned int, unsigned long);
64bdbbae24SBartosz Golaszewski typedef ssize_t (*read_fn)(struct file *, char __user *,
65bdbbae24SBartosz Golaszewski size_t count, loff_t *);
66bdbbae24SBartosz Golaszewski
call_poll_locked(struct file * file,struct poll_table_struct * wait,struct gpio_device * gdev,poll_fn func)67bdbbae24SBartosz Golaszewski static __poll_t call_poll_locked(struct file *file,
68bdbbae24SBartosz Golaszewski struct poll_table_struct *wait,
69bdbbae24SBartosz Golaszewski struct gpio_device *gdev, poll_fn func)
70bdbbae24SBartosz Golaszewski {
71bdbbae24SBartosz Golaszewski __poll_t ret;
72bdbbae24SBartosz Golaszewski
73bdbbae24SBartosz Golaszewski down_read(&gdev->sem);
74bdbbae24SBartosz Golaszewski ret = func(file, wait);
75bdbbae24SBartosz Golaszewski up_read(&gdev->sem);
76bdbbae24SBartosz Golaszewski
77bdbbae24SBartosz Golaszewski return ret;
78bdbbae24SBartosz Golaszewski }
79bdbbae24SBartosz Golaszewski
call_ioctl_locked(struct file * file,unsigned int cmd,unsigned long arg,struct gpio_device * gdev,ioctl_fn func)80bdbbae24SBartosz Golaszewski static long call_ioctl_locked(struct file *file, unsigned int cmd,
81bdbbae24SBartosz Golaszewski unsigned long arg, struct gpio_device *gdev,
82bdbbae24SBartosz Golaszewski ioctl_fn func)
83bdbbae24SBartosz Golaszewski {
84bdbbae24SBartosz Golaszewski long ret;
85bdbbae24SBartosz Golaszewski
86bdbbae24SBartosz Golaszewski down_read(&gdev->sem);
87bdbbae24SBartosz Golaszewski ret = func(file, cmd, arg);
88bdbbae24SBartosz Golaszewski up_read(&gdev->sem);
89bdbbae24SBartosz Golaszewski
90bdbbae24SBartosz Golaszewski return ret;
91bdbbae24SBartosz Golaszewski }
92bdbbae24SBartosz Golaszewski
call_read_locked(struct file * file,char __user * buf,size_t count,loff_t * f_ps,struct gpio_device * gdev,read_fn func)93bdbbae24SBartosz Golaszewski static ssize_t call_read_locked(struct file *file, char __user *buf,
94bdbbae24SBartosz Golaszewski size_t count, loff_t *f_ps,
95bdbbae24SBartosz Golaszewski struct gpio_device *gdev, read_fn func)
96bdbbae24SBartosz Golaszewski {
97bdbbae24SBartosz Golaszewski ssize_t ret;
98bdbbae24SBartosz Golaszewski
99bdbbae24SBartosz Golaszewski down_read(&gdev->sem);
100bdbbae24SBartosz Golaszewski ret = func(file, buf, count, f_ps);
101bdbbae24SBartosz Golaszewski up_read(&gdev->sem);
102bdbbae24SBartosz Golaszewski
103bdbbae24SBartosz Golaszewski return ret;
104bdbbae24SBartosz Golaszewski }
105bdbbae24SBartosz Golaszewski
106925ca369SKent Gibson /*
107925ca369SKent Gibson * GPIO line handle management
108925ca369SKent Gibson */
109925ca369SKent Gibson
1103c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
111925ca369SKent Gibson /**
112925ca369SKent Gibson * struct linehandle_state - contains the state of a userspace handle
113925ca369SKent Gibson * @gdev: the GPIO device the handle pertains to
114925ca369SKent Gibson * @label: consumer label used to tag descriptors
115925ca369SKent Gibson * @descs: the GPIO descriptors held by this handle
11652b7b596SKent Gibson * @num_descs: the number of descriptors held in the descs array
117925ca369SKent Gibson */
118925ca369SKent Gibson struct linehandle_state {
119925ca369SKent Gibson struct gpio_device *gdev;
120925ca369SKent Gibson const char *label;
121925ca369SKent Gibson struct gpio_desc *descs[GPIOHANDLES_MAX];
12252b7b596SKent Gibson u32 num_descs;
123925ca369SKent Gibson };
124925ca369SKent Gibson
125925ca369SKent Gibson #define GPIOHANDLE_REQUEST_VALID_FLAGS \
126925ca369SKent Gibson (GPIOHANDLE_REQUEST_INPUT | \
127925ca369SKent Gibson GPIOHANDLE_REQUEST_OUTPUT | \
128925ca369SKent Gibson GPIOHANDLE_REQUEST_ACTIVE_LOW | \
129925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
130925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
131925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_DISABLE | \
132925ca369SKent Gibson GPIOHANDLE_REQUEST_OPEN_DRAIN | \
133925ca369SKent Gibson GPIOHANDLE_REQUEST_OPEN_SOURCE)
134925ca369SKent Gibson
13567ccb6b0SKent Gibson #define GPIOHANDLE_REQUEST_DIRECTION_FLAGS \
13667ccb6b0SKent Gibson (GPIOHANDLE_REQUEST_INPUT | \
13767ccb6b0SKent Gibson GPIOHANDLE_REQUEST_OUTPUT)
13867ccb6b0SKent Gibson
linehandle_validate_flags(u32 flags)139925ca369SKent Gibson static int linehandle_validate_flags(u32 flags)
140925ca369SKent Gibson {
141925ca369SKent Gibson /* Return an error if an unknown flag is set */
142925ca369SKent Gibson if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
143925ca369SKent Gibson return -EINVAL;
144925ca369SKent Gibson
145925ca369SKent Gibson /*
146925ca369SKent Gibson * Do not allow both INPUT & OUTPUT flags to be set as they are
147925ca369SKent Gibson * contradictory.
148925ca369SKent Gibson */
149925ca369SKent Gibson if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
150925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OUTPUT))
151925ca369SKent Gibson return -EINVAL;
152925ca369SKent Gibson
153925ca369SKent Gibson /*
154925ca369SKent Gibson * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
155925ca369SKent Gibson * the hardware actually supports enabling both at the same time the
156925ca369SKent Gibson * electrical result would be disastrous.
157925ca369SKent Gibson */
158925ca369SKent Gibson if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
159925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
160925ca369SKent Gibson return -EINVAL;
161925ca369SKent Gibson
162925ca369SKent Gibson /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
163925ca369SKent Gibson if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
164925ca369SKent Gibson ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
165925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
166925ca369SKent Gibson return -EINVAL;
167925ca369SKent Gibson
168925ca369SKent Gibson /* Bias flags only allowed for input or output mode. */
169925ca369SKent Gibson if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
170925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
171925ca369SKent Gibson ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
172925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
173925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
174925ca369SKent Gibson return -EINVAL;
175925ca369SKent Gibson
176925ca369SKent Gibson /* Only one bias flag can be set. */
177925ca369SKent Gibson if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
178925ca369SKent Gibson (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
179925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
180925ca369SKent Gibson ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
181925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
182925ca369SKent Gibson return -EINVAL;
183925ca369SKent Gibson
184925ca369SKent Gibson return 0;
185925ca369SKent Gibson }
186925ca369SKent Gibson
linehandle_flags_to_desc_flags(u32 lflags,unsigned long * flagsp)187c274b58aSKent Gibson static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
188c274b58aSKent Gibson {
189c274b58aSKent Gibson assign_bit(FLAG_ACTIVE_LOW, flagsp,
190c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
191c274b58aSKent Gibson assign_bit(FLAG_OPEN_DRAIN, flagsp,
192c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
193c274b58aSKent Gibson assign_bit(FLAG_OPEN_SOURCE, flagsp,
194c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
195c274b58aSKent Gibson assign_bit(FLAG_PULL_UP, flagsp,
196c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
197c274b58aSKent Gibson assign_bit(FLAG_PULL_DOWN, flagsp,
198c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
199c274b58aSKent Gibson assign_bit(FLAG_BIAS_DISABLE, flagsp,
200c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
201c274b58aSKent Gibson }
202c274b58aSKent Gibson
linehandle_set_config(struct linehandle_state * lh,void __user * ip)203925ca369SKent Gibson static long linehandle_set_config(struct linehandle_state *lh,
204925ca369SKent Gibson void __user *ip)
205925ca369SKent Gibson {
206925ca369SKent Gibson struct gpiohandle_config gcnf;
207925ca369SKent Gibson struct gpio_desc *desc;
208925ca369SKent Gibson int i, ret;
209925ca369SKent Gibson u32 lflags;
210925ca369SKent Gibson
211925ca369SKent Gibson if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
212925ca369SKent Gibson return -EFAULT;
213925ca369SKent Gibson
214925ca369SKent Gibson lflags = gcnf.flags;
215925ca369SKent Gibson ret = linehandle_validate_flags(lflags);
216925ca369SKent Gibson if (ret)
217925ca369SKent Gibson return ret;
218925ca369SKent Gibson
21967ccb6b0SKent Gibson /* Lines must be reconfigured explicitly as input or output. */
22067ccb6b0SKent Gibson if (!(lflags & GPIOHANDLE_REQUEST_DIRECTION_FLAGS))
22167ccb6b0SKent Gibson return -EINVAL;
22267ccb6b0SKent Gibson
22352b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) {
224925ca369SKent Gibson desc = lh->descs[i];
22567ccb6b0SKent Gibson linehandle_flags_to_desc_flags(lflags, &desc->flags);
226925ca369SKent Gibson
227925ca369SKent Gibson if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
228925ca369SKent Gibson int val = !!gcnf.default_values[i];
229925ca369SKent Gibson
230925ca369SKent Gibson ret = gpiod_direction_output(desc, val);
231925ca369SKent Gibson if (ret)
232925ca369SKent Gibson return ret;
23367ccb6b0SKent Gibson } else {
234925ca369SKent Gibson ret = gpiod_direction_input(desc);
235925ca369SKent Gibson if (ret)
236925ca369SKent Gibson return ret;
237925ca369SKent Gibson }
238925ca369SKent Gibson
2399ce4ed5bSBartosz Golaszewski gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
240925ca369SKent Gibson }
241925ca369SKent Gibson return 0;
242925ca369SKent Gibson }
243925ca369SKent Gibson
linehandle_ioctl_unlocked(struct file * file,unsigned int cmd,unsigned long arg)244bdbbae24SBartosz Golaszewski static long linehandle_ioctl_unlocked(struct file *file, unsigned int cmd,
245925ca369SKent Gibson unsigned long arg)
246925ca369SKent Gibson {
24749bc5279SKent Gibson struct linehandle_state *lh = file->private_data;
248925ca369SKent Gibson void __user *ip = (void __user *)arg;
249925ca369SKent Gibson struct gpiohandle_data ghd;
250925ca369SKent Gibson DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
2511cef8b50SAndy Shevchenko unsigned int i;
2521cef8b50SAndy Shevchenko int ret;
253925ca369SKent Gibson
254533aae7cSBartosz Golaszewski if (!lh->gdev->chip)
255533aae7cSBartosz Golaszewski return -ENODEV;
256533aae7cSBartosz Golaszewski
2571cef8b50SAndy Shevchenko switch (cmd) {
2581cef8b50SAndy Shevchenko case GPIOHANDLE_GET_LINE_VALUES_IOCTL:
2591cef8b50SAndy Shevchenko /* NOTE: It's okay to read values of output lines */
2601cef8b50SAndy Shevchenko ret = gpiod_get_array_value_complex(false, true,
2611cef8b50SAndy Shevchenko lh->num_descs, lh->descs,
2621cef8b50SAndy Shevchenko NULL, vals);
263925ca369SKent Gibson if (ret)
264925ca369SKent Gibson return ret;
265925ca369SKent Gibson
266925ca369SKent Gibson memset(&ghd, 0, sizeof(ghd));
26752b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++)
268925ca369SKent Gibson ghd.values[i] = test_bit(i, vals);
269925ca369SKent Gibson
270925ca369SKent Gibson if (copy_to_user(ip, &ghd, sizeof(ghd)))
271925ca369SKent Gibson return -EFAULT;
272925ca369SKent Gibson
273925ca369SKent Gibson return 0;
2741cef8b50SAndy Shevchenko case GPIOHANDLE_SET_LINE_VALUES_IOCTL:
275925ca369SKent Gibson /*
276925ca369SKent Gibson * All line descriptors were created at once with the same
277925ca369SKent Gibson * flags so just check if the first one is really output.
278925ca369SKent Gibson */
279925ca369SKent Gibson if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
280925ca369SKent Gibson return -EPERM;
281925ca369SKent Gibson
282925ca369SKent Gibson if (copy_from_user(&ghd, ip, sizeof(ghd)))
283925ca369SKent Gibson return -EFAULT;
284925ca369SKent Gibson
285925ca369SKent Gibson /* Clamp all values to [0,1] */
28652b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++)
287925ca369SKent Gibson __assign_bit(i, vals, ghd.values[i]);
288925ca369SKent Gibson
289925ca369SKent Gibson /* Reuse the array setting function */
290925ca369SKent Gibson return gpiod_set_array_value_complex(false,
291925ca369SKent Gibson true,
29252b7b596SKent Gibson lh->num_descs,
293925ca369SKent Gibson lh->descs,
294925ca369SKent Gibson NULL,
295925ca369SKent Gibson vals);
2961cef8b50SAndy Shevchenko case GPIOHANDLE_SET_CONFIG_IOCTL:
297925ca369SKent Gibson return linehandle_set_config(lh, ip);
2981cef8b50SAndy Shevchenko default:
299925ca369SKent Gibson return -EINVAL;
300925ca369SKent Gibson }
3011cef8b50SAndy Shevchenko }
302925ca369SKent Gibson
linehandle_ioctl(struct file * file,unsigned int cmd,unsigned long arg)303bdbbae24SBartosz Golaszewski static long linehandle_ioctl(struct file *file, unsigned int cmd,
304bdbbae24SBartosz Golaszewski unsigned long arg)
305bdbbae24SBartosz Golaszewski {
306bdbbae24SBartosz Golaszewski struct linehandle_state *lh = file->private_data;
307bdbbae24SBartosz Golaszewski
308bdbbae24SBartosz Golaszewski return call_ioctl_locked(file, cmd, arg, lh->gdev,
309bdbbae24SBartosz Golaszewski linehandle_ioctl_unlocked);
310bdbbae24SBartosz Golaszewski }
311bdbbae24SBartosz Golaszewski
312925ca369SKent Gibson #ifdef CONFIG_COMPAT
linehandle_ioctl_compat(struct file * file,unsigned int cmd,unsigned long arg)31349bc5279SKent Gibson static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
314925ca369SKent Gibson unsigned long arg)
315925ca369SKent Gibson {
31649bc5279SKent Gibson return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
317925ca369SKent Gibson }
318925ca369SKent Gibson #endif
319925ca369SKent Gibson
linehandle_free(struct linehandle_state * lh)320883f9198SKent Gibson static void linehandle_free(struct linehandle_state *lh)
321925ca369SKent Gibson {
322925ca369SKent Gibson int i;
323925ca369SKent Gibson
32452b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++)
325883f9198SKent Gibson if (lh->descs[i])
326925ca369SKent Gibson gpiod_free(lh->descs[i]);
327925ca369SKent Gibson kfree(lh->label);
328dc0989e3SAndy Shevchenko gpio_device_put(lh->gdev);
329925ca369SKent Gibson kfree(lh);
330883f9198SKent Gibson }
331883f9198SKent Gibson
linehandle_release(struct inode * inode,struct file * file)332883f9198SKent Gibson static int linehandle_release(struct inode *inode, struct file *file)
333883f9198SKent Gibson {
334883f9198SKent Gibson linehandle_free(file->private_data);
335925ca369SKent Gibson return 0;
336925ca369SKent Gibson }
337925ca369SKent Gibson
338925ca369SKent Gibson static const struct file_operations linehandle_fileops = {
339925ca369SKent Gibson .release = linehandle_release,
340925ca369SKent Gibson .owner = THIS_MODULE,
341925ca369SKent Gibson .llseek = noop_llseek,
342925ca369SKent Gibson .unlocked_ioctl = linehandle_ioctl,
343925ca369SKent Gibson #ifdef CONFIG_COMPAT
344925ca369SKent Gibson .compat_ioctl = linehandle_ioctl_compat,
345925ca369SKent Gibson #endif
346925ca369SKent Gibson };
347925ca369SKent Gibson
linehandle_create(struct gpio_device * gdev,void __user * ip)348925ca369SKent Gibson static int linehandle_create(struct gpio_device *gdev, void __user *ip)
349925ca369SKent Gibson {
350925ca369SKent Gibson struct gpiohandle_request handlereq;
351925ca369SKent Gibson struct linehandle_state *lh;
352925ca369SKent Gibson struct file *file;
353883f9198SKent Gibson int fd, i, ret;
354925ca369SKent Gibson u32 lflags;
355925ca369SKent Gibson
356925ca369SKent Gibson if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
357925ca369SKent Gibson return -EFAULT;
358925ca369SKent Gibson if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
359925ca369SKent Gibson return -EINVAL;
360925ca369SKent Gibson
361925ca369SKent Gibson lflags = handlereq.flags;
362925ca369SKent Gibson
363925ca369SKent Gibson ret = linehandle_validate_flags(lflags);
364925ca369SKent Gibson if (ret)
365925ca369SKent Gibson return ret;
366925ca369SKent Gibson
367925ca369SKent Gibson lh = kzalloc(sizeof(*lh), GFP_KERNEL);
368925ca369SKent Gibson if (!lh)
369925ca369SKent Gibson return -ENOMEM;
370dc0989e3SAndy Shevchenko lh->gdev = gpio_device_get(gdev);
371925ca369SKent Gibson
372f188ac12SKent Gibson if (handlereq.consumer_label[0] != '\0') {
373f188ac12SKent Gibson /* label is only initialized if consumer_label is set */
374f188ac12SKent Gibson lh->label = kstrndup(handlereq.consumer_label,
375f188ac12SKent Gibson sizeof(handlereq.consumer_label) - 1,
376925ca369SKent Gibson GFP_KERNEL);
377925ca369SKent Gibson if (!lh->label) {
378925ca369SKent Gibson ret = -ENOMEM;
379925ca369SKent Gibson goto out_free_lh;
380925ca369SKent Gibson }
381925ca369SKent Gibson }
382925ca369SKent Gibson
383883f9198SKent Gibson lh->num_descs = handlereq.lines;
384883f9198SKent Gibson
385925ca369SKent Gibson /* Request each GPIO */
386925ca369SKent Gibson for (i = 0; i < handlereq.lines; i++) {
387925ca369SKent Gibson u32 offset = handlereq.lineoffsets[i];
388925ca369SKent Gibson struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
389925ca369SKent Gibson
390925ca369SKent Gibson if (IS_ERR(desc)) {
391925ca369SKent Gibson ret = PTR_ERR(desc);
392883f9198SKent Gibson goto out_free_lh;
393925ca369SKent Gibson }
394925ca369SKent Gibson
39595a4eed7SAndy Shevchenko ret = gpiod_request_user(desc, lh->label);
396925ca369SKent Gibson if (ret)
397883f9198SKent Gibson goto out_free_lh;
398925ca369SKent Gibson lh->descs[i] = desc;
399c274b58aSKent Gibson linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
400925ca369SKent Gibson
401925ca369SKent Gibson ret = gpiod_set_transitory(desc, false);
402925ca369SKent Gibson if (ret < 0)
403883f9198SKent Gibson goto out_free_lh;
404925ca369SKent Gibson
405925ca369SKent Gibson /*
406925ca369SKent Gibson * Lines have to be requested explicitly for input
407925ca369SKent Gibson * or output, else the line will be treated "as is".
408925ca369SKent Gibson */
409925ca369SKent Gibson if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
410925ca369SKent Gibson int val = !!handlereq.default_values[i];
411925ca369SKent Gibson
412925ca369SKent Gibson ret = gpiod_direction_output(desc, val);
413925ca369SKent Gibson if (ret)
414883f9198SKent Gibson goto out_free_lh;
415925ca369SKent Gibson } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
416925ca369SKent Gibson ret = gpiod_direction_input(desc);
417925ca369SKent Gibson if (ret)
418883f9198SKent Gibson goto out_free_lh;
419925ca369SKent Gibson }
420925ca369SKent Gibson
4219ce4ed5bSBartosz Golaszewski gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
422925ca369SKent Gibson
423925ca369SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
424925ca369SKent Gibson offset);
425925ca369SKent Gibson }
426925ca369SKent Gibson
427925ca369SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
428925ca369SKent Gibson if (fd < 0) {
429925ca369SKent Gibson ret = fd;
430883f9198SKent Gibson goto out_free_lh;
431925ca369SKent Gibson }
432925ca369SKent Gibson
433925ca369SKent Gibson file = anon_inode_getfile("gpio-linehandle",
434925ca369SKent Gibson &linehandle_fileops,
435925ca369SKent Gibson lh,
436925ca369SKent Gibson O_RDONLY | O_CLOEXEC);
437925ca369SKent Gibson if (IS_ERR(file)) {
438925ca369SKent Gibson ret = PTR_ERR(file);
439925ca369SKent Gibson goto out_put_unused_fd;
440925ca369SKent Gibson }
441925ca369SKent Gibson
442925ca369SKent Gibson handlereq.fd = fd;
443925ca369SKent Gibson if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
444925ca369SKent Gibson /*
445925ca369SKent Gibson * fput() will trigger the release() callback, so do not go onto
446925ca369SKent Gibson * the regular error cleanup path here.
447925ca369SKent Gibson */
448925ca369SKent Gibson fput(file);
449925ca369SKent Gibson put_unused_fd(fd);
450925ca369SKent Gibson return -EFAULT;
451925ca369SKent Gibson }
452925ca369SKent Gibson
453925ca369SKent Gibson fd_install(fd, file);
454925ca369SKent Gibson
455925ca369SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
45652b7b596SKent Gibson lh->num_descs);
457925ca369SKent Gibson
458925ca369SKent Gibson return 0;
459925ca369SKent Gibson
460925ca369SKent Gibson out_put_unused_fd:
461925ca369SKent Gibson put_unused_fd(fd);
462925ca369SKent Gibson out_free_lh:
463883f9198SKent Gibson linehandle_free(lh);
464925ca369SKent Gibson return ret;
465925ca369SKent Gibson }
4663c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
4673c0d9c63SKent Gibson
4683c0d9c63SKent Gibson /**
4693c0d9c63SKent Gibson * struct line - contains the state of a requested line
470309364bcSKent Gibson * @node: to store the object in supinfo_tree if supplemental
4713c0d9c63SKent Gibson * @desc: the GPIO descriptor for this line.
47273e03419SKent Gibson * @req: the corresponding line request
47373e03419SKent Gibson * @irq: the interrupt triggered in response to events on this GPIO
4748d259847SAndy Shevchenko * @edflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or
47573e03419SKent Gibson * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied
47673e03419SKent Gibson * @timestamp_ns: cache for the timestamp storing it between hardirq and
47773e03419SKent Gibson * IRQ thread, used to bring the timestamp close to the actual event
47873e03419SKent Gibson * @req_seqno: the seqno for the current edge event in the sequence of
47973e03419SKent Gibson * events for the corresponding line request. This is drawn from the @req.
48073e03419SKent Gibson * @line_seqno: the seqno for the current edge event in the sequence of
48173e03419SKent Gibson * events for this line.
48265cff704SKent Gibson * @work: the worker that implements software debouncing
483309364bcSKent Gibson * @debounce_period_us: the debounce period in microseconds
48465cff704SKent Gibson * @sw_debounced: flag indicating if the software debouncer is active
48565cff704SKent Gibson * @level: the current debounced physical level of the line
48685ff37e3SAndy Shevchenko * @hdesc: the Hardware Timestamp Engine (HTE) descriptor
48785ff37e3SAndy Shevchenko * @raw_level: the line level at the time of event
48885ff37e3SAndy Shevchenko * @total_discard_seq: the running counter of the discarded events
48985ff37e3SAndy Shevchenko * @last_seqno: the last sequence number before debounce period expires
4903c0d9c63SKent Gibson */
4913c0d9c63SKent Gibson struct line {
492309364bcSKent Gibson struct rb_node node;
4933c0d9c63SKent Gibson struct gpio_desc *desc;
49473e03419SKent Gibson /*
49573e03419SKent Gibson * -- edge detector specific fields --
49673e03419SKent Gibson */
49773e03419SKent Gibson struct linereq *req;
49873e03419SKent Gibson unsigned int irq;
4993ffb7c45SKent Gibson /*
500b1a92e94SKent Gibson * The flags for the active edge detector configuration.
501b1a92e94SKent Gibson *
502b1a92e94SKent Gibson * edflags is set by linereq_create(), linereq_free(), and
503b1a92e94SKent Gibson * linereq_set_config_unlocked(), which are themselves mutually
504b1a92e94SKent Gibson * exclusive, and is accessed by edge_irq_thread(),
505b1a92e94SKent Gibson * process_hw_ts_thread() and debounce_work_func(),
506b1a92e94SKent Gibson * which can all live with a slightly stale value.
5073ffb7c45SKent Gibson */
508b1a92e94SKent Gibson u64 edflags;
50973e03419SKent Gibson /*
51073e03419SKent Gibson * timestamp_ns and req_seqno are accessed only by
51173e03419SKent Gibson * edge_irq_handler() and edge_irq_thread(), which are themselves
51273e03419SKent Gibson * mutually exclusive, so no additional protection is necessary.
51373e03419SKent Gibson */
51473e03419SKent Gibson u64 timestamp_ns;
51573e03419SKent Gibson u32 req_seqno;
51665cff704SKent Gibson /*
51765cff704SKent Gibson * line_seqno is accessed by either edge_irq_thread() or
51865cff704SKent Gibson * debounce_work_func(), which are themselves mutually exclusive,
51965cff704SKent Gibson * so no additional protection is necessary.
52065cff704SKent Gibson */
52173e03419SKent Gibson u32 line_seqno;
52265cff704SKent Gibson /*
52365cff704SKent Gibson * -- debouncer specific fields --
52465cff704SKent Gibson */
52565cff704SKent Gibson struct delayed_work work;
52665cff704SKent Gibson /*
527309364bcSKent Gibson * debounce_period_us is accessed by debounce_irq_handler() and
528309364bcSKent Gibson * process_hw_ts() which are disabled when modified by
529309364bcSKent Gibson * debounce_setup(), edge_detector_setup() or edge_detector_stop()
530309364bcSKent Gibson * or can live with a stale version when updated by
531309364bcSKent Gibson * edge_detector_update().
532309364bcSKent Gibson * The modifying functions are themselves mutually exclusive.
533309364bcSKent Gibson */
534309364bcSKent Gibson unsigned int debounce_period_us;
535309364bcSKent Gibson /*
53665cff704SKent Gibson * sw_debounce is accessed by linereq_set_config(), which is the
53765cff704SKent Gibson * only setter, and linereq_get_values(), which can live with a
53865cff704SKent Gibson * slightly stale value.
53965cff704SKent Gibson */
54065cff704SKent Gibson unsigned int sw_debounced;
54165cff704SKent Gibson /*
54265cff704SKent Gibson * level is accessed by debounce_work_func(), which is the only
54365cff704SKent Gibson * setter, and linereq_get_values() which can live with a slightly
54465cff704SKent Gibson * stale value.
54565cff704SKent Gibson */
54665cff704SKent Gibson unsigned int level;
547272ddba0SKent Gibson #ifdef CONFIG_HTE
5482068339aSDipen Patel struct hte_ts_desc hdesc;
5492068339aSDipen Patel /*
5502068339aSDipen Patel * HTE provider sets line level at the time of event. The valid
5512068339aSDipen Patel * value is 0 or 1 and negative value for an error.
5522068339aSDipen Patel */
5532068339aSDipen Patel int raw_level;
5542068339aSDipen Patel /*
5552068339aSDipen Patel * when sw_debounce is set on HTE enabled line, this is running
5562068339aSDipen Patel * counter of the discarded events.
5572068339aSDipen Patel */
5582068339aSDipen Patel u32 total_discard_seq;
5592068339aSDipen Patel /*
5602068339aSDipen Patel * when sw_debounce is set on HTE enabled line, this variable records
5612068339aSDipen Patel * last sequence number before debounce period expires.
5622068339aSDipen Patel */
5632068339aSDipen Patel u32 last_seqno;
564272ddba0SKent Gibson #endif /* CONFIG_HTE */
5653c0d9c63SKent Gibson };
5663c0d9c63SKent Gibson
567309364bcSKent Gibson /*
568309364bcSKent Gibson * a rbtree of the struct lines containing supplemental info.
569309364bcSKent Gibson * Used to populate gpio_v2_line_info with cdev specific fields not contained
570309364bcSKent Gibson * in the struct gpio_desc.
571309364bcSKent Gibson * A line is determined to contain supplemental information by
572309364bcSKent Gibson * line_has_supinfo().
573309364bcSKent Gibson */
574309364bcSKent Gibson static struct rb_root supinfo_tree = RB_ROOT;
575309364bcSKent Gibson /* covers supinfo_tree */
576309364bcSKent Gibson static DEFINE_SPINLOCK(supinfo_lock);
577309364bcSKent Gibson
5783c0d9c63SKent Gibson /**
5793c0d9c63SKent Gibson * struct linereq - contains the state of a userspace line request
5803c0d9c63SKent Gibson * @gdev: the GPIO device the line request pertains to
5813c0d9c63SKent Gibson * @label: consumer label used to tag GPIO descriptors
5823c0d9c63SKent Gibson * @num_lines: the number of lines in the lines array
58373e03419SKent Gibson * @wait: wait queue that handles blocking reads of events
584a0dda508SBartosz Golaszewski * @device_unregistered_nb: notifier block for receiving gdev unregister events
58573e03419SKent Gibson * @event_buffer_size: the number of elements allocated in @events
58673e03419SKent Gibson * @events: KFIFO for the GPIO events
58773e03419SKent Gibson * @seqno: the sequence number for edge events generated on all lines in
58873e03419SKent Gibson * this line request. Note that this is not used when @num_lines is 1, as
58973e03419SKent Gibson * the line_seqno is then the same and is cheaper to calculate.
590a54756cbSKent Gibson * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
591309364bcSKent Gibson * of configuration, particularly multi-step accesses to desc flags and
592309364bcSKent Gibson * changes to supinfo status.
5933c0d9c63SKent Gibson * @lines: the lines held by this line request, with @num_lines elements.
5943c0d9c63SKent Gibson */
5953c0d9c63SKent Gibson struct linereq {
5963c0d9c63SKent Gibson struct gpio_device *gdev;
5973c0d9c63SKent Gibson const char *label;
5983c0d9c63SKent Gibson u32 num_lines;
59973e03419SKent Gibson wait_queue_head_t wait;
600a0dda508SBartosz Golaszewski struct notifier_block device_unregistered_nb;
60173e03419SKent Gibson u32 event_buffer_size;
60273e03419SKent Gibson DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
60373e03419SKent Gibson atomic_t seqno;
604a54756cbSKent Gibson struct mutex config_mutex;
6053c0d9c63SKent Gibson struct line lines[];
6063c0d9c63SKent Gibson };
6073c0d9c63SKent Gibson
supinfo_insert(struct line * line)608309364bcSKent Gibson static void supinfo_insert(struct line *line)
609309364bcSKent Gibson {
610309364bcSKent Gibson struct rb_node **new = &(supinfo_tree.rb_node), *parent = NULL;
611309364bcSKent Gibson struct line *entry;
612309364bcSKent Gibson
613309364bcSKent Gibson guard(spinlock)(&supinfo_lock);
614309364bcSKent Gibson
615309364bcSKent Gibson while (*new) {
616309364bcSKent Gibson entry = container_of(*new, struct line, node);
617309364bcSKent Gibson
618309364bcSKent Gibson parent = *new;
619309364bcSKent Gibson if (line->desc < entry->desc) {
620309364bcSKent Gibson new = &((*new)->rb_left);
621309364bcSKent Gibson } else if (line->desc > entry->desc) {
622309364bcSKent Gibson new = &((*new)->rb_right);
623309364bcSKent Gibson } else {
624309364bcSKent Gibson /* this should never happen */
625309364bcSKent Gibson WARN(1, "duplicate line inserted");
626309364bcSKent Gibson return;
627309364bcSKent Gibson }
628309364bcSKent Gibson }
629309364bcSKent Gibson
630309364bcSKent Gibson rb_link_node(&line->node, parent, new);
631309364bcSKent Gibson rb_insert_color(&line->node, &supinfo_tree);
632309364bcSKent Gibson }
633309364bcSKent Gibson
supinfo_erase(struct line * line)634309364bcSKent Gibson static void supinfo_erase(struct line *line)
635309364bcSKent Gibson {
636309364bcSKent Gibson guard(spinlock)(&supinfo_lock);
637309364bcSKent Gibson
638309364bcSKent Gibson rb_erase(&line->node, &supinfo_tree);
639309364bcSKent Gibson }
640309364bcSKent Gibson
supinfo_find(struct gpio_desc * desc)641309364bcSKent Gibson static struct line *supinfo_find(struct gpio_desc *desc)
642309364bcSKent Gibson {
643309364bcSKent Gibson struct rb_node *node = supinfo_tree.rb_node;
644309364bcSKent Gibson struct line *line;
645309364bcSKent Gibson
646309364bcSKent Gibson while (node) {
647309364bcSKent Gibson line = container_of(node, struct line, node);
648309364bcSKent Gibson if (desc < line->desc)
649309364bcSKent Gibson node = node->rb_left;
650309364bcSKent Gibson else if (desc > line->desc)
651309364bcSKent Gibson node = node->rb_right;
652309364bcSKent Gibson else
653309364bcSKent Gibson return line;
654309364bcSKent Gibson }
655309364bcSKent Gibson return NULL;
656309364bcSKent Gibson }
657309364bcSKent Gibson
supinfo_to_lineinfo(struct gpio_desc * desc,struct gpio_v2_line_info * info)658309364bcSKent Gibson static void supinfo_to_lineinfo(struct gpio_desc *desc,
659309364bcSKent Gibson struct gpio_v2_line_info *info)
660309364bcSKent Gibson {
661309364bcSKent Gibson struct gpio_v2_line_attribute *attr;
662309364bcSKent Gibson struct line *line;
663309364bcSKent Gibson
664309364bcSKent Gibson guard(spinlock)(&supinfo_lock);
665309364bcSKent Gibson
666309364bcSKent Gibson line = supinfo_find(desc);
667309364bcSKent Gibson if (!line)
668309364bcSKent Gibson return;
669309364bcSKent Gibson
670309364bcSKent Gibson attr = &info->attrs[info->num_attrs];
671309364bcSKent Gibson attr->id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
672309364bcSKent Gibson attr->debounce_period_us = READ_ONCE(line->debounce_period_us);
673309364bcSKent Gibson info->num_attrs++;
674309364bcSKent Gibson }
675309364bcSKent Gibson
line_has_supinfo(struct line * line)676309364bcSKent Gibson static inline bool line_has_supinfo(struct line *line)
677309364bcSKent Gibson {
678309364bcSKent Gibson return READ_ONCE(line->debounce_period_us);
679309364bcSKent Gibson }
680309364bcSKent Gibson
681309364bcSKent Gibson /*
682309364bcSKent Gibson * Checks line_has_supinfo() before and after the change to avoid unnecessary
683309364bcSKent Gibson * supinfo_tree access.
684309364bcSKent Gibson * Called indirectly by linereq_create() or linereq_set_config() so line
685309364bcSKent Gibson * is already protected from concurrent changes.
686309364bcSKent Gibson */
line_set_debounce_period(struct line * line,unsigned int debounce_period_us)687309364bcSKent Gibson static void line_set_debounce_period(struct line *line,
688309364bcSKent Gibson unsigned int debounce_period_us)
689309364bcSKent Gibson {
690309364bcSKent Gibson bool was_suppl = line_has_supinfo(line);
691309364bcSKent Gibson
692309364bcSKent Gibson WRITE_ONCE(line->debounce_period_us, debounce_period_us);
693309364bcSKent Gibson
694309364bcSKent Gibson /* if supinfo status is unchanged then we're done */
695309364bcSKent Gibson if (line_has_supinfo(line) == was_suppl)
696309364bcSKent Gibson return;
697309364bcSKent Gibson
698309364bcSKent Gibson /* supinfo status has changed, so update the tree */
699309364bcSKent Gibson if (was_suppl)
700309364bcSKent Gibson supinfo_erase(line);
701309364bcSKent Gibson else
702309364bcSKent Gibson supinfo_insert(line);
703309364bcSKent Gibson }
704309364bcSKent Gibson
7053c0d9c63SKent Gibson #define GPIO_V2_LINE_BIAS_FLAGS \
7063c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
7073c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
7083c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_DISABLED)
7093c0d9c63SKent Gibson
7103c0d9c63SKent Gibson #define GPIO_V2_LINE_DIRECTION_FLAGS \
7113c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_INPUT | \
7123c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_OUTPUT)
7133c0d9c63SKent Gibson
7143c0d9c63SKent Gibson #define GPIO_V2_LINE_DRIVE_FLAGS \
7153c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
7163c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_OPEN_SOURCE)
7173c0d9c63SKent Gibson
71873e03419SKent Gibson #define GPIO_V2_LINE_EDGE_FLAGS \
71973e03419SKent Gibson (GPIO_V2_LINE_FLAG_EDGE_RISING | \
72073e03419SKent Gibson GPIO_V2_LINE_FLAG_EDGE_FALLING)
72173e03419SKent Gibson
7225e2ca893SKent Gibson #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS
7235e2ca893SKent Gibson
7243c0d9c63SKent Gibson #define GPIO_V2_LINE_VALID_FLAGS \
7253c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
7263c0d9c63SKent Gibson GPIO_V2_LINE_DIRECTION_FLAGS | \
7273c0d9c63SKent Gibson GPIO_V2_LINE_DRIVE_FLAGS | \
72873e03419SKent Gibson GPIO_V2_LINE_EDGE_FLAGS | \
72926d060e4SKent Gibson GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
7302068339aSDipen Patel GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
7313c0d9c63SKent Gibson GPIO_V2_LINE_BIAS_FLAGS)
7323c0d9c63SKent Gibson
733b1a92e94SKent Gibson /* subset of flags relevant for edge detector configuration */
734b1a92e94SKent Gibson #define GPIO_V2_LINE_EDGE_DETECTOR_FLAGS \
735b1a92e94SKent Gibson (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
736b1a92e94SKent Gibson GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
737b1a92e94SKent Gibson GPIO_V2_LINE_EDGE_FLAGS)
738b1a92e94SKent Gibson
linereq_unregistered_notify(struct notifier_block * nb,unsigned long action,void * data)739a0dda508SBartosz Golaszewski static int linereq_unregistered_notify(struct notifier_block *nb,
740a0dda508SBartosz Golaszewski unsigned long action, void *data)
741a0dda508SBartosz Golaszewski {
742a0dda508SBartosz Golaszewski struct linereq *lr = container_of(nb, struct linereq,
743a0dda508SBartosz Golaszewski device_unregistered_nb);
744a0dda508SBartosz Golaszewski
745a0dda508SBartosz Golaszewski wake_up_poll(&lr->wait, EPOLLIN | EPOLLERR);
746a0dda508SBartosz Golaszewski
747a0dda508SBartosz Golaszewski return NOTIFY_OK;
748a0dda508SBartosz Golaszewski }
749a0dda508SBartosz Golaszewski
linereq_put_event(struct linereq * lr,struct gpio_v2_line_event * le)75073e03419SKent Gibson static void linereq_put_event(struct linereq *lr,
75173e03419SKent Gibson struct gpio_v2_line_event *le)
75273e03419SKent Gibson {
75373e03419SKent Gibson bool overflow = false;
75473e03419SKent Gibson
75573e03419SKent Gibson spin_lock(&lr->wait.lock);
75673e03419SKent Gibson if (kfifo_is_full(&lr->events)) {
75773e03419SKent Gibson overflow = true;
75873e03419SKent Gibson kfifo_skip(&lr->events);
75973e03419SKent Gibson }
76073e03419SKent Gibson kfifo_in(&lr->events, le, 1);
76173e03419SKent Gibson spin_unlock(&lr->wait.lock);
76273e03419SKent Gibson if (!overflow)
76373e03419SKent Gibson wake_up_poll(&lr->wait, EPOLLIN);
76473e03419SKent Gibson else
76573e03419SKent Gibson pr_debug_ratelimited("event FIFO is full - event dropped\n");
76673e03419SKent Gibson }
76773e03419SKent Gibson
line_event_timestamp(struct line * line)76826d060e4SKent Gibson static u64 line_event_timestamp(struct line *line)
76926d060e4SKent Gibson {
77026d060e4SKent Gibson if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
77126d060e4SKent Gibson return ktime_get_real_ns();
772272ddba0SKent Gibson else if (IS_ENABLED(CONFIG_HTE) &&
773272ddba0SKent Gibson test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))
7742068339aSDipen Patel return line->timestamp_ns;
77526d060e4SKent Gibson
77626d060e4SKent Gibson return ktime_get_ns();
77726d060e4SKent Gibson }
77826d060e4SKent Gibson
line_event_id(int level)77924220232SKent Gibson static u32 line_event_id(int level)
78024220232SKent Gibson {
78124220232SKent Gibson return level ? GPIO_V2_LINE_EVENT_RISING_EDGE :
78224220232SKent Gibson GPIO_V2_LINE_EVENT_FALLING_EDGE;
78324220232SKent Gibson }
78424220232SKent Gibson
make_irq_label(const char * orig)7852f0262acSKent Gibson static inline char *make_irq_label(const char *orig)
7862f0262acSKent Gibson {
7872f0262acSKent Gibson char *new;
7882f0262acSKent Gibson
7892f0262acSKent Gibson if (!orig)
7902f0262acSKent Gibson return NULL;
7912f0262acSKent Gibson
7922f0262acSKent Gibson new = kstrdup_and_replace(orig, '/', ':', GFP_KERNEL);
7932f0262acSKent Gibson if (!new)
7942f0262acSKent Gibson return ERR_PTR(-ENOMEM);
7952f0262acSKent Gibson
7962f0262acSKent Gibson return new;
7972f0262acSKent Gibson }
7982f0262acSKent Gibson
free_irq_label(const char * label)7992f0262acSKent Gibson static inline void free_irq_label(const char *label)
8002f0262acSKent Gibson {
8012f0262acSKent Gibson kfree(label);
8022f0262acSKent Gibson }
8032f0262acSKent Gibson
804272ddba0SKent Gibson #ifdef CONFIG_HTE
805272ddba0SKent Gibson
process_hw_ts_thread(void * p)8062068339aSDipen Patel static enum hte_return process_hw_ts_thread(void *p)
8072068339aSDipen Patel {
8082068339aSDipen Patel struct line *line;
8092068339aSDipen Patel struct linereq *lr;
8102068339aSDipen Patel struct gpio_v2_line_event le;
811b1a92e94SKent Gibson u64 edflags;
8122068339aSDipen Patel int level;
8132068339aSDipen Patel
8142068339aSDipen Patel if (!p)
8152068339aSDipen Patel return HTE_CB_HANDLED;
8162068339aSDipen Patel
8172068339aSDipen Patel line = p;
8182068339aSDipen Patel lr = line->req;
8192068339aSDipen Patel
8202068339aSDipen Patel memset(&le, 0, sizeof(le));
8212068339aSDipen Patel
8222068339aSDipen Patel le.timestamp_ns = line->timestamp_ns;
823b1a92e94SKent Gibson edflags = READ_ONCE(line->edflags);
8242068339aSDipen Patel
825b1a92e94SKent Gibson switch (edflags & GPIO_V2_LINE_EDGE_FLAGS) {
826cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_BOTH:
82724220232SKent Gibson level = (line->raw_level >= 0) ?
82824220232SKent Gibson line->raw_level :
82924220232SKent Gibson gpiod_get_raw_value_cansleep(line->desc);
8302068339aSDipen Patel
831b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
83224220232SKent Gibson level = !level;
83324220232SKent Gibson
83424220232SKent Gibson le.id = line_event_id(level);
835cfa53463SKent Gibson break;
836cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_RISING:
8372068339aSDipen Patel le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
838cfa53463SKent Gibson break;
839cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_FALLING:
8402068339aSDipen Patel le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
841cfa53463SKent Gibson break;
842cfa53463SKent Gibson default:
8432068339aSDipen Patel return HTE_CB_HANDLED;
8442068339aSDipen Patel }
8452068339aSDipen Patel le.line_seqno = line->line_seqno;
8462068339aSDipen Patel le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
8472068339aSDipen Patel le.offset = gpio_chip_hwgpio(line->desc);
8482068339aSDipen Patel
8492068339aSDipen Patel linereq_put_event(lr, &le);
8502068339aSDipen Patel
8512068339aSDipen Patel return HTE_CB_HANDLED;
8522068339aSDipen Patel }
8532068339aSDipen Patel
process_hw_ts(struct hte_ts_data * ts,void * p)8542068339aSDipen Patel static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p)
8552068339aSDipen Patel {
8562068339aSDipen Patel struct line *line;
8572068339aSDipen Patel struct linereq *lr;
8582068339aSDipen Patel int diff_seqno = 0;
8592068339aSDipen Patel
8602068339aSDipen Patel if (!ts || !p)
8612068339aSDipen Patel return HTE_CB_HANDLED;
8622068339aSDipen Patel
8632068339aSDipen Patel line = p;
8642068339aSDipen Patel line->timestamp_ns = ts->tsc;
8652068339aSDipen Patel line->raw_level = ts->raw_level;
8662068339aSDipen Patel lr = line->req;
8672068339aSDipen Patel
8682068339aSDipen Patel if (READ_ONCE(line->sw_debounced)) {
8692068339aSDipen Patel line->total_discard_seq++;
8702068339aSDipen Patel line->last_seqno = ts->seq;
8712068339aSDipen Patel mod_delayed_work(system_wq, &line->work,
872309364bcSKent Gibson usecs_to_jiffies(READ_ONCE(line->debounce_period_us)));
8732068339aSDipen Patel } else {
8742068339aSDipen Patel if (unlikely(ts->seq < line->line_seqno))
8752068339aSDipen Patel return HTE_CB_HANDLED;
8762068339aSDipen Patel
8772068339aSDipen Patel diff_seqno = ts->seq - line->line_seqno;
8782068339aSDipen Patel line->line_seqno = ts->seq;
8792068339aSDipen Patel if (lr->num_lines != 1)
8802068339aSDipen Patel line->req_seqno = atomic_add_return(diff_seqno,
8812068339aSDipen Patel &lr->seqno);
8822068339aSDipen Patel
8832068339aSDipen Patel return HTE_RUN_SECOND_CB;
8842068339aSDipen Patel }
8852068339aSDipen Patel
8862068339aSDipen Patel return HTE_CB_HANDLED;
8872068339aSDipen Patel }
8882068339aSDipen Patel
hte_edge_setup(struct line * line,u64 eflags)889272ddba0SKent Gibson static int hte_edge_setup(struct line *line, u64 eflags)
890272ddba0SKent Gibson {
891272ddba0SKent Gibson int ret;
892272ddba0SKent Gibson unsigned long flags = 0;
893272ddba0SKent Gibson struct hte_ts_desc *hdesc = &line->hdesc;
894272ddba0SKent Gibson
895272ddba0SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
896272ddba0SKent Gibson flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
897272ddba0SKent Gibson HTE_FALLING_EDGE_TS :
898272ddba0SKent Gibson HTE_RISING_EDGE_TS;
899272ddba0SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
900272ddba0SKent Gibson flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
901272ddba0SKent Gibson HTE_RISING_EDGE_TS :
902272ddba0SKent Gibson HTE_FALLING_EDGE_TS;
903272ddba0SKent Gibson
904272ddba0SKent Gibson line->total_discard_seq = 0;
905272ddba0SKent Gibson
906272ddba0SKent Gibson hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags, NULL,
907272ddba0SKent Gibson line->desc);
908272ddba0SKent Gibson
909272ddba0SKent Gibson ret = hte_ts_get(NULL, hdesc, 0);
910272ddba0SKent Gibson if (ret)
911272ddba0SKent Gibson return ret;
912272ddba0SKent Gibson
913272ddba0SKent Gibson return hte_request_ts_ns(hdesc, process_hw_ts, process_hw_ts_thread,
914272ddba0SKent Gibson line);
915272ddba0SKent Gibson }
916272ddba0SKent Gibson
917272ddba0SKent Gibson #else
918272ddba0SKent Gibson
hte_edge_setup(struct line * line,u64 eflags)919272ddba0SKent Gibson static int hte_edge_setup(struct line *line, u64 eflags)
920272ddba0SKent Gibson {
921272ddba0SKent Gibson return 0;
922272ddba0SKent Gibson }
923272ddba0SKent Gibson #endif /* CONFIG_HTE */
924272ddba0SKent Gibson
edge_irq_thread(int irq,void * p)92573e03419SKent Gibson static irqreturn_t edge_irq_thread(int irq, void *p)
92673e03419SKent Gibson {
92773e03419SKent Gibson struct line *line = p;
92873e03419SKent Gibson struct linereq *lr = line->req;
92973e03419SKent Gibson struct gpio_v2_line_event le;
93073e03419SKent Gibson
93173e03419SKent Gibson /* Do not leak kernel stack to userspace */
93273e03419SKent Gibson memset(&le, 0, sizeof(le));
93373e03419SKent Gibson
93473e03419SKent Gibson if (line->timestamp_ns) {
93573e03419SKent Gibson le.timestamp_ns = line->timestamp_ns;
93673e03419SKent Gibson } else {
93773e03419SKent Gibson /*
93873e03419SKent Gibson * We may be running from a nested threaded interrupt in
93973e03419SKent Gibson * which case we didn't get the timestamp from
94073e03419SKent Gibson * edge_irq_handler().
94173e03419SKent Gibson */
94226d060e4SKent Gibson le.timestamp_ns = line_event_timestamp(line);
94373e03419SKent Gibson if (lr->num_lines != 1)
94473e03419SKent Gibson line->req_seqno = atomic_inc_return(&lr->seqno);
94573e03419SKent Gibson }
94673e03419SKent Gibson line->timestamp_ns = 0;
94773e03419SKent Gibson
948b1a92e94SKent Gibson switch (READ_ONCE(line->edflags) & GPIO_V2_LINE_EDGE_FLAGS) {
949cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_BOTH:
95024220232SKent Gibson le.id = line_event_id(gpiod_get_value_cansleep(line->desc));
951cfa53463SKent Gibson break;
952cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_RISING:
95373e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
954cfa53463SKent Gibson break;
955cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_FALLING:
95673e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
957cfa53463SKent Gibson break;
958cfa53463SKent Gibson default:
95973e03419SKent Gibson return IRQ_NONE;
96073e03419SKent Gibson }
96173e03419SKent Gibson line->line_seqno++;
96273e03419SKent Gibson le.line_seqno = line->line_seqno;
96373e03419SKent Gibson le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
96473e03419SKent Gibson le.offset = gpio_chip_hwgpio(line->desc);
96573e03419SKent Gibson
96673e03419SKent Gibson linereq_put_event(lr, &le);
96773e03419SKent Gibson
96873e03419SKent Gibson return IRQ_HANDLED;
96973e03419SKent Gibson }
97073e03419SKent Gibson
edge_irq_handler(int irq,void * p)97173e03419SKent Gibson static irqreturn_t edge_irq_handler(int irq, void *p)
97273e03419SKent Gibson {
97373e03419SKent Gibson struct line *line = p;
97473e03419SKent Gibson struct linereq *lr = line->req;
97573e03419SKent Gibson
97673e03419SKent Gibson /*
97773e03419SKent Gibson * Just store the timestamp in hardirq context so we get it as
97873e03419SKent Gibson * close in time as possible to the actual event.
97973e03419SKent Gibson */
98026d060e4SKent Gibson line->timestamp_ns = line_event_timestamp(line);
98173e03419SKent Gibson
98273e03419SKent Gibson if (lr->num_lines != 1)
98373e03419SKent Gibson line->req_seqno = atomic_inc_return(&lr->seqno);
98473e03419SKent Gibson
98573e03419SKent Gibson return IRQ_WAKE_THREAD;
98673e03419SKent Gibson }
98773e03419SKent Gibson
98865cff704SKent Gibson /*
98965cff704SKent Gibson * returns the current debounced logical value.
99065cff704SKent Gibson */
debounced_value(struct line * line)99165cff704SKent Gibson static bool debounced_value(struct line *line)
99265cff704SKent Gibson {
99365cff704SKent Gibson bool value;
99465cff704SKent Gibson
99565cff704SKent Gibson /*
99665cff704SKent Gibson * minor race - debouncer may be stopped here, so edge_detector_stop()
99765cff704SKent Gibson * must leave the value unchanged so the following will read the level
99865cff704SKent Gibson * from when the debouncer was last running.
99965cff704SKent Gibson */
100065cff704SKent Gibson value = READ_ONCE(line->level);
100165cff704SKent Gibson
100265cff704SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
100365cff704SKent Gibson value = !value;
100465cff704SKent Gibson
100565cff704SKent Gibson return value;
100665cff704SKent Gibson }
100765cff704SKent Gibson
debounce_irq_handler(int irq,void * p)100865cff704SKent Gibson static irqreturn_t debounce_irq_handler(int irq, void *p)
100965cff704SKent Gibson {
101065cff704SKent Gibson struct line *line = p;
101165cff704SKent Gibson
101265cff704SKent Gibson mod_delayed_work(system_wq, &line->work,
1013309364bcSKent Gibson usecs_to_jiffies(READ_ONCE(line->debounce_period_us)));
101465cff704SKent Gibson
101565cff704SKent Gibson return IRQ_HANDLED;
101665cff704SKent Gibson }
101765cff704SKent Gibson
debounce_work_func(struct work_struct * work)101865cff704SKent Gibson static void debounce_work_func(struct work_struct *work)
101965cff704SKent Gibson {
102065cff704SKent Gibson struct gpio_v2_line_event le;
102165cff704SKent Gibson struct line *line = container_of(work, struct line, work.work);
102265cff704SKent Gibson struct linereq *lr;
1023b1a92e94SKent Gibson u64 eflags, edflags = READ_ONCE(line->edflags);
1024272ddba0SKent Gibson int level = -1;
1025272ddba0SKent Gibson #ifdef CONFIG_HTE
1026272ddba0SKent Gibson int diff_seqno;
102765cff704SKent Gibson
1028b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
10292068339aSDipen Patel level = line->raw_level;
1030272ddba0SKent Gibson #endif
10312068339aSDipen Patel if (level < 0)
103265cff704SKent Gibson level = gpiod_get_raw_value_cansleep(line->desc);
103365cff704SKent Gibson if (level < 0) {
103465cff704SKent Gibson pr_debug_ratelimited("debouncer failed to read line value\n");
103565cff704SKent Gibson return;
103665cff704SKent Gibson }
103765cff704SKent Gibson
103865cff704SKent Gibson if (READ_ONCE(line->level) == level)
103965cff704SKent Gibson return;
104065cff704SKent Gibson
104165cff704SKent Gibson WRITE_ONCE(line->level, level);
104265cff704SKent Gibson
104365cff704SKent Gibson /* -- edge detection -- */
1044b1a92e94SKent Gibson eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
10453ffb7c45SKent Gibson if (!eflags)
104665cff704SKent Gibson return;
104765cff704SKent Gibson
104865cff704SKent Gibson /* switch from physical level to logical - if they differ */
1049b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
105065cff704SKent Gibson level = !level;
105165cff704SKent Gibson
105265cff704SKent Gibson /* ignore edges that are not being monitored */
10533ffb7c45SKent Gibson if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
10543ffb7c45SKent Gibson ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
105565cff704SKent Gibson return;
105665cff704SKent Gibson
105765cff704SKent Gibson /* Do not leak kernel stack to userspace */
105865cff704SKent Gibson memset(&le, 0, sizeof(le));
105965cff704SKent Gibson
106065cff704SKent Gibson lr = line->req;
106126d060e4SKent Gibson le.timestamp_ns = line_event_timestamp(line);
106265cff704SKent Gibson le.offset = gpio_chip_hwgpio(line->desc);
1063272ddba0SKent Gibson #ifdef CONFIG_HTE
1064b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) {
10652068339aSDipen Patel /* discard events except the last one */
10662068339aSDipen Patel line->total_discard_seq -= 1;
10672068339aSDipen Patel diff_seqno = line->last_seqno - line->total_discard_seq -
10682068339aSDipen Patel line->line_seqno;
10692068339aSDipen Patel line->line_seqno = line->last_seqno - line->total_discard_seq;
10702068339aSDipen Patel le.line_seqno = line->line_seqno;
10712068339aSDipen Patel le.seqno = (lr->num_lines == 1) ?
10722068339aSDipen Patel le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno);
1073272ddba0SKent Gibson } else
1074272ddba0SKent Gibson #endif /* CONFIG_HTE */
1075272ddba0SKent Gibson {
107665cff704SKent Gibson line->line_seqno++;
107765cff704SKent Gibson le.line_seqno = line->line_seqno;
107865cff704SKent Gibson le.seqno = (lr->num_lines == 1) ?
107965cff704SKent Gibson le.line_seqno : atomic_inc_return(&lr->seqno);
10802068339aSDipen Patel }
108165cff704SKent Gibson
108224220232SKent Gibson le.id = line_event_id(level);
108365cff704SKent Gibson
108465cff704SKent Gibson linereq_put_event(lr, &le);
108565cff704SKent Gibson }
108665cff704SKent Gibson
debounce_setup(struct line * line,unsigned int debounce_period_us)1087b1a92e94SKent Gibson static int debounce_setup(struct line *line, unsigned int debounce_period_us)
108865cff704SKent Gibson {
108965cff704SKent Gibson unsigned long irqflags;
109065cff704SKent Gibson int ret, level, irq;
10912f0262acSKent Gibson char *label;
109265cff704SKent Gibson
109365cff704SKent Gibson /* try hardware */
109465cff704SKent Gibson ret = gpiod_set_debounce(line->desc, debounce_period_us);
109565cff704SKent Gibson if (!ret) {
1096309364bcSKent Gibson line_set_debounce_period(line, debounce_period_us);
109765cff704SKent Gibson return ret;
109865cff704SKent Gibson }
109965cff704SKent Gibson if (ret != -ENOTSUPP)
110065cff704SKent Gibson return ret;
110165cff704SKent Gibson
110265cff704SKent Gibson if (debounce_period_us) {
110365cff704SKent Gibson /* setup software debounce */
110465cff704SKent Gibson level = gpiod_get_raw_value_cansleep(line->desc);
110565cff704SKent Gibson if (level < 0)
110665cff704SKent Gibson return level;
110765cff704SKent Gibson
1108272ddba0SKent Gibson if (!(IS_ENABLED(CONFIG_HTE) &&
1109272ddba0SKent Gibson test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))) {
111065cff704SKent Gibson irq = gpiod_to_irq(line->desc);
111165cff704SKent Gibson if (irq < 0)
111265cff704SKent Gibson return -ENXIO;
111365cff704SKent Gibson
11142f0262acSKent Gibson label = make_irq_label(line->req->label);
11152f0262acSKent Gibson if (IS_ERR(label))
11162f0262acSKent Gibson return -ENOMEM;
11172f0262acSKent Gibson
111865cff704SKent Gibson irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
111965cff704SKent Gibson ret = request_irq(irq, debounce_irq_handler, irqflags,
11202f0262acSKent Gibson label, line);
11212f0262acSKent Gibson if (ret) {
11222f0262acSKent Gibson free_irq_label(label);
112365cff704SKent Gibson return ret;
11242f0262acSKent Gibson }
112565cff704SKent Gibson line->irq = irq;
11262068339aSDipen Patel } else {
11272487a812SKent Gibson ret = hte_edge_setup(line, GPIO_V2_LINE_FLAG_EDGE_BOTH);
11282068339aSDipen Patel if (ret)
11292068339aSDipen Patel return ret;
11302068339aSDipen Patel }
11312068339aSDipen Patel
11322068339aSDipen Patel WRITE_ONCE(line->level, level);
11332068339aSDipen Patel WRITE_ONCE(line->sw_debounced, 1);
113465cff704SKent Gibson }
113565cff704SKent Gibson return 0;
113665cff704SKent Gibson }
113765cff704SKent Gibson
gpio_v2_line_config_debounced(struct gpio_v2_line_config * lc,unsigned int line_idx)113865cff704SKent Gibson static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
113965cff704SKent Gibson unsigned int line_idx)
114065cff704SKent Gibson {
114165cff704SKent Gibson unsigned int i;
114265cff704SKent Gibson u64 mask = BIT_ULL(line_idx);
114365cff704SKent Gibson
114465cff704SKent Gibson for (i = 0; i < lc->num_attrs; i++) {
114565cff704SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
114665cff704SKent Gibson (lc->attrs[i].mask & mask))
114765cff704SKent Gibson return true;
114865cff704SKent Gibson }
114965cff704SKent Gibson return false;
115065cff704SKent Gibson }
115165cff704SKent Gibson
gpio_v2_line_config_debounce_period(struct gpio_v2_line_config * lc,unsigned int line_idx)115265cff704SKent Gibson static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
115365cff704SKent Gibson unsigned int line_idx)
115465cff704SKent Gibson {
115565cff704SKent Gibson unsigned int i;
115665cff704SKent Gibson u64 mask = BIT_ULL(line_idx);
115765cff704SKent Gibson
115865cff704SKent Gibson for (i = 0; i < lc->num_attrs; i++) {
115965cff704SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
116065cff704SKent Gibson (lc->attrs[i].mask & mask))
116165cff704SKent Gibson return lc->attrs[i].attr.debounce_period_us;
116265cff704SKent Gibson }
116365cff704SKent Gibson return 0;
116465cff704SKent Gibson }
116565cff704SKent Gibson
edge_detector_stop(struct line * line)1166b1a92e94SKent Gibson static void edge_detector_stop(struct line *line)
116773e03419SKent Gibson {
1168b1a92e94SKent Gibson if (line->irq) {
116921bc9b15SBartosz Golaszewski free_irq_label(free_irq(line->irq, line));
117073e03419SKent Gibson line->irq = 0;
117173e03419SKent Gibson }
1172a54756cbSKent Gibson
1173272ddba0SKent Gibson #ifdef CONFIG_HTE
1174b1a92e94SKent Gibson if (READ_ONCE(line->edflags) & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
11752068339aSDipen Patel hte_ts_put(&line->hdesc);
1176272ddba0SKent Gibson #endif
11772068339aSDipen Patel
117865cff704SKent Gibson cancel_delayed_work_sync(&line->work);
117965cff704SKent Gibson WRITE_ONCE(line->sw_debounced, 0);
1180b1a92e94SKent Gibson WRITE_ONCE(line->edflags, 0);
1181309364bcSKent Gibson line_set_debounce_period(line, 0);
118265cff704SKent Gibson /* do not change line->level - see comment in debounced_value() */
118373e03419SKent Gibson }
118473e03419SKent Gibson
edge_detector_setup(struct line * line,struct gpio_v2_line_config * lc,unsigned int line_idx,u64 edflags)118573e03419SKent Gibson static int edge_detector_setup(struct line *line,
118665cff704SKent Gibson struct gpio_v2_line_config *lc,
1187b1a92e94SKent Gibson unsigned int line_idx, u64 edflags)
118873e03419SKent Gibson {
118965cff704SKent Gibson u32 debounce_period_us;
119073e03419SKent Gibson unsigned long irqflags = 0;
1191b1a92e94SKent Gibson u64 eflags;
119273e03419SKent Gibson int irq, ret;
119321bc9b15SBartosz Golaszewski char *label;
119473e03419SKent Gibson
1195b1a92e94SKent Gibson eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
119673e03419SKent Gibson if (eflags && !kfifo_initialized(&line->req->events)) {
119773e03419SKent Gibson ret = kfifo_alloc(&line->req->events,
119873e03419SKent Gibson line->req->event_buffer_size, GFP_KERNEL);
119973e03419SKent Gibson if (ret)
120073e03419SKent Gibson return ret;
120173e03419SKent Gibson }
120265cff704SKent Gibson if (gpio_v2_line_config_debounced(lc, line_idx)) {
120365cff704SKent Gibson debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
1204b1a92e94SKent Gibson ret = debounce_setup(line, debounce_period_us);
120565cff704SKent Gibson if (ret)
120665cff704SKent Gibson return ret;
1207309364bcSKent Gibson line_set_debounce_period(line, debounce_period_us);
120865cff704SKent Gibson }
120973e03419SKent Gibson
121065cff704SKent Gibson /* detection disabled or sw debouncer will provide edge detection */
121165cff704SKent Gibson if (!eflags || READ_ONCE(line->sw_debounced))
121273e03419SKent Gibson return 0;
121373e03419SKent Gibson
1214272ddba0SKent Gibson if (IS_ENABLED(CONFIG_HTE) &&
1215272ddba0SKent Gibson (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1216b1a92e94SKent Gibson return hte_edge_setup(line, edflags);
12172068339aSDipen Patel
121873e03419SKent Gibson irq = gpiod_to_irq(line->desc);
121973e03419SKent Gibson if (irq < 0)
122073e03419SKent Gibson return -ENXIO;
122173e03419SKent Gibson
122273e03419SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
122373e03419SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
122473e03419SKent Gibson IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
122573e03419SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
122673e03419SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
122773e03419SKent Gibson IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
122873e03419SKent Gibson irqflags |= IRQF_ONESHOT;
122973e03419SKent Gibson
123021bc9b15SBartosz Golaszewski label = make_irq_label(line->req->label);
1231d9f0804aSBartosz Golaszewski if (IS_ERR(label))
1232d9f0804aSBartosz Golaszewski return PTR_ERR(label);
123321bc9b15SBartosz Golaszewski
123473e03419SKent Gibson /* Request a thread to read the events */
123573e03419SKent Gibson ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
123621bc9b15SBartosz Golaszewski irqflags, label, line);
123721bc9b15SBartosz Golaszewski if (ret) {
123821bc9b15SBartosz Golaszewski free_irq_label(label);
123973e03419SKent Gibson return ret;
124021bc9b15SBartosz Golaszewski }
124173e03419SKent Gibson
124273e03419SKent Gibson line->irq = irq;
124373e03419SKent Gibson return 0;
124473e03419SKent Gibson }
124573e03419SKent Gibson
edge_detector_update(struct line * line,struct gpio_v2_line_config * lc,unsigned int line_idx,u64 edflags)124665cff704SKent Gibson static int edge_detector_update(struct line *line,
124765cff704SKent Gibson struct gpio_v2_line_config *lc,
1248b1a92e94SKent Gibson unsigned int line_idx, u64 edflags)
1249a54756cbSKent Gibson {
1250883e4bbfSKent Gibson u64 eflags;
1251883e4bbfSKent Gibson int ret;
1252b1a92e94SKent Gibson u64 active_edflags = READ_ONCE(line->edflags);
125365cff704SKent Gibson unsigned int debounce_period_us =
125465cff704SKent Gibson gpio_v2_line_config_debounce_period(lc, line_idx);
125565cff704SKent Gibson
1256b1a92e94SKent Gibson if ((active_edflags == edflags) &&
1257309364bcSKent Gibson (READ_ONCE(line->debounce_period_us) == debounce_period_us))
1258a54756cbSKent Gibson return 0;
1259a54756cbSKent Gibson
126065cff704SKent Gibson /* sw debounced and still will be...*/
126165cff704SKent Gibson if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
1262309364bcSKent Gibson line_set_debounce_period(line, debounce_period_us);
1263883e4bbfSKent Gibson /*
1264883e4bbfSKent Gibson * ensure event fifo is initialised if edge detection
1265883e4bbfSKent Gibson * is now enabled.
1266883e4bbfSKent Gibson */
1267883e4bbfSKent Gibson eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
1268883e4bbfSKent Gibson if (eflags && !kfifo_initialized(&line->req->events)) {
1269883e4bbfSKent Gibson ret = kfifo_alloc(&line->req->events,
1270883e4bbfSKent Gibson line->req->event_buffer_size,
1271883e4bbfSKent Gibson GFP_KERNEL);
1272883e4bbfSKent Gibson if (ret)
1273883e4bbfSKent Gibson return ret;
1274883e4bbfSKent Gibson }
127565cff704SKent Gibson return 0;
127665cff704SKent Gibson }
127765cff704SKent Gibson
127865cff704SKent Gibson /* reconfiguring edge detection or sw debounce being disabled */
1279b1a92e94SKent Gibson if ((line->irq && !READ_ONCE(line->sw_debounced)) ||
1280b1a92e94SKent Gibson (active_edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) ||
128165cff704SKent Gibson (!debounce_period_us && READ_ONCE(line->sw_debounced)))
1282b1a92e94SKent Gibson edge_detector_stop(line);
1283a54756cbSKent Gibson
1284b1a92e94SKent Gibson return edge_detector_setup(line, lc, line_idx, edflags);
1285a54756cbSKent Gibson }
1286a54756cbSKent Gibson
gpio_v2_line_config_flags(struct gpio_v2_line_config * lc,unsigned int line_idx)12873c0d9c63SKent Gibson static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
12883c0d9c63SKent Gibson unsigned int line_idx)
12893c0d9c63SKent Gibson {
12903c0d9c63SKent Gibson unsigned int i;
12913c0d9c63SKent Gibson u64 mask = BIT_ULL(line_idx);
12923c0d9c63SKent Gibson
12933c0d9c63SKent Gibson for (i = 0; i < lc->num_attrs; i++) {
12943c0d9c63SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
12953c0d9c63SKent Gibson (lc->attrs[i].mask & mask))
12963c0d9c63SKent Gibson return lc->attrs[i].attr.flags;
12973c0d9c63SKent Gibson }
12983c0d9c63SKent Gibson return lc->flags;
12993c0d9c63SKent Gibson }
13003c0d9c63SKent Gibson
gpio_v2_line_config_output_value(struct gpio_v2_line_config * lc,unsigned int line_idx)13013c0d9c63SKent Gibson static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
13023c0d9c63SKent Gibson unsigned int line_idx)
13033c0d9c63SKent Gibson {
13043c0d9c63SKent Gibson unsigned int i;
13053c0d9c63SKent Gibson u64 mask = BIT_ULL(line_idx);
13063c0d9c63SKent Gibson
13073c0d9c63SKent Gibson for (i = 0; i < lc->num_attrs; i++) {
13083c0d9c63SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
13093c0d9c63SKent Gibson (lc->attrs[i].mask & mask))
13103c0d9c63SKent Gibson return !!(lc->attrs[i].attr.values & mask);
13113c0d9c63SKent Gibson }
13123c0d9c63SKent Gibson return 0;
13133c0d9c63SKent Gibson }
13143c0d9c63SKent Gibson
gpio_v2_line_flags_validate(u64 flags)13153c0d9c63SKent Gibson static int gpio_v2_line_flags_validate(u64 flags)
13163c0d9c63SKent Gibson {
13173c0d9c63SKent Gibson /* Return an error if an unknown flag is set */
13183c0d9c63SKent Gibson if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
13193c0d9c63SKent Gibson return -EINVAL;
1320272ddba0SKent Gibson
1321272ddba0SKent Gibson if (!IS_ENABLED(CONFIG_HTE) &&
1322272ddba0SKent Gibson (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
1323272ddba0SKent Gibson return -EOPNOTSUPP;
1324272ddba0SKent Gibson
13253c0d9c63SKent Gibson /*
13263c0d9c63SKent Gibson * Do not allow both INPUT and OUTPUT flags to be set as they are
13273c0d9c63SKent Gibson * contradictory.
13283c0d9c63SKent Gibson */
13293c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
13303c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_OUTPUT))
13313c0d9c63SKent Gibson return -EINVAL;
13323c0d9c63SKent Gibson
13332068339aSDipen Patel /* Only allow one event clock source */
1334272ddba0SKent Gibson if (IS_ENABLED(CONFIG_HTE) &&
1335272ddba0SKent Gibson (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) &&
13362068339aSDipen Patel (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
13372068339aSDipen Patel return -EINVAL;
13382068339aSDipen Patel
133973e03419SKent Gibson /* Edge detection requires explicit input. */
134073e03419SKent Gibson if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
134173e03419SKent Gibson !(flags & GPIO_V2_LINE_FLAG_INPUT))
134273e03419SKent Gibson return -EINVAL;
134373e03419SKent Gibson
13443c0d9c63SKent Gibson /*
13453c0d9c63SKent Gibson * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
13463c0d9c63SKent Gibson * request. If the hardware actually supports enabling both at the
13473c0d9c63SKent Gibson * same time the electrical result would be disastrous.
13483c0d9c63SKent Gibson */
13493c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
13503c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
13513c0d9c63SKent Gibson return -EINVAL;
13523c0d9c63SKent Gibson
13533c0d9c63SKent Gibson /* Drive requires explicit output direction. */
13543c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
13553c0d9c63SKent Gibson !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
13563c0d9c63SKent Gibson return -EINVAL;
13573c0d9c63SKent Gibson
13583c0d9c63SKent Gibson /* Bias requires explicit direction. */
13593c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
13603c0d9c63SKent Gibson !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
13613c0d9c63SKent Gibson return -EINVAL;
13623c0d9c63SKent Gibson
13633c0d9c63SKent Gibson /* Only one bias flag can be set. */
13643c0d9c63SKent Gibson if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
13653c0d9c63SKent Gibson (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
13663c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
13673c0d9c63SKent Gibson ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
13683c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
13693c0d9c63SKent Gibson return -EINVAL;
13703c0d9c63SKent Gibson
13713c0d9c63SKent Gibson return 0;
13723c0d9c63SKent Gibson }
13733c0d9c63SKent Gibson
gpio_v2_line_config_validate(struct gpio_v2_line_config * lc,unsigned int num_lines)13743c0d9c63SKent Gibson static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
13753c0d9c63SKent Gibson unsigned int num_lines)
13763c0d9c63SKent Gibson {
13773c0d9c63SKent Gibson unsigned int i;
13783c0d9c63SKent Gibson u64 flags;
13793c0d9c63SKent Gibson int ret;
13803c0d9c63SKent Gibson
13813c0d9c63SKent Gibson if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
13823c0d9c63SKent Gibson return -EINVAL;
13833c0d9c63SKent Gibson
13843c0d9c63SKent Gibson if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
13853c0d9c63SKent Gibson return -EINVAL;
13863c0d9c63SKent Gibson
13873c0d9c63SKent Gibson for (i = 0; i < num_lines; i++) {
13883c0d9c63SKent Gibson flags = gpio_v2_line_config_flags(lc, i);
13893c0d9c63SKent Gibson ret = gpio_v2_line_flags_validate(flags);
13903c0d9c63SKent Gibson if (ret)
13913c0d9c63SKent Gibson return ret;
139265cff704SKent Gibson
139365cff704SKent Gibson /* debounce requires explicit input */
139465cff704SKent Gibson if (gpio_v2_line_config_debounced(lc, i) &&
139565cff704SKent Gibson !(flags & GPIO_V2_LINE_FLAG_INPUT))
139665cff704SKent Gibson return -EINVAL;
13973c0d9c63SKent Gibson }
13983c0d9c63SKent Gibson return 0;
13993c0d9c63SKent Gibson }
14003c0d9c63SKent Gibson
gpio_v2_line_config_flags_to_desc_flags(u64 flags,unsigned long * flagsp)14013c0d9c63SKent Gibson static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
14023c0d9c63SKent Gibson unsigned long *flagsp)
14033c0d9c63SKent Gibson {
14043c0d9c63SKent Gibson assign_bit(FLAG_ACTIVE_LOW, flagsp,
14053c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
14063c0d9c63SKent Gibson
14073c0d9c63SKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
14083c0d9c63SKent Gibson set_bit(FLAG_IS_OUT, flagsp);
14093c0d9c63SKent Gibson else if (flags & GPIO_V2_LINE_FLAG_INPUT)
14103c0d9c63SKent Gibson clear_bit(FLAG_IS_OUT, flagsp);
14113c0d9c63SKent Gibson
141273e03419SKent Gibson assign_bit(FLAG_EDGE_RISING, flagsp,
141373e03419SKent Gibson flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
141473e03419SKent Gibson assign_bit(FLAG_EDGE_FALLING, flagsp,
141573e03419SKent Gibson flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
141673e03419SKent Gibson
14173c0d9c63SKent Gibson assign_bit(FLAG_OPEN_DRAIN, flagsp,
14183c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
14193c0d9c63SKent Gibson assign_bit(FLAG_OPEN_SOURCE, flagsp,
14203c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
14213c0d9c63SKent Gibson
14223c0d9c63SKent Gibson assign_bit(FLAG_PULL_UP, flagsp,
14233c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
14243c0d9c63SKent Gibson assign_bit(FLAG_PULL_DOWN, flagsp,
14253c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
14263c0d9c63SKent Gibson assign_bit(FLAG_BIAS_DISABLE, flagsp,
14273c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
142826d060e4SKent Gibson
142926d060e4SKent Gibson assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
143026d060e4SKent Gibson flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
14312068339aSDipen Patel assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp,
14322068339aSDipen Patel flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
14333c0d9c63SKent Gibson }
14343c0d9c63SKent Gibson
linereq_get_values(struct linereq * lr,void __user * ip)14353c0d9c63SKent Gibson static long linereq_get_values(struct linereq *lr, void __user *ip)
14363c0d9c63SKent Gibson {
14373c0d9c63SKent Gibson struct gpio_v2_line_values lv;
14383c0d9c63SKent Gibson DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
14393c0d9c63SKent Gibson struct gpio_desc **descs;
14403c0d9c63SKent Gibson unsigned int i, didx, num_get;
144165cff704SKent Gibson bool val;
14423c0d9c63SKent Gibson int ret;
14433c0d9c63SKent Gibson
14443c0d9c63SKent Gibson /* NOTE: It's ok to read values of output lines. */
14453c0d9c63SKent Gibson if (copy_from_user(&lv, ip, sizeof(lv)))
14463c0d9c63SKent Gibson return -EFAULT;
14473c0d9c63SKent Gibson
14483c0d9c63SKent Gibson for (num_get = 0, i = 0; i < lr->num_lines; i++) {
14493c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) {
14503c0d9c63SKent Gibson num_get++;
14513c0d9c63SKent Gibson descs = &lr->lines[i].desc;
14523c0d9c63SKent Gibson }
14533c0d9c63SKent Gibson }
14543c0d9c63SKent Gibson
14553c0d9c63SKent Gibson if (num_get == 0)
14563c0d9c63SKent Gibson return -EINVAL;
14573c0d9c63SKent Gibson
14583c0d9c63SKent Gibson if (num_get != 1) {
14593c0d9c63SKent Gibson descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
14603c0d9c63SKent Gibson if (!descs)
14613c0d9c63SKent Gibson return -ENOMEM;
14623c0d9c63SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) {
14633c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) {
14643c0d9c63SKent Gibson descs[didx] = lr->lines[i].desc;
14653c0d9c63SKent Gibson didx++;
14663c0d9c63SKent Gibson }
14673c0d9c63SKent Gibson }
14683c0d9c63SKent Gibson }
14693c0d9c63SKent Gibson ret = gpiod_get_array_value_complex(false, true, num_get,
14703c0d9c63SKent Gibson descs, NULL, vals);
14713c0d9c63SKent Gibson
14723c0d9c63SKent Gibson if (num_get != 1)
14733c0d9c63SKent Gibson kfree(descs);
14743c0d9c63SKent Gibson if (ret)
14753c0d9c63SKent Gibson return ret;
14763c0d9c63SKent Gibson
14773c0d9c63SKent Gibson lv.bits = 0;
14783c0d9c63SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) {
14793c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) {
148065cff704SKent Gibson if (lr->lines[i].sw_debounced)
148165cff704SKent Gibson val = debounced_value(&lr->lines[i]);
148265cff704SKent Gibson else
148365cff704SKent Gibson val = test_bit(didx, vals);
148465cff704SKent Gibson if (val)
14853c0d9c63SKent Gibson lv.bits |= BIT_ULL(i);
14863c0d9c63SKent Gibson didx++;
14873c0d9c63SKent Gibson }
14883c0d9c63SKent Gibson }
14893c0d9c63SKent Gibson
14903c0d9c63SKent Gibson if (copy_to_user(ip, &lv, sizeof(lv)))
14913c0d9c63SKent Gibson return -EFAULT;
14923c0d9c63SKent Gibson
14933c0d9c63SKent Gibson return 0;
14943c0d9c63SKent Gibson }
14953c0d9c63SKent Gibson
linereq_set_values_unlocked(struct linereq * lr,struct gpio_v2_line_values * lv)14967b8e00d9SKent Gibson static long linereq_set_values_unlocked(struct linereq *lr,
14977b8e00d9SKent Gibson struct gpio_v2_line_values *lv)
14987b8e00d9SKent Gibson {
14997b8e00d9SKent Gibson DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
15007b8e00d9SKent Gibson struct gpio_desc **descs;
15017b8e00d9SKent Gibson unsigned int i, didx, num_set;
15027b8e00d9SKent Gibson int ret;
15037b8e00d9SKent Gibson
15047b8e00d9SKent Gibson bitmap_zero(vals, GPIO_V2_LINES_MAX);
15057b8e00d9SKent Gibson for (num_set = 0, i = 0; i < lr->num_lines; i++) {
15067b8e00d9SKent Gibson if (lv->mask & BIT_ULL(i)) {
15077b8e00d9SKent Gibson if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
15087b8e00d9SKent Gibson return -EPERM;
15097b8e00d9SKent Gibson if (lv->bits & BIT_ULL(i))
15107b8e00d9SKent Gibson __set_bit(num_set, vals);
15117b8e00d9SKent Gibson num_set++;
15127b8e00d9SKent Gibson descs = &lr->lines[i].desc;
15137b8e00d9SKent Gibson }
15147b8e00d9SKent Gibson }
15157b8e00d9SKent Gibson if (num_set == 0)
15167b8e00d9SKent Gibson return -EINVAL;
15177b8e00d9SKent Gibson
15187b8e00d9SKent Gibson if (num_set != 1) {
15197b8e00d9SKent Gibson /* build compacted desc array and values */
15207b8e00d9SKent Gibson descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
15217b8e00d9SKent Gibson if (!descs)
15227b8e00d9SKent Gibson return -ENOMEM;
15237b8e00d9SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) {
15247b8e00d9SKent Gibson if (lv->mask & BIT_ULL(i)) {
15257b8e00d9SKent Gibson descs[didx] = lr->lines[i].desc;
15267b8e00d9SKent Gibson didx++;
15277b8e00d9SKent Gibson }
15287b8e00d9SKent Gibson }
15297b8e00d9SKent Gibson }
15307b8e00d9SKent Gibson ret = gpiod_set_array_value_complex(false, true, num_set,
15317b8e00d9SKent Gibson descs, NULL, vals);
15327b8e00d9SKent Gibson
15337b8e00d9SKent Gibson if (num_set != 1)
15347b8e00d9SKent Gibson kfree(descs);
15357b8e00d9SKent Gibson return ret;
15367b8e00d9SKent Gibson }
15377b8e00d9SKent Gibson
linereq_set_values(struct linereq * lr,void __user * ip)15387b8e00d9SKent Gibson static long linereq_set_values(struct linereq *lr, void __user *ip)
15397b8e00d9SKent Gibson {
15407b8e00d9SKent Gibson struct gpio_v2_line_values lv;
15417b8e00d9SKent Gibson int ret;
15427b8e00d9SKent Gibson
15437b8e00d9SKent Gibson if (copy_from_user(&lv, ip, sizeof(lv)))
15447b8e00d9SKent Gibson return -EFAULT;
15457b8e00d9SKent Gibson
15467b8e00d9SKent Gibson mutex_lock(&lr->config_mutex);
15477b8e00d9SKent Gibson
15487b8e00d9SKent Gibson ret = linereq_set_values_unlocked(lr, &lv);
15497b8e00d9SKent Gibson
15507b8e00d9SKent Gibson mutex_unlock(&lr->config_mutex);
15517b8e00d9SKent Gibson
15527b8e00d9SKent Gibson return ret;
15537b8e00d9SKent Gibson }
15547b8e00d9SKent Gibson
linereq_set_config_unlocked(struct linereq * lr,struct gpio_v2_line_config * lc)1555a54756cbSKent Gibson static long linereq_set_config_unlocked(struct linereq *lr,
1556a54756cbSKent Gibson struct gpio_v2_line_config *lc)
1557a54756cbSKent Gibson {
1558a54756cbSKent Gibson struct gpio_desc *desc;
1559b1a92e94SKent Gibson struct line *line;
1560a54756cbSKent Gibson unsigned int i;
1561b1a92e94SKent Gibson u64 flags, edflags;
1562a54756cbSKent Gibson int ret;
1563a54756cbSKent Gibson
1564a54756cbSKent Gibson for (i = 0; i < lr->num_lines; i++) {
1565b1a92e94SKent Gibson line = &lr->lines[i];
1566a54756cbSKent Gibson desc = lr->lines[i].desc;
1567a54756cbSKent Gibson flags = gpio_v2_line_config_flags(lc, i);
1568*c4e98006SKent Gibson /*
1569*c4e98006SKent Gibson * Lines not explicitly reconfigured as input or output
1570*c4e98006SKent Gibson * are left unchanged.
1571*c4e98006SKent Gibson */
1572*c4e98006SKent Gibson if (!(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
1573*c4e98006SKent Gibson continue;
1574a54756cbSKent Gibson gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1575b1a92e94SKent Gibson edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
1576a54756cbSKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1577a54756cbSKent Gibson int val = gpio_v2_line_config_output_value(lc, i);
1578a54756cbSKent Gibson
1579b1a92e94SKent Gibson edge_detector_stop(line);
1580a54756cbSKent Gibson ret = gpiod_direction_output(desc, val);
1581a54756cbSKent Gibson if (ret)
1582a54756cbSKent Gibson return ret;
1583*c4e98006SKent Gibson } else {
1584a54756cbSKent Gibson ret = gpiod_direction_input(desc);
1585a54756cbSKent Gibson if (ret)
1586a54756cbSKent Gibson return ret;
1587a54756cbSKent Gibson
1588b1a92e94SKent Gibson ret = edge_detector_update(line, lc, i, edflags);
1589a54756cbSKent Gibson if (ret)
1590a54756cbSKent Gibson return ret;
1591a54756cbSKent Gibson }
1592a54756cbSKent Gibson
1593b1a92e94SKent Gibson WRITE_ONCE(line->edflags, edflags);
1594b1a92e94SKent Gibson
15959ce4ed5bSBartosz Golaszewski gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
1596a54756cbSKent Gibson }
1597a54756cbSKent Gibson return 0;
1598a54756cbSKent Gibson }
1599a54756cbSKent Gibson
linereq_set_config(struct linereq * lr,void __user * ip)1600a54756cbSKent Gibson static long linereq_set_config(struct linereq *lr, void __user *ip)
1601a54756cbSKent Gibson {
1602a54756cbSKent Gibson struct gpio_v2_line_config lc;
1603a54756cbSKent Gibson int ret;
1604a54756cbSKent Gibson
1605a54756cbSKent Gibson if (copy_from_user(&lc, ip, sizeof(lc)))
1606a54756cbSKent Gibson return -EFAULT;
1607a54756cbSKent Gibson
1608a54756cbSKent Gibson ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
1609a54756cbSKent Gibson if (ret)
1610a54756cbSKent Gibson return ret;
1611a54756cbSKent Gibson
1612a54756cbSKent Gibson mutex_lock(&lr->config_mutex);
1613a54756cbSKent Gibson
1614a54756cbSKent Gibson ret = linereq_set_config_unlocked(lr, &lc);
1615a54756cbSKent Gibson
1616a54756cbSKent Gibson mutex_unlock(&lr->config_mutex);
1617a54756cbSKent Gibson
1618a54756cbSKent Gibson return ret;
1619a54756cbSKent Gibson }
1620a54756cbSKent Gibson
linereq_ioctl_unlocked(struct file * file,unsigned int cmd,unsigned long arg)1621bdbbae24SBartosz Golaszewski static long linereq_ioctl_unlocked(struct file *file, unsigned int cmd,
16223c0d9c63SKent Gibson unsigned long arg)
16233c0d9c63SKent Gibson {
16243c0d9c63SKent Gibson struct linereq *lr = file->private_data;
16253c0d9c63SKent Gibson void __user *ip = (void __user *)arg;
16263c0d9c63SKent Gibson
1627533aae7cSBartosz Golaszewski if (!lr->gdev->chip)
1628533aae7cSBartosz Golaszewski return -ENODEV;
1629533aae7cSBartosz Golaszewski
16301cef8b50SAndy Shevchenko switch (cmd) {
16311cef8b50SAndy Shevchenko case GPIO_V2_LINE_GET_VALUES_IOCTL:
16323c0d9c63SKent Gibson return linereq_get_values(lr, ip);
16331cef8b50SAndy Shevchenko case GPIO_V2_LINE_SET_VALUES_IOCTL:
16347b8e00d9SKent Gibson return linereq_set_values(lr, ip);
16351cef8b50SAndy Shevchenko case GPIO_V2_LINE_SET_CONFIG_IOCTL:
1636a54756cbSKent Gibson return linereq_set_config(lr, ip);
16371cef8b50SAndy Shevchenko default:
16383c0d9c63SKent Gibson return -EINVAL;
16393c0d9c63SKent Gibson }
16401cef8b50SAndy Shevchenko }
16413c0d9c63SKent Gibson
linereq_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1642bdbbae24SBartosz Golaszewski static long linereq_ioctl(struct file *file, unsigned int cmd,
1643bdbbae24SBartosz Golaszewski unsigned long arg)
1644bdbbae24SBartosz Golaszewski {
1645bdbbae24SBartosz Golaszewski struct linereq *lr = file->private_data;
1646bdbbae24SBartosz Golaszewski
1647bdbbae24SBartosz Golaszewski return call_ioctl_locked(file, cmd, arg, lr->gdev,
1648bdbbae24SBartosz Golaszewski linereq_ioctl_unlocked);
1649bdbbae24SBartosz Golaszewski }
1650bdbbae24SBartosz Golaszewski
16513c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
linereq_ioctl_compat(struct file * file,unsigned int cmd,unsigned long arg)16523c0d9c63SKent Gibson static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
16533c0d9c63SKent Gibson unsigned long arg)
16543c0d9c63SKent Gibson {
16553c0d9c63SKent Gibson return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
16563c0d9c63SKent Gibson }
16573c0d9c63SKent Gibson #endif
16583c0d9c63SKent Gibson
linereq_poll_unlocked(struct file * file,struct poll_table_struct * wait)1659bdbbae24SBartosz Golaszewski static __poll_t linereq_poll_unlocked(struct file *file,
166073e03419SKent Gibson struct poll_table_struct *wait)
166173e03419SKent Gibson {
166273e03419SKent Gibson struct linereq *lr = file->private_data;
166373e03419SKent Gibson __poll_t events = 0;
166473e03419SKent Gibson
1665533aae7cSBartosz Golaszewski if (!lr->gdev->chip)
1666533aae7cSBartosz Golaszewski return EPOLLHUP | EPOLLERR;
1667533aae7cSBartosz Golaszewski
166873e03419SKent Gibson poll_wait(file, &lr->wait, wait);
166973e03419SKent Gibson
167073e03419SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
167173e03419SKent Gibson &lr->wait.lock))
167273e03419SKent Gibson events = EPOLLIN | EPOLLRDNORM;
167373e03419SKent Gibson
167473e03419SKent Gibson return events;
167573e03419SKent Gibson }
167673e03419SKent Gibson
linereq_poll(struct file * file,struct poll_table_struct * wait)1677bdbbae24SBartosz Golaszewski static __poll_t linereq_poll(struct file *file,
1678bdbbae24SBartosz Golaszewski struct poll_table_struct *wait)
1679bdbbae24SBartosz Golaszewski {
1680bdbbae24SBartosz Golaszewski struct linereq *lr = file->private_data;
1681bdbbae24SBartosz Golaszewski
1682bdbbae24SBartosz Golaszewski return call_poll_locked(file, wait, lr->gdev, linereq_poll_unlocked);
1683bdbbae24SBartosz Golaszewski }
1684bdbbae24SBartosz Golaszewski
linereq_read_unlocked(struct file * file,char __user * buf,size_t count,loff_t * f_ps)1685bdbbae24SBartosz Golaszewski static ssize_t linereq_read_unlocked(struct file *file, char __user *buf,
1686bdbbae24SBartosz Golaszewski size_t count, loff_t *f_ps)
168773e03419SKent Gibson {
168873e03419SKent Gibson struct linereq *lr = file->private_data;
168973e03419SKent Gibson struct gpio_v2_line_event le;
169073e03419SKent Gibson ssize_t bytes_read = 0;
169173e03419SKent Gibson int ret;
169273e03419SKent Gibson
1693533aae7cSBartosz Golaszewski if (!lr->gdev->chip)
1694533aae7cSBartosz Golaszewski return -ENODEV;
1695533aae7cSBartosz Golaszewski
169673e03419SKent Gibson if (count < sizeof(le))
169773e03419SKent Gibson return -EINVAL;
169873e03419SKent Gibson
169973e03419SKent Gibson do {
170073e03419SKent Gibson spin_lock(&lr->wait.lock);
170173e03419SKent Gibson if (kfifo_is_empty(&lr->events)) {
170273e03419SKent Gibson if (bytes_read) {
170373e03419SKent Gibson spin_unlock(&lr->wait.lock);
170473e03419SKent Gibson return bytes_read;
170573e03419SKent Gibson }
170673e03419SKent Gibson
170773e03419SKent Gibson if (file->f_flags & O_NONBLOCK) {
170873e03419SKent Gibson spin_unlock(&lr->wait.lock);
170973e03419SKent Gibson return -EAGAIN;
171073e03419SKent Gibson }
171173e03419SKent Gibson
171273e03419SKent Gibson ret = wait_event_interruptible_locked(lr->wait,
171373e03419SKent Gibson !kfifo_is_empty(&lr->events));
171473e03419SKent Gibson if (ret) {
171573e03419SKent Gibson spin_unlock(&lr->wait.lock);
171673e03419SKent Gibson return ret;
171773e03419SKent Gibson }
171873e03419SKent Gibson }
171973e03419SKent Gibson
172073e03419SKent Gibson ret = kfifo_out(&lr->events, &le, 1);
172173e03419SKent Gibson spin_unlock(&lr->wait.lock);
172273e03419SKent Gibson if (ret != 1) {
172373e03419SKent Gibson /*
172473e03419SKent Gibson * This should never happen - we were holding the
172573e03419SKent Gibson * lock from the moment we learned the fifo is no
172673e03419SKent Gibson * longer empty until now.
172773e03419SKent Gibson */
172873e03419SKent Gibson ret = -EIO;
172973e03419SKent Gibson break;
173073e03419SKent Gibson }
173173e03419SKent Gibson
173273e03419SKent Gibson if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
173373e03419SKent Gibson return -EFAULT;
173473e03419SKent Gibson bytes_read += sizeof(le);
173573e03419SKent Gibson } while (count >= bytes_read + sizeof(le));
173673e03419SKent Gibson
173773e03419SKent Gibson return bytes_read;
173873e03419SKent Gibson }
173973e03419SKent Gibson
linereq_read(struct file * file,char __user * buf,size_t count,loff_t * f_ps)1740bdbbae24SBartosz Golaszewski static ssize_t linereq_read(struct file *file, char __user *buf,
1741bdbbae24SBartosz Golaszewski size_t count, loff_t *f_ps)
1742bdbbae24SBartosz Golaszewski {
1743bdbbae24SBartosz Golaszewski struct linereq *lr = file->private_data;
1744bdbbae24SBartosz Golaszewski
1745bdbbae24SBartosz Golaszewski return call_read_locked(file, buf, count, f_ps, lr->gdev,
1746bdbbae24SBartosz Golaszewski linereq_read_unlocked);
1747bdbbae24SBartosz Golaszewski }
1748bdbbae24SBartosz Golaszewski
linereq_free(struct linereq * lr)17493c0d9c63SKent Gibson static void linereq_free(struct linereq *lr)
17503c0d9c63SKent Gibson {
1751309364bcSKent Gibson struct line *line;
17523c0d9c63SKent Gibson unsigned int i;
17533c0d9c63SKent Gibson
1754a0dda508SBartosz Golaszewski if (lr->device_unregistered_nb.notifier_call)
1755a0dda508SBartosz Golaszewski blocking_notifier_chain_unregister(&lr->gdev->device_notifier,
1756a0dda508SBartosz Golaszewski &lr->device_unregistered_nb);
1757a0dda508SBartosz Golaszewski
17583c0d9c63SKent Gibson for (i = 0; i < lr->num_lines; i++) {
1759309364bcSKent Gibson line = &lr->lines[i];
1760309364bcSKent Gibson if (!line->desc)
1761309364bcSKent Gibson continue;
1762309364bcSKent Gibson
1763309364bcSKent Gibson edge_detector_stop(line);
1764309364bcSKent Gibson if (line_has_supinfo(line))
1765309364bcSKent Gibson supinfo_erase(line);
1766309364bcSKent Gibson gpiod_free(line->desc);
1767160d6e40SKent Gibson }
176873e03419SKent Gibson kfifo_free(&lr->events);
17693c0d9c63SKent Gibson kfree(lr->label);
1770dc0989e3SAndy Shevchenko gpio_device_put(lr->gdev);
17713c0d9c63SKent Gibson kfree(lr);
17723c0d9c63SKent Gibson }
17733c0d9c63SKent Gibson
linereq_release(struct inode * inode,struct file * file)17743c0d9c63SKent Gibson static int linereq_release(struct inode *inode, struct file *file)
17753c0d9c63SKent Gibson {
17763c0d9c63SKent Gibson struct linereq *lr = file->private_data;
17773c0d9c63SKent Gibson
17783c0d9c63SKent Gibson linereq_free(lr);
17793c0d9c63SKent Gibson return 0;
17803c0d9c63SKent Gibson }
17813c0d9c63SKent Gibson
17820ae3109aSBartosz Golaszewski #ifdef CONFIG_PROC_FS
linereq_show_fdinfo(struct seq_file * out,struct file * file)17830ae3109aSBartosz Golaszewski static void linereq_show_fdinfo(struct seq_file *out, struct file *file)
17840ae3109aSBartosz Golaszewski {
17850ae3109aSBartosz Golaszewski struct linereq *lr = file->private_data;
17860ae3109aSBartosz Golaszewski struct device *dev = &lr->gdev->dev;
17870ae3109aSBartosz Golaszewski u16 i;
17880ae3109aSBartosz Golaszewski
17890ae3109aSBartosz Golaszewski seq_printf(out, "gpio-chip:\t%s\n", dev_name(dev));
17900ae3109aSBartosz Golaszewski
17910ae3109aSBartosz Golaszewski for (i = 0; i < lr->num_lines; i++)
17920ae3109aSBartosz Golaszewski seq_printf(out, "gpio-line:\t%d\n",
17930ae3109aSBartosz Golaszewski gpio_chip_hwgpio(lr->lines[i].desc));
17940ae3109aSBartosz Golaszewski }
17950ae3109aSBartosz Golaszewski #endif
17960ae3109aSBartosz Golaszewski
17973c0d9c63SKent Gibson static const struct file_operations line_fileops = {
17983c0d9c63SKent Gibson .release = linereq_release,
179973e03419SKent Gibson .read = linereq_read,
180073e03419SKent Gibson .poll = linereq_poll,
18013c0d9c63SKent Gibson .owner = THIS_MODULE,
18023c0d9c63SKent Gibson .llseek = noop_llseek,
18033c0d9c63SKent Gibson .unlocked_ioctl = linereq_ioctl,
18043c0d9c63SKent Gibson #ifdef CONFIG_COMPAT
18053c0d9c63SKent Gibson .compat_ioctl = linereq_ioctl_compat,
18063c0d9c63SKent Gibson #endif
18070ae3109aSBartosz Golaszewski #ifdef CONFIG_PROC_FS
18080ae3109aSBartosz Golaszewski .show_fdinfo = linereq_show_fdinfo,
18090ae3109aSBartosz Golaszewski #endif
18103c0d9c63SKent Gibson };
18113c0d9c63SKent Gibson
linereq_create(struct gpio_device * gdev,void __user * ip)18123c0d9c63SKent Gibson static int linereq_create(struct gpio_device *gdev, void __user *ip)
18133c0d9c63SKent Gibson {
18143c0d9c63SKent Gibson struct gpio_v2_line_request ulr;
18153c0d9c63SKent Gibson struct gpio_v2_line_config *lc;
18163c0d9c63SKent Gibson struct linereq *lr;
18173c0d9c63SKent Gibson struct file *file;
1818b1a92e94SKent Gibson u64 flags, edflags;
18193c0d9c63SKent Gibson unsigned int i;
18203c0d9c63SKent Gibson int fd, ret;
18213c0d9c63SKent Gibson
18223c0d9c63SKent Gibson if (copy_from_user(&ulr, ip, sizeof(ulr)))
18233c0d9c63SKent Gibson return -EFAULT;
18243c0d9c63SKent Gibson
18253c0d9c63SKent Gibson if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
18263c0d9c63SKent Gibson return -EINVAL;
18273c0d9c63SKent Gibson
18283c0d9c63SKent Gibson if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
18293c0d9c63SKent Gibson return -EINVAL;
18303c0d9c63SKent Gibson
18313c0d9c63SKent Gibson lc = &ulr.config;
18323c0d9c63SKent Gibson ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
18333c0d9c63SKent Gibson if (ret)
18343c0d9c63SKent Gibson return ret;
18353c0d9c63SKent Gibson
18363c0d9c63SKent Gibson lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
18373c0d9c63SKent Gibson if (!lr)
18383c0d9c63SKent Gibson return -ENOMEM;
18393c0d9c63SKent Gibson
1840dc0989e3SAndy Shevchenko lr->gdev = gpio_device_get(gdev);
18413c0d9c63SKent Gibson
184265cff704SKent Gibson for (i = 0; i < ulr.num_lines; i++) {
184373e03419SKent Gibson lr->lines[i].req = lr;
184465cff704SKent Gibson WRITE_ONCE(lr->lines[i].sw_debounced, 0);
184565cff704SKent Gibson INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
184665cff704SKent Gibson }
184773e03419SKent Gibson
1848f188ac12SKent Gibson if (ulr.consumer[0] != '\0') {
18493c0d9c63SKent Gibson /* label is only initialized if consumer is set */
1850f188ac12SKent Gibson lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1851f188ac12SKent Gibson GFP_KERNEL);
18523c0d9c63SKent Gibson if (!lr->label) {
18533c0d9c63SKent Gibson ret = -ENOMEM;
18543c0d9c63SKent Gibson goto out_free_linereq;
18553c0d9c63SKent Gibson }
18563c0d9c63SKent Gibson }
18573c0d9c63SKent Gibson
1858a54756cbSKent Gibson mutex_init(&lr->config_mutex);
185973e03419SKent Gibson init_waitqueue_head(&lr->wait);
186073e03419SKent Gibson lr->event_buffer_size = ulr.event_buffer_size;
186173e03419SKent Gibson if (lr->event_buffer_size == 0)
186273e03419SKent Gibson lr->event_buffer_size = ulr.num_lines * 16;
186373e03419SKent Gibson else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
186473e03419SKent Gibson lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
186573e03419SKent Gibson
186673e03419SKent Gibson atomic_set(&lr->seqno, 0);
18673c0d9c63SKent Gibson lr->num_lines = ulr.num_lines;
18683c0d9c63SKent Gibson
18693c0d9c63SKent Gibson /* Request each GPIO */
18703c0d9c63SKent Gibson for (i = 0; i < ulr.num_lines; i++) {
18713c0d9c63SKent Gibson u32 offset = ulr.offsets[i];
18723c0d9c63SKent Gibson struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
18733c0d9c63SKent Gibson
18743c0d9c63SKent Gibson if (IS_ERR(desc)) {
18753c0d9c63SKent Gibson ret = PTR_ERR(desc);
18763c0d9c63SKent Gibson goto out_free_linereq;
18773c0d9c63SKent Gibson }
18783c0d9c63SKent Gibson
187995a4eed7SAndy Shevchenko ret = gpiod_request_user(desc, lr->label);
18803c0d9c63SKent Gibson if (ret)
18813c0d9c63SKent Gibson goto out_free_linereq;
18823c0d9c63SKent Gibson
18833c0d9c63SKent Gibson lr->lines[i].desc = desc;
18843c0d9c63SKent Gibson flags = gpio_v2_line_config_flags(lc, i);
18853c0d9c63SKent Gibson gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
18863c0d9c63SKent Gibson
18873c0d9c63SKent Gibson ret = gpiod_set_transitory(desc, false);
18883c0d9c63SKent Gibson if (ret < 0)
18893c0d9c63SKent Gibson goto out_free_linereq;
18903c0d9c63SKent Gibson
1891b1a92e94SKent Gibson edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
18923c0d9c63SKent Gibson /*
18933c0d9c63SKent Gibson * Lines have to be requested explicitly for input
18943c0d9c63SKent Gibson * or output, else the line will be treated "as is".
18953c0d9c63SKent Gibson */
18963c0d9c63SKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
18973c0d9c63SKent Gibson int val = gpio_v2_line_config_output_value(lc, i);
18983c0d9c63SKent Gibson
18993c0d9c63SKent Gibson ret = gpiod_direction_output(desc, val);
19003c0d9c63SKent Gibson if (ret)
19013c0d9c63SKent Gibson goto out_free_linereq;
19023c0d9c63SKent Gibson } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
19033c0d9c63SKent Gibson ret = gpiod_direction_input(desc);
19043c0d9c63SKent Gibson if (ret)
19053c0d9c63SKent Gibson goto out_free_linereq;
190673e03419SKent Gibson
190765cff704SKent Gibson ret = edge_detector_setup(&lr->lines[i], lc, i,
1908b1a92e94SKent Gibson edflags);
190973e03419SKent Gibson if (ret)
191073e03419SKent Gibson goto out_free_linereq;
19113c0d9c63SKent Gibson }
19123c0d9c63SKent Gibson
1913b1a92e94SKent Gibson lr->lines[i].edflags = edflags;
1914b1a92e94SKent Gibson
19159ce4ed5bSBartosz Golaszewski gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
19163c0d9c63SKent Gibson
19173c0d9c63SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
19183c0d9c63SKent Gibson offset);
19193c0d9c63SKent Gibson }
19203c0d9c63SKent Gibson
1921a0dda508SBartosz Golaszewski lr->device_unregistered_nb.notifier_call = linereq_unregistered_notify;
1922a0dda508SBartosz Golaszewski ret = blocking_notifier_chain_register(&gdev->device_notifier,
1923a0dda508SBartosz Golaszewski &lr->device_unregistered_nb);
1924a0dda508SBartosz Golaszewski if (ret)
1925a0dda508SBartosz Golaszewski goto out_free_linereq;
1926a0dda508SBartosz Golaszewski
19273c0d9c63SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
19283c0d9c63SKent Gibson if (fd < 0) {
19293c0d9c63SKent Gibson ret = fd;
19303c0d9c63SKent Gibson goto out_free_linereq;
19313c0d9c63SKent Gibson }
19323c0d9c63SKent Gibson
19333c0d9c63SKent Gibson file = anon_inode_getfile("gpio-line", &line_fileops, lr,
19343c0d9c63SKent Gibson O_RDONLY | O_CLOEXEC);
19353c0d9c63SKent Gibson if (IS_ERR(file)) {
19363c0d9c63SKent Gibson ret = PTR_ERR(file);
19373c0d9c63SKent Gibson goto out_put_unused_fd;
19383c0d9c63SKent Gibson }
19393c0d9c63SKent Gibson
19403c0d9c63SKent Gibson ulr.fd = fd;
19413c0d9c63SKent Gibson if (copy_to_user(ip, &ulr, sizeof(ulr))) {
19423c0d9c63SKent Gibson /*
19433c0d9c63SKent Gibson * fput() will trigger the release() callback, so do not go onto
19443c0d9c63SKent Gibson * the regular error cleanup path here.
19453c0d9c63SKent Gibson */
19463c0d9c63SKent Gibson fput(file);
19473c0d9c63SKent Gibson put_unused_fd(fd);
19483c0d9c63SKent Gibson return -EFAULT;
19493c0d9c63SKent Gibson }
19503c0d9c63SKent Gibson
19513c0d9c63SKent Gibson fd_install(fd, file);
19523c0d9c63SKent Gibson
19533c0d9c63SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
19543c0d9c63SKent Gibson lr->num_lines);
19553c0d9c63SKent Gibson
19563c0d9c63SKent Gibson return 0;
19573c0d9c63SKent Gibson
19583c0d9c63SKent Gibson out_put_unused_fd:
19593c0d9c63SKent Gibson put_unused_fd(fd);
19603c0d9c63SKent Gibson out_free_linereq:
19613c0d9c63SKent Gibson linereq_free(lr);
19623c0d9c63SKent Gibson return ret;
19633c0d9c63SKent Gibson }
19643c0d9c63SKent Gibson
19653c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
1966925ca369SKent Gibson
1967925ca369SKent Gibson /*
1968925ca369SKent Gibson * GPIO line event management
1969925ca369SKent Gibson */
1970925ca369SKent Gibson
1971925ca369SKent Gibson /**
1972925ca369SKent Gibson * struct lineevent_state - contains the state of a userspace event
1973925ca369SKent Gibson * @gdev: the GPIO device the event pertains to
1974925ca369SKent Gibson * @label: consumer label used to tag descriptors
1975925ca369SKent Gibson * @desc: the GPIO descriptor held by this event
1976925ca369SKent Gibson * @eflags: the event flags this line was requested with
1977925ca369SKent Gibson * @irq: the interrupt that trigger in response to events on this GPIO
1978925ca369SKent Gibson * @wait: wait queue that handles blocking reads of events
197991043f55SBartosz Golaszewski * @device_unregistered_nb: notifier block for receiving gdev unregister events
1980925ca369SKent Gibson * @events: KFIFO for the GPIO events
1981925ca369SKent Gibson * @timestamp: cache for the timestamp storing it between hardirq
1982925ca369SKent Gibson * and IRQ thread, used to bring the timestamp close to the actual
1983925ca369SKent Gibson * event
1984925ca369SKent Gibson */
1985925ca369SKent Gibson struct lineevent_state {
1986925ca369SKent Gibson struct gpio_device *gdev;
1987925ca369SKent Gibson const char *label;
1988925ca369SKent Gibson struct gpio_desc *desc;
1989925ca369SKent Gibson u32 eflags;
1990925ca369SKent Gibson int irq;
1991925ca369SKent Gibson wait_queue_head_t wait;
199291043f55SBartosz Golaszewski struct notifier_block device_unregistered_nb;
1993925ca369SKent Gibson DECLARE_KFIFO(events, struct gpioevent_data, 16);
1994925ca369SKent Gibson u64 timestamp;
1995925ca369SKent Gibson };
1996925ca369SKent Gibson
1997925ca369SKent Gibson #define GPIOEVENT_REQUEST_VALID_FLAGS \
1998925ca369SKent Gibson (GPIOEVENT_REQUEST_RISING_EDGE | \
1999925ca369SKent Gibson GPIOEVENT_REQUEST_FALLING_EDGE)
2000925ca369SKent Gibson
lineevent_poll_unlocked(struct file * file,struct poll_table_struct * wait)2001bdbbae24SBartosz Golaszewski static __poll_t lineevent_poll_unlocked(struct file *file,
2002925ca369SKent Gibson struct poll_table_struct *wait)
2003925ca369SKent Gibson {
200449bc5279SKent Gibson struct lineevent_state *le = file->private_data;
2005925ca369SKent Gibson __poll_t events = 0;
2006925ca369SKent Gibson
2007533aae7cSBartosz Golaszewski if (!le->gdev->chip)
2008533aae7cSBartosz Golaszewski return EPOLLHUP | EPOLLERR;
2009533aae7cSBartosz Golaszewski
201049bc5279SKent Gibson poll_wait(file, &le->wait, wait);
2011925ca369SKent Gibson
2012925ca369SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
2013925ca369SKent Gibson events = EPOLLIN | EPOLLRDNORM;
2014925ca369SKent Gibson
2015925ca369SKent Gibson return events;
2016925ca369SKent Gibson }
2017925ca369SKent Gibson
lineevent_poll(struct file * file,struct poll_table_struct * wait)2018bdbbae24SBartosz Golaszewski static __poll_t lineevent_poll(struct file *file,
2019bdbbae24SBartosz Golaszewski struct poll_table_struct *wait)
2020bdbbae24SBartosz Golaszewski {
2021bdbbae24SBartosz Golaszewski struct lineevent_state *le = file->private_data;
2022bdbbae24SBartosz Golaszewski
2023bdbbae24SBartosz Golaszewski return call_poll_locked(file, wait, le->gdev, lineevent_poll_unlocked);
2024bdbbae24SBartosz Golaszewski }
2025bdbbae24SBartosz Golaszewski
lineevent_unregistered_notify(struct notifier_block * nb,unsigned long action,void * data)202691043f55SBartosz Golaszewski static int lineevent_unregistered_notify(struct notifier_block *nb,
202791043f55SBartosz Golaszewski unsigned long action, void *data)
202891043f55SBartosz Golaszewski {
202991043f55SBartosz Golaszewski struct lineevent_state *le = container_of(nb, struct lineevent_state,
203091043f55SBartosz Golaszewski device_unregistered_nb);
203191043f55SBartosz Golaszewski
203291043f55SBartosz Golaszewski wake_up_poll(&le->wait, EPOLLIN | EPOLLERR);
203391043f55SBartosz Golaszewski
203491043f55SBartosz Golaszewski return NOTIFY_OK;
203591043f55SBartosz Golaszewski }
203691043f55SBartosz Golaszewski
20375ad284abSAndy Shevchenko struct compat_gpioeevent_data {
20385ad284abSAndy Shevchenko compat_u64 timestamp;
20395ad284abSAndy Shevchenko u32 id;
20405ad284abSAndy Shevchenko };
20415ad284abSAndy Shevchenko
lineevent_read_unlocked(struct file * file,char __user * buf,size_t count,loff_t * f_ps)2042bdbbae24SBartosz Golaszewski static ssize_t lineevent_read_unlocked(struct file *file, char __user *buf,
2043bdbbae24SBartosz Golaszewski size_t count, loff_t *f_ps)
2044925ca369SKent Gibson {
204549bc5279SKent Gibson struct lineevent_state *le = file->private_data;
2046925ca369SKent Gibson struct gpioevent_data ge;
2047925ca369SKent Gibson ssize_t bytes_read = 0;
20485ad284abSAndy Shevchenko ssize_t ge_size;
2049925ca369SKent Gibson int ret;
2050925ca369SKent Gibson
2051533aae7cSBartosz Golaszewski if (!le->gdev->chip)
2052533aae7cSBartosz Golaszewski return -ENODEV;
2053533aae7cSBartosz Golaszewski
20545ad284abSAndy Shevchenko /*
20555ad284abSAndy Shevchenko * When compatible system call is being used the struct gpioevent_data,
20565ad284abSAndy Shevchenko * in case of at least ia32, has different size due to the alignment
20575ad284abSAndy Shevchenko * differences. Because we have first member 64 bits followed by one of
20585ad284abSAndy Shevchenko * 32 bits there is no gap between them. The only difference is the
20595ad284abSAndy Shevchenko * padding at the end of the data structure. Hence, we calculate the
20605ad284abSAndy Shevchenko * actual sizeof() and pass this as an argument to copy_to_user() to
20615ad284abSAndy Shevchenko * drop unneeded bytes from the output.
20625ad284abSAndy Shevchenko */
2063163d1719SAndy Shevchenko if (compat_need_64bit_alignment_fixup())
2064163d1719SAndy Shevchenko ge_size = sizeof(struct compat_gpioeevent_data);
2065163d1719SAndy Shevchenko else
2066163d1719SAndy Shevchenko ge_size = sizeof(struct gpioevent_data);
20675ad284abSAndy Shevchenko if (count < ge_size)
2068925ca369SKent Gibson return -EINVAL;
2069925ca369SKent Gibson
2070925ca369SKent Gibson do {
2071925ca369SKent Gibson spin_lock(&le->wait.lock);
2072925ca369SKent Gibson if (kfifo_is_empty(&le->events)) {
2073925ca369SKent Gibson if (bytes_read) {
2074925ca369SKent Gibson spin_unlock(&le->wait.lock);
2075925ca369SKent Gibson return bytes_read;
2076925ca369SKent Gibson }
2077925ca369SKent Gibson
207849bc5279SKent Gibson if (file->f_flags & O_NONBLOCK) {
2079925ca369SKent Gibson spin_unlock(&le->wait.lock);
2080925ca369SKent Gibson return -EAGAIN;
2081925ca369SKent Gibson }
2082925ca369SKent Gibson
2083925ca369SKent Gibson ret = wait_event_interruptible_locked(le->wait,
2084925ca369SKent Gibson !kfifo_is_empty(&le->events));
2085925ca369SKent Gibson if (ret) {
2086925ca369SKent Gibson spin_unlock(&le->wait.lock);
2087925ca369SKent Gibson return ret;
2088925ca369SKent Gibson }
2089925ca369SKent Gibson }
2090925ca369SKent Gibson
2091925ca369SKent Gibson ret = kfifo_out(&le->events, &ge, 1);
2092925ca369SKent Gibson spin_unlock(&le->wait.lock);
2093925ca369SKent Gibson if (ret != 1) {
2094925ca369SKent Gibson /*
2095925ca369SKent Gibson * This should never happen - we were holding the lock
2096925ca369SKent Gibson * from the moment we learned the fifo is no longer
2097925ca369SKent Gibson * empty until now.
2098925ca369SKent Gibson */
2099925ca369SKent Gibson ret = -EIO;
2100925ca369SKent Gibson break;
2101925ca369SKent Gibson }
2102925ca369SKent Gibson
21035ad284abSAndy Shevchenko if (copy_to_user(buf + bytes_read, &ge, ge_size))
2104925ca369SKent Gibson return -EFAULT;
21055ad284abSAndy Shevchenko bytes_read += ge_size;
21065ad284abSAndy Shevchenko } while (count >= bytes_read + ge_size);
2107925ca369SKent Gibson
2108925ca369SKent Gibson return bytes_read;
2109925ca369SKent Gibson }
2110925ca369SKent Gibson
lineevent_read(struct file * file,char __user * buf,size_t count,loff_t * f_ps)2111bdbbae24SBartosz Golaszewski static ssize_t lineevent_read(struct file *file, char __user *buf,
2112bdbbae24SBartosz Golaszewski size_t count, loff_t *f_ps)
2113bdbbae24SBartosz Golaszewski {
2114bdbbae24SBartosz Golaszewski struct lineevent_state *le = file->private_data;
2115bdbbae24SBartosz Golaszewski
2116bdbbae24SBartosz Golaszewski return call_read_locked(file, buf, count, f_ps, le->gdev,
2117bdbbae24SBartosz Golaszewski lineevent_read_unlocked);
2118bdbbae24SBartosz Golaszewski }
2119bdbbae24SBartosz Golaszewski
lineevent_free(struct lineevent_state * le)212046824272SKent Gibson static void lineevent_free(struct lineevent_state *le)
2121925ca369SKent Gibson {
212291043f55SBartosz Golaszewski if (le->device_unregistered_nb.notifier_call)
212391043f55SBartosz Golaszewski blocking_notifier_chain_unregister(&le->gdev->device_notifier,
212491043f55SBartosz Golaszewski &le->device_unregistered_nb);
212546824272SKent Gibson if (le->irq)
212621bc9b15SBartosz Golaszewski free_irq_label(free_irq(le->irq, le));
212746824272SKent Gibson if (le->desc)
2128925ca369SKent Gibson gpiod_free(le->desc);
2129925ca369SKent Gibson kfree(le->label);
2130dc0989e3SAndy Shevchenko gpio_device_put(le->gdev);
2131925ca369SKent Gibson kfree(le);
213246824272SKent Gibson }
213346824272SKent Gibson
lineevent_release(struct inode * inode,struct file * file)213446824272SKent Gibson static int lineevent_release(struct inode *inode, struct file *file)
213546824272SKent Gibson {
213646824272SKent Gibson lineevent_free(file->private_data);
2137925ca369SKent Gibson return 0;
2138925ca369SKent Gibson }
2139925ca369SKent Gibson
lineevent_ioctl_unlocked(struct file * file,unsigned int cmd,unsigned long arg)2140bdbbae24SBartosz Golaszewski static long lineevent_ioctl_unlocked(struct file *file, unsigned int cmd,
2141925ca369SKent Gibson unsigned long arg)
2142925ca369SKent Gibson {
214349bc5279SKent Gibson struct lineevent_state *le = file->private_data;
2144925ca369SKent Gibson void __user *ip = (void __user *)arg;
2145925ca369SKent Gibson struct gpiohandle_data ghd;
2146925ca369SKent Gibson
2147533aae7cSBartosz Golaszewski if (!le->gdev->chip)
2148533aae7cSBartosz Golaszewski return -ENODEV;
2149533aae7cSBartosz Golaszewski
2150925ca369SKent Gibson /*
2151925ca369SKent Gibson * We can get the value for an event line but not set it,
2152925ca369SKent Gibson * because it is input by definition.
2153925ca369SKent Gibson */
2154925ca369SKent Gibson if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
2155925ca369SKent Gibson int val;
2156925ca369SKent Gibson
2157925ca369SKent Gibson memset(&ghd, 0, sizeof(ghd));
2158925ca369SKent Gibson
2159925ca369SKent Gibson val = gpiod_get_value_cansleep(le->desc);
2160925ca369SKent Gibson if (val < 0)
2161925ca369SKent Gibson return val;
2162925ca369SKent Gibson ghd.values[0] = val;
2163925ca369SKent Gibson
2164925ca369SKent Gibson if (copy_to_user(ip, &ghd, sizeof(ghd)))
2165925ca369SKent Gibson return -EFAULT;
2166925ca369SKent Gibson
2167925ca369SKent Gibson return 0;
2168925ca369SKent Gibson }
2169925ca369SKent Gibson return -EINVAL;
2170925ca369SKent Gibson }
2171925ca369SKent Gibson
lineevent_ioctl(struct file * file,unsigned int cmd,unsigned long arg)2172bdbbae24SBartosz Golaszewski static long lineevent_ioctl(struct file *file, unsigned int cmd,
2173bdbbae24SBartosz Golaszewski unsigned long arg)
2174bdbbae24SBartosz Golaszewski {
2175bdbbae24SBartosz Golaszewski struct lineevent_state *le = file->private_data;
2176bdbbae24SBartosz Golaszewski
2177bdbbae24SBartosz Golaszewski return call_ioctl_locked(file, cmd, arg, le->gdev,
2178bdbbae24SBartosz Golaszewski lineevent_ioctl_unlocked);
2179bdbbae24SBartosz Golaszewski }
2180bdbbae24SBartosz Golaszewski
2181925ca369SKent Gibson #ifdef CONFIG_COMPAT
lineevent_ioctl_compat(struct file * file,unsigned int cmd,unsigned long arg)218249bc5279SKent Gibson static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
2183925ca369SKent Gibson unsigned long arg)
2184925ca369SKent Gibson {
218549bc5279SKent Gibson return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2186925ca369SKent Gibson }
2187925ca369SKent Gibson #endif
2188925ca369SKent Gibson
2189925ca369SKent Gibson static const struct file_operations lineevent_fileops = {
2190925ca369SKent Gibson .release = lineevent_release,
2191925ca369SKent Gibson .read = lineevent_read,
2192925ca369SKent Gibson .poll = lineevent_poll,
2193925ca369SKent Gibson .owner = THIS_MODULE,
2194925ca369SKent Gibson .llseek = noop_llseek,
2195925ca369SKent Gibson .unlocked_ioctl = lineevent_ioctl,
2196925ca369SKent Gibson #ifdef CONFIG_COMPAT
2197925ca369SKent Gibson .compat_ioctl = lineevent_ioctl_compat,
2198925ca369SKent Gibson #endif
2199925ca369SKent Gibson };
2200925ca369SKent Gibson
lineevent_irq_thread(int irq,void * p)2201925ca369SKent Gibson static irqreturn_t lineevent_irq_thread(int irq, void *p)
2202925ca369SKent Gibson {
2203925ca369SKent Gibson struct lineevent_state *le = p;
2204925ca369SKent Gibson struct gpioevent_data ge;
2205925ca369SKent Gibson int ret;
2206925ca369SKent Gibson
2207925ca369SKent Gibson /* Do not leak kernel stack to userspace */
2208925ca369SKent Gibson memset(&ge, 0, sizeof(ge));
2209925ca369SKent Gibson
2210925ca369SKent Gibson /*
2211925ca369SKent Gibson * We may be running from a nested threaded interrupt in which case
2212925ca369SKent Gibson * we didn't get the timestamp from lineevent_irq_handler().
2213925ca369SKent Gibson */
2214925ca369SKent Gibson if (!le->timestamp)
2215925ca369SKent Gibson ge.timestamp = ktime_get_ns();
2216925ca369SKent Gibson else
2217925ca369SKent Gibson ge.timestamp = le->timestamp;
2218925ca369SKent Gibson
2219925ca369SKent Gibson if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
2220925ca369SKent Gibson && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
2221925ca369SKent Gibson int level = gpiod_get_value_cansleep(le->desc);
2222925ca369SKent Gibson
2223925ca369SKent Gibson if (level)
2224925ca369SKent Gibson /* Emit low-to-high event */
2225925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_RISING_EDGE;
2226925ca369SKent Gibson else
2227925ca369SKent Gibson /* Emit high-to-low event */
2228925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
2229925ca369SKent Gibson } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
2230925ca369SKent Gibson /* Emit low-to-high event */
2231925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_RISING_EDGE;
2232925ca369SKent Gibson } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
2233925ca369SKent Gibson /* Emit high-to-low event */
2234925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
2235925ca369SKent Gibson } else {
2236925ca369SKent Gibson return IRQ_NONE;
2237925ca369SKent Gibson }
2238925ca369SKent Gibson
2239925ca369SKent Gibson ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
2240925ca369SKent Gibson 1, &le->wait.lock);
2241925ca369SKent Gibson if (ret)
2242925ca369SKent Gibson wake_up_poll(&le->wait, EPOLLIN);
2243925ca369SKent Gibson else
2244925ca369SKent Gibson pr_debug_ratelimited("event FIFO is full - event dropped\n");
2245925ca369SKent Gibson
2246925ca369SKent Gibson return IRQ_HANDLED;
2247925ca369SKent Gibson }
2248925ca369SKent Gibson
lineevent_irq_handler(int irq,void * p)2249925ca369SKent Gibson static irqreturn_t lineevent_irq_handler(int irq, void *p)
2250925ca369SKent Gibson {
2251925ca369SKent Gibson struct lineevent_state *le = p;
2252925ca369SKent Gibson
2253925ca369SKent Gibson /*
2254925ca369SKent Gibson * Just store the timestamp in hardirq context so we get it as
2255925ca369SKent Gibson * close in time as possible to the actual event.
2256925ca369SKent Gibson */
2257925ca369SKent Gibson le->timestamp = ktime_get_ns();
2258925ca369SKent Gibson
2259925ca369SKent Gibson return IRQ_WAKE_THREAD;
2260925ca369SKent Gibson }
2261925ca369SKent Gibson
lineevent_create(struct gpio_device * gdev,void __user * ip)2262925ca369SKent Gibson static int lineevent_create(struct gpio_device *gdev, void __user *ip)
2263925ca369SKent Gibson {
2264925ca369SKent Gibson struct gpioevent_request eventreq;
2265925ca369SKent Gibson struct lineevent_state *le;
2266925ca369SKent Gibson struct gpio_desc *desc;
2267925ca369SKent Gibson struct file *file;
2268925ca369SKent Gibson u32 offset;
2269925ca369SKent Gibson u32 lflags;
2270925ca369SKent Gibson u32 eflags;
2271925ca369SKent Gibson int fd;
2272925ca369SKent Gibson int ret;
227346824272SKent Gibson int irq, irqflags = 0;
227421bc9b15SBartosz Golaszewski char *label;
2275925ca369SKent Gibson
2276925ca369SKent Gibson if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
2277925ca369SKent Gibson return -EFAULT;
2278925ca369SKent Gibson
2279925ca369SKent Gibson offset = eventreq.lineoffset;
2280925ca369SKent Gibson lflags = eventreq.handleflags;
2281925ca369SKent Gibson eflags = eventreq.eventflags;
2282925ca369SKent Gibson
2283925ca369SKent Gibson desc = gpiochip_get_desc(gdev->chip, offset);
2284925ca369SKent Gibson if (IS_ERR(desc))
2285925ca369SKent Gibson return PTR_ERR(desc);
2286925ca369SKent Gibson
2287925ca369SKent Gibson /* Return an error if a unknown flag is set */
2288925ca369SKent Gibson if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
2289925ca369SKent Gibson (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
2290925ca369SKent Gibson return -EINVAL;
2291925ca369SKent Gibson
2292925ca369SKent Gibson /* This is just wrong: we don't look for events on output lines */
2293925ca369SKent Gibson if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
2294925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
2295925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
2296925ca369SKent Gibson return -EINVAL;
2297925ca369SKent Gibson
2298925ca369SKent Gibson /* Only one bias flag can be set. */
2299925ca369SKent Gibson if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
2300925ca369SKent Gibson (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
2301925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
2302925ca369SKent Gibson ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
2303925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
2304925ca369SKent Gibson return -EINVAL;
2305925ca369SKent Gibson
2306925ca369SKent Gibson le = kzalloc(sizeof(*le), GFP_KERNEL);
2307925ca369SKent Gibson if (!le)
2308925ca369SKent Gibson return -ENOMEM;
2309dc0989e3SAndy Shevchenko le->gdev = gpio_device_get(gdev);
2310925ca369SKent Gibson
2311f188ac12SKent Gibson if (eventreq.consumer_label[0] != '\0') {
2312f188ac12SKent Gibson /* label is only initialized if consumer_label is set */
2313f188ac12SKent Gibson le->label = kstrndup(eventreq.consumer_label,
2314f188ac12SKent Gibson sizeof(eventreq.consumer_label) - 1,
2315925ca369SKent Gibson GFP_KERNEL);
2316925ca369SKent Gibson if (!le->label) {
2317925ca369SKent Gibson ret = -ENOMEM;
2318925ca369SKent Gibson goto out_free_le;
2319925ca369SKent Gibson }
2320925ca369SKent Gibson }
2321925ca369SKent Gibson
232295a4eed7SAndy Shevchenko ret = gpiod_request_user(desc, le->label);
2323925ca369SKent Gibson if (ret)
232446824272SKent Gibson goto out_free_le;
2325925ca369SKent Gibson le->desc = desc;
2326925ca369SKent Gibson le->eflags = eflags;
2327925ca369SKent Gibson
2328c274b58aSKent Gibson linehandle_flags_to_desc_flags(lflags, &desc->flags);
2329925ca369SKent Gibson
2330925ca369SKent Gibson ret = gpiod_direction_input(desc);
2331925ca369SKent Gibson if (ret)
233246824272SKent Gibson goto out_free_le;
2333925ca369SKent Gibson
23349ce4ed5bSBartosz Golaszewski gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
2335925ca369SKent Gibson
233646824272SKent Gibson irq = gpiod_to_irq(desc);
233746824272SKent Gibson if (irq <= 0) {
2338925ca369SKent Gibson ret = -ENODEV;
233946824272SKent Gibson goto out_free_le;
2340925ca369SKent Gibson }
2341925ca369SKent Gibson
2342925ca369SKent Gibson if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
2343925ca369SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
2344925ca369SKent Gibson IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
2345925ca369SKent Gibson if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
2346925ca369SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
2347925ca369SKent Gibson IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
2348925ca369SKent Gibson irqflags |= IRQF_ONESHOT;
2349925ca369SKent Gibson
2350925ca369SKent Gibson INIT_KFIFO(le->events);
2351925ca369SKent Gibson init_waitqueue_head(&le->wait);
2352925ca369SKent Gibson
235391043f55SBartosz Golaszewski le->device_unregistered_nb.notifier_call = lineevent_unregistered_notify;
235491043f55SBartosz Golaszewski ret = blocking_notifier_chain_register(&gdev->device_notifier,
235591043f55SBartosz Golaszewski &le->device_unregistered_nb);
235691043f55SBartosz Golaszewski if (ret)
235791043f55SBartosz Golaszewski goto out_free_le;
235891043f55SBartosz Golaszewski
235921bc9b15SBartosz Golaszewski label = make_irq_label(le->label);
2360d9f0804aSBartosz Golaszewski if (IS_ERR(label)) {
2361d9f0804aSBartosz Golaszewski ret = PTR_ERR(label);
236221bc9b15SBartosz Golaszewski goto out_free_le;
236321bc9b15SBartosz Golaszewski }
236421bc9b15SBartosz Golaszewski
2365925ca369SKent Gibson /* Request a thread to read the events */
236669bef19dSMeng Li ret = request_threaded_irq(irq,
2367925ca369SKent Gibson lineevent_irq_handler,
2368925ca369SKent Gibson lineevent_irq_thread,
2369925ca369SKent Gibson irqflags,
237021bc9b15SBartosz Golaszewski label,
2371925ca369SKent Gibson le);
237221bc9b15SBartosz Golaszewski if (ret) {
237321bc9b15SBartosz Golaszewski free_irq_label(label);
237446824272SKent Gibson goto out_free_le;
237521bc9b15SBartosz Golaszewski }
2376925ca369SKent Gibson
237769bef19dSMeng Li le->irq = irq;
237869bef19dSMeng Li
2379925ca369SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
2380925ca369SKent Gibson if (fd < 0) {
2381925ca369SKent Gibson ret = fd;
238246824272SKent Gibson goto out_free_le;
2383925ca369SKent Gibson }
2384925ca369SKent Gibson
2385925ca369SKent Gibson file = anon_inode_getfile("gpio-event",
2386925ca369SKent Gibson &lineevent_fileops,
2387925ca369SKent Gibson le,
2388925ca369SKent Gibson O_RDONLY | O_CLOEXEC);
2389925ca369SKent Gibson if (IS_ERR(file)) {
2390925ca369SKent Gibson ret = PTR_ERR(file);
2391925ca369SKent Gibson goto out_put_unused_fd;
2392925ca369SKent Gibson }
2393925ca369SKent Gibson
2394925ca369SKent Gibson eventreq.fd = fd;
2395925ca369SKent Gibson if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
2396925ca369SKent Gibson /*
2397925ca369SKent Gibson * fput() will trigger the release() callback, so do not go onto
2398925ca369SKent Gibson * the regular error cleanup path here.
2399925ca369SKent Gibson */
2400925ca369SKent Gibson fput(file);
2401925ca369SKent Gibson put_unused_fd(fd);
2402925ca369SKent Gibson return -EFAULT;
2403925ca369SKent Gibson }
2404925ca369SKent Gibson
2405925ca369SKent Gibson fd_install(fd, file);
2406925ca369SKent Gibson
2407925ca369SKent Gibson return 0;
2408925ca369SKent Gibson
2409925ca369SKent Gibson out_put_unused_fd:
2410925ca369SKent Gibson put_unused_fd(fd);
2411925ca369SKent Gibson out_free_le:
241246824272SKent Gibson lineevent_free(le);
2413925ca369SKent Gibson return ret;
2414925ca369SKent Gibson }
2415925ca369SKent Gibson
gpio_v2_line_info_to_v1(struct gpio_v2_line_info * info_v2,struct gpioline_info * info_v1)2416aad95584SKent Gibson static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
2417aad95584SKent Gibson struct gpioline_info *info_v1)
2418aad95584SKent Gibson {
2419aad95584SKent Gibson u64 flagsv2 = info_v2->flags;
2420aad95584SKent Gibson
2421aad95584SKent Gibson memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
2422aad95584SKent Gibson memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
2423aad95584SKent Gibson info_v1->line_offset = info_v2->offset;
2424aad95584SKent Gibson info_v1->flags = 0;
2425aad95584SKent Gibson
2426aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
2427aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_KERNEL;
2428aad95584SKent Gibson
2429aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
2430aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
2431aad95584SKent Gibson
2432aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
2433aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
2434aad95584SKent Gibson
2435aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
2436aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
2437aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
2438aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
2439aad95584SKent Gibson
2440aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
2441aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
2442aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
2443aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
2444aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
2445aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
2446aad95584SKent Gibson }
2447aad95584SKent Gibson
gpio_v2_line_info_changed_to_v1(struct gpio_v2_line_info_changed * lic_v2,struct gpioline_info_changed * lic_v1)2448aad95584SKent Gibson static void gpio_v2_line_info_changed_to_v1(
2449aad95584SKent Gibson struct gpio_v2_line_info_changed *lic_v2,
2450aad95584SKent Gibson struct gpioline_info_changed *lic_v1)
2451aad95584SKent Gibson {
2452cb8f63b8SGabriel Knezek memset(lic_v1, 0, sizeof(*lic_v1));
2453aad95584SKent Gibson gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
2454aad95584SKent Gibson lic_v1->timestamp = lic_v2->timestamp_ns;
2455aad95584SKent Gibson lic_v1->event_type = lic_v2->event_type;
2456aad95584SKent Gibson }
2457aad95584SKent Gibson
24583c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
24593c0d9c63SKent Gibson
gpio_desc_to_lineinfo(struct gpio_desc * desc,struct gpio_v2_line_info * info)2460925ca369SKent Gibson static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
2461aad95584SKent Gibson struct gpio_v2_line_info *info)
2462925ca369SKent Gibson {
2463925ca369SKent Gibson struct gpio_chip *gc = desc->gdev->chip;
2464925ca369SKent Gibson bool ok_for_pinctrl;
2465925ca369SKent Gibson unsigned long flags;
2466925ca369SKent Gibson
246769e4e136SKent Gibson memset(info, 0, sizeof(*info));
2468aad95584SKent Gibson info->offset = gpio_chip_hwgpio(desc);
2469925ca369SKent Gibson
2470925ca369SKent Gibson /*
2471925ca369SKent Gibson * This function takes a mutex so we must check this before taking
2472925ca369SKent Gibson * the spinlock.
2473925ca369SKent Gibson *
2474925ca369SKent Gibson * FIXME: find a non-racy way to retrieve this information. Maybe a
2475925ca369SKent Gibson * lock common to both frameworks?
2476925ca369SKent Gibson */
2477925ca369SKent Gibson ok_for_pinctrl =
2478aad95584SKent Gibson pinctrl_gpio_can_use_line(gc->base + info->offset);
2479925ca369SKent Gibson
2480925ca369SKent Gibson spin_lock_irqsave(&gpio_lock, flags);
2481925ca369SKent Gibson
248269e4e136SKent Gibson if (desc->name)
248369e4e136SKent Gibson strscpy(info->name, desc->name, sizeof(info->name));
2484925ca369SKent Gibson
248569e4e136SKent Gibson if (desc->label)
248669e4e136SKent Gibson strscpy(info->consumer, desc->label, sizeof(info->consumer));
2487925ca369SKent Gibson
2488925ca369SKent Gibson /*
2489925ca369SKent Gibson * Userspace only need to know that the kernel is using this GPIO so
2490925ca369SKent Gibson * it can't use it.
2491925ca369SKent Gibson */
2492925ca369SKent Gibson info->flags = 0;
2493925ca369SKent Gibson if (test_bit(FLAG_REQUESTED, &desc->flags) ||
2494925ca369SKent Gibson test_bit(FLAG_IS_HOGGED, &desc->flags) ||
2495925ca369SKent Gibson test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
2496925ca369SKent Gibson test_bit(FLAG_EXPORT, &desc->flags) ||
2497925ca369SKent Gibson test_bit(FLAG_SYSFS, &desc->flags) ||
2498a0db197fSMarc Zyngier !gpiochip_line_is_valid(gc, info->offset) ||
2499925ca369SKent Gibson !ok_for_pinctrl)
2500aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_USED;
2501aad95584SKent Gibson
2502925ca369SKent Gibson if (test_bit(FLAG_IS_OUT, &desc->flags))
2503aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
2504aad95584SKent Gibson else
2505aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_INPUT;
2506aad95584SKent Gibson
2507925ca369SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2508aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
2509aad95584SKent Gibson
2510925ca369SKent Gibson if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2511aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
2512925ca369SKent Gibson if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2513aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
2514aad95584SKent Gibson
2515925ca369SKent Gibson if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2516aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
2517925ca369SKent Gibson if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2518aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
2519925ca369SKent Gibson if (test_bit(FLAG_PULL_UP, &desc->flags))
2520aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
2521925ca369SKent Gibson
252273e03419SKent Gibson if (test_bit(FLAG_EDGE_RISING, &desc->flags))
252373e03419SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
252473e03419SKent Gibson if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
252573e03419SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
252673e03419SKent Gibson
252726d060e4SKent Gibson if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
252826d060e4SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
25292068339aSDipen Patel else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags))
25302068339aSDipen Patel info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
253126d060e4SKent Gibson
2532925ca369SKent Gibson spin_unlock_irqrestore(&gpio_lock, flags);
2533925ca369SKent Gibson }
2534925ca369SKent Gibson
2535925ca369SKent Gibson struct gpio_chardev_data {
2536925ca369SKent Gibson struct gpio_device *gdev;
2537925ca369SKent Gibson wait_queue_head_t wait;
2538aad95584SKent Gibson DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
2539925ca369SKent Gibson struct notifier_block lineinfo_changed_nb;
2540d2e2586aSBartosz Golaszewski struct notifier_block device_unregistered_nb;
2541925ca369SKent Gibson unsigned long *watched_lines;
2542aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2543aad95584SKent Gibson atomic_t watch_abi_version;
2544aad95584SKent Gibson #endif
2545925ca369SKent Gibson };
2546925ca369SKent Gibson
chipinfo_get(struct gpio_chardev_data * cdev,void __user * ip)25472e202ad8SKent Gibson static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
25482e202ad8SKent Gibson {
25492e202ad8SKent Gibson struct gpio_device *gdev = cdev->gdev;
25502e202ad8SKent Gibson struct gpiochip_info chipinfo;
25512e202ad8SKent Gibson
25522e202ad8SKent Gibson memset(&chipinfo, 0, sizeof(chipinfo));
25532e202ad8SKent Gibson
25542e202ad8SKent Gibson strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
25552e202ad8SKent Gibson strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
25562e202ad8SKent Gibson chipinfo.lines = gdev->ngpio;
25572e202ad8SKent Gibson if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
25582e202ad8SKent Gibson return -EFAULT;
25592e202ad8SKent Gibson return 0;
25602e202ad8SKent Gibson }
25612e202ad8SKent Gibson
2562aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2563aad95584SKent Gibson /*
2564aad95584SKent Gibson * returns 0 if the versions match, else the previously selected ABI version
2565aad95584SKent Gibson */
lineinfo_ensure_abi_version(struct gpio_chardev_data * cdata,unsigned int version)2566aad95584SKent Gibson static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
2567aad95584SKent Gibson unsigned int version)
2568aad95584SKent Gibson {
2569aad95584SKent Gibson int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
2570aad95584SKent Gibson
2571aad95584SKent Gibson if (abiv == version)
2572aad95584SKent Gibson return 0;
2573aad95584SKent Gibson
2574aad95584SKent Gibson return abiv;
2575aad95584SKent Gibson }
25762e202ad8SKent Gibson
lineinfo_get_v1(struct gpio_chardev_data * cdev,void __user * ip,bool watch)25772e202ad8SKent Gibson static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
25782e202ad8SKent Gibson bool watch)
25792e202ad8SKent Gibson {
25802e202ad8SKent Gibson struct gpio_desc *desc;
25812e202ad8SKent Gibson struct gpioline_info lineinfo;
25822e202ad8SKent Gibson struct gpio_v2_line_info lineinfo_v2;
25832e202ad8SKent Gibson
25842e202ad8SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
25852e202ad8SKent Gibson return -EFAULT;
25862e202ad8SKent Gibson
25872e202ad8SKent Gibson /* this doubles as a range check on line_offset */
25882e202ad8SKent Gibson desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset);
25892e202ad8SKent Gibson if (IS_ERR(desc))
25902e202ad8SKent Gibson return PTR_ERR(desc);
25912e202ad8SKent Gibson
25922e202ad8SKent Gibson if (watch) {
25932e202ad8SKent Gibson if (lineinfo_ensure_abi_version(cdev, 1))
25942e202ad8SKent Gibson return -EPERM;
25952e202ad8SKent Gibson
25962e202ad8SKent Gibson if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
25972e202ad8SKent Gibson return -EBUSY;
25982e202ad8SKent Gibson }
25992e202ad8SKent Gibson
26002e202ad8SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo_v2);
26012e202ad8SKent Gibson gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
26022e202ad8SKent Gibson
26032e202ad8SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
26042e202ad8SKent Gibson if (watch)
26052e202ad8SKent Gibson clear_bit(lineinfo.line_offset, cdev->watched_lines);
26062e202ad8SKent Gibson return -EFAULT;
26072e202ad8SKent Gibson }
26082e202ad8SKent Gibson
26092e202ad8SKent Gibson return 0;
26102e202ad8SKent Gibson }
2611aad95584SKent Gibson #endif
2612aad95584SKent Gibson
lineinfo_get(struct gpio_chardev_data * cdev,void __user * ip,bool watch)2613aad95584SKent Gibson static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
2614aad95584SKent Gibson bool watch)
2615aad95584SKent Gibson {
2616aad95584SKent Gibson struct gpio_desc *desc;
2617aad95584SKent Gibson struct gpio_v2_line_info lineinfo;
2618aad95584SKent Gibson
2619aad95584SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2620aad95584SKent Gibson return -EFAULT;
2621aad95584SKent Gibson
2622aad95584SKent Gibson if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2623aad95584SKent Gibson return -EINVAL;
2624aad95584SKent Gibson
2625aad95584SKent Gibson desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2626aad95584SKent Gibson if (IS_ERR(desc))
2627aad95584SKent Gibson return PTR_ERR(desc);
2628aad95584SKent Gibson
2629aad95584SKent Gibson if (watch) {
2630aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2631aad95584SKent Gibson if (lineinfo_ensure_abi_version(cdev, 2))
2632aad95584SKent Gibson return -EPERM;
2633aad95584SKent Gibson #endif
2634aad95584SKent Gibson if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2635aad95584SKent Gibson return -EBUSY;
2636aad95584SKent Gibson }
2637aad95584SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo);
2638309364bcSKent Gibson supinfo_to_lineinfo(desc, &lineinfo);
2639aad95584SKent Gibson
2640aad95584SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2641aad95584SKent Gibson if (watch)
2642aad95584SKent Gibson clear_bit(lineinfo.offset, cdev->watched_lines);
2643aad95584SKent Gibson return -EFAULT;
2644aad95584SKent Gibson }
2645aad95584SKent Gibson
2646aad95584SKent Gibson return 0;
2647aad95584SKent Gibson }
2648aad95584SKent Gibson
lineinfo_unwatch(struct gpio_chardev_data * cdev,void __user * ip)26492e202ad8SKent Gibson static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
2650925ca369SKent Gibson {
2651925ca369SKent Gibson __u32 offset;
2652925ca369SKent Gibson
2653925ca369SKent Gibson if (copy_from_user(&offset, ip, sizeof(offset)))
2654925ca369SKent Gibson return -EFAULT;
2655925ca369SKent Gibson
26561bf7ba40SKent Gibson if (offset >= cdev->gdev->ngpio)
26571bf7ba40SKent Gibson return -EINVAL;
2658925ca369SKent Gibson
26591bf7ba40SKent Gibson if (!test_and_clear_bit(offset, cdev->watched_lines))
2660925ca369SKent Gibson return -EBUSY;
2661925ca369SKent Gibson
2662925ca369SKent Gibson return 0;
2663925ca369SKent Gibson }
26642e202ad8SKent Gibson
gpio_ioctl_unlocked(struct file * file,unsigned int cmd,unsigned long arg)2665249cbac4SKent Gibson static long gpio_ioctl_unlocked(struct file *file, unsigned int cmd, unsigned long arg)
26662e202ad8SKent Gibson {
26672e202ad8SKent Gibson struct gpio_chardev_data *cdev = file->private_data;
26682e202ad8SKent Gibson struct gpio_device *gdev = cdev->gdev;
26692e202ad8SKent Gibson void __user *ip = (void __user *)arg;
26702e202ad8SKent Gibson
26712e202ad8SKent Gibson /* We fail any subsequent ioctl():s when the chip is gone */
26722e202ad8SKent Gibson if (!gdev->chip)
26732e202ad8SKent Gibson return -ENODEV;
26742e202ad8SKent Gibson
26752e202ad8SKent Gibson /* Fill in the struct and pass to userspace */
26761cef8b50SAndy Shevchenko switch (cmd) {
26771cef8b50SAndy Shevchenko case GPIO_GET_CHIPINFO_IOCTL:
26782e202ad8SKent Gibson return chipinfo_get(cdev, ip);
26792e202ad8SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
26801cef8b50SAndy Shevchenko case GPIO_GET_LINEHANDLE_IOCTL:
26812e202ad8SKent Gibson return linehandle_create(gdev, ip);
26821cef8b50SAndy Shevchenko case GPIO_GET_LINEEVENT_IOCTL:
26832e202ad8SKent Gibson return lineevent_create(gdev, ip);
26841cef8b50SAndy Shevchenko case GPIO_GET_LINEINFO_IOCTL:
26851cef8b50SAndy Shevchenko return lineinfo_get_v1(cdev, ip, false);
26861cef8b50SAndy Shevchenko case GPIO_GET_LINEINFO_WATCH_IOCTL:
26871cef8b50SAndy Shevchenko return lineinfo_get_v1(cdev, ip, true);
26882e202ad8SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */
26891cef8b50SAndy Shevchenko case GPIO_V2_GET_LINEINFO_IOCTL:
26901cef8b50SAndy Shevchenko return lineinfo_get(cdev, ip, false);
26911cef8b50SAndy Shevchenko case GPIO_V2_GET_LINEINFO_WATCH_IOCTL:
26921cef8b50SAndy Shevchenko return lineinfo_get(cdev, ip, true);
26931cef8b50SAndy Shevchenko case GPIO_V2_GET_LINE_IOCTL:
26942e202ad8SKent Gibson return linereq_create(gdev, ip);
26951cef8b50SAndy Shevchenko case GPIO_GET_LINEINFO_UNWATCH_IOCTL:
26962e202ad8SKent Gibson return lineinfo_unwatch(cdev, ip);
26971cef8b50SAndy Shevchenko default:
2698925ca369SKent Gibson return -EINVAL;
2699925ca369SKent Gibson }
27001cef8b50SAndy Shevchenko }
2701925ca369SKent Gibson
2702249cbac4SKent Gibson /*
2703249cbac4SKent Gibson * gpio_ioctl() - ioctl handler for the GPIO chardev
2704249cbac4SKent Gibson */
gpio_ioctl(struct file * file,unsigned int cmd,unsigned long arg)2705249cbac4SKent Gibson static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2706249cbac4SKent Gibson {
2707249cbac4SKent Gibson struct gpio_chardev_data *cdev = file->private_data;
2708249cbac4SKent Gibson
2709249cbac4SKent Gibson return call_ioctl_locked(file, cmd, arg, cdev->gdev,
2710249cbac4SKent Gibson gpio_ioctl_unlocked);
2711249cbac4SKent Gibson }
2712249cbac4SKent Gibson
2713925ca369SKent Gibson #ifdef CONFIG_COMPAT
gpio_ioctl_compat(struct file * file,unsigned int cmd,unsigned long arg)271449bc5279SKent Gibson static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2715925ca369SKent Gibson unsigned long arg)
2716925ca369SKent Gibson {
271749bc5279SKent Gibson return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2718925ca369SKent Gibson }
2719925ca369SKent Gibson #endif
2720925ca369SKent Gibson
lineinfo_changed_notify(struct notifier_block * nb,unsigned long action,void * data)2721925ca369SKent Gibson static int lineinfo_changed_notify(struct notifier_block *nb,
2722925ca369SKent Gibson unsigned long action, void *data)
2723925ca369SKent Gibson {
2724e82bbd67SBartosz Golaszewski struct gpio_chardev_data *cdev =
2725e82bbd67SBartosz Golaszewski container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2726aad95584SKent Gibson struct gpio_v2_line_info_changed chg;
2727925ca369SKent Gibson struct gpio_desc *desc = data;
2728925ca369SKent Gibson int ret;
2729925ca369SKent Gibson
2730e2b781c5SKent Gibson if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2731925ca369SKent Gibson return NOTIFY_DONE;
2732925ca369SKent Gibson
2733925ca369SKent Gibson memset(&chg, 0, sizeof(chg));
2734925ca369SKent Gibson chg.event_type = action;
2735aad95584SKent Gibson chg.timestamp_ns = ktime_get_ns();
2736925ca369SKent Gibson gpio_desc_to_lineinfo(desc, &chg.info);
2737309364bcSKent Gibson supinfo_to_lineinfo(desc, &chg.info);
2738925ca369SKent Gibson
2739e2b781c5SKent Gibson ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2740925ca369SKent Gibson if (ret)
2741e2b781c5SKent Gibson wake_up_poll(&cdev->wait, EPOLLIN);
2742925ca369SKent Gibson else
2743925ca369SKent Gibson pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2744925ca369SKent Gibson
2745925ca369SKent Gibson return NOTIFY_OK;
2746925ca369SKent Gibson }
2747925ca369SKent Gibson
gpio_device_unregistered_notify(struct notifier_block * nb,unsigned long action,void * data)2748d2e2586aSBartosz Golaszewski static int gpio_device_unregistered_notify(struct notifier_block *nb,
2749d2e2586aSBartosz Golaszewski unsigned long action, void *data)
2750d2e2586aSBartosz Golaszewski {
2751d2e2586aSBartosz Golaszewski struct gpio_chardev_data *cdev = container_of(nb,
2752d2e2586aSBartosz Golaszewski struct gpio_chardev_data,
2753d2e2586aSBartosz Golaszewski device_unregistered_nb);
2754d2e2586aSBartosz Golaszewski
2755d2e2586aSBartosz Golaszewski wake_up_poll(&cdev->wait, EPOLLIN | EPOLLERR);
2756d2e2586aSBartosz Golaszewski
2757d2e2586aSBartosz Golaszewski return NOTIFY_OK;
2758d2e2586aSBartosz Golaszewski }
2759d2e2586aSBartosz Golaszewski
lineinfo_watch_poll_unlocked(struct file * file,struct poll_table_struct * pollt)2760bdbbae24SBartosz Golaszewski static __poll_t lineinfo_watch_poll_unlocked(struct file *file,
2761925ca369SKent Gibson struct poll_table_struct *pollt)
2762925ca369SKent Gibson {
2763e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data;
2764925ca369SKent Gibson __poll_t events = 0;
2765925ca369SKent Gibson
2766533aae7cSBartosz Golaszewski if (!cdev->gdev->chip)
2767533aae7cSBartosz Golaszewski return EPOLLHUP | EPOLLERR;
2768533aae7cSBartosz Golaszewski
2769e2b781c5SKent Gibson poll_wait(file, &cdev->wait, pollt);
2770925ca369SKent Gibson
2771e2b781c5SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2772e2b781c5SKent Gibson &cdev->wait.lock))
2773925ca369SKent Gibson events = EPOLLIN | EPOLLRDNORM;
2774925ca369SKent Gibson
2775925ca369SKent Gibson return events;
2776925ca369SKent Gibson }
2777925ca369SKent Gibson
lineinfo_watch_poll(struct file * file,struct poll_table_struct * pollt)2778bdbbae24SBartosz Golaszewski static __poll_t lineinfo_watch_poll(struct file *file,
2779bdbbae24SBartosz Golaszewski struct poll_table_struct *pollt)
2780bdbbae24SBartosz Golaszewski {
2781bdbbae24SBartosz Golaszewski struct gpio_chardev_data *cdev = file->private_data;
2782bdbbae24SBartosz Golaszewski
2783bdbbae24SBartosz Golaszewski return call_poll_locked(file, pollt, cdev->gdev,
2784bdbbae24SBartosz Golaszewski lineinfo_watch_poll_unlocked);
2785bdbbae24SBartosz Golaszewski }
2786bdbbae24SBartosz Golaszewski
lineinfo_watch_read_unlocked(struct file * file,char __user * buf,size_t count,loff_t * off)2787bdbbae24SBartosz Golaszewski static ssize_t lineinfo_watch_read_unlocked(struct file *file, char __user *buf,
2788925ca369SKent Gibson size_t count, loff_t *off)
2789925ca369SKent Gibson {
2790e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data;
2791aad95584SKent Gibson struct gpio_v2_line_info_changed event;
2792925ca369SKent Gibson ssize_t bytes_read = 0;
2793925ca369SKent Gibson int ret;
2794aad95584SKent Gibson size_t event_size;
2795925ca369SKent Gibson
2796533aae7cSBartosz Golaszewski if (!cdev->gdev->chip)
2797533aae7cSBartosz Golaszewski return -ENODEV;
2798533aae7cSBartosz Golaszewski
2799aad95584SKent Gibson #ifndef CONFIG_GPIO_CDEV_V1
2800aad95584SKent Gibson event_size = sizeof(struct gpio_v2_line_info_changed);
2801aad95584SKent Gibson if (count < event_size)
2802925ca369SKent Gibson return -EINVAL;
2803aad95584SKent Gibson #endif
2804925ca369SKent Gibson
2805925ca369SKent Gibson do {
2806e2b781c5SKent Gibson spin_lock(&cdev->wait.lock);
2807e2b781c5SKent Gibson if (kfifo_is_empty(&cdev->events)) {
2808925ca369SKent Gibson if (bytes_read) {
2809e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock);
2810925ca369SKent Gibson return bytes_read;
2811925ca369SKent Gibson }
2812925ca369SKent Gibson
281349bc5279SKent Gibson if (file->f_flags & O_NONBLOCK) {
2814e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock);
2815925ca369SKent Gibson return -EAGAIN;
2816925ca369SKent Gibson }
2817925ca369SKent Gibson
2818e2b781c5SKent Gibson ret = wait_event_interruptible_locked(cdev->wait,
2819e2b781c5SKent Gibson !kfifo_is_empty(&cdev->events));
2820925ca369SKent Gibson if (ret) {
2821e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock);
2822925ca369SKent Gibson return ret;
2823925ca369SKent Gibson }
2824925ca369SKent Gibson }
2825aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2826aad95584SKent Gibson /* must be after kfifo check so watch_abi_version is set */
2827aad95584SKent Gibson if (atomic_read(&cdev->watch_abi_version) == 2)
2828aad95584SKent Gibson event_size = sizeof(struct gpio_v2_line_info_changed);
2829aad95584SKent Gibson else
2830aad95584SKent Gibson event_size = sizeof(struct gpioline_info_changed);
2831aad95584SKent Gibson if (count < event_size) {
2832aad95584SKent Gibson spin_unlock(&cdev->wait.lock);
2833aad95584SKent Gibson return -EINVAL;
2834aad95584SKent Gibson }
2835aad95584SKent Gibson #endif
2836e2b781c5SKent Gibson ret = kfifo_out(&cdev->events, &event, 1);
2837e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock);
2838925ca369SKent Gibson if (ret != 1) {
2839925ca369SKent Gibson ret = -EIO;
2840925ca369SKent Gibson break;
2841925ca369SKent Gibson /* We should never get here. See lineevent_read(). */
2842925ca369SKent Gibson }
2843925ca369SKent Gibson
2844aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1
2845aad95584SKent Gibson if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2846aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event, event_size))
2847925ca369SKent Gibson return -EFAULT;
2848aad95584SKent Gibson } else {
2849aad95584SKent Gibson struct gpioline_info_changed event_v1;
2850aad95584SKent Gibson
2851aad95584SKent Gibson gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2852aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event_v1,
2853aad95584SKent Gibson event_size))
2854aad95584SKent Gibson return -EFAULT;
2855aad95584SKent Gibson }
2856aad95584SKent Gibson #else
2857aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event, event_size))
2858aad95584SKent Gibson return -EFAULT;
2859aad95584SKent Gibson #endif
2860aad95584SKent Gibson bytes_read += event_size;
2861925ca369SKent Gibson } while (count >= bytes_read + sizeof(event));
2862925ca369SKent Gibson
2863925ca369SKent Gibson return bytes_read;
2864925ca369SKent Gibson }
2865925ca369SKent Gibson
lineinfo_watch_read(struct file * file,char __user * buf,size_t count,loff_t * off)2866bdbbae24SBartosz Golaszewski static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2867bdbbae24SBartosz Golaszewski size_t count, loff_t *off)
2868bdbbae24SBartosz Golaszewski {
2869bdbbae24SBartosz Golaszewski struct gpio_chardev_data *cdev = file->private_data;
2870bdbbae24SBartosz Golaszewski
2871bdbbae24SBartosz Golaszewski return call_read_locked(file, buf, count, off, cdev->gdev,
2872bdbbae24SBartosz Golaszewski lineinfo_watch_read_unlocked);
2873bdbbae24SBartosz Golaszewski }
2874bdbbae24SBartosz Golaszewski
2875925ca369SKent Gibson /**
2876925ca369SKent Gibson * gpio_chrdev_open() - open the chardev for ioctl operations
2877925ca369SKent Gibson * @inode: inode for this chardev
287849bc5279SKent Gibson * @file: file struct for storing private data
2879925ca369SKent Gibson * Returns 0 on success
2880925ca369SKent Gibson */
gpio_chrdev_open(struct inode * inode,struct file * file)288149bc5279SKent Gibson static int gpio_chrdev_open(struct inode *inode, struct file *file)
2882925ca369SKent Gibson {
2883925ca369SKent Gibson struct gpio_device *gdev = container_of(inode->i_cdev,
2884925ca369SKent Gibson struct gpio_device, chrdev);
2885e2b781c5SKent Gibson struct gpio_chardev_data *cdev;
2886925ca369SKent Gibson int ret = -ENOMEM;
2887925ca369SKent Gibson
2888bdbbae24SBartosz Golaszewski down_read(&gdev->sem);
2889bdbbae24SBartosz Golaszewski
2890925ca369SKent Gibson /* Fail on open if the backing gpiochip is gone */
2891bdbbae24SBartosz Golaszewski if (!gdev->chip) {
2892bdbbae24SBartosz Golaszewski ret = -ENODEV;
2893bdbbae24SBartosz Golaszewski goto out_unlock;
2894bdbbae24SBartosz Golaszewski }
2895925ca369SKent Gibson
2896e2b781c5SKent Gibson cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2897e2b781c5SKent Gibson if (!cdev)
2898bdbbae24SBartosz Golaszewski goto out_unlock;
2899925ca369SKent Gibson
2900e2b781c5SKent Gibson cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2901e2b781c5SKent Gibson if (!cdev->watched_lines)
2902e2b781c5SKent Gibson goto out_free_cdev;
2903925ca369SKent Gibson
2904e2b781c5SKent Gibson init_waitqueue_head(&cdev->wait);
2905e2b781c5SKent Gibson INIT_KFIFO(cdev->events);
2906dc0989e3SAndy Shevchenko cdev->gdev = gpio_device_get(gdev);
2907925ca369SKent Gibson
2908e2b781c5SKent Gibson cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
290917a7ca35SBartosz Golaszewski ret = blocking_notifier_chain_register(&gdev->line_state_notifier,
2910e2b781c5SKent Gibson &cdev->lineinfo_changed_nb);
2911925ca369SKent Gibson if (ret)
2912925ca369SKent Gibson goto out_free_bitmap;
2913925ca369SKent Gibson
2914d2e2586aSBartosz Golaszewski cdev->device_unregistered_nb.notifier_call =
2915d2e2586aSBartosz Golaszewski gpio_device_unregistered_notify;
2916d2e2586aSBartosz Golaszewski ret = blocking_notifier_chain_register(&gdev->device_notifier,
2917d2e2586aSBartosz Golaszewski &cdev->device_unregistered_nb);
2918d2e2586aSBartosz Golaszewski if (ret)
2919d2e2586aSBartosz Golaszewski goto out_unregister_line_notifier;
2920d2e2586aSBartosz Golaszewski
2921e2b781c5SKent Gibson file->private_data = cdev;
2922925ca369SKent Gibson
292349bc5279SKent Gibson ret = nonseekable_open(inode, file);
2924925ca369SKent Gibson if (ret)
2925d2e2586aSBartosz Golaszewski goto out_unregister_device_notifier;
2926925ca369SKent Gibson
2927bdbbae24SBartosz Golaszewski up_read(&gdev->sem);
2928bdbbae24SBartosz Golaszewski
2929925ca369SKent Gibson return ret;
2930925ca369SKent Gibson
2931d2e2586aSBartosz Golaszewski out_unregister_device_notifier:
2932d2e2586aSBartosz Golaszewski blocking_notifier_chain_unregister(&gdev->device_notifier,
2933d2e2586aSBartosz Golaszewski &cdev->device_unregistered_nb);
2934d2e2586aSBartosz Golaszewski out_unregister_line_notifier:
293517a7ca35SBartosz Golaszewski blocking_notifier_chain_unregister(&gdev->line_state_notifier,
2936e2b781c5SKent Gibson &cdev->lineinfo_changed_nb);
2937925ca369SKent Gibson out_free_bitmap:
2938dc0989e3SAndy Shevchenko gpio_device_put(gdev);
2939e2b781c5SKent Gibson bitmap_free(cdev->watched_lines);
2940e2b781c5SKent Gibson out_free_cdev:
2941e2b781c5SKent Gibson kfree(cdev);
2942bdbbae24SBartosz Golaszewski out_unlock:
2943bdbbae24SBartosz Golaszewski up_read(&gdev->sem);
2944925ca369SKent Gibson return ret;
2945925ca369SKent Gibson }
2946925ca369SKent Gibson
2947925ca369SKent Gibson /**
2948925ca369SKent Gibson * gpio_chrdev_release() - close chardev after ioctl operations
2949925ca369SKent Gibson * @inode: inode for this chardev
295049bc5279SKent Gibson * @file: file struct for storing private data
2951925ca369SKent Gibson * Returns 0 on success
2952925ca369SKent Gibson */
gpio_chrdev_release(struct inode * inode,struct file * file)295349bc5279SKent Gibson static int gpio_chrdev_release(struct inode *inode, struct file *file)
2954925ca369SKent Gibson {
2955e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data;
2956e2b781c5SKent Gibson struct gpio_device *gdev = cdev->gdev;
2957925ca369SKent Gibson
2958d2e2586aSBartosz Golaszewski blocking_notifier_chain_unregister(&gdev->device_notifier,
2959d2e2586aSBartosz Golaszewski &cdev->device_unregistered_nb);
296017a7ca35SBartosz Golaszewski blocking_notifier_chain_unregister(&gdev->line_state_notifier,
2961e2b781c5SKent Gibson &cdev->lineinfo_changed_nb);
296295ca7c90SZhongqiu Han bitmap_free(cdev->watched_lines);
2963dc0989e3SAndy Shevchenko gpio_device_put(gdev);
2964e2b781c5SKent Gibson kfree(cdev);
2965925ca369SKent Gibson
2966925ca369SKent Gibson return 0;
2967925ca369SKent Gibson }
2968925ca369SKent Gibson
2969925ca369SKent Gibson static const struct file_operations gpio_fileops = {
2970925ca369SKent Gibson .release = gpio_chrdev_release,
2971925ca369SKent Gibson .open = gpio_chrdev_open,
2972925ca369SKent Gibson .poll = lineinfo_watch_poll,
2973925ca369SKent Gibson .read = lineinfo_watch_read,
2974925ca369SKent Gibson .owner = THIS_MODULE,
2975925ca369SKent Gibson .llseek = no_llseek,
2976925ca369SKent Gibson .unlocked_ioctl = gpio_ioctl,
2977925ca369SKent Gibson #ifdef CONFIG_COMPAT
2978925ca369SKent Gibson .compat_ioctl = gpio_ioctl_compat,
2979925ca369SKent Gibson #endif
2980925ca369SKent Gibson };
2981925ca369SKent Gibson
gpiolib_cdev_register(struct gpio_device * gdev,dev_t devt)2982925ca369SKent Gibson int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2983925ca369SKent Gibson {
2984925ca369SKent Gibson int ret;
2985925ca369SKent Gibson
2986925ca369SKent Gibson cdev_init(&gdev->chrdev, &gpio_fileops);
2987925ca369SKent Gibson gdev->chrdev.owner = THIS_MODULE;
2988925ca369SKent Gibson gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2989925ca369SKent Gibson
2990925ca369SKent Gibson ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2991925ca369SKent Gibson if (ret)
2992925ca369SKent Gibson return ret;
2993925ca369SKent Gibson
2994925ca369SKent Gibson chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2995925ca369SKent Gibson MAJOR(devt), gdev->id);
2996925ca369SKent Gibson
2997925ca369SKent Gibson return 0;
2998925ca369SKent Gibson }
2999925ca369SKent Gibson
gpiolib_cdev_unregister(struct gpio_device * gdev)3000925ca369SKent Gibson void gpiolib_cdev_unregister(struct gpio_device *gdev)
3001925ca369SKent Gibson {
3002925ca369SKent Gibson cdev_device_del(&gdev->chrdev, &gdev->dev);
3003a067419bSBartosz Golaszewski blocking_notifier_call_chain(&gdev->device_notifier, 0, NULL);
3004925ca369SKent Gibson }
3005