industrialio-trigger.c 16 KB

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