Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #include <linux/anon_inodes.h>
0004 #include <linux/atomic.h>
0005 #include <linux/bitmap.h>
0006 #include <linux/build_bug.h>
0007 #include <linux/cdev.h>
0008 #include <linux/compat.h>
0009 #include <linux/compiler.h>
0010 #include <linux/device.h>
0011 #include <linux/err.h>
0012 #include <linux/file.h>
0013 #include <linux/gpio.h>
0014 #include <linux/gpio/driver.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/irqreturn.h>
0017 #include <linux/kernel.h>
0018 #include <linux/kfifo.h>
0019 #include <linux/module.h>
0020 #include <linux/mutex.h>
0021 #include <linux/pinctrl/consumer.h>
0022 #include <linux/poll.h>
0023 #include <linux/spinlock.h>
0024 #include <linux/timekeeping.h>
0025 #include <linux/uaccess.h>
0026 #include <linux/workqueue.h>
0027 #include <linux/hte.h>
0028 #include <uapi/linux/gpio.h>
0029 
0030 #include "gpiolib.h"
0031 #include "gpiolib-cdev.h"
0032 
0033 /*
0034  * Array sizes must ensure 64-bit alignment and not create holes in the
0035  * struct packing.
0036  */
0037 static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2));
0038 static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8));
0039 
0040 /*
0041  * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility
0042  */
0043 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8));
0044 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8));
0045 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8));
0046 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8));
0047 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8));
0048 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8));
0049 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8));
0050 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8));
0051 
0052 /* Character device interface to GPIO.
0053  *
0054  * The GPIO character device, /dev/gpiochipN, provides userspace an
0055  * interface to gpiolib GPIOs via ioctl()s.
0056  */
0057 
0058 /*
0059  * GPIO line handle management
0060  */
0061 
0062 #ifdef CONFIG_GPIO_CDEV_V1
0063 /**
0064  * struct linehandle_state - contains the state of a userspace handle
0065  * @gdev: the GPIO device the handle pertains to
0066  * @label: consumer label used to tag descriptors
0067  * @descs: the GPIO descriptors held by this handle
0068  * @num_descs: the number of descriptors held in the descs array
0069  */
0070 struct linehandle_state {
0071     struct gpio_device *gdev;
0072     const char *label;
0073     struct gpio_desc *descs[GPIOHANDLES_MAX];
0074     u32 num_descs;
0075 };
0076 
0077 #define GPIOHANDLE_REQUEST_VALID_FLAGS \
0078     (GPIOHANDLE_REQUEST_INPUT | \
0079     GPIOHANDLE_REQUEST_OUTPUT | \
0080     GPIOHANDLE_REQUEST_ACTIVE_LOW | \
0081     GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
0082     GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
0083     GPIOHANDLE_REQUEST_BIAS_DISABLE | \
0084     GPIOHANDLE_REQUEST_OPEN_DRAIN | \
0085     GPIOHANDLE_REQUEST_OPEN_SOURCE)
0086 
0087 static int linehandle_validate_flags(u32 flags)
0088 {
0089     /* Return an error if an unknown flag is set */
0090     if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
0091         return -EINVAL;
0092 
0093     /*
0094      * Do not allow both INPUT & OUTPUT flags to be set as they are
0095      * contradictory.
0096      */
0097     if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
0098         (flags & GPIOHANDLE_REQUEST_OUTPUT))
0099         return -EINVAL;
0100 
0101     /*
0102      * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
0103      * the hardware actually supports enabling both at the same time the
0104      * electrical result would be disastrous.
0105      */
0106     if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
0107         (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
0108         return -EINVAL;
0109 
0110     /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
0111     if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
0112         ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
0113          (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
0114         return -EINVAL;
0115 
0116     /* Bias flags only allowed for input or output mode. */
0117     if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
0118           (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
0119         ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
0120          (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
0121          (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
0122         return -EINVAL;
0123 
0124     /* Only one bias flag can be set. */
0125     if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
0126          (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
0127                GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
0128         ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
0129          (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
0130         return -EINVAL;
0131 
0132     return 0;
0133 }
0134 
0135 static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
0136 {
0137     assign_bit(FLAG_ACTIVE_LOW, flagsp,
0138            lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
0139     assign_bit(FLAG_OPEN_DRAIN, flagsp,
0140            lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
0141     assign_bit(FLAG_OPEN_SOURCE, flagsp,
0142            lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
0143     assign_bit(FLAG_PULL_UP, flagsp,
0144            lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
0145     assign_bit(FLAG_PULL_DOWN, flagsp,
0146            lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
0147     assign_bit(FLAG_BIAS_DISABLE, flagsp,
0148            lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
0149 }
0150 
0151 static long linehandle_set_config(struct linehandle_state *lh,
0152                   void __user *ip)
0153 {
0154     struct gpiohandle_config gcnf;
0155     struct gpio_desc *desc;
0156     int i, ret;
0157     u32 lflags;
0158 
0159     if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
0160         return -EFAULT;
0161 
0162     lflags = gcnf.flags;
0163     ret = linehandle_validate_flags(lflags);
0164     if (ret)
0165         return ret;
0166 
0167     for (i = 0; i < lh->num_descs; i++) {
0168         desc = lh->descs[i];
0169         linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags);
0170 
0171         /*
0172          * Lines have to be requested explicitly for input
0173          * or output, else the line will be treated "as is".
0174          */
0175         if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
0176             int val = !!gcnf.default_values[i];
0177 
0178             ret = gpiod_direction_output(desc, val);
0179             if (ret)
0180                 return ret;
0181         } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
0182             ret = gpiod_direction_input(desc);
0183             if (ret)
0184                 return ret;
0185         }
0186 
0187         blocking_notifier_call_chain(&desc->gdev->notifier,
0188                          GPIO_V2_LINE_CHANGED_CONFIG,
0189                          desc);
0190     }
0191     return 0;
0192 }
0193 
0194 static long linehandle_ioctl(struct file *file, unsigned int cmd,
0195                  unsigned long arg)
0196 {
0197     struct linehandle_state *lh = file->private_data;
0198     void __user *ip = (void __user *)arg;
0199     struct gpiohandle_data ghd;
0200     DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
0201     unsigned int i;
0202     int ret;
0203 
0204     switch (cmd) {
0205     case GPIOHANDLE_GET_LINE_VALUES_IOCTL:
0206         /* NOTE: It's okay to read values of output lines */
0207         ret = gpiod_get_array_value_complex(false, true,
0208                             lh->num_descs, lh->descs,
0209                             NULL, vals);
0210         if (ret)
0211             return ret;
0212 
0213         memset(&ghd, 0, sizeof(ghd));
0214         for (i = 0; i < lh->num_descs; i++)
0215             ghd.values[i] = test_bit(i, vals);
0216 
0217         if (copy_to_user(ip, &ghd, sizeof(ghd)))
0218             return -EFAULT;
0219 
0220         return 0;
0221     case GPIOHANDLE_SET_LINE_VALUES_IOCTL:
0222         /*
0223          * All line descriptors were created at once with the same
0224          * flags so just check if the first one is really output.
0225          */
0226         if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
0227             return -EPERM;
0228 
0229         if (copy_from_user(&ghd, ip, sizeof(ghd)))
0230             return -EFAULT;
0231 
0232         /* Clamp all values to [0,1] */
0233         for (i = 0; i < lh->num_descs; i++)
0234             __assign_bit(i, vals, ghd.values[i]);
0235 
0236         /* Reuse the array setting function */
0237         return gpiod_set_array_value_complex(false,
0238                              true,
0239                              lh->num_descs,
0240                              lh->descs,
0241                              NULL,
0242                              vals);
0243     case GPIOHANDLE_SET_CONFIG_IOCTL:
0244         return linehandle_set_config(lh, ip);
0245     default:
0246         return -EINVAL;
0247     }
0248 }
0249 
0250 #ifdef CONFIG_COMPAT
0251 static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
0252                     unsigned long arg)
0253 {
0254     return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
0255 }
0256 #endif
0257 
0258 static void linehandle_free(struct linehandle_state *lh)
0259 {
0260     int i;
0261 
0262     for (i = 0; i < lh->num_descs; i++)
0263         if (lh->descs[i])
0264             gpiod_free(lh->descs[i]);
0265     kfree(lh->label);
0266     put_device(&lh->gdev->dev);
0267     kfree(lh);
0268 }
0269 
0270 static int linehandle_release(struct inode *inode, struct file *file)
0271 {
0272     linehandle_free(file->private_data);
0273     return 0;
0274 }
0275 
0276 static const struct file_operations linehandle_fileops = {
0277     .release = linehandle_release,
0278     .owner = THIS_MODULE,
0279     .llseek = noop_llseek,
0280     .unlocked_ioctl = linehandle_ioctl,
0281 #ifdef CONFIG_COMPAT
0282     .compat_ioctl = linehandle_ioctl_compat,
0283 #endif
0284 };
0285 
0286 static int linehandle_create(struct gpio_device *gdev, void __user *ip)
0287 {
0288     struct gpiohandle_request handlereq;
0289     struct linehandle_state *lh;
0290     struct file *file;
0291     int fd, i, ret;
0292     u32 lflags;
0293 
0294     if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
0295         return -EFAULT;
0296     if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
0297         return -EINVAL;
0298 
0299     lflags = handlereq.flags;
0300 
0301     ret = linehandle_validate_flags(lflags);
0302     if (ret)
0303         return ret;
0304 
0305     lh = kzalloc(sizeof(*lh), GFP_KERNEL);
0306     if (!lh)
0307         return -ENOMEM;
0308     lh->gdev = gdev;
0309     get_device(&gdev->dev);
0310 
0311     if (handlereq.consumer_label[0] != '\0') {
0312         /* label is only initialized if consumer_label is set */
0313         lh->label = kstrndup(handlereq.consumer_label,
0314                      sizeof(handlereq.consumer_label) - 1,
0315                      GFP_KERNEL);
0316         if (!lh->label) {
0317             ret = -ENOMEM;
0318             goto out_free_lh;
0319         }
0320     }
0321 
0322     lh->num_descs = handlereq.lines;
0323 
0324     /* Request each GPIO */
0325     for (i = 0; i < handlereq.lines; i++) {
0326         u32 offset = handlereq.lineoffsets[i];
0327         struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
0328 
0329         if (IS_ERR(desc)) {
0330             ret = PTR_ERR(desc);
0331             goto out_free_lh;
0332         }
0333 
0334         ret = gpiod_request_user(desc, lh->label);
0335         if (ret)
0336             goto out_free_lh;
0337         lh->descs[i] = desc;
0338         linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
0339 
0340         ret = gpiod_set_transitory(desc, false);
0341         if (ret < 0)
0342             goto out_free_lh;
0343 
0344         /*
0345          * Lines have to be requested explicitly for input
0346          * or output, else the line will be treated "as is".
0347          */
0348         if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
0349             int val = !!handlereq.default_values[i];
0350 
0351             ret = gpiod_direction_output(desc, val);
0352             if (ret)
0353                 goto out_free_lh;
0354         } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
0355             ret = gpiod_direction_input(desc);
0356             if (ret)
0357                 goto out_free_lh;
0358         }
0359 
0360         blocking_notifier_call_chain(&desc->gdev->notifier,
0361                          GPIO_V2_LINE_CHANGED_REQUESTED, desc);
0362 
0363         dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
0364             offset);
0365     }
0366 
0367     fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
0368     if (fd < 0) {
0369         ret = fd;
0370         goto out_free_lh;
0371     }
0372 
0373     file = anon_inode_getfile("gpio-linehandle",
0374                   &linehandle_fileops,
0375                   lh,
0376                   O_RDONLY | O_CLOEXEC);
0377     if (IS_ERR(file)) {
0378         ret = PTR_ERR(file);
0379         goto out_put_unused_fd;
0380     }
0381 
0382     handlereq.fd = fd;
0383     if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
0384         /*
0385          * fput() will trigger the release() callback, so do not go onto
0386          * the regular error cleanup path here.
0387          */
0388         fput(file);
0389         put_unused_fd(fd);
0390         return -EFAULT;
0391     }
0392 
0393     fd_install(fd, file);
0394 
0395     dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
0396         lh->num_descs);
0397 
0398     return 0;
0399 
0400 out_put_unused_fd:
0401     put_unused_fd(fd);
0402 out_free_lh:
0403     linehandle_free(lh);
0404     return ret;
0405 }
0406 #endif /* CONFIG_GPIO_CDEV_V1 */
0407 
0408 /**
0409  * struct line - contains the state of a requested line
0410  * @desc: the GPIO descriptor for this line.
0411  * @req: the corresponding line request
0412  * @irq: the interrupt triggered in response to events on this GPIO
0413  * @eflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or
0414  * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied
0415  * @timestamp_ns: cache for the timestamp storing it between hardirq and
0416  * IRQ thread, used to bring the timestamp close to the actual event
0417  * @req_seqno: the seqno for the current edge event in the sequence of
0418  * events for the corresponding line request. This is drawn from the @req.
0419  * @line_seqno: the seqno for the current edge event in the sequence of
0420  * events for this line.
0421  * @work: the worker that implements software debouncing
0422  * @sw_debounced: flag indicating if the software debouncer is active
0423  * @level: the current debounced physical level of the line
0424  * @hdesc: the Hardware Timestamp Engine (HTE) descriptor
0425  * @raw_level: the line level at the time of event
0426  * @total_discard_seq: the running counter of the discarded events
0427  * @last_seqno: the last sequence number before debounce period expires
0428  */
0429 struct line {
0430     struct gpio_desc *desc;
0431     /*
0432      * -- edge detector specific fields --
0433      */
0434     struct linereq *req;
0435     unsigned int irq;
0436     /*
0437      * The flags for the active edge detector configuration.
0438      *
0439      * edflags is set by linereq_create(), linereq_free(), and
0440      * linereq_set_config_unlocked(), which are themselves mutually
0441      * exclusive, and is accessed by edge_irq_thread(),
0442      * process_hw_ts_thread() and debounce_work_func(),
0443      * which can all live with a slightly stale value.
0444      */
0445     u64 edflags;
0446     /*
0447      * timestamp_ns and req_seqno are accessed only by
0448      * edge_irq_handler() and edge_irq_thread(), which are themselves
0449      * mutually exclusive, so no additional protection is necessary.
0450      */
0451     u64 timestamp_ns;
0452     u32 req_seqno;
0453     /*
0454      * line_seqno is accessed by either edge_irq_thread() or
0455      * debounce_work_func(), which are themselves mutually exclusive,
0456      * so no additional protection is necessary.
0457      */
0458     u32 line_seqno;
0459     /*
0460      * -- debouncer specific fields --
0461      */
0462     struct delayed_work work;
0463     /*
0464      * sw_debounce is accessed by linereq_set_config(), which is the
0465      * only setter, and linereq_get_values(), which can live with a
0466      * slightly stale value.
0467      */
0468     unsigned int sw_debounced;
0469     /*
0470      * level is accessed by debounce_work_func(), which is the only
0471      * setter, and linereq_get_values() which can live with a slightly
0472      * stale value.
0473      */
0474     unsigned int level;
0475 #ifdef CONFIG_HTE
0476     struct hte_ts_desc hdesc;
0477     /*
0478      * HTE provider sets line level at the time of event. The valid
0479      * value is 0 or 1 and negative value for an error.
0480      */
0481     int raw_level;
0482     /*
0483      * when sw_debounce is set on HTE enabled line, this is running
0484      * counter of the discarded events.
0485      */
0486     u32 total_discard_seq;
0487     /*
0488      * when sw_debounce is set on HTE enabled line, this variable records
0489      * last sequence number before debounce period expires.
0490      */
0491     u32 last_seqno;
0492 #endif /* CONFIG_HTE */
0493 };
0494 
0495 /**
0496  * struct linereq - contains the state of a userspace line request
0497  * @gdev: the GPIO device the line request pertains to
0498  * @label: consumer label used to tag GPIO descriptors
0499  * @num_lines: the number of lines in the lines array
0500  * @wait: wait queue that handles blocking reads of events
0501  * @event_buffer_size: the number of elements allocated in @events
0502  * @events: KFIFO for the GPIO events
0503  * @seqno: the sequence number for edge events generated on all lines in
0504  * this line request.  Note that this is not used when @num_lines is 1, as
0505  * the line_seqno is then the same and is cheaper to calculate.
0506  * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
0507  * of configuration, particularly multi-step accesses to desc flags.
0508  * @lines: the lines held by this line request, with @num_lines elements.
0509  */
0510 struct linereq {
0511     struct gpio_device *gdev;
0512     const char *label;
0513     u32 num_lines;
0514     wait_queue_head_t wait;
0515     u32 event_buffer_size;
0516     DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
0517     atomic_t seqno;
0518     struct mutex config_mutex;
0519     struct line lines[];
0520 };
0521 
0522 #define GPIO_V2_LINE_BIAS_FLAGS \
0523     (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
0524      GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
0525      GPIO_V2_LINE_FLAG_BIAS_DISABLED)
0526 
0527 #define GPIO_V2_LINE_DIRECTION_FLAGS \
0528     (GPIO_V2_LINE_FLAG_INPUT | \
0529      GPIO_V2_LINE_FLAG_OUTPUT)
0530 
0531 #define GPIO_V2_LINE_DRIVE_FLAGS \
0532     (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
0533      GPIO_V2_LINE_FLAG_OPEN_SOURCE)
0534 
0535 #define GPIO_V2_LINE_EDGE_FLAGS \
0536     (GPIO_V2_LINE_FLAG_EDGE_RISING | \
0537      GPIO_V2_LINE_FLAG_EDGE_FALLING)
0538 
0539 #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS
0540 
0541 #define GPIO_V2_LINE_VALID_FLAGS \
0542     (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
0543      GPIO_V2_LINE_DIRECTION_FLAGS | \
0544      GPIO_V2_LINE_DRIVE_FLAGS | \
0545      GPIO_V2_LINE_EDGE_FLAGS | \
0546      GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
0547      GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
0548      GPIO_V2_LINE_BIAS_FLAGS)
0549 
0550 /* subset of flags relevant for edge detector configuration */
0551 #define GPIO_V2_LINE_EDGE_DETECTOR_FLAGS \
0552     (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
0553      GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
0554      GPIO_V2_LINE_EDGE_FLAGS)
0555 
0556 static void linereq_put_event(struct linereq *lr,
0557                   struct gpio_v2_line_event *le)
0558 {
0559     bool overflow = false;
0560 
0561     spin_lock(&lr->wait.lock);
0562     if (kfifo_is_full(&lr->events)) {
0563         overflow = true;
0564         kfifo_skip(&lr->events);
0565     }
0566     kfifo_in(&lr->events, le, 1);
0567     spin_unlock(&lr->wait.lock);
0568     if (!overflow)
0569         wake_up_poll(&lr->wait, EPOLLIN);
0570     else
0571         pr_debug_ratelimited("event FIFO is full - event dropped\n");
0572 }
0573 
0574 static u64 line_event_timestamp(struct line *line)
0575 {
0576     if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
0577         return ktime_get_real_ns();
0578     else if (IS_ENABLED(CONFIG_HTE) &&
0579          test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))
0580         return line->timestamp_ns;
0581 
0582     return ktime_get_ns();
0583 }
0584 
0585 static u32 line_event_id(int level)
0586 {
0587     return level ? GPIO_V2_LINE_EVENT_RISING_EDGE :
0588                GPIO_V2_LINE_EVENT_FALLING_EDGE;
0589 }
0590 
0591 #ifdef CONFIG_HTE
0592 
0593 static enum hte_return process_hw_ts_thread(void *p)
0594 {
0595     struct line *line;
0596     struct linereq *lr;
0597     struct gpio_v2_line_event le;
0598     u64 edflags;
0599     int level;
0600 
0601     if (!p)
0602         return HTE_CB_HANDLED;
0603 
0604     line = p;
0605     lr = line->req;
0606 
0607     memset(&le, 0, sizeof(le));
0608 
0609     le.timestamp_ns = line->timestamp_ns;
0610     edflags = READ_ONCE(line->edflags);
0611 
0612     switch (edflags & GPIO_V2_LINE_EDGE_FLAGS) {
0613     case GPIO_V2_LINE_FLAG_EDGE_BOTH:
0614         level = (line->raw_level >= 0) ?
0615                 line->raw_level :
0616                 gpiod_get_raw_value_cansleep(line->desc);
0617 
0618         if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
0619             level = !level;
0620 
0621         le.id = line_event_id(level);
0622         break;
0623     case GPIO_V2_LINE_FLAG_EDGE_RISING:
0624         le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
0625         break;
0626     case GPIO_V2_LINE_FLAG_EDGE_FALLING:
0627         le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
0628         break;
0629     default:
0630         return HTE_CB_HANDLED;
0631     }
0632     le.line_seqno = line->line_seqno;
0633     le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
0634     le.offset = gpio_chip_hwgpio(line->desc);
0635 
0636     linereq_put_event(lr, &le);
0637 
0638     return HTE_CB_HANDLED;
0639 }
0640 
0641 static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p)
0642 {
0643     struct line *line;
0644     struct linereq *lr;
0645     int diff_seqno = 0;
0646 
0647     if (!ts || !p)
0648         return HTE_CB_HANDLED;
0649 
0650     line = p;
0651     line->timestamp_ns = ts->tsc;
0652     line->raw_level = ts->raw_level;
0653     lr = line->req;
0654 
0655     if (READ_ONCE(line->sw_debounced)) {
0656         line->total_discard_seq++;
0657         line->last_seqno = ts->seq;
0658         mod_delayed_work(system_wq, &line->work,
0659           usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
0660     } else {
0661         if (unlikely(ts->seq < line->line_seqno))
0662             return HTE_CB_HANDLED;
0663 
0664         diff_seqno = ts->seq - line->line_seqno;
0665         line->line_seqno = ts->seq;
0666         if (lr->num_lines != 1)
0667             line->req_seqno = atomic_add_return(diff_seqno,
0668                                 &lr->seqno);
0669 
0670         return HTE_RUN_SECOND_CB;
0671     }
0672 
0673     return HTE_CB_HANDLED;
0674 }
0675 
0676 static int hte_edge_setup(struct line *line, u64 eflags)
0677 {
0678     int ret;
0679     unsigned long flags = 0;
0680     struct hte_ts_desc *hdesc = &line->hdesc;
0681 
0682     if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
0683         flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
0684                  HTE_FALLING_EDGE_TS :
0685                  HTE_RISING_EDGE_TS;
0686     if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
0687         flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
0688                  HTE_RISING_EDGE_TS :
0689                  HTE_FALLING_EDGE_TS;
0690 
0691     line->total_discard_seq = 0;
0692 
0693     hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags, NULL,
0694                line->desc);
0695 
0696     ret = hte_ts_get(NULL, hdesc, 0);
0697     if (ret)
0698         return ret;
0699 
0700     return hte_request_ts_ns(hdesc, process_hw_ts, process_hw_ts_thread,
0701                  line);
0702 }
0703 
0704 #else
0705 
0706 static int hte_edge_setup(struct line *line, u64 eflags)
0707 {
0708     return 0;
0709 }
0710 #endif /* CONFIG_HTE */
0711 
0712 static irqreturn_t edge_irq_thread(int irq, void *p)
0713 {
0714     struct line *line = p;
0715     struct linereq *lr = line->req;
0716     struct gpio_v2_line_event le;
0717 
0718     /* Do not leak kernel stack to userspace */
0719     memset(&le, 0, sizeof(le));
0720 
0721     if (line->timestamp_ns) {
0722         le.timestamp_ns = line->timestamp_ns;
0723     } else {
0724         /*
0725          * We may be running from a nested threaded interrupt in
0726          * which case we didn't get the timestamp from
0727          * edge_irq_handler().
0728          */
0729         le.timestamp_ns = line_event_timestamp(line);
0730         if (lr->num_lines != 1)
0731             line->req_seqno = atomic_inc_return(&lr->seqno);
0732     }
0733     line->timestamp_ns = 0;
0734 
0735     switch (READ_ONCE(line->edflags) & GPIO_V2_LINE_EDGE_FLAGS) {
0736     case GPIO_V2_LINE_FLAG_EDGE_BOTH:
0737         le.id = line_event_id(gpiod_get_value_cansleep(line->desc));
0738         break;
0739     case GPIO_V2_LINE_FLAG_EDGE_RISING:
0740         le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
0741         break;
0742     case GPIO_V2_LINE_FLAG_EDGE_FALLING:
0743         le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
0744         break;
0745     default:
0746         return IRQ_NONE;
0747     }
0748     line->line_seqno++;
0749     le.line_seqno = line->line_seqno;
0750     le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
0751     le.offset = gpio_chip_hwgpio(line->desc);
0752 
0753     linereq_put_event(lr, &le);
0754 
0755     return IRQ_HANDLED;
0756 }
0757 
0758 static irqreturn_t edge_irq_handler(int irq, void *p)
0759 {
0760     struct line *line = p;
0761     struct linereq *lr = line->req;
0762 
0763     /*
0764      * Just store the timestamp in hardirq context so we get it as
0765      * close in time as possible to the actual event.
0766      */
0767     line->timestamp_ns = line_event_timestamp(line);
0768 
0769     if (lr->num_lines != 1)
0770         line->req_seqno = atomic_inc_return(&lr->seqno);
0771 
0772     return IRQ_WAKE_THREAD;
0773 }
0774 
0775 /*
0776  * returns the current debounced logical value.
0777  */
0778 static bool debounced_value(struct line *line)
0779 {
0780     bool value;
0781 
0782     /*
0783      * minor race - debouncer may be stopped here, so edge_detector_stop()
0784      * must leave the value unchanged so the following will read the level
0785      * from when the debouncer was last running.
0786      */
0787     value = READ_ONCE(line->level);
0788 
0789     if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
0790         value = !value;
0791 
0792     return value;
0793 }
0794 
0795 static irqreturn_t debounce_irq_handler(int irq, void *p)
0796 {
0797     struct line *line = p;
0798 
0799     mod_delayed_work(system_wq, &line->work,
0800         usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
0801 
0802     return IRQ_HANDLED;
0803 }
0804 
0805 static void debounce_work_func(struct work_struct *work)
0806 {
0807     struct gpio_v2_line_event le;
0808     struct line *line = container_of(work, struct line, work.work);
0809     struct linereq *lr;
0810     u64 eflags, edflags = READ_ONCE(line->edflags);
0811     int level = -1;
0812 #ifdef CONFIG_HTE
0813     int diff_seqno;
0814 
0815     if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
0816         level = line->raw_level;
0817 #endif
0818     if (level < 0)
0819         level = gpiod_get_raw_value_cansleep(line->desc);
0820     if (level < 0) {
0821         pr_debug_ratelimited("debouncer failed to read line value\n");
0822         return;
0823     }
0824 
0825     if (READ_ONCE(line->level) == level)
0826         return;
0827 
0828     WRITE_ONCE(line->level, level);
0829 
0830     /* -- edge detection -- */
0831     eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
0832     if (!eflags)
0833         return;
0834 
0835     /* switch from physical level to logical - if they differ */
0836     if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
0837         level = !level;
0838 
0839     /* ignore edges that are not being monitored */
0840     if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
0841         ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
0842         return;
0843 
0844     /* Do not leak kernel stack to userspace */
0845     memset(&le, 0, sizeof(le));
0846 
0847     lr = line->req;
0848     le.timestamp_ns = line_event_timestamp(line);
0849     le.offset = gpio_chip_hwgpio(line->desc);
0850 #ifdef CONFIG_HTE
0851     if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) {
0852         /* discard events except the last one */
0853         line->total_discard_seq -= 1;
0854         diff_seqno = line->last_seqno - line->total_discard_seq -
0855                 line->line_seqno;
0856         line->line_seqno = line->last_seqno - line->total_discard_seq;
0857         le.line_seqno = line->line_seqno;
0858         le.seqno = (lr->num_lines == 1) ?
0859             le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno);
0860     } else
0861 #endif /* CONFIG_HTE */
0862     {
0863         line->line_seqno++;
0864         le.line_seqno = line->line_seqno;
0865         le.seqno = (lr->num_lines == 1) ?
0866             le.line_seqno : atomic_inc_return(&lr->seqno);
0867     }
0868 
0869     le.id = line_event_id(level);
0870 
0871     linereq_put_event(lr, &le);
0872 }
0873 
0874 static int debounce_setup(struct line *line, unsigned int debounce_period_us)
0875 {
0876     unsigned long irqflags;
0877     int ret, level, irq;
0878 
0879     /* try hardware */
0880     ret = gpiod_set_debounce(line->desc, debounce_period_us);
0881     if (!ret) {
0882         WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
0883         return ret;
0884     }
0885     if (ret != -ENOTSUPP)
0886         return ret;
0887 
0888     if (debounce_period_us) {
0889         /* setup software debounce */
0890         level = gpiod_get_raw_value_cansleep(line->desc);
0891         if (level < 0)
0892             return level;
0893 
0894         if (!(IS_ENABLED(CONFIG_HTE) &&
0895               test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))) {
0896             irq = gpiod_to_irq(line->desc);
0897             if (irq < 0)
0898                 return -ENXIO;
0899 
0900             irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
0901             ret = request_irq(irq, debounce_irq_handler, irqflags,
0902                       line->req->label, line);
0903             if (ret)
0904                 return ret;
0905             line->irq = irq;
0906         } else {
0907             ret = hte_edge_setup(line, GPIO_V2_LINE_FLAG_EDGE_BOTH);
0908             if (ret)
0909                 return ret;
0910         }
0911 
0912         WRITE_ONCE(line->level, level);
0913         WRITE_ONCE(line->sw_debounced, 1);
0914     }
0915     return 0;
0916 }
0917 
0918 static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
0919                       unsigned int line_idx)
0920 {
0921     unsigned int i;
0922     u64 mask = BIT_ULL(line_idx);
0923 
0924     for (i = 0; i < lc->num_attrs; i++) {
0925         if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
0926             (lc->attrs[i].mask & mask))
0927             return true;
0928     }
0929     return false;
0930 }
0931 
0932 static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
0933                            unsigned int line_idx)
0934 {
0935     unsigned int i;
0936     u64 mask = BIT_ULL(line_idx);
0937 
0938     for (i = 0; i < lc->num_attrs; i++) {
0939         if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
0940             (lc->attrs[i].mask & mask))
0941             return lc->attrs[i].attr.debounce_period_us;
0942     }
0943     return 0;
0944 }
0945 
0946 static void edge_detector_stop(struct line *line)
0947 {
0948     if (line->irq) {
0949         free_irq(line->irq, line);
0950         line->irq = 0;
0951     }
0952 
0953 #ifdef CONFIG_HTE
0954     if (READ_ONCE(line->edflags) & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
0955         hte_ts_put(&line->hdesc);
0956 #endif
0957 
0958     cancel_delayed_work_sync(&line->work);
0959     WRITE_ONCE(line->sw_debounced, 0);
0960     WRITE_ONCE(line->edflags, 0);
0961     if (line->desc)
0962         WRITE_ONCE(line->desc->debounce_period_us, 0);
0963     /* do not change line->level - see comment in debounced_value() */
0964 }
0965 
0966 static int edge_detector_setup(struct line *line,
0967                    struct gpio_v2_line_config *lc,
0968                    unsigned int line_idx, u64 edflags)
0969 {
0970     u32 debounce_period_us;
0971     unsigned long irqflags = 0;
0972     u64 eflags;
0973     int irq, ret;
0974 
0975     eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
0976     if (eflags && !kfifo_initialized(&line->req->events)) {
0977         ret = kfifo_alloc(&line->req->events,
0978                   line->req->event_buffer_size, GFP_KERNEL);
0979         if (ret)
0980             return ret;
0981     }
0982     if (gpio_v2_line_config_debounced(lc, line_idx)) {
0983         debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
0984         ret = debounce_setup(line, debounce_period_us);
0985         if (ret)
0986             return ret;
0987         WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
0988     }
0989 
0990     /* detection disabled or sw debouncer will provide edge detection */
0991     if (!eflags || READ_ONCE(line->sw_debounced))
0992         return 0;
0993 
0994     if (IS_ENABLED(CONFIG_HTE) &&
0995         (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
0996         return hte_edge_setup(line, edflags);
0997 
0998     irq = gpiod_to_irq(line->desc);
0999     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 static const struct file_operations line_fileops = {
1501     .release = linereq_release,
1502     .read = linereq_read,
1503     .poll = linereq_poll,
1504     .owner = THIS_MODULE,
1505     .llseek = noop_llseek,
1506     .unlocked_ioctl = linereq_ioctl,
1507 #ifdef CONFIG_COMPAT
1508     .compat_ioctl = linereq_ioctl_compat,
1509 #endif
1510 };
1511 
1512 static int linereq_create(struct gpio_device *gdev, void __user *ip)
1513 {
1514     struct gpio_v2_line_request ulr;
1515     struct gpio_v2_line_config *lc;
1516     struct linereq *lr;
1517     struct file *file;
1518     u64 flags, edflags;
1519     unsigned int i;
1520     int fd, ret;
1521 
1522     if (copy_from_user(&ulr, ip, sizeof(ulr)))
1523         return -EFAULT;
1524 
1525     if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
1526         return -EINVAL;
1527 
1528     if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
1529         return -EINVAL;
1530 
1531     lc = &ulr.config;
1532     ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
1533     if (ret)
1534         return ret;
1535 
1536     lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
1537     if (!lr)
1538         return -ENOMEM;
1539 
1540     lr->gdev = gdev;
1541     get_device(&gdev->dev);
1542 
1543     for (i = 0; i < ulr.num_lines; i++) {
1544         lr->lines[i].req = lr;
1545         WRITE_ONCE(lr->lines[i].sw_debounced, 0);
1546         INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
1547     }
1548 
1549     if (ulr.consumer[0] != '\0') {
1550         /* label is only initialized if consumer is set */
1551         lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1552                      GFP_KERNEL);
1553         if (!lr->label) {
1554             ret = -ENOMEM;
1555             goto out_free_linereq;
1556         }
1557     }
1558 
1559     mutex_init(&lr->config_mutex);
1560     init_waitqueue_head(&lr->wait);
1561     lr->event_buffer_size = ulr.event_buffer_size;
1562     if (lr->event_buffer_size == 0)
1563         lr->event_buffer_size = ulr.num_lines * 16;
1564     else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
1565         lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
1566 
1567     atomic_set(&lr->seqno, 0);
1568     lr->num_lines = ulr.num_lines;
1569 
1570     /* Request each GPIO */
1571     for (i = 0; i < ulr.num_lines; i++) {
1572         u32 offset = ulr.offsets[i];
1573         struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
1574 
1575         if (IS_ERR(desc)) {
1576             ret = PTR_ERR(desc);
1577             goto out_free_linereq;
1578         }
1579 
1580         ret = gpiod_request_user(desc, lr->label);
1581         if (ret)
1582             goto out_free_linereq;
1583 
1584         lr->lines[i].desc = desc;
1585         flags = gpio_v2_line_config_flags(lc, i);
1586         gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
1587 
1588         ret = gpiod_set_transitory(desc, false);
1589         if (ret < 0)
1590             goto out_free_linereq;
1591 
1592         edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
1593         /*
1594          * Lines have to be requested explicitly for input
1595          * or output, else the line will be treated "as is".
1596          */
1597         if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
1598             int val = gpio_v2_line_config_output_value(lc, i);
1599 
1600             ret = gpiod_direction_output(desc, val);
1601             if (ret)
1602                 goto out_free_linereq;
1603         } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
1604             ret = gpiod_direction_input(desc);
1605             if (ret)
1606                 goto out_free_linereq;
1607 
1608             ret = edge_detector_setup(&lr->lines[i], lc, i,
1609                           edflags);
1610             if (ret)
1611                 goto out_free_linereq;
1612         }
1613 
1614         lr->lines[i].edflags = edflags;
1615 
1616         blocking_notifier_call_chain(&desc->gdev->notifier,
1617                          GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1618 
1619         dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
1620             offset);
1621     }
1622 
1623     fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
1624     if (fd < 0) {
1625         ret = fd;
1626         goto out_free_linereq;
1627     }
1628 
1629     file = anon_inode_getfile("gpio-line", &line_fileops, lr,
1630                   O_RDONLY | O_CLOEXEC);
1631     if (IS_ERR(file)) {
1632         ret = PTR_ERR(file);
1633         goto out_put_unused_fd;
1634     }
1635 
1636     ulr.fd = fd;
1637     if (copy_to_user(ip, &ulr, sizeof(ulr))) {
1638         /*
1639          * fput() will trigger the release() callback, so do not go onto
1640          * the regular error cleanup path here.
1641          */
1642         fput(file);
1643         put_unused_fd(fd);
1644         return -EFAULT;
1645     }
1646 
1647     fd_install(fd, file);
1648 
1649     dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
1650         lr->num_lines);
1651 
1652     return 0;
1653 
1654 out_put_unused_fd:
1655     put_unused_fd(fd);
1656 out_free_linereq:
1657     linereq_free(lr);
1658     return ret;
1659 }
1660 
1661 #ifdef CONFIG_GPIO_CDEV_V1
1662 
1663 /*
1664  * GPIO line event management
1665  */
1666 
1667 /**
1668  * struct lineevent_state - contains the state of a userspace event
1669  * @gdev: the GPIO device the event pertains to
1670  * @label: consumer label used to tag descriptors
1671  * @desc: the GPIO descriptor held by this event
1672  * @eflags: the event flags this line was requested with
1673  * @irq: the interrupt that trigger in response to events on this GPIO
1674  * @wait: wait queue that handles blocking reads of events
1675  * @events: KFIFO for the GPIO events
1676  * @timestamp: cache for the timestamp storing it between hardirq
1677  * and IRQ thread, used to bring the timestamp close to the actual
1678  * event
1679  */
1680 struct lineevent_state {
1681     struct gpio_device *gdev;
1682     const char *label;
1683     struct gpio_desc *desc;
1684     u32 eflags;
1685     int irq;
1686     wait_queue_head_t wait;
1687     DECLARE_KFIFO(events, struct gpioevent_data, 16);
1688     u64 timestamp;
1689 };
1690 
1691 #define GPIOEVENT_REQUEST_VALID_FLAGS \
1692     (GPIOEVENT_REQUEST_RISING_EDGE | \
1693     GPIOEVENT_REQUEST_FALLING_EDGE)
1694 
1695 static __poll_t lineevent_poll(struct file *file,
1696                    struct poll_table_struct *wait)
1697 {
1698     struct lineevent_state *le = file->private_data;
1699     __poll_t events = 0;
1700 
1701     poll_wait(file, &le->wait, wait);
1702 
1703     if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
1704         events = EPOLLIN | EPOLLRDNORM;
1705 
1706     return events;
1707 }
1708 
1709 struct compat_gpioeevent_data {
1710     compat_u64  timestamp;
1711     u32     id;
1712 };
1713 
1714 static ssize_t lineevent_read(struct file *file,
1715                   char __user *buf,
1716                   size_t count,
1717                   loff_t *f_ps)
1718 {
1719     struct lineevent_state *le = file->private_data;
1720     struct gpioevent_data ge;
1721     ssize_t bytes_read = 0;
1722     ssize_t ge_size;
1723     int ret;
1724 
1725     /*
1726      * When compatible system call is being used the struct gpioevent_data,
1727      * in case of at least ia32, has different size due to the alignment
1728      * differences. Because we have first member 64 bits followed by one of
1729      * 32 bits there is no gap between them. The only difference is the
1730      * padding at the end of the data structure. Hence, we calculate the
1731      * actual sizeof() and pass this as an argument to copy_to_user() to
1732      * drop unneeded bytes from the output.
1733      */
1734     if (compat_need_64bit_alignment_fixup())
1735         ge_size = sizeof(struct compat_gpioeevent_data);
1736     else
1737         ge_size = sizeof(struct gpioevent_data);
1738     if (count < ge_size)
1739         return -EINVAL;
1740 
1741     do {
1742         spin_lock(&le->wait.lock);
1743         if (kfifo_is_empty(&le->events)) {
1744             if (bytes_read) {
1745                 spin_unlock(&le->wait.lock);
1746                 return bytes_read;
1747             }
1748 
1749             if (file->f_flags & O_NONBLOCK) {
1750                 spin_unlock(&le->wait.lock);
1751                 return -EAGAIN;
1752             }
1753 
1754             ret = wait_event_interruptible_locked(le->wait,
1755                     !kfifo_is_empty(&le->events));
1756             if (ret) {
1757                 spin_unlock(&le->wait.lock);
1758                 return ret;
1759             }
1760         }
1761 
1762         ret = kfifo_out(&le->events, &ge, 1);
1763         spin_unlock(&le->wait.lock);
1764         if (ret != 1) {
1765             /*
1766              * This should never happen - we were holding the lock
1767              * from the moment we learned the fifo is no longer
1768              * empty until now.
1769              */
1770             ret = -EIO;
1771             break;
1772         }
1773 
1774         if (copy_to_user(buf + bytes_read, &ge, ge_size))
1775             return -EFAULT;
1776         bytes_read += ge_size;
1777     } while (count >= bytes_read + ge_size);
1778 
1779     return bytes_read;
1780 }
1781 
1782 static void lineevent_free(struct lineevent_state *le)
1783 {
1784     if (le->irq)
1785         free_irq(le->irq, le);
1786     if (le->desc)
1787         gpiod_free(le->desc);
1788     kfree(le->label);
1789     put_device(&le->gdev->dev);
1790     kfree(le);
1791 }
1792 
1793 static int lineevent_release(struct inode *inode, struct file *file)
1794 {
1795     lineevent_free(file->private_data);
1796     return 0;
1797 }
1798 
1799 static long lineevent_ioctl(struct file *file, unsigned int cmd,
1800                 unsigned long arg)
1801 {
1802     struct lineevent_state *le = file->private_data;
1803     void __user *ip = (void __user *)arg;
1804     struct gpiohandle_data ghd;
1805 
1806     /*
1807      * We can get the value for an event line but not set it,
1808      * because it is input by definition.
1809      */
1810     if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
1811         int val;
1812 
1813         memset(&ghd, 0, sizeof(ghd));
1814 
1815         val = gpiod_get_value_cansleep(le->desc);
1816         if (val < 0)
1817             return val;
1818         ghd.values[0] = val;
1819 
1820         if (copy_to_user(ip, &ghd, sizeof(ghd)))
1821             return -EFAULT;
1822 
1823         return 0;
1824     }
1825     return -EINVAL;
1826 }
1827 
1828 #ifdef CONFIG_COMPAT
1829 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
1830                    unsigned long arg)
1831 {
1832     return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1833 }
1834 #endif
1835 
1836 static const struct file_operations lineevent_fileops = {
1837     .release = lineevent_release,
1838     .read = lineevent_read,
1839     .poll = lineevent_poll,
1840     .owner = THIS_MODULE,
1841     .llseek = noop_llseek,
1842     .unlocked_ioctl = lineevent_ioctl,
1843 #ifdef CONFIG_COMPAT
1844     .compat_ioctl = lineevent_ioctl_compat,
1845 #endif
1846 };
1847 
1848 static irqreturn_t lineevent_irq_thread(int irq, void *p)
1849 {
1850     struct lineevent_state *le = p;
1851     struct gpioevent_data ge;
1852     int ret;
1853 
1854     /* Do not leak kernel stack to userspace */
1855     memset(&ge, 0, sizeof(ge));
1856 
1857     /*
1858      * We may be running from a nested threaded interrupt in which case
1859      * we didn't get the timestamp from lineevent_irq_handler().
1860      */
1861     if (!le->timestamp)
1862         ge.timestamp = ktime_get_ns();
1863     else
1864         ge.timestamp = le->timestamp;
1865 
1866     if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
1867         && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1868         int level = gpiod_get_value_cansleep(le->desc);
1869 
1870         if (level)
1871             /* Emit low-to-high event */
1872             ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1873         else
1874             /* Emit high-to-low event */
1875             ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1876     } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
1877         /* Emit low-to-high event */
1878         ge.id = GPIOEVENT_EVENT_RISING_EDGE;
1879     } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
1880         /* Emit high-to-low event */
1881         ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
1882     } else {
1883         return IRQ_NONE;
1884     }
1885 
1886     ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
1887                         1, &le->wait.lock);
1888     if (ret)
1889         wake_up_poll(&le->wait, EPOLLIN);
1890     else
1891         pr_debug_ratelimited("event FIFO is full - event dropped\n");
1892 
1893     return IRQ_HANDLED;
1894 }
1895 
1896 static irqreturn_t lineevent_irq_handler(int irq, void *p)
1897 {
1898     struct lineevent_state *le = p;
1899 
1900     /*
1901      * Just store the timestamp in hardirq context so we get it as
1902      * close in time as possible to the actual event.
1903      */
1904     le->timestamp = ktime_get_ns();
1905 
1906     return IRQ_WAKE_THREAD;
1907 }
1908 
1909 static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1910 {
1911     struct gpioevent_request eventreq;
1912     struct lineevent_state *le;
1913     struct gpio_desc *desc;
1914     struct file *file;
1915     u32 offset;
1916     u32 lflags;
1917     u32 eflags;
1918     int fd;
1919     int ret;
1920     int irq, irqflags = 0;
1921 
1922     if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1923         return -EFAULT;
1924 
1925     offset = eventreq.lineoffset;
1926     lflags = eventreq.handleflags;
1927     eflags = eventreq.eventflags;
1928 
1929     desc = gpiochip_get_desc(gdev->chip, offset);
1930     if (IS_ERR(desc))
1931         return PTR_ERR(desc);
1932 
1933     /* Return an error if a unknown flag is set */
1934     if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1935         (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1936         return -EINVAL;
1937 
1938     /* This is just wrong: we don't look for events on output lines */
1939     if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1940         (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1941         (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1942         return -EINVAL;
1943 
1944     /* Only one bias flag can be set. */
1945     if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1946          (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1947             GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1948         ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1949          (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1950         return -EINVAL;
1951 
1952     le = kzalloc(sizeof(*le), GFP_KERNEL);
1953     if (!le)
1954         return -ENOMEM;
1955     le->gdev = gdev;
1956     get_device(&gdev->dev);
1957 
1958     if (eventreq.consumer_label[0] != '\0') {
1959         /* label is only initialized if consumer_label is set */
1960         le->label = kstrndup(eventreq.consumer_label,
1961                      sizeof(eventreq.consumer_label) - 1,
1962                      GFP_KERNEL);
1963         if (!le->label) {
1964             ret = -ENOMEM;
1965             goto out_free_le;
1966         }
1967     }
1968 
1969     ret = gpiod_request_user(desc, le->label);
1970     if (ret)
1971         goto out_free_le;
1972     le->desc = desc;
1973     le->eflags = eflags;
1974 
1975     linehandle_flags_to_desc_flags(lflags, &desc->flags);
1976 
1977     ret = gpiod_direction_input(desc);
1978     if (ret)
1979         goto out_free_le;
1980 
1981     blocking_notifier_call_chain(&desc->gdev->notifier,
1982                      GPIO_V2_LINE_CHANGED_REQUESTED, desc);
1983 
1984     irq = gpiod_to_irq(desc);
1985     if (irq <= 0) {
1986         ret = -ENODEV;
1987         goto out_free_le;
1988     }
1989 
1990     if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
1991         irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1992             IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1993     if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
1994         irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1995             IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
1996     irqflags |= IRQF_ONESHOT;
1997 
1998     INIT_KFIFO(le->events);
1999     init_waitqueue_head(&le->wait);
2000 
2001     /* Request a thread to read the events */
2002     ret = request_threaded_irq(irq,
2003                    lineevent_irq_handler,
2004                    lineevent_irq_thread,
2005                    irqflags,
2006                    le->label,
2007                    le);
2008     if (ret)
2009         goto out_free_le;
2010 
2011     le->irq = irq;
2012 
2013     fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
2014     if (fd < 0) {
2015         ret = fd;
2016         goto out_free_le;
2017     }
2018 
2019     file = anon_inode_getfile("gpio-event",
2020                   &lineevent_fileops,
2021                   le,
2022                   O_RDONLY | O_CLOEXEC);
2023     if (IS_ERR(file)) {
2024         ret = PTR_ERR(file);
2025         goto out_put_unused_fd;
2026     }
2027 
2028     eventreq.fd = fd;
2029     if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
2030         /*
2031          * fput() will trigger the release() callback, so do not go onto
2032          * the regular error cleanup path here.
2033          */
2034         fput(file);
2035         put_unused_fd(fd);
2036         return -EFAULT;
2037     }
2038 
2039     fd_install(fd, file);
2040 
2041     return 0;
2042 
2043 out_put_unused_fd:
2044     put_unused_fd(fd);
2045 out_free_le:
2046     lineevent_free(le);
2047     return ret;
2048 }
2049 
2050 static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
2051                     struct gpioline_info *info_v1)
2052 {
2053     u64 flagsv2 = info_v2->flags;
2054 
2055     memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
2056     memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
2057     info_v1->line_offset = info_v2->offset;
2058     info_v1->flags = 0;
2059 
2060     if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
2061         info_v1->flags |= GPIOLINE_FLAG_KERNEL;
2062 
2063     if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
2064         info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
2065 
2066     if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
2067         info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
2068 
2069     if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
2070         info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
2071     if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
2072         info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
2073 
2074     if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
2075         info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
2076     if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
2077         info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
2078     if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
2079         info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
2080 }
2081 
2082 static void gpio_v2_line_info_changed_to_v1(
2083         struct gpio_v2_line_info_changed *lic_v2,
2084         struct gpioline_info_changed *lic_v1)
2085 {
2086     memset(lic_v1, 0, sizeof(*lic_v1));
2087     gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
2088     lic_v1->timestamp = lic_v2->timestamp_ns;
2089     lic_v1->event_type = lic_v2->event_type;
2090 }
2091 
2092 #endif /* CONFIG_GPIO_CDEV_V1 */
2093 
2094 static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
2095                   struct gpio_v2_line_info *info)
2096 {
2097     struct gpio_chip *gc = desc->gdev->chip;
2098     bool ok_for_pinctrl;
2099     unsigned long flags;
2100     u32 debounce_period_us;
2101     unsigned int num_attrs = 0;
2102 
2103     memset(info, 0, sizeof(*info));
2104     info->offset = gpio_chip_hwgpio(desc);
2105 
2106     /*
2107      * This function takes a mutex so we must check this before taking
2108      * the spinlock.
2109      *
2110      * FIXME: find a non-racy way to retrieve this information. Maybe a
2111      * lock common to both frameworks?
2112      */
2113     ok_for_pinctrl =
2114         pinctrl_gpio_can_use_line(gc->base + info->offset);
2115 
2116     spin_lock_irqsave(&gpio_lock, flags);
2117 
2118     if (desc->name)
2119         strscpy(info->name, desc->name, sizeof(info->name));
2120 
2121     if (desc->label)
2122         strscpy(info->consumer, desc->label, sizeof(info->consumer));
2123 
2124     /*
2125      * Userspace only need to know that the kernel is using this GPIO so
2126      * it can't use it.
2127      */
2128     info->flags = 0;
2129     if (test_bit(FLAG_REQUESTED, &desc->flags) ||
2130         test_bit(FLAG_IS_HOGGED, &desc->flags) ||
2131         test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
2132         test_bit(FLAG_EXPORT, &desc->flags) ||
2133         test_bit(FLAG_SYSFS, &desc->flags) ||
2134         !gpiochip_line_is_valid(gc, info->offset) ||
2135         !ok_for_pinctrl)
2136         info->flags |= GPIO_V2_LINE_FLAG_USED;
2137 
2138     if (test_bit(FLAG_IS_OUT, &desc->flags))
2139         info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
2140     else
2141         info->flags |= GPIO_V2_LINE_FLAG_INPUT;
2142 
2143     if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2144         info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
2145 
2146     if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2147         info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
2148     if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2149         info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
2150 
2151     if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2152         info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
2153     if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2154         info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
2155     if (test_bit(FLAG_PULL_UP, &desc->flags))
2156         info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
2157 
2158     if (test_bit(FLAG_EDGE_RISING, &desc->flags))
2159         info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
2160     if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
2161         info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
2162 
2163     if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
2164         info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
2165     else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags))
2166         info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
2167 
2168     debounce_period_us = READ_ONCE(desc->debounce_period_us);
2169     if (debounce_period_us) {
2170         info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
2171         info->attrs[num_attrs].debounce_period_us = debounce_period_us;
2172         num_attrs++;
2173     }
2174     info->num_attrs = num_attrs;
2175 
2176     spin_unlock_irqrestore(&gpio_lock, flags);
2177 }
2178 
2179 struct gpio_chardev_data {
2180     struct gpio_device *gdev;
2181     wait_queue_head_t wait;
2182     DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
2183     struct notifier_block lineinfo_changed_nb;
2184     unsigned long *watched_lines;
2185 #ifdef CONFIG_GPIO_CDEV_V1
2186     atomic_t watch_abi_version;
2187 #endif
2188 };
2189 
2190 static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
2191 {
2192     struct gpio_device *gdev = cdev->gdev;
2193     struct gpiochip_info chipinfo;
2194 
2195     memset(&chipinfo, 0, sizeof(chipinfo));
2196 
2197     strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
2198     strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
2199     chipinfo.lines = gdev->ngpio;
2200     if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
2201         return -EFAULT;
2202     return 0;
2203 }
2204 
2205 #ifdef CONFIG_GPIO_CDEV_V1
2206 /*
2207  * returns 0 if the versions match, else the previously selected ABI version
2208  */
2209 static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
2210                        unsigned int version)
2211 {
2212     int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
2213 
2214     if (abiv == version)
2215         return 0;
2216 
2217     return abiv;
2218 }
2219 
2220 static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
2221                bool watch)
2222 {
2223     struct gpio_desc *desc;
2224     struct gpioline_info lineinfo;
2225     struct gpio_v2_line_info lineinfo_v2;
2226 
2227     if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2228         return -EFAULT;
2229 
2230     /* this doubles as a range check on line_offset */
2231     desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset);
2232     if (IS_ERR(desc))
2233         return PTR_ERR(desc);
2234 
2235     if (watch) {
2236         if (lineinfo_ensure_abi_version(cdev, 1))
2237             return -EPERM;
2238 
2239         if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
2240             return -EBUSY;
2241     }
2242 
2243     gpio_desc_to_lineinfo(desc, &lineinfo_v2);
2244     gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
2245 
2246     if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2247         if (watch)
2248             clear_bit(lineinfo.line_offset, cdev->watched_lines);
2249         return -EFAULT;
2250     }
2251 
2252     return 0;
2253 }
2254 #endif
2255 
2256 static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
2257             bool watch)
2258 {
2259     struct gpio_desc *desc;
2260     struct gpio_v2_line_info lineinfo;
2261 
2262     if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
2263         return -EFAULT;
2264 
2265     if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
2266         return -EINVAL;
2267 
2268     desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
2269     if (IS_ERR(desc))
2270         return PTR_ERR(desc);
2271 
2272     if (watch) {
2273 #ifdef CONFIG_GPIO_CDEV_V1
2274         if (lineinfo_ensure_abi_version(cdev, 2))
2275             return -EPERM;
2276 #endif
2277         if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
2278             return -EBUSY;
2279     }
2280     gpio_desc_to_lineinfo(desc, &lineinfo);
2281 
2282     if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
2283         if (watch)
2284             clear_bit(lineinfo.offset, cdev->watched_lines);
2285         return -EFAULT;
2286     }
2287 
2288     return 0;
2289 }
2290 
2291 static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
2292 {
2293     __u32 offset;
2294 
2295     if (copy_from_user(&offset, ip, sizeof(offset)))
2296         return -EFAULT;
2297 
2298     if (offset >= cdev->gdev->ngpio)
2299         return -EINVAL;
2300 
2301     if (!test_and_clear_bit(offset, cdev->watched_lines))
2302         return -EBUSY;
2303 
2304     return 0;
2305 }
2306 
2307 /*
2308  * gpio_ioctl() - ioctl handler for the GPIO chardev
2309  */
2310 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2311 {
2312     struct gpio_chardev_data *cdev = file->private_data;
2313     struct gpio_device *gdev = cdev->gdev;
2314     void __user *ip = (void __user *)arg;
2315 
2316     /* We fail any subsequent ioctl():s when the chip is gone */
2317     if (!gdev->chip)
2318         return -ENODEV;
2319 
2320     /* Fill in the struct and pass to userspace */
2321     switch (cmd) {
2322     case GPIO_GET_CHIPINFO_IOCTL:
2323         return chipinfo_get(cdev, ip);
2324 #ifdef CONFIG_GPIO_CDEV_V1
2325     case GPIO_GET_LINEHANDLE_IOCTL:
2326         return linehandle_create(gdev, ip);
2327     case GPIO_GET_LINEEVENT_IOCTL:
2328         return lineevent_create(gdev, ip);
2329     case GPIO_GET_LINEINFO_IOCTL:
2330         return lineinfo_get_v1(cdev, ip, false);
2331     case GPIO_GET_LINEINFO_WATCH_IOCTL:
2332         return lineinfo_get_v1(cdev, ip, true);
2333 #endif /* CONFIG_GPIO_CDEV_V1 */
2334     case GPIO_V2_GET_LINEINFO_IOCTL:
2335         return lineinfo_get(cdev, ip, false);
2336     case GPIO_V2_GET_LINEINFO_WATCH_IOCTL:
2337         return lineinfo_get(cdev, ip, true);
2338     case GPIO_V2_GET_LINE_IOCTL:
2339         return linereq_create(gdev, ip);
2340     case GPIO_GET_LINEINFO_UNWATCH_IOCTL:
2341         return lineinfo_unwatch(cdev, ip);
2342     default:
2343         return -EINVAL;
2344     }
2345 }
2346 
2347 #ifdef CONFIG_COMPAT
2348 static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
2349                   unsigned long arg)
2350 {
2351     return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2352 }
2353 #endif
2354 
2355 static struct gpio_chardev_data *
2356 to_gpio_chardev_data(struct notifier_block *nb)
2357 {
2358     return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
2359 }
2360 
2361 static int lineinfo_changed_notify(struct notifier_block *nb,
2362                    unsigned long action, void *data)
2363 {
2364     struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
2365     struct gpio_v2_line_info_changed chg;
2366     struct gpio_desc *desc = data;
2367     int ret;
2368 
2369     if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
2370         return NOTIFY_DONE;
2371 
2372     memset(&chg, 0, sizeof(chg));
2373     chg.event_type = action;
2374     chg.timestamp_ns = ktime_get_ns();
2375     gpio_desc_to_lineinfo(desc, &chg.info);
2376 
2377     ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
2378     if (ret)
2379         wake_up_poll(&cdev->wait, EPOLLIN);
2380     else
2381         pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
2382 
2383     return NOTIFY_OK;
2384 }
2385 
2386 static __poll_t lineinfo_watch_poll(struct file *file,
2387                     struct poll_table_struct *pollt)
2388 {
2389     struct gpio_chardev_data *cdev = file->private_data;
2390     __poll_t events = 0;
2391 
2392     poll_wait(file, &cdev->wait, pollt);
2393 
2394     if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
2395                          &cdev->wait.lock))
2396         events = EPOLLIN | EPOLLRDNORM;
2397 
2398     return events;
2399 }
2400 
2401 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
2402                    size_t count, loff_t *off)
2403 {
2404     struct gpio_chardev_data *cdev = file->private_data;
2405     struct gpio_v2_line_info_changed event;
2406     ssize_t bytes_read = 0;
2407     int ret;
2408     size_t event_size;
2409 
2410 #ifndef CONFIG_GPIO_CDEV_V1
2411     event_size = sizeof(struct gpio_v2_line_info_changed);
2412     if (count < event_size)
2413         return -EINVAL;
2414 #endif
2415 
2416     do {
2417         spin_lock(&cdev->wait.lock);
2418         if (kfifo_is_empty(&cdev->events)) {
2419             if (bytes_read) {
2420                 spin_unlock(&cdev->wait.lock);
2421                 return bytes_read;
2422             }
2423 
2424             if (file->f_flags & O_NONBLOCK) {
2425                 spin_unlock(&cdev->wait.lock);
2426                 return -EAGAIN;
2427             }
2428 
2429             ret = wait_event_interruptible_locked(cdev->wait,
2430                     !kfifo_is_empty(&cdev->events));
2431             if (ret) {
2432                 spin_unlock(&cdev->wait.lock);
2433                 return ret;
2434             }
2435         }
2436 #ifdef CONFIG_GPIO_CDEV_V1
2437         /* must be after kfifo check so watch_abi_version is set */
2438         if (atomic_read(&cdev->watch_abi_version) == 2)
2439             event_size = sizeof(struct gpio_v2_line_info_changed);
2440         else
2441             event_size = sizeof(struct gpioline_info_changed);
2442         if (count < event_size) {
2443             spin_unlock(&cdev->wait.lock);
2444             return -EINVAL;
2445         }
2446 #endif
2447         ret = kfifo_out(&cdev->events, &event, 1);
2448         spin_unlock(&cdev->wait.lock);
2449         if (ret != 1) {
2450             ret = -EIO;
2451             break;
2452             /* We should never get here. See lineevent_read(). */
2453         }
2454 
2455 #ifdef CONFIG_GPIO_CDEV_V1
2456         if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
2457             if (copy_to_user(buf + bytes_read, &event, event_size))
2458                 return -EFAULT;
2459         } else {
2460             struct gpioline_info_changed event_v1;
2461 
2462             gpio_v2_line_info_changed_to_v1(&event, &event_v1);
2463             if (copy_to_user(buf + bytes_read, &event_v1,
2464                      event_size))
2465                 return -EFAULT;
2466         }
2467 #else
2468         if (copy_to_user(buf + bytes_read, &event, event_size))
2469             return -EFAULT;
2470 #endif
2471         bytes_read += event_size;
2472     } while (count >= bytes_read + sizeof(event));
2473 
2474     return bytes_read;
2475 }
2476 
2477 /**
2478  * gpio_chrdev_open() - open the chardev for ioctl operations
2479  * @inode: inode for this chardev
2480  * @file: file struct for storing private data
2481  * Returns 0 on success
2482  */
2483 static int gpio_chrdev_open(struct inode *inode, struct file *file)
2484 {
2485     struct gpio_device *gdev = container_of(inode->i_cdev,
2486                         struct gpio_device, chrdev);
2487     struct gpio_chardev_data *cdev;
2488     int ret = -ENOMEM;
2489 
2490     /* Fail on open if the backing gpiochip is gone */
2491     if (!gdev->chip)
2492         return -ENODEV;
2493 
2494     cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
2495     if (!cdev)
2496         return -ENOMEM;
2497 
2498     cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
2499     if (!cdev->watched_lines)
2500         goto out_free_cdev;
2501 
2502     init_waitqueue_head(&cdev->wait);
2503     INIT_KFIFO(cdev->events);
2504     cdev->gdev = gdev;
2505 
2506     cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
2507     ret = blocking_notifier_chain_register(&gdev->notifier,
2508                            &cdev->lineinfo_changed_nb);
2509     if (ret)
2510         goto out_free_bitmap;
2511 
2512     get_device(&gdev->dev);
2513     file->private_data = cdev;
2514 
2515     ret = nonseekable_open(inode, file);
2516     if (ret)
2517         goto out_unregister_notifier;
2518 
2519     return ret;
2520 
2521 out_unregister_notifier:
2522     blocking_notifier_chain_unregister(&gdev->notifier,
2523                        &cdev->lineinfo_changed_nb);
2524 out_free_bitmap:
2525     bitmap_free(cdev->watched_lines);
2526 out_free_cdev:
2527     kfree(cdev);
2528     return ret;
2529 }
2530 
2531 /**
2532  * gpio_chrdev_release() - close chardev after ioctl operations
2533  * @inode: inode for this chardev
2534  * @file: file struct for storing private data
2535  * Returns 0 on success
2536  */
2537 static int gpio_chrdev_release(struct inode *inode, struct file *file)
2538 {
2539     struct gpio_chardev_data *cdev = file->private_data;
2540     struct gpio_device *gdev = cdev->gdev;
2541 
2542     bitmap_free(cdev->watched_lines);
2543     blocking_notifier_chain_unregister(&gdev->notifier,
2544                        &cdev->lineinfo_changed_nb);
2545     put_device(&gdev->dev);
2546     kfree(cdev);
2547 
2548     return 0;
2549 }
2550 
2551 static const struct file_operations gpio_fileops = {
2552     .release = gpio_chrdev_release,
2553     .open = gpio_chrdev_open,
2554     .poll = lineinfo_watch_poll,
2555     .read = lineinfo_watch_read,
2556     .owner = THIS_MODULE,
2557     .llseek = no_llseek,
2558     .unlocked_ioctl = gpio_ioctl,
2559 #ifdef CONFIG_COMPAT
2560     .compat_ioctl = gpio_ioctl_compat,
2561 #endif
2562 };
2563 
2564 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
2565 {
2566     int ret;
2567 
2568     cdev_init(&gdev->chrdev, &gpio_fileops);
2569     gdev->chrdev.owner = THIS_MODULE;
2570     gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
2571 
2572     ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
2573     if (ret)
2574         return ret;
2575 
2576     chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
2577          MAJOR(devt), gdev->id);
2578 
2579     return 0;
2580 }
2581 
2582 void gpiolib_cdev_unregister(struct gpio_device *gdev)
2583 {
2584     cdev_device_del(&gdev->chrdev, &gdev->dev);
2585 }