/drivers/staging/iio/industrialio-trigger.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2 · C · 507 lines · 384 code · 62 blank · 61 comment · 45 complexity · 22f4317727a5729e18d03acbd1bb9a19 MD5 · raw file

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