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