Bartosz Golaszewski | cb8c474 | 2021-12-07 10:34:09 +0100 | [diff] [blame] | 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); |
Yang Yingliang | a2d05fb | 2021-12-21 19:38:25 +0800 | [diff] [blame] | 447 | if (ret) { |
| 448 | fwnode_handle_put(swnode); |
Bartosz Golaszewski | cb8c474 | 2021-12-07 10:34:09 +0100 | [diff] [blame] | 449 | return ret; |
Yang Yingliang | a2d05fb | 2021-12-21 19:38:25 +0800 | [diff] [blame] | 450 | } |
Bartosz Golaszewski | cb8c474 | 2021-12-07 10:34:09 +0100 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | static const struct of_device_id gpio_sim_of_match[] = { |
| 457 | { .compatible = "gpio-simulator" }, |
| 458 | { } |
| 459 | }; |
| 460 | MODULE_DEVICE_TABLE(of, gpio_sim_of_match); |
| 461 | |
| 462 | static struct platform_driver gpio_sim_driver = { |
| 463 | .driver = { |
| 464 | .name = "gpio-sim", |
| 465 | .of_match_table = gpio_sim_of_match, |
| 466 | }, |
| 467 | .probe = gpio_sim_probe, |
| 468 | }; |
| 469 | |
| 470 | struct gpio_sim_device { |
| 471 | struct config_group group; |
| 472 | |
| 473 | /* |
| 474 | * If pdev is NULL, the device is 'pending' (waiting for configuration). |
| 475 | * Once the pointer is assigned, the device has been created and the |
| 476 | * item is 'live'. |
| 477 | */ |
| 478 | struct platform_device *pdev; |
| 479 | int id; |
| 480 | |
| 481 | /* |
| 482 | * Each configfs filesystem operation is protected with the subsystem |
| 483 | * mutex. Each separate attribute is protected with the buffer mutex. |
| 484 | * This structure however can be modified by callbacks of different |
| 485 | * attributes so we need another lock. |
| 486 | * |
| 487 | * We use this lock fo protecting all data structures owned by this |
| 488 | * object too. |
| 489 | */ |
| 490 | struct mutex lock; |
| 491 | |
| 492 | /* |
| 493 | * This is used to synchronously wait for the driver's probe to complete |
| 494 | * and notify the user-space about any errors. |
| 495 | */ |
| 496 | struct notifier_block bus_notifier; |
| 497 | struct completion probe_completion; |
| 498 | bool driver_bound; |
| 499 | |
| 500 | struct gpiod_hog *hogs; |
| 501 | |
| 502 | struct list_head bank_list; |
| 503 | }; |
| 504 | |
| 505 | /* This is called with dev->lock already taken. */ |
| 506 | static int gpio_sim_bus_notifier_call(struct notifier_block *nb, |
| 507 | unsigned long action, void *data) |
| 508 | { |
| 509 | struct gpio_sim_device *simdev = container_of(nb, |
| 510 | struct gpio_sim_device, |
| 511 | bus_notifier); |
| 512 | struct device *dev = data; |
| 513 | char devname[32]; |
| 514 | |
| 515 | snprintf(devname, sizeof(devname), "gpio-sim.%u", simdev->id); |
| 516 | |
| 517 | if (strcmp(dev_name(dev), devname) == 0) { |
| 518 | if (action == BUS_NOTIFY_BOUND_DRIVER) |
| 519 | simdev->driver_bound = true; |
| 520 | else if (action == BUS_NOTIFY_DRIVER_NOT_BOUND) |
| 521 | simdev->driver_bound = false; |
| 522 | else |
| 523 | return NOTIFY_DONE; |
| 524 | |
| 525 | complete(&simdev->probe_completion); |
| 526 | return NOTIFY_OK; |
| 527 | } |
| 528 | |
| 529 | return NOTIFY_DONE; |
| 530 | } |
| 531 | |
| 532 | static struct gpio_sim_device *to_gpio_sim_device(struct config_item *item) |
| 533 | { |
| 534 | struct config_group *group = to_config_group(item); |
| 535 | |
| 536 | return container_of(group, struct gpio_sim_device, group); |
| 537 | } |
| 538 | |
| 539 | struct gpio_sim_bank { |
| 540 | struct config_group group; |
| 541 | |
| 542 | /* |
| 543 | * We could have used the ci_parent field of the config_item but |
| 544 | * configfs is stupid and calls the item's release callback after |
| 545 | * already having cleared the parent pointer even though the parent |
| 546 | * is guaranteed to survive the child... |
| 547 | * |
| 548 | * So we need to store the pointer to the parent struct here. We can |
| 549 | * dereference it anywhere we need with no checks and no locking as |
| 550 | * it's guaranteed to survive the childred and protected by configfs |
| 551 | * locks. |
| 552 | * |
| 553 | * Same for other structures. |
| 554 | */ |
| 555 | struct gpio_sim_device *parent; |
| 556 | struct list_head siblings; |
| 557 | |
| 558 | char *label; |
| 559 | unsigned int num_lines; |
| 560 | |
| 561 | struct list_head line_list; |
| 562 | |
| 563 | struct fwnode_handle *swnode; |
| 564 | }; |
| 565 | |
| 566 | static struct gpio_sim_bank *to_gpio_sim_bank(struct config_item *item) |
| 567 | { |
| 568 | struct config_group *group = to_config_group(item); |
| 569 | |
| 570 | return container_of(group, struct gpio_sim_bank, group); |
| 571 | } |
| 572 | |
| 573 | static struct gpio_sim_device * |
| 574 | gpio_sim_bank_get_device(struct gpio_sim_bank *bank) |
| 575 | { |
| 576 | return bank->parent; |
| 577 | } |
| 578 | |
| 579 | struct gpio_sim_hog; |
| 580 | |
| 581 | struct gpio_sim_line { |
| 582 | struct config_group group; |
| 583 | |
| 584 | struct gpio_sim_bank *parent; |
| 585 | struct list_head siblings; |
| 586 | |
| 587 | unsigned int offset; |
| 588 | char *name; |
| 589 | |
| 590 | /* There can only be one hog per line. */ |
| 591 | struct gpio_sim_hog *hog; |
| 592 | }; |
| 593 | |
| 594 | static struct gpio_sim_line *to_gpio_sim_line(struct config_item *item) |
| 595 | { |
| 596 | struct config_group *group = to_config_group(item); |
| 597 | |
| 598 | return container_of(group, struct gpio_sim_line, group); |
| 599 | } |
| 600 | |
| 601 | static struct gpio_sim_device * |
| 602 | gpio_sim_line_get_device(struct gpio_sim_line *line) |
| 603 | { |
| 604 | struct gpio_sim_bank *bank = line->parent; |
| 605 | |
| 606 | return gpio_sim_bank_get_device(bank); |
| 607 | } |
| 608 | |
| 609 | struct gpio_sim_hog { |
| 610 | struct config_item item; |
| 611 | struct gpio_sim_line *parent; |
| 612 | |
| 613 | char *name; |
| 614 | int dir; |
| 615 | }; |
| 616 | |
| 617 | static struct gpio_sim_hog *to_gpio_sim_hog(struct config_item *item) |
| 618 | { |
| 619 | return container_of(item, struct gpio_sim_hog, item); |
| 620 | } |
| 621 | |
| 622 | static struct gpio_sim_device *gpio_sim_hog_get_device(struct gpio_sim_hog *hog) |
| 623 | { |
| 624 | struct gpio_sim_line *line = hog->parent; |
| 625 | |
| 626 | return gpio_sim_line_get_device(line); |
| 627 | } |
| 628 | |
| 629 | static bool gpio_sim_device_is_live_unlocked(struct gpio_sim_device *dev) |
| 630 | { |
| 631 | return !!dev->pdev; |
| 632 | } |
| 633 | |
| 634 | static char *gpio_sim_strdup_trimmed(const char *str, size_t count) |
| 635 | { |
| 636 | char *dup, *trimmed; |
| 637 | |
| 638 | dup = kstrndup(str, count, GFP_KERNEL); |
| 639 | if (!dup) |
| 640 | return NULL; |
| 641 | |
| 642 | trimmed = strstrip(dup); |
| 643 | memmove(dup, trimmed, strlen(trimmed) + 1); |
| 644 | |
| 645 | return dup; |
| 646 | } |
| 647 | |
| 648 | static ssize_t gpio_sim_device_config_dev_name_show(struct config_item *item, |
| 649 | char *page) |
| 650 | { |
| 651 | struct gpio_sim_device *dev = to_gpio_sim_device(item); |
| 652 | struct platform_device *pdev; |
| 653 | int ret; |
| 654 | |
| 655 | mutex_lock(&dev->lock); |
| 656 | pdev = dev->pdev; |
| 657 | if (pdev) |
| 658 | ret = sprintf(page, "%s\n", dev_name(&pdev->dev)); |
| 659 | else |
| 660 | ret = sprintf(page, "gpio-sim.%d\n", dev->id); |
| 661 | mutex_unlock(&dev->lock); |
| 662 | |
| 663 | return ret; |
| 664 | } |
| 665 | |
| 666 | CONFIGFS_ATTR_RO(gpio_sim_device_config_, dev_name); |
| 667 | |
| 668 | static ssize_t |
| 669 | gpio_sim_device_config_live_show(struct config_item *item, char *page) |
| 670 | { |
| 671 | struct gpio_sim_device *dev = to_gpio_sim_device(item); |
| 672 | bool live; |
| 673 | |
| 674 | mutex_lock(&dev->lock); |
| 675 | live = gpio_sim_device_is_live_unlocked(dev); |
| 676 | mutex_unlock(&dev->lock); |
| 677 | |
| 678 | return sprintf(page, "%c\n", live ? '1' : '0'); |
| 679 | } |
| 680 | |
| 681 | static char **gpio_sim_make_line_names(struct gpio_sim_bank *bank, |
| 682 | unsigned int *line_names_size) |
| 683 | { |
| 684 | unsigned int max_offset = 0; |
| 685 | bool has_line_names = false; |
| 686 | struct gpio_sim_line *line; |
| 687 | char **line_names; |
| 688 | |
| 689 | list_for_each_entry(line, &bank->line_list, siblings) { |
| 690 | if (line->name) { |
| 691 | if (line->offset > max_offset) |
| 692 | max_offset = line->offset; |
| 693 | |
| 694 | /* |
| 695 | * max_offset can stay at 0 so it's not an indicator |
| 696 | * of whether line names were configured at all. |
| 697 | */ |
| 698 | has_line_names = true; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | if (!has_line_names) |
| 703 | /* |
| 704 | * This is not an error - NULL means, there are no line |
| 705 | * names configured. |
| 706 | */ |
| 707 | return NULL; |
| 708 | |
| 709 | *line_names_size = max_offset + 1; |
| 710 | |
| 711 | line_names = kcalloc(*line_names_size, sizeof(*line_names), GFP_KERNEL); |
| 712 | if (!line_names) |
| 713 | return ERR_PTR(-ENOMEM); |
| 714 | |
| 715 | list_for_each_entry(line, &bank->line_list, siblings) |
| 716 | line_names[line->offset] = line->name; |
| 717 | |
| 718 | return line_names; |
| 719 | } |
| 720 | |
| 721 | static void gpio_sim_remove_hogs(struct gpio_sim_device *dev) |
| 722 | { |
| 723 | struct gpiod_hog *hog; |
| 724 | |
| 725 | if (!dev->hogs) |
| 726 | return; |
| 727 | |
| 728 | gpiod_remove_hogs(dev->hogs); |
| 729 | |
| 730 | for (hog = dev->hogs; !hog->chip_label; hog++) { |
| 731 | kfree(hog->chip_label); |
| 732 | kfree(hog->line_name); |
| 733 | } |
| 734 | |
| 735 | kfree(dev->hogs); |
| 736 | dev->hogs = NULL; |
| 737 | } |
| 738 | |
| 739 | static int gpio_sim_add_hogs(struct gpio_sim_device *dev) |
| 740 | { |
| 741 | unsigned int num_hogs = 0, idx = 0; |
| 742 | struct gpio_sim_bank *bank; |
| 743 | struct gpio_sim_line *line; |
| 744 | struct gpiod_hog *hog; |
| 745 | |
| 746 | list_for_each_entry(bank, &dev->bank_list, siblings) { |
| 747 | list_for_each_entry(line, &bank->line_list, siblings) { |
| 748 | if (line->hog) |
| 749 | num_hogs++; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | if (!num_hogs) |
| 754 | return 0; |
| 755 | |
| 756 | /* Allocate one more for the sentinel. */ |
| 757 | dev->hogs = kcalloc(num_hogs + 1, sizeof(*dev->hogs), GFP_KERNEL); |
| 758 | if (!dev->hogs) |
| 759 | return -ENOMEM; |
| 760 | |
| 761 | list_for_each_entry(bank, &dev->bank_list, siblings) { |
| 762 | list_for_each_entry(line, &bank->line_list, siblings) { |
| 763 | if (!line->hog) |
| 764 | continue; |
| 765 | |
| 766 | hog = &dev->hogs[idx++]; |
| 767 | |
| 768 | /* |
| 769 | * We need to make this string manually because at this |
| 770 | * point the device doesn't exist yet and so dev_name() |
| 771 | * is not available. |
| 772 | */ |
| 773 | hog->chip_label = kasprintf(GFP_KERNEL, |
| 774 | "gpio-sim.%u-%s", dev->id, |
| 775 | fwnode_get_name(bank->swnode)); |
| 776 | if (!hog->chip_label) { |
| 777 | gpio_sim_remove_hogs(dev); |
| 778 | return -ENOMEM; |
| 779 | } |
| 780 | |
| 781 | /* |
| 782 | * We need to duplicate this because the hog config |
| 783 | * item can be removed at any time (and we can't block |
| 784 | * it) and gpiolib doesn't make a deep copy of the hog |
| 785 | * data. |
| 786 | */ |
| 787 | if (line->hog->name) { |
| 788 | hog->line_name = kstrdup(line->hog->name, |
| 789 | GFP_KERNEL); |
| 790 | if (!hog->line_name) { |
| 791 | gpio_sim_remove_hogs(dev); |
| 792 | return -ENOMEM; |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | hog->chip_hwnum = line->offset; |
| 797 | hog->dflags = line->hog->dir; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | gpiod_add_hogs(dev->hogs); |
| 802 | |
| 803 | return 0; |
| 804 | } |
| 805 | |
| 806 | static struct fwnode_handle * |
| 807 | gpio_sim_make_bank_swnode(struct gpio_sim_bank *bank, |
| 808 | struct fwnode_handle *parent) |
| 809 | { |
| 810 | struct property_entry properties[GPIO_SIM_PROP_MAX]; |
| 811 | unsigned int prop_idx = 0, line_names_size = 0; |
| 812 | struct fwnode_handle *swnode; |
| 813 | char **line_names; |
| 814 | |
| 815 | memset(properties, 0, sizeof(properties)); |
| 816 | |
| 817 | properties[prop_idx++] = PROPERTY_ENTRY_U32("ngpios", bank->num_lines); |
| 818 | |
Bartosz Golaszewski | dda8e14 | 2022-01-20 20:49:48 +0100 | [diff] [blame^] | 819 | if (bank->label && (strlen(bank->label) > 0)) |
Bartosz Golaszewski | cb8c474 | 2021-12-07 10:34:09 +0100 | [diff] [blame] | 820 | properties[prop_idx++] = PROPERTY_ENTRY_STRING("gpio-sim,label", |
| 821 | bank->label); |
| 822 | |
| 823 | line_names = gpio_sim_make_line_names(bank, &line_names_size); |
| 824 | if (IS_ERR(line_names)) |
| 825 | return ERR_CAST(line_names); |
| 826 | |
| 827 | if (line_names) |
| 828 | properties[prop_idx++] = PROPERTY_ENTRY_STRING_ARRAY_LEN( |
| 829 | "gpio-line-names", |
| 830 | line_names, line_names_size); |
| 831 | |
| 832 | swnode = fwnode_create_software_node(properties, parent); |
| 833 | kfree(line_names); |
| 834 | return swnode; |
| 835 | } |
| 836 | |
| 837 | static void gpio_sim_remove_swnode_recursive(struct fwnode_handle *swnode) |
| 838 | { |
| 839 | struct fwnode_handle *child; |
| 840 | |
| 841 | fwnode_for_each_child_node(swnode, child) |
| 842 | fwnode_remove_software_node(child); |
| 843 | |
| 844 | fwnode_remove_software_node(swnode); |
| 845 | } |
| 846 | |
| 847 | static bool gpio_sim_bank_labels_non_unique(struct gpio_sim_device *dev) |
| 848 | { |
| 849 | struct gpio_sim_bank *this, *pos; |
| 850 | |
| 851 | list_for_each_entry(this, &dev->bank_list, siblings) { |
| 852 | list_for_each_entry(pos, &dev->bank_list, siblings) { |
| 853 | if (this == pos || (!this->label || !pos->label)) |
| 854 | continue; |
| 855 | |
| 856 | if (strcmp(this->label, pos->label) == 0) |
| 857 | return true; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | return false; |
| 862 | } |
| 863 | |
| 864 | static int gpio_sim_device_activate_unlocked(struct gpio_sim_device *dev) |
| 865 | { |
| 866 | struct platform_device_info pdevinfo; |
| 867 | struct fwnode_handle *swnode; |
| 868 | struct platform_device *pdev; |
| 869 | struct gpio_sim_bank *bank; |
| 870 | int ret; |
| 871 | |
| 872 | if (list_empty(&dev->bank_list)) |
| 873 | return -ENODATA; |
| 874 | |
| 875 | /* |
| 876 | * Non-unique GPIO device labels are a corner-case we don't support |
| 877 | * as it would interfere with machine hogging mechanism and has little |
| 878 | * use in real life. |
| 879 | */ |
| 880 | if (gpio_sim_bank_labels_non_unique(dev)) |
| 881 | return -EINVAL; |
| 882 | |
| 883 | memset(&pdevinfo, 0, sizeof(pdevinfo)); |
| 884 | |
| 885 | swnode = fwnode_create_software_node(NULL, NULL); |
| 886 | if (IS_ERR(swnode)) |
| 887 | return PTR_ERR(swnode); |
| 888 | |
| 889 | list_for_each_entry(bank, &dev->bank_list, siblings) { |
| 890 | bank->swnode = gpio_sim_make_bank_swnode(bank, swnode); |
Tom Rix | c08995b | 2021-12-18 07:27:12 -0800 | [diff] [blame] | 891 | if (IS_ERR(bank->swnode)) { |
| 892 | ret = PTR_ERR(bank->swnode); |
Bartosz Golaszewski | cb8c474 | 2021-12-07 10:34:09 +0100 | [diff] [blame] | 893 | gpio_sim_remove_swnode_recursive(swnode); |
| 894 | return ret; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | ret = gpio_sim_add_hogs(dev); |
| 899 | if (ret) { |
| 900 | gpio_sim_remove_swnode_recursive(swnode); |
| 901 | return ret; |
| 902 | } |
| 903 | |
| 904 | pdevinfo.name = "gpio-sim"; |
| 905 | pdevinfo.fwnode = swnode; |
| 906 | pdevinfo.id = dev->id; |
| 907 | |
| 908 | reinit_completion(&dev->probe_completion); |
| 909 | dev->driver_bound = false; |
| 910 | bus_register_notifier(&platform_bus_type, &dev->bus_notifier); |
| 911 | |
| 912 | pdev = platform_device_register_full(&pdevinfo); |
| 913 | if (IS_ERR(pdev)) { |
| 914 | bus_unregister_notifier(&platform_bus_type, &dev->bus_notifier); |
| 915 | gpio_sim_remove_hogs(dev); |
| 916 | gpio_sim_remove_swnode_recursive(swnode); |
| 917 | return PTR_ERR(pdev); |
| 918 | } |
| 919 | |
| 920 | wait_for_completion(&dev->probe_completion); |
| 921 | bus_unregister_notifier(&platform_bus_type, &dev->bus_notifier); |
| 922 | |
| 923 | if (!dev->driver_bound) { |
| 924 | /* Probe failed, check kernel log. */ |
| 925 | platform_device_unregister(pdev); |
| 926 | gpio_sim_remove_hogs(dev); |
| 927 | gpio_sim_remove_swnode_recursive(swnode); |
| 928 | return -ENXIO; |
| 929 | } |
| 930 | |
| 931 | dev->pdev = pdev; |
| 932 | |
| 933 | return 0; |
| 934 | } |
| 935 | |
| 936 | static void gpio_sim_device_deactivate_unlocked(struct gpio_sim_device *dev) |
| 937 | { |
| 938 | struct fwnode_handle *swnode; |
| 939 | |
| 940 | swnode = dev_fwnode(&dev->pdev->dev); |
| 941 | platform_device_unregister(dev->pdev); |
| 942 | gpio_sim_remove_swnode_recursive(swnode); |
| 943 | dev->pdev = NULL; |
| 944 | gpio_sim_remove_hogs(dev); |
| 945 | } |
| 946 | |
| 947 | static ssize_t |
| 948 | gpio_sim_device_config_live_store(struct config_item *item, |
| 949 | const char *page, size_t count) |
| 950 | { |
| 951 | struct gpio_sim_device *dev = to_gpio_sim_device(item); |
| 952 | bool live; |
| 953 | int ret; |
| 954 | |
| 955 | ret = kstrtobool(page, &live); |
| 956 | if (ret) |
| 957 | return ret; |
| 958 | |
| 959 | mutex_lock(&dev->lock); |
| 960 | |
| 961 | if ((!live && !gpio_sim_device_is_live_unlocked(dev)) || |
| 962 | (live && gpio_sim_device_is_live_unlocked(dev))) |
| 963 | ret = -EPERM; |
| 964 | else if (live) |
| 965 | ret = gpio_sim_device_activate_unlocked(dev); |
| 966 | else |
| 967 | gpio_sim_device_deactivate_unlocked(dev); |
| 968 | |
| 969 | mutex_unlock(&dev->lock); |
| 970 | |
| 971 | return ret ?: count; |
| 972 | } |
| 973 | |
| 974 | CONFIGFS_ATTR(gpio_sim_device_config_, live); |
| 975 | |
| 976 | static struct configfs_attribute *gpio_sim_device_config_attrs[] = { |
| 977 | &gpio_sim_device_config_attr_dev_name, |
| 978 | &gpio_sim_device_config_attr_live, |
| 979 | NULL |
| 980 | }; |
| 981 | |
| 982 | struct gpio_sim_chip_name_ctx { |
| 983 | struct gpio_sim_device *dev; |
| 984 | char *page; |
| 985 | }; |
| 986 | |
| 987 | static int gpio_sim_emit_chip_name(struct device *dev, void *data) |
| 988 | { |
| 989 | struct gpio_sim_chip_name_ctx *ctx = data; |
| 990 | struct fwnode_handle *swnode; |
| 991 | struct gpio_sim_bank *bank; |
| 992 | |
| 993 | /* This would be the sysfs device exported in /sys/class/gpio. */ |
| 994 | if (dev->class) |
| 995 | return 0; |
| 996 | |
| 997 | swnode = dev_fwnode(dev); |
| 998 | |
| 999 | list_for_each_entry(bank, &ctx->dev->bank_list, siblings) { |
| 1000 | if (bank->swnode == swnode) |
| 1001 | return sprintf(ctx->page, "%s\n", dev_name(dev)); |
| 1002 | } |
| 1003 | |
| 1004 | return -ENODATA; |
| 1005 | } |
| 1006 | |
| 1007 | static ssize_t gpio_sim_bank_config_chip_name_show(struct config_item *item, |
| 1008 | char *page) |
| 1009 | { |
| 1010 | struct gpio_sim_bank *bank = to_gpio_sim_bank(item); |
| 1011 | struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); |
| 1012 | struct gpio_sim_chip_name_ctx ctx = { dev, page }; |
| 1013 | int ret; |
| 1014 | |
| 1015 | mutex_lock(&dev->lock); |
| 1016 | if (gpio_sim_device_is_live_unlocked(dev)) |
| 1017 | ret = device_for_each_child(&dev->pdev->dev, &ctx, |
| 1018 | gpio_sim_emit_chip_name); |
| 1019 | else |
| 1020 | ret = sprintf(page, "none\n"); |
| 1021 | mutex_unlock(&dev->lock); |
| 1022 | |
| 1023 | return ret; |
| 1024 | } |
| 1025 | |
| 1026 | CONFIGFS_ATTR_RO(gpio_sim_bank_config_, chip_name); |
| 1027 | |
| 1028 | static ssize_t |
| 1029 | gpio_sim_bank_config_label_show(struct config_item *item, char *page) |
| 1030 | { |
| 1031 | struct gpio_sim_bank *bank = to_gpio_sim_bank(item); |
| 1032 | struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); |
| 1033 | int ret; |
| 1034 | |
| 1035 | mutex_lock(&dev->lock); |
| 1036 | ret = sprintf(page, "%s\n", bank->label ?: ""); |
| 1037 | mutex_unlock(&dev->lock); |
| 1038 | |
| 1039 | return ret; |
| 1040 | } |
| 1041 | |
| 1042 | static ssize_t gpio_sim_bank_config_label_store(struct config_item *item, |
| 1043 | const char *page, size_t count) |
| 1044 | { |
| 1045 | struct gpio_sim_bank *bank = to_gpio_sim_bank(item); |
| 1046 | struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); |
| 1047 | char *trimmed; |
| 1048 | |
| 1049 | mutex_lock(&dev->lock); |
| 1050 | |
| 1051 | if (gpio_sim_device_is_live_unlocked(dev)) { |
| 1052 | mutex_unlock(&dev->lock); |
| 1053 | return -EBUSY; |
| 1054 | } |
| 1055 | |
| 1056 | trimmed = gpio_sim_strdup_trimmed(page, count); |
| 1057 | if (!trimmed) { |
| 1058 | mutex_unlock(&dev->lock); |
| 1059 | return -ENOMEM; |
| 1060 | } |
| 1061 | |
| 1062 | kfree(bank->label); |
| 1063 | bank->label = trimmed; |
| 1064 | |
| 1065 | mutex_unlock(&dev->lock); |
| 1066 | return count; |
| 1067 | } |
| 1068 | |
| 1069 | CONFIGFS_ATTR(gpio_sim_bank_config_, label); |
| 1070 | |
| 1071 | static ssize_t |
| 1072 | gpio_sim_bank_config_num_lines_show(struct config_item *item, char *page) |
| 1073 | { |
| 1074 | struct gpio_sim_bank *bank = to_gpio_sim_bank(item); |
| 1075 | struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); |
| 1076 | int ret; |
| 1077 | |
| 1078 | mutex_lock(&dev->lock); |
| 1079 | ret = sprintf(page, "%u\n", bank->num_lines); |
| 1080 | mutex_unlock(&dev->lock); |
| 1081 | |
| 1082 | return ret; |
| 1083 | } |
| 1084 | |
| 1085 | static ssize_t |
| 1086 | gpio_sim_bank_config_num_lines_store(struct config_item *item, |
| 1087 | const char *page, size_t count) |
| 1088 | { |
| 1089 | struct gpio_sim_bank *bank = to_gpio_sim_bank(item); |
| 1090 | struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); |
| 1091 | unsigned int num_lines; |
| 1092 | int ret; |
| 1093 | |
| 1094 | ret = kstrtouint(page, 0, &num_lines); |
| 1095 | if (ret) |
| 1096 | return ret; |
| 1097 | |
| 1098 | if (num_lines == 0) |
| 1099 | return -EINVAL; |
| 1100 | |
| 1101 | mutex_lock(&dev->lock); |
| 1102 | |
| 1103 | if (gpio_sim_device_is_live_unlocked(dev)) { |
| 1104 | mutex_unlock(&dev->lock); |
| 1105 | return -EBUSY; |
| 1106 | } |
| 1107 | |
| 1108 | bank->num_lines = num_lines; |
| 1109 | |
| 1110 | mutex_unlock(&dev->lock); |
| 1111 | return count; |
| 1112 | } |
| 1113 | |
| 1114 | CONFIGFS_ATTR(gpio_sim_bank_config_, num_lines); |
| 1115 | |
| 1116 | static struct configfs_attribute *gpio_sim_bank_config_attrs[] = { |
| 1117 | &gpio_sim_bank_config_attr_chip_name, |
| 1118 | &gpio_sim_bank_config_attr_label, |
| 1119 | &gpio_sim_bank_config_attr_num_lines, |
| 1120 | NULL |
| 1121 | }; |
| 1122 | |
| 1123 | static ssize_t |
| 1124 | gpio_sim_line_config_name_show(struct config_item *item, char *page) |
| 1125 | { |
| 1126 | struct gpio_sim_line *line = to_gpio_sim_line(item); |
| 1127 | struct gpio_sim_device *dev = gpio_sim_line_get_device(line); |
| 1128 | int ret; |
| 1129 | |
| 1130 | mutex_lock(&dev->lock); |
| 1131 | ret = sprintf(page, "%s\n", line->name ?: ""); |
| 1132 | mutex_unlock(&dev->lock); |
| 1133 | |
| 1134 | return ret; |
| 1135 | } |
| 1136 | |
| 1137 | static ssize_t gpio_sim_line_config_name_store(struct config_item *item, |
| 1138 | const char *page, size_t count) |
| 1139 | { |
| 1140 | struct gpio_sim_line *line = to_gpio_sim_line(item); |
| 1141 | struct gpio_sim_device *dev = gpio_sim_line_get_device(line); |
| 1142 | char *trimmed; |
| 1143 | |
| 1144 | mutex_lock(&dev->lock); |
| 1145 | |
| 1146 | if (gpio_sim_device_is_live_unlocked(dev)) { |
| 1147 | mutex_unlock(&dev->lock); |
| 1148 | return -EBUSY; |
| 1149 | } |
| 1150 | |
| 1151 | trimmed = gpio_sim_strdup_trimmed(page, count); |
| 1152 | if (!trimmed) { |
| 1153 | mutex_unlock(&dev->lock); |
| 1154 | return -ENOMEM; |
| 1155 | } |
| 1156 | |
| 1157 | kfree(line->name); |
| 1158 | line->name = trimmed; |
| 1159 | |
| 1160 | mutex_unlock(&dev->lock); |
| 1161 | |
| 1162 | return count; |
| 1163 | } |
| 1164 | |
| 1165 | CONFIGFS_ATTR(gpio_sim_line_config_, name); |
| 1166 | |
| 1167 | static struct configfs_attribute *gpio_sim_line_config_attrs[] = { |
| 1168 | &gpio_sim_line_config_attr_name, |
| 1169 | NULL |
| 1170 | }; |
| 1171 | |
| 1172 | static ssize_t gpio_sim_hog_config_name_show(struct config_item *item, |
| 1173 | char *page) |
| 1174 | { |
| 1175 | struct gpio_sim_hog *hog = to_gpio_sim_hog(item); |
| 1176 | struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog); |
| 1177 | int ret; |
| 1178 | |
| 1179 | mutex_lock(&dev->lock); |
| 1180 | ret = sprintf(page, "%s\n", hog->name ?: ""); |
| 1181 | mutex_unlock(&dev->lock); |
| 1182 | |
| 1183 | return ret; |
| 1184 | } |
| 1185 | |
| 1186 | static ssize_t gpio_sim_hog_config_name_store(struct config_item *item, |
| 1187 | const char *page, size_t count) |
| 1188 | { |
| 1189 | struct gpio_sim_hog *hog = to_gpio_sim_hog(item); |
| 1190 | struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog); |
| 1191 | char *trimmed; |
| 1192 | |
| 1193 | mutex_lock(&dev->lock); |
| 1194 | |
| 1195 | if (gpio_sim_device_is_live_unlocked(dev)) { |
| 1196 | mutex_unlock(&dev->lock); |
| 1197 | return -EBUSY; |
| 1198 | } |
| 1199 | |
| 1200 | trimmed = gpio_sim_strdup_trimmed(page, count); |
| 1201 | if (!trimmed) { |
| 1202 | mutex_unlock(&dev->lock); |
| 1203 | return -ENOMEM; |
| 1204 | } |
| 1205 | |
| 1206 | kfree(hog->name); |
| 1207 | hog->name = trimmed; |
| 1208 | |
| 1209 | mutex_unlock(&dev->lock); |
| 1210 | |
| 1211 | return count; |
| 1212 | } |
| 1213 | |
| 1214 | CONFIGFS_ATTR(gpio_sim_hog_config_, name); |
| 1215 | |
| 1216 | static ssize_t gpio_sim_hog_config_direction_show(struct config_item *item, |
| 1217 | char *page) |
| 1218 | { |
| 1219 | struct gpio_sim_hog *hog = to_gpio_sim_hog(item); |
| 1220 | struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog); |
| 1221 | char *repr; |
| 1222 | int dir; |
| 1223 | |
| 1224 | mutex_lock(&dev->lock); |
| 1225 | dir = hog->dir; |
| 1226 | mutex_unlock(&dev->lock); |
| 1227 | |
| 1228 | switch (dir) { |
| 1229 | case GPIOD_IN: |
| 1230 | repr = "input"; |
| 1231 | break; |
| 1232 | case GPIOD_OUT_HIGH: |
| 1233 | repr = "output-high"; |
| 1234 | break; |
| 1235 | case GPIOD_OUT_LOW: |
| 1236 | repr = "output-low"; |
| 1237 | break; |
| 1238 | default: |
| 1239 | /* This would be a programmer bug. */ |
| 1240 | WARN(1, "Unexpected hog direction value: %d", dir); |
| 1241 | return -EINVAL; |
| 1242 | } |
| 1243 | |
| 1244 | return sprintf(page, "%s\n", repr); |
| 1245 | } |
| 1246 | |
| 1247 | static ssize_t |
| 1248 | gpio_sim_hog_config_direction_store(struct config_item *item, |
| 1249 | const char *page, size_t count) |
| 1250 | { |
| 1251 | struct gpio_sim_hog *hog = to_gpio_sim_hog(item); |
| 1252 | struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog); |
| 1253 | char *trimmed; |
| 1254 | int dir; |
| 1255 | |
| 1256 | mutex_lock(&dev->lock); |
| 1257 | |
| 1258 | if (gpio_sim_device_is_live_unlocked(dev)) { |
| 1259 | mutex_unlock(&dev->lock); |
| 1260 | return -EBUSY; |
| 1261 | } |
| 1262 | |
| 1263 | trimmed = gpio_sim_strdup_trimmed(page, count); |
| 1264 | if (!trimmed) { |
| 1265 | mutex_unlock(&dev->lock); |
| 1266 | return -ENOMEM; |
| 1267 | } |
| 1268 | |
| 1269 | if (strcmp(trimmed, "input") == 0) |
| 1270 | dir = GPIOD_IN; |
| 1271 | else if (strcmp(trimmed, "output-high") == 0) |
| 1272 | dir = GPIOD_OUT_HIGH; |
| 1273 | else if (strcmp(trimmed, "output-low") == 0) |
| 1274 | dir = GPIOD_OUT_LOW; |
| 1275 | else |
| 1276 | dir = -EINVAL; |
| 1277 | |
| 1278 | kfree(trimmed); |
| 1279 | |
| 1280 | if (dir < 0) { |
| 1281 | mutex_unlock(&dev->lock); |
| 1282 | return dir; |
| 1283 | } |
| 1284 | |
| 1285 | hog->dir = dir; |
| 1286 | |
| 1287 | mutex_unlock(&dev->lock); |
| 1288 | |
| 1289 | return count; |
| 1290 | } |
| 1291 | |
| 1292 | CONFIGFS_ATTR(gpio_sim_hog_config_, direction); |
| 1293 | |
| 1294 | static struct configfs_attribute *gpio_sim_hog_config_attrs[] = { |
| 1295 | &gpio_sim_hog_config_attr_name, |
| 1296 | &gpio_sim_hog_config_attr_direction, |
| 1297 | NULL |
| 1298 | }; |
| 1299 | |
| 1300 | static void gpio_sim_hog_config_item_release(struct config_item *item) |
| 1301 | { |
| 1302 | struct gpio_sim_hog *hog = to_gpio_sim_hog(item); |
| 1303 | struct gpio_sim_line *line = hog->parent; |
| 1304 | struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog); |
| 1305 | |
| 1306 | mutex_lock(&dev->lock); |
| 1307 | line->hog = NULL; |
| 1308 | mutex_unlock(&dev->lock); |
| 1309 | |
| 1310 | kfree(hog->name); |
| 1311 | kfree(hog); |
| 1312 | } |
| 1313 | |
| 1314 | struct configfs_item_operations gpio_sim_hog_config_item_ops = { |
| 1315 | .release = gpio_sim_hog_config_item_release, |
| 1316 | }; |
| 1317 | |
| 1318 | static const struct config_item_type gpio_sim_hog_config_type = { |
| 1319 | .ct_item_ops = &gpio_sim_hog_config_item_ops, |
| 1320 | .ct_attrs = gpio_sim_hog_config_attrs, |
| 1321 | .ct_owner = THIS_MODULE, |
| 1322 | }; |
| 1323 | |
| 1324 | static struct config_item * |
| 1325 | gpio_sim_line_config_make_hog_item(struct config_group *group, const char *name) |
| 1326 | { |
| 1327 | struct gpio_sim_line *line = to_gpio_sim_line(&group->cg_item); |
| 1328 | struct gpio_sim_device *dev = gpio_sim_line_get_device(line); |
| 1329 | struct gpio_sim_hog *hog; |
| 1330 | |
| 1331 | if (strcmp(name, "hog") != 0) |
| 1332 | return ERR_PTR(-EINVAL); |
| 1333 | |
| 1334 | mutex_lock(&dev->lock); |
| 1335 | |
| 1336 | hog = kzalloc(sizeof(*hog), GFP_KERNEL); |
| 1337 | if (!hog) { |
| 1338 | mutex_unlock(&dev->lock); |
| 1339 | return ERR_PTR(-ENOMEM); |
| 1340 | } |
| 1341 | |
| 1342 | config_item_init_type_name(&hog->item, name, |
| 1343 | &gpio_sim_hog_config_type); |
| 1344 | |
| 1345 | hog->dir = GPIOD_IN; |
| 1346 | hog->name = NULL; |
| 1347 | hog->parent = line; |
| 1348 | line->hog = hog; |
| 1349 | |
| 1350 | mutex_unlock(&dev->lock); |
| 1351 | |
| 1352 | return &hog->item; |
| 1353 | } |
| 1354 | |
| 1355 | static void gpio_sim_line_config_group_release(struct config_item *item) |
| 1356 | { |
| 1357 | struct gpio_sim_line *line = to_gpio_sim_line(item); |
| 1358 | struct gpio_sim_device *dev = gpio_sim_line_get_device(line); |
| 1359 | |
| 1360 | mutex_lock(&dev->lock); |
| 1361 | list_del(&line->siblings); |
| 1362 | mutex_unlock(&dev->lock); |
| 1363 | |
| 1364 | kfree(line->name); |
| 1365 | kfree(line); |
| 1366 | } |
| 1367 | |
| 1368 | static struct configfs_item_operations gpio_sim_line_config_item_ops = { |
| 1369 | .release = gpio_sim_line_config_group_release, |
| 1370 | }; |
| 1371 | |
| 1372 | static struct configfs_group_operations gpio_sim_line_config_group_ops = { |
| 1373 | .make_item = gpio_sim_line_config_make_hog_item, |
| 1374 | }; |
| 1375 | |
| 1376 | static const struct config_item_type gpio_sim_line_config_type = { |
| 1377 | .ct_item_ops = &gpio_sim_line_config_item_ops, |
| 1378 | .ct_group_ops = &gpio_sim_line_config_group_ops, |
| 1379 | .ct_attrs = gpio_sim_line_config_attrs, |
| 1380 | .ct_owner = THIS_MODULE, |
| 1381 | }; |
| 1382 | |
| 1383 | static struct config_group * |
| 1384 | gpio_sim_bank_config_make_line_group(struct config_group *group, |
| 1385 | const char *name) |
| 1386 | { |
| 1387 | struct gpio_sim_bank *bank = to_gpio_sim_bank(&group->cg_item); |
| 1388 | struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); |
| 1389 | struct gpio_sim_line *line; |
| 1390 | unsigned int offset; |
| 1391 | int ret, nchar; |
| 1392 | |
| 1393 | ret = sscanf(name, "line%u%n", &offset, &nchar); |
| 1394 | if (ret != 1 || nchar != strlen(name)) |
| 1395 | return ERR_PTR(-EINVAL); |
| 1396 | |
| 1397 | mutex_lock(&dev->lock); |
| 1398 | |
| 1399 | if (gpio_sim_device_is_live_unlocked(dev)) { |
| 1400 | mutex_unlock(&dev->lock); |
| 1401 | return ERR_PTR(-EBUSY); |
| 1402 | } |
| 1403 | |
| 1404 | line = kzalloc(sizeof(*line), GFP_KERNEL); |
| 1405 | if (!line) { |
| 1406 | mutex_unlock(&dev->lock); |
| 1407 | return ERR_PTR(-ENOMEM); |
| 1408 | } |
| 1409 | |
| 1410 | config_group_init_type_name(&line->group, name, |
| 1411 | &gpio_sim_line_config_type); |
| 1412 | |
| 1413 | line->parent = bank; |
| 1414 | line->offset = offset; |
| 1415 | list_add_tail(&line->siblings, &bank->line_list); |
| 1416 | |
| 1417 | mutex_unlock(&dev->lock); |
| 1418 | |
| 1419 | return &line->group; |
| 1420 | } |
| 1421 | |
| 1422 | static void gpio_sim_bank_config_group_release(struct config_item *item) |
| 1423 | { |
| 1424 | struct gpio_sim_bank *bank = to_gpio_sim_bank(item); |
| 1425 | struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); |
| 1426 | |
| 1427 | mutex_lock(&dev->lock); |
| 1428 | list_del(&bank->siblings); |
| 1429 | mutex_unlock(&dev->lock); |
| 1430 | |
| 1431 | kfree(bank->label); |
| 1432 | kfree(bank); |
| 1433 | } |
| 1434 | |
| 1435 | static struct configfs_item_operations gpio_sim_bank_config_item_ops = { |
| 1436 | .release = gpio_sim_bank_config_group_release, |
| 1437 | }; |
| 1438 | |
| 1439 | static struct configfs_group_operations gpio_sim_bank_config_group_ops = { |
| 1440 | .make_group = gpio_sim_bank_config_make_line_group, |
| 1441 | }; |
| 1442 | |
| 1443 | static const struct config_item_type gpio_sim_bank_config_group_type = { |
| 1444 | .ct_item_ops = &gpio_sim_bank_config_item_ops, |
| 1445 | .ct_group_ops = &gpio_sim_bank_config_group_ops, |
| 1446 | .ct_attrs = gpio_sim_bank_config_attrs, |
| 1447 | .ct_owner = THIS_MODULE, |
| 1448 | }; |
| 1449 | |
| 1450 | static struct config_group * |
| 1451 | gpio_sim_device_config_make_bank_group(struct config_group *group, |
| 1452 | const char *name) |
| 1453 | { |
| 1454 | struct gpio_sim_device *dev = to_gpio_sim_device(&group->cg_item); |
| 1455 | struct gpio_sim_bank *bank; |
| 1456 | |
| 1457 | mutex_lock(&dev->lock); |
| 1458 | |
| 1459 | if (gpio_sim_device_is_live_unlocked(dev)) { |
| 1460 | mutex_unlock(&dev->lock); |
| 1461 | return ERR_PTR(-EBUSY); |
| 1462 | } |
| 1463 | |
| 1464 | bank = kzalloc(sizeof(*bank), GFP_KERNEL); |
| 1465 | if (!bank) { |
| 1466 | mutex_unlock(&dev->lock); |
| 1467 | return ERR_PTR(-ENOMEM); |
| 1468 | } |
| 1469 | |
| 1470 | config_group_init_type_name(&bank->group, name, |
| 1471 | &gpio_sim_bank_config_group_type); |
| 1472 | bank->num_lines = 1; |
| 1473 | bank->parent = dev; |
| 1474 | INIT_LIST_HEAD(&bank->line_list); |
| 1475 | list_add_tail(&bank->siblings, &dev->bank_list); |
| 1476 | |
| 1477 | mutex_unlock(&dev->lock); |
| 1478 | |
| 1479 | return &bank->group; |
| 1480 | } |
| 1481 | |
| 1482 | static void gpio_sim_device_config_group_release(struct config_item *item) |
| 1483 | { |
| 1484 | struct gpio_sim_device *dev = to_gpio_sim_device(item); |
| 1485 | |
| 1486 | mutex_lock(&dev->lock); |
| 1487 | if (gpio_sim_device_is_live_unlocked(dev)) |
| 1488 | gpio_sim_device_deactivate_unlocked(dev); |
| 1489 | mutex_unlock(&dev->lock); |
| 1490 | |
| 1491 | mutex_destroy(&dev->lock); |
| 1492 | ida_free(&gpio_sim_ida, dev->id); |
| 1493 | kfree(dev); |
| 1494 | } |
| 1495 | |
| 1496 | static struct configfs_item_operations gpio_sim_device_config_item_ops = { |
| 1497 | .release = gpio_sim_device_config_group_release, |
| 1498 | }; |
| 1499 | |
| 1500 | static struct configfs_group_operations gpio_sim_device_config_group_ops = { |
| 1501 | .make_group = gpio_sim_device_config_make_bank_group, |
| 1502 | }; |
| 1503 | |
| 1504 | static const struct config_item_type gpio_sim_device_config_group_type = { |
| 1505 | .ct_item_ops = &gpio_sim_device_config_item_ops, |
| 1506 | .ct_group_ops = &gpio_sim_device_config_group_ops, |
| 1507 | .ct_attrs = gpio_sim_device_config_attrs, |
| 1508 | .ct_owner = THIS_MODULE, |
| 1509 | }; |
| 1510 | |
| 1511 | static struct config_group * |
| 1512 | gpio_sim_config_make_device_group(struct config_group *group, const char *name) |
| 1513 | { |
| 1514 | struct gpio_sim_device *dev; |
| 1515 | int id; |
| 1516 | |
| 1517 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 1518 | if (!dev) |
| 1519 | return ERR_PTR(-ENOMEM); |
| 1520 | |
| 1521 | id = ida_alloc(&gpio_sim_ida, GFP_KERNEL); |
| 1522 | if (id < 0) { |
| 1523 | kfree(dev); |
| 1524 | return ERR_PTR(id); |
| 1525 | } |
| 1526 | |
| 1527 | config_group_init_type_name(&dev->group, name, |
| 1528 | &gpio_sim_device_config_group_type); |
| 1529 | dev->id = id; |
| 1530 | mutex_init(&dev->lock); |
| 1531 | INIT_LIST_HEAD(&dev->bank_list); |
| 1532 | |
| 1533 | dev->bus_notifier.notifier_call = gpio_sim_bus_notifier_call; |
| 1534 | init_completion(&dev->probe_completion); |
| 1535 | |
| 1536 | return &dev->group; |
| 1537 | } |
| 1538 | |
| 1539 | static struct configfs_group_operations gpio_sim_config_group_ops = { |
| 1540 | .make_group = gpio_sim_config_make_device_group, |
| 1541 | }; |
| 1542 | |
| 1543 | static const struct config_item_type gpio_sim_config_type = { |
| 1544 | .ct_group_ops = &gpio_sim_config_group_ops, |
| 1545 | .ct_owner = THIS_MODULE, |
| 1546 | }; |
| 1547 | |
| 1548 | static struct configfs_subsystem gpio_sim_config_subsys = { |
| 1549 | .su_group = { |
| 1550 | .cg_item = { |
| 1551 | .ci_namebuf = "gpio-sim", |
| 1552 | .ci_type = &gpio_sim_config_type, |
| 1553 | }, |
| 1554 | }, |
| 1555 | }; |
| 1556 | |
| 1557 | static int __init gpio_sim_init(void) |
| 1558 | { |
| 1559 | int ret; |
| 1560 | |
| 1561 | ret = platform_driver_register(&gpio_sim_driver); |
| 1562 | if (ret) { |
| 1563 | pr_err("Error %d while registering the platform driver\n", ret); |
| 1564 | return ret; |
| 1565 | } |
| 1566 | |
| 1567 | config_group_init(&gpio_sim_config_subsys.su_group); |
| 1568 | mutex_init(&gpio_sim_config_subsys.su_mutex); |
| 1569 | ret = configfs_register_subsystem(&gpio_sim_config_subsys); |
| 1570 | if (ret) { |
| 1571 | pr_err("Error %d while registering the configfs subsystem %s\n", |
| 1572 | ret, gpio_sim_config_subsys.su_group.cg_item.ci_namebuf); |
| 1573 | mutex_destroy(&gpio_sim_config_subsys.su_mutex); |
| 1574 | platform_driver_unregister(&gpio_sim_driver); |
| 1575 | return ret; |
| 1576 | } |
| 1577 | |
| 1578 | return 0; |
| 1579 | } |
| 1580 | module_init(gpio_sim_init); |
| 1581 | |
| 1582 | static void __exit gpio_sim_exit(void) |
| 1583 | { |
| 1584 | configfs_unregister_subsystem(&gpio_sim_config_subsys); |
| 1585 | mutex_destroy(&gpio_sim_config_subsys.su_mutex); |
| 1586 | platform_driver_unregister(&gpio_sim_driver); |
| 1587 | } |
| 1588 | module_exit(gpio_sim_exit); |
| 1589 | |
| 1590 | MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl"); |
| 1591 | MODULE_DESCRIPTION("GPIO Simulator Module"); |
| 1592 | MODULE_LICENSE("GPL"); |