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