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