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