Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * The Industrial I/O core, software trigger functions
0004  *
0005  * Copyright (c) 2015 Intel Corporation
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_trigger.h>
0015 #include <linux/iio/configfs.h>
0016 #include <linux/configfs.h>
0017 
0018 static struct config_group *iio_triggers_group;
0019 static const struct config_item_type iio_trigger_type_group_type;
0020 
0021 static const struct config_item_type iio_triggers_group_type = {
0022     .ct_owner = THIS_MODULE,
0023 };
0024 
0025 static LIST_HEAD(iio_trigger_types_list);
0026 static DEFINE_MUTEX(iio_trigger_types_lock);
0027 
0028 static
0029 struct iio_sw_trigger_type *__iio_find_sw_trigger_type(const char *name,
0030                                unsigned int len)
0031 {
0032     struct iio_sw_trigger_type *t = NULL, *iter;
0033 
0034     list_for_each_entry(iter, &iio_trigger_types_list, list)
0035         if (!strcmp(iter->name, name)) {
0036             t = iter;
0037             break;
0038         }
0039 
0040     return t;
0041 }
0042 
0043 int iio_register_sw_trigger_type(struct iio_sw_trigger_type *t)
0044 {
0045     struct iio_sw_trigger_type *iter;
0046     int ret = 0;
0047 
0048     mutex_lock(&iio_trigger_types_lock);
0049     iter = __iio_find_sw_trigger_type(t->name, strlen(t->name));
0050     if (iter)
0051         ret = -EBUSY;
0052     else
0053         list_add_tail(&t->list, &iio_trigger_types_list);
0054     mutex_unlock(&iio_trigger_types_lock);
0055 
0056     if (ret)
0057         return ret;
0058 
0059     t->group = configfs_register_default_group(iio_triggers_group, t->name,
0060                         &iio_trigger_type_group_type);
0061     if (IS_ERR(t->group))
0062         ret = PTR_ERR(t->group);
0063 
0064     return ret;
0065 }
0066 EXPORT_SYMBOL(iio_register_sw_trigger_type);
0067 
0068 void iio_unregister_sw_trigger_type(struct iio_sw_trigger_type *t)
0069 {
0070     struct iio_sw_trigger_type *iter;
0071 
0072     mutex_lock(&iio_trigger_types_lock);
0073     iter = __iio_find_sw_trigger_type(t->name, strlen(t->name));
0074     if (iter)
0075         list_del(&t->list);
0076     mutex_unlock(&iio_trigger_types_lock);
0077 
0078     configfs_unregister_default_group(t->group);
0079 }
0080 EXPORT_SYMBOL(iio_unregister_sw_trigger_type);
0081 
0082 static
0083 struct iio_sw_trigger_type *iio_get_sw_trigger_type(const char *name)
0084 {
0085     struct iio_sw_trigger_type *t;
0086 
0087     mutex_lock(&iio_trigger_types_lock);
0088     t = __iio_find_sw_trigger_type(name, strlen(name));
0089     if (t && !try_module_get(t->owner))
0090         t = NULL;
0091     mutex_unlock(&iio_trigger_types_lock);
0092 
0093     return t;
0094 }
0095 
0096 struct iio_sw_trigger *iio_sw_trigger_create(const char *type, const char *name)
0097 {
0098     struct iio_sw_trigger *t;
0099     struct iio_sw_trigger_type *tt;
0100 
0101     tt = iio_get_sw_trigger_type(type);
0102     if (!tt) {
0103         pr_err("Invalid trigger type: %s\n", type);
0104         return ERR_PTR(-EINVAL);
0105     }
0106     t = tt->ops->probe(name);
0107     if (IS_ERR(t))
0108         goto out_module_put;
0109 
0110     t->trigger_type = tt;
0111 
0112     return t;
0113 out_module_put:
0114     module_put(tt->owner);
0115     return t;
0116 }
0117 EXPORT_SYMBOL(iio_sw_trigger_create);
0118 
0119 void iio_sw_trigger_destroy(struct iio_sw_trigger *t)
0120 {
0121     struct iio_sw_trigger_type *tt = t->trigger_type;
0122 
0123     tt->ops->remove(t);
0124     module_put(tt->owner);
0125 }
0126 EXPORT_SYMBOL(iio_sw_trigger_destroy);
0127 
0128 static struct config_group *trigger_make_group(struct config_group *group,
0129                            const char *name)
0130 {
0131     struct iio_sw_trigger *t;
0132 
0133     t = iio_sw_trigger_create(group->cg_item.ci_name, name);
0134     if (IS_ERR(t))
0135         return ERR_CAST(t);
0136 
0137     config_item_set_name(&t->group.cg_item, "%s", name);
0138 
0139     return &t->group;
0140 }
0141 
0142 static void trigger_drop_group(struct config_group *group,
0143                    struct config_item *item)
0144 {
0145     struct iio_sw_trigger *t = to_iio_sw_trigger(item);
0146 
0147     iio_sw_trigger_destroy(t);
0148     config_item_put(item);
0149 }
0150 
0151 static struct configfs_group_operations trigger_ops = {
0152     .make_group = &trigger_make_group,
0153     .drop_item  = &trigger_drop_group,
0154 };
0155 
0156 static const struct config_item_type iio_trigger_type_group_type = {
0157     .ct_group_ops = &trigger_ops,
0158     .ct_owner       = THIS_MODULE,
0159 };
0160 
0161 static int __init iio_sw_trigger_init(void)
0162 {
0163     iio_triggers_group =
0164         configfs_register_default_group(&iio_configfs_subsys.su_group,
0165                         "triggers",
0166                         &iio_triggers_group_type);
0167     return PTR_ERR_OR_ZERO(iio_triggers_group);
0168 }
0169 module_init(iio_sw_trigger_init);
0170 
0171 static void __exit iio_sw_trigger_exit(void)
0172 {
0173     configfs_unregister_default_group(iio_triggers_group);
0174 }
0175 module_exit(iio_sw_trigger_exit);
0176 
0177 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
0178 MODULE_DESCRIPTION("Industrial I/O software triggers support");
0179 MODULE_LICENSE("GPL v2");