1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * ACPI-WMI mapping driver 4 * 5 * Copyright (C) 2007-2008 Carlos Corbacho <carlos@strangeworlds.co.uk> 6 * 7 * GUID parsing code from ldm.c is: 8 * Copyright (C) 2001,2002 Richard Russon <ldm@flatcap.org> 9 * Copyright (c) 2001-2007 Anton Altaparmakov 10 * Copyright (C) 2001,2002 Jakob Kemi <jakob.kemi@telia.com> 11 * 12 * WMI bus infrastructure by Andrew Lutomirski and Darren Hart: 13 * Copyright (C) 2015 Andrew Lutomirski 14 * Copyright (C) 2017 VMware, Inc. All Rights Reserved. 15 */ 16 17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 18 19 #include <linux/acpi.h> 20 #include <linux/bits.h> 21 #include <linux/build_bug.h> 22 #include <linux/device.h> 23 #include <linux/init.h> 24 #include <linux/kernel.h> 25 #include <linux/list.h> 26 #include <linux/module.h> 27 #include <linux/platform_device.h> 28 #include <linux/slab.h> 29 #include <linux/sysfs.h> 30 #include <linux/types.h> 31 #include <linux/uuid.h> 32 #include <linux/wmi.h> 33 #include <linux/fs.h> 34 35 MODULE_AUTHOR("Carlos Corbacho"); 36 MODULE_DESCRIPTION("ACPI-WMI Mapping Driver"); 37 MODULE_LICENSE("GPL"); 38 39 static LIST_HEAD(wmi_block_list); 40 41 struct guid_block { 42 guid_t guid; 43 union { 44 char object_id[2]; 45 struct { 46 unsigned char notify_id; 47 unsigned char reserved; 48 }; 49 }; 50 u8 instance_count; 51 u8 flags; 52 } __packed; 53 static_assert(sizeof(typeof_member(struct guid_block, guid)) == 16); 54 static_assert(sizeof(struct guid_block) == 20); 55 static_assert(__alignof__(struct guid_block) == 1); 56 57 enum { /* wmi_block flags */ 58 WMI_READ_TAKES_NO_ARGS, 59 WMI_PROBED, 60 }; 61 62 struct wmi_block { 63 struct wmi_device dev; 64 struct list_head list; 65 struct guid_block gblock; 66 struct acpi_device *acpi_device; 67 wmi_notify_handler handler; 68 void *handler_data; 69 unsigned long flags; 70 }; 71 72 73 /* 74 * If the GUID data block is marked as expensive, we must enable and 75 * explicitily disable data collection. 76 */ 77 #define ACPI_WMI_EXPENSIVE BIT(0) 78 #define ACPI_WMI_METHOD BIT(1) /* GUID is a method */ 79 #define ACPI_WMI_STRING BIT(2) /* GUID takes & returns a string */ 80 #define ACPI_WMI_EVENT BIT(3) /* GUID is an event */ 81 82 static const struct acpi_device_id wmi_device_ids[] = { 83 {"PNP0C14", 0}, 84 {"pnp0c14", 0}, 85 { } 86 }; 87 MODULE_DEVICE_TABLE(acpi, wmi_device_ids); 88 89 /* allow duplicate GUIDs as these device drivers use struct wmi_driver */ 90 static const char * const allow_duplicates[] = { 91 "05901221-D566-11D1-B2F0-00A0C9062910", /* wmi-bmof */ 92 "8A42EA14-4F2A-FD45-6422-0087F7A7E608", /* dell-wmi-ddv */ 93 "44FADEB1-B204-40F2-8581-394BBDC1B651", /* intel-wmi-sbl-fw-update */ 94 "86CCFD48-205E-4A77-9C48-2021CBEDE341", /* intel-wmi-thunderbolt */ 95 "F1DDEE52-063C-4784-A11E-8A06684B9B01", /* dell-smm-hwmon */ 96 NULL 97 }; 98 99 #define dev_to_wblock(__dev) container_of_const(__dev, struct wmi_block, dev.dev) 100 #define dev_to_wdev(__dev) container_of_const(__dev, struct wmi_device, dev) 101 102 /* 103 * GUID parsing functions 104 */ 105 106 static bool guid_parse_and_compare(const char *string, const guid_t *guid) 107 { 108 guid_t guid_input; 109 110 if (guid_parse(string, &guid_input)) 111 return false; 112 113 return guid_equal(&guid_input, guid); 114 } 115 116 static const void *find_guid_context(struct wmi_block *wblock, 117 struct wmi_driver *wdriver) 118 { 119 const struct wmi_device_id *id; 120 121 id = wdriver->id_table; 122 if (!id) 123 return NULL; 124 125 while (*id->guid_string) { 126 if (guid_parse_and_compare(id->guid_string, &wblock->gblock.guid)) 127 return id->context; 128 id++; 129 } 130 return NULL; 131 } 132 133 static int get_subobj_info(acpi_handle handle, const char *pathname, 134 struct acpi_device_info **info) 135 { 136 acpi_handle subobj_handle; 137 acpi_status status; 138 139 status = acpi_get_handle(handle, pathname, &subobj_handle); 140 if (status == AE_NOT_FOUND) 141 return -ENOENT; 142 143 if (ACPI_FAILURE(status)) 144 return -EIO; 145 146 status = acpi_get_object_info(subobj_handle, info); 147 if (ACPI_FAILURE(status)) 148 return -EIO; 149 150 return 0; 151 } 152 153 static acpi_status wmi_method_enable(struct wmi_block *wblock, bool enable) 154 { 155 struct guid_block *block; 156 char method[5]; 157 acpi_status status; 158 acpi_handle handle; 159 160 block = &wblock->gblock; 161 handle = wblock->acpi_device->handle; 162 163 snprintf(method, 5, "WE%02X", block->notify_id); 164 status = acpi_execute_simple_method(handle, method, enable); 165 if (status == AE_NOT_FOUND) 166 return AE_OK; 167 168 return status; 169 } 170 171 #define WMI_ACPI_METHOD_NAME_SIZE 5 172 173 static inline void get_acpi_method_name(const struct wmi_block *wblock, 174 const char method, 175 char buffer[static WMI_ACPI_METHOD_NAME_SIZE]) 176 { 177 static_assert(ARRAY_SIZE(wblock->gblock.object_id) == 2); 178 static_assert(WMI_ACPI_METHOD_NAME_SIZE >= 5); 179 180 buffer[0] = 'W'; 181 buffer[1] = method; 182 buffer[2] = wblock->gblock.object_id[0]; 183 buffer[3] = wblock->gblock.object_id[1]; 184 buffer[4] = '\0'; 185 } 186 187 static inline acpi_object_type get_param_acpi_type(const struct wmi_block *wblock) 188 { 189 if (wblock->gblock.flags & ACPI_WMI_STRING) 190 return ACPI_TYPE_STRING; 191 else 192 return ACPI_TYPE_BUFFER; 193 } 194 195 static acpi_status get_event_data(const struct wmi_block *wblock, struct acpi_buffer *out) 196 { 197 union acpi_object param = { 198 .integer = { 199 .type = ACPI_TYPE_INTEGER, 200 .value = wblock->gblock.notify_id, 201 } 202 }; 203 struct acpi_object_list input = { 204 .count = 1, 205 .pointer = ¶m, 206 }; 207 208 return acpi_evaluate_object(wblock->acpi_device->handle, "_WED", &input, out); 209 } 210 211 static int wmidev_match_guid(struct device *dev, const void *data) 212 { 213 struct wmi_block *wblock = dev_to_wblock(dev); 214 const guid_t *guid = data; 215 216 if (guid_equal(guid, &wblock->gblock.guid)) 217 return 1; 218 219 return 0; 220 } 221 222 static struct bus_type wmi_bus_type; 223 224 static struct wmi_device *wmi_find_device_by_guid(const char *guid_string) 225 { 226 struct device *dev; 227 guid_t guid; 228 int ret; 229 230 ret = guid_parse(guid_string, &guid); 231 if (ret < 0) 232 return ERR_PTR(ret); 233 234 dev = bus_find_device(&wmi_bus_type, NULL, &guid, wmidev_match_guid); 235 if (!dev) 236 return ERR_PTR(-ENODEV); 237 238 return dev_to_wdev(dev); 239 } 240 241 static void wmi_device_put(struct wmi_device *wdev) 242 { 243 put_device(&wdev->dev); 244 } 245 246 /* 247 * Exported WMI functions 248 */ 249 250 /** 251 * wmi_instance_count - Get number of WMI object instances 252 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba 253 * 254 * Get the number of WMI object instances. 255 * 256 * Returns: Number of WMI object instances or negative error code. 257 */ 258 int wmi_instance_count(const char *guid_string) 259 { 260 struct wmi_device *wdev; 261 int ret; 262 263 wdev = wmi_find_device_by_guid(guid_string); 264 if (IS_ERR(wdev)) 265 return PTR_ERR(wdev); 266 267 ret = wmidev_instance_count(wdev); 268 wmi_device_put(wdev); 269 270 return ret; 271 } 272 EXPORT_SYMBOL_GPL(wmi_instance_count); 273 274 /** 275 * wmidev_instance_count - Get number of WMI object instances 276 * @wdev: A wmi bus device from a driver 277 * 278 * Get the number of WMI object instances. 279 * 280 * Returns: Number of WMI object instances. 281 */ 282 u8 wmidev_instance_count(struct wmi_device *wdev) 283 { 284 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev); 285 286 return wblock->gblock.instance_count; 287 } 288 EXPORT_SYMBOL_GPL(wmidev_instance_count); 289 290 /** 291 * wmi_evaluate_method - Evaluate a WMI method (deprecated) 292 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba 293 * @instance: Instance index 294 * @method_id: Method ID to call 295 * @in: Buffer containing input for the method call 296 * @out: Empty buffer to return the method results 297 * 298 * Call an ACPI-WMI method, the caller must free @out. 299 * 300 * Return: acpi_status signaling success or error. 301 */ 302 acpi_status wmi_evaluate_method(const char *guid_string, u8 instance, u32 method_id, 303 const struct acpi_buffer *in, struct acpi_buffer *out) 304 { 305 struct wmi_device *wdev; 306 acpi_status status; 307 308 wdev = wmi_find_device_by_guid(guid_string); 309 if (IS_ERR(wdev)) 310 return AE_ERROR; 311 312 status = wmidev_evaluate_method(wdev, instance, method_id, in, out); 313 314 wmi_device_put(wdev); 315 316 return status; 317 } 318 EXPORT_SYMBOL_GPL(wmi_evaluate_method); 319 320 /** 321 * wmidev_evaluate_method - Evaluate a WMI method 322 * @wdev: A wmi bus device from a driver 323 * @instance: Instance index 324 * @method_id: Method ID to call 325 * @in: Buffer containing input for the method call 326 * @out: Empty buffer to return the method results 327 * 328 * Call an ACPI-WMI method, the caller must free @out. 329 * 330 * Return: acpi_status signaling success or error. 331 */ 332 acpi_status wmidev_evaluate_method(struct wmi_device *wdev, u8 instance, u32 method_id, 333 const struct acpi_buffer *in, struct acpi_buffer *out) 334 { 335 struct guid_block *block; 336 struct wmi_block *wblock; 337 acpi_handle handle; 338 struct acpi_object_list input; 339 union acpi_object params[3]; 340 char method[WMI_ACPI_METHOD_NAME_SIZE]; 341 342 wblock = container_of(wdev, struct wmi_block, dev); 343 block = &wblock->gblock; 344 handle = wblock->acpi_device->handle; 345 346 if (!(block->flags & ACPI_WMI_METHOD)) 347 return AE_BAD_DATA; 348 349 if (block->instance_count <= instance) 350 return AE_BAD_PARAMETER; 351 352 input.count = 2; 353 input.pointer = params; 354 params[0].type = ACPI_TYPE_INTEGER; 355 params[0].integer.value = instance; 356 params[1].type = ACPI_TYPE_INTEGER; 357 params[1].integer.value = method_id; 358 359 if (in) { 360 input.count = 3; 361 362 params[2].type = get_param_acpi_type(wblock); 363 params[2].buffer.length = in->length; 364 params[2].buffer.pointer = in->pointer; 365 } 366 367 get_acpi_method_name(wblock, 'M', method); 368 369 return acpi_evaluate_object(handle, method, &input, out); 370 } 371 EXPORT_SYMBOL_GPL(wmidev_evaluate_method); 372 373 static acpi_status __query_block(struct wmi_block *wblock, u8 instance, 374 struct acpi_buffer *out) 375 { 376 struct guid_block *block; 377 acpi_handle handle; 378 acpi_status status, wc_status = AE_ERROR; 379 struct acpi_object_list input; 380 union acpi_object wq_params[1]; 381 char wc_method[WMI_ACPI_METHOD_NAME_SIZE]; 382 char method[WMI_ACPI_METHOD_NAME_SIZE]; 383 384 if (!out) 385 return AE_BAD_PARAMETER; 386 387 block = &wblock->gblock; 388 handle = wblock->acpi_device->handle; 389 390 if (block->instance_count <= instance) 391 return AE_BAD_PARAMETER; 392 393 /* Check GUID is a data block */ 394 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD)) 395 return AE_ERROR; 396 397 input.count = 1; 398 input.pointer = wq_params; 399 wq_params[0].type = ACPI_TYPE_INTEGER; 400 wq_params[0].integer.value = instance; 401 402 if (instance == 0 && test_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags)) 403 input.count = 0; 404 405 /* 406 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to 407 * enable collection. 408 */ 409 if (block->flags & ACPI_WMI_EXPENSIVE) { 410 get_acpi_method_name(wblock, 'C', wc_method); 411 412 /* 413 * Some GUIDs break the specification by declaring themselves 414 * expensive, but have no corresponding WCxx method. So we 415 * should not fail if this happens. 416 */ 417 wc_status = acpi_execute_simple_method(handle, wc_method, 1); 418 } 419 420 get_acpi_method_name(wblock, 'Q', method); 421 status = acpi_evaluate_object(handle, method, &input, out); 422 423 /* 424 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if 425 * the WQxx method failed - we should disable collection anyway. 426 */ 427 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) { 428 /* 429 * Ignore whether this WCxx call succeeds or not since 430 * the previously executed WQxx method call might have 431 * succeeded, and returning the failing status code 432 * of this call would throw away the result of the WQxx 433 * call, potentially leaking memory. 434 */ 435 acpi_execute_simple_method(handle, wc_method, 0); 436 } 437 438 return status; 439 } 440 441 /** 442 * wmi_query_block - Return contents of a WMI block (deprecated) 443 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba 444 * @instance: Instance index 445 * @out: Empty buffer to return the contents of the data block to 446 * 447 * Query a ACPI-WMI block, the caller must free @out. 448 * 449 * Return: ACPI object containing the content of the WMI block. 450 */ 451 acpi_status wmi_query_block(const char *guid_string, u8 instance, 452 struct acpi_buffer *out) 453 { 454 struct wmi_block *wblock; 455 struct wmi_device *wdev; 456 acpi_status status; 457 458 wdev = wmi_find_device_by_guid(guid_string); 459 if (IS_ERR(wdev)) 460 return AE_ERROR; 461 462 wblock = container_of(wdev, struct wmi_block, dev); 463 status = __query_block(wblock, instance, out); 464 465 wmi_device_put(wdev); 466 467 return status; 468 } 469 EXPORT_SYMBOL_GPL(wmi_query_block); 470 471 /** 472 * wmidev_block_query - Return contents of a WMI block 473 * @wdev: A wmi bus device from a driver 474 * @instance: Instance index 475 * 476 * Query an ACPI-WMI block, the caller must free the result. 477 * 478 * Return: ACPI object containing the content of the WMI block. 479 */ 480 union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance) 481 { 482 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL }; 483 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev); 484 485 if (ACPI_FAILURE(__query_block(wblock, instance, &out))) 486 return NULL; 487 488 return out.pointer; 489 } 490 EXPORT_SYMBOL_GPL(wmidev_block_query); 491 492 /** 493 * wmi_set_block - Write to a WMI block (deprecated) 494 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba 495 * @instance: Instance index 496 * @in: Buffer containing new values for the data block 497 * 498 * Write the contents of the input buffer to an ACPI-WMI data block. 499 * 500 * Return: acpi_status signaling success or error. 501 */ 502 acpi_status wmi_set_block(const char *guid_string, u8 instance, const struct acpi_buffer *in) 503 { 504 struct wmi_device *wdev; 505 acpi_status status; 506 507 wdev = wmi_find_device_by_guid(guid_string); 508 if (IS_ERR(wdev)) 509 return AE_ERROR; 510 511 status = wmidev_block_set(wdev, instance, in); 512 wmi_device_put(wdev); 513 514 return status; 515 } 516 EXPORT_SYMBOL_GPL(wmi_set_block); 517 518 /** 519 * wmidev_block_set - Write to a WMI block 520 * @wdev: A wmi bus device from a driver 521 * @instance: Instance index 522 * @in: Buffer containing new values for the data block 523 * 524 * Write contents of the input buffer to an ACPI-WMI data block. 525 * 526 * Return: acpi_status signaling success or error. 527 */ 528 acpi_status wmidev_block_set(struct wmi_device *wdev, u8 instance, const struct acpi_buffer *in) 529 { 530 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev); 531 acpi_handle handle = wblock->acpi_device->handle; 532 struct guid_block *block = &wblock->gblock; 533 char method[WMI_ACPI_METHOD_NAME_SIZE]; 534 struct acpi_object_list input; 535 union acpi_object params[2]; 536 537 if (!in) 538 return AE_BAD_DATA; 539 540 if (block->instance_count <= instance) 541 return AE_BAD_PARAMETER; 542 543 /* Check GUID is a data block */ 544 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD)) 545 return AE_ERROR; 546 547 input.count = 2; 548 input.pointer = params; 549 params[0].type = ACPI_TYPE_INTEGER; 550 params[0].integer.value = instance; 551 params[1].type = get_param_acpi_type(wblock); 552 params[1].buffer.length = in->length; 553 params[1].buffer.pointer = in->pointer; 554 555 get_acpi_method_name(wblock, 'S', method); 556 557 return acpi_evaluate_object(handle, method, &input, NULL); 558 } 559 EXPORT_SYMBOL_GPL(wmidev_block_set); 560 561 /** 562 * wmi_install_notify_handler - Register handler for WMI events (deprecated) 563 * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba 564 * @handler: Function to handle notifications 565 * @data: Data to be returned to handler when event is fired 566 * 567 * Register a handler for events sent to the ACPI-WMI mapper device. 568 * 569 * Return: acpi_status signaling success or error. 570 */ 571 acpi_status wmi_install_notify_handler(const char *guid, 572 wmi_notify_handler handler, 573 void *data) 574 { 575 struct wmi_block *block; 576 acpi_status status = AE_NOT_EXIST; 577 guid_t guid_input; 578 579 if (!guid || !handler) 580 return AE_BAD_PARAMETER; 581 582 if (guid_parse(guid, &guid_input)) 583 return AE_BAD_PARAMETER; 584 585 list_for_each_entry(block, &wmi_block_list, list) { 586 acpi_status wmi_status; 587 588 if (guid_equal(&block->gblock.guid, &guid_input)) { 589 if (block->handler) 590 return AE_ALREADY_ACQUIRED; 591 592 block->handler = handler; 593 block->handler_data = data; 594 595 wmi_status = wmi_method_enable(block, true); 596 if ((wmi_status != AE_OK) || 597 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST))) 598 status = wmi_status; 599 } 600 } 601 602 return status; 603 } 604 EXPORT_SYMBOL_GPL(wmi_install_notify_handler); 605 606 /** 607 * wmi_remove_notify_handler - Unregister handler for WMI events (deprecated) 608 * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba 609 * 610 * Unregister handler for events sent to the ACPI-WMI mapper device. 611 * 612 * Return: acpi_status signaling success or error. 613 */ 614 acpi_status wmi_remove_notify_handler(const char *guid) 615 { 616 struct wmi_block *block; 617 acpi_status status = AE_NOT_EXIST; 618 guid_t guid_input; 619 620 if (!guid) 621 return AE_BAD_PARAMETER; 622 623 if (guid_parse(guid, &guid_input)) 624 return AE_BAD_PARAMETER; 625 626 list_for_each_entry(block, &wmi_block_list, list) { 627 acpi_status wmi_status; 628 629 if (guid_equal(&block->gblock.guid, &guid_input)) { 630 if (!block->handler) 631 return AE_NULL_ENTRY; 632 633 wmi_status = wmi_method_enable(block, false); 634 block->handler = NULL; 635 block->handler_data = NULL; 636 if (wmi_status != AE_OK || (wmi_status == AE_OK && status == AE_NOT_EXIST)) 637 status = wmi_status; 638 } 639 } 640 641 return status; 642 } 643 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler); 644 645 /** 646 * wmi_get_event_data - Get WMI data associated with an event (deprecated) 647 * 648 * @event: Event to find 649 * @out: Buffer to hold event data 650 * 651 * Get extra data associated with an WMI event, the caller needs to free @out. 652 * 653 * Return: acpi_status signaling success or error. 654 */ 655 acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out) 656 { 657 struct wmi_block *wblock; 658 659 list_for_each_entry(wblock, &wmi_block_list, list) { 660 struct guid_block *gblock = &wblock->gblock; 661 662 if ((gblock->flags & ACPI_WMI_EVENT) && gblock->notify_id == event) 663 return get_event_data(wblock, out); 664 } 665 666 return AE_NOT_FOUND; 667 } 668 EXPORT_SYMBOL_GPL(wmi_get_event_data); 669 670 /** 671 * wmi_has_guid - Check if a GUID is available 672 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba 673 * 674 * Check if a given GUID is defined by _WDG. 675 * 676 * Return: True if GUID is available, false otherwise. 677 */ 678 bool wmi_has_guid(const char *guid_string) 679 { 680 struct wmi_device *wdev; 681 682 wdev = wmi_find_device_by_guid(guid_string); 683 if (IS_ERR(wdev)) 684 return false; 685 686 wmi_device_put(wdev); 687 688 return true; 689 } 690 EXPORT_SYMBOL_GPL(wmi_has_guid); 691 692 /** 693 * wmi_get_acpi_device_uid() - Get _UID name of ACPI device that defines GUID (deprecated) 694 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba 695 * 696 * Find the _UID of ACPI device associated with this WMI GUID. 697 * 698 * Return: The ACPI _UID field value or NULL if the WMI GUID was not found. 699 */ 700 char *wmi_get_acpi_device_uid(const char *guid_string) 701 { 702 struct wmi_block *wblock; 703 struct wmi_device *wdev; 704 char *uid; 705 706 wdev = wmi_find_device_by_guid(guid_string); 707 if (IS_ERR(wdev)) 708 return NULL; 709 710 wblock = container_of(wdev, struct wmi_block, dev); 711 uid = acpi_device_uid(wblock->acpi_device); 712 713 wmi_device_put(wdev); 714 715 return uid; 716 } 717 EXPORT_SYMBOL_GPL(wmi_get_acpi_device_uid); 718 719 static inline struct wmi_driver *drv_to_wdrv(struct device_driver *drv) 720 { 721 return container_of(drv, struct wmi_driver, driver); 722 } 723 724 /* 725 * sysfs interface 726 */ 727 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, 728 char *buf) 729 { 730 struct wmi_block *wblock = dev_to_wblock(dev); 731 732 return sysfs_emit(buf, "wmi:%pUL\n", &wblock->gblock.guid); 733 } 734 static DEVICE_ATTR_RO(modalias); 735 736 static ssize_t guid_show(struct device *dev, struct device_attribute *attr, 737 char *buf) 738 { 739 struct wmi_block *wblock = dev_to_wblock(dev); 740 741 return sysfs_emit(buf, "%pUL\n", &wblock->gblock.guid); 742 } 743 static DEVICE_ATTR_RO(guid); 744 745 static ssize_t instance_count_show(struct device *dev, 746 struct device_attribute *attr, char *buf) 747 { 748 struct wmi_block *wblock = dev_to_wblock(dev); 749 750 return sysfs_emit(buf, "%d\n", (int)wblock->gblock.instance_count); 751 } 752 static DEVICE_ATTR_RO(instance_count); 753 754 static ssize_t expensive_show(struct device *dev, 755 struct device_attribute *attr, char *buf) 756 { 757 struct wmi_block *wblock = dev_to_wblock(dev); 758 759 return sysfs_emit(buf, "%d\n", 760 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0); 761 } 762 static DEVICE_ATTR_RO(expensive); 763 764 static struct attribute *wmi_attrs[] = { 765 &dev_attr_modalias.attr, 766 &dev_attr_guid.attr, 767 &dev_attr_instance_count.attr, 768 &dev_attr_expensive.attr, 769 NULL 770 }; 771 ATTRIBUTE_GROUPS(wmi); 772 773 static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr, 774 char *buf) 775 { 776 struct wmi_block *wblock = dev_to_wblock(dev); 777 778 return sysfs_emit(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id); 779 } 780 static DEVICE_ATTR_RO(notify_id); 781 782 static struct attribute *wmi_event_attrs[] = { 783 &dev_attr_notify_id.attr, 784 NULL 785 }; 786 ATTRIBUTE_GROUPS(wmi_event); 787 788 static ssize_t object_id_show(struct device *dev, struct device_attribute *attr, 789 char *buf) 790 { 791 struct wmi_block *wblock = dev_to_wblock(dev); 792 793 return sysfs_emit(buf, "%c%c\n", wblock->gblock.object_id[0], 794 wblock->gblock.object_id[1]); 795 } 796 static DEVICE_ATTR_RO(object_id); 797 798 static ssize_t setable_show(struct device *dev, struct device_attribute *attr, 799 char *buf) 800 { 801 struct wmi_device *wdev = dev_to_wdev(dev); 802 803 return sysfs_emit(buf, "%d\n", (int)wdev->setable); 804 } 805 static DEVICE_ATTR_RO(setable); 806 807 static struct attribute *wmi_data_attrs[] = { 808 &dev_attr_object_id.attr, 809 &dev_attr_setable.attr, 810 NULL 811 }; 812 ATTRIBUTE_GROUPS(wmi_data); 813 814 static struct attribute *wmi_method_attrs[] = { 815 &dev_attr_object_id.attr, 816 NULL 817 }; 818 ATTRIBUTE_GROUPS(wmi_method); 819 820 static int wmi_dev_uevent(const struct device *dev, struct kobj_uevent_env *env) 821 { 822 const struct wmi_block *wblock = dev_to_wblock(dev); 823 824 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", &wblock->gblock.guid)) 825 return -ENOMEM; 826 827 if (add_uevent_var(env, "WMI_GUID=%pUL", &wblock->gblock.guid)) 828 return -ENOMEM; 829 830 return 0; 831 } 832 833 static void wmi_dev_release(struct device *dev) 834 { 835 struct wmi_block *wblock = dev_to_wblock(dev); 836 837 kfree(wblock); 838 } 839 840 static int wmi_dev_match(struct device *dev, struct device_driver *driver) 841 { 842 struct wmi_driver *wmi_driver = drv_to_wdrv(driver); 843 struct wmi_block *wblock = dev_to_wblock(dev); 844 const struct wmi_device_id *id = wmi_driver->id_table; 845 846 if (id == NULL) 847 return 0; 848 849 while (*id->guid_string) { 850 if (guid_parse_and_compare(id->guid_string, &wblock->gblock.guid)) 851 return 1; 852 853 id++; 854 } 855 856 return 0; 857 } 858 859 static int wmi_dev_probe(struct device *dev) 860 { 861 struct wmi_block *wblock = dev_to_wblock(dev); 862 struct wmi_driver *wdriver = drv_to_wdrv(dev->driver); 863 int ret = 0; 864 865 if (ACPI_FAILURE(wmi_method_enable(wblock, true))) 866 dev_warn(dev, "failed to enable device -- probing anyway\n"); 867 868 if (wdriver->probe) { 869 ret = wdriver->probe(dev_to_wdev(dev), 870 find_guid_context(wblock, wdriver)); 871 if (!ret) { 872 if (ACPI_FAILURE(wmi_method_enable(wblock, false))) 873 dev_warn(dev, "Failed to disable device\n"); 874 875 return ret; 876 } 877 } 878 879 set_bit(WMI_PROBED, &wblock->flags); 880 881 return 0; 882 } 883 884 static void wmi_dev_remove(struct device *dev) 885 { 886 struct wmi_block *wblock = dev_to_wblock(dev); 887 struct wmi_driver *wdriver = drv_to_wdrv(dev->driver); 888 889 clear_bit(WMI_PROBED, &wblock->flags); 890 891 if (wdriver->remove) 892 wdriver->remove(dev_to_wdev(dev)); 893 894 if (ACPI_FAILURE(wmi_method_enable(wblock, false))) 895 dev_warn(dev, "failed to disable device\n"); 896 } 897 898 static struct class wmi_bus_class = { 899 .name = "wmi_bus", 900 }; 901 902 static struct bus_type wmi_bus_type = { 903 .name = "wmi", 904 .dev_groups = wmi_groups, 905 .match = wmi_dev_match, 906 .uevent = wmi_dev_uevent, 907 .probe = wmi_dev_probe, 908 .remove = wmi_dev_remove, 909 }; 910 911 static const struct device_type wmi_type_event = { 912 .name = "event", 913 .groups = wmi_event_groups, 914 .release = wmi_dev_release, 915 }; 916 917 static const struct device_type wmi_type_method = { 918 .name = "method", 919 .groups = wmi_method_groups, 920 .release = wmi_dev_release, 921 }; 922 923 static const struct device_type wmi_type_data = { 924 .name = "data", 925 .groups = wmi_data_groups, 926 .release = wmi_dev_release, 927 }; 928 929 /* 930 * _WDG is a static list that is only parsed at startup, 931 * so it's safe to count entries without extra protection. 932 */ 933 static int guid_count(const guid_t *guid) 934 { 935 struct wmi_block *wblock; 936 int count = 0; 937 938 list_for_each_entry(wblock, &wmi_block_list, list) { 939 if (guid_equal(&wblock->gblock.guid, guid)) 940 count++; 941 } 942 943 return count; 944 } 945 946 static int wmi_create_device(struct device *wmi_bus_dev, 947 struct wmi_block *wblock, 948 struct acpi_device *device) 949 { 950 struct acpi_device_info *info; 951 char method[WMI_ACPI_METHOD_NAME_SIZE]; 952 int result; 953 uint count; 954 955 if (wblock->gblock.flags & ACPI_WMI_EVENT) { 956 wblock->dev.dev.type = &wmi_type_event; 957 goto out_init; 958 } 959 960 if (wblock->gblock.flags & ACPI_WMI_METHOD) { 961 wblock->dev.dev.type = &wmi_type_method; 962 goto out_init; 963 } 964 965 /* 966 * Data Block Query Control Method (WQxx by convention) is 967 * required per the WMI documentation. If it is not present, 968 * we ignore this data block. 969 */ 970 get_acpi_method_name(wblock, 'Q', method); 971 result = get_subobj_info(device->handle, method, &info); 972 973 if (result) { 974 dev_warn(wmi_bus_dev, 975 "%s data block query control method not found\n", 976 method); 977 return result; 978 } 979 980 wblock->dev.dev.type = &wmi_type_data; 981 982 /* 983 * The Microsoft documentation specifically states: 984 * 985 * Data blocks registered with only a single instance 986 * can ignore the parameter. 987 * 988 * ACPICA will get mad at us if we call the method with the wrong number 989 * of arguments, so check what our method expects. (On some Dell 990 * laptops, WQxx may not be a method at all.) 991 */ 992 if (info->type != ACPI_TYPE_METHOD || info->param_count == 0) 993 set_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags); 994 995 kfree(info); 996 997 get_acpi_method_name(wblock, 'S', method); 998 if (acpi_has_method(device->handle, method)) 999 wblock->dev.setable = true; 1000 1001 out_init: 1002 wblock->dev.dev.bus = &wmi_bus_type; 1003 wblock->dev.dev.parent = wmi_bus_dev; 1004 1005 count = guid_count(&wblock->gblock.guid); 1006 if (count) 1007 dev_set_name(&wblock->dev.dev, "%pUL-%d", &wblock->gblock.guid, count); 1008 else 1009 dev_set_name(&wblock->dev.dev, "%pUL", &wblock->gblock.guid); 1010 1011 device_initialize(&wblock->dev.dev); 1012 1013 return 0; 1014 } 1015 1016 static int wmi_add_device(struct platform_device *pdev, struct wmi_device *wdev) 1017 { 1018 struct device_link *link; 1019 1020 /* 1021 * Many aggregate WMI drivers do not use -EPROBE_DEFER when they 1022 * are unable to find a WMI device during probe, instead they require 1023 * all WMI devices associated with an platform device to become available 1024 * at once. This device link thus prevents WMI drivers from probing until 1025 * the associated platform device has finished probing (and has registered 1026 * all discovered WMI devices). 1027 */ 1028 1029 link = device_link_add(&wdev->dev, &pdev->dev, DL_FLAG_AUTOREMOVE_SUPPLIER); 1030 if (!link) 1031 return -EINVAL; 1032 1033 return device_add(&wdev->dev); 1034 } 1035 1036 static bool guid_already_parsed_for_legacy(struct acpi_device *device, const guid_t *guid) 1037 { 1038 struct wmi_block *wblock; 1039 1040 list_for_each_entry(wblock, &wmi_block_list, list) { 1041 /* skip warning and register if we know the driver will use struct wmi_driver */ 1042 for (int i = 0; allow_duplicates[i] != NULL; i++) { 1043 if (guid_parse_and_compare(allow_duplicates[i], guid)) 1044 return false; 1045 } 1046 if (guid_equal(&wblock->gblock.guid, guid)) { 1047 /* 1048 * Because we historically didn't track the relationship 1049 * between GUIDs and ACPI nodes, we don't know whether 1050 * we need to suppress GUIDs that are unique on a 1051 * given node but duplicated across nodes. 1052 */ 1053 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n", 1054 guid, dev_name(&wblock->acpi_device->dev)); 1055 return true; 1056 } 1057 } 1058 1059 return false; 1060 } 1061 1062 /* 1063 * Parse the _WDG method for the GUID data blocks 1064 */ 1065 static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev) 1066 { 1067 struct acpi_device *device = ACPI_COMPANION(&pdev->dev); 1068 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL}; 1069 const struct guid_block *gblock; 1070 struct wmi_block *wblock; 1071 union acpi_object *obj; 1072 acpi_status status; 1073 u32 i, total; 1074 int retval; 1075 1076 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out); 1077 if (ACPI_FAILURE(status)) 1078 return -ENXIO; 1079 1080 obj = out.pointer; 1081 if (!obj) 1082 return -ENXIO; 1083 1084 if (obj->type != ACPI_TYPE_BUFFER) { 1085 kfree(obj); 1086 return -ENXIO; 1087 } 1088 1089 gblock = (const struct guid_block *)obj->buffer.pointer; 1090 total = obj->buffer.length / sizeof(struct guid_block); 1091 1092 for (i = 0; i < total; i++) { 1093 if (!gblock[i].instance_count) { 1094 dev_info(wmi_bus_dev, FW_INFO "%pUL has zero instances\n", &gblock[i].guid); 1095 continue; 1096 } 1097 1098 if (guid_already_parsed_for_legacy(device, &gblock[i].guid)) 1099 continue; 1100 1101 wblock = kzalloc(sizeof(*wblock), GFP_KERNEL); 1102 if (!wblock) { 1103 dev_err(wmi_bus_dev, "Failed to allocate %pUL\n", &gblock[i].guid); 1104 continue; 1105 } 1106 1107 wblock->acpi_device = device; 1108 wblock->gblock = gblock[i]; 1109 1110 retval = wmi_create_device(wmi_bus_dev, wblock, device); 1111 if (retval) { 1112 kfree(wblock); 1113 continue; 1114 } 1115 1116 list_add_tail(&wblock->list, &wmi_block_list); 1117 1118 retval = wmi_add_device(pdev, &wblock->dev); 1119 if (retval) { 1120 dev_err(wmi_bus_dev, "failed to register %pUL\n", 1121 &wblock->gblock.guid); 1122 1123 list_del(&wblock->list); 1124 put_device(&wblock->dev.dev); 1125 } 1126 } 1127 1128 kfree(obj); 1129 1130 return 0; 1131 } 1132 1133 /* 1134 * WMI can have EmbeddedControl access regions. In which case, we just want to 1135 * hand these off to the EC driver. 1136 */ 1137 static acpi_status 1138 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address, 1139 u32 bits, u64 *value, 1140 void *handler_context, void *region_context) 1141 { 1142 int result = 0; 1143 u8 temp = 0; 1144 1145 if ((address > 0xFF) || !value) 1146 return AE_BAD_PARAMETER; 1147 1148 if (function != ACPI_READ && function != ACPI_WRITE) 1149 return AE_BAD_PARAMETER; 1150 1151 if (bits != 8) 1152 return AE_BAD_PARAMETER; 1153 1154 if (function == ACPI_READ) { 1155 result = ec_read(address, &temp); 1156 *value = temp; 1157 } else { 1158 temp = 0xff & *value; 1159 result = ec_write(address, temp); 1160 } 1161 1162 switch (result) { 1163 case -EINVAL: 1164 return AE_BAD_PARAMETER; 1165 case -ENODEV: 1166 return AE_NOT_FOUND; 1167 case -ETIME: 1168 return AE_TIME; 1169 default: 1170 return AE_OK; 1171 } 1172 } 1173 1174 static int wmi_notify_device(struct device *dev, void *data) 1175 { 1176 struct wmi_block *wblock = dev_to_wblock(dev); 1177 u32 *event = data; 1178 1179 if (!(wblock->gblock.flags & ACPI_WMI_EVENT && wblock->gblock.notify_id == *event)) 1180 return 0; 1181 1182 /* If a driver is bound, then notify the driver. */ 1183 if (test_bit(WMI_PROBED, &wblock->flags) && wblock->dev.dev.driver) { 1184 struct wmi_driver *driver = drv_to_wdrv(wblock->dev.dev.driver); 1185 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL }; 1186 acpi_status status; 1187 1188 if (!driver->no_notify_data) { 1189 status = get_event_data(wblock, &evdata); 1190 if (ACPI_FAILURE(status)) { 1191 dev_warn(&wblock->dev.dev, "failed to get event data\n"); 1192 return -EIO; 1193 } 1194 } 1195 1196 if (driver->notify) 1197 driver->notify(&wblock->dev, evdata.pointer); 1198 1199 kfree(evdata.pointer); 1200 } else if (wblock->handler) { 1201 /* Legacy handler */ 1202 wblock->handler(*event, wblock->handler_data); 1203 } 1204 1205 acpi_bus_generate_netlink_event(wblock->acpi_device->pnp.device_class, 1206 dev_name(&wblock->dev.dev), *event, 0); 1207 1208 return -EBUSY; 1209 } 1210 1211 static void acpi_wmi_notify_handler(acpi_handle handle, u32 event, void *context) 1212 { 1213 struct device *wmi_bus_dev = context; 1214 1215 device_for_each_child(wmi_bus_dev, &event, wmi_notify_device); 1216 } 1217 1218 static int wmi_remove_device(struct device *dev, void *data) 1219 { 1220 struct wmi_block *wblock = dev_to_wblock(dev); 1221 1222 list_del(&wblock->list); 1223 device_unregister(dev); 1224 1225 return 0; 1226 } 1227 1228 static void acpi_wmi_remove(struct platform_device *device) 1229 { 1230 struct device *wmi_bus_device = dev_get_drvdata(&device->dev); 1231 1232 device_for_each_child_reverse(wmi_bus_device, NULL, wmi_remove_device); 1233 } 1234 1235 static void acpi_wmi_remove_notify_handler(void *data) 1236 { 1237 struct acpi_device *acpi_device = data; 1238 1239 acpi_remove_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY, acpi_wmi_notify_handler); 1240 } 1241 1242 static void acpi_wmi_remove_address_space_handler(void *data) 1243 { 1244 struct acpi_device *acpi_device = data; 1245 1246 acpi_remove_address_space_handler(acpi_device->handle, ACPI_ADR_SPACE_EC, 1247 &acpi_wmi_ec_space_handler); 1248 } 1249 1250 static void acpi_wmi_remove_bus_device(void *data) 1251 { 1252 struct device *wmi_bus_dev = data; 1253 1254 device_unregister(wmi_bus_dev); 1255 } 1256 1257 static int acpi_wmi_probe(struct platform_device *device) 1258 { 1259 struct acpi_device *acpi_device; 1260 struct device *wmi_bus_dev; 1261 acpi_status status; 1262 int error; 1263 1264 acpi_device = ACPI_COMPANION(&device->dev); 1265 if (!acpi_device) { 1266 dev_err(&device->dev, "ACPI companion is missing\n"); 1267 return -ENODEV; 1268 } 1269 1270 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0), NULL, "wmi_bus-%s", 1271 dev_name(&device->dev)); 1272 if (IS_ERR(wmi_bus_dev)) 1273 return PTR_ERR(wmi_bus_dev); 1274 1275 error = devm_add_action_or_reset(&device->dev, acpi_wmi_remove_bus_device, wmi_bus_dev); 1276 if (error < 0) 1277 return error; 1278 1279 dev_set_drvdata(&device->dev, wmi_bus_dev); 1280 1281 status = acpi_install_address_space_handler(acpi_device->handle, 1282 ACPI_ADR_SPACE_EC, 1283 &acpi_wmi_ec_space_handler, 1284 NULL, NULL); 1285 if (ACPI_FAILURE(status)) { 1286 dev_err(&device->dev, "Error installing EC region handler\n"); 1287 return -ENODEV; 1288 } 1289 error = devm_add_action_or_reset(&device->dev, acpi_wmi_remove_address_space_handler, 1290 acpi_device); 1291 if (error < 0) 1292 return error; 1293 1294 status = acpi_install_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY, 1295 acpi_wmi_notify_handler, wmi_bus_dev); 1296 if (ACPI_FAILURE(status)) { 1297 dev_err(&device->dev, "Error installing notify handler\n"); 1298 return -ENODEV; 1299 } 1300 error = devm_add_action_or_reset(&device->dev, acpi_wmi_remove_notify_handler, 1301 acpi_device); 1302 if (error < 0) 1303 return error; 1304 1305 error = parse_wdg(wmi_bus_dev, device); 1306 if (error) { 1307 pr_err("Failed to parse WDG method\n"); 1308 return error; 1309 } 1310 1311 return 0; 1312 } 1313 1314 int __must_check __wmi_driver_register(struct wmi_driver *driver, 1315 struct module *owner) 1316 { 1317 driver->driver.owner = owner; 1318 driver->driver.bus = &wmi_bus_type; 1319 1320 return driver_register(&driver->driver); 1321 } 1322 EXPORT_SYMBOL(__wmi_driver_register); 1323 1324 /** 1325 * wmi_driver_unregister() - Unregister a WMI driver 1326 * @driver: WMI driver to unregister 1327 * 1328 * Unregisters a WMI driver from the WMI bus. 1329 */ 1330 void wmi_driver_unregister(struct wmi_driver *driver) 1331 { 1332 driver_unregister(&driver->driver); 1333 } 1334 EXPORT_SYMBOL(wmi_driver_unregister); 1335 1336 static struct platform_driver acpi_wmi_driver = { 1337 .driver = { 1338 .name = "acpi-wmi", 1339 .acpi_match_table = wmi_device_ids, 1340 }, 1341 .probe = acpi_wmi_probe, 1342 .remove_new = acpi_wmi_remove, 1343 }; 1344 1345 static int __init acpi_wmi_init(void) 1346 { 1347 int error; 1348 1349 if (acpi_disabled) 1350 return -ENODEV; 1351 1352 error = class_register(&wmi_bus_class); 1353 if (error) 1354 return error; 1355 1356 error = bus_register(&wmi_bus_type); 1357 if (error) 1358 goto err_unreg_class; 1359 1360 error = platform_driver_register(&acpi_wmi_driver); 1361 if (error) { 1362 pr_err("Error loading mapper\n"); 1363 goto err_unreg_bus; 1364 } 1365 1366 return 0; 1367 1368 err_unreg_bus: 1369 bus_unregister(&wmi_bus_type); 1370 1371 err_unreg_class: 1372 class_unregister(&wmi_bus_class); 1373 1374 return error; 1375 } 1376 1377 static void __exit acpi_wmi_exit(void) 1378 { 1379 platform_driver_unregister(&acpi_wmi_driver); 1380 bus_unregister(&wmi_bus_type); 1381 class_unregister(&wmi_bus_class); 1382 } 1383 1384 subsys_initcall_sync(acpi_wmi_init); 1385 module_exit(acpi_wmi_exit); 1386