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 | \ 5123c0d9c63SKent Gibson GPIO_V2_LINE_BIAS_FLAGS) 5133c0d9c63SKent Gibson 51473e03419SKent Gibson static void linereq_put_event(struct linereq *lr, 51573e03419SKent Gibson struct gpio_v2_line_event *le) 51673e03419SKent Gibson { 51773e03419SKent Gibson bool overflow = false; 51873e03419SKent Gibson 51973e03419SKent Gibson spin_lock(&lr->wait.lock); 52073e03419SKent Gibson if (kfifo_is_full(&lr->events)) { 52173e03419SKent Gibson overflow = true; 52273e03419SKent Gibson kfifo_skip(&lr->events); 52373e03419SKent Gibson } 52473e03419SKent Gibson kfifo_in(&lr->events, le, 1); 52573e03419SKent Gibson spin_unlock(&lr->wait.lock); 52673e03419SKent Gibson if (!overflow) 52773e03419SKent Gibson wake_up_poll(&lr->wait, EPOLLIN); 52873e03419SKent Gibson else 52973e03419SKent Gibson pr_debug_ratelimited("event FIFO is full - event dropped\n"); 53073e03419SKent Gibson } 53173e03419SKent Gibson 53273e03419SKent Gibson static irqreturn_t edge_irq_thread(int irq, void *p) 53373e03419SKent Gibson { 53473e03419SKent Gibson struct line *line = p; 53573e03419SKent Gibson struct linereq *lr = line->req; 53673e03419SKent Gibson struct gpio_v2_line_event le; 53773e03419SKent Gibson 53873e03419SKent Gibson /* Do not leak kernel stack to userspace */ 53973e03419SKent Gibson memset(&le, 0, sizeof(le)); 54073e03419SKent Gibson 54173e03419SKent Gibson if (line->timestamp_ns) { 54273e03419SKent Gibson le.timestamp_ns = line->timestamp_ns; 54373e03419SKent Gibson } else { 54473e03419SKent Gibson /* 54573e03419SKent Gibson * We may be running from a nested threaded interrupt in 54673e03419SKent Gibson * which case we didn't get the timestamp from 54773e03419SKent Gibson * edge_irq_handler(). 54873e03419SKent Gibson */ 54973e03419SKent Gibson le.timestamp_ns = ktime_get_ns(); 55073e03419SKent Gibson if (lr->num_lines != 1) 55173e03419SKent Gibson line->req_seqno = atomic_inc_return(&lr->seqno); 55273e03419SKent Gibson } 55373e03419SKent Gibson line->timestamp_ns = 0; 55473e03419SKent Gibson 55573e03419SKent Gibson if (line->eflags == (GPIO_V2_LINE_FLAG_EDGE_RISING | 55673e03419SKent Gibson GPIO_V2_LINE_FLAG_EDGE_FALLING)) { 55773e03419SKent Gibson int level = gpiod_get_value_cansleep(line->desc); 55873e03419SKent Gibson 55973e03419SKent Gibson if (level) 56073e03419SKent Gibson /* Emit low-to-high event */ 56173e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 56273e03419SKent Gibson else 56373e03419SKent Gibson /* Emit high-to-low event */ 56473e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 56573e03419SKent Gibson } else if (line->eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) { 56673e03419SKent Gibson /* Emit low-to-high event */ 56773e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 56873e03419SKent Gibson } else if (line->eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) { 56973e03419SKent Gibson /* Emit high-to-low event */ 57073e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 57173e03419SKent Gibson } else { 57273e03419SKent Gibson return IRQ_NONE; 57373e03419SKent Gibson } 57473e03419SKent Gibson line->line_seqno++; 57573e03419SKent Gibson le.line_seqno = line->line_seqno; 57673e03419SKent Gibson le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno; 57773e03419SKent Gibson le.offset = gpio_chip_hwgpio(line->desc); 57873e03419SKent Gibson 57973e03419SKent Gibson linereq_put_event(lr, &le); 58073e03419SKent Gibson 58173e03419SKent Gibson return IRQ_HANDLED; 58273e03419SKent Gibson } 58373e03419SKent Gibson 58473e03419SKent Gibson static irqreturn_t edge_irq_handler(int irq, void *p) 58573e03419SKent Gibson { 58673e03419SKent Gibson struct line *line = p; 58773e03419SKent Gibson struct linereq *lr = line->req; 58873e03419SKent Gibson 58973e03419SKent Gibson /* 59073e03419SKent Gibson * Just store the timestamp in hardirq context so we get it as 59173e03419SKent Gibson * close in time as possible to the actual event. 59273e03419SKent Gibson */ 59373e03419SKent Gibson line->timestamp_ns = ktime_get_ns(); 59473e03419SKent Gibson 59573e03419SKent Gibson if (lr->num_lines != 1) 59673e03419SKent Gibson line->req_seqno = atomic_inc_return(&lr->seqno); 59773e03419SKent Gibson 59873e03419SKent Gibson return IRQ_WAKE_THREAD; 59973e03419SKent Gibson } 60073e03419SKent Gibson 60165cff704SKent Gibson /* 60265cff704SKent Gibson * returns the current debounced logical value. 60365cff704SKent Gibson */ 60465cff704SKent Gibson static bool debounced_value(struct line *line) 60565cff704SKent Gibson { 60665cff704SKent Gibson bool value; 60765cff704SKent Gibson 60865cff704SKent Gibson /* 60965cff704SKent Gibson * minor race - debouncer may be stopped here, so edge_detector_stop() 61065cff704SKent Gibson * must leave the value unchanged so the following will read the level 61165cff704SKent Gibson * from when the debouncer was last running. 61265cff704SKent Gibson */ 61365cff704SKent Gibson value = READ_ONCE(line->level); 61465cff704SKent Gibson 61565cff704SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags)) 61665cff704SKent Gibson value = !value; 61765cff704SKent Gibson 61865cff704SKent Gibson return value; 61965cff704SKent Gibson } 62065cff704SKent Gibson 62165cff704SKent Gibson static irqreturn_t debounce_irq_handler(int irq, void *p) 62265cff704SKent Gibson { 62365cff704SKent Gibson struct line *line = p; 62465cff704SKent Gibson 62565cff704SKent Gibson mod_delayed_work(system_wq, &line->work, 62665cff704SKent Gibson usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us))); 62765cff704SKent Gibson 62865cff704SKent Gibson return IRQ_HANDLED; 62965cff704SKent Gibson } 63065cff704SKent Gibson 63165cff704SKent Gibson static void debounce_work_func(struct work_struct *work) 63265cff704SKent Gibson { 63365cff704SKent Gibson struct gpio_v2_line_event le; 63465cff704SKent Gibson struct line *line = container_of(work, struct line, work.work); 63565cff704SKent Gibson struct linereq *lr; 63665cff704SKent Gibson int level; 63765cff704SKent Gibson 63865cff704SKent Gibson level = gpiod_get_raw_value_cansleep(line->desc); 63965cff704SKent Gibson if (level < 0) { 64065cff704SKent Gibson pr_debug_ratelimited("debouncer failed to read line value\n"); 64165cff704SKent Gibson return; 64265cff704SKent Gibson } 64365cff704SKent Gibson 64465cff704SKent Gibson if (READ_ONCE(line->level) == level) 64565cff704SKent Gibson return; 64665cff704SKent Gibson 64765cff704SKent Gibson WRITE_ONCE(line->level, level); 64865cff704SKent Gibson 64965cff704SKent Gibson /* -- edge detection -- */ 65065cff704SKent Gibson if (!line->eflags) 65165cff704SKent Gibson return; 65265cff704SKent Gibson 65365cff704SKent Gibson /* switch from physical level to logical - if they differ */ 65465cff704SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags)) 65565cff704SKent Gibson level = !level; 65665cff704SKent Gibson 65765cff704SKent Gibson /* ignore edges that are not being monitored */ 65865cff704SKent Gibson if (((line->eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) || 65965cff704SKent Gibson ((line->eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level)) 66065cff704SKent Gibson return; 66165cff704SKent Gibson 66265cff704SKent Gibson /* Do not leak kernel stack to userspace */ 66365cff704SKent Gibson memset(&le, 0, sizeof(le)); 66465cff704SKent Gibson 66565cff704SKent Gibson lr = line->req; 66665cff704SKent Gibson le.timestamp_ns = ktime_get_ns(); 66765cff704SKent Gibson le.offset = gpio_chip_hwgpio(line->desc); 66865cff704SKent Gibson line->line_seqno++; 66965cff704SKent Gibson le.line_seqno = line->line_seqno; 67065cff704SKent Gibson le.seqno = (lr->num_lines == 1) ? 67165cff704SKent Gibson le.line_seqno : atomic_inc_return(&lr->seqno); 67265cff704SKent Gibson 67365cff704SKent Gibson if (level) 67465cff704SKent Gibson /* Emit low-to-high event */ 67565cff704SKent Gibson le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 67665cff704SKent Gibson else 67765cff704SKent Gibson /* Emit high-to-low event */ 67865cff704SKent Gibson le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 67965cff704SKent Gibson 68065cff704SKent Gibson linereq_put_event(lr, &le); 68165cff704SKent Gibson } 68265cff704SKent Gibson 68365cff704SKent Gibson static int debounce_setup(struct line *line, 68465cff704SKent Gibson unsigned int debounce_period_us) 68565cff704SKent Gibson { 68665cff704SKent Gibson unsigned long irqflags; 68765cff704SKent Gibson int ret, level, irq; 68865cff704SKent Gibson 68965cff704SKent Gibson /* try hardware */ 69065cff704SKent Gibson ret = gpiod_set_debounce(line->desc, debounce_period_us); 69165cff704SKent Gibson if (!ret) { 69265cff704SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 69365cff704SKent Gibson return ret; 69465cff704SKent Gibson } 69565cff704SKent Gibson if (ret != -ENOTSUPP) 69665cff704SKent Gibson return ret; 69765cff704SKent Gibson 69865cff704SKent Gibson if (debounce_period_us) { 69965cff704SKent Gibson /* setup software debounce */ 70065cff704SKent Gibson level = gpiod_get_raw_value_cansleep(line->desc); 70165cff704SKent Gibson if (level < 0) 70265cff704SKent Gibson return level; 70365cff704SKent Gibson 70465cff704SKent Gibson irq = gpiod_to_irq(line->desc); 70565cff704SKent Gibson if (irq < 0) 70665cff704SKent Gibson return -ENXIO; 70765cff704SKent Gibson 70865cff704SKent Gibson WRITE_ONCE(line->level, level); 70965cff704SKent Gibson irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING; 71065cff704SKent Gibson ret = request_irq(irq, debounce_irq_handler, irqflags, 71165cff704SKent Gibson line->req->label, line); 71265cff704SKent Gibson if (ret) 71365cff704SKent Gibson return ret; 71465cff704SKent Gibson 71565cff704SKent Gibson WRITE_ONCE(line->sw_debounced, 1); 71665cff704SKent Gibson line->irq = irq; 71765cff704SKent Gibson } 71865cff704SKent Gibson return 0; 71965cff704SKent Gibson } 72065cff704SKent Gibson 72165cff704SKent Gibson static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc, 72265cff704SKent Gibson unsigned int line_idx) 72365cff704SKent Gibson { 72465cff704SKent Gibson unsigned int i; 72565cff704SKent Gibson u64 mask = BIT_ULL(line_idx); 72665cff704SKent Gibson 72765cff704SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 72865cff704SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && 72965cff704SKent Gibson (lc->attrs[i].mask & mask)) 73065cff704SKent Gibson return true; 73165cff704SKent Gibson } 73265cff704SKent Gibson return false; 73365cff704SKent Gibson } 73465cff704SKent Gibson 73565cff704SKent Gibson static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc, 73665cff704SKent Gibson unsigned int line_idx) 73765cff704SKent Gibson { 73865cff704SKent Gibson unsigned int i; 73965cff704SKent Gibson u64 mask = BIT_ULL(line_idx); 74065cff704SKent Gibson 74165cff704SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 74265cff704SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && 74365cff704SKent Gibson (lc->attrs[i].mask & mask)) 74465cff704SKent Gibson return lc->attrs[i].attr.debounce_period_us; 74565cff704SKent Gibson } 74665cff704SKent Gibson return 0; 74765cff704SKent Gibson } 74865cff704SKent Gibson 74973e03419SKent Gibson static void edge_detector_stop(struct line *line) 75073e03419SKent Gibson { 75173e03419SKent Gibson if (line->irq) { 75273e03419SKent Gibson free_irq(line->irq, line); 75373e03419SKent Gibson line->irq = 0; 75473e03419SKent Gibson } 755a54756cbSKent Gibson 75665cff704SKent Gibson cancel_delayed_work_sync(&line->work); 75765cff704SKent Gibson WRITE_ONCE(line->sw_debounced, 0); 758a54756cbSKent Gibson line->eflags = 0; 75965cff704SKent Gibson /* do not change line->level - see comment in debounced_value() */ 76073e03419SKent Gibson } 76173e03419SKent Gibson 76273e03419SKent Gibson static int edge_detector_setup(struct line *line, 76365cff704SKent Gibson struct gpio_v2_line_config *lc, 76465cff704SKent Gibson unsigned int line_idx, 76573e03419SKent Gibson u64 eflags) 76673e03419SKent Gibson { 76765cff704SKent Gibson u32 debounce_period_us; 76873e03419SKent Gibson unsigned long irqflags = 0; 76973e03419SKent Gibson int irq, ret; 77073e03419SKent Gibson 77173e03419SKent Gibson if (eflags && !kfifo_initialized(&line->req->events)) { 77273e03419SKent Gibson ret = kfifo_alloc(&line->req->events, 77373e03419SKent Gibson line->req->event_buffer_size, GFP_KERNEL); 77473e03419SKent Gibson if (ret) 77573e03419SKent Gibson return ret; 77673e03419SKent Gibson } 77773e03419SKent Gibson line->eflags = eflags; 77865cff704SKent Gibson if (gpio_v2_line_config_debounced(lc, line_idx)) { 77965cff704SKent Gibson debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx); 78065cff704SKent Gibson ret = debounce_setup(line, debounce_period_us); 78165cff704SKent Gibson if (ret) 78265cff704SKent Gibson return ret; 78365cff704SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 78465cff704SKent Gibson } 78573e03419SKent Gibson 78665cff704SKent Gibson /* detection disabled or sw debouncer will provide edge detection */ 78765cff704SKent Gibson if (!eflags || READ_ONCE(line->sw_debounced)) 78873e03419SKent Gibson return 0; 78973e03419SKent Gibson 79073e03419SKent Gibson irq = gpiod_to_irq(line->desc); 79173e03419SKent Gibson if (irq < 0) 79273e03419SKent Gibson return -ENXIO; 79373e03419SKent Gibson 79473e03419SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) 79573e03419SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 79673e03419SKent Gibson IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 79773e03419SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) 79873e03419SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 79973e03419SKent Gibson IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 80073e03419SKent Gibson irqflags |= IRQF_ONESHOT; 80173e03419SKent Gibson 80273e03419SKent Gibson /* Request a thread to read the events */ 80373e03419SKent Gibson ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread, 80473e03419SKent Gibson irqflags, line->req->label, line); 80573e03419SKent Gibson if (ret) 80673e03419SKent Gibson return ret; 80773e03419SKent Gibson 80873e03419SKent Gibson line->irq = irq; 80973e03419SKent Gibson return 0; 81073e03419SKent Gibson } 81173e03419SKent Gibson 81265cff704SKent Gibson static int edge_detector_update(struct line *line, 81365cff704SKent Gibson struct gpio_v2_line_config *lc, 81465cff704SKent Gibson unsigned int line_idx, 81565cff704SKent Gibson u64 eflags, bool polarity_change) 816a54756cbSKent Gibson { 81765cff704SKent Gibson unsigned int debounce_period_us = 81865cff704SKent Gibson gpio_v2_line_config_debounce_period(lc, line_idx); 81965cff704SKent Gibson 82065cff704SKent Gibson if ((line->eflags == eflags) && !polarity_change && 82165cff704SKent Gibson (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us)) 822a54756cbSKent Gibson return 0; 823a54756cbSKent Gibson 82465cff704SKent Gibson /* sw debounced and still will be...*/ 82565cff704SKent Gibson if (debounce_period_us && READ_ONCE(line->sw_debounced)) { 82665cff704SKent Gibson line->eflags = eflags; 82765cff704SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 82865cff704SKent Gibson return 0; 82965cff704SKent Gibson } 83065cff704SKent Gibson 83165cff704SKent Gibson /* reconfiguring edge detection or sw debounce being disabled */ 83265cff704SKent Gibson if ((line->irq && !READ_ONCE(line->sw_debounced)) || 83365cff704SKent Gibson (!debounce_period_us && READ_ONCE(line->sw_debounced))) 834a54756cbSKent Gibson edge_detector_stop(line); 835a54756cbSKent Gibson 83665cff704SKent Gibson return edge_detector_setup(line, lc, line_idx, eflags); 837a54756cbSKent Gibson } 838a54756cbSKent Gibson 8393c0d9c63SKent Gibson static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, 8403c0d9c63SKent Gibson unsigned int line_idx) 8413c0d9c63SKent Gibson { 8423c0d9c63SKent Gibson unsigned int i; 8433c0d9c63SKent Gibson u64 mask = BIT_ULL(line_idx); 8443c0d9c63SKent Gibson 8453c0d9c63SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 8463c0d9c63SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) && 8473c0d9c63SKent Gibson (lc->attrs[i].mask & mask)) 8483c0d9c63SKent Gibson return lc->attrs[i].attr.flags; 8493c0d9c63SKent Gibson } 8503c0d9c63SKent Gibson return lc->flags; 8513c0d9c63SKent Gibson } 8523c0d9c63SKent Gibson 8533c0d9c63SKent Gibson static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc, 8543c0d9c63SKent Gibson unsigned int line_idx) 8553c0d9c63SKent Gibson { 8563c0d9c63SKent Gibson unsigned int i; 8573c0d9c63SKent Gibson u64 mask = BIT_ULL(line_idx); 8583c0d9c63SKent Gibson 8593c0d9c63SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 8603c0d9c63SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) && 8613c0d9c63SKent Gibson (lc->attrs[i].mask & mask)) 8623c0d9c63SKent Gibson return !!(lc->attrs[i].attr.values & mask); 8633c0d9c63SKent Gibson } 8643c0d9c63SKent Gibson return 0; 8653c0d9c63SKent Gibson } 8663c0d9c63SKent Gibson 8673c0d9c63SKent Gibson static int gpio_v2_line_flags_validate(u64 flags) 8683c0d9c63SKent Gibson { 8693c0d9c63SKent Gibson /* Return an error if an unknown flag is set */ 8703c0d9c63SKent Gibson if (flags & ~GPIO_V2_LINE_VALID_FLAGS) 8713c0d9c63SKent Gibson return -EINVAL; 8723c0d9c63SKent Gibson 8733c0d9c63SKent Gibson /* 8743c0d9c63SKent Gibson * Do not allow both INPUT and OUTPUT flags to be set as they are 8753c0d9c63SKent Gibson * contradictory. 8763c0d9c63SKent Gibson */ 8773c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_FLAG_INPUT) && 8783c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_OUTPUT)) 8793c0d9c63SKent Gibson return -EINVAL; 8803c0d9c63SKent Gibson 88173e03419SKent Gibson /* Edge detection requires explicit input. */ 88273e03419SKent Gibson if ((flags & GPIO_V2_LINE_EDGE_FLAGS) && 88373e03419SKent Gibson !(flags & GPIO_V2_LINE_FLAG_INPUT)) 88473e03419SKent Gibson return -EINVAL; 88573e03419SKent Gibson 8863c0d9c63SKent Gibson /* 8873c0d9c63SKent Gibson * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single 8883c0d9c63SKent Gibson * request. If the hardware actually supports enabling both at the 8893c0d9c63SKent Gibson * same time the electrical result would be disastrous. 8903c0d9c63SKent Gibson */ 8913c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) && 8923c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE)) 8933c0d9c63SKent Gibson return -EINVAL; 8943c0d9c63SKent Gibson 8953c0d9c63SKent Gibson /* Drive requires explicit output direction. */ 8963c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) && 8973c0d9c63SKent Gibson !(flags & GPIO_V2_LINE_FLAG_OUTPUT)) 8983c0d9c63SKent Gibson return -EINVAL; 8993c0d9c63SKent Gibson 9003c0d9c63SKent Gibson /* Bias requires explicit direction. */ 9013c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_BIAS_FLAGS) && 9023c0d9c63SKent Gibson !(flags & GPIO_V2_LINE_DIRECTION_FLAGS)) 9033c0d9c63SKent Gibson return -EINVAL; 9043c0d9c63SKent Gibson 9053c0d9c63SKent Gibson /* Only one bias flag can be set. */ 9063c0d9c63SKent Gibson if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) && 9073c0d9c63SKent Gibson (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | 9083c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) || 9093c0d9c63SKent Gibson ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) && 9103c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) 9113c0d9c63SKent Gibson return -EINVAL; 9123c0d9c63SKent Gibson 9133c0d9c63SKent Gibson return 0; 9143c0d9c63SKent Gibson } 9153c0d9c63SKent Gibson 9163c0d9c63SKent Gibson static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, 9173c0d9c63SKent Gibson unsigned int num_lines) 9183c0d9c63SKent Gibson { 9193c0d9c63SKent Gibson unsigned int i; 9203c0d9c63SKent Gibson u64 flags; 9213c0d9c63SKent Gibson int ret; 9223c0d9c63SKent Gibson 9233c0d9c63SKent Gibson if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX) 9243c0d9c63SKent Gibson return -EINVAL; 9253c0d9c63SKent Gibson 9263c0d9c63SKent Gibson if (memchr_inv(lc->padding, 0, sizeof(lc->padding))) 9273c0d9c63SKent Gibson return -EINVAL; 9283c0d9c63SKent Gibson 9293c0d9c63SKent Gibson for (i = 0; i < num_lines; i++) { 9303c0d9c63SKent Gibson flags = gpio_v2_line_config_flags(lc, i); 9313c0d9c63SKent Gibson ret = gpio_v2_line_flags_validate(flags); 9323c0d9c63SKent Gibson if (ret) 9333c0d9c63SKent Gibson return ret; 93465cff704SKent Gibson 93565cff704SKent Gibson /* debounce requires explicit input */ 93665cff704SKent Gibson if (gpio_v2_line_config_debounced(lc, i) && 93765cff704SKent Gibson !(flags & GPIO_V2_LINE_FLAG_INPUT)) 93865cff704SKent Gibson return -EINVAL; 9393c0d9c63SKent Gibson } 9403c0d9c63SKent Gibson return 0; 9413c0d9c63SKent Gibson } 9423c0d9c63SKent Gibson 9433c0d9c63SKent Gibson static void gpio_v2_line_config_flags_to_desc_flags(u64 flags, 9443c0d9c63SKent Gibson unsigned long *flagsp) 9453c0d9c63SKent Gibson { 9463c0d9c63SKent Gibson assign_bit(FLAG_ACTIVE_LOW, flagsp, 9473c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW); 9483c0d9c63SKent Gibson 9493c0d9c63SKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) 9503c0d9c63SKent Gibson set_bit(FLAG_IS_OUT, flagsp); 9513c0d9c63SKent Gibson else if (flags & GPIO_V2_LINE_FLAG_INPUT) 9523c0d9c63SKent Gibson clear_bit(FLAG_IS_OUT, flagsp); 9533c0d9c63SKent Gibson 95473e03419SKent Gibson assign_bit(FLAG_EDGE_RISING, flagsp, 95573e03419SKent Gibson flags & GPIO_V2_LINE_FLAG_EDGE_RISING); 95673e03419SKent Gibson assign_bit(FLAG_EDGE_FALLING, flagsp, 95773e03419SKent Gibson flags & GPIO_V2_LINE_FLAG_EDGE_FALLING); 95873e03419SKent Gibson 9593c0d9c63SKent Gibson assign_bit(FLAG_OPEN_DRAIN, flagsp, 9603c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN); 9613c0d9c63SKent Gibson assign_bit(FLAG_OPEN_SOURCE, flagsp, 9623c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE); 9633c0d9c63SKent Gibson 9643c0d9c63SKent Gibson assign_bit(FLAG_PULL_UP, flagsp, 9653c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP); 9663c0d9c63SKent Gibson assign_bit(FLAG_PULL_DOWN, flagsp, 9673c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN); 9683c0d9c63SKent Gibson assign_bit(FLAG_BIAS_DISABLE, flagsp, 9693c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED); 9703c0d9c63SKent Gibson } 9713c0d9c63SKent Gibson 9723c0d9c63SKent Gibson static long linereq_get_values(struct linereq *lr, void __user *ip) 9733c0d9c63SKent Gibson { 9743c0d9c63SKent Gibson struct gpio_v2_line_values lv; 9753c0d9c63SKent Gibson DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); 9763c0d9c63SKent Gibson struct gpio_desc **descs; 9773c0d9c63SKent Gibson unsigned int i, didx, num_get; 97865cff704SKent Gibson bool val; 9793c0d9c63SKent Gibson int ret; 9803c0d9c63SKent Gibson 9813c0d9c63SKent Gibson /* NOTE: It's ok to read values of output lines. */ 9823c0d9c63SKent Gibson if (copy_from_user(&lv, ip, sizeof(lv))) 9833c0d9c63SKent Gibson return -EFAULT; 9843c0d9c63SKent Gibson 9853c0d9c63SKent Gibson for (num_get = 0, i = 0; i < lr->num_lines; i++) { 9863c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 9873c0d9c63SKent Gibson num_get++; 9883c0d9c63SKent Gibson descs = &lr->lines[i].desc; 9893c0d9c63SKent Gibson } 9903c0d9c63SKent Gibson } 9913c0d9c63SKent Gibson 9923c0d9c63SKent Gibson if (num_get == 0) 9933c0d9c63SKent Gibson return -EINVAL; 9943c0d9c63SKent Gibson 9953c0d9c63SKent Gibson if (num_get != 1) { 9963c0d9c63SKent Gibson descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL); 9973c0d9c63SKent Gibson if (!descs) 9983c0d9c63SKent Gibson return -ENOMEM; 9993c0d9c63SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 10003c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 10013c0d9c63SKent Gibson descs[didx] = lr->lines[i].desc; 10023c0d9c63SKent Gibson didx++; 10033c0d9c63SKent Gibson } 10043c0d9c63SKent Gibson } 10053c0d9c63SKent Gibson } 10063c0d9c63SKent Gibson ret = gpiod_get_array_value_complex(false, true, num_get, 10073c0d9c63SKent Gibson descs, NULL, vals); 10083c0d9c63SKent Gibson 10093c0d9c63SKent Gibson if (num_get != 1) 10103c0d9c63SKent Gibson kfree(descs); 10113c0d9c63SKent Gibson if (ret) 10123c0d9c63SKent Gibson return ret; 10133c0d9c63SKent Gibson 10143c0d9c63SKent Gibson lv.bits = 0; 10153c0d9c63SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 10163c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 101765cff704SKent Gibson if (lr->lines[i].sw_debounced) 101865cff704SKent Gibson val = debounced_value(&lr->lines[i]); 101965cff704SKent Gibson else 102065cff704SKent Gibson val = test_bit(didx, vals); 102165cff704SKent Gibson if (val) 10223c0d9c63SKent Gibson lv.bits |= BIT_ULL(i); 10233c0d9c63SKent Gibson didx++; 10243c0d9c63SKent Gibson } 10253c0d9c63SKent Gibson } 10263c0d9c63SKent Gibson 10273c0d9c63SKent Gibson if (copy_to_user(ip, &lv, sizeof(lv))) 10283c0d9c63SKent Gibson return -EFAULT; 10293c0d9c63SKent Gibson 10303c0d9c63SKent Gibson return 0; 10313c0d9c63SKent Gibson } 10323c0d9c63SKent Gibson 10337b8e00d9SKent Gibson static long linereq_set_values_unlocked(struct linereq *lr, 10347b8e00d9SKent Gibson struct gpio_v2_line_values *lv) 10357b8e00d9SKent Gibson { 10367b8e00d9SKent Gibson DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); 10377b8e00d9SKent Gibson struct gpio_desc **descs; 10387b8e00d9SKent Gibson unsigned int i, didx, num_set; 10397b8e00d9SKent Gibson int ret; 10407b8e00d9SKent Gibson 10417b8e00d9SKent Gibson bitmap_zero(vals, GPIO_V2_LINES_MAX); 10427b8e00d9SKent Gibson for (num_set = 0, i = 0; i < lr->num_lines; i++) { 10437b8e00d9SKent Gibson if (lv->mask & BIT_ULL(i)) { 10447b8e00d9SKent Gibson if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags)) 10457b8e00d9SKent Gibson return -EPERM; 10467b8e00d9SKent Gibson if (lv->bits & BIT_ULL(i)) 10477b8e00d9SKent Gibson __set_bit(num_set, vals); 10487b8e00d9SKent Gibson num_set++; 10497b8e00d9SKent Gibson descs = &lr->lines[i].desc; 10507b8e00d9SKent Gibson } 10517b8e00d9SKent Gibson } 10527b8e00d9SKent Gibson if (num_set == 0) 10537b8e00d9SKent Gibson return -EINVAL; 10547b8e00d9SKent Gibson 10557b8e00d9SKent Gibson if (num_set != 1) { 10567b8e00d9SKent Gibson /* build compacted desc array and values */ 10577b8e00d9SKent Gibson descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL); 10587b8e00d9SKent Gibson if (!descs) 10597b8e00d9SKent Gibson return -ENOMEM; 10607b8e00d9SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 10617b8e00d9SKent Gibson if (lv->mask & BIT_ULL(i)) { 10627b8e00d9SKent Gibson descs[didx] = lr->lines[i].desc; 10637b8e00d9SKent Gibson didx++; 10647b8e00d9SKent Gibson } 10657b8e00d9SKent Gibson } 10667b8e00d9SKent Gibson } 10677b8e00d9SKent Gibson ret = gpiod_set_array_value_complex(false, true, num_set, 10687b8e00d9SKent Gibson descs, NULL, vals); 10697b8e00d9SKent Gibson 10707b8e00d9SKent Gibson if (num_set != 1) 10717b8e00d9SKent Gibson kfree(descs); 10727b8e00d9SKent Gibson return ret; 10737b8e00d9SKent Gibson } 10747b8e00d9SKent Gibson 10757b8e00d9SKent Gibson static long linereq_set_values(struct linereq *lr, void __user *ip) 10767b8e00d9SKent Gibson { 10777b8e00d9SKent Gibson struct gpio_v2_line_values lv; 10787b8e00d9SKent Gibson int ret; 10797b8e00d9SKent Gibson 10807b8e00d9SKent Gibson if (copy_from_user(&lv, ip, sizeof(lv))) 10817b8e00d9SKent Gibson return -EFAULT; 10827b8e00d9SKent Gibson 10837b8e00d9SKent Gibson mutex_lock(&lr->config_mutex); 10847b8e00d9SKent Gibson 10857b8e00d9SKent Gibson ret = linereq_set_values_unlocked(lr, &lv); 10867b8e00d9SKent Gibson 10877b8e00d9SKent Gibson mutex_unlock(&lr->config_mutex); 10887b8e00d9SKent Gibson 10897b8e00d9SKent Gibson return ret; 10907b8e00d9SKent Gibson } 10917b8e00d9SKent Gibson 1092a54756cbSKent Gibson static long linereq_set_config_unlocked(struct linereq *lr, 1093a54756cbSKent Gibson struct gpio_v2_line_config *lc) 1094a54756cbSKent Gibson { 1095a54756cbSKent Gibson struct gpio_desc *desc; 1096a54756cbSKent Gibson unsigned int i; 1097a54756cbSKent Gibson u64 flags; 1098a54756cbSKent Gibson bool polarity_change; 1099a54756cbSKent Gibson int ret; 1100a54756cbSKent Gibson 1101a54756cbSKent Gibson for (i = 0; i < lr->num_lines; i++) { 1102a54756cbSKent Gibson desc = lr->lines[i].desc; 1103a54756cbSKent Gibson flags = gpio_v2_line_config_flags(lc, i); 1104a54756cbSKent Gibson polarity_change = 1105a54756cbSKent Gibson (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) != 1106a54756cbSKent Gibson ((flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) != 0)); 1107a54756cbSKent Gibson 1108a54756cbSKent Gibson gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 1109a54756cbSKent Gibson /* 1110a54756cbSKent Gibson * Lines have to be requested explicitly for input 1111a54756cbSKent Gibson * or output, else the line will be treated "as is". 1112a54756cbSKent Gibson */ 1113a54756cbSKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 1114a54756cbSKent Gibson int val = gpio_v2_line_config_output_value(lc, i); 1115a54756cbSKent Gibson 1116a54756cbSKent Gibson edge_detector_stop(&lr->lines[i]); 1117a54756cbSKent Gibson ret = gpiod_direction_output(desc, val); 1118a54756cbSKent Gibson if (ret) 1119a54756cbSKent Gibson return ret; 1120a54756cbSKent Gibson } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { 1121a54756cbSKent Gibson ret = gpiod_direction_input(desc); 1122a54756cbSKent Gibson if (ret) 1123a54756cbSKent Gibson return ret; 1124a54756cbSKent Gibson 112565cff704SKent Gibson ret = edge_detector_update(&lr->lines[i], lc, i, 1126a54756cbSKent Gibson flags & GPIO_V2_LINE_EDGE_FLAGS, 1127a54756cbSKent Gibson polarity_change); 1128a54756cbSKent Gibson if (ret) 1129a54756cbSKent Gibson return ret; 1130a54756cbSKent Gibson } 1131a54756cbSKent Gibson 1132a54756cbSKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 1133a54756cbSKent Gibson GPIO_V2_LINE_CHANGED_CONFIG, 1134a54756cbSKent Gibson desc); 1135a54756cbSKent Gibson } 1136a54756cbSKent Gibson return 0; 1137a54756cbSKent Gibson } 1138a54756cbSKent Gibson 1139a54756cbSKent Gibson static long linereq_set_config(struct linereq *lr, void __user *ip) 1140a54756cbSKent Gibson { 1141a54756cbSKent Gibson struct gpio_v2_line_config lc; 1142a54756cbSKent Gibson int ret; 1143a54756cbSKent Gibson 1144a54756cbSKent Gibson if (copy_from_user(&lc, ip, sizeof(lc))) 1145a54756cbSKent Gibson return -EFAULT; 1146a54756cbSKent Gibson 1147a54756cbSKent Gibson ret = gpio_v2_line_config_validate(&lc, lr->num_lines); 1148a54756cbSKent Gibson if (ret) 1149a54756cbSKent Gibson return ret; 1150a54756cbSKent Gibson 1151a54756cbSKent Gibson mutex_lock(&lr->config_mutex); 1152a54756cbSKent Gibson 1153a54756cbSKent Gibson ret = linereq_set_config_unlocked(lr, &lc); 1154a54756cbSKent Gibson 1155a54756cbSKent Gibson mutex_unlock(&lr->config_mutex); 1156a54756cbSKent Gibson 1157a54756cbSKent Gibson return ret; 1158a54756cbSKent Gibson } 1159a54756cbSKent Gibson 11603c0d9c63SKent Gibson static long linereq_ioctl(struct file *file, unsigned int cmd, 11613c0d9c63SKent Gibson unsigned long arg) 11623c0d9c63SKent Gibson { 11633c0d9c63SKent Gibson struct linereq *lr = file->private_data; 11643c0d9c63SKent Gibson void __user *ip = (void __user *)arg; 11653c0d9c63SKent Gibson 11663c0d9c63SKent Gibson if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL) 11673c0d9c63SKent Gibson return linereq_get_values(lr, ip); 11687b8e00d9SKent Gibson else if (cmd == GPIO_V2_LINE_SET_VALUES_IOCTL) 11697b8e00d9SKent Gibson return linereq_set_values(lr, ip); 1170a54756cbSKent Gibson else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL) 1171a54756cbSKent Gibson return linereq_set_config(lr, ip); 11723c0d9c63SKent Gibson 11733c0d9c63SKent Gibson return -EINVAL; 11743c0d9c63SKent Gibson } 11753c0d9c63SKent Gibson 11763c0d9c63SKent Gibson #ifdef CONFIG_COMPAT 11773c0d9c63SKent Gibson static long linereq_ioctl_compat(struct file *file, unsigned int cmd, 11783c0d9c63SKent Gibson unsigned long arg) 11793c0d9c63SKent Gibson { 11803c0d9c63SKent Gibson return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 11813c0d9c63SKent Gibson } 11823c0d9c63SKent Gibson #endif 11833c0d9c63SKent Gibson 118473e03419SKent Gibson static __poll_t linereq_poll(struct file *file, 118573e03419SKent Gibson struct poll_table_struct *wait) 118673e03419SKent Gibson { 118773e03419SKent Gibson struct linereq *lr = file->private_data; 118873e03419SKent Gibson __poll_t events = 0; 118973e03419SKent Gibson 119073e03419SKent Gibson poll_wait(file, &lr->wait, wait); 119173e03419SKent Gibson 119273e03419SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events, 119373e03419SKent Gibson &lr->wait.lock)) 119473e03419SKent Gibson events = EPOLLIN | EPOLLRDNORM; 119573e03419SKent Gibson 119673e03419SKent Gibson return events; 119773e03419SKent Gibson } 119873e03419SKent Gibson 119973e03419SKent Gibson static ssize_t linereq_read(struct file *file, 120073e03419SKent Gibson char __user *buf, 120173e03419SKent Gibson size_t count, 120273e03419SKent Gibson loff_t *f_ps) 120373e03419SKent Gibson { 120473e03419SKent Gibson struct linereq *lr = file->private_data; 120573e03419SKent Gibson struct gpio_v2_line_event le; 120673e03419SKent Gibson ssize_t bytes_read = 0; 120773e03419SKent Gibson int ret; 120873e03419SKent Gibson 120973e03419SKent Gibson if (count < sizeof(le)) 121073e03419SKent Gibson return -EINVAL; 121173e03419SKent Gibson 121273e03419SKent Gibson do { 121373e03419SKent Gibson spin_lock(&lr->wait.lock); 121473e03419SKent Gibson if (kfifo_is_empty(&lr->events)) { 121573e03419SKent Gibson if (bytes_read) { 121673e03419SKent Gibson spin_unlock(&lr->wait.lock); 121773e03419SKent Gibson return bytes_read; 121873e03419SKent Gibson } 121973e03419SKent Gibson 122073e03419SKent Gibson if (file->f_flags & O_NONBLOCK) { 122173e03419SKent Gibson spin_unlock(&lr->wait.lock); 122273e03419SKent Gibson return -EAGAIN; 122373e03419SKent Gibson } 122473e03419SKent Gibson 122573e03419SKent Gibson ret = wait_event_interruptible_locked(lr->wait, 122673e03419SKent Gibson !kfifo_is_empty(&lr->events)); 122773e03419SKent Gibson if (ret) { 122873e03419SKent Gibson spin_unlock(&lr->wait.lock); 122973e03419SKent Gibson return ret; 123073e03419SKent Gibson } 123173e03419SKent Gibson } 123273e03419SKent Gibson 123373e03419SKent Gibson ret = kfifo_out(&lr->events, &le, 1); 123473e03419SKent Gibson spin_unlock(&lr->wait.lock); 123573e03419SKent Gibson if (ret != 1) { 123673e03419SKent Gibson /* 123773e03419SKent Gibson * This should never happen - we were holding the 123873e03419SKent Gibson * lock from the moment we learned the fifo is no 123973e03419SKent Gibson * longer empty until now. 124073e03419SKent Gibson */ 124173e03419SKent Gibson ret = -EIO; 124273e03419SKent Gibson break; 124373e03419SKent Gibson } 124473e03419SKent Gibson 124573e03419SKent Gibson if (copy_to_user(buf + bytes_read, &le, sizeof(le))) 124673e03419SKent Gibson return -EFAULT; 124773e03419SKent Gibson bytes_read += sizeof(le); 124873e03419SKent Gibson } while (count >= bytes_read + sizeof(le)); 124973e03419SKent Gibson 125073e03419SKent Gibson return bytes_read; 125173e03419SKent Gibson } 125273e03419SKent Gibson 12533c0d9c63SKent Gibson static void linereq_free(struct linereq *lr) 12543c0d9c63SKent Gibson { 12553c0d9c63SKent Gibson unsigned int i; 12563c0d9c63SKent Gibson 12573c0d9c63SKent Gibson for (i = 0; i < lr->num_lines; i++) { 125873e03419SKent Gibson edge_detector_stop(&lr->lines[i]); 12593c0d9c63SKent Gibson if (lr->lines[i].desc) 12603c0d9c63SKent Gibson gpiod_free(lr->lines[i].desc); 12613c0d9c63SKent Gibson } 126273e03419SKent Gibson kfifo_free(&lr->events); 12633c0d9c63SKent Gibson kfree(lr->label); 12643c0d9c63SKent Gibson put_device(&lr->gdev->dev); 12653c0d9c63SKent Gibson kfree(lr); 12663c0d9c63SKent Gibson } 12673c0d9c63SKent Gibson 12683c0d9c63SKent Gibson static int linereq_release(struct inode *inode, struct file *file) 12693c0d9c63SKent Gibson { 12703c0d9c63SKent Gibson struct linereq *lr = file->private_data; 12713c0d9c63SKent Gibson 12723c0d9c63SKent Gibson linereq_free(lr); 12733c0d9c63SKent Gibson return 0; 12743c0d9c63SKent Gibson } 12753c0d9c63SKent Gibson 12763c0d9c63SKent Gibson static const struct file_operations line_fileops = { 12773c0d9c63SKent Gibson .release = linereq_release, 127873e03419SKent Gibson .read = linereq_read, 127973e03419SKent Gibson .poll = linereq_poll, 12803c0d9c63SKent Gibson .owner = THIS_MODULE, 12813c0d9c63SKent Gibson .llseek = noop_llseek, 12823c0d9c63SKent Gibson .unlocked_ioctl = linereq_ioctl, 12833c0d9c63SKent Gibson #ifdef CONFIG_COMPAT 12843c0d9c63SKent Gibson .compat_ioctl = linereq_ioctl_compat, 12853c0d9c63SKent Gibson #endif 12863c0d9c63SKent Gibson }; 12873c0d9c63SKent Gibson 12883c0d9c63SKent Gibson static int linereq_create(struct gpio_device *gdev, void __user *ip) 12893c0d9c63SKent Gibson { 12903c0d9c63SKent Gibson struct gpio_v2_line_request ulr; 12913c0d9c63SKent Gibson struct gpio_v2_line_config *lc; 12923c0d9c63SKent Gibson struct linereq *lr; 12933c0d9c63SKent Gibson struct file *file; 12943c0d9c63SKent Gibson u64 flags; 12953c0d9c63SKent Gibson unsigned int i; 12963c0d9c63SKent Gibson int fd, ret; 12973c0d9c63SKent Gibson 12983c0d9c63SKent Gibson if (copy_from_user(&ulr, ip, sizeof(ulr))) 12993c0d9c63SKent Gibson return -EFAULT; 13003c0d9c63SKent Gibson 13013c0d9c63SKent Gibson if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX)) 13023c0d9c63SKent Gibson return -EINVAL; 13033c0d9c63SKent Gibson 13043c0d9c63SKent Gibson if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding))) 13053c0d9c63SKent Gibson return -EINVAL; 13063c0d9c63SKent Gibson 13073c0d9c63SKent Gibson lc = &ulr.config; 13083c0d9c63SKent Gibson ret = gpio_v2_line_config_validate(lc, ulr.num_lines); 13093c0d9c63SKent Gibson if (ret) 13103c0d9c63SKent Gibson return ret; 13113c0d9c63SKent Gibson 13123c0d9c63SKent Gibson lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); 13133c0d9c63SKent Gibson if (!lr) 13143c0d9c63SKent Gibson return -ENOMEM; 13153c0d9c63SKent Gibson 13163c0d9c63SKent Gibson lr->gdev = gdev; 13173c0d9c63SKent Gibson get_device(&gdev->dev); 13183c0d9c63SKent Gibson 131965cff704SKent Gibson for (i = 0; i < ulr.num_lines; i++) { 132073e03419SKent Gibson lr->lines[i].req = lr; 132165cff704SKent Gibson WRITE_ONCE(lr->lines[i].sw_debounced, 0); 132265cff704SKent Gibson INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func); 132365cff704SKent Gibson } 132473e03419SKent Gibson 1325f188ac12SKent Gibson if (ulr.consumer[0] != '\0') { 13263c0d9c63SKent Gibson /* label is only initialized if consumer is set */ 1327f188ac12SKent Gibson lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1, 1328f188ac12SKent Gibson GFP_KERNEL); 13293c0d9c63SKent Gibson if (!lr->label) { 13303c0d9c63SKent Gibson ret = -ENOMEM; 13313c0d9c63SKent Gibson goto out_free_linereq; 13323c0d9c63SKent Gibson } 13333c0d9c63SKent Gibson } 13343c0d9c63SKent Gibson 1335a54756cbSKent Gibson mutex_init(&lr->config_mutex); 133673e03419SKent Gibson init_waitqueue_head(&lr->wait); 133773e03419SKent Gibson lr->event_buffer_size = ulr.event_buffer_size; 133873e03419SKent Gibson if (lr->event_buffer_size == 0) 133973e03419SKent Gibson lr->event_buffer_size = ulr.num_lines * 16; 134073e03419SKent Gibson else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16) 134173e03419SKent Gibson lr->event_buffer_size = GPIO_V2_LINES_MAX * 16; 134273e03419SKent Gibson 134373e03419SKent Gibson atomic_set(&lr->seqno, 0); 13443c0d9c63SKent Gibson lr->num_lines = ulr.num_lines; 13453c0d9c63SKent Gibson 13463c0d9c63SKent Gibson /* Request each GPIO */ 13473c0d9c63SKent Gibson for (i = 0; i < ulr.num_lines; i++) { 13483c0d9c63SKent Gibson u32 offset = ulr.offsets[i]; 13493c0d9c63SKent Gibson struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); 13503c0d9c63SKent Gibson 13513c0d9c63SKent Gibson if (IS_ERR(desc)) { 13523c0d9c63SKent Gibson ret = PTR_ERR(desc); 13533c0d9c63SKent Gibson goto out_free_linereq; 13543c0d9c63SKent Gibson } 13553c0d9c63SKent Gibson 13563c0d9c63SKent Gibson ret = gpiod_request(desc, lr->label); 13573c0d9c63SKent Gibson if (ret) 13583c0d9c63SKent Gibson goto out_free_linereq; 13593c0d9c63SKent Gibson 13603c0d9c63SKent Gibson lr->lines[i].desc = desc; 13613c0d9c63SKent Gibson flags = gpio_v2_line_config_flags(lc, i); 13623c0d9c63SKent Gibson gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 13633c0d9c63SKent Gibson 13643c0d9c63SKent Gibson ret = gpiod_set_transitory(desc, false); 13653c0d9c63SKent Gibson if (ret < 0) 13663c0d9c63SKent Gibson goto out_free_linereq; 13673c0d9c63SKent Gibson 13683c0d9c63SKent Gibson /* 13693c0d9c63SKent Gibson * Lines have to be requested explicitly for input 13703c0d9c63SKent Gibson * or output, else the line will be treated "as is". 13713c0d9c63SKent Gibson */ 13723c0d9c63SKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 13733c0d9c63SKent Gibson int val = gpio_v2_line_config_output_value(lc, i); 13743c0d9c63SKent Gibson 13753c0d9c63SKent Gibson ret = gpiod_direction_output(desc, val); 13763c0d9c63SKent Gibson if (ret) 13773c0d9c63SKent Gibson goto out_free_linereq; 13783c0d9c63SKent Gibson } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { 13793c0d9c63SKent Gibson ret = gpiod_direction_input(desc); 13803c0d9c63SKent Gibson if (ret) 13813c0d9c63SKent Gibson goto out_free_linereq; 138273e03419SKent Gibson 138365cff704SKent Gibson ret = edge_detector_setup(&lr->lines[i], lc, i, 138473e03419SKent Gibson flags & GPIO_V2_LINE_EDGE_FLAGS); 138573e03419SKent Gibson if (ret) 138673e03419SKent Gibson goto out_free_linereq; 13873c0d9c63SKent Gibson } 13883c0d9c63SKent Gibson 13893c0d9c63SKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 1390aad95584SKent Gibson GPIO_V2_LINE_CHANGED_REQUESTED, desc); 13913c0d9c63SKent Gibson 13923c0d9c63SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", 13933c0d9c63SKent Gibson offset); 13943c0d9c63SKent Gibson } 13953c0d9c63SKent Gibson 13963c0d9c63SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 13973c0d9c63SKent Gibson if (fd < 0) { 13983c0d9c63SKent Gibson ret = fd; 13993c0d9c63SKent Gibson goto out_free_linereq; 14003c0d9c63SKent Gibson } 14013c0d9c63SKent Gibson 14023c0d9c63SKent Gibson file = anon_inode_getfile("gpio-line", &line_fileops, lr, 14033c0d9c63SKent Gibson O_RDONLY | O_CLOEXEC); 14043c0d9c63SKent Gibson if (IS_ERR(file)) { 14053c0d9c63SKent Gibson ret = PTR_ERR(file); 14063c0d9c63SKent Gibson goto out_put_unused_fd; 14073c0d9c63SKent Gibson } 14083c0d9c63SKent Gibson 14093c0d9c63SKent Gibson ulr.fd = fd; 14103c0d9c63SKent Gibson if (copy_to_user(ip, &ulr, sizeof(ulr))) { 14113c0d9c63SKent Gibson /* 14123c0d9c63SKent Gibson * fput() will trigger the release() callback, so do not go onto 14133c0d9c63SKent Gibson * the regular error cleanup path here. 14143c0d9c63SKent Gibson */ 14153c0d9c63SKent Gibson fput(file); 14163c0d9c63SKent Gibson put_unused_fd(fd); 14173c0d9c63SKent Gibson return -EFAULT; 14183c0d9c63SKent Gibson } 14193c0d9c63SKent Gibson 14203c0d9c63SKent Gibson fd_install(fd, file); 14213c0d9c63SKent Gibson 14223c0d9c63SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", 14233c0d9c63SKent Gibson lr->num_lines); 14243c0d9c63SKent Gibson 14253c0d9c63SKent Gibson return 0; 14263c0d9c63SKent Gibson 14273c0d9c63SKent Gibson out_put_unused_fd: 14283c0d9c63SKent Gibson put_unused_fd(fd); 14293c0d9c63SKent Gibson out_free_linereq: 14303c0d9c63SKent Gibson linereq_free(lr); 14313c0d9c63SKent Gibson return ret; 14323c0d9c63SKent Gibson } 14333c0d9c63SKent Gibson 14343c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 1435925ca369SKent Gibson 1436925ca369SKent Gibson /* 1437925ca369SKent Gibson * GPIO line event management 1438925ca369SKent Gibson */ 1439925ca369SKent Gibson 1440925ca369SKent Gibson /** 1441925ca369SKent Gibson * struct lineevent_state - contains the state of a userspace event 1442925ca369SKent Gibson * @gdev: the GPIO device the event pertains to 1443925ca369SKent Gibson * @label: consumer label used to tag descriptors 1444925ca369SKent Gibson * @desc: the GPIO descriptor held by this event 1445925ca369SKent Gibson * @eflags: the event flags this line was requested with 1446925ca369SKent Gibson * @irq: the interrupt that trigger in response to events on this GPIO 1447925ca369SKent Gibson * @wait: wait queue that handles blocking reads of events 1448925ca369SKent Gibson * @events: KFIFO for the GPIO events 1449925ca369SKent Gibson * @timestamp: cache for the timestamp storing it between hardirq 1450925ca369SKent Gibson * and IRQ thread, used to bring the timestamp close to the actual 1451925ca369SKent Gibson * event 1452925ca369SKent Gibson */ 1453925ca369SKent Gibson struct lineevent_state { 1454925ca369SKent Gibson struct gpio_device *gdev; 1455925ca369SKent Gibson const char *label; 1456925ca369SKent Gibson struct gpio_desc *desc; 1457925ca369SKent Gibson u32 eflags; 1458925ca369SKent Gibson int irq; 1459925ca369SKent Gibson wait_queue_head_t wait; 1460925ca369SKent Gibson DECLARE_KFIFO(events, struct gpioevent_data, 16); 1461925ca369SKent Gibson u64 timestamp; 1462925ca369SKent Gibson }; 1463925ca369SKent Gibson 1464925ca369SKent Gibson #define GPIOEVENT_REQUEST_VALID_FLAGS \ 1465925ca369SKent Gibson (GPIOEVENT_REQUEST_RISING_EDGE | \ 1466925ca369SKent Gibson GPIOEVENT_REQUEST_FALLING_EDGE) 1467925ca369SKent Gibson 146849bc5279SKent Gibson static __poll_t lineevent_poll(struct file *file, 1469925ca369SKent Gibson struct poll_table_struct *wait) 1470925ca369SKent Gibson { 147149bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1472925ca369SKent Gibson __poll_t events = 0; 1473925ca369SKent Gibson 147449bc5279SKent Gibson poll_wait(file, &le->wait, wait); 1475925ca369SKent Gibson 1476925ca369SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock)) 1477925ca369SKent Gibson events = EPOLLIN | EPOLLRDNORM; 1478925ca369SKent Gibson 1479925ca369SKent Gibson return events; 1480925ca369SKent Gibson } 1481925ca369SKent Gibson 14825ad284abSAndy Shevchenko struct compat_gpioeevent_data { 14835ad284abSAndy Shevchenko compat_u64 timestamp; 14845ad284abSAndy Shevchenko u32 id; 14855ad284abSAndy Shevchenko }; 14865ad284abSAndy Shevchenko 148749bc5279SKent Gibson static ssize_t lineevent_read(struct file *file, 1488925ca369SKent Gibson char __user *buf, 1489925ca369SKent Gibson size_t count, 1490925ca369SKent Gibson loff_t *f_ps) 1491925ca369SKent Gibson { 149249bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1493925ca369SKent Gibson struct gpioevent_data ge; 1494925ca369SKent Gibson ssize_t bytes_read = 0; 14955ad284abSAndy Shevchenko ssize_t ge_size; 1496925ca369SKent Gibson int ret; 1497925ca369SKent Gibson 14985ad284abSAndy Shevchenko /* 14995ad284abSAndy Shevchenko * When compatible system call is being used the struct gpioevent_data, 15005ad284abSAndy Shevchenko * in case of at least ia32, has different size due to the alignment 15015ad284abSAndy Shevchenko * differences. Because we have first member 64 bits followed by one of 15025ad284abSAndy Shevchenko * 32 bits there is no gap between them. The only difference is the 15035ad284abSAndy Shevchenko * padding at the end of the data structure. Hence, we calculate the 15045ad284abSAndy Shevchenko * actual sizeof() and pass this as an argument to copy_to_user() to 15055ad284abSAndy Shevchenko * drop unneeded bytes from the output. 15065ad284abSAndy Shevchenko */ 1507*163d1719SAndy Shevchenko if (compat_need_64bit_alignment_fixup()) 1508*163d1719SAndy Shevchenko ge_size = sizeof(struct compat_gpioeevent_data); 1509*163d1719SAndy Shevchenko else 1510*163d1719SAndy Shevchenko ge_size = sizeof(struct gpioevent_data); 15115ad284abSAndy Shevchenko if (count < ge_size) 1512925ca369SKent Gibson return -EINVAL; 1513925ca369SKent Gibson 1514925ca369SKent Gibson do { 1515925ca369SKent Gibson spin_lock(&le->wait.lock); 1516925ca369SKent Gibson if (kfifo_is_empty(&le->events)) { 1517925ca369SKent Gibson if (bytes_read) { 1518925ca369SKent Gibson spin_unlock(&le->wait.lock); 1519925ca369SKent Gibson return bytes_read; 1520925ca369SKent Gibson } 1521925ca369SKent Gibson 152249bc5279SKent Gibson if (file->f_flags & O_NONBLOCK) { 1523925ca369SKent Gibson spin_unlock(&le->wait.lock); 1524925ca369SKent Gibson return -EAGAIN; 1525925ca369SKent Gibson } 1526925ca369SKent Gibson 1527925ca369SKent Gibson ret = wait_event_interruptible_locked(le->wait, 1528925ca369SKent Gibson !kfifo_is_empty(&le->events)); 1529925ca369SKent Gibson if (ret) { 1530925ca369SKent Gibson spin_unlock(&le->wait.lock); 1531925ca369SKent Gibson return ret; 1532925ca369SKent Gibson } 1533925ca369SKent Gibson } 1534925ca369SKent Gibson 1535925ca369SKent Gibson ret = kfifo_out(&le->events, &ge, 1); 1536925ca369SKent Gibson spin_unlock(&le->wait.lock); 1537925ca369SKent Gibson if (ret != 1) { 1538925ca369SKent Gibson /* 1539925ca369SKent Gibson * This should never happen - we were holding the lock 1540925ca369SKent Gibson * from the moment we learned the fifo is no longer 1541925ca369SKent Gibson * empty until now. 1542925ca369SKent Gibson */ 1543925ca369SKent Gibson ret = -EIO; 1544925ca369SKent Gibson break; 1545925ca369SKent Gibson } 1546925ca369SKent Gibson 15475ad284abSAndy Shevchenko if (copy_to_user(buf + bytes_read, &ge, ge_size)) 1548925ca369SKent Gibson return -EFAULT; 15495ad284abSAndy Shevchenko bytes_read += ge_size; 15505ad284abSAndy Shevchenko } while (count >= bytes_read + ge_size); 1551925ca369SKent Gibson 1552925ca369SKent Gibson return bytes_read; 1553925ca369SKent Gibson } 1554925ca369SKent Gibson 155546824272SKent Gibson static void lineevent_free(struct lineevent_state *le) 1556925ca369SKent Gibson { 155746824272SKent Gibson if (le->irq) 1558925ca369SKent Gibson free_irq(le->irq, le); 155946824272SKent Gibson if (le->desc) 1560925ca369SKent Gibson gpiod_free(le->desc); 1561925ca369SKent Gibson kfree(le->label); 156246824272SKent Gibson put_device(&le->gdev->dev); 1563925ca369SKent Gibson kfree(le); 156446824272SKent Gibson } 156546824272SKent Gibson 156646824272SKent Gibson static int lineevent_release(struct inode *inode, struct file *file) 156746824272SKent Gibson { 156846824272SKent Gibson lineevent_free(file->private_data); 1569925ca369SKent Gibson return 0; 1570925ca369SKent Gibson } 1571925ca369SKent Gibson 157249bc5279SKent Gibson static long lineevent_ioctl(struct file *file, unsigned int cmd, 1573925ca369SKent Gibson unsigned long arg) 1574925ca369SKent Gibson { 157549bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1576925ca369SKent Gibson void __user *ip = (void __user *)arg; 1577925ca369SKent Gibson struct gpiohandle_data ghd; 1578925ca369SKent Gibson 1579925ca369SKent Gibson /* 1580925ca369SKent Gibson * We can get the value for an event line but not set it, 1581925ca369SKent Gibson * because it is input by definition. 1582925ca369SKent Gibson */ 1583925ca369SKent Gibson if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { 1584925ca369SKent Gibson int val; 1585925ca369SKent Gibson 1586925ca369SKent Gibson memset(&ghd, 0, sizeof(ghd)); 1587925ca369SKent Gibson 1588925ca369SKent Gibson val = gpiod_get_value_cansleep(le->desc); 1589925ca369SKent Gibson if (val < 0) 1590925ca369SKent Gibson return val; 1591925ca369SKent Gibson ghd.values[0] = val; 1592925ca369SKent Gibson 1593925ca369SKent Gibson if (copy_to_user(ip, &ghd, sizeof(ghd))) 1594925ca369SKent Gibson return -EFAULT; 1595925ca369SKent Gibson 1596925ca369SKent Gibson return 0; 1597925ca369SKent Gibson } 1598925ca369SKent Gibson return -EINVAL; 1599925ca369SKent Gibson } 1600925ca369SKent Gibson 1601925ca369SKent Gibson #ifdef CONFIG_COMPAT 160249bc5279SKent Gibson static long lineevent_ioctl_compat(struct file *file, unsigned int cmd, 1603925ca369SKent Gibson unsigned long arg) 1604925ca369SKent Gibson { 160549bc5279SKent Gibson return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 1606925ca369SKent Gibson } 1607925ca369SKent Gibson #endif 1608925ca369SKent Gibson 1609925ca369SKent Gibson static const struct file_operations lineevent_fileops = { 1610925ca369SKent Gibson .release = lineevent_release, 1611925ca369SKent Gibson .read = lineevent_read, 1612925ca369SKent Gibson .poll = lineevent_poll, 1613925ca369SKent Gibson .owner = THIS_MODULE, 1614925ca369SKent Gibson .llseek = noop_llseek, 1615925ca369SKent Gibson .unlocked_ioctl = lineevent_ioctl, 1616925ca369SKent Gibson #ifdef CONFIG_COMPAT 1617925ca369SKent Gibson .compat_ioctl = lineevent_ioctl_compat, 1618925ca369SKent Gibson #endif 1619925ca369SKent Gibson }; 1620925ca369SKent Gibson 1621925ca369SKent Gibson static irqreturn_t lineevent_irq_thread(int irq, void *p) 1622925ca369SKent Gibson { 1623925ca369SKent Gibson struct lineevent_state *le = p; 1624925ca369SKent Gibson struct gpioevent_data ge; 1625925ca369SKent Gibson int ret; 1626925ca369SKent Gibson 1627925ca369SKent Gibson /* Do not leak kernel stack to userspace */ 1628925ca369SKent Gibson memset(&ge, 0, sizeof(ge)); 1629925ca369SKent Gibson 1630925ca369SKent Gibson /* 1631925ca369SKent Gibson * We may be running from a nested threaded interrupt in which case 1632925ca369SKent Gibson * we didn't get the timestamp from lineevent_irq_handler(). 1633925ca369SKent Gibson */ 1634925ca369SKent Gibson if (!le->timestamp) 1635925ca369SKent Gibson ge.timestamp = ktime_get_ns(); 1636925ca369SKent Gibson else 1637925ca369SKent Gibson ge.timestamp = le->timestamp; 1638925ca369SKent Gibson 1639925ca369SKent Gibson if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE 1640925ca369SKent Gibson && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { 1641925ca369SKent Gibson int level = gpiod_get_value_cansleep(le->desc); 1642925ca369SKent Gibson 1643925ca369SKent Gibson if (level) 1644925ca369SKent Gibson /* Emit low-to-high event */ 1645925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_RISING_EDGE; 1646925ca369SKent Gibson else 1647925ca369SKent Gibson /* Emit high-to-low event */ 1648925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_FALLING_EDGE; 1649925ca369SKent Gibson } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) { 1650925ca369SKent Gibson /* Emit low-to-high event */ 1651925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_RISING_EDGE; 1652925ca369SKent Gibson } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { 1653925ca369SKent Gibson /* Emit high-to-low event */ 1654925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_FALLING_EDGE; 1655925ca369SKent Gibson } else { 1656925ca369SKent Gibson return IRQ_NONE; 1657925ca369SKent Gibson } 1658925ca369SKent Gibson 1659925ca369SKent Gibson ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge, 1660925ca369SKent Gibson 1, &le->wait.lock); 1661925ca369SKent Gibson if (ret) 1662925ca369SKent Gibson wake_up_poll(&le->wait, EPOLLIN); 1663925ca369SKent Gibson else 1664925ca369SKent Gibson pr_debug_ratelimited("event FIFO is full - event dropped\n"); 1665925ca369SKent Gibson 1666925ca369SKent Gibson return IRQ_HANDLED; 1667925ca369SKent Gibson } 1668925ca369SKent Gibson 1669925ca369SKent Gibson static irqreturn_t lineevent_irq_handler(int irq, void *p) 1670925ca369SKent Gibson { 1671925ca369SKent Gibson struct lineevent_state *le = p; 1672925ca369SKent Gibson 1673925ca369SKent Gibson /* 1674925ca369SKent Gibson * Just store the timestamp in hardirq context so we get it as 1675925ca369SKent Gibson * close in time as possible to the actual event. 1676925ca369SKent Gibson */ 1677925ca369SKent Gibson le->timestamp = ktime_get_ns(); 1678925ca369SKent Gibson 1679925ca369SKent Gibson return IRQ_WAKE_THREAD; 1680925ca369SKent Gibson } 1681925ca369SKent Gibson 1682925ca369SKent Gibson static int lineevent_create(struct gpio_device *gdev, void __user *ip) 1683925ca369SKent Gibson { 1684925ca369SKent Gibson struct gpioevent_request eventreq; 1685925ca369SKent Gibson struct lineevent_state *le; 1686925ca369SKent Gibson struct gpio_desc *desc; 1687925ca369SKent Gibson struct file *file; 1688925ca369SKent Gibson u32 offset; 1689925ca369SKent Gibson u32 lflags; 1690925ca369SKent Gibson u32 eflags; 1691925ca369SKent Gibson int fd; 1692925ca369SKent Gibson int ret; 169346824272SKent Gibson int irq, irqflags = 0; 1694925ca369SKent Gibson 1695925ca369SKent Gibson if (copy_from_user(&eventreq, ip, sizeof(eventreq))) 1696925ca369SKent Gibson return -EFAULT; 1697925ca369SKent Gibson 1698925ca369SKent Gibson offset = eventreq.lineoffset; 1699925ca369SKent Gibson lflags = eventreq.handleflags; 1700925ca369SKent Gibson eflags = eventreq.eventflags; 1701925ca369SKent Gibson 1702925ca369SKent Gibson desc = gpiochip_get_desc(gdev->chip, offset); 1703925ca369SKent Gibson if (IS_ERR(desc)) 1704925ca369SKent Gibson return PTR_ERR(desc); 1705925ca369SKent Gibson 1706925ca369SKent Gibson /* Return an error if a unknown flag is set */ 1707925ca369SKent Gibson if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || 1708925ca369SKent Gibson (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) 1709925ca369SKent Gibson return -EINVAL; 1710925ca369SKent Gibson 1711925ca369SKent Gibson /* This is just wrong: we don't look for events on output lines */ 1712925ca369SKent Gibson if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) || 1713925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || 1714925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) 1715925ca369SKent Gibson return -EINVAL; 1716925ca369SKent Gibson 1717925ca369SKent Gibson /* Only one bias flag can be set. */ 1718925ca369SKent Gibson if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && 1719925ca369SKent Gibson (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | 1720925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || 1721925ca369SKent Gibson ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && 1722925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) 1723925ca369SKent Gibson return -EINVAL; 1724925ca369SKent Gibson 1725925ca369SKent Gibson le = kzalloc(sizeof(*le), GFP_KERNEL); 1726925ca369SKent Gibson if (!le) 1727925ca369SKent Gibson return -ENOMEM; 1728925ca369SKent Gibson le->gdev = gdev; 1729925ca369SKent Gibson get_device(&gdev->dev); 1730925ca369SKent Gibson 1731f188ac12SKent Gibson if (eventreq.consumer_label[0] != '\0') { 1732f188ac12SKent Gibson /* label is only initialized if consumer_label is set */ 1733f188ac12SKent Gibson le->label = kstrndup(eventreq.consumer_label, 1734f188ac12SKent Gibson sizeof(eventreq.consumer_label) - 1, 1735925ca369SKent Gibson GFP_KERNEL); 1736925ca369SKent Gibson if (!le->label) { 1737925ca369SKent Gibson ret = -ENOMEM; 1738925ca369SKent Gibson goto out_free_le; 1739925ca369SKent Gibson } 1740925ca369SKent Gibson } 1741925ca369SKent Gibson 1742925ca369SKent Gibson ret = gpiod_request(desc, le->label); 1743925ca369SKent Gibson if (ret) 174446824272SKent Gibson goto out_free_le; 1745925ca369SKent Gibson le->desc = desc; 1746925ca369SKent Gibson le->eflags = eflags; 1747925ca369SKent Gibson 1748c274b58aSKent Gibson linehandle_flags_to_desc_flags(lflags, &desc->flags); 1749925ca369SKent Gibson 1750925ca369SKent Gibson ret = gpiod_direction_input(desc); 1751925ca369SKent Gibson if (ret) 175246824272SKent Gibson goto out_free_le; 1753925ca369SKent Gibson 17546accc376SKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 1755aad95584SKent Gibson GPIO_V2_LINE_CHANGED_REQUESTED, desc); 1756925ca369SKent Gibson 175746824272SKent Gibson irq = gpiod_to_irq(desc); 175846824272SKent Gibson if (irq <= 0) { 1759925ca369SKent Gibson ret = -ENODEV; 176046824272SKent Gibson goto out_free_le; 1761925ca369SKent Gibson } 176246824272SKent Gibson le->irq = irq; 1763925ca369SKent Gibson 1764925ca369SKent Gibson if (eflags & GPIOEVENT_REQUEST_RISING_EDGE) 1765925ca369SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 1766925ca369SKent Gibson IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 1767925ca369SKent Gibson if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE) 1768925ca369SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 1769925ca369SKent Gibson IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 1770925ca369SKent Gibson irqflags |= IRQF_ONESHOT; 1771925ca369SKent Gibson 1772925ca369SKent Gibson INIT_KFIFO(le->events); 1773925ca369SKent Gibson init_waitqueue_head(&le->wait); 1774925ca369SKent Gibson 1775925ca369SKent Gibson /* Request a thread to read the events */ 1776925ca369SKent Gibson ret = request_threaded_irq(le->irq, 1777925ca369SKent Gibson lineevent_irq_handler, 1778925ca369SKent Gibson lineevent_irq_thread, 1779925ca369SKent Gibson irqflags, 1780925ca369SKent Gibson le->label, 1781925ca369SKent Gibson le); 1782925ca369SKent Gibson if (ret) 178346824272SKent Gibson goto out_free_le; 1784925ca369SKent Gibson 1785925ca369SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 1786925ca369SKent Gibson if (fd < 0) { 1787925ca369SKent Gibson ret = fd; 178846824272SKent Gibson goto out_free_le; 1789925ca369SKent Gibson } 1790925ca369SKent Gibson 1791925ca369SKent Gibson file = anon_inode_getfile("gpio-event", 1792925ca369SKent Gibson &lineevent_fileops, 1793925ca369SKent Gibson le, 1794925ca369SKent Gibson O_RDONLY | O_CLOEXEC); 1795925ca369SKent Gibson if (IS_ERR(file)) { 1796925ca369SKent Gibson ret = PTR_ERR(file); 1797925ca369SKent Gibson goto out_put_unused_fd; 1798925ca369SKent Gibson } 1799925ca369SKent Gibson 1800925ca369SKent Gibson eventreq.fd = fd; 1801925ca369SKent Gibson if (copy_to_user(ip, &eventreq, sizeof(eventreq))) { 1802925ca369SKent Gibson /* 1803925ca369SKent Gibson * fput() will trigger the release() callback, so do not go onto 1804925ca369SKent Gibson * the regular error cleanup path here. 1805925ca369SKent Gibson */ 1806925ca369SKent Gibson fput(file); 1807925ca369SKent Gibson put_unused_fd(fd); 1808925ca369SKent Gibson return -EFAULT; 1809925ca369SKent Gibson } 1810925ca369SKent Gibson 1811925ca369SKent Gibson fd_install(fd, file); 1812925ca369SKent Gibson 1813925ca369SKent Gibson return 0; 1814925ca369SKent Gibson 1815925ca369SKent Gibson out_put_unused_fd: 1816925ca369SKent Gibson put_unused_fd(fd); 1817925ca369SKent Gibson out_free_le: 181846824272SKent Gibson lineevent_free(le); 1819925ca369SKent Gibson return ret; 1820925ca369SKent Gibson } 1821925ca369SKent Gibson 1822aad95584SKent Gibson static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2, 1823aad95584SKent Gibson struct gpioline_info *info_v1) 1824aad95584SKent Gibson { 1825aad95584SKent Gibson u64 flagsv2 = info_v2->flags; 1826aad95584SKent Gibson 1827aad95584SKent Gibson memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name)); 1828aad95584SKent Gibson memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer)); 1829aad95584SKent Gibson info_v1->line_offset = info_v2->offset; 1830aad95584SKent Gibson info_v1->flags = 0; 1831aad95584SKent Gibson 1832aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_USED) 1833aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_KERNEL; 1834aad95584SKent Gibson 1835aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT) 1836aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_IS_OUT; 1837aad95584SKent Gibson 1838aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 1839aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW; 1840aad95584SKent Gibson 1841aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN) 1842aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN; 1843aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE) 1844aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE; 1845aad95584SKent Gibson 1846aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP) 1847aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; 1848aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) 1849aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; 1850aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED) 1851aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE; 1852aad95584SKent Gibson } 1853aad95584SKent Gibson 1854aad95584SKent Gibson static void gpio_v2_line_info_changed_to_v1( 1855aad95584SKent Gibson struct gpio_v2_line_info_changed *lic_v2, 1856aad95584SKent Gibson struct gpioline_info_changed *lic_v1) 1857aad95584SKent Gibson { 1858aad95584SKent Gibson gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info); 1859aad95584SKent Gibson lic_v1->timestamp = lic_v2->timestamp_ns; 1860aad95584SKent Gibson lic_v1->event_type = lic_v2->event_type; 1861aad95584SKent Gibson } 1862aad95584SKent Gibson 18633c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 18643c0d9c63SKent Gibson 1865925ca369SKent Gibson static void gpio_desc_to_lineinfo(struct gpio_desc *desc, 1866aad95584SKent Gibson struct gpio_v2_line_info *info) 1867925ca369SKent Gibson { 1868925ca369SKent Gibson struct gpio_chip *gc = desc->gdev->chip; 1869925ca369SKent Gibson bool ok_for_pinctrl; 1870925ca369SKent Gibson unsigned long flags; 187165cff704SKent Gibson u32 debounce_period_us; 187265cff704SKent Gibson unsigned int num_attrs = 0; 1873925ca369SKent Gibson 187469e4e136SKent Gibson memset(info, 0, sizeof(*info)); 1875aad95584SKent Gibson info->offset = gpio_chip_hwgpio(desc); 1876925ca369SKent Gibson 1877925ca369SKent Gibson /* 1878925ca369SKent Gibson * This function takes a mutex so we must check this before taking 1879925ca369SKent Gibson * the spinlock. 1880925ca369SKent Gibson * 1881925ca369SKent Gibson * FIXME: find a non-racy way to retrieve this information. Maybe a 1882925ca369SKent Gibson * lock common to both frameworks? 1883925ca369SKent Gibson */ 1884925ca369SKent Gibson ok_for_pinctrl = 1885aad95584SKent Gibson pinctrl_gpio_can_use_line(gc->base + info->offset); 1886925ca369SKent Gibson 1887925ca369SKent Gibson spin_lock_irqsave(&gpio_lock, flags); 1888925ca369SKent Gibson 188969e4e136SKent Gibson if (desc->name) 189069e4e136SKent Gibson strscpy(info->name, desc->name, sizeof(info->name)); 1891925ca369SKent Gibson 189269e4e136SKent Gibson if (desc->label) 189369e4e136SKent Gibson strscpy(info->consumer, desc->label, sizeof(info->consumer)); 1894925ca369SKent Gibson 1895925ca369SKent Gibson /* 1896925ca369SKent Gibson * Userspace only need to know that the kernel is using this GPIO so 1897925ca369SKent Gibson * it can't use it. 1898925ca369SKent Gibson */ 1899925ca369SKent Gibson info->flags = 0; 1900925ca369SKent Gibson if (test_bit(FLAG_REQUESTED, &desc->flags) || 1901925ca369SKent Gibson test_bit(FLAG_IS_HOGGED, &desc->flags) || 1902925ca369SKent Gibson test_bit(FLAG_USED_AS_IRQ, &desc->flags) || 1903925ca369SKent Gibson test_bit(FLAG_EXPORT, &desc->flags) || 1904925ca369SKent Gibson test_bit(FLAG_SYSFS, &desc->flags) || 1905925ca369SKent Gibson !ok_for_pinctrl) 1906aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_USED; 1907aad95584SKent Gibson 1908925ca369SKent Gibson if (test_bit(FLAG_IS_OUT, &desc->flags)) 1909aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OUTPUT; 1910aad95584SKent Gibson else 1911aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_INPUT; 1912aad95584SKent Gibson 1913925ca369SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 1914aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW; 1915aad95584SKent Gibson 1916925ca369SKent Gibson if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 1917aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; 1918925ca369SKent Gibson if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 1919aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; 1920aad95584SKent Gibson 1921925ca369SKent Gibson if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) 1922aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED; 1923925ca369SKent Gibson if (test_bit(FLAG_PULL_DOWN, &desc->flags)) 1924aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN; 1925925ca369SKent Gibson if (test_bit(FLAG_PULL_UP, &desc->flags)) 1926aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; 1927925ca369SKent Gibson 192873e03419SKent Gibson if (test_bit(FLAG_EDGE_RISING, &desc->flags)) 192973e03419SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; 193073e03419SKent Gibson if (test_bit(FLAG_EDGE_FALLING, &desc->flags)) 193173e03419SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; 193273e03419SKent Gibson 193365cff704SKent Gibson debounce_period_us = READ_ONCE(desc->debounce_period_us); 193465cff704SKent Gibson if (debounce_period_us) { 193565cff704SKent Gibson info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; 193665cff704SKent Gibson info->attrs[num_attrs].debounce_period_us = debounce_period_us; 193765cff704SKent Gibson num_attrs++; 193865cff704SKent Gibson } 193965cff704SKent Gibson info->num_attrs = num_attrs; 1940925ca369SKent Gibson 1941925ca369SKent Gibson spin_unlock_irqrestore(&gpio_lock, flags); 1942925ca369SKent Gibson } 1943925ca369SKent Gibson 1944925ca369SKent Gibson struct gpio_chardev_data { 1945925ca369SKent Gibson struct gpio_device *gdev; 1946925ca369SKent Gibson wait_queue_head_t wait; 1947aad95584SKent Gibson DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32); 1948925ca369SKent Gibson struct notifier_block lineinfo_changed_nb; 1949925ca369SKent Gibson unsigned long *watched_lines; 1950aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 1951aad95584SKent Gibson atomic_t watch_abi_version; 1952aad95584SKent Gibson #endif 1953925ca369SKent Gibson }; 1954925ca369SKent Gibson 1955aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 1956aad95584SKent Gibson /* 1957aad95584SKent Gibson * returns 0 if the versions match, else the previously selected ABI version 1958aad95584SKent Gibson */ 1959aad95584SKent Gibson static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata, 1960aad95584SKent Gibson unsigned int version) 1961aad95584SKent Gibson { 1962aad95584SKent Gibson int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version); 1963aad95584SKent Gibson 1964aad95584SKent Gibson if (abiv == version) 1965aad95584SKent Gibson return 0; 1966aad95584SKent Gibson 1967aad95584SKent Gibson return abiv; 1968aad95584SKent Gibson } 1969aad95584SKent Gibson #endif 1970aad95584SKent Gibson 1971aad95584SKent Gibson static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip, 1972aad95584SKent Gibson bool watch) 1973aad95584SKent Gibson { 1974aad95584SKent Gibson struct gpio_desc *desc; 1975aad95584SKent Gibson struct gpio_v2_line_info lineinfo; 1976aad95584SKent Gibson 1977aad95584SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 1978aad95584SKent Gibson return -EFAULT; 1979aad95584SKent Gibson 1980aad95584SKent Gibson if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding))) 1981aad95584SKent Gibson return -EINVAL; 1982aad95584SKent Gibson 1983aad95584SKent Gibson desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset); 1984aad95584SKent Gibson if (IS_ERR(desc)) 1985aad95584SKent Gibson return PTR_ERR(desc); 1986aad95584SKent Gibson 1987aad95584SKent Gibson if (watch) { 1988aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 1989aad95584SKent Gibson if (lineinfo_ensure_abi_version(cdev, 2)) 1990aad95584SKent Gibson return -EPERM; 1991aad95584SKent Gibson #endif 1992aad95584SKent Gibson if (test_and_set_bit(lineinfo.offset, cdev->watched_lines)) 1993aad95584SKent Gibson return -EBUSY; 1994aad95584SKent Gibson } 1995aad95584SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo); 1996aad95584SKent Gibson 1997aad95584SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 1998aad95584SKent Gibson if (watch) 1999aad95584SKent Gibson clear_bit(lineinfo.offset, cdev->watched_lines); 2000aad95584SKent Gibson return -EFAULT; 2001aad95584SKent Gibson } 2002aad95584SKent Gibson 2003aad95584SKent Gibson return 0; 2004aad95584SKent Gibson } 2005aad95584SKent Gibson 2006925ca369SKent Gibson /* 2007925ca369SKent Gibson * gpio_ioctl() - ioctl handler for the GPIO chardev 2008925ca369SKent Gibson */ 200949bc5279SKent Gibson static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 2010925ca369SKent Gibson { 2011e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2012e2b781c5SKent Gibson struct gpio_device *gdev = cdev->gdev; 2013925ca369SKent Gibson struct gpio_chip *gc = gdev->chip; 2014925ca369SKent Gibson void __user *ip = (void __user *)arg; 2015925ca369SKent Gibson __u32 offset; 2016925ca369SKent Gibson 2017925ca369SKent Gibson /* We fail any subsequent ioctl():s when the chip is gone */ 2018925ca369SKent Gibson if (!gc) 2019925ca369SKent Gibson return -ENODEV; 2020925ca369SKent Gibson 2021925ca369SKent Gibson /* Fill in the struct and pass to userspace */ 2022925ca369SKent Gibson if (cmd == GPIO_GET_CHIPINFO_IOCTL) { 2023925ca369SKent Gibson struct gpiochip_info chipinfo; 2024925ca369SKent Gibson 2025925ca369SKent Gibson memset(&chipinfo, 0, sizeof(chipinfo)); 2026925ca369SKent Gibson 202769e4e136SKent Gibson strscpy(chipinfo.name, dev_name(&gdev->dev), 2028925ca369SKent Gibson sizeof(chipinfo.name)); 202969e4e136SKent Gibson strscpy(chipinfo.label, gdev->label, 2030925ca369SKent Gibson sizeof(chipinfo.label)); 2031925ca369SKent Gibson chipinfo.lines = gdev->ngpio; 2032925ca369SKent Gibson if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) 2033925ca369SKent Gibson return -EFAULT; 2034925ca369SKent Gibson return 0; 20353c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2036925ca369SKent Gibson } else if (cmd == GPIO_GET_LINEINFO_IOCTL) { 2037aad95584SKent Gibson struct gpio_desc *desc; 2038925ca369SKent Gibson struct gpioline_info lineinfo; 2039aad95584SKent Gibson struct gpio_v2_line_info lineinfo_v2; 2040925ca369SKent Gibson 2041925ca369SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 2042925ca369SKent Gibson return -EFAULT; 2043925ca369SKent Gibson 20441bf7ba40SKent Gibson /* this doubles as a range check on line_offset */ 2045925ca369SKent Gibson desc = gpiochip_get_desc(gc, lineinfo.line_offset); 2046925ca369SKent Gibson if (IS_ERR(desc)) 2047925ca369SKent Gibson return PTR_ERR(desc); 2048925ca369SKent Gibson 2049aad95584SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo_v2); 2050aad95584SKent Gibson gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); 2051925ca369SKent Gibson 2052925ca369SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) 2053925ca369SKent Gibson return -EFAULT; 2054925ca369SKent Gibson return 0; 2055925ca369SKent Gibson } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) { 2056925ca369SKent Gibson return linehandle_create(gdev, ip); 2057925ca369SKent Gibson } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) { 2058925ca369SKent Gibson return lineevent_create(gdev, ip); 2059925ca369SKent Gibson } else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) { 2060aad95584SKent Gibson struct gpio_desc *desc; 2061925ca369SKent Gibson struct gpioline_info lineinfo; 2062aad95584SKent Gibson struct gpio_v2_line_info lineinfo_v2; 2063925ca369SKent Gibson 2064925ca369SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 2065925ca369SKent Gibson return -EFAULT; 2066925ca369SKent Gibson 20671bf7ba40SKent Gibson /* this doubles as a range check on line_offset */ 2068925ca369SKent Gibson desc = gpiochip_get_desc(gc, lineinfo.line_offset); 2069925ca369SKent Gibson if (IS_ERR(desc)) 2070925ca369SKent Gibson return PTR_ERR(desc); 2071925ca369SKent Gibson 2072aad95584SKent Gibson if (lineinfo_ensure_abi_version(cdev, 1)) 2073aad95584SKent Gibson return -EPERM; 2074aad95584SKent Gibson 20751bf7ba40SKent Gibson if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines)) 2076925ca369SKent Gibson return -EBUSY; 2077925ca369SKent Gibson 2078aad95584SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo_v2); 2079aad95584SKent Gibson gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); 2080925ca369SKent Gibson 2081f30ef3e8SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 20821bf7ba40SKent Gibson clear_bit(lineinfo.line_offset, cdev->watched_lines); 2083925ca369SKent Gibson return -EFAULT; 2084f30ef3e8SKent Gibson } 2085925ca369SKent Gibson 2086925ca369SKent Gibson return 0; 20873c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 2088aad95584SKent Gibson } else if (cmd == GPIO_V2_GET_LINEINFO_IOCTL || 2089aad95584SKent Gibson cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL) { 2090aad95584SKent Gibson return lineinfo_get(cdev, ip, 2091aad95584SKent Gibson cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL); 20923c0d9c63SKent Gibson } else if (cmd == GPIO_V2_GET_LINE_IOCTL) { 20933c0d9c63SKent Gibson return linereq_create(gdev, ip); 2094925ca369SKent Gibson } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) { 2095925ca369SKent Gibson if (copy_from_user(&offset, ip, sizeof(offset))) 2096925ca369SKent Gibson return -EFAULT; 2097925ca369SKent Gibson 20981bf7ba40SKent Gibson if (offset >= cdev->gdev->ngpio) 20991bf7ba40SKent Gibson return -EINVAL; 2100925ca369SKent Gibson 21011bf7ba40SKent Gibson if (!test_and_clear_bit(offset, cdev->watched_lines)) 2102925ca369SKent Gibson return -EBUSY; 2103925ca369SKent Gibson 2104925ca369SKent Gibson return 0; 2105925ca369SKent Gibson } 2106925ca369SKent Gibson return -EINVAL; 2107925ca369SKent Gibson } 2108925ca369SKent Gibson 2109925ca369SKent Gibson #ifdef CONFIG_COMPAT 211049bc5279SKent Gibson static long gpio_ioctl_compat(struct file *file, unsigned int cmd, 2111925ca369SKent Gibson unsigned long arg) 2112925ca369SKent Gibson { 211349bc5279SKent Gibson return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 2114925ca369SKent Gibson } 2115925ca369SKent Gibson #endif 2116925ca369SKent Gibson 2117925ca369SKent Gibson static struct gpio_chardev_data * 2118925ca369SKent Gibson to_gpio_chardev_data(struct notifier_block *nb) 2119925ca369SKent Gibson { 2120925ca369SKent Gibson return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb); 2121925ca369SKent Gibson } 2122925ca369SKent Gibson 2123925ca369SKent Gibson static int lineinfo_changed_notify(struct notifier_block *nb, 2124925ca369SKent Gibson unsigned long action, void *data) 2125925ca369SKent Gibson { 2126e2b781c5SKent Gibson struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb); 2127aad95584SKent Gibson struct gpio_v2_line_info_changed chg; 2128925ca369SKent Gibson struct gpio_desc *desc = data; 2129925ca369SKent Gibson int ret; 2130925ca369SKent Gibson 2131e2b781c5SKent Gibson if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) 2132925ca369SKent Gibson return NOTIFY_DONE; 2133925ca369SKent Gibson 2134925ca369SKent Gibson memset(&chg, 0, sizeof(chg)); 2135925ca369SKent Gibson chg.event_type = action; 2136aad95584SKent Gibson chg.timestamp_ns = ktime_get_ns(); 2137925ca369SKent Gibson gpio_desc_to_lineinfo(desc, &chg.info); 2138925ca369SKent Gibson 2139e2b781c5SKent Gibson ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); 2140925ca369SKent Gibson if (ret) 2141e2b781c5SKent Gibson wake_up_poll(&cdev->wait, EPOLLIN); 2142925ca369SKent Gibson else 2143925ca369SKent Gibson pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n"); 2144925ca369SKent Gibson 2145925ca369SKent Gibson return NOTIFY_OK; 2146925ca369SKent Gibson } 2147925ca369SKent Gibson 214849bc5279SKent Gibson static __poll_t lineinfo_watch_poll(struct file *file, 2149925ca369SKent Gibson struct poll_table_struct *pollt) 2150925ca369SKent Gibson { 2151e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2152925ca369SKent Gibson __poll_t events = 0; 2153925ca369SKent Gibson 2154e2b781c5SKent Gibson poll_wait(file, &cdev->wait, pollt); 2155925ca369SKent Gibson 2156e2b781c5SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events, 2157e2b781c5SKent Gibson &cdev->wait.lock)) 2158925ca369SKent Gibson events = EPOLLIN | EPOLLRDNORM; 2159925ca369SKent Gibson 2160925ca369SKent Gibson return events; 2161925ca369SKent Gibson } 2162925ca369SKent Gibson 216349bc5279SKent Gibson static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, 2164925ca369SKent Gibson size_t count, loff_t *off) 2165925ca369SKent Gibson { 2166e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2167aad95584SKent Gibson struct gpio_v2_line_info_changed event; 2168925ca369SKent Gibson ssize_t bytes_read = 0; 2169925ca369SKent Gibson int ret; 2170aad95584SKent Gibson size_t event_size; 2171925ca369SKent Gibson 2172aad95584SKent Gibson #ifndef CONFIG_GPIO_CDEV_V1 2173aad95584SKent Gibson event_size = sizeof(struct gpio_v2_line_info_changed); 2174aad95584SKent Gibson if (count < event_size) 2175925ca369SKent Gibson return -EINVAL; 2176aad95584SKent Gibson #endif 2177925ca369SKent Gibson 2178925ca369SKent Gibson do { 2179e2b781c5SKent Gibson spin_lock(&cdev->wait.lock); 2180e2b781c5SKent Gibson if (kfifo_is_empty(&cdev->events)) { 2181925ca369SKent Gibson if (bytes_read) { 2182e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2183925ca369SKent Gibson return bytes_read; 2184925ca369SKent Gibson } 2185925ca369SKent Gibson 218649bc5279SKent Gibson if (file->f_flags & O_NONBLOCK) { 2187e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2188925ca369SKent Gibson return -EAGAIN; 2189925ca369SKent Gibson } 2190925ca369SKent Gibson 2191e2b781c5SKent Gibson ret = wait_event_interruptible_locked(cdev->wait, 2192e2b781c5SKent Gibson !kfifo_is_empty(&cdev->events)); 2193925ca369SKent Gibson if (ret) { 2194e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2195925ca369SKent Gibson return ret; 2196925ca369SKent Gibson } 2197925ca369SKent Gibson } 2198aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2199aad95584SKent Gibson /* must be after kfifo check so watch_abi_version is set */ 2200aad95584SKent Gibson if (atomic_read(&cdev->watch_abi_version) == 2) 2201aad95584SKent Gibson event_size = sizeof(struct gpio_v2_line_info_changed); 2202aad95584SKent Gibson else 2203aad95584SKent Gibson event_size = sizeof(struct gpioline_info_changed); 2204aad95584SKent Gibson if (count < event_size) { 2205aad95584SKent Gibson spin_unlock(&cdev->wait.lock); 2206aad95584SKent Gibson return -EINVAL; 2207aad95584SKent Gibson } 2208aad95584SKent Gibson #endif 2209e2b781c5SKent Gibson ret = kfifo_out(&cdev->events, &event, 1); 2210e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2211925ca369SKent Gibson if (ret != 1) { 2212925ca369SKent Gibson ret = -EIO; 2213925ca369SKent Gibson break; 2214925ca369SKent Gibson /* We should never get here. See lineevent_read(). */ 2215925ca369SKent Gibson } 2216925ca369SKent Gibson 2217aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2218aad95584SKent Gibson if (event_size == sizeof(struct gpio_v2_line_info_changed)) { 2219aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event, event_size)) 2220925ca369SKent Gibson return -EFAULT; 2221aad95584SKent Gibson } else { 2222aad95584SKent Gibson struct gpioline_info_changed event_v1; 2223aad95584SKent Gibson 2224aad95584SKent Gibson gpio_v2_line_info_changed_to_v1(&event, &event_v1); 2225aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event_v1, 2226aad95584SKent Gibson event_size)) 2227aad95584SKent Gibson return -EFAULT; 2228aad95584SKent Gibson } 2229aad95584SKent Gibson #else 2230aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event, event_size)) 2231aad95584SKent Gibson return -EFAULT; 2232aad95584SKent Gibson #endif 2233aad95584SKent Gibson bytes_read += event_size; 2234925ca369SKent Gibson } while (count >= bytes_read + sizeof(event)); 2235925ca369SKent Gibson 2236925ca369SKent Gibson return bytes_read; 2237925ca369SKent Gibson } 2238925ca369SKent Gibson 2239925ca369SKent Gibson /** 2240925ca369SKent Gibson * gpio_chrdev_open() - open the chardev for ioctl operations 2241925ca369SKent Gibson * @inode: inode for this chardev 224249bc5279SKent Gibson * @file: file struct for storing private data 2243925ca369SKent Gibson * Returns 0 on success 2244925ca369SKent Gibson */ 224549bc5279SKent Gibson static int gpio_chrdev_open(struct inode *inode, struct file *file) 2246925ca369SKent Gibson { 2247925ca369SKent Gibson struct gpio_device *gdev = container_of(inode->i_cdev, 2248925ca369SKent Gibson struct gpio_device, chrdev); 2249e2b781c5SKent Gibson struct gpio_chardev_data *cdev; 2250925ca369SKent Gibson int ret = -ENOMEM; 2251925ca369SKent Gibson 2252925ca369SKent Gibson /* Fail on open if the backing gpiochip is gone */ 2253925ca369SKent Gibson if (!gdev->chip) 2254925ca369SKent Gibson return -ENODEV; 2255925ca369SKent Gibson 2256e2b781c5SKent Gibson cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); 2257e2b781c5SKent Gibson if (!cdev) 2258925ca369SKent Gibson return -ENOMEM; 2259925ca369SKent Gibson 2260e2b781c5SKent Gibson cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL); 2261e2b781c5SKent Gibson if (!cdev->watched_lines) 2262e2b781c5SKent Gibson goto out_free_cdev; 2263925ca369SKent Gibson 2264e2b781c5SKent Gibson init_waitqueue_head(&cdev->wait); 2265e2b781c5SKent Gibson INIT_KFIFO(cdev->events); 2266e2b781c5SKent Gibson cdev->gdev = gdev; 2267925ca369SKent Gibson 2268e2b781c5SKent Gibson cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify; 22696accc376SKent Gibson ret = blocking_notifier_chain_register(&gdev->notifier, 2270e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2271925ca369SKent Gibson if (ret) 2272925ca369SKent Gibson goto out_free_bitmap; 2273925ca369SKent Gibson 2274925ca369SKent Gibson get_device(&gdev->dev); 2275e2b781c5SKent Gibson file->private_data = cdev; 2276925ca369SKent Gibson 227749bc5279SKent Gibson ret = nonseekable_open(inode, file); 2278925ca369SKent Gibson if (ret) 2279925ca369SKent Gibson goto out_unregister_notifier; 2280925ca369SKent Gibson 2281925ca369SKent Gibson return ret; 2282925ca369SKent Gibson 2283925ca369SKent Gibson out_unregister_notifier: 22846accc376SKent Gibson blocking_notifier_chain_unregister(&gdev->notifier, 2285e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2286925ca369SKent Gibson out_free_bitmap: 2287e2b781c5SKent Gibson bitmap_free(cdev->watched_lines); 2288e2b781c5SKent Gibson out_free_cdev: 2289e2b781c5SKent Gibson kfree(cdev); 2290925ca369SKent Gibson return ret; 2291925ca369SKent Gibson } 2292925ca369SKent Gibson 2293925ca369SKent Gibson /** 2294925ca369SKent Gibson * gpio_chrdev_release() - close chardev after ioctl operations 2295925ca369SKent Gibson * @inode: inode for this chardev 229649bc5279SKent Gibson * @file: file struct for storing private data 2297925ca369SKent Gibson * Returns 0 on success 2298925ca369SKent Gibson */ 229949bc5279SKent Gibson static int gpio_chrdev_release(struct inode *inode, struct file *file) 2300925ca369SKent Gibson { 2301e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2302e2b781c5SKent Gibson struct gpio_device *gdev = cdev->gdev; 2303925ca369SKent Gibson 2304e2b781c5SKent Gibson bitmap_free(cdev->watched_lines); 23056accc376SKent Gibson blocking_notifier_chain_unregister(&gdev->notifier, 2306e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2307925ca369SKent Gibson put_device(&gdev->dev); 2308e2b781c5SKent Gibson kfree(cdev); 2309925ca369SKent Gibson 2310925ca369SKent Gibson return 0; 2311925ca369SKent Gibson } 2312925ca369SKent Gibson 2313925ca369SKent Gibson static const struct file_operations gpio_fileops = { 2314925ca369SKent Gibson .release = gpio_chrdev_release, 2315925ca369SKent Gibson .open = gpio_chrdev_open, 2316925ca369SKent Gibson .poll = lineinfo_watch_poll, 2317925ca369SKent Gibson .read = lineinfo_watch_read, 2318925ca369SKent Gibson .owner = THIS_MODULE, 2319925ca369SKent Gibson .llseek = no_llseek, 2320925ca369SKent Gibson .unlocked_ioctl = gpio_ioctl, 2321925ca369SKent Gibson #ifdef CONFIG_COMPAT 2322925ca369SKent Gibson .compat_ioctl = gpio_ioctl_compat, 2323925ca369SKent Gibson #endif 2324925ca369SKent Gibson }; 2325925ca369SKent Gibson 2326925ca369SKent Gibson int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) 2327925ca369SKent Gibson { 2328925ca369SKent Gibson int ret; 2329925ca369SKent Gibson 2330925ca369SKent Gibson cdev_init(&gdev->chrdev, &gpio_fileops); 2331925ca369SKent Gibson gdev->chrdev.owner = THIS_MODULE; 2332925ca369SKent Gibson gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id); 2333925ca369SKent Gibson 2334925ca369SKent Gibson ret = cdev_device_add(&gdev->chrdev, &gdev->dev); 2335925ca369SKent Gibson if (ret) 2336925ca369SKent Gibson return ret; 2337925ca369SKent Gibson 2338925ca369SKent Gibson chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n", 2339925ca369SKent Gibson MAJOR(devt), gdev->id); 2340925ca369SKent Gibson 2341925ca369SKent Gibson return 0; 2342925ca369SKent Gibson } 2343925ca369SKent Gibson 2344925ca369SKent Gibson void gpiolib_cdev_unregister(struct gpio_device *gdev) 2345925ca369SKent Gibson { 2346925ca369SKent Gibson cdev_device_del(&gdev->chrdev, &gdev->dev); 2347925ca369SKent Gibson } 2348