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