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