iio-trig-sysfs.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright 2011 Analog Devices Inc.
  3. *
  4. * Licensed under the GPL-2.
  5. *
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/slab.h>
  11. #include <linux/list.h>
  12. #include <linux/irq_work.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/iio/trigger.h>
  15. struct iio_sysfs_trig {
  16. struct iio_trigger *trig;
  17. struct irq_work work;
  18. int id;
  19. struct list_head l;
  20. };
  21. static LIST_HEAD(iio_sysfs_trig_list);
  22. static DEFINE_MUTEX(iio_sysfs_trig_list_mut);
  23. static int iio_sysfs_trigger_probe(int id);
  24. static ssize_t iio_sysfs_trig_add(struct device *dev,
  25. struct device_attribute *attr,
  26. const char *buf,
  27. size_t len)
  28. {
  29. int ret;
  30. unsigned long input;
  31. ret = kstrtoul(buf, 10, &input);
  32. if (ret)
  33. return ret;
  34. ret = iio_sysfs_trigger_probe(input);
  35. if (ret)
  36. return ret;
  37. return len;
  38. }
  39. static DEVICE_ATTR(add_trigger, S_IWUSR, NULL, &iio_sysfs_trig_add);
  40. static int iio_sysfs_trigger_remove(int id);
  41. static ssize_t iio_sysfs_trig_remove(struct device *dev,
  42. struct device_attribute *attr,
  43. const char *buf,
  44. size_t len)
  45. {
  46. int ret;
  47. unsigned long input;
  48. ret = kstrtoul(buf, 10, &input);
  49. if (ret)
  50. return ret;
  51. ret = iio_sysfs_trigger_remove(input);
  52. if (ret)
  53. return ret;
  54. return len;
  55. }
  56. static DEVICE_ATTR(remove_trigger, S_IWUSR, NULL, &iio_sysfs_trig_remove);
  57. static struct attribute *iio_sysfs_trig_attrs[] = {
  58. &dev_attr_add_trigger.attr,
  59. &dev_attr_remove_trigger.attr,
  60. NULL,
  61. };
  62. static const struct attribute_group iio_sysfs_trig_group = {
  63. .attrs = iio_sysfs_trig_attrs,
  64. };
  65. static const struct attribute_group *iio_sysfs_trig_groups[] = {
  66. &iio_sysfs_trig_group,
  67. NULL
  68. };
  69. /* Nothing to actually do upon release */
  70. static void iio_trigger_sysfs_release(struct device *dev)
  71. {
  72. }
  73. static struct device iio_sysfs_trig_dev = {
  74. .bus = &iio_bus_type,
  75. .groups = iio_sysfs_trig_groups,
  76. .release = &iio_trigger_sysfs_release,
  77. };
  78. static void iio_sysfs_trigger_work(struct irq_work *work)
  79. {
  80. struct iio_sysfs_trig *trig = container_of(work, struct iio_sysfs_trig,
  81. work);
  82. iio_trigger_poll(trig->trig);
  83. }
  84. static ssize_t iio_sysfs_trigger_poll(struct device *dev,
  85. struct device_attribute *attr, const char *buf, size_t count)
  86. {
  87. struct iio_trigger *trig = to_iio_trigger(dev);
  88. struct iio_sysfs_trig *sysfs_trig = iio_trigger_get_drvdata(trig);
  89. irq_work_queue(&sysfs_trig->work);
  90. return count;
  91. }
  92. static DEVICE_ATTR(trigger_now, S_IWUSR, NULL, iio_sysfs_trigger_poll);
  93. static struct attribute *iio_sysfs_trigger_attrs[] = {
  94. &dev_attr_trigger_now.attr,
  95. NULL,
  96. };
  97. static const struct attribute_group iio_sysfs_trigger_attr_group = {
  98. .attrs = iio_sysfs_trigger_attrs,
  99. };
  100. static const struct attribute_group *iio_sysfs_trigger_attr_groups[] = {
  101. &iio_sysfs_trigger_attr_group,
  102. NULL
  103. };
  104. static const struct iio_trigger_ops iio_sysfs_trigger_ops = {
  105. .owner = THIS_MODULE,
  106. };
  107. static int iio_sysfs_trigger_probe(int id)
  108. {
  109. struct iio_sysfs_trig *t;
  110. int ret;
  111. bool foundit = false;
  112. mutex_lock(&iio_sysfs_trig_list_mut);
  113. list_for_each_entry(t, &iio_sysfs_trig_list, l)
  114. if (id == t->id) {
  115. foundit = true;
  116. break;
  117. }
  118. if (foundit) {
  119. ret = -EINVAL;
  120. goto out1;
  121. }
  122. t = kmalloc(sizeof(*t), GFP_KERNEL);
  123. if (t == NULL) {
  124. ret = -ENOMEM;
  125. goto out1;
  126. }
  127. t->id = id;
  128. t->trig = iio_trigger_alloc("sysfstrig%d", id);
  129. if (!t->trig) {
  130. ret = -ENOMEM;
  131. goto free_t;
  132. }
  133. t->trig->dev.groups = iio_sysfs_trigger_attr_groups;
  134. t->trig->ops = &iio_sysfs_trigger_ops;
  135. t->trig->dev.parent = &iio_sysfs_trig_dev;
  136. iio_trigger_set_drvdata(t->trig, t);
  137. init_irq_work(&t->work, iio_sysfs_trigger_work);
  138. ret = iio_trigger_register(t->trig);
  139. if (ret)
  140. goto out2;
  141. list_add(&t->l, &iio_sysfs_trig_list);
  142. __module_get(THIS_MODULE);
  143. mutex_unlock(&iio_sysfs_trig_list_mut);
  144. return 0;
  145. out2:
  146. iio_trigger_free(t->trig);
  147. free_t:
  148. kfree(t);
  149. out1:
  150. mutex_unlock(&iio_sysfs_trig_list_mut);
  151. return ret;
  152. }
  153. static int iio_sysfs_trigger_remove(int id)
  154. {
  155. bool foundit = false;
  156. struct iio_sysfs_trig *t;
  157. mutex_lock(&iio_sysfs_trig_list_mut);
  158. list_for_each_entry(t, &iio_sysfs_trig_list, l)
  159. if (id == t->id) {
  160. foundit = true;
  161. break;
  162. }
  163. if (!foundit) {
  164. mutex_unlock(&iio_sysfs_trig_list_mut);
  165. return -EINVAL;
  166. }
  167. iio_trigger_unregister(t->trig);
  168. iio_trigger_free(t->trig);
  169. list_del(&t->l);
  170. kfree(t);
  171. module_put(THIS_MODULE);
  172. mutex_unlock(&iio_sysfs_trig_list_mut);
  173. return 0;
  174. }
  175. static int __init iio_sysfs_trig_init(void)
  176. {
  177. device_initialize(&iio_sysfs_trig_dev);
  178. dev_set_name(&iio_sysfs_trig_dev, "iio_sysfs_trigger");
  179. return device_add(&iio_sysfs_trig_dev);
  180. }
  181. module_init(iio_sysfs_trig_init);
  182. static void __exit iio_sysfs_trig_exit(void)
  183. {
  184. device_unregister(&iio_sysfs_trig_dev);
  185. }
  186. module_exit(iio_sysfs_trig_exit);
  187. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  188. MODULE_DESCRIPTION("Sysfs based trigger for the iio subsystem");
  189. MODULE_LICENSE("GPL v2");
  190. MODULE_ALIAS("platform:iio-trig-sysfs");