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