iio-trig-sysfs.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. };
  106. static int iio_sysfs_trigger_probe(int id)
  107. {
  108. struct iio_sysfs_trig *t;
  109. int ret;
  110. bool foundit = false;
  111. mutex_lock(&iio_sysfs_trig_list_mut);
  112. list_for_each_entry(t, &iio_sysfs_trig_list, l)
  113. if (id == t->id) {
  114. foundit = true;
  115. break;
  116. }
  117. if (foundit) {
  118. ret = -EINVAL;
  119. goto out1;
  120. }
  121. t = kmalloc(sizeof(*t), GFP_KERNEL);
  122. if (t == NULL) {
  123. ret = -ENOMEM;
  124. goto out1;
  125. }
  126. t->id = id;
  127. t->trig = iio_trigger_alloc("sysfstrig%d", id);
  128. if (!t->trig) {
  129. ret = -ENOMEM;
  130. goto free_t;
  131. }
  132. t->trig->dev.groups = iio_sysfs_trigger_attr_groups;
  133. t->trig->ops = &iio_sysfs_trigger_ops;
  134. t->trig->dev.parent = &iio_sysfs_trig_dev;
  135. iio_trigger_set_drvdata(t->trig, t);
  136. init_irq_work(&t->work, iio_sysfs_trigger_work);
  137. ret = iio_trigger_register(t->trig);
  138. if (ret)
  139. goto out2;
  140. list_add(&t->l, &iio_sysfs_trig_list);
  141. __module_get(THIS_MODULE);
  142. mutex_unlock(&iio_sysfs_trig_list_mut);
  143. return 0;
  144. out2:
  145. iio_trigger_free(t->trig);
  146. free_t:
  147. kfree(t);
  148. out1:
  149. mutex_unlock(&iio_sysfs_trig_list_mut);
  150. return ret;
  151. }
  152. static int iio_sysfs_trigger_remove(int id)
  153. {
  154. bool foundit = false;
  155. struct iio_sysfs_trig *t;
  156. mutex_lock(&iio_sysfs_trig_list_mut);
  157. list_for_each_entry(t, &iio_sysfs_trig_list, l)
  158. if (id == t->id) {
  159. foundit = true;
  160. break;
  161. }
  162. if (!foundit) {
  163. mutex_unlock(&iio_sysfs_trig_list_mut);
  164. return -EINVAL;
  165. }
  166. iio_trigger_unregister(t->trig);
  167. iio_trigger_free(t->trig);
  168. list_del(&t->l);
  169. kfree(t);
  170. module_put(THIS_MODULE);
  171. mutex_unlock(&iio_sysfs_trig_list_mut);
  172. return 0;
  173. }
  174. static int __init iio_sysfs_trig_init(void)
  175. {
  176. device_initialize(&iio_sysfs_trig_dev);
  177. dev_set_name(&iio_sysfs_trig_dev, "iio_sysfs_trigger");
  178. return device_add(&iio_sysfs_trig_dev);
  179. }
  180. module_init(iio_sysfs_trig_init);
  181. static void __exit iio_sysfs_trig_exit(void)
  182. {
  183. device_unregister(&iio_sysfs_trig_dev);
  184. }
  185. module_exit(iio_sysfs_trig_exit);
  186. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  187. MODULE_DESCRIPTION("Sysfs based trigger for the iio subsystem");
  188. MODULE_LICENSE("GPL v2");
  189. MODULE_ALIAS("platform:iio-trig-sysfs");