industrialio-sw-trigger.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 int 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. mutex_lock(&iio_trigger_types_lock);
  51. list_del(&t->list);
  52. mutex_unlock(&iio_trigger_types_lock);
  53. ret = PTR_ERR(t->group);
  54. }
  55. return ret;
  56. }
  57. EXPORT_SYMBOL(iio_register_sw_trigger_type);
  58. void iio_unregister_sw_trigger_type(struct iio_sw_trigger_type *t)
  59. {
  60. struct iio_sw_trigger_type *iter;
  61. mutex_lock(&iio_trigger_types_lock);
  62. iter = __iio_find_sw_trigger_type(t->name, strlen(t->name));
  63. if (iter)
  64. list_del(&t->list);
  65. mutex_unlock(&iio_trigger_types_lock);
  66. configfs_unregister_default_group(t->group);
  67. }
  68. EXPORT_SYMBOL(iio_unregister_sw_trigger_type);
  69. static
  70. struct iio_sw_trigger_type *iio_get_sw_trigger_type(const char *name)
  71. {
  72. struct iio_sw_trigger_type *t;
  73. mutex_lock(&iio_trigger_types_lock);
  74. t = __iio_find_sw_trigger_type(name, strlen(name));
  75. if (t && !try_module_get(t->owner))
  76. t = NULL;
  77. mutex_unlock(&iio_trigger_types_lock);
  78. return t;
  79. }
  80. struct iio_sw_trigger *iio_sw_trigger_create(const char *type, const char *name)
  81. {
  82. struct iio_sw_trigger *t;
  83. struct iio_sw_trigger_type *tt;
  84. tt = iio_get_sw_trigger_type(type);
  85. if (!tt) {
  86. pr_err("Invalid trigger type: %s\n", type);
  87. return ERR_PTR(-EINVAL);
  88. }
  89. t = tt->ops->probe(name);
  90. if (IS_ERR(t))
  91. goto out_module_put;
  92. t->trigger_type = tt;
  93. return t;
  94. out_module_put:
  95. module_put(tt->owner);
  96. return t;
  97. }
  98. EXPORT_SYMBOL(iio_sw_trigger_create);
  99. void iio_sw_trigger_destroy(struct iio_sw_trigger *t)
  100. {
  101. struct iio_sw_trigger_type *tt = t->trigger_type;
  102. tt->ops->remove(t);
  103. module_put(tt->owner);
  104. }
  105. EXPORT_SYMBOL(iio_sw_trigger_destroy);
  106. static struct config_group *trigger_make_group(struct config_group *group,
  107. const char *name)
  108. {
  109. struct iio_sw_trigger *t;
  110. t = iio_sw_trigger_create(group->cg_item.ci_name, name);
  111. if (IS_ERR(t))
  112. return ERR_CAST(t);
  113. config_item_set_name(&t->group.cg_item, "%s", name);
  114. return &t->group;
  115. }
  116. static void trigger_drop_group(struct config_group *group,
  117. struct config_item *item)
  118. {
  119. struct iio_sw_trigger *t = to_iio_sw_trigger(item);
  120. iio_sw_trigger_destroy(t);
  121. config_item_put(item);
  122. }
  123. static struct configfs_group_operations trigger_ops = {
  124. .make_group = &trigger_make_group,
  125. .drop_item = &trigger_drop_group,
  126. };
  127. static const struct config_item_type iio_trigger_type_group_type = {
  128. .ct_group_ops = &trigger_ops,
  129. .ct_owner = THIS_MODULE,
  130. };
  131. static int __init iio_sw_trigger_init(void)
  132. {
  133. iio_triggers_group =
  134. configfs_register_default_group(&iio_configfs_subsys.su_group,
  135. "triggers",
  136. &iio_triggers_group_type);
  137. return PTR_ERR_OR_ZERO(iio_triggers_group);
  138. }
  139. module_init(iio_sw_trigger_init);
  140. static void __exit iio_sw_trigger_exit(void)
  141. {
  142. configfs_unregister_default_group(iio_triggers_group);
  143. }
  144. module_exit(iio_sw_trigger_exit);
  145. MODULE_AUTHOR("Daniel Baluta <[email protected]>");
  146. MODULE_DESCRIPTION("Industrial I/O software triggers support");
  147. MODULE_LICENSE("GPL v2");