xref: /freebsd/sys/dev/acpi_support/acpi_hp.c (revision 5d3e7166)
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 	{ NULL, 0, NULL, 0 }
302 };
303 
304 ACPI_SERIAL_DECL(hp, "HP ACPI-WMI Mapping");
305 
306 static void	acpi_hp_identify(driver_t *driver, device_t parent);
307 static int	acpi_hp_probe(device_t dev);
308 static int	acpi_hp_attach(device_t dev);
309 static int	acpi_hp_detach(device_t dev);
310 
311 static void	acpi_hp_evaluate_auto_on_off(struct acpi_hp_softc* sc);
312 static int	acpi_hp_sysctl(SYSCTL_HANDLER_ARGS);
313 static int	acpi_hp_sysctl_set(struct acpi_hp_softc *sc, int method,
314 		    int arg, int oldarg);
315 static int	acpi_hp_sysctl_get(struct acpi_hp_softc *sc, int method);
316 static int	acpi_hp_exec_wmi_command(device_t wmi_dev, int command,
317 		    int is_write, int val, int *retval);
318 static void	acpi_hp_notify(ACPI_HANDLE h, UINT32 notify, void *context);
319 static int	acpi_hp_get_cmi_block(device_t wmi_dev, const char* guid,
320 		    UINT8 instance, char* outbuf, size_t outsize,
321 		    UINT32* sequence, int detail);
322 static void	acpi_hp_hex_decode(char* buffer);
323 
324 static d_open_t	acpi_hp_hpcmi_open;
325 static d_close_t acpi_hp_hpcmi_close;
326 static d_read_t	acpi_hp_hpcmi_read;
327 
328 /* handler /dev/hpcmi device */
329 static struct cdevsw hpcmi_cdevsw = {
330 	.d_version = D_VERSION,
331 	.d_open = acpi_hp_hpcmi_open,
332 	.d_close = acpi_hp_hpcmi_close,
333 	.d_read = acpi_hp_hpcmi_read,
334 	.d_name = "hpcmi",
335 };
336 
337 static device_method_t acpi_hp_methods[] = {
338 	DEVMETHOD(device_identify, acpi_hp_identify),
339 	DEVMETHOD(device_probe, acpi_hp_probe),
340 	DEVMETHOD(device_attach, acpi_hp_attach),
341 	DEVMETHOD(device_detach, acpi_hp_detach),
342 
343 	DEVMETHOD_END
344 };
345 
346 static driver_t	acpi_hp_driver = {
347 	"acpi_hp",
348 	acpi_hp_methods,
349 	sizeof(struct acpi_hp_softc),
350 };
351 
352 DRIVER_MODULE(acpi_hp, acpi_wmi, acpi_hp_driver, 0, 0);
353 MODULE_DEPEND(acpi_hp, acpi_wmi, 1, 1, 1);
354 MODULE_DEPEND(acpi_hp, acpi, 1, 1, 1);
355 
356 static void
357 acpi_hp_evaluate_auto_on_off(struct acpi_hp_softc *sc)
358 {
359 	int	res;
360 	int	wireless;
361 	int	new_wlan_status;
362 	int	new_bluetooth_status;
363 	int	new_wwan_status;
364 
365 	res = acpi_hp_exec_wmi_command(sc->wmi_dev,
366 	    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &wireless);
367 	if (res != 0) {
368 		device_printf(sc->wmi_dev, "Wireless command error %x\n", res);
369 		return;
370 	}
371 	new_wlan_status = -1;
372 	new_bluetooth_status = -1;
373 	new_wwan_status = -1;
374 
375 	if (sc->verbose)
376 		device_printf(sc->wmi_dev, "Wireless status is %x\n", wireless);
377 	if (sc->wlan_disable_if_radio_off && !(wireless & HP_MASK_WLAN_RADIO)
378 	    &&  (wireless & HP_MASK_WLAN_ENABLED)) {
379 		acpi_hp_exec_wmi_command(sc->wmi_dev,
380 		    ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x100, NULL);
381 		new_wlan_status = 0;
382 	}
383 	else if (sc->wlan_enable_if_radio_on && (wireless & HP_MASK_WLAN_RADIO)
384 		&&  !(wireless & HP_MASK_WLAN_ENABLED)) {
385 		acpi_hp_exec_wmi_command(sc->wmi_dev,
386 		    ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x101, NULL);
387 		new_wlan_status = 1;
388 	}
389 	if (sc->bluetooth_disable_if_radio_off &&
390 	    !(wireless & HP_MASK_BLUETOOTH_RADIO) &&
391 	    (wireless & HP_MASK_BLUETOOTH_ENABLED)) {
392 		acpi_hp_exec_wmi_command(sc->wmi_dev,
393 		    ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x200, NULL);
394 		new_bluetooth_status = 0;
395 	}
396 	else if (sc->bluetooth_enable_if_radio_on &&
397 		(wireless & HP_MASK_BLUETOOTH_RADIO) &&
398 		!(wireless & HP_MASK_BLUETOOTH_ENABLED)) {
399 		acpi_hp_exec_wmi_command(sc->wmi_dev,
400 		    ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x202, NULL);
401 		new_bluetooth_status = 1;
402 	}
403 	if (sc->wwan_disable_if_radio_off &&
404 	    !(wireless & HP_MASK_WWAN_RADIO) &&
405 	    (wireless & HP_MASK_WWAN_ENABLED)) {
406 		acpi_hp_exec_wmi_command(sc->wmi_dev,
407 		ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x400, NULL);
408 		new_wwan_status = 0;
409 	}
410 	else if (sc->wwan_enable_if_radio_on &&
411 		(wireless & HP_MASK_WWAN_RADIO) &&
412 		!(wireless & HP_MASK_WWAN_ENABLED)) {
413 		acpi_hp_exec_wmi_command(sc->wmi_dev,
414 		    ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x404, NULL);
415 		new_wwan_status = 1;
416 	}
417 
418 	if (new_wlan_status == -1) {
419 		new_wlan_status = (wireless & HP_MASK_WLAN_ON_AIR);
420 		if ((new_wlan_status?1:0) != sc->was_wlan_on_air) {
421 			sc->was_wlan_on_air = sc->was_wlan_on_air?0:1;
422 			if (sc->verbose)
423 				device_printf(sc->wmi_dev,
424 			    	    "WLAN on air changed to %i "
425 			    	    "(new_wlan_status is %i)\n",
426 			    	    sc->was_wlan_on_air, new_wlan_status);
427 			acpi_UserNotify("HP", ACPI_ROOT_OBJECT,
428 			    0xc0+sc->was_wlan_on_air);
429 		}
430 	}
431 	if (new_bluetooth_status == -1) {
432 		new_bluetooth_status = (wireless & HP_MASK_BLUETOOTH_ON_AIR);
433 		if ((new_bluetooth_status?1:0) != sc->was_bluetooth_on_air) {
434 			sc->was_bluetooth_on_air = sc->was_bluetooth_on_air?
435 			    0:1;
436 			if (sc->verbose)
437 				device_printf(sc->wmi_dev,
438 				    "BLUETOOTH on air changed"
439 				    " to %i (new_bluetooth_status is %i)\n",
440 				    sc->was_bluetooth_on_air,
441 				    new_bluetooth_status);
442 			acpi_UserNotify("HP", ACPI_ROOT_OBJECT,
443 			    0xd0+sc->was_bluetooth_on_air);
444 		}
445 	}
446 	if (new_wwan_status == -1) {
447 		new_wwan_status = (wireless & HP_MASK_WWAN_ON_AIR);
448 		if ((new_wwan_status?1:0) != sc->was_wwan_on_air) {
449 			sc->was_wwan_on_air = sc->was_wwan_on_air?0:1;
450 			if (sc->verbose)
451 				device_printf(sc->wmi_dev,
452 				    "WWAN on air changed to %i"
453 			    	    " (new_wwan_status is %i)\n",
454 				    sc->was_wwan_on_air, new_wwan_status);
455 			acpi_UserNotify("HP", ACPI_ROOT_OBJECT,
456 			    0xe0+sc->was_wwan_on_air);
457 		}
458 	}
459 }
460 
461 static void
462 acpi_hp_identify(driver_t *driver, device_t parent)
463 {
464 
465 	/* Don't do anything if driver is disabled. */
466 	if (acpi_disabled("hp"))
467 		return;
468 
469 	/* Add only a single device instance. */
470 	if (device_find_child(parent, "acpi_hp", -1) != NULL)
471 		return;
472 
473 	/* Check BIOS GUID to see whether system is compatible. */
474 	if (!ACPI_WMI_PROVIDES_GUID_STRING(parent,
475 	    ACPI_HP_WMI_BIOS_GUID))
476 		return;
477 
478 	if (BUS_ADD_CHILD(parent, 0, "acpi_hp", -1) == NULL)
479 		device_printf(parent, "add acpi_hp child failed\n");
480 }
481 
482 static int
483 acpi_hp_probe(device_t dev)
484 {
485 
486 	device_set_desc(dev, "HP ACPI-WMI Mapping");
487 	return (0);
488 }
489 
490 static int
491 acpi_hp_attach(device_t dev)
492 {
493 	struct acpi_hp_softc	*sc;
494 	int			arg;
495 
496 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
497 
498 	sc = device_get_softc(dev);
499 	sc->dev = dev;
500 	sc->has_notify = 0;
501 	sc->has_cmi = 0;
502 	sc->bluetooth_enable_if_radio_on = 0;
503 	sc->bluetooth_disable_if_radio_off = 0;
504 	sc->wlan_enable_if_radio_on = 0;
505 	sc->wlan_disable_if_radio_off = 0;
506 	sc->wlan_enable_if_radio_on = 0;
507 	sc->wlan_disable_if_radio_off = 0;
508 	sc->was_wlan_on_air = 0;
509 	sc->was_bluetooth_on_air = 0;
510 	sc->was_wwan_on_air = 0;
511 	sc->cmi_detail = 0;
512 	sc->cmi_order_size = -1;
513 	sc->verbose = bootverbose;
514 	memset(sc->cmi_order, 0, sizeof(sc->cmi_order));
515 
516 	sc->wmi_dev = device_get_parent(dev);
517 	if (!ACPI_WMI_PROVIDES_GUID_STRING(sc->wmi_dev,
518 	    ACPI_HP_WMI_BIOS_GUID)) {
519 		device_printf(dev,
520 		    "WMI device does not provide the HP BIOS GUID\n");
521 		return (EINVAL);
522 	}
523 	if (ACPI_WMI_PROVIDES_GUID_STRING(sc->wmi_dev,
524 	    ACPI_HP_WMI_EVENT_GUID)) {
525 		device_printf(dev,
526 		    "HP event GUID detected, installing event handler\n");
527 		if (ACPI_WMI_INSTALL_EVENT_HANDLER(sc->wmi_dev,
528 		    ACPI_HP_WMI_EVENT_GUID, acpi_hp_notify, dev)) {
529 			device_printf(dev,
530 			    "Could not install notification handler!\n");
531 		}
532 		else {
533 			sc->has_notify = 1;
534 		}
535 	}
536 	if ((sc->has_cmi =
537 	    ACPI_WMI_PROVIDES_GUID_STRING(sc->wmi_dev, ACPI_HP_WMI_CMI_GUID)
538 	    )) {
539 		device_printf(dev, "HP CMI GUID detected\n");
540 	}
541 
542 	if (sc->has_cmi) {
543 		sc->hpcmi_dev_t = make_dev(&hpcmi_cdevsw, 0, UID_ROOT,
544 			    GID_WHEEL, 0644, "hpcmi");
545 		sc->hpcmi_dev_t->si_drv1 = sc;
546 		sc->hpcmi_open_pid = 0;
547 		sc->hpcmi_bufptr = -1;
548 	}
549 
550 	if (acpi_hp_exec_wmi_command(sc->wmi_dev,
551 	    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, NULL) == 0)
552 		sc->has_wireless = 1;
553 
554 	ACPI_SERIAL_BEGIN(hp);
555 
556 	sc->sysctl_ctx = device_get_sysctl_ctx(dev);
557 	sc->sysctl_tree = device_get_sysctl_tree(dev);
558 	for (int i = 0; acpi_hp_sysctls[i].name != NULL; ++i) {
559 		arg = 0;
560 		if (((!sc->has_notify || !sc->has_wireless) &&
561 		    (acpi_hp_sysctls[i].method ==
562 			ACPI_HP_METHOD_WLAN_ENABLE_IF_RADIO_ON ||
563 		    acpi_hp_sysctls[i].method ==
564 			ACPI_HP_METHOD_WLAN_DISABLE_IF_RADIO_OFF ||
565 		    acpi_hp_sysctls[i].method ==
566 			ACPI_HP_METHOD_BLUETOOTH_ENABLE_IF_RADIO_ON ||
567 		    acpi_hp_sysctls[i].method ==
568 			ACPI_HP_METHOD_BLUETOOTH_DISABLE_IF_RADIO_OFF ||
569 		    acpi_hp_sysctls[i].method ==
570 			ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON ||
571 		    acpi_hp_sysctls[i].method ==
572 			ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF)) ||
573 		    (arg = acpi_hp_sysctl_get(sc,
574 		    acpi_hp_sysctls[i].method)) < 0) {
575 			continue;
576 		}
577 		if (acpi_hp_sysctls[i].method == ACPI_HP_METHOD_WLAN_ON_AIR) {
578 			sc->was_wlan_on_air = arg;
579 		}
580 		else if (acpi_hp_sysctls[i].method ==
581 			    ACPI_HP_METHOD_BLUETOOTH_ON_AIR) {
582 			sc->was_bluetooth_on_air = arg;
583 		}
584 		else if (acpi_hp_sysctls[i].method ==
585 			    ACPI_HP_METHOD_WWAN_ON_AIR) {
586 			sc->was_wwan_on_air = arg;
587 		}
588 
589 		if (acpi_hp_sysctls[i].flag_rdonly != 0) {
590 			SYSCTL_ADD_PROC(sc->sysctl_ctx,
591 			    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
592 			    acpi_hp_sysctls[i].name,
593 			    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
594 			    sc, i, acpi_hp_sysctl, "I",
595 			    acpi_hp_sysctls[i].description);
596 		} else {
597 			SYSCTL_ADD_PROC(sc->sysctl_ctx,
598 			    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
599 			    acpi_hp_sysctls[i].name,
600 			    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
601 			    sc, i, acpi_hp_sysctl, "I",
602 			    acpi_hp_sysctls[i].description);
603 		}
604 	}
605 	ACPI_SERIAL_END(hp);
606 
607 	return (0);
608 }
609 
610 static int
611 acpi_hp_detach(device_t dev)
612 {
613 	struct acpi_hp_softc *sc;
614 
615 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
616 	sc = device_get_softc(dev);
617 	if (sc->has_cmi && sc->hpcmi_open_pid != 0)
618 		return (EBUSY);
619 
620 	if (sc->has_notify) {
621 		ACPI_WMI_REMOVE_EVENT_HANDLER(sc->wmi_dev,
622 		    ACPI_HP_WMI_EVENT_GUID);
623 	}
624 
625 	if (sc->has_cmi) {
626 		if (sc->hpcmi_bufptr != -1) {
627 			sbuf_delete(&sc->hpcmi_sbuf);
628 			sc->hpcmi_bufptr = -1;
629 		}
630 		sc->hpcmi_open_pid = 0;
631 		destroy_dev(sc->hpcmi_dev_t);
632 	}
633 
634 	return (0);
635 }
636 
637 static int
638 acpi_hp_sysctl(SYSCTL_HANDLER_ARGS)
639 {
640 	struct acpi_hp_softc	*sc;
641 	int			arg;
642 	int			oldarg;
643 	int			error = 0;
644 	int			function;
645 	int			method;
646 
647 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
648 
649 	sc = (struct acpi_hp_softc *)oidp->oid_arg1;
650 	function = oidp->oid_arg2;
651 	method = acpi_hp_sysctls[function].method;
652 
653 	ACPI_SERIAL_BEGIN(hp);
654 	arg = acpi_hp_sysctl_get(sc, method);
655 	oldarg = arg;
656 	error = sysctl_handle_int(oidp, &arg, 0, req);
657 	if (!error && req->newptr != NULL) {
658 		error = acpi_hp_sysctl_set(sc, method, arg, oldarg);
659 	}
660 	ACPI_SERIAL_END(hp);
661 
662 	return (error);
663 }
664 
665 static int
666 acpi_hp_sysctl_get(struct acpi_hp_softc *sc, int method)
667 {
668 	int	val = 0;
669 
670 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
671 	ACPI_SERIAL_ASSERT(hp);
672 
673 	switch (method) {
674 	case ACPI_HP_METHOD_WLAN_ENABLED:
675 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
676 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
677 			return (-EINVAL);
678 		val = ((val & HP_MASK_WLAN_ENABLED) != 0);
679 		break;
680 	case ACPI_HP_METHOD_WLAN_RADIO:
681 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
682 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
683 			return (-EINVAL);
684 		val = ((val & HP_MASK_WLAN_RADIO) != 0);
685 		break;
686 	case ACPI_HP_METHOD_WLAN_ON_AIR:
687 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
688 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
689 			return (-EINVAL);
690 		val = ((val & HP_MASK_WLAN_ON_AIR) != 0);
691 		break;
692 	case ACPI_HP_METHOD_WLAN_ENABLE_IF_RADIO_ON:
693 		val = sc->wlan_enable_if_radio_on;
694 		break;
695 	case ACPI_HP_METHOD_WLAN_DISABLE_IF_RADIO_OFF:
696 		val = sc->wlan_disable_if_radio_off;
697 		break;
698 	case ACPI_HP_METHOD_BLUETOOTH_ENABLED:
699 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
700 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
701 			return (-EINVAL);
702 		val = ((val & HP_MASK_BLUETOOTH_ENABLED) != 0);
703 		break;
704 	case ACPI_HP_METHOD_BLUETOOTH_RADIO:
705 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
706 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
707 			return (-EINVAL);
708 		val = ((val & HP_MASK_BLUETOOTH_RADIO) != 0);
709 		break;
710 	case ACPI_HP_METHOD_BLUETOOTH_ON_AIR:
711 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
712 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
713 			return (-EINVAL);
714 		val = ((val & HP_MASK_BLUETOOTH_ON_AIR) != 0);
715 		break;
716 	case ACPI_HP_METHOD_BLUETOOTH_ENABLE_IF_RADIO_ON:
717 		val = sc->bluetooth_enable_if_radio_on;
718 		break;
719 	case ACPI_HP_METHOD_BLUETOOTH_DISABLE_IF_RADIO_OFF:
720 		val = sc->bluetooth_disable_if_radio_off;
721 		break;
722 	case ACPI_HP_METHOD_WWAN_ENABLED:
723 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
724 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
725 			return (-EINVAL);
726 		val = ((val & HP_MASK_WWAN_ENABLED) != 0);
727 		break;
728 	case ACPI_HP_METHOD_WWAN_RADIO:
729 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
730 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
731 			return (-EINVAL);
732 		val = ((val & HP_MASK_WWAN_RADIO) != 0);
733 		break;
734 	case ACPI_HP_METHOD_WWAN_ON_AIR:
735 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
736 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
737 			return (-EINVAL);
738 		val = ((val & HP_MASK_WWAN_ON_AIR) != 0);
739 		break;
740 	case ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON:
741 		val = sc->wwan_enable_if_radio_on;
742 		break;
743 	case ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF:
744 		val = sc->wwan_disable_if_radio_off;
745 		break;
746 	case ACPI_HP_METHOD_ALS:
747 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
748 		    ACPI_HP_WMI_ALS_COMMAND, 0, 0, &val))
749 			return (-EINVAL);
750 		break;
751 	case ACPI_HP_METHOD_DISPLAY:
752 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
753 		    ACPI_HP_WMI_DISPLAY_COMMAND, 0, 0, &val))
754 			return (-EINVAL);
755 		break;
756 	case ACPI_HP_METHOD_HDDTEMP:
757 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
758 		    ACPI_HP_WMI_HDDTEMP_COMMAND, 0, 0, &val))
759 			return (-EINVAL);
760 		break;
761 	case ACPI_HP_METHOD_DOCK:
762 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
763 		    ACPI_HP_WMI_DOCK_COMMAND, 0, 0, &val))
764 			return (-EINVAL);
765 		break;
766 	case ACPI_HP_METHOD_CMI_DETAIL:
767 		val = sc->cmi_detail;
768 		break;
769 	case ACPI_HP_METHOD_VERBOSE:
770 		val = sc->verbose;
771 		break;
772 	}
773 
774 	return (val);
775 }
776 
777 static int
778 acpi_hp_sysctl_set(struct acpi_hp_softc *sc, int method, int arg, int oldarg)
779 {
780 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
781 	ACPI_SERIAL_ASSERT(hp);
782 
783 	if (method != ACPI_HP_METHOD_CMI_DETAIL &&
784 	    method != ACPI_HP_METHOD_VERBOSE)
785 		arg = arg?1:0;
786 
787 	if (arg != oldarg) {
788 		switch (method) {
789 		case ACPI_HP_METHOD_WLAN_ENABLED:
790 			if (acpi_hp_exec_wmi_command(sc->wmi_dev,
791 			    ACPI_HP_WMI_WIRELESS_COMMAND, 1,
792 			    arg?0x101:0x100, NULL))
793 				return (-EINVAL);
794 			break;
795 		case ACPI_HP_METHOD_WLAN_ENABLE_IF_RADIO_ON:
796 			sc->wlan_enable_if_radio_on = arg;
797 			acpi_hp_evaluate_auto_on_off(sc);
798 			break;
799 		case ACPI_HP_METHOD_WLAN_DISABLE_IF_RADIO_OFF:
800 			sc->wlan_disable_if_radio_off = arg;
801 			acpi_hp_evaluate_auto_on_off(sc);
802 			break;
803 		case ACPI_HP_METHOD_BLUETOOTH_ENABLED:
804 			if (acpi_hp_exec_wmi_command(sc->wmi_dev,
805 			    ACPI_HP_WMI_WIRELESS_COMMAND, 1,
806 			    arg?0x202:0x200, NULL))
807 				return (-EINVAL);
808 			break;
809 		case ACPI_HP_METHOD_BLUETOOTH_ENABLE_IF_RADIO_ON:
810 			sc->bluetooth_enable_if_radio_on = arg;
811 			acpi_hp_evaluate_auto_on_off(sc);
812 			break;
813 		case ACPI_HP_METHOD_BLUETOOTH_DISABLE_IF_RADIO_OFF:
814 			sc->bluetooth_disable_if_radio_off = arg?1:0;
815 			acpi_hp_evaluate_auto_on_off(sc);
816 			break;
817 		case ACPI_HP_METHOD_WWAN_ENABLED:
818 			if (acpi_hp_exec_wmi_command(sc->wmi_dev,
819 			    ACPI_HP_WMI_WIRELESS_COMMAND, 1,
820 			    arg?0x404:0x400, NULL))
821 				return (-EINVAL);
822 			break;
823 		case ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON:
824 			sc->wwan_enable_if_radio_on = arg?1:0;
825 			acpi_hp_evaluate_auto_on_off(sc);
826 			break;
827 		case ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF:
828 			sc->wwan_disable_if_radio_off = arg?1:0;
829 			acpi_hp_evaluate_auto_on_off(sc);
830 			break;
831 		case ACPI_HP_METHOD_ALS:
832 			if (acpi_hp_exec_wmi_command(sc->wmi_dev,
833 			    ACPI_HP_WMI_ALS_COMMAND, 1, arg?1:0, NULL))
834 				return (-EINVAL);
835 			break;
836 		case ACPI_HP_METHOD_CMI_DETAIL:
837 			sc->cmi_detail = arg;
838 			if ((arg & ACPI_HP_CMI_DETAIL_SHOW_MAX_INSTANCE) !=
839 			    (oldarg & ACPI_HP_CMI_DETAIL_SHOW_MAX_INSTANCE)) {
840 			    sc->cmi_order_size = -1;
841 			}
842 			break;
843 		case ACPI_HP_METHOD_VERBOSE:
844 			sc->verbose = arg;
845 			break;
846 		}
847 	}
848 
849 	return (0);
850 }
851 
852 static __inline void
853 acpi_hp_free_buffer(ACPI_BUFFER* buf) {
854 	if (buf && buf->Pointer) {
855 		AcpiOsFree(buf->Pointer);
856 	}
857 }
858 
859 static void
860 acpi_hp_notify(ACPI_HANDLE h, UINT32 notify, void *context)
861 {
862 	device_t dev = context;
863 	ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
864 
865 	struct acpi_hp_softc *sc = device_get_softc(dev);
866 	ACPI_BUFFER response = { ACPI_ALLOCATE_BUFFER, NULL };
867 	ACPI_OBJECT *obj;
868 	ACPI_WMI_GET_EVENT_DATA(sc->wmi_dev, notify, &response);
869 	obj = (ACPI_OBJECT*) response.Pointer;
870 	if (obj && obj->Type == ACPI_TYPE_BUFFER && obj->Buffer.Length == 8) {
871 		switch (*((UINT8 *) obj->Buffer.Pointer)) {
872 		case ACPI_HP_EVENT_WIRELESS:
873 			acpi_hp_evaluate_auto_on_off(sc);
874 			break;
875 		default:
876 			if (sc->verbose) {
877 				device_printf(sc->dev, "Event %02x\n",
878 				    *((UINT8 *) obj->Buffer.Pointer));
879 			}
880 			break;
881 		}
882 	}
883 	acpi_hp_free_buffer(&response);
884 }
885 
886 static int
887 acpi_hp_exec_wmi_command(device_t wmi_dev, int command, int is_write,
888     int val, int *retval)
889 {
890 	UINT32		params[4+32] = { 0x55434553, is_write ? 2 : 1,
891 			    command, 4, val};
892 	UINT32*		result;
893 	ACPI_OBJECT	*obj;
894 	ACPI_BUFFER	in = { sizeof(params), &params };
895 	ACPI_BUFFER	out = { ACPI_ALLOCATE_BUFFER, NULL };
896 	int res;
897 
898 	if (ACPI_FAILURE(ACPI_WMI_EVALUATE_CALL(wmi_dev, ACPI_HP_WMI_BIOS_GUID,
899 		    0, 0x3, &in, &out))) {
900 		acpi_hp_free_buffer(&out);
901 		return (-EINVAL);
902 	}
903 	obj = out.Pointer;
904 	if (!obj || obj->Type != ACPI_TYPE_BUFFER) {
905 		acpi_hp_free_buffer(&out);
906 		return (-EINVAL);
907 	}
908 	result = (UINT32*) obj->Buffer.Pointer;
909 	res = result[1];
910 	if (res == 0 && retval != NULL)
911 		*retval = result[2];
912 	acpi_hp_free_buffer(&out);
913 
914 	return (res);
915 }
916 
917 static __inline char*
918 acpi_hp_get_string_from_object(ACPI_OBJECT* obj, char* dst, size_t size) {
919 	int	length;
920 
921 	dst[0] = 0;
922 	if (obj->Type == ACPI_TYPE_STRING) {
923 		length = obj->String.Length+1;
924 		if (length > size) {
925 			length = size - 1;
926 		}
927 		strlcpy(dst, obj->String.Pointer, length);
928 		acpi_hp_hex_decode(dst);
929 	}
930 
931 	return (dst);
932 }
933 
934 /*
935  * Read BIOS Setting block in instance "instance".
936  * The block returned is ACPI_TYPE_PACKAGE which should contain the following
937  * elements:
938  * Index Meaning
939  * 0        Setting Name [string]
940  * 1        Value (comma separated, asterisk marks the current value) [string]
941  * 2        Path within the bios hierarchy [string]
942  * 3        IsReadOnly [int]
943  * 4        DisplayInUI [int]
944  * 5        RequiresPhysicalPresence [int]
945  * 6        Sequence for ordering within the bios settings (absolute) [int]
946  * 7        Length of prerequisites array [int]
947  * 8..8+[7] PrerequisiteN [string]
948  * 9+[7]    Current value (in case of enum) [string] / Array length [int]
949  * 10+[7]   Enum length [int] / Array values
950  * 11+[7]ff Enum value at index x [string]
951  */
952 static int
953 acpi_hp_get_cmi_block(device_t wmi_dev, const char* guid, UINT8 instance,
954     char* outbuf, size_t outsize, UINT32* sequence, int detail)
955 {
956 	ACPI_OBJECT	*obj;
957 	ACPI_BUFFER	out = { ACPI_ALLOCATE_BUFFER, NULL };
958 	int		i;
959 	int		outlen;
960 	int		has_enums = 0;
961 	int		valuebase = 0;
962 	char		string_buffer[255];
963 	int		enumbase;
964 
965 	outlen = 0;
966 	outbuf[0] = 0;
967 	if (ACPI_FAILURE(ACPI_WMI_GET_BLOCK(wmi_dev, guid, instance, &out))) {
968 		acpi_hp_free_buffer(&out);
969 		return (-EINVAL);
970 	}
971 	obj = out.Pointer;
972 	if (!obj || obj->Type != ACPI_TYPE_PACKAGE) {
973 		acpi_hp_free_buffer(&out);
974 		return (-EINVAL);
975 	}
976 
977 	/* Check if first 6 bytes matches our expectations. */
978 	if (obj->Package.Count < 8 ||
979 	    obj->Package.Elements[0].Type != ACPI_TYPE_STRING ||
980 	    obj->Package.Elements[1].Type != ACPI_TYPE_STRING ||
981 	    obj->Package.Elements[2].Type != ACPI_TYPE_STRING ||
982 	    obj->Package.Elements[3].Type != ACPI_TYPE_INTEGER ||
983 	    obj->Package.Elements[4].Type != ACPI_TYPE_INTEGER ||
984 	    obj->Package.Elements[5].Type != ACPI_TYPE_INTEGER ||
985 	    obj->Package.Elements[6].Type != ACPI_TYPE_INTEGER ||
986 	    obj->Package.Elements[7].Type != ACPI_TYPE_INTEGER) {
987 		acpi_hp_free_buffer(&out);
988 		return (-EINVAL);
989 	}
990 
991 	/* Skip prerequisites and optionally array. */
992 	valuebase = 8 + obj->Package.Elements[7].Integer.Value;
993 	if (obj->Package.Count <= valuebase) {
994 		acpi_hp_free_buffer(&out);
995 		return (-EINVAL);
996 	}
997 	if (obj->Package.Elements[valuebase].Type == ACPI_TYPE_INTEGER)
998 		valuebase += 1 + obj->Package.Elements[valuebase].Integer.Value;
999 
1000 	/* Check if we have value and enum. */
1001 	if (obj->Package.Count <= valuebase + 1 ||
1002 	    obj->Package.Elements[valuebase].Type != ACPI_TYPE_STRING ||
1003 	    obj->Package.Elements[valuebase+1].Type != ACPI_TYPE_INTEGER) {
1004 		acpi_hp_free_buffer(&out);
1005 		return (-EINVAL);
1006 	}
1007 	enumbase = valuebase + 1;
1008 	if (obj->Package.Count <= valuebase +
1009 	        obj->Package.Elements[enumbase].Integer.Value) {
1010 		acpi_hp_free_buffer(&out);
1011 		return (-EINVAL);
1012 	}
1013 
1014 	if (detail & ACPI_HP_CMI_DETAIL_PATHS) {
1015 		strlcat(outbuf, acpi_hp_get_string_from_object(
1016 		    &obj->Package.Elements[2],
1017 		    string_buffer, sizeof(string_buffer)), outsize);
1018 		outlen += 48;
1019 		while (strlen(outbuf) < outlen)
1020 			strlcat(outbuf, " ", outsize);
1021 	}
1022 	strlcat(outbuf, acpi_hp_get_string_from_object(
1023 	    &obj->Package.Elements[0],
1024 	    string_buffer, sizeof(string_buffer)), outsize);
1025 	outlen += 43;
1026 	while (strlen(outbuf) < outlen)
1027 		strlcat(outbuf, " ", outsize);
1028 	strlcat(outbuf, acpi_hp_get_string_from_object(
1029 	    &obj->Package.Elements[valuebase],
1030 	    string_buffer, sizeof(string_buffer)), outsize);
1031 	outlen += 21;
1032 	while (strlen(outbuf) < outlen)
1033 		strlcat(outbuf, " ", outsize);
1034 	for (i = 0; i < strlen(outbuf); ++i)
1035 		if (outbuf[i] == '\\')
1036 			outbuf[i] = '/';
1037 	if (detail & ACPI_HP_CMI_DETAIL_ENUMS) {
1038 		for (i = enumbase + 1; i < enumbase + 1 +
1039 		    obj->Package.Elements[enumbase].Integer.Value; ++i) {
1040 			acpi_hp_get_string_from_object(
1041 			    &obj->Package.Elements[i],
1042 			    string_buffer, sizeof(string_buffer));
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  * Convert given two digit hex string (hexin) to an UINT8 referenced
1073  * by byteout.
1074  * Return != 0 if the was a problem (invalid input)
1075  */
1076 static __inline int acpi_hp_hex_to_int(const UINT8 *hexin, UINT8 *byteout)
1077 {
1078 	unsigned int	hi;
1079 	unsigned int	lo;
1080 
1081 	hi = hexin[0];
1082 	lo = hexin[1];
1083 	if ('0' <= hi && hi <= '9')
1084 		hi -= '0';
1085 	else if ('A' <= hi && hi <= 'F')
1086 		hi -= ('A' - 10);
1087 	else if ('a' <= hi && hi <= 'f')
1088 		hi -= ('a' - 10);
1089 	else
1090 		return (1);
1091 	if ('0' <= lo && lo <= '9')
1092 		lo -= '0';
1093 	else if ('A' <= lo && lo <= 'F')
1094 		lo -= ('A' - 10);
1095 	else if ('a' <= lo && lo <= 'f')
1096 		lo -= ('a' - 10);
1097 	else
1098 		return (1);
1099 	*byteout = (hi << 4) + lo;
1100 
1101 	return (0);
1102 }
1103 
1104 static void
1105 acpi_hp_hex_decode(char* buffer)
1106 {
1107 	int	i;
1108 	int	length = strlen(buffer);
1109 	UINT8	*uin;
1110 	UINT8	uout;
1111 
1112 	if (rounddown((int)length, 2) == length || length < 10)
1113 		return;
1114 
1115 	for (i = 0; i<length; ++i) {
1116 		if (!((i+1)%3)) {
1117 			if (buffer[i] != ' ')
1118 				return;
1119 		}
1120 		else
1121 			if (!((buffer[i] >= '0' && buffer[i] <= '9') ||
1122 		    	    (buffer[i] >= 'A' && buffer[i] <= 'F')))
1123 				return;
1124 	}
1125 
1126 	for (i = 0; i<length; i += 3) {
1127 		uin = &buffer[i];
1128 		uout = 0;
1129 		acpi_hp_hex_to_int(uin, &uout);
1130 		buffer[i/3] = (char) uout;
1131 	}
1132 	buffer[(length+1)/3] = 0;
1133 }
1134 
1135 /*
1136  * open hpcmi device
1137  */
1138 static int
1139 acpi_hp_hpcmi_open(struct cdev* dev, int flags, int mode, struct thread *td)
1140 {
1141 	struct acpi_hp_softc	*sc;
1142 	int			ret;
1143 
1144 	if (dev == NULL || dev->si_drv1 == NULL)
1145 		return (EBADF);
1146 	sc = dev->si_drv1;
1147 
1148 	ACPI_SERIAL_BEGIN(hp);
1149 	if (sc->hpcmi_open_pid != 0) {
1150 		ret = EBUSY;
1151 	}
1152 	else {
1153 		if (sbuf_new(&sc->hpcmi_sbuf, NULL, 4096, SBUF_AUTOEXTEND)
1154 		    == NULL) {
1155 			ret = ENXIO;
1156 		} else {
1157 			sc->hpcmi_open_pid = td->td_proc->p_pid;
1158 			sc->hpcmi_bufptr = 0;
1159 			ret = 0;
1160 		}
1161 	}
1162 	ACPI_SERIAL_END(hp);
1163 
1164 	return (ret);
1165 }
1166 
1167 /*
1168  * close hpcmi device
1169  */
1170 static int
1171 acpi_hp_hpcmi_close(struct cdev* dev, int flags, int mode, struct thread *td)
1172 {
1173 	struct acpi_hp_softc	*sc;
1174 	int			ret;
1175 
1176 	if (dev == NULL || dev->si_drv1 == NULL)
1177 		return (EBADF);
1178 	sc = dev->si_drv1;
1179 
1180 	ACPI_SERIAL_BEGIN(hp);
1181 	if (sc->hpcmi_open_pid == 0) {
1182 		ret = EBADF;
1183 	}
1184 	else {
1185 		if (sc->hpcmi_bufptr != -1) {
1186 			sbuf_delete(&sc->hpcmi_sbuf);
1187 			sc->hpcmi_bufptr = -1;
1188 		}
1189 		sc->hpcmi_open_pid = 0;
1190 		ret = 0;
1191 	}
1192 	ACPI_SERIAL_END(hp);
1193 
1194 	return (ret);
1195 }
1196 
1197 /*
1198  * Read from hpcmi bios information
1199  */
1200 static int
1201 acpi_hp_hpcmi_read(struct cdev *dev, struct uio *buf, int flag)
1202 {
1203 	struct acpi_hp_softc	*sc;
1204 	int			pos, i, l, ret;
1205 	UINT8			instance;
1206 	UINT8			maxInstance;
1207 	UINT32			sequence;
1208 	char			line[1025];
1209 
1210 	if (dev == NULL || dev->si_drv1 == NULL)
1211 		return (EBADF);
1212 	sc = dev->si_drv1;
1213 
1214 	ACPI_SERIAL_BEGIN(hp);
1215 	if (sc->hpcmi_open_pid != buf->uio_td->td_proc->p_pid
1216 	    || sc->hpcmi_bufptr == -1) {
1217 		ret = EBADF;
1218 	}
1219 	else {
1220 		if (!sbuf_done(&sc->hpcmi_sbuf)) {
1221 			if (sc->cmi_order_size < 0) {
1222 				maxInstance = sc->has_cmi;
1223 				if (!(sc->cmi_detail &
1224 				    ACPI_HP_CMI_DETAIL_SHOW_MAX_INSTANCE) &&
1225 				    maxInstance > 0) {
1226 					maxInstance--;
1227 				}
1228 				sc->cmi_order_size = 0;
1229 				for (instance = 0; instance < maxInstance;
1230 				    ++instance) {
1231 					if (acpi_hp_get_cmi_block(sc->wmi_dev,
1232 						ACPI_HP_WMI_CMI_GUID, instance,
1233 						line, sizeof(line), &sequence,
1234 						sc->cmi_detail)) {
1235 						instance = maxInstance;
1236 					}
1237 					else {
1238 						pos = sc->cmi_order_size;
1239 						for (i=0;
1240 						  i<sc->cmi_order_size && i<127;
1241 						     ++i) {
1242 				if (sc->cmi_order[i].sequence > sequence) {
1243 								pos = i;
1244 								break;
1245 							}
1246 						}
1247 						for (i=sc->cmi_order_size;
1248 						    i>pos;
1249 						    --i) {
1250 						sc->cmi_order[i].sequence =
1251 						    sc->cmi_order[i-1].sequence;
1252 						sc->cmi_order[i].instance =
1253 						    sc->cmi_order[i-1].instance;
1254 						}
1255 						sc->cmi_order[pos].sequence =
1256 						    sequence;
1257 						sc->cmi_order[pos].instance =
1258 						    instance;
1259 						sc->cmi_order_size++;
1260 					}
1261 				}
1262 			}
1263 			for (i=0; i<sc->cmi_order_size; ++i) {
1264 				if (!acpi_hp_get_cmi_block(sc->wmi_dev,
1265 				    ACPI_HP_WMI_CMI_GUID,
1266 				    sc->cmi_order[i].instance, line, sizeof(line),
1267 				    &sequence, sc->cmi_detail)) {
1268 					sbuf_printf(&sc->hpcmi_sbuf, "%s\n", line);
1269 				}
1270 			}
1271 			sbuf_finish(&sc->hpcmi_sbuf);
1272 		}
1273 		if (sbuf_len(&sc->hpcmi_sbuf) <= 0) {
1274 			sbuf_delete(&sc->hpcmi_sbuf);
1275 			sc->hpcmi_bufptr = -1;
1276 			sc->hpcmi_open_pid = 0;
1277 			ret = ENOMEM;
1278 		} else {
1279 			l = min(buf->uio_resid, sbuf_len(&sc->hpcmi_sbuf) -
1280 			    sc->hpcmi_bufptr);
1281 			ret = (l > 0)?uiomove(sbuf_data(&sc->hpcmi_sbuf) +
1282 			    sc->hpcmi_bufptr, l, buf) : 0;
1283 			sc->hpcmi_bufptr += l;
1284 		}
1285 	}
1286 	ACPI_SERIAL_END(hp);
1287 
1288 	return (ret);
1289 }
1290