Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1 | /* The industrial I/O core |
| 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/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/idr.h> |
| 15 | #include <linux/kdev_t.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/fs.h> |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 19 | #include <linux/poll.h> |
Jonathan Cameron | ffc18af | 2009-10-12 19:18:09 +0100 | [diff] [blame] | 20 | #include <linux/sched.h> |
Jeff Mahoney | 4439c93 | 2009-10-12 17:10:34 -0400 | [diff] [blame] | 21 | #include <linux/wait.h> |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 22 | #include <linux/cdev.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 24 | #include "iio.h" |
| 25 | #include "trigger_consumer.h" |
| 26 | |
| 27 | #define IIO_ID_PREFIX "device" |
| 28 | #define IIO_ID_FORMAT IIO_ID_PREFIX "%d" |
| 29 | |
| 30 | /* IDR to assign each registered device a unique id*/ |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 31 | static DEFINE_IDA(iio_ida); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 32 | /* IDR to allocate character device minor numbers */ |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 33 | static DEFINE_IDA(iio_chrdev_ida); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 34 | /* Lock used to protect both of the above */ |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 35 | static DEFINE_SPINLOCK(iio_ida_lock); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 36 | |
| 37 | dev_t iio_devt; |
| 38 | EXPORT_SYMBOL(iio_devt); |
| 39 | |
| 40 | #define IIO_DEV_MAX 256 |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 41 | struct bus_type iio_bus_type = { |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 42 | .name = "iio", |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 43 | }; |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 44 | EXPORT_SYMBOL(iio_bus_type); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 45 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 46 | static const char * const iio_chan_type_name_spec_shared[] = { |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 47 | [IIO_IN] = "in", |
Michael Hennerich | ae19178 | 2011-06-10 15:40:48 +0200 | [diff] [blame] | 48 | [IIO_OUT] = "out", |
Michael Hennerich | faf290e | 2011-05-18 14:42:02 +0100 | [diff] [blame] | 49 | [IIO_CURRENT] = "current", |
| 50 | [IIO_POWER] = "power", |
Bryan Freed | 9bff02f | 2011-07-07 12:01:54 -0700 | [diff] [blame] | 51 | [IIO_ACCEL] = "accel", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 52 | [IIO_IN_DIFF] = "in-in", |
| 53 | [IIO_GYRO] = "gyro", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 54 | [IIO_MAGN] = "magn", |
Bryan Freed | 9bff02f | 2011-07-07 12:01:54 -0700 | [diff] [blame] | 55 | [IIO_LIGHT] = "illuminance", |
| 56 | [IIO_INTENSITY] = "intensity", |
Bryan Freed | f09f2c8 | 2011-07-07 12:01:55 -0700 | [diff] [blame] | 57 | [IIO_PROXIMITY] = "proximity", |
Bryan Freed | 9bff02f | 2011-07-07 12:01:54 -0700 | [diff] [blame] | 58 | [IIO_TEMP] = "temp", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 59 | [IIO_INCLI] = "incli", |
| 60 | [IIO_ROT] = "rot", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 61 | [IIO_ANGL] = "angl", |
Bryan Freed | 9bff02f | 2011-07-07 12:01:54 -0700 | [diff] [blame] | 62 | [IIO_TIMESTAMP] = "timestamp", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | static const char * const iio_chan_type_name_spec_complex[] = { |
| 66 | [IIO_IN_DIFF] = "in%d-in%d", |
| 67 | }; |
| 68 | |
| 69 | static const char * const iio_modifier_names_light[] = { |
| 70 | [IIO_MOD_LIGHT_BOTH] = "both", |
| 71 | [IIO_MOD_LIGHT_IR] = "ir", |
| 72 | }; |
| 73 | |
| 74 | static const char * const iio_modifier_names_axial[] = { |
| 75 | [IIO_MOD_X] = "x", |
| 76 | [IIO_MOD_Y] = "y", |
| 77 | [IIO_MOD_Z] = "z", |
| 78 | }; |
| 79 | |
| 80 | /* relies on pairs of these shared then separate */ |
| 81 | static const char * const iio_chan_info_postfix[] = { |
| 82 | [IIO_CHAN_INFO_SCALE_SHARED/2] = "scale", |
| 83 | [IIO_CHAN_INFO_OFFSET_SHARED/2] = "offset", |
| 84 | [IIO_CHAN_INFO_CALIBSCALE_SHARED/2] = "calibscale", |
| 85 | [IIO_CHAN_INFO_CALIBBIAS_SHARED/2] = "calibbias", |
Jonathan Cameron | eb7fea5 | 2011-05-18 14:41:57 +0100 | [diff] [blame] | 86 | [IIO_CHAN_INFO_PEAK_SHARED/2] = "peak_raw", |
| 87 | [IIO_CHAN_INFO_PEAK_SCALE_SHARED/2] = "peak_scale", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 88 | }; |
| 89 | |
Jonathan Cameron | aaf370d | 2011-05-18 14:41:16 +0100 | [diff] [blame] | 90 | int iio_push_event(struct iio_dev *dev_info, |
| 91 | int ev_line, |
| 92 | int ev_code, |
| 93 | s64 timestamp) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 94 | { |
Jonathan Cameron | aaf370d | 2011-05-18 14:41:16 +0100 | [diff] [blame] | 95 | struct iio_event_interface *ev_int |
| 96 | = &dev_info->event_interfaces[ev_line]; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 97 | struct iio_detected_event_list *ev; |
| 98 | int ret = 0; |
| 99 | |
| 100 | /* Does anyone care? */ |
| 101 | mutex_lock(&ev_int->event_list_lock); |
| 102 | if (test_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags)) { |
Jonathan Cameron | 75c8075 | 2010-01-09 16:57:34 +0000 | [diff] [blame] | 103 | if (ev_int->current_events == ev_int->max_events) { |
| 104 | mutex_unlock(&ev_int->event_list_lock); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 105 | return 0; |
Jonathan Cameron | 75c8075 | 2010-01-09 16:57:34 +0000 | [diff] [blame] | 106 | } |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 107 | ev = kmalloc(sizeof(*ev), GFP_KERNEL); |
| 108 | if (ev == NULL) { |
| 109 | ret = -ENOMEM; |
Jonathan Cameron | 75c8075 | 2010-01-09 16:57:34 +0000 | [diff] [blame] | 110 | mutex_unlock(&ev_int->event_list_lock); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 111 | goto error_ret; |
| 112 | } |
| 113 | ev->ev.id = ev_code; |
| 114 | ev->ev.timestamp = timestamp; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 115 | |
Jonathan Cameron | 3b8ebfb | 2011-05-18 14:42:21 +0100 | [diff] [blame] | 116 | list_add_tail(&ev->list, &ev_int->det_events); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 117 | ev_int->current_events++; |
| 118 | mutex_unlock(&ev_int->event_list_lock); |
| 119 | wake_up_interruptible(&ev_int->wait); |
| 120 | } else |
| 121 | mutex_unlock(&ev_int->event_list_lock); |
| 122 | |
| 123 | error_ret: |
| 124 | return ret; |
| 125 | } |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 126 | EXPORT_SYMBOL(iio_push_event); |
| 127 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 128 | |
| 129 | /* This turns up an awful lot */ |
| 130 | ssize_t iio_read_const_attr(struct device *dev, |
| 131 | struct device_attribute *attr, |
| 132 | char *buf) |
| 133 | { |
| 134 | return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); |
| 135 | } |
| 136 | EXPORT_SYMBOL(iio_read_const_attr); |
| 137 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 138 | |
Mark Brown | 77712e5 | 2010-02-18 17:19:17 +0000 | [diff] [blame] | 139 | static ssize_t iio_event_chrdev_read(struct file *filep, |
| 140 | char __user *buf, |
| 141 | size_t count, |
| 142 | loff_t *f_ps) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 143 | { |
| 144 | struct iio_event_interface *ev_int = filep->private_data; |
| 145 | struct iio_detected_event_list *el; |
| 146 | int ret; |
| 147 | size_t len; |
| 148 | |
| 149 | mutex_lock(&ev_int->event_list_lock); |
Jonathan Cameron | 3b8ebfb | 2011-05-18 14:42:21 +0100 | [diff] [blame] | 150 | if (list_empty(&ev_int->det_events)) { |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 151 | if (filep->f_flags & O_NONBLOCK) { |
| 152 | ret = -EAGAIN; |
| 153 | goto error_mutex_unlock; |
| 154 | } |
| 155 | mutex_unlock(&ev_int->event_list_lock); |
| 156 | /* Blocking on device; waiting for something to be there */ |
| 157 | ret = wait_event_interruptible(ev_int->wait, |
| 158 | !list_empty(&ev_int |
Jonathan Cameron | 3b8ebfb | 2011-05-18 14:42:21 +0100 | [diff] [blame] | 159 | ->det_events)); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 160 | if (ret) |
| 161 | goto error_ret; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 162 | /* Single access device so no one else can get the data */ |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 163 | mutex_lock(&ev_int->event_list_lock); |
| 164 | } |
| 165 | |
Jonathan Cameron | 3b8ebfb | 2011-05-18 14:42:21 +0100 | [diff] [blame] | 166 | el = list_first_entry(&ev_int->det_events, |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 167 | struct iio_detected_event_list, |
| 168 | list); |
| 169 | len = sizeof el->ev; |
| 170 | if (copy_to_user(buf, &(el->ev), len)) { |
| 171 | ret = -EFAULT; |
| 172 | goto error_mutex_unlock; |
| 173 | } |
| 174 | list_del(&el->list); |
| 175 | ev_int->current_events--; |
| 176 | mutex_unlock(&ev_int->event_list_lock); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 177 | kfree(el); |
| 178 | |
| 179 | return len; |
| 180 | |
| 181 | error_mutex_unlock: |
| 182 | mutex_unlock(&ev_int->event_list_lock); |
| 183 | error_ret: |
| 184 | |
| 185 | return ret; |
| 186 | } |
| 187 | |
Mark Brown | 77712e5 | 2010-02-18 17:19:17 +0000 | [diff] [blame] | 188 | static int iio_event_chrdev_release(struct inode *inode, struct file *filep) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 189 | { |
| 190 | struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev); |
| 191 | struct iio_event_interface *ev_int = hand->private; |
| 192 | struct iio_detected_event_list *el, *t; |
| 193 | |
| 194 | mutex_lock(&ev_int->event_list_lock); |
| 195 | clear_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags); |
| 196 | /* |
| 197 | * In order to maintain a clean state for reopening, |
| 198 | * clear out any awaiting events. The mask will prevent |
| 199 | * any new __iio_push_event calls running. |
| 200 | */ |
Jonathan Cameron | 3b8ebfb | 2011-05-18 14:42:21 +0100 | [diff] [blame] | 201 | list_for_each_entry_safe(el, t, &ev_int->det_events, list) { |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 202 | list_del(&el->list); |
| 203 | kfree(el); |
| 204 | } |
| 205 | mutex_unlock(&ev_int->event_list_lock); |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
Mark Brown | 77712e5 | 2010-02-18 17:19:17 +0000 | [diff] [blame] | 210 | static int iio_event_chrdev_open(struct inode *inode, struct file *filep) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 211 | { |
| 212 | struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev); |
| 213 | struct iio_event_interface *ev_int = hand->private; |
| 214 | |
| 215 | mutex_lock(&ev_int->event_list_lock); |
| 216 | if (test_and_set_bit(IIO_BUSY_BIT_POS, &hand->flags)) { |
| 217 | fops_put(filep->f_op); |
| 218 | mutex_unlock(&ev_int->event_list_lock); |
| 219 | return -EBUSY; |
| 220 | } |
| 221 | filep->private_data = hand->private; |
| 222 | mutex_unlock(&ev_int->event_list_lock); |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static const struct file_operations iio_event_chrdev_fileops = { |
| 228 | .read = iio_event_chrdev_read, |
| 229 | .release = iio_event_chrdev_release, |
| 230 | .open = iio_event_chrdev_open, |
| 231 | .owner = THIS_MODULE, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 232 | .llseek = noop_llseek, |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | static void iio_event_dev_release(struct device *dev) |
| 236 | { |
| 237 | struct iio_event_interface *ev_int |
| 238 | = container_of(dev, struct iio_event_interface, dev); |
| 239 | cdev_del(&ev_int->handler.chrdev); |
| 240 | iio_device_free_chrdev_minor(MINOR(dev->devt)); |
| 241 | }; |
| 242 | |
| 243 | static struct device_type iio_event_type = { |
| 244 | .release = iio_event_dev_release, |
| 245 | }; |
| 246 | |
| 247 | int iio_device_get_chrdev_minor(void) |
| 248 | { |
| 249 | int ret, val; |
| 250 | |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 251 | ida_again: |
| 252 | if (unlikely(ida_pre_get(&iio_chrdev_ida, GFP_KERNEL) == 0)) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 253 | return -ENOMEM; |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 254 | spin_lock(&iio_ida_lock); |
| 255 | ret = ida_get_new(&iio_chrdev_ida, &val); |
| 256 | spin_unlock(&iio_ida_lock); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 257 | if (unlikely(ret == -EAGAIN)) |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 258 | goto ida_again; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 259 | else if (unlikely(ret)) |
| 260 | return ret; |
| 261 | if (val > IIO_DEV_MAX) |
| 262 | return -ENOMEM; |
| 263 | return val; |
| 264 | } |
| 265 | |
| 266 | void iio_device_free_chrdev_minor(int val) |
| 267 | { |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 268 | spin_lock(&iio_ida_lock); |
| 269 | ida_remove(&iio_chrdev_ida, val); |
| 270 | spin_unlock(&iio_ida_lock); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 271 | } |
| 272 | |
Jonathan Cameron | b9d40a9 | 2011-05-18 14:40:56 +0100 | [diff] [blame] | 273 | static int iio_setup_ev_int(struct iio_event_interface *ev_int, |
Jonathan Cameron | c74b0de | 2011-05-18 14:42:33 +0100 | [diff] [blame] | 274 | const char *dev_name, |
| 275 | int index, |
| 276 | struct module *owner, |
| 277 | struct device *dev) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 278 | { |
| 279 | int ret, minor; |
| 280 | |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 281 | ev_int->dev.bus = &iio_bus_type; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 282 | ev_int->dev.parent = dev; |
| 283 | ev_int->dev.type = &iio_event_type; |
| 284 | device_initialize(&ev_int->dev); |
| 285 | |
| 286 | minor = iio_device_get_chrdev_minor(); |
| 287 | if (minor < 0) { |
| 288 | ret = minor; |
| 289 | goto error_device_put; |
| 290 | } |
| 291 | ev_int->dev.devt = MKDEV(MAJOR(iio_devt), minor); |
Jonathan Cameron | c74b0de | 2011-05-18 14:42:33 +0100 | [diff] [blame] | 292 | dev_set_name(&ev_int->dev, "%s:event%d", dev_name, index); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 293 | |
| 294 | ret = device_add(&ev_int->dev); |
| 295 | if (ret) |
| 296 | goto error_free_minor; |
| 297 | |
| 298 | cdev_init(&ev_int->handler.chrdev, &iio_event_chrdev_fileops); |
| 299 | ev_int->handler.chrdev.owner = owner; |
| 300 | |
| 301 | mutex_init(&ev_int->event_list_lock); |
| 302 | /* discussion point - make this variable? */ |
| 303 | ev_int->max_events = 10; |
| 304 | ev_int->current_events = 0; |
Jonathan Cameron | 3b8ebfb | 2011-05-18 14:42:21 +0100 | [diff] [blame] | 305 | INIT_LIST_HEAD(&ev_int->det_events); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 306 | init_waitqueue_head(&ev_int->wait); |
| 307 | ev_int->handler.private = ev_int; |
| 308 | ev_int->handler.flags = 0; |
| 309 | |
| 310 | ret = cdev_add(&ev_int->handler.chrdev, ev_int->dev.devt, 1); |
| 311 | if (ret) |
| 312 | goto error_unreg_device; |
| 313 | |
| 314 | return 0; |
| 315 | |
| 316 | error_unreg_device: |
| 317 | device_unregister(&ev_int->dev); |
| 318 | error_free_minor: |
| 319 | iio_device_free_chrdev_minor(minor); |
| 320 | error_device_put: |
| 321 | put_device(&ev_int->dev); |
| 322 | |
| 323 | return ret; |
| 324 | } |
| 325 | |
Jonathan Cameron | b9d40a9 | 2011-05-18 14:40:56 +0100 | [diff] [blame] | 326 | static void iio_free_ev_int(struct iio_event_interface *ev_int) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 327 | { |
| 328 | device_unregister(&ev_int->dev); |
| 329 | put_device(&ev_int->dev); |
| 330 | } |
| 331 | |
| 332 | static int __init iio_dev_init(void) |
| 333 | { |
| 334 | int err; |
| 335 | |
| 336 | err = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); |
| 337 | if (err < 0) |
| 338 | printk(KERN_ERR "%s: failed to allocate char dev region\n", |
| 339 | __FILE__); |
| 340 | |
| 341 | return err; |
| 342 | } |
| 343 | |
| 344 | static void __exit iio_dev_exit(void) |
| 345 | { |
| 346 | if (iio_devt) |
| 347 | unregister_chrdev_region(iio_devt, IIO_DEV_MAX); |
| 348 | } |
| 349 | |
| 350 | static int __init iio_init(void) |
| 351 | { |
| 352 | int ret; |
| 353 | |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 354 | /* Register sysfs bus */ |
| 355 | ret = bus_register(&iio_bus_type); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 356 | if (ret < 0) { |
| 357 | printk(KERN_ERR |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 358 | "%s could not register bus type\n", |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 359 | __FILE__); |
| 360 | goto error_nothing; |
| 361 | } |
| 362 | |
| 363 | ret = iio_dev_init(); |
| 364 | if (ret < 0) |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 365 | goto error_unregister_bus_type; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 366 | |
| 367 | return 0; |
| 368 | |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 369 | error_unregister_bus_type: |
| 370 | bus_unregister(&iio_bus_type); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 371 | error_nothing: |
| 372 | return ret; |
| 373 | } |
| 374 | |
| 375 | static void __exit iio_exit(void) |
| 376 | { |
| 377 | iio_dev_exit(); |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 378 | bus_unregister(&iio_bus_type); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 379 | } |
| 380 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 381 | static ssize_t iio_read_channel_info(struct device *dev, |
| 382 | struct device_attribute *attr, |
| 383 | char *buf) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 384 | { |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 385 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 386 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 387 | int val, val2; |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 388 | int ret = indio_dev->info->read_raw(indio_dev, this_attr->c, |
| 389 | &val, &val2, this_attr->address); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 390 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 391 | if (ret < 0) |
| 392 | return ret; |
| 393 | |
| 394 | if (ret == IIO_VAL_INT) |
| 395 | return sprintf(buf, "%d\n", val); |
| 396 | else if (ret == IIO_VAL_INT_PLUS_MICRO) { |
| 397 | if (val2 < 0) |
| 398 | return sprintf(buf, "-%d.%06u\n", val, -val2); |
| 399 | else |
| 400 | return sprintf(buf, "%d.%06u\n", val, val2); |
Michael Hennerich | 71646e2 | 2011-06-27 13:07:07 +0100 | [diff] [blame] | 401 | } else if (ret == IIO_VAL_INT_PLUS_NANO) { |
| 402 | if (val2 < 0) |
| 403 | return sprintf(buf, "-%d.%09u\n", val, -val2); |
| 404 | else |
| 405 | return sprintf(buf, "%d.%09u\n", val, val2); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 406 | } else |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static ssize_t iio_write_channel_info(struct device *dev, |
| 411 | struct device_attribute *attr, |
| 412 | const char *buf, |
| 413 | size_t len) |
| 414 | { |
| 415 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 416 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Michael Hennerich | 5c04af0 | 2011-06-27 13:07:10 +0100 | [diff] [blame] | 417 | int ret, integer = 0, fract = 0, fract_mult = 100000; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 418 | bool integer_part = true, negative = false; |
| 419 | |
| 420 | /* Assumes decimal - precision based on number of digits */ |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 421 | if (!indio_dev->info->write_raw) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 422 | return -EINVAL; |
Michael Hennerich | 5c04af0 | 2011-06-27 13:07:10 +0100 | [diff] [blame] | 423 | |
| 424 | if (indio_dev->info->write_raw_get_fmt) |
| 425 | switch (indio_dev->info->write_raw_get_fmt(indio_dev, |
| 426 | this_attr->c, this_attr->address)) { |
| 427 | case IIO_VAL_INT_PLUS_MICRO: |
| 428 | fract_mult = 100000; |
| 429 | break; |
| 430 | case IIO_VAL_INT_PLUS_NANO: |
| 431 | fract_mult = 100000000; |
| 432 | break; |
| 433 | default: |
| 434 | return -EINVAL; |
| 435 | } |
| 436 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 437 | if (buf[0] == '-') { |
| 438 | negative = true; |
| 439 | buf++; |
| 440 | } |
Michael Hennerich | 5c04af0 | 2011-06-27 13:07:10 +0100 | [diff] [blame] | 441 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 442 | while (*buf) { |
| 443 | if ('0' <= *buf && *buf <= '9') { |
| 444 | if (integer_part) |
| 445 | integer = integer*10 + *buf - '0'; |
| 446 | else { |
Michael Hennerich | 5c04af0 | 2011-06-27 13:07:10 +0100 | [diff] [blame] | 447 | fract += fract_mult*(*buf - '0'); |
| 448 | if (fract_mult == 1) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 449 | break; |
Michael Hennerich | 5c04af0 | 2011-06-27 13:07:10 +0100 | [diff] [blame] | 450 | fract_mult /= 10; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 451 | } |
| 452 | } else if (*buf == '\n') { |
| 453 | if (*(buf + 1) == '\0') |
| 454 | break; |
| 455 | else |
| 456 | return -EINVAL; |
| 457 | } else if (*buf == '.') { |
| 458 | integer_part = false; |
| 459 | } else { |
| 460 | return -EINVAL; |
| 461 | } |
| 462 | buf++; |
| 463 | } |
| 464 | if (negative) { |
| 465 | if (integer) |
| 466 | integer = -integer; |
| 467 | else |
Michael Hennerich | 5c04af0 | 2011-06-27 13:07:10 +0100 | [diff] [blame] | 468 | fract = -fract; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 469 | } |
| 470 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 471 | ret = indio_dev->info->write_raw(indio_dev, this_attr->c, |
Michael Hennerich | 5c04af0 | 2011-06-27 13:07:10 +0100 | [diff] [blame] | 472 | integer, fract, this_attr->address); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 473 | if (ret) |
| 474 | return ret; |
| 475 | |
| 476 | return len; |
| 477 | } |
| 478 | |
| 479 | static int __iio_build_postfix(struct iio_chan_spec const *chan, |
| 480 | bool generic, |
| 481 | const char *postfix, |
| 482 | char **result) |
| 483 | { |
| 484 | char *all_post; |
| 485 | /* 3 options - generic, extend_name, modified - if generic, extend_name |
| 486 | * and modified cannot apply.*/ |
| 487 | |
| 488 | if (generic || (!chan->modified && !chan->extend_name)) { |
| 489 | all_post = kasprintf(GFP_KERNEL, "%s", postfix); |
| 490 | } else if (chan->modified) { |
| 491 | const char *intermediate; |
| 492 | switch (chan->type) { |
| 493 | case IIO_INTENSITY: |
| 494 | intermediate |
| 495 | = iio_modifier_names_light[chan->channel2]; |
| 496 | break; |
| 497 | case IIO_ACCEL: |
| 498 | case IIO_GYRO: |
| 499 | case IIO_MAGN: |
| 500 | case IIO_INCLI: |
| 501 | case IIO_ROT: |
| 502 | case IIO_ANGL: |
| 503 | intermediate |
| 504 | = iio_modifier_names_axial[chan->channel2]; |
| 505 | break; |
| 506 | default: |
| 507 | return -EINVAL; |
| 508 | } |
| 509 | if (chan->extend_name) |
| 510 | all_post = kasprintf(GFP_KERNEL, "%s_%s_%s", |
| 511 | intermediate, |
| 512 | chan->extend_name, |
| 513 | postfix); |
| 514 | else |
| 515 | all_post = kasprintf(GFP_KERNEL, "%s_%s", |
| 516 | intermediate, |
| 517 | postfix); |
| 518 | } else |
| 519 | all_post = kasprintf(GFP_KERNEL, "%s_%s", chan->extend_name, |
| 520 | postfix); |
| 521 | if (all_post == NULL) |
| 522 | return -ENOMEM; |
| 523 | *result = all_post; |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | int __iio_device_attr_init(struct device_attribute *dev_attr, |
| 528 | const char *postfix, |
| 529 | struct iio_chan_spec const *chan, |
| 530 | ssize_t (*readfunc)(struct device *dev, |
| 531 | struct device_attribute *attr, |
| 532 | char *buf), |
| 533 | ssize_t (*writefunc)(struct device *dev, |
| 534 | struct device_attribute *attr, |
| 535 | const char *buf, |
| 536 | size_t len), |
| 537 | bool generic) |
| 538 | { |
| 539 | int ret; |
| 540 | char *name_format, *full_postfix; |
| 541 | sysfs_attr_init(&dev_attr->attr); |
| 542 | ret = __iio_build_postfix(chan, generic, postfix, &full_postfix); |
| 543 | if (ret) |
| 544 | goto error_ret; |
| 545 | |
| 546 | /* Special case for types that uses both channel numbers in naming */ |
| 547 | if (chan->type == IIO_IN_DIFF && !generic) |
| 548 | name_format |
| 549 | = kasprintf(GFP_KERNEL, "%s_%s", |
| 550 | iio_chan_type_name_spec_complex[chan->type], |
| 551 | full_postfix); |
| 552 | else if (generic || !chan->indexed) |
| 553 | name_format |
| 554 | = kasprintf(GFP_KERNEL, "%s_%s", |
| 555 | iio_chan_type_name_spec_shared[chan->type], |
| 556 | full_postfix); |
| 557 | else |
| 558 | name_format |
| 559 | = kasprintf(GFP_KERNEL, "%s%d_%s", |
| 560 | iio_chan_type_name_spec_shared[chan->type], |
| 561 | chan->channel, |
| 562 | full_postfix); |
| 563 | |
| 564 | if (name_format == NULL) { |
| 565 | ret = -ENOMEM; |
| 566 | goto error_free_full_postfix; |
| 567 | } |
| 568 | dev_attr->attr.name = kasprintf(GFP_KERNEL, |
| 569 | name_format, |
| 570 | chan->channel, |
| 571 | chan->channel2); |
| 572 | if (dev_attr->attr.name == NULL) { |
| 573 | ret = -ENOMEM; |
| 574 | goto error_free_name_format; |
| 575 | } |
| 576 | |
| 577 | if (readfunc) { |
| 578 | dev_attr->attr.mode |= S_IRUGO; |
| 579 | dev_attr->show = readfunc; |
| 580 | } |
| 581 | |
| 582 | if (writefunc) { |
| 583 | dev_attr->attr.mode |= S_IWUSR; |
| 584 | dev_attr->store = writefunc; |
| 585 | } |
| 586 | kfree(name_format); |
| 587 | kfree(full_postfix); |
| 588 | |
| 589 | return 0; |
| 590 | |
| 591 | error_free_name_format: |
| 592 | kfree(name_format); |
| 593 | error_free_full_postfix: |
| 594 | kfree(full_postfix); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 595 | error_ret: |
| 596 | return ret; |
| 597 | } |
| 598 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 599 | void __iio_device_attr_deinit(struct device_attribute *dev_attr) |
| 600 | { |
| 601 | kfree(dev_attr->attr.name); |
| 602 | } |
| 603 | |
| 604 | int __iio_add_chan_devattr(const char *postfix, |
| 605 | const char *group, |
| 606 | struct iio_chan_spec const *chan, |
| 607 | ssize_t (*readfunc)(struct device *dev, |
| 608 | struct device_attribute *attr, |
| 609 | char *buf), |
| 610 | ssize_t (*writefunc)(struct device *dev, |
| 611 | struct device_attribute *attr, |
| 612 | const char *buf, |
| 613 | size_t len), |
| 614 | int mask, |
| 615 | bool generic, |
| 616 | struct device *dev, |
| 617 | struct list_head *attr_list) |
| 618 | { |
| 619 | int ret; |
| 620 | struct iio_dev_attr *iio_attr, *t; |
| 621 | |
| 622 | iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL); |
| 623 | if (iio_attr == NULL) { |
| 624 | ret = -ENOMEM; |
| 625 | goto error_ret; |
| 626 | } |
| 627 | ret = __iio_device_attr_init(&iio_attr->dev_attr, |
| 628 | postfix, chan, |
| 629 | readfunc, writefunc, generic); |
| 630 | if (ret) |
| 631 | goto error_iio_dev_attr_free; |
| 632 | iio_attr->c = chan; |
| 633 | iio_attr->address = mask; |
| 634 | list_for_each_entry(t, attr_list, l) |
| 635 | if (strcmp(t->dev_attr.attr.name, |
| 636 | iio_attr->dev_attr.attr.name) == 0) { |
| 637 | if (!generic) |
| 638 | dev_err(dev, "tried to double register : %s\n", |
| 639 | t->dev_attr.attr.name); |
| 640 | ret = -EBUSY; |
| 641 | goto error_device_attr_deinit; |
| 642 | } |
| 643 | |
| 644 | ret = sysfs_add_file_to_group(&dev->kobj, |
| 645 | &iio_attr->dev_attr.attr, group); |
| 646 | if (ret < 0) |
| 647 | goto error_device_attr_deinit; |
| 648 | |
| 649 | list_add(&iio_attr->l, attr_list); |
| 650 | |
| 651 | return 0; |
| 652 | |
| 653 | error_device_attr_deinit: |
| 654 | __iio_device_attr_deinit(&iio_attr->dev_attr); |
| 655 | error_iio_dev_attr_free: |
| 656 | kfree(iio_attr); |
| 657 | error_ret: |
| 658 | return ret; |
| 659 | } |
| 660 | |
| 661 | static int iio_device_add_channel_sysfs(struct iio_dev *dev_info, |
| 662 | struct iio_chan_spec const *chan) |
| 663 | { |
| 664 | int ret, i; |
| 665 | |
| 666 | |
| 667 | if (chan->channel < 0) |
| 668 | return 0; |
| 669 | if (chan->processed_val) |
| 670 | ret = __iio_add_chan_devattr("input", NULL, chan, |
| 671 | &iio_read_channel_info, |
| 672 | NULL, |
| 673 | 0, |
| 674 | 0, |
| 675 | &dev_info->dev, |
| 676 | &dev_info->channel_attr_list); |
| 677 | else |
| 678 | ret = __iio_add_chan_devattr("raw", NULL, chan, |
| 679 | &iio_read_channel_info, |
Michael Hennerich | ae19178 | 2011-06-10 15:40:48 +0200 | [diff] [blame] | 680 | (chan->type == IIO_OUT ? |
| 681 | &iio_write_channel_info : NULL), |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 682 | 0, |
| 683 | 0, |
| 684 | &dev_info->dev, |
| 685 | &dev_info->channel_attr_list); |
| 686 | if (ret) |
| 687 | goto error_ret; |
| 688 | |
| 689 | for_each_set_bit(i, &chan->info_mask, sizeof(long)*8) { |
| 690 | ret = __iio_add_chan_devattr(iio_chan_info_postfix[i/2], |
| 691 | NULL, chan, |
| 692 | &iio_read_channel_info, |
| 693 | &iio_write_channel_info, |
| 694 | (1 << i), |
| 695 | !(i%2), |
| 696 | &dev_info->dev, |
| 697 | &dev_info->channel_attr_list); |
| 698 | if (ret == -EBUSY && (i%2 == 0)) { |
| 699 | ret = 0; |
| 700 | continue; |
| 701 | } |
| 702 | if (ret < 0) |
| 703 | goto error_ret; |
| 704 | } |
| 705 | error_ret: |
| 706 | return ret; |
| 707 | } |
| 708 | |
| 709 | static void iio_device_remove_and_free_read_attr(struct iio_dev *dev_info, |
| 710 | struct iio_dev_attr *p) |
| 711 | { |
| 712 | sysfs_remove_file_from_group(&dev_info->dev.kobj, |
| 713 | &p->dev_attr.attr, NULL); |
| 714 | kfree(p->dev_attr.attr.name); |
| 715 | kfree(p); |
| 716 | } |
| 717 | |
Jonathan Cameron | 1b73288 | 2011-05-18 14:41:43 +0100 | [diff] [blame] | 718 | static ssize_t iio_show_dev_name(struct device *dev, |
| 719 | struct device_attribute *attr, |
| 720 | char *buf) |
| 721 | { |
| 722 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 723 | return sprintf(buf, "%s\n", indio_dev->name); |
| 724 | } |
| 725 | |
| 726 | static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); |
| 727 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 728 | static int iio_device_register_sysfs(struct iio_dev *dev_info) |
| 729 | { |
| 730 | int i, ret = 0; |
| 731 | struct iio_dev_attr *p, *n; |
| 732 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 733 | if (dev_info->info->attrs) { |
| 734 | ret = sysfs_create_group(&dev_info->dev.kobj, |
| 735 | dev_info->info->attrs); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 736 | if (ret) { |
| 737 | dev_err(dev_info->dev.parent, |
| 738 | "Failed to register sysfs hooks\n"); |
| 739 | goto error_ret; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | /* |
| 744 | * New channel registration method - relies on the fact a group does |
| 745 | * not need to be initialized if it is name is NULL. |
| 746 | */ |
| 747 | INIT_LIST_HEAD(&dev_info->channel_attr_list); |
| 748 | if (dev_info->channels) |
| 749 | for (i = 0; i < dev_info->num_channels; i++) { |
| 750 | ret = iio_device_add_channel_sysfs(dev_info, |
| 751 | &dev_info |
| 752 | ->channels[i]); |
| 753 | if (ret < 0) |
| 754 | goto error_clear_attrs; |
| 755 | } |
Jonathan Cameron | 1b73288 | 2011-05-18 14:41:43 +0100 | [diff] [blame] | 756 | if (dev_info->name) { |
| 757 | ret = sysfs_add_file_to_group(&dev_info->dev.kobj, |
| 758 | &dev_attr_name.attr, |
| 759 | NULL); |
| 760 | if (ret) |
| 761 | goto error_clear_attrs; |
| 762 | } |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 763 | return 0; |
Jonathan Cameron | 1b73288 | 2011-05-18 14:41:43 +0100 | [diff] [blame] | 764 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 765 | error_clear_attrs: |
| 766 | list_for_each_entry_safe(p, n, |
| 767 | &dev_info->channel_attr_list, l) { |
| 768 | list_del(&p->l); |
| 769 | iio_device_remove_and_free_read_attr(dev_info, p); |
| 770 | } |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 771 | if (dev_info->info->attrs) |
| 772 | sysfs_remove_group(&dev_info->dev.kobj, dev_info->info->attrs); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 773 | error_ret: |
| 774 | return ret; |
| 775 | |
| 776 | } |
| 777 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 778 | static void iio_device_unregister_sysfs(struct iio_dev *dev_info) |
| 779 | { |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 780 | |
| 781 | struct iio_dev_attr *p, *n; |
Jonathan Cameron | 1b73288 | 2011-05-18 14:41:43 +0100 | [diff] [blame] | 782 | if (dev_info->name) |
| 783 | sysfs_remove_file_from_group(&dev_info->dev.kobj, |
| 784 | &dev_attr_name.attr, |
| 785 | NULL); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 786 | list_for_each_entry_safe(p, n, &dev_info->channel_attr_list, l) { |
| 787 | list_del(&p->l); |
| 788 | iio_device_remove_and_free_read_attr(dev_info, p); |
| 789 | } |
| 790 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 791 | if (dev_info->info->attrs) |
| 792 | sysfs_remove_group(&dev_info->dev.kobj, dev_info->info->attrs); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 793 | } |
| 794 | |
Andrew Morton | f3cdc28 | 2010-04-27 11:29:54 -0700 | [diff] [blame] | 795 | /* Return a negative errno on failure */ |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 796 | int iio_get_new_ida_val(struct ida *this_ida) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 797 | { |
| 798 | int ret; |
| 799 | int val; |
| 800 | |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 801 | ida_again: |
| 802 | if (unlikely(ida_pre_get(this_ida, GFP_KERNEL) == 0)) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 803 | return -ENOMEM; |
| 804 | |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 805 | spin_lock(&iio_ida_lock); |
| 806 | ret = ida_get_new(this_ida, &val); |
| 807 | spin_unlock(&iio_ida_lock); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 808 | if (unlikely(ret == -EAGAIN)) |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 809 | goto ida_again; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 810 | else if (unlikely(ret)) |
| 811 | return ret; |
| 812 | |
| 813 | return val; |
| 814 | } |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 815 | EXPORT_SYMBOL(iio_get_new_ida_val); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 816 | |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 817 | void iio_free_ida_val(struct ida *this_ida, int id) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 818 | { |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 819 | spin_lock(&iio_ida_lock); |
| 820 | ida_remove(this_ida, id); |
| 821 | spin_unlock(&iio_ida_lock); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 822 | } |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 823 | EXPORT_SYMBOL(iio_free_ida_val); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 824 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 825 | static const char * const iio_ev_type_text[] = { |
| 826 | [IIO_EV_TYPE_THRESH] = "thresh", |
| 827 | [IIO_EV_TYPE_MAG] = "mag", |
| 828 | [IIO_EV_TYPE_ROC] = "roc" |
| 829 | }; |
| 830 | |
| 831 | static const char * const iio_ev_dir_text[] = { |
| 832 | [IIO_EV_DIR_EITHER] = "either", |
| 833 | [IIO_EV_DIR_RISING] = "rising", |
| 834 | [IIO_EV_DIR_FALLING] = "falling" |
| 835 | }; |
| 836 | |
| 837 | static ssize_t iio_ev_state_store(struct device *dev, |
| 838 | struct device_attribute *attr, |
| 839 | const char *buf, |
| 840 | size_t len) |
| 841 | { |
| 842 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Jonathan Cameron | aaf370d | 2011-05-18 14:41:16 +0100 | [diff] [blame] | 843 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 844 | int ret; |
Jonathan Cameron | c74b0de | 2011-05-18 14:42:33 +0100 | [diff] [blame] | 845 | bool val; |
| 846 | |
| 847 | ret = strtobool(buf, &val); |
| 848 | if (ret < 0) |
| 849 | return ret; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 850 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 851 | ret = indio_dev->info->write_event_config(indio_dev, |
| 852 | this_attr->address, |
| 853 | val); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 854 | return (ret < 0) ? ret : len; |
| 855 | } |
| 856 | |
| 857 | static ssize_t iio_ev_state_show(struct device *dev, |
| 858 | struct device_attribute *attr, |
| 859 | char *buf) |
| 860 | { |
| 861 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Jonathan Cameron | aaf370d | 2011-05-18 14:41:16 +0100 | [diff] [blame] | 862 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 863 | int val = indio_dev->info->read_event_config(indio_dev, |
| 864 | this_attr->address); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 865 | |
| 866 | if (val < 0) |
| 867 | return val; |
| 868 | else |
| 869 | return sprintf(buf, "%d\n", val); |
| 870 | } |
| 871 | |
| 872 | static ssize_t iio_ev_value_show(struct device *dev, |
| 873 | struct device_attribute *attr, |
| 874 | char *buf) |
| 875 | { |
| 876 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 877 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 878 | int val, ret; |
| 879 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 880 | ret = indio_dev->info->read_event_value(indio_dev, |
| 881 | this_attr->address, &val); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 882 | if (ret < 0) |
| 883 | return ret; |
| 884 | |
| 885 | return sprintf(buf, "%d\n", val); |
| 886 | } |
| 887 | |
| 888 | static ssize_t iio_ev_value_store(struct device *dev, |
| 889 | struct device_attribute *attr, |
| 890 | const char *buf, |
| 891 | size_t len) |
| 892 | { |
| 893 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 894 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 895 | unsigned long val; |
| 896 | int ret; |
| 897 | |
| 898 | ret = strict_strtoul(buf, 10, &val); |
| 899 | if (ret) |
| 900 | return ret; |
| 901 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 902 | ret = indio_dev->info->write_event_value(indio_dev, this_attr->address, |
| 903 | val); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 904 | if (ret < 0) |
| 905 | return ret; |
| 906 | |
| 907 | return len; |
| 908 | } |
| 909 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 910 | static int iio_device_add_event_sysfs(struct iio_dev *dev_info, |
| 911 | struct iio_chan_spec const *chan) |
| 912 | { |
| 913 | |
| 914 | int ret = 0, i, mask; |
| 915 | char *postfix; |
| 916 | if (!chan->event_mask) |
| 917 | return 0; |
| 918 | |
| 919 | for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) { |
| 920 | postfix = kasprintf(GFP_KERNEL, "%s_%s_en", |
| 921 | iio_ev_type_text[i/IIO_EV_TYPE_MAX], |
| 922 | iio_ev_dir_text[i%IIO_EV_TYPE_MAX]); |
| 923 | if (postfix == NULL) { |
| 924 | ret = -ENOMEM; |
| 925 | goto error_ret; |
| 926 | } |
| 927 | switch (chan->type) { |
| 928 | /* Switch this to a table at some point */ |
| 929 | case IIO_IN: |
| 930 | mask = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel, |
| 931 | i/IIO_EV_TYPE_MAX, |
| 932 | i%IIO_EV_TYPE_MAX); |
| 933 | break; |
| 934 | case IIO_ACCEL: |
| 935 | mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel, |
| 936 | i/IIO_EV_TYPE_MAX, |
| 937 | i%IIO_EV_TYPE_MAX); |
| 938 | break; |
| 939 | case IIO_IN_DIFF: |
| 940 | mask = IIO_MOD_EVENT_CODE(chan->type, chan->channel, |
| 941 | chan->channel2, |
| 942 | i/IIO_EV_TYPE_MAX, |
| 943 | i%IIO_EV_TYPE_MAX); |
| 944 | break; |
| 945 | default: |
| 946 | printk(KERN_INFO "currently unhandled type of event\n"); |
| 947 | } |
Jonathan Cameron | aaf370d | 2011-05-18 14:41:16 +0100 | [diff] [blame] | 948 | ret = __iio_add_chan_devattr(postfix, |
| 949 | NULL, |
| 950 | chan, |
| 951 | &iio_ev_state_show, |
| 952 | iio_ev_state_store, |
| 953 | mask, |
| 954 | /*HACK. - limits us to one |
| 955 | event interface - fix by |
| 956 | extending the bitmask - but |
| 957 | how far*/ |
| 958 | 0, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 959 | &dev_info->event_interfaces[0].dev, |
Jonathan Cameron | aaf370d | 2011-05-18 14:41:16 +0100 | [diff] [blame] | 960 | &dev_info->event_interfaces[0]. |
| 961 | dev_attr_list); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 962 | kfree(postfix); |
| 963 | if (ret) |
| 964 | goto error_ret; |
| 965 | |
| 966 | postfix = kasprintf(GFP_KERNEL, "%s_%s_value", |
| 967 | iio_ev_type_text[i/IIO_EV_TYPE_MAX], |
| 968 | iio_ev_dir_text[i%IIO_EV_TYPE_MAX]); |
| 969 | if (postfix == NULL) { |
| 970 | ret = -ENOMEM; |
| 971 | goto error_ret; |
| 972 | } |
| 973 | ret = __iio_add_chan_devattr(postfix, NULL, chan, |
| 974 | iio_ev_value_show, |
| 975 | iio_ev_value_store, |
| 976 | mask, |
| 977 | 0, |
| 978 | &dev_info->event_interfaces[0] |
| 979 | .dev, |
| 980 | &dev_info->event_interfaces[0] |
| 981 | .dev_attr_list); |
| 982 | kfree(postfix); |
| 983 | if (ret) |
| 984 | goto error_ret; |
| 985 | |
| 986 | } |
| 987 | |
| 988 | error_ret: |
| 989 | return ret; |
| 990 | } |
| 991 | |
| 992 | static inline void __iio_remove_all_event_sysfs(struct iio_dev *dev_info, |
| 993 | const char *groupname, |
| 994 | int num) |
| 995 | { |
| 996 | struct iio_dev_attr *p, *n; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 997 | list_for_each_entry_safe(p, n, |
| 998 | &dev_info->event_interfaces[num]. |
| 999 | dev_attr_list, l) { |
| 1000 | sysfs_remove_file_from_group(&dev_info |
| 1001 | ->event_interfaces[num].dev.kobj, |
| 1002 | &p->dev_attr.attr, |
| 1003 | groupname); |
| 1004 | kfree(p->dev_attr.attr.name); |
| 1005 | kfree(p); |
| 1006 | } |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1007 | } |
| 1008 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1009 | static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i) |
| 1010 | { |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1011 | int j; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1012 | int ret; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1013 | INIT_LIST_HEAD(&dev_info->event_interfaces[0].dev_attr_list); |
| 1014 | /* Dynically created from the channels array */ |
| 1015 | if (dev_info->channels) { |
| 1016 | for (j = 0; j < dev_info->num_channels; j++) { |
| 1017 | ret = iio_device_add_event_sysfs(dev_info, |
| 1018 | &dev_info |
| 1019 | ->channels[j]); |
| 1020 | if (ret) |
| 1021 | goto error_clear_attrs; |
| 1022 | } |
| 1023 | } |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1024 | return 0; |
| 1025 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1026 | error_clear_attrs: |
Jonathan Cameron | 41c7752 | 2011-05-18 14:42:12 +0100 | [diff] [blame] | 1027 | __iio_remove_all_event_sysfs(dev_info, NULL, i); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1028 | |
| 1029 | return ret; |
| 1030 | } |
| 1031 | |
| 1032 | static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info, |
| 1033 | int i) |
| 1034 | { |
Jonathan Cameron | 41c7752 | 2011-05-18 14:42:12 +0100 | [diff] [blame] | 1035 | __iio_remove_all_event_sysfs(dev_info, NULL, i); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1036 | return 0; |
| 1037 | } |
| 1038 | |
| 1039 | static int iio_device_register_eventset(struct iio_dev *dev_info) |
| 1040 | { |
| 1041 | int ret = 0, i, j; |
| 1042 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 1043 | if (dev_info->info->num_interrupt_lines == 0) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1044 | return 0; |
| 1045 | |
| 1046 | dev_info->event_interfaces = |
| 1047 | kzalloc(sizeof(struct iio_event_interface) |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 1048 | *dev_info->info->num_interrupt_lines, |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1049 | GFP_KERNEL); |
| 1050 | if (dev_info->event_interfaces == NULL) { |
| 1051 | ret = -ENOMEM; |
| 1052 | goto error_ret; |
| 1053 | } |
| 1054 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 1055 | for (i = 0; i < dev_info->info->num_interrupt_lines; i++) { |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1056 | ret = iio_setup_ev_int(&dev_info->event_interfaces[i], |
Jonathan Cameron | c74b0de | 2011-05-18 14:42:33 +0100 | [diff] [blame] | 1057 | dev_name(&dev_info->dev), |
| 1058 | i, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 1059 | dev_info->info->driver_module, |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1060 | &dev_info->dev); |
| 1061 | if (ret) { |
| 1062 | dev_err(&dev_info->dev, |
| 1063 | "Could not get chrdev interface\n"); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1064 | goto error_free_setup_ev_ints; |
| 1065 | } |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1066 | |
Jonathan Cameron | 5cba220 | 2010-05-04 14:43:01 +0100 | [diff] [blame] | 1067 | dev_set_drvdata(&dev_info->event_interfaces[i].dev, |
| 1068 | (void *)dev_info); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1069 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 1070 | if (dev_info->info->event_attrs != NULL) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1071 | ret = sysfs_create_group(&dev_info |
| 1072 | ->event_interfaces[i] |
| 1073 | .dev.kobj, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 1074 | &dev_info->info |
| 1075 | ->event_attrs[i]); |
Jonathan Cameron | 5cba220 | 2010-05-04 14:43:01 +0100 | [diff] [blame] | 1076 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1077 | if (ret) { |
| 1078 | dev_err(&dev_info->dev, |
| 1079 | "Failed to register sysfs for event attrs"); |
| 1080 | goto error_remove_sysfs_interfaces; |
| 1081 | } |
| 1082 | } |
| 1083 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 1084 | for (i = 0; i < dev_info->info->num_interrupt_lines; i++) { |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1085 | ret = __iio_add_event_config_attrs(dev_info, i); |
| 1086 | if (ret) |
| 1087 | goto error_unregister_config_attrs; |
| 1088 | } |
| 1089 | |
| 1090 | return 0; |
| 1091 | |
| 1092 | error_unregister_config_attrs: |
| 1093 | for (j = 0; j < i; j++) |
| 1094 | __iio_remove_event_config_attrs(dev_info, i); |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 1095 | i = dev_info->info->num_interrupt_lines - 1; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1096 | error_remove_sysfs_interfaces: |
| 1097 | for (j = 0; j < i; j++) |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 1098 | if (dev_info->info->event_attrs != NULL) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1099 | sysfs_remove_group(&dev_info |
Jonathan Cameron | 5cba220 | 2010-05-04 14:43:01 +0100 | [diff] [blame] | 1100 | ->event_interfaces[j].dev.kobj, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 1101 | &dev_info->info->event_attrs[j]); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1102 | error_free_setup_ev_ints: |
Jonathan Cameron | 3d550fb | 2010-06-26 12:54:18 +0100 | [diff] [blame] | 1103 | for (j = 0; j < i; j++) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1104 | iio_free_ev_int(&dev_info->event_interfaces[j]); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1105 | kfree(dev_info->event_interfaces); |
| 1106 | error_ret: |
| 1107 | |
| 1108 | return ret; |
| 1109 | } |
| 1110 | |
| 1111 | static void iio_device_unregister_eventset(struct iio_dev *dev_info) |
| 1112 | { |
| 1113 | int i; |
| 1114 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 1115 | if (dev_info->info->num_interrupt_lines == 0) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1116 | return; |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 1117 | for (i = 0; i < dev_info->info->num_interrupt_lines; i++) { |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1118 | __iio_remove_event_config_attrs(dev_info, i); |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 1119 | if (dev_info->info->event_attrs != NULL) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1120 | sysfs_remove_group(&dev_info |
| 1121 | ->event_interfaces[i].dev.kobj, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 1122 | &dev_info->info->event_attrs[i]); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1123 | } |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1124 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 1125 | for (i = 0; i < dev_info->info->num_interrupt_lines; i++) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1126 | iio_free_ev_int(&dev_info->event_interfaces[i]); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1127 | kfree(dev_info->event_interfaces); |
| 1128 | } |
| 1129 | |
| 1130 | static void iio_dev_release(struct device *device) |
| 1131 | { |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1132 | iio_put(); |
Jonathan Cameron | c74b0de | 2011-05-18 14:42:33 +0100 | [diff] [blame] | 1133 | kfree(to_iio_dev(device)); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | static struct device_type iio_dev_type = { |
| 1137 | .name = "iio_device", |
| 1138 | .release = iio_dev_release, |
| 1139 | }; |
| 1140 | |
Jonathan Cameron | 6f7c8ee | 2011-04-15 18:55:56 +0100 | [diff] [blame] | 1141 | struct iio_dev *iio_allocate_device(int sizeof_priv) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1142 | { |
Jonathan Cameron | 6f7c8ee | 2011-04-15 18:55:56 +0100 | [diff] [blame] | 1143 | struct iio_dev *dev; |
| 1144 | size_t alloc_size; |
| 1145 | |
| 1146 | alloc_size = sizeof(struct iio_dev); |
| 1147 | if (sizeof_priv) { |
| 1148 | alloc_size = ALIGN(alloc_size, IIO_ALIGN); |
| 1149 | alloc_size += sizeof_priv; |
| 1150 | } |
| 1151 | /* ensure 32-byte alignment of whole construct ? */ |
| 1152 | alloc_size += IIO_ALIGN - 1; |
| 1153 | |
| 1154 | dev = kzalloc(alloc_size, GFP_KERNEL); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1155 | |
| 1156 | if (dev) { |
| 1157 | dev->dev.type = &iio_dev_type; |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 1158 | dev->dev.bus = &iio_bus_type; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1159 | device_initialize(&dev->dev); |
| 1160 | dev_set_drvdata(&dev->dev, (void *)dev); |
| 1161 | mutex_init(&dev->mlock); |
| 1162 | iio_get(); |
| 1163 | } |
| 1164 | |
| 1165 | return dev; |
| 1166 | } |
| 1167 | EXPORT_SYMBOL(iio_allocate_device); |
| 1168 | |
| 1169 | void iio_free_device(struct iio_dev *dev) |
| 1170 | { |
| 1171 | if (dev) |
| 1172 | iio_put_device(dev); |
| 1173 | } |
| 1174 | EXPORT_SYMBOL(iio_free_device); |
| 1175 | |
| 1176 | int iio_device_register(struct iio_dev *dev_info) |
| 1177 | { |
| 1178 | int ret; |
| 1179 | |
Jonathan Cameron | c74b0de | 2011-05-18 14:42:33 +0100 | [diff] [blame] | 1180 | dev_info->id = iio_get_new_ida_val(&iio_ida); |
| 1181 | if (dev_info->id < 0) { |
| 1182 | ret = dev_info->id; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1183 | dev_err(&dev_info->dev, "Failed to get id\n"); |
| 1184 | goto error_ret; |
| 1185 | } |
| 1186 | dev_set_name(&dev_info->dev, "device%d", dev_info->id); |
| 1187 | |
| 1188 | ret = device_add(&dev_info->dev); |
| 1189 | if (ret) |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 1190 | goto error_free_ida; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1191 | ret = iio_device_register_sysfs(dev_info); |
| 1192 | if (ret) { |
| 1193 | dev_err(dev_info->dev.parent, |
| 1194 | "Failed to register sysfs interfaces\n"); |
| 1195 | goto error_del_device; |
| 1196 | } |
| 1197 | ret = iio_device_register_eventset(dev_info); |
| 1198 | if (ret) { |
| 1199 | dev_err(dev_info->dev.parent, |
Roel Van Nyen | c849d25 | 2010-04-29 19:27:31 +0200 | [diff] [blame] | 1200 | "Failed to register event set\n"); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1201 | goto error_free_sysfs; |
| 1202 | } |
| 1203 | if (dev_info->modes & INDIO_RING_TRIGGERED) |
| 1204 | iio_device_register_trigger_consumer(dev_info); |
| 1205 | |
| 1206 | return 0; |
| 1207 | |
| 1208 | error_free_sysfs: |
| 1209 | iio_device_unregister_sysfs(dev_info); |
| 1210 | error_del_device: |
| 1211 | device_del(&dev_info->dev); |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 1212 | error_free_ida: |
Jonathan Cameron | c74b0de | 2011-05-18 14:42:33 +0100 | [diff] [blame] | 1213 | iio_free_ida_val(&iio_ida, dev_info->id); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1214 | error_ret: |
| 1215 | return ret; |
| 1216 | } |
| 1217 | EXPORT_SYMBOL(iio_device_register); |
| 1218 | |
| 1219 | void iio_device_unregister(struct iio_dev *dev_info) |
| 1220 | { |
| 1221 | if (dev_info->modes & INDIO_RING_TRIGGERED) |
| 1222 | iio_device_unregister_trigger_consumer(dev_info); |
| 1223 | iio_device_unregister_eventset(dev_info); |
| 1224 | iio_device_unregister_sysfs(dev_info); |
Jonathan Cameron | c74b0de | 2011-05-18 14:42:33 +0100 | [diff] [blame] | 1225 | iio_free_ida_val(&iio_ida, dev_info->id); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1226 | device_unregister(&dev_info->dev); |
| 1227 | } |
| 1228 | EXPORT_SYMBOL(iio_device_unregister); |
| 1229 | |
| 1230 | void iio_put(void) |
| 1231 | { |
| 1232 | module_put(THIS_MODULE); |
| 1233 | } |
| 1234 | |
| 1235 | void iio_get(void) |
| 1236 | { |
| 1237 | __module_get(THIS_MODULE); |
| 1238 | } |
| 1239 | |
| 1240 | subsys_initcall(iio_init); |
| 1241 | module_exit(iio_exit); |
| 1242 | |
| 1243 | MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>"); |
| 1244 | MODULE_DESCRIPTION("Industrial I/O core"); |
| 1245 | MODULE_LICENSE("GPL"); |