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