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