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; 431*3ffb7c45SKent Gibson /* 432*3ffb7c45SKent Gibson * eflags is set by edge_detector_setup(), edge_detector_stop() and 433*3ffb7c45SKent Gibson * edge_detector_update(), which are themselves mutually exclusive, 434*3ffb7c45SKent Gibson * and is accessed by edge_irq_thread() and debounce_work_func(), 435*3ffb7c45SKent Gibson * which can both live with a slightly stale value. 436*3ffb7c45SKent Gibson */ 43773e03419SKent Gibson u64 eflags; 43873e03419SKent Gibson /* 43973e03419SKent Gibson * timestamp_ns and req_seqno are accessed only by 44073e03419SKent Gibson * edge_irq_handler() and edge_irq_thread(), which are themselves 44173e03419SKent Gibson * mutually exclusive, so no additional protection is necessary. 44273e03419SKent Gibson */ 44373e03419SKent Gibson u64 timestamp_ns; 44473e03419SKent Gibson u32 req_seqno; 44565cff704SKent Gibson /* 44665cff704SKent Gibson * line_seqno is accessed by either edge_irq_thread() or 44765cff704SKent Gibson * debounce_work_func(), which are themselves mutually exclusive, 44865cff704SKent Gibson * so no additional protection is necessary. 44965cff704SKent Gibson */ 45073e03419SKent Gibson u32 line_seqno; 45165cff704SKent Gibson /* 45265cff704SKent Gibson * -- debouncer specific fields -- 45365cff704SKent Gibson */ 45465cff704SKent Gibson struct delayed_work work; 45565cff704SKent Gibson /* 45665cff704SKent Gibson * sw_debounce is accessed by linereq_set_config(), which is the 45765cff704SKent Gibson * only setter, and linereq_get_values(), which can live with a 45865cff704SKent Gibson * slightly stale value. 45965cff704SKent Gibson */ 46065cff704SKent Gibson unsigned int sw_debounced; 46165cff704SKent Gibson /* 46265cff704SKent Gibson * level is accessed by debounce_work_func(), which is the only 46365cff704SKent Gibson * setter, and linereq_get_values() which can live with a slightly 46465cff704SKent Gibson * stale value. 46565cff704SKent Gibson */ 46665cff704SKent Gibson unsigned int level; 4673c0d9c63SKent Gibson }; 4683c0d9c63SKent Gibson 4693c0d9c63SKent Gibson /** 4703c0d9c63SKent Gibson * struct linereq - contains the state of a userspace line request 4713c0d9c63SKent Gibson * @gdev: the GPIO device the line request pertains to 4723c0d9c63SKent Gibson * @label: consumer label used to tag GPIO descriptors 4733c0d9c63SKent Gibson * @num_lines: the number of lines in the lines array 47473e03419SKent Gibson * @wait: wait queue that handles blocking reads of events 47573e03419SKent Gibson * @event_buffer_size: the number of elements allocated in @events 47673e03419SKent Gibson * @events: KFIFO for the GPIO events 47773e03419SKent Gibson * @seqno: the sequence number for edge events generated on all lines in 47873e03419SKent Gibson * this line request. Note that this is not used when @num_lines is 1, as 47973e03419SKent Gibson * the line_seqno is then the same and is cheaper to calculate. 480a54756cbSKent Gibson * @config_mutex: mutex for serializing ioctl() calls to ensure consistency 481a54756cbSKent Gibson * of configuration, particularly multi-step accesses to desc flags. 4823c0d9c63SKent Gibson * @lines: the lines held by this line request, with @num_lines elements. 4833c0d9c63SKent Gibson */ 4843c0d9c63SKent Gibson struct linereq { 4853c0d9c63SKent Gibson struct gpio_device *gdev; 4863c0d9c63SKent Gibson const char *label; 4873c0d9c63SKent Gibson u32 num_lines; 48873e03419SKent Gibson wait_queue_head_t wait; 48973e03419SKent Gibson u32 event_buffer_size; 49073e03419SKent Gibson DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); 49173e03419SKent Gibson atomic_t seqno; 492a54756cbSKent Gibson struct mutex config_mutex; 4933c0d9c63SKent Gibson struct line lines[]; 4943c0d9c63SKent Gibson }; 4953c0d9c63SKent Gibson 4963c0d9c63SKent Gibson #define GPIO_V2_LINE_BIAS_FLAGS \ 4973c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ 4983c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \ 4993c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_DISABLED) 5003c0d9c63SKent Gibson 5013c0d9c63SKent Gibson #define GPIO_V2_LINE_DIRECTION_FLAGS \ 5023c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_INPUT | \ 5033c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_OUTPUT) 5043c0d9c63SKent Gibson 5053c0d9c63SKent Gibson #define GPIO_V2_LINE_DRIVE_FLAGS \ 5063c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \ 5073c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_OPEN_SOURCE) 5083c0d9c63SKent Gibson 50973e03419SKent Gibson #define GPIO_V2_LINE_EDGE_FLAGS \ 51073e03419SKent Gibson (GPIO_V2_LINE_FLAG_EDGE_RISING | \ 51173e03419SKent Gibson GPIO_V2_LINE_FLAG_EDGE_FALLING) 51273e03419SKent Gibson 5133c0d9c63SKent Gibson #define GPIO_V2_LINE_VALID_FLAGS \ 5143c0d9c63SKent Gibson (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ 5153c0d9c63SKent Gibson GPIO_V2_LINE_DIRECTION_FLAGS | \ 5163c0d9c63SKent Gibson GPIO_V2_LINE_DRIVE_FLAGS | \ 51773e03419SKent Gibson GPIO_V2_LINE_EDGE_FLAGS | \ 5183c0d9c63SKent Gibson GPIO_V2_LINE_BIAS_FLAGS) 5193c0d9c63SKent Gibson 52073e03419SKent Gibson static void linereq_put_event(struct linereq *lr, 52173e03419SKent Gibson struct gpio_v2_line_event *le) 52273e03419SKent Gibson { 52373e03419SKent Gibson bool overflow = false; 52473e03419SKent Gibson 52573e03419SKent Gibson spin_lock(&lr->wait.lock); 52673e03419SKent Gibson if (kfifo_is_full(&lr->events)) { 52773e03419SKent Gibson overflow = true; 52873e03419SKent Gibson kfifo_skip(&lr->events); 52973e03419SKent Gibson } 53073e03419SKent Gibson kfifo_in(&lr->events, le, 1); 53173e03419SKent Gibson spin_unlock(&lr->wait.lock); 53273e03419SKent Gibson if (!overflow) 53373e03419SKent Gibson wake_up_poll(&lr->wait, EPOLLIN); 53473e03419SKent Gibson else 53573e03419SKent Gibson pr_debug_ratelimited("event FIFO is full - event dropped\n"); 53673e03419SKent Gibson } 53773e03419SKent Gibson 53873e03419SKent Gibson static irqreturn_t edge_irq_thread(int irq, void *p) 53973e03419SKent Gibson { 54073e03419SKent Gibson struct line *line = p; 54173e03419SKent Gibson struct linereq *lr = line->req; 54273e03419SKent Gibson struct gpio_v2_line_event le; 543*3ffb7c45SKent Gibson u64 eflags; 54473e03419SKent Gibson 54573e03419SKent Gibson /* Do not leak kernel stack to userspace */ 54673e03419SKent Gibson memset(&le, 0, sizeof(le)); 54773e03419SKent Gibson 54873e03419SKent Gibson if (line->timestamp_ns) { 54973e03419SKent Gibson le.timestamp_ns = line->timestamp_ns; 55073e03419SKent Gibson } else { 55173e03419SKent Gibson /* 55273e03419SKent Gibson * We may be running from a nested threaded interrupt in 55373e03419SKent Gibson * which case we didn't get the timestamp from 55473e03419SKent Gibson * edge_irq_handler(). 55573e03419SKent Gibson */ 55673e03419SKent Gibson le.timestamp_ns = ktime_get_ns(); 55773e03419SKent Gibson if (lr->num_lines != 1) 55873e03419SKent Gibson line->req_seqno = atomic_inc_return(&lr->seqno); 55973e03419SKent Gibson } 56073e03419SKent Gibson line->timestamp_ns = 0; 56173e03419SKent Gibson 562*3ffb7c45SKent Gibson eflags = READ_ONCE(line->eflags); 563*3ffb7c45SKent Gibson if (eflags == (GPIO_V2_LINE_FLAG_EDGE_RISING | 56473e03419SKent Gibson GPIO_V2_LINE_FLAG_EDGE_FALLING)) { 56573e03419SKent Gibson int level = gpiod_get_value_cansleep(line->desc); 56673e03419SKent Gibson 56773e03419SKent Gibson if (level) 56873e03419SKent Gibson /* Emit low-to-high event */ 56973e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 57073e03419SKent Gibson else 57173e03419SKent Gibson /* Emit high-to-low event */ 57273e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 573*3ffb7c45SKent Gibson } else if (eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) { 57473e03419SKent Gibson /* Emit low-to-high event */ 57573e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 576*3ffb7c45SKent Gibson } else if (eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) { 57773e03419SKent Gibson /* Emit high-to-low event */ 57873e03419SKent Gibson le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 57973e03419SKent Gibson } else { 58073e03419SKent Gibson return IRQ_NONE; 58173e03419SKent Gibson } 58273e03419SKent Gibson line->line_seqno++; 58373e03419SKent Gibson le.line_seqno = line->line_seqno; 58473e03419SKent Gibson le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno; 58573e03419SKent Gibson le.offset = gpio_chip_hwgpio(line->desc); 58673e03419SKent Gibson 58773e03419SKent Gibson linereq_put_event(lr, &le); 58873e03419SKent Gibson 58973e03419SKent Gibson return IRQ_HANDLED; 59073e03419SKent Gibson } 59173e03419SKent Gibson 59273e03419SKent Gibson static irqreturn_t edge_irq_handler(int irq, void *p) 59373e03419SKent Gibson { 59473e03419SKent Gibson struct line *line = p; 59573e03419SKent Gibson struct linereq *lr = line->req; 59673e03419SKent Gibson 59773e03419SKent Gibson /* 59873e03419SKent Gibson * Just store the timestamp in hardirq context so we get it as 59973e03419SKent Gibson * close in time as possible to the actual event. 60073e03419SKent Gibson */ 60173e03419SKent Gibson line->timestamp_ns = ktime_get_ns(); 60273e03419SKent Gibson 60373e03419SKent Gibson if (lr->num_lines != 1) 60473e03419SKent Gibson line->req_seqno = atomic_inc_return(&lr->seqno); 60573e03419SKent Gibson 60673e03419SKent Gibson return IRQ_WAKE_THREAD; 60773e03419SKent Gibson } 60873e03419SKent Gibson 60965cff704SKent Gibson /* 61065cff704SKent Gibson * returns the current debounced logical value. 61165cff704SKent Gibson */ 61265cff704SKent Gibson static bool debounced_value(struct line *line) 61365cff704SKent Gibson { 61465cff704SKent Gibson bool value; 61565cff704SKent Gibson 61665cff704SKent Gibson /* 61765cff704SKent Gibson * minor race - debouncer may be stopped here, so edge_detector_stop() 61865cff704SKent Gibson * must leave the value unchanged so the following will read the level 61965cff704SKent Gibson * from when the debouncer was last running. 62065cff704SKent Gibson */ 62165cff704SKent Gibson value = READ_ONCE(line->level); 62265cff704SKent Gibson 62365cff704SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags)) 62465cff704SKent Gibson value = !value; 62565cff704SKent Gibson 62665cff704SKent Gibson return value; 62765cff704SKent Gibson } 62865cff704SKent Gibson 62965cff704SKent Gibson static irqreturn_t debounce_irq_handler(int irq, void *p) 63065cff704SKent Gibson { 63165cff704SKent Gibson struct line *line = p; 63265cff704SKent Gibson 63365cff704SKent Gibson mod_delayed_work(system_wq, &line->work, 63465cff704SKent Gibson usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us))); 63565cff704SKent Gibson 63665cff704SKent Gibson return IRQ_HANDLED; 63765cff704SKent Gibson } 63865cff704SKent Gibson 63965cff704SKent Gibson static void debounce_work_func(struct work_struct *work) 64065cff704SKent Gibson { 64165cff704SKent Gibson struct gpio_v2_line_event le; 64265cff704SKent Gibson struct line *line = container_of(work, struct line, work.work); 64365cff704SKent Gibson struct linereq *lr; 64465cff704SKent Gibson int level; 645*3ffb7c45SKent Gibson u64 eflags; 64665cff704SKent Gibson 64765cff704SKent Gibson level = gpiod_get_raw_value_cansleep(line->desc); 64865cff704SKent Gibson if (level < 0) { 64965cff704SKent Gibson pr_debug_ratelimited("debouncer failed to read line value\n"); 65065cff704SKent Gibson return; 65165cff704SKent Gibson } 65265cff704SKent Gibson 65365cff704SKent Gibson if (READ_ONCE(line->level) == level) 65465cff704SKent Gibson return; 65565cff704SKent Gibson 65665cff704SKent Gibson WRITE_ONCE(line->level, level); 65765cff704SKent Gibson 65865cff704SKent Gibson /* -- edge detection -- */ 659*3ffb7c45SKent Gibson eflags = READ_ONCE(line->eflags); 660*3ffb7c45SKent Gibson if (!eflags) 66165cff704SKent Gibson return; 66265cff704SKent Gibson 66365cff704SKent Gibson /* switch from physical level to logical - if they differ */ 66465cff704SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags)) 66565cff704SKent Gibson level = !level; 66665cff704SKent Gibson 66765cff704SKent Gibson /* ignore edges that are not being monitored */ 668*3ffb7c45SKent Gibson if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) || 669*3ffb7c45SKent Gibson ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level)) 67065cff704SKent Gibson return; 67165cff704SKent Gibson 67265cff704SKent Gibson /* Do not leak kernel stack to userspace */ 67365cff704SKent Gibson memset(&le, 0, sizeof(le)); 67465cff704SKent Gibson 67565cff704SKent Gibson lr = line->req; 67665cff704SKent Gibson le.timestamp_ns = ktime_get_ns(); 67765cff704SKent Gibson le.offset = gpio_chip_hwgpio(line->desc); 67865cff704SKent Gibson line->line_seqno++; 67965cff704SKent Gibson le.line_seqno = line->line_seqno; 68065cff704SKent Gibson le.seqno = (lr->num_lines == 1) ? 68165cff704SKent Gibson le.line_seqno : atomic_inc_return(&lr->seqno); 68265cff704SKent Gibson 68365cff704SKent Gibson if (level) 68465cff704SKent Gibson /* Emit low-to-high event */ 68565cff704SKent Gibson le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 68665cff704SKent Gibson else 68765cff704SKent Gibson /* Emit high-to-low event */ 68865cff704SKent Gibson le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 68965cff704SKent Gibson 69065cff704SKent Gibson linereq_put_event(lr, &le); 69165cff704SKent Gibson } 69265cff704SKent Gibson 69365cff704SKent Gibson static int debounce_setup(struct line *line, 69465cff704SKent Gibson unsigned int debounce_period_us) 69565cff704SKent Gibson { 69665cff704SKent Gibson unsigned long irqflags; 69765cff704SKent Gibson int ret, level, irq; 69865cff704SKent Gibson 69965cff704SKent Gibson /* try hardware */ 70065cff704SKent Gibson ret = gpiod_set_debounce(line->desc, debounce_period_us); 70165cff704SKent Gibson if (!ret) { 70265cff704SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 70365cff704SKent Gibson return ret; 70465cff704SKent Gibson } 70565cff704SKent Gibson if (ret != -ENOTSUPP) 70665cff704SKent Gibson return ret; 70765cff704SKent Gibson 70865cff704SKent Gibson if (debounce_period_us) { 70965cff704SKent Gibson /* setup software debounce */ 71065cff704SKent Gibson level = gpiod_get_raw_value_cansleep(line->desc); 71165cff704SKent Gibson if (level < 0) 71265cff704SKent Gibson return level; 71365cff704SKent Gibson 71465cff704SKent Gibson irq = gpiod_to_irq(line->desc); 71565cff704SKent Gibson if (irq < 0) 71665cff704SKent Gibson return -ENXIO; 71765cff704SKent Gibson 71865cff704SKent Gibson WRITE_ONCE(line->level, level); 71965cff704SKent Gibson irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING; 72065cff704SKent Gibson ret = request_irq(irq, debounce_irq_handler, irqflags, 72165cff704SKent Gibson line->req->label, line); 72265cff704SKent Gibson if (ret) 72365cff704SKent Gibson return ret; 72465cff704SKent Gibson 72565cff704SKent Gibson WRITE_ONCE(line->sw_debounced, 1); 72665cff704SKent Gibson line->irq = irq; 72765cff704SKent Gibson } 72865cff704SKent Gibson return 0; 72965cff704SKent Gibson } 73065cff704SKent Gibson 73165cff704SKent Gibson static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc, 73265cff704SKent Gibson unsigned int line_idx) 73365cff704SKent Gibson { 73465cff704SKent Gibson unsigned int i; 73565cff704SKent Gibson u64 mask = BIT_ULL(line_idx); 73665cff704SKent Gibson 73765cff704SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 73865cff704SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && 73965cff704SKent Gibson (lc->attrs[i].mask & mask)) 74065cff704SKent Gibson return true; 74165cff704SKent Gibson } 74265cff704SKent Gibson return false; 74365cff704SKent Gibson } 74465cff704SKent Gibson 74565cff704SKent Gibson static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc, 74665cff704SKent Gibson unsigned int line_idx) 74765cff704SKent Gibson { 74865cff704SKent Gibson unsigned int i; 74965cff704SKent Gibson u64 mask = BIT_ULL(line_idx); 75065cff704SKent Gibson 75165cff704SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 75265cff704SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && 75365cff704SKent Gibson (lc->attrs[i].mask & mask)) 75465cff704SKent Gibson return lc->attrs[i].attr.debounce_period_us; 75565cff704SKent Gibson } 75665cff704SKent Gibson return 0; 75765cff704SKent Gibson } 75865cff704SKent Gibson 75973e03419SKent Gibson static void edge_detector_stop(struct line *line) 76073e03419SKent Gibson { 76173e03419SKent Gibson if (line->irq) { 76273e03419SKent Gibson free_irq(line->irq, line); 76373e03419SKent Gibson line->irq = 0; 76473e03419SKent Gibson } 765a54756cbSKent Gibson 76665cff704SKent Gibson cancel_delayed_work_sync(&line->work); 76765cff704SKent Gibson WRITE_ONCE(line->sw_debounced, 0); 768*3ffb7c45SKent Gibson WRITE_ONCE(line->eflags, 0); 76965cff704SKent Gibson /* do not change line->level - see comment in debounced_value() */ 77073e03419SKent Gibson } 77173e03419SKent Gibson 77273e03419SKent Gibson static int edge_detector_setup(struct line *line, 77365cff704SKent Gibson struct gpio_v2_line_config *lc, 77465cff704SKent Gibson unsigned int line_idx, 77573e03419SKent Gibson u64 eflags) 77673e03419SKent Gibson { 77765cff704SKent Gibson u32 debounce_period_us; 77873e03419SKent Gibson unsigned long irqflags = 0; 77973e03419SKent Gibson int irq, ret; 78073e03419SKent Gibson 78173e03419SKent Gibson if (eflags && !kfifo_initialized(&line->req->events)) { 78273e03419SKent Gibson ret = kfifo_alloc(&line->req->events, 78373e03419SKent Gibson line->req->event_buffer_size, GFP_KERNEL); 78473e03419SKent Gibson if (ret) 78573e03419SKent Gibson return ret; 78673e03419SKent Gibson } 787*3ffb7c45SKent Gibson WRITE_ONCE(line->eflags, eflags); 78865cff704SKent Gibson if (gpio_v2_line_config_debounced(lc, line_idx)) { 78965cff704SKent Gibson debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx); 79065cff704SKent Gibson ret = debounce_setup(line, debounce_period_us); 79165cff704SKent Gibson if (ret) 79265cff704SKent Gibson return ret; 79365cff704SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 79465cff704SKent Gibson } 79573e03419SKent Gibson 79665cff704SKent Gibson /* detection disabled or sw debouncer will provide edge detection */ 79765cff704SKent Gibson if (!eflags || READ_ONCE(line->sw_debounced)) 79873e03419SKent Gibson return 0; 79973e03419SKent Gibson 80073e03419SKent Gibson irq = gpiod_to_irq(line->desc); 80173e03419SKent Gibson if (irq < 0) 80273e03419SKent Gibson return -ENXIO; 80373e03419SKent Gibson 80473e03419SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) 80573e03419SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 80673e03419SKent Gibson IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 80773e03419SKent Gibson if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) 80873e03419SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 80973e03419SKent Gibson IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 81073e03419SKent Gibson irqflags |= IRQF_ONESHOT; 81173e03419SKent Gibson 81273e03419SKent Gibson /* Request a thread to read the events */ 81373e03419SKent Gibson ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread, 81473e03419SKent Gibson irqflags, line->req->label, line); 81573e03419SKent Gibson if (ret) 81673e03419SKent Gibson return ret; 81773e03419SKent Gibson 81873e03419SKent Gibson line->irq = irq; 81973e03419SKent Gibson return 0; 82073e03419SKent Gibson } 82173e03419SKent Gibson 82265cff704SKent Gibson static int edge_detector_update(struct line *line, 82365cff704SKent Gibson struct gpio_v2_line_config *lc, 82465cff704SKent Gibson unsigned int line_idx, 82565cff704SKent Gibson u64 eflags, bool polarity_change) 826a54756cbSKent Gibson { 82765cff704SKent Gibson unsigned int debounce_period_us = 82865cff704SKent Gibson gpio_v2_line_config_debounce_period(lc, line_idx); 82965cff704SKent Gibson 830*3ffb7c45SKent Gibson if ((READ_ONCE(line->eflags) == eflags) && !polarity_change && 83165cff704SKent Gibson (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us)) 832a54756cbSKent Gibson return 0; 833a54756cbSKent Gibson 83465cff704SKent Gibson /* sw debounced and still will be...*/ 83565cff704SKent Gibson if (debounce_period_us && READ_ONCE(line->sw_debounced)) { 836*3ffb7c45SKent Gibson WRITE_ONCE(line->eflags, eflags); 83765cff704SKent Gibson WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 83865cff704SKent Gibson return 0; 83965cff704SKent Gibson } 84065cff704SKent Gibson 84165cff704SKent Gibson /* reconfiguring edge detection or sw debounce being disabled */ 84265cff704SKent Gibson if ((line->irq && !READ_ONCE(line->sw_debounced)) || 84365cff704SKent Gibson (!debounce_period_us && READ_ONCE(line->sw_debounced))) 844a54756cbSKent Gibson edge_detector_stop(line); 845a54756cbSKent Gibson 84665cff704SKent Gibson return edge_detector_setup(line, lc, line_idx, eflags); 847a54756cbSKent Gibson } 848a54756cbSKent Gibson 8493c0d9c63SKent Gibson static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, 8503c0d9c63SKent Gibson unsigned int line_idx) 8513c0d9c63SKent Gibson { 8523c0d9c63SKent Gibson unsigned int i; 8533c0d9c63SKent Gibson u64 mask = BIT_ULL(line_idx); 8543c0d9c63SKent Gibson 8553c0d9c63SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 8563c0d9c63SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) && 8573c0d9c63SKent Gibson (lc->attrs[i].mask & mask)) 8583c0d9c63SKent Gibson return lc->attrs[i].attr.flags; 8593c0d9c63SKent Gibson } 8603c0d9c63SKent Gibson return lc->flags; 8613c0d9c63SKent Gibson } 8623c0d9c63SKent Gibson 8633c0d9c63SKent Gibson static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc, 8643c0d9c63SKent Gibson unsigned int line_idx) 8653c0d9c63SKent Gibson { 8663c0d9c63SKent Gibson unsigned int i; 8673c0d9c63SKent Gibson u64 mask = BIT_ULL(line_idx); 8683c0d9c63SKent Gibson 8693c0d9c63SKent Gibson for (i = 0; i < lc->num_attrs; i++) { 8703c0d9c63SKent Gibson if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) && 8713c0d9c63SKent Gibson (lc->attrs[i].mask & mask)) 8723c0d9c63SKent Gibson return !!(lc->attrs[i].attr.values & mask); 8733c0d9c63SKent Gibson } 8743c0d9c63SKent Gibson return 0; 8753c0d9c63SKent Gibson } 8763c0d9c63SKent Gibson 8773c0d9c63SKent Gibson static int gpio_v2_line_flags_validate(u64 flags) 8783c0d9c63SKent Gibson { 8793c0d9c63SKent Gibson /* Return an error if an unknown flag is set */ 8803c0d9c63SKent Gibson if (flags & ~GPIO_V2_LINE_VALID_FLAGS) 8813c0d9c63SKent Gibson return -EINVAL; 8823c0d9c63SKent Gibson 8833c0d9c63SKent Gibson /* 8843c0d9c63SKent Gibson * Do not allow both INPUT and OUTPUT flags to be set as they are 8853c0d9c63SKent Gibson * contradictory. 8863c0d9c63SKent Gibson */ 8873c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_FLAG_INPUT) && 8883c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_OUTPUT)) 8893c0d9c63SKent Gibson return -EINVAL; 8903c0d9c63SKent Gibson 89173e03419SKent Gibson /* Edge detection requires explicit input. */ 89273e03419SKent Gibson if ((flags & GPIO_V2_LINE_EDGE_FLAGS) && 89373e03419SKent Gibson !(flags & GPIO_V2_LINE_FLAG_INPUT)) 89473e03419SKent Gibson return -EINVAL; 89573e03419SKent Gibson 8963c0d9c63SKent Gibson /* 8973c0d9c63SKent Gibson * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single 8983c0d9c63SKent Gibson * request. If the hardware actually supports enabling both at the 8993c0d9c63SKent Gibson * same time the electrical result would be disastrous. 9003c0d9c63SKent Gibson */ 9013c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) && 9023c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE)) 9033c0d9c63SKent Gibson return -EINVAL; 9043c0d9c63SKent Gibson 9053c0d9c63SKent Gibson /* Drive requires explicit output direction. */ 9063c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) && 9073c0d9c63SKent Gibson !(flags & GPIO_V2_LINE_FLAG_OUTPUT)) 9083c0d9c63SKent Gibson return -EINVAL; 9093c0d9c63SKent Gibson 9103c0d9c63SKent Gibson /* Bias requires explicit direction. */ 9113c0d9c63SKent Gibson if ((flags & GPIO_V2_LINE_BIAS_FLAGS) && 9123c0d9c63SKent Gibson !(flags & GPIO_V2_LINE_DIRECTION_FLAGS)) 9133c0d9c63SKent Gibson return -EINVAL; 9143c0d9c63SKent Gibson 9153c0d9c63SKent Gibson /* Only one bias flag can be set. */ 9163c0d9c63SKent Gibson if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) && 9173c0d9c63SKent Gibson (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | 9183c0d9c63SKent Gibson GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) || 9193c0d9c63SKent Gibson ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) && 9203c0d9c63SKent Gibson (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) 9213c0d9c63SKent Gibson return -EINVAL; 9223c0d9c63SKent Gibson 9233c0d9c63SKent Gibson return 0; 9243c0d9c63SKent Gibson } 9253c0d9c63SKent Gibson 9263c0d9c63SKent Gibson static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, 9273c0d9c63SKent Gibson unsigned int num_lines) 9283c0d9c63SKent Gibson { 9293c0d9c63SKent Gibson unsigned int i; 9303c0d9c63SKent Gibson u64 flags; 9313c0d9c63SKent Gibson int ret; 9323c0d9c63SKent Gibson 9333c0d9c63SKent Gibson if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX) 9343c0d9c63SKent Gibson return -EINVAL; 9353c0d9c63SKent Gibson 9363c0d9c63SKent Gibson if (memchr_inv(lc->padding, 0, sizeof(lc->padding))) 9373c0d9c63SKent Gibson return -EINVAL; 9383c0d9c63SKent Gibson 9393c0d9c63SKent Gibson for (i = 0; i < num_lines; i++) { 9403c0d9c63SKent Gibson flags = gpio_v2_line_config_flags(lc, i); 9413c0d9c63SKent Gibson ret = gpio_v2_line_flags_validate(flags); 9423c0d9c63SKent Gibson if (ret) 9433c0d9c63SKent Gibson return ret; 94465cff704SKent Gibson 94565cff704SKent Gibson /* debounce requires explicit input */ 94665cff704SKent Gibson if (gpio_v2_line_config_debounced(lc, i) && 94765cff704SKent Gibson !(flags & GPIO_V2_LINE_FLAG_INPUT)) 94865cff704SKent Gibson return -EINVAL; 9493c0d9c63SKent Gibson } 9503c0d9c63SKent Gibson return 0; 9513c0d9c63SKent Gibson } 9523c0d9c63SKent Gibson 9533c0d9c63SKent Gibson static void gpio_v2_line_config_flags_to_desc_flags(u64 flags, 9543c0d9c63SKent Gibson unsigned long *flagsp) 9553c0d9c63SKent Gibson { 9563c0d9c63SKent Gibson assign_bit(FLAG_ACTIVE_LOW, flagsp, 9573c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW); 9583c0d9c63SKent Gibson 9593c0d9c63SKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) 9603c0d9c63SKent Gibson set_bit(FLAG_IS_OUT, flagsp); 9613c0d9c63SKent Gibson else if (flags & GPIO_V2_LINE_FLAG_INPUT) 9623c0d9c63SKent Gibson clear_bit(FLAG_IS_OUT, flagsp); 9633c0d9c63SKent Gibson 96473e03419SKent Gibson assign_bit(FLAG_EDGE_RISING, flagsp, 96573e03419SKent Gibson flags & GPIO_V2_LINE_FLAG_EDGE_RISING); 96673e03419SKent Gibson assign_bit(FLAG_EDGE_FALLING, flagsp, 96773e03419SKent Gibson flags & GPIO_V2_LINE_FLAG_EDGE_FALLING); 96873e03419SKent Gibson 9693c0d9c63SKent Gibson assign_bit(FLAG_OPEN_DRAIN, flagsp, 9703c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN); 9713c0d9c63SKent Gibson assign_bit(FLAG_OPEN_SOURCE, flagsp, 9723c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE); 9733c0d9c63SKent Gibson 9743c0d9c63SKent Gibson assign_bit(FLAG_PULL_UP, flagsp, 9753c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP); 9763c0d9c63SKent Gibson assign_bit(FLAG_PULL_DOWN, flagsp, 9773c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN); 9783c0d9c63SKent Gibson assign_bit(FLAG_BIAS_DISABLE, flagsp, 9793c0d9c63SKent Gibson flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED); 9803c0d9c63SKent Gibson } 9813c0d9c63SKent Gibson 9823c0d9c63SKent Gibson static long linereq_get_values(struct linereq *lr, void __user *ip) 9833c0d9c63SKent Gibson { 9843c0d9c63SKent Gibson struct gpio_v2_line_values lv; 9853c0d9c63SKent Gibson DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); 9863c0d9c63SKent Gibson struct gpio_desc **descs; 9873c0d9c63SKent Gibson unsigned int i, didx, num_get; 98865cff704SKent Gibson bool val; 9893c0d9c63SKent Gibson int ret; 9903c0d9c63SKent Gibson 9913c0d9c63SKent Gibson /* NOTE: It's ok to read values of output lines. */ 9923c0d9c63SKent Gibson if (copy_from_user(&lv, ip, sizeof(lv))) 9933c0d9c63SKent Gibson return -EFAULT; 9943c0d9c63SKent Gibson 9953c0d9c63SKent Gibson for (num_get = 0, i = 0; i < lr->num_lines; i++) { 9963c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 9973c0d9c63SKent Gibson num_get++; 9983c0d9c63SKent Gibson descs = &lr->lines[i].desc; 9993c0d9c63SKent Gibson } 10003c0d9c63SKent Gibson } 10013c0d9c63SKent Gibson 10023c0d9c63SKent Gibson if (num_get == 0) 10033c0d9c63SKent Gibson return -EINVAL; 10043c0d9c63SKent Gibson 10053c0d9c63SKent Gibson if (num_get != 1) { 10063c0d9c63SKent Gibson descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL); 10073c0d9c63SKent Gibson if (!descs) 10083c0d9c63SKent Gibson return -ENOMEM; 10093c0d9c63SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 10103c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 10113c0d9c63SKent Gibson descs[didx] = lr->lines[i].desc; 10123c0d9c63SKent Gibson didx++; 10133c0d9c63SKent Gibson } 10143c0d9c63SKent Gibson } 10153c0d9c63SKent Gibson } 10163c0d9c63SKent Gibson ret = gpiod_get_array_value_complex(false, true, num_get, 10173c0d9c63SKent Gibson descs, NULL, vals); 10183c0d9c63SKent Gibson 10193c0d9c63SKent Gibson if (num_get != 1) 10203c0d9c63SKent Gibson kfree(descs); 10213c0d9c63SKent Gibson if (ret) 10223c0d9c63SKent Gibson return ret; 10233c0d9c63SKent Gibson 10243c0d9c63SKent Gibson lv.bits = 0; 10253c0d9c63SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 10263c0d9c63SKent Gibson if (lv.mask & BIT_ULL(i)) { 102765cff704SKent Gibson if (lr->lines[i].sw_debounced) 102865cff704SKent Gibson val = debounced_value(&lr->lines[i]); 102965cff704SKent Gibson else 103065cff704SKent Gibson val = test_bit(didx, vals); 103165cff704SKent Gibson if (val) 10323c0d9c63SKent Gibson lv.bits |= BIT_ULL(i); 10333c0d9c63SKent Gibson didx++; 10343c0d9c63SKent Gibson } 10353c0d9c63SKent Gibson } 10363c0d9c63SKent Gibson 10373c0d9c63SKent Gibson if (copy_to_user(ip, &lv, sizeof(lv))) 10383c0d9c63SKent Gibson return -EFAULT; 10393c0d9c63SKent Gibson 10403c0d9c63SKent Gibson return 0; 10413c0d9c63SKent Gibson } 10423c0d9c63SKent Gibson 10437b8e00d9SKent Gibson static long linereq_set_values_unlocked(struct linereq *lr, 10447b8e00d9SKent Gibson struct gpio_v2_line_values *lv) 10457b8e00d9SKent Gibson { 10467b8e00d9SKent Gibson DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); 10477b8e00d9SKent Gibson struct gpio_desc **descs; 10487b8e00d9SKent Gibson unsigned int i, didx, num_set; 10497b8e00d9SKent Gibson int ret; 10507b8e00d9SKent Gibson 10517b8e00d9SKent Gibson bitmap_zero(vals, GPIO_V2_LINES_MAX); 10527b8e00d9SKent Gibson for (num_set = 0, i = 0; i < lr->num_lines; i++) { 10537b8e00d9SKent Gibson if (lv->mask & BIT_ULL(i)) { 10547b8e00d9SKent Gibson if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags)) 10557b8e00d9SKent Gibson return -EPERM; 10567b8e00d9SKent Gibson if (lv->bits & BIT_ULL(i)) 10577b8e00d9SKent Gibson __set_bit(num_set, vals); 10587b8e00d9SKent Gibson num_set++; 10597b8e00d9SKent Gibson descs = &lr->lines[i].desc; 10607b8e00d9SKent Gibson } 10617b8e00d9SKent Gibson } 10627b8e00d9SKent Gibson if (num_set == 0) 10637b8e00d9SKent Gibson return -EINVAL; 10647b8e00d9SKent Gibson 10657b8e00d9SKent Gibson if (num_set != 1) { 10667b8e00d9SKent Gibson /* build compacted desc array and values */ 10677b8e00d9SKent Gibson descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL); 10687b8e00d9SKent Gibson if (!descs) 10697b8e00d9SKent Gibson return -ENOMEM; 10707b8e00d9SKent Gibson for (didx = 0, i = 0; i < lr->num_lines; i++) { 10717b8e00d9SKent Gibson if (lv->mask & BIT_ULL(i)) { 10727b8e00d9SKent Gibson descs[didx] = lr->lines[i].desc; 10737b8e00d9SKent Gibson didx++; 10747b8e00d9SKent Gibson } 10757b8e00d9SKent Gibson } 10767b8e00d9SKent Gibson } 10777b8e00d9SKent Gibson ret = gpiod_set_array_value_complex(false, true, num_set, 10787b8e00d9SKent Gibson descs, NULL, vals); 10797b8e00d9SKent Gibson 10807b8e00d9SKent Gibson if (num_set != 1) 10817b8e00d9SKent Gibson kfree(descs); 10827b8e00d9SKent Gibson return ret; 10837b8e00d9SKent Gibson } 10847b8e00d9SKent Gibson 10857b8e00d9SKent Gibson static long linereq_set_values(struct linereq *lr, void __user *ip) 10867b8e00d9SKent Gibson { 10877b8e00d9SKent Gibson struct gpio_v2_line_values lv; 10887b8e00d9SKent Gibson int ret; 10897b8e00d9SKent Gibson 10907b8e00d9SKent Gibson if (copy_from_user(&lv, ip, sizeof(lv))) 10917b8e00d9SKent Gibson return -EFAULT; 10927b8e00d9SKent Gibson 10937b8e00d9SKent Gibson mutex_lock(&lr->config_mutex); 10947b8e00d9SKent Gibson 10957b8e00d9SKent Gibson ret = linereq_set_values_unlocked(lr, &lv); 10967b8e00d9SKent Gibson 10977b8e00d9SKent Gibson mutex_unlock(&lr->config_mutex); 10987b8e00d9SKent Gibson 10997b8e00d9SKent Gibson return ret; 11007b8e00d9SKent Gibson } 11017b8e00d9SKent Gibson 1102a54756cbSKent Gibson static long linereq_set_config_unlocked(struct linereq *lr, 1103a54756cbSKent Gibson struct gpio_v2_line_config *lc) 1104a54756cbSKent Gibson { 1105a54756cbSKent Gibson struct gpio_desc *desc; 1106a54756cbSKent Gibson unsigned int i; 1107a54756cbSKent Gibson u64 flags; 1108a54756cbSKent Gibson bool polarity_change; 1109a54756cbSKent Gibson int ret; 1110a54756cbSKent Gibson 1111a54756cbSKent Gibson for (i = 0; i < lr->num_lines; i++) { 1112a54756cbSKent Gibson desc = lr->lines[i].desc; 1113a54756cbSKent Gibson flags = gpio_v2_line_config_flags(lc, i); 1114a54756cbSKent Gibson polarity_change = 1115a54756cbSKent Gibson (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) != 1116a54756cbSKent Gibson ((flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) != 0)); 1117a54756cbSKent Gibson 1118a54756cbSKent Gibson gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 1119a54756cbSKent Gibson /* 1120a54756cbSKent Gibson * Lines have to be requested explicitly for input 1121a54756cbSKent Gibson * or output, else the line will be treated "as is". 1122a54756cbSKent Gibson */ 1123a54756cbSKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 1124a54756cbSKent Gibson int val = gpio_v2_line_config_output_value(lc, i); 1125a54756cbSKent Gibson 1126a54756cbSKent Gibson edge_detector_stop(&lr->lines[i]); 1127a54756cbSKent Gibson ret = gpiod_direction_output(desc, val); 1128a54756cbSKent Gibson if (ret) 1129a54756cbSKent Gibson return ret; 1130a54756cbSKent Gibson } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { 1131a54756cbSKent Gibson ret = gpiod_direction_input(desc); 1132a54756cbSKent Gibson if (ret) 1133a54756cbSKent Gibson return ret; 1134a54756cbSKent Gibson 113565cff704SKent Gibson ret = edge_detector_update(&lr->lines[i], lc, i, 1136a54756cbSKent Gibson flags & GPIO_V2_LINE_EDGE_FLAGS, 1137a54756cbSKent Gibson polarity_change); 1138a54756cbSKent Gibson if (ret) 1139a54756cbSKent Gibson return ret; 1140a54756cbSKent Gibson } 1141a54756cbSKent Gibson 1142a54756cbSKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 1143a54756cbSKent Gibson GPIO_V2_LINE_CHANGED_CONFIG, 1144a54756cbSKent Gibson desc); 1145a54756cbSKent Gibson } 1146a54756cbSKent Gibson return 0; 1147a54756cbSKent Gibson } 1148a54756cbSKent Gibson 1149a54756cbSKent Gibson static long linereq_set_config(struct linereq *lr, void __user *ip) 1150a54756cbSKent Gibson { 1151a54756cbSKent Gibson struct gpio_v2_line_config lc; 1152a54756cbSKent Gibson int ret; 1153a54756cbSKent Gibson 1154a54756cbSKent Gibson if (copy_from_user(&lc, ip, sizeof(lc))) 1155a54756cbSKent Gibson return -EFAULT; 1156a54756cbSKent Gibson 1157a54756cbSKent Gibson ret = gpio_v2_line_config_validate(&lc, lr->num_lines); 1158a54756cbSKent Gibson if (ret) 1159a54756cbSKent Gibson return ret; 1160a54756cbSKent Gibson 1161a54756cbSKent Gibson mutex_lock(&lr->config_mutex); 1162a54756cbSKent Gibson 1163a54756cbSKent Gibson ret = linereq_set_config_unlocked(lr, &lc); 1164a54756cbSKent Gibson 1165a54756cbSKent Gibson mutex_unlock(&lr->config_mutex); 1166a54756cbSKent Gibson 1167a54756cbSKent Gibson return ret; 1168a54756cbSKent Gibson } 1169a54756cbSKent Gibson 11703c0d9c63SKent Gibson static long linereq_ioctl(struct file *file, unsigned int cmd, 11713c0d9c63SKent Gibson unsigned long arg) 11723c0d9c63SKent Gibson { 11733c0d9c63SKent Gibson struct linereq *lr = file->private_data; 11743c0d9c63SKent Gibson void __user *ip = (void __user *)arg; 11753c0d9c63SKent Gibson 11763c0d9c63SKent Gibson if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL) 11773c0d9c63SKent Gibson return linereq_get_values(lr, ip); 11787b8e00d9SKent Gibson else if (cmd == GPIO_V2_LINE_SET_VALUES_IOCTL) 11797b8e00d9SKent Gibson return linereq_set_values(lr, ip); 1180a54756cbSKent Gibson else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL) 1181a54756cbSKent Gibson return linereq_set_config(lr, ip); 11823c0d9c63SKent Gibson 11833c0d9c63SKent Gibson return -EINVAL; 11843c0d9c63SKent Gibson } 11853c0d9c63SKent Gibson 11863c0d9c63SKent Gibson #ifdef CONFIG_COMPAT 11873c0d9c63SKent Gibson static long linereq_ioctl_compat(struct file *file, unsigned int cmd, 11883c0d9c63SKent Gibson unsigned long arg) 11893c0d9c63SKent Gibson { 11903c0d9c63SKent Gibson return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 11913c0d9c63SKent Gibson } 11923c0d9c63SKent Gibson #endif 11933c0d9c63SKent Gibson 119473e03419SKent Gibson static __poll_t linereq_poll(struct file *file, 119573e03419SKent Gibson struct poll_table_struct *wait) 119673e03419SKent Gibson { 119773e03419SKent Gibson struct linereq *lr = file->private_data; 119873e03419SKent Gibson __poll_t events = 0; 119973e03419SKent Gibson 120073e03419SKent Gibson poll_wait(file, &lr->wait, wait); 120173e03419SKent Gibson 120273e03419SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events, 120373e03419SKent Gibson &lr->wait.lock)) 120473e03419SKent Gibson events = EPOLLIN | EPOLLRDNORM; 120573e03419SKent Gibson 120673e03419SKent Gibson return events; 120773e03419SKent Gibson } 120873e03419SKent Gibson 120973e03419SKent Gibson static ssize_t linereq_read(struct file *file, 121073e03419SKent Gibson char __user *buf, 121173e03419SKent Gibson size_t count, 121273e03419SKent Gibson loff_t *f_ps) 121373e03419SKent Gibson { 121473e03419SKent Gibson struct linereq *lr = file->private_data; 121573e03419SKent Gibson struct gpio_v2_line_event le; 121673e03419SKent Gibson ssize_t bytes_read = 0; 121773e03419SKent Gibson int ret; 121873e03419SKent Gibson 121973e03419SKent Gibson if (count < sizeof(le)) 122073e03419SKent Gibson return -EINVAL; 122173e03419SKent Gibson 122273e03419SKent Gibson do { 122373e03419SKent Gibson spin_lock(&lr->wait.lock); 122473e03419SKent Gibson if (kfifo_is_empty(&lr->events)) { 122573e03419SKent Gibson if (bytes_read) { 122673e03419SKent Gibson spin_unlock(&lr->wait.lock); 122773e03419SKent Gibson return bytes_read; 122873e03419SKent Gibson } 122973e03419SKent Gibson 123073e03419SKent Gibson if (file->f_flags & O_NONBLOCK) { 123173e03419SKent Gibson spin_unlock(&lr->wait.lock); 123273e03419SKent Gibson return -EAGAIN; 123373e03419SKent Gibson } 123473e03419SKent Gibson 123573e03419SKent Gibson ret = wait_event_interruptible_locked(lr->wait, 123673e03419SKent Gibson !kfifo_is_empty(&lr->events)); 123773e03419SKent Gibson if (ret) { 123873e03419SKent Gibson spin_unlock(&lr->wait.lock); 123973e03419SKent Gibson return ret; 124073e03419SKent Gibson } 124173e03419SKent Gibson } 124273e03419SKent Gibson 124373e03419SKent Gibson ret = kfifo_out(&lr->events, &le, 1); 124473e03419SKent Gibson spin_unlock(&lr->wait.lock); 124573e03419SKent Gibson if (ret != 1) { 124673e03419SKent Gibson /* 124773e03419SKent Gibson * This should never happen - we were holding the 124873e03419SKent Gibson * lock from the moment we learned the fifo is no 124973e03419SKent Gibson * longer empty until now. 125073e03419SKent Gibson */ 125173e03419SKent Gibson ret = -EIO; 125273e03419SKent Gibson break; 125373e03419SKent Gibson } 125473e03419SKent Gibson 125573e03419SKent Gibson if (copy_to_user(buf + bytes_read, &le, sizeof(le))) 125673e03419SKent Gibson return -EFAULT; 125773e03419SKent Gibson bytes_read += sizeof(le); 125873e03419SKent Gibson } while (count >= bytes_read + sizeof(le)); 125973e03419SKent Gibson 126073e03419SKent Gibson return bytes_read; 126173e03419SKent Gibson } 126273e03419SKent Gibson 12633c0d9c63SKent Gibson static void linereq_free(struct linereq *lr) 12643c0d9c63SKent Gibson { 12653c0d9c63SKent Gibson unsigned int i; 12663c0d9c63SKent Gibson 12673c0d9c63SKent Gibson for (i = 0; i < lr->num_lines; i++) { 126873e03419SKent Gibson edge_detector_stop(&lr->lines[i]); 12693c0d9c63SKent Gibson if (lr->lines[i].desc) 12703c0d9c63SKent Gibson gpiod_free(lr->lines[i].desc); 12713c0d9c63SKent Gibson } 127273e03419SKent Gibson kfifo_free(&lr->events); 12733c0d9c63SKent Gibson kfree(lr->label); 12743c0d9c63SKent Gibson put_device(&lr->gdev->dev); 12753c0d9c63SKent Gibson kfree(lr); 12763c0d9c63SKent Gibson } 12773c0d9c63SKent Gibson 12783c0d9c63SKent Gibson static int linereq_release(struct inode *inode, struct file *file) 12793c0d9c63SKent Gibson { 12803c0d9c63SKent Gibson struct linereq *lr = file->private_data; 12813c0d9c63SKent Gibson 12823c0d9c63SKent Gibson linereq_free(lr); 12833c0d9c63SKent Gibson return 0; 12843c0d9c63SKent Gibson } 12853c0d9c63SKent Gibson 12863c0d9c63SKent Gibson static const struct file_operations line_fileops = { 12873c0d9c63SKent Gibson .release = linereq_release, 128873e03419SKent Gibson .read = linereq_read, 128973e03419SKent Gibson .poll = linereq_poll, 12903c0d9c63SKent Gibson .owner = THIS_MODULE, 12913c0d9c63SKent Gibson .llseek = noop_llseek, 12923c0d9c63SKent Gibson .unlocked_ioctl = linereq_ioctl, 12933c0d9c63SKent Gibson #ifdef CONFIG_COMPAT 12943c0d9c63SKent Gibson .compat_ioctl = linereq_ioctl_compat, 12953c0d9c63SKent Gibson #endif 12963c0d9c63SKent Gibson }; 12973c0d9c63SKent Gibson 12983c0d9c63SKent Gibson static int linereq_create(struct gpio_device *gdev, void __user *ip) 12993c0d9c63SKent Gibson { 13003c0d9c63SKent Gibson struct gpio_v2_line_request ulr; 13013c0d9c63SKent Gibson struct gpio_v2_line_config *lc; 13023c0d9c63SKent Gibson struct linereq *lr; 13033c0d9c63SKent Gibson struct file *file; 13043c0d9c63SKent Gibson u64 flags; 13053c0d9c63SKent Gibson unsigned int i; 13063c0d9c63SKent Gibson int fd, ret; 13073c0d9c63SKent Gibson 13083c0d9c63SKent Gibson if (copy_from_user(&ulr, ip, sizeof(ulr))) 13093c0d9c63SKent Gibson return -EFAULT; 13103c0d9c63SKent Gibson 13113c0d9c63SKent Gibson if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX)) 13123c0d9c63SKent Gibson return -EINVAL; 13133c0d9c63SKent Gibson 13143c0d9c63SKent Gibson if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding))) 13153c0d9c63SKent Gibson return -EINVAL; 13163c0d9c63SKent Gibson 13173c0d9c63SKent Gibson lc = &ulr.config; 13183c0d9c63SKent Gibson ret = gpio_v2_line_config_validate(lc, ulr.num_lines); 13193c0d9c63SKent Gibson if (ret) 13203c0d9c63SKent Gibson return ret; 13213c0d9c63SKent Gibson 13223c0d9c63SKent Gibson lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); 13233c0d9c63SKent Gibson if (!lr) 13243c0d9c63SKent Gibson return -ENOMEM; 13253c0d9c63SKent Gibson 13263c0d9c63SKent Gibson lr->gdev = gdev; 13273c0d9c63SKent Gibson get_device(&gdev->dev); 13283c0d9c63SKent Gibson 132965cff704SKent Gibson for (i = 0; i < ulr.num_lines; i++) { 133073e03419SKent Gibson lr->lines[i].req = lr; 133165cff704SKent Gibson WRITE_ONCE(lr->lines[i].sw_debounced, 0); 133265cff704SKent Gibson INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func); 133365cff704SKent Gibson } 133473e03419SKent Gibson 1335f188ac12SKent Gibson if (ulr.consumer[0] != '\0') { 13363c0d9c63SKent Gibson /* label is only initialized if consumer is set */ 1337f188ac12SKent Gibson lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1, 1338f188ac12SKent Gibson GFP_KERNEL); 13393c0d9c63SKent Gibson if (!lr->label) { 13403c0d9c63SKent Gibson ret = -ENOMEM; 13413c0d9c63SKent Gibson goto out_free_linereq; 13423c0d9c63SKent Gibson } 13433c0d9c63SKent Gibson } 13443c0d9c63SKent Gibson 1345a54756cbSKent Gibson mutex_init(&lr->config_mutex); 134673e03419SKent Gibson init_waitqueue_head(&lr->wait); 134773e03419SKent Gibson lr->event_buffer_size = ulr.event_buffer_size; 134873e03419SKent Gibson if (lr->event_buffer_size == 0) 134973e03419SKent Gibson lr->event_buffer_size = ulr.num_lines * 16; 135073e03419SKent Gibson else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16) 135173e03419SKent Gibson lr->event_buffer_size = GPIO_V2_LINES_MAX * 16; 135273e03419SKent Gibson 135373e03419SKent Gibson atomic_set(&lr->seqno, 0); 13543c0d9c63SKent Gibson lr->num_lines = ulr.num_lines; 13553c0d9c63SKent Gibson 13563c0d9c63SKent Gibson /* Request each GPIO */ 13573c0d9c63SKent Gibson for (i = 0; i < ulr.num_lines; i++) { 13583c0d9c63SKent Gibson u32 offset = ulr.offsets[i]; 13593c0d9c63SKent Gibson struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); 13603c0d9c63SKent Gibson 13613c0d9c63SKent Gibson if (IS_ERR(desc)) { 13623c0d9c63SKent Gibson ret = PTR_ERR(desc); 13633c0d9c63SKent Gibson goto out_free_linereq; 13643c0d9c63SKent Gibson } 13653c0d9c63SKent Gibson 13663c0d9c63SKent Gibson ret = gpiod_request(desc, lr->label); 13673c0d9c63SKent Gibson if (ret) 13683c0d9c63SKent Gibson goto out_free_linereq; 13693c0d9c63SKent Gibson 13703c0d9c63SKent Gibson lr->lines[i].desc = desc; 13713c0d9c63SKent Gibson flags = gpio_v2_line_config_flags(lc, i); 13723c0d9c63SKent Gibson gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 13733c0d9c63SKent Gibson 13743c0d9c63SKent Gibson ret = gpiod_set_transitory(desc, false); 13753c0d9c63SKent Gibson if (ret < 0) 13763c0d9c63SKent Gibson goto out_free_linereq; 13773c0d9c63SKent Gibson 13783c0d9c63SKent Gibson /* 13793c0d9c63SKent Gibson * Lines have to be requested explicitly for input 13803c0d9c63SKent Gibson * or output, else the line will be treated "as is". 13813c0d9c63SKent Gibson */ 13823c0d9c63SKent Gibson if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 13833c0d9c63SKent Gibson int val = gpio_v2_line_config_output_value(lc, i); 13843c0d9c63SKent Gibson 13853c0d9c63SKent Gibson ret = gpiod_direction_output(desc, val); 13863c0d9c63SKent Gibson if (ret) 13873c0d9c63SKent Gibson goto out_free_linereq; 13883c0d9c63SKent Gibson } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { 13893c0d9c63SKent Gibson ret = gpiod_direction_input(desc); 13903c0d9c63SKent Gibson if (ret) 13913c0d9c63SKent Gibson goto out_free_linereq; 139273e03419SKent Gibson 139365cff704SKent Gibson ret = edge_detector_setup(&lr->lines[i], lc, i, 139473e03419SKent Gibson flags & GPIO_V2_LINE_EDGE_FLAGS); 139573e03419SKent Gibson if (ret) 139673e03419SKent Gibson goto out_free_linereq; 13973c0d9c63SKent Gibson } 13983c0d9c63SKent Gibson 13993c0d9c63SKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 1400aad95584SKent Gibson GPIO_V2_LINE_CHANGED_REQUESTED, desc); 14013c0d9c63SKent Gibson 14023c0d9c63SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", 14033c0d9c63SKent Gibson offset); 14043c0d9c63SKent Gibson } 14053c0d9c63SKent Gibson 14063c0d9c63SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 14073c0d9c63SKent Gibson if (fd < 0) { 14083c0d9c63SKent Gibson ret = fd; 14093c0d9c63SKent Gibson goto out_free_linereq; 14103c0d9c63SKent Gibson } 14113c0d9c63SKent Gibson 14123c0d9c63SKent Gibson file = anon_inode_getfile("gpio-line", &line_fileops, lr, 14133c0d9c63SKent Gibson O_RDONLY | O_CLOEXEC); 14143c0d9c63SKent Gibson if (IS_ERR(file)) { 14153c0d9c63SKent Gibson ret = PTR_ERR(file); 14163c0d9c63SKent Gibson goto out_put_unused_fd; 14173c0d9c63SKent Gibson } 14183c0d9c63SKent Gibson 14193c0d9c63SKent Gibson ulr.fd = fd; 14203c0d9c63SKent Gibson if (copy_to_user(ip, &ulr, sizeof(ulr))) { 14213c0d9c63SKent Gibson /* 14223c0d9c63SKent Gibson * fput() will trigger the release() callback, so do not go onto 14233c0d9c63SKent Gibson * the regular error cleanup path here. 14243c0d9c63SKent Gibson */ 14253c0d9c63SKent Gibson fput(file); 14263c0d9c63SKent Gibson put_unused_fd(fd); 14273c0d9c63SKent Gibson return -EFAULT; 14283c0d9c63SKent Gibson } 14293c0d9c63SKent Gibson 14303c0d9c63SKent Gibson fd_install(fd, file); 14313c0d9c63SKent Gibson 14323c0d9c63SKent Gibson dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", 14333c0d9c63SKent Gibson lr->num_lines); 14343c0d9c63SKent Gibson 14353c0d9c63SKent Gibson return 0; 14363c0d9c63SKent Gibson 14373c0d9c63SKent Gibson out_put_unused_fd: 14383c0d9c63SKent Gibson put_unused_fd(fd); 14393c0d9c63SKent Gibson out_free_linereq: 14403c0d9c63SKent Gibson linereq_free(lr); 14413c0d9c63SKent Gibson return ret; 14423c0d9c63SKent Gibson } 14433c0d9c63SKent Gibson 14443c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 1445925ca369SKent Gibson 1446925ca369SKent Gibson /* 1447925ca369SKent Gibson * GPIO line event management 1448925ca369SKent Gibson */ 1449925ca369SKent Gibson 1450925ca369SKent Gibson /** 1451925ca369SKent Gibson * struct lineevent_state - contains the state of a userspace event 1452925ca369SKent Gibson * @gdev: the GPIO device the event pertains to 1453925ca369SKent Gibson * @label: consumer label used to tag descriptors 1454925ca369SKent Gibson * @desc: the GPIO descriptor held by this event 1455925ca369SKent Gibson * @eflags: the event flags this line was requested with 1456925ca369SKent Gibson * @irq: the interrupt that trigger in response to events on this GPIO 1457925ca369SKent Gibson * @wait: wait queue that handles blocking reads of events 1458925ca369SKent Gibson * @events: KFIFO for the GPIO events 1459925ca369SKent Gibson * @timestamp: cache for the timestamp storing it between hardirq 1460925ca369SKent Gibson * and IRQ thread, used to bring the timestamp close to the actual 1461925ca369SKent Gibson * event 1462925ca369SKent Gibson */ 1463925ca369SKent Gibson struct lineevent_state { 1464925ca369SKent Gibson struct gpio_device *gdev; 1465925ca369SKent Gibson const char *label; 1466925ca369SKent Gibson struct gpio_desc *desc; 1467925ca369SKent Gibson u32 eflags; 1468925ca369SKent Gibson int irq; 1469925ca369SKent Gibson wait_queue_head_t wait; 1470925ca369SKent Gibson DECLARE_KFIFO(events, struct gpioevent_data, 16); 1471925ca369SKent Gibson u64 timestamp; 1472925ca369SKent Gibson }; 1473925ca369SKent Gibson 1474925ca369SKent Gibson #define GPIOEVENT_REQUEST_VALID_FLAGS \ 1475925ca369SKent Gibson (GPIOEVENT_REQUEST_RISING_EDGE | \ 1476925ca369SKent Gibson GPIOEVENT_REQUEST_FALLING_EDGE) 1477925ca369SKent Gibson 147849bc5279SKent Gibson static __poll_t lineevent_poll(struct file *file, 1479925ca369SKent Gibson struct poll_table_struct *wait) 1480925ca369SKent Gibson { 148149bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1482925ca369SKent Gibson __poll_t events = 0; 1483925ca369SKent Gibson 148449bc5279SKent Gibson poll_wait(file, &le->wait, wait); 1485925ca369SKent Gibson 1486925ca369SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock)) 1487925ca369SKent Gibson events = EPOLLIN | EPOLLRDNORM; 1488925ca369SKent Gibson 1489925ca369SKent Gibson return events; 1490925ca369SKent Gibson } 1491925ca369SKent Gibson 14925ad284abSAndy Shevchenko static ssize_t lineevent_get_size(void) 14935ad284abSAndy Shevchenko { 149447e538d8SAndy Shevchenko #if defined(CONFIG_X86_64) && !defined(CONFIG_UML) 14955ad284abSAndy Shevchenko /* i386 has no padding after 'id' */ 14965ad284abSAndy Shevchenko if (in_ia32_syscall()) { 14975ad284abSAndy Shevchenko struct compat_gpioeevent_data { 14985ad284abSAndy Shevchenko compat_u64 timestamp; 14995ad284abSAndy Shevchenko u32 id; 15005ad284abSAndy Shevchenko }; 15015ad284abSAndy Shevchenko 15025ad284abSAndy Shevchenko return sizeof(struct compat_gpioeevent_data); 15035ad284abSAndy Shevchenko } 15045ad284abSAndy Shevchenko #endif 15055ad284abSAndy Shevchenko return sizeof(struct gpioevent_data); 15065ad284abSAndy Shevchenko } 1507925ca369SKent Gibson 150849bc5279SKent Gibson static ssize_t lineevent_read(struct file *file, 1509925ca369SKent Gibson char __user *buf, 1510925ca369SKent Gibson size_t count, 1511925ca369SKent Gibson loff_t *f_ps) 1512925ca369SKent Gibson { 151349bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1514925ca369SKent Gibson struct gpioevent_data ge; 1515925ca369SKent Gibson ssize_t bytes_read = 0; 15165ad284abSAndy Shevchenko ssize_t ge_size; 1517925ca369SKent Gibson int ret; 1518925ca369SKent Gibson 15195ad284abSAndy Shevchenko /* 15205ad284abSAndy Shevchenko * When compatible system call is being used the struct gpioevent_data, 15215ad284abSAndy Shevchenko * in case of at least ia32, has different size due to the alignment 15225ad284abSAndy Shevchenko * differences. Because we have first member 64 bits followed by one of 15235ad284abSAndy Shevchenko * 32 bits there is no gap between them. The only difference is the 15245ad284abSAndy Shevchenko * padding at the end of the data structure. Hence, we calculate the 15255ad284abSAndy Shevchenko * actual sizeof() and pass this as an argument to copy_to_user() to 15265ad284abSAndy Shevchenko * drop unneeded bytes from the output. 15275ad284abSAndy Shevchenko */ 15285ad284abSAndy Shevchenko ge_size = lineevent_get_size(); 15295ad284abSAndy Shevchenko if (count < ge_size) 1530925ca369SKent Gibson return -EINVAL; 1531925ca369SKent Gibson 1532925ca369SKent Gibson do { 1533925ca369SKent Gibson spin_lock(&le->wait.lock); 1534925ca369SKent Gibson if (kfifo_is_empty(&le->events)) { 1535925ca369SKent Gibson if (bytes_read) { 1536925ca369SKent Gibson spin_unlock(&le->wait.lock); 1537925ca369SKent Gibson return bytes_read; 1538925ca369SKent Gibson } 1539925ca369SKent Gibson 154049bc5279SKent Gibson if (file->f_flags & O_NONBLOCK) { 1541925ca369SKent Gibson spin_unlock(&le->wait.lock); 1542925ca369SKent Gibson return -EAGAIN; 1543925ca369SKent Gibson } 1544925ca369SKent Gibson 1545925ca369SKent Gibson ret = wait_event_interruptible_locked(le->wait, 1546925ca369SKent Gibson !kfifo_is_empty(&le->events)); 1547925ca369SKent Gibson if (ret) { 1548925ca369SKent Gibson spin_unlock(&le->wait.lock); 1549925ca369SKent Gibson return ret; 1550925ca369SKent Gibson } 1551925ca369SKent Gibson } 1552925ca369SKent Gibson 1553925ca369SKent Gibson ret = kfifo_out(&le->events, &ge, 1); 1554925ca369SKent Gibson spin_unlock(&le->wait.lock); 1555925ca369SKent Gibson if (ret != 1) { 1556925ca369SKent Gibson /* 1557925ca369SKent Gibson * This should never happen - we were holding the lock 1558925ca369SKent Gibson * from the moment we learned the fifo is no longer 1559925ca369SKent Gibson * empty until now. 1560925ca369SKent Gibson */ 1561925ca369SKent Gibson ret = -EIO; 1562925ca369SKent Gibson break; 1563925ca369SKent Gibson } 1564925ca369SKent Gibson 15655ad284abSAndy Shevchenko if (copy_to_user(buf + bytes_read, &ge, ge_size)) 1566925ca369SKent Gibson return -EFAULT; 15675ad284abSAndy Shevchenko bytes_read += ge_size; 15685ad284abSAndy Shevchenko } while (count >= bytes_read + ge_size); 1569925ca369SKent Gibson 1570925ca369SKent Gibson return bytes_read; 1571925ca369SKent Gibson } 1572925ca369SKent Gibson 157346824272SKent Gibson static void lineevent_free(struct lineevent_state *le) 1574925ca369SKent Gibson { 157546824272SKent Gibson if (le->irq) 1576925ca369SKent Gibson free_irq(le->irq, le); 157746824272SKent Gibson if (le->desc) 1578925ca369SKent Gibson gpiod_free(le->desc); 1579925ca369SKent Gibson kfree(le->label); 158046824272SKent Gibson put_device(&le->gdev->dev); 1581925ca369SKent Gibson kfree(le); 158246824272SKent Gibson } 158346824272SKent Gibson 158446824272SKent Gibson static int lineevent_release(struct inode *inode, struct file *file) 158546824272SKent Gibson { 158646824272SKent Gibson lineevent_free(file->private_data); 1587925ca369SKent Gibson return 0; 1588925ca369SKent Gibson } 1589925ca369SKent Gibson 159049bc5279SKent Gibson static long lineevent_ioctl(struct file *file, unsigned int cmd, 1591925ca369SKent Gibson unsigned long arg) 1592925ca369SKent Gibson { 159349bc5279SKent Gibson struct lineevent_state *le = file->private_data; 1594925ca369SKent Gibson void __user *ip = (void __user *)arg; 1595925ca369SKent Gibson struct gpiohandle_data ghd; 1596925ca369SKent Gibson 1597925ca369SKent Gibson /* 1598925ca369SKent Gibson * We can get the value for an event line but not set it, 1599925ca369SKent Gibson * because it is input by definition. 1600925ca369SKent Gibson */ 1601925ca369SKent Gibson if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { 1602925ca369SKent Gibson int val; 1603925ca369SKent Gibson 1604925ca369SKent Gibson memset(&ghd, 0, sizeof(ghd)); 1605925ca369SKent Gibson 1606925ca369SKent Gibson val = gpiod_get_value_cansleep(le->desc); 1607925ca369SKent Gibson if (val < 0) 1608925ca369SKent Gibson return val; 1609925ca369SKent Gibson ghd.values[0] = val; 1610925ca369SKent Gibson 1611925ca369SKent Gibson if (copy_to_user(ip, &ghd, sizeof(ghd))) 1612925ca369SKent Gibson return -EFAULT; 1613925ca369SKent Gibson 1614925ca369SKent Gibson return 0; 1615925ca369SKent Gibson } 1616925ca369SKent Gibson return -EINVAL; 1617925ca369SKent Gibson } 1618925ca369SKent Gibson 1619925ca369SKent Gibson #ifdef CONFIG_COMPAT 162049bc5279SKent Gibson static long lineevent_ioctl_compat(struct file *file, unsigned int cmd, 1621925ca369SKent Gibson unsigned long arg) 1622925ca369SKent Gibson { 162349bc5279SKent Gibson return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 1624925ca369SKent Gibson } 1625925ca369SKent Gibson #endif 1626925ca369SKent Gibson 1627925ca369SKent Gibson static const struct file_operations lineevent_fileops = { 1628925ca369SKent Gibson .release = lineevent_release, 1629925ca369SKent Gibson .read = lineevent_read, 1630925ca369SKent Gibson .poll = lineevent_poll, 1631925ca369SKent Gibson .owner = THIS_MODULE, 1632925ca369SKent Gibson .llseek = noop_llseek, 1633925ca369SKent Gibson .unlocked_ioctl = lineevent_ioctl, 1634925ca369SKent Gibson #ifdef CONFIG_COMPAT 1635925ca369SKent Gibson .compat_ioctl = lineevent_ioctl_compat, 1636925ca369SKent Gibson #endif 1637925ca369SKent Gibson }; 1638925ca369SKent Gibson 1639925ca369SKent Gibson static irqreturn_t lineevent_irq_thread(int irq, void *p) 1640925ca369SKent Gibson { 1641925ca369SKent Gibson struct lineevent_state *le = p; 1642925ca369SKent Gibson struct gpioevent_data ge; 1643925ca369SKent Gibson int ret; 1644925ca369SKent Gibson 1645925ca369SKent Gibson /* Do not leak kernel stack to userspace */ 1646925ca369SKent Gibson memset(&ge, 0, sizeof(ge)); 1647925ca369SKent Gibson 1648925ca369SKent Gibson /* 1649925ca369SKent Gibson * We may be running from a nested threaded interrupt in which case 1650925ca369SKent Gibson * we didn't get the timestamp from lineevent_irq_handler(). 1651925ca369SKent Gibson */ 1652925ca369SKent Gibson if (!le->timestamp) 1653925ca369SKent Gibson ge.timestamp = ktime_get_ns(); 1654925ca369SKent Gibson else 1655925ca369SKent Gibson ge.timestamp = le->timestamp; 1656925ca369SKent Gibson 1657925ca369SKent Gibson if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE 1658925ca369SKent Gibson && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { 1659925ca369SKent Gibson int level = gpiod_get_value_cansleep(le->desc); 1660925ca369SKent Gibson 1661925ca369SKent Gibson if (level) 1662925ca369SKent Gibson /* Emit low-to-high event */ 1663925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_RISING_EDGE; 1664925ca369SKent Gibson else 1665925ca369SKent Gibson /* Emit high-to-low event */ 1666925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_FALLING_EDGE; 1667925ca369SKent Gibson } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) { 1668925ca369SKent Gibson /* Emit low-to-high event */ 1669925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_RISING_EDGE; 1670925ca369SKent Gibson } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { 1671925ca369SKent Gibson /* Emit high-to-low event */ 1672925ca369SKent Gibson ge.id = GPIOEVENT_EVENT_FALLING_EDGE; 1673925ca369SKent Gibson } else { 1674925ca369SKent Gibson return IRQ_NONE; 1675925ca369SKent Gibson } 1676925ca369SKent Gibson 1677925ca369SKent Gibson ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge, 1678925ca369SKent Gibson 1, &le->wait.lock); 1679925ca369SKent Gibson if (ret) 1680925ca369SKent Gibson wake_up_poll(&le->wait, EPOLLIN); 1681925ca369SKent Gibson else 1682925ca369SKent Gibson pr_debug_ratelimited("event FIFO is full - event dropped\n"); 1683925ca369SKent Gibson 1684925ca369SKent Gibson return IRQ_HANDLED; 1685925ca369SKent Gibson } 1686925ca369SKent Gibson 1687925ca369SKent Gibson static irqreturn_t lineevent_irq_handler(int irq, void *p) 1688925ca369SKent Gibson { 1689925ca369SKent Gibson struct lineevent_state *le = p; 1690925ca369SKent Gibson 1691925ca369SKent Gibson /* 1692925ca369SKent Gibson * Just store the timestamp in hardirq context so we get it as 1693925ca369SKent Gibson * close in time as possible to the actual event. 1694925ca369SKent Gibson */ 1695925ca369SKent Gibson le->timestamp = ktime_get_ns(); 1696925ca369SKent Gibson 1697925ca369SKent Gibson return IRQ_WAKE_THREAD; 1698925ca369SKent Gibson } 1699925ca369SKent Gibson 1700925ca369SKent Gibson static int lineevent_create(struct gpio_device *gdev, void __user *ip) 1701925ca369SKent Gibson { 1702925ca369SKent Gibson struct gpioevent_request eventreq; 1703925ca369SKent Gibson struct lineevent_state *le; 1704925ca369SKent Gibson struct gpio_desc *desc; 1705925ca369SKent Gibson struct file *file; 1706925ca369SKent Gibson u32 offset; 1707925ca369SKent Gibson u32 lflags; 1708925ca369SKent Gibson u32 eflags; 1709925ca369SKent Gibson int fd; 1710925ca369SKent Gibson int ret; 171146824272SKent Gibson int irq, irqflags = 0; 1712925ca369SKent Gibson 1713925ca369SKent Gibson if (copy_from_user(&eventreq, ip, sizeof(eventreq))) 1714925ca369SKent Gibson return -EFAULT; 1715925ca369SKent Gibson 1716925ca369SKent Gibson offset = eventreq.lineoffset; 1717925ca369SKent Gibson lflags = eventreq.handleflags; 1718925ca369SKent Gibson eflags = eventreq.eventflags; 1719925ca369SKent Gibson 1720925ca369SKent Gibson desc = gpiochip_get_desc(gdev->chip, offset); 1721925ca369SKent Gibson if (IS_ERR(desc)) 1722925ca369SKent Gibson return PTR_ERR(desc); 1723925ca369SKent Gibson 1724925ca369SKent Gibson /* Return an error if a unknown flag is set */ 1725925ca369SKent Gibson if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || 1726925ca369SKent Gibson (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) 1727925ca369SKent Gibson return -EINVAL; 1728925ca369SKent Gibson 1729925ca369SKent Gibson /* This is just wrong: we don't look for events on output lines */ 1730925ca369SKent Gibson if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) || 1731925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || 1732925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) 1733925ca369SKent Gibson return -EINVAL; 1734925ca369SKent Gibson 1735925ca369SKent Gibson /* Only one bias flag can be set. */ 1736925ca369SKent Gibson if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && 1737925ca369SKent Gibson (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | 1738925ca369SKent Gibson GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || 1739925ca369SKent Gibson ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && 1740925ca369SKent Gibson (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) 1741925ca369SKent Gibson return -EINVAL; 1742925ca369SKent Gibson 1743925ca369SKent Gibson le = kzalloc(sizeof(*le), GFP_KERNEL); 1744925ca369SKent Gibson if (!le) 1745925ca369SKent Gibson return -ENOMEM; 1746925ca369SKent Gibson le->gdev = gdev; 1747925ca369SKent Gibson get_device(&gdev->dev); 1748925ca369SKent Gibson 1749f188ac12SKent Gibson if (eventreq.consumer_label[0] != '\0') { 1750f188ac12SKent Gibson /* label is only initialized if consumer_label is set */ 1751f188ac12SKent Gibson le->label = kstrndup(eventreq.consumer_label, 1752f188ac12SKent Gibson sizeof(eventreq.consumer_label) - 1, 1753925ca369SKent Gibson GFP_KERNEL); 1754925ca369SKent Gibson if (!le->label) { 1755925ca369SKent Gibson ret = -ENOMEM; 1756925ca369SKent Gibson goto out_free_le; 1757925ca369SKent Gibson } 1758925ca369SKent Gibson } 1759925ca369SKent Gibson 1760925ca369SKent Gibson ret = gpiod_request(desc, le->label); 1761925ca369SKent Gibson if (ret) 176246824272SKent Gibson goto out_free_le; 1763925ca369SKent Gibson le->desc = desc; 1764925ca369SKent Gibson le->eflags = eflags; 1765925ca369SKent Gibson 1766c274b58aSKent Gibson linehandle_flags_to_desc_flags(lflags, &desc->flags); 1767925ca369SKent Gibson 1768925ca369SKent Gibson ret = gpiod_direction_input(desc); 1769925ca369SKent Gibson if (ret) 177046824272SKent Gibson goto out_free_le; 1771925ca369SKent Gibson 17726accc376SKent Gibson blocking_notifier_call_chain(&desc->gdev->notifier, 1773aad95584SKent Gibson GPIO_V2_LINE_CHANGED_REQUESTED, desc); 1774925ca369SKent Gibson 177546824272SKent Gibson irq = gpiod_to_irq(desc); 177646824272SKent Gibson if (irq <= 0) { 1777925ca369SKent Gibson ret = -ENODEV; 177846824272SKent Gibson goto out_free_le; 1779925ca369SKent Gibson } 178046824272SKent Gibson le->irq = irq; 1781925ca369SKent Gibson 1782925ca369SKent Gibson if (eflags & GPIOEVENT_REQUEST_RISING_EDGE) 1783925ca369SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 1784925ca369SKent Gibson IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 1785925ca369SKent Gibson if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE) 1786925ca369SKent Gibson irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 1787925ca369SKent Gibson IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 1788925ca369SKent Gibson irqflags |= IRQF_ONESHOT; 1789925ca369SKent Gibson 1790925ca369SKent Gibson INIT_KFIFO(le->events); 1791925ca369SKent Gibson init_waitqueue_head(&le->wait); 1792925ca369SKent Gibson 1793925ca369SKent Gibson /* Request a thread to read the events */ 1794925ca369SKent Gibson ret = request_threaded_irq(le->irq, 1795925ca369SKent Gibson lineevent_irq_handler, 1796925ca369SKent Gibson lineevent_irq_thread, 1797925ca369SKent Gibson irqflags, 1798925ca369SKent Gibson le->label, 1799925ca369SKent Gibson le); 1800925ca369SKent Gibson if (ret) 180146824272SKent Gibson goto out_free_le; 1802925ca369SKent Gibson 1803925ca369SKent Gibson fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 1804925ca369SKent Gibson if (fd < 0) { 1805925ca369SKent Gibson ret = fd; 180646824272SKent Gibson goto out_free_le; 1807925ca369SKent Gibson } 1808925ca369SKent Gibson 1809925ca369SKent Gibson file = anon_inode_getfile("gpio-event", 1810925ca369SKent Gibson &lineevent_fileops, 1811925ca369SKent Gibson le, 1812925ca369SKent Gibson O_RDONLY | O_CLOEXEC); 1813925ca369SKent Gibson if (IS_ERR(file)) { 1814925ca369SKent Gibson ret = PTR_ERR(file); 1815925ca369SKent Gibson goto out_put_unused_fd; 1816925ca369SKent Gibson } 1817925ca369SKent Gibson 1818925ca369SKent Gibson eventreq.fd = fd; 1819925ca369SKent Gibson if (copy_to_user(ip, &eventreq, sizeof(eventreq))) { 1820925ca369SKent Gibson /* 1821925ca369SKent Gibson * fput() will trigger the release() callback, so do not go onto 1822925ca369SKent Gibson * the regular error cleanup path here. 1823925ca369SKent Gibson */ 1824925ca369SKent Gibson fput(file); 1825925ca369SKent Gibson put_unused_fd(fd); 1826925ca369SKent Gibson return -EFAULT; 1827925ca369SKent Gibson } 1828925ca369SKent Gibson 1829925ca369SKent Gibson fd_install(fd, file); 1830925ca369SKent Gibson 1831925ca369SKent Gibson return 0; 1832925ca369SKent Gibson 1833925ca369SKent Gibson out_put_unused_fd: 1834925ca369SKent Gibson put_unused_fd(fd); 1835925ca369SKent Gibson out_free_le: 183646824272SKent Gibson lineevent_free(le); 1837925ca369SKent Gibson return ret; 1838925ca369SKent Gibson } 1839925ca369SKent Gibson 1840aad95584SKent Gibson static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2, 1841aad95584SKent Gibson struct gpioline_info *info_v1) 1842aad95584SKent Gibson { 1843aad95584SKent Gibson u64 flagsv2 = info_v2->flags; 1844aad95584SKent Gibson 1845aad95584SKent Gibson memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name)); 1846aad95584SKent Gibson memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer)); 1847aad95584SKent Gibson info_v1->line_offset = info_v2->offset; 1848aad95584SKent Gibson info_v1->flags = 0; 1849aad95584SKent Gibson 1850aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_USED) 1851aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_KERNEL; 1852aad95584SKent Gibson 1853aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT) 1854aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_IS_OUT; 1855aad95584SKent Gibson 1856aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 1857aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW; 1858aad95584SKent Gibson 1859aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN) 1860aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN; 1861aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE) 1862aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE; 1863aad95584SKent Gibson 1864aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP) 1865aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; 1866aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) 1867aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; 1868aad95584SKent Gibson if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED) 1869aad95584SKent Gibson info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE; 1870aad95584SKent Gibson } 1871aad95584SKent Gibson 1872aad95584SKent Gibson static void gpio_v2_line_info_changed_to_v1( 1873aad95584SKent Gibson struct gpio_v2_line_info_changed *lic_v2, 1874aad95584SKent Gibson struct gpioline_info_changed *lic_v1) 1875aad95584SKent Gibson { 1876aad95584SKent Gibson gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info); 1877aad95584SKent Gibson lic_v1->timestamp = lic_v2->timestamp_ns; 1878aad95584SKent Gibson lic_v1->event_type = lic_v2->event_type; 1879aad95584SKent Gibson } 1880aad95584SKent Gibson 18813c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 18823c0d9c63SKent Gibson 1883925ca369SKent Gibson static void gpio_desc_to_lineinfo(struct gpio_desc *desc, 1884aad95584SKent Gibson struct gpio_v2_line_info *info) 1885925ca369SKent Gibson { 1886925ca369SKent Gibson struct gpio_chip *gc = desc->gdev->chip; 1887925ca369SKent Gibson bool ok_for_pinctrl; 1888925ca369SKent Gibson unsigned long flags; 188965cff704SKent Gibson u32 debounce_period_us; 189065cff704SKent Gibson unsigned int num_attrs = 0; 1891925ca369SKent Gibson 189269e4e136SKent Gibson memset(info, 0, sizeof(*info)); 1893aad95584SKent Gibson info->offset = gpio_chip_hwgpio(desc); 1894925ca369SKent Gibson 1895925ca369SKent Gibson /* 1896925ca369SKent Gibson * This function takes a mutex so we must check this before taking 1897925ca369SKent Gibson * the spinlock. 1898925ca369SKent Gibson * 1899925ca369SKent Gibson * FIXME: find a non-racy way to retrieve this information. Maybe a 1900925ca369SKent Gibson * lock common to both frameworks? 1901925ca369SKent Gibson */ 1902925ca369SKent Gibson ok_for_pinctrl = 1903aad95584SKent Gibson pinctrl_gpio_can_use_line(gc->base + info->offset); 1904925ca369SKent Gibson 1905925ca369SKent Gibson spin_lock_irqsave(&gpio_lock, flags); 1906925ca369SKent Gibson 190769e4e136SKent Gibson if (desc->name) 190869e4e136SKent Gibson strscpy(info->name, desc->name, sizeof(info->name)); 1909925ca369SKent Gibson 191069e4e136SKent Gibson if (desc->label) 191169e4e136SKent Gibson strscpy(info->consumer, desc->label, sizeof(info->consumer)); 1912925ca369SKent Gibson 1913925ca369SKent Gibson /* 1914925ca369SKent Gibson * Userspace only need to know that the kernel is using this GPIO so 1915925ca369SKent Gibson * it can't use it. 1916925ca369SKent Gibson */ 1917925ca369SKent Gibson info->flags = 0; 1918925ca369SKent Gibson if (test_bit(FLAG_REQUESTED, &desc->flags) || 1919925ca369SKent Gibson test_bit(FLAG_IS_HOGGED, &desc->flags) || 1920925ca369SKent Gibson test_bit(FLAG_USED_AS_IRQ, &desc->flags) || 1921925ca369SKent Gibson test_bit(FLAG_EXPORT, &desc->flags) || 1922925ca369SKent Gibson test_bit(FLAG_SYSFS, &desc->flags) || 1923925ca369SKent Gibson !ok_for_pinctrl) 1924aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_USED; 1925aad95584SKent Gibson 1926925ca369SKent Gibson if (test_bit(FLAG_IS_OUT, &desc->flags)) 1927aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OUTPUT; 1928aad95584SKent Gibson else 1929aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_INPUT; 1930aad95584SKent Gibson 1931925ca369SKent Gibson if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 1932aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW; 1933aad95584SKent Gibson 1934925ca369SKent Gibson if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 1935aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; 1936925ca369SKent Gibson if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 1937aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; 1938aad95584SKent Gibson 1939925ca369SKent Gibson if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) 1940aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED; 1941925ca369SKent Gibson if (test_bit(FLAG_PULL_DOWN, &desc->flags)) 1942aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN; 1943925ca369SKent Gibson if (test_bit(FLAG_PULL_UP, &desc->flags)) 1944aad95584SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; 1945925ca369SKent Gibson 194673e03419SKent Gibson if (test_bit(FLAG_EDGE_RISING, &desc->flags)) 194773e03419SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; 194873e03419SKent Gibson if (test_bit(FLAG_EDGE_FALLING, &desc->flags)) 194973e03419SKent Gibson info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; 195073e03419SKent Gibson 195165cff704SKent Gibson debounce_period_us = READ_ONCE(desc->debounce_period_us); 195265cff704SKent Gibson if (debounce_period_us) { 195365cff704SKent Gibson info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; 195465cff704SKent Gibson info->attrs[num_attrs].debounce_period_us = debounce_period_us; 195565cff704SKent Gibson num_attrs++; 195665cff704SKent Gibson } 195765cff704SKent Gibson info->num_attrs = num_attrs; 1958925ca369SKent Gibson 1959925ca369SKent Gibson spin_unlock_irqrestore(&gpio_lock, flags); 1960925ca369SKent Gibson } 1961925ca369SKent Gibson 1962925ca369SKent Gibson struct gpio_chardev_data { 1963925ca369SKent Gibson struct gpio_device *gdev; 1964925ca369SKent Gibson wait_queue_head_t wait; 1965aad95584SKent Gibson DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32); 1966925ca369SKent Gibson struct notifier_block lineinfo_changed_nb; 1967925ca369SKent Gibson unsigned long *watched_lines; 1968aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 1969aad95584SKent Gibson atomic_t watch_abi_version; 1970aad95584SKent Gibson #endif 1971925ca369SKent Gibson }; 1972925ca369SKent Gibson 1973aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 1974aad95584SKent Gibson /* 1975aad95584SKent Gibson * returns 0 if the versions match, else the previously selected ABI version 1976aad95584SKent Gibson */ 1977aad95584SKent Gibson static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata, 1978aad95584SKent Gibson unsigned int version) 1979aad95584SKent Gibson { 1980aad95584SKent Gibson int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version); 1981aad95584SKent Gibson 1982aad95584SKent Gibson if (abiv == version) 1983aad95584SKent Gibson return 0; 1984aad95584SKent Gibson 1985aad95584SKent Gibson return abiv; 1986aad95584SKent Gibson } 1987aad95584SKent Gibson #endif 1988aad95584SKent Gibson 1989aad95584SKent Gibson static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip, 1990aad95584SKent Gibson bool watch) 1991aad95584SKent Gibson { 1992aad95584SKent Gibson struct gpio_desc *desc; 1993aad95584SKent Gibson struct gpio_v2_line_info lineinfo; 1994aad95584SKent Gibson 1995aad95584SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 1996aad95584SKent Gibson return -EFAULT; 1997aad95584SKent Gibson 1998aad95584SKent Gibson if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding))) 1999aad95584SKent Gibson return -EINVAL; 2000aad95584SKent Gibson 2001aad95584SKent Gibson desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset); 2002aad95584SKent Gibson if (IS_ERR(desc)) 2003aad95584SKent Gibson return PTR_ERR(desc); 2004aad95584SKent Gibson 2005aad95584SKent Gibson if (watch) { 2006aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2007aad95584SKent Gibson if (lineinfo_ensure_abi_version(cdev, 2)) 2008aad95584SKent Gibson return -EPERM; 2009aad95584SKent Gibson #endif 2010aad95584SKent Gibson if (test_and_set_bit(lineinfo.offset, cdev->watched_lines)) 2011aad95584SKent Gibson return -EBUSY; 2012aad95584SKent Gibson } 2013aad95584SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo); 2014aad95584SKent Gibson 2015aad95584SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 2016aad95584SKent Gibson if (watch) 2017aad95584SKent Gibson clear_bit(lineinfo.offset, cdev->watched_lines); 2018aad95584SKent Gibson return -EFAULT; 2019aad95584SKent Gibson } 2020aad95584SKent Gibson 2021aad95584SKent Gibson return 0; 2022aad95584SKent Gibson } 2023aad95584SKent Gibson 2024925ca369SKent Gibson /* 2025925ca369SKent Gibson * gpio_ioctl() - ioctl handler for the GPIO chardev 2026925ca369SKent Gibson */ 202749bc5279SKent Gibson static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 2028925ca369SKent Gibson { 2029e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2030e2b781c5SKent Gibson struct gpio_device *gdev = cdev->gdev; 2031925ca369SKent Gibson struct gpio_chip *gc = gdev->chip; 2032925ca369SKent Gibson void __user *ip = (void __user *)arg; 2033925ca369SKent Gibson __u32 offset; 2034925ca369SKent Gibson 2035925ca369SKent Gibson /* We fail any subsequent ioctl():s when the chip is gone */ 2036925ca369SKent Gibson if (!gc) 2037925ca369SKent Gibson return -ENODEV; 2038925ca369SKent Gibson 2039925ca369SKent Gibson /* Fill in the struct and pass to userspace */ 2040925ca369SKent Gibson if (cmd == GPIO_GET_CHIPINFO_IOCTL) { 2041925ca369SKent Gibson struct gpiochip_info chipinfo; 2042925ca369SKent Gibson 2043925ca369SKent Gibson memset(&chipinfo, 0, sizeof(chipinfo)); 2044925ca369SKent Gibson 204569e4e136SKent Gibson strscpy(chipinfo.name, dev_name(&gdev->dev), 2046925ca369SKent Gibson sizeof(chipinfo.name)); 204769e4e136SKent Gibson strscpy(chipinfo.label, gdev->label, 2048925ca369SKent Gibson sizeof(chipinfo.label)); 2049925ca369SKent Gibson chipinfo.lines = gdev->ngpio; 2050925ca369SKent Gibson if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) 2051925ca369SKent Gibson return -EFAULT; 2052925ca369SKent Gibson return 0; 20533c0d9c63SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2054925ca369SKent Gibson } else if (cmd == GPIO_GET_LINEINFO_IOCTL) { 2055aad95584SKent Gibson struct gpio_desc *desc; 2056925ca369SKent Gibson struct gpioline_info lineinfo; 2057aad95584SKent Gibson struct gpio_v2_line_info lineinfo_v2; 2058925ca369SKent Gibson 2059925ca369SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 2060925ca369SKent Gibson return -EFAULT; 2061925ca369SKent Gibson 20621bf7ba40SKent Gibson /* this doubles as a range check on line_offset */ 2063925ca369SKent Gibson desc = gpiochip_get_desc(gc, lineinfo.line_offset); 2064925ca369SKent Gibson if (IS_ERR(desc)) 2065925ca369SKent Gibson return PTR_ERR(desc); 2066925ca369SKent Gibson 2067aad95584SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo_v2); 2068aad95584SKent Gibson gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); 2069925ca369SKent Gibson 2070925ca369SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) 2071925ca369SKent Gibson return -EFAULT; 2072925ca369SKent Gibson return 0; 2073925ca369SKent Gibson } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) { 2074925ca369SKent Gibson return linehandle_create(gdev, ip); 2075925ca369SKent Gibson } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) { 2076925ca369SKent Gibson return lineevent_create(gdev, ip); 2077925ca369SKent Gibson } else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) { 2078aad95584SKent Gibson struct gpio_desc *desc; 2079925ca369SKent Gibson struct gpioline_info lineinfo; 2080aad95584SKent Gibson struct gpio_v2_line_info lineinfo_v2; 2081925ca369SKent Gibson 2082925ca369SKent Gibson if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 2083925ca369SKent Gibson return -EFAULT; 2084925ca369SKent Gibson 20851bf7ba40SKent Gibson /* this doubles as a range check on line_offset */ 2086925ca369SKent Gibson desc = gpiochip_get_desc(gc, lineinfo.line_offset); 2087925ca369SKent Gibson if (IS_ERR(desc)) 2088925ca369SKent Gibson return PTR_ERR(desc); 2089925ca369SKent Gibson 2090aad95584SKent Gibson if (lineinfo_ensure_abi_version(cdev, 1)) 2091aad95584SKent Gibson return -EPERM; 2092aad95584SKent Gibson 20931bf7ba40SKent Gibson if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines)) 2094925ca369SKent Gibson return -EBUSY; 2095925ca369SKent Gibson 2096aad95584SKent Gibson gpio_desc_to_lineinfo(desc, &lineinfo_v2); 2097aad95584SKent Gibson gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); 2098925ca369SKent Gibson 2099f30ef3e8SKent Gibson if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 21001bf7ba40SKent Gibson clear_bit(lineinfo.line_offset, cdev->watched_lines); 2101925ca369SKent Gibson return -EFAULT; 2102f30ef3e8SKent Gibson } 2103925ca369SKent Gibson 2104925ca369SKent Gibson return 0; 21053c0d9c63SKent Gibson #endif /* CONFIG_GPIO_CDEV_V1 */ 2106aad95584SKent Gibson } else if (cmd == GPIO_V2_GET_LINEINFO_IOCTL || 2107aad95584SKent Gibson cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL) { 2108aad95584SKent Gibson return lineinfo_get(cdev, ip, 2109aad95584SKent Gibson cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL); 21103c0d9c63SKent Gibson } else if (cmd == GPIO_V2_GET_LINE_IOCTL) { 21113c0d9c63SKent Gibson return linereq_create(gdev, ip); 2112925ca369SKent Gibson } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) { 2113925ca369SKent Gibson if (copy_from_user(&offset, ip, sizeof(offset))) 2114925ca369SKent Gibson return -EFAULT; 2115925ca369SKent Gibson 21161bf7ba40SKent Gibson if (offset >= cdev->gdev->ngpio) 21171bf7ba40SKent Gibson return -EINVAL; 2118925ca369SKent Gibson 21191bf7ba40SKent Gibson if (!test_and_clear_bit(offset, cdev->watched_lines)) 2120925ca369SKent Gibson return -EBUSY; 2121925ca369SKent Gibson 2122925ca369SKent Gibson return 0; 2123925ca369SKent Gibson } 2124925ca369SKent Gibson return -EINVAL; 2125925ca369SKent Gibson } 2126925ca369SKent Gibson 2127925ca369SKent Gibson #ifdef CONFIG_COMPAT 212849bc5279SKent Gibson static long gpio_ioctl_compat(struct file *file, unsigned int cmd, 2129925ca369SKent Gibson unsigned long arg) 2130925ca369SKent Gibson { 213149bc5279SKent Gibson return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 2132925ca369SKent Gibson } 2133925ca369SKent Gibson #endif 2134925ca369SKent Gibson 2135925ca369SKent Gibson static struct gpio_chardev_data * 2136925ca369SKent Gibson to_gpio_chardev_data(struct notifier_block *nb) 2137925ca369SKent Gibson { 2138925ca369SKent Gibson return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb); 2139925ca369SKent Gibson } 2140925ca369SKent Gibson 2141925ca369SKent Gibson static int lineinfo_changed_notify(struct notifier_block *nb, 2142925ca369SKent Gibson unsigned long action, void *data) 2143925ca369SKent Gibson { 2144e2b781c5SKent Gibson struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb); 2145aad95584SKent Gibson struct gpio_v2_line_info_changed chg; 2146925ca369SKent Gibson struct gpio_desc *desc = data; 2147925ca369SKent Gibson int ret; 2148925ca369SKent Gibson 2149e2b781c5SKent Gibson if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) 2150925ca369SKent Gibson return NOTIFY_DONE; 2151925ca369SKent Gibson 2152925ca369SKent Gibson memset(&chg, 0, sizeof(chg)); 2153925ca369SKent Gibson chg.event_type = action; 2154aad95584SKent Gibson chg.timestamp_ns = ktime_get_ns(); 2155925ca369SKent Gibson gpio_desc_to_lineinfo(desc, &chg.info); 2156925ca369SKent Gibson 2157e2b781c5SKent Gibson ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); 2158925ca369SKent Gibson if (ret) 2159e2b781c5SKent Gibson wake_up_poll(&cdev->wait, EPOLLIN); 2160925ca369SKent Gibson else 2161925ca369SKent Gibson pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n"); 2162925ca369SKent Gibson 2163925ca369SKent Gibson return NOTIFY_OK; 2164925ca369SKent Gibson } 2165925ca369SKent Gibson 216649bc5279SKent Gibson static __poll_t lineinfo_watch_poll(struct file *file, 2167925ca369SKent Gibson struct poll_table_struct *pollt) 2168925ca369SKent Gibson { 2169e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2170925ca369SKent Gibson __poll_t events = 0; 2171925ca369SKent Gibson 2172e2b781c5SKent Gibson poll_wait(file, &cdev->wait, pollt); 2173925ca369SKent Gibson 2174e2b781c5SKent Gibson if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events, 2175e2b781c5SKent Gibson &cdev->wait.lock)) 2176925ca369SKent Gibson events = EPOLLIN | EPOLLRDNORM; 2177925ca369SKent Gibson 2178925ca369SKent Gibson return events; 2179925ca369SKent Gibson } 2180925ca369SKent Gibson 218149bc5279SKent Gibson static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, 2182925ca369SKent Gibson size_t count, loff_t *off) 2183925ca369SKent Gibson { 2184e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2185aad95584SKent Gibson struct gpio_v2_line_info_changed event; 2186925ca369SKent Gibson ssize_t bytes_read = 0; 2187925ca369SKent Gibson int ret; 2188aad95584SKent Gibson size_t event_size; 2189925ca369SKent Gibson 2190aad95584SKent Gibson #ifndef CONFIG_GPIO_CDEV_V1 2191aad95584SKent Gibson event_size = sizeof(struct gpio_v2_line_info_changed); 2192aad95584SKent Gibson if (count < event_size) 2193925ca369SKent Gibson return -EINVAL; 2194aad95584SKent Gibson #endif 2195925ca369SKent Gibson 2196925ca369SKent Gibson do { 2197e2b781c5SKent Gibson spin_lock(&cdev->wait.lock); 2198e2b781c5SKent Gibson if (kfifo_is_empty(&cdev->events)) { 2199925ca369SKent Gibson if (bytes_read) { 2200e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2201925ca369SKent Gibson return bytes_read; 2202925ca369SKent Gibson } 2203925ca369SKent Gibson 220449bc5279SKent Gibson if (file->f_flags & O_NONBLOCK) { 2205e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2206925ca369SKent Gibson return -EAGAIN; 2207925ca369SKent Gibson } 2208925ca369SKent Gibson 2209e2b781c5SKent Gibson ret = wait_event_interruptible_locked(cdev->wait, 2210e2b781c5SKent Gibson !kfifo_is_empty(&cdev->events)); 2211925ca369SKent Gibson if (ret) { 2212e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2213925ca369SKent Gibson return ret; 2214925ca369SKent Gibson } 2215925ca369SKent Gibson } 2216aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2217aad95584SKent Gibson /* must be after kfifo check so watch_abi_version is set */ 2218aad95584SKent Gibson if (atomic_read(&cdev->watch_abi_version) == 2) 2219aad95584SKent Gibson event_size = sizeof(struct gpio_v2_line_info_changed); 2220aad95584SKent Gibson else 2221aad95584SKent Gibson event_size = sizeof(struct gpioline_info_changed); 2222aad95584SKent Gibson if (count < event_size) { 2223aad95584SKent Gibson spin_unlock(&cdev->wait.lock); 2224aad95584SKent Gibson return -EINVAL; 2225aad95584SKent Gibson } 2226aad95584SKent Gibson #endif 2227e2b781c5SKent Gibson ret = kfifo_out(&cdev->events, &event, 1); 2228e2b781c5SKent Gibson spin_unlock(&cdev->wait.lock); 2229925ca369SKent Gibson if (ret != 1) { 2230925ca369SKent Gibson ret = -EIO; 2231925ca369SKent Gibson break; 2232925ca369SKent Gibson /* We should never get here. See lineevent_read(). */ 2233925ca369SKent Gibson } 2234925ca369SKent Gibson 2235aad95584SKent Gibson #ifdef CONFIG_GPIO_CDEV_V1 2236aad95584SKent Gibson if (event_size == sizeof(struct gpio_v2_line_info_changed)) { 2237aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event, event_size)) 2238925ca369SKent Gibson return -EFAULT; 2239aad95584SKent Gibson } else { 2240aad95584SKent Gibson struct gpioline_info_changed event_v1; 2241aad95584SKent Gibson 2242aad95584SKent Gibson gpio_v2_line_info_changed_to_v1(&event, &event_v1); 2243aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event_v1, 2244aad95584SKent Gibson event_size)) 2245aad95584SKent Gibson return -EFAULT; 2246aad95584SKent Gibson } 2247aad95584SKent Gibson #else 2248aad95584SKent Gibson if (copy_to_user(buf + bytes_read, &event, event_size)) 2249aad95584SKent Gibson return -EFAULT; 2250aad95584SKent Gibson #endif 2251aad95584SKent Gibson bytes_read += event_size; 2252925ca369SKent Gibson } while (count >= bytes_read + sizeof(event)); 2253925ca369SKent Gibson 2254925ca369SKent Gibson return bytes_read; 2255925ca369SKent Gibson } 2256925ca369SKent Gibson 2257925ca369SKent Gibson /** 2258925ca369SKent Gibson * gpio_chrdev_open() - open the chardev for ioctl operations 2259925ca369SKent Gibson * @inode: inode for this chardev 226049bc5279SKent Gibson * @file: file struct for storing private data 2261925ca369SKent Gibson * Returns 0 on success 2262925ca369SKent Gibson */ 226349bc5279SKent Gibson static int gpio_chrdev_open(struct inode *inode, struct file *file) 2264925ca369SKent Gibson { 2265925ca369SKent Gibson struct gpio_device *gdev = container_of(inode->i_cdev, 2266925ca369SKent Gibson struct gpio_device, chrdev); 2267e2b781c5SKent Gibson struct gpio_chardev_data *cdev; 2268925ca369SKent Gibson int ret = -ENOMEM; 2269925ca369SKent Gibson 2270925ca369SKent Gibson /* Fail on open if the backing gpiochip is gone */ 2271925ca369SKent Gibson if (!gdev->chip) 2272925ca369SKent Gibson return -ENODEV; 2273925ca369SKent Gibson 2274e2b781c5SKent Gibson cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); 2275e2b781c5SKent Gibson if (!cdev) 2276925ca369SKent Gibson return -ENOMEM; 2277925ca369SKent Gibson 2278e2b781c5SKent Gibson cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL); 2279e2b781c5SKent Gibson if (!cdev->watched_lines) 2280e2b781c5SKent Gibson goto out_free_cdev; 2281925ca369SKent Gibson 2282e2b781c5SKent Gibson init_waitqueue_head(&cdev->wait); 2283e2b781c5SKent Gibson INIT_KFIFO(cdev->events); 2284e2b781c5SKent Gibson cdev->gdev = gdev; 2285925ca369SKent Gibson 2286e2b781c5SKent Gibson cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify; 22876accc376SKent Gibson ret = blocking_notifier_chain_register(&gdev->notifier, 2288e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2289925ca369SKent Gibson if (ret) 2290925ca369SKent Gibson goto out_free_bitmap; 2291925ca369SKent Gibson 2292925ca369SKent Gibson get_device(&gdev->dev); 2293e2b781c5SKent Gibson file->private_data = cdev; 2294925ca369SKent Gibson 229549bc5279SKent Gibson ret = nonseekable_open(inode, file); 2296925ca369SKent Gibson if (ret) 2297925ca369SKent Gibson goto out_unregister_notifier; 2298925ca369SKent Gibson 2299925ca369SKent Gibson return ret; 2300925ca369SKent Gibson 2301925ca369SKent Gibson out_unregister_notifier: 23026accc376SKent Gibson blocking_notifier_chain_unregister(&gdev->notifier, 2303e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2304925ca369SKent Gibson out_free_bitmap: 2305e2b781c5SKent Gibson bitmap_free(cdev->watched_lines); 2306e2b781c5SKent Gibson out_free_cdev: 2307e2b781c5SKent Gibson kfree(cdev); 2308925ca369SKent Gibson return ret; 2309925ca369SKent Gibson } 2310925ca369SKent Gibson 2311925ca369SKent Gibson /** 2312925ca369SKent Gibson * gpio_chrdev_release() - close chardev after ioctl operations 2313925ca369SKent Gibson * @inode: inode for this chardev 231449bc5279SKent Gibson * @file: file struct for storing private data 2315925ca369SKent Gibson * Returns 0 on success 2316925ca369SKent Gibson */ 231749bc5279SKent Gibson static int gpio_chrdev_release(struct inode *inode, struct file *file) 2318925ca369SKent Gibson { 2319e2b781c5SKent Gibson struct gpio_chardev_data *cdev = file->private_data; 2320e2b781c5SKent Gibson struct gpio_device *gdev = cdev->gdev; 2321925ca369SKent Gibson 2322e2b781c5SKent Gibson bitmap_free(cdev->watched_lines); 23236accc376SKent Gibson blocking_notifier_chain_unregister(&gdev->notifier, 2324e2b781c5SKent Gibson &cdev->lineinfo_changed_nb); 2325925ca369SKent Gibson put_device(&gdev->dev); 2326e2b781c5SKent Gibson kfree(cdev); 2327925ca369SKent Gibson 2328925ca369SKent Gibson return 0; 2329925ca369SKent Gibson } 2330925ca369SKent Gibson 2331925ca369SKent Gibson static const struct file_operations gpio_fileops = { 2332925ca369SKent Gibson .release = gpio_chrdev_release, 2333925ca369SKent Gibson .open = gpio_chrdev_open, 2334925ca369SKent Gibson .poll = lineinfo_watch_poll, 2335925ca369SKent Gibson .read = lineinfo_watch_read, 2336925ca369SKent Gibson .owner = THIS_MODULE, 2337925ca369SKent Gibson .llseek = no_llseek, 2338925ca369SKent Gibson .unlocked_ioctl = gpio_ioctl, 2339925ca369SKent Gibson #ifdef CONFIG_COMPAT 2340925ca369SKent Gibson .compat_ioctl = gpio_ioctl_compat, 2341925ca369SKent Gibson #endif 2342925ca369SKent Gibson }; 2343925ca369SKent Gibson 2344925ca369SKent Gibson int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) 2345925ca369SKent Gibson { 2346925ca369SKent Gibson int ret; 2347925ca369SKent Gibson 2348925ca369SKent Gibson cdev_init(&gdev->chrdev, &gpio_fileops); 2349925ca369SKent Gibson gdev->chrdev.owner = THIS_MODULE; 2350925ca369SKent Gibson gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id); 2351925ca369SKent Gibson 2352925ca369SKent Gibson ret = cdev_device_add(&gdev->chrdev, &gdev->dev); 2353925ca369SKent Gibson if (ret) 2354925ca369SKent Gibson return ret; 2355925ca369SKent Gibson 2356925ca369SKent Gibson chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n", 2357925ca369SKent Gibson MAJOR(devt), gdev->id); 2358925ca369SKent Gibson 2359925ca369SKent Gibson return 0; 2360925ca369SKent Gibson } 2361925ca369SKent Gibson 2362925ca369SKent Gibson void gpiolib_cdev_unregister(struct gpio_device *gdev) 2363925ca369SKent Gibson { 2364925ca369SKent Gibson cdev_device_del(&gdev->chrdev, &gdev->dev); 2365925ca369SKent Gibson } 2366