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