wmi_acpi.c revision 1.6
1/* $NetBSD: wmi_acpi.c,v 1.6 2010/07/29 07:10:39 jruoho Exp $ */ 2 3/*- 4 * Copyright (c) 2009, 2010 Jukka Ruohonen <jruohonen@iki.fi> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29#include <sys/cdefs.h> 30__KERNEL_RCSID(0, "$NetBSD: wmi_acpi.c,v 1.6 2010/07/29 07:10:39 jruoho Exp $"); 31 32#include <sys/param.h> 33#include <sys/device.h> 34#include <sys/endian.h> 35#include <sys/kmem.h> 36#include <sys/systm.h> 37 38#include <dev/acpi/acpireg.h> 39#include <dev/acpi/acpivar.h> 40#include <dev/acpi/wmi/wmi_acpivar.h> 41 42#define _COMPONENT ACPI_RESOURCE_COMPONENT 43ACPI_MODULE_NAME ("wmi_acpi") 44 45/* 46 * This implements something called "Microsoft Windows Management 47 * Instrumentation" (WMI). This subset of ACPI is desribed in: 48 * 49 * http://www.microsoft.com/whdc/system/pnppwr/wmi/wmi-acpi.mspx 50 * 51 * (Obtained on Thu Feb 12 18:21:44 EET 2009.) 52 */ 53 54static int acpi_wmi_match(device_t, cfdata_t, void *); 55static void acpi_wmi_attach(device_t, device_t, void *); 56static int acpi_wmi_detach(device_t, int); 57static int acpi_wmi_print(void *, const char *); 58static bool acpi_wmi_init(struct acpi_wmi_softc *); 59static bool acpi_wmi_add(struct acpi_wmi_softc *, ACPI_OBJECT *); 60static void acpi_wmi_del(struct acpi_wmi_softc *); 61 62static ACPI_STATUS acpi_wmi_guid_get(struct acpi_wmi_softc *, 63 const char *, struct wmi_t **); 64static void acpi_wmi_event_add(struct acpi_wmi_softc *); 65static void acpi_wmi_event_del(struct acpi_wmi_softc *); 66static void acpi_wmi_event_handler(ACPI_HANDLE, uint32_t, void *); 67static bool acpi_wmi_suspend(device_t, const pmf_qual_t *); 68static bool acpi_wmi_resume(device_t, const pmf_qual_t *); 69static ACPI_STATUS acpi_wmi_enable(ACPI_HANDLE, const char *, bool, bool); 70static bool acpi_wmi_input(struct wmi_t *, uint8_t, uint8_t); 71 72const char * const acpi_wmi_ids[] = { 73 "PNP0C14", 74 "pnp0c14", 75 NULL 76}; 77 78CFATTACH_DECL_NEW(acpiwmi, sizeof(struct acpi_wmi_softc), 79 acpi_wmi_match, acpi_wmi_attach, acpi_wmi_detach, NULL); 80 81static int 82acpi_wmi_match(device_t parent, cfdata_t match, void *aux) 83{ 84 struct acpi_attach_args *aa = aux; 85 86 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) 87 return 0; 88 89 return acpi_match_hid(aa->aa_node->ad_devinfo, acpi_wmi_ids); 90} 91 92static void 93acpi_wmi_attach(device_t parent, device_t self, void *aux) 94{ 95 struct acpi_wmi_softc *sc = device_private(self); 96 struct acpi_attach_args *aa = aux; 97 98 sc->sc_dev = self; 99 sc->sc_node = aa->aa_node; 100 101 sc->sc_child = NULL; 102 sc->sc_handler = NULL; 103 104 aprint_naive("\n"); 105 aprint_normal(": ACPI WMI Interface\n"); 106 107 if (acpi_wmi_init(sc) != true) 108 return; 109 110 acpi_wmidump(sc); 111 112 acpi_wmi_event_add(sc); 113 114 sc->sc_child = config_found_ia(self, "acpiwmibus", 115 NULL, acpi_wmi_print); 116 117 (void)pmf_device_register(self, acpi_wmi_suspend, acpi_wmi_resume); 118} 119 120static int 121acpi_wmi_detach(device_t self, int flags) 122{ 123 struct acpi_wmi_softc *sc = device_private(self); 124 125 acpi_wmi_event_del(sc); 126 127 if (sc->sc_child != NULL) 128 (void)config_detach(sc->sc_child, flags); 129 130 acpi_wmi_del(sc); 131 pmf_device_deregister(self); 132 133 return 0; 134} 135 136static int 137acpi_wmi_print(void *aux, const char *pnp) 138{ 139 140 if (pnp != NULL) 141 aprint_normal("acpiwmibus at %s", pnp); 142 143 return UNCONF; 144} 145 146static bool 147acpi_wmi_init(struct acpi_wmi_softc *sc) 148{ 149 ACPI_OBJECT *obj; 150 ACPI_BUFFER buf; 151 ACPI_STATUS rv; 152 uint32_t len; 153 154 rv = acpi_eval_struct(sc->sc_node->ad_handle, "_WDG", &buf); 155 156 if (ACPI_FAILURE(rv)) 157 goto fail; 158 159 obj = buf.Pointer; 160 161 if (obj->Type != ACPI_TYPE_BUFFER) { 162 rv = AE_TYPE; 163 goto fail; 164 } 165 166 len = obj->Buffer.Length; 167 168 if (len != obj->Package.Count) { 169 rv = AE_BAD_VALUE; 170 goto fail; 171 } 172 173 CTASSERT(sizeof(struct guid_t) == 20); 174 175 if (len < sizeof(struct guid_t) || 176 len % sizeof(struct guid_t) != 0) { 177 rv = AE_BAD_DATA; 178 goto fail; 179 } 180 181 return acpi_wmi_add(sc, obj); 182 183fail: 184 aprint_error_dev(sc->sc_dev, "failed to evaluate _WDG: %s\n", 185 AcpiFormatException(rv)); 186 187 if (buf.Pointer != NULL) 188 ACPI_FREE(buf.Pointer); 189 190 return false; 191} 192 193static bool 194acpi_wmi_add(struct acpi_wmi_softc *sc, ACPI_OBJECT *obj) 195{ 196 struct wmi_t *wmi; 197 size_t i, n, offset, siz; 198 199 siz = sizeof(struct guid_t); 200 n = obj->Buffer.Length / siz; 201 202 SIMPLEQ_INIT(&sc->wmi_head); 203 204 for (i = offset = 0; i < n; ++i) { 205 206 if ((wmi = kmem_zalloc(sizeof(*wmi), KM_NOSLEEP)) == NULL) 207 goto fail; 208 209 (void)memcpy(&wmi->guid, obj->Buffer.Pointer + offset, siz); 210 211 wmi->eevent = false; 212 offset = offset + siz; 213 214 SIMPLEQ_INSERT_TAIL(&sc->wmi_head, wmi, wmi_link); 215 } 216 217 ACPI_FREE(obj); 218 219 return true; 220 221fail: 222 ACPI_FREE(obj); 223 acpi_wmi_del(sc); 224 225 return false; 226} 227 228static void 229acpi_wmi_del(struct acpi_wmi_softc *sc) 230{ 231 struct wmi_t *wmi; 232 233 if (SIMPLEQ_EMPTY(&sc->wmi_head) != 0) 234 return; 235 236 while (SIMPLEQ_FIRST(&sc->wmi_head) != NULL) { 237 238 wmi = SIMPLEQ_FIRST(&sc->wmi_head); 239 SIMPLEQ_REMOVE_HEAD(&sc->wmi_head, wmi_link); 240 241 KASSERT(wmi != NULL); 242 243 kmem_free(wmi, sizeof(*wmi)); 244 } 245} 246 247static ACPI_STATUS 248acpi_wmi_guid_get(struct acpi_wmi_softc *sc, 249 const char *src, struct wmi_t **out) 250{ 251 struct wmi_t *wmi; 252 struct guid_t *guid; 253 char bin[16]; 254 char hex[2]; 255 const char *ptr; 256 uint8_t i; 257 258 if (sc == NULL || src == NULL || strlen(src) != 36) 259 return AE_BAD_PARAMETER; 260 261 for (ptr = src, i = 0; i < 16; i++) { 262 263 if (*ptr == '-') 264 ptr++; 265 266 (void)memcpy(hex, ptr, 2); 267 268 if (HEXCHAR(hex[0]) == 0 || HEXCHAR(hex[1]) == 0) 269 return AE_BAD_HEX_CONSTANT; 270 271 bin[i] = strtoul(hex, NULL, 16) & 0xFF; 272 273 ptr++; 274 ptr++; 275 } 276 277 guid = (struct guid_t *)bin; 278 guid->data1 = be32toh(guid->data1); 279 guid->data2 = be16toh(guid->data2); 280 guid->data3 = be16toh(guid->data3); 281 282 SIMPLEQ_FOREACH(wmi, &sc->wmi_head, wmi_link) { 283 284 if (GUIDCMP(guid, &wmi->guid) != 0) { 285 286 if (out != NULL) 287 *out = wmi; 288 289 return AE_OK; 290 } 291 } 292 293 return AE_NOT_FOUND; 294} 295 296/* 297 * Checks if a GUID is present. Child devices 298 * can use this in their autoconf(9) routines. 299 */ 300int 301acpi_wmi_guid_match(device_t self, const char *guid) 302{ 303 struct acpi_wmi_softc *sc = device_private(self); 304 ACPI_STATUS rv; 305 306 rv = acpi_wmi_guid_get(sc, guid, NULL); 307 308 if (ACPI_SUCCESS(rv)) 309 return 1; 310 311 return 0; 312} 313 314/* 315 * Adds internal event handler. 316 */ 317static void 318acpi_wmi_event_add(struct acpi_wmi_softc *sc) 319{ 320 struct wmi_t *wmi; 321 ACPI_STATUS rv; 322 323 if (acpi_register_notify(sc->sc_node, acpi_wmi_event_handler) != true) 324 return; 325 326 /* 327 * Enable possible expensive events. 328 */ 329 SIMPLEQ_FOREACH(wmi, &sc->wmi_head, wmi_link) { 330 331 if ((wmi->guid.flags & ACPI_WMI_FLAG_EVENT) != 0 && 332 (wmi->guid.flags & ACPI_WMI_FLAG_EXPENSIVE) != 0) { 333 334 rv = acpi_wmi_enable(sc->sc_node->ad_handle, 335 wmi->guid.oid, false, true); 336 337 if (ACPI_SUCCESS(rv)) { 338 wmi->eevent = true; 339 continue; 340 } 341 342 aprint_debug_dev(sc->sc_dev, "failed to enable " 343 "expensive WExx: %s\n", AcpiFormatException(rv)); 344 } 345 } 346} 347 348/* 349 * Removes the internal event handler. 350 */ 351static void 352acpi_wmi_event_del(struct acpi_wmi_softc *sc) 353{ 354 struct wmi_t *wmi; 355 ACPI_STATUS rv; 356 357 acpi_deregister_notify(sc->sc_node); 358 359 SIMPLEQ_FOREACH(wmi, &sc->wmi_head, wmi_link) { 360 361 if (wmi->eevent != true) 362 continue; 363 364 KASSERT((wmi->guid.flags & ACPI_WMI_FLAG_EVENT) != 0); 365 KASSERT((wmi->guid.flags & ACPI_WMI_FLAG_EXPENSIVE) != 0); 366 367 rv = acpi_wmi_enable(sc->sc_node->ad_handle, 368 wmi->guid.oid, false, false); 369 370 if (ACPI_SUCCESS(rv)) { 371 wmi->eevent = false; 372 continue; 373 } 374 375 aprint_debug_dev(sc->sc_dev, "failed to disable " 376 "expensive WExx: %s\n", AcpiFormatException(rv)); 377 } 378} 379 380/* 381 * Returns extra information possibly associated with an event. 382 */ 383ACPI_STATUS 384acpi_wmi_event_get(device_t self, uint32_t event, ACPI_BUFFER *obuf) 385{ 386 struct acpi_wmi_softc *sc = device_private(self); 387 struct wmi_t *wmi; 388 ACPI_OBJECT_LIST arg; 389 ACPI_OBJECT obj; 390 ACPI_HANDLE hdl; 391 392 if (sc == NULL || obuf == NULL) 393 return AE_BAD_PARAMETER; 394 395 if (sc->sc_handler == NULL) 396 return AE_ABORT_METHOD; 397 398 hdl = sc->sc_node->ad_handle; 399 400 obj.Type = ACPI_TYPE_INTEGER; 401 obj.Integer.Value = event; 402 403 arg.Count = 0x01; 404 arg.Pointer = &obj; 405 406 obuf->Pointer = NULL; 407 obuf->Length = ACPI_ALLOCATE_LOCAL_BUFFER; 408 409 SIMPLEQ_FOREACH(wmi, &sc->wmi_head, wmi_link) { 410 411 if ((wmi->guid.flags & ACPI_WMI_FLAG_EVENT) == 0) 412 continue; 413 414 if (wmi->guid.nid != event) 415 continue; 416 417 return AcpiEvaluateObject(hdl, "_WED", &arg, obuf); 418 } 419 420 return AE_NOT_FOUND; 421} 422 423/* 424 * Forwards events to the external handler through the internal one. 425 */ 426static void 427acpi_wmi_event_handler(ACPI_HANDLE hdl, uint32_t evt, void *aux) 428{ 429 struct acpi_wmi_softc *sc; 430 device_t self = aux; 431 432 sc = device_private(self); 433 434 if (sc->sc_child == NULL) 435 return; 436 437 if (sc->sc_handler == NULL) 438 return; 439 440 (*sc->sc_handler)(NULL, evt, sc->sc_child); 441} 442 443ACPI_STATUS 444acpi_wmi_event_register(device_t self, ACPI_NOTIFY_HANDLER handler) 445{ 446 struct acpi_wmi_softc *sc = device_private(self); 447 448 if (sc == NULL) 449 return AE_BAD_PARAMETER; 450 451 if (handler != NULL && sc->sc_handler != NULL) 452 return AE_ALREADY_EXISTS; 453 454 sc->sc_handler = handler; 455 456 return AE_OK; 457} 458 459ACPI_STATUS 460acpi_wmi_event_deregister(device_t self) 461{ 462 return acpi_wmi_event_register(self, NULL); 463} 464 465/* 466 * As there is no prior knowledge about the expensive 467 * events that cause "significant overhead", try to 468 * disable (enable) these before suspending (resuming). 469 */ 470static bool 471acpi_wmi_suspend(device_t self, const pmf_qual_t *qual) 472{ 473 struct acpi_wmi_softc *sc = device_private(self); 474 475 acpi_wmi_event_del(sc); 476 477 return true; 478} 479 480static bool 481acpi_wmi_resume(device_t self, const pmf_qual_t *qual) 482{ 483 struct acpi_wmi_softc *sc = device_private(self); 484 485 acpi_wmi_event_add(sc); 486 487 return true; 488} 489 490/* 491 * Enables or disables data collection (WCxx) or an event (WExx). 492 */ 493static ACPI_STATUS 494acpi_wmi_enable(ACPI_HANDLE hdl, const char *oid, bool data, bool flag) 495{ 496 char path[5]; 497 const char *str; 498 499 str = (data != false) ? "WC" : "WE"; 500 501 (void)strlcpy(path, str, sizeof(path)); 502 (void)strlcat(path, oid, sizeof(path)); 503 504 return acpi_eval_set_integer(hdl, path, (flag != false) ? 0x01 : 0x00); 505} 506 507static bool 508acpi_wmi_input(struct wmi_t *wmi, uint8_t flag, uint8_t idx) 509{ 510 511 if ((wmi->guid.flags & flag) == 0) 512 return false; 513 514 if (wmi->guid.count == 0x00) 515 return false; 516 517 if (wmi->guid.count < idx) 518 return false; 519 520 return true; 521} 522 523/* 524 * Makes a WMI data block query (WQxx). The corresponding control 525 * method for data collection will be invoked if it is available. 526 */ 527ACPI_STATUS 528acpi_wmi_data_query(device_t self, const char *guid, 529 uint8_t idx, ACPI_BUFFER *obuf) 530{ 531 struct acpi_wmi_softc *sc = device_private(self); 532 struct wmi_t *wmi; 533 char path[5] = "WQ"; 534 ACPI_OBJECT_LIST arg; 535 ACPI_STATUS rv, rvxx; 536 ACPI_OBJECT obj; 537 538 rvxx = AE_SUPPORT; 539 540 if (obuf == NULL) 541 return AE_BAD_PARAMETER; 542 543 rv = acpi_wmi_guid_get(sc, guid, &wmi); 544 545 if (ACPI_FAILURE(rv)) 546 return rv; 547 548 if (acpi_wmi_input(wmi, ACPI_WMI_FLAG_DATA, idx) != true) 549 return AE_BAD_DATA; 550 551 (void)strlcat(path, wmi->guid.oid, sizeof(path)); 552 553 obj.Type = ACPI_TYPE_INTEGER; 554 obj.Integer.Value = idx; 555 556 arg.Count = 0x01; 557 arg.Pointer = &obj; 558 559 obuf->Pointer = NULL; 560 obuf->Length = ACPI_ALLOCATE_LOCAL_BUFFER; 561 562 /* 563 * If the expensive flag is set, we should enable 564 * data collection before evaluating the WQxx buffer. 565 */ 566 if ((wmi->guid.flags & ACPI_WMI_FLAG_EXPENSIVE) != 0) { 567 568 rvxx = acpi_wmi_enable(sc->sc_node->ad_handle, 569 wmi->guid.oid, true, true); 570 } 571 572 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, path, &arg, obuf); 573 574 /* No longer needed. */ 575 if (ACPI_SUCCESS(rvxx)) { 576 577 (void)acpi_wmi_enable(sc->sc_node->ad_handle, 578 wmi->guid.oid, true, false); 579 } 580 581#ifdef DIAGNOSTIC 582 /* 583 * XXX: It appears that quite a few laptops have WQxx 584 * methods that are declared as expensive, but lack the 585 * corresponding WCxx control method. 586 * 587 * -- Acer Aspire One is one example <jruohonen@iki.fi>. 588 */ 589 if (ACPI_FAILURE(rvxx) && rvxx != AE_SUPPORT) 590 aprint_error_dev(sc->sc_dev, "failed to evaluate WCxx " 591 "for %s: %s\n", path, AcpiFormatException(rvxx)); 592#endif 593 return rv; 594} 595 596/* 597 * Writes to a data block (WSxx). 598 */ 599ACPI_STATUS 600acpi_wmi_data_write(device_t self, const char *guid, 601 uint8_t idx, ACPI_BUFFER *ibuf) 602{ 603 struct acpi_wmi_softc *sc = device_private(self); 604 struct wmi_t *wmi; 605 ACPI_OBJECT_LIST arg; 606 ACPI_OBJECT obj[2]; 607 char path[5] = "WS"; 608 ACPI_STATUS rv; 609 610 if (ibuf == NULL) 611 return AE_BAD_PARAMETER; 612 613 rv = acpi_wmi_guid_get(sc, guid, &wmi); 614 615 if (ACPI_FAILURE(rv)) 616 return rv; 617 618 if (acpi_wmi_input(wmi, ACPI_WMI_FLAG_DATA, idx) != true) 619 return AE_BAD_DATA; 620 621 (void)strlcat(path, wmi->guid.oid, sizeof(path)); 622 623 obj[0].Integer.Value = idx; 624 obj[0].Type = ACPI_TYPE_INTEGER; 625 626 obj[1].Buffer.Length = ibuf->Length; 627 obj[1].Buffer.Pointer = ibuf->Pointer; 628 629 obj[1].Type = ((wmi->guid.flags & ACPI_WMI_FLAG_STRING) != 0) ? 630 ACPI_TYPE_STRING : ACPI_TYPE_BUFFER; 631 632 arg.Count = 0x02; 633 arg.Pointer = obj; 634 635 return AcpiEvaluateObject(sc->sc_node->ad_handle, path, &arg, NULL); 636} 637 638/* 639 * Executes a method (WMxx). 640 */ 641ACPI_STATUS 642acpi_wmi_method(device_t self, const char *guid, uint8_t idx, 643 uint32_t mid, ACPI_BUFFER *ibuf, ACPI_BUFFER *obuf) 644{ 645 struct acpi_wmi_softc *sc = device_private(self); 646 struct wmi_t *wmi; 647 ACPI_OBJECT_LIST arg; 648 ACPI_OBJECT obj[3]; 649 char path[5] = "WM"; 650 ACPI_STATUS rv; 651 652 if (ibuf == NULL || obuf == NULL) 653 return AE_BAD_PARAMETER; 654 655 rv = acpi_wmi_guid_get(sc, guid, &wmi); 656 657 if (ACPI_FAILURE(rv)) 658 return rv; 659 660 if (acpi_wmi_input(wmi, ACPI_WMI_FLAG_METHOD, idx) != true) 661 return AE_BAD_DATA; 662 663 (void)strlcat(path, wmi->guid.oid, sizeof(path)); 664 665 obj[0].Integer.Value = idx; 666 obj[1].Integer.Value = mid; 667 obj[0].Type = obj[1].Type = ACPI_TYPE_INTEGER; 668 669 obj[2].Buffer.Length = ibuf->Length; 670 obj[2].Buffer.Pointer = ibuf->Pointer; 671 672 obj[2].Type = ((wmi->guid.flags & ACPI_WMI_FLAG_STRING) != 0) ? 673 ACPI_TYPE_STRING : ACPI_TYPE_BUFFER; 674 675 arg.Count = 0x03; 676 arg.Pointer = obj; 677 678 obuf->Pointer = NULL; 679 obuf->Length = ACPI_ALLOCATE_LOCAL_BUFFER; 680 681 return AcpiEvaluateObject(sc->sc_node->ad_handle, path, &arg, obuf); 682} 683