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