Commit | Line | Data |
---|---|---|
925ca369 KG |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | ||
d189f627 | 3 | #include <linux/anon_inodes.h> |
3c0d9c63 | 4 | #include <linux/atomic.h> |
925ca369 | 5 | #include <linux/bitmap.h> |
3c0d9c63 | 6 | #include <linux/build_bug.h> |
d189f627 KG |
7 | #include <linux/cdev.h> |
8 | #include <linux/compat.h> | |
65cff704 | 9 | #include <linux/compiler.h> |
925ca369 KG |
10 | #include <linux/device.h> |
11 | #include <linux/err.h> | |
d189f627 | 12 | #include <linux/file.h> |
925ca369 KG |
13 | #include <linux/gpio.h> |
14 | #include <linux/gpio/driver.h> | |
d189f627 KG |
15 | #include <linux/interrupt.h> |
16 | #include <linux/irqreturn.h> | |
17 | #include <linux/kernel.h> | |
925ca369 | 18 | #include <linux/kfifo.h> |
d189f627 | 19 | #include <linux/module.h> |
a54756cb | 20 | #include <linux/mutex.h> |
d189f627 | 21 | #include <linux/pinctrl/consumer.h> |
925ca369 | 22 | #include <linux/poll.h> |
d189f627 | 23 | #include <linux/spinlock.h> |
925ca369 | 24 | #include <linux/timekeeping.h> |
d189f627 | 25 | #include <linux/uaccess.h> |
65cff704 | 26 | #include <linux/workqueue.h> |
925ca369 KG |
27 | #include <uapi/linux/gpio.h> |
28 | ||
29 | #include "gpiolib.h" | |
30 | #include "gpiolib-cdev.h" | |
31 | ||
3c0d9c63 KG |
32 | /* |
33 | * Array sizes must ensure 64-bit alignment and not create holes in the | |
34 | * struct packing. | |
35 | */ | |
36 | static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2)); | |
37 | static_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 | */ | |
42 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8)); | |
43 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8)); | |
44 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8)); | |
45 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8)); | |
46 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8)); | |
47 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8)); | |
48 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8)); | |
49 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8)); | |
50 | ||
925ca369 KG |
51 | /* 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 | ||
3c0d9c63 | 61 | #ifdef CONFIG_GPIO_CDEV_V1 |
925ca369 KG |
62 | /** |
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 | |
52b7b596 | 67 | * @num_descs: the number of descriptors held in the descs array |
925ca369 KG |
68 | */ |
69 | struct linehandle_state { | |
70 | struct gpio_device *gdev; | |
71 | const char *label; | |
72 | struct gpio_desc *descs[GPIOHANDLES_MAX]; | |
52b7b596 | 73 | u32 num_descs; |
925ca369 KG |
74 | }; |
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 | ||
86 | static 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 | | |
a18512e3 | 126 | GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || |
925ca369 KG |
127 | ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && |
128 | (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) | |
129 | return -EINVAL; | |
130 | ||
131 | return 0; | |
132 | } | |
133 | ||
c274b58a KG |
134 | static 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 | ||
925ca369 KG |
150 | static 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; | |
925ca369 KG |
157 | |
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 | ||
52b7b596 | 166 | for (i = 0; i < lh->num_descs; i++) { |
925ca369 | 167 | desc = lh->descs[i]; |
c274b58a | 168 | linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags); |
925ca369 KG |
169 | |
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 | ||
6accc376 | 186 | blocking_notifier_call_chain(&desc->gdev->notifier, |
aad95584 KG |
187 | GPIO_V2_LINE_CHANGED_CONFIG, |
188 | desc); | |
925ca369 KG |
189 | } |
190 | return 0; | |
191 | } | |
192 | ||
49bc5279 | 193 | static long linehandle_ioctl(struct file *file, unsigned int cmd, |
925ca369 KG |
194 | unsigned long arg) |
195 | { | |
49bc5279 | 196 | struct linehandle_state *lh = file->private_data; |
925ca369 KG |
197 | 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, | |
52b7b596 | 206 | lh->num_descs, |
925ca369 KG |
207 | lh->descs, |
208 | NULL, | |
209 | vals); | |
210 | if (ret) | |
211 | return ret; | |
212 | ||
213 | memset(&ghd, 0, sizeof(ghd)); | |
52b7b596 | 214 | for (i = 0; i < lh->num_descs; i++) |
925ca369 KG |
215 | 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] */ | |
52b7b596 | 233 | for (i = 0; i < lh->num_descs; i++) |
925ca369 KG |
234 | __assign_bit(i, vals, ghd.values[i]); |
235 | ||
236 | /* Reuse the array setting function */ | |
237 | return gpiod_set_array_value_complex(false, | |
a18512e3 | 238 | true, |
52b7b596 | 239 | lh->num_descs, |
a18512e3 KG |
240 | lh->descs, |
241 | NULL, | |
242 | vals); | |
925ca369 KG |
243 | } else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) { |
244 | return linehandle_set_config(lh, ip); | |
245 | } | |
246 | return -EINVAL; | |
247 | } | |
248 | ||
249 | #ifdef CONFIG_COMPAT | |
49bc5279 | 250 | static long linehandle_ioctl_compat(struct file *file, unsigned int cmd, |
a18512e3 | 251 | unsigned long arg) |
925ca369 | 252 | { |
49bc5279 | 253 | return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
925ca369 KG |
254 | } |
255 | #endif | |
256 | ||
883f9198 | 257 | static void linehandle_free(struct linehandle_state *lh) |
925ca369 | 258 | { |
925ca369 KG |
259 | int i; |
260 | ||
52b7b596 | 261 | for (i = 0; i < lh->num_descs; i++) |
883f9198 KG |
262 | if (lh->descs[i]) |
263 | gpiod_free(lh->descs[i]); | |
925ca369 | 264 | kfree(lh->label); |
883f9198 | 265 | put_device(&lh->gdev->dev); |
925ca369 | 266 | kfree(lh); |
883f9198 KG |
267 | } |
268 | ||
269 | static int linehandle_release(struct inode *inode, struct file *file) | |
270 | { | |
271 | linehandle_free(file->private_data); | |
925ca369 KG |
272 | return 0; |
273 | } | |
274 | ||
275 | static 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 | ||
285 | static 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; | |
883f9198 | 290 | int fd, i, ret; |
925ca369 KG |
291 | 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 | ||
f188ac12 KG |
310 | 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); | |
925ca369 KG |
315 | if (!lh->label) { |
316 | ret = -ENOMEM; | |
317 | goto out_free_lh; | |
318 | } | |
319 | } | |
320 | ||
883f9198 KG |
321 | lh->num_descs = handlereq.lines; |
322 | ||
925ca369 KG |
323 | /* 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); | |
883f9198 | 330 | goto out_free_lh; |
925ca369 KG |
331 | } |
332 | ||
333 | ret = gpiod_request(desc, lh->label); | |
334 | if (ret) | |
883f9198 | 335 | goto out_free_lh; |
925ca369 | 336 | lh->descs[i] = desc; |
c274b58a | 337 | linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags); |
925ca369 KG |
338 | |
339 | ret = gpiod_set_transitory(desc, false); | |
340 | if (ret < 0) | |
883f9198 | 341 | goto out_free_lh; |
925ca369 KG |
342 | |
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) | |
883f9198 | 352 | goto out_free_lh; |
925ca369 KG |
353 | } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { |
354 | ret = gpiod_direction_input(desc); | |
355 | if (ret) | |
883f9198 | 356 | goto out_free_lh; |
925ca369 KG |
357 | } |
358 | ||
6accc376 | 359 | blocking_notifier_call_chain(&desc->gdev->notifier, |
aad95584 | 360 | GPIO_V2_LINE_CHANGED_REQUESTED, desc); |
925ca369 KG |
361 | |
362 | dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", | |
363 | offset); | |
364 | } | |
925ca369 KG |
365 | |
366 | fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); | |
367 | if (fd < 0) { | |
368 | ret = fd; | |
883f9198 | 369 | goto out_free_lh; |
925ca369 KG |
370 | } |
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", | |
52b7b596 | 395 | lh->num_descs); |
925ca369 KG |
396 | |
397 | return 0; | |
398 | ||
399 | out_put_unused_fd: | |
400 | put_unused_fd(fd); | |
925ca369 | 401 | out_free_lh: |
883f9198 | 402 | linehandle_free(lh); |
925ca369 KG |
403 | return ret; |
404 | } | |
3c0d9c63 KG |
405 | #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. | |
73e03419 KG |
410 | * @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. | |
65cff704 KG |
420 | * @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 | |
3c0d9c63 KG |
423 | */ |
424 | struct line { | |
425 | struct gpio_desc *desc; | |
73e03419 KG |
426 | /* |
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; | |
65cff704 KG |
439 | /* |
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 | */ | |
73e03419 | 444 | u32 line_seqno; |
65cff704 KG |
445 | /* |
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; | |
3c0d9c63 KG |
461 | }; |
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 | |
73e03419 KG |
468 | * @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. | |
a54756cb KG |
474 | * @config_mutex: mutex for serializing ioctl() calls to ensure consistency |
475 | * of configuration, particularly multi-step accesses to desc flags. | |
3c0d9c63 KG |
476 | * @lines: the lines held by this line request, with @num_lines elements. |
477 | */ | |
478 | struct linereq { | |
479 | struct gpio_device *gdev; | |
480 | const char *label; | |
481 | u32 num_lines; | |
73e03419 KG |
482 | wait_queue_head_t wait; |
483 | u32 event_buffer_size; | |
484 | DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); | |
485 | atomic_t seqno; | |
a54756cb | 486 | struct mutex config_mutex; |
3c0d9c63 KG |
487 | 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 | ||
73e03419 KG |
503 | #define GPIO_V2_LINE_EDGE_FLAGS \ |
504 | (GPIO_V2_LINE_FLAG_EDGE_RISING | \ | |
505 | GPIO_V2_LINE_FLAG_EDGE_FALLING) | |
506 | ||
3c0d9c63 KG |
507 | #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 | \ | |
73e03419 | 511 | GPIO_V2_LINE_EDGE_FLAGS | \ |
3c0d9c63 KG |
512 | GPIO_V2_LINE_BIAS_FLAGS) |
513 | ||
73e03419 KG |
514 | static 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 | ||
532 | static 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 | ||
584 | static 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 | ||
65cff704 KG |
601 | /* |
602 | * returns the current debounced logical value. | |
603 | */ | |
604 | static 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 | ||
621 | static 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 | ||
631 | static 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 | ||
683 | static 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 | ||
721 | static 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 | ||
735 | static 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 | ||
73e03419 KG |
749 | static void edge_detector_stop(struct line *line) |
750 | { | |
751 | if (line->irq) { | |
752 | free_irq(line->irq, line); | |
753 | line->irq = 0; | |
754 | } | |
a54756cb | 755 | |
65cff704 KG |
756 | cancel_delayed_work_sync(&line->work); |
757 | WRITE_ONCE(line->sw_debounced, 0); | |
a54756cb | 758 | line->eflags = 0; |
65cff704 | 759 | /* do not change line->level - see comment in debounced_value() */ |
73e03419 KG |
760 | } |
761 | ||
762 | static int edge_detector_setup(struct line *line, | |
65cff704 KG |
763 | struct gpio_v2_line_config *lc, |
764 | unsigned int line_idx, | |
73e03419 KG |
765 | u64 eflags) |
766 | { | |
65cff704 | 767 | u32 debounce_period_us; |
73e03419 KG |
768 | unsigned long irqflags = 0; |
769 | int irq, ret; | |
770 | ||
771 | if (eflags && !kfifo_initialized(&line->req->events)) { | |
772 | ret = kfifo_alloc(&line->req->events, | |
773 | line->req->event_buffer_size, GFP_KERNEL); | |
774 | if (ret) | |
775 | return ret; | |
776 | } | |
777 | line->eflags = eflags; | |
65cff704 KG |
778 | if (gpio_v2_line_config_debounced(lc, line_idx)) { |
779 | debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx); | |
780 | ret = debounce_setup(line, debounce_period_us); | |
781 | if (ret) | |
782 | return ret; | |
783 | WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); | |
784 | } | |
73e03419 | 785 | |
65cff704 KG |
786 | /* detection disabled or sw debouncer will provide edge detection */ |
787 | if (!eflags || READ_ONCE(line->sw_debounced)) | |
73e03419 KG |
788 | return 0; |
789 | ||
790 | irq = gpiod_to_irq(line->desc); | |
791 | if (irq < 0) | |
792 | return -ENXIO; | |
793 | ||
794 | if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) | |
795 | irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? | |
796 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; | |
797 | if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) | |
798 | irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? | |
799 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; | |
800 | irqflags |= IRQF_ONESHOT; | |
801 | ||
802 | /* Request a thread to read the events */ | |
803 | ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread, | |
804 | irqflags, line->req->label, line); | |
805 | if (ret) | |
806 | return ret; | |
807 | ||
808 | line->irq = irq; | |
809 | return 0; | |
810 | } | |
811 | ||
65cff704 KG |
812 | static int edge_detector_update(struct line *line, |
813 | struct gpio_v2_line_config *lc, | |
814 | unsigned int line_idx, | |
815 | u64 eflags, bool polarity_change) | |
a54756cb | 816 | { |
65cff704 KG |
817 | unsigned int debounce_period_us = |
818 | gpio_v2_line_config_debounce_period(lc, line_idx); | |
819 | ||
820 | if ((line->eflags == eflags) && !polarity_change && | |
821 | (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us)) | |
822 | return 0; | |
823 | ||
824 | /* sw debounced and still will be...*/ | |
825 | if (debounce_period_us && READ_ONCE(line->sw_debounced)) { | |
826 | line->eflags = eflags; | |
827 | WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); | |
a54756cb | 828 | return 0; |
65cff704 | 829 | } |
a54756cb | 830 | |
65cff704 KG |
831 | /* reconfiguring edge detection or sw debounce being disabled */ |
832 | if ((line->irq && !READ_ONCE(line->sw_debounced)) || | |
833 | (!debounce_period_us && READ_ONCE(line->sw_debounced))) | |
834 | edge_detector_stop(line); | |
a54756cb | 835 | |
65cff704 | 836 | return edge_detector_setup(line, lc, line_idx, eflags); |
a54756cb KG |
837 | } |
838 | ||
3c0d9c63 KG |
839 | static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, |
840 | unsigned int line_idx) | |
841 | { | |
842 | unsigned int i; | |
843 | u64 mask = BIT_ULL(line_idx); | |
844 | ||
845 | for (i = 0; i < lc->num_attrs; i++) { | |
846 | if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) && | |
847 | (lc->attrs[i].mask & mask)) | |
848 | return lc->attrs[i].attr.flags; | |
849 | } | |
850 | return lc->flags; | |
851 | } | |
852 | ||
853 | static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc, | |
854 | unsigned int line_idx) | |
855 | { | |
856 | unsigned int i; | |
857 | u64 mask = BIT_ULL(line_idx); | |
858 | ||
859 | for (i = 0; i < lc->num_attrs; i++) { | |
860 | if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) && | |
861 | (lc->attrs[i].mask & mask)) | |
862 | return !!(lc->attrs[i].attr.values & mask); | |
863 | } | |
864 | return 0; | |
865 | } | |
866 | ||
867 | static int gpio_v2_line_flags_validate(u64 flags) | |
868 | { | |
869 | /* Return an error if an unknown flag is set */ | |
870 | if (flags & ~GPIO_V2_LINE_VALID_FLAGS) | |
871 | return -EINVAL; | |
872 | ||
873 | /* | |
874 | * Do not allow both INPUT and OUTPUT flags to be set as they are | |
875 | * contradictory. | |
876 | */ | |
877 | if ((flags & GPIO_V2_LINE_FLAG_INPUT) && | |
878 | (flags & GPIO_V2_LINE_FLAG_OUTPUT)) | |
879 | return -EINVAL; | |
880 | ||
73e03419 KG |
881 | /* Edge detection requires explicit input. */ |
882 | if ((flags & GPIO_V2_LINE_EDGE_FLAGS) && | |
883 | !(flags & GPIO_V2_LINE_FLAG_INPUT)) | |
884 | return -EINVAL; | |
885 | ||
3c0d9c63 KG |
886 | /* |
887 | * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single | |
888 | * request. If the hardware actually supports enabling both at the | |
889 | * same time the electrical result would be disastrous. | |
890 | */ | |
891 | if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) && | |
892 | (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE)) | |
893 | return -EINVAL; | |
894 | ||
895 | /* Drive requires explicit output direction. */ | |
896 | if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) && | |
897 | !(flags & GPIO_V2_LINE_FLAG_OUTPUT)) | |
898 | return -EINVAL; | |
899 | ||
900 | /* Bias requires explicit direction. */ | |
901 | if ((flags & GPIO_V2_LINE_BIAS_FLAGS) && | |
902 | !(flags & GPIO_V2_LINE_DIRECTION_FLAGS)) | |
903 | return -EINVAL; | |
904 | ||
905 | /* Only one bias flag can be set. */ | |
906 | if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) && | |
907 | (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | | |
908 | GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) || | |
909 | ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) && | |
910 | (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) | |
911 | return -EINVAL; | |
912 | ||
913 | return 0; | |
914 | } | |
915 | ||
916 | static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, | |
917 | unsigned int num_lines) | |
918 | { | |
919 | unsigned int i; | |
920 | u64 flags; | |
921 | int ret; | |
922 | ||
923 | if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX) | |
924 | return -EINVAL; | |
925 | ||
926 | if (memchr_inv(lc->padding, 0, sizeof(lc->padding))) | |
927 | return -EINVAL; | |
928 | ||
929 | for (i = 0; i < num_lines; i++) { | |
930 | flags = gpio_v2_line_config_flags(lc, i); | |
931 | ret = gpio_v2_line_flags_validate(flags); | |
932 | if (ret) | |
933 | return ret; | |
65cff704 KG |
934 | |
935 | /* debounce requires explicit input */ | |
936 | if (gpio_v2_line_config_debounced(lc, i) && | |
937 | !(flags & GPIO_V2_LINE_FLAG_INPUT)) | |
938 | return -EINVAL; | |
3c0d9c63 KG |
939 | } |
940 | return 0; | |
941 | } | |
942 | ||
943 | static void gpio_v2_line_config_flags_to_desc_flags(u64 flags, | |
944 | unsigned long *flagsp) | |
945 | { | |
946 | assign_bit(FLAG_ACTIVE_LOW, flagsp, | |
947 | flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW); | |
948 | ||
949 | if (flags & GPIO_V2_LINE_FLAG_OUTPUT) | |
950 | set_bit(FLAG_IS_OUT, flagsp); | |
951 | else if (flags & GPIO_V2_LINE_FLAG_INPUT) | |
952 | clear_bit(FLAG_IS_OUT, flagsp); | |
953 | ||
73e03419 KG |
954 | assign_bit(FLAG_EDGE_RISING, flagsp, |
955 | flags & GPIO_V2_LINE_FLAG_EDGE_RISING); | |
956 | assign_bit(FLAG_EDGE_FALLING, flagsp, | |
957 | flags & GPIO_V2_LINE_FLAG_EDGE_FALLING); | |
958 | ||
3c0d9c63 KG |
959 | assign_bit(FLAG_OPEN_DRAIN, flagsp, |
960 | flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN); | |
961 | assign_bit(FLAG_OPEN_SOURCE, flagsp, | |
962 | flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE); | |
963 | ||
964 | assign_bit(FLAG_PULL_UP, flagsp, | |
965 | flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP); | |
966 | assign_bit(FLAG_PULL_DOWN, flagsp, | |
967 | flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN); | |
968 | assign_bit(FLAG_BIAS_DISABLE, flagsp, | |
969 | flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED); | |
970 | } | |
971 | ||
972 | static long linereq_get_values(struct linereq *lr, void __user *ip) | |
973 | { | |
974 | struct gpio_v2_line_values lv; | |
975 | DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); | |
976 | struct gpio_desc **descs; | |
977 | unsigned int i, didx, num_get; | |
65cff704 | 978 | bool val; |
3c0d9c63 KG |
979 | int ret; |
980 | ||
981 | /* NOTE: It's ok to read values of output lines. */ | |
982 | if (copy_from_user(&lv, ip, sizeof(lv))) | |
983 | return -EFAULT; | |
984 | ||
985 | for (num_get = 0, i = 0; i < lr->num_lines; i++) { | |
986 | if (lv.mask & BIT_ULL(i)) { | |
987 | num_get++; | |
988 | descs = &lr->lines[i].desc; | |
989 | } | |
990 | } | |
991 | ||
992 | if (num_get == 0) | |
993 | return -EINVAL; | |
994 | ||
995 | if (num_get != 1) { | |
996 | descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL); | |
997 | if (!descs) | |
998 | return -ENOMEM; | |
999 | for (didx = 0, i = 0; i < lr->num_lines; i++) { | |
1000 | if (lv.mask & BIT_ULL(i)) { | |
1001 | descs[didx] = lr->lines[i].desc; | |
1002 | didx++; | |
1003 | } | |
1004 | } | |
1005 | } | |
1006 | ret = gpiod_get_array_value_complex(false, true, num_get, | |
1007 | descs, NULL, vals); | |
1008 | ||
1009 | if (num_get != 1) | |
1010 | kfree(descs); | |
1011 | if (ret) | |
1012 | return ret; | |
1013 | ||
1014 | lv.bits = 0; | |
1015 | for (didx = 0, i = 0; i < lr->num_lines; i++) { | |
1016 | if (lv.mask & BIT_ULL(i)) { | |
65cff704 KG |
1017 | if (lr->lines[i].sw_debounced) |
1018 | val = debounced_value(&lr->lines[i]); | |
1019 | else | |
1020 | val = test_bit(didx, vals); | |
1021 | if (val) | |
3c0d9c63 KG |
1022 | lv.bits |= BIT_ULL(i); |
1023 | didx++; | |
1024 | } | |
1025 | } | |
1026 | ||
1027 | if (copy_to_user(ip, &lv, sizeof(lv))) | |
1028 | return -EFAULT; | |
1029 | ||
1030 | return 0; | |
1031 | } | |
1032 | ||
7b8e00d9 KG |
1033 | static long linereq_set_values_unlocked(struct linereq *lr, |
1034 | struct gpio_v2_line_values *lv) | |
1035 | { | |
1036 | DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); | |
1037 | struct gpio_desc **descs; | |
1038 | unsigned int i, didx, num_set; | |
1039 | int ret; | |
1040 | ||
1041 | bitmap_zero(vals, GPIO_V2_LINES_MAX); | |
1042 | for (num_set = 0, i = 0; i < lr->num_lines; i++) { | |
1043 | if (lv->mask & BIT_ULL(i)) { | |
1044 | if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags)) | |
1045 | return -EPERM; | |
1046 | if (lv->bits & BIT_ULL(i)) | |
1047 | __set_bit(num_set, vals); | |
1048 | num_set++; | |
1049 | descs = &lr->lines[i].desc; | |
1050 | } | |
1051 | } | |
1052 | if (num_set == 0) | |
1053 | return -EINVAL; | |
1054 | ||
1055 | if (num_set != 1) { | |
1056 | /* build compacted desc array and values */ | |
1057 | descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL); | |
1058 | if (!descs) | |
1059 | return -ENOMEM; | |
1060 | for (didx = 0, i = 0; i < lr->num_lines; i++) { | |
1061 | if (lv->mask & BIT_ULL(i)) { | |
1062 | descs[didx] = lr->lines[i].desc; | |
1063 | didx++; | |
1064 | } | |
1065 | } | |
1066 | } | |
1067 | ret = gpiod_set_array_value_complex(false, true, num_set, | |
1068 | descs, NULL, vals); | |
1069 | ||
1070 | if (num_set != 1) | |
1071 | kfree(descs); | |
1072 | return ret; | |
1073 | } | |
1074 | ||
1075 | static long linereq_set_values(struct linereq *lr, void __user *ip) | |
1076 | { | |
1077 | struct gpio_v2_line_values lv; | |
1078 | int ret; | |
925ca369 | 1079 | |
7b8e00d9 | 1080 | if (copy_from_user(&lv, ip, sizeof(lv))) |
925ca369 KG |
1081 | return -EFAULT; |
1082 | ||
7b8e00d9 | 1083 | mutex_lock(&lr->config_mutex); |
925ca369 | 1084 | |
7b8e00d9 KG |
1085 | ret = linereq_set_values_unlocked(lr, &lv); |
1086 | ||
1087 | mutex_unlock(&lr->config_mutex); | |
1088 | ||
1089 | return ret; | |
1090 | } | |
1091 | ||
a54756cb KG |
1092 | static long linereq_set_config_unlocked(struct linereq *lr, |
1093 | struct gpio_v2_line_config *lc) | |
1094 | { | |
1095 | struct gpio_desc *desc; | |
1096 | unsigned int i; | |
1097 | u64 flags; | |
1098 | bool polarity_change; | |
1099 | int ret; | |
1100 | ||
1101 | for (i = 0; i < lr->num_lines; i++) { | |
1102 | desc = lr->lines[i].desc; | |
1103 | flags = gpio_v2_line_config_flags(lc, i); | |
1104 | polarity_change = | |
1105 | (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) != | |
1106 | ((flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) != 0)); | |
925ca369 | 1107 | |
a54756cb | 1108 | gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); |
925ca369 KG |
1109 | /* |
1110 | * Lines have to be requested explicitly for input | |
1111 | * or output, else the line will be treated "as is". | |
1112 | */ | |
a54756cb KG |
1113 | if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { |
1114 | int val = gpio_v2_line_config_output_value(lc, i); | |
925ca369 | 1115 | |
a54756cb | 1116 | edge_detector_stop(&lr->lines[i]); |
925ca369 KG |
1117 | ret = gpiod_direction_output(desc, val); |
1118 | if (ret) | |
1119 | return ret; | |
a54756cb | 1120 | } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { |
925ca369 KG |
1121 | ret = gpiod_direction_input(desc); |
1122 | if (ret) | |
1123 | return ret; | |
a54756cb | 1124 | |
65cff704 | 1125 | ret = edge_detector_update(&lr->lines[i], lc, i, |
a54756cb KG |
1126 | flags & GPIO_V2_LINE_EDGE_FLAGS, |
1127 | polarity_change); | |
1128 | if (ret) | |
1129 | return ret; | |
925ca369 KG |
1130 | } |
1131 | ||
6accc376 | 1132 | blocking_notifier_call_chain(&desc->gdev->notifier, |
a54756cb KG |
1133 | GPIO_V2_LINE_CHANGED_CONFIG, |
1134 | desc); | |
925ca369 KG |
1135 | } |
1136 | return 0; | |
1137 | } | |
1138 | ||
a54756cb | 1139 | static long linereq_set_config(struct linereq *lr, void __user *ip) |
925ca369 | 1140 | { |
a54756cb KG |
1141 | struct gpio_v2_line_config lc; |
1142 | int ret; | |
925ca369 | 1143 | |
a54756cb KG |
1144 | if (copy_from_user(&lc, ip, sizeof(lc))) |
1145 | return -EFAULT; | |
925ca369 | 1146 | |
a54756cb KG |
1147 | ret = gpio_v2_line_config_validate(&lc, lr->num_lines); |
1148 | if (ret) | |
1149 | return ret; | |
925ca369 | 1150 | |
a54756cb | 1151 | mutex_lock(&lr->config_mutex); |
925ca369 | 1152 | |
a54756cb | 1153 | ret = linereq_set_config_unlocked(lr, &lc); |
925ca369 | 1154 | |
a54756cb | 1155 | mutex_unlock(&lr->config_mutex); |
925ca369 | 1156 | |
a54756cb KG |
1157 | return ret; |
1158 | } | |
1159 | ||
3c0d9c63 KG |
1160 | static long linereq_ioctl(struct file *file, unsigned int cmd, |
1161 | unsigned long arg) | |
1162 | { | |
1163 | struct linereq *lr = file->private_data; | |
1164 | void __user *ip = (void __user *)arg; | |
1165 | ||
1166 | if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL) | |
1167 | return linereq_get_values(lr, ip); | |
7b8e00d9 KG |
1168 | else if (cmd == GPIO_V2_LINE_SET_VALUES_IOCTL) |
1169 | return linereq_set_values(lr, ip); | |
a54756cb KG |
1170 | else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL) |
1171 | return linereq_set_config(lr, ip); | |
925ca369 | 1172 | |
925ca369 KG |
1173 | return -EINVAL; |
1174 | } | |
1175 | ||
1176 | #ifdef CONFIG_COMPAT | |
3c0d9c63 KG |
1177 | static long linereq_ioctl_compat(struct file *file, unsigned int cmd, |
1178 | unsigned long arg) | |
925ca369 | 1179 | { |
3c0d9c63 | 1180 | return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
925ca369 KG |
1181 | } |
1182 | #endif | |
1183 | ||
73e03419 KG |
1184 | static __poll_t linereq_poll(struct file *file, |
1185 | struct poll_table_struct *wait) | |
925ca369 | 1186 | { |
73e03419 KG |
1187 | struct linereq *lr = file->private_data; |
1188 | __poll_t events = 0; | |
925ca369 | 1189 | |
73e03419 KG |
1190 | poll_wait(file, &lr->wait, wait); |
1191 | ||
1192 | if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events, | |
1193 | &lr->wait.lock)) | |
1194 | events = EPOLLIN | EPOLLRDNORM; | |
1195 | ||
1196 | return events; | |
883f9198 KG |
1197 | } |
1198 | ||
73e03419 KG |
1199 | static ssize_t linereq_read(struct file *file, |
1200 | char __user *buf, | |
1201 | size_t count, | |
1202 | loff_t *f_ps) | |
883f9198 | 1203 | { |
73e03419 KG |
1204 | struct linereq *lr = file->private_data; |
1205 | struct gpio_v2_line_event le; | |
1206 | ssize_t bytes_read = 0; | |
1207 | int ret; | |
1208 | ||
1209 | if (count < sizeof(le)) | |
1210 | return -EINVAL; | |
1211 | ||
1212 | do { | |
1213 | spin_lock(&lr->wait.lock); | |
1214 | if (kfifo_is_empty(&lr->events)) { | |
1215 | if (bytes_read) { | |
1216 | spin_unlock(&lr->wait.lock); | |
1217 | return bytes_read; | |
1218 | } | |
1219 | ||
1220 | if (file->f_flags & O_NONBLOCK) { | |
1221 | spin_unlock(&lr->wait.lock); | |
1222 | return -EAGAIN; | |
1223 | } | |
1224 | ||
1225 | ret = wait_event_interruptible_locked(lr->wait, | |
1226 | !kfifo_is_empty(&lr->events)); | |
1227 | if (ret) { | |
1228 | spin_unlock(&lr->wait.lock); | |
1229 | return ret; | |
1230 | } | |
1231 | } | |
1232 | ||
1233 | ret = kfifo_out(&lr->events, &le, 1); | |
1234 | spin_unlock(&lr->wait.lock); | |
1235 | if (ret != 1) { | |
1236 | /* | |
1237 | * This should never happen - we were holding the | |
1238 | * lock from the moment we learned the fifo is no | |
1239 | * longer empty until now. | |
1240 | */ | |
1241 | ret = -EIO; | |
1242 | break; | |
1243 | } | |
1244 | ||
1245 | if (copy_to_user(buf + bytes_read, &le, sizeof(le))) | |
1246 | return -EFAULT; | |
1247 | bytes_read += sizeof(le); | |
1248 | } while (count >= bytes_read + sizeof(le)); | |
1249 | ||
1250 | return bytes_read; | |
1251 | } | |
1252 | ||
3c0d9c63 KG |
1253 | static void linereq_free(struct linereq *lr) |
1254 | { | |
1255 | unsigned int i; | |
1256 | ||
1257 | for (i = 0; i < lr->num_lines; i++) { | |
73e03419 | 1258 | edge_detector_stop(&lr->lines[i]); |
3c0d9c63 KG |
1259 | if (lr->lines[i].desc) |
1260 | gpiod_free(lr->lines[i].desc); | |
1261 | } | |
73e03419 | 1262 | kfifo_free(&lr->events); |
3c0d9c63 KG |
1263 | kfree(lr->label); |
1264 | put_device(&lr->gdev->dev); | |
1265 | kfree(lr); | |
1266 | } | |
1267 | ||
1268 | static int linereq_release(struct inode *inode, struct file *file) | |
1269 | { | |
1270 | struct linereq *lr = file->private_data; | |
1271 | ||
1272 | linereq_free(lr); | |
925ca369 KG |
1273 | return 0; |
1274 | } | |
1275 | ||
3c0d9c63 KG |
1276 | static const struct file_operations line_fileops = { |
1277 | .release = linereq_release, | |
73e03419 KG |
1278 | .read = linereq_read, |
1279 | .poll = linereq_poll, | |
925ca369 KG |
1280 | .owner = THIS_MODULE, |
1281 | .llseek = noop_llseek, | |
3c0d9c63 | 1282 | .unlocked_ioctl = linereq_ioctl, |
925ca369 | 1283 | #ifdef CONFIG_COMPAT |
3c0d9c63 | 1284 | .compat_ioctl = linereq_ioctl_compat, |
925ca369 KG |
1285 | #endif |
1286 | }; | |
1287 | ||
3c0d9c63 | 1288 | static int linereq_create(struct gpio_device *gdev, void __user *ip) |
925ca369 | 1289 | { |
3c0d9c63 KG |
1290 | struct gpio_v2_line_request ulr; |
1291 | struct gpio_v2_line_config *lc; | |
1292 | struct linereq *lr; | |
925ca369 | 1293 | struct file *file; |
3c0d9c63 KG |
1294 | u64 flags; |
1295 | unsigned int i; | |
1296 | int fd, ret; | |
925ca369 | 1297 | |
3c0d9c63 | 1298 | if (copy_from_user(&ulr, ip, sizeof(ulr))) |
925ca369 | 1299 | return -EFAULT; |
3c0d9c63 KG |
1300 | |
1301 | if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX)) | |
925ca369 KG |
1302 | return -EINVAL; |
1303 | ||
3c0d9c63 KG |
1304 | if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding))) |
1305 | return -EINVAL; | |
925ca369 | 1306 | |
3c0d9c63 KG |
1307 | lc = &ulr.config; |
1308 | ret = gpio_v2_line_config_validate(lc, ulr.num_lines); | |
925ca369 KG |
1309 | if (ret) |
1310 | return ret; | |
1311 | ||
3c0d9c63 KG |
1312 | lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); |
1313 | if (!lr) | |
925ca369 | 1314 | return -ENOMEM; |
3c0d9c63 KG |
1315 | |
1316 | lr->gdev = gdev; | |
925ca369 KG |
1317 | get_device(&gdev->dev); |
1318 | ||
65cff704 | 1319 | for (i = 0; i < ulr.num_lines; i++) { |
73e03419 | 1320 | lr->lines[i].req = lr; |
65cff704 KG |
1321 | WRITE_ONCE(lr->lines[i].sw_debounced, 0); |
1322 | INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func); | |
1323 | } | |
73e03419 | 1324 | |
f188ac12 | 1325 | if (ulr.consumer[0] != '\0') { |
3c0d9c63 | 1326 | /* label is only initialized if consumer is set */ |
f188ac12 KG |
1327 | lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1, |
1328 | GFP_KERNEL); | |
3c0d9c63 | 1329 | if (!lr->label) { |
925ca369 | 1330 | ret = -ENOMEM; |
3c0d9c63 | 1331 | goto out_free_linereq; |
925ca369 KG |
1332 | } |
1333 | } | |
1334 | ||
a54756cb | 1335 | mutex_init(&lr->config_mutex); |
73e03419 KG |
1336 | init_waitqueue_head(&lr->wait); |
1337 | lr->event_buffer_size = ulr.event_buffer_size; | |
1338 | if (lr->event_buffer_size == 0) | |
1339 | lr->event_buffer_size = ulr.num_lines * 16; | |
1340 | else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16) | |
1341 | lr->event_buffer_size = GPIO_V2_LINES_MAX * 16; | |
1342 | ||
1343 | atomic_set(&lr->seqno, 0); | |
3c0d9c63 | 1344 | lr->num_lines = ulr.num_lines; |
883f9198 | 1345 | |
925ca369 | 1346 | /* Request each GPIO */ |
3c0d9c63 KG |
1347 | for (i = 0; i < ulr.num_lines; i++) { |
1348 | u32 offset = ulr.offsets[i]; | |
925ca369 KG |
1349 | struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); |
1350 | ||
1351 | if (IS_ERR(desc)) { | |
1352 | ret = PTR_ERR(desc); | |
3c0d9c63 | 1353 | goto out_free_linereq; |
925ca369 KG |
1354 | } |
1355 | ||
3c0d9c63 | 1356 | ret = gpiod_request(desc, lr->label); |
925ca369 | 1357 | if (ret) |
3c0d9c63 KG |
1358 | goto out_free_linereq; |
1359 | ||
1360 | lr->lines[i].desc = desc; | |
1361 | flags = gpio_v2_line_config_flags(lc, i); | |
1362 | gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); | |
925ca369 KG |
1363 | |
1364 | ret = gpiod_set_transitory(desc, false); | |
1365 | if (ret < 0) | |
3c0d9c63 | 1366 | goto out_free_linereq; |
925ca369 KG |
1367 | |
1368 | /* | |
1369 | * Lines have to be requested explicitly for input | |
1370 | * or output, else the line will be treated "as is". | |
1371 | */ | |
3c0d9c63 KG |
1372 | if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { |
1373 | int val = gpio_v2_line_config_output_value(lc, i); | |
925ca369 KG |
1374 | |
1375 | ret = gpiod_direction_output(desc, val); | |
1376 | if (ret) | |
3c0d9c63 KG |
1377 | goto out_free_linereq; |
1378 | } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { | |
925ca369 KG |
1379 | ret = gpiod_direction_input(desc); |
1380 | if (ret) | |
3c0d9c63 | 1381 | goto out_free_linereq; |
73e03419 | 1382 | |
65cff704 | 1383 | ret = edge_detector_setup(&lr->lines[i], lc, i, |
73e03419 KG |
1384 | flags & GPIO_V2_LINE_EDGE_FLAGS); |
1385 | if (ret) | |
1386 | goto out_free_linereq; | |
925ca369 KG |
1387 | } |
1388 | ||
6accc376 | 1389 | blocking_notifier_call_chain(&desc->gdev->notifier, |
aad95584 | 1390 | GPIO_V2_LINE_CHANGED_REQUESTED, desc); |
925ca369 KG |
1391 | |
1392 | dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", | |
1393 | offset); | |
1394 | } | |
925ca369 KG |
1395 | |
1396 | fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); | |
1397 | if (fd < 0) { | |
1398 | ret = fd; | |
3c0d9c63 | 1399 | goto out_free_linereq; |
925ca369 KG |
1400 | } |
1401 | ||
3c0d9c63 | 1402 | file = anon_inode_getfile("gpio-line", &line_fileops, lr, |
925ca369 KG |
1403 | O_RDONLY | O_CLOEXEC); |
1404 | if (IS_ERR(file)) { | |
1405 | ret = PTR_ERR(file); | |
1406 | goto out_put_unused_fd; | |
1407 | } | |
1408 | ||
3c0d9c63 KG |
1409 | ulr.fd = fd; |
1410 | if (copy_to_user(ip, &ulr, sizeof(ulr))) { | |
925ca369 KG |
1411 | /* |
1412 | * fput() will trigger the release() callback, so do not go onto | |
1413 | * the regular error cleanup path here. | |
1414 | */ | |
1415 | fput(file); | |
1416 | put_unused_fd(fd); | |
1417 | return -EFAULT; | |
1418 | } | |
1419 | ||
1420 | fd_install(fd, file); | |
1421 | ||
1422 | dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", | |
3c0d9c63 | 1423 | lr->num_lines); |
925ca369 KG |
1424 | |
1425 | return 0; | |
1426 | ||
1427 | out_put_unused_fd: | |
1428 | put_unused_fd(fd); | |
3c0d9c63 KG |
1429 | out_free_linereq: |
1430 | linereq_free(lr); | |
925ca369 KG |
1431 | return ret; |
1432 | } | |
1433 | ||
3c0d9c63 | 1434 | #ifdef CONFIG_GPIO_CDEV_V1 |
925ca369 KG |
1435 | |
1436 | /* | |
1437 | * GPIO line event management | |
1438 | */ | |
1439 | ||
1440 | /** | |
1441 | * struct lineevent_state - contains the state of a userspace event | |
1442 | * @gdev: the GPIO device the event pertains to | |
1443 | * @label: consumer label used to tag descriptors | |
1444 | * @desc: the GPIO descriptor held by this event | |
1445 | * @eflags: the event flags this line was requested with | |
1446 | * @irq: the interrupt that trigger in response to events on this GPIO | |
1447 | * @wait: wait queue that handles blocking reads of events | |
1448 | * @events: KFIFO for the GPIO events | |
1449 | * @timestamp: cache for the timestamp storing it between hardirq | |
1450 | * and IRQ thread, used to bring the timestamp close to the actual | |
1451 | * event | |
1452 | */ | |
1453 | struct lineevent_state { | |
1454 | struct gpio_device *gdev; | |
1455 | const char *label; | |
1456 | struct gpio_desc *desc; | |
1457 | u32 eflags; | |
1458 | int irq; | |
1459 | wait_queue_head_t wait; | |
1460 | DECLARE_KFIFO(events, struct gpioevent_data, 16); | |
1461 | u64 timestamp; | |
1462 | }; | |
1463 | ||
1464 | #define GPIOEVENT_REQUEST_VALID_FLAGS \ | |
1465 | (GPIOEVENT_REQUEST_RISING_EDGE | \ | |
1466 | GPIOEVENT_REQUEST_FALLING_EDGE) | |
1467 | ||
49bc5279 | 1468 | static __poll_t lineevent_poll(struct file *file, |
a18512e3 | 1469 | struct poll_table_struct *wait) |
925ca369 | 1470 | { |
49bc5279 | 1471 | struct lineevent_state *le = file->private_data; |
925ca369 KG |
1472 | __poll_t events = 0; |
1473 | ||
49bc5279 | 1474 | poll_wait(file, &le->wait, wait); |
925ca369 KG |
1475 | |
1476 | if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock)) | |
1477 | events = EPOLLIN | EPOLLRDNORM; | |
1478 | ||
1479 | return events; | |
1480 | } | |
1481 | ||
5ad284ab AS |
1482 | static ssize_t lineevent_get_size(void) |
1483 | { | |
47e538d8 | 1484 | #if defined(CONFIG_X86_64) && !defined(CONFIG_UML) |
5ad284ab AS |
1485 | /* i386 has no padding after 'id' */ |
1486 | if (in_ia32_syscall()) { | |
1487 | struct compat_gpioeevent_data { | |
1488 | compat_u64 timestamp; | |
1489 | u32 id; | |
1490 | }; | |
1491 | ||
1492 | return sizeof(struct compat_gpioeevent_data); | |
1493 | } | |
1494 | #endif | |
1495 | return sizeof(struct gpioevent_data); | |
1496 | } | |
925ca369 | 1497 | |
49bc5279 | 1498 | static ssize_t lineevent_read(struct file *file, |
925ca369 KG |
1499 | char __user *buf, |
1500 | size_t count, | |
1501 | loff_t *f_ps) | |
1502 | { | |
49bc5279 | 1503 | struct lineevent_state *le = file->private_data; |
925ca369 KG |
1504 | struct gpioevent_data ge; |
1505 | ssize_t bytes_read = 0; | |
5ad284ab | 1506 | ssize_t ge_size; |
925ca369 KG |
1507 | int ret; |
1508 | ||
5ad284ab AS |
1509 | /* |
1510 | * When compatible system call is being used the struct gpioevent_data, | |
1511 | * in case of at least ia32, has different size due to the alignment | |
1512 | * differences. Because we have first member 64 bits followed by one of | |
1513 | * 32 bits there is no gap between them. The only difference is the | |
1514 | * padding at the end of the data structure. Hence, we calculate the | |
1515 | * actual sizeof() and pass this as an argument to copy_to_user() to | |
1516 | * drop unneeded bytes from the output. | |
1517 | */ | |
1518 | ge_size = lineevent_get_size(); | |
1519 | if (count < ge_size) | |
925ca369 KG |
1520 | return -EINVAL; |
1521 | ||
1522 | do { | |
1523 | spin_lock(&le->wait.lock); | |
1524 | if (kfifo_is_empty(&le->events)) { | |
1525 | if (bytes_read) { | |
1526 | spin_unlock(&le->wait.lock); | |
1527 | return bytes_read; | |
1528 | } | |
1529 | ||
49bc5279 | 1530 | if (file->f_flags & O_NONBLOCK) { |
925ca369 KG |
1531 | spin_unlock(&le->wait.lock); |
1532 | return -EAGAIN; | |
1533 | } | |
1534 | ||
1535 | ret = wait_event_interruptible_locked(le->wait, | |
1536 | !kfifo_is_empty(&le->events)); | |
1537 | if (ret) { | |
1538 | spin_unlock(&le->wait.lock); | |
1539 | return ret; | |
1540 | } | |
1541 | } | |
1542 | ||
1543 | ret = kfifo_out(&le->events, &ge, 1); | |
1544 | spin_unlock(&le->wait.lock); | |
1545 | if (ret != 1) { | |
1546 | /* | |
1547 | * This should never happen - we were holding the lock | |
1548 | * from the moment we learned the fifo is no longer | |
1549 | * empty until now. | |
1550 | */ | |
1551 | ret = -EIO; | |
1552 | break; | |
1553 | } | |
1554 | ||
5ad284ab | 1555 | if (copy_to_user(buf + bytes_read, &ge, ge_size)) |
925ca369 | 1556 | return -EFAULT; |
5ad284ab AS |
1557 | bytes_read += ge_size; |
1558 | } while (count >= bytes_read + ge_size); | |
925ca369 KG |
1559 | |
1560 | return bytes_read; | |
1561 | } | |
1562 | ||
46824272 | 1563 | static void lineevent_free(struct lineevent_state *le) |
925ca369 | 1564 | { |
46824272 KG |
1565 | if (le->irq) |
1566 | free_irq(le->irq, le); | |
1567 | if (le->desc) | |
1568 | gpiod_free(le->desc); | |
925ca369 | 1569 | kfree(le->label); |
46824272 | 1570 | put_device(&le->gdev->dev); |
925ca369 | 1571 | kfree(le); |
46824272 KG |
1572 | } |
1573 | ||
1574 | static int lineevent_release(struct inode *inode, struct file *file) | |
1575 | { | |
1576 | lineevent_free(file->private_data); | |
925ca369 KG |
1577 | return 0; |
1578 | } | |
1579 | ||
49bc5279 | 1580 | static long lineevent_ioctl(struct file *file, unsigned int cmd, |
925ca369 KG |
1581 | unsigned long arg) |
1582 | { | |
49bc5279 | 1583 | struct lineevent_state *le = file->private_data; |
925ca369 KG |
1584 | void __user *ip = (void __user *)arg; |
1585 | struct gpiohandle_data ghd; | |
1586 | ||
1587 | /* | |
1588 | * We can get the value for an event line but not set it, | |
1589 | * because it is input by definition. | |
1590 | */ | |
1591 | if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { | |
1592 | int val; | |
1593 | ||
1594 | memset(&ghd, 0, sizeof(ghd)); | |
1595 | ||
1596 | val = gpiod_get_value_cansleep(le->desc); | |
1597 | if (val < 0) | |
1598 | return val; | |
1599 | ghd.values[0] = val; | |
1600 | ||
1601 | if (copy_to_user(ip, &ghd, sizeof(ghd))) | |
1602 | return -EFAULT; | |
1603 | ||
1604 | return 0; | |
1605 | } | |
1606 | return -EINVAL; | |
1607 | } | |
1608 | ||
1609 | #ifdef CONFIG_COMPAT | |
49bc5279 | 1610 | static long lineevent_ioctl_compat(struct file *file, unsigned int cmd, |
925ca369 KG |
1611 | unsigned long arg) |
1612 | { | |
49bc5279 | 1613 | return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
925ca369 KG |
1614 | } |
1615 | #endif | |
1616 | ||
1617 | static const struct file_operations lineevent_fileops = { | |
1618 | .release = lineevent_release, | |
1619 | .read = lineevent_read, | |
1620 | .poll = lineevent_poll, | |
1621 | .owner = THIS_MODULE, | |
1622 | .llseek = noop_llseek, | |
1623 | .unlocked_ioctl = lineevent_ioctl, | |
1624 | #ifdef CONFIG_COMPAT | |
1625 | .compat_ioctl = lineevent_ioctl_compat, | |
1626 | #endif | |
1627 | }; | |
1628 | ||
1629 | static irqreturn_t lineevent_irq_thread(int irq, void *p) | |
1630 | { | |
1631 | struct lineevent_state *le = p; | |
1632 | struct gpioevent_data ge; | |
1633 | int ret; | |
1634 | ||
1635 | /* Do not leak kernel stack to userspace */ | |
1636 | memset(&ge, 0, sizeof(ge)); | |
1637 | ||
1638 | /* | |
1639 | * We may be running from a nested threaded interrupt in which case | |
1640 | * we didn't get the timestamp from lineevent_irq_handler(). | |
1641 | */ | |
1642 | if (!le->timestamp) | |
1643 | ge.timestamp = ktime_get_ns(); | |
1644 | else | |
1645 | ge.timestamp = le->timestamp; | |
1646 | ||
1647 | if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE | |
1648 | && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { | |
1649 | int level = gpiod_get_value_cansleep(le->desc); | |
1650 | ||
1651 | if (level) | |
1652 | /* Emit low-to-high event */ | |
1653 | ge.id = GPIOEVENT_EVENT_RISING_EDGE; | |
1654 | else | |
1655 | /* Emit high-to-low event */ | |
1656 | ge.id = GPIOEVENT_EVENT_FALLING_EDGE; | |
1657 | } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) { | |
1658 | /* Emit low-to-high event */ | |
1659 | ge.id = GPIOEVENT_EVENT_RISING_EDGE; | |
1660 | } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { | |
1661 | /* Emit high-to-low event */ | |
1662 | ge.id = GPIOEVENT_EVENT_FALLING_EDGE; | |
1663 | } else { | |
1664 | return IRQ_NONE; | |
1665 | } | |
1666 | ||
1667 | ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge, | |
1668 | 1, &le->wait.lock); | |
1669 | if (ret) | |
1670 | wake_up_poll(&le->wait, EPOLLIN); | |
1671 | else | |
1672 | pr_debug_ratelimited("event FIFO is full - event dropped\n"); | |
1673 | ||
1674 | return IRQ_HANDLED; | |
1675 | } | |
1676 | ||
1677 | static irqreturn_t lineevent_irq_handler(int irq, void *p) | |
1678 | { | |
1679 | struct lineevent_state *le = p; | |
1680 | ||
1681 | /* | |
1682 | * Just store the timestamp in hardirq context so we get it as | |
1683 | * close in time as possible to the actual event. | |
1684 | */ | |
1685 | le->timestamp = ktime_get_ns(); | |
1686 | ||
1687 | return IRQ_WAKE_THREAD; | |
1688 | } | |
1689 | ||
1690 | static int lineevent_create(struct gpio_device *gdev, void __user *ip) | |
1691 | { | |
1692 | struct gpioevent_request eventreq; | |
1693 | struct lineevent_state *le; | |
1694 | struct gpio_desc *desc; | |
1695 | struct file *file; | |
1696 | u32 offset; | |
1697 | u32 lflags; | |
1698 | u32 eflags; | |
1699 | int fd; | |
1700 | int ret; | |
46824272 | 1701 | int irq, irqflags = 0; |
925ca369 KG |
1702 | |
1703 | if (copy_from_user(&eventreq, ip, sizeof(eventreq))) | |
1704 | return -EFAULT; | |
1705 | ||
1706 | offset = eventreq.lineoffset; | |
1707 | lflags = eventreq.handleflags; | |
1708 | eflags = eventreq.eventflags; | |
1709 | ||
1710 | desc = gpiochip_get_desc(gdev->chip, offset); | |
1711 | if (IS_ERR(desc)) | |
1712 | return PTR_ERR(desc); | |
1713 | ||
1714 | /* Return an error if a unknown flag is set */ | |
1715 | if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || | |
1716 | (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) | |
1717 | return -EINVAL; | |
1718 | ||
1719 | /* This is just wrong: we don't look for events on output lines */ | |
1720 | if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) || | |
1721 | (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || | |
1722 | (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) | |
1723 | return -EINVAL; | |
1724 | ||
1725 | /* Only one bias flag can be set. */ | |
1726 | if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && | |
1727 | (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | | |
1728 | GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || | |
1729 | ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && | |
1730 | (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) | |
1731 | return -EINVAL; | |
1732 | ||
1733 | le = kzalloc(sizeof(*le), GFP_KERNEL); | |
1734 | if (!le) | |
1735 | return -ENOMEM; | |
1736 | le->gdev = gdev; | |
1737 | get_device(&gdev->dev); | |
1738 | ||
f188ac12 KG |
1739 | if (eventreq.consumer_label[0] != '\0') { |
1740 | /* label is only initialized if consumer_label is set */ | |
1741 | le->label = kstrndup(eventreq.consumer_label, | |
1742 | sizeof(eventreq.consumer_label) - 1, | |
1743 | GFP_KERNEL); | |
925ca369 KG |
1744 | if (!le->label) { |
1745 | ret = -ENOMEM; | |
1746 | goto out_free_le; | |
1747 | } | |
1748 | } | |
1749 | ||
1750 | ret = gpiod_request(desc, le->label); | |
1751 | if (ret) | |
46824272 | 1752 | goto out_free_le; |
925ca369 KG |
1753 | le->desc = desc; |
1754 | le->eflags = eflags; | |
1755 | ||
c274b58a | 1756 | linehandle_flags_to_desc_flags(lflags, &desc->flags); |
925ca369 KG |
1757 | |
1758 | ret = gpiod_direction_input(desc); | |
1759 | if (ret) | |
46824272 | 1760 | goto out_free_le; |
925ca369 | 1761 | |
6accc376 | 1762 | blocking_notifier_call_chain(&desc->gdev->notifier, |
aad95584 | 1763 | GPIO_V2_LINE_CHANGED_REQUESTED, desc); |
925ca369 | 1764 | |
46824272 KG |
1765 | irq = gpiod_to_irq(desc); |
1766 | if (irq <= 0) { | |
925ca369 | 1767 | ret = -ENODEV; |
46824272 | 1768 | goto out_free_le; |
925ca369 | 1769 | } |
46824272 | 1770 | le->irq = irq; |
925ca369 KG |
1771 | |
1772 | if (eflags & GPIOEVENT_REQUEST_RISING_EDGE) | |
1773 | irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? | |
1774 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; | |
1775 | if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE) | |
1776 | irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? | |
1777 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; | |
1778 | irqflags |= IRQF_ONESHOT; | |
1779 | ||
1780 | INIT_KFIFO(le->events); | |
1781 | init_waitqueue_head(&le->wait); | |
1782 | ||
1783 | /* Request a thread to read the events */ | |
1784 | ret = request_threaded_irq(le->irq, | |
a18512e3 KG |
1785 | lineevent_irq_handler, |
1786 | lineevent_irq_thread, | |
1787 | irqflags, | |
1788 | le->label, | |
1789 | le); | |
925ca369 | 1790 | if (ret) |
46824272 | 1791 | goto out_free_le; |
925ca369 KG |
1792 | |
1793 | fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); | |
1794 | if (fd < 0) { | |
1795 | ret = fd; | |
46824272 | 1796 | goto out_free_le; |
925ca369 KG |
1797 | } |
1798 | ||
1799 | file = anon_inode_getfile("gpio-event", | |
1800 | &lineevent_fileops, | |
1801 | le, | |
1802 | O_RDONLY | O_CLOEXEC); | |
1803 | if (IS_ERR(file)) { | |
1804 | ret = PTR_ERR(file); | |
1805 | goto out_put_unused_fd; | |
1806 | } | |
1807 | ||
1808 | eventreq.fd = fd; | |
1809 | if (copy_to_user(ip, &eventreq, sizeof(eventreq))) { | |
1810 | /* | |
1811 | * fput() will trigger the release() callback, so do not go onto | |
1812 | * the regular error cleanup path here. | |
1813 | */ | |
1814 | fput(file); | |
1815 | put_unused_fd(fd); | |
1816 | return -EFAULT; | |
1817 | } | |
1818 | ||
1819 | fd_install(fd, file); | |
1820 | ||
1821 | return 0; | |
1822 | ||
1823 | out_put_unused_fd: | |
1824 | put_unused_fd(fd); | |
925ca369 | 1825 | out_free_le: |
46824272 | 1826 | lineevent_free(le); |
925ca369 KG |
1827 | return ret; |
1828 | } | |
1829 | ||
aad95584 KG |
1830 | static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2, |
1831 | struct gpioline_info *info_v1) | |
1832 | { | |
1833 | u64 flagsv2 = info_v2->flags; | |
1834 | ||
1835 | memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name)); | |
1836 | memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer)); | |
1837 | info_v1->line_offset = info_v2->offset; | |
1838 | info_v1->flags = 0; | |
1839 | ||
1840 | if (flagsv2 & GPIO_V2_LINE_FLAG_USED) | |
1841 | info_v1->flags |= GPIOLINE_FLAG_KERNEL; | |
1842 | ||
1843 | if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT) | |
1844 | info_v1->flags |= GPIOLINE_FLAG_IS_OUT; | |
1845 | ||
1846 | if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW) | |
1847 | info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW; | |
1848 | ||
1849 | if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN) | |
1850 | info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN; | |
1851 | if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE) | |
1852 | info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE; | |
1853 | ||
1854 | if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP) | |
1855 | info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; | |
1856 | if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) | |
1857 | info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; | |
1858 | if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED) | |
1859 | info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE; | |
1860 | } | |
1861 | ||
1862 | static void gpio_v2_line_info_changed_to_v1( | |
1863 | struct gpio_v2_line_info_changed *lic_v2, | |
1864 | struct gpioline_info_changed *lic_v1) | |
1865 | { | |
1866 | gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info); | |
1867 | lic_v1->timestamp = lic_v2->timestamp_ns; | |
1868 | lic_v1->event_type = lic_v2->event_type; | |
1869 | } | |
1870 | ||
3c0d9c63 KG |
1871 | #endif /* CONFIG_GPIO_CDEV_V1 */ |
1872 | ||
925ca369 | 1873 | static void gpio_desc_to_lineinfo(struct gpio_desc *desc, |
aad95584 | 1874 | struct gpio_v2_line_info *info) |
925ca369 KG |
1875 | { |
1876 | struct gpio_chip *gc = desc->gdev->chip; | |
1877 | bool ok_for_pinctrl; | |
1878 | unsigned long flags; | |
65cff704 KG |
1879 | u32 debounce_period_us; |
1880 | unsigned int num_attrs = 0; | |
925ca369 | 1881 | |
69e4e136 | 1882 | memset(info, 0, sizeof(*info)); |
aad95584 | 1883 | info->offset = gpio_chip_hwgpio(desc); |
925ca369 KG |
1884 | |
1885 | /* | |
1886 | * This function takes a mutex so we must check this before taking | |
1887 | * the spinlock. | |
1888 | * | |
1889 | * FIXME: find a non-racy way to retrieve this information. Maybe a | |
1890 | * lock common to both frameworks? | |
1891 | */ | |
1892 | ok_for_pinctrl = | |
aad95584 | 1893 | pinctrl_gpio_can_use_line(gc->base + info->offset); |
925ca369 KG |
1894 | |
1895 | spin_lock_irqsave(&gpio_lock, flags); | |
1896 | ||
69e4e136 KG |
1897 | if (desc->name) |
1898 | strscpy(info->name, desc->name, sizeof(info->name)); | |
925ca369 | 1899 | |
69e4e136 KG |
1900 | if (desc->label) |
1901 | strscpy(info->consumer, desc->label, sizeof(info->consumer)); | |
925ca369 KG |
1902 | |
1903 | /* | |
1904 | * Userspace only need to know that the kernel is using this GPIO so | |
1905 | * it can't use it. | |
1906 | */ | |
1907 | info->flags = 0; | |
1908 | if (test_bit(FLAG_REQUESTED, &desc->flags) || | |
1909 | test_bit(FLAG_IS_HOGGED, &desc->flags) || | |
1910 | test_bit(FLAG_USED_AS_IRQ, &desc->flags) || | |
1911 | test_bit(FLAG_EXPORT, &desc->flags) || | |
1912 | test_bit(FLAG_SYSFS, &desc->flags) || | |
1913 | !ok_for_pinctrl) | |
aad95584 KG |
1914 | info->flags |= GPIO_V2_LINE_FLAG_USED; |
1915 | ||
925ca369 | 1916 | if (test_bit(FLAG_IS_OUT, &desc->flags)) |
aad95584 KG |
1917 | info->flags |= GPIO_V2_LINE_FLAG_OUTPUT; |
1918 | else | |
1919 | info->flags |= GPIO_V2_LINE_FLAG_INPUT; | |
1920 | ||
925ca369 | 1921 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
aad95584 KG |
1922 | info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW; |
1923 | ||
925ca369 | 1924 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
aad95584 | 1925 | info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; |
925ca369 | 1926 | if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
aad95584 KG |
1927 | info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; |
1928 | ||
925ca369 | 1929 | if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) |
aad95584 | 1930 | info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED; |
925ca369 | 1931 | if (test_bit(FLAG_PULL_DOWN, &desc->flags)) |
aad95584 | 1932 | info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN; |
925ca369 | 1933 | if (test_bit(FLAG_PULL_UP, &desc->flags)) |
aad95584 | 1934 | info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; |
925ca369 | 1935 | |
73e03419 KG |
1936 | if (test_bit(FLAG_EDGE_RISING, &desc->flags)) |
1937 | info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; | |
1938 | if (test_bit(FLAG_EDGE_FALLING, &desc->flags)) | |
1939 | info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; | |
1940 | ||
65cff704 KG |
1941 | debounce_period_us = READ_ONCE(desc->debounce_period_us); |
1942 | if (debounce_period_us) { | |
1943 | info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; | |
1944 | info->attrs[num_attrs].debounce_period_us = debounce_period_us; | |
1945 | num_attrs++; | |
1946 | } | |
1947 | info->num_attrs = num_attrs; | |
925ca369 KG |
1948 | |
1949 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1950 | } | |
1951 | ||
1952 | struct gpio_chardev_data { | |
1953 | struct gpio_device *gdev; | |
1954 | wait_queue_head_t wait; | |
aad95584 | 1955 | DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32); |
925ca369 KG |
1956 | struct notifier_block lineinfo_changed_nb; |
1957 | unsigned long *watched_lines; | |
aad95584 KG |
1958 | #ifdef CONFIG_GPIO_CDEV_V1 |
1959 | atomic_t watch_abi_version; | |
1960 | #endif | |
925ca369 KG |
1961 | }; |
1962 | ||
aad95584 KG |
1963 | #ifdef CONFIG_GPIO_CDEV_V1 |
1964 | /* | |
1965 | * returns 0 if the versions match, else the previously selected ABI version | |
1966 | */ | |
1967 | static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata, | |
1968 | unsigned int version) | |
1969 | { | |
1970 | int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version); | |
1971 | ||
1972 | if (abiv == version) | |
1973 | return 0; | |
1974 | ||
1975 | return abiv; | |
1976 | } | |
1977 | #endif | |
1978 | ||
1979 | static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip, | |
1980 | bool watch) | |
1981 | { | |
1982 | struct gpio_desc *desc; | |
1983 | struct gpio_v2_line_info lineinfo; | |
1984 | ||
1985 | if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) | |
1986 | return -EFAULT; | |
1987 | ||
1988 | if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding))) | |
1989 | return -EINVAL; | |
1990 | ||
1991 | desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset); | |
1992 | if (IS_ERR(desc)) | |
1993 | return PTR_ERR(desc); | |
1994 | ||
1995 | if (watch) { | |
1996 | #ifdef CONFIG_GPIO_CDEV_V1 | |
1997 | if (lineinfo_ensure_abi_version(cdev, 2)) | |
1998 | return -EPERM; | |
1999 | #endif | |
2000 | if (test_and_set_bit(lineinfo.offset, cdev->watched_lines)) | |
2001 | return -EBUSY; | |
2002 | } | |
2003 | gpio_desc_to_lineinfo(desc, &lineinfo); | |
2004 | ||
2005 | if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { | |
2006 | if (watch) | |
2007 | clear_bit(lineinfo.offset, cdev->watched_lines); | |
2008 | return -EFAULT; | |
2009 | } | |
2010 | ||
2011 | return 0; | |
2012 | } | |
2013 | ||
925ca369 KG |
2014 | /* |
2015 | * gpio_ioctl() - ioctl handler for the GPIO chardev | |
2016 | */ | |
49bc5279 | 2017 | static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
925ca369 | 2018 | { |
e2b781c5 KG |
2019 | struct gpio_chardev_data *cdev = file->private_data; |
2020 | struct gpio_device *gdev = cdev->gdev; | |
925ca369 KG |
2021 | struct gpio_chip *gc = gdev->chip; |
2022 | void __user *ip = (void __user *)arg; | |
925ca369 | 2023 | __u32 offset; |
925ca369 KG |
2024 | |
2025 | /* We fail any subsequent ioctl():s when the chip is gone */ | |
2026 | if (!gc) | |
2027 | return -ENODEV; | |
2028 | ||
2029 | /* Fill in the struct and pass to userspace */ | |
2030 | if (cmd == GPIO_GET_CHIPINFO_IOCTL) { | |
2031 | struct gpiochip_info chipinfo; | |
2032 | ||
2033 | memset(&chipinfo, 0, sizeof(chipinfo)); | |
2034 | ||
69e4e136 | 2035 | strscpy(chipinfo.name, dev_name(&gdev->dev), |
925ca369 | 2036 | sizeof(chipinfo.name)); |
69e4e136 | 2037 | strscpy(chipinfo.label, gdev->label, |
925ca369 | 2038 | sizeof(chipinfo.label)); |
925ca369 KG |
2039 | chipinfo.lines = gdev->ngpio; |
2040 | if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) | |
2041 | return -EFAULT; | |
2042 | return 0; | |
3c0d9c63 | 2043 | #ifdef CONFIG_GPIO_CDEV_V1 |
925ca369 | 2044 | } else if (cmd == GPIO_GET_LINEINFO_IOCTL) { |
aad95584 | 2045 | struct gpio_desc *desc; |
925ca369 | 2046 | struct gpioline_info lineinfo; |
aad95584 | 2047 | struct gpio_v2_line_info lineinfo_v2; |
925ca369 KG |
2048 | |
2049 | if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) | |
2050 | return -EFAULT; | |
2051 | ||
1bf7ba40 | 2052 | /* this doubles as a range check on line_offset */ |
925ca369 KG |
2053 | desc = gpiochip_get_desc(gc, lineinfo.line_offset); |
2054 | if (IS_ERR(desc)) | |
2055 | return PTR_ERR(desc); | |
2056 | ||
aad95584 KG |
2057 | gpio_desc_to_lineinfo(desc, &lineinfo_v2); |
2058 | gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); | |
925ca369 KG |
2059 | |
2060 | if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) | |
2061 | return -EFAULT; | |
2062 | return 0; | |
2063 | } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) { | |
2064 | return linehandle_create(gdev, ip); | |
2065 | } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) { | |
2066 | return lineevent_create(gdev, ip); | |
2067 | } else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) { | |
aad95584 | 2068 | struct gpio_desc *desc; |
925ca369 | 2069 | struct gpioline_info lineinfo; |
aad95584 | 2070 | struct gpio_v2_line_info lineinfo_v2; |
925ca369 KG |
2071 | |
2072 | if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) | |
2073 | return -EFAULT; | |
2074 | ||
1bf7ba40 | 2075 | /* this doubles as a range check on line_offset */ |
925ca369 KG |
2076 | desc = gpiochip_get_desc(gc, lineinfo.line_offset); |
2077 | if (IS_ERR(desc)) | |
2078 | return PTR_ERR(desc); | |
2079 | ||
aad95584 KG |
2080 | if (lineinfo_ensure_abi_version(cdev, 1)) |
2081 | return -EPERM; | |
2082 | ||
1bf7ba40 | 2083 | if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines)) |
925ca369 KG |
2084 | return -EBUSY; |
2085 | ||
aad95584 KG |
2086 | gpio_desc_to_lineinfo(desc, &lineinfo_v2); |
2087 | gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); | |
925ca369 | 2088 | |
f30ef3e8 | 2089 | if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { |
1bf7ba40 | 2090 | clear_bit(lineinfo.line_offset, cdev->watched_lines); |
925ca369 | 2091 | return -EFAULT; |
f30ef3e8 | 2092 | } |
925ca369 | 2093 | |
925ca369 | 2094 | return 0; |
3c0d9c63 | 2095 | #endif /* CONFIG_GPIO_CDEV_V1 */ |
aad95584 KG |
2096 | } else if (cmd == GPIO_V2_GET_LINEINFO_IOCTL || |
2097 | cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL) { | |
2098 | return lineinfo_get(cdev, ip, | |
2099 | cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL); | |
3c0d9c63 KG |
2100 | } else if (cmd == GPIO_V2_GET_LINE_IOCTL) { |
2101 | return linereq_create(gdev, ip); | |
925ca369 KG |
2102 | } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) { |
2103 | if (copy_from_user(&offset, ip, sizeof(offset))) | |
2104 | return -EFAULT; | |
2105 | ||
1bf7ba40 KG |
2106 | if (offset >= cdev->gdev->ngpio) |
2107 | return -EINVAL; | |
925ca369 | 2108 | |
1bf7ba40 | 2109 | if (!test_and_clear_bit(offset, cdev->watched_lines)) |
925ca369 KG |
2110 | return -EBUSY; |
2111 | ||
925ca369 KG |
2112 | return 0; |
2113 | } | |
2114 | return -EINVAL; | |
2115 | } | |
2116 | ||
2117 | #ifdef CONFIG_COMPAT | |
49bc5279 | 2118 | static long gpio_ioctl_compat(struct file *file, unsigned int cmd, |
925ca369 KG |
2119 | unsigned long arg) |
2120 | { | |
49bc5279 | 2121 | return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
925ca369 KG |
2122 | } |
2123 | #endif | |
2124 | ||
2125 | static struct gpio_chardev_data * | |
2126 | to_gpio_chardev_data(struct notifier_block *nb) | |
2127 | { | |
2128 | return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb); | |
2129 | } | |
2130 | ||
2131 | static int lineinfo_changed_notify(struct notifier_block *nb, | |
2132 | unsigned long action, void *data) | |
2133 | { | |
e2b781c5 | 2134 | struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb); |
aad95584 | 2135 | struct gpio_v2_line_info_changed chg; |
925ca369 KG |
2136 | struct gpio_desc *desc = data; |
2137 | int ret; | |
2138 | ||
e2b781c5 | 2139 | if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) |
925ca369 KG |
2140 | return NOTIFY_DONE; |
2141 | ||
2142 | memset(&chg, 0, sizeof(chg)); | |
925ca369 | 2143 | chg.event_type = action; |
aad95584 | 2144 | chg.timestamp_ns = ktime_get_ns(); |
925ca369 KG |
2145 | gpio_desc_to_lineinfo(desc, &chg.info); |
2146 | ||
e2b781c5 | 2147 | ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); |
925ca369 | 2148 | if (ret) |
e2b781c5 | 2149 | wake_up_poll(&cdev->wait, EPOLLIN); |
925ca369 KG |
2150 | else |
2151 | pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n"); | |
2152 | ||
2153 | return NOTIFY_OK; | |
2154 | } | |
2155 | ||
49bc5279 | 2156 | static __poll_t lineinfo_watch_poll(struct file *file, |
925ca369 KG |
2157 | struct poll_table_struct *pollt) |
2158 | { | |
e2b781c5 | 2159 | struct gpio_chardev_data *cdev = file->private_data; |
925ca369 KG |
2160 | __poll_t events = 0; |
2161 | ||
e2b781c5 | 2162 | poll_wait(file, &cdev->wait, pollt); |
925ca369 | 2163 | |
e2b781c5 KG |
2164 | if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events, |
2165 | &cdev->wait.lock)) | |
925ca369 KG |
2166 | events = EPOLLIN | EPOLLRDNORM; |
2167 | ||
2168 | return events; | |
2169 | } | |
2170 | ||
49bc5279 | 2171 | static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, |
925ca369 KG |
2172 | size_t count, loff_t *off) |
2173 | { | |
e2b781c5 | 2174 | struct gpio_chardev_data *cdev = file->private_data; |
aad95584 | 2175 | struct gpio_v2_line_info_changed event; |
925ca369 KG |
2176 | ssize_t bytes_read = 0; |
2177 | int ret; | |
aad95584 | 2178 | size_t event_size; |
925ca369 | 2179 | |
aad95584 KG |
2180 | #ifndef CONFIG_GPIO_CDEV_V1 |
2181 | event_size = sizeof(struct gpio_v2_line_info_changed); | |
2182 | if (count < event_size) | |
925ca369 | 2183 | return -EINVAL; |
aad95584 | 2184 | #endif |
925ca369 KG |
2185 | |
2186 | do { | |
e2b781c5 KG |
2187 | spin_lock(&cdev->wait.lock); |
2188 | if (kfifo_is_empty(&cdev->events)) { | |
925ca369 | 2189 | if (bytes_read) { |
e2b781c5 | 2190 | spin_unlock(&cdev->wait.lock); |
925ca369 KG |
2191 | return bytes_read; |
2192 | } | |
2193 | ||
49bc5279 | 2194 | if (file->f_flags & O_NONBLOCK) { |
e2b781c5 | 2195 | spin_unlock(&cdev->wait.lock); |
925ca369 KG |
2196 | return -EAGAIN; |
2197 | } | |
2198 | ||
e2b781c5 KG |
2199 | ret = wait_event_interruptible_locked(cdev->wait, |
2200 | !kfifo_is_empty(&cdev->events)); | |
925ca369 | 2201 | if (ret) { |
e2b781c5 | 2202 | spin_unlock(&cdev->wait.lock); |
925ca369 KG |
2203 | return ret; |
2204 | } | |
2205 | } | |
aad95584 KG |
2206 | #ifdef CONFIG_GPIO_CDEV_V1 |
2207 | /* must be after kfifo check so watch_abi_version is set */ | |
2208 | if (atomic_read(&cdev->watch_abi_version) == 2) | |
2209 | event_size = sizeof(struct gpio_v2_line_info_changed); | |
2210 | else | |
2211 | event_size = sizeof(struct gpioline_info_changed); | |
2212 | if (count < event_size) { | |
2213 | spin_unlock(&cdev->wait.lock); | |
2214 | return -EINVAL; | |
2215 | } | |
2216 | #endif | |
e2b781c5 KG |
2217 | ret = kfifo_out(&cdev->events, &event, 1); |
2218 | spin_unlock(&cdev->wait.lock); | |
925ca369 KG |
2219 | if (ret != 1) { |
2220 | ret = -EIO; | |
2221 | break; | |
2222 | /* We should never get here. See lineevent_read(). */ | |
2223 | } | |
2224 | ||
aad95584 KG |
2225 | #ifdef CONFIG_GPIO_CDEV_V1 |
2226 | if (event_size == sizeof(struct gpio_v2_line_info_changed)) { | |
2227 | if (copy_to_user(buf + bytes_read, &event, event_size)) | |
2228 | return -EFAULT; | |
2229 | } else { | |
2230 | struct gpioline_info_changed event_v1; | |
2231 | ||
2232 | gpio_v2_line_info_changed_to_v1(&event, &event_v1); | |
2233 | if (copy_to_user(buf + bytes_read, &event_v1, | |
2234 | event_size)) | |
2235 | return -EFAULT; | |
2236 | } | |
2237 | #else | |
2238 | if (copy_to_user(buf + bytes_read, &event, event_size)) | |
925ca369 | 2239 | return -EFAULT; |
aad95584 KG |
2240 | #endif |
2241 | bytes_read += event_size; | |
925ca369 KG |
2242 | } while (count >= bytes_read + sizeof(event)); |
2243 | ||
2244 | return bytes_read; | |
2245 | } | |
2246 | ||
2247 | /** | |
2248 | * gpio_chrdev_open() - open the chardev for ioctl operations | |
2249 | * @inode: inode for this chardev | |
49bc5279 | 2250 | * @file: file struct for storing private data |
925ca369 KG |
2251 | * Returns 0 on success |
2252 | */ | |
49bc5279 | 2253 | static int gpio_chrdev_open(struct inode *inode, struct file *file) |
925ca369 KG |
2254 | { |
2255 | struct gpio_device *gdev = container_of(inode->i_cdev, | |
a18512e3 | 2256 | struct gpio_device, chrdev); |
e2b781c5 | 2257 | struct gpio_chardev_data *cdev; |
925ca369 KG |
2258 | int ret = -ENOMEM; |
2259 | ||
2260 | /* Fail on open if the backing gpiochip is gone */ | |
2261 | if (!gdev->chip) | |
2262 | return -ENODEV; | |
2263 | ||
e2b781c5 KG |
2264 | cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); |
2265 | if (!cdev) | |
925ca369 KG |
2266 | return -ENOMEM; |
2267 | ||
e2b781c5 KG |
2268 | cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL); |
2269 | if (!cdev->watched_lines) | |
2270 | goto out_free_cdev; | |
925ca369 | 2271 | |
e2b781c5 KG |
2272 | init_waitqueue_head(&cdev->wait); |
2273 | INIT_KFIFO(cdev->events); | |
2274 | cdev->gdev = gdev; | |
925ca369 | 2275 | |
e2b781c5 | 2276 | cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify; |
6accc376 | 2277 | ret = blocking_notifier_chain_register(&gdev->notifier, |
e2b781c5 | 2278 | &cdev->lineinfo_changed_nb); |
925ca369 KG |
2279 | if (ret) |
2280 | goto out_free_bitmap; | |
2281 | ||
2282 | get_device(&gdev->dev); | |
e2b781c5 | 2283 | file->private_data = cdev; |
925ca369 | 2284 | |
49bc5279 | 2285 | ret = nonseekable_open(inode, file); |
925ca369 KG |
2286 | if (ret) |
2287 | goto out_unregister_notifier; | |
2288 | ||
2289 | return ret; | |
2290 | ||
2291 | out_unregister_notifier: | |
6accc376 | 2292 | blocking_notifier_chain_unregister(&gdev->notifier, |
e2b781c5 | 2293 | &cdev->lineinfo_changed_nb); |
925ca369 | 2294 | out_free_bitmap: |
e2b781c5 KG |
2295 | bitmap_free(cdev->watched_lines); |
2296 | out_free_cdev: | |
2297 | kfree(cdev); | |
925ca369 KG |
2298 | return ret; |
2299 | } | |
2300 | ||
2301 | /** | |
2302 | * gpio_chrdev_release() - close chardev after ioctl operations | |
2303 | * @inode: inode for this chardev | |
49bc5279 | 2304 | * @file: file struct for storing private data |
925ca369 KG |
2305 | * Returns 0 on success |
2306 | */ | |
49bc5279 | 2307 | static int gpio_chrdev_release(struct inode *inode, struct file *file) |
925ca369 | 2308 | { |
e2b781c5 KG |
2309 | struct gpio_chardev_data *cdev = file->private_data; |
2310 | struct gpio_device *gdev = cdev->gdev; | |
925ca369 | 2311 | |
e2b781c5 | 2312 | bitmap_free(cdev->watched_lines); |
6accc376 | 2313 | blocking_notifier_chain_unregister(&gdev->notifier, |
e2b781c5 | 2314 | &cdev->lineinfo_changed_nb); |
925ca369 | 2315 | put_device(&gdev->dev); |
e2b781c5 | 2316 | kfree(cdev); |
925ca369 KG |
2317 | |
2318 | return 0; | |
2319 | } | |
2320 | ||
2321 | static const struct file_operations gpio_fileops = { | |
2322 | .release = gpio_chrdev_release, | |
2323 | .open = gpio_chrdev_open, | |
2324 | .poll = lineinfo_watch_poll, | |
2325 | .read = lineinfo_watch_read, | |
2326 | .owner = THIS_MODULE, | |
2327 | .llseek = no_llseek, | |
2328 | .unlocked_ioctl = gpio_ioctl, | |
2329 | #ifdef CONFIG_COMPAT | |
2330 | .compat_ioctl = gpio_ioctl_compat, | |
2331 | #endif | |
2332 | }; | |
2333 | ||
2334 | int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) | |
2335 | { | |
2336 | int ret; | |
2337 | ||
2338 | cdev_init(&gdev->chrdev, &gpio_fileops); | |
2339 | gdev->chrdev.owner = THIS_MODULE; | |
2340 | gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id); | |
2341 | ||
2342 | ret = cdev_device_add(&gdev->chrdev, &gdev->dev); | |
2343 | if (ret) | |
2344 | return ret; | |
2345 | ||
2346 | chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n", | |
2347 | MAJOR(devt), gdev->id); | |
2348 | ||
2349 | return 0; | |
2350 | } | |
2351 | ||
2352 | void gpiolib_cdev_unregister(struct gpio_device *gdev) | |
2353 | { | |
2354 | cdev_device_del(&gdev->chrdev, &gdev->dev); | |
2355 | } |