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