1 /* The industrial I/O core 2 * 3 * Copyright (c) 2008 Jonathan Cameron 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 as published by 7 * the Free Software Foundation. 8 * 9 * Based on elements of hwmon and input subsystems. 10 */ 11 12 #define pr_fmt(fmt) "iio-core: " fmt 13 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/idr.h> 17 #include <linux/kdev_t.h> 18 #include <linux/err.h> 19 #include <linux/device.h> 20 #include <linux/fs.h> 21 #include <linux/poll.h> 22 #include <linux/sched.h> 23 #include <linux/wait.h> 24 #include <linux/cdev.h> 25 #include <linux/slab.h> 26 #include <linux/anon_inodes.h> 27 #include <linux/debugfs.h> 28 #include <linux/iio/iio.h> 29 #include "iio_core.h" 30 #include "iio_core_trigger.h" 31 #include <linux/iio/sysfs.h> 32 #include <linux/iio/events.h> 33 #include <linux/iio/buffer.h> 34 35 /* IDA to assign each registered device a unique id */ 36 static DEFINE_IDA(iio_ida); 37 38 static dev_t iio_devt; 39 40 #define IIO_DEV_MAX 256 41 struct bus_type iio_bus_type = { 42 .name = "iio", 43 }; 44 EXPORT_SYMBOL(iio_bus_type); 45 46 static struct dentry *iio_debugfs_dentry; 47 48 static const char * const iio_direction[] = { 49 [0] = "in", 50 [1] = "out", 51 }; 52 53 static const char * const iio_chan_type_name_spec[] = { 54 [IIO_VOLTAGE] = "voltage", 55 [IIO_CURRENT] = "current", 56 [IIO_POWER] = "power", 57 [IIO_ACCEL] = "accel", 58 [IIO_ANGL_VEL] = "anglvel", 59 [IIO_MAGN] = "magn", 60 [IIO_LIGHT] = "illuminance", 61 [IIO_INTENSITY] = "intensity", 62 [IIO_PROXIMITY] = "proximity", 63 [IIO_TEMP] = "temp", 64 [IIO_INCLI] = "incli", 65 [IIO_ROT] = "rot", 66 [IIO_ANGL] = "angl", 67 [IIO_TIMESTAMP] = "timestamp", 68 [IIO_CAPACITANCE] = "capacitance", 69 [IIO_ALTVOLTAGE] = "altvoltage", 70 [IIO_CCT] = "cct", 71 [IIO_PRESSURE] = "pressure", 72 }; 73 74 static const char * const iio_modifier_names[] = { 75 [IIO_MOD_X] = "x", 76 [IIO_MOD_Y] = "y", 77 [IIO_MOD_Z] = "z", 78 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)", 79 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2", 80 [IIO_MOD_LIGHT_BOTH] = "both", 81 [IIO_MOD_LIGHT_IR] = "ir", 82 [IIO_MOD_LIGHT_CLEAR] = "clear", 83 [IIO_MOD_LIGHT_RED] = "red", 84 [IIO_MOD_LIGHT_GREEN] = "green", 85 [IIO_MOD_LIGHT_BLUE] = "blue", 86 }; 87 88 /* relies on pairs of these shared then separate */ 89 static const char * const iio_chan_info_postfix[] = { 90 [IIO_CHAN_INFO_RAW] = "raw", 91 [IIO_CHAN_INFO_PROCESSED] = "input", 92 [IIO_CHAN_INFO_SCALE] = "scale", 93 [IIO_CHAN_INFO_OFFSET] = "offset", 94 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale", 95 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias", 96 [IIO_CHAN_INFO_PEAK] = "peak_raw", 97 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale", 98 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw", 99 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", 100 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] 101 = "filter_low_pass_3db_frequency", 102 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", 103 [IIO_CHAN_INFO_FREQUENCY] = "frequency", 104 [IIO_CHAN_INFO_PHASE] = "phase", 105 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", 106 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", 107 [IIO_CHAN_INFO_INT_TIME] = "integration_time", 108 }; 109 110 const struct iio_chan_spec 111 *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) 112 { 113 int i; 114 115 for (i = 0; i < indio_dev->num_channels; i++) 116 if (indio_dev->channels[i].scan_index == si) 117 return &indio_dev->channels[i]; 118 return NULL; 119 } 120 121 /* This turns up an awful lot */ 122 ssize_t iio_read_const_attr(struct device *dev, 123 struct device_attribute *attr, 124 char *buf) 125 { 126 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); 127 } 128 EXPORT_SYMBOL(iio_read_const_attr); 129 130 static int __init iio_init(void) 131 { 132 int ret; 133 134 /* Register sysfs bus */ 135 ret = bus_register(&iio_bus_type); 136 if (ret < 0) { 137 pr_err("could not register bus type\n"); 138 goto error_nothing; 139 } 140 141 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); 142 if (ret < 0) { 143 pr_err("failed to allocate char dev region\n"); 144 goto error_unregister_bus_type; 145 } 146 147 iio_debugfs_dentry = debugfs_create_dir("iio", NULL); 148 149 return 0; 150 151 error_unregister_bus_type: 152 bus_unregister(&iio_bus_type); 153 error_nothing: 154 return ret; 155 } 156 157 static void __exit iio_exit(void) 158 { 159 if (iio_devt) 160 unregister_chrdev_region(iio_devt, IIO_DEV_MAX); 161 bus_unregister(&iio_bus_type); 162 debugfs_remove(iio_debugfs_dentry); 163 } 164 165 #if defined(CONFIG_DEBUG_FS) 166 static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, 167 size_t count, loff_t *ppos) 168 { 169 struct iio_dev *indio_dev = file->private_data; 170 char buf[20]; 171 unsigned val = 0; 172 ssize_t len; 173 int ret; 174 175 ret = indio_dev->info->debugfs_reg_access(indio_dev, 176 indio_dev->cached_reg_addr, 177 0, &val); 178 if (ret) 179 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); 180 181 len = snprintf(buf, sizeof(buf), "0x%X\n", val); 182 183 return simple_read_from_buffer(userbuf, count, ppos, buf, len); 184 } 185 186 static ssize_t iio_debugfs_write_reg(struct file *file, 187 const char __user *userbuf, size_t count, loff_t *ppos) 188 { 189 struct iio_dev *indio_dev = file->private_data; 190 unsigned reg, val; 191 char buf[80]; 192 int ret; 193 194 count = min_t(size_t, count, (sizeof(buf)-1)); 195 if (copy_from_user(buf, userbuf, count)) 196 return -EFAULT; 197 198 buf[count] = 0; 199 200 ret = sscanf(buf, "%i %i", ®, &val); 201 202 switch (ret) { 203 case 1: 204 indio_dev->cached_reg_addr = reg; 205 break; 206 case 2: 207 indio_dev->cached_reg_addr = reg; 208 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, 209 val, NULL); 210 if (ret) { 211 dev_err(indio_dev->dev.parent, "%s: write failed\n", 212 __func__); 213 return ret; 214 } 215 break; 216 default: 217 return -EINVAL; 218 } 219 220 return count; 221 } 222 223 static const struct file_operations iio_debugfs_reg_fops = { 224 .open = simple_open, 225 .read = iio_debugfs_read_reg, 226 .write = iio_debugfs_write_reg, 227 }; 228 229 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 230 { 231 debugfs_remove_recursive(indio_dev->debugfs_dentry); 232 } 233 234 static int iio_device_register_debugfs(struct iio_dev *indio_dev) 235 { 236 struct dentry *d; 237 238 if (indio_dev->info->debugfs_reg_access == NULL) 239 return 0; 240 241 if (!iio_debugfs_dentry) 242 return 0; 243 244 indio_dev->debugfs_dentry = 245 debugfs_create_dir(dev_name(&indio_dev->dev), 246 iio_debugfs_dentry); 247 if (indio_dev->debugfs_dentry == NULL) { 248 dev_warn(indio_dev->dev.parent, 249 "Failed to create debugfs directory\n"); 250 return -EFAULT; 251 } 252 253 d = debugfs_create_file("direct_reg_access", 0644, 254 indio_dev->debugfs_dentry, 255 indio_dev, &iio_debugfs_reg_fops); 256 if (!d) { 257 iio_device_unregister_debugfs(indio_dev); 258 return -ENOMEM; 259 } 260 261 return 0; 262 } 263 #else 264 static int iio_device_register_debugfs(struct iio_dev *indio_dev) 265 { 266 return 0; 267 } 268 269 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 270 { 271 } 272 #endif /* CONFIG_DEBUG_FS */ 273 274 static ssize_t iio_read_channel_ext_info(struct device *dev, 275 struct device_attribute *attr, 276 char *buf) 277 { 278 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 279 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 280 const struct iio_chan_spec_ext_info *ext_info; 281 282 ext_info = &this_attr->c->ext_info[this_attr->address]; 283 284 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); 285 } 286 287 static ssize_t iio_write_channel_ext_info(struct device *dev, 288 struct device_attribute *attr, 289 const char *buf, 290 size_t len) 291 { 292 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 293 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 294 const struct iio_chan_spec_ext_info *ext_info; 295 296 ext_info = &this_attr->c->ext_info[this_attr->address]; 297 298 return ext_info->write(indio_dev, ext_info->private, 299 this_attr->c, buf, len); 300 } 301 302 ssize_t iio_enum_available_read(struct iio_dev *indio_dev, 303 uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 304 { 305 const struct iio_enum *e = (const struct iio_enum *)priv; 306 unsigned int i; 307 size_t len = 0; 308 309 if (!e->num_items) 310 return 0; 311 312 for (i = 0; i < e->num_items; ++i) 313 len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]); 314 315 /* replace last space with a newline */ 316 buf[len - 1] = '\n'; 317 318 return len; 319 } 320 EXPORT_SYMBOL_GPL(iio_enum_available_read); 321 322 ssize_t iio_enum_read(struct iio_dev *indio_dev, 323 uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 324 { 325 const struct iio_enum *e = (const struct iio_enum *)priv; 326 int i; 327 328 if (!e->get) 329 return -EINVAL; 330 331 i = e->get(indio_dev, chan); 332 if (i < 0) 333 return i; 334 else if (i >= e->num_items) 335 return -EINVAL; 336 337 return sprintf(buf, "%s\n", e->items[i]); 338 } 339 EXPORT_SYMBOL_GPL(iio_enum_read); 340 341 ssize_t iio_enum_write(struct iio_dev *indio_dev, 342 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, 343 size_t len) 344 { 345 const struct iio_enum *e = (const struct iio_enum *)priv; 346 unsigned int i; 347 int ret; 348 349 if (!e->set) 350 return -EINVAL; 351 352 for (i = 0; i < e->num_items; i++) { 353 if (sysfs_streq(buf, e->items[i])) 354 break; 355 } 356 357 if (i == e->num_items) 358 return -EINVAL; 359 360 ret = e->set(indio_dev, chan, i); 361 return ret ? ret : len; 362 } 363 EXPORT_SYMBOL_GPL(iio_enum_write); 364 365 /** 366 * iio_format_value() - Formats a IIO value into its string representation 367 * @buf: The buffer to which the formated value gets written 368 * @type: One of the IIO_VAL_... constants. This decides how the val and val2 369 * parameters are formatted. 370 * @val: First part of the value, exact meaning depends on the type parameter. 371 * @val2: Second part of the value, exact meaning depends on the type parameter. 372 */ 373 ssize_t iio_format_value(char *buf, unsigned int type, int val, int val2) 374 { 375 unsigned long long tmp; 376 bool scale_db = false; 377 378 switch (type) { 379 case IIO_VAL_INT: 380 return sprintf(buf, "%d\n", val); 381 case IIO_VAL_INT_PLUS_MICRO_DB: 382 scale_db = true; 383 case IIO_VAL_INT_PLUS_MICRO: 384 if (val2 < 0) 385 return sprintf(buf, "-%ld.%06u%s\n", abs(val), -val2, 386 scale_db ? " dB" : ""); 387 else 388 return sprintf(buf, "%d.%06u%s\n", val, val2, 389 scale_db ? " dB" : ""); 390 case IIO_VAL_INT_PLUS_NANO: 391 if (val2 < 0) 392 return sprintf(buf, "-%ld.%09u\n", abs(val), -val2); 393 else 394 return sprintf(buf, "%d.%09u\n", val, val2); 395 case IIO_VAL_FRACTIONAL: 396 tmp = div_s64((s64)val * 1000000000LL, val2); 397 val2 = do_div(tmp, 1000000000LL); 398 val = tmp; 399 return sprintf(buf, "%d.%09u\n", val, val2); 400 case IIO_VAL_FRACTIONAL_LOG2: 401 tmp = (s64)val * 1000000000LL >> val2; 402 val2 = do_div(tmp, 1000000000LL); 403 val = tmp; 404 return sprintf(buf, "%d.%09u\n", val, val2); 405 default: 406 return 0; 407 } 408 } 409 410 static ssize_t iio_read_channel_info(struct device *dev, 411 struct device_attribute *attr, 412 char *buf) 413 { 414 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 415 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 416 int val, val2; 417 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c, 418 &val, &val2, this_attr->address); 419 420 if (ret < 0) 421 return ret; 422 423 return iio_format_value(buf, ret, val, val2); 424 } 425 426 /** 427 * iio_str_to_fixpoint() - Parse a fixed-point number from a string 428 * @str: The string to parse 429 * @fract_mult: Multiplier for the first decimal place, should be a power of 10 430 * @integer: The integer part of the number 431 * @fract: The fractional part of the number 432 * 433 * Returns 0 on success, or a negative error code if the string could not be 434 * parsed. 435 */ 436 int iio_str_to_fixpoint(const char *str, int fract_mult, 437 int *integer, int *fract) 438 { 439 int i = 0, f = 0; 440 bool integer_part = true, negative = false; 441 442 if (str[0] == '-') { 443 negative = true; 444 str++; 445 } else if (str[0] == '+') { 446 str++; 447 } 448 449 while (*str) { 450 if ('0' <= *str && *str <= '9') { 451 if (integer_part) { 452 i = i * 10 + *str - '0'; 453 } else { 454 f += fract_mult * (*str - '0'); 455 fract_mult /= 10; 456 } 457 } else if (*str == '\n') { 458 if (*(str + 1) == '\0') 459 break; 460 else 461 return -EINVAL; 462 } else if (*str == '.' && integer_part) { 463 integer_part = false; 464 } else { 465 return -EINVAL; 466 } 467 str++; 468 } 469 470 if (negative) { 471 if (i) 472 i = -i; 473 else 474 f = -f; 475 } 476 477 *integer = i; 478 *fract = f; 479 480 return 0; 481 } 482 EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); 483 484 static ssize_t iio_write_channel_info(struct device *dev, 485 struct device_attribute *attr, 486 const char *buf, 487 size_t len) 488 { 489 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 490 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 491 int ret, fract_mult = 100000; 492 int integer, fract; 493 494 /* Assumes decimal - precision based on number of digits */ 495 if (!indio_dev->info->write_raw) 496 return -EINVAL; 497 498 if (indio_dev->info->write_raw_get_fmt) 499 switch (indio_dev->info->write_raw_get_fmt(indio_dev, 500 this_attr->c, this_attr->address)) { 501 case IIO_VAL_INT_PLUS_MICRO: 502 fract_mult = 100000; 503 break; 504 case IIO_VAL_INT_PLUS_NANO: 505 fract_mult = 100000000; 506 break; 507 default: 508 return -EINVAL; 509 } 510 511 ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract); 512 if (ret) 513 return ret; 514 515 ret = indio_dev->info->write_raw(indio_dev, this_attr->c, 516 integer, fract, this_attr->address); 517 if (ret) 518 return ret; 519 520 return len; 521 } 522 523 static 524 int __iio_device_attr_init(struct device_attribute *dev_attr, 525 const char *postfix, 526 struct iio_chan_spec const *chan, 527 ssize_t (*readfunc)(struct device *dev, 528 struct device_attribute *attr, 529 char *buf), 530 ssize_t (*writefunc)(struct device *dev, 531 struct device_attribute *attr, 532 const char *buf, 533 size_t len), 534 enum iio_shared_by shared_by) 535 { 536 int ret = 0; 537 char *name_format = NULL; 538 char *full_postfix; 539 sysfs_attr_init(&dev_attr->attr); 540 541 /* Build up postfix of <extend_name>_<modifier>_postfix */ 542 if (chan->modified && (shared_by == IIO_SEPARATE)) { 543 if (chan->extend_name) 544 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", 545 iio_modifier_names[chan 546 ->channel2], 547 chan->extend_name, 548 postfix); 549 else 550 full_postfix = kasprintf(GFP_KERNEL, "%s_%s", 551 iio_modifier_names[chan 552 ->channel2], 553 postfix); 554 } else { 555 if (chan->extend_name == NULL) 556 full_postfix = kstrdup(postfix, GFP_KERNEL); 557 else 558 full_postfix = kasprintf(GFP_KERNEL, 559 "%s_%s", 560 chan->extend_name, 561 postfix); 562 } 563 if (full_postfix == NULL) 564 return -ENOMEM; 565 566 if (chan->differential) { /* Differential can not have modifier */ 567 switch (shared_by) { 568 case IIO_SHARED_BY_ALL: 569 name_format = kasprintf(GFP_KERNEL, "%s", full_postfix); 570 break; 571 case IIO_SHARED_BY_DIR: 572 name_format = kasprintf(GFP_KERNEL, "%s_%s", 573 iio_direction[chan->output], 574 full_postfix); 575 break; 576 case IIO_SHARED_BY_TYPE: 577 name_format 578 = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", 579 iio_direction[chan->output], 580 iio_chan_type_name_spec[chan->type], 581 iio_chan_type_name_spec[chan->type], 582 full_postfix); 583 break; 584 case IIO_SEPARATE: 585 if (!chan->indexed) { 586 WARN_ON("Differential channels must be indexed\n"); 587 ret = -EINVAL; 588 goto error_free_full_postfix; 589 } 590 name_format 591 = kasprintf(GFP_KERNEL, 592 "%s_%s%d-%s%d_%s", 593 iio_direction[chan->output], 594 iio_chan_type_name_spec[chan->type], 595 chan->channel, 596 iio_chan_type_name_spec[chan->type], 597 chan->channel2, 598 full_postfix); 599 break; 600 } 601 } else { /* Single ended */ 602 switch (shared_by) { 603 case IIO_SHARED_BY_ALL: 604 name_format = kasprintf(GFP_KERNEL, "%s", full_postfix); 605 break; 606 case IIO_SHARED_BY_DIR: 607 name_format = kasprintf(GFP_KERNEL, "%s_%s", 608 iio_direction[chan->output], 609 full_postfix); 610 break; 611 case IIO_SHARED_BY_TYPE: 612 name_format 613 = kasprintf(GFP_KERNEL, "%s_%s_%s", 614 iio_direction[chan->output], 615 iio_chan_type_name_spec[chan->type], 616 full_postfix); 617 break; 618 619 case IIO_SEPARATE: 620 if (chan->indexed) 621 name_format 622 = kasprintf(GFP_KERNEL, "%s_%s%d_%s", 623 iio_direction[chan->output], 624 iio_chan_type_name_spec[chan->type], 625 chan->channel, 626 full_postfix); 627 else 628 name_format 629 = kasprintf(GFP_KERNEL, "%s_%s_%s", 630 iio_direction[chan->output], 631 iio_chan_type_name_spec[chan->type], 632 full_postfix); 633 break; 634 } 635 } 636 if (name_format == NULL) { 637 ret = -ENOMEM; 638 goto error_free_full_postfix; 639 } 640 dev_attr->attr.name = kasprintf(GFP_KERNEL, 641 name_format, 642 chan->channel, 643 chan->channel2); 644 if (dev_attr->attr.name == NULL) { 645 ret = -ENOMEM; 646 goto error_free_name_format; 647 } 648 649 if (readfunc) { 650 dev_attr->attr.mode |= S_IRUGO; 651 dev_attr->show = readfunc; 652 } 653 654 if (writefunc) { 655 dev_attr->attr.mode |= S_IWUSR; 656 dev_attr->store = writefunc; 657 } 658 error_free_name_format: 659 kfree(name_format); 660 error_free_full_postfix: 661 kfree(full_postfix); 662 663 return ret; 664 } 665 666 static void __iio_device_attr_deinit(struct device_attribute *dev_attr) 667 { 668 kfree(dev_attr->attr.name); 669 } 670 671 int __iio_add_chan_devattr(const char *postfix, 672 struct iio_chan_spec const *chan, 673 ssize_t (*readfunc)(struct device *dev, 674 struct device_attribute *attr, 675 char *buf), 676 ssize_t (*writefunc)(struct device *dev, 677 struct device_attribute *attr, 678 const char *buf, 679 size_t len), 680 u64 mask, 681 enum iio_shared_by shared_by, 682 struct device *dev, 683 struct list_head *attr_list) 684 { 685 int ret; 686 struct iio_dev_attr *iio_attr, *t; 687 688 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL); 689 if (iio_attr == NULL) { 690 ret = -ENOMEM; 691 goto error_ret; 692 } 693 ret = __iio_device_attr_init(&iio_attr->dev_attr, 694 postfix, chan, 695 readfunc, writefunc, shared_by); 696 if (ret) 697 goto error_iio_dev_attr_free; 698 iio_attr->c = chan; 699 iio_attr->address = mask; 700 list_for_each_entry(t, attr_list, l) 701 if (strcmp(t->dev_attr.attr.name, 702 iio_attr->dev_attr.attr.name) == 0) { 703 if (shared_by == IIO_SEPARATE) 704 dev_err(dev, "tried to double register : %s\n", 705 t->dev_attr.attr.name); 706 ret = -EBUSY; 707 goto error_device_attr_deinit; 708 } 709 list_add(&iio_attr->l, attr_list); 710 711 return 0; 712 713 error_device_attr_deinit: 714 __iio_device_attr_deinit(&iio_attr->dev_attr); 715 error_iio_dev_attr_free: 716 kfree(iio_attr); 717 error_ret: 718 return ret; 719 } 720 721 static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, 722 struct iio_chan_spec const *chan, 723 enum iio_shared_by shared_by, 724 const long *infomask) 725 { 726 int i, ret, attrcount = 0; 727 728 for_each_set_bit(i, infomask, sizeof(infomask)*8) { 729 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], 730 chan, 731 &iio_read_channel_info, 732 &iio_write_channel_info, 733 i, 734 shared_by, 735 &indio_dev->dev, 736 &indio_dev->channel_attr_list); 737 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 738 continue; 739 else if (ret < 0) 740 return ret; 741 attrcount++; 742 } 743 744 return attrcount; 745 } 746 747 static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, 748 struct iio_chan_spec const *chan) 749 { 750 int ret, attrcount = 0; 751 const struct iio_chan_spec_ext_info *ext_info; 752 753 if (chan->channel < 0) 754 return 0; 755 ret = iio_device_add_info_mask_type(indio_dev, chan, 756 IIO_SEPARATE, 757 &chan->info_mask_separate); 758 if (ret < 0) 759 return ret; 760 attrcount += ret; 761 762 ret = iio_device_add_info_mask_type(indio_dev, chan, 763 IIO_SHARED_BY_TYPE, 764 &chan->info_mask_shared_by_type); 765 if (ret < 0) 766 return ret; 767 attrcount += ret; 768 769 ret = iio_device_add_info_mask_type(indio_dev, chan, 770 IIO_SHARED_BY_DIR, 771 &chan->info_mask_shared_by_dir); 772 if (ret < 0) 773 return ret; 774 attrcount += ret; 775 776 ret = iio_device_add_info_mask_type(indio_dev, chan, 777 IIO_SHARED_BY_ALL, 778 &chan->info_mask_shared_by_all); 779 if (ret < 0) 780 return ret; 781 attrcount += ret; 782 783 if (chan->ext_info) { 784 unsigned int i = 0; 785 for (ext_info = chan->ext_info; ext_info->name; ext_info++) { 786 ret = __iio_add_chan_devattr(ext_info->name, 787 chan, 788 ext_info->read ? 789 &iio_read_channel_ext_info : NULL, 790 ext_info->write ? 791 &iio_write_channel_ext_info : NULL, 792 i, 793 ext_info->shared, 794 &indio_dev->dev, 795 &indio_dev->channel_attr_list); 796 i++; 797 if (ret == -EBUSY && ext_info->shared) 798 continue; 799 800 if (ret) 801 return ret; 802 803 attrcount++; 804 } 805 } 806 807 return attrcount; 808 } 809 810 /** 811 * iio_free_chan_devattr_list() - Free a list of IIO device attributes 812 * @attr_list: List of IIO device attributes 813 * 814 * This function frees the memory allocated for each of the IIO device 815 * attributes in the list. Note: if you want to reuse the list after calling 816 * this function you have to reinitialize it using INIT_LIST_HEAD(). 817 */ 818 void iio_free_chan_devattr_list(struct list_head *attr_list) 819 { 820 struct iio_dev_attr *p, *n; 821 822 list_for_each_entry_safe(p, n, attr_list, l) { 823 kfree(p->dev_attr.attr.name); 824 kfree(p); 825 } 826 } 827 828 static ssize_t iio_show_dev_name(struct device *dev, 829 struct device_attribute *attr, 830 char *buf) 831 { 832 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 833 return sprintf(buf, "%s\n", indio_dev->name); 834 } 835 836 static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); 837 838 static int iio_device_register_sysfs(struct iio_dev *indio_dev) 839 { 840 int i, ret = 0, attrcount, attrn, attrcount_orig = 0; 841 struct iio_dev_attr *p; 842 struct attribute **attr; 843 844 /* First count elements in any existing group */ 845 if (indio_dev->info->attrs) { 846 attr = indio_dev->info->attrs->attrs; 847 while (*attr++ != NULL) 848 attrcount_orig++; 849 } 850 attrcount = attrcount_orig; 851 /* 852 * New channel registration method - relies on the fact a group does 853 * not need to be initialized if its name is NULL. 854 */ 855 if (indio_dev->channels) 856 for (i = 0; i < indio_dev->num_channels; i++) { 857 ret = iio_device_add_channel_sysfs(indio_dev, 858 &indio_dev 859 ->channels[i]); 860 if (ret < 0) 861 goto error_clear_attrs; 862 attrcount += ret; 863 } 864 865 if (indio_dev->name) 866 attrcount++; 867 868 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1, 869 sizeof(indio_dev->chan_attr_group.attrs[0]), 870 GFP_KERNEL); 871 if (indio_dev->chan_attr_group.attrs == NULL) { 872 ret = -ENOMEM; 873 goto error_clear_attrs; 874 } 875 /* Copy across original attributes */ 876 if (indio_dev->info->attrs) 877 memcpy(indio_dev->chan_attr_group.attrs, 878 indio_dev->info->attrs->attrs, 879 sizeof(indio_dev->chan_attr_group.attrs[0]) 880 *attrcount_orig); 881 attrn = attrcount_orig; 882 /* Add all elements from the list. */ 883 list_for_each_entry(p, &indio_dev->channel_attr_list, l) 884 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; 885 if (indio_dev->name) 886 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; 887 888 indio_dev->groups[indio_dev->groupcounter++] = 889 &indio_dev->chan_attr_group; 890 891 return 0; 892 893 error_clear_attrs: 894 iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 895 896 return ret; 897 } 898 899 static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) 900 { 901 902 iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 903 kfree(indio_dev->chan_attr_group.attrs); 904 } 905 906 static void iio_dev_release(struct device *device) 907 { 908 struct iio_dev *indio_dev = dev_to_iio_dev(device); 909 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) 910 iio_device_unregister_trigger_consumer(indio_dev); 911 iio_device_unregister_eventset(indio_dev); 912 iio_device_unregister_sysfs(indio_dev); 913 914 iio_buffer_put(indio_dev->buffer); 915 916 ida_simple_remove(&iio_ida, indio_dev->id); 917 kfree(indio_dev); 918 } 919 920 struct device_type iio_device_type = { 921 .name = "iio_device", 922 .release = iio_dev_release, 923 }; 924 925 struct iio_dev *iio_device_alloc(int sizeof_priv) 926 { 927 struct iio_dev *dev; 928 size_t alloc_size; 929 930 alloc_size = sizeof(struct iio_dev); 931 if (sizeof_priv) { 932 alloc_size = ALIGN(alloc_size, IIO_ALIGN); 933 alloc_size += sizeof_priv; 934 } 935 /* ensure 32-byte alignment of whole construct ? */ 936 alloc_size += IIO_ALIGN - 1; 937 938 dev = kzalloc(alloc_size, GFP_KERNEL); 939 940 if (dev) { 941 dev->dev.groups = dev->groups; 942 dev->dev.type = &iio_device_type; 943 dev->dev.bus = &iio_bus_type; 944 device_initialize(&dev->dev); 945 dev_set_drvdata(&dev->dev, (void *)dev); 946 mutex_init(&dev->mlock); 947 mutex_init(&dev->info_exist_lock); 948 INIT_LIST_HEAD(&dev->channel_attr_list); 949 950 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); 951 if (dev->id < 0) { 952 /* cannot use a dev_err as the name isn't available */ 953 pr_err("failed to get device id\n"); 954 kfree(dev); 955 return NULL; 956 } 957 dev_set_name(&dev->dev, "iio:device%d", dev->id); 958 INIT_LIST_HEAD(&dev->buffer_list); 959 } 960 961 return dev; 962 } 963 EXPORT_SYMBOL(iio_device_alloc); 964 965 void iio_device_free(struct iio_dev *dev) 966 { 967 if (dev) 968 put_device(&dev->dev); 969 } 970 EXPORT_SYMBOL(iio_device_free); 971 972 static void devm_iio_device_release(struct device *dev, void *res) 973 { 974 iio_device_free(*(struct iio_dev **)res); 975 } 976 977 static int devm_iio_device_match(struct device *dev, void *res, void *data) 978 { 979 struct iio_dev **r = res; 980 if (!r || !*r) { 981 WARN_ON(!r || !*r); 982 return 0; 983 } 984 return *r == data; 985 } 986 987 struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv) 988 { 989 struct iio_dev **ptr, *iio_dev; 990 991 ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr), 992 GFP_KERNEL); 993 if (!ptr) 994 return NULL; 995 996 /* use raw alloc_dr for kmalloc caller tracing */ 997 iio_dev = iio_device_alloc(sizeof_priv); 998 if (iio_dev) { 999 *ptr = iio_dev; 1000 devres_add(dev, ptr); 1001 } else { 1002 devres_free(ptr); 1003 } 1004 1005 return iio_dev; 1006 } 1007 EXPORT_SYMBOL_GPL(devm_iio_device_alloc); 1008 1009 void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev) 1010 { 1011 int rc; 1012 1013 rc = devres_release(dev, devm_iio_device_release, 1014 devm_iio_device_match, iio_dev); 1015 WARN_ON(rc); 1016 } 1017 EXPORT_SYMBOL_GPL(devm_iio_device_free); 1018 1019 /** 1020 * iio_chrdev_open() - chrdev file open for buffer access and ioctls 1021 **/ 1022 static int iio_chrdev_open(struct inode *inode, struct file *filp) 1023 { 1024 struct iio_dev *indio_dev = container_of(inode->i_cdev, 1025 struct iio_dev, chrdev); 1026 1027 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags)) 1028 return -EBUSY; 1029 1030 iio_device_get(indio_dev); 1031 1032 filp->private_data = indio_dev; 1033 1034 return 0; 1035 } 1036 1037 /** 1038 * iio_chrdev_release() - chrdev file close buffer access and ioctls 1039 **/ 1040 static int iio_chrdev_release(struct inode *inode, struct file *filp) 1041 { 1042 struct iio_dev *indio_dev = container_of(inode->i_cdev, 1043 struct iio_dev, chrdev); 1044 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags); 1045 iio_device_put(indio_dev); 1046 1047 return 0; 1048 } 1049 1050 /* Somewhat of a cross file organization violation - ioctls here are actually 1051 * event related */ 1052 static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1053 { 1054 struct iio_dev *indio_dev = filp->private_data; 1055 int __user *ip = (int __user *)arg; 1056 int fd; 1057 1058 if (!indio_dev->info) 1059 return -ENODEV; 1060 1061 if (cmd == IIO_GET_EVENT_FD_IOCTL) { 1062 fd = iio_event_getfd(indio_dev); 1063 if (copy_to_user(ip, &fd, sizeof(fd))) 1064 return -EFAULT; 1065 return 0; 1066 } 1067 return -EINVAL; 1068 } 1069 1070 static const struct file_operations iio_buffer_fileops = { 1071 .read = iio_buffer_read_first_n_outer_addr, 1072 .release = iio_chrdev_release, 1073 .open = iio_chrdev_open, 1074 .poll = iio_buffer_poll_addr, 1075 .owner = THIS_MODULE, 1076 .llseek = noop_llseek, 1077 .unlocked_ioctl = iio_ioctl, 1078 .compat_ioctl = iio_ioctl, 1079 }; 1080 1081 static const struct iio_buffer_setup_ops noop_ring_setup_ops; 1082 1083 int iio_device_register(struct iio_dev *indio_dev) 1084 { 1085 int ret; 1086 1087 /* If the calling driver did not initialize of_node, do it here */ 1088 if (!indio_dev->dev.of_node && indio_dev->dev.parent) 1089 indio_dev->dev.of_node = indio_dev->dev.parent->of_node; 1090 1091 /* configure elements for the chrdev */ 1092 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id); 1093 1094 ret = iio_device_register_debugfs(indio_dev); 1095 if (ret) { 1096 dev_err(indio_dev->dev.parent, 1097 "Failed to register debugfs interfaces\n"); 1098 goto error_ret; 1099 } 1100 ret = iio_device_register_sysfs(indio_dev); 1101 if (ret) { 1102 dev_err(indio_dev->dev.parent, 1103 "Failed to register sysfs interfaces\n"); 1104 goto error_unreg_debugfs; 1105 } 1106 ret = iio_device_register_eventset(indio_dev); 1107 if (ret) { 1108 dev_err(indio_dev->dev.parent, 1109 "Failed to register event set\n"); 1110 goto error_free_sysfs; 1111 } 1112 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) 1113 iio_device_register_trigger_consumer(indio_dev); 1114 1115 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && 1116 indio_dev->setup_ops == NULL) 1117 indio_dev->setup_ops = &noop_ring_setup_ops; 1118 1119 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops); 1120 indio_dev->chrdev.owner = indio_dev->info->driver_module; 1121 indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj; 1122 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1); 1123 if (ret < 0) 1124 goto error_unreg_eventset; 1125 1126 ret = device_add(&indio_dev->dev); 1127 if (ret < 0) 1128 goto error_cdev_del; 1129 1130 return 0; 1131 error_cdev_del: 1132 cdev_del(&indio_dev->chrdev); 1133 error_unreg_eventset: 1134 iio_device_unregister_eventset(indio_dev); 1135 error_free_sysfs: 1136 iio_device_unregister_sysfs(indio_dev); 1137 error_unreg_debugfs: 1138 iio_device_unregister_debugfs(indio_dev); 1139 error_ret: 1140 return ret; 1141 } 1142 EXPORT_SYMBOL(iio_device_register); 1143 1144 void iio_device_unregister(struct iio_dev *indio_dev) 1145 { 1146 mutex_lock(&indio_dev->info_exist_lock); 1147 1148 device_del(&indio_dev->dev); 1149 1150 if (indio_dev->chrdev.dev) 1151 cdev_del(&indio_dev->chrdev); 1152 iio_device_unregister_debugfs(indio_dev); 1153 1154 iio_disable_all_buffers(indio_dev); 1155 1156 indio_dev->info = NULL; 1157 1158 iio_device_wakeup_eventset(indio_dev); 1159 iio_buffer_wakeup_poll(indio_dev); 1160 1161 mutex_unlock(&indio_dev->info_exist_lock); 1162 } 1163 EXPORT_SYMBOL(iio_device_unregister); 1164 subsys_initcall(iio_init); 1165 module_exit(iio_exit); 1166 1167 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 1168 MODULE_DESCRIPTION("Industrial I/O core"); 1169 MODULE_LICENSE("GPL"); 1170