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