industrialio-event.c 14 KB

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