xref: /openbmc/linux/drivers/gpio/gpio-sim.c (revision 3180449e)
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 
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->offset >= bank->num_lines)
700 			continue;
701 
702 		if (line->name) {
703 			if (line->offset > max_offset)
704 				max_offset = line->offset;
705 
706 			/*
707 			 * max_offset can stay at 0 so it's not an indicator
708 			 * of whether line names were configured at all.
709 			 */
710 			has_line_names = true;
711 		}
712 	}
713 
714 	if (!has_line_names)
715 		/*
716 		 * This is not an error - NULL means, there are no line
717 		 * names configured.
718 		 */
719 		return NULL;
720 
721 	*line_names_size = max_offset + 1;
722 
723 	line_names = kcalloc(*line_names_size, sizeof(*line_names), GFP_KERNEL);
724 	if (!line_names)
725 		return ERR_PTR(-ENOMEM);
726 
727 	list_for_each_entry(line, &bank->line_list, siblings) {
728 		if (line->offset >= bank->num_lines)
729 			continue;
730 
731 		if (line->name && (line->offset <= max_offset))
732 			line_names[line->offset] = line->name;
733 	}
734 
735 	return line_names;
736 }
737 
738 static void gpio_sim_remove_hogs(struct gpio_sim_device *dev)
739 {
740 	struct gpiod_hog *hog;
741 
742 	if (!dev->hogs)
743 		return;
744 
745 	gpiod_remove_hogs(dev->hogs);
746 
747 	for (hog = dev->hogs; hog->chip_label; hog++) {
748 		kfree(hog->chip_label);
749 		kfree(hog->line_name);
750 	}
751 
752 	kfree(dev->hogs);
753 	dev->hogs = NULL;
754 }
755 
756 static int gpio_sim_add_hogs(struct gpio_sim_device *dev)
757 {
758 	unsigned int num_hogs = 0, idx = 0;
759 	struct gpio_sim_bank *bank;
760 	struct gpio_sim_line *line;
761 	struct gpiod_hog *hog;
762 
763 	list_for_each_entry(bank, &dev->bank_list, siblings) {
764 		list_for_each_entry(line, &bank->line_list, siblings) {
765 			if (line->offset >= bank->num_lines)
766 				continue;
767 
768 			if (line->hog)
769 				num_hogs++;
770 		}
771 	}
772 
773 	if (!num_hogs)
774 		return 0;
775 
776 	/* Allocate one more for the sentinel. */
777 	dev->hogs = kcalloc(num_hogs + 1, sizeof(*dev->hogs), GFP_KERNEL);
778 	if (!dev->hogs)
779 		return -ENOMEM;
780 
781 	list_for_each_entry(bank, &dev->bank_list, siblings) {
782 		list_for_each_entry(line, &bank->line_list, siblings) {
783 			if (line->offset >= bank->num_lines)
784 				continue;
785 
786 			if (!line->hog)
787 				continue;
788 
789 			hog = &dev->hogs[idx++];
790 
791 			/*
792 			 * We need to make this string manually because at this
793 			 * point the device doesn't exist yet and so dev_name()
794 			 * is not available.
795 			 */
796 			if (gpio_sim_bank_has_label(bank))
797 				hog->chip_label = kstrdup(bank->label,
798 							  GFP_KERNEL);
799 			else
800 				hog->chip_label = kasprintf(GFP_KERNEL,
801 							"gpio-sim.%u-%pfwP",
802 							dev->id,
803 							bank->swnode);
804 			if (!hog->chip_label) {
805 				gpio_sim_remove_hogs(dev);
806 				return -ENOMEM;
807 			}
808 
809 			/*
810 			 * We need to duplicate this because the hog config
811 			 * item can be removed at any time (and we can't block
812 			 * it) and gpiolib doesn't make a deep copy of the hog
813 			 * data.
814 			 */
815 			if (line->hog->name) {
816 				hog->line_name = kstrdup(line->hog->name,
817 							 GFP_KERNEL);
818 				if (!hog->line_name) {
819 					gpio_sim_remove_hogs(dev);
820 					return -ENOMEM;
821 				}
822 			}
823 
824 			hog->chip_hwnum = line->offset;
825 			hog->dflags = line->hog->dir;
826 		}
827 	}
828 
829 	gpiod_add_hogs(dev->hogs);
830 
831 	return 0;
832 }
833 
834 static struct fwnode_handle *
835 gpio_sim_make_bank_swnode(struct gpio_sim_bank *bank,
836 			  struct fwnode_handle *parent)
837 {
838 	struct property_entry properties[GPIO_SIM_PROP_MAX];
839 	unsigned int prop_idx = 0, line_names_size = 0;
840 	struct fwnode_handle *swnode;
841 	char **line_names;
842 
843 	memset(properties, 0, sizeof(properties));
844 
845 	properties[prop_idx++] = PROPERTY_ENTRY_U32("ngpios", bank->num_lines);
846 
847 	if (gpio_sim_bank_has_label(bank))
848 		properties[prop_idx++] = PROPERTY_ENTRY_STRING("gpio-sim,label",
849 							       bank->label);
850 
851 	line_names = gpio_sim_make_line_names(bank, &line_names_size);
852 	if (IS_ERR(line_names))
853 		return ERR_CAST(line_names);
854 
855 	if (line_names)
856 		properties[prop_idx++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
857 						"gpio-line-names",
858 						line_names, line_names_size);
859 
860 	swnode = fwnode_create_software_node(properties, parent);
861 	kfree(line_names);
862 	return swnode;
863 }
864 
865 static void gpio_sim_remove_swnode_recursive(struct fwnode_handle *swnode)
866 {
867 	struct fwnode_handle *child;
868 
869 	fwnode_for_each_child_node(swnode, child)
870 		fwnode_remove_software_node(child);
871 
872 	fwnode_remove_software_node(swnode);
873 }
874 
875 static bool gpio_sim_bank_labels_non_unique(struct gpio_sim_device *dev)
876 {
877 	struct gpio_sim_bank *this, *pos;
878 
879 	list_for_each_entry(this, &dev->bank_list, siblings) {
880 		list_for_each_entry(pos, &dev->bank_list, siblings) {
881 			if (this == pos || (!this->label || !pos->label))
882 				continue;
883 
884 			if (strcmp(this->label, pos->label) == 0)
885 				return true;
886 		}
887 	}
888 
889 	return false;
890 }
891 
892 static int gpio_sim_device_activate_unlocked(struct gpio_sim_device *dev)
893 {
894 	struct platform_device_info pdevinfo;
895 	struct fwnode_handle *swnode;
896 	struct platform_device *pdev;
897 	struct gpio_sim_bank *bank;
898 	int ret;
899 
900 	if (list_empty(&dev->bank_list))
901 		return -ENODATA;
902 
903 	/*
904 	 * Non-unique GPIO device labels are a corner-case we don't support
905 	 * as it would interfere with machine hogging mechanism and has little
906 	 * use in real life.
907 	 */
908 	if (gpio_sim_bank_labels_non_unique(dev))
909 		return -EINVAL;
910 
911 	memset(&pdevinfo, 0, sizeof(pdevinfo));
912 
913 	swnode = fwnode_create_software_node(NULL, NULL);
914 	if (IS_ERR(swnode))
915 		return PTR_ERR(swnode);
916 
917 	list_for_each_entry(bank, &dev->bank_list, siblings) {
918 		bank->swnode = gpio_sim_make_bank_swnode(bank, swnode);
919 		if (IS_ERR(bank->swnode)) {
920 			ret = PTR_ERR(bank->swnode);
921 			gpio_sim_remove_swnode_recursive(swnode);
922 			return ret;
923 		}
924 	}
925 
926 	ret = gpio_sim_add_hogs(dev);
927 	if (ret) {
928 		gpio_sim_remove_swnode_recursive(swnode);
929 		return ret;
930 	}
931 
932 	pdevinfo.name = "gpio-sim";
933 	pdevinfo.fwnode = swnode;
934 	pdevinfo.id = dev->id;
935 
936 	reinit_completion(&dev->probe_completion);
937 	dev->driver_bound = false;
938 	bus_register_notifier(&platform_bus_type, &dev->bus_notifier);
939 
940 	pdev = platform_device_register_full(&pdevinfo);
941 	if (IS_ERR(pdev)) {
942 		bus_unregister_notifier(&platform_bus_type, &dev->bus_notifier);
943 		gpio_sim_remove_hogs(dev);
944 		gpio_sim_remove_swnode_recursive(swnode);
945 		return PTR_ERR(pdev);
946 	}
947 
948 	wait_for_completion(&dev->probe_completion);
949 	bus_unregister_notifier(&platform_bus_type, &dev->bus_notifier);
950 
951 	if (!dev->driver_bound) {
952 		/* Probe failed, check kernel log. */
953 		platform_device_unregister(pdev);
954 		gpio_sim_remove_hogs(dev);
955 		gpio_sim_remove_swnode_recursive(swnode);
956 		return -ENXIO;
957 	}
958 
959 	dev->pdev = pdev;
960 
961 	return 0;
962 }
963 
964 static void gpio_sim_device_deactivate_unlocked(struct gpio_sim_device *dev)
965 {
966 	struct fwnode_handle *swnode;
967 
968 	swnode = dev_fwnode(&dev->pdev->dev);
969 	platform_device_unregister(dev->pdev);
970 	gpio_sim_remove_hogs(dev);
971 	gpio_sim_remove_swnode_recursive(swnode);
972 	dev->pdev = NULL;
973 }
974 
975 static ssize_t
976 gpio_sim_device_config_live_store(struct config_item *item,
977 				  const char *page, size_t count)
978 {
979 	struct gpio_sim_device *dev = to_gpio_sim_device(item);
980 	bool live;
981 	int ret;
982 
983 	ret = kstrtobool(page, &live);
984 	if (ret)
985 		return ret;
986 
987 	mutex_lock(&dev->lock);
988 
989 	if ((!live && !gpio_sim_device_is_live_unlocked(dev)) ||
990 	    (live && gpio_sim_device_is_live_unlocked(dev)))
991 		ret = -EPERM;
992 	else if (live)
993 		ret = gpio_sim_device_activate_unlocked(dev);
994 	else
995 		gpio_sim_device_deactivate_unlocked(dev);
996 
997 	mutex_unlock(&dev->lock);
998 
999 	return ret ?: count;
1000 }
1001 
1002 CONFIGFS_ATTR(gpio_sim_device_config_, live);
1003 
1004 static struct configfs_attribute *gpio_sim_device_config_attrs[] = {
1005 	&gpio_sim_device_config_attr_dev_name,
1006 	&gpio_sim_device_config_attr_live,
1007 	NULL
1008 };
1009 
1010 struct gpio_sim_chip_name_ctx {
1011 	struct fwnode_handle *swnode;
1012 	char *page;
1013 };
1014 
1015 static int gpio_sim_emit_chip_name(struct device *dev, void *data)
1016 {
1017 	struct gpio_sim_chip_name_ctx *ctx = data;
1018 
1019 	/* This would be the sysfs device exported in /sys/class/gpio. */
1020 	if (dev->class)
1021 		return 0;
1022 
1023 	if (device_match_fwnode(dev, ctx->swnode))
1024 		return sprintf(ctx->page, "%s\n", dev_name(dev));
1025 
1026 	return 0;
1027 }
1028 
1029 static ssize_t gpio_sim_bank_config_chip_name_show(struct config_item *item,
1030 						   char *page)
1031 {
1032 	struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1033 	struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1034 	struct gpio_sim_chip_name_ctx ctx = { bank->swnode, page };
1035 	int ret;
1036 
1037 	mutex_lock(&dev->lock);
1038 	if (gpio_sim_device_is_live_unlocked(dev))
1039 		ret = device_for_each_child(&dev->pdev->dev, &ctx,
1040 					    gpio_sim_emit_chip_name);
1041 	else
1042 		ret = sprintf(page, "none\n");
1043 	mutex_unlock(&dev->lock);
1044 
1045 	return ret;
1046 }
1047 
1048 CONFIGFS_ATTR_RO(gpio_sim_bank_config_, chip_name);
1049 
1050 static ssize_t
1051 gpio_sim_bank_config_label_show(struct config_item *item, char *page)
1052 {
1053 	struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1054 	struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1055 	int ret;
1056 
1057 	mutex_lock(&dev->lock);
1058 	ret = sprintf(page, "%s\n", bank->label ?: "");
1059 	mutex_unlock(&dev->lock);
1060 
1061 	return ret;
1062 }
1063 
1064 static ssize_t gpio_sim_bank_config_label_store(struct config_item *item,
1065 						const char *page, size_t count)
1066 {
1067 	struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1068 	struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1069 	char *trimmed;
1070 
1071 	mutex_lock(&dev->lock);
1072 
1073 	if (gpio_sim_device_is_live_unlocked(dev)) {
1074 		mutex_unlock(&dev->lock);
1075 		return -EBUSY;
1076 	}
1077 
1078 	trimmed = gpio_sim_strdup_trimmed(page, count);
1079 	if (!trimmed) {
1080 		mutex_unlock(&dev->lock);
1081 		return -ENOMEM;
1082 	}
1083 
1084 	kfree(bank->label);
1085 	bank->label = trimmed;
1086 
1087 	mutex_unlock(&dev->lock);
1088 	return count;
1089 }
1090 
1091 CONFIGFS_ATTR(gpio_sim_bank_config_, label);
1092 
1093 static ssize_t
1094 gpio_sim_bank_config_num_lines_show(struct config_item *item, char *page)
1095 {
1096 	struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1097 	struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1098 	int ret;
1099 
1100 	mutex_lock(&dev->lock);
1101 	ret = sprintf(page, "%u\n", bank->num_lines);
1102 	mutex_unlock(&dev->lock);
1103 
1104 	return ret;
1105 }
1106 
1107 static ssize_t
1108 gpio_sim_bank_config_num_lines_store(struct config_item *item,
1109 				     const char *page, size_t count)
1110 {
1111 	struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1112 	struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1113 	unsigned int num_lines;
1114 	int ret;
1115 
1116 	ret = kstrtouint(page, 0, &num_lines);
1117 	if (ret)
1118 		return ret;
1119 
1120 	if (num_lines == 0)
1121 		return -EINVAL;
1122 
1123 	mutex_lock(&dev->lock);
1124 
1125 	if (gpio_sim_device_is_live_unlocked(dev)) {
1126 		mutex_unlock(&dev->lock);
1127 		return -EBUSY;
1128 	}
1129 
1130 	bank->num_lines = num_lines;
1131 
1132 	mutex_unlock(&dev->lock);
1133 	return count;
1134 }
1135 
1136 CONFIGFS_ATTR(gpio_sim_bank_config_, num_lines);
1137 
1138 static struct configfs_attribute *gpio_sim_bank_config_attrs[] = {
1139 	&gpio_sim_bank_config_attr_chip_name,
1140 	&gpio_sim_bank_config_attr_label,
1141 	&gpio_sim_bank_config_attr_num_lines,
1142 	NULL
1143 };
1144 
1145 static ssize_t
1146 gpio_sim_line_config_name_show(struct config_item *item, char *page)
1147 {
1148 	struct gpio_sim_line *line = to_gpio_sim_line(item);
1149 	struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1150 	int ret;
1151 
1152 	mutex_lock(&dev->lock);
1153 	ret = sprintf(page, "%s\n", line->name ?: "");
1154 	mutex_unlock(&dev->lock);
1155 
1156 	return ret;
1157 }
1158 
1159 static ssize_t gpio_sim_line_config_name_store(struct config_item *item,
1160 					       const char *page, size_t count)
1161 {
1162 	struct gpio_sim_line *line = to_gpio_sim_line(item);
1163 	struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1164 	char *trimmed;
1165 
1166 	mutex_lock(&dev->lock);
1167 
1168 	if (gpio_sim_device_is_live_unlocked(dev)) {
1169 		mutex_unlock(&dev->lock);
1170 		return -EBUSY;
1171 	}
1172 
1173 	trimmed = gpio_sim_strdup_trimmed(page, count);
1174 	if (!trimmed) {
1175 		mutex_unlock(&dev->lock);
1176 		return -ENOMEM;
1177 	}
1178 
1179 	kfree(line->name);
1180 	line->name = trimmed;
1181 
1182 	mutex_unlock(&dev->lock);
1183 
1184 	return count;
1185 }
1186 
1187 CONFIGFS_ATTR(gpio_sim_line_config_, name);
1188 
1189 static struct configfs_attribute *gpio_sim_line_config_attrs[] = {
1190 	&gpio_sim_line_config_attr_name,
1191 	NULL
1192 };
1193 
1194 static ssize_t gpio_sim_hog_config_name_show(struct config_item *item,
1195 					     char *page)
1196 {
1197 	struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1198 	struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1199 	int ret;
1200 
1201 	mutex_lock(&dev->lock);
1202 	ret = sprintf(page, "%s\n", hog->name ?: "");
1203 	mutex_unlock(&dev->lock);
1204 
1205 	return ret;
1206 }
1207 
1208 static ssize_t gpio_sim_hog_config_name_store(struct config_item *item,
1209 					      const char *page, size_t count)
1210 {
1211 	struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1212 	struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1213 	char *trimmed;
1214 
1215 	mutex_lock(&dev->lock);
1216 
1217 	if (gpio_sim_device_is_live_unlocked(dev)) {
1218 		mutex_unlock(&dev->lock);
1219 		return -EBUSY;
1220 	}
1221 
1222 	trimmed = gpio_sim_strdup_trimmed(page, count);
1223 	if (!trimmed) {
1224 		mutex_unlock(&dev->lock);
1225 		return -ENOMEM;
1226 	}
1227 
1228 	kfree(hog->name);
1229 	hog->name = trimmed;
1230 
1231 	mutex_unlock(&dev->lock);
1232 
1233 	return count;
1234 }
1235 
1236 CONFIGFS_ATTR(gpio_sim_hog_config_, name);
1237 
1238 static ssize_t gpio_sim_hog_config_direction_show(struct config_item *item,
1239 						  char *page)
1240 {
1241 	struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1242 	struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1243 	char *repr;
1244 	int dir;
1245 
1246 	mutex_lock(&dev->lock);
1247 	dir = hog->dir;
1248 	mutex_unlock(&dev->lock);
1249 
1250 	switch (dir) {
1251 	case GPIOD_IN:
1252 		repr = "input";
1253 		break;
1254 	case GPIOD_OUT_HIGH:
1255 		repr = "output-high";
1256 		break;
1257 	case GPIOD_OUT_LOW:
1258 		repr = "output-low";
1259 		break;
1260 	default:
1261 		/* This would be a programmer bug. */
1262 		WARN(1, "Unexpected hog direction value: %d", dir);
1263 		return -EINVAL;
1264 	}
1265 
1266 	return sprintf(page, "%s\n", repr);
1267 }
1268 
1269 static ssize_t
1270 gpio_sim_hog_config_direction_store(struct config_item *item,
1271 				    const char *page, size_t count)
1272 {
1273 	struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1274 	struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1275 	char *trimmed;
1276 	int dir;
1277 
1278 	mutex_lock(&dev->lock);
1279 
1280 	if (gpio_sim_device_is_live_unlocked(dev)) {
1281 		mutex_unlock(&dev->lock);
1282 		return -EBUSY;
1283 	}
1284 
1285 	trimmed = gpio_sim_strdup_trimmed(page, count);
1286 	if (!trimmed) {
1287 		mutex_unlock(&dev->lock);
1288 		return -ENOMEM;
1289 	}
1290 
1291 	if (strcmp(trimmed, "input") == 0)
1292 		dir = GPIOD_IN;
1293 	else if (strcmp(trimmed, "output-high") == 0)
1294 		dir = GPIOD_OUT_HIGH;
1295 	else if (strcmp(trimmed, "output-low") == 0)
1296 		dir = GPIOD_OUT_LOW;
1297 	else
1298 		dir = -EINVAL;
1299 
1300 	kfree(trimmed);
1301 
1302 	if (dir < 0) {
1303 		mutex_unlock(&dev->lock);
1304 		return dir;
1305 	}
1306 
1307 	hog->dir = dir;
1308 
1309 	mutex_unlock(&dev->lock);
1310 
1311 	return count;
1312 }
1313 
1314 CONFIGFS_ATTR(gpio_sim_hog_config_, direction);
1315 
1316 static struct configfs_attribute *gpio_sim_hog_config_attrs[] = {
1317 	&gpio_sim_hog_config_attr_name,
1318 	&gpio_sim_hog_config_attr_direction,
1319 	NULL
1320 };
1321 
1322 static void gpio_sim_hog_config_item_release(struct config_item *item)
1323 {
1324 	struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1325 	struct gpio_sim_line *line = hog->parent;
1326 	struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1327 
1328 	mutex_lock(&dev->lock);
1329 	line->hog = NULL;
1330 	mutex_unlock(&dev->lock);
1331 
1332 	kfree(hog->name);
1333 	kfree(hog);
1334 }
1335 
1336 static struct configfs_item_operations gpio_sim_hog_config_item_ops = {
1337 	.release	= gpio_sim_hog_config_item_release,
1338 };
1339 
1340 static const struct config_item_type gpio_sim_hog_config_type = {
1341 	.ct_item_ops	= &gpio_sim_hog_config_item_ops,
1342 	.ct_attrs	= gpio_sim_hog_config_attrs,
1343 	.ct_owner	= THIS_MODULE,
1344 };
1345 
1346 static struct config_item *
1347 gpio_sim_line_config_make_hog_item(struct config_group *group, const char *name)
1348 {
1349 	struct gpio_sim_line *line = to_gpio_sim_line(&group->cg_item);
1350 	struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1351 	struct gpio_sim_hog *hog;
1352 
1353 	if (strcmp(name, "hog") != 0)
1354 		return ERR_PTR(-EINVAL);
1355 
1356 	mutex_lock(&dev->lock);
1357 
1358 	hog = kzalloc(sizeof(*hog), GFP_KERNEL);
1359 	if (!hog) {
1360 		mutex_unlock(&dev->lock);
1361 		return ERR_PTR(-ENOMEM);
1362 	}
1363 
1364 	config_item_init_type_name(&hog->item, name,
1365 				   &gpio_sim_hog_config_type);
1366 
1367 	hog->dir = GPIOD_IN;
1368 	hog->name = NULL;
1369 	hog->parent = line;
1370 	line->hog = hog;
1371 
1372 	mutex_unlock(&dev->lock);
1373 
1374 	return &hog->item;
1375 }
1376 
1377 static void gpio_sim_line_config_group_release(struct config_item *item)
1378 {
1379 	struct gpio_sim_line *line = to_gpio_sim_line(item);
1380 	struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1381 
1382 	mutex_lock(&dev->lock);
1383 	list_del(&line->siblings);
1384 	mutex_unlock(&dev->lock);
1385 
1386 	kfree(line->name);
1387 	kfree(line);
1388 }
1389 
1390 static struct configfs_item_operations gpio_sim_line_config_item_ops = {
1391 	.release	= gpio_sim_line_config_group_release,
1392 };
1393 
1394 static struct configfs_group_operations gpio_sim_line_config_group_ops = {
1395 	.make_item	= gpio_sim_line_config_make_hog_item,
1396 };
1397 
1398 static const struct config_item_type gpio_sim_line_config_type = {
1399 	.ct_item_ops	= &gpio_sim_line_config_item_ops,
1400 	.ct_group_ops	= &gpio_sim_line_config_group_ops,
1401 	.ct_attrs	= gpio_sim_line_config_attrs,
1402 	.ct_owner       = THIS_MODULE,
1403 };
1404 
1405 static struct config_group *
1406 gpio_sim_bank_config_make_line_group(struct config_group *group,
1407 				     const char *name)
1408 {
1409 	struct gpio_sim_bank *bank = to_gpio_sim_bank(&group->cg_item);
1410 	struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1411 	struct gpio_sim_line *line;
1412 	unsigned int offset;
1413 	int ret, nchar;
1414 
1415 	ret = sscanf(name, "line%u%n", &offset, &nchar);
1416 	if (ret != 1 || nchar != strlen(name))
1417 		return ERR_PTR(-EINVAL);
1418 
1419 	mutex_lock(&dev->lock);
1420 
1421 	if (gpio_sim_device_is_live_unlocked(dev)) {
1422 		mutex_unlock(&dev->lock);
1423 		return ERR_PTR(-EBUSY);
1424 	}
1425 
1426 	line = kzalloc(sizeof(*line), GFP_KERNEL);
1427 	if (!line) {
1428 		mutex_unlock(&dev->lock);
1429 		return ERR_PTR(-ENOMEM);
1430 	}
1431 
1432 	config_group_init_type_name(&line->group, name,
1433 				    &gpio_sim_line_config_type);
1434 
1435 	line->parent = bank;
1436 	line->offset = offset;
1437 	list_add_tail(&line->siblings, &bank->line_list);
1438 
1439 	mutex_unlock(&dev->lock);
1440 
1441 	return &line->group;
1442 }
1443 
1444 static void gpio_sim_bank_config_group_release(struct config_item *item)
1445 {
1446 	struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1447 	struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1448 
1449 	mutex_lock(&dev->lock);
1450 	list_del(&bank->siblings);
1451 	mutex_unlock(&dev->lock);
1452 
1453 	kfree(bank->label);
1454 	kfree(bank);
1455 }
1456 
1457 static struct configfs_item_operations gpio_sim_bank_config_item_ops = {
1458 	.release	= gpio_sim_bank_config_group_release,
1459 };
1460 
1461 static struct configfs_group_operations gpio_sim_bank_config_group_ops = {
1462 	.make_group	= gpio_sim_bank_config_make_line_group,
1463 };
1464 
1465 static const struct config_item_type gpio_sim_bank_config_group_type = {
1466 	.ct_item_ops	= &gpio_sim_bank_config_item_ops,
1467 	.ct_group_ops	= &gpio_sim_bank_config_group_ops,
1468 	.ct_attrs	= gpio_sim_bank_config_attrs,
1469 	.ct_owner	= THIS_MODULE,
1470 };
1471 
1472 static struct config_group *
1473 gpio_sim_device_config_make_bank_group(struct config_group *group,
1474 				       const char *name)
1475 {
1476 	struct gpio_sim_device *dev = to_gpio_sim_device(&group->cg_item);
1477 	struct gpio_sim_bank *bank;
1478 
1479 	mutex_lock(&dev->lock);
1480 
1481 	if (gpio_sim_device_is_live_unlocked(dev)) {
1482 		mutex_unlock(&dev->lock);
1483 		return ERR_PTR(-EBUSY);
1484 	}
1485 
1486 	bank = kzalloc(sizeof(*bank), GFP_KERNEL);
1487 	if (!bank) {
1488 		mutex_unlock(&dev->lock);
1489 		return ERR_PTR(-ENOMEM);
1490 	}
1491 
1492 	config_group_init_type_name(&bank->group, name,
1493 				    &gpio_sim_bank_config_group_type);
1494 	bank->num_lines = 1;
1495 	bank->parent = dev;
1496 	INIT_LIST_HEAD(&bank->line_list);
1497 	list_add_tail(&bank->siblings, &dev->bank_list);
1498 
1499 	mutex_unlock(&dev->lock);
1500 
1501 	return &bank->group;
1502 }
1503 
1504 static void gpio_sim_device_config_group_release(struct config_item *item)
1505 {
1506 	struct gpio_sim_device *dev = to_gpio_sim_device(item);
1507 
1508 	mutex_lock(&dev->lock);
1509 	if (gpio_sim_device_is_live_unlocked(dev))
1510 		gpio_sim_device_deactivate_unlocked(dev);
1511 	mutex_unlock(&dev->lock);
1512 
1513 	mutex_destroy(&dev->lock);
1514 	ida_free(&gpio_sim_ida, dev->id);
1515 	kfree(dev);
1516 }
1517 
1518 static struct configfs_item_operations gpio_sim_device_config_item_ops = {
1519 	.release	= gpio_sim_device_config_group_release,
1520 };
1521 
1522 static struct configfs_group_operations gpio_sim_device_config_group_ops = {
1523 	.make_group	= gpio_sim_device_config_make_bank_group,
1524 };
1525 
1526 static const struct config_item_type gpio_sim_device_config_group_type = {
1527 	.ct_item_ops	= &gpio_sim_device_config_item_ops,
1528 	.ct_group_ops	= &gpio_sim_device_config_group_ops,
1529 	.ct_attrs	= gpio_sim_device_config_attrs,
1530 	.ct_owner	= THIS_MODULE,
1531 };
1532 
1533 static struct config_group *
1534 gpio_sim_config_make_device_group(struct config_group *group, const char *name)
1535 {
1536 	struct gpio_sim_device *dev;
1537 	int id;
1538 
1539 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1540 	if (!dev)
1541 		return ERR_PTR(-ENOMEM);
1542 
1543 	id = ida_alloc(&gpio_sim_ida, GFP_KERNEL);
1544 	if (id < 0) {
1545 		kfree(dev);
1546 		return ERR_PTR(id);
1547 	}
1548 
1549 	config_group_init_type_name(&dev->group, name,
1550 				    &gpio_sim_device_config_group_type);
1551 	dev->id = id;
1552 	mutex_init(&dev->lock);
1553 	INIT_LIST_HEAD(&dev->bank_list);
1554 
1555 	dev->bus_notifier.notifier_call = gpio_sim_bus_notifier_call;
1556 	init_completion(&dev->probe_completion);
1557 
1558 	return &dev->group;
1559 }
1560 
1561 static struct configfs_group_operations gpio_sim_config_group_ops = {
1562 	.make_group	= gpio_sim_config_make_device_group,
1563 };
1564 
1565 static const struct config_item_type gpio_sim_config_type = {
1566 	.ct_group_ops	= &gpio_sim_config_group_ops,
1567 	.ct_owner	= THIS_MODULE,
1568 };
1569 
1570 static struct configfs_subsystem gpio_sim_config_subsys = {
1571 	.su_group = {
1572 		.cg_item = {
1573 			.ci_namebuf	= "gpio-sim",
1574 			.ci_type	= &gpio_sim_config_type,
1575 		},
1576 	},
1577 };
1578 
1579 static int __init gpio_sim_init(void)
1580 {
1581 	int ret;
1582 
1583 	ret = platform_driver_register(&gpio_sim_driver);
1584 	if (ret) {
1585 		pr_err("Error %d while registering the platform driver\n", ret);
1586 		return ret;
1587 	}
1588 
1589 	config_group_init(&gpio_sim_config_subsys.su_group);
1590 	mutex_init(&gpio_sim_config_subsys.su_mutex);
1591 	ret = configfs_register_subsystem(&gpio_sim_config_subsys);
1592 	if (ret) {
1593 		pr_err("Error %d while registering the configfs subsystem %s\n",
1594 		       ret, gpio_sim_config_subsys.su_group.cg_item.ci_namebuf);
1595 		mutex_destroy(&gpio_sim_config_subsys.su_mutex);
1596 		platform_driver_unregister(&gpio_sim_driver);
1597 		return ret;
1598 	}
1599 
1600 	return 0;
1601 }
1602 module_init(gpio_sim_init);
1603 
1604 static void __exit gpio_sim_exit(void)
1605 {
1606 	configfs_unregister_subsystem(&gpio_sim_config_subsys);
1607 	mutex_destroy(&gpio_sim_config_subsys.su_mutex);
1608 	platform_driver_unregister(&gpio_sim_driver);
1609 }
1610 module_exit(gpio_sim_exit);
1611 
1612 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl");
1613 MODULE_DESCRIPTION("GPIO Simulator Module");
1614 MODULE_LICENSE("GPL");
1615