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