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> 8*309364bcSKent Gibson #include <linux/cleanup.h> 9d189f627SKent Gibson #include <linux/compat.h> 1065cff704SKent Gibson #include <linux/compiler.h> 11925ca369SKent Gibson #include <linux/device.h> 12925ca369SKent Gibson #include <linux/err.h> 13d189f627SKent Gibson #include <linux/file.h> 14925ca369SKent Gibson #include <linux/gpio.h> 15925ca369SKent Gibson #include <linux/gpio/driver.h> 1652ee7c02SAndy Shevchenko #include <linux/hte.h> 17d189f627SKent Gibson #include <linux/interrupt.h> 18d189f627SKent Gibson #include <linux/irqreturn.h> 19d189f627SKent Gibson #include <linux/kernel.h> 20925ca369SKent Gibson #include <linux/kfifo.h> 21d189f627SKent Gibson #include <linux/module.h> 22a54756cbSKent Gibson #include <linux/mutex.h> 23d189f627SKent Gibson #include <linux/pinctrl/consumer.h> 24925ca369SKent Gibson #include <linux/poll.h> 25*309364bcSKent Gibson #include <linux/rbtree.h> 2652ee7c02SAndy Shevchenko #include <linux/seq_file.h> 27d189f627SKent Gibson #include <linux/spinlock.h> 28925ca369SKent Gibson #include <linux/timekeeping.h> 29d189f627SKent Gibson #include <linux/uaccess.h> 3065cff704SKent Gibson #include <linux/workqueue.h> 3152ee7c02SAndy Shevchenko 32925ca369SKent Gibson #include <uapi/linux/gpio.h> 33925ca369SKent Gibson 34925ca369SKent Gibson #include "gpiolib.h" 35925ca369SKent Gibson #include "gpiolib-cdev.h" 36925ca369SKent Gibson 373c0d9c63SKent Gibson /* 383c0d9c63SKent Gibson * Array sizes must ensure 64-bit alignment and not create holes in the 393c0d9c63SKent Gibson * struct packing. 403c0d9c63SKent Gibson */ 413c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2)); 423c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8)); 433c0d9c63SKent Gibson 443c0d9c63SKent Gibson /* 453c0d9c63SKent Gibson * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility 463c0d9c63SKent Gibson */ 473c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8)); 483c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8)); 493c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8)); 503c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8)); 513c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8)); 523c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8)); 533c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8)); 543c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8)); 553c0d9c63SKent Gibson 56925ca369SKent Gibson /* Character device interface to GPIO. 57925ca369SKent Gibson * 58925ca369SKent Gibson * The GPIO character device, /dev/gpiochipN, provides userspace an 59925ca369SKent Gibson * interface to gpiolib GPIOs via ioctl()s. 60925ca369SKent Gibson */ 61925ca369SKent Gibson 62bdbbae24SBartosz Golaszewski typedef __poll_t (*poll_fn)(struct file *, struct poll_table_struct *); 63bdbbae24SBartosz Golaszewski typedef long (*ioctl_fn)(struct file *, unsigned int, unsigned long); 64bdbbae24SBartosz Golaszewski typedef ssize_t (*read_fn)(struct file *, char __user *, 65bdbbae24SBartosz Golaszewski size_t count, loff_t *); 66bdbbae24SBartosz Golaszewski 67bdbbae24SBartosz Golaszewski static __poll_t call_poll_locked(struct file *file, 68bdbbae24SBartosz Golaszewski struct poll_table_struct *wait, 69bdbbae24SBartosz Golaszewski struct gpio_device *gdev, poll_fn func) 70bdbbae24SBartosz Golaszewski { 71bdbbae24SBartosz Golaszewski __poll_t ret; 72bdbbae24SBartosz Golaszewski 73bdbbae24SBartosz Golaszewski down_read(&gdev->sem); 74bdbbae24SBartosz Golaszewski ret = func(file, wait); 75bdbbae24SBartosz Golaszewski up_read(&gdev->sem); 76bdbbae24SBartosz Golaszewski 77bdbbae24SBartosz Golaszewski return ret; 78bdbbae24SBartosz Golaszewski } 79bdbbae24SBartosz Golaszewski 80bdbbae24SBartosz Golaszewski static long call_ioctl_locked(struct file *file, unsigned int cmd, 81bdbbae24SBartosz Golaszewski unsigned long arg, struct gpio_device *gdev, 82bdbbae24SBartosz Golaszewski ioctl_fn func) 83bdbbae24SBartosz Golaszewski { 84bdbbae24SBartosz Golaszewski long ret; 85bdbbae24SBartosz Golaszewski 86bdbbae24SBartosz Golaszewski down_read(&gdev->sem); 87bdbbae24SBartosz Golaszewski ret = func(file, cmd, arg); 88bdbbae24SBartosz Golaszewski up_read(&gdev->sem); 89bdbbae24SBartosz Golaszewski 90bdbbae24SBartosz Golaszewski return ret; 91bdbbae24SBartosz Golaszewski } 92bdbbae24SBartosz Golaszewski 93bdbbae24SBartosz Golaszewski static ssize_t call_read_locked(struct file *file, char __user *buf, 94bdbbae24SBartosz Golaszewski size_t count, loff_t *f_ps, 95bdbbae24SBartosz Golaszewski struct gpio_device *gdev, read_fn func) 96bdbbae24SBartosz Golaszewski { 97bdbbae24SBartosz Golaszewski ssize_t ret; 98bdbbae24SBartosz Golaszewski 99bdbbae24SBartosz Golaszewski down_read(&gdev->sem); 100bdbbae24SBartosz Golaszewski ret = func(file, buf, count, f_ps); 101bdbbae24SBartosz Golaszewski up_read(&gdev->sem); 102bdbbae24SBartosz Golaszewski 103bdbbae24SBartosz Golaszewski return ret; 104bdbbae24SBartosz Golaszewski } 105bdbbae24SBartosz Golaszewski 106925ca369SKent Gibson /* 107925ca369SKent Gibson * GPIO line handle management 108925ca369SKent Gibson */ 109925ca369SKent Gibson 1103c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 111925ca369SKent Gibson /** 112925ca369SKent Gibson * struct linehandle_state - contains the state of a userspace handle 113925ca369SKent Gibson * @gdev: the GPIO device the handle pertains to 114925ca369SKent Gibson * @label: consumer label used to tag descriptors 115925ca369SKent Gibson * @descs: the GPIO descriptors held by this handle 11652b7b596SKent Gibson * @num_descs: the number of descriptors held in the descs array 117925ca369SKent Gibson */ 118925ca369SKent Gibson struct linehandle_state { 119925ca369SKent Gibson struct gpio_device *gdev; 120925ca369SKent Gibson const char *label; 121925ca369SKent Gibson struct gpio_desc *descs[GPIOHANDLES_MAX]; 12252b7b596SKent Gibson u32 num_descs; 123925ca369SKent Gibson }; 124925ca369SKent Gibson 125925ca369SKent Gibson #define GPIOHANDLE_REQUEST_VALID_FLAGS \ 126925ca369SKent Gibson (GPIOHANDLE_REQUEST_INPUT | \ 127925ca369SKent Gibson GPIOHANDLE_REQUEST_OUTPUT | \ 128925ca369SKent Gibson GPIOHANDLE_REQUEST_ACTIVE_LOW | \ 129925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_UP | \ 130925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \ 131925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_DISABLE | \ 132925ca369SKent Gibson GPIOHANDLE_REQUEST_OPEN_DRAIN | \ 133925ca369SKent Gibson GPIOHANDLE_REQUEST_OPEN_SOURCE) 134925ca369SKent Gibson 135925ca369SKent Gibson static int linehandle_validate_flags(u32 flags) 136925ca369SKent Gibson { 137925ca369SKent Gibson /* Return an error if an unknown flag is set */ 138925ca369SKent Gibson if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) 139925ca369SKent Gibson return -EINVAL; 140925ca369SKent Gibson 141925ca369SKent Gibson /* 142925ca369SKent Gibson * Do not allow both INPUT & OUTPUT flags to be set as they are 143925ca369SKent Gibson * contradictory. 144925ca369SKent Gibson */ 145925ca369SKent Gibson if ((flags & GPIOHANDLE_REQUEST_INPUT) && 146925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OUTPUT)) 147925ca369SKent Gibson return -EINVAL; 148925ca369SKent Gibson 149925ca369SKent Gibson /* 150925ca369SKent Gibson * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If 151925ca369SKent Gibson * the hardware actually supports enabling both at the same time the 152925ca369SKent Gibson * electrical result would be disastrous. 153925ca369SKent Gibson */ 154925ca369SKent Gibson if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) && 155925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) 156925ca369SKent Gibson return -EINVAL; 157925ca369SKent Gibson 158925ca369SKent Gibson /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */ 159925ca369SKent Gibson if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) && 160925ca369SKent Gibson ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || 161925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))) 162925ca369SKent Gibson return -EINVAL; 163925ca369SKent Gibson 164925ca369SKent Gibson /* Bias flags only allowed for input or output mode. */ 165925ca369SKent Gibson if (!((flags & GPIOHANDLE_REQUEST_INPUT) || 166925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OUTPUT)) && 167925ca369SKent Gibson ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) || 168925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) || 169925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN))) 170925ca369SKent Gibson return -EINVAL; 171925ca369SKent Gibson 172925ca369SKent Gibson /* Only one bias flag can be set. */ 173925ca369SKent Gibson if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && 174925ca369SKent Gibson (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | 175925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || 176925ca369SKent Gibson ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && 177925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) 178925ca369SKent Gibson return -EINVAL; 179925ca369SKent Gibson 180925ca369SKent Gibson return 0; 181925ca369SKent Gibson } 182925ca369SKent Gibson 183c274b58aSKent Gibson static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp) 184c274b58aSKent Gibson { 185c274b58aSKent Gibson assign_bit(FLAG_ACTIVE_LOW, flagsp, 186c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW); 187c274b58aSKent Gibson assign_bit(FLAG_OPEN_DRAIN, flagsp, 188c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN); 189c274b58aSKent Gibson assign_bit(FLAG_OPEN_SOURCE, flagsp, 190c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE); 191c274b58aSKent Gibson assign_bit(FLAG_PULL_UP, flagsp, 192c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP); 193c274b58aSKent Gibson assign_bit(FLAG_PULL_DOWN, flagsp, 194c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN); 195c274b58aSKent Gibson assign_bit(FLAG_BIAS_DISABLE, flagsp, 196c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE); 197c274b58aSKent Gibson } 198c274b58aSKent Gibson 199925ca369SKent Gibson static long linehandle_set_config(struct linehandle_state *lh, 200925ca369SKent Gibson void __user *ip) 201925ca369SKent Gibson { 202925ca369SKent Gibson struct gpiohandle_config gcnf; 203925ca369SKent Gibson struct gpio_desc *desc; 204925ca369SKent Gibson int i, ret; 205925ca369SKent Gibson u32 lflags; 206925ca369SKent Gibson 207925ca369SKent Gibson if (copy_from_user(&gcnf, ip, sizeof(gcnf))) 208925ca369SKent Gibson return -EFAULT; 209925ca369SKent Gibson 210925ca369SKent Gibson lflags = gcnf.flags; 211925ca369SKent Gibson ret = linehandle_validate_flags(lflags); 212925ca369SKent Gibson if (ret) 213925ca369SKent Gibson return ret; 214925ca369SKent Gibson 21552b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) { 216925ca369SKent Gibson desc = lh->descs[i]; 217c274b58aSKent Gibson linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags); 218925ca369SKent Gibson 219925ca369SKent Gibson /* 220925ca369SKent Gibson * Lines have to be requested explicitly for input 221925ca369SKent Gibson * or output, else the line will be treated "as is". 222925ca369SKent Gibson */ 223925ca369SKent Gibson if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { 224925ca369SKent Gibson int val = !!gcnf.default_values[i]; 225925ca369SKent Gibson 226925ca369SKent Gibson ret = gpiod_direction_output(desc, val); 227925ca369SKent Gibson if (ret) 228925ca369SKent Gibson return ret; 229925ca369SKent Gibson } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { 230925ca369SKent Gibson ret = gpiod_direction_input(desc); 231925ca369SKent Gibson if (ret) 232925ca369SKent Gibson return ret; 233925ca369SKent Gibson } 234925ca369SKent Gibson 2359ce4ed5bSBartosz Golaszewski gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); 236925ca369SKent Gibson } 237925ca369SKent Gibson return 0; 238925ca369SKent Gibson } 239925ca369SKent Gibson 240bdbbae24SBartosz Golaszewski static long linehandle_ioctl_unlocked(struct file *file, unsigned int cmd, 241925ca369SKent Gibson unsigned long arg) 242925ca369SKent Gibson { 24349bc5279SKent Gibson struct linehandle_state *lh = file->private_data; 244925ca369SKent Gibson void __user *ip = (void __user *)arg; 245925ca369SKent Gibson struct gpiohandle_data ghd; 246925ca369SKent Gibson DECLARE_BITMAP(vals, GPIOHANDLES_MAX); 2471cef8b50SAndy Shevchenko unsigned int i; 2481cef8b50SAndy Shevchenko int ret; 249925ca369SKent Gibson 250533aae7cSBartosz Golaszewski if (!lh->gdev->chip) 251533aae7cSBartosz Golaszewski return -ENODEV; 252533aae7cSBartosz Golaszewski 2531cef8b50SAndy Shevchenko switch (cmd) { 2541cef8b50SAndy Shevchenko case GPIOHANDLE_GET_LINE_VALUES_IOCTL: 2551cef8b50SAndy Shevchenko /* NOTE: It's okay to read values of output lines */ 2561cef8b50SAndy Shevchenko ret = gpiod_get_array_value_complex(false, true, 2571cef8b50SAndy Shevchenko lh->num_descs, lh->descs, 2581cef8b50SAndy Shevchenko NULL, vals); 259925ca369SKent Gibson if (ret) 260925ca369SKent Gibson return ret; 261925ca369SKent Gibson 262925ca369SKent Gibson memset(&ghd, 0, sizeof(ghd)); 26352b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) 264925ca369SKent Gibson ghd.values[i] = test_bit(i, vals); 265925ca369SKent Gibson 266925ca369SKent Gibson if (copy_to_user(ip, &ghd, sizeof(ghd))) 267925ca369SKent Gibson return -EFAULT; 268925ca369SKent Gibson 269925ca369SKent Gibson return 0; 2701cef8b50SAndy Shevchenko case GPIOHANDLE_SET_LINE_VALUES_IOCTL: 271925ca369SKent Gibson /* 272925ca369SKent Gibson * All line descriptors were created at once with the same 273925ca369SKent Gibson * flags so just check if the first one is really output. 274925ca369SKent Gibson */ 275925ca369SKent Gibson if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags)) 276925ca369SKent Gibson return -EPERM; 277925ca369SKent Gibson 278925ca369SKent Gibson if (copy_from_user(&ghd, ip, sizeof(ghd))) 279925ca369SKent Gibson return -EFAULT; 280925ca369SKent Gibson 281925ca369SKent Gibson /* Clamp all values to [0,1] */ 28252b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) 283925ca369SKent Gibson __assign_bit(i, vals, ghd.values[i]); 284925ca369SKent Gibson 285925ca369SKent Gibson /* Reuse the array setting function */ 286925ca369SKent Gibson return gpiod_set_array_value_complex(false, 287925ca369SKent Gibson true, 28852b7b596SKent Gibson lh->num_descs, 289925ca369SKent Gibson lh->descs, 290925ca369SKent Gibson NULL, 291925ca369SKent Gibson vals); 2921cef8b50SAndy Shevchenko case GPIOHANDLE_SET_CONFIG_IOCTL: 293925ca369SKent Gibson return linehandle_set_config(lh, ip); 2941cef8b50SAndy Shevchenko default: 295925ca369SKent Gibson return -EINVAL; 296925ca369SKent Gibson } 2971cef8b50SAndy Shevchenko } 298925ca369SKent Gibson 299bdbbae24SBartosz Golaszewski static long linehandle_ioctl(struct file *file, unsigned int cmd, 300bdbbae24SBartosz Golaszewski unsigned long arg) 301bdbbae24SBartosz Golaszewski { 302bdbbae24SBartosz Golaszewski struct linehandle_state *lh = file->private_data; 303bdbbae24SBartosz Golaszewski 304bdbbae24SBartosz Golaszewski return call_ioctl_locked(file, cmd, arg, lh->gdev, 305bdbbae24SBartosz Golaszewski linehandle_ioctl_unlocked); 306bdbbae24SBartosz Golaszewski } 307bdbbae24SBartosz Golaszewski 308925ca369SKent Gibson #ifdef CONFIG_COMPAT 30949bc5279SKent Gibson static long linehandle_ioctl_compat(struct file *file, unsigned int cmd, 310925ca369SKent Gibson unsigned long arg) 311925ca369SKent Gibson { 31249bc5279SKent Gibson return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 313925ca369SKent Gibson } 314925ca369SKent Gibson #endif 315925ca369SKent Gibson 316883f9198SKent Gibson static void linehandle_free(struct linehandle_state *lh) 317925ca369SKent Gibson { 318925ca369SKent Gibson int i; 319925ca369SKent Gibson 32052b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) 321883f9198SKent Gibson if (lh->descs[i]) 322925ca369SKent Gibson gpiod_free(lh->descs[i]); 323925ca369SKent Gibson kfree(lh->label); 324dc0989e3SAndy Shevchenko gpio_device_put(lh->gdev); 325925ca369SKent Gibson kfree(lh); 326883f9198SKent Gibson } 327883f9198SKent Gibson 328883f9198SKent Gibson static int linehandle_release(struct inode *inode, struct file *file) 329883f9198SKent Gibson { 330883f9198SKent Gibson linehandle_free(file->private_data); 331925ca369SKent Gibson return 0; 332925ca369SKent Gibson } 333925ca369SKent Gibson 334925ca369SKent Gibson static const struct file_operations linehandle_fileops = { 335925ca369SKent Gibson .release = linehandle_release, 336925ca369SKent Gibson .owner = THIS_MODULE, 337925ca369SKent Gibson .llseek = noop_llseek, 338925ca369SKent Gibson .unlocked_ioctl = linehandle_ioctl, 339925ca369SKent Gibson #ifdef CONFIG_COMPAT 340925ca369SKent Gibson .compat_ioctl = linehandle_ioctl_compat, 341925ca369SKent Gibson #endif 342925ca369SKent Gibson }; 343925ca369SKent Gibson 344925ca369SKent Gibson static int linehandle_create(struct gpio_device *gdev, void __user *ip) 345925ca369SKent Gibson { 346925ca369SKent Gibson struct gpiohandle_request handlereq; 347925ca369SKent Gibson struct linehandle_state *lh; 348925ca369SKent Gibson struct file *file; 349883f9198SKent Gibson int fd, i, ret; 350925ca369SKent Gibson u32 lflags; 351925ca369SKent Gibson 352925ca369SKent Gibson if (copy_from_user(&handlereq, ip, sizeof(handlereq))) 353925ca369SKent Gibson return -EFAULT; 354925ca369SKent Gibson if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) 355925ca369SKent Gibson return -EINVAL; 356925ca369SKent Gibson 357925ca369SKent Gibson lflags = handlereq.flags; 358925ca369SKent Gibson 359925ca369SKent Gibson ret = linehandle_validate_flags(lflags); 360925ca369SKent Gibson if (ret) 361925ca369SKent Gibson return ret; 362925ca369SKent Gibson 363925ca369SKent Gibson lh = kzalloc(sizeof(*lh), GFP_KERNEL); 364925ca369SKent Gibson if (!lh) 365925ca369SKent Gibson return -ENOMEM; 366dc0989e3SAndy Shevchenko lh->gdev = gpio_device_get(gdev); 367925ca369SKent Gibson 368f188ac12SKent Gibson if (handlereq.consumer_label[0] != '\0') { 369f188ac12SKent Gibson /* label is only initialized if consumer_label is set */ 370f188ac12SKent Gibson lh->label = kstrndup(handlereq.consumer_label, 371f188ac12SKent Gibson sizeof(handlereq.consumer_label) - 1, 372925ca369SKent Gibson GFP_KERNEL); 373925ca369SKent Gibson if (!lh->label) { 374925ca369SKent Gibson ret = -ENOMEM; 375925ca369SKent Gibson goto out_free_lh; 376925ca369SKent Gibson } 377925ca369SKent Gibson } 378925ca369SKent Gibson 379883f9198SKent Gibson lh->num_descs = handlereq.lines; 380883f9198SKent Gibson 381925ca369SKent Gibson /* Request each GPIO */ 382925ca369SKent Gibson for (i = 0; i < handlereq.lines; i++) { 383925ca369SKent Gibson u32 offset = handlereq.lineoffsets[i]; 384925ca369SKent Gibson struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); 385925ca369SKent Gibson 386925ca369SKent Gibson if (IS_ERR(desc)) { 387925ca369SKent Gibson ret = PTR_ERR(desc); 388883f9198SKent Gibson goto out_free_lh; 389925ca369SKent Gibson } 390925ca369SKent Gibson 39195a4eed7SAndy Shevchenko ret = gpiod_request_user(desc, lh->label); 392925ca369SKent Gibson if (ret) 393883f9198SKent Gibson goto out_free_lh; 394925ca369SKent Gibson lh->descs[i] = desc; 395c274b58aSKent Gibson linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags); 396925ca369SKent Gibson 397925ca369SKent Gibson ret = gpiod_set_transitory(desc, false); 398925ca369SKent Gibson if (ret < 0) 399883f9198SKent Gibson goto out_free_lh; 400925ca369SKent Gibson 401925ca369SKent Gibson /* 402925ca369SKent Gibson * Lines have to be requested explicitly for input 403925ca369SKent Gibson * or output, else the line will be treated "as is". 404925ca369SKent Gibson */ 405925ca369SKent Gibson if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { 406925ca369SKent Gibson int val = !!handlereq.default_values[i]; 407925ca369SKent Gibson 408925ca369SKent Gibson ret = gpiod_direction_output(desc, val); 409925ca369SKent Gibson if (ret) 410883f9198SKent Gibson goto out_free_lh; 411925ca369SKent Gibson } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { 412925ca369SKent Gibson ret = gpiod_direction_input(desc); 413925ca369SKent Gibson if (ret) 414883f9198SKent Gibson goto out_free_lh; 415925ca369SKent Gibson } 416925ca369SKent Gibson 4179ce4ed5bSBartosz Golaszewski gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED); 418925ca369SKent Gibson 419925ca369SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", 420925ca369SKent Gibson offset); 421925ca369SKent Gibson } 422925ca369SKent Gibson 423925ca369SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 424925ca369SKent Gibson if (fd < 0) { 425925ca369SKent Gibson ret = fd; 426883f9198SKent Gibson goto out_free_lh; 427925ca369SKent Gibson } 428925ca369SKent Gibson 429925ca369SKent Gibson file = anon_inode_getfile("gpio-linehandle", 430925ca369SKent Gibson &linehandle_fileops, 431925ca369SKent Gibson lh, 432925ca369SKent Gibson O_RDONLY | O_CLOEXEC); 433925ca369SKent Gibson if (IS_ERR(file)) { 434925ca369SKent Gibson ret = PTR_ERR(file); 435925ca369SKent Gibson goto out_put_unused_fd; 436925ca369SKent Gibson } 437925ca369SKent Gibson 438925ca369SKent Gibson handlereq.fd = fd; 439925ca369SKent Gibson if (copy_to_user(ip, &handlereq, sizeof(handlereq))) { 440925ca369SKent Gibson /* 441925ca369SKent Gibson * fput() will trigger the release() callback, so do not go onto 442925ca369SKent Gibson * the regular error cleanup path here. 443925ca369SKent Gibson */ 444925ca369SKent Gibson fput(file); 445925ca369SKent Gibson put_unused_fd(fd); 446925ca369SKent Gibson return -EFAULT; 447925ca369SKent Gibson } 448925ca369SKent Gibson 449925ca369SKent Gibson fd_install(fd, file); 450925ca369SKent Gibson 451925ca369SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", 45252b7b596SKent Gibson lh->num_descs); 453925ca369SKent Gibson 454925ca369SKent Gibson return 0; 455925ca369SKent Gibson 456925ca369SKent Gibson out_put_unused_fd: 457925ca369SKent Gibson put_unused_fd(fd); 458925ca369SKent Gibson out_free_lh: 459883f9198SKent Gibson linehandle_free(lh); 460925ca369SKent Gibson return ret; 461925ca369SKent Gibson } 4623c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 4633c0d9c63SKent Gibson 4643c0d9c63SKent Gibson /** 4653c0d9c63SKent Gibson * struct line - contains the state of a requested line 466*309364bcSKent Gibson * @node: to store the object in supinfo_tree if supplemental 4673c0d9c63SKent Gibson * @desc: the GPIO descriptor for this line. 46873e03419SKent Gibson * @req: the corresponding line request 46973e03419SKent Gibson * @irq: the interrupt triggered in response to events on this GPIO 4708d259847SAndy Shevchenko * @edflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or 47173e03419SKent Gibson * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied 47273e03419SKent Gibson * @timestamp_ns: cache for the timestamp storing it between hardirq and 47373e03419SKent Gibson * IRQ thread, used to bring the timestamp close to the actual event 47473e03419SKent Gibson * @req_seqno: the seqno for the current edge event in the sequence of 47573e03419SKent Gibson * events for the corresponding line request. This is drawn from the @req. 47673e03419SKent Gibson * @line_seqno: the seqno for the current edge event in the sequence of 47773e03419SKent Gibson * events for this line. 47865cff704SKent Gibson * @work: the worker that implements software debouncing 479*309364bcSKent Gibson * @debounce_period_us: the debounce period in microseconds 48065cff704SKent Gibson * @sw_debounced: flag indicating if the software debouncer is active 48165cff704SKent Gibson * @level: the current debounced physical level of the line 48285ff37e3SAndy Shevchenko * @hdesc: the Hardware Timestamp Engine (HTE) descriptor 48385ff37e3SAndy Shevchenko * @raw_level: the line level at the time of event 48485ff37e3SAndy Shevchenko * @total_discard_seq: the running counter of the discarded events 48585ff37e3SAndy Shevchenko * @last_seqno: the last sequence number before debounce period expires 4863c0d9c63SKent Gibson */ 4873c0d9c63SKent Gibson struct line { 488*309364bcSKent Gibson struct rb_node node; 4893c0d9c63SKent Gibson struct gpio_desc *desc; 49073e03419SKent Gibson /* 49173e03419SKent Gibson * -- edge detector specific fields -- 49273e03419SKent Gibson */ 49373e03419SKent Gibson struct linereq *req; 49473e03419SKent Gibson unsigned int irq; 4953ffb7c45SKent Gibson /* 496b1a92e94SKent Gibson * The flags for the active edge detector configuration. 497b1a92e94SKent Gibson * 498b1a92e94SKent Gibson * edflags is set by linereq_create(), linereq_free(), and 499b1a92e94SKent Gibson * linereq_set_config_unlocked(), which are themselves mutually 500b1a92e94SKent Gibson * exclusive, and is accessed by edge_irq_thread(), 501b1a92e94SKent Gibson * process_hw_ts_thread() and debounce_work_func(), 502b1a92e94SKent Gibson * which can all live with a slightly stale value. 5033ffb7c45SKent Gibson */ 504b1a92e94SKent Gibson u64 edflags; 50573e03419SKent Gibson /* 50673e03419SKent Gibson * timestamp_ns and req_seqno are accessed only by 50773e03419SKent Gibson * edge_irq_handler() and edge_irq_thread(), which are themselves 50873e03419SKent Gibson * mutually exclusive, so no additional protection is necessary. 50973e03419SKent Gibson */ 51073e03419SKent Gibson u64 timestamp_ns; 51173e03419SKent Gibson u32 req_seqno; 51265cff704SKent Gibson /* 51365cff704SKent Gibson * line_seqno is accessed by either edge_irq_thread() or 51465cff704SKent Gibson * debounce_work_func(), which are themselves mutually exclusive, 51565cff704SKent Gibson * so no additional protection is necessary. 51665cff704SKent Gibson */ 51773e03419SKent Gibson u32 line_seqno; 51865cff704SKent Gibson /* 51965cff704SKent Gibson * -- debouncer specific fields -- 52065cff704SKent Gibson */ 52165cff704SKent Gibson struct delayed_work work; 52265cff704SKent Gibson /* 523*309364bcSKent Gibson * debounce_period_us is accessed by debounce_irq_handler() and 524*309364bcSKent Gibson * process_hw_ts() which are disabled when modified by 525*309364bcSKent Gibson * debounce_setup(), edge_detector_setup() or edge_detector_stop() 526*309364bcSKent Gibson * or can live with a stale version when updated by 527*309364bcSKent Gibson * edge_detector_update(). 528*309364bcSKent Gibson * The modifying functions are themselves mutually exclusive. 529*309364bcSKent Gibson */ 530*309364bcSKent Gibson unsigned int debounce_period_us; 531*309364bcSKent Gibson /* 53265cff704SKent Gibson * sw_debounce is accessed by linereq_set_config(), which is the 53365cff704SKent Gibson * only setter, and linereq_get_values(), which can live with a 53465cff704SKent Gibson * slightly stale value. 53565cff704SKent Gibson */ 53665cff704SKent Gibson unsigned int sw_debounced; 53765cff704SKent Gibson /* 53865cff704SKent Gibson * level is accessed by debounce_work_func(), which is the only 53965cff704SKent Gibson * setter, and linereq_get_values() which can live with a slightly 54065cff704SKent Gibson * stale value. 54165cff704SKent Gibson */ 54265cff704SKent Gibson unsigned int level; 543272ddba0SKent Gibson #ifdef CONFIG_HTE 5442068339aSDipen Patel struct hte_ts_desc hdesc; 5452068339aSDipen Patel /* 5462068339aSDipen Patel * HTE provider sets line level at the time of event. The valid 5472068339aSDipen Patel * value is 0 or 1 and negative value for an error. 5482068339aSDipen Patel */ 5492068339aSDipen Patel int raw_level; 5502068339aSDipen Patel /* 5512068339aSDipen Patel * when sw_debounce is set on HTE enabled line, this is running 5522068339aSDipen Patel * counter of the discarded events. 5532068339aSDipen Patel */ 5542068339aSDipen Patel u32 total_discard_seq; 5552068339aSDipen Patel /* 5562068339aSDipen Patel * when sw_debounce is set on HTE enabled line, this variable records 5572068339aSDipen Patel * last sequence number before debounce period expires. 5582068339aSDipen Patel */ 5592068339aSDipen Patel u32 last_seqno; 560272ddba0SKent Gibson #endif /* CONFIG_HTE */ 5613c0d9c63SKent Gibson }; 5623c0d9c63SKent Gibson 563*309364bcSKent Gibson /* 564*309364bcSKent Gibson * a rbtree of the struct lines containing supplemental info. 565*309364bcSKent Gibson * Used to populate gpio_v2_line_info with cdev specific fields not contained 566*309364bcSKent Gibson * in the struct gpio_desc. 567*309364bcSKent Gibson * A line is determined to contain supplemental information by 568*309364bcSKent Gibson * line_has_supinfo(). 569*309364bcSKent Gibson */ 570*309364bcSKent Gibson static struct rb_root supinfo_tree = RB_ROOT; 571*309364bcSKent Gibson /* covers supinfo_tree */ 572*309364bcSKent Gibson static DEFINE_SPINLOCK(supinfo_lock); 573*309364bcSKent Gibson 5743c0d9c63SKent Gibson /** 5753c0d9c63SKent Gibson * struct linereq - contains the state of a userspace line request 5763c0d9c63SKent Gibson * @gdev: the GPIO device the line request pertains to 5773c0d9c63SKent Gibson * @label: consumer label used to tag GPIO descriptors 5783c0d9c63SKent Gibson * @num_lines: the number of lines in the lines array 57973e03419SKent Gibson * @wait: wait queue that handles blocking reads of events 580a0dda508SBartosz Golaszewski * @device_unregistered_nb: notifier block for receiving gdev unregister events 58173e03419SKent Gibson * @event_buffer_size: the number of elements allocated in @events 58273e03419SKent Gibson * @events: KFIFO for the GPIO events 58373e03419SKent Gibson * @seqno: the sequence number for edge events generated on all lines in 58473e03419SKent Gibson * this line request. Note that this is not used when @num_lines is 1, as 58573e03419SKent Gibson * the line_seqno is then the same and is cheaper to calculate. 586a54756cbSKent Gibson * @config_mutex: mutex for serializing ioctl() calls to ensure consistency 587*309364bcSKent Gibson * of configuration, particularly multi-step accesses to desc flags and 588*309364bcSKent Gibson * changes to supinfo status. 5893c0d9c63SKent Gibson * @lines: the lines held by this line request, with @num_lines elements. 5903c0d9c63SKent Gibson */ 5913c0d9c63SKent Gibson struct linereq { 5923c0d9c63SKent Gibson struct gpio_device *gdev; 5933c0d9c63SKent Gibson const char *label; 5943c0d9c63SKent Gibson u32 num_lines; 59573e03419SKent Gibson wait_queue_head_t wait; 596a0dda508SBartosz Golaszewski struct notifier_block device_unregistered_nb; 59773e03419SKent Gibson u32 event_buffer_size; 59873e03419SKent Gibson DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); 59973e03419SKent Gibson atomic_t seqno; 600a54756cbSKent Gibson struct mutex config_mutex; 6013c0d9c63SKent Gibson struct line lines[]; 6023c0d9c63SKent Gibson }; 6033c0d9c63SKent Gibson 604*309364bcSKent Gibson static void supinfo_insert(struct line *line) 605*309364bcSKent Gibson { 606*309364bcSKent Gibson struct rb_node **new = &(supinfo_tree.rb_node), *parent = NULL; 607*309364bcSKent Gibson struct line *entry; 608*309364bcSKent Gibson 609*309364bcSKent Gibson guard(spinlock)(&supinfo_lock); 610*309364bcSKent Gibson 611*309364bcSKent Gibson while (*new) { 612*309364bcSKent Gibson entry = container_of(*new, struct line, node); 613*309364bcSKent Gibson 614*309364bcSKent Gibson parent = *new; 615*309364bcSKent Gibson if (line->desc < entry->desc) { 616*309364bcSKent Gibson new = &((*new)->rb_left); 617*309364bcSKent Gibson } else if (line->desc > entry->desc) { 618*309364bcSKent Gibson new = &((*new)->rb_right); 619*309364bcSKent Gibson } else { 620*309364bcSKent Gibson /* this should never happen */ 621*309364bcSKent Gibson WARN(1, "duplicate line inserted"); 622*309364bcSKent Gibson return; 623*309364bcSKent Gibson } 624*309364bcSKent Gibson } 625*309364bcSKent Gibson 626*309364bcSKent Gibson rb_link_node(&line->node, parent, new); 627*309364bcSKent Gibson rb_insert_color(&line->node, &supinfo_tree); 628*309364bcSKent Gibson } 629*309364bcSKent Gibson 630*309364bcSKent Gibson static void supinfo_erase(struct line *line) 631*309364bcSKent Gibson { 632*309364bcSKent Gibson guard(spinlock)(&supinfo_lock); 633*309364bcSKent Gibson 634*309364bcSKent Gibson rb_erase(&line->node, &supinfo_tree); 635*309364bcSKent Gibson } 636*309364bcSKent Gibson 637*309364bcSKent Gibson static struct line *supinfo_find(struct gpio_desc *desc) 638*309364bcSKent Gibson { 639*309364bcSKent Gibson struct rb_node *node = supinfo_tree.rb_node; 640*309364bcSKent Gibson struct line *line; 641*309364bcSKent Gibson 642*309364bcSKent Gibson while (node) { 643*309364bcSKent Gibson line = container_of(node, struct line, node); 644*309364bcSKent Gibson if (desc < line->desc) 645*309364bcSKent Gibson node = node->rb_left; 646*309364bcSKent Gibson else if (desc > line->desc) 647*309364bcSKent Gibson node = node->rb_right; 648*309364bcSKent Gibson else 649*309364bcSKent Gibson return line; 650*309364bcSKent Gibson } 651*309364bcSKent Gibson return NULL; 652*309364bcSKent Gibson } 653*309364bcSKent Gibson 654*309364bcSKent Gibson static void supinfo_to_lineinfo(struct gpio_desc *desc, 655*309364bcSKent Gibson struct gpio_v2_line_info *info) 656*309364bcSKent Gibson { 657*309364bcSKent Gibson struct gpio_v2_line_attribute *attr; 658*309364bcSKent Gibson struct line *line; 659*309364bcSKent Gibson 660*309364bcSKent Gibson guard(spinlock)(&supinfo_lock); 661*309364bcSKent Gibson 662*309364bcSKent Gibson line = supinfo_find(desc); 663*309364bcSKent Gibson if (!line) 664*309364bcSKent Gibson return; 665*309364bcSKent Gibson 666*309364bcSKent Gibson attr = &info->attrs[info->num_attrs]; 667*309364bcSKent Gibson attr->id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; 668*309364bcSKent Gibson attr->debounce_period_us = READ_ONCE(line->debounce_period_us); 669*309364bcSKent Gibson info->num_attrs++; 670*309364bcSKent Gibson } 671*309364bcSKent Gibson 672*309364bcSKent Gibson static inline bool line_has_supinfo(struct line *line) 673*309364bcSKent Gibson { 674*309364bcSKent Gibson return READ_ONCE(line->debounce_period_us); 675*309364bcSKent Gibson } 676*309364bcSKent Gibson 677*309364bcSKent Gibson /* 678*309364bcSKent Gibson * Checks line_has_supinfo() before and after the change to avoid unnecessary 679*309364bcSKent Gibson * supinfo_tree access. 680*309364bcSKent Gibson * Called indirectly by linereq_create() or linereq_set_config() so line 681*309364bcSKent Gibson * is already protected from concurrent changes. 682*309364bcSKent Gibson */ 683*309364bcSKent Gibson static void line_set_debounce_period(struct line *line, 684*309364bcSKent Gibson unsigned int debounce_period_us) 685*309364bcSKent Gibson { 686*309364bcSKent Gibson bool was_suppl = line_has_supinfo(line); 687*309364bcSKent Gibson 688*309364bcSKent Gibson WRITE_ONCE(line->debounce_period_us, debounce_period_us); 689*309364bcSKent Gibson 690*309364bcSKent Gibson /* if supinfo status is unchanged then we're done */ 691*309364bcSKent Gibson if (line_has_supinfo(line) == was_suppl) 692*309364bcSKent Gibson return; 693*309364bcSKent Gibson 694*309364bcSKent Gibson /* supinfo status has changed, so update the tree */ 695*309364bcSKent Gibson if (was_suppl) 696*309364bcSKent Gibson supinfo_erase(line); 697*309364bcSKent Gibson else 698*309364bcSKent Gibson supinfo_insert(line); 699*309364bcSKent Gibson } 700*309364bcSKent Gibson 7013c0d9c63SKent Gibson #define GPIO_V2_LINE_BIAS_FLAGS \ 7023c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ 7033c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \ 7043c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_DISABLED) 7053c0d9c63SKent Gibson 7063c0d9c63SKent Gibson #define GPIO_V2_LINE_DIRECTION_FLAGS \ 7073c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_INPUT | \ 7083c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_OUTPUT) 7093c0d9c63SKent Gibson 7103c0d9c63SKent Gibson #define GPIO_V2_LINE_DRIVE_FLAGS \ 7113c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \ 7123c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_OPEN_SOURCE) 7133c0d9c63SKent Gibson 71473e03419SKent Gibson #define GPIO_V2_LINE_EDGE_FLAGS \ 71573e03419SKent Gibson (GPIO_V2_LINE_FLAG_EDGE_RISING | \ 71673e03419SKent Gibson GPIO_V2_LINE_FLAG_EDGE_FALLING) 71773e03419SKent Gibson 7185e2ca893SKent Gibson #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS 7195e2ca893SKent Gibson 7203c0d9c63SKent Gibson #define GPIO_V2_LINE_VALID_FLAGS \ 7213c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ 7223c0d9c63SKent Gibson GPIO_V2_LINE_DIRECTION_FLAGS | \ 7233c0d9c63SKent Gibson GPIO_V2_LINE_DRIVE_FLAGS | \ 72473e03419SKent Gibson GPIO_V2_LINE_EDGE_FLAGS | \ 72526d060e4SKent Gibson GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \ 7262068339aSDipen Patel GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \ 7273c0d9c63SKent Gibson GPIO_V2_LINE_BIAS_FLAGS) 7283c0d9c63SKent Gibson 729b1a92e94SKent Gibson /* subset of flags relevant for edge detector configuration */ 730b1a92e94SKent Gibson #define GPIO_V2_LINE_EDGE_DETECTOR_FLAGS \ 731b1a92e94SKent Gibson (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ 732b1a92e94SKent Gibson GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \ 733b1a92e94SKent Gibson GPIO_V2_LINE_EDGE_FLAGS) 734b1a92e94SKent Gibson 735a0dda508SBartosz Golaszewski static int linereq_unregistered_notify(struct notifier_block *nb, 736a0dda508SBartosz Golaszewski unsigned long action, void *data) 737a0dda508SBartosz Golaszewski { 738a0dda508SBartosz Golaszewski struct linereq *lr = container_of(nb, struct linereq, 739a0dda508SBartosz Golaszewski device_unregistered_nb); 740a0dda508SBartosz Golaszewski 741a0dda508SBartosz Golaszewski wake_up_poll(&lr->wait, EPOLLIN | EPOLLERR); 742a0dda508SBartosz Golaszewski 743a0dda508SBartosz Golaszewski return NOTIFY_OK; 744a0dda508SBartosz Golaszewski } 745a0dda508SBartosz Golaszewski 74673e03419SKent Gibson static void linereq_put_event(struct linereq *lr, 74773e03419SKent Gibson struct gpio_v2_line_event *le) 74873e03419SKent Gibson { 74973e03419SKent Gibson bool overflow = false; 75073e03419SKent Gibson 75173e03419SKent Gibson spin_lock(&lr->wait.lock); 75273e03419SKent Gibson if (kfifo_is_full(&lr->events)) { 75373e03419SKent Gibson overflow = true; 75473e03419SKent Gibson kfifo_skip(&lr->events); 75573e03419SKent Gibson } 75673e03419SKent Gibson kfifo_in(&lr->events, le, 1); 75773e03419SKent Gibson spin_unlock(&lr->wait.lock); 75873e03419SKent Gibson if (!overflow) 75973e03419SKent Gibson wake_up_poll(&lr->wait, EPOLLIN); 76073e03419SKent Gibson else 76173e03419SKent Gibson pr_debug_ratelimited("event FIFO is full - event dropped\n"); 76273e03419SKent Gibson } 76373e03419SKent Gibson 76426d060e4SKent Gibson static u64 line_event_timestamp(struct line *line) 76526d060e4SKent Gibson { 76626d060e4SKent Gibson if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags)) 76726d060e4SKent Gibson return ktime_get_real_ns(); 768272ddba0SKent Gibson else if (IS_ENABLED(CONFIG_HTE) && 769272ddba0SKent Gibson test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags)) 7702068339aSDipen Patel return line->timestamp_ns; 77126d060e4SKent Gibson 77226d060e4SKent Gibson return ktime_get_ns(); 77326d060e4SKent Gibson } 77426d060e4SKent Gibson 77524220232SKent Gibson static u32 line_event_id(int level) 77624220232SKent Gibson { 77724220232SKent Gibson return level ? GPIO_V2_LINE_EVENT_RISING_EDGE : 77824220232SKent Gibson GPIO_V2_LINE_EVENT_FALLING_EDGE; 77924220232SKent Gibson } 78024220232SKent Gibson 7812f0262acSKent Gibson static inline char *make_irq_label(const char *orig) 7822f0262acSKent Gibson { 7832f0262acSKent Gibson char *new; 7842f0262acSKent Gibson 7852f0262acSKent Gibson if (!orig) 7862f0262acSKent Gibson return NULL; 7872f0262acSKent Gibson 7882f0262acSKent Gibson new = kstrdup_and_replace(orig, '/', ':', GFP_KERNEL); 7892f0262acSKent Gibson if (!new) 7902f0262acSKent Gibson return ERR_PTR(-ENOMEM); 7912f0262acSKent Gibson 7922f0262acSKent Gibson return new; 7932f0262acSKent Gibson } 7942f0262acSKent Gibson 7952f0262acSKent Gibson static inline void free_irq_label(const char *label) 7962f0262acSKent Gibson { 7972f0262acSKent Gibson kfree(label); 7982f0262acSKent Gibson } 7992f0262acSKent Gibson 800272ddba0SKent Gibson #ifdef CONFIG_HTE 801272ddba0SKent Gibson 8022068339aSDipen Patel static enum hte_return process_hw_ts_thread(void *p) 8032068339aSDipen Patel { 8042068339aSDipen Patel struct line *line; 8052068339aSDipen Patel struct linereq *lr; 8062068339aSDipen Patel struct gpio_v2_line_event le; 807b1a92e94SKent Gibson u64 edflags; 8082068339aSDipen Patel int level; 8092068339aSDipen Patel 8102068339aSDipen Patel if (!p) 8112068339aSDipen Patel return HTE_CB_HANDLED; 8122068339aSDipen Patel 8132068339aSDipen Patel line = p; 8142068339aSDipen Patel lr = line->req; 8152068339aSDipen Patel 8162068339aSDipen Patel memset(&le, 0, sizeof(le)); 8172068339aSDipen Patel 8182068339aSDipen Patel le.timestamp_ns = line->timestamp_ns; 819b1a92e94SKent Gibson edflags = READ_ONCE(line->edflags); 8202068339aSDipen Patel 821b1a92e94SKent Gibson switch (edflags & GPIO_V2_LINE_EDGE_FLAGS) { 822cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_BOTH: 82324220232SKent Gibson level = (line->raw_level >= 0) ? 82424220232SKent Gibson line->raw_level : 82524220232SKent Gibson gpiod_get_raw_value_cansleep(line->desc); 8262068339aSDipen Patel 827b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 82824220232SKent Gibson level = !level; 82924220232SKent Gibson 83024220232SKent Gibson le.id = line_event_id(level); 831cfa53463SKent Gibson break; 832cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_RISING: 8332068339aSDipen Patel le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 834cfa53463SKent Gibson break; 835cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_FALLING: 8362068339aSDipen Patel le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 837cfa53463SKent Gibson break; 838cfa53463SKent Gibson default: 8392068339aSDipen Patel return HTE_CB_HANDLED; 8402068339aSDipen Patel } 8412068339aSDipen Patel le.line_seqno = line->line_seqno; 8422068339aSDipen Patel le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno; 8432068339aSDipen Patel le.offset = gpio_chip_hwgpio(line->desc); 8442068339aSDipen Patel 8452068339aSDipen Patel linereq_put_event(lr, &le); 8462068339aSDipen Patel 8472068339aSDipen Patel return HTE_CB_HANDLED; 8482068339aSDipen Patel } 8492068339aSDipen Patel 8502068339aSDipen Patel static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p) 8512068339aSDipen Patel { 8522068339aSDipen Patel struct line *line; 8532068339aSDipen Patel struct linereq *lr; 8542068339aSDipen Patel int diff_seqno = 0; 8552068339aSDipen Patel 8562068339aSDipen Patel if (!ts || !p) 8572068339aSDipen Patel return HTE_CB_HANDLED; 8582068339aSDipen Patel 8592068339aSDipen Patel line = p; 8602068339aSDipen Patel line->timestamp_ns = ts->tsc; 8612068339aSDipen Patel line->raw_level = ts->raw_level; 8622068339aSDipen Patel lr = line->req; 8632068339aSDipen Patel 8642068339aSDipen Patel if (READ_ONCE(line->sw_debounced)) { 8652068339aSDipen Patel line->total_discard_seq++; 8662068339aSDipen Patel line->last_seqno = ts->seq; 8672068339aSDipen Patel mod_delayed_work(system_wq, &line->work, 868*309364bcSKent Gibson usecs_to_jiffies(READ_ONCE(line->debounce_period_us))); 8692068339aSDipen Patel } else { 8702068339aSDipen Patel if (unlikely(ts->seq < line->line_seqno)) 8712068339aSDipen Patel return HTE_CB_HANDLED; 8722068339aSDipen Patel 8732068339aSDipen Patel diff_seqno = ts->seq - line->line_seqno; 8742068339aSDipen Patel line->line_seqno = ts->seq; 8752068339aSDipen Patel if (lr->num_lines != 1) 8762068339aSDipen Patel line->req_seqno = atomic_add_return(diff_seqno, 8772068339aSDipen Patel &lr->seqno); 8782068339aSDipen Patel 8792068339aSDipen Patel return HTE_RUN_SECOND_CB; 8802068339aSDipen Patel } 8812068339aSDipen Patel 8822068339aSDipen Patel return HTE_CB_HANDLED; 8832068339aSDipen Patel } 8842068339aSDipen Patel 885272ddba0SKent Gibson static int hte_edge_setup(struct line *line, u64 eflags) 886272ddba0SKent Gibson { 887272ddba0SKent Gibson int ret; 888272ddba0SKent Gibson unsigned long flags = 0; 889272ddba0SKent Gibson struct hte_ts_desc *hdesc = &line->hdesc; 890272ddba0SKent Gibson 891272ddba0SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) 892272ddba0SKent Gibson flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 893272ddba0SKent Gibson HTE_FALLING_EDGE_TS : 894272ddba0SKent Gibson HTE_RISING_EDGE_TS; 895272ddba0SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) 896272ddba0SKent Gibson flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 897272ddba0SKent Gibson HTE_RISING_EDGE_TS : 898272ddba0SKent Gibson HTE_FALLING_EDGE_TS; 899272ddba0SKent Gibson 900272ddba0SKent Gibson line->total_discard_seq = 0; 901272ddba0SKent Gibson 902272ddba0SKent Gibson hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags, NULL, 903272ddba0SKent Gibson line->desc); 904272ddba0SKent Gibson 905272ddba0SKent Gibson ret = hte_ts_get(NULL, hdesc, 0); 906272ddba0SKent Gibson if (ret) 907272ddba0SKent Gibson return ret; 908272ddba0SKent Gibson 909272ddba0SKent Gibson return hte_request_ts_ns(hdesc, process_hw_ts, process_hw_ts_thread, 910272ddba0SKent Gibson line); 911272ddba0SKent Gibson } 912272ddba0SKent Gibson 913272ddba0SKent Gibson #else 914272ddba0SKent Gibson 915272ddba0SKent Gibson static int hte_edge_setup(struct line *line, u64 eflags) 916272ddba0SKent Gibson { 917272ddba0SKent Gibson return 0; 918272ddba0SKent Gibson } 919272ddba0SKent Gibson #endif /* CONFIG_HTE */ 920272ddba0SKent Gibson 92173e03419SKent Gibson static irqreturn_t edge_irq_thread(int irq, void *p) 92273e03419SKent Gibson { 92373e03419SKent Gibson struct line *line = p; 92473e03419SKent Gibson struct linereq *lr = line->req; 92573e03419SKent Gibson struct gpio_v2_line_event le; 92673e03419SKent Gibson 92773e03419SKent Gibson /* Do not leak kernel stack to userspace */ 92873e03419SKent Gibson memset(&le, 0, sizeof(le)); 92973e03419SKent Gibson 93073e03419SKent Gibson if (line->timestamp_ns) { 93173e03419SKent Gibson le.timestamp_ns = line->timestamp_ns; 93273e03419SKent Gibson } else { 93373e03419SKent Gibson /* 93473e03419SKent Gibson * We may be running from a nested threaded interrupt in 93573e03419SKent Gibson * which case we didn't get the timestamp from 93673e03419SKent Gibson * edge_irq_handler(). 93773e03419SKent Gibson */ 93826d060e4SKent Gibson le.timestamp_ns = line_event_timestamp(line); 93973e03419SKent Gibson if (lr->num_lines != 1) 94073e03419SKent Gibson line->req_seqno = atomic_inc_return(&lr->seqno); 94173e03419SKent Gibson } 94273e03419SKent Gibson line->timestamp_ns = 0; 94373e03419SKent Gibson 944b1a92e94SKent Gibson switch (READ_ONCE(line->edflags) & GPIO_V2_LINE_EDGE_FLAGS) { 945cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_BOTH: 94624220232SKent Gibson le.id = line_event_id(gpiod_get_value_cansleep(line->desc)); 947cfa53463SKent Gibson break; 948cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_RISING: 94973e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 950cfa53463SKent Gibson break; 951cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_FALLING: 95273e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 953cfa53463SKent Gibson break; 954cfa53463SKent Gibson default: 95573e03419SKent Gibson return IRQ_NONE; 95673e03419SKent Gibson } 95773e03419SKent Gibson line->line_seqno++; 95873e03419SKent Gibson le.line_seqno = line->line_seqno; 95973e03419SKent Gibson le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno; 96073e03419SKent Gibson le.offset = gpio_chip_hwgpio(line->desc); 96173e03419SKent Gibson 96273e03419SKent Gibson linereq_put_event(lr, &le); 96373e03419SKent Gibson 96473e03419SKent Gibson return IRQ_HANDLED; 96573e03419SKent Gibson } 96673e03419SKent Gibson 96773e03419SKent Gibson static irqreturn_t edge_irq_handler(int irq, void *p) 96873e03419SKent Gibson { 96973e03419SKent Gibson struct line *line = p; 97073e03419SKent Gibson struct linereq *lr = line->req; 97173e03419SKent Gibson 97273e03419SKent Gibson /* 97373e03419SKent Gibson * Just store the timestamp in hardirq context so we get it as 97473e03419SKent Gibson * close in time as possible to the actual event. 97573e03419SKent Gibson */ 97626d060e4SKent Gibson line->timestamp_ns = line_event_timestamp(line); 97773e03419SKent Gibson 97873e03419SKent Gibson if (lr->num_lines != 1) 97973e03419SKent Gibson line->req_seqno = atomic_inc_return(&lr->seqno); 98073e03419SKent Gibson 98173e03419SKent Gibson return IRQ_WAKE_THREAD; 98273e03419SKent Gibson } 98373e03419SKent Gibson 98465cff704SKent Gibson /* 98565cff704SKent Gibson * returns the current debounced logical value. 98665cff704SKent Gibson */ 98765cff704SKent Gibson static bool debounced_value(struct line *line) 98865cff704SKent Gibson { 98965cff704SKent Gibson bool value; 99065cff704SKent Gibson 99165cff704SKent Gibson /* 99265cff704SKent Gibson * minor race - debouncer may be stopped here, so edge_detector_stop() 99365cff704SKent Gibson * must leave the value unchanged so the following will read the level 99465cff704SKent Gibson * from when the debouncer was last running. 99565cff704SKent Gibson */ 99665cff704SKent Gibson value = READ_ONCE(line->level); 99765cff704SKent Gibson 99865cff704SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags)) 99965cff704SKent Gibson value = !value; 100065cff704SKent Gibson 100165cff704SKent Gibson return value; 100265cff704SKent Gibson } 100365cff704SKent Gibson 100465cff704SKent Gibson static irqreturn_t debounce_irq_handler(int irq, void *p) 100565cff704SKent Gibson { 100665cff704SKent Gibson struct line *line = p; 100765cff704SKent Gibson 100865cff704SKent Gibson mod_delayed_work(system_wq, &line->work, 1009*309364bcSKent Gibson usecs_to_jiffies(READ_ONCE(line->debounce_period_us))); 101065cff704SKent Gibson 101165cff704SKent Gibson return IRQ_HANDLED; 101265cff704SKent Gibson } 101365cff704SKent Gibson 101465cff704SKent Gibson static void debounce_work_func(struct work_struct *work) 101565cff704SKent Gibson { 101665cff704SKent Gibson struct gpio_v2_line_event le; 101765cff704SKent Gibson struct line *line = container_of(work, struct line, work.work); 101865cff704SKent Gibson struct linereq *lr; 1019b1a92e94SKent Gibson u64 eflags, edflags = READ_ONCE(line->edflags); 1020272ddba0SKent Gibson int level = -1; 1021272ddba0SKent Gibson #ifdef CONFIG_HTE 1022272ddba0SKent Gibson int diff_seqno; 102365cff704SKent Gibson 1024b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) 10252068339aSDipen Patel level = line->raw_level; 1026272ddba0SKent Gibson #endif 10272068339aSDipen Patel if (level < 0) 102865cff704SKent Gibson level = gpiod_get_raw_value_cansleep(line->desc); 102965cff704SKent Gibson if (level < 0) { 103065cff704SKent Gibson pr_debug_ratelimited("debouncer failed to read line value\n"); 103165cff704SKent Gibson return; 103265cff704SKent Gibson } 103365cff704SKent Gibson 103465cff704SKent Gibson if (READ_ONCE(line->level) == level) 103565cff704SKent Gibson return; 103665cff704SKent Gibson 103765cff704SKent Gibson WRITE_ONCE(line->level, level); 103865cff704SKent Gibson 103965cff704SKent Gibson /* -- edge detection -- */ 1040b1a92e94SKent Gibson eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS; 10413ffb7c45SKent Gibson if (!eflags) 104265cff704SKent Gibson return; 104365cff704SKent Gibson 104465cff704SKent Gibson /* switch from physical level to logical - if they differ */ 1045b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 104665cff704SKent Gibson level = !level; 104765cff704SKent Gibson 104865cff704SKent Gibson /* ignore edges that are not being monitored */ 10493ffb7c45SKent Gibson if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) || 10503ffb7c45SKent Gibson ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level)) 105165cff704SKent Gibson return; 105265cff704SKent Gibson 105365cff704SKent Gibson /* Do not leak kernel stack to userspace */ 105465cff704SKent Gibson memset(&le, 0, sizeof(le)); 105565cff704SKent Gibson 105665cff704SKent Gibson lr = line->req; 105726d060e4SKent Gibson le.timestamp_ns = line_event_timestamp(line); 105865cff704SKent Gibson le.offset = gpio_chip_hwgpio(line->desc); 1059272ddba0SKent Gibson #ifdef CONFIG_HTE 1060b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) { 10612068339aSDipen Patel /* discard events except the last one */ 10622068339aSDipen Patel line->total_discard_seq -= 1; 10632068339aSDipen Patel diff_seqno = line->last_seqno - line->total_discard_seq - 10642068339aSDipen Patel line->line_seqno; 10652068339aSDipen Patel line->line_seqno = line->last_seqno - line->total_discard_seq; 10662068339aSDipen Patel le.line_seqno = line->line_seqno; 10672068339aSDipen Patel le.seqno = (lr->num_lines == 1) ? 10682068339aSDipen Patel le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno); 1069272ddba0SKent Gibson } else 1070272ddba0SKent Gibson #endif /* CONFIG_HTE */ 1071272ddba0SKent Gibson { 107265cff704SKent Gibson line->line_seqno++; 107365cff704SKent Gibson le.line_seqno = line->line_seqno; 107465cff704SKent Gibson le.seqno = (lr->num_lines == 1) ? 107565cff704SKent Gibson le.line_seqno : atomic_inc_return(&lr->seqno); 10762068339aSDipen Patel } 107765cff704SKent Gibson 107824220232SKent Gibson le.id = line_event_id(level); 107965cff704SKent Gibson 108065cff704SKent Gibson linereq_put_event(lr, &le); 108165cff704SKent Gibson } 108265cff704SKent Gibson 1083b1a92e94SKent Gibson static int debounce_setup(struct line *line, unsigned int debounce_period_us) 108465cff704SKent Gibson { 108565cff704SKent Gibson unsigned long irqflags; 108665cff704SKent Gibson int ret, level, irq; 10872f0262acSKent Gibson char *label; 108865cff704SKent Gibson 108965cff704SKent Gibson /* try hardware */ 109065cff704SKent Gibson ret = gpiod_set_debounce(line->desc, debounce_period_us); 109165cff704SKent Gibson if (!ret) { 1092*309364bcSKent Gibson line_set_debounce_period(line, debounce_period_us); 109365cff704SKent Gibson return ret; 109465cff704SKent Gibson } 109565cff704SKent Gibson if (ret != -ENOTSUPP) 109665cff704SKent Gibson return ret; 109765cff704SKent Gibson 109865cff704SKent Gibson if (debounce_period_us) { 109965cff704SKent Gibson /* setup software debounce */ 110065cff704SKent Gibson level = gpiod_get_raw_value_cansleep(line->desc); 110165cff704SKent Gibson if (level < 0) 110265cff704SKent Gibson return level; 110365cff704SKent Gibson 1104272ddba0SKent Gibson if (!(IS_ENABLED(CONFIG_HTE) && 1105272ddba0SKent Gibson test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))) { 110665cff704SKent Gibson irq = gpiod_to_irq(line->desc); 110765cff704SKent Gibson if (irq < 0) 110865cff704SKent Gibson return -ENXIO; 110965cff704SKent Gibson 11102f0262acSKent Gibson label = make_irq_label(line->req->label); 11112f0262acSKent Gibson if (IS_ERR(label)) 11122f0262acSKent Gibson return -ENOMEM; 11132f0262acSKent Gibson 111465cff704SKent Gibson irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING; 111565cff704SKent Gibson ret = request_irq(irq, debounce_irq_handler, irqflags, 11162f0262acSKent Gibson label, line); 11172f0262acSKent Gibson if (ret) { 11182f0262acSKent Gibson free_irq_label(label); 111965cff704SKent Gibson return ret; 11202f0262acSKent Gibson } 112165cff704SKent Gibson line->irq = irq; 11222068339aSDipen Patel } else { 11232487a812SKent Gibson ret = hte_edge_setup(line, GPIO_V2_LINE_FLAG_EDGE_BOTH); 11242068339aSDipen Patel if (ret) 11252068339aSDipen Patel return ret; 11262068339aSDipen Patel } 11272068339aSDipen Patel 11282068339aSDipen Patel WRITE_ONCE(line->level, level); 11292068339aSDipen Patel WRITE_ONCE(line->sw_debounced, 1); 113065cff704SKent Gibson } 113165cff704SKent Gibson return 0; 113265cff704SKent Gibson } 113365cff704SKent Gibson 113465cff704SKent Gibson static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc, 113565cff704SKent Gibson unsigned int line_idx) 113665cff704SKent Gibson { 113765cff704SKent Gibson unsigned int i; 113865cff704SKent Gibson u64 mask = BIT_ULL(line_idx); 113965cff704SKent Gibson 114065cff704SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 114165cff704SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && 114265cff704SKent Gibson (lc->attrs[i].mask & mask)) 114365cff704SKent Gibson return true; 114465cff704SKent Gibson } 114565cff704SKent Gibson return false; 114665cff704SKent Gibson } 114765cff704SKent Gibson 114865cff704SKent Gibson static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc, 114965cff704SKent Gibson unsigned int line_idx) 115065cff704SKent Gibson { 115165cff704SKent Gibson unsigned int i; 115265cff704SKent Gibson u64 mask = BIT_ULL(line_idx); 115365cff704SKent Gibson 115465cff704SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 115565cff704SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && 115665cff704SKent Gibson (lc->attrs[i].mask & mask)) 115765cff704SKent Gibson return lc->attrs[i].attr.debounce_period_us; 115865cff704SKent Gibson } 115965cff704SKent Gibson return 0; 116065cff704SKent Gibson } 116165cff704SKent Gibson 1162b1a92e94SKent Gibson static void edge_detector_stop(struct line *line) 116373e03419SKent Gibson { 1164b1a92e94SKent Gibson if (line->irq) { 116521bc9b15SBartosz Golaszewski free_irq_label(free_irq(line->irq, line)); 116673e03419SKent Gibson line->irq = 0; 116773e03419SKent Gibson } 1168a54756cbSKent Gibson 1169272ddba0SKent Gibson #ifdef CONFIG_HTE 1170b1a92e94SKent Gibson if (READ_ONCE(line->edflags) & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) 11712068339aSDipen Patel hte_ts_put(&line->hdesc); 1172272ddba0SKent Gibson #endif 11732068339aSDipen Patel 117465cff704SKent Gibson cancel_delayed_work_sync(&line->work); 117565cff704SKent Gibson WRITE_ONCE(line->sw_debounced, 0); 1176b1a92e94SKent Gibson WRITE_ONCE(line->edflags, 0); 1177*309364bcSKent Gibson line_set_debounce_period(line, 0); 117865cff704SKent Gibson /* do not change line->level - see comment in debounced_value() */ 117973e03419SKent Gibson } 118073e03419SKent Gibson 118173e03419SKent Gibson static int edge_detector_setup(struct line *line, 118265cff704SKent Gibson struct gpio_v2_line_config *lc, 1183b1a92e94SKent Gibson unsigned int line_idx, u64 edflags) 118473e03419SKent Gibson { 118565cff704SKent Gibson u32 debounce_period_us; 118673e03419SKent Gibson unsigned long irqflags = 0; 1187b1a92e94SKent Gibson u64 eflags; 118873e03419SKent Gibson int irq, ret; 118921bc9b15SBartosz Golaszewski char *label; 119073e03419SKent Gibson 1191b1a92e94SKent Gibson eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS; 119273e03419SKent Gibson if (eflags && !kfifo_initialized(&line->req->events)) { 119373e03419SKent Gibson ret = kfifo_alloc(&line->req->events, 119473e03419SKent Gibson line->req->event_buffer_size, GFP_KERNEL); 119573e03419SKent Gibson if (ret) 119673e03419SKent Gibson return ret; 119773e03419SKent Gibson } 119865cff704SKent Gibson if (gpio_v2_line_config_debounced(lc, line_idx)) { 119965cff704SKent Gibson debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx); 1200b1a92e94SKent Gibson ret = debounce_setup(line, debounce_period_us); 120165cff704SKent Gibson if (ret) 120265cff704SKent Gibson return ret; 1203*309364bcSKent Gibson line_set_debounce_period(line, debounce_period_us); 120465cff704SKent Gibson } 120573e03419SKent Gibson 120665cff704SKent Gibson /* detection disabled or sw debouncer will provide edge detection */ 120765cff704SKent Gibson if (!eflags || READ_ONCE(line->sw_debounced)) 120873e03419SKent Gibson return 0; 120973e03419SKent Gibson 1210272ddba0SKent Gibson if (IS_ENABLED(CONFIG_HTE) && 1211272ddba0SKent Gibson (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)) 1212b1a92e94SKent Gibson return hte_edge_setup(line, edflags); 12132068339aSDipen Patel 121473e03419SKent Gibson irq = gpiod_to_irq(line->desc); 121573e03419SKent Gibson if (irq < 0) 121673e03419SKent Gibson return -ENXIO; 121773e03419SKent Gibson 121873e03419SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) 121973e03419SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 122073e03419SKent Gibson IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 122173e03419SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) 122273e03419SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 122373e03419SKent Gibson IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 122473e03419SKent Gibson irqflags |= IRQF_ONESHOT; 122573e03419SKent Gibson 122621bc9b15SBartosz Golaszewski label = make_irq_label(line->req->label); 1227d9f0804aSBartosz Golaszewski if (IS_ERR(label)) 1228d9f0804aSBartosz Golaszewski return PTR_ERR(label); 122921bc9b15SBartosz Golaszewski 123073e03419SKent Gibson /* Request a thread to read the events */ 123173e03419SKent Gibson ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread, 123221bc9b15SBartosz Golaszewski irqflags, label, line); 123321bc9b15SBartosz Golaszewski if (ret) { 123421bc9b15SBartosz Golaszewski free_irq_label(label); 123573e03419SKent Gibson return ret; 123621bc9b15SBartosz Golaszewski } 123773e03419SKent Gibson 123873e03419SKent Gibson line->irq = irq; 123973e03419SKent Gibson return 0; 124073e03419SKent Gibson } 124173e03419SKent Gibson 124265cff704SKent Gibson static int edge_detector_update(struct line *line, 124365cff704SKent Gibson struct gpio_v2_line_config *lc, 1244b1a92e94SKent Gibson unsigned int line_idx, u64 edflags) 1245a54756cbSKent Gibson { 1246b1a92e94SKent Gibson u64 active_edflags = READ_ONCE(line->edflags); 124765cff704SKent Gibson unsigned int debounce_period_us = 124865cff704SKent Gibson gpio_v2_line_config_debounce_period(lc, line_idx); 124965cff704SKent Gibson 1250b1a92e94SKent Gibson if ((active_edflags == edflags) && 1251*309364bcSKent Gibson (READ_ONCE(line->debounce_period_us) == debounce_period_us)) 1252a54756cbSKent Gibson return 0; 1253a54756cbSKent Gibson 125465cff704SKent Gibson /* sw debounced and still will be...*/ 125565cff704SKent Gibson if (debounce_period_us && READ_ONCE(line->sw_debounced)) { 1256*309364bcSKent Gibson line_set_debounce_period(line, debounce_period_us); 125765cff704SKent Gibson return 0; 125865cff704SKent Gibson } 125965cff704SKent Gibson 126065cff704SKent Gibson /* reconfiguring edge detection or sw debounce being disabled */ 1261b1a92e94SKent Gibson if ((line->irq && !READ_ONCE(line->sw_debounced)) || 1262b1a92e94SKent Gibson (active_edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) || 126365cff704SKent Gibson (!debounce_period_us && READ_ONCE(line->sw_debounced))) 1264b1a92e94SKent Gibson edge_detector_stop(line); 1265a54756cbSKent Gibson 1266b1a92e94SKent Gibson return edge_detector_setup(line, lc, line_idx, edflags); 1267a54756cbSKent Gibson } 1268a54756cbSKent Gibson 12693c0d9c63SKent Gibson static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, 12703c0d9c63SKent Gibson unsigned int line_idx) 12713c0d9c63SKent Gibson { 12723c0d9c63SKent Gibson unsigned int i; 12733c0d9c63SKent Gibson u64 mask = BIT_ULL(line_idx); 12743c0d9c63SKent Gibson 12753c0d9c63SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 12763c0d9c63SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) && 12773c0d9c63SKent Gibson (lc->attrs[i].mask & mask)) 12783c0d9c63SKent Gibson return lc->attrs[i].attr.flags; 12793c0d9c63SKent Gibson } 12803c0d9c63SKent Gibson return lc->flags; 12813c0d9c63SKent Gibson } 12823c0d9c63SKent Gibson 12833c0d9c63SKent Gibson static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc, 12843c0d9c63SKent Gibson unsigned int line_idx) 12853c0d9c63SKent Gibson { 12863c0d9c63SKent Gibson unsigned int i; 12873c0d9c63SKent Gibson u64 mask = BIT_ULL(line_idx); 12883c0d9c63SKent Gibson 12893c0d9c63SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 12903c0d9c63SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) && 12913c0d9c63SKent Gibson (lc->attrs[i].mask & mask)) 12923c0d9c63SKent Gibson return !!(lc->attrs[i].attr.values & mask); 12933c0d9c63SKent Gibson } 12943c0d9c63SKent Gibson return 0; 12953c0d9c63SKent Gibson } 12963c0d9c63SKent Gibson 12973c0d9c63SKent Gibson static int gpio_v2_line_flags_validate(u64 flags) 12983c0d9c63SKent Gibson { 12993c0d9c63SKent Gibson /* Return an error if an unknown flag is set */ 13003c0d9c63SKent Gibson if (flags & ~GPIO_V2_LINE_VALID_FLAGS) 13013c0d9c63SKent Gibson return -EINVAL; 1302272ddba0SKent Gibson 1303272ddba0SKent Gibson if (!IS_ENABLED(CONFIG_HTE) && 1304272ddba0SKent Gibson (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)) 1305272ddba0SKent Gibson return -EOPNOTSUPP; 1306272ddba0SKent Gibson 13073c0d9c63SKent Gibson /* 13083c0d9c63SKent Gibson * Do not allow both INPUT and OUTPUT flags to be set as they are 13093c0d9c63SKent Gibson * contradictory. 13103c0d9c63SKent Gibson */ 13113c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_FLAG_INPUT) && 13123c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_OUTPUT)) 13133c0d9c63SKent Gibson return -EINVAL; 13143c0d9c63SKent Gibson 13152068339aSDipen Patel /* Only allow one event clock source */ 1316272ddba0SKent Gibson if (IS_ENABLED(CONFIG_HTE) && 1317272ddba0SKent Gibson (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) && 13182068339aSDipen Patel (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)) 13192068339aSDipen Patel return -EINVAL; 13202068339aSDipen Patel 132173e03419SKent Gibson /* Edge detection requires explicit input. */ 132273e03419SKent Gibson if ((flags & GPIO_V2_LINE_EDGE_FLAGS) && 132373e03419SKent Gibson !(flags & GPIO_V2_LINE_FLAG_INPUT)) 132473e03419SKent Gibson return -EINVAL; 132573e03419SKent Gibson 13263c0d9c63SKent Gibson /* 13273c0d9c63SKent Gibson * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single 13283c0d9c63SKent Gibson * request. If the hardware actually supports enabling both at the 13293c0d9c63SKent Gibson * same time the electrical result would be disastrous. 13303c0d9c63SKent Gibson */ 13313c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) && 13323c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE)) 13333c0d9c63SKent Gibson return -EINVAL; 13343c0d9c63SKent Gibson 13353c0d9c63SKent Gibson /* Drive requires explicit output direction. */ 13363c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) && 13373c0d9c63SKent Gibson !(flags & GPIO_V2_LINE_FLAG_OUTPUT)) 13383c0d9c63SKent Gibson return -EINVAL; 13393c0d9c63SKent Gibson 13403c0d9c63SKent Gibson /* Bias requires explicit direction. */ 13413c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_BIAS_FLAGS) && 13423c0d9c63SKent Gibson !(flags & GPIO_V2_LINE_DIRECTION_FLAGS)) 13433c0d9c63SKent Gibson return -EINVAL; 13443c0d9c63SKent Gibson 13453c0d9c63SKent Gibson /* Only one bias flag can be set. */ 13463c0d9c63SKent Gibson if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) && 13473c0d9c63SKent Gibson (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | 13483c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) || 13493c0d9c63SKent Gibson ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) && 13503c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) 13513c0d9c63SKent Gibson return -EINVAL; 13523c0d9c63SKent Gibson 13533c0d9c63SKent Gibson return 0; 13543c0d9c63SKent Gibson } 13553c0d9c63SKent Gibson 13563c0d9c63SKent Gibson static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, 13573c0d9c63SKent Gibson unsigned int num_lines) 13583c0d9c63SKent Gibson { 13593c0d9c63SKent Gibson unsigned int i; 13603c0d9c63SKent Gibson u64 flags; 13613c0d9c63SKent Gibson int ret; 13623c0d9c63SKent Gibson 13633c0d9c63SKent Gibson if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX) 13643c0d9c63SKent Gibson return -EINVAL; 13653c0d9c63SKent Gibson 13663c0d9c63SKent Gibson if (memchr_inv(lc->padding, 0, sizeof(lc->padding))) 13673c0d9c63SKent Gibson return -EINVAL; 13683c0d9c63SKent Gibson 13693c0d9c63SKent Gibson for (i = 0; i < num_lines; i++) { 13703c0d9c63SKent Gibson flags = gpio_v2_line_config_flags(lc, i); 13713c0d9c63SKent Gibson ret = gpio_v2_line_flags_validate(flags); 13723c0d9c63SKent Gibson if (ret) 13733c0d9c63SKent Gibson return ret; 137465cff704SKent Gibson 137565cff704SKent Gibson /* debounce requires explicit input */ 137665cff704SKent Gibson if (gpio_v2_line_config_debounced(lc, i) && 137765cff704SKent Gibson !(flags & GPIO_V2_LINE_FLAG_INPUT)) 137865cff704SKent Gibson return -EINVAL; 13793c0d9c63SKent Gibson } 13803c0d9c63SKent Gibson return 0; 13813c0d9c63SKent Gibson } 13823c0d9c63SKent Gibson 13833c0d9c63SKent Gibson static void gpio_v2_line_config_flags_to_desc_flags(u64 flags, 13843c0d9c63SKent Gibson unsigned long *flagsp) 13853c0d9c63SKent Gibson { 13863c0d9c63SKent Gibson assign_bit(FLAG_ACTIVE_LOW, flagsp, 13873c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW); 13883c0d9c63SKent Gibson 13893c0d9c63SKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) 13903c0d9c63SKent Gibson set_bit(FLAG_IS_OUT, flagsp); 13913c0d9c63SKent Gibson else if (flags & GPIO_V2_LINE_FLAG_INPUT) 13923c0d9c63SKent Gibson clear_bit(FLAG_IS_OUT, flagsp); 13933c0d9c63SKent Gibson 139473e03419SKent Gibson assign_bit(FLAG_EDGE_RISING, flagsp, 139573e03419SKent Gibson flags & GPIO_V2_LINE_FLAG_EDGE_RISING); 139673e03419SKent Gibson assign_bit(FLAG_EDGE_FALLING, flagsp, 139773e03419SKent Gibson flags & GPIO_V2_LINE_FLAG_EDGE_FALLING); 139873e03419SKent Gibson 13993c0d9c63SKent Gibson assign_bit(FLAG_OPEN_DRAIN, flagsp, 14003c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN); 14013c0d9c63SKent Gibson assign_bit(FLAG_OPEN_SOURCE, flagsp, 14023c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE); 14033c0d9c63SKent Gibson 14043c0d9c63SKent Gibson assign_bit(FLAG_PULL_UP, flagsp, 14053c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP); 14063c0d9c63SKent Gibson assign_bit(FLAG_PULL_DOWN, flagsp, 14073c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN); 14083c0d9c63SKent Gibson assign_bit(FLAG_BIAS_DISABLE, flagsp, 14093c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED); 141026d060e4SKent Gibson 141126d060e4SKent Gibson assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp, 141226d060e4SKent Gibson flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME); 14132068339aSDipen Patel assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp, 14142068339aSDipen Patel flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE); 14153c0d9c63SKent Gibson } 14163c0d9c63SKent Gibson 14173c0d9c63SKent Gibson static long linereq_get_values(struct linereq *lr, void __user *ip) 14183c0d9c63SKent Gibson { 14193c0d9c63SKent Gibson struct gpio_v2_line_values lv; 14203c0d9c63SKent Gibson DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); 14213c0d9c63SKent Gibson struct gpio_desc **descs; 14223c0d9c63SKent Gibson unsigned int i, didx, num_get; 142365cff704SKent Gibson bool val; 14243c0d9c63SKent Gibson int ret; 14253c0d9c63SKent Gibson 14263c0d9c63SKent Gibson /* NOTE: It's ok to read values of output lines. */ 14273c0d9c63SKent Gibson if (copy_from_user(&lv, ip, sizeof(lv))) 14283c0d9c63SKent Gibson return -EFAULT; 14293c0d9c63SKent Gibson 14303c0d9c63SKent Gibson for (num_get = 0, i = 0; i < lr->num_lines; i++) { 14313c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 14323c0d9c63SKent Gibson num_get++; 14333c0d9c63SKent Gibson descs = &lr->lines[i].desc; 14343c0d9c63SKent Gibson } 14353c0d9c63SKent Gibson } 14363c0d9c63SKent Gibson 14373c0d9c63SKent Gibson if (num_get == 0) 14383c0d9c63SKent Gibson return -EINVAL; 14393c0d9c63SKent Gibson 14403c0d9c63SKent Gibson if (num_get != 1) { 14413c0d9c63SKent Gibson descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL); 14423c0d9c63SKent Gibson if (!descs) 14433c0d9c63SKent Gibson return -ENOMEM; 14443c0d9c63SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 14453c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 14463c0d9c63SKent Gibson descs[didx] = lr->lines[i].desc; 14473c0d9c63SKent Gibson didx++; 14483c0d9c63SKent Gibson } 14493c0d9c63SKent Gibson } 14503c0d9c63SKent Gibson } 14513c0d9c63SKent Gibson ret = gpiod_get_array_value_complex(false, true, num_get, 14523c0d9c63SKent Gibson descs, NULL, vals); 14533c0d9c63SKent Gibson 14543c0d9c63SKent Gibson if (num_get != 1) 14553c0d9c63SKent Gibson kfree(descs); 14563c0d9c63SKent Gibson if (ret) 14573c0d9c63SKent Gibson return ret; 14583c0d9c63SKent Gibson 14593c0d9c63SKent Gibson lv.bits = 0; 14603c0d9c63SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 14613c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 146265cff704SKent Gibson if (lr->lines[i].sw_debounced) 146365cff704SKent Gibson val = debounced_value(&lr->lines[i]); 146465cff704SKent Gibson else 146565cff704SKent Gibson val = test_bit(didx, vals); 146665cff704SKent Gibson if (val) 14673c0d9c63SKent Gibson lv.bits |= BIT_ULL(i); 14683c0d9c63SKent Gibson didx++; 14693c0d9c63SKent Gibson } 14703c0d9c63SKent Gibson } 14713c0d9c63SKent Gibson 14723c0d9c63SKent Gibson if (copy_to_user(ip, &lv, sizeof(lv))) 14733c0d9c63SKent Gibson return -EFAULT; 14743c0d9c63SKent Gibson 14753c0d9c63SKent Gibson return 0; 14763c0d9c63SKent Gibson } 14773c0d9c63SKent Gibson 14787b8e00d9SKent Gibson static long linereq_set_values_unlocked(struct linereq *lr, 14797b8e00d9SKent Gibson struct gpio_v2_line_values *lv) 14807b8e00d9SKent Gibson { 14817b8e00d9SKent Gibson DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); 14827b8e00d9SKent Gibson struct gpio_desc **descs; 14837b8e00d9SKent Gibson unsigned int i, didx, num_set; 14847b8e00d9SKent Gibson int ret; 14857b8e00d9SKent Gibson 14867b8e00d9SKent Gibson bitmap_zero(vals, GPIO_V2_LINES_MAX); 14877b8e00d9SKent Gibson for (num_set = 0, i = 0; i < lr->num_lines; i++) { 14887b8e00d9SKent Gibson if (lv->mask & BIT_ULL(i)) { 14897b8e00d9SKent Gibson if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags)) 14907b8e00d9SKent Gibson return -EPERM; 14917b8e00d9SKent Gibson if (lv->bits & BIT_ULL(i)) 14927b8e00d9SKent Gibson __set_bit(num_set, vals); 14937b8e00d9SKent Gibson num_set++; 14947b8e00d9SKent Gibson descs = &lr->lines[i].desc; 14957b8e00d9SKent Gibson } 14967b8e00d9SKent Gibson } 14977b8e00d9SKent Gibson if (num_set == 0) 14987b8e00d9SKent Gibson return -EINVAL; 14997b8e00d9SKent Gibson 15007b8e00d9SKent Gibson if (num_set != 1) { 15017b8e00d9SKent Gibson /* build compacted desc array and values */ 15027b8e00d9SKent Gibson descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL); 15037b8e00d9SKent Gibson if (!descs) 15047b8e00d9SKent Gibson return -ENOMEM; 15057b8e00d9SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 15067b8e00d9SKent Gibson if (lv->mask & BIT_ULL(i)) { 15077b8e00d9SKent Gibson descs[didx] = lr->lines[i].desc; 15087b8e00d9SKent Gibson didx++; 15097b8e00d9SKent Gibson } 15107b8e00d9SKent Gibson } 15117b8e00d9SKent Gibson } 15127b8e00d9SKent Gibson ret = gpiod_set_array_value_complex(false, true, num_set, 15137b8e00d9SKent Gibson descs, NULL, vals); 15147b8e00d9SKent Gibson 15157b8e00d9SKent Gibson if (num_set != 1) 15167b8e00d9SKent Gibson kfree(descs); 15177b8e00d9SKent Gibson return ret; 15187b8e00d9SKent Gibson } 15197b8e00d9SKent Gibson 15207b8e00d9SKent Gibson static long linereq_set_values(struct linereq *lr, void __user *ip) 15217b8e00d9SKent Gibson { 15227b8e00d9SKent Gibson struct gpio_v2_line_values lv; 15237b8e00d9SKent Gibson int ret; 15247b8e00d9SKent Gibson 15257b8e00d9SKent Gibson if (copy_from_user(&lv, ip, sizeof(lv))) 15267b8e00d9SKent Gibson return -EFAULT; 15277b8e00d9SKent Gibson 15287b8e00d9SKent Gibson mutex_lock(&lr->config_mutex); 15297b8e00d9SKent Gibson 15307b8e00d9SKent Gibson ret = linereq_set_values_unlocked(lr, &lv); 15317b8e00d9SKent Gibson 15327b8e00d9SKent Gibson mutex_unlock(&lr->config_mutex); 15337b8e00d9SKent Gibson 15347b8e00d9SKent Gibson return ret; 15357b8e00d9SKent Gibson } 15367b8e00d9SKent Gibson 1537a54756cbSKent Gibson static long linereq_set_config_unlocked(struct linereq *lr, 1538a54756cbSKent Gibson struct gpio_v2_line_config *lc) 1539a54756cbSKent Gibson { 1540a54756cbSKent Gibson struct gpio_desc *desc; 1541b1a92e94SKent Gibson struct line *line; 1542a54756cbSKent Gibson unsigned int i; 1543b1a92e94SKent Gibson u64 flags, edflags; 1544a54756cbSKent Gibson int ret; 1545a54756cbSKent Gibson 1546a54756cbSKent Gibson for (i = 0; i < lr->num_lines; i++) { 1547b1a92e94SKent Gibson line = &lr->lines[i]; 1548a54756cbSKent Gibson desc = lr->lines[i].desc; 1549a54756cbSKent Gibson flags = gpio_v2_line_config_flags(lc, i); 1550a54756cbSKent Gibson gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 1551b1a92e94SKent Gibson edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS; 1552a54756cbSKent Gibson /* 1553a54756cbSKent Gibson * Lines have to be requested explicitly for input 1554a54756cbSKent Gibson * or output, else the line will be treated "as is". 1555a54756cbSKent Gibson */ 1556a54756cbSKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 1557a54756cbSKent Gibson int val = gpio_v2_line_config_output_value(lc, i); 1558a54756cbSKent Gibson 1559b1a92e94SKent Gibson edge_detector_stop(line); 1560a54756cbSKent Gibson ret = gpiod_direction_output(desc, val); 1561a54756cbSKent Gibson if (ret) 1562a54756cbSKent Gibson return ret; 1563a54756cbSKent Gibson } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { 1564a54756cbSKent Gibson ret = gpiod_direction_input(desc); 1565a54756cbSKent Gibson if (ret) 1566a54756cbSKent Gibson return ret; 1567a54756cbSKent Gibson 1568b1a92e94SKent Gibson ret = edge_detector_update(line, lc, i, edflags); 1569a54756cbSKent Gibson if (ret) 1570a54756cbSKent Gibson return ret; 1571a54756cbSKent Gibson } 1572a54756cbSKent Gibson 1573b1a92e94SKent Gibson WRITE_ONCE(line->edflags, edflags); 1574b1a92e94SKent Gibson 15759ce4ed5bSBartosz Golaszewski gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); 1576a54756cbSKent Gibson } 1577a54756cbSKent Gibson return 0; 1578a54756cbSKent Gibson } 1579a54756cbSKent Gibson 1580a54756cbSKent Gibson static long linereq_set_config(struct linereq *lr, void __user *ip) 1581a54756cbSKent Gibson { 1582a54756cbSKent Gibson struct gpio_v2_line_config lc; 1583a54756cbSKent Gibson int ret; 1584a54756cbSKent Gibson 1585a54756cbSKent Gibson if (copy_from_user(&lc, ip, sizeof(lc))) 1586a54756cbSKent Gibson return -EFAULT; 1587a54756cbSKent Gibson 1588a54756cbSKent Gibson ret = gpio_v2_line_config_validate(&lc, lr->num_lines); 1589a54756cbSKent Gibson if (ret) 1590a54756cbSKent Gibson return ret; 1591a54756cbSKent Gibson 1592a54756cbSKent Gibson mutex_lock(&lr->config_mutex); 1593a54756cbSKent Gibson 1594a54756cbSKent Gibson ret = linereq_set_config_unlocked(lr, &lc); 1595a54756cbSKent Gibson 1596a54756cbSKent Gibson mutex_unlock(&lr->config_mutex); 1597a54756cbSKent Gibson 1598a54756cbSKent Gibson return ret; 1599a54756cbSKent Gibson } 1600a54756cbSKent Gibson 1601bdbbae24SBartosz Golaszewski static long linereq_ioctl_unlocked(struct file *file, unsigned int cmd, 16023c0d9c63SKent Gibson unsigned long arg) 16033c0d9c63SKent Gibson { 16043c0d9c63SKent Gibson struct linereq *lr = file->private_data; 16053c0d9c63SKent Gibson void __user *ip = (void __user *)arg; 16063c0d9c63SKent Gibson 1607533aae7cSBartosz Golaszewski if (!lr->gdev->chip) 1608533aae7cSBartosz Golaszewski return -ENODEV; 1609533aae7cSBartosz Golaszewski 16101cef8b50SAndy Shevchenko switch (cmd) { 16111cef8b50SAndy Shevchenko case GPIO_V2_LINE_GET_VALUES_IOCTL: 16123c0d9c63SKent Gibson return linereq_get_values(lr, ip); 16131cef8b50SAndy Shevchenko case GPIO_V2_LINE_SET_VALUES_IOCTL: 16147b8e00d9SKent Gibson return linereq_set_values(lr, ip); 16151cef8b50SAndy Shevchenko case GPIO_V2_LINE_SET_CONFIG_IOCTL: 1616a54756cbSKent Gibson return linereq_set_config(lr, ip); 16171cef8b50SAndy Shevchenko default: 16183c0d9c63SKent Gibson return -EINVAL; 16193c0d9c63SKent Gibson } 16201cef8b50SAndy Shevchenko } 16213c0d9c63SKent Gibson 1622bdbbae24SBartosz Golaszewski static long linereq_ioctl(struct file *file, unsigned int cmd, 1623bdbbae24SBartosz Golaszewski unsigned long arg) 1624bdbbae24SBartosz Golaszewski { 1625bdbbae24SBartosz Golaszewski struct linereq *lr = file->private_data; 1626bdbbae24SBartosz Golaszewski 1627bdbbae24SBartosz Golaszewski return call_ioctl_locked(file, cmd, arg, lr->gdev, 1628bdbbae24SBartosz Golaszewski linereq_ioctl_unlocked); 1629bdbbae24SBartosz Golaszewski } 1630bdbbae24SBartosz Golaszewski 16313c0d9c63SKent Gibson #ifdef CONFIG_COMPAT 16323c0d9c63SKent Gibson static long linereq_ioctl_compat(struct file *file, unsigned int cmd, 16333c0d9c63SKent Gibson unsigned long arg) 16343c0d9c63SKent Gibson { 16353c0d9c63SKent Gibson return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 16363c0d9c63SKent Gibson } 16373c0d9c63SKent Gibson #endif 16383c0d9c63SKent Gibson 1639bdbbae24SBartosz Golaszewski static __poll_t linereq_poll_unlocked(struct file *file, 164073e03419SKent Gibson struct poll_table_struct *wait) 164173e03419SKent Gibson { 164273e03419SKent Gibson struct linereq *lr = file->private_data; 164373e03419SKent Gibson __poll_t events = 0; 164473e03419SKent Gibson 1645533aae7cSBartosz Golaszewski if (!lr->gdev->chip) 1646533aae7cSBartosz Golaszewski return EPOLLHUP | EPOLLERR; 1647533aae7cSBartosz Golaszewski 164873e03419SKent Gibson poll_wait(file, &lr->wait, wait); 164973e03419SKent Gibson 165073e03419SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events, 165173e03419SKent Gibson &lr->wait.lock)) 165273e03419SKent Gibson events = EPOLLIN | EPOLLRDNORM; 165373e03419SKent Gibson 165473e03419SKent Gibson return events; 165573e03419SKent Gibson } 165673e03419SKent Gibson 1657bdbbae24SBartosz Golaszewski static __poll_t linereq_poll(struct file *file, 1658bdbbae24SBartosz Golaszewski struct poll_table_struct *wait) 1659bdbbae24SBartosz Golaszewski { 1660bdbbae24SBartosz Golaszewski struct linereq *lr = file->private_data; 1661bdbbae24SBartosz Golaszewski 1662bdbbae24SBartosz Golaszewski return call_poll_locked(file, wait, lr->gdev, linereq_poll_unlocked); 1663bdbbae24SBartosz Golaszewski } 1664bdbbae24SBartosz Golaszewski 1665bdbbae24SBartosz Golaszewski static ssize_t linereq_read_unlocked(struct file *file, char __user *buf, 1666bdbbae24SBartosz Golaszewski size_t count, loff_t *f_ps) 166773e03419SKent Gibson { 166873e03419SKent Gibson struct linereq *lr = file->private_data; 166973e03419SKent Gibson struct gpio_v2_line_event le; 167073e03419SKent Gibson ssize_t bytes_read = 0; 167173e03419SKent Gibson int ret; 167273e03419SKent Gibson 1673533aae7cSBartosz Golaszewski if (!lr->gdev->chip) 1674533aae7cSBartosz Golaszewski return -ENODEV; 1675533aae7cSBartosz Golaszewski 167673e03419SKent Gibson if (count < sizeof(le)) 167773e03419SKent Gibson return -EINVAL; 167873e03419SKent Gibson 167973e03419SKent Gibson do { 168073e03419SKent Gibson spin_lock(&lr->wait.lock); 168173e03419SKent Gibson if (kfifo_is_empty(&lr->events)) { 168273e03419SKent Gibson if (bytes_read) { 168373e03419SKent Gibson spin_unlock(&lr->wait.lock); 168473e03419SKent Gibson return bytes_read; 168573e03419SKent Gibson } 168673e03419SKent Gibson 168773e03419SKent Gibson if (file->f_flags & O_NONBLOCK) { 168873e03419SKent Gibson spin_unlock(&lr->wait.lock); 168973e03419SKent Gibson return -EAGAIN; 169073e03419SKent Gibson } 169173e03419SKent Gibson 169273e03419SKent Gibson ret = wait_event_interruptible_locked(lr->wait, 169373e03419SKent Gibson !kfifo_is_empty(&lr->events)); 169473e03419SKent Gibson if (ret) { 169573e03419SKent Gibson spin_unlock(&lr->wait.lock); 169673e03419SKent Gibson return ret; 169773e03419SKent Gibson } 169873e03419SKent Gibson } 169973e03419SKent Gibson 170073e03419SKent Gibson ret = kfifo_out(&lr->events, &le, 1); 170173e03419SKent Gibson spin_unlock(&lr->wait.lock); 170273e03419SKent Gibson if (ret != 1) { 170373e03419SKent Gibson /* 170473e03419SKent Gibson * This should never happen - we were holding the 170573e03419SKent Gibson * lock from the moment we learned the fifo is no 170673e03419SKent Gibson * longer empty until now. 170773e03419SKent Gibson */ 170873e03419SKent Gibson ret = -EIO; 170973e03419SKent Gibson break; 171073e03419SKent Gibson } 171173e03419SKent Gibson 171273e03419SKent Gibson if (copy_to_user(buf + bytes_read, &le, sizeof(le))) 171373e03419SKent Gibson return -EFAULT; 171473e03419SKent Gibson bytes_read += sizeof(le); 171573e03419SKent Gibson } while (count >= bytes_read + sizeof(le)); 171673e03419SKent Gibson 171773e03419SKent Gibson return bytes_read; 171873e03419SKent Gibson } 171973e03419SKent Gibson 1720bdbbae24SBartosz Golaszewski static ssize_t linereq_read(struct file *file, char __user *buf, 1721bdbbae24SBartosz Golaszewski size_t count, loff_t *f_ps) 1722bdbbae24SBartosz Golaszewski { 1723bdbbae24SBartosz Golaszewski struct linereq *lr = file->private_data; 1724bdbbae24SBartosz Golaszewski 1725bdbbae24SBartosz Golaszewski return call_read_locked(file, buf, count, f_ps, lr->gdev, 1726bdbbae24SBartosz Golaszewski linereq_read_unlocked); 1727bdbbae24SBartosz Golaszewski } 1728bdbbae24SBartosz Golaszewski 17293c0d9c63SKent Gibson static void linereq_free(struct linereq *lr) 17303c0d9c63SKent Gibson { 1731*309364bcSKent Gibson struct line *line; 17323c0d9c63SKent Gibson unsigned int i; 17333c0d9c63SKent Gibson 1734a0dda508SBartosz Golaszewski if (lr->device_unregistered_nb.notifier_call) 1735a0dda508SBartosz Golaszewski blocking_notifier_chain_unregister(&lr->gdev->device_notifier, 1736a0dda508SBartosz Golaszewski &lr->device_unregistered_nb); 1737a0dda508SBartosz Golaszewski 17383c0d9c63SKent Gibson for (i = 0; i < lr->num_lines; i++) { 1739*309364bcSKent Gibson line = &lr->lines[i]; 1740*309364bcSKent Gibson if (!line->desc) 1741*309364bcSKent Gibson continue; 1742*309364bcSKent Gibson 1743*309364bcSKent Gibson edge_detector_stop(line); 1744*309364bcSKent Gibson if (line_has_supinfo(line)) 1745*309364bcSKent Gibson supinfo_erase(line); 1746*309364bcSKent Gibson gpiod_free(line->desc); 1747160d6e40SKent Gibson } 174873e03419SKent Gibson kfifo_free(&lr->events); 17493c0d9c63SKent Gibson kfree(lr->label); 1750dc0989e3SAndy Shevchenko gpio_device_put(lr->gdev); 17513c0d9c63SKent Gibson kfree(lr); 17523c0d9c63SKent Gibson } 17533c0d9c63SKent Gibson 17543c0d9c63SKent Gibson static int linereq_release(struct inode *inode, struct file *file) 17553c0d9c63SKent Gibson { 17563c0d9c63SKent Gibson struct linereq *lr = file->private_data; 17573c0d9c63SKent Gibson 17583c0d9c63SKent Gibson linereq_free(lr); 17593c0d9c63SKent Gibson return 0; 17603c0d9c63SKent Gibson } 17613c0d9c63SKent Gibson 17620ae3109aSBartosz Golaszewski #ifdef CONFIG_PROC_FS 17630ae3109aSBartosz Golaszewski static void linereq_show_fdinfo(struct seq_file *out, struct file *file) 17640ae3109aSBartosz Golaszewski { 17650ae3109aSBartosz Golaszewski struct linereq *lr = file->private_data; 17660ae3109aSBartosz Golaszewski struct device *dev = &lr->gdev->dev; 17670ae3109aSBartosz Golaszewski u16 i; 17680ae3109aSBartosz Golaszewski 17690ae3109aSBartosz Golaszewski seq_printf(out, "gpio-chip:\t%s\n", dev_name(dev)); 17700ae3109aSBartosz Golaszewski 17710ae3109aSBartosz Golaszewski for (i = 0; i < lr->num_lines; i++) 17720ae3109aSBartosz Golaszewski seq_printf(out, "gpio-line:\t%d\n", 17730ae3109aSBartosz Golaszewski gpio_chip_hwgpio(lr->lines[i].desc)); 17740ae3109aSBartosz Golaszewski } 17750ae3109aSBartosz Golaszewski #endif 17760ae3109aSBartosz Golaszewski 17773c0d9c63SKent Gibson static const struct file_operations line_fileops = { 17783c0d9c63SKent Gibson .release = linereq_release, 177973e03419SKent Gibson .read = linereq_read, 178073e03419SKent Gibson .poll = linereq_poll, 17813c0d9c63SKent Gibson .owner = THIS_MODULE, 17823c0d9c63SKent Gibson .llseek = noop_llseek, 17833c0d9c63SKent Gibson .unlocked_ioctl = linereq_ioctl, 17843c0d9c63SKent Gibson #ifdef CONFIG_COMPAT 17853c0d9c63SKent Gibson .compat_ioctl = linereq_ioctl_compat, 17863c0d9c63SKent Gibson #endif 17870ae3109aSBartosz Golaszewski #ifdef CONFIG_PROC_FS 17880ae3109aSBartosz Golaszewski .show_fdinfo = linereq_show_fdinfo, 17890ae3109aSBartosz Golaszewski #endif 17903c0d9c63SKent Gibson }; 17913c0d9c63SKent Gibson 17923c0d9c63SKent Gibson static int linereq_create(struct gpio_device *gdev, void __user *ip) 17933c0d9c63SKent Gibson { 17943c0d9c63SKent Gibson struct gpio_v2_line_request ulr; 17953c0d9c63SKent Gibson struct gpio_v2_line_config *lc; 17963c0d9c63SKent Gibson struct linereq *lr; 17973c0d9c63SKent Gibson struct file *file; 1798b1a92e94SKent Gibson u64 flags, edflags; 17993c0d9c63SKent Gibson unsigned int i; 18003c0d9c63SKent Gibson int fd, ret; 18013c0d9c63SKent Gibson 18023c0d9c63SKent Gibson if (copy_from_user(&ulr, ip, sizeof(ulr))) 18033c0d9c63SKent Gibson return -EFAULT; 18043c0d9c63SKent Gibson 18053c0d9c63SKent Gibson if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX)) 18063c0d9c63SKent Gibson return -EINVAL; 18073c0d9c63SKent Gibson 18083c0d9c63SKent Gibson if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding))) 18093c0d9c63SKent Gibson return -EINVAL; 18103c0d9c63SKent Gibson 18113c0d9c63SKent Gibson lc = &ulr.config; 18123c0d9c63SKent Gibson ret = gpio_v2_line_config_validate(lc, ulr.num_lines); 18133c0d9c63SKent Gibson if (ret) 18143c0d9c63SKent Gibson return ret; 18153c0d9c63SKent Gibson 18163c0d9c63SKent Gibson lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); 18173c0d9c63SKent Gibson if (!lr) 18183c0d9c63SKent Gibson return -ENOMEM; 18193c0d9c63SKent Gibson 1820dc0989e3SAndy Shevchenko lr->gdev = gpio_device_get(gdev); 18213c0d9c63SKent Gibson 182265cff704SKent Gibson for (i = 0; i < ulr.num_lines; i++) { 182373e03419SKent Gibson lr->lines[i].req = lr; 182465cff704SKent Gibson WRITE_ONCE(lr->lines[i].sw_debounced, 0); 182565cff704SKent Gibson INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func); 182665cff704SKent Gibson } 182773e03419SKent Gibson 1828f188ac12SKent Gibson if (ulr.consumer[0] != '\0') { 18293c0d9c63SKent Gibson /* label is only initialized if consumer is set */ 1830f188ac12SKent Gibson lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1, 1831f188ac12SKent Gibson GFP_KERNEL); 18323c0d9c63SKent Gibson if (!lr->label) { 18333c0d9c63SKent Gibson ret = -ENOMEM; 18343c0d9c63SKent Gibson goto out_free_linereq; 18353c0d9c63SKent Gibson } 18363c0d9c63SKent Gibson } 18373c0d9c63SKent Gibson 1838a54756cbSKent Gibson mutex_init(&lr->config_mutex); 183973e03419SKent Gibson init_waitqueue_head(&lr->wait); 184073e03419SKent Gibson lr->event_buffer_size = ulr.event_buffer_size; 184173e03419SKent Gibson if (lr->event_buffer_size == 0) 184273e03419SKent Gibson lr->event_buffer_size = ulr.num_lines * 16; 184373e03419SKent Gibson else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16) 184473e03419SKent Gibson lr->event_buffer_size = GPIO_V2_LINES_MAX * 16; 184573e03419SKent Gibson 184673e03419SKent Gibson atomic_set(&lr->seqno, 0); 18473c0d9c63SKent Gibson lr->num_lines = ulr.num_lines; 18483c0d9c63SKent Gibson 18493c0d9c63SKent Gibson /* Request each GPIO */ 18503c0d9c63SKent Gibson for (i = 0; i < ulr.num_lines; i++) { 18513c0d9c63SKent Gibson u32 offset = ulr.offsets[i]; 18523c0d9c63SKent Gibson struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); 18533c0d9c63SKent Gibson 18543c0d9c63SKent Gibson if (IS_ERR(desc)) { 18553c0d9c63SKent Gibson ret = PTR_ERR(desc); 18563c0d9c63SKent Gibson goto out_free_linereq; 18573c0d9c63SKent Gibson } 18583c0d9c63SKent Gibson 185995a4eed7SAndy Shevchenko ret = gpiod_request_user(desc, lr->label); 18603c0d9c63SKent Gibson if (ret) 18613c0d9c63SKent Gibson goto out_free_linereq; 18623c0d9c63SKent Gibson 18633c0d9c63SKent Gibson lr->lines[i].desc = desc; 18643c0d9c63SKent Gibson flags = gpio_v2_line_config_flags(lc, i); 18653c0d9c63SKent Gibson gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 18663c0d9c63SKent Gibson 18673c0d9c63SKent Gibson ret = gpiod_set_transitory(desc, false); 18683c0d9c63SKent Gibson if (ret < 0) 18693c0d9c63SKent Gibson goto out_free_linereq; 18703c0d9c63SKent Gibson 1871b1a92e94SKent Gibson edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS; 18723c0d9c63SKent Gibson /* 18733c0d9c63SKent Gibson * Lines have to be requested explicitly for input 18743c0d9c63SKent Gibson * or output, else the line will be treated "as is". 18753c0d9c63SKent Gibson */ 18763c0d9c63SKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 18773c0d9c63SKent Gibson int val = gpio_v2_line_config_output_value(lc, i); 18783c0d9c63SKent Gibson 18793c0d9c63SKent Gibson ret = gpiod_direction_output(desc, val); 18803c0d9c63SKent Gibson if (ret) 18813c0d9c63SKent Gibson goto out_free_linereq; 18823c0d9c63SKent Gibson } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { 18833c0d9c63SKent Gibson ret = gpiod_direction_input(desc); 18843c0d9c63SKent Gibson if (ret) 18853c0d9c63SKent Gibson goto out_free_linereq; 188673e03419SKent Gibson 188765cff704SKent Gibson ret = edge_detector_setup(&lr->lines[i], lc, i, 1888b1a92e94SKent Gibson edflags); 188973e03419SKent Gibson if (ret) 189073e03419SKent Gibson goto out_free_linereq; 18913c0d9c63SKent Gibson } 18923c0d9c63SKent Gibson 1893b1a92e94SKent Gibson lr->lines[i].edflags = edflags; 1894b1a92e94SKent Gibson 18959ce4ed5bSBartosz Golaszewski gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED); 18963c0d9c63SKent Gibson 18973c0d9c63SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", 18983c0d9c63SKent Gibson offset); 18993c0d9c63SKent Gibson } 19003c0d9c63SKent Gibson 1901a0dda508SBartosz Golaszewski lr->device_unregistered_nb.notifier_call = linereq_unregistered_notify; 1902a0dda508SBartosz Golaszewski ret = blocking_notifier_chain_register(&gdev->device_notifier, 1903a0dda508SBartosz Golaszewski &lr->device_unregistered_nb); 1904a0dda508SBartosz Golaszewski if (ret) 1905a0dda508SBartosz Golaszewski goto out_free_linereq; 1906a0dda508SBartosz Golaszewski 19073c0d9c63SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 19083c0d9c63SKent Gibson if (fd < 0) { 19093c0d9c63SKent Gibson ret = fd; 19103c0d9c63SKent Gibson goto out_free_linereq; 19113c0d9c63SKent Gibson } 19123c0d9c63SKent Gibson 19133c0d9c63SKent Gibson file = anon_inode_getfile("gpio-line", &line_fileops, lr, 19143c0d9c63SKent Gibson O_RDONLY | O_CLOEXEC); 19153c0d9c63SKent Gibson if (IS_ERR(file)) { 19163c0d9c63SKent Gibson ret = PTR_ERR(file); 19173c0d9c63SKent Gibson goto out_put_unused_fd; 19183c0d9c63SKent Gibson } 19193c0d9c63SKent Gibson 19203c0d9c63SKent Gibson ulr.fd = fd; 19213c0d9c63SKent Gibson if (copy_to_user(ip, &ulr, sizeof(ulr))) { 19223c0d9c63SKent Gibson /* 19233c0d9c63SKent Gibson * fput() will trigger the release() callback, so do not go onto 19243c0d9c63SKent Gibson * the regular error cleanup path here. 19253c0d9c63SKent Gibson */ 19263c0d9c63SKent Gibson fput(file); 19273c0d9c63SKent Gibson put_unused_fd(fd); 19283c0d9c63SKent Gibson return -EFAULT; 19293c0d9c63SKent Gibson } 19303c0d9c63SKent Gibson 19313c0d9c63SKent Gibson fd_install(fd, file); 19323c0d9c63SKent Gibson 19333c0d9c63SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", 19343c0d9c63SKent Gibson lr->num_lines); 19353c0d9c63SKent Gibson 19363c0d9c63SKent Gibson return 0; 19373c0d9c63SKent Gibson 19383c0d9c63SKent Gibson out_put_unused_fd: 19393c0d9c63SKent Gibson put_unused_fd(fd); 19403c0d9c63SKent Gibson out_free_linereq: 19413c0d9c63SKent Gibson linereq_free(lr); 19423c0d9c63SKent Gibson return ret; 19433c0d9c63SKent Gibson } 19443c0d9c63SKent Gibson 19453c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 1946925ca369SKent Gibson 1947925ca369SKent Gibson /* 1948925ca369SKent Gibson * GPIO line event management 1949925ca369SKent Gibson */ 1950925ca369SKent Gibson 1951925ca369SKent Gibson /** 1952925ca369SKent Gibson * struct lineevent_state - contains the state of a userspace event 1953925ca369SKent Gibson * @gdev: the GPIO device the event pertains to 1954925ca369SKent Gibson * @label: consumer label used to tag descriptors 1955925ca369SKent Gibson * @desc: the GPIO descriptor held by this event 1956925ca369SKent Gibson * @eflags: the event flags this line was requested with 1957925ca369SKent Gibson * @irq: the interrupt that trigger in response to events on this GPIO 1958925ca369SKent Gibson * @wait: wait queue that handles blocking reads of events 195991043f55SBartosz Golaszewski * @device_unregistered_nb: notifier block for receiving gdev unregister events 1960925ca369SKent Gibson * @events: KFIFO for the GPIO events 1961925ca369SKent Gibson * @timestamp: cache for the timestamp storing it between hardirq 1962925ca369SKent Gibson * and IRQ thread, used to bring the timestamp close to the actual 1963925ca369SKent Gibson * event 1964925ca369SKent Gibson */ 1965925ca369SKent Gibson struct lineevent_state { 1966925ca369SKent Gibson struct gpio_device *gdev; 1967925ca369SKent Gibson const char *label; 1968925ca369SKent Gibson struct gpio_desc *desc; 1969925ca369SKent Gibson u32 eflags; 1970925ca369SKent Gibson int irq; 1971925ca369SKent Gibson wait_queue_head_t wait; 197291043f55SBartosz Golaszewski struct notifier_block device_unregistered_nb; 1973925ca369SKent Gibson DECLARE_KFIFO(events, struct gpioevent_data, 16); 1974925ca369SKent Gibson u64 timestamp; 1975925ca369SKent Gibson }; 1976925ca369SKent Gibson 1977925ca369SKent Gibson #define GPIOEVENT_REQUEST_VALID_FLAGS \ 1978925ca369SKent Gibson (GPIOEVENT_REQUEST_RISING_EDGE | \ 1979925ca369SKent Gibson GPIOEVENT_REQUEST_FALLING_EDGE) 1980925ca369SKent Gibson 1981bdbbae24SBartosz Golaszewski static __poll_t lineevent_poll_unlocked(struct file *file, 1982925ca369SKent Gibson struct poll_table_struct *wait) 1983925ca369SKent Gibson { 198449bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1985925ca369SKent Gibson __poll_t events = 0; 1986925ca369SKent Gibson 1987533aae7cSBartosz Golaszewski if (!le->gdev->chip) 1988533aae7cSBartosz Golaszewski return EPOLLHUP | EPOLLERR; 1989533aae7cSBartosz Golaszewski 199049bc5279SKent Gibson poll_wait(file, &le->wait, wait); 1991925ca369SKent Gibson 1992925ca369SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock)) 1993925ca369SKent Gibson events = EPOLLIN | EPOLLRDNORM; 1994925ca369SKent Gibson 1995925ca369SKent Gibson return events; 1996925ca369SKent Gibson } 1997925ca369SKent Gibson 1998bdbbae24SBartosz Golaszewski static __poll_t lineevent_poll(struct file *file, 1999bdbbae24SBartosz Golaszewski struct poll_table_struct *wait) 2000bdbbae24SBartosz Golaszewski { 2001bdbbae24SBartosz Golaszewski struct lineevent_state *le = file->private_data; 2002bdbbae24SBartosz Golaszewski 2003bdbbae24SBartosz Golaszewski return call_poll_locked(file, wait, le->gdev, lineevent_poll_unlocked); 2004bdbbae24SBartosz Golaszewski } 2005bdbbae24SBartosz Golaszewski 200691043f55SBartosz Golaszewski static int lineevent_unregistered_notify(struct notifier_block *nb, 200791043f55SBartosz Golaszewski unsigned long action, void *data) 200891043f55SBartosz Golaszewski { 200991043f55SBartosz Golaszewski struct lineevent_state *le = container_of(nb, struct lineevent_state, 201091043f55SBartosz Golaszewski device_unregistered_nb); 201191043f55SBartosz Golaszewski 201291043f55SBartosz Golaszewski wake_up_poll(&le->wait, EPOLLIN | EPOLLERR); 201391043f55SBartosz Golaszewski 201491043f55SBartosz Golaszewski return NOTIFY_OK; 201591043f55SBartosz Golaszewski } 201691043f55SBartosz Golaszewski 20175ad284abSAndy Shevchenko struct compat_gpioeevent_data { 20185ad284abSAndy Shevchenko compat_u64 timestamp; 20195ad284abSAndy Shevchenko u32 id; 20205ad284abSAndy Shevchenko }; 20215ad284abSAndy Shevchenko 2022bdbbae24SBartosz Golaszewski static ssize_t lineevent_read_unlocked(struct file *file, char __user *buf, 2023bdbbae24SBartosz Golaszewski size_t count, loff_t *f_ps) 2024925ca369SKent Gibson { 202549bc5279SKent Gibson struct lineevent_state *le = file->private_data; 2026925ca369SKent Gibson struct gpioevent_data ge; 2027925ca369SKent Gibson ssize_t bytes_read = 0; 20285ad284abSAndy Shevchenko ssize_t ge_size; 2029925ca369SKent Gibson int ret; 2030925ca369SKent Gibson 2031533aae7cSBartosz Golaszewski if (!le->gdev->chip) 2032533aae7cSBartosz Golaszewski return -ENODEV; 2033533aae7cSBartosz Golaszewski 20345ad284abSAndy Shevchenko /* 20355ad284abSAndy Shevchenko * When compatible system call is being used the struct gpioevent_data, 20365ad284abSAndy Shevchenko * in case of at least ia32, has different size due to the alignment 20375ad284abSAndy Shevchenko * differences. Because we have first member 64 bits followed by one of 20385ad284abSAndy Shevchenko * 32 bits there is no gap between them. The only difference is the 20395ad284abSAndy Shevchenko * padding at the end of the data structure. Hence, we calculate the 20405ad284abSAndy Shevchenko * actual sizeof() and pass this as an argument to copy_to_user() to 20415ad284abSAndy Shevchenko * drop unneeded bytes from the output. 20425ad284abSAndy Shevchenko */ 2043163d1719SAndy Shevchenko if (compat_need_64bit_alignment_fixup()) 2044163d1719SAndy Shevchenko ge_size = sizeof(struct compat_gpioeevent_data); 2045163d1719SAndy Shevchenko else 2046163d1719SAndy Shevchenko ge_size = sizeof(struct gpioevent_data); 20475ad284abSAndy Shevchenko if (count < ge_size) 2048925ca369SKent Gibson return -EINVAL; 2049925ca369SKent Gibson 2050925ca369SKent Gibson do { 2051925ca369SKent Gibson spin_lock(&le->wait.lock); 2052925ca369SKent Gibson if (kfifo_is_empty(&le->events)) { 2053925ca369SKent Gibson if (bytes_read) { 2054925ca369SKent Gibson spin_unlock(&le->wait.lock); 2055925ca369SKent Gibson return bytes_read; 2056925ca369SKent Gibson } 2057925ca369SKent Gibson 205849bc5279SKent Gibson if (file->f_flags & O_NONBLOCK) { 2059925ca369SKent Gibson spin_unlock(&le->wait.lock); 2060925ca369SKent Gibson return -EAGAIN; 2061925ca369SKent Gibson } 2062925ca369SKent Gibson 2063925ca369SKent Gibson ret = wait_event_interruptible_locked(le->wait, 2064925ca369SKent Gibson !kfifo_is_empty(&le->events)); 2065925ca369SKent Gibson if (ret) { 2066925ca369SKent Gibson spin_unlock(&le->wait.lock); 2067925ca369SKent Gibson return ret; 2068925ca369SKent Gibson } 2069925ca369SKent Gibson } 2070925ca369SKent Gibson 2071925ca369SKent Gibson ret = kfifo_out(&le->events, &ge, 1); 2072925ca369SKent Gibson spin_unlock(&le->wait.lock); 2073925ca369SKent Gibson if (ret != 1) { 2074925ca369SKent Gibson /* 2075925ca369SKent Gibson * This should never happen - we were holding the lock 2076925ca369SKent Gibson * from the moment we learned the fifo is no longer 2077925ca369SKent Gibson * empty until now. 2078925ca369SKent Gibson */ 2079925ca369SKent Gibson ret = -EIO; 2080925ca369SKent Gibson break; 2081925ca369SKent Gibson } 2082925ca369SKent Gibson 20835ad284abSAndy Shevchenko if (copy_to_user(buf + bytes_read, &ge, ge_size)) 2084925ca369SKent Gibson return -EFAULT; 20855ad284abSAndy Shevchenko bytes_read += ge_size; 20865ad284abSAndy Shevchenko } while (count >= bytes_read + ge_size); 2087925ca369SKent Gibson 2088925ca369SKent Gibson return bytes_read; 2089925ca369SKent Gibson } 2090925ca369SKent Gibson 2091bdbbae24SBartosz Golaszewski static ssize_t lineevent_read(struct file *file, char __user *buf, 2092bdbbae24SBartosz Golaszewski size_t count, loff_t *f_ps) 2093bdbbae24SBartosz Golaszewski { 2094bdbbae24SBartosz Golaszewski struct lineevent_state *le = file->private_data; 2095bdbbae24SBartosz Golaszewski 2096bdbbae24SBartosz Golaszewski return call_read_locked(file, buf, count, f_ps, le->gdev, 2097bdbbae24SBartosz Golaszewski lineevent_read_unlocked); 2098bdbbae24SBartosz Golaszewski } 2099bdbbae24SBartosz Golaszewski 210046824272SKent Gibson static void lineevent_free(struct lineevent_state *le) 2101925ca369SKent Gibson { 210291043f55SBartosz Golaszewski if (le->device_unregistered_nb.notifier_call) 210391043f55SBartosz Golaszewski blocking_notifier_chain_unregister(&le->gdev->device_notifier, 210491043f55SBartosz Golaszewski &le->device_unregistered_nb); 210546824272SKent Gibson if (le->irq) 210621bc9b15SBartosz Golaszewski free_irq_label(free_irq(le->irq, le)); 210746824272SKent Gibson if (le->desc) 2108925ca369SKent Gibson gpiod_free(le->desc); 2109925ca369SKent Gibson kfree(le->label); 2110dc0989e3SAndy Shevchenko gpio_device_put(le->gdev); 2111925ca369SKent Gibson kfree(le); 211246824272SKent Gibson } 211346824272SKent Gibson 211446824272SKent Gibson static int lineevent_release(struct inode *inode, struct file *file) 211546824272SKent Gibson { 211646824272SKent Gibson lineevent_free(file->private_data); 2117925ca369SKent Gibson return 0; 2118925ca369SKent Gibson } 2119925ca369SKent Gibson 2120bdbbae24SBartosz Golaszewski static long lineevent_ioctl_unlocked(struct file *file, unsigned int cmd, 2121925ca369SKent Gibson unsigned long arg) 2122925ca369SKent Gibson { 212349bc5279SKent Gibson struct lineevent_state *le = file->private_data; 2124925ca369SKent Gibson void __user *ip = (void __user *)arg; 2125925ca369SKent Gibson struct gpiohandle_data ghd; 2126925ca369SKent Gibson 2127533aae7cSBartosz Golaszewski if (!le->gdev->chip) 2128533aae7cSBartosz Golaszewski return -ENODEV; 2129533aae7cSBartosz Golaszewski 2130925ca369SKent Gibson /* 2131925ca369SKent Gibson * We can get the value for an event line but not set it, 2132925ca369SKent Gibson * because it is input by definition. 2133925ca369SKent Gibson */ 2134925ca369SKent Gibson if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { 2135925ca369SKent Gibson int val; 2136925ca369SKent Gibson 2137925ca369SKent Gibson memset(&ghd, 0, sizeof(ghd)); 2138925ca369SKent Gibson 2139925ca369SKent Gibson val = gpiod_get_value_cansleep(le->desc); 2140925ca369SKent Gibson if (val < 0) 2141925ca369SKent Gibson return val; 2142925ca369SKent Gibson ghd.values[0] = val; 2143925ca369SKent Gibson 2144925ca369SKent Gibson if (copy_to_user(ip, &ghd, sizeof(ghd))) 2145925ca369SKent Gibson return -EFAULT; 2146925ca369SKent Gibson 2147925ca369SKent Gibson return 0; 2148925ca369SKent Gibson } 2149925ca369SKent Gibson return -EINVAL; 2150925ca369SKent Gibson } 2151925ca369SKent Gibson 2152bdbbae24SBartosz Golaszewski static long lineevent_ioctl(struct file *file, unsigned int cmd, 2153bdbbae24SBartosz Golaszewski unsigned long arg) 2154bdbbae24SBartosz Golaszewski { 2155bdbbae24SBartosz Golaszewski struct lineevent_state *le = file->private_data; 2156bdbbae24SBartosz Golaszewski 2157bdbbae24SBartosz Golaszewski return call_ioctl_locked(file, cmd, arg, le->gdev, 2158bdbbae24SBartosz Golaszewski lineevent_ioctl_unlocked); 2159bdbbae24SBartosz Golaszewski } 2160bdbbae24SBartosz Golaszewski 2161925ca369SKent Gibson #ifdef CONFIG_COMPAT 216249bc5279SKent Gibson static long lineevent_ioctl_compat(struct file *file, unsigned int cmd, 2163925ca369SKent Gibson unsigned long arg) 2164925ca369SKent Gibson { 216549bc5279SKent Gibson return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 2166925ca369SKent Gibson } 2167925ca369SKent Gibson #endif 2168925ca369SKent Gibson 2169925ca369SKent Gibson static const struct file_operations lineevent_fileops = { 2170925ca369SKent Gibson .release = lineevent_release, 2171925ca369SKent Gibson .read = lineevent_read, 2172925ca369SKent Gibson .poll = lineevent_poll, 2173925ca369SKent Gibson .owner = THIS_MODULE, 2174925ca369SKent Gibson .llseek = noop_llseek, 2175925ca369SKent Gibson .unlocked_ioctl = lineevent_ioctl, 2176925ca369SKent Gibson #ifdef CONFIG_COMPAT 2177925ca369SKent Gibson .compat_ioctl = lineevent_ioctl_compat, 2178925ca369SKent Gibson #endif 2179925ca369SKent Gibson }; 2180925ca369SKent Gibson 2181925ca369SKent Gibson static irqreturn_t lineevent_irq_thread(int irq, void *p) 2182925ca369SKent Gibson { 2183925ca369SKent Gibson struct lineevent_state *le = p; 2184925ca369SKent Gibson struct gpioevent_data ge; 2185925ca369SKent Gibson int ret; 2186925ca369SKent Gibson 2187925ca369SKent Gibson /* Do not leak kernel stack to userspace */ 2188925ca369SKent Gibson memset(&ge, 0, sizeof(ge)); 2189925ca369SKent Gibson 2190925ca369SKent Gibson /* 2191925ca369SKent Gibson * We may be running from a nested threaded interrupt in which case 2192925ca369SKent Gibson * we didn't get the timestamp from lineevent_irq_handler(). 2193925ca369SKent Gibson */ 2194925ca369SKent Gibson if (!le->timestamp) 2195925ca369SKent Gibson ge.timestamp = ktime_get_ns(); 2196925ca369SKent Gibson else 2197925ca369SKent Gibson ge.timestamp = le->timestamp; 2198925ca369SKent Gibson 2199925ca369SKent Gibson if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE 2200925ca369SKent Gibson && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { 2201925ca369SKent Gibson int level = gpiod_get_value_cansleep(le->desc); 2202925ca369SKent Gibson 2203925ca369SKent Gibson if (level) 2204925ca369SKent Gibson /* Emit low-to-high event */ 2205925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_RISING_EDGE; 2206925ca369SKent Gibson else 2207925ca369SKent Gibson /* Emit high-to-low event */ 2208925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_FALLING_EDGE; 2209925ca369SKent Gibson } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) { 2210925ca369SKent Gibson /* Emit low-to-high event */ 2211925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_RISING_EDGE; 2212925ca369SKent Gibson } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { 2213925ca369SKent Gibson /* Emit high-to-low event */ 2214925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_FALLING_EDGE; 2215925ca369SKent Gibson } else { 2216925ca369SKent Gibson return IRQ_NONE; 2217925ca369SKent Gibson } 2218925ca369SKent Gibson 2219925ca369SKent Gibson ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge, 2220925ca369SKent Gibson 1, &le->wait.lock); 2221925ca369SKent Gibson if (ret) 2222925ca369SKent Gibson wake_up_poll(&le->wait, EPOLLIN); 2223925ca369SKent Gibson else 2224925ca369SKent Gibson pr_debug_ratelimited("event FIFO is full - event dropped\n"); 2225925ca369SKent Gibson 2226925ca369SKent Gibson return IRQ_HANDLED; 2227925ca369SKent Gibson } 2228925ca369SKent Gibson 2229925ca369SKent Gibson static irqreturn_t lineevent_irq_handler(int irq, void *p) 2230925ca369SKent Gibson { 2231925ca369SKent Gibson struct lineevent_state *le = p; 2232925ca369SKent Gibson 2233925ca369SKent Gibson /* 2234925ca369SKent Gibson * Just store the timestamp in hardirq context so we get it as 2235925ca369SKent Gibson * close in time as possible to the actual event. 2236925ca369SKent Gibson */ 2237925ca369SKent Gibson le->timestamp = ktime_get_ns(); 2238925ca369SKent Gibson 2239925ca369SKent Gibson return IRQ_WAKE_THREAD; 2240925ca369SKent Gibson } 2241925ca369SKent Gibson 2242925ca369SKent Gibson static int lineevent_create(struct gpio_device *gdev, void __user *ip) 2243925ca369SKent Gibson { 2244925ca369SKent Gibson struct gpioevent_request eventreq; 2245925ca369SKent Gibson struct lineevent_state *le; 2246925ca369SKent Gibson struct gpio_desc *desc; 2247925ca369SKent Gibson struct file *file; 2248925ca369SKent Gibson u32 offset; 2249925ca369SKent Gibson u32 lflags; 2250925ca369SKent Gibson u32 eflags; 2251925ca369SKent Gibson int fd; 2252925ca369SKent Gibson int ret; 225346824272SKent Gibson int irq, irqflags = 0; 225421bc9b15SBartosz Golaszewski char *label; 2255925ca369SKent Gibson 2256925ca369SKent Gibson if (copy_from_user(&eventreq, ip, sizeof(eventreq))) 2257925ca369SKent Gibson return -EFAULT; 2258925ca369SKent Gibson 2259925ca369SKent Gibson offset = eventreq.lineoffset; 2260925ca369SKent Gibson lflags = eventreq.handleflags; 2261925ca369SKent Gibson eflags = eventreq.eventflags; 2262925ca369SKent Gibson 2263925ca369SKent Gibson desc = gpiochip_get_desc(gdev->chip, offset); 2264925ca369SKent Gibson if (IS_ERR(desc)) 2265925ca369SKent Gibson return PTR_ERR(desc); 2266925ca369SKent Gibson 2267925ca369SKent Gibson /* Return an error if a unknown flag is set */ 2268925ca369SKent Gibson if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || 2269925ca369SKent Gibson (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) 2270925ca369SKent Gibson return -EINVAL; 2271925ca369SKent Gibson 2272925ca369SKent Gibson /* This is just wrong: we don't look for events on output lines */ 2273925ca369SKent Gibson if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) || 2274925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || 2275925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) 2276925ca369SKent Gibson return -EINVAL; 2277925ca369SKent Gibson 2278925ca369SKent Gibson /* Only one bias flag can be set. */ 2279925ca369SKent Gibson if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && 2280925ca369SKent Gibson (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | 2281925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || 2282925ca369SKent Gibson ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && 2283925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) 2284925ca369SKent Gibson return -EINVAL; 2285925ca369SKent Gibson 2286925ca369SKent Gibson le = kzalloc(sizeof(*le), GFP_KERNEL); 2287925ca369SKent Gibson if (!le) 2288925ca369SKent Gibson return -ENOMEM; 2289dc0989e3SAndy Shevchenko le->gdev = gpio_device_get(gdev); 2290925ca369SKent Gibson 2291f188ac12SKent Gibson if (eventreq.consumer_label[0] != '\0') { 2292f188ac12SKent Gibson /* label is only initialized if consumer_label is set */ 2293f188ac12SKent Gibson le->label = kstrndup(eventreq.consumer_label, 2294f188ac12SKent Gibson sizeof(eventreq.consumer_label) - 1, 2295925ca369SKent Gibson GFP_KERNEL); 2296925ca369SKent Gibson if (!le->label) { 2297925ca369SKent Gibson ret = -ENOMEM; 2298925ca369SKent Gibson goto out_free_le; 2299925ca369SKent Gibson } 2300925ca369SKent Gibson } 2301925ca369SKent Gibson 230295a4eed7SAndy Shevchenko ret = gpiod_request_user(desc, le->label); 2303925ca369SKent Gibson if (ret) 230446824272SKent Gibson goto out_free_le; 2305925ca369SKent Gibson le->desc = desc; 2306925ca369SKent Gibson le->eflags = eflags; 2307925ca369SKent Gibson 2308c274b58aSKent Gibson linehandle_flags_to_desc_flags(lflags, &desc->flags); 2309925ca369SKent Gibson 2310925ca369SKent Gibson ret = gpiod_direction_input(desc); 2311925ca369SKent Gibson if (ret) 231246824272SKent Gibson goto out_free_le; 2313925ca369SKent Gibson 23149ce4ed5bSBartosz Golaszewski gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED); 2315925ca369SKent Gibson 231646824272SKent Gibson irq = gpiod_to_irq(desc); 231746824272SKent Gibson if (irq <= 0) { 2318925ca369SKent Gibson ret = -ENODEV; 231946824272SKent Gibson goto out_free_le; 2320925ca369SKent Gibson } 2321925ca369SKent Gibson 2322925ca369SKent Gibson if (eflags & GPIOEVENT_REQUEST_RISING_EDGE) 2323925ca369SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 2324925ca369SKent Gibson IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 2325925ca369SKent Gibson if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE) 2326925ca369SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 2327925ca369SKent Gibson IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 2328925ca369SKent Gibson irqflags |= IRQF_ONESHOT; 2329925ca369SKent Gibson 2330925ca369SKent Gibson INIT_KFIFO(le->events); 2331925ca369SKent Gibson init_waitqueue_head(&le->wait); 2332925ca369SKent Gibson 233391043f55SBartosz Golaszewski le->device_unregistered_nb.notifier_call = lineevent_unregistered_notify; 233491043f55SBartosz Golaszewski ret = blocking_notifier_chain_register(&gdev->device_notifier, 233591043f55SBartosz Golaszewski &le->device_unregistered_nb); 233691043f55SBartosz Golaszewski if (ret) 233791043f55SBartosz Golaszewski goto out_free_le; 233891043f55SBartosz Golaszewski 233921bc9b15SBartosz Golaszewski label = make_irq_label(le->label); 2340d9f0804aSBartosz Golaszewski if (IS_ERR(label)) { 2341d9f0804aSBartosz Golaszewski ret = PTR_ERR(label); 234221bc9b15SBartosz Golaszewski goto out_free_le; 234321bc9b15SBartosz Golaszewski } 234421bc9b15SBartosz Golaszewski 2345925ca369SKent Gibson /* Request a thread to read the events */ 234669bef19dSMeng Li ret = request_threaded_irq(irq, 2347925ca369SKent Gibson lineevent_irq_handler, 2348925ca369SKent Gibson lineevent_irq_thread, 2349925ca369SKent Gibson irqflags, 235021bc9b15SBartosz Golaszewski label, 2351925ca369SKent Gibson le); 235221bc9b15SBartosz Golaszewski if (ret) { 235321bc9b15SBartosz Golaszewski free_irq_label(label); 235446824272SKent Gibson goto out_free_le; 235521bc9b15SBartosz Golaszewski } 2356925ca369SKent Gibson 235769bef19dSMeng Li le->irq = irq; 235869bef19dSMeng Li 2359925ca369SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 2360925ca369SKent Gibson if (fd < 0) { 2361925ca369SKent Gibson ret = fd; 236246824272SKent Gibson goto out_free_le; 2363925ca369SKent Gibson } 2364925ca369SKent Gibson 2365925ca369SKent Gibson file = anon_inode_getfile("gpio-event", 2366925ca369SKent Gibson &lineevent_fileops, 2367925ca369SKent Gibson le, 2368925ca369SKent Gibson O_RDONLY | O_CLOEXEC); 2369925ca369SKent Gibson if (IS_ERR(file)) { 2370925ca369SKent Gibson ret = PTR_ERR(file); 2371925ca369SKent Gibson goto out_put_unused_fd; 2372925ca369SKent Gibson } 2373925ca369SKent Gibson 2374925ca369SKent Gibson eventreq.fd = fd; 2375925ca369SKent Gibson if (copy_to_user(ip, &eventreq, sizeof(eventreq))) { 2376925ca369SKent Gibson /* 2377925ca369SKent Gibson * fput() will trigger the release() callback, so do not go onto 2378925ca369SKent Gibson * the regular error cleanup path here. 2379925ca369SKent Gibson */ 2380925ca369SKent Gibson fput(file); 2381925ca369SKent Gibson put_unused_fd(fd); 2382925ca369SKent Gibson return -EFAULT; 2383925ca369SKent Gibson } 2384925ca369SKent Gibson 2385925ca369SKent Gibson fd_install(fd, file); 2386925ca369SKent Gibson 2387925ca369SKent Gibson return 0; 2388925ca369SKent Gibson 2389925ca369SKent Gibson out_put_unused_fd: 2390925ca369SKent Gibson put_unused_fd(fd); 2391925ca369SKent Gibson out_free_le: 239246824272SKent Gibson lineevent_free(le); 2393925ca369SKent Gibson return ret; 2394925ca369SKent Gibson } 2395925ca369SKent Gibson 2396aad95584SKent Gibson static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2, 2397aad95584SKent Gibson struct gpioline_info *info_v1) 2398aad95584SKent Gibson { 2399aad95584SKent Gibson u64 flagsv2 = info_v2->flags; 2400aad95584SKent Gibson 2401aad95584SKent Gibson memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name)); 2402aad95584SKent Gibson memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer)); 2403aad95584SKent Gibson info_v1->line_offset = info_v2->offset; 2404aad95584SKent Gibson info_v1->flags = 0; 2405aad95584SKent Gibson 2406aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_USED) 2407aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_KERNEL; 2408aad95584SKent Gibson 2409aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT) 2410aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_IS_OUT; 2411aad95584SKent Gibson 2412aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 2413aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW; 2414aad95584SKent Gibson 2415aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN) 2416aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN; 2417aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE) 2418aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE; 2419aad95584SKent Gibson 2420aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP) 2421aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; 2422aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) 2423aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; 2424aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED) 2425aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE; 2426aad95584SKent Gibson } 2427aad95584SKent Gibson 2428aad95584SKent Gibson static void gpio_v2_line_info_changed_to_v1( 2429aad95584SKent Gibson struct gpio_v2_line_info_changed *lic_v2, 2430aad95584SKent Gibson struct gpioline_info_changed *lic_v1) 2431aad95584SKent Gibson { 2432cb8f63b8SGabriel Knezek memset(lic_v1, 0, sizeof(*lic_v1)); 2433aad95584SKent Gibson gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info); 2434aad95584SKent Gibson lic_v1->timestamp = lic_v2->timestamp_ns; 2435aad95584SKent Gibson lic_v1->event_type = lic_v2->event_type; 2436aad95584SKent Gibson } 2437aad95584SKent Gibson 24383c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 24393c0d9c63SKent Gibson 2440925ca369SKent Gibson static void gpio_desc_to_lineinfo(struct gpio_desc *desc, 2441aad95584SKent Gibson struct gpio_v2_line_info *info) 2442925ca369SKent Gibson { 2443925ca369SKent Gibson struct gpio_chip *gc = desc->gdev->chip; 2444925ca369SKent Gibson bool ok_for_pinctrl; 2445925ca369SKent Gibson unsigned long flags; 2446925ca369SKent Gibson 244769e4e136SKent Gibson memset(info, 0, sizeof(*info)); 2448aad95584SKent Gibson info->offset = gpio_chip_hwgpio(desc); 2449925ca369SKent Gibson 2450925ca369SKent Gibson /* 2451925ca369SKent Gibson * This function takes a mutex so we must check this before taking 2452925ca369SKent Gibson * the spinlock. 2453925ca369SKent Gibson * 2454925ca369SKent Gibson * FIXME: find a non-racy way to retrieve this information. Maybe a 2455925ca369SKent Gibson * lock common to both frameworks? 2456925ca369SKent Gibson */ 2457925ca369SKent Gibson ok_for_pinctrl = 2458aad95584SKent Gibson pinctrl_gpio_can_use_line(gc->base + info->offset); 2459925ca369SKent Gibson 2460925ca369SKent Gibson spin_lock_irqsave(&gpio_lock, flags); 2461925ca369SKent Gibson 246269e4e136SKent Gibson if (desc->name) 246369e4e136SKent Gibson strscpy(info->name, desc->name, sizeof(info->name)); 2464925ca369SKent Gibson 246569e4e136SKent Gibson if (desc->label) 246669e4e136SKent Gibson strscpy(info->consumer, desc->label, sizeof(info->consumer)); 2467925ca369SKent Gibson 2468925ca369SKent Gibson /* 2469925ca369SKent Gibson * Userspace only need to know that the kernel is using this GPIO so 2470925ca369SKent Gibson * it can't use it. 2471925ca369SKent Gibson */ 2472925ca369SKent Gibson info->flags = 0; 2473925ca369SKent Gibson if (test_bit(FLAG_REQUESTED, &desc->flags) || 2474925ca369SKent Gibson test_bit(FLAG_IS_HOGGED, &desc->flags) || 2475925ca369SKent Gibson test_bit(FLAG_USED_AS_IRQ, &desc->flags) || 2476925ca369SKent Gibson test_bit(FLAG_EXPORT, &desc->flags) || 2477925ca369SKent Gibson test_bit(FLAG_SYSFS, &desc->flags) || 2478a0db197fSMarc Zyngier !gpiochip_line_is_valid(gc, info->offset) || 2479925ca369SKent Gibson !ok_for_pinctrl) 2480aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_USED; 2481aad95584SKent Gibson 2482925ca369SKent Gibson if (test_bit(FLAG_IS_OUT, &desc->flags)) 2483aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OUTPUT; 2484aad95584SKent Gibson else 2485aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_INPUT; 2486aad95584SKent Gibson 2487925ca369SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 2488aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW; 2489aad95584SKent Gibson 2490925ca369SKent Gibson if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 2491aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; 2492925ca369SKent Gibson if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 2493aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; 2494aad95584SKent Gibson 2495925ca369SKent Gibson if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) 2496aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED; 2497925ca369SKent Gibson if (test_bit(FLAG_PULL_DOWN, &desc->flags)) 2498aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN; 2499925ca369SKent Gibson if (test_bit(FLAG_PULL_UP, &desc->flags)) 2500aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; 2501925ca369SKent Gibson 250273e03419SKent Gibson if (test_bit(FLAG_EDGE_RISING, &desc->flags)) 250373e03419SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; 250473e03419SKent Gibson if (test_bit(FLAG_EDGE_FALLING, &desc->flags)) 250573e03419SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; 250673e03419SKent Gibson 250726d060e4SKent Gibson if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags)) 250826d060e4SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME; 25092068339aSDipen Patel else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags)) 25102068339aSDipen Patel info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE; 251126d060e4SKent Gibson 2512925ca369SKent Gibson spin_unlock_irqrestore(&gpio_lock, flags); 2513925ca369SKent Gibson } 2514925ca369SKent Gibson 2515925ca369SKent Gibson struct gpio_chardev_data { 2516925ca369SKent Gibson struct gpio_device *gdev; 2517925ca369SKent Gibson wait_queue_head_t wait; 2518aad95584SKent Gibson DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32); 2519925ca369SKent Gibson struct notifier_block lineinfo_changed_nb; 2520d2e2586aSBartosz Golaszewski struct notifier_block device_unregistered_nb; 2521925ca369SKent Gibson unsigned long *watched_lines; 2522aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2523aad95584SKent Gibson atomic_t watch_abi_version; 2524aad95584SKent Gibson #endif 2525925ca369SKent Gibson }; 2526925ca369SKent Gibson 25272e202ad8SKent Gibson static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip) 25282e202ad8SKent Gibson { 25292e202ad8SKent Gibson struct gpio_device *gdev = cdev->gdev; 25302e202ad8SKent Gibson struct gpiochip_info chipinfo; 25312e202ad8SKent Gibson 25322e202ad8SKent Gibson memset(&chipinfo, 0, sizeof(chipinfo)); 25332e202ad8SKent Gibson 25342e202ad8SKent Gibson strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name)); 25352e202ad8SKent Gibson strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label)); 25362e202ad8SKent Gibson chipinfo.lines = gdev->ngpio; 25372e202ad8SKent Gibson if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) 25382e202ad8SKent Gibson return -EFAULT; 25392e202ad8SKent Gibson return 0; 25402e202ad8SKent Gibson } 25412e202ad8SKent Gibson 2542aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2543aad95584SKent Gibson /* 2544aad95584SKent Gibson * returns 0 if the versions match, else the previously selected ABI version 2545aad95584SKent Gibson */ 2546aad95584SKent Gibson static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata, 2547aad95584SKent Gibson unsigned int version) 2548aad95584SKent Gibson { 2549aad95584SKent Gibson int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version); 2550aad95584SKent Gibson 2551aad95584SKent Gibson if (abiv == version) 2552aad95584SKent Gibson return 0; 2553aad95584SKent Gibson 2554aad95584SKent Gibson return abiv; 2555aad95584SKent Gibson } 25562e202ad8SKent Gibson 25572e202ad8SKent Gibson static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip, 25582e202ad8SKent Gibson bool watch) 25592e202ad8SKent Gibson { 25602e202ad8SKent Gibson struct gpio_desc *desc; 25612e202ad8SKent Gibson struct gpioline_info lineinfo; 25622e202ad8SKent Gibson struct gpio_v2_line_info lineinfo_v2; 25632e202ad8SKent Gibson 25642e202ad8SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 25652e202ad8SKent Gibson return -EFAULT; 25662e202ad8SKent Gibson 25672e202ad8SKent Gibson /* this doubles as a range check on line_offset */ 25682e202ad8SKent Gibson desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset); 25692e202ad8SKent Gibson if (IS_ERR(desc)) 25702e202ad8SKent Gibson return PTR_ERR(desc); 25712e202ad8SKent Gibson 25722e202ad8SKent Gibson if (watch) { 25732e202ad8SKent Gibson if (lineinfo_ensure_abi_version(cdev, 1)) 25742e202ad8SKent Gibson return -EPERM; 25752e202ad8SKent Gibson 25762e202ad8SKent Gibson if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines)) 25772e202ad8SKent Gibson return -EBUSY; 25782e202ad8SKent Gibson } 25792e202ad8SKent Gibson 25802e202ad8SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo_v2); 25812e202ad8SKent Gibson gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); 25822e202ad8SKent Gibson 25832e202ad8SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 25842e202ad8SKent Gibson if (watch) 25852e202ad8SKent Gibson clear_bit(lineinfo.line_offset, cdev->watched_lines); 25862e202ad8SKent Gibson return -EFAULT; 25872e202ad8SKent Gibson } 25882e202ad8SKent Gibson 25892e202ad8SKent Gibson return 0; 25902e202ad8SKent Gibson } 2591aad95584SKent Gibson #endif 2592aad95584SKent Gibson 2593aad95584SKent Gibson static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip, 2594aad95584SKent Gibson bool watch) 2595aad95584SKent Gibson { 2596aad95584SKent Gibson struct gpio_desc *desc; 2597aad95584SKent Gibson struct gpio_v2_line_info lineinfo; 2598aad95584SKent Gibson 2599aad95584SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 2600aad95584SKent Gibson return -EFAULT; 2601aad95584SKent Gibson 2602aad95584SKent Gibson if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding))) 2603aad95584SKent Gibson return -EINVAL; 2604aad95584SKent Gibson 2605aad95584SKent Gibson desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset); 2606aad95584SKent Gibson if (IS_ERR(desc)) 2607aad95584SKent Gibson return PTR_ERR(desc); 2608aad95584SKent Gibson 2609aad95584SKent Gibson if (watch) { 2610aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2611aad95584SKent Gibson if (lineinfo_ensure_abi_version(cdev, 2)) 2612aad95584SKent Gibson return -EPERM; 2613aad95584SKent Gibson #endif 2614aad95584SKent Gibson if (test_and_set_bit(lineinfo.offset, cdev->watched_lines)) 2615aad95584SKent Gibson return -EBUSY; 2616aad95584SKent Gibson } 2617aad95584SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo); 2618*309364bcSKent Gibson supinfo_to_lineinfo(desc, &lineinfo); 2619aad95584SKent Gibson 2620aad95584SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 2621aad95584SKent Gibson if (watch) 2622aad95584SKent Gibson clear_bit(lineinfo.offset, cdev->watched_lines); 2623aad95584SKent Gibson return -EFAULT; 2624aad95584SKent Gibson } 2625aad95584SKent Gibson 2626aad95584SKent Gibson return 0; 2627aad95584SKent Gibson } 2628aad95584SKent Gibson 26292e202ad8SKent Gibson static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip) 2630925ca369SKent Gibson { 2631925ca369SKent Gibson __u32 offset; 2632925ca369SKent Gibson 2633925ca369SKent Gibson if (copy_from_user(&offset, ip, sizeof(offset))) 2634925ca369SKent Gibson return -EFAULT; 2635925ca369SKent Gibson 26361bf7ba40SKent Gibson if (offset >= cdev->gdev->ngpio) 26371bf7ba40SKent Gibson return -EINVAL; 2638925ca369SKent Gibson 26391bf7ba40SKent Gibson if (!test_and_clear_bit(offset, cdev->watched_lines)) 2640925ca369SKent Gibson return -EBUSY; 2641925ca369SKent Gibson 2642925ca369SKent Gibson return 0; 2643925ca369SKent Gibson } 26442e202ad8SKent Gibson 2645249cbac4SKent Gibson static long gpio_ioctl_unlocked(struct file *file, unsigned int cmd, unsigned long arg) 26462e202ad8SKent Gibson { 26472e202ad8SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 26482e202ad8SKent Gibson struct gpio_device *gdev = cdev->gdev; 26492e202ad8SKent Gibson void __user *ip = (void __user *)arg; 26502e202ad8SKent Gibson 26512e202ad8SKent Gibson /* We fail any subsequent ioctl():s when the chip is gone */ 26522e202ad8SKent Gibson if (!gdev->chip) 26532e202ad8SKent Gibson return -ENODEV; 26542e202ad8SKent Gibson 26552e202ad8SKent Gibson /* Fill in the struct and pass to userspace */ 26561cef8b50SAndy Shevchenko switch (cmd) { 26571cef8b50SAndy Shevchenko case GPIO_GET_CHIPINFO_IOCTL: 26582e202ad8SKent Gibson return chipinfo_get(cdev, ip); 26592e202ad8SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 26601cef8b50SAndy Shevchenko case GPIO_GET_LINEHANDLE_IOCTL: 26612e202ad8SKent Gibson return linehandle_create(gdev, ip); 26621cef8b50SAndy Shevchenko case GPIO_GET_LINEEVENT_IOCTL: 26632e202ad8SKent Gibson return lineevent_create(gdev, ip); 26641cef8b50SAndy Shevchenko case GPIO_GET_LINEINFO_IOCTL: 26651cef8b50SAndy Shevchenko return lineinfo_get_v1(cdev, ip, false); 26661cef8b50SAndy Shevchenko case GPIO_GET_LINEINFO_WATCH_IOCTL: 26671cef8b50SAndy Shevchenko return lineinfo_get_v1(cdev, ip, true); 26682e202ad8SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 26691cef8b50SAndy Shevchenko case GPIO_V2_GET_LINEINFO_IOCTL: 26701cef8b50SAndy Shevchenko return lineinfo_get(cdev, ip, false); 26711cef8b50SAndy Shevchenko case GPIO_V2_GET_LINEINFO_WATCH_IOCTL: 26721cef8b50SAndy Shevchenko return lineinfo_get(cdev, ip, true); 26731cef8b50SAndy Shevchenko case GPIO_V2_GET_LINE_IOCTL: 26742e202ad8SKent Gibson return linereq_create(gdev, ip); 26751cef8b50SAndy Shevchenko case GPIO_GET_LINEINFO_UNWATCH_IOCTL: 26762e202ad8SKent Gibson return lineinfo_unwatch(cdev, ip); 26771cef8b50SAndy Shevchenko default: 2678925ca369SKent Gibson return -EINVAL; 2679925ca369SKent Gibson } 26801cef8b50SAndy Shevchenko } 2681925ca369SKent Gibson 2682249cbac4SKent Gibson /* 2683249cbac4SKent Gibson * gpio_ioctl() - ioctl handler for the GPIO chardev 2684249cbac4SKent Gibson */ 2685249cbac4SKent Gibson static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 2686249cbac4SKent Gibson { 2687249cbac4SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2688249cbac4SKent Gibson 2689249cbac4SKent Gibson return call_ioctl_locked(file, cmd, arg, cdev->gdev, 2690249cbac4SKent Gibson gpio_ioctl_unlocked); 2691249cbac4SKent Gibson } 2692249cbac4SKent Gibson 2693925ca369SKent Gibson #ifdef CONFIG_COMPAT 269449bc5279SKent Gibson static long gpio_ioctl_compat(struct file *file, unsigned int cmd, 2695925ca369SKent Gibson unsigned long arg) 2696925ca369SKent Gibson { 269749bc5279SKent Gibson return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 2698925ca369SKent Gibson } 2699925ca369SKent Gibson #endif 2700925ca369SKent Gibson 2701925ca369SKent Gibson static int lineinfo_changed_notify(struct notifier_block *nb, 2702925ca369SKent Gibson unsigned long action, void *data) 2703925ca369SKent Gibson { 2704e82bbd67SBartosz Golaszewski struct gpio_chardev_data *cdev = 2705e82bbd67SBartosz Golaszewski container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb); 2706aad95584SKent Gibson struct gpio_v2_line_info_changed chg; 2707925ca369SKent Gibson struct gpio_desc *desc = data; 2708925ca369SKent Gibson int ret; 2709925ca369SKent Gibson 2710e2b781c5SKent Gibson if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) 2711925ca369SKent Gibson return NOTIFY_DONE; 2712925ca369SKent Gibson 2713925ca369SKent Gibson memset(&chg, 0, sizeof(chg)); 2714925ca369SKent Gibson chg.event_type = action; 2715aad95584SKent Gibson chg.timestamp_ns = ktime_get_ns(); 2716925ca369SKent Gibson gpio_desc_to_lineinfo(desc, &chg.info); 2717*309364bcSKent Gibson supinfo_to_lineinfo(desc, &chg.info); 2718925ca369SKent Gibson 2719e2b781c5SKent Gibson ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); 2720925ca369SKent Gibson if (ret) 2721e2b781c5SKent Gibson wake_up_poll(&cdev->wait, EPOLLIN); 2722925ca369SKent Gibson else 2723925ca369SKent Gibson pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n"); 2724925ca369SKent Gibson 2725925ca369SKent Gibson return NOTIFY_OK; 2726925ca369SKent Gibson } 2727925ca369SKent Gibson 2728d2e2586aSBartosz Golaszewski static int gpio_device_unregistered_notify(struct notifier_block *nb, 2729d2e2586aSBartosz Golaszewski unsigned long action, void *data) 2730d2e2586aSBartosz Golaszewski { 2731d2e2586aSBartosz Golaszewski struct gpio_chardev_data *cdev = container_of(nb, 2732d2e2586aSBartosz Golaszewski struct gpio_chardev_data, 2733d2e2586aSBartosz Golaszewski device_unregistered_nb); 2734d2e2586aSBartosz Golaszewski 2735d2e2586aSBartosz Golaszewski wake_up_poll(&cdev->wait, EPOLLIN | EPOLLERR); 2736d2e2586aSBartosz Golaszewski 2737d2e2586aSBartosz Golaszewski return NOTIFY_OK; 2738d2e2586aSBartosz Golaszewski } 2739d2e2586aSBartosz Golaszewski 2740bdbbae24SBartosz Golaszewski static __poll_t lineinfo_watch_poll_unlocked(struct file *file, 2741925ca369SKent Gibson struct poll_table_struct *pollt) 2742925ca369SKent Gibson { 2743e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2744925ca369SKent Gibson __poll_t events = 0; 2745925ca369SKent Gibson 2746533aae7cSBartosz Golaszewski if (!cdev->gdev->chip) 2747533aae7cSBartosz Golaszewski return EPOLLHUP | EPOLLERR; 2748533aae7cSBartosz Golaszewski 2749e2b781c5SKent Gibson poll_wait(file, &cdev->wait, pollt); 2750925ca369SKent Gibson 2751e2b781c5SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events, 2752e2b781c5SKent Gibson &cdev->wait.lock)) 2753925ca369SKent Gibson events = EPOLLIN | EPOLLRDNORM; 2754925ca369SKent Gibson 2755925ca369SKent Gibson return events; 2756925ca369SKent Gibson } 2757925ca369SKent Gibson 2758bdbbae24SBartosz Golaszewski static __poll_t lineinfo_watch_poll(struct file *file, 2759bdbbae24SBartosz Golaszewski struct poll_table_struct *pollt) 2760bdbbae24SBartosz Golaszewski { 2761bdbbae24SBartosz Golaszewski struct gpio_chardev_data *cdev = file->private_data; 2762bdbbae24SBartosz Golaszewski 2763bdbbae24SBartosz Golaszewski return call_poll_locked(file, pollt, cdev->gdev, 2764bdbbae24SBartosz Golaszewski lineinfo_watch_poll_unlocked); 2765bdbbae24SBartosz Golaszewski } 2766bdbbae24SBartosz Golaszewski 2767bdbbae24SBartosz Golaszewski static ssize_t lineinfo_watch_read_unlocked(struct file *file, char __user *buf, 2768925ca369SKent Gibson size_t count, loff_t *off) 2769925ca369SKent Gibson { 2770e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2771aad95584SKent Gibson struct gpio_v2_line_info_changed event; 2772925ca369SKent Gibson ssize_t bytes_read = 0; 2773925ca369SKent Gibson int ret; 2774aad95584SKent Gibson size_t event_size; 2775925ca369SKent Gibson 2776533aae7cSBartosz Golaszewski if (!cdev->gdev->chip) 2777533aae7cSBartosz Golaszewski return -ENODEV; 2778533aae7cSBartosz Golaszewski 2779aad95584SKent Gibson #ifndef CONFIG_GPIO_CDEV_V1 2780aad95584SKent Gibson event_size = sizeof(struct gpio_v2_line_info_changed); 2781aad95584SKent Gibson if (count < event_size) 2782925ca369SKent Gibson return -EINVAL; 2783aad95584SKent Gibson #endif 2784925ca369SKent Gibson 2785925ca369SKent Gibson do { 2786e2b781c5SKent Gibson spin_lock(&cdev->wait.lock); 2787e2b781c5SKent Gibson if (kfifo_is_empty(&cdev->events)) { 2788925ca369SKent Gibson if (bytes_read) { 2789e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2790925ca369SKent Gibson return bytes_read; 2791925ca369SKent Gibson } 2792925ca369SKent Gibson 279349bc5279SKent Gibson if (file->f_flags & O_NONBLOCK) { 2794e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2795925ca369SKent Gibson return -EAGAIN; 2796925ca369SKent Gibson } 2797925ca369SKent Gibson 2798e2b781c5SKent Gibson ret = wait_event_interruptible_locked(cdev->wait, 2799e2b781c5SKent Gibson !kfifo_is_empty(&cdev->events)); 2800925ca369SKent Gibson if (ret) { 2801e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2802925ca369SKent Gibson return ret; 2803925ca369SKent Gibson } 2804925ca369SKent Gibson } 2805aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2806aad95584SKent Gibson /* must be after kfifo check so watch_abi_version is set */ 2807aad95584SKent Gibson if (atomic_read(&cdev->watch_abi_version) == 2) 2808aad95584SKent Gibson event_size = sizeof(struct gpio_v2_line_info_changed); 2809aad95584SKent Gibson else 2810aad95584SKent Gibson event_size = sizeof(struct gpioline_info_changed); 2811aad95584SKent Gibson if (count < event_size) { 2812aad95584SKent Gibson spin_unlock(&cdev->wait.lock); 2813aad95584SKent Gibson return -EINVAL; 2814aad95584SKent Gibson } 2815aad95584SKent Gibson #endif 2816e2b781c5SKent Gibson ret = kfifo_out(&cdev->events, &event, 1); 2817e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2818925ca369SKent Gibson if (ret != 1) { 2819925ca369SKent Gibson ret = -EIO; 2820925ca369SKent Gibson break; 2821925ca369SKent Gibson /* We should never get here. See lineevent_read(). */ 2822925ca369SKent Gibson } 2823925ca369SKent Gibson 2824aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2825aad95584SKent Gibson if (event_size == sizeof(struct gpio_v2_line_info_changed)) { 2826aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event, event_size)) 2827925ca369SKent Gibson return -EFAULT; 2828aad95584SKent Gibson } else { 2829aad95584SKent Gibson struct gpioline_info_changed event_v1; 2830aad95584SKent Gibson 2831aad95584SKent Gibson gpio_v2_line_info_changed_to_v1(&event, &event_v1); 2832aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event_v1, 2833aad95584SKent Gibson event_size)) 2834aad95584SKent Gibson return -EFAULT; 2835aad95584SKent Gibson } 2836aad95584SKent Gibson #else 2837aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event, event_size)) 2838aad95584SKent Gibson return -EFAULT; 2839aad95584SKent Gibson #endif 2840aad95584SKent Gibson bytes_read += event_size; 2841925ca369SKent Gibson } while (count >= bytes_read + sizeof(event)); 2842925ca369SKent Gibson 2843925ca369SKent Gibson return bytes_read; 2844925ca369SKent Gibson } 2845925ca369SKent Gibson 2846bdbbae24SBartosz Golaszewski static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, 2847bdbbae24SBartosz Golaszewski size_t count, loff_t *off) 2848bdbbae24SBartosz Golaszewski { 2849bdbbae24SBartosz Golaszewski struct gpio_chardev_data *cdev = file->private_data; 2850bdbbae24SBartosz Golaszewski 2851bdbbae24SBartosz Golaszewski return call_read_locked(file, buf, count, off, cdev->gdev, 2852bdbbae24SBartosz Golaszewski lineinfo_watch_read_unlocked); 2853bdbbae24SBartosz Golaszewski } 2854bdbbae24SBartosz Golaszewski 2855925ca369SKent Gibson /** 2856925ca369SKent Gibson * gpio_chrdev_open() - open the chardev for ioctl operations 2857925ca369SKent Gibson * @inode: inode for this chardev 285849bc5279SKent Gibson * @file: file struct for storing private data 2859925ca369SKent Gibson * Returns 0 on success 2860925ca369SKent Gibson */ 286149bc5279SKent Gibson static int gpio_chrdev_open(struct inode *inode, struct file *file) 2862925ca369SKent Gibson { 2863925ca369SKent Gibson struct gpio_device *gdev = container_of(inode->i_cdev, 2864925ca369SKent Gibson struct gpio_device, chrdev); 2865e2b781c5SKent Gibson struct gpio_chardev_data *cdev; 2866925ca369SKent Gibson int ret = -ENOMEM; 2867925ca369SKent Gibson 2868bdbbae24SBartosz Golaszewski down_read(&gdev->sem); 2869bdbbae24SBartosz Golaszewski 2870925ca369SKent Gibson /* Fail on open if the backing gpiochip is gone */ 2871bdbbae24SBartosz Golaszewski if (!gdev->chip) { 2872bdbbae24SBartosz Golaszewski ret = -ENODEV; 2873bdbbae24SBartosz Golaszewski goto out_unlock; 2874bdbbae24SBartosz Golaszewski } 2875925ca369SKent Gibson 2876e2b781c5SKent Gibson cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); 2877e2b781c5SKent Gibson if (!cdev) 2878bdbbae24SBartosz Golaszewski goto out_unlock; 2879925ca369SKent Gibson 2880e2b781c5SKent Gibson cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL); 2881e2b781c5SKent Gibson if (!cdev->watched_lines) 2882e2b781c5SKent Gibson goto out_free_cdev; 2883925ca369SKent Gibson 2884e2b781c5SKent Gibson init_waitqueue_head(&cdev->wait); 2885e2b781c5SKent Gibson INIT_KFIFO(cdev->events); 2886dc0989e3SAndy Shevchenko cdev->gdev = gpio_device_get(gdev); 2887925ca369SKent Gibson 2888e2b781c5SKent Gibson cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify; 288917a7ca35SBartosz Golaszewski ret = blocking_notifier_chain_register(&gdev->line_state_notifier, 2890e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2891925ca369SKent Gibson if (ret) 2892925ca369SKent Gibson goto out_free_bitmap; 2893925ca369SKent Gibson 2894d2e2586aSBartosz Golaszewski cdev->device_unregistered_nb.notifier_call = 2895d2e2586aSBartosz Golaszewski gpio_device_unregistered_notify; 2896d2e2586aSBartosz Golaszewski ret = blocking_notifier_chain_register(&gdev->device_notifier, 2897d2e2586aSBartosz Golaszewski &cdev->device_unregistered_nb); 2898d2e2586aSBartosz Golaszewski if (ret) 2899d2e2586aSBartosz Golaszewski goto out_unregister_line_notifier; 2900d2e2586aSBartosz Golaszewski 2901e2b781c5SKent Gibson file->private_data = cdev; 2902925ca369SKent Gibson 290349bc5279SKent Gibson ret = nonseekable_open(inode, file); 2904925ca369SKent Gibson if (ret) 2905d2e2586aSBartosz Golaszewski goto out_unregister_device_notifier; 2906925ca369SKent Gibson 2907bdbbae24SBartosz Golaszewski up_read(&gdev->sem); 2908bdbbae24SBartosz Golaszewski 2909925ca369SKent Gibson return ret; 2910925ca369SKent Gibson 2911d2e2586aSBartosz Golaszewski out_unregister_device_notifier: 2912d2e2586aSBartosz Golaszewski blocking_notifier_chain_unregister(&gdev->device_notifier, 2913d2e2586aSBartosz Golaszewski &cdev->device_unregistered_nb); 2914d2e2586aSBartosz Golaszewski out_unregister_line_notifier: 291517a7ca35SBartosz Golaszewski blocking_notifier_chain_unregister(&gdev->line_state_notifier, 2916e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2917925ca369SKent Gibson out_free_bitmap: 2918dc0989e3SAndy Shevchenko gpio_device_put(gdev); 2919e2b781c5SKent Gibson bitmap_free(cdev->watched_lines); 2920e2b781c5SKent Gibson out_free_cdev: 2921e2b781c5SKent Gibson kfree(cdev); 2922bdbbae24SBartosz Golaszewski out_unlock: 2923bdbbae24SBartosz Golaszewski up_read(&gdev->sem); 2924925ca369SKent Gibson return ret; 2925925ca369SKent Gibson } 2926925ca369SKent Gibson 2927925ca369SKent Gibson /** 2928925ca369SKent Gibson * gpio_chrdev_release() - close chardev after ioctl operations 2929925ca369SKent Gibson * @inode: inode for this chardev 293049bc5279SKent Gibson * @file: file struct for storing private data 2931925ca369SKent Gibson * Returns 0 on success 2932925ca369SKent Gibson */ 293349bc5279SKent Gibson static int gpio_chrdev_release(struct inode *inode, struct file *file) 2934925ca369SKent Gibson { 2935e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2936e2b781c5SKent Gibson struct gpio_device *gdev = cdev->gdev; 2937925ca369SKent Gibson 2938d2e2586aSBartosz Golaszewski blocking_notifier_chain_unregister(&gdev->device_notifier, 2939d2e2586aSBartosz Golaszewski &cdev->device_unregistered_nb); 294017a7ca35SBartosz Golaszewski blocking_notifier_chain_unregister(&gdev->line_state_notifier, 2941e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 294295ca7c90SZhongqiu Han bitmap_free(cdev->watched_lines); 2943dc0989e3SAndy Shevchenko gpio_device_put(gdev); 2944e2b781c5SKent Gibson kfree(cdev); 2945925ca369SKent Gibson 2946925ca369SKent Gibson return 0; 2947925ca369SKent Gibson } 2948925ca369SKent Gibson 2949925ca369SKent Gibson static const struct file_operations gpio_fileops = { 2950925ca369SKent Gibson .release = gpio_chrdev_release, 2951925ca369SKent Gibson .open = gpio_chrdev_open, 2952925ca369SKent Gibson .poll = lineinfo_watch_poll, 2953925ca369SKent Gibson .read = lineinfo_watch_read, 2954925ca369SKent Gibson .owner = THIS_MODULE, 2955925ca369SKent Gibson .llseek = no_llseek, 2956925ca369SKent Gibson .unlocked_ioctl = gpio_ioctl, 2957925ca369SKent Gibson #ifdef CONFIG_COMPAT 2958925ca369SKent Gibson .compat_ioctl = gpio_ioctl_compat, 2959925ca369SKent Gibson #endif 2960925ca369SKent Gibson }; 2961925ca369SKent Gibson 2962925ca369SKent Gibson int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) 2963925ca369SKent Gibson { 2964925ca369SKent Gibson int ret; 2965925ca369SKent Gibson 2966925ca369SKent Gibson cdev_init(&gdev->chrdev, &gpio_fileops); 2967925ca369SKent Gibson gdev->chrdev.owner = THIS_MODULE; 2968925ca369SKent Gibson gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id); 2969925ca369SKent Gibson 2970925ca369SKent Gibson ret = cdev_device_add(&gdev->chrdev, &gdev->dev); 2971925ca369SKent Gibson if (ret) 2972925ca369SKent Gibson return ret; 2973925ca369SKent Gibson 2974925ca369SKent Gibson chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n", 2975925ca369SKent Gibson MAJOR(devt), gdev->id); 2976925ca369SKent Gibson 2977925ca369SKent Gibson return 0; 2978925ca369SKent Gibson } 2979925ca369SKent Gibson 2980925ca369SKent Gibson void gpiolib_cdev_unregister(struct gpio_device *gdev) 2981925ca369SKent Gibson { 2982925ca369SKent Gibson cdev_device_del(&gdev->chrdev, &gdev->dev); 2983a067419bSBartosz Golaszewski blocking_notifier_call_chain(&gdev->device_notifier, 0, NULL); 2984925ca369SKent Gibson } 2985