]>
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 | ||
3661f3f5 LPC |
366 | /** |
367 | * iio_format_value() - Formats a IIO value into its string representation | |
368 | * @buf: The buffer to which the formated value gets written | |
369 | * @type: One of the IIO_VAL_... constants. This decides how the val and val2 | |
370 | * parameters are formatted. | |
371 | * @val: First part of the value, exact meaning depends on the type parameter. | |
372 | * @val2: Second part of the value, exact meaning depends on the type parameter. | |
373 | */ | |
374 | ssize_t iio_format_value(char *buf, unsigned int type, int val, int val2) | |
847ec80b | 375 | { |
7985e7c1 | 376 | unsigned long long tmp; |
67eedba3 | 377 | bool scale_db = false; |
1d892719 | 378 | |
3661f3f5 | 379 | switch (type) { |
67eedba3 | 380 | case IIO_VAL_INT: |
1d892719 | 381 | return sprintf(buf, "%d\n", val); |
67eedba3 MH |
382 | case IIO_VAL_INT_PLUS_MICRO_DB: |
383 | scale_db = true; | |
384 | case IIO_VAL_INT_PLUS_MICRO: | |
1d892719 | 385 | if (val2 < 0) |
d9a0134e | 386 | return sprintf(buf, "-%ld.%06u%s\n", abs(val), -val2, |
67eedba3 | 387 | scale_db ? " dB" : ""); |
1d892719 | 388 | else |
67eedba3 MH |
389 | return sprintf(buf, "%d.%06u%s\n", val, val2, |
390 | scale_db ? " dB" : ""); | |
391 | case IIO_VAL_INT_PLUS_NANO: | |
71646e2c | 392 | if (val2 < 0) |
d9a0134e | 393 | return sprintf(buf, "-%ld.%09u\n", abs(val), -val2); |
71646e2c MH |
394 | else |
395 | return sprintf(buf, "%d.%09u\n", val, val2); | |
7985e7c1 LPC |
396 | case IIO_VAL_FRACTIONAL: |
397 | tmp = div_s64((s64)val * 1000000000LL, val2); | |
398 | val2 = do_div(tmp, 1000000000LL); | |
399 | val = tmp; | |
400 | return sprintf(buf, "%d.%09u\n", val, val2); | |
103d9fb9 LPC |
401 | case IIO_VAL_FRACTIONAL_LOG2: |
402 | tmp = (s64)val * 1000000000LL >> val2; | |
403 | val2 = do_div(tmp, 1000000000LL); | |
404 | val = tmp; | |
405 | return sprintf(buf, "%d.%09u\n", val, val2); | |
67eedba3 | 406 | default: |
1d892719 | 407 | return 0; |
67eedba3 | 408 | } |
1d892719 JC |
409 | } |
410 | ||
3661f3f5 LPC |
411 | static ssize_t iio_read_channel_info(struct device *dev, |
412 | struct device_attribute *attr, | |
413 | char *buf) | |
414 | { | |
415 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); | |
416 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
417 | int val, val2; | |
418 | int ret = indio_dev->info->read_raw(indio_dev, this_attr->c, | |
419 | &val, &val2, this_attr->address); | |
420 | ||
421 | if (ret < 0) | |
422 | return ret; | |
423 | ||
424 | return iio_format_value(buf, ret, val, val2); | |
425 | } | |
426 | ||
6807d721 LPC |
427 | /** |
428 | * iio_str_to_fixpoint() - Parse a fixed-point number from a string | |
429 | * @str: The string to parse | |
430 | * @fract_mult: Multiplier for the first decimal place, should be a power of 10 | |
431 | * @integer: The integer part of the number | |
432 | * @fract: The fractional part of the number | |
433 | * | |
434 | * Returns 0 on success, or a negative error code if the string could not be | |
435 | * parsed. | |
436 | */ | |
437 | int iio_str_to_fixpoint(const char *str, int fract_mult, | |
438 | int *integer, int *fract) | |
439 | { | |
440 | int i = 0, f = 0; | |
441 | bool integer_part = true, negative = false; | |
442 | ||
443 | if (str[0] == '-') { | |
444 | negative = true; | |
445 | str++; | |
446 | } else if (str[0] == '+') { | |
447 | str++; | |
448 | } | |
449 | ||
450 | while (*str) { | |
451 | if ('0' <= *str && *str <= '9') { | |
452 | if (integer_part) { | |
453 | i = i * 10 + *str - '0'; | |
454 | } else { | |
455 | f += fract_mult * (*str - '0'); | |
456 | fract_mult /= 10; | |
457 | } | |
458 | } else if (*str == '\n') { | |
459 | if (*(str + 1) == '\0') | |
460 | break; | |
461 | else | |
462 | return -EINVAL; | |
463 | } else if (*str == '.' && integer_part) { | |
464 | integer_part = false; | |
465 | } else { | |
466 | return -EINVAL; | |
467 | } | |
468 | str++; | |
469 | } | |
470 | ||
471 | if (negative) { | |
472 | if (i) | |
473 | i = -i; | |
474 | else | |
475 | f = -f; | |
476 | } | |
477 | ||
478 | *integer = i; | |
479 | *fract = f; | |
480 | ||
481 | return 0; | |
482 | } | |
483 | EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); | |
484 | ||
1d892719 JC |
485 | static ssize_t iio_write_channel_info(struct device *dev, |
486 | struct device_attribute *attr, | |
487 | const char *buf, | |
488 | size_t len) | |
489 | { | |
e53f5ac5 | 490 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
1d892719 | 491 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
6807d721 LPC |
492 | int ret, fract_mult = 100000; |
493 | int integer, fract; | |
1d892719 JC |
494 | |
495 | /* Assumes decimal - precision based on number of digits */ | |
6fe8135f | 496 | if (!indio_dev->info->write_raw) |
1d892719 | 497 | return -EINVAL; |
5c04af04 MH |
498 | |
499 | if (indio_dev->info->write_raw_get_fmt) | |
500 | switch (indio_dev->info->write_raw_get_fmt(indio_dev, | |
501 | this_attr->c, this_attr->address)) { | |
502 | case IIO_VAL_INT_PLUS_MICRO: | |
503 | fract_mult = 100000; | |
504 | break; | |
505 | case IIO_VAL_INT_PLUS_NANO: | |
506 | fract_mult = 100000000; | |
507 | break; | |
508 | default: | |
509 | return -EINVAL; | |
510 | } | |
511 | ||
6807d721 LPC |
512 | ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract); |
513 | if (ret) | |
514 | return ret; | |
1d892719 | 515 | |
6fe8135f | 516 | ret = indio_dev->info->write_raw(indio_dev, this_attr->c, |
5c04af04 | 517 | integer, fract, this_attr->address); |
1d892719 JC |
518 | if (ret) |
519 | return ret; | |
520 | ||
521 | return len; | |
522 | } | |
523 | ||
df9c1c42 | 524 | static |
1d892719 JC |
525 | int __iio_device_attr_init(struct device_attribute *dev_attr, |
526 | const char *postfix, | |
527 | struct iio_chan_spec const *chan, | |
528 | ssize_t (*readfunc)(struct device *dev, | |
529 | struct device_attribute *attr, | |
530 | char *buf), | |
531 | ssize_t (*writefunc)(struct device *dev, | |
532 | struct device_attribute *attr, | |
533 | const char *buf, | |
534 | size_t len), | |
3704432f | 535 | enum iio_shared_by shared_by) |
1d892719 | 536 | { |
3704432f JC |
537 | int ret = 0; |
538 | char *name_format = NULL; | |
539 | char *full_postfix; | |
1d892719 | 540 | sysfs_attr_init(&dev_attr->attr); |
1d892719 | 541 | |
ade7ef7b | 542 | /* Build up postfix of <extend_name>_<modifier>_postfix */ |
3704432f | 543 | if (chan->modified && (shared_by == IIO_SEPARATE)) { |
ade7ef7b JC |
544 | if (chan->extend_name) |
545 | full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", | |
546 | iio_modifier_names[chan | |
547 | ->channel2], | |
548 | chan->extend_name, | |
549 | postfix); | |
550 | else | |
551 | full_postfix = kasprintf(GFP_KERNEL, "%s_%s", | |
552 | iio_modifier_names[chan | |
553 | ->channel2], | |
554 | postfix); | |
555 | } else { | |
556 | if (chan->extend_name == NULL) | |
557 | full_postfix = kstrdup(postfix, GFP_KERNEL); | |
558 | else | |
559 | full_postfix = kasprintf(GFP_KERNEL, | |
560 | "%s_%s", | |
561 | chan->extend_name, | |
562 | postfix); | |
563 | } | |
3704432f JC |
564 | if (full_postfix == NULL) |
565 | return -ENOMEM; | |
1d892719 | 566 | |
4abf6f8b | 567 | if (chan->differential) { /* Differential can not have modifier */ |
3704432f | 568 | switch (shared_by) { |
c006ec83 JC |
569 | case IIO_SHARED_BY_ALL: |
570 | name_format = kasprintf(GFP_KERNEL, "%s", full_postfix); | |
571 | break; | |
572 | case IIO_SHARED_BY_DIR: | |
573 | name_format = kasprintf(GFP_KERNEL, "%s_%s", | |
574 | iio_direction[chan->output], | |
575 | full_postfix); | |
576 | break; | |
3704432f | 577 | case IIO_SHARED_BY_TYPE: |
ade7ef7b JC |
578 | name_format |
579 | = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", | |
580 | iio_direction[chan->output], | |
581 | iio_chan_type_name_spec[chan->type], | |
582 | iio_chan_type_name_spec[chan->type], | |
583 | full_postfix); | |
3704432f JC |
584 | break; |
585 | case IIO_SEPARATE: | |
586 | if (!chan->indexed) { | |
587 | WARN_ON("Differential channels must be indexed\n"); | |
588 | ret = -EINVAL; | |
589 | goto error_free_full_postfix; | |
590 | } | |
ade7ef7b | 591 | name_format |
3704432f JC |
592 | = kasprintf(GFP_KERNEL, |
593 | "%s_%s%d-%s%d_%s", | |
ade7ef7b JC |
594 | iio_direction[chan->output], |
595 | iio_chan_type_name_spec[chan->type], | |
596 | chan->channel, | |
597 | iio_chan_type_name_spec[chan->type], | |
598 | chan->channel2, | |
599 | full_postfix); | |
3704432f | 600 | break; |
ade7ef7b JC |
601 | } |
602 | } else { /* Single ended */ | |
3704432f | 603 | switch (shared_by) { |
c006ec83 JC |
604 | case IIO_SHARED_BY_ALL: |
605 | name_format = kasprintf(GFP_KERNEL, "%s", full_postfix); | |
606 | break; | |
607 | case IIO_SHARED_BY_DIR: | |
608 | name_format = kasprintf(GFP_KERNEL, "%s_%s", | |
609 | iio_direction[chan->output], | |
610 | full_postfix); | |
611 | break; | |
3704432f | 612 | case IIO_SHARED_BY_TYPE: |
ade7ef7b JC |
613 | name_format |
614 | = kasprintf(GFP_KERNEL, "%s_%s_%s", | |
615 | iio_direction[chan->output], | |
616 | iio_chan_type_name_spec[chan->type], | |
617 | full_postfix); | |
3704432f JC |
618 | break; |
619 | ||
620 | case IIO_SEPARATE: | |
621 | if (chan->indexed) | |
622 | name_format | |
623 | = kasprintf(GFP_KERNEL, "%s_%s%d_%s", | |
624 | iio_direction[chan->output], | |
625 | iio_chan_type_name_spec[chan->type], | |
626 | chan->channel, | |
627 | full_postfix); | |
628 | else | |
629 | name_format | |
630 | = kasprintf(GFP_KERNEL, "%s_%s_%s", | |
631 | iio_direction[chan->output], | |
632 | iio_chan_type_name_spec[chan->type], | |
633 | full_postfix); | |
634 | break; | |
635 | } | |
ade7ef7b | 636 | } |
1d892719 JC |
637 | if (name_format == NULL) { |
638 | ret = -ENOMEM; | |
639 | goto error_free_full_postfix; | |
640 | } | |
641 | dev_attr->attr.name = kasprintf(GFP_KERNEL, | |
642 | name_format, | |
643 | chan->channel, | |
644 | chan->channel2); | |
645 | if (dev_attr->attr.name == NULL) { | |
646 | ret = -ENOMEM; | |
647 | goto error_free_name_format; | |
648 | } | |
649 | ||
650 | if (readfunc) { | |
651 | dev_attr->attr.mode |= S_IRUGO; | |
652 | dev_attr->show = readfunc; | |
653 | } | |
654 | ||
655 | if (writefunc) { | |
656 | dev_attr->attr.mode |= S_IWUSR; | |
657 | dev_attr->store = writefunc; | |
658 | } | |
1d892719 JC |
659 | error_free_name_format: |
660 | kfree(name_format); | |
661 | error_free_full_postfix: | |
662 | kfree(full_postfix); | |
3704432f | 663 | |
1d892719 JC |
664 | return ret; |
665 | } | |
666 | ||
df9c1c42 | 667 | static void __iio_device_attr_deinit(struct device_attribute *dev_attr) |
1d892719 JC |
668 | { |
669 | kfree(dev_attr->attr.name); | |
670 | } | |
671 | ||
672 | int __iio_add_chan_devattr(const char *postfix, | |
1d892719 JC |
673 | struct iio_chan_spec const *chan, |
674 | ssize_t (*readfunc)(struct device *dev, | |
675 | struct device_attribute *attr, | |
676 | char *buf), | |
677 | ssize_t (*writefunc)(struct device *dev, | |
678 | struct device_attribute *attr, | |
679 | const char *buf, | |
680 | size_t len), | |
e614a54b | 681 | u64 mask, |
3704432f | 682 | enum iio_shared_by shared_by, |
1d892719 JC |
683 | struct device *dev, |
684 | struct list_head *attr_list) | |
685 | { | |
686 | int ret; | |
687 | struct iio_dev_attr *iio_attr, *t; | |
688 | ||
689 | iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL); | |
690 | if (iio_attr == NULL) { | |
691 | ret = -ENOMEM; | |
692 | goto error_ret; | |
693 | } | |
694 | ret = __iio_device_attr_init(&iio_attr->dev_attr, | |
695 | postfix, chan, | |
3704432f | 696 | readfunc, writefunc, shared_by); |
1d892719 JC |
697 | if (ret) |
698 | goto error_iio_dev_attr_free; | |
699 | iio_attr->c = chan; | |
700 | iio_attr->address = mask; | |
701 | list_for_each_entry(t, attr_list, l) | |
702 | if (strcmp(t->dev_attr.attr.name, | |
703 | iio_attr->dev_attr.attr.name) == 0) { | |
3704432f | 704 | if (shared_by == IIO_SEPARATE) |
1d892719 JC |
705 | dev_err(dev, "tried to double register : %s\n", |
706 | t->dev_attr.attr.name); | |
707 | ret = -EBUSY; | |
708 | goto error_device_attr_deinit; | |
709 | } | |
1d892719 JC |
710 | list_add(&iio_attr->l, attr_list); |
711 | ||
712 | return 0; | |
713 | ||
714 | error_device_attr_deinit: | |
715 | __iio_device_attr_deinit(&iio_attr->dev_attr); | |
716 | error_iio_dev_attr_free: | |
717 | kfree(iio_attr); | |
718 | error_ret: | |
719 | return ret; | |
720 | } | |
721 | ||
3704432f JC |
722 | static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, |
723 | struct iio_chan_spec const *chan, | |
724 | enum iio_shared_by shared_by, | |
725 | const long *infomask) | |
1d892719 | 726 | { |
3704432f | 727 | int i, ret, attrcount = 0; |
1d892719 | 728 | |
3704432f | 729 | for_each_set_bit(i, infomask, sizeof(infomask)*8) { |
8655cc49 JC |
730 | ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], |
731 | chan, | |
732 | &iio_read_channel_info, | |
733 | &iio_write_channel_info, | |
734 | i, | |
3704432f | 735 | shared_by, |
8655cc49 JC |
736 | &indio_dev->dev, |
737 | &indio_dev->channel_attr_list); | |
3704432f | 738 | if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) |
8655cc49 | 739 | continue; |
3704432f JC |
740 | else if (ret < 0) |
741 | return ret; | |
8655cc49 JC |
742 | attrcount++; |
743 | } | |
5f420b42 | 744 | |
3704432f JC |
745 | return attrcount; |
746 | } | |
747 | ||
748 | static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, | |
749 | struct iio_chan_spec const *chan) | |
750 | { | |
751 | int ret, attrcount = 0; | |
752 | const struct iio_chan_spec_ext_info *ext_info; | |
753 | ||
754 | if (chan->channel < 0) | |
755 | return 0; | |
756 | ret = iio_device_add_info_mask_type(indio_dev, chan, | |
757 | IIO_SEPARATE, | |
758 | &chan->info_mask_separate); | |
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_TYPE, | |
765 | &chan->info_mask_shared_by_type); | |
766 | if (ret < 0) | |
767 | return ret; | |
768 | attrcount += ret; | |
769 | ||
c006ec83 JC |
770 | ret = iio_device_add_info_mask_type(indio_dev, chan, |
771 | IIO_SHARED_BY_DIR, | |
772 | &chan->info_mask_shared_by_dir); | |
773 | if (ret < 0) | |
774 | return ret; | |
775 | attrcount += ret; | |
776 | ||
777 | ret = iio_device_add_info_mask_type(indio_dev, chan, | |
778 | IIO_SHARED_BY_ALL, | |
779 | &chan->info_mask_shared_by_all); | |
780 | if (ret < 0) | |
781 | return ret; | |
782 | attrcount += ret; | |
783 | ||
5f420b42 LPC |
784 | if (chan->ext_info) { |
785 | unsigned int i = 0; | |
786 | for (ext_info = chan->ext_info; ext_info->name; ext_info++) { | |
787 | ret = __iio_add_chan_devattr(ext_info->name, | |
788 | chan, | |
789 | ext_info->read ? | |
790 | &iio_read_channel_ext_info : NULL, | |
791 | ext_info->write ? | |
792 | &iio_write_channel_ext_info : NULL, | |
793 | i, | |
794 | ext_info->shared, | |
795 | &indio_dev->dev, | |
796 | &indio_dev->channel_attr_list); | |
797 | i++; | |
798 | if (ret == -EBUSY && ext_info->shared) | |
799 | continue; | |
800 | ||
801 | if (ret) | |
3704432f | 802 | return ret; |
5f420b42 LPC |
803 | |
804 | attrcount++; | |
805 | } | |
806 | } | |
807 | ||
3704432f | 808 | return attrcount; |
1d892719 JC |
809 | } |
810 | ||
84088ebd LPC |
811 | /** |
812 | * iio_free_chan_devattr_list() - Free a list of IIO device attributes | |
813 | * @attr_list: List of IIO device attributes | |
814 | * | |
815 | * This function frees the memory allocated for each of the IIO device | |
816 | * attributes in the list. Note: if you want to reuse the list after calling | |
817 | * this function you have to reinitialize it using INIT_LIST_HEAD(). | |
818 | */ | |
819 | void iio_free_chan_devattr_list(struct list_head *attr_list) | |
1d892719 | 820 | { |
84088ebd LPC |
821 | struct iio_dev_attr *p, *n; |
822 | ||
823 | list_for_each_entry_safe(p, n, attr_list, l) { | |
824 | kfree(p->dev_attr.attr.name); | |
825 | kfree(p); | |
826 | } | |
1d892719 JC |
827 | } |
828 | ||
1b732888 JC |
829 | static ssize_t iio_show_dev_name(struct device *dev, |
830 | struct device_attribute *attr, | |
831 | char *buf) | |
832 | { | |
e53f5ac5 | 833 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
1b732888 JC |
834 | return sprintf(buf, "%s\n", indio_dev->name); |
835 | } | |
836 | ||
837 | static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); | |
838 | ||
f8c6f4e9 | 839 | static int iio_device_register_sysfs(struct iio_dev *indio_dev) |
1d892719 | 840 | { |
26d25ae3 | 841 | int i, ret = 0, attrcount, attrn, attrcount_orig = 0; |
84088ebd | 842 | struct iio_dev_attr *p; |
26d25ae3 | 843 | struct attribute **attr; |
1d892719 | 844 | |
26d25ae3 | 845 | /* First count elements in any existing group */ |
f8c6f4e9 JC |
846 | if (indio_dev->info->attrs) { |
847 | attr = indio_dev->info->attrs->attrs; | |
26d25ae3 JC |
848 | while (*attr++ != NULL) |
849 | attrcount_orig++; | |
847ec80b | 850 | } |
26d25ae3 | 851 | attrcount = attrcount_orig; |
1d892719 JC |
852 | /* |
853 | * New channel registration method - relies on the fact a group does | |
d25b3808 | 854 | * not need to be initialized if its name is NULL. |
1d892719 | 855 | */ |
f8c6f4e9 JC |
856 | if (indio_dev->channels) |
857 | for (i = 0; i < indio_dev->num_channels; i++) { | |
858 | ret = iio_device_add_channel_sysfs(indio_dev, | |
859 | &indio_dev | |
1d892719 JC |
860 | ->channels[i]); |
861 | if (ret < 0) | |
862 | goto error_clear_attrs; | |
26d25ae3 | 863 | attrcount += ret; |
1d892719 | 864 | } |
26d25ae3 | 865 | |
f8c6f4e9 | 866 | if (indio_dev->name) |
26d25ae3 JC |
867 | attrcount++; |
868 | ||
d83fb184 TM |
869 | indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1, |
870 | sizeof(indio_dev->chan_attr_group.attrs[0]), | |
871 | GFP_KERNEL); | |
f8c6f4e9 | 872 | if (indio_dev->chan_attr_group.attrs == NULL) { |
26d25ae3 JC |
873 | ret = -ENOMEM; |
874 | goto error_clear_attrs; | |
1b732888 | 875 | } |
26d25ae3 | 876 | /* Copy across original attributes */ |
f8c6f4e9 JC |
877 | if (indio_dev->info->attrs) |
878 | memcpy(indio_dev->chan_attr_group.attrs, | |
879 | indio_dev->info->attrs->attrs, | |
880 | sizeof(indio_dev->chan_attr_group.attrs[0]) | |
26d25ae3 JC |
881 | *attrcount_orig); |
882 | attrn = attrcount_orig; | |
883 | /* Add all elements from the list. */ | |
f8c6f4e9 JC |
884 | list_for_each_entry(p, &indio_dev->channel_attr_list, l) |
885 | indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; | |
886 | if (indio_dev->name) | |
887 | indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; | |
26d25ae3 | 888 | |
f8c6f4e9 JC |
889 | indio_dev->groups[indio_dev->groupcounter++] = |
890 | &indio_dev->chan_attr_group; | |
26d25ae3 | 891 | |
1d892719 | 892 | return 0; |
1b732888 | 893 | |
1d892719 | 894 | error_clear_attrs: |
84088ebd | 895 | iio_free_chan_devattr_list(&indio_dev->channel_attr_list); |
1d892719 | 896 | |
26d25ae3 | 897 | return ret; |
847ec80b JC |
898 | } |
899 | ||
f8c6f4e9 | 900 | static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) |
847ec80b | 901 | { |
1d892719 | 902 | |
84088ebd | 903 | iio_free_chan_devattr_list(&indio_dev->channel_attr_list); |
f8c6f4e9 | 904 | kfree(indio_dev->chan_attr_group.attrs); |
847ec80b JC |
905 | } |
906 | ||
847ec80b JC |
907 | static void iio_dev_release(struct device *device) |
908 | { | |
e53f5ac5 | 909 | struct iio_dev *indio_dev = dev_to_iio_dev(device); |
f8c6f4e9 JC |
910 | if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) |
911 | iio_device_unregister_trigger_consumer(indio_dev); | |
912 | iio_device_unregister_eventset(indio_dev); | |
913 | iio_device_unregister_sysfs(indio_dev); | |
e407fd65 | 914 | |
9e69c935 LPC |
915 | iio_buffer_put(indio_dev->buffer); |
916 | ||
e407fd65 LPC |
917 | ida_simple_remove(&iio_ida, indio_dev->id); |
918 | kfree(indio_dev); | |
847ec80b JC |
919 | } |
920 | ||
17d82b47 | 921 | struct device_type iio_device_type = { |
847ec80b JC |
922 | .name = "iio_device", |
923 | .release = iio_dev_release, | |
924 | }; | |
925 | ||
7cbb7537 | 926 | struct iio_dev *iio_device_alloc(int sizeof_priv) |
847ec80b | 927 | { |
6f7c8ee5 JC |
928 | struct iio_dev *dev; |
929 | size_t alloc_size; | |
930 | ||
931 | alloc_size = sizeof(struct iio_dev); | |
932 | if (sizeof_priv) { | |
933 | alloc_size = ALIGN(alloc_size, IIO_ALIGN); | |
934 | alloc_size += sizeof_priv; | |
935 | } | |
936 | /* ensure 32-byte alignment of whole construct ? */ | |
937 | alloc_size += IIO_ALIGN - 1; | |
938 | ||
939 | dev = kzalloc(alloc_size, GFP_KERNEL); | |
847ec80b JC |
940 | |
941 | if (dev) { | |
26d25ae3 | 942 | dev->dev.groups = dev->groups; |
17d82b47 | 943 | dev->dev.type = &iio_device_type; |
5aaaeba8 | 944 | dev->dev.bus = &iio_bus_type; |
847ec80b JC |
945 | device_initialize(&dev->dev); |
946 | dev_set_drvdata(&dev->dev, (void *)dev); | |
947 | mutex_init(&dev->mlock); | |
ac917a81 | 948 | mutex_init(&dev->info_exist_lock); |
e407fd65 | 949 | INIT_LIST_HEAD(&dev->channel_attr_list); |
a9e39f9e JC |
950 | |
951 | dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); | |
952 | if (dev->id < 0) { | |
953 | /* cannot use a dev_err as the name isn't available */ | |
954 | printk(KERN_ERR "Failed to get id\n"); | |
955 | kfree(dev); | |
956 | return NULL; | |
957 | } | |
958 | dev_set_name(&dev->dev, "iio:device%d", dev->id); | |
84b36ce5 | 959 | INIT_LIST_HEAD(&dev->buffer_list); |
847ec80b JC |
960 | } |
961 | ||
962 | return dev; | |
963 | } | |
7cbb7537 | 964 | EXPORT_SYMBOL(iio_device_alloc); |
847ec80b | 965 | |
7cbb7537 | 966 | void iio_device_free(struct iio_dev *dev) |
847ec80b | 967 | { |
e407fd65 LPC |
968 | if (dev) |
969 | put_device(&dev->dev); | |
847ec80b | 970 | } |
7cbb7537 | 971 | EXPORT_SYMBOL(iio_device_free); |
847ec80b | 972 | |
9dabaf5e GS |
973 | static void devm_iio_device_release(struct device *dev, void *res) |
974 | { | |
975 | iio_device_free(*(struct iio_dev **)res); | |
976 | } | |
977 | ||
978 | static int devm_iio_device_match(struct device *dev, void *res, void *data) | |
979 | { | |
980 | struct iio_dev **r = res; | |
981 | if (!r || !*r) { | |
982 | WARN_ON(!r || !*r); | |
983 | return 0; | |
984 | } | |
985 | return *r == data; | |
986 | } | |
987 | ||
988 | struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv) | |
989 | { | |
990 | struct iio_dev **ptr, *iio_dev; | |
991 | ||
992 | ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr), | |
993 | GFP_KERNEL); | |
994 | if (!ptr) | |
995 | return NULL; | |
996 | ||
997 | /* use raw alloc_dr for kmalloc caller tracing */ | |
998 | iio_dev = iio_device_alloc(sizeof_priv); | |
999 | if (iio_dev) { | |
1000 | *ptr = iio_dev; | |
1001 | devres_add(dev, ptr); | |
1002 | } else { | |
1003 | devres_free(ptr); | |
1004 | } | |
1005 | ||
1006 | return iio_dev; | |
1007 | } | |
1008 | EXPORT_SYMBOL_GPL(devm_iio_device_alloc); | |
1009 | ||
1010 | void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev) | |
1011 | { | |
1012 | int rc; | |
1013 | ||
1014 | rc = devres_release(dev, devm_iio_device_release, | |
1015 | devm_iio_device_match, iio_dev); | |
1016 | WARN_ON(rc); | |
1017 | } | |
1018 | EXPORT_SYMBOL_GPL(devm_iio_device_free); | |
1019 | ||
1aa04278 | 1020 | /** |
14555b14 | 1021 | * iio_chrdev_open() - chrdev file open for buffer access and ioctls |
1aa04278 JC |
1022 | **/ |
1023 | static int iio_chrdev_open(struct inode *inode, struct file *filp) | |
1024 | { | |
f8c6f4e9 | 1025 | struct iio_dev *indio_dev = container_of(inode->i_cdev, |
1aa04278 | 1026 | struct iio_dev, chrdev); |
bb01443e LPC |
1027 | |
1028 | if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags)) | |
1029 | return -EBUSY; | |
1030 | ||
cadc2125 LPC |
1031 | iio_device_get(indio_dev); |
1032 | ||
f8c6f4e9 | 1033 | filp->private_data = indio_dev; |
30eb82f0 | 1034 | |
79335140 | 1035 | return 0; |
1aa04278 JC |
1036 | } |
1037 | ||
1038 | /** | |
14555b14 | 1039 | * iio_chrdev_release() - chrdev file close buffer access and ioctls |
1aa04278 JC |
1040 | **/ |
1041 | static int iio_chrdev_release(struct inode *inode, struct file *filp) | |
1042 | { | |
bb01443e LPC |
1043 | struct iio_dev *indio_dev = container_of(inode->i_cdev, |
1044 | struct iio_dev, chrdev); | |
bb01443e | 1045 | clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags); |
cadc2125 LPC |
1046 | iio_device_put(indio_dev); |
1047 | ||
1aa04278 JC |
1048 | return 0; |
1049 | } | |
1050 | ||
1051 | /* Somewhat of a cross file organization violation - ioctls here are actually | |
1052 | * event related */ | |
1053 | static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) | |
1054 | { | |
1055 | struct iio_dev *indio_dev = filp->private_data; | |
1056 | int __user *ip = (int __user *)arg; | |
1057 | int fd; | |
1058 | ||
f18e7a06 LPC |
1059 | if (!indio_dev->info) |
1060 | return -ENODEV; | |
1061 | ||
1aa04278 JC |
1062 | if (cmd == IIO_GET_EVENT_FD_IOCTL) { |
1063 | fd = iio_event_getfd(indio_dev); | |
1064 | if (copy_to_user(ip, &fd, sizeof(fd))) | |
1065 | return -EFAULT; | |
1066 | return 0; | |
1067 | } | |
1068 | return -EINVAL; | |
1069 | } | |
1070 | ||
14555b14 JC |
1071 | static const struct file_operations iio_buffer_fileops = { |
1072 | .read = iio_buffer_read_first_n_outer_addr, | |
1aa04278 JC |
1073 | .release = iio_chrdev_release, |
1074 | .open = iio_chrdev_open, | |
14555b14 | 1075 | .poll = iio_buffer_poll_addr, |
1aa04278 JC |
1076 | .owner = THIS_MODULE, |
1077 | .llseek = noop_llseek, | |
1078 | .unlocked_ioctl = iio_ioctl, | |
1079 | .compat_ioctl = iio_ioctl, | |
1080 | }; | |
1081 | ||
0f1acee5 MH |
1082 | static const struct iio_buffer_setup_ops noop_ring_setup_ops; |
1083 | ||
f8c6f4e9 | 1084 | int iio_device_register(struct iio_dev *indio_dev) |
847ec80b JC |
1085 | { |
1086 | int ret; | |
1087 | ||
17d82b47 GR |
1088 | /* If the calling driver did not initialize of_node, do it here */ |
1089 | if (!indio_dev->dev.of_node && indio_dev->dev.parent) | |
1090 | indio_dev->dev.of_node = indio_dev->dev.parent->of_node; | |
1091 | ||
1aa04278 | 1092 | /* configure elements for the chrdev */ |
f8c6f4e9 | 1093 | indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id); |
847ec80b | 1094 | |
e553f182 MH |
1095 | ret = iio_device_register_debugfs(indio_dev); |
1096 | if (ret) { | |
1097 | dev_err(indio_dev->dev.parent, | |
1098 | "Failed to register debugfs interfaces\n"); | |
1099 | goto error_ret; | |
1100 | } | |
f8c6f4e9 | 1101 | ret = iio_device_register_sysfs(indio_dev); |
847ec80b | 1102 | if (ret) { |
f8c6f4e9 | 1103 | dev_err(indio_dev->dev.parent, |
847ec80b | 1104 | "Failed to register sysfs interfaces\n"); |
e553f182 | 1105 | goto error_unreg_debugfs; |
847ec80b | 1106 | } |
f8c6f4e9 | 1107 | ret = iio_device_register_eventset(indio_dev); |
847ec80b | 1108 | if (ret) { |
f8c6f4e9 | 1109 | dev_err(indio_dev->dev.parent, |
c849d253 | 1110 | "Failed to register event set\n"); |
847ec80b JC |
1111 | goto error_free_sysfs; |
1112 | } | |
f8c6f4e9 JC |
1113 | if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) |
1114 | iio_device_register_trigger_consumer(indio_dev); | |
847ec80b | 1115 | |
0f1acee5 MH |
1116 | if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && |
1117 | indio_dev->setup_ops == NULL) | |
1118 | indio_dev->setup_ops = &noop_ring_setup_ops; | |
1119 | ||
f8c6f4e9 JC |
1120 | cdev_init(&indio_dev->chrdev, &iio_buffer_fileops); |
1121 | indio_dev->chrdev.owner = indio_dev->info->driver_module; | |
0d5b7dae | 1122 | indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj; |
f8c6f4e9 | 1123 | ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1); |
26d25ae3 | 1124 | if (ret < 0) |
0d5b7dae | 1125 | goto error_unreg_eventset; |
847ec80b | 1126 | |
0d5b7dae LPC |
1127 | ret = device_add(&indio_dev->dev); |
1128 | if (ret < 0) | |
1129 | goto error_cdev_del; | |
1130 | ||
1131 | return 0; | |
1132 | error_cdev_del: | |
1133 | cdev_del(&indio_dev->chrdev); | |
26d25ae3 | 1134 | error_unreg_eventset: |
f8c6f4e9 | 1135 | iio_device_unregister_eventset(indio_dev); |
26d25ae3 | 1136 | error_free_sysfs: |
f8c6f4e9 | 1137 | iio_device_unregister_sysfs(indio_dev); |
e553f182 MH |
1138 | error_unreg_debugfs: |
1139 | iio_device_unregister_debugfs(indio_dev); | |
847ec80b JC |
1140 | error_ret: |
1141 | return ret; | |
1142 | } | |
1143 | EXPORT_SYMBOL(iio_device_register); | |
1144 | ||
f8c6f4e9 | 1145 | void iio_device_unregister(struct iio_dev *indio_dev) |
847ec80b | 1146 | { |
ac917a81 | 1147 | mutex_lock(&indio_dev->info_exist_lock); |
a87c82e4 LPC |
1148 | |
1149 | device_del(&indio_dev->dev); | |
1150 | ||
0d5b7dae LPC |
1151 | if (indio_dev->chrdev.dev) |
1152 | cdev_del(&indio_dev->chrdev); | |
bc4c9612 | 1153 | iio_device_unregister_debugfs(indio_dev); |
0d5b7dae | 1154 | |
a87c82e4 LPC |
1155 | iio_disable_all_buffers(indio_dev); |
1156 | ||
ac917a81 | 1157 | indio_dev->info = NULL; |
d2f0a48f LPC |
1158 | |
1159 | iio_device_wakeup_eventset(indio_dev); | |
1160 | iio_buffer_wakeup_poll(indio_dev); | |
1161 | ||
ac917a81 | 1162 | mutex_unlock(&indio_dev->info_exist_lock); |
847ec80b JC |
1163 | } |
1164 | EXPORT_SYMBOL(iio_device_unregister); | |
847ec80b JC |
1165 | subsys_initcall(iio_init); |
1166 | module_exit(iio_exit); | |
1167 | ||
c8b95952 | 1168 | MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); |
847ec80b JC |
1169 | MODULE_DESCRIPTION("Industrial I/O core"); |
1170 | MODULE_LICENSE("GPL"); |