Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 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 | |
| 12 | #include <linux/anon_inodes.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/fs.h> |
| 15 | #include <linux/kernel.h> |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 16 | #include <linux/kfifo.h> |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 17 | #include <linux/module.h> |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 18 | #include <linux/poll.h> |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 19 | #include <linux/sched.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | #include <linux/wait.h> |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 23 | #include <linux/iio/iio.h> |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 24 | #include "iio_core.h" |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 25 | #include <linux/iio/sysfs.h> |
| 26 | #include <linux/iio/events.h> |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 27 | |
| 28 | /** |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 29 | * struct iio_event_interface - chrdev interface for an event line |
| 30 | * @wait: wait queue to allow blocking reads of events |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 31 | * @det_events: list of detected events |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 32 | * @dev_attr_list: list of event interface sysfs attribute |
| 33 | * @flags: file operations related flags including busy flag. |
| 34 | * @group: event interface sysfs attribute group |
| 35 | */ |
| 36 | struct iio_event_interface { |
| 37 | wait_queue_head_t wait; |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 38 | DECLARE_KFIFO(det_events, struct iio_event_data, 16); |
| 39 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 40 | struct list_head dev_attr_list; |
| 41 | unsigned long flags; |
| 42 | struct attribute_group group; |
| 43 | }; |
| 44 | |
| 45 | int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp) |
| 46 | { |
| 47 | struct iio_event_interface *ev_int = indio_dev->event_interface; |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 48 | struct iio_event_data ev; |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 49 | unsigned long flags; |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 50 | int copied; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 51 | |
| 52 | /* Does anyone care? */ |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 53 | spin_lock_irqsave(&ev_int->wait.lock, flags); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 54 | if (test_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) { |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 55 | |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 56 | ev.id = ev_code; |
| 57 | ev.timestamp = timestamp; |
| 58 | |
Stefani Seibold | 498d319 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 59 | copied = kfifo_put(&ev_int->det_events, ev); |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 60 | if (copied != 0) |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 61 | wake_up_locked_poll(&ev_int->wait, POLLIN); |
Lars-Peter Clausen | 43ba110 | 2012-01-03 14:59:40 +0100 | [diff] [blame] | 62 | } |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 63 | spin_unlock_irqrestore(&ev_int->wait.lock, flags); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 64 | |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 65 | return 0; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 66 | } |
| 67 | EXPORT_SYMBOL(iio_push_event); |
| 68 | |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 69 | /** |
| 70 | * iio_event_poll() - poll the event queue to find out if it has data |
| 71 | */ |
| 72 | static unsigned int iio_event_poll(struct file *filep, |
| 73 | struct poll_table_struct *wait) |
| 74 | { |
Lars-Peter Clausen | cadc212 | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 75 | struct iio_dev *indio_dev = filep->private_data; |
| 76 | struct iio_event_interface *ev_int = indio_dev->event_interface; |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 77 | unsigned int events = 0; |
| 78 | |
Lars-Peter Clausen | f18e7a0 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 79 | if (!indio_dev->info) |
| 80 | return -ENODEV; |
| 81 | |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 82 | poll_wait(filep, &ev_int->wait, wait); |
| 83 | |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 84 | spin_lock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 85 | if (!kfifo_is_empty(&ev_int->det_events)) |
| 86 | events = POLLIN | POLLRDNORM; |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 87 | spin_unlock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 88 | |
| 89 | return events; |
| 90 | } |
| 91 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 92 | static ssize_t iio_event_chrdev_read(struct file *filep, |
| 93 | char __user *buf, |
| 94 | size_t count, |
| 95 | loff_t *f_ps) |
| 96 | { |
Lars-Peter Clausen | cadc212 | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 97 | struct iio_dev *indio_dev = filep->private_data; |
| 98 | struct iio_event_interface *ev_int = indio_dev->event_interface; |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 99 | unsigned int copied; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 100 | int ret; |
| 101 | |
Lars-Peter Clausen | f18e7a0 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 102 | if (!indio_dev->info) |
| 103 | return -ENODEV; |
| 104 | |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 105 | if (count < sizeof(struct iio_event_data)) |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 106 | return -EINVAL; |
| 107 | |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 108 | spin_lock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 109 | if (kfifo_is_empty(&ev_int->det_events)) { |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 110 | if (filep->f_flags & O_NONBLOCK) { |
| 111 | ret = -EAGAIN; |
Lars-Peter Clausen | 43ba110 | 2012-01-03 14:59:40 +0100 | [diff] [blame] | 112 | goto error_unlock; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 113 | } |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 114 | /* Blocking on device; waiting for something to be there */ |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 115 | ret = wait_event_interruptible_locked_irq(ev_int->wait, |
Lars-Peter Clausen | d2f0a48 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 116 | !kfifo_is_empty(&ev_int->det_events) || |
| 117 | indio_dev->info == NULL); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 118 | if (ret) |
Lars-Peter Clausen | 43ba110 | 2012-01-03 14:59:40 +0100 | [diff] [blame] | 119 | goto error_unlock; |
Lars-Peter Clausen | d2f0a48 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 120 | if (indio_dev->info == NULL) { |
| 121 | ret = -ENODEV; |
| 122 | goto error_unlock; |
| 123 | } |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 124 | /* Single access device so no one else can get the data */ |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 127 | ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 128 | |
Lars-Peter Clausen | 43ba110 | 2012-01-03 14:59:40 +0100 | [diff] [blame] | 129 | error_unlock: |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 130 | spin_unlock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | 43ba110 | 2012-01-03 14:59:40 +0100 | [diff] [blame] | 131 | |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 132 | return ret ? ret : copied; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static int iio_event_chrdev_release(struct inode *inode, struct file *filep) |
| 136 | { |
Lars-Peter Clausen | cadc212 | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 137 | struct iio_dev *indio_dev = filep->private_data; |
| 138 | struct iio_event_interface *ev_int = indio_dev->event_interface; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 139 | |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 140 | spin_lock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | a046c1e | 2012-01-03 14:59:42 +0100 | [diff] [blame] | 141 | __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 142 | /* |
| 143 | * In order to maintain a clean state for reopening, |
| 144 | * clear out any awaiting events. The mask will prevent |
| 145 | * any new __iio_push_event calls running. |
| 146 | */ |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 147 | kfifo_reset_out(&ev_int->det_events); |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 148 | spin_unlock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 149 | |
Lars-Peter Clausen | cadc212 | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 150 | iio_device_put(indio_dev); |
| 151 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static const struct file_operations iio_event_chrdev_fileops = { |
| 156 | .read = iio_event_chrdev_read, |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 157 | .poll = iio_event_poll, |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 158 | .release = iio_event_chrdev_release, |
| 159 | .owner = THIS_MODULE, |
| 160 | .llseek = noop_llseek, |
| 161 | }; |
| 162 | |
| 163 | int iio_event_getfd(struct iio_dev *indio_dev) |
| 164 | { |
| 165 | struct iio_event_interface *ev_int = indio_dev->event_interface; |
| 166 | int fd; |
| 167 | |
| 168 | if (ev_int == NULL) |
| 169 | return -ENODEV; |
| 170 | |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 171 | spin_lock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | a046c1e | 2012-01-03 14:59:42 +0100 | [diff] [blame] | 172 | if (__test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) { |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 173 | spin_unlock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 174 | return -EBUSY; |
| 175 | } |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 176 | spin_unlock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | cadc212 | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 177 | iio_device_get(indio_dev); |
| 178 | |
| 179 | fd = anon_inode_getfd("iio:event", &iio_event_chrdev_fileops, |
Greg Kroah-Hartman | e2aad1d | 2013-09-25 08:59:04 -0700 | [diff] [blame] | 180 | indio_dev, O_RDONLY | O_CLOEXEC); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 181 | if (fd < 0) { |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 182 | spin_lock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | a046c1e | 2012-01-03 14:59:42 +0100 | [diff] [blame] | 183 | __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags); |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 184 | spin_unlock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | cadc212 | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 185 | iio_device_put(indio_dev); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 186 | } |
| 187 | return fd; |
| 188 | } |
| 189 | |
| 190 | static const char * const iio_ev_type_text[] = { |
| 191 | [IIO_EV_TYPE_THRESH] = "thresh", |
| 192 | [IIO_EV_TYPE_MAG] = "mag", |
| 193 | [IIO_EV_TYPE_ROC] = "roc", |
| 194 | [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive", |
| 195 | [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive", |
| 196 | }; |
| 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 | }; |
| 203 | |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 204 | static const char * const iio_ev_info_text[] = { |
| 205 | [IIO_EV_INFO_ENABLE] = "en", |
| 206 | [IIO_EV_INFO_VALUE] = "value", |
Lars-Peter Clausen | ec6670a | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 207 | [IIO_EV_INFO_HYSTERESIS] = "hysteresis", |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | static enum iio_event_direction iio_ev_attr_dir(struct iio_dev_attr *attr) |
| 211 | { |
| 212 | return attr->c->event_spec[attr->address & 0xffff].dir; |
| 213 | } |
| 214 | |
| 215 | static enum iio_event_type iio_ev_attr_type(struct iio_dev_attr *attr) |
| 216 | { |
| 217 | return attr->c->event_spec[attr->address & 0xffff].type; |
| 218 | } |
| 219 | |
| 220 | static enum iio_event_info iio_ev_attr_info(struct iio_dev_attr *attr) |
| 221 | { |
| 222 | return (attr->address >> 16) & 0xffff; |
| 223 | } |
| 224 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 225 | static ssize_t iio_ev_state_store(struct device *dev, |
| 226 | struct device_attribute *attr, |
| 227 | const char *buf, |
| 228 | size_t len) |
| 229 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 230 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 231 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 232 | int ret; |
| 233 | bool val; |
| 234 | |
| 235 | ret = strtobool(buf, &val); |
| 236 | if (ret < 0) |
| 237 | return ret; |
| 238 | |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 239 | if (indio_dev->info->write_event_config) |
| 240 | ret = indio_dev->info->write_event_config(indio_dev, |
| 241 | this_attr->address, val); |
| 242 | else |
| 243 | ret = indio_dev->info->write_event_config_new(indio_dev, |
| 244 | this_attr->c, iio_ev_attr_type(this_attr), |
| 245 | iio_ev_attr_dir(this_attr), val); |
| 246 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 247 | return (ret < 0) ? ret : len; |
| 248 | } |
| 249 | |
| 250 | static ssize_t iio_ev_state_show(struct device *dev, |
| 251 | struct device_attribute *attr, |
| 252 | char *buf) |
| 253 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 254 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 255 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 256 | int val; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 257 | |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 258 | if (indio_dev->info->read_event_config) |
| 259 | val = indio_dev->info->read_event_config(indio_dev, |
| 260 | this_attr->address); |
| 261 | else |
| 262 | val = indio_dev->info->read_event_config_new(indio_dev, |
| 263 | this_attr->c, iio_ev_attr_type(this_attr), |
| 264 | iio_ev_attr_dir(this_attr)); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 265 | if (val < 0) |
| 266 | return val; |
| 267 | else |
| 268 | return sprintf(buf, "%d\n", val); |
| 269 | } |
| 270 | |
| 271 | static ssize_t iio_ev_value_show(struct device *dev, |
| 272 | struct device_attribute *attr, |
| 273 | char *buf) |
| 274 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 275 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 276 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 277 | int val, val2; |
| 278 | int ret; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 279 | |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 280 | if (indio_dev->info->read_event_value) { |
| 281 | ret = indio_dev->info->read_event_value(indio_dev, |
| 282 | this_attr->address, &val); |
| 283 | if (ret < 0) |
| 284 | return ret; |
| 285 | return sprintf(buf, "%d\n", val); |
| 286 | } else { |
| 287 | ret = indio_dev->info->read_event_value_new(indio_dev, |
| 288 | this_attr->c, iio_ev_attr_type(this_attr), |
| 289 | iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr), |
| 290 | &val, &val2); |
| 291 | if (ret < 0) |
| 292 | return ret; |
| 293 | return iio_format_value(buf, ret, val, val2); |
| 294 | } |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static ssize_t iio_ev_value_store(struct device *dev, |
| 298 | struct device_attribute *attr, |
| 299 | const char *buf, |
| 300 | size_t len) |
| 301 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 302 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 303 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 304 | int val, val2; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 305 | int ret; |
| 306 | |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 307 | if (!indio_dev->info->write_event_value && |
| 308 | !indio_dev->info->write_event_value_new) |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 309 | return -EINVAL; |
| 310 | |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 311 | if (indio_dev->info->write_event_value) { |
| 312 | ret = kstrtoint(buf, 10, &val); |
| 313 | if (ret) |
| 314 | return ret; |
| 315 | ret = indio_dev->info->write_event_value(indio_dev, |
| 316 | this_attr->address, val); |
| 317 | } else { |
| 318 | ret = iio_str_to_fixpoint(buf, 100000, &val, &val2); |
| 319 | if (ret) |
| 320 | return ret; |
| 321 | ret = indio_dev->info->write_event_value_new(indio_dev, |
| 322 | this_attr->c, iio_ev_attr_type(this_attr), |
| 323 | iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr), |
| 324 | val, val2); |
| 325 | } |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 326 | if (ret < 0) |
| 327 | return ret; |
| 328 | |
| 329 | return len; |
| 330 | } |
| 331 | |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 332 | static int iio_device_add_event(struct iio_dev *indio_dev, |
| 333 | const struct iio_chan_spec *chan, unsigned int spec_index, |
| 334 | enum iio_event_type type, enum iio_event_direction dir, |
| 335 | enum iio_shared_by shared_by, const unsigned long *mask) |
| 336 | { |
| 337 | ssize_t (*show)(struct device *, struct device_attribute *, char *); |
| 338 | ssize_t (*store)(struct device *, struct device_attribute *, |
| 339 | const char *, size_t); |
| 340 | unsigned int attrcount = 0; |
| 341 | unsigned int i; |
| 342 | char *postfix; |
| 343 | int ret; |
| 344 | |
| 345 | for_each_set_bit(i, mask, sizeof(*mask)) { |
| 346 | postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", |
| 347 | iio_ev_type_text[type], iio_ev_dir_text[dir], |
| 348 | iio_ev_info_text[i]); |
| 349 | if (postfix == NULL) |
| 350 | return -ENOMEM; |
| 351 | |
| 352 | if (i == IIO_EV_INFO_ENABLE) { |
| 353 | show = iio_ev_state_show; |
| 354 | store = iio_ev_state_store; |
| 355 | } else { |
| 356 | show = iio_ev_value_show; |
| 357 | store = iio_ev_value_store; |
| 358 | } |
| 359 | |
| 360 | ret = __iio_add_chan_devattr(postfix, chan, show, store, |
| 361 | (i << 16) | spec_index, shared_by, &indio_dev->dev, |
| 362 | &indio_dev->event_interface->dev_attr_list); |
| 363 | kfree(postfix); |
| 364 | |
| 365 | if (ret) |
| 366 | return ret; |
| 367 | |
| 368 | attrcount++; |
| 369 | } |
| 370 | |
| 371 | return attrcount; |
| 372 | } |
| 373 | |
| 374 | static int iio_device_add_event_sysfs_new(struct iio_dev *indio_dev, |
| 375 | struct iio_chan_spec const *chan) |
| 376 | { |
| 377 | int ret = 0, i, attrcount = 0; |
| 378 | enum iio_event_direction dir; |
| 379 | enum iio_event_type type; |
| 380 | |
| 381 | for (i = 0; i < chan->num_event_specs; i++) { |
| 382 | type = chan->event_spec[i].type; |
| 383 | dir = chan->event_spec[i].dir; |
| 384 | |
| 385 | ret = iio_device_add_event(indio_dev, chan, i, type, dir, |
| 386 | IIO_SEPARATE, &chan->event_spec[i].mask_separate); |
| 387 | if (ret < 0) |
| 388 | goto error_ret; |
| 389 | attrcount += ret; |
| 390 | |
| 391 | ret = iio_device_add_event(indio_dev, chan, i, type, dir, |
| 392 | IIO_SHARED_BY_TYPE, |
| 393 | &chan->event_spec[i].mask_shared_by_type); |
| 394 | if (ret < 0) |
| 395 | goto error_ret; |
| 396 | attrcount += ret; |
| 397 | |
| 398 | ret = iio_device_add_event(indio_dev, chan, i, type, dir, |
| 399 | IIO_SHARED_BY_DIR, |
| 400 | &chan->event_spec[i].mask_shared_by_dir); |
| 401 | if (ret < 0) |
| 402 | goto error_ret; |
| 403 | attrcount += ret; |
| 404 | |
| 405 | ret = iio_device_add_event(indio_dev, chan, i, type, dir, |
| 406 | IIO_SHARED_BY_ALL, |
| 407 | &chan->event_spec[i].mask_shared_by_all); |
| 408 | if (ret < 0) |
| 409 | goto error_ret; |
| 410 | attrcount += ret; |
| 411 | } |
| 412 | ret = attrcount; |
| 413 | error_ret: |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | static int iio_device_add_event_sysfs_old(struct iio_dev *indio_dev, |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 418 | struct iio_chan_spec const *chan) |
| 419 | { |
| 420 | int ret = 0, i, attrcount = 0; |
| 421 | u64 mask = 0; |
| 422 | char *postfix; |
| 423 | if (!chan->event_mask) |
| 424 | return 0; |
| 425 | |
| 426 | for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) { |
| 427 | postfix = kasprintf(GFP_KERNEL, "%s_%s_en", |
| 428 | iio_ev_type_text[i/IIO_EV_DIR_MAX], |
| 429 | iio_ev_dir_text[i%IIO_EV_DIR_MAX]); |
| 430 | if (postfix == NULL) { |
| 431 | ret = -ENOMEM; |
| 432 | goto error_ret; |
| 433 | } |
| 434 | if (chan->modified) |
Lukasz Czerwinski | 2b0774d | 2013-09-18 10:21:00 +0100 | [diff] [blame] | 435 | mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel2, |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 436 | i/IIO_EV_DIR_MAX, |
| 437 | i%IIO_EV_DIR_MAX); |
| 438 | else if (chan->differential) |
| 439 | mask = IIO_EVENT_CODE(chan->type, |
| 440 | 0, 0, |
| 441 | i%IIO_EV_DIR_MAX, |
| 442 | i/IIO_EV_DIR_MAX, |
| 443 | 0, |
| 444 | chan->channel, |
| 445 | chan->channel2); |
| 446 | else |
| 447 | mask = IIO_UNMOD_EVENT_CODE(chan->type, |
| 448 | chan->channel, |
| 449 | i/IIO_EV_DIR_MAX, |
| 450 | i%IIO_EV_DIR_MAX); |
| 451 | |
| 452 | ret = __iio_add_chan_devattr(postfix, |
| 453 | chan, |
| 454 | &iio_ev_state_show, |
| 455 | iio_ev_state_store, |
| 456 | mask, |
| 457 | 0, |
| 458 | &indio_dev->dev, |
| 459 | &indio_dev->event_interface-> |
| 460 | dev_attr_list); |
| 461 | kfree(postfix); |
| 462 | if (ret) |
| 463 | goto error_ret; |
| 464 | attrcount++; |
| 465 | postfix = kasprintf(GFP_KERNEL, "%s_%s_value", |
| 466 | iio_ev_type_text[i/IIO_EV_DIR_MAX], |
| 467 | iio_ev_dir_text[i%IIO_EV_DIR_MAX]); |
| 468 | if (postfix == NULL) { |
| 469 | ret = -ENOMEM; |
| 470 | goto error_ret; |
| 471 | } |
| 472 | ret = __iio_add_chan_devattr(postfix, chan, |
| 473 | iio_ev_value_show, |
| 474 | iio_ev_value_store, |
| 475 | mask, |
| 476 | 0, |
| 477 | &indio_dev->dev, |
| 478 | &indio_dev->event_interface-> |
| 479 | dev_attr_list); |
| 480 | kfree(postfix); |
| 481 | if (ret) |
| 482 | goto error_ret; |
| 483 | attrcount++; |
| 484 | } |
| 485 | ret = attrcount; |
| 486 | error_ret: |
| 487 | return ret; |
| 488 | } |
| 489 | |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 490 | |
| 491 | static int iio_device_add_event_sysfs(struct iio_dev *indio_dev, |
| 492 | struct iio_chan_spec const *chan) |
| 493 | { |
| 494 | if (chan->event_mask) |
| 495 | return iio_device_add_event_sysfs_old(indio_dev, chan); |
| 496 | else |
| 497 | return iio_device_add_event_sysfs_new(indio_dev, chan); |
| 498 | } |
| 499 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 500 | static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev) |
| 501 | { |
| 502 | int j, ret, attrcount = 0; |
| 503 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 504 | /* Dynically created from the channels array */ |
| 505 | for (j = 0; j < indio_dev->num_channels; j++) { |
| 506 | ret = iio_device_add_event_sysfs(indio_dev, |
| 507 | &indio_dev->channels[j]); |
| 508 | if (ret < 0) |
Julia Lawall | e3db9ef | 2012-10-21 11:52:00 +0100 | [diff] [blame] | 509 | return ret; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 510 | attrcount += ret; |
| 511 | } |
| 512 | return attrcount; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev) |
| 516 | { |
| 517 | int j; |
| 518 | |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 519 | for (j = 0; j < indio_dev->num_channels; j++) { |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 520 | if (indio_dev->channels[j].event_mask != 0) |
| 521 | return true; |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 522 | if (indio_dev->channels[j].num_event_specs != 0) |
| 523 | return true; |
| 524 | } |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 525 | return false; |
| 526 | } |
| 527 | |
| 528 | static void iio_setup_ev_int(struct iio_event_interface *ev_int) |
| 529 | { |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 530 | INIT_KFIFO(ev_int->det_events); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 531 | init_waitqueue_head(&ev_int->wait); |
| 532 | } |
| 533 | |
| 534 | static const char *iio_event_group_name = "events"; |
| 535 | int iio_device_register_eventset(struct iio_dev *indio_dev) |
| 536 | { |
| 537 | struct iio_dev_attr *p; |
| 538 | int ret = 0, attrcount_orig = 0, attrcount, attrn; |
| 539 | struct attribute **attr; |
| 540 | |
| 541 | if (!(indio_dev->info->event_attrs || |
| 542 | iio_check_for_dynamic_events(indio_dev))) |
| 543 | return 0; |
| 544 | |
| 545 | indio_dev->event_interface = |
| 546 | kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL); |
| 547 | if (indio_dev->event_interface == NULL) { |
| 548 | ret = -ENOMEM; |
| 549 | goto error_ret; |
| 550 | } |
| 551 | |
Sascha Hauer | 46b2431 | 2012-07-03 10:55:40 +0200 | [diff] [blame] | 552 | INIT_LIST_HEAD(&indio_dev->event_interface->dev_attr_list); |
| 553 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 554 | iio_setup_ev_int(indio_dev->event_interface); |
| 555 | if (indio_dev->info->event_attrs != NULL) { |
| 556 | attr = indio_dev->info->event_attrs->attrs; |
| 557 | while (*attr++ != NULL) |
| 558 | attrcount_orig++; |
| 559 | } |
| 560 | attrcount = attrcount_orig; |
| 561 | if (indio_dev->channels) { |
| 562 | ret = __iio_add_event_config_attrs(indio_dev); |
| 563 | if (ret < 0) |
| 564 | goto error_free_setup_event_lines; |
| 565 | attrcount += ret; |
| 566 | } |
| 567 | |
| 568 | indio_dev->event_interface->group.name = iio_event_group_name; |
| 569 | indio_dev->event_interface->group.attrs = kcalloc(attrcount + 1, |
| 570 | sizeof(indio_dev->event_interface->group.attrs[0]), |
| 571 | GFP_KERNEL); |
| 572 | if (indio_dev->event_interface->group.attrs == NULL) { |
| 573 | ret = -ENOMEM; |
| 574 | goto error_free_setup_event_lines; |
| 575 | } |
| 576 | if (indio_dev->info->event_attrs) |
| 577 | memcpy(indio_dev->event_interface->group.attrs, |
| 578 | indio_dev->info->event_attrs->attrs, |
| 579 | sizeof(indio_dev->event_interface->group.attrs[0]) |
| 580 | *attrcount_orig); |
| 581 | attrn = attrcount_orig; |
| 582 | /* Add all elements from the list. */ |
| 583 | list_for_each_entry(p, |
| 584 | &indio_dev->event_interface->dev_attr_list, |
| 585 | l) |
| 586 | indio_dev->event_interface->group.attrs[attrn++] = |
| 587 | &p->dev_attr.attr; |
| 588 | indio_dev->groups[indio_dev->groupcounter++] = |
| 589 | &indio_dev->event_interface->group; |
| 590 | |
| 591 | return 0; |
| 592 | |
| 593 | error_free_setup_event_lines: |
Lars-Peter Clausen | 84088eb | 2013-10-07 12:50:00 +0100 | [diff] [blame] | 594 | iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 595 | kfree(indio_dev->event_interface); |
| 596 | error_ret: |
| 597 | |
| 598 | return ret; |
| 599 | } |
| 600 | |
Lars-Peter Clausen | d2f0a48 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 601 | /** |
| 602 | * iio_device_wakeup_eventset - Wakes up the event waitqueue |
| 603 | * @indio_dev: The IIO device |
| 604 | * |
| 605 | * Wakes up the event waitqueue used for poll() and blocking read(). |
| 606 | * Should usually be called when the device is unregistered. |
| 607 | */ |
| 608 | void iio_device_wakeup_eventset(struct iio_dev *indio_dev) |
| 609 | { |
| 610 | if (indio_dev->event_interface == NULL) |
| 611 | return; |
| 612 | wake_up(&indio_dev->event_interface->wait); |
| 613 | } |
| 614 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 615 | void iio_device_unregister_eventset(struct iio_dev *indio_dev) |
| 616 | { |
| 617 | if (indio_dev->event_interface == NULL) |
| 618 | return; |
Lars-Peter Clausen | 84088eb | 2013-10-07 12:50:00 +0100 | [diff] [blame] | 619 | iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 620 | kfree(indio_dev->event_interface->group.attrs); |
| 621 | kfree(indio_dev->event_interface); |
| 622 | } |