xref: /freebsd/sys/dev/acpi_support/acpi_ibm.c (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
1 /*-
2  * Copyright (c) 2004 Takanori Watanabe
3  * Copyright (c) 2005 Markus Brueffer <markus@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 /*
32  * Driver for extra ACPI-controlled gadgets found on IBM ThinkPad laptops.
33  * Inspired by the ibm-acpi and tpb projects which implement these features
34  * on Linux.
35  *
36  *   acpi-ibm: <http://ibm-acpi.sourceforge.net/>
37  *        tpb: <http://www.nongnu.org/tpb/>
38  */
39 
40 #include "opt_acpi.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/bus.h>
45 #include <machine/cpufunc.h>
46 
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 
50 #include "acpi_if.h"
51 #include <sys/module.h>
52 #include <dev/acpica/acpivar.h>
53 #include <dev/led/led.h>
54 #include <sys/power.h>
55 #include <sys/sbuf.h>
56 #include <sys/sysctl.h>
57 #include <isa/rtc.h>
58 
59 #define _COMPONENT	ACPI_OEM
60 ACPI_MODULE_NAME("IBM")
61 
62 /* Internal methods */
63 #define ACPI_IBM_METHOD_EVENTS		1
64 #define ACPI_IBM_METHOD_EVENTMASK	2
65 #define ACPI_IBM_METHOD_HOTKEY		3
66 #define ACPI_IBM_METHOD_BRIGHTNESS	4
67 #define ACPI_IBM_METHOD_VOLUME		5
68 #define ACPI_IBM_METHOD_MUTE		6
69 #define ACPI_IBM_METHOD_THINKLIGHT	7
70 #define ACPI_IBM_METHOD_BLUETOOTH	8
71 #define ACPI_IBM_METHOD_WLAN		9
72 #define ACPI_IBM_METHOD_FANSPEED	10
73 #define ACPI_IBM_METHOD_FANLEVEL	11
74 #define ACPI_IBM_METHOD_FANSTATUS	12
75 #define ACPI_IBM_METHOD_THERMAL		13
76 #define ACPI_IBM_METHOD_HANDLEREVENTS	14
77 
78 /* Hotkeys/Buttons */
79 #define IBM_RTC_HOTKEY1			0x64
80 #define   IBM_RTC_MASK_HOME		(1 << 0)
81 #define   IBM_RTC_MASK_SEARCH		(1 << 1)
82 #define   IBM_RTC_MASK_MAIL		(1 << 2)
83 #define   IBM_RTC_MASK_WLAN		(1 << 5)
84 #define IBM_RTC_HOTKEY2			0x65
85 #define   IBM_RTC_MASK_THINKPAD		(1 << 3)
86 #define   IBM_RTC_MASK_ZOOM		(1 << 5)
87 #define   IBM_RTC_MASK_VIDEO		(1 << 6)
88 #define   IBM_RTC_MASK_HIBERNATE	(1 << 7)
89 #define IBM_RTC_THINKLIGHT		0x66
90 #define   IBM_RTC_MASK_THINKLIGHT	(1 << 4)
91 #define IBM_RTC_SCREENEXPAND		0x67
92 #define   IBM_RTC_MASK_SCREENEXPAND	(1 << 5)
93 #define IBM_RTC_BRIGHTNESS		0x6c
94 #define   IBM_RTC_MASK_BRIGHTNESS	(1 << 5)
95 #define IBM_RTC_VOLUME			0x6e
96 #define   IBM_RTC_MASK_VOLUME		(1 << 7)
97 
98 /* Embedded Controller registers */
99 #define IBM_EC_BRIGHTNESS		0x31
100 #define   IBM_EC_MASK_BRI		0x7
101 #define IBM_EC_VOLUME			0x30
102 #define   IBM_EC_MASK_VOL		0xf
103 #define   IBM_EC_MASK_MUTE		(1 << 6)
104 #define IBM_EC_FANSTATUS		0x2F
105 #define   IBM_EC_MASK_FANLEVEL		0x3f
106 #define   IBM_EC_MASK_FANDISENGAGED	(1 << 6)
107 #define   IBM_EC_MASK_FANSTATUS		(1 << 7)
108 #define IBM_EC_FANSPEED			0x84
109 
110 /* CMOS Commands */
111 #define IBM_CMOS_VOLUME_DOWN		0
112 #define IBM_CMOS_VOLUME_UP		1
113 #define IBM_CMOS_VOLUME_MUTE		2
114 #define IBM_CMOS_BRIGHTNESS_UP		4
115 #define IBM_CMOS_BRIGHTNESS_DOWN	5
116 
117 /* ACPI methods */
118 #define IBM_NAME_KEYLIGHT		"KBLT"
119 #define IBM_NAME_WLAN_BT_GET		"GBDC"
120 #define IBM_NAME_WLAN_BT_SET		"SBDC"
121 #define   IBM_NAME_MASK_BT		(1 << 1)
122 #define   IBM_NAME_MASK_WLAN		(1 << 2)
123 #define IBM_NAME_THERMAL_GET		"TMP7"
124 #define IBM_NAME_THERMAL_UPDT		"UPDT"
125 
126 #define IBM_NAME_EVENTS_STATUS_GET	"DHKC"
127 #define IBM_NAME_EVENTS_MASK_GET	"DHKN"
128 #define IBM_NAME_EVENTS_STATUS_SET	"MHKC"
129 #define IBM_NAME_EVENTS_MASK_SET	"MHKM"
130 #define IBM_NAME_EVENTS_GET		"MHKP"
131 #define IBM_NAME_EVENTS_AVAILMASK	"MHKA"
132 
133 /* Event Code */
134 #define IBM_EVENT_LCD_BACKLIGHT		0x03
135 #define IBM_EVENT_SUSPEND_TO_RAM	0x04
136 #define IBM_EVENT_BLUETOOTH		0x05
137 #define IBM_EVENT_SCREEN_EXPAND		0x07
138 #define IBM_EVENT_SUSPEND_TO_DISK	0x0c
139 #define IBM_EVENT_BRIGHTNESS_UP		0x10
140 #define IBM_EVENT_BRIGHTNESS_DOWN	0x11
141 #define IBM_EVENT_THINKLIGHT		0x12
142 #define IBM_EVENT_ZOOM			0x14
143 #define IBM_EVENT_VOLUME_UP		0x15
144 #define IBM_EVENT_VOLUME_DOWN		0x16
145 #define IBM_EVENT_MUTE			0x17
146 #define IBM_EVENT_ACCESS_IBM_BUTTON	0x18
147 
148 #define ABS(x) (((x) < 0)? -(x) : (x))
149 
150 struct acpi_ibm_softc {
151 	device_t	dev;
152 	ACPI_HANDLE	handle;
153 
154 	/* Embedded controller */
155 	device_t	ec_dev;
156 	ACPI_HANDLE	ec_handle;
157 
158 	/* CMOS */
159 	ACPI_HANDLE	cmos_handle;
160 
161 	/* Fan status */
162 	ACPI_HANDLE	fan_handle;
163 	int		fan_levels;
164 
165 	/* Keylight commands and states */
166 	ACPI_HANDLE	light_handle;
167 	int		light_cmd_on;
168 	int		light_cmd_off;
169 	int		light_val;
170 	int		light_get_supported;
171 	int		light_set_supported;
172 
173 	/* led(4) interface */
174 	struct cdev	*led_dev;
175 	int		led_busy;
176 	int		led_state;
177 
178 	int		wlan_bt_flags;
179 	int		thermal_updt_supported;
180 
181 	unsigned int	events_availmask;
182 	unsigned int	events_initialmask;
183 	int		events_mask_supported;
184 	int		events_enable;
185 
186 	unsigned int	handler_events;
187 
188 	struct sysctl_ctx_list	*sysctl_ctx;
189 	struct sysctl_oid	*sysctl_tree;
190 };
191 
192 static struct {
193 	char	*name;
194 	int	method;
195 	char	*description;
196 	int	flag_rdonly;
197 } acpi_ibm_sysctls[] = {
198 	{
199 		.name		= "events",
200 		.method		= ACPI_IBM_METHOD_EVENTS,
201 		.description	= "ACPI events enable",
202 	},
203 	{
204 		.name		= "eventmask",
205 		.method		= ACPI_IBM_METHOD_EVENTMASK,
206 		.description	= "ACPI eventmask",
207 	},
208 	{
209 		.name		= "hotkey",
210 		.method		= ACPI_IBM_METHOD_HOTKEY,
211 		.description	= "Key Status",
212 		.flag_rdonly	= 1
213 	},
214 	{
215 		.name		= "lcd_brightness",
216 		.method		= ACPI_IBM_METHOD_BRIGHTNESS,
217 		.description	= "LCD Brightness",
218 	},
219 	{
220 		.name		= "volume",
221 		.method		= ACPI_IBM_METHOD_VOLUME,
222 		.description	= "Volume",
223 	},
224 	{
225 		.name		= "mute",
226 		.method		= ACPI_IBM_METHOD_MUTE,
227 		.description	= "Mute",
228 	},
229 	{
230 		.name		= "thinklight",
231 		.method		= ACPI_IBM_METHOD_THINKLIGHT,
232 		.description	= "Thinklight enable",
233 	},
234 	{
235 		.name		= "bluetooth",
236 		.method		= ACPI_IBM_METHOD_BLUETOOTH,
237 		.description	= "Bluetooth enable",
238 	},
239 	{
240 		.name		= "wlan",
241 		.method		= ACPI_IBM_METHOD_WLAN,
242 		.description	= "WLAN enable",
243 		.flag_rdonly	= 1
244 	},
245 	{
246 		.name		= "fan_speed",
247 		.method		= ACPI_IBM_METHOD_FANSPEED,
248 		.description	= "Fan speed",
249 		.flag_rdonly	= 1
250 	},
251 	{
252 		.name		= "fan_level",
253 		.method		= ACPI_IBM_METHOD_FANLEVEL,
254 		.description	= "Fan level",
255 	},
256 	{
257 		.name		= "fan",
258 		.method		= ACPI_IBM_METHOD_FANSTATUS,
259 		.description	= "Fan enable",
260 	},
261 
262 	{ NULL, 0, NULL, 0 }
263 };
264 
265 /*
266  * Per-model default list of event mask.
267  */
268 #define	ACPI_IBM_HKEY_RFKILL_MASK		(1 << 4)
269 #define	ACPI_IBM_HKEY_DSWITCH_MASK		(1 << 6)
270 #define	ACPI_IBM_HKEY_BRIGHTNESS_UP_MASK	(1 << 15)
271 #define	ACPI_IBM_HKEY_BRIGHTNESS_DOWN_MASK	(1 << 16)
272 #define	ACPI_IBM_HKEY_SEARCH_MASK		(1 << 18)
273 #define	ACPI_IBM_HKEY_MICMUTE_MASK		(1 << 26)
274 #define	ACPI_IBM_HKEY_SETTINGS_MASK		(1 << 28)
275 #define	ACPI_IBM_HKEY_VIEWOPEN_MASK		(1 << 30)
276 #define	ACPI_IBM_HKEY_VIEWALL_MASK		(1 << 31)
277 
278 struct acpi_ibm_models {
279 	const char *maker;
280 	const char *product;
281 	uint32_t eventmask;
282 } acpi_ibm_models[] = {
283 	{ "LENOVO", "20BSCTO1WW",
284 	  ACPI_IBM_HKEY_RFKILL_MASK |
285 	  ACPI_IBM_HKEY_DSWITCH_MASK |
286 	  ACPI_IBM_HKEY_BRIGHTNESS_UP_MASK |
287 	  ACPI_IBM_HKEY_BRIGHTNESS_DOWN_MASK |
288 	  ACPI_IBM_HKEY_SEARCH_MASK |
289 	  ACPI_IBM_HKEY_MICMUTE_MASK |
290 	  ACPI_IBM_HKEY_SETTINGS_MASK |
291 	  ACPI_IBM_HKEY_VIEWOPEN_MASK |
292 	  ACPI_IBM_HKEY_VIEWALL_MASK
293 	}
294 };
295 
296 ACPI_SERIAL_DECL(ibm, "ACPI IBM extras");
297 
298 static int	acpi_ibm_probe(device_t dev);
299 static int	acpi_ibm_attach(device_t dev);
300 static int	acpi_ibm_detach(device_t dev);
301 static int	acpi_ibm_resume(device_t dev);
302 
303 static void	ibm_led(void *softc, int onoff);
304 static void	ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused);
305 
306 static int	acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS);
307 static int	acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method);
308 static int	acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method);
309 static int	acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int val);
310 
311 static int	acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val);
312 static int	acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS);
313 static int	acpi_ibm_handlerevents_sysctl(SYSCTL_HANDLER_ARGS);
314 static void	acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context);
315 
316 static int	acpi_ibm_brightness_set(struct acpi_ibm_softc *sc, int arg);
317 static int	acpi_ibm_bluetooth_set(struct acpi_ibm_softc *sc, int arg);
318 static int	acpi_ibm_thinklight_set(struct acpi_ibm_softc *sc, int arg);
319 static int	acpi_ibm_volume_set(struct acpi_ibm_softc *sc, int arg);
320 static int	acpi_ibm_mute_set(struct acpi_ibm_softc *sc, int arg);
321 
322 static device_method_t acpi_ibm_methods[] = {
323 	/* Device interface */
324 	DEVMETHOD(device_probe, acpi_ibm_probe),
325 	DEVMETHOD(device_attach, acpi_ibm_attach),
326 	DEVMETHOD(device_detach, acpi_ibm_detach),
327 	DEVMETHOD(device_resume, acpi_ibm_resume),
328 
329 	DEVMETHOD_END
330 };
331 
332 static driver_t	acpi_ibm_driver = {
333 	"acpi_ibm",
334 	acpi_ibm_methods,
335 	sizeof(struct acpi_ibm_softc),
336 };
337 
338 static devclass_t acpi_ibm_devclass;
339 
340 DRIVER_MODULE(acpi_ibm, acpi, acpi_ibm_driver, acpi_ibm_devclass,
341 	      0, 0);
342 MODULE_DEPEND(acpi_ibm, acpi, 1, 1, 1);
343 static char    *ibm_ids[] = {"IBM0068", "LEN0068", NULL};
344 
345 static void
346 ibm_led(void *softc, int onoff)
347 {
348 	struct acpi_ibm_softc* sc = (struct acpi_ibm_softc*) softc;
349 
350 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
351 
352 	if (sc->led_busy)
353 		return;
354 
355 	sc->led_busy = 1;
356 	sc->led_state = onoff;
357 
358 	AcpiOsExecute(OSL_NOTIFY_HANDLER, (void *)ibm_led_task, sc);
359 }
360 
361 static void
362 ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused)
363 {
364 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
365 
366 	ACPI_SERIAL_BEGIN(ibm);
367 	acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_THINKLIGHT, sc->led_state);
368 	ACPI_SERIAL_END(ibm);
369 
370 	sc->led_busy = 0;
371 }
372 
373 static int
374 acpi_ibm_probe(device_t dev)
375 {
376 	if (acpi_disabled("ibm") ||
377 	    ACPI_ID_PROBE(device_get_parent(dev), dev, ibm_ids) == NULL ||
378 	    device_get_unit(dev) != 0)
379 		return (ENXIO);
380 
381 	device_set_desc(dev, "IBM ThinkPad ACPI Extras");
382 
383 	return (0);
384 }
385 
386 static int
387 acpi_ibm_attach(device_t dev)
388 {
389 	int i;
390 	struct acpi_ibm_softc	*sc;
391 	char *maker, *product;
392 	devclass_t		ec_devclass;
393 
394 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
395 
396 	sc = device_get_softc(dev);
397 	sc->dev = dev;
398 	sc->handle = acpi_get_handle(dev);
399 
400 	/* Look for the first embedded controller */
401         if (!(ec_devclass = devclass_find ("acpi_ec"))) {
402 		if (bootverbose)
403 			device_printf(dev, "Couldn't find acpi_ec devclass\n");
404 		return (EINVAL);
405 	}
406         if (!(sc->ec_dev = devclass_get_device(ec_devclass, 0))) {
407 		if (bootverbose)
408 			device_printf(dev, "Couldn't find acpi_ec device\n");
409 		return (EINVAL);
410 	}
411 	sc->ec_handle = acpi_get_handle(sc->ec_dev);
412 
413 	/* Get the sysctl tree */
414 	sc->sysctl_ctx = device_get_sysctl_ctx(dev);
415 	sc->sysctl_tree = device_get_sysctl_tree(dev);
416 
417 	/* Look for event mask and hook up the nodes */
418 	sc->events_mask_supported = ACPI_SUCCESS(acpi_GetInteger(sc->handle,
419 	    IBM_NAME_EVENTS_MASK_GET, &sc->events_initialmask));
420 
421 	if (sc->events_mask_supported) {
422 		SYSCTL_ADD_UINT(sc->sysctl_ctx,
423 		    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
424 		    "initialmask", CTLFLAG_RD,
425 		    &sc->events_initialmask, 0, "Initial eventmask");
426 
427 		/* The availmask is the bitmask of supported events */
428 		if (ACPI_FAILURE(acpi_GetInteger(sc->handle,
429 		    IBM_NAME_EVENTS_AVAILMASK, &sc->events_availmask)))
430 			sc->events_availmask = 0xffffffff;
431 
432 		SYSCTL_ADD_UINT(sc->sysctl_ctx,
433 		    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
434 		    "availmask", CTLFLAG_RD,
435 		    &sc->events_availmask, 0, "Mask of supported events");
436 	}
437 
438 	/* Hook up proc nodes */
439 	for (int i = 0; acpi_ibm_sysctls[i].name != NULL; i++) {
440 		if (!acpi_ibm_sysctl_init(sc, acpi_ibm_sysctls[i].method))
441 			continue;
442 
443 		if (acpi_ibm_sysctls[i].flag_rdonly != 0) {
444 			SYSCTL_ADD_PROC(sc->sysctl_ctx,
445 			    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
446 			    acpi_ibm_sysctls[i].name, CTLTYPE_INT | CTLFLAG_RD,
447 			    sc, i, acpi_ibm_sysctl, "I",
448 			    acpi_ibm_sysctls[i].description);
449 		} else {
450 			SYSCTL_ADD_PROC(sc->sysctl_ctx,
451 			    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
452 			    acpi_ibm_sysctls[i].name, CTLTYPE_INT | CTLFLAG_RW,
453 			    sc, i, acpi_ibm_sysctl, "I",
454 			    acpi_ibm_sysctls[i].description);
455 		}
456 	}
457 
458 	/* Hook up thermal node */
459 	if (acpi_ibm_sysctl_init(sc, ACPI_IBM_METHOD_THERMAL)) {
460 		SYSCTL_ADD_PROC(sc->sysctl_ctx,
461 		    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
462 		    "thermal", CTLTYPE_INT | CTLFLAG_RD,
463 		    sc, 0, acpi_ibm_thermal_sysctl, "I",
464 		    "Thermal zones");
465 	}
466 
467 	/* Hook up handlerevents node */
468 	if (acpi_ibm_sysctl_init(sc, ACPI_IBM_METHOD_HANDLEREVENTS)) {
469 		SYSCTL_ADD_PROC(sc->sysctl_ctx,
470 		    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
471 		    "handlerevents", CTLTYPE_STRING | CTLFLAG_RW,
472 		    sc, 0, acpi_ibm_handlerevents_sysctl, "I",
473 		    "devd(8) events handled by acpi_ibm");
474 	}
475 
476 	/* Handle notifies */
477 	AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
478 	    acpi_ibm_notify, dev);
479 
480 	/* Hook up light to led(4) */
481 	if (sc->light_set_supported)
482 		sc->led_dev = led_create_state(ibm_led, sc, "thinklight",
483 		    (sc->light_val ? 1 : 0));
484 
485 	/* Enable per-model events. */
486 	maker = kern_getenv("smbios.system.maker");
487 	product = kern_getenv("smbios.system.product");
488 	if (maker == NULL || product == NULL)
489 		goto nosmbios;
490 
491 	for (i = 0; i < nitems(acpi_ibm_models); i++) {
492 		if (strcmp(maker, acpi_ibm_models[i].maker) == 0 &&
493 		    strcmp(product, acpi_ibm_models[i].product) == 0) {
494 			ACPI_SERIAL_BEGIN(ibm);
495 			acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTMASK,
496 			    acpi_ibm_models[i].eventmask);
497 			ACPI_SERIAL_END(ibm);
498 		}
499 	}
500 
501 nosmbios:
502 	freeenv(maker);
503 	freeenv(product);
504 
505 	/* Enable events by default. */
506 	ACPI_SERIAL_BEGIN(ibm);
507 	acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTS, 1);
508 	ACPI_SERIAL_END(ibm);
509 
510 
511 	return (0);
512 }
513 
514 static int
515 acpi_ibm_detach(device_t dev)
516 {
517 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
518 
519 	struct acpi_ibm_softc *sc = device_get_softc(dev);
520 
521 	/* Disable events and restore eventmask */
522 	ACPI_SERIAL_BEGIN(ibm);
523 	acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTS, 0);
524 	acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTMASK, sc->events_initialmask);
525 	ACPI_SERIAL_END(ibm);
526 
527 	AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY, acpi_ibm_notify);
528 
529 	if (sc->led_dev != NULL)
530 		led_destroy(sc->led_dev);
531 
532 	return (0);
533 }
534 
535 static int
536 acpi_ibm_resume(device_t dev)
537 {
538 	struct acpi_ibm_softc *sc = device_get_softc(dev);
539 
540 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
541 
542 	ACPI_SERIAL_BEGIN(ibm);
543 	for (int i = 0; acpi_ibm_sysctls[i].name != NULL; i++) {
544 		int val;
545 
546 		val = acpi_ibm_sysctl_get(sc, i);
547 
548 		if (acpi_ibm_sysctls[i].flag_rdonly != 0)
549 			continue;
550 
551 		acpi_ibm_sysctl_set(sc, i, val);
552 	}
553 	ACPI_SERIAL_END(ibm);
554 
555 	return (0);
556 }
557 
558 static int
559 acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val)
560 {
561 	ACPI_OBJECT		arg[2];
562 	ACPI_OBJECT_LIST	args;
563 	ACPI_STATUS		status;
564 
565 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
566 	ACPI_SERIAL_ASSERT(ibm);
567 
568 	args.Count = 2;
569 	args.Pointer = arg;
570 	arg[0].Type = ACPI_TYPE_INTEGER;
571 	arg[1].Type = ACPI_TYPE_INTEGER;
572 
573 	for (int i = 0; i < 32; ++i) {
574 		arg[0].Integer.Value = i+1;
575 		arg[1].Integer.Value = (((1 << i) & val) != 0);
576 		status = AcpiEvaluateObject(sc->handle,
577 		    IBM_NAME_EVENTS_MASK_SET, &args, NULL);
578 
579 		if (ACPI_FAILURE(status))
580 			return (status);
581 	}
582 
583 	return (0);
584 }
585 
586 static int
587 acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS)
588 {
589 	struct acpi_ibm_softc	*sc;
590 	int			arg;
591 	int			error = 0;
592 	int			function;
593 	int			method;
594 
595 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
596 
597 	sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
598 	function = oidp->oid_arg2;
599 	method = acpi_ibm_sysctls[function].method;
600 
601 	ACPI_SERIAL_BEGIN(ibm);
602 	arg = acpi_ibm_sysctl_get(sc, method);
603 	error = sysctl_handle_int(oidp, &arg, 0, req);
604 
605 	/* Sanity check */
606 	if (error != 0 || req->newptr == NULL)
607 		goto out;
608 
609 	/* Update */
610 	error = acpi_ibm_sysctl_set(sc, method, arg);
611 
612 out:
613 	ACPI_SERIAL_END(ibm);
614 	return (error);
615 }
616 
617 static int
618 acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method)
619 {
620 	UINT64		val_ec;
621 	int 		val = 0, key;
622 
623 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
624 	ACPI_SERIAL_ASSERT(ibm);
625 
626 	switch (method) {
627 	case ACPI_IBM_METHOD_EVENTS:
628 		acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_GET, &val);
629 		break;
630 
631 	case ACPI_IBM_METHOD_EVENTMASK:
632 		if (sc->events_mask_supported)
633 			acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_MASK_GET, &val);
634 		break;
635 
636 	case ACPI_IBM_METHOD_HOTKEY:
637 		/*
638 		 * Construct the hotkey as a bitmask as illustrated below.
639 		 * Note that whenever a key was pressed, the respecting bit
640 		 * toggles and nothing else changes.
641 		 * +--+--+-+-+-+-+-+-+-+-+-+-+
642 		 * |11|10|9|8|7|6|5|4|3|2|1|0|
643 		 * +--+--+-+-+-+-+-+-+-+-+-+-+
644 		 *   |  | | | | | | | | | | |
645 		 *   |  | | | | | | | | | | +- Home Button
646 		 *   |  | | | | | | | | | +--- Search Button
647 		 *   |  | | | | | | | | +----- Mail Button
648 		 *   |  | | | | | | | +------- Thinkpad Button
649 		 *   |  | | | | | | +--------- Zoom (Fn + Space)
650 		 *   |  | | | | | +----------- WLAN Button
651 		 *   |  | | | | +------------- Video Button
652 		 *   |  | | | +--------------- Hibernate Button
653 		 *   |  | | +----------------- Thinklight Button
654 		 *   |  | +------------------- Screen expand (Fn + F8)
655 		 *   |  +--------------------- Brightness
656 		 *   +------------------------ Volume/Mute
657 		 */
658 		key = rtcin(IBM_RTC_HOTKEY1);
659 		val = (IBM_RTC_MASK_HOME | IBM_RTC_MASK_SEARCH | IBM_RTC_MASK_MAIL | IBM_RTC_MASK_WLAN) & key;
660 		key = rtcin(IBM_RTC_HOTKEY2);
661 		val |= (IBM_RTC_MASK_THINKPAD | IBM_RTC_MASK_VIDEO | IBM_RTC_MASK_HIBERNATE) & key;
662 		val |= (IBM_RTC_MASK_ZOOM & key) >> 1;
663 		key = rtcin(IBM_RTC_THINKLIGHT);
664 		val |= (IBM_RTC_MASK_THINKLIGHT & key) << 4;
665 		key = rtcin(IBM_RTC_SCREENEXPAND);
666 		val |= (IBM_RTC_MASK_THINKLIGHT & key) << 4;
667 		key = rtcin(IBM_RTC_BRIGHTNESS);
668 		val |= (IBM_RTC_MASK_BRIGHTNESS & key) << 5;
669 		key = rtcin(IBM_RTC_VOLUME);
670 		val |= (IBM_RTC_MASK_VOLUME & key) << 4;
671 		break;
672 
673 	case ACPI_IBM_METHOD_BRIGHTNESS:
674 		ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, &val_ec, 1);
675 		val = val_ec & IBM_EC_MASK_BRI;
676 		break;
677 
678 	case ACPI_IBM_METHOD_VOLUME:
679 		ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
680 		val = val_ec & IBM_EC_MASK_VOL;
681 		break;
682 
683 	case ACPI_IBM_METHOD_MUTE:
684 		ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
685 		val = ((val_ec & IBM_EC_MASK_MUTE) == IBM_EC_MASK_MUTE);
686 		break;
687 
688 	case ACPI_IBM_METHOD_THINKLIGHT:
689 		if (sc->light_get_supported)
690 			acpi_GetInteger(sc->ec_handle, IBM_NAME_KEYLIGHT, &val);
691 		else
692 			val = sc->light_val;
693 		break;
694 
695 	case ACPI_IBM_METHOD_BLUETOOTH:
696 		acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &val);
697 		sc->wlan_bt_flags = val;
698 		val = ((val & IBM_NAME_MASK_BT) != 0);
699 		break;
700 
701 	case ACPI_IBM_METHOD_WLAN:
702 		acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &val);
703 		sc->wlan_bt_flags = val;
704 		val = ((val & IBM_NAME_MASK_WLAN) != 0);
705 		break;
706 
707 	case ACPI_IBM_METHOD_FANSPEED:
708 		if (sc->fan_handle) {
709 			if(ACPI_FAILURE(acpi_GetInteger(sc->fan_handle, NULL, &val)))
710 				val = -1;
711 		}
712 		else {
713 			ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSPEED, &val_ec, 2);
714 			val = val_ec;
715 		}
716 		break;
717 
718 	case ACPI_IBM_METHOD_FANLEVEL:
719 		/*
720 		 * The IBM_EC_FANSTATUS register works as follows:
721 		 * Bit 0-5 indicate the level at which the fan operates. Only
722 		 *       values between 0 and 7 have an effect. Everything
723 		 *       above 7 is treated the same as level 7
724 		 * Bit 6 overrides the fan speed limit if set to 1
725 		 * Bit 7 indicates at which mode the fan operates:
726 		 *       manual (0) or automatic (1)
727 		 */
728 		if (!sc->fan_handle) {
729 			ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
730 			val = val_ec & IBM_EC_MASK_FANLEVEL;
731 		}
732 		break;
733 
734 	case ACPI_IBM_METHOD_FANSTATUS:
735 		if (!sc->fan_handle) {
736 			ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
737 			val = (val_ec & IBM_EC_MASK_FANSTATUS) == IBM_EC_MASK_FANSTATUS;
738 		}
739 		else
740 			val = -1;
741 		break;
742 	}
743 
744 	return (val);
745 }
746 
747 static int
748 acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int arg)
749 {
750 	int			val;
751 	UINT64			val_ec;
752 	ACPI_STATUS		status;
753 
754 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
755 	ACPI_SERIAL_ASSERT(ibm);
756 
757 	switch (method) {
758 	case ACPI_IBM_METHOD_EVENTS:
759 		if (arg < 0 || arg > 1)
760 			return (EINVAL);
761 
762 		status = acpi_SetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_SET, arg);
763 		if (ACPI_FAILURE(status))
764 			return (status);
765 		if (sc->events_mask_supported)
766 			return acpi_ibm_eventmask_set(sc, sc->events_availmask);
767 		break;
768 
769 	case ACPI_IBM_METHOD_EVENTMASK:
770 		if (sc->events_mask_supported)
771 			return acpi_ibm_eventmask_set(sc, arg);
772 		break;
773 
774 	case ACPI_IBM_METHOD_BRIGHTNESS:
775 		return acpi_ibm_brightness_set(sc, arg);
776 		break;
777 
778 	case ACPI_IBM_METHOD_VOLUME:
779 		return acpi_ibm_volume_set(sc, arg);
780 		break;
781 
782 	case ACPI_IBM_METHOD_MUTE:
783 		return acpi_ibm_mute_set(sc, arg);
784 		break;
785 
786 	case ACPI_IBM_METHOD_THINKLIGHT:
787 		return acpi_ibm_thinklight_set(sc, arg);
788 		break;
789 
790 	case ACPI_IBM_METHOD_BLUETOOTH:
791 		return acpi_ibm_bluetooth_set(sc, arg);
792 		break;
793 
794 	case ACPI_IBM_METHOD_FANLEVEL:
795 		if (arg < 0 || arg > 7)
796 			return (EINVAL);
797 
798 		if (!sc->fan_handle) {
799 			/* Read the current fanstatus */
800 			ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
801 			val = val_ec & (~IBM_EC_MASK_FANLEVEL);
802 
803 			return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_FANSTATUS, val | arg, 1);
804 		}
805 		break;
806 
807 	case ACPI_IBM_METHOD_FANSTATUS:
808 		if (arg < 0 || arg > 1)
809 			return (EINVAL);
810 
811 		if (!sc->fan_handle) {
812 			/* Read the current fanstatus */
813 			ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
814 
815 			return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_FANSTATUS,
816 				(arg == 1) ? (val_ec | IBM_EC_MASK_FANSTATUS) : (val_ec & (~IBM_EC_MASK_FANSTATUS)), 1);
817 		}
818 		break;
819 	}
820 
821 	return (0);
822 }
823 
824 static int
825 acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method)
826 {
827 	int 			dummy;
828 	ACPI_OBJECT_TYPE 	cmos_t;
829 	ACPI_HANDLE		ledb_handle;
830 
831 	switch (method) {
832 	case ACPI_IBM_METHOD_EVENTS:
833 		return (TRUE);
834 
835 	case ACPI_IBM_METHOD_EVENTMASK:
836 		return (sc->events_mask_supported);
837 
838 	case ACPI_IBM_METHOD_HOTKEY:
839 	case ACPI_IBM_METHOD_BRIGHTNESS:
840 	case ACPI_IBM_METHOD_VOLUME:
841 	case ACPI_IBM_METHOD_MUTE:
842 		/* EC is required here, which was aready checked before */
843 		return (TRUE);
844 
845 	case ACPI_IBM_METHOD_THINKLIGHT:
846 		sc->cmos_handle = NULL;
847 		sc->light_get_supported = ACPI_SUCCESS(acpi_GetInteger(
848 		    sc->ec_handle, IBM_NAME_KEYLIGHT, &sc->light_val));
849 
850 		if ((ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\UCMS", &sc->light_handle)) ||
851 		     ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMOS", &sc->light_handle)) ||
852 		     ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMS", &sc->light_handle))) &&
853 		     ACPI_SUCCESS(AcpiGetType(sc->light_handle, &cmos_t)) &&
854 		     cmos_t == ACPI_TYPE_METHOD) {
855 			sc->light_cmd_on = 0x0c;
856 			sc->light_cmd_off = 0x0d;
857 			sc->cmos_handle = sc->light_handle;
858 		}
859 		else if (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\LGHT", &sc->light_handle))) {
860 			sc->light_cmd_on = 1;
861 			sc->light_cmd_off = 0;
862 		}
863 		else
864 			sc->light_handle = NULL;
865 
866 		sc->light_set_supported = (sc->light_handle &&
867 		    ACPI_FAILURE(AcpiGetHandle(sc->ec_handle, "LEDB", &ledb_handle)));
868 
869 		if (sc->light_get_supported)
870 			return (TRUE);
871 
872 		if (sc->light_set_supported) {
873 			sc->light_val = 0;
874 			return (TRUE);
875 		}
876 
877 		return (FALSE);
878 
879 	case ACPI_IBM_METHOD_BLUETOOTH:
880 	case ACPI_IBM_METHOD_WLAN:
881 		if (ACPI_SUCCESS(acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &dummy)))
882 			return (TRUE);
883 		return (FALSE);
884 
885 	case ACPI_IBM_METHOD_FANSPEED:
886 		/*
887 		 * Some models report the fan speed in levels from 0-7
888 		 * Newer models report it contiguously
889 		 */
890 		sc->fan_levels =
891 		    (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "GFAN", &sc->fan_handle)) ||
892 		     ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\FSPD", &sc->fan_handle)));
893 		return (TRUE);
894 
895 	case ACPI_IBM_METHOD_FANLEVEL:
896 	case ACPI_IBM_METHOD_FANSTATUS:
897 		/*
898 		 * Fan status is only supported on those models,
899 		 * which report fan RPM contiguously, not in levels
900 		 */
901 		if (sc->fan_levels)
902 			return (FALSE);
903 		return (TRUE);
904 
905 	case ACPI_IBM_METHOD_THERMAL:
906 		if (ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_THERMAL_GET, &dummy))) {
907 			sc->thermal_updt_supported = ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_THERMAL_UPDT, &dummy));
908 			return (TRUE);
909 		}
910 		return (FALSE);
911 
912 	case ACPI_IBM_METHOD_HANDLEREVENTS:
913 		return (TRUE);
914 	}
915 	return (FALSE);
916 }
917 
918 static int
919 acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS)
920 {
921 	struct acpi_ibm_softc	*sc;
922 	int			error = 0;
923 	char			temp_cmd[] = "TMP0";
924 	int			temp[8];
925 
926 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
927 
928 	sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
929 
930 	ACPI_SERIAL_BEGIN(ibm);
931 
932 	for (int i = 0; i < 8; ++i) {
933 		temp_cmd[3] = '0' + i;
934 
935 		/*
936 		 * The TMPx methods seem to return +/- 128 or 0
937 		 * when the respecting sensor is not available
938 		 */
939 		if (ACPI_FAILURE(acpi_GetInteger(sc->ec_handle, temp_cmd,
940 		    &temp[i])) || ABS(temp[i]) == 128 || temp[i] == 0)
941 			temp[i] = -1;
942 		else if (sc->thermal_updt_supported)
943 			/* Temperature is reported in tenth of Kelvin */
944 			temp[i] = (temp[i] - 2732 + 5) / 10;
945 	}
946 
947 	error = sysctl_handle_opaque(oidp, &temp, 8*sizeof(int), req);
948 
949 	ACPI_SERIAL_END(ibm);
950 	return (error);
951 }
952 
953 static int
954 acpi_ibm_handlerevents_sysctl(SYSCTL_HANDLER_ARGS)
955 {
956 	struct acpi_ibm_softc	*sc;
957 	int			error = 0;
958 	struct sbuf		sb;
959 	char			*cp, *ep;
960 	int			l, val;
961 	unsigned int		handler_events;
962 	char			temp[128];
963 
964 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
965 
966 	sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
967 
968 	if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
969 		return (ENOMEM);
970 
971 	ACPI_SERIAL_BEGIN(ibm);
972 
973 	/* Get old values if this is a get request. */
974 	if (req->newptr == NULL) {
975 		for (int i = 0; i < 8 * sizeof(sc->handler_events); i++)
976 			if (sc->handler_events & (1 << i))
977 				sbuf_printf(&sb, "0x%02x ", i + 1);
978 		if (sbuf_len(&sb) == 0)
979 			sbuf_printf(&sb, "NONE");
980 	}
981 
982 	sbuf_trim(&sb);
983 	sbuf_finish(&sb);
984 	strlcpy(temp, sbuf_data(&sb), sizeof(temp));
985 	sbuf_delete(&sb);
986 
987 	error = sysctl_handle_string(oidp, temp, sizeof(temp), req);
988 
989 	/* Check for error or no change */
990 	if (error != 0 || req->newptr == NULL)
991 		goto out;
992 
993 	/* If the user is setting a string, parse it. */
994 	handler_events = 0;
995 	cp = temp;
996 	while (*cp) {
997 		if (isspace(*cp)) {
998 			cp++;
999 			continue;
1000 		}
1001 
1002 		ep = cp;
1003 
1004 		while (*ep && !isspace(*ep))
1005 			ep++;
1006 
1007 		l = ep - cp;
1008 		if (l == 0)
1009 			break;
1010 
1011 		if (strncmp(cp, "NONE", 4) == 0) {
1012 			cp = ep;
1013 			continue;
1014 		}
1015 
1016 		if (l >= 3 && cp[0] == '0' && (cp[1] == 'X' || cp[1] == 'x'))
1017 			val = strtoul(cp, &ep, 16);
1018 		else
1019 			val = strtoul(cp, &ep, 10);
1020 
1021 		if (val == 0 || ep == cp || val >= 8 * sizeof(handler_events)) {
1022 			cp[l] = '\0';
1023 			device_printf(sc->dev, "invalid event code: %s\n", cp);
1024 			error = EINVAL;
1025 			goto out;
1026 		}
1027 
1028 		handler_events |= 1 << (val - 1);
1029 
1030 		cp = ep;
1031 	}
1032 
1033 	sc->handler_events = handler_events;
1034 out:
1035 	ACPI_SERIAL_END(ibm);
1036 	return (error);
1037 }
1038 
1039 static int
1040 acpi_ibm_brightness_set(struct acpi_ibm_softc *sc, int arg)
1041 {
1042 	int			val, step;
1043 	UINT64			val_ec;
1044 	ACPI_OBJECT		Arg;
1045 	ACPI_OBJECT_LIST	Args;
1046 	ACPI_STATUS		status;
1047 
1048 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1049 	ACPI_SERIAL_ASSERT(ibm);
1050 
1051 	if (arg < 0 || arg > 7)
1052 		return (EINVAL);
1053 
1054 	/* Read the current brightness */
1055 	status = ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, &val_ec, 1);
1056 	if (ACPI_FAILURE(status))
1057 		return (status);
1058 
1059 	if (sc->cmos_handle) {
1060 		val = val_ec & IBM_EC_MASK_BRI;
1061 
1062 		Args.Count = 1;
1063 		Args.Pointer = &Arg;
1064 		Arg.Type = ACPI_TYPE_INTEGER;
1065 		Arg.Integer.Value = (arg > val) ? IBM_CMOS_BRIGHTNESS_UP :
1066 						  IBM_CMOS_BRIGHTNESS_DOWN;
1067 
1068 		step = (arg > val) ? 1 : -1;
1069 		for (int i = val; i != arg; i += step) {
1070 			status = AcpiEvaluateObject(sc->cmos_handle, NULL,
1071 						    &Args, NULL);
1072 			if (ACPI_FAILURE(status)) {
1073 				/* Record the last value */
1074 				if (i != val) {
1075 					ACPI_EC_WRITE(sc->ec_dev,
1076 					    IBM_EC_BRIGHTNESS, i - step, 1);
1077 				}
1078 				return (status);
1079 			}
1080 		}
1081 	}
1082 
1083 	return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_BRIGHTNESS, arg, 1);
1084 }
1085 
1086 static int
1087 acpi_ibm_bluetooth_set(struct acpi_ibm_softc *sc, int arg)
1088 {
1089 	int			val;
1090 
1091 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1092 	ACPI_SERIAL_ASSERT(ibm);
1093 
1094 	if (arg < 0 || arg > 1)
1095 		return (EINVAL);
1096 
1097 	val = (arg == 1) ? sc->wlan_bt_flags | IBM_NAME_MASK_BT :
1098 			   sc->wlan_bt_flags & (~IBM_NAME_MASK_BT);
1099 	return acpi_SetInteger(sc->handle, IBM_NAME_WLAN_BT_SET, val);
1100 }
1101 
1102 static int
1103 acpi_ibm_thinklight_set(struct acpi_ibm_softc *sc, int arg)
1104 {
1105 	ACPI_OBJECT		Arg;
1106 	ACPI_OBJECT_LIST	Args;
1107 	ACPI_STATUS		status;
1108 
1109 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1110 	ACPI_SERIAL_ASSERT(ibm);
1111 
1112 	if (arg < 0 || arg > 1)
1113 		return (EINVAL);
1114 
1115 	if (sc->light_set_supported) {
1116 		Args.Count = 1;
1117 		Args.Pointer = &Arg;
1118 		Arg.Type = ACPI_TYPE_INTEGER;
1119 		Arg.Integer.Value = arg ? sc->light_cmd_on : sc->light_cmd_off;
1120 
1121 		status = AcpiEvaluateObject(sc->light_handle, NULL,
1122 					    &Args, NULL);
1123 		if (ACPI_SUCCESS(status))
1124 			sc->light_val = arg;
1125 		return (status);
1126 	}
1127 
1128 	return (0);
1129 }
1130 
1131 static int
1132 acpi_ibm_volume_set(struct acpi_ibm_softc *sc, int arg)
1133 {
1134 	int			val, step;
1135 	UINT64			val_ec;
1136 	ACPI_OBJECT		Arg;
1137 	ACPI_OBJECT_LIST	Args;
1138 	ACPI_STATUS		status;
1139 
1140 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1141 	ACPI_SERIAL_ASSERT(ibm);
1142 
1143 	if (arg < 0 || arg > 14)
1144 		return (EINVAL);
1145 
1146 	/* Read the current volume */
1147 	status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
1148 	if (ACPI_FAILURE(status))
1149 		return (status);
1150 
1151 	if (sc->cmos_handle) {
1152 		val = val_ec & IBM_EC_MASK_VOL;
1153 
1154 		Args.Count = 1;
1155 		Args.Pointer = &Arg;
1156 		Arg.Type = ACPI_TYPE_INTEGER;
1157 		Arg.Integer.Value = (arg > val) ? IBM_CMOS_VOLUME_UP :
1158 						  IBM_CMOS_VOLUME_DOWN;
1159 
1160 		step = (arg > val) ? 1 : -1;
1161 		for (int i = val; i != arg; i += step) {
1162 			status = AcpiEvaluateObject(sc->cmos_handle, NULL,
1163 						    &Args, NULL);
1164 			if (ACPI_FAILURE(status)) {
1165 				/* Record the last value */
1166 				if (i != val) {
1167 					val_ec = i - step +
1168 						 (val_ec & (~IBM_EC_MASK_VOL));
1169 					ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME,
1170 						      val_ec, 1);
1171 				}
1172 				return (status);
1173 			}
1174 		}
1175 	}
1176 
1177 	val_ec = arg + (val_ec & (~IBM_EC_MASK_VOL));
1178 	return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME, val_ec, 1);
1179 }
1180 
1181 static int
1182 acpi_ibm_mute_set(struct acpi_ibm_softc *sc, int arg)
1183 {
1184 	UINT64			val_ec;
1185 	ACPI_OBJECT		Arg;
1186 	ACPI_OBJECT_LIST	Args;
1187 	ACPI_STATUS		status;
1188 
1189 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1190 	ACPI_SERIAL_ASSERT(ibm);
1191 
1192 	if (arg < 0 || arg > 1)
1193 		return (EINVAL);
1194 
1195 	status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
1196 	if (ACPI_FAILURE(status))
1197 		return (status);
1198 
1199 	if (sc->cmos_handle) {
1200 		Args.Count = 1;
1201 		Args.Pointer = &Arg;
1202 		Arg.Type = ACPI_TYPE_INTEGER;
1203 		Arg.Integer.Value = IBM_CMOS_VOLUME_MUTE;
1204 
1205 		status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
1206 		if (ACPI_FAILURE(status))
1207 			return (status);
1208 	}
1209 
1210 	val_ec = (arg == 1) ? val_ec | IBM_EC_MASK_MUTE :
1211 			      val_ec & (~IBM_EC_MASK_MUTE);
1212 	return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME, val_ec, 1);
1213 }
1214 
1215 static void
1216 acpi_ibm_eventhandler(struct acpi_ibm_softc *sc, int arg)
1217 {
1218 	int			val;
1219 	UINT64			val_ec;
1220 	ACPI_STATUS		status;
1221 
1222 	ACPI_SERIAL_BEGIN(ibm);
1223 	switch (arg) {
1224 	case IBM_EVENT_SUSPEND_TO_RAM:
1225 		power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
1226 		break;
1227 
1228 	case IBM_EVENT_BLUETOOTH:
1229 		acpi_ibm_bluetooth_set(sc, (sc->wlan_bt_flags == 0));
1230 		break;
1231 
1232 	case IBM_EVENT_BRIGHTNESS_UP:
1233 	case IBM_EVENT_BRIGHTNESS_DOWN:
1234 		/* Read the current brightness */
1235 		status = ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS,
1236 				      &val_ec, 1);
1237 		if (ACPI_FAILURE(status))
1238 			return;
1239 
1240 		val = val_ec & IBM_EC_MASK_BRI;
1241 		val = (arg == IBM_EVENT_BRIGHTNESS_UP) ? val + 1 : val - 1;
1242 		acpi_ibm_brightness_set(sc, val);
1243 		break;
1244 
1245 	case IBM_EVENT_THINKLIGHT:
1246 		acpi_ibm_thinklight_set(sc, (sc->light_val == 0));
1247 		break;
1248 
1249 	case IBM_EVENT_VOLUME_UP:
1250 	case IBM_EVENT_VOLUME_DOWN:
1251 		/* Read the current volume */
1252 		status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
1253 		if (ACPI_FAILURE(status))
1254 			return;
1255 
1256 		val = val_ec & IBM_EC_MASK_VOL;
1257 		val = (arg == IBM_EVENT_VOLUME_UP) ? val + 1 : val - 1;
1258 		acpi_ibm_volume_set(sc, val);
1259 		break;
1260 
1261 	case IBM_EVENT_MUTE:
1262 		/* Read the current value */
1263 		status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
1264 		if (ACPI_FAILURE(status))
1265 			return;
1266 
1267 		val = ((val_ec & IBM_EC_MASK_MUTE) == IBM_EC_MASK_MUTE);
1268 		acpi_ibm_mute_set(sc, (val == 0));
1269 		break;
1270 
1271 	default:
1272 		break;
1273 	}
1274 	ACPI_SERIAL_END(ibm);
1275 }
1276 
1277 static void
1278 acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context)
1279 {
1280 	int		event, arg, type;
1281 	device_t	dev = context;
1282 	struct acpi_ibm_softc *sc = device_get_softc(dev);
1283 
1284 	ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
1285 
1286 	if (notify != 0x80)
1287 		device_printf(dev, "Unknown notify\n");
1288 
1289 	for (;;) {
1290 		acpi_GetInteger(acpi_get_handle(dev), IBM_NAME_EVENTS_GET, &event);
1291 		if (event == 0)
1292 			break;
1293 
1294 
1295 		type = (event >> 12) & 0xf;
1296 		arg = event & 0xfff;
1297 		switch (type) {
1298 		case 1:
1299 			if (!(sc->events_availmask & (1 << (arg - 1)))) {
1300 				device_printf(dev, "Unknown key %d\n", arg);
1301 				break;
1302 			}
1303 
1304 			/* Execute event handler */
1305 			if (sc->handler_events & (1 << (arg - 1)))
1306 				acpi_ibm_eventhandler(sc, (arg & 0xff));
1307 
1308 			/* Notify devd(8) */
1309 			acpi_UserNotify("IBM", h, (arg & 0xff));
1310 			break;
1311 		default:
1312 			break;
1313 		}
1314 	}
1315 }
1316