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

source code of linux/drivers/iio/industrialio-core.c