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