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