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 | 7 | #include <linux/cdev.h> |
9344e34e | 8 | #include <linux/cleanup.h> |
d189f627 | 9 | #include <linux/compat.h> |
65cff704 | 10 | #include <linux/compiler.h> |
925ca369 KG |
11 | #include <linux/device.h> |
12 | #include <linux/err.h> | |
d189f627 | 13 | #include <linux/file.h> |
925ca369 KG |
14 | #include <linux/gpio.h> |
15 | #include <linux/gpio/driver.h> | |
52ee7c02 | 16 | #include <linux/hte.h> |
d189f627 KG |
17 | #include <linux/interrupt.h> |
18 | #include <linux/irqreturn.h> | |
19 | #include <linux/kernel.h> | |
925ca369 | 20 | #include <linux/kfifo.h> |
d189f627 | 21 | #include <linux/module.h> |
a54756cb | 22 | #include <linux/mutex.h> |
ede7511e | 23 | #include <linux/overflow.h> |
d189f627 | 24 | #include <linux/pinctrl/consumer.h> |
925ca369 | 25 | #include <linux/poll.h> |
9344e34e | 26 | #include <linux/rbtree.h> |
52ee7c02 | 27 | #include <linux/seq_file.h> |
d189f627 | 28 | #include <linux/spinlock.h> |
925ca369 | 29 | #include <linux/timekeeping.h> |
d189f627 | 30 | #include <linux/uaccess.h> |
65cff704 | 31 | #include <linux/workqueue.h> |
52ee7c02 | 32 | |
925ca369 KG |
33 | #include <uapi/linux/gpio.h> |
34 | ||
35 | #include "gpiolib.h" | |
36 | #include "gpiolib-cdev.h" | |
37 | ||
3c0d9c63 KG |
38 | /* |
39 | * Array sizes must ensure 64-bit alignment and not create holes in the | |
40 | * struct packing. | |
41 | */ | |
42 | static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2)); | |
43 | static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8)); | |
44 | ||
45 | /* | |
46 | * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility | |
47 | */ | |
48 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8)); | |
49 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8)); | |
50 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8)); | |
51 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8)); | |
52 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8)); | |
53 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8)); | |
54 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8)); | |
55 | static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8)); | |
56 | ||
925ca369 KG |
57 | /* Character device interface to GPIO. |
58 | * | |
59 | * The GPIO character device, /dev/gpiochipN, provides userspace an | |
60 | * interface to gpiolib GPIOs via ioctl()s. | |
61 | */ | |
62 | ||
63 | /* | |
64 | * GPIO line handle management | |
65 | */ | |
66 | ||
3c0d9c63 | 67 | #ifdef CONFIG_GPIO_CDEV_V1 |
925ca369 KG |
68 | /** |
69 | * struct linehandle_state - contains the state of a userspace handle | |
70 | * @gdev: the GPIO device the handle pertains to | |
71 | * @label: consumer label used to tag descriptors | |
72 | * @descs: the GPIO descriptors held by this handle | |
52b7b596 | 73 | * @num_descs: the number of descriptors held in the descs array |
925ca369 KG |
74 | */ |
75 | struct linehandle_state { | |
76 | struct gpio_device *gdev; | |
77 | const char *label; | |
78 | struct gpio_desc *descs[GPIOHANDLES_MAX]; | |
52b7b596 | 79 | u32 num_descs; |
925ca369 KG |
80 | }; |
81 | ||
82 | #define GPIOHANDLE_REQUEST_VALID_FLAGS \ | |
83 | (GPIOHANDLE_REQUEST_INPUT | \ | |
84 | GPIOHANDLE_REQUEST_OUTPUT | \ | |
85 | GPIOHANDLE_REQUEST_ACTIVE_LOW | \ | |
86 | GPIOHANDLE_REQUEST_BIAS_PULL_UP | \ | |
87 | GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \ | |
88 | GPIOHANDLE_REQUEST_BIAS_DISABLE | \ | |
89 | GPIOHANDLE_REQUEST_OPEN_DRAIN | \ | |
90 | GPIOHANDLE_REQUEST_OPEN_SOURCE) | |
91 | ||
92 | static int linehandle_validate_flags(u32 flags) | |
93 | { | |
94 | /* Return an error if an unknown flag is set */ | |
95 | if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) | |
96 | return -EINVAL; | |
97 | ||
98 | /* | |
99 | * Do not allow both INPUT & OUTPUT flags to be set as they are | |
100 | * contradictory. | |
101 | */ | |
102 | if ((flags & GPIOHANDLE_REQUEST_INPUT) && | |
103 | (flags & GPIOHANDLE_REQUEST_OUTPUT)) | |
104 | return -EINVAL; | |
105 | ||
106 | /* | |
107 | * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If | |
108 | * the hardware actually supports enabling both at the same time the | |
109 | * electrical result would be disastrous. | |
110 | */ | |
111 | if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) && | |
112 | (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) | |
113 | return -EINVAL; | |
114 | ||
115 | /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */ | |
116 | if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) && | |
117 | ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || | |
118 | (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))) | |
119 | return -EINVAL; | |
120 | ||
121 | /* Bias flags only allowed for input or output mode. */ | |
122 | if (!((flags & GPIOHANDLE_REQUEST_INPUT) || | |
123 | (flags & GPIOHANDLE_REQUEST_OUTPUT)) && | |
124 | ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) || | |
125 | (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) || | |
126 | (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN))) | |
127 | return -EINVAL; | |
128 | ||
129 | /* Only one bias flag can be set. */ | |
130 | if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && | |
131 | (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | | |
a18512e3 | 132 | GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || |
925ca369 KG |
133 | ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && |
134 | (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) | |
135 | return -EINVAL; | |
136 | ||
137 | return 0; | |
138 | } | |
139 | ||
c274b58a KG |
140 | static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp) |
141 | { | |
142 | assign_bit(FLAG_ACTIVE_LOW, flagsp, | |
143 | lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW); | |
144 | assign_bit(FLAG_OPEN_DRAIN, flagsp, | |
145 | lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN); | |
146 | assign_bit(FLAG_OPEN_SOURCE, flagsp, | |
147 | lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE); | |
148 | assign_bit(FLAG_PULL_UP, flagsp, | |
149 | lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP); | |
150 | assign_bit(FLAG_PULL_DOWN, flagsp, | |
151 | lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN); | |
152 | assign_bit(FLAG_BIAS_DISABLE, flagsp, | |
153 | lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE); | |
154 | } | |
155 | ||
925ca369 KG |
156 | static long linehandle_set_config(struct linehandle_state *lh, |
157 | void __user *ip) | |
158 | { | |
159 | struct gpiohandle_config gcnf; | |
160 | struct gpio_desc *desc; | |
161 | int i, ret; | |
162 | u32 lflags; | |
925ca369 KG |
163 | |
164 | if (copy_from_user(&gcnf, ip, sizeof(gcnf))) | |
165 | return -EFAULT; | |
166 | ||
167 | lflags = gcnf.flags; | |
168 | ret = linehandle_validate_flags(lflags); | |
169 | if (ret) | |
170 | return ret; | |
171 | ||
52b7b596 | 172 | for (i = 0; i < lh->num_descs; i++) { |
925ca369 | 173 | desc = lh->descs[i]; |
c274b58a | 174 | linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags); |
925ca369 KG |
175 | |
176 | /* | |
177 | * Lines have to be requested explicitly for input | |
178 | * or output, else the line will be treated "as is". | |
179 | */ | |
180 | if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { | |
181 | int val = !!gcnf.default_values[i]; | |
182 | ||
183 | ret = gpiod_direction_output(desc, val); | |
184 | if (ret) | |
185 | return ret; | |
186 | } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { | |
187 | ret = gpiod_direction_input(desc); | |
188 | if (ret) | |
189 | return ret; | |
190 | } | |
191 | ||
9ce4ed5b | 192 | gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); |
925ca369 KG |
193 | } |
194 | return 0; | |
195 | } | |
196 | ||
20bddcb4 KG |
197 | static long linehandle_ioctl(struct file *file, unsigned int cmd, |
198 | unsigned long arg) | |
925ca369 | 199 | { |
49bc5279 | 200 | struct linehandle_state *lh = file->private_data; |
925ca369 KG |
201 | void __user *ip = (void __user *)arg; |
202 | struct gpiohandle_data ghd; | |
203 | DECLARE_BITMAP(vals, GPIOHANDLES_MAX); | |
1cef8b50 AS |
204 | unsigned int i; |
205 | int ret; | |
925ca369 | 206 | |
d83cee3d | 207 | guard(srcu)(&lh->gdev->srcu); |
20bddcb4 | 208 | |
8574b5b4 | 209 | if (!rcu_access_pointer(lh->gdev->chip)) |
533aae7c BG |
210 | return -ENODEV; |
211 | ||
1cef8b50 AS |
212 | switch (cmd) { |
213 | case GPIOHANDLE_GET_LINE_VALUES_IOCTL: | |
214 | /* NOTE: It's okay to read values of output lines */ | |
215 | ret = gpiod_get_array_value_complex(false, true, | |
216 | lh->num_descs, lh->descs, | |
217 | NULL, vals); | |
925ca369 KG |
218 | if (ret) |
219 | return ret; | |
220 | ||
221 | memset(&ghd, 0, sizeof(ghd)); | |
52b7b596 | 222 | for (i = 0; i < lh->num_descs; i++) |
925ca369 KG |
223 | ghd.values[i] = test_bit(i, vals); |
224 | ||
225 | if (copy_to_user(ip, &ghd, sizeof(ghd))) | |
226 | return -EFAULT; | |
227 | ||
228 | return 0; | |
1cef8b50 | 229 | case GPIOHANDLE_SET_LINE_VALUES_IOCTL: |
925ca369 KG |
230 | /* |
231 | * All line descriptors were created at once with the same | |
232 | * flags so just check if the first one is really output. | |
233 | */ | |
234 | if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags)) | |
235 | return -EPERM; | |
236 | ||
237 | if (copy_from_user(&ghd, ip, sizeof(ghd))) | |
238 | return -EFAULT; | |
239 | ||
240 | /* Clamp all values to [0,1] */ | |
52b7b596 | 241 | for (i = 0; i < lh->num_descs; i++) |
925ca369 KG |
242 | __assign_bit(i, vals, ghd.values[i]); |
243 | ||
244 | /* Reuse the array setting function */ | |
245 | return gpiod_set_array_value_complex(false, | |
a18512e3 | 246 | true, |
52b7b596 | 247 | lh->num_descs, |
a18512e3 KG |
248 | lh->descs, |
249 | NULL, | |
250 | vals); | |
1cef8b50 | 251 | case GPIOHANDLE_SET_CONFIG_IOCTL: |
925ca369 | 252 | return linehandle_set_config(lh, ip); |
1cef8b50 AS |
253 | default: |
254 | return -EINVAL; | |
925ca369 | 255 | } |
925ca369 KG |
256 | } |
257 | ||
258 | #ifdef CONFIG_COMPAT | |
49bc5279 | 259 | static long linehandle_ioctl_compat(struct file *file, unsigned int cmd, |
a18512e3 | 260 | unsigned long arg) |
925ca369 | 261 | { |
49bc5279 | 262 | return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
925ca369 KG |
263 | } |
264 | #endif | |
265 | ||
883f9198 | 266 | static void linehandle_free(struct linehandle_state *lh) |
925ca369 | 267 | { |
925ca369 KG |
268 | int i; |
269 | ||
52b7b596 | 270 | for (i = 0; i < lh->num_descs; i++) |
883f9198 KG |
271 | if (lh->descs[i]) |
272 | gpiod_free(lh->descs[i]); | |
925ca369 | 273 | kfree(lh->label); |
dc0989e3 | 274 | gpio_device_put(lh->gdev); |
925ca369 | 275 | kfree(lh); |
883f9198 KG |
276 | } |
277 | ||
278 | static int linehandle_release(struct inode *inode, struct file *file) | |
279 | { | |
280 | linehandle_free(file->private_data); | |
925ca369 KG |
281 | return 0; |
282 | } | |
283 | ||
284 | static const struct file_operations linehandle_fileops = { | |
285 | .release = linehandle_release, | |
286 | .owner = THIS_MODULE, | |
287 | .llseek = noop_llseek, | |
288 | .unlocked_ioctl = linehandle_ioctl, | |
289 | #ifdef CONFIG_COMPAT | |
290 | .compat_ioctl = linehandle_ioctl_compat, | |
291 | #endif | |
292 | }; | |
293 | ||
294 | static int linehandle_create(struct gpio_device *gdev, void __user *ip) | |
295 | { | |
296 | struct gpiohandle_request handlereq; | |
297 | struct linehandle_state *lh; | |
298 | struct file *file; | |
883f9198 | 299 | int fd, i, ret; |
925ca369 KG |
300 | u32 lflags; |
301 | ||
302 | if (copy_from_user(&handlereq, ip, sizeof(handlereq))) | |
303 | return -EFAULT; | |
304 | if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) | |
305 | return -EINVAL; | |
306 | ||
307 | lflags = handlereq.flags; | |
308 | ||
309 | ret = linehandle_validate_flags(lflags); | |
310 | if (ret) | |
311 | return ret; | |
312 | ||
313 | lh = kzalloc(sizeof(*lh), GFP_KERNEL); | |
314 | if (!lh) | |
315 | return -ENOMEM; | |
dc0989e3 | 316 | lh->gdev = gpio_device_get(gdev); |
925ca369 | 317 | |
f188ac12 KG |
318 | if (handlereq.consumer_label[0] != '\0') { |
319 | /* label is only initialized if consumer_label is set */ | |
320 | lh->label = kstrndup(handlereq.consumer_label, | |
321 | sizeof(handlereq.consumer_label) - 1, | |
322 | GFP_KERNEL); | |
925ca369 KG |
323 | if (!lh->label) { |
324 | ret = -ENOMEM; | |
325 | goto out_free_lh; | |
326 | } | |
327 | } | |
328 | ||
883f9198 KG |
329 | lh->num_descs = handlereq.lines; |
330 | ||
925ca369 KG |
331 | /* Request each GPIO */ |
332 | for (i = 0; i < handlereq.lines; i++) { | |
333 | u32 offset = handlereq.lineoffsets[i]; | |
f4e14d45 | 334 | struct gpio_desc *desc = gpio_device_get_desc(gdev, offset); |
925ca369 KG |
335 | |
336 | if (IS_ERR(desc)) { | |
337 | ret = PTR_ERR(desc); | |
883f9198 | 338 | goto out_free_lh; |
925ca369 KG |
339 | } |
340 | ||
95a4eed7 | 341 | ret = gpiod_request_user(desc, lh->label); |
925ca369 | 342 | if (ret) |
883f9198 | 343 | goto out_free_lh; |
925ca369 | 344 | lh->descs[i] = desc; |
c274b58a | 345 | linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags); |
925ca369 KG |
346 | |
347 | ret = gpiod_set_transitory(desc, false); | |
348 | if (ret < 0) | |
883f9198 | 349 | goto out_free_lh; |
925ca369 KG |
350 | |
351 | /* | |
352 | * Lines have to be requested explicitly for input | |
353 | * or output, else the line will be treated "as is". | |
354 | */ | |
355 | if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { | |
356 | int val = !!handlereq.default_values[i]; | |
357 | ||
358 | ret = gpiod_direction_output(desc, val); | |
359 | if (ret) | |
883f9198 | 360 | goto out_free_lh; |
925ca369 KG |
361 | } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { |
362 | ret = gpiod_direction_input(desc); | |
363 | if (ret) | |
883f9198 | 364 | goto out_free_lh; |
925ca369 KG |
365 | } |
366 | ||
9ce4ed5b | 367 | gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED); |
925ca369 KG |
368 | |
369 | dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", | |
370 | offset); | |
371 | } | |
925ca369 KG |
372 | |
373 | fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); | |
374 | if (fd < 0) { | |
375 | ret = fd; | |
883f9198 | 376 | goto out_free_lh; |
925ca369 KG |
377 | } |
378 | ||
379 | file = anon_inode_getfile("gpio-linehandle", | |
380 | &linehandle_fileops, | |
381 | lh, | |
382 | O_RDONLY | O_CLOEXEC); | |
383 | if (IS_ERR(file)) { | |
384 | ret = PTR_ERR(file); | |
385 | goto out_put_unused_fd; | |
386 | } | |
387 | ||
388 | handlereq.fd = fd; | |
389 | if (copy_to_user(ip, &handlereq, sizeof(handlereq))) { | |
390 | /* | |
391 | * fput() will trigger the release() callback, so do not go onto | |
392 | * the regular error cleanup path here. | |
393 | */ | |
394 | fput(file); | |
395 | put_unused_fd(fd); | |
396 | return -EFAULT; | |
397 | } | |
398 | ||
399 | fd_install(fd, file); | |
400 | ||
401 | dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", | |
52b7b596 | 402 | lh->num_descs); |
925ca369 KG |
403 | |
404 | return 0; | |
405 | ||
406 | out_put_unused_fd: | |
407 | put_unused_fd(fd); | |
925ca369 | 408 | out_free_lh: |
883f9198 | 409 | linehandle_free(lh); |
925ca369 KG |
410 | return ret; |
411 | } | |
3c0d9c63 KG |
412 | #endif /* CONFIG_GPIO_CDEV_V1 */ |
413 | ||
414 | /** | |
415 | * struct line - contains the state of a requested line | |
9344e34e | 416 | * @node: to store the object in supinfo_tree if supplemental |
3c0d9c63 | 417 | * @desc: the GPIO descriptor for this line. |
73e03419 KG |
418 | * @req: the corresponding line request |
419 | * @irq: the interrupt triggered in response to events on this GPIO | |
8d259847 | 420 | * @edflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or |
73e03419 KG |
421 | * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied |
422 | * @timestamp_ns: cache for the timestamp storing it between hardirq and | |
423 | * IRQ thread, used to bring the timestamp close to the actual event | |
424 | * @req_seqno: the seqno for the current edge event in the sequence of | |
425 | * events for the corresponding line request. This is drawn from the @req. | |
426 | * @line_seqno: the seqno for the current edge event in the sequence of | |
427 | * events for this line. | |
65cff704 | 428 | * @work: the worker that implements software debouncing |
9344e34e | 429 | * @debounce_period_us: the debounce period in microseconds |
65cff704 KG |
430 | * @sw_debounced: flag indicating if the software debouncer is active |
431 | * @level: the current debounced physical level of the line | |
85ff37e3 AS |
432 | * @hdesc: the Hardware Timestamp Engine (HTE) descriptor |
433 | * @raw_level: the line level at the time of event | |
434 | * @total_discard_seq: the running counter of the discarded events | |
435 | * @last_seqno: the last sequence number before debounce period expires | |
3c0d9c63 KG |
436 | */ |
437 | struct line { | |
9344e34e | 438 | struct rb_node node; |
3c0d9c63 | 439 | struct gpio_desc *desc; |
73e03419 KG |
440 | /* |
441 | * -- edge detector specific fields -- | |
442 | */ | |
443 | struct linereq *req; | |
444 | unsigned int irq; | |
3ffb7c45 | 445 | /* |
b1a92e94 KG |
446 | * The flags for the active edge detector configuration. |
447 | * | |
448 | * edflags is set by linereq_create(), linereq_free(), and | |
449 | * linereq_set_config_unlocked(), which are themselves mutually | |
450 | * exclusive, and is accessed by edge_irq_thread(), | |
451 | * process_hw_ts_thread() and debounce_work_func(), | |
452 | * which can all live with a slightly stale value. | |
3ffb7c45 | 453 | */ |
b1a92e94 | 454 | u64 edflags; |
73e03419 KG |
455 | /* |
456 | * timestamp_ns and req_seqno are accessed only by | |
457 | * edge_irq_handler() and edge_irq_thread(), which are themselves | |
458 | * mutually exclusive, so no additional protection is necessary. | |
459 | */ | |
460 | u64 timestamp_ns; | |
461 | u32 req_seqno; | |
65cff704 KG |
462 | /* |
463 | * line_seqno is accessed by either edge_irq_thread() or | |
464 | * debounce_work_func(), which are themselves mutually exclusive, | |
465 | * so no additional protection is necessary. | |
466 | */ | |
73e03419 | 467 | u32 line_seqno; |
65cff704 KG |
468 | /* |
469 | * -- debouncer specific fields -- | |
470 | */ | |
471 | struct delayed_work work; | |
9344e34e KG |
472 | /* |
473 | * debounce_period_us is accessed by debounce_irq_handler() and | |
474 | * process_hw_ts() which are disabled when modified by | |
475 | * debounce_setup(), edge_detector_setup() or edge_detector_stop() | |
476 | * or can live with a stale version when updated by | |
477 | * edge_detector_update(). | |
478 | * The modifying functions are themselves mutually exclusive. | |
479 | */ | |
480 | unsigned int debounce_period_us; | |
65cff704 KG |
481 | /* |
482 | * sw_debounce is accessed by linereq_set_config(), which is the | |
483 | * only setter, and linereq_get_values(), which can live with a | |
484 | * slightly stale value. | |
485 | */ | |
486 | unsigned int sw_debounced; | |
487 | /* | |
488 | * level is accessed by debounce_work_func(), which is the only | |
489 | * setter, and linereq_get_values() which can live with a slightly | |
490 | * stale value. | |
491 | */ | |
492 | unsigned int level; | |
272ddba0 | 493 | #ifdef CONFIG_HTE |
2068339a DP |
494 | struct hte_ts_desc hdesc; |
495 | /* | |
496 | * HTE provider sets line level at the time of event. The valid | |
497 | * value is 0 or 1 and negative value for an error. | |
498 | */ | |
499 | int raw_level; | |
500 | /* | |
501 | * when sw_debounce is set on HTE enabled line, this is running | |
502 | * counter of the discarded events. | |
503 | */ | |
504 | u32 total_discard_seq; | |
505 | /* | |
506 | * when sw_debounce is set on HTE enabled line, this variable records | |
507 | * last sequence number before debounce period expires. | |
508 | */ | |
509 | u32 last_seqno; | |
272ddba0 | 510 | #endif /* CONFIG_HTE */ |
3c0d9c63 KG |
511 | }; |
512 | ||
9344e34e KG |
513 | /* |
514 | * a rbtree of the struct lines containing supplemental info. | |
515 | * Used to populate gpio_v2_line_info with cdev specific fields not contained | |
516 | * in the struct gpio_desc. | |
517 | * A line is determined to contain supplemental information by | |
518 | * line_has_supinfo(). | |
519 | */ | |
520 | static struct rb_root supinfo_tree = RB_ROOT; | |
521 | /* covers supinfo_tree */ | |
522 | static DEFINE_SPINLOCK(supinfo_lock); | |
523 | ||
3c0d9c63 KG |
524 | /** |
525 | * struct linereq - contains the state of a userspace line request | |
526 | * @gdev: the GPIO device the line request pertains to | |
527 | * @label: consumer label used to tag GPIO descriptors | |
528 | * @num_lines: the number of lines in the lines array | |
73e03419 | 529 | * @wait: wait queue that handles blocking reads of events |
a0dda508 | 530 | * @device_unregistered_nb: notifier block for receiving gdev unregister events |
73e03419 KG |
531 | * @event_buffer_size: the number of elements allocated in @events |
532 | * @events: KFIFO for the GPIO events | |
533 | * @seqno: the sequence number for edge events generated on all lines in | |
534 | * this line request. Note that this is not used when @num_lines is 1, as | |
535 | * the line_seqno is then the same and is cheaper to calculate. | |
a54756cb | 536 | * @config_mutex: mutex for serializing ioctl() calls to ensure consistency |
9344e34e KG |
537 | * of configuration, particularly multi-step accesses to desc flags and |
538 | * changes to supinfo status. | |
3c0d9c63 KG |
539 | * @lines: the lines held by this line request, with @num_lines elements. |
540 | */ | |
541 | struct linereq { | |
542 | struct gpio_device *gdev; | |
543 | const char *label; | |
544 | u32 num_lines; | |
73e03419 | 545 | wait_queue_head_t wait; |
a0dda508 | 546 | struct notifier_block device_unregistered_nb; |
73e03419 KG |
547 | u32 event_buffer_size; |
548 | DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); | |
549 | atomic_t seqno; | |
a54756cb | 550 | struct mutex config_mutex; |
a512635d | 551 | struct line lines[] __counted_by(num_lines); |
3c0d9c63 KG |
552 | }; |
553 | ||
9344e34e KG |
554 | static void supinfo_insert(struct line *line) |
555 | { | |
556 | struct rb_node **new = &(supinfo_tree.rb_node), *parent = NULL; | |
557 | struct line *entry; | |
558 | ||
559 | guard(spinlock)(&supinfo_lock); | |
560 | ||
561 | while (*new) { | |
562 | entry = container_of(*new, struct line, node); | |
563 | ||
564 | parent = *new; | |
565 | if (line->desc < entry->desc) { | |
566 | new = &((*new)->rb_left); | |
567 | } else if (line->desc > entry->desc) { | |
568 | new = &((*new)->rb_right); | |
569 | } else { | |
570 | /* this should never happen */ | |
571 | WARN(1, "duplicate line inserted"); | |
572 | return; | |
573 | } | |
574 | } | |
575 | ||
576 | rb_link_node(&line->node, parent, new); | |
577 | rb_insert_color(&line->node, &supinfo_tree); | |
578 | } | |
579 | ||
580 | static void supinfo_erase(struct line *line) | |
581 | { | |
582 | guard(spinlock)(&supinfo_lock); | |
583 | ||
584 | rb_erase(&line->node, &supinfo_tree); | |
585 | } | |
586 | ||
587 | static struct line *supinfo_find(struct gpio_desc *desc) | |
588 | { | |
589 | struct rb_node *node = supinfo_tree.rb_node; | |
590 | struct line *line; | |
591 | ||
592 | while (node) { | |
593 | line = container_of(node, struct line, node); | |
594 | if (desc < line->desc) | |
595 | node = node->rb_left; | |
596 | else if (desc > line->desc) | |
597 | node = node->rb_right; | |
598 | else | |
599 | return line; | |
600 | } | |
601 | return NULL; | |
602 | } | |
603 | ||
604 | static void supinfo_to_lineinfo(struct gpio_desc *desc, | |
605 | struct gpio_v2_line_info *info) | |
606 | { | |
607 | struct gpio_v2_line_attribute *attr; | |
608 | struct line *line; | |
609 | ||
610 | guard(spinlock)(&supinfo_lock); | |
611 | ||
612 | line = supinfo_find(desc); | |
613 | if (!line) | |
614 | return; | |
615 | ||
616 | attr = &info->attrs[info->num_attrs]; | |
617 | attr->id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; | |
618 | attr->debounce_period_us = READ_ONCE(line->debounce_period_us); | |
619 | info->num_attrs++; | |
620 | } | |
621 | ||
622 | static inline bool line_has_supinfo(struct line *line) | |
623 | { | |
624 | return READ_ONCE(line->debounce_period_us); | |
625 | } | |
626 | ||
627 | /* | |
628 | * Checks line_has_supinfo() before and after the change to avoid unnecessary | |
629 | * supinfo_tree access. | |
630 | * Called indirectly by linereq_create() or linereq_set_config() so line | |
631 | * is already protected from concurrent changes. | |
632 | */ | |
633 | static void line_set_debounce_period(struct line *line, | |
634 | unsigned int debounce_period_us) | |
635 | { | |
636 | bool was_suppl = line_has_supinfo(line); | |
637 | ||
638 | WRITE_ONCE(line->debounce_period_us, debounce_period_us); | |
639 | ||
640 | /* if supinfo status is unchanged then we're done */ | |
641 | if (line_has_supinfo(line) == was_suppl) | |
642 | return; | |
643 | ||
644 | /* supinfo status has changed, so update the tree */ | |
645 | if (was_suppl) | |
646 | supinfo_erase(line); | |
647 | else | |
648 | supinfo_insert(line); | |
649 | } | |
650 | ||
3c0d9c63 KG |
651 | #define GPIO_V2_LINE_BIAS_FLAGS \ |
652 | (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ | |
653 | GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \ | |
654 | GPIO_V2_LINE_FLAG_BIAS_DISABLED) | |
655 | ||
656 | #define GPIO_V2_LINE_DIRECTION_FLAGS \ | |
657 | (GPIO_V2_LINE_FLAG_INPUT | \ | |
658 | GPIO_V2_LINE_FLAG_OUTPUT) | |
659 | ||
660 | #define GPIO_V2_LINE_DRIVE_FLAGS \ | |
661 | (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \ | |
662 | GPIO_V2_LINE_FLAG_OPEN_SOURCE) | |
663 | ||
73e03419 KG |
664 | #define GPIO_V2_LINE_EDGE_FLAGS \ |
665 | (GPIO_V2_LINE_FLAG_EDGE_RISING | \ | |
666 | GPIO_V2_LINE_FLAG_EDGE_FALLING) | |
667 | ||
5e2ca893 KG |
668 | #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS |
669 | ||
3c0d9c63 KG |
670 | #define GPIO_V2_LINE_VALID_FLAGS \ |
671 | (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ | |
672 | GPIO_V2_LINE_DIRECTION_FLAGS | \ | |
673 | GPIO_V2_LINE_DRIVE_FLAGS | \ | |
73e03419 | 674 | GPIO_V2_LINE_EDGE_FLAGS | \ |
26d060e4 | 675 | GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \ |
2068339a | 676 | GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \ |
3c0d9c63 KG |
677 | GPIO_V2_LINE_BIAS_FLAGS) |
678 | ||
b1a92e94 KG |
679 | /* subset of flags relevant for edge detector configuration */ |
680 | #define GPIO_V2_LINE_EDGE_DETECTOR_FLAGS \ | |
681 | (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ | |
682 | GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \ | |
683 | GPIO_V2_LINE_EDGE_FLAGS) | |
684 | ||
a0dda508 BG |
685 | static int linereq_unregistered_notify(struct notifier_block *nb, |
686 | unsigned long action, void *data) | |
687 | { | |
688 | struct linereq *lr = container_of(nb, struct linereq, | |
689 | device_unregistered_nb); | |
690 | ||
691 | wake_up_poll(&lr->wait, EPOLLIN | EPOLLERR); | |
692 | ||
693 | return NOTIFY_OK; | |
694 | } | |
695 | ||
73e03419 KG |
696 | static void linereq_put_event(struct linereq *lr, |
697 | struct gpio_v2_line_event *le) | |
698 | { | |
699 | bool overflow = false; | |
700 | ||
0ebeaab4 KG |
701 | scoped_guard(spinlock, &lr->wait.lock) { |
702 | if (kfifo_is_full(&lr->events)) { | |
703 | overflow = true; | |
704 | kfifo_skip(&lr->events); | |
705 | } | |
706 | kfifo_in(&lr->events, le, 1); | |
73e03419 | 707 | } |
73e03419 KG |
708 | if (!overflow) |
709 | wake_up_poll(&lr->wait, EPOLLIN); | |
710 | else | |
711 | pr_debug_ratelimited("event FIFO is full - event dropped\n"); | |
712 | } | |
713 | ||
26d060e4 KG |
714 | static u64 line_event_timestamp(struct line *line) |
715 | { | |
716 | if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags)) | |
717 | return ktime_get_real_ns(); | |
272ddba0 KG |
718 | else if (IS_ENABLED(CONFIG_HTE) && |
719 | test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags)) | |
2068339a | 720 | return line->timestamp_ns; |
26d060e4 KG |
721 | |
722 | return ktime_get_ns(); | |
723 | } | |
724 | ||
24220232 KG |
725 | static u32 line_event_id(int level) |
726 | { | |
727 | return level ? GPIO_V2_LINE_EVENT_RISING_EDGE : | |
728 | GPIO_V2_LINE_EVENT_FALLING_EDGE; | |
729 | } | |
730 | ||
83092341 KG |
731 | static inline char *make_irq_label(const char *orig) |
732 | { | |
733 | char *new; | |
734 | ||
735 | if (!orig) | |
736 | return NULL; | |
737 | ||
738 | new = kstrdup_and_replace(orig, '/', ':', GFP_KERNEL); | |
739 | if (!new) | |
740 | return ERR_PTR(-ENOMEM); | |
741 | ||
742 | return new; | |
743 | } | |
744 | ||
745 | static inline void free_irq_label(const char *label) | |
746 | { | |
747 | kfree(label); | |
748 | } | |
749 | ||
272ddba0 KG |
750 | #ifdef CONFIG_HTE |
751 | ||
2068339a DP |
752 | static enum hte_return process_hw_ts_thread(void *p) |
753 | { | |
754 | struct line *line; | |
755 | struct linereq *lr; | |
756 | struct gpio_v2_line_event le; | |
b1a92e94 | 757 | u64 edflags; |
2068339a | 758 | int level; |
2068339a DP |
759 | |
760 | if (!p) | |
761 | return HTE_CB_HANDLED; | |
762 | ||
763 | line = p; | |
764 | lr = line->req; | |
765 | ||
766 | memset(&le, 0, sizeof(le)); | |
767 | ||
768 | le.timestamp_ns = line->timestamp_ns; | |
b1a92e94 | 769 | edflags = READ_ONCE(line->edflags); |
2068339a | 770 | |
b1a92e94 | 771 | switch (edflags & GPIO_V2_LINE_EDGE_FLAGS) { |
cfa53463 | 772 | case GPIO_V2_LINE_FLAG_EDGE_BOTH: |
24220232 KG |
773 | level = (line->raw_level >= 0) ? |
774 | line->raw_level : | |
775 | gpiod_get_raw_value_cansleep(line->desc); | |
776 | ||
b1a92e94 | 777 | if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) |
24220232 | 778 | level = !level; |
2068339a | 779 | |
24220232 | 780 | le.id = line_event_id(level); |
cfa53463 KG |
781 | break; |
782 | case GPIO_V2_LINE_FLAG_EDGE_RISING: | |
2068339a | 783 | le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; |
cfa53463 KG |
784 | break; |
785 | case GPIO_V2_LINE_FLAG_EDGE_FALLING: | |
2068339a | 786 | le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; |
cfa53463 KG |
787 | break; |
788 | default: | |
2068339a DP |
789 | return HTE_CB_HANDLED; |
790 | } | |
791 | le.line_seqno = line->line_seqno; | |
792 | le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno; | |
793 | le.offset = gpio_chip_hwgpio(line->desc); | |
794 | ||
795 | linereq_put_event(lr, &le); | |
796 | ||
797 | return HTE_CB_HANDLED; | |
798 | } | |
799 | ||
800 | static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p) | |
801 | { | |
802 | struct line *line; | |
803 | struct linereq *lr; | |
804 | int diff_seqno = 0; | |
805 | ||
806 | if (!ts || !p) | |
807 | return HTE_CB_HANDLED; | |
808 | ||
809 | line = p; | |
810 | line->timestamp_ns = ts->tsc; | |
811 | line->raw_level = ts->raw_level; | |
812 | lr = line->req; | |
813 | ||
814 | if (READ_ONCE(line->sw_debounced)) { | |
815 | line->total_discard_seq++; | |
816 | line->last_seqno = ts->seq; | |
817 | mod_delayed_work(system_wq, &line->work, | |
9344e34e | 818 | usecs_to_jiffies(READ_ONCE(line->debounce_period_us))); |
2068339a DP |
819 | } else { |
820 | if (unlikely(ts->seq < line->line_seqno)) | |
821 | return HTE_CB_HANDLED; | |
822 | ||
823 | diff_seqno = ts->seq - line->line_seqno; | |
824 | line->line_seqno = ts->seq; | |
825 | if (lr->num_lines != 1) | |
826 | line->req_seqno = atomic_add_return(diff_seqno, | |
827 | &lr->seqno); | |
828 | ||
829 | return HTE_RUN_SECOND_CB; | |
830 | } | |
831 | ||
832 | return HTE_CB_HANDLED; | |
833 | } | |
834 | ||
272ddba0 KG |
835 | static int hte_edge_setup(struct line *line, u64 eflags) |
836 | { | |
837 | int ret; | |
838 | unsigned long flags = 0; | |
839 | struct hte_ts_desc *hdesc = &line->hdesc; | |
840 | ||
841 | if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) | |
842 | flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? | |
843 | HTE_FALLING_EDGE_TS : | |
844 | HTE_RISING_EDGE_TS; | |
845 | if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) | |
846 | flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? | |
847 | HTE_RISING_EDGE_TS : | |
848 | HTE_FALLING_EDGE_TS; | |
849 | ||
850 | line->total_discard_seq = 0; | |
851 | ||
852 | hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags, NULL, | |
853 | line->desc); | |
854 | ||
855 | ret = hte_ts_get(NULL, hdesc, 0); | |
856 | if (ret) | |
857 | return ret; | |
858 | ||
859 | return hte_request_ts_ns(hdesc, process_hw_ts, process_hw_ts_thread, | |
860 | line); | |
861 | } | |
862 | ||
863 | #else | |
864 | ||
865 | static int hte_edge_setup(struct line *line, u64 eflags) | |
866 | { | |
867 | return 0; | |
868 | } | |
869 | #endif /* CONFIG_HTE */ | |
870 | ||
73e03419 KG |
871 | static irqreturn_t edge_irq_thread(int irq, void *p) |
872 | { | |
873 | struct line *line = p; | |
874 | struct linereq *lr = line->req; | |
875 | struct gpio_v2_line_event le; | |
876 | ||
877 | /* Do not leak kernel stack to userspace */ | |
878 | memset(&le, 0, sizeof(le)); | |
879 | ||
880 | if (line->timestamp_ns) { | |
881 | le.timestamp_ns = line->timestamp_ns; | |
882 | } else { | |
883 | /* | |
884 | * We may be running from a nested threaded interrupt in | |
885 | * which case we didn't get the timestamp from | |
886 | * edge_irq_handler(). | |
887 | */ | |
26d060e4 | 888 | le.timestamp_ns = line_event_timestamp(line); |
73e03419 KG |
889 | if (lr->num_lines != 1) |
890 | line->req_seqno = atomic_inc_return(&lr->seqno); | |
891 | } | |
892 | line->timestamp_ns = 0; | |
893 | ||
b1a92e94 | 894 | switch (READ_ONCE(line->edflags) & GPIO_V2_LINE_EDGE_FLAGS) { |
cfa53463 | 895 | case GPIO_V2_LINE_FLAG_EDGE_BOTH: |
24220232 | 896 | le.id = line_event_id(gpiod_get_value_cansleep(line->desc)); |
cfa53463 KG |
897 | break; |
898 | case GPIO_V2_LINE_FLAG_EDGE_RISING: | |
73e03419 | 899 | le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; |
cfa53463 KG |
900 | break; |
901 | case GPIO_V2_LINE_FLAG_EDGE_FALLING: | |
73e03419 | 902 | le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; |
cfa53463 KG |
903 | break; |
904 | default: | |
73e03419 KG |
905 | return IRQ_NONE; |
906 | } | |
907 | line->line_seqno++; | |
908 | le.line_seqno = line->line_seqno; | |
909 | le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno; | |
910 | le.offset = gpio_chip_hwgpio(line->desc); | |
911 | ||
912 | linereq_put_event(lr, &le); | |
913 | ||
914 | return IRQ_HANDLED; | |
915 | } | |
916 | ||
917 | static irqreturn_t edge_irq_handler(int irq, void *p) | |
918 | { | |
919 | struct line *line = p; | |
920 | struct linereq *lr = line->req; | |
921 | ||
922 | /* | |
923 | * Just store the timestamp in hardirq context so we get it as | |
924 | * close in time as possible to the actual event. | |
925 | */ | |
26d060e4 | 926 | line->timestamp_ns = line_event_timestamp(line); |
73e03419 KG |
927 | |
928 | if (lr->num_lines != 1) | |
929 | line->req_seqno = atomic_inc_return(&lr->seqno); | |
930 | ||
931 | return IRQ_WAKE_THREAD; | |
932 | } | |
933 | ||
65cff704 KG |
934 | /* |
935 | * returns the current debounced logical value. | |
936 | */ | |
937 | static bool debounced_value(struct line *line) | |
938 | { | |
939 | bool value; | |
940 | ||
941 | /* | |
942 | * minor race - debouncer may be stopped here, so edge_detector_stop() | |
943 | * must leave the value unchanged so the following will read the level | |
944 | * from when the debouncer was last running. | |
945 | */ | |
946 | value = READ_ONCE(line->level); | |
947 | ||
948 | if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags)) | |
949 | value = !value; | |
950 | ||
951 | return value; | |
952 | } | |
953 | ||
954 | static irqreturn_t debounce_irq_handler(int irq, void *p) | |
955 | { | |
956 | struct line *line = p; | |
957 | ||
958 | mod_delayed_work(system_wq, &line->work, | |
9344e34e | 959 | usecs_to_jiffies(READ_ONCE(line->debounce_period_us))); |
65cff704 KG |
960 | |
961 | return IRQ_HANDLED; | |
962 | } | |
963 | ||
964 | static void debounce_work_func(struct work_struct *work) | |
965 | { | |
966 | struct gpio_v2_line_event le; | |
967 | struct line *line = container_of(work, struct line, work.work); | |
968 | struct linereq *lr; | |
b1a92e94 | 969 | u64 eflags, edflags = READ_ONCE(line->edflags); |
272ddba0 KG |
970 | int level = -1; |
971 | #ifdef CONFIG_HTE | |
972 | int diff_seqno; | |
65cff704 | 973 | |
b1a92e94 | 974 | if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) |
2068339a | 975 | level = line->raw_level; |
272ddba0 | 976 | #endif |
b1a92e94 | 977 | if (level < 0) |
2068339a | 978 | level = gpiod_get_raw_value_cansleep(line->desc); |
65cff704 KG |
979 | if (level < 0) { |
980 | pr_debug_ratelimited("debouncer failed to read line value\n"); | |
981 | return; | |
982 | } | |
983 | ||
984 | if (READ_ONCE(line->level) == level) | |
985 | return; | |
986 | ||
987 | WRITE_ONCE(line->level, level); | |
988 | ||
989 | /* -- edge detection -- */ | |
b1a92e94 | 990 | eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS; |
3ffb7c45 | 991 | if (!eflags) |
65cff704 KG |
992 | return; |
993 | ||
994 | /* switch from physical level to logical - if they differ */ | |
b1a92e94 | 995 | if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) |
65cff704 KG |
996 | level = !level; |
997 | ||
998 | /* ignore edges that are not being monitored */ | |
3ffb7c45 KG |
999 | if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) || |
1000 | ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level)) | |
65cff704 KG |
1001 | return; |
1002 | ||
1003 | /* Do not leak kernel stack to userspace */ | |
1004 | memset(&le, 0, sizeof(le)); | |
1005 | ||
1006 | lr = line->req; | |
26d060e4 | 1007 | le.timestamp_ns = line_event_timestamp(line); |
65cff704 | 1008 | le.offset = gpio_chip_hwgpio(line->desc); |
272ddba0 | 1009 | #ifdef CONFIG_HTE |
b1a92e94 | 1010 | if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) { |
2068339a DP |
1011 | /* discard events except the last one */ |
1012 | line->total_discard_seq -= 1; | |
1013 | diff_seqno = line->last_seqno - line->total_discard_seq - | |
1014 | line->line_seqno; | |
1015 | line->line_seqno = line->last_seqno - line->total_discard_seq; | |
1016 | le.line_seqno = line->line_seqno; | |
1017 | le.seqno = (lr->num_lines == 1) ? | |
1018 | le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno); | |
272ddba0 KG |
1019 | } else |
1020 | #endif /* CONFIG_HTE */ | |
1021 | { | |
2068339a DP |
1022 | line->line_seqno++; |
1023 | le.line_seqno = line->line_seqno; | |
1024 | le.seqno = (lr->num_lines == 1) ? | |
1025 | le.line_seqno : atomic_inc_return(&lr->seqno); | |
1026 | } | |
65cff704 | 1027 | |
24220232 | 1028 | le.id = line_event_id(level); |
65cff704 KG |
1029 | |
1030 | linereq_put_event(lr, &le); | |
1031 | } | |
1032 | ||
b1a92e94 | 1033 | static int debounce_setup(struct line *line, unsigned int debounce_period_us) |
65cff704 KG |
1034 | { |
1035 | unsigned long irqflags; | |
1036 | int ret, level, irq; | |
83092341 | 1037 | char *label; |
65cff704 KG |
1038 | |
1039 | /* try hardware */ | |
1040 | ret = gpiod_set_debounce(line->desc, debounce_period_us); | |
1041 | if (!ret) { | |
9344e34e | 1042 | line_set_debounce_period(line, debounce_period_us); |
65cff704 KG |
1043 | return ret; |
1044 | } | |
1045 | if (ret != -ENOTSUPP) | |
1046 | return ret; | |
1047 | ||
1048 | if (debounce_period_us) { | |
1049 | /* setup software debounce */ | |
1050 | level = gpiod_get_raw_value_cansleep(line->desc); | |
1051 | if (level < 0) | |
1052 | return level; | |
1053 | ||
272ddba0 KG |
1054 | if (!(IS_ENABLED(CONFIG_HTE) && |
1055 | test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))) { | |
2068339a DP |
1056 | irq = gpiod_to_irq(line->desc); |
1057 | if (irq < 0) | |
1058 | return -ENXIO; | |
65cff704 | 1059 | |
83092341 KG |
1060 | label = make_irq_label(line->req->label); |
1061 | if (IS_ERR(label)) | |
1062 | return -ENOMEM; | |
1063 | ||
2068339a DP |
1064 | irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING; |
1065 | ret = request_irq(irq, debounce_irq_handler, irqflags, | |
83092341 KG |
1066 | label, line); |
1067 | if (ret) { | |
1068 | free_irq_label(label); | |
2068339a | 1069 | return ret; |
83092341 | 1070 | } |
2068339a DP |
1071 | line->irq = irq; |
1072 | } else { | |
2487a812 | 1073 | ret = hte_edge_setup(line, GPIO_V2_LINE_FLAG_EDGE_BOTH); |
2068339a DP |
1074 | if (ret) |
1075 | return ret; | |
1076 | } | |
65cff704 | 1077 | |
2068339a | 1078 | WRITE_ONCE(line->level, level); |
65cff704 | 1079 | WRITE_ONCE(line->sw_debounced, 1); |
65cff704 KG |
1080 | } |
1081 | return 0; | |
1082 | } | |
1083 | ||
1084 | static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc, | |
1085 | unsigned int line_idx) | |
1086 | { | |
1087 | unsigned int i; | |
1088 | u64 mask = BIT_ULL(line_idx); | |
1089 | ||
1090 | for (i = 0; i < lc->num_attrs; i++) { | |
1091 | if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && | |
1092 | (lc->attrs[i].mask & mask)) | |
1093 | return true; | |
1094 | } | |
1095 | return false; | |
1096 | } | |
1097 | ||
1098 | static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc, | |
1099 | unsigned int line_idx) | |
1100 | { | |
1101 | unsigned int i; | |
1102 | u64 mask = BIT_ULL(line_idx); | |
1103 | ||
1104 | for (i = 0; i < lc->num_attrs; i++) { | |
1105 | if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && | |
1106 | (lc->attrs[i].mask & mask)) | |
1107 | return lc->attrs[i].attr.debounce_period_us; | |
1108 | } | |
1109 | return 0; | |
1110 | } | |
1111 | ||
b1a92e94 | 1112 | static void edge_detector_stop(struct line *line) |
73e03419 | 1113 | { |
b1a92e94 | 1114 | if (line->irq) { |
b3449087 | 1115 | free_irq_label(free_irq(line->irq, line)); |
73e03419 KG |
1116 | line->irq = 0; |
1117 | } | |
a54756cb | 1118 | |
272ddba0 | 1119 | #ifdef CONFIG_HTE |
b1a92e94 | 1120 | if (READ_ONCE(line->edflags) & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) |
2068339a | 1121 | hte_ts_put(&line->hdesc); |
272ddba0 | 1122 | #endif |
2068339a | 1123 | |
65cff704 KG |
1124 | cancel_delayed_work_sync(&line->work); |
1125 | WRITE_ONCE(line->sw_debounced, 0); | |
b1a92e94 | 1126 | WRITE_ONCE(line->edflags, 0); |
9344e34e | 1127 | line_set_debounce_period(line, 0); |
65cff704 | 1128 | /* do not change line->level - see comment in debounced_value() */ |
73e03419 KG |
1129 | } |
1130 | ||
1131 | static int edge_detector_setup(struct line *line, | |
65cff704 | 1132 | struct gpio_v2_line_config *lc, |
b1a92e94 | 1133 | unsigned int line_idx, u64 edflags) |
73e03419 | 1134 | { |
65cff704 | 1135 | u32 debounce_period_us; |
73e03419 | 1136 | unsigned long irqflags = 0; |
b1a92e94 | 1137 | u64 eflags; |
73e03419 | 1138 | int irq, ret; |
b3449087 | 1139 | char *label; |
73e03419 | 1140 | |
b1a92e94 | 1141 | eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS; |
73e03419 KG |
1142 | if (eflags && !kfifo_initialized(&line->req->events)) { |
1143 | ret = kfifo_alloc(&line->req->events, | |
1144 | line->req->event_buffer_size, GFP_KERNEL); | |
1145 | if (ret) | |
1146 | return ret; | |
1147 | } | |
65cff704 KG |
1148 | if (gpio_v2_line_config_debounced(lc, line_idx)) { |
1149 | debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx); | |
b1a92e94 | 1150 | ret = debounce_setup(line, debounce_period_us); |
65cff704 KG |
1151 | if (ret) |
1152 | return ret; | |
9344e34e | 1153 | line_set_debounce_period(line, debounce_period_us); |
65cff704 | 1154 | } |
73e03419 | 1155 | |
65cff704 KG |
1156 | /* detection disabled or sw debouncer will provide edge detection */ |
1157 | if (!eflags || READ_ONCE(line->sw_debounced)) | |
73e03419 KG |
1158 | return 0; |
1159 | ||
272ddba0 KG |
1160 | if (IS_ENABLED(CONFIG_HTE) && |
1161 | (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)) | |
b1a92e94 | 1162 | return hte_edge_setup(line, edflags); |
2068339a | 1163 | |
73e03419 KG |
1164 | irq = gpiod_to_irq(line->desc); |
1165 | if (irq < 0) | |
1166 | return -ENXIO; | |
1167 | ||
1168 | if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) | |
1169 | irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? | |
1170 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; | |
1171 | if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) | |
1172 | irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? | |
1173 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; | |
1174 | irqflags |= IRQF_ONESHOT; | |
1175 | ||
b3449087 | 1176 | label = make_irq_label(line->req->label); |
b3b95964 BG |
1177 | if (IS_ERR(label)) |
1178 | return PTR_ERR(label); | |
b3449087 | 1179 | |
73e03419 KG |
1180 | /* Request a thread to read the events */ |
1181 | ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread, | |
b3449087 BG |
1182 | irqflags, label, line); |
1183 | if (ret) { | |
1184 | free_irq_label(label); | |
73e03419 | 1185 | return ret; |
b3449087 | 1186 | } |
73e03419 KG |
1187 | |
1188 | line->irq = irq; | |
1189 | return 0; | |
1190 | } | |
1191 | ||
65cff704 KG |
1192 | static int edge_detector_update(struct line *line, |
1193 | struct gpio_v2_line_config *lc, | |
b1a92e94 | 1194 | unsigned int line_idx, u64 edflags) |
a54756cb | 1195 | { |
b1a92e94 | 1196 | u64 active_edflags = READ_ONCE(line->edflags); |
65cff704 | 1197 | unsigned int debounce_period_us = |
2068339a | 1198 | gpio_v2_line_config_debounce_period(lc, line_idx); |
65cff704 | 1199 | |
b1a92e94 | 1200 | if ((active_edflags == edflags) && |
9344e34e | 1201 | (READ_ONCE(line->debounce_period_us) == debounce_period_us)) |
65cff704 KG |
1202 | return 0; |
1203 | ||
1204 | /* sw debounced and still will be...*/ | |
1205 | if (debounce_period_us && READ_ONCE(line->sw_debounced)) { | |
9344e34e | 1206 | line_set_debounce_period(line, debounce_period_us); |
a54756cb | 1207 | return 0; |
65cff704 | 1208 | } |
a54756cb | 1209 | |
65cff704 | 1210 | /* reconfiguring edge detection or sw debounce being disabled */ |
b1a92e94 KG |
1211 | if ((line->irq && !READ_ONCE(line->sw_debounced)) || |
1212 | (active_edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) || | |
65cff704 | 1213 | (!debounce_period_us && READ_ONCE(line->sw_debounced))) |
b1a92e94 | 1214 | edge_detector_stop(line); |
a54756cb | 1215 | |
b1a92e94 | 1216 | return edge_detector_setup(line, lc, line_idx, edflags); |
a54756cb KG |
1217 | } |
1218 | ||
3c0d9c63 KG |
1219 | static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, |
1220 | unsigned int line_idx) | |
1221 | { | |
1222 | unsigned int i; | |
1223 | u64 mask = BIT_ULL(line_idx); | |
1224 | ||
1225 | for (i = 0; i < lc->num_attrs; i++) { | |
1226 | if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) && | |
1227 | (lc->attrs[i].mask & mask)) | |
1228 | return lc->attrs[i].attr.flags; | |
1229 | } | |
1230 | return lc->flags; | |
1231 | } | |
1232 | ||
1233 | static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc, | |
1234 | unsigned int line_idx) | |
1235 | { | |
1236 | unsigned int i; | |
1237 | u64 mask = BIT_ULL(line_idx); | |
1238 | ||
1239 | for (i = 0; i < lc->num_attrs; i++) { | |
1240 | if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) && | |
1241 | (lc->attrs[i].mask & mask)) | |
1242 | return !!(lc->attrs[i].attr.values & mask); | |
1243 | } | |
1244 | return 0; | |
1245 | } | |
1246 | ||
1247 | static int gpio_v2_line_flags_validate(u64 flags) | |
1248 | { | |
1249 | /* Return an error if an unknown flag is set */ | |
1250 | if (flags & ~GPIO_V2_LINE_VALID_FLAGS) | |
1251 | return -EINVAL; | |
272ddba0 KG |
1252 | |
1253 | if (!IS_ENABLED(CONFIG_HTE) && | |
1254 | (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)) | |
1255 | return -EOPNOTSUPP; | |
1256 | ||
3c0d9c63 KG |
1257 | /* |
1258 | * Do not allow both INPUT and OUTPUT flags to be set as they are | |
1259 | * contradictory. | |
1260 | */ | |
1261 | if ((flags & GPIO_V2_LINE_FLAG_INPUT) && | |
1262 | (flags & GPIO_V2_LINE_FLAG_OUTPUT)) | |
1263 | return -EINVAL; | |
1264 | ||
2068339a | 1265 | /* Only allow one event clock source */ |
272ddba0 KG |
1266 | if (IS_ENABLED(CONFIG_HTE) && |
1267 | (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) && | |
2068339a DP |
1268 | (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)) |
1269 | return -EINVAL; | |
1270 | ||
73e03419 KG |
1271 | /* Edge detection requires explicit input. */ |
1272 | if ((flags & GPIO_V2_LINE_EDGE_FLAGS) && | |
1273 | !(flags & GPIO_V2_LINE_FLAG_INPUT)) | |
1274 | return -EINVAL; | |
1275 | ||
3c0d9c63 KG |
1276 | /* |
1277 | * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single | |
1278 | * request. If the hardware actually supports enabling both at the | |
1279 | * same time the electrical result would be disastrous. | |
1280 | */ | |
1281 | if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) && | |
1282 | (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE)) | |
1283 | return -EINVAL; | |
1284 | ||
1285 | /* Drive requires explicit output direction. */ | |
1286 | if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) && | |
1287 | !(flags & GPIO_V2_LINE_FLAG_OUTPUT)) | |
1288 | return -EINVAL; | |
1289 | ||
1290 | /* Bias requires explicit direction. */ | |
1291 | if ((flags & GPIO_V2_LINE_BIAS_FLAGS) && | |
1292 | !(flags & GPIO_V2_LINE_DIRECTION_FLAGS)) | |
1293 | return -EINVAL; | |
1294 | ||
1295 | /* Only one bias flag can be set. */ | |
1296 | if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) && | |
1297 | (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | | |
1298 | GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) || | |
1299 | ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) && | |
1300 | (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) | |
1301 | return -EINVAL; | |
1302 | ||
1303 | return 0; | |
1304 | } | |
1305 | ||
1306 | static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, | |
1307 | unsigned int num_lines) | |
1308 | { | |
1309 | unsigned int i; | |
1310 | u64 flags; | |
1311 | int ret; | |
1312 | ||
1313 | if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX) | |
1314 | return -EINVAL; | |
1315 | ||
1316 | if (memchr_inv(lc->padding, 0, sizeof(lc->padding))) | |
1317 | return -EINVAL; | |
1318 | ||
1319 | for (i = 0; i < num_lines; i++) { | |
1320 | flags = gpio_v2_line_config_flags(lc, i); | |
1321 | ret = gpio_v2_line_flags_validate(flags); | |
1322 | if (ret) | |
1323 | return ret; | |
65cff704 KG |
1324 | |
1325 | /* debounce requires explicit input */ | |
1326 | if (gpio_v2_line_config_debounced(lc, i) && | |
1327 | !(flags & GPIO_V2_LINE_FLAG_INPUT)) | |
1328 | return -EINVAL; | |
3c0d9c63 KG |
1329 | } |
1330 | return 0; | |
1331 | } | |
1332 | ||
1333 | static void gpio_v2_line_config_flags_to_desc_flags(u64 flags, | |
1334 | unsigned long *flagsp) | |
1335 | { | |
1336 | assign_bit(FLAG_ACTIVE_LOW, flagsp, | |
1337 | flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW); | |
1338 | ||
1339 | if (flags & GPIO_V2_LINE_FLAG_OUTPUT) | |
1340 | set_bit(FLAG_IS_OUT, flagsp); | |
1341 | else if (flags & GPIO_V2_LINE_FLAG_INPUT) | |
1342 | clear_bit(FLAG_IS_OUT, flagsp); | |
1343 | ||
73e03419 KG |
1344 | assign_bit(FLAG_EDGE_RISING, flagsp, |
1345 | flags & GPIO_V2_LINE_FLAG_EDGE_RISING); | |
1346 | assign_bit(FLAG_EDGE_FALLING, flagsp, | |
1347 | flags & GPIO_V2_LINE_FLAG_EDGE_FALLING); | |
1348 | ||
3c0d9c63 KG |
1349 | assign_bit(FLAG_OPEN_DRAIN, flagsp, |
1350 | flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN); | |
1351 | assign_bit(FLAG_OPEN_SOURCE, flagsp, | |
1352 | flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE); | |
1353 | ||
1354 | assign_bit(FLAG_PULL_UP, flagsp, | |
1355 | flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP); | |
1356 | assign_bit(FLAG_PULL_DOWN, flagsp, | |
1357 | flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN); | |
1358 | assign_bit(FLAG_BIAS_DISABLE, flagsp, | |
1359 | flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED); | |
26d060e4 KG |
1360 | |
1361 | assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp, | |
1362 | flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME); | |
2068339a DP |
1363 | assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp, |
1364 | flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE); | |
3c0d9c63 KG |
1365 | } |
1366 | ||
1367 | static long linereq_get_values(struct linereq *lr, void __user *ip) | |
1368 | { | |
1369 | struct gpio_v2_line_values lv; | |
1370 | DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); | |
1371 | struct gpio_desc **descs; | |
1372 | unsigned int i, didx, num_get; | |
65cff704 | 1373 | bool val; |
3c0d9c63 KG |
1374 | int ret; |
1375 | ||
1376 | /* NOTE: It's ok to read values of output lines. */ | |
1377 | if (copy_from_user(&lv, ip, sizeof(lv))) | |
1378 | return -EFAULT; | |
1379 | ||
193b6b09 KG |
1380 | /* |
1381 | * gpiod_get_array_value_complex() requires compacted desc and val | |
1382 | * arrays, rather than the sparse ones in lv. | |
1383 | * Calculation of num_get and construction of the desc array is | |
1384 | * optimized to avoid allocation for the desc array for the common | |
1385 | * num_get == 1 case. | |
1386 | */ | |
1387 | /* scan requested lines to calculate the subset to get */ | |
3c0d9c63 KG |
1388 | for (num_get = 0, i = 0; i < lr->num_lines; i++) { |
1389 | if (lv.mask & BIT_ULL(i)) { | |
1390 | num_get++; | |
193b6b09 | 1391 | /* capture desc for the num_get == 1 case */ |
3c0d9c63 KG |
1392 | descs = &lr->lines[i].desc; |
1393 | } | |
1394 | } | |
1395 | ||
1396 | if (num_get == 0) | |
1397 | return -EINVAL; | |
1398 | ||
1399 | if (num_get != 1) { | |
193b6b09 | 1400 | /* build compacted desc array */ |
3c0d9c63 KG |
1401 | descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL); |
1402 | if (!descs) | |
1403 | return -ENOMEM; | |
1404 | for (didx = 0, i = 0; i < lr->num_lines; i++) { | |
1405 | if (lv.mask & BIT_ULL(i)) { | |
1406 | descs[didx] = lr->lines[i].desc; | |
1407 | didx++; | |
1408 | } | |
1409 | } | |
1410 | } | |
1411 | ret = gpiod_get_array_value_complex(false, true, num_get, | |
1412 | descs, NULL, vals); | |
1413 | ||
1414 | if (num_get != 1) | |
1415 | kfree(descs); | |
1416 | if (ret) | |
1417 | return ret; | |
1418 | ||
1419 | lv.bits = 0; | |
1420 | for (didx = 0, i = 0; i < lr->num_lines; i++) { | |
193b6b09 | 1421 | /* unpack compacted vals for the response */ |
3c0d9c63 | 1422 | if (lv.mask & BIT_ULL(i)) { |
65cff704 KG |
1423 | if (lr->lines[i].sw_debounced) |
1424 | val = debounced_value(&lr->lines[i]); | |
1425 | else | |
1426 | val = test_bit(didx, vals); | |
1427 | if (val) | |
3c0d9c63 KG |
1428 | lv.bits |= BIT_ULL(i); |
1429 | didx++; | |
1430 | } | |
1431 | } | |
1432 | ||
1433 | if (copy_to_user(ip, &lv, sizeof(lv))) | |
1434 | return -EFAULT; | |
1435 | ||
1436 | return 0; | |
1437 | } | |
1438 | ||
32d8e3b6 | 1439 | static long linereq_set_values(struct linereq *lr, void __user *ip) |
7b8e00d9 KG |
1440 | { |
1441 | DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); | |
32d8e3b6 | 1442 | struct gpio_v2_line_values lv; |
7b8e00d9 KG |
1443 | struct gpio_desc **descs; |
1444 | unsigned int i, didx, num_set; | |
1445 | int ret; | |
1446 | ||
32d8e3b6 KG |
1447 | if (copy_from_user(&lv, ip, sizeof(lv))) |
1448 | return -EFAULT; | |
1449 | ||
1450 | guard(mutex)(&lr->config_mutex); | |
1451 | ||
193b6b09 KG |
1452 | /* |
1453 | * gpiod_set_array_value_complex() requires compacted desc and val | |
1454 | * arrays, rather than the sparse ones in lv. | |
1455 | * Calculation of num_set and construction of the descs and vals arrays | |
1456 | * is optimized to minimize scanning the lv->mask, and to avoid | |
1457 | * allocation for the desc array for the common num_set == 1 case. | |
1458 | */ | |
7b8e00d9 | 1459 | bitmap_zero(vals, GPIO_V2_LINES_MAX); |
193b6b09 | 1460 | /* scan requested lines to determine the subset to be set */ |
7b8e00d9 | 1461 | for (num_set = 0, i = 0; i < lr->num_lines; i++) { |
32d8e3b6 | 1462 | if (lv.mask & BIT_ULL(i)) { |
193b6b09 | 1463 | /* setting inputs is not allowed */ |
7b8e00d9 KG |
1464 | if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags)) |
1465 | return -EPERM; | |
193b6b09 | 1466 | /* add to compacted values */ |
32d8e3b6 | 1467 | if (lv.bits & BIT_ULL(i)) |
7b8e00d9 KG |
1468 | __set_bit(num_set, vals); |
1469 | num_set++; | |
193b6b09 | 1470 | /* capture desc for the num_set == 1 case */ |
7b8e00d9 KG |
1471 | descs = &lr->lines[i].desc; |
1472 | } | |
1473 | } | |
1474 | if (num_set == 0) | |
1475 | return -EINVAL; | |
1476 | ||
1477 | if (num_set != 1) { | |
193b6b09 | 1478 | /* build compacted desc array */ |
7b8e00d9 KG |
1479 | descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL); |
1480 | if (!descs) | |
1481 | return -ENOMEM; | |
1482 | for (didx = 0, i = 0; i < lr->num_lines; i++) { | |
32d8e3b6 | 1483 | if (lv.mask & BIT_ULL(i)) { |
7b8e00d9 KG |
1484 | descs[didx] = lr->lines[i].desc; |
1485 | didx++; | |
1486 | } | |
1487 | } | |
1488 | } | |
1489 | ret = gpiod_set_array_value_complex(false, true, num_set, | |
1490 | descs, NULL, vals); | |
1491 | ||
1492 | if (num_set != 1) | |
1493 | kfree(descs); | |
1494 | return ret; | |
1495 | } | |
1496 | ||
32d8e3b6 | 1497 | static long linereq_set_config(struct linereq *lr, void __user *ip) |
a54756cb | 1498 | { |
32d8e3b6 | 1499 | struct gpio_v2_line_config lc; |
a54756cb | 1500 | struct gpio_desc *desc; |
b1a92e94 | 1501 | struct line *line; |
a54756cb | 1502 | unsigned int i; |
b1a92e94 | 1503 | u64 flags, edflags; |
a54756cb KG |
1504 | int ret; |
1505 | ||
32d8e3b6 KG |
1506 | if (copy_from_user(&lc, ip, sizeof(lc))) |
1507 | return -EFAULT; | |
1508 | ||
1509 | ret = gpio_v2_line_config_validate(&lc, lr->num_lines); | |
1510 | if (ret) | |
1511 | return ret; | |
1512 | ||
1513 | guard(mutex)(&lr->config_mutex); | |
1514 | ||
a54756cb | 1515 | for (i = 0; i < lr->num_lines; i++) { |
b1a92e94 | 1516 | line = &lr->lines[i]; |
a54756cb | 1517 | desc = lr->lines[i].desc; |
32d8e3b6 | 1518 | flags = gpio_v2_line_config_flags(&lc, i); |
a54756cb | 1519 | gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); |
b1a92e94 | 1520 | edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS; |
925ca369 KG |
1521 | /* |
1522 | * Lines have to be requested explicitly for input | |
1523 | * or output, else the line will be treated "as is". | |
1524 | */ | |
a54756cb | 1525 | if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { |
32d8e3b6 | 1526 | int val = gpio_v2_line_config_output_value(&lc, i); |
925ca369 | 1527 | |
b1a92e94 | 1528 | edge_detector_stop(line); |
925ca369 KG |
1529 | ret = gpiod_direction_output(desc, val); |
1530 | if (ret) | |
1531 | return ret; | |
a54756cb | 1532 | } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { |
925ca369 KG |
1533 | ret = gpiod_direction_input(desc); |
1534 | if (ret) | |
1535 | return ret; | |
a54756cb | 1536 | |
32d8e3b6 | 1537 | ret = edge_detector_update(line, &lc, i, edflags); |
a54756cb KG |
1538 | if (ret) |
1539 | return ret; | |
925ca369 KG |
1540 | } |
1541 | ||
b1a92e94 KG |
1542 | WRITE_ONCE(line->edflags, edflags); |
1543 | ||
9ce4ed5b | 1544 | gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); |
925ca369 KG |
1545 | } |
1546 | return 0; | |
1547 | } | |
1548 | ||
20bddcb4 KG |
1549 | static long linereq_ioctl(struct file *file, unsigned int cmd, |
1550 | unsigned long arg) | |
3c0d9c63 KG |
1551 | { |
1552 | struct linereq *lr = file->private_data; | |
1553 | void __user *ip = (void __user *)arg; | |
1554 | ||
d83cee3d | 1555 | guard(srcu)(&lr->gdev->srcu); |
20bddcb4 | 1556 | |
8574b5b4 | 1557 | if (!rcu_access_pointer(lr->gdev->chip)) |
533aae7c BG |
1558 | return -ENODEV; |
1559 | ||
1cef8b50 AS |
1560 | switch (cmd) { |
1561 | case GPIO_V2_LINE_GET_VALUES_IOCTL: | |
3c0d9c63 | 1562 | return linereq_get_values(lr, ip); |
1cef8b50 | 1563 | case GPIO_V2_LINE_SET_VALUES_IOCTL: |
7b8e00d9 | 1564 | return linereq_set_values(lr, ip); |
1cef8b50 | 1565 | case GPIO_V2_LINE_SET_CONFIG_IOCTL: |
a54756cb | 1566 | return linereq_set_config(lr, ip); |
1cef8b50 AS |
1567 | default: |
1568 | return -EINVAL; | |
1569 | } | |
925ca369 KG |
1570 | } |
1571 | ||
1572 | #ifdef CONFIG_COMPAT | |
3c0d9c63 KG |
1573 | static long linereq_ioctl_compat(struct file *file, unsigned int cmd, |
1574 | unsigned long arg) | |
925ca369 | 1575 | { |
3c0d9c63 | 1576 | return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
925ca369 KG |
1577 | } |
1578 | #endif | |
1579 | ||
20bddcb4 KG |
1580 | static __poll_t linereq_poll(struct file *file, |
1581 | struct poll_table_struct *wait) | |
925ca369 | 1582 | { |
73e03419 KG |
1583 | struct linereq *lr = file->private_data; |
1584 | __poll_t events = 0; | |
925ca369 | 1585 | |
d83cee3d | 1586 | guard(srcu)(&lr->gdev->srcu); |
20bddcb4 | 1587 | |
8574b5b4 | 1588 | if (!rcu_access_pointer(lr->gdev->chip)) |
533aae7c BG |
1589 | return EPOLLHUP | EPOLLERR; |
1590 | ||
73e03419 KG |
1591 | poll_wait(file, &lr->wait, wait); |
1592 | ||
1593 | if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events, | |
1594 | &lr->wait.lock)) | |
1595 | events = EPOLLIN | EPOLLRDNORM; | |
1596 | ||
1597 | return events; | |
883f9198 KG |
1598 | } |
1599 | ||
20bddcb4 KG |
1600 | static ssize_t linereq_read(struct file *file, char __user *buf, |
1601 | size_t count, loff_t *f_ps) | |
883f9198 | 1602 | { |
73e03419 KG |
1603 | struct linereq *lr = file->private_data; |
1604 | struct gpio_v2_line_event le; | |
1605 | ssize_t bytes_read = 0; | |
1606 | int ret; | |
1607 | ||
d83cee3d | 1608 | guard(srcu)(&lr->gdev->srcu); |
20bddcb4 | 1609 | |
8574b5b4 | 1610 | if (!rcu_access_pointer(lr->gdev->chip)) |
533aae7c BG |
1611 | return -ENODEV; |
1612 | ||
73e03419 KG |
1613 | if (count < sizeof(le)) |
1614 | return -EINVAL; | |
1615 | ||
1616 | do { | |
0ebeaab4 KG |
1617 | scoped_guard(spinlock, &lr->wait.lock) { |
1618 | if (kfifo_is_empty(&lr->events)) { | |
1619 | if (bytes_read) | |
1620 | return bytes_read; | |
1621 | ||
1622 | if (file->f_flags & O_NONBLOCK) | |
1623 | return -EAGAIN; | |
1624 | ||
1625 | ret = wait_event_interruptible_locked(lr->wait, | |
1626 | !kfifo_is_empty(&lr->events)); | |
1627 | if (ret) | |
1628 | return ret; | |
73e03419 KG |
1629 | } |
1630 | ||
0ebeaab4 | 1631 | ret = kfifo_out(&lr->events, &le, 1); |
73e03419 | 1632 | } |
73e03419 KG |
1633 | if (ret != 1) { |
1634 | /* | |
1635 | * This should never happen - we were holding the | |
1636 | * lock from the moment we learned the fifo is no | |
1637 | * longer empty until now. | |
1638 | */ | |
1639 | ret = -EIO; | |
1640 | break; | |
1641 | } | |
1642 | ||
1643 | if (copy_to_user(buf + bytes_read, &le, sizeof(le))) | |
1644 | return -EFAULT; | |
1645 | bytes_read += sizeof(le); | |
1646 | } while (count >= bytes_read + sizeof(le)); | |
1647 | ||
1648 | return bytes_read; | |
1649 | } | |
1650 | ||
3c0d9c63 KG |
1651 | static void linereq_free(struct linereq *lr) |
1652 | { | |
9344e34e | 1653 | struct line *line; |
3c0d9c63 KG |
1654 | unsigned int i; |
1655 | ||
a0dda508 BG |
1656 | if (lr->device_unregistered_nb.notifier_call) |
1657 | blocking_notifier_chain_unregister(&lr->gdev->device_notifier, | |
1658 | &lr->device_unregistered_nb); | |
1659 | ||
3c0d9c63 | 1660 | for (i = 0; i < lr->num_lines; i++) { |
9344e34e KG |
1661 | line = &lr->lines[i]; |
1662 | if (!line->desc) | |
1663 | continue; | |
1664 | ||
1665 | edge_detector_stop(line); | |
1666 | if (line_has_supinfo(line)) | |
1667 | supinfo_erase(line); | |
1668 | gpiod_free(line->desc); | |
3c0d9c63 | 1669 | } |
73e03419 | 1670 | kfifo_free(&lr->events); |
3c0d9c63 | 1671 | kfree(lr->label); |
dc0989e3 | 1672 | gpio_device_put(lr->gdev); |
b718fbfe | 1673 | kvfree(lr); |
3c0d9c63 KG |
1674 | } |
1675 | ||
1676 | static int linereq_release(struct inode *inode, struct file *file) | |
1677 | { | |
1678 | struct linereq *lr = file->private_data; | |
1679 | ||
1680 | linereq_free(lr); | |
925ca369 KG |
1681 | return 0; |
1682 | } | |
1683 | ||
0ae3109a BG |
1684 | #ifdef CONFIG_PROC_FS |
1685 | static void linereq_show_fdinfo(struct seq_file *out, struct file *file) | |
1686 | { | |
1687 | struct linereq *lr = file->private_data; | |
1688 | struct device *dev = &lr->gdev->dev; | |
1689 | u16 i; | |
1690 | ||
1691 | seq_printf(out, "gpio-chip:\t%s\n", dev_name(dev)); | |
1692 | ||
1693 | for (i = 0; i < lr->num_lines; i++) | |
1694 | seq_printf(out, "gpio-line:\t%d\n", | |
1695 | gpio_chip_hwgpio(lr->lines[i].desc)); | |
1696 | } | |
1697 | #endif | |
1698 | ||
3c0d9c63 KG |
1699 | static const struct file_operations line_fileops = { |
1700 | .release = linereq_release, | |
73e03419 KG |
1701 | .read = linereq_read, |
1702 | .poll = linereq_poll, | |
925ca369 KG |
1703 | .owner = THIS_MODULE, |
1704 | .llseek = noop_llseek, | |
3c0d9c63 | 1705 | .unlocked_ioctl = linereq_ioctl, |
925ca369 | 1706 | #ifdef CONFIG_COMPAT |
3c0d9c63 | 1707 | .compat_ioctl = linereq_ioctl_compat, |
925ca369 | 1708 | #endif |
0ae3109a BG |
1709 | #ifdef CONFIG_PROC_FS |
1710 | .show_fdinfo = linereq_show_fdinfo, | |
1711 | #endif | |
925ca369 KG |
1712 | }; |
1713 | ||
3c0d9c63 | 1714 | static int linereq_create(struct gpio_device *gdev, void __user *ip) |
925ca369 | 1715 | { |
3c0d9c63 KG |
1716 | struct gpio_v2_line_request ulr; |
1717 | struct gpio_v2_line_config *lc; | |
1718 | struct linereq *lr; | |
925ca369 | 1719 | struct file *file; |
b1a92e94 | 1720 | u64 flags, edflags; |
3c0d9c63 KG |
1721 | unsigned int i; |
1722 | int fd, ret; | |
925ca369 | 1723 | |
3c0d9c63 | 1724 | if (copy_from_user(&ulr, ip, sizeof(ulr))) |
925ca369 | 1725 | return -EFAULT; |
3c0d9c63 KG |
1726 | |
1727 | if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX)) | |
925ca369 KG |
1728 | return -EINVAL; |
1729 | ||
3c0d9c63 KG |
1730 | if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding))) |
1731 | return -EINVAL; | |
925ca369 | 1732 | |
3c0d9c63 KG |
1733 | lc = &ulr.config; |
1734 | ret = gpio_v2_line_config_validate(lc, ulr.num_lines); | |
925ca369 KG |
1735 | if (ret) |
1736 | return ret; | |
1737 | ||
b718fbfe | 1738 | lr = kvzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); |
3c0d9c63 | 1739 | if (!lr) |
925ca369 | 1740 | return -ENOMEM; |
a512635d | 1741 | lr->num_lines = ulr.num_lines; |
3c0d9c63 | 1742 | |
dc0989e3 | 1743 | lr->gdev = gpio_device_get(gdev); |
925ca369 | 1744 | |
65cff704 | 1745 | for (i = 0; i < ulr.num_lines; i++) { |
73e03419 | 1746 | lr->lines[i].req = lr; |
65cff704 KG |
1747 | WRITE_ONCE(lr->lines[i].sw_debounced, 0); |
1748 | INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func); | |
1749 | } | |
73e03419 | 1750 | |
f188ac12 | 1751 | if (ulr.consumer[0] != '\0') { |
3c0d9c63 | 1752 | /* label is only initialized if consumer is set */ |
f188ac12 KG |
1753 | lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1, |
1754 | GFP_KERNEL); | |
3c0d9c63 | 1755 | if (!lr->label) { |
925ca369 | 1756 | ret = -ENOMEM; |
3c0d9c63 | 1757 | goto out_free_linereq; |
925ca369 KG |
1758 | } |
1759 | } | |
1760 | ||
a54756cb | 1761 | mutex_init(&lr->config_mutex); |
73e03419 KG |
1762 | init_waitqueue_head(&lr->wait); |
1763 | lr->event_buffer_size = ulr.event_buffer_size; | |
1764 | if (lr->event_buffer_size == 0) | |
1765 | lr->event_buffer_size = ulr.num_lines * 16; | |
1766 | else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16) | |
1767 | lr->event_buffer_size = GPIO_V2_LINES_MAX * 16; | |
1768 | ||
1769 | atomic_set(&lr->seqno, 0); | |
883f9198 | 1770 | |
925ca369 | 1771 | /* Request each GPIO */ |
3c0d9c63 KG |
1772 | for (i = 0; i < ulr.num_lines; i++) { |
1773 | u32 offset = ulr.offsets[i]; | |
f4e14d45 | 1774 | struct gpio_desc *desc = gpio_device_get_desc(gdev, offset); |
925ca369 KG |
1775 | |
1776 | if (IS_ERR(desc)) { | |
1777 | ret = PTR_ERR(desc); | |
3c0d9c63 | 1778 | goto out_free_linereq; |
925ca369 KG |
1779 | } |
1780 | ||
95a4eed7 | 1781 | ret = gpiod_request_user(desc, lr->label); |
925ca369 | 1782 | if (ret) |
3c0d9c63 KG |
1783 | goto out_free_linereq; |
1784 | ||
1785 | lr->lines[i].desc = desc; | |
1786 | flags = gpio_v2_line_config_flags(lc, i); | |
1787 | gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); | |
925ca369 KG |
1788 | |
1789 | ret = gpiod_set_transitory(desc, false); | |
1790 | if (ret < 0) | |
3c0d9c63 | 1791 | goto out_free_linereq; |
925ca369 | 1792 | |
b1a92e94 | 1793 | edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS; |
925ca369 KG |
1794 | /* |
1795 | * Lines have to be requested explicitly for input | |
1796 | * or output, else the line will be treated "as is". | |
1797 | */ | |
3c0d9c63 KG |
1798 | if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { |
1799 | int val = gpio_v2_line_config_output_value(lc, i); | |
925ca369 KG |
1800 | |
1801 | ret = gpiod_direction_output(desc, val); | |
1802 | if (ret) | |
3c0d9c63 KG |
1803 | goto out_free_linereq; |
1804 | } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { | |
925ca369 KG |
1805 | ret = gpiod_direction_input(desc); |
1806 | if (ret) | |
3c0d9c63 | 1807 | goto out_free_linereq; |
73e03419 | 1808 | |
65cff704 | 1809 | ret = edge_detector_setup(&lr->lines[i], lc, i, |
b1a92e94 | 1810 | edflags); |
73e03419 KG |
1811 | if (ret) |
1812 | goto out_free_linereq; | |
925ca369 KG |
1813 | } |
1814 | ||
b1a92e94 KG |
1815 | lr->lines[i].edflags = edflags; |
1816 | ||
9ce4ed5b | 1817 | gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED); |
925ca369 KG |
1818 | |
1819 | dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", | |
1820 | offset); | |
1821 | } | |
925ca369 | 1822 | |
a0dda508 BG |
1823 | lr->device_unregistered_nb.notifier_call = linereq_unregistered_notify; |
1824 | ret = blocking_notifier_chain_register(&gdev->device_notifier, | |
1825 | &lr->device_unregistered_nb); | |
1826 | if (ret) | |
1827 | goto out_free_linereq; | |
1828 | ||
925ca369 KG |
1829 | fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); |
1830 | if (fd < 0) { | |
1831 | ret = fd; | |
3c0d9c63 | 1832 | goto out_free_linereq; |
925ca369 KG |
1833 | } |
1834 | ||
3c0d9c63 | 1835 | file = anon_inode_getfile("gpio-line", &line_fileops, lr, |
925ca369 KG |
1836 | O_RDONLY | O_CLOEXEC); |
1837 | if (IS_ERR(file)) { | |
1838 | ret = PTR_ERR(file); | |
1839 | goto out_put_unused_fd; | |
1840 | } | |
1841 | ||
3c0d9c63 KG |
1842 | ulr.fd = fd; |
1843 | if (copy_to_user(ip, &ulr, sizeof(ulr))) { | |
925ca369 KG |
1844 | /* |
1845 | * fput() will trigger the release() callback, so do not go onto | |
1846 | * the regular error cleanup path here. | |
1847 | */ | |
1848 | fput(file); | |
1849 | put_unused_fd(fd); | |
1850 | return -EFAULT; | |
1851 | } | |
1852 | ||
1853 | fd_install(fd, file); | |
1854 | ||
1855 | dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", | |
3c0d9c63 | 1856 | lr->num_lines); |
925ca369 KG |
1857 | |
1858 | return 0; | |
1859 | ||
1860 | out_put_unused_fd: | |
1861 | put_unused_fd(fd); | |
3c0d9c63 KG |
1862 | out_free_linereq: |
1863 | linereq_free(lr); | |
925ca369 KG |
1864 | return ret; |
1865 | } | |
1866 | ||
3c0d9c63 | 1867 | #ifdef CONFIG_GPIO_CDEV_V1 |
925ca369 KG |
1868 | |
1869 | /* | |
1870 | * GPIO line event management | |
1871 | */ | |
1872 | ||
1873 | /** | |
1874 | * struct lineevent_state - contains the state of a userspace event | |
1875 | * @gdev: the GPIO device the event pertains to | |
1876 | * @label: consumer label used to tag descriptors | |
1877 | * @desc: the GPIO descriptor held by this event | |
1878 | * @eflags: the event flags this line was requested with | |
1879 | * @irq: the interrupt that trigger in response to events on this GPIO | |
1880 | * @wait: wait queue that handles blocking reads of events | |
91043f55 | 1881 | * @device_unregistered_nb: notifier block for receiving gdev unregister events |
925ca369 KG |
1882 | * @events: KFIFO for the GPIO events |
1883 | * @timestamp: cache for the timestamp storing it between hardirq | |
1884 | * and IRQ thread, used to bring the timestamp close to the actual | |
1885 | * event | |
1886 | */ | |
1887 | struct lineevent_state { | |
1888 | struct gpio_device *gdev; | |
1889 | const char *label; | |
1890 | struct gpio_desc *desc; | |
1891 | u32 eflags; | |
1892 | int irq; | |
1893 | wait_queue_head_t wait; | |
91043f55 | 1894 | struct notifier_block device_unregistered_nb; |
925ca369 KG |
1895 | DECLARE_KFIFO(events, struct gpioevent_data, 16); |
1896 | u64 timestamp; | |
1897 | }; | |
1898 | ||
1899 | #define GPIOEVENT_REQUEST_VALID_FLAGS \ | |
1900 | (GPIOEVENT_REQUEST_RISING_EDGE | \ | |
1901 | GPIOEVENT_REQUEST_FALLING_EDGE) | |
1902 | ||
20bddcb4 KG |
1903 | static __poll_t lineevent_poll(struct file *file, |
1904 | struct poll_table_struct *wait) | |
925ca369 | 1905 | { |
49bc5279 | 1906 | struct lineevent_state *le = file->private_data; |
925ca369 KG |
1907 | __poll_t events = 0; |
1908 | ||
d83cee3d | 1909 | guard(srcu)(&le->gdev->srcu); |
20bddcb4 | 1910 | |
8574b5b4 | 1911 | if (!rcu_access_pointer(le->gdev->chip)) |
533aae7c BG |
1912 | return EPOLLHUP | EPOLLERR; |
1913 | ||
49bc5279 | 1914 | poll_wait(file, &le->wait, wait); |
925ca369 KG |
1915 | |
1916 | if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock)) | |
1917 | events = EPOLLIN | EPOLLRDNORM; | |
1918 | ||
1919 | return events; | |
1920 | } | |
1921 | ||
91043f55 BG |
1922 | static int lineevent_unregistered_notify(struct notifier_block *nb, |
1923 | unsigned long action, void *data) | |
1924 | { | |
1925 | struct lineevent_state *le = container_of(nb, struct lineevent_state, | |
1926 | device_unregistered_nb); | |
1927 | ||
1928 | wake_up_poll(&le->wait, EPOLLIN | EPOLLERR); | |
1929 | ||
1930 | return NOTIFY_OK; | |
1931 | } | |
1932 | ||
163d1719 AS |
1933 | struct compat_gpioeevent_data { |
1934 | compat_u64 timestamp; | |
1935 | u32 id; | |
1936 | }; | |
925ca369 | 1937 | |
20bddcb4 KG |
1938 | static ssize_t lineevent_read(struct file *file, char __user *buf, |
1939 | size_t count, loff_t *f_ps) | |
925ca369 | 1940 | { |
49bc5279 | 1941 | struct lineevent_state *le = file->private_data; |
925ca369 KG |
1942 | struct gpioevent_data ge; |
1943 | ssize_t bytes_read = 0; | |
5ad284ab | 1944 | ssize_t ge_size; |
925ca369 KG |
1945 | int ret; |
1946 | ||
d83cee3d | 1947 | guard(srcu)(&le->gdev->srcu); |
20bddcb4 | 1948 | |
8574b5b4 | 1949 | if (!rcu_access_pointer(le->gdev->chip)) |
533aae7c BG |
1950 | return -ENODEV; |
1951 | ||
5ad284ab AS |
1952 | /* |
1953 | * When compatible system call is being used the struct gpioevent_data, | |
1954 | * in case of at least ia32, has different size due to the alignment | |
1955 | * differences. Because we have first member 64 bits followed by one of | |
1956 | * 32 bits there is no gap between them. The only difference is the | |
1957 | * padding at the end of the data structure. Hence, we calculate the | |
1958 | * actual sizeof() and pass this as an argument to copy_to_user() to | |
1959 | * drop unneeded bytes from the output. | |
1960 | */ | |
163d1719 AS |
1961 | if (compat_need_64bit_alignment_fixup()) |
1962 | ge_size = sizeof(struct compat_gpioeevent_data); | |
1963 | else | |
1964 | ge_size = sizeof(struct gpioevent_data); | |
5ad284ab | 1965 | if (count < ge_size) |
925ca369 KG |
1966 | return -EINVAL; |
1967 | ||
1968 | do { | |
0ebeaab4 KG |
1969 | scoped_guard(spinlock, &le->wait.lock) { |
1970 | if (kfifo_is_empty(&le->events)) { | |
1971 | if (bytes_read) | |
1972 | return bytes_read; | |
1973 | ||
1974 | if (file->f_flags & O_NONBLOCK) | |
1975 | return -EAGAIN; | |
1976 | ||
1977 | ret = wait_event_interruptible_locked(le->wait, | |
1978 | !kfifo_is_empty(&le->events)); | |
1979 | if (ret) | |
1980 | return ret; | |
925ca369 KG |
1981 | } |
1982 | ||
0ebeaab4 | 1983 | ret = kfifo_out(&le->events, &ge, 1); |
925ca369 | 1984 | } |
925ca369 KG |
1985 | if (ret != 1) { |
1986 | /* | |
1987 | * This should never happen - we were holding the lock | |
1988 | * from the moment we learned the fifo is no longer | |
1989 | * empty until now. | |
1990 | */ | |
1991 | ret = -EIO; | |
1992 | break; | |
1993 | } | |
1994 | ||
5ad284ab | 1995 | if (copy_to_user(buf + bytes_read, &ge, ge_size)) |
925ca369 | 1996 | return -EFAULT; |
5ad284ab AS |
1997 | bytes_read += ge_size; |
1998 | } while (count >= bytes_read + ge_size); | |
925ca369 KG |
1999 | |
2000 | return bytes_read; | |
2001 | } | |
2002 | ||
46824272 | 2003 | static void lineevent_free(struct lineevent_state *le) |
925ca369 | 2004 | { |
91043f55 BG |
2005 | if (le->device_unregistered_nb.notifier_call) |
2006 | blocking_notifier_chain_unregister(&le->gdev->device_notifier, | |
2007 | &le->device_unregistered_nb); | |
46824272 | 2008 | if (le->irq) |
b3449087 | 2009 | free_irq_label(free_irq(le->irq, le)); |
46824272 KG |
2010 | if (le->desc) |
2011 | gpiod_free(le->desc); | |
925ca369 | 2012 | kfree(le->label); |
dc0989e3 | 2013 | gpio_device_put(le->gdev); |
925ca369 | 2014 | kfree(le); |
46824272 KG |
2015 | } |
2016 | ||
2017 | static int lineevent_release(struct inode *inode, struct file *file) | |
2018 | { | |
2019 | lineevent_free(file->private_data); | |
925ca369 KG |
2020 | return 0; |
2021 | } | |
2022 | ||
20bddcb4 KG |
2023 | static long lineevent_ioctl(struct file *file, unsigned int cmd, |
2024 | unsigned long arg) | |
925ca369 | 2025 | { |
49bc5279 | 2026 | struct lineevent_state *le = file->private_data; |
925ca369 KG |
2027 | void __user *ip = (void __user *)arg; |
2028 | struct gpiohandle_data ghd; | |
2029 | ||
d83cee3d | 2030 | guard(srcu)(&le->gdev->srcu); |
20bddcb4 | 2031 | |
8574b5b4 | 2032 | if (!rcu_access_pointer(le->gdev->chip)) |
533aae7c BG |
2033 | return -ENODEV; |
2034 | ||
925ca369 KG |
2035 | /* |
2036 | * We can get the value for an event line but not set it, | |
2037 | * because it is input by definition. | |
2038 | */ | |
2039 | if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { | |
2040 | int val; | |
2041 | ||
2042 | memset(&ghd, 0, sizeof(ghd)); | |
2043 | ||
2044 | val = gpiod_get_value_cansleep(le->desc); | |
2045 | if (val < 0) | |
2046 | return val; | |
2047 | ghd.values[0] = val; | |
2048 | ||
2049 | if (copy_to_user(ip, &ghd, sizeof(ghd))) | |
2050 | return -EFAULT; | |
2051 | ||
2052 | return 0; | |
2053 | } | |
2054 | return -EINVAL; | |
2055 | } | |
2056 | ||
2057 | #ifdef CONFIG_COMPAT | |
49bc5279 | 2058 | static long lineevent_ioctl_compat(struct file *file, unsigned int cmd, |
925ca369 KG |
2059 | unsigned long arg) |
2060 | { | |
49bc5279 | 2061 | return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
925ca369 KG |
2062 | } |
2063 | #endif | |
2064 | ||
2065 | static const struct file_operations lineevent_fileops = { | |
2066 | .release = lineevent_release, | |
2067 | .read = lineevent_read, | |
2068 | .poll = lineevent_poll, | |
2069 | .owner = THIS_MODULE, | |
2070 | .llseek = noop_llseek, | |
2071 | .unlocked_ioctl = lineevent_ioctl, | |
2072 | #ifdef CONFIG_COMPAT | |
2073 | .compat_ioctl = lineevent_ioctl_compat, | |
2074 | #endif | |
2075 | }; | |
2076 | ||
2077 | static irqreturn_t lineevent_irq_thread(int irq, void *p) | |
2078 | { | |
2079 | struct lineevent_state *le = p; | |
2080 | struct gpioevent_data ge; | |
2081 | int ret; | |
2082 | ||
2083 | /* Do not leak kernel stack to userspace */ | |
2084 | memset(&ge, 0, sizeof(ge)); | |
2085 | ||
2086 | /* | |
2087 | * We may be running from a nested threaded interrupt in which case | |
2088 | * we didn't get the timestamp from lineevent_irq_handler(). | |
2089 | */ | |
2090 | if (!le->timestamp) | |
2091 | ge.timestamp = ktime_get_ns(); | |
2092 | else | |
2093 | ge.timestamp = le->timestamp; | |
2094 | ||
2095 | if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE | |
2096 | && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { | |
2097 | int level = gpiod_get_value_cansleep(le->desc); | |
2098 | ||
2099 | if (level) | |
2100 | /* Emit low-to-high event */ | |
2101 | ge.id = GPIOEVENT_EVENT_RISING_EDGE; | |
2102 | else | |
2103 | /* Emit high-to-low event */ | |
2104 | ge.id = GPIOEVENT_EVENT_FALLING_EDGE; | |
2105 | } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) { | |
2106 | /* Emit low-to-high event */ | |
2107 | ge.id = GPIOEVENT_EVENT_RISING_EDGE; | |
2108 | } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { | |
2109 | /* Emit high-to-low event */ | |
2110 | ge.id = GPIOEVENT_EVENT_FALLING_EDGE; | |
2111 | } else { | |
2112 | return IRQ_NONE; | |
2113 | } | |
2114 | ||
2115 | ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge, | |
2116 | 1, &le->wait.lock); | |
2117 | if (ret) | |
2118 | wake_up_poll(&le->wait, EPOLLIN); | |
2119 | else | |
2120 | pr_debug_ratelimited("event FIFO is full - event dropped\n"); | |
2121 | ||
2122 | return IRQ_HANDLED; | |
2123 | } | |
2124 | ||
2125 | static irqreturn_t lineevent_irq_handler(int irq, void *p) | |
2126 | { | |
2127 | struct lineevent_state *le = p; | |
2128 | ||
2129 | /* | |
2130 | * Just store the timestamp in hardirq context so we get it as | |
2131 | * close in time as possible to the actual event. | |
2132 | */ | |
2133 | le->timestamp = ktime_get_ns(); | |
2134 | ||
2135 | return IRQ_WAKE_THREAD; | |
2136 | } | |
2137 | ||
2138 | static int lineevent_create(struct gpio_device *gdev, void __user *ip) | |
2139 | { | |
2140 | struct gpioevent_request eventreq; | |
2141 | struct lineevent_state *le; | |
2142 | struct gpio_desc *desc; | |
2143 | struct file *file; | |
2144 | u32 offset; | |
2145 | u32 lflags; | |
2146 | u32 eflags; | |
2147 | int fd; | |
2148 | int ret; | |
46824272 | 2149 | int irq, irqflags = 0; |
b3449087 | 2150 | char *label; |
925ca369 KG |
2151 | |
2152 | if (copy_from_user(&eventreq, ip, sizeof(eventreq))) | |
2153 | return -EFAULT; | |
2154 | ||
2155 | offset = eventreq.lineoffset; | |
2156 | lflags = eventreq.handleflags; | |
2157 | eflags = eventreq.eventflags; | |
2158 | ||
f4e14d45 | 2159 | desc = gpio_device_get_desc(gdev, offset); |
925ca369 KG |
2160 | if (IS_ERR(desc)) |
2161 | return PTR_ERR(desc); | |
2162 | ||
2163 | /* Return an error if a unknown flag is set */ | |
2164 | if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || | |
2165 | (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) | |
2166 | return -EINVAL; | |
2167 | ||
2168 | /* This is just wrong: we don't look for events on output lines */ | |
2169 | if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) || | |
2170 | (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || | |
2171 | (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) | |
2172 | return -EINVAL; | |
2173 | ||
2174 | /* Only one bias flag can be set. */ | |
2175 | if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && | |
2176 | (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | | |
2177 | GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || | |
2178 | ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && | |
2179 | (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) | |
2180 | return -EINVAL; | |
2181 | ||
2182 | le = kzalloc(sizeof(*le), GFP_KERNEL); | |
2183 | if (!le) | |
2184 | return -ENOMEM; | |
dc0989e3 | 2185 | le->gdev = gpio_device_get(gdev); |
925ca369 | 2186 | |
f188ac12 KG |
2187 | if (eventreq.consumer_label[0] != '\0') { |
2188 | /* label is only initialized if consumer_label is set */ | |
2189 | le->label = kstrndup(eventreq.consumer_label, | |
2190 | sizeof(eventreq.consumer_label) - 1, | |
2191 | GFP_KERNEL); | |
925ca369 KG |
2192 | if (!le->label) { |
2193 | ret = -ENOMEM; | |
2194 | goto out_free_le; | |
2195 | } | |
2196 | } | |
2197 | ||
95a4eed7 | 2198 | ret = gpiod_request_user(desc, le->label); |
925ca369 | 2199 | if (ret) |
46824272 | 2200 | goto out_free_le; |
925ca369 KG |
2201 | le->desc = desc; |
2202 | le->eflags = eflags; | |
2203 | ||
c274b58a | 2204 | linehandle_flags_to_desc_flags(lflags, &desc->flags); |
925ca369 KG |
2205 | |
2206 | ret = gpiod_direction_input(desc); | |
2207 | if (ret) | |
46824272 | 2208 | goto out_free_le; |
925ca369 | 2209 | |
9ce4ed5b | 2210 | gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED); |
925ca369 | 2211 | |
46824272 KG |
2212 | irq = gpiod_to_irq(desc); |
2213 | if (irq <= 0) { | |
925ca369 | 2214 | ret = -ENODEV; |
46824272 | 2215 | goto out_free_le; |
925ca369 KG |
2216 | } |
2217 | ||
2218 | if (eflags & GPIOEVENT_REQUEST_RISING_EDGE) | |
2219 | irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? | |
2220 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; | |
2221 | if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE) | |
2222 | irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? | |
2223 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; | |
2224 | irqflags |= IRQF_ONESHOT; | |
2225 | ||
2226 | INIT_KFIFO(le->events); | |
2227 | init_waitqueue_head(&le->wait); | |
2228 | ||
91043f55 BG |
2229 | le->device_unregistered_nb.notifier_call = lineevent_unregistered_notify; |
2230 | ret = blocking_notifier_chain_register(&gdev->device_notifier, | |
2231 | &le->device_unregistered_nb); | |
2232 | if (ret) | |
2233 | goto out_free_le; | |
2234 | ||
b3449087 | 2235 | label = make_irq_label(le->label); |
b3b95964 BG |
2236 | if (IS_ERR(label)) { |
2237 | ret = PTR_ERR(label); | |
b3449087 BG |
2238 | goto out_free_le; |
2239 | } | |
2240 | ||
925ca369 | 2241 | /* Request a thread to read the events */ |
69bef19d | 2242 | ret = request_threaded_irq(irq, |
a18512e3 KG |
2243 | lineevent_irq_handler, |
2244 | lineevent_irq_thread, | |
2245 | irqflags, | |
b3449087 | 2246 | label, |
a18512e3 | 2247 | le); |
b3449087 BG |
2248 | if (ret) { |
2249 | free_irq_label(label); | |
46824272 | 2250 | goto out_free_le; |
b3449087 | 2251 | } |
925ca369 | 2252 | |
69bef19d ML |
2253 | le->irq = irq; |
2254 | ||
925ca369 KG |
2255 | fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); |
2256 | if (fd < 0) { | |
2257 | ret = fd; | |
46824272 | 2258 | goto out_free_le; |
925ca369 KG |
2259 | } |
2260 | ||
2261 | file = anon_inode_getfile("gpio-event", | |
2262 | &lineevent_fileops, | |
2263 | le, | |
2264 | O_RDONLY | O_CLOEXEC); | |
2265 | if (IS_ERR(file)) { | |
2266 | ret = PTR_ERR(file); | |
2267 | goto out_put_unused_fd; | |
2268 | } | |
2269 | ||
2270 | eventreq.fd = fd; | |
2271 | if (copy_to_user(ip, &eventreq, sizeof(eventreq))) { | |
2272 | /* | |
2273 | * fput() will trigger the release() callback, so do not go onto | |
2274 | * the regular error cleanup path here. | |
2275 | */ | |
2276 | fput(file); | |
2277 | put_unused_fd(fd); | |
2278 | return -EFAULT; | |
2279 | } | |
2280 | ||
2281 | fd_install(fd, file); | |
2282 | ||
2283 | return 0; | |
2284 | ||
2285 | out_put_unused_fd: | |
2286 | put_unused_fd(fd); | |
925ca369 | 2287 | out_free_le: |
46824272 | 2288 | lineevent_free(le); |
925ca369 KG |
2289 | return ret; |
2290 | } | |
2291 | ||
aad95584 KG |
2292 | static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2, |
2293 | struct gpioline_info *info_v1) | |
2294 | { | |
2295 | u64 flagsv2 = info_v2->flags; | |
2296 | ||
2297 | memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name)); | |
2298 | memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer)); | |
2299 | info_v1->line_offset = info_v2->offset; | |
2300 | info_v1->flags = 0; | |
2301 | ||
2302 | if (flagsv2 & GPIO_V2_LINE_FLAG_USED) | |
2303 | info_v1->flags |= GPIOLINE_FLAG_KERNEL; | |
2304 | ||
2305 | if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT) | |
2306 | info_v1->flags |= GPIOLINE_FLAG_IS_OUT; | |
2307 | ||
2308 | if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW) | |
2309 | info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW; | |
2310 | ||
2311 | if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN) | |
2312 | info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN; | |
2313 | if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE) | |
2314 | info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE; | |
2315 | ||
2316 | if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP) | |
2317 | info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; | |
2318 | if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) | |
2319 | info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; | |
2320 | if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED) | |
2321 | info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE; | |
2322 | } | |
2323 | ||
2324 | static void gpio_v2_line_info_changed_to_v1( | |
2325 | struct gpio_v2_line_info_changed *lic_v2, | |
2326 | struct gpioline_info_changed *lic_v1) | |
2327 | { | |
cb8f63b8 | 2328 | memset(lic_v1, 0, sizeof(*lic_v1)); |
aad95584 KG |
2329 | gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info); |
2330 | lic_v1->timestamp = lic_v2->timestamp_ns; | |
2331 | lic_v1->event_type = lic_v2->event_type; | |
2332 | } | |
2333 | ||
3c0d9c63 KG |
2334 | #endif /* CONFIG_GPIO_CDEV_V1 */ |
2335 | ||
925ca369 | 2336 | static void gpio_desc_to_lineinfo(struct gpio_desc *desc, |
aad95584 | 2337 | struct gpio_v2_line_info *info) |
925ca369 | 2338 | { |
1cdc605c | 2339 | unsigned long dflags; |
1f2bcb8c | 2340 | const char *label; |
925ca369 | 2341 | |
d83cee3d BG |
2342 | CLASS(gpio_chip_guard, guard)(desc); |
2343 | if (!guard.gc) | |
2344 | return; | |
2345 | ||
69e4e136 | 2346 | memset(info, 0, sizeof(*info)); |
aad95584 | 2347 | info->offset = gpio_chip_hwgpio(desc); |
925ca369 | 2348 | |
35b54533 BG |
2349 | if (desc->name) |
2350 | strscpy(info->name, desc->name, sizeof(info->name)); | |
2351 | ||
2352 | dflags = READ_ONCE(desc->flags); | |
925ca369 | 2353 | |
35b54533 BG |
2354 | scoped_guard(srcu, &desc->srcu) { |
2355 | label = gpiod_get_label(desc); | |
2356 | if (label && test_bit(FLAG_REQUESTED, &dflags)) | |
2357 | strscpy(info->consumer, label, | |
2358 | sizeof(info->consumer)); | |
1cdc605c | 2359 | } |
925ca369 KG |
2360 | |
2361 | /* | |
1cdc605c KG |
2362 | * Userspace only need know that the kernel is using this GPIO so it |
2363 | * can't use it. | |
2364 | * The calculation of the used flag is slightly racy, as it may read | |
2365 | * desc, gc and pinctrl state without a lock covering all three at | |
2366 | * once. Worst case if the line is in transition and the calculation | |
2367 | * is inconsistent then it looks to the user like they performed the | |
2368 | * read on the other side of the transition - but that can always | |
2369 | * happen. | |
2370 | * The definitive test that a line is available to userspace is to | |
2371 | * request it. | |
925ca369 | 2372 | */ |
1cdc605c KG |
2373 | if (test_bit(FLAG_REQUESTED, &dflags) || |
2374 | test_bit(FLAG_IS_HOGGED, &dflags) || | |
2375 | test_bit(FLAG_USED_AS_IRQ, &dflags) || | |
2376 | test_bit(FLAG_EXPORT, &dflags) || | |
2377 | test_bit(FLAG_SYSFS, &dflags) || | |
d83cee3d BG |
2378 | !gpiochip_line_is_valid(guard.gc, info->offset) || |
2379 | !pinctrl_gpio_can_use_line(guard.gc, info->offset)) | |
aad95584 KG |
2380 | info->flags |= GPIO_V2_LINE_FLAG_USED; |
2381 | ||
1cdc605c | 2382 | if (test_bit(FLAG_IS_OUT, &dflags)) |
aad95584 KG |
2383 | info->flags |= GPIO_V2_LINE_FLAG_OUTPUT; |
2384 | else | |
2385 | info->flags |= GPIO_V2_LINE_FLAG_INPUT; | |
2386 | ||
1cdc605c | 2387 | if (test_bit(FLAG_ACTIVE_LOW, &dflags)) |
aad95584 KG |
2388 | info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW; |
2389 | ||
1cdc605c | 2390 | if (test_bit(FLAG_OPEN_DRAIN, &dflags)) |
aad95584 | 2391 | info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; |
1cdc605c | 2392 | if (test_bit(FLAG_OPEN_SOURCE, &dflags)) |
aad95584 KG |
2393 | info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; |
2394 | ||
1cdc605c | 2395 | if (test_bit(FLAG_BIAS_DISABLE, &dflags)) |
aad95584 | 2396 | info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED; |
1cdc605c | 2397 | if (test_bit(FLAG_PULL_DOWN, &dflags)) |
aad95584 | 2398 | info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN; |
1cdc605c | 2399 | if (test_bit(FLAG_PULL_UP, &dflags)) |
aad95584 | 2400 | info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; |
925ca369 | 2401 | |
1cdc605c | 2402 | if (test_bit(FLAG_EDGE_RISING, &dflags)) |
73e03419 | 2403 | info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; |
1cdc605c | 2404 | if (test_bit(FLAG_EDGE_FALLING, &dflags)) |
73e03419 KG |
2405 | info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; |
2406 | ||
1cdc605c | 2407 | if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &dflags)) |
26d060e4 | 2408 | info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME; |
1cdc605c | 2409 | else if (test_bit(FLAG_EVENT_CLOCK_HTE, &dflags)) |
2068339a | 2410 | info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE; |
925ca369 KG |
2411 | } |
2412 | ||
2413 | struct gpio_chardev_data { | |
2414 | struct gpio_device *gdev; | |
2415 | wait_queue_head_t wait; | |
aad95584 | 2416 | DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32); |
925ca369 | 2417 | struct notifier_block lineinfo_changed_nb; |
d2e2586a | 2418 | struct notifier_block device_unregistered_nb; |
925ca369 | 2419 | unsigned long *watched_lines; |
aad95584 KG |
2420 | #ifdef CONFIG_GPIO_CDEV_V1 |
2421 | atomic_t watch_abi_version; | |
2422 | #endif | |
925ca369 KG |
2423 | }; |
2424 | ||
2e202ad8 KG |
2425 | static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip) |
2426 | { | |
2427 | struct gpio_device *gdev = cdev->gdev; | |
2428 | struct gpiochip_info chipinfo; | |
2429 | ||
2430 | memset(&chipinfo, 0, sizeof(chipinfo)); | |
2431 | ||
2432 | strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name)); | |
2433 | strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label)); | |
2434 | chipinfo.lines = gdev->ngpio; | |
2435 | if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) | |
2436 | return -EFAULT; | |
2437 | return 0; | |
2438 | } | |
2439 | ||
aad95584 KG |
2440 | #ifdef CONFIG_GPIO_CDEV_V1 |
2441 | /* | |
2442 | * returns 0 if the versions match, else the previously selected ABI version | |
2443 | */ | |
2444 | static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata, | |
2445 | unsigned int version) | |
2446 | { | |
2447 | int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version); | |
2448 | ||
2449 | if (abiv == version) | |
2450 | return 0; | |
2451 | ||
2452 | return abiv; | |
2453 | } | |
2e202ad8 KG |
2454 | |
2455 | static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip, | |
2456 | bool watch) | |
2457 | { | |
2458 | struct gpio_desc *desc; | |
2459 | struct gpioline_info lineinfo; | |
2460 | struct gpio_v2_line_info lineinfo_v2; | |
2461 | ||
2462 | if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) | |
2463 | return -EFAULT; | |
2464 | ||
2465 | /* this doubles as a range check on line_offset */ | |
f4e14d45 | 2466 | desc = gpio_device_get_desc(cdev->gdev, lineinfo.line_offset); |
2e202ad8 KG |
2467 | if (IS_ERR(desc)) |
2468 | return PTR_ERR(desc); | |
2469 | ||
2470 | if (watch) { | |
2471 | if (lineinfo_ensure_abi_version(cdev, 1)) | |
2472 | return -EPERM; | |
2473 | ||
2474 | if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines)) | |
2475 | return -EBUSY; | |
2476 | } | |
2477 | ||
2478 | gpio_desc_to_lineinfo(desc, &lineinfo_v2); | |
2479 | gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); | |
2480 | ||
2481 | if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { | |
2482 | if (watch) | |
2483 | clear_bit(lineinfo.line_offset, cdev->watched_lines); | |
2484 | return -EFAULT; | |
2485 | } | |
2486 | ||
2487 | return 0; | |
2488 | } | |
aad95584 KG |
2489 | #endif |
2490 | ||
2491 | static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip, | |
2492 | bool watch) | |
2493 | { | |
2494 | struct gpio_desc *desc; | |
2495 | struct gpio_v2_line_info lineinfo; | |
2496 | ||
2497 | if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) | |
2498 | return -EFAULT; | |
2499 | ||
2500 | if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding))) | |
2501 | return -EINVAL; | |
2502 | ||
f4e14d45 | 2503 | desc = gpio_device_get_desc(cdev->gdev, lineinfo.offset); |
aad95584 KG |
2504 | if (IS_ERR(desc)) |
2505 | return PTR_ERR(desc); | |
2506 | ||
2507 | if (watch) { | |
2508 | #ifdef CONFIG_GPIO_CDEV_V1 | |
2509 | if (lineinfo_ensure_abi_version(cdev, 2)) | |
2510 | return -EPERM; | |
2511 | #endif | |
2512 | if (test_and_set_bit(lineinfo.offset, cdev->watched_lines)) | |
2513 | return -EBUSY; | |
2514 | } | |
2515 | gpio_desc_to_lineinfo(desc, &lineinfo); | |
9344e34e | 2516 | supinfo_to_lineinfo(desc, &lineinfo); |
aad95584 KG |
2517 | |
2518 | if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { | |
2519 | if (watch) | |
2520 | clear_bit(lineinfo.offset, cdev->watched_lines); | |
2521 | return -EFAULT; | |
2522 | } | |
2523 | ||
2524 | return 0; | |
2525 | } | |
2526 | ||
2e202ad8 KG |
2527 | static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip) |
2528 | { | |
2529 | __u32 offset; | |
2530 | ||
2531 | if (copy_from_user(&offset, ip, sizeof(offset))) | |
2532 | return -EFAULT; | |
2533 | ||
2534 | if (offset >= cdev->gdev->ngpio) | |
2535 | return -EINVAL; | |
2536 | ||
2537 | if (!test_and_clear_bit(offset, cdev->watched_lines)) | |
2538 | return -EBUSY; | |
2539 | ||
2540 | return 0; | |
2541 | } | |
2542 | ||
20bddcb4 KG |
2543 | /* |
2544 | * gpio_ioctl() - ioctl handler for the GPIO chardev | |
2545 | */ | |
2546 | static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
925ca369 | 2547 | { |
e2b781c5 KG |
2548 | struct gpio_chardev_data *cdev = file->private_data; |
2549 | struct gpio_device *gdev = cdev->gdev; | |
925ca369 | 2550 | void __user *ip = (void __user *)arg; |
925ca369 | 2551 | |
d83cee3d | 2552 | guard(srcu)(&gdev->srcu); |
20bddcb4 | 2553 | |
925ca369 | 2554 | /* We fail any subsequent ioctl():s when the chip is gone */ |
8574b5b4 | 2555 | if (!rcu_access_pointer(gdev->chip)) |
925ca369 KG |
2556 | return -ENODEV; |
2557 | ||
2558 | /* Fill in the struct and pass to userspace */ | |
1cef8b50 AS |
2559 | switch (cmd) { |
2560 | case GPIO_GET_CHIPINFO_IOCTL: | |
2e202ad8 | 2561 | return chipinfo_get(cdev, ip); |
3c0d9c63 | 2562 | #ifdef CONFIG_GPIO_CDEV_V1 |
1cef8b50 | 2563 | case GPIO_GET_LINEHANDLE_IOCTL: |
925ca369 | 2564 | return linehandle_create(gdev, ip); |
1cef8b50 | 2565 | case GPIO_GET_LINEEVENT_IOCTL: |
925ca369 | 2566 | return lineevent_create(gdev, ip); |
1cef8b50 AS |
2567 | case GPIO_GET_LINEINFO_IOCTL: |
2568 | return lineinfo_get_v1(cdev, ip, false); | |
2569 | case GPIO_GET_LINEINFO_WATCH_IOCTL: | |
2570 | return lineinfo_get_v1(cdev, ip, true); | |
3c0d9c63 | 2571 | #endif /* CONFIG_GPIO_CDEV_V1 */ |
1cef8b50 AS |
2572 | case GPIO_V2_GET_LINEINFO_IOCTL: |
2573 | return lineinfo_get(cdev, ip, false); | |
2574 | case GPIO_V2_GET_LINEINFO_WATCH_IOCTL: | |
2575 | return lineinfo_get(cdev, ip, true); | |
2576 | case GPIO_V2_GET_LINE_IOCTL: | |
3c0d9c63 | 2577 | return linereq_create(gdev, ip); |
1cef8b50 | 2578 | case GPIO_GET_LINEINFO_UNWATCH_IOCTL: |
2e202ad8 | 2579 | return lineinfo_unwatch(cdev, ip); |
1cef8b50 AS |
2580 | default: |
2581 | return -EINVAL; | |
925ca369 | 2582 | } |
925ca369 KG |
2583 | } |
2584 | ||
2585 | #ifdef CONFIG_COMPAT | |
49bc5279 | 2586 | static long gpio_ioctl_compat(struct file *file, unsigned int cmd, |
925ca369 KG |
2587 | unsigned long arg) |
2588 | { | |
49bc5279 | 2589 | return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
925ca369 KG |
2590 | } |
2591 | #endif | |
2592 | ||
925ca369 KG |
2593 | static int lineinfo_changed_notify(struct notifier_block *nb, |
2594 | unsigned long action, void *data) | |
2595 | { | |
e82bbd67 BG |
2596 | struct gpio_chardev_data *cdev = |
2597 | container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb); | |
aad95584 | 2598 | struct gpio_v2_line_info_changed chg; |
925ca369 KG |
2599 | struct gpio_desc *desc = data; |
2600 | int ret; | |
2601 | ||
e2b781c5 | 2602 | if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) |
925ca369 KG |
2603 | return NOTIFY_DONE; |
2604 | ||
2605 | memset(&chg, 0, sizeof(chg)); | |
925ca369 | 2606 | chg.event_type = action; |
aad95584 | 2607 | chg.timestamp_ns = ktime_get_ns(); |
925ca369 | 2608 | gpio_desc_to_lineinfo(desc, &chg.info); |
9344e34e | 2609 | supinfo_to_lineinfo(desc, &chg.info); |
925ca369 | 2610 | |
e2b781c5 | 2611 | ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); |
925ca369 | 2612 | if (ret) |
e2b781c5 | 2613 | wake_up_poll(&cdev->wait, EPOLLIN); |
925ca369 KG |
2614 | else |
2615 | pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n"); | |
2616 | ||
2617 | return NOTIFY_OK; | |
2618 | } | |
2619 | ||
d2e2586a BG |
2620 | static int gpio_device_unregistered_notify(struct notifier_block *nb, |
2621 | unsigned long action, void *data) | |
2622 | { | |
2623 | struct gpio_chardev_data *cdev = container_of(nb, | |
2624 | struct gpio_chardev_data, | |
2625 | device_unregistered_nb); | |
2626 | ||
2627 | wake_up_poll(&cdev->wait, EPOLLIN | EPOLLERR); | |
2628 | ||
2629 | return NOTIFY_OK; | |
2630 | } | |
2631 | ||
20bddcb4 KG |
2632 | static __poll_t lineinfo_watch_poll(struct file *file, |
2633 | struct poll_table_struct *pollt) | |
925ca369 | 2634 | { |
e2b781c5 | 2635 | struct gpio_chardev_data *cdev = file->private_data; |
925ca369 KG |
2636 | __poll_t events = 0; |
2637 | ||
d83cee3d | 2638 | guard(srcu)(&cdev->gdev->srcu); |
20bddcb4 | 2639 | |
8574b5b4 | 2640 | if (!rcu_access_pointer(cdev->gdev->chip)) |
533aae7c BG |
2641 | return EPOLLHUP | EPOLLERR; |
2642 | ||
e2b781c5 | 2643 | poll_wait(file, &cdev->wait, pollt); |
925ca369 | 2644 | |
e2b781c5 KG |
2645 | if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events, |
2646 | &cdev->wait.lock)) | |
925ca369 KG |
2647 | events = EPOLLIN | EPOLLRDNORM; |
2648 | ||
2649 | return events; | |
2650 | } | |
2651 | ||
20bddcb4 KG |
2652 | static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, |
2653 | size_t count, loff_t *off) | |
925ca369 | 2654 | { |
e2b781c5 | 2655 | struct gpio_chardev_data *cdev = file->private_data; |
aad95584 | 2656 | struct gpio_v2_line_info_changed event; |
925ca369 KG |
2657 | ssize_t bytes_read = 0; |
2658 | int ret; | |
aad95584 | 2659 | size_t event_size; |
925ca369 | 2660 | |
d83cee3d | 2661 | guard(srcu)(&cdev->gdev->srcu); |
20bddcb4 | 2662 | |
8574b5b4 | 2663 | if (!rcu_access_pointer(cdev->gdev->chip)) |
533aae7c BG |
2664 | return -ENODEV; |
2665 | ||
aad95584 KG |
2666 | #ifndef CONFIG_GPIO_CDEV_V1 |
2667 | event_size = sizeof(struct gpio_v2_line_info_changed); | |
2668 | if (count < event_size) | |
925ca369 | 2669 | return -EINVAL; |
aad95584 | 2670 | #endif |
925ca369 KG |
2671 | |
2672 | do { | |
0ebeaab4 KG |
2673 | scoped_guard(spinlock, &cdev->wait.lock) { |
2674 | if (kfifo_is_empty(&cdev->events)) { | |
2675 | if (bytes_read) | |
2676 | return bytes_read; | |
2677 | ||
2678 | if (file->f_flags & O_NONBLOCK) | |
2679 | return -EAGAIN; | |
2680 | ||
2681 | ret = wait_event_interruptible_locked(cdev->wait, | |
2682 | !kfifo_is_empty(&cdev->events)); | |
2683 | if (ret) | |
2684 | return ret; | |
925ca369 | 2685 | } |
aad95584 | 2686 | #ifdef CONFIG_GPIO_CDEV_V1 |
0ebeaab4 KG |
2687 | /* must be after kfifo check so watch_abi_version is set */ |
2688 | if (atomic_read(&cdev->watch_abi_version) == 2) | |
2689 | event_size = sizeof(struct gpio_v2_line_info_changed); | |
2690 | else | |
2691 | event_size = sizeof(struct gpioline_info_changed); | |
2692 | if (count < event_size) | |
2693 | return -EINVAL; | |
aad95584 | 2694 | #endif |
0ebeaab4 KG |
2695 | ret = kfifo_out(&cdev->events, &event, 1); |
2696 | } | |
925ca369 KG |
2697 | if (ret != 1) { |
2698 | ret = -EIO; | |
2699 | break; | |
2700 | /* We should never get here. See lineevent_read(). */ | |
2701 | } | |
2702 | ||
aad95584 KG |
2703 | #ifdef CONFIG_GPIO_CDEV_V1 |
2704 | if (event_size == sizeof(struct gpio_v2_line_info_changed)) { | |
2705 | if (copy_to_user(buf + bytes_read, &event, event_size)) | |
2706 | return -EFAULT; | |
2707 | } else { | |
2708 | struct gpioline_info_changed event_v1; | |
2709 | ||
2710 | gpio_v2_line_info_changed_to_v1(&event, &event_v1); | |
2711 | if (copy_to_user(buf + bytes_read, &event_v1, | |
2712 | event_size)) | |
2713 | return -EFAULT; | |
2714 | } | |
2715 | #else | |
2716 | if (copy_to_user(buf + bytes_read, &event, event_size)) | |
925ca369 | 2717 | return -EFAULT; |
aad95584 KG |
2718 | #endif |
2719 | bytes_read += event_size; | |
925ca369 KG |
2720 | } while (count >= bytes_read + sizeof(event)); |
2721 | ||
2722 | return bytes_read; | |
2723 | } | |
2724 | ||
2725 | /** | |
2726 | * gpio_chrdev_open() - open the chardev for ioctl operations | |
2727 | * @inode: inode for this chardev | |
49bc5279 | 2728 | * @file: file struct for storing private data |
925ca369 KG |
2729 | * Returns 0 on success |
2730 | */ | |
49bc5279 | 2731 | static int gpio_chrdev_open(struct inode *inode, struct file *file) |
925ca369 KG |
2732 | { |
2733 | struct gpio_device *gdev = container_of(inode->i_cdev, | |
a18512e3 | 2734 | struct gpio_device, chrdev); |
e2b781c5 | 2735 | struct gpio_chardev_data *cdev; |
925ca369 KG |
2736 | int ret = -ENOMEM; |
2737 | ||
d83cee3d | 2738 | guard(srcu)(&gdev->srcu); |
bdbbae24 | 2739 | |
925ca369 | 2740 | /* Fail on open if the backing gpiochip is gone */ |
8574b5b4 | 2741 | if (!rcu_access_pointer(gdev->chip)) |
20bddcb4 | 2742 | return -ENODEV; |
925ca369 | 2743 | |
e2b781c5 KG |
2744 | cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); |
2745 | if (!cdev) | |
20bddcb4 | 2746 | return -ENODEV; |
925ca369 | 2747 | |
3c7a47f6 | 2748 | cdev->watched_lines = bitmap_zalloc(gdev->ngpio, GFP_KERNEL); |
e2b781c5 KG |
2749 | if (!cdev->watched_lines) |
2750 | goto out_free_cdev; | |
925ca369 | 2751 | |
e2b781c5 KG |
2752 | init_waitqueue_head(&cdev->wait); |
2753 | INIT_KFIFO(cdev->events); | |
dc0989e3 | 2754 | cdev->gdev = gpio_device_get(gdev); |
925ca369 | 2755 | |
e2b781c5 | 2756 | cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify; |
17a7ca35 | 2757 | ret = blocking_notifier_chain_register(&gdev->line_state_notifier, |
e2b781c5 | 2758 | &cdev->lineinfo_changed_nb); |
925ca369 KG |
2759 | if (ret) |
2760 | goto out_free_bitmap; | |
2761 | ||
d2e2586a BG |
2762 | cdev->device_unregistered_nb.notifier_call = |
2763 | gpio_device_unregistered_notify; | |
2764 | ret = blocking_notifier_chain_register(&gdev->device_notifier, | |
2765 | &cdev->device_unregistered_nb); | |
2766 | if (ret) | |
2767 | goto out_unregister_line_notifier; | |
2768 | ||
e2b781c5 | 2769 | file->private_data = cdev; |
925ca369 | 2770 | |
49bc5279 | 2771 | ret = nonseekable_open(inode, file); |
925ca369 | 2772 | if (ret) |
d2e2586a | 2773 | goto out_unregister_device_notifier; |
925ca369 KG |
2774 | |
2775 | return ret; | |
2776 | ||
d2e2586a BG |
2777 | out_unregister_device_notifier: |
2778 | blocking_notifier_chain_unregister(&gdev->device_notifier, | |
2779 | &cdev->device_unregistered_nb); | |
2780 | out_unregister_line_notifier: | |
17a7ca35 | 2781 | blocking_notifier_chain_unregister(&gdev->line_state_notifier, |
e2b781c5 | 2782 | &cdev->lineinfo_changed_nb); |
925ca369 | 2783 | out_free_bitmap: |
dc0989e3 | 2784 | gpio_device_put(gdev); |
e2b781c5 KG |
2785 | bitmap_free(cdev->watched_lines); |
2786 | out_free_cdev: | |
2787 | kfree(cdev); | |
925ca369 KG |
2788 | return ret; |
2789 | } | |
2790 | ||
2791 | /** | |
2792 | * gpio_chrdev_release() - close chardev after ioctl operations | |
2793 | * @inode: inode for this chardev | |
49bc5279 | 2794 | * @file: file struct for storing private data |
925ca369 KG |
2795 | * Returns 0 on success |
2796 | */ | |
49bc5279 | 2797 | static int gpio_chrdev_release(struct inode *inode, struct file *file) |
925ca369 | 2798 | { |
e2b781c5 KG |
2799 | struct gpio_chardev_data *cdev = file->private_data; |
2800 | struct gpio_device *gdev = cdev->gdev; | |
925ca369 | 2801 | |
e2b781c5 | 2802 | bitmap_free(cdev->watched_lines); |
d2e2586a BG |
2803 | blocking_notifier_chain_unregister(&gdev->device_notifier, |
2804 | &cdev->device_unregistered_nb); | |
17a7ca35 | 2805 | blocking_notifier_chain_unregister(&gdev->line_state_notifier, |
e2b781c5 | 2806 | &cdev->lineinfo_changed_nb); |
dc0989e3 | 2807 | gpio_device_put(gdev); |
e2b781c5 | 2808 | kfree(cdev); |
925ca369 KG |
2809 | |
2810 | return 0; | |
2811 | } | |
2812 | ||
2813 | static const struct file_operations gpio_fileops = { | |
2814 | .release = gpio_chrdev_release, | |
2815 | .open = gpio_chrdev_open, | |
2816 | .poll = lineinfo_watch_poll, | |
2817 | .read = lineinfo_watch_read, | |
2818 | .owner = THIS_MODULE, | |
2819 | .llseek = no_llseek, | |
2820 | .unlocked_ioctl = gpio_ioctl, | |
2821 | #ifdef CONFIG_COMPAT | |
2822 | .compat_ioctl = gpio_ioctl_compat, | |
2823 | #endif | |
2824 | }; | |
2825 | ||
2826 | int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) | |
2827 | { | |
d83cee3d | 2828 | struct gpio_chip *gc; |
925ca369 KG |
2829 | int ret; |
2830 | ||
2831 | cdev_init(&gdev->chrdev, &gpio_fileops); | |
2832 | gdev->chrdev.owner = THIS_MODULE; | |
2833 | gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id); | |
2834 | ||
2835 | ret = cdev_device_add(&gdev->chrdev, &gdev->dev); | |
2836 | if (ret) | |
2837 | return ret; | |
2838 | ||
d83cee3d | 2839 | guard(srcu)(&gdev->srcu); |
91510d59 BG |
2840 | gc = srcu_dereference(gdev->chip, &gdev->srcu); |
2841 | if (!gc) | |
d83cee3d BG |
2842 | return -ENODEV; |
2843 | ||
2844 | chip_dbg(gc, "added GPIO chardev (%d:%d)\n", MAJOR(devt), gdev->id); | |
925ca369 KG |
2845 | |
2846 | return 0; | |
2847 | } | |
2848 | ||
2849 | void gpiolib_cdev_unregister(struct gpio_device *gdev) | |
2850 | { | |
2851 | cdev_device_del(&gdev->chrdev, &gdev->dev); | |
a067419b | 2852 | blocking_notifier_call_chain(&gdev->device_notifier, 0, NULL); |
925ca369 | 2853 | } |