industrialio-trigger.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. pf->irq = iio_trigger_get_irq(trig);
  181. ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
  182. pf->type, pf->name,
  183. pf);
  184. if (ret < 0) {
  185. module_put(pf->indio_dev->info->driver_module);
  186. return ret;
  187. }
  188. if (trig->ops && trig->ops->set_trigger_state && notinuse) {
  189. ret = trig->ops->set_trigger_state(trig, true);
  190. if (ret < 0)
  191. module_put(pf->indio_dev->info->driver_module);
  192. }
  193. return ret;
  194. }
  195. static int iio_trigger_detach_poll_func(struct iio_trigger *trig,
  196. struct iio_poll_func *pf)
  197. {
  198. int ret = 0;
  199. bool no_other_users
  200. = (bitmap_weight(trig->pool,
  201. CONFIG_IIO_CONSUMERS_PER_TRIGGER)
  202. == 1);
  203. if (trig->ops && trig->ops->set_trigger_state && no_other_users) {
  204. ret = trig->ops->set_trigger_state(trig, false);
  205. if (ret)
  206. return ret;
  207. }
  208. iio_trigger_put_irq(trig, pf->irq);
  209. free_irq(pf->irq, pf);
  210. module_put(pf->indio_dev->info->driver_module);
  211. return ret;
  212. }
  213. irqreturn_t iio_pollfunc_store_time(int irq, void *p)
  214. {
  215. struct iio_poll_func *pf = p;
  216. pf->timestamp = iio_get_time_ns();
  217. return IRQ_WAKE_THREAD;
  218. }
  219. EXPORT_SYMBOL(iio_pollfunc_store_time);
  220. struct iio_poll_func
  221. *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
  222. irqreturn_t (*thread)(int irq, void *p),
  223. int type,
  224. struct iio_dev *indio_dev,
  225. const char *fmt,
  226. ...)
  227. {
  228. va_list vargs;
  229. struct iio_poll_func *pf;
  230. pf = kmalloc(sizeof *pf, GFP_KERNEL);
  231. if (pf == NULL)
  232. return NULL;
  233. va_start(vargs, fmt);
  234. pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
  235. va_end(vargs);
  236. if (pf->name == NULL) {
  237. kfree(pf);
  238. return NULL;
  239. }
  240. pf->h = h;
  241. pf->thread = thread;
  242. pf->type = type;
  243. pf->indio_dev = indio_dev;
  244. return pf;
  245. }
  246. EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
  247. void iio_dealloc_pollfunc(struct iio_poll_func *pf)
  248. {
  249. kfree(pf->name);
  250. kfree(pf);
  251. }
  252. EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
  253. /**
  254. * iio_trigger_read_current() - trigger consumer sysfs query current trigger
  255. * @dev: device associated with an industrial I/O device
  256. * @attr: pointer to the device_attribute structure that
  257. * is being processed
  258. * @buf: buffer where the current trigger name will be printed into
  259. *
  260. * For trigger consumers the current_trigger interface allows the trigger
  261. * used by the device to be queried.
  262. *
  263. * Return: a negative number on failure, the number of characters written
  264. * on success or 0 if no trigger is available
  265. */
  266. static ssize_t iio_trigger_read_current(struct device *dev,
  267. struct device_attribute *attr,
  268. char *buf)
  269. {
  270. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  271. if (indio_dev->trig)
  272. return sprintf(buf, "%s\n", indio_dev->trig->name);
  273. return 0;
  274. }
  275. /**
  276. * iio_trigger_write_current() - trigger consumer sysfs set current trigger
  277. * @dev: device associated with an industrial I/O device
  278. * @attr: device attribute that is being processed
  279. * @buf: string buffer that holds the name of the trigger
  280. * @len: length of the trigger name held by buf
  281. *
  282. * For trigger consumers the current_trigger interface allows the trigger
  283. * used for this device to be specified at run time based on the trigger's
  284. * name.
  285. *
  286. * Return: negative error code on failure or length of the buffer
  287. * on success
  288. */
  289. static ssize_t iio_trigger_write_current(struct device *dev,
  290. struct device_attribute *attr,
  291. const char *buf,
  292. size_t len)
  293. {
  294. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  295. struct iio_trigger *oldtrig = indio_dev->trig;
  296. struct iio_trigger *trig;
  297. int ret;
  298. mutex_lock(&indio_dev->mlock);
  299. if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
  300. mutex_unlock(&indio_dev->mlock);
  301. return -EBUSY;
  302. }
  303. mutex_unlock(&indio_dev->mlock);
  304. trig = iio_trigger_find_by_name(buf, len);
  305. if (oldtrig == trig)
  306. return len;
  307. if (trig && indio_dev->info->validate_trigger) {
  308. ret = indio_dev->info->validate_trigger(indio_dev, trig);
  309. if (ret)
  310. return ret;
  311. }
  312. if (trig && trig->ops && trig->ops->validate_device) {
  313. ret = trig->ops->validate_device(trig, indio_dev);
  314. if (ret)
  315. return ret;
  316. }
  317. indio_dev->trig = trig;
  318. if (oldtrig)
  319. iio_trigger_put(oldtrig);
  320. if (indio_dev->trig)
  321. iio_trigger_get(indio_dev->trig);
  322. return len;
  323. }
  324. static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
  325. iio_trigger_read_current,
  326. iio_trigger_write_current);
  327. static struct attribute *iio_trigger_consumer_attrs[] = {
  328. &dev_attr_current_trigger.attr,
  329. NULL,
  330. };
  331. static const struct attribute_group iio_trigger_consumer_attr_group = {
  332. .name = "trigger",
  333. .attrs = iio_trigger_consumer_attrs,
  334. };
  335. static void iio_trig_release(struct device *device)
  336. {
  337. struct iio_trigger *trig = to_iio_trigger(device);
  338. int i;
  339. if (trig->subirq_base) {
  340. for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
  341. irq_modify_status(trig->subirq_base + i,
  342. IRQ_NOAUTOEN,
  343. IRQ_NOREQUEST | IRQ_NOPROBE);
  344. irq_set_chip(trig->subirq_base + i,
  345. NULL);
  346. irq_set_handler(trig->subirq_base + i,
  347. NULL);
  348. }
  349. irq_free_descs(trig->subirq_base,
  350. CONFIG_IIO_CONSUMERS_PER_TRIGGER);
  351. }
  352. kfree(trig->name);
  353. kfree(trig);
  354. }
  355. static struct device_type iio_trig_type = {
  356. .release = iio_trig_release,
  357. .groups = iio_trig_dev_groups,
  358. };
  359. static void iio_trig_subirqmask(struct irq_data *d)
  360. {
  361. struct irq_chip *chip = irq_data_get_irq_chip(d);
  362. struct iio_trigger *trig
  363. = container_of(chip,
  364. struct iio_trigger, subirq_chip);
  365. trig->subirqs[d->irq - trig->subirq_base].enabled = false;
  366. }
  367. static void iio_trig_subirqunmask(struct irq_data *d)
  368. {
  369. struct irq_chip *chip = irq_data_get_irq_chip(d);
  370. struct iio_trigger *trig
  371. = container_of(chip,
  372. struct iio_trigger, subirq_chip);
  373. trig->subirqs[d->irq - trig->subirq_base].enabled = true;
  374. }
  375. static struct iio_trigger *viio_trigger_alloc(const char *fmt, va_list vargs)
  376. {
  377. struct iio_trigger *trig;
  378. trig = kzalloc(sizeof *trig, GFP_KERNEL);
  379. if (trig) {
  380. int i;
  381. trig->dev.type = &iio_trig_type;
  382. trig->dev.bus = &iio_bus_type;
  383. device_initialize(&trig->dev);
  384. mutex_init(&trig->pool_lock);
  385. trig->subirq_base
  386. = irq_alloc_descs(-1, 0,
  387. CONFIG_IIO_CONSUMERS_PER_TRIGGER,
  388. 0);
  389. if (trig->subirq_base < 0) {
  390. kfree(trig);
  391. return NULL;
  392. }
  393. trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
  394. if (trig->name == NULL) {
  395. irq_free_descs(trig->subirq_base,
  396. CONFIG_IIO_CONSUMERS_PER_TRIGGER);
  397. kfree(trig);
  398. return NULL;
  399. }
  400. trig->subirq_chip.name = trig->name;
  401. trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
  402. trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
  403. for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
  404. irq_set_chip(trig->subirq_base + i,
  405. &trig->subirq_chip);
  406. irq_set_handler(trig->subirq_base + i,
  407. &handle_simple_irq);
  408. irq_modify_status(trig->subirq_base + i,
  409. IRQ_NOREQUEST | IRQ_NOAUTOEN,
  410. IRQ_NOPROBE);
  411. }
  412. get_device(&trig->dev);
  413. }
  414. return trig;
  415. }
  416. struct iio_trigger *iio_trigger_alloc(const char *fmt, ...)
  417. {
  418. struct iio_trigger *trig;
  419. va_list vargs;
  420. va_start(vargs, fmt);
  421. trig = viio_trigger_alloc(fmt, vargs);
  422. va_end(vargs);
  423. return trig;
  424. }
  425. EXPORT_SYMBOL(iio_trigger_alloc);
  426. void iio_trigger_free(struct iio_trigger *trig)
  427. {
  428. if (trig)
  429. put_device(&trig->dev);
  430. }
  431. EXPORT_SYMBOL(iio_trigger_free);
  432. static void devm_iio_trigger_release(struct device *dev, void *res)
  433. {
  434. iio_trigger_free(*(struct iio_trigger **)res);
  435. }
  436. static int devm_iio_trigger_match(struct device *dev, void *res, void *data)
  437. {
  438. struct iio_trigger **r = res;
  439. if (!r || !*r) {
  440. WARN_ON(!r || !*r);
  441. return 0;
  442. }
  443. return *r == data;
  444. }
  445. /**
  446. * devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc()
  447. * @dev: Device to allocate iio_trigger for
  448. * @fmt: trigger name format. If it includes format
  449. * specifiers, the additional arguments following
  450. * format are formatted and inserted in the resulting
  451. * string replacing their respective specifiers.
  452. *
  453. * Managed iio_trigger_alloc. iio_trigger allocated with this function is
  454. * automatically freed on driver detach.
  455. *
  456. * If an iio_trigger allocated with this function needs to be freed separately,
  457. * devm_iio_trigger_free() must be used.
  458. *
  459. * RETURNS:
  460. * Pointer to allocated iio_trigger on success, NULL on failure.
  461. */
  462. struct iio_trigger *devm_iio_trigger_alloc(struct device *dev,
  463. const char *fmt, ...)
  464. {
  465. struct iio_trigger **ptr, *trig;
  466. va_list vargs;
  467. ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
  468. GFP_KERNEL);
  469. if (!ptr)
  470. return NULL;
  471. /* use raw alloc_dr for kmalloc caller tracing */
  472. va_start(vargs, fmt);
  473. trig = viio_trigger_alloc(fmt, vargs);
  474. va_end(vargs);
  475. if (trig) {
  476. *ptr = trig;
  477. devres_add(dev, ptr);
  478. } else {
  479. devres_free(ptr);
  480. }
  481. return trig;
  482. }
  483. EXPORT_SYMBOL_GPL(devm_iio_trigger_alloc);
  484. /**
  485. * devm_iio_trigger_free - Resource-managed iio_trigger_free()
  486. * @dev: Device this iio_dev belongs to
  487. * @iio_trig: the iio_trigger associated with the device
  488. *
  489. * Free iio_trigger allocated with devm_iio_trigger_alloc().
  490. */
  491. void devm_iio_trigger_free(struct device *dev, struct iio_trigger *iio_trig)
  492. {
  493. int rc;
  494. rc = devres_release(dev, devm_iio_trigger_release,
  495. devm_iio_trigger_match, iio_trig);
  496. WARN_ON(rc);
  497. }
  498. EXPORT_SYMBOL_GPL(devm_iio_trigger_free);
  499. void iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
  500. {
  501. indio_dev->groups[indio_dev->groupcounter++] =
  502. &iio_trigger_consumer_attr_group;
  503. }
  504. void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
  505. {
  506. /* Clean up an associated but not attached trigger reference */
  507. if (indio_dev->trig)
  508. iio_trigger_put(indio_dev->trig);
  509. }
  510. int iio_triggered_buffer_postenable(struct iio_dev *indio_dev)
  511. {
  512. return iio_trigger_attach_poll_func(indio_dev->trig,
  513. indio_dev->pollfunc);
  514. }
  515. EXPORT_SYMBOL(iio_triggered_buffer_postenable);
  516. int iio_triggered_buffer_predisable(struct iio_dev *indio_dev)
  517. {
  518. return iio_trigger_detach_poll_func(indio_dev->trig,
  519. indio_dev->pollfunc);
  520. }
  521. EXPORT_SYMBOL(iio_triggered_buffer_predisable);