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