0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/module.h>
0009 #include <linux/init.h>
0010 #include <linux/kmod.h>
0011 #include <linux/list.h>
0012 #include <linux/slab.h>
0013
0014 #include <linux/iio/sw_device.h>
0015 #include <linux/iio/configfs.h>
0016 #include <linux/configfs.h>
0017
0018 static struct config_group *iio_devices_group;
0019 static const struct config_item_type iio_device_type_group_type;
0020
0021 static const struct config_item_type iio_devices_group_type = {
0022 .ct_owner = THIS_MODULE,
0023 };
0024
0025 static LIST_HEAD(iio_device_types_list);
0026 static DEFINE_MUTEX(iio_device_types_lock);
0027
0028 static
0029 struct iio_sw_device_type *__iio_find_sw_device_type(const char *name,
0030 unsigned int len)
0031 {
0032 struct iio_sw_device_type *d = NULL, *iter;
0033
0034 list_for_each_entry(iter, &iio_device_types_list, list)
0035 if (!strcmp(iter->name, name)) {
0036 d = iter;
0037 break;
0038 }
0039
0040 return d;
0041 }
0042
0043 int iio_register_sw_device_type(struct iio_sw_device_type *d)
0044 {
0045 struct iio_sw_device_type *iter;
0046 int ret = 0;
0047
0048 mutex_lock(&iio_device_types_lock);
0049 iter = __iio_find_sw_device_type(d->name, strlen(d->name));
0050 if (iter)
0051 ret = -EBUSY;
0052 else
0053 list_add_tail(&d->list, &iio_device_types_list);
0054 mutex_unlock(&iio_device_types_lock);
0055
0056 if (ret)
0057 return ret;
0058
0059 d->group = configfs_register_default_group(iio_devices_group, d->name,
0060 &iio_device_type_group_type);
0061 if (IS_ERR(d->group))
0062 ret = PTR_ERR(d->group);
0063
0064 return ret;
0065 }
0066 EXPORT_SYMBOL(iio_register_sw_device_type);
0067
0068 void iio_unregister_sw_device_type(struct iio_sw_device_type *dt)
0069 {
0070 struct iio_sw_device_type *iter;
0071
0072 mutex_lock(&iio_device_types_lock);
0073 iter = __iio_find_sw_device_type(dt->name, strlen(dt->name));
0074 if (iter)
0075 list_del(&dt->list);
0076 mutex_unlock(&iio_device_types_lock);
0077
0078 configfs_unregister_default_group(dt->group);
0079 }
0080 EXPORT_SYMBOL(iio_unregister_sw_device_type);
0081
0082 static
0083 struct iio_sw_device_type *iio_get_sw_device_type(const char *name)
0084 {
0085 struct iio_sw_device_type *dt;
0086
0087 mutex_lock(&iio_device_types_lock);
0088 dt = __iio_find_sw_device_type(name, strlen(name));
0089 if (dt && !try_module_get(dt->owner))
0090 dt = NULL;
0091 mutex_unlock(&iio_device_types_lock);
0092
0093 return dt;
0094 }
0095
0096 struct iio_sw_device *iio_sw_device_create(const char *type, const char *name)
0097 {
0098 struct iio_sw_device *d;
0099 struct iio_sw_device_type *dt;
0100
0101 dt = iio_get_sw_device_type(type);
0102 if (!dt) {
0103 pr_err("Invalid device type: %s\n", type);
0104 return ERR_PTR(-EINVAL);
0105 }
0106 d = dt->ops->probe(name);
0107 if (IS_ERR(d))
0108 goto out_module_put;
0109
0110 d->device_type = dt;
0111
0112 return d;
0113 out_module_put:
0114 module_put(dt->owner);
0115 return d;
0116 }
0117 EXPORT_SYMBOL(iio_sw_device_create);
0118
0119 void iio_sw_device_destroy(struct iio_sw_device *d)
0120 {
0121 struct iio_sw_device_type *dt = d->device_type;
0122
0123 dt->ops->remove(d);
0124 module_put(dt->owner);
0125 }
0126 EXPORT_SYMBOL(iio_sw_device_destroy);
0127
0128 static struct config_group *device_make_group(struct config_group *group,
0129 const char *name)
0130 {
0131 struct iio_sw_device *d;
0132
0133 d = iio_sw_device_create(group->cg_item.ci_name, name);
0134 if (IS_ERR(d))
0135 return ERR_CAST(d);
0136
0137 config_item_set_name(&d->group.cg_item, "%s", name);
0138
0139 return &d->group;
0140 }
0141
0142 static void device_drop_group(struct config_group *group,
0143 struct config_item *item)
0144 {
0145 struct iio_sw_device *d = to_iio_sw_device(item);
0146
0147 iio_sw_device_destroy(d);
0148 config_item_put(item);
0149 }
0150
0151 static struct configfs_group_operations device_ops = {
0152 .make_group = &device_make_group,
0153 .drop_item = &device_drop_group,
0154 };
0155
0156 static const struct config_item_type iio_device_type_group_type = {
0157 .ct_group_ops = &device_ops,
0158 .ct_owner = THIS_MODULE,
0159 };
0160
0161 static int __init iio_sw_device_init(void)
0162 {
0163 iio_devices_group =
0164 configfs_register_default_group(&iio_configfs_subsys.su_group,
0165 "devices",
0166 &iio_devices_group_type);
0167 return PTR_ERR_OR_ZERO(iio_devices_group);
0168 }
0169 module_init(iio_sw_device_init);
0170
0171 static void __exit iio_sw_device_exit(void)
0172 {
0173 configfs_unregister_default_group(iio_devices_group);
0174 }
0175 module_exit(iio_sw_device_exit);
0176
0177 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
0178 MODULE_DESCRIPTION("Industrial I/O software devices support");
0179 MODULE_LICENSE("GPL v2");