1 // SPDX-License-Identifier: GPL-2.0-only
2 /* The industrial I/O core
3 *
4 * Copyright (c) 2008 Jonathan Cameron
5 *
6 * Based on elements of hwmon and input subsystems.
7 */
8
9 #define pr_fmt(fmt) "iio-core: " fmt
10
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/idr.h>
14 #include <linux/kdev_t.h>
15 #include <linux/err.h>
16 #include <linux/device.h>
17 #include <linux/fs.h>
18 #include <linux/poll.h>
19 #include <linux/property.h>
20 #include <linux/sched.h>
21 #include <linux/wait.h>
22 #include <linux/cdev.h>
23 #include <linux/slab.h>
24 #include <linux/anon_inodes.h>
25 #include <linux/debugfs.h>
26 #include <linux/mutex.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/iio-opaque.h>
29 #include "iio_core.h"
30 #include "iio_core_trigger.h"
31 #include <linux/iio/sysfs.h>
32 #include <linux/iio/events.h>
33 #include <linux/iio/buffer.h>
34 #include <linux/iio/buffer_impl.h>
35
36 /* IDA to assign each registered device a unique id */
37 static DEFINE_IDA(iio_ida);
38
39 static dev_t iio_devt;
40
41 #define IIO_DEV_MAX 256
42 struct bus_type iio_bus_type = {
43 .name = "iio",
44 };
45 EXPORT_SYMBOL(iio_bus_type);
46
47 static struct dentry *iio_debugfs_dentry;
48
49 static const char * const iio_direction[] = {
50 [0] = "in",
51 [1] = "out",
52 };
53
54 static const char * const iio_chan_type_name_spec[] = {
55 [IIO_VOLTAGE] = "voltage",
56 [IIO_CURRENT] = "current",
57 [IIO_POWER] = "power",
58 [IIO_ACCEL] = "accel",
59 [IIO_ANGL_VEL] = "anglvel",
60 [IIO_MAGN] = "magn",
61 [IIO_LIGHT] = "illuminance",
62 [IIO_INTENSITY] = "intensity",
63 [IIO_PROXIMITY] = "proximity",
64 [IIO_TEMP] = "temp",
65 [IIO_INCLI] = "incli",
66 [IIO_ROT] = "rot",
67 [IIO_ANGL] = "angl",
68 [IIO_TIMESTAMP] = "timestamp",
69 [IIO_CAPACITANCE] = "capacitance",
70 [IIO_ALTVOLTAGE] = "altvoltage",
71 [IIO_CCT] = "cct",
72 [IIO_PRESSURE] = "pressure",
73 [IIO_HUMIDITYRELATIVE] = "humidityrelative",
74 [IIO_ACTIVITY] = "activity",
75 [IIO_STEPS] = "steps",
76 [IIO_ENERGY] = "energy",
77 [IIO_DISTANCE] = "distance",
78 [IIO_VELOCITY] = "velocity",
79 [IIO_CONCENTRATION] = "concentration",
80 [IIO_RESISTANCE] = "resistance",
81 [IIO_PH] = "ph",
82 [IIO_UVINDEX] = "uvindex",
83 [IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity",
84 [IIO_COUNT] = "count",
85 [IIO_INDEX] = "index",
86 [IIO_GRAVITY] = "gravity",
87 [IIO_POSITIONRELATIVE] = "positionrelative",
88 [IIO_PHASE] = "phase",
89 [IIO_MASSCONCENTRATION] = "massconcentration",
90 #ifdef CONFIG_NO_GKI
91 [IIO_SIGN_MOTION] = "signmotion",
92 [IIO_STEP_DETECTOR] = "stepdetector",
93 [IIO_STEP_COUNTER] = "stepcounter",
94 [IIO_TILT] = "tilt",
95 [IIO_TAP] = "tap",
96 [IIO_TAP_TAP] = "taptap",
97 [IIO_WRIST_TILT_GESTURE] = "wristtiltgesture",
98 [IIO_GESTURE] = "gesture",
99 #endif
100 };
101
102 static const char * const iio_modifier_names[] = {
103 [IIO_MOD_X] = "x",
104 [IIO_MOD_Y] = "y",
105 [IIO_MOD_Z] = "z",
106 [IIO_MOD_X_AND_Y] = "x&y",
107 [IIO_MOD_X_AND_Z] = "x&z",
108 [IIO_MOD_Y_AND_Z] = "y&z",
109 [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
110 [IIO_MOD_X_OR_Y] = "x|y",
111 [IIO_MOD_X_OR_Z] = "x|z",
112 [IIO_MOD_Y_OR_Z] = "y|z",
113 [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
114 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
115 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
116 [IIO_MOD_LIGHT_BOTH] = "both",
117 [IIO_MOD_LIGHT_IR] = "ir",
118 [IIO_MOD_LIGHT_CLEAR] = "clear",
119 [IIO_MOD_LIGHT_RED] = "red",
120 [IIO_MOD_LIGHT_GREEN] = "green",
121 [IIO_MOD_LIGHT_BLUE] = "blue",
122 [IIO_MOD_LIGHT_UV] = "uv",
123 [IIO_MOD_LIGHT_DUV] = "duv",
124 [IIO_MOD_QUATERNION] = "quaternion",
125 [IIO_MOD_TEMP_AMBIENT] = "ambient",
126 [IIO_MOD_TEMP_OBJECT] = "object",
127 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
128 [IIO_MOD_NORTH_TRUE] = "from_north_true",
129 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
130 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
131 [IIO_MOD_RUNNING] = "running",
132 [IIO_MOD_JOGGING] = "jogging",
133 [IIO_MOD_WALKING] = "walking",
134 [IIO_MOD_STILL] = "still",
135 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
136 [IIO_MOD_I] = "i",
137 [IIO_MOD_Q] = "q",
138 [IIO_MOD_CO2] = "co2",
139 [IIO_MOD_VOC] = "voc",
140 [IIO_MOD_PM1] = "pm1",
141 [IIO_MOD_PM2P5] = "pm2p5",
142 [IIO_MOD_PM4] = "pm4",
143 [IIO_MOD_PM10] = "pm10",
144 [IIO_MOD_ETHANOL] = "ethanol",
145 [IIO_MOD_H2] = "h2",
146 [IIO_MOD_O2] = "o2",
147 };
148
149 /* relies on pairs of these shared then separate */
150 static const char * const iio_chan_info_postfix[] = {
151 [IIO_CHAN_INFO_RAW] = "raw",
152 [IIO_CHAN_INFO_PROCESSED] = "input",
153 [IIO_CHAN_INFO_SCALE] = "scale",
154 [IIO_CHAN_INFO_OFFSET] = "offset",
155 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
156 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
157 [IIO_CHAN_INFO_PEAK] = "peak_raw",
158 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
159 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
160 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
161 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
162 = "filter_low_pass_3db_frequency",
163 [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
164 = "filter_high_pass_3db_frequency",
165 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
166 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
167 [IIO_CHAN_INFO_PHASE] = "phase",
168 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
169 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
170 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
171 [IIO_CHAN_INFO_ENABLE] = "en",
172 [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
173 [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
174 [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
175 [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
176 [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
177 [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
178 [IIO_CHAN_INFO_THERMOCOUPLE_TYPE] = "thermocouple_type",
179 [IIO_CHAN_INFO_CALIBAMBIENT] = "calibambient",
180 };
181
182 #if defined(CONFIG_DEBUG_FS)
183 /*
184 * There's also a CONFIG_DEBUG_FS guard in include/linux/iio/iio.h for
185 * iio_get_debugfs_dentry() to make it inline if CONFIG_DEBUG_FS is undefined
186 */
iio_get_debugfs_dentry(struct iio_dev * indio_dev)187 struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
188 {
189 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
190 return iio_dev_opaque->debugfs_dentry;
191 }
192 EXPORT_SYMBOL_GPL(iio_get_debugfs_dentry);
193 #endif
194
195 /**
196 * iio_find_channel_from_si() - get channel from its scan index
197 * @indio_dev: device
198 * @si: scan index to match
199 */
200 const struct iio_chan_spec
iio_find_channel_from_si(struct iio_dev * indio_dev,int si)201 *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
202 {
203 int i;
204
205 for (i = 0; i < indio_dev->num_channels; i++)
206 if (indio_dev->channels[i].scan_index == si)
207 return &indio_dev->channels[i];
208 return NULL;
209 }
210
211 /* This turns up an awful lot */
iio_read_const_attr(struct device * dev,struct device_attribute * attr,char * buf)212 ssize_t iio_read_const_attr(struct device *dev,
213 struct device_attribute *attr,
214 char *buf)
215 {
216 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
217 }
218 EXPORT_SYMBOL(iio_read_const_attr);
219
220 /**
221 * iio_device_set_clock() - Set current timestamping clock for the device
222 * @indio_dev: IIO device structure containing the device
223 * @clock_id: timestamping clock posix identifier to set.
224 */
iio_device_set_clock(struct iio_dev * indio_dev,clockid_t clock_id)225 int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id)
226 {
227 int ret;
228 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
229 const struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
230
231 ret = mutex_lock_interruptible(&indio_dev->mlock);
232 if (ret)
233 return ret;
234 if ((ev_int && iio_event_enabled(ev_int)) ||
235 iio_buffer_enabled(indio_dev)) {
236 mutex_unlock(&indio_dev->mlock);
237 return -EBUSY;
238 }
239 indio_dev->clock_id = clock_id;
240 mutex_unlock(&indio_dev->mlock);
241
242 return 0;
243 }
244 EXPORT_SYMBOL(iio_device_set_clock);
245
246 /**
247 * iio_get_time_ns() - utility function to get a time stamp for events etc
248 * @indio_dev: device
249 */
iio_get_time_ns(const struct iio_dev * indio_dev)250 s64 iio_get_time_ns(const struct iio_dev *indio_dev)
251 {
252 struct timespec64 tp;
253
254 switch (iio_device_get_clock(indio_dev)) {
255 case CLOCK_REALTIME:
256 return ktime_get_real_ns();
257 case CLOCK_MONOTONIC:
258 return ktime_get_ns();
259 case CLOCK_MONOTONIC_RAW:
260 return ktime_get_raw_ns();
261 case CLOCK_REALTIME_COARSE:
262 return ktime_to_ns(ktime_get_coarse_real());
263 case CLOCK_MONOTONIC_COARSE:
264 ktime_get_coarse_ts64(&tp);
265 return timespec64_to_ns(&tp);
266 case CLOCK_BOOTTIME:
267 return ktime_get_boottime_ns();
268 case CLOCK_TAI:
269 return ktime_get_clocktai_ns();
270 default:
271 BUG();
272 }
273 }
274 EXPORT_SYMBOL(iio_get_time_ns);
275
276 /**
277 * iio_get_time_res() - utility function to get time stamp clock resolution in
278 * nano seconds.
279 * @indio_dev: device
280 */
iio_get_time_res(const struct iio_dev * indio_dev)281 unsigned int iio_get_time_res(const struct iio_dev *indio_dev)
282 {
283 switch (iio_device_get_clock(indio_dev)) {
284 case CLOCK_REALTIME:
285 case CLOCK_MONOTONIC:
286 case CLOCK_MONOTONIC_RAW:
287 case CLOCK_BOOTTIME:
288 case CLOCK_TAI:
289 return hrtimer_resolution;
290 case CLOCK_REALTIME_COARSE:
291 case CLOCK_MONOTONIC_COARSE:
292 return LOW_RES_NSEC;
293 default:
294 BUG();
295 }
296 }
297 EXPORT_SYMBOL(iio_get_time_res);
298
iio_init(void)299 static int __init iio_init(void)
300 {
301 int ret;
302
303 /* Register sysfs bus */
304 ret = bus_register(&iio_bus_type);
305 if (ret < 0) {
306 pr_err("could not register bus type\n");
307 goto error_nothing;
308 }
309
310 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
311 if (ret < 0) {
312 pr_err("failed to allocate char dev region\n");
313 goto error_unregister_bus_type;
314 }
315
316 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
317
318 return 0;
319
320 error_unregister_bus_type:
321 bus_unregister(&iio_bus_type);
322 error_nothing:
323 return ret;
324 }
325
iio_exit(void)326 static void __exit iio_exit(void)
327 {
328 if (iio_devt)
329 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
330 bus_unregister(&iio_bus_type);
331 debugfs_remove(iio_debugfs_dentry);
332 }
333
334 #if defined(CONFIG_DEBUG_FS)
iio_debugfs_read_reg(struct file * file,char __user * userbuf,size_t count,loff_t * ppos)335 static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
336 size_t count, loff_t *ppos)
337 {
338 struct iio_dev *indio_dev = file->private_data;
339 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
340 unsigned val = 0;
341 int ret;
342
343 if (*ppos > 0)
344 return simple_read_from_buffer(userbuf, count, ppos,
345 iio_dev_opaque->read_buf,
346 iio_dev_opaque->read_buf_len);
347
348 ret = indio_dev->info->debugfs_reg_access(indio_dev,
349 iio_dev_opaque->cached_reg_addr,
350 0, &val);
351 if (ret) {
352 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
353 return ret;
354 }
355
356 iio_dev_opaque->read_buf_len = snprintf(iio_dev_opaque->read_buf,
357 sizeof(iio_dev_opaque->read_buf),
358 "0x%X\n", val);
359
360 return simple_read_from_buffer(userbuf, count, ppos,
361 iio_dev_opaque->read_buf,
362 iio_dev_opaque->read_buf_len);
363 }
364
iio_debugfs_write_reg(struct file * file,const char __user * userbuf,size_t count,loff_t * ppos)365 static ssize_t iio_debugfs_write_reg(struct file *file,
366 const char __user *userbuf, size_t count, loff_t *ppos)
367 {
368 struct iio_dev *indio_dev = file->private_data;
369 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
370 unsigned reg, val;
371 char buf[80];
372 int ret;
373
374 count = min_t(size_t, count, (sizeof(buf)-1));
375 if (copy_from_user(buf, userbuf, count))
376 return -EFAULT;
377
378 buf[count] = 0;
379
380 ret = sscanf(buf, "%i %i", ®, &val);
381
382 switch (ret) {
383 case 1:
384 iio_dev_opaque->cached_reg_addr = reg;
385 break;
386 case 2:
387 iio_dev_opaque->cached_reg_addr = reg;
388 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
389 val, NULL);
390 if (ret) {
391 dev_err(indio_dev->dev.parent, "%s: write failed\n",
392 __func__);
393 return ret;
394 }
395 break;
396 default:
397 return -EINVAL;
398 }
399
400 return count;
401 }
402
403 static const struct file_operations iio_debugfs_reg_fops = {
404 .open = simple_open,
405 .read = iio_debugfs_read_reg,
406 .write = iio_debugfs_write_reg,
407 };
408
iio_device_unregister_debugfs(struct iio_dev * indio_dev)409 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
410 {
411 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
412 debugfs_remove_recursive(iio_dev_opaque->debugfs_dentry);
413 }
414
iio_device_register_debugfs(struct iio_dev * indio_dev)415 static void iio_device_register_debugfs(struct iio_dev *indio_dev)
416 {
417 struct iio_dev_opaque *iio_dev_opaque;
418
419 if (indio_dev->info->debugfs_reg_access == NULL)
420 return;
421
422 if (!iio_debugfs_dentry)
423 return;
424
425 iio_dev_opaque = to_iio_dev_opaque(indio_dev);
426
427 iio_dev_opaque->debugfs_dentry =
428 debugfs_create_dir(dev_name(&indio_dev->dev),
429 iio_debugfs_dentry);
430
431 debugfs_create_file("direct_reg_access", 0644,
432 iio_dev_opaque->debugfs_dentry, indio_dev,
433 &iio_debugfs_reg_fops);
434 }
435 #else
iio_device_register_debugfs(struct iio_dev * indio_dev)436 static void iio_device_register_debugfs(struct iio_dev *indio_dev)
437 {
438 }
439
iio_device_unregister_debugfs(struct iio_dev * indio_dev)440 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
441 {
442 }
443 #endif /* CONFIG_DEBUG_FS */
444
iio_read_channel_ext_info(struct device * dev,struct device_attribute * attr,char * buf)445 static ssize_t iio_read_channel_ext_info(struct device *dev,
446 struct device_attribute *attr,
447 char *buf)
448 {
449 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
450 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
451 const struct iio_chan_spec_ext_info *ext_info;
452
453 ext_info = &this_attr->c->ext_info[this_attr->address];
454
455 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
456 }
457
iio_write_channel_ext_info(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)458 static ssize_t iio_write_channel_ext_info(struct device *dev,
459 struct device_attribute *attr,
460 const char *buf,
461 size_t len)
462 {
463 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
464 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
465 const struct iio_chan_spec_ext_info *ext_info;
466
467 ext_info = &this_attr->c->ext_info[this_attr->address];
468
469 return ext_info->write(indio_dev, ext_info->private,
470 this_attr->c, buf, len);
471 }
472
iio_enum_available_read(struct iio_dev * indio_dev,uintptr_t priv,const struct iio_chan_spec * chan,char * buf)473 ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
474 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
475 {
476 const struct iio_enum *e = (const struct iio_enum *)priv;
477 unsigned int i;
478 size_t len = 0;
479
480 if (!e->num_items)
481 return 0;
482
483 for (i = 0; i < e->num_items; ++i)
484 len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
485
486 /* replace last space with a newline */
487 buf[len - 1] = '\n';
488
489 return len;
490 }
491 EXPORT_SYMBOL_GPL(iio_enum_available_read);
492
iio_enum_read(struct iio_dev * indio_dev,uintptr_t priv,const struct iio_chan_spec * chan,char * buf)493 ssize_t iio_enum_read(struct iio_dev *indio_dev,
494 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
495 {
496 const struct iio_enum *e = (const struct iio_enum *)priv;
497 int i;
498
499 if (!e->get)
500 return -EINVAL;
501
502 i = e->get(indio_dev, chan);
503 if (i < 0)
504 return i;
505 else if (i >= e->num_items)
506 return -EINVAL;
507
508 return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
509 }
510 EXPORT_SYMBOL_GPL(iio_enum_read);
511
iio_enum_write(struct iio_dev * indio_dev,uintptr_t priv,const struct iio_chan_spec * chan,const char * buf,size_t len)512 ssize_t iio_enum_write(struct iio_dev *indio_dev,
513 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
514 size_t len)
515 {
516 const struct iio_enum *e = (const struct iio_enum *)priv;
517 int ret;
518
519 if (!e->set)
520 return -EINVAL;
521
522 ret = __sysfs_match_string(e->items, e->num_items, buf);
523 if (ret < 0)
524 return ret;
525
526 ret = e->set(indio_dev, chan, ret);
527 return ret ? ret : len;
528 }
529 EXPORT_SYMBOL_GPL(iio_enum_write);
530
531 static const struct iio_mount_matrix iio_mount_idmatrix = {
532 .rotation = {
533 "1", "0", "0",
534 "0", "1", "0",
535 "0", "0", "1"
536 }
537 };
538
iio_setup_mount_idmatrix(const struct device * dev,struct iio_mount_matrix * matrix)539 static int iio_setup_mount_idmatrix(const struct device *dev,
540 struct iio_mount_matrix *matrix)
541 {
542 *matrix = iio_mount_idmatrix;
543 dev_info(dev, "mounting matrix not found: using identity...\n");
544 return 0;
545 }
546
iio_show_mount_matrix(struct iio_dev * indio_dev,uintptr_t priv,const struct iio_chan_spec * chan,char * buf)547 ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
548 const struct iio_chan_spec *chan, char *buf)
549 {
550 const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *)
551 priv)(indio_dev, chan);
552
553 if (IS_ERR(mtx))
554 return PTR_ERR(mtx);
555
556 if (!mtx)
557 mtx = &iio_mount_idmatrix;
558
559 return snprintf(buf, PAGE_SIZE, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n",
560 mtx->rotation[0], mtx->rotation[1], mtx->rotation[2],
561 mtx->rotation[3], mtx->rotation[4], mtx->rotation[5],
562 mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]);
563 }
564 EXPORT_SYMBOL_GPL(iio_show_mount_matrix);
565
566 /**
567 * iio_read_mount_matrix() - retrieve iio device mounting matrix from
568 * device "mount-matrix" property
569 * @dev: device the mounting matrix property is assigned to
570 * @propname: device specific mounting matrix property name
571 * @matrix: where to store retrieved matrix
572 *
573 * If device is assigned no mounting matrix property, a default 3x3 identity
574 * matrix will be filled in.
575 *
576 * Return: 0 if success, or a negative error code on failure.
577 */
iio_read_mount_matrix(struct device * dev,const char * propname,struct iio_mount_matrix * matrix)578 int iio_read_mount_matrix(struct device *dev, const char *propname,
579 struct iio_mount_matrix *matrix)
580 {
581 size_t len = ARRAY_SIZE(iio_mount_idmatrix.rotation);
582 int err;
583
584 err = device_property_read_string_array(dev, propname,
585 matrix->rotation, len);
586 if (err == len)
587 return 0;
588
589 if (err >= 0)
590 /* Invalid number of matrix entries. */
591 return -EINVAL;
592
593 if (err != -EINVAL)
594 /* Invalid matrix declaration format. */
595 return err;
596
597 /* Matrix was not declared at all: fallback to identity. */
598 return iio_setup_mount_idmatrix(dev, matrix);
599 }
600 EXPORT_SYMBOL(iio_read_mount_matrix);
601
__iio_format_value(char * buf,size_t len,unsigned int type,int size,const int * vals)602 static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
603 int size, const int *vals)
604 {
605 unsigned long long tmp;
606 int tmp0, tmp1;
607 bool scale_db = false;
608
609 switch (type) {
610 case IIO_VAL_INT:
611 return scnprintf(buf, len, "%d", vals[0]);
612 case IIO_VAL_INT_PLUS_MICRO_DB:
613 scale_db = true;
614 fallthrough;
615 case IIO_VAL_INT_PLUS_MICRO:
616 if (vals[1] < 0)
617 return scnprintf(buf, len, "-%d.%06u%s", abs(vals[0]),
618 -vals[1], scale_db ? " dB" : "");
619 else
620 return scnprintf(buf, len, "%d.%06u%s", vals[0], vals[1],
621 scale_db ? " dB" : "");
622 case IIO_VAL_INT_PLUS_NANO:
623 if (vals[1] < 0)
624 return scnprintf(buf, len, "-%d.%09u", abs(vals[0]),
625 -vals[1]);
626 else
627 return scnprintf(buf, len, "%d.%09u", vals[0], vals[1]);
628 case IIO_VAL_FRACTIONAL:
629 tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
630 tmp1 = vals[1];
631 tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1);
632 return scnprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
633 case IIO_VAL_FRACTIONAL_LOG2:
634 tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
635 tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1);
636 return scnprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
637 case IIO_VAL_INT_MULTIPLE:
638 {
639 int i;
640 int l = 0;
641
642 for (i = 0; i < size; ++i) {
643 l += scnprintf(&buf[l], len - l, "%d ", vals[i]);
644 if (l >= len)
645 break;
646 }
647 return l;
648 }
649 case IIO_VAL_CHAR:
650 return scnprintf(buf, len, "%c", (char)vals[0]);
651 default:
652 return 0;
653 }
654 }
655
656 /**
657 * iio_format_value() - Formats a IIO value into its string representation
658 * @buf: The buffer to which the formatted value gets written
659 * which is assumed to be big enough (i.e. PAGE_SIZE).
660 * @type: One of the IIO_VAL_* constants. This decides how the val
661 * and val2 parameters are formatted.
662 * @size: Number of IIO value entries contained in vals
663 * @vals: Pointer to the values, exact meaning depends on the
664 * type parameter.
665 *
666 * Return: 0 by default, a negative number on failure or the
667 * total number of characters written for a type that belongs
668 * to the IIO_VAL_* constant.
669 */
iio_format_value(char * buf,unsigned int type,int size,int * vals)670 ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
671 {
672 ssize_t len;
673
674 len = __iio_format_value(buf, PAGE_SIZE, type, size, vals);
675 if (len >= PAGE_SIZE - 1)
676 return -EFBIG;
677
678 return len + sprintf(buf + len, "\n");
679 }
680 EXPORT_SYMBOL_GPL(iio_format_value);
681
iio_read_channel_info(struct device * dev,struct device_attribute * attr,char * buf)682 static ssize_t iio_read_channel_info(struct device *dev,
683 struct device_attribute *attr,
684 char *buf)
685 {
686 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
687 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
688 int vals[INDIO_MAX_RAW_ELEMENTS];
689 int ret;
690 int val_len = 2;
691
692 if (indio_dev->info->read_raw_multi)
693 ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
694 INDIO_MAX_RAW_ELEMENTS,
695 vals, &val_len,
696 this_attr->address);
697 else
698 ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
699 &vals[0], &vals[1], this_attr->address);
700
701 if (ret < 0)
702 return ret;
703
704 return iio_format_value(buf, ret, val_len, vals);
705 }
706
iio_format_avail_list(char * buf,const int * vals,int type,int length)707 static ssize_t iio_format_avail_list(char *buf, const int *vals,
708 int type, int length)
709 {
710 int i;
711 ssize_t len = 0;
712
713 switch (type) {
714 case IIO_VAL_INT:
715 for (i = 0; i < length; i++) {
716 len += __iio_format_value(buf + len, PAGE_SIZE - len,
717 type, 1, &vals[i]);
718 if (len >= PAGE_SIZE)
719 return -EFBIG;
720 if (i < length - 1)
721 len += scnprintf(buf + len, PAGE_SIZE - len,
722 " ");
723 else
724 len += scnprintf(buf + len, PAGE_SIZE - len,
725 "\n");
726 if (len >= PAGE_SIZE)
727 return -EFBIG;
728 }
729 break;
730 default:
731 for (i = 0; i < length / 2; i++) {
732 len += __iio_format_value(buf + len, PAGE_SIZE - len,
733 type, 2, &vals[i * 2]);
734 if (len >= PAGE_SIZE)
735 return -EFBIG;
736 if (i < length / 2 - 1)
737 len += scnprintf(buf + len, PAGE_SIZE - len,
738 " ");
739 else
740 len += scnprintf(buf + len, PAGE_SIZE - len,
741 "\n");
742 if (len >= PAGE_SIZE)
743 return -EFBIG;
744 }
745 }
746
747 return len;
748 }
749
iio_format_avail_range(char * buf,const int * vals,int type)750 static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
751 {
752 int i;
753 ssize_t len;
754
755 len = snprintf(buf, PAGE_SIZE, "[");
756 switch (type) {
757 case IIO_VAL_INT:
758 for (i = 0; i < 3; i++) {
759 len += __iio_format_value(buf + len, PAGE_SIZE - len,
760 type, 1, &vals[i]);
761 if (len >= PAGE_SIZE)
762 return -EFBIG;
763 if (i < 2)
764 len += scnprintf(buf + len, PAGE_SIZE - len,
765 " ");
766 else
767 len += scnprintf(buf + len, PAGE_SIZE - len,
768 "]\n");
769 if (len >= PAGE_SIZE)
770 return -EFBIG;
771 }
772 break;
773 default:
774 for (i = 0; i < 3; i++) {
775 len += __iio_format_value(buf + len, PAGE_SIZE - len,
776 type, 2, &vals[i * 2]);
777 if (len >= PAGE_SIZE)
778 return -EFBIG;
779 if (i < 2)
780 len += scnprintf(buf + len, PAGE_SIZE - len,
781 " ");
782 else
783 len += scnprintf(buf + len, PAGE_SIZE - len,
784 "]\n");
785 if (len >= PAGE_SIZE)
786 return -EFBIG;
787 }
788 }
789
790 return len;
791 }
792
iio_read_channel_info_avail(struct device * dev,struct device_attribute * attr,char * buf)793 static ssize_t iio_read_channel_info_avail(struct device *dev,
794 struct device_attribute *attr,
795 char *buf)
796 {
797 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
798 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
799 const int *vals;
800 int ret;
801 int length;
802 int type;
803
804 ret = indio_dev->info->read_avail(indio_dev, this_attr->c,
805 &vals, &type, &length,
806 this_attr->address);
807
808 if (ret < 0)
809 return ret;
810 switch (ret) {
811 case IIO_AVAIL_LIST:
812 return iio_format_avail_list(buf, vals, type, length);
813 case IIO_AVAIL_RANGE:
814 return iio_format_avail_range(buf, vals, type);
815 default:
816 return -EINVAL;
817 }
818 }
819
820 /**
821 * __iio_str_to_fixpoint() - Parse a fixed-point number from a string
822 * @str: The string to parse
823 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
824 * @integer: The integer part of the number
825 * @fract: The fractional part of the number
826 * @scale_db: True if this should parse as dB
827 *
828 * Returns 0 on success, or a negative error code if the string could not be
829 * parsed.
830 */
__iio_str_to_fixpoint(const char * str,int fract_mult,int * integer,int * fract,bool scale_db)831 static int __iio_str_to_fixpoint(const char *str, int fract_mult,
832 int *integer, int *fract, bool scale_db)
833 {
834 int i = 0, f = 0;
835 bool integer_part = true, negative = false;
836
837 if (fract_mult == 0) {
838 *fract = 0;
839
840 return kstrtoint(str, 0, integer);
841 }
842
843 if (str[0] == '-') {
844 negative = true;
845 str++;
846 } else if (str[0] == '+') {
847 str++;
848 }
849
850 while (*str) {
851 if ('0' <= *str && *str <= '9') {
852 if (integer_part) {
853 i = i * 10 + *str - '0';
854 } else {
855 f += fract_mult * (*str - '0');
856 fract_mult /= 10;
857 }
858 } else if (*str == '\n') {
859 if (*(str + 1) == '\0')
860 break;
861 else
862 return -EINVAL;
863 } else if (!strncmp(str, " dB", sizeof(" dB") - 1) && scale_db) {
864 /* Ignore the dB suffix */
865 str += sizeof(" dB") - 1;
866 continue;
867 } else if (!strncmp(str, "dB", sizeof("dB") - 1) && scale_db) {
868 /* Ignore the dB suffix */
869 str += sizeof("dB") - 1;
870 continue;
871 } else if (*str == '.' && integer_part) {
872 integer_part = false;
873 } else {
874 return -EINVAL;
875 }
876 str++;
877 }
878
879 if (negative) {
880 if (i)
881 i = -i;
882 else
883 f = -f;
884 }
885
886 *integer = i;
887 *fract = f;
888
889 return 0;
890 }
891
892 /**
893 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
894 * @str: The string to parse
895 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
896 * @integer: The integer part of the number
897 * @fract: The fractional part of the number
898 *
899 * Returns 0 on success, or a negative error code if the string could not be
900 * parsed.
901 */
iio_str_to_fixpoint(const char * str,int fract_mult,int * integer,int * fract)902 int iio_str_to_fixpoint(const char *str, int fract_mult,
903 int *integer, int *fract)
904 {
905 return __iio_str_to_fixpoint(str, fract_mult, integer, fract, false);
906 }
907 EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
908
iio_write_channel_info(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)909 static ssize_t iio_write_channel_info(struct device *dev,
910 struct device_attribute *attr,
911 const char *buf,
912 size_t len)
913 {
914 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
915 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
916 int ret, fract_mult = 100000;
917 int integer, fract = 0;
918 bool is_char = false;
919 bool scale_db = false;
920
921 /* Assumes decimal - precision based on number of digits */
922 if (!indio_dev->info->write_raw)
923 return -EINVAL;
924
925 if (indio_dev->info->write_raw_get_fmt)
926 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
927 this_attr->c, this_attr->address)) {
928 case IIO_VAL_INT:
929 fract_mult = 0;
930 break;
931 case IIO_VAL_INT_PLUS_MICRO_DB:
932 scale_db = true;
933 fallthrough;
934 case IIO_VAL_INT_PLUS_MICRO:
935 fract_mult = 100000;
936 break;
937 case IIO_VAL_INT_PLUS_NANO:
938 fract_mult = 100000000;
939 break;
940 case IIO_VAL_CHAR:
941 is_char = true;
942 break;
943 default:
944 return -EINVAL;
945 }
946
947 if (is_char) {
948 char ch;
949
950 if (sscanf(buf, "%c", &ch) != 1)
951 return -EINVAL;
952 integer = ch;
953 } else {
954 ret = __iio_str_to_fixpoint(buf, fract_mult, &integer, &fract,
955 scale_db);
956 if (ret)
957 return ret;
958 }
959
960 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
961 integer, fract, this_attr->address);
962 if (ret)
963 return ret;
964
965 return len;
966 }
967
968 static
__iio_device_attr_init(struct device_attribute * dev_attr,const char * postfix,struct iio_chan_spec const * chan,ssize_t (* readfunc)(struct device * dev,struct device_attribute * attr,char * buf),ssize_t (* writefunc)(struct device * dev,struct device_attribute * attr,const char * buf,size_t len),enum iio_shared_by shared_by)969 int __iio_device_attr_init(struct device_attribute *dev_attr,
970 const char *postfix,
971 struct iio_chan_spec const *chan,
972 ssize_t (*readfunc)(struct device *dev,
973 struct device_attribute *attr,
974 char *buf),
975 ssize_t (*writefunc)(struct device *dev,
976 struct device_attribute *attr,
977 const char *buf,
978 size_t len),
979 enum iio_shared_by shared_by)
980 {
981 int ret = 0;
982 char *name = NULL;
983 char *full_postfix;
984 sysfs_attr_init(&dev_attr->attr);
985
986 /* Build up postfix of <extend_name>_<modifier>_postfix */
987 if (chan->modified && (shared_by == IIO_SEPARATE)) {
988 if (chan->extend_name)
989 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
990 iio_modifier_names[chan
991 ->channel2],
992 chan->extend_name,
993 postfix);
994 else
995 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
996 iio_modifier_names[chan
997 ->channel2],
998 postfix);
999 } else {
1000 if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
1001 full_postfix = kstrdup(postfix, GFP_KERNEL);
1002 else
1003 full_postfix = kasprintf(GFP_KERNEL,
1004 "%s_%s",
1005 chan->extend_name,
1006 postfix);
1007 }
1008 if (full_postfix == NULL)
1009 return -ENOMEM;
1010
1011 if (chan->differential) { /* Differential can not have modifier */
1012 switch (shared_by) {
1013 case IIO_SHARED_BY_ALL:
1014 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
1015 break;
1016 case IIO_SHARED_BY_DIR:
1017 name = kasprintf(GFP_KERNEL, "%s_%s",
1018 iio_direction[chan->output],
1019 full_postfix);
1020 break;
1021 case IIO_SHARED_BY_TYPE:
1022 name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
1023 iio_direction[chan->output],
1024 iio_chan_type_name_spec[chan->type],
1025 iio_chan_type_name_spec[chan->type],
1026 full_postfix);
1027 break;
1028 case IIO_SEPARATE:
1029 if (!chan->indexed) {
1030 WARN(1, "Differential channels must be indexed\n");
1031 ret = -EINVAL;
1032 goto error_free_full_postfix;
1033 }
1034 name = kasprintf(GFP_KERNEL,
1035 "%s_%s%d-%s%d_%s",
1036 iio_direction[chan->output],
1037 iio_chan_type_name_spec[chan->type],
1038 chan->channel,
1039 iio_chan_type_name_spec[chan->type],
1040 chan->channel2,
1041 full_postfix);
1042 break;
1043 }
1044 } else { /* Single ended */
1045 switch (shared_by) {
1046 case IIO_SHARED_BY_ALL:
1047 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
1048 break;
1049 case IIO_SHARED_BY_DIR:
1050 name = kasprintf(GFP_KERNEL, "%s_%s",
1051 iio_direction[chan->output],
1052 full_postfix);
1053 break;
1054 case IIO_SHARED_BY_TYPE:
1055 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
1056 iio_direction[chan->output],
1057 iio_chan_type_name_spec[chan->type],
1058 full_postfix);
1059 break;
1060
1061 case IIO_SEPARATE:
1062 if (chan->indexed)
1063 name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
1064 iio_direction[chan->output],
1065 iio_chan_type_name_spec[chan->type],
1066 chan->channel,
1067 full_postfix);
1068 else
1069 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
1070 iio_direction[chan->output],
1071 iio_chan_type_name_spec[chan->type],
1072 full_postfix);
1073 break;
1074 }
1075 }
1076 if (name == NULL) {
1077 ret = -ENOMEM;
1078 goto error_free_full_postfix;
1079 }
1080 dev_attr->attr.name = name;
1081
1082 if (readfunc) {
1083 dev_attr->attr.mode |= S_IRUGO;
1084 dev_attr->show = readfunc;
1085 }
1086
1087 if (writefunc) {
1088 dev_attr->attr.mode |= S_IWUSR;
1089 dev_attr->store = writefunc;
1090 }
1091
1092 error_free_full_postfix:
1093 kfree(full_postfix);
1094
1095 return ret;
1096 }
1097
__iio_device_attr_deinit(struct device_attribute * dev_attr)1098 static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
1099 {
1100 kfree(dev_attr->attr.name);
1101 }
1102
__iio_add_chan_devattr(const char * postfix,struct iio_chan_spec const * chan,ssize_t (* readfunc)(struct device * dev,struct device_attribute * attr,char * buf),ssize_t (* writefunc)(struct device * dev,struct device_attribute * attr,const char * buf,size_t len),u64 mask,enum iio_shared_by shared_by,struct device * dev,struct list_head * attr_list)1103 int __iio_add_chan_devattr(const char *postfix,
1104 struct iio_chan_spec const *chan,
1105 ssize_t (*readfunc)(struct device *dev,
1106 struct device_attribute *attr,
1107 char *buf),
1108 ssize_t (*writefunc)(struct device *dev,
1109 struct device_attribute *attr,
1110 const char *buf,
1111 size_t len),
1112 u64 mask,
1113 enum iio_shared_by shared_by,
1114 struct device *dev,
1115 struct list_head *attr_list)
1116 {
1117 int ret;
1118 struct iio_dev_attr *iio_attr, *t;
1119
1120 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
1121 if (iio_attr == NULL)
1122 return -ENOMEM;
1123 ret = __iio_device_attr_init(&iio_attr->dev_attr,
1124 postfix, chan,
1125 readfunc, writefunc, shared_by);
1126 if (ret)
1127 goto error_iio_dev_attr_free;
1128 iio_attr->c = chan;
1129 iio_attr->address = mask;
1130 list_for_each_entry(t, attr_list, l)
1131 if (strcmp(t->dev_attr.attr.name,
1132 iio_attr->dev_attr.attr.name) == 0) {
1133 if (shared_by == IIO_SEPARATE)
1134 dev_err(dev, "tried to double register : %s\n",
1135 t->dev_attr.attr.name);
1136 ret = -EBUSY;
1137 goto error_device_attr_deinit;
1138 }
1139 list_add(&iio_attr->l, attr_list);
1140
1141 return 0;
1142
1143 error_device_attr_deinit:
1144 __iio_device_attr_deinit(&iio_attr->dev_attr);
1145 error_iio_dev_attr_free:
1146 kfree(iio_attr);
1147 return ret;
1148 }
1149
iio_device_add_info_mask_type(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,enum iio_shared_by shared_by,const long * infomask)1150 static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
1151 struct iio_chan_spec const *chan,
1152 enum iio_shared_by shared_by,
1153 const long *infomask)
1154 {
1155 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1156 int i, ret, attrcount = 0;
1157
1158 for_each_set_bit(i, infomask, sizeof(*infomask)*8) {
1159 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1160 return -EINVAL;
1161 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
1162 chan,
1163 &iio_read_channel_info,
1164 &iio_write_channel_info,
1165 i,
1166 shared_by,
1167 &indio_dev->dev,
1168 &iio_dev_opaque->channel_attr_list);
1169 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
1170 continue;
1171 else if (ret < 0)
1172 return ret;
1173 attrcount++;
1174 }
1175
1176 return attrcount;
1177 }
1178
iio_device_add_info_mask_type_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,enum iio_shared_by shared_by,const long * infomask)1179 static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev,
1180 struct iio_chan_spec const *chan,
1181 enum iio_shared_by shared_by,
1182 const long *infomask)
1183 {
1184 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1185 int i, ret, attrcount = 0;
1186 char *avail_postfix;
1187
1188 for_each_set_bit(i, infomask, sizeof(*infomask) * 8) {
1189 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1190 return -EINVAL;
1191 avail_postfix = kasprintf(GFP_KERNEL,
1192 "%s_available",
1193 iio_chan_info_postfix[i]);
1194 if (!avail_postfix)
1195 return -ENOMEM;
1196
1197 ret = __iio_add_chan_devattr(avail_postfix,
1198 chan,
1199 &iio_read_channel_info_avail,
1200 NULL,
1201 i,
1202 shared_by,
1203 &indio_dev->dev,
1204 &iio_dev_opaque->channel_attr_list);
1205 kfree(avail_postfix);
1206 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
1207 continue;
1208 else if (ret < 0)
1209 return ret;
1210 attrcount++;
1211 }
1212
1213 return attrcount;
1214 }
1215
iio_device_add_channel_sysfs(struct iio_dev * indio_dev,struct iio_chan_spec const * chan)1216 static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
1217 struct iio_chan_spec const *chan)
1218 {
1219 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1220 int ret, attrcount = 0;
1221 const struct iio_chan_spec_ext_info *ext_info;
1222
1223 if (chan->channel < 0)
1224 return 0;
1225 ret = iio_device_add_info_mask_type(indio_dev, chan,
1226 IIO_SEPARATE,
1227 &chan->info_mask_separate);
1228 if (ret < 0)
1229 return ret;
1230 attrcount += ret;
1231
1232 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1233 IIO_SEPARATE,
1234 &chan->
1235 info_mask_separate_available);
1236 if (ret < 0)
1237 return ret;
1238 attrcount += ret;
1239
1240 ret = iio_device_add_info_mask_type(indio_dev, chan,
1241 IIO_SHARED_BY_TYPE,
1242 &chan->info_mask_shared_by_type);
1243 if (ret < 0)
1244 return ret;
1245 attrcount += ret;
1246
1247 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1248 IIO_SHARED_BY_TYPE,
1249 &chan->
1250 info_mask_shared_by_type_available);
1251 if (ret < 0)
1252 return ret;
1253 attrcount += ret;
1254
1255 ret = iio_device_add_info_mask_type(indio_dev, chan,
1256 IIO_SHARED_BY_DIR,
1257 &chan->info_mask_shared_by_dir);
1258 if (ret < 0)
1259 return ret;
1260 attrcount += ret;
1261
1262 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1263 IIO_SHARED_BY_DIR,
1264 &chan->info_mask_shared_by_dir_available);
1265 if (ret < 0)
1266 return ret;
1267 attrcount += ret;
1268
1269 ret = iio_device_add_info_mask_type(indio_dev, chan,
1270 IIO_SHARED_BY_ALL,
1271 &chan->info_mask_shared_by_all);
1272 if (ret < 0)
1273 return ret;
1274 attrcount += ret;
1275
1276 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1277 IIO_SHARED_BY_ALL,
1278 &chan->info_mask_shared_by_all_available);
1279 if (ret < 0)
1280 return ret;
1281 attrcount += ret;
1282
1283 if (chan->ext_info) {
1284 unsigned int i = 0;
1285 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
1286 ret = __iio_add_chan_devattr(ext_info->name,
1287 chan,
1288 ext_info->read ?
1289 &iio_read_channel_ext_info : NULL,
1290 ext_info->write ?
1291 &iio_write_channel_ext_info : NULL,
1292 i,
1293 ext_info->shared,
1294 &indio_dev->dev,
1295 &iio_dev_opaque->channel_attr_list);
1296 i++;
1297 if (ret == -EBUSY && ext_info->shared)
1298 continue;
1299
1300 if (ret)
1301 return ret;
1302
1303 attrcount++;
1304 }
1305 }
1306
1307 return attrcount;
1308 }
1309
1310 /**
1311 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
1312 * @attr_list: List of IIO device attributes
1313 *
1314 * This function frees the memory allocated for each of the IIO device
1315 * attributes in the list.
1316 */
iio_free_chan_devattr_list(struct list_head * attr_list)1317 void iio_free_chan_devattr_list(struct list_head *attr_list)
1318 {
1319 struct iio_dev_attr *p, *n;
1320
1321 list_for_each_entry_safe(p, n, attr_list, l) {
1322 kfree(p->dev_attr.attr.name);
1323 list_del(&p->l);
1324 kfree(p);
1325 }
1326 }
1327
iio_show_dev_name(struct device * dev,struct device_attribute * attr,char * buf)1328 static ssize_t iio_show_dev_name(struct device *dev,
1329 struct device_attribute *attr,
1330 char *buf)
1331 {
1332 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1333 return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
1334 }
1335
1336 static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
1337
iio_show_dev_label(struct device * dev,struct device_attribute * attr,char * buf)1338 static ssize_t iio_show_dev_label(struct device *dev,
1339 struct device_attribute *attr,
1340 char *buf)
1341 {
1342 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1343 return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->label);
1344 }
1345
1346 static DEVICE_ATTR(label, S_IRUGO, iio_show_dev_label, NULL);
1347
iio_show_timestamp_clock(struct device * dev,struct device_attribute * attr,char * buf)1348 static ssize_t iio_show_timestamp_clock(struct device *dev,
1349 struct device_attribute *attr,
1350 char *buf)
1351 {
1352 const struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1353 const clockid_t clk = iio_device_get_clock(indio_dev);
1354 const char *name;
1355 ssize_t sz;
1356
1357 switch (clk) {
1358 case CLOCK_REALTIME:
1359 name = "realtime\n";
1360 sz = sizeof("realtime\n");
1361 break;
1362 case CLOCK_MONOTONIC:
1363 name = "monotonic\n";
1364 sz = sizeof("monotonic\n");
1365 break;
1366 case CLOCK_MONOTONIC_RAW:
1367 name = "monotonic_raw\n";
1368 sz = sizeof("monotonic_raw\n");
1369 break;
1370 case CLOCK_REALTIME_COARSE:
1371 name = "realtime_coarse\n";
1372 sz = sizeof("realtime_coarse\n");
1373 break;
1374 case CLOCK_MONOTONIC_COARSE:
1375 name = "monotonic_coarse\n";
1376 sz = sizeof("monotonic_coarse\n");
1377 break;
1378 case CLOCK_BOOTTIME:
1379 name = "boottime\n";
1380 sz = sizeof("boottime\n");
1381 break;
1382 case CLOCK_TAI:
1383 name = "tai\n";
1384 sz = sizeof("tai\n");
1385 break;
1386 default:
1387 BUG();
1388 }
1389
1390 memcpy(buf, name, sz);
1391 return sz;
1392 }
1393
iio_store_timestamp_clock(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)1394 static ssize_t iio_store_timestamp_clock(struct device *dev,
1395 struct device_attribute *attr,
1396 const char *buf, size_t len)
1397 {
1398 clockid_t clk;
1399 int ret;
1400
1401 if (sysfs_streq(buf, "realtime"))
1402 clk = CLOCK_REALTIME;
1403 else if (sysfs_streq(buf, "monotonic"))
1404 clk = CLOCK_MONOTONIC;
1405 else if (sysfs_streq(buf, "monotonic_raw"))
1406 clk = CLOCK_MONOTONIC_RAW;
1407 else if (sysfs_streq(buf, "realtime_coarse"))
1408 clk = CLOCK_REALTIME_COARSE;
1409 else if (sysfs_streq(buf, "monotonic_coarse"))
1410 clk = CLOCK_MONOTONIC_COARSE;
1411 else if (sysfs_streq(buf, "boottime"))
1412 clk = CLOCK_BOOTTIME;
1413 else if (sysfs_streq(buf, "tai"))
1414 clk = CLOCK_TAI;
1415 else
1416 return -EINVAL;
1417
1418 ret = iio_device_set_clock(dev_to_iio_dev(dev), clk);
1419 if (ret)
1420 return ret;
1421
1422 return len;
1423 }
1424
1425 static DEVICE_ATTR(current_timestamp_clock, S_IRUGO | S_IWUSR,
1426 iio_show_timestamp_clock, iio_store_timestamp_clock);
1427
iio_device_register_sysfs(struct iio_dev * indio_dev)1428 static int iio_device_register_sysfs(struct iio_dev *indio_dev)
1429 {
1430 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1431 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
1432 struct iio_dev_attr *p;
1433 struct attribute **attr, *clk = NULL;
1434
1435 /* First count elements in any existing group */
1436 if (indio_dev->info->attrs) {
1437 attr = indio_dev->info->attrs->attrs;
1438 while (*attr++ != NULL)
1439 attrcount_orig++;
1440 }
1441 attrcount = attrcount_orig;
1442 /*
1443 * New channel registration method - relies on the fact a group does
1444 * not need to be initialized if its name is NULL.
1445 */
1446 if (indio_dev->channels)
1447 for (i = 0; i < indio_dev->num_channels; i++) {
1448 const struct iio_chan_spec *chan =
1449 &indio_dev->channels[i];
1450
1451 if (chan->type == IIO_TIMESTAMP)
1452 clk = &dev_attr_current_timestamp_clock.attr;
1453
1454 ret = iio_device_add_channel_sysfs(indio_dev, chan);
1455 if (ret < 0)
1456 goto error_clear_attrs;
1457 attrcount += ret;
1458 }
1459
1460 if (iio_dev_opaque->event_interface)
1461 clk = &dev_attr_current_timestamp_clock.attr;
1462
1463 if (indio_dev->name)
1464 attrcount++;
1465 if (indio_dev->label)
1466 attrcount++;
1467 if (clk)
1468 attrcount++;
1469
1470 iio_dev_opaque->chan_attr_group.attrs =
1471 kcalloc(attrcount + 1,
1472 sizeof(iio_dev_opaque->chan_attr_group.attrs[0]),
1473 GFP_KERNEL);
1474 if (iio_dev_opaque->chan_attr_group.attrs == NULL) {
1475 ret = -ENOMEM;
1476 goto error_clear_attrs;
1477 }
1478 /* Copy across original attributes */
1479 if (indio_dev->info->attrs)
1480 memcpy(iio_dev_opaque->chan_attr_group.attrs,
1481 indio_dev->info->attrs->attrs,
1482 sizeof(iio_dev_opaque->chan_attr_group.attrs[0])
1483 *attrcount_orig);
1484 attrn = attrcount_orig;
1485 /* Add all elements from the list. */
1486 list_for_each_entry(p, &iio_dev_opaque->channel_attr_list, l)
1487 iio_dev_opaque->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
1488 if (indio_dev->name)
1489 iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
1490 if (indio_dev->label)
1491 iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_label.attr;
1492 if (clk)
1493 iio_dev_opaque->chan_attr_group.attrs[attrn++] = clk;
1494
1495 indio_dev->groups[indio_dev->groupcounter++] =
1496 &iio_dev_opaque->chan_attr_group;
1497
1498 return 0;
1499
1500 error_clear_attrs:
1501 iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list);
1502
1503 return ret;
1504 }
1505
iio_device_unregister_sysfs(struct iio_dev * indio_dev)1506 static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
1507 {
1508 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1509
1510 iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list);
1511 kfree(iio_dev_opaque->chan_attr_group.attrs);
1512 iio_dev_opaque->chan_attr_group.attrs = NULL;
1513 }
1514
iio_dev_release(struct device * device)1515 static void iio_dev_release(struct device *device)
1516 {
1517 struct iio_dev *indio_dev = dev_to_iio_dev(device);
1518 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1519
1520 if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
1521 iio_device_unregister_trigger_consumer(indio_dev);
1522 iio_device_unregister_eventset(indio_dev);
1523 iio_device_unregister_sysfs(indio_dev);
1524
1525 iio_buffer_put(indio_dev->buffer);
1526
1527 ida_simple_remove(&iio_ida, indio_dev->id);
1528 kfree(iio_dev_opaque);
1529 }
1530
1531 struct device_type iio_device_type = {
1532 .name = "iio_device",
1533 .release = iio_dev_release,
1534 };
1535
1536 /**
1537 * iio_device_alloc() - allocate an iio_dev from a driver
1538 * @parent: Parent device.
1539 * @sizeof_priv: Space to allocate for private structure.
1540 **/
iio_device_alloc(struct device * parent,int sizeof_priv)1541 struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)
1542 {
1543 struct iio_dev_opaque *iio_dev_opaque;
1544 struct iio_dev *dev;
1545 size_t alloc_size;
1546
1547 alloc_size = sizeof(struct iio_dev_opaque);
1548 if (sizeof_priv) {
1549 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1550 alloc_size += sizeof_priv;
1551 }
1552
1553 iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL);
1554 if (!iio_dev_opaque)
1555 return NULL;
1556
1557 dev = &iio_dev_opaque->indio_dev;
1558 dev->priv = (char *)iio_dev_opaque +
1559 ALIGN(sizeof(struct iio_dev_opaque), IIO_ALIGN);
1560
1561 dev->dev.parent = parent;
1562 dev->dev.groups = dev->groups;
1563 dev->dev.type = &iio_device_type;
1564 dev->dev.bus = &iio_bus_type;
1565 device_initialize(&dev->dev);
1566 dev_set_drvdata(&dev->dev, (void *)dev);
1567 mutex_init(&dev->mlock);
1568 mutex_init(&dev->info_exist_lock);
1569 INIT_LIST_HEAD(&iio_dev_opaque->channel_attr_list);
1570
1571 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
1572 if (dev->id < 0) {
1573 /* cannot use a dev_err as the name isn't available */
1574 pr_err("failed to get device id\n");
1575 kfree(iio_dev_opaque);
1576 return NULL;
1577 }
1578 dev_set_name(&dev->dev, "iio:device%d", dev->id);
1579 INIT_LIST_HEAD(&iio_dev_opaque->buffer_list);
1580
1581 return dev;
1582 }
1583 EXPORT_SYMBOL(iio_device_alloc);
1584
1585 /**
1586 * iio_device_free() - free an iio_dev from a driver
1587 * @dev: the iio_dev associated with the device
1588 **/
iio_device_free(struct iio_dev * dev)1589 void iio_device_free(struct iio_dev *dev)
1590 {
1591 if (dev)
1592 put_device(&dev->dev);
1593 }
1594 EXPORT_SYMBOL(iio_device_free);
1595
devm_iio_device_release(struct device * dev,void * res)1596 static void devm_iio_device_release(struct device *dev, void *res)
1597 {
1598 iio_device_free(*(struct iio_dev **)res);
1599 }
1600
1601 /**
1602 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1603 * @parent: Device to allocate iio_dev for, and parent for this IIO device
1604 * @sizeof_priv: Space to allocate for private structure.
1605 *
1606 * Managed iio_device_alloc. iio_dev allocated with this function is
1607 * automatically freed on driver detach.
1608 *
1609 * RETURNS:
1610 * Pointer to allocated iio_dev on success, NULL on failure.
1611 */
devm_iio_device_alloc(struct device * parent,int sizeof_priv)1612 struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv)
1613 {
1614 struct iio_dev **ptr, *iio_dev;
1615
1616 ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
1617 GFP_KERNEL);
1618 if (!ptr)
1619 return NULL;
1620
1621 iio_dev = iio_device_alloc(parent, sizeof_priv);
1622 if (iio_dev) {
1623 *ptr = iio_dev;
1624 devres_add(parent, ptr);
1625 } else {
1626 devres_free(ptr);
1627 }
1628
1629 return iio_dev;
1630 }
1631 EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1632
1633 /**
1634 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1635 * @inode: Inode structure for identifying the device in the file system
1636 * @filp: File structure for iio device used to keep and later access
1637 * private data
1638 *
1639 * Return: 0 on success or -EBUSY if the device is already opened
1640 **/
iio_chrdev_open(struct inode * inode,struct file * filp)1641 static int iio_chrdev_open(struct inode *inode, struct file *filp)
1642 {
1643 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1644 struct iio_dev, chrdev);
1645
1646 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1647 return -EBUSY;
1648
1649 iio_device_get(indio_dev);
1650
1651 filp->private_data = indio_dev;
1652
1653 return 0;
1654 }
1655
1656 /**
1657 * iio_chrdev_release() - chrdev file close buffer access and ioctls
1658 * @inode: Inode structure pointer for the char device
1659 * @filp: File structure pointer for the char device
1660 *
1661 * Return: 0 for successful release
1662 */
iio_chrdev_release(struct inode * inode,struct file * filp)1663 static int iio_chrdev_release(struct inode *inode, struct file *filp)
1664 {
1665 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1666 struct iio_dev, chrdev);
1667 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
1668 iio_device_put(indio_dev);
1669
1670 return 0;
1671 }
1672
1673 /* Somewhat of a cross file organization violation - ioctls here are actually
1674 * event related */
iio_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)1675 static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1676 {
1677 struct iio_dev *indio_dev = filp->private_data;
1678 int __user *ip = (int __user *)arg;
1679 int fd;
1680
1681 if (!indio_dev->info)
1682 return -ENODEV;
1683
1684 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1685 fd = iio_event_getfd(indio_dev);
1686 if (fd < 0)
1687 return fd;
1688 if (copy_to_user(ip, &fd, sizeof(fd)))
1689 return -EFAULT;
1690 return 0;
1691 }
1692 return -EINVAL;
1693 }
1694
1695 static const struct file_operations iio_buffer_fileops = {
1696 .read = iio_buffer_read_outer_addr,
1697 .release = iio_chrdev_release,
1698 .open = iio_chrdev_open,
1699 .poll = iio_buffer_poll_addr,
1700 .owner = THIS_MODULE,
1701 .llseek = noop_llseek,
1702 .unlocked_ioctl = iio_ioctl,
1703 .compat_ioctl = compat_ptr_ioctl,
1704 };
1705
iio_check_unique_scan_index(struct iio_dev * indio_dev)1706 static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
1707 {
1708 int i, j;
1709 const struct iio_chan_spec *channels = indio_dev->channels;
1710
1711 if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
1712 return 0;
1713
1714 for (i = 0; i < indio_dev->num_channels - 1; i++) {
1715 if (channels[i].scan_index < 0)
1716 continue;
1717 for (j = i + 1; j < indio_dev->num_channels; j++)
1718 if (channels[i].scan_index == channels[j].scan_index) {
1719 dev_err(&indio_dev->dev,
1720 "Duplicate scan index %d\n",
1721 channels[i].scan_index);
1722 return -EINVAL;
1723 }
1724 }
1725
1726 return 0;
1727 }
1728
1729 static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1730
__iio_device_register(struct iio_dev * indio_dev,struct module * this_mod)1731 int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod)
1732 {
1733 int ret;
1734
1735 if (!indio_dev->info)
1736 return -EINVAL;
1737
1738 indio_dev->driver_module = this_mod;
1739 /* If the calling driver did not initialize of_node, do it here */
1740 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1741 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1742
1743 indio_dev->label = of_get_property(indio_dev->dev.of_node, "label",
1744 NULL);
1745
1746 ret = iio_check_unique_scan_index(indio_dev);
1747 if (ret < 0)
1748 return ret;
1749
1750 /* configure elements for the chrdev */
1751 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
1752
1753 iio_device_register_debugfs(indio_dev);
1754
1755 ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
1756 if (ret) {
1757 dev_err(indio_dev->dev.parent,
1758 "Failed to create buffer sysfs interfaces\n");
1759 goto error_unreg_debugfs;
1760 }
1761
1762 ret = iio_device_register_sysfs(indio_dev);
1763 if (ret) {
1764 dev_err(indio_dev->dev.parent,
1765 "Failed to register sysfs interfaces\n");
1766 goto error_buffer_free_sysfs;
1767 }
1768 ret = iio_device_register_eventset(indio_dev);
1769 if (ret) {
1770 dev_err(indio_dev->dev.parent,
1771 "Failed to register event set\n");
1772 goto error_free_sysfs;
1773 }
1774 if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
1775 iio_device_register_trigger_consumer(indio_dev);
1776
1777 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1778 indio_dev->setup_ops == NULL)
1779 indio_dev->setup_ops = &noop_ring_setup_ops;
1780
1781 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1782
1783 indio_dev->chrdev.owner = this_mod;
1784
1785 ret = cdev_device_add(&indio_dev->chrdev, &indio_dev->dev);
1786 if (ret < 0)
1787 goto error_unreg_eventset;
1788
1789 return 0;
1790
1791 error_unreg_eventset:
1792 iio_device_unregister_eventset(indio_dev);
1793 error_free_sysfs:
1794 iio_device_unregister_sysfs(indio_dev);
1795 error_buffer_free_sysfs:
1796 iio_buffer_free_sysfs_and_mask(indio_dev);
1797 error_unreg_debugfs:
1798 iio_device_unregister_debugfs(indio_dev);
1799 return ret;
1800 }
1801 EXPORT_SYMBOL(__iio_device_register);
1802
1803 /**
1804 * iio_device_unregister() - unregister a device from the IIO subsystem
1805 * @indio_dev: Device structure representing the device.
1806 **/
iio_device_unregister(struct iio_dev * indio_dev)1807 void iio_device_unregister(struct iio_dev *indio_dev)
1808 {
1809 cdev_device_del(&indio_dev->chrdev, &indio_dev->dev);
1810
1811 mutex_lock(&indio_dev->info_exist_lock);
1812
1813 iio_device_unregister_debugfs(indio_dev);
1814
1815 iio_disable_all_buffers(indio_dev);
1816
1817 indio_dev->info = NULL;
1818
1819 iio_device_wakeup_eventset(indio_dev);
1820 iio_buffer_wakeup_poll(indio_dev);
1821
1822 mutex_unlock(&indio_dev->info_exist_lock);
1823
1824 iio_buffer_free_sysfs_and_mask(indio_dev);
1825 }
1826 EXPORT_SYMBOL(iio_device_unregister);
1827
devm_iio_device_unreg(struct device * dev,void * res)1828 static void devm_iio_device_unreg(struct device *dev, void *res)
1829 {
1830 iio_device_unregister(*(struct iio_dev **)res);
1831 }
1832
__devm_iio_device_register(struct device * dev,struct iio_dev * indio_dev,struct module * this_mod)1833 int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
1834 struct module *this_mod)
1835 {
1836 struct iio_dev **ptr;
1837 int ret;
1838
1839 ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
1840 if (!ptr)
1841 return -ENOMEM;
1842
1843 *ptr = indio_dev;
1844 ret = __iio_device_register(indio_dev, this_mod);
1845 if (!ret)
1846 devres_add(dev, ptr);
1847 else
1848 devres_free(ptr);
1849
1850 return ret;
1851 }
1852 EXPORT_SYMBOL_GPL(__devm_iio_device_register);
1853
1854 /**
1855 * iio_device_claim_direct_mode - Keep device in direct mode
1856 * @indio_dev: the iio_dev associated with the device
1857 *
1858 * If the device is in direct mode it is guaranteed to stay
1859 * that way until iio_device_release_direct_mode() is called.
1860 *
1861 * Use with iio_device_release_direct_mode()
1862 *
1863 * Returns: 0 on success, -EBUSY on failure
1864 */
iio_device_claim_direct_mode(struct iio_dev * indio_dev)1865 int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
1866 {
1867 mutex_lock(&indio_dev->mlock);
1868
1869 if (iio_buffer_enabled(indio_dev)) {
1870 mutex_unlock(&indio_dev->mlock);
1871 return -EBUSY;
1872 }
1873 return 0;
1874 }
1875 EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
1876
1877 /**
1878 * iio_device_release_direct_mode - releases claim on direct mode
1879 * @indio_dev: the iio_dev associated with the device
1880 *
1881 * Release the claim. Device is no longer guaranteed to stay
1882 * in direct mode.
1883 *
1884 * Use with iio_device_claim_direct_mode()
1885 */
iio_device_release_direct_mode(struct iio_dev * indio_dev)1886 void iio_device_release_direct_mode(struct iio_dev *indio_dev)
1887 {
1888 mutex_unlock(&indio_dev->mlock);
1889 }
1890 EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
1891
1892 subsys_initcall(iio_init);
1893 module_exit(iio_exit);
1894
1895 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
1896 MODULE_DESCRIPTION("Industrial I/O core");
1897 MODULE_LICENSE("GPL");
1898