xref: /freebsd/sys/dev/acpi_support/acpi_hp.c (revision 41059135ce931c0f1014a999ffabc6bc470ce856)
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 extra ACPI-controlled features found on HP laptops
32  * that use a WMI enabled BIOS (e.g. HP Compaq 8510p and 6510p).
33  * Allows to control and read status of integrated hardware and read
34  * BIOS settings through CMI.
35  * Inspired by the hp-wmi driver, which implements a subset of these
36  * features (hotkeys) on Linux.
37  *
38  * HP CMI whitepaper:
39  *     http://h20331.www2.hp.com/Hpsub/downloads/cmi_whitepaper.pdf
40  * wmi-hp for Linux:
41  *     http://www.kernel.org
42  * WMI and ACPI:
43  *     http://www.microsoft.com/whdc/system/pnppwr/wmi/wmi-acpi.mspx
44  */
45 
46 #include "opt_acpi.h"
47 #include <sys/param.h>
48 #include <sys/conf.h>
49 #include <sys/uio.h>
50 #include <sys/proc.h>
51 #include <sys/kernel.h>
52 #include <sys/bus.h>
53 #include <sys/sbuf.h>
54 #include <sys/module.h>
55 #include <sys/sysctl.h>
56 
57 #include <contrib/dev/acpica/include/acpi.h>
58 #include <contrib/dev/acpica/include/accommon.h>
59 #include <dev/acpica/acpivar.h>
60 #include "acpi_wmi_if.h"
61 
62 #define _COMPONENT	ACPI_OEM
63 ACPI_MODULE_NAME("HP")
64 
65 #define ACPI_HP_WMI_EVENT_GUID		"95F24279-4D7B-4334-9387-ACCDC67EF61C"
66 #define ACPI_HP_WMI_BIOS_GUID		"5FB7F034-2C63-45E9-BE91-3D44E2C707E4"
67 #define ACPI_HP_WMI_CMI_GUID		"2D114B49-2DFB-4130-B8FE-4A3C09E75133"
68 
69 #define ACPI_HP_WMI_DISPLAY_COMMAND		0x1
70 #define ACPI_HP_WMI_HDDTEMP_COMMAND		0x2
71 #define ACPI_HP_WMI_ALS_COMMAND			0x3
72 #define ACPI_HP_WMI_DOCK_COMMAND		0x4
73 #define ACPI_HP_WMI_WIRELESS_COMMAND		0x5
74 #define ACPI_HP_WMI_BIOS_COMMAND		0x9
75 #define ACPI_HP_WMI_FEATURE_COMMAND		0xb
76 #define ACPI_HP_WMI_HOTKEY_COMMAND		0xc
77 #define ACPI_HP_WMI_FEATURE2_COMMAND		0xd
78 #define ACPI_HP_WMI_WIRELESS2_COMMAND		0x1b
79 #define ACPI_HP_WMI_POSTCODEERROR_COMMAND	0x2a
80 
81 #define ACPI_HP_METHOD_WLAN_ENABLED			1
82 #define ACPI_HP_METHOD_WLAN_RADIO			2
83 #define ACPI_HP_METHOD_WLAN_ON_AIR			3
84 #define ACPI_HP_METHOD_WLAN_ENABLE_IF_RADIO_ON		4
85 #define ACPI_HP_METHOD_WLAN_DISABLE_IF_RADIO_OFF	5
86 #define ACPI_HP_METHOD_BLUETOOTH_ENABLED		6
87 #define ACPI_HP_METHOD_BLUETOOTH_RADIO			7
88 #define ACPI_HP_METHOD_BLUETOOTH_ON_AIR			8
89 #define ACPI_HP_METHOD_BLUETOOTH_ENABLE_IF_RADIO_ON	9
90 #define ACPI_HP_METHOD_BLUETOOTH_DISABLE_IF_RADIO_OFF	10
91 #define ACPI_HP_METHOD_WWAN_ENABLED			11
92 #define ACPI_HP_METHOD_WWAN_RADIO			12
93 #define ACPI_HP_METHOD_WWAN_ON_AIR			13
94 #define ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON		14
95 #define ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF	15
96 #define ACPI_HP_METHOD_ALS				16
97 #define ACPI_HP_METHOD_DISPLAY				17
98 #define ACPI_HP_METHOD_HDDTEMP				18
99 #define ACPI_HP_METHOD_DOCK				19
100 #define ACPI_HP_METHOD_CMI_DETAIL			20
101 #define ACPI_HP_METHOD_VERBOSE				21
102 
103 #define HP_MASK_WWAN_ON_AIR			0x1000000
104 #define HP_MASK_BLUETOOTH_ON_AIR		0x10000
105 #define HP_MASK_WLAN_ON_AIR			0x100
106 #define HP_MASK_WWAN_RADIO			0x8000000
107 #define HP_MASK_BLUETOOTH_RADIO			0x80000
108 #define HP_MASK_WLAN_RADIO			0x800
109 #define HP_MASK_WWAN_ENABLED			0x2000000
110 #define HP_MASK_BLUETOOTH_ENABLED		0x20000
111 #define HP_MASK_WLAN_ENABLED			0x200
112 
113 #define ACPI_HP_EVENT_DOCK			0x01
114 #define ACPI_HP_EVENT_PARK_HDD			0x02
115 #define ACPI_HP_EVENT_SMART_ADAPTER		0x03
116 #define ACPI_HP_EVENT_BEZEL_BUTTON		0x04
117 #define ACPI_HP_EVENT_WIRELESS			0x05
118 #define ACPI_HP_EVENT_CPU_BATTERY_THROTTLE	0x06
119 #define ACPI_HP_EVENT_LOCK_SWITCH		0x07
120 #define ACPI_HP_EVENT_LID_SWITCH		0x08
121 #define ACPI_HP_EVENT_SCREEN_ROTATION		0x09
122 #define ACPI_HP_EVENT_COOLSENSE_SYSTEM_MOBILE	0x0A
123 #define ACPI_HP_EVENT_COOLSENSE_SYSTEM_HOT	0x0B
124 #define ACPI_HP_EVENT_PROXIMITY_SENSOR		0x0C
125 #define ACPI_HP_EVENT_BACKLIT_KB_BRIGHTNESS	0x0D
126 #define ACPI_HP_EVENT_PEAKSHIFT_PERIOD		0x0F
127 #define ACPI_HP_EVENT_BATTERY_CHARGE_PERIOD	0x10
128 
129 #define ACPI_HP_CMI_DETAIL_PATHS		0x01
130 #define ACPI_HP_CMI_DETAIL_ENUMS		0x02
131 #define ACPI_HP_CMI_DETAIL_FLAGS		0x04
132 #define ACPI_HP_CMI_DETAIL_SHOW_MAX_INSTANCE	0x08
133 
134 #define ACPI_HP_WMI_RET_WRONG_SIGNATURE		0x02
135 #define ACPI_HP_WMI_RET_UNKNOWN_COMMAND		0x03
136 #define ACPI_HP_WMI_RET_UNKNOWN_CMDTYPE		0x04
137 #define ACPI_HP_WMI_RET_INVALID_PARAMETERS	0x05
138 
139 struct acpi_hp_inst_seq_pair {
140 	UINT32	sequence;	/* sequence number as suggested by cmi bios */
141 	UINT8	instance;	/* object instance on guid */
142 };
143 
144 struct acpi_hp_softc {
145 	device_t	dev;
146 	device_t	wmi_dev;
147 	int		has_notify;		/* notification GUID found */
148 	int		has_cmi;		/* CMI GUID found */
149 	int		has_wireless;		/* Wireless command found */
150 	int		cmi_detail;		/* CMI detail level
151 						   (set by sysctl) */
152 	int		verbose;		/* add debug output */
153 	int		wlan_enable_if_radio_on;	/* set by sysctl */
154 	int		wlan_disable_if_radio_off;	/* set by sysctl */
155 	int		bluetooth_enable_if_radio_on;	/* set by sysctl */
156 	int		bluetooth_disable_if_radio_off;	/* set by sysctl */
157 	int		wwan_enable_if_radio_on;	/* set by sysctl */
158 	int		wwan_disable_if_radio_off;	/* set by sysctl */
159 	int		was_wlan_on_air;		/* last known WLAN
160 							   on air status */
161 	int		was_bluetooth_on_air;		/* last known BT
162 							   on air status */
163 	int		was_wwan_on_air;		/* last known WWAN
164 							   on air status */
165 	struct sysctl_ctx_list	*sysctl_ctx;
166 	struct sysctl_oid	*sysctl_tree;
167 	struct cdev	*hpcmi_dev_t;		/* hpcmi device handle */
168 	struct sbuf	hpcmi_sbuf;		/* /dev/hpcmi output sbuf */
169 	pid_t		hpcmi_open_pid;		/* pid operating on
170 						   /dev/hpcmi */
171 	int		hpcmi_bufptr;		/* current pointer position
172 						   in /dev/hpcmi output buffer
173 						 */
174 	int		cmi_order_size;		/* size of cmi_order list */
175 	struct acpi_hp_inst_seq_pair cmi_order[128];	/* list of CMI
176 			     instances ordered by BIOS suggested sequence */
177 };
178 
179 static struct {
180 	char	*name;
181 	int	method;
182 	char	*description;
183 	int	flag_rdonly;
184 } acpi_hp_sysctls[] = {
185 	{
186 		.name		= "wlan_enabled",
187 		.method		= ACPI_HP_METHOD_WLAN_ENABLED,
188 		.description	= "Enable/Disable WLAN (WiFi)",
189 	},
190 	{
191 		.name		= "wlan_radio",
192 		.method		= ACPI_HP_METHOD_WLAN_RADIO,
193 		.description	= "WLAN radio status",
194 		.flag_rdonly	= 1
195 	},
196 	{
197 		.name		= "wlan_on_air",
198 		.method		= ACPI_HP_METHOD_WLAN_ON_AIR,
199 		.description	= "WLAN radio ready to use (enabled and radio)",
200 		.flag_rdonly	= 1
201 	},
202 	{
203 		.name		= "wlan_enable_if_radio_on",
204 		.method		= ACPI_HP_METHOD_WLAN_ENABLE_IF_RADIO_ON,
205 		.description	= "Enable WLAN if radio is turned on",
206 	},
207 	{
208 		.name		= "wlan_disable_if_radio_off",
209 		.method		= ACPI_HP_METHOD_WLAN_DISABLE_IF_RADIO_OFF,
210 		.description	= "Disable WLAN if radio is turned off",
211 	},
212 	{
213 		.name		= "bt_enabled",
214 		.method		= ACPI_HP_METHOD_BLUETOOTH_ENABLED,
215 		.description	= "Enable/Disable Bluetooth",
216 	},
217 	{
218 		.name		= "bt_radio",
219 		.method		= ACPI_HP_METHOD_BLUETOOTH_RADIO,
220 		.description	= "Bluetooth radio status",
221 		.flag_rdonly	= 1
222 	},
223 	{
224 		.name		= "bt_on_air",
225 		.method		= ACPI_HP_METHOD_BLUETOOTH_ON_AIR,
226 		.description	= "Bluetooth radio ready to use"
227 				    " (enabled and radio)",
228 		.flag_rdonly	= 1
229 	},
230 	{
231 		.name		= "bt_enable_if_radio_on",
232 		.method		= ACPI_HP_METHOD_BLUETOOTH_ENABLE_IF_RADIO_ON,
233 		.description	= "Enable bluetooth if radio is turned on",
234 	},
235 	{
236 		.name		= "bt_disable_if_radio_off",
237 		.method		= ACPI_HP_METHOD_BLUETOOTH_DISABLE_IF_RADIO_OFF,
238 		.description	= "Disable bluetooth if radio is turned off",
239 	},
240 	{
241 		.name		= "wwan_enabled",
242 		.method		= ACPI_HP_METHOD_WWAN_ENABLED,
243 		.description	= "Enable/Disable WWAN (UMTS)",
244 	},
245 	{
246 		.name		= "wwan_radio",
247 		.method		= ACPI_HP_METHOD_WWAN_RADIO,
248 		.description	= "WWAN radio status",
249 		.flag_rdonly	= 1
250 	},
251 	{
252 		.name		= "wwan_on_air",
253 		.method		= ACPI_HP_METHOD_WWAN_ON_AIR,
254 		.description	= "WWAN radio ready to use (enabled and radio)",
255 		.flag_rdonly	= 1
256 	},
257 	{
258 		.name		= "wwan_enable_if_radio_on",
259 		.method		= ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON,
260 		.description	= "Enable WWAN if radio is turned on",
261 	},
262 	{
263 		.name		= "wwan_disable_if_radio_off",
264 		.method		= ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF,
265 		.description	= "Disable WWAN if radio is turned off",
266 	},
267 	{
268 		.name		= "als_enabled",
269 		.method		= ACPI_HP_METHOD_ALS,
270 		.description	= "Enable/Disable ALS (Ambient light sensor)",
271 	},
272 	{
273 		.name		= "display",
274 		.method		= ACPI_HP_METHOD_DISPLAY,
275 		.description	= "Display status",
276 		.flag_rdonly	= 1
277 	},
278 	{
279 		.name		= "hdd_temperature",
280 		.method		= ACPI_HP_METHOD_HDDTEMP,
281 		.description	= "HDD temperature",
282 		.flag_rdonly	= 1
283 	},
284 	{
285 		.name		= "is_docked",
286 		.method		= ACPI_HP_METHOD_DOCK,
287 		.description	= "Docking station status",
288 		.flag_rdonly	= 1
289 	},
290 	{
291 		.name		= "cmi_detail",
292 		.method		= ACPI_HP_METHOD_CMI_DETAIL,
293 		.description	= "Details shown in CMI output "
294 				    "(cat /dev/hpcmi)",
295 	},
296 	{
297 		.name		= "verbose",
298 		.method		= ACPI_HP_METHOD_VERBOSE,
299 		.description	= "Verbosity level",
300 	},
301 
302 	{ NULL, 0, NULL, 0 }
303 };
304 
305 ACPI_SERIAL_DECL(hp, "HP ACPI-WMI Mapping");
306 
307 static void	acpi_hp_identify(driver_t *driver, device_t parent);
308 static int	acpi_hp_probe(device_t dev);
309 static int	acpi_hp_attach(device_t dev);
310 static int	acpi_hp_detach(device_t dev);
311 
312 static void	acpi_hp_evaluate_auto_on_off(struct acpi_hp_softc* sc);
313 static int	acpi_hp_sysctl(SYSCTL_HANDLER_ARGS);
314 static int	acpi_hp_sysctl_set(struct acpi_hp_softc *sc, int method,
315 		    int arg, int oldarg);
316 static int	acpi_hp_sysctl_get(struct acpi_hp_softc *sc, int method);
317 static int	acpi_hp_exec_wmi_command(device_t wmi_dev, int command,
318 		    int is_write, int val, int *retval);
319 static void	acpi_hp_notify(ACPI_HANDLE h, UINT32 notify, void *context);
320 static int	acpi_hp_get_cmi_block(device_t wmi_dev, const char* guid,
321 		    UINT8 instance, char* outbuf, size_t outsize,
322 		    UINT32* sequence, int detail);
323 static void	acpi_hp_hex_decode(char* buffer);
324 
325 static d_open_t	acpi_hp_hpcmi_open;
326 static d_close_t acpi_hp_hpcmi_close;
327 static d_read_t	acpi_hp_hpcmi_read;
328 
329 /* handler /dev/hpcmi device */
330 static struct cdevsw hpcmi_cdevsw = {
331 	.d_version = D_VERSION,
332 	.d_open = acpi_hp_hpcmi_open,
333 	.d_close = acpi_hp_hpcmi_close,
334 	.d_read = acpi_hp_hpcmi_read,
335 	.d_name = "hpcmi",
336 };
337 
338 static device_method_t acpi_hp_methods[] = {
339 	DEVMETHOD(device_identify, acpi_hp_identify),
340 	DEVMETHOD(device_probe, acpi_hp_probe),
341 	DEVMETHOD(device_attach, acpi_hp_attach),
342 	DEVMETHOD(device_detach, acpi_hp_detach),
343 
344 	DEVMETHOD_END
345 };
346 
347 static driver_t	acpi_hp_driver = {
348 	"acpi_hp",
349 	acpi_hp_methods,
350 	sizeof(struct acpi_hp_softc),
351 };
352 
353 static devclass_t acpi_hp_devclass;
354 
355 DRIVER_MODULE(acpi_hp, acpi_wmi, acpi_hp_driver, acpi_hp_devclass,
356 		0, 0);
357 MODULE_DEPEND(acpi_hp, acpi_wmi, 1, 1, 1);
358 MODULE_DEPEND(acpi_hp, acpi, 1, 1, 1);
359 
360 static void
361 acpi_hp_evaluate_auto_on_off(struct acpi_hp_softc *sc)
362 {
363 	int	res;
364 	int	wireless;
365 	int	new_wlan_status;
366 	int	new_bluetooth_status;
367 	int	new_wwan_status;
368 
369 	res = acpi_hp_exec_wmi_command(sc->wmi_dev,
370 	    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &wireless);
371 	if (res != 0) {
372 		device_printf(sc->wmi_dev, "Wireless command error %x\n", res);
373 		return;
374 	}
375 	new_wlan_status = -1;
376 	new_bluetooth_status = -1;
377 	new_wwan_status = -1;
378 
379 	if (sc->verbose)
380 		device_printf(sc->wmi_dev, "Wireless status is %x\n", wireless);
381 	if (sc->wlan_disable_if_radio_off && !(wireless & HP_MASK_WLAN_RADIO)
382 	    &&  (wireless & HP_MASK_WLAN_ENABLED)) {
383 		acpi_hp_exec_wmi_command(sc->wmi_dev,
384 		    ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x100, NULL);
385 		new_wlan_status = 0;
386 	}
387 	else if (sc->wlan_enable_if_radio_on && (wireless & HP_MASK_WLAN_RADIO)
388 		&&  !(wireless & HP_MASK_WLAN_ENABLED)) {
389 		acpi_hp_exec_wmi_command(sc->wmi_dev,
390 		    ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x101, NULL);
391 		new_wlan_status = 1;
392 	}
393 	if (sc->bluetooth_disable_if_radio_off &&
394 	    !(wireless & HP_MASK_BLUETOOTH_RADIO) &&
395 	    (wireless & HP_MASK_BLUETOOTH_ENABLED)) {
396 		acpi_hp_exec_wmi_command(sc->wmi_dev,
397 		    ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x200, NULL);
398 		new_bluetooth_status = 0;
399 	}
400 	else if (sc->bluetooth_enable_if_radio_on &&
401 		(wireless & HP_MASK_BLUETOOTH_RADIO) &&
402 		!(wireless & HP_MASK_BLUETOOTH_ENABLED)) {
403 		acpi_hp_exec_wmi_command(sc->wmi_dev,
404 		    ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x202, NULL);
405 		new_bluetooth_status = 1;
406 	}
407 	if (sc->wwan_disable_if_radio_off &&
408 	    !(wireless & HP_MASK_WWAN_RADIO) &&
409 	    (wireless & HP_MASK_WWAN_ENABLED)) {
410 		acpi_hp_exec_wmi_command(sc->wmi_dev,
411 		ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x400, NULL);
412 		new_wwan_status = 0;
413 	}
414 	else if (sc->wwan_enable_if_radio_on &&
415 		(wireless & HP_MASK_WWAN_RADIO) &&
416 		!(wireless & HP_MASK_WWAN_ENABLED)) {
417 		acpi_hp_exec_wmi_command(sc->wmi_dev,
418 		    ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x404, NULL);
419 		new_wwan_status = 1;
420 	}
421 
422 	if (new_wlan_status == -1) {
423 		new_wlan_status = (wireless & HP_MASK_WLAN_ON_AIR);
424 		if ((new_wlan_status?1:0) != sc->was_wlan_on_air) {
425 			sc->was_wlan_on_air = sc->was_wlan_on_air?0:1;
426 			if (sc->verbose)
427 				device_printf(sc->wmi_dev,
428 			    	    "WLAN on air changed to %i "
429 			    	    "(new_wlan_status is %i)\n",
430 			    	    sc->was_wlan_on_air, new_wlan_status);
431 			acpi_UserNotify("HP", ACPI_ROOT_OBJECT,
432 			    0xc0+sc->was_wlan_on_air);
433 		}
434 	}
435 	if (new_bluetooth_status == -1) {
436 		new_bluetooth_status = (wireless & HP_MASK_BLUETOOTH_ON_AIR);
437 		if ((new_bluetooth_status?1:0) != sc->was_bluetooth_on_air) {
438 			sc->was_bluetooth_on_air = sc->was_bluetooth_on_air?
439 			    0:1;
440 			if (sc->verbose)
441 				device_printf(sc->wmi_dev,
442 				    "BLUETOOTH on air changed"
443 				    " to %i (new_bluetooth_status is %i)\n",
444 				    sc->was_bluetooth_on_air,
445 				    new_bluetooth_status);
446 			acpi_UserNotify("HP", ACPI_ROOT_OBJECT,
447 			    0xd0+sc->was_bluetooth_on_air);
448 		}
449 	}
450 	if (new_wwan_status == -1) {
451 		new_wwan_status = (wireless & HP_MASK_WWAN_ON_AIR);
452 		if ((new_wwan_status?1:0) != sc->was_wwan_on_air) {
453 			sc->was_wwan_on_air = sc->was_wwan_on_air?0:1;
454 			if (sc->verbose)
455 				device_printf(sc->wmi_dev,
456 				    "WWAN on air changed to %i"
457 			    	    " (new_wwan_status is %i)\n",
458 				    sc->was_wwan_on_air, new_wwan_status);
459 			acpi_UserNotify("HP", ACPI_ROOT_OBJECT,
460 			    0xe0+sc->was_wwan_on_air);
461 		}
462 	}
463 }
464 
465 static void
466 acpi_hp_identify(driver_t *driver, device_t parent)
467 {
468 
469 	/* Don't do anything if driver is disabled. */
470 	if (acpi_disabled("hp"))
471 		return;
472 
473 	/* Add only a single device instance. */
474 	if (device_find_child(parent, "acpi_hp", -1) != NULL)
475 		return;
476 
477 	/* Check BIOS GUID to see whether system is compatible. */
478 	if (!ACPI_WMI_PROVIDES_GUID_STRING(parent,
479 	    ACPI_HP_WMI_BIOS_GUID))
480 		return;
481 
482 	if (BUS_ADD_CHILD(parent, 0, "acpi_hp", -1) == NULL)
483 		device_printf(parent, "add acpi_hp child failed\n");
484 }
485 
486 static int
487 acpi_hp_probe(device_t dev)
488 {
489 
490 	device_set_desc(dev, "HP ACPI-WMI Mapping");
491 	return (0);
492 }
493 
494 static int
495 acpi_hp_attach(device_t dev)
496 {
497 	struct acpi_hp_softc	*sc;
498 	int			arg;
499 
500 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
501 
502 	sc = device_get_softc(dev);
503 	sc->dev = dev;
504 	sc->has_notify = 0;
505 	sc->has_cmi = 0;
506 	sc->bluetooth_enable_if_radio_on = 0;
507 	sc->bluetooth_disable_if_radio_off = 0;
508 	sc->wlan_enable_if_radio_on = 0;
509 	sc->wlan_disable_if_radio_off = 0;
510 	sc->wlan_enable_if_radio_on = 0;
511 	sc->wlan_disable_if_radio_off = 0;
512 	sc->was_wlan_on_air = 0;
513 	sc->was_bluetooth_on_air = 0;
514 	sc->was_wwan_on_air = 0;
515 	sc->cmi_detail = 0;
516 	sc->cmi_order_size = -1;
517 	sc->verbose = bootverbose;
518 	memset(sc->cmi_order, 0, sizeof(sc->cmi_order));
519 
520 	sc->wmi_dev = device_get_parent(dev);
521 	if (!ACPI_WMI_PROVIDES_GUID_STRING(sc->wmi_dev,
522 	    ACPI_HP_WMI_BIOS_GUID)) {
523 		device_printf(dev,
524 		    "WMI device does not provide the HP BIOS GUID\n");
525 		return (EINVAL);
526 	}
527 	if (ACPI_WMI_PROVIDES_GUID_STRING(sc->wmi_dev,
528 	    ACPI_HP_WMI_EVENT_GUID)) {
529 		device_printf(dev,
530 		    "HP event GUID detected, installing event handler\n");
531 		if (ACPI_WMI_INSTALL_EVENT_HANDLER(sc->wmi_dev,
532 		    ACPI_HP_WMI_EVENT_GUID, acpi_hp_notify, dev)) {
533 			device_printf(dev,
534 			    "Could not install notification handler!\n");
535 		}
536 		else {
537 			sc->has_notify = 1;
538 		}
539 	}
540 	if ((sc->has_cmi =
541 	    ACPI_WMI_PROVIDES_GUID_STRING(sc->wmi_dev, ACPI_HP_WMI_CMI_GUID)
542 	    )) {
543 		device_printf(dev, "HP CMI GUID detected\n");
544 	}
545 
546 	if (sc->has_cmi) {
547 		sc->hpcmi_dev_t = make_dev(&hpcmi_cdevsw, 0, UID_ROOT,
548 			    GID_WHEEL, 0644, "hpcmi");
549 		sc->hpcmi_dev_t->si_drv1 = sc;
550 		sc->hpcmi_open_pid = 0;
551 		sc->hpcmi_bufptr = -1;
552 	}
553 
554 	if (acpi_hp_exec_wmi_command(sc->wmi_dev,
555 	    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, NULL) == 0)
556 		sc->has_wireless = 1;
557 
558 	ACPI_SERIAL_BEGIN(hp);
559 
560 	sc->sysctl_ctx = device_get_sysctl_ctx(dev);
561 	sc->sysctl_tree = device_get_sysctl_tree(dev);
562 	for (int i = 0; acpi_hp_sysctls[i].name != NULL; ++i) {
563 		arg = 0;
564 		if (((!sc->has_notify || !sc->has_wireless) &&
565 		    (acpi_hp_sysctls[i].method ==
566 			ACPI_HP_METHOD_WLAN_ENABLE_IF_RADIO_ON ||
567 		    acpi_hp_sysctls[i].method ==
568 			ACPI_HP_METHOD_WLAN_DISABLE_IF_RADIO_OFF ||
569 		    acpi_hp_sysctls[i].method ==
570 			ACPI_HP_METHOD_BLUETOOTH_ENABLE_IF_RADIO_ON ||
571 		    acpi_hp_sysctls[i].method ==
572 			ACPI_HP_METHOD_BLUETOOTH_DISABLE_IF_RADIO_OFF ||
573 		    acpi_hp_sysctls[i].method ==
574 			ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON ||
575 		    acpi_hp_sysctls[i].method ==
576 			ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF)) ||
577 		    (arg = acpi_hp_sysctl_get(sc,
578 		    acpi_hp_sysctls[i].method)) < 0) {
579 			continue;
580 		}
581 		if (acpi_hp_sysctls[i].method == ACPI_HP_METHOD_WLAN_ON_AIR) {
582 			sc->was_wlan_on_air = arg;
583 		}
584 		else if (acpi_hp_sysctls[i].method ==
585 			    ACPI_HP_METHOD_BLUETOOTH_ON_AIR) {
586 			sc->was_bluetooth_on_air = arg;
587 		}
588 		else if (acpi_hp_sysctls[i].method ==
589 			    ACPI_HP_METHOD_WWAN_ON_AIR) {
590 			sc->was_wwan_on_air = arg;
591 		}
592 
593 		if (acpi_hp_sysctls[i].flag_rdonly != 0) {
594 			SYSCTL_ADD_PROC(sc->sysctl_ctx,
595 			    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
596 			    acpi_hp_sysctls[i].name, CTLTYPE_INT | CTLFLAG_RD,
597 			    sc, i, acpi_hp_sysctl, "I",
598 			    acpi_hp_sysctls[i].description);
599 		} else {
600 			SYSCTL_ADD_PROC(sc->sysctl_ctx,
601 			    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
602 			    acpi_hp_sysctls[i].name, CTLTYPE_INT | CTLFLAG_RW,
603 			    sc, i, acpi_hp_sysctl, "I",
604 			    acpi_hp_sysctls[i].description);
605 		}
606 	}
607 	ACPI_SERIAL_END(hp);
608 
609 	return (0);
610 }
611 
612 static int
613 acpi_hp_detach(device_t dev)
614 {
615 	struct acpi_hp_softc *sc;
616 
617 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
618 	sc = device_get_softc(dev);
619 	if (sc->has_cmi && sc->hpcmi_open_pid != 0)
620 		return (EBUSY);
621 
622 	if (sc->has_notify) {
623 		ACPI_WMI_REMOVE_EVENT_HANDLER(sc->wmi_dev,
624 		    ACPI_HP_WMI_EVENT_GUID);
625 	}
626 
627 	if (sc->has_cmi) {
628 		if (sc->hpcmi_bufptr != -1) {
629 			sbuf_delete(&sc->hpcmi_sbuf);
630 			sc->hpcmi_bufptr = -1;
631 		}
632 		sc->hpcmi_open_pid = 0;
633 		destroy_dev(sc->hpcmi_dev_t);
634 	}
635 
636 	return (0);
637 }
638 
639 static int
640 acpi_hp_sysctl(SYSCTL_HANDLER_ARGS)
641 {
642 	struct acpi_hp_softc	*sc;
643 	int			arg;
644 	int			oldarg;
645 	int			error = 0;
646 	int			function;
647 	int			method;
648 
649 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
650 
651 	sc = (struct acpi_hp_softc *)oidp->oid_arg1;
652 	function = oidp->oid_arg2;
653 	method = acpi_hp_sysctls[function].method;
654 
655 	ACPI_SERIAL_BEGIN(hp);
656 	arg = acpi_hp_sysctl_get(sc, method);
657 	oldarg = arg;
658 	error = sysctl_handle_int(oidp, &arg, 0, req);
659 	if (!error && req->newptr != NULL) {
660 		error = acpi_hp_sysctl_set(sc, method, arg, oldarg);
661 	}
662 	ACPI_SERIAL_END(hp);
663 
664 	return (error);
665 }
666 
667 static int
668 acpi_hp_sysctl_get(struct acpi_hp_softc *sc, int method)
669 {
670 	int	val = 0;
671 
672 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
673 	ACPI_SERIAL_ASSERT(hp);
674 
675 	switch (method) {
676 	case ACPI_HP_METHOD_WLAN_ENABLED:
677 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
678 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
679 			return (-EINVAL);
680 		val = ((val & HP_MASK_WLAN_ENABLED) != 0);
681 		break;
682 	case ACPI_HP_METHOD_WLAN_RADIO:
683 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
684 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
685 			return (-EINVAL);
686 		val = ((val & HP_MASK_WLAN_RADIO) != 0);
687 		break;
688 	case ACPI_HP_METHOD_WLAN_ON_AIR:
689 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
690 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
691 			return (-EINVAL);
692 		val = ((val & HP_MASK_WLAN_ON_AIR) != 0);
693 		break;
694 	case ACPI_HP_METHOD_WLAN_ENABLE_IF_RADIO_ON:
695 		val = sc->wlan_enable_if_radio_on;
696 		break;
697 	case ACPI_HP_METHOD_WLAN_DISABLE_IF_RADIO_OFF:
698 		val = sc->wlan_disable_if_radio_off;
699 		break;
700 	case ACPI_HP_METHOD_BLUETOOTH_ENABLED:
701 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
702 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
703 			return (-EINVAL);
704 		val = ((val & HP_MASK_BLUETOOTH_ENABLED) != 0);
705 		break;
706 	case ACPI_HP_METHOD_BLUETOOTH_RADIO:
707 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
708 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
709 			return (-EINVAL);
710 		val = ((val & HP_MASK_BLUETOOTH_RADIO) != 0);
711 		break;
712 	case ACPI_HP_METHOD_BLUETOOTH_ON_AIR:
713 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
714 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
715 			return (-EINVAL);
716 		val = ((val & HP_MASK_BLUETOOTH_ON_AIR) != 0);
717 		break;
718 	case ACPI_HP_METHOD_BLUETOOTH_ENABLE_IF_RADIO_ON:
719 		val = sc->bluetooth_enable_if_radio_on;
720 		break;
721 	case ACPI_HP_METHOD_BLUETOOTH_DISABLE_IF_RADIO_OFF:
722 		val = sc->bluetooth_disable_if_radio_off;
723 		break;
724 	case ACPI_HP_METHOD_WWAN_ENABLED:
725 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
726 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
727 			return (-EINVAL);
728 		val = ((val & HP_MASK_WWAN_ENABLED) != 0);
729 		break;
730 	case ACPI_HP_METHOD_WWAN_RADIO:
731 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
732 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
733 			return (-EINVAL);
734 		val = ((val & HP_MASK_WWAN_RADIO) != 0);
735 		break;
736 	case ACPI_HP_METHOD_WWAN_ON_AIR:
737 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
738 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
739 			return (-EINVAL);
740 		val = ((val & HP_MASK_WWAN_ON_AIR) != 0);
741 		break;
742 	case ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON:
743 		val = sc->wwan_enable_if_radio_on;
744 		break;
745 	case ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF:
746 		val = sc->wwan_disable_if_radio_off;
747 		break;
748 	case ACPI_HP_METHOD_ALS:
749 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
750 		    ACPI_HP_WMI_ALS_COMMAND, 0, 0, &val))
751 			return (-EINVAL);
752 		break;
753 	case ACPI_HP_METHOD_DISPLAY:
754 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
755 		    ACPI_HP_WMI_DISPLAY_COMMAND, 0, 0, &val))
756 			return (-EINVAL);
757 		break;
758 	case ACPI_HP_METHOD_HDDTEMP:
759 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
760 		    ACPI_HP_WMI_HDDTEMP_COMMAND, 0, 0, &val))
761 			return (-EINVAL);
762 		break;
763 	case ACPI_HP_METHOD_DOCK:
764 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
765 		    ACPI_HP_WMI_DOCK_COMMAND, 0, 0, &val))
766 			return (-EINVAL);
767 		break;
768 	case ACPI_HP_METHOD_CMI_DETAIL:
769 		val = sc->cmi_detail;
770 		break;
771 	case ACPI_HP_METHOD_VERBOSE:
772 		val = sc->verbose;
773 		break;
774 	}
775 
776 	return (val);
777 }
778 
779 static int
780 acpi_hp_sysctl_set(struct acpi_hp_softc *sc, int method, int arg, int oldarg)
781 {
782 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
783 	ACPI_SERIAL_ASSERT(hp);
784 
785 	if (method != ACPI_HP_METHOD_CMI_DETAIL &&
786 	    method != ACPI_HP_METHOD_VERBOSE)
787 		arg = arg?1:0;
788 
789 	if (arg != oldarg) {
790 		switch (method) {
791 		case ACPI_HP_METHOD_WLAN_ENABLED:
792 			if (acpi_hp_exec_wmi_command(sc->wmi_dev,
793 			    ACPI_HP_WMI_WIRELESS_COMMAND, 1,
794 			    arg?0x101:0x100, NULL))
795 				return (-EINVAL);
796 			break;
797 		case ACPI_HP_METHOD_WLAN_ENABLE_IF_RADIO_ON:
798 			sc->wlan_enable_if_radio_on = arg;
799 			acpi_hp_evaluate_auto_on_off(sc);
800 			break;
801 		case ACPI_HP_METHOD_WLAN_DISABLE_IF_RADIO_OFF:
802 			sc->wlan_disable_if_radio_off = arg;
803 			acpi_hp_evaluate_auto_on_off(sc);
804 			break;
805 		case ACPI_HP_METHOD_BLUETOOTH_ENABLED:
806 			if (acpi_hp_exec_wmi_command(sc->wmi_dev,
807 			    ACPI_HP_WMI_WIRELESS_COMMAND, 1,
808 			    arg?0x202:0x200, NULL))
809 				return (-EINVAL);
810 			break;
811 		case ACPI_HP_METHOD_BLUETOOTH_ENABLE_IF_RADIO_ON:
812 			sc->bluetooth_enable_if_radio_on = arg;
813 			acpi_hp_evaluate_auto_on_off(sc);
814 			break;
815 		case ACPI_HP_METHOD_BLUETOOTH_DISABLE_IF_RADIO_OFF:
816 			sc->bluetooth_disable_if_radio_off = arg?1:0;
817 			acpi_hp_evaluate_auto_on_off(sc);
818 			break;
819 		case ACPI_HP_METHOD_WWAN_ENABLED:
820 			if (acpi_hp_exec_wmi_command(sc->wmi_dev,
821 			    ACPI_HP_WMI_WIRELESS_COMMAND, 1,
822 			    arg?0x404:0x400, NULL))
823 				return (-EINVAL);
824 			break;
825 		case ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON:
826 			sc->wwan_enable_if_radio_on = arg?1:0;
827 			acpi_hp_evaluate_auto_on_off(sc);
828 			break;
829 		case ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF:
830 			sc->wwan_disable_if_radio_off = arg?1:0;
831 			acpi_hp_evaluate_auto_on_off(sc);
832 			break;
833 		case ACPI_HP_METHOD_ALS:
834 			if (acpi_hp_exec_wmi_command(sc->wmi_dev,
835 			    ACPI_HP_WMI_ALS_COMMAND, 1, arg?1:0, NULL))
836 				return (-EINVAL);
837 			break;
838 		case ACPI_HP_METHOD_CMI_DETAIL:
839 			sc->cmi_detail = arg;
840 			if ((arg & ACPI_HP_CMI_DETAIL_SHOW_MAX_INSTANCE) !=
841 			    (oldarg & ACPI_HP_CMI_DETAIL_SHOW_MAX_INSTANCE)) {
842 			    sc->cmi_order_size = -1;
843 			}
844 			break;
845 		case ACPI_HP_METHOD_VERBOSE:
846 			sc->verbose = arg;
847 			break;
848 		}
849 	}
850 
851 	return (0);
852 }
853 
854 static __inline void
855 acpi_hp_free_buffer(ACPI_BUFFER* buf) {
856 	if (buf && buf->Pointer) {
857 		AcpiOsFree(buf->Pointer);
858 	}
859 }
860 
861 static void
862 acpi_hp_notify(ACPI_HANDLE h, UINT32 notify, void *context)
863 {
864 	device_t dev = context;
865 	ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
866 
867 	struct acpi_hp_softc *sc = device_get_softc(dev);
868 	ACPI_BUFFER response = { ACPI_ALLOCATE_BUFFER, NULL };
869 	ACPI_OBJECT *obj;
870 	ACPI_WMI_GET_EVENT_DATA(sc->wmi_dev, notify, &response);
871 	obj = (ACPI_OBJECT*) response.Pointer;
872 	if (obj && obj->Type == ACPI_TYPE_BUFFER && obj->Buffer.Length == 8) {
873 		switch (*((UINT8 *) obj->Buffer.Pointer)) {
874 		case ACPI_HP_EVENT_WIRELESS:
875 			acpi_hp_evaluate_auto_on_off(sc);
876 			break;
877 		default:
878 			if (sc->verbose) {
879 				device_printf(sc->dev, "Event %02x\n",
880 				    *((UINT8 *) obj->Buffer.Pointer));
881 			}
882 			break;
883 		}
884 	}
885 	acpi_hp_free_buffer(&response);
886 }
887 
888 static int
889 acpi_hp_exec_wmi_command(device_t wmi_dev, int command, int is_write,
890     int val, int *retval)
891 {
892 	UINT32		params[4+32] = { 0x55434553, is_write ? 2 : 1,
893 			    command, 4, val};
894 	UINT32*		result;
895 	ACPI_OBJECT	*obj;
896 	ACPI_BUFFER	in = { sizeof(params), &params };
897 	ACPI_BUFFER	out = { ACPI_ALLOCATE_BUFFER, NULL };
898 	int res;
899 
900 	if (ACPI_FAILURE(ACPI_WMI_EVALUATE_CALL(wmi_dev, ACPI_HP_WMI_BIOS_GUID,
901 		    0, 0x3, &in, &out))) {
902 		acpi_hp_free_buffer(&out);
903 		return (-EINVAL);
904 	}
905 	obj = out.Pointer;
906 	if (!obj || obj->Type != ACPI_TYPE_BUFFER) {
907 		acpi_hp_free_buffer(&out);
908 		return (-EINVAL);
909 	}
910 	result = (UINT32*) obj->Buffer.Pointer;
911 	res = result[1];
912 	if (res == 0 && retval != NULL)
913 		*retval = result[2];
914 	acpi_hp_free_buffer(&out);
915 
916 	return (res);
917 }
918 
919 static __inline char*
920 acpi_hp_get_string_from_object(ACPI_OBJECT* obj, char* dst, size_t size) {
921 	int	length;
922 
923 	dst[0] = 0;
924 	if (obj->Type == ACPI_TYPE_STRING) {
925 		length = obj->String.Length+1;
926 		if (length > size) {
927 			length = size - 1;
928 		}
929 		strlcpy(dst, obj->String.Pointer, length);
930 		acpi_hp_hex_decode(dst);
931 	}
932 
933 	return (dst);
934 }
935 
936 
937 /*
938  * Read BIOS Setting block in instance "instance".
939  * The block returned is ACPI_TYPE_PACKAGE which should contain the following
940  * elements:
941  * Index Meaning
942  * 0        Setting Name [string]
943  * 1        Value (comma separated, asterisk marks the current value) [string]
944  * 2        Path within the bios hierarchy [string]
945  * 3        IsReadOnly [int]
946  * 4        DisplayInUI [int]
947  * 5        RequiresPhysicalPresence [int]
948  * 6        Sequence for ordering within the bios settings (absolute) [int]
949  * 7        Length of prerequisites array [int]
950  * 8..8+[7] PrerequisiteN [string]
951  * 9+[7]    Current value (in case of enum) [string] / Array length [int]
952  * 10+[7]   Enum length [int] / Array values
953  * 11+[7]ff Enum value at index x [string]
954  */
955 static int
956 acpi_hp_get_cmi_block(device_t wmi_dev, const char* guid, UINT8 instance,
957     char* outbuf, size_t outsize, UINT32* sequence, int detail)
958 {
959 	ACPI_OBJECT	*obj;
960 	ACPI_BUFFER	out = { ACPI_ALLOCATE_BUFFER, NULL };
961 	int		i;
962 	int		outlen;
963 	int		size = 255;
964 	int		has_enums = 0;
965 	int		valuebase = 0;
966 	char		string_buffer[size];
967 	int		enumbase;
968 
969 	outlen = 0;
970 	outbuf[0] = 0;
971 	if (ACPI_FAILURE(ACPI_WMI_GET_BLOCK(wmi_dev, guid, instance, &out))) {
972 		acpi_hp_free_buffer(&out);
973 		return (-EINVAL);
974 	}
975 	obj = out.Pointer;
976 	if (!obj || obj->Type != ACPI_TYPE_PACKAGE) {
977 		acpi_hp_free_buffer(&out);
978 		return (-EINVAL);
979 	}
980 
981 	/* Check if first 6 bytes matches our expectations. */
982 	if (obj->Package.Count < 8 ||
983 	    obj->Package.Elements[0].Type != ACPI_TYPE_STRING ||
984 	    obj->Package.Elements[1].Type != ACPI_TYPE_STRING ||
985 	    obj->Package.Elements[2].Type != ACPI_TYPE_STRING ||
986 	    obj->Package.Elements[3].Type != ACPI_TYPE_INTEGER ||
987 	    obj->Package.Elements[4].Type != ACPI_TYPE_INTEGER ||
988 	    obj->Package.Elements[5].Type != ACPI_TYPE_INTEGER ||
989 	    obj->Package.Elements[6].Type != ACPI_TYPE_INTEGER ||
990 	    obj->Package.Elements[7].Type != ACPI_TYPE_INTEGER) {
991 		acpi_hp_free_buffer(&out);
992 		return (-EINVAL);
993 	}
994 
995 	/* Skip prerequisites and optionally array. */
996 	valuebase = 8 + obj->Package.Elements[7].Integer.Value;
997 	if (obj->Package.Count <= valuebase) {
998 		acpi_hp_free_buffer(&out);
999 		return (-EINVAL);
1000 	}
1001 	if (obj->Package.Elements[valuebase].Type == ACPI_TYPE_INTEGER)
1002 		valuebase += 1 + obj->Package.Elements[valuebase].Integer.Value;
1003 
1004 	/* Check if we have value and enum. */
1005 	if (obj->Package.Count <= valuebase + 1 ||
1006 	    obj->Package.Elements[valuebase].Type != ACPI_TYPE_STRING ||
1007 	    obj->Package.Elements[valuebase+1].Type != ACPI_TYPE_INTEGER) {
1008 		acpi_hp_free_buffer(&out);
1009 		return (-EINVAL);
1010 	}
1011 	enumbase = valuebase + 1;
1012 	if (obj->Package.Count <= valuebase +
1013 	        obj->Package.Elements[enumbase].Integer.Value) {
1014 		acpi_hp_free_buffer(&out);
1015 		return (-EINVAL);
1016 	}
1017 
1018 	if (detail & ACPI_HP_CMI_DETAIL_PATHS) {
1019 		strlcat(outbuf, acpi_hp_get_string_from_object(
1020 		    &obj->Package.Elements[2], string_buffer, size), outsize);
1021 		outlen += 48;
1022 		while (strlen(outbuf) < outlen)
1023 			strlcat(outbuf, " ", outsize);
1024 	}
1025 	strlcat(outbuf, acpi_hp_get_string_from_object(
1026 	    &obj->Package.Elements[0], string_buffer, size), outsize);
1027 	outlen += 43;
1028 	while (strlen(outbuf) < outlen)
1029 		strlcat(outbuf, " ", outsize);
1030 	strlcat(outbuf, acpi_hp_get_string_from_object(
1031 	    &obj->Package.Elements[valuebase], string_buffer, size), outsize);
1032 	outlen += 21;
1033 	while (strlen(outbuf) < outlen)
1034 		strlcat(outbuf, " ", outsize);
1035 	for (i = 0; i < strlen(outbuf); ++i)
1036 		if (outbuf[i] == '\\')
1037 			outbuf[i] = '/';
1038 	if (detail & ACPI_HP_CMI_DETAIL_ENUMS) {
1039 		for (i = enumbase + 1; i < enumbase + 1 +
1040 		    obj->Package.Elements[enumbase].Integer.Value; ++i) {
1041 			acpi_hp_get_string_from_object(
1042 			    &obj->Package.Elements[i], string_buffer, size);
1043 			if (strlen(string_buffer) > 1 ||
1044 			    (strlen(string_buffer) == 1 &&
1045 			    string_buffer[0] != ' ')) {
1046 				if (has_enums)
1047 					strlcat(outbuf, "/", outsize);
1048 				else
1049 					strlcat(outbuf, " (", outsize);
1050 				strlcat(outbuf, string_buffer, outsize);
1051 				has_enums = 1;
1052 			}
1053 		}
1054 	}
1055 	if (has_enums)
1056 		strlcat(outbuf, ")", outsize);
1057 	if (detail & ACPI_HP_CMI_DETAIL_FLAGS) {
1058 		strlcat(outbuf, obj->Package.Elements[3].Integer.Value ?
1059 		    " [ReadOnly]" : "", outsize);
1060 		strlcat(outbuf, obj->Package.Elements[4].Integer.Value ?
1061 		    "" : " [NOUI]", outsize);
1062 		strlcat(outbuf, obj->Package.Elements[5].Integer.Value ?
1063 		    " [RPP]" : "", outsize);
1064 	}
1065 	*sequence = (UINT32) obj->Package.Elements[6].Integer.Value;
1066 	acpi_hp_free_buffer(&out);
1067 
1068 	return (0);
1069 }
1070 
1071 
1072 
1073 /*
1074  * Convert given two digit hex string (hexin) to an UINT8 referenced
1075  * by byteout.
1076  * Return != 0 if the was a problem (invalid input)
1077  */
1078 static __inline int acpi_hp_hex_to_int(const UINT8 *hexin, UINT8 *byteout)
1079 {
1080 	unsigned int	hi;
1081 	unsigned int	lo;
1082 
1083 	hi = hexin[0];
1084 	lo = hexin[1];
1085 	if ('0' <= hi && hi <= '9')
1086 		hi -= '0';
1087 	else if ('A' <= hi && hi <= 'F')
1088 		hi -= ('A' - 10);
1089 	else if ('a' <= hi && hi <= 'f')
1090 		hi -= ('a' - 10);
1091 	else
1092 		return (1);
1093 	if ('0' <= lo && lo <= '9')
1094 		lo -= '0';
1095 	else if ('A' <= lo && lo <= 'F')
1096 		lo -= ('A' - 10);
1097 	else if ('a' <= lo && lo <= 'f')
1098 		lo -= ('a' - 10);
1099 	else
1100 		return (1);
1101 	*byteout = (hi << 4) + lo;
1102 
1103 	return (0);
1104 }
1105 
1106 
1107 static void
1108 acpi_hp_hex_decode(char* buffer)
1109 {
1110 	int	i;
1111 	int	length = strlen(buffer);
1112 	UINT8	*uin;
1113 	UINT8	uout;
1114 
1115 	if (rounddown((int)length, 2) == length || length < 10)
1116 		return;
1117 
1118 	for (i = 0; i<length; ++i) {
1119 		if (!((i+1)%3)) {
1120 			if (buffer[i] != ' ')
1121 				return;
1122 		}
1123 		else
1124 			if (!((buffer[i] >= '0' && buffer[i] <= '9') ||
1125 		    	    (buffer[i] >= 'A' && buffer[i] <= 'F')))
1126 				return;
1127 	}
1128 
1129 	for (i = 0; i<length; i += 3) {
1130 		uin = &buffer[i];
1131 		uout = 0;
1132 		acpi_hp_hex_to_int(uin, &uout);
1133 		buffer[i/3] = (char) uout;
1134 	}
1135 	buffer[(length+1)/3] = 0;
1136 }
1137 
1138 
1139 /*
1140  * open hpcmi device
1141  */
1142 static int
1143 acpi_hp_hpcmi_open(struct cdev* dev, int flags, int mode, struct thread *td)
1144 {
1145 	struct acpi_hp_softc	*sc;
1146 	int			ret;
1147 
1148 	if (dev == NULL || dev->si_drv1 == NULL)
1149 		return (EBADF);
1150 	sc = dev->si_drv1;
1151 
1152 	ACPI_SERIAL_BEGIN(hp);
1153 	if (sc->hpcmi_open_pid != 0) {
1154 		ret = EBUSY;
1155 	}
1156 	else {
1157 		if (sbuf_new(&sc->hpcmi_sbuf, NULL, 4096, SBUF_AUTOEXTEND)
1158 		    == NULL) {
1159 			ret = ENXIO;
1160 		} else {
1161 			sc->hpcmi_open_pid = td->td_proc->p_pid;
1162 			sc->hpcmi_bufptr = 0;
1163 			ret = 0;
1164 		}
1165 	}
1166 	ACPI_SERIAL_END(hp);
1167 
1168 	return (ret);
1169 }
1170 
1171 /*
1172  * close hpcmi device
1173  */
1174 static int
1175 acpi_hp_hpcmi_close(struct cdev* dev, int flags, int mode, struct thread *td)
1176 {
1177 	struct acpi_hp_softc	*sc;
1178 	int			ret;
1179 
1180 	if (dev == NULL || dev->si_drv1 == NULL)
1181 		return (EBADF);
1182 	sc = dev->si_drv1;
1183 
1184 	ACPI_SERIAL_BEGIN(hp);
1185 	if (sc->hpcmi_open_pid == 0) {
1186 		ret = EBADF;
1187 	}
1188 	else {
1189 		if (sc->hpcmi_bufptr != -1) {
1190 			sbuf_delete(&sc->hpcmi_sbuf);
1191 			sc->hpcmi_bufptr = -1;
1192 		}
1193 		sc->hpcmi_open_pid = 0;
1194 		ret = 0;
1195 	}
1196 	ACPI_SERIAL_END(hp);
1197 
1198 	return (ret);
1199 }
1200 
1201 /*
1202  * Read from hpcmi bios information
1203  */
1204 static int
1205 acpi_hp_hpcmi_read(struct cdev *dev, struct uio *buf, int flag)
1206 {
1207 	struct acpi_hp_softc	*sc;
1208 	int			pos, i, l, ret;
1209 	UINT8			instance;
1210 	UINT8			maxInstance;
1211 	UINT32			sequence;
1212 	int			linesize = 1025;
1213 	char			line[linesize];
1214 
1215 	if (dev == NULL || dev->si_drv1 == NULL)
1216 		return (EBADF);
1217 	sc = dev->si_drv1;
1218 
1219 	ACPI_SERIAL_BEGIN(hp);
1220 	if (sc->hpcmi_open_pid != buf->uio_td->td_proc->p_pid
1221 	    || sc->hpcmi_bufptr == -1) {
1222 		ret = EBADF;
1223 	}
1224 	else {
1225 		if (!sbuf_done(&sc->hpcmi_sbuf)) {
1226 			if (sc->cmi_order_size < 0) {
1227 				maxInstance = sc->has_cmi;
1228 				if (!(sc->cmi_detail &
1229 				    ACPI_HP_CMI_DETAIL_SHOW_MAX_INSTANCE) &&
1230 				    maxInstance > 0) {
1231 					maxInstance--;
1232 				}
1233 				sc->cmi_order_size = 0;
1234 				for (instance = 0; instance < maxInstance;
1235 				    ++instance) {
1236 					if (acpi_hp_get_cmi_block(sc->wmi_dev,
1237 						ACPI_HP_WMI_CMI_GUID, instance,
1238 						line, linesize, &sequence,
1239 						sc->cmi_detail)) {
1240 						instance = maxInstance;
1241 					}
1242 					else {
1243 						pos = sc->cmi_order_size;
1244 						for (i=0;
1245 						  i<sc->cmi_order_size && i<127;
1246 						     ++i) {
1247 				if (sc->cmi_order[i].sequence > sequence) {
1248 								pos = i;
1249 								break;
1250 							}
1251 						}
1252 						for (i=sc->cmi_order_size;
1253 						    i>pos;
1254 						    --i) {
1255 						sc->cmi_order[i].sequence =
1256 						    sc->cmi_order[i-1].sequence;
1257 						sc->cmi_order[i].instance =
1258 						    sc->cmi_order[i-1].instance;
1259 						}
1260 						sc->cmi_order[pos].sequence =
1261 						    sequence;
1262 						sc->cmi_order[pos].instance =
1263 						    instance;
1264 						sc->cmi_order_size++;
1265 					}
1266 				}
1267 			}
1268 			for (i=0; i<sc->cmi_order_size; ++i) {
1269 				if (!acpi_hp_get_cmi_block(sc->wmi_dev,
1270 				    ACPI_HP_WMI_CMI_GUID,
1271 				    sc->cmi_order[i].instance, line, linesize,
1272 				    &sequence, sc->cmi_detail)) {
1273 					sbuf_printf(&sc->hpcmi_sbuf, "%s\n", line);
1274 				}
1275 			}
1276 			sbuf_finish(&sc->hpcmi_sbuf);
1277 		}
1278 		if (sbuf_len(&sc->hpcmi_sbuf) <= 0) {
1279 			sbuf_delete(&sc->hpcmi_sbuf);
1280 			sc->hpcmi_bufptr = -1;
1281 			sc->hpcmi_open_pid = 0;
1282 			ret = ENOMEM;
1283 		} else {
1284 			l = min(buf->uio_resid, sbuf_len(&sc->hpcmi_sbuf) -
1285 			    sc->hpcmi_bufptr);
1286 			ret = (l > 0)?uiomove(sbuf_data(&sc->hpcmi_sbuf) +
1287 			    sc->hpcmi_bufptr, l, buf) : 0;
1288 			sc->hpcmi_bufptr += l;
1289 		}
1290 	}
1291 	ACPI_SERIAL_END(hp);
1292 
1293 	return (ret);
1294 }
1295