xref: /openbmc/linux/drivers/gpio/gpio-sim.c (revision e855afd715656a9f25cf62fa68d99c33213b83b7)
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-%pfwP",
381 				       dev_name(dev), 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 	gc->can_sleep = true;
433 
434 	ret = devm_gpiochip_add_data(dev, gc, chip);
435 	if (ret)
436 		return ret;
437 
438 	/* Used by sysfs and configfs callbacks. */
439 	dev_set_drvdata(&gc->gpiodev->dev, chip);
440 
441 	return gpio_sim_setup_sysfs(chip);
442 }
443 
444 static int gpio_sim_probe(struct platform_device *pdev)
445 {
446 	struct device *dev = &pdev->dev;
447 	struct fwnode_handle *swnode;
448 	int ret;
449 
450 	device_for_each_child_node(dev, swnode) {
451 		ret = gpio_sim_add_bank(swnode, dev);
452 		if (ret) {
453 			fwnode_handle_put(swnode);
454 			return ret;
455 		}
456 	}
457 
458 	return 0;
459 }
460 
461 static const struct of_device_id gpio_sim_of_match[] = {
462 	{ .compatible = "gpio-simulator" },
463 	{ }
464 };
465 MODULE_DEVICE_TABLE(of, gpio_sim_of_match);
466 
467 static struct platform_driver gpio_sim_driver = {
468 	.driver = {
469 		.name = "gpio-sim",
470 		.of_match_table = gpio_sim_of_match,
471 	},
472 	.probe = gpio_sim_probe,
473 };
474 
475 struct gpio_sim_device {
476 	struct config_group group;
477 
478 	/*
479 	 * If pdev is NULL, the device is 'pending' (waiting for configuration).
480 	 * Once the pointer is assigned, the device has been created and the
481 	 * item is 'live'.
482 	 */
483 	struct platform_device *pdev;
484 	int id;
485 
486 	/*
487 	 * Each configfs filesystem operation is protected with the subsystem
488 	 * mutex. Each separate attribute is protected with the buffer mutex.
489 	 * This structure however can be modified by callbacks of different
490 	 * attributes so we need another lock.
491 	 *
492 	 * We use this lock fo protecting all data structures owned by this
493 	 * object too.
494 	 */
495 	struct mutex lock;
496 
497 	/*
498 	 * This is used to synchronously wait for the driver's probe to complete
499 	 * and notify the user-space about any errors.
500 	 */
501 	struct notifier_block bus_notifier;
502 	struct completion probe_completion;
503 	bool driver_bound;
504 
505 	struct gpiod_hog *hogs;
506 
507 	struct list_head bank_list;
508 };
509 
510 /* This is called with dev->lock already taken. */
511 static int gpio_sim_bus_notifier_call(struct notifier_block *nb,
512 				      unsigned long action, void *data)
513 {
514 	struct gpio_sim_device *simdev = container_of(nb,
515 						      struct gpio_sim_device,
516 						      bus_notifier);
517 	struct device *dev = data;
518 	char devname[32];
519 
520 	snprintf(devname, sizeof(devname), "gpio-sim.%u", simdev->id);
521 
522 	if (strcmp(dev_name(dev), devname) == 0) {
523 		if (action == BUS_NOTIFY_BOUND_DRIVER)
524 			simdev->driver_bound = true;
525 		else if (action == BUS_NOTIFY_DRIVER_NOT_BOUND)
526 			simdev->driver_bound = false;
527 		else
528 			return NOTIFY_DONE;
529 
530 		complete(&simdev->probe_completion);
531 		return NOTIFY_OK;
532 	}
533 
534 	return NOTIFY_DONE;
535 }
536 
537 static struct gpio_sim_device *to_gpio_sim_device(struct config_item *item)
538 {
539 	struct config_group *group = to_config_group(item);
540 
541 	return container_of(group, struct gpio_sim_device, group);
542 }
543 
544 struct gpio_sim_bank {
545 	struct config_group group;
546 
547 	/*
548 	 * We could have used the ci_parent field of the config_item but
549 	 * configfs is stupid and calls the item's release callback after
550 	 * already having cleared the parent pointer even though the parent
551 	 * is guaranteed to survive the child...
552 	 *
553 	 * So we need to store the pointer to the parent struct here. We can
554 	 * dereference it anywhere we need with no checks and no locking as
555 	 * it's guaranteed to survive the children and protected by configfs
556 	 * locks.
557 	 *
558 	 * Same for other structures.
559 	 */
560 	struct gpio_sim_device *parent;
561 	struct list_head siblings;
562 
563 	char *label;
564 	unsigned int num_lines;
565 
566 	struct list_head line_list;
567 
568 	struct fwnode_handle *swnode;
569 };
570 
571 static struct gpio_sim_bank *to_gpio_sim_bank(struct config_item *item)
572 {
573 	struct config_group *group = to_config_group(item);
574 
575 	return container_of(group, struct gpio_sim_bank, group);
576 }
577 
578 static bool gpio_sim_bank_has_label(struct gpio_sim_bank *bank)
579 {
580 	return bank->label && *bank->label;
581 }
582 
583 static struct gpio_sim_device *
584 gpio_sim_bank_get_device(struct gpio_sim_bank *bank)
585 {
586 	return bank->parent;
587 }
588 
589 struct gpio_sim_hog;
590 
591 struct gpio_sim_line {
592 	struct config_group group;
593 
594 	struct gpio_sim_bank *parent;
595 	struct list_head siblings;
596 
597 	unsigned int offset;
598 	char *name;
599 
600 	/* There can only be one hog per line. */
601 	struct gpio_sim_hog *hog;
602 };
603 
604 static struct gpio_sim_line *to_gpio_sim_line(struct config_item *item)
605 {
606 	struct config_group *group = to_config_group(item);
607 
608 	return container_of(group, struct gpio_sim_line, group);
609 }
610 
611 static struct gpio_sim_device *
612 gpio_sim_line_get_device(struct gpio_sim_line *line)
613 {
614 	struct gpio_sim_bank *bank = line->parent;
615 
616 	return gpio_sim_bank_get_device(bank);
617 }
618 
619 struct gpio_sim_hog {
620 	struct config_item item;
621 	struct gpio_sim_line *parent;
622 
623 	char *name;
624 	int dir;
625 };
626 
627 static struct gpio_sim_hog *to_gpio_sim_hog(struct config_item *item)
628 {
629 	return container_of(item, struct gpio_sim_hog, item);
630 }
631 
632 static struct gpio_sim_device *gpio_sim_hog_get_device(struct gpio_sim_hog *hog)
633 {
634 	struct gpio_sim_line *line = hog->parent;
635 
636 	return gpio_sim_line_get_device(line);
637 }
638 
639 static bool gpio_sim_device_is_live_unlocked(struct gpio_sim_device *dev)
640 {
641 	return !!dev->pdev;
642 }
643 
644 static char *gpio_sim_strdup_trimmed(const char *str, size_t count)
645 {
646 	char *dup, *trimmed;
647 
648 	dup = kstrndup(str, count, GFP_KERNEL);
649 	if (!dup)
650 		return NULL;
651 
652 	trimmed = strstrip(dup);
653 	memmove(dup, trimmed, strlen(trimmed) + 1);
654 
655 	return dup;
656 }
657 
658 static ssize_t gpio_sim_device_config_dev_name_show(struct config_item *item,
659 						    char *page)
660 {
661 	struct gpio_sim_device *dev = to_gpio_sim_device(item);
662 	struct platform_device *pdev;
663 	int ret;
664 
665 	mutex_lock(&dev->lock);
666 	pdev = dev->pdev;
667 	if (pdev)
668 		ret = sprintf(page, "%s\n", dev_name(&pdev->dev));
669 	else
670 		ret = sprintf(page, "gpio-sim.%d\n", dev->id);
671 	mutex_unlock(&dev->lock);
672 
673 	return ret;
674 }
675 
676 CONFIGFS_ATTR_RO(gpio_sim_device_config_, dev_name);
677 
678 static ssize_t
679 gpio_sim_device_config_live_show(struct config_item *item, char *page)
680 {
681 	struct gpio_sim_device *dev = to_gpio_sim_device(item);
682 	bool live;
683 
684 	mutex_lock(&dev->lock);
685 	live = gpio_sim_device_is_live_unlocked(dev);
686 	mutex_unlock(&dev->lock);
687 
688 	return sprintf(page, "%c\n", live ? '1' : '0');
689 }
690 
691 static char **gpio_sim_make_line_names(struct gpio_sim_bank *bank,
692 				       unsigned int *line_names_size)
693 {
694 	unsigned int max_offset = 0;
695 	bool has_line_names = false;
696 	struct gpio_sim_line *line;
697 	char **line_names;
698 
699 	list_for_each_entry(line, &bank->line_list, siblings) {
700 		if (line->offset >= bank->num_lines)
701 			continue;
702 
703 		if (line->name) {
704 			if (line->offset > max_offset)
705 				max_offset = line->offset;
706 
707 			/*
708 			 * max_offset can stay at 0 so it's not an indicator
709 			 * of whether line names were configured at all.
710 			 */
711 			has_line_names = true;
712 		}
713 	}
714 
715 	if (!has_line_names)
716 		/*
717 		 * This is not an error - NULL means, there are no line
718 		 * names configured.
719 		 */
720 		return NULL;
721 
722 	*line_names_size = max_offset + 1;
723 
724 	line_names = kcalloc(*line_names_size, sizeof(*line_names), GFP_KERNEL);
725 	if (!line_names)
726 		return ERR_PTR(-ENOMEM);
727 
728 	list_for_each_entry(line, &bank->line_list, siblings) {
729 		if (line->offset >= bank->num_lines)
730 			continue;
731 
732 		if (line->name && (line->offset <= max_offset))
733 			line_names[line->offset] = line->name;
734 	}
735 
736 	return line_names;
737 }
738 
739 static void gpio_sim_remove_hogs(struct gpio_sim_device *dev)
740 {
741 	struct gpiod_hog *hog;
742 
743 	if (!dev->hogs)
744 		return;
745 
746 	gpiod_remove_hogs(dev->hogs);
747 
748 	for (hog = dev->hogs; hog->chip_label; hog++) {
749 		kfree(hog->chip_label);
750 		kfree(hog->line_name);
751 	}
752 
753 	kfree(dev->hogs);
754 	dev->hogs = NULL;
755 }
756 
757 static int gpio_sim_add_hogs(struct gpio_sim_device *dev)
758 {
759 	unsigned int num_hogs = 0, idx = 0;
760 	struct gpio_sim_bank *bank;
761 	struct gpio_sim_line *line;
762 	struct gpiod_hog *hog;
763 
764 	list_for_each_entry(bank, &dev->bank_list, siblings) {
765 		list_for_each_entry(line, &bank->line_list, siblings) {
766 			if (line->offset >= bank->num_lines)
767 				continue;
768 
769 			if (line->hog)
770 				num_hogs++;
771 		}
772 	}
773 
774 	if (!num_hogs)
775 		return 0;
776 
777 	/* Allocate one more for the sentinel. */
778 	dev->hogs = kcalloc(num_hogs + 1, sizeof(*dev->hogs), GFP_KERNEL);
779 	if (!dev->hogs)
780 		return -ENOMEM;
781 
782 	list_for_each_entry(bank, &dev->bank_list, siblings) {
783 		list_for_each_entry(line, &bank->line_list, siblings) {
784 			if (line->offset >= bank->num_lines)
785 				continue;
786 
787 			if (!line->hog)
788 				continue;
789 
790 			hog = &dev->hogs[idx++];
791 
792 			/*
793 			 * We need to make this string manually because at this
794 			 * point the device doesn't exist yet and so dev_name()
795 			 * is not available.
796 			 */
797 			if (gpio_sim_bank_has_label(bank))
798 				hog->chip_label = kstrdup(bank->label,
799 							  GFP_KERNEL);
800 			else
801 				hog->chip_label = kasprintf(GFP_KERNEL,
802 							"gpio-sim.%u-%pfwP",
803 							dev->id,
804 							bank->swnode);
805 			if (!hog->chip_label) {
806 				gpio_sim_remove_hogs(dev);
807 				return -ENOMEM;
808 			}
809 
810 			/*
811 			 * We need to duplicate this because the hog config
812 			 * item can be removed at any time (and we can't block
813 			 * it) and gpiolib doesn't make a deep copy of the hog
814 			 * data.
815 			 */
816 			if (line->hog->name) {
817 				hog->line_name = kstrdup(line->hog->name,
818 							 GFP_KERNEL);
819 				if (!hog->line_name) {
820 					gpio_sim_remove_hogs(dev);
821 					return -ENOMEM;
822 				}
823 			}
824 
825 			hog->chip_hwnum = line->offset;
826 			hog->dflags = line->hog->dir;
827 		}
828 	}
829 
830 	gpiod_add_hogs(dev->hogs);
831 
832 	return 0;
833 }
834 
835 static struct fwnode_handle *
836 gpio_sim_make_bank_swnode(struct gpio_sim_bank *bank,
837 			  struct fwnode_handle *parent)
838 {
839 	struct property_entry properties[GPIO_SIM_PROP_MAX];
840 	unsigned int prop_idx = 0, line_names_size = 0;
841 	struct fwnode_handle *swnode;
842 	char **line_names;
843 
844 	memset(properties, 0, sizeof(properties));
845 
846 	properties[prop_idx++] = PROPERTY_ENTRY_U32("ngpios", bank->num_lines);
847 
848 	if (gpio_sim_bank_has_label(bank))
849 		properties[prop_idx++] = PROPERTY_ENTRY_STRING("gpio-sim,label",
850 							       bank->label);
851 
852 	line_names = gpio_sim_make_line_names(bank, &line_names_size);
853 	if (IS_ERR(line_names))
854 		return ERR_CAST(line_names);
855 
856 	if (line_names)
857 		properties[prop_idx++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
858 						"gpio-line-names",
859 						line_names, line_names_size);
860 
861 	swnode = fwnode_create_software_node(properties, parent);
862 	kfree(line_names);
863 	return swnode;
864 }
865 
866 static void gpio_sim_remove_swnode_recursive(struct fwnode_handle *swnode)
867 {
868 	struct fwnode_handle *child;
869 
870 	fwnode_for_each_child_node(swnode, child)
871 		fwnode_remove_software_node(child);
872 
873 	fwnode_remove_software_node(swnode);
874 }
875 
876 static bool gpio_sim_bank_labels_non_unique(struct gpio_sim_device *dev)
877 {
878 	struct gpio_sim_bank *this, *pos;
879 
880 	list_for_each_entry(this, &dev->bank_list, siblings) {
881 		list_for_each_entry(pos, &dev->bank_list, siblings) {
882 			if (this == pos || (!this->label || !pos->label))
883 				continue;
884 
885 			if (strcmp(this->label, pos->label) == 0)
886 				return true;
887 		}
888 	}
889 
890 	return false;
891 }
892 
893 static int gpio_sim_device_activate_unlocked(struct gpio_sim_device *dev)
894 {
895 	struct platform_device_info pdevinfo;
896 	struct fwnode_handle *swnode;
897 	struct platform_device *pdev;
898 	struct gpio_sim_bank *bank;
899 	int ret;
900 
901 	if (list_empty(&dev->bank_list))
902 		return -ENODATA;
903 
904 	/*
905 	 * Non-unique GPIO device labels are a corner-case we don't support
906 	 * as it would interfere with machine hogging mechanism and has little
907 	 * use in real life.
908 	 */
909 	if (gpio_sim_bank_labels_non_unique(dev))
910 		return -EINVAL;
911 
912 	memset(&pdevinfo, 0, sizeof(pdevinfo));
913 
914 	swnode = fwnode_create_software_node(NULL, NULL);
915 	if (IS_ERR(swnode))
916 		return PTR_ERR(swnode);
917 
918 	list_for_each_entry(bank, &dev->bank_list, siblings) {
919 		bank->swnode = gpio_sim_make_bank_swnode(bank, swnode);
920 		if (IS_ERR(bank->swnode)) {
921 			ret = PTR_ERR(bank->swnode);
922 			gpio_sim_remove_swnode_recursive(swnode);
923 			return ret;
924 		}
925 	}
926 
927 	ret = gpio_sim_add_hogs(dev);
928 	if (ret) {
929 		gpio_sim_remove_swnode_recursive(swnode);
930 		return ret;
931 	}
932 
933 	pdevinfo.name = "gpio-sim";
934 	pdevinfo.fwnode = swnode;
935 	pdevinfo.id = dev->id;
936 
937 	reinit_completion(&dev->probe_completion);
938 	dev->driver_bound = false;
939 	bus_register_notifier(&platform_bus_type, &dev->bus_notifier);
940 
941 	pdev = platform_device_register_full(&pdevinfo);
942 	if (IS_ERR(pdev)) {
943 		bus_unregister_notifier(&platform_bus_type, &dev->bus_notifier);
944 		gpio_sim_remove_hogs(dev);
945 		gpio_sim_remove_swnode_recursive(swnode);
946 		return PTR_ERR(pdev);
947 	}
948 
949 	wait_for_completion(&dev->probe_completion);
950 	bus_unregister_notifier(&platform_bus_type, &dev->bus_notifier);
951 
952 	if (!dev->driver_bound) {
953 		/* Probe failed, check kernel log. */
954 		platform_device_unregister(pdev);
955 		gpio_sim_remove_hogs(dev);
956 		gpio_sim_remove_swnode_recursive(swnode);
957 		return -ENXIO;
958 	}
959 
960 	dev->pdev = pdev;
961 
962 	return 0;
963 }
964 
965 static void gpio_sim_device_deactivate_unlocked(struct gpio_sim_device *dev)
966 {
967 	struct fwnode_handle *swnode;
968 
969 	swnode = dev_fwnode(&dev->pdev->dev);
970 	platform_device_unregister(dev->pdev);
971 	gpio_sim_remove_hogs(dev);
972 	gpio_sim_remove_swnode_recursive(swnode);
973 	dev->pdev = NULL;
974 }
975 
976 static ssize_t
977 gpio_sim_device_config_live_store(struct config_item *item,
978 				  const char *page, size_t count)
979 {
980 	struct gpio_sim_device *dev = to_gpio_sim_device(item);
981 	bool live;
982 	int ret;
983 
984 	ret = kstrtobool(page, &live);
985 	if (ret)
986 		return ret;
987 
988 	mutex_lock(&dev->lock);
989 
990 	if ((!live && !gpio_sim_device_is_live_unlocked(dev)) ||
991 	    (live && gpio_sim_device_is_live_unlocked(dev)))
992 		ret = -EPERM;
993 	else if (live)
994 		ret = gpio_sim_device_activate_unlocked(dev);
995 	else
996 		gpio_sim_device_deactivate_unlocked(dev);
997 
998 	mutex_unlock(&dev->lock);
999 
1000 	return ret ?: count;
1001 }
1002 
1003 CONFIGFS_ATTR(gpio_sim_device_config_, live);
1004 
1005 static struct configfs_attribute *gpio_sim_device_config_attrs[] = {
1006 	&gpio_sim_device_config_attr_dev_name,
1007 	&gpio_sim_device_config_attr_live,
1008 	NULL
1009 };
1010 
1011 struct gpio_sim_chip_name_ctx {
1012 	struct fwnode_handle *swnode;
1013 	char *page;
1014 };
1015 
1016 static int gpio_sim_emit_chip_name(struct device *dev, void *data)
1017 {
1018 	struct gpio_sim_chip_name_ctx *ctx = data;
1019 
1020 	/* This would be the sysfs device exported in /sys/class/gpio. */
1021 	if (dev->class)
1022 		return 0;
1023 
1024 	if (device_match_fwnode(dev, ctx->swnode))
1025 		return sprintf(ctx->page, "%s\n", dev_name(dev));
1026 
1027 	return 0;
1028 }
1029 
1030 static ssize_t gpio_sim_bank_config_chip_name_show(struct config_item *item,
1031 						   char *page)
1032 {
1033 	struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1034 	struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1035 	struct gpio_sim_chip_name_ctx ctx = { bank->swnode, page };
1036 	int ret;
1037 
1038 	mutex_lock(&dev->lock);
1039 	if (gpio_sim_device_is_live_unlocked(dev))
1040 		ret = device_for_each_child(&dev->pdev->dev, &ctx,
1041 					    gpio_sim_emit_chip_name);
1042 	else
1043 		ret = sprintf(page, "none\n");
1044 	mutex_unlock(&dev->lock);
1045 
1046 	return ret;
1047 }
1048 
1049 CONFIGFS_ATTR_RO(gpio_sim_bank_config_, chip_name);
1050 
1051 static ssize_t
1052 gpio_sim_bank_config_label_show(struct config_item *item, char *page)
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 	int ret;
1057 
1058 	mutex_lock(&dev->lock);
1059 	ret = sprintf(page, "%s\n", bank->label ?: "");
1060 	mutex_unlock(&dev->lock);
1061 
1062 	return ret;
1063 }
1064 
1065 static ssize_t gpio_sim_bank_config_label_store(struct config_item *item,
1066 						const char *page, size_t count)
1067 {
1068 	struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1069 	struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1070 	char *trimmed;
1071 
1072 	mutex_lock(&dev->lock);
1073 
1074 	if (gpio_sim_device_is_live_unlocked(dev)) {
1075 		mutex_unlock(&dev->lock);
1076 		return -EBUSY;
1077 	}
1078 
1079 	trimmed = gpio_sim_strdup_trimmed(page, count);
1080 	if (!trimmed) {
1081 		mutex_unlock(&dev->lock);
1082 		return -ENOMEM;
1083 	}
1084 
1085 	kfree(bank->label);
1086 	bank->label = trimmed;
1087 
1088 	mutex_unlock(&dev->lock);
1089 	return count;
1090 }
1091 
1092 CONFIGFS_ATTR(gpio_sim_bank_config_, label);
1093 
1094 static ssize_t
1095 gpio_sim_bank_config_num_lines_show(struct config_item *item, char *page)
1096 {
1097 	struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1098 	struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1099 	int ret;
1100 
1101 	mutex_lock(&dev->lock);
1102 	ret = sprintf(page, "%u\n", bank->num_lines);
1103 	mutex_unlock(&dev->lock);
1104 
1105 	return ret;
1106 }
1107 
1108 static ssize_t
1109 gpio_sim_bank_config_num_lines_store(struct config_item *item,
1110 				     const char *page, size_t count)
1111 {
1112 	struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1113 	struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1114 	unsigned int num_lines;
1115 	int ret;
1116 
1117 	ret = kstrtouint(page, 0, &num_lines);
1118 	if (ret)
1119 		return ret;
1120 
1121 	if (num_lines == 0)
1122 		return -EINVAL;
1123 
1124 	mutex_lock(&dev->lock);
1125 
1126 	if (gpio_sim_device_is_live_unlocked(dev)) {
1127 		mutex_unlock(&dev->lock);
1128 		return -EBUSY;
1129 	}
1130 
1131 	bank->num_lines = num_lines;
1132 
1133 	mutex_unlock(&dev->lock);
1134 	return count;
1135 }
1136 
1137 CONFIGFS_ATTR(gpio_sim_bank_config_, num_lines);
1138 
1139 static struct configfs_attribute *gpio_sim_bank_config_attrs[] = {
1140 	&gpio_sim_bank_config_attr_chip_name,
1141 	&gpio_sim_bank_config_attr_label,
1142 	&gpio_sim_bank_config_attr_num_lines,
1143 	NULL
1144 };
1145 
1146 static ssize_t
1147 gpio_sim_line_config_name_show(struct config_item *item, char *page)
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 	int ret;
1152 
1153 	mutex_lock(&dev->lock);
1154 	ret = sprintf(page, "%s\n", line->name ?: "");
1155 	mutex_unlock(&dev->lock);
1156 
1157 	return ret;
1158 }
1159 
1160 static ssize_t gpio_sim_line_config_name_store(struct config_item *item,
1161 					       const char *page, size_t count)
1162 {
1163 	struct gpio_sim_line *line = to_gpio_sim_line(item);
1164 	struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1165 	char *trimmed;
1166 
1167 	mutex_lock(&dev->lock);
1168 
1169 	if (gpio_sim_device_is_live_unlocked(dev)) {
1170 		mutex_unlock(&dev->lock);
1171 		return -EBUSY;
1172 	}
1173 
1174 	trimmed = gpio_sim_strdup_trimmed(page, count);
1175 	if (!trimmed) {
1176 		mutex_unlock(&dev->lock);
1177 		return -ENOMEM;
1178 	}
1179 
1180 	kfree(line->name);
1181 	line->name = trimmed;
1182 
1183 	mutex_unlock(&dev->lock);
1184 
1185 	return count;
1186 }
1187 
1188 CONFIGFS_ATTR(gpio_sim_line_config_, name);
1189 
1190 static struct configfs_attribute *gpio_sim_line_config_attrs[] = {
1191 	&gpio_sim_line_config_attr_name,
1192 	NULL
1193 };
1194 
1195 static ssize_t gpio_sim_hog_config_name_show(struct config_item *item,
1196 					     char *page)
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 	int ret;
1201 
1202 	mutex_lock(&dev->lock);
1203 	ret = sprintf(page, "%s\n", hog->name ?: "");
1204 	mutex_unlock(&dev->lock);
1205 
1206 	return ret;
1207 }
1208 
1209 static ssize_t gpio_sim_hog_config_name_store(struct config_item *item,
1210 					      const char *page, size_t count)
1211 {
1212 	struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1213 	struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1214 	char *trimmed;
1215 
1216 	mutex_lock(&dev->lock);
1217 
1218 	if (gpio_sim_device_is_live_unlocked(dev)) {
1219 		mutex_unlock(&dev->lock);
1220 		return -EBUSY;
1221 	}
1222 
1223 	trimmed = gpio_sim_strdup_trimmed(page, count);
1224 	if (!trimmed) {
1225 		mutex_unlock(&dev->lock);
1226 		return -ENOMEM;
1227 	}
1228 
1229 	kfree(hog->name);
1230 	hog->name = trimmed;
1231 
1232 	mutex_unlock(&dev->lock);
1233 
1234 	return count;
1235 }
1236 
1237 CONFIGFS_ATTR(gpio_sim_hog_config_, name);
1238 
1239 static ssize_t gpio_sim_hog_config_direction_show(struct config_item *item,
1240 						  char *page)
1241 {
1242 	struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1243 	struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1244 	char *repr;
1245 	int dir;
1246 
1247 	mutex_lock(&dev->lock);
1248 	dir = hog->dir;
1249 	mutex_unlock(&dev->lock);
1250 
1251 	switch (dir) {
1252 	case GPIOD_IN:
1253 		repr = "input";
1254 		break;
1255 	case GPIOD_OUT_HIGH:
1256 		repr = "output-high";
1257 		break;
1258 	case GPIOD_OUT_LOW:
1259 		repr = "output-low";
1260 		break;
1261 	default:
1262 		/* This would be a programmer bug. */
1263 		WARN(1, "Unexpected hog direction value: %d", dir);
1264 		return -EINVAL;
1265 	}
1266 
1267 	return sprintf(page, "%s\n", repr);
1268 }
1269 
1270 static ssize_t
1271 gpio_sim_hog_config_direction_store(struct config_item *item,
1272 				    const char *page, size_t count)
1273 {
1274 	struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1275 	struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1276 	char *trimmed;
1277 	int dir;
1278 
1279 	mutex_lock(&dev->lock);
1280 
1281 	if (gpio_sim_device_is_live_unlocked(dev)) {
1282 		mutex_unlock(&dev->lock);
1283 		return -EBUSY;
1284 	}
1285 
1286 	trimmed = gpio_sim_strdup_trimmed(page, count);
1287 	if (!trimmed) {
1288 		mutex_unlock(&dev->lock);
1289 		return -ENOMEM;
1290 	}
1291 
1292 	if (strcmp(trimmed, "input") == 0)
1293 		dir = GPIOD_IN;
1294 	else if (strcmp(trimmed, "output-high") == 0)
1295 		dir = GPIOD_OUT_HIGH;
1296 	else if (strcmp(trimmed, "output-low") == 0)
1297 		dir = GPIOD_OUT_LOW;
1298 	else
1299 		dir = -EINVAL;
1300 
1301 	kfree(trimmed);
1302 
1303 	if (dir < 0) {
1304 		mutex_unlock(&dev->lock);
1305 		return dir;
1306 	}
1307 
1308 	hog->dir = dir;
1309 
1310 	mutex_unlock(&dev->lock);
1311 
1312 	return count;
1313 }
1314 
1315 CONFIGFS_ATTR(gpio_sim_hog_config_, direction);
1316 
1317 static struct configfs_attribute *gpio_sim_hog_config_attrs[] = {
1318 	&gpio_sim_hog_config_attr_name,
1319 	&gpio_sim_hog_config_attr_direction,
1320 	NULL
1321 };
1322 
1323 static void gpio_sim_hog_config_item_release(struct config_item *item)
1324 {
1325 	struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1326 	struct gpio_sim_line *line = hog->parent;
1327 	struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1328 
1329 	mutex_lock(&dev->lock);
1330 	line->hog = NULL;
1331 	mutex_unlock(&dev->lock);
1332 
1333 	kfree(hog->name);
1334 	kfree(hog);
1335 }
1336 
1337 static struct configfs_item_operations gpio_sim_hog_config_item_ops = {
1338 	.release	= gpio_sim_hog_config_item_release,
1339 };
1340 
1341 static const struct config_item_type gpio_sim_hog_config_type = {
1342 	.ct_item_ops	= &gpio_sim_hog_config_item_ops,
1343 	.ct_attrs	= gpio_sim_hog_config_attrs,
1344 	.ct_owner	= THIS_MODULE,
1345 };
1346 
1347 static struct config_item *
1348 gpio_sim_line_config_make_hog_item(struct config_group *group, const char *name)
1349 {
1350 	struct gpio_sim_line *line = to_gpio_sim_line(&group->cg_item);
1351 	struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1352 	struct gpio_sim_hog *hog;
1353 
1354 	if (strcmp(name, "hog") != 0)
1355 		return ERR_PTR(-EINVAL);
1356 
1357 	mutex_lock(&dev->lock);
1358 
1359 	hog = kzalloc(sizeof(*hog), GFP_KERNEL);
1360 	if (!hog) {
1361 		mutex_unlock(&dev->lock);
1362 		return ERR_PTR(-ENOMEM);
1363 	}
1364 
1365 	config_item_init_type_name(&hog->item, name,
1366 				   &gpio_sim_hog_config_type);
1367 
1368 	hog->dir = GPIOD_IN;
1369 	hog->name = NULL;
1370 	hog->parent = line;
1371 	line->hog = hog;
1372 
1373 	mutex_unlock(&dev->lock);
1374 
1375 	return &hog->item;
1376 }
1377 
1378 static void gpio_sim_line_config_group_release(struct config_item *item)
1379 {
1380 	struct gpio_sim_line *line = to_gpio_sim_line(item);
1381 	struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1382 
1383 	mutex_lock(&dev->lock);
1384 	list_del(&line->siblings);
1385 	mutex_unlock(&dev->lock);
1386 
1387 	kfree(line->name);
1388 	kfree(line);
1389 }
1390 
1391 static struct configfs_item_operations gpio_sim_line_config_item_ops = {
1392 	.release	= gpio_sim_line_config_group_release,
1393 };
1394 
1395 static struct configfs_group_operations gpio_sim_line_config_group_ops = {
1396 	.make_item	= gpio_sim_line_config_make_hog_item,
1397 };
1398 
1399 static const struct config_item_type gpio_sim_line_config_type = {
1400 	.ct_item_ops	= &gpio_sim_line_config_item_ops,
1401 	.ct_group_ops	= &gpio_sim_line_config_group_ops,
1402 	.ct_attrs	= gpio_sim_line_config_attrs,
1403 	.ct_owner       = THIS_MODULE,
1404 };
1405 
1406 static struct config_group *
1407 gpio_sim_bank_config_make_line_group(struct config_group *group,
1408 				     const char *name)
1409 {
1410 	struct gpio_sim_bank *bank = to_gpio_sim_bank(&group->cg_item);
1411 	struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1412 	struct gpio_sim_line *line;
1413 	unsigned int offset;
1414 	int ret, nchar;
1415 
1416 	ret = sscanf(name, "line%u%n", &offset, &nchar);
1417 	if (ret != 1 || nchar != strlen(name))
1418 		return ERR_PTR(-EINVAL);
1419 
1420 	mutex_lock(&dev->lock);
1421 
1422 	if (gpio_sim_device_is_live_unlocked(dev)) {
1423 		mutex_unlock(&dev->lock);
1424 		return ERR_PTR(-EBUSY);
1425 	}
1426 
1427 	line = kzalloc(sizeof(*line), GFP_KERNEL);
1428 	if (!line) {
1429 		mutex_unlock(&dev->lock);
1430 		return ERR_PTR(-ENOMEM);
1431 	}
1432 
1433 	config_group_init_type_name(&line->group, name,
1434 				    &gpio_sim_line_config_type);
1435 
1436 	line->parent = bank;
1437 	line->offset = offset;
1438 	list_add_tail(&line->siblings, &bank->line_list);
1439 
1440 	mutex_unlock(&dev->lock);
1441 
1442 	return &line->group;
1443 }
1444 
1445 static void gpio_sim_bank_config_group_release(struct config_item *item)
1446 {
1447 	struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1448 	struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1449 
1450 	mutex_lock(&dev->lock);
1451 	list_del(&bank->siblings);
1452 	mutex_unlock(&dev->lock);
1453 
1454 	kfree(bank->label);
1455 	kfree(bank);
1456 }
1457 
1458 static struct configfs_item_operations gpio_sim_bank_config_item_ops = {
1459 	.release	= gpio_sim_bank_config_group_release,
1460 };
1461 
1462 static struct configfs_group_operations gpio_sim_bank_config_group_ops = {
1463 	.make_group	= gpio_sim_bank_config_make_line_group,
1464 };
1465 
1466 static const struct config_item_type gpio_sim_bank_config_group_type = {
1467 	.ct_item_ops	= &gpio_sim_bank_config_item_ops,
1468 	.ct_group_ops	= &gpio_sim_bank_config_group_ops,
1469 	.ct_attrs	= gpio_sim_bank_config_attrs,
1470 	.ct_owner	= THIS_MODULE,
1471 };
1472 
1473 static struct config_group *
1474 gpio_sim_device_config_make_bank_group(struct config_group *group,
1475 				       const char *name)
1476 {
1477 	struct gpio_sim_device *dev = to_gpio_sim_device(&group->cg_item);
1478 	struct gpio_sim_bank *bank;
1479 
1480 	mutex_lock(&dev->lock);
1481 
1482 	if (gpio_sim_device_is_live_unlocked(dev)) {
1483 		mutex_unlock(&dev->lock);
1484 		return ERR_PTR(-EBUSY);
1485 	}
1486 
1487 	bank = kzalloc(sizeof(*bank), GFP_KERNEL);
1488 	if (!bank) {
1489 		mutex_unlock(&dev->lock);
1490 		return ERR_PTR(-ENOMEM);
1491 	}
1492 
1493 	config_group_init_type_name(&bank->group, name,
1494 				    &gpio_sim_bank_config_group_type);
1495 	bank->num_lines = 1;
1496 	bank->parent = dev;
1497 	INIT_LIST_HEAD(&bank->line_list);
1498 	list_add_tail(&bank->siblings, &dev->bank_list);
1499 
1500 	mutex_unlock(&dev->lock);
1501 
1502 	return &bank->group;
1503 }
1504 
1505 static void gpio_sim_device_config_group_release(struct config_item *item)
1506 {
1507 	struct gpio_sim_device *dev = to_gpio_sim_device(item);
1508 
1509 	mutex_lock(&dev->lock);
1510 	if (gpio_sim_device_is_live_unlocked(dev))
1511 		gpio_sim_device_deactivate_unlocked(dev);
1512 	mutex_unlock(&dev->lock);
1513 
1514 	mutex_destroy(&dev->lock);
1515 	ida_free(&gpio_sim_ida, dev->id);
1516 	kfree(dev);
1517 }
1518 
1519 static struct configfs_item_operations gpio_sim_device_config_item_ops = {
1520 	.release	= gpio_sim_device_config_group_release,
1521 };
1522 
1523 static struct configfs_group_operations gpio_sim_device_config_group_ops = {
1524 	.make_group	= gpio_sim_device_config_make_bank_group,
1525 };
1526 
1527 static const struct config_item_type gpio_sim_device_config_group_type = {
1528 	.ct_item_ops	= &gpio_sim_device_config_item_ops,
1529 	.ct_group_ops	= &gpio_sim_device_config_group_ops,
1530 	.ct_attrs	= gpio_sim_device_config_attrs,
1531 	.ct_owner	= THIS_MODULE,
1532 };
1533 
1534 static struct config_group *
1535 gpio_sim_config_make_device_group(struct config_group *group, const char *name)
1536 {
1537 	struct gpio_sim_device *dev;
1538 	int id;
1539 
1540 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1541 	if (!dev)
1542 		return ERR_PTR(-ENOMEM);
1543 
1544 	id = ida_alloc(&gpio_sim_ida, GFP_KERNEL);
1545 	if (id < 0) {
1546 		kfree(dev);
1547 		return ERR_PTR(id);
1548 	}
1549 
1550 	config_group_init_type_name(&dev->group, name,
1551 				    &gpio_sim_device_config_group_type);
1552 	dev->id = id;
1553 	mutex_init(&dev->lock);
1554 	INIT_LIST_HEAD(&dev->bank_list);
1555 
1556 	dev->bus_notifier.notifier_call = gpio_sim_bus_notifier_call;
1557 	init_completion(&dev->probe_completion);
1558 
1559 	return &dev->group;
1560 }
1561 
1562 static struct configfs_group_operations gpio_sim_config_group_ops = {
1563 	.make_group	= gpio_sim_config_make_device_group,
1564 };
1565 
1566 static const struct config_item_type gpio_sim_config_type = {
1567 	.ct_group_ops	= &gpio_sim_config_group_ops,
1568 	.ct_owner	= THIS_MODULE,
1569 };
1570 
1571 static struct configfs_subsystem gpio_sim_config_subsys = {
1572 	.su_group = {
1573 		.cg_item = {
1574 			.ci_namebuf	= "gpio-sim",
1575 			.ci_type	= &gpio_sim_config_type,
1576 		},
1577 	},
1578 };
1579 
1580 static int __init gpio_sim_init(void)
1581 {
1582 	int ret;
1583 
1584 	ret = platform_driver_register(&gpio_sim_driver);
1585 	if (ret) {
1586 		pr_err("Error %d while registering the platform driver\n", ret);
1587 		return ret;
1588 	}
1589 
1590 	config_group_init(&gpio_sim_config_subsys.su_group);
1591 	mutex_init(&gpio_sim_config_subsys.su_mutex);
1592 	ret = configfs_register_subsystem(&gpio_sim_config_subsys);
1593 	if (ret) {
1594 		pr_err("Error %d while registering the configfs subsystem %s\n",
1595 		       ret, gpio_sim_config_subsys.su_group.cg_item.ci_namebuf);
1596 		mutex_destroy(&gpio_sim_config_subsys.su_mutex);
1597 		platform_driver_unregister(&gpio_sim_driver);
1598 		return ret;
1599 	}
1600 
1601 	return 0;
1602 }
1603 module_init(gpio_sim_init);
1604 
1605 static void __exit gpio_sim_exit(void)
1606 {
1607 	configfs_unregister_subsystem(&gpio_sim_config_subsys);
1608 	mutex_destroy(&gpio_sim_config_subsys.su_mutex);
1609 	platform_driver_unregister(&gpio_sim_driver);
1610 }
1611 module_exit(gpio_sim_exit);
1612 
1613 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl");
1614 MODULE_DESCRIPTION("GPIO Simulator Module");
1615 MODULE_LICENSE("GPL");
1616