Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* The industrial I/O core
0003  *
0004  * Copyright (c) 2008 Jonathan Cameron
0005  *
0006  * Based on elements of hwmon and input subsystems.
0007  */
0008 
0009 #define pr_fmt(fmt) "iio-core: " fmt
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/idr.h>
0014 #include <linux/kdev_t.h>
0015 #include <linux/err.h>
0016 #include <linux/device.h>
0017 #include <linux/fs.h>
0018 #include <linux/poll.h>
0019 #include <linux/property.h>
0020 #include <linux/sched.h>
0021 #include <linux/wait.h>
0022 #include <linux/cdev.h>
0023 #include <linux/slab.h>
0024 #include <linux/anon_inodes.h>
0025 #include <linux/debugfs.h>
0026 #include <linux/mutex.h>
0027 #include <linux/iio/iio.h>
0028 #include <linux/iio/iio-opaque.h>
0029 #include "iio_core.h"
0030 #include "iio_core_trigger.h"
0031 #include <linux/iio/sysfs.h>
0032 #include <linux/iio/events.h>
0033 #include <linux/iio/buffer.h>
0034 #include <linux/iio/buffer_impl.h>
0035 
0036 /* IDA to assign each registered device a unique id */
0037 static DEFINE_IDA(iio_ida);
0038 
0039 static dev_t iio_devt;
0040 
0041 #define IIO_DEV_MAX 256
0042 struct bus_type iio_bus_type = {
0043     .name = "iio",
0044 };
0045 EXPORT_SYMBOL(iio_bus_type);
0046 
0047 static struct dentry *iio_debugfs_dentry;
0048 
0049 static const char * const iio_direction[] = {
0050     [0] = "in",
0051     [1] = "out",
0052 };
0053 
0054 static const char * const iio_chan_type_name_spec[] = {
0055     [IIO_VOLTAGE] = "voltage",
0056     [IIO_CURRENT] = "current",
0057     [IIO_POWER] = "power",
0058     [IIO_ACCEL] = "accel",
0059     [IIO_ANGL_VEL] = "anglvel",
0060     [IIO_MAGN] = "magn",
0061     [IIO_LIGHT] = "illuminance",
0062     [IIO_INTENSITY] = "intensity",
0063     [IIO_PROXIMITY] = "proximity",
0064     [IIO_TEMP] = "temp",
0065     [IIO_INCLI] = "incli",
0066     [IIO_ROT] = "rot",
0067     [IIO_ANGL] = "angl",
0068     [IIO_TIMESTAMP] = "timestamp",
0069     [IIO_CAPACITANCE] = "capacitance",
0070     [IIO_ALTVOLTAGE] = "altvoltage",
0071     [IIO_CCT] = "cct",
0072     [IIO_PRESSURE] = "pressure",
0073     [IIO_HUMIDITYRELATIVE] = "humidityrelative",
0074     [IIO_ACTIVITY] = "activity",
0075     [IIO_STEPS] = "steps",
0076     [IIO_ENERGY] = "energy",
0077     [IIO_DISTANCE] = "distance",
0078     [IIO_VELOCITY] = "velocity",
0079     [IIO_CONCENTRATION] = "concentration",
0080     [IIO_RESISTANCE] = "resistance",
0081     [IIO_PH] = "ph",
0082     [IIO_UVINDEX] = "uvindex",
0083     [IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity",
0084     [IIO_COUNT] = "count",
0085     [IIO_INDEX] = "index",
0086     [IIO_GRAVITY]  = "gravity",
0087     [IIO_POSITIONRELATIVE]  = "positionrelative",
0088     [IIO_PHASE] = "phase",
0089     [IIO_MASSCONCENTRATION] = "massconcentration",
0090 };
0091 
0092 static const char * const iio_modifier_names[] = {
0093     [IIO_MOD_X] = "x",
0094     [IIO_MOD_Y] = "y",
0095     [IIO_MOD_Z] = "z",
0096     [IIO_MOD_X_AND_Y] = "x&y",
0097     [IIO_MOD_X_AND_Z] = "x&z",
0098     [IIO_MOD_Y_AND_Z] = "y&z",
0099     [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
0100     [IIO_MOD_X_OR_Y] = "x|y",
0101     [IIO_MOD_X_OR_Z] = "x|z",
0102     [IIO_MOD_Y_OR_Z] = "y|z",
0103     [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
0104     [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
0105     [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
0106     [IIO_MOD_LIGHT_BOTH] = "both",
0107     [IIO_MOD_LIGHT_IR] = "ir",
0108     [IIO_MOD_LIGHT_CLEAR] = "clear",
0109     [IIO_MOD_LIGHT_RED] = "red",
0110     [IIO_MOD_LIGHT_GREEN] = "green",
0111     [IIO_MOD_LIGHT_BLUE] = "blue",
0112     [IIO_MOD_LIGHT_UV] = "uv",
0113     [IIO_MOD_LIGHT_DUV] = "duv",
0114     [IIO_MOD_QUATERNION] = "quaternion",
0115     [IIO_MOD_TEMP_AMBIENT] = "ambient",
0116     [IIO_MOD_TEMP_OBJECT] = "object",
0117     [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
0118     [IIO_MOD_NORTH_TRUE] = "from_north_true",
0119     [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
0120     [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
0121     [IIO_MOD_RUNNING] = "running",
0122     [IIO_MOD_JOGGING] = "jogging",
0123     [IIO_MOD_WALKING] = "walking",
0124     [IIO_MOD_STILL] = "still",
0125     [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
0126     [IIO_MOD_I] = "i",
0127     [IIO_MOD_Q] = "q",
0128     [IIO_MOD_CO2] = "co2",
0129     [IIO_MOD_VOC] = "voc",
0130     [IIO_MOD_PM1] = "pm1",
0131     [IIO_MOD_PM2P5] = "pm2p5",
0132     [IIO_MOD_PM4] = "pm4",
0133     [IIO_MOD_PM10] = "pm10",
0134     [IIO_MOD_ETHANOL] = "ethanol",
0135     [IIO_MOD_H2] = "h2",
0136     [IIO_MOD_O2] = "o2",
0137 };
0138 
0139 /* relies on pairs of these shared then separate */
0140 static const char * const iio_chan_info_postfix[] = {
0141     [IIO_CHAN_INFO_RAW] = "raw",
0142     [IIO_CHAN_INFO_PROCESSED] = "input",
0143     [IIO_CHAN_INFO_SCALE] = "scale",
0144     [IIO_CHAN_INFO_OFFSET] = "offset",
0145     [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
0146     [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
0147     [IIO_CHAN_INFO_PEAK] = "peak_raw",
0148     [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
0149     [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
0150     [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
0151     [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
0152     = "filter_low_pass_3db_frequency",
0153     [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
0154     = "filter_high_pass_3db_frequency",
0155     [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
0156     [IIO_CHAN_INFO_FREQUENCY] = "frequency",
0157     [IIO_CHAN_INFO_PHASE] = "phase",
0158     [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
0159     [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
0160     [IIO_CHAN_INFO_HYSTERESIS_RELATIVE] = "hysteresis_relative",
0161     [IIO_CHAN_INFO_INT_TIME] = "integration_time",
0162     [IIO_CHAN_INFO_ENABLE] = "en",
0163     [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
0164     [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
0165     [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
0166     [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
0167     [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
0168     [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
0169     [IIO_CHAN_INFO_THERMOCOUPLE_TYPE] = "thermocouple_type",
0170     [IIO_CHAN_INFO_CALIBAMBIENT] = "calibambient",
0171 };
0172 /**
0173  * iio_device_id() - query the unique ID for the device
0174  * @indio_dev:      Device structure whose ID is being queried
0175  *
0176  * The IIO device ID is a unique index used for example for the naming
0177  * of the character device /dev/iio\:device[ID]
0178  */
0179 int iio_device_id(struct iio_dev *indio_dev)
0180 {
0181     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
0182 
0183     return iio_dev_opaque->id;
0184 }
0185 EXPORT_SYMBOL_GPL(iio_device_id);
0186 
0187 /**
0188  * iio_buffer_enabled() - helper function to test if the buffer is enabled
0189  * @indio_dev:      IIO device structure for device
0190  */
0191 bool iio_buffer_enabled(struct iio_dev *indio_dev)
0192 {
0193     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
0194 
0195     return iio_dev_opaque->currentmode
0196         & (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE |
0197            INDIO_BUFFER_SOFTWARE);
0198 }
0199 EXPORT_SYMBOL_GPL(iio_buffer_enabled);
0200 
0201 /**
0202  * iio_sysfs_match_string_with_gaps - matches given string in an array with gaps
0203  * @array: array of strings
0204  * @n: number of strings in the array
0205  * @str: string to match with
0206  *
0207  * Returns index of @str in the @array or -EINVAL, similar to match_string().
0208  * Uses sysfs_streq instead of strcmp for matching.
0209  *
0210  * This routine will look for a string in an array of strings.
0211  * The search will continue until the element is found or the n-th element
0212  * is reached, regardless of any NULL elements in the array.
0213  */
0214 static int iio_sysfs_match_string_with_gaps(const char * const *array, size_t n,
0215                         const char *str)
0216 {
0217     const char *item;
0218     int index;
0219 
0220     for (index = 0; index < n; index++) {
0221         item = array[index];
0222         if (!item)
0223             continue;
0224         if (sysfs_streq(item, str))
0225             return index;
0226     }
0227 
0228     return -EINVAL;
0229 }
0230 
0231 #if defined(CONFIG_DEBUG_FS)
0232 /*
0233  * There's also a CONFIG_DEBUG_FS guard in include/linux/iio/iio.h for
0234  * iio_get_debugfs_dentry() to make it inline if CONFIG_DEBUG_FS is undefined
0235  */
0236 struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
0237 {
0238     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
0239     return iio_dev_opaque->debugfs_dentry;
0240 }
0241 EXPORT_SYMBOL_GPL(iio_get_debugfs_dentry);
0242 #endif
0243 
0244 /**
0245  * iio_find_channel_from_si() - get channel from its scan index
0246  * @indio_dev:      device
0247  * @si:         scan index to match
0248  */
0249 const struct iio_chan_spec
0250 *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
0251 {
0252     int i;
0253 
0254     for (i = 0; i < indio_dev->num_channels; i++)
0255         if (indio_dev->channels[i].scan_index == si)
0256             return &indio_dev->channels[i];
0257     return NULL;
0258 }
0259 
0260 /* This turns up an awful lot */
0261 ssize_t iio_read_const_attr(struct device *dev,
0262                 struct device_attribute *attr,
0263                 char *buf)
0264 {
0265     return sysfs_emit(buf, "%s\n", to_iio_const_attr(attr)->string);
0266 }
0267 EXPORT_SYMBOL(iio_read_const_attr);
0268 
0269 /**
0270  * iio_device_set_clock() - Set current timestamping clock for the device
0271  * @indio_dev: IIO device structure containing the device
0272  * @clock_id: timestamping clock posix identifier to set.
0273  */
0274 int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id)
0275 {
0276     int ret;
0277     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
0278     const struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
0279 
0280     ret = mutex_lock_interruptible(&indio_dev->mlock);
0281     if (ret)
0282         return ret;
0283     if ((ev_int && iio_event_enabled(ev_int)) ||
0284         iio_buffer_enabled(indio_dev)) {
0285         mutex_unlock(&indio_dev->mlock);
0286         return -EBUSY;
0287     }
0288     iio_dev_opaque->clock_id = clock_id;
0289     mutex_unlock(&indio_dev->mlock);
0290 
0291     return 0;
0292 }
0293 EXPORT_SYMBOL(iio_device_set_clock);
0294 
0295 /**
0296  * iio_device_get_clock() - Retrieve current timestamping clock for the device
0297  * @indio_dev: IIO device structure containing the device
0298  */
0299 clockid_t iio_device_get_clock(const struct iio_dev *indio_dev)
0300 {
0301     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
0302 
0303     return iio_dev_opaque->clock_id;
0304 }
0305 EXPORT_SYMBOL(iio_device_get_clock);
0306 
0307 /**
0308  * iio_get_time_ns() - utility function to get a time stamp for events etc
0309  * @indio_dev: device
0310  */
0311 s64 iio_get_time_ns(const struct iio_dev *indio_dev)
0312 {
0313     struct timespec64 tp;
0314 
0315     switch (iio_device_get_clock(indio_dev)) {
0316     case CLOCK_REALTIME:
0317         return ktime_get_real_ns();
0318     case CLOCK_MONOTONIC:
0319         return ktime_get_ns();
0320     case CLOCK_MONOTONIC_RAW:
0321         return ktime_get_raw_ns();
0322     case CLOCK_REALTIME_COARSE:
0323         return ktime_to_ns(ktime_get_coarse_real());
0324     case CLOCK_MONOTONIC_COARSE:
0325         ktime_get_coarse_ts64(&tp);
0326         return timespec64_to_ns(&tp);
0327     case CLOCK_BOOTTIME:
0328         return ktime_get_boottime_ns();
0329     case CLOCK_TAI:
0330         return ktime_get_clocktai_ns();
0331     default:
0332         BUG();
0333     }
0334 }
0335 EXPORT_SYMBOL(iio_get_time_ns);
0336 
0337 static int __init iio_init(void)
0338 {
0339     int ret;
0340 
0341     /* Register sysfs bus */
0342     ret  = bus_register(&iio_bus_type);
0343     if (ret < 0) {
0344         pr_err("could not register bus type\n");
0345         goto error_nothing;
0346     }
0347 
0348     ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
0349     if (ret < 0) {
0350         pr_err("failed to allocate char dev region\n");
0351         goto error_unregister_bus_type;
0352     }
0353 
0354     iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
0355 
0356     return 0;
0357 
0358 error_unregister_bus_type:
0359     bus_unregister(&iio_bus_type);
0360 error_nothing:
0361     return ret;
0362 }
0363 
0364 static void __exit iio_exit(void)
0365 {
0366     if (iio_devt)
0367         unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
0368     bus_unregister(&iio_bus_type);
0369     debugfs_remove(iio_debugfs_dentry);
0370 }
0371 
0372 #if defined(CONFIG_DEBUG_FS)
0373 static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
0374                   size_t count, loff_t *ppos)
0375 {
0376     struct iio_dev *indio_dev = file->private_data;
0377     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
0378     unsigned int val = 0;
0379     int ret;
0380 
0381     if (*ppos > 0)
0382         return simple_read_from_buffer(userbuf, count, ppos,
0383                            iio_dev_opaque->read_buf,
0384                            iio_dev_opaque->read_buf_len);
0385 
0386     ret = indio_dev->info->debugfs_reg_access(indio_dev,
0387                           iio_dev_opaque->cached_reg_addr,
0388                           0, &val);
0389     if (ret) {
0390         dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
0391         return ret;
0392     }
0393 
0394     iio_dev_opaque->read_buf_len = snprintf(iio_dev_opaque->read_buf,
0395                           sizeof(iio_dev_opaque->read_buf),
0396                           "0x%X\n", val);
0397 
0398     return simple_read_from_buffer(userbuf, count, ppos,
0399                        iio_dev_opaque->read_buf,
0400                        iio_dev_opaque->read_buf_len);
0401 }
0402 
0403 static ssize_t iio_debugfs_write_reg(struct file *file,
0404              const char __user *userbuf, size_t count, loff_t *ppos)
0405 {
0406     struct iio_dev *indio_dev = file->private_data;
0407     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
0408     unsigned int reg, val;
0409     char buf[80];
0410     int ret;
0411 
0412     count = min_t(size_t, count, (sizeof(buf)-1));
0413     if (copy_from_user(buf, userbuf, count))
0414         return -EFAULT;
0415 
0416     buf[count] = 0;
0417 
0418     ret = sscanf(buf, "%i %i", &reg, &val);
0419 
0420     switch (ret) {
0421     case 1:
0422         iio_dev_opaque->cached_reg_addr = reg;
0423         break;
0424     case 2:
0425         iio_dev_opaque->cached_reg_addr = reg;
0426         ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
0427                               val, NULL);
0428         if (ret) {
0429             dev_err(indio_dev->dev.parent, "%s: write failed\n",
0430                 __func__);
0431             return ret;
0432         }
0433         break;
0434     default:
0435         return -EINVAL;
0436     }
0437 
0438     return count;
0439 }
0440 
0441 static const struct file_operations iio_debugfs_reg_fops = {
0442     .open = simple_open,
0443     .read = iio_debugfs_read_reg,
0444     .write = iio_debugfs_write_reg,
0445 };
0446 
0447 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
0448 {
0449     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
0450     debugfs_remove_recursive(iio_dev_opaque->debugfs_dentry);
0451 }
0452 
0453 static void iio_device_register_debugfs(struct iio_dev *indio_dev)
0454 {
0455     struct iio_dev_opaque *iio_dev_opaque;
0456 
0457     if (indio_dev->info->debugfs_reg_access == NULL)
0458         return;
0459 
0460     if (!iio_debugfs_dentry)
0461         return;
0462 
0463     iio_dev_opaque = to_iio_dev_opaque(indio_dev);
0464 
0465     iio_dev_opaque->debugfs_dentry =
0466         debugfs_create_dir(dev_name(&indio_dev->dev),
0467                    iio_debugfs_dentry);
0468 
0469     debugfs_create_file("direct_reg_access", 0644,
0470                 iio_dev_opaque->debugfs_dentry, indio_dev,
0471                 &iio_debugfs_reg_fops);
0472 }
0473 #else
0474 static void iio_device_register_debugfs(struct iio_dev *indio_dev)
0475 {
0476 }
0477 
0478 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
0479 {
0480 }
0481 #endif /* CONFIG_DEBUG_FS */
0482 
0483 static ssize_t iio_read_channel_ext_info(struct device *dev,
0484                      struct device_attribute *attr,
0485                      char *buf)
0486 {
0487     struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0488     struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
0489     const struct iio_chan_spec_ext_info *ext_info;
0490 
0491     ext_info = &this_attr->c->ext_info[this_attr->address];
0492 
0493     return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
0494 }
0495 
0496 static ssize_t iio_write_channel_ext_info(struct device *dev,
0497                      struct device_attribute *attr,
0498                      const char *buf,
0499                      size_t len)
0500 {
0501     struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0502     struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
0503     const struct iio_chan_spec_ext_info *ext_info;
0504 
0505     ext_info = &this_attr->c->ext_info[this_attr->address];
0506 
0507     return ext_info->write(indio_dev, ext_info->private,
0508                    this_attr->c, buf, len);
0509 }
0510 
0511 ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
0512     uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
0513 {
0514     const struct iio_enum *e = (const struct iio_enum *)priv;
0515     unsigned int i;
0516     size_t len = 0;
0517 
0518     if (!e->num_items)
0519         return 0;
0520 
0521     for (i = 0; i < e->num_items; ++i) {
0522         if (!e->items[i])
0523             continue;
0524         len += sysfs_emit_at(buf, len, "%s ", e->items[i]);
0525     }
0526 
0527     /* replace last space with a newline */
0528     buf[len - 1] = '\n';
0529 
0530     return len;
0531 }
0532 EXPORT_SYMBOL_GPL(iio_enum_available_read);
0533 
0534 ssize_t iio_enum_read(struct iio_dev *indio_dev,
0535     uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
0536 {
0537     const struct iio_enum *e = (const struct iio_enum *)priv;
0538     int i;
0539 
0540     if (!e->get)
0541         return -EINVAL;
0542 
0543     i = e->get(indio_dev, chan);
0544     if (i < 0)
0545         return i;
0546     else if (i >= e->num_items || !e->items[i])
0547         return -EINVAL;
0548 
0549     return sysfs_emit(buf, "%s\n", e->items[i]);
0550 }
0551 EXPORT_SYMBOL_GPL(iio_enum_read);
0552 
0553 ssize_t iio_enum_write(struct iio_dev *indio_dev,
0554     uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
0555     size_t len)
0556 {
0557     const struct iio_enum *e = (const struct iio_enum *)priv;
0558     int ret;
0559 
0560     if (!e->set)
0561         return -EINVAL;
0562 
0563     ret = iio_sysfs_match_string_with_gaps(e->items, e->num_items, buf);
0564     if (ret < 0)
0565         return ret;
0566 
0567     ret = e->set(indio_dev, chan, ret);
0568     return ret ? ret : len;
0569 }
0570 EXPORT_SYMBOL_GPL(iio_enum_write);
0571 
0572 static const struct iio_mount_matrix iio_mount_idmatrix = {
0573     .rotation = {
0574         "1", "0", "0",
0575         "0", "1", "0",
0576         "0", "0", "1"
0577     }
0578 };
0579 
0580 static int iio_setup_mount_idmatrix(const struct device *dev,
0581                     struct iio_mount_matrix *matrix)
0582 {
0583     *matrix = iio_mount_idmatrix;
0584     dev_info(dev, "mounting matrix not found: using identity...\n");
0585     return 0;
0586 }
0587 
0588 ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
0589                   const struct iio_chan_spec *chan, char *buf)
0590 {
0591     const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *)
0592                           priv)(indio_dev, chan);
0593 
0594     if (IS_ERR(mtx))
0595         return PTR_ERR(mtx);
0596 
0597     if (!mtx)
0598         mtx = &iio_mount_idmatrix;
0599 
0600     return sysfs_emit(buf, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n",
0601               mtx->rotation[0], mtx->rotation[1], mtx->rotation[2],
0602               mtx->rotation[3], mtx->rotation[4], mtx->rotation[5],
0603               mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]);
0604 }
0605 EXPORT_SYMBOL_GPL(iio_show_mount_matrix);
0606 
0607 /**
0608  * iio_read_mount_matrix() - retrieve iio device mounting matrix from
0609  *                           device "mount-matrix" property
0610  * @dev:    device the mounting matrix property is assigned to
0611  * @matrix: where to store retrieved matrix
0612  *
0613  * If device is assigned no mounting matrix property, a default 3x3 identity
0614  * matrix will be filled in.
0615  *
0616  * Return: 0 if success, or a negative error code on failure.
0617  */
0618 int iio_read_mount_matrix(struct device *dev, struct iio_mount_matrix *matrix)
0619 {
0620     size_t len = ARRAY_SIZE(iio_mount_idmatrix.rotation);
0621     int err;
0622 
0623     err = device_property_read_string_array(dev, "mount-matrix", matrix->rotation, len);
0624     if (err == len)
0625         return 0;
0626 
0627     if (err >= 0)
0628         /* Invalid number of matrix entries. */
0629         return -EINVAL;
0630 
0631     if (err != -EINVAL)
0632         /* Invalid matrix declaration format. */
0633         return err;
0634 
0635     /* Matrix was not declared at all: fallback to identity. */
0636     return iio_setup_mount_idmatrix(dev, matrix);
0637 }
0638 EXPORT_SYMBOL(iio_read_mount_matrix);
0639 
0640 static ssize_t __iio_format_value(char *buf, size_t offset, unsigned int type,
0641                   int size, const int *vals)
0642 {
0643     int tmp0, tmp1;
0644     s64 tmp2;
0645     bool scale_db = false;
0646 
0647     switch (type) {
0648     case IIO_VAL_INT:
0649         return sysfs_emit_at(buf, offset, "%d", vals[0]);
0650     case IIO_VAL_INT_PLUS_MICRO_DB:
0651         scale_db = true;
0652         fallthrough;
0653     case IIO_VAL_INT_PLUS_MICRO:
0654         if (vals[1] < 0)
0655             return sysfs_emit_at(buf, offset, "-%d.%06u%s",
0656                          abs(vals[0]), -vals[1],
0657                          scale_db ? " dB" : "");
0658         else
0659             return sysfs_emit_at(buf, offset, "%d.%06u%s", vals[0],
0660                          vals[1], scale_db ? " dB" : "");
0661     case IIO_VAL_INT_PLUS_NANO:
0662         if (vals[1] < 0)
0663             return sysfs_emit_at(buf, offset, "-%d.%09u",
0664                          abs(vals[0]), -vals[1]);
0665         else
0666             return sysfs_emit_at(buf, offset, "%d.%09u", vals[0],
0667                          vals[1]);
0668     case IIO_VAL_FRACTIONAL:
0669         tmp2 = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
0670         tmp1 = vals[1];
0671         tmp0 = (int)div_s64_rem(tmp2, 1000000000, &tmp1);
0672         if ((tmp2 < 0) && (tmp0 == 0))
0673             return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1));
0674         else
0675             return sysfs_emit_at(buf, offset, "%d.%09u", tmp0,
0676                          abs(tmp1));
0677     case IIO_VAL_FRACTIONAL_LOG2:
0678         tmp2 = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
0679         tmp0 = (int)div_s64_rem(tmp2, 1000000000LL, &tmp1);
0680         if (tmp0 == 0 && tmp2 < 0)
0681             return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1));
0682         else
0683             return sysfs_emit_at(buf, offset, "%d.%09u", tmp0,
0684                          abs(tmp1));
0685     case IIO_VAL_INT_MULTIPLE:
0686     {
0687         int i;
0688         int l = 0;
0689 
0690         for (i = 0; i < size; ++i)
0691             l += sysfs_emit_at(buf, offset + l, "%d ", vals[i]);
0692         return l;
0693     }
0694     case IIO_VAL_CHAR:
0695         return sysfs_emit_at(buf, offset, "%c", (char)vals[0]);
0696     case IIO_VAL_INT_64:
0697         tmp2 = (s64)((((u64)vals[1]) << 32) | (u32)vals[0]);
0698         return sysfs_emit_at(buf, offset, "%lld", tmp2);
0699     default:
0700         return 0;
0701     }
0702 }
0703 
0704 /**
0705  * iio_format_value() - Formats a IIO value into its string representation
0706  * @buf:    The buffer to which the formatted value gets written
0707  *      which is assumed to be big enough (i.e. PAGE_SIZE).
0708  * @type:   One of the IIO_VAL_* constants. This decides how the val
0709  *      and val2 parameters are formatted.
0710  * @size:   Number of IIO value entries contained in vals
0711  * @vals:   Pointer to the values, exact meaning depends on the
0712  *      type parameter.
0713  *
0714  * Return: 0 by default, a negative number on failure or the
0715  *     total number of characters written for a type that belongs
0716  *     to the IIO_VAL_* constant.
0717  */
0718 ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
0719 {
0720     ssize_t len;
0721 
0722     len = __iio_format_value(buf, 0, type, size, vals);
0723     if (len >= PAGE_SIZE - 1)
0724         return -EFBIG;
0725 
0726     return len + sysfs_emit_at(buf, len, "\n");
0727 }
0728 EXPORT_SYMBOL_GPL(iio_format_value);
0729 
0730 static ssize_t iio_read_channel_label(struct device *dev,
0731                       struct device_attribute *attr,
0732                       char *buf)
0733 {
0734     struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0735     struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
0736 
0737     if (indio_dev->info->read_label)
0738         return indio_dev->info->read_label(indio_dev, this_attr->c, buf);
0739 
0740     if (this_attr->c->extend_name)
0741         return sysfs_emit(buf, "%s\n", this_attr->c->extend_name);
0742 
0743     return -EINVAL;
0744 }
0745 
0746 static ssize_t iio_read_channel_info(struct device *dev,
0747                      struct device_attribute *attr,
0748                      char *buf)
0749 {
0750     struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0751     struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
0752     int vals[INDIO_MAX_RAW_ELEMENTS];
0753     int ret;
0754     int val_len = 2;
0755 
0756     if (indio_dev->info->read_raw_multi)
0757         ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
0758                             INDIO_MAX_RAW_ELEMENTS,
0759                             vals, &val_len,
0760                             this_attr->address);
0761     else
0762         ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
0763                     &vals[0], &vals[1], this_attr->address);
0764 
0765     if (ret < 0)
0766         return ret;
0767 
0768     return iio_format_value(buf, ret, val_len, vals);
0769 }
0770 
0771 static ssize_t iio_format_list(char *buf, const int *vals, int type, int length,
0772                    const char *prefix, const char *suffix)
0773 {
0774     ssize_t len;
0775     int stride;
0776     int i;
0777 
0778     switch (type) {
0779     case IIO_VAL_INT:
0780         stride = 1;
0781         break;
0782     default:
0783         stride = 2;
0784         break;
0785     }
0786 
0787     len = sysfs_emit(buf, prefix);
0788 
0789     for (i = 0; i <= length - stride; i += stride) {
0790         if (i != 0) {
0791             len += sysfs_emit_at(buf, len, " ");
0792             if (len >= PAGE_SIZE)
0793                 return -EFBIG;
0794         }
0795 
0796         len += __iio_format_value(buf, len, type, stride, &vals[i]);
0797         if (len >= PAGE_SIZE)
0798             return -EFBIG;
0799     }
0800 
0801     len += sysfs_emit_at(buf, len, "%s\n", suffix);
0802 
0803     return len;
0804 }
0805 
0806 static ssize_t iio_format_avail_list(char *buf, const int *vals,
0807                      int type, int length)
0808 {
0809 
0810     return iio_format_list(buf, vals, type, length, "", "");
0811 }
0812 
0813 static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
0814 {
0815     int length;
0816 
0817     /*
0818      * length refers to the array size , not the number of elements.
0819      * The purpose is to print the range [min , step ,max] so length should
0820      * be 3 in case of int, and 6 for other types.
0821      */
0822     switch (type) {
0823     case IIO_VAL_INT:
0824         length = 3;
0825         break;
0826     default:
0827         length = 6;
0828         break;
0829     }
0830 
0831     return iio_format_list(buf, vals, type, length, "[", "]");
0832 }
0833 
0834 static ssize_t iio_read_channel_info_avail(struct device *dev,
0835                        struct device_attribute *attr,
0836                        char *buf)
0837 {
0838     struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0839     struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
0840     const int *vals;
0841     int ret;
0842     int length;
0843     int type;
0844 
0845     ret = indio_dev->info->read_avail(indio_dev, this_attr->c,
0846                       &vals, &type, &length,
0847                       this_attr->address);
0848 
0849     if (ret < 0)
0850         return ret;
0851     switch (ret) {
0852     case IIO_AVAIL_LIST:
0853         return iio_format_avail_list(buf, vals, type, length);
0854     case IIO_AVAIL_RANGE:
0855         return iio_format_avail_range(buf, vals, type);
0856     default:
0857         return -EINVAL;
0858     }
0859 }
0860 
0861 /**
0862  * __iio_str_to_fixpoint() - Parse a fixed-point number from a string
0863  * @str: The string to parse
0864  * @fract_mult: Multiplier for the first decimal place, should be a power of 10
0865  * @integer: The integer part of the number
0866  * @fract: The fractional part of the number
0867  * @scale_db: True if this should parse as dB
0868  *
0869  * Returns 0 on success, or a negative error code if the string could not be
0870  * parsed.
0871  */
0872 static int __iio_str_to_fixpoint(const char *str, int fract_mult,
0873                  int *integer, int *fract, bool scale_db)
0874 {
0875     int i = 0, f = 0;
0876     bool integer_part = true, negative = false;
0877 
0878     if (fract_mult == 0) {
0879         *fract = 0;
0880 
0881         return kstrtoint(str, 0, integer);
0882     }
0883 
0884     if (str[0] == '-') {
0885         negative = true;
0886         str++;
0887     } else if (str[0] == '+') {
0888         str++;
0889     }
0890 
0891     while (*str) {
0892         if ('0' <= *str && *str <= '9') {
0893             if (integer_part) {
0894                 i = i * 10 + *str - '0';
0895             } else {
0896                 f += fract_mult * (*str - '0');
0897                 fract_mult /= 10;
0898             }
0899         } else if (*str == '\n') {
0900             if (*(str + 1) == '\0')
0901                 break;
0902             return -EINVAL;
0903         } else if (!strncmp(str, " dB", sizeof(" dB") - 1) && scale_db) {
0904             /* Ignore the dB suffix */
0905             str += sizeof(" dB") - 1;
0906             continue;
0907         } else if (!strncmp(str, "dB", sizeof("dB") - 1) && scale_db) {
0908             /* Ignore the dB suffix */
0909             str += sizeof("dB") - 1;
0910             continue;
0911         } else if (*str == '.' && integer_part) {
0912             integer_part = false;
0913         } else {
0914             return -EINVAL;
0915         }
0916         str++;
0917     }
0918 
0919     if (negative) {
0920         if (i)
0921             i = -i;
0922         else
0923             f = -f;
0924     }
0925 
0926     *integer = i;
0927     *fract = f;
0928 
0929     return 0;
0930 }
0931 
0932 /**
0933  * iio_str_to_fixpoint() - Parse a fixed-point number from a string
0934  * @str: The string to parse
0935  * @fract_mult: Multiplier for the first decimal place, should be a power of 10
0936  * @integer: The integer part of the number
0937  * @fract: The fractional part of the number
0938  *
0939  * Returns 0 on success, or a negative error code if the string could not be
0940  * parsed.
0941  */
0942 int iio_str_to_fixpoint(const char *str, int fract_mult,
0943             int *integer, int *fract)
0944 {
0945     return __iio_str_to_fixpoint(str, fract_mult, integer, fract, false);
0946 }
0947 EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
0948 
0949 static ssize_t iio_write_channel_info(struct device *dev,
0950                       struct device_attribute *attr,
0951                       const char *buf,
0952                       size_t len)
0953 {
0954     struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0955     struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
0956     int ret, fract_mult = 100000;
0957     int integer, fract = 0;
0958     bool is_char = false;
0959     bool scale_db = false;
0960 
0961     /* Assumes decimal - precision based on number of digits */
0962     if (!indio_dev->info->write_raw)
0963         return -EINVAL;
0964 
0965     if (indio_dev->info->write_raw_get_fmt)
0966         switch (indio_dev->info->write_raw_get_fmt(indio_dev,
0967             this_attr->c, this_attr->address)) {
0968         case IIO_VAL_INT:
0969             fract_mult = 0;
0970             break;
0971         case IIO_VAL_INT_PLUS_MICRO_DB:
0972             scale_db = true;
0973             fallthrough;
0974         case IIO_VAL_INT_PLUS_MICRO:
0975             fract_mult = 100000;
0976             break;
0977         case IIO_VAL_INT_PLUS_NANO:
0978             fract_mult = 100000000;
0979             break;
0980         case IIO_VAL_CHAR:
0981             is_char = true;
0982             break;
0983         default:
0984             return -EINVAL;
0985         }
0986 
0987     if (is_char) {
0988         char ch;
0989 
0990         if (sscanf(buf, "%c", &ch) != 1)
0991             return -EINVAL;
0992         integer = ch;
0993     } else {
0994         ret = __iio_str_to_fixpoint(buf, fract_mult, &integer, &fract,
0995                         scale_db);
0996         if (ret)
0997             return ret;
0998     }
0999 
1000     ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
1001                      integer, fract, this_attr->address);
1002     if (ret)
1003         return ret;
1004 
1005     return len;
1006 }
1007 
1008 static
1009 int __iio_device_attr_init(struct device_attribute *dev_attr,
1010                const char *postfix,
1011                struct iio_chan_spec const *chan,
1012                ssize_t (*readfunc)(struct device *dev,
1013                            struct device_attribute *attr,
1014                            char *buf),
1015                ssize_t (*writefunc)(struct device *dev,
1016                         struct device_attribute *attr,
1017                         const char *buf,
1018                         size_t len),
1019                enum iio_shared_by shared_by)
1020 {
1021     int ret = 0;
1022     char *name = NULL;
1023     char *full_postfix;
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, "%s_%s_%s",
1030                          iio_modifier_names[chan
1031                                     ->channel2],
1032                          chan->extend_name,
1033                          postfix);
1034         else
1035             full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
1036                          iio_modifier_names[chan
1037                                     ->channel2],
1038                          postfix);
1039     } else {
1040         if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
1041             full_postfix = kstrdup(postfix, GFP_KERNEL);
1042         else
1043             full_postfix = kasprintf(GFP_KERNEL,
1044                          "%s_%s",
1045                          chan->extend_name,
1046                          postfix);
1047     }
1048     if (full_postfix == NULL)
1049         return -ENOMEM;
1050 
1051     if (chan->differential) { /* Differential can not have modifier */
1052         switch (shared_by) {
1053         case IIO_SHARED_BY_ALL:
1054             name = kasprintf(GFP_KERNEL, "%s", full_postfix);
1055             break;
1056         case IIO_SHARED_BY_DIR:
1057             name = kasprintf(GFP_KERNEL, "%s_%s",
1058                         iio_direction[chan->output],
1059                         full_postfix);
1060             break;
1061         case IIO_SHARED_BY_TYPE:
1062             name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
1063                         iio_direction[chan->output],
1064                         iio_chan_type_name_spec[chan->type],
1065                         iio_chan_type_name_spec[chan->type],
1066                         full_postfix);
1067             break;
1068         case IIO_SEPARATE:
1069             if (!chan->indexed) {
1070                 WARN(1, "Differential channels must be indexed\n");
1071                 ret = -EINVAL;
1072                 goto error_free_full_postfix;
1073             }
1074             name = kasprintf(GFP_KERNEL,
1075                         "%s_%s%d-%s%d_%s",
1076                         iio_direction[chan->output],
1077                         iio_chan_type_name_spec[chan->type],
1078                         chan->channel,
1079                         iio_chan_type_name_spec[chan->type],
1080                         chan->channel2,
1081                         full_postfix);
1082             break;
1083         }
1084     } else { /* Single ended */
1085         switch (shared_by) {
1086         case IIO_SHARED_BY_ALL:
1087             name = kasprintf(GFP_KERNEL, "%s", full_postfix);
1088             break;
1089         case IIO_SHARED_BY_DIR:
1090             name = kasprintf(GFP_KERNEL, "%s_%s",
1091                         iio_direction[chan->output],
1092                         full_postfix);
1093             break;
1094         case IIO_SHARED_BY_TYPE:
1095             name = kasprintf(GFP_KERNEL, "%s_%s_%s",
1096                         iio_direction[chan->output],
1097                         iio_chan_type_name_spec[chan->type],
1098                         full_postfix);
1099             break;
1100 
1101         case IIO_SEPARATE:
1102             if (chan->indexed)
1103                 name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
1104                             iio_direction[chan->output],
1105                             iio_chan_type_name_spec[chan->type],
1106                             chan->channel,
1107                             full_postfix);
1108             else
1109                 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
1110                             iio_direction[chan->output],
1111                             iio_chan_type_name_spec[chan->type],
1112                             full_postfix);
1113             break;
1114         }
1115     }
1116     if (name == NULL) {
1117         ret = -ENOMEM;
1118         goto error_free_full_postfix;
1119     }
1120     dev_attr->attr.name = name;
1121 
1122     if (readfunc) {
1123         dev_attr->attr.mode |= 0444;
1124         dev_attr->show = readfunc;
1125     }
1126 
1127     if (writefunc) {
1128         dev_attr->attr.mode |= 0200;
1129         dev_attr->store = writefunc;
1130     }
1131 
1132 error_free_full_postfix:
1133     kfree(full_postfix);
1134 
1135     return ret;
1136 }
1137 
1138 static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
1139 {
1140     kfree(dev_attr->attr.name);
1141 }
1142 
1143 int __iio_add_chan_devattr(const char *postfix,
1144                struct iio_chan_spec const *chan,
1145                ssize_t (*readfunc)(struct device *dev,
1146                            struct device_attribute *attr,
1147                            char *buf),
1148                ssize_t (*writefunc)(struct device *dev,
1149                         struct device_attribute *attr,
1150                         const char *buf,
1151                         size_t len),
1152                u64 mask,
1153                enum iio_shared_by shared_by,
1154                struct device *dev,
1155                struct iio_buffer *buffer,
1156                struct list_head *attr_list)
1157 {
1158     int ret;
1159     struct iio_dev_attr *iio_attr, *t;
1160 
1161     iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
1162     if (iio_attr == NULL)
1163         return -ENOMEM;
1164     ret = __iio_device_attr_init(&iio_attr->dev_attr,
1165                      postfix, chan,
1166                      readfunc, writefunc, shared_by);
1167     if (ret)
1168         goto error_iio_dev_attr_free;
1169     iio_attr->c = chan;
1170     iio_attr->address = mask;
1171     iio_attr->buffer = buffer;
1172     list_for_each_entry(t, attr_list, l)
1173         if (strcmp(t->dev_attr.attr.name,
1174                iio_attr->dev_attr.attr.name) == 0) {
1175             if (shared_by == IIO_SEPARATE)
1176                 dev_err(dev, "tried to double register : %s\n",
1177                     t->dev_attr.attr.name);
1178             ret = -EBUSY;
1179             goto error_device_attr_deinit;
1180         }
1181     list_add(&iio_attr->l, attr_list);
1182 
1183     return 0;
1184 
1185 error_device_attr_deinit:
1186     __iio_device_attr_deinit(&iio_attr->dev_attr);
1187 error_iio_dev_attr_free:
1188     kfree(iio_attr);
1189     return ret;
1190 }
1191 
1192 static int iio_device_add_channel_label(struct iio_dev *indio_dev,
1193                      struct iio_chan_spec const *chan)
1194 {
1195     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1196     int ret;
1197 
1198     if (!indio_dev->info->read_label && !chan->extend_name)
1199         return 0;
1200 
1201     ret = __iio_add_chan_devattr("label",
1202                      chan,
1203                      &iio_read_channel_label,
1204                      NULL,
1205                      0,
1206                      IIO_SEPARATE,
1207                      &indio_dev->dev,
1208                      NULL,
1209                      &iio_dev_opaque->channel_attr_list);
1210     if (ret < 0)
1211         return ret;
1212 
1213     return 1;
1214 }
1215 
1216 static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
1217                      struct iio_chan_spec const *chan,
1218                      enum iio_shared_by shared_by,
1219                      const long *infomask)
1220 {
1221     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1222     int i, ret, attrcount = 0;
1223 
1224     for_each_set_bit(i, infomask, sizeof(*infomask)*8) {
1225         if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1226             return -EINVAL;
1227         ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
1228                          chan,
1229                          &iio_read_channel_info,
1230                          &iio_write_channel_info,
1231                          i,
1232                          shared_by,
1233                          &indio_dev->dev,
1234                          NULL,
1235                          &iio_dev_opaque->channel_attr_list);
1236         if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
1237             continue;
1238         else if (ret < 0)
1239             return ret;
1240         attrcount++;
1241     }
1242 
1243     return attrcount;
1244 }
1245 
1246 static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev,
1247                            struct iio_chan_spec const *chan,
1248                            enum iio_shared_by shared_by,
1249                            const long *infomask)
1250 {
1251     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1252     int i, ret, attrcount = 0;
1253     char *avail_postfix;
1254 
1255     for_each_set_bit(i, infomask, sizeof(*infomask) * 8) {
1256         if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1257             return -EINVAL;
1258         avail_postfix = kasprintf(GFP_KERNEL,
1259                       "%s_available",
1260                       iio_chan_info_postfix[i]);
1261         if (!avail_postfix)
1262             return -ENOMEM;
1263 
1264         ret = __iio_add_chan_devattr(avail_postfix,
1265                          chan,
1266                          &iio_read_channel_info_avail,
1267                          NULL,
1268                          i,
1269                          shared_by,
1270                          &indio_dev->dev,
1271                          NULL,
1272                          &iio_dev_opaque->channel_attr_list);
1273         kfree(avail_postfix);
1274         if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
1275             continue;
1276         else if (ret < 0)
1277             return ret;
1278         attrcount++;
1279     }
1280 
1281     return attrcount;
1282 }
1283 
1284 static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
1285                     struct iio_chan_spec const *chan)
1286 {
1287     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1288     int ret, attrcount = 0;
1289     const struct iio_chan_spec_ext_info *ext_info;
1290 
1291     if (chan->channel < 0)
1292         return 0;
1293     ret = iio_device_add_info_mask_type(indio_dev, chan,
1294                         IIO_SEPARATE,
1295                         &chan->info_mask_separate);
1296     if (ret < 0)
1297         return ret;
1298     attrcount += ret;
1299 
1300     ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1301                           IIO_SEPARATE,
1302                           &chan->
1303                           info_mask_separate_available);
1304     if (ret < 0)
1305         return ret;
1306     attrcount += ret;
1307 
1308     ret = iio_device_add_info_mask_type(indio_dev, chan,
1309                         IIO_SHARED_BY_TYPE,
1310                         &chan->info_mask_shared_by_type);
1311     if (ret < 0)
1312         return ret;
1313     attrcount += ret;
1314 
1315     ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1316                           IIO_SHARED_BY_TYPE,
1317                           &chan->
1318                           info_mask_shared_by_type_available);
1319     if (ret < 0)
1320         return ret;
1321     attrcount += ret;
1322 
1323     ret = iio_device_add_info_mask_type(indio_dev, chan,
1324                         IIO_SHARED_BY_DIR,
1325                         &chan->info_mask_shared_by_dir);
1326     if (ret < 0)
1327         return ret;
1328     attrcount += ret;
1329 
1330     ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1331                           IIO_SHARED_BY_DIR,
1332                           &chan->info_mask_shared_by_dir_available);
1333     if (ret < 0)
1334         return ret;
1335     attrcount += ret;
1336 
1337     ret = iio_device_add_info_mask_type(indio_dev, chan,
1338                         IIO_SHARED_BY_ALL,
1339                         &chan->info_mask_shared_by_all);
1340     if (ret < 0)
1341         return ret;
1342     attrcount += ret;
1343 
1344     ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1345                           IIO_SHARED_BY_ALL,
1346                           &chan->info_mask_shared_by_all_available);
1347     if (ret < 0)
1348         return ret;
1349     attrcount += ret;
1350 
1351     ret = iio_device_add_channel_label(indio_dev, chan);
1352     if (ret < 0)
1353         return ret;
1354     attrcount += ret;
1355 
1356     if (chan->ext_info) {
1357         unsigned int i = 0;
1358         for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
1359             ret = __iio_add_chan_devattr(ext_info->name,
1360                     chan,
1361                     ext_info->read ?
1362                         &iio_read_channel_ext_info : NULL,
1363                     ext_info->write ?
1364                         &iio_write_channel_ext_info : NULL,
1365                     i,
1366                     ext_info->shared,
1367                     &indio_dev->dev,
1368                     NULL,
1369                     &iio_dev_opaque->channel_attr_list);
1370             i++;
1371             if (ret == -EBUSY && ext_info->shared)
1372                 continue;
1373 
1374             if (ret)
1375                 return ret;
1376 
1377             attrcount++;
1378         }
1379     }
1380 
1381     return attrcount;
1382 }
1383 
1384 /**
1385  * iio_free_chan_devattr_list() - Free a list of IIO device attributes
1386  * @attr_list: List of IIO device attributes
1387  *
1388  * This function frees the memory allocated for each of the IIO device
1389  * attributes in the list.
1390  */
1391 void iio_free_chan_devattr_list(struct list_head *attr_list)
1392 {
1393     struct iio_dev_attr *p, *n;
1394 
1395     list_for_each_entry_safe(p, n, attr_list, l) {
1396         kfree_const(p->dev_attr.attr.name);
1397         list_del(&p->l);
1398         kfree(p);
1399     }
1400 }
1401 
1402 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
1403              char *buf)
1404 {
1405     struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1406     return sysfs_emit(buf, "%s\n", indio_dev->name);
1407 }
1408 
1409 static DEVICE_ATTR_RO(name);
1410 
1411 static ssize_t label_show(struct device *dev, struct device_attribute *attr,
1412               char *buf)
1413 {
1414     struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1415     return sysfs_emit(buf, "%s\n", indio_dev->label);
1416 }
1417 
1418 static DEVICE_ATTR_RO(label);
1419 
1420 static ssize_t current_timestamp_clock_show(struct device *dev,
1421                         struct device_attribute *attr,
1422                         char *buf)
1423 {
1424     const struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1425     const clockid_t clk = iio_device_get_clock(indio_dev);
1426     const char *name;
1427     ssize_t sz;
1428 
1429     switch (clk) {
1430     case CLOCK_REALTIME:
1431         name = "realtime\n";
1432         sz = sizeof("realtime\n");
1433         break;
1434     case CLOCK_MONOTONIC:
1435         name = "monotonic\n";
1436         sz = sizeof("monotonic\n");
1437         break;
1438     case CLOCK_MONOTONIC_RAW:
1439         name = "monotonic_raw\n";
1440         sz = sizeof("monotonic_raw\n");
1441         break;
1442     case CLOCK_REALTIME_COARSE:
1443         name = "realtime_coarse\n";
1444         sz = sizeof("realtime_coarse\n");
1445         break;
1446     case CLOCK_MONOTONIC_COARSE:
1447         name = "monotonic_coarse\n";
1448         sz = sizeof("monotonic_coarse\n");
1449         break;
1450     case CLOCK_BOOTTIME:
1451         name = "boottime\n";
1452         sz = sizeof("boottime\n");
1453         break;
1454     case CLOCK_TAI:
1455         name = "tai\n";
1456         sz = sizeof("tai\n");
1457         break;
1458     default:
1459         BUG();
1460     }
1461 
1462     memcpy(buf, name, sz);
1463     return sz;
1464 }
1465 
1466 static ssize_t current_timestamp_clock_store(struct device *dev,
1467                          struct device_attribute *attr,
1468                          const char *buf, size_t len)
1469 {
1470     clockid_t clk;
1471     int ret;
1472 
1473     if (sysfs_streq(buf, "realtime"))
1474         clk = CLOCK_REALTIME;
1475     else if (sysfs_streq(buf, "monotonic"))
1476         clk = CLOCK_MONOTONIC;
1477     else if (sysfs_streq(buf, "monotonic_raw"))
1478         clk = CLOCK_MONOTONIC_RAW;
1479     else if (sysfs_streq(buf, "realtime_coarse"))
1480         clk = CLOCK_REALTIME_COARSE;
1481     else if (sysfs_streq(buf, "monotonic_coarse"))
1482         clk = CLOCK_MONOTONIC_COARSE;
1483     else if (sysfs_streq(buf, "boottime"))
1484         clk = CLOCK_BOOTTIME;
1485     else if (sysfs_streq(buf, "tai"))
1486         clk = CLOCK_TAI;
1487     else
1488         return -EINVAL;
1489 
1490     ret = iio_device_set_clock(dev_to_iio_dev(dev), clk);
1491     if (ret)
1492         return ret;
1493 
1494     return len;
1495 }
1496 
1497 int iio_device_register_sysfs_group(struct iio_dev *indio_dev,
1498                     const struct attribute_group *group)
1499 {
1500     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1501     const struct attribute_group **new, **old = iio_dev_opaque->groups;
1502     unsigned int cnt = iio_dev_opaque->groupcounter;
1503 
1504     new = krealloc(old, sizeof(*new) * (cnt + 2), GFP_KERNEL);
1505     if (!new)
1506         return -ENOMEM;
1507 
1508     new[iio_dev_opaque->groupcounter++] = group;
1509     new[iio_dev_opaque->groupcounter] = NULL;
1510 
1511     iio_dev_opaque->groups = new;
1512 
1513     return 0;
1514 }
1515 
1516 static DEVICE_ATTR_RW(current_timestamp_clock);
1517 
1518 static int iio_device_register_sysfs(struct iio_dev *indio_dev)
1519 {
1520     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1521     int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
1522     struct iio_dev_attr *p;
1523     struct attribute **attr, *clk = NULL;
1524 
1525     /* First count elements in any existing group */
1526     if (indio_dev->info->attrs) {
1527         attr = indio_dev->info->attrs->attrs;
1528         while (*attr++ != NULL)
1529             attrcount_orig++;
1530     }
1531     attrcount = attrcount_orig;
1532     /*
1533      * New channel registration method - relies on the fact a group does
1534      * not need to be initialized if its name is NULL.
1535      */
1536     if (indio_dev->channels)
1537         for (i = 0; i < indio_dev->num_channels; i++) {
1538             const struct iio_chan_spec *chan =
1539                 &indio_dev->channels[i];
1540 
1541             if (chan->type == IIO_TIMESTAMP)
1542                 clk = &dev_attr_current_timestamp_clock.attr;
1543 
1544             ret = iio_device_add_channel_sysfs(indio_dev, chan);
1545             if (ret < 0)
1546                 goto error_clear_attrs;
1547             attrcount += ret;
1548         }
1549 
1550     if (iio_dev_opaque->event_interface)
1551         clk = &dev_attr_current_timestamp_clock.attr;
1552 
1553     if (indio_dev->name)
1554         attrcount++;
1555     if (indio_dev->label)
1556         attrcount++;
1557     if (clk)
1558         attrcount++;
1559 
1560     iio_dev_opaque->chan_attr_group.attrs =
1561         kcalloc(attrcount + 1,
1562             sizeof(iio_dev_opaque->chan_attr_group.attrs[0]),
1563             GFP_KERNEL);
1564     if (iio_dev_opaque->chan_attr_group.attrs == NULL) {
1565         ret = -ENOMEM;
1566         goto error_clear_attrs;
1567     }
1568     /* Copy across original attributes */
1569     if (indio_dev->info->attrs) {
1570         memcpy(iio_dev_opaque->chan_attr_group.attrs,
1571                indio_dev->info->attrs->attrs,
1572                sizeof(iio_dev_opaque->chan_attr_group.attrs[0])
1573                *attrcount_orig);
1574         iio_dev_opaque->chan_attr_group.is_visible =
1575             indio_dev->info->attrs->is_visible;
1576     }
1577     attrn = attrcount_orig;
1578     /* Add all elements from the list. */
1579     list_for_each_entry(p, &iio_dev_opaque->channel_attr_list, l)
1580         iio_dev_opaque->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
1581     if (indio_dev->name)
1582         iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
1583     if (indio_dev->label)
1584         iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_label.attr;
1585     if (clk)
1586         iio_dev_opaque->chan_attr_group.attrs[attrn++] = clk;
1587 
1588     ret = iio_device_register_sysfs_group(indio_dev,
1589                           &iio_dev_opaque->chan_attr_group);
1590     if (ret)
1591         goto error_clear_attrs;
1592 
1593     return 0;
1594 
1595 error_clear_attrs:
1596     iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list);
1597 
1598     return ret;
1599 }
1600 
1601 static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
1602 {
1603     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1604 
1605     iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list);
1606     kfree(iio_dev_opaque->chan_attr_group.attrs);
1607     iio_dev_opaque->chan_attr_group.attrs = NULL;
1608     kfree(iio_dev_opaque->groups);
1609     iio_dev_opaque->groups = NULL;
1610 }
1611 
1612 static void iio_dev_release(struct device *device)
1613 {
1614     struct iio_dev *indio_dev = dev_to_iio_dev(device);
1615     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1616 
1617     if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
1618         iio_device_unregister_trigger_consumer(indio_dev);
1619     iio_device_unregister_eventset(indio_dev);
1620     iio_device_unregister_sysfs(indio_dev);
1621 
1622     iio_device_detach_buffers(indio_dev);
1623 
1624     ida_free(&iio_ida, iio_dev_opaque->id);
1625     kfree(iio_dev_opaque);
1626 }
1627 
1628 const struct device_type iio_device_type = {
1629     .name = "iio_device",
1630     .release = iio_dev_release,
1631 };
1632 
1633 /**
1634  * iio_device_alloc() - allocate an iio_dev from a driver
1635  * @parent:     Parent device.
1636  * @sizeof_priv:    Space to allocate for private structure.
1637  **/
1638 struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)
1639 {
1640     struct iio_dev_opaque *iio_dev_opaque;
1641     struct iio_dev *indio_dev;
1642     size_t alloc_size;
1643 
1644     alloc_size = sizeof(struct iio_dev_opaque);
1645     if (sizeof_priv) {
1646         alloc_size = ALIGN(alloc_size, IIO_DMA_MINALIGN);
1647         alloc_size += sizeof_priv;
1648     }
1649 
1650     iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL);
1651     if (!iio_dev_opaque)
1652         return NULL;
1653 
1654     indio_dev = &iio_dev_opaque->indio_dev;
1655     indio_dev->priv = (char *)iio_dev_opaque +
1656         ALIGN(sizeof(struct iio_dev_opaque), IIO_DMA_MINALIGN);
1657 
1658     indio_dev->dev.parent = parent;
1659     indio_dev->dev.type = &iio_device_type;
1660     indio_dev->dev.bus = &iio_bus_type;
1661     device_initialize(&indio_dev->dev);
1662     mutex_init(&indio_dev->mlock);
1663     mutex_init(&iio_dev_opaque->info_exist_lock);
1664     INIT_LIST_HEAD(&iio_dev_opaque->channel_attr_list);
1665 
1666     iio_dev_opaque->id = ida_alloc(&iio_ida, GFP_KERNEL);
1667     if (iio_dev_opaque->id < 0) {
1668         /* cannot use a dev_err as the name isn't available */
1669         pr_err("failed to get device id\n");
1670         kfree(iio_dev_opaque);
1671         return NULL;
1672     }
1673 
1674     if (dev_set_name(&indio_dev->dev, "iio:device%d", iio_dev_opaque->id)) {
1675         ida_free(&iio_ida, iio_dev_opaque->id);
1676         kfree(iio_dev_opaque);
1677         return NULL;
1678     }
1679 
1680     INIT_LIST_HEAD(&iio_dev_opaque->buffer_list);
1681     INIT_LIST_HEAD(&iio_dev_opaque->ioctl_handlers);
1682 
1683     return indio_dev;
1684 }
1685 EXPORT_SYMBOL(iio_device_alloc);
1686 
1687 /**
1688  * iio_device_free() - free an iio_dev from a driver
1689  * @dev:        the iio_dev associated with the device
1690  **/
1691 void iio_device_free(struct iio_dev *dev)
1692 {
1693     if (dev)
1694         put_device(&dev->dev);
1695 }
1696 EXPORT_SYMBOL(iio_device_free);
1697 
1698 static void devm_iio_device_release(void *iio_dev)
1699 {
1700     iio_device_free(iio_dev);
1701 }
1702 
1703 /**
1704  * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1705  * @parent:     Device to allocate iio_dev for, and parent for this IIO device
1706  * @sizeof_priv:    Space to allocate for private structure.
1707  *
1708  * Managed iio_device_alloc. iio_dev allocated with this function is
1709  * automatically freed on driver detach.
1710  *
1711  * RETURNS:
1712  * Pointer to allocated iio_dev on success, NULL on failure.
1713  */
1714 struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv)
1715 {
1716     struct iio_dev *iio_dev;
1717     int ret;
1718 
1719     iio_dev = iio_device_alloc(parent, sizeof_priv);
1720     if (!iio_dev)
1721         return NULL;
1722 
1723     ret = devm_add_action_or_reset(parent, devm_iio_device_release,
1724                        iio_dev);
1725     if (ret)
1726         return NULL;
1727 
1728     return iio_dev;
1729 }
1730 EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1731 
1732 /**
1733  * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1734  * @inode:  Inode structure for identifying the device in the file system
1735  * @filp:   File structure for iio device used to keep and later access
1736  *      private data
1737  *
1738  * Return: 0 on success or -EBUSY if the device is already opened
1739  **/
1740 static int iio_chrdev_open(struct inode *inode, struct file *filp)
1741 {
1742     struct iio_dev_opaque *iio_dev_opaque =
1743         container_of(inode->i_cdev, struct iio_dev_opaque, chrdev);
1744     struct iio_dev *indio_dev = &iio_dev_opaque->indio_dev;
1745     struct iio_dev_buffer_pair *ib;
1746 
1747     if (test_and_set_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags))
1748         return -EBUSY;
1749 
1750     iio_device_get(indio_dev);
1751 
1752     ib = kmalloc(sizeof(*ib), GFP_KERNEL);
1753     if (!ib) {
1754         iio_device_put(indio_dev);
1755         clear_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags);
1756         return -ENOMEM;
1757     }
1758 
1759     ib->indio_dev = indio_dev;
1760     ib->buffer = indio_dev->buffer;
1761 
1762     filp->private_data = ib;
1763 
1764     return 0;
1765 }
1766 
1767 /**
1768  * iio_chrdev_release() - chrdev file close buffer access and ioctls
1769  * @inode:  Inode structure pointer for the char device
1770  * @filp:   File structure pointer for the char device
1771  *
1772  * Return: 0 for successful release
1773  */
1774 static int iio_chrdev_release(struct inode *inode, struct file *filp)
1775 {
1776     struct iio_dev_buffer_pair *ib = filp->private_data;
1777     struct iio_dev_opaque *iio_dev_opaque =
1778         container_of(inode->i_cdev, struct iio_dev_opaque, chrdev);
1779     struct iio_dev *indio_dev = &iio_dev_opaque->indio_dev;
1780     kfree(ib);
1781     clear_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags);
1782     iio_device_put(indio_dev);
1783 
1784     return 0;
1785 }
1786 
1787 void iio_device_ioctl_handler_register(struct iio_dev *indio_dev,
1788                        struct iio_ioctl_handler *h)
1789 {
1790     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1791 
1792     list_add_tail(&h->entry, &iio_dev_opaque->ioctl_handlers);
1793 }
1794 
1795 void iio_device_ioctl_handler_unregister(struct iio_ioctl_handler *h)
1796 {
1797     list_del(&h->entry);
1798 }
1799 
1800 static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1801 {
1802     struct iio_dev_buffer_pair *ib = filp->private_data;
1803     struct iio_dev *indio_dev = ib->indio_dev;
1804     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1805     struct iio_ioctl_handler *h;
1806     int ret = -ENODEV;
1807 
1808     mutex_lock(&iio_dev_opaque->info_exist_lock);
1809 
1810     /**
1811      * The NULL check here is required to prevent crashing when a device
1812      * is being removed while userspace would still have open file handles
1813      * to try to access this device.
1814      */
1815     if (!indio_dev->info)
1816         goto out_unlock;
1817 
1818     list_for_each_entry(h, &iio_dev_opaque->ioctl_handlers, entry) {
1819         ret = h->ioctl(indio_dev, filp, cmd, arg);
1820         if (ret != IIO_IOCTL_UNHANDLED)
1821             break;
1822     }
1823 
1824     if (ret == IIO_IOCTL_UNHANDLED)
1825         ret = -ENODEV;
1826 
1827 out_unlock:
1828     mutex_unlock(&iio_dev_opaque->info_exist_lock);
1829 
1830     return ret;
1831 }
1832 
1833 static const struct file_operations iio_buffer_fileops = {
1834     .owner = THIS_MODULE,
1835     .llseek = noop_llseek,
1836     .read = iio_buffer_read_outer_addr,
1837     .write = iio_buffer_write_outer_addr,
1838     .poll = iio_buffer_poll_addr,
1839     .unlocked_ioctl = iio_ioctl,
1840     .compat_ioctl = compat_ptr_ioctl,
1841     .open = iio_chrdev_open,
1842     .release = iio_chrdev_release,
1843 };
1844 
1845 static const struct file_operations iio_event_fileops = {
1846     .owner = THIS_MODULE,
1847     .llseek = noop_llseek,
1848     .unlocked_ioctl = iio_ioctl,
1849     .compat_ioctl = compat_ptr_ioctl,
1850     .open = iio_chrdev_open,
1851     .release = iio_chrdev_release,
1852 };
1853 
1854 static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
1855 {
1856     int i, j;
1857     const struct iio_chan_spec *channels = indio_dev->channels;
1858 
1859     if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
1860         return 0;
1861 
1862     for (i = 0; i < indio_dev->num_channels - 1; i++) {
1863         if (channels[i].scan_index < 0)
1864             continue;
1865         for (j = i + 1; j < indio_dev->num_channels; j++)
1866             if (channels[i].scan_index == channels[j].scan_index) {
1867                 dev_err(&indio_dev->dev,
1868                     "Duplicate scan index %d\n",
1869                     channels[i].scan_index);
1870                 return -EINVAL;
1871             }
1872     }
1873 
1874     return 0;
1875 }
1876 
1877 static int iio_check_extended_name(const struct iio_dev *indio_dev)
1878 {
1879     unsigned int i;
1880 
1881     if (!indio_dev->info->read_label)
1882         return 0;
1883 
1884     for (i = 0; i < indio_dev->num_channels; i++) {
1885         if (indio_dev->channels[i].extend_name) {
1886             dev_err(&indio_dev->dev,
1887                 "Cannot use labels and extend_name at the same time\n");
1888             return -EINVAL;
1889         }
1890     }
1891 
1892     return 0;
1893 }
1894 
1895 static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1896 
1897 int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod)
1898 {
1899     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1900     struct fwnode_handle *fwnode;
1901     int ret;
1902 
1903     if (!indio_dev->info)
1904         return -EINVAL;
1905 
1906     iio_dev_opaque->driver_module = this_mod;
1907 
1908     /* If the calling driver did not initialize firmware node, do it here */
1909     if (dev_fwnode(&indio_dev->dev))
1910         fwnode = dev_fwnode(&indio_dev->dev);
1911     else
1912         fwnode = dev_fwnode(indio_dev->dev.parent);
1913     device_set_node(&indio_dev->dev, fwnode);
1914 
1915     fwnode_property_read_string(fwnode, "label", &indio_dev->label);
1916 
1917     ret = iio_check_unique_scan_index(indio_dev);
1918     if (ret < 0)
1919         return ret;
1920 
1921     ret = iio_check_extended_name(indio_dev);
1922     if (ret < 0)
1923         return ret;
1924 
1925     iio_device_register_debugfs(indio_dev);
1926 
1927     ret = iio_buffers_alloc_sysfs_and_mask(indio_dev);
1928     if (ret) {
1929         dev_err(indio_dev->dev.parent,
1930             "Failed to create buffer sysfs interfaces\n");
1931         goto error_unreg_debugfs;
1932     }
1933 
1934     ret = iio_device_register_sysfs(indio_dev);
1935     if (ret) {
1936         dev_err(indio_dev->dev.parent,
1937             "Failed to register sysfs interfaces\n");
1938         goto error_buffer_free_sysfs;
1939     }
1940     ret = iio_device_register_eventset(indio_dev);
1941     if (ret) {
1942         dev_err(indio_dev->dev.parent,
1943             "Failed to register event set\n");
1944         goto error_free_sysfs;
1945     }
1946     if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
1947         iio_device_register_trigger_consumer(indio_dev);
1948 
1949     if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1950         indio_dev->setup_ops == NULL)
1951         indio_dev->setup_ops = &noop_ring_setup_ops;
1952 
1953     if (iio_dev_opaque->attached_buffers_cnt)
1954         cdev_init(&iio_dev_opaque->chrdev, &iio_buffer_fileops);
1955     else if (iio_dev_opaque->event_interface)
1956         cdev_init(&iio_dev_opaque->chrdev, &iio_event_fileops);
1957 
1958     if (iio_dev_opaque->attached_buffers_cnt || iio_dev_opaque->event_interface) {
1959         indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), iio_dev_opaque->id);
1960         iio_dev_opaque->chrdev.owner = this_mod;
1961     }
1962 
1963     /* assign device groups now; they should be all registered now */
1964     indio_dev->dev.groups = iio_dev_opaque->groups;
1965 
1966     ret = cdev_device_add(&iio_dev_opaque->chrdev, &indio_dev->dev);
1967     if (ret < 0)
1968         goto error_unreg_eventset;
1969 
1970     return 0;
1971 
1972 error_unreg_eventset:
1973     iio_device_unregister_eventset(indio_dev);
1974 error_free_sysfs:
1975     iio_device_unregister_sysfs(indio_dev);
1976 error_buffer_free_sysfs:
1977     iio_buffers_free_sysfs_and_mask(indio_dev);
1978 error_unreg_debugfs:
1979     iio_device_unregister_debugfs(indio_dev);
1980     return ret;
1981 }
1982 EXPORT_SYMBOL(__iio_device_register);
1983 
1984 /**
1985  * iio_device_unregister() - unregister a device from the IIO subsystem
1986  * @indio_dev:      Device structure representing the device.
1987  **/
1988 void iio_device_unregister(struct iio_dev *indio_dev)
1989 {
1990     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1991 
1992     cdev_device_del(&iio_dev_opaque->chrdev, &indio_dev->dev);
1993 
1994     mutex_lock(&iio_dev_opaque->info_exist_lock);
1995 
1996     iio_device_unregister_debugfs(indio_dev);
1997 
1998     iio_disable_all_buffers(indio_dev);
1999 
2000     indio_dev->info = NULL;
2001 
2002     iio_device_wakeup_eventset(indio_dev);
2003     iio_buffer_wakeup_poll(indio_dev);
2004 
2005     mutex_unlock(&iio_dev_opaque->info_exist_lock);
2006 
2007     iio_buffers_free_sysfs_and_mask(indio_dev);
2008 }
2009 EXPORT_SYMBOL(iio_device_unregister);
2010 
2011 static void devm_iio_device_unreg(void *indio_dev)
2012 {
2013     iio_device_unregister(indio_dev);
2014 }
2015 
2016 int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
2017                    struct module *this_mod)
2018 {
2019     int ret;
2020 
2021     ret = __iio_device_register(indio_dev, this_mod);
2022     if (ret)
2023         return ret;
2024 
2025     return devm_add_action_or_reset(dev, devm_iio_device_unreg, indio_dev);
2026 }
2027 EXPORT_SYMBOL_GPL(__devm_iio_device_register);
2028 
2029 /**
2030  * iio_device_claim_direct_mode - Keep device in direct mode
2031  * @indio_dev:  the iio_dev associated with the device
2032  *
2033  * If the device is in direct mode it is guaranteed to stay
2034  * that way until iio_device_release_direct_mode() is called.
2035  *
2036  * Use with iio_device_release_direct_mode()
2037  *
2038  * Returns: 0 on success, -EBUSY on failure
2039  */
2040 int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
2041 {
2042     mutex_lock(&indio_dev->mlock);
2043 
2044     if (iio_buffer_enabled(indio_dev)) {
2045         mutex_unlock(&indio_dev->mlock);
2046         return -EBUSY;
2047     }
2048     return 0;
2049 }
2050 EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
2051 
2052 /**
2053  * iio_device_release_direct_mode - releases claim on direct mode
2054  * @indio_dev:  the iio_dev associated with the device
2055  *
2056  * Release the claim. Device is no longer guaranteed to stay
2057  * in direct mode.
2058  *
2059  * Use with iio_device_claim_direct_mode()
2060  */
2061 void iio_device_release_direct_mode(struct iio_dev *indio_dev)
2062 {
2063     mutex_unlock(&indio_dev->mlock);
2064 }
2065 EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
2066 
2067 /**
2068  * iio_device_get_current_mode() - helper function providing read-only access to
2069  *                 the opaque @currentmode variable
2070  * @indio_dev:             IIO device structure for device
2071  */
2072 int iio_device_get_current_mode(struct iio_dev *indio_dev)
2073 {
2074     struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
2075 
2076     return iio_dev_opaque->currentmode;
2077 }
2078 EXPORT_SYMBOL_GPL(iio_device_get_current_mode);
2079 
2080 subsys_initcall(iio_init);
2081 module_exit(iio_exit);
2082 
2083 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
2084 MODULE_DESCRIPTION("Industrial I/O core");
2085 MODULE_LICENSE("GPL");