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/compiler.h> 10 #include <linux/device.h> 11 #include <linux/err.h> 12 #include <linux/file.h> 13 #include <linux/gpio.h> 14 #include <linux/gpio/driver.h> 15 #include <linux/interrupt.h> 16 #include <linux/irqreturn.h> 17 #include <linux/kernel.h> 18 #include <linux/kfifo.h> 19 #include <linux/module.h> 20 #include <linux/mutex.h> 21 #include <linux/pinctrl/consumer.h> 22 #include <linux/poll.h> 23 #include <linux/spinlock.h> 24 #include <linux/timekeeping.h> 25 #include <linux/uaccess.h> 26 #include <linux/workqueue.h> 27 #include <linux/hte.h> 28 #include <uapi/linux/gpio.h> 29 30 #include "gpiolib.h" 31 #include "gpiolib-cdev.h" 32 33 /* 34 * Array sizes must ensure 64-bit alignment and not create holes in the 35 * struct packing. 36 */ 37 static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2)); 38 static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8)); 39 40 /* 41 * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility 42 */ 43 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8)); 44 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8)); 45 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8)); 46 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8)); 47 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8)); 48 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8)); 49 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8)); 50 static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8)); 51 52 /* Character device interface to GPIO. 53 * 54 * The GPIO character device, /dev/gpiochipN, provides userspace an 55 * interface to gpiolib GPIOs via ioctl()s. 56 */ 57 58 /* 59 * GPIO line handle management 60 */ 61 62 #ifdef CONFIG_GPIO_CDEV_V1 63 /** 64 * struct linehandle_state - contains the state of a userspace handle 65 * @gdev: the GPIO device the handle pertains to 66 * @label: consumer label used to tag descriptors 67 * @descs: the GPIO descriptors held by this handle 68 * @num_descs: the number of descriptors held in the descs array 69 */ 70 struct linehandle_state { 71 struct gpio_device *gdev; 72 const char *label; 73 struct gpio_desc *descs[GPIOHANDLES_MAX]; 74 u32 num_descs; 75 }; 76 77 #define GPIOHANDLE_REQUEST_VALID_FLAGS \ 78 (GPIOHANDLE_REQUEST_INPUT | \ 79 GPIOHANDLE_REQUEST_OUTPUT | \ 80 GPIOHANDLE_REQUEST_ACTIVE_LOW | \ 81 GPIOHANDLE_REQUEST_BIAS_PULL_UP | \ 82 GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \ 83 GPIOHANDLE_REQUEST_BIAS_DISABLE | \ 84 GPIOHANDLE_REQUEST_OPEN_DRAIN | \ 85 GPIOHANDLE_REQUEST_OPEN_SOURCE) 86 87 static int linehandle_validate_flags(u32 flags) 88 { 89 /* Return an error if an unknown flag is set */ 90 if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) 91 return -EINVAL; 92 93 /* 94 * Do not allow both INPUT & OUTPUT flags to be set as they are 95 * contradictory. 96 */ 97 if ((flags & GPIOHANDLE_REQUEST_INPUT) && 98 (flags & GPIOHANDLE_REQUEST_OUTPUT)) 99 return -EINVAL; 100 101 /* 102 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If 103 * the hardware actually supports enabling both at the same time the 104 * electrical result would be disastrous. 105 */ 106 if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) && 107 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) 108 return -EINVAL; 109 110 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */ 111 if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) && 112 ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || 113 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))) 114 return -EINVAL; 115 116 /* Bias flags only allowed for input or output mode. */ 117 if (!((flags & GPIOHANDLE_REQUEST_INPUT) || 118 (flags & GPIOHANDLE_REQUEST_OUTPUT)) && 119 ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) || 120 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) || 121 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN))) 122 return -EINVAL; 123 124 /* Only one bias flag can be set. */ 125 if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && 126 (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | 127 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || 128 ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && 129 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) 130 return -EINVAL; 131 132 return 0; 133 } 134 135 static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp) 136 { 137 assign_bit(FLAG_ACTIVE_LOW, flagsp, 138 lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW); 139 assign_bit(FLAG_OPEN_DRAIN, flagsp, 140 lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN); 141 assign_bit(FLAG_OPEN_SOURCE, flagsp, 142 lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE); 143 assign_bit(FLAG_PULL_UP, flagsp, 144 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP); 145 assign_bit(FLAG_PULL_DOWN, flagsp, 146 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN); 147 assign_bit(FLAG_BIAS_DISABLE, flagsp, 148 lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE); 149 } 150 151 static long linehandle_set_config(struct linehandle_state *lh, 152 void __user *ip) 153 { 154 struct gpiohandle_config gcnf; 155 struct gpio_desc *desc; 156 int i, ret; 157 u32 lflags; 158 159 if (copy_from_user(&gcnf, ip, sizeof(gcnf))) 160 return -EFAULT; 161 162 lflags = gcnf.flags; 163 ret = linehandle_validate_flags(lflags); 164 if (ret) 165 return ret; 166 167 for (i = 0; i < lh->num_descs; i++) { 168 desc = lh->descs[i]; 169 linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags); 170 171 /* 172 * Lines have to be requested explicitly for input 173 * or output, else the line will be treated "as is". 174 */ 175 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { 176 int val = !!gcnf.default_values[i]; 177 178 ret = gpiod_direction_output(desc, val); 179 if (ret) 180 return ret; 181 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { 182 ret = gpiod_direction_input(desc); 183 if (ret) 184 return ret; 185 } 186 187 blocking_notifier_call_chain(&desc->gdev->notifier, 188 GPIO_V2_LINE_CHANGED_CONFIG, 189 desc); 190 } 191 return 0; 192 } 193 194 static long linehandle_ioctl(struct file *file, unsigned int cmd, 195 unsigned long arg) 196 { 197 struct linehandle_state *lh = file->private_data; 198 void __user *ip = (void __user *)arg; 199 struct gpiohandle_data ghd; 200 DECLARE_BITMAP(vals, GPIOHANDLES_MAX); 201 unsigned int i; 202 int ret; 203 204 switch (cmd) { 205 case GPIOHANDLE_GET_LINE_VALUES_IOCTL: 206 /* NOTE: It's okay to read values of output lines */ 207 ret = gpiod_get_array_value_complex(false, true, 208 lh->num_descs, lh->descs, 209 NULL, vals); 210 if (ret) 211 return ret; 212 213 memset(&ghd, 0, sizeof(ghd)); 214 for (i = 0; i < lh->num_descs; i++) 215 ghd.values[i] = test_bit(i, vals); 216 217 if (copy_to_user(ip, &ghd, sizeof(ghd))) 218 return -EFAULT; 219 220 return 0; 221 case GPIOHANDLE_SET_LINE_VALUES_IOCTL: 222 /* 223 * All line descriptors were created at once with the same 224 * flags so just check if the first one is really output. 225 */ 226 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags)) 227 return -EPERM; 228 229 if (copy_from_user(&ghd, ip, sizeof(ghd))) 230 return -EFAULT; 231 232 /* Clamp all values to [0,1] */ 233 for (i = 0; i < lh->num_descs; i++) 234 __assign_bit(i, vals, ghd.values[i]); 235 236 /* Reuse the array setting function */ 237 return gpiod_set_array_value_complex(false, 238 true, 239 lh->num_descs, 240 lh->descs, 241 NULL, 242 vals); 243 case GPIOHANDLE_SET_CONFIG_IOCTL: 244 return linehandle_set_config(lh, ip); 245 default: 246 return -EINVAL; 247 } 248 } 249 250 #ifdef CONFIG_COMPAT 251 static long linehandle_ioctl_compat(struct file *file, unsigned int cmd, 252 unsigned long arg) 253 { 254 return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 255 } 256 #endif 257 258 static void linehandle_free(struct linehandle_state *lh) 259 { 260 int i; 261 262 for (i = 0; i < lh->num_descs; i++) 263 if (lh->descs[i]) 264 gpiod_free(lh->descs[i]); 265 kfree(lh->label); 266 put_device(&lh->gdev->dev); 267 kfree(lh); 268 } 269 270 static int linehandle_release(struct inode *inode, struct file *file) 271 { 272 linehandle_free(file->private_data); 273 return 0; 274 } 275 276 static const struct file_operations linehandle_fileops = { 277 .release = linehandle_release, 278 .owner = THIS_MODULE, 279 .llseek = noop_llseek, 280 .unlocked_ioctl = linehandle_ioctl, 281 #ifdef CONFIG_COMPAT 282 .compat_ioctl = linehandle_ioctl_compat, 283 #endif 284 }; 285 286 static int linehandle_create(struct gpio_device *gdev, void __user *ip) 287 { 288 struct gpiohandle_request handlereq; 289 struct linehandle_state *lh; 290 struct file *file; 291 int fd, i, ret; 292 u32 lflags; 293 294 if (copy_from_user(&handlereq, ip, sizeof(handlereq))) 295 return -EFAULT; 296 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) 297 return -EINVAL; 298 299 lflags = handlereq.flags; 300 301 ret = linehandle_validate_flags(lflags); 302 if (ret) 303 return ret; 304 305 lh = kzalloc(sizeof(*lh), GFP_KERNEL); 306 if (!lh) 307 return -ENOMEM; 308 lh->gdev = gdev; 309 get_device(&gdev->dev); 310 311 if (handlereq.consumer_label[0] != '\0') { 312 /* label is only initialized if consumer_label is set */ 313 lh->label = kstrndup(handlereq.consumer_label, 314 sizeof(handlereq.consumer_label) - 1, 315 GFP_KERNEL); 316 if (!lh->label) { 317 ret = -ENOMEM; 318 goto out_free_lh; 319 } 320 } 321 322 lh->num_descs = handlereq.lines; 323 324 /* Request each GPIO */ 325 for (i = 0; i < handlereq.lines; i++) { 326 u32 offset = handlereq.lineoffsets[i]; 327 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); 328 329 if (IS_ERR(desc)) { 330 ret = PTR_ERR(desc); 331 goto out_free_lh; 332 } 333 334 ret = gpiod_request_user(desc, lh->label); 335 if (ret) 336 goto out_free_lh; 337 lh->descs[i] = desc; 338 linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags); 339 340 ret = gpiod_set_transitory(desc, false); 341 if (ret < 0) 342 goto out_free_lh; 343 344 /* 345 * Lines have to be requested explicitly for input 346 * or output, else the line will be treated "as is". 347 */ 348 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { 349 int val = !!handlereq.default_values[i]; 350 351 ret = gpiod_direction_output(desc, val); 352 if (ret) 353 goto out_free_lh; 354 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { 355 ret = gpiod_direction_input(desc); 356 if (ret) 357 goto out_free_lh; 358 } 359 360 blocking_notifier_call_chain(&desc->gdev->notifier, 361 GPIO_V2_LINE_CHANGED_REQUESTED, desc); 362 363 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", 364 offset); 365 } 366 367 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 368 if (fd < 0) { 369 ret = fd; 370 goto out_free_lh; 371 } 372 373 file = anon_inode_getfile("gpio-linehandle", 374 &linehandle_fileops, 375 lh, 376 O_RDONLY | O_CLOEXEC); 377 if (IS_ERR(file)) { 378 ret = PTR_ERR(file); 379 goto out_put_unused_fd; 380 } 381 382 handlereq.fd = fd; 383 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) { 384 /* 385 * fput() will trigger the release() callback, so do not go onto 386 * the regular error cleanup path here. 387 */ 388 fput(file); 389 put_unused_fd(fd); 390 return -EFAULT; 391 } 392 393 fd_install(fd, file); 394 395 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", 396 lh->num_descs); 397 398 return 0; 399 400 out_put_unused_fd: 401 put_unused_fd(fd); 402 out_free_lh: 403 linehandle_free(lh); 404 return ret; 405 } 406 #endif /* CONFIG_GPIO_CDEV_V1 */ 407 408 /** 409 * struct line - contains the state of a requested line 410 * @desc: the GPIO descriptor for this line. 411 * @req: the corresponding line request 412 * @irq: the interrupt triggered in response to events on this GPIO 413 * @eflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or 414 * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied 415 * @timestamp_ns: cache for the timestamp storing it between hardirq and 416 * IRQ thread, used to bring the timestamp close to the actual event 417 * @req_seqno: the seqno for the current edge event in the sequence of 418 * events for the corresponding line request. This is drawn from the @req. 419 * @line_seqno: the seqno for the current edge event in the sequence of 420 * events for this line. 421 * @work: the worker that implements software debouncing 422 * @sw_debounced: flag indicating if the software debouncer is active 423 * @level: the current debounced physical level of the line 424 * @hdesc: the Hardware Timestamp Engine (HTE) descriptor 425 * @raw_level: the line level at the time of event 426 * @total_discard_seq: the running counter of the discarded events 427 * @last_seqno: the last sequence number before debounce period expires 428 */ 429 struct line { 430 struct gpio_desc *desc; 431 /* 432 * -- edge detector specific fields -- 433 */ 434 struct linereq *req; 435 unsigned int irq; 436 /* 437 * The flags for the active edge detector configuration. 438 * 439 * edflags is set by linereq_create(), linereq_free(), and 440 * linereq_set_config_unlocked(), which are themselves mutually 441 * exclusive, and is accessed by edge_irq_thread(), 442 * process_hw_ts_thread() and debounce_work_func(), 443 * which can all live with a slightly stale value. 444 */ 445 u64 edflags; 446 /* 447 * timestamp_ns and req_seqno are accessed only by 448 * edge_irq_handler() and edge_irq_thread(), which are themselves 449 * mutually exclusive, so no additional protection is necessary. 450 */ 451 u64 timestamp_ns; 452 u32 req_seqno; 453 /* 454 * line_seqno is accessed by either edge_irq_thread() or 455 * debounce_work_func(), which are themselves mutually exclusive, 456 * so no additional protection is necessary. 457 */ 458 u32 line_seqno; 459 /* 460 * -- debouncer specific fields -- 461 */ 462 struct delayed_work work; 463 /* 464 * sw_debounce is accessed by linereq_set_config(), which is the 465 * only setter, and linereq_get_values(), which can live with a 466 * slightly stale value. 467 */ 468 unsigned int sw_debounced; 469 /* 470 * level is accessed by debounce_work_func(), which is the only 471 * setter, and linereq_get_values() which can live with a slightly 472 * stale value. 473 */ 474 unsigned int level; 475 #ifdef CONFIG_HTE 476 struct hte_ts_desc hdesc; 477 /* 478 * HTE provider sets line level at the time of event. The valid 479 * value is 0 or 1 and negative value for an error. 480 */ 481 int raw_level; 482 /* 483 * when sw_debounce is set on HTE enabled line, this is running 484 * counter of the discarded events. 485 */ 486 u32 total_discard_seq; 487 /* 488 * when sw_debounce is set on HTE enabled line, this variable records 489 * last sequence number before debounce period expires. 490 */ 491 u32 last_seqno; 492 #endif /* CONFIG_HTE */ 493 }; 494 495 /** 496 * struct linereq - contains the state of a userspace line request 497 * @gdev: the GPIO device the line request pertains to 498 * @label: consumer label used to tag GPIO descriptors 499 * @num_lines: the number of lines in the lines array 500 * @wait: wait queue that handles blocking reads of events 501 * @event_buffer_size: the number of elements allocated in @events 502 * @events: KFIFO for the GPIO events 503 * @seqno: the sequence number for edge events generated on all lines in 504 * this line request. Note that this is not used when @num_lines is 1, as 505 * the line_seqno is then the same and is cheaper to calculate. 506 * @config_mutex: mutex for serializing ioctl() calls to ensure consistency 507 * of configuration, particularly multi-step accesses to desc flags. 508 * @lines: the lines held by this line request, with @num_lines elements. 509 */ 510 struct linereq { 511 struct gpio_device *gdev; 512 const char *label; 513 u32 num_lines; 514 wait_queue_head_t wait; 515 u32 event_buffer_size; 516 DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); 517 atomic_t seqno; 518 struct mutex config_mutex; 519 struct line lines[]; 520 }; 521 522 #define GPIO_V2_LINE_BIAS_FLAGS \ 523 (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ 524 GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \ 525 GPIO_V2_LINE_FLAG_BIAS_DISABLED) 526 527 #define GPIO_V2_LINE_DIRECTION_FLAGS \ 528 (GPIO_V2_LINE_FLAG_INPUT | \ 529 GPIO_V2_LINE_FLAG_OUTPUT) 530 531 #define GPIO_V2_LINE_DRIVE_FLAGS \ 532 (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \ 533 GPIO_V2_LINE_FLAG_OPEN_SOURCE) 534 535 #define GPIO_V2_LINE_EDGE_FLAGS \ 536 (GPIO_V2_LINE_FLAG_EDGE_RISING | \ 537 GPIO_V2_LINE_FLAG_EDGE_FALLING) 538 539 #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS 540 541 #define GPIO_V2_LINE_VALID_FLAGS \ 542 (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ 543 GPIO_V2_LINE_DIRECTION_FLAGS | \ 544 GPIO_V2_LINE_DRIVE_FLAGS | \ 545 GPIO_V2_LINE_EDGE_FLAGS | \ 546 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \ 547 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \ 548 GPIO_V2_LINE_BIAS_FLAGS) 549 550 /* subset of flags relevant for edge detector configuration */ 551 #define GPIO_V2_LINE_EDGE_DETECTOR_FLAGS \ 552 (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ 553 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \ 554 GPIO_V2_LINE_EDGE_FLAGS) 555 556 static void linereq_put_event(struct linereq *lr, 557 struct gpio_v2_line_event *le) 558 { 559 bool overflow = false; 560 561 spin_lock(&lr->wait.lock); 562 if (kfifo_is_full(&lr->events)) { 563 overflow = true; 564 kfifo_skip(&lr->events); 565 } 566 kfifo_in(&lr->events, le, 1); 567 spin_unlock(&lr->wait.lock); 568 if (!overflow) 569 wake_up_poll(&lr->wait, EPOLLIN); 570 else 571 pr_debug_ratelimited("event FIFO is full - event dropped\n"); 572 } 573 574 static u64 line_event_timestamp(struct line *line) 575 { 576 if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags)) 577 return ktime_get_real_ns(); 578 else if (IS_ENABLED(CONFIG_HTE) && 579 test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags)) 580 return line->timestamp_ns; 581 582 return ktime_get_ns(); 583 } 584 585 static u32 line_event_id(int level) 586 { 587 return level ? GPIO_V2_LINE_EVENT_RISING_EDGE : 588 GPIO_V2_LINE_EVENT_FALLING_EDGE; 589 } 590 591 #ifdef CONFIG_HTE 592 593 static enum hte_return process_hw_ts_thread(void *p) 594 { 595 struct line *line; 596 struct linereq *lr; 597 struct gpio_v2_line_event le; 598 u64 edflags; 599 int level; 600 601 if (!p) 602 return HTE_CB_HANDLED; 603 604 line = p; 605 lr = line->req; 606 607 memset(&le, 0, sizeof(le)); 608 609 le.timestamp_ns = line->timestamp_ns; 610 edflags = READ_ONCE(line->edflags); 611 612 switch (edflags & GPIO_V2_LINE_EDGE_FLAGS) { 613 case GPIO_V2_LINE_FLAG_EDGE_BOTH: 614 level = (line->raw_level >= 0) ? 615 line->raw_level : 616 gpiod_get_raw_value_cansleep(line->desc); 617 618 if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 619 level = !level; 620 621 le.id = line_event_id(level); 622 break; 623 case GPIO_V2_LINE_FLAG_EDGE_RISING: 624 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 625 break; 626 case GPIO_V2_LINE_FLAG_EDGE_FALLING: 627 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 628 break; 629 default: 630 return HTE_CB_HANDLED; 631 } 632 le.line_seqno = line->line_seqno; 633 le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno; 634 le.offset = gpio_chip_hwgpio(line->desc); 635 636 linereq_put_event(lr, &le); 637 638 return HTE_CB_HANDLED; 639 } 640 641 static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p) 642 { 643 struct line *line; 644 struct linereq *lr; 645 int diff_seqno = 0; 646 647 if (!ts || !p) 648 return HTE_CB_HANDLED; 649 650 line = p; 651 line->timestamp_ns = ts->tsc; 652 line->raw_level = ts->raw_level; 653 lr = line->req; 654 655 if (READ_ONCE(line->sw_debounced)) { 656 line->total_discard_seq++; 657 line->last_seqno = ts->seq; 658 mod_delayed_work(system_wq, &line->work, 659 usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us))); 660 } else { 661 if (unlikely(ts->seq < line->line_seqno)) 662 return HTE_CB_HANDLED; 663 664 diff_seqno = ts->seq - line->line_seqno; 665 line->line_seqno = ts->seq; 666 if (lr->num_lines != 1) 667 line->req_seqno = atomic_add_return(diff_seqno, 668 &lr->seqno); 669 670 return HTE_RUN_SECOND_CB; 671 } 672 673 return HTE_CB_HANDLED; 674 } 675 676 static int hte_edge_setup(struct line *line, u64 eflags) 677 { 678 int ret; 679 unsigned long flags = 0; 680 struct hte_ts_desc *hdesc = &line->hdesc; 681 682 if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) 683 flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 684 HTE_FALLING_EDGE_TS : 685 HTE_RISING_EDGE_TS; 686 if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) 687 flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 688 HTE_RISING_EDGE_TS : 689 HTE_FALLING_EDGE_TS; 690 691 line->total_discard_seq = 0; 692 693 hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags, NULL, 694 line->desc); 695 696 ret = hte_ts_get(NULL, hdesc, 0); 697 if (ret) 698 return ret; 699 700 return hte_request_ts_ns(hdesc, process_hw_ts, process_hw_ts_thread, 701 line); 702 } 703 704 #else 705 706 static int hte_edge_setup(struct line *line, u64 eflags) 707 { 708 return 0; 709 } 710 #endif /* CONFIG_HTE */ 711 712 static irqreturn_t edge_irq_thread(int irq, void *p) 713 { 714 struct line *line = p; 715 struct linereq *lr = line->req; 716 struct gpio_v2_line_event le; 717 718 /* Do not leak kernel stack to userspace */ 719 memset(&le, 0, sizeof(le)); 720 721 if (line->timestamp_ns) { 722 le.timestamp_ns = line->timestamp_ns; 723 } else { 724 /* 725 * We may be running from a nested threaded interrupt in 726 * which case we didn't get the timestamp from 727 * edge_irq_handler(). 728 */ 729 le.timestamp_ns = line_event_timestamp(line); 730 if (lr->num_lines != 1) 731 line->req_seqno = atomic_inc_return(&lr->seqno); 732 } 733 line->timestamp_ns = 0; 734 735 switch (READ_ONCE(line->edflags) & GPIO_V2_LINE_EDGE_FLAGS) { 736 case GPIO_V2_LINE_FLAG_EDGE_BOTH: 737 le.id = line_event_id(gpiod_get_value_cansleep(line->desc)); 738 break; 739 case GPIO_V2_LINE_FLAG_EDGE_RISING: 740 le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; 741 break; 742 case GPIO_V2_LINE_FLAG_EDGE_FALLING: 743 le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; 744 break; 745 default: 746 return IRQ_NONE; 747 } 748 line->line_seqno++; 749 le.line_seqno = line->line_seqno; 750 le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno; 751 le.offset = gpio_chip_hwgpio(line->desc); 752 753 linereq_put_event(lr, &le); 754 755 return IRQ_HANDLED; 756 } 757 758 static irqreturn_t edge_irq_handler(int irq, void *p) 759 { 760 struct line *line = p; 761 struct linereq *lr = line->req; 762 763 /* 764 * Just store the timestamp in hardirq context so we get it as 765 * close in time as possible to the actual event. 766 */ 767 line->timestamp_ns = line_event_timestamp(line); 768 769 if (lr->num_lines != 1) 770 line->req_seqno = atomic_inc_return(&lr->seqno); 771 772 return IRQ_WAKE_THREAD; 773 } 774 775 /* 776 * returns the current debounced logical value. 777 */ 778 static bool debounced_value(struct line *line) 779 { 780 bool value; 781 782 /* 783 * minor race - debouncer may be stopped here, so edge_detector_stop() 784 * must leave the value unchanged so the following will read the level 785 * from when the debouncer was last running. 786 */ 787 value = READ_ONCE(line->level); 788 789 if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags)) 790 value = !value; 791 792 return value; 793 } 794 795 static irqreturn_t debounce_irq_handler(int irq, void *p) 796 { 797 struct line *line = p; 798 799 mod_delayed_work(system_wq, &line->work, 800 usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us))); 801 802 return IRQ_HANDLED; 803 } 804 805 static void debounce_work_func(struct work_struct *work) 806 { 807 struct gpio_v2_line_event le; 808 struct line *line = container_of(work, struct line, work.work); 809 struct linereq *lr; 810 u64 eflags, edflags = READ_ONCE(line->edflags); 811 int level = -1; 812 #ifdef CONFIG_HTE 813 int diff_seqno; 814 815 if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) 816 level = line->raw_level; 817 #endif 818 if (level < 0) 819 level = gpiod_get_raw_value_cansleep(line->desc); 820 if (level < 0) { 821 pr_debug_ratelimited("debouncer failed to read line value\n"); 822 return; 823 } 824 825 if (READ_ONCE(line->level) == level) 826 return; 827 828 WRITE_ONCE(line->level, level); 829 830 /* -- edge detection -- */ 831 eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS; 832 if (!eflags) 833 return; 834 835 /* switch from physical level to logical - if they differ */ 836 if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 837 level = !level; 838 839 /* ignore edges that are not being monitored */ 840 if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) || 841 ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level)) 842 return; 843 844 /* Do not leak kernel stack to userspace */ 845 memset(&le, 0, sizeof(le)); 846 847 lr = line->req; 848 le.timestamp_ns = line_event_timestamp(line); 849 le.offset = gpio_chip_hwgpio(line->desc); 850 #ifdef CONFIG_HTE 851 if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) { 852 /* discard events except the last one */ 853 line->total_discard_seq -= 1; 854 diff_seqno = line->last_seqno - line->total_discard_seq - 855 line->line_seqno; 856 line->line_seqno = line->last_seqno - line->total_discard_seq; 857 le.line_seqno = line->line_seqno; 858 le.seqno = (lr->num_lines == 1) ? 859 le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno); 860 } else 861 #endif /* CONFIG_HTE */ 862 { 863 line->line_seqno++; 864 le.line_seqno = line->line_seqno; 865 le.seqno = (lr->num_lines == 1) ? 866 le.line_seqno : atomic_inc_return(&lr->seqno); 867 } 868 869 le.id = line_event_id(level); 870 871 linereq_put_event(lr, &le); 872 } 873 874 static int debounce_setup(struct line *line, unsigned int debounce_period_us) 875 { 876 unsigned long irqflags; 877 int ret, level, irq; 878 879 /* try hardware */ 880 ret = gpiod_set_debounce(line->desc, debounce_period_us); 881 if (!ret) { 882 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 883 return ret; 884 } 885 if (ret != -ENOTSUPP) 886 return ret; 887 888 if (debounce_period_us) { 889 /* setup software debounce */ 890 level = gpiod_get_raw_value_cansleep(line->desc); 891 if (level < 0) 892 return level; 893 894 if (!(IS_ENABLED(CONFIG_HTE) && 895 test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))) { 896 irq = gpiod_to_irq(line->desc); 897 if (irq < 0) 898 return -ENXIO; 899 900 irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING; 901 ret = request_irq(irq, debounce_irq_handler, irqflags, 902 line->req->label, line); 903 if (ret) 904 return ret; 905 line->irq = irq; 906 } else { 907 ret = hte_edge_setup(line, GPIO_V2_LINE_FLAG_EDGE_BOTH); 908 if (ret) 909 return ret; 910 } 911 912 WRITE_ONCE(line->level, level); 913 WRITE_ONCE(line->sw_debounced, 1); 914 } 915 return 0; 916 } 917 918 static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc, 919 unsigned int line_idx) 920 { 921 unsigned int i; 922 u64 mask = BIT_ULL(line_idx); 923 924 for (i = 0; i < lc->num_attrs; i++) { 925 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && 926 (lc->attrs[i].mask & mask)) 927 return true; 928 } 929 return false; 930 } 931 932 static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc, 933 unsigned int line_idx) 934 { 935 unsigned int i; 936 u64 mask = BIT_ULL(line_idx); 937 938 for (i = 0; i < lc->num_attrs; i++) { 939 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) && 940 (lc->attrs[i].mask & mask)) 941 return lc->attrs[i].attr.debounce_period_us; 942 } 943 return 0; 944 } 945 946 static void edge_detector_stop(struct line *line) 947 { 948 if (line->irq) { 949 free_irq(line->irq, line); 950 line->irq = 0; 951 } 952 953 #ifdef CONFIG_HTE 954 if (READ_ONCE(line->edflags) & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) 955 hte_ts_put(&line->hdesc); 956 #endif 957 958 cancel_delayed_work_sync(&line->work); 959 WRITE_ONCE(line->sw_debounced, 0); 960 WRITE_ONCE(line->edflags, 0); 961 if (line->desc) 962 WRITE_ONCE(line->desc->debounce_period_us, 0); 963 /* do not change line->level - see comment in debounced_value() */ 964 } 965 966 static int edge_detector_setup(struct line *line, 967 struct gpio_v2_line_config *lc, 968 unsigned int line_idx, u64 edflags) 969 { 970 u32 debounce_period_us; 971 unsigned long irqflags = 0; 972 u64 eflags; 973 int irq, ret; 974 975 eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS; 976 if (eflags && !kfifo_initialized(&line->req->events)) { 977 ret = kfifo_alloc(&line->req->events, 978 line->req->event_buffer_size, GFP_KERNEL); 979 if (ret) 980 return ret; 981 } 982 if (gpio_v2_line_config_debounced(lc, line_idx)) { 983 debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx); 984 ret = debounce_setup(line, debounce_period_us); 985 if (ret) 986 return ret; 987 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 988 } 989 990 /* detection disabled or sw debouncer will provide edge detection */ 991 if (!eflags || READ_ONCE(line->sw_debounced)) 992 return 0; 993 994 if (IS_ENABLED(CONFIG_HTE) && 995 (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)) 996 return hte_edge_setup(line, edflags); 997 998 irq = gpiod_to_irq(line->desc); 999 if (irq < 0) 1000 return -ENXIO; 1001 1002 if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) 1003 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 1004 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 1005 if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) 1006 irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? 1007 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 1008 irqflags |= IRQF_ONESHOT; 1009 1010 /* Request a thread to read the events */ 1011 ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread, 1012 irqflags, line->req->label, line); 1013 if (ret) 1014 return ret; 1015 1016 line->irq = irq; 1017 return 0; 1018 } 1019 1020 static int edge_detector_update(struct line *line, 1021 struct gpio_v2_line_config *lc, 1022 unsigned int line_idx, u64 edflags) 1023 { 1024 u64 active_edflags = READ_ONCE(line->edflags); 1025 unsigned int debounce_period_us = 1026 gpio_v2_line_config_debounce_period(lc, line_idx); 1027 1028 if ((active_edflags == edflags) && 1029 (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us)) 1030 return 0; 1031 1032 /* sw debounced and still will be...*/ 1033 if (debounce_period_us && READ_ONCE(line->sw_debounced)) { 1034 WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 1035 return 0; 1036 } 1037 1038 /* reconfiguring edge detection or sw debounce being disabled */ 1039 if ((line->irq && !READ_ONCE(line->sw_debounced)) || 1040 (active_edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) || 1041 (!debounce_period_us && READ_ONCE(line->sw_debounced))) 1042 edge_detector_stop(line); 1043 1044 return edge_detector_setup(line, lc, line_idx, edflags); 1045 } 1046 1047 static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, 1048 unsigned int line_idx) 1049 { 1050 unsigned int i; 1051 u64 mask = BIT_ULL(line_idx); 1052 1053 for (i = 0; i < lc->num_attrs; i++) { 1054 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) && 1055 (lc->attrs[i].mask & mask)) 1056 return lc->attrs[i].attr.flags; 1057 } 1058 return lc->flags; 1059 } 1060 1061 static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc, 1062 unsigned int line_idx) 1063 { 1064 unsigned int i; 1065 u64 mask = BIT_ULL(line_idx); 1066 1067 for (i = 0; i < lc->num_attrs; i++) { 1068 if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) && 1069 (lc->attrs[i].mask & mask)) 1070 return !!(lc->attrs[i].attr.values & mask); 1071 } 1072 return 0; 1073 } 1074 1075 static int gpio_v2_line_flags_validate(u64 flags) 1076 { 1077 /* Return an error if an unknown flag is set */ 1078 if (flags & ~GPIO_V2_LINE_VALID_FLAGS) 1079 return -EINVAL; 1080 1081 if (!IS_ENABLED(CONFIG_HTE) && 1082 (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)) 1083 return -EOPNOTSUPP; 1084 1085 /* 1086 * Do not allow both INPUT and OUTPUT flags to be set as they are 1087 * contradictory. 1088 */ 1089 if ((flags & GPIO_V2_LINE_FLAG_INPUT) && 1090 (flags & GPIO_V2_LINE_FLAG_OUTPUT)) 1091 return -EINVAL; 1092 1093 /* Only allow one event clock source */ 1094 if (IS_ENABLED(CONFIG_HTE) && 1095 (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) && 1096 (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)) 1097 return -EINVAL; 1098 1099 /* Edge detection requires explicit input. */ 1100 if ((flags & GPIO_V2_LINE_EDGE_FLAGS) && 1101 !(flags & GPIO_V2_LINE_FLAG_INPUT)) 1102 return -EINVAL; 1103 1104 /* 1105 * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single 1106 * request. If the hardware actually supports enabling both at the 1107 * same time the electrical result would be disastrous. 1108 */ 1109 if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) && 1110 (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE)) 1111 return -EINVAL; 1112 1113 /* Drive requires explicit output direction. */ 1114 if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) && 1115 !(flags & GPIO_V2_LINE_FLAG_OUTPUT)) 1116 return -EINVAL; 1117 1118 /* Bias requires explicit direction. */ 1119 if ((flags & GPIO_V2_LINE_BIAS_FLAGS) && 1120 !(flags & GPIO_V2_LINE_DIRECTION_FLAGS)) 1121 return -EINVAL; 1122 1123 /* Only one bias flag can be set. */ 1124 if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) && 1125 (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | 1126 GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) || 1127 ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) && 1128 (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) 1129 return -EINVAL; 1130 1131 return 0; 1132 } 1133 1134 static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, 1135 unsigned int num_lines) 1136 { 1137 unsigned int i; 1138 u64 flags; 1139 int ret; 1140 1141 if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX) 1142 return -EINVAL; 1143 1144 if (memchr_inv(lc->padding, 0, sizeof(lc->padding))) 1145 return -EINVAL; 1146 1147 for (i = 0; i < num_lines; i++) { 1148 flags = gpio_v2_line_config_flags(lc, i); 1149 ret = gpio_v2_line_flags_validate(flags); 1150 if (ret) 1151 return ret; 1152 1153 /* debounce requires explicit input */ 1154 if (gpio_v2_line_config_debounced(lc, i) && 1155 !(flags & GPIO_V2_LINE_FLAG_INPUT)) 1156 return -EINVAL; 1157 } 1158 return 0; 1159 } 1160 1161 static void gpio_v2_line_config_flags_to_desc_flags(u64 flags, 1162 unsigned long *flagsp) 1163 { 1164 assign_bit(FLAG_ACTIVE_LOW, flagsp, 1165 flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW); 1166 1167 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) 1168 set_bit(FLAG_IS_OUT, flagsp); 1169 else if (flags & GPIO_V2_LINE_FLAG_INPUT) 1170 clear_bit(FLAG_IS_OUT, flagsp); 1171 1172 assign_bit(FLAG_EDGE_RISING, flagsp, 1173 flags & GPIO_V2_LINE_FLAG_EDGE_RISING); 1174 assign_bit(FLAG_EDGE_FALLING, flagsp, 1175 flags & GPIO_V2_LINE_FLAG_EDGE_FALLING); 1176 1177 assign_bit(FLAG_OPEN_DRAIN, flagsp, 1178 flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN); 1179 assign_bit(FLAG_OPEN_SOURCE, flagsp, 1180 flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE); 1181 1182 assign_bit(FLAG_PULL_UP, flagsp, 1183 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP); 1184 assign_bit(FLAG_PULL_DOWN, flagsp, 1185 flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN); 1186 assign_bit(FLAG_BIAS_DISABLE, flagsp, 1187 flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED); 1188 1189 assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp, 1190 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME); 1191 assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp, 1192 flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE); 1193 } 1194 1195 static long linereq_get_values(struct linereq *lr, void __user *ip) 1196 { 1197 struct gpio_v2_line_values lv; 1198 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); 1199 struct gpio_desc **descs; 1200 unsigned int i, didx, num_get; 1201 bool val; 1202 int ret; 1203 1204 /* NOTE: It's ok to read values of output lines. */ 1205 if (copy_from_user(&lv, ip, sizeof(lv))) 1206 return -EFAULT; 1207 1208 for (num_get = 0, i = 0; i < lr->num_lines; i++) { 1209 if (lv.mask & BIT_ULL(i)) { 1210 num_get++; 1211 descs = &lr->lines[i].desc; 1212 } 1213 } 1214 1215 if (num_get == 0) 1216 return -EINVAL; 1217 1218 if (num_get != 1) { 1219 descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL); 1220 if (!descs) 1221 return -ENOMEM; 1222 for (didx = 0, i = 0; i < lr->num_lines; i++) { 1223 if (lv.mask & BIT_ULL(i)) { 1224 descs[didx] = lr->lines[i].desc; 1225 didx++; 1226 } 1227 } 1228 } 1229 ret = gpiod_get_array_value_complex(false, true, num_get, 1230 descs, NULL, vals); 1231 1232 if (num_get != 1) 1233 kfree(descs); 1234 if (ret) 1235 return ret; 1236 1237 lv.bits = 0; 1238 for (didx = 0, i = 0; i < lr->num_lines; i++) { 1239 if (lv.mask & BIT_ULL(i)) { 1240 if (lr->lines[i].sw_debounced) 1241 val = debounced_value(&lr->lines[i]); 1242 else 1243 val = test_bit(didx, vals); 1244 if (val) 1245 lv.bits |= BIT_ULL(i); 1246 didx++; 1247 } 1248 } 1249 1250 if (copy_to_user(ip, &lv, sizeof(lv))) 1251 return -EFAULT; 1252 1253 return 0; 1254 } 1255 1256 static long linereq_set_values_unlocked(struct linereq *lr, 1257 struct gpio_v2_line_values *lv) 1258 { 1259 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); 1260 struct gpio_desc **descs; 1261 unsigned int i, didx, num_set; 1262 int ret; 1263 1264 bitmap_zero(vals, GPIO_V2_LINES_MAX); 1265 for (num_set = 0, i = 0; i < lr->num_lines; i++) { 1266 if (lv->mask & BIT_ULL(i)) { 1267 if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags)) 1268 return -EPERM; 1269 if (lv->bits & BIT_ULL(i)) 1270 __set_bit(num_set, vals); 1271 num_set++; 1272 descs = &lr->lines[i].desc; 1273 } 1274 } 1275 if (num_set == 0) 1276 return -EINVAL; 1277 1278 if (num_set != 1) { 1279 /* build compacted desc array and values */ 1280 descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL); 1281 if (!descs) 1282 return -ENOMEM; 1283 for (didx = 0, i = 0; i < lr->num_lines; i++) { 1284 if (lv->mask & BIT_ULL(i)) { 1285 descs[didx] = lr->lines[i].desc; 1286 didx++; 1287 } 1288 } 1289 } 1290 ret = gpiod_set_array_value_complex(false, true, num_set, 1291 descs, NULL, vals); 1292 1293 if (num_set != 1) 1294 kfree(descs); 1295 return ret; 1296 } 1297 1298 static long linereq_set_values(struct linereq *lr, void __user *ip) 1299 { 1300 struct gpio_v2_line_values lv; 1301 int ret; 1302 1303 if (copy_from_user(&lv, ip, sizeof(lv))) 1304 return -EFAULT; 1305 1306 mutex_lock(&lr->config_mutex); 1307 1308 ret = linereq_set_values_unlocked(lr, &lv); 1309 1310 mutex_unlock(&lr->config_mutex); 1311 1312 return ret; 1313 } 1314 1315 static long linereq_set_config_unlocked(struct linereq *lr, 1316 struct gpio_v2_line_config *lc) 1317 { 1318 struct gpio_desc *desc; 1319 struct line *line; 1320 unsigned int i; 1321 u64 flags, edflags; 1322 int ret; 1323 1324 for (i = 0; i < lr->num_lines; i++) { 1325 line = &lr->lines[i]; 1326 desc = lr->lines[i].desc; 1327 flags = gpio_v2_line_config_flags(lc, i); 1328 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 1329 edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS; 1330 /* 1331 * Lines have to be requested explicitly for input 1332 * or output, else the line will be treated "as is". 1333 */ 1334 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 1335 int val = gpio_v2_line_config_output_value(lc, i); 1336 1337 edge_detector_stop(line); 1338 ret = gpiod_direction_output(desc, val); 1339 if (ret) 1340 return ret; 1341 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { 1342 ret = gpiod_direction_input(desc); 1343 if (ret) 1344 return ret; 1345 1346 ret = edge_detector_update(line, lc, i, edflags); 1347 if (ret) 1348 return ret; 1349 } 1350 1351 WRITE_ONCE(line->edflags, edflags); 1352 1353 blocking_notifier_call_chain(&desc->gdev->notifier, 1354 GPIO_V2_LINE_CHANGED_CONFIG, 1355 desc); 1356 } 1357 return 0; 1358 } 1359 1360 static long linereq_set_config(struct linereq *lr, void __user *ip) 1361 { 1362 struct gpio_v2_line_config lc; 1363 int ret; 1364 1365 if (copy_from_user(&lc, ip, sizeof(lc))) 1366 return -EFAULT; 1367 1368 ret = gpio_v2_line_config_validate(&lc, lr->num_lines); 1369 if (ret) 1370 return ret; 1371 1372 mutex_lock(&lr->config_mutex); 1373 1374 ret = linereq_set_config_unlocked(lr, &lc); 1375 1376 mutex_unlock(&lr->config_mutex); 1377 1378 return ret; 1379 } 1380 1381 static long linereq_ioctl(struct file *file, unsigned int cmd, 1382 unsigned long arg) 1383 { 1384 struct linereq *lr = file->private_data; 1385 void __user *ip = (void __user *)arg; 1386 1387 switch (cmd) { 1388 case GPIO_V2_LINE_GET_VALUES_IOCTL: 1389 return linereq_get_values(lr, ip); 1390 case GPIO_V2_LINE_SET_VALUES_IOCTL: 1391 return linereq_set_values(lr, ip); 1392 case GPIO_V2_LINE_SET_CONFIG_IOCTL: 1393 return linereq_set_config(lr, ip); 1394 default: 1395 return -EINVAL; 1396 } 1397 } 1398 1399 #ifdef CONFIG_COMPAT 1400 static long linereq_ioctl_compat(struct file *file, unsigned int cmd, 1401 unsigned long arg) 1402 { 1403 return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 1404 } 1405 #endif 1406 1407 static __poll_t linereq_poll(struct file *file, 1408 struct poll_table_struct *wait) 1409 { 1410 struct linereq *lr = file->private_data; 1411 __poll_t events = 0; 1412 1413 poll_wait(file, &lr->wait, wait); 1414 1415 if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events, 1416 &lr->wait.lock)) 1417 events = EPOLLIN | EPOLLRDNORM; 1418 1419 return events; 1420 } 1421 1422 static ssize_t linereq_read(struct file *file, 1423 char __user *buf, 1424 size_t count, 1425 loff_t *f_ps) 1426 { 1427 struct linereq *lr = file->private_data; 1428 struct gpio_v2_line_event le; 1429 ssize_t bytes_read = 0; 1430 int ret; 1431 1432 if (count < sizeof(le)) 1433 return -EINVAL; 1434 1435 do { 1436 spin_lock(&lr->wait.lock); 1437 if (kfifo_is_empty(&lr->events)) { 1438 if (bytes_read) { 1439 spin_unlock(&lr->wait.lock); 1440 return bytes_read; 1441 } 1442 1443 if (file->f_flags & O_NONBLOCK) { 1444 spin_unlock(&lr->wait.lock); 1445 return -EAGAIN; 1446 } 1447 1448 ret = wait_event_interruptible_locked(lr->wait, 1449 !kfifo_is_empty(&lr->events)); 1450 if (ret) { 1451 spin_unlock(&lr->wait.lock); 1452 return ret; 1453 } 1454 } 1455 1456 ret = kfifo_out(&lr->events, &le, 1); 1457 spin_unlock(&lr->wait.lock); 1458 if (ret != 1) { 1459 /* 1460 * This should never happen - we were holding the 1461 * lock from the moment we learned the fifo is no 1462 * longer empty until now. 1463 */ 1464 ret = -EIO; 1465 break; 1466 } 1467 1468 if (copy_to_user(buf + bytes_read, &le, sizeof(le))) 1469 return -EFAULT; 1470 bytes_read += sizeof(le); 1471 } while (count >= bytes_read + sizeof(le)); 1472 1473 return bytes_read; 1474 } 1475 1476 static void linereq_free(struct linereq *lr) 1477 { 1478 unsigned int i; 1479 1480 for (i = 0; i < lr->num_lines; i++) { 1481 if (lr->lines[i].desc) { 1482 edge_detector_stop(&lr->lines[i]); 1483 gpiod_free(lr->lines[i].desc); 1484 } 1485 } 1486 kfifo_free(&lr->events); 1487 kfree(lr->label); 1488 put_device(&lr->gdev->dev); 1489 kfree(lr); 1490 } 1491 1492 static int linereq_release(struct inode *inode, struct file *file) 1493 { 1494 struct linereq *lr = file->private_data; 1495 1496 linereq_free(lr); 1497 return 0; 1498 } 1499 1500 #ifdef CONFIG_PROC_FS 1501 static void linereq_show_fdinfo(struct seq_file *out, struct file *file) 1502 { 1503 struct linereq *lr = file->private_data; 1504 struct device *dev = &lr->gdev->dev; 1505 u16 i; 1506 1507 seq_printf(out, "gpio-chip:\t%s\n", dev_name(dev)); 1508 1509 for (i = 0; i < lr->num_lines; i++) 1510 seq_printf(out, "gpio-line:\t%d\n", 1511 gpio_chip_hwgpio(lr->lines[i].desc)); 1512 } 1513 #endif 1514 1515 static const struct file_operations line_fileops = { 1516 .release = linereq_release, 1517 .read = linereq_read, 1518 .poll = linereq_poll, 1519 .owner = THIS_MODULE, 1520 .llseek = noop_llseek, 1521 .unlocked_ioctl = linereq_ioctl, 1522 #ifdef CONFIG_COMPAT 1523 .compat_ioctl = linereq_ioctl_compat, 1524 #endif 1525 #ifdef CONFIG_PROC_FS 1526 .show_fdinfo = linereq_show_fdinfo, 1527 #endif 1528 }; 1529 1530 static int linereq_create(struct gpio_device *gdev, void __user *ip) 1531 { 1532 struct gpio_v2_line_request ulr; 1533 struct gpio_v2_line_config *lc; 1534 struct linereq *lr; 1535 struct file *file; 1536 u64 flags, edflags; 1537 unsigned int i; 1538 int fd, ret; 1539 1540 if (copy_from_user(&ulr, ip, sizeof(ulr))) 1541 return -EFAULT; 1542 1543 if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX)) 1544 return -EINVAL; 1545 1546 if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding))) 1547 return -EINVAL; 1548 1549 lc = &ulr.config; 1550 ret = gpio_v2_line_config_validate(lc, ulr.num_lines); 1551 if (ret) 1552 return ret; 1553 1554 lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); 1555 if (!lr) 1556 return -ENOMEM; 1557 1558 lr->gdev = gdev; 1559 get_device(&gdev->dev); 1560 1561 for (i = 0; i < ulr.num_lines; i++) { 1562 lr->lines[i].req = lr; 1563 WRITE_ONCE(lr->lines[i].sw_debounced, 0); 1564 INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func); 1565 } 1566 1567 if (ulr.consumer[0] != '\0') { 1568 /* label is only initialized if consumer is set */ 1569 lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1, 1570 GFP_KERNEL); 1571 if (!lr->label) { 1572 ret = -ENOMEM; 1573 goto out_free_linereq; 1574 } 1575 } 1576 1577 mutex_init(&lr->config_mutex); 1578 init_waitqueue_head(&lr->wait); 1579 lr->event_buffer_size = ulr.event_buffer_size; 1580 if (lr->event_buffer_size == 0) 1581 lr->event_buffer_size = ulr.num_lines * 16; 1582 else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16) 1583 lr->event_buffer_size = GPIO_V2_LINES_MAX * 16; 1584 1585 atomic_set(&lr->seqno, 0); 1586 lr->num_lines = ulr.num_lines; 1587 1588 /* Request each GPIO */ 1589 for (i = 0; i < ulr.num_lines; i++) { 1590 u32 offset = ulr.offsets[i]; 1591 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); 1592 1593 if (IS_ERR(desc)) { 1594 ret = PTR_ERR(desc); 1595 goto out_free_linereq; 1596 } 1597 1598 ret = gpiod_request_user(desc, lr->label); 1599 if (ret) 1600 goto out_free_linereq; 1601 1602 lr->lines[i].desc = desc; 1603 flags = gpio_v2_line_config_flags(lc, i); 1604 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 1605 1606 ret = gpiod_set_transitory(desc, false); 1607 if (ret < 0) 1608 goto out_free_linereq; 1609 1610 edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS; 1611 /* 1612 * Lines have to be requested explicitly for input 1613 * or output, else the line will be treated "as is". 1614 */ 1615 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 1616 int val = gpio_v2_line_config_output_value(lc, i); 1617 1618 ret = gpiod_direction_output(desc, val); 1619 if (ret) 1620 goto out_free_linereq; 1621 } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { 1622 ret = gpiod_direction_input(desc); 1623 if (ret) 1624 goto out_free_linereq; 1625 1626 ret = edge_detector_setup(&lr->lines[i], lc, i, 1627 edflags); 1628 if (ret) 1629 goto out_free_linereq; 1630 } 1631 1632 lr->lines[i].edflags = edflags; 1633 1634 blocking_notifier_call_chain(&desc->gdev->notifier, 1635 GPIO_V2_LINE_CHANGED_REQUESTED, desc); 1636 1637 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", 1638 offset); 1639 } 1640 1641 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 1642 if (fd < 0) { 1643 ret = fd; 1644 goto out_free_linereq; 1645 } 1646 1647 file = anon_inode_getfile("gpio-line", &line_fileops, lr, 1648 O_RDONLY | O_CLOEXEC); 1649 if (IS_ERR(file)) { 1650 ret = PTR_ERR(file); 1651 goto out_put_unused_fd; 1652 } 1653 1654 ulr.fd = fd; 1655 if (copy_to_user(ip, &ulr, sizeof(ulr))) { 1656 /* 1657 * fput() will trigger the release() callback, so do not go onto 1658 * the regular error cleanup path here. 1659 */ 1660 fput(file); 1661 put_unused_fd(fd); 1662 return -EFAULT; 1663 } 1664 1665 fd_install(fd, file); 1666 1667 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", 1668 lr->num_lines); 1669 1670 return 0; 1671 1672 out_put_unused_fd: 1673 put_unused_fd(fd); 1674 out_free_linereq: 1675 linereq_free(lr); 1676 return ret; 1677 } 1678 1679 #ifdef CONFIG_GPIO_CDEV_V1 1680 1681 /* 1682 * GPIO line event management 1683 */ 1684 1685 /** 1686 * struct lineevent_state - contains the state of a userspace event 1687 * @gdev: the GPIO device the event pertains to 1688 * @label: consumer label used to tag descriptors 1689 * @desc: the GPIO descriptor held by this event 1690 * @eflags: the event flags this line was requested with 1691 * @irq: the interrupt that trigger in response to events on this GPIO 1692 * @wait: wait queue that handles blocking reads of events 1693 * @events: KFIFO for the GPIO events 1694 * @timestamp: cache for the timestamp storing it between hardirq 1695 * and IRQ thread, used to bring the timestamp close to the actual 1696 * event 1697 */ 1698 struct lineevent_state { 1699 struct gpio_device *gdev; 1700 const char *label; 1701 struct gpio_desc *desc; 1702 u32 eflags; 1703 int irq; 1704 wait_queue_head_t wait; 1705 DECLARE_KFIFO(events, struct gpioevent_data, 16); 1706 u64 timestamp; 1707 }; 1708 1709 #define GPIOEVENT_REQUEST_VALID_FLAGS \ 1710 (GPIOEVENT_REQUEST_RISING_EDGE | \ 1711 GPIOEVENT_REQUEST_FALLING_EDGE) 1712 1713 static __poll_t lineevent_poll(struct file *file, 1714 struct poll_table_struct *wait) 1715 { 1716 struct lineevent_state *le = file->private_data; 1717 __poll_t events = 0; 1718 1719 poll_wait(file, &le->wait, wait); 1720 1721 if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock)) 1722 events = EPOLLIN | EPOLLRDNORM; 1723 1724 return events; 1725 } 1726 1727 struct compat_gpioeevent_data { 1728 compat_u64 timestamp; 1729 u32 id; 1730 }; 1731 1732 static ssize_t lineevent_read(struct file *file, 1733 char __user *buf, 1734 size_t count, 1735 loff_t *f_ps) 1736 { 1737 struct lineevent_state *le = file->private_data; 1738 struct gpioevent_data ge; 1739 ssize_t bytes_read = 0; 1740 ssize_t ge_size; 1741 int ret; 1742 1743 /* 1744 * When compatible system call is being used the struct gpioevent_data, 1745 * in case of at least ia32, has different size due to the alignment 1746 * differences. Because we have first member 64 bits followed by one of 1747 * 32 bits there is no gap between them. The only difference is the 1748 * padding at the end of the data structure. Hence, we calculate the 1749 * actual sizeof() and pass this as an argument to copy_to_user() to 1750 * drop unneeded bytes from the output. 1751 */ 1752 if (compat_need_64bit_alignment_fixup()) 1753 ge_size = sizeof(struct compat_gpioeevent_data); 1754 else 1755 ge_size = sizeof(struct gpioevent_data); 1756 if (count < ge_size) 1757 return -EINVAL; 1758 1759 do { 1760 spin_lock(&le->wait.lock); 1761 if (kfifo_is_empty(&le->events)) { 1762 if (bytes_read) { 1763 spin_unlock(&le->wait.lock); 1764 return bytes_read; 1765 } 1766 1767 if (file->f_flags & O_NONBLOCK) { 1768 spin_unlock(&le->wait.lock); 1769 return -EAGAIN; 1770 } 1771 1772 ret = wait_event_interruptible_locked(le->wait, 1773 !kfifo_is_empty(&le->events)); 1774 if (ret) { 1775 spin_unlock(&le->wait.lock); 1776 return ret; 1777 } 1778 } 1779 1780 ret = kfifo_out(&le->events, &ge, 1); 1781 spin_unlock(&le->wait.lock); 1782 if (ret != 1) { 1783 /* 1784 * This should never happen - we were holding the lock 1785 * from the moment we learned the fifo is no longer 1786 * empty until now. 1787 */ 1788 ret = -EIO; 1789 break; 1790 } 1791 1792 if (copy_to_user(buf + bytes_read, &ge, ge_size)) 1793 return -EFAULT; 1794 bytes_read += ge_size; 1795 } while (count >= bytes_read + ge_size); 1796 1797 return bytes_read; 1798 } 1799 1800 static void lineevent_free(struct lineevent_state *le) 1801 { 1802 if (le->irq) 1803 free_irq(le->irq, le); 1804 if (le->desc) 1805 gpiod_free(le->desc); 1806 kfree(le->label); 1807 put_device(&le->gdev->dev); 1808 kfree(le); 1809 } 1810 1811 static int lineevent_release(struct inode *inode, struct file *file) 1812 { 1813 lineevent_free(file->private_data); 1814 return 0; 1815 } 1816 1817 static long lineevent_ioctl(struct file *file, unsigned int cmd, 1818 unsigned long arg) 1819 { 1820 struct lineevent_state *le = file->private_data; 1821 void __user *ip = (void __user *)arg; 1822 struct gpiohandle_data ghd; 1823 1824 /* 1825 * We can get the value for an event line but not set it, 1826 * because it is input by definition. 1827 */ 1828 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { 1829 int val; 1830 1831 memset(&ghd, 0, sizeof(ghd)); 1832 1833 val = gpiod_get_value_cansleep(le->desc); 1834 if (val < 0) 1835 return val; 1836 ghd.values[0] = val; 1837 1838 if (copy_to_user(ip, &ghd, sizeof(ghd))) 1839 return -EFAULT; 1840 1841 return 0; 1842 } 1843 return -EINVAL; 1844 } 1845 1846 #ifdef CONFIG_COMPAT 1847 static long lineevent_ioctl_compat(struct file *file, unsigned int cmd, 1848 unsigned long arg) 1849 { 1850 return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 1851 } 1852 #endif 1853 1854 static const struct file_operations lineevent_fileops = { 1855 .release = lineevent_release, 1856 .read = lineevent_read, 1857 .poll = lineevent_poll, 1858 .owner = THIS_MODULE, 1859 .llseek = noop_llseek, 1860 .unlocked_ioctl = lineevent_ioctl, 1861 #ifdef CONFIG_COMPAT 1862 .compat_ioctl = lineevent_ioctl_compat, 1863 #endif 1864 }; 1865 1866 static irqreturn_t lineevent_irq_thread(int irq, void *p) 1867 { 1868 struct lineevent_state *le = p; 1869 struct gpioevent_data ge; 1870 int ret; 1871 1872 /* Do not leak kernel stack to userspace */ 1873 memset(&ge, 0, sizeof(ge)); 1874 1875 /* 1876 * We may be running from a nested threaded interrupt in which case 1877 * we didn't get the timestamp from lineevent_irq_handler(). 1878 */ 1879 if (!le->timestamp) 1880 ge.timestamp = ktime_get_ns(); 1881 else 1882 ge.timestamp = le->timestamp; 1883 1884 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE 1885 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { 1886 int level = gpiod_get_value_cansleep(le->desc); 1887 1888 if (level) 1889 /* Emit low-to-high event */ 1890 ge.id = GPIOEVENT_EVENT_RISING_EDGE; 1891 else 1892 /* Emit high-to-low event */ 1893 ge.id = GPIOEVENT_EVENT_FALLING_EDGE; 1894 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) { 1895 /* Emit low-to-high event */ 1896 ge.id = GPIOEVENT_EVENT_RISING_EDGE; 1897 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { 1898 /* Emit high-to-low event */ 1899 ge.id = GPIOEVENT_EVENT_FALLING_EDGE; 1900 } else { 1901 return IRQ_NONE; 1902 } 1903 1904 ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge, 1905 1, &le->wait.lock); 1906 if (ret) 1907 wake_up_poll(&le->wait, EPOLLIN); 1908 else 1909 pr_debug_ratelimited("event FIFO is full - event dropped\n"); 1910 1911 return IRQ_HANDLED; 1912 } 1913 1914 static irqreturn_t lineevent_irq_handler(int irq, void *p) 1915 { 1916 struct lineevent_state *le = p; 1917 1918 /* 1919 * Just store the timestamp in hardirq context so we get it as 1920 * close in time as possible to the actual event. 1921 */ 1922 le->timestamp = ktime_get_ns(); 1923 1924 return IRQ_WAKE_THREAD; 1925 } 1926 1927 static int lineevent_create(struct gpio_device *gdev, void __user *ip) 1928 { 1929 struct gpioevent_request eventreq; 1930 struct lineevent_state *le; 1931 struct gpio_desc *desc; 1932 struct file *file; 1933 u32 offset; 1934 u32 lflags; 1935 u32 eflags; 1936 int fd; 1937 int ret; 1938 int irq, irqflags = 0; 1939 1940 if (copy_from_user(&eventreq, ip, sizeof(eventreq))) 1941 return -EFAULT; 1942 1943 offset = eventreq.lineoffset; 1944 lflags = eventreq.handleflags; 1945 eflags = eventreq.eventflags; 1946 1947 desc = gpiochip_get_desc(gdev->chip, offset); 1948 if (IS_ERR(desc)) 1949 return PTR_ERR(desc); 1950 1951 /* Return an error if a unknown flag is set */ 1952 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || 1953 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) 1954 return -EINVAL; 1955 1956 /* This is just wrong: we don't look for events on output lines */ 1957 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) || 1958 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || 1959 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) 1960 return -EINVAL; 1961 1962 /* Only one bias flag can be set. */ 1963 if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && 1964 (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | 1965 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || 1966 ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && 1967 (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) 1968 return -EINVAL; 1969 1970 le = kzalloc(sizeof(*le), GFP_KERNEL); 1971 if (!le) 1972 return -ENOMEM; 1973 le->gdev = gdev; 1974 get_device(&gdev->dev); 1975 1976 if (eventreq.consumer_label[0] != '\0') { 1977 /* label is only initialized if consumer_label is set */ 1978 le->label = kstrndup(eventreq.consumer_label, 1979 sizeof(eventreq.consumer_label) - 1, 1980 GFP_KERNEL); 1981 if (!le->label) { 1982 ret = -ENOMEM; 1983 goto out_free_le; 1984 } 1985 } 1986 1987 ret = gpiod_request_user(desc, le->label); 1988 if (ret) 1989 goto out_free_le; 1990 le->desc = desc; 1991 le->eflags = eflags; 1992 1993 linehandle_flags_to_desc_flags(lflags, &desc->flags); 1994 1995 ret = gpiod_direction_input(desc); 1996 if (ret) 1997 goto out_free_le; 1998 1999 blocking_notifier_call_chain(&desc->gdev->notifier, 2000 GPIO_V2_LINE_CHANGED_REQUESTED, desc); 2001 2002 irq = gpiod_to_irq(desc); 2003 if (irq <= 0) { 2004 ret = -ENODEV; 2005 goto out_free_le; 2006 } 2007 le->irq = irq; 2008 2009 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE) 2010 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 2011 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 2012 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE) 2013 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 2014 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 2015 irqflags |= IRQF_ONESHOT; 2016 2017 INIT_KFIFO(le->events); 2018 init_waitqueue_head(&le->wait); 2019 2020 /* Request a thread to read the events */ 2021 ret = request_threaded_irq(le->irq, 2022 lineevent_irq_handler, 2023 lineevent_irq_thread, 2024 irqflags, 2025 le->label, 2026 le); 2027 if (ret) 2028 goto out_free_le; 2029 2030 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); 2031 if (fd < 0) { 2032 ret = fd; 2033 goto out_free_le; 2034 } 2035 2036 file = anon_inode_getfile("gpio-event", 2037 &lineevent_fileops, 2038 le, 2039 O_RDONLY | O_CLOEXEC); 2040 if (IS_ERR(file)) { 2041 ret = PTR_ERR(file); 2042 goto out_put_unused_fd; 2043 } 2044 2045 eventreq.fd = fd; 2046 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) { 2047 /* 2048 * fput() will trigger the release() callback, so do not go onto 2049 * the regular error cleanup path here. 2050 */ 2051 fput(file); 2052 put_unused_fd(fd); 2053 return -EFAULT; 2054 } 2055 2056 fd_install(fd, file); 2057 2058 return 0; 2059 2060 out_put_unused_fd: 2061 put_unused_fd(fd); 2062 out_free_le: 2063 lineevent_free(le); 2064 return ret; 2065 } 2066 2067 static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2, 2068 struct gpioline_info *info_v1) 2069 { 2070 u64 flagsv2 = info_v2->flags; 2071 2072 memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name)); 2073 memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer)); 2074 info_v1->line_offset = info_v2->offset; 2075 info_v1->flags = 0; 2076 2077 if (flagsv2 & GPIO_V2_LINE_FLAG_USED) 2078 info_v1->flags |= GPIOLINE_FLAG_KERNEL; 2079 2080 if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT) 2081 info_v1->flags |= GPIOLINE_FLAG_IS_OUT; 2082 2083 if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW) 2084 info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW; 2085 2086 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN) 2087 info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN; 2088 if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE) 2089 info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE; 2090 2091 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP) 2092 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; 2093 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) 2094 info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; 2095 if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED) 2096 info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE; 2097 } 2098 2099 static void gpio_v2_line_info_changed_to_v1( 2100 struct gpio_v2_line_info_changed *lic_v2, 2101 struct gpioline_info_changed *lic_v1) 2102 { 2103 memset(lic_v1, 0, sizeof(*lic_v1)); 2104 gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info); 2105 lic_v1->timestamp = lic_v2->timestamp_ns; 2106 lic_v1->event_type = lic_v2->event_type; 2107 } 2108 2109 #endif /* CONFIG_GPIO_CDEV_V1 */ 2110 2111 static void gpio_desc_to_lineinfo(struct gpio_desc *desc, 2112 struct gpio_v2_line_info *info) 2113 { 2114 struct gpio_chip *gc = desc->gdev->chip; 2115 bool ok_for_pinctrl; 2116 unsigned long flags; 2117 u32 debounce_period_us; 2118 unsigned int num_attrs = 0; 2119 2120 memset(info, 0, sizeof(*info)); 2121 info->offset = gpio_chip_hwgpio(desc); 2122 2123 /* 2124 * This function takes a mutex so we must check this before taking 2125 * the spinlock. 2126 * 2127 * FIXME: find a non-racy way to retrieve this information. Maybe a 2128 * lock common to both frameworks? 2129 */ 2130 ok_for_pinctrl = 2131 pinctrl_gpio_can_use_line(gc->base + info->offset); 2132 2133 spin_lock_irqsave(&gpio_lock, flags); 2134 2135 if (desc->name) 2136 strscpy(info->name, desc->name, sizeof(info->name)); 2137 2138 if (desc->label) 2139 strscpy(info->consumer, desc->label, sizeof(info->consumer)); 2140 2141 /* 2142 * Userspace only need to know that the kernel is using this GPIO so 2143 * it can't use it. 2144 */ 2145 info->flags = 0; 2146 if (test_bit(FLAG_REQUESTED, &desc->flags) || 2147 test_bit(FLAG_IS_HOGGED, &desc->flags) || 2148 test_bit(FLAG_USED_AS_IRQ, &desc->flags) || 2149 test_bit(FLAG_EXPORT, &desc->flags) || 2150 test_bit(FLAG_SYSFS, &desc->flags) || 2151 !gpiochip_line_is_valid(gc, info->offset) || 2152 !ok_for_pinctrl) 2153 info->flags |= GPIO_V2_LINE_FLAG_USED; 2154 2155 if (test_bit(FLAG_IS_OUT, &desc->flags)) 2156 info->flags |= GPIO_V2_LINE_FLAG_OUTPUT; 2157 else 2158 info->flags |= GPIO_V2_LINE_FLAG_INPUT; 2159 2160 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 2161 info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW; 2162 2163 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 2164 info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; 2165 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 2166 info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; 2167 2168 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) 2169 info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED; 2170 if (test_bit(FLAG_PULL_DOWN, &desc->flags)) 2171 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN; 2172 if (test_bit(FLAG_PULL_UP, &desc->flags)) 2173 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; 2174 2175 if (test_bit(FLAG_EDGE_RISING, &desc->flags)) 2176 info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; 2177 if (test_bit(FLAG_EDGE_FALLING, &desc->flags)) 2178 info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; 2179 2180 if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags)) 2181 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME; 2182 else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags)) 2183 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE; 2184 2185 debounce_period_us = READ_ONCE(desc->debounce_period_us); 2186 if (debounce_period_us) { 2187 info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; 2188 info->attrs[num_attrs].debounce_period_us = debounce_period_us; 2189 num_attrs++; 2190 } 2191 info->num_attrs = num_attrs; 2192 2193 spin_unlock_irqrestore(&gpio_lock, flags); 2194 } 2195 2196 struct gpio_chardev_data { 2197 struct gpio_device *gdev; 2198 wait_queue_head_t wait; 2199 DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32); 2200 struct notifier_block lineinfo_changed_nb; 2201 unsigned long *watched_lines; 2202 #ifdef CONFIG_GPIO_CDEV_V1 2203 atomic_t watch_abi_version; 2204 #endif 2205 }; 2206 2207 static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip) 2208 { 2209 struct gpio_device *gdev = cdev->gdev; 2210 struct gpiochip_info chipinfo; 2211 2212 memset(&chipinfo, 0, sizeof(chipinfo)); 2213 2214 strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name)); 2215 strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label)); 2216 chipinfo.lines = gdev->ngpio; 2217 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) 2218 return -EFAULT; 2219 return 0; 2220 } 2221 2222 #ifdef CONFIG_GPIO_CDEV_V1 2223 /* 2224 * returns 0 if the versions match, else the previously selected ABI version 2225 */ 2226 static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata, 2227 unsigned int version) 2228 { 2229 int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version); 2230 2231 if (abiv == version) 2232 return 0; 2233 2234 return abiv; 2235 } 2236 2237 static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip, 2238 bool watch) 2239 { 2240 struct gpio_desc *desc; 2241 struct gpioline_info lineinfo; 2242 struct gpio_v2_line_info lineinfo_v2; 2243 2244 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 2245 return -EFAULT; 2246 2247 /* this doubles as a range check on line_offset */ 2248 desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset); 2249 if (IS_ERR(desc)) 2250 return PTR_ERR(desc); 2251 2252 if (watch) { 2253 if (lineinfo_ensure_abi_version(cdev, 1)) 2254 return -EPERM; 2255 2256 if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines)) 2257 return -EBUSY; 2258 } 2259 2260 gpio_desc_to_lineinfo(desc, &lineinfo_v2); 2261 gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); 2262 2263 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 2264 if (watch) 2265 clear_bit(lineinfo.line_offset, cdev->watched_lines); 2266 return -EFAULT; 2267 } 2268 2269 return 0; 2270 } 2271 #endif 2272 2273 static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip, 2274 bool watch) 2275 { 2276 struct gpio_desc *desc; 2277 struct gpio_v2_line_info lineinfo; 2278 2279 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) 2280 return -EFAULT; 2281 2282 if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding))) 2283 return -EINVAL; 2284 2285 desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset); 2286 if (IS_ERR(desc)) 2287 return PTR_ERR(desc); 2288 2289 if (watch) { 2290 #ifdef CONFIG_GPIO_CDEV_V1 2291 if (lineinfo_ensure_abi_version(cdev, 2)) 2292 return -EPERM; 2293 #endif 2294 if (test_and_set_bit(lineinfo.offset, cdev->watched_lines)) 2295 return -EBUSY; 2296 } 2297 gpio_desc_to_lineinfo(desc, &lineinfo); 2298 2299 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 2300 if (watch) 2301 clear_bit(lineinfo.offset, cdev->watched_lines); 2302 return -EFAULT; 2303 } 2304 2305 return 0; 2306 } 2307 2308 static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip) 2309 { 2310 __u32 offset; 2311 2312 if (copy_from_user(&offset, ip, sizeof(offset))) 2313 return -EFAULT; 2314 2315 if (offset >= cdev->gdev->ngpio) 2316 return -EINVAL; 2317 2318 if (!test_and_clear_bit(offset, cdev->watched_lines)) 2319 return -EBUSY; 2320 2321 return 0; 2322 } 2323 2324 /* 2325 * gpio_ioctl() - ioctl handler for the GPIO chardev 2326 */ 2327 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 2328 { 2329 struct gpio_chardev_data *cdev = file->private_data; 2330 struct gpio_device *gdev = cdev->gdev; 2331 void __user *ip = (void __user *)arg; 2332 2333 /* We fail any subsequent ioctl():s when the chip is gone */ 2334 if (!gdev->chip) 2335 return -ENODEV; 2336 2337 /* Fill in the struct and pass to userspace */ 2338 switch (cmd) { 2339 case GPIO_GET_CHIPINFO_IOCTL: 2340 return chipinfo_get(cdev, ip); 2341 #ifdef CONFIG_GPIO_CDEV_V1 2342 case GPIO_GET_LINEHANDLE_IOCTL: 2343 return linehandle_create(gdev, ip); 2344 case GPIO_GET_LINEEVENT_IOCTL: 2345 return lineevent_create(gdev, ip); 2346 case GPIO_GET_LINEINFO_IOCTL: 2347 return lineinfo_get_v1(cdev, ip, false); 2348 case GPIO_GET_LINEINFO_WATCH_IOCTL: 2349 return lineinfo_get_v1(cdev, ip, true); 2350 #endif /* CONFIG_GPIO_CDEV_V1 */ 2351 case GPIO_V2_GET_LINEINFO_IOCTL: 2352 return lineinfo_get(cdev, ip, false); 2353 case GPIO_V2_GET_LINEINFO_WATCH_IOCTL: 2354 return lineinfo_get(cdev, ip, true); 2355 case GPIO_V2_GET_LINE_IOCTL: 2356 return linereq_create(gdev, ip); 2357 case GPIO_GET_LINEINFO_UNWATCH_IOCTL: 2358 return lineinfo_unwatch(cdev, ip); 2359 default: 2360 return -EINVAL; 2361 } 2362 } 2363 2364 #ifdef CONFIG_COMPAT 2365 static long gpio_ioctl_compat(struct file *file, unsigned int cmd, 2366 unsigned long arg) 2367 { 2368 return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 2369 } 2370 #endif 2371 2372 static struct gpio_chardev_data * 2373 to_gpio_chardev_data(struct notifier_block *nb) 2374 { 2375 return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb); 2376 } 2377 2378 static int lineinfo_changed_notify(struct notifier_block *nb, 2379 unsigned long action, void *data) 2380 { 2381 struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb); 2382 struct gpio_v2_line_info_changed chg; 2383 struct gpio_desc *desc = data; 2384 int ret; 2385 2386 if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines)) 2387 return NOTIFY_DONE; 2388 2389 memset(&chg, 0, sizeof(chg)); 2390 chg.event_type = action; 2391 chg.timestamp_ns = ktime_get_ns(); 2392 gpio_desc_to_lineinfo(desc, &chg.info); 2393 2394 ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); 2395 if (ret) 2396 wake_up_poll(&cdev->wait, EPOLLIN); 2397 else 2398 pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n"); 2399 2400 return NOTIFY_OK; 2401 } 2402 2403 static __poll_t lineinfo_watch_poll(struct file *file, 2404 struct poll_table_struct *pollt) 2405 { 2406 struct gpio_chardev_data *cdev = file->private_data; 2407 __poll_t events = 0; 2408 2409 poll_wait(file, &cdev->wait, pollt); 2410 2411 if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events, 2412 &cdev->wait.lock)) 2413 events = EPOLLIN | EPOLLRDNORM; 2414 2415 return events; 2416 } 2417 2418 static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, 2419 size_t count, loff_t *off) 2420 { 2421 struct gpio_chardev_data *cdev = file->private_data; 2422 struct gpio_v2_line_info_changed event; 2423 ssize_t bytes_read = 0; 2424 int ret; 2425 size_t event_size; 2426 2427 #ifndef CONFIG_GPIO_CDEV_V1 2428 event_size = sizeof(struct gpio_v2_line_info_changed); 2429 if (count < event_size) 2430 return -EINVAL; 2431 #endif 2432 2433 do { 2434 spin_lock(&cdev->wait.lock); 2435 if (kfifo_is_empty(&cdev->events)) { 2436 if (bytes_read) { 2437 spin_unlock(&cdev->wait.lock); 2438 return bytes_read; 2439 } 2440 2441 if (file->f_flags & O_NONBLOCK) { 2442 spin_unlock(&cdev->wait.lock); 2443 return -EAGAIN; 2444 } 2445 2446 ret = wait_event_interruptible_locked(cdev->wait, 2447 !kfifo_is_empty(&cdev->events)); 2448 if (ret) { 2449 spin_unlock(&cdev->wait.lock); 2450 return ret; 2451 } 2452 } 2453 #ifdef CONFIG_GPIO_CDEV_V1 2454 /* must be after kfifo check so watch_abi_version is set */ 2455 if (atomic_read(&cdev->watch_abi_version) == 2) 2456 event_size = sizeof(struct gpio_v2_line_info_changed); 2457 else 2458 event_size = sizeof(struct gpioline_info_changed); 2459 if (count < event_size) { 2460 spin_unlock(&cdev->wait.lock); 2461 return -EINVAL; 2462 } 2463 #endif 2464 ret = kfifo_out(&cdev->events, &event, 1); 2465 spin_unlock(&cdev->wait.lock); 2466 if (ret != 1) { 2467 ret = -EIO; 2468 break; 2469 /* We should never get here. See lineevent_read(). */ 2470 } 2471 2472 #ifdef CONFIG_GPIO_CDEV_V1 2473 if (event_size == sizeof(struct gpio_v2_line_info_changed)) { 2474 if (copy_to_user(buf + bytes_read, &event, event_size)) 2475 return -EFAULT; 2476 } else { 2477 struct gpioline_info_changed event_v1; 2478 2479 gpio_v2_line_info_changed_to_v1(&event, &event_v1); 2480 if (copy_to_user(buf + bytes_read, &event_v1, 2481 event_size)) 2482 return -EFAULT; 2483 } 2484 #else 2485 if (copy_to_user(buf + bytes_read, &event, event_size)) 2486 return -EFAULT; 2487 #endif 2488 bytes_read += event_size; 2489 } while (count >= bytes_read + sizeof(event)); 2490 2491 return bytes_read; 2492 } 2493 2494 /** 2495 * gpio_chrdev_open() - open the chardev for ioctl operations 2496 * @inode: inode for this chardev 2497 * @file: file struct for storing private data 2498 * Returns 0 on success 2499 */ 2500 static int gpio_chrdev_open(struct inode *inode, struct file *file) 2501 { 2502 struct gpio_device *gdev = container_of(inode->i_cdev, 2503 struct gpio_device, chrdev); 2504 struct gpio_chardev_data *cdev; 2505 int ret = -ENOMEM; 2506 2507 /* Fail on open if the backing gpiochip is gone */ 2508 if (!gdev->chip) 2509 return -ENODEV; 2510 2511 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); 2512 if (!cdev) 2513 return -ENOMEM; 2514 2515 cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL); 2516 if (!cdev->watched_lines) 2517 goto out_free_cdev; 2518 2519 init_waitqueue_head(&cdev->wait); 2520 INIT_KFIFO(cdev->events); 2521 cdev->gdev = gdev; 2522 2523 cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify; 2524 ret = blocking_notifier_chain_register(&gdev->notifier, 2525 &cdev->lineinfo_changed_nb); 2526 if (ret) 2527 goto out_free_bitmap; 2528 2529 get_device(&gdev->dev); 2530 file->private_data = cdev; 2531 2532 ret = nonseekable_open(inode, file); 2533 if (ret) 2534 goto out_unregister_notifier; 2535 2536 return ret; 2537 2538 out_unregister_notifier: 2539 blocking_notifier_chain_unregister(&gdev->notifier, 2540 &cdev->lineinfo_changed_nb); 2541 out_free_bitmap: 2542 bitmap_free(cdev->watched_lines); 2543 out_free_cdev: 2544 kfree(cdev); 2545 return ret; 2546 } 2547 2548 /** 2549 * gpio_chrdev_release() - close chardev after ioctl operations 2550 * @inode: inode for this chardev 2551 * @file: file struct for storing private data 2552 * Returns 0 on success 2553 */ 2554 static int gpio_chrdev_release(struct inode *inode, struct file *file) 2555 { 2556 struct gpio_chardev_data *cdev = file->private_data; 2557 struct gpio_device *gdev = cdev->gdev; 2558 2559 bitmap_free(cdev->watched_lines); 2560 blocking_notifier_chain_unregister(&gdev->notifier, 2561 &cdev->lineinfo_changed_nb); 2562 put_device(&gdev->dev); 2563 kfree(cdev); 2564 2565 return 0; 2566 } 2567 2568 static const struct file_operations gpio_fileops = { 2569 .release = gpio_chrdev_release, 2570 .open = gpio_chrdev_open, 2571 .poll = lineinfo_watch_poll, 2572 .read = lineinfo_watch_read, 2573 .owner = THIS_MODULE, 2574 .llseek = no_llseek, 2575 .unlocked_ioctl = gpio_ioctl, 2576 #ifdef CONFIG_COMPAT 2577 .compat_ioctl = gpio_ioctl_compat, 2578 #endif 2579 }; 2580 2581 int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) 2582 { 2583 int ret; 2584 2585 cdev_init(&gdev->chrdev, &gpio_fileops); 2586 gdev->chrdev.owner = THIS_MODULE; 2587 gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id); 2588 2589 ret = cdev_device_add(&gdev->chrdev, &gdev->dev); 2590 if (ret) 2591 return ret; 2592 2593 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n", 2594 MAJOR(devt), gdev->id); 2595 2596 return 0; 2597 } 2598 2599 void gpiolib_cdev_unregister(struct gpio_device *gdev) 2600 { 2601 cdev_device_del(&gdev->chrdev, &gdev->dev); 2602 } 2603