Commit | Line | Data |
---|---|---|
847ec80b JC |
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 | ||
3176dd5d SK |
12 | #define pr_fmt(fmt) "iio-core: " fmt |
13 | ||
847ec80b JC |
14 | #include <linux/kernel.h> |
15 | #include <linux/module.h> | |
16 | #include <linux/idr.h> | |
17 | #include <linux/kdev_t.h> | |
18 | #include <linux/err.h> | |
19 | #include <linux/device.h> | |
20 | #include <linux/fs.h> | |
847ec80b | 21 | #include <linux/poll.h> |
ffc18afa | 22 | #include <linux/sched.h> |
4439c935 | 23 | #include <linux/wait.h> |
847ec80b | 24 | #include <linux/cdev.h> |
5a0e3ad6 | 25 | #include <linux/slab.h> |
8e7d9672 | 26 | #include <linux/anon_inodes.h> |
e553f182 | 27 | #include <linux/debugfs.h> |
06458e27 | 28 | #include <linux/iio/iio.h> |
df9c1c42 | 29 | #include "iio_core.h" |
6aea1c36 | 30 | #include "iio_core_trigger.h" |
06458e27 JC |
31 | #include <linux/iio/sysfs.h> |
32 | #include <linux/iio/events.h> | |
9e69c935 | 33 | #include <linux/iio/buffer.h> |
9dd1cb30 | 34 | |
99698b45 | 35 | /* IDA to assign each registered device a unique id */ |
b156cf70 | 36 | static DEFINE_IDA(iio_ida); |
847ec80b | 37 | |
f625cb97 | 38 | static dev_t iio_devt; |
847ec80b JC |
39 | |
40 | #define IIO_DEV_MAX 256 | |
5aaaeba8 | 41 | struct bus_type iio_bus_type = { |
847ec80b | 42 | .name = "iio", |
847ec80b | 43 | }; |
5aaaeba8 | 44 | EXPORT_SYMBOL(iio_bus_type); |
847ec80b | 45 | |
e553f182 MH |
46 | static struct dentry *iio_debugfs_dentry; |
47 | ||
c6fc8062 JC |
48 | static const char * const iio_direction[] = { |
49 | [0] = "in", | |
50 | [1] = "out", | |
51 | }; | |
52 | ||
ade7ef7b | 53 | static const char * const iio_chan_type_name_spec[] = { |
c6fc8062 | 54 | [IIO_VOLTAGE] = "voltage", |
faf290e8 MH |
55 | [IIO_CURRENT] = "current", |
56 | [IIO_POWER] = "power", | |
9bff02f8 | 57 | [IIO_ACCEL] = "accel", |
41ea040c | 58 | [IIO_ANGL_VEL] = "anglvel", |
1d892719 | 59 | [IIO_MAGN] = "magn", |
9bff02f8 BF |
60 | [IIO_LIGHT] = "illuminance", |
61 | [IIO_INTENSITY] = "intensity", | |
f09f2c81 | 62 | [IIO_PROXIMITY] = "proximity", |
9bff02f8 | 63 | [IIO_TEMP] = "temp", |
1d892719 JC |
64 | [IIO_INCLI] = "incli", |
65 | [IIO_ROT] = "rot", | |
1d892719 | 66 | [IIO_ANGL] = "angl", |
9bff02f8 | 67 | [IIO_TIMESTAMP] = "timestamp", |
66dbe704 | 68 | [IIO_CAPACITANCE] = "capacitance", |
a6b12855 | 69 | [IIO_ALTVOLTAGE] = "altvoltage", |
21cd1fab | 70 | [IIO_CCT] = "cct", |
c4f0c693 | 71 | [IIO_PRESSURE] = "pressure", |
ac216aa2 | 72 | [IIO_HUMIDITYRELATIVE] = "humidityrelative", |
55aebeb9 | 73 | [IIO_ACTIVITY] = "activity", |
a88bfe78 | 74 | [IIO_STEPS] = "steps", |
72c66644 | 75 | [IIO_ENERGY] = "energy", |
cc3c9eec | 76 | [IIO_DISTANCE] = "distance", |
5a1a9329 | 77 | [IIO_VELOCITY] = "velocity", |
8ff6b3bc | 78 | [IIO_CONCENTRATION] = "concentration", |
d38d5469 | 79 | [IIO_RESISTANCE] = "resistance", |
ecb3a7cc | 80 | [IIO_PH] = "ph", |
1d892719 JC |
81 | }; |
82 | ||
330c6c57 | 83 | static const char * const iio_modifier_names[] = { |
1d892719 JC |
84 | [IIO_MOD_X] = "x", |
85 | [IIO_MOD_Y] = "y", | |
86 | [IIO_MOD_Z] = "z", | |
4b8d8015 PM |
87 | [IIO_MOD_X_AND_Y] = "x&y", |
88 | [IIO_MOD_X_AND_Z] = "x&z", | |
89 | [IIO_MOD_Y_AND_Z] = "y&z", | |
90 | [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z", | |
91 | [IIO_MOD_X_OR_Y] = "x|y", | |
92 | [IIO_MOD_X_OR_Z] = "x|z", | |
93 | [IIO_MOD_Y_OR_Z] = "y|z", | |
94 | [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z", | |
8f5879b2 | 95 | [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)", |
cf82cb81 | 96 | [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2", |
330c6c57 JC |
97 | [IIO_MOD_LIGHT_BOTH] = "both", |
98 | [IIO_MOD_LIGHT_IR] = "ir", | |
21cd1fab JB |
99 | [IIO_MOD_LIGHT_CLEAR] = "clear", |
100 | [IIO_MOD_LIGHT_RED] = "red", | |
101 | [IIO_MOD_LIGHT_GREEN] = "green", | |
102 | [IIO_MOD_LIGHT_BLUE] = "blue", | |
5082f405 | 103 | [IIO_MOD_QUATERNION] = "quaternion", |
638b43b3 PM |
104 | [IIO_MOD_TEMP_AMBIENT] = "ambient", |
105 | [IIO_MOD_TEMP_OBJECT] = "object", | |
11b8ddab RA |
106 | [IIO_MOD_NORTH_MAGN] = "from_north_magnetic", |
107 | [IIO_MOD_NORTH_TRUE] = "from_north_true", | |
108 | [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp", | |
109 | [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp", | |
55aebeb9 DB |
110 | [IIO_MOD_RUNNING] = "running", |
111 | [IIO_MOD_JOGGING] = "jogging", | |
112 | [IIO_MOD_WALKING] = "walking", | |
113 | [IIO_MOD_STILL] = "still", | |
5a1a9329 | 114 | [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)", |
1ce87f21 LPC |
115 | [IIO_MOD_I] = "i", |
116 | [IIO_MOD_Q] = "q", | |
8ff6b3bc MR |
117 | [IIO_MOD_CO2] = "co2", |
118 | [IIO_MOD_VOC] = "voc", | |
1d892719 JC |
119 | }; |
120 | ||
121 | /* relies on pairs of these shared then separate */ | |
122 | static const char * const iio_chan_info_postfix[] = { | |
75a973c7 JC |
123 | [IIO_CHAN_INFO_RAW] = "raw", |
124 | [IIO_CHAN_INFO_PROCESSED] = "input", | |
c8a9f805 JC |
125 | [IIO_CHAN_INFO_SCALE] = "scale", |
126 | [IIO_CHAN_INFO_OFFSET] = "offset", | |
127 | [IIO_CHAN_INFO_CALIBSCALE] = "calibscale", | |
128 | [IIO_CHAN_INFO_CALIBBIAS] = "calibbias", | |
129 | [IIO_CHAN_INFO_PEAK] = "peak_raw", | |
130 | [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale", | |
131 | [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw", | |
132 | [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", | |
df94aba8 JC |
133 | [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] |
134 | = "filter_low_pass_3db_frequency", | |
3f7f642b MF |
135 | [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY] |
136 | = "filter_high_pass_3db_frequency", | |
ce85a1cb | 137 | [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", |
a6b12855 MH |
138 | [IIO_CHAN_INFO_FREQUENCY] = "frequency", |
139 | [IIO_CHAN_INFO_PHASE] = "phase", | |
b65d6212 | 140 | [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", |
7c9ab035 | 141 | [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", |
899d90bd | 142 | [IIO_CHAN_INFO_INT_TIME] = "integration_time", |
a88bfe78 | 143 | [IIO_CHAN_INFO_ENABLE] = "en", |
bcdf28fb | 144 | [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight", |
d37f6836 | 145 | [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight", |
2f0ecb7c IT |
146 | [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count", |
147 | [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time", | |
c8a85854 | 148 | [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity", |
faaa4495 | 149 | [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio", |
1d892719 JC |
150 | }; |
151 | ||
a7e57dce SK |
152 | /** |
153 | * iio_find_channel_from_si() - get channel from its scan index | |
154 | * @indio_dev: device | |
155 | * @si: scan index to match | |
156 | */ | |
5fb21c82 JC |
157 | const struct iio_chan_spec |
158 | *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) | |
159 | { | |
160 | int i; | |
161 | ||
162 | for (i = 0; i < indio_dev->num_channels; i++) | |
163 | if (indio_dev->channels[i].scan_index == si) | |
164 | return &indio_dev->channels[i]; | |
165 | return NULL; | |
166 | } | |
167 | ||
847ec80b JC |
168 | /* This turns up an awful lot */ |
169 | ssize_t iio_read_const_attr(struct device *dev, | |
170 | struct device_attribute *attr, | |
171 | char *buf) | |
172 | { | |
173 | return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); | |
174 | } | |
175 | EXPORT_SYMBOL(iio_read_const_attr); | |
176 | ||
847ec80b JC |
177 | static int __init iio_init(void) |
178 | { | |
179 | int ret; | |
180 | ||
5aaaeba8 JC |
181 | /* Register sysfs bus */ |
182 | ret = bus_register(&iio_bus_type); | |
847ec80b | 183 | if (ret < 0) { |
3176dd5d | 184 | pr_err("could not register bus type\n"); |
847ec80b JC |
185 | goto error_nothing; |
186 | } | |
187 | ||
9aa1a167 JC |
188 | ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); |
189 | if (ret < 0) { | |
3176dd5d | 190 | pr_err("failed to allocate char dev region\n"); |
5aaaeba8 | 191 | goto error_unregister_bus_type; |
9aa1a167 | 192 | } |
847ec80b | 193 | |
e553f182 MH |
194 | iio_debugfs_dentry = debugfs_create_dir("iio", NULL); |
195 | ||
847ec80b JC |
196 | return 0; |
197 | ||
5aaaeba8 JC |
198 | error_unregister_bus_type: |
199 | bus_unregister(&iio_bus_type); | |
847ec80b JC |
200 | error_nothing: |
201 | return ret; | |
202 | } | |
203 | ||
204 | static void __exit iio_exit(void) | |
205 | { | |
9aa1a167 JC |
206 | if (iio_devt) |
207 | unregister_chrdev_region(iio_devt, IIO_DEV_MAX); | |
5aaaeba8 | 208 | bus_unregister(&iio_bus_type); |
e553f182 MH |
209 | debugfs_remove(iio_debugfs_dentry); |
210 | } | |
211 | ||
212 | #if defined(CONFIG_DEBUG_FS) | |
e553f182 MH |
213 | static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, |
214 | size_t count, loff_t *ppos) | |
215 | { | |
216 | struct iio_dev *indio_dev = file->private_data; | |
217 | char buf[20]; | |
218 | unsigned val = 0; | |
219 | ssize_t len; | |
220 | int ret; | |
221 | ||
222 | ret = indio_dev->info->debugfs_reg_access(indio_dev, | |
223 | indio_dev->cached_reg_addr, | |
224 | 0, &val); | |
225 | if (ret) | |
226 | dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); | |
227 | ||
228 | len = snprintf(buf, sizeof(buf), "0x%X\n", val); | |
229 | ||
230 | return simple_read_from_buffer(userbuf, count, ppos, buf, len); | |
231 | } | |
232 | ||
233 | static ssize_t iio_debugfs_write_reg(struct file *file, | |
234 | const char __user *userbuf, size_t count, loff_t *ppos) | |
235 | { | |
236 | struct iio_dev *indio_dev = file->private_data; | |
237 | unsigned reg, val; | |
238 | char buf[80]; | |
239 | int ret; | |
240 | ||
241 | count = min_t(size_t, count, (sizeof(buf)-1)); | |
242 | if (copy_from_user(buf, userbuf, count)) | |
243 | return -EFAULT; | |
244 | ||
245 | buf[count] = 0; | |
246 | ||
247 | ret = sscanf(buf, "%i %i", ®, &val); | |
248 | ||
249 | switch (ret) { | |
250 | case 1: | |
251 | indio_dev->cached_reg_addr = reg; | |
252 | break; | |
253 | case 2: | |
254 | indio_dev->cached_reg_addr = reg; | |
255 | ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, | |
256 | val, NULL); | |
257 | if (ret) { | |
258 | dev_err(indio_dev->dev.parent, "%s: write failed\n", | |
259 | __func__); | |
260 | return ret; | |
261 | } | |
262 | break; | |
263 | default: | |
264 | return -EINVAL; | |
265 | } | |
266 | ||
267 | return count; | |
268 | } | |
269 | ||
270 | static const struct file_operations iio_debugfs_reg_fops = { | |
5a28c873 | 271 | .open = simple_open, |
e553f182 MH |
272 | .read = iio_debugfs_read_reg, |
273 | .write = iio_debugfs_write_reg, | |
274 | }; | |
275 | ||
276 | static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) | |
277 | { | |
278 | debugfs_remove_recursive(indio_dev->debugfs_dentry); | |
847ec80b JC |
279 | } |
280 | ||
e553f182 MH |
281 | static int iio_device_register_debugfs(struct iio_dev *indio_dev) |
282 | { | |
283 | struct dentry *d; | |
284 | ||
285 | if (indio_dev->info->debugfs_reg_access == NULL) | |
286 | return 0; | |
287 | ||
abd5a2fb | 288 | if (!iio_debugfs_dentry) |
e553f182 MH |
289 | return 0; |
290 | ||
291 | indio_dev->debugfs_dentry = | |
292 | debugfs_create_dir(dev_name(&indio_dev->dev), | |
293 | iio_debugfs_dentry); | |
e553f182 MH |
294 | if (indio_dev->debugfs_dentry == NULL) { |
295 | dev_warn(indio_dev->dev.parent, | |
296 | "Failed to create debugfs directory\n"); | |
297 | return -EFAULT; | |
298 | } | |
299 | ||
300 | d = debugfs_create_file("direct_reg_access", 0644, | |
301 | indio_dev->debugfs_dentry, | |
302 | indio_dev, &iio_debugfs_reg_fops); | |
303 | if (!d) { | |
304 | iio_device_unregister_debugfs(indio_dev); | |
305 | return -ENOMEM; | |
306 | } | |
307 | ||
308 | return 0; | |
309 | } | |
310 | #else | |
311 | static int iio_device_register_debugfs(struct iio_dev *indio_dev) | |
312 | { | |
313 | return 0; | |
314 | } | |
315 | ||
316 | static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) | |
317 | { | |
318 | } | |
319 | #endif /* CONFIG_DEBUG_FS */ | |
320 | ||
4fee7e16 LPC |
321 | static ssize_t iio_read_channel_ext_info(struct device *dev, |
322 | struct device_attribute *attr, | |
323 | char *buf) | |
324 | { | |
e53f5ac5 | 325 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
4fee7e16 LPC |
326 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
327 | const struct iio_chan_spec_ext_info *ext_info; | |
328 | ||
329 | ext_info = &this_attr->c->ext_info[this_attr->address]; | |
330 | ||
fc6d1139 | 331 | return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); |
4fee7e16 LPC |
332 | } |
333 | ||
334 | static ssize_t iio_write_channel_ext_info(struct device *dev, | |
335 | struct device_attribute *attr, | |
336 | const char *buf, | |
337 | size_t len) | |
338 | { | |
e53f5ac5 | 339 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
4fee7e16 LPC |
340 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
341 | const struct iio_chan_spec_ext_info *ext_info; | |
342 | ||
343 | ext_info = &this_attr->c->ext_info[this_attr->address]; | |
344 | ||
fc6d1139 MH |
345 | return ext_info->write(indio_dev, ext_info->private, |
346 | this_attr->c, buf, len); | |
4fee7e16 LPC |
347 | } |
348 | ||
5212cc8a LPC |
349 | ssize_t iio_enum_available_read(struct iio_dev *indio_dev, |
350 | uintptr_t priv, const struct iio_chan_spec *chan, char *buf) | |
351 | { | |
352 | const struct iio_enum *e = (const struct iio_enum *)priv; | |
353 | unsigned int i; | |
354 | size_t len = 0; | |
355 | ||
356 | if (!e->num_items) | |
357 | return 0; | |
358 | ||
359 | for (i = 0; i < e->num_items; ++i) | |
74dcd439 | 360 | len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]); |
5212cc8a LPC |
361 | |
362 | /* replace last space with a newline */ | |
363 | buf[len - 1] = '\n'; | |
364 | ||
365 | return len; | |
366 | } | |
367 | EXPORT_SYMBOL_GPL(iio_enum_available_read); | |
368 | ||
369 | ssize_t iio_enum_read(struct iio_dev *indio_dev, | |
370 | uintptr_t priv, const struct iio_chan_spec *chan, char *buf) | |
371 | { | |
372 | const struct iio_enum *e = (const struct iio_enum *)priv; | |
373 | int i; | |
374 | ||
375 | if (!e->get) | |
376 | return -EINVAL; | |
377 | ||
378 | i = e->get(indio_dev, chan); | |
379 | if (i < 0) | |
380 | return i; | |
381 | else if (i >= e->num_items) | |
382 | return -EINVAL; | |
383 | ||
598db581 | 384 | return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]); |
5212cc8a LPC |
385 | } |
386 | EXPORT_SYMBOL_GPL(iio_enum_read); | |
387 | ||
388 | ssize_t iio_enum_write(struct iio_dev *indio_dev, | |
389 | uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, | |
390 | size_t len) | |
391 | { | |
392 | const struct iio_enum *e = (const struct iio_enum *)priv; | |
393 | unsigned int i; | |
394 | int ret; | |
395 | ||
396 | if (!e->set) | |
397 | return -EINVAL; | |
398 | ||
399 | for (i = 0; i < e->num_items; i++) { | |
400 | if (sysfs_streq(buf, e->items[i])) | |
401 | break; | |
402 | } | |
403 | ||
404 | if (i == e->num_items) | |
405 | return -EINVAL; | |
406 | ||
407 | ret = e->set(indio_dev, chan, i); | |
408 | return ret ? ret : len; | |
409 | } | |
410 | EXPORT_SYMBOL_GPL(iio_enum_write); | |
411 | ||
3661f3f5 LPC |
412 | /** |
413 | * iio_format_value() - Formats a IIO value into its string representation | |
2498dcf6 CO |
414 | * @buf: The buffer to which the formatted value gets written |
415 | * @type: One of the IIO_VAL_... constants. This decides how the val | |
416 | * and val2 parameters are formatted. | |
417 | * @size: Number of IIO value entries contained in vals | |
418 | * @vals: Pointer to the values, exact meaning depends on the | |
419 | * type parameter. | |
420 | * | |
421 | * Return: 0 by default, a negative number on failure or the | |
422 | * total number of characters written for a type that belongs | |
423 | * to the IIO_VAL_... constant. | |
3661f3f5 | 424 | */ |
9fbfb4b3 | 425 | ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) |
847ec80b | 426 | { |
7985e7c1 | 427 | unsigned long long tmp; |
67eedba3 | 428 | bool scale_db = false; |
1d892719 | 429 | |
3661f3f5 | 430 | switch (type) { |
67eedba3 | 431 | case IIO_VAL_INT: |
9fbfb4b3 | 432 | return sprintf(buf, "%d\n", vals[0]); |
67eedba3 MH |
433 | case IIO_VAL_INT_PLUS_MICRO_DB: |
434 | scale_db = true; | |
435 | case IIO_VAL_INT_PLUS_MICRO: | |
9fbfb4b3 | 436 | if (vals[1] < 0) |
8f57e4d9 MN |
437 | return sprintf(buf, "-%d.%06u%s\n", abs(vals[0]), |
438 | -vals[1], scale_db ? " dB" : ""); | |
1d892719 | 439 | else |
9fbfb4b3 | 440 | return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1], |
67eedba3 MH |
441 | scale_db ? " dB" : ""); |
442 | case IIO_VAL_INT_PLUS_NANO: | |
9fbfb4b3 | 443 | if (vals[1] < 0) |
8f57e4d9 MN |
444 | return sprintf(buf, "-%d.%09u\n", abs(vals[0]), |
445 | -vals[1]); | |
71646e2c | 446 | else |
9fbfb4b3 | 447 | return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); |
7985e7c1 | 448 | case IIO_VAL_FRACTIONAL: |
9fbfb4b3 SP |
449 | tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]); |
450 | vals[1] = do_div(tmp, 1000000000LL); | |
451 | vals[0] = tmp; | |
452 | return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); | |
103d9fb9 | 453 | case IIO_VAL_FRACTIONAL_LOG2: |
9fbfb4b3 SP |
454 | tmp = (s64)vals[0] * 1000000000LL >> vals[1]; |
455 | vals[1] = do_div(tmp, 1000000000LL); | |
456 | vals[0] = tmp; | |
457 | return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); | |
458 | case IIO_VAL_INT_MULTIPLE: | |
459 | { | |
460 | int i; | |
461 | int len = 0; | |
462 | ||
463 | for (i = 0; i < size; ++i) | |
464 | len += snprintf(&buf[len], PAGE_SIZE - len, "%d ", | |
465 | vals[i]); | |
466 | len += snprintf(&buf[len], PAGE_SIZE - len, "\n"); | |
467 | return len; | |
468 | } | |
67eedba3 | 469 | default: |
1d892719 | 470 | return 0; |
67eedba3 | 471 | } |
1d892719 | 472 | } |
7d2c2aca | 473 | EXPORT_SYMBOL_GPL(iio_format_value); |
1d892719 | 474 | |
3661f3f5 LPC |
475 | static ssize_t iio_read_channel_info(struct device *dev, |
476 | struct device_attribute *attr, | |
477 | char *buf) | |
478 | { | |
479 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); | |
480 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
9fbfb4b3 SP |
481 | int vals[INDIO_MAX_RAW_ELEMENTS]; |
482 | int ret; | |
483 | int val_len = 2; | |
484 | ||
485 | if (indio_dev->info->read_raw_multi) | |
486 | ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c, | |
487 | INDIO_MAX_RAW_ELEMENTS, | |
488 | vals, &val_len, | |
489 | this_attr->address); | |
490 | else | |
491 | ret = indio_dev->info->read_raw(indio_dev, this_attr->c, | |
492 | &vals[0], &vals[1], this_attr->address); | |
3661f3f5 LPC |
493 | |
494 | if (ret < 0) | |
495 | return ret; | |
496 | ||
9fbfb4b3 | 497 | return iio_format_value(buf, ret, val_len, vals); |
3661f3f5 LPC |
498 | } |
499 | ||
6807d721 LPC |
500 | /** |
501 | * iio_str_to_fixpoint() - Parse a fixed-point number from a string | |
502 | * @str: The string to parse | |
503 | * @fract_mult: Multiplier for the first decimal place, should be a power of 10 | |
504 | * @integer: The integer part of the number | |
505 | * @fract: The fractional part of the number | |
506 | * | |
507 | * Returns 0 on success, or a negative error code if the string could not be | |
508 | * parsed. | |
509 | */ | |
510 | int iio_str_to_fixpoint(const char *str, int fract_mult, | |
511 | int *integer, int *fract) | |
512 | { | |
513 | int i = 0, f = 0; | |
514 | bool integer_part = true, negative = false; | |
515 | ||
f47dff32 SN |
516 | if (fract_mult == 0) { |
517 | *fract = 0; | |
518 | ||
519 | return kstrtoint(str, 0, integer); | |
520 | } | |
521 | ||
6807d721 LPC |
522 | if (str[0] == '-') { |
523 | negative = true; | |
524 | str++; | |
525 | } else if (str[0] == '+') { | |
526 | str++; | |
527 | } | |
528 | ||
529 | while (*str) { | |
530 | if ('0' <= *str && *str <= '9') { | |
531 | if (integer_part) { | |
532 | i = i * 10 + *str - '0'; | |
533 | } else { | |
534 | f += fract_mult * (*str - '0'); | |
535 | fract_mult /= 10; | |
536 | } | |
537 | } else if (*str == '\n') { | |
538 | if (*(str + 1) == '\0') | |
539 | break; | |
540 | else | |
541 | return -EINVAL; | |
542 | } else if (*str == '.' && integer_part) { | |
543 | integer_part = false; | |
544 | } else { | |
545 | return -EINVAL; | |
546 | } | |
547 | str++; | |
548 | } | |
549 | ||
550 | if (negative) { | |
551 | if (i) | |
552 | i = -i; | |
553 | else | |
554 | f = -f; | |
555 | } | |
556 | ||
557 | *integer = i; | |
558 | *fract = f; | |
559 | ||
560 | return 0; | |
561 | } | |
562 | EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); | |
563 | ||
1d892719 JC |
564 | static ssize_t iio_write_channel_info(struct device *dev, |
565 | struct device_attribute *attr, | |
566 | const char *buf, | |
567 | size_t len) | |
568 | { | |
e53f5ac5 | 569 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
1d892719 | 570 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
6807d721 LPC |
571 | int ret, fract_mult = 100000; |
572 | int integer, fract; | |
1d892719 JC |
573 | |
574 | /* Assumes decimal - precision based on number of digits */ | |
6fe8135f | 575 | if (!indio_dev->info->write_raw) |
1d892719 | 576 | return -EINVAL; |
5c04af04 MH |
577 | |
578 | if (indio_dev->info->write_raw_get_fmt) | |
579 | switch (indio_dev->info->write_raw_get_fmt(indio_dev, | |
580 | this_attr->c, this_attr->address)) { | |
f47dff32 SN |
581 | case IIO_VAL_INT: |
582 | fract_mult = 0; | |
583 | break; | |
5c04af04 MH |
584 | case IIO_VAL_INT_PLUS_MICRO: |
585 | fract_mult = 100000; | |
586 | break; | |
587 | case IIO_VAL_INT_PLUS_NANO: | |
588 | fract_mult = 100000000; | |
589 | break; | |
590 | default: | |
591 | return -EINVAL; | |
592 | } | |
593 | ||
6807d721 LPC |
594 | ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract); |
595 | if (ret) | |
596 | return ret; | |
1d892719 | 597 | |
6fe8135f | 598 | ret = indio_dev->info->write_raw(indio_dev, this_attr->c, |
5c04af04 | 599 | integer, fract, this_attr->address); |
1d892719 JC |
600 | if (ret) |
601 | return ret; | |
602 | ||
603 | return len; | |
604 | } | |
605 | ||
df9c1c42 | 606 | static |
1d892719 JC |
607 | int __iio_device_attr_init(struct device_attribute *dev_attr, |
608 | const char *postfix, | |
609 | struct iio_chan_spec const *chan, | |
610 | ssize_t (*readfunc)(struct device *dev, | |
611 | struct device_attribute *attr, | |
612 | char *buf), | |
613 | ssize_t (*writefunc)(struct device *dev, | |
614 | struct device_attribute *attr, | |
615 | const char *buf, | |
616 | size_t len), | |
3704432f | 617 | enum iio_shared_by shared_by) |
1d892719 | 618 | { |
3704432f | 619 | int ret = 0; |
7bbcf7e1 | 620 | char *name = NULL; |
3704432f | 621 | char *full_postfix; |
1d892719 | 622 | sysfs_attr_init(&dev_attr->attr); |
1d892719 | 623 | |
ade7ef7b | 624 | /* Build up postfix of <extend_name>_<modifier>_postfix */ |
3704432f | 625 | if (chan->modified && (shared_by == IIO_SEPARATE)) { |
ade7ef7b JC |
626 | if (chan->extend_name) |
627 | full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", | |
628 | iio_modifier_names[chan | |
629 | ->channel2], | |
630 | chan->extend_name, | |
631 | postfix); | |
632 | else | |
633 | full_postfix = kasprintf(GFP_KERNEL, "%s_%s", | |
634 | iio_modifier_names[chan | |
635 | ->channel2], | |
636 | postfix); | |
637 | } else { | |
77bfa8ba | 638 | if (chan->extend_name == NULL || shared_by != IIO_SEPARATE) |
ade7ef7b JC |
639 | full_postfix = kstrdup(postfix, GFP_KERNEL); |
640 | else | |
641 | full_postfix = kasprintf(GFP_KERNEL, | |
642 | "%s_%s", | |
643 | chan->extend_name, | |
644 | postfix); | |
645 | } | |
3704432f JC |
646 | if (full_postfix == NULL) |
647 | return -ENOMEM; | |
1d892719 | 648 | |
4abf6f8b | 649 | if (chan->differential) { /* Differential can not have modifier */ |
3704432f | 650 | switch (shared_by) { |
c006ec83 | 651 | case IIO_SHARED_BY_ALL: |
7bbcf7e1 | 652 | name = kasprintf(GFP_KERNEL, "%s", full_postfix); |
c006ec83 JC |
653 | break; |
654 | case IIO_SHARED_BY_DIR: | |
7bbcf7e1 | 655 | name = kasprintf(GFP_KERNEL, "%s_%s", |
c006ec83 JC |
656 | iio_direction[chan->output], |
657 | full_postfix); | |
658 | break; | |
3704432f | 659 | case IIO_SHARED_BY_TYPE: |
7bbcf7e1 | 660 | name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", |
ade7ef7b JC |
661 | iio_direction[chan->output], |
662 | iio_chan_type_name_spec[chan->type], | |
663 | iio_chan_type_name_spec[chan->type], | |
664 | full_postfix); | |
3704432f JC |
665 | break; |
666 | case IIO_SEPARATE: | |
667 | if (!chan->indexed) { | |
231bfe53 | 668 | WARN(1, "Differential channels must be indexed\n"); |
3704432f JC |
669 | ret = -EINVAL; |
670 | goto error_free_full_postfix; | |
671 | } | |
7bbcf7e1 | 672 | name = kasprintf(GFP_KERNEL, |
3704432f | 673 | "%s_%s%d-%s%d_%s", |
ade7ef7b JC |
674 | iio_direction[chan->output], |
675 | iio_chan_type_name_spec[chan->type], | |
676 | chan->channel, | |
677 | iio_chan_type_name_spec[chan->type], | |
678 | chan->channel2, | |
679 | full_postfix); | |
3704432f | 680 | break; |
ade7ef7b JC |
681 | } |
682 | } else { /* Single ended */ | |
3704432f | 683 | switch (shared_by) { |
c006ec83 | 684 | case IIO_SHARED_BY_ALL: |
7bbcf7e1 | 685 | name = kasprintf(GFP_KERNEL, "%s", full_postfix); |
c006ec83 JC |
686 | break; |
687 | case IIO_SHARED_BY_DIR: | |
7bbcf7e1 | 688 | name = kasprintf(GFP_KERNEL, "%s_%s", |
c006ec83 JC |
689 | iio_direction[chan->output], |
690 | full_postfix); | |
691 | break; | |
3704432f | 692 | case IIO_SHARED_BY_TYPE: |
7bbcf7e1 | 693 | name = kasprintf(GFP_KERNEL, "%s_%s_%s", |
ade7ef7b JC |
694 | iio_direction[chan->output], |
695 | iio_chan_type_name_spec[chan->type], | |
696 | full_postfix); | |
3704432f JC |
697 | break; |
698 | ||
699 | case IIO_SEPARATE: | |
700 | if (chan->indexed) | |
7bbcf7e1 | 701 | name = kasprintf(GFP_KERNEL, "%s_%s%d_%s", |
3704432f JC |
702 | iio_direction[chan->output], |
703 | iio_chan_type_name_spec[chan->type], | |
704 | chan->channel, | |
705 | full_postfix); | |
706 | else | |
7bbcf7e1 | 707 | name = kasprintf(GFP_KERNEL, "%s_%s_%s", |
3704432f JC |
708 | iio_direction[chan->output], |
709 | iio_chan_type_name_spec[chan->type], | |
710 | full_postfix); | |
711 | break; | |
712 | } | |
ade7ef7b | 713 | } |
7bbcf7e1 | 714 | if (name == NULL) { |
1d892719 JC |
715 | ret = -ENOMEM; |
716 | goto error_free_full_postfix; | |
717 | } | |
7bbcf7e1 | 718 | dev_attr->attr.name = name; |
1d892719 JC |
719 | |
720 | if (readfunc) { | |
721 | dev_attr->attr.mode |= S_IRUGO; | |
722 | dev_attr->show = readfunc; | |
723 | } | |
724 | ||
725 | if (writefunc) { | |
726 | dev_attr->attr.mode |= S_IWUSR; | |
727 | dev_attr->store = writefunc; | |
728 | } | |
7bbcf7e1 | 729 | |
1d892719 JC |
730 | error_free_full_postfix: |
731 | kfree(full_postfix); | |
3704432f | 732 | |
1d892719 JC |
733 | return ret; |
734 | } | |
735 | ||
df9c1c42 | 736 | static void __iio_device_attr_deinit(struct device_attribute *dev_attr) |
1d892719 JC |
737 | { |
738 | kfree(dev_attr->attr.name); | |
739 | } | |
740 | ||
741 | int __iio_add_chan_devattr(const char *postfix, | |
1d892719 JC |
742 | struct iio_chan_spec const *chan, |
743 | ssize_t (*readfunc)(struct device *dev, | |
744 | struct device_attribute *attr, | |
745 | char *buf), | |
746 | ssize_t (*writefunc)(struct device *dev, | |
747 | struct device_attribute *attr, | |
748 | const char *buf, | |
749 | size_t len), | |
e614a54b | 750 | u64 mask, |
3704432f | 751 | enum iio_shared_by shared_by, |
1d892719 JC |
752 | struct device *dev, |
753 | struct list_head *attr_list) | |
754 | { | |
755 | int ret; | |
756 | struct iio_dev_attr *iio_attr, *t; | |
757 | ||
670c1103 | 758 | iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL); |
92825ff9 HK |
759 | if (iio_attr == NULL) |
760 | return -ENOMEM; | |
1d892719 JC |
761 | ret = __iio_device_attr_init(&iio_attr->dev_attr, |
762 | postfix, chan, | |
3704432f | 763 | readfunc, writefunc, shared_by); |
1d892719 JC |
764 | if (ret) |
765 | goto error_iio_dev_attr_free; | |
766 | iio_attr->c = chan; | |
767 | iio_attr->address = mask; | |
768 | list_for_each_entry(t, attr_list, l) | |
769 | if (strcmp(t->dev_attr.attr.name, | |
770 | iio_attr->dev_attr.attr.name) == 0) { | |
3704432f | 771 | if (shared_by == IIO_SEPARATE) |
1d892719 JC |
772 | dev_err(dev, "tried to double register : %s\n", |
773 | t->dev_attr.attr.name); | |
774 | ret = -EBUSY; | |
775 | goto error_device_attr_deinit; | |
776 | } | |
1d892719 JC |
777 | list_add(&iio_attr->l, attr_list); |
778 | ||
779 | return 0; | |
780 | ||
781 | error_device_attr_deinit: | |
782 | __iio_device_attr_deinit(&iio_attr->dev_attr); | |
783 | error_iio_dev_attr_free: | |
784 | kfree(iio_attr); | |
1d892719 JC |
785 | return ret; |
786 | } | |
787 | ||
3704432f JC |
788 | static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, |
789 | struct iio_chan_spec const *chan, | |
790 | enum iio_shared_by shared_by, | |
791 | const long *infomask) | |
1d892719 | 792 | { |
3704432f | 793 | int i, ret, attrcount = 0; |
1d892719 | 794 | |
3704432f | 795 | for_each_set_bit(i, infomask, sizeof(infomask)*8) { |
ef4b4856 JC |
796 | if (i >= ARRAY_SIZE(iio_chan_info_postfix)) |
797 | return -EINVAL; | |
8655cc49 JC |
798 | ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], |
799 | chan, | |
800 | &iio_read_channel_info, | |
801 | &iio_write_channel_info, | |
802 | i, | |
3704432f | 803 | shared_by, |
8655cc49 JC |
804 | &indio_dev->dev, |
805 | &indio_dev->channel_attr_list); | |
3704432f | 806 | if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) |
8655cc49 | 807 | continue; |
3704432f JC |
808 | else if (ret < 0) |
809 | return ret; | |
8655cc49 JC |
810 | attrcount++; |
811 | } | |
5f420b42 | 812 | |
3704432f JC |
813 | return attrcount; |
814 | } | |
815 | ||
816 | static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, | |
817 | struct iio_chan_spec const *chan) | |
818 | { | |
819 | int ret, attrcount = 0; | |
820 | const struct iio_chan_spec_ext_info *ext_info; | |
821 | ||
822 | if (chan->channel < 0) | |
823 | return 0; | |
824 | ret = iio_device_add_info_mask_type(indio_dev, chan, | |
825 | IIO_SEPARATE, | |
826 | &chan->info_mask_separate); | |
827 | if (ret < 0) | |
828 | return ret; | |
829 | attrcount += ret; | |
830 | ||
831 | ret = iio_device_add_info_mask_type(indio_dev, chan, | |
832 | IIO_SHARED_BY_TYPE, | |
833 | &chan->info_mask_shared_by_type); | |
834 | if (ret < 0) | |
835 | return ret; | |
836 | attrcount += ret; | |
837 | ||
c006ec83 JC |
838 | ret = iio_device_add_info_mask_type(indio_dev, chan, |
839 | IIO_SHARED_BY_DIR, | |
840 | &chan->info_mask_shared_by_dir); | |
841 | if (ret < 0) | |
842 | return ret; | |
843 | attrcount += ret; | |
844 | ||
845 | ret = iio_device_add_info_mask_type(indio_dev, chan, | |
846 | IIO_SHARED_BY_ALL, | |
847 | &chan->info_mask_shared_by_all); | |
848 | if (ret < 0) | |
849 | return ret; | |
850 | attrcount += ret; | |
851 | ||
5f420b42 LPC |
852 | if (chan->ext_info) { |
853 | unsigned int i = 0; | |
854 | for (ext_info = chan->ext_info; ext_info->name; ext_info++) { | |
855 | ret = __iio_add_chan_devattr(ext_info->name, | |
856 | chan, | |
857 | ext_info->read ? | |
858 | &iio_read_channel_ext_info : NULL, | |
859 | ext_info->write ? | |
860 | &iio_write_channel_ext_info : NULL, | |
861 | i, | |
862 | ext_info->shared, | |
863 | &indio_dev->dev, | |
864 | &indio_dev->channel_attr_list); | |
865 | i++; | |
866 | if (ret == -EBUSY && ext_info->shared) | |
867 | continue; | |
868 | ||
869 | if (ret) | |
3704432f | 870 | return ret; |
5f420b42 LPC |
871 | |
872 | attrcount++; | |
873 | } | |
874 | } | |
875 | ||
3704432f | 876 | return attrcount; |
1d892719 JC |
877 | } |
878 | ||
84088ebd LPC |
879 | /** |
880 | * iio_free_chan_devattr_list() - Free a list of IIO device attributes | |
881 | * @attr_list: List of IIO device attributes | |
882 | * | |
883 | * This function frees the memory allocated for each of the IIO device | |
c1b03ab5 | 884 | * attributes in the list. |
84088ebd LPC |
885 | */ |
886 | void iio_free_chan_devattr_list(struct list_head *attr_list) | |
1d892719 | 887 | { |
84088ebd LPC |
888 | struct iio_dev_attr *p, *n; |
889 | ||
890 | list_for_each_entry_safe(p, n, attr_list, l) { | |
891 | kfree(p->dev_attr.attr.name); | |
c1b03ab5 | 892 | list_del(&p->l); |
84088ebd LPC |
893 | kfree(p); |
894 | } | |
1d892719 JC |
895 | } |
896 | ||
1b732888 JC |
897 | static ssize_t iio_show_dev_name(struct device *dev, |
898 | struct device_attribute *attr, | |
899 | char *buf) | |
900 | { | |
e53f5ac5 | 901 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
598db581 | 902 | return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name); |
1b732888 JC |
903 | } |
904 | ||
905 | static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); | |
906 | ||
f8c6f4e9 | 907 | static int iio_device_register_sysfs(struct iio_dev *indio_dev) |
1d892719 | 908 | { |
26d25ae3 | 909 | int i, ret = 0, attrcount, attrn, attrcount_orig = 0; |
84088ebd | 910 | struct iio_dev_attr *p; |
26d25ae3 | 911 | struct attribute **attr; |
1d892719 | 912 | |
26d25ae3 | 913 | /* First count elements in any existing group */ |
f8c6f4e9 JC |
914 | if (indio_dev->info->attrs) { |
915 | attr = indio_dev->info->attrs->attrs; | |
26d25ae3 JC |
916 | while (*attr++ != NULL) |
917 | attrcount_orig++; | |
847ec80b | 918 | } |
26d25ae3 | 919 | attrcount = attrcount_orig; |
1d892719 JC |
920 | /* |
921 | * New channel registration method - relies on the fact a group does | |
d25b3808 | 922 | * not need to be initialized if its name is NULL. |
1d892719 | 923 | */ |
f8c6f4e9 JC |
924 | if (indio_dev->channels) |
925 | for (i = 0; i < indio_dev->num_channels; i++) { | |
926 | ret = iio_device_add_channel_sysfs(indio_dev, | |
927 | &indio_dev | |
1d892719 JC |
928 | ->channels[i]); |
929 | if (ret < 0) | |
930 | goto error_clear_attrs; | |
26d25ae3 | 931 | attrcount += ret; |
1d892719 | 932 | } |
26d25ae3 | 933 | |
f8c6f4e9 | 934 | if (indio_dev->name) |
26d25ae3 JC |
935 | attrcount++; |
936 | ||
d83fb184 TM |
937 | indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1, |
938 | sizeof(indio_dev->chan_attr_group.attrs[0]), | |
939 | GFP_KERNEL); | |
f8c6f4e9 | 940 | if (indio_dev->chan_attr_group.attrs == NULL) { |
26d25ae3 JC |
941 | ret = -ENOMEM; |
942 | goto error_clear_attrs; | |
1b732888 | 943 | } |
26d25ae3 | 944 | /* Copy across original attributes */ |
f8c6f4e9 JC |
945 | if (indio_dev->info->attrs) |
946 | memcpy(indio_dev->chan_attr_group.attrs, | |
947 | indio_dev->info->attrs->attrs, | |
948 | sizeof(indio_dev->chan_attr_group.attrs[0]) | |
26d25ae3 JC |
949 | *attrcount_orig); |
950 | attrn = attrcount_orig; | |
951 | /* Add all elements from the list. */ | |
f8c6f4e9 JC |
952 | list_for_each_entry(p, &indio_dev->channel_attr_list, l) |
953 | indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; | |
954 | if (indio_dev->name) | |
955 | indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; | |
26d25ae3 | 956 | |
f8c6f4e9 JC |
957 | indio_dev->groups[indio_dev->groupcounter++] = |
958 | &indio_dev->chan_attr_group; | |
26d25ae3 | 959 | |
1d892719 | 960 | return 0; |
1b732888 | 961 | |
1d892719 | 962 | error_clear_attrs: |
84088ebd | 963 | iio_free_chan_devattr_list(&indio_dev->channel_attr_list); |
1d892719 | 964 | |
26d25ae3 | 965 | return ret; |
847ec80b JC |
966 | } |
967 | ||
f8c6f4e9 | 968 | static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) |
847ec80b | 969 | { |
1d892719 | 970 | |
84088ebd | 971 | iio_free_chan_devattr_list(&indio_dev->channel_attr_list); |
f8c6f4e9 | 972 | kfree(indio_dev->chan_attr_group.attrs); |
c1b03ab5 | 973 | indio_dev->chan_attr_group.attrs = NULL; |
847ec80b JC |
974 | } |
975 | ||
847ec80b JC |
976 | static void iio_dev_release(struct device *device) |
977 | { | |
e53f5ac5 | 978 | struct iio_dev *indio_dev = dev_to_iio_dev(device); |
735ad074 | 979 | if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED)) |
f8c6f4e9 JC |
980 | iio_device_unregister_trigger_consumer(indio_dev); |
981 | iio_device_unregister_eventset(indio_dev); | |
982 | iio_device_unregister_sysfs(indio_dev); | |
e407fd65 | 983 | |
9e69c935 LPC |
984 | iio_buffer_put(indio_dev->buffer); |
985 | ||
e407fd65 LPC |
986 | ida_simple_remove(&iio_ida, indio_dev->id); |
987 | kfree(indio_dev); | |
847ec80b JC |
988 | } |
989 | ||
17d82b47 | 990 | struct device_type iio_device_type = { |
847ec80b JC |
991 | .name = "iio_device", |
992 | .release = iio_dev_release, | |
993 | }; | |
994 | ||
a7e57dce SK |
995 | /** |
996 | * iio_device_alloc() - allocate an iio_dev from a driver | |
997 | * @sizeof_priv: Space to allocate for private structure. | |
998 | **/ | |
7cbb7537 | 999 | struct iio_dev *iio_device_alloc(int sizeof_priv) |
847ec80b | 1000 | { |
6f7c8ee5 JC |
1001 | struct iio_dev *dev; |
1002 | size_t alloc_size; | |
1003 | ||
1004 | alloc_size = sizeof(struct iio_dev); | |
1005 | if (sizeof_priv) { | |
1006 | alloc_size = ALIGN(alloc_size, IIO_ALIGN); | |
1007 | alloc_size += sizeof_priv; | |
1008 | } | |
1009 | /* ensure 32-byte alignment of whole construct ? */ | |
1010 | alloc_size += IIO_ALIGN - 1; | |
1011 | ||
1012 | dev = kzalloc(alloc_size, GFP_KERNEL); | |
847ec80b JC |
1013 | |
1014 | if (dev) { | |
26d25ae3 | 1015 | dev->dev.groups = dev->groups; |
17d82b47 | 1016 | dev->dev.type = &iio_device_type; |
5aaaeba8 | 1017 | dev->dev.bus = &iio_bus_type; |
847ec80b JC |
1018 | device_initialize(&dev->dev); |
1019 | dev_set_drvdata(&dev->dev, (void *)dev); | |
1020 | mutex_init(&dev->mlock); | |
ac917a81 | 1021 | mutex_init(&dev->info_exist_lock); |
e407fd65 | 1022 | INIT_LIST_HEAD(&dev->channel_attr_list); |
a9e39f9e JC |
1023 | |
1024 | dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); | |
1025 | if (dev->id < 0) { | |
1026 | /* cannot use a dev_err as the name isn't available */ | |
3176dd5d | 1027 | pr_err("failed to get device id\n"); |
a9e39f9e JC |
1028 | kfree(dev); |
1029 | return NULL; | |
1030 | } | |
1031 | dev_set_name(&dev->dev, "iio:device%d", dev->id); | |
84b36ce5 | 1032 | INIT_LIST_HEAD(&dev->buffer_list); |
847ec80b JC |
1033 | } |
1034 | ||
1035 | return dev; | |
1036 | } | |
7cbb7537 | 1037 | EXPORT_SYMBOL(iio_device_alloc); |
847ec80b | 1038 | |
a7e57dce SK |
1039 | /** |
1040 | * iio_device_free() - free an iio_dev from a driver | |
1041 | * @dev: the iio_dev associated with the device | |
1042 | **/ | |
7cbb7537 | 1043 | void iio_device_free(struct iio_dev *dev) |
847ec80b | 1044 | { |
e407fd65 LPC |
1045 | if (dev) |
1046 | put_device(&dev->dev); | |
847ec80b | 1047 | } |
7cbb7537 | 1048 | EXPORT_SYMBOL(iio_device_free); |
847ec80b | 1049 | |
9dabaf5e GS |
1050 | static void devm_iio_device_release(struct device *dev, void *res) |
1051 | { | |
1052 | iio_device_free(*(struct iio_dev **)res); | |
1053 | } | |
1054 | ||
1055 | static int devm_iio_device_match(struct device *dev, void *res, void *data) | |
1056 | { | |
1057 | struct iio_dev **r = res; | |
1058 | if (!r || !*r) { | |
1059 | WARN_ON(!r || !*r); | |
1060 | return 0; | |
1061 | } | |
1062 | return *r == data; | |
1063 | } | |
1064 | ||
a7e57dce SK |
1065 | /** |
1066 | * devm_iio_device_alloc - Resource-managed iio_device_alloc() | |
1067 | * @dev: Device to allocate iio_dev for | |
1068 | * @sizeof_priv: Space to allocate for private structure. | |
1069 | * | |
1070 | * Managed iio_device_alloc. iio_dev allocated with this function is | |
1071 | * automatically freed on driver detach. | |
1072 | * | |
1073 | * If an iio_dev allocated with this function needs to be freed separately, | |
1074 | * devm_iio_device_free() must be used. | |
1075 | * | |
1076 | * RETURNS: | |
1077 | * Pointer to allocated iio_dev on success, NULL on failure. | |
1078 | */ | |
9dabaf5e GS |
1079 | struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv) |
1080 | { | |
1081 | struct iio_dev **ptr, *iio_dev; | |
1082 | ||
1083 | ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr), | |
1084 | GFP_KERNEL); | |
1085 | if (!ptr) | |
1086 | return NULL; | |
1087 | ||
9dabaf5e GS |
1088 | iio_dev = iio_device_alloc(sizeof_priv); |
1089 | if (iio_dev) { | |
1090 | *ptr = iio_dev; | |
1091 | devres_add(dev, ptr); | |
1092 | } else { | |
1093 | devres_free(ptr); | |
1094 | } | |
1095 | ||
1096 | return iio_dev; | |
1097 | } | |
1098 | EXPORT_SYMBOL_GPL(devm_iio_device_alloc); | |
1099 | ||
a7e57dce SK |
1100 | /** |
1101 | * devm_iio_device_free - Resource-managed iio_device_free() | |
1102 | * @dev: Device this iio_dev belongs to | |
1103 | * @iio_dev: the iio_dev associated with the device | |
1104 | * | |
1105 | * Free iio_dev allocated with devm_iio_device_alloc(). | |
1106 | */ | |
9dabaf5e GS |
1107 | void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev) |
1108 | { | |
1109 | int rc; | |
1110 | ||
1111 | rc = devres_release(dev, devm_iio_device_release, | |
1112 | devm_iio_device_match, iio_dev); | |
1113 | WARN_ON(rc); | |
1114 | } | |
1115 | EXPORT_SYMBOL_GPL(devm_iio_device_free); | |
1116 | ||
1aa04278 | 1117 | /** |
14555b14 | 1118 | * iio_chrdev_open() - chrdev file open for buffer access and ioctls |
2498dcf6 CO |
1119 | * @inode: Inode structure for identifying the device in the file system |
1120 | * @filp: File structure for iio device used to keep and later access | |
1121 | * private data | |
1122 | * | |
1123 | * Return: 0 on success or -EBUSY if the device is already opened | |
1aa04278 JC |
1124 | **/ |
1125 | static int iio_chrdev_open(struct inode *inode, struct file *filp) | |
1126 | { | |
f8c6f4e9 | 1127 | struct iio_dev *indio_dev = container_of(inode->i_cdev, |
1aa04278 | 1128 | struct iio_dev, chrdev); |
bb01443e LPC |
1129 | |
1130 | if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags)) | |
1131 | return -EBUSY; | |
1132 | ||
cadc2125 LPC |
1133 | iio_device_get(indio_dev); |
1134 | ||
f8c6f4e9 | 1135 | filp->private_data = indio_dev; |
30eb82f0 | 1136 | |
79335140 | 1137 | return 0; |
1aa04278 JC |
1138 | } |
1139 | ||
1140 | /** | |
14555b14 | 1141 | * iio_chrdev_release() - chrdev file close buffer access and ioctls |
2498dcf6 CO |
1142 | * @inode: Inode structure pointer for the char device |
1143 | * @filp: File structure pointer for the char device | |
1144 | * | |
1145 | * Return: 0 for successful release | |
1146 | */ | |
1aa04278 JC |
1147 | static int iio_chrdev_release(struct inode *inode, struct file *filp) |
1148 | { | |
bb01443e LPC |
1149 | struct iio_dev *indio_dev = container_of(inode->i_cdev, |
1150 | struct iio_dev, chrdev); | |
bb01443e | 1151 | clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags); |
cadc2125 LPC |
1152 | iio_device_put(indio_dev); |
1153 | ||
1aa04278 JC |
1154 | return 0; |
1155 | } | |
1156 | ||
1157 | /* Somewhat of a cross file organization violation - ioctls here are actually | |
1158 | * event related */ | |
1159 | static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) | |
1160 | { | |
1161 | struct iio_dev *indio_dev = filp->private_data; | |
1162 | int __user *ip = (int __user *)arg; | |
1163 | int fd; | |
1164 | ||
f18e7a06 LPC |
1165 | if (!indio_dev->info) |
1166 | return -ENODEV; | |
1167 | ||
1aa04278 JC |
1168 | if (cmd == IIO_GET_EVENT_FD_IOCTL) { |
1169 | fd = iio_event_getfd(indio_dev); | |
3f9059b7 LW |
1170 | if (fd < 0) |
1171 | return fd; | |
1aa04278 JC |
1172 | if (copy_to_user(ip, &fd, sizeof(fd))) |
1173 | return -EFAULT; | |
1174 | return 0; | |
1175 | } | |
1176 | return -EINVAL; | |
1177 | } | |
1178 | ||
14555b14 JC |
1179 | static const struct file_operations iio_buffer_fileops = { |
1180 | .read = iio_buffer_read_first_n_outer_addr, | |
1aa04278 JC |
1181 | .release = iio_chrdev_release, |
1182 | .open = iio_chrdev_open, | |
14555b14 | 1183 | .poll = iio_buffer_poll_addr, |
1aa04278 JC |
1184 | .owner = THIS_MODULE, |
1185 | .llseek = noop_llseek, | |
1186 | .unlocked_ioctl = iio_ioctl, | |
1187 | .compat_ioctl = iio_ioctl, | |
1188 | }; | |
1189 | ||
8f5d8727 VD |
1190 | static int iio_check_unique_scan_index(struct iio_dev *indio_dev) |
1191 | { | |
1192 | int i, j; | |
1193 | const struct iio_chan_spec *channels = indio_dev->channels; | |
1194 | ||
1195 | if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES)) | |
1196 | return 0; | |
1197 | ||
1198 | for (i = 0; i < indio_dev->num_channels - 1; i++) { | |
1199 | if (channels[i].scan_index < 0) | |
1200 | continue; | |
1201 | for (j = i + 1; j < indio_dev->num_channels; j++) | |
1202 | if (channels[i].scan_index == channels[j].scan_index) { | |
1203 | dev_err(&indio_dev->dev, | |
1204 | "Duplicate scan index %d\n", | |
1205 | channels[i].scan_index); | |
1206 | return -EINVAL; | |
1207 | } | |
1208 | } | |
1209 | ||
1210 | return 0; | |
1211 | } | |
1212 | ||
0f1acee5 MH |
1213 | static const struct iio_buffer_setup_ops noop_ring_setup_ops; |
1214 | ||
a7e57dce SK |
1215 | /** |
1216 | * iio_device_register() - register a device with the IIO subsystem | |
1217 | * @indio_dev: Device structure filled by the device driver | |
1218 | **/ | |
f8c6f4e9 | 1219 | int iio_device_register(struct iio_dev *indio_dev) |
847ec80b JC |
1220 | { |
1221 | int ret; | |
1222 | ||
17d82b47 GR |
1223 | /* If the calling driver did not initialize of_node, do it here */ |
1224 | if (!indio_dev->dev.of_node && indio_dev->dev.parent) | |
1225 | indio_dev->dev.of_node = indio_dev->dev.parent->of_node; | |
1226 | ||
8f5d8727 VD |
1227 | ret = iio_check_unique_scan_index(indio_dev); |
1228 | if (ret < 0) | |
1229 | return ret; | |
1230 | ||
1aa04278 | 1231 | /* configure elements for the chrdev */ |
f8c6f4e9 | 1232 | indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id); |
847ec80b | 1233 | |
e553f182 MH |
1234 | ret = iio_device_register_debugfs(indio_dev); |
1235 | if (ret) { | |
1236 | dev_err(indio_dev->dev.parent, | |
1237 | "Failed to register debugfs interfaces\n"); | |
92825ff9 | 1238 | return ret; |
e553f182 | 1239 | } |
3e1b6c95 LPC |
1240 | |
1241 | ret = iio_buffer_alloc_sysfs_and_mask(indio_dev); | |
1242 | if (ret) { | |
1243 | dev_err(indio_dev->dev.parent, | |
1244 | "Failed to create buffer sysfs interfaces\n"); | |
1245 | goto error_unreg_debugfs; | |
1246 | } | |
1247 | ||
f8c6f4e9 | 1248 | ret = iio_device_register_sysfs(indio_dev); |
847ec80b | 1249 | if (ret) { |
f8c6f4e9 | 1250 | dev_err(indio_dev->dev.parent, |
847ec80b | 1251 | "Failed to register sysfs interfaces\n"); |
3e1b6c95 | 1252 | goto error_buffer_free_sysfs; |
847ec80b | 1253 | } |
f8c6f4e9 | 1254 | ret = iio_device_register_eventset(indio_dev); |
847ec80b | 1255 | if (ret) { |
f8c6f4e9 | 1256 | dev_err(indio_dev->dev.parent, |
c849d253 | 1257 | "Failed to register event set\n"); |
847ec80b JC |
1258 | goto error_free_sysfs; |
1259 | } | |
735ad074 | 1260 | if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED)) |
f8c6f4e9 | 1261 | iio_device_register_trigger_consumer(indio_dev); |
847ec80b | 1262 | |
0f1acee5 MH |
1263 | if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && |
1264 | indio_dev->setup_ops == NULL) | |
1265 | indio_dev->setup_ops = &noop_ring_setup_ops; | |
1266 | ||
f8c6f4e9 JC |
1267 | cdev_init(&indio_dev->chrdev, &iio_buffer_fileops); |
1268 | indio_dev->chrdev.owner = indio_dev->info->driver_module; | |
0d5b7dae | 1269 | indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj; |
f8c6f4e9 | 1270 | ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1); |
26d25ae3 | 1271 | if (ret < 0) |
0d5b7dae | 1272 | goto error_unreg_eventset; |
847ec80b | 1273 | |
0d5b7dae LPC |
1274 | ret = device_add(&indio_dev->dev); |
1275 | if (ret < 0) | |
1276 | goto error_cdev_del; | |
1277 | ||
1278 | return 0; | |
1279 | error_cdev_del: | |
1280 | cdev_del(&indio_dev->chrdev); | |
26d25ae3 | 1281 | error_unreg_eventset: |
f8c6f4e9 | 1282 | iio_device_unregister_eventset(indio_dev); |
26d25ae3 | 1283 | error_free_sysfs: |
f8c6f4e9 | 1284 | iio_device_unregister_sysfs(indio_dev); |
3e1b6c95 LPC |
1285 | error_buffer_free_sysfs: |
1286 | iio_buffer_free_sysfs_and_mask(indio_dev); | |
e553f182 MH |
1287 | error_unreg_debugfs: |
1288 | iio_device_unregister_debugfs(indio_dev); | |
847ec80b JC |
1289 | return ret; |
1290 | } | |
1291 | EXPORT_SYMBOL(iio_device_register); | |
1292 | ||
a7e57dce SK |
1293 | /** |
1294 | * iio_device_unregister() - unregister a device from the IIO subsystem | |
1295 | * @indio_dev: Device structure representing the device. | |
1296 | **/ | |
f8c6f4e9 | 1297 | void iio_device_unregister(struct iio_dev *indio_dev) |
847ec80b | 1298 | { |
ac917a81 | 1299 | mutex_lock(&indio_dev->info_exist_lock); |
a87c82e4 LPC |
1300 | |
1301 | device_del(&indio_dev->dev); | |
1302 | ||
0d5b7dae LPC |
1303 | if (indio_dev->chrdev.dev) |
1304 | cdev_del(&indio_dev->chrdev); | |
bc4c9612 | 1305 | iio_device_unregister_debugfs(indio_dev); |
0d5b7dae | 1306 | |
a87c82e4 LPC |
1307 | iio_disable_all_buffers(indio_dev); |
1308 | ||
ac917a81 | 1309 | indio_dev->info = NULL; |
d2f0a48f LPC |
1310 | |
1311 | iio_device_wakeup_eventset(indio_dev); | |
1312 | iio_buffer_wakeup_poll(indio_dev); | |
1313 | ||
ac917a81 | 1314 | mutex_unlock(&indio_dev->info_exist_lock); |
3e1b6c95 LPC |
1315 | |
1316 | iio_buffer_free_sysfs_and_mask(indio_dev); | |
847ec80b JC |
1317 | } |
1318 | EXPORT_SYMBOL(iio_device_unregister); | |
8caa07c0 SK |
1319 | |
1320 | static void devm_iio_device_unreg(struct device *dev, void *res) | |
1321 | { | |
1322 | iio_device_unregister(*(struct iio_dev **)res); | |
1323 | } | |
1324 | ||
1325 | /** | |
1326 | * devm_iio_device_register - Resource-managed iio_device_register() | |
1327 | * @dev: Device to allocate iio_dev for | |
1328 | * @indio_dev: Device structure filled by the device driver | |
1329 | * | |
1330 | * Managed iio_device_register. The IIO device registered with this | |
1331 | * function is automatically unregistered on driver detach. This function | |
1332 | * calls iio_device_register() internally. Refer to that function for more | |
1333 | * information. | |
1334 | * | |
1335 | * If an iio_dev registered with this function needs to be unregistered | |
1336 | * separately, devm_iio_device_unregister() must be used. | |
1337 | * | |
1338 | * RETURNS: | |
1339 | * 0 on success, negative error number on failure. | |
1340 | */ | |
1341 | int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev) | |
1342 | { | |
1343 | struct iio_dev **ptr; | |
1344 | int ret; | |
1345 | ||
1346 | ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL); | |
1347 | if (!ptr) | |
1348 | return -ENOMEM; | |
1349 | ||
1350 | *ptr = indio_dev; | |
1351 | ret = iio_device_register(indio_dev); | |
1352 | if (!ret) | |
1353 | devres_add(dev, ptr); | |
1354 | else | |
1355 | devres_free(ptr); | |
1356 | ||
1357 | return ret; | |
1358 | } | |
1359 | EXPORT_SYMBOL_GPL(devm_iio_device_register); | |
1360 | ||
1361 | /** | |
1362 | * devm_iio_device_unregister - Resource-managed iio_device_unregister() | |
1363 | * @dev: Device this iio_dev belongs to | |
1364 | * @indio_dev: the iio_dev associated with the device | |
1365 | * | |
1366 | * Unregister iio_dev registered with devm_iio_device_register(). | |
1367 | */ | |
1368 | void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev) | |
1369 | { | |
1370 | int rc; | |
1371 | ||
1372 | rc = devres_release(dev, devm_iio_device_unreg, | |
1373 | devm_iio_device_match, indio_dev); | |
1374 | WARN_ON(rc); | |
1375 | } | |
1376 | EXPORT_SYMBOL_GPL(devm_iio_device_unregister); | |
1377 | ||
847ec80b JC |
1378 | subsys_initcall(iio_init); |
1379 | module_exit(iio_exit); | |
1380 | ||
c8b95952 | 1381 | MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); |
847ec80b JC |
1382 | MODULE_DESCRIPTION("Industrial I/O core"); |
1383 | MODULE_LICENSE("GPL"); |