1925ca369SKent Gibson // SPDX-License-Identifier: GPL-2.0 2925ca369SKent Gibson 3d189f627SKent Gibson #include <linux/anon_inodes.h> 43c0d9c63SKent Gibson #include <linux/atomic.h> 5925ca369SKent Gibson #include <linux/bitmap.h> 63c0d9c63SKent Gibson #include <linux/build_bug.h> 7d189f627SKent Gibson #include <linux/cdev.h> 8d189f627SKent Gibson #include <linux/compat.h> 965cff704SKent Gibson #include <linux/compiler.h> 10925ca369SKent Gibson #include <linux/device.h> 11925ca369SKent Gibson #include <linux/err.h> 12d189f627SKent Gibson #include <linux/file.h> 13925ca369SKent Gibson #include <linux/gpio.h> 14925ca369SKent Gibson #include <linux/gpio/driver.h> 15d189f627SKent Gibson #include <linux/interrupt.h> 16d189f627SKent Gibson #include <linux/irqreturn.h> 17d189f627SKent Gibson #include <linux/kernel.h> 18925ca369SKent Gibson #include <linux/kfifo.h> 19d189f627SKent Gibson #include <linux/module.h> 20a54756cbSKent Gibson #include <linux/mutex.h> 21d189f627SKent Gibson #include <linux/pinctrl/consumer.h> 22925ca369SKent Gibson #include <linux/poll.h> 23d189f627SKent Gibson #include <linux/spinlock.h> 24925ca369SKent Gibson #include <linux/timekeeping.h> 25d189f627SKent Gibson #include <linux/uaccess.h> 2665cff704SKent Gibson #include <linux/workqueue.h> 27925ca369SKent Gibson #include <uapi/linux/gpio.h> 28925ca369SKent Gibson 29925ca369SKent Gibson #include "gpiolib.h" 30925ca369SKent Gibson #include "gpiolib-cdev.h" 31925ca369SKent Gibson 323c0d9c63SKent Gibson /* 333c0d9c63SKent Gibson * Array sizes must ensure 64-bit alignment and not create holes in the 343c0d9c63SKent Gibson * struct packing. 353c0d9c63SKent Gibson */ 363c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2)); 373c0d9c63SKent Gibson static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8)); 383c0d9c63SKent Gibson 393c0d9c63SKent Gibson /* 403c0d9c63SKent Gibson * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility 413c0d9c63SKent Gibson */ 423c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8)); 433c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8)); 443c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8)); 453c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8)); 463c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8)); 473c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8)); 483c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8)); 493c0d9c63SKent Gibson static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8)); 503c0d9c63SKent Gibson 51925ca369SKent Gibson /* Character device interface to GPIO. 52925ca369SKent Gibson * 53925ca369SKent Gibson * The GPIO character device, /dev/gpiochipN, provides userspace an 54925ca369SKent Gibson * interface to gpiolib GPIOs via ioctl()s. 55925ca369SKent Gibson */ 56925ca369SKent Gibson 57925ca369SKent Gibson /* 58925ca369SKent Gibson * GPIO line handle management 59925ca369SKent Gibson */ 60925ca369SKent Gibson 613c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 62925ca369SKent Gibson /** 63925ca369SKent Gibson * struct linehandle_state - contains the state of a userspace handle 64925ca369SKent Gibson * @gdev: the GPIO device the handle pertains to 65925ca369SKent Gibson * @label: consumer label used to tag descriptors 66925ca369SKent Gibson * @descs: the GPIO descriptors held by this handle 6752b7b596SKent Gibson * @num_descs: the number of descriptors held in the descs array 68925ca369SKent Gibson */ 69925ca369SKent Gibson struct linehandle_state { 70925ca369SKent Gibson struct gpio_device *gdev; 71925ca369SKent Gibson const char *label; 72925ca369SKent Gibson struct gpio_desc *descs[GPIOHANDLES_MAX]; 7352b7b596SKent Gibson u32 num_descs; 74925ca369SKent Gibson }; 75925ca369SKent Gibson 76925ca369SKent Gibson #define GPIOHANDLE_REQUEST_VALID_FLAGS \ 77925ca369SKent Gibson (GPIOHANDLE_REQUEST_INPUT | \ 78925ca369SKent Gibson GPIOHANDLE_REQUEST_OUTPUT | \ 79925ca369SKent Gibson GPIOHANDLE_REQUEST_ACTIVE_LOW | \ 80925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_UP | \ 81925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \ 82925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_DISABLE | \ 83925ca369SKent Gibson GPIOHANDLE_REQUEST_OPEN_DRAIN | \ 84925ca369SKent Gibson GPIOHANDLE_REQUEST_OPEN_SOURCE) 85925ca369SKent Gibson 86925ca369SKent Gibson static int linehandle_validate_flags(u32 flags) 87925ca369SKent Gibson { 88925ca369SKent Gibson /* Return an error if an unknown flag is set */ 89925ca369SKent Gibson if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) 90925ca369SKent Gibson return -EINVAL; 91925ca369SKent Gibson 92925ca369SKent Gibson /* 93925ca369SKent Gibson * Do not allow both INPUT & OUTPUT flags to be set as they are 94925ca369SKent Gibson * contradictory. 95925ca369SKent Gibson */ 96925ca369SKent Gibson if ((flags & GPIOHANDLE_REQUEST_INPUT) && 97925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OUTPUT)) 98925ca369SKent Gibson return -EINVAL; 99925ca369SKent Gibson 100925ca369SKent Gibson /* 101925ca369SKent Gibson * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If 102925ca369SKent Gibson * the hardware actually supports enabling both at the same time the 103925ca369SKent Gibson * electrical result would be disastrous. 104925ca369SKent Gibson */ 105925ca369SKent Gibson if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) && 106925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) 107925ca369SKent Gibson return -EINVAL; 108925ca369SKent Gibson 109925ca369SKent Gibson /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */ 110925ca369SKent Gibson if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) && 111925ca369SKent Gibson ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || 112925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))) 113925ca369SKent Gibson return -EINVAL; 114925ca369SKent Gibson 115925ca369SKent Gibson /* Bias flags only allowed for input or output mode. */ 116925ca369SKent Gibson if (!((flags & GPIOHANDLE_REQUEST_INPUT) || 117925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_OUTPUT)) && 118925ca369SKent Gibson ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) || 119925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) || 120925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN))) 121925ca369SKent Gibson return -EINVAL; 122925ca369SKent Gibson 123925ca369SKent Gibson /* Only one bias flag can be set. */ 124925ca369SKent Gibson if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && 125925ca369SKent Gibson (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | 126925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || 127925ca369SKent Gibson ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && 128925ca369SKent Gibson (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) 129925ca369SKent Gibson return -EINVAL; 130925ca369SKent Gibson 131925ca369SKent Gibson return 0; 132925ca369SKent Gibson } 133925ca369SKent Gibson 134c274b58aSKent Gibson static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp) 135c274b58aSKent Gibson { 136c274b58aSKent Gibson assign_bit(FLAG_ACTIVE_LOW, flagsp, 137c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW); 138c274b58aSKent Gibson assign_bit(FLAG_OPEN_DRAIN, flagsp, 139c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN); 140c274b58aSKent Gibson assign_bit(FLAG_OPEN_SOURCE, flagsp, 141c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE); 142c274b58aSKent Gibson assign_bit(FLAG_PULL_UP, flagsp, 143c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP); 144c274b58aSKent Gibson assign_bit(FLAG_PULL_DOWN, flagsp, 145c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN); 146c274b58aSKent Gibson assign_bit(FLAG_BIAS_DISABLE, flagsp, 147c274b58aSKent Gibson lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE); 148c274b58aSKent Gibson } 149c274b58aSKent Gibson 150925ca369SKent Gibson static long linehandle_set_config(struct linehandle_state *lh, 151925ca369SKent Gibson void __user *ip) 152925ca369SKent Gibson { 153925ca369SKent Gibson struct gpiohandle_config gcnf; 154925ca369SKent Gibson struct gpio_desc *desc; 155925ca369SKent Gibson int i, ret; 156925ca369SKent Gibson u32 lflags; 157925ca369SKent Gibson 158925ca369SKent Gibson if (copy_from_user(&gcnf, ip, sizeof(gcnf))) 159925ca369SKent Gibson return -EFAULT; 160925ca369SKent Gibson 161925ca369SKent Gibson lflags = gcnf.flags; 162925ca369SKent Gibson ret = linehandle_validate_flags(lflags); 163925ca369SKent Gibson if (ret) 164925ca369SKent Gibson return ret; 165925ca369SKent Gibson 16652b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) { 167925ca369SKent Gibson desc = lh->descs[i]; 168c274b58aSKent Gibson linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags); 169925ca369SKent Gibson 170925ca369SKent Gibson /* 171925ca369SKent Gibson * Lines have to be requested explicitly for input 172925ca369SKent Gibson * or output, else the line will be treated "as is". 173925ca369SKent Gibson */ 174925ca369SKent Gibson if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { 175925ca369SKent Gibson int val = !!gcnf.default_values[i]; 176925ca369SKent Gibson 177925ca369SKent Gibson ret = gpiod_direction_output(desc, val); 178925ca369SKent Gibson if (ret) 179925ca369SKent Gibson return ret; 180925ca369SKent Gibson } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { 181925ca369SKent Gibson ret = gpiod_direction_input(desc); 182925ca369SKent Gibson if (ret) 183925ca369SKent Gibson return ret; 184925ca369SKent Gibson } 185925ca369SKent Gibson 1866accc376SKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 187aad95584SKent Gibson GPIO_V2_LINE_CHANGED_CONFIG, 188aad95584SKent Gibson desc); 189925ca369SKent Gibson } 190925ca369SKent Gibson return 0; 191925ca369SKent Gibson } 192925ca369SKent Gibson 19349bc5279SKent Gibson static long linehandle_ioctl(struct file *file, unsigned int cmd, 194925ca369SKent Gibson unsigned long arg) 195925ca369SKent Gibson { 19649bc5279SKent Gibson struct linehandle_state *lh = file->private_data; 197925ca369SKent Gibson void __user *ip = (void __user *)arg; 198925ca369SKent Gibson struct gpiohandle_data ghd; 199925ca369SKent Gibson DECLARE_BITMAP(vals, GPIOHANDLES_MAX); 200925ca369SKent Gibson int i; 201925ca369SKent Gibson 202925ca369SKent Gibson if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { 203925ca369SKent Gibson /* NOTE: It's ok to read values of output lines. */ 204925ca369SKent Gibson int ret = gpiod_get_array_value_complex(false, 205925ca369SKent Gibson true, 20652b7b596SKent Gibson lh->num_descs, 207925ca369SKent Gibson lh->descs, 208925ca369SKent Gibson NULL, 209925ca369SKent Gibson vals); 210925ca369SKent Gibson if (ret) 211925ca369SKent Gibson return ret; 212925ca369SKent Gibson 213925ca369SKent Gibson memset(&ghd, 0, sizeof(ghd)); 21452b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) 215925ca369SKent Gibson ghd.values[i] = test_bit(i, vals); 216925ca369SKent Gibson 217925ca369SKent Gibson if (copy_to_user(ip, &ghd, sizeof(ghd))) 218925ca369SKent Gibson return -EFAULT; 219925ca369SKent Gibson 220925ca369SKent Gibson return 0; 221925ca369SKent Gibson } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) { 222925ca369SKent Gibson /* 223925ca369SKent Gibson * All line descriptors were created at once with the same 224925ca369SKent Gibson * flags so just check if the first one is really output. 225925ca369SKent Gibson */ 226925ca369SKent Gibson if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags)) 227925ca369SKent Gibson return -EPERM; 228925ca369SKent Gibson 229925ca369SKent Gibson if (copy_from_user(&ghd, ip, sizeof(ghd))) 230925ca369SKent Gibson return -EFAULT; 231925ca369SKent Gibson 232925ca369SKent Gibson /* Clamp all values to [0,1] */ 23352b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) 234925ca369SKent Gibson __assign_bit(i, vals, ghd.values[i]); 235925ca369SKent Gibson 236925ca369SKent Gibson /* Reuse the array setting function */ 237925ca369SKent Gibson return gpiod_set_array_value_complex(false, 238925ca369SKent Gibson true, 23952b7b596SKent Gibson lh->num_descs, 240925ca369SKent Gibson lh->descs, 241925ca369SKent Gibson NULL, 242925ca369SKent Gibson vals); 243925ca369SKent Gibson } else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) { 244925ca369SKent Gibson return linehandle_set_config(lh, ip); 245925ca369SKent Gibson } 246925ca369SKent Gibson return -EINVAL; 247925ca369SKent Gibson } 248925ca369SKent Gibson 249925ca369SKent Gibson #ifdef CONFIG_COMPAT 25049bc5279SKent Gibson static long linehandle_ioctl_compat(struct file *file, unsigned int cmd, 251925ca369SKent Gibson unsigned long arg) 252925ca369SKent Gibson { 25349bc5279SKent Gibson return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 254925ca369SKent Gibson } 255925ca369SKent Gibson #endif 256925ca369SKent Gibson 257883f9198SKent Gibson static void linehandle_free(struct linehandle_state *lh) 258925ca369SKent Gibson { 259925ca369SKent Gibson int i; 260925ca369SKent Gibson 26152b7b596SKent Gibson for (i = 0; i < lh->num_descs; i++) 262883f9198SKent Gibson if (lh->descs[i]) 263925ca369SKent Gibson gpiod_free(lh->descs[i]); 264925ca369SKent Gibson kfree(lh->label); 265883f9198SKent Gibson put_device(&lh->gdev->dev); 266925ca369SKent Gibson kfree(lh); 267883f9198SKent Gibson } 268883f9198SKent Gibson 269883f9198SKent Gibson static int linehandle_release(struct inode *inode, struct file *file) 270883f9198SKent Gibson { 271883f9198SKent Gibson linehandle_free(file->private_data); 272925ca369SKent Gibson return 0; 273925ca369SKent Gibson } 274925ca369SKent Gibson 275925ca369SKent Gibson static const struct file_operations linehandle_fileops = { 276925ca369SKent Gibson .release = linehandle_release, 277925ca369SKent Gibson .owner = THIS_MODULE, 278925ca369SKent Gibson .llseek = noop_llseek, 279925ca369SKent Gibson .unlocked_ioctl = linehandle_ioctl, 280925ca369SKent Gibson #ifdef CONFIG_COMPAT 281925ca369SKent Gibson .compat_ioctl = linehandle_ioctl_compat, 282925ca369SKent Gibson #endif 283925ca369SKent Gibson }; 284925ca369SKent Gibson 285925ca369SKent Gibson static int linehandle_create(struct gpio_device *gdev, void __user *ip) 286925ca369SKent Gibson { 287925ca369SKent Gibson struct gpiohandle_request handlereq; 288925ca369SKent Gibson struct linehandle_state *lh; 289925ca369SKent Gibson struct file *file; 290883f9198SKent Gibson int fd, i, ret; 291925ca369SKent Gibson u32 lflags; 292925ca369SKent Gibson 293925ca369SKent Gibson if (copy_from_user(&handlereq, ip, sizeof(handlereq))) 294925ca369SKent Gibson return -EFAULT; 295925ca369SKent Gibson if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) 296925ca369SKent Gibson return -EINVAL; 297925ca369SKent Gibson 298925ca369SKent Gibson lflags = handlereq.flags; 299925ca369SKent Gibson 300925ca369SKent Gibson ret = linehandle_validate_flags(lflags); 301925ca369SKent Gibson if (ret) 302925ca369SKent Gibson return ret; 303925ca369SKent Gibson 304925ca369SKent Gibson lh = kzalloc(sizeof(*lh), GFP_KERNEL); 305925ca369SKent Gibson if (!lh) 306925ca369SKent Gibson return -ENOMEM; 307925ca369SKent Gibson lh->gdev = gdev; 308925ca369SKent Gibson get_device(&gdev->dev); 309925ca369SKent Gibson 310f188ac12SKent Gibson if (handlereq.consumer_label[0] != '\0') { 311f188ac12SKent Gibson /* label is only initialized if consumer_label is set */ 312f188ac12SKent Gibson lh->label = kstrndup(handlereq.consumer_label, 313f188ac12SKent Gibson sizeof(handlereq.consumer_label) - 1, 314925ca369SKent Gibson GFP_KERNEL); 315925ca369SKent Gibson if (!lh->label) { 316925ca369SKent Gibson ret = -ENOMEM; 317925ca369SKent Gibson goto out_free_lh; 318925ca369SKent Gibson } 319925ca369SKent Gibson } 320925ca369SKent Gibson 321883f9198SKent Gibson lh->num_descs = handlereq.lines; 322883f9198SKent Gibson 323925ca369SKent Gibson /* Request each GPIO */ 324925ca369SKent Gibson for (i = 0; i < handlereq.lines; i++) { 325925ca369SKent Gibson u32 offset = handlereq.lineoffsets[i]; 326925ca369SKent Gibson struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); 327925ca369SKent Gibson 328925ca369SKent Gibson if (IS_ERR(desc)) { 329925ca369SKent Gibson ret = PTR_ERR(desc); 330883f9198SKent Gibson goto out_free_lh; 331925ca369SKent Gibson } 332925ca369SKent Gibson 333925ca369SKent Gibson ret = gpiod_request(desc, lh->label); 334925ca369SKent Gibson if (ret) 335883f9198SKent Gibson goto out_free_lh; 336925ca369SKent Gibson lh->descs[i] = desc; 337c274b58aSKent Gibson linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags); 338925ca369SKent Gibson 339925ca369SKent Gibson ret = gpiod_set_transitory(desc, false); 340925ca369SKent Gibson if (ret < 0) 341883f9198SKent Gibson goto out_free_lh; 342925ca369SKent Gibson 343925ca369SKent Gibson /* 344925ca369SKent Gibson * Lines have to be requested explicitly for input 345925ca369SKent Gibson * or output, else the line will be treated "as is". 346925ca369SKent Gibson */ 347925ca369SKent Gibson if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { 348925ca369SKent Gibson int val = !!handlereq.default_values[i]; 349925ca369SKent Gibson 350925ca369SKent Gibson ret = gpiod_direction_output(desc, val); 351925ca369SKent Gibson if (ret) 352883f9198SKent Gibson goto out_free_lh; 353925ca369SKent Gibson } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { 354925ca369SKent Gibson ret = gpiod_direction_input(desc); 355925ca369SKent Gibson if (ret) 356883f9198SKent Gibson goto out_free_lh; 357925ca369SKent Gibson } 358925ca369SKent Gibson 3596accc376SKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 360aad95584SKent Gibson GPIO_V2_LINE_CHANGED_REQUESTED, desc); 361925ca369SKent Gibson 362925ca369SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", 363925ca369SKent Gibson offset); 364925ca369SKent Gibson } 365925ca369SKent Gibson 366925ca369SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 367925ca369SKent Gibson if (fd < 0) { 368925ca369SKent Gibson ret = fd; 369883f9198SKent Gibson goto out_free_lh; 370925ca369SKent Gibson } 371925ca369SKent Gibson 372925ca369SKent Gibson file = anon_inode_getfile("gpio-linehandle", 373925ca369SKent Gibson &linehandle_fileops, 374925ca369SKent Gibson lh, 375925ca369SKent Gibson O_RDONLY | O_CLOEXEC); 376925ca369SKent Gibson if (IS_ERR(file)) { 377925ca369SKent Gibson ret = PTR_ERR(file); 378925ca369SKent Gibson goto out_put_unused_fd; 379925ca369SKent Gibson } 380925ca369SKent Gibson 381925ca369SKent Gibson handlereq.fd = fd; 382925ca369SKent Gibson if (copy_to_user(ip, &handlereq, sizeof(handlereq))) { 383925ca369SKent Gibson /* 384925ca369SKent Gibson * fput() will trigger the release() callback, so do not go onto 385925ca369SKent Gibson * the regular error cleanup path here. 386925ca369SKent Gibson */ 387925ca369SKent Gibson fput(file); 388925ca369SKent Gibson put_unused_fd(fd); 389925ca369SKent Gibson return -EFAULT; 390925ca369SKent Gibson } 391925ca369SKent Gibson 392925ca369SKent Gibson fd_install(fd, file); 393925ca369SKent Gibson 394925ca369SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", 39552b7b596SKent Gibson lh->num_descs); 396925ca369SKent Gibson 397925ca369SKent Gibson return 0; 398925ca369SKent Gibson 399925ca369SKent Gibson out_put_unused_fd: 400925ca369SKent Gibson put_unused_fd(fd); 401925ca369SKent Gibson out_free_lh: 402883f9198SKent Gibson linehandle_free(lh); 403925ca369SKent Gibson return ret; 404925ca369SKent Gibson } 4053c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 4063c0d9c63SKent Gibson 4073c0d9c63SKent Gibson /** 4083c0d9c63SKent Gibson * struct line - contains the state of a requested line 4093c0d9c63SKent Gibson * @desc: the GPIO descriptor for this line. 41073e03419SKent Gibson * @req: the corresponding line request 41173e03419SKent Gibson * @irq: the interrupt triggered in response to events on this GPIO 41273e03419SKent Gibson * @eflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or 41373e03419SKent Gibson * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied 41473e03419SKent Gibson * @timestamp_ns: cache for the timestamp storing it between hardirq and 41573e03419SKent Gibson * IRQ thread, used to bring the timestamp close to the actual event 41673e03419SKent Gibson * @req_seqno: the seqno for the current edge event in the sequence of 41773e03419SKent Gibson * events for the corresponding line request. This is drawn from the @req. 41873e03419SKent Gibson * @line_seqno: the seqno for the current edge event in the sequence of 41973e03419SKent Gibson * events for this line. 42065cff704SKent Gibson * @work: the worker that implements software debouncing 42165cff704SKent Gibson * @sw_debounced: flag indicating if the software debouncer is active 42265cff704SKent Gibson * @level: the current debounced physical level of the line 4233c0d9c63SKent Gibson */ 4243c0d9c63SKent Gibson struct line { 4253c0d9c63SKent Gibson struct gpio_desc *desc; 42673e03419SKent Gibson /* 42773e03419SKent Gibson * -- edge detector specific fields -- 42873e03419SKent Gibson */ 42973e03419SKent Gibson struct linereq *req; 43073e03419SKent Gibson unsigned int irq; 43173e03419SKent Gibson u64 eflags; 43273e03419SKent Gibson /* 43373e03419SKent Gibson * timestamp_ns and req_seqno are accessed only by 43473e03419SKent Gibson * edge_irq_handler() and edge_irq_thread(), which are themselves 43573e03419SKent Gibson * mutually exclusive, so no additional protection is necessary. 43673e03419SKent Gibson */ 43773e03419SKent Gibson u64 timestamp_ns; 43873e03419SKent Gibson u32 req_seqno; 43965cff704SKent Gibson /* 44065cff704SKent Gibson * line_seqno is accessed by either edge_irq_thread() or 44165cff704SKent Gibson * debounce_work_func(), which are themselves mutually exclusive, 44265cff704SKent Gibson * so no additional protection is necessary. 44365cff704SKent Gibson */ 44473e03419SKent Gibson u32 line_seqno; 44565cff704SKent Gibson /* 44665cff704SKent Gibson * -- debouncer specific fields -- 44765cff704SKent Gibson */ 44865cff704SKent Gibson struct delayed_work work; 44965cff704SKent Gibson /* 45065cff704SKent Gibson * sw_debounce is accessed by linereq_set_config(), which is the 45165cff704SKent Gibson * only setter, and linereq_get_values(), which can live with a 45265cff704SKent Gibson * slightly stale value. 45365cff704SKent Gibson */ 45465cff704SKent Gibson unsigned int sw_debounced; 45565cff704SKent Gibson /* 45665cff704SKent Gibson * level is accessed by debounce_work_func(), which is the only 45765cff704SKent Gibson * setter, and linereq_get_values() which can live with a slightly 45865cff704SKent Gibson * stale value. 45965cff704SKent Gibson */ 46065cff704SKent Gibson unsigned int level; 4613c0d9c63SKent Gibson }; 4623c0d9c63SKent Gibson 4633c0d9c63SKent Gibson /** 4643c0d9c63SKent Gibson * struct linereq - contains the state of a userspace line request 4653c0d9c63SKent Gibson * @gdev: the GPIO device the line request pertains to 4663c0d9c63SKent Gibson * @label: consumer label used to tag GPIO descriptors 4673c0d9c63SKent Gibson * @num_lines: the number of lines in the lines array 46873e03419SKent Gibson * @wait: wait queue that handles blocking reads of events 46973e03419SKent Gibson * @event_buffer_size: the number of elements allocated in @events 47073e03419SKent Gibson * @events: KFIFO for the GPIO events 47173e03419SKent Gibson * @seqno: the sequence number for edge events generated on all lines in 47273e03419SKent Gibson * this line request. Note that this is not used when @num_lines is 1, as 47373e03419SKent Gibson * the line_seqno is then the same and is cheaper to calculate. 474a54756cbSKent Gibson * @config_mutex: mutex for serializing ioctl() calls to ensure consistency 475a54756cbSKent Gibson * of configuration, particularly multi-step accesses to desc flags. 4763c0d9c63SKent Gibson * @lines: the lines held by this line request, with @num_lines elements. 4773c0d9c63SKent Gibson */ 4783c0d9c63SKent Gibson struct linereq { 4793c0d9c63SKent Gibson struct gpio_device *gdev; 4803c0d9c63SKent Gibson const char *label; 4813c0d9c63SKent Gibson u32 num_lines; 48273e03419SKent Gibson wait_queue_head_t wait; 48373e03419SKent Gibson u32 event_buffer_size; 48473e03419SKent Gibson DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); 48573e03419SKent Gibson atomic_t seqno; 486a54756cbSKent Gibson struct mutex config_mutex; 4873c0d9c63SKent Gibson struct line lines[]; 4883c0d9c63SKent Gibson }; 4893c0d9c63SKent Gibson 4903c0d9c63SKent Gibson #define GPIO_V2_LINE_BIAS_FLAGS \ 4913c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ 4923c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \ 4933c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_DISABLED) 4943c0d9c63SKent Gibson 4953c0d9c63SKent Gibson #define GPIO_V2_LINE_DIRECTION_FLAGS \ 4963c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_INPUT | \ 4973c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_OUTPUT) 4983c0d9c63SKent Gibson 4993c0d9c63SKent Gibson #define GPIO_V2_LINE_DRIVE_FLAGS \ 5003c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \ 5013c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_OPEN_SOURCE) 5023c0d9c63SKent Gibson 50373e03419SKent Gibson #define GPIO_V2_LINE_EDGE_FLAGS \ 50473e03419SKent Gibson (GPIO_V2_LINE_FLAG_EDGE_RISING | \ 50573e03419SKent Gibson GPIO_V2_LINE_FLAG_EDGE_FALLING) 50673e03419SKent Gibson 5073c0d9c63SKent Gibson #define GPIO_V2_LINE_VALID_FLAGS \ 5083c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ 5093c0d9c63SKent Gibson GPIO_V2_LINE_DIRECTION_FLAGS | \ 5103c0d9c63SKent Gibson GPIO_V2_LINE_DRIVE_FLAGS | \ 51173e03419SKent Gibson GPIO_V2_LINE_EDGE_FLAGS | \ 51226d060e4SKent Gibson GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \ 5133c0d9c63SKent Gibson GPIO_V2_LINE_BIAS_FLAGS) 5143c0d9c63SKent Gibson 51573e03419SKent Gibson static void linereq_put_event(struct linereq *lr, 51673e03419SKent Gibson struct gpio_v2_line_event *le) 51773e03419SKent Gibson { 51873e03419SKent Gibson bool overflow = false; 51973e03419SKent Gibson 52073e03419SKent Gibson spin_lock(&lr->wait.lock); 52173e03419SKent Gibson if (kfifo_is_full(&lr->events)) { 52273e03419SKent Gibson overflow = true; 52373e03419SKent Gibson kfifo_skip(&lr->events); 52473e03419SKent Gibson } 52573e03419SKent Gibson kfifo_in(&lr->events, le, 1); 52673e03419SKent Gibson spin_unlock(&lr->wait.lock); 52773e03419SKent Gibson if (!overflow) 52873e03419SKent Gibson wake_up_poll(&lr->wait, EPOLLIN); 52973e03419SKent Gibson else 53073e03419SKent Gibson pr_debug_ratelimited("event FIFO is full - event dropped\n"); 53173e03419SKent Gibson } 53273e03419SKent Gibson 53326d060e4SKent Gibson static u64 line_event_timestamp(struct line *line) 53426d060e4SKent Gibson { 53526d060e4SKent Gibson if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags)) 53626d060e4SKent Gibson return ktime_get_real_ns(); 53726d060e4SKent Gibson 53826d060e4SKent Gibson return ktime_get_ns(); 53926d060e4SKent Gibson } 54026d060e4SKent Gibson 54173e03419SKent Gibson static irqreturn_t edge_irq_thread(int irq, void *p) 54273e03419SKent Gibson { 54373e03419SKent Gibson struct line *line = p; 54473e03419SKent Gibson struct linereq *lr = line->req; 54573e03419SKent Gibson struct gpio_v2_line_event le; 54673e03419SKent Gibson 54773e03419SKent Gibson /* Do not leak kernel stack to userspace */ 54873e03419SKent Gibson memset(&le, 0, sizeof(le)); 54973e03419SKent Gibson 55073e03419SKent Gibson if (line->timestamp_ns) { 55173e03419SKent Gibson le.timestamp_ns = line->timestamp_ns; 55273e03419SKent Gibson } else { 55373e03419SKent Gibson /* 55473e03419SKent Gibson * We may be running from a nested threaded interrupt in 55573e03419SKent Gibson * which case we didn't get the timestamp from 55673e03419SKent Gibson * edge_irq_handler(). 55773e03419SKent Gibson */ 55826d060e4SKent Gibson le.timestamp_ns = line_event_timestamp(line); 55973e03419SKent Gibson if (lr->num_lines != 1) 56073e03419SKent Gibson line->req_seqno = atomic_inc_return(&lr->seqno); 56173e03419SKent Gibson } 56273e03419SKent Gibson line->timestamp_ns = 0; 56373e03419SKent Gibson 56473e03419SKent Gibson if (line->eflags == (GPIO_V2_LINE_FLAG_EDGE_RISING | 56573e03419SKent Gibson GPIO_V2_LINE_FLAG_EDGE_FALLING)) { 56673e03419SKent Gibson int level = gpiod_get_value_cansleep(line->desc); 56773e03419SKent Gibson 56873e03419SKent Gibson if (level) 56973e03419SKent Gibson /* Emit low-to-high event */ 57073e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 57173e03419SKent Gibson else 57273e03419SKent Gibson /* Emit high-to-low event */ 57373e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 57473e03419SKent Gibson } else if (line->eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) { 57573e03419SKent Gibson /* Emit low-to-high event */ 57673e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 57773e03419SKent Gibson } else if (line->eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) { 57873e03419SKent Gibson /* Emit high-to-low event */ 57973e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 58073e03419SKent Gibson } else { 58173e03419SKent Gibson return IRQ_NONE; 58273e03419SKent Gibson } 58373e03419SKent Gibson line->line_seqno++; 58473e03419SKent Gibson le.line_seqno = line->line_seqno; 58573e03419SKent Gibson le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno; 58673e03419SKent Gibson le.offset = gpio_chip_hwgpio(line->desc); 58773e03419SKent Gibson 58873e03419SKent Gibson linereq_put_event(lr, &le); 58973e03419SKent Gibson 59073e03419SKent Gibson return IRQ_HANDLED; 59173e03419SKent Gibson } 59273e03419SKent Gibson 59373e03419SKent Gibson static irqreturn_t edge_irq_handler(int irq, void *p) 59473e03419SKent Gibson { 59573e03419SKent Gibson struct line *line = p; 59673e03419SKent Gibson struct linereq *lr = line->req; 59773e03419SKent Gibson 59873e03419SKent Gibson /* 59973e03419SKent Gibson * Just store the timestamp in hardirq context so we get it as 60073e03419SKent Gibson * close in time as possible to the actual event. 60173e03419SKent Gibson */ 60226d060e4SKent Gibson line->timestamp_ns = line_event_timestamp(line); 60373e03419SKent Gibson 60473e03419SKent Gibson if (lr->num_lines != 1) 60573e03419SKent Gibson line->req_seqno = atomic_inc_return(&lr->seqno); 60673e03419SKent Gibson 60773e03419SKent Gibson return IRQ_WAKE_THREAD; 60873e03419SKent Gibson } 60973e03419SKent Gibson 61065cff704SKent Gibson /* 61165cff704SKent Gibson * returns the current debounced logical value. 61265cff704SKent Gibson */ 61365cff704SKent Gibson static bool debounced_value(struct line *line) 61465cff704SKent Gibson { 61565cff704SKent Gibson bool value; 61665cff704SKent Gibson 61765cff704SKent Gibson /* 61865cff704SKent Gibson * minor race - debouncer may be stopped here, so edge_detector_stop() 61965cff704SKent Gibson * must leave the value unchanged so the following will read the level 62065cff704SKent Gibson * from when the debouncer was last running. 62165cff704SKent Gibson */ 62265cff704SKent Gibson value = READ_ONCE(line->level); 62365cff704SKent Gibson 62465cff704SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags)) 62565cff704SKent Gibson value = !value; 62665cff704SKent Gibson 62765cff704SKent Gibson return value; 62865cff704SKent Gibson } 62965cff704SKent Gibson 63065cff704SKent Gibson static irqreturn_t debounce_irq_handler(int irq, void *p) 63165cff704SKent Gibson { 63265cff704SKent Gibson struct line *line = p; 63365cff704SKent Gibson 63465cff704SKent Gibson mod_delayed_work(system_wq, &line->work, 63565cff704SKent Gibson usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us))); 63665cff704SKent Gibson 63765cff704SKent Gibson return IRQ_HANDLED; 63865cff704SKent Gibson } 63965cff704SKent Gibson 64065cff704SKent Gibson static void debounce_work_func(struct work_struct *work) 64165cff704SKent Gibson { 64265cff704SKent Gibson struct gpio_v2_line_event le; 64365cff704SKent Gibson struct line *line = container_of(work, struct line, work.work); 64465cff704SKent Gibson struct linereq *lr; 64565cff704SKent Gibson int level; 64665cff704SKent Gibson 64765cff704SKent Gibson level = gpiod_get_raw_value_cansleep(line->desc); 64865cff704SKent Gibson if (level < 0) { 64965cff704SKent Gibson pr_debug_ratelimited("debouncer failed to read line value\n"); 65065cff704SKent Gibson return; 65165cff704SKent Gibson } 65265cff704SKent Gibson 65365cff704SKent Gibson if (READ_ONCE(line->level) == level) 65465cff704SKent Gibson return; 65565cff704SKent Gibson 65665cff704SKent Gibson WRITE_ONCE(line->level, level); 65765cff704SKent Gibson 65865cff704SKent Gibson /* -- edge detection -- */ 65965cff704SKent Gibson if (!line->eflags) 66065cff704SKent Gibson return; 66165cff704SKent Gibson 66265cff704SKent Gibson /* switch from physical level to logical - if they differ */ 66365cff704SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags)) 66465cff704SKent Gibson level = !level; 66565cff704SKent Gibson 66665cff704SKent Gibson /* ignore edges that are not being monitored */ 66765cff704SKent Gibson if (((line->eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) || 66865cff704SKent Gibson ((line->eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level)) 66965cff704SKent Gibson return; 67065cff704SKent Gibson 67165cff704SKent Gibson /* Do not leak kernel stack to userspace */ 67265cff704SKent Gibson memset(&le, 0, sizeof(le)); 67365cff704SKent Gibson 67465cff704SKent Gibson lr = line->req; 67526d060e4SKent Gibson le.timestamp_ns = line_event_timestamp(line); 67665cff704SKent Gibson le.offset = gpio_chip_hwgpio(line->desc); 67765cff704SKent Gibson line->line_seqno++; 67865cff704SKent Gibson le.line_seqno = line->line_seqno; 67965cff704SKent Gibson le.seqno = (lr->num_lines == 1) ? 68065cff704SKent Gibson le.line_seqno : atomic_inc_return(&lr->seqno); 68165cff704SKent Gibson 68265cff704SKent Gibson if (level) 68365cff704SKent Gibson /* Emit low-to-high event */ 68465cff704SKent Gibson le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 68565cff704SKent Gibson else 68665cff704SKent Gibson /* Emit high-to-low event */ 68765cff704SKent Gibson le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 68865cff704SKent Gibson 68965cff704SKent Gibson linereq_put_event(lr, &le); 69065cff704SKent Gibson } 69165cff704SKent Gibson 69265cff704SKent Gibson static int debounce_setup(struct line *line, 69365cff704SKent Gibson unsigned int debounce_period_us) 69465cff704SKent Gibson { 69565cff704SKent Gibson unsigned long irqflags; 69665cff704SKent Gibson int ret, level, irq; 69765cff704SKent Gibson 69865cff704SKent Gibson /* try hardware */ 69965cff704SKent Gibson ret = gpiod_set_debounce(line->desc, debounce_period_us); 70065cff704SKent Gibson if (!ret) { 70165cff704SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 70265cff704SKent Gibson return ret; 70365cff704SKent Gibson } 70465cff704SKent Gibson if (ret != -ENOTSUPP) 70565cff704SKent Gibson return ret; 70665cff704SKent Gibson 70765cff704SKent Gibson if (debounce_period_us) { 70865cff704SKent Gibson /* setup software debounce */ 70965cff704SKent Gibson level = gpiod_get_raw_value_cansleep(line->desc); 71065cff704SKent Gibson if (level < 0) 71165cff704SKent Gibson return level; 71265cff704SKent Gibson 71365cff704SKent Gibson irq = gpiod_to_irq(line->desc); 71465cff704SKent Gibson if (irq < 0) 71565cff704SKent Gibson return -ENXIO; 71665cff704SKent Gibson 71765cff704SKent Gibson WRITE_ONCE(line->level, level); 71865cff704SKent Gibson irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING; 71965cff704SKent Gibson ret = request_irq(irq, debounce_irq_handler, irqflags, 72065cff704SKent Gibson line->req->label, line); 72165cff704SKent Gibson if (ret) 72265cff704SKent Gibson return ret; 72365cff704SKent Gibson 72465cff704SKent Gibson WRITE_ONCE(line->sw_debounced, 1); 72565cff704SKent Gibson line->irq = irq; 72665cff704SKent Gibson } 72765cff704SKent Gibson return 0; 72865cff704SKent Gibson } 72965cff704SKent Gibson 73065cff704SKent Gibson static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc, 73165cff704SKent Gibson unsigned int line_idx) 73265cff704SKent Gibson { 73365cff704SKent Gibson unsigned int i; 73465cff704SKent Gibson u64 mask = BIT_ULL(line_idx); 73565cff704SKent Gibson 73665cff704SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 73765cff704SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && 73865cff704SKent Gibson (lc->attrs[i].mask & mask)) 73965cff704SKent Gibson return true; 74065cff704SKent Gibson } 74165cff704SKent Gibson return false; 74265cff704SKent Gibson } 74365cff704SKent Gibson 74465cff704SKent Gibson static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc, 74565cff704SKent Gibson unsigned int line_idx) 74665cff704SKent Gibson { 74765cff704SKent Gibson unsigned int i; 74865cff704SKent Gibson u64 mask = BIT_ULL(line_idx); 74965cff704SKent Gibson 75065cff704SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 75165cff704SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && 75265cff704SKent Gibson (lc->attrs[i].mask & mask)) 75365cff704SKent Gibson return lc->attrs[i].attr.debounce_period_us; 75465cff704SKent Gibson } 75565cff704SKent Gibson return 0; 75665cff704SKent Gibson } 75765cff704SKent Gibson 75873e03419SKent Gibson static void edge_detector_stop(struct line *line) 75973e03419SKent Gibson { 76073e03419SKent Gibson if (line->irq) { 76173e03419SKent Gibson free_irq(line->irq, line); 76273e03419SKent Gibson line->irq = 0; 76373e03419SKent Gibson } 764a54756cbSKent Gibson 76565cff704SKent Gibson cancel_delayed_work_sync(&line->work); 76665cff704SKent Gibson WRITE_ONCE(line->sw_debounced, 0); 767a54756cbSKent Gibson line->eflags = 0; 76865cff704SKent Gibson /* do not change line->level - see comment in debounced_value() */ 76973e03419SKent Gibson } 77073e03419SKent Gibson 77173e03419SKent Gibson static int edge_detector_setup(struct line *line, 77265cff704SKent Gibson struct gpio_v2_line_config *lc, 77365cff704SKent Gibson unsigned int line_idx, 77473e03419SKent Gibson u64 eflags) 77573e03419SKent Gibson { 77665cff704SKent Gibson u32 debounce_period_us; 77773e03419SKent Gibson unsigned long irqflags = 0; 77873e03419SKent Gibson int irq, ret; 77973e03419SKent Gibson 78073e03419SKent Gibson if (eflags && !kfifo_initialized(&line->req->events)) { 78173e03419SKent Gibson ret = kfifo_alloc(&line->req->events, 78273e03419SKent Gibson line->req->event_buffer_size, GFP_KERNEL); 78373e03419SKent Gibson if (ret) 78473e03419SKent Gibson return ret; 78573e03419SKent Gibson } 78673e03419SKent Gibson line->eflags = eflags; 78765cff704SKent Gibson if (gpio_v2_line_config_debounced(lc, line_idx)) { 78865cff704SKent Gibson debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx); 78965cff704SKent Gibson ret = debounce_setup(line, debounce_period_us); 79065cff704SKent Gibson if (ret) 79165cff704SKent Gibson return ret; 79265cff704SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 79365cff704SKent Gibson } 79473e03419SKent Gibson 79565cff704SKent Gibson /* detection disabled or sw debouncer will provide edge detection */ 79665cff704SKent Gibson if (!eflags || READ_ONCE(line->sw_debounced)) 79773e03419SKent Gibson return 0; 79873e03419SKent Gibson 79973e03419SKent Gibson irq = gpiod_to_irq(line->desc); 80073e03419SKent Gibson if (irq < 0) 80173e03419SKent Gibson return -ENXIO; 80273e03419SKent Gibson 80373e03419SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) 80473e03419SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 80573e03419SKent Gibson IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 80673e03419SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) 80773e03419SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 80873e03419SKent Gibson IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 80973e03419SKent Gibson irqflags |= IRQF_ONESHOT; 81073e03419SKent Gibson 81173e03419SKent Gibson /* Request a thread to read the events */ 81273e03419SKent Gibson ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread, 81373e03419SKent Gibson irqflags, line->req->label, line); 81473e03419SKent Gibson if (ret) 81573e03419SKent Gibson return ret; 81673e03419SKent Gibson 81773e03419SKent Gibson line->irq = irq; 81873e03419SKent Gibson return 0; 81973e03419SKent Gibson } 82073e03419SKent Gibson 82165cff704SKent Gibson static int edge_detector_update(struct line *line, 82265cff704SKent Gibson struct gpio_v2_line_config *lc, 82365cff704SKent Gibson unsigned int line_idx, 82465cff704SKent Gibson u64 eflags, bool polarity_change) 825a54756cbSKent Gibson { 82665cff704SKent Gibson unsigned int debounce_period_us = 82765cff704SKent Gibson gpio_v2_line_config_debounce_period(lc, line_idx); 82865cff704SKent Gibson 82965cff704SKent Gibson if ((line->eflags == eflags) && !polarity_change && 83065cff704SKent Gibson (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us)) 831a54756cbSKent Gibson return 0; 832a54756cbSKent Gibson 83365cff704SKent Gibson /* sw debounced and still will be...*/ 83465cff704SKent Gibson if (debounce_period_us && READ_ONCE(line->sw_debounced)) { 83565cff704SKent Gibson line->eflags = eflags; 83665cff704SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 83765cff704SKent Gibson return 0; 83865cff704SKent Gibson } 83965cff704SKent Gibson 84065cff704SKent Gibson /* reconfiguring edge detection or sw debounce being disabled */ 84165cff704SKent Gibson if ((line->irq && !READ_ONCE(line->sw_debounced)) || 84265cff704SKent Gibson (!debounce_period_us && READ_ONCE(line->sw_debounced))) 843a54756cbSKent Gibson edge_detector_stop(line); 844a54756cbSKent Gibson 84565cff704SKent Gibson return edge_detector_setup(line, lc, line_idx, eflags); 846a54756cbSKent Gibson } 847a54756cbSKent Gibson 8483c0d9c63SKent Gibson static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, 8493c0d9c63SKent Gibson unsigned int line_idx) 8503c0d9c63SKent Gibson { 8513c0d9c63SKent Gibson unsigned int i; 8523c0d9c63SKent Gibson u64 mask = BIT_ULL(line_idx); 8533c0d9c63SKent Gibson 8543c0d9c63SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 8553c0d9c63SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) && 8563c0d9c63SKent Gibson (lc->attrs[i].mask & mask)) 8573c0d9c63SKent Gibson return lc->attrs[i].attr.flags; 8583c0d9c63SKent Gibson } 8593c0d9c63SKent Gibson return lc->flags; 8603c0d9c63SKent Gibson } 8613c0d9c63SKent Gibson 8623c0d9c63SKent Gibson static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc, 8633c0d9c63SKent Gibson unsigned int line_idx) 8643c0d9c63SKent Gibson { 8653c0d9c63SKent Gibson unsigned int i; 8663c0d9c63SKent Gibson u64 mask = BIT_ULL(line_idx); 8673c0d9c63SKent Gibson 8683c0d9c63SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 8693c0d9c63SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) && 8703c0d9c63SKent Gibson (lc->attrs[i].mask & mask)) 8713c0d9c63SKent Gibson return !!(lc->attrs[i].attr.values & mask); 8723c0d9c63SKent Gibson } 8733c0d9c63SKent Gibson return 0; 8743c0d9c63SKent Gibson } 8753c0d9c63SKent Gibson 8763c0d9c63SKent Gibson static int gpio_v2_line_flags_validate(u64 flags) 8773c0d9c63SKent Gibson { 8783c0d9c63SKent Gibson /* Return an error if an unknown flag is set */ 8793c0d9c63SKent Gibson if (flags & ~GPIO_V2_LINE_VALID_FLAGS) 8803c0d9c63SKent Gibson return -EINVAL; 8813c0d9c63SKent Gibson 8823c0d9c63SKent Gibson /* 8833c0d9c63SKent Gibson * Do not allow both INPUT and OUTPUT flags to be set as they are 8843c0d9c63SKent Gibson * contradictory. 8853c0d9c63SKent Gibson */ 8863c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_FLAG_INPUT) && 8873c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_OUTPUT)) 8883c0d9c63SKent Gibson return -EINVAL; 8893c0d9c63SKent Gibson 89073e03419SKent Gibson /* Edge detection requires explicit input. */ 89173e03419SKent Gibson if ((flags & GPIO_V2_LINE_EDGE_FLAGS) && 89273e03419SKent Gibson !(flags & GPIO_V2_LINE_FLAG_INPUT)) 89373e03419SKent Gibson return -EINVAL; 89473e03419SKent Gibson 8953c0d9c63SKent Gibson /* 8963c0d9c63SKent Gibson * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single 8973c0d9c63SKent Gibson * request. If the hardware actually supports enabling both at the 8983c0d9c63SKent Gibson * same time the electrical result would be disastrous. 8993c0d9c63SKent Gibson */ 9003c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) && 9013c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE)) 9023c0d9c63SKent Gibson return -EINVAL; 9033c0d9c63SKent Gibson 9043c0d9c63SKent Gibson /* Drive requires explicit output direction. */ 9053c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) && 9063c0d9c63SKent Gibson !(flags & GPIO_V2_LINE_FLAG_OUTPUT)) 9073c0d9c63SKent Gibson return -EINVAL; 9083c0d9c63SKent Gibson 9093c0d9c63SKent Gibson /* Bias requires explicit direction. */ 9103c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_BIAS_FLAGS) && 9113c0d9c63SKent Gibson !(flags & GPIO_V2_LINE_DIRECTION_FLAGS)) 9123c0d9c63SKent Gibson return -EINVAL; 9133c0d9c63SKent Gibson 9143c0d9c63SKent Gibson /* Only one bias flag can be set. */ 9153c0d9c63SKent Gibson if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) && 9163c0d9c63SKent Gibson (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | 9173c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) || 9183c0d9c63SKent Gibson ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) && 9193c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) 9203c0d9c63SKent Gibson return -EINVAL; 9213c0d9c63SKent Gibson 9223c0d9c63SKent Gibson return 0; 9233c0d9c63SKent Gibson } 9243c0d9c63SKent Gibson 9253c0d9c63SKent Gibson static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, 9263c0d9c63SKent Gibson unsigned int num_lines) 9273c0d9c63SKent Gibson { 9283c0d9c63SKent Gibson unsigned int i; 9293c0d9c63SKent Gibson u64 flags; 9303c0d9c63SKent Gibson int ret; 9313c0d9c63SKent Gibson 9323c0d9c63SKent Gibson if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX) 9333c0d9c63SKent Gibson return -EINVAL; 9343c0d9c63SKent Gibson 9353c0d9c63SKent Gibson if (memchr_inv(lc->padding, 0, sizeof(lc->padding))) 9363c0d9c63SKent Gibson return -EINVAL; 9373c0d9c63SKent Gibson 9383c0d9c63SKent Gibson for (i = 0; i < num_lines; i++) { 9393c0d9c63SKent Gibson flags = gpio_v2_line_config_flags(lc, i); 9403c0d9c63SKent Gibson ret = gpio_v2_line_flags_validate(flags); 9413c0d9c63SKent Gibson if (ret) 9423c0d9c63SKent Gibson return ret; 94365cff704SKent Gibson 94465cff704SKent Gibson /* debounce requires explicit input */ 94565cff704SKent Gibson if (gpio_v2_line_config_debounced(lc, i) && 94665cff704SKent Gibson !(flags & GPIO_V2_LINE_FLAG_INPUT)) 94765cff704SKent Gibson return -EINVAL; 9483c0d9c63SKent Gibson } 9493c0d9c63SKent Gibson return 0; 9503c0d9c63SKent Gibson } 9513c0d9c63SKent Gibson 9523c0d9c63SKent Gibson static void gpio_v2_line_config_flags_to_desc_flags(u64 flags, 9533c0d9c63SKent Gibson unsigned long *flagsp) 9543c0d9c63SKent Gibson { 9553c0d9c63SKent Gibson assign_bit(FLAG_ACTIVE_LOW, flagsp, 9563c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW); 9573c0d9c63SKent Gibson 9583c0d9c63SKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) 9593c0d9c63SKent Gibson set_bit(FLAG_IS_OUT, flagsp); 9603c0d9c63SKent Gibson else if (flags & GPIO_V2_LINE_FLAG_INPUT) 9613c0d9c63SKent Gibson clear_bit(FLAG_IS_OUT, flagsp); 9623c0d9c63SKent Gibson 96373e03419SKent Gibson assign_bit(FLAG_EDGE_RISING, flagsp, 96473e03419SKent Gibson flags & GPIO_V2_LINE_FLAG_EDGE_RISING); 96573e03419SKent Gibson assign_bit(FLAG_EDGE_FALLING, flagsp, 96673e03419SKent Gibson flags & GPIO_V2_LINE_FLAG_EDGE_FALLING); 96773e03419SKent Gibson 9683c0d9c63SKent Gibson assign_bit(FLAG_OPEN_DRAIN, flagsp, 9693c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN); 9703c0d9c63SKent Gibson assign_bit(FLAG_OPEN_SOURCE, flagsp, 9713c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE); 9723c0d9c63SKent Gibson 9733c0d9c63SKent Gibson assign_bit(FLAG_PULL_UP, flagsp, 9743c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP); 9753c0d9c63SKent Gibson assign_bit(FLAG_PULL_DOWN, flagsp, 9763c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN); 9773c0d9c63SKent Gibson assign_bit(FLAG_BIAS_DISABLE, flagsp, 9783c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED); 97926d060e4SKent Gibson 98026d060e4SKent Gibson assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp, 98126d060e4SKent Gibson flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME); 9823c0d9c63SKent Gibson } 9833c0d9c63SKent Gibson 9843c0d9c63SKent Gibson static long linereq_get_values(struct linereq *lr, void __user *ip) 9853c0d9c63SKent Gibson { 9863c0d9c63SKent Gibson struct gpio_v2_line_values lv; 9873c0d9c63SKent Gibson DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); 9883c0d9c63SKent Gibson struct gpio_desc **descs; 9893c0d9c63SKent Gibson unsigned int i, didx, num_get; 99065cff704SKent Gibson bool val; 9913c0d9c63SKent Gibson int ret; 9923c0d9c63SKent Gibson 9933c0d9c63SKent Gibson /* NOTE: It's ok to read values of output lines. */ 9943c0d9c63SKent Gibson if (copy_from_user(&lv, ip, sizeof(lv))) 9953c0d9c63SKent Gibson return -EFAULT; 9963c0d9c63SKent Gibson 9973c0d9c63SKent Gibson for (num_get = 0, i = 0; i < lr->num_lines; i++) { 9983c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 9993c0d9c63SKent Gibson num_get++; 10003c0d9c63SKent Gibson descs = &lr->lines[i].desc; 10013c0d9c63SKent Gibson } 10023c0d9c63SKent Gibson } 10033c0d9c63SKent Gibson 10043c0d9c63SKent Gibson if (num_get == 0) 10053c0d9c63SKent Gibson return -EINVAL; 10063c0d9c63SKent Gibson 10073c0d9c63SKent Gibson if (num_get != 1) { 10083c0d9c63SKent Gibson descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL); 10093c0d9c63SKent Gibson if (!descs) 10103c0d9c63SKent Gibson return -ENOMEM; 10113c0d9c63SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 10123c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 10133c0d9c63SKent Gibson descs[didx] = lr->lines[i].desc; 10143c0d9c63SKent Gibson didx++; 10153c0d9c63SKent Gibson } 10163c0d9c63SKent Gibson } 10173c0d9c63SKent Gibson } 10183c0d9c63SKent Gibson ret = gpiod_get_array_value_complex(false, true, num_get, 10193c0d9c63SKent Gibson descs, NULL, vals); 10203c0d9c63SKent Gibson 10213c0d9c63SKent Gibson if (num_get != 1) 10223c0d9c63SKent Gibson kfree(descs); 10233c0d9c63SKent Gibson if (ret) 10243c0d9c63SKent Gibson return ret; 10253c0d9c63SKent Gibson 10263c0d9c63SKent Gibson lv.bits = 0; 10273c0d9c63SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 10283c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 102965cff704SKent Gibson if (lr->lines[i].sw_debounced) 103065cff704SKent Gibson val = debounced_value(&lr->lines[i]); 103165cff704SKent Gibson else 103265cff704SKent Gibson val = test_bit(didx, vals); 103365cff704SKent Gibson if (val) 10343c0d9c63SKent Gibson lv.bits |= BIT_ULL(i); 10353c0d9c63SKent Gibson didx++; 10363c0d9c63SKent Gibson } 10373c0d9c63SKent Gibson } 10383c0d9c63SKent Gibson 10393c0d9c63SKent Gibson if (copy_to_user(ip, &lv, sizeof(lv))) 10403c0d9c63SKent Gibson return -EFAULT; 10413c0d9c63SKent Gibson 10423c0d9c63SKent Gibson return 0; 10433c0d9c63SKent Gibson } 10443c0d9c63SKent Gibson 10457b8e00d9SKent Gibson static long linereq_set_values_unlocked(struct linereq *lr, 10467b8e00d9SKent Gibson struct gpio_v2_line_values *lv) 10477b8e00d9SKent Gibson { 10487b8e00d9SKent Gibson DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); 10497b8e00d9SKent Gibson struct gpio_desc **descs; 10507b8e00d9SKent Gibson unsigned int i, didx, num_set; 10517b8e00d9SKent Gibson int ret; 10527b8e00d9SKent Gibson 10537b8e00d9SKent Gibson bitmap_zero(vals, GPIO_V2_LINES_MAX); 10547b8e00d9SKent Gibson for (num_set = 0, i = 0; i < lr->num_lines; i++) { 10557b8e00d9SKent Gibson if (lv->mask & BIT_ULL(i)) { 10567b8e00d9SKent Gibson if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags)) 10577b8e00d9SKent Gibson return -EPERM; 10587b8e00d9SKent Gibson if (lv->bits & BIT_ULL(i)) 10597b8e00d9SKent Gibson __set_bit(num_set, vals); 10607b8e00d9SKent Gibson num_set++; 10617b8e00d9SKent Gibson descs = &lr->lines[i].desc; 10627b8e00d9SKent Gibson } 10637b8e00d9SKent Gibson } 10647b8e00d9SKent Gibson if (num_set == 0) 10657b8e00d9SKent Gibson return -EINVAL; 10667b8e00d9SKent Gibson 10677b8e00d9SKent Gibson if (num_set != 1) { 10687b8e00d9SKent Gibson /* build compacted desc array and values */ 10697b8e00d9SKent Gibson descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL); 10707b8e00d9SKent Gibson if (!descs) 10717b8e00d9SKent Gibson return -ENOMEM; 10727b8e00d9SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 10737b8e00d9SKent Gibson if (lv->mask & BIT_ULL(i)) { 10747b8e00d9SKent Gibson descs[didx] = lr->lines[i].desc; 10757b8e00d9SKent Gibson didx++; 10767b8e00d9SKent Gibson } 10777b8e00d9SKent Gibson } 10787b8e00d9SKent Gibson } 10797b8e00d9SKent Gibson ret = gpiod_set_array_value_complex(false, true, num_set, 10807b8e00d9SKent Gibson descs, NULL, vals); 10817b8e00d9SKent Gibson 10827b8e00d9SKent Gibson if (num_set != 1) 10837b8e00d9SKent Gibson kfree(descs); 10847b8e00d9SKent Gibson return ret; 10857b8e00d9SKent Gibson } 10867b8e00d9SKent Gibson 10877b8e00d9SKent Gibson static long linereq_set_values(struct linereq *lr, void __user *ip) 10887b8e00d9SKent Gibson { 10897b8e00d9SKent Gibson struct gpio_v2_line_values lv; 10907b8e00d9SKent Gibson int ret; 10917b8e00d9SKent Gibson 10927b8e00d9SKent Gibson if (copy_from_user(&lv, ip, sizeof(lv))) 10937b8e00d9SKent Gibson return -EFAULT; 10947b8e00d9SKent Gibson 10957b8e00d9SKent Gibson mutex_lock(&lr->config_mutex); 10967b8e00d9SKent Gibson 10977b8e00d9SKent Gibson ret = linereq_set_values_unlocked(lr, &lv); 10987b8e00d9SKent Gibson 10997b8e00d9SKent Gibson mutex_unlock(&lr->config_mutex); 11007b8e00d9SKent Gibson 11017b8e00d9SKent Gibson return ret; 11027b8e00d9SKent Gibson } 11037b8e00d9SKent Gibson 1104a54756cbSKent Gibson static long linereq_set_config_unlocked(struct linereq *lr, 1105a54756cbSKent Gibson struct gpio_v2_line_config *lc) 1106a54756cbSKent Gibson { 1107a54756cbSKent Gibson struct gpio_desc *desc; 1108a54756cbSKent Gibson unsigned int i; 1109a54756cbSKent Gibson u64 flags; 1110a54756cbSKent Gibson bool polarity_change; 1111a54756cbSKent Gibson int ret; 1112a54756cbSKent Gibson 1113a54756cbSKent Gibson for (i = 0; i < lr->num_lines; i++) { 1114a54756cbSKent Gibson desc = lr->lines[i].desc; 1115a54756cbSKent Gibson flags = gpio_v2_line_config_flags(lc, i); 1116a54756cbSKent Gibson polarity_change = 1117a54756cbSKent Gibson (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) != 1118a54756cbSKent Gibson ((flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) != 0)); 1119a54756cbSKent Gibson 1120a54756cbSKent Gibson gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 1121a54756cbSKent Gibson /* 1122a54756cbSKent Gibson * Lines have to be requested explicitly for input 1123a54756cbSKent Gibson * or output, else the line will be treated "as is". 1124a54756cbSKent Gibson */ 1125a54756cbSKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 1126a54756cbSKent Gibson int val = gpio_v2_line_config_output_value(lc, i); 1127a54756cbSKent Gibson 1128a54756cbSKent Gibson edge_detector_stop(&lr->lines[i]); 1129a54756cbSKent Gibson ret = gpiod_direction_output(desc, val); 1130a54756cbSKent Gibson if (ret) 1131a54756cbSKent Gibson return ret; 1132a54756cbSKent Gibson } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { 1133a54756cbSKent Gibson ret = gpiod_direction_input(desc); 1134a54756cbSKent Gibson if (ret) 1135a54756cbSKent Gibson return ret; 1136a54756cbSKent Gibson 113765cff704SKent Gibson ret = edge_detector_update(&lr->lines[i], lc, i, 1138a54756cbSKent Gibson flags & GPIO_V2_LINE_EDGE_FLAGS, 1139a54756cbSKent Gibson polarity_change); 1140a54756cbSKent Gibson if (ret) 1141a54756cbSKent Gibson return ret; 1142a54756cbSKent Gibson } 1143a54756cbSKent Gibson 1144a54756cbSKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 1145a54756cbSKent Gibson GPIO_V2_LINE_CHANGED_CONFIG, 1146a54756cbSKent Gibson desc); 1147a54756cbSKent Gibson } 1148a54756cbSKent Gibson return 0; 1149a54756cbSKent Gibson } 1150a54756cbSKent Gibson 1151a54756cbSKent Gibson static long linereq_set_config(struct linereq *lr, void __user *ip) 1152a54756cbSKent Gibson { 1153a54756cbSKent Gibson struct gpio_v2_line_config lc; 1154a54756cbSKent Gibson int ret; 1155a54756cbSKent Gibson 1156a54756cbSKent Gibson if (copy_from_user(&lc, ip, sizeof(lc))) 1157a54756cbSKent Gibson return -EFAULT; 1158a54756cbSKent Gibson 1159a54756cbSKent Gibson ret = gpio_v2_line_config_validate(&lc, lr->num_lines); 1160a54756cbSKent Gibson if (ret) 1161a54756cbSKent Gibson return ret; 1162a54756cbSKent Gibson 1163a54756cbSKent Gibson mutex_lock(&lr->config_mutex); 1164a54756cbSKent Gibson 1165a54756cbSKent Gibson ret = linereq_set_config_unlocked(lr, &lc); 1166a54756cbSKent Gibson 1167a54756cbSKent Gibson mutex_unlock(&lr->config_mutex); 1168a54756cbSKent Gibson 1169a54756cbSKent Gibson return ret; 1170a54756cbSKent Gibson } 1171a54756cbSKent Gibson 11723c0d9c63SKent Gibson static long linereq_ioctl(struct file *file, unsigned int cmd, 11733c0d9c63SKent Gibson unsigned long arg) 11743c0d9c63SKent Gibson { 11753c0d9c63SKent Gibson struct linereq *lr = file->private_data; 11763c0d9c63SKent Gibson void __user *ip = (void __user *)arg; 11773c0d9c63SKent Gibson 11783c0d9c63SKent Gibson if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL) 11793c0d9c63SKent Gibson return linereq_get_values(lr, ip); 11807b8e00d9SKent Gibson else if (cmd == GPIO_V2_LINE_SET_VALUES_IOCTL) 11817b8e00d9SKent Gibson return linereq_set_values(lr, ip); 1182a54756cbSKent Gibson else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL) 1183a54756cbSKent Gibson return linereq_set_config(lr, ip); 11843c0d9c63SKent Gibson 11853c0d9c63SKent Gibson return -EINVAL; 11863c0d9c63SKent Gibson } 11873c0d9c63SKent Gibson 11883c0d9c63SKent Gibson #ifdef CONFIG_COMPAT 11893c0d9c63SKent Gibson static long linereq_ioctl_compat(struct file *file, unsigned int cmd, 11903c0d9c63SKent Gibson unsigned long arg) 11913c0d9c63SKent Gibson { 11923c0d9c63SKent Gibson return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 11933c0d9c63SKent Gibson } 11943c0d9c63SKent Gibson #endif 11953c0d9c63SKent Gibson 119673e03419SKent Gibson static __poll_t linereq_poll(struct file *file, 119773e03419SKent Gibson struct poll_table_struct *wait) 119873e03419SKent Gibson { 119973e03419SKent Gibson struct linereq *lr = file->private_data; 120073e03419SKent Gibson __poll_t events = 0; 120173e03419SKent Gibson 120273e03419SKent Gibson poll_wait(file, &lr->wait, wait); 120373e03419SKent Gibson 120473e03419SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events, 120573e03419SKent Gibson &lr->wait.lock)) 120673e03419SKent Gibson events = EPOLLIN | EPOLLRDNORM; 120773e03419SKent Gibson 120873e03419SKent Gibson return events; 120973e03419SKent Gibson } 121073e03419SKent Gibson 121173e03419SKent Gibson static ssize_t linereq_read(struct file *file, 121273e03419SKent Gibson char __user *buf, 121373e03419SKent Gibson size_t count, 121473e03419SKent Gibson loff_t *f_ps) 121573e03419SKent Gibson { 121673e03419SKent Gibson struct linereq *lr = file->private_data; 121773e03419SKent Gibson struct gpio_v2_line_event le; 121873e03419SKent Gibson ssize_t bytes_read = 0; 121973e03419SKent Gibson int ret; 122073e03419SKent Gibson 122173e03419SKent Gibson if (count < sizeof(le)) 122273e03419SKent Gibson return -EINVAL; 122373e03419SKent Gibson 122473e03419SKent Gibson do { 122573e03419SKent Gibson spin_lock(&lr->wait.lock); 122673e03419SKent Gibson if (kfifo_is_empty(&lr->events)) { 122773e03419SKent Gibson if (bytes_read) { 122873e03419SKent Gibson spin_unlock(&lr->wait.lock); 122973e03419SKent Gibson return bytes_read; 123073e03419SKent Gibson } 123173e03419SKent Gibson 123273e03419SKent Gibson if (file->f_flags & O_NONBLOCK) { 123373e03419SKent Gibson spin_unlock(&lr->wait.lock); 123473e03419SKent Gibson return -EAGAIN; 123573e03419SKent Gibson } 123673e03419SKent Gibson 123773e03419SKent Gibson ret = wait_event_interruptible_locked(lr->wait, 123873e03419SKent Gibson !kfifo_is_empty(&lr->events)); 123973e03419SKent Gibson if (ret) { 124073e03419SKent Gibson spin_unlock(&lr->wait.lock); 124173e03419SKent Gibson return ret; 124273e03419SKent Gibson } 124373e03419SKent Gibson } 124473e03419SKent Gibson 124573e03419SKent Gibson ret = kfifo_out(&lr->events, &le, 1); 124673e03419SKent Gibson spin_unlock(&lr->wait.lock); 124773e03419SKent Gibson if (ret != 1) { 124873e03419SKent Gibson /* 124973e03419SKent Gibson * This should never happen - we were holding the 125073e03419SKent Gibson * lock from the moment we learned the fifo is no 125173e03419SKent Gibson * longer empty until now. 125273e03419SKent Gibson */ 125373e03419SKent Gibson ret = -EIO; 125473e03419SKent Gibson break; 125573e03419SKent Gibson } 125673e03419SKent Gibson 125773e03419SKent Gibson if (copy_to_user(buf + bytes_read, &le, sizeof(le))) 125873e03419SKent Gibson return -EFAULT; 125973e03419SKent Gibson bytes_read += sizeof(le); 126073e03419SKent Gibson } while (count >= bytes_read + sizeof(le)); 126173e03419SKent Gibson 126273e03419SKent Gibson return bytes_read; 126373e03419SKent Gibson } 126473e03419SKent Gibson 12653c0d9c63SKent Gibson static void linereq_free(struct linereq *lr) 12663c0d9c63SKent Gibson { 12673c0d9c63SKent Gibson unsigned int i; 12683c0d9c63SKent Gibson 12693c0d9c63SKent Gibson for (i = 0; i < lr->num_lines; i++) { 127073e03419SKent Gibson edge_detector_stop(&lr->lines[i]); 12713c0d9c63SKent Gibson if (lr->lines[i].desc) 12723c0d9c63SKent Gibson gpiod_free(lr->lines[i].desc); 12733c0d9c63SKent Gibson } 127473e03419SKent Gibson kfifo_free(&lr->events); 12753c0d9c63SKent Gibson kfree(lr->label); 12763c0d9c63SKent Gibson put_device(&lr->gdev->dev); 12773c0d9c63SKent Gibson kfree(lr); 12783c0d9c63SKent Gibson } 12793c0d9c63SKent Gibson 12803c0d9c63SKent Gibson static int linereq_release(struct inode *inode, struct file *file) 12813c0d9c63SKent Gibson { 12823c0d9c63SKent Gibson struct linereq *lr = file->private_data; 12833c0d9c63SKent Gibson 12843c0d9c63SKent Gibson linereq_free(lr); 12853c0d9c63SKent Gibson return 0; 12863c0d9c63SKent Gibson } 12873c0d9c63SKent Gibson 12883c0d9c63SKent Gibson static const struct file_operations line_fileops = { 12893c0d9c63SKent Gibson .release = linereq_release, 129073e03419SKent Gibson .read = linereq_read, 129173e03419SKent Gibson .poll = linereq_poll, 12923c0d9c63SKent Gibson .owner = THIS_MODULE, 12933c0d9c63SKent Gibson .llseek = noop_llseek, 12943c0d9c63SKent Gibson .unlocked_ioctl = linereq_ioctl, 12953c0d9c63SKent Gibson #ifdef CONFIG_COMPAT 12963c0d9c63SKent Gibson .compat_ioctl = linereq_ioctl_compat, 12973c0d9c63SKent Gibson #endif 12983c0d9c63SKent Gibson }; 12993c0d9c63SKent Gibson 13003c0d9c63SKent Gibson static int linereq_create(struct gpio_device *gdev, void __user *ip) 13013c0d9c63SKent Gibson { 13023c0d9c63SKent Gibson struct gpio_v2_line_request ulr; 13033c0d9c63SKent Gibson struct gpio_v2_line_config *lc; 13043c0d9c63SKent Gibson struct linereq *lr; 13053c0d9c63SKent Gibson struct file *file; 13063c0d9c63SKent Gibson u64 flags; 13073c0d9c63SKent Gibson unsigned int i; 13083c0d9c63SKent Gibson int fd, ret; 13093c0d9c63SKent Gibson 13103c0d9c63SKent Gibson if (copy_from_user(&ulr, ip, sizeof(ulr))) 13113c0d9c63SKent Gibson return -EFAULT; 13123c0d9c63SKent Gibson 13133c0d9c63SKent Gibson if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX)) 13143c0d9c63SKent Gibson return -EINVAL; 13153c0d9c63SKent Gibson 13163c0d9c63SKent Gibson if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding))) 13173c0d9c63SKent Gibson return -EINVAL; 13183c0d9c63SKent Gibson 13193c0d9c63SKent Gibson lc = &ulr.config; 13203c0d9c63SKent Gibson ret = gpio_v2_line_config_validate(lc, ulr.num_lines); 13213c0d9c63SKent Gibson if (ret) 13223c0d9c63SKent Gibson return ret; 13233c0d9c63SKent Gibson 13243c0d9c63SKent Gibson lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); 13253c0d9c63SKent Gibson if (!lr) 13263c0d9c63SKent Gibson return -ENOMEM; 13273c0d9c63SKent Gibson 13283c0d9c63SKent Gibson lr->gdev = gdev; 13293c0d9c63SKent Gibson get_device(&gdev->dev); 13303c0d9c63SKent Gibson 133165cff704SKent Gibson for (i = 0; i < ulr.num_lines; i++) { 133273e03419SKent Gibson lr->lines[i].req = lr; 133365cff704SKent Gibson WRITE_ONCE(lr->lines[i].sw_debounced, 0); 133465cff704SKent Gibson INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func); 133565cff704SKent Gibson } 133673e03419SKent Gibson 1337f188ac12SKent Gibson if (ulr.consumer[0] != '\0') { 13383c0d9c63SKent Gibson /* label is only initialized if consumer is set */ 1339f188ac12SKent Gibson lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1, 1340f188ac12SKent Gibson GFP_KERNEL); 13413c0d9c63SKent Gibson if (!lr->label) { 13423c0d9c63SKent Gibson ret = -ENOMEM; 13433c0d9c63SKent Gibson goto out_free_linereq; 13443c0d9c63SKent Gibson } 13453c0d9c63SKent Gibson } 13463c0d9c63SKent Gibson 1347a54756cbSKent Gibson mutex_init(&lr->config_mutex); 134873e03419SKent Gibson init_waitqueue_head(&lr->wait); 134973e03419SKent Gibson lr->event_buffer_size = ulr.event_buffer_size; 135073e03419SKent Gibson if (lr->event_buffer_size == 0) 135173e03419SKent Gibson lr->event_buffer_size = ulr.num_lines * 16; 135273e03419SKent Gibson else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16) 135373e03419SKent Gibson lr->event_buffer_size = GPIO_V2_LINES_MAX * 16; 135473e03419SKent Gibson 135573e03419SKent Gibson atomic_set(&lr->seqno, 0); 13563c0d9c63SKent Gibson lr->num_lines = ulr.num_lines; 13573c0d9c63SKent Gibson 13583c0d9c63SKent Gibson /* Request each GPIO */ 13593c0d9c63SKent Gibson for (i = 0; i < ulr.num_lines; i++) { 13603c0d9c63SKent Gibson u32 offset = ulr.offsets[i]; 13613c0d9c63SKent Gibson struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); 13623c0d9c63SKent Gibson 13633c0d9c63SKent Gibson if (IS_ERR(desc)) { 13643c0d9c63SKent Gibson ret = PTR_ERR(desc); 13653c0d9c63SKent Gibson goto out_free_linereq; 13663c0d9c63SKent Gibson } 13673c0d9c63SKent Gibson 13683c0d9c63SKent Gibson ret = gpiod_request(desc, lr->label); 13693c0d9c63SKent Gibson if (ret) 13703c0d9c63SKent Gibson goto out_free_linereq; 13713c0d9c63SKent Gibson 13723c0d9c63SKent Gibson lr->lines[i].desc = desc; 13733c0d9c63SKent Gibson flags = gpio_v2_line_config_flags(lc, i); 13743c0d9c63SKent Gibson gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 13753c0d9c63SKent Gibson 13763c0d9c63SKent Gibson ret = gpiod_set_transitory(desc, false); 13773c0d9c63SKent Gibson if (ret < 0) 13783c0d9c63SKent Gibson goto out_free_linereq; 13793c0d9c63SKent Gibson 13803c0d9c63SKent Gibson /* 13813c0d9c63SKent Gibson * Lines have to be requested explicitly for input 13823c0d9c63SKent Gibson * or output, else the line will be treated "as is". 13833c0d9c63SKent Gibson */ 13843c0d9c63SKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 13853c0d9c63SKent Gibson int val = gpio_v2_line_config_output_value(lc, i); 13863c0d9c63SKent Gibson 13873c0d9c63SKent Gibson ret = gpiod_direction_output(desc, val); 13883c0d9c63SKent Gibson if (ret) 13893c0d9c63SKent Gibson goto out_free_linereq; 13903c0d9c63SKent Gibson } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { 13913c0d9c63SKent Gibson ret = gpiod_direction_input(desc); 13923c0d9c63SKent Gibson if (ret) 13933c0d9c63SKent Gibson goto out_free_linereq; 139473e03419SKent Gibson 139565cff704SKent Gibson ret = edge_detector_setup(&lr->lines[i], lc, i, 139673e03419SKent Gibson flags & GPIO_V2_LINE_EDGE_FLAGS); 139773e03419SKent Gibson if (ret) 139873e03419SKent Gibson goto out_free_linereq; 13993c0d9c63SKent Gibson } 14003c0d9c63SKent Gibson 14013c0d9c63SKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 1402aad95584SKent Gibson GPIO_V2_LINE_CHANGED_REQUESTED, desc); 14033c0d9c63SKent Gibson 14043c0d9c63SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", 14053c0d9c63SKent Gibson offset); 14063c0d9c63SKent Gibson } 14073c0d9c63SKent Gibson 14083c0d9c63SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 14093c0d9c63SKent Gibson if (fd < 0) { 14103c0d9c63SKent Gibson ret = fd; 14113c0d9c63SKent Gibson goto out_free_linereq; 14123c0d9c63SKent Gibson } 14133c0d9c63SKent Gibson 14143c0d9c63SKent Gibson file = anon_inode_getfile("gpio-line", &line_fileops, lr, 14153c0d9c63SKent Gibson O_RDONLY | O_CLOEXEC); 14163c0d9c63SKent Gibson if (IS_ERR(file)) { 14173c0d9c63SKent Gibson ret = PTR_ERR(file); 14183c0d9c63SKent Gibson goto out_put_unused_fd; 14193c0d9c63SKent Gibson } 14203c0d9c63SKent Gibson 14213c0d9c63SKent Gibson ulr.fd = fd; 14223c0d9c63SKent Gibson if (copy_to_user(ip, &ulr, sizeof(ulr))) { 14233c0d9c63SKent Gibson /* 14243c0d9c63SKent Gibson * fput() will trigger the release() callback, so do not go onto 14253c0d9c63SKent Gibson * the regular error cleanup path here. 14263c0d9c63SKent Gibson */ 14273c0d9c63SKent Gibson fput(file); 14283c0d9c63SKent Gibson put_unused_fd(fd); 14293c0d9c63SKent Gibson return -EFAULT; 14303c0d9c63SKent Gibson } 14313c0d9c63SKent Gibson 14323c0d9c63SKent Gibson fd_install(fd, file); 14333c0d9c63SKent Gibson 14343c0d9c63SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", 14353c0d9c63SKent Gibson lr->num_lines); 14363c0d9c63SKent Gibson 14373c0d9c63SKent Gibson return 0; 14383c0d9c63SKent Gibson 14393c0d9c63SKent Gibson out_put_unused_fd: 14403c0d9c63SKent Gibson put_unused_fd(fd); 14413c0d9c63SKent Gibson out_free_linereq: 14423c0d9c63SKent Gibson linereq_free(lr); 14433c0d9c63SKent Gibson return ret; 14443c0d9c63SKent Gibson } 14453c0d9c63SKent Gibson 14463c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 1447925ca369SKent Gibson 1448925ca369SKent Gibson /* 1449925ca369SKent Gibson * GPIO line event management 1450925ca369SKent Gibson */ 1451925ca369SKent Gibson 1452925ca369SKent Gibson /** 1453925ca369SKent Gibson * struct lineevent_state - contains the state of a userspace event 1454925ca369SKent Gibson * @gdev: the GPIO device the event pertains to 1455925ca369SKent Gibson * @label: consumer label used to tag descriptors 1456925ca369SKent Gibson * @desc: the GPIO descriptor held by this event 1457925ca369SKent Gibson * @eflags: the event flags this line was requested with 1458925ca369SKent Gibson * @irq: the interrupt that trigger in response to events on this GPIO 1459925ca369SKent Gibson * @wait: wait queue that handles blocking reads of events 1460925ca369SKent Gibson * @events: KFIFO for the GPIO events 1461925ca369SKent Gibson * @timestamp: cache for the timestamp storing it between hardirq 1462925ca369SKent Gibson * and IRQ thread, used to bring the timestamp close to the actual 1463925ca369SKent Gibson * event 1464925ca369SKent Gibson */ 1465925ca369SKent Gibson struct lineevent_state { 1466925ca369SKent Gibson struct gpio_device *gdev; 1467925ca369SKent Gibson const char *label; 1468925ca369SKent Gibson struct gpio_desc *desc; 1469925ca369SKent Gibson u32 eflags; 1470925ca369SKent Gibson int irq; 1471925ca369SKent Gibson wait_queue_head_t wait; 1472925ca369SKent Gibson DECLARE_KFIFO(events, struct gpioevent_data, 16); 1473925ca369SKent Gibson u64 timestamp; 1474925ca369SKent Gibson }; 1475925ca369SKent Gibson 1476925ca369SKent Gibson #define GPIOEVENT_REQUEST_VALID_FLAGS \ 1477925ca369SKent Gibson (GPIOEVENT_REQUEST_RISING_EDGE | \ 1478925ca369SKent Gibson GPIOEVENT_REQUEST_FALLING_EDGE) 1479925ca369SKent Gibson 148049bc5279SKent Gibson static __poll_t lineevent_poll(struct file *file, 1481925ca369SKent Gibson struct poll_table_struct *wait) 1482925ca369SKent Gibson { 148349bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1484925ca369SKent Gibson __poll_t events = 0; 1485925ca369SKent Gibson 148649bc5279SKent Gibson poll_wait(file, &le->wait, wait); 1487925ca369SKent Gibson 1488925ca369SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock)) 1489925ca369SKent Gibson events = EPOLLIN | EPOLLRDNORM; 1490925ca369SKent Gibson 1491925ca369SKent Gibson return events; 1492925ca369SKent Gibson } 1493925ca369SKent Gibson 14945ad284abSAndy Shevchenko struct compat_gpioeevent_data { 14955ad284abSAndy Shevchenko compat_u64 timestamp; 14965ad284abSAndy Shevchenko u32 id; 14975ad284abSAndy Shevchenko }; 14985ad284abSAndy Shevchenko 149949bc5279SKent Gibson static ssize_t lineevent_read(struct file *file, 1500925ca369SKent Gibson char __user *buf, 1501925ca369SKent Gibson size_t count, 1502925ca369SKent Gibson loff_t *f_ps) 1503925ca369SKent Gibson { 150449bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1505925ca369SKent Gibson struct gpioevent_data ge; 1506925ca369SKent Gibson ssize_t bytes_read = 0; 15075ad284abSAndy Shevchenko ssize_t ge_size; 1508925ca369SKent Gibson int ret; 1509925ca369SKent Gibson 15105ad284abSAndy Shevchenko /* 15115ad284abSAndy Shevchenko * When compatible system call is being used the struct gpioevent_data, 15125ad284abSAndy Shevchenko * in case of at least ia32, has different size due to the alignment 15135ad284abSAndy Shevchenko * differences. Because we have first member 64 bits followed by one of 15145ad284abSAndy Shevchenko * 32 bits there is no gap between them. The only difference is the 15155ad284abSAndy Shevchenko * padding at the end of the data structure. Hence, we calculate the 15165ad284abSAndy Shevchenko * actual sizeof() and pass this as an argument to copy_to_user() to 15175ad284abSAndy Shevchenko * drop unneeded bytes from the output. 15185ad284abSAndy Shevchenko */ 1519163d1719SAndy Shevchenko if (compat_need_64bit_alignment_fixup()) 1520163d1719SAndy Shevchenko ge_size = sizeof(struct compat_gpioeevent_data); 1521163d1719SAndy Shevchenko else 1522163d1719SAndy Shevchenko ge_size = sizeof(struct gpioevent_data); 15235ad284abSAndy Shevchenko if (count < ge_size) 1524925ca369SKent Gibson return -EINVAL; 1525925ca369SKent Gibson 1526925ca369SKent Gibson do { 1527925ca369SKent Gibson spin_lock(&le->wait.lock); 1528925ca369SKent Gibson if (kfifo_is_empty(&le->events)) { 1529925ca369SKent Gibson if (bytes_read) { 1530925ca369SKent Gibson spin_unlock(&le->wait.lock); 1531925ca369SKent Gibson return bytes_read; 1532925ca369SKent Gibson } 1533925ca369SKent Gibson 153449bc5279SKent Gibson if (file->f_flags & O_NONBLOCK) { 1535925ca369SKent Gibson spin_unlock(&le->wait.lock); 1536925ca369SKent Gibson return -EAGAIN; 1537925ca369SKent Gibson } 1538925ca369SKent Gibson 1539925ca369SKent Gibson ret = wait_event_interruptible_locked(le->wait, 1540925ca369SKent Gibson !kfifo_is_empty(&le->events)); 1541925ca369SKent Gibson if (ret) { 1542925ca369SKent Gibson spin_unlock(&le->wait.lock); 1543925ca369SKent Gibson return ret; 1544925ca369SKent Gibson } 1545925ca369SKent Gibson } 1546925ca369SKent Gibson 1547925ca369SKent Gibson ret = kfifo_out(&le->events, &ge, 1); 1548925ca369SKent Gibson spin_unlock(&le->wait.lock); 1549925ca369SKent Gibson if (ret != 1) { 1550925ca369SKent Gibson /* 1551925ca369SKent Gibson * This should never happen - we were holding the lock 1552925ca369SKent Gibson * from the moment we learned the fifo is no longer 1553925ca369SKent Gibson * empty until now. 1554925ca369SKent Gibson */ 1555925ca369SKent Gibson ret = -EIO; 1556925ca369SKent Gibson break; 1557925ca369SKent Gibson } 1558925ca369SKent Gibson 15595ad284abSAndy Shevchenko if (copy_to_user(buf + bytes_read, &ge, ge_size)) 1560925ca369SKent Gibson return -EFAULT; 15615ad284abSAndy Shevchenko bytes_read += ge_size; 15625ad284abSAndy Shevchenko } while (count >= bytes_read + ge_size); 1563925ca369SKent Gibson 1564925ca369SKent Gibson return bytes_read; 1565925ca369SKent Gibson } 1566925ca369SKent Gibson 156746824272SKent Gibson static void lineevent_free(struct lineevent_state *le) 1568925ca369SKent Gibson { 156946824272SKent Gibson if (le->irq) 1570925ca369SKent Gibson free_irq(le->irq, le); 157146824272SKent Gibson if (le->desc) 1572925ca369SKent Gibson gpiod_free(le->desc); 1573925ca369SKent Gibson kfree(le->label); 157446824272SKent Gibson put_device(&le->gdev->dev); 1575925ca369SKent Gibson kfree(le); 157646824272SKent Gibson } 157746824272SKent Gibson 157846824272SKent Gibson static int lineevent_release(struct inode *inode, struct file *file) 157946824272SKent Gibson { 158046824272SKent Gibson lineevent_free(file->private_data); 1581925ca369SKent Gibson return 0; 1582925ca369SKent Gibson } 1583925ca369SKent Gibson 158449bc5279SKent Gibson static long lineevent_ioctl(struct file *file, unsigned int cmd, 1585925ca369SKent Gibson unsigned long arg) 1586925ca369SKent Gibson { 158749bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1588925ca369SKent Gibson void __user *ip = (void __user *)arg; 1589925ca369SKent Gibson struct gpiohandle_data ghd; 1590925ca369SKent Gibson 1591925ca369SKent Gibson /* 1592925ca369SKent Gibson * We can get the value for an event line but not set it, 1593925ca369SKent Gibson * because it is input by definition. 1594925ca369SKent Gibson */ 1595925ca369SKent Gibson if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { 1596925ca369SKent Gibson int val; 1597925ca369SKent Gibson 1598925ca369SKent Gibson memset(&ghd, 0, sizeof(ghd)); 1599925ca369SKent Gibson 1600925ca369SKent Gibson val = gpiod_get_value_cansleep(le->desc); 1601925ca369SKent Gibson if (val < 0) 1602925ca369SKent Gibson return val; 1603925ca369SKent Gibson ghd.values[0] = val; 1604925ca369SKent Gibson 1605925ca369SKent Gibson if (copy_to_user(ip, &ghd, sizeof(ghd))) 1606925ca369SKent Gibson return -EFAULT; 1607925ca369SKent Gibson 1608925ca369SKent Gibson return 0; 1609925ca369SKent Gibson } 1610925ca369SKent Gibson return -EINVAL; 1611925ca369SKent Gibson } 1612925ca369SKent Gibson 1613925ca369SKent Gibson #ifdef CONFIG_COMPAT 161449bc5279SKent Gibson static long lineevent_ioctl_compat(struct file *file, unsigned int cmd, 1615925ca369SKent Gibson unsigned long arg) 1616925ca369SKent Gibson { 161749bc5279SKent Gibson return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 1618925ca369SKent Gibson } 1619925ca369SKent Gibson #endif 1620925ca369SKent Gibson 1621925ca369SKent Gibson static const struct file_operations lineevent_fileops = { 1622925ca369SKent Gibson .release = lineevent_release, 1623925ca369SKent Gibson .read = lineevent_read, 1624925ca369SKent Gibson .poll = lineevent_poll, 1625925ca369SKent Gibson .owner = THIS_MODULE, 1626925ca369SKent Gibson .llseek = noop_llseek, 1627925ca369SKent Gibson .unlocked_ioctl = lineevent_ioctl, 1628925ca369SKent Gibson #ifdef CONFIG_COMPAT 1629925ca369SKent Gibson .compat_ioctl = lineevent_ioctl_compat, 1630925ca369SKent Gibson #endif 1631925ca369SKent Gibson }; 1632925ca369SKent Gibson 1633925ca369SKent Gibson static irqreturn_t lineevent_irq_thread(int irq, void *p) 1634925ca369SKent Gibson { 1635925ca369SKent Gibson struct lineevent_state *le = p; 1636925ca369SKent Gibson struct gpioevent_data ge; 1637925ca369SKent Gibson int ret; 1638925ca369SKent Gibson 1639925ca369SKent Gibson /* Do not leak kernel stack to userspace */ 1640925ca369SKent Gibson memset(&ge, 0, sizeof(ge)); 1641925ca369SKent Gibson 1642925ca369SKent Gibson /* 1643925ca369SKent Gibson * We may be running from a nested threaded interrupt in which case 1644925ca369SKent Gibson * we didn't get the timestamp from lineevent_irq_handler(). 1645925ca369SKent Gibson */ 1646925ca369SKent Gibson if (!le->timestamp) 1647925ca369SKent Gibson ge.timestamp = ktime_get_ns(); 1648925ca369SKent Gibson else 1649925ca369SKent Gibson ge.timestamp = le->timestamp; 1650925ca369SKent Gibson 1651925ca369SKent Gibson if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE 1652925ca369SKent Gibson && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { 1653925ca369SKent Gibson int level = gpiod_get_value_cansleep(le->desc); 1654925ca369SKent Gibson 1655925ca369SKent Gibson if (level) 1656925ca369SKent Gibson /* Emit low-to-high event */ 1657925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_RISING_EDGE; 1658925ca369SKent Gibson else 1659925ca369SKent Gibson /* Emit high-to-low event */ 1660925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_FALLING_EDGE; 1661925ca369SKent Gibson } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) { 1662925ca369SKent Gibson /* Emit low-to-high event */ 1663925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_RISING_EDGE; 1664925ca369SKent Gibson } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { 1665925ca369SKent Gibson /* Emit high-to-low event */ 1666925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_FALLING_EDGE; 1667925ca369SKent Gibson } else { 1668925ca369SKent Gibson return IRQ_NONE; 1669925ca369SKent Gibson } 1670925ca369SKent Gibson 1671925ca369SKent Gibson ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge, 1672925ca369SKent Gibson 1, &le->wait.lock); 1673925ca369SKent Gibson if (ret) 1674925ca369SKent Gibson wake_up_poll(&le->wait, EPOLLIN); 1675925ca369SKent Gibson else 1676925ca369SKent Gibson pr_debug_ratelimited("event FIFO is full - event dropped\n"); 1677925ca369SKent Gibson 1678925ca369SKent Gibson return IRQ_HANDLED; 1679925ca369SKent Gibson } 1680925ca369SKent Gibson 1681925ca369SKent Gibson static irqreturn_t lineevent_irq_handler(int irq, void *p) 1682925ca369SKent Gibson { 1683925ca369SKent Gibson struct lineevent_state *le = p; 1684925ca369SKent Gibson 1685925ca369SKent Gibson /* 1686925ca369SKent Gibson * Just store the timestamp in hardirq context so we get it as 1687925ca369SKent Gibson * close in time as possible to the actual event. 1688925ca369SKent Gibson */ 1689925ca369SKent Gibson le->timestamp = ktime_get_ns(); 1690925ca369SKent Gibson 1691925ca369SKent Gibson return IRQ_WAKE_THREAD; 1692925ca369SKent Gibson } 1693925ca369SKent Gibson 1694925ca369SKent Gibson static int lineevent_create(struct gpio_device *gdev, void __user *ip) 1695925ca369SKent Gibson { 1696925ca369SKent Gibson struct gpioevent_request eventreq; 1697925ca369SKent Gibson struct lineevent_state *le; 1698925ca369SKent Gibson struct gpio_desc *desc; 1699925ca369SKent Gibson struct file *file; 1700925ca369SKent Gibson u32 offset; 1701925ca369SKent Gibson u32 lflags; 1702925ca369SKent Gibson u32 eflags; 1703925ca369SKent Gibson int fd; 1704925ca369SKent Gibson int ret; 170546824272SKent Gibson int irq, irqflags = 0; 1706925ca369SKent Gibson 1707925ca369SKent Gibson if (copy_from_user(&eventreq, ip, sizeof(eventreq))) 1708925ca369SKent Gibson return -EFAULT; 1709925ca369SKent Gibson 1710925ca369SKent Gibson offset = eventreq.lineoffset; 1711925ca369SKent Gibson lflags = eventreq.handleflags; 1712925ca369SKent Gibson eflags = eventreq.eventflags; 1713925ca369SKent Gibson 1714925ca369SKent Gibson desc = gpiochip_get_desc(gdev->chip, offset); 1715925ca369SKent Gibson if (IS_ERR(desc)) 1716925ca369SKent Gibson return PTR_ERR(desc); 1717925ca369SKent Gibson 1718925ca369SKent Gibson /* Return an error if a unknown flag is set */ 1719925ca369SKent Gibson if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || 1720925ca369SKent Gibson (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) 1721925ca369SKent Gibson return -EINVAL; 1722925ca369SKent Gibson 1723925ca369SKent Gibson /* This is just wrong: we don't look for events on output lines */ 1724925ca369SKent Gibson if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) || 1725925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || 1726925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) 1727925ca369SKent Gibson return -EINVAL; 1728925ca369SKent Gibson 1729925ca369SKent Gibson /* Only one bias flag can be set. */ 1730925ca369SKent Gibson if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && 1731925ca369SKent Gibson (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | 1732925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || 1733925ca369SKent Gibson ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && 1734925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) 1735925ca369SKent Gibson return -EINVAL; 1736925ca369SKent Gibson 1737925ca369SKent Gibson le = kzalloc(sizeof(*le), GFP_KERNEL); 1738925ca369SKent Gibson if (!le) 1739925ca369SKent Gibson return -ENOMEM; 1740925ca369SKent Gibson le->gdev = gdev; 1741925ca369SKent Gibson get_device(&gdev->dev); 1742925ca369SKent Gibson 1743f188ac12SKent Gibson if (eventreq.consumer_label[0] != '\0') { 1744f188ac12SKent Gibson /* label is only initialized if consumer_label is set */ 1745f188ac12SKent Gibson le->label = kstrndup(eventreq.consumer_label, 1746f188ac12SKent Gibson sizeof(eventreq.consumer_label) - 1, 1747925ca369SKent Gibson GFP_KERNEL); 1748925ca369SKent Gibson if (!le->label) { 1749925ca369SKent Gibson ret = -ENOMEM; 1750925ca369SKent Gibson goto out_free_le; 1751925ca369SKent Gibson } 1752925ca369SKent Gibson } 1753925ca369SKent Gibson 1754925ca369SKent Gibson ret = gpiod_request(desc, le->label); 1755925ca369SKent Gibson if (ret) 175646824272SKent Gibson goto out_free_le; 1757925ca369SKent Gibson le->desc = desc; 1758925ca369SKent Gibson le->eflags = eflags; 1759925ca369SKent Gibson 1760c274b58aSKent Gibson linehandle_flags_to_desc_flags(lflags, &desc->flags); 1761925ca369SKent Gibson 1762925ca369SKent Gibson ret = gpiod_direction_input(desc); 1763925ca369SKent Gibson if (ret) 176446824272SKent Gibson goto out_free_le; 1765925ca369SKent Gibson 17666accc376SKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 1767aad95584SKent Gibson GPIO_V2_LINE_CHANGED_REQUESTED, desc); 1768925ca369SKent Gibson 176946824272SKent Gibson irq = gpiod_to_irq(desc); 177046824272SKent Gibson if (irq <= 0) { 1771925ca369SKent Gibson ret = -ENODEV; 177246824272SKent Gibson goto out_free_le; 1773925ca369SKent Gibson } 177446824272SKent Gibson le->irq = irq; 1775925ca369SKent Gibson 1776925ca369SKent Gibson if (eflags & GPIOEVENT_REQUEST_RISING_EDGE) 1777925ca369SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 1778925ca369SKent Gibson IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 1779925ca369SKent Gibson if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE) 1780925ca369SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 1781925ca369SKent Gibson IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 1782925ca369SKent Gibson irqflags |= IRQF_ONESHOT; 1783925ca369SKent Gibson 1784925ca369SKent Gibson INIT_KFIFO(le->events); 1785925ca369SKent Gibson init_waitqueue_head(&le->wait); 1786925ca369SKent Gibson 1787925ca369SKent Gibson /* Request a thread to read the events */ 1788925ca369SKent Gibson ret = request_threaded_irq(le->irq, 1789925ca369SKent Gibson lineevent_irq_handler, 1790925ca369SKent Gibson lineevent_irq_thread, 1791925ca369SKent Gibson irqflags, 1792925ca369SKent Gibson le->label, 1793925ca369SKent Gibson le); 1794925ca369SKent Gibson if (ret) 179546824272SKent Gibson goto out_free_le; 1796925ca369SKent Gibson 1797925ca369SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 1798925ca369SKent Gibson if (fd < 0) { 1799925ca369SKent Gibson ret = fd; 180046824272SKent Gibson goto out_free_le; 1801925ca369SKent Gibson } 1802925ca369SKent Gibson 1803925ca369SKent Gibson file = anon_inode_getfile("gpio-event", 1804925ca369SKent Gibson &lineevent_fileops, 1805925ca369SKent Gibson le, 1806925ca369SKent Gibson O_RDONLY | O_CLOEXEC); 1807925ca369SKent Gibson if (IS_ERR(file)) { 1808925ca369SKent Gibson ret = PTR_ERR(file); 1809925ca369SKent Gibson goto out_put_unused_fd; 1810925ca369SKent Gibson } 1811925ca369SKent Gibson 1812925ca369SKent Gibson eventreq.fd = fd; 1813925ca369SKent Gibson if (copy_to_user(ip, &eventreq, sizeof(eventreq))) { 1814925ca369SKent Gibson /* 1815925ca369SKent Gibson * fput() will trigger the release() callback, so do not go onto 1816925ca369SKent Gibson * the regular error cleanup path here. 1817925ca369SKent Gibson */ 1818925ca369SKent Gibson fput(file); 1819925ca369SKent Gibson put_unused_fd(fd); 1820925ca369SKent Gibson return -EFAULT; 1821925ca369SKent Gibson } 1822925ca369SKent Gibson 1823925ca369SKent Gibson fd_install(fd, file); 1824925ca369SKent Gibson 1825925ca369SKent Gibson return 0; 1826925ca369SKent Gibson 1827925ca369SKent Gibson out_put_unused_fd: 1828925ca369SKent Gibson put_unused_fd(fd); 1829925ca369SKent Gibson out_free_le: 183046824272SKent Gibson lineevent_free(le); 1831925ca369SKent Gibson return ret; 1832925ca369SKent Gibson } 1833925ca369SKent Gibson 1834aad95584SKent Gibson static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2, 1835aad95584SKent Gibson struct gpioline_info *info_v1) 1836aad95584SKent Gibson { 1837aad95584SKent Gibson u64 flagsv2 = info_v2->flags; 1838aad95584SKent Gibson 1839aad95584SKent Gibson memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name)); 1840aad95584SKent Gibson memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer)); 1841aad95584SKent Gibson info_v1->line_offset = info_v2->offset; 1842aad95584SKent Gibson info_v1->flags = 0; 1843aad95584SKent Gibson 1844aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_USED) 1845aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_KERNEL; 1846aad95584SKent Gibson 1847aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT) 1848aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_IS_OUT; 1849aad95584SKent Gibson 1850aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 1851aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW; 1852aad95584SKent Gibson 1853aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN) 1854aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN; 1855aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE) 1856aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE; 1857aad95584SKent Gibson 1858aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP) 1859aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; 1860aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) 1861aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; 1862aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED) 1863aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE; 1864aad95584SKent Gibson } 1865aad95584SKent Gibson 1866aad95584SKent Gibson static void gpio_v2_line_info_changed_to_v1( 1867aad95584SKent Gibson struct gpio_v2_line_info_changed *lic_v2, 1868aad95584SKent Gibson struct gpioline_info_changed *lic_v1) 1869aad95584SKent Gibson { 1870aad95584SKent Gibson gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info); 1871aad95584SKent Gibson lic_v1->timestamp = lic_v2->timestamp_ns; 1872aad95584SKent Gibson lic_v1->event_type = lic_v2->event_type; 1873aad95584SKent Gibson } 1874aad95584SKent Gibson 18753c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 18763c0d9c63SKent Gibson 1877925ca369SKent Gibson static void gpio_desc_to_lineinfo(struct gpio_desc *desc, 1878aad95584SKent Gibson struct gpio_v2_line_info *info) 1879925ca369SKent Gibson { 1880925ca369SKent Gibson struct gpio_chip *gc = desc->gdev->chip; 1881925ca369SKent Gibson bool ok_for_pinctrl; 1882925ca369SKent Gibson unsigned long flags; 188365cff704SKent Gibson u32 debounce_period_us; 188465cff704SKent Gibson unsigned int num_attrs = 0; 1885925ca369SKent Gibson 188669e4e136SKent Gibson memset(info, 0, sizeof(*info)); 1887aad95584SKent Gibson info->offset = gpio_chip_hwgpio(desc); 1888925ca369SKent Gibson 1889925ca369SKent Gibson /* 1890925ca369SKent Gibson * This function takes a mutex so we must check this before taking 1891925ca369SKent Gibson * the spinlock. 1892925ca369SKent Gibson * 1893925ca369SKent Gibson * FIXME: find a non-racy way to retrieve this information. Maybe a 1894925ca369SKent Gibson * lock common to both frameworks? 1895925ca369SKent Gibson */ 1896925ca369SKent Gibson ok_for_pinctrl = 1897aad95584SKent Gibson pinctrl_gpio_can_use_line(gc->base + info->offset); 1898925ca369SKent Gibson 1899925ca369SKent Gibson spin_lock_irqsave(&gpio_lock, flags); 1900925ca369SKent Gibson 190169e4e136SKent Gibson if (desc->name) 190269e4e136SKent Gibson strscpy(info->name, desc->name, sizeof(info->name)); 1903925ca369SKent Gibson 190469e4e136SKent Gibson if (desc->label) 190569e4e136SKent Gibson strscpy(info->consumer, desc->label, sizeof(info->consumer)); 1906925ca369SKent Gibson 1907925ca369SKent Gibson /* 1908925ca369SKent Gibson * Userspace only need to know that the kernel is using this GPIO so 1909925ca369SKent Gibson * it can't use it. 1910925ca369SKent Gibson */ 1911925ca369SKent Gibson info->flags = 0; 1912925ca369SKent Gibson if (test_bit(FLAG_REQUESTED, &desc->flags) || 1913925ca369SKent Gibson test_bit(FLAG_IS_HOGGED, &desc->flags) || 1914925ca369SKent Gibson test_bit(FLAG_USED_AS_IRQ, &desc->flags) || 1915925ca369SKent Gibson test_bit(FLAG_EXPORT, &desc->flags) || 1916925ca369SKent Gibson test_bit(FLAG_SYSFS, &desc->flags) || 1917*a0db197fSMarc Zyngier !gpiochip_line_is_valid(gc, info->offset) || 1918925ca369SKent Gibson !ok_for_pinctrl) 1919aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_USED; 1920aad95584SKent Gibson 1921925ca369SKent Gibson if (test_bit(FLAG_IS_OUT, &desc->flags)) 1922aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OUTPUT; 1923aad95584SKent Gibson else 1924aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_INPUT; 1925aad95584SKent Gibson 1926925ca369SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 1927aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW; 1928aad95584SKent Gibson 1929925ca369SKent Gibson if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 1930aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; 1931925ca369SKent Gibson if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 1932aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; 1933aad95584SKent Gibson 1934925ca369SKent Gibson if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) 1935aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED; 1936925ca369SKent Gibson if (test_bit(FLAG_PULL_DOWN, &desc->flags)) 1937aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN; 1938925ca369SKent Gibson if (test_bit(FLAG_PULL_UP, &desc->flags)) 1939aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; 1940925ca369SKent Gibson 194173e03419SKent Gibson if (test_bit(FLAG_EDGE_RISING, &desc->flags)) 194273e03419SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; 194373e03419SKent Gibson if (test_bit(FLAG_EDGE_FALLING, &desc->flags)) 194473e03419SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; 194573e03419SKent Gibson 194626d060e4SKent Gibson if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags)) 194726d060e4SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME; 194826d060e4SKent Gibson 194965cff704SKent Gibson debounce_period_us = READ_ONCE(desc->debounce_period_us); 195065cff704SKent Gibson if (debounce_period_us) { 195165cff704SKent Gibson info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; 195265cff704SKent Gibson info->attrs[num_attrs].debounce_period_us = debounce_period_us; 195365cff704SKent Gibson num_attrs++; 195465cff704SKent Gibson } 195565cff704SKent Gibson info->num_attrs = num_attrs; 1956925ca369SKent Gibson 1957925ca369SKent Gibson spin_unlock_irqrestore(&gpio_lock, flags); 1958925ca369SKent Gibson } 1959925ca369SKent Gibson 1960925ca369SKent Gibson struct gpio_chardev_data { 1961925ca369SKent Gibson struct gpio_device *gdev; 1962925ca369SKent Gibson wait_queue_head_t wait; 1963aad95584SKent Gibson DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32); 1964925ca369SKent Gibson struct notifier_block lineinfo_changed_nb; 1965925ca369SKent Gibson unsigned long *watched_lines; 1966aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 1967aad95584SKent Gibson atomic_t watch_abi_version; 1968aad95584SKent Gibson #endif 1969925ca369SKent Gibson }; 1970925ca369SKent Gibson 1971aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 1972aad95584SKent Gibson /* 1973aad95584SKent Gibson * returns 0 if the versions match, else the previously selected ABI version 1974aad95584SKent Gibson */ 1975aad95584SKent Gibson static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata, 1976aad95584SKent Gibson unsigned int version) 1977aad95584SKent Gibson { 1978aad95584SKent Gibson int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version); 1979aad95584SKent Gibson 1980aad95584SKent Gibson if (abiv == version) 1981aad95584SKent Gibson return 0; 1982aad95584SKent Gibson 1983aad95584SKent Gibson return abiv; 1984aad95584SKent Gibson } 1985aad95584SKent Gibson #endif 1986aad95584SKent Gibson 1987aad95584SKent Gibson static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip, 1988aad95584SKent Gibson bool watch) 1989aad95584SKent Gibson { 1990aad95584SKent Gibson struct gpio_desc *desc; 1991aad95584SKent Gibson struct gpio_v2_line_info lineinfo; 1992aad95584SKent Gibson 1993aad95584SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 1994aad95584SKent Gibson return -EFAULT; 1995aad95584SKent Gibson 1996aad95584SKent Gibson if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding))) 1997aad95584SKent Gibson return -EINVAL; 1998aad95584SKent Gibson 1999aad95584SKent Gibson desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset); 2000aad95584SKent Gibson if (IS_ERR(desc)) 2001aad95584SKent Gibson return PTR_ERR(desc); 2002aad95584SKent Gibson 2003aad95584SKent Gibson if (watch) { 2004aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2005aad95584SKent Gibson if (lineinfo_ensure_abi_version(cdev, 2)) 2006aad95584SKent Gibson return -EPERM; 2007aad95584SKent Gibson #endif 2008aad95584SKent Gibson if (test_and_set_bit(lineinfo.offset, cdev->watched_lines)) 2009aad95584SKent Gibson return -EBUSY; 2010aad95584SKent Gibson } 2011aad95584SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo); 2012aad95584SKent Gibson 2013aad95584SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 2014aad95584SKent Gibson if (watch) 2015aad95584SKent Gibson clear_bit(lineinfo.offset, cdev->watched_lines); 2016aad95584SKent Gibson return -EFAULT; 2017aad95584SKent Gibson } 2018aad95584SKent Gibson 2019aad95584SKent Gibson return 0; 2020aad95584SKent Gibson } 2021aad95584SKent Gibson 2022925ca369SKent Gibson /* 2023925ca369SKent Gibson * gpio_ioctl() - ioctl handler for the GPIO chardev 2024925ca369SKent Gibson */ 202549bc5279SKent Gibson static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 2026925ca369SKent Gibson { 2027e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2028e2b781c5SKent Gibson struct gpio_device *gdev = cdev->gdev; 2029925ca369SKent Gibson struct gpio_chip *gc = gdev->chip; 2030925ca369SKent Gibson void __user *ip = (void __user *)arg; 2031925ca369SKent Gibson __u32 offset; 2032925ca369SKent Gibson 2033925ca369SKent Gibson /* We fail any subsequent ioctl():s when the chip is gone */ 2034925ca369SKent Gibson if (!gc) 2035925ca369SKent Gibson return -ENODEV; 2036925ca369SKent Gibson 2037925ca369SKent Gibson /* Fill in the struct and pass to userspace */ 2038925ca369SKent Gibson if (cmd == GPIO_GET_CHIPINFO_IOCTL) { 2039925ca369SKent Gibson struct gpiochip_info chipinfo; 2040925ca369SKent Gibson 2041925ca369SKent Gibson memset(&chipinfo, 0, sizeof(chipinfo)); 2042925ca369SKent Gibson 204369e4e136SKent Gibson strscpy(chipinfo.name, dev_name(&gdev->dev), 2044925ca369SKent Gibson sizeof(chipinfo.name)); 204569e4e136SKent Gibson strscpy(chipinfo.label, gdev->label, 2046925ca369SKent Gibson sizeof(chipinfo.label)); 2047925ca369SKent Gibson chipinfo.lines = gdev->ngpio; 2048925ca369SKent Gibson if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) 2049925ca369SKent Gibson return -EFAULT; 2050925ca369SKent Gibson return 0; 20513c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2052925ca369SKent Gibson } else if (cmd == GPIO_GET_LINEINFO_IOCTL) { 2053aad95584SKent Gibson struct gpio_desc *desc; 2054925ca369SKent Gibson struct gpioline_info lineinfo; 2055aad95584SKent Gibson struct gpio_v2_line_info lineinfo_v2; 2056925ca369SKent Gibson 2057925ca369SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 2058925ca369SKent Gibson return -EFAULT; 2059925ca369SKent Gibson 20601bf7ba40SKent Gibson /* this doubles as a range check on line_offset */ 2061925ca369SKent Gibson desc = gpiochip_get_desc(gc, lineinfo.line_offset); 2062925ca369SKent Gibson if (IS_ERR(desc)) 2063925ca369SKent Gibson return PTR_ERR(desc); 2064925ca369SKent Gibson 2065aad95584SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo_v2); 2066aad95584SKent Gibson gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); 2067925ca369SKent Gibson 2068925ca369SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) 2069925ca369SKent Gibson return -EFAULT; 2070925ca369SKent Gibson return 0; 2071925ca369SKent Gibson } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) { 2072925ca369SKent Gibson return linehandle_create(gdev, ip); 2073925ca369SKent Gibson } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) { 2074925ca369SKent Gibson return lineevent_create(gdev, ip); 2075925ca369SKent Gibson } else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) { 2076aad95584SKent Gibson struct gpio_desc *desc; 2077925ca369SKent Gibson struct gpioline_info lineinfo; 2078aad95584SKent Gibson struct gpio_v2_line_info lineinfo_v2; 2079925ca369SKent Gibson 2080925ca369SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 2081925ca369SKent Gibson return -EFAULT; 2082925ca369SKent Gibson 20831bf7ba40SKent Gibson /* this doubles as a range check on line_offset */ 2084925ca369SKent Gibson desc = gpiochip_get_desc(gc, lineinfo.line_offset); 2085925ca369SKent Gibson if (IS_ERR(desc)) 2086925ca369SKent Gibson return PTR_ERR(desc); 2087925ca369SKent Gibson 2088aad95584SKent Gibson if (lineinfo_ensure_abi_version(cdev, 1)) 2089aad95584SKent Gibson return -EPERM; 2090aad95584SKent Gibson 20911bf7ba40SKent Gibson if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines)) 2092925ca369SKent Gibson return -EBUSY; 2093925ca369SKent Gibson 2094aad95584SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo_v2); 2095aad95584SKent Gibson gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); 2096925ca369SKent Gibson 2097f30ef3e8SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 20981bf7ba40SKent Gibson clear_bit(lineinfo.line_offset, cdev->watched_lines); 2099925ca369SKent Gibson return -EFAULT; 2100f30ef3e8SKent Gibson } 2101925ca369SKent Gibson 2102925ca369SKent Gibson return 0; 21033c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 2104aad95584SKent Gibson } else if (cmd == GPIO_V2_GET_LINEINFO_IOCTL || 2105aad95584SKent Gibson cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL) { 2106aad95584SKent Gibson return lineinfo_get(cdev, ip, 2107aad95584SKent Gibson cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL); 21083c0d9c63SKent Gibson } else if (cmd == GPIO_V2_GET_LINE_IOCTL) { 21093c0d9c63SKent Gibson return linereq_create(gdev, ip); 2110925ca369SKent Gibson } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) { 2111925ca369SKent Gibson if (copy_from_user(&offset, ip, sizeof(offset))) 2112925ca369SKent Gibson return -EFAULT; 2113925ca369SKent Gibson 21141bf7ba40SKent Gibson if (offset >= cdev->gdev->ngpio) 21151bf7ba40SKent Gibson return -EINVAL; 2116925ca369SKent Gibson 21171bf7ba40SKent Gibson if (!test_and_clear_bit(offset, cdev->watched_lines)) 2118925ca369SKent Gibson return -EBUSY; 2119925ca369SKent Gibson 2120925ca369SKent Gibson return 0; 2121925ca369SKent Gibson } 2122925ca369SKent Gibson return -EINVAL; 2123925ca369SKent Gibson } 2124925ca369SKent Gibson 2125925ca369SKent Gibson #ifdef CONFIG_COMPAT 212649bc5279SKent Gibson static long gpio_ioctl_compat(struct file *file, unsigned int cmd, 2127925ca369SKent Gibson unsigned long arg) 2128925ca369SKent Gibson { 212949bc5279SKent Gibson return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 2130925ca369SKent Gibson } 2131925ca369SKent Gibson #endif 2132925ca369SKent Gibson 2133925ca369SKent Gibson static struct gpio_chardev_data * 2134925ca369SKent Gibson to_gpio_chardev_data(struct notifier_block *nb) 2135925ca369SKent Gibson { 2136925ca369SKent Gibson return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb); 2137925ca369SKent Gibson } 2138925ca369SKent Gibson 2139925ca369SKent Gibson static int lineinfo_changed_notify(struct notifier_block *nb, 2140925ca369SKent Gibson unsigned long action, void *data) 2141925ca369SKent Gibson { 2142e2b781c5SKent Gibson struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb); 2143aad95584SKent Gibson struct gpio_v2_line_info_changed chg; 2144925ca369SKent Gibson struct gpio_desc *desc = data; 2145925ca369SKent Gibson int ret; 2146925ca369SKent Gibson 2147e2b781c5SKent Gibson if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) 2148925ca369SKent Gibson return NOTIFY_DONE; 2149925ca369SKent Gibson 2150925ca369SKent Gibson memset(&chg, 0, sizeof(chg)); 2151925ca369SKent Gibson chg.event_type = action; 2152aad95584SKent Gibson chg.timestamp_ns = ktime_get_ns(); 2153925ca369SKent Gibson gpio_desc_to_lineinfo(desc, &chg.info); 2154925ca369SKent Gibson 2155e2b781c5SKent Gibson ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); 2156925ca369SKent Gibson if (ret) 2157e2b781c5SKent Gibson wake_up_poll(&cdev->wait, EPOLLIN); 2158925ca369SKent Gibson else 2159925ca369SKent Gibson pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n"); 2160925ca369SKent Gibson 2161925ca369SKent Gibson return NOTIFY_OK; 2162925ca369SKent Gibson } 2163925ca369SKent Gibson 216449bc5279SKent Gibson static __poll_t lineinfo_watch_poll(struct file *file, 2165925ca369SKent Gibson struct poll_table_struct *pollt) 2166925ca369SKent Gibson { 2167e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2168925ca369SKent Gibson __poll_t events = 0; 2169925ca369SKent Gibson 2170e2b781c5SKent Gibson poll_wait(file, &cdev->wait, pollt); 2171925ca369SKent Gibson 2172e2b781c5SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events, 2173e2b781c5SKent Gibson &cdev->wait.lock)) 2174925ca369SKent Gibson events = EPOLLIN | EPOLLRDNORM; 2175925ca369SKent Gibson 2176925ca369SKent Gibson return events; 2177925ca369SKent Gibson } 2178925ca369SKent Gibson 217949bc5279SKent Gibson static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, 2180925ca369SKent Gibson size_t count, loff_t *off) 2181925ca369SKent Gibson { 2182e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2183aad95584SKent Gibson struct gpio_v2_line_info_changed event; 2184925ca369SKent Gibson ssize_t bytes_read = 0; 2185925ca369SKent Gibson int ret; 2186aad95584SKent Gibson size_t event_size; 2187925ca369SKent Gibson 2188aad95584SKent Gibson #ifndef CONFIG_GPIO_CDEV_V1 2189aad95584SKent Gibson event_size = sizeof(struct gpio_v2_line_info_changed); 2190aad95584SKent Gibson if (count < event_size) 2191925ca369SKent Gibson return -EINVAL; 2192aad95584SKent Gibson #endif 2193925ca369SKent Gibson 2194925ca369SKent Gibson do { 2195e2b781c5SKent Gibson spin_lock(&cdev->wait.lock); 2196e2b781c5SKent Gibson if (kfifo_is_empty(&cdev->events)) { 2197925ca369SKent Gibson if (bytes_read) { 2198e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2199925ca369SKent Gibson return bytes_read; 2200925ca369SKent Gibson } 2201925ca369SKent Gibson 220249bc5279SKent Gibson if (file->f_flags & O_NONBLOCK) { 2203e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2204925ca369SKent Gibson return -EAGAIN; 2205925ca369SKent Gibson } 2206925ca369SKent Gibson 2207e2b781c5SKent Gibson ret = wait_event_interruptible_locked(cdev->wait, 2208e2b781c5SKent Gibson !kfifo_is_empty(&cdev->events)); 2209925ca369SKent Gibson if (ret) { 2210e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2211925ca369SKent Gibson return ret; 2212925ca369SKent Gibson } 2213925ca369SKent Gibson } 2214aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2215aad95584SKent Gibson /* must be after kfifo check so watch_abi_version is set */ 2216aad95584SKent Gibson if (atomic_read(&cdev->watch_abi_version) == 2) 2217aad95584SKent Gibson event_size = sizeof(struct gpio_v2_line_info_changed); 2218aad95584SKent Gibson else 2219aad95584SKent Gibson event_size = sizeof(struct gpioline_info_changed); 2220aad95584SKent Gibson if (count < event_size) { 2221aad95584SKent Gibson spin_unlock(&cdev->wait.lock); 2222aad95584SKent Gibson return -EINVAL; 2223aad95584SKent Gibson } 2224aad95584SKent Gibson #endif 2225e2b781c5SKent Gibson ret = kfifo_out(&cdev->events, &event, 1); 2226e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2227925ca369SKent Gibson if (ret != 1) { 2228925ca369SKent Gibson ret = -EIO; 2229925ca369SKent Gibson break; 2230925ca369SKent Gibson /* We should never get here. See lineevent_read(). */ 2231925ca369SKent Gibson } 2232925ca369SKent Gibson 2233aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2234aad95584SKent Gibson if (event_size == sizeof(struct gpio_v2_line_info_changed)) { 2235aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event, event_size)) 2236925ca369SKent Gibson return -EFAULT; 2237aad95584SKent Gibson } else { 2238aad95584SKent Gibson struct gpioline_info_changed event_v1; 2239aad95584SKent Gibson 2240aad95584SKent Gibson gpio_v2_line_info_changed_to_v1(&event, &event_v1); 2241aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event_v1, 2242aad95584SKent Gibson event_size)) 2243aad95584SKent Gibson return -EFAULT; 2244aad95584SKent Gibson } 2245aad95584SKent Gibson #else 2246aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event, event_size)) 2247aad95584SKent Gibson return -EFAULT; 2248aad95584SKent Gibson #endif 2249aad95584SKent Gibson bytes_read += event_size; 2250925ca369SKent Gibson } while (count >= bytes_read + sizeof(event)); 2251925ca369SKent Gibson 2252925ca369SKent Gibson return bytes_read; 2253925ca369SKent Gibson } 2254925ca369SKent Gibson 2255925ca369SKent Gibson /** 2256925ca369SKent Gibson * gpio_chrdev_open() - open the chardev for ioctl operations 2257925ca369SKent Gibson * @inode: inode for this chardev 225849bc5279SKent Gibson * @file: file struct for storing private data 2259925ca369SKent Gibson * Returns 0 on success 2260925ca369SKent Gibson */ 226149bc5279SKent Gibson static int gpio_chrdev_open(struct inode *inode, struct file *file) 2262925ca369SKent Gibson { 2263925ca369SKent Gibson struct gpio_device *gdev = container_of(inode->i_cdev, 2264925ca369SKent Gibson struct gpio_device, chrdev); 2265e2b781c5SKent Gibson struct gpio_chardev_data *cdev; 2266925ca369SKent Gibson int ret = -ENOMEM; 2267925ca369SKent Gibson 2268925ca369SKent Gibson /* Fail on open if the backing gpiochip is gone */ 2269925ca369SKent Gibson if (!gdev->chip) 2270925ca369SKent Gibson return -ENODEV; 2271925ca369SKent Gibson 2272e2b781c5SKent Gibson cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); 2273e2b781c5SKent Gibson if (!cdev) 2274925ca369SKent Gibson return -ENOMEM; 2275925ca369SKent Gibson 2276e2b781c5SKent Gibson cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL); 2277e2b781c5SKent Gibson if (!cdev->watched_lines) 2278e2b781c5SKent Gibson goto out_free_cdev; 2279925ca369SKent Gibson 2280e2b781c5SKent Gibson init_waitqueue_head(&cdev->wait); 2281e2b781c5SKent Gibson INIT_KFIFO(cdev->events); 2282e2b781c5SKent Gibson cdev->gdev = gdev; 2283925ca369SKent Gibson 2284e2b781c5SKent Gibson cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify; 22856accc376SKent Gibson ret = blocking_notifier_chain_register(&gdev->notifier, 2286e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2287925ca369SKent Gibson if (ret) 2288925ca369SKent Gibson goto out_free_bitmap; 2289925ca369SKent Gibson 2290925ca369SKent Gibson get_device(&gdev->dev); 2291e2b781c5SKent Gibson file->private_data = cdev; 2292925ca369SKent Gibson 229349bc5279SKent Gibson ret = nonseekable_open(inode, file); 2294925ca369SKent Gibson if (ret) 2295925ca369SKent Gibson goto out_unregister_notifier; 2296925ca369SKent Gibson 2297925ca369SKent Gibson return ret; 2298925ca369SKent Gibson 2299925ca369SKent Gibson out_unregister_notifier: 23006accc376SKent Gibson blocking_notifier_chain_unregister(&gdev->notifier, 2301e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2302925ca369SKent Gibson out_free_bitmap: 2303e2b781c5SKent Gibson bitmap_free(cdev->watched_lines); 2304e2b781c5SKent Gibson out_free_cdev: 2305e2b781c5SKent Gibson kfree(cdev); 2306925ca369SKent Gibson return ret; 2307925ca369SKent Gibson } 2308925ca369SKent Gibson 2309925ca369SKent Gibson /** 2310925ca369SKent Gibson * gpio_chrdev_release() - close chardev after ioctl operations 2311925ca369SKent Gibson * @inode: inode for this chardev 231249bc5279SKent Gibson * @file: file struct for storing private data 2313925ca369SKent Gibson * Returns 0 on success 2314925ca369SKent Gibson */ 231549bc5279SKent Gibson static int gpio_chrdev_release(struct inode *inode, struct file *file) 2316925ca369SKent Gibson { 2317e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2318e2b781c5SKent Gibson struct gpio_device *gdev = cdev->gdev; 2319925ca369SKent Gibson 2320e2b781c5SKent Gibson bitmap_free(cdev->watched_lines); 23216accc376SKent Gibson blocking_notifier_chain_unregister(&gdev->notifier, 2322e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2323925ca369SKent Gibson put_device(&gdev->dev); 2324e2b781c5SKent Gibson kfree(cdev); 2325925ca369SKent Gibson 2326925ca369SKent Gibson return 0; 2327925ca369SKent Gibson } 2328925ca369SKent Gibson 2329925ca369SKent Gibson static const struct file_operations gpio_fileops = { 2330925ca369SKent Gibson .release = gpio_chrdev_release, 2331925ca369SKent Gibson .open = gpio_chrdev_open, 2332925ca369SKent Gibson .poll = lineinfo_watch_poll, 2333925ca369SKent Gibson .read = lineinfo_watch_read, 2334925ca369SKent Gibson .owner = THIS_MODULE, 2335925ca369SKent Gibson .llseek = no_llseek, 2336925ca369SKent Gibson .unlocked_ioctl = gpio_ioctl, 2337925ca369SKent Gibson #ifdef CONFIG_COMPAT 2338925ca369SKent Gibson .compat_ioctl = gpio_ioctl_compat, 2339925ca369SKent Gibson #endif 2340925ca369SKent Gibson }; 2341925ca369SKent Gibson 2342925ca369SKent Gibson int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) 2343925ca369SKent Gibson { 2344925ca369SKent Gibson int ret; 2345925ca369SKent Gibson 2346925ca369SKent Gibson cdev_init(&gdev->chrdev, &gpio_fileops); 2347925ca369SKent Gibson gdev->chrdev.owner = THIS_MODULE; 2348925ca369SKent Gibson gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id); 2349925ca369SKent Gibson 2350925ca369SKent Gibson ret = cdev_device_add(&gdev->chrdev, &gdev->dev); 2351925ca369SKent Gibson if (ret) 2352925ca369SKent Gibson return ret; 2353925ca369SKent Gibson 2354925ca369SKent Gibson chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n", 2355925ca369SKent Gibson MAJOR(devt), gdev->id); 2356925ca369SKent Gibson 2357925ca369SKent Gibson return 0; 2358925ca369SKent Gibson } 2359925ca369SKent Gibson 2360925ca369SKent Gibson void gpiolib_cdev_unregister(struct gpio_device *gdev) 2361925ca369SKent Gibson { 2362925ca369SKent Gibson cdev_device_del(&gdev->chrdev, &gdev->dev); 2363925ca369SKent Gibson } 2364