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