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