industrialio-event.c 14 KB

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