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