industrialio-sw-trigger.c 4.3 KB

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