Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / iio / industrialio-sw-device.c
blob49f775f16ad52100fb8dd2c172d16e36a8cb08b2
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * The Industrial I/O core, software IIO devices functions
5 * Copyright (c) 2016 Intel Corporation
6 */
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/kmod.h>
11 #include <linux/list.h>
12 #include <linux/slab.h>
14 #include <linux/iio/sw_device.h>
15 #include <linux/iio/configfs.h>
16 #include <linux/configfs.h>
18 static struct config_group *iio_devices_group;
19 static const struct config_item_type iio_device_type_group_type;
21 static const struct config_item_type iio_devices_group_type = {
22 .ct_owner = THIS_MODULE,
25 static LIST_HEAD(iio_device_types_list);
26 static DEFINE_MUTEX(iio_device_types_lock);
28 static
29 struct iio_sw_device_type *__iio_find_sw_device_type(const char *name,
30 unsigned len)
32 struct iio_sw_device_type *d = NULL, *iter;
34 list_for_each_entry(iter, &iio_device_types_list, list)
35 if (!strcmp(iter->name, name)) {
36 d = iter;
37 break;
40 return d;
43 int iio_register_sw_device_type(struct iio_sw_device_type *d)
45 struct iio_sw_device_type *iter;
46 int ret = 0;
48 mutex_lock(&iio_device_types_lock);
49 iter = __iio_find_sw_device_type(d->name, strlen(d->name));
50 if (iter)
51 ret = -EBUSY;
52 else
53 list_add_tail(&d->list, &iio_device_types_list);
54 mutex_unlock(&iio_device_types_lock);
56 if (ret)
57 return ret;
59 d->group = configfs_register_default_group(iio_devices_group, d->name,
60 &iio_device_type_group_type);
61 if (IS_ERR(d->group))
62 ret = PTR_ERR(d->group);
64 return ret;
66 EXPORT_SYMBOL(iio_register_sw_device_type);
68 void iio_unregister_sw_device_type(struct iio_sw_device_type *dt)
70 struct iio_sw_device_type *iter;
72 mutex_lock(&iio_device_types_lock);
73 iter = __iio_find_sw_device_type(dt->name, strlen(dt->name));
74 if (iter)
75 list_del(&dt->list);
76 mutex_unlock(&iio_device_types_lock);
78 configfs_unregister_default_group(dt->group);
80 EXPORT_SYMBOL(iio_unregister_sw_device_type);
82 static
83 struct iio_sw_device_type *iio_get_sw_device_type(const char *name)
85 struct iio_sw_device_type *dt;
87 mutex_lock(&iio_device_types_lock);
88 dt = __iio_find_sw_device_type(name, strlen(name));
89 if (dt && !try_module_get(dt->owner))
90 dt = NULL;
91 mutex_unlock(&iio_device_types_lock);
93 return dt;
96 struct iio_sw_device *iio_sw_device_create(const char *type, const char *name)
98 struct iio_sw_device *d;
99 struct iio_sw_device_type *dt;
101 dt = iio_get_sw_device_type(type);
102 if (!dt) {
103 pr_err("Invalid device type: %s\n", type);
104 return ERR_PTR(-EINVAL);
106 d = dt->ops->probe(name);
107 if (IS_ERR(d))
108 goto out_module_put;
110 d->device_type = dt;
112 return d;
113 out_module_put:
114 module_put(dt->owner);
115 return d;
117 EXPORT_SYMBOL(iio_sw_device_create);
119 void iio_sw_device_destroy(struct iio_sw_device *d)
121 struct iio_sw_device_type *dt = d->device_type;
123 dt->ops->remove(d);
124 module_put(dt->owner);
126 EXPORT_SYMBOL(iio_sw_device_destroy);
128 static struct config_group *device_make_group(struct config_group *group,
129 const char *name)
131 struct iio_sw_device *d;
133 d = iio_sw_device_create(group->cg_item.ci_name, name);
134 if (IS_ERR(d))
135 return ERR_CAST(d);
137 config_item_set_name(&d->group.cg_item, "%s", name);
139 return &d->group;
142 static void device_drop_group(struct config_group *group,
143 struct config_item *item)
145 struct iio_sw_device *d = to_iio_sw_device(item);
147 iio_sw_device_destroy(d);
148 config_item_put(item);
151 static struct configfs_group_operations device_ops = {
152 .make_group = &device_make_group,
153 .drop_item = &device_drop_group,
156 static const struct config_item_type iio_device_type_group_type = {
157 .ct_group_ops = &device_ops,
158 .ct_owner = THIS_MODULE,
161 static int __init iio_sw_device_init(void)
163 iio_devices_group =
164 configfs_register_default_group(&iio_configfs_subsys.su_group,
165 "devices",
166 &iio_devices_group_type);
167 return PTR_ERR_OR_ZERO(iio_devices_group);
169 module_init(iio_sw_device_init);
171 static void __exit iio_sw_device_exit(void)
173 configfs_unregister_default_group(iio_devices_group);
175 module_exit(iio_sw_device_exit);
177 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
178 MODULE_DESCRIPTION("Industrial I/O software devices support");
179 MODULE_LICENSE("GPL v2");