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 | 8e7d967 | 2011-08-30 12:32:45 +0100 | [diff] [blame] | 24 | #include <linux/anon_inodes.h> |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 25 | #include <linux/debugfs.h> |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 26 | #include <linux/iio/iio.h> |
Jonathan Cameron | df9c1c4 | 2011-08-12 17:56:03 +0100 | [diff] [blame] | 27 | #include "iio_core.h" |
Jonathan Cameron | 6aea1c3 | 2011-08-24 17:28:38 +0100 | [diff] [blame] | 28 | #include "iio_core_trigger.h" |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 29 | #include <linux/iio/sysfs.h> |
| 30 | #include <linux/iio/events.h> |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 31 | #include <linux/iio/buffer.h> |
Jonathan Cameron | 9dd1cb3 | 2011-08-30 12:41:15 +0100 | [diff] [blame] | 32 | |
Peter Meerwald | 99698b4 | 2012-08-26 13:43:00 +0100 | [diff] [blame] | 33 | /* IDA to assign each registered device a unique id */ |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 34 | static DEFINE_IDA(iio_ida); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 35 | |
Jonathan Cameron | f625cb9 | 2011-08-30 12:32:48 +0100 | [diff] [blame] | 36 | static dev_t iio_devt; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 37 | |
| 38 | #define IIO_DEV_MAX 256 |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 39 | struct bus_type iio_bus_type = { |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 40 | .name = "iio", |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 41 | }; |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 42 | EXPORT_SYMBOL(iio_bus_type); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 43 | |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 44 | static struct dentry *iio_debugfs_dentry; |
| 45 | |
Jonathan Cameron | c6fc806 | 2011-09-02 17:14:34 +0100 | [diff] [blame] | 46 | static const char * const iio_direction[] = { |
| 47 | [0] = "in", |
| 48 | [1] = "out", |
| 49 | }; |
| 50 | |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 51 | static const char * const iio_chan_type_name_spec[] = { |
Jonathan Cameron | c6fc806 | 2011-09-02 17:14:34 +0100 | [diff] [blame] | 52 | [IIO_VOLTAGE] = "voltage", |
Michael Hennerich | faf290e | 2011-05-18 14:42:02 +0100 | [diff] [blame] | 53 | [IIO_CURRENT] = "current", |
| 54 | [IIO_POWER] = "power", |
Bryan Freed | 9bff02f | 2011-07-07 12:01:54 -0700 | [diff] [blame] | 55 | [IIO_ACCEL] = "accel", |
Jonathan Cameron | 41ea040 | 2011-10-05 15:27:59 +0100 | [diff] [blame] | 56 | [IIO_ANGL_VEL] = "anglvel", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 57 | [IIO_MAGN] = "magn", |
Bryan Freed | 9bff02f | 2011-07-07 12:01:54 -0700 | [diff] [blame] | 58 | [IIO_LIGHT] = "illuminance", |
| 59 | [IIO_INTENSITY] = "intensity", |
Bryan Freed | f09f2c8 | 2011-07-07 12:01:55 -0700 | [diff] [blame] | 60 | [IIO_PROXIMITY] = "proximity", |
Bryan Freed | 9bff02f | 2011-07-07 12:01:54 -0700 | [diff] [blame] | 61 | [IIO_TEMP] = "temp", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 62 | [IIO_INCLI] = "incli", |
| 63 | [IIO_ROT] = "rot", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 64 | [IIO_ANGL] = "angl", |
Bryan Freed | 9bff02f | 2011-07-07 12:01:54 -0700 | [diff] [blame] | 65 | [IIO_TIMESTAMP] = "timestamp", |
Jonathan Cameron | 66dbe70 | 2011-09-02 17:14:43 +0100 | [diff] [blame] | 66 | [IIO_CAPACITANCE] = "capacitance", |
Michael Hennerich | a6b1285 | 2012-04-27 10:58:34 +0200 | [diff] [blame] | 67 | [IIO_ALTVOLTAGE] = "altvoltage", |
Jon Brenner | 21cd1fa | 2012-05-16 10:46:42 -0500 | [diff] [blame] | 68 | [IIO_CCT] = "cct", |
Lars-Peter Clausen | c4f0c69 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 69 | [IIO_PRESSURE] = "pressure", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 70 | }; |
| 71 | |
Jonathan Cameron | 330c6c5 | 2011-09-02 17:14:39 +0100 | [diff] [blame] | 72 | static const char * const iio_modifier_names[] = { |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 73 | [IIO_MOD_X] = "x", |
| 74 | [IIO_MOD_Y] = "y", |
| 75 | [IIO_MOD_Z] = "z", |
Jonathan Cameron | 8f5879b | 2012-05-05 10:39:22 +0100 | [diff] [blame] | 76 | [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)", |
Jonathan Cameron | cf82cb8 | 2012-05-05 10:56:41 +0100 | [diff] [blame] | 77 | [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2", |
Jonathan Cameron | 330c6c5 | 2011-09-02 17:14:39 +0100 | [diff] [blame] | 78 | [IIO_MOD_LIGHT_BOTH] = "both", |
| 79 | [IIO_MOD_LIGHT_IR] = "ir", |
Jon Brenner | 21cd1fa | 2012-05-16 10:46:42 -0500 | [diff] [blame] | 80 | [IIO_MOD_LIGHT_CLEAR] = "clear", |
| 81 | [IIO_MOD_LIGHT_RED] = "red", |
| 82 | [IIO_MOD_LIGHT_GREEN] = "green", |
| 83 | [IIO_MOD_LIGHT_BLUE] = "blue", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /* relies on pairs of these shared then separate */ |
| 87 | static const char * const iio_chan_info_postfix[] = { |
Jonathan Cameron | 75a973c | 2012-04-15 17:41:30 +0100 | [diff] [blame] | 88 | [IIO_CHAN_INFO_RAW] = "raw", |
| 89 | [IIO_CHAN_INFO_PROCESSED] = "input", |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 90 | [IIO_CHAN_INFO_SCALE] = "scale", |
| 91 | [IIO_CHAN_INFO_OFFSET] = "offset", |
| 92 | [IIO_CHAN_INFO_CALIBSCALE] = "calibscale", |
| 93 | [IIO_CHAN_INFO_CALIBBIAS] = "calibbias", |
| 94 | [IIO_CHAN_INFO_PEAK] = "peak_raw", |
| 95 | [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale", |
| 96 | [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw", |
| 97 | [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", |
Jonathan Cameron | df94aba | 2011-11-27 11:39:12 +0000 | [diff] [blame] | 98 | [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] |
| 99 | = "filter_low_pass_3db_frequency", |
Laxman Dewangan | ce85a1c | 2012-04-13 16:03:31 +0530 | [diff] [blame] | 100 | [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", |
Michael Hennerich | a6b1285 | 2012-04-27 10:58:34 +0200 | [diff] [blame] | 101 | [IIO_CHAN_INFO_FREQUENCY] = "frequency", |
| 102 | [IIO_CHAN_INFO_PHASE] = "phase", |
Michael Hennerich | b65d621 | 2012-05-11 11:36:53 +0200 | [diff] [blame] | 103 | [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", |
srinivas pandruvada | 7c9ab03 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 104 | [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", |
Peter Meerwald | 899d90b | 2013-09-08 16:20:00 +0100 | [diff] [blame] | 105 | [IIO_CHAN_INFO_INT_TIME] = "integration_time", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 106 | }; |
| 107 | |
Jonathan Cameron | 5fb21c8 | 2011-12-05 21:37:10 +0000 | [diff] [blame] | 108 | const struct iio_chan_spec |
| 109 | *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) |
| 110 | { |
| 111 | int i; |
| 112 | |
| 113 | for (i = 0; i < indio_dev->num_channels; i++) |
| 114 | if (indio_dev->channels[i].scan_index == si) |
| 115 | return &indio_dev->channels[i]; |
| 116 | return NULL; |
| 117 | } |
| 118 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 119 | /* This turns up an awful lot */ |
| 120 | ssize_t iio_read_const_attr(struct device *dev, |
| 121 | struct device_attribute *attr, |
| 122 | char *buf) |
| 123 | { |
| 124 | return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); |
| 125 | } |
| 126 | EXPORT_SYMBOL(iio_read_const_attr); |
| 127 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 128 | static int __init iio_init(void) |
| 129 | { |
| 130 | int ret; |
| 131 | |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 132 | /* Register sysfs bus */ |
| 133 | ret = bus_register(&iio_bus_type); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 134 | if (ret < 0) { |
| 135 | printk(KERN_ERR |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 136 | "%s could not register bus type\n", |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 137 | __FILE__); |
| 138 | goto error_nothing; |
| 139 | } |
| 140 | |
Jonathan Cameron | 9aa1a16 | 2011-08-12 17:08:50 +0100 | [diff] [blame] | 141 | ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); |
| 142 | if (ret < 0) { |
| 143 | printk(KERN_ERR "%s: failed to allocate char dev region\n", |
| 144 | __FILE__); |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 145 | goto error_unregister_bus_type; |
Jonathan Cameron | 9aa1a16 | 2011-08-12 17:08:50 +0100 | [diff] [blame] | 146 | } |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 147 | |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 148 | iio_debugfs_dentry = debugfs_create_dir("iio", NULL); |
| 149 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 150 | return 0; |
| 151 | |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 152 | error_unregister_bus_type: |
| 153 | bus_unregister(&iio_bus_type); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 154 | error_nothing: |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | static void __exit iio_exit(void) |
| 159 | { |
Jonathan Cameron | 9aa1a16 | 2011-08-12 17:08:50 +0100 | [diff] [blame] | 160 | if (iio_devt) |
| 161 | unregister_chrdev_region(iio_devt, IIO_DEV_MAX); |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 162 | bus_unregister(&iio_bus_type); |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 163 | debugfs_remove(iio_debugfs_dentry); |
| 164 | } |
| 165 | |
| 166 | #if defined(CONFIG_DEBUG_FS) |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 167 | static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, |
| 168 | size_t count, loff_t *ppos) |
| 169 | { |
| 170 | struct iio_dev *indio_dev = file->private_data; |
| 171 | char buf[20]; |
| 172 | unsigned val = 0; |
| 173 | ssize_t len; |
| 174 | int ret; |
| 175 | |
| 176 | ret = indio_dev->info->debugfs_reg_access(indio_dev, |
| 177 | indio_dev->cached_reg_addr, |
| 178 | 0, &val); |
| 179 | if (ret) |
| 180 | dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); |
| 181 | |
| 182 | len = snprintf(buf, sizeof(buf), "0x%X\n", val); |
| 183 | |
| 184 | return simple_read_from_buffer(userbuf, count, ppos, buf, len); |
| 185 | } |
| 186 | |
| 187 | static ssize_t iio_debugfs_write_reg(struct file *file, |
| 188 | const char __user *userbuf, size_t count, loff_t *ppos) |
| 189 | { |
| 190 | struct iio_dev *indio_dev = file->private_data; |
| 191 | unsigned reg, val; |
| 192 | char buf[80]; |
| 193 | int ret; |
| 194 | |
| 195 | count = min_t(size_t, count, (sizeof(buf)-1)); |
| 196 | if (copy_from_user(buf, userbuf, count)) |
| 197 | return -EFAULT; |
| 198 | |
| 199 | buf[count] = 0; |
| 200 | |
| 201 | ret = sscanf(buf, "%i %i", ®, &val); |
| 202 | |
| 203 | switch (ret) { |
| 204 | case 1: |
| 205 | indio_dev->cached_reg_addr = reg; |
| 206 | break; |
| 207 | case 2: |
| 208 | indio_dev->cached_reg_addr = reg; |
| 209 | ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, |
| 210 | val, NULL); |
| 211 | if (ret) { |
| 212 | dev_err(indio_dev->dev.parent, "%s: write failed\n", |
| 213 | __func__); |
| 214 | return ret; |
| 215 | } |
| 216 | break; |
| 217 | default: |
| 218 | return -EINVAL; |
| 219 | } |
| 220 | |
| 221 | return count; |
| 222 | } |
| 223 | |
| 224 | static const struct file_operations iio_debugfs_reg_fops = { |
Axel Lin | 5a28c87 | 2012-05-03 09:50:51 +0800 | [diff] [blame] | 225 | .open = simple_open, |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 226 | .read = iio_debugfs_read_reg, |
| 227 | .write = iio_debugfs_write_reg, |
| 228 | }; |
| 229 | |
| 230 | static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) |
| 231 | { |
| 232 | debugfs_remove_recursive(indio_dev->debugfs_dentry); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 235 | static int iio_device_register_debugfs(struct iio_dev *indio_dev) |
| 236 | { |
| 237 | struct dentry *d; |
| 238 | |
| 239 | if (indio_dev->info->debugfs_reg_access == NULL) |
| 240 | return 0; |
| 241 | |
Axel Lin | abd5a2f | 2012-05-03 22:56:58 +0800 | [diff] [blame] | 242 | if (!iio_debugfs_dentry) |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 243 | return 0; |
| 244 | |
| 245 | indio_dev->debugfs_dentry = |
| 246 | debugfs_create_dir(dev_name(&indio_dev->dev), |
| 247 | iio_debugfs_dentry); |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 248 | if (indio_dev->debugfs_dentry == NULL) { |
| 249 | dev_warn(indio_dev->dev.parent, |
| 250 | "Failed to create debugfs directory\n"); |
| 251 | return -EFAULT; |
| 252 | } |
| 253 | |
| 254 | d = debugfs_create_file("direct_reg_access", 0644, |
| 255 | indio_dev->debugfs_dentry, |
| 256 | indio_dev, &iio_debugfs_reg_fops); |
| 257 | if (!d) { |
| 258 | iio_device_unregister_debugfs(indio_dev); |
| 259 | return -ENOMEM; |
| 260 | } |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | #else |
| 265 | static int iio_device_register_debugfs(struct iio_dev *indio_dev) |
| 266 | { |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) |
| 271 | { |
| 272 | } |
| 273 | #endif /* CONFIG_DEBUG_FS */ |
| 274 | |
Lars-Peter Clausen | 4fee7e1 | 2012-03-06 20:43:45 +0100 | [diff] [blame] | 275 | static ssize_t iio_read_channel_ext_info(struct device *dev, |
| 276 | struct device_attribute *attr, |
| 277 | char *buf) |
| 278 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 279 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 4fee7e1 | 2012-03-06 20:43:45 +0100 | [diff] [blame] | 280 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 281 | const struct iio_chan_spec_ext_info *ext_info; |
| 282 | |
| 283 | ext_info = &this_attr->c->ext_info[this_attr->address]; |
| 284 | |
Michael Hennerich | fc6d113 | 2012-04-27 10:58:36 +0200 | [diff] [blame] | 285 | return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); |
Lars-Peter Clausen | 4fee7e1 | 2012-03-06 20:43:45 +0100 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static ssize_t iio_write_channel_ext_info(struct device *dev, |
| 289 | struct device_attribute *attr, |
| 290 | const char *buf, |
| 291 | size_t len) |
| 292 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 293 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 4fee7e1 | 2012-03-06 20:43:45 +0100 | [diff] [blame] | 294 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 295 | const struct iio_chan_spec_ext_info *ext_info; |
| 296 | |
| 297 | ext_info = &this_attr->c->ext_info[this_attr->address]; |
| 298 | |
Michael Hennerich | fc6d113 | 2012-04-27 10:58:36 +0200 | [diff] [blame] | 299 | return ext_info->write(indio_dev, ext_info->private, |
| 300 | this_attr->c, buf, len); |
Lars-Peter Clausen | 4fee7e1 | 2012-03-06 20:43:45 +0100 | [diff] [blame] | 301 | } |
| 302 | |
Lars-Peter Clausen | 5212cc8 | 2012-06-04 11:36:11 +0200 | [diff] [blame] | 303 | ssize_t iio_enum_available_read(struct iio_dev *indio_dev, |
| 304 | uintptr_t priv, const struct iio_chan_spec *chan, char *buf) |
| 305 | { |
| 306 | const struct iio_enum *e = (const struct iio_enum *)priv; |
| 307 | unsigned int i; |
| 308 | size_t len = 0; |
| 309 | |
| 310 | if (!e->num_items) |
| 311 | return 0; |
| 312 | |
| 313 | for (i = 0; i < e->num_items; ++i) |
Lars-Peter Clausen | 74dcd43 | 2012-06-05 18:24:12 +0200 | [diff] [blame] | 314 | len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]); |
Lars-Peter Clausen | 5212cc8 | 2012-06-04 11:36:11 +0200 | [diff] [blame] | 315 | |
| 316 | /* replace last space with a newline */ |
| 317 | buf[len - 1] = '\n'; |
| 318 | |
| 319 | return len; |
| 320 | } |
| 321 | EXPORT_SYMBOL_GPL(iio_enum_available_read); |
| 322 | |
| 323 | ssize_t iio_enum_read(struct iio_dev *indio_dev, |
| 324 | uintptr_t priv, const struct iio_chan_spec *chan, char *buf) |
| 325 | { |
| 326 | const struct iio_enum *e = (const struct iio_enum *)priv; |
| 327 | int i; |
| 328 | |
| 329 | if (!e->get) |
| 330 | return -EINVAL; |
| 331 | |
| 332 | i = e->get(indio_dev, chan); |
| 333 | if (i < 0) |
| 334 | return i; |
| 335 | else if (i >= e->num_items) |
| 336 | return -EINVAL; |
| 337 | |
| 338 | return sprintf(buf, "%s\n", e->items[i]); |
| 339 | } |
| 340 | EXPORT_SYMBOL_GPL(iio_enum_read); |
| 341 | |
| 342 | ssize_t iio_enum_write(struct iio_dev *indio_dev, |
| 343 | uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, |
| 344 | size_t len) |
| 345 | { |
| 346 | const struct iio_enum *e = (const struct iio_enum *)priv; |
| 347 | unsigned int i; |
| 348 | int ret; |
| 349 | |
| 350 | if (!e->set) |
| 351 | return -EINVAL; |
| 352 | |
| 353 | for (i = 0; i < e->num_items; i++) { |
| 354 | if (sysfs_streq(buf, e->items[i])) |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | if (i == e->num_items) |
| 359 | return -EINVAL; |
| 360 | |
| 361 | ret = e->set(indio_dev, chan, i); |
| 362 | return ret ? ret : len; |
| 363 | } |
| 364 | EXPORT_SYMBOL_GPL(iio_enum_write); |
| 365 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 366 | static ssize_t iio_read_channel_info(struct device *dev, |
| 367 | struct device_attribute *attr, |
| 368 | char *buf) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 369 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 370 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 371 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Lars-Peter Clausen | 7985e7c | 2012-09-14 16:21:00 +0100 | [diff] [blame] | 372 | unsigned long long tmp; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 373 | int val, val2; |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 374 | bool scale_db = false; |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 375 | int ret = indio_dev->info->read_raw(indio_dev, this_attr->c, |
| 376 | &val, &val2, this_attr->address); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 377 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 378 | if (ret < 0) |
| 379 | return ret; |
| 380 | |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 381 | switch (ret) { |
| 382 | case IIO_VAL_INT: |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 383 | return sprintf(buf, "%d\n", val); |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 384 | case IIO_VAL_INT_PLUS_MICRO_DB: |
| 385 | scale_db = true; |
| 386 | case IIO_VAL_INT_PLUS_MICRO: |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 387 | if (val2 < 0) |
Oleksandr Kravchenko | d9a0134 | 2013-07-22 12:16:00 +0100 | [diff] [blame] | 388 | return sprintf(buf, "-%ld.%06u%s\n", abs(val), -val2, |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 389 | scale_db ? " dB" : ""); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 390 | else |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 391 | return sprintf(buf, "%d.%06u%s\n", val, val2, |
| 392 | scale_db ? " dB" : ""); |
| 393 | case IIO_VAL_INT_PLUS_NANO: |
Michael Hennerich | 71646e2 | 2011-06-27 13:07:07 +0100 | [diff] [blame] | 394 | if (val2 < 0) |
Oleksandr Kravchenko | d9a0134 | 2013-07-22 12:16:00 +0100 | [diff] [blame] | 395 | return sprintf(buf, "-%ld.%09u\n", abs(val), -val2); |
Michael Hennerich | 71646e2 | 2011-06-27 13:07:07 +0100 | [diff] [blame] | 396 | else |
| 397 | return sprintf(buf, "%d.%09u\n", val, val2); |
Lars-Peter Clausen | 7985e7c | 2012-09-14 16:21:00 +0100 | [diff] [blame] | 398 | case IIO_VAL_FRACTIONAL: |
| 399 | tmp = div_s64((s64)val * 1000000000LL, val2); |
| 400 | val2 = do_div(tmp, 1000000000LL); |
| 401 | val = tmp; |
| 402 | return sprintf(buf, "%d.%09u\n", val, val2); |
Lars-Peter Clausen | 103d9fb | 2012-10-16 17:29:00 +0100 | [diff] [blame] | 403 | case IIO_VAL_FRACTIONAL_LOG2: |
| 404 | tmp = (s64)val * 1000000000LL >> val2; |
| 405 | val2 = do_div(tmp, 1000000000LL); |
| 406 | val = tmp; |
| 407 | return sprintf(buf, "%d.%09u\n", val, val2); |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 408 | default: |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 409 | return 0; |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 410 | } |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 411 | } |
| 412 | |
Lars-Peter Clausen | 6807d72 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 413 | /** |
| 414 | * iio_str_to_fixpoint() - Parse a fixed-point number from a string |
| 415 | * @str: The string to parse |
| 416 | * @fract_mult: Multiplier for the first decimal place, should be a power of 10 |
| 417 | * @integer: The integer part of the number |
| 418 | * @fract: The fractional part of the number |
| 419 | * |
| 420 | * Returns 0 on success, or a negative error code if the string could not be |
| 421 | * parsed. |
| 422 | */ |
| 423 | int iio_str_to_fixpoint(const char *str, int fract_mult, |
| 424 | int *integer, int *fract) |
| 425 | { |
| 426 | int i = 0, f = 0; |
| 427 | bool integer_part = true, negative = false; |
| 428 | |
| 429 | if (str[0] == '-') { |
| 430 | negative = true; |
| 431 | str++; |
| 432 | } else if (str[0] == '+') { |
| 433 | str++; |
| 434 | } |
| 435 | |
| 436 | while (*str) { |
| 437 | if ('0' <= *str && *str <= '9') { |
| 438 | if (integer_part) { |
| 439 | i = i * 10 + *str - '0'; |
| 440 | } else { |
| 441 | f += fract_mult * (*str - '0'); |
| 442 | fract_mult /= 10; |
| 443 | } |
| 444 | } else if (*str == '\n') { |
| 445 | if (*(str + 1) == '\0') |
| 446 | break; |
| 447 | else |
| 448 | return -EINVAL; |
| 449 | } else if (*str == '.' && integer_part) { |
| 450 | integer_part = false; |
| 451 | } else { |
| 452 | return -EINVAL; |
| 453 | } |
| 454 | str++; |
| 455 | } |
| 456 | |
| 457 | if (negative) { |
| 458 | if (i) |
| 459 | i = -i; |
| 460 | else |
| 461 | f = -f; |
| 462 | } |
| 463 | |
| 464 | *integer = i; |
| 465 | *fract = f; |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); |
| 470 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 471 | static ssize_t iio_write_channel_info(struct device *dev, |
| 472 | struct device_attribute *attr, |
| 473 | const char *buf, |
| 474 | size_t len) |
| 475 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 476 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 477 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Lars-Peter Clausen | 6807d72 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 478 | int ret, fract_mult = 100000; |
| 479 | int integer, fract; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 480 | |
| 481 | /* Assumes decimal - precision based on number of digits */ |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 482 | if (!indio_dev->info->write_raw) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 483 | return -EINVAL; |
Michael Hennerich | 5c04af0 | 2011-06-27 13:07:10 +0100 | [diff] [blame] | 484 | |
| 485 | if (indio_dev->info->write_raw_get_fmt) |
| 486 | switch (indio_dev->info->write_raw_get_fmt(indio_dev, |
| 487 | this_attr->c, this_attr->address)) { |
| 488 | case IIO_VAL_INT_PLUS_MICRO: |
| 489 | fract_mult = 100000; |
| 490 | break; |
| 491 | case IIO_VAL_INT_PLUS_NANO: |
| 492 | fract_mult = 100000000; |
| 493 | break; |
| 494 | default: |
| 495 | return -EINVAL; |
| 496 | } |
| 497 | |
Lars-Peter Clausen | 6807d72 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 498 | ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract); |
| 499 | if (ret) |
| 500 | return ret; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 501 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 502 | ret = indio_dev->info->write_raw(indio_dev, this_attr->c, |
Michael Hennerich | 5c04af0 | 2011-06-27 13:07:10 +0100 | [diff] [blame] | 503 | integer, fract, this_attr->address); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 504 | if (ret) |
| 505 | return ret; |
| 506 | |
| 507 | return len; |
| 508 | } |
| 509 | |
Jonathan Cameron | df9c1c4 | 2011-08-12 17:56:03 +0100 | [diff] [blame] | 510 | static |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 511 | int __iio_device_attr_init(struct device_attribute *dev_attr, |
| 512 | const char *postfix, |
| 513 | struct iio_chan_spec const *chan, |
| 514 | ssize_t (*readfunc)(struct device *dev, |
| 515 | struct device_attribute *attr, |
| 516 | char *buf), |
| 517 | ssize_t (*writefunc)(struct device *dev, |
| 518 | struct device_attribute *attr, |
| 519 | const char *buf, |
| 520 | size_t len), |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 521 | enum iio_shared_by shared_by) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 522 | { |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 523 | int ret = 0; |
| 524 | char *name_format = NULL; |
| 525 | char *full_postfix; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 526 | sysfs_attr_init(&dev_attr->attr); |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 527 | |
| 528 | /* Build up postfix of <extend_name>_<modifier>_postfix */ |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 529 | if (chan->modified && (shared_by == IIO_SEPARATE)) { |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 530 | if (chan->extend_name) |
| 531 | full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", |
| 532 | iio_modifier_names[chan |
| 533 | ->channel2], |
| 534 | chan->extend_name, |
| 535 | postfix); |
| 536 | else |
| 537 | full_postfix = kasprintf(GFP_KERNEL, "%s_%s", |
| 538 | iio_modifier_names[chan |
| 539 | ->channel2], |
| 540 | postfix); |
| 541 | } else { |
| 542 | if (chan->extend_name == NULL) |
| 543 | full_postfix = kstrdup(postfix, GFP_KERNEL); |
| 544 | else |
| 545 | full_postfix = kasprintf(GFP_KERNEL, |
| 546 | "%s_%s", |
| 547 | chan->extend_name, |
| 548 | postfix); |
| 549 | } |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 550 | if (full_postfix == NULL) |
| 551 | return -ENOMEM; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 552 | |
Justin P. Mattock | 4abf6f8 | 2012-02-29 22:00:38 -0800 | [diff] [blame] | 553 | if (chan->differential) { /* Differential can not have modifier */ |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 554 | switch (shared_by) { |
Jonathan Cameron | c006ec8 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 555 | case IIO_SHARED_BY_ALL: |
| 556 | name_format = kasprintf(GFP_KERNEL, "%s", full_postfix); |
| 557 | break; |
| 558 | case IIO_SHARED_BY_DIR: |
| 559 | name_format = kasprintf(GFP_KERNEL, "%s_%s", |
| 560 | iio_direction[chan->output], |
| 561 | full_postfix); |
| 562 | break; |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 563 | case IIO_SHARED_BY_TYPE: |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 564 | name_format |
| 565 | = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", |
| 566 | iio_direction[chan->output], |
| 567 | iio_chan_type_name_spec[chan->type], |
| 568 | iio_chan_type_name_spec[chan->type], |
| 569 | full_postfix); |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 570 | break; |
| 571 | case IIO_SEPARATE: |
| 572 | if (!chan->indexed) { |
| 573 | WARN_ON("Differential channels must be indexed\n"); |
| 574 | ret = -EINVAL; |
| 575 | goto error_free_full_postfix; |
| 576 | } |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 577 | name_format |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 578 | = kasprintf(GFP_KERNEL, |
| 579 | "%s_%s%d-%s%d_%s", |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 580 | iio_direction[chan->output], |
| 581 | iio_chan_type_name_spec[chan->type], |
| 582 | chan->channel, |
| 583 | iio_chan_type_name_spec[chan->type], |
| 584 | chan->channel2, |
| 585 | full_postfix); |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 586 | break; |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 587 | } |
| 588 | } else { /* Single ended */ |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 589 | switch (shared_by) { |
Jonathan Cameron | c006ec8 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 590 | case IIO_SHARED_BY_ALL: |
| 591 | name_format = kasprintf(GFP_KERNEL, "%s", full_postfix); |
| 592 | break; |
| 593 | case IIO_SHARED_BY_DIR: |
| 594 | name_format = kasprintf(GFP_KERNEL, "%s_%s", |
| 595 | iio_direction[chan->output], |
| 596 | full_postfix); |
| 597 | break; |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 598 | case IIO_SHARED_BY_TYPE: |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 599 | name_format |
| 600 | = kasprintf(GFP_KERNEL, "%s_%s_%s", |
| 601 | iio_direction[chan->output], |
| 602 | iio_chan_type_name_spec[chan->type], |
| 603 | full_postfix); |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 604 | break; |
| 605 | |
| 606 | case IIO_SEPARATE: |
| 607 | if (chan->indexed) |
| 608 | name_format |
| 609 | = kasprintf(GFP_KERNEL, "%s_%s%d_%s", |
| 610 | iio_direction[chan->output], |
| 611 | iio_chan_type_name_spec[chan->type], |
| 612 | chan->channel, |
| 613 | full_postfix); |
| 614 | else |
| 615 | name_format |
| 616 | = kasprintf(GFP_KERNEL, "%s_%s_%s", |
| 617 | iio_direction[chan->output], |
| 618 | iio_chan_type_name_spec[chan->type], |
| 619 | full_postfix); |
| 620 | break; |
| 621 | } |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 622 | } |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 623 | if (name_format == NULL) { |
| 624 | ret = -ENOMEM; |
| 625 | goto error_free_full_postfix; |
| 626 | } |
| 627 | dev_attr->attr.name = kasprintf(GFP_KERNEL, |
| 628 | name_format, |
| 629 | chan->channel, |
| 630 | chan->channel2); |
| 631 | if (dev_attr->attr.name == NULL) { |
| 632 | ret = -ENOMEM; |
| 633 | goto error_free_name_format; |
| 634 | } |
| 635 | |
| 636 | if (readfunc) { |
| 637 | dev_attr->attr.mode |= S_IRUGO; |
| 638 | dev_attr->show = readfunc; |
| 639 | } |
| 640 | |
| 641 | if (writefunc) { |
| 642 | dev_attr->attr.mode |= S_IWUSR; |
| 643 | dev_attr->store = writefunc; |
| 644 | } |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 645 | error_free_name_format: |
| 646 | kfree(name_format); |
| 647 | error_free_full_postfix: |
| 648 | kfree(full_postfix); |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 649 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 650 | return ret; |
| 651 | } |
| 652 | |
Jonathan Cameron | df9c1c4 | 2011-08-12 17:56:03 +0100 | [diff] [blame] | 653 | static void __iio_device_attr_deinit(struct device_attribute *dev_attr) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 654 | { |
| 655 | kfree(dev_attr->attr.name); |
| 656 | } |
| 657 | |
| 658 | int __iio_add_chan_devattr(const char *postfix, |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 659 | struct iio_chan_spec const *chan, |
| 660 | ssize_t (*readfunc)(struct device *dev, |
| 661 | struct device_attribute *attr, |
| 662 | char *buf), |
| 663 | ssize_t (*writefunc)(struct device *dev, |
| 664 | struct device_attribute *attr, |
| 665 | const char *buf, |
| 666 | size_t len), |
Jonathan Cameron | e614a54 | 2011-09-02 17:14:41 +0100 | [diff] [blame] | 667 | u64 mask, |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 668 | enum iio_shared_by shared_by, |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 669 | struct device *dev, |
| 670 | struct list_head *attr_list) |
| 671 | { |
| 672 | int ret; |
| 673 | struct iio_dev_attr *iio_attr, *t; |
| 674 | |
| 675 | iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL); |
| 676 | if (iio_attr == NULL) { |
| 677 | ret = -ENOMEM; |
| 678 | goto error_ret; |
| 679 | } |
| 680 | ret = __iio_device_attr_init(&iio_attr->dev_attr, |
| 681 | postfix, chan, |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 682 | readfunc, writefunc, shared_by); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 683 | if (ret) |
| 684 | goto error_iio_dev_attr_free; |
| 685 | iio_attr->c = chan; |
| 686 | iio_attr->address = mask; |
| 687 | list_for_each_entry(t, attr_list, l) |
| 688 | if (strcmp(t->dev_attr.attr.name, |
| 689 | iio_attr->dev_attr.attr.name) == 0) { |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 690 | if (shared_by == IIO_SEPARATE) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 691 | dev_err(dev, "tried to double register : %s\n", |
| 692 | t->dev_attr.attr.name); |
| 693 | ret = -EBUSY; |
| 694 | goto error_device_attr_deinit; |
| 695 | } |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 696 | list_add(&iio_attr->l, attr_list); |
| 697 | |
| 698 | return 0; |
| 699 | |
| 700 | error_device_attr_deinit: |
| 701 | __iio_device_attr_deinit(&iio_attr->dev_attr); |
| 702 | error_iio_dev_attr_free: |
| 703 | kfree(iio_attr); |
| 704 | error_ret: |
| 705 | return ret; |
| 706 | } |
| 707 | |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 708 | static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, |
| 709 | struct iio_chan_spec const *chan, |
| 710 | enum iio_shared_by shared_by, |
| 711 | const long *infomask) |
| 712 | { |
| 713 | int i, ret, attrcount = 0; |
| 714 | |
| 715 | for_each_set_bit(i, infomask, sizeof(infomask)*8) { |
| 716 | ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], |
| 717 | chan, |
| 718 | &iio_read_channel_info, |
| 719 | &iio_write_channel_info, |
| 720 | i, |
| 721 | shared_by, |
| 722 | &indio_dev->dev, |
| 723 | &indio_dev->channel_attr_list); |
| 724 | if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) |
| 725 | continue; |
| 726 | else if (ret < 0) |
| 727 | return ret; |
| 728 | attrcount++; |
| 729 | } |
| 730 | |
| 731 | return attrcount; |
| 732 | } |
| 733 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 734 | static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 735 | struct iio_chan_spec const *chan) |
| 736 | { |
Jonathan Cameron | 5ccb3ad | 2012-04-15 17:41:16 +0100 | [diff] [blame] | 737 | int ret, attrcount = 0; |
Lars-Peter Clausen | 5f420b4 | 2012-02-21 18:38:12 +0100 | [diff] [blame] | 738 | const struct iio_chan_spec_ext_info *ext_info; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 739 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 740 | if (chan->channel < 0) |
| 741 | return 0; |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 742 | ret = iio_device_add_info_mask_type(indio_dev, chan, |
| 743 | IIO_SEPARATE, |
| 744 | &chan->info_mask_separate); |
| 745 | if (ret < 0) |
| 746 | return ret; |
| 747 | attrcount += ret; |
| 748 | |
| 749 | ret = iio_device_add_info_mask_type(indio_dev, chan, |
| 750 | IIO_SHARED_BY_TYPE, |
| 751 | &chan->info_mask_shared_by_type); |
| 752 | if (ret < 0) |
| 753 | return ret; |
| 754 | attrcount += ret; |
Lars-Peter Clausen | 5f420b4 | 2012-02-21 18:38:12 +0100 | [diff] [blame] | 755 | |
Jonathan Cameron | c006ec8 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 756 | ret = iio_device_add_info_mask_type(indio_dev, chan, |
| 757 | IIO_SHARED_BY_DIR, |
| 758 | &chan->info_mask_shared_by_dir); |
| 759 | if (ret < 0) |
| 760 | return ret; |
| 761 | attrcount += ret; |
| 762 | |
| 763 | ret = iio_device_add_info_mask_type(indio_dev, chan, |
| 764 | IIO_SHARED_BY_ALL, |
| 765 | &chan->info_mask_shared_by_all); |
| 766 | if (ret < 0) |
| 767 | return ret; |
| 768 | attrcount += ret; |
| 769 | |
Lars-Peter Clausen | 5f420b4 | 2012-02-21 18:38:12 +0100 | [diff] [blame] | 770 | if (chan->ext_info) { |
| 771 | unsigned int i = 0; |
| 772 | for (ext_info = chan->ext_info; ext_info->name; ext_info++) { |
| 773 | ret = __iio_add_chan_devattr(ext_info->name, |
| 774 | chan, |
| 775 | ext_info->read ? |
| 776 | &iio_read_channel_ext_info : NULL, |
| 777 | ext_info->write ? |
| 778 | &iio_write_channel_ext_info : NULL, |
| 779 | i, |
| 780 | ext_info->shared, |
| 781 | &indio_dev->dev, |
| 782 | &indio_dev->channel_attr_list); |
| 783 | i++; |
| 784 | if (ret == -EBUSY && ext_info->shared) |
| 785 | continue; |
| 786 | |
| 787 | if (ret) |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 788 | return ret; |
Lars-Peter Clausen | 5f420b4 | 2012-02-21 18:38:12 +0100 | [diff] [blame] | 789 | |
| 790 | attrcount++; |
| 791 | } |
| 792 | } |
| 793 | |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 794 | return attrcount; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 795 | } |
| 796 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 797 | static void iio_device_remove_and_free_read_attr(struct iio_dev *indio_dev, |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 798 | struct iio_dev_attr *p) |
| 799 | { |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 800 | kfree(p->dev_attr.attr.name); |
| 801 | kfree(p); |
| 802 | } |
| 803 | |
Jonathan Cameron | 1b73288 | 2011-05-18 14:41:43 +0100 | [diff] [blame] | 804 | static ssize_t iio_show_dev_name(struct device *dev, |
| 805 | struct device_attribute *attr, |
| 806 | char *buf) |
| 807 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 808 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 1b73288 | 2011-05-18 14:41:43 +0100 | [diff] [blame] | 809 | return sprintf(buf, "%s\n", indio_dev->name); |
| 810 | } |
| 811 | |
| 812 | static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); |
| 813 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 814 | static int iio_device_register_sysfs(struct iio_dev *indio_dev) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 815 | { |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 816 | int i, ret = 0, attrcount, attrn, attrcount_orig = 0; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 817 | struct iio_dev_attr *p, *n; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 818 | struct attribute **attr; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 819 | |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 820 | /* First count elements in any existing group */ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 821 | if (indio_dev->info->attrs) { |
| 822 | attr = indio_dev->info->attrs->attrs; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 823 | while (*attr++ != NULL) |
| 824 | attrcount_orig++; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 825 | } |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 826 | attrcount = attrcount_orig; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 827 | /* |
| 828 | * New channel registration method - relies on the fact a group does |
Peter Meerwald | d25b380 | 2012-08-26 13:43:00 +0100 | [diff] [blame] | 829 | * not need to be initialized if its name is NULL. |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 830 | */ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 831 | if (indio_dev->channels) |
| 832 | for (i = 0; i < indio_dev->num_channels; i++) { |
| 833 | ret = iio_device_add_channel_sysfs(indio_dev, |
| 834 | &indio_dev |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 835 | ->channels[i]); |
| 836 | if (ret < 0) |
| 837 | goto error_clear_attrs; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 838 | attrcount += ret; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 839 | } |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 840 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 841 | if (indio_dev->name) |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 842 | attrcount++; |
| 843 | |
Thomas Meyer | d83fb18 | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 844 | indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1, |
| 845 | sizeof(indio_dev->chan_attr_group.attrs[0]), |
| 846 | GFP_KERNEL); |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 847 | if (indio_dev->chan_attr_group.attrs == NULL) { |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 848 | ret = -ENOMEM; |
| 849 | goto error_clear_attrs; |
Jonathan Cameron | 1b73288 | 2011-05-18 14:41:43 +0100 | [diff] [blame] | 850 | } |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 851 | /* Copy across original attributes */ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 852 | if (indio_dev->info->attrs) |
| 853 | memcpy(indio_dev->chan_attr_group.attrs, |
| 854 | indio_dev->info->attrs->attrs, |
| 855 | sizeof(indio_dev->chan_attr_group.attrs[0]) |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 856 | *attrcount_orig); |
| 857 | attrn = attrcount_orig; |
| 858 | /* Add all elements from the list. */ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 859 | list_for_each_entry(p, &indio_dev->channel_attr_list, l) |
| 860 | indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; |
| 861 | if (indio_dev->name) |
| 862 | indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 863 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 864 | indio_dev->groups[indio_dev->groupcounter++] = |
| 865 | &indio_dev->chan_attr_group; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 866 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 867 | return 0; |
Jonathan Cameron | 1b73288 | 2011-05-18 14:41:43 +0100 | [diff] [blame] | 868 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 869 | error_clear_attrs: |
| 870 | list_for_each_entry_safe(p, n, |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 871 | &indio_dev->channel_attr_list, l) { |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 872 | list_del(&p->l); |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 873 | iio_device_remove_and_free_read_attr(indio_dev, p); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 874 | } |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 875 | |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 876 | return ret; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 877 | } |
| 878 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 879 | static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 880 | { |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 881 | |
| 882 | struct iio_dev_attr *p, *n; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 883 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 884 | list_for_each_entry_safe(p, n, &indio_dev->channel_attr_list, l) { |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 885 | list_del(&p->l); |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 886 | iio_device_remove_and_free_read_attr(indio_dev, p); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 887 | } |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 888 | kfree(indio_dev->chan_attr_group.attrs); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 889 | } |
| 890 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 891 | static void iio_dev_release(struct device *device) |
| 892 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 893 | struct iio_dev *indio_dev = dev_to_iio_dev(device); |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 894 | if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) |
| 895 | iio_device_unregister_trigger_consumer(indio_dev); |
| 896 | iio_device_unregister_eventset(indio_dev); |
| 897 | iio_device_unregister_sysfs(indio_dev); |
Lars-Peter Clausen | e407fd6 | 2012-06-04 10:41:42 +0200 | [diff] [blame] | 898 | |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 899 | iio_buffer_put(indio_dev->buffer); |
| 900 | |
Lars-Peter Clausen | e407fd6 | 2012-06-04 10:41:42 +0200 | [diff] [blame] | 901 | ida_simple_remove(&iio_ida, indio_dev->id); |
| 902 | kfree(indio_dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 903 | } |
| 904 | |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 905 | struct device_type iio_device_type = { |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 906 | .name = "iio_device", |
| 907 | .release = iio_dev_release, |
| 908 | }; |
| 909 | |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 910 | struct iio_dev *iio_device_alloc(int sizeof_priv) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 911 | { |
Jonathan Cameron | 6f7c8ee | 2011-04-15 18:55:56 +0100 | [diff] [blame] | 912 | struct iio_dev *dev; |
| 913 | size_t alloc_size; |
| 914 | |
| 915 | alloc_size = sizeof(struct iio_dev); |
| 916 | if (sizeof_priv) { |
| 917 | alloc_size = ALIGN(alloc_size, IIO_ALIGN); |
| 918 | alloc_size += sizeof_priv; |
| 919 | } |
| 920 | /* ensure 32-byte alignment of whole construct ? */ |
| 921 | alloc_size += IIO_ALIGN - 1; |
| 922 | |
| 923 | dev = kzalloc(alloc_size, GFP_KERNEL); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 924 | |
| 925 | if (dev) { |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 926 | dev->dev.groups = dev->groups; |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 927 | dev->dev.type = &iio_device_type; |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 928 | dev->dev.bus = &iio_bus_type; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 929 | device_initialize(&dev->dev); |
| 930 | dev_set_drvdata(&dev->dev, (void *)dev); |
| 931 | mutex_init(&dev->mlock); |
Jonathan Cameron | ac917a8 | 2012-02-15 19:48:00 +0000 | [diff] [blame] | 932 | mutex_init(&dev->info_exist_lock); |
Lars-Peter Clausen | e407fd6 | 2012-06-04 10:41:42 +0200 | [diff] [blame] | 933 | INIT_LIST_HEAD(&dev->channel_attr_list); |
Jonathan Cameron | a9e39f9 | 2011-09-14 13:01:25 +0100 | [diff] [blame] | 934 | |
| 935 | dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); |
| 936 | if (dev->id < 0) { |
| 937 | /* cannot use a dev_err as the name isn't available */ |
| 938 | printk(KERN_ERR "Failed to get id\n"); |
| 939 | kfree(dev); |
| 940 | return NULL; |
| 941 | } |
| 942 | dev_set_name(&dev->dev, "iio:device%d", dev->id); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 943 | INIT_LIST_HEAD(&dev->buffer_list); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | return dev; |
| 947 | } |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 948 | EXPORT_SYMBOL(iio_device_alloc); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 949 | |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 950 | void iio_device_free(struct iio_dev *dev) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 951 | { |
Lars-Peter Clausen | e407fd6 | 2012-06-04 10:41:42 +0200 | [diff] [blame] | 952 | if (dev) |
| 953 | put_device(&dev->dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 954 | } |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 955 | EXPORT_SYMBOL(iio_device_free); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 956 | |
Grygorii Strashko | 9dabaf5 | 2013-07-18 11:19:00 +0100 | [diff] [blame] | 957 | static void devm_iio_device_release(struct device *dev, void *res) |
| 958 | { |
| 959 | iio_device_free(*(struct iio_dev **)res); |
| 960 | } |
| 961 | |
| 962 | static int devm_iio_device_match(struct device *dev, void *res, void *data) |
| 963 | { |
| 964 | struct iio_dev **r = res; |
| 965 | if (!r || !*r) { |
| 966 | WARN_ON(!r || !*r); |
| 967 | return 0; |
| 968 | } |
| 969 | return *r == data; |
| 970 | } |
| 971 | |
| 972 | struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv) |
| 973 | { |
| 974 | struct iio_dev **ptr, *iio_dev; |
| 975 | |
| 976 | ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr), |
| 977 | GFP_KERNEL); |
| 978 | if (!ptr) |
| 979 | return NULL; |
| 980 | |
| 981 | /* use raw alloc_dr for kmalloc caller tracing */ |
| 982 | iio_dev = iio_device_alloc(sizeof_priv); |
| 983 | if (iio_dev) { |
| 984 | *ptr = iio_dev; |
| 985 | devres_add(dev, ptr); |
| 986 | } else { |
| 987 | devres_free(ptr); |
| 988 | } |
| 989 | |
| 990 | return iio_dev; |
| 991 | } |
| 992 | EXPORT_SYMBOL_GPL(devm_iio_device_alloc); |
| 993 | |
| 994 | void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev) |
| 995 | { |
| 996 | int rc; |
| 997 | |
| 998 | rc = devres_release(dev, devm_iio_device_release, |
| 999 | devm_iio_device_match, iio_dev); |
| 1000 | WARN_ON(rc); |
| 1001 | } |
| 1002 | EXPORT_SYMBOL_GPL(devm_iio_device_free); |
| 1003 | |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1004 | /** |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 1005 | * iio_chrdev_open() - chrdev file open for buffer access and ioctls |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1006 | **/ |
| 1007 | static int iio_chrdev_open(struct inode *inode, struct file *filp) |
| 1008 | { |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1009 | struct iio_dev *indio_dev = container_of(inode->i_cdev, |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1010 | struct iio_dev, chrdev); |
Lars-Peter Clausen | bb01443 | 2011-12-19 15:23:45 +0100 | [diff] [blame] | 1011 | |
| 1012 | if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags)) |
| 1013 | return -EBUSY; |
| 1014 | |
Lars-Peter Clausen | cadc212 | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1015 | iio_device_get(indio_dev); |
| 1016 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1017 | filp->private_data = indio_dev; |
Jonathan Cameron | 30eb82f | 2011-09-21 11:16:02 +0100 | [diff] [blame] | 1018 | |
Lars-Peter Clausen | 7933514 | 2011-12-19 15:23:49 +0100 | [diff] [blame] | 1019 | return 0; |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | /** |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 1023 | * iio_chrdev_release() - chrdev file close buffer access and ioctls |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1024 | **/ |
| 1025 | static int iio_chrdev_release(struct inode *inode, struct file *filp) |
| 1026 | { |
Lars-Peter Clausen | bb01443 | 2011-12-19 15:23:45 +0100 | [diff] [blame] | 1027 | struct iio_dev *indio_dev = container_of(inode->i_cdev, |
| 1028 | struct iio_dev, chrdev); |
Lars-Peter Clausen | bb01443 | 2011-12-19 15:23:45 +0100 | [diff] [blame] | 1029 | clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags); |
Lars-Peter Clausen | cadc212 | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1030 | iio_device_put(indio_dev); |
| 1031 | |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1032 | return 0; |
| 1033 | } |
| 1034 | |
| 1035 | /* Somewhat of a cross file organization violation - ioctls here are actually |
| 1036 | * event related */ |
| 1037 | static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 1038 | { |
| 1039 | struct iio_dev *indio_dev = filp->private_data; |
| 1040 | int __user *ip = (int __user *)arg; |
| 1041 | int fd; |
| 1042 | |
Lars-Peter Clausen | f18e7a0 | 2013-10-04 12:06:00 +0100 | [diff] [blame^] | 1043 | if (!indio_dev->info) |
| 1044 | return -ENODEV; |
| 1045 | |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1046 | if (cmd == IIO_GET_EVENT_FD_IOCTL) { |
| 1047 | fd = iio_event_getfd(indio_dev); |
| 1048 | if (copy_to_user(ip, &fd, sizeof(fd))) |
| 1049 | return -EFAULT; |
| 1050 | return 0; |
| 1051 | } |
| 1052 | return -EINVAL; |
| 1053 | } |
| 1054 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 1055 | static const struct file_operations iio_buffer_fileops = { |
| 1056 | .read = iio_buffer_read_first_n_outer_addr, |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1057 | .release = iio_chrdev_release, |
| 1058 | .open = iio_chrdev_open, |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 1059 | .poll = iio_buffer_poll_addr, |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1060 | .owner = THIS_MODULE, |
| 1061 | .llseek = noop_llseek, |
| 1062 | .unlocked_ioctl = iio_ioctl, |
| 1063 | .compat_ioctl = iio_ioctl, |
| 1064 | }; |
| 1065 | |
Michael Hennerich | 0f1acee | 2012-03-01 10:51:04 +0100 | [diff] [blame] | 1066 | static const struct iio_buffer_setup_ops noop_ring_setup_ops; |
| 1067 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1068 | int iio_device_register(struct iio_dev *indio_dev) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1069 | { |
| 1070 | int ret; |
| 1071 | |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 1072 | /* If the calling driver did not initialize of_node, do it here */ |
| 1073 | if (!indio_dev->dev.of_node && indio_dev->dev.parent) |
| 1074 | indio_dev->dev.of_node = indio_dev->dev.parent->of_node; |
| 1075 | |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1076 | /* configure elements for the chrdev */ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1077 | indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1078 | |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 1079 | ret = iio_device_register_debugfs(indio_dev); |
| 1080 | if (ret) { |
| 1081 | dev_err(indio_dev->dev.parent, |
| 1082 | "Failed to register debugfs interfaces\n"); |
| 1083 | goto error_ret; |
| 1084 | } |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1085 | ret = iio_device_register_sysfs(indio_dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1086 | if (ret) { |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1087 | dev_err(indio_dev->dev.parent, |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1088 | "Failed to register sysfs interfaces\n"); |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 1089 | goto error_unreg_debugfs; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1090 | } |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1091 | ret = iio_device_register_eventset(indio_dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1092 | if (ret) { |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1093 | dev_err(indio_dev->dev.parent, |
Roel Van Nyen | c849d25 | 2010-04-29 19:27:31 +0200 | [diff] [blame] | 1094 | "Failed to register event set\n"); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1095 | goto error_free_sysfs; |
| 1096 | } |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1097 | if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) |
| 1098 | iio_device_register_trigger_consumer(indio_dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1099 | |
Michael Hennerich | 0f1acee | 2012-03-01 10:51:04 +0100 | [diff] [blame] | 1100 | if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && |
| 1101 | indio_dev->setup_ops == NULL) |
| 1102 | indio_dev->setup_ops = &noop_ring_setup_ops; |
| 1103 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1104 | cdev_init(&indio_dev->chrdev, &iio_buffer_fileops); |
| 1105 | indio_dev->chrdev.owner = indio_dev->info->driver_module; |
Lars-Peter Clausen | 0d5b7da | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1106 | indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj; |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1107 | ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1); |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1108 | if (ret < 0) |
Lars-Peter Clausen | 0d5b7da | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1109 | goto error_unreg_eventset; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1110 | |
Lars-Peter Clausen | 0d5b7da | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1111 | ret = device_add(&indio_dev->dev); |
| 1112 | if (ret < 0) |
| 1113 | goto error_cdev_del; |
| 1114 | |
| 1115 | return 0; |
| 1116 | error_cdev_del: |
| 1117 | cdev_del(&indio_dev->chrdev); |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1118 | error_unreg_eventset: |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1119 | iio_device_unregister_eventset(indio_dev); |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1120 | error_free_sysfs: |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1121 | iio_device_unregister_sysfs(indio_dev); |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 1122 | error_unreg_debugfs: |
| 1123 | iio_device_unregister_debugfs(indio_dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1124 | error_ret: |
| 1125 | return ret; |
| 1126 | } |
| 1127 | EXPORT_SYMBOL(iio_device_register); |
| 1128 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1129 | void iio_device_unregister(struct iio_dev *indio_dev) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1130 | { |
Jonathan Cameron | ac917a8 | 2012-02-15 19:48:00 +0000 | [diff] [blame] | 1131 | mutex_lock(&indio_dev->info_exist_lock); |
Lars-Peter Clausen | a87c82e | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1132 | |
| 1133 | device_del(&indio_dev->dev); |
| 1134 | |
Lars-Peter Clausen | 0d5b7da | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1135 | if (indio_dev->chrdev.dev) |
| 1136 | cdev_del(&indio_dev->chrdev); |
Lars-Peter Clausen | bc4c961 | 2013-09-21 16:21:00 +0100 | [diff] [blame] | 1137 | iio_device_unregister_debugfs(indio_dev); |
Lars-Peter Clausen | 0d5b7da | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1138 | |
Lars-Peter Clausen | a87c82e | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1139 | iio_disable_all_buffers(indio_dev); |
| 1140 | |
Jonathan Cameron | ac917a8 | 2012-02-15 19:48:00 +0000 | [diff] [blame] | 1141 | indio_dev->info = NULL; |
| 1142 | mutex_unlock(&indio_dev->info_exist_lock); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1143 | } |
| 1144 | EXPORT_SYMBOL(iio_device_unregister); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1145 | subsys_initcall(iio_init); |
| 1146 | module_exit(iio_exit); |
| 1147 | |
Jonathan Cameron | c8b9595 | 2012-09-02 21:27:56 +0100 | [diff] [blame] | 1148 | MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1149 | MODULE_DESCRIPTION("Industrial I/O core"); |
| 1150 | MODULE_LICENSE("GPL"); |