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