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