^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * The Industrial I/O core, software IIO devices functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2016 Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kmod.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/iio/sw_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/iio/configfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/configfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static struct config_group *iio_devices_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static const struct config_item_type iio_device_type_group_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static const struct config_item_type iio_devices_group_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) .ct_owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static LIST_HEAD(iio_device_types_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static DEFINE_MUTEX(iio_device_types_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct iio_sw_device_type *__iio_find_sw_device_type(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) unsigned len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct iio_sw_device_type *d = NULL, *iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) list_for_each_entry(iter, &iio_device_types_list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (!strcmp(iter->name, name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) d = iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int iio_register_sw_device_type(struct iio_sw_device_type *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct iio_sw_device_type *iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) mutex_lock(&iio_device_types_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) iter = __iio_find_sw_device_type(d->name, strlen(d->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) list_add_tail(&d->list, &iio_device_types_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) mutex_unlock(&iio_device_types_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) d->group = configfs_register_default_group(iio_devices_group, d->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) &iio_device_type_group_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (IS_ERR(d->group))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ret = PTR_ERR(d->group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) EXPORT_SYMBOL(iio_register_sw_device_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) void iio_unregister_sw_device_type(struct iio_sw_device_type *dt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct iio_sw_device_type *iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) mutex_lock(&iio_device_types_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) iter = __iio_find_sw_device_type(dt->name, strlen(dt->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) list_del(&dt->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) mutex_unlock(&iio_device_types_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) configfs_unregister_default_group(dt->group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) EXPORT_SYMBOL(iio_unregister_sw_device_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct iio_sw_device_type *iio_get_sw_device_type(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct iio_sw_device_type *dt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) mutex_lock(&iio_device_types_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) dt = __iio_find_sw_device_type(name, strlen(name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (dt && !try_module_get(dt->owner))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) dt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) mutex_unlock(&iio_device_types_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return dt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct iio_sw_device *iio_sw_device_create(const char *type, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct iio_sw_device *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct iio_sw_device_type *dt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) dt = iio_get_sw_device_type(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (!dt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) pr_err("Invalid device type: %s\n", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) d = dt->ops->probe(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (IS_ERR(d))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) goto out_module_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) d->device_type = dt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) out_module_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) module_put(dt->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) EXPORT_SYMBOL(iio_sw_device_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) void iio_sw_device_destroy(struct iio_sw_device *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct iio_sw_device_type *dt = d->device_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) dt->ops->remove(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) module_put(dt->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) EXPORT_SYMBOL(iio_sw_device_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static struct config_group *device_make_group(struct config_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct iio_sw_device *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) d = iio_sw_device_create(group->cg_item.ci_name, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (IS_ERR(d))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return ERR_CAST(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) config_item_set_name(&d->group.cg_item, "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return &d->group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static void device_drop_group(struct config_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct iio_sw_device *d = to_iio_sw_device(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) iio_sw_device_destroy(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) config_item_put(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static struct configfs_group_operations device_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) .make_group = &device_make_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) .drop_item = &device_drop_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static const struct config_item_type iio_device_type_group_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) .ct_group_ops = &device_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) .ct_owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static int __init iio_sw_device_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) iio_devices_group =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) configfs_register_default_group(&iio_configfs_subsys.su_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) "devices",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) &iio_devices_group_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return PTR_ERR_OR_ZERO(iio_devices_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) module_init(iio_sw_device_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static void __exit iio_sw_device_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) configfs_unregister_default_group(iio_devices_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) module_exit(iio_sw_device_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) MODULE_DESCRIPTION("Industrial I/O software devices support");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) MODULE_LICENSE("GPL v2");