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