industrialio-event.c 13 KB

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