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 2041cef8b50SAndy Shevchenko switch (cmd) { 2051cef8b50SAndy Shevchenko case GPIOHANDLE_GET_LINE_VALUES_IOCTL: 2061cef8b50SAndy Shevchenko /* NOTE: It's okay to read values of output lines */ 2071cef8b50SAndy Shevchenko ret = gpiod_get_array_value_complex(false, true, 2081cef8b50SAndy Shevchenko lh->num_descs, lh->descs, 2091cef8b50SAndy Shevchenko NULL, vals); 210925ca369SKent Gibson if (ret) 211925ca369SKent Gibson return ret; 212925ca369SKent Gibson 213925ca369SKent Gibson memset(&ghd, 0, sizeof(ghd)); 21452b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) 215925ca369SKent Gibson ghd.values[i] = test_bit(i, vals); 216925ca369SKent Gibson 217925ca369SKent Gibson if (copy_to_user(ip, &ghd, sizeof(ghd))) 218925ca369SKent Gibson return -EFAULT; 219925ca369SKent Gibson 220925ca369SKent Gibson return 0; 2211cef8b50SAndy Shevchenko case GPIOHANDLE_SET_LINE_VALUES_IOCTL: 222925ca369SKent Gibson /* 223925ca369SKent Gibson * All line descriptors were created at once with the same 224925ca369SKent Gibson * flags so just check if the first one is really output. 225925ca369SKent Gibson */ 226925ca369SKent Gibson if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags)) 227925ca369SKent Gibson return -EPERM; 228925ca369SKent Gibson 229925ca369SKent Gibson if (copy_from_user(&ghd, ip, sizeof(ghd))) 230925ca369SKent Gibson return -EFAULT; 231925ca369SKent Gibson 232925ca369SKent Gibson /* Clamp all values to [0,1] */ 23352b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) 234925ca369SKent Gibson __assign_bit(i, vals, ghd.values[i]); 235925ca369SKent Gibson 236925ca369SKent Gibson /* Reuse the array setting function */ 237925ca369SKent Gibson return gpiod_set_array_value_complex(false, 238925ca369SKent Gibson true, 23952b7b596SKent Gibson lh->num_descs, 240925ca369SKent Gibson lh->descs, 241925ca369SKent Gibson NULL, 242925ca369SKent Gibson vals); 2431cef8b50SAndy Shevchenko case GPIOHANDLE_SET_CONFIG_IOCTL: 244925ca369SKent Gibson return linehandle_set_config(lh, ip); 2451cef8b50SAndy Shevchenko default: 246925ca369SKent Gibson return -EINVAL; 247925ca369SKent Gibson } 2481cef8b50SAndy Shevchenko } 249925ca369SKent Gibson 250925ca369SKent Gibson #ifdef CONFIG_COMPAT 25149bc5279SKent Gibson static long linehandle_ioctl_compat(struct file *file, unsigned int cmd, 252925ca369SKent Gibson unsigned long arg) 253925ca369SKent Gibson { 25449bc5279SKent Gibson return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 255925ca369SKent Gibson } 256925ca369SKent Gibson #endif 257925ca369SKent Gibson 258883f9198SKent Gibson static void linehandle_free(struct linehandle_state *lh) 259925ca369SKent Gibson { 260925ca369SKent Gibson int i; 261925ca369SKent Gibson 26252b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) 263883f9198SKent Gibson if (lh->descs[i]) 264925ca369SKent Gibson gpiod_free(lh->descs[i]); 265925ca369SKent Gibson kfree(lh->label); 266883f9198SKent Gibson put_device(&lh->gdev->dev); 267925ca369SKent Gibson kfree(lh); 268883f9198SKent Gibson } 269883f9198SKent Gibson 270883f9198SKent Gibson static int linehandle_release(struct inode *inode, struct file *file) 271883f9198SKent Gibson { 272883f9198SKent Gibson linehandle_free(file->private_data); 273925ca369SKent Gibson return 0; 274925ca369SKent Gibson } 275925ca369SKent Gibson 276925ca369SKent Gibson static const struct file_operations linehandle_fileops = { 277925ca369SKent Gibson .release = linehandle_release, 278925ca369SKent Gibson .owner = THIS_MODULE, 279925ca369SKent Gibson .llseek = noop_llseek, 280925ca369SKent Gibson .unlocked_ioctl = linehandle_ioctl, 281925ca369SKent Gibson #ifdef CONFIG_COMPAT 282925ca369SKent Gibson .compat_ioctl = linehandle_ioctl_compat, 283925ca369SKent Gibson #endif 284925ca369SKent Gibson }; 285925ca369SKent Gibson 286925ca369SKent Gibson static int linehandle_create(struct gpio_device *gdev, void __user *ip) 287925ca369SKent Gibson { 288925ca369SKent Gibson struct gpiohandle_request handlereq; 289925ca369SKent Gibson struct linehandle_state *lh; 290925ca369SKent Gibson struct file *file; 291883f9198SKent Gibson int fd, i, ret; 292925ca369SKent Gibson u32 lflags; 293925ca369SKent Gibson 294925ca369SKent Gibson if (copy_from_user(&handlereq, ip, sizeof(handlereq))) 295925ca369SKent Gibson return -EFAULT; 296925ca369SKent Gibson if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) 297925ca369SKent Gibson return -EINVAL; 298925ca369SKent Gibson 299925ca369SKent Gibson lflags = handlereq.flags; 300925ca369SKent Gibson 301925ca369SKent Gibson ret = linehandle_validate_flags(lflags); 302925ca369SKent Gibson if (ret) 303925ca369SKent Gibson return ret; 304925ca369SKent Gibson 305925ca369SKent Gibson lh = kzalloc(sizeof(*lh), GFP_KERNEL); 306925ca369SKent Gibson if (!lh) 307925ca369SKent Gibson return -ENOMEM; 308925ca369SKent Gibson lh->gdev = gdev; 309925ca369SKent Gibson get_device(&gdev->dev); 310925ca369SKent Gibson 311f188ac12SKent Gibson if (handlereq.consumer_label[0] != '\0') { 312f188ac12SKent Gibson /* label is only initialized if consumer_label is set */ 313f188ac12SKent Gibson lh->label = kstrndup(handlereq.consumer_label, 314f188ac12SKent Gibson sizeof(handlereq.consumer_label) - 1, 315925ca369SKent Gibson GFP_KERNEL); 316925ca369SKent Gibson if (!lh->label) { 317925ca369SKent Gibson ret = -ENOMEM; 318925ca369SKent Gibson goto out_free_lh; 319925ca369SKent Gibson } 320925ca369SKent Gibson } 321925ca369SKent Gibson 322883f9198SKent Gibson lh->num_descs = handlereq.lines; 323883f9198SKent Gibson 324925ca369SKent Gibson /* Request each GPIO */ 325925ca369SKent Gibson for (i = 0; i < handlereq.lines; i++) { 326925ca369SKent Gibson u32 offset = handlereq.lineoffsets[i]; 327925ca369SKent Gibson struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); 328925ca369SKent Gibson 329925ca369SKent Gibson if (IS_ERR(desc)) { 330925ca369SKent Gibson ret = PTR_ERR(desc); 331883f9198SKent Gibson goto out_free_lh; 332925ca369SKent Gibson } 333925ca369SKent Gibson 33495a4eed7SAndy Shevchenko ret = gpiod_request_user(desc, lh->label); 335925ca369SKent Gibson if (ret) 336883f9198SKent Gibson goto out_free_lh; 337925ca369SKent Gibson lh->descs[i] = desc; 338c274b58aSKent Gibson linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags); 339925ca369SKent Gibson 340925ca369SKent Gibson ret = gpiod_set_transitory(desc, false); 341925ca369SKent Gibson if (ret < 0) 342883f9198SKent Gibson goto out_free_lh; 343925ca369SKent Gibson 344925ca369SKent Gibson /* 345925ca369SKent Gibson * Lines have to be requested explicitly for input 346925ca369SKent Gibson * or output, else the line will be treated "as is". 347925ca369SKent Gibson */ 348925ca369SKent Gibson if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { 349925ca369SKent Gibson int val = !!handlereq.default_values[i]; 350925ca369SKent Gibson 351925ca369SKent Gibson ret = gpiod_direction_output(desc, val); 352925ca369SKent Gibson if (ret) 353883f9198SKent Gibson goto out_free_lh; 354925ca369SKent Gibson } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { 355925ca369SKent Gibson ret = gpiod_direction_input(desc); 356925ca369SKent Gibson if (ret) 357883f9198SKent Gibson goto out_free_lh; 358925ca369SKent Gibson } 359925ca369SKent Gibson 3606accc376SKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 361aad95584SKent Gibson GPIO_V2_LINE_CHANGED_REQUESTED, desc); 362925ca369SKent Gibson 363925ca369SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", 364925ca369SKent Gibson offset); 365925ca369SKent Gibson } 366925ca369SKent Gibson 367925ca369SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 368925ca369SKent Gibson if (fd < 0) { 369925ca369SKent Gibson ret = fd; 370883f9198SKent Gibson goto out_free_lh; 371925ca369SKent Gibson } 372925ca369SKent Gibson 373925ca369SKent Gibson file = anon_inode_getfile("gpio-linehandle", 374925ca369SKent Gibson &linehandle_fileops, 375925ca369SKent Gibson lh, 376925ca369SKent Gibson O_RDONLY | O_CLOEXEC); 377925ca369SKent Gibson if (IS_ERR(file)) { 378925ca369SKent Gibson ret = PTR_ERR(file); 379925ca369SKent Gibson goto out_put_unused_fd; 380925ca369SKent Gibson } 381925ca369SKent Gibson 382925ca369SKent Gibson handlereq.fd = fd; 383925ca369SKent Gibson if (copy_to_user(ip, &handlereq, sizeof(handlereq))) { 384925ca369SKent Gibson /* 385925ca369SKent Gibson * fput() will trigger the release() callback, so do not go onto 386925ca369SKent Gibson * the regular error cleanup path here. 387925ca369SKent Gibson */ 388925ca369SKent Gibson fput(file); 389925ca369SKent Gibson put_unused_fd(fd); 390925ca369SKent Gibson return -EFAULT; 391925ca369SKent Gibson } 392925ca369SKent Gibson 393925ca369SKent Gibson fd_install(fd, file); 394925ca369SKent Gibson 395925ca369SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", 39652b7b596SKent Gibson lh->num_descs); 397925ca369SKent Gibson 398925ca369SKent Gibson return 0; 399925ca369SKent Gibson 400925ca369SKent Gibson out_put_unused_fd: 401925ca369SKent Gibson put_unused_fd(fd); 402925ca369SKent Gibson out_free_lh: 403883f9198SKent Gibson linehandle_free(lh); 404925ca369SKent Gibson return ret; 405925ca369SKent Gibson } 4063c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 4073c0d9c63SKent Gibson 4083c0d9c63SKent Gibson /** 4093c0d9c63SKent Gibson * struct line - contains the state of a requested line 4103c0d9c63SKent Gibson * @desc: the GPIO descriptor for this line. 41173e03419SKent Gibson * @req: the corresponding line request 41273e03419SKent Gibson * @irq: the interrupt triggered in response to events on this GPIO 41373e03419SKent Gibson * @eflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or 41473e03419SKent Gibson * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied 41573e03419SKent Gibson * @timestamp_ns: cache for the timestamp storing it between hardirq and 41673e03419SKent Gibson * IRQ thread, used to bring the timestamp close to the actual event 41773e03419SKent Gibson * @req_seqno: the seqno for the current edge event in the sequence of 41873e03419SKent Gibson * events for the corresponding line request. This is drawn from the @req. 41973e03419SKent Gibson * @line_seqno: the seqno for the current edge event in the sequence of 42073e03419SKent Gibson * events for this line. 42165cff704SKent Gibson * @work: the worker that implements software debouncing 42265cff704SKent Gibson * @sw_debounced: flag indicating if the software debouncer is active 42365cff704SKent Gibson * @level: the current debounced physical level of the line 4243c0d9c63SKent Gibson */ 4253c0d9c63SKent Gibson struct line { 4263c0d9c63SKent Gibson struct gpio_desc *desc; 42773e03419SKent Gibson /* 42873e03419SKent Gibson * -- edge detector specific fields -- 42973e03419SKent Gibson */ 43073e03419SKent Gibson struct linereq *req; 43173e03419SKent Gibson unsigned int irq; 4323ffb7c45SKent Gibson /* 433*b1a92e94SKent Gibson * The flags for the active edge detector configuration. 434*b1a92e94SKent Gibson * 435*b1a92e94SKent Gibson * edflags is set by linereq_create(), linereq_free(), and 436*b1a92e94SKent Gibson * linereq_set_config_unlocked(), which are themselves mutually 437*b1a92e94SKent Gibson * exclusive, and is accessed by edge_irq_thread(), 438*b1a92e94SKent Gibson * process_hw_ts_thread() and debounce_work_func(), 439*b1a92e94SKent Gibson * which can all live with a slightly stale value. 4403ffb7c45SKent Gibson */ 441*b1a92e94SKent Gibson u64 edflags; 44273e03419SKent Gibson /* 44373e03419SKent Gibson * timestamp_ns and req_seqno are accessed only by 44473e03419SKent Gibson * edge_irq_handler() and edge_irq_thread(), which are themselves 44573e03419SKent Gibson * mutually exclusive, so no additional protection is necessary. 44673e03419SKent Gibson */ 44773e03419SKent Gibson u64 timestamp_ns; 44873e03419SKent Gibson u32 req_seqno; 44965cff704SKent Gibson /* 45065cff704SKent Gibson * line_seqno is accessed by either edge_irq_thread() or 45165cff704SKent Gibson * debounce_work_func(), which are themselves mutually exclusive, 45265cff704SKent Gibson * so no additional protection is necessary. 45365cff704SKent Gibson */ 45473e03419SKent Gibson u32 line_seqno; 45565cff704SKent Gibson /* 45665cff704SKent Gibson * -- debouncer specific fields -- 45765cff704SKent Gibson */ 45865cff704SKent Gibson struct delayed_work work; 45965cff704SKent Gibson /* 46065cff704SKent Gibson * sw_debounce is accessed by linereq_set_config(), which is the 46165cff704SKent Gibson * only setter, and linereq_get_values(), which can live with a 46265cff704SKent Gibson * slightly stale value. 46365cff704SKent Gibson */ 46465cff704SKent Gibson unsigned int sw_debounced; 46565cff704SKent Gibson /* 46665cff704SKent Gibson * level is accessed by debounce_work_func(), which is the only 46765cff704SKent Gibson * setter, and linereq_get_values() which can live with a slightly 46865cff704SKent Gibson * stale value. 46965cff704SKent Gibson */ 47065cff704SKent Gibson unsigned int level; 4712068339aSDipen Patel /* 4722068339aSDipen Patel * -- hte specific fields -- 4732068339aSDipen Patel */ 4742068339aSDipen Patel struct hte_ts_desc hdesc; 4752068339aSDipen Patel /* 4762068339aSDipen Patel * HTE provider sets line level at the time of event. The valid 4772068339aSDipen Patel * value is 0 or 1 and negative value for an error. 4782068339aSDipen Patel */ 4792068339aSDipen Patel int raw_level; 4802068339aSDipen Patel /* 4812068339aSDipen Patel * when sw_debounce is set on HTE enabled line, this is running 4822068339aSDipen Patel * counter of the discarded events. 4832068339aSDipen Patel */ 4842068339aSDipen Patel u32 total_discard_seq; 4852068339aSDipen Patel /* 4862068339aSDipen Patel * when sw_debounce is set on HTE enabled line, this variable records 4872068339aSDipen Patel * last sequence number before debounce period expires. 4882068339aSDipen Patel */ 4892068339aSDipen Patel u32 last_seqno; 4903c0d9c63SKent Gibson }; 4913c0d9c63SKent Gibson 4923c0d9c63SKent Gibson /** 4933c0d9c63SKent Gibson * struct linereq - contains the state of a userspace line request 4943c0d9c63SKent Gibson * @gdev: the GPIO device the line request pertains to 4953c0d9c63SKent Gibson * @label: consumer label used to tag GPIO descriptors 4963c0d9c63SKent Gibson * @num_lines: the number of lines in the lines array 49773e03419SKent Gibson * @wait: wait queue that handles blocking reads of events 49873e03419SKent Gibson * @event_buffer_size: the number of elements allocated in @events 49973e03419SKent Gibson * @events: KFIFO for the GPIO events 50073e03419SKent Gibson * @seqno: the sequence number for edge events generated on all lines in 50173e03419SKent Gibson * this line request. Note that this is not used when @num_lines is 1, as 50273e03419SKent Gibson * the line_seqno is then the same and is cheaper to calculate. 503a54756cbSKent Gibson * @config_mutex: mutex for serializing ioctl() calls to ensure consistency 504a54756cbSKent Gibson * of configuration, particularly multi-step accesses to desc flags. 5053c0d9c63SKent Gibson * @lines: the lines held by this line request, with @num_lines elements. 5063c0d9c63SKent Gibson */ 5073c0d9c63SKent Gibson struct linereq { 5083c0d9c63SKent Gibson struct gpio_device *gdev; 5093c0d9c63SKent Gibson const char *label; 5103c0d9c63SKent Gibson u32 num_lines; 51173e03419SKent Gibson wait_queue_head_t wait; 51273e03419SKent Gibson u32 event_buffer_size; 51373e03419SKent Gibson DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); 51473e03419SKent Gibson atomic_t seqno; 515a54756cbSKent Gibson struct mutex config_mutex; 5163c0d9c63SKent Gibson struct line lines[]; 5173c0d9c63SKent Gibson }; 5183c0d9c63SKent Gibson 5193c0d9c63SKent Gibson #define GPIO_V2_LINE_BIAS_FLAGS \ 5203c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ 5213c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \ 5223c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_DISABLED) 5233c0d9c63SKent Gibson 5243c0d9c63SKent Gibson #define GPIO_V2_LINE_DIRECTION_FLAGS \ 5253c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_INPUT | \ 5263c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_OUTPUT) 5273c0d9c63SKent Gibson 5283c0d9c63SKent Gibson #define GPIO_V2_LINE_DRIVE_FLAGS \ 5293c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \ 5303c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_OPEN_SOURCE) 5313c0d9c63SKent Gibson 53273e03419SKent Gibson #define GPIO_V2_LINE_EDGE_FLAGS \ 53373e03419SKent Gibson (GPIO_V2_LINE_FLAG_EDGE_RISING | \ 53473e03419SKent Gibson GPIO_V2_LINE_FLAG_EDGE_FALLING) 53573e03419SKent Gibson 5365e2ca893SKent Gibson #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS 5375e2ca893SKent Gibson 5383c0d9c63SKent Gibson #define GPIO_V2_LINE_VALID_FLAGS \ 5393c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ 5403c0d9c63SKent Gibson GPIO_V2_LINE_DIRECTION_FLAGS | \ 5413c0d9c63SKent Gibson GPIO_V2_LINE_DRIVE_FLAGS | \ 54273e03419SKent Gibson GPIO_V2_LINE_EDGE_FLAGS | \ 54326d060e4SKent Gibson GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \ 5442068339aSDipen Patel GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \ 5453c0d9c63SKent Gibson GPIO_V2_LINE_BIAS_FLAGS) 5463c0d9c63SKent Gibson 547*b1a92e94SKent Gibson /* subset of flags relevant for edge detector configuration */ 548*b1a92e94SKent Gibson #define GPIO_V2_LINE_EDGE_DETECTOR_FLAGS \ 549*b1a92e94SKent Gibson (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ 550*b1a92e94SKent Gibson GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \ 551*b1a92e94SKent Gibson GPIO_V2_LINE_EDGE_FLAGS) 552*b1a92e94SKent Gibson 55373e03419SKent Gibson static void linereq_put_event(struct linereq *lr, 55473e03419SKent Gibson struct gpio_v2_line_event *le) 55573e03419SKent Gibson { 55673e03419SKent Gibson bool overflow = false; 55773e03419SKent Gibson 55873e03419SKent Gibson spin_lock(&lr->wait.lock); 55973e03419SKent Gibson if (kfifo_is_full(&lr->events)) { 56073e03419SKent Gibson overflow = true; 56173e03419SKent Gibson kfifo_skip(&lr->events); 56273e03419SKent Gibson } 56373e03419SKent Gibson kfifo_in(&lr->events, le, 1); 56473e03419SKent Gibson spin_unlock(&lr->wait.lock); 56573e03419SKent Gibson if (!overflow) 56673e03419SKent Gibson wake_up_poll(&lr->wait, EPOLLIN); 56773e03419SKent Gibson else 56873e03419SKent Gibson pr_debug_ratelimited("event FIFO is full - event dropped\n"); 56973e03419SKent Gibson } 57073e03419SKent Gibson 57126d060e4SKent Gibson static u64 line_event_timestamp(struct line *line) 57226d060e4SKent Gibson { 57326d060e4SKent Gibson if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags)) 57426d060e4SKent Gibson return ktime_get_real_ns(); 5752068339aSDipen Patel else if (test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags)) 5762068339aSDipen Patel return line->timestamp_ns; 57726d060e4SKent Gibson 57826d060e4SKent Gibson return ktime_get_ns(); 57926d060e4SKent Gibson } 58026d060e4SKent Gibson 58124220232SKent Gibson static u32 line_event_id(int level) 58224220232SKent Gibson { 58324220232SKent Gibson return level ? GPIO_V2_LINE_EVENT_RISING_EDGE : 58424220232SKent Gibson GPIO_V2_LINE_EVENT_FALLING_EDGE; 58524220232SKent Gibson } 58624220232SKent Gibson 5872068339aSDipen Patel static enum hte_return process_hw_ts_thread(void *p) 5882068339aSDipen Patel { 5892068339aSDipen Patel struct line *line; 5902068339aSDipen Patel struct linereq *lr; 5912068339aSDipen Patel struct gpio_v2_line_event le; 592*b1a92e94SKent Gibson u64 edflags; 5932068339aSDipen Patel int level; 5942068339aSDipen Patel 5952068339aSDipen Patel if (!p) 5962068339aSDipen Patel return HTE_CB_HANDLED; 5972068339aSDipen Patel 5982068339aSDipen Patel line = p; 5992068339aSDipen Patel lr = line->req; 6002068339aSDipen Patel 6012068339aSDipen Patel memset(&le, 0, sizeof(le)); 6022068339aSDipen Patel 6032068339aSDipen Patel le.timestamp_ns = line->timestamp_ns; 604*b1a92e94SKent Gibson edflags = READ_ONCE(line->edflags); 6052068339aSDipen Patel 606*b1a92e94SKent Gibson switch (edflags & GPIO_V2_LINE_EDGE_FLAGS) { 607cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_BOTH: 60824220232SKent Gibson level = (line->raw_level >= 0) ? 60924220232SKent Gibson line->raw_level : 61024220232SKent Gibson gpiod_get_raw_value_cansleep(line->desc); 6112068339aSDipen Patel 612*b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 61324220232SKent Gibson level = !level; 61424220232SKent Gibson 61524220232SKent Gibson le.id = line_event_id(level); 616cfa53463SKent Gibson break; 617cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_RISING: 6182068339aSDipen Patel le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 619cfa53463SKent Gibson break; 620cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_FALLING: 6212068339aSDipen Patel le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 622cfa53463SKent Gibson break; 623cfa53463SKent Gibson default: 6242068339aSDipen Patel return HTE_CB_HANDLED; 6252068339aSDipen Patel } 6262068339aSDipen Patel le.line_seqno = line->line_seqno; 6272068339aSDipen Patel le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno; 6282068339aSDipen Patel le.offset = gpio_chip_hwgpio(line->desc); 6292068339aSDipen Patel 6302068339aSDipen Patel linereq_put_event(lr, &le); 6312068339aSDipen Patel 6322068339aSDipen Patel return HTE_CB_HANDLED; 6332068339aSDipen Patel } 6342068339aSDipen Patel 6352068339aSDipen Patel static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p) 6362068339aSDipen Patel { 6372068339aSDipen Patel struct line *line; 6382068339aSDipen Patel struct linereq *lr; 6392068339aSDipen Patel int diff_seqno = 0; 6402068339aSDipen Patel 6412068339aSDipen Patel if (!ts || !p) 6422068339aSDipen Patel return HTE_CB_HANDLED; 6432068339aSDipen Patel 6442068339aSDipen Patel line = p; 6452068339aSDipen Patel line->timestamp_ns = ts->tsc; 6462068339aSDipen Patel line->raw_level = ts->raw_level; 6472068339aSDipen Patel lr = line->req; 6482068339aSDipen Patel 6492068339aSDipen Patel if (READ_ONCE(line->sw_debounced)) { 6502068339aSDipen Patel line->total_discard_seq++; 6512068339aSDipen Patel line->last_seqno = ts->seq; 6522068339aSDipen Patel mod_delayed_work(system_wq, &line->work, 6532068339aSDipen Patel usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us))); 6542068339aSDipen Patel } else { 6552068339aSDipen Patel if (unlikely(ts->seq < line->line_seqno)) 6562068339aSDipen Patel return HTE_CB_HANDLED; 6572068339aSDipen Patel 6582068339aSDipen Patel diff_seqno = ts->seq - line->line_seqno; 6592068339aSDipen Patel line->line_seqno = ts->seq; 6602068339aSDipen Patel if (lr->num_lines != 1) 6612068339aSDipen Patel line->req_seqno = atomic_add_return(diff_seqno, 6622068339aSDipen Patel &lr->seqno); 6632068339aSDipen Patel 6642068339aSDipen Patel return HTE_RUN_SECOND_CB; 6652068339aSDipen Patel } 6662068339aSDipen Patel 6672068339aSDipen Patel return HTE_CB_HANDLED; 6682068339aSDipen Patel } 6692068339aSDipen Patel 67073e03419SKent Gibson static irqreturn_t edge_irq_thread(int irq, void *p) 67173e03419SKent Gibson { 67273e03419SKent Gibson struct line *line = p; 67373e03419SKent Gibson struct linereq *lr = line->req; 67473e03419SKent Gibson struct gpio_v2_line_event le; 67573e03419SKent Gibson 67673e03419SKent Gibson /* Do not leak kernel stack to userspace */ 67773e03419SKent Gibson memset(&le, 0, sizeof(le)); 67873e03419SKent Gibson 67973e03419SKent Gibson if (line->timestamp_ns) { 68073e03419SKent Gibson le.timestamp_ns = line->timestamp_ns; 68173e03419SKent Gibson } else { 68273e03419SKent Gibson /* 68373e03419SKent Gibson * We may be running from a nested threaded interrupt in 68473e03419SKent Gibson * which case we didn't get the timestamp from 68573e03419SKent Gibson * edge_irq_handler(). 68673e03419SKent Gibson */ 68726d060e4SKent Gibson le.timestamp_ns = line_event_timestamp(line); 68873e03419SKent Gibson if (lr->num_lines != 1) 68973e03419SKent Gibson line->req_seqno = atomic_inc_return(&lr->seqno); 69073e03419SKent Gibson } 69173e03419SKent Gibson line->timestamp_ns = 0; 69273e03419SKent Gibson 693*b1a92e94SKent Gibson switch (READ_ONCE(line->edflags) & GPIO_V2_LINE_EDGE_FLAGS) { 694cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_BOTH: 69524220232SKent Gibson le.id = line_event_id(gpiod_get_value_cansleep(line->desc)); 696cfa53463SKent Gibson break; 697cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_RISING: 69873e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 699cfa53463SKent Gibson break; 700cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_FALLING: 70173e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 702cfa53463SKent Gibson break; 703cfa53463SKent Gibson default: 70473e03419SKent Gibson return IRQ_NONE; 70573e03419SKent Gibson } 70673e03419SKent Gibson line->line_seqno++; 70773e03419SKent Gibson le.line_seqno = line->line_seqno; 70873e03419SKent Gibson le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno; 70973e03419SKent Gibson le.offset = gpio_chip_hwgpio(line->desc); 71073e03419SKent Gibson 71173e03419SKent Gibson linereq_put_event(lr, &le); 71273e03419SKent Gibson 71373e03419SKent Gibson return IRQ_HANDLED; 71473e03419SKent Gibson } 71573e03419SKent Gibson 71673e03419SKent Gibson static irqreturn_t edge_irq_handler(int irq, void *p) 71773e03419SKent Gibson { 71873e03419SKent Gibson struct line *line = p; 71973e03419SKent Gibson struct linereq *lr = line->req; 72073e03419SKent Gibson 72173e03419SKent Gibson /* 72273e03419SKent Gibson * Just store the timestamp in hardirq context so we get it as 72373e03419SKent Gibson * close in time as possible to the actual event. 72473e03419SKent Gibson */ 72526d060e4SKent Gibson line->timestamp_ns = line_event_timestamp(line); 72673e03419SKent Gibson 72773e03419SKent Gibson if (lr->num_lines != 1) 72873e03419SKent Gibson line->req_seqno = atomic_inc_return(&lr->seqno); 72973e03419SKent Gibson 73073e03419SKent Gibson return IRQ_WAKE_THREAD; 73173e03419SKent Gibson } 73273e03419SKent Gibson 73365cff704SKent Gibson /* 73465cff704SKent Gibson * returns the current debounced logical value. 73565cff704SKent Gibson */ 73665cff704SKent Gibson static bool debounced_value(struct line *line) 73765cff704SKent Gibson { 73865cff704SKent Gibson bool value; 73965cff704SKent Gibson 74065cff704SKent Gibson /* 74165cff704SKent Gibson * minor race - debouncer may be stopped here, so edge_detector_stop() 74265cff704SKent Gibson * must leave the value unchanged so the following will read the level 74365cff704SKent Gibson * from when the debouncer was last running. 74465cff704SKent Gibson */ 74565cff704SKent Gibson value = READ_ONCE(line->level); 74665cff704SKent Gibson 74765cff704SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags)) 74865cff704SKent Gibson value = !value; 74965cff704SKent Gibson 75065cff704SKent Gibson return value; 75165cff704SKent Gibson } 75265cff704SKent Gibson 75365cff704SKent Gibson static irqreturn_t debounce_irq_handler(int irq, void *p) 75465cff704SKent Gibson { 75565cff704SKent Gibson struct line *line = p; 75665cff704SKent Gibson 75765cff704SKent Gibson mod_delayed_work(system_wq, &line->work, 75865cff704SKent Gibson usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us))); 75965cff704SKent Gibson 76065cff704SKent Gibson return IRQ_HANDLED; 76165cff704SKent Gibson } 76265cff704SKent Gibson 76365cff704SKent Gibson static void debounce_work_func(struct work_struct *work) 76465cff704SKent Gibson { 76565cff704SKent Gibson struct gpio_v2_line_event le; 76665cff704SKent Gibson struct line *line = container_of(work, struct line, work.work); 76765cff704SKent Gibson struct linereq *lr; 768*b1a92e94SKent Gibson u64 eflags, edflags = READ_ONCE(line->edflags); 769*b1a92e94SKent Gibson int level = -1, diff_seqno; 77065cff704SKent Gibson 771*b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) 7722068339aSDipen Patel level = line->raw_level; 7732068339aSDipen Patel if (level < 0) 77465cff704SKent Gibson level = gpiod_get_raw_value_cansleep(line->desc); 77565cff704SKent Gibson if (level < 0) { 77665cff704SKent Gibson pr_debug_ratelimited("debouncer failed to read line value\n"); 77765cff704SKent Gibson return; 77865cff704SKent Gibson } 77965cff704SKent Gibson 78065cff704SKent Gibson if (READ_ONCE(line->level) == level) 78165cff704SKent Gibson return; 78265cff704SKent Gibson 78365cff704SKent Gibson WRITE_ONCE(line->level, level); 78465cff704SKent Gibson 78565cff704SKent Gibson /* -- edge detection -- */ 786*b1a92e94SKent Gibson eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS; 7873ffb7c45SKent Gibson if (!eflags) 78865cff704SKent Gibson return; 78965cff704SKent Gibson 79065cff704SKent Gibson /* switch from physical level to logical - if they differ */ 791*b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 79265cff704SKent Gibson level = !level; 79365cff704SKent Gibson 79465cff704SKent Gibson /* ignore edges that are not being monitored */ 7953ffb7c45SKent Gibson if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) || 7963ffb7c45SKent Gibson ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level)) 79765cff704SKent Gibson return; 79865cff704SKent Gibson 79965cff704SKent Gibson /* Do not leak kernel stack to userspace */ 80065cff704SKent Gibson memset(&le, 0, sizeof(le)); 80165cff704SKent Gibson 80265cff704SKent Gibson lr = line->req; 80326d060e4SKent Gibson le.timestamp_ns = line_event_timestamp(line); 80465cff704SKent Gibson le.offset = gpio_chip_hwgpio(line->desc); 805*b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) { 8062068339aSDipen Patel /* discard events except the last one */ 8072068339aSDipen Patel line->total_discard_seq -= 1; 8082068339aSDipen Patel diff_seqno = line->last_seqno - line->total_discard_seq - 8092068339aSDipen Patel line->line_seqno; 8102068339aSDipen Patel line->line_seqno = line->last_seqno - line->total_discard_seq; 8112068339aSDipen Patel le.line_seqno = line->line_seqno; 8122068339aSDipen Patel le.seqno = (lr->num_lines == 1) ? 8132068339aSDipen Patel le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno); 8142068339aSDipen Patel } else { 81565cff704SKent Gibson line->line_seqno++; 81665cff704SKent Gibson le.line_seqno = line->line_seqno; 81765cff704SKent Gibson le.seqno = (lr->num_lines == 1) ? 81865cff704SKent Gibson le.line_seqno : atomic_inc_return(&lr->seqno); 8192068339aSDipen Patel } 82065cff704SKent Gibson 82124220232SKent Gibson le.id = line_event_id(level); 82265cff704SKent Gibson 82365cff704SKent Gibson linereq_put_event(lr, &le); 82465cff704SKent Gibson } 82565cff704SKent Gibson 8262068339aSDipen Patel static int hte_edge_setup(struct line *line, u64 eflags) 8272068339aSDipen Patel { 8282068339aSDipen Patel int ret; 8292068339aSDipen Patel unsigned long flags = 0; 8302068339aSDipen Patel struct hte_ts_desc *hdesc = &line->hdesc; 8312068339aSDipen Patel 8322068339aSDipen Patel if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) 8332068339aSDipen Patel flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 8342068339aSDipen Patel HTE_FALLING_EDGE_TS : HTE_RISING_EDGE_TS; 8352068339aSDipen Patel if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) 8362068339aSDipen Patel flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 8372068339aSDipen Patel HTE_RISING_EDGE_TS : HTE_FALLING_EDGE_TS; 8382068339aSDipen Patel 8392068339aSDipen Patel line->total_discard_seq = 0; 8402068339aSDipen Patel 8412068339aSDipen Patel hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags, 8422068339aSDipen Patel NULL, line->desc); 8432068339aSDipen Patel 8442068339aSDipen Patel ret = hte_ts_get(NULL, hdesc, 0); 8452068339aSDipen Patel if (ret) 8462068339aSDipen Patel return ret; 8472068339aSDipen Patel 8482068339aSDipen Patel return hte_request_ts_ns(hdesc, process_hw_ts, 8492068339aSDipen Patel process_hw_ts_thread, line); 8502068339aSDipen Patel } 8512068339aSDipen Patel 852*b1a92e94SKent Gibson static int debounce_setup(struct line *line, unsigned int debounce_period_us) 85365cff704SKent Gibson { 85465cff704SKent Gibson unsigned long irqflags; 85565cff704SKent Gibson int ret, level, irq; 85665cff704SKent Gibson 85765cff704SKent Gibson /* try hardware */ 85865cff704SKent Gibson ret = gpiod_set_debounce(line->desc, debounce_period_us); 85965cff704SKent Gibson if (!ret) { 86065cff704SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 86165cff704SKent Gibson return ret; 86265cff704SKent Gibson } 86365cff704SKent Gibson if (ret != -ENOTSUPP) 86465cff704SKent Gibson return ret; 86565cff704SKent Gibson 86665cff704SKent Gibson if (debounce_period_us) { 86765cff704SKent Gibson /* setup software debounce */ 86865cff704SKent Gibson level = gpiod_get_raw_value_cansleep(line->desc); 86965cff704SKent Gibson if (level < 0) 87065cff704SKent Gibson return level; 87165cff704SKent Gibson 872*b1a92e94SKent Gibson if (!test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags)) { 87365cff704SKent Gibson irq = gpiod_to_irq(line->desc); 87465cff704SKent Gibson if (irq < 0) 87565cff704SKent Gibson return -ENXIO; 87665cff704SKent Gibson 87765cff704SKent Gibson irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING; 87865cff704SKent Gibson ret = request_irq(irq, debounce_irq_handler, irqflags, 87965cff704SKent Gibson line->req->label, line); 88065cff704SKent Gibson if (ret) 88165cff704SKent Gibson return ret; 88265cff704SKent Gibson line->irq = irq; 8832068339aSDipen Patel } else { 8842487a812SKent Gibson ret = hte_edge_setup(line, GPIO_V2_LINE_FLAG_EDGE_BOTH); 8852068339aSDipen Patel if (ret) 8862068339aSDipen Patel return ret; 8872068339aSDipen Patel } 8882068339aSDipen Patel 8892068339aSDipen Patel WRITE_ONCE(line->level, level); 8902068339aSDipen Patel WRITE_ONCE(line->sw_debounced, 1); 89165cff704SKent Gibson } 89265cff704SKent Gibson return 0; 89365cff704SKent Gibson } 89465cff704SKent Gibson 89565cff704SKent Gibson static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc, 89665cff704SKent Gibson unsigned int line_idx) 89765cff704SKent Gibson { 89865cff704SKent Gibson unsigned int i; 89965cff704SKent Gibson u64 mask = BIT_ULL(line_idx); 90065cff704SKent Gibson 90165cff704SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 90265cff704SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && 90365cff704SKent Gibson (lc->attrs[i].mask & mask)) 90465cff704SKent Gibson return true; 90565cff704SKent Gibson } 90665cff704SKent Gibson return false; 90765cff704SKent Gibson } 90865cff704SKent Gibson 90965cff704SKent Gibson static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc, 91065cff704SKent Gibson unsigned int line_idx) 91165cff704SKent Gibson { 91265cff704SKent Gibson unsigned int i; 91365cff704SKent Gibson u64 mask = BIT_ULL(line_idx); 91465cff704SKent Gibson 91565cff704SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 91665cff704SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && 91765cff704SKent Gibson (lc->attrs[i].mask & mask)) 91865cff704SKent Gibson return lc->attrs[i].attr.debounce_period_us; 91965cff704SKent Gibson } 92065cff704SKent Gibson return 0; 92165cff704SKent Gibson } 92265cff704SKent Gibson 923*b1a92e94SKent Gibson static void edge_detector_stop(struct line *line) 92473e03419SKent Gibson { 925*b1a92e94SKent Gibson if (line->irq) { 92673e03419SKent Gibson free_irq(line->irq, line); 92773e03419SKent Gibson line->irq = 0; 92873e03419SKent Gibson } 929a54756cbSKent Gibson 930*b1a92e94SKent Gibson if (READ_ONCE(line->edflags) & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) 9312068339aSDipen Patel hte_ts_put(&line->hdesc); 9322068339aSDipen Patel 93365cff704SKent Gibson cancel_delayed_work_sync(&line->work); 93465cff704SKent Gibson WRITE_ONCE(line->sw_debounced, 0); 935*b1a92e94SKent Gibson WRITE_ONCE(line->edflags, 0); 93603a58ea5SKent Gibson if (line->desc) 93703a58ea5SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, 0); 93865cff704SKent Gibson /* do not change line->level - see comment in debounced_value() */ 93973e03419SKent Gibson } 94073e03419SKent Gibson 94173e03419SKent Gibson static int edge_detector_setup(struct line *line, 94265cff704SKent Gibson struct gpio_v2_line_config *lc, 943*b1a92e94SKent Gibson unsigned int line_idx, u64 edflags) 94473e03419SKent Gibson { 94565cff704SKent Gibson u32 debounce_period_us; 94673e03419SKent Gibson unsigned long irqflags = 0; 947*b1a92e94SKent Gibson u64 eflags; 94873e03419SKent Gibson int irq, ret; 94973e03419SKent Gibson 950*b1a92e94SKent Gibson eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS; 95173e03419SKent Gibson if (eflags && !kfifo_initialized(&line->req->events)) { 95273e03419SKent Gibson ret = kfifo_alloc(&line->req->events, 95373e03419SKent Gibson line->req->event_buffer_size, GFP_KERNEL); 95473e03419SKent Gibson if (ret) 95573e03419SKent Gibson return ret; 95673e03419SKent Gibson } 95765cff704SKent Gibson if (gpio_v2_line_config_debounced(lc, line_idx)) { 95865cff704SKent Gibson debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx); 959*b1a92e94SKent Gibson ret = debounce_setup(line, debounce_period_us); 96065cff704SKent Gibson if (ret) 96165cff704SKent Gibson return ret; 96265cff704SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 96365cff704SKent Gibson } 96473e03419SKent Gibson 96565cff704SKent Gibson /* detection disabled or sw debouncer will provide edge detection */ 96665cff704SKent Gibson if (!eflags || READ_ONCE(line->sw_debounced)) 96773e03419SKent Gibson return 0; 96873e03419SKent Gibson 969*b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) 970*b1a92e94SKent Gibson return hte_edge_setup(line, edflags); 9712068339aSDipen Patel 97273e03419SKent Gibson irq = gpiod_to_irq(line->desc); 97373e03419SKent Gibson if (irq < 0) 97473e03419SKent Gibson return -ENXIO; 97573e03419SKent Gibson 97673e03419SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) 97773e03419SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 97873e03419SKent Gibson IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 97973e03419SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) 98073e03419SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 98173e03419SKent Gibson IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 98273e03419SKent Gibson irqflags |= IRQF_ONESHOT; 98373e03419SKent Gibson 98473e03419SKent Gibson /* Request a thread to read the events */ 98573e03419SKent Gibson ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread, 98673e03419SKent Gibson irqflags, line->req->label, line); 98773e03419SKent Gibson if (ret) 98873e03419SKent Gibson return ret; 98973e03419SKent Gibson 99073e03419SKent Gibson line->irq = irq; 99173e03419SKent Gibson return 0; 99273e03419SKent Gibson } 99373e03419SKent Gibson 99465cff704SKent Gibson static int edge_detector_update(struct line *line, 99565cff704SKent Gibson struct gpio_v2_line_config *lc, 996*b1a92e94SKent Gibson unsigned int line_idx, u64 edflags) 997a54756cbSKent Gibson { 998*b1a92e94SKent Gibson u64 active_edflags = READ_ONCE(line->edflags); 99965cff704SKent Gibson unsigned int debounce_period_us = 100065cff704SKent Gibson gpio_v2_line_config_debounce_period(lc, line_idx); 100165cff704SKent Gibson 1002*b1a92e94SKent Gibson if ((active_edflags == edflags) && 1003*b1a92e94SKent Gibson (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us)) 1004a54756cbSKent Gibson return 0; 1005a54756cbSKent Gibson 100665cff704SKent Gibson /* sw debounced and still will be...*/ 100765cff704SKent Gibson if (debounce_period_us && READ_ONCE(line->sw_debounced)) { 100865cff704SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 100965cff704SKent Gibson return 0; 101065cff704SKent Gibson } 101165cff704SKent Gibson 101265cff704SKent Gibson /* reconfiguring edge detection or sw debounce being disabled */ 1013*b1a92e94SKent Gibson if ((line->irq && !READ_ONCE(line->sw_debounced)) || 1014*b1a92e94SKent Gibson (active_edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) || 101565cff704SKent Gibson (!debounce_period_us && READ_ONCE(line->sw_debounced))) 1016*b1a92e94SKent Gibson edge_detector_stop(line); 1017a54756cbSKent Gibson 1018*b1a92e94SKent Gibson return edge_detector_setup(line, lc, line_idx, edflags); 1019a54756cbSKent Gibson } 1020a54756cbSKent Gibson 10213c0d9c63SKent Gibson static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, 10223c0d9c63SKent Gibson unsigned int line_idx) 10233c0d9c63SKent Gibson { 10243c0d9c63SKent Gibson unsigned int i; 10253c0d9c63SKent Gibson u64 mask = BIT_ULL(line_idx); 10263c0d9c63SKent Gibson 10273c0d9c63SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 10283c0d9c63SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) && 10293c0d9c63SKent Gibson (lc->attrs[i].mask & mask)) 10303c0d9c63SKent Gibson return lc->attrs[i].attr.flags; 10313c0d9c63SKent Gibson } 10323c0d9c63SKent Gibson return lc->flags; 10333c0d9c63SKent Gibson } 10343c0d9c63SKent Gibson 10353c0d9c63SKent Gibson static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc, 10363c0d9c63SKent Gibson unsigned int line_idx) 10373c0d9c63SKent Gibson { 10383c0d9c63SKent Gibson unsigned int i; 10393c0d9c63SKent Gibson u64 mask = BIT_ULL(line_idx); 10403c0d9c63SKent Gibson 10413c0d9c63SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 10423c0d9c63SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) && 10433c0d9c63SKent Gibson (lc->attrs[i].mask & mask)) 10443c0d9c63SKent Gibson return !!(lc->attrs[i].attr.values & mask); 10453c0d9c63SKent Gibson } 10463c0d9c63SKent Gibson return 0; 10473c0d9c63SKent Gibson } 10483c0d9c63SKent Gibson 10493c0d9c63SKent Gibson static int gpio_v2_line_flags_validate(u64 flags) 10503c0d9c63SKent Gibson { 10513c0d9c63SKent Gibson /* Return an error if an unknown flag is set */ 10523c0d9c63SKent Gibson if (flags & ~GPIO_V2_LINE_VALID_FLAGS) 10533c0d9c63SKent Gibson return -EINVAL; 10543c0d9c63SKent Gibson /* 10553c0d9c63SKent Gibson * Do not allow both INPUT and OUTPUT flags to be set as they are 10563c0d9c63SKent Gibson * contradictory. 10573c0d9c63SKent Gibson */ 10583c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_FLAG_INPUT) && 10593c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_OUTPUT)) 10603c0d9c63SKent Gibson return -EINVAL; 10613c0d9c63SKent Gibson 10622068339aSDipen Patel /* Only allow one event clock source */ 10632068339aSDipen Patel if ((flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) && 10642068339aSDipen Patel (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)) 10652068339aSDipen Patel return -EINVAL; 10662068339aSDipen Patel 106773e03419SKent Gibson /* Edge detection requires explicit input. */ 106873e03419SKent Gibson if ((flags & GPIO_V2_LINE_EDGE_FLAGS) && 106973e03419SKent Gibson !(flags & GPIO_V2_LINE_FLAG_INPUT)) 107073e03419SKent Gibson return -EINVAL; 107173e03419SKent Gibson 10723c0d9c63SKent Gibson /* 10733c0d9c63SKent Gibson * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single 10743c0d9c63SKent Gibson * request. If the hardware actually supports enabling both at the 10753c0d9c63SKent Gibson * same time the electrical result would be disastrous. 10763c0d9c63SKent Gibson */ 10773c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) && 10783c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE)) 10793c0d9c63SKent Gibson return -EINVAL; 10803c0d9c63SKent Gibson 10813c0d9c63SKent Gibson /* Drive requires explicit output direction. */ 10823c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) && 10833c0d9c63SKent Gibson !(flags & GPIO_V2_LINE_FLAG_OUTPUT)) 10843c0d9c63SKent Gibson return -EINVAL; 10853c0d9c63SKent Gibson 10863c0d9c63SKent Gibson /* Bias requires explicit direction. */ 10873c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_BIAS_FLAGS) && 10883c0d9c63SKent Gibson !(flags & GPIO_V2_LINE_DIRECTION_FLAGS)) 10893c0d9c63SKent Gibson return -EINVAL; 10903c0d9c63SKent Gibson 10913c0d9c63SKent Gibson /* Only one bias flag can be set. */ 10923c0d9c63SKent Gibson if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) && 10933c0d9c63SKent Gibson (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | 10943c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) || 10953c0d9c63SKent Gibson ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) && 10963c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) 10973c0d9c63SKent Gibson return -EINVAL; 10983c0d9c63SKent Gibson 10993c0d9c63SKent Gibson return 0; 11003c0d9c63SKent Gibson } 11013c0d9c63SKent Gibson 11023c0d9c63SKent Gibson static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, 11033c0d9c63SKent Gibson unsigned int num_lines) 11043c0d9c63SKent Gibson { 11053c0d9c63SKent Gibson unsigned int i; 11063c0d9c63SKent Gibson u64 flags; 11073c0d9c63SKent Gibson int ret; 11083c0d9c63SKent Gibson 11093c0d9c63SKent Gibson if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX) 11103c0d9c63SKent Gibson return -EINVAL; 11113c0d9c63SKent Gibson 11123c0d9c63SKent Gibson if (memchr_inv(lc->padding, 0, sizeof(lc->padding))) 11133c0d9c63SKent Gibson return -EINVAL; 11143c0d9c63SKent Gibson 11153c0d9c63SKent Gibson for (i = 0; i < num_lines; i++) { 11163c0d9c63SKent Gibson flags = gpio_v2_line_config_flags(lc, i); 11173c0d9c63SKent Gibson ret = gpio_v2_line_flags_validate(flags); 11183c0d9c63SKent Gibson if (ret) 11193c0d9c63SKent Gibson return ret; 112065cff704SKent Gibson 112165cff704SKent Gibson /* debounce requires explicit input */ 112265cff704SKent Gibson if (gpio_v2_line_config_debounced(lc, i) && 112365cff704SKent Gibson !(flags & GPIO_V2_LINE_FLAG_INPUT)) 112465cff704SKent Gibson return -EINVAL; 11253c0d9c63SKent Gibson } 11263c0d9c63SKent Gibson return 0; 11273c0d9c63SKent Gibson } 11283c0d9c63SKent Gibson 11293c0d9c63SKent Gibson static void gpio_v2_line_config_flags_to_desc_flags(u64 flags, 11303c0d9c63SKent Gibson unsigned long *flagsp) 11313c0d9c63SKent Gibson { 11323c0d9c63SKent Gibson assign_bit(FLAG_ACTIVE_LOW, flagsp, 11333c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW); 11343c0d9c63SKent Gibson 11353c0d9c63SKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) 11363c0d9c63SKent Gibson set_bit(FLAG_IS_OUT, flagsp); 11373c0d9c63SKent Gibson else if (flags & GPIO_V2_LINE_FLAG_INPUT) 11383c0d9c63SKent Gibson clear_bit(FLAG_IS_OUT, flagsp); 11393c0d9c63SKent Gibson 114073e03419SKent Gibson assign_bit(FLAG_EDGE_RISING, flagsp, 114173e03419SKent Gibson flags & GPIO_V2_LINE_FLAG_EDGE_RISING); 114273e03419SKent Gibson assign_bit(FLAG_EDGE_FALLING, flagsp, 114373e03419SKent Gibson flags & GPIO_V2_LINE_FLAG_EDGE_FALLING); 114473e03419SKent Gibson 11453c0d9c63SKent Gibson assign_bit(FLAG_OPEN_DRAIN, flagsp, 11463c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN); 11473c0d9c63SKent Gibson assign_bit(FLAG_OPEN_SOURCE, flagsp, 11483c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE); 11493c0d9c63SKent Gibson 11503c0d9c63SKent Gibson assign_bit(FLAG_PULL_UP, flagsp, 11513c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP); 11523c0d9c63SKent Gibson assign_bit(FLAG_PULL_DOWN, flagsp, 11533c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN); 11543c0d9c63SKent Gibson assign_bit(FLAG_BIAS_DISABLE, flagsp, 11553c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED); 115626d060e4SKent Gibson 115726d060e4SKent Gibson assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp, 115826d060e4SKent Gibson flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME); 11592068339aSDipen Patel assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp, 11602068339aSDipen Patel flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE); 11613c0d9c63SKent Gibson } 11623c0d9c63SKent Gibson 11633c0d9c63SKent Gibson static long linereq_get_values(struct linereq *lr, void __user *ip) 11643c0d9c63SKent Gibson { 11653c0d9c63SKent Gibson struct gpio_v2_line_values lv; 11663c0d9c63SKent Gibson DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); 11673c0d9c63SKent Gibson struct gpio_desc **descs; 11683c0d9c63SKent Gibson unsigned int i, didx, num_get; 116965cff704SKent Gibson bool val; 11703c0d9c63SKent Gibson int ret; 11713c0d9c63SKent Gibson 11723c0d9c63SKent Gibson /* NOTE: It's ok to read values of output lines. */ 11733c0d9c63SKent Gibson if (copy_from_user(&lv, ip, sizeof(lv))) 11743c0d9c63SKent Gibson return -EFAULT; 11753c0d9c63SKent Gibson 11763c0d9c63SKent Gibson for (num_get = 0, i = 0; i < lr->num_lines; i++) { 11773c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 11783c0d9c63SKent Gibson num_get++; 11793c0d9c63SKent Gibson descs = &lr->lines[i].desc; 11803c0d9c63SKent Gibson } 11813c0d9c63SKent Gibson } 11823c0d9c63SKent Gibson 11833c0d9c63SKent Gibson if (num_get == 0) 11843c0d9c63SKent Gibson return -EINVAL; 11853c0d9c63SKent Gibson 11863c0d9c63SKent Gibson if (num_get != 1) { 11873c0d9c63SKent Gibson descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL); 11883c0d9c63SKent Gibson if (!descs) 11893c0d9c63SKent Gibson return -ENOMEM; 11903c0d9c63SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 11913c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 11923c0d9c63SKent Gibson descs[didx] = lr->lines[i].desc; 11933c0d9c63SKent Gibson didx++; 11943c0d9c63SKent Gibson } 11953c0d9c63SKent Gibson } 11963c0d9c63SKent Gibson } 11973c0d9c63SKent Gibson ret = gpiod_get_array_value_complex(false, true, num_get, 11983c0d9c63SKent Gibson descs, NULL, vals); 11993c0d9c63SKent Gibson 12003c0d9c63SKent Gibson if (num_get != 1) 12013c0d9c63SKent Gibson kfree(descs); 12023c0d9c63SKent Gibson if (ret) 12033c0d9c63SKent Gibson return ret; 12043c0d9c63SKent Gibson 12053c0d9c63SKent Gibson lv.bits = 0; 12063c0d9c63SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 12073c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 120865cff704SKent Gibson if (lr->lines[i].sw_debounced) 120965cff704SKent Gibson val = debounced_value(&lr->lines[i]); 121065cff704SKent Gibson else 121165cff704SKent Gibson val = test_bit(didx, vals); 121265cff704SKent Gibson if (val) 12133c0d9c63SKent Gibson lv.bits |= BIT_ULL(i); 12143c0d9c63SKent Gibson didx++; 12153c0d9c63SKent Gibson } 12163c0d9c63SKent Gibson } 12173c0d9c63SKent Gibson 12183c0d9c63SKent Gibson if (copy_to_user(ip, &lv, sizeof(lv))) 12193c0d9c63SKent Gibson return -EFAULT; 12203c0d9c63SKent Gibson 12213c0d9c63SKent Gibson return 0; 12223c0d9c63SKent Gibson } 12233c0d9c63SKent Gibson 12247b8e00d9SKent Gibson static long linereq_set_values_unlocked(struct linereq *lr, 12257b8e00d9SKent Gibson struct gpio_v2_line_values *lv) 12267b8e00d9SKent Gibson { 12277b8e00d9SKent Gibson DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); 12287b8e00d9SKent Gibson struct gpio_desc **descs; 12297b8e00d9SKent Gibson unsigned int i, didx, num_set; 12307b8e00d9SKent Gibson int ret; 12317b8e00d9SKent Gibson 12327b8e00d9SKent Gibson bitmap_zero(vals, GPIO_V2_LINES_MAX); 12337b8e00d9SKent Gibson for (num_set = 0, i = 0; i < lr->num_lines; i++) { 12347b8e00d9SKent Gibson if (lv->mask & BIT_ULL(i)) { 12357b8e00d9SKent Gibson if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags)) 12367b8e00d9SKent Gibson return -EPERM; 12377b8e00d9SKent Gibson if (lv->bits & BIT_ULL(i)) 12387b8e00d9SKent Gibson __set_bit(num_set, vals); 12397b8e00d9SKent Gibson num_set++; 12407b8e00d9SKent Gibson descs = &lr->lines[i].desc; 12417b8e00d9SKent Gibson } 12427b8e00d9SKent Gibson } 12437b8e00d9SKent Gibson if (num_set == 0) 12447b8e00d9SKent Gibson return -EINVAL; 12457b8e00d9SKent Gibson 12467b8e00d9SKent Gibson if (num_set != 1) { 12477b8e00d9SKent Gibson /* build compacted desc array and values */ 12487b8e00d9SKent Gibson descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL); 12497b8e00d9SKent Gibson if (!descs) 12507b8e00d9SKent Gibson return -ENOMEM; 12517b8e00d9SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 12527b8e00d9SKent Gibson if (lv->mask & BIT_ULL(i)) { 12537b8e00d9SKent Gibson descs[didx] = lr->lines[i].desc; 12547b8e00d9SKent Gibson didx++; 12557b8e00d9SKent Gibson } 12567b8e00d9SKent Gibson } 12577b8e00d9SKent Gibson } 12587b8e00d9SKent Gibson ret = gpiod_set_array_value_complex(false, true, num_set, 12597b8e00d9SKent Gibson descs, NULL, vals); 12607b8e00d9SKent Gibson 12617b8e00d9SKent Gibson if (num_set != 1) 12627b8e00d9SKent Gibson kfree(descs); 12637b8e00d9SKent Gibson return ret; 12647b8e00d9SKent Gibson } 12657b8e00d9SKent Gibson 12667b8e00d9SKent Gibson static long linereq_set_values(struct linereq *lr, void __user *ip) 12677b8e00d9SKent Gibson { 12687b8e00d9SKent Gibson struct gpio_v2_line_values lv; 12697b8e00d9SKent Gibson int ret; 12707b8e00d9SKent Gibson 12717b8e00d9SKent Gibson if (copy_from_user(&lv, ip, sizeof(lv))) 12727b8e00d9SKent Gibson return -EFAULT; 12737b8e00d9SKent Gibson 12747b8e00d9SKent Gibson mutex_lock(&lr->config_mutex); 12757b8e00d9SKent Gibson 12767b8e00d9SKent Gibson ret = linereq_set_values_unlocked(lr, &lv); 12777b8e00d9SKent Gibson 12787b8e00d9SKent Gibson mutex_unlock(&lr->config_mutex); 12797b8e00d9SKent Gibson 12807b8e00d9SKent Gibson return ret; 12817b8e00d9SKent Gibson } 12827b8e00d9SKent Gibson 1283a54756cbSKent Gibson static long linereq_set_config_unlocked(struct linereq *lr, 1284a54756cbSKent Gibson struct gpio_v2_line_config *lc) 1285a54756cbSKent Gibson { 1286a54756cbSKent Gibson struct gpio_desc *desc; 1287*b1a92e94SKent Gibson struct line *line; 1288a54756cbSKent Gibson unsigned int i; 1289*b1a92e94SKent Gibson u64 flags, edflags; 1290a54756cbSKent Gibson int ret; 1291a54756cbSKent Gibson 1292a54756cbSKent Gibson for (i = 0; i < lr->num_lines; i++) { 1293*b1a92e94SKent Gibson line = &lr->lines[i]; 1294a54756cbSKent Gibson desc = lr->lines[i].desc; 1295a54756cbSKent Gibson flags = gpio_v2_line_config_flags(lc, i); 1296a54756cbSKent Gibson gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 1297*b1a92e94SKent Gibson edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS; 1298a54756cbSKent Gibson /* 1299a54756cbSKent Gibson * Lines have to be requested explicitly for input 1300a54756cbSKent Gibson * or output, else the line will be treated "as is". 1301a54756cbSKent Gibson */ 1302a54756cbSKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 1303a54756cbSKent Gibson int val = gpio_v2_line_config_output_value(lc, i); 1304a54756cbSKent Gibson 1305*b1a92e94SKent Gibson edge_detector_stop(line); 1306a54756cbSKent Gibson ret = gpiod_direction_output(desc, val); 1307a54756cbSKent Gibson if (ret) 1308a54756cbSKent Gibson return ret; 1309a54756cbSKent Gibson } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { 1310a54756cbSKent Gibson ret = gpiod_direction_input(desc); 1311a54756cbSKent Gibson if (ret) 1312a54756cbSKent Gibson return ret; 1313a54756cbSKent Gibson 1314*b1a92e94SKent Gibson ret = edge_detector_update(line, lc, i, edflags); 1315a54756cbSKent Gibson if (ret) 1316a54756cbSKent Gibson return ret; 1317a54756cbSKent Gibson } 1318a54756cbSKent Gibson 1319*b1a92e94SKent Gibson WRITE_ONCE(line->edflags, edflags); 1320*b1a92e94SKent Gibson 1321a54756cbSKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 1322a54756cbSKent Gibson GPIO_V2_LINE_CHANGED_CONFIG, 1323a54756cbSKent Gibson desc); 1324a54756cbSKent Gibson } 1325a54756cbSKent Gibson return 0; 1326a54756cbSKent Gibson } 1327a54756cbSKent Gibson 1328a54756cbSKent Gibson static long linereq_set_config(struct linereq *lr, void __user *ip) 1329a54756cbSKent Gibson { 1330a54756cbSKent Gibson struct gpio_v2_line_config lc; 1331a54756cbSKent Gibson int ret; 1332a54756cbSKent Gibson 1333a54756cbSKent Gibson if (copy_from_user(&lc, ip, sizeof(lc))) 1334a54756cbSKent Gibson return -EFAULT; 1335a54756cbSKent Gibson 1336a54756cbSKent Gibson ret = gpio_v2_line_config_validate(&lc, lr->num_lines); 1337a54756cbSKent Gibson if (ret) 1338a54756cbSKent Gibson return ret; 1339a54756cbSKent Gibson 1340a54756cbSKent Gibson mutex_lock(&lr->config_mutex); 1341a54756cbSKent Gibson 1342a54756cbSKent Gibson ret = linereq_set_config_unlocked(lr, &lc); 1343a54756cbSKent Gibson 1344a54756cbSKent Gibson mutex_unlock(&lr->config_mutex); 1345a54756cbSKent Gibson 1346a54756cbSKent Gibson return ret; 1347a54756cbSKent Gibson } 1348a54756cbSKent Gibson 13493c0d9c63SKent Gibson static long linereq_ioctl(struct file *file, unsigned int cmd, 13503c0d9c63SKent Gibson unsigned long arg) 13513c0d9c63SKent Gibson { 13523c0d9c63SKent Gibson struct linereq *lr = file->private_data; 13533c0d9c63SKent Gibson void __user *ip = (void __user *)arg; 13543c0d9c63SKent Gibson 13551cef8b50SAndy Shevchenko switch (cmd) { 13561cef8b50SAndy Shevchenko case GPIO_V2_LINE_GET_VALUES_IOCTL: 13573c0d9c63SKent Gibson return linereq_get_values(lr, ip); 13581cef8b50SAndy Shevchenko case GPIO_V2_LINE_SET_VALUES_IOCTL: 13597b8e00d9SKent Gibson return linereq_set_values(lr, ip); 13601cef8b50SAndy Shevchenko case GPIO_V2_LINE_SET_CONFIG_IOCTL: 1361a54756cbSKent Gibson return linereq_set_config(lr, ip); 13621cef8b50SAndy Shevchenko default: 13633c0d9c63SKent Gibson return -EINVAL; 13643c0d9c63SKent Gibson } 13651cef8b50SAndy Shevchenko } 13663c0d9c63SKent Gibson 13673c0d9c63SKent Gibson #ifdef CONFIG_COMPAT 13683c0d9c63SKent Gibson static long linereq_ioctl_compat(struct file *file, unsigned int cmd, 13693c0d9c63SKent Gibson unsigned long arg) 13703c0d9c63SKent Gibson { 13713c0d9c63SKent Gibson return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 13723c0d9c63SKent Gibson } 13733c0d9c63SKent Gibson #endif 13743c0d9c63SKent Gibson 137573e03419SKent Gibson static __poll_t linereq_poll(struct file *file, 137673e03419SKent Gibson struct poll_table_struct *wait) 137773e03419SKent Gibson { 137873e03419SKent Gibson struct linereq *lr = file->private_data; 137973e03419SKent Gibson __poll_t events = 0; 138073e03419SKent Gibson 138173e03419SKent Gibson poll_wait(file, &lr->wait, wait); 138273e03419SKent Gibson 138373e03419SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events, 138473e03419SKent Gibson &lr->wait.lock)) 138573e03419SKent Gibson events = EPOLLIN | EPOLLRDNORM; 138673e03419SKent Gibson 138773e03419SKent Gibson return events; 138873e03419SKent Gibson } 138973e03419SKent Gibson 139073e03419SKent Gibson static ssize_t linereq_read(struct file *file, 139173e03419SKent Gibson char __user *buf, 139273e03419SKent Gibson size_t count, 139373e03419SKent Gibson loff_t *f_ps) 139473e03419SKent Gibson { 139573e03419SKent Gibson struct linereq *lr = file->private_data; 139673e03419SKent Gibson struct gpio_v2_line_event le; 139773e03419SKent Gibson ssize_t bytes_read = 0; 139873e03419SKent Gibson int ret; 139973e03419SKent Gibson 140073e03419SKent Gibson if (count < sizeof(le)) 140173e03419SKent Gibson return -EINVAL; 140273e03419SKent Gibson 140373e03419SKent Gibson do { 140473e03419SKent Gibson spin_lock(&lr->wait.lock); 140573e03419SKent Gibson if (kfifo_is_empty(&lr->events)) { 140673e03419SKent Gibson if (bytes_read) { 140773e03419SKent Gibson spin_unlock(&lr->wait.lock); 140873e03419SKent Gibson return bytes_read; 140973e03419SKent Gibson } 141073e03419SKent Gibson 141173e03419SKent Gibson if (file->f_flags & O_NONBLOCK) { 141273e03419SKent Gibson spin_unlock(&lr->wait.lock); 141373e03419SKent Gibson return -EAGAIN; 141473e03419SKent Gibson } 141573e03419SKent Gibson 141673e03419SKent Gibson ret = wait_event_interruptible_locked(lr->wait, 141773e03419SKent Gibson !kfifo_is_empty(&lr->events)); 141873e03419SKent Gibson if (ret) { 141973e03419SKent Gibson spin_unlock(&lr->wait.lock); 142073e03419SKent Gibson return ret; 142173e03419SKent Gibson } 142273e03419SKent Gibson } 142373e03419SKent Gibson 142473e03419SKent Gibson ret = kfifo_out(&lr->events, &le, 1); 142573e03419SKent Gibson spin_unlock(&lr->wait.lock); 142673e03419SKent Gibson if (ret != 1) { 142773e03419SKent Gibson /* 142873e03419SKent Gibson * This should never happen - we were holding the 142973e03419SKent Gibson * lock from the moment we learned the fifo is no 143073e03419SKent Gibson * longer empty until now. 143173e03419SKent Gibson */ 143273e03419SKent Gibson ret = -EIO; 143373e03419SKent Gibson break; 143473e03419SKent Gibson } 143573e03419SKent Gibson 143673e03419SKent Gibson if (copy_to_user(buf + bytes_read, &le, sizeof(le))) 143773e03419SKent Gibson return -EFAULT; 143873e03419SKent Gibson bytes_read += sizeof(le); 143973e03419SKent Gibson } while (count >= bytes_read + sizeof(le)); 144073e03419SKent Gibson 144173e03419SKent Gibson return bytes_read; 144273e03419SKent Gibson } 144373e03419SKent Gibson 14443c0d9c63SKent Gibson static void linereq_free(struct linereq *lr) 14453c0d9c63SKent Gibson { 14463c0d9c63SKent Gibson unsigned int i; 14473c0d9c63SKent Gibson 14483c0d9c63SKent Gibson for (i = 0; i < lr->num_lines; i++) { 1449160d6e40SKent Gibson if (lr->lines[i].desc) { 1450*b1a92e94SKent Gibson edge_detector_stop(&lr->lines[i]); 14513c0d9c63SKent Gibson gpiod_free(lr->lines[i].desc); 14523c0d9c63SKent Gibson } 1453160d6e40SKent Gibson } 145473e03419SKent Gibson kfifo_free(&lr->events); 14553c0d9c63SKent Gibson kfree(lr->label); 14563c0d9c63SKent Gibson put_device(&lr->gdev->dev); 14573c0d9c63SKent Gibson kfree(lr); 14583c0d9c63SKent Gibson } 14593c0d9c63SKent Gibson 14603c0d9c63SKent Gibson static int linereq_release(struct inode *inode, struct file *file) 14613c0d9c63SKent Gibson { 14623c0d9c63SKent Gibson struct linereq *lr = file->private_data; 14633c0d9c63SKent Gibson 14643c0d9c63SKent Gibson linereq_free(lr); 14653c0d9c63SKent Gibson return 0; 14663c0d9c63SKent Gibson } 14673c0d9c63SKent Gibson 14683c0d9c63SKent Gibson static const struct file_operations line_fileops = { 14693c0d9c63SKent Gibson .release = linereq_release, 147073e03419SKent Gibson .read = linereq_read, 147173e03419SKent Gibson .poll = linereq_poll, 14723c0d9c63SKent Gibson .owner = THIS_MODULE, 14733c0d9c63SKent Gibson .llseek = noop_llseek, 14743c0d9c63SKent Gibson .unlocked_ioctl = linereq_ioctl, 14753c0d9c63SKent Gibson #ifdef CONFIG_COMPAT 14763c0d9c63SKent Gibson .compat_ioctl = linereq_ioctl_compat, 14773c0d9c63SKent Gibson #endif 14783c0d9c63SKent Gibson }; 14793c0d9c63SKent Gibson 14803c0d9c63SKent Gibson static int linereq_create(struct gpio_device *gdev, void __user *ip) 14813c0d9c63SKent Gibson { 14823c0d9c63SKent Gibson struct gpio_v2_line_request ulr; 14833c0d9c63SKent Gibson struct gpio_v2_line_config *lc; 14843c0d9c63SKent Gibson struct linereq *lr; 14853c0d9c63SKent Gibson struct file *file; 1486*b1a92e94SKent Gibson u64 flags, edflags; 14873c0d9c63SKent Gibson unsigned int i; 14883c0d9c63SKent Gibson int fd, ret; 14893c0d9c63SKent Gibson 14903c0d9c63SKent Gibson if (copy_from_user(&ulr, ip, sizeof(ulr))) 14913c0d9c63SKent Gibson return -EFAULT; 14923c0d9c63SKent Gibson 14933c0d9c63SKent Gibson if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX)) 14943c0d9c63SKent Gibson return -EINVAL; 14953c0d9c63SKent Gibson 14963c0d9c63SKent Gibson if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding))) 14973c0d9c63SKent Gibson return -EINVAL; 14983c0d9c63SKent Gibson 14993c0d9c63SKent Gibson lc = &ulr.config; 15003c0d9c63SKent Gibson ret = gpio_v2_line_config_validate(lc, ulr.num_lines); 15013c0d9c63SKent Gibson if (ret) 15023c0d9c63SKent Gibson return ret; 15033c0d9c63SKent Gibson 15043c0d9c63SKent Gibson lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); 15053c0d9c63SKent Gibson if (!lr) 15063c0d9c63SKent Gibson return -ENOMEM; 15073c0d9c63SKent Gibson 15083c0d9c63SKent Gibson lr->gdev = gdev; 15093c0d9c63SKent Gibson get_device(&gdev->dev); 15103c0d9c63SKent Gibson 151165cff704SKent Gibson for (i = 0; i < ulr.num_lines; i++) { 151273e03419SKent Gibson lr->lines[i].req = lr; 151365cff704SKent Gibson WRITE_ONCE(lr->lines[i].sw_debounced, 0); 151465cff704SKent Gibson INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func); 151565cff704SKent Gibson } 151673e03419SKent Gibson 1517f188ac12SKent Gibson if (ulr.consumer[0] != '\0') { 15183c0d9c63SKent Gibson /* label is only initialized if consumer is set */ 1519f188ac12SKent Gibson lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1, 1520f188ac12SKent Gibson GFP_KERNEL); 15213c0d9c63SKent Gibson if (!lr->label) { 15223c0d9c63SKent Gibson ret = -ENOMEM; 15233c0d9c63SKent Gibson goto out_free_linereq; 15243c0d9c63SKent Gibson } 15253c0d9c63SKent Gibson } 15263c0d9c63SKent Gibson 1527a54756cbSKent Gibson mutex_init(&lr->config_mutex); 152873e03419SKent Gibson init_waitqueue_head(&lr->wait); 152973e03419SKent Gibson lr->event_buffer_size = ulr.event_buffer_size; 153073e03419SKent Gibson if (lr->event_buffer_size == 0) 153173e03419SKent Gibson lr->event_buffer_size = ulr.num_lines * 16; 153273e03419SKent Gibson else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16) 153373e03419SKent Gibson lr->event_buffer_size = GPIO_V2_LINES_MAX * 16; 153473e03419SKent Gibson 153573e03419SKent Gibson atomic_set(&lr->seqno, 0); 15363c0d9c63SKent Gibson lr->num_lines = ulr.num_lines; 15373c0d9c63SKent Gibson 15383c0d9c63SKent Gibson /* Request each GPIO */ 15393c0d9c63SKent Gibson for (i = 0; i < ulr.num_lines; i++) { 15403c0d9c63SKent Gibson u32 offset = ulr.offsets[i]; 15413c0d9c63SKent Gibson struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); 15423c0d9c63SKent Gibson 15433c0d9c63SKent Gibson if (IS_ERR(desc)) { 15443c0d9c63SKent Gibson ret = PTR_ERR(desc); 15453c0d9c63SKent Gibson goto out_free_linereq; 15463c0d9c63SKent Gibson } 15473c0d9c63SKent Gibson 154895a4eed7SAndy Shevchenko ret = gpiod_request_user(desc, lr->label); 15493c0d9c63SKent Gibson if (ret) 15503c0d9c63SKent Gibson goto out_free_linereq; 15513c0d9c63SKent Gibson 15523c0d9c63SKent Gibson lr->lines[i].desc = desc; 15533c0d9c63SKent Gibson flags = gpio_v2_line_config_flags(lc, i); 15543c0d9c63SKent Gibson gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 15553c0d9c63SKent Gibson 15563c0d9c63SKent Gibson ret = gpiod_set_transitory(desc, false); 15573c0d9c63SKent Gibson if (ret < 0) 15583c0d9c63SKent Gibson goto out_free_linereq; 15593c0d9c63SKent Gibson 1560*b1a92e94SKent Gibson edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS; 15613c0d9c63SKent Gibson /* 15623c0d9c63SKent Gibson * Lines have to be requested explicitly for input 15633c0d9c63SKent Gibson * or output, else the line will be treated "as is". 15643c0d9c63SKent Gibson */ 15653c0d9c63SKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 15663c0d9c63SKent Gibson int val = gpio_v2_line_config_output_value(lc, i); 15673c0d9c63SKent Gibson 15683c0d9c63SKent Gibson ret = gpiod_direction_output(desc, val); 15693c0d9c63SKent Gibson if (ret) 15703c0d9c63SKent Gibson goto out_free_linereq; 15713c0d9c63SKent Gibson } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { 15723c0d9c63SKent Gibson ret = gpiod_direction_input(desc); 15733c0d9c63SKent Gibson if (ret) 15743c0d9c63SKent Gibson goto out_free_linereq; 157573e03419SKent Gibson 157665cff704SKent Gibson ret = edge_detector_setup(&lr->lines[i], lc, i, 1577*b1a92e94SKent Gibson edflags); 157873e03419SKent Gibson if (ret) 157973e03419SKent Gibson goto out_free_linereq; 15803c0d9c63SKent Gibson } 15813c0d9c63SKent Gibson 1582*b1a92e94SKent Gibson lr->lines[i].edflags = edflags; 1583*b1a92e94SKent Gibson 15843c0d9c63SKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 1585aad95584SKent Gibson GPIO_V2_LINE_CHANGED_REQUESTED, desc); 15863c0d9c63SKent Gibson 15873c0d9c63SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", 15883c0d9c63SKent Gibson offset); 15893c0d9c63SKent Gibson } 15903c0d9c63SKent Gibson 15913c0d9c63SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 15923c0d9c63SKent Gibson if (fd < 0) { 15933c0d9c63SKent Gibson ret = fd; 15943c0d9c63SKent Gibson goto out_free_linereq; 15953c0d9c63SKent Gibson } 15963c0d9c63SKent Gibson 15973c0d9c63SKent Gibson file = anon_inode_getfile("gpio-line", &line_fileops, lr, 15983c0d9c63SKent Gibson O_RDONLY | O_CLOEXEC); 15993c0d9c63SKent Gibson if (IS_ERR(file)) { 16003c0d9c63SKent Gibson ret = PTR_ERR(file); 16013c0d9c63SKent Gibson goto out_put_unused_fd; 16023c0d9c63SKent Gibson } 16033c0d9c63SKent Gibson 16043c0d9c63SKent Gibson ulr.fd = fd; 16053c0d9c63SKent Gibson if (copy_to_user(ip, &ulr, sizeof(ulr))) { 16063c0d9c63SKent Gibson /* 16073c0d9c63SKent Gibson * fput() will trigger the release() callback, so do not go onto 16083c0d9c63SKent Gibson * the regular error cleanup path here. 16093c0d9c63SKent Gibson */ 16103c0d9c63SKent Gibson fput(file); 16113c0d9c63SKent Gibson put_unused_fd(fd); 16123c0d9c63SKent Gibson return -EFAULT; 16133c0d9c63SKent Gibson } 16143c0d9c63SKent Gibson 16153c0d9c63SKent Gibson fd_install(fd, file); 16163c0d9c63SKent Gibson 16173c0d9c63SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", 16183c0d9c63SKent Gibson lr->num_lines); 16193c0d9c63SKent Gibson 16203c0d9c63SKent Gibson return 0; 16213c0d9c63SKent Gibson 16223c0d9c63SKent Gibson out_put_unused_fd: 16233c0d9c63SKent Gibson put_unused_fd(fd); 16243c0d9c63SKent Gibson out_free_linereq: 16253c0d9c63SKent Gibson linereq_free(lr); 16263c0d9c63SKent Gibson return ret; 16273c0d9c63SKent Gibson } 16283c0d9c63SKent Gibson 16293c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 1630925ca369SKent Gibson 1631925ca369SKent Gibson /* 1632925ca369SKent Gibson * GPIO line event management 1633925ca369SKent Gibson */ 1634925ca369SKent Gibson 1635925ca369SKent Gibson /** 1636925ca369SKent Gibson * struct lineevent_state - contains the state of a userspace event 1637925ca369SKent Gibson * @gdev: the GPIO device the event pertains to 1638925ca369SKent Gibson * @label: consumer label used to tag descriptors 1639925ca369SKent Gibson * @desc: the GPIO descriptor held by this event 1640925ca369SKent Gibson * @eflags: the event flags this line was requested with 1641925ca369SKent Gibson * @irq: the interrupt that trigger in response to events on this GPIO 1642925ca369SKent Gibson * @wait: wait queue that handles blocking reads of events 1643925ca369SKent Gibson * @events: KFIFO for the GPIO events 1644925ca369SKent Gibson * @timestamp: cache for the timestamp storing it between hardirq 1645925ca369SKent Gibson * and IRQ thread, used to bring the timestamp close to the actual 1646925ca369SKent Gibson * event 1647925ca369SKent Gibson */ 1648925ca369SKent Gibson struct lineevent_state { 1649925ca369SKent Gibson struct gpio_device *gdev; 1650925ca369SKent Gibson const char *label; 1651925ca369SKent Gibson struct gpio_desc *desc; 1652925ca369SKent Gibson u32 eflags; 1653925ca369SKent Gibson int irq; 1654925ca369SKent Gibson wait_queue_head_t wait; 1655925ca369SKent Gibson DECLARE_KFIFO(events, struct gpioevent_data, 16); 1656925ca369SKent Gibson u64 timestamp; 1657925ca369SKent Gibson }; 1658925ca369SKent Gibson 1659925ca369SKent Gibson #define GPIOEVENT_REQUEST_VALID_FLAGS \ 1660925ca369SKent Gibson (GPIOEVENT_REQUEST_RISING_EDGE | \ 1661925ca369SKent Gibson GPIOEVENT_REQUEST_FALLING_EDGE) 1662925ca369SKent Gibson 166349bc5279SKent Gibson static __poll_t lineevent_poll(struct file *file, 1664925ca369SKent Gibson struct poll_table_struct *wait) 1665925ca369SKent Gibson { 166649bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1667925ca369SKent Gibson __poll_t events = 0; 1668925ca369SKent Gibson 166949bc5279SKent Gibson poll_wait(file, &le->wait, wait); 1670925ca369SKent Gibson 1671925ca369SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock)) 1672925ca369SKent Gibson events = EPOLLIN | EPOLLRDNORM; 1673925ca369SKent Gibson 1674925ca369SKent Gibson return events; 1675925ca369SKent Gibson } 1676925ca369SKent Gibson 16775ad284abSAndy Shevchenko struct compat_gpioeevent_data { 16785ad284abSAndy Shevchenko compat_u64 timestamp; 16795ad284abSAndy Shevchenko u32 id; 16805ad284abSAndy Shevchenko }; 16815ad284abSAndy Shevchenko 168249bc5279SKent Gibson static ssize_t lineevent_read(struct file *file, 1683925ca369SKent Gibson char __user *buf, 1684925ca369SKent Gibson size_t count, 1685925ca369SKent Gibson loff_t *f_ps) 1686925ca369SKent Gibson { 168749bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1688925ca369SKent Gibson struct gpioevent_data ge; 1689925ca369SKent Gibson ssize_t bytes_read = 0; 16905ad284abSAndy Shevchenko ssize_t ge_size; 1691925ca369SKent Gibson int ret; 1692925ca369SKent Gibson 16935ad284abSAndy Shevchenko /* 16945ad284abSAndy Shevchenko * When compatible system call is being used the struct gpioevent_data, 16955ad284abSAndy Shevchenko * in case of at least ia32, has different size due to the alignment 16965ad284abSAndy Shevchenko * differences. Because we have first member 64 bits followed by one of 16975ad284abSAndy Shevchenko * 32 bits there is no gap between them. The only difference is the 16985ad284abSAndy Shevchenko * padding at the end of the data structure. Hence, we calculate the 16995ad284abSAndy Shevchenko * actual sizeof() and pass this as an argument to copy_to_user() to 17005ad284abSAndy Shevchenko * drop unneeded bytes from the output. 17015ad284abSAndy Shevchenko */ 1702163d1719SAndy Shevchenko if (compat_need_64bit_alignment_fixup()) 1703163d1719SAndy Shevchenko ge_size = sizeof(struct compat_gpioeevent_data); 1704163d1719SAndy Shevchenko else 1705163d1719SAndy Shevchenko ge_size = sizeof(struct gpioevent_data); 17065ad284abSAndy Shevchenko if (count < ge_size) 1707925ca369SKent Gibson return -EINVAL; 1708925ca369SKent Gibson 1709925ca369SKent Gibson do { 1710925ca369SKent Gibson spin_lock(&le->wait.lock); 1711925ca369SKent Gibson if (kfifo_is_empty(&le->events)) { 1712925ca369SKent Gibson if (bytes_read) { 1713925ca369SKent Gibson spin_unlock(&le->wait.lock); 1714925ca369SKent Gibson return bytes_read; 1715925ca369SKent Gibson } 1716925ca369SKent Gibson 171749bc5279SKent Gibson if (file->f_flags & O_NONBLOCK) { 1718925ca369SKent Gibson spin_unlock(&le->wait.lock); 1719925ca369SKent Gibson return -EAGAIN; 1720925ca369SKent Gibson } 1721925ca369SKent Gibson 1722925ca369SKent Gibson ret = wait_event_interruptible_locked(le->wait, 1723925ca369SKent Gibson !kfifo_is_empty(&le->events)); 1724925ca369SKent Gibson if (ret) { 1725925ca369SKent Gibson spin_unlock(&le->wait.lock); 1726925ca369SKent Gibson return ret; 1727925ca369SKent Gibson } 1728925ca369SKent Gibson } 1729925ca369SKent Gibson 1730925ca369SKent Gibson ret = kfifo_out(&le->events, &ge, 1); 1731925ca369SKent Gibson spin_unlock(&le->wait.lock); 1732925ca369SKent Gibson if (ret != 1) { 1733925ca369SKent Gibson /* 1734925ca369SKent Gibson * This should never happen - we were holding the lock 1735925ca369SKent Gibson * from the moment we learned the fifo is no longer 1736925ca369SKent Gibson * empty until now. 1737925ca369SKent Gibson */ 1738925ca369SKent Gibson ret = -EIO; 1739925ca369SKent Gibson break; 1740925ca369SKent Gibson } 1741925ca369SKent Gibson 17425ad284abSAndy Shevchenko if (copy_to_user(buf + bytes_read, &ge, ge_size)) 1743925ca369SKent Gibson return -EFAULT; 17445ad284abSAndy Shevchenko bytes_read += ge_size; 17455ad284abSAndy Shevchenko } while (count >= bytes_read + ge_size); 1746925ca369SKent Gibson 1747925ca369SKent Gibson return bytes_read; 1748925ca369SKent Gibson } 1749925ca369SKent Gibson 175046824272SKent Gibson static void lineevent_free(struct lineevent_state *le) 1751925ca369SKent Gibson { 175246824272SKent Gibson if (le->irq) 1753925ca369SKent Gibson free_irq(le->irq, le); 175446824272SKent Gibson if (le->desc) 1755925ca369SKent Gibson gpiod_free(le->desc); 1756925ca369SKent Gibson kfree(le->label); 175746824272SKent Gibson put_device(&le->gdev->dev); 1758925ca369SKent Gibson kfree(le); 175946824272SKent Gibson } 176046824272SKent Gibson 176146824272SKent Gibson static int lineevent_release(struct inode *inode, struct file *file) 176246824272SKent Gibson { 176346824272SKent Gibson lineevent_free(file->private_data); 1764925ca369SKent Gibson return 0; 1765925ca369SKent Gibson } 1766925ca369SKent Gibson 176749bc5279SKent Gibson static long lineevent_ioctl(struct file *file, unsigned int cmd, 1768925ca369SKent Gibson unsigned long arg) 1769925ca369SKent Gibson { 177049bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1771925ca369SKent Gibson void __user *ip = (void __user *)arg; 1772925ca369SKent Gibson struct gpiohandle_data ghd; 1773925ca369SKent Gibson 1774925ca369SKent Gibson /* 1775925ca369SKent Gibson * We can get the value for an event line but not set it, 1776925ca369SKent Gibson * because it is input by definition. 1777925ca369SKent Gibson */ 1778925ca369SKent Gibson if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { 1779925ca369SKent Gibson int val; 1780925ca369SKent Gibson 1781925ca369SKent Gibson memset(&ghd, 0, sizeof(ghd)); 1782925ca369SKent Gibson 1783925ca369SKent Gibson val = gpiod_get_value_cansleep(le->desc); 1784925ca369SKent Gibson if (val < 0) 1785925ca369SKent Gibson return val; 1786925ca369SKent Gibson ghd.values[0] = val; 1787925ca369SKent Gibson 1788925ca369SKent Gibson if (copy_to_user(ip, &ghd, sizeof(ghd))) 1789925ca369SKent Gibson return -EFAULT; 1790925ca369SKent Gibson 1791925ca369SKent Gibson return 0; 1792925ca369SKent Gibson } 1793925ca369SKent Gibson return -EINVAL; 1794925ca369SKent Gibson } 1795925ca369SKent Gibson 1796925ca369SKent Gibson #ifdef CONFIG_COMPAT 179749bc5279SKent Gibson static long lineevent_ioctl_compat(struct file *file, unsigned int cmd, 1798925ca369SKent Gibson unsigned long arg) 1799925ca369SKent Gibson { 180049bc5279SKent Gibson return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 1801925ca369SKent Gibson } 1802925ca369SKent Gibson #endif 1803925ca369SKent Gibson 1804925ca369SKent Gibson static const struct file_operations lineevent_fileops = { 1805925ca369SKent Gibson .release = lineevent_release, 1806925ca369SKent Gibson .read = lineevent_read, 1807925ca369SKent Gibson .poll = lineevent_poll, 1808925ca369SKent Gibson .owner = THIS_MODULE, 1809925ca369SKent Gibson .llseek = noop_llseek, 1810925ca369SKent Gibson .unlocked_ioctl = lineevent_ioctl, 1811925ca369SKent Gibson #ifdef CONFIG_COMPAT 1812925ca369SKent Gibson .compat_ioctl = lineevent_ioctl_compat, 1813925ca369SKent Gibson #endif 1814925ca369SKent Gibson }; 1815925ca369SKent Gibson 1816925ca369SKent Gibson static irqreturn_t lineevent_irq_thread(int irq, void *p) 1817925ca369SKent Gibson { 1818925ca369SKent Gibson struct lineevent_state *le = p; 1819925ca369SKent Gibson struct gpioevent_data ge; 1820925ca369SKent Gibson int ret; 1821925ca369SKent Gibson 1822925ca369SKent Gibson /* Do not leak kernel stack to userspace */ 1823925ca369SKent Gibson memset(&ge, 0, sizeof(ge)); 1824925ca369SKent Gibson 1825925ca369SKent Gibson /* 1826925ca369SKent Gibson * We may be running from a nested threaded interrupt in which case 1827925ca369SKent Gibson * we didn't get the timestamp from lineevent_irq_handler(). 1828925ca369SKent Gibson */ 1829925ca369SKent Gibson if (!le->timestamp) 1830925ca369SKent Gibson ge.timestamp = ktime_get_ns(); 1831925ca369SKent Gibson else 1832925ca369SKent Gibson ge.timestamp = le->timestamp; 1833925ca369SKent Gibson 1834925ca369SKent Gibson if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE 1835925ca369SKent Gibson && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { 1836925ca369SKent Gibson int level = gpiod_get_value_cansleep(le->desc); 1837925ca369SKent Gibson 1838925ca369SKent Gibson if (level) 1839925ca369SKent Gibson /* Emit low-to-high event */ 1840925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_RISING_EDGE; 1841925ca369SKent Gibson else 1842925ca369SKent Gibson /* Emit high-to-low event */ 1843925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_FALLING_EDGE; 1844925ca369SKent Gibson } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) { 1845925ca369SKent Gibson /* Emit low-to-high event */ 1846925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_RISING_EDGE; 1847925ca369SKent Gibson } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { 1848925ca369SKent Gibson /* Emit high-to-low event */ 1849925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_FALLING_EDGE; 1850925ca369SKent Gibson } else { 1851925ca369SKent Gibson return IRQ_NONE; 1852925ca369SKent Gibson } 1853925ca369SKent Gibson 1854925ca369SKent Gibson ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge, 1855925ca369SKent Gibson 1, &le->wait.lock); 1856925ca369SKent Gibson if (ret) 1857925ca369SKent Gibson wake_up_poll(&le->wait, EPOLLIN); 1858925ca369SKent Gibson else 1859925ca369SKent Gibson pr_debug_ratelimited("event FIFO is full - event dropped\n"); 1860925ca369SKent Gibson 1861925ca369SKent Gibson return IRQ_HANDLED; 1862925ca369SKent Gibson } 1863925ca369SKent Gibson 1864925ca369SKent Gibson static irqreturn_t lineevent_irq_handler(int irq, void *p) 1865925ca369SKent Gibson { 1866925ca369SKent Gibson struct lineevent_state *le = p; 1867925ca369SKent Gibson 1868925ca369SKent Gibson /* 1869925ca369SKent Gibson * Just store the timestamp in hardirq context so we get it as 1870925ca369SKent Gibson * close in time as possible to the actual event. 1871925ca369SKent Gibson */ 1872925ca369SKent Gibson le->timestamp = ktime_get_ns(); 1873925ca369SKent Gibson 1874925ca369SKent Gibson return IRQ_WAKE_THREAD; 1875925ca369SKent Gibson } 1876925ca369SKent Gibson 1877925ca369SKent Gibson static int lineevent_create(struct gpio_device *gdev, void __user *ip) 1878925ca369SKent Gibson { 1879925ca369SKent Gibson struct gpioevent_request eventreq; 1880925ca369SKent Gibson struct lineevent_state *le; 1881925ca369SKent Gibson struct gpio_desc *desc; 1882925ca369SKent Gibson struct file *file; 1883925ca369SKent Gibson u32 offset; 1884925ca369SKent Gibson u32 lflags; 1885925ca369SKent Gibson u32 eflags; 1886925ca369SKent Gibson int fd; 1887925ca369SKent Gibson int ret; 188846824272SKent Gibson int irq, irqflags = 0; 1889925ca369SKent Gibson 1890925ca369SKent Gibson if (copy_from_user(&eventreq, ip, sizeof(eventreq))) 1891925ca369SKent Gibson return -EFAULT; 1892925ca369SKent Gibson 1893925ca369SKent Gibson offset = eventreq.lineoffset; 1894925ca369SKent Gibson lflags = eventreq.handleflags; 1895925ca369SKent Gibson eflags = eventreq.eventflags; 1896925ca369SKent Gibson 1897925ca369SKent Gibson desc = gpiochip_get_desc(gdev->chip, offset); 1898925ca369SKent Gibson if (IS_ERR(desc)) 1899925ca369SKent Gibson return PTR_ERR(desc); 1900925ca369SKent Gibson 1901925ca369SKent Gibson /* Return an error if a unknown flag is set */ 1902925ca369SKent Gibson if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || 1903925ca369SKent Gibson (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) 1904925ca369SKent Gibson return -EINVAL; 1905925ca369SKent Gibson 1906925ca369SKent Gibson /* This is just wrong: we don't look for events on output lines */ 1907925ca369SKent Gibson if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) || 1908925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || 1909925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) 1910925ca369SKent Gibson return -EINVAL; 1911925ca369SKent Gibson 1912925ca369SKent Gibson /* Only one bias flag can be set. */ 1913925ca369SKent Gibson if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && 1914925ca369SKent Gibson (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | 1915925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || 1916925ca369SKent Gibson ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && 1917925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) 1918925ca369SKent Gibson return -EINVAL; 1919925ca369SKent Gibson 1920925ca369SKent Gibson le = kzalloc(sizeof(*le), GFP_KERNEL); 1921925ca369SKent Gibson if (!le) 1922925ca369SKent Gibson return -ENOMEM; 1923925ca369SKent Gibson le->gdev = gdev; 1924925ca369SKent Gibson get_device(&gdev->dev); 1925925ca369SKent Gibson 1926f188ac12SKent Gibson if (eventreq.consumer_label[0] != '\0') { 1927f188ac12SKent Gibson /* label is only initialized if consumer_label is set */ 1928f188ac12SKent Gibson le->label = kstrndup(eventreq.consumer_label, 1929f188ac12SKent Gibson sizeof(eventreq.consumer_label) - 1, 1930925ca369SKent Gibson GFP_KERNEL); 1931925ca369SKent Gibson if (!le->label) { 1932925ca369SKent Gibson ret = -ENOMEM; 1933925ca369SKent Gibson goto out_free_le; 1934925ca369SKent Gibson } 1935925ca369SKent Gibson } 1936925ca369SKent Gibson 193795a4eed7SAndy Shevchenko ret = gpiod_request_user(desc, le->label); 1938925ca369SKent Gibson if (ret) 193946824272SKent Gibson goto out_free_le; 1940925ca369SKent Gibson le->desc = desc; 1941925ca369SKent Gibson le->eflags = eflags; 1942925ca369SKent Gibson 1943c274b58aSKent Gibson linehandle_flags_to_desc_flags(lflags, &desc->flags); 1944925ca369SKent Gibson 1945925ca369SKent Gibson ret = gpiod_direction_input(desc); 1946925ca369SKent Gibson if (ret) 194746824272SKent Gibson goto out_free_le; 1948925ca369SKent Gibson 19496accc376SKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 1950aad95584SKent Gibson GPIO_V2_LINE_CHANGED_REQUESTED, desc); 1951925ca369SKent Gibson 195246824272SKent Gibson irq = gpiod_to_irq(desc); 195346824272SKent Gibson if (irq <= 0) { 1954925ca369SKent Gibson ret = -ENODEV; 195546824272SKent Gibson goto out_free_le; 1956925ca369SKent Gibson } 195746824272SKent Gibson le->irq = irq; 1958925ca369SKent Gibson 1959925ca369SKent Gibson if (eflags & GPIOEVENT_REQUEST_RISING_EDGE) 1960925ca369SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 1961925ca369SKent Gibson IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 1962925ca369SKent Gibson if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE) 1963925ca369SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 1964925ca369SKent Gibson IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 1965925ca369SKent Gibson irqflags |= IRQF_ONESHOT; 1966925ca369SKent Gibson 1967925ca369SKent Gibson INIT_KFIFO(le->events); 1968925ca369SKent Gibson init_waitqueue_head(&le->wait); 1969925ca369SKent Gibson 1970925ca369SKent Gibson /* Request a thread to read the events */ 1971925ca369SKent Gibson ret = request_threaded_irq(le->irq, 1972925ca369SKent Gibson lineevent_irq_handler, 1973925ca369SKent Gibson lineevent_irq_thread, 1974925ca369SKent Gibson irqflags, 1975925ca369SKent Gibson le->label, 1976925ca369SKent Gibson le); 1977925ca369SKent Gibson if (ret) 197846824272SKent Gibson goto out_free_le; 1979925ca369SKent Gibson 1980925ca369SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 1981925ca369SKent Gibson if (fd < 0) { 1982925ca369SKent Gibson ret = fd; 198346824272SKent Gibson goto out_free_le; 1984925ca369SKent Gibson } 1985925ca369SKent Gibson 1986925ca369SKent Gibson file = anon_inode_getfile("gpio-event", 1987925ca369SKent Gibson &lineevent_fileops, 1988925ca369SKent Gibson le, 1989925ca369SKent Gibson O_RDONLY | O_CLOEXEC); 1990925ca369SKent Gibson if (IS_ERR(file)) { 1991925ca369SKent Gibson ret = PTR_ERR(file); 1992925ca369SKent Gibson goto out_put_unused_fd; 1993925ca369SKent Gibson } 1994925ca369SKent Gibson 1995925ca369SKent Gibson eventreq.fd = fd; 1996925ca369SKent Gibson if (copy_to_user(ip, &eventreq, sizeof(eventreq))) { 1997925ca369SKent Gibson /* 1998925ca369SKent Gibson * fput() will trigger the release() callback, so do not go onto 1999925ca369SKent Gibson * the regular error cleanup path here. 2000925ca369SKent Gibson */ 2001925ca369SKent Gibson fput(file); 2002925ca369SKent Gibson put_unused_fd(fd); 2003925ca369SKent Gibson return -EFAULT; 2004925ca369SKent Gibson } 2005925ca369SKent Gibson 2006925ca369SKent Gibson fd_install(fd, file); 2007925ca369SKent Gibson 2008925ca369SKent Gibson return 0; 2009925ca369SKent Gibson 2010925ca369SKent Gibson out_put_unused_fd: 2011925ca369SKent Gibson put_unused_fd(fd); 2012925ca369SKent Gibson out_free_le: 201346824272SKent Gibson lineevent_free(le); 2014925ca369SKent Gibson return ret; 2015925ca369SKent Gibson } 2016925ca369SKent Gibson 2017aad95584SKent Gibson static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2, 2018aad95584SKent Gibson struct gpioline_info *info_v1) 2019aad95584SKent Gibson { 2020aad95584SKent Gibson u64 flagsv2 = info_v2->flags; 2021aad95584SKent Gibson 2022aad95584SKent Gibson memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name)); 2023aad95584SKent Gibson memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer)); 2024aad95584SKent Gibson info_v1->line_offset = info_v2->offset; 2025aad95584SKent Gibson info_v1->flags = 0; 2026aad95584SKent Gibson 2027aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_USED) 2028aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_KERNEL; 2029aad95584SKent Gibson 2030aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT) 2031aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_IS_OUT; 2032aad95584SKent Gibson 2033aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 2034aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW; 2035aad95584SKent Gibson 2036aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN) 2037aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN; 2038aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE) 2039aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE; 2040aad95584SKent Gibson 2041aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP) 2042aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; 2043aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) 2044aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; 2045aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED) 2046aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE; 2047aad95584SKent Gibson } 2048aad95584SKent Gibson 2049aad95584SKent Gibson static void gpio_v2_line_info_changed_to_v1( 2050aad95584SKent Gibson struct gpio_v2_line_info_changed *lic_v2, 2051aad95584SKent Gibson struct gpioline_info_changed *lic_v1) 2052aad95584SKent Gibson { 2053cb8f63b8SGabriel Knezek memset(lic_v1, 0, sizeof(*lic_v1)); 2054aad95584SKent Gibson gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info); 2055aad95584SKent Gibson lic_v1->timestamp = lic_v2->timestamp_ns; 2056aad95584SKent Gibson lic_v1->event_type = lic_v2->event_type; 2057aad95584SKent Gibson } 2058aad95584SKent Gibson 20593c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 20603c0d9c63SKent Gibson 2061925ca369SKent Gibson static void gpio_desc_to_lineinfo(struct gpio_desc *desc, 2062aad95584SKent Gibson struct gpio_v2_line_info *info) 2063925ca369SKent Gibson { 2064925ca369SKent Gibson struct gpio_chip *gc = desc->gdev->chip; 2065925ca369SKent Gibson bool ok_for_pinctrl; 2066925ca369SKent Gibson unsigned long flags; 206765cff704SKent Gibson u32 debounce_period_us; 206865cff704SKent Gibson unsigned int num_attrs = 0; 2069925ca369SKent Gibson 207069e4e136SKent Gibson memset(info, 0, sizeof(*info)); 2071aad95584SKent Gibson info->offset = gpio_chip_hwgpio(desc); 2072925ca369SKent Gibson 2073925ca369SKent Gibson /* 2074925ca369SKent Gibson * This function takes a mutex so we must check this before taking 2075925ca369SKent Gibson * the spinlock. 2076925ca369SKent Gibson * 2077925ca369SKent Gibson * FIXME: find a non-racy way to retrieve this information. Maybe a 2078925ca369SKent Gibson * lock common to both frameworks? 2079925ca369SKent Gibson */ 2080925ca369SKent Gibson ok_for_pinctrl = 2081aad95584SKent Gibson pinctrl_gpio_can_use_line(gc->base + info->offset); 2082925ca369SKent Gibson 2083925ca369SKent Gibson spin_lock_irqsave(&gpio_lock, flags); 2084925ca369SKent Gibson 208569e4e136SKent Gibson if (desc->name) 208669e4e136SKent Gibson strscpy(info->name, desc->name, sizeof(info->name)); 2087925ca369SKent Gibson 208869e4e136SKent Gibson if (desc->label) 208969e4e136SKent Gibson strscpy(info->consumer, desc->label, sizeof(info->consumer)); 2090925ca369SKent Gibson 2091925ca369SKent Gibson /* 2092925ca369SKent Gibson * Userspace only need to know that the kernel is using this GPIO so 2093925ca369SKent Gibson * it can't use it. 2094925ca369SKent Gibson */ 2095925ca369SKent Gibson info->flags = 0; 2096925ca369SKent Gibson if (test_bit(FLAG_REQUESTED, &desc->flags) || 2097925ca369SKent Gibson test_bit(FLAG_IS_HOGGED, &desc->flags) || 2098925ca369SKent Gibson test_bit(FLAG_USED_AS_IRQ, &desc->flags) || 2099925ca369SKent Gibson test_bit(FLAG_EXPORT, &desc->flags) || 2100925ca369SKent Gibson test_bit(FLAG_SYSFS, &desc->flags) || 2101a0db197fSMarc Zyngier !gpiochip_line_is_valid(gc, info->offset) || 2102925ca369SKent Gibson !ok_for_pinctrl) 2103aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_USED; 2104aad95584SKent Gibson 2105925ca369SKent Gibson if (test_bit(FLAG_IS_OUT, &desc->flags)) 2106aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OUTPUT; 2107aad95584SKent Gibson else 2108aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_INPUT; 2109aad95584SKent Gibson 2110925ca369SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 2111aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW; 2112aad95584SKent Gibson 2113925ca369SKent Gibson if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 2114aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; 2115925ca369SKent Gibson if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 2116aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; 2117aad95584SKent Gibson 2118925ca369SKent Gibson if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) 2119aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED; 2120925ca369SKent Gibson if (test_bit(FLAG_PULL_DOWN, &desc->flags)) 2121aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN; 2122925ca369SKent Gibson if (test_bit(FLAG_PULL_UP, &desc->flags)) 2123aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; 2124925ca369SKent Gibson 212573e03419SKent Gibson if (test_bit(FLAG_EDGE_RISING, &desc->flags)) 212673e03419SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; 212773e03419SKent Gibson if (test_bit(FLAG_EDGE_FALLING, &desc->flags)) 212873e03419SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; 212973e03419SKent Gibson 213026d060e4SKent Gibson if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags)) 213126d060e4SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME; 21322068339aSDipen Patel else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags)) 21332068339aSDipen Patel info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE; 213426d060e4SKent Gibson 213565cff704SKent Gibson debounce_period_us = READ_ONCE(desc->debounce_period_us); 213665cff704SKent Gibson if (debounce_period_us) { 213765cff704SKent Gibson info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; 213865cff704SKent Gibson info->attrs[num_attrs].debounce_period_us = debounce_period_us; 213965cff704SKent Gibson num_attrs++; 214065cff704SKent Gibson } 214165cff704SKent Gibson info->num_attrs = num_attrs; 2142925ca369SKent Gibson 2143925ca369SKent Gibson spin_unlock_irqrestore(&gpio_lock, flags); 2144925ca369SKent Gibson } 2145925ca369SKent Gibson 2146925ca369SKent Gibson struct gpio_chardev_data { 2147925ca369SKent Gibson struct gpio_device *gdev; 2148925ca369SKent Gibson wait_queue_head_t wait; 2149aad95584SKent Gibson DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32); 2150925ca369SKent Gibson struct notifier_block lineinfo_changed_nb; 2151925ca369SKent Gibson unsigned long *watched_lines; 2152aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2153aad95584SKent Gibson atomic_t watch_abi_version; 2154aad95584SKent Gibson #endif 2155925ca369SKent Gibson }; 2156925ca369SKent Gibson 21572e202ad8SKent Gibson static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip) 21582e202ad8SKent Gibson { 21592e202ad8SKent Gibson struct gpio_device *gdev = cdev->gdev; 21602e202ad8SKent Gibson struct gpiochip_info chipinfo; 21612e202ad8SKent Gibson 21622e202ad8SKent Gibson memset(&chipinfo, 0, sizeof(chipinfo)); 21632e202ad8SKent Gibson 21642e202ad8SKent Gibson strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name)); 21652e202ad8SKent Gibson strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label)); 21662e202ad8SKent Gibson chipinfo.lines = gdev->ngpio; 21672e202ad8SKent Gibson if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) 21682e202ad8SKent Gibson return -EFAULT; 21692e202ad8SKent Gibson return 0; 21702e202ad8SKent Gibson } 21712e202ad8SKent Gibson 2172aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2173aad95584SKent Gibson /* 2174aad95584SKent Gibson * returns 0 if the versions match, else the previously selected ABI version 2175aad95584SKent Gibson */ 2176aad95584SKent Gibson static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata, 2177aad95584SKent Gibson unsigned int version) 2178aad95584SKent Gibson { 2179aad95584SKent Gibson int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version); 2180aad95584SKent Gibson 2181aad95584SKent Gibson if (abiv == version) 2182aad95584SKent Gibson return 0; 2183aad95584SKent Gibson 2184aad95584SKent Gibson return abiv; 2185aad95584SKent Gibson } 21862e202ad8SKent Gibson 21872e202ad8SKent Gibson static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip, 21882e202ad8SKent Gibson bool watch) 21892e202ad8SKent Gibson { 21902e202ad8SKent Gibson struct gpio_desc *desc; 21912e202ad8SKent Gibson struct gpioline_info lineinfo; 21922e202ad8SKent Gibson struct gpio_v2_line_info lineinfo_v2; 21932e202ad8SKent Gibson 21942e202ad8SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 21952e202ad8SKent Gibson return -EFAULT; 21962e202ad8SKent Gibson 21972e202ad8SKent Gibson /* this doubles as a range check on line_offset */ 21982e202ad8SKent Gibson desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset); 21992e202ad8SKent Gibson if (IS_ERR(desc)) 22002e202ad8SKent Gibson return PTR_ERR(desc); 22012e202ad8SKent Gibson 22022e202ad8SKent Gibson if (watch) { 22032e202ad8SKent Gibson if (lineinfo_ensure_abi_version(cdev, 1)) 22042e202ad8SKent Gibson return -EPERM; 22052e202ad8SKent Gibson 22062e202ad8SKent Gibson if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines)) 22072e202ad8SKent Gibson return -EBUSY; 22082e202ad8SKent Gibson } 22092e202ad8SKent Gibson 22102e202ad8SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo_v2); 22112e202ad8SKent Gibson gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); 22122e202ad8SKent Gibson 22132e202ad8SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 22142e202ad8SKent Gibson if (watch) 22152e202ad8SKent Gibson clear_bit(lineinfo.line_offset, cdev->watched_lines); 22162e202ad8SKent Gibson return -EFAULT; 22172e202ad8SKent Gibson } 22182e202ad8SKent Gibson 22192e202ad8SKent Gibson return 0; 22202e202ad8SKent Gibson } 2221aad95584SKent Gibson #endif 2222aad95584SKent Gibson 2223aad95584SKent Gibson static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip, 2224aad95584SKent Gibson bool watch) 2225aad95584SKent Gibson { 2226aad95584SKent Gibson struct gpio_desc *desc; 2227aad95584SKent Gibson struct gpio_v2_line_info lineinfo; 2228aad95584SKent Gibson 2229aad95584SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 2230aad95584SKent Gibson return -EFAULT; 2231aad95584SKent Gibson 2232aad95584SKent Gibson if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding))) 2233aad95584SKent Gibson return -EINVAL; 2234aad95584SKent Gibson 2235aad95584SKent Gibson desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset); 2236aad95584SKent Gibson if (IS_ERR(desc)) 2237aad95584SKent Gibson return PTR_ERR(desc); 2238aad95584SKent Gibson 2239aad95584SKent Gibson if (watch) { 2240aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2241aad95584SKent Gibson if (lineinfo_ensure_abi_version(cdev, 2)) 2242aad95584SKent Gibson return -EPERM; 2243aad95584SKent Gibson #endif 2244aad95584SKent Gibson if (test_and_set_bit(lineinfo.offset, cdev->watched_lines)) 2245aad95584SKent Gibson return -EBUSY; 2246aad95584SKent Gibson } 2247aad95584SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo); 2248aad95584SKent Gibson 2249aad95584SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 2250aad95584SKent Gibson if (watch) 2251aad95584SKent Gibson clear_bit(lineinfo.offset, cdev->watched_lines); 2252aad95584SKent Gibson return -EFAULT; 2253aad95584SKent Gibson } 2254aad95584SKent Gibson 2255aad95584SKent Gibson return 0; 2256aad95584SKent Gibson } 2257aad95584SKent Gibson 22582e202ad8SKent Gibson static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip) 2259925ca369SKent Gibson { 2260925ca369SKent Gibson __u32 offset; 2261925ca369SKent Gibson 2262925ca369SKent Gibson if (copy_from_user(&offset, ip, sizeof(offset))) 2263925ca369SKent Gibson return -EFAULT; 2264925ca369SKent Gibson 22651bf7ba40SKent Gibson if (offset >= cdev->gdev->ngpio) 22661bf7ba40SKent Gibson return -EINVAL; 2267925ca369SKent Gibson 22681bf7ba40SKent Gibson if (!test_and_clear_bit(offset, cdev->watched_lines)) 2269925ca369SKent Gibson return -EBUSY; 2270925ca369SKent Gibson 2271925ca369SKent Gibson return 0; 2272925ca369SKent Gibson } 22732e202ad8SKent Gibson 22742e202ad8SKent Gibson /* 22752e202ad8SKent Gibson * gpio_ioctl() - ioctl handler for the GPIO chardev 22762e202ad8SKent Gibson */ 22772e202ad8SKent Gibson static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 22782e202ad8SKent Gibson { 22792e202ad8SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 22802e202ad8SKent Gibson struct gpio_device *gdev = cdev->gdev; 22812e202ad8SKent Gibson void __user *ip = (void __user *)arg; 22822e202ad8SKent Gibson 22832e202ad8SKent Gibson /* We fail any subsequent ioctl():s when the chip is gone */ 22842e202ad8SKent Gibson if (!gdev->chip) 22852e202ad8SKent Gibson return -ENODEV; 22862e202ad8SKent Gibson 22872e202ad8SKent Gibson /* Fill in the struct and pass to userspace */ 22881cef8b50SAndy Shevchenko switch (cmd) { 22891cef8b50SAndy Shevchenko case GPIO_GET_CHIPINFO_IOCTL: 22902e202ad8SKent Gibson return chipinfo_get(cdev, ip); 22912e202ad8SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 22921cef8b50SAndy Shevchenko case GPIO_GET_LINEHANDLE_IOCTL: 22932e202ad8SKent Gibson return linehandle_create(gdev, ip); 22941cef8b50SAndy Shevchenko case GPIO_GET_LINEEVENT_IOCTL: 22952e202ad8SKent Gibson return lineevent_create(gdev, ip); 22961cef8b50SAndy Shevchenko case GPIO_GET_LINEINFO_IOCTL: 22971cef8b50SAndy Shevchenko return lineinfo_get_v1(cdev, ip, false); 22981cef8b50SAndy Shevchenko case GPIO_GET_LINEINFO_WATCH_IOCTL: 22991cef8b50SAndy Shevchenko return lineinfo_get_v1(cdev, ip, true); 23002e202ad8SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 23011cef8b50SAndy Shevchenko case GPIO_V2_GET_LINEINFO_IOCTL: 23021cef8b50SAndy Shevchenko return lineinfo_get(cdev, ip, false); 23031cef8b50SAndy Shevchenko case GPIO_V2_GET_LINEINFO_WATCH_IOCTL: 23041cef8b50SAndy Shevchenko return lineinfo_get(cdev, ip, true); 23051cef8b50SAndy Shevchenko case GPIO_V2_GET_LINE_IOCTL: 23062e202ad8SKent Gibson return linereq_create(gdev, ip); 23071cef8b50SAndy Shevchenko case GPIO_GET_LINEINFO_UNWATCH_IOCTL: 23082e202ad8SKent Gibson return lineinfo_unwatch(cdev, ip); 23091cef8b50SAndy Shevchenko default: 2310925ca369SKent Gibson return -EINVAL; 2311925ca369SKent Gibson } 23121cef8b50SAndy Shevchenko } 2313925ca369SKent Gibson 2314925ca369SKent Gibson #ifdef CONFIG_COMPAT 231549bc5279SKent Gibson static long gpio_ioctl_compat(struct file *file, unsigned int cmd, 2316925ca369SKent Gibson unsigned long arg) 2317925ca369SKent Gibson { 231849bc5279SKent Gibson return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 2319925ca369SKent Gibson } 2320925ca369SKent Gibson #endif 2321925ca369SKent Gibson 2322925ca369SKent Gibson static struct gpio_chardev_data * 2323925ca369SKent Gibson to_gpio_chardev_data(struct notifier_block *nb) 2324925ca369SKent Gibson { 2325925ca369SKent Gibson return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb); 2326925ca369SKent Gibson } 2327925ca369SKent Gibson 2328925ca369SKent Gibson static int lineinfo_changed_notify(struct notifier_block *nb, 2329925ca369SKent Gibson unsigned long action, void *data) 2330925ca369SKent Gibson { 2331e2b781c5SKent Gibson struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb); 2332aad95584SKent Gibson struct gpio_v2_line_info_changed chg; 2333925ca369SKent Gibson struct gpio_desc *desc = data; 2334925ca369SKent Gibson int ret; 2335925ca369SKent Gibson 2336e2b781c5SKent Gibson if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) 2337925ca369SKent Gibson return NOTIFY_DONE; 2338925ca369SKent Gibson 2339925ca369SKent Gibson memset(&chg, 0, sizeof(chg)); 2340925ca369SKent Gibson chg.event_type = action; 2341aad95584SKent Gibson chg.timestamp_ns = ktime_get_ns(); 2342925ca369SKent Gibson gpio_desc_to_lineinfo(desc, &chg.info); 2343925ca369SKent Gibson 2344e2b781c5SKent Gibson ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); 2345925ca369SKent Gibson if (ret) 2346e2b781c5SKent Gibson wake_up_poll(&cdev->wait, EPOLLIN); 2347925ca369SKent Gibson else 2348925ca369SKent Gibson pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n"); 2349925ca369SKent Gibson 2350925ca369SKent Gibson return NOTIFY_OK; 2351925ca369SKent Gibson } 2352925ca369SKent Gibson 235349bc5279SKent Gibson static __poll_t lineinfo_watch_poll(struct file *file, 2354925ca369SKent Gibson struct poll_table_struct *pollt) 2355925ca369SKent Gibson { 2356e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2357925ca369SKent Gibson __poll_t events = 0; 2358925ca369SKent Gibson 2359e2b781c5SKent Gibson poll_wait(file, &cdev->wait, pollt); 2360925ca369SKent Gibson 2361e2b781c5SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events, 2362e2b781c5SKent Gibson &cdev->wait.lock)) 2363925ca369SKent Gibson events = EPOLLIN | EPOLLRDNORM; 2364925ca369SKent Gibson 2365925ca369SKent Gibson return events; 2366925ca369SKent Gibson } 2367925ca369SKent Gibson 236849bc5279SKent Gibson static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, 2369925ca369SKent Gibson size_t count, loff_t *off) 2370925ca369SKent Gibson { 2371e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2372aad95584SKent Gibson struct gpio_v2_line_info_changed event; 2373925ca369SKent Gibson ssize_t bytes_read = 0; 2374925ca369SKent Gibson int ret; 2375aad95584SKent Gibson size_t event_size; 2376925ca369SKent Gibson 2377aad95584SKent Gibson #ifndef CONFIG_GPIO_CDEV_V1 2378aad95584SKent Gibson event_size = sizeof(struct gpio_v2_line_info_changed); 2379aad95584SKent Gibson if (count < event_size) 2380925ca369SKent Gibson return -EINVAL; 2381aad95584SKent Gibson #endif 2382925ca369SKent Gibson 2383925ca369SKent Gibson do { 2384e2b781c5SKent Gibson spin_lock(&cdev->wait.lock); 2385e2b781c5SKent Gibson if (kfifo_is_empty(&cdev->events)) { 2386925ca369SKent Gibson if (bytes_read) { 2387e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2388925ca369SKent Gibson return bytes_read; 2389925ca369SKent Gibson } 2390925ca369SKent Gibson 239149bc5279SKent Gibson if (file->f_flags & O_NONBLOCK) { 2392e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2393925ca369SKent Gibson return -EAGAIN; 2394925ca369SKent Gibson } 2395925ca369SKent Gibson 2396e2b781c5SKent Gibson ret = wait_event_interruptible_locked(cdev->wait, 2397e2b781c5SKent Gibson !kfifo_is_empty(&cdev->events)); 2398925ca369SKent Gibson if (ret) { 2399e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2400925ca369SKent Gibson return ret; 2401925ca369SKent Gibson } 2402925ca369SKent Gibson } 2403aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2404aad95584SKent Gibson /* must be after kfifo check so watch_abi_version is set */ 2405aad95584SKent Gibson if (atomic_read(&cdev->watch_abi_version) == 2) 2406aad95584SKent Gibson event_size = sizeof(struct gpio_v2_line_info_changed); 2407aad95584SKent Gibson else 2408aad95584SKent Gibson event_size = sizeof(struct gpioline_info_changed); 2409aad95584SKent Gibson if (count < event_size) { 2410aad95584SKent Gibson spin_unlock(&cdev->wait.lock); 2411aad95584SKent Gibson return -EINVAL; 2412aad95584SKent Gibson } 2413aad95584SKent Gibson #endif 2414e2b781c5SKent Gibson ret = kfifo_out(&cdev->events, &event, 1); 2415e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2416925ca369SKent Gibson if (ret != 1) { 2417925ca369SKent Gibson ret = -EIO; 2418925ca369SKent Gibson break; 2419925ca369SKent Gibson /* We should never get here. See lineevent_read(). */ 2420925ca369SKent Gibson } 2421925ca369SKent Gibson 2422aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2423aad95584SKent Gibson if (event_size == sizeof(struct gpio_v2_line_info_changed)) { 2424aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event, event_size)) 2425925ca369SKent Gibson return -EFAULT; 2426aad95584SKent Gibson } else { 2427aad95584SKent Gibson struct gpioline_info_changed event_v1; 2428aad95584SKent Gibson 2429aad95584SKent Gibson gpio_v2_line_info_changed_to_v1(&event, &event_v1); 2430aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event_v1, 2431aad95584SKent Gibson event_size)) 2432aad95584SKent Gibson return -EFAULT; 2433aad95584SKent Gibson } 2434aad95584SKent Gibson #else 2435aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event, event_size)) 2436aad95584SKent Gibson return -EFAULT; 2437aad95584SKent Gibson #endif 2438aad95584SKent Gibson bytes_read += event_size; 2439925ca369SKent Gibson } while (count >= bytes_read + sizeof(event)); 2440925ca369SKent Gibson 2441925ca369SKent Gibson return bytes_read; 2442925ca369SKent Gibson } 2443925ca369SKent Gibson 2444925ca369SKent Gibson /** 2445925ca369SKent Gibson * gpio_chrdev_open() - open the chardev for ioctl operations 2446925ca369SKent Gibson * @inode: inode for this chardev 244749bc5279SKent Gibson * @file: file struct for storing private data 2448925ca369SKent Gibson * Returns 0 on success 2449925ca369SKent Gibson */ 245049bc5279SKent Gibson static int gpio_chrdev_open(struct inode *inode, struct file *file) 2451925ca369SKent Gibson { 2452925ca369SKent Gibson struct gpio_device *gdev = container_of(inode->i_cdev, 2453925ca369SKent Gibson struct gpio_device, chrdev); 2454e2b781c5SKent Gibson struct gpio_chardev_data *cdev; 2455925ca369SKent Gibson int ret = -ENOMEM; 2456925ca369SKent Gibson 2457925ca369SKent Gibson /* Fail on open if the backing gpiochip is gone */ 2458925ca369SKent Gibson if (!gdev->chip) 2459925ca369SKent Gibson return -ENODEV; 2460925ca369SKent Gibson 2461e2b781c5SKent Gibson cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); 2462e2b781c5SKent Gibson if (!cdev) 2463925ca369SKent Gibson return -ENOMEM; 2464925ca369SKent Gibson 2465e2b781c5SKent Gibson cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL); 2466e2b781c5SKent Gibson if (!cdev->watched_lines) 2467e2b781c5SKent Gibson goto out_free_cdev; 2468925ca369SKent Gibson 2469e2b781c5SKent Gibson init_waitqueue_head(&cdev->wait); 2470e2b781c5SKent Gibson INIT_KFIFO(cdev->events); 2471e2b781c5SKent Gibson cdev->gdev = gdev; 2472925ca369SKent Gibson 2473e2b781c5SKent Gibson cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify; 24746accc376SKent Gibson ret = blocking_notifier_chain_register(&gdev->notifier, 2475e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2476925ca369SKent Gibson if (ret) 2477925ca369SKent Gibson goto out_free_bitmap; 2478925ca369SKent Gibson 2479925ca369SKent Gibson get_device(&gdev->dev); 2480e2b781c5SKent Gibson file->private_data = cdev; 2481925ca369SKent Gibson 248249bc5279SKent Gibson ret = nonseekable_open(inode, file); 2483925ca369SKent Gibson if (ret) 2484925ca369SKent Gibson goto out_unregister_notifier; 2485925ca369SKent Gibson 2486925ca369SKent Gibson return ret; 2487925ca369SKent Gibson 2488925ca369SKent Gibson out_unregister_notifier: 24896accc376SKent Gibson blocking_notifier_chain_unregister(&gdev->notifier, 2490e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2491925ca369SKent Gibson out_free_bitmap: 2492e2b781c5SKent Gibson bitmap_free(cdev->watched_lines); 2493e2b781c5SKent Gibson out_free_cdev: 2494e2b781c5SKent Gibson kfree(cdev); 2495925ca369SKent Gibson return ret; 2496925ca369SKent Gibson } 2497925ca369SKent Gibson 2498925ca369SKent Gibson /** 2499925ca369SKent Gibson * gpio_chrdev_release() - close chardev after ioctl operations 2500925ca369SKent Gibson * @inode: inode for this chardev 250149bc5279SKent Gibson * @file: file struct for storing private data 2502925ca369SKent Gibson * Returns 0 on success 2503925ca369SKent Gibson */ 250449bc5279SKent Gibson static int gpio_chrdev_release(struct inode *inode, struct file *file) 2505925ca369SKent Gibson { 2506e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2507e2b781c5SKent Gibson struct gpio_device *gdev = cdev->gdev; 2508925ca369SKent Gibson 2509e2b781c5SKent Gibson bitmap_free(cdev->watched_lines); 25106accc376SKent Gibson blocking_notifier_chain_unregister(&gdev->notifier, 2511e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2512925ca369SKent Gibson put_device(&gdev->dev); 2513e2b781c5SKent Gibson kfree(cdev); 2514925ca369SKent Gibson 2515925ca369SKent Gibson return 0; 2516925ca369SKent Gibson } 2517925ca369SKent Gibson 2518925ca369SKent Gibson static const struct file_operations gpio_fileops = { 2519925ca369SKent Gibson .release = gpio_chrdev_release, 2520925ca369SKent Gibson .open = gpio_chrdev_open, 2521925ca369SKent Gibson .poll = lineinfo_watch_poll, 2522925ca369SKent Gibson .read = lineinfo_watch_read, 2523925ca369SKent Gibson .owner = THIS_MODULE, 2524925ca369SKent Gibson .llseek = no_llseek, 2525925ca369SKent Gibson .unlocked_ioctl = gpio_ioctl, 2526925ca369SKent Gibson #ifdef CONFIG_COMPAT 2527925ca369SKent Gibson .compat_ioctl = gpio_ioctl_compat, 2528925ca369SKent Gibson #endif 2529925ca369SKent Gibson }; 2530925ca369SKent Gibson 2531925ca369SKent Gibson int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) 2532925ca369SKent Gibson { 2533925ca369SKent Gibson int ret; 2534925ca369SKent Gibson 2535925ca369SKent Gibson cdev_init(&gdev->chrdev, &gpio_fileops); 2536925ca369SKent Gibson gdev->chrdev.owner = THIS_MODULE; 2537925ca369SKent Gibson gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id); 2538925ca369SKent Gibson 2539925ca369SKent Gibson ret = cdev_device_add(&gdev->chrdev, &gdev->dev); 2540925ca369SKent Gibson if (ret) 2541925ca369SKent Gibson return ret; 2542925ca369SKent Gibson 2543925ca369SKent Gibson chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n", 2544925ca369SKent Gibson MAJOR(devt), gdev->id); 2545925ca369SKent Gibson 2546925ca369SKent Gibson return 0; 2547925ca369SKent Gibson } 2548925ca369SKent Gibson 2549925ca369SKent Gibson void gpiolib_cdev_unregister(struct gpio_device *gdev) 2550925ca369SKent Gibson { 2551925ca369SKent Gibson cdev_device_del(&gdev->chrdev, &gdev->dev); 2552925ca369SKent Gibson } 2553