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