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