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