industrialio-event.c 11 KB

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