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