1925ca369SKent Gibson // SPDX-License-Identifier: GPL-2.0 2925ca369SKent Gibson 3d189f627SKent Gibson #include <linux/anon_inodes.h> 43c0d9c63SKent Gibson #include <linux/atomic.h> 5925ca369SKent Gibson #include <linux/bitmap.h> 63c0d9c63SKent Gibson #include <linux/build_bug.h> 7d189f627SKent Gibson #include <linux/cdev.h> 8d189f627SKent Gibson #include <linux/compat.h> 965cff704SKent Gibson #include <linux/compiler.h> 10925ca369SKent Gibson #include <linux/device.h> 11925ca369SKent Gibson #include <linux/err.h> 12d189f627SKent Gibson #include <linux/file.h> 13925ca369SKent Gibson #include <linux/gpio.h> 14925ca369SKent Gibson #include <linux/gpio/driver.h> 1552ee7c02SAndy Shevchenko #include <linux/hte.h> 16d189f627SKent Gibson #include <linux/interrupt.h> 17d189f627SKent Gibson #include <linux/irqreturn.h> 18d189f627SKent Gibson #include <linux/kernel.h> 19925ca369SKent Gibson #include <linux/kfifo.h> 20d189f627SKent Gibson #include <linux/module.h> 21a54756cbSKent Gibson #include <linux/mutex.h> 22d189f627SKent Gibson #include <linux/pinctrl/consumer.h> 23925ca369SKent Gibson #include <linux/poll.h> 2452ee7c02SAndy Shevchenko #include <linux/seq_file.h> 25d189f627SKent Gibson #include <linux/spinlock.h> 26925ca369SKent Gibson #include <linux/timekeeping.h> 27d189f627SKent Gibson #include <linux/uaccess.h> 2865cff704SKent Gibson #include <linux/workqueue.h> 2952ee7c02SAndy Shevchenko 30925ca369SKent Gibson #include <uapi/linux/gpio.h> 31925ca369SKent Gibson 32925ca369SKent Gibson #include "gpiolib.h" 33925ca369SKent Gibson #include "gpiolib-cdev.h" 34925ca369SKent Gibson 353c0d9c63SKent Gibson /* 363c0d9c63SKent Gibson * Array sizes must ensure 64-bit alignment and not create holes in the 373c0d9c63SKent Gibson * struct packing. 383c0d9c63SKent Gibson */ 393c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2)); 403c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8)); 413c0d9c63SKent Gibson 423c0d9c63SKent Gibson /* 433c0d9c63SKent Gibson * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility 443c0d9c63SKent Gibson */ 453c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8)); 463c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8)); 473c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8)); 483c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8)); 493c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8)); 503c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8)); 513c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8)); 523c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8)); 533c0d9c63SKent Gibson 54925ca369SKent Gibson /* Character device interface to GPIO. 55925ca369SKent Gibson * 56925ca369SKent Gibson * The GPIO character device, /dev/gpiochipN, provides userspace an 57925ca369SKent Gibson * interface to gpiolib GPIOs via ioctl()s. 58925ca369SKent Gibson */ 59925ca369SKent Gibson 60bdbbae24SBartosz Golaszewski typedef __poll_t (*poll_fn)(struct file *, struct poll_table_struct *); 61bdbbae24SBartosz Golaszewski typedef long (*ioctl_fn)(struct file *, unsigned int, unsigned long); 62bdbbae24SBartosz Golaszewski typedef ssize_t (*read_fn)(struct file *, char __user *, 63bdbbae24SBartosz Golaszewski size_t count, loff_t *); 64bdbbae24SBartosz Golaszewski 65bdbbae24SBartosz Golaszewski static __poll_t call_poll_locked(struct file *file, 66bdbbae24SBartosz Golaszewski struct poll_table_struct *wait, 67bdbbae24SBartosz Golaszewski struct gpio_device *gdev, poll_fn func) 68bdbbae24SBartosz Golaszewski { 69bdbbae24SBartosz Golaszewski __poll_t ret; 70bdbbae24SBartosz Golaszewski 71bdbbae24SBartosz Golaszewski down_read(&gdev->sem); 72bdbbae24SBartosz Golaszewski ret = func(file, wait); 73bdbbae24SBartosz Golaszewski up_read(&gdev->sem); 74bdbbae24SBartosz Golaszewski 75bdbbae24SBartosz Golaszewski return ret; 76bdbbae24SBartosz Golaszewski } 77bdbbae24SBartosz Golaszewski 78bdbbae24SBartosz Golaszewski static long call_ioctl_locked(struct file *file, unsigned int cmd, 79bdbbae24SBartosz Golaszewski unsigned long arg, struct gpio_device *gdev, 80bdbbae24SBartosz Golaszewski ioctl_fn func) 81bdbbae24SBartosz Golaszewski { 82bdbbae24SBartosz Golaszewski long ret; 83bdbbae24SBartosz Golaszewski 84bdbbae24SBartosz Golaszewski down_read(&gdev->sem); 85bdbbae24SBartosz Golaszewski ret = func(file, cmd, arg); 86bdbbae24SBartosz Golaszewski up_read(&gdev->sem); 87bdbbae24SBartosz Golaszewski 88bdbbae24SBartosz Golaszewski return ret; 89bdbbae24SBartosz Golaszewski } 90bdbbae24SBartosz Golaszewski 91bdbbae24SBartosz Golaszewski static ssize_t call_read_locked(struct file *file, char __user *buf, 92bdbbae24SBartosz Golaszewski size_t count, loff_t *f_ps, 93bdbbae24SBartosz Golaszewski struct gpio_device *gdev, read_fn func) 94bdbbae24SBartosz Golaszewski { 95bdbbae24SBartosz Golaszewski ssize_t ret; 96bdbbae24SBartosz Golaszewski 97bdbbae24SBartosz Golaszewski down_read(&gdev->sem); 98bdbbae24SBartosz Golaszewski ret = func(file, buf, count, f_ps); 99bdbbae24SBartosz Golaszewski up_read(&gdev->sem); 100bdbbae24SBartosz Golaszewski 101bdbbae24SBartosz Golaszewski return ret; 102bdbbae24SBartosz Golaszewski } 103bdbbae24SBartosz Golaszewski 104925ca369SKent Gibson /* 105925ca369SKent Gibson * GPIO line handle management 106925ca369SKent Gibson */ 107925ca369SKent Gibson 1083c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 109925ca369SKent Gibson /** 110925ca369SKent Gibson * struct linehandle_state - contains the state of a userspace handle 111925ca369SKent Gibson * @gdev: the GPIO device the handle pertains to 112925ca369SKent Gibson * @label: consumer label used to tag descriptors 113925ca369SKent Gibson * @descs: the GPIO descriptors held by this handle 11452b7b596SKent Gibson * @num_descs: the number of descriptors held in the descs array 115925ca369SKent Gibson */ 116925ca369SKent Gibson struct linehandle_state { 117925ca369SKent Gibson struct gpio_device *gdev; 118925ca369SKent Gibson const char *label; 119925ca369SKent Gibson struct gpio_desc *descs[GPIOHANDLES_MAX]; 12052b7b596SKent Gibson u32 num_descs; 121925ca369SKent Gibson }; 122925ca369SKent Gibson 123925ca369SKent Gibson #define GPIOHANDLE_REQUEST_VALID_FLAGS \ 124925ca369SKent Gibson (GPIOHANDLE_REQUEST_INPUT | \ 125925ca369SKent Gibson GPIOHANDLE_REQUEST_OUTPUT | \ 126925ca369SKent Gibson GPIOHANDLE_REQUEST_ACTIVE_LOW | \ 127925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_UP | \ 128925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \ 129925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_DISABLE | \ 130925ca369SKent Gibson GPIOHANDLE_REQUEST_OPEN_DRAIN | \ 131925ca369SKent Gibson GPIOHANDLE_REQUEST_OPEN_SOURCE) 132925ca369SKent Gibson 133925ca369SKent Gibson static int linehandle_validate_flags(u32 flags) 134925ca369SKent Gibson { 135925ca369SKent Gibson /* Return an error if an unknown flag is set */ 136925ca369SKent Gibson if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) 137925ca369SKent Gibson return -EINVAL; 138925ca369SKent Gibson 139925ca369SKent Gibson /* 140925ca369SKent Gibson * Do not allow both INPUT & OUTPUT flags to be set as they are 141925ca369SKent Gibson * contradictory. 142925ca369SKent Gibson */ 143925ca369SKent Gibson if ((flags & GPIOHANDLE_REQUEST_INPUT) && 144925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OUTPUT)) 145925ca369SKent Gibson return -EINVAL; 146925ca369SKent Gibson 147925ca369SKent Gibson /* 148925ca369SKent Gibson * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If 149925ca369SKent Gibson * the hardware actually supports enabling both at the same time the 150925ca369SKent Gibson * electrical result would be disastrous. 151925ca369SKent Gibson */ 152925ca369SKent Gibson if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) && 153925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) 154925ca369SKent Gibson return -EINVAL; 155925ca369SKent Gibson 156925ca369SKent Gibson /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */ 157925ca369SKent Gibson if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) && 158925ca369SKent Gibson ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || 159925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))) 160925ca369SKent Gibson return -EINVAL; 161925ca369SKent Gibson 162925ca369SKent Gibson /* Bias flags only allowed for input or output mode. */ 163925ca369SKent Gibson if (!((flags & GPIOHANDLE_REQUEST_INPUT) || 164925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OUTPUT)) && 165925ca369SKent Gibson ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) || 166925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) || 167925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN))) 168925ca369SKent Gibson return -EINVAL; 169925ca369SKent Gibson 170925ca369SKent Gibson /* Only one bias flag can be set. */ 171925ca369SKent Gibson if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && 172925ca369SKent Gibson (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | 173925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || 174925ca369SKent Gibson ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && 175925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) 176925ca369SKent Gibson return -EINVAL; 177925ca369SKent Gibson 178925ca369SKent Gibson return 0; 179925ca369SKent Gibson } 180925ca369SKent Gibson 181c274b58aSKent Gibson static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp) 182c274b58aSKent Gibson { 183c274b58aSKent Gibson assign_bit(FLAG_ACTIVE_LOW, flagsp, 184c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW); 185c274b58aSKent Gibson assign_bit(FLAG_OPEN_DRAIN, flagsp, 186c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN); 187c274b58aSKent Gibson assign_bit(FLAG_OPEN_SOURCE, flagsp, 188c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE); 189c274b58aSKent Gibson assign_bit(FLAG_PULL_UP, flagsp, 190c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP); 191c274b58aSKent Gibson assign_bit(FLAG_PULL_DOWN, flagsp, 192c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN); 193c274b58aSKent Gibson assign_bit(FLAG_BIAS_DISABLE, flagsp, 194c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE); 195c274b58aSKent Gibson } 196c274b58aSKent Gibson 197925ca369SKent Gibson static long linehandle_set_config(struct linehandle_state *lh, 198925ca369SKent Gibson void __user *ip) 199925ca369SKent Gibson { 200925ca369SKent Gibson struct gpiohandle_config gcnf; 201925ca369SKent Gibson struct gpio_desc *desc; 202925ca369SKent Gibson int i, ret; 203925ca369SKent Gibson u32 lflags; 204925ca369SKent Gibson 205925ca369SKent Gibson if (copy_from_user(&gcnf, ip, sizeof(gcnf))) 206925ca369SKent Gibson return -EFAULT; 207925ca369SKent Gibson 208925ca369SKent Gibson lflags = gcnf.flags; 209925ca369SKent Gibson ret = linehandle_validate_flags(lflags); 210925ca369SKent Gibson if (ret) 211925ca369SKent Gibson return ret; 212925ca369SKent Gibson 21352b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) { 214925ca369SKent Gibson desc = lh->descs[i]; 215c274b58aSKent Gibson linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags); 216925ca369SKent Gibson 217925ca369SKent Gibson /* 218925ca369SKent Gibson * Lines have to be requested explicitly for input 219925ca369SKent Gibson * or output, else the line will be treated "as is". 220925ca369SKent Gibson */ 221925ca369SKent Gibson if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { 222925ca369SKent Gibson int val = !!gcnf.default_values[i]; 223925ca369SKent Gibson 224925ca369SKent Gibson ret = gpiod_direction_output(desc, val); 225925ca369SKent Gibson if (ret) 226925ca369SKent Gibson return ret; 227925ca369SKent Gibson } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { 228925ca369SKent Gibson ret = gpiod_direction_input(desc); 229925ca369SKent Gibson if (ret) 230925ca369SKent Gibson return ret; 231925ca369SKent Gibson } 232925ca369SKent Gibson 233*9ce4ed5bSBartosz Golaszewski gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); 234925ca369SKent Gibson } 235925ca369SKent Gibson return 0; 236925ca369SKent Gibson } 237925ca369SKent Gibson 238bdbbae24SBartosz Golaszewski static long linehandle_ioctl_unlocked(struct file *file, unsigned int cmd, 239925ca369SKent Gibson unsigned long arg) 240925ca369SKent Gibson { 24149bc5279SKent Gibson struct linehandle_state *lh = file->private_data; 242925ca369SKent Gibson void __user *ip = (void __user *)arg; 243925ca369SKent Gibson struct gpiohandle_data ghd; 244925ca369SKent Gibson DECLARE_BITMAP(vals, GPIOHANDLES_MAX); 2451cef8b50SAndy Shevchenko unsigned int i; 2461cef8b50SAndy Shevchenko int ret; 247925ca369SKent Gibson 248533aae7cSBartosz Golaszewski if (!lh->gdev->chip) 249533aae7cSBartosz Golaszewski return -ENODEV; 250533aae7cSBartosz Golaszewski 2511cef8b50SAndy Shevchenko switch (cmd) { 2521cef8b50SAndy Shevchenko case GPIOHANDLE_GET_LINE_VALUES_IOCTL: 2531cef8b50SAndy Shevchenko /* NOTE: It's okay to read values of output lines */ 2541cef8b50SAndy Shevchenko ret = gpiod_get_array_value_complex(false, true, 2551cef8b50SAndy Shevchenko lh->num_descs, lh->descs, 2561cef8b50SAndy Shevchenko NULL, vals); 257925ca369SKent Gibson if (ret) 258925ca369SKent Gibson return ret; 259925ca369SKent Gibson 260925ca369SKent Gibson memset(&ghd, 0, sizeof(ghd)); 26152b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) 262925ca369SKent Gibson ghd.values[i] = test_bit(i, vals); 263925ca369SKent Gibson 264925ca369SKent Gibson if (copy_to_user(ip, &ghd, sizeof(ghd))) 265925ca369SKent Gibson return -EFAULT; 266925ca369SKent Gibson 267925ca369SKent Gibson return 0; 2681cef8b50SAndy Shevchenko case GPIOHANDLE_SET_LINE_VALUES_IOCTL: 269925ca369SKent Gibson /* 270925ca369SKent Gibson * All line descriptors were created at once with the same 271925ca369SKent Gibson * flags so just check if the first one is really output. 272925ca369SKent Gibson */ 273925ca369SKent Gibson if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags)) 274925ca369SKent Gibson return -EPERM; 275925ca369SKent Gibson 276925ca369SKent Gibson if (copy_from_user(&ghd, ip, sizeof(ghd))) 277925ca369SKent Gibson return -EFAULT; 278925ca369SKent Gibson 279925ca369SKent Gibson /* Clamp all values to [0,1] */ 28052b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) 281925ca369SKent Gibson __assign_bit(i, vals, ghd.values[i]); 282925ca369SKent Gibson 283925ca369SKent Gibson /* Reuse the array setting function */ 284925ca369SKent Gibson return gpiod_set_array_value_complex(false, 285925ca369SKent Gibson true, 28652b7b596SKent Gibson lh->num_descs, 287925ca369SKent Gibson lh->descs, 288925ca369SKent Gibson NULL, 289925ca369SKent Gibson vals); 2901cef8b50SAndy Shevchenko case GPIOHANDLE_SET_CONFIG_IOCTL: 291925ca369SKent Gibson return linehandle_set_config(lh, ip); 2921cef8b50SAndy Shevchenko default: 293925ca369SKent Gibson return -EINVAL; 294925ca369SKent Gibson } 2951cef8b50SAndy Shevchenko } 296925ca369SKent Gibson 297bdbbae24SBartosz Golaszewski static long linehandle_ioctl(struct file *file, unsigned int cmd, 298bdbbae24SBartosz Golaszewski unsigned long arg) 299bdbbae24SBartosz Golaszewski { 300bdbbae24SBartosz Golaszewski struct linehandle_state *lh = file->private_data; 301bdbbae24SBartosz Golaszewski 302bdbbae24SBartosz Golaszewski return call_ioctl_locked(file, cmd, arg, lh->gdev, 303bdbbae24SBartosz Golaszewski linehandle_ioctl_unlocked); 304bdbbae24SBartosz Golaszewski } 305bdbbae24SBartosz Golaszewski 306925ca369SKent Gibson #ifdef CONFIG_COMPAT 30749bc5279SKent Gibson static long linehandle_ioctl_compat(struct file *file, unsigned int cmd, 308925ca369SKent Gibson unsigned long arg) 309925ca369SKent Gibson { 31049bc5279SKent Gibson return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 311925ca369SKent Gibson } 312925ca369SKent Gibson #endif 313925ca369SKent Gibson 314883f9198SKent Gibson static void linehandle_free(struct linehandle_state *lh) 315925ca369SKent Gibson { 316925ca369SKent Gibson int i; 317925ca369SKent Gibson 31852b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) 319883f9198SKent Gibson if (lh->descs[i]) 320925ca369SKent Gibson gpiod_free(lh->descs[i]); 321925ca369SKent Gibson kfree(lh->label); 322dc0989e3SAndy Shevchenko gpio_device_put(lh->gdev); 323925ca369SKent Gibson kfree(lh); 324883f9198SKent Gibson } 325883f9198SKent Gibson 326883f9198SKent Gibson static int linehandle_release(struct inode *inode, struct file *file) 327883f9198SKent Gibson { 328883f9198SKent Gibson linehandle_free(file->private_data); 329925ca369SKent Gibson return 0; 330925ca369SKent Gibson } 331925ca369SKent Gibson 332925ca369SKent Gibson static const struct file_operations linehandle_fileops = { 333925ca369SKent Gibson .release = linehandle_release, 334925ca369SKent Gibson .owner = THIS_MODULE, 335925ca369SKent Gibson .llseek = noop_llseek, 336925ca369SKent Gibson .unlocked_ioctl = linehandle_ioctl, 337925ca369SKent Gibson #ifdef CONFIG_COMPAT 338925ca369SKent Gibson .compat_ioctl = linehandle_ioctl_compat, 339925ca369SKent Gibson #endif 340925ca369SKent Gibson }; 341925ca369SKent Gibson 342925ca369SKent Gibson static int linehandle_create(struct gpio_device *gdev, void __user *ip) 343925ca369SKent Gibson { 344925ca369SKent Gibson struct gpiohandle_request handlereq; 345925ca369SKent Gibson struct linehandle_state *lh; 346925ca369SKent Gibson struct file *file; 347883f9198SKent Gibson int fd, i, ret; 348925ca369SKent Gibson u32 lflags; 349925ca369SKent Gibson 350925ca369SKent Gibson if (copy_from_user(&handlereq, ip, sizeof(handlereq))) 351925ca369SKent Gibson return -EFAULT; 352925ca369SKent Gibson if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) 353925ca369SKent Gibson return -EINVAL; 354925ca369SKent Gibson 355925ca369SKent Gibson lflags = handlereq.flags; 356925ca369SKent Gibson 357925ca369SKent Gibson ret = linehandle_validate_flags(lflags); 358925ca369SKent Gibson if (ret) 359925ca369SKent Gibson return ret; 360925ca369SKent Gibson 361925ca369SKent Gibson lh = kzalloc(sizeof(*lh), GFP_KERNEL); 362925ca369SKent Gibson if (!lh) 363925ca369SKent Gibson return -ENOMEM; 364dc0989e3SAndy Shevchenko lh->gdev = gpio_device_get(gdev); 365925ca369SKent Gibson 366f188ac12SKent Gibson if (handlereq.consumer_label[0] != '\0') { 367f188ac12SKent Gibson /* label is only initialized if consumer_label is set */ 368f188ac12SKent Gibson lh->label = kstrndup(handlereq.consumer_label, 369f188ac12SKent Gibson sizeof(handlereq.consumer_label) - 1, 370925ca369SKent Gibson GFP_KERNEL); 371925ca369SKent Gibson if (!lh->label) { 372925ca369SKent Gibson ret = -ENOMEM; 373925ca369SKent Gibson goto out_free_lh; 374925ca369SKent Gibson } 375925ca369SKent Gibson } 376925ca369SKent Gibson 377883f9198SKent Gibson lh->num_descs = handlereq.lines; 378883f9198SKent Gibson 379925ca369SKent Gibson /* Request each GPIO */ 380925ca369SKent Gibson for (i = 0; i < handlereq.lines; i++) { 381925ca369SKent Gibson u32 offset = handlereq.lineoffsets[i]; 382925ca369SKent Gibson struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); 383925ca369SKent Gibson 384925ca369SKent Gibson if (IS_ERR(desc)) { 385925ca369SKent Gibson ret = PTR_ERR(desc); 386883f9198SKent Gibson goto out_free_lh; 387925ca369SKent Gibson } 388925ca369SKent Gibson 38995a4eed7SAndy Shevchenko ret = gpiod_request_user(desc, lh->label); 390925ca369SKent Gibson if (ret) 391883f9198SKent Gibson goto out_free_lh; 392925ca369SKent Gibson lh->descs[i] = desc; 393c274b58aSKent Gibson linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags); 394925ca369SKent Gibson 395925ca369SKent Gibson ret = gpiod_set_transitory(desc, false); 396925ca369SKent Gibson if (ret < 0) 397883f9198SKent Gibson goto out_free_lh; 398925ca369SKent Gibson 399925ca369SKent Gibson /* 400925ca369SKent Gibson * Lines have to be requested explicitly for input 401925ca369SKent Gibson * or output, else the line will be treated "as is". 402925ca369SKent Gibson */ 403925ca369SKent Gibson if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { 404925ca369SKent Gibson int val = !!handlereq.default_values[i]; 405925ca369SKent Gibson 406925ca369SKent Gibson ret = gpiod_direction_output(desc, val); 407925ca369SKent Gibson if (ret) 408883f9198SKent Gibson goto out_free_lh; 409925ca369SKent Gibson } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { 410925ca369SKent Gibson ret = gpiod_direction_input(desc); 411925ca369SKent Gibson if (ret) 412883f9198SKent Gibson goto out_free_lh; 413925ca369SKent Gibson } 414925ca369SKent Gibson 415*9ce4ed5bSBartosz Golaszewski gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED); 416925ca369SKent Gibson 417925ca369SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", 418925ca369SKent Gibson offset); 419925ca369SKent Gibson } 420925ca369SKent Gibson 421925ca369SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 422925ca369SKent Gibson if (fd < 0) { 423925ca369SKent Gibson ret = fd; 424883f9198SKent Gibson goto out_free_lh; 425925ca369SKent Gibson } 426925ca369SKent Gibson 427925ca369SKent Gibson file = anon_inode_getfile("gpio-linehandle", 428925ca369SKent Gibson &linehandle_fileops, 429925ca369SKent Gibson lh, 430925ca369SKent Gibson O_RDONLY | O_CLOEXEC); 431925ca369SKent Gibson if (IS_ERR(file)) { 432925ca369SKent Gibson ret = PTR_ERR(file); 433925ca369SKent Gibson goto out_put_unused_fd; 434925ca369SKent Gibson } 435925ca369SKent Gibson 436925ca369SKent Gibson handlereq.fd = fd; 437925ca369SKent Gibson if (copy_to_user(ip, &handlereq, sizeof(handlereq))) { 438925ca369SKent Gibson /* 439925ca369SKent Gibson * fput() will trigger the release() callback, so do not go onto 440925ca369SKent Gibson * the regular error cleanup path here. 441925ca369SKent Gibson */ 442925ca369SKent Gibson fput(file); 443925ca369SKent Gibson put_unused_fd(fd); 444925ca369SKent Gibson return -EFAULT; 445925ca369SKent Gibson } 446925ca369SKent Gibson 447925ca369SKent Gibson fd_install(fd, file); 448925ca369SKent Gibson 449925ca369SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", 45052b7b596SKent Gibson lh->num_descs); 451925ca369SKent Gibson 452925ca369SKent Gibson return 0; 453925ca369SKent Gibson 454925ca369SKent Gibson out_put_unused_fd: 455925ca369SKent Gibson put_unused_fd(fd); 456925ca369SKent Gibson out_free_lh: 457883f9198SKent Gibson linehandle_free(lh); 458925ca369SKent Gibson return ret; 459925ca369SKent Gibson } 4603c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 4613c0d9c63SKent Gibson 4623c0d9c63SKent Gibson /** 4633c0d9c63SKent Gibson * struct line - contains the state of a requested line 4643c0d9c63SKent Gibson * @desc: the GPIO descriptor for this line. 46573e03419SKent Gibson * @req: the corresponding line request 46673e03419SKent Gibson * @irq: the interrupt triggered in response to events on this GPIO 4678d259847SAndy Shevchenko * @edflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or 46873e03419SKent Gibson * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied 46973e03419SKent Gibson * @timestamp_ns: cache for the timestamp storing it between hardirq and 47073e03419SKent Gibson * IRQ thread, used to bring the timestamp close to the actual event 47173e03419SKent Gibson * @req_seqno: the seqno for the current edge event in the sequence of 47273e03419SKent Gibson * events for the corresponding line request. This is drawn from the @req. 47373e03419SKent Gibson * @line_seqno: the seqno for the current edge event in the sequence of 47473e03419SKent Gibson * events for this line. 47565cff704SKent Gibson * @work: the worker that implements software debouncing 47665cff704SKent Gibson * @sw_debounced: flag indicating if the software debouncer is active 47765cff704SKent Gibson * @level: the current debounced physical level of the line 47885ff37e3SAndy Shevchenko * @hdesc: the Hardware Timestamp Engine (HTE) descriptor 47985ff37e3SAndy Shevchenko * @raw_level: the line level at the time of event 48085ff37e3SAndy Shevchenko * @total_discard_seq: the running counter of the discarded events 48185ff37e3SAndy Shevchenko * @last_seqno: the last sequence number before debounce period expires 4823c0d9c63SKent Gibson */ 4833c0d9c63SKent Gibson struct line { 4843c0d9c63SKent Gibson struct gpio_desc *desc; 48573e03419SKent Gibson /* 48673e03419SKent Gibson * -- edge detector specific fields -- 48773e03419SKent Gibson */ 48873e03419SKent Gibson struct linereq *req; 48973e03419SKent Gibson unsigned int irq; 4903ffb7c45SKent Gibson /* 491b1a92e94SKent Gibson * The flags for the active edge detector configuration. 492b1a92e94SKent Gibson * 493b1a92e94SKent Gibson * edflags is set by linereq_create(), linereq_free(), and 494b1a92e94SKent Gibson * linereq_set_config_unlocked(), which are themselves mutually 495b1a92e94SKent Gibson * exclusive, and is accessed by edge_irq_thread(), 496b1a92e94SKent Gibson * process_hw_ts_thread() and debounce_work_func(), 497b1a92e94SKent Gibson * which can all live with a slightly stale value. 4983ffb7c45SKent Gibson */ 499b1a92e94SKent Gibson u64 edflags; 50073e03419SKent Gibson /* 50173e03419SKent Gibson * timestamp_ns and req_seqno are accessed only by 50273e03419SKent Gibson * edge_irq_handler() and edge_irq_thread(), which are themselves 50373e03419SKent Gibson * mutually exclusive, so no additional protection is necessary. 50473e03419SKent Gibson */ 50573e03419SKent Gibson u64 timestamp_ns; 50673e03419SKent Gibson u32 req_seqno; 50765cff704SKent Gibson /* 50865cff704SKent Gibson * line_seqno is accessed by either edge_irq_thread() or 50965cff704SKent Gibson * debounce_work_func(), which are themselves mutually exclusive, 51065cff704SKent Gibson * so no additional protection is necessary. 51165cff704SKent Gibson */ 51273e03419SKent Gibson u32 line_seqno; 51365cff704SKent Gibson /* 51465cff704SKent Gibson * -- debouncer specific fields -- 51565cff704SKent Gibson */ 51665cff704SKent Gibson struct delayed_work work; 51765cff704SKent Gibson /* 51865cff704SKent Gibson * sw_debounce is accessed by linereq_set_config(), which is the 51965cff704SKent Gibson * only setter, and linereq_get_values(), which can live with a 52065cff704SKent Gibson * slightly stale value. 52165cff704SKent Gibson */ 52265cff704SKent Gibson unsigned int sw_debounced; 52365cff704SKent Gibson /* 52465cff704SKent Gibson * level is accessed by debounce_work_func(), which is the only 52565cff704SKent Gibson * setter, and linereq_get_values() which can live with a slightly 52665cff704SKent Gibson * stale value. 52765cff704SKent Gibson */ 52865cff704SKent Gibson unsigned int level; 529272ddba0SKent Gibson #ifdef CONFIG_HTE 5302068339aSDipen Patel struct hte_ts_desc hdesc; 5312068339aSDipen Patel /* 5322068339aSDipen Patel * HTE provider sets line level at the time of event. The valid 5332068339aSDipen Patel * value is 0 or 1 and negative value for an error. 5342068339aSDipen Patel */ 5352068339aSDipen Patel int raw_level; 5362068339aSDipen Patel /* 5372068339aSDipen Patel * when sw_debounce is set on HTE enabled line, this is running 5382068339aSDipen Patel * counter of the discarded events. 5392068339aSDipen Patel */ 5402068339aSDipen Patel u32 total_discard_seq; 5412068339aSDipen Patel /* 5422068339aSDipen Patel * when sw_debounce is set on HTE enabled line, this variable records 5432068339aSDipen Patel * last sequence number before debounce period expires. 5442068339aSDipen Patel */ 5452068339aSDipen Patel u32 last_seqno; 546272ddba0SKent Gibson #endif /* CONFIG_HTE */ 5473c0d9c63SKent Gibson }; 5483c0d9c63SKent Gibson 5493c0d9c63SKent Gibson /** 5503c0d9c63SKent Gibson * struct linereq - contains the state of a userspace line request 5513c0d9c63SKent Gibson * @gdev: the GPIO device the line request pertains to 5523c0d9c63SKent Gibson * @label: consumer label used to tag GPIO descriptors 5533c0d9c63SKent Gibson * @num_lines: the number of lines in the lines array 55473e03419SKent Gibson * @wait: wait queue that handles blocking reads of events 555a0dda508SBartosz Golaszewski * @device_unregistered_nb: notifier block for receiving gdev unregister events 55673e03419SKent Gibson * @event_buffer_size: the number of elements allocated in @events 55773e03419SKent Gibson * @events: KFIFO for the GPIO events 55873e03419SKent Gibson * @seqno: the sequence number for edge events generated on all lines in 55973e03419SKent Gibson * this line request. Note that this is not used when @num_lines is 1, as 56073e03419SKent Gibson * the line_seqno is then the same and is cheaper to calculate. 561a54756cbSKent Gibson * @config_mutex: mutex for serializing ioctl() calls to ensure consistency 562a54756cbSKent Gibson * of configuration, particularly multi-step accesses to desc flags. 5633c0d9c63SKent Gibson * @lines: the lines held by this line request, with @num_lines elements. 5643c0d9c63SKent Gibson */ 5653c0d9c63SKent Gibson struct linereq { 5663c0d9c63SKent Gibson struct gpio_device *gdev; 5673c0d9c63SKent Gibson const char *label; 5683c0d9c63SKent Gibson u32 num_lines; 56973e03419SKent Gibson wait_queue_head_t wait; 570a0dda508SBartosz Golaszewski struct notifier_block device_unregistered_nb; 57173e03419SKent Gibson u32 event_buffer_size; 57273e03419SKent Gibson DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); 57373e03419SKent Gibson atomic_t seqno; 574a54756cbSKent Gibson struct mutex config_mutex; 5753c0d9c63SKent Gibson struct line lines[]; 5763c0d9c63SKent Gibson }; 5773c0d9c63SKent Gibson 5783c0d9c63SKent Gibson #define GPIO_V2_LINE_BIAS_FLAGS \ 5793c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ 5803c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \ 5813c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_DISABLED) 5823c0d9c63SKent Gibson 5833c0d9c63SKent Gibson #define GPIO_V2_LINE_DIRECTION_FLAGS \ 5843c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_INPUT | \ 5853c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_OUTPUT) 5863c0d9c63SKent Gibson 5873c0d9c63SKent Gibson #define GPIO_V2_LINE_DRIVE_FLAGS \ 5883c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \ 5893c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_OPEN_SOURCE) 5903c0d9c63SKent Gibson 59173e03419SKent Gibson #define GPIO_V2_LINE_EDGE_FLAGS \ 59273e03419SKent Gibson (GPIO_V2_LINE_FLAG_EDGE_RISING | \ 59373e03419SKent Gibson GPIO_V2_LINE_FLAG_EDGE_FALLING) 59473e03419SKent Gibson 5955e2ca893SKent Gibson #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS 5965e2ca893SKent Gibson 5973c0d9c63SKent Gibson #define GPIO_V2_LINE_VALID_FLAGS \ 5983c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ 5993c0d9c63SKent Gibson GPIO_V2_LINE_DIRECTION_FLAGS | \ 6003c0d9c63SKent Gibson GPIO_V2_LINE_DRIVE_FLAGS | \ 60173e03419SKent Gibson GPIO_V2_LINE_EDGE_FLAGS | \ 60226d060e4SKent Gibson GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \ 6032068339aSDipen Patel GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \ 6043c0d9c63SKent Gibson GPIO_V2_LINE_BIAS_FLAGS) 6053c0d9c63SKent Gibson 606b1a92e94SKent Gibson /* subset of flags relevant for edge detector configuration */ 607b1a92e94SKent Gibson #define GPIO_V2_LINE_EDGE_DETECTOR_FLAGS \ 608b1a92e94SKent Gibson (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ 609b1a92e94SKent Gibson GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \ 610b1a92e94SKent Gibson GPIO_V2_LINE_EDGE_FLAGS) 611b1a92e94SKent Gibson 612a0dda508SBartosz Golaszewski static int linereq_unregistered_notify(struct notifier_block *nb, 613a0dda508SBartosz Golaszewski unsigned long action, void *data) 614a0dda508SBartosz Golaszewski { 615a0dda508SBartosz Golaszewski struct linereq *lr = container_of(nb, struct linereq, 616a0dda508SBartosz Golaszewski device_unregistered_nb); 617a0dda508SBartosz Golaszewski 618a0dda508SBartosz Golaszewski wake_up_poll(&lr->wait, EPOLLIN | EPOLLERR); 619a0dda508SBartosz Golaszewski 620a0dda508SBartosz Golaszewski return NOTIFY_OK; 621a0dda508SBartosz Golaszewski } 622a0dda508SBartosz Golaszewski 62373e03419SKent Gibson static void linereq_put_event(struct linereq *lr, 62473e03419SKent Gibson struct gpio_v2_line_event *le) 62573e03419SKent Gibson { 62673e03419SKent Gibson bool overflow = false; 62773e03419SKent Gibson 62873e03419SKent Gibson spin_lock(&lr->wait.lock); 62973e03419SKent Gibson if (kfifo_is_full(&lr->events)) { 63073e03419SKent Gibson overflow = true; 63173e03419SKent Gibson kfifo_skip(&lr->events); 63273e03419SKent Gibson } 63373e03419SKent Gibson kfifo_in(&lr->events, le, 1); 63473e03419SKent Gibson spin_unlock(&lr->wait.lock); 63573e03419SKent Gibson if (!overflow) 63673e03419SKent Gibson wake_up_poll(&lr->wait, EPOLLIN); 63773e03419SKent Gibson else 63873e03419SKent Gibson pr_debug_ratelimited("event FIFO is full - event dropped\n"); 63973e03419SKent Gibson } 64073e03419SKent Gibson 64126d060e4SKent Gibson static u64 line_event_timestamp(struct line *line) 64226d060e4SKent Gibson { 64326d060e4SKent Gibson if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags)) 64426d060e4SKent Gibson return ktime_get_real_ns(); 645272ddba0SKent Gibson else if (IS_ENABLED(CONFIG_HTE) && 646272ddba0SKent Gibson test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags)) 6472068339aSDipen Patel return line->timestamp_ns; 64826d060e4SKent Gibson 64926d060e4SKent Gibson return ktime_get_ns(); 65026d060e4SKent Gibson } 65126d060e4SKent Gibson 65224220232SKent Gibson static u32 line_event_id(int level) 65324220232SKent Gibson { 65424220232SKent Gibson return level ? GPIO_V2_LINE_EVENT_RISING_EDGE : 65524220232SKent Gibson GPIO_V2_LINE_EVENT_FALLING_EDGE; 65624220232SKent Gibson } 65724220232SKent Gibson 658272ddba0SKent Gibson #ifdef CONFIG_HTE 659272ddba0SKent Gibson 6602068339aSDipen Patel static enum hte_return process_hw_ts_thread(void *p) 6612068339aSDipen Patel { 6622068339aSDipen Patel struct line *line; 6632068339aSDipen Patel struct linereq *lr; 6642068339aSDipen Patel struct gpio_v2_line_event le; 665b1a92e94SKent Gibson u64 edflags; 6662068339aSDipen Patel int level; 6672068339aSDipen Patel 6682068339aSDipen Patel if (!p) 6692068339aSDipen Patel return HTE_CB_HANDLED; 6702068339aSDipen Patel 6712068339aSDipen Patel line = p; 6722068339aSDipen Patel lr = line->req; 6732068339aSDipen Patel 6742068339aSDipen Patel memset(&le, 0, sizeof(le)); 6752068339aSDipen Patel 6762068339aSDipen Patel le.timestamp_ns = line->timestamp_ns; 677b1a92e94SKent Gibson edflags = READ_ONCE(line->edflags); 6782068339aSDipen Patel 679b1a92e94SKent Gibson switch (edflags & GPIO_V2_LINE_EDGE_FLAGS) { 680cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_BOTH: 68124220232SKent Gibson level = (line->raw_level >= 0) ? 68224220232SKent Gibson line->raw_level : 68324220232SKent Gibson gpiod_get_raw_value_cansleep(line->desc); 6842068339aSDipen Patel 685b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 68624220232SKent Gibson level = !level; 68724220232SKent Gibson 68824220232SKent Gibson le.id = line_event_id(level); 689cfa53463SKent Gibson break; 690cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_RISING: 6912068339aSDipen Patel le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 692cfa53463SKent Gibson break; 693cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_FALLING: 6942068339aSDipen Patel le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 695cfa53463SKent Gibson break; 696cfa53463SKent Gibson default: 6972068339aSDipen Patel return HTE_CB_HANDLED; 6982068339aSDipen Patel } 6992068339aSDipen Patel le.line_seqno = line->line_seqno; 7002068339aSDipen Patel le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno; 7012068339aSDipen Patel le.offset = gpio_chip_hwgpio(line->desc); 7022068339aSDipen Patel 7032068339aSDipen Patel linereq_put_event(lr, &le); 7042068339aSDipen Patel 7052068339aSDipen Patel return HTE_CB_HANDLED; 7062068339aSDipen Patel } 7072068339aSDipen Patel 7082068339aSDipen Patel static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p) 7092068339aSDipen Patel { 7102068339aSDipen Patel struct line *line; 7112068339aSDipen Patel struct linereq *lr; 7122068339aSDipen Patel int diff_seqno = 0; 7132068339aSDipen Patel 7142068339aSDipen Patel if (!ts || !p) 7152068339aSDipen Patel return HTE_CB_HANDLED; 7162068339aSDipen Patel 7172068339aSDipen Patel line = p; 7182068339aSDipen Patel line->timestamp_ns = ts->tsc; 7192068339aSDipen Patel line->raw_level = ts->raw_level; 7202068339aSDipen Patel lr = line->req; 7212068339aSDipen Patel 7222068339aSDipen Patel if (READ_ONCE(line->sw_debounced)) { 7232068339aSDipen Patel line->total_discard_seq++; 7242068339aSDipen Patel line->last_seqno = ts->seq; 7252068339aSDipen Patel mod_delayed_work(system_wq, &line->work, 7262068339aSDipen Patel usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us))); 7272068339aSDipen Patel } else { 7282068339aSDipen Patel if (unlikely(ts->seq < line->line_seqno)) 7292068339aSDipen Patel return HTE_CB_HANDLED; 7302068339aSDipen Patel 7312068339aSDipen Patel diff_seqno = ts->seq - line->line_seqno; 7322068339aSDipen Patel line->line_seqno = ts->seq; 7332068339aSDipen Patel if (lr->num_lines != 1) 7342068339aSDipen Patel line->req_seqno = atomic_add_return(diff_seqno, 7352068339aSDipen Patel &lr->seqno); 7362068339aSDipen Patel 7372068339aSDipen Patel return HTE_RUN_SECOND_CB; 7382068339aSDipen Patel } 7392068339aSDipen Patel 7402068339aSDipen Patel return HTE_CB_HANDLED; 7412068339aSDipen Patel } 7422068339aSDipen Patel 743272ddba0SKent Gibson static int hte_edge_setup(struct line *line, u64 eflags) 744272ddba0SKent Gibson { 745272ddba0SKent Gibson int ret; 746272ddba0SKent Gibson unsigned long flags = 0; 747272ddba0SKent Gibson struct hte_ts_desc *hdesc = &line->hdesc; 748272ddba0SKent Gibson 749272ddba0SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) 750272ddba0SKent Gibson flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 751272ddba0SKent Gibson HTE_FALLING_EDGE_TS : 752272ddba0SKent Gibson HTE_RISING_EDGE_TS; 753272ddba0SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) 754272ddba0SKent Gibson flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 755272ddba0SKent Gibson HTE_RISING_EDGE_TS : 756272ddba0SKent Gibson HTE_FALLING_EDGE_TS; 757272ddba0SKent Gibson 758272ddba0SKent Gibson line->total_discard_seq = 0; 759272ddba0SKent Gibson 760272ddba0SKent Gibson hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags, NULL, 761272ddba0SKent Gibson line->desc); 762272ddba0SKent Gibson 763272ddba0SKent Gibson ret = hte_ts_get(NULL, hdesc, 0); 764272ddba0SKent Gibson if (ret) 765272ddba0SKent Gibson return ret; 766272ddba0SKent Gibson 767272ddba0SKent Gibson return hte_request_ts_ns(hdesc, process_hw_ts, process_hw_ts_thread, 768272ddba0SKent Gibson line); 769272ddba0SKent Gibson } 770272ddba0SKent Gibson 771272ddba0SKent Gibson #else 772272ddba0SKent Gibson 773272ddba0SKent Gibson static int hte_edge_setup(struct line *line, u64 eflags) 774272ddba0SKent Gibson { 775272ddba0SKent Gibson return 0; 776272ddba0SKent Gibson } 777272ddba0SKent Gibson #endif /* CONFIG_HTE */ 778272ddba0SKent Gibson 77973e03419SKent Gibson static irqreturn_t edge_irq_thread(int irq, void *p) 78073e03419SKent Gibson { 78173e03419SKent Gibson struct line *line = p; 78273e03419SKent Gibson struct linereq *lr = line->req; 78373e03419SKent Gibson struct gpio_v2_line_event le; 78473e03419SKent Gibson 78573e03419SKent Gibson /* Do not leak kernel stack to userspace */ 78673e03419SKent Gibson memset(&le, 0, sizeof(le)); 78773e03419SKent Gibson 78873e03419SKent Gibson if (line->timestamp_ns) { 78973e03419SKent Gibson le.timestamp_ns = line->timestamp_ns; 79073e03419SKent Gibson } else { 79173e03419SKent Gibson /* 79273e03419SKent Gibson * We may be running from a nested threaded interrupt in 79373e03419SKent Gibson * which case we didn't get the timestamp from 79473e03419SKent Gibson * edge_irq_handler(). 79573e03419SKent Gibson */ 79626d060e4SKent Gibson le.timestamp_ns = line_event_timestamp(line); 79773e03419SKent Gibson if (lr->num_lines != 1) 79873e03419SKent Gibson line->req_seqno = atomic_inc_return(&lr->seqno); 79973e03419SKent Gibson } 80073e03419SKent Gibson line->timestamp_ns = 0; 80173e03419SKent Gibson 802b1a92e94SKent Gibson switch (READ_ONCE(line->edflags) & GPIO_V2_LINE_EDGE_FLAGS) { 803cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_BOTH: 80424220232SKent Gibson le.id = line_event_id(gpiod_get_value_cansleep(line->desc)); 805cfa53463SKent Gibson break; 806cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_RISING: 80773e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 808cfa53463SKent Gibson break; 809cfa53463SKent Gibson case GPIO_V2_LINE_FLAG_EDGE_FALLING: 81073e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 811cfa53463SKent Gibson break; 812cfa53463SKent Gibson default: 81373e03419SKent Gibson return IRQ_NONE; 81473e03419SKent Gibson } 81573e03419SKent Gibson line->line_seqno++; 81673e03419SKent Gibson le.line_seqno = line->line_seqno; 81773e03419SKent Gibson le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno; 81873e03419SKent Gibson le.offset = gpio_chip_hwgpio(line->desc); 81973e03419SKent Gibson 82073e03419SKent Gibson linereq_put_event(lr, &le); 82173e03419SKent Gibson 82273e03419SKent Gibson return IRQ_HANDLED; 82373e03419SKent Gibson } 82473e03419SKent Gibson 82573e03419SKent Gibson static irqreturn_t edge_irq_handler(int irq, void *p) 82673e03419SKent Gibson { 82773e03419SKent Gibson struct line *line = p; 82873e03419SKent Gibson struct linereq *lr = line->req; 82973e03419SKent Gibson 83073e03419SKent Gibson /* 83173e03419SKent Gibson * Just store the timestamp in hardirq context so we get it as 83273e03419SKent Gibson * close in time as possible to the actual event. 83373e03419SKent Gibson */ 83426d060e4SKent Gibson line->timestamp_ns = line_event_timestamp(line); 83573e03419SKent Gibson 83673e03419SKent Gibson if (lr->num_lines != 1) 83773e03419SKent Gibson line->req_seqno = atomic_inc_return(&lr->seqno); 83873e03419SKent Gibson 83973e03419SKent Gibson return IRQ_WAKE_THREAD; 84073e03419SKent Gibson } 84173e03419SKent Gibson 84265cff704SKent Gibson /* 84365cff704SKent Gibson * returns the current debounced logical value. 84465cff704SKent Gibson */ 84565cff704SKent Gibson static bool debounced_value(struct line *line) 84665cff704SKent Gibson { 84765cff704SKent Gibson bool value; 84865cff704SKent Gibson 84965cff704SKent Gibson /* 85065cff704SKent Gibson * minor race - debouncer may be stopped here, so edge_detector_stop() 85165cff704SKent Gibson * must leave the value unchanged so the following will read the level 85265cff704SKent Gibson * from when the debouncer was last running. 85365cff704SKent Gibson */ 85465cff704SKent Gibson value = READ_ONCE(line->level); 85565cff704SKent Gibson 85665cff704SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags)) 85765cff704SKent Gibson value = !value; 85865cff704SKent Gibson 85965cff704SKent Gibson return value; 86065cff704SKent Gibson } 86165cff704SKent Gibson 86265cff704SKent Gibson static irqreturn_t debounce_irq_handler(int irq, void *p) 86365cff704SKent Gibson { 86465cff704SKent Gibson struct line *line = p; 86565cff704SKent Gibson 86665cff704SKent Gibson mod_delayed_work(system_wq, &line->work, 86765cff704SKent Gibson usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us))); 86865cff704SKent Gibson 86965cff704SKent Gibson return IRQ_HANDLED; 87065cff704SKent Gibson } 87165cff704SKent Gibson 87265cff704SKent Gibson static void debounce_work_func(struct work_struct *work) 87365cff704SKent Gibson { 87465cff704SKent Gibson struct gpio_v2_line_event le; 87565cff704SKent Gibson struct line *line = container_of(work, struct line, work.work); 87665cff704SKent Gibson struct linereq *lr; 877b1a92e94SKent Gibson u64 eflags, edflags = READ_ONCE(line->edflags); 878272ddba0SKent Gibson int level = -1; 879272ddba0SKent Gibson #ifdef CONFIG_HTE 880272ddba0SKent Gibson int diff_seqno; 88165cff704SKent Gibson 882b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) 8832068339aSDipen Patel level = line->raw_level; 884272ddba0SKent Gibson #endif 8852068339aSDipen Patel if (level < 0) 88665cff704SKent Gibson level = gpiod_get_raw_value_cansleep(line->desc); 88765cff704SKent Gibson if (level < 0) { 88865cff704SKent Gibson pr_debug_ratelimited("debouncer failed to read line value\n"); 88965cff704SKent Gibson return; 89065cff704SKent Gibson } 89165cff704SKent Gibson 89265cff704SKent Gibson if (READ_ONCE(line->level) == level) 89365cff704SKent Gibson return; 89465cff704SKent Gibson 89565cff704SKent Gibson WRITE_ONCE(line->level, level); 89665cff704SKent Gibson 89765cff704SKent Gibson /* -- edge detection -- */ 898b1a92e94SKent Gibson eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS; 8993ffb7c45SKent Gibson if (!eflags) 90065cff704SKent Gibson return; 90165cff704SKent Gibson 90265cff704SKent Gibson /* switch from physical level to logical - if they differ */ 903b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 90465cff704SKent Gibson level = !level; 90565cff704SKent Gibson 90665cff704SKent Gibson /* ignore edges that are not being monitored */ 9073ffb7c45SKent Gibson if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) || 9083ffb7c45SKent Gibson ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level)) 90965cff704SKent Gibson return; 91065cff704SKent Gibson 91165cff704SKent Gibson /* Do not leak kernel stack to userspace */ 91265cff704SKent Gibson memset(&le, 0, sizeof(le)); 91365cff704SKent Gibson 91465cff704SKent Gibson lr = line->req; 91526d060e4SKent Gibson le.timestamp_ns = line_event_timestamp(line); 91665cff704SKent Gibson le.offset = gpio_chip_hwgpio(line->desc); 917272ddba0SKent Gibson #ifdef CONFIG_HTE 918b1a92e94SKent Gibson if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) { 9192068339aSDipen Patel /* discard events except the last one */ 9202068339aSDipen Patel line->total_discard_seq -= 1; 9212068339aSDipen Patel diff_seqno = line->last_seqno - line->total_discard_seq - 9222068339aSDipen Patel line->line_seqno; 9232068339aSDipen Patel line->line_seqno = line->last_seqno - line->total_discard_seq; 9242068339aSDipen Patel le.line_seqno = line->line_seqno; 9252068339aSDipen Patel le.seqno = (lr->num_lines == 1) ? 9262068339aSDipen Patel le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno); 927272ddba0SKent Gibson } else 928272ddba0SKent Gibson #endif /* CONFIG_HTE */ 929272ddba0SKent Gibson { 93065cff704SKent Gibson line->line_seqno++; 93165cff704SKent Gibson le.line_seqno = line->line_seqno; 93265cff704SKent Gibson le.seqno = (lr->num_lines == 1) ? 93365cff704SKent Gibson le.line_seqno : atomic_inc_return(&lr->seqno); 9342068339aSDipen Patel } 93565cff704SKent Gibson 93624220232SKent Gibson le.id = line_event_id(level); 93765cff704SKent Gibson 93865cff704SKent Gibson linereq_put_event(lr, &le); 93965cff704SKent Gibson } 94065cff704SKent Gibson 941b1a92e94SKent Gibson static int debounce_setup(struct line *line, unsigned int debounce_period_us) 94265cff704SKent Gibson { 94365cff704SKent Gibson unsigned long irqflags; 94465cff704SKent Gibson int ret, level, irq; 94565cff704SKent Gibson 94665cff704SKent Gibson /* try hardware */ 94765cff704SKent Gibson ret = gpiod_set_debounce(line->desc, debounce_period_us); 94865cff704SKent Gibson if (!ret) { 94965cff704SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 95065cff704SKent Gibson return ret; 95165cff704SKent Gibson } 95265cff704SKent Gibson if (ret != -ENOTSUPP) 95365cff704SKent Gibson return ret; 95465cff704SKent Gibson 95565cff704SKent Gibson if (debounce_period_us) { 95665cff704SKent Gibson /* setup software debounce */ 95765cff704SKent Gibson level = gpiod_get_raw_value_cansleep(line->desc); 95865cff704SKent Gibson if (level < 0) 95965cff704SKent Gibson return level; 96065cff704SKent Gibson 961272ddba0SKent Gibson if (!(IS_ENABLED(CONFIG_HTE) && 962272ddba0SKent Gibson test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))) { 96365cff704SKent Gibson irq = gpiod_to_irq(line->desc); 96465cff704SKent Gibson if (irq < 0) 96565cff704SKent Gibson return -ENXIO; 96665cff704SKent Gibson 96765cff704SKent Gibson irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING; 96865cff704SKent Gibson ret = request_irq(irq, debounce_irq_handler, irqflags, 96965cff704SKent Gibson line->req->label, line); 97065cff704SKent Gibson if (ret) 97165cff704SKent Gibson return ret; 97265cff704SKent Gibson line->irq = irq; 9732068339aSDipen Patel } else { 9742487a812SKent Gibson ret = hte_edge_setup(line, GPIO_V2_LINE_FLAG_EDGE_BOTH); 9752068339aSDipen Patel if (ret) 9762068339aSDipen Patel return ret; 9772068339aSDipen Patel } 9782068339aSDipen Patel 9792068339aSDipen Patel WRITE_ONCE(line->level, level); 9802068339aSDipen Patel WRITE_ONCE(line->sw_debounced, 1); 98165cff704SKent Gibson } 98265cff704SKent Gibson return 0; 98365cff704SKent Gibson } 98465cff704SKent Gibson 98565cff704SKent Gibson static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc, 98665cff704SKent Gibson unsigned int line_idx) 98765cff704SKent Gibson { 98865cff704SKent Gibson unsigned int i; 98965cff704SKent Gibson u64 mask = BIT_ULL(line_idx); 99065cff704SKent Gibson 99165cff704SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 99265cff704SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && 99365cff704SKent Gibson (lc->attrs[i].mask & mask)) 99465cff704SKent Gibson return true; 99565cff704SKent Gibson } 99665cff704SKent Gibson return false; 99765cff704SKent Gibson } 99865cff704SKent Gibson 99965cff704SKent Gibson static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc, 100065cff704SKent Gibson unsigned int line_idx) 100165cff704SKent Gibson { 100265cff704SKent Gibson unsigned int i; 100365cff704SKent Gibson u64 mask = BIT_ULL(line_idx); 100465cff704SKent Gibson 100565cff704SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 100665cff704SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && 100765cff704SKent Gibson (lc->attrs[i].mask & mask)) 100865cff704SKent Gibson return lc->attrs[i].attr.debounce_period_us; 100965cff704SKent Gibson } 101065cff704SKent Gibson return 0; 101165cff704SKent Gibson } 101265cff704SKent Gibson 1013b1a92e94SKent Gibson static void edge_detector_stop(struct line *line) 101473e03419SKent Gibson { 1015b1a92e94SKent Gibson if (line->irq) { 101673e03419SKent Gibson free_irq(line->irq, line); 101773e03419SKent Gibson line->irq = 0; 101873e03419SKent Gibson } 1019a54756cbSKent Gibson 1020272ddba0SKent Gibson #ifdef CONFIG_HTE 1021b1a92e94SKent Gibson if (READ_ONCE(line->edflags) & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) 10222068339aSDipen Patel hte_ts_put(&line->hdesc); 1023272ddba0SKent Gibson #endif 10242068339aSDipen Patel 102565cff704SKent Gibson cancel_delayed_work_sync(&line->work); 102665cff704SKent Gibson WRITE_ONCE(line->sw_debounced, 0); 1027b1a92e94SKent Gibson WRITE_ONCE(line->edflags, 0); 102803a58ea5SKent Gibson if (line->desc) 102903a58ea5SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, 0); 103065cff704SKent Gibson /* do not change line->level - see comment in debounced_value() */ 103173e03419SKent Gibson } 103273e03419SKent Gibson 103373e03419SKent Gibson static int edge_detector_setup(struct line *line, 103465cff704SKent Gibson struct gpio_v2_line_config *lc, 1035b1a92e94SKent Gibson unsigned int line_idx, u64 edflags) 103673e03419SKent Gibson { 103765cff704SKent Gibson u32 debounce_period_us; 103873e03419SKent Gibson unsigned long irqflags = 0; 1039b1a92e94SKent Gibson u64 eflags; 104073e03419SKent Gibson int irq, ret; 104173e03419SKent Gibson 1042b1a92e94SKent Gibson eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS; 104373e03419SKent Gibson if (eflags && !kfifo_initialized(&line->req->events)) { 104473e03419SKent Gibson ret = kfifo_alloc(&line->req->events, 104573e03419SKent Gibson line->req->event_buffer_size, GFP_KERNEL); 104673e03419SKent Gibson if (ret) 104773e03419SKent Gibson return ret; 104873e03419SKent Gibson } 104965cff704SKent Gibson if (gpio_v2_line_config_debounced(lc, line_idx)) { 105065cff704SKent Gibson debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx); 1051b1a92e94SKent Gibson ret = debounce_setup(line, debounce_period_us); 105265cff704SKent Gibson if (ret) 105365cff704SKent Gibson return ret; 105465cff704SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 105565cff704SKent Gibson } 105673e03419SKent Gibson 105765cff704SKent Gibson /* detection disabled or sw debouncer will provide edge detection */ 105865cff704SKent Gibson if (!eflags || READ_ONCE(line->sw_debounced)) 105973e03419SKent Gibson return 0; 106073e03419SKent Gibson 1061272ddba0SKent Gibson if (IS_ENABLED(CONFIG_HTE) && 1062272ddba0SKent Gibson (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)) 1063b1a92e94SKent Gibson return hte_edge_setup(line, edflags); 10642068339aSDipen Patel 106573e03419SKent Gibson irq = gpiod_to_irq(line->desc); 106673e03419SKent Gibson if (irq < 0) 106773e03419SKent Gibson return -ENXIO; 106873e03419SKent Gibson 106973e03419SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) 107073e03419SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 107173e03419SKent Gibson IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 107273e03419SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) 107373e03419SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 107473e03419SKent Gibson IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 107573e03419SKent Gibson irqflags |= IRQF_ONESHOT; 107673e03419SKent Gibson 107773e03419SKent Gibson /* Request a thread to read the events */ 107873e03419SKent Gibson ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread, 107973e03419SKent Gibson irqflags, line->req->label, line); 108073e03419SKent Gibson if (ret) 108173e03419SKent Gibson return ret; 108273e03419SKent Gibson 108373e03419SKent Gibson line->irq = irq; 108473e03419SKent Gibson return 0; 108573e03419SKent Gibson } 108673e03419SKent Gibson 108765cff704SKent Gibson static int edge_detector_update(struct line *line, 108865cff704SKent Gibson struct gpio_v2_line_config *lc, 1089b1a92e94SKent Gibson unsigned int line_idx, u64 edflags) 1090a54756cbSKent Gibson { 1091b1a92e94SKent Gibson u64 active_edflags = READ_ONCE(line->edflags); 109265cff704SKent Gibson unsigned int debounce_period_us = 109365cff704SKent Gibson gpio_v2_line_config_debounce_period(lc, line_idx); 109465cff704SKent Gibson 1095b1a92e94SKent Gibson if ((active_edflags == edflags) && 1096b1a92e94SKent Gibson (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us)) 1097a54756cbSKent Gibson return 0; 1098a54756cbSKent Gibson 109965cff704SKent Gibson /* sw debounced and still will be...*/ 110065cff704SKent Gibson if (debounce_period_us && READ_ONCE(line->sw_debounced)) { 110165cff704SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 110265cff704SKent Gibson return 0; 110365cff704SKent Gibson } 110465cff704SKent Gibson 110565cff704SKent Gibson /* reconfiguring edge detection or sw debounce being disabled */ 1106b1a92e94SKent Gibson if ((line->irq && !READ_ONCE(line->sw_debounced)) || 1107b1a92e94SKent Gibson (active_edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) || 110865cff704SKent Gibson (!debounce_period_us && READ_ONCE(line->sw_debounced))) 1109b1a92e94SKent Gibson edge_detector_stop(line); 1110a54756cbSKent Gibson 1111b1a92e94SKent Gibson return edge_detector_setup(line, lc, line_idx, edflags); 1112a54756cbSKent Gibson } 1113a54756cbSKent Gibson 11143c0d9c63SKent Gibson static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, 11153c0d9c63SKent Gibson unsigned int line_idx) 11163c0d9c63SKent Gibson { 11173c0d9c63SKent Gibson unsigned int i; 11183c0d9c63SKent Gibson u64 mask = BIT_ULL(line_idx); 11193c0d9c63SKent Gibson 11203c0d9c63SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 11213c0d9c63SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) && 11223c0d9c63SKent Gibson (lc->attrs[i].mask & mask)) 11233c0d9c63SKent Gibson return lc->attrs[i].attr.flags; 11243c0d9c63SKent Gibson } 11253c0d9c63SKent Gibson return lc->flags; 11263c0d9c63SKent Gibson } 11273c0d9c63SKent Gibson 11283c0d9c63SKent Gibson static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc, 11293c0d9c63SKent Gibson unsigned int line_idx) 11303c0d9c63SKent Gibson { 11313c0d9c63SKent Gibson unsigned int i; 11323c0d9c63SKent Gibson u64 mask = BIT_ULL(line_idx); 11333c0d9c63SKent Gibson 11343c0d9c63SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 11353c0d9c63SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) && 11363c0d9c63SKent Gibson (lc->attrs[i].mask & mask)) 11373c0d9c63SKent Gibson return !!(lc->attrs[i].attr.values & mask); 11383c0d9c63SKent Gibson } 11393c0d9c63SKent Gibson return 0; 11403c0d9c63SKent Gibson } 11413c0d9c63SKent Gibson 11423c0d9c63SKent Gibson static int gpio_v2_line_flags_validate(u64 flags) 11433c0d9c63SKent Gibson { 11443c0d9c63SKent Gibson /* Return an error if an unknown flag is set */ 11453c0d9c63SKent Gibson if (flags & ~GPIO_V2_LINE_VALID_FLAGS) 11463c0d9c63SKent Gibson return -EINVAL; 1147272ddba0SKent Gibson 1148272ddba0SKent Gibson if (!IS_ENABLED(CONFIG_HTE) && 1149272ddba0SKent Gibson (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)) 1150272ddba0SKent Gibson return -EOPNOTSUPP; 1151272ddba0SKent Gibson 11523c0d9c63SKent Gibson /* 11533c0d9c63SKent Gibson * Do not allow both INPUT and OUTPUT flags to be set as they are 11543c0d9c63SKent Gibson * contradictory. 11553c0d9c63SKent Gibson */ 11563c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_FLAG_INPUT) && 11573c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_OUTPUT)) 11583c0d9c63SKent Gibson return -EINVAL; 11593c0d9c63SKent Gibson 11602068339aSDipen Patel /* Only allow one event clock source */ 1161272ddba0SKent Gibson if (IS_ENABLED(CONFIG_HTE) && 1162272ddba0SKent Gibson (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) && 11632068339aSDipen Patel (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)) 11642068339aSDipen Patel return -EINVAL; 11652068339aSDipen Patel 116673e03419SKent Gibson /* Edge detection requires explicit input. */ 116773e03419SKent Gibson if ((flags & GPIO_V2_LINE_EDGE_FLAGS) && 116873e03419SKent Gibson !(flags & GPIO_V2_LINE_FLAG_INPUT)) 116973e03419SKent Gibson return -EINVAL; 117073e03419SKent Gibson 11713c0d9c63SKent Gibson /* 11723c0d9c63SKent Gibson * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single 11733c0d9c63SKent Gibson * request. If the hardware actually supports enabling both at the 11743c0d9c63SKent Gibson * same time the electrical result would be disastrous. 11753c0d9c63SKent Gibson */ 11763c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) && 11773c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE)) 11783c0d9c63SKent Gibson return -EINVAL; 11793c0d9c63SKent Gibson 11803c0d9c63SKent Gibson /* Drive requires explicit output direction. */ 11813c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) && 11823c0d9c63SKent Gibson !(flags & GPIO_V2_LINE_FLAG_OUTPUT)) 11833c0d9c63SKent Gibson return -EINVAL; 11843c0d9c63SKent Gibson 11853c0d9c63SKent Gibson /* Bias requires explicit direction. */ 11863c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_BIAS_FLAGS) && 11873c0d9c63SKent Gibson !(flags & GPIO_V2_LINE_DIRECTION_FLAGS)) 11883c0d9c63SKent Gibson return -EINVAL; 11893c0d9c63SKent Gibson 11903c0d9c63SKent Gibson /* Only one bias flag can be set. */ 11913c0d9c63SKent Gibson if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) && 11923c0d9c63SKent Gibson (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | 11933c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) || 11943c0d9c63SKent Gibson ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) && 11953c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) 11963c0d9c63SKent Gibson return -EINVAL; 11973c0d9c63SKent Gibson 11983c0d9c63SKent Gibson return 0; 11993c0d9c63SKent Gibson } 12003c0d9c63SKent Gibson 12013c0d9c63SKent Gibson static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, 12023c0d9c63SKent Gibson unsigned int num_lines) 12033c0d9c63SKent Gibson { 12043c0d9c63SKent Gibson unsigned int i; 12053c0d9c63SKent Gibson u64 flags; 12063c0d9c63SKent Gibson int ret; 12073c0d9c63SKent Gibson 12083c0d9c63SKent Gibson if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX) 12093c0d9c63SKent Gibson return -EINVAL; 12103c0d9c63SKent Gibson 12113c0d9c63SKent Gibson if (memchr_inv(lc->padding, 0, sizeof(lc->padding))) 12123c0d9c63SKent Gibson return -EINVAL; 12133c0d9c63SKent Gibson 12143c0d9c63SKent Gibson for (i = 0; i < num_lines; i++) { 12153c0d9c63SKent Gibson flags = gpio_v2_line_config_flags(lc, i); 12163c0d9c63SKent Gibson ret = gpio_v2_line_flags_validate(flags); 12173c0d9c63SKent Gibson if (ret) 12183c0d9c63SKent Gibson return ret; 121965cff704SKent Gibson 122065cff704SKent Gibson /* debounce requires explicit input */ 122165cff704SKent Gibson if (gpio_v2_line_config_debounced(lc, i) && 122265cff704SKent Gibson !(flags & GPIO_V2_LINE_FLAG_INPUT)) 122365cff704SKent Gibson return -EINVAL; 12243c0d9c63SKent Gibson } 12253c0d9c63SKent Gibson return 0; 12263c0d9c63SKent Gibson } 12273c0d9c63SKent Gibson 12283c0d9c63SKent Gibson static void gpio_v2_line_config_flags_to_desc_flags(u64 flags, 12293c0d9c63SKent Gibson unsigned long *flagsp) 12303c0d9c63SKent Gibson { 12313c0d9c63SKent Gibson assign_bit(FLAG_ACTIVE_LOW, flagsp, 12323c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW); 12333c0d9c63SKent Gibson 12343c0d9c63SKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) 12353c0d9c63SKent Gibson set_bit(FLAG_IS_OUT, flagsp); 12363c0d9c63SKent Gibson else if (flags & GPIO_V2_LINE_FLAG_INPUT) 12373c0d9c63SKent Gibson clear_bit(FLAG_IS_OUT, flagsp); 12383c0d9c63SKent Gibson 123973e03419SKent Gibson assign_bit(FLAG_EDGE_RISING, flagsp, 124073e03419SKent Gibson flags & GPIO_V2_LINE_FLAG_EDGE_RISING); 124173e03419SKent Gibson assign_bit(FLAG_EDGE_FALLING, flagsp, 124273e03419SKent Gibson flags & GPIO_V2_LINE_FLAG_EDGE_FALLING); 124373e03419SKent Gibson 12443c0d9c63SKent Gibson assign_bit(FLAG_OPEN_DRAIN, flagsp, 12453c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN); 12463c0d9c63SKent Gibson assign_bit(FLAG_OPEN_SOURCE, flagsp, 12473c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE); 12483c0d9c63SKent Gibson 12493c0d9c63SKent Gibson assign_bit(FLAG_PULL_UP, flagsp, 12503c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP); 12513c0d9c63SKent Gibson assign_bit(FLAG_PULL_DOWN, flagsp, 12523c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN); 12533c0d9c63SKent Gibson assign_bit(FLAG_BIAS_DISABLE, flagsp, 12543c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED); 125526d060e4SKent Gibson 125626d060e4SKent Gibson assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp, 125726d060e4SKent Gibson flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME); 12582068339aSDipen Patel assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp, 12592068339aSDipen Patel flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE); 12603c0d9c63SKent Gibson } 12613c0d9c63SKent Gibson 12623c0d9c63SKent Gibson static long linereq_get_values(struct linereq *lr, void __user *ip) 12633c0d9c63SKent Gibson { 12643c0d9c63SKent Gibson struct gpio_v2_line_values lv; 12653c0d9c63SKent Gibson DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); 12663c0d9c63SKent Gibson struct gpio_desc **descs; 12673c0d9c63SKent Gibson unsigned int i, didx, num_get; 126865cff704SKent Gibson bool val; 12693c0d9c63SKent Gibson int ret; 12703c0d9c63SKent Gibson 12713c0d9c63SKent Gibson /* NOTE: It's ok to read values of output lines. */ 12723c0d9c63SKent Gibson if (copy_from_user(&lv, ip, sizeof(lv))) 12733c0d9c63SKent Gibson return -EFAULT; 12743c0d9c63SKent Gibson 12753c0d9c63SKent Gibson for (num_get = 0, i = 0; i < lr->num_lines; i++) { 12763c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 12773c0d9c63SKent Gibson num_get++; 12783c0d9c63SKent Gibson descs = &lr->lines[i].desc; 12793c0d9c63SKent Gibson } 12803c0d9c63SKent Gibson } 12813c0d9c63SKent Gibson 12823c0d9c63SKent Gibson if (num_get == 0) 12833c0d9c63SKent Gibson return -EINVAL; 12843c0d9c63SKent Gibson 12853c0d9c63SKent Gibson if (num_get != 1) { 12863c0d9c63SKent Gibson descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL); 12873c0d9c63SKent Gibson if (!descs) 12883c0d9c63SKent Gibson return -ENOMEM; 12893c0d9c63SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 12903c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 12913c0d9c63SKent Gibson descs[didx] = lr->lines[i].desc; 12923c0d9c63SKent Gibson didx++; 12933c0d9c63SKent Gibson } 12943c0d9c63SKent Gibson } 12953c0d9c63SKent Gibson } 12963c0d9c63SKent Gibson ret = gpiod_get_array_value_complex(false, true, num_get, 12973c0d9c63SKent Gibson descs, NULL, vals); 12983c0d9c63SKent Gibson 12993c0d9c63SKent Gibson if (num_get != 1) 13003c0d9c63SKent Gibson kfree(descs); 13013c0d9c63SKent Gibson if (ret) 13023c0d9c63SKent Gibson return ret; 13033c0d9c63SKent Gibson 13043c0d9c63SKent Gibson lv.bits = 0; 13053c0d9c63SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 13063c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 130765cff704SKent Gibson if (lr->lines[i].sw_debounced) 130865cff704SKent Gibson val = debounced_value(&lr->lines[i]); 130965cff704SKent Gibson else 131065cff704SKent Gibson val = test_bit(didx, vals); 131165cff704SKent Gibson if (val) 13123c0d9c63SKent Gibson lv.bits |= BIT_ULL(i); 13133c0d9c63SKent Gibson didx++; 13143c0d9c63SKent Gibson } 13153c0d9c63SKent Gibson } 13163c0d9c63SKent Gibson 13173c0d9c63SKent Gibson if (copy_to_user(ip, &lv, sizeof(lv))) 13183c0d9c63SKent Gibson return -EFAULT; 13193c0d9c63SKent Gibson 13203c0d9c63SKent Gibson return 0; 13213c0d9c63SKent Gibson } 13223c0d9c63SKent Gibson 13237b8e00d9SKent Gibson static long linereq_set_values_unlocked(struct linereq *lr, 13247b8e00d9SKent Gibson struct gpio_v2_line_values *lv) 13257b8e00d9SKent Gibson { 13267b8e00d9SKent Gibson DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); 13277b8e00d9SKent Gibson struct gpio_desc **descs; 13287b8e00d9SKent Gibson unsigned int i, didx, num_set; 13297b8e00d9SKent Gibson int ret; 13307b8e00d9SKent Gibson 13317b8e00d9SKent Gibson bitmap_zero(vals, GPIO_V2_LINES_MAX); 13327b8e00d9SKent Gibson for (num_set = 0, i = 0; i < lr->num_lines; i++) { 13337b8e00d9SKent Gibson if (lv->mask & BIT_ULL(i)) { 13347b8e00d9SKent Gibson if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags)) 13357b8e00d9SKent Gibson return -EPERM; 13367b8e00d9SKent Gibson if (lv->bits & BIT_ULL(i)) 13377b8e00d9SKent Gibson __set_bit(num_set, vals); 13387b8e00d9SKent Gibson num_set++; 13397b8e00d9SKent Gibson descs = &lr->lines[i].desc; 13407b8e00d9SKent Gibson } 13417b8e00d9SKent Gibson } 13427b8e00d9SKent Gibson if (num_set == 0) 13437b8e00d9SKent Gibson return -EINVAL; 13447b8e00d9SKent Gibson 13457b8e00d9SKent Gibson if (num_set != 1) { 13467b8e00d9SKent Gibson /* build compacted desc array and values */ 13477b8e00d9SKent Gibson descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL); 13487b8e00d9SKent Gibson if (!descs) 13497b8e00d9SKent Gibson return -ENOMEM; 13507b8e00d9SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 13517b8e00d9SKent Gibson if (lv->mask & BIT_ULL(i)) { 13527b8e00d9SKent Gibson descs[didx] = lr->lines[i].desc; 13537b8e00d9SKent Gibson didx++; 13547b8e00d9SKent Gibson } 13557b8e00d9SKent Gibson } 13567b8e00d9SKent Gibson } 13577b8e00d9SKent Gibson ret = gpiod_set_array_value_complex(false, true, num_set, 13587b8e00d9SKent Gibson descs, NULL, vals); 13597b8e00d9SKent Gibson 13607b8e00d9SKent Gibson if (num_set != 1) 13617b8e00d9SKent Gibson kfree(descs); 13627b8e00d9SKent Gibson return ret; 13637b8e00d9SKent Gibson } 13647b8e00d9SKent Gibson 13657b8e00d9SKent Gibson static long linereq_set_values(struct linereq *lr, void __user *ip) 13667b8e00d9SKent Gibson { 13677b8e00d9SKent Gibson struct gpio_v2_line_values lv; 13687b8e00d9SKent Gibson int ret; 13697b8e00d9SKent Gibson 13707b8e00d9SKent Gibson if (copy_from_user(&lv, ip, sizeof(lv))) 13717b8e00d9SKent Gibson return -EFAULT; 13727b8e00d9SKent Gibson 13737b8e00d9SKent Gibson mutex_lock(&lr->config_mutex); 13747b8e00d9SKent Gibson 13757b8e00d9SKent Gibson ret = linereq_set_values_unlocked(lr, &lv); 13767b8e00d9SKent Gibson 13777b8e00d9SKent Gibson mutex_unlock(&lr->config_mutex); 13787b8e00d9SKent Gibson 13797b8e00d9SKent Gibson return ret; 13807b8e00d9SKent Gibson } 13817b8e00d9SKent Gibson 1382a54756cbSKent Gibson static long linereq_set_config_unlocked(struct linereq *lr, 1383a54756cbSKent Gibson struct gpio_v2_line_config *lc) 1384a54756cbSKent Gibson { 1385a54756cbSKent Gibson struct gpio_desc *desc; 1386b1a92e94SKent Gibson struct line *line; 1387a54756cbSKent Gibson unsigned int i; 1388b1a92e94SKent Gibson u64 flags, edflags; 1389a54756cbSKent Gibson int ret; 1390a54756cbSKent Gibson 1391a54756cbSKent Gibson for (i = 0; i < lr->num_lines; i++) { 1392b1a92e94SKent Gibson line = &lr->lines[i]; 1393a54756cbSKent Gibson desc = lr->lines[i].desc; 1394a54756cbSKent Gibson flags = gpio_v2_line_config_flags(lc, i); 1395a54756cbSKent Gibson gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 1396b1a92e94SKent Gibson edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS; 1397a54756cbSKent Gibson /* 1398a54756cbSKent Gibson * Lines have to be requested explicitly for input 1399a54756cbSKent Gibson * or output, else the line will be treated "as is". 1400a54756cbSKent Gibson */ 1401a54756cbSKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 1402a54756cbSKent Gibson int val = gpio_v2_line_config_output_value(lc, i); 1403a54756cbSKent Gibson 1404b1a92e94SKent Gibson edge_detector_stop(line); 1405a54756cbSKent Gibson ret = gpiod_direction_output(desc, val); 1406a54756cbSKent Gibson if (ret) 1407a54756cbSKent Gibson return ret; 1408a54756cbSKent Gibson } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { 1409a54756cbSKent Gibson ret = gpiod_direction_input(desc); 1410a54756cbSKent Gibson if (ret) 1411a54756cbSKent Gibson return ret; 1412a54756cbSKent Gibson 1413b1a92e94SKent Gibson ret = edge_detector_update(line, lc, i, edflags); 1414a54756cbSKent Gibson if (ret) 1415a54756cbSKent Gibson return ret; 1416a54756cbSKent Gibson } 1417a54756cbSKent Gibson 1418b1a92e94SKent Gibson WRITE_ONCE(line->edflags, edflags); 1419b1a92e94SKent Gibson 1420*9ce4ed5bSBartosz Golaszewski gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); 1421a54756cbSKent Gibson } 1422a54756cbSKent Gibson return 0; 1423a54756cbSKent Gibson } 1424a54756cbSKent Gibson 1425a54756cbSKent Gibson static long linereq_set_config(struct linereq *lr, void __user *ip) 1426a54756cbSKent Gibson { 1427a54756cbSKent Gibson struct gpio_v2_line_config lc; 1428a54756cbSKent Gibson int ret; 1429a54756cbSKent Gibson 1430a54756cbSKent Gibson if (copy_from_user(&lc, ip, sizeof(lc))) 1431a54756cbSKent Gibson return -EFAULT; 1432a54756cbSKent Gibson 1433a54756cbSKent Gibson ret = gpio_v2_line_config_validate(&lc, lr->num_lines); 1434a54756cbSKent Gibson if (ret) 1435a54756cbSKent Gibson return ret; 1436a54756cbSKent Gibson 1437a54756cbSKent Gibson mutex_lock(&lr->config_mutex); 1438a54756cbSKent Gibson 1439a54756cbSKent Gibson ret = linereq_set_config_unlocked(lr, &lc); 1440a54756cbSKent Gibson 1441a54756cbSKent Gibson mutex_unlock(&lr->config_mutex); 1442a54756cbSKent Gibson 1443a54756cbSKent Gibson return ret; 1444a54756cbSKent Gibson } 1445a54756cbSKent Gibson 1446bdbbae24SBartosz Golaszewski static long linereq_ioctl_unlocked(struct file *file, unsigned int cmd, 14473c0d9c63SKent Gibson unsigned long arg) 14483c0d9c63SKent Gibson { 14493c0d9c63SKent Gibson struct linereq *lr = file->private_data; 14503c0d9c63SKent Gibson void __user *ip = (void __user *)arg; 14513c0d9c63SKent Gibson 1452533aae7cSBartosz Golaszewski if (!lr->gdev->chip) 1453533aae7cSBartosz Golaszewski return -ENODEV; 1454533aae7cSBartosz Golaszewski 14551cef8b50SAndy Shevchenko switch (cmd) { 14561cef8b50SAndy Shevchenko case GPIO_V2_LINE_GET_VALUES_IOCTL: 14573c0d9c63SKent Gibson return linereq_get_values(lr, ip); 14581cef8b50SAndy Shevchenko case GPIO_V2_LINE_SET_VALUES_IOCTL: 14597b8e00d9SKent Gibson return linereq_set_values(lr, ip); 14601cef8b50SAndy Shevchenko case GPIO_V2_LINE_SET_CONFIG_IOCTL: 1461a54756cbSKent Gibson return linereq_set_config(lr, ip); 14621cef8b50SAndy Shevchenko default: 14633c0d9c63SKent Gibson return -EINVAL; 14643c0d9c63SKent Gibson } 14651cef8b50SAndy Shevchenko } 14663c0d9c63SKent Gibson 1467bdbbae24SBartosz Golaszewski static long linereq_ioctl(struct file *file, unsigned int cmd, 1468bdbbae24SBartosz Golaszewski unsigned long arg) 1469bdbbae24SBartosz Golaszewski { 1470bdbbae24SBartosz Golaszewski struct linereq *lr = file->private_data; 1471bdbbae24SBartosz Golaszewski 1472bdbbae24SBartosz Golaszewski return call_ioctl_locked(file, cmd, arg, lr->gdev, 1473bdbbae24SBartosz Golaszewski linereq_ioctl_unlocked); 1474bdbbae24SBartosz Golaszewski } 1475bdbbae24SBartosz Golaszewski 14763c0d9c63SKent Gibson #ifdef CONFIG_COMPAT 14773c0d9c63SKent Gibson static long linereq_ioctl_compat(struct file *file, unsigned int cmd, 14783c0d9c63SKent Gibson unsigned long arg) 14793c0d9c63SKent Gibson { 14803c0d9c63SKent Gibson return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 14813c0d9c63SKent Gibson } 14823c0d9c63SKent Gibson #endif 14833c0d9c63SKent Gibson 1484bdbbae24SBartosz Golaszewski static __poll_t linereq_poll_unlocked(struct file *file, 148573e03419SKent Gibson struct poll_table_struct *wait) 148673e03419SKent Gibson { 148773e03419SKent Gibson struct linereq *lr = file->private_data; 148873e03419SKent Gibson __poll_t events = 0; 148973e03419SKent Gibson 1490533aae7cSBartosz Golaszewski if (!lr->gdev->chip) 1491533aae7cSBartosz Golaszewski return EPOLLHUP | EPOLLERR; 1492533aae7cSBartosz Golaszewski 149373e03419SKent Gibson poll_wait(file, &lr->wait, wait); 149473e03419SKent Gibson 149573e03419SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events, 149673e03419SKent Gibson &lr->wait.lock)) 149773e03419SKent Gibson events = EPOLLIN | EPOLLRDNORM; 149873e03419SKent Gibson 149973e03419SKent Gibson return events; 150073e03419SKent Gibson } 150173e03419SKent Gibson 1502bdbbae24SBartosz Golaszewski static __poll_t linereq_poll(struct file *file, 1503bdbbae24SBartosz Golaszewski struct poll_table_struct *wait) 1504bdbbae24SBartosz Golaszewski { 1505bdbbae24SBartosz Golaszewski struct linereq *lr = file->private_data; 1506bdbbae24SBartosz Golaszewski 1507bdbbae24SBartosz Golaszewski return call_poll_locked(file, wait, lr->gdev, linereq_poll_unlocked); 1508bdbbae24SBartosz Golaszewski } 1509bdbbae24SBartosz Golaszewski 1510bdbbae24SBartosz Golaszewski static ssize_t linereq_read_unlocked(struct file *file, char __user *buf, 1511bdbbae24SBartosz Golaszewski size_t count, loff_t *f_ps) 151273e03419SKent Gibson { 151373e03419SKent Gibson struct linereq *lr = file->private_data; 151473e03419SKent Gibson struct gpio_v2_line_event le; 151573e03419SKent Gibson ssize_t bytes_read = 0; 151673e03419SKent Gibson int ret; 151773e03419SKent Gibson 1518533aae7cSBartosz Golaszewski if (!lr->gdev->chip) 1519533aae7cSBartosz Golaszewski return -ENODEV; 1520533aae7cSBartosz Golaszewski 152173e03419SKent Gibson if (count < sizeof(le)) 152273e03419SKent Gibson return -EINVAL; 152373e03419SKent Gibson 152473e03419SKent Gibson do { 152573e03419SKent Gibson spin_lock(&lr->wait.lock); 152673e03419SKent Gibson if (kfifo_is_empty(&lr->events)) { 152773e03419SKent Gibson if (bytes_read) { 152873e03419SKent Gibson spin_unlock(&lr->wait.lock); 152973e03419SKent Gibson return bytes_read; 153073e03419SKent Gibson } 153173e03419SKent Gibson 153273e03419SKent Gibson if (file->f_flags & O_NONBLOCK) { 153373e03419SKent Gibson spin_unlock(&lr->wait.lock); 153473e03419SKent Gibson return -EAGAIN; 153573e03419SKent Gibson } 153673e03419SKent Gibson 153773e03419SKent Gibson ret = wait_event_interruptible_locked(lr->wait, 153873e03419SKent Gibson !kfifo_is_empty(&lr->events)); 153973e03419SKent Gibson if (ret) { 154073e03419SKent Gibson spin_unlock(&lr->wait.lock); 154173e03419SKent Gibson return ret; 154273e03419SKent Gibson } 154373e03419SKent Gibson } 154473e03419SKent Gibson 154573e03419SKent Gibson ret = kfifo_out(&lr->events, &le, 1); 154673e03419SKent Gibson spin_unlock(&lr->wait.lock); 154773e03419SKent Gibson if (ret != 1) { 154873e03419SKent Gibson /* 154973e03419SKent Gibson * This should never happen - we were holding the 155073e03419SKent Gibson * lock from the moment we learned the fifo is no 155173e03419SKent Gibson * longer empty until now. 155273e03419SKent Gibson */ 155373e03419SKent Gibson ret = -EIO; 155473e03419SKent Gibson break; 155573e03419SKent Gibson } 155673e03419SKent Gibson 155773e03419SKent Gibson if (copy_to_user(buf + bytes_read, &le, sizeof(le))) 155873e03419SKent Gibson return -EFAULT; 155973e03419SKent Gibson bytes_read += sizeof(le); 156073e03419SKent Gibson } while (count >= bytes_read + sizeof(le)); 156173e03419SKent Gibson 156273e03419SKent Gibson return bytes_read; 156373e03419SKent Gibson } 156473e03419SKent Gibson 1565bdbbae24SBartosz Golaszewski static ssize_t linereq_read(struct file *file, char __user *buf, 1566bdbbae24SBartosz Golaszewski size_t count, loff_t *f_ps) 1567bdbbae24SBartosz Golaszewski { 1568bdbbae24SBartosz Golaszewski struct linereq *lr = file->private_data; 1569bdbbae24SBartosz Golaszewski 1570bdbbae24SBartosz Golaszewski return call_read_locked(file, buf, count, f_ps, lr->gdev, 1571bdbbae24SBartosz Golaszewski linereq_read_unlocked); 1572bdbbae24SBartosz Golaszewski } 1573bdbbae24SBartosz Golaszewski 15743c0d9c63SKent Gibson static void linereq_free(struct linereq *lr) 15753c0d9c63SKent Gibson { 15763c0d9c63SKent Gibson unsigned int i; 15773c0d9c63SKent Gibson 1578a0dda508SBartosz Golaszewski if (lr->device_unregistered_nb.notifier_call) 1579a0dda508SBartosz Golaszewski blocking_notifier_chain_unregister(&lr->gdev->device_notifier, 1580a0dda508SBartosz Golaszewski &lr->device_unregistered_nb); 1581a0dda508SBartosz Golaszewski 15823c0d9c63SKent Gibson for (i = 0; i < lr->num_lines; i++) { 1583160d6e40SKent Gibson if (lr->lines[i].desc) { 1584b1a92e94SKent Gibson edge_detector_stop(&lr->lines[i]); 15853c0d9c63SKent Gibson gpiod_free(lr->lines[i].desc); 15863c0d9c63SKent Gibson } 1587160d6e40SKent Gibson } 158873e03419SKent Gibson kfifo_free(&lr->events); 15893c0d9c63SKent Gibson kfree(lr->label); 1590dc0989e3SAndy Shevchenko gpio_device_put(lr->gdev); 15913c0d9c63SKent Gibson kfree(lr); 15923c0d9c63SKent Gibson } 15933c0d9c63SKent Gibson 15943c0d9c63SKent Gibson static int linereq_release(struct inode *inode, struct file *file) 15953c0d9c63SKent Gibson { 15963c0d9c63SKent Gibson struct linereq *lr = file->private_data; 15973c0d9c63SKent Gibson 15983c0d9c63SKent Gibson linereq_free(lr); 15993c0d9c63SKent Gibson return 0; 16003c0d9c63SKent Gibson } 16013c0d9c63SKent Gibson 16020ae3109aSBartosz Golaszewski #ifdef CONFIG_PROC_FS 16030ae3109aSBartosz Golaszewski static void linereq_show_fdinfo(struct seq_file *out, struct file *file) 16040ae3109aSBartosz Golaszewski { 16050ae3109aSBartosz Golaszewski struct linereq *lr = file->private_data; 16060ae3109aSBartosz Golaszewski struct device *dev = &lr->gdev->dev; 16070ae3109aSBartosz Golaszewski u16 i; 16080ae3109aSBartosz Golaszewski 16090ae3109aSBartosz Golaszewski seq_printf(out, "gpio-chip:\t%s\n", dev_name(dev)); 16100ae3109aSBartosz Golaszewski 16110ae3109aSBartosz Golaszewski for (i = 0; i < lr->num_lines; i++) 16120ae3109aSBartosz Golaszewski seq_printf(out, "gpio-line:\t%d\n", 16130ae3109aSBartosz Golaszewski gpio_chip_hwgpio(lr->lines[i].desc)); 16140ae3109aSBartosz Golaszewski } 16150ae3109aSBartosz Golaszewski #endif 16160ae3109aSBartosz Golaszewski 16173c0d9c63SKent Gibson static const struct file_operations line_fileops = { 16183c0d9c63SKent Gibson .release = linereq_release, 161973e03419SKent Gibson .read = linereq_read, 162073e03419SKent Gibson .poll = linereq_poll, 16213c0d9c63SKent Gibson .owner = THIS_MODULE, 16223c0d9c63SKent Gibson .llseek = noop_llseek, 16233c0d9c63SKent Gibson .unlocked_ioctl = linereq_ioctl, 16243c0d9c63SKent Gibson #ifdef CONFIG_COMPAT 16253c0d9c63SKent Gibson .compat_ioctl = linereq_ioctl_compat, 16263c0d9c63SKent Gibson #endif 16270ae3109aSBartosz Golaszewski #ifdef CONFIG_PROC_FS 16280ae3109aSBartosz Golaszewski .show_fdinfo = linereq_show_fdinfo, 16290ae3109aSBartosz Golaszewski #endif 16303c0d9c63SKent Gibson }; 16313c0d9c63SKent Gibson 16323c0d9c63SKent Gibson static int linereq_create(struct gpio_device *gdev, void __user *ip) 16333c0d9c63SKent Gibson { 16343c0d9c63SKent Gibson struct gpio_v2_line_request ulr; 16353c0d9c63SKent Gibson struct gpio_v2_line_config *lc; 16363c0d9c63SKent Gibson struct linereq *lr; 16373c0d9c63SKent Gibson struct file *file; 1638b1a92e94SKent Gibson u64 flags, edflags; 16393c0d9c63SKent Gibson unsigned int i; 16403c0d9c63SKent Gibson int fd, ret; 16413c0d9c63SKent Gibson 16423c0d9c63SKent Gibson if (copy_from_user(&ulr, ip, sizeof(ulr))) 16433c0d9c63SKent Gibson return -EFAULT; 16443c0d9c63SKent Gibson 16453c0d9c63SKent Gibson if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX)) 16463c0d9c63SKent Gibson return -EINVAL; 16473c0d9c63SKent Gibson 16483c0d9c63SKent Gibson if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding))) 16493c0d9c63SKent Gibson return -EINVAL; 16503c0d9c63SKent Gibson 16513c0d9c63SKent Gibson lc = &ulr.config; 16523c0d9c63SKent Gibson ret = gpio_v2_line_config_validate(lc, ulr.num_lines); 16533c0d9c63SKent Gibson if (ret) 16543c0d9c63SKent Gibson return ret; 16553c0d9c63SKent Gibson 16563c0d9c63SKent Gibson lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); 16573c0d9c63SKent Gibson if (!lr) 16583c0d9c63SKent Gibson return -ENOMEM; 16593c0d9c63SKent Gibson 1660dc0989e3SAndy Shevchenko lr->gdev = gpio_device_get(gdev); 16613c0d9c63SKent Gibson 166265cff704SKent Gibson for (i = 0; i < ulr.num_lines; i++) { 166373e03419SKent Gibson lr->lines[i].req = lr; 166465cff704SKent Gibson WRITE_ONCE(lr->lines[i].sw_debounced, 0); 166565cff704SKent Gibson INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func); 166665cff704SKent Gibson } 166773e03419SKent Gibson 1668f188ac12SKent Gibson if (ulr.consumer[0] != '\0') { 16693c0d9c63SKent Gibson /* label is only initialized if consumer is set */ 1670f188ac12SKent Gibson lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1, 1671f188ac12SKent Gibson GFP_KERNEL); 16723c0d9c63SKent Gibson if (!lr->label) { 16733c0d9c63SKent Gibson ret = -ENOMEM; 16743c0d9c63SKent Gibson goto out_free_linereq; 16753c0d9c63SKent Gibson } 16763c0d9c63SKent Gibson } 16773c0d9c63SKent Gibson 1678a54756cbSKent Gibson mutex_init(&lr->config_mutex); 167973e03419SKent Gibson init_waitqueue_head(&lr->wait); 168073e03419SKent Gibson lr->event_buffer_size = ulr.event_buffer_size; 168173e03419SKent Gibson if (lr->event_buffer_size == 0) 168273e03419SKent Gibson lr->event_buffer_size = ulr.num_lines * 16; 168373e03419SKent Gibson else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16) 168473e03419SKent Gibson lr->event_buffer_size = GPIO_V2_LINES_MAX * 16; 168573e03419SKent Gibson 168673e03419SKent Gibson atomic_set(&lr->seqno, 0); 16873c0d9c63SKent Gibson lr->num_lines = ulr.num_lines; 16883c0d9c63SKent Gibson 16893c0d9c63SKent Gibson /* Request each GPIO */ 16903c0d9c63SKent Gibson for (i = 0; i < ulr.num_lines; i++) { 16913c0d9c63SKent Gibson u32 offset = ulr.offsets[i]; 16923c0d9c63SKent Gibson struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); 16933c0d9c63SKent Gibson 16943c0d9c63SKent Gibson if (IS_ERR(desc)) { 16953c0d9c63SKent Gibson ret = PTR_ERR(desc); 16963c0d9c63SKent Gibson goto out_free_linereq; 16973c0d9c63SKent Gibson } 16983c0d9c63SKent Gibson 169995a4eed7SAndy Shevchenko ret = gpiod_request_user(desc, lr->label); 17003c0d9c63SKent Gibson if (ret) 17013c0d9c63SKent Gibson goto out_free_linereq; 17023c0d9c63SKent Gibson 17033c0d9c63SKent Gibson lr->lines[i].desc = desc; 17043c0d9c63SKent Gibson flags = gpio_v2_line_config_flags(lc, i); 17053c0d9c63SKent Gibson gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 17063c0d9c63SKent Gibson 17073c0d9c63SKent Gibson ret = gpiod_set_transitory(desc, false); 17083c0d9c63SKent Gibson if (ret < 0) 17093c0d9c63SKent Gibson goto out_free_linereq; 17103c0d9c63SKent Gibson 1711b1a92e94SKent Gibson edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS; 17123c0d9c63SKent Gibson /* 17133c0d9c63SKent Gibson * Lines have to be requested explicitly for input 17143c0d9c63SKent Gibson * or output, else the line will be treated "as is". 17153c0d9c63SKent Gibson */ 17163c0d9c63SKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 17173c0d9c63SKent Gibson int val = gpio_v2_line_config_output_value(lc, i); 17183c0d9c63SKent Gibson 17193c0d9c63SKent Gibson ret = gpiod_direction_output(desc, val); 17203c0d9c63SKent Gibson if (ret) 17213c0d9c63SKent Gibson goto out_free_linereq; 17223c0d9c63SKent Gibson } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { 17233c0d9c63SKent Gibson ret = gpiod_direction_input(desc); 17243c0d9c63SKent Gibson if (ret) 17253c0d9c63SKent Gibson goto out_free_linereq; 172673e03419SKent Gibson 172765cff704SKent Gibson ret = edge_detector_setup(&lr->lines[i], lc, i, 1728b1a92e94SKent Gibson edflags); 172973e03419SKent Gibson if (ret) 173073e03419SKent Gibson goto out_free_linereq; 17313c0d9c63SKent Gibson } 17323c0d9c63SKent Gibson 1733b1a92e94SKent Gibson lr->lines[i].edflags = edflags; 1734b1a92e94SKent Gibson 1735*9ce4ed5bSBartosz Golaszewski gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED); 17363c0d9c63SKent Gibson 17373c0d9c63SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", 17383c0d9c63SKent Gibson offset); 17393c0d9c63SKent Gibson } 17403c0d9c63SKent Gibson 1741a0dda508SBartosz Golaszewski lr->device_unregistered_nb.notifier_call = linereq_unregistered_notify; 1742a0dda508SBartosz Golaszewski ret = blocking_notifier_chain_register(&gdev->device_notifier, 1743a0dda508SBartosz Golaszewski &lr->device_unregistered_nb); 1744a0dda508SBartosz Golaszewski if (ret) 1745a0dda508SBartosz Golaszewski goto out_free_linereq; 1746a0dda508SBartosz Golaszewski 17473c0d9c63SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 17483c0d9c63SKent Gibson if (fd < 0) { 17493c0d9c63SKent Gibson ret = fd; 17503c0d9c63SKent Gibson goto out_free_linereq; 17513c0d9c63SKent Gibson } 17523c0d9c63SKent Gibson 17533c0d9c63SKent Gibson file = anon_inode_getfile("gpio-line", &line_fileops, lr, 17543c0d9c63SKent Gibson O_RDONLY | O_CLOEXEC); 17553c0d9c63SKent Gibson if (IS_ERR(file)) { 17563c0d9c63SKent Gibson ret = PTR_ERR(file); 17573c0d9c63SKent Gibson goto out_put_unused_fd; 17583c0d9c63SKent Gibson } 17593c0d9c63SKent Gibson 17603c0d9c63SKent Gibson ulr.fd = fd; 17613c0d9c63SKent Gibson if (copy_to_user(ip, &ulr, sizeof(ulr))) { 17623c0d9c63SKent Gibson /* 17633c0d9c63SKent Gibson * fput() will trigger the release() callback, so do not go onto 17643c0d9c63SKent Gibson * the regular error cleanup path here. 17653c0d9c63SKent Gibson */ 17663c0d9c63SKent Gibson fput(file); 17673c0d9c63SKent Gibson put_unused_fd(fd); 17683c0d9c63SKent Gibson return -EFAULT; 17693c0d9c63SKent Gibson } 17703c0d9c63SKent Gibson 17713c0d9c63SKent Gibson fd_install(fd, file); 17723c0d9c63SKent Gibson 17733c0d9c63SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", 17743c0d9c63SKent Gibson lr->num_lines); 17753c0d9c63SKent Gibson 17763c0d9c63SKent Gibson return 0; 17773c0d9c63SKent Gibson 17783c0d9c63SKent Gibson out_put_unused_fd: 17793c0d9c63SKent Gibson put_unused_fd(fd); 17803c0d9c63SKent Gibson out_free_linereq: 17813c0d9c63SKent Gibson linereq_free(lr); 17823c0d9c63SKent Gibson return ret; 17833c0d9c63SKent Gibson } 17843c0d9c63SKent Gibson 17853c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 1786925ca369SKent Gibson 1787925ca369SKent Gibson /* 1788925ca369SKent Gibson * GPIO line event management 1789925ca369SKent Gibson */ 1790925ca369SKent Gibson 1791925ca369SKent Gibson /** 1792925ca369SKent Gibson * struct lineevent_state - contains the state of a userspace event 1793925ca369SKent Gibson * @gdev: the GPIO device the event pertains to 1794925ca369SKent Gibson * @label: consumer label used to tag descriptors 1795925ca369SKent Gibson * @desc: the GPIO descriptor held by this event 1796925ca369SKent Gibson * @eflags: the event flags this line was requested with 1797925ca369SKent Gibson * @irq: the interrupt that trigger in response to events on this GPIO 1798925ca369SKent Gibson * @wait: wait queue that handles blocking reads of events 179991043f55SBartosz Golaszewski * @device_unregistered_nb: notifier block for receiving gdev unregister events 1800925ca369SKent Gibson * @events: KFIFO for the GPIO events 1801925ca369SKent Gibson * @timestamp: cache for the timestamp storing it between hardirq 1802925ca369SKent Gibson * and IRQ thread, used to bring the timestamp close to the actual 1803925ca369SKent Gibson * event 1804925ca369SKent Gibson */ 1805925ca369SKent Gibson struct lineevent_state { 1806925ca369SKent Gibson struct gpio_device *gdev; 1807925ca369SKent Gibson const char *label; 1808925ca369SKent Gibson struct gpio_desc *desc; 1809925ca369SKent Gibson u32 eflags; 1810925ca369SKent Gibson int irq; 1811925ca369SKent Gibson wait_queue_head_t wait; 181291043f55SBartosz Golaszewski struct notifier_block device_unregistered_nb; 1813925ca369SKent Gibson DECLARE_KFIFO(events, struct gpioevent_data, 16); 1814925ca369SKent Gibson u64 timestamp; 1815925ca369SKent Gibson }; 1816925ca369SKent Gibson 1817925ca369SKent Gibson #define GPIOEVENT_REQUEST_VALID_FLAGS \ 1818925ca369SKent Gibson (GPIOEVENT_REQUEST_RISING_EDGE | \ 1819925ca369SKent Gibson GPIOEVENT_REQUEST_FALLING_EDGE) 1820925ca369SKent Gibson 1821bdbbae24SBartosz Golaszewski static __poll_t lineevent_poll_unlocked(struct file *file, 1822925ca369SKent Gibson struct poll_table_struct *wait) 1823925ca369SKent Gibson { 182449bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1825925ca369SKent Gibson __poll_t events = 0; 1826925ca369SKent Gibson 1827533aae7cSBartosz Golaszewski if (!le->gdev->chip) 1828533aae7cSBartosz Golaszewski return EPOLLHUP | EPOLLERR; 1829533aae7cSBartosz Golaszewski 183049bc5279SKent Gibson poll_wait(file, &le->wait, wait); 1831925ca369SKent Gibson 1832925ca369SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock)) 1833925ca369SKent Gibson events = EPOLLIN | EPOLLRDNORM; 1834925ca369SKent Gibson 1835925ca369SKent Gibson return events; 1836925ca369SKent Gibson } 1837925ca369SKent Gibson 1838bdbbae24SBartosz Golaszewski static __poll_t lineevent_poll(struct file *file, 1839bdbbae24SBartosz Golaszewski struct poll_table_struct *wait) 1840bdbbae24SBartosz Golaszewski { 1841bdbbae24SBartosz Golaszewski struct lineevent_state *le = file->private_data; 1842bdbbae24SBartosz Golaszewski 1843bdbbae24SBartosz Golaszewski return call_poll_locked(file, wait, le->gdev, lineevent_poll_unlocked); 1844bdbbae24SBartosz Golaszewski } 1845bdbbae24SBartosz Golaszewski 184691043f55SBartosz Golaszewski static int lineevent_unregistered_notify(struct notifier_block *nb, 184791043f55SBartosz Golaszewski unsigned long action, void *data) 184891043f55SBartosz Golaszewski { 184991043f55SBartosz Golaszewski struct lineevent_state *le = container_of(nb, struct lineevent_state, 185091043f55SBartosz Golaszewski device_unregistered_nb); 185191043f55SBartosz Golaszewski 185291043f55SBartosz Golaszewski wake_up_poll(&le->wait, EPOLLIN | EPOLLERR); 185391043f55SBartosz Golaszewski 185491043f55SBartosz Golaszewski return NOTIFY_OK; 185591043f55SBartosz Golaszewski } 185691043f55SBartosz Golaszewski 18575ad284abSAndy Shevchenko struct compat_gpioeevent_data { 18585ad284abSAndy Shevchenko compat_u64 timestamp; 18595ad284abSAndy Shevchenko u32 id; 18605ad284abSAndy Shevchenko }; 18615ad284abSAndy Shevchenko 1862bdbbae24SBartosz Golaszewski static ssize_t lineevent_read_unlocked(struct file *file, char __user *buf, 1863bdbbae24SBartosz Golaszewski size_t count, loff_t *f_ps) 1864925ca369SKent Gibson { 186549bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1866925ca369SKent Gibson struct gpioevent_data ge; 1867925ca369SKent Gibson ssize_t bytes_read = 0; 18685ad284abSAndy Shevchenko ssize_t ge_size; 1869925ca369SKent Gibson int ret; 1870925ca369SKent Gibson 1871533aae7cSBartosz Golaszewski if (!le->gdev->chip) 1872533aae7cSBartosz Golaszewski return -ENODEV; 1873533aae7cSBartosz Golaszewski 18745ad284abSAndy Shevchenko /* 18755ad284abSAndy Shevchenko * When compatible system call is being used the struct gpioevent_data, 18765ad284abSAndy Shevchenko * in case of at least ia32, has different size due to the alignment 18775ad284abSAndy Shevchenko * differences. Because we have first member 64 bits followed by one of 18785ad284abSAndy Shevchenko * 32 bits there is no gap between them. The only difference is the 18795ad284abSAndy Shevchenko * padding at the end of the data structure. Hence, we calculate the 18805ad284abSAndy Shevchenko * actual sizeof() and pass this as an argument to copy_to_user() to 18815ad284abSAndy Shevchenko * drop unneeded bytes from the output. 18825ad284abSAndy Shevchenko */ 1883163d1719SAndy Shevchenko if (compat_need_64bit_alignment_fixup()) 1884163d1719SAndy Shevchenko ge_size = sizeof(struct compat_gpioeevent_data); 1885163d1719SAndy Shevchenko else 1886163d1719SAndy Shevchenko ge_size = sizeof(struct gpioevent_data); 18875ad284abSAndy Shevchenko if (count < ge_size) 1888925ca369SKent Gibson return -EINVAL; 1889925ca369SKent Gibson 1890925ca369SKent Gibson do { 1891925ca369SKent Gibson spin_lock(&le->wait.lock); 1892925ca369SKent Gibson if (kfifo_is_empty(&le->events)) { 1893925ca369SKent Gibson if (bytes_read) { 1894925ca369SKent Gibson spin_unlock(&le->wait.lock); 1895925ca369SKent Gibson return bytes_read; 1896925ca369SKent Gibson } 1897925ca369SKent Gibson 189849bc5279SKent Gibson if (file->f_flags & O_NONBLOCK) { 1899925ca369SKent Gibson spin_unlock(&le->wait.lock); 1900925ca369SKent Gibson return -EAGAIN; 1901925ca369SKent Gibson } 1902925ca369SKent Gibson 1903925ca369SKent Gibson ret = wait_event_interruptible_locked(le->wait, 1904925ca369SKent Gibson !kfifo_is_empty(&le->events)); 1905925ca369SKent Gibson if (ret) { 1906925ca369SKent Gibson spin_unlock(&le->wait.lock); 1907925ca369SKent Gibson return ret; 1908925ca369SKent Gibson } 1909925ca369SKent Gibson } 1910925ca369SKent Gibson 1911925ca369SKent Gibson ret = kfifo_out(&le->events, &ge, 1); 1912925ca369SKent Gibson spin_unlock(&le->wait.lock); 1913925ca369SKent Gibson if (ret != 1) { 1914925ca369SKent Gibson /* 1915925ca369SKent Gibson * This should never happen - we were holding the lock 1916925ca369SKent Gibson * from the moment we learned the fifo is no longer 1917925ca369SKent Gibson * empty until now. 1918925ca369SKent Gibson */ 1919925ca369SKent Gibson ret = -EIO; 1920925ca369SKent Gibson break; 1921925ca369SKent Gibson } 1922925ca369SKent Gibson 19235ad284abSAndy Shevchenko if (copy_to_user(buf + bytes_read, &ge, ge_size)) 1924925ca369SKent Gibson return -EFAULT; 19255ad284abSAndy Shevchenko bytes_read += ge_size; 19265ad284abSAndy Shevchenko } while (count >= bytes_read + ge_size); 1927925ca369SKent Gibson 1928925ca369SKent Gibson return bytes_read; 1929925ca369SKent Gibson } 1930925ca369SKent Gibson 1931bdbbae24SBartosz Golaszewski static ssize_t lineevent_read(struct file *file, char __user *buf, 1932bdbbae24SBartosz Golaszewski size_t count, loff_t *f_ps) 1933bdbbae24SBartosz Golaszewski { 1934bdbbae24SBartosz Golaszewski struct lineevent_state *le = file->private_data; 1935bdbbae24SBartosz Golaszewski 1936bdbbae24SBartosz Golaszewski return call_read_locked(file, buf, count, f_ps, le->gdev, 1937bdbbae24SBartosz Golaszewski lineevent_read_unlocked); 1938bdbbae24SBartosz Golaszewski } 1939bdbbae24SBartosz Golaszewski 194046824272SKent Gibson static void lineevent_free(struct lineevent_state *le) 1941925ca369SKent Gibson { 194291043f55SBartosz Golaszewski if (le->device_unregistered_nb.notifier_call) 194391043f55SBartosz Golaszewski blocking_notifier_chain_unregister(&le->gdev->device_notifier, 194491043f55SBartosz Golaszewski &le->device_unregistered_nb); 194546824272SKent Gibson if (le->irq) 1946925ca369SKent Gibson free_irq(le->irq, le); 194746824272SKent Gibson if (le->desc) 1948925ca369SKent Gibson gpiod_free(le->desc); 1949925ca369SKent Gibson kfree(le->label); 1950dc0989e3SAndy Shevchenko gpio_device_put(le->gdev); 1951925ca369SKent Gibson kfree(le); 195246824272SKent Gibson } 195346824272SKent Gibson 195446824272SKent Gibson static int lineevent_release(struct inode *inode, struct file *file) 195546824272SKent Gibson { 195646824272SKent Gibson lineevent_free(file->private_data); 1957925ca369SKent Gibson return 0; 1958925ca369SKent Gibson } 1959925ca369SKent Gibson 1960bdbbae24SBartosz Golaszewski static long lineevent_ioctl_unlocked(struct file *file, unsigned int cmd, 1961925ca369SKent Gibson unsigned long arg) 1962925ca369SKent Gibson { 196349bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1964925ca369SKent Gibson void __user *ip = (void __user *)arg; 1965925ca369SKent Gibson struct gpiohandle_data ghd; 1966925ca369SKent Gibson 1967533aae7cSBartosz Golaszewski if (!le->gdev->chip) 1968533aae7cSBartosz Golaszewski return -ENODEV; 1969533aae7cSBartosz Golaszewski 1970925ca369SKent Gibson /* 1971925ca369SKent Gibson * We can get the value for an event line but not set it, 1972925ca369SKent Gibson * because it is input by definition. 1973925ca369SKent Gibson */ 1974925ca369SKent Gibson if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { 1975925ca369SKent Gibson int val; 1976925ca369SKent Gibson 1977925ca369SKent Gibson memset(&ghd, 0, sizeof(ghd)); 1978925ca369SKent Gibson 1979925ca369SKent Gibson val = gpiod_get_value_cansleep(le->desc); 1980925ca369SKent Gibson if (val < 0) 1981925ca369SKent Gibson return val; 1982925ca369SKent Gibson ghd.values[0] = val; 1983925ca369SKent Gibson 1984925ca369SKent Gibson if (copy_to_user(ip, &ghd, sizeof(ghd))) 1985925ca369SKent Gibson return -EFAULT; 1986925ca369SKent Gibson 1987925ca369SKent Gibson return 0; 1988925ca369SKent Gibson } 1989925ca369SKent Gibson return -EINVAL; 1990925ca369SKent Gibson } 1991925ca369SKent Gibson 1992bdbbae24SBartosz Golaszewski static long lineevent_ioctl(struct file *file, unsigned int cmd, 1993bdbbae24SBartosz Golaszewski unsigned long arg) 1994bdbbae24SBartosz Golaszewski { 1995bdbbae24SBartosz Golaszewski struct lineevent_state *le = file->private_data; 1996bdbbae24SBartosz Golaszewski 1997bdbbae24SBartosz Golaszewski return call_ioctl_locked(file, cmd, arg, le->gdev, 1998bdbbae24SBartosz Golaszewski lineevent_ioctl_unlocked); 1999bdbbae24SBartosz Golaszewski } 2000bdbbae24SBartosz Golaszewski 2001925ca369SKent Gibson #ifdef CONFIG_COMPAT 200249bc5279SKent Gibson static long lineevent_ioctl_compat(struct file *file, unsigned int cmd, 2003925ca369SKent Gibson unsigned long arg) 2004925ca369SKent Gibson { 200549bc5279SKent Gibson return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 2006925ca369SKent Gibson } 2007925ca369SKent Gibson #endif 2008925ca369SKent Gibson 2009925ca369SKent Gibson static const struct file_operations lineevent_fileops = { 2010925ca369SKent Gibson .release = lineevent_release, 2011925ca369SKent Gibson .read = lineevent_read, 2012925ca369SKent Gibson .poll = lineevent_poll, 2013925ca369SKent Gibson .owner = THIS_MODULE, 2014925ca369SKent Gibson .llseek = noop_llseek, 2015925ca369SKent Gibson .unlocked_ioctl = lineevent_ioctl, 2016925ca369SKent Gibson #ifdef CONFIG_COMPAT 2017925ca369SKent Gibson .compat_ioctl = lineevent_ioctl_compat, 2018925ca369SKent Gibson #endif 2019925ca369SKent Gibson }; 2020925ca369SKent Gibson 2021925ca369SKent Gibson static irqreturn_t lineevent_irq_thread(int irq, void *p) 2022925ca369SKent Gibson { 2023925ca369SKent Gibson struct lineevent_state *le = p; 2024925ca369SKent Gibson struct gpioevent_data ge; 2025925ca369SKent Gibson int ret; 2026925ca369SKent Gibson 2027925ca369SKent Gibson /* Do not leak kernel stack to userspace */ 2028925ca369SKent Gibson memset(&ge, 0, sizeof(ge)); 2029925ca369SKent Gibson 2030925ca369SKent Gibson /* 2031925ca369SKent Gibson * We may be running from a nested threaded interrupt in which case 2032925ca369SKent Gibson * we didn't get the timestamp from lineevent_irq_handler(). 2033925ca369SKent Gibson */ 2034925ca369SKent Gibson if (!le->timestamp) 2035925ca369SKent Gibson ge.timestamp = ktime_get_ns(); 2036925ca369SKent Gibson else 2037925ca369SKent Gibson ge.timestamp = le->timestamp; 2038925ca369SKent Gibson 2039925ca369SKent Gibson if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE 2040925ca369SKent Gibson && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { 2041925ca369SKent Gibson int level = gpiod_get_value_cansleep(le->desc); 2042925ca369SKent Gibson 2043925ca369SKent Gibson if (level) 2044925ca369SKent Gibson /* Emit low-to-high event */ 2045925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_RISING_EDGE; 2046925ca369SKent Gibson else 2047925ca369SKent Gibson /* Emit high-to-low event */ 2048925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_FALLING_EDGE; 2049925ca369SKent Gibson } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) { 2050925ca369SKent Gibson /* Emit low-to-high event */ 2051925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_RISING_EDGE; 2052925ca369SKent Gibson } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { 2053925ca369SKent Gibson /* Emit high-to-low event */ 2054925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_FALLING_EDGE; 2055925ca369SKent Gibson } else { 2056925ca369SKent Gibson return IRQ_NONE; 2057925ca369SKent Gibson } 2058925ca369SKent Gibson 2059925ca369SKent Gibson ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge, 2060925ca369SKent Gibson 1, &le->wait.lock); 2061925ca369SKent Gibson if (ret) 2062925ca369SKent Gibson wake_up_poll(&le->wait, EPOLLIN); 2063925ca369SKent Gibson else 2064925ca369SKent Gibson pr_debug_ratelimited("event FIFO is full - event dropped\n"); 2065925ca369SKent Gibson 2066925ca369SKent Gibson return IRQ_HANDLED; 2067925ca369SKent Gibson } 2068925ca369SKent Gibson 2069925ca369SKent Gibson static irqreturn_t lineevent_irq_handler(int irq, void *p) 2070925ca369SKent Gibson { 2071925ca369SKent Gibson struct lineevent_state *le = p; 2072925ca369SKent Gibson 2073925ca369SKent Gibson /* 2074925ca369SKent Gibson * Just store the timestamp in hardirq context so we get it as 2075925ca369SKent Gibson * close in time as possible to the actual event. 2076925ca369SKent Gibson */ 2077925ca369SKent Gibson le->timestamp = ktime_get_ns(); 2078925ca369SKent Gibson 2079925ca369SKent Gibson return IRQ_WAKE_THREAD; 2080925ca369SKent Gibson } 2081925ca369SKent Gibson 2082925ca369SKent Gibson static int lineevent_create(struct gpio_device *gdev, void __user *ip) 2083925ca369SKent Gibson { 2084925ca369SKent Gibson struct gpioevent_request eventreq; 2085925ca369SKent Gibson struct lineevent_state *le; 2086925ca369SKent Gibson struct gpio_desc *desc; 2087925ca369SKent Gibson struct file *file; 2088925ca369SKent Gibson u32 offset; 2089925ca369SKent Gibson u32 lflags; 2090925ca369SKent Gibson u32 eflags; 2091925ca369SKent Gibson int fd; 2092925ca369SKent Gibson int ret; 209346824272SKent Gibson int irq, irqflags = 0; 2094925ca369SKent Gibson 2095925ca369SKent Gibson if (copy_from_user(&eventreq, ip, sizeof(eventreq))) 2096925ca369SKent Gibson return -EFAULT; 2097925ca369SKent Gibson 2098925ca369SKent Gibson offset = eventreq.lineoffset; 2099925ca369SKent Gibson lflags = eventreq.handleflags; 2100925ca369SKent Gibson eflags = eventreq.eventflags; 2101925ca369SKent Gibson 2102925ca369SKent Gibson desc = gpiochip_get_desc(gdev->chip, offset); 2103925ca369SKent Gibson if (IS_ERR(desc)) 2104925ca369SKent Gibson return PTR_ERR(desc); 2105925ca369SKent Gibson 2106925ca369SKent Gibson /* Return an error if a unknown flag is set */ 2107925ca369SKent Gibson if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || 2108925ca369SKent Gibson (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) 2109925ca369SKent Gibson return -EINVAL; 2110925ca369SKent Gibson 2111925ca369SKent Gibson /* This is just wrong: we don't look for events on output lines */ 2112925ca369SKent Gibson if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) || 2113925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || 2114925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) 2115925ca369SKent Gibson return -EINVAL; 2116925ca369SKent Gibson 2117925ca369SKent Gibson /* Only one bias flag can be set. */ 2118925ca369SKent Gibson if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && 2119925ca369SKent Gibson (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | 2120925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || 2121925ca369SKent Gibson ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && 2122925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) 2123925ca369SKent Gibson return -EINVAL; 2124925ca369SKent Gibson 2125925ca369SKent Gibson le = kzalloc(sizeof(*le), GFP_KERNEL); 2126925ca369SKent Gibson if (!le) 2127925ca369SKent Gibson return -ENOMEM; 2128dc0989e3SAndy Shevchenko le->gdev = gpio_device_get(gdev); 2129925ca369SKent Gibson 2130f188ac12SKent Gibson if (eventreq.consumer_label[0] != '\0') { 2131f188ac12SKent Gibson /* label is only initialized if consumer_label is set */ 2132f188ac12SKent Gibson le->label = kstrndup(eventreq.consumer_label, 2133f188ac12SKent Gibson sizeof(eventreq.consumer_label) - 1, 2134925ca369SKent Gibson GFP_KERNEL); 2135925ca369SKent Gibson if (!le->label) { 2136925ca369SKent Gibson ret = -ENOMEM; 2137925ca369SKent Gibson goto out_free_le; 2138925ca369SKent Gibson } 2139925ca369SKent Gibson } 2140925ca369SKent Gibson 214195a4eed7SAndy Shevchenko ret = gpiod_request_user(desc, le->label); 2142925ca369SKent Gibson if (ret) 214346824272SKent Gibson goto out_free_le; 2144925ca369SKent Gibson le->desc = desc; 2145925ca369SKent Gibson le->eflags = eflags; 2146925ca369SKent Gibson 2147c274b58aSKent Gibson linehandle_flags_to_desc_flags(lflags, &desc->flags); 2148925ca369SKent Gibson 2149925ca369SKent Gibson ret = gpiod_direction_input(desc); 2150925ca369SKent Gibson if (ret) 215146824272SKent Gibson goto out_free_le; 2152925ca369SKent Gibson 2153*9ce4ed5bSBartosz Golaszewski gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED); 2154925ca369SKent Gibson 215546824272SKent Gibson irq = gpiod_to_irq(desc); 215646824272SKent Gibson if (irq <= 0) { 2157925ca369SKent Gibson ret = -ENODEV; 215846824272SKent Gibson goto out_free_le; 2159925ca369SKent Gibson } 2160925ca369SKent Gibson 2161925ca369SKent Gibson if (eflags & GPIOEVENT_REQUEST_RISING_EDGE) 2162925ca369SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 2163925ca369SKent Gibson IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 2164925ca369SKent Gibson if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE) 2165925ca369SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 2166925ca369SKent Gibson IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 2167925ca369SKent Gibson irqflags |= IRQF_ONESHOT; 2168925ca369SKent Gibson 2169925ca369SKent Gibson INIT_KFIFO(le->events); 2170925ca369SKent Gibson init_waitqueue_head(&le->wait); 2171925ca369SKent Gibson 217291043f55SBartosz Golaszewski le->device_unregistered_nb.notifier_call = lineevent_unregistered_notify; 217391043f55SBartosz Golaszewski ret = blocking_notifier_chain_register(&gdev->device_notifier, 217491043f55SBartosz Golaszewski &le->device_unregistered_nb); 217591043f55SBartosz Golaszewski if (ret) 217691043f55SBartosz Golaszewski goto out_free_le; 217791043f55SBartosz Golaszewski 2178925ca369SKent Gibson /* Request a thread to read the events */ 217969bef19dSMeng Li ret = request_threaded_irq(irq, 2180925ca369SKent Gibson lineevent_irq_handler, 2181925ca369SKent Gibson lineevent_irq_thread, 2182925ca369SKent Gibson irqflags, 2183925ca369SKent Gibson le->label, 2184925ca369SKent Gibson le); 2185925ca369SKent Gibson if (ret) 218646824272SKent Gibson goto out_free_le; 2187925ca369SKent Gibson 218869bef19dSMeng Li le->irq = irq; 218969bef19dSMeng Li 2190925ca369SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 2191925ca369SKent Gibson if (fd < 0) { 2192925ca369SKent Gibson ret = fd; 219346824272SKent Gibson goto out_free_le; 2194925ca369SKent Gibson } 2195925ca369SKent Gibson 2196925ca369SKent Gibson file = anon_inode_getfile("gpio-event", 2197925ca369SKent Gibson &lineevent_fileops, 2198925ca369SKent Gibson le, 2199925ca369SKent Gibson O_RDONLY | O_CLOEXEC); 2200925ca369SKent Gibson if (IS_ERR(file)) { 2201925ca369SKent Gibson ret = PTR_ERR(file); 2202925ca369SKent Gibson goto out_put_unused_fd; 2203925ca369SKent Gibson } 2204925ca369SKent Gibson 2205925ca369SKent Gibson eventreq.fd = fd; 2206925ca369SKent Gibson if (copy_to_user(ip, &eventreq, sizeof(eventreq))) { 2207925ca369SKent Gibson /* 2208925ca369SKent Gibson * fput() will trigger the release() callback, so do not go onto 2209925ca369SKent Gibson * the regular error cleanup path here. 2210925ca369SKent Gibson */ 2211925ca369SKent Gibson fput(file); 2212925ca369SKent Gibson put_unused_fd(fd); 2213925ca369SKent Gibson return -EFAULT; 2214925ca369SKent Gibson } 2215925ca369SKent Gibson 2216925ca369SKent Gibson fd_install(fd, file); 2217925ca369SKent Gibson 2218925ca369SKent Gibson return 0; 2219925ca369SKent Gibson 2220925ca369SKent Gibson out_put_unused_fd: 2221925ca369SKent Gibson put_unused_fd(fd); 2222925ca369SKent Gibson out_free_le: 222346824272SKent Gibson lineevent_free(le); 2224925ca369SKent Gibson return ret; 2225925ca369SKent Gibson } 2226925ca369SKent Gibson 2227aad95584SKent Gibson static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2, 2228aad95584SKent Gibson struct gpioline_info *info_v1) 2229aad95584SKent Gibson { 2230aad95584SKent Gibson u64 flagsv2 = info_v2->flags; 2231aad95584SKent Gibson 2232aad95584SKent Gibson memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name)); 2233aad95584SKent Gibson memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer)); 2234aad95584SKent Gibson info_v1->line_offset = info_v2->offset; 2235aad95584SKent Gibson info_v1->flags = 0; 2236aad95584SKent Gibson 2237aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_USED) 2238aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_KERNEL; 2239aad95584SKent Gibson 2240aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT) 2241aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_IS_OUT; 2242aad95584SKent Gibson 2243aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 2244aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW; 2245aad95584SKent Gibson 2246aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN) 2247aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN; 2248aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE) 2249aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE; 2250aad95584SKent Gibson 2251aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP) 2252aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; 2253aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) 2254aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; 2255aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED) 2256aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE; 2257aad95584SKent Gibson } 2258aad95584SKent Gibson 2259aad95584SKent Gibson static void gpio_v2_line_info_changed_to_v1( 2260aad95584SKent Gibson struct gpio_v2_line_info_changed *lic_v2, 2261aad95584SKent Gibson struct gpioline_info_changed *lic_v1) 2262aad95584SKent Gibson { 2263cb8f63b8SGabriel Knezek memset(lic_v1, 0, sizeof(*lic_v1)); 2264aad95584SKent Gibson gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info); 2265aad95584SKent Gibson lic_v1->timestamp = lic_v2->timestamp_ns; 2266aad95584SKent Gibson lic_v1->event_type = lic_v2->event_type; 2267aad95584SKent Gibson } 2268aad95584SKent Gibson 22693c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 22703c0d9c63SKent Gibson 2271925ca369SKent Gibson static void gpio_desc_to_lineinfo(struct gpio_desc *desc, 2272aad95584SKent Gibson struct gpio_v2_line_info *info) 2273925ca369SKent Gibson { 2274925ca369SKent Gibson struct gpio_chip *gc = desc->gdev->chip; 2275925ca369SKent Gibson bool ok_for_pinctrl; 2276925ca369SKent Gibson unsigned long flags; 227765cff704SKent Gibson u32 debounce_period_us; 227865cff704SKent Gibson unsigned int num_attrs = 0; 2279925ca369SKent Gibson 228069e4e136SKent Gibson memset(info, 0, sizeof(*info)); 2281aad95584SKent Gibson info->offset = gpio_chip_hwgpio(desc); 2282925ca369SKent Gibson 2283925ca369SKent Gibson /* 2284925ca369SKent Gibson * This function takes a mutex so we must check this before taking 2285925ca369SKent Gibson * the spinlock. 2286925ca369SKent Gibson * 2287925ca369SKent Gibson * FIXME: find a non-racy way to retrieve this information. Maybe a 2288925ca369SKent Gibson * lock common to both frameworks? 2289925ca369SKent Gibson */ 2290925ca369SKent Gibson ok_for_pinctrl = 2291aad95584SKent Gibson pinctrl_gpio_can_use_line(gc->base + info->offset); 2292925ca369SKent Gibson 2293925ca369SKent Gibson spin_lock_irqsave(&gpio_lock, flags); 2294925ca369SKent Gibson 229569e4e136SKent Gibson if (desc->name) 229669e4e136SKent Gibson strscpy(info->name, desc->name, sizeof(info->name)); 2297925ca369SKent Gibson 229869e4e136SKent Gibson if (desc->label) 229969e4e136SKent Gibson strscpy(info->consumer, desc->label, sizeof(info->consumer)); 2300925ca369SKent Gibson 2301925ca369SKent Gibson /* 2302925ca369SKent Gibson * Userspace only need to know that the kernel is using this GPIO so 2303925ca369SKent Gibson * it can't use it. 2304925ca369SKent Gibson */ 2305925ca369SKent Gibson info->flags = 0; 2306925ca369SKent Gibson if (test_bit(FLAG_REQUESTED, &desc->flags) || 2307925ca369SKent Gibson test_bit(FLAG_IS_HOGGED, &desc->flags) || 2308925ca369SKent Gibson test_bit(FLAG_USED_AS_IRQ, &desc->flags) || 2309925ca369SKent Gibson test_bit(FLAG_EXPORT, &desc->flags) || 2310925ca369SKent Gibson test_bit(FLAG_SYSFS, &desc->flags) || 2311a0db197fSMarc Zyngier !gpiochip_line_is_valid(gc, info->offset) || 2312925ca369SKent Gibson !ok_for_pinctrl) 2313aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_USED; 2314aad95584SKent Gibson 2315925ca369SKent Gibson if (test_bit(FLAG_IS_OUT, &desc->flags)) 2316aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OUTPUT; 2317aad95584SKent Gibson else 2318aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_INPUT; 2319aad95584SKent Gibson 2320925ca369SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 2321aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW; 2322aad95584SKent Gibson 2323925ca369SKent Gibson if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 2324aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; 2325925ca369SKent Gibson if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 2326aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; 2327aad95584SKent Gibson 2328925ca369SKent Gibson if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) 2329aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED; 2330925ca369SKent Gibson if (test_bit(FLAG_PULL_DOWN, &desc->flags)) 2331aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN; 2332925ca369SKent Gibson if (test_bit(FLAG_PULL_UP, &desc->flags)) 2333aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; 2334925ca369SKent Gibson 233573e03419SKent Gibson if (test_bit(FLAG_EDGE_RISING, &desc->flags)) 233673e03419SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; 233773e03419SKent Gibson if (test_bit(FLAG_EDGE_FALLING, &desc->flags)) 233873e03419SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; 233973e03419SKent Gibson 234026d060e4SKent Gibson if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags)) 234126d060e4SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME; 23422068339aSDipen Patel else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags)) 23432068339aSDipen Patel info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE; 234426d060e4SKent Gibson 234565cff704SKent Gibson debounce_period_us = READ_ONCE(desc->debounce_period_us); 234665cff704SKent Gibson if (debounce_period_us) { 234765cff704SKent Gibson info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; 234865cff704SKent Gibson info->attrs[num_attrs].debounce_period_us = debounce_period_us; 234965cff704SKent Gibson num_attrs++; 235065cff704SKent Gibson } 235165cff704SKent Gibson info->num_attrs = num_attrs; 2352925ca369SKent Gibson 2353925ca369SKent Gibson spin_unlock_irqrestore(&gpio_lock, flags); 2354925ca369SKent Gibson } 2355925ca369SKent Gibson 2356925ca369SKent Gibson struct gpio_chardev_data { 2357925ca369SKent Gibson struct gpio_device *gdev; 2358925ca369SKent Gibson wait_queue_head_t wait; 2359aad95584SKent Gibson DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32); 2360925ca369SKent Gibson struct notifier_block lineinfo_changed_nb; 2361d2e2586aSBartosz Golaszewski struct notifier_block device_unregistered_nb; 2362925ca369SKent Gibson unsigned long *watched_lines; 2363aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2364aad95584SKent Gibson atomic_t watch_abi_version; 2365aad95584SKent Gibson #endif 2366925ca369SKent Gibson }; 2367925ca369SKent Gibson 23682e202ad8SKent Gibson static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip) 23692e202ad8SKent Gibson { 23702e202ad8SKent Gibson struct gpio_device *gdev = cdev->gdev; 23712e202ad8SKent Gibson struct gpiochip_info chipinfo; 23722e202ad8SKent Gibson 23732e202ad8SKent Gibson memset(&chipinfo, 0, sizeof(chipinfo)); 23742e202ad8SKent Gibson 23752e202ad8SKent Gibson strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name)); 23762e202ad8SKent Gibson strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label)); 23772e202ad8SKent Gibson chipinfo.lines = gdev->ngpio; 23782e202ad8SKent Gibson if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) 23792e202ad8SKent Gibson return -EFAULT; 23802e202ad8SKent Gibson return 0; 23812e202ad8SKent Gibson } 23822e202ad8SKent Gibson 2383aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2384aad95584SKent Gibson /* 2385aad95584SKent Gibson * returns 0 if the versions match, else the previously selected ABI version 2386aad95584SKent Gibson */ 2387aad95584SKent Gibson static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata, 2388aad95584SKent Gibson unsigned int version) 2389aad95584SKent Gibson { 2390aad95584SKent Gibson int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version); 2391aad95584SKent Gibson 2392aad95584SKent Gibson if (abiv == version) 2393aad95584SKent Gibson return 0; 2394aad95584SKent Gibson 2395aad95584SKent Gibson return abiv; 2396aad95584SKent Gibson } 23972e202ad8SKent Gibson 23982e202ad8SKent Gibson static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip, 23992e202ad8SKent Gibson bool watch) 24002e202ad8SKent Gibson { 24012e202ad8SKent Gibson struct gpio_desc *desc; 24022e202ad8SKent Gibson struct gpioline_info lineinfo; 24032e202ad8SKent Gibson struct gpio_v2_line_info lineinfo_v2; 24042e202ad8SKent Gibson 24052e202ad8SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 24062e202ad8SKent Gibson return -EFAULT; 24072e202ad8SKent Gibson 24082e202ad8SKent Gibson /* this doubles as a range check on line_offset */ 24092e202ad8SKent Gibson desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset); 24102e202ad8SKent Gibson if (IS_ERR(desc)) 24112e202ad8SKent Gibson return PTR_ERR(desc); 24122e202ad8SKent Gibson 24132e202ad8SKent Gibson if (watch) { 24142e202ad8SKent Gibson if (lineinfo_ensure_abi_version(cdev, 1)) 24152e202ad8SKent Gibson return -EPERM; 24162e202ad8SKent Gibson 24172e202ad8SKent Gibson if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines)) 24182e202ad8SKent Gibson return -EBUSY; 24192e202ad8SKent Gibson } 24202e202ad8SKent Gibson 24212e202ad8SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo_v2); 24222e202ad8SKent Gibson gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); 24232e202ad8SKent Gibson 24242e202ad8SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 24252e202ad8SKent Gibson if (watch) 24262e202ad8SKent Gibson clear_bit(lineinfo.line_offset, cdev->watched_lines); 24272e202ad8SKent Gibson return -EFAULT; 24282e202ad8SKent Gibson } 24292e202ad8SKent Gibson 24302e202ad8SKent Gibson return 0; 24312e202ad8SKent Gibson } 2432aad95584SKent Gibson #endif 2433aad95584SKent Gibson 2434aad95584SKent Gibson static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip, 2435aad95584SKent Gibson bool watch) 2436aad95584SKent Gibson { 2437aad95584SKent Gibson struct gpio_desc *desc; 2438aad95584SKent Gibson struct gpio_v2_line_info lineinfo; 2439aad95584SKent Gibson 2440aad95584SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 2441aad95584SKent Gibson return -EFAULT; 2442aad95584SKent Gibson 2443aad95584SKent Gibson if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding))) 2444aad95584SKent Gibson return -EINVAL; 2445aad95584SKent Gibson 2446aad95584SKent Gibson desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset); 2447aad95584SKent Gibson if (IS_ERR(desc)) 2448aad95584SKent Gibson return PTR_ERR(desc); 2449aad95584SKent Gibson 2450aad95584SKent Gibson if (watch) { 2451aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2452aad95584SKent Gibson if (lineinfo_ensure_abi_version(cdev, 2)) 2453aad95584SKent Gibson return -EPERM; 2454aad95584SKent Gibson #endif 2455aad95584SKent Gibson if (test_and_set_bit(lineinfo.offset, cdev->watched_lines)) 2456aad95584SKent Gibson return -EBUSY; 2457aad95584SKent Gibson } 2458aad95584SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo); 2459aad95584SKent Gibson 2460aad95584SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 2461aad95584SKent Gibson if (watch) 2462aad95584SKent Gibson clear_bit(lineinfo.offset, cdev->watched_lines); 2463aad95584SKent Gibson return -EFAULT; 2464aad95584SKent Gibson } 2465aad95584SKent Gibson 2466aad95584SKent Gibson return 0; 2467aad95584SKent Gibson } 2468aad95584SKent Gibson 24692e202ad8SKent Gibson static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip) 2470925ca369SKent Gibson { 2471925ca369SKent Gibson __u32 offset; 2472925ca369SKent Gibson 2473925ca369SKent Gibson if (copy_from_user(&offset, ip, sizeof(offset))) 2474925ca369SKent Gibson return -EFAULT; 2475925ca369SKent Gibson 24761bf7ba40SKent Gibson if (offset >= cdev->gdev->ngpio) 24771bf7ba40SKent Gibson return -EINVAL; 2478925ca369SKent Gibson 24791bf7ba40SKent Gibson if (!test_and_clear_bit(offset, cdev->watched_lines)) 2480925ca369SKent Gibson return -EBUSY; 2481925ca369SKent Gibson 2482925ca369SKent Gibson return 0; 2483925ca369SKent Gibson } 24842e202ad8SKent Gibson 24852e202ad8SKent Gibson /* 24862e202ad8SKent Gibson * gpio_ioctl() - ioctl handler for the GPIO chardev 24872e202ad8SKent Gibson */ 24882e202ad8SKent Gibson static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 24892e202ad8SKent Gibson { 24902e202ad8SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 24912e202ad8SKent Gibson struct gpio_device *gdev = cdev->gdev; 24922e202ad8SKent Gibson void __user *ip = (void __user *)arg; 24932e202ad8SKent Gibson 24942e202ad8SKent Gibson /* We fail any subsequent ioctl():s when the chip is gone */ 24952e202ad8SKent Gibson if (!gdev->chip) 24962e202ad8SKent Gibson return -ENODEV; 24972e202ad8SKent Gibson 24982e202ad8SKent Gibson /* Fill in the struct and pass to userspace */ 24991cef8b50SAndy Shevchenko switch (cmd) { 25001cef8b50SAndy Shevchenko case GPIO_GET_CHIPINFO_IOCTL: 25012e202ad8SKent Gibson return chipinfo_get(cdev, ip); 25022e202ad8SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 25031cef8b50SAndy Shevchenko case GPIO_GET_LINEHANDLE_IOCTL: 25042e202ad8SKent Gibson return linehandle_create(gdev, ip); 25051cef8b50SAndy Shevchenko case GPIO_GET_LINEEVENT_IOCTL: 25062e202ad8SKent Gibson return lineevent_create(gdev, ip); 25071cef8b50SAndy Shevchenko case GPIO_GET_LINEINFO_IOCTL: 25081cef8b50SAndy Shevchenko return lineinfo_get_v1(cdev, ip, false); 25091cef8b50SAndy Shevchenko case GPIO_GET_LINEINFO_WATCH_IOCTL: 25101cef8b50SAndy Shevchenko return lineinfo_get_v1(cdev, ip, true); 25112e202ad8SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 25121cef8b50SAndy Shevchenko case GPIO_V2_GET_LINEINFO_IOCTL: 25131cef8b50SAndy Shevchenko return lineinfo_get(cdev, ip, false); 25141cef8b50SAndy Shevchenko case GPIO_V2_GET_LINEINFO_WATCH_IOCTL: 25151cef8b50SAndy Shevchenko return lineinfo_get(cdev, ip, true); 25161cef8b50SAndy Shevchenko case GPIO_V2_GET_LINE_IOCTL: 25172e202ad8SKent Gibson return linereq_create(gdev, ip); 25181cef8b50SAndy Shevchenko case GPIO_GET_LINEINFO_UNWATCH_IOCTL: 25192e202ad8SKent Gibson return lineinfo_unwatch(cdev, ip); 25201cef8b50SAndy Shevchenko default: 2521925ca369SKent Gibson return -EINVAL; 2522925ca369SKent Gibson } 25231cef8b50SAndy Shevchenko } 2524925ca369SKent Gibson 2525925ca369SKent Gibson #ifdef CONFIG_COMPAT 252649bc5279SKent Gibson static long gpio_ioctl_compat(struct file *file, unsigned int cmd, 2527925ca369SKent Gibson unsigned long arg) 2528925ca369SKent Gibson { 252949bc5279SKent Gibson return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 2530925ca369SKent Gibson } 2531925ca369SKent Gibson #endif 2532925ca369SKent Gibson 2533925ca369SKent Gibson static int lineinfo_changed_notify(struct notifier_block *nb, 2534925ca369SKent Gibson unsigned long action, void *data) 2535925ca369SKent Gibson { 2536e82bbd67SBartosz Golaszewski struct gpio_chardev_data *cdev = 2537e82bbd67SBartosz Golaszewski container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb); 2538aad95584SKent Gibson struct gpio_v2_line_info_changed chg; 2539925ca369SKent Gibson struct gpio_desc *desc = data; 2540925ca369SKent Gibson int ret; 2541925ca369SKent Gibson 2542e2b781c5SKent Gibson if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) 2543925ca369SKent Gibson return NOTIFY_DONE; 2544925ca369SKent Gibson 2545925ca369SKent Gibson memset(&chg, 0, sizeof(chg)); 2546925ca369SKent Gibson chg.event_type = action; 2547aad95584SKent Gibson chg.timestamp_ns = ktime_get_ns(); 2548925ca369SKent Gibson gpio_desc_to_lineinfo(desc, &chg.info); 2549925ca369SKent Gibson 2550e2b781c5SKent Gibson ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); 2551925ca369SKent Gibson if (ret) 2552e2b781c5SKent Gibson wake_up_poll(&cdev->wait, EPOLLIN); 2553925ca369SKent Gibson else 2554925ca369SKent Gibson pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n"); 2555925ca369SKent Gibson 2556925ca369SKent Gibson return NOTIFY_OK; 2557925ca369SKent Gibson } 2558925ca369SKent Gibson 2559d2e2586aSBartosz Golaszewski static int gpio_device_unregistered_notify(struct notifier_block *nb, 2560d2e2586aSBartosz Golaszewski unsigned long action, void *data) 2561d2e2586aSBartosz Golaszewski { 2562d2e2586aSBartosz Golaszewski struct gpio_chardev_data *cdev = container_of(nb, 2563d2e2586aSBartosz Golaszewski struct gpio_chardev_data, 2564d2e2586aSBartosz Golaszewski device_unregistered_nb); 2565d2e2586aSBartosz Golaszewski 2566d2e2586aSBartosz Golaszewski wake_up_poll(&cdev->wait, EPOLLIN | EPOLLERR); 2567d2e2586aSBartosz Golaszewski 2568d2e2586aSBartosz Golaszewski return NOTIFY_OK; 2569d2e2586aSBartosz Golaszewski } 2570d2e2586aSBartosz Golaszewski 2571bdbbae24SBartosz Golaszewski static __poll_t lineinfo_watch_poll_unlocked(struct file *file, 2572925ca369SKent Gibson struct poll_table_struct *pollt) 2573925ca369SKent Gibson { 2574e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2575925ca369SKent Gibson __poll_t events = 0; 2576925ca369SKent Gibson 2577533aae7cSBartosz Golaszewski if (!cdev->gdev->chip) 2578533aae7cSBartosz Golaszewski return EPOLLHUP | EPOLLERR; 2579533aae7cSBartosz Golaszewski 2580e2b781c5SKent Gibson poll_wait(file, &cdev->wait, pollt); 2581925ca369SKent Gibson 2582e2b781c5SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events, 2583e2b781c5SKent Gibson &cdev->wait.lock)) 2584925ca369SKent Gibson events = EPOLLIN | EPOLLRDNORM; 2585925ca369SKent Gibson 2586925ca369SKent Gibson return events; 2587925ca369SKent Gibson } 2588925ca369SKent Gibson 2589bdbbae24SBartosz Golaszewski static __poll_t lineinfo_watch_poll(struct file *file, 2590bdbbae24SBartosz Golaszewski struct poll_table_struct *pollt) 2591bdbbae24SBartosz Golaszewski { 2592bdbbae24SBartosz Golaszewski struct gpio_chardev_data *cdev = file->private_data; 2593bdbbae24SBartosz Golaszewski 2594bdbbae24SBartosz Golaszewski return call_poll_locked(file, pollt, cdev->gdev, 2595bdbbae24SBartosz Golaszewski lineinfo_watch_poll_unlocked); 2596bdbbae24SBartosz Golaszewski } 2597bdbbae24SBartosz Golaszewski 2598bdbbae24SBartosz Golaszewski static ssize_t lineinfo_watch_read_unlocked(struct file *file, char __user *buf, 2599925ca369SKent Gibson size_t count, loff_t *off) 2600925ca369SKent Gibson { 2601e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2602aad95584SKent Gibson struct gpio_v2_line_info_changed event; 2603925ca369SKent Gibson ssize_t bytes_read = 0; 2604925ca369SKent Gibson int ret; 2605aad95584SKent Gibson size_t event_size; 2606925ca369SKent Gibson 2607533aae7cSBartosz Golaszewski if (!cdev->gdev->chip) 2608533aae7cSBartosz Golaszewski return -ENODEV; 2609533aae7cSBartosz Golaszewski 2610aad95584SKent Gibson #ifndef CONFIG_GPIO_CDEV_V1 2611aad95584SKent Gibson event_size = sizeof(struct gpio_v2_line_info_changed); 2612aad95584SKent Gibson if (count < event_size) 2613925ca369SKent Gibson return -EINVAL; 2614aad95584SKent Gibson #endif 2615925ca369SKent Gibson 2616925ca369SKent Gibson do { 2617e2b781c5SKent Gibson spin_lock(&cdev->wait.lock); 2618e2b781c5SKent Gibson if (kfifo_is_empty(&cdev->events)) { 2619925ca369SKent Gibson if (bytes_read) { 2620e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2621925ca369SKent Gibson return bytes_read; 2622925ca369SKent Gibson } 2623925ca369SKent Gibson 262449bc5279SKent Gibson if (file->f_flags & O_NONBLOCK) { 2625e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2626925ca369SKent Gibson return -EAGAIN; 2627925ca369SKent Gibson } 2628925ca369SKent Gibson 2629e2b781c5SKent Gibson ret = wait_event_interruptible_locked(cdev->wait, 2630e2b781c5SKent Gibson !kfifo_is_empty(&cdev->events)); 2631925ca369SKent Gibson if (ret) { 2632e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2633925ca369SKent Gibson return ret; 2634925ca369SKent Gibson } 2635925ca369SKent Gibson } 2636aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2637aad95584SKent Gibson /* must be after kfifo check so watch_abi_version is set */ 2638aad95584SKent Gibson if (atomic_read(&cdev->watch_abi_version) == 2) 2639aad95584SKent Gibson event_size = sizeof(struct gpio_v2_line_info_changed); 2640aad95584SKent Gibson else 2641aad95584SKent Gibson event_size = sizeof(struct gpioline_info_changed); 2642aad95584SKent Gibson if (count < event_size) { 2643aad95584SKent Gibson spin_unlock(&cdev->wait.lock); 2644aad95584SKent Gibson return -EINVAL; 2645aad95584SKent Gibson } 2646aad95584SKent Gibson #endif 2647e2b781c5SKent Gibson ret = kfifo_out(&cdev->events, &event, 1); 2648e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2649925ca369SKent Gibson if (ret != 1) { 2650925ca369SKent Gibson ret = -EIO; 2651925ca369SKent Gibson break; 2652925ca369SKent Gibson /* We should never get here. See lineevent_read(). */ 2653925ca369SKent Gibson } 2654925ca369SKent Gibson 2655aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2656aad95584SKent Gibson if (event_size == sizeof(struct gpio_v2_line_info_changed)) { 2657aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event, event_size)) 2658925ca369SKent Gibson return -EFAULT; 2659aad95584SKent Gibson } else { 2660aad95584SKent Gibson struct gpioline_info_changed event_v1; 2661aad95584SKent Gibson 2662aad95584SKent Gibson gpio_v2_line_info_changed_to_v1(&event, &event_v1); 2663aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event_v1, 2664aad95584SKent Gibson event_size)) 2665aad95584SKent Gibson return -EFAULT; 2666aad95584SKent Gibson } 2667aad95584SKent Gibson #else 2668aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event, event_size)) 2669aad95584SKent Gibson return -EFAULT; 2670aad95584SKent Gibson #endif 2671aad95584SKent Gibson bytes_read += event_size; 2672925ca369SKent Gibson } while (count >= bytes_read + sizeof(event)); 2673925ca369SKent Gibson 2674925ca369SKent Gibson return bytes_read; 2675925ca369SKent Gibson } 2676925ca369SKent Gibson 2677bdbbae24SBartosz Golaszewski static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, 2678bdbbae24SBartosz Golaszewski size_t count, loff_t *off) 2679bdbbae24SBartosz Golaszewski { 2680bdbbae24SBartosz Golaszewski struct gpio_chardev_data *cdev = file->private_data; 2681bdbbae24SBartosz Golaszewski 2682bdbbae24SBartosz Golaszewski return call_read_locked(file, buf, count, off, cdev->gdev, 2683bdbbae24SBartosz Golaszewski lineinfo_watch_read_unlocked); 2684bdbbae24SBartosz Golaszewski } 2685bdbbae24SBartosz Golaszewski 2686925ca369SKent Gibson /** 2687925ca369SKent Gibson * gpio_chrdev_open() - open the chardev for ioctl operations 2688925ca369SKent Gibson * @inode: inode for this chardev 268949bc5279SKent Gibson * @file: file struct for storing private data 2690925ca369SKent Gibson * Returns 0 on success 2691925ca369SKent Gibson */ 269249bc5279SKent Gibson static int gpio_chrdev_open(struct inode *inode, struct file *file) 2693925ca369SKent Gibson { 2694925ca369SKent Gibson struct gpio_device *gdev = container_of(inode->i_cdev, 2695925ca369SKent Gibson struct gpio_device, chrdev); 2696e2b781c5SKent Gibson struct gpio_chardev_data *cdev; 2697925ca369SKent Gibson int ret = -ENOMEM; 2698925ca369SKent Gibson 2699bdbbae24SBartosz Golaszewski down_read(&gdev->sem); 2700bdbbae24SBartosz Golaszewski 2701925ca369SKent Gibson /* Fail on open if the backing gpiochip is gone */ 2702bdbbae24SBartosz Golaszewski if (!gdev->chip) { 2703bdbbae24SBartosz Golaszewski ret = -ENODEV; 2704bdbbae24SBartosz Golaszewski goto out_unlock; 2705bdbbae24SBartosz Golaszewski } 2706925ca369SKent Gibson 2707e2b781c5SKent Gibson cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); 2708e2b781c5SKent Gibson if (!cdev) 2709bdbbae24SBartosz Golaszewski goto out_unlock; 2710925ca369SKent Gibson 2711e2b781c5SKent Gibson cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL); 2712e2b781c5SKent Gibson if (!cdev->watched_lines) 2713e2b781c5SKent Gibson goto out_free_cdev; 2714925ca369SKent Gibson 2715e2b781c5SKent Gibson init_waitqueue_head(&cdev->wait); 2716e2b781c5SKent Gibson INIT_KFIFO(cdev->events); 2717dc0989e3SAndy Shevchenko cdev->gdev = gpio_device_get(gdev); 2718925ca369SKent Gibson 2719e2b781c5SKent Gibson cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify; 272017a7ca35SBartosz Golaszewski ret = blocking_notifier_chain_register(&gdev->line_state_notifier, 2721e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2722925ca369SKent Gibson if (ret) 2723925ca369SKent Gibson goto out_free_bitmap; 2724925ca369SKent Gibson 2725d2e2586aSBartosz Golaszewski cdev->device_unregistered_nb.notifier_call = 2726d2e2586aSBartosz Golaszewski gpio_device_unregistered_notify; 2727d2e2586aSBartosz Golaszewski ret = blocking_notifier_chain_register(&gdev->device_notifier, 2728d2e2586aSBartosz Golaszewski &cdev->device_unregistered_nb); 2729d2e2586aSBartosz Golaszewski if (ret) 2730d2e2586aSBartosz Golaszewski goto out_unregister_line_notifier; 2731d2e2586aSBartosz Golaszewski 2732e2b781c5SKent Gibson file->private_data = cdev; 2733925ca369SKent Gibson 273449bc5279SKent Gibson ret = nonseekable_open(inode, file); 2735925ca369SKent Gibson if (ret) 2736d2e2586aSBartosz Golaszewski goto out_unregister_device_notifier; 2737925ca369SKent Gibson 2738bdbbae24SBartosz Golaszewski up_read(&gdev->sem); 2739bdbbae24SBartosz Golaszewski 2740925ca369SKent Gibson return ret; 2741925ca369SKent Gibson 2742d2e2586aSBartosz Golaszewski out_unregister_device_notifier: 2743d2e2586aSBartosz Golaszewski blocking_notifier_chain_unregister(&gdev->device_notifier, 2744d2e2586aSBartosz Golaszewski &cdev->device_unregistered_nb); 2745d2e2586aSBartosz Golaszewski out_unregister_line_notifier: 274617a7ca35SBartosz Golaszewski blocking_notifier_chain_unregister(&gdev->line_state_notifier, 2747e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2748925ca369SKent Gibson out_free_bitmap: 2749dc0989e3SAndy Shevchenko gpio_device_put(gdev); 2750e2b781c5SKent Gibson bitmap_free(cdev->watched_lines); 2751e2b781c5SKent Gibson out_free_cdev: 2752e2b781c5SKent Gibson kfree(cdev); 2753bdbbae24SBartosz Golaszewski out_unlock: 2754bdbbae24SBartosz Golaszewski up_read(&gdev->sem); 2755925ca369SKent Gibson return ret; 2756925ca369SKent Gibson } 2757925ca369SKent Gibson 2758925ca369SKent Gibson /** 2759925ca369SKent Gibson * gpio_chrdev_release() - close chardev after ioctl operations 2760925ca369SKent Gibson * @inode: inode for this chardev 276149bc5279SKent Gibson * @file: file struct for storing private data 2762925ca369SKent Gibson * Returns 0 on success 2763925ca369SKent Gibson */ 276449bc5279SKent Gibson static int gpio_chrdev_release(struct inode *inode, struct file *file) 2765925ca369SKent Gibson { 2766e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2767e2b781c5SKent Gibson struct gpio_device *gdev = cdev->gdev; 2768925ca369SKent Gibson 2769e2b781c5SKent Gibson bitmap_free(cdev->watched_lines); 2770d2e2586aSBartosz Golaszewski blocking_notifier_chain_unregister(&gdev->device_notifier, 2771d2e2586aSBartosz Golaszewski &cdev->device_unregistered_nb); 277217a7ca35SBartosz Golaszewski blocking_notifier_chain_unregister(&gdev->line_state_notifier, 2773e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2774dc0989e3SAndy Shevchenko gpio_device_put(gdev); 2775e2b781c5SKent Gibson kfree(cdev); 2776925ca369SKent Gibson 2777925ca369SKent Gibson return 0; 2778925ca369SKent Gibson } 2779925ca369SKent Gibson 2780925ca369SKent Gibson static const struct file_operations gpio_fileops = { 2781925ca369SKent Gibson .release = gpio_chrdev_release, 2782925ca369SKent Gibson .open = gpio_chrdev_open, 2783925ca369SKent Gibson .poll = lineinfo_watch_poll, 2784925ca369SKent Gibson .read = lineinfo_watch_read, 2785925ca369SKent Gibson .owner = THIS_MODULE, 2786925ca369SKent Gibson .llseek = no_llseek, 2787925ca369SKent Gibson .unlocked_ioctl = gpio_ioctl, 2788925ca369SKent Gibson #ifdef CONFIG_COMPAT 2789925ca369SKent Gibson .compat_ioctl = gpio_ioctl_compat, 2790925ca369SKent Gibson #endif 2791925ca369SKent Gibson }; 2792925ca369SKent Gibson 2793925ca369SKent Gibson int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) 2794925ca369SKent Gibson { 2795925ca369SKent Gibson int ret; 2796925ca369SKent Gibson 2797925ca369SKent Gibson cdev_init(&gdev->chrdev, &gpio_fileops); 2798925ca369SKent Gibson gdev->chrdev.owner = THIS_MODULE; 2799925ca369SKent Gibson gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id); 2800925ca369SKent Gibson 2801925ca369SKent Gibson ret = cdev_device_add(&gdev->chrdev, &gdev->dev); 2802925ca369SKent Gibson if (ret) 2803925ca369SKent Gibson return ret; 2804925ca369SKent Gibson 2805925ca369SKent Gibson chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n", 2806925ca369SKent Gibson MAJOR(devt), gdev->id); 2807925ca369SKent Gibson 2808925ca369SKent Gibson return 0; 2809925ca369SKent Gibson } 2810925ca369SKent Gibson 2811925ca369SKent Gibson void gpiolib_cdev_unregister(struct gpio_device *gdev) 2812925ca369SKent Gibson { 2813925ca369SKent Gibson cdev_device_del(&gdev->chrdev, &gdev->dev); 2814a067419bSBartosz Golaszewski blocking_notifier_call_chain(&gdev->device_notifier, 0, NULL); 2815925ca369SKent Gibson } 2816