industrialio-sw-trigger.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * The Industrial I/O core, software trigger functions
  4. *
  5. * Copyright (c) 2015 Intel Corporation
  6. */
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/kmod.h>
  10. #include <linux/list.h>
  11. #include <linux/slab.h>
  12. #include <linux/iio/sw_trigger.h>
  13. #include <linux/iio/configfs.h>
  14. #include <linux/configfs.h>
  15. static struct config_group *iio_triggers_group;
  16. static const struct config_item_type iio_trigger_type_group_type;
  17. static const struct config_item_type iio_triggers_group_type = {
  18. .ct_owner = THIS_MODULE,
  19. };
  20. static LIST_HEAD(iio_trigger_types_list);
  21. static DEFINE_MUTEX(iio_trigger_types_lock);
  22. static
  23. struct iio_sw_trigger_type *__iio_find_sw_trigger_type(const char *name,
  24. unsigned len)
  25. {
  26. struct iio_sw_trigger_type *t = NULL, *iter;
  27. list_for_each_entry(iter, &iio_trigger_types_list, list)
  28. if (!strcmp(iter->name, name)) {
  29. t = iter;
  30. break;
  31. }
  32. return t;
  33. }
  34. int iio_register_sw_trigger_type(struct iio_sw_trigger_type *t)
  35. {
  36. struct iio_sw_trigger_type *iter;
  37. int ret = 0;
  38. mutex_lock(&iio_trigger_types_lock);
  39. iter = __iio_find_sw_trigger_type(t->name, strlen(t->name));
  40. if (iter)
  41. ret = -EBUSY;
  42. else
  43. list_add_tail(&t->list, &iio_trigger_types_list);
  44. mutex_unlock(&iio_trigger_types_lock);
  45. if (ret)
  46. return ret;
  47. t->group = configfs_register_default_group(iio_triggers_group, t->name,
  48. &iio_trigger_type_group_type);
  49. if (IS_ERR(t->group))
  50. ret = PTR_ERR(t->group);
  51. return ret;
  52. }
  53. EXPORT_SYMBOL(iio_register_sw_trigger_type);
  54. void iio_unregister_sw_trigger_type(struct iio_sw_trigger_type *t)
  55. {
  56. struct iio_sw_trigger_type *iter;
  57. mutex_lock(&iio_trigger_types_lock);
  58. iter = __iio_find_sw_trigger_type(t->name, strlen(t->name));
  59. if (iter)
  60. list_del(&t->list);
  61. mutex_unlock(&iio_trigger_types_lock);
  62. configfs_unregister_default_group(t->group);
  63. }
  64. EXPORT_SYMBOL(iio_unregister_sw_trigger_type);
  65. static
  66. struct iio_sw_trigger_type *iio_get_sw_trigger_type(const char *name)
  67. {
  68. struct iio_sw_trigger_type *t;
  69. mutex_lock(&iio_trigger_types_lock);
  70. t = __iio_find_sw_trigger_type(name, strlen(name));
  71. if (t && !try_module_get(t->owner))
  72. t = NULL;
  73. mutex_unlock(&iio_trigger_types_lock);
  74. return t;
  75. }
  76. struct iio_sw_trigger *iio_sw_trigger_create(const char *type, const char *name)
  77. {
  78. struct iio_sw_trigger *t;
  79. struct iio_sw_trigger_type *tt;
  80. tt = iio_get_sw_trigger_type(type);
  81. if (!tt) {
  82. pr_err("Invalid trigger type: %s\n", type);
  83. return ERR_PTR(-EINVAL);
  84. }
  85. t = tt->ops->probe(name);
  86. if (IS_ERR(t))
  87. goto out_module_put;
  88. t->trigger_type = tt;
  89. return t;
  90. out_module_put:
  91. module_put(tt->owner);
  92. return t;
  93. }
  94. EXPORT_SYMBOL(iio_sw_trigger_create);
  95. void iio_sw_trigger_destroy(struct iio_sw_trigger *t)
  96. {
  97. struct iio_sw_trigger_type *tt = t->trigger_type;
  98. tt->ops->remove(t);
  99. module_put(tt->owner);
  100. }
  101. EXPORT_SYMBOL(iio_sw_trigger_destroy);
  102. static struct config_group *trigger_make_group(struct config_group *group,
  103. const char *name)
  104. {
  105. struct iio_sw_trigger *t;
  106. t = iio_sw_trigger_create(group->cg_item.ci_name, name);
  107. if (IS_ERR(t))
  108. return ERR_CAST(t);
  109. config_item_set_name(&t->group.cg_item, "%s", name);
  110. return &t->group;
  111. }
  112. static void trigger_drop_group(struct config_group *group,
  113. struct config_item *item)
  114. {
  115. struct iio_sw_trigger *t = to_iio_sw_trigger(item);
  116. iio_sw_trigger_destroy(t);
  117. config_item_put(item);
  118. }
  119. static struct configfs_group_operations trigger_ops = {
  120. .make_group = &trigger_make_group,
  121. .drop_item = &trigger_drop_group,
  122. };
  123. static const struct config_item_type iio_trigger_type_group_type = {
  124. .ct_group_ops = &trigger_ops,
  125. .ct_owner = THIS_MODULE,
  126. };
  127. static int __init iio_sw_trigger_init(void)
  128. {
  129. iio_triggers_group =
  130. configfs_register_default_group(&iio_configfs_subsys.su_group,
  131. "triggers",
  132. &iio_triggers_group_type);
  133. return PTR_ERR_OR_ZERO(iio_triggers_group);
  134. }
  135. module_init(iio_sw_trigger_init);
  136. static void __exit iio_sw_trigger_exit(void)
  137. {
  138. configfs_unregister_default_group(iio_triggers_group);
  139. }
  140. module_exit(iio_sw_trigger_exit);
  141. MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
  142. MODULE_DESCRIPTION("Industrial I/O software triggers support");
  143. MODULE_LICENSE("GPL v2");