2 * Copyright (c) 2009 Michael Gmelin <freebsd@grem.de>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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.
36 * WMI and ACPI: http://www.microsoft.com/whdc/system/pnppwr/wmi/wmi-acpi.mspx
37 * acpi-wmi for Linux: http://www.kernel.org
41 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
48 #include <sys/module.h>
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"
56 MALLOC_DEFINE(M_ACPIWMI, "acpiwmi", "ACPI-WMI mapping");
58 #define _COMPONENT ACPI_OEM
59 ACPI_MODULE_NAME("ACPI_WMI");
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 */
67 * acpi_wmi driver private structure
69 struct 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 */
80 * Struct that holds information about
81 * about a single GUID entry in _WDG
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 */
90 /* WExx event generation state (on/off) */
91 enum event_generation_state {
92 EVENT_GENERATION_ON = 1,
93 EVENT_GENERATION_OFF = 0
98 * Information about one entry in _WDG.
99 * List of those is used to lookup information by GUID.
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 */
108 TAILQ_HEAD(wmi_info_list_head, wmi_info)
109 wmi_info_list = TAILQ_HEAD_INITIALIZER(wmi_info_list);
111 ACPI_SERIAL_DECL(acpi_wmi, "ACPI-WMI Mapping");
113 /* public interface - declaration */
114 /* standard device interface*/
115 static int acpi_wmi_probe(device_t dev);
116 static int acpi_wmi_attach(device_t dev);
117 static int acpi_wmi_detach(device_t dev);
118 /* see acpi_wmi_if.m */
119 static int acpi_wmi_provides_guid_string_method(device_t dev,
120 const char *guid_string);
121 static 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,
125 static ACPI_STATUS acpi_wmi_install_event_handler_method(device_t dev,
126 const char *guid_string, ACPI_NOTIFY_HANDLER handler,
128 static ACPI_STATUS acpi_wmi_remove_event_handler_method(device_t dev,
129 const char *guid_string);
130 static ACPI_STATUS acpi_wmi_get_event_data_method(device_t dev,
131 UINT32 event_id, ACPI_BUFFER *out);
132 static ACPI_STATUS acpi_wmi_get_block_method(device_t dev,
133 const char *guid_string,
134 UINT8 instance, ACPI_BUFFER *out);
135 static 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 */
140 static void acpi_wmi_notify_handler(ACPI_HANDLE h, UINT32 notify,
142 static ACPI_STATUS acpi_wmi_ec_handler(UINT32 function,
143 ACPI_PHYSICAL_ADDRESS address, UINT32 width,
144 UINT64 *value, void *context,
145 void *region_context);
147 static ACPI_STATUS acpi_wmi_read_wdg_blocks(ACPI_HANDLE h);
148 static ACPI_STATUS acpi_wmi_toggle_we_event_generation(device_t dev,
149 struct wmi_info *winfo,
150 enum event_generation_state state);
151 static int acpi_wmi_guid_string_to_guid(const UINT8 *guid_string,
153 static struct wmi_info* acpi_wmi_lookup_wmi_info_by_guid_string(
154 const char *guid_string);
156 static d_open_t acpi_wmi_wmistat_open;
157 static d_close_t acpi_wmi_wmistat_close;
158 static d_read_t acpi_wmi_wmistat_read;
160 /* handler /dev/wmistat device */
161 static 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,
170 static 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),
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),
191 static driver_t acpi_wmi_driver = {
194 sizeof(struct acpi_wmi_softc),
197 static devclass_t acpi_wmi_devclass;
198 DRIVER_MODULE(acpi_wmi, acpi, acpi_wmi_driver, acpi_wmi_devclass, 0, 0);
199 MODULE_VERSION(acpi_wmi, 1);
200 MODULE_DEPEND(acpi_wmi, acpi, 1, 1, 1);
201 static char *wmi_ids[] = {"PNP0C14", "PNP0c14", NULL};
204 * Probe for the PNP0C14 ACPI node
207 acpi_wmi_probe(device_t dev)
209 if (acpi_disabled("wmi") ||
210 ACPI_ID_PROBE(device_get_parent(dev), dev, wmi_ids) == NULL)
212 device_set_desc(dev, "ACPI-WMI mapping");
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
225 acpi_wmi_attach(device_t dev)
227 struct acpi_wmi_softc *sc;
231 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
232 sc = device_get_softc(dev);
235 ACPI_SERIAL_BEGIN(acpi_wmi);
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))
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,
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(
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);
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;
270 ACPI_SERIAL_END(acpi_wmi);
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
283 acpi_wmi_detach(device_t dev)
285 struct wmi_info *winfo, *tmp;
286 struct acpi_wmi_softc *sc;
289 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
290 sc = device_get_softc(dev);
291 ACPI_SERIAL_BEGIN(acpi_wmi);
293 if (sc->wmistat_open_pid != 0) {
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);
307 if (sc->wmistat_bufptr != -1) {
308 sbuf_delete(&sc->wmistat_sbuf);
309 sc->wmistat_bufptr = -1;
311 sc->wmistat_open_pid = 0;
312 destroy_dev(sc->wmistat_dev_t);
315 ACPI_SERIAL_END(acpi_wmi);
322 * Check if the given GUID string (human readable format
323 * AABBCCDD-EEFF-GGHH-IIJJ-KKLLMMNNOOPP)
327 acpi_wmi_provides_guid_string_method(device_t dev, const char *guid_string)
329 struct wmi_info *winfo;
332 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
333 ACPI_SERIAL_BEGIN(acpi_wmi);
334 winfo = acpi_wmi_lookup_wmi_info_by_guid_string(guid_string);
335 ret = (winfo == NULL)?0:winfo->ginfo.max_instance+1;
336 ACPI_SERIAL_END(acpi_wmi);
342 * Call a method "method_id" on the given GUID block
343 * write result into user provided output buffer
346 acpi_wmi_evaluate_call_method(device_t dev, const char *guid_string,
347 UINT8 instance, UINT32 method_id, const ACPI_BUFFER *in, ACPI_BUFFER *out)
349 ACPI_OBJECT params[3];
350 ACPI_OBJECT_LIST input;
351 char method[5] = "WMxx";
352 struct wmi_info *winfo;
353 struct acpi_wmi_softc *sc;
356 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
358 sc = device_get_softc(dev);
359 ACPI_SERIAL_BEGIN(acpi_wmi);
360 if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(guid_string))
362 status = AE_NOT_FOUND;
363 else if (!(winfo->ginfo.flags & ACPI_WMI_REGFLAG_METHOD))
364 status = AE_BAD_DATA;
365 else if (instance > winfo->ginfo.max_instance)
366 status = AE_BAD_PARAMETER;
368 params[0].Type = ACPI_TYPE_INTEGER;
369 params[0].Integer.Value = instance;
370 params[1].Type = ACPI_TYPE_INTEGER;
371 params[1].Integer.Value = method_id;
372 input.Pointer = params;
376 (winfo->ginfo.flags & ACPI_WMI_REGFLAG_STRING)
377 ?ACPI_TYPE_STRING:ACPI_TYPE_BUFFER;
378 params[2].Buffer.Length = in->Length;
379 params[2].Buffer.Pointer = in->Pointer;
382 method[2] = winfo->ginfo.oid[0];
383 method[3] = winfo->ginfo.oid[1];
384 status = AcpiEvaluateObject(sc->wmi_handle, method,
387 ACPI_SERIAL_END(acpi_wmi);
393 * Install a user provided event_handler on the given GUID
394 * provided *data will be passed on callback
395 * If there is already an existing event handler registered it will be silently
399 acpi_wmi_install_event_handler_method(device_t dev, const char *guid_string,
400 ACPI_NOTIFY_HANDLER event_handler, void *data)
402 struct wmi_info *winfo;
405 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
408 ACPI_SERIAL_BEGIN(acpi_wmi);
409 if (guid_string == NULL || event_handler == NULL)
410 status = AE_BAD_PARAMETER;
411 else if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(guid_string))
413 status = AE_NOT_EXIST;
414 else if (winfo->event_handler != NULL ||
415 (status = acpi_wmi_toggle_we_event_generation(dev, winfo,
416 EVENT_GENERATION_ON)) == AE_OK) {
417 winfo->event_handler = event_handler;
418 winfo->event_handler_user_data = data;
420 ACPI_SERIAL_END(acpi_wmi);
426 * Remove a previously installed event handler from the given GUID
427 * If there was none installed, this call is silently discarded and
431 acpi_wmi_remove_event_handler_method(device_t dev, const char *guid_string)
433 struct wmi_info *winfo;
436 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
439 ACPI_SERIAL_BEGIN(acpi_wmi);
441 (winfo = acpi_wmi_lookup_wmi_info_by_guid_string(guid_string))
442 != NULL && winfo->event_handler) {
443 status = acpi_wmi_toggle_we_event_generation(dev, winfo,
444 EVENT_GENERATION_OFF);
445 winfo->event_handler = NULL;
446 winfo->event_handler_user_data = NULL;
448 ACPI_SERIAL_END(acpi_wmi);
454 * Get details on an event received through a callback registered
455 * through ACPI_WMI_REMOVE_EVENT_HANDLER into a user provided output buffer.
456 * (event_id equals "notify" passed in the callback)
459 acpi_wmi_get_event_data_method(device_t dev, UINT32 event_id, ACPI_BUFFER *out)
461 ACPI_OBJECT_LIST input;
462 ACPI_OBJECT params[1];
463 struct acpi_wmi_softc *sc;
464 struct wmi_info *winfo;
467 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
469 sc = device_get_softc(dev);
470 status = AE_NOT_FOUND;
471 ACPI_SERIAL_BEGIN(acpi_wmi);
472 params[0].Type = ACPI_TYPE_INTEGER;
473 params[0].Integer.Value = event_id;
474 input.Pointer = params;
476 TAILQ_FOREACH(winfo, &wmi_info_list, wmi_list) {
477 if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) &&
478 ((UINT8) winfo->ginfo.oid[0] == event_id)) {
479 status = AcpiEvaluateObject(sc->wmi_handle, "_WED",
484 ACPI_SERIAL_END(acpi_wmi);
490 * Read a block of data from the given GUID (using WQxx (query))
491 * Will be returned in a user provided buffer (out).
492 * If the method is marked as expensive (ACPI_WMI_REGFLAG_EXPENSIVE)
493 * we will first call the WCxx control method to lock the node to
494 * lock the node for data collection and release it afterwards.
495 * (Failed WCxx calls are ignored to "support" broken implementations)
498 acpi_wmi_get_block_method(device_t dev, const char *guid_string, UINT8 instance,
501 char wc_method[5] = "WCxx";
502 char wq_method[5] = "WQxx";
503 ACPI_OBJECT_LIST wc_input;
504 ACPI_OBJECT_LIST wq_input;
505 ACPI_OBJECT wc_params[1];
506 ACPI_OBJECT wq_params[1];
507 ACPI_HANDLE wc_handle;
508 struct acpi_wmi_softc *sc;
509 struct wmi_info *winfo;
511 ACPI_STATUS wc_status;
513 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
515 sc = device_get_softc(dev);
516 wc_status = AE_ERROR;
517 ACPI_SERIAL_BEGIN(acpi_wmi);
518 if (guid_string == NULL || out == NULL)
519 status = AE_BAD_PARAMETER;
520 else if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(guid_string))
523 else if (instance > winfo->ginfo.max_instance)
524 status = AE_BAD_PARAMETER;
525 else if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) ||
526 (winfo->ginfo.flags & ACPI_WMI_REGFLAG_METHOD))
529 wq_params[0].Type = ACPI_TYPE_INTEGER;
530 wq_params[0].Integer.Value = instance;
531 wq_input.Pointer = wq_params;
533 if (winfo->ginfo.flags & ACPI_WMI_REGFLAG_EXPENSIVE) {
534 wc_params[0].Type = ACPI_TYPE_INTEGER;
535 wc_params[0].Integer.Value = 1;
536 wc_input.Pointer = wc_params;
538 wc_method[2] = winfo->ginfo.oid[0];
539 wc_method[3] = winfo->ginfo.oid[1];
540 wc_status = AcpiGetHandle(sc->wmi_handle, wc_method,
542 if (ACPI_SUCCESS(wc_status))
543 wc_status = AcpiEvaluateObject(wc_handle,
544 wc_method, &wc_input, NULL);
546 wq_method[2] = winfo->ginfo.oid[0];
547 wq_method[3] = winfo->ginfo.oid[1];
548 status = AcpiEvaluateObject(sc->wmi_handle, wq_method,
550 if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EXPENSIVE)
551 && ACPI_SUCCESS(wc_status)) {
552 wc_params[0].Integer.Value = 0;
553 status = AcpiEvaluateObject(wc_handle, wc_method,
554 &wc_input, NULL); /* XXX this might be
559 ACPI_SERIAL_END(acpi_wmi);
565 * Write a block of data to the given GUID (using WSxx)
568 acpi_wmi_set_block_method(device_t dev, const char *guid_string, UINT8 instance,
569 const ACPI_BUFFER *in)
571 char method[5] = "WSxx";
572 ACPI_OBJECT_LIST input;
573 ACPI_OBJECT params[2];
574 struct wmi_info *winfo;
575 struct acpi_wmi_softc *sc;
578 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
580 sc = device_get_softc(dev);
581 ACPI_SERIAL_BEGIN(acpi_wmi);
582 if (guid_string == NULL || in == NULL)
583 status = AE_BAD_DATA;
584 else if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(guid_string))
587 else if (instance > winfo->ginfo.max_instance)
588 status = AE_BAD_PARAMETER;
589 else if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) ||
590 (winfo->ginfo.flags & ACPI_WMI_REGFLAG_METHOD))
593 params[0].Type = ACPI_TYPE_INTEGER;
594 params[0].Integer.Value = instance;
595 input.Pointer = params;
597 params[1].Type = (winfo->ginfo.flags & ACPI_WMI_REGFLAG_STRING)
598 ?ACPI_TYPE_STRING:ACPI_TYPE_BUFFER;
599 params[1].Buffer.Length = in->Length;
600 params[1].Buffer.Pointer = in->Pointer;
601 method[2] = winfo->ginfo.oid[0];
602 method[3] = winfo->ginfo.oid[1];
603 status = AcpiEvaluateObject(sc->wmi_handle, method,
606 ACPI_SERIAL_END(acpi_wmi);
612 * Handle events received and dispatch them to
613 * stakeholders that registered through ACPI_WMI_INSTALL_EVENT_HANDLER
616 acpi_wmi_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
618 ACPI_NOTIFY_HANDLER handler;
620 struct wmi_info *winfo;
622 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
626 ACPI_SERIAL_BEGIN(acpi_wmi);
627 TAILQ_FOREACH(winfo, &wmi_info_list, wmi_list) {
628 if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) &&
629 ((UINT8) winfo->ginfo.oid[0] == notify)) {
630 if (winfo->event_handler) {
631 handler = winfo->event_handler;
632 handler_data = winfo->event_handler_user_data;
637 ACPI_SERIAL_END(acpi_wmi);
639 handler(h, notify, handler_data);
644 * Handle EC address space notifications reveived on the WDG node
645 * (this mimics EcAddressSpaceHandler in acpi_ec.c)
648 acpi_wmi_ec_handler(UINT32 function, ACPI_PHYSICAL_ADDRESS address,
649 UINT32 width, UINT64 *value, void *context,
650 void *region_context)
652 struct acpi_wmi_softc *sc;
658 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)address);
660 sc = (struct acpi_wmi_softc *)context;
661 if (width % 8 != 0 || value == NULL || context == NULL)
662 return (AE_BAD_PARAMETER);
663 if (address + (width / 8) - 1 > 0xFF)
664 return (AE_BAD_ADDRESS);
665 if (function == ACPI_READ)
670 for (i = 0; i < width; i += 8, ++ec_addr) {
673 status = ACPI_EC_READ(sc->ec_dev, ec_addr, &ec_data, 1);
674 if (ACPI_SUCCESS(status))
675 *value |= ((UINT64)ec_data) << i;
678 ec_data = (UINT8)((*value) >> i);
679 status = ACPI_EC_WRITE(sc->ec_dev, ec_addr, ec_data, 1);
682 device_printf(sc->wmi_dev,
683 "invalid acpi_wmi_ec_handler function %d\n",
685 status = AE_BAD_PARAMETER;
688 if (ACPI_FAILURE(status))
696 * Read GUID blocks from the _WDG node
697 * into wmi_info_list.
700 acpi_wmi_read_wdg_blocks(ACPI_HANDLE h)
702 ACPI_BUFFER out = {ACPI_ALLOCATE_BUFFER, NULL};
703 struct guid_info *ginfo;
705 struct wmi_info *winfo;
707 UINT32 wdg_block_count;
710 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
712 ACPI_SERIAL_ASSERT(acpi_wmi);
713 if (ACPI_FAILURE(status = AcpiEvaluateObject(h, "_WDG", NULL, &out)))
715 obj = (ACPI_OBJECT*) out.Pointer;
716 wdg_block_count = obj->Buffer.Length / sizeof(struct guid_info);
717 if ((ginfo = malloc(obj->Buffer.Length, M_ACPIWMI, M_NOWAIT))
719 AcpiOsFree(out.Pointer);
720 return (AE_NO_MEMORY);
722 memcpy(ginfo, obj->Buffer.Pointer, obj->Buffer.Length);
723 for (i = 0; i < wdg_block_count; ++i) {
724 if ((winfo = malloc(sizeof(struct wmi_info), M_ACPIWMI,
725 M_NOWAIT | M_ZERO)) == NULL) {
726 AcpiOsFree(out.Pointer);
727 free(ginfo, M_ACPIWMI);
728 return (AE_NO_MEMORY);
730 winfo->ginfo = ginfo[i];
731 TAILQ_INSERT_TAIL(&wmi_info_list, winfo, wmi_list);
733 AcpiOsFree(out.Pointer);
734 free(ginfo, M_ACPIWMI);
740 * Toggle event generation in for the given GUID (passed by winfo)
741 * Turn on to get notified (through acpi_wmi_notify_handler) if events happen
745 acpi_wmi_toggle_we_event_generation(device_t dev, struct wmi_info *winfo,
746 enum event_generation_state state)
748 char method[5] = "WExx";
749 ACPI_OBJECT_LIST input;
750 ACPI_OBJECT params[1];
751 struct acpi_wmi_softc *sc;
754 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
756 sc = device_get_softc(dev);
757 ACPI_SERIAL_ASSERT(acpi_wmi);
758 params[0].Type = ACPI_TYPE_INTEGER;
759 params[0].Integer.Value = state==EVENT_GENERATION_ON?1:0;
760 input.Pointer = params;
763 UINT8 hi = ((UINT8) winfo->ginfo.oid[0]) >> 4;
764 UINT8 lo = ((UINT8) winfo->ginfo.oid[0]) & 0xf;
765 method[2] = (hi > 9 ? hi + 55: hi + 48);
766 method[3] = (lo > 9 ? lo + 55: lo + 48);
767 status = AcpiEvaluateObject(sc->wmi_handle, method, &input, NULL);
768 if (status == AE_NOT_FOUND) status = AE_OK;
774 * Convert given two digit hex string (hexin) to an UINT8 referenced
776 * Return != 0 if the was a problem (invalid input)
778 static __inline int acpi_wmi_hex_to_int(const UINT8 *hexin, UINT8 *byteout)
785 if ('0' <= hi && hi <= '9')
787 else if ('A' <= hi && hi <= 'F')
789 else if ('a' <= hi && hi <= 'f')
793 if ('0' <= lo && lo <= '9')
795 else if ('A' <= lo && lo <= 'F')
797 else if ('a' <= lo && lo <= 'f')
801 *byteout = (hi << 4) + lo;
807 * Convert a human readable 36 character GUID into a 16byte
808 * machine readable one.
809 * The basic algorithm looks as follows:
810 * Input: AABBCCDD-EEFF-GGHH-IIJJ-KKLLMMNNOOPP
811 * Output: DCBAFEHGIJKLMNOP
812 * (AA BB CC etc. represent two digit hex numbers == bytes)
813 * Return != 0 if passed guid string is invalid
816 acpi_wmi_guid_string_to_guid(const UINT8 *guid_string, UINT8 *guid)
818 static const int mapping[20] = {3, 2, 1, 0, -1, 5, 4, -1, 7, 6, -1,
819 8, 9, -1, 10, 11, 12, 13, 14, 15};
822 for (i = 0; i < 20; ++i, ++guid_string) {
823 if (mapping[i] >= 0) {
824 if (acpi_wmi_hex_to_int(guid_string,
828 } else if (*guid_string != '-')
836 * Lookup a wmi_info structure in wmi_list based on a
837 * human readable GUID
838 * Return NULL if the GUID is unknown in the _WDG
840 static struct wmi_info*
841 acpi_wmi_lookup_wmi_info_by_guid_string(const char *guid_string)
844 struct wmi_info *winfo;
846 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
848 ACPI_SERIAL_ASSERT(acpi_wmi);
850 if (!acpi_wmi_guid_string_to_guid(guid_string, guid)) {
851 TAILQ_FOREACH(winfo, &wmi_info_list, wmi_list) {
852 if (!memcmp(winfo->ginfo.guid, guid, 16)) {
862 * open wmistat device
865 acpi_wmi_wmistat_open(struct cdev* dev, int flags, int mode, struct thread *td)
867 struct acpi_wmi_softc *sc;
870 if (dev == NULL || dev->si_drv1 == NULL)
874 ACPI_SERIAL_BEGIN(acpi_wmi);
875 if (sc->wmistat_open_pid != 0) {
879 if (sbuf_new(&sc->wmistat_sbuf, NULL, 4096, SBUF_AUTOEXTEND)
883 sc->wmistat_open_pid = td->td_proc->p_pid;
884 sc->wmistat_bufptr = 0;
888 ACPI_SERIAL_END(acpi_wmi);
894 * close wmistat device
897 acpi_wmi_wmistat_close(struct cdev* dev, int flags, int mode,
900 struct acpi_wmi_softc *sc;
903 if (dev == NULL || dev->si_drv1 == NULL)
907 ACPI_SERIAL_BEGIN(acpi_wmi);
908 if (sc->wmistat_open_pid == 0) {
912 if (sc->wmistat_bufptr != -1) {
913 sbuf_delete(&sc->wmistat_sbuf);
914 sc->wmistat_bufptr = -1;
916 sc->wmistat_open_pid = 0;
919 ACPI_SERIAL_END(acpi_wmi);
925 * Read from wmistat guid information
928 acpi_wmi_wmistat_read(struct cdev *dev, struct uio *buf, int flag)
930 struct acpi_wmi_softc *sc;
931 struct wmi_info *winfo;
936 if (dev == NULL || dev->si_drv1 == NULL)
940 ACPI_SERIAL_BEGIN(acpi_wmi);
941 if (sc->wmistat_open_pid != buf->uio_td->td_proc->p_pid ||
942 sc->wmistat_bufptr == -1) {
946 if (!sbuf_done(&sc->wmistat_sbuf)) {
947 sbuf_printf(&sc->wmistat_sbuf, "GUID "
948 " INST EXPE METH STR "
950 TAILQ_FOREACH(winfo, &wmi_info_list, wmi_list) {
951 guid = (UINT8*)winfo->ginfo.guid;
952 sbuf_printf(&sc->wmistat_sbuf,
953 "{%02X%02X%02X%02X-%02X%02X-"
954 "%02X%02X-%02X%02X-%02X%02X"
955 "%02X%02X%02X%02X} %3d %-5s",
956 guid[3], guid[2], guid[1], guid[0],
960 guid[10], guid[11], guid[12],
961 guid[13], guid[14], guid[15],
962 winfo->ginfo.max_instance,
964 ACPI_WMI_REGFLAG_EXPENSIVE)?
967 if (winfo->ginfo.flags&ACPI_WMI_REGFLAG_METHOD)
968 sbuf_printf(&sc->wmistat_sbuf,
971 winfo->ginfo.oid[1]);
973 sbuf_printf(&sc->wmistat_sbuf, "NO ");
974 sbuf_printf(&sc->wmistat_sbuf, "%-4s",
976 ACPI_WMI_REGFLAG_STRING)?"YES":"NO"
978 if (winfo->ginfo.flags&ACPI_WMI_REGFLAG_EVENT)
979 sbuf_printf(&sc->wmistat_sbuf,
981 (UINT8)winfo->ginfo.oid[0],
982 winfo->event_handler==NULL?
985 sbuf_printf(&sc->wmistat_sbuf,
988 winfo->ginfo.oid[1]);
990 sbuf_finish(&sc->wmistat_sbuf);
992 if (sbuf_len(&sc->wmistat_sbuf) <= 0) {
993 sbuf_delete(&sc->wmistat_sbuf);
994 sc->wmistat_bufptr = -1;
995 sc->wmistat_open_pid = 0;
998 l = min(buf->uio_resid, sbuf_len(&sc->wmistat_sbuf) -
1000 ret = (l > 0)?uiomove(sbuf_data(&sc->wmistat_sbuf) +
1001 sc->wmistat_bufptr, l, buf) : 0;
1002 sc->wmistat_bufptr += l;
1005 ACPI_SERIAL_END(acpi_wmi);