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