1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <linux/anon_inodes.h> 4 #include <linux/atomic.h> 5 #include <linux/bitmap.h> 6 #include <linux/build_bug.h> 7 #include <linux/cdev.h> 8 #include <linux/compat.h> 9 #include <linux/device.h> 10 #include <linux/err.h> 11 #include <linux/file.h> 12 #include <linux/gpio.h> 13 #include <linux/gpio/driver.h> 14 #include <linux/interrupt.h> 15 #include <linux/irqreturn.h> 16 #include <linux/kernel.h> 17 #include <linux/kfifo.h> 18 #include <linux/module.h> 19 #include <linux/mutex.h> 20 #include <linux/pinctrl/consumer.h> 21 #include <linux/poll.h> 22 #include <linux/spinlock.h> 23 #include <linux/timekeeping.h> 24 #include <linux/uaccess.h> 25 #include <uapi/linux/gpio.h> 26 27 #include "gpiolib.h" 28 #include "gpiolib-cdev.h" 29 30 /* 31 * Array sizes must ensure 64-bit alignment and not create holes in the 32 * struct packing. 33 */ 34 static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2)); 35 static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8)); 36 37 /* 38 * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility 39 */ 40 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8)); 41 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8)); 42 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8)); 43 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8)); 44 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8)); 45 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8)); 46 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8)); 47 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8)); 48 49 /* Character device interface to GPIO. 50 * 51 * The GPIO character device, /dev/gpiochipN, provides userspace an 52 * interface to gpiolib GPIOs via ioctl()s. 53 */ 54 55 /* 56 * GPIO line handle management 57 */ 58 59 #ifdef CONFIG_GPIO_CDEV_V1 60 /** 61 * struct linehandle_state - contains the state of a userspace handle 62 * @gdev: the GPIO device the handle pertains to 63 * @label: consumer label used to tag descriptors 64 * @descs: the GPIO descriptors held by this handle 65 * @num_descs: the number of descriptors held in the descs array 66 */ 67 struct linehandle_state { 68 struct gpio_device *gdev; 69 const char *label; 70 struct gpio_desc *descs[GPIOHANDLES_MAX]; 71 u32 num_descs; 72 }; 73 74 #define GPIOHANDLE_REQUEST_VALID_FLAGS \ 75 (GPIOHANDLE_REQUEST_INPUT | \ 76 GPIOHANDLE_REQUEST_OUTPUT | \ 77 GPIOHANDLE_REQUEST_ACTIVE_LOW | \ 78 GPIOHANDLE_REQUEST_BIAS_PULL_UP | \ 79 GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \ 80 GPIOHANDLE_REQUEST_BIAS_DISABLE | \ 81 GPIOHANDLE_REQUEST_OPEN_DRAIN | \ 82 GPIOHANDLE_REQUEST_OPEN_SOURCE) 83 84 static int linehandle_validate_flags(u32 flags) 85 { 86 /* Return an error if an unknown flag is set */ 87 if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) 88 return -EINVAL; 89 90 /* 91 * Do not allow both INPUT & OUTPUT flags to be set as they are 92 * contradictory. 93 */ 94 if ((flags & GPIOHANDLE_REQUEST_INPUT) && 95 (flags & GPIOHANDLE_REQUEST_OUTPUT)) 96 return -EINVAL; 97 98 /* 99 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If 100 * the hardware actually supports enabling both at the same time the 101 * electrical result would be disastrous. 102 */ 103 if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) && 104 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) 105 return -EINVAL; 106 107 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */ 108 if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) && 109 ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || 110 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))) 111 return -EINVAL; 112 113 /* Bias flags only allowed for input or output mode. */ 114 if (!((flags & GPIOHANDLE_REQUEST_INPUT) || 115 (flags & GPIOHANDLE_REQUEST_OUTPUT)) && 116 ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) || 117 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) || 118 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN))) 119 return -EINVAL; 120 121 /* Only one bias flag can be set. */ 122 if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && 123 (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | 124 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || 125 ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && 126 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) 127 return -EINVAL; 128 129 return 0; 130 } 131 132 static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp) 133 { 134 assign_bit(FLAG_ACTIVE_LOW, flagsp, 135 lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW); 136 assign_bit(FLAG_OPEN_DRAIN, flagsp, 137 lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN); 138 assign_bit(FLAG_OPEN_SOURCE, flagsp, 139 lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE); 140 assign_bit(FLAG_PULL_UP, flagsp, 141 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP); 142 assign_bit(FLAG_PULL_DOWN, flagsp, 143 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN); 144 assign_bit(FLAG_BIAS_DISABLE, flagsp, 145 lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE); 146 } 147 148 static long linehandle_set_config(struct linehandle_state *lh, 149 void __user *ip) 150 { 151 struct gpiohandle_config gcnf; 152 struct gpio_desc *desc; 153 int i, ret; 154 u32 lflags; 155 156 if (copy_from_user(&gcnf, ip, sizeof(gcnf))) 157 return -EFAULT; 158 159 lflags = gcnf.flags; 160 ret = linehandle_validate_flags(lflags); 161 if (ret) 162 return ret; 163 164 for (i = 0; i < lh->num_descs; i++) { 165 desc = lh->descs[i]; 166 linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags); 167 168 /* 169 * Lines have to be requested explicitly for input 170 * or output, else the line will be treated "as is". 171 */ 172 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { 173 int val = !!gcnf.default_values[i]; 174 175 ret = gpiod_direction_output(desc, val); 176 if (ret) 177 return ret; 178 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { 179 ret = gpiod_direction_input(desc); 180 if (ret) 181 return ret; 182 } 183 184 blocking_notifier_call_chain(&desc->gdev->notifier, 185 GPIO_V2_LINE_CHANGED_CONFIG, 186 desc); 187 } 188 return 0; 189 } 190 191 static long linehandle_ioctl(struct file *file, unsigned int cmd, 192 unsigned long arg) 193 { 194 struct linehandle_state *lh = file->private_data; 195 void __user *ip = (void __user *)arg; 196 struct gpiohandle_data ghd; 197 DECLARE_BITMAP(vals, GPIOHANDLES_MAX); 198 int i; 199 200 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { 201 /* NOTE: It's ok to read values of output lines. */ 202 int ret = gpiod_get_array_value_complex(false, 203 true, 204 lh->num_descs, 205 lh->descs, 206 NULL, 207 vals); 208 if (ret) 209 return ret; 210 211 memset(&ghd, 0, sizeof(ghd)); 212 for (i = 0; i < lh->num_descs; i++) 213 ghd.values[i] = test_bit(i, vals); 214 215 if (copy_to_user(ip, &ghd, sizeof(ghd))) 216 return -EFAULT; 217 218 return 0; 219 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) { 220 /* 221 * All line descriptors were created at once with the same 222 * flags so just check if the first one is really output. 223 */ 224 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags)) 225 return -EPERM; 226 227 if (copy_from_user(&ghd, ip, sizeof(ghd))) 228 return -EFAULT; 229 230 /* Clamp all values to [0,1] */ 231 for (i = 0; i < lh->num_descs; i++) 232 __assign_bit(i, vals, ghd.values[i]); 233 234 /* Reuse the array setting function */ 235 return gpiod_set_array_value_complex(false, 236 true, 237 lh->num_descs, 238 lh->descs, 239 NULL, 240 vals); 241 } else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) { 242 return linehandle_set_config(lh, ip); 243 } 244 return -EINVAL; 245 } 246 247 #ifdef CONFIG_COMPAT 248 static long linehandle_ioctl_compat(struct file *file, unsigned int cmd, 249 unsigned long arg) 250 { 251 return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 252 } 253 #endif 254 255 static void linehandle_free(struct linehandle_state *lh) 256 { 257 int i; 258 259 for (i = 0; i < lh->num_descs; i++) 260 if (lh->descs[i]) 261 gpiod_free(lh->descs[i]); 262 kfree(lh->label); 263 put_device(&lh->gdev->dev); 264 kfree(lh); 265 } 266 267 static int linehandle_release(struct inode *inode, struct file *file) 268 { 269 linehandle_free(file->private_data); 270 return 0; 271 } 272 273 static const struct file_operations linehandle_fileops = { 274 .release = linehandle_release, 275 .owner = THIS_MODULE, 276 .llseek = noop_llseek, 277 .unlocked_ioctl = linehandle_ioctl, 278 #ifdef CONFIG_COMPAT 279 .compat_ioctl = linehandle_ioctl_compat, 280 #endif 281 }; 282 283 static int linehandle_create(struct gpio_device *gdev, void __user *ip) 284 { 285 struct gpiohandle_request handlereq; 286 struct linehandle_state *lh; 287 struct file *file; 288 int fd, i, ret; 289 u32 lflags; 290 291 if (copy_from_user(&handlereq, ip, sizeof(handlereq))) 292 return -EFAULT; 293 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) 294 return -EINVAL; 295 296 lflags = handlereq.flags; 297 298 ret = linehandle_validate_flags(lflags); 299 if (ret) 300 return ret; 301 302 lh = kzalloc(sizeof(*lh), GFP_KERNEL); 303 if (!lh) 304 return -ENOMEM; 305 lh->gdev = gdev; 306 get_device(&gdev->dev); 307 308 /* Make sure this is terminated */ 309 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0'; 310 if (strlen(handlereq.consumer_label)) { 311 lh->label = kstrdup(handlereq.consumer_label, 312 GFP_KERNEL); 313 if (!lh->label) { 314 ret = -ENOMEM; 315 goto out_free_lh; 316 } 317 } 318 319 lh->num_descs = handlereq.lines; 320 321 /* Request each GPIO */ 322 for (i = 0; i < handlereq.lines; i++) { 323 u32 offset = handlereq.lineoffsets[i]; 324 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); 325 326 if (IS_ERR(desc)) { 327 ret = PTR_ERR(desc); 328 goto out_free_lh; 329 } 330 331 ret = gpiod_request(desc, lh->label); 332 if (ret) 333 goto out_free_lh; 334 lh->descs[i] = desc; 335 linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags); 336 337 ret = gpiod_set_transitory(desc, false); 338 if (ret < 0) 339 goto out_free_lh; 340 341 /* 342 * Lines have to be requested explicitly for input 343 * or output, else the line will be treated "as is". 344 */ 345 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { 346 int val = !!handlereq.default_values[i]; 347 348 ret = gpiod_direction_output(desc, val); 349 if (ret) 350 goto out_free_lh; 351 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { 352 ret = gpiod_direction_input(desc); 353 if (ret) 354 goto out_free_lh; 355 } 356 357 blocking_notifier_call_chain(&desc->gdev->notifier, 358 GPIO_V2_LINE_CHANGED_REQUESTED, desc); 359 360 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", 361 offset); 362 } 363 364 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 365 if (fd < 0) { 366 ret = fd; 367 goto out_free_lh; 368 } 369 370 file = anon_inode_getfile("gpio-linehandle", 371 &linehandle_fileops, 372 lh, 373 O_RDONLY | O_CLOEXEC); 374 if (IS_ERR(file)) { 375 ret = PTR_ERR(file); 376 goto out_put_unused_fd; 377 } 378 379 handlereq.fd = fd; 380 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) { 381 /* 382 * fput() will trigger the release() callback, so do not go onto 383 * the regular error cleanup path here. 384 */ 385 fput(file); 386 put_unused_fd(fd); 387 return -EFAULT; 388 } 389 390 fd_install(fd, file); 391 392 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", 393 lh->num_descs); 394 395 return 0; 396 397 out_put_unused_fd: 398 put_unused_fd(fd); 399 out_free_lh: 400 linehandle_free(lh); 401 return ret; 402 } 403 #endif /* CONFIG_GPIO_CDEV_V1 */ 404 405 /** 406 * struct line - contains the state of a requested line 407 * @desc: the GPIO descriptor for this line. 408 * @req: the corresponding line request 409 * @irq: the interrupt triggered in response to events on this GPIO 410 * @eflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or 411 * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied 412 * @timestamp_ns: cache for the timestamp storing it between hardirq and 413 * IRQ thread, used to bring the timestamp close to the actual event 414 * @req_seqno: the seqno for the current edge event in the sequence of 415 * events for the corresponding line request. This is drawn from the @req. 416 * @line_seqno: the seqno for the current edge event in the sequence of 417 * events for this line. 418 */ 419 struct line { 420 struct gpio_desc *desc; 421 /* 422 * -- edge detector specific fields -- 423 */ 424 struct linereq *req; 425 unsigned int irq; 426 u64 eflags; 427 /* 428 * timestamp_ns and req_seqno are accessed only by 429 * edge_irq_handler() and edge_irq_thread(), which are themselves 430 * mutually exclusive, so no additional protection is necessary. 431 */ 432 u64 timestamp_ns; 433 u32 req_seqno; 434 u32 line_seqno; 435 }; 436 437 /** 438 * struct linereq - contains the state of a userspace line request 439 * @gdev: the GPIO device the line request pertains to 440 * @label: consumer label used to tag GPIO descriptors 441 * @num_lines: the number of lines in the lines array 442 * @wait: wait queue that handles blocking reads of events 443 * @event_buffer_size: the number of elements allocated in @events 444 * @events: KFIFO for the GPIO events 445 * @seqno: the sequence number for edge events generated on all lines in 446 * this line request. Note that this is not used when @num_lines is 1, as 447 * the line_seqno is then the same and is cheaper to calculate. 448 * @config_mutex: mutex for serializing ioctl() calls to ensure consistency 449 * of configuration, particularly multi-step accesses to desc flags. 450 * @lines: the lines held by this line request, with @num_lines elements. 451 */ 452 struct linereq { 453 struct gpio_device *gdev; 454 const char *label; 455 u32 num_lines; 456 wait_queue_head_t wait; 457 u32 event_buffer_size; 458 DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); 459 atomic_t seqno; 460 struct mutex config_mutex; 461 struct line lines[]; 462 }; 463 464 #define GPIO_V2_LINE_BIAS_FLAGS \ 465 (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ 466 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \ 467 GPIO_V2_LINE_FLAG_BIAS_DISABLED) 468 469 #define GPIO_V2_LINE_DIRECTION_FLAGS \ 470 (GPIO_V2_LINE_FLAG_INPUT | \ 471 GPIO_V2_LINE_FLAG_OUTPUT) 472 473 #define GPIO_V2_LINE_DRIVE_FLAGS \ 474 (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \ 475 GPIO_V2_LINE_FLAG_OPEN_SOURCE) 476 477 #define GPIO_V2_LINE_EDGE_FLAGS \ 478 (GPIO_V2_LINE_FLAG_EDGE_RISING | \ 479 GPIO_V2_LINE_FLAG_EDGE_FALLING) 480 481 #define GPIO_V2_LINE_VALID_FLAGS \ 482 (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ 483 GPIO_V2_LINE_DIRECTION_FLAGS | \ 484 GPIO_V2_LINE_DRIVE_FLAGS | \ 485 GPIO_V2_LINE_EDGE_FLAGS | \ 486 GPIO_V2_LINE_BIAS_FLAGS) 487 488 static void linereq_put_event(struct linereq *lr, 489 struct gpio_v2_line_event *le) 490 { 491 bool overflow = false; 492 493 spin_lock(&lr->wait.lock); 494 if (kfifo_is_full(&lr->events)) { 495 overflow = true; 496 kfifo_skip(&lr->events); 497 } 498 kfifo_in(&lr->events, le, 1); 499 spin_unlock(&lr->wait.lock); 500 if (!overflow) 501 wake_up_poll(&lr->wait, EPOLLIN); 502 else 503 pr_debug_ratelimited("event FIFO is full - event dropped\n"); 504 } 505 506 static irqreturn_t edge_irq_thread(int irq, void *p) 507 { 508 struct line *line = p; 509 struct linereq *lr = line->req; 510 struct gpio_v2_line_event le; 511 512 /* Do not leak kernel stack to userspace */ 513 memset(&le, 0, sizeof(le)); 514 515 if (line->timestamp_ns) { 516 le.timestamp_ns = line->timestamp_ns; 517 } else { 518 /* 519 * We may be running from a nested threaded interrupt in 520 * which case we didn't get the timestamp from 521 * edge_irq_handler(). 522 */ 523 le.timestamp_ns = ktime_get_ns(); 524 if (lr->num_lines != 1) 525 line->req_seqno = atomic_inc_return(&lr->seqno); 526 } 527 line->timestamp_ns = 0; 528 529 if (line->eflags == (GPIO_V2_LINE_FLAG_EDGE_RISING | 530 GPIO_V2_LINE_FLAG_EDGE_FALLING)) { 531 int level = gpiod_get_value_cansleep(line->desc); 532 533 if (level) 534 /* Emit low-to-high event */ 535 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 536 else 537 /* Emit high-to-low event */ 538 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 539 } else if (line->eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) { 540 /* Emit low-to-high event */ 541 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 542 } else if (line->eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) { 543 /* Emit high-to-low event */ 544 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 545 } else { 546 return IRQ_NONE; 547 } 548 line->line_seqno++; 549 le.line_seqno = line->line_seqno; 550 le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno; 551 le.offset = gpio_chip_hwgpio(line->desc); 552 553 linereq_put_event(lr, &le); 554 555 return IRQ_HANDLED; 556 } 557 558 static irqreturn_t edge_irq_handler(int irq, void *p) 559 { 560 struct line *line = p; 561 struct linereq *lr = line->req; 562 563 /* 564 * Just store the timestamp in hardirq context so we get it as 565 * close in time as possible to the actual event. 566 */ 567 line->timestamp_ns = ktime_get_ns(); 568 569 if (lr->num_lines != 1) 570 line->req_seqno = atomic_inc_return(&lr->seqno); 571 572 return IRQ_WAKE_THREAD; 573 } 574 575 static void edge_detector_stop(struct line *line) 576 { 577 if (line->irq) { 578 free_irq(line->irq, line); 579 line->irq = 0; 580 } 581 582 line->eflags = 0; 583 } 584 585 static int edge_detector_setup(struct line *line, 586 u64 eflags) 587 { 588 unsigned long irqflags = 0; 589 int irq, ret; 590 591 if (eflags && !kfifo_initialized(&line->req->events)) { 592 ret = kfifo_alloc(&line->req->events, 593 line->req->event_buffer_size, GFP_KERNEL); 594 if (ret) 595 return ret; 596 } 597 line->eflags = eflags; 598 599 if (!eflags) 600 return 0; 601 602 irq = gpiod_to_irq(line->desc); 603 if (irq < 0) 604 return -ENXIO; 605 606 if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) 607 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 608 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 609 if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) 610 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 611 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 612 irqflags |= IRQF_ONESHOT; 613 614 /* Request a thread to read the events */ 615 ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread, 616 irqflags, line->req->label, line); 617 if (ret) 618 return ret; 619 620 line->irq = irq; 621 return 0; 622 } 623 624 static int edge_detector_update(struct line *line, u64 eflags, 625 bool polarity_change) 626 { 627 if ((line->eflags == eflags) && !polarity_change) 628 return 0; 629 630 edge_detector_stop(line); 631 632 return edge_detector_setup(line, eflags); 633 } 634 635 static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, 636 unsigned int line_idx) 637 { 638 unsigned int i; 639 u64 mask = BIT_ULL(line_idx); 640 641 for (i = 0; i < lc->num_attrs; i++) { 642 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) && 643 (lc->attrs[i].mask & mask)) 644 return lc->attrs[i].attr.flags; 645 } 646 return lc->flags; 647 } 648 649 static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc, 650 unsigned int line_idx) 651 { 652 unsigned int i; 653 u64 mask = BIT_ULL(line_idx); 654 655 for (i = 0; i < lc->num_attrs; i++) { 656 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) && 657 (lc->attrs[i].mask & mask)) 658 return !!(lc->attrs[i].attr.values & mask); 659 } 660 return 0; 661 } 662 663 static int gpio_v2_line_flags_validate(u64 flags) 664 { 665 /* Return an error if an unknown flag is set */ 666 if (flags & ~GPIO_V2_LINE_VALID_FLAGS) 667 return -EINVAL; 668 669 /* 670 * Do not allow both INPUT and OUTPUT flags to be set as they are 671 * contradictory. 672 */ 673 if ((flags & GPIO_V2_LINE_FLAG_INPUT) && 674 (flags & GPIO_V2_LINE_FLAG_OUTPUT)) 675 return -EINVAL; 676 677 /* Edge detection requires explicit input. */ 678 if ((flags & GPIO_V2_LINE_EDGE_FLAGS) && 679 !(flags & GPIO_V2_LINE_FLAG_INPUT)) 680 return -EINVAL; 681 682 /* 683 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single 684 * request. If the hardware actually supports enabling both at the 685 * same time the electrical result would be disastrous. 686 */ 687 if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) && 688 (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE)) 689 return -EINVAL; 690 691 /* Drive requires explicit output direction. */ 692 if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) && 693 !(flags & GPIO_V2_LINE_FLAG_OUTPUT)) 694 return -EINVAL; 695 696 /* Bias requires explicit direction. */ 697 if ((flags & GPIO_V2_LINE_BIAS_FLAGS) && 698 !(flags & GPIO_V2_LINE_DIRECTION_FLAGS)) 699 return -EINVAL; 700 701 /* Only one bias flag can be set. */ 702 if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) && 703 (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | 704 GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) || 705 ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) && 706 (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) 707 return -EINVAL; 708 709 return 0; 710 } 711 712 static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, 713 unsigned int num_lines) 714 { 715 unsigned int i; 716 u64 flags; 717 int ret; 718 719 if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX) 720 return -EINVAL; 721 722 if (memchr_inv(lc->padding, 0, sizeof(lc->padding))) 723 return -EINVAL; 724 725 for (i = 0; i < num_lines; i++) { 726 flags = gpio_v2_line_config_flags(lc, i); 727 ret = gpio_v2_line_flags_validate(flags); 728 if (ret) 729 return ret; 730 } 731 return 0; 732 } 733 734 static void gpio_v2_line_config_flags_to_desc_flags(u64 flags, 735 unsigned long *flagsp) 736 { 737 assign_bit(FLAG_ACTIVE_LOW, flagsp, 738 flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW); 739 740 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) 741 set_bit(FLAG_IS_OUT, flagsp); 742 else if (flags & GPIO_V2_LINE_FLAG_INPUT) 743 clear_bit(FLAG_IS_OUT, flagsp); 744 745 assign_bit(FLAG_EDGE_RISING, flagsp, 746 flags & GPIO_V2_LINE_FLAG_EDGE_RISING); 747 assign_bit(FLAG_EDGE_FALLING, flagsp, 748 flags & GPIO_V2_LINE_FLAG_EDGE_FALLING); 749 750 assign_bit(FLAG_OPEN_DRAIN, flagsp, 751 flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN); 752 assign_bit(FLAG_OPEN_SOURCE, flagsp, 753 flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE); 754 755 assign_bit(FLAG_PULL_UP, flagsp, 756 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP); 757 assign_bit(FLAG_PULL_DOWN, flagsp, 758 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN); 759 assign_bit(FLAG_BIAS_DISABLE, flagsp, 760 flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED); 761 } 762 763 static long linereq_get_values(struct linereq *lr, void __user *ip) 764 { 765 struct gpio_v2_line_values lv; 766 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); 767 struct gpio_desc **descs; 768 unsigned int i, didx, num_get; 769 int ret; 770 771 /* NOTE: It's ok to read values of output lines. */ 772 if (copy_from_user(&lv, ip, sizeof(lv))) 773 return -EFAULT; 774 775 for (num_get = 0, i = 0; i < lr->num_lines; i++) { 776 if (lv.mask & BIT_ULL(i)) { 777 num_get++; 778 descs = &lr->lines[i].desc; 779 } 780 } 781 782 if (num_get == 0) 783 return -EINVAL; 784 785 if (num_get != 1) { 786 descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL); 787 if (!descs) 788 return -ENOMEM; 789 for (didx = 0, i = 0; i < lr->num_lines; i++) { 790 if (lv.mask & BIT_ULL(i)) { 791 descs[didx] = lr->lines[i].desc; 792 didx++; 793 } 794 } 795 } 796 ret = gpiod_get_array_value_complex(false, true, num_get, 797 descs, NULL, vals); 798 799 if (num_get != 1) 800 kfree(descs); 801 if (ret) 802 return ret; 803 804 lv.bits = 0; 805 for (didx = 0, i = 0; i < lr->num_lines; i++) { 806 if (lv.mask & BIT_ULL(i)) { 807 if (test_bit(didx, vals)) 808 lv.bits |= BIT_ULL(i); 809 didx++; 810 } 811 } 812 813 if (copy_to_user(ip, &lv, sizeof(lv))) 814 return -EFAULT; 815 816 return 0; 817 } 818 819 static long linereq_set_config_unlocked(struct linereq *lr, 820 struct gpio_v2_line_config *lc) 821 { 822 struct gpio_desc *desc; 823 unsigned int i; 824 u64 flags; 825 bool polarity_change; 826 int ret; 827 828 for (i = 0; i < lr->num_lines; i++) { 829 desc = lr->lines[i].desc; 830 flags = gpio_v2_line_config_flags(lc, i); 831 polarity_change = 832 (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) != 833 ((flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) != 0)); 834 835 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 836 /* 837 * Lines have to be requested explicitly for input 838 * or output, else the line will be treated "as is". 839 */ 840 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 841 int val = gpio_v2_line_config_output_value(lc, i); 842 843 edge_detector_stop(&lr->lines[i]); 844 ret = gpiod_direction_output(desc, val); 845 if (ret) 846 return ret; 847 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { 848 ret = gpiod_direction_input(desc); 849 if (ret) 850 return ret; 851 852 ret = edge_detector_update(&lr->lines[i], 853 flags & GPIO_V2_LINE_EDGE_FLAGS, 854 polarity_change); 855 if (ret) 856 return ret; 857 } 858 859 blocking_notifier_call_chain(&desc->gdev->notifier, 860 GPIO_V2_LINE_CHANGED_CONFIG, 861 desc); 862 } 863 return 0; 864 } 865 866 static long linereq_set_config(struct linereq *lr, void __user *ip) 867 { 868 struct gpio_v2_line_config lc; 869 int ret; 870 871 if (copy_from_user(&lc, ip, sizeof(lc))) 872 return -EFAULT; 873 874 ret = gpio_v2_line_config_validate(&lc, lr->num_lines); 875 if (ret) 876 return ret; 877 878 mutex_lock(&lr->config_mutex); 879 880 ret = linereq_set_config_unlocked(lr, &lc); 881 882 mutex_unlock(&lr->config_mutex); 883 884 return ret; 885 } 886 887 static long linereq_ioctl(struct file *file, unsigned int cmd, 888 unsigned long arg) 889 { 890 struct linereq *lr = file->private_data; 891 void __user *ip = (void __user *)arg; 892 893 if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL) 894 return linereq_get_values(lr, ip); 895 else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL) 896 return linereq_set_config(lr, ip); 897 898 return -EINVAL; 899 } 900 901 #ifdef CONFIG_COMPAT 902 static long linereq_ioctl_compat(struct file *file, unsigned int cmd, 903 unsigned long arg) 904 { 905 return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 906 } 907 #endif 908 909 static __poll_t linereq_poll(struct file *file, 910 struct poll_table_struct *wait) 911 { 912 struct linereq *lr = file->private_data; 913 __poll_t events = 0; 914 915 poll_wait(file, &lr->wait, wait); 916 917 if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events, 918 &lr->wait.lock)) 919 events = EPOLLIN | EPOLLRDNORM; 920 921 return events; 922 } 923 924 static ssize_t linereq_read(struct file *file, 925 char __user *buf, 926 size_t count, 927 loff_t *f_ps) 928 { 929 struct linereq *lr = file->private_data; 930 struct gpio_v2_line_event le; 931 ssize_t bytes_read = 0; 932 int ret; 933 934 if (count < sizeof(le)) 935 return -EINVAL; 936 937 do { 938 spin_lock(&lr->wait.lock); 939 if (kfifo_is_empty(&lr->events)) { 940 if (bytes_read) { 941 spin_unlock(&lr->wait.lock); 942 return bytes_read; 943 } 944 945 if (file->f_flags & O_NONBLOCK) { 946 spin_unlock(&lr->wait.lock); 947 return -EAGAIN; 948 } 949 950 ret = wait_event_interruptible_locked(lr->wait, 951 !kfifo_is_empty(&lr->events)); 952 if (ret) { 953 spin_unlock(&lr->wait.lock); 954 return ret; 955 } 956 } 957 958 ret = kfifo_out(&lr->events, &le, 1); 959 spin_unlock(&lr->wait.lock); 960 if (ret != 1) { 961 /* 962 * This should never happen - we were holding the 963 * lock from the moment we learned the fifo is no 964 * longer empty until now. 965 */ 966 ret = -EIO; 967 break; 968 } 969 970 if (copy_to_user(buf + bytes_read, &le, sizeof(le))) 971 return -EFAULT; 972 bytes_read += sizeof(le); 973 } while (count >= bytes_read + sizeof(le)); 974 975 return bytes_read; 976 } 977 978 static void linereq_free(struct linereq *lr) 979 { 980 unsigned int i; 981 982 for (i = 0; i < lr->num_lines; i++) { 983 edge_detector_stop(&lr->lines[i]); 984 if (lr->lines[i].desc) 985 gpiod_free(lr->lines[i].desc); 986 } 987 kfifo_free(&lr->events); 988 kfree(lr->label); 989 put_device(&lr->gdev->dev); 990 kfree(lr); 991 } 992 993 static int linereq_release(struct inode *inode, struct file *file) 994 { 995 struct linereq *lr = file->private_data; 996 997 linereq_free(lr); 998 return 0; 999 } 1000 1001 static const struct file_operations line_fileops = { 1002 .release = linereq_release, 1003 .read = linereq_read, 1004 .poll = linereq_poll, 1005 .owner = THIS_MODULE, 1006 .llseek = noop_llseek, 1007 .unlocked_ioctl = linereq_ioctl, 1008 #ifdef CONFIG_COMPAT 1009 .compat_ioctl = linereq_ioctl_compat, 1010 #endif 1011 }; 1012 1013 static int linereq_create(struct gpio_device *gdev, void __user *ip) 1014 { 1015 struct gpio_v2_line_request ulr; 1016 struct gpio_v2_line_config *lc; 1017 struct linereq *lr; 1018 struct file *file; 1019 u64 flags; 1020 unsigned int i; 1021 int fd, ret; 1022 1023 if (copy_from_user(&ulr, ip, sizeof(ulr))) 1024 return -EFAULT; 1025 1026 if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX)) 1027 return -EINVAL; 1028 1029 if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding))) 1030 return -EINVAL; 1031 1032 lc = &ulr.config; 1033 ret = gpio_v2_line_config_validate(lc, ulr.num_lines); 1034 if (ret) 1035 return ret; 1036 1037 lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); 1038 if (!lr) 1039 return -ENOMEM; 1040 1041 lr->gdev = gdev; 1042 get_device(&gdev->dev); 1043 1044 for (i = 0; i < ulr.num_lines; i++) 1045 lr->lines[i].req = lr; 1046 1047 /* Make sure this is terminated */ 1048 ulr.consumer[sizeof(ulr.consumer)-1] = '\0'; 1049 if (strlen(ulr.consumer)) { 1050 /* label is only initialized if consumer is set */ 1051 lr->label = kstrdup(ulr.consumer, GFP_KERNEL); 1052 if (!lr->label) { 1053 ret = -ENOMEM; 1054 goto out_free_linereq; 1055 } 1056 } 1057 1058 mutex_init(&lr->config_mutex); 1059 init_waitqueue_head(&lr->wait); 1060 lr->event_buffer_size = ulr.event_buffer_size; 1061 if (lr->event_buffer_size == 0) 1062 lr->event_buffer_size = ulr.num_lines * 16; 1063 else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16) 1064 lr->event_buffer_size = GPIO_V2_LINES_MAX * 16; 1065 1066 atomic_set(&lr->seqno, 0); 1067 lr->num_lines = ulr.num_lines; 1068 1069 /* Request each GPIO */ 1070 for (i = 0; i < ulr.num_lines; i++) { 1071 u32 offset = ulr.offsets[i]; 1072 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); 1073 1074 if (IS_ERR(desc)) { 1075 ret = PTR_ERR(desc); 1076 goto out_free_linereq; 1077 } 1078 1079 ret = gpiod_request(desc, lr->label); 1080 if (ret) 1081 goto out_free_linereq; 1082 1083 lr->lines[i].desc = desc; 1084 flags = gpio_v2_line_config_flags(lc, i); 1085 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 1086 1087 ret = gpiod_set_transitory(desc, false); 1088 if (ret < 0) 1089 goto out_free_linereq; 1090 1091 /* 1092 * Lines have to be requested explicitly for input 1093 * or output, else the line will be treated "as is". 1094 */ 1095 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 1096 int val = gpio_v2_line_config_output_value(lc, i); 1097 1098 ret = gpiod_direction_output(desc, val); 1099 if (ret) 1100 goto out_free_linereq; 1101 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { 1102 ret = gpiod_direction_input(desc); 1103 if (ret) 1104 goto out_free_linereq; 1105 1106 ret = edge_detector_setup(&lr->lines[i], 1107 flags & GPIO_V2_LINE_EDGE_FLAGS); 1108 if (ret) 1109 goto out_free_linereq; 1110 } 1111 1112 blocking_notifier_call_chain(&desc->gdev->notifier, 1113 GPIO_V2_LINE_CHANGED_REQUESTED, desc); 1114 1115 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", 1116 offset); 1117 } 1118 1119 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 1120 if (fd < 0) { 1121 ret = fd; 1122 goto out_free_linereq; 1123 } 1124 1125 file = anon_inode_getfile("gpio-line", &line_fileops, lr, 1126 O_RDONLY | O_CLOEXEC); 1127 if (IS_ERR(file)) { 1128 ret = PTR_ERR(file); 1129 goto out_put_unused_fd; 1130 } 1131 1132 ulr.fd = fd; 1133 if (copy_to_user(ip, &ulr, sizeof(ulr))) { 1134 /* 1135 * fput() will trigger the release() callback, so do not go onto 1136 * the regular error cleanup path here. 1137 */ 1138 fput(file); 1139 put_unused_fd(fd); 1140 return -EFAULT; 1141 } 1142 1143 fd_install(fd, file); 1144 1145 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", 1146 lr->num_lines); 1147 1148 return 0; 1149 1150 out_put_unused_fd: 1151 put_unused_fd(fd); 1152 out_free_linereq: 1153 linereq_free(lr); 1154 return ret; 1155 } 1156 1157 #ifdef CONFIG_GPIO_CDEV_V1 1158 1159 /* 1160 * GPIO line event management 1161 */ 1162 1163 /** 1164 * struct lineevent_state - contains the state of a userspace event 1165 * @gdev: the GPIO device the event pertains to 1166 * @label: consumer label used to tag descriptors 1167 * @desc: the GPIO descriptor held by this event 1168 * @eflags: the event flags this line was requested with 1169 * @irq: the interrupt that trigger in response to events on this GPIO 1170 * @wait: wait queue that handles blocking reads of events 1171 * @events: KFIFO for the GPIO events 1172 * @timestamp: cache for the timestamp storing it between hardirq 1173 * and IRQ thread, used to bring the timestamp close to the actual 1174 * event 1175 */ 1176 struct lineevent_state { 1177 struct gpio_device *gdev; 1178 const char *label; 1179 struct gpio_desc *desc; 1180 u32 eflags; 1181 int irq; 1182 wait_queue_head_t wait; 1183 DECLARE_KFIFO(events, struct gpioevent_data, 16); 1184 u64 timestamp; 1185 }; 1186 1187 #define GPIOEVENT_REQUEST_VALID_FLAGS \ 1188 (GPIOEVENT_REQUEST_RISING_EDGE | \ 1189 GPIOEVENT_REQUEST_FALLING_EDGE) 1190 1191 static __poll_t lineevent_poll(struct file *file, 1192 struct poll_table_struct *wait) 1193 { 1194 struct lineevent_state *le = file->private_data; 1195 __poll_t events = 0; 1196 1197 poll_wait(file, &le->wait, wait); 1198 1199 if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock)) 1200 events = EPOLLIN | EPOLLRDNORM; 1201 1202 return events; 1203 } 1204 1205 1206 static ssize_t lineevent_read(struct file *file, 1207 char __user *buf, 1208 size_t count, 1209 loff_t *f_ps) 1210 { 1211 struct lineevent_state *le = file->private_data; 1212 struct gpioevent_data ge; 1213 ssize_t bytes_read = 0; 1214 int ret; 1215 1216 if (count < sizeof(ge)) 1217 return -EINVAL; 1218 1219 do { 1220 spin_lock(&le->wait.lock); 1221 if (kfifo_is_empty(&le->events)) { 1222 if (bytes_read) { 1223 spin_unlock(&le->wait.lock); 1224 return bytes_read; 1225 } 1226 1227 if (file->f_flags & O_NONBLOCK) { 1228 spin_unlock(&le->wait.lock); 1229 return -EAGAIN; 1230 } 1231 1232 ret = wait_event_interruptible_locked(le->wait, 1233 !kfifo_is_empty(&le->events)); 1234 if (ret) { 1235 spin_unlock(&le->wait.lock); 1236 return ret; 1237 } 1238 } 1239 1240 ret = kfifo_out(&le->events, &ge, 1); 1241 spin_unlock(&le->wait.lock); 1242 if (ret != 1) { 1243 /* 1244 * This should never happen - we were holding the lock 1245 * from the moment we learned the fifo is no longer 1246 * empty until now. 1247 */ 1248 ret = -EIO; 1249 break; 1250 } 1251 1252 if (copy_to_user(buf + bytes_read, &ge, sizeof(ge))) 1253 return -EFAULT; 1254 bytes_read += sizeof(ge); 1255 } while (count >= bytes_read + sizeof(ge)); 1256 1257 return bytes_read; 1258 } 1259 1260 static void lineevent_free(struct lineevent_state *le) 1261 { 1262 if (le->irq) 1263 free_irq(le->irq, le); 1264 if (le->desc) 1265 gpiod_free(le->desc); 1266 kfree(le->label); 1267 put_device(&le->gdev->dev); 1268 kfree(le); 1269 } 1270 1271 static int lineevent_release(struct inode *inode, struct file *file) 1272 { 1273 lineevent_free(file->private_data); 1274 return 0; 1275 } 1276 1277 static long lineevent_ioctl(struct file *file, unsigned int cmd, 1278 unsigned long arg) 1279 { 1280 struct lineevent_state *le = file->private_data; 1281 void __user *ip = (void __user *)arg; 1282 struct gpiohandle_data ghd; 1283 1284 /* 1285 * We can get the value for an event line but not set it, 1286 * because it is input by definition. 1287 */ 1288 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { 1289 int val; 1290 1291 memset(&ghd, 0, sizeof(ghd)); 1292 1293 val = gpiod_get_value_cansleep(le->desc); 1294 if (val < 0) 1295 return val; 1296 ghd.values[0] = val; 1297 1298 if (copy_to_user(ip, &ghd, sizeof(ghd))) 1299 return -EFAULT; 1300 1301 return 0; 1302 } 1303 return -EINVAL; 1304 } 1305 1306 #ifdef CONFIG_COMPAT 1307 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd, 1308 unsigned long arg) 1309 { 1310 return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 1311 } 1312 #endif 1313 1314 static const struct file_operations lineevent_fileops = { 1315 .release = lineevent_release, 1316 .read = lineevent_read, 1317 .poll = lineevent_poll, 1318 .owner = THIS_MODULE, 1319 .llseek = noop_llseek, 1320 .unlocked_ioctl = lineevent_ioctl, 1321 #ifdef CONFIG_COMPAT 1322 .compat_ioctl = lineevent_ioctl_compat, 1323 #endif 1324 }; 1325 1326 static irqreturn_t lineevent_irq_thread(int irq, void *p) 1327 { 1328 struct lineevent_state *le = p; 1329 struct gpioevent_data ge; 1330 int ret; 1331 1332 /* Do not leak kernel stack to userspace */ 1333 memset(&ge, 0, sizeof(ge)); 1334 1335 /* 1336 * We may be running from a nested threaded interrupt in which case 1337 * we didn't get the timestamp from lineevent_irq_handler(). 1338 */ 1339 if (!le->timestamp) 1340 ge.timestamp = ktime_get_ns(); 1341 else 1342 ge.timestamp = le->timestamp; 1343 1344 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE 1345 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { 1346 int level = gpiod_get_value_cansleep(le->desc); 1347 1348 if (level) 1349 /* Emit low-to-high event */ 1350 ge.id = GPIOEVENT_EVENT_RISING_EDGE; 1351 else 1352 /* Emit high-to-low event */ 1353 ge.id = GPIOEVENT_EVENT_FALLING_EDGE; 1354 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) { 1355 /* Emit low-to-high event */ 1356 ge.id = GPIOEVENT_EVENT_RISING_EDGE; 1357 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { 1358 /* Emit high-to-low event */ 1359 ge.id = GPIOEVENT_EVENT_FALLING_EDGE; 1360 } else { 1361 return IRQ_NONE; 1362 } 1363 1364 ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge, 1365 1, &le->wait.lock); 1366 if (ret) 1367 wake_up_poll(&le->wait, EPOLLIN); 1368 else 1369 pr_debug_ratelimited("event FIFO is full - event dropped\n"); 1370 1371 return IRQ_HANDLED; 1372 } 1373 1374 static irqreturn_t lineevent_irq_handler(int irq, void *p) 1375 { 1376 struct lineevent_state *le = p; 1377 1378 /* 1379 * Just store the timestamp in hardirq context so we get it as 1380 * close in time as possible to the actual event. 1381 */ 1382 le->timestamp = ktime_get_ns(); 1383 1384 return IRQ_WAKE_THREAD; 1385 } 1386 1387 static int lineevent_create(struct gpio_device *gdev, void __user *ip) 1388 { 1389 struct gpioevent_request eventreq; 1390 struct lineevent_state *le; 1391 struct gpio_desc *desc; 1392 struct file *file; 1393 u32 offset; 1394 u32 lflags; 1395 u32 eflags; 1396 int fd; 1397 int ret; 1398 int irq, irqflags = 0; 1399 1400 if (copy_from_user(&eventreq, ip, sizeof(eventreq))) 1401 return -EFAULT; 1402 1403 offset = eventreq.lineoffset; 1404 lflags = eventreq.handleflags; 1405 eflags = eventreq.eventflags; 1406 1407 desc = gpiochip_get_desc(gdev->chip, offset); 1408 if (IS_ERR(desc)) 1409 return PTR_ERR(desc); 1410 1411 /* Return an error if a unknown flag is set */ 1412 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || 1413 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) 1414 return -EINVAL; 1415 1416 /* This is just wrong: we don't look for events on output lines */ 1417 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) || 1418 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || 1419 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) 1420 return -EINVAL; 1421 1422 /* Only one bias flag can be set. */ 1423 if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && 1424 (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | 1425 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || 1426 ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && 1427 (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) 1428 return -EINVAL; 1429 1430 le = kzalloc(sizeof(*le), GFP_KERNEL); 1431 if (!le) 1432 return -ENOMEM; 1433 le->gdev = gdev; 1434 get_device(&gdev->dev); 1435 1436 /* Make sure this is terminated */ 1437 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0'; 1438 if (strlen(eventreq.consumer_label)) { 1439 le->label = kstrdup(eventreq.consumer_label, 1440 GFP_KERNEL); 1441 if (!le->label) { 1442 ret = -ENOMEM; 1443 goto out_free_le; 1444 } 1445 } 1446 1447 ret = gpiod_request(desc, le->label); 1448 if (ret) 1449 goto out_free_le; 1450 le->desc = desc; 1451 le->eflags = eflags; 1452 1453 linehandle_flags_to_desc_flags(lflags, &desc->flags); 1454 1455 ret = gpiod_direction_input(desc); 1456 if (ret) 1457 goto out_free_le; 1458 1459 blocking_notifier_call_chain(&desc->gdev->notifier, 1460 GPIO_V2_LINE_CHANGED_REQUESTED, desc); 1461 1462 irq = gpiod_to_irq(desc); 1463 if (irq <= 0) { 1464 ret = -ENODEV; 1465 goto out_free_le; 1466 } 1467 le->irq = irq; 1468 1469 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE) 1470 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 1471 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 1472 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE) 1473 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 1474 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 1475 irqflags |= IRQF_ONESHOT; 1476 1477 INIT_KFIFO(le->events); 1478 init_waitqueue_head(&le->wait); 1479 1480 /* Request a thread to read the events */ 1481 ret = request_threaded_irq(le->irq, 1482 lineevent_irq_handler, 1483 lineevent_irq_thread, 1484 irqflags, 1485 le->label, 1486 le); 1487 if (ret) 1488 goto out_free_le; 1489 1490 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 1491 if (fd < 0) { 1492 ret = fd; 1493 goto out_free_le; 1494 } 1495 1496 file = anon_inode_getfile("gpio-event", 1497 &lineevent_fileops, 1498 le, 1499 O_RDONLY | O_CLOEXEC); 1500 if (IS_ERR(file)) { 1501 ret = PTR_ERR(file); 1502 goto out_put_unused_fd; 1503 } 1504 1505 eventreq.fd = fd; 1506 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) { 1507 /* 1508 * fput() will trigger the release() callback, so do not go onto 1509 * the regular error cleanup path here. 1510 */ 1511 fput(file); 1512 put_unused_fd(fd); 1513 return -EFAULT; 1514 } 1515 1516 fd_install(fd, file); 1517 1518 return 0; 1519 1520 out_put_unused_fd: 1521 put_unused_fd(fd); 1522 out_free_le: 1523 lineevent_free(le); 1524 return ret; 1525 } 1526 1527 static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2, 1528 struct gpioline_info *info_v1) 1529 { 1530 u64 flagsv2 = info_v2->flags; 1531 1532 memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name)); 1533 memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer)); 1534 info_v1->line_offset = info_v2->offset; 1535 info_v1->flags = 0; 1536 1537 if (flagsv2 & GPIO_V2_LINE_FLAG_USED) 1538 info_v1->flags |= GPIOLINE_FLAG_KERNEL; 1539 1540 if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT) 1541 info_v1->flags |= GPIOLINE_FLAG_IS_OUT; 1542 1543 if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 1544 info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW; 1545 1546 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN) 1547 info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN; 1548 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE) 1549 info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE; 1550 1551 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP) 1552 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; 1553 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) 1554 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; 1555 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED) 1556 info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE; 1557 } 1558 1559 static void gpio_v2_line_info_changed_to_v1( 1560 struct gpio_v2_line_info_changed *lic_v2, 1561 struct gpioline_info_changed *lic_v1) 1562 { 1563 gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info); 1564 lic_v1->timestamp = lic_v2->timestamp_ns; 1565 lic_v1->event_type = lic_v2->event_type; 1566 } 1567 1568 #endif /* CONFIG_GPIO_CDEV_V1 */ 1569 1570 static void gpio_desc_to_lineinfo(struct gpio_desc *desc, 1571 struct gpio_v2_line_info *info) 1572 { 1573 struct gpio_chip *gc = desc->gdev->chip; 1574 bool ok_for_pinctrl; 1575 unsigned long flags; 1576 1577 memset(info, 0, sizeof(*info)); 1578 info->offset = gpio_chip_hwgpio(desc); 1579 1580 /* 1581 * This function takes a mutex so we must check this before taking 1582 * the spinlock. 1583 * 1584 * FIXME: find a non-racy way to retrieve this information. Maybe a 1585 * lock common to both frameworks? 1586 */ 1587 ok_for_pinctrl = 1588 pinctrl_gpio_can_use_line(gc->base + info->offset); 1589 1590 spin_lock_irqsave(&gpio_lock, flags); 1591 1592 if (desc->name) 1593 strscpy(info->name, desc->name, sizeof(info->name)); 1594 1595 if (desc->label) 1596 strscpy(info->consumer, desc->label, sizeof(info->consumer)); 1597 1598 /* 1599 * Userspace only need to know that the kernel is using this GPIO so 1600 * it can't use it. 1601 */ 1602 info->flags = 0; 1603 if (test_bit(FLAG_REQUESTED, &desc->flags) || 1604 test_bit(FLAG_IS_HOGGED, &desc->flags) || 1605 test_bit(FLAG_USED_AS_IRQ, &desc->flags) || 1606 test_bit(FLAG_EXPORT, &desc->flags) || 1607 test_bit(FLAG_SYSFS, &desc->flags) || 1608 !ok_for_pinctrl) 1609 info->flags |= GPIO_V2_LINE_FLAG_USED; 1610 1611 if (test_bit(FLAG_IS_OUT, &desc->flags)) 1612 info->flags |= GPIO_V2_LINE_FLAG_OUTPUT; 1613 else 1614 info->flags |= GPIO_V2_LINE_FLAG_INPUT; 1615 1616 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 1617 info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW; 1618 1619 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 1620 info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; 1621 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 1622 info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; 1623 1624 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) 1625 info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED; 1626 if (test_bit(FLAG_PULL_DOWN, &desc->flags)) 1627 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN; 1628 if (test_bit(FLAG_PULL_UP, &desc->flags)) 1629 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; 1630 1631 if (test_bit(FLAG_EDGE_RISING, &desc->flags)) 1632 info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; 1633 if (test_bit(FLAG_EDGE_FALLING, &desc->flags)) 1634 info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; 1635 1636 spin_unlock_irqrestore(&gpio_lock, flags); 1637 } 1638 1639 struct gpio_chardev_data { 1640 struct gpio_device *gdev; 1641 wait_queue_head_t wait; 1642 DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32); 1643 struct notifier_block lineinfo_changed_nb; 1644 unsigned long *watched_lines; 1645 #ifdef CONFIG_GPIO_CDEV_V1 1646 atomic_t watch_abi_version; 1647 #endif 1648 }; 1649 1650 #ifdef CONFIG_GPIO_CDEV_V1 1651 /* 1652 * returns 0 if the versions match, else the previously selected ABI version 1653 */ 1654 static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata, 1655 unsigned int version) 1656 { 1657 int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version); 1658 1659 if (abiv == version) 1660 return 0; 1661 1662 return abiv; 1663 } 1664 #endif 1665 1666 static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip, 1667 bool watch) 1668 { 1669 struct gpio_desc *desc; 1670 struct gpio_v2_line_info lineinfo; 1671 1672 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 1673 return -EFAULT; 1674 1675 if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding))) 1676 return -EINVAL; 1677 1678 desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset); 1679 if (IS_ERR(desc)) 1680 return PTR_ERR(desc); 1681 1682 if (watch) { 1683 #ifdef CONFIG_GPIO_CDEV_V1 1684 if (lineinfo_ensure_abi_version(cdev, 2)) 1685 return -EPERM; 1686 #endif 1687 if (test_and_set_bit(lineinfo.offset, cdev->watched_lines)) 1688 return -EBUSY; 1689 } 1690 gpio_desc_to_lineinfo(desc, &lineinfo); 1691 1692 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 1693 if (watch) 1694 clear_bit(lineinfo.offset, cdev->watched_lines); 1695 return -EFAULT; 1696 } 1697 1698 return 0; 1699 } 1700 1701 /* 1702 * gpio_ioctl() - ioctl handler for the GPIO chardev 1703 */ 1704 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 1705 { 1706 struct gpio_chardev_data *cdev = file->private_data; 1707 struct gpio_device *gdev = cdev->gdev; 1708 struct gpio_chip *gc = gdev->chip; 1709 void __user *ip = (void __user *)arg; 1710 __u32 offset; 1711 1712 /* We fail any subsequent ioctl():s when the chip is gone */ 1713 if (!gc) 1714 return -ENODEV; 1715 1716 /* Fill in the struct and pass to userspace */ 1717 if (cmd == GPIO_GET_CHIPINFO_IOCTL) { 1718 struct gpiochip_info chipinfo; 1719 1720 memset(&chipinfo, 0, sizeof(chipinfo)); 1721 1722 strscpy(chipinfo.name, dev_name(&gdev->dev), 1723 sizeof(chipinfo.name)); 1724 strscpy(chipinfo.label, gdev->label, 1725 sizeof(chipinfo.label)); 1726 chipinfo.lines = gdev->ngpio; 1727 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) 1728 return -EFAULT; 1729 return 0; 1730 #ifdef CONFIG_GPIO_CDEV_V1 1731 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) { 1732 struct gpio_desc *desc; 1733 struct gpioline_info lineinfo; 1734 struct gpio_v2_line_info lineinfo_v2; 1735 1736 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 1737 return -EFAULT; 1738 1739 /* this doubles as a range check on line_offset */ 1740 desc = gpiochip_get_desc(gc, lineinfo.line_offset); 1741 if (IS_ERR(desc)) 1742 return PTR_ERR(desc); 1743 1744 gpio_desc_to_lineinfo(desc, &lineinfo_v2); 1745 gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); 1746 1747 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) 1748 return -EFAULT; 1749 return 0; 1750 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) { 1751 return linehandle_create(gdev, ip); 1752 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) { 1753 return lineevent_create(gdev, ip); 1754 } else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) { 1755 struct gpio_desc *desc; 1756 struct gpioline_info lineinfo; 1757 struct gpio_v2_line_info lineinfo_v2; 1758 1759 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 1760 return -EFAULT; 1761 1762 /* this doubles as a range check on line_offset */ 1763 desc = gpiochip_get_desc(gc, lineinfo.line_offset); 1764 if (IS_ERR(desc)) 1765 return PTR_ERR(desc); 1766 1767 if (lineinfo_ensure_abi_version(cdev, 1)) 1768 return -EPERM; 1769 1770 if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines)) 1771 return -EBUSY; 1772 1773 gpio_desc_to_lineinfo(desc, &lineinfo_v2); 1774 gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); 1775 1776 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 1777 clear_bit(lineinfo.line_offset, cdev->watched_lines); 1778 return -EFAULT; 1779 } 1780 1781 return 0; 1782 #endif /* CONFIG_GPIO_CDEV_V1 */ 1783 } else if (cmd == GPIO_V2_GET_LINEINFO_IOCTL || 1784 cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL) { 1785 return lineinfo_get(cdev, ip, 1786 cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL); 1787 } else if (cmd == GPIO_V2_GET_LINE_IOCTL) { 1788 return linereq_create(gdev, ip); 1789 } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) { 1790 if (copy_from_user(&offset, ip, sizeof(offset))) 1791 return -EFAULT; 1792 1793 if (offset >= cdev->gdev->ngpio) 1794 return -EINVAL; 1795 1796 if (!test_and_clear_bit(offset, cdev->watched_lines)) 1797 return -EBUSY; 1798 1799 return 0; 1800 } 1801 return -EINVAL; 1802 } 1803 1804 #ifdef CONFIG_COMPAT 1805 static long gpio_ioctl_compat(struct file *file, unsigned int cmd, 1806 unsigned long arg) 1807 { 1808 return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 1809 } 1810 #endif 1811 1812 static struct gpio_chardev_data * 1813 to_gpio_chardev_data(struct notifier_block *nb) 1814 { 1815 return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb); 1816 } 1817 1818 static int lineinfo_changed_notify(struct notifier_block *nb, 1819 unsigned long action, void *data) 1820 { 1821 struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb); 1822 struct gpio_v2_line_info_changed chg; 1823 struct gpio_desc *desc = data; 1824 int ret; 1825 1826 if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) 1827 return NOTIFY_DONE; 1828 1829 memset(&chg, 0, sizeof(chg)); 1830 chg.event_type = action; 1831 chg.timestamp_ns = ktime_get_ns(); 1832 gpio_desc_to_lineinfo(desc, &chg.info); 1833 1834 ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); 1835 if (ret) 1836 wake_up_poll(&cdev->wait, EPOLLIN); 1837 else 1838 pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n"); 1839 1840 return NOTIFY_OK; 1841 } 1842 1843 static __poll_t lineinfo_watch_poll(struct file *file, 1844 struct poll_table_struct *pollt) 1845 { 1846 struct gpio_chardev_data *cdev = file->private_data; 1847 __poll_t events = 0; 1848 1849 poll_wait(file, &cdev->wait, pollt); 1850 1851 if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events, 1852 &cdev->wait.lock)) 1853 events = EPOLLIN | EPOLLRDNORM; 1854 1855 return events; 1856 } 1857 1858 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, 1859 size_t count, loff_t *off) 1860 { 1861 struct gpio_chardev_data *cdev = file->private_data; 1862 struct gpio_v2_line_info_changed event; 1863 ssize_t bytes_read = 0; 1864 int ret; 1865 size_t event_size; 1866 1867 #ifndef CONFIG_GPIO_CDEV_V1 1868 event_size = sizeof(struct gpio_v2_line_info_changed); 1869 if (count < event_size) 1870 return -EINVAL; 1871 #endif 1872 1873 do { 1874 spin_lock(&cdev->wait.lock); 1875 if (kfifo_is_empty(&cdev->events)) { 1876 if (bytes_read) { 1877 spin_unlock(&cdev->wait.lock); 1878 return bytes_read; 1879 } 1880 1881 if (file->f_flags & O_NONBLOCK) { 1882 spin_unlock(&cdev->wait.lock); 1883 return -EAGAIN; 1884 } 1885 1886 ret = wait_event_interruptible_locked(cdev->wait, 1887 !kfifo_is_empty(&cdev->events)); 1888 if (ret) { 1889 spin_unlock(&cdev->wait.lock); 1890 return ret; 1891 } 1892 } 1893 #ifdef CONFIG_GPIO_CDEV_V1 1894 /* must be after kfifo check so watch_abi_version is set */ 1895 if (atomic_read(&cdev->watch_abi_version) == 2) 1896 event_size = sizeof(struct gpio_v2_line_info_changed); 1897 else 1898 event_size = sizeof(struct gpioline_info_changed); 1899 if (count < event_size) { 1900 spin_unlock(&cdev->wait.lock); 1901 return -EINVAL; 1902 } 1903 #endif 1904 ret = kfifo_out(&cdev->events, &event, 1); 1905 spin_unlock(&cdev->wait.lock); 1906 if (ret != 1) { 1907 ret = -EIO; 1908 break; 1909 /* We should never get here. See lineevent_read(). */ 1910 } 1911 1912 #ifdef CONFIG_GPIO_CDEV_V1 1913 if (event_size == sizeof(struct gpio_v2_line_info_changed)) { 1914 if (copy_to_user(buf + bytes_read, &event, event_size)) 1915 return -EFAULT; 1916 } else { 1917 struct gpioline_info_changed event_v1; 1918 1919 gpio_v2_line_info_changed_to_v1(&event, &event_v1); 1920 if (copy_to_user(buf + bytes_read, &event_v1, 1921 event_size)) 1922 return -EFAULT; 1923 } 1924 #else 1925 if (copy_to_user(buf + bytes_read, &event, event_size)) 1926 return -EFAULT; 1927 #endif 1928 bytes_read += event_size; 1929 } while (count >= bytes_read + sizeof(event)); 1930 1931 return bytes_read; 1932 } 1933 1934 /** 1935 * gpio_chrdev_open() - open the chardev for ioctl operations 1936 * @inode: inode for this chardev 1937 * @file: file struct for storing private data 1938 * Returns 0 on success 1939 */ 1940 static int gpio_chrdev_open(struct inode *inode, struct file *file) 1941 { 1942 struct gpio_device *gdev = container_of(inode->i_cdev, 1943 struct gpio_device, chrdev); 1944 struct gpio_chardev_data *cdev; 1945 int ret = -ENOMEM; 1946 1947 /* Fail on open if the backing gpiochip is gone */ 1948 if (!gdev->chip) 1949 return -ENODEV; 1950 1951 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); 1952 if (!cdev) 1953 return -ENOMEM; 1954 1955 cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL); 1956 if (!cdev->watched_lines) 1957 goto out_free_cdev; 1958 1959 init_waitqueue_head(&cdev->wait); 1960 INIT_KFIFO(cdev->events); 1961 cdev->gdev = gdev; 1962 1963 cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify; 1964 ret = blocking_notifier_chain_register(&gdev->notifier, 1965 &cdev->lineinfo_changed_nb); 1966 if (ret) 1967 goto out_free_bitmap; 1968 1969 get_device(&gdev->dev); 1970 file->private_data = cdev; 1971 1972 ret = nonseekable_open(inode, file); 1973 if (ret) 1974 goto out_unregister_notifier; 1975 1976 return ret; 1977 1978 out_unregister_notifier: 1979 blocking_notifier_chain_unregister(&gdev->notifier, 1980 &cdev->lineinfo_changed_nb); 1981 out_free_bitmap: 1982 bitmap_free(cdev->watched_lines); 1983 out_free_cdev: 1984 kfree(cdev); 1985 return ret; 1986 } 1987 1988 /** 1989 * gpio_chrdev_release() - close chardev after ioctl operations 1990 * @inode: inode for this chardev 1991 * @file: file struct for storing private data 1992 * Returns 0 on success 1993 */ 1994 static int gpio_chrdev_release(struct inode *inode, struct file *file) 1995 { 1996 struct gpio_chardev_data *cdev = file->private_data; 1997 struct gpio_device *gdev = cdev->gdev; 1998 1999 bitmap_free(cdev->watched_lines); 2000 blocking_notifier_chain_unregister(&gdev->notifier, 2001 &cdev->lineinfo_changed_nb); 2002 put_device(&gdev->dev); 2003 kfree(cdev); 2004 2005 return 0; 2006 } 2007 2008 static const struct file_operations gpio_fileops = { 2009 .release = gpio_chrdev_release, 2010 .open = gpio_chrdev_open, 2011 .poll = lineinfo_watch_poll, 2012 .read = lineinfo_watch_read, 2013 .owner = THIS_MODULE, 2014 .llseek = no_llseek, 2015 .unlocked_ioctl = gpio_ioctl, 2016 #ifdef CONFIG_COMPAT 2017 .compat_ioctl = gpio_ioctl_compat, 2018 #endif 2019 }; 2020 2021 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) 2022 { 2023 int ret; 2024 2025 cdev_init(&gdev->chrdev, &gpio_fileops); 2026 gdev->chrdev.owner = THIS_MODULE; 2027 gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id); 2028 2029 ret = cdev_device_add(&gdev->chrdev, &gdev->dev); 2030 if (ret) 2031 return ret; 2032 2033 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n", 2034 MAJOR(devt), gdev->id); 2035 2036 return 0; 2037 } 2038 2039 void gpiolib_cdev_unregister(struct gpio_device *gdev) 2040 { 2041 cdev_device_del(&gdev->chrdev, &gdev->dev); 2042 } 2043