industrialio-event.c 14 KB

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