industrialio-trigger.c 14 KB

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