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