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