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