acpi_wmi.c revision 194701
1/*- 2 * Copyright (c) 2009 Michael Gmelin <freebsd@grem.de> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/dev/acpi_support/acpi_wmi.c 194701 2009-06-23 13:17:25Z rpaulo $"); 29 30/* 31 * Driver for acpi-wmi mapping, provides an interface for vendor specific 32 * implementations (e.g. HP and Acer laptops). 33 * Inspired by the ACPI-WMI mapping driver (c) 2008-2008 Carlos Corbacho which 34 * implements this functionality for Linux. 35 * 36 * WMI and ACPI: http://www.microsoft.com/whdc/system/pnppwr/wmi/wmi-acpi.mspx 37 * acpi-wmi for Linux: http://www.kernel.org 38 */ 39 40#include "opt_acpi.h" 41#include <sys/param.h> 42#include <sys/conf.h> 43#include <sys/uio.h> 44#include <sys/proc.h> 45#include <sys/kernel.h> 46#include <sys/malloc.h> 47#include <sys/sbuf.h> 48#include <sys/module.h> 49#include <sys/bus.h> 50 51#include <contrib/dev/acpica/include/acpi.h> 52#include <contrib/dev/acpica/include/accommon.h> 53#include <dev/acpica/acpivar.h> 54#include "acpi_wmi_if.h" 55 56MALLOC_DEFINE(M_ACPIWMI, "acpiwmi", "ACPI-WMI mapping"); 57 58#define _COMPONENT ACPI_OEM 59ACPI_MODULE_NAME("ACPI_WMI"); 60 61#define ACPI_WMI_REGFLAG_EXPENSIVE 0x1 /* GUID flag: Expensive operation */ 62#define ACPI_WMI_REGFLAG_METHOD 0x2 /* GUID flag: Method call */ 63#define ACPI_WMI_REGFLAG_STRING 0x4 /* GUID flag: String */ 64#define ACPI_WMI_REGFLAG_EVENT 0x8 /* GUID flag: Event */ 65 66/* 67 * acpi_wmi driver private structure 68 */ 69struct acpi_wmi_softc { 70 device_t wmi_dev; /* wmi device id */ 71 ACPI_HANDLE wmi_handle; /* handle of the PNP0C14 node */ 72 device_t ec_dev; /* acpi_ec0 */ 73 struct cdev *wmistat_dev_t; /* wmistat device handle */ 74 struct sbuf wmistat_sbuf; /* sbuf for /dev/wmistat output */ 75 pid_t wmistat_open_pid; /* pid operating on /dev/wmistat */ 76 int wmistat_bufptr; /* /dev/wmistat ptr to buffer position */ 77}; 78 79/* 80 * Struct that holds information about 81 * about a single GUID entry in _WDG 82 */ 83struct guid_info { 84 char guid[16]; /* 16 byte non human readable GUID */ 85 char oid[2]; /* object id or event notify id (first byte) */ 86 UINT8 max_instance; /* highest instance known for this GUID */ 87 UINT8 flags; /* ACPI_WMI_REGFLAG_%s */ 88}; 89 90/* WExx event generation state (on/off) */ 91enum event_generation_state { 92 EVENT_GENERATION_ON = 1, 93 EVENT_GENERATION_OFF = 0 94}; 95 96 97/* 98 * Information about one entry in _WDG. 99 * List of those is used to lookup information by GUID. 100 */ 101struct wmi_info { 102 TAILQ_ENTRY(wmi_info) wmi_list; 103 struct guid_info ginfo; /* information on guid */ 104 ACPI_NOTIFY_HANDLER event_handler;/* client provided event handler */ 105 void *event_handler_user_data; /* ev handler cookie */ 106}; 107 108TAILQ_HEAD(wmi_info_list_head, wmi_info) 109 wmi_info_list = TAILQ_HEAD_INITIALIZER(wmi_info_list); 110 111ACPI_SERIAL_DECL(acpi_wmi, "ACPI-WMI Mapping"); 112 113/* public interface - declaration */ 114/* standard device interface*/ 115static int acpi_wmi_probe(device_t dev); 116static int acpi_wmi_attach(device_t dev); 117static int acpi_wmi_detach(device_t dev); 118/* see acpi_wmi_if.m */ 119static int acpi_wmi_provides_guid_string_method(device_t dev, 120 const char *guid_string); 121static ACPI_STATUS acpi_wmi_evaluate_call_method(device_t dev, 122 const char *guid_string, UINT8 instance, 123 UINT32 method_id, const ACPI_BUFFER *in, 124 ACPI_BUFFER *out); 125static ACPI_STATUS acpi_wmi_install_event_handler_method(device_t dev, 126 const char *guid_string, ACPI_NOTIFY_HANDLER handler, 127 void *data); 128static ACPI_STATUS acpi_wmi_remove_event_handler_method(device_t dev, 129 const char *guid_string); 130static ACPI_STATUS acpi_wmi_get_event_data_method(device_t dev, 131 UINT32 event_id, ACPI_BUFFER *out); 132static ACPI_STATUS acpi_wmi_get_block_method(device_t dev, 133 const char *guid_string, 134 UINT8 instance, ACPI_BUFFER *out); 135static ACPI_STATUS acpi_wmi_set_block_method(device_t dev, 136 const char *guid_string, 137 UINT8 instance, const ACPI_BUFFER *in); 138/* private interface - declaration */ 139/* callbacks */ 140static void acpi_wmi_notify_handler(ACPI_HANDLE h, UINT32 notify, 141 void *context); 142static ACPI_STATUS acpi_wmi_ec_handler(UINT32 function, 143 ACPI_PHYSICAL_ADDRESS address, UINT32 width, 144 ACPI_INTEGER *value, void *context, 145 void *region_context); 146/* helpers */ 147static ACPI_STATUS acpi_wmi_read_wdg_blocks(ACPI_HANDLE h); 148static ACPI_STATUS acpi_wmi_toggle_we_event_generation(device_t dev, 149 struct wmi_info *winfo, 150 enum event_generation_state state); 151static int acpi_wmi_guid_string_to_guid(const UINT8 *guid_string, 152 UINT8 *guid); 153static struct wmi_info* acpi_wmi_lookup_wmi_info_by_guid_string( 154 const char *guid_string); 155 156static d_open_t acpi_wmi_wmistat_open; 157static d_close_t acpi_wmi_wmistat_close; 158static d_read_t acpi_wmi_wmistat_read; 159 160/* handler /dev/wmistat device */ 161static struct cdevsw wmistat_cdevsw = { 162 .d_version = D_VERSION, 163 .d_open = acpi_wmi_wmistat_open, 164 .d_close = acpi_wmi_wmistat_close, 165 .d_read = acpi_wmi_wmistat_read, 166 .d_name = "wmistat", 167}; 168 169 170static device_method_t acpi_wmi_methods[] = { 171 /* Device interface */ 172 DEVMETHOD(device_probe, acpi_wmi_probe), 173 DEVMETHOD(device_attach, acpi_wmi_attach), 174 DEVMETHOD(device_detach, acpi_wmi_detach), 175 176 /* acpi_wmi interface */ 177 DEVMETHOD(acpi_wmi_provides_guid_string, 178 acpi_wmi_provides_guid_string_method), 179 DEVMETHOD(acpi_wmi_evaluate_call, acpi_wmi_evaluate_call_method), 180 DEVMETHOD(acpi_wmi_install_event_handler, 181 acpi_wmi_install_event_handler_method), 182 DEVMETHOD(acpi_wmi_remove_event_handler, 183 acpi_wmi_remove_event_handler_method), 184 DEVMETHOD(acpi_wmi_get_event_data, acpi_wmi_get_event_data_method), 185 DEVMETHOD(acpi_wmi_get_block, acpi_wmi_get_block_method), 186 DEVMETHOD(acpi_wmi_set_block, acpi_wmi_set_block_method), 187 188 {0, 0} 189}; 190 191static driver_t acpi_wmi_driver = { 192 "acpi_wmi", 193 acpi_wmi_methods, 194 sizeof(struct acpi_wmi_softc), 195}; 196 197static devclass_t acpi_wmi_devclass; 198DRIVER_MODULE(acpi_wmi, acpi, acpi_wmi_driver, acpi_wmi_devclass, 0, 0); 199MODULE_VERSION(acpi_wmi, 1); 200MODULE_DEPEND(acpi_wmi, acpi, 1, 1, 1); 201static char *wmi_ids[] = {"PNP0C14", "PNP0c14", NULL}; 202 203/* 204 * Probe for the PNP0C14 ACPI node 205 */ 206static int 207acpi_wmi_probe(device_t dev) 208{ 209 if (acpi_disabled("wmi") || 210 ACPI_ID_PROBE(device_get_parent(dev), dev, wmi_ids) == NULL) 211 return (ENXIO); 212 device_set_desc(dev, "ACPI-WMI mapping"); 213 214 return (0); 215} 216 217/* 218 * Attach the device by: 219 * - Looking for the first ACPI EC device 220 * - Install the notify handler 221 * - Install the EC address space handler 222 * - Look for the _WDG node and read GUID information blocks 223 */ 224static int 225acpi_wmi_attach(device_t dev) 226{ 227 struct acpi_wmi_softc *sc; 228 int ret; 229 ACPI_STATUS status; 230 231 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 232 sc = device_get_softc(dev); 233 ret = ENXIO; 234 235 ACPI_SERIAL_BEGIN(acpi_wmi); 236 sc->wmi_dev = dev; 237 sc->wmi_handle = acpi_get_handle(dev); 238 TAILQ_INIT(&wmi_info_list); 239 /* XXX Only works with one EC, but nearly all systems only have one. */ 240 if ((sc->ec_dev = devclass_get_device(devclass_find("acpi_ec"), 0)) 241 == NULL) 242 device_printf(dev, "cannot find EC device\n"); 243 else if (ACPI_FAILURE((status = AcpiInstallNotifyHandler(sc->wmi_handle, 244 ACPI_DEVICE_NOTIFY, acpi_wmi_notify_handler, sc)))) 245 device_printf(sc->wmi_dev, "couldn't install notify handler - %s\n", 246 AcpiFormatException(status)); 247 else if (ACPI_FAILURE((status = AcpiInstallAddressSpaceHandler( 248 sc->wmi_handle, ACPI_ADR_SPACE_EC, acpi_wmi_ec_handler, 249 NULL, sc)))) { 250 device_printf(sc->wmi_dev, "couldn't install EC handler - %s\n", 251 AcpiFormatException(status)); 252 AcpiRemoveNotifyHandler(sc->wmi_handle, ACPI_DEVICE_NOTIFY, 253 acpi_wmi_notify_handler); 254 } else if (ACPI_FAILURE((status = acpi_wmi_read_wdg_blocks( 255 sc->wmi_handle)))) { 256 device_printf(sc->wmi_dev, "couldn't parse _WDG - %s\n", 257 AcpiFormatException(status)); 258 AcpiRemoveNotifyHandler(sc->wmi_handle, ACPI_DEVICE_NOTIFY, 259 acpi_wmi_notify_handler); 260 AcpiRemoveAddressSpaceHandler(sc->wmi_handle, ACPI_ADR_SPACE_EC, 261 acpi_wmi_ec_handler); 262 } else { 263 sc->wmistat_dev_t = make_dev(&wmistat_cdevsw, 0, UID_ROOT, 264 GID_WHEEL, 0644, "wmistat"); 265 sc->wmistat_dev_t->si_drv1 = sc; 266 sc->wmistat_open_pid = 0; 267 sc->wmistat_bufptr = -1; 268 ret = 0; 269 } 270 ACPI_SERIAL_END(acpi_wmi); 271 272 return (ret); 273} 274 275/* 276 * Detach the driver by: 277 * - Removing notification handler 278 * - Removing address space handler 279 * - Turning off event generation for all WExx event activated by 280 * child drivers 281 */ 282static int 283acpi_wmi_detach(device_t dev) 284{ 285 struct wmi_info *winfo, *tmp; 286 struct acpi_wmi_softc *sc; 287 int ret; 288 289 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 290 sc = device_get_softc(dev); 291 ACPI_SERIAL_BEGIN(acpi_wmi); 292 293 if (sc->wmistat_open_pid != 0) { 294 ret = EBUSY; 295 } else { 296 AcpiRemoveNotifyHandler(sc->wmi_handle, ACPI_DEVICE_NOTIFY, 297 acpi_wmi_notify_handler); 298 AcpiRemoveAddressSpaceHandler(sc->wmi_handle, 299 ACPI_ADR_SPACE_EC, acpi_wmi_ec_handler); 300 TAILQ_FOREACH_SAFE(winfo, &wmi_info_list, wmi_list, tmp) { 301 if (winfo->event_handler) 302 acpi_wmi_toggle_we_event_generation(dev, 303 winfo, EVENT_GENERATION_OFF); 304 TAILQ_REMOVE(&wmi_info_list, winfo, wmi_list); 305 free(winfo, M_ACPIWMI); 306 } 307 if (sc->wmistat_bufptr != -1) { 308 sbuf_delete(&sc->wmistat_sbuf); 309 sc->wmistat_bufptr = -1; 310 } 311 sc->wmistat_open_pid = 0; 312 destroy_dev(sc->wmistat_dev_t); 313 ret = 0; 314 } 315 ACPI_SERIAL_END(acpi_wmi); 316 317 return (ret); 318} 319 320 321/* 322 * Check if the given GUID string (human readable format 323 * AABBCCDD-EEFF-GGHH-IIJJ-KKLLMMNNOOPP) 324 * exists within _WDG 325 */ 326static int 327acpi_wmi_provides_guid_string_method(device_t dev, const char *guid_string) 328{ 329 int ret; 330 331 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 332 ACPI_SERIAL_BEGIN(acpi_wmi); 333 ret = (acpi_wmi_lookup_wmi_info_by_guid_string(guid_string) == NULL)?0:1; 334 ACPI_SERIAL_END(acpi_wmi); 335 336 return (ret); 337} 338 339/* 340 * Call a method "method_id" on the given GUID block 341 * write result into user provided output buffer 342 */ 343static ACPI_STATUS 344acpi_wmi_evaluate_call_method(device_t dev, const char *guid_string, 345 UINT8 instance, UINT32 method_id, const ACPI_BUFFER *in, ACPI_BUFFER *out) 346{ 347 ACPI_OBJECT params[3]; 348 ACPI_OBJECT_LIST input; 349 char method[5] = "WMxx"; 350 struct wmi_info *winfo; 351 struct acpi_wmi_softc *sc; 352 ACPI_STATUS status; 353 354 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 355 356 sc = device_get_softc(dev); 357 ACPI_SERIAL_BEGIN(acpi_wmi); 358 if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(guid_string)) 359 == NULL) 360 status = AE_NOT_FOUND; 361 else if (!(winfo->ginfo.flags & ACPI_WMI_REGFLAG_METHOD)) 362 status = AE_BAD_DATA; 363 else if (instance > winfo->ginfo.max_instance) 364 status = AE_BAD_PARAMETER; 365 else { 366 params[0].Type = ACPI_TYPE_INTEGER; 367 params[0].Integer.Value = instance; 368 params[1].Type = ACPI_TYPE_INTEGER; 369 params[1].Integer.Value = method_id; 370 input.Pointer = params; 371 input.Count = 2; 372 if (in) { 373 params[2].Type = 374 (winfo->ginfo.flags & ACPI_WMI_REGFLAG_STRING) 375 ?ACPI_TYPE_STRING:ACPI_TYPE_BUFFER; 376 params[2].Buffer.Length = in->Length; 377 params[2].Buffer.Pointer = in->Pointer; 378 input.Count = 3; 379 } 380 method[2] = winfo->ginfo.oid[0]; 381 method[3] = winfo->ginfo.oid[1]; 382 status = AcpiEvaluateObject(sc->wmi_handle, method, 383 &input, out); 384 } 385 ACPI_SERIAL_END(acpi_wmi); 386 387 return (status); 388} 389 390/* 391 * Install a user provided event_handler on the given GUID 392 * provided *data will be passed on callback 393 * If there is already an existing event handler registered it will be silently 394 * discarded 395 */ 396static ACPI_STATUS 397acpi_wmi_install_event_handler_method(device_t dev, const char *guid_string, 398 ACPI_NOTIFY_HANDLER event_handler, void *data) 399{ 400 struct wmi_info *winfo; 401 ACPI_STATUS status; 402 403 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 404 405 status = AE_OK; 406 ACPI_SERIAL_BEGIN(acpi_wmi); 407 if (guid_string == NULL || event_handler == NULL) 408 status = AE_BAD_PARAMETER; 409 else if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(guid_string)) 410 == NULL) 411 status = AE_NOT_EXIST; 412 else if (winfo->event_handler != NULL || 413 (status = acpi_wmi_toggle_we_event_generation(dev, winfo, 414 EVENT_GENERATION_ON)) == AE_OK) { 415 winfo->event_handler = event_handler; 416 winfo->event_handler_user_data = data; 417 } 418 ACPI_SERIAL_END(acpi_wmi); 419 420 return (status); 421} 422 423/* 424 * Remove a previously installed event handler from the given GUID 425 * If there was none installed, this call is silently discarded and 426 * reported as AE_OK 427 */ 428static ACPI_STATUS 429acpi_wmi_remove_event_handler_method(device_t dev, const char *guid_string) 430{ 431 struct wmi_info *winfo; 432 ACPI_STATUS status; 433 434 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 435 436 status = AE_OK; 437 ACPI_SERIAL_BEGIN(acpi_wmi); 438 if (guid_string && 439 (winfo = acpi_wmi_lookup_wmi_info_by_guid_string(guid_string)) 440 != NULL && winfo->event_handler) { 441 status = acpi_wmi_toggle_we_event_generation(dev, winfo, 442 EVENT_GENERATION_OFF); 443 winfo->event_handler = NULL; 444 winfo->event_handler_user_data = NULL; 445 } 446 ACPI_SERIAL_END(acpi_wmi); 447 448 return (status); 449} 450 451/* 452 * Get details on an event received through a callback registered 453 * through ACPI_WMI_REMOVE_EVENT_HANDLER into a user provided output buffer. 454 * (event_id equals "notify" passed in the callback) 455 */ 456static ACPI_STATUS 457acpi_wmi_get_event_data_method(device_t dev, UINT32 event_id, ACPI_BUFFER *out) 458{ 459 ACPI_OBJECT_LIST input; 460 ACPI_OBJECT params[1]; 461 struct acpi_wmi_softc *sc; 462 struct wmi_info *winfo; 463 ACPI_STATUS status; 464 465 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 466 467 sc = device_get_softc(dev); 468 status = AE_NOT_FOUND; 469 ACPI_SERIAL_BEGIN(acpi_wmi); 470 params[0].Type = ACPI_TYPE_INTEGER; 471 params[0].Integer.Value = event_id; 472 input.Pointer = params; 473 input.Count = 1; 474 TAILQ_FOREACH(winfo, &wmi_info_list, wmi_list) { 475 if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) && 476 ((UINT8) winfo->ginfo.oid[0] == event_id)) { 477 status = AcpiEvaluateObject(sc->wmi_handle, "_WED", 478 &input, out); 479 break; 480 } 481 } 482 ACPI_SERIAL_END(acpi_wmi); 483 484 return (status); 485} 486 487/* 488 * Read a block of data from the given GUID (using WQxx (query)) 489 * Will be returned in a user provided buffer (out). 490 * If the method is marked as expensive (ACPI_WMI_REGFLAG_EXPENSIVE) 491 * we will first call the WCxx control method to lock the node to 492 * lock the node for data collection and release it afterwards. 493 * (Failed WCxx calls are ignored to "support" broken implementations) 494 */ 495static ACPI_STATUS 496acpi_wmi_get_block_method(device_t dev, const char *guid_string, UINT8 instance, 497 ACPI_BUFFER *out) 498{ 499 char wc_method[5] = "WCxx"; 500 char wq_method[5] = "WQxx"; 501 ACPI_OBJECT_LIST wc_input; 502 ACPI_OBJECT_LIST wq_input; 503 ACPI_OBJECT wc_params[1]; 504 ACPI_OBJECT wq_params[1]; 505 ACPI_HANDLE wc_handle; 506 struct acpi_wmi_softc *sc; 507 struct wmi_info *winfo; 508 ACPI_STATUS status; 509 ACPI_STATUS wc_status; 510 511 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 512 513 sc = device_get_softc(dev); 514 wc_status = AE_ERROR; 515 ACPI_SERIAL_BEGIN(acpi_wmi); 516 if (guid_string == NULL || out == NULL) 517 status = AE_BAD_PARAMETER; 518 else if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(guid_string)) 519 == NULL) 520 status = AE_ERROR; 521 else if (instance > winfo->ginfo.max_instance) 522 status = AE_BAD_PARAMETER; 523 else if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) || 524 (winfo->ginfo.flags & ACPI_WMI_REGFLAG_METHOD)) 525 status = AE_ERROR; 526 else { 527 wq_params[0].Type = ACPI_TYPE_INTEGER; 528 wq_params[0].Integer.Value = instance; 529 wq_input.Pointer = wq_params; 530 wq_input.Count = 1; 531 if (winfo->ginfo.flags & ACPI_WMI_REGFLAG_EXPENSIVE) { 532 wc_params[0].Type = ACPI_TYPE_INTEGER; 533 wc_params[0].Integer.Value = 1; 534 wc_input.Pointer = wc_params; 535 wc_input.Count = 1; 536 wc_method[2] = winfo->ginfo.oid[0]; 537 wc_method[3] = winfo->ginfo.oid[1]; 538 wc_status = AcpiGetHandle(sc->wmi_handle, wc_method, 539 &wc_handle); 540 if (ACPI_SUCCESS(wc_status)) 541 wc_status = AcpiEvaluateObject(wc_handle, 542 wc_method, &wc_input, NULL); 543 } 544 wq_method[2] = winfo->ginfo.oid[0]; 545 wq_method[3] = winfo->ginfo.oid[1]; 546 status = AcpiEvaluateObject(sc->wmi_handle, wq_method, 547 &wq_input, out); 548 if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EXPENSIVE) 549 && ACPI_SUCCESS(wc_status)) { 550 wc_params[0].Integer.Value = 0; 551 status = AcpiEvaluateObject(wc_handle, wc_method, 552 &wc_input, NULL); /* XXX this might be 553 the wrong status to 554 return? */ 555 } 556 } 557 ACPI_SERIAL_END(acpi_wmi); 558 559 return (status); 560} 561 562/* 563 * Write a block of data to the given GUID (using WSxx) 564 */ 565static ACPI_STATUS 566acpi_wmi_set_block_method(device_t dev, const char *guid_string, UINT8 instance, 567 const ACPI_BUFFER *in) 568{ 569 char method[5] = "WSxx"; 570 ACPI_OBJECT_LIST input; 571 ACPI_OBJECT params[2]; 572 struct wmi_info *winfo; 573 struct acpi_wmi_softc *sc; 574 ACPI_STATUS status; 575 576 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 577 578 sc = device_get_softc(dev); 579 ACPI_SERIAL_BEGIN(acpi_wmi); 580 if (guid_string == NULL || in == NULL) 581 status = AE_BAD_DATA; 582 else if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(guid_string)) 583 == NULL) 584 status = AE_ERROR; 585 else if (instance > winfo->ginfo.max_instance) 586 status = AE_BAD_PARAMETER; 587 else if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) || 588 (winfo->ginfo.flags & ACPI_WMI_REGFLAG_METHOD)) 589 status = AE_ERROR; 590 else { 591 params[0].Type = ACPI_TYPE_INTEGER; 592 params[0].Integer.Value = instance; 593 input.Pointer = params; 594 input.Count = 2; 595 params[1].Type = (winfo->ginfo.flags & ACPI_WMI_REGFLAG_STRING) 596 ?ACPI_TYPE_STRING:ACPI_TYPE_BUFFER; 597 params[1].Buffer.Length = in->Length; 598 params[1].Buffer.Pointer = in->Pointer; 599 method[2] = winfo->ginfo.oid[0]; 600 method[3] = winfo->ginfo.oid[1]; 601 status = AcpiEvaluateObject(sc->wmi_handle, method, 602 &input, NULL); 603 } 604 ACPI_SERIAL_END(acpi_wmi); 605 606 return (status); 607} 608 609/* 610 * Handle events received and dispatch them to 611 * stakeholders that registered through ACPI_WMI_INSTALL_EVENT_HANDLER 612 */ 613static void 614acpi_wmi_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 615{ 616 ACPI_NOTIFY_HANDLER handler; 617 void *handler_data; 618 struct wmi_info *winfo; 619 620 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify); 621 622 handler = NULL; 623 handler_data = NULL; 624 ACPI_SERIAL_BEGIN(acpi_wmi); 625 TAILQ_FOREACH(winfo, &wmi_info_list, wmi_list) { 626 if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) && 627 ((UINT8) winfo->ginfo.oid[0] == notify)) { 628 if (winfo->event_handler) { 629 handler = winfo->event_handler; 630 handler_data = winfo->event_handler_user_data; 631 break; 632 } 633 } 634 } 635 ACPI_SERIAL_END(acpi_wmi); 636 if (handler) { 637 handler(h, notify, handler_data); 638 } 639} 640 641/* 642 * Handle EC address space notifications reveived on the WDG node 643 * (this mimics EcAddressSpaceHandler in acpi_ec.c) 644 */ 645static ACPI_STATUS 646acpi_wmi_ec_handler(UINT32 function, ACPI_PHYSICAL_ADDRESS address, 647 UINT32 width, ACPI_INTEGER *value, void *context, 648 void *region_context) 649{ 650 struct acpi_wmi_softc *sc; 651 int i; 652 ACPI_INTEGER ec_data; 653 UINT8 ec_addr; 654 ACPI_STATUS status; 655 656 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address); 657 658 sc = (struct acpi_wmi_softc *)context; 659 if (width % 8 != 0 || value == NULL || context == NULL) 660 return (AE_BAD_PARAMETER); 661 if (address + (width / 8) - 1 > 0xFF) 662 return (AE_BAD_ADDRESS); 663 if (function == ACPI_READ) 664 *value = 0; 665 ec_addr = address; 666 status = AE_ERROR; 667 668 for (i = 0; i < width; i += 8, ++ec_addr) { 669 switch (function) { 670 case ACPI_READ: 671 status = ACPI_EC_READ(sc->ec_dev, ec_addr, &ec_data, 1); 672 if (ACPI_SUCCESS(status)) 673 *value |= ((ACPI_INTEGER)ec_data) << i; 674 break; 675 case ACPI_WRITE: 676 ec_data = (UINT8)((*value) >> i); 677 status = ACPI_EC_WRITE(sc->ec_dev, ec_addr, ec_data, 1); 678 break; 679 default: 680 device_printf(sc->wmi_dev, 681 "invalid acpi_wmi_ec_handler function %d\n", 682 function); 683 status = AE_BAD_PARAMETER; 684 break; 685 } 686 if (ACPI_FAILURE(status)) 687 break; 688 } 689 690 return (status); 691} 692 693/* 694 * Read GUID blocks from the _WDG node 695 * into wmi_info_list. 696 */ 697static ACPI_STATUS 698acpi_wmi_read_wdg_blocks(ACPI_HANDLE h) 699{ 700 ACPI_BUFFER out = {ACPI_ALLOCATE_BUFFER, NULL}; 701 struct guid_info *ginfo; 702 ACPI_OBJECT *obj; 703 struct wmi_info *winfo; 704 UINT32 i; 705 UINT32 wdg_block_count; 706 ACPI_STATUS status; 707 708 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 709 710 ACPI_SERIAL_ASSERT(acpi_wmi); 711 if (ACPI_FAILURE(status = AcpiEvaluateObject(h, "_WDG", NULL, &out))) 712 return (status); 713 obj = (ACPI_OBJECT*) out.Pointer; 714 wdg_block_count = obj->Buffer.Length / sizeof(struct guid_info); 715 if ((ginfo = malloc(obj->Buffer.Length, M_ACPIWMI, M_NOWAIT)) 716 == NULL) { 717 AcpiOsFree(out.Pointer); 718 return (AE_NO_MEMORY); 719 } 720 memcpy(ginfo, obj->Buffer.Pointer, obj->Buffer.Length); 721 for (i = 0; i < wdg_block_count; ++i) { 722 if ((winfo = malloc(sizeof(struct wmi_info), M_ACPIWMI, 723 M_NOWAIT | M_ZERO)) == NULL) { 724 AcpiOsFree(out.Pointer); 725 free(ginfo, M_ACPIWMI); 726 return (AE_NO_MEMORY); 727 } 728 winfo->ginfo = ginfo[i]; 729 TAILQ_INSERT_TAIL(&wmi_info_list, winfo, wmi_list); 730 } 731 AcpiOsFree(out.Pointer); 732 free(ginfo, M_ACPIWMI); 733 734 return (status); 735} 736 737/* 738 * Toggle event generation in for the given GUID (passed by winfo) 739 * Turn on to get notified (through acpi_wmi_notify_handler) if events happen 740 * on the given GUID. 741 */ 742static ACPI_STATUS 743acpi_wmi_toggle_we_event_generation(device_t dev, struct wmi_info *winfo, 744 enum event_generation_state state) 745{ 746 char method[5] = "WExx"; 747 ACPI_OBJECT_LIST input; 748 ACPI_OBJECT params[1]; 749 struct acpi_wmi_softc *sc; 750 ACPI_STATUS status; 751 752 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 753 754 sc = device_get_softc(dev); 755 ACPI_SERIAL_ASSERT(acpi_wmi); 756 params[0].Type = ACPI_TYPE_INTEGER; 757 params[0].Integer.Value = state==EVENT_GENERATION_ON?1:0; 758 input.Pointer = params; 759 input.Count = 1; 760 761 UINT8 hi = ((UINT8) winfo->ginfo.oid[0]) >> 4; 762 UINT8 lo = ((UINT8) winfo->ginfo.oid[0]) & 0xf; 763 method[2] = (hi > 9 ? hi + 55: hi + 48); 764 method[3] = (lo > 9 ? lo + 55: lo + 48); 765 status = AcpiEvaluateObject(sc->wmi_handle, method, &input, NULL); 766 if (status == AE_NOT_FOUND) status = AE_OK; 767 768 return (status); 769} 770 771/* 772 * Convert given two digit hex string (hexin) to an UINT8 referenced 773 * by byteout. 774 * Return != 0 if the was a problem (invalid input) 775 */ 776static __inline int acpi_wmi_hex_to_int(const UINT8 *hexin, UINT8 *byteout) 777{ 778 unsigned int hi; 779 unsigned int lo; 780 781 hi = hexin[0]; 782 lo = hexin[1]; 783 if ('0' <= hi && hi <= '9') 784 hi -= '0'; 785 else if ('A' <= hi && hi <= 'F') 786 hi -= ('A' - 10); 787 else if ('a' <= hi && hi <= 'f') 788 hi -= ('a' - 10); 789 else 790 return (1); 791 if ('0' <= lo && lo <= '9') 792 lo -= '0'; 793 else if ('A' <= lo && lo <= 'F') 794 lo -= ('A' - 10); 795 else if ('a' <= lo && lo <= 'f') 796 lo -= ('a' - 10); 797 else 798 return (1); 799 *byteout = (hi << 4) + lo; 800 801 return (0); 802} 803 804/* 805 * Convert a human readable 36 character GUID into a 16byte 806 * machine readable one. 807 * The basic algorithm looks as follows: 808 * Input: AABBCCDD-EEFF-GGHH-IIJJ-KKLLMMNNOOPP 809 * Output: DCBAFEHGIJKLMNOP 810 * (AA BB CC etc. represent two digit hex numbers == bytes) 811 * Return != 0 if passed guid string is invalid 812 */ 813static int 814acpi_wmi_guid_string_to_guid(const UINT8 *guid_string, UINT8 *guid) 815{ 816 static const int mapping[20] = {3, 2, 1, 0, -1, 5, 4, -1, 7, 6, -1, 817 8, 9, -1, 10, 11, 12, 13, 14, 15}; 818 int i; 819 820 for (i = 0; i < 20; ++i, ++guid_string) { 821 if (mapping[i] >= 0) { 822 if (acpi_wmi_hex_to_int(guid_string, 823 &guid[mapping[i]])) 824 return (-1); 825 ++guid_string; 826 } else if (*guid_string != '-') 827 return (-1); 828 } 829 830 return (0); 831} 832 833/* 834 * Lookup a wmi_info structure in wmi_list based on a 835 * human readable GUID 836 * Return NULL if the GUID is unknown in the _WDG 837 */ 838static struct wmi_info* 839acpi_wmi_lookup_wmi_info_by_guid_string(const char *guid_string) 840{ 841 char guid[16]; 842 struct wmi_info *winfo; 843 844 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 845 846 ACPI_SERIAL_ASSERT(acpi_wmi); 847 848 if (!acpi_wmi_guid_string_to_guid(guid_string, guid)) { 849 TAILQ_FOREACH(winfo, &wmi_info_list, wmi_list) { 850 if (!memcmp(winfo->ginfo.guid, guid, 16)) { 851 return (winfo); 852 } 853 } 854 } 855 856 return (NULL); 857} 858 859/* 860 * open wmistat device 861 */ 862static int 863acpi_wmi_wmistat_open(struct cdev* dev, int flags, int mode, struct thread *td) 864{ 865 struct acpi_wmi_softc *sc; 866 int ret; 867 868 if (dev == NULL || dev->si_drv1 == NULL) 869 return (EBADF); 870 sc = dev->si_drv1; 871 872 ACPI_SERIAL_BEGIN(acpi_wmi); 873 if (sc->wmistat_open_pid != 0) { 874 ret = EBUSY; 875 } 876 else { 877 if (sbuf_new(&sc->wmistat_sbuf, NULL, 4096, SBUF_AUTOEXTEND) 878 == NULL) { 879 ret = ENXIO; 880 } else { 881 sc->wmistat_open_pid = td->td_proc->p_pid; 882 sc->wmistat_bufptr = 0; 883 ret = 0; 884 } 885 } 886 ACPI_SERIAL_END(acpi_wmi); 887 888 return (ret); 889} 890 891/* 892 * close wmistat device 893 */ 894static int 895acpi_wmi_wmistat_close(struct cdev* dev, int flags, int mode, 896 struct thread *td) 897{ 898 struct acpi_wmi_softc *sc; 899 int ret; 900 901 if (dev == NULL || dev->si_drv1 == NULL) 902 return (EBADF); 903 sc = dev->si_drv1; 904 905 ACPI_SERIAL_BEGIN(acpi_wmi); 906 if (sc->wmistat_open_pid == 0) { 907 ret = EBADF; 908 } 909 else { 910 if (sc->wmistat_bufptr != -1) { 911 sbuf_delete(&sc->wmistat_sbuf); 912 sc->wmistat_bufptr = -1; 913 } 914 sc->wmistat_open_pid = 0; 915 ret = 0; 916 } 917 ACPI_SERIAL_END(acpi_wmi); 918 919 return (ret); 920} 921 922/* 923 * Read from wmistat guid information 924 */ 925static int 926acpi_wmi_wmistat_read(struct cdev *dev, struct uio *buf, int flag) 927{ 928 struct acpi_wmi_softc *sc; 929 struct wmi_info *winfo; 930 int l; 931 int ret; 932 UINT8* guid; 933 934 if (dev == NULL || dev->si_drv1 == NULL) 935 return (EBADF); 936 sc = dev->si_drv1; 937 938 ACPI_SERIAL_BEGIN(acpi_wmi); 939 if (sc->wmistat_open_pid != buf->uio_td->td_proc->p_pid || 940 sc->wmistat_bufptr == -1) { 941 ret = EBADF; 942 } 943 else { 944 if (!sbuf_done(&sc->wmistat_sbuf)) { 945 sbuf_printf(&sc->wmistat_sbuf, "GUID " 946 " INST EXPE METH STR " 947 "EVENT OID\n"); 948 TAILQ_FOREACH(winfo, &wmi_info_list, wmi_list) { 949 guid = (UINT8*)winfo->ginfo.guid; 950 sbuf_printf(&sc->wmistat_sbuf, 951 "{%02X%02X%02X%02X-%02X%02X-" 952 "%02X%02X-%02X%02X-%02X%02X" 953 "%02X%02X%02X%02X} %3d %-5s", 954 guid[3], guid[2], guid[1], guid[0], 955 guid[5], guid[4], 956 guid[7], guid[6], 957 guid[8], guid[9], 958 guid[10], guid[11], guid[12], 959 guid[13], guid[14], guid[15], 960 winfo->ginfo.max_instance, 961 (winfo->ginfo.flags& 962 ACPI_WMI_REGFLAG_EXPENSIVE)? 963 "YES":"NO" 964 ); 965 if (winfo->ginfo.flags&ACPI_WMI_REGFLAG_METHOD) 966 sbuf_printf(&sc->wmistat_sbuf, 967 "WM%c%c ", 968 winfo->ginfo.oid[0], 969 winfo->ginfo.oid[1]); 970 else 971 sbuf_printf(&sc->wmistat_sbuf, "NO "); 972 sbuf_printf(&sc->wmistat_sbuf, "%-4s", 973 (winfo->ginfo.flags& 974 ACPI_WMI_REGFLAG_STRING)?"YES":"NO" 975 ); 976 if (winfo->ginfo.flags&ACPI_WMI_REGFLAG_EVENT) 977 sbuf_printf(&sc->wmistat_sbuf, 978 "0x%02X%s -\n", 979 (UINT8)winfo->ginfo.oid[0], 980 winfo->event_handler==NULL? 981 " ":"+"); 982 else 983 sbuf_printf(&sc->wmistat_sbuf, 984 "NO %c%c\n", 985 winfo->ginfo.oid[0], 986 winfo->ginfo.oid[1]); 987 } 988 sbuf_finish(&sc->wmistat_sbuf); 989 } 990 if (sbuf_len(&sc->wmistat_sbuf) <= 0) { 991 sbuf_delete(&sc->wmistat_sbuf); 992 sc->wmistat_bufptr = -1; 993 sc->wmistat_open_pid = 0; 994 ret = ENOMEM; 995 } else { 996 l = min(buf->uio_resid, sbuf_len(&sc->wmistat_sbuf) - 997 sc->wmistat_bufptr); 998 ret = (l > 0)?uiomove(sbuf_data(&sc->wmistat_sbuf) + 999 sc->wmistat_bufptr, l, buf) : 0; 1000 sc->wmistat_bufptr += l; 1001 } 1002 } 1003 ACPI_SERIAL_END(acpi_wmi); 1004 1005 return (ret); 1006} 1007