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