industrialio-trigger.c 13 KB

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