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