industrialio-event.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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(&iio_dev_opaque->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(&iio_dev_opaque->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. [IIO_EV_INFO_RUNNING_PERIOD] = "runningperiod",
  216. [IIO_EV_INFO_RUNNING_COUNT] = "runningcount",
  217. };
  218. static enum iio_event_direction iio_ev_attr_dir(struct iio_dev_attr *attr)
  219. {
  220. return attr->c->event_spec[attr->address & 0xffff].dir;
  221. }
  222. static enum iio_event_type iio_ev_attr_type(struct iio_dev_attr *attr)
  223. {
  224. return attr->c->event_spec[attr->address & 0xffff].type;
  225. }
  226. static enum iio_event_info iio_ev_attr_info(struct iio_dev_attr *attr)
  227. {
  228. return (attr->address >> 16) & 0xffff;
  229. }
  230. static ssize_t iio_ev_state_store(struct device *dev,
  231. struct device_attribute *attr,
  232. const char *buf,
  233. size_t len)
  234. {
  235. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  236. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  237. int ret;
  238. bool val;
  239. ret = kstrtobool(buf, &val);
  240. if (ret < 0)
  241. return ret;
  242. if (!indio_dev->info->write_event_config)
  243. return -EINVAL;
  244. ret = indio_dev->info->write_event_config(indio_dev,
  245. this_attr->c, iio_ev_attr_type(this_attr),
  246. iio_ev_attr_dir(this_attr), val);
  247. return (ret < 0) ? ret : len;
  248. }
  249. static ssize_t iio_ev_state_show(struct device *dev,
  250. struct device_attribute *attr,
  251. char *buf)
  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;
  256. if (!indio_dev->info->read_event_config)
  257. return -EINVAL;
  258. val = indio_dev->info->read_event_config(indio_dev,
  259. this_attr->c, iio_ev_attr_type(this_attr),
  260. iio_ev_attr_dir(this_attr));
  261. if (val < 0)
  262. return val;
  263. else
  264. return sysfs_emit(buf, "%d\n", val);
  265. }
  266. static ssize_t iio_ev_value_show(struct device *dev,
  267. struct device_attribute *attr,
  268. char *buf)
  269. {
  270. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  271. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  272. int val, val2, val_arr[2];
  273. int ret;
  274. if (!indio_dev->info->read_event_value)
  275. return -EINVAL;
  276. ret = indio_dev->info->read_event_value(indio_dev,
  277. this_attr->c, iio_ev_attr_type(this_attr),
  278. iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
  279. &val, &val2);
  280. if (ret < 0)
  281. return ret;
  282. val_arr[0] = val;
  283. val_arr[1] = val2;
  284. return iio_format_value(buf, ret, 2, val_arr);
  285. }
  286. static ssize_t iio_ev_value_store(struct device *dev,
  287. struct device_attribute *attr,
  288. const char *buf,
  289. size_t len)
  290. {
  291. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  292. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  293. int val, val2;
  294. int ret;
  295. if (!indio_dev->info->write_event_value)
  296. return -EINVAL;
  297. ret = iio_str_to_fixpoint(buf, 100000, &val, &val2);
  298. if (ret)
  299. return ret;
  300. ret = indio_dev->info->write_event_value(indio_dev,
  301. this_attr->c, iio_ev_attr_type(this_attr),
  302. iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
  303. val, val2);
  304. if (ret < 0)
  305. return ret;
  306. return len;
  307. }
  308. static ssize_t iio_ev_label_show(struct device *dev,
  309. struct device_attribute *attr,
  310. char *buf)
  311. {
  312. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  313. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  314. if (indio_dev->info->read_event_label)
  315. return indio_dev->info->read_event_label(indio_dev,
  316. this_attr->c, iio_ev_attr_type(this_attr),
  317. iio_ev_attr_dir(this_attr), buf);
  318. return -EINVAL;
  319. }
  320. static int iio_device_add_event(struct iio_dev *indio_dev,
  321. const struct iio_chan_spec *chan, unsigned int spec_index,
  322. enum iio_event_type type, enum iio_event_direction dir,
  323. enum iio_shared_by shared_by, const unsigned long *mask)
  324. {
  325. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  326. ssize_t (*show)(struct device *dev, struct device_attribute *attr,
  327. char *buf);
  328. ssize_t (*store)(struct device *dev, struct device_attribute *attr,
  329. const char *buf, size_t len);
  330. unsigned int attrcount = 0;
  331. unsigned int i;
  332. char *postfix;
  333. int ret;
  334. for_each_set_bit(i, mask, sizeof(*mask)*8) {
  335. if (i >= ARRAY_SIZE(iio_ev_info_text))
  336. return -EINVAL;
  337. if (dir != IIO_EV_DIR_NONE)
  338. postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
  339. iio_ev_type_text[type],
  340. iio_ev_dir_text[dir],
  341. iio_ev_info_text[i]);
  342. else
  343. postfix = kasprintf(GFP_KERNEL, "%s_%s",
  344. iio_ev_type_text[type],
  345. iio_ev_info_text[i]);
  346. if (postfix == NULL)
  347. return -ENOMEM;
  348. if (i == IIO_EV_INFO_ENABLE) {
  349. show = iio_ev_state_show;
  350. store = iio_ev_state_store;
  351. } else {
  352. show = iio_ev_value_show;
  353. store = iio_ev_value_store;
  354. }
  355. ret = __iio_add_chan_devattr(postfix, chan, show, store,
  356. (i << 16) | spec_index, shared_by, &indio_dev->dev,
  357. NULL,
  358. &iio_dev_opaque->event_interface->dev_attr_list);
  359. kfree(postfix);
  360. if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
  361. continue;
  362. if (ret)
  363. return ret;
  364. attrcount++;
  365. }
  366. return attrcount;
  367. }
  368. static int iio_device_add_event_label(struct iio_dev *indio_dev,
  369. const struct iio_chan_spec *chan,
  370. unsigned int spec_index,
  371. enum iio_event_type type,
  372. enum iio_event_direction dir)
  373. {
  374. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  375. char *postfix;
  376. int ret;
  377. if (!indio_dev->info->read_event_label)
  378. return 0;
  379. if (dir != IIO_EV_DIR_NONE)
  380. postfix = kasprintf(GFP_KERNEL, "%s_%s_label",
  381. iio_ev_type_text[type],
  382. iio_ev_dir_text[dir]);
  383. else
  384. postfix = kasprintf(GFP_KERNEL, "%s_label",
  385. iio_ev_type_text[type]);
  386. if (postfix == NULL)
  387. return -ENOMEM;
  388. ret = __iio_add_chan_devattr(postfix, chan, &iio_ev_label_show, NULL,
  389. spec_index, IIO_SEPARATE, &indio_dev->dev, NULL,
  390. &iio_dev_opaque->event_interface->dev_attr_list);
  391. kfree(postfix);
  392. if (ret < 0)
  393. return ret;
  394. return 1;
  395. }
  396. static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
  397. struct iio_chan_spec const *chan)
  398. {
  399. int ret = 0, i, attrcount = 0;
  400. enum iio_event_direction dir;
  401. enum iio_event_type type;
  402. for (i = 0; i < chan->num_event_specs; i++) {
  403. type = chan->event_spec[i].type;
  404. dir = chan->event_spec[i].dir;
  405. ret = iio_device_add_event(indio_dev, chan, i, type, dir,
  406. IIO_SEPARATE, &chan->event_spec[i].mask_separate);
  407. if (ret < 0)
  408. return ret;
  409. attrcount += ret;
  410. ret = iio_device_add_event(indio_dev, chan, i, type, dir,
  411. IIO_SHARED_BY_TYPE,
  412. &chan->event_spec[i].mask_shared_by_type);
  413. if (ret < 0)
  414. return ret;
  415. attrcount += ret;
  416. ret = iio_device_add_event(indio_dev, chan, i, type, dir,
  417. IIO_SHARED_BY_DIR,
  418. &chan->event_spec[i].mask_shared_by_dir);
  419. if (ret < 0)
  420. return ret;
  421. attrcount += ret;
  422. ret = iio_device_add_event(indio_dev, chan, i, type, dir,
  423. IIO_SHARED_BY_ALL,
  424. &chan->event_spec[i].mask_shared_by_all);
  425. if (ret < 0)
  426. return ret;
  427. attrcount += ret;
  428. ret = iio_device_add_event_label(indio_dev, chan, i, type, dir);
  429. if (ret < 0)
  430. return ret;
  431. attrcount += ret;
  432. }
  433. ret = attrcount;
  434. return ret;
  435. }
  436. static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
  437. {
  438. int j, ret, attrcount = 0;
  439. /* Dynamically created from the channels array */
  440. for (j = 0; j < indio_dev->num_channels; j++) {
  441. ret = iio_device_add_event_sysfs(indio_dev,
  442. &indio_dev->channels[j]);
  443. if (ret < 0)
  444. return ret;
  445. attrcount += ret;
  446. }
  447. return attrcount;
  448. }
  449. static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
  450. {
  451. int j;
  452. for (j = 0; j < indio_dev->num_channels; j++) {
  453. if (indio_dev->channels[j].num_event_specs != 0)
  454. return true;
  455. }
  456. return false;
  457. }
  458. static void iio_setup_ev_int(struct iio_event_interface *ev_int)
  459. {
  460. INIT_KFIFO(ev_int->det_events);
  461. init_waitqueue_head(&ev_int->wait);
  462. mutex_init(&ev_int->read_lock);
  463. }
  464. static long iio_event_ioctl(struct iio_dev *indio_dev, struct file *filp,
  465. unsigned int cmd, unsigned long arg)
  466. {
  467. int __user *ip = (int __user *)arg;
  468. int fd;
  469. if (cmd == IIO_GET_EVENT_FD_IOCTL) {
  470. fd = iio_event_getfd(indio_dev);
  471. if (fd < 0)
  472. return fd;
  473. if (copy_to_user(ip, &fd, sizeof(fd)))
  474. return -EFAULT;
  475. return 0;
  476. }
  477. return IIO_IOCTL_UNHANDLED;
  478. }
  479. static const char *iio_event_group_name = "events";
  480. int iio_device_register_eventset(struct iio_dev *indio_dev)
  481. {
  482. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  483. struct iio_event_interface *ev_int;
  484. struct iio_dev_attr *p;
  485. int ret = 0, attrcount_orig = 0, attrcount, attrn;
  486. struct attribute **attr;
  487. if (!(indio_dev->info->event_attrs ||
  488. iio_check_for_dynamic_events(indio_dev)))
  489. return 0;
  490. ev_int = kzalloc(sizeof(*ev_int), GFP_KERNEL);
  491. if (!ev_int)
  492. return -ENOMEM;
  493. iio_dev_opaque->event_interface = ev_int;
  494. INIT_LIST_HEAD(&ev_int->dev_attr_list);
  495. iio_setup_ev_int(ev_int);
  496. if (indio_dev->info->event_attrs != NULL) {
  497. attr = indio_dev->info->event_attrs->attrs;
  498. while (*attr++ != NULL)
  499. attrcount_orig++;
  500. }
  501. attrcount = attrcount_orig;
  502. if (indio_dev->channels) {
  503. ret = __iio_add_event_config_attrs(indio_dev);
  504. if (ret < 0)
  505. goto error_free_setup_event_lines;
  506. attrcount += ret;
  507. }
  508. ev_int->group.name = iio_event_group_name;
  509. ev_int->group.attrs = kcalloc(attrcount + 1,
  510. sizeof(ev_int->group.attrs[0]),
  511. GFP_KERNEL);
  512. if (ev_int->group.attrs == NULL) {
  513. ret = -ENOMEM;
  514. goto error_free_setup_event_lines;
  515. }
  516. if (indio_dev->info->event_attrs)
  517. memcpy(ev_int->group.attrs,
  518. indio_dev->info->event_attrs->attrs,
  519. sizeof(ev_int->group.attrs[0]) * attrcount_orig);
  520. attrn = attrcount_orig;
  521. /* Add all elements from the list. */
  522. list_for_each_entry(p, &ev_int->dev_attr_list, l)
  523. ev_int->group.attrs[attrn++] = &p->dev_attr.attr;
  524. ret = iio_device_register_sysfs_group(indio_dev, &ev_int->group);
  525. if (ret)
  526. goto error_free_group_attrs;
  527. ev_int->ioctl_handler.ioctl = iio_event_ioctl;
  528. iio_device_ioctl_handler_register(&iio_dev_opaque->indio_dev,
  529. &ev_int->ioctl_handler);
  530. return 0;
  531. error_free_group_attrs:
  532. kfree(ev_int->group.attrs);
  533. error_free_setup_event_lines:
  534. iio_free_chan_devattr_list(&ev_int->dev_attr_list);
  535. kfree(ev_int);
  536. iio_dev_opaque->event_interface = NULL;
  537. return ret;
  538. }
  539. /**
  540. * iio_device_wakeup_eventset - Wakes up the event waitqueue
  541. * @indio_dev: The IIO device
  542. *
  543. * Wakes up the event waitqueue used for poll() and blocking read().
  544. * Should usually be called when the device is unregistered.
  545. */
  546. void iio_device_wakeup_eventset(struct iio_dev *indio_dev)
  547. {
  548. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  549. if (iio_dev_opaque->event_interface == NULL)
  550. return;
  551. wake_up(&iio_dev_opaque->event_interface->wait);
  552. }
  553. void iio_device_unregister_eventset(struct iio_dev *indio_dev)
  554. {
  555. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
  556. struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
  557. if (ev_int == NULL)
  558. return;
  559. iio_device_ioctl_handler_unregister(&ev_int->ioctl_handler);
  560. iio_free_chan_devattr_list(&ev_int->dev_attr_list);
  561. kfree(ev_int->group.attrs);
  562. kfree(ev_int);
  563. iio_dev_opaque->event_interface = NULL;
  564. }