industrialio-event.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. struct mutex read_lock;
  41. };
  42. /**
  43. * iio_push_event() - try to add event to the list for userspace reading
  44. * @indio_dev: IIO device structure
  45. * @ev_code: What event
  46. * @timestamp: When the event occurred
  47. *
  48. * Note: The caller must make sure that this function is not running
  49. * concurrently for the same indio_dev more than once.
  50. **/
  51. int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp)
  52. {
  53. struct iio_event_interface *ev_int = indio_dev->event_interface;
  54. struct iio_event_data ev;
  55. int copied;
  56. /* Does anyone care? */
  57. if (test_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
  58. ev.id = ev_code;
  59. ev.timestamp = timestamp;
  60. copied = kfifo_put(&ev_int->det_events, ev);
  61. if (copied != 0)
  62. wake_up_poll(&ev_int->wait, POLLIN);
  63. }
  64. return 0;
  65. }
  66. EXPORT_SYMBOL(iio_push_event);
  67. /**
  68. * iio_event_poll() - poll the event queue to find out if it has data
  69. */
  70. static unsigned int iio_event_poll(struct file *filep,
  71. struct poll_table_struct *wait)
  72. {
  73. struct iio_dev *indio_dev = filep->private_data;
  74. struct iio_event_interface *ev_int = indio_dev->event_interface;
  75. unsigned int events = 0;
  76. if (!indio_dev->info)
  77. return -ENODEV;
  78. poll_wait(filep, &ev_int->wait, wait);
  79. if (!kfifo_is_empty(&ev_int->det_events))
  80. events = POLLIN | POLLRDNORM;
  81. return events;
  82. }
  83. static ssize_t iio_event_chrdev_read(struct file *filep,
  84. char __user *buf,
  85. size_t count,
  86. loff_t *f_ps)
  87. {
  88. struct iio_dev *indio_dev = filep->private_data;
  89. struct iio_event_interface *ev_int = indio_dev->event_interface;
  90. unsigned int copied;
  91. int ret;
  92. if (!indio_dev->info)
  93. return -ENODEV;
  94. if (count < sizeof(struct iio_event_data))
  95. return -EINVAL;
  96. do {
  97. if (kfifo_is_empty(&ev_int->det_events)) {
  98. if (filep->f_flags & O_NONBLOCK)
  99. return -EAGAIN;
  100. ret = wait_event_interruptible(ev_int->wait,
  101. !kfifo_is_empty(&ev_int->det_events) ||
  102. indio_dev->info == NULL);
  103. if (ret)
  104. return ret;
  105. if (indio_dev->info == NULL)
  106. return -ENODEV;
  107. }
  108. if (mutex_lock_interruptible(&ev_int->read_lock))
  109. return -ERESTARTSYS;
  110. ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied);
  111. mutex_unlock(&ev_int->read_lock);
  112. if (ret)
  113. return ret;
  114. /*
  115. * If we couldn't read anything from the fifo (a different
  116. * thread might have been faster) we either return -EAGAIN if
  117. * the file descriptor is non-blocking, otherwise we go back to
  118. * sleep and wait for more data to arrive.
  119. */
  120. if (copied == 0 && (filep->f_flags & O_NONBLOCK))
  121. return -EAGAIN;
  122. } while (copied == 0);
  123. return copied;
  124. }
  125. static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
  126. {
  127. struct iio_dev *indio_dev = filep->private_data;
  128. struct iio_event_interface *ev_int = indio_dev->event_interface;
  129. clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
  130. iio_device_put(indio_dev);
  131. return 0;
  132. }
  133. static const struct file_operations iio_event_chrdev_fileops = {
  134. .read = iio_event_chrdev_read,
  135. .poll = iio_event_poll,
  136. .release = iio_event_chrdev_release,
  137. .owner = THIS_MODULE,
  138. .llseek = noop_llseek,
  139. };
  140. int iio_event_getfd(struct iio_dev *indio_dev)
  141. {
  142. struct iio_event_interface *ev_int = indio_dev->event_interface;
  143. int fd;
  144. if (ev_int == NULL)
  145. return -ENODEV;
  146. if (test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags))
  147. return -EBUSY;
  148. iio_device_get(indio_dev);
  149. fd = anon_inode_getfd("iio:event", &iio_event_chrdev_fileops,
  150. indio_dev, O_RDONLY | O_CLOEXEC);
  151. if (fd < 0) {
  152. clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
  153. iio_device_put(indio_dev);
  154. } else {
  155. kfifo_reset_out(&ev_int->det_events);
  156. }
  157. return fd;
  158. }
  159. static const char * const iio_ev_type_text[] = {
  160. [IIO_EV_TYPE_THRESH] = "thresh",
  161. [IIO_EV_TYPE_MAG] = "mag",
  162. [IIO_EV_TYPE_ROC] = "roc",
  163. [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
  164. [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
  165. [IIO_EV_TYPE_CHANGE] = "change",
  166. };
  167. static const char * const iio_ev_dir_text[] = {
  168. [IIO_EV_DIR_EITHER] = "either",
  169. [IIO_EV_DIR_RISING] = "rising",
  170. [IIO_EV_DIR_FALLING] = "falling"
  171. };
  172. static const char * const iio_ev_info_text[] = {
  173. [IIO_EV_INFO_ENABLE] = "en",
  174. [IIO_EV_INFO_VALUE] = "value",
  175. [IIO_EV_INFO_HYSTERESIS] = "hysteresis",
  176. [IIO_EV_INFO_PERIOD] = "period",
  177. };
  178. static enum iio_event_direction iio_ev_attr_dir(struct iio_dev_attr *attr)
  179. {
  180. return attr->c->event_spec[attr->address & 0xffff].dir;
  181. }
  182. static enum iio_event_type iio_ev_attr_type(struct iio_dev_attr *attr)
  183. {
  184. return attr->c->event_spec[attr->address & 0xffff].type;
  185. }
  186. static enum iio_event_info iio_ev_attr_info(struct iio_dev_attr *attr)
  187. {
  188. return (attr->address >> 16) & 0xffff;
  189. }
  190. static ssize_t iio_ev_state_store(struct device *dev,
  191. struct device_attribute *attr,
  192. const char *buf,
  193. size_t len)
  194. {
  195. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  196. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  197. int ret;
  198. bool val;
  199. ret = strtobool(buf, &val);
  200. if (ret < 0)
  201. return ret;
  202. ret = indio_dev->info->write_event_config(indio_dev,
  203. this_attr->c, iio_ev_attr_type(this_attr),
  204. iio_ev_attr_dir(this_attr), val);
  205. return (ret < 0) ? ret : len;
  206. }
  207. static ssize_t iio_ev_state_show(struct device *dev,
  208. struct device_attribute *attr,
  209. char *buf)
  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. val = indio_dev->info->read_event_config(indio_dev,
  215. this_attr->c, iio_ev_attr_type(this_attr),
  216. iio_ev_attr_dir(this_attr));
  217. if (val < 0)
  218. return val;
  219. else
  220. return sprintf(buf, "%d\n", val);
  221. }
  222. static ssize_t iio_ev_value_show(struct device *dev,
  223. struct device_attribute *attr,
  224. char *buf)
  225. {
  226. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  227. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  228. int val, val2, val_arr[2];
  229. int ret;
  230. ret = indio_dev->info->read_event_value(indio_dev,
  231. this_attr->c, iio_ev_attr_type(this_attr),
  232. iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
  233. &val, &val2);
  234. if (ret < 0)
  235. return ret;
  236. val_arr[0] = val;
  237. val_arr[1] = val2;
  238. return iio_format_value(buf, ret, 2, val_arr);
  239. }
  240. static ssize_t iio_ev_value_store(struct device *dev,
  241. struct device_attribute *attr,
  242. const char *buf,
  243. size_t len)
  244. {
  245. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  246. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  247. int val, val2;
  248. int ret;
  249. if (!indio_dev->info->write_event_value)
  250. return -EINVAL;
  251. ret = iio_str_to_fixpoint(buf, 100000, &val, &val2);
  252. if (ret)
  253. return ret;
  254. ret = indio_dev->info->write_event_value(indio_dev,
  255. this_attr->c, iio_ev_attr_type(this_attr),
  256. iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
  257. val, val2);
  258. if (ret < 0)
  259. return ret;
  260. return len;
  261. }
  262. static int iio_device_add_event(struct iio_dev *indio_dev,
  263. const struct iio_chan_spec *chan, unsigned int spec_index,
  264. enum iio_event_type type, enum iio_event_direction dir,
  265. enum iio_shared_by shared_by, const unsigned long *mask)
  266. {
  267. ssize_t (*show)(struct device *, struct device_attribute *, char *);
  268. ssize_t (*store)(struct device *, struct device_attribute *,
  269. const char *, size_t);
  270. unsigned int attrcount = 0;
  271. unsigned int i;
  272. char *postfix;
  273. int ret;
  274. for_each_set_bit(i, mask, sizeof(*mask)*8) {
  275. if (i >= ARRAY_SIZE(iio_ev_info_text))
  276. return -EINVAL;
  277. if (dir != IIO_EV_DIR_NONE)
  278. postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
  279. iio_ev_type_text[type],
  280. iio_ev_dir_text[dir],
  281. iio_ev_info_text[i]);
  282. else
  283. postfix = kasprintf(GFP_KERNEL, "%s_%s",
  284. iio_ev_type_text[type],
  285. iio_ev_info_text[i]);
  286. if (postfix == NULL)
  287. return -ENOMEM;
  288. if (i == IIO_EV_INFO_ENABLE) {
  289. show = iio_ev_state_show;
  290. store = iio_ev_state_store;
  291. } else {
  292. show = iio_ev_value_show;
  293. store = iio_ev_value_store;
  294. }
  295. ret = __iio_add_chan_devattr(postfix, chan, show, store,
  296. (i << 16) | spec_index, shared_by, &indio_dev->dev,
  297. &indio_dev->event_interface->dev_attr_list);
  298. kfree(postfix);
  299. if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
  300. continue;
  301. if (ret)
  302. return ret;
  303. attrcount++;
  304. }
  305. return attrcount;
  306. }
  307. static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
  308. struct iio_chan_spec const *chan)
  309. {
  310. int ret = 0, i, attrcount = 0;
  311. enum iio_event_direction dir;
  312. enum iio_event_type type;
  313. for (i = 0; i < chan->num_event_specs; i++) {
  314. type = chan->event_spec[i].type;
  315. dir = chan->event_spec[i].dir;
  316. ret = iio_device_add_event(indio_dev, chan, i, type, dir,
  317. IIO_SEPARATE, &chan->event_spec[i].mask_separate);
  318. if (ret < 0)
  319. return ret;
  320. attrcount += ret;
  321. ret = iio_device_add_event(indio_dev, chan, i, type, dir,
  322. IIO_SHARED_BY_TYPE,
  323. &chan->event_spec[i].mask_shared_by_type);
  324. if (ret < 0)
  325. return ret;
  326. attrcount += ret;
  327. ret = iio_device_add_event(indio_dev, chan, i, type, dir,
  328. IIO_SHARED_BY_DIR,
  329. &chan->event_spec[i].mask_shared_by_dir);
  330. if (ret < 0)
  331. return ret;
  332. attrcount += ret;
  333. ret = iio_device_add_event(indio_dev, chan, i, type, dir,
  334. IIO_SHARED_BY_ALL,
  335. &chan->event_spec[i].mask_shared_by_all);
  336. if (ret < 0)
  337. return ret;
  338. attrcount += ret;
  339. }
  340. ret = attrcount;
  341. return ret;
  342. }
  343. static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
  344. {
  345. int j, ret, attrcount = 0;
  346. /* Dynamically created from the channels array */
  347. for (j = 0; j < indio_dev->num_channels; j++) {
  348. ret = iio_device_add_event_sysfs(indio_dev,
  349. &indio_dev->channels[j]);
  350. if (ret < 0)
  351. return ret;
  352. attrcount += ret;
  353. }
  354. return attrcount;
  355. }
  356. static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
  357. {
  358. int j;
  359. for (j = 0; j < indio_dev->num_channels; j++) {
  360. if (indio_dev->channels[j].num_event_specs != 0)
  361. return true;
  362. }
  363. return false;
  364. }
  365. static void iio_setup_ev_int(struct iio_event_interface *ev_int)
  366. {
  367. INIT_KFIFO(ev_int->det_events);
  368. init_waitqueue_head(&ev_int->wait);
  369. mutex_init(&ev_int->read_lock);
  370. }
  371. static const char *iio_event_group_name = "events";
  372. int iio_device_register_eventset(struct iio_dev *indio_dev)
  373. {
  374. struct iio_dev_attr *p;
  375. int ret = 0, attrcount_orig = 0, attrcount, attrn;
  376. struct attribute **attr;
  377. if (!(indio_dev->info->event_attrs ||
  378. iio_check_for_dynamic_events(indio_dev)))
  379. return 0;
  380. indio_dev->event_interface =
  381. kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL);
  382. if (indio_dev->event_interface == NULL)
  383. return -ENOMEM;
  384. INIT_LIST_HEAD(&indio_dev->event_interface->dev_attr_list);
  385. iio_setup_ev_int(indio_dev->event_interface);
  386. if (indio_dev->info->event_attrs != NULL) {
  387. attr = indio_dev->info->event_attrs->attrs;
  388. while (*attr++ != NULL)
  389. attrcount_orig++;
  390. }
  391. attrcount = attrcount_orig;
  392. if (indio_dev->channels) {
  393. ret = __iio_add_event_config_attrs(indio_dev);
  394. if (ret < 0)
  395. goto error_free_setup_event_lines;
  396. attrcount += ret;
  397. }
  398. indio_dev->event_interface->group.name = iio_event_group_name;
  399. indio_dev->event_interface->group.attrs = kcalloc(attrcount + 1,
  400. sizeof(indio_dev->event_interface->group.attrs[0]),
  401. GFP_KERNEL);
  402. if (indio_dev->event_interface->group.attrs == NULL) {
  403. ret = -ENOMEM;
  404. goto error_free_setup_event_lines;
  405. }
  406. if (indio_dev->info->event_attrs)
  407. memcpy(indio_dev->event_interface->group.attrs,
  408. indio_dev->info->event_attrs->attrs,
  409. sizeof(indio_dev->event_interface->group.attrs[0])
  410. *attrcount_orig);
  411. attrn = attrcount_orig;
  412. /* Add all elements from the list. */
  413. list_for_each_entry(p,
  414. &indio_dev->event_interface->dev_attr_list,
  415. l)
  416. indio_dev->event_interface->group.attrs[attrn++] =
  417. &p->dev_attr.attr;
  418. indio_dev->groups[indio_dev->groupcounter++] =
  419. &indio_dev->event_interface->group;
  420. return 0;
  421. error_free_setup_event_lines:
  422. iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list);
  423. kfree(indio_dev->event_interface);
  424. indio_dev->event_interface = NULL;
  425. return ret;
  426. }
  427. /**
  428. * iio_device_wakeup_eventset - Wakes up the event waitqueue
  429. * @indio_dev: The IIO device
  430. *
  431. * Wakes up the event waitqueue used for poll() and blocking read().
  432. * Should usually be called when the device is unregistered.
  433. */
  434. void iio_device_wakeup_eventset(struct iio_dev *indio_dev)
  435. {
  436. if (indio_dev->event_interface == NULL)
  437. return;
  438. wake_up(&indio_dev->event_interface->wait);
  439. }
  440. void iio_device_unregister_eventset(struct iio_dev *indio_dev)
  441. {
  442. if (indio_dev->event_interface == NULL)
  443. return;
  444. iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list);
  445. kfree(indio_dev->event_interface->group.attrs);
  446. kfree(indio_dev->event_interface);
  447. }