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