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 | |
| 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 | { |
| 75 | struct iio_event_interface *ev_int = filep->private_data; |
| 76 | unsigned int events = 0; |
| 77 | |
| 78 | poll_wait(filep, &ev_int->wait, wait); |
| 79 | |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 80 | spin_lock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 81 | if (!kfifo_is_empty(&ev_int->det_events)) |
| 82 | events = POLLIN | POLLRDNORM; |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 83 | spin_unlock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 84 | |
| 85 | return events; |
| 86 | } |
| 87 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 88 | static ssize_t iio_event_chrdev_read(struct file *filep, |
| 89 | char __user *buf, |
| 90 | size_t count, |
| 91 | loff_t *f_ps) |
| 92 | { |
| 93 | struct iio_event_interface *ev_int = filep->private_data; |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 94 | unsigned int copied; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 95 | int ret; |
| 96 | |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 97 | if (count < sizeof(struct iio_event_data)) |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 98 | return -EINVAL; |
| 99 | |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 100 | spin_lock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 101 | if (kfifo_is_empty(&ev_int->det_events)) { |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 102 | if (filep->f_flags & O_NONBLOCK) { |
| 103 | ret = -EAGAIN; |
Lars-Peter Clausen | 43ba110 | 2012-01-03 14:59:40 +0100 | [diff] [blame] | 104 | goto error_unlock; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 105 | } |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 106 | /* Blocking on device; waiting for something to be there */ |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 107 | ret = wait_event_interruptible_locked_irq(ev_int->wait, |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 108 | !kfifo_is_empty(&ev_int->det_events)); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 109 | if (ret) |
Lars-Peter Clausen | 43ba110 | 2012-01-03 14:59:40 +0100 | [diff] [blame] | 110 | goto error_unlock; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 111 | /* Single access device so no one else can get the data */ |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 114 | ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 115 | |
Lars-Peter Clausen | 43ba110 | 2012-01-03 14:59:40 +0100 | [diff] [blame] | 116 | error_unlock: |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 117 | spin_unlock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | 43ba110 | 2012-01-03 14:59:40 +0100 | [diff] [blame] | 118 | |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 119 | return ret ? ret : copied; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static int iio_event_chrdev_release(struct inode *inode, struct file *filep) |
| 123 | { |
| 124 | struct iio_event_interface *ev_int = filep->private_data; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 125 | |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 126 | spin_lock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | a046c1e | 2012-01-03 14:59:42 +0100 | [diff] [blame] | 127 | __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 128 | /* |
| 129 | * In order to maintain a clean state for reopening, |
| 130 | * clear out any awaiting events. The mask will prevent |
| 131 | * any new __iio_push_event calls running. |
| 132 | */ |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 133 | kfifo_reset_out(&ev_int->det_events); |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 134 | spin_unlock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static const struct file_operations iio_event_chrdev_fileops = { |
| 140 | .read = iio_event_chrdev_read, |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 141 | .poll = iio_event_poll, |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 142 | .release = iio_event_chrdev_release, |
| 143 | .owner = THIS_MODULE, |
| 144 | .llseek = noop_llseek, |
| 145 | }; |
| 146 | |
| 147 | int iio_event_getfd(struct iio_dev *indio_dev) |
| 148 | { |
| 149 | struct iio_event_interface *ev_int = indio_dev->event_interface; |
| 150 | int fd; |
| 151 | |
| 152 | if (ev_int == NULL) |
| 153 | return -ENODEV; |
| 154 | |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 155 | spin_lock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | a046c1e | 2012-01-03 14:59:42 +0100 | [diff] [blame] | 156 | 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] | 157 | spin_unlock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 158 | return -EBUSY; |
| 159 | } |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 160 | spin_unlock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 161 | fd = anon_inode_getfd("iio:event", |
| 162 | &iio_event_chrdev_fileops, ev_int, O_RDONLY); |
| 163 | if (fd < 0) { |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 164 | spin_lock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | a046c1e | 2012-01-03 14:59:42 +0100 | [diff] [blame] | 165 | __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags); |
Lars-Peter Clausen | 1b9dc91 | 2013-03-07 19:53:00 +0000 | [diff] [blame] | 166 | spin_unlock_irq(&ev_int->wait.lock); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 167 | } |
| 168 | return fd; |
| 169 | } |
| 170 | |
| 171 | static const char * const iio_ev_type_text[] = { |
| 172 | [IIO_EV_TYPE_THRESH] = "thresh", |
| 173 | [IIO_EV_TYPE_MAG] = "mag", |
| 174 | [IIO_EV_TYPE_ROC] = "roc", |
| 175 | [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive", |
| 176 | [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive", |
| 177 | }; |
| 178 | |
| 179 | static const char * const iio_ev_dir_text[] = { |
| 180 | [IIO_EV_DIR_EITHER] = "either", |
| 181 | [IIO_EV_DIR_RISING] = "rising", |
| 182 | [IIO_EV_DIR_FALLING] = "falling" |
| 183 | }; |
| 184 | |
| 185 | static ssize_t iio_ev_state_store(struct device *dev, |
| 186 | struct device_attribute *attr, |
| 187 | const char *buf, |
| 188 | size_t len) |
| 189 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 190 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 191 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 192 | int ret; |
| 193 | bool val; |
| 194 | |
| 195 | ret = strtobool(buf, &val); |
| 196 | if (ret < 0) |
| 197 | return ret; |
| 198 | |
| 199 | ret = indio_dev->info->write_event_config(indio_dev, |
| 200 | this_attr->address, |
| 201 | val); |
| 202 | return (ret < 0) ? ret : len; |
| 203 | } |
| 204 | |
| 205 | static ssize_t iio_ev_state_show(struct device *dev, |
| 206 | struct device_attribute *attr, |
| 207 | char *buf) |
| 208 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 209 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 210 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 211 | int val = indio_dev->info->read_event_config(indio_dev, |
| 212 | this_attr->address); |
| 213 | |
| 214 | if (val < 0) |
| 215 | return val; |
| 216 | else |
| 217 | return sprintf(buf, "%d\n", val); |
| 218 | } |
| 219 | |
| 220 | static ssize_t iio_ev_value_show(struct device *dev, |
| 221 | struct device_attribute *attr, |
| 222 | char *buf) |
| 223 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 224 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 225 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 226 | int val, ret; |
| 227 | |
| 228 | ret = indio_dev->info->read_event_value(indio_dev, |
| 229 | this_attr->address, &val); |
| 230 | if (ret < 0) |
| 231 | return ret; |
| 232 | |
| 233 | return sprintf(buf, "%d\n", val); |
| 234 | } |
| 235 | |
| 236 | static ssize_t iio_ev_value_store(struct device *dev, |
| 237 | struct device_attribute *attr, |
| 238 | const char *buf, |
| 239 | size_t len) |
| 240 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 241 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 242 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Lars-Peter Clausen | 948ad20 | 2012-10-18 14:47:00 +0100 | [diff] [blame] | 243 | int val; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 244 | int ret; |
| 245 | |
| 246 | if (!indio_dev->info->write_event_value) |
| 247 | return -EINVAL; |
| 248 | |
Lars-Peter Clausen | 948ad20 | 2012-10-18 14:47:00 +0100 | [diff] [blame] | 249 | ret = kstrtoint(buf, 10, &val); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 250 | if (ret) |
| 251 | return ret; |
| 252 | |
| 253 | ret = indio_dev->info->write_event_value(indio_dev, this_attr->address, |
| 254 | val); |
| 255 | if (ret < 0) |
| 256 | return ret; |
| 257 | |
| 258 | return len; |
| 259 | } |
| 260 | |
| 261 | static int iio_device_add_event_sysfs(struct iio_dev *indio_dev, |
| 262 | struct iio_chan_spec const *chan) |
| 263 | { |
| 264 | int ret = 0, i, attrcount = 0; |
| 265 | u64 mask = 0; |
| 266 | char *postfix; |
| 267 | if (!chan->event_mask) |
| 268 | return 0; |
| 269 | |
| 270 | for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) { |
| 271 | postfix = kasprintf(GFP_KERNEL, "%s_%s_en", |
| 272 | iio_ev_type_text[i/IIO_EV_DIR_MAX], |
| 273 | iio_ev_dir_text[i%IIO_EV_DIR_MAX]); |
| 274 | if (postfix == NULL) { |
| 275 | ret = -ENOMEM; |
| 276 | goto error_ret; |
| 277 | } |
| 278 | if (chan->modified) |
| 279 | mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel, |
| 280 | i/IIO_EV_DIR_MAX, |
| 281 | i%IIO_EV_DIR_MAX); |
| 282 | else if (chan->differential) |
| 283 | mask = IIO_EVENT_CODE(chan->type, |
| 284 | 0, 0, |
| 285 | i%IIO_EV_DIR_MAX, |
| 286 | i/IIO_EV_DIR_MAX, |
| 287 | 0, |
| 288 | chan->channel, |
| 289 | chan->channel2); |
| 290 | else |
| 291 | mask = IIO_UNMOD_EVENT_CODE(chan->type, |
| 292 | chan->channel, |
| 293 | i/IIO_EV_DIR_MAX, |
| 294 | i%IIO_EV_DIR_MAX); |
| 295 | |
| 296 | ret = __iio_add_chan_devattr(postfix, |
| 297 | chan, |
| 298 | &iio_ev_state_show, |
| 299 | iio_ev_state_store, |
| 300 | mask, |
| 301 | 0, |
| 302 | &indio_dev->dev, |
| 303 | &indio_dev->event_interface-> |
| 304 | dev_attr_list); |
| 305 | kfree(postfix); |
| 306 | if (ret) |
| 307 | goto error_ret; |
| 308 | attrcount++; |
| 309 | postfix = kasprintf(GFP_KERNEL, "%s_%s_value", |
| 310 | iio_ev_type_text[i/IIO_EV_DIR_MAX], |
| 311 | iio_ev_dir_text[i%IIO_EV_DIR_MAX]); |
| 312 | if (postfix == NULL) { |
| 313 | ret = -ENOMEM; |
| 314 | goto error_ret; |
| 315 | } |
| 316 | ret = __iio_add_chan_devattr(postfix, chan, |
| 317 | iio_ev_value_show, |
| 318 | iio_ev_value_store, |
| 319 | mask, |
| 320 | 0, |
| 321 | &indio_dev->dev, |
| 322 | &indio_dev->event_interface-> |
| 323 | dev_attr_list); |
| 324 | kfree(postfix); |
| 325 | if (ret) |
| 326 | goto error_ret; |
| 327 | attrcount++; |
| 328 | } |
| 329 | ret = attrcount; |
| 330 | error_ret: |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | static inline void __iio_remove_event_config_attrs(struct iio_dev *indio_dev) |
| 335 | { |
| 336 | struct iio_dev_attr *p, *n; |
| 337 | list_for_each_entry_safe(p, n, |
| 338 | &indio_dev->event_interface-> |
| 339 | dev_attr_list, l) { |
| 340 | kfree(p->dev_attr.attr.name); |
| 341 | kfree(p); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev) |
| 346 | { |
| 347 | int j, ret, attrcount = 0; |
| 348 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 349 | /* Dynically created from the channels array */ |
| 350 | for (j = 0; j < indio_dev->num_channels; j++) { |
| 351 | ret = iio_device_add_event_sysfs(indio_dev, |
| 352 | &indio_dev->channels[j]); |
| 353 | if (ret < 0) |
Julia Lawall | e3db9ef | 2012-10-21 11:52:00 +0100 | [diff] [blame] | 354 | return ret; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 355 | attrcount += ret; |
| 356 | } |
| 357 | return attrcount; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev) |
| 361 | { |
| 362 | int j; |
| 363 | |
| 364 | for (j = 0; j < indio_dev->num_channels; j++) |
| 365 | if (indio_dev->channels[j].event_mask != 0) |
| 366 | return true; |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | static void iio_setup_ev_int(struct iio_event_interface *ev_int) |
| 371 | { |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 372 | INIT_KFIFO(ev_int->det_events); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 373 | init_waitqueue_head(&ev_int->wait); |
| 374 | } |
| 375 | |
| 376 | static const char *iio_event_group_name = "events"; |
| 377 | int iio_device_register_eventset(struct iio_dev *indio_dev) |
| 378 | { |
| 379 | struct iio_dev_attr *p; |
| 380 | int ret = 0, attrcount_orig = 0, attrcount, attrn; |
| 381 | struct attribute **attr; |
| 382 | |
| 383 | if (!(indio_dev->info->event_attrs || |
| 384 | iio_check_for_dynamic_events(indio_dev))) |
| 385 | return 0; |
| 386 | |
| 387 | indio_dev->event_interface = |
| 388 | kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL); |
| 389 | if (indio_dev->event_interface == NULL) { |
| 390 | ret = -ENOMEM; |
| 391 | goto error_ret; |
| 392 | } |
| 393 | |
Sascha Hauer | 46b2431 | 2012-07-03 10:55:40 +0200 | [diff] [blame] | 394 | INIT_LIST_HEAD(&indio_dev->event_interface->dev_attr_list); |
| 395 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 396 | iio_setup_ev_int(indio_dev->event_interface); |
| 397 | if (indio_dev->info->event_attrs != NULL) { |
| 398 | attr = indio_dev->info->event_attrs->attrs; |
| 399 | while (*attr++ != NULL) |
| 400 | attrcount_orig++; |
| 401 | } |
| 402 | attrcount = attrcount_orig; |
| 403 | if (indio_dev->channels) { |
| 404 | ret = __iio_add_event_config_attrs(indio_dev); |
| 405 | if (ret < 0) |
| 406 | goto error_free_setup_event_lines; |
| 407 | attrcount += ret; |
| 408 | } |
| 409 | |
| 410 | indio_dev->event_interface->group.name = iio_event_group_name; |
| 411 | indio_dev->event_interface->group.attrs = kcalloc(attrcount + 1, |
| 412 | sizeof(indio_dev->event_interface->group.attrs[0]), |
| 413 | GFP_KERNEL); |
| 414 | if (indio_dev->event_interface->group.attrs == NULL) { |
| 415 | ret = -ENOMEM; |
| 416 | goto error_free_setup_event_lines; |
| 417 | } |
| 418 | if (indio_dev->info->event_attrs) |
| 419 | memcpy(indio_dev->event_interface->group.attrs, |
| 420 | indio_dev->info->event_attrs->attrs, |
| 421 | sizeof(indio_dev->event_interface->group.attrs[0]) |
| 422 | *attrcount_orig); |
| 423 | attrn = attrcount_orig; |
| 424 | /* Add all elements from the list. */ |
| 425 | list_for_each_entry(p, |
| 426 | &indio_dev->event_interface->dev_attr_list, |
| 427 | l) |
| 428 | indio_dev->event_interface->group.attrs[attrn++] = |
| 429 | &p->dev_attr.attr; |
| 430 | indio_dev->groups[indio_dev->groupcounter++] = |
| 431 | &indio_dev->event_interface->group; |
| 432 | |
| 433 | return 0; |
| 434 | |
| 435 | error_free_setup_event_lines: |
| 436 | __iio_remove_event_config_attrs(indio_dev); |
| 437 | kfree(indio_dev->event_interface); |
| 438 | error_ret: |
| 439 | |
| 440 | return ret; |
| 441 | } |
| 442 | |
| 443 | void iio_device_unregister_eventset(struct iio_dev *indio_dev) |
| 444 | { |
| 445 | if (indio_dev->event_interface == NULL) |
| 446 | return; |
| 447 | __iio_remove_event_config_attrs(indio_dev); |
| 448 | kfree(indio_dev->event_interface->group.attrs); |
| 449 | kfree(indio_dev->event_interface); |
| 450 | } |