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