blob: 2613881a66e662beccd0b8b51f98133ea52114ca [file] [log] [blame]
Kent Gibson925ca362020-06-16 17:36:15 +08001// SPDX-License-Identifier: GPL-2.0
2
Kent Gibsond189f622020-07-08 12:15:45 +08003#include <linux/anon_inodes.h>
Kent Gibson3c0d9c62020-09-28 08:27:54 +08004#include <linux/atomic.h>
Kent Gibson925ca362020-06-16 17:36:15 +08005#include <linux/bitmap.h>
Kent Gibson3c0d9c62020-09-28 08:27:54 +08006#include <linux/build_bug.h>
Kent Gibsond189f622020-07-08 12:15:45 +08007#include <linux/cdev.h>
8#include <linux/compat.h>
Kent Gibson65cff702020-09-28 08:27:59 +08009#include <linux/compiler.h>
Kent Gibson925ca362020-06-16 17:36:15 +080010#include <linux/device.h>
11#include <linux/err.h>
Kent Gibsond189f622020-07-08 12:15:45 +080012#include <linux/file.h>
Kent Gibson925ca362020-06-16 17:36:15 +080013#include <linux/gpio.h>
14#include <linux/gpio/driver.h>
Kent Gibsond189f622020-07-08 12:15:45 +080015#include <linux/interrupt.h>
16#include <linux/irqreturn.h>
17#include <linux/kernel.h>
Kent Gibson925ca362020-06-16 17:36:15 +080018#include <linux/kfifo.h>
Kent Gibsond189f622020-07-08 12:15:45 +080019#include <linux/module.h>
Kent Gibsona54756c2020-09-28 08:27:57 +080020#include <linux/mutex.h>
Kent Gibsond189f622020-07-08 12:15:45 +080021#include <linux/pinctrl/consumer.h>
Kent Gibson925ca362020-06-16 17:36:15 +080022#include <linux/poll.h>
Kent Gibsond189f622020-07-08 12:15:45 +080023#include <linux/spinlock.h>
Kent Gibson925ca362020-06-16 17:36:15 +080024#include <linux/timekeeping.h>
Kent Gibsond189f622020-07-08 12:15:45 +080025#include <linux/uaccess.h>
Kent Gibson65cff702020-09-28 08:27:59 +080026#include <linux/workqueue.h>
Kent Gibson925ca362020-06-16 17:36:15 +080027#include <uapi/linux/gpio.h>
28
29#include "gpiolib.h"
30#include "gpiolib-cdev.h"
31
Kent Gibson3c0d9c62020-09-28 08:27:54 +080032/*
33 * Array sizes must ensure 64-bit alignment and not create holes in the
34 * struct packing.
35 */
36static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2));
37static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8));
38
39/*
40 * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility
41 */
42static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8));
43static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8));
44static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8));
45static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8));
46static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8));
47static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8));
48static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8));
49static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8));
50
Kent Gibson925ca362020-06-16 17:36:15 +080051/* Character device interface to GPIO.
52 *
53 * The GPIO character device, /dev/gpiochipN, provides userspace an
54 * interface to gpiolib GPIOs via ioctl()s.
55 */
56
57/*
58 * GPIO line handle management
59 */
60
Kent Gibson3c0d9c62020-09-28 08:27:54 +080061#ifdef CONFIG_GPIO_CDEV_V1
Kent Gibson925ca362020-06-16 17:36:15 +080062/**
63 * struct linehandle_state - contains the state of a userspace handle
64 * @gdev: the GPIO device the handle pertains to
65 * @label: consumer label used to tag descriptors
66 * @descs: the GPIO descriptors held by this handle
Kent Gibson52b7b592020-07-08 12:15:49 +080067 * @num_descs: the number of descriptors held in the descs array
Kent Gibson925ca362020-06-16 17:36:15 +080068 */
69struct linehandle_state {
70 struct gpio_device *gdev;
71 const char *label;
72 struct gpio_desc *descs[GPIOHANDLES_MAX];
Kent Gibson52b7b592020-07-08 12:15:49 +080073 u32 num_descs;
Kent Gibson925ca362020-06-16 17:36:15 +080074};
75
76#define GPIOHANDLE_REQUEST_VALID_FLAGS \
77 (GPIOHANDLE_REQUEST_INPUT | \
78 GPIOHANDLE_REQUEST_OUTPUT | \
79 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
80 GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
81 GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
82 GPIOHANDLE_REQUEST_BIAS_DISABLE | \
83 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
84 GPIOHANDLE_REQUEST_OPEN_SOURCE)
85
86static int linehandle_validate_flags(u32 flags)
87{
88 /* Return an error if an unknown flag is set */
89 if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
90 return -EINVAL;
91
92 /*
93 * Do not allow both INPUT & OUTPUT flags to be set as they are
94 * contradictory.
95 */
96 if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
97 (flags & GPIOHANDLE_REQUEST_OUTPUT))
98 return -EINVAL;
99
100 /*
101 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
102 * the hardware actually supports enabling both at the same time the
103 * electrical result would be disastrous.
104 */
105 if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
106 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
107 return -EINVAL;
108
109 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
110 if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
111 ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
112 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
113 return -EINVAL;
114
115 /* Bias flags only allowed for input or output mode. */
116 if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
117 (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
118 ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
119 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
120 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
121 return -EINVAL;
122
123 /* Only one bias flag can be set. */
124 if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
125 (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
Kent Gibsona18512e2020-07-08 12:15:46 +0800126 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
Kent Gibson925ca362020-06-16 17:36:15 +0800127 ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
128 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
129 return -EINVAL;
130
131 return 0;
132}
133
Kent Gibsonc274b582020-07-08 12:15:47 +0800134static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
135{
136 assign_bit(FLAG_ACTIVE_LOW, flagsp,
137 lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
138 assign_bit(FLAG_OPEN_DRAIN, flagsp,
139 lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
140 assign_bit(FLAG_OPEN_SOURCE, flagsp,
141 lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
142 assign_bit(FLAG_PULL_UP, flagsp,
143 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
144 assign_bit(FLAG_PULL_DOWN, flagsp,
145 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
146 assign_bit(FLAG_BIAS_DISABLE, flagsp,
147 lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
148}
149
Kent Gibson925ca362020-06-16 17:36:15 +0800150static long linehandle_set_config(struct linehandle_state *lh,
151 void __user *ip)
152{
153 struct gpiohandle_config gcnf;
154 struct gpio_desc *desc;
155 int i, ret;
156 u32 lflags;
Kent Gibson925ca362020-06-16 17:36:15 +0800157
158 if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
159 return -EFAULT;
160
161 lflags = gcnf.flags;
162 ret = linehandle_validate_flags(lflags);
163 if (ret)
164 return ret;
165
Kent Gibson52b7b592020-07-08 12:15:49 +0800166 for (i = 0; i < lh->num_descs; i++) {
Kent Gibson925ca362020-06-16 17:36:15 +0800167 desc = lh->descs[i];
Kent Gibsonc274b582020-07-08 12:15:47 +0800168 linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags);
Kent Gibson925ca362020-06-16 17:36:15 +0800169
170 /*
171 * Lines have to be requested explicitly for input
172 * or output, else the line will be treated "as is".
173 */
174 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
175 int val = !!gcnf.default_values[i];
176
177 ret = gpiod_direction_output(desc, val);
178 if (ret)
179 return ret;
180 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
181 ret = gpiod_direction_input(desc);
182 if (ret)
183 return ret;
184 }
185
Kent Gibson6accc372020-07-08 12:15:51 +0800186 blocking_notifier_call_chain(&desc->gdev->notifier,
Kent Gibsonaad95582020-09-28 08:27:55 +0800187 GPIO_V2_LINE_CHANGED_CONFIG,
188 desc);
Kent Gibson925ca362020-06-16 17:36:15 +0800189 }
190 return 0;
191}
192
Kent Gibson49bc5272020-07-08 12:15:48 +0800193static long linehandle_ioctl(struct file *file, unsigned int cmd,
Kent Gibson925ca362020-06-16 17:36:15 +0800194 unsigned long arg)
195{
Kent Gibson49bc5272020-07-08 12:15:48 +0800196 struct linehandle_state *lh = file->private_data;
Kent Gibson925ca362020-06-16 17:36:15 +0800197 void __user *ip = (void __user *)arg;
198 struct gpiohandle_data ghd;
199 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
200 int i;
201
202 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
203 /* NOTE: It's ok to read values of output lines. */
204 int ret = gpiod_get_array_value_complex(false,
205 true,
Kent Gibson52b7b592020-07-08 12:15:49 +0800206 lh->num_descs,
Kent Gibson925ca362020-06-16 17:36:15 +0800207 lh->descs,
208 NULL,
209 vals);
210 if (ret)
211 return ret;
212
213 memset(&ghd, 0, sizeof(ghd));
Kent Gibson52b7b592020-07-08 12:15:49 +0800214 for (i = 0; i < lh->num_descs; i++)
Kent Gibson925ca362020-06-16 17:36:15 +0800215 ghd.values[i] = test_bit(i, vals);
216
217 if (copy_to_user(ip, &ghd, sizeof(ghd)))
218 return -EFAULT;
219
220 return 0;
221 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
222 /*
223 * All line descriptors were created at once with the same
224 * flags so just check if the first one is really output.
225 */
226 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
227 return -EPERM;
228
229 if (copy_from_user(&ghd, ip, sizeof(ghd)))
230 return -EFAULT;
231
232 /* Clamp all values to [0,1] */
Kent Gibson52b7b592020-07-08 12:15:49 +0800233 for (i = 0; i < lh->num_descs; i++)
Kent Gibson925ca362020-06-16 17:36:15 +0800234 __assign_bit(i, vals, ghd.values[i]);
235
236 /* Reuse the array setting function */
237 return gpiod_set_array_value_complex(false,
Kent Gibsona18512e2020-07-08 12:15:46 +0800238 true,
Kent Gibson52b7b592020-07-08 12:15:49 +0800239 lh->num_descs,
Kent Gibsona18512e2020-07-08 12:15:46 +0800240 lh->descs,
241 NULL,
242 vals);
Kent Gibson925ca362020-06-16 17:36:15 +0800243 } else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) {
244 return linehandle_set_config(lh, ip);
245 }
246 return -EINVAL;
247}
248
249#ifdef CONFIG_COMPAT
Kent Gibson49bc5272020-07-08 12:15:48 +0800250static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
Kent Gibsona18512e2020-07-08 12:15:46 +0800251 unsigned long arg)
Kent Gibson925ca362020-06-16 17:36:15 +0800252{
Kent Gibson49bc5272020-07-08 12:15:48 +0800253 return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
Kent Gibson925ca362020-06-16 17:36:15 +0800254}
255#endif
256
Kent Gibson883f9192020-07-08 12:15:55 +0800257static void linehandle_free(struct linehandle_state *lh)
Kent Gibson925ca362020-06-16 17:36:15 +0800258{
Kent Gibson925ca362020-06-16 17:36:15 +0800259 int i;
260
Kent Gibson52b7b592020-07-08 12:15:49 +0800261 for (i = 0; i < lh->num_descs; i++)
Kent Gibson883f9192020-07-08 12:15:55 +0800262 if (lh->descs[i])
263 gpiod_free(lh->descs[i]);
Kent Gibson925ca362020-06-16 17:36:15 +0800264 kfree(lh->label);
Kent Gibson883f9192020-07-08 12:15:55 +0800265 put_device(&lh->gdev->dev);
Kent Gibson925ca362020-06-16 17:36:15 +0800266 kfree(lh);
Kent Gibson883f9192020-07-08 12:15:55 +0800267}
268
269static int linehandle_release(struct inode *inode, struct file *file)
270{
271 linehandle_free(file->private_data);
Kent Gibson925ca362020-06-16 17:36:15 +0800272 return 0;
273}
274
275static const struct file_operations linehandle_fileops = {
276 .release = linehandle_release,
277 .owner = THIS_MODULE,
278 .llseek = noop_llseek,
279 .unlocked_ioctl = linehandle_ioctl,
280#ifdef CONFIG_COMPAT
281 .compat_ioctl = linehandle_ioctl_compat,
282#endif
283};
284
285static int linehandle_create(struct gpio_device *gdev, void __user *ip)
286{
287 struct gpiohandle_request handlereq;
288 struct linehandle_state *lh;
289 struct file *file;
Kent Gibson883f9192020-07-08 12:15:55 +0800290 int fd, i, ret;
Kent Gibson925ca362020-06-16 17:36:15 +0800291 u32 lflags;
292
293 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
294 return -EFAULT;
295 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
296 return -EINVAL;
297
298 lflags = handlereq.flags;
299
300 ret = linehandle_validate_flags(lflags);
301 if (ret)
302 return ret;
303
304 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
305 if (!lh)
306 return -ENOMEM;
307 lh->gdev = gdev;
308 get_device(&gdev->dev);
309
Kent Gibsonf188ac12020-10-05 15:02:46 +0800310 if (handlereq.consumer_label[0] != '\0') {
311 /* label is only initialized if consumer_label is set */
312 lh->label = kstrndup(handlereq.consumer_label,
313 sizeof(handlereq.consumer_label) - 1,
314 GFP_KERNEL);
Kent Gibson925ca362020-06-16 17:36:15 +0800315 if (!lh->label) {
316 ret = -ENOMEM;
317 goto out_free_lh;
318 }
319 }
320
Kent Gibson883f9192020-07-08 12:15:55 +0800321 lh->num_descs = handlereq.lines;
322
Kent Gibson925ca362020-06-16 17:36:15 +0800323 /* Request each GPIO */
324 for (i = 0; i < handlereq.lines; i++) {
325 u32 offset = handlereq.lineoffsets[i];
326 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
327
328 if (IS_ERR(desc)) {
329 ret = PTR_ERR(desc);
Kent Gibson883f9192020-07-08 12:15:55 +0800330 goto out_free_lh;
Kent Gibson925ca362020-06-16 17:36:15 +0800331 }
332
333 ret = gpiod_request(desc, lh->label);
334 if (ret)
Kent Gibson883f9192020-07-08 12:15:55 +0800335 goto out_free_lh;
Kent Gibson925ca362020-06-16 17:36:15 +0800336 lh->descs[i] = desc;
Kent Gibsonc274b582020-07-08 12:15:47 +0800337 linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
Kent Gibson925ca362020-06-16 17:36:15 +0800338
339 ret = gpiod_set_transitory(desc, false);
340 if (ret < 0)
Kent Gibson883f9192020-07-08 12:15:55 +0800341 goto out_free_lh;
Kent Gibson925ca362020-06-16 17:36:15 +0800342
343 /*
344 * Lines have to be requested explicitly for input
345 * or output, else the line will be treated "as is".
346 */
347 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
348 int val = !!handlereq.default_values[i];
349
350 ret = gpiod_direction_output(desc, val);
351 if (ret)
Kent Gibson883f9192020-07-08 12:15:55 +0800352 goto out_free_lh;
Kent Gibson925ca362020-06-16 17:36:15 +0800353 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
354 ret = gpiod_direction_input(desc);
355 if (ret)
Kent Gibson883f9192020-07-08 12:15:55 +0800356 goto out_free_lh;
Kent Gibson925ca362020-06-16 17:36:15 +0800357 }
358
Kent Gibson6accc372020-07-08 12:15:51 +0800359 blocking_notifier_call_chain(&desc->gdev->notifier,
Kent Gibsonaad95582020-09-28 08:27:55 +0800360 GPIO_V2_LINE_CHANGED_REQUESTED, desc);
Kent Gibson925ca362020-06-16 17:36:15 +0800361
362 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
363 offset);
364 }
Kent Gibson925ca362020-06-16 17:36:15 +0800365
366 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
367 if (fd < 0) {
368 ret = fd;
Kent Gibson883f9192020-07-08 12:15:55 +0800369 goto out_free_lh;
Kent Gibson925ca362020-06-16 17:36:15 +0800370 }
371
372 file = anon_inode_getfile("gpio-linehandle",
373 &linehandle_fileops,
374 lh,
375 O_RDONLY | O_CLOEXEC);
376 if (IS_ERR(file)) {
377 ret = PTR_ERR(file);
378 goto out_put_unused_fd;
379 }
380
381 handlereq.fd = fd;
382 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
383 /*
384 * fput() will trigger the release() callback, so do not go onto
385 * the regular error cleanup path here.
386 */
387 fput(file);
388 put_unused_fd(fd);
389 return -EFAULT;
390 }
391
392 fd_install(fd, file);
393
394 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
Kent Gibson52b7b592020-07-08 12:15:49 +0800395 lh->num_descs);
Kent Gibson925ca362020-06-16 17:36:15 +0800396
397 return 0;
398
399out_put_unused_fd:
400 put_unused_fd(fd);
Kent Gibson925ca362020-06-16 17:36:15 +0800401out_free_lh:
Kent Gibson883f9192020-07-08 12:15:55 +0800402 linehandle_free(lh);
Kent Gibson925ca362020-06-16 17:36:15 +0800403 return ret;
404}
Kent Gibson3c0d9c62020-09-28 08:27:54 +0800405#endif /* CONFIG_GPIO_CDEV_V1 */
406
407/**
408 * struct line - contains the state of a requested line
409 * @desc: the GPIO descriptor for this line.
Kent Gibson73e03412020-09-28 08:27:56 +0800410 * @req: the corresponding line request
411 * @irq: the interrupt triggered in response to events on this GPIO
412 * @eflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or
413 * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied
414 * @timestamp_ns: cache for the timestamp storing it between hardirq and
415 * IRQ thread, used to bring the timestamp close to the actual event
416 * @req_seqno: the seqno for the current edge event in the sequence of
417 * events for the corresponding line request. This is drawn from the @req.
418 * @line_seqno: the seqno for the current edge event in the sequence of
419 * events for this line.
Kent Gibson65cff702020-09-28 08:27:59 +0800420 * @work: the worker that implements software debouncing
421 * @sw_debounced: flag indicating if the software debouncer is active
422 * @level: the current debounced physical level of the line
Kent Gibson3c0d9c62020-09-28 08:27:54 +0800423 */
424struct line {
425 struct gpio_desc *desc;
Kent Gibson73e03412020-09-28 08:27:56 +0800426 /*
427 * -- edge detector specific fields --
428 */
429 struct linereq *req;
430 unsigned int irq;
431 u64 eflags;
432 /*
433 * timestamp_ns and req_seqno are accessed only by
434 * edge_irq_handler() and edge_irq_thread(), which are themselves
435 * mutually exclusive, so no additional protection is necessary.
436 */
437 u64 timestamp_ns;
438 u32 req_seqno;
Kent Gibson65cff702020-09-28 08:27:59 +0800439 /*
440 * line_seqno is accessed by either edge_irq_thread() or
441 * debounce_work_func(), which are themselves mutually exclusive,
442 * so no additional protection is necessary.
443 */
Kent Gibson73e03412020-09-28 08:27:56 +0800444 u32 line_seqno;
Kent Gibson65cff702020-09-28 08:27:59 +0800445 /*
446 * -- debouncer specific fields --
447 */
448 struct delayed_work work;
449 /*
450 * sw_debounce is accessed by linereq_set_config(), which is the
451 * only setter, and linereq_get_values(), which can live with a
452 * slightly stale value.
453 */
454 unsigned int sw_debounced;
455 /*
456 * level is accessed by debounce_work_func(), which is the only
457 * setter, and linereq_get_values() which can live with a slightly
458 * stale value.
459 */
460 unsigned int level;
Kent Gibson3c0d9c62020-09-28 08:27:54 +0800461};
462
463/**
464 * struct linereq - contains the state of a userspace line request
465 * @gdev: the GPIO device the line request pertains to
466 * @label: consumer label used to tag GPIO descriptors
467 * @num_lines: the number of lines in the lines array
Kent Gibson73e03412020-09-28 08:27:56 +0800468 * @wait: wait queue that handles blocking reads of events
469 * @event_buffer_size: the number of elements allocated in @events
470 * @events: KFIFO for the GPIO events
471 * @seqno: the sequence number for edge events generated on all lines in
472 * this line request. Note that this is not used when @num_lines is 1, as
473 * the line_seqno is then the same and is cheaper to calculate.
Kent Gibsona54756c2020-09-28 08:27:57 +0800474 * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
475 * of configuration, particularly multi-step accesses to desc flags.
Kent Gibson3c0d9c62020-09-28 08:27:54 +0800476 * @lines: the lines held by this line request, with @num_lines elements.
477 */
478struct linereq {
479 struct gpio_device *gdev;
480 const char *label;
481 u32 num_lines;
Kent Gibson73e03412020-09-28 08:27:56 +0800482 wait_queue_head_t wait;
483 u32 event_buffer_size;
484 DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
485 atomic_t seqno;
Kent Gibsona54756c2020-09-28 08:27:57 +0800486 struct mutex config_mutex;
Kent Gibson3c0d9c62020-09-28 08:27:54 +0800487 struct line lines[];
488};
489
490#define GPIO_V2_LINE_BIAS_FLAGS \
491 (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
492 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
493 GPIO_V2_LINE_FLAG_BIAS_DISABLED)
494
495#define GPIO_V2_LINE_DIRECTION_FLAGS \
496 (GPIO_V2_LINE_FLAG_INPUT | \
497 GPIO_V2_LINE_FLAG_OUTPUT)
498
499#define GPIO_V2_LINE_DRIVE_FLAGS \
500 (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
501 GPIO_V2_LINE_FLAG_OPEN_SOURCE)
502
Kent Gibson73e03412020-09-28 08:27:56 +0800503#define GPIO_V2_LINE_EDGE_FLAGS \
504 (GPIO_V2_LINE_FLAG_EDGE_RISING | \
505 GPIO_V2_LINE_FLAG_EDGE_FALLING)
506
Kent Gibson3c0d9c62020-09-28 08:27:54 +0800507#define GPIO_V2_LINE_VALID_FLAGS \
508 (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
509 GPIO_V2_LINE_DIRECTION_FLAGS | \
510 GPIO_V2_LINE_DRIVE_FLAGS | \
Kent Gibson73e03412020-09-28 08:27:56 +0800511 GPIO_V2_LINE_EDGE_FLAGS | \
Kent Gibson3c0d9c62020-09-28 08:27:54 +0800512 GPIO_V2_LINE_BIAS_FLAGS)
513
Kent Gibson73e03412020-09-28 08:27:56 +0800514static void linereq_put_event(struct linereq *lr,
515 struct gpio_v2_line_event *le)
516{
517 bool overflow = false;
518
519 spin_lock(&lr->wait.lock);
520 if (kfifo_is_full(&lr->events)) {
521 overflow = true;
522 kfifo_skip(&lr->events);
523 }
524 kfifo_in(&lr->events, le, 1);
525 spin_unlock(&lr->wait.lock);
526 if (!overflow)
527 wake_up_poll(&lr->wait, EPOLLIN);
528 else
529 pr_debug_ratelimited("event FIFO is full - event dropped\n");
530}
531
532static irqreturn_t edge_irq_thread(int irq, void *p)
533{
534 struct line *line = p;
535 struct linereq *lr = line->req;
536 struct gpio_v2_line_event le;
537
538 /* Do not leak kernel stack to userspace */
539 memset(&le, 0, sizeof(le));
540
541 if (line->timestamp_ns) {
542 le.timestamp_ns = line->timestamp_ns;
543 } else {
544 /*
545 * We may be running from a nested threaded interrupt in
546 * which case we didn't get the timestamp from
547 * edge_irq_handler().
548 */
549 le.timestamp_ns = ktime_get_ns();
550 if (lr->num_lines != 1)
551 line->req_seqno = atomic_inc_return(&lr->seqno);
552 }
553 line->timestamp_ns = 0;
554
555 if (line->eflags == (GPIO_V2_LINE_FLAG_EDGE_RISING |
556 GPIO_V2_LINE_FLAG_EDGE_FALLING)) {
557 int level = gpiod_get_value_cansleep(line->desc);
558
559 if (level)
560 /* Emit low-to-high event */
561 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
562 else
563 /* Emit high-to-low event */
564 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
565 } else if (line->eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) {
566 /* Emit low-to-high event */
567 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
568 } else if (line->eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) {
569 /* Emit high-to-low event */
570 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
571 } else {
572 return IRQ_NONE;
573 }
574 line->line_seqno++;
575 le.line_seqno = line->line_seqno;
576 le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
577 le.offset = gpio_chip_hwgpio(line->desc);
578
579 linereq_put_event(lr, &le);
580
581 return IRQ_HANDLED;
582}
583
584static irqreturn_t edge_irq_handler(int irq, void *p)
585{
586 struct line *line = p;
587 struct linereq *lr = line->req;
588
589 /*
590 * Just store the timestamp in hardirq context so we get it as
591 * close in time as possible to the actual event.
592 */
593 line->timestamp_ns = ktime_get_ns();
594
595 if (lr->num_lines != 1)
596 line->req_seqno = atomic_inc_return(&lr->seqno);
597
598 return IRQ_WAKE_THREAD;
599}
600
Kent Gibson65cff702020-09-28 08:27:59 +0800601/*
602 * returns the current debounced logical value.
603 */
604static bool debounced_value(struct line *line)
605{
606 bool value;
607
608 /*
609 * minor race - debouncer may be stopped here, so edge_detector_stop()
610 * must leave the value unchanged so the following will read the level
611 * from when the debouncer was last running.
612 */
613 value = READ_ONCE(line->level);
614
615 if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
616 value = !value;
617
618 return value;
619}
620
621static irqreturn_t debounce_irq_handler(int irq, void *p)
622{
623 struct line *line = p;
624
625 mod_delayed_work(system_wq, &line->work,
626 usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
627
628 return IRQ_HANDLED;
629}
630
631static void debounce_work_func(struct work_struct *work)
632{
633 struct gpio_v2_line_event le;
634 struct line *line = container_of(work, struct line, work.work);
635 struct linereq *lr;
636 int level;
637
638 level = gpiod_get_raw_value_cansleep(line->desc);
639 if (level < 0) {
640 pr_debug_ratelimited("debouncer failed to read line value\n");
641 return;
642 }
643
644 if (READ_ONCE(line->level) == level)
645 return;
646
647 WRITE_ONCE(line->level, level);
648
649 /* -- edge detection -- */
650 if (!line->eflags)
651 return;
652
653 /* switch from physical level to logical - if they differ */
654 if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
655 level = !level;
656
657 /* ignore edges that are not being monitored */
658 if (((line->eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
659 ((line->eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
660 return;
661
662 /* Do not leak kernel stack to userspace */
663 memset(&le, 0, sizeof(le));
664
665 lr = line->req;
666 le.timestamp_ns = ktime_get_ns();
667 le.offset = gpio_chip_hwgpio(line->desc);
668 line->line_seqno++;
669 le.line_seqno = line->line_seqno;
670 le.seqno = (lr->num_lines == 1) ?
671 le.line_seqno : atomic_inc_return(&lr->seqno);
672
673 if (level)
674 /* Emit low-to-high event */
675 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
676 else
677 /* Emit high-to-low event */
678 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
679
680 linereq_put_event(lr, &le);
681}
682
683static int debounce_setup(struct line *line,
684 unsigned int debounce_period_us)
685{
686 unsigned long irqflags;
687 int ret, level, irq;
688
689 /* try hardware */
690 ret = gpiod_set_debounce(line->desc, debounce_period_us);
691 if (!ret) {
692 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
693 return ret;
694 }
695 if (ret != -ENOTSUPP)
696 return ret;
697
698 if (debounce_period_us) {
699 /* setup software debounce */
700 level = gpiod_get_raw_value_cansleep(line->desc);
701 if (level < 0)
702 return level;
703
704 irq = gpiod_to_irq(line->desc);
705 if (irq < 0)
706 return -ENXIO;
707
708 WRITE_ONCE(line->level, level);
709 irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
710 ret = request_irq(irq, debounce_irq_handler, irqflags,
711 line->req->label, line);
712 if (ret)
713 return ret;
714
715 WRITE_ONCE(line->sw_debounced, 1);
716 line->irq = irq;
717 }
718 return 0;
719}
720
721static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
722 unsigned int line_idx)
723{
724 unsigned int i;
725 u64 mask = BIT_ULL(line_idx);
726
727 for (i = 0; i < lc->num_attrs; i++) {
728 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
729 (lc->attrs[i].mask & mask))
730 return true;
731 }
732 return false;
733}
734
735static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
736 unsigned int line_idx)
737{
738 unsigned int i;
739 u64 mask = BIT_ULL(line_idx);
740
741 for (i = 0; i < lc->num_attrs; i++) {
742 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
743 (lc->attrs[i].mask & mask))
744 return lc->attrs[i].attr.debounce_period_us;
745 }
746 return 0;
747}
748
Kent Gibson73e03412020-09-28 08:27:56 +0800749static void edge_detector_stop(struct line *line)
750{
751 if (line->irq) {
752 free_irq(line->irq, line);
753 line->irq = 0;
754 }
Kent Gibsona54756c2020-09-28 08:27:57 +0800755
Kent Gibson65cff702020-09-28 08:27:59 +0800756 cancel_delayed_work_sync(&line->work);
757 WRITE_ONCE(line->sw_debounced, 0);
Kent Gibsona54756c2020-09-28 08:27:57 +0800758 line->eflags = 0;
Kent Gibson3cb83932021-01-21 22:10:38 +0800759 if (line->desc)
760 WRITE_ONCE(line->desc->debounce_period_us, 0);
Kent Gibson65cff702020-09-28 08:27:59 +0800761 /* do not change line->level - see comment in debounced_value() */
Kent Gibson73e03412020-09-28 08:27:56 +0800762}
763
764static int edge_detector_setup(struct line *line,
Kent Gibson65cff702020-09-28 08:27:59 +0800765 struct gpio_v2_line_config *lc,
766 unsigned int line_idx,
Kent Gibson73e03412020-09-28 08:27:56 +0800767 u64 eflags)
768{
Kent Gibson65cff702020-09-28 08:27:59 +0800769 u32 debounce_period_us;
Kent Gibson73e03412020-09-28 08:27:56 +0800770 unsigned long irqflags = 0;
771 int irq, ret;
772
773 if (eflags && !kfifo_initialized(&line->req->events)) {
774 ret = kfifo_alloc(&line->req->events,
775 line->req->event_buffer_size, GFP_KERNEL);
776 if (ret)
777 return ret;
778 }
779 line->eflags = eflags;
Kent Gibson65cff702020-09-28 08:27:59 +0800780 if (gpio_v2_line_config_debounced(lc, line_idx)) {
781 debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
782 ret = debounce_setup(line, debounce_period_us);
783 if (ret)
784 return ret;
785 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
786 }
Kent Gibson73e03412020-09-28 08:27:56 +0800787
Kent Gibson65cff702020-09-28 08:27:59 +0800788 /* detection disabled or sw debouncer will provide edge detection */
789 if (!eflags || READ_ONCE(line->sw_debounced))
Kent Gibson73e03412020-09-28 08:27:56 +0800790 return 0;
791
792 irq = gpiod_to_irq(line->desc);
793 if (irq < 0)
794 return -ENXIO;
795
796 if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
797 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
798 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
799 if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
800 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
801 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
802 irqflags |= IRQF_ONESHOT;
803
804 /* Request a thread to read the events */
805 ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
806 irqflags, line->req->label, line);
807 if (ret)
808 return ret;
809
810 line->irq = irq;
811 return 0;
812}
813
Kent Gibson65cff702020-09-28 08:27:59 +0800814static int edge_detector_update(struct line *line,
815 struct gpio_v2_line_config *lc,
816 unsigned int line_idx,
817 u64 eflags, bool polarity_change)
Kent Gibsona54756c2020-09-28 08:27:57 +0800818{
Kent Gibson65cff702020-09-28 08:27:59 +0800819 unsigned int debounce_period_us =
820 gpio_v2_line_config_debounce_period(lc, line_idx);
821
822 if ((line->eflags == eflags) && !polarity_change &&
823 (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us))
Kent Gibsona54756c2020-09-28 08:27:57 +0800824 return 0;
825
Kent Gibson65cff702020-09-28 08:27:59 +0800826 /* sw debounced and still will be...*/
827 if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
828 line->eflags = eflags;
829 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
830 return 0;
831 }
Kent Gibsona54756c2020-09-28 08:27:57 +0800832
Kent Gibson65cff702020-09-28 08:27:59 +0800833 /* reconfiguring edge detection or sw debounce being disabled */
834 if ((line->irq && !READ_ONCE(line->sw_debounced)) ||
835 (!debounce_period_us && READ_ONCE(line->sw_debounced)))
836 edge_detector_stop(line);
837
838 return edge_detector_setup(line, lc, line_idx, eflags);
Kent Gibsona54756c2020-09-28 08:27:57 +0800839}
840
Kent Gibson3c0d9c62020-09-28 08:27:54 +0800841static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
842 unsigned int line_idx)
843{
844 unsigned int i;
845 u64 mask = BIT_ULL(line_idx);
846
847 for (i = 0; i < lc->num_attrs; i++) {
848 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
849 (lc->attrs[i].mask & mask))
850 return lc->attrs[i].attr.flags;
851 }
852 return lc->flags;
853}
854
855static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
856 unsigned int line_idx)
857{
858 unsigned int i;
859 u64 mask = BIT_ULL(line_idx);
860
861 for (i = 0; i < lc->num_attrs; i++) {
862 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
863 (lc->attrs[i].mask & mask))
864 return !!(lc->attrs[i].attr.values & mask);
865 }
866 return 0;
867}
868
869static int gpio_v2_line_flags_validate(u64 flags)
870{
871 /* Return an error if an unknown flag is set */
872 if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
873 return -EINVAL;
874
875 /*
876 * Do not allow both INPUT and OUTPUT flags to be set as they are
877 * contradictory.
878 */
879 if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
880 (flags & GPIO_V2_LINE_FLAG_OUTPUT))
881 return -EINVAL;
882
Kent Gibson73e03412020-09-28 08:27:56 +0800883 /* Edge detection requires explicit input. */
884 if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
885 !(flags & GPIO_V2_LINE_FLAG_INPUT))
886 return -EINVAL;
887
Kent Gibson3c0d9c62020-09-28 08:27:54 +0800888 /*
889 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
890 * request. If the hardware actually supports enabling both at the
891 * same time the electrical result would be disastrous.
892 */
893 if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
894 (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
895 return -EINVAL;
896
897 /* Drive requires explicit output direction. */
898 if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
899 !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
900 return -EINVAL;
901
902 /* Bias requires explicit direction. */
903 if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
904 !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
905 return -EINVAL;
906
907 /* Only one bias flag can be set. */
908 if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
909 (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
910 GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
911 ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
912 (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
913 return -EINVAL;
914
915 return 0;
916}
917
918static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
919 unsigned int num_lines)
920{
921 unsigned int i;
922 u64 flags;
923 int ret;
924
925 if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
926 return -EINVAL;
927
928 if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
929 return -EINVAL;
930
931 for (i = 0; i < num_lines; i++) {
932 flags = gpio_v2_line_config_flags(lc, i);
933 ret = gpio_v2_line_flags_validate(flags);
934 if (ret)
935 return ret;
Kent Gibson65cff702020-09-28 08:27:59 +0800936
937 /* debounce requires explicit input */
938 if (gpio_v2_line_config_debounced(lc, i) &&
939 !(flags & GPIO_V2_LINE_FLAG_INPUT))
940 return -EINVAL;
Kent Gibson3c0d9c62020-09-28 08:27:54 +0800941 }
942 return 0;
943}
944
945static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
946 unsigned long *flagsp)
947{
948 assign_bit(FLAG_ACTIVE_LOW, flagsp,
949 flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
950
951 if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
952 set_bit(FLAG_IS_OUT, flagsp);
953 else if (flags & GPIO_V2_LINE_FLAG_INPUT)
954 clear_bit(FLAG_IS_OUT, flagsp);
955
Kent Gibson73e03412020-09-28 08:27:56 +0800956 assign_bit(FLAG_EDGE_RISING, flagsp,
957 flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
958 assign_bit(FLAG_EDGE_FALLING, flagsp,
959 flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
960
Kent Gibson3c0d9c62020-09-28 08:27:54 +0800961 assign_bit(FLAG_OPEN_DRAIN, flagsp,
962 flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
963 assign_bit(FLAG_OPEN_SOURCE, flagsp,
964 flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
965
966 assign_bit(FLAG_PULL_UP, flagsp,
967 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
968 assign_bit(FLAG_PULL_DOWN, flagsp,
969 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
970 assign_bit(FLAG_BIAS_DISABLE, flagsp,
971 flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
972}
973
974static long linereq_get_values(struct linereq *lr, void __user *ip)
975{
976 struct gpio_v2_line_values lv;
977 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
978 struct gpio_desc **descs;
979 unsigned int i, didx, num_get;
Kent Gibson65cff702020-09-28 08:27:59 +0800980 bool val;
Kent Gibson3c0d9c62020-09-28 08:27:54 +0800981 int ret;
982
983 /* NOTE: It's ok to read values of output lines. */
984 if (copy_from_user(&lv, ip, sizeof(lv)))
985 return -EFAULT;
986
987 for (num_get = 0, i = 0; i < lr->num_lines; i++) {
988 if (lv.mask & BIT_ULL(i)) {
989 num_get++;
990 descs = &lr->lines[i].desc;
991 }
992 }
993
994 if (num_get == 0)
995 return -EINVAL;
996
997 if (num_get != 1) {
998 descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
999 if (!descs)
1000 return -ENOMEM;
1001 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1002 if (lv.mask & BIT_ULL(i)) {
1003 descs[didx] = lr->lines[i].desc;
1004 didx++;
1005 }
1006 }
1007 }
1008 ret = gpiod_get_array_value_complex(false, true, num_get,
1009 descs, NULL, vals);
1010
1011 if (num_get != 1)
1012 kfree(descs);
1013 if (ret)
1014 return ret;
1015
1016 lv.bits = 0;
1017 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1018 if (lv.mask & BIT_ULL(i)) {
Kent Gibson65cff702020-09-28 08:27:59 +08001019 if (lr->lines[i].sw_debounced)
1020 val = debounced_value(&lr->lines[i]);
1021 else
1022 val = test_bit(didx, vals);
1023 if (val)
Kent Gibson3c0d9c62020-09-28 08:27:54 +08001024 lv.bits |= BIT_ULL(i);
1025 didx++;
1026 }
1027 }
1028
1029 if (copy_to_user(ip, &lv, sizeof(lv)))
1030 return -EFAULT;
1031
1032 return 0;
1033}
1034
Kent Gibson7b8e00d92020-09-28 08:27:58 +08001035static long linereq_set_values_unlocked(struct linereq *lr,
1036 struct gpio_v2_line_values *lv)
1037{
1038 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
1039 struct gpio_desc **descs;
1040 unsigned int i, didx, num_set;
1041 int ret;
1042
1043 bitmap_zero(vals, GPIO_V2_LINES_MAX);
1044 for (num_set = 0, i = 0; i < lr->num_lines; i++) {
1045 if (lv->mask & BIT_ULL(i)) {
1046 if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
1047 return -EPERM;
1048 if (lv->bits & BIT_ULL(i))
1049 __set_bit(num_set, vals);
1050 num_set++;
1051 descs = &lr->lines[i].desc;
1052 }
1053 }
1054 if (num_set == 0)
1055 return -EINVAL;
1056
1057 if (num_set != 1) {
1058 /* build compacted desc array and values */
1059 descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
1060 if (!descs)
1061 return -ENOMEM;
1062 for (didx = 0, i = 0; i < lr->num_lines; i++) {
1063 if (lv->mask & BIT_ULL(i)) {
1064 descs[didx] = lr->lines[i].desc;
1065 didx++;
1066 }
1067 }
1068 }
1069 ret = gpiod_set_array_value_complex(false, true, num_set,
1070 descs, NULL, vals);
1071
1072 if (num_set != 1)
1073 kfree(descs);
1074 return ret;
1075}
1076
1077static long linereq_set_values(struct linereq *lr, void __user *ip)
1078{
1079 struct gpio_v2_line_values lv;
1080 int ret;
1081
1082 if (copy_from_user(&lv, ip, sizeof(lv)))
1083 return -EFAULT;
1084
1085 mutex_lock(&lr->config_mutex);
1086
1087 ret = linereq_set_values_unlocked(lr, &lv);
1088
1089 mutex_unlock(&lr->config_mutex);
1090
1091 return ret;
1092}
1093
Kent Gibsona54756c2020-09-28 08:27:57 +08001094static long linereq_set_config_unlocked(struct linereq *lr,
1095 struct gpio_v2_line_config *lc)
1096{
1097 struct gpio_desc *desc;
1098 unsigned int i;
1099 u64 flags;
1100 bool polarity_change;
1101 int ret;
1102
1103 for (i = 0; i < lr->num_lines; i++) {
1104 desc = lr->lines[i].desc;
1105 flags = gpio_v2_line_config_flags(lc, i);
1106 polarity_change =
1107 (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) !=
1108 ((flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) != 0));
1109
1110 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1111 /*
1112 * Lines have to be requested explicitly for input
1113 * or output, else the line will be treated "as is".
1114 */
1115 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1116 int val = gpio_v2_line_config_output_value(lc, i);
1117
1118 edge_detector_stop(&lr->lines[i]);
1119 ret = gpiod_direction_output(desc, val);
1120 if (ret)
1121 return ret;
1122 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1123 ret = gpiod_direction_input(desc);
1124 if (ret)
1125 return ret;
1126
Kent Gibson65cff702020-09-28 08:27:59 +08001127 ret = edge_detector_update(&lr->lines[i], lc, i,
Kent Gibsona54756c2020-09-28 08:27:57 +08001128 flags & GPIO_V2_LINE_EDGE_FLAGS,
1129 polarity_change);
1130 if (ret)
1131 return ret;
1132 }
1133
1134 blocking_notifier_call_chain(&desc->gdev->notifier,
1135 GPIO_V2_LINE_CHANGED_CONFIG,
1136 desc);
1137 }
1138 return 0;
1139}
1140
1141static long linereq_set_config(struct linereq *lr, void __user *ip)
1142{
1143 struct gpio_v2_line_config lc;
1144 int ret;
1145
1146 if (copy_from_user(&lc, ip, sizeof(lc)))
1147 return -EFAULT;
1148
1149 ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
1150 if (ret)
1151 return ret;
1152
1153 mutex_lock(&lr->config_mutex);
1154
1155 ret = linereq_set_config_unlocked(lr, &lc);
1156
1157 mutex_unlock(&lr->config_mutex);
1158
1159 return ret;
1160}
1161
Kent Gibson3c0d9c62020-09-28 08:27:54 +08001162static long linereq_ioctl(struct file *file, unsigned int cmd,
1163 unsigned long arg)
1164{
1165 struct linereq *lr = file->private_data;
1166 void __user *ip = (void __user *)arg;
1167
1168 if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL)
1169 return linereq_get_values(lr, ip);
Kent Gibson7b8e00d92020-09-28 08:27:58 +08001170 else if (cmd == GPIO_V2_LINE_SET_VALUES_IOCTL)
1171 return linereq_set_values(lr, ip);
Kent Gibsona54756c2020-09-28 08:27:57 +08001172 else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL)
1173 return linereq_set_config(lr, ip);
Kent Gibson3c0d9c62020-09-28 08:27:54 +08001174
1175 return -EINVAL;
1176}
1177
1178#ifdef CONFIG_COMPAT
1179static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
1180 unsigned long arg)
1181{
1182 return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1183}
1184#endif
1185
Kent Gibson73e03412020-09-28 08:27:56 +08001186static __poll_t linereq_poll(struct file *file,
1187 struct poll_table_struct *wait)
1188{
1189 struct linereq *lr = file->private_data;
1190 __poll_t events = 0;
1191
1192 poll_wait(file, &lr->wait, wait);
1193
1194 if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
1195 &lr->wait.lock))
1196 events = EPOLLIN | EPOLLRDNORM;
1197
1198 return events;
1199}
1200
1201static ssize_t linereq_read(struct file *file,
1202 char __user *buf,
1203 size_t count,
1204 loff_t *f_ps)
1205{
1206 struct linereq *lr = file->private_data;
1207 struct gpio_v2_line_event le;
1208 ssize_t bytes_read = 0;
1209 int ret;
1210
1211 if (count < sizeof(le))
1212 return -EINVAL;
1213
1214 do {
1215 spin_lock(&lr->wait.lock);
1216 if (kfifo_is_empty(&lr->events)) {
1217 if (bytes_read) {
1218 spin_unlock(&lr->wait.lock);
1219 return bytes_read;
1220 }
1221
1222 if (file->f_flags & O_NONBLOCK) {
1223 spin_unlock(&lr->wait.lock);
1224 return -EAGAIN;
1225 }
1226
1227 ret = wait_event_interruptible_locked(lr->wait,
1228 !kfifo_is_empty(&lr->events));
1229 if (ret) {
1230 spin_unlock(&lr->wait.lock);
1231 return ret;
1232 }
1233 }
1234
1235 ret = kfifo_out(&lr->events, &le, 1);
1236 spin_unlock(&lr->wait.lock);
1237 if (ret != 1) {
1238 /*
1239 * This should never happen - we were holding the
1240 * lock from the moment we learned the fifo is no
1241 * longer empty until now.
1242 */
1243 ret = -EIO;
1244 break;
1245 }
1246
1247 if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
1248 return -EFAULT;
1249 bytes_read += sizeof(le);
1250 } while (count >= bytes_read + sizeof(le));
1251
1252 return bytes_read;
1253}
1254
Kent Gibson3c0d9c62020-09-28 08:27:54 +08001255static void linereq_free(struct linereq *lr)
1256{
1257 unsigned int i;
1258
1259 for (i = 0; i < lr->num_lines; i++) {
Kent Gibson73e03412020-09-28 08:27:56 +08001260 edge_detector_stop(&lr->lines[i]);
Kent Gibson3c0d9c62020-09-28 08:27:54 +08001261 if (lr->lines[i].desc)
1262 gpiod_free(lr->lines[i].desc);
1263 }
Kent Gibson73e03412020-09-28 08:27:56 +08001264 kfifo_free(&lr->events);
Kent Gibson3c0d9c62020-09-28 08:27:54 +08001265 kfree(lr->label);
1266 put_device(&lr->gdev->dev);
1267 kfree(lr);
1268}
1269
1270static int linereq_release(struct inode *inode, struct file *file)
1271{
1272 struct linereq *lr = file->private_data;
1273
1274 linereq_free(lr);
1275 return 0;
1276}
1277
1278static const struct file_operations line_fileops = {
1279 .release = linereq_release,
Kent Gibson73e03412020-09-28 08:27:56 +08001280 .read = linereq_read,
1281 .poll = linereq_poll,
Kent Gibson3c0d9c62020-09-28 08:27:54 +08001282 .owner = THIS_MODULE,
1283 .llseek = noop_llseek,
1284 .unlocked_ioctl = linereq_ioctl,
1285#ifdef CONFIG_COMPAT
1286 .compat_ioctl = linereq_ioctl_compat,
1287#endif
1288};
1289
1290static int linereq_create(struct gpio_device *gdev, void __user *ip)
1291{
1292 struct gpio_v2_line_request ulr;
1293 struct gpio_v2_line_config *lc;
1294 struct linereq *lr;
1295 struct file *file;
1296 u64 flags;
1297 unsigned int i;
1298 int fd, ret;
1299
1300 if (copy_from_user(&ulr, ip, sizeof(ulr)))
1301 return -EFAULT;
1302
1303 if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
1304 return -EINVAL;
1305
1306 if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
1307 return -EINVAL;
1308
1309 lc = &ulr.config;
1310 ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
1311 if (ret)
1312 return ret;
1313
1314 lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
1315 if (!lr)
1316 return -ENOMEM;
1317
1318 lr->gdev = gdev;
1319 get_device(&gdev->dev);
1320
Kent Gibson65cff702020-09-28 08:27:59 +08001321 for (i = 0; i < ulr.num_lines; i++) {
Kent Gibson73e03412020-09-28 08:27:56 +08001322 lr->lines[i].req = lr;
Kent Gibson65cff702020-09-28 08:27:59 +08001323 WRITE_ONCE(lr->lines[i].sw_debounced, 0);
1324 INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
1325 }
Kent Gibson73e03412020-09-28 08:27:56 +08001326
Kent Gibsonf188ac12020-10-05 15:02:46 +08001327 if (ulr.consumer[0] != '\0') {
Kent Gibson3c0d9c62020-09-28 08:27:54 +08001328 /* label is only initialized if consumer is set */
Kent Gibsonf188ac12020-10-05 15:02:46 +08001329 lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1330 GFP_KERNEL);
Kent Gibson3c0d9c62020-09-28 08:27:54 +08001331 if (!lr->label) {
1332 ret = -ENOMEM;
1333 goto out_free_linereq;
1334 }
1335 }
1336
Kent Gibsona54756c2020-09-28 08:27:57 +08001337 mutex_init(&lr->config_mutex);
Kent Gibson73e03412020-09-28 08:27:56 +08001338 init_waitqueue_head(&lr->wait);
1339 lr->event_buffer_size = ulr.event_buffer_size;
1340 if (lr->event_buffer_size == 0)
1341 lr->event_buffer_size = ulr.num_lines * 16;
1342 else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
1343 lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
1344
1345 atomic_set(&lr->seqno, 0);
Kent Gibson3c0d9c62020-09-28 08:27:54 +08001346 lr->num_lines = ulr.num_lines;
1347
1348 /* Request each GPIO */
1349 for (i = 0; i < ulr.num_lines; i++) {
1350 u32 offset = ulr.offsets[i];
1351 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
1352
1353 if (IS_ERR(desc)) {
1354 ret = PTR_ERR(desc);
1355 goto out_free_linereq;
1356 }
1357
1358 ret = gpiod_request(desc, lr->label);
1359 if (ret)
1360 goto out_free_linereq;
1361
1362 lr->lines[i].desc = desc;
1363 flags = gpio_v2_line_config_flags(lc, i);
1364 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1365
1366 ret = gpiod_set_transitory(desc, false);
1367 if (ret < 0)
1368 goto out_free_linereq;
1369
1370 /*
1371 * Lines have to be requested explicitly for input
1372 * or output, else the line will be treated "as is".
1373 */
1374 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1375 int val = gpio_v2_line_config_output_value(lc, i);
1376
1377 ret = gpiod_direction_output(desc, val);
1378 if (ret)
1379 goto out_free_linereq;
1380 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1381 ret = gpiod_direction_input(desc);
1382 if (ret)
1383 goto out_free_linereq;
Kent Gibson73e03412020-09-28 08:27:56 +08001384
Kent Gibson65cff702020-09-28 08:27:59 +08001385 ret = edge_detector_setup(&lr->lines[i], lc, i,
Kent Gibson73e03412020-09-28 08:27:56 +08001386 flags & GPIO_V2_LINE_EDGE_FLAGS);
1387 if (ret)
1388 goto out_free_linereq;
Kent Gibson3c0d9c62020-09-28 08:27:54 +08001389 }
1390
1391 blocking_notifier_call_chain(&desc->gdev->notifier,
Kent Gibsonaad95582020-09-28 08:27:55 +08001392 GPIO_V2_LINE_CHANGED_REQUESTED, desc);
Kent Gibson3c0d9c62020-09-28 08:27:54 +08001393
1394 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
1395 offset);
1396 }
1397
1398 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1399 if (fd < 0) {
1400 ret = fd;
1401 goto out_free_linereq;
1402 }
1403
1404 file = anon_inode_getfile("gpio-line", &line_fileops, lr,
1405 O_RDONLY | O_CLOEXEC);
1406 if (IS_ERR(file)) {
1407 ret = PTR_ERR(file);
1408 goto out_put_unused_fd;
1409 }
1410
1411 ulr.fd = fd;
1412 if (copy_to_user(ip, &ulr, sizeof(ulr))) {
1413 /*
1414 * fput() will trigger the release() callback, so do not go onto
1415 * the regular error cleanup path here.
1416 */
1417 fput(file);
1418 put_unused_fd(fd);
1419 return -EFAULT;
1420 }
1421
1422 fd_install(fd, file);
1423
1424 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
1425 lr->num_lines);
1426
1427 return 0;
1428
1429out_put_unused_fd:
1430 put_unused_fd(fd);
1431out_free_linereq:
1432 linereq_free(lr);
1433 return ret;
1434}
1435
1436#ifdef CONFIG_GPIO_CDEV_V1
Kent Gibson925ca362020-06-16 17:36:15 +08001437
1438/*
1439 * GPIO line event management
1440 */
1441
1442/**
1443 * struct lineevent_state - contains the state of a userspace event
1444 * @gdev: the GPIO device the event pertains to
1445 * @label: consumer label used to tag descriptors
1446 * @desc: the GPIO descriptor held by this event
1447 * @eflags: the event flags this line was requested with
1448 * @irq: the interrupt that trigger in response to events on this GPIO
1449 * @wait: wait queue that handles blocking reads of events
1450 * @events: KFIFO for the GPIO events
1451 * @timestamp: cache for the timestamp storing it between hardirq
1452 * and IRQ thread, used to bring the timestamp close to the actual
1453 * event
1454 */
1455struct lineevent_state {
1456 struct gpio_device *gdev;
1457 const char *label;
1458 struct gpio_desc *desc;
1459 u32 eflags;
1460 int irq;
1461 wait_queue_head_t wait;
1462 DECLARE_KFIFO(events, struct gpioevent_data, 16);
1463 u64 timestamp;
1464};
1465
1466#define GPIOEVENT_REQUEST_VALID_FLAGS \
1467 (GPIOEVENT_REQUEST_RISING_EDGE | \
1468 GPIOEVENT_REQUEST_FALLING_EDGE)
1469
Kent Gibson49bc5272020-07-08 12:15:48 +08001470static __poll_t lineevent_poll(struct file *file,
Kent Gibsona18512e2020-07-08 12:15:46 +08001471 struct poll_table_struct *wait)
Kent Gibson925ca362020-06-16 17:36:15 +08001472{
Kent Gibson49bc5272020-07-08 12:15:48 +08001473 struct lineevent_state *le = file->private_data;
Kent Gibson925ca362020-06-16 17:36:15 +08001474 __poll_t events = 0;
1475
Kent Gibson49bc5272020-07-08 12:15:48 +08001476 poll_wait(file, &le->wait, wait);
Kent Gibson925ca362020-06-16 17:36:15 +08001477
1478 if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1479 events = EPOLLIN | EPOLLRDNORM;
1480
1481 return events;
1482}
1483
Andy Shevchenko5ad284a2020-09-15 15:58:16 +03001484static ssize_t lineevent_get_size(void)
1485{
Andy Shevchenko47e538d2020-10-05 16:10:44 +03001486#if defined(CONFIG_X86_64) && !defined(CONFIG_UML)
Andy Shevchenko5ad284a2020-09-15 15:58:16 +03001487 /* i386 has no padding after 'id' */
1488 if (in_ia32_syscall()) {
1489 struct compat_gpioeevent_data {
1490 compat_u64 timestamp;
1491 u32 id;
1492 };
1493
1494 return sizeof(struct compat_gpioeevent_data);
1495 }
1496#endif
1497 return sizeof(struct gpioevent_data);
1498}
Kent Gibson925ca362020-06-16 17:36:15 +08001499
Kent Gibson49bc5272020-07-08 12:15:48 +08001500static ssize_t lineevent_read(struct file *file,
Kent Gibson925ca362020-06-16 17:36:15 +08001501 char __user *buf,
1502 size_t count,
1503 loff_t *f_ps)
1504{
Kent Gibson49bc5272020-07-08 12:15:48 +08001505 struct lineevent_state *le = file->private_data;
Kent Gibson925ca362020-06-16 17:36:15 +08001506 struct gpioevent_data ge;
1507 ssize_t bytes_read = 0;
Andy Shevchenko5ad284a2020-09-15 15:58:16 +03001508 ssize_t ge_size;
Kent Gibson925ca362020-06-16 17:36:15 +08001509 int ret;
1510
Andy Shevchenko5ad284a2020-09-15 15:58:16 +03001511 /*
1512 * When compatible system call is being used the struct gpioevent_data,
1513 * in case of at least ia32, has different size due to the alignment
1514 * differences. Because we have first member 64 bits followed by one of
1515 * 32 bits there is no gap between them. The only difference is the
1516 * padding at the end of the data structure. Hence, we calculate the
1517 * actual sizeof() and pass this as an argument to copy_to_user() to
1518 * drop unneeded bytes from the output.
1519 */
1520 ge_size = lineevent_get_size();
1521 if (count < ge_size)
Kent Gibson925ca362020-06-16 17:36:15 +08001522 return -EINVAL;
1523
1524 do {
1525 spin_lock(&le->wait.lock);
1526 if (kfifo_is_empty(&le->events)) {
1527 if (bytes_read) {
1528 spin_unlock(&le->wait.lock);
1529 return bytes_read;
1530 }
1531
Kent Gibson49bc5272020-07-08 12:15:48 +08001532 if (file->f_flags & O_NONBLOCK) {
Kent Gibson925ca362020-06-16 17:36:15 +08001533 spin_unlock(&le->wait.lock);
1534 return -EAGAIN;
1535 }
1536
1537 ret = wait_event_interruptible_locked(le->wait,
1538 !kfifo_is_empty(&le->events));
1539 if (ret) {
1540 spin_unlock(&le->wait.lock);
1541 return ret;
1542 }
1543 }
1544
1545 ret = kfifo_out(&le->events, &ge, 1);
1546 spin_unlock(&le->wait.lock);
1547 if (ret != 1) {
1548 /*
1549 * This should never happen - we were holding the lock
1550 * from the moment we learned the fifo is no longer
1551 * empty until now.
1552 */
1553 ret = -EIO;
1554 break;
1555 }
1556
Andy Shevchenko5ad284a2020-09-15 15:58:16 +03001557 if (copy_to_user(buf + bytes_read, &ge, ge_size))
Kent Gibson925ca362020-06-16 17:36:15 +08001558 return -EFAULT;
Andy Shevchenko5ad284a2020-09-15 15:58:16 +03001559 bytes_read += ge_size;
1560 } while (count >= bytes_read + ge_size);
Kent Gibson925ca362020-06-16 17:36:15 +08001561
1562 return bytes_read;
1563}
1564
Kent Gibson46824272020-07-08 12:15:56 +08001565static void lineevent_free(struct lineevent_state *le)
1566{
1567 if (le->irq)
1568 free_irq(le->irq, le);
1569 if (le->desc)
1570 gpiod_free(le->desc);
1571 kfree(le->label);
1572 put_device(&le->gdev->dev);
1573 kfree(le);
1574}
1575
Kent Gibson49bc5272020-07-08 12:15:48 +08001576static int lineevent_release(struct inode *inode, struct file *file)
Kent Gibson925ca362020-06-16 17:36:15 +08001577{
Kent Gibson46824272020-07-08 12:15:56 +08001578 lineevent_free(file->private_data);
Kent Gibson925ca362020-06-16 17:36:15 +08001579 return 0;
1580}
1581
Kent Gibson49bc5272020-07-08 12:15:48 +08001582static long lineevent_ioctl(struct file *file, unsigned int cmd,
Kent Gibson925ca362020-06-16 17:36:15 +08001583 unsigned long arg)
1584{
Kent Gibson49bc5272020-07-08 12:15:48 +08001585 struct lineevent_state *le = file->private_data;
Kent Gibson925ca362020-06-16 17:36:15 +08001586 void __user *ip = (void __user *)arg;
1587 struct gpiohandle_data ghd;
1588
1589 /*
1590 * We can get the value for an event line but not set it,
1591 * because it is input by definition.
1592 */
1593 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
1594 int val;
1595
1596 memset(&ghd, 0, sizeof(ghd));
1597
1598 val = gpiod_get_value_cansleep(le->desc);
1599 if (val < 0)
1600 return val;
1601 ghd.values[0] = val;
1602
1603 if (copy_to_user(ip, &ghd, sizeof(ghd)))
1604 return -EFAULT;
1605
1606 return 0;
1607 }
1608 return -EINVAL;
1609}
1610
1611#ifdef CONFIG_COMPAT
Kent Gibson49bc5272020-07-08 12:15:48 +08001612static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
Kent Gibson925ca362020-06-16 17:36:15 +08001613 unsigned long arg)
1614{
Kent Gibson49bc5272020-07-08 12:15:48 +08001615 return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
Kent Gibson925ca362020-06-16 17:36:15 +08001616}
1617#endif
1618
1619static const struct file_operations lineevent_fileops = {
1620 .release = lineevent_release,
1621 .read = lineevent_read,
1622 .poll = lineevent_poll,
1623 .owner = THIS_MODULE,
1624 .llseek = noop_llseek,
1625 .unlocked_ioctl = lineevent_ioctl,
1626#ifdef CONFIG_COMPAT
1627 .compat_ioctl = lineevent_ioctl_compat,
1628#endif
1629};
1630
1631static irqreturn_t lineevent_irq_thread(int irq, void *p)
1632{
1633 struct lineevent_state *le = p;
1634 struct gpioevent_data ge;
1635 int ret;
1636
1637 /* Do not leak kernel stack to userspace */
1638 memset(&ge, 0, sizeof(ge));
1639
1640 /*
1641 * We may be running from a nested threaded interrupt in which case
1642 * we didn't get the timestamp from lineevent_irq_handler().
1643 */
1644 if (!le->timestamp)
1645 ge.timestamp = ktime_get_ns();
1646 else
1647 ge.timestamp = le->timestamp;
1648
1649 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
1650 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1651 int level = gpiod_get_value_cansleep(le->desc);
1652
1653 if (level)
1654 /* Emit low-to-high event */
1655 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1656 else
1657 /* Emit high-to-low event */
1658 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1659 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
1660 /* Emit low-to-high event */
1661 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1662 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1663 /* Emit high-to-low event */
1664 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1665 } else {
1666 return IRQ_NONE;
1667 }
1668
1669 ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
1670 1, &le->wait.lock);
1671 if (ret)
1672 wake_up_poll(&le->wait, EPOLLIN);
1673 else
1674 pr_debug_ratelimited("event FIFO is full - event dropped\n");
1675
1676 return IRQ_HANDLED;
1677}
1678
1679static irqreturn_t lineevent_irq_handler(int irq, void *p)
1680{
1681 struct lineevent_state *le = p;
1682
1683 /*
1684 * Just store the timestamp in hardirq context so we get it as
1685 * close in time as possible to the actual event.
1686 */
1687 le->timestamp = ktime_get_ns();
1688
1689 return IRQ_WAKE_THREAD;
1690}
1691
1692static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1693{
1694 struct gpioevent_request eventreq;
1695 struct lineevent_state *le;
1696 struct gpio_desc *desc;
1697 struct file *file;
1698 u32 offset;
1699 u32 lflags;
1700 u32 eflags;
1701 int fd;
1702 int ret;
Kent Gibson46824272020-07-08 12:15:56 +08001703 int irq, irqflags = 0;
Kent Gibson925ca362020-06-16 17:36:15 +08001704
1705 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1706 return -EFAULT;
1707
1708 offset = eventreq.lineoffset;
1709 lflags = eventreq.handleflags;
1710 eflags = eventreq.eventflags;
1711
1712 desc = gpiochip_get_desc(gdev->chip, offset);
1713 if (IS_ERR(desc))
1714 return PTR_ERR(desc);
1715
1716 /* Return an error if a unknown flag is set */
1717 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1718 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1719 return -EINVAL;
1720
1721 /* This is just wrong: we don't look for events on output lines */
1722 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1723 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1724 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1725 return -EINVAL;
1726
1727 /* Only one bias flag can be set. */
1728 if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1729 (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1730 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1731 ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1732 (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1733 return -EINVAL;
1734
1735 le = kzalloc(sizeof(*le), GFP_KERNEL);
1736 if (!le)
1737 return -ENOMEM;
1738 le->gdev = gdev;
1739 get_device(&gdev->dev);
1740
Kent Gibsonf188ac12020-10-05 15:02:46 +08001741 if (eventreq.consumer_label[0] != '\0') {
1742 /* label is only initialized if consumer_label is set */
1743 le->label = kstrndup(eventreq.consumer_label,
1744 sizeof(eventreq.consumer_label) - 1,
1745 GFP_KERNEL);
Kent Gibson925ca362020-06-16 17:36:15 +08001746 if (!le->label) {
1747 ret = -ENOMEM;
1748 goto out_free_le;
1749 }
1750 }
1751
1752 ret = gpiod_request(desc, le->label);
1753 if (ret)
Kent Gibson46824272020-07-08 12:15:56 +08001754 goto out_free_le;
Kent Gibson925ca362020-06-16 17:36:15 +08001755 le->desc = desc;
1756 le->eflags = eflags;
1757
Kent Gibsonc274b582020-07-08 12:15:47 +08001758 linehandle_flags_to_desc_flags(lflags, &desc->flags);
Kent Gibson925ca362020-06-16 17:36:15 +08001759
1760 ret = gpiod_direction_input(desc);
1761 if (ret)
Kent Gibson46824272020-07-08 12:15:56 +08001762 goto out_free_le;
Kent Gibson925ca362020-06-16 17:36:15 +08001763
Kent Gibson6accc372020-07-08 12:15:51 +08001764 blocking_notifier_call_chain(&desc->gdev->notifier,
Kent Gibsonaad95582020-09-28 08:27:55 +08001765 GPIO_V2_LINE_CHANGED_REQUESTED, desc);
Kent Gibson925ca362020-06-16 17:36:15 +08001766
Kent Gibson46824272020-07-08 12:15:56 +08001767 irq = gpiod_to_irq(desc);
1768 if (irq <= 0) {
Kent Gibson925ca362020-06-16 17:36:15 +08001769 ret = -ENODEV;
Kent Gibson46824272020-07-08 12:15:56 +08001770 goto out_free_le;
Kent Gibson925ca362020-06-16 17:36:15 +08001771 }
Kent Gibson46824272020-07-08 12:15:56 +08001772 le->irq = irq;
Kent Gibson925ca362020-06-16 17:36:15 +08001773
1774 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
1775 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1776 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1777 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
1778 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1779 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1780 irqflags |= IRQF_ONESHOT;
1781
1782 INIT_KFIFO(le->events);
1783 init_waitqueue_head(&le->wait);
1784
1785 /* Request a thread to read the events */
1786 ret = request_threaded_irq(le->irq,
Kent Gibsona18512e2020-07-08 12:15:46 +08001787 lineevent_irq_handler,
1788 lineevent_irq_thread,
1789 irqflags,
1790 le->label,
1791 le);
Kent Gibson925ca362020-06-16 17:36:15 +08001792 if (ret)
Kent Gibson46824272020-07-08 12:15:56 +08001793 goto out_free_le;
Kent Gibson925ca362020-06-16 17:36:15 +08001794
1795 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1796 if (fd < 0) {
1797 ret = fd;
Kent Gibson46824272020-07-08 12:15:56 +08001798 goto out_free_le;
Kent Gibson925ca362020-06-16 17:36:15 +08001799 }
1800
1801 file = anon_inode_getfile("gpio-event",
1802 &lineevent_fileops,
1803 le,
1804 O_RDONLY | O_CLOEXEC);
1805 if (IS_ERR(file)) {
1806 ret = PTR_ERR(file);
1807 goto out_put_unused_fd;
1808 }
1809
1810 eventreq.fd = fd;
1811 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
1812 /*
1813 * fput() will trigger the release() callback, so do not go onto
1814 * the regular error cleanup path here.
1815 */
1816 fput(file);
1817 put_unused_fd(fd);
1818 return -EFAULT;
1819 }
1820
1821 fd_install(fd, file);
1822
1823 return 0;
1824
1825out_put_unused_fd:
1826 put_unused_fd(fd);
Kent Gibson925ca362020-06-16 17:36:15 +08001827out_free_le:
Kent Gibson46824272020-07-08 12:15:56 +08001828 lineevent_free(le);
Kent Gibson925ca362020-06-16 17:36:15 +08001829 return ret;
1830}
1831
Kent Gibsonaad95582020-09-28 08:27:55 +08001832static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
1833 struct gpioline_info *info_v1)
1834{
1835 u64 flagsv2 = info_v2->flags;
1836
1837 memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
1838 memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
1839 info_v1->line_offset = info_v2->offset;
1840 info_v1->flags = 0;
1841
1842 if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
1843 info_v1->flags |= GPIOLINE_FLAG_KERNEL;
1844
1845 if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
1846 info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
1847
1848 if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
1849 info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
1850
1851 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
1852 info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
1853 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
1854 info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
1855
1856 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
1857 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
1858 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
1859 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
1860 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
1861 info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
1862}
1863
1864static void gpio_v2_line_info_changed_to_v1(
1865 struct gpio_v2_line_info_changed *lic_v2,
1866 struct gpioline_info_changed *lic_v1)
1867{
Gabriel Knezekb9e6c202021-06-21 15:28:59 -07001868 memset(lic_v1, 0, sizeof(*lic_v1));
Kent Gibsonaad95582020-09-28 08:27:55 +08001869 gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
1870 lic_v1->timestamp = lic_v2->timestamp_ns;
1871 lic_v1->event_type = lic_v2->event_type;
1872}
1873
Kent Gibson3c0d9c62020-09-28 08:27:54 +08001874#endif /* CONFIG_GPIO_CDEV_V1 */
1875
Kent Gibson925ca362020-06-16 17:36:15 +08001876static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
Kent Gibsonaad95582020-09-28 08:27:55 +08001877 struct gpio_v2_line_info *info)
Kent Gibson925ca362020-06-16 17:36:15 +08001878{
1879 struct gpio_chip *gc = desc->gdev->chip;
1880 bool ok_for_pinctrl;
1881 unsigned long flags;
Kent Gibson65cff702020-09-28 08:27:59 +08001882 u32 debounce_period_us;
1883 unsigned int num_attrs = 0;
Kent Gibson925ca362020-06-16 17:36:15 +08001884
Kent Gibson69e4e132020-09-28 08:27:49 +08001885 memset(info, 0, sizeof(*info));
Kent Gibsonaad95582020-09-28 08:27:55 +08001886 info->offset = gpio_chip_hwgpio(desc);
Kent Gibson925ca362020-06-16 17:36:15 +08001887
1888 /*
1889 * This function takes a mutex so we must check this before taking
1890 * the spinlock.
1891 *
1892 * FIXME: find a non-racy way to retrieve this information. Maybe a
1893 * lock common to both frameworks?
1894 */
1895 ok_for_pinctrl =
Kent Gibsonaad95582020-09-28 08:27:55 +08001896 pinctrl_gpio_can_use_line(gc->base + info->offset);
Kent Gibson925ca362020-06-16 17:36:15 +08001897
1898 spin_lock_irqsave(&gpio_lock, flags);
1899
Kent Gibson69e4e132020-09-28 08:27:49 +08001900 if (desc->name)
1901 strscpy(info->name, desc->name, sizeof(info->name));
Kent Gibson925ca362020-06-16 17:36:15 +08001902
Kent Gibson69e4e132020-09-28 08:27:49 +08001903 if (desc->label)
1904 strscpy(info->consumer, desc->label, sizeof(info->consumer));
Kent Gibson925ca362020-06-16 17:36:15 +08001905
1906 /*
1907 * Userspace only need to know that the kernel is using this GPIO so
1908 * it can't use it.
1909 */
1910 info->flags = 0;
1911 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1912 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1913 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1914 test_bit(FLAG_EXPORT, &desc->flags) ||
1915 test_bit(FLAG_SYSFS, &desc->flags) ||
1916 !ok_for_pinctrl)
Kent Gibsonaad95582020-09-28 08:27:55 +08001917 info->flags |= GPIO_V2_LINE_FLAG_USED;
1918
Kent Gibson925ca362020-06-16 17:36:15 +08001919 if (test_bit(FLAG_IS_OUT, &desc->flags))
Kent Gibsonaad95582020-09-28 08:27:55 +08001920 info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
1921 else
1922 info->flags |= GPIO_V2_LINE_FLAG_INPUT;
1923
Kent Gibson925ca362020-06-16 17:36:15 +08001924 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Kent Gibsonaad95582020-09-28 08:27:55 +08001925 info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
1926
Kent Gibson925ca362020-06-16 17:36:15 +08001927 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Kent Gibsonaad95582020-09-28 08:27:55 +08001928 info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
Kent Gibson925ca362020-06-16 17:36:15 +08001929 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Kent Gibsonaad95582020-09-28 08:27:55 +08001930 info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
1931
Kent Gibson925ca362020-06-16 17:36:15 +08001932 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
Kent Gibsonaad95582020-09-28 08:27:55 +08001933 info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
Kent Gibson925ca362020-06-16 17:36:15 +08001934 if (test_bit(FLAG_PULL_DOWN, &desc->flags))
Kent Gibsonaad95582020-09-28 08:27:55 +08001935 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
Kent Gibson925ca362020-06-16 17:36:15 +08001936 if (test_bit(FLAG_PULL_UP, &desc->flags))
Kent Gibsonaad95582020-09-28 08:27:55 +08001937 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
Kent Gibson925ca362020-06-16 17:36:15 +08001938
Kent Gibson73e03412020-09-28 08:27:56 +08001939 if (test_bit(FLAG_EDGE_RISING, &desc->flags))
1940 info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
1941 if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
1942 info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
1943
Kent Gibson65cff702020-09-28 08:27:59 +08001944 debounce_period_us = READ_ONCE(desc->debounce_period_us);
1945 if (debounce_period_us) {
1946 info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
1947 info->attrs[num_attrs].debounce_period_us = debounce_period_us;
1948 num_attrs++;
1949 }
1950 info->num_attrs = num_attrs;
Kent Gibson925ca362020-06-16 17:36:15 +08001951
1952 spin_unlock_irqrestore(&gpio_lock, flags);
1953}
1954
1955struct gpio_chardev_data {
1956 struct gpio_device *gdev;
1957 wait_queue_head_t wait;
Kent Gibsonaad95582020-09-28 08:27:55 +08001958 DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
Kent Gibson925ca362020-06-16 17:36:15 +08001959 struct notifier_block lineinfo_changed_nb;
1960 unsigned long *watched_lines;
Kent Gibsonaad95582020-09-28 08:27:55 +08001961#ifdef CONFIG_GPIO_CDEV_V1
1962 atomic_t watch_abi_version;
1963#endif
Kent Gibson925ca362020-06-16 17:36:15 +08001964};
1965
Kent Gibson73ad8d02020-12-28 00:10:40 +08001966static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
1967{
1968 struct gpio_device *gdev = cdev->gdev;
1969 struct gpiochip_info chipinfo;
1970
1971 memset(&chipinfo, 0, sizeof(chipinfo));
1972
1973 strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
1974 strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
1975 chipinfo.lines = gdev->ngpio;
1976 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1977 return -EFAULT;
1978 return 0;
1979}
1980
Kent Gibsonaad95582020-09-28 08:27:55 +08001981#ifdef CONFIG_GPIO_CDEV_V1
1982/*
1983 * returns 0 if the versions match, else the previously selected ABI version
1984 */
1985static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
1986 unsigned int version)
1987{
1988 int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
1989
1990 if (abiv == version)
1991 return 0;
1992
1993 return abiv;
1994}
Kent Gibson73ad8d02020-12-28 00:10:40 +08001995
1996static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
1997 bool watch)
1998{
1999 struct gpio_desc *desc;
2000 struct gpioline_info lineinfo;
2001 struct gpio_v2_line_info lineinfo_v2;
2002
2003 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2004 return -EFAULT;
2005
2006 /* this doubles as a range check on line_offset */
2007 desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset);
2008 if (IS_ERR(desc))
2009 return PTR_ERR(desc);
2010
2011 if (watch) {
2012 if (lineinfo_ensure_abi_version(cdev, 1))
2013 return -EPERM;
2014
2015 if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
2016 return -EBUSY;
2017 }
2018
2019 gpio_desc_to_lineinfo(desc, &lineinfo_v2);
2020 gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
2021
2022 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2023 if (watch)
2024 clear_bit(lineinfo.line_offset, cdev->watched_lines);
2025 return -EFAULT;
2026 }
2027
2028 return 0;
2029}
Kent Gibsonaad95582020-09-28 08:27:55 +08002030#endif
2031
2032static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
2033 bool watch)
2034{
2035 struct gpio_desc *desc;
2036 struct gpio_v2_line_info lineinfo;
2037
2038 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2039 return -EFAULT;
2040
2041 if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2042 return -EINVAL;
2043
2044 desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2045 if (IS_ERR(desc))
2046 return PTR_ERR(desc);
2047
2048 if (watch) {
2049#ifdef CONFIG_GPIO_CDEV_V1
2050 if (lineinfo_ensure_abi_version(cdev, 2))
2051 return -EPERM;
2052#endif
2053 if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2054 return -EBUSY;
2055 }
2056 gpio_desc_to_lineinfo(desc, &lineinfo);
2057
2058 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2059 if (watch)
2060 clear_bit(lineinfo.offset, cdev->watched_lines);
2061 return -EFAULT;
2062 }
2063
2064 return 0;
2065}
2066
Kent Gibson73ad8d02020-12-28 00:10:40 +08002067static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
2068{
2069 __u32 offset;
2070
2071 if (copy_from_user(&offset, ip, sizeof(offset)))
2072 return -EFAULT;
2073
2074 if (offset >= cdev->gdev->ngpio)
2075 return -EINVAL;
2076
2077 if (!test_and_clear_bit(offset, cdev->watched_lines))
2078 return -EBUSY;
2079
2080 return 0;
2081}
2082
Kent Gibson925ca362020-06-16 17:36:15 +08002083/*
2084 * gpio_ioctl() - ioctl handler for the GPIO chardev
2085 */
Kent Gibson49bc5272020-07-08 12:15:48 +08002086static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Kent Gibson925ca362020-06-16 17:36:15 +08002087{
Kent Gibsone2b781c2020-07-08 12:15:52 +08002088 struct gpio_chardev_data *cdev = file->private_data;
2089 struct gpio_device *gdev = cdev->gdev;
Kent Gibson925ca362020-06-16 17:36:15 +08002090 void __user *ip = (void __user *)arg;
Kent Gibson925ca362020-06-16 17:36:15 +08002091
2092 /* We fail any subsequent ioctl():s when the chip is gone */
Kent Gibson73ad8d02020-12-28 00:10:40 +08002093 if (!gdev->chip)
Kent Gibson925ca362020-06-16 17:36:15 +08002094 return -ENODEV;
2095
2096 /* Fill in the struct and pass to userspace */
2097 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Kent Gibson73ad8d02020-12-28 00:10:40 +08002098 return chipinfo_get(cdev, ip);
Kent Gibson3c0d9c62020-09-28 08:27:54 +08002099#ifdef CONFIG_GPIO_CDEV_V1
Kent Gibson925ca362020-06-16 17:36:15 +08002100 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
2101 return linehandle_create(gdev, ip);
2102 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
2103 return lineevent_create(gdev, ip);
Kent Gibson73ad8d02020-12-28 00:10:40 +08002104 } else if (cmd == GPIO_GET_LINEINFO_IOCTL ||
2105 cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
2106 return lineinfo_get_v1(cdev, ip,
2107 cmd == GPIO_GET_LINEINFO_WATCH_IOCTL);
Kent Gibson3c0d9c62020-09-28 08:27:54 +08002108#endif /* CONFIG_GPIO_CDEV_V1 */
Kent Gibsonaad95582020-09-28 08:27:55 +08002109 } else if (cmd == GPIO_V2_GET_LINEINFO_IOCTL ||
2110 cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL) {
2111 return lineinfo_get(cdev, ip,
2112 cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL);
Kent Gibson3c0d9c62020-09-28 08:27:54 +08002113 } else if (cmd == GPIO_V2_GET_LINE_IOCTL) {
2114 return linereq_create(gdev, ip);
Kent Gibson925ca362020-06-16 17:36:15 +08002115 } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) {
Kent Gibson73ad8d02020-12-28 00:10:40 +08002116 return lineinfo_unwatch(cdev, ip);
Kent Gibson925ca362020-06-16 17:36:15 +08002117 }
2118 return -EINVAL;
2119}
2120
2121#ifdef CONFIG_COMPAT
Kent Gibson49bc5272020-07-08 12:15:48 +08002122static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
Kent Gibson925ca362020-06-16 17:36:15 +08002123 unsigned long arg)
2124{
Kent Gibson49bc5272020-07-08 12:15:48 +08002125 return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
Kent Gibson925ca362020-06-16 17:36:15 +08002126}
2127#endif
2128
2129static struct gpio_chardev_data *
2130to_gpio_chardev_data(struct notifier_block *nb)
2131{
2132 return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2133}
2134
2135static int lineinfo_changed_notify(struct notifier_block *nb,
2136 unsigned long action, void *data)
2137{
Kent Gibsone2b781c2020-07-08 12:15:52 +08002138 struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
Kent Gibsonaad95582020-09-28 08:27:55 +08002139 struct gpio_v2_line_info_changed chg;
Kent Gibson925ca362020-06-16 17:36:15 +08002140 struct gpio_desc *desc = data;
2141 int ret;
2142
Kent Gibsone2b781c2020-07-08 12:15:52 +08002143 if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
Kent Gibson925ca362020-06-16 17:36:15 +08002144 return NOTIFY_DONE;
2145
2146 memset(&chg, 0, sizeof(chg));
Kent Gibson925ca362020-06-16 17:36:15 +08002147 chg.event_type = action;
Kent Gibsonaad95582020-09-28 08:27:55 +08002148 chg.timestamp_ns = ktime_get_ns();
Kent Gibson925ca362020-06-16 17:36:15 +08002149 gpio_desc_to_lineinfo(desc, &chg.info);
2150
Kent Gibsone2b781c2020-07-08 12:15:52 +08002151 ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
Kent Gibson925ca362020-06-16 17:36:15 +08002152 if (ret)
Kent Gibsone2b781c2020-07-08 12:15:52 +08002153 wake_up_poll(&cdev->wait, EPOLLIN);
Kent Gibson925ca362020-06-16 17:36:15 +08002154 else
2155 pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2156
2157 return NOTIFY_OK;
2158}
2159
Kent Gibson49bc5272020-07-08 12:15:48 +08002160static __poll_t lineinfo_watch_poll(struct file *file,
Kent Gibson925ca362020-06-16 17:36:15 +08002161 struct poll_table_struct *pollt)
2162{
Kent Gibsone2b781c2020-07-08 12:15:52 +08002163 struct gpio_chardev_data *cdev = file->private_data;
Kent Gibson925ca362020-06-16 17:36:15 +08002164 __poll_t events = 0;
2165
Kent Gibsone2b781c2020-07-08 12:15:52 +08002166 poll_wait(file, &cdev->wait, pollt);
Kent Gibson925ca362020-06-16 17:36:15 +08002167
Kent Gibsone2b781c2020-07-08 12:15:52 +08002168 if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2169 &cdev->wait.lock))
Kent Gibson925ca362020-06-16 17:36:15 +08002170 events = EPOLLIN | EPOLLRDNORM;
2171
2172 return events;
2173}
2174
Kent Gibson49bc5272020-07-08 12:15:48 +08002175static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
Kent Gibson925ca362020-06-16 17:36:15 +08002176 size_t count, loff_t *off)
2177{
Kent Gibsone2b781c2020-07-08 12:15:52 +08002178 struct gpio_chardev_data *cdev = file->private_data;
Kent Gibsonaad95582020-09-28 08:27:55 +08002179 struct gpio_v2_line_info_changed event;
Kent Gibson925ca362020-06-16 17:36:15 +08002180 ssize_t bytes_read = 0;
2181 int ret;
Kent Gibsonaad95582020-09-28 08:27:55 +08002182 size_t event_size;
Kent Gibson925ca362020-06-16 17:36:15 +08002183
Kent Gibsonaad95582020-09-28 08:27:55 +08002184#ifndef CONFIG_GPIO_CDEV_V1
2185 event_size = sizeof(struct gpio_v2_line_info_changed);
2186 if (count < event_size)
Kent Gibson925ca362020-06-16 17:36:15 +08002187 return -EINVAL;
Kent Gibsonaad95582020-09-28 08:27:55 +08002188#endif
Kent Gibson925ca362020-06-16 17:36:15 +08002189
2190 do {
Kent Gibsone2b781c2020-07-08 12:15:52 +08002191 spin_lock(&cdev->wait.lock);
2192 if (kfifo_is_empty(&cdev->events)) {
Kent Gibson925ca362020-06-16 17:36:15 +08002193 if (bytes_read) {
Kent Gibsone2b781c2020-07-08 12:15:52 +08002194 spin_unlock(&cdev->wait.lock);
Kent Gibson925ca362020-06-16 17:36:15 +08002195 return bytes_read;
2196 }
2197
Kent Gibson49bc5272020-07-08 12:15:48 +08002198 if (file->f_flags & O_NONBLOCK) {
Kent Gibsone2b781c2020-07-08 12:15:52 +08002199 spin_unlock(&cdev->wait.lock);
Kent Gibson925ca362020-06-16 17:36:15 +08002200 return -EAGAIN;
2201 }
2202
Kent Gibsone2b781c2020-07-08 12:15:52 +08002203 ret = wait_event_interruptible_locked(cdev->wait,
2204 !kfifo_is_empty(&cdev->events));
Kent Gibson925ca362020-06-16 17:36:15 +08002205 if (ret) {
Kent Gibsone2b781c2020-07-08 12:15:52 +08002206 spin_unlock(&cdev->wait.lock);
Kent Gibson925ca362020-06-16 17:36:15 +08002207 return ret;
2208 }
2209 }
Kent Gibsonaad95582020-09-28 08:27:55 +08002210#ifdef CONFIG_GPIO_CDEV_V1
2211 /* must be after kfifo check so watch_abi_version is set */
2212 if (atomic_read(&cdev->watch_abi_version) == 2)
2213 event_size = sizeof(struct gpio_v2_line_info_changed);
2214 else
2215 event_size = sizeof(struct gpioline_info_changed);
2216 if (count < event_size) {
2217 spin_unlock(&cdev->wait.lock);
2218 return -EINVAL;
2219 }
2220#endif
Kent Gibsone2b781c2020-07-08 12:15:52 +08002221 ret = kfifo_out(&cdev->events, &event, 1);
2222 spin_unlock(&cdev->wait.lock);
Kent Gibson925ca362020-06-16 17:36:15 +08002223 if (ret != 1) {
2224 ret = -EIO;
2225 break;
2226 /* We should never get here. See lineevent_read(). */
2227 }
2228
Kent Gibsonaad95582020-09-28 08:27:55 +08002229#ifdef CONFIG_GPIO_CDEV_V1
2230 if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2231 if (copy_to_user(buf + bytes_read, &event, event_size))
2232 return -EFAULT;
2233 } else {
2234 struct gpioline_info_changed event_v1;
2235
2236 gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2237 if (copy_to_user(buf + bytes_read, &event_v1,
2238 event_size))
2239 return -EFAULT;
2240 }
2241#else
2242 if (copy_to_user(buf + bytes_read, &event, event_size))
Kent Gibson925ca362020-06-16 17:36:15 +08002243 return -EFAULT;
Kent Gibsonaad95582020-09-28 08:27:55 +08002244#endif
2245 bytes_read += event_size;
Kent Gibson925ca362020-06-16 17:36:15 +08002246 } while (count >= bytes_read + sizeof(event));
2247
2248 return bytes_read;
2249}
2250
2251/**
2252 * gpio_chrdev_open() - open the chardev for ioctl operations
2253 * @inode: inode for this chardev
Kent Gibson49bc5272020-07-08 12:15:48 +08002254 * @file: file struct for storing private data
Kent Gibson925ca362020-06-16 17:36:15 +08002255 * Returns 0 on success
2256 */
Kent Gibson49bc5272020-07-08 12:15:48 +08002257static int gpio_chrdev_open(struct inode *inode, struct file *file)
Kent Gibson925ca362020-06-16 17:36:15 +08002258{
2259 struct gpio_device *gdev = container_of(inode->i_cdev,
Kent Gibsona18512e2020-07-08 12:15:46 +08002260 struct gpio_device, chrdev);
Kent Gibsone2b781c2020-07-08 12:15:52 +08002261 struct gpio_chardev_data *cdev;
Kent Gibson925ca362020-06-16 17:36:15 +08002262 int ret = -ENOMEM;
2263
2264 /* Fail on open if the backing gpiochip is gone */
2265 if (!gdev->chip)
2266 return -ENODEV;
2267
Kent Gibsone2b781c2020-07-08 12:15:52 +08002268 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2269 if (!cdev)
Kent Gibson925ca362020-06-16 17:36:15 +08002270 return -ENOMEM;
2271
Kent Gibsone2b781c2020-07-08 12:15:52 +08002272 cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2273 if (!cdev->watched_lines)
2274 goto out_free_cdev;
Kent Gibson925ca362020-06-16 17:36:15 +08002275
Kent Gibsone2b781c2020-07-08 12:15:52 +08002276 init_waitqueue_head(&cdev->wait);
2277 INIT_KFIFO(cdev->events);
2278 cdev->gdev = gdev;
Kent Gibson925ca362020-06-16 17:36:15 +08002279
Kent Gibsone2b781c2020-07-08 12:15:52 +08002280 cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
Kent Gibson6accc372020-07-08 12:15:51 +08002281 ret = blocking_notifier_chain_register(&gdev->notifier,
Kent Gibsone2b781c2020-07-08 12:15:52 +08002282 &cdev->lineinfo_changed_nb);
Kent Gibson925ca362020-06-16 17:36:15 +08002283 if (ret)
2284 goto out_free_bitmap;
2285
2286 get_device(&gdev->dev);
Kent Gibsone2b781c2020-07-08 12:15:52 +08002287 file->private_data = cdev;
Kent Gibson925ca362020-06-16 17:36:15 +08002288
Kent Gibson49bc5272020-07-08 12:15:48 +08002289 ret = nonseekable_open(inode, file);
Kent Gibson925ca362020-06-16 17:36:15 +08002290 if (ret)
2291 goto out_unregister_notifier;
2292
2293 return ret;
2294
2295out_unregister_notifier:
Kent Gibson6accc372020-07-08 12:15:51 +08002296 blocking_notifier_chain_unregister(&gdev->notifier,
Kent Gibsone2b781c2020-07-08 12:15:52 +08002297 &cdev->lineinfo_changed_nb);
Kent Gibson925ca362020-06-16 17:36:15 +08002298out_free_bitmap:
Kent Gibsone2b781c2020-07-08 12:15:52 +08002299 bitmap_free(cdev->watched_lines);
2300out_free_cdev:
2301 kfree(cdev);
Kent Gibson925ca362020-06-16 17:36:15 +08002302 return ret;
2303}
2304
2305/**
2306 * gpio_chrdev_release() - close chardev after ioctl operations
2307 * @inode: inode for this chardev
Kent Gibson49bc5272020-07-08 12:15:48 +08002308 * @file: file struct for storing private data
Kent Gibson925ca362020-06-16 17:36:15 +08002309 * Returns 0 on success
2310 */
Kent Gibson49bc5272020-07-08 12:15:48 +08002311static int gpio_chrdev_release(struct inode *inode, struct file *file)
Kent Gibson925ca362020-06-16 17:36:15 +08002312{
Kent Gibsone2b781c2020-07-08 12:15:52 +08002313 struct gpio_chardev_data *cdev = file->private_data;
2314 struct gpio_device *gdev = cdev->gdev;
Kent Gibson925ca362020-06-16 17:36:15 +08002315
Kent Gibsone2b781c2020-07-08 12:15:52 +08002316 bitmap_free(cdev->watched_lines);
Kent Gibson6accc372020-07-08 12:15:51 +08002317 blocking_notifier_chain_unregister(&gdev->notifier,
Kent Gibsone2b781c2020-07-08 12:15:52 +08002318 &cdev->lineinfo_changed_nb);
Kent Gibson925ca362020-06-16 17:36:15 +08002319 put_device(&gdev->dev);
Kent Gibsone2b781c2020-07-08 12:15:52 +08002320 kfree(cdev);
Kent Gibson925ca362020-06-16 17:36:15 +08002321
2322 return 0;
2323}
2324
2325static const struct file_operations gpio_fileops = {
2326 .release = gpio_chrdev_release,
2327 .open = gpio_chrdev_open,
2328 .poll = lineinfo_watch_poll,
2329 .read = lineinfo_watch_read,
2330 .owner = THIS_MODULE,
2331 .llseek = no_llseek,
2332 .unlocked_ioctl = gpio_ioctl,
2333#ifdef CONFIG_COMPAT
2334 .compat_ioctl = gpio_ioctl_compat,
2335#endif
2336};
2337
2338int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2339{
2340 int ret;
2341
2342 cdev_init(&gdev->chrdev, &gpio_fileops);
2343 gdev->chrdev.owner = THIS_MODULE;
2344 gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2345
2346 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2347 if (ret)
2348 return ret;
2349
2350 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2351 MAJOR(devt), gdev->id);
2352
2353 return 0;
2354}
2355
2356void gpiolib_cdev_unregister(struct gpio_device *gdev)
2357{
2358 cdev_device_del(&gdev->chrdev, &gdev->dev);
2359}