1925ca369SKent Gibson // SPDX-License-Identifier: GPL-2.0 2925ca369SKent Gibson 3d189f627SKent Gibson #include <linux/anon_inodes.h> 43c0d9c63SKent Gibson #include <linux/atomic.h> 5925ca369SKent Gibson #include <linux/bitmap.h> 63c0d9c63SKent Gibson #include <linux/build_bug.h> 7d189f627SKent Gibson #include <linux/cdev.h> 8d189f627SKent Gibson #include <linux/compat.h> 965cff704SKent Gibson #include <linux/compiler.h> 10925ca369SKent Gibson #include <linux/device.h> 11925ca369SKent Gibson #include <linux/err.h> 12d189f627SKent Gibson #include <linux/file.h> 13925ca369SKent Gibson #include <linux/gpio.h> 14925ca369SKent Gibson #include <linux/gpio/driver.h> 15d189f627SKent Gibson #include <linux/interrupt.h> 16d189f627SKent Gibson #include <linux/irqreturn.h> 17d189f627SKent Gibson #include <linux/kernel.h> 18925ca369SKent Gibson #include <linux/kfifo.h> 19d189f627SKent Gibson #include <linux/module.h> 20a54756cbSKent Gibson #include <linux/mutex.h> 21d189f627SKent Gibson #include <linux/pinctrl/consumer.h> 22925ca369SKent Gibson #include <linux/poll.h> 23d189f627SKent Gibson #include <linux/spinlock.h> 24925ca369SKent Gibson #include <linux/timekeeping.h> 25d189f627SKent Gibson #include <linux/uaccess.h> 2665cff704SKent Gibson #include <linux/workqueue.h> 272068339aSDipen Patel #include <linux/hte.h> 28925ca369SKent Gibson #include <uapi/linux/gpio.h> 29925ca369SKent Gibson 30925ca369SKent Gibson #include "gpiolib.h" 31925ca369SKent Gibson #include "gpiolib-cdev.h" 32925ca369SKent Gibson 333c0d9c63SKent Gibson /* 343c0d9c63SKent Gibson * Array sizes must ensure 64-bit alignment and not create holes in the 353c0d9c63SKent Gibson * struct packing. 363c0d9c63SKent Gibson */ 373c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2)); 383c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8)); 393c0d9c63SKent Gibson 403c0d9c63SKent Gibson /* 413c0d9c63SKent Gibson * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility 423c0d9c63SKent Gibson */ 433c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8)); 443c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8)); 453c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8)); 463c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8)); 473c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8)); 483c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8)); 493c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8)); 503c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8)); 513c0d9c63SKent Gibson 52925ca369SKent Gibson /* Character device interface to GPIO. 53925ca369SKent Gibson * 54925ca369SKent Gibson * The GPIO character device, /dev/gpiochipN, provides userspace an 55925ca369SKent Gibson * interface to gpiolib GPIOs via ioctl()s. 56925ca369SKent Gibson */ 57925ca369SKent Gibson 58925ca369SKent Gibson /* 59925ca369SKent Gibson * GPIO line handle management 60925ca369SKent Gibson */ 61925ca369SKent Gibson 623c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 63925ca369SKent Gibson /** 64925ca369SKent Gibson * struct linehandle_state - contains the state of a userspace handle 65925ca369SKent Gibson * @gdev: the GPIO device the handle pertains to 66925ca369SKent Gibson * @label: consumer label used to tag descriptors 67925ca369SKent Gibson * @descs: the GPIO descriptors held by this handle 6852b7b596SKent Gibson * @num_descs: the number of descriptors held in the descs array 69925ca369SKent Gibson */ 70925ca369SKent Gibson struct linehandle_state { 71925ca369SKent Gibson struct gpio_device *gdev; 72925ca369SKent Gibson const char *label; 73925ca369SKent Gibson struct gpio_desc *descs[GPIOHANDLES_MAX]; 7452b7b596SKent Gibson u32 num_descs; 75925ca369SKent Gibson }; 76925ca369SKent Gibson 77925ca369SKent Gibson #define GPIOHANDLE_REQUEST_VALID_FLAGS \ 78925ca369SKent Gibson (GPIOHANDLE_REQUEST_INPUT | \ 79925ca369SKent Gibson GPIOHANDLE_REQUEST_OUTPUT | \ 80925ca369SKent Gibson GPIOHANDLE_REQUEST_ACTIVE_LOW | \ 81925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_UP | \ 82925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \ 83925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_DISABLE | \ 84925ca369SKent Gibson GPIOHANDLE_REQUEST_OPEN_DRAIN | \ 85925ca369SKent Gibson GPIOHANDLE_REQUEST_OPEN_SOURCE) 86925ca369SKent Gibson 87925ca369SKent Gibson static int linehandle_validate_flags(u32 flags) 88925ca369SKent Gibson { 89925ca369SKent Gibson /* Return an error if an unknown flag is set */ 90925ca369SKent Gibson if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) 91925ca369SKent Gibson return -EINVAL; 92925ca369SKent Gibson 93925ca369SKent Gibson /* 94925ca369SKent Gibson * Do not allow both INPUT & OUTPUT flags to be set as they are 95925ca369SKent Gibson * contradictory. 96925ca369SKent Gibson */ 97925ca369SKent Gibson if ((flags & GPIOHANDLE_REQUEST_INPUT) && 98925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OUTPUT)) 99925ca369SKent Gibson return -EINVAL; 100925ca369SKent Gibson 101925ca369SKent Gibson /* 102925ca369SKent Gibson * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If 103925ca369SKent Gibson * the hardware actually supports enabling both at the same time the 104925ca369SKent Gibson * electrical result would be disastrous. 105925ca369SKent Gibson */ 106925ca369SKent Gibson if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) && 107925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) 108925ca369SKent Gibson return -EINVAL; 109925ca369SKent Gibson 110925ca369SKent Gibson /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */ 111925ca369SKent Gibson if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) && 112925ca369SKent Gibson ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || 113925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))) 114925ca369SKent Gibson return -EINVAL; 115925ca369SKent Gibson 116925ca369SKent Gibson /* Bias flags only allowed for input or output mode. */ 117925ca369SKent Gibson if (!((flags & GPIOHANDLE_REQUEST_INPUT) || 118925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OUTPUT)) && 119925ca369SKent Gibson ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) || 120925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) || 121925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN))) 122925ca369SKent Gibson return -EINVAL; 123925ca369SKent Gibson 124925ca369SKent Gibson /* Only one bias flag can be set. */ 125925ca369SKent Gibson if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && 126925ca369SKent Gibson (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | 127925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || 128925ca369SKent Gibson ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && 129925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) 130925ca369SKent Gibson return -EINVAL; 131925ca369SKent Gibson 132925ca369SKent Gibson return 0; 133925ca369SKent Gibson } 134925ca369SKent Gibson 135c274b58aSKent Gibson static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp) 136c274b58aSKent Gibson { 137c274b58aSKent Gibson assign_bit(FLAG_ACTIVE_LOW, flagsp, 138c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW); 139c274b58aSKent Gibson assign_bit(FLAG_OPEN_DRAIN, flagsp, 140c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN); 141c274b58aSKent Gibson assign_bit(FLAG_OPEN_SOURCE, flagsp, 142c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE); 143c274b58aSKent Gibson assign_bit(FLAG_PULL_UP, flagsp, 144c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP); 145c274b58aSKent Gibson assign_bit(FLAG_PULL_DOWN, flagsp, 146c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN); 147c274b58aSKent Gibson assign_bit(FLAG_BIAS_DISABLE, flagsp, 148c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE); 149c274b58aSKent Gibson } 150c274b58aSKent Gibson 151925ca369SKent Gibson static long linehandle_set_config(struct linehandle_state *lh, 152925ca369SKent Gibson void __user *ip) 153925ca369SKent Gibson { 154925ca369SKent Gibson struct gpiohandle_config gcnf; 155925ca369SKent Gibson struct gpio_desc *desc; 156925ca369SKent Gibson int i, ret; 157925ca369SKent Gibson u32 lflags; 158925ca369SKent Gibson 159925ca369SKent Gibson if (copy_from_user(&gcnf, ip, sizeof(gcnf))) 160925ca369SKent Gibson return -EFAULT; 161925ca369SKent Gibson 162925ca369SKent Gibson lflags = gcnf.flags; 163925ca369SKent Gibson ret = linehandle_validate_flags(lflags); 164925ca369SKent Gibson if (ret) 165925ca369SKent Gibson return ret; 166925ca369SKent Gibson 16752b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) { 168925ca369SKent Gibson desc = lh->descs[i]; 169c274b58aSKent Gibson linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags); 170925ca369SKent Gibson 171925ca369SKent Gibson /* 172925ca369SKent Gibson * Lines have to be requested explicitly for input 173925ca369SKent Gibson * or output, else the line will be treated "as is". 174925ca369SKent Gibson */ 175925ca369SKent Gibson if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { 176925ca369SKent Gibson int val = !!gcnf.default_values[i]; 177925ca369SKent Gibson 178925ca369SKent Gibson ret = gpiod_direction_output(desc, val); 179925ca369SKent Gibson if (ret) 180925ca369SKent Gibson return ret; 181925ca369SKent Gibson } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { 182925ca369SKent Gibson ret = gpiod_direction_input(desc); 183925ca369SKent Gibson if (ret) 184925ca369SKent Gibson return ret; 185925ca369SKent Gibson } 186925ca369SKent Gibson 1876accc376SKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 188aad95584SKent Gibson GPIO_V2_LINE_CHANGED_CONFIG, 189aad95584SKent Gibson desc); 190925ca369SKent Gibson } 191925ca369SKent Gibson return 0; 192925ca369SKent Gibson } 193925ca369SKent Gibson 19449bc5279SKent Gibson static long linehandle_ioctl(struct file *file, unsigned int cmd, 195925ca369SKent Gibson unsigned long arg) 196925ca369SKent Gibson { 19749bc5279SKent Gibson struct linehandle_state *lh = file->private_data; 198925ca369SKent Gibson void __user *ip = (void __user *)arg; 199925ca369SKent Gibson struct gpiohandle_data ghd; 200925ca369SKent Gibson DECLARE_BITMAP(vals, GPIOHANDLES_MAX); 2011cef8b50SAndy Shevchenko unsigned int i; 2021cef8b50SAndy Shevchenko int ret; 203925ca369SKent Gibson 204*533aae7cSBartosz Golaszewski if (!lh->gdev->chip) 205*533aae7cSBartosz Golaszewski return -ENODEV; 206*533aae7cSBartosz Golaszewski 2071cef8b50SAndy Shevchenko switch (cmd) { 2081cef8b50SAndy Shevchenko case GPIOHANDLE_GET_LINE_VALUES_IOCTL: 2091cef8b50SAndy Shevchenko /* NOTE: It's okay to read values of output lines */ 2101cef8b50SAndy Shevchenko ret = gpiod_get_array_value_complex(false, true, 2111cef8b50SAndy Shevchenko lh->num_descs, lh->descs, 2121cef8b50SAndy Shevchenko NULL, vals); 213925ca369SKent Gibson if (ret) 214925ca369SKent Gibson return ret; 215925ca369SKent Gibson 216925ca369SKent Gibson memset(&ghd, 0, sizeof(ghd)); 21752b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) 218925ca369SKent Gibson ghd.values[i] = test_bit(i, vals); 219925ca369SKent Gibson 220925ca369SKent Gibson if (copy_to_user(ip, &ghd, sizeof(ghd))) 221925ca369SKent Gibson return -EFAULT; 222925ca369SKent Gibson 223925ca369SKent Gibson return 0; 2241cef8b50SAndy Shevchenko case GPIOHANDLE_SET_LINE_VALUES_IOCTL: 225925ca369SKent Gibson /* 226925ca369SKent Gibson * All line descriptors were created at once with the same 227925ca369SKent Gibson * flags so just check if the first one is really output. 228925ca369SKent Gibson */ 229925ca369SKent Gibson if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags)) 230925ca369SKent Gibson return -EPERM; 231925ca369SKent Gibson 232925ca369SKent Gibson if (copy_from_user(&ghd, ip, sizeof(ghd))) 233925ca369SKent Gibson return -EFAULT; 234925ca369SKent Gibson 235925ca369SKent Gibson /* Clamp all values to [0,1] */ 23652b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) 237925ca369SKent Gibson __assign_bit(i, vals, ghd.values[i]); 238925ca369SKent Gibson 239925ca369SKent Gibson /* Reuse the array setting function */ 240925ca369SKent Gibson return gpiod_set_array_value_complex(false, 241925ca369SKent Gibson true, 24252b7b596SKent Gibson lh->num_descs, 243925ca369SKent Gibson lh->descs, 244925ca369SKent Gibson NULL, 245925ca369SKent Gibson vals); 2461cef8b50SAndy Shevchenko case GPIOHANDLE_SET_CONFIG_IOCTL: 247925ca369SKent Gibson return linehandle_set_config(lh, ip); 2481cef8b50SAndy Shevchenko default: 249925ca369SKent Gibson return -EINVAL; 250925ca369SKent Gibson } 2511cef8b50SAndy Shevchenko } 252925ca369SKent Gibson 253925ca369SKent Gibson #ifdef CONFIG_COMPAT 25449bc5279SKent Gibson static long linehandle_ioctl_compat(struct file *file, unsigned int cmd, 255925ca369SKent Gibson unsigned long arg) 256925ca369SKent Gibson { 25749bc5279SKent Gibson return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 258925ca369SKent Gibson } 259925ca369SKent Gibson #endif 260925ca369SKent Gibson 261883f9198SKent Gibson static void linehandle_free(struct linehandle_state *lh) 262925ca369SKent Gibson { 263925ca369SKent Gibson int i; 264925ca369SKent Gibson 26552b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) 266883f9198SKent Gibson if (lh->descs[i]) 267925ca369SKent Gibson gpiod_free(lh->descs[i]); 268925ca369SKent Gibson kfree(lh->label); 269883f9198SKent Gibson put_device(&lh->gdev->dev); 270925ca369SKent Gibson kfree(lh); 271883f9198SKent Gibson } 272883f9198SKent Gibson 273883f9198SKent Gibson static int linehandle_release(struct inode *inode, struct file *file) 274883f9198SKent Gibson { 275883f9198SKent Gibson linehandle_free(file->private_data); 276925ca369SKent Gibson return 0; 277925ca369SKent Gibson } 278925ca369SKent Gibson 279925ca369SKent Gibson static const struct file_operations linehandle_fileops = { 280925ca369SKent Gibson .release = linehandle_release, 281925ca369SKent Gibson .owner = THIS_MODULE, 282925ca369SKent Gibson .llseek = noop_llseek, 283925ca369SKent Gibson .unlocked_ioctl = linehandle_ioctl, 284925ca369SKent Gibson #ifdef CONFIG_COMPAT 285925ca369SKent Gibson .compat_ioctl = linehandle_ioctl_compat, 286925ca369SKent Gibson #endif 287925ca369SKent Gibson }; 288925ca369SKent Gibson 289925ca369SKent Gibson static int linehandle_create(struct gpio_device *gdev, void __user *ip) 290925ca369SKent Gibson { 291925ca369SKent Gibson struct gpiohandle_request handlereq; 292925ca369SKent Gibson struct linehandle_state *lh; 293925ca369SKent Gibson struct file *file; 294883f9198SKent Gibson int fd, i, ret; 295925ca369SKent Gibson u32 lflags; 296925ca369SKent Gibson 297925ca369SKent Gibson if (copy_from_user(&handlereq, ip, sizeof(handlereq))) 298925ca369SKent Gibson return -EFAULT; 299925ca369SKent Gibson if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) 300925ca369SKent Gibson return -EINVAL; 301925ca369SKent Gibson 302925ca369SKent Gibson lflags = handlereq.flags; 303925ca369SKent Gibson 304925ca369SKent Gibson ret = linehandle_validate_flags(lflags); 305925ca369SKent Gibson if (ret) 306925ca369SKent Gibson return ret; 307925ca369SKent Gibson 308925ca369SKent Gibson lh = kzalloc(sizeof(*lh), GFP_KERNEL); 309925ca369SKent Gibson if (!lh) 310925ca369SKent Gibson return -ENOMEM; 311925ca369SKent Gibson lh->gdev = gdev; 312925ca369SKent Gibson get_device(&gdev->dev); 313925ca369SKent Gibson 314f188ac12SKent Gibson if (handlereq.consumer_label[0] != '\0') { 315f188ac12SKent Gibson /* label is only initialized if consumer_label is set */ 316f188ac12SKent Gibson lh->label = kstrndup(handlereq.consumer_label, 317f188ac12SKent Gibson sizeof(handlereq.consumer_label) - 1, 318925ca369SKent Gibson GFP_KERNEL); 319925ca369SKent Gibson if (!lh->label) { 320925ca369SKent Gibson ret = -ENOMEM; 321925ca369SKent Gibson goto out_free_lh; 322925ca369SKent Gibson } 323925ca369SKent Gibson } 324925ca369SKent Gibson 325883f9198SKent Gibson lh->num_descs = handlereq.lines; 326883f9198SKent Gibson 327925ca369SKent Gibson /* Request each GPIO */ 328925ca369SKent Gibson for (i = 0; i < handlereq.lines; i++) { 329925ca369SKent Gibson u32 offset = handlereq.lineoffsets[i]; 330925ca369SKent Gibson struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); 331925ca369SKent Gibson 332925ca369SKent Gibson if (IS_ERR(desc)) { 333925ca369SKent Gibson ret = PTR_ERR(desc); 334883f9198SKent Gibson goto out_free_lh; 335925ca369SKent Gibson } 336925ca369SKent Gibson 33795a4eed7SAndy Shevchenko ret = gpiod_request_user(desc, lh->label); 338925ca369SKent Gibson if (ret) 339883f9198SKent Gibson goto out_free_lh; 340925ca369SKent Gibson lh->descs[i] = desc; 341c274b58aSKent Gibson linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags); 342925ca369SKent Gibson 343925ca369SKent Gibson ret = gpiod_set_transitory(desc, false); 344925ca369SKent Gibson if (ret < 0) 345883f9198SKent Gibson goto out_free_lh; 346925ca369SKent Gibson 347925ca369SKent Gibson /* 348925ca369SKent Gibson * Lines have to be requested explicitly for input 349925ca369SKent Gibson * or output, else the line will be treated "as is". 350925ca369SKent Gibson */ 351925ca369SKent Gibson if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { 352925ca369SKent Gibson int val = !!handlereq.default_values[i]; 353925ca369SKent Gibson 354925ca369SKent Gibson ret = gpiod_direction_output(desc, val); 355925ca369SKent Gibson if (ret) 356883f9198SKent Gibson goto out_free_lh; 357925ca369SKent Gibson } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { 358925ca369SKent Gibson ret = gpiod_direction_input(desc); 359925ca369SKent Gibson if (ret) 360883f9198SKent Gibson goto out_free_lh; 361925ca369SKent Gibson } 362925ca369SKent Gibson 3636accc376SKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 364aad95584SKent Gibson GPIO_V2_LINE_CHANGED_REQUESTED, desc); 365925ca369SKent Gibson 366925ca369SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", 367925ca369SKent Gibson offset); 368925ca369SKent Gibson } 369925ca369SKent Gibson 370925ca369SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 371925ca369SKent Gibson if (fd < 0) { 372925ca369SKent Gibson ret = fd; 373883f9198SKent Gibson goto out_free_lh; 374925ca369SKent Gibson } 375925ca369SKent Gibson 376925ca369SKent Gibson file = anon_inode_getfile("gpio-linehandle", 377925ca369SKent Gibson &linehandle_fileops, 378925ca369SKent Gibson lh, 379925ca369SKent Gibson O_RDONLY | O_CLOEXEC); 380925ca369SKent Gibson if (IS_ERR(file)) { 381925ca369SKent Gibson ret = PTR_ERR(file); 382925ca369SKent Gibson goto out_put_unused_fd; 383925ca369SKent Gibson } 384925ca369SKent Gibson 385925ca369SKent Gibson handlereq.fd = fd; 386925ca369SKent Gibson if (copy_to_user(ip, &handlereq, sizeof(handlereq))) { 387925ca369SKent Gibson /* 388925ca369SKent Gibson * fput() will trigger the release() callback, so do not go onto 389925ca369SKent Gibson * the regular error cleanup path here. 390925ca369SKent Gibson */ 391925ca369SKent Gibson fput(file); 392925ca369SKent Gibson put_unused_fd(fd); 393925ca369SKent Gibson return -EFAULT; 394925ca369SKent Gibson } 395925ca369SKent Gibson 396925ca369SKent Gibson fd_install(fd, file); 397925ca369SKent Gibson 398925ca369SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", 39952b7b596SKent Gibson lh->num_descs); 400925ca369SKent Gibson 401925ca369SKent Gibson return 0; 402925ca369SKent Gibson 403925ca369SKent Gibson out_put_unused_fd: 404925ca369SKent Gibson put_unused_fd(fd); 405925ca369SKent Gibson out_free_lh: 406883f9198SKent Gibson linehandle_free(lh); 407925ca369SKent Gibson return ret; 408925ca369SKent Gibson } 4093c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 4103c0d9c63SKent Gibson 4113c0d9c63SKent Gibson /** 4123c0d9c63SKent Gibson * struct line - contains the state of a requested line 4133c0d9c63SKent Gibson * @desc: the GPIO descriptor for this line. 41473e03419SKent Gibson * @req: the corresponding line request 41573e03419SKent Gibson * @irq: the interrupt triggered in response to events on this GPIO 4168d259847SAndy Shevchenko * @edflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or 41773e03419SKent Gibson * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied 41873e03419SKent Gibson * @timestamp_ns: cache for the timestamp storing it between hardirq and 41973e03419SKent Gibson * IRQ thread, used to bring the timestamp close to the actual event 42073e03419SKent Gibson * @req_seqno: the seqno for the current edge event in the sequence of 42173e03419SKent Gibson * events for the corresponding line request. This is drawn from the @req. 42273e03419SKent Gibson * @line_seqno: the seqno for the current edge event in the sequence of 42373e03419SKent Gibson * events for this line. 42465cff704SKent Gibson * @work: the worker that implements software debouncing 42565cff704SKent Gibson * @sw_debounced: flag indicating if the software debouncer is active 42665cff704SKent Gibson * @level: the current debounced physical level of the line 42785ff37e3SAndy Shevchenko * @hdesc: the Hardware Timestamp Engine (HTE) descriptor 42885ff37e3SAndy Shevchenko * @raw_level: the line level at the time of event 42985ff37e3SAndy Shevchenko * @total_discard_seq: the running counter of the discarded events 43085ff37e3SAndy Shevchenko * @last_seqno: the last sequence number before debounce period expires 4313c0d9c63SKent Gibson */ 4323c0d9c63SKent Gibson struct line { 4333c0d9c63SKent Gibson struct gpio_desc *desc; 43473e03419SKent Gibson /* 43573e03419SKent Gibson * -- edge detector specific fields -- 43673e03419SKent Gibson */ 43773e03419SKent Gibson struct linereq *req; 43873e03419SKent Gibson unsigned int irq; 4393ffb7c45SKent Gibson /* 440b1a92e94SKent Gibson * The flags for the active edge detector configuration. 441b1a92e94SKent Gibson * 442b1a92e94SKent Gibson * edflags is set by linereq_create(), linereq_free(), and 443b1a92e94SKent Gibson * linereq_set_config_unlocked(), which are themselves mutually 444b1a92e94SKent Gibson * exclusive, and is accessed by edge_irq_thread(), 445b1a92e94SKent Gibson * process_hw_ts_thread() and debounce_work_func(), 446b1a92e94SKent Gibson * which can all live with a slightly stale value. 4473ffb7c45SKent Gibson */ 448b1a92e94SKent Gibson u64 edflags; 44973e03419SKent Gibson /* 45073e03419SKent Gibson * timestamp_ns and req_seqno are accessed only by 45173e03419SKent Gibson * edge_irq_handler() and edge_irq_thread(), which are themselves 45273e03419SKent Gibson * mutually exclusive, so no additional protection is necessary. 45373e03419SKent Gibson */ 45473e03419SKent Gibson u64 timestamp_ns; 45573e03419SKent Gibson u32 req_seqno; 45665cff704SKent Gibson /* 45765cff704SKent Gibson * line_seqno is accessed by either edge_irq_thread() or 45865cff704SKent Gibson * debounce_work_func(), which are themselves mutually exclusive, 45965cff704SKent Gibson * so no additional protection is necessary. 46065cff704SKent Gibson */ 46173e03419SKent Gibson u32 line_seqno; 46265cff704SKent Gibson /* 46365cff704SKent Gibson * -- debouncer specific fields -- 46465cff704SKent Gibson */ 46565cff704SKent Gibson struct delayed_work work; 46665cff704SKent Gibson /* 46765cff704SKent Gibson * sw_debounce is accessed by linereq_set_config(), which is the 46865cff704SKent Gibson * only setter, and linereq_get_values(), which can live with a 46965cff704SKent Gibson * slightly stale value. 47065cff704SKent Gibson */ 47165cff704SKent Gibson unsigned int sw_debounced; 47265cff704SKent Gibson /* 47365cff704SKent Gibson * level is accessed by debounce_work_func(), which is the only 47465cff704SKent Gibson * setter, and linereq_get_values() which can live with a slightly 47565cff704SKent Gibson * stale value. 47665cff704SKent Gibson */ 47765cff704SKent Gibson unsigned int level; 478272ddba0SKent Gibson #ifdef CONFIG_HTE 4792068339aSDipen Patel struct hte_ts_desc hdesc; 4802068339aSDipen Patel /* 4812068339aSDipen Patel * HTE provider sets line level at the time of event. The valid 4822068339aSDipen Patel * value is 0 or 1 and negative value for an error. 4832068339aSDipen Patel */ 4842068339aSDipen Patel int raw_level; 4852068339aSDipen Patel /* 4862068339aSDipen Patel * when sw_debounce is set on HTE enabled line, this is running 4872068339aSDipen Patel * counter of the discarded events. 4882068339aSDipen Patel */ 4892068339aSDipen Patel u32 total_discard_seq; 4902068339aSDipen Patel /* 4912068339aSDipen Patel * when sw_debounce is set on HTE enabled line, this variable records 4922068339aSDipen Patel * last sequence number before debounce period expires. 4932068339aSDipen Patel */ 4942068339aSDipen Patel u32 last_seqno; 495272ddba0SKent Gibson #endif /* CONFIG_HTE */ 4963c0d9c63SKent Gibson }; 4973c0d9c63SKent Gibson 4983c0d9c63SKent Gibson /** 4993c0d9c63SKent Gibson * struct linereq - contains the state of a userspace line request 5003c0d9c63SKent Gibson * @gdev: the GPIO device the line request pertains to 5013c0d9c63SKent Gibson * @label: consumer label used to tag GPIO descriptors 5023c0d9c63SKent Gibson * @num_lines: the number of lines in the lines array 50373e03419SKent Gibson * @wait: wait queue that handles blocking reads of events 50473e03419SKent Gibson * @event_buffer_size: the number of elements allocated in @events 50573e03419SKent Gibson * @events: KFIFO for the GPIO events 50673e03419SKent Gibson * @seqno: the sequence number for edge events generated on all lines in 50773e03419SKent Gibson * this line request. Note that this is not used when @num_lines is 1, as 50873e03419SKent Gibson * the line_seqno is then the same and is cheaper to calculate. 509a54756cbSKent Gibson * @config_mutex: mutex for serializing ioctl() calls to ensure consistency 510a54756cbSKent Gibson * of configuration, particularly multi-step accesses to desc flags. 5113c0d9c63SKent Gibson * @lines: the lines held by this line request, with @num_lines elements. 5123c0d9c63SKent Gibson */ 5133c0d9c63SKent Gibson struct linereq { 5143c0d9c63SKent Gibson struct gpio_device *gdev; 5153c0d9c63SKent Gibson const char *label; 5163c0d9c63SKent Gibson u32 num_lines; 51773e03419SKent Gibson wait_queue_head_t wait; 51873e03419SKent Gibson u32 event_buffer_size; 51973e03419SKent Gibson DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); 52073e03419SKent Gibson atomic_t seqno; 521a54756cbSKent Gibson struct mutex config_mutex; 5223c0d9c63SKent Gibson struct line lines[]; 5233c0d9c63SKent Gibson }; 5243c0d9c63SKent Gibson 5253c0d9c63SKent Gibson #define GPIO_V2_LINE_BIAS_FLAGS \ 5263c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ 5273c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \ 5283c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_DISABLED) 5293c0d9c63SKent Gibson 5303c0d9c63SKent Gibson #define GPIO_V2_LINE_DIRECTION_FLAGS \ 5313c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_INPUT | \ 5323c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_OUTPUT) 5333c0d9c63SKent Gibson 5343c0d9c63SKent Gibson #define GPIO_V2_LINE_DRIVE_FLAGS \ 5353c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \ 5363c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_OPEN_SOURCE) 5373c0d9c63SKent Gibson 53873e03419SKent Gibson #define GPIO_V2_LINE_EDGE_FLAGS \ 53973e03419SKent Gibson (GPIO_V2_LINE_FLAG_EDGE_RISING | \ 54073e03419SKent Gibson GPIO_V2_LINE_FLAG_EDGE_FALLING) 54173e03419SKent Gibson 5425e2ca893SKent Gibson #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS 5435e2ca893SKent Gibson 5443c0d9c63SKent Gibson #define GPIO_V2_LINE_VALID_FLAGS \ 5453c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ 5463c0d9c63SKent Gibson GPIO_V2_LINE_DIRECTION_FLAGS | \ 5473c0d9c63SKent Gibson GPIO_V2_LINE_DRIVE_FLAGS | \ 54873e03419SKent Gibson GPIO_V2_LINE_EDGE_FLAGS | \ 54926d060e4SKent Gibson GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \ 5502068339aSDipen Patel GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \ 5513c0d9c63SKent Gibson GPIO_V2_LINE_BIAS_FLAGS) 5523c0d9c63SKent Gibson 553b1a92e94SKent Gibson /* subset of flags relevant for edge detector configuration */ 554b1a92e94SKent Gibson #define GPIO_V2_LINE_EDGE_DETECTOR_FLAGS \ 555b1a92e94SKent Gibson (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ 556b1a92e94SKent Gibson GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \ 557b1a92e94SKent Gibson GPIO_V2_LINE_EDGE_FLAGS) 558b1a92e94SKent Gibson 55973e03419SKent Gibson static void linereq_put_event(struct linereq *lr, 56073e03419SKent Gibson struct gpio_v2_line_event *le) 56173e03419SKent Gibson { 56273e03419SKent Gibson bool overflow = false; 56373e03419SKent Gibson 56473e03419SKent Gibson spin_lock(&lr->wait.lock); 56573e03419SKent Gibson if (kfifo_is_full(&lr->events)) { 56673e03419SKent Gibson overflow = true; 56773e03419SKent Gibson kfifo_skip(&lr->events); 56873e03419SKent Gibson } 56973e03419SKent Gibson kfifo_in(&lr->events, le, 1); 57073e03419SKent Gibson spin_unlock(&lr->wait.lock); 57173e03419SKent Gibson if (!overflow) 57273e03419SKent Gibson wake_up_poll(&lr->wait, EPOLLIN); 57373e03419SKent Gibson else 57473e03419SKent Gibson pr_debug_ratelimited("event FIFO is full - event dropped\n"); 57573e03419SKent Gibson } 57673e03419SKent Gibson 57726d060e4SKent Gibson static u64 line_event_timestamp(struct line *line) 57826d060e4SKent Gibson { 57926d060e4SKent Gibson if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags)) 58026d060e4SKent Gibson return ktime_get_real_ns(); 581272ddba0SKent Gibson else if (IS_ENABLED(CONFIG_HTE) && 582272ddba0SKent Gibson test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags)) 5832068339aSDipen Patel return line->timestamp_ns; 58426d060e4SKent Gibson 58526d060e4SKent Gibson return ktime_get_ns(); 58626d060e4SKent Gibson } 58726d060e4SKent Gibson 58824220232SKent Gibson static u32 line_event_id(int level) 58924220232SKent Gibson { 59024220232SKent Gibson return level ? GPIO_V2_LINE_EVENT_RISING_EDGE : 59124220232SKent Gibson GPIO_V2_LINE_EVENT_FALLING_EDGE; 59224220232SKent Gibson } 59324220232SKent Gibson 594272ddba0SKent Gibson #ifdef CONFIG_HTE 595272ddba0SKent Gibson 5962068339aSDipen Patel static enum hte_return process_hw_ts_thread(void *p) 5972068339aSDipen Patel { 5982068339aSDipen Patel struct line *line; 5992068339aSDipen Patel struct linereq *lr; 6002068339aSDipen Patel struct gpio_v2_line_event le; 601b1a92e94SKent Gibson u64 edflags; 6022068339aSDipen Patel int level; 6032068339aSDipen Patel 6042068339aSDipen Patel if (!p) 6052068339aSDipen Patel return HTE_CB_HANDLED; 6062068339aSDipen Patel 6072068339aSDipen Patel line = p; 6082068339aSDipen Patel lr = line->req; 6092068339aSDipen Patel 6102068339aSDipen Patel memset(&le, 0, sizeof(le)); 6112068339aSDipen Patel 6122068339aSDipen Patel le.timestamp_ns = line->timestamp_ns; 613b1a92e94SKent Gibson edflags = READ_ONCE(line->edflags); 6142068339aSDipen Patel 615b1a92e94SKent Gibson switch (edflags & GPIO_V2_LINE_EDGE_FLAGS) { 616cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_BOTH: 61724220232SKent Gibson level = (line->raw_level >= 0) ? 61824220232SKent Gibson line->raw_level : 61924220232SKent Gibson gpiod_get_raw_value_cansleep(line->desc); 6202068339aSDipen Patel 621b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 62224220232SKent Gibson level = !level; 62324220232SKent Gibson 62424220232SKent Gibson le.id = line_event_id(level); 625cfa53463SKent Gibson break; 626cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_RISING: 6272068339aSDipen Patel le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 628cfa53463SKent Gibson break; 629cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_FALLING: 6302068339aSDipen Patel le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 631cfa53463SKent Gibson break; 632cfa53463SKent Gibson default: 6332068339aSDipen Patel return HTE_CB_HANDLED; 6342068339aSDipen Patel } 6352068339aSDipen Patel le.line_seqno = line->line_seqno; 6362068339aSDipen Patel le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno; 6372068339aSDipen Patel le.offset = gpio_chip_hwgpio(line->desc); 6382068339aSDipen Patel 6392068339aSDipen Patel linereq_put_event(lr, &le); 6402068339aSDipen Patel 6412068339aSDipen Patel return HTE_CB_HANDLED; 6422068339aSDipen Patel } 6432068339aSDipen Patel 6442068339aSDipen Patel static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p) 6452068339aSDipen Patel { 6462068339aSDipen Patel struct line *line; 6472068339aSDipen Patel struct linereq *lr; 6482068339aSDipen Patel int diff_seqno = 0; 6492068339aSDipen Patel 6502068339aSDipen Patel if (!ts || !p) 6512068339aSDipen Patel return HTE_CB_HANDLED; 6522068339aSDipen Patel 6532068339aSDipen Patel line = p; 6542068339aSDipen Patel line->timestamp_ns = ts->tsc; 6552068339aSDipen Patel line->raw_level = ts->raw_level; 6562068339aSDipen Patel lr = line->req; 6572068339aSDipen Patel 6582068339aSDipen Patel if (READ_ONCE(line->sw_debounced)) { 6592068339aSDipen Patel line->total_discard_seq++; 6602068339aSDipen Patel line->last_seqno = ts->seq; 6612068339aSDipen Patel mod_delayed_work(system_wq, &line->work, 6622068339aSDipen Patel usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us))); 6632068339aSDipen Patel } else { 6642068339aSDipen Patel if (unlikely(ts->seq < line->line_seqno)) 6652068339aSDipen Patel return HTE_CB_HANDLED; 6662068339aSDipen Patel 6672068339aSDipen Patel diff_seqno = ts->seq - line->line_seqno; 6682068339aSDipen Patel line->line_seqno = ts->seq; 6692068339aSDipen Patel if (lr->num_lines != 1) 6702068339aSDipen Patel line->req_seqno = atomic_add_return(diff_seqno, 6712068339aSDipen Patel &lr->seqno); 6722068339aSDipen Patel 6732068339aSDipen Patel return HTE_RUN_SECOND_CB; 6742068339aSDipen Patel } 6752068339aSDipen Patel 6762068339aSDipen Patel return HTE_CB_HANDLED; 6772068339aSDipen Patel } 6782068339aSDipen Patel 679272ddba0SKent Gibson static int hte_edge_setup(struct line *line, u64 eflags) 680272ddba0SKent Gibson { 681272ddba0SKent Gibson int ret; 682272ddba0SKent Gibson unsigned long flags = 0; 683272ddba0SKent Gibson struct hte_ts_desc *hdesc = &line->hdesc; 684272ddba0SKent Gibson 685272ddba0SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) 686272ddba0SKent Gibson flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 687272ddba0SKent Gibson HTE_FALLING_EDGE_TS : 688272ddba0SKent Gibson HTE_RISING_EDGE_TS; 689272ddba0SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) 690272ddba0SKent Gibson flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 691272ddba0SKent Gibson HTE_RISING_EDGE_TS : 692272ddba0SKent Gibson HTE_FALLING_EDGE_TS; 693272ddba0SKent Gibson 694272ddba0SKent Gibson line->total_discard_seq = 0; 695272ddba0SKent Gibson 696272ddba0SKent Gibson hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags, NULL, 697272ddba0SKent Gibson line->desc); 698272ddba0SKent Gibson 699272ddba0SKent Gibson ret = hte_ts_get(NULL, hdesc, 0); 700272ddba0SKent Gibson if (ret) 701272ddba0SKent Gibson return ret; 702272ddba0SKent Gibson 703272ddba0SKent Gibson return hte_request_ts_ns(hdesc, process_hw_ts, process_hw_ts_thread, 704272ddba0SKent Gibson line); 705272ddba0SKent Gibson } 706272ddba0SKent Gibson 707272ddba0SKent Gibson #else 708272ddba0SKent Gibson 709272ddba0SKent Gibson static int hte_edge_setup(struct line *line, u64 eflags) 710272ddba0SKent Gibson { 711272ddba0SKent Gibson return 0; 712272ddba0SKent Gibson } 713272ddba0SKent Gibson #endif /* CONFIG_HTE */ 714272ddba0SKent Gibson 71573e03419SKent Gibson static irqreturn_t edge_irq_thread(int irq, void *p) 71673e03419SKent Gibson { 71773e03419SKent Gibson struct line *line = p; 71873e03419SKent Gibson struct linereq *lr = line->req; 71973e03419SKent Gibson struct gpio_v2_line_event le; 72073e03419SKent Gibson 72173e03419SKent Gibson /* Do not leak kernel stack to userspace */ 72273e03419SKent Gibson memset(&le, 0, sizeof(le)); 72373e03419SKent Gibson 72473e03419SKent Gibson if (line->timestamp_ns) { 72573e03419SKent Gibson le.timestamp_ns = line->timestamp_ns; 72673e03419SKent Gibson } else { 72773e03419SKent Gibson /* 72873e03419SKent Gibson * We may be running from a nested threaded interrupt in 72973e03419SKent Gibson * which case we didn't get the timestamp from 73073e03419SKent Gibson * edge_irq_handler(). 73173e03419SKent Gibson */ 73226d060e4SKent Gibson le.timestamp_ns = line_event_timestamp(line); 73373e03419SKent Gibson if (lr->num_lines != 1) 73473e03419SKent Gibson line->req_seqno = atomic_inc_return(&lr->seqno); 73573e03419SKent Gibson } 73673e03419SKent Gibson line->timestamp_ns = 0; 73773e03419SKent Gibson 738b1a92e94SKent Gibson switch (READ_ONCE(line->edflags) & GPIO_V2_LINE_EDGE_FLAGS) { 739cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_BOTH: 74024220232SKent Gibson le.id = line_event_id(gpiod_get_value_cansleep(line->desc)); 741cfa53463SKent Gibson break; 742cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_RISING: 74373e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 744cfa53463SKent Gibson break; 745cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_FALLING: 74673e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 747cfa53463SKent Gibson break; 748cfa53463SKent Gibson default: 74973e03419SKent Gibson return IRQ_NONE; 75073e03419SKent Gibson } 75173e03419SKent Gibson line->line_seqno++; 75273e03419SKent Gibson le.line_seqno = line->line_seqno; 75373e03419SKent Gibson le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno; 75473e03419SKent Gibson le.offset = gpio_chip_hwgpio(line->desc); 75573e03419SKent Gibson 75673e03419SKent Gibson linereq_put_event(lr, &le); 75773e03419SKent Gibson 75873e03419SKent Gibson return IRQ_HANDLED; 75973e03419SKent Gibson } 76073e03419SKent Gibson 76173e03419SKent Gibson static irqreturn_t edge_irq_handler(int irq, void *p) 76273e03419SKent Gibson { 76373e03419SKent Gibson struct line *line = p; 76473e03419SKent Gibson struct linereq *lr = line->req; 76573e03419SKent Gibson 76673e03419SKent Gibson /* 76773e03419SKent Gibson * Just store the timestamp in hardirq context so we get it as 76873e03419SKent Gibson * close in time as possible to the actual event. 76973e03419SKent Gibson */ 77026d060e4SKent Gibson line->timestamp_ns = line_event_timestamp(line); 77173e03419SKent Gibson 77273e03419SKent Gibson if (lr->num_lines != 1) 77373e03419SKent Gibson line->req_seqno = atomic_inc_return(&lr->seqno); 77473e03419SKent Gibson 77573e03419SKent Gibson return IRQ_WAKE_THREAD; 77673e03419SKent Gibson } 77773e03419SKent Gibson 77865cff704SKent Gibson /* 77965cff704SKent Gibson * returns the current debounced logical value. 78065cff704SKent Gibson */ 78165cff704SKent Gibson static bool debounced_value(struct line *line) 78265cff704SKent Gibson { 78365cff704SKent Gibson bool value; 78465cff704SKent Gibson 78565cff704SKent Gibson /* 78665cff704SKent Gibson * minor race - debouncer may be stopped here, so edge_detector_stop() 78765cff704SKent Gibson * must leave the value unchanged so the following will read the level 78865cff704SKent Gibson * from when the debouncer was last running. 78965cff704SKent Gibson */ 79065cff704SKent Gibson value = READ_ONCE(line->level); 79165cff704SKent Gibson 79265cff704SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags)) 79365cff704SKent Gibson value = !value; 79465cff704SKent Gibson 79565cff704SKent Gibson return value; 79665cff704SKent Gibson } 79765cff704SKent Gibson 79865cff704SKent Gibson static irqreturn_t debounce_irq_handler(int irq, void *p) 79965cff704SKent Gibson { 80065cff704SKent Gibson struct line *line = p; 80165cff704SKent Gibson 80265cff704SKent Gibson mod_delayed_work(system_wq, &line->work, 80365cff704SKent Gibson usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us))); 80465cff704SKent Gibson 80565cff704SKent Gibson return IRQ_HANDLED; 80665cff704SKent Gibson } 80765cff704SKent Gibson 80865cff704SKent Gibson static void debounce_work_func(struct work_struct *work) 80965cff704SKent Gibson { 81065cff704SKent Gibson struct gpio_v2_line_event le; 81165cff704SKent Gibson struct line *line = container_of(work, struct line, work.work); 81265cff704SKent Gibson struct linereq *lr; 813b1a92e94SKent Gibson u64 eflags, edflags = READ_ONCE(line->edflags); 814272ddba0SKent Gibson int level = -1; 815272ddba0SKent Gibson #ifdef CONFIG_HTE 816272ddba0SKent Gibson int diff_seqno; 81765cff704SKent Gibson 818b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) 8192068339aSDipen Patel level = line->raw_level; 820272ddba0SKent Gibson #endif 8212068339aSDipen Patel if (level < 0) 82265cff704SKent Gibson level = gpiod_get_raw_value_cansleep(line->desc); 82365cff704SKent Gibson if (level < 0) { 82465cff704SKent Gibson pr_debug_ratelimited("debouncer failed to read line value\n"); 82565cff704SKent Gibson return; 82665cff704SKent Gibson } 82765cff704SKent Gibson 82865cff704SKent Gibson if (READ_ONCE(line->level) == level) 82965cff704SKent Gibson return; 83065cff704SKent Gibson 83165cff704SKent Gibson WRITE_ONCE(line->level, level); 83265cff704SKent Gibson 83365cff704SKent Gibson /* -- edge detection -- */ 834b1a92e94SKent Gibson eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS; 8353ffb7c45SKent Gibson if (!eflags) 83665cff704SKent Gibson return; 83765cff704SKent Gibson 83865cff704SKent Gibson /* switch from physical level to logical - if they differ */ 839b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 84065cff704SKent Gibson level = !level; 84165cff704SKent Gibson 84265cff704SKent Gibson /* ignore edges that are not being monitored */ 8433ffb7c45SKent Gibson if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) || 8443ffb7c45SKent Gibson ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level)) 84565cff704SKent Gibson return; 84665cff704SKent Gibson 84765cff704SKent Gibson /* Do not leak kernel stack to userspace */ 84865cff704SKent Gibson memset(&le, 0, sizeof(le)); 84965cff704SKent Gibson 85065cff704SKent Gibson lr = line->req; 85126d060e4SKent Gibson le.timestamp_ns = line_event_timestamp(line); 85265cff704SKent Gibson le.offset = gpio_chip_hwgpio(line->desc); 853272ddba0SKent Gibson #ifdef CONFIG_HTE 854b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) { 8552068339aSDipen Patel /* discard events except the last one */ 8562068339aSDipen Patel line->total_discard_seq -= 1; 8572068339aSDipen Patel diff_seqno = line->last_seqno - line->total_discard_seq - 8582068339aSDipen Patel line->line_seqno; 8592068339aSDipen Patel line->line_seqno = line->last_seqno - line->total_discard_seq; 8602068339aSDipen Patel le.line_seqno = line->line_seqno; 8612068339aSDipen Patel le.seqno = (lr->num_lines == 1) ? 8622068339aSDipen Patel le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno); 863272ddba0SKent Gibson } else 864272ddba0SKent Gibson #endif /* CONFIG_HTE */ 865272ddba0SKent Gibson { 86665cff704SKent Gibson line->line_seqno++; 86765cff704SKent Gibson le.line_seqno = line->line_seqno; 86865cff704SKent Gibson le.seqno = (lr->num_lines == 1) ? 86965cff704SKent Gibson le.line_seqno : atomic_inc_return(&lr->seqno); 8702068339aSDipen Patel } 87165cff704SKent Gibson 87224220232SKent Gibson le.id = line_event_id(level); 87365cff704SKent Gibson 87465cff704SKent Gibson linereq_put_event(lr, &le); 87565cff704SKent Gibson } 87665cff704SKent Gibson 877b1a92e94SKent Gibson static int debounce_setup(struct line *line, unsigned int debounce_period_us) 87865cff704SKent Gibson { 87965cff704SKent Gibson unsigned long irqflags; 88065cff704SKent Gibson int ret, level, irq; 88165cff704SKent Gibson 88265cff704SKent Gibson /* try hardware */ 88365cff704SKent Gibson ret = gpiod_set_debounce(line->desc, debounce_period_us); 88465cff704SKent Gibson if (!ret) { 88565cff704SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 88665cff704SKent Gibson return ret; 88765cff704SKent Gibson } 88865cff704SKent Gibson if (ret != -ENOTSUPP) 88965cff704SKent Gibson return ret; 89065cff704SKent Gibson 89165cff704SKent Gibson if (debounce_period_us) { 89265cff704SKent Gibson /* setup software debounce */ 89365cff704SKent Gibson level = gpiod_get_raw_value_cansleep(line->desc); 89465cff704SKent Gibson if (level < 0) 89565cff704SKent Gibson return level; 89665cff704SKent Gibson 897272ddba0SKent Gibson if (!(IS_ENABLED(CONFIG_HTE) && 898272ddba0SKent Gibson test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))) { 89965cff704SKent Gibson irq = gpiod_to_irq(line->desc); 90065cff704SKent Gibson if (irq < 0) 90165cff704SKent Gibson return -ENXIO; 90265cff704SKent Gibson 90365cff704SKent Gibson irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING; 90465cff704SKent Gibson ret = request_irq(irq, debounce_irq_handler, irqflags, 90565cff704SKent Gibson line->req->label, line); 90665cff704SKent Gibson if (ret) 90765cff704SKent Gibson return ret; 90865cff704SKent Gibson line->irq = irq; 9092068339aSDipen Patel } else { 9102487a812SKent Gibson ret = hte_edge_setup(line, GPIO_V2_LINE_FLAG_EDGE_BOTH); 9112068339aSDipen Patel if (ret) 9122068339aSDipen Patel return ret; 9132068339aSDipen Patel } 9142068339aSDipen Patel 9152068339aSDipen Patel WRITE_ONCE(line->level, level); 9162068339aSDipen Patel WRITE_ONCE(line->sw_debounced, 1); 91765cff704SKent Gibson } 91865cff704SKent Gibson return 0; 91965cff704SKent Gibson } 92065cff704SKent Gibson 92165cff704SKent Gibson static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc, 92265cff704SKent Gibson unsigned int line_idx) 92365cff704SKent Gibson { 92465cff704SKent Gibson unsigned int i; 92565cff704SKent Gibson u64 mask = BIT_ULL(line_idx); 92665cff704SKent Gibson 92765cff704SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 92865cff704SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && 92965cff704SKent Gibson (lc->attrs[i].mask & mask)) 93065cff704SKent Gibson return true; 93165cff704SKent Gibson } 93265cff704SKent Gibson return false; 93365cff704SKent Gibson } 93465cff704SKent Gibson 93565cff704SKent Gibson static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc, 93665cff704SKent Gibson unsigned int line_idx) 93765cff704SKent Gibson { 93865cff704SKent Gibson unsigned int i; 93965cff704SKent Gibson u64 mask = BIT_ULL(line_idx); 94065cff704SKent Gibson 94165cff704SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 94265cff704SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && 94365cff704SKent Gibson (lc->attrs[i].mask & mask)) 94465cff704SKent Gibson return lc->attrs[i].attr.debounce_period_us; 94565cff704SKent Gibson } 94665cff704SKent Gibson return 0; 94765cff704SKent Gibson } 94865cff704SKent Gibson 949b1a92e94SKent Gibson static void edge_detector_stop(struct line *line) 95073e03419SKent Gibson { 951b1a92e94SKent Gibson if (line->irq) { 95273e03419SKent Gibson free_irq(line->irq, line); 95373e03419SKent Gibson line->irq = 0; 95473e03419SKent Gibson } 955a54756cbSKent Gibson 956272ddba0SKent Gibson #ifdef CONFIG_HTE 957b1a92e94SKent Gibson if (READ_ONCE(line->edflags) & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) 9582068339aSDipen Patel hte_ts_put(&line->hdesc); 959272ddba0SKent Gibson #endif 9602068339aSDipen Patel 96165cff704SKent Gibson cancel_delayed_work_sync(&line->work); 96265cff704SKent Gibson WRITE_ONCE(line->sw_debounced, 0); 963b1a92e94SKent Gibson WRITE_ONCE(line->edflags, 0); 96403a58ea5SKent Gibson if (line->desc) 96503a58ea5SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, 0); 96665cff704SKent Gibson /* do not change line->level - see comment in debounced_value() */ 96773e03419SKent Gibson } 96873e03419SKent Gibson 96973e03419SKent Gibson static int edge_detector_setup(struct line *line, 97065cff704SKent Gibson struct gpio_v2_line_config *lc, 971b1a92e94SKent Gibson unsigned int line_idx, u64 edflags) 97273e03419SKent Gibson { 97365cff704SKent Gibson u32 debounce_period_us; 97473e03419SKent Gibson unsigned long irqflags = 0; 975b1a92e94SKent Gibson u64 eflags; 97673e03419SKent Gibson int irq, ret; 97773e03419SKent Gibson 978b1a92e94SKent Gibson eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS; 97973e03419SKent Gibson if (eflags && !kfifo_initialized(&line->req->events)) { 98073e03419SKent Gibson ret = kfifo_alloc(&line->req->events, 98173e03419SKent Gibson line->req->event_buffer_size, GFP_KERNEL); 98273e03419SKent Gibson if (ret) 98373e03419SKent Gibson return ret; 98473e03419SKent Gibson } 98565cff704SKent Gibson if (gpio_v2_line_config_debounced(lc, line_idx)) { 98665cff704SKent Gibson debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx); 987b1a92e94SKent Gibson ret = debounce_setup(line, debounce_period_us); 98865cff704SKent Gibson if (ret) 98965cff704SKent Gibson return ret; 99065cff704SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 99165cff704SKent Gibson } 99273e03419SKent Gibson 99365cff704SKent Gibson /* detection disabled or sw debouncer will provide edge detection */ 99465cff704SKent Gibson if (!eflags || READ_ONCE(line->sw_debounced)) 99573e03419SKent Gibson return 0; 99673e03419SKent Gibson 997272ddba0SKent Gibson if (IS_ENABLED(CONFIG_HTE) && 998272ddba0SKent Gibson (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)) 999b1a92e94SKent Gibson return hte_edge_setup(line, edflags); 10002068339aSDipen Patel 100173e03419SKent Gibson irq = gpiod_to_irq(line->desc); 100273e03419SKent Gibson if (irq < 0) 100373e03419SKent Gibson return -ENXIO; 100473e03419SKent Gibson 100573e03419SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) 100673e03419SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 100773e03419SKent Gibson IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 100873e03419SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) 100973e03419SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 101073e03419SKent Gibson IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 101173e03419SKent Gibson irqflags |= IRQF_ONESHOT; 101273e03419SKent Gibson 101373e03419SKent Gibson /* Request a thread to read the events */ 101473e03419SKent Gibson ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread, 101573e03419SKent Gibson irqflags, line->req->label, line); 101673e03419SKent Gibson if (ret) 101773e03419SKent Gibson return ret; 101873e03419SKent Gibson 101973e03419SKent Gibson line->irq = irq; 102073e03419SKent Gibson return 0; 102173e03419SKent Gibson } 102273e03419SKent Gibson 102365cff704SKent Gibson static int edge_detector_update(struct line *line, 102465cff704SKent Gibson struct gpio_v2_line_config *lc, 1025b1a92e94SKent Gibson unsigned int line_idx, u64 edflags) 1026a54756cbSKent Gibson { 1027b1a92e94SKent Gibson u64 active_edflags = READ_ONCE(line->edflags); 102865cff704SKent Gibson unsigned int debounce_period_us = 102965cff704SKent Gibson gpio_v2_line_config_debounce_period(lc, line_idx); 103065cff704SKent Gibson 1031b1a92e94SKent Gibson if ((active_edflags == edflags) && 1032b1a92e94SKent Gibson (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us)) 1033a54756cbSKent Gibson return 0; 1034a54756cbSKent Gibson 103565cff704SKent Gibson /* sw debounced and still will be...*/ 103665cff704SKent Gibson if (debounce_period_us && READ_ONCE(line->sw_debounced)) { 103765cff704SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 103865cff704SKent Gibson return 0; 103965cff704SKent Gibson } 104065cff704SKent Gibson 104165cff704SKent Gibson /* reconfiguring edge detection or sw debounce being disabled */ 1042b1a92e94SKent Gibson if ((line->irq && !READ_ONCE(line->sw_debounced)) || 1043b1a92e94SKent Gibson (active_edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) || 104465cff704SKent Gibson (!debounce_period_us && READ_ONCE(line->sw_debounced))) 1045b1a92e94SKent Gibson edge_detector_stop(line); 1046a54756cbSKent Gibson 1047b1a92e94SKent Gibson return edge_detector_setup(line, lc, line_idx, edflags); 1048a54756cbSKent Gibson } 1049a54756cbSKent Gibson 10503c0d9c63SKent Gibson static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, 10513c0d9c63SKent Gibson unsigned int line_idx) 10523c0d9c63SKent Gibson { 10533c0d9c63SKent Gibson unsigned int i; 10543c0d9c63SKent Gibson u64 mask = BIT_ULL(line_idx); 10553c0d9c63SKent Gibson 10563c0d9c63SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 10573c0d9c63SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) && 10583c0d9c63SKent Gibson (lc->attrs[i].mask & mask)) 10593c0d9c63SKent Gibson return lc->attrs[i].attr.flags; 10603c0d9c63SKent Gibson } 10613c0d9c63SKent Gibson return lc->flags; 10623c0d9c63SKent Gibson } 10633c0d9c63SKent Gibson 10643c0d9c63SKent Gibson static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc, 10653c0d9c63SKent Gibson unsigned int line_idx) 10663c0d9c63SKent Gibson { 10673c0d9c63SKent Gibson unsigned int i; 10683c0d9c63SKent Gibson u64 mask = BIT_ULL(line_idx); 10693c0d9c63SKent Gibson 10703c0d9c63SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 10713c0d9c63SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) && 10723c0d9c63SKent Gibson (lc->attrs[i].mask & mask)) 10733c0d9c63SKent Gibson return !!(lc->attrs[i].attr.values & mask); 10743c0d9c63SKent Gibson } 10753c0d9c63SKent Gibson return 0; 10763c0d9c63SKent Gibson } 10773c0d9c63SKent Gibson 10783c0d9c63SKent Gibson static int gpio_v2_line_flags_validate(u64 flags) 10793c0d9c63SKent Gibson { 10803c0d9c63SKent Gibson /* Return an error if an unknown flag is set */ 10813c0d9c63SKent Gibson if (flags & ~GPIO_V2_LINE_VALID_FLAGS) 10823c0d9c63SKent Gibson return -EINVAL; 1083272ddba0SKent Gibson 1084272ddba0SKent Gibson if (!IS_ENABLED(CONFIG_HTE) && 1085272ddba0SKent Gibson (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)) 1086272ddba0SKent Gibson return -EOPNOTSUPP; 1087272ddba0SKent Gibson 10883c0d9c63SKent Gibson /* 10893c0d9c63SKent Gibson * Do not allow both INPUT and OUTPUT flags to be set as they are 10903c0d9c63SKent Gibson * contradictory. 10913c0d9c63SKent Gibson */ 10923c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_FLAG_INPUT) && 10933c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_OUTPUT)) 10943c0d9c63SKent Gibson return -EINVAL; 10953c0d9c63SKent Gibson 10962068339aSDipen Patel /* Only allow one event clock source */ 1097272ddba0SKent Gibson if (IS_ENABLED(CONFIG_HTE) && 1098272ddba0SKent Gibson (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) && 10992068339aSDipen Patel (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)) 11002068339aSDipen Patel return -EINVAL; 11012068339aSDipen Patel 110273e03419SKent Gibson /* Edge detection requires explicit input. */ 110373e03419SKent Gibson if ((flags & GPIO_V2_LINE_EDGE_FLAGS) && 110473e03419SKent Gibson !(flags & GPIO_V2_LINE_FLAG_INPUT)) 110573e03419SKent Gibson return -EINVAL; 110673e03419SKent Gibson 11073c0d9c63SKent Gibson /* 11083c0d9c63SKent Gibson * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single 11093c0d9c63SKent Gibson * request. If the hardware actually supports enabling both at the 11103c0d9c63SKent Gibson * same time the electrical result would be disastrous. 11113c0d9c63SKent Gibson */ 11123c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) && 11133c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE)) 11143c0d9c63SKent Gibson return -EINVAL; 11153c0d9c63SKent Gibson 11163c0d9c63SKent Gibson /* Drive requires explicit output direction. */ 11173c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) && 11183c0d9c63SKent Gibson !(flags & GPIO_V2_LINE_FLAG_OUTPUT)) 11193c0d9c63SKent Gibson return -EINVAL; 11203c0d9c63SKent Gibson 11213c0d9c63SKent Gibson /* Bias requires explicit direction. */ 11223c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_BIAS_FLAGS) && 11233c0d9c63SKent Gibson !(flags & GPIO_V2_LINE_DIRECTION_FLAGS)) 11243c0d9c63SKent Gibson return -EINVAL; 11253c0d9c63SKent Gibson 11263c0d9c63SKent Gibson /* Only one bias flag can be set. */ 11273c0d9c63SKent Gibson if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) && 11283c0d9c63SKent Gibson (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | 11293c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) || 11303c0d9c63SKent Gibson ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) && 11313c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) 11323c0d9c63SKent Gibson return -EINVAL; 11333c0d9c63SKent Gibson 11343c0d9c63SKent Gibson return 0; 11353c0d9c63SKent Gibson } 11363c0d9c63SKent Gibson 11373c0d9c63SKent Gibson static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, 11383c0d9c63SKent Gibson unsigned int num_lines) 11393c0d9c63SKent Gibson { 11403c0d9c63SKent Gibson unsigned int i; 11413c0d9c63SKent Gibson u64 flags; 11423c0d9c63SKent Gibson int ret; 11433c0d9c63SKent Gibson 11443c0d9c63SKent Gibson if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX) 11453c0d9c63SKent Gibson return -EINVAL; 11463c0d9c63SKent Gibson 11473c0d9c63SKent Gibson if (memchr_inv(lc->padding, 0, sizeof(lc->padding))) 11483c0d9c63SKent Gibson return -EINVAL; 11493c0d9c63SKent Gibson 11503c0d9c63SKent Gibson for (i = 0; i < num_lines; i++) { 11513c0d9c63SKent Gibson flags = gpio_v2_line_config_flags(lc, i); 11523c0d9c63SKent Gibson ret = gpio_v2_line_flags_validate(flags); 11533c0d9c63SKent Gibson if (ret) 11543c0d9c63SKent Gibson return ret; 115565cff704SKent Gibson 115665cff704SKent Gibson /* debounce requires explicit input */ 115765cff704SKent Gibson if (gpio_v2_line_config_debounced(lc, i) && 115865cff704SKent Gibson !(flags & GPIO_V2_LINE_FLAG_INPUT)) 115965cff704SKent Gibson return -EINVAL; 11603c0d9c63SKent Gibson } 11613c0d9c63SKent Gibson return 0; 11623c0d9c63SKent Gibson } 11633c0d9c63SKent Gibson 11643c0d9c63SKent Gibson static void gpio_v2_line_config_flags_to_desc_flags(u64 flags, 11653c0d9c63SKent Gibson unsigned long *flagsp) 11663c0d9c63SKent Gibson { 11673c0d9c63SKent Gibson assign_bit(FLAG_ACTIVE_LOW, flagsp, 11683c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW); 11693c0d9c63SKent Gibson 11703c0d9c63SKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) 11713c0d9c63SKent Gibson set_bit(FLAG_IS_OUT, flagsp); 11723c0d9c63SKent Gibson else if (flags & GPIO_V2_LINE_FLAG_INPUT) 11733c0d9c63SKent Gibson clear_bit(FLAG_IS_OUT, flagsp); 11743c0d9c63SKent Gibson 117573e03419SKent Gibson assign_bit(FLAG_EDGE_RISING, flagsp, 117673e03419SKent Gibson flags & GPIO_V2_LINE_FLAG_EDGE_RISING); 117773e03419SKent Gibson assign_bit(FLAG_EDGE_FALLING, flagsp, 117873e03419SKent Gibson flags & GPIO_V2_LINE_FLAG_EDGE_FALLING); 117973e03419SKent Gibson 11803c0d9c63SKent Gibson assign_bit(FLAG_OPEN_DRAIN, flagsp, 11813c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN); 11823c0d9c63SKent Gibson assign_bit(FLAG_OPEN_SOURCE, flagsp, 11833c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE); 11843c0d9c63SKent Gibson 11853c0d9c63SKent Gibson assign_bit(FLAG_PULL_UP, flagsp, 11863c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP); 11873c0d9c63SKent Gibson assign_bit(FLAG_PULL_DOWN, flagsp, 11883c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN); 11893c0d9c63SKent Gibson assign_bit(FLAG_BIAS_DISABLE, flagsp, 11903c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED); 119126d060e4SKent Gibson 119226d060e4SKent Gibson assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp, 119326d060e4SKent Gibson flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME); 11942068339aSDipen Patel assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp, 11952068339aSDipen Patel flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE); 11963c0d9c63SKent Gibson } 11973c0d9c63SKent Gibson 11983c0d9c63SKent Gibson static long linereq_get_values(struct linereq *lr, void __user *ip) 11993c0d9c63SKent Gibson { 12003c0d9c63SKent Gibson struct gpio_v2_line_values lv; 12013c0d9c63SKent Gibson DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); 12023c0d9c63SKent Gibson struct gpio_desc **descs; 12033c0d9c63SKent Gibson unsigned int i, didx, num_get; 120465cff704SKent Gibson bool val; 12053c0d9c63SKent Gibson int ret; 12063c0d9c63SKent Gibson 12073c0d9c63SKent Gibson /* NOTE: It's ok to read values of output lines. */ 12083c0d9c63SKent Gibson if (copy_from_user(&lv, ip, sizeof(lv))) 12093c0d9c63SKent Gibson return -EFAULT; 12103c0d9c63SKent Gibson 12113c0d9c63SKent Gibson for (num_get = 0, i = 0; i < lr->num_lines; i++) { 12123c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 12133c0d9c63SKent Gibson num_get++; 12143c0d9c63SKent Gibson descs = &lr->lines[i].desc; 12153c0d9c63SKent Gibson } 12163c0d9c63SKent Gibson } 12173c0d9c63SKent Gibson 12183c0d9c63SKent Gibson if (num_get == 0) 12193c0d9c63SKent Gibson return -EINVAL; 12203c0d9c63SKent Gibson 12213c0d9c63SKent Gibson if (num_get != 1) { 12223c0d9c63SKent Gibson descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL); 12233c0d9c63SKent Gibson if (!descs) 12243c0d9c63SKent Gibson return -ENOMEM; 12253c0d9c63SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 12263c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 12273c0d9c63SKent Gibson descs[didx] = lr->lines[i].desc; 12283c0d9c63SKent Gibson didx++; 12293c0d9c63SKent Gibson } 12303c0d9c63SKent Gibson } 12313c0d9c63SKent Gibson } 12323c0d9c63SKent Gibson ret = gpiod_get_array_value_complex(false, true, num_get, 12333c0d9c63SKent Gibson descs, NULL, vals); 12343c0d9c63SKent Gibson 12353c0d9c63SKent Gibson if (num_get != 1) 12363c0d9c63SKent Gibson kfree(descs); 12373c0d9c63SKent Gibson if (ret) 12383c0d9c63SKent Gibson return ret; 12393c0d9c63SKent Gibson 12403c0d9c63SKent Gibson lv.bits = 0; 12413c0d9c63SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 12423c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 124365cff704SKent Gibson if (lr->lines[i].sw_debounced) 124465cff704SKent Gibson val = debounced_value(&lr->lines[i]); 124565cff704SKent Gibson else 124665cff704SKent Gibson val = test_bit(didx, vals); 124765cff704SKent Gibson if (val) 12483c0d9c63SKent Gibson lv.bits |= BIT_ULL(i); 12493c0d9c63SKent Gibson didx++; 12503c0d9c63SKent Gibson } 12513c0d9c63SKent Gibson } 12523c0d9c63SKent Gibson 12533c0d9c63SKent Gibson if (copy_to_user(ip, &lv, sizeof(lv))) 12543c0d9c63SKent Gibson return -EFAULT; 12553c0d9c63SKent Gibson 12563c0d9c63SKent Gibson return 0; 12573c0d9c63SKent Gibson } 12583c0d9c63SKent Gibson 12597b8e00d9SKent Gibson static long linereq_set_values_unlocked(struct linereq *lr, 12607b8e00d9SKent Gibson struct gpio_v2_line_values *lv) 12617b8e00d9SKent Gibson { 12627b8e00d9SKent Gibson DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); 12637b8e00d9SKent Gibson struct gpio_desc **descs; 12647b8e00d9SKent Gibson unsigned int i, didx, num_set; 12657b8e00d9SKent Gibson int ret; 12667b8e00d9SKent Gibson 12677b8e00d9SKent Gibson bitmap_zero(vals, GPIO_V2_LINES_MAX); 12687b8e00d9SKent Gibson for (num_set = 0, i = 0; i < lr->num_lines; i++) { 12697b8e00d9SKent Gibson if (lv->mask & BIT_ULL(i)) { 12707b8e00d9SKent Gibson if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags)) 12717b8e00d9SKent Gibson return -EPERM; 12727b8e00d9SKent Gibson if (lv->bits & BIT_ULL(i)) 12737b8e00d9SKent Gibson __set_bit(num_set, vals); 12747b8e00d9SKent Gibson num_set++; 12757b8e00d9SKent Gibson descs = &lr->lines[i].desc; 12767b8e00d9SKent Gibson } 12777b8e00d9SKent Gibson } 12787b8e00d9SKent Gibson if (num_set == 0) 12797b8e00d9SKent Gibson return -EINVAL; 12807b8e00d9SKent Gibson 12817b8e00d9SKent Gibson if (num_set != 1) { 12827b8e00d9SKent Gibson /* build compacted desc array and values */ 12837b8e00d9SKent Gibson descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL); 12847b8e00d9SKent Gibson if (!descs) 12857b8e00d9SKent Gibson return -ENOMEM; 12867b8e00d9SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 12877b8e00d9SKent Gibson if (lv->mask & BIT_ULL(i)) { 12887b8e00d9SKent Gibson descs[didx] = lr->lines[i].desc; 12897b8e00d9SKent Gibson didx++; 12907b8e00d9SKent Gibson } 12917b8e00d9SKent Gibson } 12927b8e00d9SKent Gibson } 12937b8e00d9SKent Gibson ret = gpiod_set_array_value_complex(false, true, num_set, 12947b8e00d9SKent Gibson descs, NULL, vals); 12957b8e00d9SKent Gibson 12967b8e00d9SKent Gibson if (num_set != 1) 12977b8e00d9SKent Gibson kfree(descs); 12987b8e00d9SKent Gibson return ret; 12997b8e00d9SKent Gibson } 13007b8e00d9SKent Gibson 13017b8e00d9SKent Gibson static long linereq_set_values(struct linereq *lr, void __user *ip) 13027b8e00d9SKent Gibson { 13037b8e00d9SKent Gibson struct gpio_v2_line_values lv; 13047b8e00d9SKent Gibson int ret; 13057b8e00d9SKent Gibson 13067b8e00d9SKent Gibson if (copy_from_user(&lv, ip, sizeof(lv))) 13077b8e00d9SKent Gibson return -EFAULT; 13087b8e00d9SKent Gibson 13097b8e00d9SKent Gibson mutex_lock(&lr->config_mutex); 13107b8e00d9SKent Gibson 13117b8e00d9SKent Gibson ret = linereq_set_values_unlocked(lr, &lv); 13127b8e00d9SKent Gibson 13137b8e00d9SKent Gibson mutex_unlock(&lr->config_mutex); 13147b8e00d9SKent Gibson 13157b8e00d9SKent Gibson return ret; 13167b8e00d9SKent Gibson } 13177b8e00d9SKent Gibson 1318a54756cbSKent Gibson static long linereq_set_config_unlocked(struct linereq *lr, 1319a54756cbSKent Gibson struct gpio_v2_line_config *lc) 1320a54756cbSKent Gibson { 1321a54756cbSKent Gibson struct gpio_desc *desc; 1322b1a92e94SKent Gibson struct line *line; 1323a54756cbSKent Gibson unsigned int i; 1324b1a92e94SKent Gibson u64 flags, edflags; 1325a54756cbSKent Gibson int ret; 1326a54756cbSKent Gibson 1327a54756cbSKent Gibson for (i = 0; i < lr->num_lines; i++) { 1328b1a92e94SKent Gibson line = &lr->lines[i]; 1329a54756cbSKent Gibson desc = lr->lines[i].desc; 1330a54756cbSKent Gibson flags = gpio_v2_line_config_flags(lc, i); 1331a54756cbSKent Gibson gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 1332b1a92e94SKent Gibson edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS; 1333a54756cbSKent Gibson /* 1334a54756cbSKent Gibson * Lines have to be requested explicitly for input 1335a54756cbSKent Gibson * or output, else the line will be treated "as is". 1336a54756cbSKent Gibson */ 1337a54756cbSKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 1338a54756cbSKent Gibson int val = gpio_v2_line_config_output_value(lc, i); 1339a54756cbSKent Gibson 1340b1a92e94SKent Gibson edge_detector_stop(line); 1341a54756cbSKent Gibson ret = gpiod_direction_output(desc, val); 1342a54756cbSKent Gibson if (ret) 1343a54756cbSKent Gibson return ret; 1344a54756cbSKent Gibson } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { 1345a54756cbSKent Gibson ret = gpiod_direction_input(desc); 1346a54756cbSKent Gibson if (ret) 1347a54756cbSKent Gibson return ret; 1348a54756cbSKent Gibson 1349b1a92e94SKent Gibson ret = edge_detector_update(line, lc, i, edflags); 1350a54756cbSKent Gibson if (ret) 1351a54756cbSKent Gibson return ret; 1352a54756cbSKent Gibson } 1353a54756cbSKent Gibson 1354b1a92e94SKent Gibson WRITE_ONCE(line->edflags, edflags); 1355b1a92e94SKent Gibson 1356a54756cbSKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 1357a54756cbSKent Gibson GPIO_V2_LINE_CHANGED_CONFIG, 1358a54756cbSKent Gibson desc); 1359a54756cbSKent Gibson } 1360a54756cbSKent Gibson return 0; 1361a54756cbSKent Gibson } 1362a54756cbSKent Gibson 1363a54756cbSKent Gibson static long linereq_set_config(struct linereq *lr, void __user *ip) 1364a54756cbSKent Gibson { 1365a54756cbSKent Gibson struct gpio_v2_line_config lc; 1366a54756cbSKent Gibson int ret; 1367a54756cbSKent Gibson 1368a54756cbSKent Gibson if (copy_from_user(&lc, ip, sizeof(lc))) 1369a54756cbSKent Gibson return -EFAULT; 1370a54756cbSKent Gibson 1371a54756cbSKent Gibson ret = gpio_v2_line_config_validate(&lc, lr->num_lines); 1372a54756cbSKent Gibson if (ret) 1373a54756cbSKent Gibson return ret; 1374a54756cbSKent Gibson 1375a54756cbSKent Gibson mutex_lock(&lr->config_mutex); 1376a54756cbSKent Gibson 1377a54756cbSKent Gibson ret = linereq_set_config_unlocked(lr, &lc); 1378a54756cbSKent Gibson 1379a54756cbSKent Gibson mutex_unlock(&lr->config_mutex); 1380a54756cbSKent Gibson 1381a54756cbSKent Gibson return ret; 1382a54756cbSKent Gibson } 1383a54756cbSKent Gibson 13843c0d9c63SKent Gibson static long linereq_ioctl(struct file *file, unsigned int cmd, 13853c0d9c63SKent Gibson unsigned long arg) 13863c0d9c63SKent Gibson { 13873c0d9c63SKent Gibson struct linereq *lr = file->private_data; 13883c0d9c63SKent Gibson void __user *ip = (void __user *)arg; 13893c0d9c63SKent Gibson 1390*533aae7cSBartosz Golaszewski if (!lr->gdev->chip) 1391*533aae7cSBartosz Golaszewski return -ENODEV; 1392*533aae7cSBartosz Golaszewski 13931cef8b50SAndy Shevchenko switch (cmd) { 13941cef8b50SAndy Shevchenko case GPIO_V2_LINE_GET_VALUES_IOCTL: 13953c0d9c63SKent Gibson return linereq_get_values(lr, ip); 13961cef8b50SAndy Shevchenko case GPIO_V2_LINE_SET_VALUES_IOCTL: 13977b8e00d9SKent Gibson return linereq_set_values(lr, ip); 13981cef8b50SAndy Shevchenko case GPIO_V2_LINE_SET_CONFIG_IOCTL: 1399a54756cbSKent Gibson return linereq_set_config(lr, ip); 14001cef8b50SAndy Shevchenko default: 14013c0d9c63SKent Gibson return -EINVAL; 14023c0d9c63SKent Gibson } 14031cef8b50SAndy Shevchenko } 14043c0d9c63SKent Gibson 14053c0d9c63SKent Gibson #ifdef CONFIG_COMPAT 14063c0d9c63SKent Gibson static long linereq_ioctl_compat(struct file *file, unsigned int cmd, 14073c0d9c63SKent Gibson unsigned long arg) 14083c0d9c63SKent Gibson { 14093c0d9c63SKent Gibson return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 14103c0d9c63SKent Gibson } 14113c0d9c63SKent Gibson #endif 14123c0d9c63SKent Gibson 141373e03419SKent Gibson static __poll_t linereq_poll(struct file *file, 141473e03419SKent Gibson struct poll_table_struct *wait) 141573e03419SKent Gibson { 141673e03419SKent Gibson struct linereq *lr = file->private_data; 141773e03419SKent Gibson __poll_t events = 0; 141873e03419SKent Gibson 1419*533aae7cSBartosz Golaszewski if (!lr->gdev->chip) 1420*533aae7cSBartosz Golaszewski return EPOLLHUP | EPOLLERR; 1421*533aae7cSBartosz Golaszewski 142273e03419SKent Gibson poll_wait(file, &lr->wait, wait); 142373e03419SKent Gibson 142473e03419SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events, 142573e03419SKent Gibson &lr->wait.lock)) 142673e03419SKent Gibson events = EPOLLIN | EPOLLRDNORM; 142773e03419SKent Gibson 142873e03419SKent Gibson return events; 142973e03419SKent Gibson } 143073e03419SKent Gibson 143173e03419SKent Gibson static ssize_t linereq_read(struct file *file, 143273e03419SKent Gibson char __user *buf, 143373e03419SKent Gibson size_t count, 143473e03419SKent Gibson loff_t *f_ps) 143573e03419SKent Gibson { 143673e03419SKent Gibson struct linereq *lr = file->private_data; 143773e03419SKent Gibson struct gpio_v2_line_event le; 143873e03419SKent Gibson ssize_t bytes_read = 0; 143973e03419SKent Gibson int ret; 144073e03419SKent Gibson 1441*533aae7cSBartosz Golaszewski if (!lr->gdev->chip) 1442*533aae7cSBartosz Golaszewski return -ENODEV; 1443*533aae7cSBartosz Golaszewski 144473e03419SKent Gibson if (count < sizeof(le)) 144573e03419SKent Gibson return -EINVAL; 144673e03419SKent Gibson 144773e03419SKent Gibson do { 144873e03419SKent Gibson spin_lock(&lr->wait.lock); 144973e03419SKent Gibson if (kfifo_is_empty(&lr->events)) { 145073e03419SKent Gibson if (bytes_read) { 145173e03419SKent Gibson spin_unlock(&lr->wait.lock); 145273e03419SKent Gibson return bytes_read; 145373e03419SKent Gibson } 145473e03419SKent Gibson 145573e03419SKent Gibson if (file->f_flags & O_NONBLOCK) { 145673e03419SKent Gibson spin_unlock(&lr->wait.lock); 145773e03419SKent Gibson return -EAGAIN; 145873e03419SKent Gibson } 145973e03419SKent Gibson 146073e03419SKent Gibson ret = wait_event_interruptible_locked(lr->wait, 146173e03419SKent Gibson !kfifo_is_empty(&lr->events)); 146273e03419SKent Gibson if (ret) { 146373e03419SKent Gibson spin_unlock(&lr->wait.lock); 146473e03419SKent Gibson return ret; 146573e03419SKent Gibson } 146673e03419SKent Gibson } 146773e03419SKent Gibson 146873e03419SKent Gibson ret = kfifo_out(&lr->events, &le, 1); 146973e03419SKent Gibson spin_unlock(&lr->wait.lock); 147073e03419SKent Gibson if (ret != 1) { 147173e03419SKent Gibson /* 147273e03419SKent Gibson * This should never happen - we were holding the 147373e03419SKent Gibson * lock from the moment we learned the fifo is no 147473e03419SKent Gibson * longer empty until now. 147573e03419SKent Gibson */ 147673e03419SKent Gibson ret = -EIO; 147773e03419SKent Gibson break; 147873e03419SKent Gibson } 147973e03419SKent Gibson 148073e03419SKent Gibson if (copy_to_user(buf + bytes_read, &le, sizeof(le))) 148173e03419SKent Gibson return -EFAULT; 148273e03419SKent Gibson bytes_read += sizeof(le); 148373e03419SKent Gibson } while (count >= bytes_read + sizeof(le)); 148473e03419SKent Gibson 148573e03419SKent Gibson return bytes_read; 148673e03419SKent Gibson } 148773e03419SKent Gibson 14883c0d9c63SKent Gibson static void linereq_free(struct linereq *lr) 14893c0d9c63SKent Gibson { 14903c0d9c63SKent Gibson unsigned int i; 14913c0d9c63SKent Gibson 14923c0d9c63SKent Gibson for (i = 0; i < lr->num_lines; i++) { 1493160d6e40SKent Gibson if (lr->lines[i].desc) { 1494b1a92e94SKent Gibson edge_detector_stop(&lr->lines[i]); 14953c0d9c63SKent Gibson gpiod_free(lr->lines[i].desc); 14963c0d9c63SKent Gibson } 1497160d6e40SKent Gibson } 149873e03419SKent Gibson kfifo_free(&lr->events); 14993c0d9c63SKent Gibson kfree(lr->label); 15003c0d9c63SKent Gibson put_device(&lr->gdev->dev); 15013c0d9c63SKent Gibson kfree(lr); 15023c0d9c63SKent Gibson } 15033c0d9c63SKent Gibson 15043c0d9c63SKent Gibson static int linereq_release(struct inode *inode, struct file *file) 15053c0d9c63SKent Gibson { 15063c0d9c63SKent Gibson struct linereq *lr = file->private_data; 15073c0d9c63SKent Gibson 15083c0d9c63SKent Gibson linereq_free(lr); 15093c0d9c63SKent Gibson return 0; 15103c0d9c63SKent Gibson } 15113c0d9c63SKent Gibson 15120ae3109aSBartosz Golaszewski #ifdef CONFIG_PROC_FS 15130ae3109aSBartosz Golaszewski static void linereq_show_fdinfo(struct seq_file *out, struct file *file) 15140ae3109aSBartosz Golaszewski { 15150ae3109aSBartosz Golaszewski struct linereq *lr = file->private_data; 15160ae3109aSBartosz Golaszewski struct device *dev = &lr->gdev->dev; 15170ae3109aSBartosz Golaszewski u16 i; 15180ae3109aSBartosz Golaszewski 15190ae3109aSBartosz Golaszewski seq_printf(out, "gpio-chip:\t%s\n", dev_name(dev)); 15200ae3109aSBartosz Golaszewski 15210ae3109aSBartosz Golaszewski for (i = 0; i < lr->num_lines; i++) 15220ae3109aSBartosz Golaszewski seq_printf(out, "gpio-line:\t%d\n", 15230ae3109aSBartosz Golaszewski gpio_chip_hwgpio(lr->lines[i].desc)); 15240ae3109aSBartosz Golaszewski } 15250ae3109aSBartosz Golaszewski #endif 15260ae3109aSBartosz Golaszewski 15273c0d9c63SKent Gibson static const struct file_operations line_fileops = { 15283c0d9c63SKent Gibson .release = linereq_release, 152973e03419SKent Gibson .read = linereq_read, 153073e03419SKent Gibson .poll = linereq_poll, 15313c0d9c63SKent Gibson .owner = THIS_MODULE, 15323c0d9c63SKent Gibson .llseek = noop_llseek, 15333c0d9c63SKent Gibson .unlocked_ioctl = linereq_ioctl, 15343c0d9c63SKent Gibson #ifdef CONFIG_COMPAT 15353c0d9c63SKent Gibson .compat_ioctl = linereq_ioctl_compat, 15363c0d9c63SKent Gibson #endif 15370ae3109aSBartosz Golaszewski #ifdef CONFIG_PROC_FS 15380ae3109aSBartosz Golaszewski .show_fdinfo = linereq_show_fdinfo, 15390ae3109aSBartosz Golaszewski #endif 15403c0d9c63SKent Gibson }; 15413c0d9c63SKent Gibson 15423c0d9c63SKent Gibson static int linereq_create(struct gpio_device *gdev, void __user *ip) 15433c0d9c63SKent Gibson { 15443c0d9c63SKent Gibson struct gpio_v2_line_request ulr; 15453c0d9c63SKent Gibson struct gpio_v2_line_config *lc; 15463c0d9c63SKent Gibson struct linereq *lr; 15473c0d9c63SKent Gibson struct file *file; 1548b1a92e94SKent Gibson u64 flags, edflags; 15493c0d9c63SKent Gibson unsigned int i; 15503c0d9c63SKent Gibson int fd, ret; 15513c0d9c63SKent Gibson 15523c0d9c63SKent Gibson if (copy_from_user(&ulr, ip, sizeof(ulr))) 15533c0d9c63SKent Gibson return -EFAULT; 15543c0d9c63SKent Gibson 15553c0d9c63SKent Gibson if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX)) 15563c0d9c63SKent Gibson return -EINVAL; 15573c0d9c63SKent Gibson 15583c0d9c63SKent Gibson if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding))) 15593c0d9c63SKent Gibson return -EINVAL; 15603c0d9c63SKent Gibson 15613c0d9c63SKent Gibson lc = &ulr.config; 15623c0d9c63SKent Gibson ret = gpio_v2_line_config_validate(lc, ulr.num_lines); 15633c0d9c63SKent Gibson if (ret) 15643c0d9c63SKent Gibson return ret; 15653c0d9c63SKent Gibson 15663c0d9c63SKent Gibson lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); 15673c0d9c63SKent Gibson if (!lr) 15683c0d9c63SKent Gibson return -ENOMEM; 15693c0d9c63SKent Gibson 15703c0d9c63SKent Gibson lr->gdev = gdev; 15713c0d9c63SKent Gibson get_device(&gdev->dev); 15723c0d9c63SKent Gibson 157365cff704SKent Gibson for (i = 0; i < ulr.num_lines; i++) { 157473e03419SKent Gibson lr->lines[i].req = lr; 157565cff704SKent Gibson WRITE_ONCE(lr->lines[i].sw_debounced, 0); 157665cff704SKent Gibson INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func); 157765cff704SKent Gibson } 157873e03419SKent Gibson 1579f188ac12SKent Gibson if (ulr.consumer[0] != '\0') { 15803c0d9c63SKent Gibson /* label is only initialized if consumer is set */ 1581f188ac12SKent Gibson lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1, 1582f188ac12SKent Gibson GFP_KERNEL); 15833c0d9c63SKent Gibson if (!lr->label) { 15843c0d9c63SKent Gibson ret = -ENOMEM; 15853c0d9c63SKent Gibson goto out_free_linereq; 15863c0d9c63SKent Gibson } 15873c0d9c63SKent Gibson } 15883c0d9c63SKent Gibson 1589a54756cbSKent Gibson mutex_init(&lr->config_mutex); 159073e03419SKent Gibson init_waitqueue_head(&lr->wait); 159173e03419SKent Gibson lr->event_buffer_size = ulr.event_buffer_size; 159273e03419SKent Gibson if (lr->event_buffer_size == 0) 159373e03419SKent Gibson lr->event_buffer_size = ulr.num_lines * 16; 159473e03419SKent Gibson else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16) 159573e03419SKent Gibson lr->event_buffer_size = GPIO_V2_LINES_MAX * 16; 159673e03419SKent Gibson 159773e03419SKent Gibson atomic_set(&lr->seqno, 0); 15983c0d9c63SKent Gibson lr->num_lines = ulr.num_lines; 15993c0d9c63SKent Gibson 16003c0d9c63SKent Gibson /* Request each GPIO */ 16013c0d9c63SKent Gibson for (i = 0; i < ulr.num_lines; i++) { 16023c0d9c63SKent Gibson u32 offset = ulr.offsets[i]; 16033c0d9c63SKent Gibson struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); 16043c0d9c63SKent Gibson 16053c0d9c63SKent Gibson if (IS_ERR(desc)) { 16063c0d9c63SKent Gibson ret = PTR_ERR(desc); 16073c0d9c63SKent Gibson goto out_free_linereq; 16083c0d9c63SKent Gibson } 16093c0d9c63SKent Gibson 161095a4eed7SAndy Shevchenko ret = gpiod_request_user(desc, lr->label); 16113c0d9c63SKent Gibson if (ret) 16123c0d9c63SKent Gibson goto out_free_linereq; 16133c0d9c63SKent Gibson 16143c0d9c63SKent Gibson lr->lines[i].desc = desc; 16153c0d9c63SKent Gibson flags = gpio_v2_line_config_flags(lc, i); 16163c0d9c63SKent Gibson gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 16173c0d9c63SKent Gibson 16183c0d9c63SKent Gibson ret = gpiod_set_transitory(desc, false); 16193c0d9c63SKent Gibson if (ret < 0) 16203c0d9c63SKent Gibson goto out_free_linereq; 16213c0d9c63SKent Gibson 1622b1a92e94SKent Gibson edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS; 16233c0d9c63SKent Gibson /* 16243c0d9c63SKent Gibson * Lines have to be requested explicitly for input 16253c0d9c63SKent Gibson * or output, else the line will be treated "as is". 16263c0d9c63SKent Gibson */ 16273c0d9c63SKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 16283c0d9c63SKent Gibson int val = gpio_v2_line_config_output_value(lc, i); 16293c0d9c63SKent Gibson 16303c0d9c63SKent Gibson ret = gpiod_direction_output(desc, val); 16313c0d9c63SKent Gibson if (ret) 16323c0d9c63SKent Gibson goto out_free_linereq; 16333c0d9c63SKent Gibson } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { 16343c0d9c63SKent Gibson ret = gpiod_direction_input(desc); 16353c0d9c63SKent Gibson if (ret) 16363c0d9c63SKent Gibson goto out_free_linereq; 163773e03419SKent Gibson 163865cff704SKent Gibson ret = edge_detector_setup(&lr->lines[i], lc, i, 1639b1a92e94SKent Gibson edflags); 164073e03419SKent Gibson if (ret) 164173e03419SKent Gibson goto out_free_linereq; 16423c0d9c63SKent Gibson } 16433c0d9c63SKent Gibson 1644b1a92e94SKent Gibson lr->lines[i].edflags = edflags; 1645b1a92e94SKent Gibson 16463c0d9c63SKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 1647aad95584SKent Gibson GPIO_V2_LINE_CHANGED_REQUESTED, desc); 16483c0d9c63SKent Gibson 16493c0d9c63SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", 16503c0d9c63SKent Gibson offset); 16513c0d9c63SKent Gibson } 16523c0d9c63SKent Gibson 16533c0d9c63SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 16543c0d9c63SKent Gibson if (fd < 0) { 16553c0d9c63SKent Gibson ret = fd; 16563c0d9c63SKent Gibson goto out_free_linereq; 16573c0d9c63SKent Gibson } 16583c0d9c63SKent Gibson 16593c0d9c63SKent Gibson file = anon_inode_getfile("gpio-line", &line_fileops, lr, 16603c0d9c63SKent Gibson O_RDONLY | O_CLOEXEC); 16613c0d9c63SKent Gibson if (IS_ERR(file)) { 16623c0d9c63SKent Gibson ret = PTR_ERR(file); 16633c0d9c63SKent Gibson goto out_put_unused_fd; 16643c0d9c63SKent Gibson } 16653c0d9c63SKent Gibson 16663c0d9c63SKent Gibson ulr.fd = fd; 16673c0d9c63SKent Gibson if (copy_to_user(ip, &ulr, sizeof(ulr))) { 16683c0d9c63SKent Gibson /* 16693c0d9c63SKent Gibson * fput() will trigger the release() callback, so do not go onto 16703c0d9c63SKent Gibson * the regular error cleanup path here. 16713c0d9c63SKent Gibson */ 16723c0d9c63SKent Gibson fput(file); 16733c0d9c63SKent Gibson put_unused_fd(fd); 16743c0d9c63SKent Gibson return -EFAULT; 16753c0d9c63SKent Gibson } 16763c0d9c63SKent Gibson 16773c0d9c63SKent Gibson fd_install(fd, file); 16783c0d9c63SKent Gibson 16793c0d9c63SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", 16803c0d9c63SKent Gibson lr->num_lines); 16813c0d9c63SKent Gibson 16823c0d9c63SKent Gibson return 0; 16833c0d9c63SKent Gibson 16843c0d9c63SKent Gibson out_put_unused_fd: 16853c0d9c63SKent Gibson put_unused_fd(fd); 16863c0d9c63SKent Gibson out_free_linereq: 16873c0d9c63SKent Gibson linereq_free(lr); 16883c0d9c63SKent Gibson return ret; 16893c0d9c63SKent Gibson } 16903c0d9c63SKent Gibson 16913c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 1692925ca369SKent Gibson 1693925ca369SKent Gibson /* 1694925ca369SKent Gibson * GPIO line event management 1695925ca369SKent Gibson */ 1696925ca369SKent Gibson 1697925ca369SKent Gibson /** 1698925ca369SKent Gibson * struct lineevent_state - contains the state of a userspace event 1699925ca369SKent Gibson * @gdev: the GPIO device the event pertains to 1700925ca369SKent Gibson * @label: consumer label used to tag descriptors 1701925ca369SKent Gibson * @desc: the GPIO descriptor held by this event 1702925ca369SKent Gibson * @eflags: the event flags this line was requested with 1703925ca369SKent Gibson * @irq: the interrupt that trigger in response to events on this GPIO 1704925ca369SKent Gibson * @wait: wait queue that handles blocking reads of events 1705925ca369SKent Gibson * @events: KFIFO for the GPIO events 1706925ca369SKent Gibson * @timestamp: cache for the timestamp storing it between hardirq 1707925ca369SKent Gibson * and IRQ thread, used to bring the timestamp close to the actual 1708925ca369SKent Gibson * event 1709925ca369SKent Gibson */ 1710925ca369SKent Gibson struct lineevent_state { 1711925ca369SKent Gibson struct gpio_device *gdev; 1712925ca369SKent Gibson const char *label; 1713925ca369SKent Gibson struct gpio_desc *desc; 1714925ca369SKent Gibson u32 eflags; 1715925ca369SKent Gibson int irq; 1716925ca369SKent Gibson wait_queue_head_t wait; 1717925ca369SKent Gibson DECLARE_KFIFO(events, struct gpioevent_data, 16); 1718925ca369SKent Gibson u64 timestamp; 1719925ca369SKent Gibson }; 1720925ca369SKent Gibson 1721925ca369SKent Gibson #define GPIOEVENT_REQUEST_VALID_FLAGS \ 1722925ca369SKent Gibson (GPIOEVENT_REQUEST_RISING_EDGE | \ 1723925ca369SKent Gibson GPIOEVENT_REQUEST_FALLING_EDGE) 1724925ca369SKent Gibson 172549bc5279SKent Gibson static __poll_t lineevent_poll(struct file *file, 1726925ca369SKent Gibson struct poll_table_struct *wait) 1727925ca369SKent Gibson { 172849bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1729925ca369SKent Gibson __poll_t events = 0; 1730925ca369SKent Gibson 1731*533aae7cSBartosz Golaszewski if (!le->gdev->chip) 1732*533aae7cSBartosz Golaszewski return EPOLLHUP | EPOLLERR; 1733*533aae7cSBartosz Golaszewski 173449bc5279SKent Gibson poll_wait(file, &le->wait, wait); 1735925ca369SKent Gibson 1736925ca369SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock)) 1737925ca369SKent Gibson events = EPOLLIN | EPOLLRDNORM; 1738925ca369SKent Gibson 1739925ca369SKent Gibson return events; 1740925ca369SKent Gibson } 1741925ca369SKent Gibson 17425ad284abSAndy Shevchenko struct compat_gpioeevent_data { 17435ad284abSAndy Shevchenko compat_u64 timestamp; 17445ad284abSAndy Shevchenko u32 id; 17455ad284abSAndy Shevchenko }; 17465ad284abSAndy Shevchenko 174749bc5279SKent Gibson static ssize_t lineevent_read(struct file *file, 1748925ca369SKent Gibson char __user *buf, 1749925ca369SKent Gibson size_t count, 1750925ca369SKent Gibson loff_t *f_ps) 1751925ca369SKent Gibson { 175249bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1753925ca369SKent Gibson struct gpioevent_data ge; 1754925ca369SKent Gibson ssize_t bytes_read = 0; 17555ad284abSAndy Shevchenko ssize_t ge_size; 1756925ca369SKent Gibson int ret; 1757925ca369SKent Gibson 1758*533aae7cSBartosz Golaszewski if (!le->gdev->chip) 1759*533aae7cSBartosz Golaszewski return -ENODEV; 1760*533aae7cSBartosz Golaszewski 17615ad284abSAndy Shevchenko /* 17625ad284abSAndy Shevchenko * When compatible system call is being used the struct gpioevent_data, 17635ad284abSAndy Shevchenko * in case of at least ia32, has different size due to the alignment 17645ad284abSAndy Shevchenko * differences. Because we have first member 64 bits followed by one of 17655ad284abSAndy Shevchenko * 32 bits there is no gap between them. The only difference is the 17665ad284abSAndy Shevchenko * padding at the end of the data structure. Hence, we calculate the 17675ad284abSAndy Shevchenko * actual sizeof() and pass this as an argument to copy_to_user() to 17685ad284abSAndy Shevchenko * drop unneeded bytes from the output. 17695ad284abSAndy Shevchenko */ 1770163d1719SAndy Shevchenko if (compat_need_64bit_alignment_fixup()) 1771163d1719SAndy Shevchenko ge_size = sizeof(struct compat_gpioeevent_data); 1772163d1719SAndy Shevchenko else 1773163d1719SAndy Shevchenko ge_size = sizeof(struct gpioevent_data); 17745ad284abSAndy Shevchenko if (count < ge_size) 1775925ca369SKent Gibson return -EINVAL; 1776925ca369SKent Gibson 1777925ca369SKent Gibson do { 1778925ca369SKent Gibson spin_lock(&le->wait.lock); 1779925ca369SKent Gibson if (kfifo_is_empty(&le->events)) { 1780925ca369SKent Gibson if (bytes_read) { 1781925ca369SKent Gibson spin_unlock(&le->wait.lock); 1782925ca369SKent Gibson return bytes_read; 1783925ca369SKent Gibson } 1784925ca369SKent Gibson 178549bc5279SKent Gibson if (file->f_flags & O_NONBLOCK) { 1786925ca369SKent Gibson spin_unlock(&le->wait.lock); 1787925ca369SKent Gibson return -EAGAIN; 1788925ca369SKent Gibson } 1789925ca369SKent Gibson 1790925ca369SKent Gibson ret = wait_event_interruptible_locked(le->wait, 1791925ca369SKent Gibson !kfifo_is_empty(&le->events)); 1792925ca369SKent Gibson if (ret) { 1793925ca369SKent Gibson spin_unlock(&le->wait.lock); 1794925ca369SKent Gibson return ret; 1795925ca369SKent Gibson } 1796925ca369SKent Gibson } 1797925ca369SKent Gibson 1798925ca369SKent Gibson ret = kfifo_out(&le->events, &ge, 1); 1799925ca369SKent Gibson spin_unlock(&le->wait.lock); 1800925ca369SKent Gibson if (ret != 1) { 1801925ca369SKent Gibson /* 1802925ca369SKent Gibson * This should never happen - we were holding the lock 1803925ca369SKent Gibson * from the moment we learned the fifo is no longer 1804925ca369SKent Gibson * empty until now. 1805925ca369SKent Gibson */ 1806925ca369SKent Gibson ret = -EIO; 1807925ca369SKent Gibson break; 1808925ca369SKent Gibson } 1809925ca369SKent Gibson 18105ad284abSAndy Shevchenko if (copy_to_user(buf + bytes_read, &ge, ge_size)) 1811925ca369SKent Gibson return -EFAULT; 18125ad284abSAndy Shevchenko bytes_read += ge_size; 18135ad284abSAndy Shevchenko } while (count >= bytes_read + ge_size); 1814925ca369SKent Gibson 1815925ca369SKent Gibson return bytes_read; 1816925ca369SKent Gibson } 1817925ca369SKent Gibson 181846824272SKent Gibson static void lineevent_free(struct lineevent_state *le) 1819925ca369SKent Gibson { 182046824272SKent Gibson if (le->irq) 1821925ca369SKent Gibson free_irq(le->irq, le); 182246824272SKent Gibson if (le->desc) 1823925ca369SKent Gibson gpiod_free(le->desc); 1824925ca369SKent Gibson kfree(le->label); 182546824272SKent Gibson put_device(&le->gdev->dev); 1826925ca369SKent Gibson kfree(le); 182746824272SKent Gibson } 182846824272SKent Gibson 182946824272SKent Gibson static int lineevent_release(struct inode *inode, struct file *file) 183046824272SKent Gibson { 183146824272SKent Gibson lineevent_free(file->private_data); 1832925ca369SKent Gibson return 0; 1833925ca369SKent Gibson } 1834925ca369SKent Gibson 183549bc5279SKent Gibson static long lineevent_ioctl(struct file *file, unsigned int cmd, 1836925ca369SKent Gibson unsigned long arg) 1837925ca369SKent Gibson { 183849bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1839925ca369SKent Gibson void __user *ip = (void __user *)arg; 1840925ca369SKent Gibson struct gpiohandle_data ghd; 1841925ca369SKent Gibson 1842*533aae7cSBartosz Golaszewski if (!le->gdev->chip) 1843*533aae7cSBartosz Golaszewski return -ENODEV; 1844*533aae7cSBartosz Golaszewski 1845925ca369SKent Gibson /* 1846925ca369SKent Gibson * We can get the value for an event line but not set it, 1847925ca369SKent Gibson * because it is input by definition. 1848925ca369SKent Gibson */ 1849925ca369SKent Gibson if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { 1850925ca369SKent Gibson int val; 1851925ca369SKent Gibson 1852925ca369SKent Gibson memset(&ghd, 0, sizeof(ghd)); 1853925ca369SKent Gibson 1854925ca369SKent Gibson val = gpiod_get_value_cansleep(le->desc); 1855925ca369SKent Gibson if (val < 0) 1856925ca369SKent Gibson return val; 1857925ca369SKent Gibson ghd.values[0] = val; 1858925ca369SKent Gibson 1859925ca369SKent Gibson if (copy_to_user(ip, &ghd, sizeof(ghd))) 1860925ca369SKent Gibson return -EFAULT; 1861925ca369SKent Gibson 1862925ca369SKent Gibson return 0; 1863925ca369SKent Gibson } 1864925ca369SKent Gibson return -EINVAL; 1865925ca369SKent Gibson } 1866925ca369SKent Gibson 1867925ca369SKent Gibson #ifdef CONFIG_COMPAT 186849bc5279SKent Gibson static long lineevent_ioctl_compat(struct file *file, unsigned int cmd, 1869925ca369SKent Gibson unsigned long arg) 1870925ca369SKent Gibson { 187149bc5279SKent Gibson return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 1872925ca369SKent Gibson } 1873925ca369SKent Gibson #endif 1874925ca369SKent Gibson 1875925ca369SKent Gibson static const struct file_operations lineevent_fileops = { 1876925ca369SKent Gibson .release = lineevent_release, 1877925ca369SKent Gibson .read = lineevent_read, 1878925ca369SKent Gibson .poll = lineevent_poll, 1879925ca369SKent Gibson .owner = THIS_MODULE, 1880925ca369SKent Gibson .llseek = noop_llseek, 1881925ca369SKent Gibson .unlocked_ioctl = lineevent_ioctl, 1882925ca369SKent Gibson #ifdef CONFIG_COMPAT 1883925ca369SKent Gibson .compat_ioctl = lineevent_ioctl_compat, 1884925ca369SKent Gibson #endif 1885925ca369SKent Gibson }; 1886925ca369SKent Gibson 1887925ca369SKent Gibson static irqreturn_t lineevent_irq_thread(int irq, void *p) 1888925ca369SKent Gibson { 1889925ca369SKent Gibson struct lineevent_state *le = p; 1890925ca369SKent Gibson struct gpioevent_data ge; 1891925ca369SKent Gibson int ret; 1892925ca369SKent Gibson 1893925ca369SKent Gibson /* Do not leak kernel stack to userspace */ 1894925ca369SKent Gibson memset(&ge, 0, sizeof(ge)); 1895925ca369SKent Gibson 1896925ca369SKent Gibson /* 1897925ca369SKent Gibson * We may be running from a nested threaded interrupt in which case 1898925ca369SKent Gibson * we didn't get the timestamp from lineevent_irq_handler(). 1899925ca369SKent Gibson */ 1900925ca369SKent Gibson if (!le->timestamp) 1901925ca369SKent Gibson ge.timestamp = ktime_get_ns(); 1902925ca369SKent Gibson else 1903925ca369SKent Gibson ge.timestamp = le->timestamp; 1904925ca369SKent Gibson 1905925ca369SKent Gibson if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE 1906925ca369SKent Gibson && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { 1907925ca369SKent Gibson int level = gpiod_get_value_cansleep(le->desc); 1908925ca369SKent Gibson 1909925ca369SKent Gibson if (level) 1910925ca369SKent Gibson /* Emit low-to-high event */ 1911925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_RISING_EDGE; 1912925ca369SKent Gibson else 1913925ca369SKent Gibson /* Emit high-to-low event */ 1914925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_FALLING_EDGE; 1915925ca369SKent Gibson } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) { 1916925ca369SKent Gibson /* Emit low-to-high event */ 1917925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_RISING_EDGE; 1918925ca369SKent Gibson } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { 1919925ca369SKent Gibson /* Emit high-to-low event */ 1920925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_FALLING_EDGE; 1921925ca369SKent Gibson } else { 1922925ca369SKent Gibson return IRQ_NONE; 1923925ca369SKent Gibson } 1924925ca369SKent Gibson 1925925ca369SKent Gibson ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge, 1926925ca369SKent Gibson 1, &le->wait.lock); 1927925ca369SKent Gibson if (ret) 1928925ca369SKent Gibson wake_up_poll(&le->wait, EPOLLIN); 1929925ca369SKent Gibson else 1930925ca369SKent Gibson pr_debug_ratelimited("event FIFO is full - event dropped\n"); 1931925ca369SKent Gibson 1932925ca369SKent Gibson return IRQ_HANDLED; 1933925ca369SKent Gibson } 1934925ca369SKent Gibson 1935925ca369SKent Gibson static irqreturn_t lineevent_irq_handler(int irq, void *p) 1936925ca369SKent Gibson { 1937925ca369SKent Gibson struct lineevent_state *le = p; 1938925ca369SKent Gibson 1939925ca369SKent Gibson /* 1940925ca369SKent Gibson * Just store the timestamp in hardirq context so we get it as 1941925ca369SKent Gibson * close in time as possible to the actual event. 1942925ca369SKent Gibson */ 1943925ca369SKent Gibson le->timestamp = ktime_get_ns(); 1944925ca369SKent Gibson 1945925ca369SKent Gibson return IRQ_WAKE_THREAD; 1946925ca369SKent Gibson } 1947925ca369SKent Gibson 1948925ca369SKent Gibson static int lineevent_create(struct gpio_device *gdev, void __user *ip) 1949925ca369SKent Gibson { 1950925ca369SKent Gibson struct gpioevent_request eventreq; 1951925ca369SKent Gibson struct lineevent_state *le; 1952925ca369SKent Gibson struct gpio_desc *desc; 1953925ca369SKent Gibson struct file *file; 1954925ca369SKent Gibson u32 offset; 1955925ca369SKent Gibson u32 lflags; 1956925ca369SKent Gibson u32 eflags; 1957925ca369SKent Gibson int fd; 1958925ca369SKent Gibson int ret; 195946824272SKent Gibson int irq, irqflags = 0; 1960925ca369SKent Gibson 1961925ca369SKent Gibson if (copy_from_user(&eventreq, ip, sizeof(eventreq))) 1962925ca369SKent Gibson return -EFAULT; 1963925ca369SKent Gibson 1964925ca369SKent Gibson offset = eventreq.lineoffset; 1965925ca369SKent Gibson lflags = eventreq.handleflags; 1966925ca369SKent Gibson eflags = eventreq.eventflags; 1967925ca369SKent Gibson 1968925ca369SKent Gibson desc = gpiochip_get_desc(gdev->chip, offset); 1969925ca369SKent Gibson if (IS_ERR(desc)) 1970925ca369SKent Gibson return PTR_ERR(desc); 1971925ca369SKent Gibson 1972925ca369SKent Gibson /* Return an error if a unknown flag is set */ 1973925ca369SKent Gibson if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || 1974925ca369SKent Gibson (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) 1975925ca369SKent Gibson return -EINVAL; 1976925ca369SKent Gibson 1977925ca369SKent Gibson /* This is just wrong: we don't look for events on output lines */ 1978925ca369SKent Gibson if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) || 1979925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || 1980925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) 1981925ca369SKent Gibson return -EINVAL; 1982925ca369SKent Gibson 1983925ca369SKent Gibson /* Only one bias flag can be set. */ 1984925ca369SKent Gibson if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && 1985925ca369SKent Gibson (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | 1986925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || 1987925ca369SKent Gibson ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && 1988925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) 1989925ca369SKent Gibson return -EINVAL; 1990925ca369SKent Gibson 1991925ca369SKent Gibson le = kzalloc(sizeof(*le), GFP_KERNEL); 1992925ca369SKent Gibson if (!le) 1993925ca369SKent Gibson return -ENOMEM; 1994925ca369SKent Gibson le->gdev = gdev; 1995925ca369SKent Gibson get_device(&gdev->dev); 1996925ca369SKent Gibson 1997f188ac12SKent Gibson if (eventreq.consumer_label[0] != '\0') { 1998f188ac12SKent Gibson /* label is only initialized if consumer_label is set */ 1999f188ac12SKent Gibson le->label = kstrndup(eventreq.consumer_label, 2000f188ac12SKent Gibson sizeof(eventreq.consumer_label) - 1, 2001925ca369SKent Gibson GFP_KERNEL); 2002925ca369SKent Gibson if (!le->label) { 2003925ca369SKent Gibson ret = -ENOMEM; 2004925ca369SKent Gibson goto out_free_le; 2005925ca369SKent Gibson } 2006925ca369SKent Gibson } 2007925ca369SKent Gibson 200895a4eed7SAndy Shevchenko ret = gpiod_request_user(desc, le->label); 2009925ca369SKent Gibson if (ret) 201046824272SKent Gibson goto out_free_le; 2011925ca369SKent Gibson le->desc = desc; 2012925ca369SKent Gibson le->eflags = eflags; 2013925ca369SKent Gibson 2014c274b58aSKent Gibson linehandle_flags_to_desc_flags(lflags, &desc->flags); 2015925ca369SKent Gibson 2016925ca369SKent Gibson ret = gpiod_direction_input(desc); 2017925ca369SKent Gibson if (ret) 201846824272SKent Gibson goto out_free_le; 2019925ca369SKent Gibson 20206accc376SKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 2021aad95584SKent Gibson GPIO_V2_LINE_CHANGED_REQUESTED, desc); 2022925ca369SKent Gibson 202346824272SKent Gibson irq = gpiod_to_irq(desc); 202446824272SKent Gibson if (irq <= 0) { 2025925ca369SKent Gibson ret = -ENODEV; 202646824272SKent Gibson goto out_free_le; 2027925ca369SKent Gibson } 2028925ca369SKent Gibson 2029925ca369SKent Gibson if (eflags & GPIOEVENT_REQUEST_RISING_EDGE) 2030925ca369SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 2031925ca369SKent Gibson IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 2032925ca369SKent Gibson if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE) 2033925ca369SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 2034925ca369SKent Gibson IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 2035925ca369SKent Gibson irqflags |= IRQF_ONESHOT; 2036925ca369SKent Gibson 2037925ca369SKent Gibson INIT_KFIFO(le->events); 2038925ca369SKent Gibson init_waitqueue_head(&le->wait); 2039925ca369SKent Gibson 2040925ca369SKent Gibson /* Request a thread to read the events */ 204169bef19dSMeng Li ret = request_threaded_irq(irq, 2042925ca369SKent Gibson lineevent_irq_handler, 2043925ca369SKent Gibson lineevent_irq_thread, 2044925ca369SKent Gibson irqflags, 2045925ca369SKent Gibson le->label, 2046925ca369SKent Gibson le); 2047925ca369SKent Gibson if (ret) 204846824272SKent Gibson goto out_free_le; 2049925ca369SKent Gibson 205069bef19dSMeng Li le->irq = irq; 205169bef19dSMeng Li 2052925ca369SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 2053925ca369SKent Gibson if (fd < 0) { 2054925ca369SKent Gibson ret = fd; 205546824272SKent Gibson goto out_free_le; 2056925ca369SKent Gibson } 2057925ca369SKent Gibson 2058925ca369SKent Gibson file = anon_inode_getfile("gpio-event", 2059925ca369SKent Gibson &lineevent_fileops, 2060925ca369SKent Gibson le, 2061925ca369SKent Gibson O_RDONLY | O_CLOEXEC); 2062925ca369SKent Gibson if (IS_ERR(file)) { 2063925ca369SKent Gibson ret = PTR_ERR(file); 2064925ca369SKent Gibson goto out_put_unused_fd; 2065925ca369SKent Gibson } 2066925ca369SKent Gibson 2067925ca369SKent Gibson eventreq.fd = fd; 2068925ca369SKent Gibson if (copy_to_user(ip, &eventreq, sizeof(eventreq))) { 2069925ca369SKent Gibson /* 2070925ca369SKent Gibson * fput() will trigger the release() callback, so do not go onto 2071925ca369SKent Gibson * the regular error cleanup path here. 2072925ca369SKent Gibson */ 2073925ca369SKent Gibson fput(file); 2074925ca369SKent Gibson put_unused_fd(fd); 2075925ca369SKent Gibson return -EFAULT; 2076925ca369SKent Gibson } 2077925ca369SKent Gibson 2078925ca369SKent Gibson fd_install(fd, file); 2079925ca369SKent Gibson 2080925ca369SKent Gibson return 0; 2081925ca369SKent Gibson 2082925ca369SKent Gibson out_put_unused_fd: 2083925ca369SKent Gibson put_unused_fd(fd); 2084925ca369SKent Gibson out_free_le: 208546824272SKent Gibson lineevent_free(le); 2086925ca369SKent Gibson return ret; 2087925ca369SKent Gibson } 2088925ca369SKent Gibson 2089aad95584SKent Gibson static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2, 2090aad95584SKent Gibson struct gpioline_info *info_v1) 2091aad95584SKent Gibson { 2092aad95584SKent Gibson u64 flagsv2 = info_v2->flags; 2093aad95584SKent Gibson 2094aad95584SKent Gibson memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name)); 2095aad95584SKent Gibson memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer)); 2096aad95584SKent Gibson info_v1->line_offset = info_v2->offset; 2097aad95584SKent Gibson info_v1->flags = 0; 2098aad95584SKent Gibson 2099aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_USED) 2100aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_KERNEL; 2101aad95584SKent Gibson 2102aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT) 2103aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_IS_OUT; 2104aad95584SKent Gibson 2105aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 2106aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW; 2107aad95584SKent Gibson 2108aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN) 2109aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN; 2110aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE) 2111aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE; 2112aad95584SKent Gibson 2113aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP) 2114aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; 2115aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) 2116aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; 2117aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED) 2118aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE; 2119aad95584SKent Gibson } 2120aad95584SKent Gibson 2121aad95584SKent Gibson static void gpio_v2_line_info_changed_to_v1( 2122aad95584SKent Gibson struct gpio_v2_line_info_changed *lic_v2, 2123aad95584SKent Gibson struct gpioline_info_changed *lic_v1) 2124aad95584SKent Gibson { 2125cb8f63b8SGabriel Knezek memset(lic_v1, 0, sizeof(*lic_v1)); 2126aad95584SKent Gibson gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info); 2127aad95584SKent Gibson lic_v1->timestamp = lic_v2->timestamp_ns; 2128aad95584SKent Gibson lic_v1->event_type = lic_v2->event_type; 2129aad95584SKent Gibson } 2130aad95584SKent Gibson 21313c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 21323c0d9c63SKent Gibson 2133925ca369SKent Gibson static void gpio_desc_to_lineinfo(struct gpio_desc *desc, 2134aad95584SKent Gibson struct gpio_v2_line_info *info) 2135925ca369SKent Gibson { 2136925ca369SKent Gibson struct gpio_chip *gc = desc->gdev->chip; 2137925ca369SKent Gibson bool ok_for_pinctrl; 2138925ca369SKent Gibson unsigned long flags; 213965cff704SKent Gibson u32 debounce_period_us; 214065cff704SKent Gibson unsigned int num_attrs = 0; 2141925ca369SKent Gibson 214269e4e136SKent Gibson memset(info, 0, sizeof(*info)); 2143aad95584SKent Gibson info->offset = gpio_chip_hwgpio(desc); 2144925ca369SKent Gibson 2145925ca369SKent Gibson /* 2146925ca369SKent Gibson * This function takes a mutex so we must check this before taking 2147925ca369SKent Gibson * the spinlock. 2148925ca369SKent Gibson * 2149925ca369SKent Gibson * FIXME: find a non-racy way to retrieve this information. Maybe a 2150925ca369SKent Gibson * lock common to both frameworks? 2151925ca369SKent Gibson */ 2152925ca369SKent Gibson ok_for_pinctrl = 2153aad95584SKent Gibson pinctrl_gpio_can_use_line(gc->base + info->offset); 2154925ca369SKent Gibson 2155925ca369SKent Gibson spin_lock_irqsave(&gpio_lock, flags); 2156925ca369SKent Gibson 215769e4e136SKent Gibson if (desc->name) 215869e4e136SKent Gibson strscpy(info->name, desc->name, sizeof(info->name)); 2159925ca369SKent Gibson 216069e4e136SKent Gibson if (desc->label) 216169e4e136SKent Gibson strscpy(info->consumer, desc->label, sizeof(info->consumer)); 2162925ca369SKent Gibson 2163925ca369SKent Gibson /* 2164925ca369SKent Gibson * Userspace only need to know that the kernel is using this GPIO so 2165925ca369SKent Gibson * it can't use it. 2166925ca369SKent Gibson */ 2167925ca369SKent Gibson info->flags = 0; 2168925ca369SKent Gibson if (test_bit(FLAG_REQUESTED, &desc->flags) || 2169925ca369SKent Gibson test_bit(FLAG_IS_HOGGED, &desc->flags) || 2170925ca369SKent Gibson test_bit(FLAG_USED_AS_IRQ, &desc->flags) || 2171925ca369SKent Gibson test_bit(FLAG_EXPORT, &desc->flags) || 2172925ca369SKent Gibson test_bit(FLAG_SYSFS, &desc->flags) || 2173a0db197fSMarc Zyngier !gpiochip_line_is_valid(gc, info->offset) || 2174925ca369SKent Gibson !ok_for_pinctrl) 2175aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_USED; 2176aad95584SKent Gibson 2177925ca369SKent Gibson if (test_bit(FLAG_IS_OUT, &desc->flags)) 2178aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OUTPUT; 2179aad95584SKent Gibson else 2180aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_INPUT; 2181aad95584SKent Gibson 2182925ca369SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 2183aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW; 2184aad95584SKent Gibson 2185925ca369SKent Gibson if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 2186aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; 2187925ca369SKent Gibson if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 2188aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; 2189aad95584SKent Gibson 2190925ca369SKent Gibson if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) 2191aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED; 2192925ca369SKent Gibson if (test_bit(FLAG_PULL_DOWN, &desc->flags)) 2193aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN; 2194925ca369SKent Gibson if (test_bit(FLAG_PULL_UP, &desc->flags)) 2195aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; 2196925ca369SKent Gibson 219773e03419SKent Gibson if (test_bit(FLAG_EDGE_RISING, &desc->flags)) 219873e03419SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; 219973e03419SKent Gibson if (test_bit(FLAG_EDGE_FALLING, &desc->flags)) 220073e03419SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; 220173e03419SKent Gibson 220226d060e4SKent Gibson if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags)) 220326d060e4SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME; 22042068339aSDipen Patel else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags)) 22052068339aSDipen Patel info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE; 220626d060e4SKent Gibson 220765cff704SKent Gibson debounce_period_us = READ_ONCE(desc->debounce_period_us); 220865cff704SKent Gibson if (debounce_period_us) { 220965cff704SKent Gibson info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; 221065cff704SKent Gibson info->attrs[num_attrs].debounce_period_us = debounce_period_us; 221165cff704SKent Gibson num_attrs++; 221265cff704SKent Gibson } 221365cff704SKent Gibson info->num_attrs = num_attrs; 2214925ca369SKent Gibson 2215925ca369SKent Gibson spin_unlock_irqrestore(&gpio_lock, flags); 2216925ca369SKent Gibson } 2217925ca369SKent Gibson 2218925ca369SKent Gibson struct gpio_chardev_data { 2219925ca369SKent Gibson struct gpio_device *gdev; 2220925ca369SKent Gibson wait_queue_head_t wait; 2221aad95584SKent Gibson DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32); 2222925ca369SKent Gibson struct notifier_block lineinfo_changed_nb; 2223925ca369SKent Gibson unsigned long *watched_lines; 2224aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2225aad95584SKent Gibson atomic_t watch_abi_version; 2226aad95584SKent Gibson #endif 2227925ca369SKent Gibson }; 2228925ca369SKent Gibson 22292e202ad8SKent Gibson static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip) 22302e202ad8SKent Gibson { 22312e202ad8SKent Gibson struct gpio_device *gdev = cdev->gdev; 22322e202ad8SKent Gibson struct gpiochip_info chipinfo; 22332e202ad8SKent Gibson 22342e202ad8SKent Gibson memset(&chipinfo, 0, sizeof(chipinfo)); 22352e202ad8SKent Gibson 22362e202ad8SKent Gibson strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name)); 22372e202ad8SKent Gibson strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label)); 22382e202ad8SKent Gibson chipinfo.lines = gdev->ngpio; 22392e202ad8SKent Gibson if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) 22402e202ad8SKent Gibson return -EFAULT; 22412e202ad8SKent Gibson return 0; 22422e202ad8SKent Gibson } 22432e202ad8SKent Gibson 2244aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2245aad95584SKent Gibson /* 2246aad95584SKent Gibson * returns 0 if the versions match, else the previously selected ABI version 2247aad95584SKent Gibson */ 2248aad95584SKent Gibson static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata, 2249aad95584SKent Gibson unsigned int version) 2250aad95584SKent Gibson { 2251aad95584SKent Gibson int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version); 2252aad95584SKent Gibson 2253aad95584SKent Gibson if (abiv == version) 2254aad95584SKent Gibson return 0; 2255aad95584SKent Gibson 2256aad95584SKent Gibson return abiv; 2257aad95584SKent Gibson } 22582e202ad8SKent Gibson 22592e202ad8SKent Gibson static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip, 22602e202ad8SKent Gibson bool watch) 22612e202ad8SKent Gibson { 22622e202ad8SKent Gibson struct gpio_desc *desc; 22632e202ad8SKent Gibson struct gpioline_info lineinfo; 22642e202ad8SKent Gibson struct gpio_v2_line_info lineinfo_v2; 22652e202ad8SKent Gibson 22662e202ad8SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 22672e202ad8SKent Gibson return -EFAULT; 22682e202ad8SKent Gibson 22692e202ad8SKent Gibson /* this doubles as a range check on line_offset */ 22702e202ad8SKent Gibson desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset); 22712e202ad8SKent Gibson if (IS_ERR(desc)) 22722e202ad8SKent Gibson return PTR_ERR(desc); 22732e202ad8SKent Gibson 22742e202ad8SKent Gibson if (watch) { 22752e202ad8SKent Gibson if (lineinfo_ensure_abi_version(cdev, 1)) 22762e202ad8SKent Gibson return -EPERM; 22772e202ad8SKent Gibson 22782e202ad8SKent Gibson if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines)) 22792e202ad8SKent Gibson return -EBUSY; 22802e202ad8SKent Gibson } 22812e202ad8SKent Gibson 22822e202ad8SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo_v2); 22832e202ad8SKent Gibson gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); 22842e202ad8SKent Gibson 22852e202ad8SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 22862e202ad8SKent Gibson if (watch) 22872e202ad8SKent Gibson clear_bit(lineinfo.line_offset, cdev->watched_lines); 22882e202ad8SKent Gibson return -EFAULT; 22892e202ad8SKent Gibson } 22902e202ad8SKent Gibson 22912e202ad8SKent Gibson return 0; 22922e202ad8SKent Gibson } 2293aad95584SKent Gibson #endif 2294aad95584SKent Gibson 2295aad95584SKent Gibson static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip, 2296aad95584SKent Gibson bool watch) 2297aad95584SKent Gibson { 2298aad95584SKent Gibson struct gpio_desc *desc; 2299aad95584SKent Gibson struct gpio_v2_line_info lineinfo; 2300aad95584SKent Gibson 2301aad95584SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 2302aad95584SKent Gibson return -EFAULT; 2303aad95584SKent Gibson 2304aad95584SKent Gibson if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding))) 2305aad95584SKent Gibson return -EINVAL; 2306aad95584SKent Gibson 2307aad95584SKent Gibson desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset); 2308aad95584SKent Gibson if (IS_ERR(desc)) 2309aad95584SKent Gibson return PTR_ERR(desc); 2310aad95584SKent Gibson 2311aad95584SKent Gibson if (watch) { 2312aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2313aad95584SKent Gibson if (lineinfo_ensure_abi_version(cdev, 2)) 2314aad95584SKent Gibson return -EPERM; 2315aad95584SKent Gibson #endif 2316aad95584SKent Gibson if (test_and_set_bit(lineinfo.offset, cdev->watched_lines)) 2317aad95584SKent Gibson return -EBUSY; 2318aad95584SKent Gibson } 2319aad95584SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo); 2320aad95584SKent Gibson 2321aad95584SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 2322aad95584SKent Gibson if (watch) 2323aad95584SKent Gibson clear_bit(lineinfo.offset, cdev->watched_lines); 2324aad95584SKent Gibson return -EFAULT; 2325aad95584SKent Gibson } 2326aad95584SKent Gibson 2327aad95584SKent Gibson return 0; 2328aad95584SKent Gibson } 2329aad95584SKent Gibson 23302e202ad8SKent Gibson static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip) 2331925ca369SKent Gibson { 2332925ca369SKent Gibson __u32 offset; 2333925ca369SKent Gibson 2334925ca369SKent Gibson if (copy_from_user(&offset, ip, sizeof(offset))) 2335925ca369SKent Gibson return -EFAULT; 2336925ca369SKent Gibson 23371bf7ba40SKent Gibson if (offset >= cdev->gdev->ngpio) 23381bf7ba40SKent Gibson return -EINVAL; 2339925ca369SKent Gibson 23401bf7ba40SKent Gibson if (!test_and_clear_bit(offset, cdev->watched_lines)) 2341925ca369SKent Gibson return -EBUSY; 2342925ca369SKent Gibson 2343925ca369SKent Gibson return 0; 2344925ca369SKent Gibson } 23452e202ad8SKent Gibson 23462e202ad8SKent Gibson /* 23472e202ad8SKent Gibson * gpio_ioctl() - ioctl handler for the GPIO chardev 23482e202ad8SKent Gibson */ 23492e202ad8SKent Gibson static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 23502e202ad8SKent Gibson { 23512e202ad8SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 23522e202ad8SKent Gibson struct gpio_device *gdev = cdev->gdev; 23532e202ad8SKent Gibson void __user *ip = (void __user *)arg; 23542e202ad8SKent Gibson 23552e202ad8SKent Gibson /* We fail any subsequent ioctl():s when the chip is gone */ 23562e202ad8SKent Gibson if (!gdev->chip) 23572e202ad8SKent Gibson return -ENODEV; 23582e202ad8SKent Gibson 23592e202ad8SKent Gibson /* Fill in the struct and pass to userspace */ 23601cef8b50SAndy Shevchenko switch (cmd) { 23611cef8b50SAndy Shevchenko case GPIO_GET_CHIPINFO_IOCTL: 23622e202ad8SKent Gibson return chipinfo_get(cdev, ip); 23632e202ad8SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 23641cef8b50SAndy Shevchenko case GPIO_GET_LINEHANDLE_IOCTL: 23652e202ad8SKent Gibson return linehandle_create(gdev, ip); 23661cef8b50SAndy Shevchenko case GPIO_GET_LINEEVENT_IOCTL: 23672e202ad8SKent Gibson return lineevent_create(gdev, ip); 23681cef8b50SAndy Shevchenko case GPIO_GET_LINEINFO_IOCTL: 23691cef8b50SAndy Shevchenko return lineinfo_get_v1(cdev, ip, false); 23701cef8b50SAndy Shevchenko case GPIO_GET_LINEINFO_WATCH_IOCTL: 23711cef8b50SAndy Shevchenko return lineinfo_get_v1(cdev, ip, true); 23722e202ad8SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 23731cef8b50SAndy Shevchenko case GPIO_V2_GET_LINEINFO_IOCTL: 23741cef8b50SAndy Shevchenko return lineinfo_get(cdev, ip, false); 23751cef8b50SAndy Shevchenko case GPIO_V2_GET_LINEINFO_WATCH_IOCTL: 23761cef8b50SAndy Shevchenko return lineinfo_get(cdev, ip, true); 23771cef8b50SAndy Shevchenko case GPIO_V2_GET_LINE_IOCTL: 23782e202ad8SKent Gibson return linereq_create(gdev, ip); 23791cef8b50SAndy Shevchenko case GPIO_GET_LINEINFO_UNWATCH_IOCTL: 23802e202ad8SKent Gibson return lineinfo_unwatch(cdev, ip); 23811cef8b50SAndy Shevchenko default: 2382925ca369SKent Gibson return -EINVAL; 2383925ca369SKent Gibson } 23841cef8b50SAndy Shevchenko } 2385925ca369SKent Gibson 2386925ca369SKent Gibson #ifdef CONFIG_COMPAT 238749bc5279SKent Gibson static long gpio_ioctl_compat(struct file *file, unsigned int cmd, 2388925ca369SKent Gibson unsigned long arg) 2389925ca369SKent Gibson { 239049bc5279SKent Gibson return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 2391925ca369SKent Gibson } 2392925ca369SKent Gibson #endif 2393925ca369SKent Gibson 2394925ca369SKent Gibson static struct gpio_chardev_data * 2395925ca369SKent Gibson to_gpio_chardev_data(struct notifier_block *nb) 2396925ca369SKent Gibson { 2397925ca369SKent Gibson return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb); 2398925ca369SKent Gibson } 2399925ca369SKent Gibson 2400925ca369SKent Gibson static int lineinfo_changed_notify(struct notifier_block *nb, 2401925ca369SKent Gibson unsigned long action, void *data) 2402925ca369SKent Gibson { 2403e2b781c5SKent Gibson struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb); 2404aad95584SKent Gibson struct gpio_v2_line_info_changed chg; 2405925ca369SKent Gibson struct gpio_desc *desc = data; 2406925ca369SKent Gibson int ret; 2407925ca369SKent Gibson 2408e2b781c5SKent Gibson if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) 2409925ca369SKent Gibson return NOTIFY_DONE; 2410925ca369SKent Gibson 2411925ca369SKent Gibson memset(&chg, 0, sizeof(chg)); 2412925ca369SKent Gibson chg.event_type = action; 2413aad95584SKent Gibson chg.timestamp_ns = ktime_get_ns(); 2414925ca369SKent Gibson gpio_desc_to_lineinfo(desc, &chg.info); 2415925ca369SKent Gibson 2416e2b781c5SKent Gibson ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); 2417925ca369SKent Gibson if (ret) 2418e2b781c5SKent Gibson wake_up_poll(&cdev->wait, EPOLLIN); 2419925ca369SKent Gibson else 2420925ca369SKent Gibson pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n"); 2421925ca369SKent Gibson 2422925ca369SKent Gibson return NOTIFY_OK; 2423925ca369SKent Gibson } 2424925ca369SKent Gibson 242549bc5279SKent Gibson static __poll_t lineinfo_watch_poll(struct file *file, 2426925ca369SKent Gibson struct poll_table_struct *pollt) 2427925ca369SKent Gibson { 2428e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2429925ca369SKent Gibson __poll_t events = 0; 2430925ca369SKent Gibson 2431*533aae7cSBartosz Golaszewski if (!cdev->gdev->chip) 2432*533aae7cSBartosz Golaszewski return EPOLLHUP | EPOLLERR; 2433*533aae7cSBartosz Golaszewski 2434e2b781c5SKent Gibson poll_wait(file, &cdev->wait, pollt); 2435925ca369SKent Gibson 2436e2b781c5SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events, 2437e2b781c5SKent Gibson &cdev->wait.lock)) 2438925ca369SKent Gibson events = EPOLLIN | EPOLLRDNORM; 2439925ca369SKent Gibson 2440925ca369SKent Gibson return events; 2441925ca369SKent Gibson } 2442925ca369SKent Gibson 244349bc5279SKent Gibson static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, 2444925ca369SKent Gibson size_t count, loff_t *off) 2445925ca369SKent Gibson { 2446e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2447aad95584SKent Gibson struct gpio_v2_line_info_changed event; 2448925ca369SKent Gibson ssize_t bytes_read = 0; 2449925ca369SKent Gibson int ret; 2450aad95584SKent Gibson size_t event_size; 2451925ca369SKent Gibson 2452*533aae7cSBartosz Golaszewski if (!cdev->gdev->chip) 2453*533aae7cSBartosz Golaszewski return -ENODEV; 2454*533aae7cSBartosz Golaszewski 2455aad95584SKent Gibson #ifndef CONFIG_GPIO_CDEV_V1 2456aad95584SKent Gibson event_size = sizeof(struct gpio_v2_line_info_changed); 2457aad95584SKent Gibson if (count < event_size) 2458925ca369SKent Gibson return -EINVAL; 2459aad95584SKent Gibson #endif 2460925ca369SKent Gibson 2461925ca369SKent Gibson do { 2462e2b781c5SKent Gibson spin_lock(&cdev->wait.lock); 2463e2b781c5SKent Gibson if (kfifo_is_empty(&cdev->events)) { 2464925ca369SKent Gibson if (bytes_read) { 2465e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2466925ca369SKent Gibson return bytes_read; 2467925ca369SKent Gibson } 2468925ca369SKent Gibson 246949bc5279SKent Gibson if (file->f_flags & O_NONBLOCK) { 2470e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2471925ca369SKent Gibson return -EAGAIN; 2472925ca369SKent Gibson } 2473925ca369SKent Gibson 2474e2b781c5SKent Gibson ret = wait_event_interruptible_locked(cdev->wait, 2475e2b781c5SKent Gibson !kfifo_is_empty(&cdev->events)); 2476925ca369SKent Gibson if (ret) { 2477e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2478925ca369SKent Gibson return ret; 2479925ca369SKent Gibson } 2480925ca369SKent Gibson } 2481aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2482aad95584SKent Gibson /* must be after kfifo check so watch_abi_version is set */ 2483aad95584SKent Gibson if (atomic_read(&cdev->watch_abi_version) == 2) 2484aad95584SKent Gibson event_size = sizeof(struct gpio_v2_line_info_changed); 2485aad95584SKent Gibson else 2486aad95584SKent Gibson event_size = sizeof(struct gpioline_info_changed); 2487aad95584SKent Gibson if (count < event_size) { 2488aad95584SKent Gibson spin_unlock(&cdev->wait.lock); 2489aad95584SKent Gibson return -EINVAL; 2490aad95584SKent Gibson } 2491aad95584SKent Gibson #endif 2492e2b781c5SKent Gibson ret = kfifo_out(&cdev->events, &event, 1); 2493e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2494925ca369SKent Gibson if (ret != 1) { 2495925ca369SKent Gibson ret = -EIO; 2496925ca369SKent Gibson break; 2497925ca369SKent Gibson /* We should never get here. See lineevent_read(). */ 2498925ca369SKent Gibson } 2499925ca369SKent Gibson 2500aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2501aad95584SKent Gibson if (event_size == sizeof(struct gpio_v2_line_info_changed)) { 2502aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event, event_size)) 2503925ca369SKent Gibson return -EFAULT; 2504aad95584SKent Gibson } else { 2505aad95584SKent Gibson struct gpioline_info_changed event_v1; 2506aad95584SKent Gibson 2507aad95584SKent Gibson gpio_v2_line_info_changed_to_v1(&event, &event_v1); 2508aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event_v1, 2509aad95584SKent Gibson event_size)) 2510aad95584SKent Gibson return -EFAULT; 2511aad95584SKent Gibson } 2512aad95584SKent Gibson #else 2513aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event, event_size)) 2514aad95584SKent Gibson return -EFAULT; 2515aad95584SKent Gibson #endif 2516aad95584SKent Gibson bytes_read += event_size; 2517925ca369SKent Gibson } while (count >= bytes_read + sizeof(event)); 2518925ca369SKent Gibson 2519925ca369SKent Gibson return bytes_read; 2520925ca369SKent Gibson } 2521925ca369SKent Gibson 2522925ca369SKent Gibson /** 2523925ca369SKent Gibson * gpio_chrdev_open() - open the chardev for ioctl operations 2524925ca369SKent Gibson * @inode: inode for this chardev 252549bc5279SKent Gibson * @file: file struct for storing private data 2526925ca369SKent Gibson * Returns 0 on success 2527925ca369SKent Gibson */ 252849bc5279SKent Gibson static int gpio_chrdev_open(struct inode *inode, struct file *file) 2529925ca369SKent Gibson { 2530925ca369SKent Gibson struct gpio_device *gdev = container_of(inode->i_cdev, 2531925ca369SKent Gibson struct gpio_device, chrdev); 2532e2b781c5SKent Gibson struct gpio_chardev_data *cdev; 2533925ca369SKent Gibson int ret = -ENOMEM; 2534925ca369SKent Gibson 2535925ca369SKent Gibson /* Fail on open if the backing gpiochip is gone */ 2536925ca369SKent Gibson if (!gdev->chip) 2537925ca369SKent Gibson return -ENODEV; 2538925ca369SKent Gibson 2539e2b781c5SKent Gibson cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); 2540e2b781c5SKent Gibson if (!cdev) 2541925ca369SKent Gibson return -ENOMEM; 2542925ca369SKent Gibson 2543e2b781c5SKent Gibson cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL); 2544e2b781c5SKent Gibson if (!cdev->watched_lines) 2545e2b781c5SKent Gibson goto out_free_cdev; 2546925ca369SKent Gibson 2547e2b781c5SKent Gibson init_waitqueue_head(&cdev->wait); 2548e2b781c5SKent Gibson INIT_KFIFO(cdev->events); 2549e2b781c5SKent Gibson cdev->gdev = gdev; 2550925ca369SKent Gibson 2551e2b781c5SKent Gibson cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify; 25526accc376SKent Gibson ret = blocking_notifier_chain_register(&gdev->notifier, 2553e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2554925ca369SKent Gibson if (ret) 2555925ca369SKent Gibson goto out_free_bitmap; 2556925ca369SKent Gibson 2557925ca369SKent Gibson get_device(&gdev->dev); 2558e2b781c5SKent Gibson file->private_data = cdev; 2559925ca369SKent Gibson 256049bc5279SKent Gibson ret = nonseekable_open(inode, file); 2561925ca369SKent Gibson if (ret) 2562925ca369SKent Gibson goto out_unregister_notifier; 2563925ca369SKent Gibson 2564925ca369SKent Gibson return ret; 2565925ca369SKent Gibson 2566925ca369SKent Gibson out_unregister_notifier: 25676accc376SKent Gibson blocking_notifier_chain_unregister(&gdev->notifier, 2568e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2569925ca369SKent Gibson out_free_bitmap: 2570e2b781c5SKent Gibson bitmap_free(cdev->watched_lines); 2571e2b781c5SKent Gibson out_free_cdev: 2572e2b781c5SKent Gibson kfree(cdev); 2573925ca369SKent Gibson return ret; 2574925ca369SKent Gibson } 2575925ca369SKent Gibson 2576925ca369SKent Gibson /** 2577925ca369SKent Gibson * gpio_chrdev_release() - close chardev after ioctl operations 2578925ca369SKent Gibson * @inode: inode for this chardev 257949bc5279SKent Gibson * @file: file struct for storing private data 2580925ca369SKent Gibson * Returns 0 on success 2581925ca369SKent Gibson */ 258249bc5279SKent Gibson static int gpio_chrdev_release(struct inode *inode, struct file *file) 2583925ca369SKent Gibson { 2584e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2585e2b781c5SKent Gibson struct gpio_device *gdev = cdev->gdev; 2586925ca369SKent Gibson 2587e2b781c5SKent Gibson bitmap_free(cdev->watched_lines); 25886accc376SKent Gibson blocking_notifier_chain_unregister(&gdev->notifier, 2589e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2590925ca369SKent Gibson put_device(&gdev->dev); 2591e2b781c5SKent Gibson kfree(cdev); 2592925ca369SKent Gibson 2593925ca369SKent Gibson return 0; 2594925ca369SKent Gibson } 2595925ca369SKent Gibson 2596925ca369SKent Gibson static const struct file_operations gpio_fileops = { 2597925ca369SKent Gibson .release = gpio_chrdev_release, 2598925ca369SKent Gibson .open = gpio_chrdev_open, 2599925ca369SKent Gibson .poll = lineinfo_watch_poll, 2600925ca369SKent Gibson .read = lineinfo_watch_read, 2601925ca369SKent Gibson .owner = THIS_MODULE, 2602925ca369SKent Gibson .llseek = no_llseek, 2603925ca369SKent Gibson .unlocked_ioctl = gpio_ioctl, 2604925ca369SKent Gibson #ifdef CONFIG_COMPAT 2605925ca369SKent Gibson .compat_ioctl = gpio_ioctl_compat, 2606925ca369SKent Gibson #endif 2607925ca369SKent Gibson }; 2608925ca369SKent Gibson 2609925ca369SKent Gibson int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) 2610925ca369SKent Gibson { 2611925ca369SKent Gibson int ret; 2612925ca369SKent Gibson 2613925ca369SKent Gibson cdev_init(&gdev->chrdev, &gpio_fileops); 2614925ca369SKent Gibson gdev->chrdev.owner = THIS_MODULE; 2615925ca369SKent Gibson gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id); 2616925ca369SKent Gibson 2617925ca369SKent Gibson ret = cdev_device_add(&gdev->chrdev, &gdev->dev); 2618925ca369SKent Gibson if (ret) 2619925ca369SKent Gibson return ret; 2620925ca369SKent Gibson 2621925ca369SKent Gibson chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n", 2622925ca369SKent Gibson MAJOR(devt), gdev->id); 2623925ca369SKent Gibson 2624925ca369SKent Gibson return 0; 2625925ca369SKent Gibson } 2626925ca369SKent Gibson 2627925ca369SKent Gibson void gpiolib_cdev_unregister(struct gpio_device *gdev) 2628925ca369SKent Gibson { 2629925ca369SKent Gibson cdev_device_del(&gdev->chrdev, &gdev->dev); 2630925ca369SKent Gibson } 2631