industrialio-event.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Industrial I/O event handling
  3. *
  4. * Copyright (c) 2008 Jonathan Cameron
  5. *
  6. * Based on elements of hwmon and input subsystems.
  7. */
  8. #include <linux/anon_inodes.h>
  9. #include <linux/device.h>
  10. #include <linux/fs.h>
  11. #include <linux/kernel.h>
  12. #include <linux/kfifo.h>
  13. #include <linux/module.h>
  14. #include <linux/poll.h>
  15. #include <linux/sched.h>
  16. #include <linux/slab.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/wait.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/iio-opaque.h>
  21. #include "iio_core.h"
  22. #include <linux/iio/sysfs.h>
  23. #include <linux/iio/events.h>
  24. /**
  25. * struct iio_event_interface - chrdev interface for an event line
  26. * @wait: wait queue to allow blocking reads of events
  27. * @det_events: list of detected events
  28. * @dev_attr_list: list of event interface sysfs attribute
  29. * @flags: file operations related flags including busy flag.
  30. * @group: event interface sysfs attribute group
  31. * @read_lock: lock to protect kfifo read operations
  32. * @ioctl_handler: handler for event ioctl() calls
  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. struct iio_ioctl_handler ioctl_handler;
  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_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  64. struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
  65. struct iio_event_data ev;
  66. int copied;
  67. if (!ev_int)
  68. return 0;
  69. /* Does anyone care? */
  70. if (iio_event_enabled(ev_int)) {
  71. ev.id = ev_code;
  72. ev.timestamp = timestamp;
  73. copied = kfifo_put(&ev_int->det_events, ev);
  74. if (copied != 0)
  75. wake_up_poll(&ev_int->wait, EPOLLIN);
  76. }
  77. return 0;
  78. }
  79. EXPORT_SYMBOL(iio_push_event);
  80. /**
  81. * iio_event_poll() - poll the event queue to find out if it has data
  82. * @filep: File structure pointer to identify the device
  83. * @wait: Poll table pointer to add the wait queue on
  84. *
  85. * Return: (EPOLLIN | EPOLLRDNORM) if data is available for reading
  86. * or a negative error code on failure
  87. */
  88. static __poll_t iio_event_poll(struct file *filep,
  89. struct poll_table_struct *wait)
  90. {
  91. struct iio_dev *indio_dev = filep->private_data;
  92. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  93. struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
  94. __poll_t events = 0;
  95. if (!indio_dev->info)
  96. return events;
  97. poll_wait(filep, &ev_int->wait, wait);
  98. if (!kfifo_is_empty(&ev_int->det_events))
  99. events = EPOLLIN | EPOLLRDNORM;
  100. return events;
  101. }
  102. static ssize_t iio_event_chrdev_read(struct file *filep,
  103. char __user *buf,
  104. size_t count,
  105. loff_t *f_ps)
  106. {
  107. struct iio_dev *indio_dev = filep->private_data;
  108. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  109. struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
  110. unsigned int copied;
  111. int ret;
  112. if (!indio_dev->info)
  113. return -ENODEV;
  114. if (count < sizeof(struct iio_event_data))
  115. return -EINVAL;
  116. do {
  117. if (kfifo_is_empty(&ev_int->det_events)) {
  118. if (filep->f_flags & O_NONBLOCK)
  119. return -EAGAIN;
  120. ret = wait_event_interruptible(ev_int->wait,
  121. !kfifo_is_empty(&ev_int->det_events) ||
  122. indio_dev->info == NULL);
  123. if (ret)
  124. return ret;
  125. if (indio_dev->info == NULL)
  126. return -ENODEV;
  127. }
  128. if (mutex_lock_interruptible(&ev_int->read_lock))
  129. return -ERESTARTSYS;
  130. ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied);
  131. mutex_unlock(&ev_int->read_lock);
  132. if (ret)
  133. return ret;
  134. /*
  135. * If we couldn't read anything from the fifo (a different
  136. * thread might have been faster) we either return -EAGAIN if
  137. * the file descriptor is non-blocking, otherwise we go back to
  138. * sleep and wait for more data to arrive.
  139. */
  140. if (copied == 0 && (filep->f_flags & O_NONBLOCK))
  141. return -EAGAIN;
  142. } while (copied == 0);
  143. return copied;
  144. }
  145. static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
  146. {
  147. struct iio_dev *indio_dev = filep->private_data;
  148. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  149. struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
  150. clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
  151. iio_device_put(indio_dev);
  152. return 0;
  153. }
  154. static const struct file_operations iio_event_chrdev_fileops = {
  155. .read = iio_event_chrdev_read,
  156. .poll = iio_event_poll,
  157. .release = iio_event_chrdev_release,
  158. .owner = THIS_MODULE,
  159. .llseek = noop_llseek,
  160. };
  161. static int iio_event_getfd(struct iio_dev *indio_dev)
  162. {
  163. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  164. struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
  165. int fd;
  166. if (ev_int == NULL)
  167. return -ENODEV;
  168. fd = mutex_lock_interruptible(&indio_dev->mlock);
  169. if (fd)
  170. return fd;
  171. if (test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
  172. fd = -EBUSY;
  173. goto unlock;
  174. }
  175. iio_device_get(indio_dev);
  176. fd = anon_inode_getfd("iio:event", &iio_event_chrdev_fileops,
  177. indio_dev, O_RDONLY | O_CLOEXEC);
  178. if (fd < 0) {
  179. clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
  180. iio_device_put(indio_dev);
  181. } else {
  182. kfifo_reset_out(&ev_int->det_events);
  183. }
  184. unlock:
  185. mutex_unlock(&indio_dev->mlock);
  186. return fd;
  187. }
  188. static const char * const iio_ev_type_text[] = {
  189. [IIO_EV_TYPE_THRESH] = "thresh",
  190. [IIO_EV_TYPE_MAG] = "mag",
  191. [IIO_EV_TYPE_ROC] = "roc",
  192. [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
  193. [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
  194. [IIO_EV_TYPE_CHANGE] = "change",
  195. [IIO_EV_TYPE_MAG_REFERENCED] = "mag_referenced",
  196. [IIO_EV_TYPE_GESTURE] = "gesture",
  197. };
  198. static const char * const iio_ev_dir_text[] = {
  199. [IIO_EV_DIR_EITHER] = "either",
  200. [IIO_EV_DIR_RISING] = "rising",
  201. [IIO_EV_DIR_FALLING] = "falling",
  202. [IIO_EV_DIR_SINGLETAP] = "singletap",
  203. [IIO_EV_DIR_DOUBLETAP] = "doubletap",
  204. };
  205. static const char * const iio_ev_info_text[] = {
  206. [IIO_EV_INFO_ENABLE] = "en",
  207. [IIO_EV_INFO_VALUE] = "value",
  208. [IIO_EV_INFO_HYSTERESIS] = "hysteresis",
  209. [IIO_EV_INFO_PERIOD] = "period",
  210. [IIO_EV_INFO_HIGH_PASS_FILTER_3DB] = "high_pass_filter_3db",
  211. [IIO_EV_INFO_LOW_PASS_FILTER_3DB] = "low_pass_filter_3db",
  212. [IIO_EV_INFO_TIMEOUT] = "timeout",
  213. [IIO_EV_INFO_RESET_TIMEOUT] = "reset_timeout",
  214. [IIO_EV_INFO_TAP2_MIN_DELAY] = "tap2_min_delay",
  215. };
  216. static enum iio_event_direction iio_ev_attr_dir(struct iio_dev_attr *attr)
  217. {
  218. return attr->c->event_spec[attr->address & 0xffff].dir;
  219. }
  220. static enum iio_event_type iio_ev_attr_type(struct iio_dev_attr *attr)
  221. {
  222. return attr->c->event_spec[attr->address & 0xffff].type;
  223. }
  224. static enum iio_event_info iio_ev_attr_info(struct iio_dev_attr *attr)
  225. {
  226. return (attr->address >> 16) & 0xffff;
  227. }
  228. static ssize_t iio_ev_state_store(struct device *dev,
  229. struct device_attribute *attr,
  230. const char *buf,
  231. size_t len)
  232. {
  233. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  234. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  235. int ret;
  236. bool val;
  237. ret = kstrtobool(buf, &val);
  238. if (ret < 0)
  239. return ret;
  240. ret = indio_dev->info->write_event_config(indio_dev,
  241. this_attr->c, iio_ev_attr_type(this_attr),
  242. iio_ev_attr_dir(this_attr), val);
  243. return (ret < 0) ? ret : len;
  244. }
  245. static ssize_t iio_ev_state_show(struct device *dev,
  246. struct device_attribute *attr,
  247. char *buf)
  248. {
  249. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  250. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  251. int val;
  252. val = indio_dev->info->read_event_config(indio_dev,
  253. this_attr->c, iio_ev_attr_type(this_attr),
  254. iio_ev_attr_dir(this_attr));
  255. if (val < 0)
  256. return val;
  257. else
  258. return sysfs_emit(buf, "%d\n", val);
  259. }
  260. static ssize_t iio_ev_value_show(struct device *dev,
  261. struct device_attribute *attr,
  262. char *buf)
  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, val_arr[2];
  267. int ret;
  268. ret = indio_dev->info->read_event_value(indio_dev,
  269. this_attr->c, iio_ev_attr_type(this_attr),
  270. iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
  271. &val, &val2);
  272. if (ret < 0)
  273. return ret;
  274. val_arr[0] = val;
  275. val_arr[1] = val2;
  276. return iio_format_value(buf, ret, 2, val_arr);
  277. }
  278. static ssize_t iio_ev_value_store(struct device *dev,
  279. struct device_attribute *attr,
  280. const char *buf,
  281. size_t len)
  282. {
  283. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  284. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  285. int val, val2;
  286. int ret;
  287. if (!indio_dev->info->write_event_value)
  288. return -EINVAL;
  289. ret = iio_str_to_fixpoint(buf, 100000, &val, &val2);
  290. if (ret)
  291. return ret;
  292. ret = indio_dev->info->write_event_value(indio_dev,
  293. this_attr->c, iio_ev_attr_type(this_attr),
  294. iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
  295. val, val2);
  296. if (ret < 0)
  297. return ret;
  298. return len;
  299. }
  300. static int iio_device_add_event(struct iio_dev *indio_dev,
  301. const struct iio_chan_spec *chan, unsigned int spec_index,
  302. enum iio_event_type type, enum iio_event_direction dir,
  303. enum iio_shared_by shared_by, const unsigned long *mask)
  304. {
  305. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  306. ssize_t (*show)(struct device *dev, struct device_attribute *attr,
  307. char *buf);
  308. ssize_t (*store)(struct device *dev, struct device_attribute *attr,
  309. const char *buf, size_t len);
  310. unsigned int attrcount = 0;
  311. unsigned int i;
  312. char *postfix;
  313. int ret;
  314. for_each_set_bit(i, mask, sizeof(*mask)*8) {
  315. if (i >= ARRAY_SIZE(iio_ev_info_text))
  316. return -EINVAL;
  317. if (dir != IIO_EV_DIR_NONE)
  318. postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
  319. iio_ev_type_text[type],
  320. iio_ev_dir_text[dir],
  321. iio_ev_info_text[i]);
  322. else
  323. postfix = kasprintf(GFP_KERNEL, "%s_%s",
  324. iio_ev_type_text[type],
  325. iio_ev_info_text[i]);
  326. if (postfix == NULL)
  327. return -ENOMEM;
  328. if (i == IIO_EV_INFO_ENABLE) {
  329. show = iio_ev_state_show;
  330. store = iio_ev_state_store;
  331. } else {
  332. show = iio_ev_value_show;
  333. store = iio_ev_value_store;
  334. }
  335. ret = __iio_add_chan_devattr(postfix, chan, show, store,
  336. (i << 16) | spec_index, shared_by, &indio_dev->dev,
  337. NULL,
  338. &iio_dev_opaque->event_interface->dev_attr_list);
  339. kfree(postfix);
  340. if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
  341. continue;
  342. if (ret)
  343. return ret;
  344. attrcount++;
  345. }
  346. return attrcount;
  347. }
  348. static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
  349. struct iio_chan_spec const *chan)
  350. {
  351. int ret = 0, i, attrcount = 0;
  352. enum iio_event_direction dir;
  353. enum iio_event_type type;
  354. for (i = 0; i < chan->num_event_specs; i++) {
  355. type = chan->event_spec[i].type;
  356. dir = chan->event_spec[i].dir;
  357. ret = iio_device_add_event(indio_dev, chan, i, type, dir,
  358. IIO_SEPARATE, &chan->event_spec[i].mask_separate);
  359. if (ret < 0)
  360. return ret;
  361. attrcount += ret;
  362. ret = iio_device_add_event(indio_dev, chan, i, type, dir,
  363. IIO_SHARED_BY_TYPE,
  364. &chan->event_spec[i].mask_shared_by_type);
  365. if (ret < 0)
  366. return ret;
  367. attrcount += ret;
  368. ret = iio_device_add_event(indio_dev, chan, i, type, dir,
  369. IIO_SHARED_BY_DIR,
  370. &chan->event_spec[i].mask_shared_by_dir);
  371. if (ret < 0)
  372. return ret;
  373. attrcount += ret;
  374. ret = iio_device_add_event(indio_dev, chan, i, type, dir,
  375. IIO_SHARED_BY_ALL,
  376. &chan->event_spec[i].mask_shared_by_all);
  377. if (ret < 0)
  378. return ret;
  379. attrcount += ret;
  380. }
  381. ret = attrcount;
  382. return ret;
  383. }
  384. static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
  385. {
  386. int j, ret, attrcount = 0;
  387. /* Dynamically created from the channels array */
  388. for (j = 0; j < indio_dev->num_channels; j++) {
  389. ret = iio_device_add_event_sysfs(indio_dev,
  390. &indio_dev->channels[j]);
  391. if (ret < 0)
  392. return ret;
  393. attrcount += ret;
  394. }
  395. return attrcount;
  396. }
  397. static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
  398. {
  399. int j;
  400. for (j = 0; j < indio_dev->num_channels; j++) {
  401. if (indio_dev->channels[j].num_event_specs != 0)
  402. return true;
  403. }
  404. return false;
  405. }
  406. static void iio_setup_ev_int(struct iio_event_interface *ev_int)
  407. {
  408. INIT_KFIFO(ev_int->det_events);
  409. init_waitqueue_head(&ev_int->wait);
  410. mutex_init(&ev_int->read_lock);
  411. }
  412. static long iio_event_ioctl(struct iio_dev *indio_dev, struct file *filp,
  413. unsigned int cmd, unsigned long arg)
  414. {
  415. int __user *ip = (int __user *)arg;
  416. int fd;
  417. if (cmd == IIO_GET_EVENT_FD_IOCTL) {
  418. fd = iio_event_getfd(indio_dev);
  419. if (fd < 0)
  420. return fd;
  421. if (copy_to_user(ip, &fd, sizeof(fd)))
  422. return -EFAULT;
  423. return 0;
  424. }
  425. return IIO_IOCTL_UNHANDLED;
  426. }
  427. static const char *iio_event_group_name = "events";
  428. int iio_device_register_eventset(struct iio_dev *indio_dev)
  429. {
  430. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  431. struct iio_event_interface *ev_int;
  432. struct iio_dev_attr *p;
  433. int ret = 0, attrcount_orig = 0, attrcount, attrn;
  434. struct attribute **attr;
  435. if (!(indio_dev->info->event_attrs ||
  436. iio_check_for_dynamic_events(indio_dev)))
  437. return 0;
  438. ev_int = kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL);
  439. if (ev_int == NULL)
  440. return -ENOMEM;
  441. iio_dev_opaque->event_interface = ev_int;
  442. INIT_LIST_HEAD(&ev_int->dev_attr_list);
  443. iio_setup_ev_int(ev_int);
  444. if (indio_dev->info->event_attrs != NULL) {
  445. attr = indio_dev->info->event_attrs->attrs;
  446. while (*attr++ != NULL)
  447. attrcount_orig++;
  448. }
  449. attrcount = attrcount_orig;
  450. if (indio_dev->channels) {
  451. ret = __iio_add_event_config_attrs(indio_dev);
  452. if (ret < 0)
  453. goto error_free_setup_event_lines;
  454. attrcount += ret;
  455. }
  456. ev_int->group.name = iio_event_group_name;
  457. ev_int->group.attrs = kcalloc(attrcount + 1,
  458. sizeof(ev_int->group.attrs[0]),
  459. GFP_KERNEL);
  460. if (ev_int->group.attrs == NULL) {
  461. ret = -ENOMEM;
  462. goto error_free_setup_event_lines;
  463. }
  464. if (indio_dev->info->event_attrs)
  465. memcpy(ev_int->group.attrs,
  466. indio_dev->info->event_attrs->attrs,
  467. sizeof(ev_int->group.attrs[0]) * attrcount_orig);
  468. attrn = attrcount_orig;
  469. /* Add all elements from the list. */
  470. list_for_each_entry(p, &ev_int->dev_attr_list, l)
  471. ev_int->group.attrs[attrn++] = &p->dev_attr.attr;
  472. ret = iio_device_register_sysfs_group(indio_dev, &ev_int->group);
  473. if (ret)
  474. goto error_free_group_attrs;
  475. ev_int->ioctl_handler.ioctl = iio_event_ioctl;
  476. iio_device_ioctl_handler_register(&iio_dev_opaque->indio_dev,
  477. &ev_int->ioctl_handler);
  478. return 0;
  479. error_free_group_attrs:
  480. kfree(ev_int->group.attrs);
  481. error_free_setup_event_lines:
  482. iio_free_chan_devattr_list(&ev_int->dev_attr_list);
  483. kfree(ev_int);
  484. iio_dev_opaque->event_interface = NULL;
  485. return ret;
  486. }
  487. /**
  488. * iio_device_wakeup_eventset - Wakes up the event waitqueue
  489. * @indio_dev: The IIO device
  490. *
  491. * Wakes up the event waitqueue used for poll() and blocking read().
  492. * Should usually be called when the device is unregistered.
  493. */
  494. void iio_device_wakeup_eventset(struct iio_dev *indio_dev)
  495. {
  496. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  497. if (iio_dev_opaque->event_interface == NULL)
  498. return;
  499. wake_up(&iio_dev_opaque->event_interface->wait);
  500. }
  501. void iio_device_unregister_eventset(struct iio_dev *indio_dev)
  502. {
  503. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  504. struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
  505. if (ev_int == NULL)
  506. return;
  507. iio_device_ioctl_handler_unregister(&ev_int->ioctl_handler);
  508. iio_free_chan_devattr_list(&ev_int->dev_attr_list);
  509. kfree(ev_int->group.attrs);
  510. kfree(ev_int);
  511. iio_dev_opaque->event_interface = NULL;
  512. }