industrialio-event.c 11 KB

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