2 * Copyright (c) 2004 Takanori Watanabe
3 * Copyright (c) 2005 Markus Brueffer <markus@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
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
36 * acpi-ibm: <http://ibm-acpi.sourceforge.net/>
37 * tpb: <http://www.nongnu.org/tpb/>
41 #include <sys/param.h>
42 #include <sys/kernel.h>
44 #include <machine/cpufunc.h>
45 #include <contrib/dev/acpica/acpi.h>
47 #include <sys/module.h>
48 #include <dev/acpica/acpivar.h>
49 #include <dev/led/led.h>
50 #include <sys/sysctl.h>
51 #include <machine/clock.h>
53 #define _COMPONENT ACPI_OEM
54 ACPI_MODULE_NAME("IBM")
56 /* Internal methods */
57 #define ACPI_IBM_METHOD_EVENTS 1
58 #define ACPI_IBM_METHOD_EVENTMASK 2
59 #define ACPI_IBM_METHOD_HOTKEY 3
60 #define ACPI_IBM_METHOD_BRIGHTNESS 4
61 #define ACPI_IBM_METHOD_VOLUME 5
62 #define ACPI_IBM_METHOD_MUTE 6
63 #define ACPI_IBM_METHOD_THINKLIGHT 7
64 #define ACPI_IBM_METHOD_BLUETOOTH 8
65 #define ACPI_IBM_METHOD_WLAN 9
66 #define ACPI_IBM_METHOD_FANSPEED 10
67 #define ACPI_IBM_METHOD_FANLEVEL 11
68 #define ACPI_IBM_METHOD_FANSTATUS 12
69 #define ACPI_IBM_METHOD_THERMAL 13
72 #define IBM_RTC_HOTKEY1 0x64
73 #define IBM_RTC_MASK_HOME (1 << 0)
74 #define IBM_RTC_MASK_SEARCH (1 << 1)
75 #define IBM_RTC_MASK_MAIL (1 << 2)
76 #define IBM_RTC_MASK_WLAN (1 << 5)
77 #define IBM_RTC_HOTKEY2 0x65
78 #define IBM_RTC_MASK_THINKPAD (1 << 3)
79 #define IBM_RTC_MASK_ZOOM (1 << 5)
80 #define IBM_RTC_MASK_VIDEO (1 << 6)
81 #define IBM_RTC_MASK_HIBERNATE (1 << 7)
82 #define IBM_RTC_THINKLIGHT 0x66
83 #define IBM_RTC_MASK_THINKLIGHT (1 << 4)
84 #define IBM_RTC_SCREENEXPAND 0x67
85 #define IBM_RTC_MASK_SCREENEXPAND (1 << 5)
86 #define IBM_RTC_BRIGHTNESS 0x6c
87 #define IBM_RTC_MASK_BRIGHTNESS (1 << 5)
88 #define IBM_RTC_VOLUME 0x6e
89 #define IBM_RTC_MASK_VOLUME (1 << 7)
91 /* Embedded Controller registers */
92 #define IBM_EC_BRIGHTNESS 0x31
93 #define IBM_EC_MASK_BRI 0x7
94 #define IBM_EC_VOLUME 0x30
95 #define IBM_EC_MASK_VOL 0xf
96 #define IBM_EC_MASK_MUTE (1 << 6)
97 #define IBM_EC_FANSTATUS 0x2F
98 #define IBM_EC_MASK_FANLEVEL 0x3f
99 #define IBM_EC_MASK_FANDISENGAGED (1 << 6)
100 #define IBM_EC_MASK_FANSTATUS (1 << 7)
101 #define IBM_EC_FANSPEED 0x84
104 #define IBM_CMOS_VOLUME_DOWN 0
105 #define IBM_CMOS_VOLUME_UP 1
106 #define IBM_CMOS_VOLUME_MUTE 2
107 #define IBM_CMOS_BRIGHTNESS_UP 4
108 #define IBM_CMOS_BRIGHTNESS_DOWN 5
111 #define IBM_NAME_KEYLIGHT "KBLT"
112 #define IBM_NAME_WLAN_BT_GET "GBDC"
113 #define IBM_NAME_WLAN_BT_SET "SBDC"
114 #define IBM_NAME_MASK_BT (1 << 1)
115 #define IBM_NAME_MASK_WLAN (1 << 2)
116 #define IBM_NAME_THERMAL_GET "TMP7"
117 #define IBM_NAME_THERMAL_UPDT "UPDT"
119 #define IBM_NAME_EVENTS_STATUS_GET "DHKC"
120 #define IBM_NAME_EVENTS_MASK_GET "DHKN"
121 #define IBM_NAME_EVENTS_STATUS_SET "MHKC"
122 #define IBM_NAME_EVENTS_MASK_SET "MHKM"
123 #define IBM_NAME_EVENTS_GET "MHKP"
124 #define IBM_NAME_EVENTS_AVAILMASK "MHKA"
126 #define ABS(x) (((x) < 0)? -(x) : (x))
128 struct acpi_ibm_softc {
132 /* Embedded controller */
134 ACPI_HANDLE ec_handle;
137 ACPI_HANDLE cmos_handle;
140 ACPI_HANDLE fan_handle;
143 /* Keylight commands and states */
144 ACPI_HANDLE light_handle;
148 int light_get_supported;
149 int light_set_supported;
151 /* led(4) interface */
152 struct cdev *led_dev;
157 int thermal_updt_supported;
159 unsigned int events_availmask;
160 unsigned int events_initialmask;
161 int events_mask_supported;
164 struct sysctl_ctx_list *sysctl_ctx;
165 struct sysctl_oid *sysctl_tree;
173 } acpi_ibm_sysctls[] = {
176 .method = ACPI_IBM_METHOD_EVENTS,
177 .description = "ACPI events enable",
178 .access = CTLTYPE_INT | CTLFLAG_RW
182 .method = ACPI_IBM_METHOD_EVENTMASK,
183 .description = "ACPI eventmask",
184 .access = CTLTYPE_INT | CTLFLAG_RW
188 .method = ACPI_IBM_METHOD_HOTKEY,
189 .description = "Key Status",
190 .access = CTLTYPE_INT | CTLFLAG_RD
193 .name = "lcd_brightness",
194 .method = ACPI_IBM_METHOD_BRIGHTNESS,
195 .description = "LCD Brightness",
196 .access = CTLTYPE_INT | CTLFLAG_RW
200 .method = ACPI_IBM_METHOD_VOLUME,
201 .description = "Volume",
202 .access = CTLTYPE_INT | CTLFLAG_RW
206 .method = ACPI_IBM_METHOD_MUTE,
207 .description = "Mute",
208 .access = CTLTYPE_INT | CTLFLAG_RW
211 .name = "thinklight",
212 .method = ACPI_IBM_METHOD_THINKLIGHT,
213 .description = "Thinklight enable",
214 .access = CTLTYPE_INT | CTLFLAG_RW
218 .method = ACPI_IBM_METHOD_BLUETOOTH,
219 .description = "Bluetooth enable",
220 .access = CTLTYPE_INT | CTLFLAG_RW
224 .method = ACPI_IBM_METHOD_WLAN,
225 .description = "WLAN enable",
226 .access = CTLTYPE_INT | CTLFLAG_RD
230 .method = ACPI_IBM_METHOD_FANSPEED,
231 .description = "Fan speed",
232 .access = CTLTYPE_INT | CTLFLAG_RD
236 .method = ACPI_IBM_METHOD_FANLEVEL,
237 .description = "Fan level",
238 .access = CTLTYPE_INT | CTLFLAG_RW
242 .method = ACPI_IBM_METHOD_FANSTATUS,
243 .description = "Fan enable",
244 .access = CTLTYPE_INT | CTLFLAG_RW
250 ACPI_SERIAL_DECL(ibm, "ACPI IBM extras");
252 static int acpi_ibm_probe(device_t dev);
253 static int acpi_ibm_attach(device_t dev);
254 static int acpi_ibm_detach(device_t dev);
256 static void ibm_led(void *softc, int onoff);
257 static void ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused);
259 static int acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS);
260 static int acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method);
261 static int acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method);
262 static int acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int val);
264 static int acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val);
265 static int acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS);
266 static void acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context);
268 static device_method_t acpi_ibm_methods[] = {
269 /* Device interface */
270 DEVMETHOD(device_probe, acpi_ibm_probe),
271 DEVMETHOD(device_attach, acpi_ibm_attach),
272 DEVMETHOD(device_detach, acpi_ibm_detach),
277 static driver_t acpi_ibm_driver = {
280 sizeof(struct acpi_ibm_softc),
283 static devclass_t acpi_ibm_devclass;
285 DRIVER_MODULE(acpi_ibm, acpi, acpi_ibm_driver, acpi_ibm_devclass,
287 MODULE_DEPEND(acpi_ibm, acpi, 1, 1, 1);
288 static char *ibm_ids[] = {"IBM0057", "IBM0068", NULL};
291 ibm_led(void *softc, int onoff)
293 struct acpi_ibm_softc* sc = (struct acpi_ibm_softc*) softc;
295 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
301 sc->led_state = onoff;
303 AcpiOsQueueForExecution(OSD_PRIORITY_LO,
304 (void *)ibm_led_task, sc);
308 ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused)
310 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
312 ACPI_SERIAL_BEGIN(ibm);
313 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_THINKLIGHT, sc->led_state);
314 ACPI_SERIAL_END(ibm);
320 acpi_ibm_probe(device_t dev)
322 if (acpi_disabled("ibm") ||
323 ACPI_ID_PROBE(device_get_parent(dev), dev, ibm_ids) == NULL ||
324 device_get_unit(dev) != 0)
327 device_set_desc(dev, "IBM ThinkPad ACPI Extras");
333 acpi_ibm_attach(device_t dev)
335 struct acpi_ibm_softc *sc;
336 struct acpi_softc *acpi_sc;
337 devclass_t ec_devclass;
339 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
341 sc = device_get_softc(dev);
343 sc->handle = acpi_get_handle(dev);
345 acpi_sc = acpi_device_get_parent_softc(dev);
347 /* Look for the first embedded controller */
348 if (!(ec_devclass = devclass_find ("acpi_ec"))) {
350 device_printf(dev, "Couldn't find acpi_ec devclass\n");
353 if (!(sc->ec_dev = devclass_get_device(ec_devclass, 0))) {
355 device_printf(dev, "Couldn't find acpi_ec device\n");
358 sc->ec_handle = acpi_get_handle(sc->ec_dev);
360 ACPI_SERIAL_BEGIN(ibm);
362 /* Get the sysctl tree */
363 sc->sysctl_ctx = device_get_sysctl_ctx(dev);
364 sc->sysctl_tree = device_get_sysctl_tree(dev);
366 /* Look for event mask and hook up the nodes */
367 sc->events_mask_supported = ACPI_SUCCESS(acpi_GetInteger(sc->handle,
368 IBM_NAME_EVENTS_MASK_GET, &sc->events_initialmask));
370 if (sc->events_mask_supported) {
371 SYSCTL_ADD_INT(sc->sysctl_ctx,
372 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
373 "initialmask", CTLFLAG_RD,
374 &sc->events_initialmask, 0, "Initial eventmask");
376 /* The availmask is the bitmask of supported events */
377 if (ACPI_FAILURE(acpi_GetInteger(sc->handle,
378 IBM_NAME_EVENTS_AVAILMASK, &sc->events_availmask)))
379 sc->events_availmask = 0xffffffff;
381 SYSCTL_ADD_INT(sc->sysctl_ctx,
382 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
383 "availmask", CTLFLAG_RD,
384 &sc->events_availmask, 0, "Mask of supported events");
387 /* Hook up proc nodes */
388 for (int i = 0; acpi_ibm_sysctls[i].name != NULL; i++) {
389 if (!acpi_ibm_sysctl_init(sc, acpi_ibm_sysctls[i].method))
392 SYSCTL_ADD_PROC(sc->sysctl_ctx,
393 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
394 acpi_ibm_sysctls[i].name, acpi_ibm_sysctls[i].access,
395 sc, i, acpi_ibm_sysctl, "I",
396 acpi_ibm_sysctls[i].description);
399 /* Hook up thermal node */
400 if (acpi_ibm_sysctl_init(sc, ACPI_IBM_METHOD_THERMAL)) {
401 SYSCTL_ADD_PROC(sc->sysctl_ctx,
402 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
403 "thermal", CTLTYPE_STRING | CTLFLAG_RD,
404 sc, 0, acpi_ibm_thermal_sysctl, "I",
408 ACPI_SERIAL_END(ibm);
410 /* Handle notifies */
411 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
412 acpi_ibm_notify, dev);
414 /* Hook up light to led(4) */
415 if (sc->light_set_supported)
416 sc->led_dev = led_create(ibm_led, sc, "thinklight");
422 acpi_ibm_detach(device_t dev)
424 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
426 struct acpi_ibm_softc *sc = device_get_softc(dev);
428 /* Disable events and restore eventmask */
429 ACPI_SERIAL_BEGIN(ibm);
430 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTS, 0);
431 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTMASK, sc->events_initialmask);
432 ACPI_SERIAL_END(ibm);
434 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY, acpi_ibm_notify);
436 if (sc->led_dev != NULL)
437 led_destroy(sc->led_dev);
443 acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val)
446 ACPI_OBJECT_LIST args;
449 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
450 ACPI_SERIAL_ASSERT(ibm);
454 arg[0].Type = ACPI_TYPE_INTEGER;
455 arg[1].Type = ACPI_TYPE_INTEGER;
457 for (int i = 0; i < 32; ++i) {
458 arg[0].Integer.Value = i+1;
459 arg[1].Integer.Value = (((1 << i) & val) != 0);
460 status = AcpiEvaluateObject(sc->handle,
461 IBM_NAME_EVENTS_MASK_SET, &args, NULL);
463 if (ACPI_FAILURE(status))
471 acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS)
473 struct acpi_ibm_softc *sc;
479 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
481 sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
482 function = oidp->oid_arg2;
483 method = acpi_ibm_sysctls[function].method;
485 ACPI_SERIAL_BEGIN(ibm);
486 arg = acpi_ibm_sysctl_get(sc, method);
487 error = sysctl_handle_int(oidp, &arg, 0, req);
490 if (error != 0 || req->newptr == NULL)
494 error = acpi_ibm_sysctl_set(sc, method, arg);
497 ACPI_SERIAL_END(ibm);
502 acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method)
507 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
508 ACPI_SERIAL_ASSERT(ibm);
511 case ACPI_IBM_METHOD_EVENTS:
512 acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_GET, &val);
515 case ACPI_IBM_METHOD_EVENTMASK:
516 if (sc->events_mask_supported)
517 acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_MASK_GET, &val);
520 case ACPI_IBM_METHOD_HOTKEY:
522 * Construct the hotkey as a bitmask as illustrated below.
523 * Note that whenever a key was pressed, the respecting bit
524 * toggles and nothing else changes.
525 * +--+--+-+-+-+-+-+-+-+-+-+-+
526 * |11|10|9|8|7|6|5|4|3|2|1|0|
527 * +--+--+-+-+-+-+-+-+-+-+-+-+
528 * | | | | | | | | | | | |
529 * | | | | | | | | | | | +- Home Button
530 * | | | | | | | | | | +--- Search Button
531 * | | | | | | | | | +----- Mail Button
532 * | | | | | | | | +------- Thinkpad Button
533 * | | | | | | | +--------- Zoom (Fn + Space)
534 * | | | | | | +----------- WLAN Button
535 * | | | | | +------------- Video Button
536 * | | | | +--------------- Hibernate Button
537 * | | | +----------------- Thinklight Button
538 * | | +------------------- Screen expand (Fn + F8)
539 * | +--------------------- Brightness
540 * +------------------------ Volume/Mute
542 key = rtcin(IBM_RTC_HOTKEY1);
543 val = (IBM_RTC_MASK_HOME | IBM_RTC_MASK_SEARCH | IBM_RTC_MASK_MAIL | IBM_RTC_MASK_WLAN) & key;
544 key = rtcin(IBM_RTC_HOTKEY2);
545 val |= (IBM_RTC_MASK_THINKPAD | IBM_RTC_MASK_VIDEO | IBM_RTC_MASK_HIBERNATE) & key;
546 val |= (IBM_RTC_MASK_ZOOM & key) >> 1;
547 key = rtcin(IBM_RTC_THINKLIGHT);
548 val |= (IBM_RTC_MASK_THINKLIGHT & key) << 4;
549 key = rtcin(IBM_RTC_SCREENEXPAND);
550 val |= (IBM_RTC_MASK_THINKLIGHT & key) << 4;
551 key = rtcin(IBM_RTC_BRIGHTNESS);
552 val |= (IBM_RTC_MASK_BRIGHTNESS & key) << 5;
553 key = rtcin(IBM_RTC_VOLUME);
554 val |= (IBM_RTC_MASK_VOLUME & key) << 4;
557 case ACPI_IBM_METHOD_BRIGHTNESS:
558 ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, &val_ec, 1);
559 val = val_ec & IBM_EC_MASK_BRI;
562 case ACPI_IBM_METHOD_VOLUME:
563 ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
564 val = val_ec & IBM_EC_MASK_VOL;
567 case ACPI_IBM_METHOD_MUTE:
568 ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
569 val = ((val_ec & IBM_EC_MASK_MUTE) == IBM_EC_MASK_MUTE);
572 case ACPI_IBM_METHOD_THINKLIGHT:
573 if (sc->light_get_supported)
574 acpi_GetInteger(sc->ec_handle, IBM_NAME_KEYLIGHT, &val);
579 case ACPI_IBM_METHOD_BLUETOOTH:
580 acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &val);
581 sc->wlan_bt_flags = val;
582 val = ((val & IBM_NAME_MASK_BT) != 0);
585 case ACPI_IBM_METHOD_WLAN:
586 acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &val);
587 sc->wlan_bt_flags = val;
588 val = ((val & IBM_NAME_MASK_WLAN) != 0);
591 case ACPI_IBM_METHOD_FANSPEED:
592 if (sc->fan_handle) {
593 if(ACPI_FAILURE(acpi_GetInteger(sc->fan_handle, NULL, &val)))
597 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSPEED, &val_ec, 2);
602 case ACPI_IBM_METHOD_FANLEVEL:
604 * The IBM_EC_FANSTATUS register works as follows:
605 * Bit 0-5 indicate the level at which the fan operates. Only
606 * values between 0 and 7 have an effect. Everything
607 * above 7 is treated the same as level 7
608 * Bit 6 overrides the fan speed limit if set to 1
609 * Bit 7 indicates at which mode the fan operates:
610 * manual (0) or automatic (1)
612 if (!sc->fan_handle) {
613 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
614 val = val_ec & IBM_EC_MASK_FANLEVEL;
618 case ACPI_IBM_METHOD_FANSTATUS:
619 if (!sc->fan_handle) {
620 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
621 val = (val_ec & IBM_EC_MASK_FANSTATUS) == IBM_EC_MASK_FANSTATUS;
632 acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int arg)
637 ACPI_OBJECT_LIST Args;
640 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
641 ACPI_SERIAL_ASSERT(ibm);
644 case ACPI_IBM_METHOD_EVENTS:
645 if (arg < 0 || arg > 1)
648 status = acpi_SetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_SET, arg);
649 if (ACPI_FAILURE(status))
651 if (sc->events_mask_supported)
652 return acpi_ibm_eventmask_set(sc, sc->events_availmask);
655 case ACPI_IBM_METHOD_EVENTMASK:
656 if (sc->events_mask_supported)
657 return acpi_ibm_eventmask_set(sc, arg);
660 case ACPI_IBM_METHOD_BRIGHTNESS:
661 if (arg < 0 || arg > 7)
664 if (sc->cmos_handle) {
665 /* Read the current brightness */
666 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, &val_ec, 1);
667 if (ACPI_FAILURE(status))
669 val = val_ec & IBM_EC_MASK_BRI;
673 Arg.Type = ACPI_TYPE_INTEGER;
674 Arg.Integer.Value = (arg > val) ? IBM_CMOS_BRIGHTNESS_UP : IBM_CMOS_BRIGHTNESS_DOWN;
676 step = (arg > val) ? 1 : -1;
677 for (int i = val; i != arg; i += step) {
678 status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
679 if (ACPI_FAILURE(status))
683 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_BRIGHTNESS, arg, 1);
686 case ACPI_IBM_METHOD_VOLUME:
687 if (arg < 0 || arg > 14)
690 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
691 if (ACPI_FAILURE(status))
694 if (sc->cmos_handle) {
695 val = val_ec & IBM_EC_MASK_VOL;
699 Arg.Type = ACPI_TYPE_INTEGER;
700 Arg.Integer.Value = (arg > val) ? IBM_CMOS_VOLUME_UP : IBM_CMOS_VOLUME_DOWN;
702 step = (arg > val) ? 1 : -1;
703 for (int i = val; i != arg; i += step) {
704 status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
705 if (ACPI_FAILURE(status))
709 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME, arg + (val_ec & (~IBM_EC_MASK_VOL)), 1);
712 case ACPI_IBM_METHOD_MUTE:
713 if (arg < 0 || arg > 1)
716 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
717 if (ACPI_FAILURE(status))
720 if (sc->cmos_handle) {
721 val = val_ec & IBM_EC_MASK_VOL;
725 Arg.Type = ACPI_TYPE_INTEGER;
726 Arg.Integer.Value = IBM_CMOS_VOLUME_MUTE;
728 status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
729 if (ACPI_FAILURE(status))
732 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME, (arg==1) ? val_ec | IBM_EC_MASK_MUTE : val_ec & (~IBM_EC_MASK_MUTE), 1);
735 case ACPI_IBM_METHOD_THINKLIGHT:
736 if (arg < 0 || arg > 1)
739 if (sc->light_set_supported) {
742 Arg.Type = ACPI_TYPE_INTEGER;
743 Arg.Integer.Value = arg ? sc->light_cmd_on : sc->light_cmd_off;
745 status = AcpiEvaluateObject(sc->light_handle, NULL, &Args, NULL);
746 if (ACPI_SUCCESS(status))
752 case ACPI_IBM_METHOD_BLUETOOTH:
753 if (arg < 0 || arg > 1)
756 val = (arg == 1) ? sc->wlan_bt_flags | IBM_NAME_MASK_BT : sc->wlan_bt_flags & (~IBM_NAME_MASK_BT);
757 return acpi_SetInteger(sc->handle, IBM_NAME_WLAN_BT_SET, val);
760 case ACPI_IBM_METHOD_FANLEVEL:
761 if (arg < 0 || arg > 7)
764 if (!sc->fan_handle) {
765 /* Read the current fanstatus */
766 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
767 val = val_ec & (~IBM_EC_MASK_FANLEVEL);
769 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_FANSTATUS, val | arg, 1);
773 case ACPI_IBM_METHOD_FANSTATUS:
774 if (arg < 0 || arg > 1)
777 if (!sc->fan_handle) {
778 /* Read the current fanstatus */
779 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
781 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_FANSTATUS,
782 (arg == 1) ? (val_ec | IBM_EC_MASK_FANSTATUS) : (val_ec & (~IBM_EC_MASK_FANSTATUS)), 1);
791 acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method)
794 ACPI_OBJECT_TYPE cmos_t;
795 ACPI_HANDLE ledb_handle;
798 case ACPI_IBM_METHOD_EVENTS:
799 /* Events are disabled by default */
802 case ACPI_IBM_METHOD_EVENTMASK:
803 return (sc->events_mask_supported);
805 case ACPI_IBM_METHOD_HOTKEY:
806 case ACPI_IBM_METHOD_BRIGHTNESS:
807 case ACPI_IBM_METHOD_VOLUME:
808 case ACPI_IBM_METHOD_MUTE:
809 /* EC is required here, which was aready checked before */
812 case ACPI_IBM_METHOD_THINKLIGHT:
813 sc->cmos_handle = NULL;
814 sc->light_get_supported = ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_KEYLIGHT, &dummy));
816 if ((ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\UCMS", &sc->light_handle)) ||
817 ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMOS", &sc->light_handle)) ||
818 ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMS", &sc->light_handle))) &&
819 ACPI_SUCCESS(AcpiGetType(sc->light_handle, &cmos_t)) &&
820 cmos_t == ACPI_TYPE_METHOD) {
821 sc->light_cmd_on = 0x0c;
822 sc->light_cmd_off = 0x0d;
823 sc->cmos_handle = sc->light_handle;
825 else if (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\LGHT", &sc->light_handle))) {
826 sc->light_cmd_on = 1;
827 sc->light_cmd_off = 0;
830 sc->light_handle = NULL;
832 sc->light_set_supported = (sc->light_handle &&
833 ACPI_FAILURE(AcpiGetHandle(sc->ec_handle, "LEDB", &ledb_handle)));
835 if (sc->light_get_supported || sc->light_set_supported) {
842 case ACPI_IBM_METHOD_BLUETOOTH:
843 case ACPI_IBM_METHOD_WLAN:
844 if (ACPI_SUCCESS(acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &dummy)))
848 case ACPI_IBM_METHOD_FANSPEED:
850 * Some models report the fan speed in levels from 0-7
851 * Newer models report it contiguously
854 (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "GFAN", &sc->fan_handle)) ||
855 ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\FSPD", &sc->fan_handle)));
858 case ACPI_IBM_METHOD_FANLEVEL:
859 case ACPI_IBM_METHOD_FANSTATUS:
861 * Fan status is only supported on those models,
862 * which report fan RPM contiguously, not in levels
868 case ACPI_IBM_METHOD_THERMAL:
869 if (ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_THERMAL_GET, &dummy))) {
870 sc->thermal_updt_supported = ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_THERMAL_UPDT, &dummy));
879 acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS)
881 struct acpi_ibm_softc *sc;
883 char temp_cmd[] = "TMP0";
886 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
888 sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
890 ACPI_SERIAL_BEGIN(ibm);
892 for (int i = 0; i < 8; ++i) {
893 temp_cmd[3] = '0' + i;
896 * The TMPx methods seem to return +/- 128 or 0
897 * when the respecting sensor is not available
899 if (ACPI_FAILURE(acpi_GetInteger(sc->ec_handle, temp_cmd,
900 &temp[i])) || ABS(temp[i]) == 128 || temp[i] == 0)
902 else if (sc->thermal_updt_supported)
903 /* Temperature is reported in tenth of Kelvin */
904 temp[i] = (temp[i] - 2732 + 5) / 10;
907 error = sysctl_handle_opaque(oidp, &temp, 8*sizeof(int), req);
909 ACPI_SERIAL_END(ibm);
914 acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context)
916 int event, arg, type;
917 device_t dev = context;
918 struct acpi_ibm_softc *sc = device_get_softc(dev);
920 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
922 printf("IBM:NOTIFY:%x\n", notify);
924 printf("Unknown notify\n");
927 acpi_GetInteger(acpi_get_handle(dev), IBM_NAME_EVENTS_GET, &event);
932 printf("notify:%x\n", event);
934 type = (event >> 12) & 0xf;
938 if (!(sc->events_availmask & (1 << (arg - 1)))) {
939 printf("Unknown key %d\n", arg);
944 acpi_UserNotify("IBM", h, (arg & 0xff));