industrialio-event.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* Industrial I/O event handling
  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. * Based on elements of hwmon and input subsystems.
  10. */
  11. #include <linux/anon_inodes.h>
  12. #include <linux/device.h>
  13. #include <linux/fs.h>
  14. #include <linux/kernel.h>
  15. #include <linux/kfifo.h>
  16. #include <linux/module.h>
  17. #include <linux/poll.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/wait.h>
  22. #include <linux/iio/iio.h>
  23. #include "iio_core.h"
  24. #include <linux/iio/sysfs.h>
  25. #include <linux/iio/events.h>
  26. /**
  27. * struct iio_event_interface - chrdev interface for an event line
  28. * @wait: wait queue to allow blocking reads of events
  29. * @det_events: list of detected events
  30. * @dev_attr_list: list of event interface sysfs attribute
  31. * @flags: file operations related flags including busy flag.
  32. * @group: event interface sysfs attribute group
  33. */
  34. struct iio_event_interface {
  35. wait_queue_head_t wait;
  36. DECLARE_KFIFO(det_events, struct iio_event_data, 16);
  37. struct list_head dev_attr_list;
  38. unsigned long flags;
  39. struct attribute_group group;
  40. };
  41. int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp)
  42. {
  43. struct iio_event_interface *ev_int = indio_dev->event_interface;
  44. struct iio_event_data ev;
  45. unsigned long flags;
  46. int copied;
  47. /* Does anyone care? */
  48. spin_lock_irqsave(&ev_int->wait.lock, flags);
  49. if (test_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
  50. ev.id = ev_code;
  51. ev.timestamp = timestamp;
  52. copied = kfifo_put(&ev_int->det_events, &ev);
  53. if (copied != 0)
  54. wake_up_locked_poll(&ev_int->wait, POLLIN);
  55. }
  56. spin_unlock_irqrestore(&ev_int->wait.lock, flags);
  57. return 0;
  58. }
  59. EXPORT_SYMBOL(iio_push_event);
  60. /**
  61. * iio_event_poll() - poll the event queue to find out if it has data
  62. */
  63. static unsigned int iio_event_poll(struct file *filep,
  64. struct poll_table_struct *wait)
  65. {
  66. struct iio_dev *indio_dev = filep->private_data;
  67. struct iio_event_interface *ev_int = indio_dev->event_interface;
  68. unsigned int events = 0;
  69. poll_wait(filep, &ev_int->wait, wait);
  70. spin_lock_irq(&ev_int->wait.lock);
  71. if (!kfifo_is_empty(&ev_int->det_events))
  72. events = POLLIN | POLLRDNORM;
  73. spin_unlock_irq(&ev_int->wait.lock);
  74. return events;
  75. }
  76. static ssize_t iio_event_chrdev_read(struct file *filep,
  77. char __user *buf,
  78. size_t count,
  79. loff_t *f_ps)
  80. {
  81. struct iio_dev *indio_dev = filep->private_data;
  82. struct iio_event_interface *ev_int = indio_dev->event_interface;
  83. unsigned int copied;
  84. int ret;
  85. if (count < sizeof(struct iio_event_data))
  86. return -EINVAL;
  87. spin_lock_irq(&ev_int->wait.lock);
  88. if (kfifo_is_empty(&ev_int->det_events)) {
  89. if (filep->f_flags & O_NONBLOCK) {
  90. ret = -EAGAIN;
  91. goto error_unlock;
  92. }
  93. /* Blocking on device; waiting for something to be there */
  94. ret = wait_event_interruptible_locked_irq(ev_int->wait,
  95. !kfifo_is_empty(&ev_int->det_events));
  96. if (ret)
  97. goto error_unlock;
  98. /* Single access device so no one else can get the data */
  99. }
  100. ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied);
  101. error_unlock:
  102. spin_unlock_irq(&ev_int->wait.lock);
  103. return ret ? ret : copied;
  104. }
  105. static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
  106. {
  107. struct iio_dev *indio_dev = filep->private_data;
  108. struct iio_event_interface *ev_int = indio_dev->event_interface;
  109. spin_lock_irq(&ev_int->wait.lock);
  110. __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
  111. /*
  112. * In order to maintain a clean state for reopening,
  113. * clear out any awaiting events. The mask will prevent
  114. * any new __iio_push_event calls running.
  115. */
  116. kfifo_reset_out(&ev_int->det_events);
  117. spin_unlock_irq(&ev_int->wait.lock);
  118. iio_device_put(indio_dev);
  119. return 0;
  120. }
  121. static const struct file_operations iio_event_chrdev_fileops = {
  122. .read = iio_event_chrdev_read,
  123. .poll = iio_event_poll,
  124. .release = iio_event_chrdev_release,
  125. .owner = THIS_MODULE,
  126. .llseek = noop_llseek,
  127. };
  128. int iio_event_getfd(struct iio_dev *indio_dev)
  129. {
  130. struct iio_event_interface *ev_int = indio_dev->event_interface;
  131. int fd;
  132. if (ev_int == NULL)
  133. return -ENODEV;
  134. spin_lock_irq(&ev_int->wait.lock);
  135. if (__test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
  136. spin_unlock_irq(&ev_int->wait.lock);
  137. return -EBUSY;
  138. }
  139. spin_unlock_irq(&ev_int->wait.lock);
  140. iio_device_get(indio_dev);
  141. fd = anon_inode_getfd("iio:event", &iio_event_chrdev_fileops,
  142. indio_dev, O_RDONLY);
  143. if (fd < 0) {
  144. spin_lock_irq(&ev_int->wait.lock);
  145. __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
  146. spin_unlock_irq(&ev_int->wait.lock);
  147. iio_device_put(indio_dev);
  148. }
  149. return fd;
  150. }
  151. static const char * const iio_ev_type_text[] = {
  152. [IIO_EV_TYPE_THRESH] = "thresh",
  153. [IIO_EV_TYPE_MAG] = "mag",
  154. [IIO_EV_TYPE_ROC] = "roc",
  155. [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
  156. [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
  157. };
  158. static const char * const iio_ev_dir_text[] = {
  159. [IIO_EV_DIR_EITHER] = "either",
  160. [IIO_EV_DIR_RISING] = "rising",
  161. [IIO_EV_DIR_FALLING] = "falling"
  162. };
  163. static ssize_t iio_ev_state_store(struct device *dev,
  164. struct device_attribute *attr,
  165. const char *buf,
  166. size_t len)
  167. {
  168. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  169. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  170. int ret;
  171. bool val;
  172. ret = strtobool(buf, &val);
  173. if (ret < 0)
  174. return ret;
  175. ret = indio_dev->info->write_event_config(indio_dev,
  176. this_attr->address,
  177. val);
  178. return (ret < 0) ? ret : len;
  179. }
  180. static ssize_t iio_ev_state_show(struct device *dev,
  181. struct device_attribute *attr,
  182. char *buf)
  183. {
  184. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  185. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  186. int val = indio_dev->info->read_event_config(indio_dev,
  187. this_attr->address);
  188. if (val < 0)
  189. return val;
  190. else
  191. return sprintf(buf, "%d\n", val);
  192. }
  193. static ssize_t iio_ev_value_show(struct device *dev,
  194. struct device_attribute *attr,
  195. char *buf)
  196. {
  197. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  198. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  199. int val, ret;
  200. ret = indio_dev->info->read_event_value(indio_dev,
  201. this_attr->address, &val);
  202. if (ret < 0)
  203. return ret;
  204. return sprintf(buf, "%d\n", val);
  205. }
  206. static ssize_t iio_ev_value_store(struct device *dev,
  207. struct device_attribute *attr,
  208. const char *buf,
  209. size_t len)
  210. {
  211. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  212. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  213. int val;
  214. int ret;
  215. if (!indio_dev->info->write_event_value)
  216. return -EINVAL;
  217. ret = kstrtoint(buf, 10, &val);
  218. if (ret)
  219. return ret;
  220. ret = indio_dev->info->write_event_value(indio_dev, this_attr->address,
  221. val);
  222. if (ret < 0)
  223. return ret;
  224. return len;
  225. }
  226. static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
  227. struct iio_chan_spec const *chan)
  228. {
  229. int ret = 0, i, attrcount = 0;
  230. u64 mask = 0;
  231. char *postfix;
  232. if (!chan->event_mask)
  233. return 0;
  234. for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) {
  235. postfix = kasprintf(GFP_KERNEL, "%s_%s_en",
  236. iio_ev_type_text[i/IIO_EV_DIR_MAX],
  237. iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
  238. if (postfix == NULL) {
  239. ret = -ENOMEM;
  240. goto error_ret;
  241. }
  242. if (chan->modified)
  243. mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel2,
  244. i/IIO_EV_DIR_MAX,
  245. i%IIO_EV_DIR_MAX);
  246. else if (chan->differential)
  247. mask = IIO_EVENT_CODE(chan->type,
  248. 0, 0,
  249. i%IIO_EV_DIR_MAX,
  250. i/IIO_EV_DIR_MAX,
  251. 0,
  252. chan->channel,
  253. chan->channel2);
  254. else
  255. mask = IIO_UNMOD_EVENT_CODE(chan->type,
  256. chan->channel,
  257. i/IIO_EV_DIR_MAX,
  258. i%IIO_EV_DIR_MAX);
  259. ret = __iio_add_chan_devattr(postfix,
  260. chan,
  261. &iio_ev_state_show,
  262. iio_ev_state_store,
  263. mask,
  264. 0,
  265. &indio_dev->dev,
  266. &indio_dev->event_interface->
  267. dev_attr_list);
  268. kfree(postfix);
  269. if (ret)
  270. goto error_ret;
  271. attrcount++;
  272. postfix = kasprintf(GFP_KERNEL, "%s_%s_value",
  273. iio_ev_type_text[i/IIO_EV_DIR_MAX],
  274. iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
  275. if (postfix == NULL) {
  276. ret = -ENOMEM;
  277. goto error_ret;
  278. }
  279. ret = __iio_add_chan_devattr(postfix, chan,
  280. iio_ev_value_show,
  281. iio_ev_value_store,
  282. mask,
  283. 0,
  284. &indio_dev->dev,
  285. &indio_dev->event_interface->
  286. dev_attr_list);
  287. kfree(postfix);
  288. if (ret)
  289. goto error_ret;
  290. attrcount++;
  291. }
  292. ret = attrcount;
  293. error_ret:
  294. return ret;
  295. }
  296. static inline void __iio_remove_event_config_attrs(struct iio_dev *indio_dev)
  297. {
  298. struct iio_dev_attr *p, *n;
  299. list_for_each_entry_safe(p, n,
  300. &indio_dev->event_interface->
  301. dev_attr_list, l) {
  302. kfree(p->dev_attr.attr.name);
  303. kfree(p);
  304. }
  305. }
  306. static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
  307. {
  308. int j, ret, attrcount = 0;
  309. /* Dynically created from the channels array */
  310. for (j = 0; j < indio_dev->num_channels; j++) {
  311. ret = iio_device_add_event_sysfs(indio_dev,
  312. &indio_dev->channels[j]);
  313. if (ret < 0)
  314. return ret;
  315. attrcount += ret;
  316. }
  317. return attrcount;
  318. }
  319. static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
  320. {
  321. int j;
  322. for (j = 0; j < indio_dev->num_channels; j++)
  323. if (indio_dev->channels[j].event_mask != 0)
  324. return true;
  325. return false;
  326. }
  327. static void iio_setup_ev_int(struct iio_event_interface *ev_int)
  328. {
  329. INIT_KFIFO(ev_int->det_events);
  330. init_waitqueue_head(&ev_int->wait);
  331. }
  332. static const char *iio_event_group_name = "events";
  333. int iio_device_register_eventset(struct iio_dev *indio_dev)
  334. {
  335. struct iio_dev_attr *p;
  336. int ret = 0, attrcount_orig = 0, attrcount, attrn;
  337. struct attribute **attr;
  338. if (!(indio_dev->info->event_attrs ||
  339. iio_check_for_dynamic_events(indio_dev)))
  340. return 0;
  341. indio_dev->event_interface =
  342. kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL);
  343. if (indio_dev->event_interface == NULL) {
  344. ret = -ENOMEM;
  345. goto error_ret;
  346. }
  347. INIT_LIST_HEAD(&indio_dev->event_interface->dev_attr_list);
  348. iio_setup_ev_int(indio_dev->event_interface);
  349. if (indio_dev->info->event_attrs != NULL) {
  350. attr = indio_dev->info->event_attrs->attrs;
  351. while (*attr++ != NULL)
  352. attrcount_orig++;
  353. }
  354. attrcount = attrcount_orig;
  355. if (indio_dev->channels) {
  356. ret = __iio_add_event_config_attrs(indio_dev);
  357. if (ret < 0)
  358. goto error_free_setup_event_lines;
  359. attrcount += ret;
  360. }
  361. indio_dev->event_interface->group.name = iio_event_group_name;
  362. indio_dev->event_interface->group.attrs = kcalloc(attrcount + 1,
  363. sizeof(indio_dev->event_interface->group.attrs[0]),
  364. GFP_KERNEL);
  365. if (indio_dev->event_interface->group.attrs == NULL) {
  366. ret = -ENOMEM;
  367. goto error_free_setup_event_lines;
  368. }
  369. if (indio_dev->info->event_attrs)
  370. memcpy(indio_dev->event_interface->group.attrs,
  371. indio_dev->info->event_attrs->attrs,
  372. sizeof(indio_dev->event_interface->group.attrs[0])
  373. *attrcount_orig);
  374. attrn = attrcount_orig;
  375. /* Add all elements from the list. */
  376. list_for_each_entry(p,
  377. &indio_dev->event_interface->dev_attr_list,
  378. l)
  379. indio_dev->event_interface->group.attrs[attrn++] =
  380. &p->dev_attr.attr;
  381. indio_dev->groups[indio_dev->groupcounter++] =
  382. &indio_dev->event_interface->group;
  383. return 0;
  384. error_free_setup_event_lines:
  385. __iio_remove_event_config_attrs(indio_dev);
  386. kfree(indio_dev->event_interface);
  387. error_ret:
  388. return ret;
  389. }
  390. void iio_device_unregister_eventset(struct iio_dev *indio_dev)
  391. {
  392. if (indio_dev->event_interface == NULL)
  393. return;
  394. __iio_remove_event_config_attrs(indio_dev);
  395. kfree(indio_dev->event_interface->group.attrs);
  396. kfree(indio_dev->event_interface);
  397. }