1 // SPDX-License-Identifier: GPL-2.0-only
3 * Configurable virtual GPIO consumer module.
5 * Copyright (C) 2023-2024 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/array_size.h>
11 #include <linux/atomic.h>
12 #include <linux/bitmap.h>
13 #include <linux/cleanup.h>
14 #include <linux/completion.h>
15 #include <linux/configfs.h>
16 #include <linux/debugfs.h>
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/gpio/machine.h>
21 #include <linux/idr.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq_work.h>
24 #include <linux/limits.h>
25 #include <linux/list.h>
26 #include <linux/lockdep.h>
27 #include <linux/mod_devicetable.h>
28 #include <linux/module.h>
29 #include <linux/mutex.h>
30 #include <linux/notifier.h>
32 #include <linux/overflow.h>
33 #include <linux/platform_device.h>
34 #include <linux/printk.h>
35 #include <linux/property.h>
36 #include <linux/slab.h>
37 #include <linux/string_helpers.h>
38 #include <linux/types.h>
40 #define GPIO_VIRTUSER_NAME_BUF_LEN 32
42 static DEFINE_IDA(gpio_virtuser_ida
);
43 static struct dentry
*gpio_virtuser_dbg_root
;
45 struct gpio_virtuser_attr_data
{
47 struct gpio_desc
*desc
;
48 struct gpio_descs
*descs
;
50 struct dentry
*dbgfs_dir
;
53 struct gpio_virtuser_line_array_data
{
54 struct gpio_virtuser_attr_data ad
;
57 struct gpio_virtuser_line_data
{
58 struct gpio_virtuser_attr_data ad
;
59 char consumer
[GPIO_VIRTUSER_NAME_BUF_LEN
];
60 struct mutex consumer_lock
;
61 unsigned int debounce
;
66 struct gpio_virtuser_dbgfs_attr_descr
{
68 const struct file_operations
*fops
;
71 struct gpio_virtuser_irq_work_context
{
73 struct completion work_completion
;
76 struct gpio_desc
*desc
;
82 struct gpio_descs
*descs
;
83 unsigned long *values
;
88 static struct gpio_virtuser_irq_work_context
*
89 to_gpio_virtuser_irq_work_context(struct irq_work
*work
)
91 return container_of(work
, struct gpio_virtuser_irq_work_context
, work
);
95 gpio_virtuser_init_irq_work_context(struct gpio_virtuser_irq_work_context
*ctx
)
97 memset(ctx
, 0, sizeof(*ctx
));
98 init_completion(&ctx
->work_completion
);
102 gpio_virtuser_irq_work_queue_sync(struct gpio_virtuser_irq_work_context
*ctx
)
104 irq_work_queue(&ctx
->work
);
105 wait_for_completion(&ctx
->work_completion
);
108 static void gpio_virtuser_dbgfs_emit_value_array(char *buf
,
109 unsigned long *values
,
114 for (i
= 0; i
< num_values
; i
++)
115 buf
[i
] = test_bit(i
, values
) ? '1' : '0';
120 static void gpio_virtuser_get_value_array_atomic(struct irq_work
*work
)
122 struct gpio_virtuser_irq_work_context
*ctx
=
123 to_gpio_virtuser_irq_work_context(work
);
124 struct gpio_descs
*descs
= ctx
->descs
;
126 ctx
->ret
= gpiod_get_array_value(descs
->ndescs
, descs
->desc
,
127 descs
->info
, ctx
->values
);
128 complete(&ctx
->work_completion
);
131 static int gpio_virtuser_get_array_value(struct gpio_descs
*descs
,
132 unsigned long *values
, bool atomic
)
134 struct gpio_virtuser_irq_work_context ctx
;
137 return gpiod_get_array_value_cansleep(descs
->ndescs
,
139 descs
->info
, values
);
141 gpio_virtuser_init_irq_work_context(&ctx
);
142 ctx
.work
= IRQ_WORK_INIT_HARD(gpio_virtuser_get_value_array_atomic
);
146 gpio_virtuser_irq_work_queue_sync(&ctx
);
151 static ssize_t
gpio_virtuser_value_array_do_read(struct file
*file
,
152 char __user
*user_buf
,
153 size_t size
, loff_t
*ppos
,
156 struct gpio_virtuser_line_data
*data
= file
->private_data
;
157 struct gpio_descs
*descs
= data
->ad
.descs
;
161 unsigned long *values
__free(bitmap
) = bitmap_zalloc(descs
->ndescs
,
166 ret
= gpio_virtuser_get_array_value(descs
, values
, atomic
);
170 bufsize
= descs
->ndescs
+ 2;
172 char *buf
__free(kfree
) = kzalloc(bufsize
, GFP_KERNEL
);
176 gpio_virtuser_dbgfs_emit_value_array(buf
, values
, descs
->ndescs
);
178 return simple_read_from_buffer(user_buf
, size
, ppos
, buf
,
182 static int gpio_virtuser_dbgfs_parse_value_array(const char *buf
,
184 unsigned long *values
)
188 for (i
= 0; i
< len
; i
++) {
190 clear_bit(i
, values
);
191 else if (buf
[i
] == '1')
200 static void gpio_virtuser_set_value_array_atomic(struct irq_work
*work
)
202 struct gpio_virtuser_irq_work_context
*ctx
=
203 to_gpio_virtuser_irq_work_context(work
);
204 struct gpio_descs
*descs
= ctx
->descs
;
206 ctx
->ret
= gpiod_set_array_value(descs
->ndescs
, descs
->desc
,
207 descs
->info
, ctx
->values
);
208 complete(&ctx
->work_completion
);
211 static int gpio_virtuser_set_array_value(struct gpio_descs
*descs
,
212 unsigned long *values
, bool atomic
)
214 struct gpio_virtuser_irq_work_context ctx
;
217 return gpiod_set_array_value_cansleep(descs
->ndescs
,
219 descs
->info
, values
);
221 gpio_virtuser_init_irq_work_context(&ctx
);
222 ctx
.work
= IRQ_WORK_INIT_HARD(gpio_virtuser_set_value_array_atomic
);
226 gpio_virtuser_irq_work_queue_sync(&ctx
);
231 static ssize_t
gpio_virtuser_value_array_do_write(struct file
*file
,
232 const char __user
*user_buf
,
233 size_t count
, loff_t
*ppos
,
236 struct gpio_virtuser_line_data
*data
= file
->private_data
;
237 struct gpio_descs
*descs
= data
->ad
.descs
;
240 if (count
- 1 != descs
->ndescs
)
243 char *buf
__free(kfree
) = kzalloc(count
, GFP_KERNEL
);
247 ret
= simple_write_to_buffer(buf
, count
, ppos
, user_buf
, count
);
251 unsigned long *values
__free(bitmap
) = bitmap_zalloc(descs
->ndescs
,
256 ret
= gpio_virtuser_dbgfs_parse_value_array(buf
, count
- 1, values
);
260 ret
= gpio_virtuser_set_array_value(descs
, values
, atomic
);
267 static ssize_t
gpio_virtuser_value_array_read(struct file
*file
,
268 char __user
*user_buf
,
269 size_t count
, loff_t
*ppos
)
271 return gpio_virtuser_value_array_do_read(file
, user_buf
, count
, ppos
,
275 static ssize_t
gpio_virtuser_value_array_write(struct file
*file
,
276 const char __user
*user_buf
,
277 size_t count
, loff_t
*ppos
)
279 return gpio_virtuser_value_array_do_write(file
, user_buf
, count
, ppos
,
283 static const struct file_operations gpio_virtuser_value_array_fops
= {
284 .read
= gpio_virtuser_value_array_read
,
285 .write
= gpio_virtuser_value_array_write
,
287 .owner
= THIS_MODULE
,
288 .llseek
= default_llseek
,
292 gpio_virtuser_value_array_atomic_read(struct file
*file
, char __user
*user_buf
,
293 size_t count
, loff_t
*ppos
)
295 return gpio_virtuser_value_array_do_read(file
, user_buf
, count
, ppos
,
300 gpio_virtuser_value_array_atomic_write(struct file
*file
,
301 const char __user
*user_buf
,
302 size_t count
, loff_t
*ppos
)
304 return gpio_virtuser_value_array_do_write(file
, user_buf
, count
, ppos
,
308 static const struct file_operations gpio_virtuser_value_array_atomic_fops
= {
309 .read
= gpio_virtuser_value_array_atomic_read
,
310 .write
= gpio_virtuser_value_array_atomic_write
,
312 .owner
= THIS_MODULE
,
313 .llseek
= default_llseek
,
316 static void gpio_virtuser_do_get_direction_atomic(struct irq_work
*work
)
318 struct gpio_virtuser_irq_work_context
*ctx
=
319 to_gpio_virtuser_irq_work_context(work
);
321 ctx
->ret
= gpiod_get_direction(ctx
->desc
);
322 complete(&ctx
->work_completion
);
325 static int gpio_virtuser_get_direction_atomic(struct gpio_desc
*desc
)
327 struct gpio_virtuser_irq_work_context ctx
;
329 gpio_virtuser_init_irq_work_context(&ctx
);
330 ctx
.work
= IRQ_WORK_INIT_HARD(gpio_virtuser_do_get_direction_atomic
);
333 gpio_virtuser_irq_work_queue_sync(&ctx
);
338 static ssize_t
gpio_virtuser_direction_do_read(struct file
*file
,
339 char __user
*user_buf
,
340 size_t size
, loff_t
*ppos
,
343 struct gpio_virtuser_line_data
*data
= file
->private_data
;
344 struct gpio_desc
*desc
= data
->ad
.desc
;
349 dir
= gpiod_get_direction(desc
);
351 dir
= gpio_virtuser_get_direction_atomic(desc
);
355 snprintf(buf
, sizeof(buf
), "%s\n", dir
? "input" : "output");
357 return simple_read_from_buffer(user_buf
, size
, ppos
, buf
, strlen(buf
));
360 static int gpio_virtuser_set_direction(struct gpio_desc
*desc
, int dir
, int val
)
363 return gpiod_direction_input(desc
);
365 return gpiod_direction_output(desc
, val
);
368 static void gpio_virtuser_do_set_direction_atomic(struct irq_work
*work
)
370 struct gpio_virtuser_irq_work_context
*ctx
=
371 to_gpio_virtuser_irq_work_context(work
);
373 ctx
->ret
= gpio_virtuser_set_direction(ctx
->desc
, ctx
->dir
, ctx
->val
);
374 complete(&ctx
->work_completion
);
377 static int gpio_virtuser_set_direction_atomic(struct gpio_desc
*desc
,
380 struct gpio_virtuser_irq_work_context ctx
;
382 gpio_virtuser_init_irq_work_context(&ctx
);
383 ctx
.work
= IRQ_WORK_INIT_HARD(gpio_virtuser_do_set_direction_atomic
);
388 gpio_virtuser_irq_work_queue_sync(&ctx
);
393 static ssize_t
gpio_virtuser_direction_do_write(struct file
*file
,
394 const char __user
*user_buf
,
395 size_t count
, loff_t
*ppos
,
398 struct gpio_virtuser_line_data
*data
= file
->private_data
;
399 struct gpio_desc
*desc
= data
->ad
.desc
;
400 char buf
[32], *trimmed
;
401 int ret
, dir
, val
= 0;
403 ret
= simple_write_to_buffer(buf
, sizeof(buf
), ppos
, user_buf
, count
);
407 trimmed
= strim(buf
);
409 if (strcmp(trimmed
, "input") == 0) {
411 } else if (strcmp(trimmed
, "output-high") == 0) {
414 } else if (strcmp(trimmed
, "output-low") == 0) {
421 ret
= gpio_virtuser_set_direction(desc
, dir
, val
);
423 ret
= gpio_virtuser_set_direction_atomic(desc
, dir
, val
);
430 static ssize_t
gpio_virtuser_direction_read(struct file
*file
,
431 char __user
*user_buf
,
432 size_t size
, loff_t
*ppos
)
434 return gpio_virtuser_direction_do_read(file
, user_buf
, size
, ppos
,
438 static ssize_t
gpio_virtuser_direction_write(struct file
*file
,
439 const char __user
*user_buf
,
440 size_t count
, loff_t
*ppos
)
442 return gpio_virtuser_direction_do_write(file
, user_buf
, count
, ppos
,
446 static const struct file_operations gpio_virtuser_direction_fops
= {
447 .read
= gpio_virtuser_direction_read
,
448 .write
= gpio_virtuser_direction_write
,
450 .owner
= THIS_MODULE
,
451 .llseek
= default_llseek
,
454 static ssize_t
gpio_virtuser_direction_atomic_read(struct file
*file
,
455 char __user
*user_buf
,
456 size_t size
, loff_t
*ppos
)
458 return gpio_virtuser_direction_do_read(file
, user_buf
, size
, ppos
,
462 static ssize_t
gpio_virtuser_direction_atomic_write(struct file
*file
,
463 const char __user
*user_buf
,
464 size_t count
, loff_t
*ppos
)
466 return gpio_virtuser_direction_do_write(file
, user_buf
, count
, ppos
,
470 static const struct file_operations gpio_virtuser_direction_atomic_fops
= {
471 .read
= gpio_virtuser_direction_atomic_read
,
472 .write
= gpio_virtuser_direction_atomic_write
,
474 .owner
= THIS_MODULE
,
475 .llseek
= default_llseek
,
478 static int gpio_virtuser_value_get(void *data
, u64
*val
)
480 struct gpio_virtuser_line_data
*ld
= data
;
483 ret
= gpiod_get_value_cansleep(ld
->ad
.desc
);
492 static int gpio_virtuser_value_set(void *data
, u64 val
)
494 struct gpio_virtuser_line_data
*ld
= data
;
499 gpiod_set_value_cansleep(ld
->ad
.desc
, (int)val
);
504 DEFINE_DEBUGFS_ATTRIBUTE(gpio_virtuser_value_fops
,
505 gpio_virtuser_value_get
,
506 gpio_virtuser_value_set
,
509 static void gpio_virtuser_get_value_atomic(struct irq_work
*work
)
511 struct gpio_virtuser_irq_work_context
*ctx
=
512 to_gpio_virtuser_irq_work_context(work
);
514 ctx
->val
= gpiod_get_value(ctx
->desc
);
515 complete(&ctx
->work_completion
);
518 static int gpio_virtuser_value_atomic_get(void *data
, u64
*val
)
520 struct gpio_virtuser_line_data
*ld
= data
;
521 struct gpio_virtuser_irq_work_context ctx
;
523 gpio_virtuser_init_irq_work_context(&ctx
);
524 ctx
.work
= IRQ_WORK_INIT_HARD(gpio_virtuser_get_value_atomic
);
525 ctx
.desc
= ld
->ad
.desc
;
527 gpio_virtuser_irq_work_queue_sync(&ctx
);
537 static void gpio_virtuser_set_value_atomic(struct irq_work
*work
)
539 struct gpio_virtuser_irq_work_context
*ctx
=
540 to_gpio_virtuser_irq_work_context(work
);
542 gpiod_set_value(ctx
->desc
, ctx
->val
);
543 complete(&ctx
->work_completion
);
546 static int gpio_virtuser_value_atomic_set(void *data
, u64 val
)
548 struct gpio_virtuser_line_data
*ld
= data
;
549 struct gpio_virtuser_irq_work_context ctx
;
554 gpio_virtuser_init_irq_work_context(&ctx
);
555 ctx
.work
= IRQ_WORK_INIT_HARD(gpio_virtuser_set_value_atomic
);
556 ctx
.desc
= ld
->ad
.desc
;
559 gpio_virtuser_irq_work_queue_sync(&ctx
);
564 DEFINE_DEBUGFS_ATTRIBUTE(gpio_virtuser_value_atomic_fops
,
565 gpio_virtuser_value_atomic_get
,
566 gpio_virtuser_value_atomic_set
,
569 static int gpio_virtuser_debounce_get(void *data
, u64
*val
)
571 struct gpio_virtuser_line_data
*ld
= data
;
573 *val
= READ_ONCE(ld
->debounce
);
578 static int gpio_virtuser_debounce_set(void *data
, u64 val
)
580 struct gpio_virtuser_line_data
*ld
= data
;
586 ret
= gpiod_set_debounce(ld
->ad
.desc
, (unsigned int)val
);
588 /* Don't propagate errno unknown to user-space. */
589 return ret
== -ENOTSUPP
? -EOPNOTSUPP
: ret
;
591 WRITE_ONCE(ld
->debounce
, (unsigned int)val
);
596 DEFINE_DEBUGFS_ATTRIBUTE(gpio_virtuser_debounce_fops
,
597 gpio_virtuser_debounce_get
,
598 gpio_virtuser_debounce_set
,
601 static ssize_t
gpio_virtuser_consumer_read(struct file
*file
,
602 char __user
*user_buf
,
603 size_t size
, loff_t
*ppos
)
605 struct gpio_virtuser_line_data
*data
= file
->private_data
;
606 char buf
[GPIO_VIRTUSER_NAME_BUF_LEN
+ 1];
609 memset(buf
, 0x0, sizeof(buf
));
611 scoped_guard(mutex
, &data
->consumer_lock
)
612 ret
= snprintf(buf
, sizeof(buf
), "%s\n", data
->consumer
);
614 return simple_read_from_buffer(user_buf
, size
, ppos
, buf
, ret
);
617 static ssize_t
gpio_virtuser_consumer_write(struct file
*file
,
618 const char __user
*user_buf
,
619 size_t count
, loff_t
*ppos
)
621 struct gpio_virtuser_line_data
*data
= file
->private_data
;
622 char buf
[GPIO_VIRTUSER_NAME_BUF_LEN
+ 2];
625 ret
= simple_write_to_buffer(buf
, GPIO_VIRTUSER_NAME_BUF_LEN
, ppos
,
630 buf
[strlen(buf
) - 1] = '\0';
632 ret
= gpiod_set_consumer_name(data
->ad
.desc
, buf
);
636 scoped_guard(mutex
, &data
->consumer_lock
)
637 strscpy(data
->consumer
, buf
, sizeof(data
->consumer
));
642 static const struct file_operations gpio_virtuser_consumer_fops
= {
643 .read
= gpio_virtuser_consumer_read
,
644 .write
= gpio_virtuser_consumer_write
,
646 .owner
= THIS_MODULE
,
647 .llseek
= default_llseek
,
650 static int gpio_virtuser_interrupts_get(void *data
, u64
*val
)
652 struct gpio_virtuser_line_data
*ld
= data
;
654 *val
= atomic_read(&ld
->irq_count
);
659 static irqreturn_t
gpio_virtuser_irq_handler(int irq
, void *data
)
661 struct gpio_virtuser_line_data
*ld
= data
;
663 atomic_inc(&ld
->irq_count
);
668 static int gpio_virtuser_interrupts_set(void *data
, u64 val
)
670 struct gpio_virtuser_line_data
*ld
= data
;
677 irq
= gpiod_to_irq(ld
->ad
.desc
);
681 ret
= request_threaded_irq(irq
, NULL
,
682 gpio_virtuser_irq_handler
,
683 IRQF_TRIGGER_RISING
|
684 IRQF_TRIGGER_FALLING
|
690 atomic_set(&ld
->irq
, irq
);
692 irq
= atomic_xchg(&ld
->irq
, 0);
699 DEFINE_DEBUGFS_ATTRIBUTE(gpio_virtuser_interrupts_fops
,
700 gpio_virtuser_interrupts_get
,
701 gpio_virtuser_interrupts_set
,
704 static const struct gpio_virtuser_dbgfs_attr_descr
705 gpio_virtuser_line_array_dbgfs_attrs
[] = {
708 .fops
= &gpio_virtuser_value_array_fops
,
711 .name
= "values_atomic",
712 .fops
= &gpio_virtuser_value_array_atomic_fops
,
716 static const struct gpio_virtuser_dbgfs_attr_descr
717 gpio_virtuser_line_dbgfs_attrs
[] = {
720 .fops
= &gpio_virtuser_direction_fops
,
723 .name
= "direction_atomic",
724 .fops
= &gpio_virtuser_direction_atomic_fops
,
728 .fops
= &gpio_virtuser_value_fops
,
731 .name
= "value_atomic",
732 .fops
= &gpio_virtuser_value_atomic_fops
,
736 .fops
= &gpio_virtuser_debounce_fops
,
740 .fops
= &gpio_virtuser_consumer_fops
,
743 .name
= "interrupts",
744 .fops
= &gpio_virtuser_interrupts_fops
,
748 static int gpio_virtuser_create_debugfs_attrs(
749 const struct gpio_virtuser_dbgfs_attr_descr
*attr
,
750 size_t num_attrs
, struct dentry
*parent
, void *data
)
755 for (i
= 0; i
< num_attrs
; i
++, attr
++) {
756 ret
= debugfs_create_file(attr
->name
, 0644, parent
, data
,
765 static int gpio_virtuser_dbgfs_init_line_array_attrs(struct device
*dev
,
766 struct gpio_descs
*descs
,
768 struct dentry
*dbgfs_entry
)
770 struct gpio_virtuser_line_array_data
*data
;
773 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
777 data
->ad
.descs
= descs
;
779 name
= devm_kasprintf(dev
, GFP_KERNEL
, "gpiod:%s", id
);
783 data
->ad
.dbgfs_dir
= debugfs_create_dir(name
, dbgfs_entry
);
784 if (IS_ERR(data
->ad
.dbgfs_dir
))
785 return PTR_ERR(data
->ad
.dbgfs_dir
);
787 return gpio_virtuser_create_debugfs_attrs(
788 gpio_virtuser_line_array_dbgfs_attrs
,
789 ARRAY_SIZE(gpio_virtuser_line_array_dbgfs_attrs
),
790 data
->ad
.dbgfs_dir
, data
);
793 static int gpio_virtuser_dbgfs_init_line_attrs(struct device
*dev
,
794 struct gpio_desc
*desc
,
797 struct dentry
*dbgfs_entry
)
799 struct gpio_virtuser_line_data
*data
;
803 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
807 data
->ad
.desc
= desc
;
808 strscpy(data
->consumer
, id
);
809 atomic_set(&data
->irq
, 0);
810 atomic_set(&data
->irq_count
, 0);
812 name
= devm_kasprintf(dev
, GFP_KERNEL
, "gpiod:%s:%u", id
, index
);
816 ret
= devm_mutex_init(dev
, &data
->consumer_lock
);
820 data
->ad
.dbgfs_dir
= debugfs_create_dir(name
, dbgfs_entry
);
821 if (IS_ERR(data
->ad
.dbgfs_dir
))
822 return PTR_ERR(data
->ad
.dbgfs_dir
);
824 return gpio_virtuser_create_debugfs_attrs(
825 gpio_virtuser_line_dbgfs_attrs
,
826 ARRAY_SIZE(gpio_virtuser_line_dbgfs_attrs
),
827 data
->ad
.dbgfs_dir
, data
);
830 static void gpio_virtuser_debugfs_remove(void *data
)
832 struct dentry
*dbgfs_entry
= data
;
834 debugfs_remove_recursive(dbgfs_entry
);
837 static int gpio_virtuser_prop_is_gpio(struct property
*prop
)
839 char *dash
= strrchr(prop
->name
, '-');
841 return dash
&& strcmp(dash
, "-gpios") == 0;
845 * If this is an OF-based system, then we iterate over properties and consider
846 * all whose names end in "-gpios". For configfs we expect an additional string
847 * array property - "gpio-virtuser,ids" - containing the list of all GPIO IDs
850 static int gpio_virtuser_count_ids(struct device
*dev
)
852 struct device_node
*of_node
= dev_of_node(dev
);
853 struct property
*prop
;
857 return device_property_string_array_count(dev
,
858 "gpio-virtuser,ids");
860 for_each_property_of_node(of_node
, prop
) {
861 if (gpio_virtuser_prop_is_gpio(prop
))
868 static int gpio_virtuser_get_ids(struct device
*dev
, const char **ids
,
871 struct device_node
*of_node
= dev_of_node(dev
);
872 struct property
*prop
;
873 size_t pos
= 0, diff
;
877 return device_property_read_string_array(dev
,
881 for_each_property_of_node(of_node
, prop
) {
882 if (!gpio_virtuser_prop_is_gpio(prop
))
885 dash
= strrchr(prop
->name
, '-');
886 diff
= dash
- prop
->name
;
888 tmp
= devm_kmemdup(dev
, prop
->name
, diff
+ 1,
900 static int gpio_virtuser_probe(struct platform_device
*pdev
)
902 struct device
*dev
= &pdev
->dev
;
903 struct dentry
*dbgfs_entry
;
904 struct gpio_descs
*descs
;
905 int ret
, num_ids
= 0, i
;
909 num_ids
= gpio_virtuser_count_ids(dev
);
911 return dev_err_probe(dev
, num_ids
,
912 "Failed to get the number of GPIOs to request\n");
915 return dev_err_probe(dev
, -EINVAL
, "No GPIO IDs specified\n");
917 ids
= devm_kcalloc(dev
, num_ids
, sizeof(*ids
), GFP_KERNEL
);
921 ret
= gpio_virtuser_get_ids(dev
, ids
, num_ids
);
923 return dev_err_probe(dev
, ret
,
924 "Failed to get the IDs of GPIOs to request\n");
926 dbgfs_entry
= debugfs_create_dir(dev_name(dev
), gpio_virtuser_dbg_root
);
927 ret
= devm_add_action_or_reset(dev
, gpio_virtuser_debugfs_remove
,
932 for (i
= 0; i
< num_ids
; i
++) {
933 descs
= devm_gpiod_get_array(dev
, ids
[i
], GPIOD_ASIS
);
935 return dev_err_probe(dev
, PTR_ERR(descs
),
936 "Failed to request the '%s' GPIOs\n",
939 ret
= gpio_virtuser_dbgfs_init_line_array_attrs(dev
, descs
,
943 return dev_err_probe(dev
, ret
,
944 "Failed to setup the debugfs array interface for the '%s' GPIOs\n",
947 for (j
= 0; j
< descs
->ndescs
; j
++) {
948 ret
= gpio_virtuser_dbgfs_init_line_attrs(dev
,
949 descs
->desc
[j
], ids
[i
],
952 return dev_err_probe(dev
, ret
,
953 "Failed to setup the debugfs line interface for the '%s' GPIOs\n",
961 static const struct of_device_id gpio_virtuser_of_match
[] = {
962 { .compatible
= "gpio-virtuser" },
965 MODULE_DEVICE_TABLE(of
, gpio_virtuser_of_match
);
967 static struct platform_driver gpio_virtuser_driver
= {
969 .name
= "gpio-virtuser",
970 .of_match_table
= gpio_virtuser_of_match
,
972 .probe
= gpio_virtuser_probe
,
975 struct gpio_virtuser_device
{
976 struct config_group group
;
978 struct platform_device
*pdev
;
982 struct notifier_block bus_notifier
;
983 struct completion probe_completion
;
986 struct gpiod_lookup_table
*lookup_table
;
988 struct list_head lookup_list
;
991 static int gpio_virtuser_bus_notifier_call(struct notifier_block
*nb
,
992 unsigned long action
, void *data
)
994 struct gpio_virtuser_device
*vdev
;
995 struct device
*dev
= data
;
998 vdev
= container_of(nb
, struct gpio_virtuser_device
, bus_notifier
);
999 snprintf(devname
, sizeof(devname
), "gpio-virtuser.%d", vdev
->id
);
1001 if (!device_match_name(dev
, devname
))
1005 case BUS_NOTIFY_BOUND_DRIVER
:
1006 vdev
->driver_bound
= true;
1008 case BUS_NOTIFY_DRIVER_NOT_BOUND
:
1009 vdev
->driver_bound
= false;
1015 complete(&vdev
->probe_completion
);
1019 static struct gpio_virtuser_device
*
1020 to_gpio_virtuser_device(struct config_item
*item
)
1022 struct config_group
*group
= to_config_group(item
);
1024 return container_of(group
, struct gpio_virtuser_device
, group
);
1028 gpio_virtuser_device_is_live(struct gpio_virtuser_device
*dev
)
1030 lockdep_assert_held(&dev
->lock
);
1035 struct gpio_virtuser_lookup
{
1036 struct config_group group
;
1038 struct gpio_virtuser_device
*parent
;
1039 struct list_head siblings
;
1043 struct list_head entry_list
;
1046 static struct gpio_virtuser_lookup
*
1047 to_gpio_virtuser_lookup(struct config_item
*item
)
1049 struct config_group
*group
= to_config_group(item
);
1051 return container_of(group
, struct gpio_virtuser_lookup
, group
);
1054 struct gpio_virtuser_lookup_entry
{
1055 struct config_group group
;
1057 struct gpio_virtuser_lookup
*parent
;
1058 struct list_head siblings
;
1061 /* Can be negative to indicate lookup by name. */
1063 enum gpio_lookup_flags flags
;
1066 static struct gpio_virtuser_lookup_entry
*
1067 to_gpio_virtuser_lookup_entry(struct config_item
*item
)
1069 struct config_group
*group
= to_config_group(item
);
1071 return container_of(group
, struct gpio_virtuser_lookup_entry
, group
);
1075 gpio_virtuser_lookup_entry_config_key_show(struct config_item
*item
, char *page
)
1077 struct gpio_virtuser_lookup_entry
*entry
=
1078 to_gpio_virtuser_lookup_entry(item
);
1079 struct gpio_virtuser_device
*dev
= entry
->parent
->parent
;
1081 guard(mutex
)(&dev
->lock
);
1083 return sprintf(page
, "%s\n", entry
->key
?: "");
1087 gpio_virtuser_lookup_entry_config_key_store(struct config_item
*item
,
1088 const char *page
, size_t count
)
1090 struct gpio_virtuser_lookup_entry
*entry
=
1091 to_gpio_virtuser_lookup_entry(item
);
1092 struct gpio_virtuser_device
*dev
= entry
->parent
->parent
;
1094 char *key
__free(kfree
) = kstrndup(skip_spaces(page
), count
,
1101 guard(mutex
)(&dev
->lock
);
1103 if (gpio_virtuser_device_is_live(dev
))
1107 entry
->key
= no_free_ptr(key
);
1112 CONFIGFS_ATTR(gpio_virtuser_lookup_entry_config_
, key
);
1115 gpio_virtuser_lookup_entry_config_offset_show(struct config_item
*item
,
1118 struct gpio_virtuser_lookup_entry
*entry
=
1119 to_gpio_virtuser_lookup_entry(item
);
1120 struct gpio_virtuser_device
*dev
= entry
->parent
->parent
;
1121 unsigned int offset
;
1123 scoped_guard(mutex
, &dev
->lock
)
1124 offset
= entry
->offset
;
1126 return sprintf(page
, "%d\n", offset
);
1130 gpio_virtuser_lookup_entry_config_offset_store(struct config_item
*item
,
1131 const char *page
, size_t count
)
1133 struct gpio_virtuser_lookup_entry
*entry
=
1134 to_gpio_virtuser_lookup_entry(item
);
1135 struct gpio_virtuser_device
*dev
= entry
->parent
->parent
;
1138 ret
= kstrtoint(page
, 0, &offset
);
1143 * Negative number here means: 'key' represents a line name to lookup.
1144 * Non-negative means: 'key' represents the label of the chip with
1145 * the 'offset' value representing the line within that chip.
1147 * GPIOLIB uses the U16_MAX value to indicate lookup by line name so
1148 * the greatest offset we can accept is (U16_MAX - 1).
1150 if (offset
> (U16_MAX
- 1))
1153 guard(mutex
)(&dev
->lock
);
1155 if (gpio_virtuser_device_is_live(dev
))
1158 entry
->offset
= offset
;
1163 CONFIGFS_ATTR(gpio_virtuser_lookup_entry_config_
, offset
);
1165 static enum gpio_lookup_flags
1166 gpio_virtuser_lookup_get_flags(struct config_item
*item
)
1168 struct gpio_virtuser_lookup_entry
*entry
=
1169 to_gpio_virtuser_lookup_entry(item
);
1170 struct gpio_virtuser_device
*dev
= entry
->parent
->parent
;
1172 guard(mutex
)(&dev
->lock
);
1174 return entry
->flags
;
1178 gpio_virtuser_lookup_entry_config_drive_show(struct config_item
*item
, char *page
)
1180 enum gpio_lookup_flags flags
= gpio_virtuser_lookup_get_flags(item
);
1183 if (flags
& GPIO_OPEN_DRAIN
)
1184 repr
= "open-drain";
1185 else if (flags
& GPIO_OPEN_SOURCE
)
1186 repr
= "open-source";
1190 return sprintf(page
, "%s\n", repr
);
1194 gpio_virtuser_lookup_entry_config_drive_store(struct config_item
*item
,
1195 const char *page
, size_t count
)
1197 struct gpio_virtuser_lookup_entry
*entry
=
1198 to_gpio_virtuser_lookup_entry(item
);
1199 struct gpio_virtuser_device
*dev
= entry
->parent
->parent
;
1201 guard(mutex
)(&dev
->lock
);
1203 if (gpio_virtuser_device_is_live(dev
))
1206 if (sysfs_streq(page
, "push-pull")) {
1207 entry
->flags
&= ~(GPIO_OPEN_DRAIN
| GPIO_OPEN_SOURCE
);
1208 } else if (sysfs_streq(page
, "open-drain")) {
1209 entry
->flags
&= ~GPIO_OPEN_SOURCE
;
1210 entry
->flags
|= GPIO_OPEN_DRAIN
;
1211 } else if (sysfs_streq(page
, "open-source")) {
1212 entry
->flags
&= ~GPIO_OPEN_DRAIN
;
1213 entry
->flags
|= GPIO_OPEN_SOURCE
;
1221 CONFIGFS_ATTR(gpio_virtuser_lookup_entry_config_
, drive
);
1224 gpio_virtuser_lookup_entry_config_pull_show(struct config_item
*item
, char *page
)
1226 enum gpio_lookup_flags flags
= gpio_virtuser_lookup_get_flags(item
);
1229 if (flags
& GPIO_PULL_UP
)
1231 else if (flags
& GPIO_PULL_DOWN
)
1233 else if (flags
& GPIO_PULL_DISABLE
)
1234 repr
= "pull-disabled";
1238 return sprintf(page
, "%s\n", repr
);
1242 gpio_virtuser_lookup_entry_config_pull_store(struct config_item
*item
,
1243 const char *page
, size_t count
)
1245 struct gpio_virtuser_lookup_entry
*entry
=
1246 to_gpio_virtuser_lookup_entry(item
);
1247 struct gpio_virtuser_device
*dev
= entry
->parent
->parent
;
1249 guard(mutex
)(&dev
->lock
);
1251 if (gpio_virtuser_device_is_live(dev
))
1254 if (sysfs_streq(page
, "pull-up")) {
1255 entry
->flags
&= ~(GPIO_PULL_DOWN
| GPIO_PULL_DISABLE
);
1256 entry
->flags
|= GPIO_PULL_UP
;
1257 } else if (sysfs_streq(page
, "pull-down")) {
1258 entry
->flags
&= ~(GPIO_PULL_UP
| GPIO_PULL_DISABLE
);
1259 entry
->flags
|= GPIO_PULL_DOWN
;
1260 } else if (sysfs_streq(page
, "pull-disabled")) {
1261 entry
->flags
&= ~(GPIO_PULL_UP
| GPIO_PULL_DOWN
);
1262 entry
->flags
|= GPIO_PULL_DISABLE
;
1263 } else if (sysfs_streq(page
, "as-is")) {
1264 entry
->flags
&= ~(GPIO_PULL_UP
| GPIO_PULL_DOWN
|
1273 CONFIGFS_ATTR(gpio_virtuser_lookup_entry_config_
, pull
);
1276 gpio_virtuser_lookup_entry_config_active_low_show(struct config_item
*item
,
1279 enum gpio_lookup_flags flags
= gpio_virtuser_lookup_get_flags(item
);
1281 return sprintf(page
, "%c\n", flags
& GPIO_ACTIVE_LOW
? '1' : '0');
1285 gpio_virtuser_lookup_entry_config_active_low_store(struct config_item
*item
,
1289 struct gpio_virtuser_lookup_entry
*entry
=
1290 to_gpio_virtuser_lookup_entry(item
);
1291 struct gpio_virtuser_device
*dev
= entry
->parent
->parent
;
1295 ret
= kstrtobool(page
, &active_low
);
1299 guard(mutex
)(&dev
->lock
);
1301 if (gpio_virtuser_device_is_live(dev
))
1305 entry
->flags
|= GPIO_ACTIVE_LOW
;
1307 entry
->flags
&= ~GPIO_ACTIVE_LOW
;
1312 CONFIGFS_ATTR(gpio_virtuser_lookup_entry_config_
, active_low
);
1315 gpio_virtuser_lookup_entry_config_transitory_show(struct config_item
*item
,
1318 enum gpio_lookup_flags flags
= gpio_virtuser_lookup_get_flags(item
);
1320 return sprintf(page
, "%c\n", flags
& GPIO_TRANSITORY
? '1' : '0');
1324 gpio_virtuser_lookup_entry_config_transitory_store(struct config_item
*item
,
1328 struct gpio_virtuser_lookup_entry
*entry
=
1329 to_gpio_virtuser_lookup_entry(item
);
1330 struct gpio_virtuser_device
*dev
= entry
->parent
->parent
;
1334 ret
= kstrtobool(page
, &transitory
);
1338 guard(mutex
)(&dev
->lock
);
1340 if (gpio_virtuser_device_is_live(dev
))
1344 entry
->flags
|= GPIO_TRANSITORY
;
1346 entry
->flags
&= ~GPIO_TRANSITORY
;
1351 CONFIGFS_ATTR(gpio_virtuser_lookup_entry_config_
, transitory
);
1353 static struct configfs_attribute
*gpio_virtuser_lookup_entry_config_attrs
[] = {
1354 &gpio_virtuser_lookup_entry_config_attr_key
,
1355 &gpio_virtuser_lookup_entry_config_attr_offset
,
1356 &gpio_virtuser_lookup_entry_config_attr_drive
,
1357 &gpio_virtuser_lookup_entry_config_attr_pull
,
1358 &gpio_virtuser_lookup_entry_config_attr_active_low
,
1359 &gpio_virtuser_lookup_entry_config_attr_transitory
,
1364 gpio_virtuser_device_config_dev_name_show(struct config_item
*item
,
1367 struct gpio_virtuser_device
*dev
= to_gpio_virtuser_device(item
);
1368 struct platform_device
*pdev
;
1370 guard(mutex
)(&dev
->lock
);
1374 return sprintf(page
, "%s\n", dev_name(&pdev
->dev
));
1376 return sprintf(page
, "gpio-sim.%d\n", dev
->id
);
1379 CONFIGFS_ATTR_RO(gpio_virtuser_device_config_
, dev_name
);
1381 static ssize_t
gpio_virtuser_device_config_live_show(struct config_item
*item
,
1384 struct gpio_virtuser_device
*dev
= to_gpio_virtuser_device(item
);
1387 scoped_guard(mutex
, &dev
->lock
)
1388 live
= gpio_virtuser_device_is_live(dev
);
1390 return sprintf(page
, "%c\n", live
? '1' : '0');
1394 gpio_virtuser_get_lookup_count(struct gpio_virtuser_device
*dev
)
1396 struct gpio_virtuser_lookup
*lookup
;
1399 lockdep_assert_held(&dev
->lock
);
1401 list_for_each_entry(lookup
, &dev
->lookup_list
, siblings
)
1402 count
+= list_count_nodes(&lookup
->entry_list
);
1408 gpio_virtuser_make_lookup_table(struct gpio_virtuser_device
*dev
)
1410 size_t num_entries
= gpio_virtuser_get_lookup_count(dev
);
1411 struct gpio_virtuser_lookup_entry
*entry
;
1412 struct gpio_virtuser_lookup
*lookup
;
1415 lockdep_assert_held(&dev
->lock
);
1417 struct gpiod_lookup_table
*table
__free(kfree
) =
1418 kzalloc(struct_size(table
, table
, num_entries
+ 1), GFP_KERNEL
);
1422 table
->dev_id
= kasprintf(GFP_KERNEL
, "gpio-virtuser.%d", dev
->id
);
1426 list_for_each_entry(lookup
, &dev
->lookup_list
, siblings
) {
1427 list_for_each_entry(entry
, &lookup
->entry_list
, siblings
) {
1429 GPIO_LOOKUP_IDX(entry
->key
,
1430 entry
->offset
< 0 ? U16_MAX
: entry
->offset
,
1431 lookup
->con_id
, i
, entry
->flags
);
1436 gpiod_add_lookup_table(table
);
1437 dev
->lookup_table
= no_free_ptr(table
);
1442 static struct fwnode_handle
*
1443 gpio_virtuser_make_device_swnode(struct gpio_virtuser_device
*dev
)
1445 struct property_entry properties
[2];
1446 struct gpio_virtuser_lookup
*lookup
;
1450 memset(properties
, 0, sizeof(properties
));
1452 num_ids
= list_count_nodes(&dev
->lookup_list
);
1453 char **ids
__free(kfree
) = kcalloc(num_ids
+ 1, sizeof(*ids
),
1456 return ERR_PTR(-ENOMEM
);
1458 list_for_each_entry(lookup
, &dev
->lookup_list
, siblings
)
1459 ids
[i
++] = lookup
->con_id
;
1461 properties
[0] = PROPERTY_ENTRY_STRING_ARRAY_LEN("gpio-virtuser,ids",
1464 return fwnode_create_software_node(properties
, NULL
);
1468 gpio_virtuser_device_activate(struct gpio_virtuser_device
*dev
)
1470 struct platform_device_info pdevinfo
;
1471 struct fwnode_handle
*swnode
;
1472 struct platform_device
*pdev
;
1475 lockdep_assert_held(&dev
->lock
);
1477 if (list_empty(&dev
->lookup_list
))
1480 swnode
= gpio_virtuser_make_device_swnode(dev
);
1482 return PTR_ERR(swnode
);
1484 memset(&pdevinfo
, 0, sizeof(pdevinfo
));
1485 pdevinfo
.name
= "gpio-virtuser";
1486 pdevinfo
.id
= dev
->id
;
1487 pdevinfo
.fwnode
= swnode
;
1489 ret
= gpio_virtuser_make_lookup_table(dev
);
1491 fwnode_remove_software_node(swnode
);
1495 reinit_completion(&dev
->probe_completion
);
1496 dev
->driver_bound
= false;
1497 bus_register_notifier(&platform_bus_type
, &dev
->bus_notifier
);
1499 pdev
= platform_device_register_full(&pdevinfo
);
1501 bus_unregister_notifier(&platform_bus_type
, &dev
->bus_notifier
);
1502 fwnode_remove_software_node(swnode
);
1503 return PTR_ERR(pdev
);
1506 wait_for_completion(&dev
->probe_completion
);
1507 bus_unregister_notifier(&platform_bus_type
, &dev
->bus_notifier
);
1509 if (!dev
->driver_bound
) {
1510 platform_device_unregister(pdev
);
1511 fwnode_remove_software_node(swnode
);
1521 gpio_virtuser_device_deactivate(struct gpio_virtuser_device
*dev
)
1523 struct fwnode_handle
*swnode
;
1525 lockdep_assert_held(&dev
->lock
);
1527 swnode
= dev_fwnode(&dev
->pdev
->dev
);
1528 platform_device_unregister(dev
->pdev
);
1529 fwnode_remove_software_node(swnode
);
1531 gpiod_remove_lookup_table(dev
->lookup_table
);
1532 kfree(dev
->lookup_table
);
1536 gpio_virtuser_device_config_live_store(struct config_item
*item
,
1537 const char *page
, size_t count
)
1539 struct gpio_virtuser_device
*dev
= to_gpio_virtuser_device(item
);
1543 ret
= kstrtobool(page
, &live
);
1547 guard(mutex
)(&dev
->lock
);
1549 if (live
== gpio_virtuser_device_is_live(dev
))
1553 ret
= gpio_virtuser_device_activate(dev
);
1555 gpio_virtuser_device_deactivate(dev
);
1557 return ret
?: count
;
1560 CONFIGFS_ATTR(gpio_virtuser_device_config_
, live
);
1562 static struct configfs_attribute
*gpio_virtuser_device_config_attrs
[] = {
1563 &gpio_virtuser_device_config_attr_dev_name
,
1564 &gpio_virtuser_device_config_attr_live
,
1569 gpio_virtuser_lookup_entry_config_group_release(struct config_item
*item
)
1571 struct gpio_virtuser_lookup_entry
*entry
=
1572 to_gpio_virtuser_lookup_entry(item
);
1573 struct gpio_virtuser_device
*dev
= entry
->parent
->parent
;
1575 guard(mutex
)(&dev
->lock
);
1577 list_del(&entry
->siblings
);
1584 configfs_item_operations gpio_virtuser_lookup_entry_config_item_ops
= {
1585 .release
= gpio_virtuser_lookup_entry_config_group_release
,
1589 config_item_type gpio_virtuser_lookup_entry_config_group_type
= {
1590 .ct_item_ops
= &gpio_virtuser_lookup_entry_config_item_ops
,
1591 .ct_attrs
= gpio_virtuser_lookup_entry_config_attrs
,
1592 .ct_owner
= THIS_MODULE
,
1595 static struct config_group
*
1596 gpio_virtuser_make_lookup_entry_group(struct config_group
*group
,
1599 struct gpio_virtuser_lookup
*lookup
=
1600 to_gpio_virtuser_lookup(&group
->cg_item
);
1601 struct gpio_virtuser_device
*dev
= lookup
->parent
;
1603 guard(mutex
)(&dev
->lock
);
1605 if (gpio_virtuser_device_is_live(dev
))
1606 return ERR_PTR(-EBUSY
);
1608 struct gpio_virtuser_lookup_entry
*entry
__free(kfree
) =
1609 kzalloc(sizeof(*entry
), GFP_KERNEL
);
1611 return ERR_PTR(-ENOMEM
);
1613 config_group_init_type_name(&entry
->group
, name
,
1614 &gpio_virtuser_lookup_entry_config_group_type
);
1615 entry
->flags
= GPIO_LOOKUP_FLAGS_DEFAULT
;
1616 entry
->parent
= lookup
;
1617 list_add_tail(&entry
->siblings
, &lookup
->entry_list
);
1619 return &no_free_ptr(entry
)->group
;
1622 static void gpio_virtuser_lookup_config_group_release(struct config_item
*item
)
1624 struct gpio_virtuser_lookup
*lookup
= to_gpio_virtuser_lookup(item
);
1625 struct gpio_virtuser_device
*dev
= lookup
->parent
;
1627 guard(mutex
)(&dev
->lock
);
1629 list_del(&lookup
->siblings
);
1631 kfree(lookup
->con_id
);
1635 static struct configfs_item_operations gpio_virtuser_lookup_config_item_ops
= {
1636 .release
= gpio_virtuser_lookup_config_group_release
,
1640 configfs_group_operations gpio_virtuser_lookup_config_group_ops
= {
1641 .make_group
= gpio_virtuser_make_lookup_entry_group
,
1644 static const struct config_item_type gpio_virtuser_lookup_config_group_type
= {
1645 .ct_group_ops
= &gpio_virtuser_lookup_config_group_ops
,
1646 .ct_item_ops
= &gpio_virtuser_lookup_config_item_ops
,
1647 .ct_owner
= THIS_MODULE
,
1650 static struct config_group
*
1651 gpio_virtuser_make_lookup_group(struct config_group
*group
, const char *name
)
1653 struct gpio_virtuser_device
*dev
=
1654 to_gpio_virtuser_device(&group
->cg_item
);
1656 if (strlen(name
) > (GPIO_VIRTUSER_NAME_BUF_LEN
- 1))
1657 return ERR_PTR(-E2BIG
);
1659 guard(mutex
)(&dev
->lock
);
1661 if (gpio_virtuser_device_is_live(dev
))
1662 return ERR_PTR(-EBUSY
);
1664 struct gpio_virtuser_lookup
*lookup
__free(kfree
) =
1665 kzalloc(sizeof(*lookup
), GFP_KERNEL
);
1667 return ERR_PTR(-ENOMEM
);
1669 lookup
->con_id
= kstrdup(name
, GFP_KERNEL
);
1670 if (!lookup
->con_id
)
1671 return ERR_PTR(-ENOMEM
);
1673 config_group_init_type_name(&lookup
->group
, name
,
1674 &gpio_virtuser_lookup_config_group_type
);
1675 INIT_LIST_HEAD(&lookup
->entry_list
);
1676 lookup
->parent
= dev
;
1677 list_add_tail(&lookup
->siblings
, &dev
->lookup_list
);
1679 return &no_free_ptr(lookup
)->group
;
1682 static void gpio_virtuser_device_config_group_release(struct config_item
*item
)
1684 struct gpio_virtuser_device
*dev
= to_gpio_virtuser_device(item
);
1686 guard(mutex
)(&dev
->lock
);
1688 if (gpio_virtuser_device_is_live(dev
))
1689 gpio_virtuser_device_deactivate(dev
);
1691 mutex_destroy(&dev
->lock
);
1692 ida_free(&gpio_virtuser_ida
, dev
->id
);
1696 static struct configfs_item_operations gpio_virtuser_device_config_item_ops
= {
1697 .release
= gpio_virtuser_device_config_group_release
,
1700 static struct configfs_group_operations gpio_virtuser_device_config_group_ops
= {
1701 .make_group
= gpio_virtuser_make_lookup_group
,
1704 static const struct config_item_type gpio_virtuser_device_config_group_type
= {
1705 .ct_group_ops
= &gpio_virtuser_device_config_group_ops
,
1706 .ct_item_ops
= &gpio_virtuser_device_config_item_ops
,
1707 .ct_attrs
= gpio_virtuser_device_config_attrs
,
1708 .ct_owner
= THIS_MODULE
,
1711 static struct config_group
*
1712 gpio_virtuser_config_make_device_group(struct config_group
*group
,
1715 struct gpio_virtuser_device
*dev
__free(kfree
) = kzalloc(sizeof(*dev
),
1718 return ERR_PTR(-ENOMEM
);
1720 dev
->id
= ida_alloc(&gpio_virtuser_ida
, GFP_KERNEL
);
1722 return ERR_PTR(dev
->id
);
1724 config_group_init_type_name(&dev
->group
, name
,
1725 &gpio_virtuser_device_config_group_type
);
1726 mutex_init(&dev
->lock
);
1727 INIT_LIST_HEAD(&dev
->lookup_list
);
1728 dev
->bus_notifier
.notifier_call
= gpio_virtuser_bus_notifier_call
;
1729 init_completion(&dev
->probe_completion
);
1731 return &no_free_ptr(dev
)->group
;
1734 static struct configfs_group_operations gpio_virtuser_config_group_ops
= {
1735 .make_group
= gpio_virtuser_config_make_device_group
,
1738 static const struct config_item_type gpio_virtuser_config_type
= {
1739 .ct_group_ops
= &gpio_virtuser_config_group_ops
,
1740 .ct_owner
= THIS_MODULE
,
1743 static struct configfs_subsystem gpio_virtuser_config_subsys
= {
1746 .ci_namebuf
= "gpio-virtuser",
1747 .ci_type
= &gpio_virtuser_config_type
,
1752 static int __init
gpio_virtuser_init(void)
1756 ret
= platform_driver_register(&gpio_virtuser_driver
);
1758 pr_err("Failed to register the platform driver: %d\n", ret
);
1762 config_group_init(&gpio_virtuser_config_subsys
.su_group
);
1763 mutex_init(&gpio_virtuser_config_subsys
.su_mutex
);
1764 ret
= configfs_register_subsystem(&gpio_virtuser_config_subsys
);
1766 pr_err("Failed to register the '%s' configfs subsystem: %d\n",
1767 gpio_virtuser_config_subsys
.su_group
.cg_item
.ci_namebuf
,
1769 goto err_plat_drv_unreg
;
1772 gpio_virtuser_dbg_root
= debugfs_create_dir("gpio-virtuser", NULL
);
1773 if (IS_ERR(gpio_virtuser_dbg_root
)) {
1774 ret
= PTR_ERR(gpio_virtuser_dbg_root
);
1775 pr_err("Failed to create the debugfs tree: %d\n", ret
);
1776 goto err_configfs_unreg
;
1782 configfs_unregister_subsystem(&gpio_virtuser_config_subsys
);
1784 mutex_destroy(&gpio_virtuser_config_subsys
.su_mutex
);
1785 platform_driver_unregister(&gpio_virtuser_driver
);
1789 module_init(gpio_virtuser_init
);
1791 static void __exit
gpio_virtuser_exit(void)
1793 configfs_unregister_subsystem(&gpio_virtuser_config_subsys
);
1794 mutex_destroy(&gpio_virtuser_config_subsys
.su_mutex
);
1795 platform_driver_unregister(&gpio_virtuser_driver
);
1796 debugfs_remove_recursive(gpio_virtuser_dbg_root
);
1798 module_exit(gpio_virtuser_exit
);
1800 MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
1801 MODULE_DESCRIPTION("Virtual GPIO consumer module");
1802 MODULE_LICENSE("GPL");