1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * GPIO testing driver based on configfs. 4 * 5 * Copyright (C) 2021 Bartosz Golaszewski <brgl@bgdev.pl> 6 */ 7 8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 10 #include <linux/bitmap.h> 11 #include <linux/completion.h> 12 #include <linux/configfs.h> 13 #include <linux/device.h> 14 #include <linux/gpio/driver.h> 15 #include <linux/gpio/machine.h> 16 #include <linux/idr.h> 17 #include <linux/interrupt.h> 18 #include <linux/irq.h> 19 #include <linux/irq_sim.h> 20 #include <linux/list.h> 21 #include <linux/mod_devicetable.h> 22 #include <linux/module.h> 23 #include <linux/mutex.h> 24 #include <linux/notifier.h> 25 #include <linux/platform_device.h> 26 #include <linux/property.h> 27 #include <linux/slab.h> 28 #include <linux/string.h> 29 #include <linux/string_helpers.h> 30 #include <linux/sysfs.h> 31 32 #include "gpiolib.h" 33 34 #define GPIO_SIM_NGPIO_MAX 1024 35 #define GPIO_SIM_PROP_MAX 4 /* Max 3 properties + sentinel. */ 36 #define GPIO_SIM_NUM_ATTRS 3 /* value, pull and sentinel */ 37 38 static DEFINE_IDA(gpio_sim_ida); 39 40 struct gpio_sim_chip { 41 struct gpio_chip gc; 42 unsigned long *direction_map; 43 unsigned long *value_map; 44 unsigned long *pull_map; 45 struct irq_domain *irq_sim; 46 struct mutex lock; 47 const struct attribute_group **attr_groups; 48 }; 49 50 struct gpio_sim_attribute { 51 struct device_attribute dev_attr; 52 unsigned int offset; 53 }; 54 55 static struct gpio_sim_attribute * 56 to_gpio_sim_attr(struct device_attribute *dev_attr) 57 { 58 return container_of(dev_attr, struct gpio_sim_attribute, dev_attr); 59 } 60 61 static int gpio_sim_apply_pull(struct gpio_sim_chip *chip, 62 unsigned int offset, int value) 63 { 64 int irq, irq_type, ret; 65 struct gpio_desc *desc; 66 struct gpio_chip *gc; 67 68 gc = &chip->gc; 69 desc = &gc->gpiodev->descs[offset]; 70 71 mutex_lock(&chip->lock); 72 73 if (test_bit(FLAG_REQUESTED, &desc->flags) && 74 !test_bit(FLAG_IS_OUT, &desc->flags)) { 75 if (value == !!test_bit(offset, chip->value_map)) 76 goto set_pull; 77 78 /* 79 * This is fine - it just means, nobody is listening 80 * for interrupts on this line, otherwise 81 * irq_create_mapping() would have been called from 82 * the to_irq() callback. 83 */ 84 irq = irq_find_mapping(chip->irq_sim, offset); 85 if (!irq) 86 goto set_value; 87 88 irq_type = irq_get_trigger_type(irq); 89 90 if ((value && (irq_type & IRQ_TYPE_EDGE_RISING)) || 91 (!value && (irq_type & IRQ_TYPE_EDGE_FALLING))) { 92 ret = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING, 93 true); 94 if (ret) 95 goto set_pull; 96 } 97 } 98 99 set_value: 100 /* Change the value unless we're actively driving the line. */ 101 if (!test_bit(FLAG_REQUESTED, &desc->flags) || 102 !test_bit(FLAG_IS_OUT, &desc->flags)) 103 __assign_bit(offset, chip->value_map, value); 104 105 set_pull: 106 __assign_bit(offset, chip->pull_map, value); 107 mutex_unlock(&chip->lock); 108 return 0; 109 } 110 111 static int gpio_sim_get(struct gpio_chip *gc, unsigned int offset) 112 { 113 struct gpio_sim_chip *chip = gpiochip_get_data(gc); 114 int ret; 115 116 mutex_lock(&chip->lock); 117 ret = !!test_bit(offset, chip->value_map); 118 mutex_unlock(&chip->lock); 119 120 return ret; 121 } 122 123 static void gpio_sim_set(struct gpio_chip *gc, unsigned int offset, int value) 124 { 125 struct gpio_sim_chip *chip = gpiochip_get_data(gc); 126 127 mutex_lock(&chip->lock); 128 __assign_bit(offset, chip->value_map, value); 129 mutex_unlock(&chip->lock); 130 } 131 132 static int gpio_sim_get_multiple(struct gpio_chip *gc, 133 unsigned long *mask, unsigned long *bits) 134 { 135 struct gpio_sim_chip *chip = gpiochip_get_data(gc); 136 137 mutex_lock(&chip->lock); 138 bitmap_replace(bits, bits, chip->value_map, mask, gc->ngpio); 139 mutex_unlock(&chip->lock); 140 141 return 0; 142 } 143 144 static void gpio_sim_set_multiple(struct gpio_chip *gc, 145 unsigned long *mask, unsigned long *bits) 146 { 147 struct gpio_sim_chip *chip = gpiochip_get_data(gc); 148 149 mutex_lock(&chip->lock); 150 bitmap_replace(chip->value_map, chip->value_map, bits, mask, gc->ngpio); 151 mutex_unlock(&chip->lock); 152 } 153 154 static int gpio_sim_direction_output(struct gpio_chip *gc, 155 unsigned int offset, int value) 156 { 157 struct gpio_sim_chip *chip = gpiochip_get_data(gc); 158 159 mutex_lock(&chip->lock); 160 __clear_bit(offset, chip->direction_map); 161 __assign_bit(offset, chip->value_map, value); 162 mutex_unlock(&chip->lock); 163 164 return 0; 165 } 166 167 static int gpio_sim_direction_input(struct gpio_chip *gc, unsigned int offset) 168 { 169 struct gpio_sim_chip *chip = gpiochip_get_data(gc); 170 171 mutex_lock(&chip->lock); 172 __set_bit(offset, chip->direction_map); 173 mutex_unlock(&chip->lock); 174 175 return 0; 176 } 177 178 static int gpio_sim_get_direction(struct gpio_chip *gc, unsigned int offset) 179 { 180 struct gpio_sim_chip *chip = gpiochip_get_data(gc); 181 int direction; 182 183 mutex_lock(&chip->lock); 184 direction = !!test_bit(offset, chip->direction_map); 185 mutex_unlock(&chip->lock); 186 187 return direction ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT; 188 } 189 190 static int gpio_sim_set_config(struct gpio_chip *gc, 191 unsigned int offset, unsigned long config) 192 { 193 struct gpio_sim_chip *chip = gpiochip_get_data(gc); 194 195 switch (pinconf_to_config_param(config)) { 196 case PIN_CONFIG_BIAS_PULL_UP: 197 return gpio_sim_apply_pull(chip, offset, 1); 198 case PIN_CONFIG_BIAS_PULL_DOWN: 199 return gpio_sim_apply_pull(chip, offset, 0); 200 default: 201 break; 202 } 203 204 return -ENOTSUPP; 205 } 206 207 static int gpio_sim_to_irq(struct gpio_chip *gc, unsigned int offset) 208 { 209 struct gpio_sim_chip *chip = gpiochip_get_data(gc); 210 211 return irq_create_mapping(chip->irq_sim, offset); 212 } 213 214 static void gpio_sim_free(struct gpio_chip *gc, unsigned int offset) 215 { 216 struct gpio_sim_chip *chip = gpiochip_get_data(gc); 217 218 mutex_lock(&chip->lock); 219 __assign_bit(offset, chip->value_map, !!test_bit(offset, chip->pull_map)); 220 mutex_unlock(&chip->lock); 221 } 222 223 static ssize_t gpio_sim_sysfs_val_show(struct device *dev, 224 struct device_attribute *attr, char *buf) 225 { 226 struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr); 227 struct gpio_sim_chip *chip = dev_get_drvdata(dev); 228 int val; 229 230 mutex_lock(&chip->lock); 231 val = !!test_bit(line_attr->offset, chip->value_map); 232 mutex_unlock(&chip->lock); 233 234 return sysfs_emit(buf, "%d\n", val); 235 } 236 237 static ssize_t gpio_sim_sysfs_val_store(struct device *dev, 238 struct device_attribute *attr, 239 const char *buf, size_t count) 240 { 241 /* 242 * Not assigning this function will result in write() returning -EIO 243 * which is confusing. Return -EPERM explicitly. 244 */ 245 return -EPERM; 246 } 247 248 static const char *const gpio_sim_sysfs_pull_strings[] = { 249 [0] = "pull-down", 250 [1] = "pull-up", 251 }; 252 253 static ssize_t gpio_sim_sysfs_pull_show(struct device *dev, 254 struct device_attribute *attr, 255 char *buf) 256 { 257 struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr); 258 struct gpio_sim_chip *chip = dev_get_drvdata(dev); 259 int pull; 260 261 mutex_lock(&chip->lock); 262 pull = !!test_bit(line_attr->offset, chip->pull_map); 263 mutex_unlock(&chip->lock); 264 265 return sysfs_emit(buf, "%s\n", gpio_sim_sysfs_pull_strings[pull]); 266 } 267 268 static ssize_t gpio_sim_sysfs_pull_store(struct device *dev, 269 struct device_attribute *attr, 270 const char *buf, size_t len) 271 { 272 struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr); 273 struct gpio_sim_chip *chip = dev_get_drvdata(dev); 274 int ret, pull; 275 276 pull = sysfs_match_string(gpio_sim_sysfs_pull_strings, buf); 277 if (pull < 0) 278 return pull; 279 280 ret = gpio_sim_apply_pull(chip, line_attr->offset, pull); 281 if (ret) 282 return ret; 283 284 return len; 285 } 286 287 static void gpio_sim_mutex_destroy(void *data) 288 { 289 struct mutex *lock = data; 290 291 mutex_destroy(lock); 292 } 293 294 static void gpio_sim_sysfs_remove(void *data) 295 { 296 struct gpio_sim_chip *chip = data; 297 298 sysfs_remove_groups(&chip->gc.gpiodev->dev.kobj, chip->attr_groups); 299 } 300 301 static int gpio_sim_setup_sysfs(struct gpio_sim_chip *chip) 302 { 303 struct device_attribute *val_dev_attr, *pull_dev_attr; 304 struct gpio_sim_attribute *val_attr, *pull_attr; 305 unsigned int num_lines = chip->gc.ngpio; 306 struct device *dev = chip->gc.parent; 307 struct attribute_group *attr_group; 308 struct attribute **attrs; 309 int i, ret; 310 311 chip->attr_groups = devm_kcalloc(dev, sizeof(*chip->attr_groups), 312 num_lines + 1, GFP_KERNEL); 313 if (!chip->attr_groups) 314 return -ENOMEM; 315 316 for (i = 0; i < num_lines; i++) { 317 attr_group = devm_kzalloc(dev, sizeof(*attr_group), GFP_KERNEL); 318 attrs = devm_kcalloc(dev, GPIO_SIM_NUM_ATTRS, sizeof(*attrs), 319 GFP_KERNEL); 320 val_attr = devm_kzalloc(dev, sizeof(*val_attr), GFP_KERNEL); 321 pull_attr = devm_kzalloc(dev, sizeof(*pull_attr), GFP_KERNEL); 322 if (!attr_group || !attrs || !val_attr || !pull_attr) 323 return -ENOMEM; 324 325 attr_group->name = devm_kasprintf(dev, GFP_KERNEL, 326 "sim_gpio%u", i); 327 if (!attr_group->name) 328 return -ENOMEM; 329 330 val_attr->offset = pull_attr->offset = i; 331 332 val_dev_attr = &val_attr->dev_attr; 333 pull_dev_attr = &pull_attr->dev_attr; 334 335 sysfs_attr_init(&val_dev_attr->attr); 336 sysfs_attr_init(&pull_dev_attr->attr); 337 338 val_dev_attr->attr.name = "value"; 339 pull_dev_attr->attr.name = "pull"; 340 341 val_dev_attr->attr.mode = pull_dev_attr->attr.mode = 0644; 342 343 val_dev_attr->show = gpio_sim_sysfs_val_show; 344 val_dev_attr->store = gpio_sim_sysfs_val_store; 345 pull_dev_attr->show = gpio_sim_sysfs_pull_show; 346 pull_dev_attr->store = gpio_sim_sysfs_pull_store; 347 348 attrs[0] = &val_dev_attr->attr; 349 attrs[1] = &pull_dev_attr->attr; 350 351 attr_group->attrs = attrs; 352 chip->attr_groups[i] = attr_group; 353 } 354 355 ret = sysfs_create_groups(&chip->gc.gpiodev->dev.kobj, 356 chip->attr_groups); 357 if (ret) 358 return ret; 359 360 return devm_add_action_or_reset(dev, gpio_sim_sysfs_remove, chip); 361 } 362 363 static int gpio_sim_add_bank(struct fwnode_handle *swnode, struct device *dev) 364 { 365 struct gpio_sim_chip *chip; 366 struct gpio_chip *gc; 367 const char *label; 368 u32 num_lines; 369 int ret; 370 371 ret = fwnode_property_read_u32(swnode, "ngpios", &num_lines); 372 if (ret) 373 return ret; 374 375 if (num_lines > GPIO_SIM_NGPIO_MAX) 376 return -ERANGE; 377 378 ret = fwnode_property_read_string(swnode, "gpio-sim,label", &label); 379 if (ret) { 380 label = devm_kasprintf(dev, GFP_KERNEL, "%s-%pfwP", 381 dev_name(dev), swnode); 382 if (!label) 383 return -ENOMEM; 384 } 385 386 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); 387 if (!chip) 388 return -ENOMEM; 389 390 chip->direction_map = devm_bitmap_alloc(dev, num_lines, GFP_KERNEL); 391 if (!chip->direction_map) 392 return -ENOMEM; 393 394 /* Default to input mode. */ 395 bitmap_fill(chip->direction_map, num_lines); 396 397 chip->value_map = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL); 398 if (!chip->value_map) 399 return -ENOMEM; 400 401 chip->pull_map = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL); 402 if (!chip->pull_map) 403 return -ENOMEM; 404 405 chip->irq_sim = devm_irq_domain_create_sim(dev, NULL, num_lines); 406 if (IS_ERR(chip->irq_sim)) 407 return PTR_ERR(chip->irq_sim); 408 409 mutex_init(&chip->lock); 410 ret = devm_add_action_or_reset(dev, gpio_sim_mutex_destroy, 411 &chip->lock); 412 if (ret) 413 return ret; 414 415 gc = &chip->gc; 416 gc->base = -1; 417 gc->ngpio = num_lines; 418 gc->label = label; 419 gc->owner = THIS_MODULE; 420 gc->parent = dev; 421 gc->fwnode = swnode; 422 gc->get = gpio_sim_get; 423 gc->set = gpio_sim_set; 424 gc->get_multiple = gpio_sim_get_multiple; 425 gc->set_multiple = gpio_sim_set_multiple; 426 gc->direction_output = gpio_sim_direction_output; 427 gc->direction_input = gpio_sim_direction_input; 428 gc->get_direction = gpio_sim_get_direction; 429 gc->set_config = gpio_sim_set_config; 430 gc->to_irq = gpio_sim_to_irq; 431 gc->free = gpio_sim_free; 432 433 ret = devm_gpiochip_add_data(dev, gc, chip); 434 if (ret) 435 return ret; 436 437 /* Used by sysfs and configfs callbacks. */ 438 dev_set_drvdata(&gc->gpiodev->dev, chip); 439 440 return gpio_sim_setup_sysfs(chip); 441 } 442 443 static int gpio_sim_probe(struct platform_device *pdev) 444 { 445 struct device *dev = &pdev->dev; 446 struct fwnode_handle *swnode; 447 int ret; 448 449 device_for_each_child_node(dev, swnode) { 450 ret = gpio_sim_add_bank(swnode, dev); 451 if (ret) { 452 fwnode_handle_put(swnode); 453 return ret; 454 } 455 } 456 457 return 0; 458 } 459 460 static const struct of_device_id gpio_sim_of_match[] = { 461 { .compatible = "gpio-simulator" }, 462 { } 463 }; 464 MODULE_DEVICE_TABLE(of, gpio_sim_of_match); 465 466 static struct platform_driver gpio_sim_driver = { 467 .driver = { 468 .name = "gpio-sim", 469 .of_match_table = gpio_sim_of_match, 470 }, 471 .probe = gpio_sim_probe, 472 }; 473 474 struct gpio_sim_device { 475 struct config_group group; 476 477 /* 478 * If pdev is NULL, the device is 'pending' (waiting for configuration). 479 * Once the pointer is assigned, the device has been created and the 480 * item is 'live'. 481 */ 482 struct platform_device *pdev; 483 int id; 484 485 /* 486 * Each configfs filesystem operation is protected with the subsystem 487 * mutex. Each separate attribute is protected with the buffer mutex. 488 * This structure however can be modified by callbacks of different 489 * attributes so we need another lock. 490 * 491 * We use this lock fo protecting all data structures owned by this 492 * object too. 493 */ 494 struct mutex lock; 495 496 /* 497 * This is used to synchronously wait for the driver's probe to complete 498 * and notify the user-space about any errors. 499 */ 500 struct notifier_block bus_notifier; 501 struct completion probe_completion; 502 bool driver_bound; 503 504 struct gpiod_hog *hogs; 505 506 struct list_head bank_list; 507 }; 508 509 /* This is called with dev->lock already taken. */ 510 static int gpio_sim_bus_notifier_call(struct notifier_block *nb, 511 unsigned long action, void *data) 512 { 513 struct gpio_sim_device *simdev = container_of(nb, 514 struct gpio_sim_device, 515 bus_notifier); 516 struct device *dev = data; 517 char devname[32]; 518 519 snprintf(devname, sizeof(devname), "gpio-sim.%u", simdev->id); 520 521 if (strcmp(dev_name(dev), devname) == 0) { 522 if (action == BUS_NOTIFY_BOUND_DRIVER) 523 simdev->driver_bound = true; 524 else if (action == BUS_NOTIFY_DRIVER_NOT_BOUND) 525 simdev->driver_bound = false; 526 else 527 return NOTIFY_DONE; 528 529 complete(&simdev->probe_completion); 530 return NOTIFY_OK; 531 } 532 533 return NOTIFY_DONE; 534 } 535 536 static struct gpio_sim_device *to_gpio_sim_device(struct config_item *item) 537 { 538 struct config_group *group = to_config_group(item); 539 540 return container_of(group, struct gpio_sim_device, group); 541 } 542 543 struct gpio_sim_bank { 544 struct config_group group; 545 546 /* 547 * We could have used the ci_parent field of the config_item but 548 * configfs is stupid and calls the item's release callback after 549 * already having cleared the parent pointer even though the parent 550 * is guaranteed to survive the child... 551 * 552 * So we need to store the pointer to the parent struct here. We can 553 * dereference it anywhere we need with no checks and no locking as 554 * it's guaranteed to survive the children and protected by configfs 555 * locks. 556 * 557 * Same for other structures. 558 */ 559 struct gpio_sim_device *parent; 560 struct list_head siblings; 561 562 char *label; 563 unsigned int num_lines; 564 565 struct list_head line_list; 566 567 struct fwnode_handle *swnode; 568 }; 569 570 static struct gpio_sim_bank *to_gpio_sim_bank(struct config_item *item) 571 { 572 struct config_group *group = to_config_group(item); 573 574 return container_of(group, struct gpio_sim_bank, group); 575 } 576 577 static bool gpio_sim_bank_has_label(struct gpio_sim_bank *bank) 578 { 579 return bank->label && *bank->label; 580 } 581 582 static struct gpio_sim_device * 583 gpio_sim_bank_get_device(struct gpio_sim_bank *bank) 584 { 585 return bank->parent; 586 } 587 588 struct gpio_sim_hog; 589 590 struct gpio_sim_line { 591 struct config_group group; 592 593 struct gpio_sim_bank *parent; 594 struct list_head siblings; 595 596 unsigned int offset; 597 char *name; 598 599 /* There can only be one hog per line. */ 600 struct gpio_sim_hog *hog; 601 }; 602 603 static struct gpio_sim_line *to_gpio_sim_line(struct config_item *item) 604 { 605 struct config_group *group = to_config_group(item); 606 607 return container_of(group, struct gpio_sim_line, group); 608 } 609 610 static struct gpio_sim_device * 611 gpio_sim_line_get_device(struct gpio_sim_line *line) 612 { 613 struct gpio_sim_bank *bank = line->parent; 614 615 return gpio_sim_bank_get_device(bank); 616 } 617 618 struct gpio_sim_hog { 619 struct config_item item; 620 struct gpio_sim_line *parent; 621 622 char *name; 623 int dir; 624 }; 625 626 static struct gpio_sim_hog *to_gpio_sim_hog(struct config_item *item) 627 { 628 return container_of(item, struct gpio_sim_hog, item); 629 } 630 631 static struct gpio_sim_device *gpio_sim_hog_get_device(struct gpio_sim_hog *hog) 632 { 633 struct gpio_sim_line *line = hog->parent; 634 635 return gpio_sim_line_get_device(line); 636 } 637 638 static bool gpio_sim_device_is_live_unlocked(struct gpio_sim_device *dev) 639 { 640 return !!dev->pdev; 641 } 642 643 static char *gpio_sim_strdup_trimmed(const char *str, size_t count) 644 { 645 char *dup, *trimmed; 646 647 dup = kstrndup(str, count, GFP_KERNEL); 648 if (!dup) 649 return NULL; 650 651 trimmed = strstrip(dup); 652 memmove(dup, trimmed, strlen(trimmed) + 1); 653 654 return dup; 655 } 656 657 static ssize_t gpio_sim_device_config_dev_name_show(struct config_item *item, 658 char *page) 659 { 660 struct gpio_sim_device *dev = to_gpio_sim_device(item); 661 struct platform_device *pdev; 662 int ret; 663 664 mutex_lock(&dev->lock); 665 pdev = dev->pdev; 666 if (pdev) 667 ret = sprintf(page, "%s\n", dev_name(&pdev->dev)); 668 else 669 ret = sprintf(page, "gpio-sim.%d\n", dev->id); 670 mutex_unlock(&dev->lock); 671 672 return ret; 673 } 674 675 CONFIGFS_ATTR_RO(gpio_sim_device_config_, dev_name); 676 677 static ssize_t 678 gpio_sim_device_config_live_show(struct config_item *item, char *page) 679 { 680 struct gpio_sim_device *dev = to_gpio_sim_device(item); 681 bool live; 682 683 mutex_lock(&dev->lock); 684 live = gpio_sim_device_is_live_unlocked(dev); 685 mutex_unlock(&dev->lock); 686 687 return sprintf(page, "%c\n", live ? '1' : '0'); 688 } 689 690 static char **gpio_sim_make_line_names(struct gpio_sim_bank *bank, 691 unsigned int *line_names_size) 692 { 693 unsigned int max_offset = 0; 694 bool has_line_names = false; 695 struct gpio_sim_line *line; 696 char **line_names; 697 698 list_for_each_entry(line, &bank->line_list, siblings) { 699 if (line->name) { 700 if (line->offset > max_offset) 701 max_offset = line->offset; 702 703 /* 704 * max_offset can stay at 0 so it's not an indicator 705 * of whether line names were configured at all. 706 */ 707 has_line_names = true; 708 } 709 } 710 711 if (!has_line_names) 712 /* 713 * This is not an error - NULL means, there are no line 714 * names configured. 715 */ 716 return NULL; 717 718 *line_names_size = max_offset + 1; 719 720 line_names = kcalloc(*line_names_size, sizeof(*line_names), GFP_KERNEL); 721 if (!line_names) 722 return ERR_PTR(-ENOMEM); 723 724 list_for_each_entry(line, &bank->line_list, siblings) 725 line_names[line->offset] = line->name; 726 727 return line_names; 728 } 729 730 static void gpio_sim_remove_hogs(struct gpio_sim_device *dev) 731 { 732 struct gpiod_hog *hog; 733 734 if (!dev->hogs) 735 return; 736 737 gpiod_remove_hogs(dev->hogs); 738 739 for (hog = dev->hogs; hog->chip_label; hog++) { 740 kfree(hog->chip_label); 741 kfree(hog->line_name); 742 } 743 744 kfree(dev->hogs); 745 dev->hogs = NULL; 746 } 747 748 static int gpio_sim_add_hogs(struct gpio_sim_device *dev) 749 { 750 unsigned int num_hogs = 0, idx = 0; 751 struct gpio_sim_bank *bank; 752 struct gpio_sim_line *line; 753 struct gpiod_hog *hog; 754 755 list_for_each_entry(bank, &dev->bank_list, siblings) { 756 list_for_each_entry(line, &bank->line_list, siblings) { 757 if (line->hog) 758 num_hogs++; 759 } 760 } 761 762 if (!num_hogs) 763 return 0; 764 765 /* Allocate one more for the sentinel. */ 766 dev->hogs = kcalloc(num_hogs + 1, sizeof(*dev->hogs), GFP_KERNEL); 767 if (!dev->hogs) 768 return -ENOMEM; 769 770 list_for_each_entry(bank, &dev->bank_list, siblings) { 771 list_for_each_entry(line, &bank->line_list, siblings) { 772 if (!line->hog) 773 continue; 774 775 hog = &dev->hogs[idx++]; 776 777 /* 778 * We need to make this string manually because at this 779 * point the device doesn't exist yet and so dev_name() 780 * is not available. 781 */ 782 if (gpio_sim_bank_has_label(bank)) 783 hog->chip_label = kstrdup(bank->label, 784 GFP_KERNEL); 785 else 786 hog->chip_label = kasprintf(GFP_KERNEL, 787 "gpio-sim.%u-%pfwP", 788 dev->id, 789 bank->swnode); 790 if (!hog->chip_label) { 791 gpio_sim_remove_hogs(dev); 792 return -ENOMEM; 793 } 794 795 /* 796 * We need to duplicate this because the hog config 797 * item can be removed at any time (and we can't block 798 * it) and gpiolib doesn't make a deep copy of the hog 799 * data. 800 */ 801 if (line->hog->name) { 802 hog->line_name = kstrdup(line->hog->name, 803 GFP_KERNEL); 804 if (!hog->line_name) { 805 gpio_sim_remove_hogs(dev); 806 return -ENOMEM; 807 } 808 } 809 810 hog->chip_hwnum = line->offset; 811 hog->dflags = line->hog->dir; 812 } 813 } 814 815 gpiod_add_hogs(dev->hogs); 816 817 return 0; 818 } 819 820 static struct fwnode_handle * 821 gpio_sim_make_bank_swnode(struct gpio_sim_bank *bank, 822 struct fwnode_handle *parent) 823 { 824 struct property_entry properties[GPIO_SIM_PROP_MAX]; 825 unsigned int prop_idx = 0, line_names_size = 0; 826 struct fwnode_handle *swnode; 827 char **line_names; 828 829 memset(properties, 0, sizeof(properties)); 830 831 properties[prop_idx++] = PROPERTY_ENTRY_U32("ngpios", bank->num_lines); 832 833 if (gpio_sim_bank_has_label(bank)) 834 properties[prop_idx++] = PROPERTY_ENTRY_STRING("gpio-sim,label", 835 bank->label); 836 837 line_names = gpio_sim_make_line_names(bank, &line_names_size); 838 if (IS_ERR(line_names)) 839 return ERR_CAST(line_names); 840 841 if (line_names) 842 properties[prop_idx++] = PROPERTY_ENTRY_STRING_ARRAY_LEN( 843 "gpio-line-names", 844 line_names, line_names_size); 845 846 swnode = fwnode_create_software_node(properties, parent); 847 kfree(line_names); 848 return swnode; 849 } 850 851 static void gpio_sim_remove_swnode_recursive(struct fwnode_handle *swnode) 852 { 853 struct fwnode_handle *child; 854 855 fwnode_for_each_child_node(swnode, child) 856 fwnode_remove_software_node(child); 857 858 fwnode_remove_software_node(swnode); 859 } 860 861 static bool gpio_sim_bank_labels_non_unique(struct gpio_sim_device *dev) 862 { 863 struct gpio_sim_bank *this, *pos; 864 865 list_for_each_entry(this, &dev->bank_list, siblings) { 866 list_for_each_entry(pos, &dev->bank_list, siblings) { 867 if (this == pos || (!this->label || !pos->label)) 868 continue; 869 870 if (strcmp(this->label, pos->label) == 0) 871 return true; 872 } 873 } 874 875 return false; 876 } 877 878 static int gpio_sim_device_activate_unlocked(struct gpio_sim_device *dev) 879 { 880 struct platform_device_info pdevinfo; 881 struct fwnode_handle *swnode; 882 struct platform_device *pdev; 883 struct gpio_sim_bank *bank; 884 int ret; 885 886 if (list_empty(&dev->bank_list)) 887 return -ENODATA; 888 889 /* 890 * Non-unique GPIO device labels are a corner-case we don't support 891 * as it would interfere with machine hogging mechanism and has little 892 * use in real life. 893 */ 894 if (gpio_sim_bank_labels_non_unique(dev)) 895 return -EINVAL; 896 897 memset(&pdevinfo, 0, sizeof(pdevinfo)); 898 899 swnode = fwnode_create_software_node(NULL, NULL); 900 if (IS_ERR(swnode)) 901 return PTR_ERR(swnode); 902 903 list_for_each_entry(bank, &dev->bank_list, siblings) { 904 bank->swnode = gpio_sim_make_bank_swnode(bank, swnode); 905 if (IS_ERR(bank->swnode)) { 906 ret = PTR_ERR(bank->swnode); 907 gpio_sim_remove_swnode_recursive(swnode); 908 return ret; 909 } 910 } 911 912 ret = gpio_sim_add_hogs(dev); 913 if (ret) { 914 gpio_sim_remove_swnode_recursive(swnode); 915 return ret; 916 } 917 918 pdevinfo.name = "gpio-sim"; 919 pdevinfo.fwnode = swnode; 920 pdevinfo.id = dev->id; 921 922 reinit_completion(&dev->probe_completion); 923 dev->driver_bound = false; 924 bus_register_notifier(&platform_bus_type, &dev->bus_notifier); 925 926 pdev = platform_device_register_full(&pdevinfo); 927 if (IS_ERR(pdev)) { 928 bus_unregister_notifier(&platform_bus_type, &dev->bus_notifier); 929 gpio_sim_remove_hogs(dev); 930 gpio_sim_remove_swnode_recursive(swnode); 931 return PTR_ERR(pdev); 932 } 933 934 wait_for_completion(&dev->probe_completion); 935 bus_unregister_notifier(&platform_bus_type, &dev->bus_notifier); 936 937 if (!dev->driver_bound) { 938 /* Probe failed, check kernel log. */ 939 platform_device_unregister(pdev); 940 gpio_sim_remove_hogs(dev); 941 gpio_sim_remove_swnode_recursive(swnode); 942 return -ENXIO; 943 } 944 945 dev->pdev = pdev; 946 947 return 0; 948 } 949 950 static void gpio_sim_device_deactivate_unlocked(struct gpio_sim_device *dev) 951 { 952 struct fwnode_handle *swnode; 953 954 swnode = dev_fwnode(&dev->pdev->dev); 955 platform_device_unregister(dev->pdev); 956 gpio_sim_remove_hogs(dev); 957 gpio_sim_remove_swnode_recursive(swnode); 958 dev->pdev = NULL; 959 } 960 961 static ssize_t 962 gpio_sim_device_config_live_store(struct config_item *item, 963 const char *page, size_t count) 964 { 965 struct gpio_sim_device *dev = to_gpio_sim_device(item); 966 bool live; 967 int ret; 968 969 ret = kstrtobool(page, &live); 970 if (ret) 971 return ret; 972 973 mutex_lock(&dev->lock); 974 975 if ((!live && !gpio_sim_device_is_live_unlocked(dev)) || 976 (live && gpio_sim_device_is_live_unlocked(dev))) 977 ret = -EPERM; 978 else if (live) 979 ret = gpio_sim_device_activate_unlocked(dev); 980 else 981 gpio_sim_device_deactivate_unlocked(dev); 982 983 mutex_unlock(&dev->lock); 984 985 return ret ?: count; 986 } 987 988 CONFIGFS_ATTR(gpio_sim_device_config_, live); 989 990 static struct configfs_attribute *gpio_sim_device_config_attrs[] = { 991 &gpio_sim_device_config_attr_dev_name, 992 &gpio_sim_device_config_attr_live, 993 NULL 994 }; 995 996 struct gpio_sim_chip_name_ctx { 997 struct fwnode_handle *swnode; 998 char *page; 999 }; 1000 1001 static int gpio_sim_emit_chip_name(struct device *dev, void *data) 1002 { 1003 struct gpio_sim_chip_name_ctx *ctx = data; 1004 1005 /* This would be the sysfs device exported in /sys/class/gpio. */ 1006 if (dev->class) 1007 return 0; 1008 1009 if (device_match_fwnode(dev, ctx->swnode)) 1010 return sprintf(ctx->page, "%s\n", dev_name(dev)); 1011 1012 return 0; 1013 } 1014 1015 static ssize_t gpio_sim_bank_config_chip_name_show(struct config_item *item, 1016 char *page) 1017 { 1018 struct gpio_sim_bank *bank = to_gpio_sim_bank(item); 1019 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); 1020 struct gpio_sim_chip_name_ctx ctx = { bank->swnode, page }; 1021 int ret; 1022 1023 mutex_lock(&dev->lock); 1024 if (gpio_sim_device_is_live_unlocked(dev)) 1025 ret = device_for_each_child(&dev->pdev->dev, &ctx, 1026 gpio_sim_emit_chip_name); 1027 else 1028 ret = sprintf(page, "none\n"); 1029 mutex_unlock(&dev->lock); 1030 1031 return ret; 1032 } 1033 1034 CONFIGFS_ATTR_RO(gpio_sim_bank_config_, chip_name); 1035 1036 static ssize_t 1037 gpio_sim_bank_config_label_show(struct config_item *item, char *page) 1038 { 1039 struct gpio_sim_bank *bank = to_gpio_sim_bank(item); 1040 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); 1041 int ret; 1042 1043 mutex_lock(&dev->lock); 1044 ret = sprintf(page, "%s\n", bank->label ?: ""); 1045 mutex_unlock(&dev->lock); 1046 1047 return ret; 1048 } 1049 1050 static ssize_t gpio_sim_bank_config_label_store(struct config_item *item, 1051 const char *page, size_t count) 1052 { 1053 struct gpio_sim_bank *bank = to_gpio_sim_bank(item); 1054 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); 1055 char *trimmed; 1056 1057 mutex_lock(&dev->lock); 1058 1059 if (gpio_sim_device_is_live_unlocked(dev)) { 1060 mutex_unlock(&dev->lock); 1061 return -EBUSY; 1062 } 1063 1064 trimmed = gpio_sim_strdup_trimmed(page, count); 1065 if (!trimmed) { 1066 mutex_unlock(&dev->lock); 1067 return -ENOMEM; 1068 } 1069 1070 kfree(bank->label); 1071 bank->label = trimmed; 1072 1073 mutex_unlock(&dev->lock); 1074 return count; 1075 } 1076 1077 CONFIGFS_ATTR(gpio_sim_bank_config_, label); 1078 1079 static ssize_t 1080 gpio_sim_bank_config_num_lines_show(struct config_item *item, char *page) 1081 { 1082 struct gpio_sim_bank *bank = to_gpio_sim_bank(item); 1083 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); 1084 int ret; 1085 1086 mutex_lock(&dev->lock); 1087 ret = sprintf(page, "%u\n", bank->num_lines); 1088 mutex_unlock(&dev->lock); 1089 1090 return ret; 1091 } 1092 1093 static ssize_t 1094 gpio_sim_bank_config_num_lines_store(struct config_item *item, 1095 const char *page, size_t count) 1096 { 1097 struct gpio_sim_bank *bank = to_gpio_sim_bank(item); 1098 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); 1099 unsigned int num_lines; 1100 int ret; 1101 1102 ret = kstrtouint(page, 0, &num_lines); 1103 if (ret) 1104 return ret; 1105 1106 if (num_lines == 0) 1107 return -EINVAL; 1108 1109 mutex_lock(&dev->lock); 1110 1111 if (gpio_sim_device_is_live_unlocked(dev)) { 1112 mutex_unlock(&dev->lock); 1113 return -EBUSY; 1114 } 1115 1116 bank->num_lines = num_lines; 1117 1118 mutex_unlock(&dev->lock); 1119 return count; 1120 } 1121 1122 CONFIGFS_ATTR(gpio_sim_bank_config_, num_lines); 1123 1124 static struct configfs_attribute *gpio_sim_bank_config_attrs[] = { 1125 &gpio_sim_bank_config_attr_chip_name, 1126 &gpio_sim_bank_config_attr_label, 1127 &gpio_sim_bank_config_attr_num_lines, 1128 NULL 1129 }; 1130 1131 static ssize_t 1132 gpio_sim_line_config_name_show(struct config_item *item, char *page) 1133 { 1134 struct gpio_sim_line *line = to_gpio_sim_line(item); 1135 struct gpio_sim_device *dev = gpio_sim_line_get_device(line); 1136 int ret; 1137 1138 mutex_lock(&dev->lock); 1139 ret = sprintf(page, "%s\n", line->name ?: ""); 1140 mutex_unlock(&dev->lock); 1141 1142 return ret; 1143 } 1144 1145 static ssize_t gpio_sim_line_config_name_store(struct config_item *item, 1146 const char *page, size_t count) 1147 { 1148 struct gpio_sim_line *line = to_gpio_sim_line(item); 1149 struct gpio_sim_device *dev = gpio_sim_line_get_device(line); 1150 char *trimmed; 1151 1152 mutex_lock(&dev->lock); 1153 1154 if (gpio_sim_device_is_live_unlocked(dev)) { 1155 mutex_unlock(&dev->lock); 1156 return -EBUSY; 1157 } 1158 1159 trimmed = gpio_sim_strdup_trimmed(page, count); 1160 if (!trimmed) { 1161 mutex_unlock(&dev->lock); 1162 return -ENOMEM; 1163 } 1164 1165 kfree(line->name); 1166 line->name = trimmed; 1167 1168 mutex_unlock(&dev->lock); 1169 1170 return count; 1171 } 1172 1173 CONFIGFS_ATTR(gpio_sim_line_config_, name); 1174 1175 static struct configfs_attribute *gpio_sim_line_config_attrs[] = { 1176 &gpio_sim_line_config_attr_name, 1177 NULL 1178 }; 1179 1180 static ssize_t gpio_sim_hog_config_name_show(struct config_item *item, 1181 char *page) 1182 { 1183 struct gpio_sim_hog *hog = to_gpio_sim_hog(item); 1184 struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog); 1185 int ret; 1186 1187 mutex_lock(&dev->lock); 1188 ret = sprintf(page, "%s\n", hog->name ?: ""); 1189 mutex_unlock(&dev->lock); 1190 1191 return ret; 1192 } 1193 1194 static ssize_t gpio_sim_hog_config_name_store(struct config_item *item, 1195 const char *page, size_t count) 1196 { 1197 struct gpio_sim_hog *hog = to_gpio_sim_hog(item); 1198 struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog); 1199 char *trimmed; 1200 1201 mutex_lock(&dev->lock); 1202 1203 if (gpio_sim_device_is_live_unlocked(dev)) { 1204 mutex_unlock(&dev->lock); 1205 return -EBUSY; 1206 } 1207 1208 trimmed = gpio_sim_strdup_trimmed(page, count); 1209 if (!trimmed) { 1210 mutex_unlock(&dev->lock); 1211 return -ENOMEM; 1212 } 1213 1214 kfree(hog->name); 1215 hog->name = trimmed; 1216 1217 mutex_unlock(&dev->lock); 1218 1219 return count; 1220 } 1221 1222 CONFIGFS_ATTR(gpio_sim_hog_config_, name); 1223 1224 static ssize_t gpio_sim_hog_config_direction_show(struct config_item *item, 1225 char *page) 1226 { 1227 struct gpio_sim_hog *hog = to_gpio_sim_hog(item); 1228 struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog); 1229 char *repr; 1230 int dir; 1231 1232 mutex_lock(&dev->lock); 1233 dir = hog->dir; 1234 mutex_unlock(&dev->lock); 1235 1236 switch (dir) { 1237 case GPIOD_IN: 1238 repr = "input"; 1239 break; 1240 case GPIOD_OUT_HIGH: 1241 repr = "output-high"; 1242 break; 1243 case GPIOD_OUT_LOW: 1244 repr = "output-low"; 1245 break; 1246 default: 1247 /* This would be a programmer bug. */ 1248 WARN(1, "Unexpected hog direction value: %d", dir); 1249 return -EINVAL; 1250 } 1251 1252 return sprintf(page, "%s\n", repr); 1253 } 1254 1255 static ssize_t 1256 gpio_sim_hog_config_direction_store(struct config_item *item, 1257 const char *page, size_t count) 1258 { 1259 struct gpio_sim_hog *hog = to_gpio_sim_hog(item); 1260 struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog); 1261 char *trimmed; 1262 int dir; 1263 1264 mutex_lock(&dev->lock); 1265 1266 if (gpio_sim_device_is_live_unlocked(dev)) { 1267 mutex_unlock(&dev->lock); 1268 return -EBUSY; 1269 } 1270 1271 trimmed = gpio_sim_strdup_trimmed(page, count); 1272 if (!trimmed) { 1273 mutex_unlock(&dev->lock); 1274 return -ENOMEM; 1275 } 1276 1277 if (strcmp(trimmed, "input") == 0) 1278 dir = GPIOD_IN; 1279 else if (strcmp(trimmed, "output-high") == 0) 1280 dir = GPIOD_OUT_HIGH; 1281 else if (strcmp(trimmed, "output-low") == 0) 1282 dir = GPIOD_OUT_LOW; 1283 else 1284 dir = -EINVAL; 1285 1286 kfree(trimmed); 1287 1288 if (dir < 0) { 1289 mutex_unlock(&dev->lock); 1290 return dir; 1291 } 1292 1293 hog->dir = dir; 1294 1295 mutex_unlock(&dev->lock); 1296 1297 return count; 1298 } 1299 1300 CONFIGFS_ATTR(gpio_sim_hog_config_, direction); 1301 1302 static struct configfs_attribute *gpio_sim_hog_config_attrs[] = { 1303 &gpio_sim_hog_config_attr_name, 1304 &gpio_sim_hog_config_attr_direction, 1305 NULL 1306 }; 1307 1308 static void gpio_sim_hog_config_item_release(struct config_item *item) 1309 { 1310 struct gpio_sim_hog *hog = to_gpio_sim_hog(item); 1311 struct gpio_sim_line *line = hog->parent; 1312 struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog); 1313 1314 mutex_lock(&dev->lock); 1315 line->hog = NULL; 1316 mutex_unlock(&dev->lock); 1317 1318 kfree(hog->name); 1319 kfree(hog); 1320 } 1321 1322 static struct configfs_item_operations gpio_sim_hog_config_item_ops = { 1323 .release = gpio_sim_hog_config_item_release, 1324 }; 1325 1326 static const struct config_item_type gpio_sim_hog_config_type = { 1327 .ct_item_ops = &gpio_sim_hog_config_item_ops, 1328 .ct_attrs = gpio_sim_hog_config_attrs, 1329 .ct_owner = THIS_MODULE, 1330 }; 1331 1332 static struct config_item * 1333 gpio_sim_line_config_make_hog_item(struct config_group *group, const char *name) 1334 { 1335 struct gpio_sim_line *line = to_gpio_sim_line(&group->cg_item); 1336 struct gpio_sim_device *dev = gpio_sim_line_get_device(line); 1337 struct gpio_sim_hog *hog; 1338 1339 if (strcmp(name, "hog") != 0) 1340 return ERR_PTR(-EINVAL); 1341 1342 mutex_lock(&dev->lock); 1343 1344 hog = kzalloc(sizeof(*hog), GFP_KERNEL); 1345 if (!hog) { 1346 mutex_unlock(&dev->lock); 1347 return ERR_PTR(-ENOMEM); 1348 } 1349 1350 config_item_init_type_name(&hog->item, name, 1351 &gpio_sim_hog_config_type); 1352 1353 hog->dir = GPIOD_IN; 1354 hog->name = NULL; 1355 hog->parent = line; 1356 line->hog = hog; 1357 1358 mutex_unlock(&dev->lock); 1359 1360 return &hog->item; 1361 } 1362 1363 static void gpio_sim_line_config_group_release(struct config_item *item) 1364 { 1365 struct gpio_sim_line *line = to_gpio_sim_line(item); 1366 struct gpio_sim_device *dev = gpio_sim_line_get_device(line); 1367 1368 mutex_lock(&dev->lock); 1369 list_del(&line->siblings); 1370 mutex_unlock(&dev->lock); 1371 1372 kfree(line->name); 1373 kfree(line); 1374 } 1375 1376 static struct configfs_item_operations gpio_sim_line_config_item_ops = { 1377 .release = gpio_sim_line_config_group_release, 1378 }; 1379 1380 static struct configfs_group_operations gpio_sim_line_config_group_ops = { 1381 .make_item = gpio_sim_line_config_make_hog_item, 1382 }; 1383 1384 static const struct config_item_type gpio_sim_line_config_type = { 1385 .ct_item_ops = &gpio_sim_line_config_item_ops, 1386 .ct_group_ops = &gpio_sim_line_config_group_ops, 1387 .ct_attrs = gpio_sim_line_config_attrs, 1388 .ct_owner = THIS_MODULE, 1389 }; 1390 1391 static struct config_group * 1392 gpio_sim_bank_config_make_line_group(struct config_group *group, 1393 const char *name) 1394 { 1395 struct gpio_sim_bank *bank = to_gpio_sim_bank(&group->cg_item); 1396 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); 1397 struct gpio_sim_line *line; 1398 unsigned int offset; 1399 int ret, nchar; 1400 1401 ret = sscanf(name, "line%u%n", &offset, &nchar); 1402 if (ret != 1 || nchar != strlen(name)) 1403 return ERR_PTR(-EINVAL); 1404 1405 mutex_lock(&dev->lock); 1406 1407 if (gpio_sim_device_is_live_unlocked(dev)) { 1408 mutex_unlock(&dev->lock); 1409 return ERR_PTR(-EBUSY); 1410 } 1411 1412 line = kzalloc(sizeof(*line), GFP_KERNEL); 1413 if (!line) { 1414 mutex_unlock(&dev->lock); 1415 return ERR_PTR(-ENOMEM); 1416 } 1417 1418 config_group_init_type_name(&line->group, name, 1419 &gpio_sim_line_config_type); 1420 1421 line->parent = bank; 1422 line->offset = offset; 1423 list_add_tail(&line->siblings, &bank->line_list); 1424 1425 mutex_unlock(&dev->lock); 1426 1427 return &line->group; 1428 } 1429 1430 static void gpio_sim_bank_config_group_release(struct config_item *item) 1431 { 1432 struct gpio_sim_bank *bank = to_gpio_sim_bank(item); 1433 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); 1434 1435 mutex_lock(&dev->lock); 1436 list_del(&bank->siblings); 1437 mutex_unlock(&dev->lock); 1438 1439 kfree(bank->label); 1440 kfree(bank); 1441 } 1442 1443 static struct configfs_item_operations gpio_sim_bank_config_item_ops = { 1444 .release = gpio_sim_bank_config_group_release, 1445 }; 1446 1447 static struct configfs_group_operations gpio_sim_bank_config_group_ops = { 1448 .make_group = gpio_sim_bank_config_make_line_group, 1449 }; 1450 1451 static const struct config_item_type gpio_sim_bank_config_group_type = { 1452 .ct_item_ops = &gpio_sim_bank_config_item_ops, 1453 .ct_group_ops = &gpio_sim_bank_config_group_ops, 1454 .ct_attrs = gpio_sim_bank_config_attrs, 1455 .ct_owner = THIS_MODULE, 1456 }; 1457 1458 static struct config_group * 1459 gpio_sim_device_config_make_bank_group(struct config_group *group, 1460 const char *name) 1461 { 1462 struct gpio_sim_device *dev = to_gpio_sim_device(&group->cg_item); 1463 struct gpio_sim_bank *bank; 1464 1465 mutex_lock(&dev->lock); 1466 1467 if (gpio_sim_device_is_live_unlocked(dev)) { 1468 mutex_unlock(&dev->lock); 1469 return ERR_PTR(-EBUSY); 1470 } 1471 1472 bank = kzalloc(sizeof(*bank), GFP_KERNEL); 1473 if (!bank) { 1474 mutex_unlock(&dev->lock); 1475 return ERR_PTR(-ENOMEM); 1476 } 1477 1478 config_group_init_type_name(&bank->group, name, 1479 &gpio_sim_bank_config_group_type); 1480 bank->num_lines = 1; 1481 bank->parent = dev; 1482 INIT_LIST_HEAD(&bank->line_list); 1483 list_add_tail(&bank->siblings, &dev->bank_list); 1484 1485 mutex_unlock(&dev->lock); 1486 1487 return &bank->group; 1488 } 1489 1490 static void gpio_sim_device_config_group_release(struct config_item *item) 1491 { 1492 struct gpio_sim_device *dev = to_gpio_sim_device(item); 1493 1494 mutex_lock(&dev->lock); 1495 if (gpio_sim_device_is_live_unlocked(dev)) 1496 gpio_sim_device_deactivate_unlocked(dev); 1497 mutex_unlock(&dev->lock); 1498 1499 mutex_destroy(&dev->lock); 1500 ida_free(&gpio_sim_ida, dev->id); 1501 kfree(dev); 1502 } 1503 1504 static struct configfs_item_operations gpio_sim_device_config_item_ops = { 1505 .release = gpio_sim_device_config_group_release, 1506 }; 1507 1508 static struct configfs_group_operations gpio_sim_device_config_group_ops = { 1509 .make_group = gpio_sim_device_config_make_bank_group, 1510 }; 1511 1512 static const struct config_item_type gpio_sim_device_config_group_type = { 1513 .ct_item_ops = &gpio_sim_device_config_item_ops, 1514 .ct_group_ops = &gpio_sim_device_config_group_ops, 1515 .ct_attrs = gpio_sim_device_config_attrs, 1516 .ct_owner = THIS_MODULE, 1517 }; 1518 1519 static struct config_group * 1520 gpio_sim_config_make_device_group(struct config_group *group, const char *name) 1521 { 1522 struct gpio_sim_device *dev; 1523 int id; 1524 1525 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1526 if (!dev) 1527 return ERR_PTR(-ENOMEM); 1528 1529 id = ida_alloc(&gpio_sim_ida, GFP_KERNEL); 1530 if (id < 0) { 1531 kfree(dev); 1532 return ERR_PTR(id); 1533 } 1534 1535 config_group_init_type_name(&dev->group, name, 1536 &gpio_sim_device_config_group_type); 1537 dev->id = id; 1538 mutex_init(&dev->lock); 1539 INIT_LIST_HEAD(&dev->bank_list); 1540 1541 dev->bus_notifier.notifier_call = gpio_sim_bus_notifier_call; 1542 init_completion(&dev->probe_completion); 1543 1544 return &dev->group; 1545 } 1546 1547 static struct configfs_group_operations gpio_sim_config_group_ops = { 1548 .make_group = gpio_sim_config_make_device_group, 1549 }; 1550 1551 static const struct config_item_type gpio_sim_config_type = { 1552 .ct_group_ops = &gpio_sim_config_group_ops, 1553 .ct_owner = THIS_MODULE, 1554 }; 1555 1556 static struct configfs_subsystem gpio_sim_config_subsys = { 1557 .su_group = { 1558 .cg_item = { 1559 .ci_namebuf = "gpio-sim", 1560 .ci_type = &gpio_sim_config_type, 1561 }, 1562 }, 1563 }; 1564 1565 static int __init gpio_sim_init(void) 1566 { 1567 int ret; 1568 1569 ret = platform_driver_register(&gpio_sim_driver); 1570 if (ret) { 1571 pr_err("Error %d while registering the platform driver\n", ret); 1572 return ret; 1573 } 1574 1575 config_group_init(&gpio_sim_config_subsys.su_group); 1576 mutex_init(&gpio_sim_config_subsys.su_mutex); 1577 ret = configfs_register_subsystem(&gpio_sim_config_subsys); 1578 if (ret) { 1579 pr_err("Error %d while registering the configfs subsystem %s\n", 1580 ret, gpio_sim_config_subsys.su_group.cg_item.ci_namebuf); 1581 mutex_destroy(&gpio_sim_config_subsys.su_mutex); 1582 platform_driver_unregister(&gpio_sim_driver); 1583 return ret; 1584 } 1585 1586 return 0; 1587 } 1588 module_init(gpio_sim_init); 1589 1590 static void __exit gpio_sim_exit(void) 1591 { 1592 configfs_unregister_subsystem(&gpio_sim_config_subsys); 1593 mutex_destroy(&gpio_sim_config_subsys.su_mutex); 1594 platform_driver_unregister(&gpio_sim_driver); 1595 } 1596 module_exit(gpio_sim_exit); 1597 1598 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl"); 1599 MODULE_DESCRIPTION("GPIO Simulator Module"); 1600 MODULE_LICENSE("GPL"); 1601