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