industrialio-trigger.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /* The industrial I/O core, trigger handling functions
  2. *
  3. * Copyright (c) 2008 Jonathan Cameron
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/idr.h>
  11. #include <linux/err.h>
  12. #include <linux/device.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/list.h>
  15. #include <linux/slab.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/trigger.h>
  18. #include "iio_core.h"
  19. #include "iio_core_trigger.h"
  20. #include <linux/iio/trigger_consumer.h>
  21. /* RFC - Question of approach
  22. * Make the common case (single sensor single trigger)
  23. * simple by starting trigger capture from when first sensors
  24. * is added.
  25. *
  26. * Complex simultaneous start requires use of 'hold' functionality
  27. * of the trigger. (not implemented)
  28. *
  29. * Any other suggestions?
  30. */
  31. static DEFINE_IDA(iio_trigger_ida);
  32. /* Single list of all available triggers */
  33. static LIST_HEAD(iio_trigger_list);
  34. static DEFINE_MUTEX(iio_trigger_list_lock);
  35. /**
  36. * iio_trigger_read_name() - retrieve useful identifying name
  37. **/
  38. static ssize_t iio_trigger_read_name(struct device *dev,
  39. struct device_attribute *attr,
  40. char *buf)
  41. {
  42. struct iio_trigger *trig = to_iio_trigger(dev);
  43. return sprintf(buf, "%s\n", trig->name);
  44. }
  45. static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
  46. static struct attribute *iio_trig_dev_attrs[] = {
  47. &dev_attr_name.attr,
  48. NULL,
  49. };
  50. static struct attribute_group iio_trig_attr_group = {
  51. .attrs = iio_trig_dev_attrs,
  52. };
  53. static const struct attribute_group *iio_trig_attr_groups[] = {
  54. &iio_trig_attr_group,
  55. NULL
  56. };
  57. int iio_trigger_register(struct iio_trigger *trig_info)
  58. {
  59. int ret;
  60. trig_info->id = ida_simple_get(&iio_trigger_ida, 0, 0, GFP_KERNEL);
  61. if (trig_info->id < 0) {
  62. ret = trig_info->id;
  63. goto error_ret;
  64. }
  65. /* Set the name used for the sysfs directory etc */
  66. dev_set_name(&trig_info->dev, "trigger%ld",
  67. (unsigned long) trig_info->id);
  68. ret = device_add(&trig_info->dev);
  69. if (ret)
  70. goto error_unregister_id;
  71. /* Add to list of available triggers held by the IIO core */
  72. mutex_lock(&iio_trigger_list_lock);
  73. list_add_tail(&trig_info->list, &iio_trigger_list);
  74. mutex_unlock(&iio_trigger_list_lock);
  75. return 0;
  76. error_unregister_id:
  77. ida_simple_remove(&iio_trigger_ida, trig_info->id);
  78. error_ret:
  79. return ret;
  80. }
  81. EXPORT_SYMBOL(iio_trigger_register);
  82. void iio_trigger_unregister(struct iio_trigger *trig_info)
  83. {
  84. mutex_lock(&iio_trigger_list_lock);
  85. list_del(&trig_info->list);
  86. mutex_unlock(&iio_trigger_list_lock);
  87. ida_simple_remove(&iio_trigger_ida, trig_info->id);
  88. /* Possible issue in here */
  89. device_unregister(&trig_info->dev);
  90. }
  91. EXPORT_SYMBOL(iio_trigger_unregister);
  92. static struct iio_trigger *iio_trigger_find_by_name(const char *name,
  93. size_t len)
  94. {
  95. struct iio_trigger *trig = NULL, *iter;
  96. mutex_lock(&iio_trigger_list_lock);
  97. list_for_each_entry(iter, &iio_trigger_list, list)
  98. if (sysfs_streq(iter->name, name)) {
  99. trig = iter;
  100. break;
  101. }
  102. mutex_unlock(&iio_trigger_list_lock);
  103. return trig;
  104. }
  105. void iio_trigger_poll(struct iio_trigger *trig, s64 time)
  106. {
  107. int i;
  108. if (!trig->use_count)
  109. for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++)
  110. if (trig->subirqs[i].enabled) {
  111. trig->use_count++;
  112. generic_handle_irq(trig->subirq_base + i);
  113. }
  114. }
  115. EXPORT_SYMBOL(iio_trigger_poll);
  116. irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
  117. {
  118. iio_trigger_poll(private, iio_get_time_ns());
  119. return IRQ_HANDLED;
  120. }
  121. EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
  122. void iio_trigger_poll_chained(struct iio_trigger *trig, s64 time)
  123. {
  124. int i;
  125. if (!trig->use_count)
  126. for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++)
  127. if (trig->subirqs[i].enabled) {
  128. trig->use_count++;
  129. handle_nested_irq(trig->subirq_base + i);
  130. }
  131. }
  132. EXPORT_SYMBOL(iio_trigger_poll_chained);
  133. void iio_trigger_notify_done(struct iio_trigger *trig)
  134. {
  135. trig->use_count--;
  136. if (trig->use_count == 0 && trig->ops && trig->ops->try_reenable)
  137. if (trig->ops->try_reenable(trig))
  138. /* Missed an interrupt so launch new poll now */
  139. iio_trigger_poll(trig, 0);
  140. }
  141. EXPORT_SYMBOL(iio_trigger_notify_done);
  142. /* Trigger Consumer related functions */
  143. static int iio_trigger_get_irq(struct iio_trigger *trig)
  144. {
  145. int ret;
  146. mutex_lock(&trig->pool_lock);
  147. ret = bitmap_find_free_region(trig->pool,
  148. CONFIG_IIO_CONSUMERS_PER_TRIGGER,
  149. ilog2(1));
  150. mutex_unlock(&trig->pool_lock);
  151. if (ret >= 0)
  152. ret += trig->subirq_base;
  153. return ret;
  154. }
  155. static void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
  156. {
  157. mutex_lock(&trig->pool_lock);
  158. clear_bit(irq - trig->subirq_base, trig->pool);
  159. mutex_unlock(&trig->pool_lock);
  160. }
  161. /* Complexity in here. With certain triggers (datardy) an acknowledgement
  162. * may be needed if the pollfuncs do not include the data read for the
  163. * triggering device.
  164. * This is not currently handled. Alternative of not enabling trigger unless
  165. * the relevant function is in there may be the best option.
  166. */
  167. /* Worth protecting against double additions? */
  168. static int iio_trigger_attach_poll_func(struct iio_trigger *trig,
  169. struct iio_poll_func *pf)
  170. {
  171. int ret = 0;
  172. bool notinuse
  173. = bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
  174. /* Prevent the module from being removed whilst attached to a trigger */
  175. __module_get(pf->indio_dev->info->driver_module);
  176. pf->irq = iio_trigger_get_irq(trig);
  177. ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
  178. pf->type, pf->name,
  179. pf);
  180. if (ret < 0) {
  181. module_put(pf->indio_dev->info->driver_module);
  182. return ret;
  183. }
  184. if (trig->ops && trig->ops->set_trigger_state && notinuse) {
  185. ret = trig->ops->set_trigger_state(trig, true);
  186. if (ret < 0)
  187. module_put(pf->indio_dev->info->driver_module);
  188. }
  189. return ret;
  190. }
  191. static int iio_trigger_detach_poll_func(struct iio_trigger *trig,
  192. struct iio_poll_func *pf)
  193. {
  194. int ret = 0;
  195. bool no_other_users
  196. = (bitmap_weight(trig->pool,
  197. CONFIG_IIO_CONSUMERS_PER_TRIGGER)
  198. == 1);
  199. if (trig->ops && trig->ops->set_trigger_state && no_other_users) {
  200. ret = trig->ops->set_trigger_state(trig, false);
  201. if (ret)
  202. goto error_ret;
  203. }
  204. iio_trigger_put_irq(trig, pf->irq);
  205. free_irq(pf->irq, pf);
  206. module_put(pf->indio_dev->info->driver_module);
  207. error_ret:
  208. return ret;
  209. }
  210. irqreturn_t iio_pollfunc_store_time(int irq, void *p)
  211. {
  212. struct iio_poll_func *pf = p;
  213. pf->timestamp = iio_get_time_ns();
  214. return IRQ_WAKE_THREAD;
  215. }
  216. EXPORT_SYMBOL(iio_pollfunc_store_time);
  217. struct iio_poll_func
  218. *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
  219. irqreturn_t (*thread)(int irq, void *p),
  220. int type,
  221. struct iio_dev *indio_dev,
  222. const char *fmt,
  223. ...)
  224. {
  225. va_list vargs;
  226. struct iio_poll_func *pf;
  227. pf = kmalloc(sizeof *pf, GFP_KERNEL);
  228. if (pf == NULL)
  229. return NULL;
  230. va_start(vargs, fmt);
  231. pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
  232. va_end(vargs);
  233. if (pf->name == NULL) {
  234. kfree(pf);
  235. return NULL;
  236. }
  237. pf->h = h;
  238. pf->thread = thread;
  239. pf->type = type;
  240. pf->indio_dev = indio_dev;
  241. return pf;
  242. }
  243. EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
  244. void iio_dealloc_pollfunc(struct iio_poll_func *pf)
  245. {
  246. kfree(pf->name);
  247. kfree(pf);
  248. }
  249. EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
  250. /**
  251. * iio_trigger_read_current() - trigger consumer sysfs query current trigger
  252. *
  253. * For trigger consumers the current_trigger interface allows the trigger
  254. * used by the device to be queried.
  255. **/
  256. static ssize_t iio_trigger_read_current(struct device *dev,
  257. struct device_attribute *attr,
  258. char *buf)
  259. {
  260. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  261. if (indio_dev->trig)
  262. return sprintf(buf, "%s\n", indio_dev->trig->name);
  263. return 0;
  264. }
  265. /**
  266. * iio_trigger_write_current() - trigger consumer sysfs set current trigger
  267. *
  268. * For trigger consumers the current_trigger interface allows the trigger
  269. * used for this device to be specified at run time based on the triggers
  270. * name.
  271. **/
  272. static ssize_t iio_trigger_write_current(struct device *dev,
  273. struct device_attribute *attr,
  274. const char *buf,
  275. size_t len)
  276. {
  277. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  278. struct iio_trigger *oldtrig = indio_dev->trig;
  279. struct iio_trigger *trig;
  280. int ret;
  281. mutex_lock(&indio_dev->mlock);
  282. if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
  283. mutex_unlock(&indio_dev->mlock);
  284. return -EBUSY;
  285. }
  286. mutex_unlock(&indio_dev->mlock);
  287. trig = iio_trigger_find_by_name(buf, len);
  288. if (oldtrig == trig)
  289. return len;
  290. if (trig && indio_dev->info->validate_trigger) {
  291. ret = indio_dev->info->validate_trigger(indio_dev, trig);
  292. if (ret)
  293. return ret;
  294. }
  295. if (trig && trig->ops && trig->ops->validate_device) {
  296. ret = trig->ops->validate_device(trig, indio_dev);
  297. if (ret)
  298. return ret;
  299. }
  300. indio_dev->trig = trig;
  301. if (oldtrig && indio_dev->trig != oldtrig)
  302. iio_trigger_put(oldtrig);
  303. if (indio_dev->trig)
  304. iio_trigger_get(indio_dev->trig);
  305. return len;
  306. }
  307. static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
  308. iio_trigger_read_current,
  309. iio_trigger_write_current);
  310. static struct attribute *iio_trigger_consumer_attrs[] = {
  311. &dev_attr_current_trigger.attr,
  312. NULL,
  313. };
  314. static const struct attribute_group iio_trigger_consumer_attr_group = {
  315. .name = "trigger",
  316. .attrs = iio_trigger_consumer_attrs,
  317. };
  318. static void iio_trig_release(struct device *device)
  319. {
  320. struct iio_trigger *trig = to_iio_trigger(device);
  321. int i;
  322. if (trig->subirq_base) {
  323. for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
  324. irq_modify_status(trig->subirq_base + i,
  325. IRQ_NOAUTOEN,
  326. IRQ_NOREQUEST | IRQ_NOPROBE);
  327. irq_set_chip(trig->subirq_base + i,
  328. NULL);
  329. irq_set_handler(trig->subirq_base + i,
  330. NULL);
  331. }
  332. irq_free_descs(trig->subirq_base,
  333. CONFIG_IIO_CONSUMERS_PER_TRIGGER);
  334. }
  335. kfree(trig->name);
  336. kfree(trig);
  337. }
  338. static struct device_type iio_trig_type = {
  339. .release = iio_trig_release,
  340. .groups = iio_trig_attr_groups,
  341. };
  342. static void iio_trig_subirqmask(struct irq_data *d)
  343. {
  344. struct irq_chip *chip = irq_data_get_irq_chip(d);
  345. struct iio_trigger *trig
  346. = container_of(chip,
  347. struct iio_trigger, subirq_chip);
  348. trig->subirqs[d->irq - trig->subirq_base].enabled = false;
  349. }
  350. static void iio_trig_subirqunmask(struct irq_data *d)
  351. {
  352. struct irq_chip *chip = irq_data_get_irq_chip(d);
  353. struct iio_trigger *trig
  354. = container_of(chip,
  355. struct iio_trigger, subirq_chip);
  356. trig->subirqs[d->irq - trig->subirq_base].enabled = true;
  357. }
  358. struct iio_trigger *iio_trigger_alloc(const char *fmt, ...)
  359. {
  360. va_list vargs;
  361. struct iio_trigger *trig;
  362. trig = kzalloc(sizeof *trig, GFP_KERNEL);
  363. if (trig) {
  364. int i;
  365. trig->dev.type = &iio_trig_type;
  366. trig->dev.bus = &iio_bus_type;
  367. device_initialize(&trig->dev);
  368. mutex_init(&trig->pool_lock);
  369. trig->subirq_base
  370. = irq_alloc_descs(-1, 0,
  371. CONFIG_IIO_CONSUMERS_PER_TRIGGER,
  372. 0);
  373. if (trig->subirq_base < 0) {
  374. kfree(trig);
  375. return NULL;
  376. }
  377. va_start(vargs, fmt);
  378. trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
  379. va_end(vargs);
  380. if (trig->name == NULL) {
  381. irq_free_descs(trig->subirq_base,
  382. CONFIG_IIO_CONSUMERS_PER_TRIGGER);
  383. kfree(trig);
  384. return NULL;
  385. }
  386. trig->subirq_chip.name = trig->name;
  387. trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
  388. trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
  389. for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
  390. irq_set_chip(trig->subirq_base + i,
  391. &trig->subirq_chip);
  392. irq_set_handler(trig->subirq_base + i,
  393. &handle_simple_irq);
  394. irq_modify_status(trig->subirq_base + i,
  395. IRQ_NOREQUEST | IRQ_NOAUTOEN,
  396. IRQ_NOPROBE);
  397. }
  398. get_device(&trig->dev);
  399. }
  400. return trig;
  401. }
  402. EXPORT_SYMBOL(iio_trigger_alloc);
  403. void iio_trigger_free(struct iio_trigger *trig)
  404. {
  405. if (trig)
  406. put_device(&trig->dev);
  407. }
  408. EXPORT_SYMBOL(iio_trigger_free);
  409. void iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
  410. {
  411. indio_dev->groups[indio_dev->groupcounter++] =
  412. &iio_trigger_consumer_attr_group;
  413. }
  414. void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
  415. {
  416. /* Clean up an associated but not attached trigger reference */
  417. if (indio_dev->trig)
  418. iio_trigger_put(indio_dev->trig);
  419. }
  420. int iio_triggered_buffer_postenable(struct iio_dev *indio_dev)
  421. {
  422. return iio_trigger_attach_poll_func(indio_dev->trig,
  423. indio_dev->pollfunc);
  424. }
  425. EXPORT_SYMBOL(iio_triggered_buffer_postenable);
  426. int iio_triggered_buffer_predisable(struct iio_dev *indio_dev)
  427. {
  428. return iio_trigger_detach_poll_func(indio_dev->trig,
  429. indio_dev->pollfunc);
  430. }
  431. EXPORT_SYMBOL(iio_triggered_buffer_predisable);