1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * ACPI-WMI mapping driver 4 * 5 * Copyright (C) 2007-2008 Carlos Corbacho <carlos@strangeworlds.co.uk> 6 * 7 * GUID parsing code from ldm.c is: 8 * Copyright (C) 2001,2002 Richard Russon <ldm@flatcap.org> 9 * Copyright (c) 2001-2007 Anton Altaparmakov 10 * Copyright (C) 2001,2002 Jakob Kemi <jakob.kemi@telia.com> 11 * 12 * WMI bus infrastructure by Andrew Lutomirski and Darren Hart: 13 * Copyright (C) 2015 Andrew Lutomirski 14 * Copyright (C) 2017 VMware, Inc. All Rights Reserved. 15 */ 16 17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 18 19 #include <linux/acpi.h> 20 #include <linux/bits.h> 21 #include <linux/build_bug.h> 22 #include <linux/device.h> 23 #include <linux/init.h> 24 #include <linux/kernel.h> 25 #include <linux/list.h> 26 #include <linux/miscdevice.h> 27 #include <linux/module.h> 28 #include <linux/platform_device.h> 29 #include <linux/slab.h> 30 #include <linux/sysfs.h> 31 #include <linux/types.h> 32 #include <linux/uaccess.h> 33 #include <linux/uuid.h> 34 #include <linux/wmi.h> 35 #include <linux/fs.h> 36 #include <uapi/linux/wmi.h> 37 38 MODULE_AUTHOR("Carlos Corbacho"); 39 MODULE_DESCRIPTION("ACPI-WMI Mapping Driver"); 40 MODULE_LICENSE("GPL"); 41 42 static LIST_HEAD(wmi_block_list); 43 44 struct guid_block { 45 guid_t guid; 46 union { 47 char object_id[2]; 48 struct { 49 unsigned char notify_id; 50 unsigned char reserved; 51 }; 52 }; 53 u8 instance_count; 54 u8 flags; 55 } __packed; 56 static_assert(sizeof(typeof_member(struct guid_block, guid)) == 16); 57 static_assert(sizeof(struct guid_block) == 20); 58 static_assert(__alignof__(struct guid_block) == 1); 59 60 enum { /* wmi_block flags */ 61 WMI_READ_TAKES_NO_ARGS, 62 WMI_PROBED, 63 }; 64 65 struct wmi_block { 66 struct wmi_device dev; 67 struct list_head list; 68 struct guid_block gblock; 69 struct miscdevice char_dev; 70 struct mutex char_mutex; 71 struct acpi_device *acpi_device; 72 wmi_notify_handler handler; 73 void *handler_data; 74 u64 req_buf_size; 75 unsigned long flags; 76 }; 77 78 79 /* 80 * If the GUID data block is marked as expensive, we must enable and 81 * explicitily disable data collection. 82 */ 83 #define ACPI_WMI_EXPENSIVE BIT(0) 84 #define ACPI_WMI_METHOD BIT(1) /* GUID is a method */ 85 #define ACPI_WMI_STRING BIT(2) /* GUID takes & returns a string */ 86 #define ACPI_WMI_EVENT BIT(3) /* GUID is an event */ 87 88 static bool debug_event; 89 module_param(debug_event, bool, 0444); 90 MODULE_PARM_DESC(debug_event, 91 "Log WMI Events [0/1]"); 92 93 static bool debug_dump_wdg; 94 module_param(debug_dump_wdg, bool, 0444); 95 MODULE_PARM_DESC(debug_dump_wdg, 96 "Dump available WMI interfaces [0/1]"); 97 98 static const struct acpi_device_id wmi_device_ids[] = { 99 {"PNP0C14", 0}, 100 {"pnp0c14", 0}, 101 { } 102 }; 103 MODULE_DEVICE_TABLE(acpi, wmi_device_ids); 104 105 /* allow duplicate GUIDs as these device drivers use struct wmi_driver */ 106 static const char * const allow_duplicates[] = { 107 "05901221-D566-11D1-B2F0-00A0C9062910", /* wmi-bmof */ 108 "8A42EA14-4F2A-FD45-6422-0087F7A7E608", /* dell-wmi-ddv */ 109 NULL 110 }; 111 112 /* 113 * GUID parsing functions 114 */ 115 116 static acpi_status find_guid(const char *guid_string, struct wmi_block **out) 117 { 118 guid_t guid_input; 119 struct wmi_block *wblock; 120 121 if (!guid_string) 122 return AE_BAD_PARAMETER; 123 124 if (guid_parse(guid_string, &guid_input)) 125 return AE_BAD_PARAMETER; 126 127 list_for_each_entry(wblock, &wmi_block_list, list) { 128 if (guid_equal(&wblock->gblock.guid, &guid_input)) { 129 if (out) 130 *out = wblock; 131 132 return AE_OK; 133 } 134 } 135 136 return AE_NOT_FOUND; 137 } 138 139 static bool guid_parse_and_compare(const char *string, const guid_t *guid) 140 { 141 guid_t guid_input; 142 143 if (guid_parse(string, &guid_input)) 144 return false; 145 146 return guid_equal(&guid_input, guid); 147 } 148 149 static const void *find_guid_context(struct wmi_block *wblock, 150 struct wmi_driver *wdriver) 151 { 152 const struct wmi_device_id *id; 153 154 id = wdriver->id_table; 155 if (!id) 156 return NULL; 157 158 while (*id->guid_string) { 159 if (guid_parse_and_compare(id->guid_string, &wblock->gblock.guid)) 160 return id->context; 161 id++; 162 } 163 return NULL; 164 } 165 166 static int get_subobj_info(acpi_handle handle, const char *pathname, 167 struct acpi_device_info **info) 168 { 169 struct acpi_device_info *dummy_info, **info_ptr; 170 acpi_handle subobj_handle; 171 acpi_status status; 172 173 status = acpi_get_handle(handle, (char *)pathname, &subobj_handle); 174 if (status == AE_NOT_FOUND) 175 return -ENOENT; 176 else if (ACPI_FAILURE(status)) 177 return -EIO; 178 179 info_ptr = info ? info : &dummy_info; 180 status = acpi_get_object_info(subobj_handle, info_ptr); 181 if (ACPI_FAILURE(status)) 182 return -EIO; 183 184 if (!info) 185 kfree(dummy_info); 186 187 return 0; 188 } 189 190 static acpi_status wmi_method_enable(struct wmi_block *wblock, bool enable) 191 { 192 struct guid_block *block; 193 char method[5]; 194 acpi_status status; 195 acpi_handle handle; 196 197 block = &wblock->gblock; 198 handle = wblock->acpi_device->handle; 199 200 snprintf(method, 5, "WE%02X", block->notify_id); 201 status = acpi_execute_simple_method(handle, method, enable); 202 if (status == AE_NOT_FOUND) 203 return AE_OK; 204 205 return status; 206 } 207 208 #define WMI_ACPI_METHOD_NAME_SIZE 5 209 210 static inline void get_acpi_method_name(const struct wmi_block *wblock, 211 const char method, 212 char buffer[static WMI_ACPI_METHOD_NAME_SIZE]) 213 { 214 static_assert(ARRAY_SIZE(wblock->gblock.object_id) == 2); 215 static_assert(WMI_ACPI_METHOD_NAME_SIZE >= 5); 216 217 buffer[0] = 'W'; 218 buffer[1] = method; 219 buffer[2] = wblock->gblock.object_id[0]; 220 buffer[3] = wblock->gblock.object_id[1]; 221 buffer[4] = '\0'; 222 } 223 224 static inline acpi_object_type get_param_acpi_type(const struct wmi_block *wblock) 225 { 226 if (wblock->gblock.flags & ACPI_WMI_STRING) 227 return ACPI_TYPE_STRING; 228 else 229 return ACPI_TYPE_BUFFER; 230 } 231 232 static acpi_status get_event_data(const struct wmi_block *wblock, struct acpi_buffer *out) 233 { 234 union acpi_object param = { 235 .integer = { 236 .type = ACPI_TYPE_INTEGER, 237 .value = wblock->gblock.notify_id, 238 } 239 }; 240 struct acpi_object_list input = { 241 .count = 1, 242 .pointer = ¶m, 243 }; 244 245 return acpi_evaluate_object(wblock->acpi_device->handle, "_WED", &input, out); 246 } 247 248 /* 249 * Exported WMI functions 250 */ 251 252 /** 253 * set_required_buffer_size - Sets the buffer size needed for performing IOCTL 254 * @wdev: A wmi bus device from a driver 255 * @length: Required buffer size 256 * 257 * Allocates memory needed for buffer, stores the buffer size in that memory. 258 * 259 * Return: 0 on success or a negative error code for failure. 260 */ 261 int set_required_buffer_size(struct wmi_device *wdev, u64 length) 262 { 263 struct wmi_block *wblock; 264 265 wblock = container_of(wdev, struct wmi_block, dev); 266 wblock->req_buf_size = length; 267 268 return 0; 269 } 270 EXPORT_SYMBOL_GPL(set_required_buffer_size); 271 272 /** 273 * wmi_instance_count - Get number of WMI object instances 274 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba 275 * 276 * Get the number of WMI object instances. 277 * 278 * Returns: Number of WMI object instances or negative error code. 279 */ 280 int wmi_instance_count(const char *guid_string) 281 { 282 struct wmi_block *wblock; 283 acpi_status status; 284 285 status = find_guid(guid_string, &wblock); 286 if (ACPI_FAILURE(status)) { 287 if (status == AE_BAD_PARAMETER) 288 return -EINVAL; 289 290 return -ENODEV; 291 } 292 293 return wmidev_instance_count(&wblock->dev); 294 } 295 EXPORT_SYMBOL_GPL(wmi_instance_count); 296 297 /** 298 * wmidev_instance_count - Get number of WMI object instances 299 * @wdev: A wmi bus device from a driver 300 * 301 * Get the number of WMI object instances. 302 * 303 * Returns: Number of WMI object instances. 304 */ 305 u8 wmidev_instance_count(struct wmi_device *wdev) 306 { 307 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev); 308 309 return wblock->gblock.instance_count; 310 } 311 EXPORT_SYMBOL_GPL(wmidev_instance_count); 312 313 /** 314 * wmi_evaluate_method - Evaluate a WMI method (deprecated) 315 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba 316 * @instance: Instance index 317 * @method_id: Method ID to call 318 * @in: Buffer containing input for the method call 319 * @out: Empty buffer to return the method results 320 * 321 * Call an ACPI-WMI method, the caller must free @out. 322 * 323 * Return: acpi_status signaling success or error. 324 */ 325 acpi_status wmi_evaluate_method(const char *guid_string, u8 instance, u32 method_id, 326 const struct acpi_buffer *in, struct acpi_buffer *out) 327 { 328 struct wmi_block *wblock = NULL; 329 acpi_status status; 330 331 status = find_guid(guid_string, &wblock); 332 if (ACPI_FAILURE(status)) 333 return status; 334 335 return wmidev_evaluate_method(&wblock->dev, instance, method_id, 336 in, out); 337 } 338 EXPORT_SYMBOL_GPL(wmi_evaluate_method); 339 340 /** 341 * wmidev_evaluate_method - Evaluate a WMI method 342 * @wdev: A wmi bus device from a driver 343 * @instance: Instance index 344 * @method_id: Method ID to call 345 * @in: Buffer containing input for the method call 346 * @out: Empty buffer to return the method results 347 * 348 * Call an ACPI-WMI method, the caller must free @out. 349 * 350 * Return: acpi_status signaling success or error. 351 */ 352 acpi_status wmidev_evaluate_method(struct wmi_device *wdev, u8 instance, u32 method_id, 353 const struct acpi_buffer *in, struct acpi_buffer *out) 354 { 355 struct guid_block *block; 356 struct wmi_block *wblock; 357 acpi_handle handle; 358 struct acpi_object_list input; 359 union acpi_object params[3]; 360 char method[WMI_ACPI_METHOD_NAME_SIZE]; 361 362 wblock = container_of(wdev, struct wmi_block, dev); 363 block = &wblock->gblock; 364 handle = wblock->acpi_device->handle; 365 366 if (!(block->flags & ACPI_WMI_METHOD)) 367 return AE_BAD_DATA; 368 369 if (block->instance_count <= instance) 370 return AE_BAD_PARAMETER; 371 372 input.count = 2; 373 input.pointer = params; 374 params[0].type = ACPI_TYPE_INTEGER; 375 params[0].integer.value = instance; 376 params[1].type = ACPI_TYPE_INTEGER; 377 params[1].integer.value = method_id; 378 379 if (in) { 380 input.count = 3; 381 382 params[2].type = get_param_acpi_type(wblock); 383 params[2].buffer.length = in->length; 384 params[2].buffer.pointer = in->pointer; 385 } 386 387 get_acpi_method_name(wblock, 'M', method); 388 389 return acpi_evaluate_object(handle, method, &input, out); 390 } 391 EXPORT_SYMBOL_GPL(wmidev_evaluate_method); 392 393 static acpi_status __query_block(struct wmi_block *wblock, u8 instance, 394 struct acpi_buffer *out) 395 { 396 struct guid_block *block; 397 acpi_handle handle; 398 acpi_status status, wc_status = AE_ERROR; 399 struct acpi_object_list input; 400 union acpi_object wq_params[1]; 401 char wc_method[WMI_ACPI_METHOD_NAME_SIZE]; 402 char method[WMI_ACPI_METHOD_NAME_SIZE]; 403 404 if (!out) 405 return AE_BAD_PARAMETER; 406 407 block = &wblock->gblock; 408 handle = wblock->acpi_device->handle; 409 410 if (block->instance_count <= instance) 411 return AE_BAD_PARAMETER; 412 413 /* Check GUID is a data block */ 414 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD)) 415 return AE_ERROR; 416 417 input.count = 1; 418 input.pointer = wq_params; 419 wq_params[0].type = ACPI_TYPE_INTEGER; 420 wq_params[0].integer.value = instance; 421 422 if (instance == 0 && test_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags)) 423 input.count = 0; 424 425 /* 426 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method first to 427 * enable collection. 428 */ 429 if (block->flags & ACPI_WMI_EXPENSIVE) { 430 get_acpi_method_name(wblock, 'C', wc_method); 431 432 /* 433 * Some GUIDs break the specification by declaring themselves 434 * expensive, but have no corresponding WCxx method. So we 435 * should not fail if this happens. 436 */ 437 wc_status = acpi_execute_simple_method(handle, wc_method, 1); 438 } 439 440 get_acpi_method_name(wblock, 'Q', method); 441 status = acpi_evaluate_object(handle, method, &input, out); 442 443 /* 444 * If ACPI_WMI_EXPENSIVE, call the relevant WCxx method, even if 445 * the WQxx method failed - we should disable collection anyway. 446 */ 447 if ((block->flags & ACPI_WMI_EXPENSIVE) && ACPI_SUCCESS(wc_status)) { 448 /* 449 * Ignore whether this WCxx call succeeds or not since 450 * the previously executed WQxx method call might have 451 * succeeded, and returning the failing status code 452 * of this call would throw away the result of the WQxx 453 * call, potentially leaking memory. 454 */ 455 acpi_execute_simple_method(handle, wc_method, 0); 456 } 457 458 return status; 459 } 460 461 /** 462 * wmi_query_block - Return contents of a WMI block (deprecated) 463 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba 464 * @instance: Instance index 465 * @out: Empty buffer to return the contents of the data block to 466 * 467 * Query a ACPI-WMI block, the caller must free @out. 468 * 469 * Return: ACPI object containing the content of the WMI block. 470 */ 471 acpi_status wmi_query_block(const char *guid_string, u8 instance, 472 struct acpi_buffer *out) 473 { 474 struct wmi_block *wblock; 475 acpi_status status; 476 477 status = find_guid(guid_string, &wblock); 478 if (ACPI_FAILURE(status)) 479 return status; 480 481 return __query_block(wblock, instance, out); 482 } 483 EXPORT_SYMBOL_GPL(wmi_query_block); 484 485 /** 486 * wmidev_block_query - Return contents of a WMI block 487 * @wdev: A wmi bus device from a driver 488 * @instance: Instance index 489 * 490 * Query an ACPI-WMI block, the caller must free the result. 491 * 492 * Return: ACPI object containing the content of the WMI block. 493 */ 494 union acpi_object *wmidev_block_query(struct wmi_device *wdev, u8 instance) 495 { 496 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL }; 497 struct wmi_block *wblock = container_of(wdev, struct wmi_block, dev); 498 499 if (ACPI_FAILURE(__query_block(wblock, instance, &out))) 500 return NULL; 501 502 return out.pointer; 503 } 504 EXPORT_SYMBOL_GPL(wmidev_block_query); 505 506 /** 507 * wmi_set_block - Write to a WMI block (deprecated) 508 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba 509 * @instance: Instance index 510 * @in: Buffer containing new values for the data block 511 * 512 * Write the contents of the input buffer to an ACPI-WMI data block. 513 * 514 * Return: acpi_status signaling success or error. 515 */ 516 acpi_status wmi_set_block(const char *guid_string, u8 instance, 517 const struct acpi_buffer *in) 518 { 519 struct wmi_block *wblock = NULL; 520 struct guid_block *block; 521 acpi_handle handle; 522 struct acpi_object_list input; 523 union acpi_object params[2]; 524 char method[WMI_ACPI_METHOD_NAME_SIZE]; 525 acpi_status status; 526 527 if (!in) 528 return AE_BAD_DATA; 529 530 status = find_guid(guid_string, &wblock); 531 if (ACPI_FAILURE(status)) 532 return status; 533 534 block = &wblock->gblock; 535 handle = wblock->acpi_device->handle; 536 537 if (block->instance_count <= instance) 538 return AE_BAD_PARAMETER; 539 540 /* Check GUID is a data block */ 541 if (block->flags & (ACPI_WMI_EVENT | ACPI_WMI_METHOD)) 542 return AE_ERROR; 543 544 input.count = 2; 545 input.pointer = params; 546 params[0].type = ACPI_TYPE_INTEGER; 547 params[0].integer.value = instance; 548 params[1].type = get_param_acpi_type(wblock); 549 params[1].buffer.length = in->length; 550 params[1].buffer.pointer = in->pointer; 551 552 get_acpi_method_name(wblock, 'S', method); 553 554 return acpi_evaluate_object(handle, method, &input, NULL); 555 } 556 EXPORT_SYMBOL_GPL(wmi_set_block); 557 558 static void wmi_dump_wdg(const struct guid_block *g) 559 { 560 pr_info("%pUL:\n", &g->guid); 561 if (g->flags & ACPI_WMI_EVENT) 562 pr_info("\tnotify_id: 0x%02X\n", g->notify_id); 563 else 564 pr_info("\tobject_id: %2pE\n", g->object_id); 565 pr_info("\tinstance_count: %d\n", g->instance_count); 566 pr_info("\tflags: %#x", g->flags); 567 if (g->flags) { 568 if (g->flags & ACPI_WMI_EXPENSIVE) 569 pr_cont(" ACPI_WMI_EXPENSIVE"); 570 if (g->flags & ACPI_WMI_METHOD) 571 pr_cont(" ACPI_WMI_METHOD"); 572 if (g->flags & ACPI_WMI_STRING) 573 pr_cont(" ACPI_WMI_STRING"); 574 if (g->flags & ACPI_WMI_EVENT) 575 pr_cont(" ACPI_WMI_EVENT"); 576 } 577 pr_cont("\n"); 578 579 } 580 581 static void wmi_notify_debug(u32 value, void *context) 582 { 583 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL }; 584 union acpi_object *obj; 585 acpi_status status; 586 587 status = wmi_get_event_data(value, &response); 588 if (status != AE_OK) { 589 pr_info("bad event status 0x%x\n", status); 590 return; 591 } 592 593 obj = response.pointer; 594 if (!obj) 595 return; 596 597 pr_info("DEBUG: event 0x%02X ", value); 598 switch (obj->type) { 599 case ACPI_TYPE_BUFFER: 600 pr_cont("BUFFER_TYPE - length %u\n", obj->buffer.length); 601 break; 602 case ACPI_TYPE_STRING: 603 pr_cont("STRING_TYPE - %s\n", obj->string.pointer); 604 break; 605 case ACPI_TYPE_INTEGER: 606 pr_cont("INTEGER_TYPE - %llu\n", obj->integer.value); 607 break; 608 case ACPI_TYPE_PACKAGE: 609 pr_cont("PACKAGE_TYPE - %u elements\n", obj->package.count); 610 break; 611 default: 612 pr_cont("object type 0x%X\n", obj->type); 613 } 614 kfree(obj); 615 } 616 617 /** 618 * wmi_install_notify_handler - Register handler for WMI events (deprecated) 619 * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba 620 * @handler: Function to handle notifications 621 * @data: Data to be returned to handler when event is fired 622 * 623 * Register a handler for events sent to the ACPI-WMI mapper device. 624 * 625 * Return: acpi_status signaling success or error. 626 */ 627 acpi_status wmi_install_notify_handler(const char *guid, 628 wmi_notify_handler handler, 629 void *data) 630 { 631 struct wmi_block *block; 632 acpi_status status = AE_NOT_EXIST; 633 guid_t guid_input; 634 635 if (!guid || !handler) 636 return AE_BAD_PARAMETER; 637 638 if (guid_parse(guid, &guid_input)) 639 return AE_BAD_PARAMETER; 640 641 list_for_each_entry(block, &wmi_block_list, list) { 642 acpi_status wmi_status; 643 644 if (guid_equal(&block->gblock.guid, &guid_input)) { 645 if (block->handler && 646 block->handler != wmi_notify_debug) 647 return AE_ALREADY_ACQUIRED; 648 649 block->handler = handler; 650 block->handler_data = data; 651 652 wmi_status = wmi_method_enable(block, true); 653 if ((wmi_status != AE_OK) || 654 ((wmi_status == AE_OK) && (status == AE_NOT_EXIST))) 655 status = wmi_status; 656 } 657 } 658 659 return status; 660 } 661 EXPORT_SYMBOL_GPL(wmi_install_notify_handler); 662 663 /** 664 * wmi_remove_notify_handler - Unregister handler for WMI events (deprecated) 665 * @guid: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba 666 * 667 * Unregister handler for events sent to the ACPI-WMI mapper device. 668 * 669 * Return: acpi_status signaling success or error. 670 */ 671 acpi_status wmi_remove_notify_handler(const char *guid) 672 { 673 struct wmi_block *block; 674 acpi_status status = AE_NOT_EXIST; 675 guid_t guid_input; 676 677 if (!guid) 678 return AE_BAD_PARAMETER; 679 680 if (guid_parse(guid, &guid_input)) 681 return AE_BAD_PARAMETER; 682 683 list_for_each_entry(block, &wmi_block_list, list) { 684 acpi_status wmi_status; 685 686 if (guid_equal(&block->gblock.guid, &guid_input)) { 687 if (!block->handler || 688 block->handler == wmi_notify_debug) 689 return AE_NULL_ENTRY; 690 691 if (debug_event) { 692 block->handler = wmi_notify_debug; 693 status = AE_OK; 694 } else { 695 wmi_status = wmi_method_enable(block, false); 696 block->handler = NULL; 697 block->handler_data = NULL; 698 if ((wmi_status != AE_OK) || 699 ((wmi_status == AE_OK) && 700 (status == AE_NOT_EXIST))) 701 status = wmi_status; 702 } 703 } 704 } 705 706 return status; 707 } 708 EXPORT_SYMBOL_GPL(wmi_remove_notify_handler); 709 710 /** 711 * wmi_get_event_data - Get WMI data associated with an event (deprecated) 712 * 713 * @event: Event to find 714 * @out: Buffer to hold event data 715 * 716 * Get extra data associated with an WMI event, the caller needs to free @out. 717 * 718 * Return: acpi_status signaling success or error. 719 */ 720 acpi_status wmi_get_event_data(u32 event, struct acpi_buffer *out) 721 { 722 struct wmi_block *wblock; 723 724 list_for_each_entry(wblock, &wmi_block_list, list) { 725 struct guid_block *gblock = &wblock->gblock; 726 727 if ((gblock->flags & ACPI_WMI_EVENT) && gblock->notify_id == event) 728 return get_event_data(wblock, out); 729 } 730 731 return AE_NOT_FOUND; 732 } 733 EXPORT_SYMBOL_GPL(wmi_get_event_data); 734 735 /** 736 * wmi_has_guid - Check if a GUID is available 737 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba 738 * 739 * Check if a given GUID is defined by _WDG. 740 * 741 * Return: True if GUID is available, false otherwise. 742 */ 743 bool wmi_has_guid(const char *guid_string) 744 { 745 return ACPI_SUCCESS(find_guid(guid_string, NULL)); 746 } 747 EXPORT_SYMBOL_GPL(wmi_has_guid); 748 749 /** 750 * wmi_get_acpi_device_uid() - Get _UID name of ACPI device that defines GUID (deprecated) 751 * @guid_string: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba 752 * 753 * Find the _UID of ACPI device associated with this WMI GUID. 754 * 755 * Return: The ACPI _UID field value or NULL if the WMI GUID was not found. 756 */ 757 char *wmi_get_acpi_device_uid(const char *guid_string) 758 { 759 struct wmi_block *wblock = NULL; 760 acpi_status status; 761 762 status = find_guid(guid_string, &wblock); 763 if (ACPI_FAILURE(status)) 764 return NULL; 765 766 return acpi_device_uid(wblock->acpi_device); 767 } 768 EXPORT_SYMBOL_GPL(wmi_get_acpi_device_uid); 769 770 #define dev_to_wblock(__dev) container_of_const(__dev, struct wmi_block, dev.dev) 771 #define dev_to_wdev(__dev) container_of_const(__dev, struct wmi_device, dev) 772 773 static inline struct wmi_driver *drv_to_wdrv(struct device_driver *drv) 774 { 775 return container_of(drv, struct wmi_driver, driver); 776 } 777 778 /* 779 * sysfs interface 780 */ 781 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, 782 char *buf) 783 { 784 struct wmi_block *wblock = dev_to_wblock(dev); 785 786 return sysfs_emit(buf, "wmi:%pUL\n", &wblock->gblock.guid); 787 } 788 static DEVICE_ATTR_RO(modalias); 789 790 static ssize_t guid_show(struct device *dev, struct device_attribute *attr, 791 char *buf) 792 { 793 struct wmi_block *wblock = dev_to_wblock(dev); 794 795 return sysfs_emit(buf, "%pUL\n", &wblock->gblock.guid); 796 } 797 static DEVICE_ATTR_RO(guid); 798 799 static ssize_t instance_count_show(struct device *dev, 800 struct device_attribute *attr, char *buf) 801 { 802 struct wmi_block *wblock = dev_to_wblock(dev); 803 804 return sysfs_emit(buf, "%d\n", (int)wblock->gblock.instance_count); 805 } 806 static DEVICE_ATTR_RO(instance_count); 807 808 static ssize_t expensive_show(struct device *dev, 809 struct device_attribute *attr, char *buf) 810 { 811 struct wmi_block *wblock = dev_to_wblock(dev); 812 813 return sysfs_emit(buf, "%d\n", 814 (wblock->gblock.flags & ACPI_WMI_EXPENSIVE) != 0); 815 } 816 static DEVICE_ATTR_RO(expensive); 817 818 static struct attribute *wmi_attrs[] = { 819 &dev_attr_modalias.attr, 820 &dev_attr_guid.attr, 821 &dev_attr_instance_count.attr, 822 &dev_attr_expensive.attr, 823 NULL 824 }; 825 ATTRIBUTE_GROUPS(wmi); 826 827 static ssize_t notify_id_show(struct device *dev, struct device_attribute *attr, 828 char *buf) 829 { 830 struct wmi_block *wblock = dev_to_wblock(dev); 831 832 return sysfs_emit(buf, "%02X\n", (unsigned int)wblock->gblock.notify_id); 833 } 834 static DEVICE_ATTR_RO(notify_id); 835 836 static struct attribute *wmi_event_attrs[] = { 837 &dev_attr_notify_id.attr, 838 NULL 839 }; 840 ATTRIBUTE_GROUPS(wmi_event); 841 842 static ssize_t object_id_show(struct device *dev, struct device_attribute *attr, 843 char *buf) 844 { 845 struct wmi_block *wblock = dev_to_wblock(dev); 846 847 return sysfs_emit(buf, "%c%c\n", wblock->gblock.object_id[0], 848 wblock->gblock.object_id[1]); 849 } 850 static DEVICE_ATTR_RO(object_id); 851 852 static ssize_t setable_show(struct device *dev, struct device_attribute *attr, 853 char *buf) 854 { 855 struct wmi_device *wdev = dev_to_wdev(dev); 856 857 return sysfs_emit(buf, "%d\n", (int)wdev->setable); 858 } 859 static DEVICE_ATTR_RO(setable); 860 861 static struct attribute *wmi_data_attrs[] = { 862 &dev_attr_object_id.attr, 863 &dev_attr_setable.attr, 864 NULL 865 }; 866 ATTRIBUTE_GROUPS(wmi_data); 867 868 static struct attribute *wmi_method_attrs[] = { 869 &dev_attr_object_id.attr, 870 NULL 871 }; 872 ATTRIBUTE_GROUPS(wmi_method); 873 874 static int wmi_dev_uevent(const struct device *dev, struct kobj_uevent_env *env) 875 { 876 const struct wmi_block *wblock = dev_to_wblock(dev); 877 878 if (add_uevent_var(env, "MODALIAS=wmi:%pUL", &wblock->gblock.guid)) 879 return -ENOMEM; 880 881 if (add_uevent_var(env, "WMI_GUID=%pUL", &wblock->gblock.guid)) 882 return -ENOMEM; 883 884 return 0; 885 } 886 887 static void wmi_dev_release(struct device *dev) 888 { 889 struct wmi_block *wblock = dev_to_wblock(dev); 890 891 kfree(wblock); 892 } 893 894 static int wmi_dev_match(struct device *dev, struct device_driver *driver) 895 { 896 struct wmi_driver *wmi_driver = drv_to_wdrv(driver); 897 struct wmi_block *wblock = dev_to_wblock(dev); 898 const struct wmi_device_id *id = wmi_driver->id_table; 899 900 if (id == NULL) 901 return 0; 902 903 while (*id->guid_string) { 904 if (guid_parse_and_compare(id->guid_string, &wblock->gblock.guid)) 905 return 1; 906 907 id++; 908 } 909 910 return 0; 911 } 912 static int wmi_char_open(struct inode *inode, struct file *filp) 913 { 914 const char *driver_name = filp->f_path.dentry->d_iname; 915 struct wmi_block *wblock; 916 struct wmi_block *next; 917 918 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) { 919 if (!wblock->dev.dev.driver) 920 continue; 921 if (strcmp(driver_name, wblock->dev.dev.driver->name) == 0) { 922 filp->private_data = wblock; 923 break; 924 } 925 } 926 927 if (!filp->private_data) 928 return -ENODEV; 929 930 return nonseekable_open(inode, filp); 931 } 932 933 static ssize_t wmi_char_read(struct file *filp, char __user *buffer, 934 size_t length, loff_t *offset) 935 { 936 struct wmi_block *wblock = filp->private_data; 937 938 return simple_read_from_buffer(buffer, length, offset, 939 &wblock->req_buf_size, 940 sizeof(wblock->req_buf_size)); 941 } 942 943 static long wmi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 944 { 945 struct wmi_ioctl_buffer __user *input = 946 (struct wmi_ioctl_buffer __user *) arg; 947 struct wmi_block *wblock = filp->private_data; 948 struct wmi_ioctl_buffer *buf; 949 struct wmi_driver *wdriver; 950 int ret; 951 952 if (_IOC_TYPE(cmd) != WMI_IOC) 953 return -ENOTTY; 954 955 /* make sure we're not calling a higher instance than exists*/ 956 if (_IOC_NR(cmd) >= wblock->gblock.instance_count) 957 return -EINVAL; 958 959 mutex_lock(&wblock->char_mutex); 960 buf = wblock->handler_data; 961 if (get_user(buf->length, &input->length)) { 962 dev_dbg(&wblock->dev.dev, "Read length from user failed\n"); 963 ret = -EFAULT; 964 goto out_ioctl; 965 } 966 /* if it's too small, abort */ 967 if (buf->length < wblock->req_buf_size) { 968 dev_err(&wblock->dev.dev, 969 "Buffer %lld too small, need at least %lld\n", 970 buf->length, wblock->req_buf_size); 971 ret = -EINVAL; 972 goto out_ioctl; 973 } 974 /* if it's too big, warn, driver will only use what is needed */ 975 if (buf->length > wblock->req_buf_size) 976 dev_warn(&wblock->dev.dev, 977 "Buffer %lld is bigger than required %lld\n", 978 buf->length, wblock->req_buf_size); 979 980 /* copy the structure from userspace */ 981 if (copy_from_user(buf, input, wblock->req_buf_size)) { 982 dev_dbg(&wblock->dev.dev, "Copy %llu from user failed\n", 983 wblock->req_buf_size); 984 ret = -EFAULT; 985 goto out_ioctl; 986 } 987 988 /* let the driver do any filtering and do the call */ 989 wdriver = drv_to_wdrv(wblock->dev.dev.driver); 990 if (!try_module_get(wdriver->driver.owner)) { 991 ret = -EBUSY; 992 goto out_ioctl; 993 } 994 ret = wdriver->filter_callback(&wblock->dev, cmd, buf); 995 module_put(wdriver->driver.owner); 996 if (ret) 997 goto out_ioctl; 998 999 /* return the result (only up to our internal buffer size) */ 1000 if (copy_to_user(input, buf, wblock->req_buf_size)) { 1001 dev_dbg(&wblock->dev.dev, "Copy %llu to user failed\n", 1002 wblock->req_buf_size); 1003 ret = -EFAULT; 1004 } 1005 1006 out_ioctl: 1007 mutex_unlock(&wblock->char_mutex); 1008 return ret; 1009 } 1010 1011 static const struct file_operations wmi_fops = { 1012 .owner = THIS_MODULE, 1013 .read = wmi_char_read, 1014 .open = wmi_char_open, 1015 .unlocked_ioctl = wmi_ioctl, 1016 .compat_ioctl = compat_ptr_ioctl, 1017 }; 1018 1019 static int wmi_dev_probe(struct device *dev) 1020 { 1021 struct wmi_block *wblock = dev_to_wblock(dev); 1022 struct wmi_driver *wdriver = drv_to_wdrv(dev->driver); 1023 int ret = 0; 1024 char *buf; 1025 1026 if (ACPI_FAILURE(wmi_method_enable(wblock, true))) 1027 dev_warn(dev, "failed to enable device -- probing anyway\n"); 1028 1029 if (wdriver->probe) { 1030 ret = wdriver->probe(dev_to_wdev(dev), 1031 find_guid_context(wblock, wdriver)); 1032 if (ret != 0) 1033 goto probe_failure; 1034 } 1035 1036 /* driver wants a character device made */ 1037 if (wdriver->filter_callback) { 1038 /* check that required buffer size declared by driver or MOF */ 1039 if (!wblock->req_buf_size) { 1040 dev_err(&wblock->dev.dev, 1041 "Required buffer size not set\n"); 1042 ret = -EINVAL; 1043 goto probe_failure; 1044 } 1045 1046 wblock->handler_data = kmalloc(wblock->req_buf_size, 1047 GFP_KERNEL); 1048 if (!wblock->handler_data) { 1049 ret = -ENOMEM; 1050 goto probe_failure; 1051 } 1052 1053 buf = kasprintf(GFP_KERNEL, "wmi/%s", wdriver->driver.name); 1054 if (!buf) { 1055 ret = -ENOMEM; 1056 goto probe_string_failure; 1057 } 1058 wblock->char_dev.minor = MISC_DYNAMIC_MINOR; 1059 wblock->char_dev.name = buf; 1060 wblock->char_dev.fops = &wmi_fops; 1061 wblock->char_dev.mode = 0444; 1062 ret = misc_register(&wblock->char_dev); 1063 if (ret) { 1064 dev_warn(dev, "failed to register char dev: %d\n", ret); 1065 ret = -ENOMEM; 1066 goto probe_misc_failure; 1067 } 1068 } 1069 1070 set_bit(WMI_PROBED, &wblock->flags); 1071 return 0; 1072 1073 probe_misc_failure: 1074 kfree(buf); 1075 probe_string_failure: 1076 kfree(wblock->handler_data); 1077 probe_failure: 1078 if (ACPI_FAILURE(wmi_method_enable(wblock, false))) 1079 dev_warn(dev, "failed to disable device\n"); 1080 return ret; 1081 } 1082 1083 static void wmi_dev_remove(struct device *dev) 1084 { 1085 struct wmi_block *wblock = dev_to_wblock(dev); 1086 struct wmi_driver *wdriver = drv_to_wdrv(dev->driver); 1087 1088 clear_bit(WMI_PROBED, &wblock->flags); 1089 1090 if (wdriver->filter_callback) { 1091 misc_deregister(&wblock->char_dev); 1092 kfree(wblock->char_dev.name); 1093 kfree(wblock->handler_data); 1094 } 1095 1096 if (wdriver->remove) 1097 wdriver->remove(dev_to_wdev(dev)); 1098 1099 if (ACPI_FAILURE(wmi_method_enable(wblock, false))) 1100 dev_warn(dev, "failed to disable device\n"); 1101 } 1102 1103 static struct class wmi_bus_class = { 1104 .name = "wmi_bus", 1105 }; 1106 1107 static struct bus_type wmi_bus_type = { 1108 .name = "wmi", 1109 .dev_groups = wmi_groups, 1110 .match = wmi_dev_match, 1111 .uevent = wmi_dev_uevent, 1112 .probe = wmi_dev_probe, 1113 .remove = wmi_dev_remove, 1114 }; 1115 1116 static const struct device_type wmi_type_event = { 1117 .name = "event", 1118 .groups = wmi_event_groups, 1119 .release = wmi_dev_release, 1120 }; 1121 1122 static const struct device_type wmi_type_method = { 1123 .name = "method", 1124 .groups = wmi_method_groups, 1125 .release = wmi_dev_release, 1126 }; 1127 1128 static const struct device_type wmi_type_data = { 1129 .name = "data", 1130 .groups = wmi_data_groups, 1131 .release = wmi_dev_release, 1132 }; 1133 1134 /* 1135 * _WDG is a static list that is only parsed at startup, 1136 * so it's safe to count entries without extra protection. 1137 */ 1138 static int guid_count(const guid_t *guid) 1139 { 1140 struct wmi_block *wblock; 1141 int count = 0; 1142 1143 list_for_each_entry(wblock, &wmi_block_list, list) { 1144 if (guid_equal(&wblock->gblock.guid, guid)) 1145 count++; 1146 } 1147 1148 return count; 1149 } 1150 1151 static int wmi_create_device(struct device *wmi_bus_dev, 1152 struct wmi_block *wblock, 1153 struct acpi_device *device) 1154 { 1155 struct acpi_device_info *info; 1156 char method[WMI_ACPI_METHOD_NAME_SIZE]; 1157 int result; 1158 uint count; 1159 1160 if (wblock->gblock.flags & ACPI_WMI_EVENT) { 1161 wblock->dev.dev.type = &wmi_type_event; 1162 goto out_init; 1163 } 1164 1165 if (wblock->gblock.flags & ACPI_WMI_METHOD) { 1166 wblock->dev.dev.type = &wmi_type_method; 1167 mutex_init(&wblock->char_mutex); 1168 goto out_init; 1169 } 1170 1171 /* 1172 * Data Block Query Control Method (WQxx by convention) is 1173 * required per the WMI documentation. If it is not present, 1174 * we ignore this data block. 1175 */ 1176 get_acpi_method_name(wblock, 'Q', method); 1177 result = get_subobj_info(device->handle, method, &info); 1178 1179 if (result) { 1180 dev_warn(wmi_bus_dev, 1181 "%s data block query control method not found\n", 1182 method); 1183 return result; 1184 } 1185 1186 wblock->dev.dev.type = &wmi_type_data; 1187 1188 /* 1189 * The Microsoft documentation specifically states: 1190 * 1191 * Data blocks registered with only a single instance 1192 * can ignore the parameter. 1193 * 1194 * ACPICA will get mad at us if we call the method with the wrong number 1195 * of arguments, so check what our method expects. (On some Dell 1196 * laptops, WQxx may not be a method at all.) 1197 */ 1198 if (info->type != ACPI_TYPE_METHOD || info->param_count == 0) 1199 set_bit(WMI_READ_TAKES_NO_ARGS, &wblock->flags); 1200 1201 kfree(info); 1202 1203 get_acpi_method_name(wblock, 'S', method); 1204 result = get_subobj_info(device->handle, method, NULL); 1205 1206 if (result == 0) 1207 wblock->dev.setable = true; 1208 1209 out_init: 1210 wblock->dev.dev.bus = &wmi_bus_type; 1211 wblock->dev.dev.parent = wmi_bus_dev; 1212 1213 count = guid_count(&wblock->gblock.guid); 1214 if (count) 1215 dev_set_name(&wblock->dev.dev, "%pUL-%d", &wblock->gblock.guid, count); 1216 else 1217 dev_set_name(&wblock->dev.dev, "%pUL", &wblock->gblock.guid); 1218 1219 device_initialize(&wblock->dev.dev); 1220 1221 return 0; 1222 } 1223 1224 static void wmi_free_devices(struct acpi_device *device) 1225 { 1226 struct wmi_block *wblock, *next; 1227 1228 /* Delete devices for all the GUIDs */ 1229 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) { 1230 if (wblock->acpi_device == device) { 1231 list_del(&wblock->list); 1232 device_unregister(&wblock->dev.dev); 1233 } 1234 } 1235 } 1236 1237 static bool guid_already_parsed_for_legacy(struct acpi_device *device, const guid_t *guid) 1238 { 1239 struct wmi_block *wblock; 1240 1241 list_for_each_entry(wblock, &wmi_block_list, list) { 1242 /* skip warning and register if we know the driver will use struct wmi_driver */ 1243 for (int i = 0; allow_duplicates[i] != NULL; i++) { 1244 if (guid_parse_and_compare(allow_duplicates[i], guid)) 1245 return false; 1246 } 1247 if (guid_equal(&wblock->gblock.guid, guid)) { 1248 /* 1249 * Because we historically didn't track the relationship 1250 * between GUIDs and ACPI nodes, we don't know whether 1251 * we need to suppress GUIDs that are unique on a 1252 * given node but duplicated across nodes. 1253 */ 1254 dev_warn(&device->dev, "duplicate WMI GUID %pUL (first instance was on %s)\n", 1255 guid, dev_name(&wblock->acpi_device->dev)); 1256 return true; 1257 } 1258 } 1259 1260 return false; 1261 } 1262 1263 /* 1264 * Parse the _WDG method for the GUID data blocks 1265 */ 1266 static int parse_wdg(struct device *wmi_bus_dev, struct acpi_device *device) 1267 { 1268 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL}; 1269 const struct guid_block *gblock; 1270 struct wmi_block *wblock, *next; 1271 union acpi_object *obj; 1272 acpi_status status; 1273 int retval = 0; 1274 u32 i, total; 1275 1276 status = acpi_evaluate_object(device->handle, "_WDG", NULL, &out); 1277 if (ACPI_FAILURE(status)) 1278 return -ENXIO; 1279 1280 obj = out.pointer; 1281 if (!obj) 1282 return -ENXIO; 1283 1284 if (obj->type != ACPI_TYPE_BUFFER) { 1285 retval = -ENXIO; 1286 goto out_free_pointer; 1287 } 1288 1289 gblock = (const struct guid_block *)obj->buffer.pointer; 1290 total = obj->buffer.length / sizeof(struct guid_block); 1291 1292 for (i = 0; i < total; i++) { 1293 if (debug_dump_wdg) 1294 wmi_dump_wdg(&gblock[i]); 1295 1296 if (guid_already_parsed_for_legacy(device, &gblock[i].guid)) 1297 continue; 1298 1299 wblock = kzalloc(sizeof(*wblock), GFP_KERNEL); 1300 if (!wblock) { 1301 retval = -ENOMEM; 1302 break; 1303 } 1304 1305 wblock->acpi_device = device; 1306 wblock->gblock = gblock[i]; 1307 1308 retval = wmi_create_device(wmi_bus_dev, wblock, device); 1309 if (retval) { 1310 kfree(wblock); 1311 continue; 1312 } 1313 1314 list_add_tail(&wblock->list, &wmi_block_list); 1315 1316 if (debug_event) { 1317 wblock->handler = wmi_notify_debug; 1318 wmi_method_enable(wblock, true); 1319 } 1320 } 1321 1322 /* 1323 * Now that all of the devices are created, add them to the 1324 * device tree and probe subdrivers. 1325 */ 1326 list_for_each_entry_safe(wblock, next, &wmi_block_list, list) { 1327 if (wblock->acpi_device != device) 1328 continue; 1329 1330 retval = device_add(&wblock->dev.dev); 1331 if (retval) { 1332 dev_err(wmi_bus_dev, "failed to register %pUL\n", 1333 &wblock->gblock.guid); 1334 if (debug_event) 1335 wmi_method_enable(wblock, false); 1336 list_del(&wblock->list); 1337 put_device(&wblock->dev.dev); 1338 } 1339 } 1340 1341 out_free_pointer: 1342 kfree(out.pointer); 1343 return retval; 1344 } 1345 1346 /* 1347 * WMI can have EmbeddedControl access regions. In which case, we just want to 1348 * hand these off to the EC driver. 1349 */ 1350 static acpi_status 1351 acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address, 1352 u32 bits, u64 *value, 1353 void *handler_context, void *region_context) 1354 { 1355 int result = 0, i = 0; 1356 u8 temp = 0; 1357 1358 if ((address > 0xFF) || !value) 1359 return AE_BAD_PARAMETER; 1360 1361 if (function != ACPI_READ && function != ACPI_WRITE) 1362 return AE_BAD_PARAMETER; 1363 1364 if (bits != 8) 1365 return AE_BAD_PARAMETER; 1366 1367 if (function == ACPI_READ) { 1368 result = ec_read(address, &temp); 1369 (*value) |= ((u64)temp) << i; 1370 } else { 1371 temp = 0xff & ((*value) >> i); 1372 result = ec_write(address, temp); 1373 } 1374 1375 switch (result) { 1376 case -EINVAL: 1377 return AE_BAD_PARAMETER; 1378 case -ENODEV: 1379 return AE_NOT_FOUND; 1380 case -ETIME: 1381 return AE_TIME; 1382 default: 1383 return AE_OK; 1384 } 1385 } 1386 1387 static void acpi_wmi_notify_handler(acpi_handle handle, u32 event, 1388 void *context) 1389 { 1390 struct wmi_block *wblock = NULL, *iter; 1391 1392 list_for_each_entry(iter, &wmi_block_list, list) { 1393 struct guid_block *block = &iter->gblock; 1394 1395 if (iter->acpi_device->handle == handle && 1396 (block->flags & ACPI_WMI_EVENT) && 1397 (block->notify_id == event)) { 1398 wblock = iter; 1399 break; 1400 } 1401 } 1402 1403 if (!wblock) 1404 return; 1405 1406 /* If a driver is bound, then notify the driver. */ 1407 if (test_bit(WMI_PROBED, &wblock->flags) && wblock->dev.dev.driver) { 1408 struct wmi_driver *driver = drv_to_wdrv(wblock->dev.dev.driver); 1409 struct acpi_buffer evdata = { ACPI_ALLOCATE_BUFFER, NULL }; 1410 acpi_status status; 1411 1412 if (!driver->no_notify_data) { 1413 status = get_event_data(wblock, &evdata); 1414 if (ACPI_FAILURE(status)) { 1415 dev_warn(&wblock->dev.dev, "failed to get event data\n"); 1416 return; 1417 } 1418 } 1419 1420 if (driver->notify) 1421 driver->notify(&wblock->dev, evdata.pointer); 1422 1423 kfree(evdata.pointer); 1424 } else if (wblock->handler) { 1425 /* Legacy handler */ 1426 wblock->handler(event, wblock->handler_data); 1427 } 1428 1429 if (debug_event) 1430 pr_info("DEBUG: GUID %pUL event 0x%02X\n", &wblock->gblock.guid, event); 1431 1432 acpi_bus_generate_netlink_event( 1433 wblock->acpi_device->pnp.device_class, 1434 dev_name(&wblock->dev.dev), 1435 event, 0); 1436 } 1437 1438 static void acpi_wmi_remove(struct platform_device *device) 1439 { 1440 struct acpi_device *acpi_device = ACPI_COMPANION(&device->dev); 1441 1442 acpi_remove_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY, 1443 acpi_wmi_notify_handler); 1444 acpi_remove_address_space_handler(acpi_device->handle, 1445 ACPI_ADR_SPACE_EC, &acpi_wmi_ec_space_handler); 1446 wmi_free_devices(acpi_device); 1447 device_unregister(dev_get_drvdata(&device->dev)); 1448 } 1449 1450 static int acpi_wmi_probe(struct platform_device *device) 1451 { 1452 struct acpi_device *acpi_device; 1453 struct device *wmi_bus_dev; 1454 acpi_status status; 1455 int error; 1456 1457 acpi_device = ACPI_COMPANION(&device->dev); 1458 if (!acpi_device) { 1459 dev_err(&device->dev, "ACPI companion is missing\n"); 1460 return -ENODEV; 1461 } 1462 1463 status = acpi_install_address_space_handler(acpi_device->handle, 1464 ACPI_ADR_SPACE_EC, 1465 &acpi_wmi_ec_space_handler, 1466 NULL, NULL); 1467 if (ACPI_FAILURE(status)) { 1468 dev_err(&device->dev, "Error installing EC region handler\n"); 1469 return -ENODEV; 1470 } 1471 1472 status = acpi_install_notify_handler(acpi_device->handle, 1473 ACPI_ALL_NOTIFY, 1474 acpi_wmi_notify_handler, 1475 NULL); 1476 if (ACPI_FAILURE(status)) { 1477 dev_err(&device->dev, "Error installing notify handler\n"); 1478 error = -ENODEV; 1479 goto err_remove_ec_handler; 1480 } 1481 1482 wmi_bus_dev = device_create(&wmi_bus_class, &device->dev, MKDEV(0, 0), 1483 NULL, "wmi_bus-%s", dev_name(&device->dev)); 1484 if (IS_ERR(wmi_bus_dev)) { 1485 error = PTR_ERR(wmi_bus_dev); 1486 goto err_remove_notify_handler; 1487 } 1488 dev_set_drvdata(&device->dev, wmi_bus_dev); 1489 1490 error = parse_wdg(wmi_bus_dev, acpi_device); 1491 if (error) { 1492 pr_err("Failed to parse WDG method\n"); 1493 goto err_remove_busdev; 1494 } 1495 1496 return 0; 1497 1498 err_remove_busdev: 1499 device_unregister(wmi_bus_dev); 1500 1501 err_remove_notify_handler: 1502 acpi_remove_notify_handler(acpi_device->handle, ACPI_ALL_NOTIFY, 1503 acpi_wmi_notify_handler); 1504 1505 err_remove_ec_handler: 1506 acpi_remove_address_space_handler(acpi_device->handle, 1507 ACPI_ADR_SPACE_EC, 1508 &acpi_wmi_ec_space_handler); 1509 1510 return error; 1511 } 1512 1513 int __must_check __wmi_driver_register(struct wmi_driver *driver, 1514 struct module *owner) 1515 { 1516 driver->driver.owner = owner; 1517 driver->driver.bus = &wmi_bus_type; 1518 1519 return driver_register(&driver->driver); 1520 } 1521 EXPORT_SYMBOL(__wmi_driver_register); 1522 1523 /** 1524 * wmi_driver_unregister() - Unregister a WMI driver 1525 * @driver: WMI driver to unregister 1526 * 1527 * Unregisters a WMI driver from the WMI bus. 1528 */ 1529 void wmi_driver_unregister(struct wmi_driver *driver) 1530 { 1531 driver_unregister(&driver->driver); 1532 } 1533 EXPORT_SYMBOL(wmi_driver_unregister); 1534 1535 static struct platform_driver acpi_wmi_driver = { 1536 .driver = { 1537 .name = "acpi-wmi", 1538 .acpi_match_table = wmi_device_ids, 1539 }, 1540 .probe = acpi_wmi_probe, 1541 .remove_new = acpi_wmi_remove, 1542 }; 1543 1544 static int __init acpi_wmi_init(void) 1545 { 1546 int error; 1547 1548 if (acpi_disabled) 1549 return -ENODEV; 1550 1551 error = class_register(&wmi_bus_class); 1552 if (error) 1553 return error; 1554 1555 error = bus_register(&wmi_bus_type); 1556 if (error) 1557 goto err_unreg_class; 1558 1559 error = platform_driver_register(&acpi_wmi_driver); 1560 if (error) { 1561 pr_err("Error loading mapper\n"); 1562 goto err_unreg_bus; 1563 } 1564 1565 return 0; 1566 1567 err_unreg_bus: 1568 bus_unregister(&wmi_bus_type); 1569 1570 err_unreg_class: 1571 class_unregister(&wmi_bus_class); 1572 1573 return error; 1574 } 1575 1576 static void __exit acpi_wmi_exit(void) 1577 { 1578 platform_driver_unregister(&acpi_wmi_driver); 1579 bus_unregister(&wmi_bus_type); 1580 class_unregister(&wmi_bus_class); 1581 } 1582 1583 subsys_initcall_sync(acpi_wmi_init); 1584 module_exit(acpi_wmi_exit); 1585