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