1 /* 2 * Copyright (C) 2007-2009 Luca Tettamanti <kronos.it@gmail.com> 3 * 4 * This file is released under the GPLv2 5 * See COPYING in the top level directory of the kernel tree. 6 */ 7 8 #include <linux/debugfs.h> 9 #include <linux/kernel.h> 10 #include <linux/hwmon.h> 11 #include <linux/list.h> 12 #include <linux/module.h> 13 14 #include <acpi/acpi.h> 15 #include <acpi/acpixf.h> 16 #include <acpi/acpi_drivers.h> 17 #include <acpi/acpi_bus.h> 18 19 20 #define ATK_HID "ATK0110" 21 22 /* Minimum time between readings, enforced in order to avoid 23 * hogging the CPU. 24 */ 25 #define CACHE_TIME HZ 26 27 #define BOARD_ID "MBIF" 28 #define METHOD_ENUMERATE "GGRP" 29 #define METHOD_READ "GITM" 30 #define METHOD_WRITE "SITM" 31 #define METHOD_OLD_READ_TMP "RTMP" 32 #define METHOD_OLD_READ_VLT "RVLT" 33 #define METHOD_OLD_READ_FAN "RFAN" 34 #define METHOD_OLD_ENUM_TMP "TSIF" 35 #define METHOD_OLD_ENUM_VLT "VSIF" 36 #define METHOD_OLD_ENUM_FAN "FSIF" 37 38 #define ATK_MUX_HWMON 0x00000006ULL 39 #define ATK_MUX_MGMT 0x00000011ULL 40 41 #define ATK_CLASS_MASK 0xff000000ULL 42 #define ATK_CLASS_FREQ_CTL 0x03000000ULL 43 #define ATK_CLASS_FAN_CTL 0x04000000ULL 44 #define ATK_CLASS_HWMON 0x06000000ULL 45 #define ATK_CLASS_MGMT 0x11000000ULL 46 47 #define ATK_TYPE_MASK 0x00ff0000ULL 48 #define HWMON_TYPE_VOLT 0x00020000ULL 49 #define HWMON_TYPE_TEMP 0x00030000ULL 50 #define HWMON_TYPE_FAN 0x00040000ULL 51 52 #define ATK_ELEMENT_ID_MASK 0x0000ffffULL 53 54 #define ATK_EC_ID 0x11060004ULL 55 56 enum atk_pack_member { 57 HWMON_PACK_FLAGS, 58 HWMON_PACK_NAME, 59 HWMON_PACK_LIMIT1, 60 HWMON_PACK_LIMIT2, 61 HWMON_PACK_ENABLE 62 }; 63 64 /* New package format */ 65 #define _HWMON_NEW_PACK_SIZE 7 66 #define _HWMON_NEW_PACK_FLAGS 0 67 #define _HWMON_NEW_PACK_NAME 1 68 #define _HWMON_NEW_PACK_UNK1 2 69 #define _HWMON_NEW_PACK_UNK2 3 70 #define _HWMON_NEW_PACK_LIMIT1 4 71 #define _HWMON_NEW_PACK_LIMIT2 5 72 #define _HWMON_NEW_PACK_ENABLE 6 73 74 /* Old package format */ 75 #define _HWMON_OLD_PACK_SIZE 5 76 #define _HWMON_OLD_PACK_FLAGS 0 77 #define _HWMON_OLD_PACK_NAME 1 78 #define _HWMON_OLD_PACK_LIMIT1 2 79 #define _HWMON_OLD_PACK_LIMIT2 3 80 #define _HWMON_OLD_PACK_ENABLE 4 81 82 83 struct atk_data { 84 struct device *hwmon_dev; 85 acpi_handle atk_handle; 86 struct acpi_device *acpi_dev; 87 88 bool old_interface; 89 90 /* old interface */ 91 acpi_handle rtmp_handle; 92 acpi_handle rvlt_handle; 93 acpi_handle rfan_handle; 94 /* new inteface */ 95 acpi_handle enumerate_handle; 96 acpi_handle read_handle; 97 acpi_handle write_handle; 98 99 bool disable_ec; 100 101 int voltage_count; 102 int temperature_count; 103 int fan_count; 104 struct list_head sensor_list; 105 106 struct { 107 struct dentry *root; 108 u32 id; 109 } debugfs; 110 }; 111 112 113 typedef ssize_t (*sysfs_show_func)(struct device *dev, 114 struct device_attribute *attr, char *buf); 115 116 static const struct acpi_device_id atk_ids[] = { 117 {ATK_HID, 0}, 118 {"", 0}, 119 }; 120 MODULE_DEVICE_TABLE(acpi, atk_ids); 121 122 #define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */ 123 124 struct atk_sensor_data { 125 struct list_head list; 126 struct atk_data *data; 127 struct device_attribute label_attr; 128 struct device_attribute input_attr; 129 struct device_attribute limit1_attr; 130 struct device_attribute limit2_attr; 131 char label_attr_name[ATTR_NAME_SIZE]; 132 char input_attr_name[ATTR_NAME_SIZE]; 133 char limit1_attr_name[ATTR_NAME_SIZE]; 134 char limit2_attr_name[ATTR_NAME_SIZE]; 135 u64 id; 136 u64 type; 137 u64 limit1; 138 u64 limit2; 139 u64 cached_value; 140 unsigned long last_updated; /* in jiffies */ 141 bool is_valid; 142 char const *acpi_name; 143 }; 144 145 /* Return buffer format: 146 * [0-3] "value" is valid flag 147 * [4-7] value 148 * [8- ] unknown stuff on newer mobos 149 */ 150 struct atk_acpi_ret_buffer { 151 u32 flags; 152 u32 value; 153 u8 data[]; 154 }; 155 156 /* Input buffer used for GITM and SITM methods */ 157 struct atk_acpi_input_buf { 158 u32 id; 159 u32 param1; 160 u32 param2; 161 }; 162 163 static int atk_add(struct acpi_device *device); 164 static int atk_remove(struct acpi_device *device, int type); 165 static void atk_print_sensor(struct atk_data *data, union acpi_object *obj); 166 static int atk_read_value(struct atk_sensor_data *sensor, u64 *value); 167 static void atk_free_sensors(struct atk_data *data); 168 169 static struct acpi_driver atk_driver = { 170 .name = ATK_HID, 171 .class = "hwmon", 172 .ids = atk_ids, 173 .ops = { 174 .add = atk_add, 175 .remove = atk_remove, 176 }, 177 }; 178 179 #define input_to_atk_sensor(attr) \ 180 container_of(attr, struct atk_sensor_data, input_attr) 181 182 #define label_to_atk_sensor(attr) \ 183 container_of(attr, struct atk_sensor_data, label_attr) 184 185 #define limit1_to_atk_sensor(attr) \ 186 container_of(attr, struct atk_sensor_data, limit1_attr) 187 188 #define limit2_to_atk_sensor(attr) \ 189 container_of(attr, struct atk_sensor_data, limit2_attr) 190 191 static ssize_t atk_input_show(struct device *dev, 192 struct device_attribute *attr, char *buf) 193 { 194 struct atk_sensor_data *s = input_to_atk_sensor(attr); 195 u64 value; 196 int err; 197 198 err = atk_read_value(s, &value); 199 if (err) 200 return err; 201 202 if (s->type == HWMON_TYPE_TEMP) 203 /* ACPI returns decidegree */ 204 value *= 100; 205 206 return sprintf(buf, "%llu\n", value); 207 } 208 209 static ssize_t atk_label_show(struct device *dev, 210 struct device_attribute *attr, char *buf) 211 { 212 struct atk_sensor_data *s = label_to_atk_sensor(attr); 213 214 return sprintf(buf, "%s\n", s->acpi_name); 215 } 216 217 static ssize_t atk_limit1_show(struct device *dev, 218 struct device_attribute *attr, char *buf) 219 { 220 struct atk_sensor_data *s = limit1_to_atk_sensor(attr); 221 u64 value = s->limit1; 222 223 if (s->type == HWMON_TYPE_TEMP) 224 value *= 100; 225 226 return sprintf(buf, "%lld\n", value); 227 } 228 229 static ssize_t atk_limit2_show(struct device *dev, 230 struct device_attribute *attr, char *buf) 231 { 232 struct atk_sensor_data *s = limit2_to_atk_sensor(attr); 233 u64 value = s->limit2; 234 235 if (s->type == HWMON_TYPE_TEMP) 236 value *= 100; 237 238 return sprintf(buf, "%lld\n", value); 239 } 240 241 static ssize_t atk_name_show(struct device *dev, 242 struct device_attribute *attr, char *buf) 243 { 244 return sprintf(buf, "atk0110\n"); 245 } 246 static struct device_attribute atk_name_attr = 247 __ATTR(name, 0444, atk_name_show, NULL); 248 249 static void atk_init_attribute(struct device_attribute *attr, char *name, 250 sysfs_show_func show) 251 { 252 attr->attr.name = name; 253 attr->attr.mode = 0444; 254 attr->show = show; 255 attr->store = NULL; 256 } 257 258 259 static union acpi_object *atk_get_pack_member(struct atk_data *data, 260 union acpi_object *pack, 261 enum atk_pack_member m) 262 { 263 bool old_if = data->old_interface; 264 int offset; 265 266 switch (m) { 267 case HWMON_PACK_FLAGS: 268 offset = old_if ? _HWMON_OLD_PACK_FLAGS : _HWMON_NEW_PACK_FLAGS; 269 break; 270 case HWMON_PACK_NAME: 271 offset = old_if ? _HWMON_OLD_PACK_NAME : _HWMON_NEW_PACK_NAME; 272 break; 273 case HWMON_PACK_LIMIT1: 274 offset = old_if ? _HWMON_OLD_PACK_LIMIT1 : 275 _HWMON_NEW_PACK_LIMIT1; 276 break; 277 case HWMON_PACK_LIMIT2: 278 offset = old_if ? _HWMON_OLD_PACK_LIMIT2 : 279 _HWMON_NEW_PACK_LIMIT2; 280 break; 281 case HWMON_PACK_ENABLE: 282 offset = old_if ? _HWMON_OLD_PACK_ENABLE : 283 _HWMON_NEW_PACK_ENABLE; 284 break; 285 default: 286 return NULL; 287 } 288 289 return &pack->package.elements[offset]; 290 } 291 292 293 /* New package format is: 294 * - flag (int) 295 * class - used for de-muxing the request to the correct GITn 296 * type (volt, temp, fan) 297 * sensor id | 298 * sensor id - used for de-muxing the request _inside_ the GITn 299 * - name (str) 300 * - unknown (int) 301 * - unknown (int) 302 * - limit1 (int) 303 * - limit2 (int) 304 * - enable (int) 305 * 306 * The old package has the same format but it's missing the two unknown fields. 307 */ 308 static int validate_hwmon_pack(struct atk_data *data, union acpi_object *obj) 309 { 310 struct device *dev = &data->acpi_dev->dev; 311 union acpi_object *tmp; 312 bool old_if = data->old_interface; 313 int const expected_size = old_if ? _HWMON_OLD_PACK_SIZE : 314 _HWMON_NEW_PACK_SIZE; 315 316 if (obj->type != ACPI_TYPE_PACKAGE) { 317 dev_warn(dev, "Invalid type: %d\n", obj->type); 318 return -EINVAL; 319 } 320 321 if (obj->package.count != expected_size) { 322 dev_warn(dev, "Invalid package size: %d, expected: %d\n", 323 obj->package.count, expected_size); 324 return -EINVAL; 325 } 326 327 tmp = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS); 328 if (tmp->type != ACPI_TYPE_INTEGER) { 329 dev_warn(dev, "Invalid type (flag): %d\n", tmp->type); 330 return -EINVAL; 331 } 332 333 tmp = atk_get_pack_member(data, obj, HWMON_PACK_NAME); 334 if (tmp->type != ACPI_TYPE_STRING) { 335 dev_warn(dev, "Invalid type (name): %d\n", tmp->type); 336 return -EINVAL; 337 } 338 339 /* Don't check... we don't know what they're useful for anyway */ 340 #if 0 341 tmp = &obj->package.elements[HWMON_PACK_UNK1]; 342 if (tmp->type != ACPI_TYPE_INTEGER) { 343 dev_warn(dev, "Invalid type (unk1): %d\n", tmp->type); 344 return -EINVAL; 345 } 346 347 tmp = &obj->package.elements[HWMON_PACK_UNK2]; 348 if (tmp->type != ACPI_TYPE_INTEGER) { 349 dev_warn(dev, "Invalid type (unk2): %d\n", tmp->type); 350 return -EINVAL; 351 } 352 #endif 353 354 tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1); 355 if (tmp->type != ACPI_TYPE_INTEGER) { 356 dev_warn(dev, "Invalid type (limit1): %d\n", tmp->type); 357 return -EINVAL; 358 } 359 360 tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2); 361 if (tmp->type != ACPI_TYPE_INTEGER) { 362 dev_warn(dev, "Invalid type (limit2): %d\n", tmp->type); 363 return -EINVAL; 364 } 365 366 tmp = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE); 367 if (tmp->type != ACPI_TYPE_INTEGER) { 368 dev_warn(dev, "Invalid type (enable): %d\n", tmp->type); 369 return -EINVAL; 370 } 371 372 atk_print_sensor(data, obj); 373 374 return 0; 375 } 376 377 #ifdef DEBUG 378 static char const *atk_sensor_type(union acpi_object *flags) 379 { 380 u64 type = flags->integer.value & ATK_TYPE_MASK; 381 char const *what; 382 383 switch (type) { 384 case HWMON_TYPE_VOLT: 385 what = "voltage"; 386 break; 387 case HWMON_TYPE_TEMP: 388 what = "temperature"; 389 break; 390 case HWMON_TYPE_FAN: 391 what = "fan"; 392 break; 393 default: 394 what = "unknown"; 395 break; 396 } 397 398 return what; 399 } 400 #endif 401 402 static void atk_print_sensor(struct atk_data *data, union acpi_object *obj) 403 { 404 #ifdef DEBUG 405 struct device *dev = &data->acpi_dev->dev; 406 union acpi_object *flags; 407 union acpi_object *name; 408 union acpi_object *limit1; 409 union acpi_object *limit2; 410 union acpi_object *enable; 411 char const *what; 412 413 flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS); 414 name = atk_get_pack_member(data, obj, HWMON_PACK_NAME); 415 limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1); 416 limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2); 417 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE); 418 419 what = atk_sensor_type(flags); 420 421 dev_dbg(dev, "%s: %#llx %s [%llu-%llu] %s\n", what, 422 flags->integer.value, 423 name->string.pointer, 424 limit1->integer.value, limit2->integer.value, 425 enable->integer.value ? "enabled" : "disabled"); 426 #endif 427 } 428 429 static int atk_read_value_old(struct atk_sensor_data *sensor, u64 *value) 430 { 431 struct atk_data *data = sensor->data; 432 struct device *dev = &data->acpi_dev->dev; 433 struct acpi_object_list params; 434 union acpi_object id; 435 acpi_status status; 436 acpi_handle method; 437 438 switch (sensor->type) { 439 case HWMON_TYPE_VOLT: 440 method = data->rvlt_handle; 441 break; 442 case HWMON_TYPE_TEMP: 443 method = data->rtmp_handle; 444 break; 445 case HWMON_TYPE_FAN: 446 method = data->rfan_handle; 447 break; 448 default: 449 return -EINVAL; 450 } 451 452 id.type = ACPI_TYPE_INTEGER; 453 id.integer.value = sensor->id; 454 455 params.count = 1; 456 params.pointer = &id; 457 458 status = acpi_evaluate_integer(method, NULL, ¶ms, value); 459 if (status != AE_OK) { 460 dev_warn(dev, "%s: ACPI exception: %s\n", __func__, 461 acpi_format_exception(status)); 462 return -EIO; 463 } 464 465 return 0; 466 } 467 468 static union acpi_object *atk_ggrp(struct atk_data *data, u16 mux) 469 { 470 struct device *dev = &data->acpi_dev->dev; 471 struct acpi_buffer buf; 472 acpi_status ret; 473 struct acpi_object_list params; 474 union acpi_object id; 475 union acpi_object *pack; 476 477 id.type = ACPI_TYPE_INTEGER; 478 id.integer.value = mux; 479 params.count = 1; 480 params.pointer = &id; 481 482 buf.length = ACPI_ALLOCATE_BUFFER; 483 ret = acpi_evaluate_object(data->enumerate_handle, NULL, ¶ms, &buf); 484 if (ret != AE_OK) { 485 dev_err(dev, "GGRP[%#x] ACPI exception: %s\n", mux, 486 acpi_format_exception(ret)); 487 return ERR_PTR(-EIO); 488 } 489 pack = buf.pointer; 490 if (pack->type != ACPI_TYPE_PACKAGE) { 491 /* Execution was successful, but the id was not found */ 492 ACPI_FREE(pack); 493 return ERR_PTR(-ENOENT); 494 } 495 496 if (pack->package.count < 1) { 497 dev_err(dev, "GGRP[%#x] package is too small\n", mux); 498 ACPI_FREE(pack); 499 return ERR_PTR(-EIO); 500 } 501 return pack; 502 } 503 504 static union acpi_object *atk_gitm(struct atk_data *data, u64 id) 505 { 506 struct device *dev = &data->acpi_dev->dev; 507 struct atk_acpi_input_buf buf; 508 union acpi_object tmp; 509 struct acpi_object_list params; 510 struct acpi_buffer ret; 511 union acpi_object *obj; 512 acpi_status status; 513 514 buf.id = id; 515 buf.param1 = 0; 516 buf.param2 = 0; 517 518 tmp.type = ACPI_TYPE_BUFFER; 519 tmp.buffer.pointer = (u8 *)&buf; 520 tmp.buffer.length = sizeof(buf); 521 522 params.count = 1; 523 params.pointer = (void *)&tmp; 524 525 ret.length = ACPI_ALLOCATE_BUFFER; 526 status = acpi_evaluate_object_typed(data->read_handle, NULL, ¶ms, 527 &ret, ACPI_TYPE_BUFFER); 528 if (status != AE_OK) { 529 dev_warn(dev, "GITM[%#llx] ACPI exception: %s\n", id, 530 acpi_format_exception(status)); 531 return ERR_PTR(-EIO); 532 } 533 obj = ret.pointer; 534 535 /* Sanity check */ 536 if (obj->buffer.length < 8) { 537 dev_warn(dev, "Unexpected ASBF length: %u\n", 538 obj->buffer.length); 539 ACPI_FREE(obj); 540 return ERR_PTR(-EIO); 541 } 542 return obj; 543 } 544 545 static union acpi_object *atk_sitm(struct atk_data *data, 546 struct atk_acpi_input_buf *buf) 547 { 548 struct device *dev = &data->acpi_dev->dev; 549 struct acpi_object_list params; 550 union acpi_object tmp; 551 struct acpi_buffer ret; 552 union acpi_object *obj; 553 acpi_status status; 554 555 tmp.type = ACPI_TYPE_BUFFER; 556 tmp.buffer.pointer = (u8 *)buf; 557 tmp.buffer.length = sizeof(*buf); 558 559 params.count = 1; 560 params.pointer = &tmp; 561 562 ret.length = ACPI_ALLOCATE_BUFFER; 563 status = acpi_evaluate_object_typed(data->write_handle, NULL, ¶ms, 564 &ret, ACPI_TYPE_BUFFER); 565 if (status != AE_OK) { 566 dev_warn(dev, "SITM[%#x] ACPI exception: %s\n", buf->id, 567 acpi_format_exception(status)); 568 return ERR_PTR(-EIO); 569 } 570 obj = ret.pointer; 571 572 /* Sanity check */ 573 if (obj->buffer.length < 8) { 574 dev_warn(dev, "Unexpected ASBF length: %u\n", 575 obj->buffer.length); 576 ACPI_FREE(obj); 577 return ERR_PTR(-EIO); 578 } 579 return obj; 580 } 581 582 static int atk_read_value_new(struct atk_sensor_data *sensor, u64 *value) 583 { 584 struct atk_data *data = sensor->data; 585 struct device *dev = &data->acpi_dev->dev; 586 union acpi_object *obj; 587 struct atk_acpi_ret_buffer *buf; 588 int err = 0; 589 590 obj = atk_gitm(data, sensor->id); 591 if (IS_ERR(obj)) 592 return PTR_ERR(obj); 593 594 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer; 595 if (buf->flags == 0) { 596 /* The reading is not valid, possible causes: 597 * - sensor failure 598 * - enumeration was FUBAR (and we didn't notice) 599 */ 600 dev_warn(dev, "Read failed, sensor = %#llx\n", sensor->id); 601 err = -EIO; 602 goto out; 603 } 604 605 *value = buf->value; 606 out: 607 ACPI_FREE(obj); 608 return err; 609 } 610 611 static int atk_read_value(struct atk_sensor_data *sensor, u64 *value) 612 { 613 int err; 614 615 if (!sensor->is_valid || 616 time_after(jiffies, sensor->last_updated + CACHE_TIME)) { 617 if (sensor->data->old_interface) 618 err = atk_read_value_old(sensor, value); 619 else 620 err = atk_read_value_new(sensor, value); 621 622 sensor->is_valid = true; 623 sensor->last_updated = jiffies; 624 sensor->cached_value = *value; 625 } else { 626 *value = sensor->cached_value; 627 err = 0; 628 } 629 630 return err; 631 } 632 633 #ifdef CONFIG_DEBUG_FS 634 static int atk_debugfs_gitm_get(void *p, u64 *val) 635 { 636 struct atk_data *data = p; 637 union acpi_object *ret; 638 struct atk_acpi_ret_buffer *buf; 639 int err = 0; 640 641 if (!data->read_handle) 642 return -ENODEV; 643 644 if (!data->debugfs.id) 645 return -EINVAL; 646 647 ret = atk_gitm(data, data->debugfs.id); 648 if (IS_ERR(ret)) 649 return PTR_ERR(ret); 650 651 buf = (struct atk_acpi_ret_buffer *)ret->buffer.pointer; 652 if (buf->flags) 653 *val = buf->value; 654 else 655 err = -EIO; 656 657 return err; 658 } 659 660 DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm, 661 atk_debugfs_gitm_get, 662 NULL, 663 "0x%08llx\n") 664 665 static int atk_acpi_print(char *buf, size_t sz, union acpi_object *obj) 666 { 667 int ret = 0; 668 669 switch (obj->type) { 670 case ACPI_TYPE_INTEGER: 671 ret = snprintf(buf, sz, "0x%08llx\n", obj->integer.value); 672 break; 673 case ACPI_TYPE_STRING: 674 ret = snprintf(buf, sz, "%s\n", obj->string.pointer); 675 break; 676 } 677 678 return ret; 679 } 680 681 static void atk_pack_print(char *buf, size_t sz, union acpi_object *pack) 682 { 683 int ret; 684 int i; 685 686 for (i = 0; i < pack->package.count; i++) { 687 union acpi_object *obj = &pack->package.elements[i]; 688 689 ret = atk_acpi_print(buf, sz, obj); 690 if (ret >= sz) 691 break; 692 buf += ret; 693 sz -= ret; 694 } 695 } 696 697 static int atk_debugfs_ggrp_open(struct inode *inode, struct file *file) 698 { 699 struct atk_data *data = inode->i_private; 700 char *buf = NULL; 701 union acpi_object *ret; 702 u8 cls; 703 int i; 704 705 if (!data->enumerate_handle) 706 return -ENODEV; 707 if (!data->debugfs.id) 708 return -EINVAL; 709 710 cls = (data->debugfs.id & 0xff000000) >> 24; 711 ret = atk_ggrp(data, cls); 712 if (IS_ERR(ret)) 713 return PTR_ERR(ret); 714 715 for (i = 0; i < ret->package.count; i++) { 716 union acpi_object *pack = &ret->package.elements[i]; 717 union acpi_object *id; 718 719 if (pack->type != ACPI_TYPE_PACKAGE) 720 continue; 721 if (!pack->package.count) 722 continue; 723 id = &pack->package.elements[0]; 724 if (id->integer.value == data->debugfs.id) { 725 /* Print the package */ 726 buf = kzalloc(512, GFP_KERNEL); 727 if (!buf) { 728 ACPI_FREE(ret); 729 return -ENOMEM; 730 } 731 atk_pack_print(buf, 512, pack); 732 break; 733 } 734 } 735 ACPI_FREE(ret); 736 737 if (!buf) 738 return -EINVAL; 739 740 file->private_data = buf; 741 742 return nonseekable_open(inode, file); 743 } 744 745 static ssize_t atk_debugfs_ggrp_read(struct file *file, char __user *buf, 746 size_t count, loff_t *pos) 747 { 748 char *str = file->private_data; 749 size_t len = strlen(str); 750 751 return simple_read_from_buffer(buf, count, pos, str, len); 752 } 753 754 static int atk_debugfs_ggrp_release(struct inode *inode, struct file *file) 755 { 756 kfree(file->private_data); 757 return 0; 758 } 759 760 static const struct file_operations atk_debugfs_ggrp_fops = { 761 .read = atk_debugfs_ggrp_read, 762 .open = atk_debugfs_ggrp_open, 763 .release = atk_debugfs_ggrp_release, 764 }; 765 766 static void atk_debugfs_init(struct atk_data *data) 767 { 768 struct dentry *d; 769 struct dentry *f; 770 771 data->debugfs.id = 0; 772 773 d = debugfs_create_dir("asus_atk0110", NULL); 774 if (!d || IS_ERR(d)) 775 return; 776 777 f = debugfs_create_x32("id", S_IRUSR | S_IWUSR, d, &data->debugfs.id); 778 if (!f || IS_ERR(f)) 779 goto cleanup; 780 781 f = debugfs_create_file("gitm", S_IRUSR, d, data, 782 &atk_debugfs_gitm); 783 if (!f || IS_ERR(f)) 784 goto cleanup; 785 786 f = debugfs_create_file("ggrp", S_IRUSR, d, data, 787 &atk_debugfs_ggrp_fops); 788 if (!f || IS_ERR(f)) 789 goto cleanup; 790 791 data->debugfs.root = d; 792 793 return; 794 cleanup: 795 debugfs_remove_recursive(d); 796 } 797 798 static void atk_debugfs_cleanup(struct atk_data *data) 799 { 800 debugfs_remove_recursive(data->debugfs.root); 801 } 802 803 #else /* CONFIG_DEBUG_FS */ 804 805 static void atk_debugfs_init(struct atk_data *data) 806 { 807 } 808 809 static void atk_debugfs_cleanup(struct atk_data *data) 810 { 811 } 812 #endif 813 814 static int atk_add_sensor(struct atk_data *data, union acpi_object *obj) 815 { 816 struct device *dev = &data->acpi_dev->dev; 817 union acpi_object *flags; 818 union acpi_object *name; 819 union acpi_object *limit1; 820 union acpi_object *limit2; 821 union acpi_object *enable; 822 struct atk_sensor_data *sensor; 823 char const *base_name; 824 char const *limit1_name; 825 char const *limit2_name; 826 u64 type; 827 int err; 828 int *num; 829 int start; 830 831 if (obj->type != ACPI_TYPE_PACKAGE) { 832 /* wft is this? */ 833 dev_warn(dev, "Unknown type for ACPI object: (%d)\n", 834 obj->type); 835 return -EINVAL; 836 } 837 838 err = validate_hwmon_pack(data, obj); 839 if (err) 840 return err; 841 842 /* Ok, we have a valid hwmon package */ 843 type = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS)->integer.value 844 & ATK_TYPE_MASK; 845 846 switch (type) { 847 case HWMON_TYPE_VOLT: 848 base_name = "in"; 849 limit1_name = "min"; 850 limit2_name = "max"; 851 num = &data->voltage_count; 852 start = 0; 853 break; 854 case HWMON_TYPE_TEMP: 855 base_name = "temp"; 856 limit1_name = "max"; 857 limit2_name = "crit"; 858 num = &data->temperature_count; 859 start = 1; 860 break; 861 case HWMON_TYPE_FAN: 862 base_name = "fan"; 863 limit1_name = "min"; 864 limit2_name = "max"; 865 num = &data->fan_count; 866 start = 1; 867 break; 868 default: 869 dev_warn(dev, "Unknown sensor type: %#llx\n", type); 870 return -EINVAL; 871 } 872 873 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE); 874 if (!enable->integer.value) 875 /* sensor is disabled */ 876 return 0; 877 878 flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS); 879 name = atk_get_pack_member(data, obj, HWMON_PACK_NAME); 880 limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1); 881 limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2); 882 883 sensor = kzalloc(sizeof(*sensor), GFP_KERNEL); 884 if (!sensor) 885 return -ENOMEM; 886 887 sensor->acpi_name = kstrdup(name->string.pointer, GFP_KERNEL); 888 if (!sensor->acpi_name) { 889 err = -ENOMEM; 890 goto out; 891 } 892 893 INIT_LIST_HEAD(&sensor->list); 894 sensor->type = type; 895 sensor->data = data; 896 sensor->id = flags->integer.value; 897 sensor->limit1 = limit1->integer.value; 898 if (data->old_interface) 899 sensor->limit2 = limit2->integer.value; 900 else 901 /* The upper limit is expressed as delta from lower limit */ 902 sensor->limit2 = sensor->limit1 + limit2->integer.value; 903 904 snprintf(sensor->input_attr_name, ATTR_NAME_SIZE, 905 "%s%d_input", base_name, start + *num); 906 atk_init_attribute(&sensor->input_attr, 907 sensor->input_attr_name, 908 atk_input_show); 909 910 snprintf(sensor->label_attr_name, ATTR_NAME_SIZE, 911 "%s%d_label", base_name, start + *num); 912 atk_init_attribute(&sensor->label_attr, 913 sensor->label_attr_name, 914 atk_label_show); 915 916 snprintf(sensor->limit1_attr_name, ATTR_NAME_SIZE, 917 "%s%d_%s", base_name, start + *num, limit1_name); 918 atk_init_attribute(&sensor->limit1_attr, 919 sensor->limit1_attr_name, 920 atk_limit1_show); 921 922 snprintf(sensor->limit2_attr_name, ATTR_NAME_SIZE, 923 "%s%d_%s", base_name, start + *num, limit2_name); 924 atk_init_attribute(&sensor->limit2_attr, 925 sensor->limit2_attr_name, 926 atk_limit2_show); 927 928 list_add(&sensor->list, &data->sensor_list); 929 (*num)++; 930 931 return 1; 932 out: 933 kfree(sensor->acpi_name); 934 kfree(sensor); 935 return err; 936 } 937 938 static int atk_enumerate_old_hwmon(struct atk_data *data) 939 { 940 struct device *dev = &data->acpi_dev->dev; 941 struct acpi_buffer buf; 942 union acpi_object *pack; 943 acpi_status status; 944 int i, ret; 945 int count = 0; 946 947 /* Voltages */ 948 buf.length = ACPI_ALLOCATE_BUFFER; 949 status = acpi_evaluate_object_typed(data->atk_handle, 950 METHOD_OLD_ENUM_VLT, NULL, &buf, ACPI_TYPE_PACKAGE); 951 if (status != AE_OK) { 952 dev_warn(dev, METHOD_OLD_ENUM_VLT ": ACPI exception: %s\n", 953 acpi_format_exception(status)); 954 955 return -ENODEV; 956 } 957 958 pack = buf.pointer; 959 for (i = 1; i < pack->package.count; i++) { 960 union acpi_object *obj = &pack->package.elements[i]; 961 962 ret = atk_add_sensor(data, obj); 963 if (ret > 0) 964 count++; 965 } 966 ACPI_FREE(buf.pointer); 967 968 /* Temperatures */ 969 buf.length = ACPI_ALLOCATE_BUFFER; 970 status = acpi_evaluate_object_typed(data->atk_handle, 971 METHOD_OLD_ENUM_TMP, NULL, &buf, ACPI_TYPE_PACKAGE); 972 if (status != AE_OK) { 973 dev_warn(dev, METHOD_OLD_ENUM_TMP ": ACPI exception: %s\n", 974 acpi_format_exception(status)); 975 976 ret = -ENODEV; 977 goto cleanup; 978 } 979 980 pack = buf.pointer; 981 for (i = 1; i < pack->package.count; i++) { 982 union acpi_object *obj = &pack->package.elements[i]; 983 984 ret = atk_add_sensor(data, obj); 985 if (ret > 0) 986 count++; 987 } 988 ACPI_FREE(buf.pointer); 989 990 /* Fans */ 991 buf.length = ACPI_ALLOCATE_BUFFER; 992 status = acpi_evaluate_object_typed(data->atk_handle, 993 METHOD_OLD_ENUM_FAN, NULL, &buf, ACPI_TYPE_PACKAGE); 994 if (status != AE_OK) { 995 dev_warn(dev, METHOD_OLD_ENUM_FAN ": ACPI exception: %s\n", 996 acpi_format_exception(status)); 997 998 ret = -ENODEV; 999 goto cleanup; 1000 } 1001 1002 pack = buf.pointer; 1003 for (i = 1; i < pack->package.count; i++) { 1004 union acpi_object *obj = &pack->package.elements[i]; 1005 1006 ret = atk_add_sensor(data, obj); 1007 if (ret > 0) 1008 count++; 1009 } 1010 ACPI_FREE(buf.pointer); 1011 1012 return count; 1013 cleanup: 1014 atk_free_sensors(data); 1015 return ret; 1016 } 1017 1018 static int atk_ec_present(struct atk_data *data) 1019 { 1020 struct device *dev = &data->acpi_dev->dev; 1021 union acpi_object *pack; 1022 union acpi_object *ec; 1023 int ret; 1024 int i; 1025 1026 pack = atk_ggrp(data, ATK_MUX_MGMT); 1027 if (IS_ERR(pack)) { 1028 if (PTR_ERR(pack) == -ENOENT) { 1029 /* The MGMT class does not exists - that's ok */ 1030 dev_dbg(dev, "Class %#llx not found\n", ATK_MUX_MGMT); 1031 return 0; 1032 } 1033 return PTR_ERR(pack); 1034 } 1035 1036 /* Search the EC */ 1037 ec = NULL; 1038 for (i = 0; i < pack->package.count; i++) { 1039 union acpi_object *obj = &pack->package.elements[i]; 1040 union acpi_object *id; 1041 1042 if (obj->type != ACPI_TYPE_PACKAGE) 1043 continue; 1044 1045 id = &obj->package.elements[0]; 1046 if (id->type != ACPI_TYPE_INTEGER) 1047 continue; 1048 1049 if (id->integer.value == ATK_EC_ID) { 1050 ec = obj; 1051 break; 1052 } 1053 } 1054 1055 ret = (ec != NULL); 1056 if (!ret) 1057 /* The system has no EC */ 1058 dev_dbg(dev, "EC not found\n"); 1059 1060 ACPI_FREE(pack); 1061 return ret; 1062 } 1063 1064 static int atk_ec_enabled(struct atk_data *data) 1065 { 1066 struct device *dev = &data->acpi_dev->dev; 1067 union acpi_object *obj; 1068 struct atk_acpi_ret_buffer *buf; 1069 int err; 1070 1071 obj = atk_gitm(data, ATK_EC_ID); 1072 if (IS_ERR(obj)) { 1073 dev_err(dev, "Unable to query EC status\n"); 1074 return PTR_ERR(obj); 1075 } 1076 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer; 1077 1078 if (buf->flags == 0) { 1079 dev_err(dev, "Unable to query EC status\n"); 1080 err = -EIO; 1081 } else { 1082 err = (buf->value != 0); 1083 dev_dbg(dev, "EC is %sabled\n", 1084 err ? "en" : "dis"); 1085 } 1086 1087 ACPI_FREE(obj); 1088 return err; 1089 } 1090 1091 static int atk_ec_ctl(struct atk_data *data, int enable) 1092 { 1093 struct device *dev = &data->acpi_dev->dev; 1094 union acpi_object *obj; 1095 struct atk_acpi_input_buf sitm; 1096 struct atk_acpi_ret_buffer *ec_ret; 1097 int err = 0; 1098 1099 sitm.id = ATK_EC_ID; 1100 sitm.param1 = enable; 1101 sitm.param2 = 0; 1102 1103 obj = atk_sitm(data, &sitm); 1104 if (IS_ERR(obj)) { 1105 dev_err(dev, "Failed to %sable the EC\n", 1106 enable ? "en" : "dis"); 1107 return PTR_ERR(obj); 1108 } 1109 ec_ret = (struct atk_acpi_ret_buffer *)obj->buffer.pointer; 1110 if (ec_ret->flags == 0) { 1111 dev_err(dev, "Failed to %sable the EC\n", 1112 enable ? "en" : "dis"); 1113 err = -EIO; 1114 } else { 1115 dev_info(dev, "EC %sabled\n", 1116 enable ? "en" : "dis"); 1117 } 1118 1119 ACPI_FREE(obj); 1120 return err; 1121 } 1122 1123 static int atk_enumerate_new_hwmon(struct atk_data *data) 1124 { 1125 struct device *dev = &data->acpi_dev->dev; 1126 union acpi_object *pack; 1127 int err; 1128 int i; 1129 1130 err = atk_ec_present(data); 1131 if (err < 0) 1132 return err; 1133 if (err) { 1134 err = atk_ec_enabled(data); 1135 if (err < 0) 1136 return err; 1137 /* If the EC was disabled we will disable it again on unload */ 1138 data->disable_ec = err; 1139 1140 err = atk_ec_ctl(data, 1); 1141 if (err) { 1142 data->disable_ec = false; 1143 return err; 1144 } 1145 } 1146 1147 dev_dbg(dev, "Enumerating hwmon sensors\n"); 1148 1149 pack = atk_ggrp(data, ATK_MUX_HWMON); 1150 if (IS_ERR(pack)) 1151 return PTR_ERR(pack); 1152 1153 for (i = 0; i < pack->package.count; i++) { 1154 union acpi_object *obj = &pack->package.elements[i]; 1155 1156 atk_add_sensor(data, obj); 1157 } 1158 1159 err = data->voltage_count + data->temperature_count + data->fan_count; 1160 1161 ACPI_FREE(pack); 1162 return err; 1163 } 1164 1165 static int atk_create_files(struct atk_data *data) 1166 { 1167 struct atk_sensor_data *s; 1168 int err; 1169 1170 list_for_each_entry(s, &data->sensor_list, list) { 1171 err = device_create_file(data->hwmon_dev, &s->input_attr); 1172 if (err) 1173 return err; 1174 err = device_create_file(data->hwmon_dev, &s->label_attr); 1175 if (err) 1176 return err; 1177 err = device_create_file(data->hwmon_dev, &s->limit1_attr); 1178 if (err) 1179 return err; 1180 err = device_create_file(data->hwmon_dev, &s->limit2_attr); 1181 if (err) 1182 return err; 1183 } 1184 1185 err = device_create_file(data->hwmon_dev, &atk_name_attr); 1186 1187 return err; 1188 } 1189 1190 static void atk_remove_files(struct atk_data *data) 1191 { 1192 struct atk_sensor_data *s; 1193 1194 list_for_each_entry(s, &data->sensor_list, list) { 1195 device_remove_file(data->hwmon_dev, &s->input_attr); 1196 device_remove_file(data->hwmon_dev, &s->label_attr); 1197 device_remove_file(data->hwmon_dev, &s->limit1_attr); 1198 device_remove_file(data->hwmon_dev, &s->limit2_attr); 1199 } 1200 device_remove_file(data->hwmon_dev, &atk_name_attr); 1201 } 1202 1203 static void atk_free_sensors(struct atk_data *data) 1204 { 1205 struct list_head *head = &data->sensor_list; 1206 struct atk_sensor_data *s, *tmp; 1207 1208 list_for_each_entry_safe(s, tmp, head, list) { 1209 kfree(s->acpi_name); 1210 kfree(s); 1211 } 1212 } 1213 1214 static int atk_register_hwmon(struct atk_data *data) 1215 { 1216 struct device *dev = &data->acpi_dev->dev; 1217 int err; 1218 1219 dev_dbg(dev, "registering hwmon device\n"); 1220 data->hwmon_dev = hwmon_device_register(dev); 1221 if (IS_ERR(data->hwmon_dev)) 1222 return PTR_ERR(data->hwmon_dev); 1223 1224 dev_dbg(dev, "populating sysfs directory\n"); 1225 err = atk_create_files(data); 1226 if (err) 1227 goto remove; 1228 1229 return 0; 1230 remove: 1231 /* Cleanup the registered files */ 1232 atk_remove_files(data); 1233 hwmon_device_unregister(data->hwmon_dev); 1234 return err; 1235 } 1236 1237 static int atk_probe_if(struct atk_data *data) 1238 { 1239 struct device *dev = &data->acpi_dev->dev; 1240 acpi_handle ret; 1241 acpi_status status; 1242 int err = 0; 1243 1244 /* RTMP: read temperature */ 1245 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_TMP, &ret); 1246 if (ACPI_SUCCESS(status)) 1247 data->rtmp_handle = ret; 1248 else 1249 dev_dbg(dev, "method " METHOD_OLD_READ_TMP " not found: %s\n", 1250 acpi_format_exception(status)); 1251 1252 /* RVLT: read voltage */ 1253 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_VLT, &ret); 1254 if (ACPI_SUCCESS(status)) 1255 data->rvlt_handle = ret; 1256 else 1257 dev_dbg(dev, "method " METHOD_OLD_READ_VLT " not found: %s\n", 1258 acpi_format_exception(status)); 1259 1260 /* RFAN: read fan status */ 1261 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_FAN, &ret); 1262 if (ACPI_SUCCESS(status)) 1263 data->rfan_handle = ret; 1264 else 1265 dev_dbg(dev, "method " METHOD_OLD_READ_FAN " not found: %s\n", 1266 acpi_format_exception(status)); 1267 1268 /* Enumeration */ 1269 status = acpi_get_handle(data->atk_handle, METHOD_ENUMERATE, &ret); 1270 if (ACPI_SUCCESS(status)) 1271 data->enumerate_handle = ret; 1272 else 1273 dev_dbg(dev, "method " METHOD_ENUMERATE " not found: %s\n", 1274 acpi_format_exception(status)); 1275 1276 /* De-multiplexer (read) */ 1277 status = acpi_get_handle(data->atk_handle, METHOD_READ, &ret); 1278 if (ACPI_SUCCESS(status)) 1279 data->read_handle = ret; 1280 else 1281 dev_dbg(dev, "method " METHOD_READ " not found: %s\n", 1282 acpi_format_exception(status)); 1283 1284 /* De-multiplexer (write) */ 1285 status = acpi_get_handle(data->atk_handle, METHOD_WRITE, &ret); 1286 if (ACPI_SUCCESS(status)) 1287 data->write_handle = ret; 1288 else 1289 dev_dbg(dev, "method " METHOD_WRITE " not found: %s\n", 1290 acpi_format_exception(status)); 1291 1292 /* Check for hwmon methods: first check "old" style methods; note that 1293 * both may be present: in this case we stick to the old interface; 1294 * analysis of multiple DSDTs indicates that when both interfaces 1295 * are present the new one (GGRP/GITM) is not functional. 1296 */ 1297 if (data->rtmp_handle && data->rvlt_handle && data->rfan_handle) 1298 data->old_interface = true; 1299 else if (data->enumerate_handle && data->read_handle && 1300 data->write_handle) 1301 data->old_interface = false; 1302 else 1303 err = -ENODEV; 1304 1305 return err; 1306 } 1307 1308 static int atk_add(struct acpi_device *device) 1309 { 1310 acpi_status ret; 1311 int err; 1312 struct acpi_buffer buf; 1313 union acpi_object *obj; 1314 struct atk_data *data; 1315 1316 dev_dbg(&device->dev, "adding...\n"); 1317 1318 data = kzalloc(sizeof(*data), GFP_KERNEL); 1319 if (!data) 1320 return -ENOMEM; 1321 1322 data->acpi_dev = device; 1323 data->atk_handle = device->handle; 1324 INIT_LIST_HEAD(&data->sensor_list); 1325 data->disable_ec = false; 1326 1327 buf.length = ACPI_ALLOCATE_BUFFER; 1328 ret = acpi_evaluate_object_typed(data->atk_handle, BOARD_ID, NULL, 1329 &buf, ACPI_TYPE_PACKAGE); 1330 if (ret != AE_OK) { 1331 dev_dbg(&device->dev, "atk: method MBIF not found\n"); 1332 } else { 1333 obj = buf.pointer; 1334 if (obj->package.count >= 2) { 1335 union acpi_object *id = &obj->package.elements[1]; 1336 if (id->type == ACPI_TYPE_STRING) 1337 dev_dbg(&device->dev, "board ID = %s\n", 1338 id->string.pointer); 1339 } 1340 ACPI_FREE(buf.pointer); 1341 } 1342 1343 err = atk_probe_if(data); 1344 if (err) { 1345 dev_err(&device->dev, "No usable hwmon interface detected\n"); 1346 goto out; 1347 } 1348 1349 if (data->old_interface) { 1350 dev_dbg(&device->dev, "Using old hwmon interface\n"); 1351 err = atk_enumerate_old_hwmon(data); 1352 } else { 1353 dev_dbg(&device->dev, "Using new hwmon interface\n"); 1354 err = atk_enumerate_new_hwmon(data); 1355 } 1356 if (err < 0) 1357 goto out; 1358 if (err == 0) { 1359 dev_info(&device->dev, 1360 "No usable sensor detected, bailing out\n"); 1361 err = -ENODEV; 1362 goto out; 1363 } 1364 1365 err = atk_register_hwmon(data); 1366 if (err) 1367 goto cleanup; 1368 1369 atk_debugfs_init(data); 1370 1371 device->driver_data = data; 1372 return 0; 1373 cleanup: 1374 atk_free_sensors(data); 1375 out: 1376 if (data->disable_ec) 1377 atk_ec_ctl(data, 0); 1378 kfree(data); 1379 return err; 1380 } 1381 1382 static int atk_remove(struct acpi_device *device, int type) 1383 { 1384 struct atk_data *data = device->driver_data; 1385 dev_dbg(&device->dev, "removing...\n"); 1386 1387 device->driver_data = NULL; 1388 1389 atk_debugfs_cleanup(data); 1390 1391 atk_remove_files(data); 1392 atk_free_sensors(data); 1393 hwmon_device_unregister(data->hwmon_dev); 1394 1395 if (data->disable_ec) { 1396 if (atk_ec_ctl(data, 0)) 1397 dev_err(&device->dev, "Failed to disable EC\n"); 1398 } 1399 1400 kfree(data); 1401 1402 return 0; 1403 } 1404 1405 static int __init atk0110_init(void) 1406 { 1407 int ret; 1408 1409 ret = acpi_bus_register_driver(&atk_driver); 1410 if (ret) 1411 pr_info("atk: acpi_bus_register_driver failed: %d\n", ret); 1412 1413 return ret; 1414 } 1415 1416 static void __exit atk0110_exit(void) 1417 { 1418 acpi_bus_unregister_driver(&atk_driver); 1419 } 1420 1421 module_init(atk0110_init); 1422 module_exit(atk0110_exit); 1423 1424 MODULE_LICENSE("GPL"); 1425