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>
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
50 #include <sys/module.h>
51 #include <dev/acpica/acpivar.h>
52 #include <dev/led/led.h>
53 #include <sys/sysctl.h>
56 #define _COMPONENT ACPI_OEM
57 ACPI_MODULE_NAME("IBM")
59 /* Internal methods */
60 #define ACPI_IBM_METHOD_EVENTS 1
61 #define ACPI_IBM_METHOD_EVENTMASK 2
62 #define ACPI_IBM_METHOD_HOTKEY 3
63 #define ACPI_IBM_METHOD_BRIGHTNESS 4
64 #define ACPI_IBM_METHOD_VOLUME 5
65 #define ACPI_IBM_METHOD_MUTE 6
66 #define ACPI_IBM_METHOD_THINKLIGHT 7
67 #define ACPI_IBM_METHOD_BLUETOOTH 8
68 #define ACPI_IBM_METHOD_WLAN 9
69 #define ACPI_IBM_METHOD_FANSPEED 10
70 #define ACPI_IBM_METHOD_FANLEVEL 11
71 #define ACPI_IBM_METHOD_FANSTATUS 12
72 #define ACPI_IBM_METHOD_THERMAL 13
75 #define IBM_RTC_HOTKEY1 0x64
76 #define IBM_RTC_MASK_HOME (1 << 0)
77 #define IBM_RTC_MASK_SEARCH (1 << 1)
78 #define IBM_RTC_MASK_MAIL (1 << 2)
79 #define IBM_RTC_MASK_WLAN (1 << 5)
80 #define IBM_RTC_HOTKEY2 0x65
81 #define IBM_RTC_MASK_THINKPAD (1 << 3)
82 #define IBM_RTC_MASK_ZOOM (1 << 5)
83 #define IBM_RTC_MASK_VIDEO (1 << 6)
84 #define IBM_RTC_MASK_HIBERNATE (1 << 7)
85 #define IBM_RTC_THINKLIGHT 0x66
86 #define IBM_RTC_MASK_THINKLIGHT (1 << 4)
87 #define IBM_RTC_SCREENEXPAND 0x67
88 #define IBM_RTC_MASK_SCREENEXPAND (1 << 5)
89 #define IBM_RTC_BRIGHTNESS 0x6c
90 #define IBM_RTC_MASK_BRIGHTNESS (1 << 5)
91 #define IBM_RTC_VOLUME 0x6e
92 #define IBM_RTC_MASK_VOLUME (1 << 7)
94 /* Embedded Controller registers */
95 #define IBM_EC_BRIGHTNESS 0x31
96 #define IBM_EC_MASK_BRI 0x7
97 #define IBM_EC_VOLUME 0x30
98 #define IBM_EC_MASK_VOL 0xf
99 #define IBM_EC_MASK_MUTE (1 << 6)
100 #define IBM_EC_FANSTATUS 0x2F
101 #define IBM_EC_MASK_FANLEVEL 0x3f
102 #define IBM_EC_MASK_FANDISENGAGED (1 << 6)
103 #define IBM_EC_MASK_FANSTATUS (1 << 7)
104 #define IBM_EC_FANSPEED 0x84
107 #define IBM_CMOS_VOLUME_DOWN 0
108 #define IBM_CMOS_VOLUME_UP 1
109 #define IBM_CMOS_VOLUME_MUTE 2
110 #define IBM_CMOS_BRIGHTNESS_UP 4
111 #define IBM_CMOS_BRIGHTNESS_DOWN 5
114 #define IBM_NAME_KEYLIGHT "KBLT"
115 #define IBM_NAME_WLAN_BT_GET "GBDC"
116 #define IBM_NAME_WLAN_BT_SET "SBDC"
117 #define IBM_NAME_MASK_BT (1 << 1)
118 #define IBM_NAME_MASK_WLAN (1 << 2)
119 #define IBM_NAME_THERMAL_GET "TMP7"
120 #define IBM_NAME_THERMAL_UPDT "UPDT"
122 #define IBM_NAME_EVENTS_STATUS_GET "DHKC"
123 #define IBM_NAME_EVENTS_MASK_GET "DHKN"
124 #define IBM_NAME_EVENTS_STATUS_SET "MHKC"
125 #define IBM_NAME_EVENTS_MASK_SET "MHKM"
126 #define IBM_NAME_EVENTS_GET "MHKP"
127 #define IBM_NAME_EVENTS_AVAILMASK "MHKA"
129 #define ABS(x) (((x) < 0)? -(x) : (x))
131 struct acpi_ibm_softc {
135 /* Embedded controller */
137 ACPI_HANDLE ec_handle;
140 ACPI_HANDLE cmos_handle;
143 ACPI_HANDLE fan_handle;
146 /* Keylight commands and states */
147 ACPI_HANDLE light_handle;
151 int light_get_supported;
152 int light_set_supported;
154 /* led(4) interface */
155 struct cdev *led_dev;
160 int thermal_updt_supported;
162 unsigned int events_availmask;
163 unsigned int events_initialmask;
164 int events_mask_supported;
167 struct sysctl_ctx_list *sysctl_ctx;
168 struct sysctl_oid *sysctl_tree;
176 } acpi_ibm_sysctls[] = {
179 .method = ACPI_IBM_METHOD_EVENTS,
180 .description = "ACPI events enable",
181 .access = CTLTYPE_INT | CTLFLAG_RW
185 .method = ACPI_IBM_METHOD_EVENTMASK,
186 .description = "ACPI eventmask",
187 .access = CTLTYPE_INT | CTLFLAG_RW
191 .method = ACPI_IBM_METHOD_HOTKEY,
192 .description = "Key Status",
193 .access = CTLTYPE_INT | CTLFLAG_RD
196 .name = "lcd_brightness",
197 .method = ACPI_IBM_METHOD_BRIGHTNESS,
198 .description = "LCD Brightness",
199 .access = CTLTYPE_INT | CTLFLAG_RW
203 .method = ACPI_IBM_METHOD_VOLUME,
204 .description = "Volume",
205 .access = CTLTYPE_INT | CTLFLAG_RW
209 .method = ACPI_IBM_METHOD_MUTE,
210 .description = "Mute",
211 .access = CTLTYPE_INT | CTLFLAG_RW
214 .name = "thinklight",
215 .method = ACPI_IBM_METHOD_THINKLIGHT,
216 .description = "Thinklight enable",
217 .access = CTLTYPE_INT | CTLFLAG_RW
221 .method = ACPI_IBM_METHOD_BLUETOOTH,
222 .description = "Bluetooth enable",
223 .access = CTLTYPE_INT | CTLFLAG_RW
227 .method = ACPI_IBM_METHOD_WLAN,
228 .description = "WLAN enable",
229 .access = CTLTYPE_INT | CTLFLAG_RD
233 .method = ACPI_IBM_METHOD_FANSPEED,
234 .description = "Fan speed",
235 .access = CTLTYPE_INT | CTLFLAG_RD
239 .method = ACPI_IBM_METHOD_FANLEVEL,
240 .description = "Fan level",
241 .access = CTLTYPE_INT | CTLFLAG_RW
245 .method = ACPI_IBM_METHOD_FANSTATUS,
246 .description = "Fan enable",
247 .access = CTLTYPE_INT | CTLFLAG_RW
253 ACPI_SERIAL_DECL(ibm, "ACPI IBM extras");
255 static int acpi_ibm_probe(device_t dev);
256 static int acpi_ibm_attach(device_t dev);
257 static int acpi_ibm_detach(device_t dev);
259 static void ibm_led(void *softc, int onoff);
260 static void ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused);
262 static int acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS);
263 static int acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method);
264 static int acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method);
265 static int acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int val);
267 static int acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val);
268 static int acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS);
269 static void acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context);
271 static device_method_t acpi_ibm_methods[] = {
272 /* Device interface */
273 DEVMETHOD(device_probe, acpi_ibm_probe),
274 DEVMETHOD(device_attach, acpi_ibm_attach),
275 DEVMETHOD(device_detach, acpi_ibm_detach),
280 static driver_t acpi_ibm_driver = {
283 sizeof(struct acpi_ibm_softc),
286 static devclass_t acpi_ibm_devclass;
288 DRIVER_MODULE(acpi_ibm, acpi, acpi_ibm_driver, acpi_ibm_devclass,
290 MODULE_DEPEND(acpi_ibm, acpi, 1, 1, 1);
291 static char *ibm_ids[] = {"IBM0057", "IBM0068", NULL};
294 ibm_led(void *softc, int onoff)
296 struct acpi_ibm_softc* sc = (struct acpi_ibm_softc*) softc;
298 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
304 sc->led_state = onoff;
306 AcpiOsExecute(OSL_NOTIFY_HANDLER, (void *)ibm_led_task, sc);
310 ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused)
312 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
314 ACPI_SERIAL_BEGIN(ibm);
315 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_THINKLIGHT, sc->led_state);
316 ACPI_SERIAL_END(ibm);
322 acpi_ibm_probe(device_t dev)
324 if (acpi_disabled("ibm") ||
325 ACPI_ID_PROBE(device_get_parent(dev), dev, ibm_ids) == NULL ||
326 device_get_unit(dev) != 0)
329 device_set_desc(dev, "IBM ThinkPad ACPI Extras");
335 acpi_ibm_attach(device_t dev)
337 struct acpi_ibm_softc *sc;
338 devclass_t ec_devclass;
340 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
342 sc = device_get_softc(dev);
344 sc->handle = acpi_get_handle(dev);
346 /* Look for the first embedded controller */
347 if (!(ec_devclass = devclass_find ("acpi_ec"))) {
349 device_printf(dev, "Couldn't find acpi_ec devclass\n");
352 if (!(sc->ec_dev = devclass_get_device(ec_devclass, 0))) {
354 device_printf(dev, "Couldn't find acpi_ec device\n");
357 sc->ec_handle = acpi_get_handle(sc->ec_dev);
359 ACPI_SERIAL_BEGIN(ibm);
361 /* Get the sysctl tree */
362 sc->sysctl_ctx = device_get_sysctl_ctx(dev);
363 sc->sysctl_tree = device_get_sysctl_tree(dev);
365 /* Look for event mask and hook up the nodes */
366 sc->events_mask_supported = ACPI_SUCCESS(acpi_GetInteger(sc->handle,
367 IBM_NAME_EVENTS_MASK_GET, &sc->events_initialmask));
369 if (sc->events_mask_supported) {
370 SYSCTL_ADD_INT(sc->sysctl_ctx,
371 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
372 "initialmask", CTLFLAG_RD,
373 &sc->events_initialmask, 0, "Initial eventmask");
375 /* The availmask is the bitmask of supported events */
376 if (ACPI_FAILURE(acpi_GetInteger(sc->handle,
377 IBM_NAME_EVENTS_AVAILMASK, &sc->events_availmask)))
378 sc->events_availmask = 0xffffffff;
380 SYSCTL_ADD_INT(sc->sysctl_ctx,
381 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
382 "availmask", CTLFLAG_RD,
383 &sc->events_availmask, 0, "Mask of supported events");
386 /* Hook up proc nodes */
387 for (int i = 0; acpi_ibm_sysctls[i].name != NULL; i++) {
388 if (!acpi_ibm_sysctl_init(sc, acpi_ibm_sysctls[i].method))
391 SYSCTL_ADD_PROC(sc->sysctl_ctx,
392 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
393 acpi_ibm_sysctls[i].name, acpi_ibm_sysctls[i].access,
394 sc, i, acpi_ibm_sysctl, "I",
395 acpi_ibm_sysctls[i].description);
398 /* Hook up thermal node */
399 if (acpi_ibm_sysctl_init(sc, ACPI_IBM_METHOD_THERMAL)) {
400 SYSCTL_ADD_PROC(sc->sysctl_ctx,
401 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
402 "thermal", CTLTYPE_STRING | CTLFLAG_RD,
403 sc, 0, acpi_ibm_thermal_sysctl, "I",
407 ACPI_SERIAL_END(ibm);
409 /* Handle notifies */
410 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
411 acpi_ibm_notify, dev);
413 /* Hook up light to led(4) */
414 if (sc->light_set_supported)
415 sc->led_dev = led_create_state(ibm_led, sc, "thinklight", sc->light_val);
421 acpi_ibm_detach(device_t dev)
423 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
425 struct acpi_ibm_softc *sc = device_get_softc(dev);
427 /* Disable events and restore eventmask */
428 ACPI_SERIAL_BEGIN(ibm);
429 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTS, 0);
430 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTMASK, sc->events_initialmask);
431 ACPI_SERIAL_END(ibm);
433 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY, acpi_ibm_notify);
435 if (sc->led_dev != NULL)
436 led_destroy(sc->led_dev);
442 acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val)
445 ACPI_OBJECT_LIST args;
448 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
449 ACPI_SERIAL_ASSERT(ibm);
453 arg[0].Type = ACPI_TYPE_INTEGER;
454 arg[1].Type = ACPI_TYPE_INTEGER;
456 for (int i = 0; i < 32; ++i) {
457 arg[0].Integer.Value = i+1;
458 arg[1].Integer.Value = (((1 << i) & val) != 0);
459 status = AcpiEvaluateObject(sc->handle,
460 IBM_NAME_EVENTS_MASK_SET, &args, NULL);
462 if (ACPI_FAILURE(status))
470 acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS)
472 struct acpi_ibm_softc *sc;
478 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
480 sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
481 function = oidp->oid_arg2;
482 method = acpi_ibm_sysctls[function].method;
484 ACPI_SERIAL_BEGIN(ibm);
485 arg = acpi_ibm_sysctl_get(sc, method);
486 error = sysctl_handle_int(oidp, &arg, 0, req);
489 if (error != 0 || req->newptr == NULL)
493 error = acpi_ibm_sysctl_set(sc, method, arg);
496 ACPI_SERIAL_END(ibm);
501 acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method)
506 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
507 ACPI_SERIAL_ASSERT(ibm);
510 case ACPI_IBM_METHOD_EVENTS:
511 acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_GET, &val);
514 case ACPI_IBM_METHOD_EVENTMASK:
515 if (sc->events_mask_supported)
516 acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_MASK_GET, &val);
519 case ACPI_IBM_METHOD_HOTKEY:
521 * Construct the hotkey as a bitmask as illustrated below.
522 * Note that whenever a key was pressed, the respecting bit
523 * toggles and nothing else changes.
524 * +--+--+-+-+-+-+-+-+-+-+-+-+
525 * |11|10|9|8|7|6|5|4|3|2|1|0|
526 * +--+--+-+-+-+-+-+-+-+-+-+-+
527 * | | | | | | | | | | | |
528 * | | | | | | | | | | | +- Home Button
529 * | | | | | | | | | | +--- Search Button
530 * | | | | | | | | | +----- Mail Button
531 * | | | | | | | | +------- Thinkpad Button
532 * | | | | | | | +--------- Zoom (Fn + Space)
533 * | | | | | | +----------- WLAN Button
534 * | | | | | +------------- Video Button
535 * | | | | +--------------- Hibernate Button
536 * | | | +----------------- Thinklight Button
537 * | | +------------------- Screen expand (Fn + F8)
538 * | +--------------------- Brightness
539 * +------------------------ Volume/Mute
541 key = rtcin(IBM_RTC_HOTKEY1);
542 val = (IBM_RTC_MASK_HOME | IBM_RTC_MASK_SEARCH | IBM_RTC_MASK_MAIL | IBM_RTC_MASK_WLAN) & key;
543 key = rtcin(IBM_RTC_HOTKEY2);
544 val |= (IBM_RTC_MASK_THINKPAD | IBM_RTC_MASK_VIDEO | IBM_RTC_MASK_HIBERNATE) & key;
545 val |= (IBM_RTC_MASK_ZOOM & key) >> 1;
546 key = rtcin(IBM_RTC_THINKLIGHT);
547 val |= (IBM_RTC_MASK_THINKLIGHT & key) << 4;
548 key = rtcin(IBM_RTC_SCREENEXPAND);
549 val |= (IBM_RTC_MASK_THINKLIGHT & key) << 4;
550 key = rtcin(IBM_RTC_BRIGHTNESS);
551 val |= (IBM_RTC_MASK_BRIGHTNESS & key) << 5;
552 key = rtcin(IBM_RTC_VOLUME);
553 val |= (IBM_RTC_MASK_VOLUME & key) << 4;
556 case ACPI_IBM_METHOD_BRIGHTNESS:
557 ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, &val_ec, 1);
558 val = val_ec & IBM_EC_MASK_BRI;
561 case ACPI_IBM_METHOD_VOLUME:
562 ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
563 val = val_ec & IBM_EC_MASK_VOL;
566 case ACPI_IBM_METHOD_MUTE:
567 ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
568 val = ((val_ec & IBM_EC_MASK_MUTE) == IBM_EC_MASK_MUTE);
571 case ACPI_IBM_METHOD_THINKLIGHT:
572 if (sc->light_get_supported)
573 acpi_GetInteger(sc->ec_handle, IBM_NAME_KEYLIGHT, &val);
578 case ACPI_IBM_METHOD_BLUETOOTH:
579 acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &val);
580 sc->wlan_bt_flags = val;
581 val = ((val & IBM_NAME_MASK_BT) != 0);
584 case ACPI_IBM_METHOD_WLAN:
585 acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &val);
586 sc->wlan_bt_flags = val;
587 val = ((val & IBM_NAME_MASK_WLAN) != 0);
590 case ACPI_IBM_METHOD_FANSPEED:
591 if (sc->fan_handle) {
592 if(ACPI_FAILURE(acpi_GetInteger(sc->fan_handle, NULL, &val)))
596 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSPEED, &val_ec, 2);
601 case ACPI_IBM_METHOD_FANLEVEL:
603 * The IBM_EC_FANSTATUS register works as follows:
604 * Bit 0-5 indicate the level at which the fan operates. Only
605 * values between 0 and 7 have an effect. Everything
606 * above 7 is treated the same as level 7
607 * Bit 6 overrides the fan speed limit if set to 1
608 * Bit 7 indicates at which mode the fan operates:
609 * manual (0) or automatic (1)
611 if (!sc->fan_handle) {
612 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
613 val = val_ec & IBM_EC_MASK_FANLEVEL;
617 case ACPI_IBM_METHOD_FANSTATUS:
618 if (!sc->fan_handle) {
619 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
620 val = (val_ec & IBM_EC_MASK_FANSTATUS) == IBM_EC_MASK_FANSTATUS;
631 acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int arg)
636 ACPI_OBJECT_LIST Args;
639 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
640 ACPI_SERIAL_ASSERT(ibm);
643 case ACPI_IBM_METHOD_EVENTS:
644 if (arg < 0 || arg > 1)
647 status = acpi_SetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_SET, arg);
648 if (ACPI_FAILURE(status))
650 if (sc->events_mask_supported)
651 return acpi_ibm_eventmask_set(sc, sc->events_availmask);
654 case ACPI_IBM_METHOD_EVENTMASK:
655 if (sc->events_mask_supported)
656 return acpi_ibm_eventmask_set(sc, arg);
659 case ACPI_IBM_METHOD_BRIGHTNESS:
660 if (arg < 0 || arg > 7)
663 if (sc->cmos_handle) {
664 /* Read the current brightness */
665 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, &val_ec, 1);
666 if (ACPI_FAILURE(status))
668 val = val_ec & IBM_EC_MASK_BRI;
672 Arg.Type = ACPI_TYPE_INTEGER;
673 Arg.Integer.Value = (arg > val) ? IBM_CMOS_BRIGHTNESS_UP : IBM_CMOS_BRIGHTNESS_DOWN;
675 step = (arg > val) ? 1 : -1;
676 for (int i = val; i != arg; i += step) {
677 status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
678 if (ACPI_FAILURE(status))
682 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_BRIGHTNESS, arg, 1);
685 case ACPI_IBM_METHOD_VOLUME:
686 if (arg < 0 || arg > 14)
689 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
690 if (ACPI_FAILURE(status))
693 if (sc->cmos_handle) {
694 val = val_ec & IBM_EC_MASK_VOL;
698 Arg.Type = ACPI_TYPE_INTEGER;
699 Arg.Integer.Value = (arg > val) ? IBM_CMOS_VOLUME_UP : IBM_CMOS_VOLUME_DOWN;
701 step = (arg > val) ? 1 : -1;
702 for (int i = val; i != arg; i += step) {
703 status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
704 if (ACPI_FAILURE(status))
708 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME, arg + (val_ec & (~IBM_EC_MASK_VOL)), 1);
711 case ACPI_IBM_METHOD_MUTE:
712 if (arg < 0 || arg > 1)
715 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
716 if (ACPI_FAILURE(status))
719 if (sc->cmos_handle) {
720 val = val_ec & IBM_EC_MASK_VOL;
724 Arg.Type = ACPI_TYPE_INTEGER;
725 Arg.Integer.Value = IBM_CMOS_VOLUME_MUTE;
727 status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
728 if (ACPI_FAILURE(status))
731 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);
734 case ACPI_IBM_METHOD_THINKLIGHT:
735 if (arg < 0 || arg > 1)
738 if (sc->light_set_supported) {
741 Arg.Type = ACPI_TYPE_INTEGER;
742 Arg.Integer.Value = arg ? sc->light_cmd_on : sc->light_cmd_off;
744 status = AcpiEvaluateObject(sc->light_handle, NULL, &Args, NULL);
745 if (ACPI_SUCCESS(status))
751 case ACPI_IBM_METHOD_BLUETOOTH:
752 if (arg < 0 || arg > 1)
755 val = (arg == 1) ? sc->wlan_bt_flags | IBM_NAME_MASK_BT : sc->wlan_bt_flags & (~IBM_NAME_MASK_BT);
756 return acpi_SetInteger(sc->handle, IBM_NAME_WLAN_BT_SET, val);
759 case ACPI_IBM_METHOD_FANLEVEL:
760 if (arg < 0 || arg > 7)
763 if (!sc->fan_handle) {
764 /* Read the current fanstatus */
765 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
766 val = val_ec & (~IBM_EC_MASK_FANLEVEL);
768 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_FANSTATUS, val | arg, 1);
772 case ACPI_IBM_METHOD_FANSTATUS:
773 if (arg < 0 || arg > 1)
776 if (!sc->fan_handle) {
777 /* Read the current fanstatus */
778 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
780 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_FANSTATUS,
781 (arg == 1) ? (val_ec | IBM_EC_MASK_FANSTATUS) : (val_ec & (~IBM_EC_MASK_FANSTATUS)), 1);
790 acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method)
793 ACPI_OBJECT_TYPE cmos_t;
794 ACPI_HANDLE ledb_handle;
797 case ACPI_IBM_METHOD_EVENTS:
798 /* Events are disabled by default */
801 case ACPI_IBM_METHOD_EVENTMASK:
802 return (sc->events_mask_supported);
804 case ACPI_IBM_METHOD_HOTKEY:
805 case ACPI_IBM_METHOD_BRIGHTNESS:
806 case ACPI_IBM_METHOD_VOLUME:
807 case ACPI_IBM_METHOD_MUTE:
808 /* EC is required here, which was aready checked before */
811 case ACPI_IBM_METHOD_THINKLIGHT:
812 sc->cmos_handle = NULL;
813 sc->light_get_supported = ACPI_SUCCESS(acpi_GetInteger(
814 sc->ec_handle, IBM_NAME_KEYLIGHT, &sc->light_val));
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)
838 if (sc->light_set_supported) {
845 case ACPI_IBM_METHOD_BLUETOOTH:
846 case ACPI_IBM_METHOD_WLAN:
847 if (ACPI_SUCCESS(acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &dummy)))
851 case ACPI_IBM_METHOD_FANSPEED:
853 * Some models report the fan speed in levels from 0-7
854 * Newer models report it contiguously
857 (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "GFAN", &sc->fan_handle)) ||
858 ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\FSPD", &sc->fan_handle)));
861 case ACPI_IBM_METHOD_FANLEVEL:
862 case ACPI_IBM_METHOD_FANSTATUS:
864 * Fan status is only supported on those models,
865 * which report fan RPM contiguously, not in levels
871 case ACPI_IBM_METHOD_THERMAL:
872 if (ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_THERMAL_GET, &dummy))) {
873 sc->thermal_updt_supported = ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_THERMAL_UPDT, &dummy));
882 acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS)
884 struct acpi_ibm_softc *sc;
886 char temp_cmd[] = "TMP0";
889 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
891 sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
893 ACPI_SERIAL_BEGIN(ibm);
895 for (int i = 0; i < 8; ++i) {
896 temp_cmd[3] = '0' + i;
899 * The TMPx methods seem to return +/- 128 or 0
900 * when the respecting sensor is not available
902 if (ACPI_FAILURE(acpi_GetInteger(sc->ec_handle, temp_cmd,
903 &temp[i])) || ABS(temp[i]) == 128 || temp[i] == 0)
905 else if (sc->thermal_updt_supported)
906 /* Temperature is reported in tenth of Kelvin */
907 temp[i] = (temp[i] - 2732 + 5) / 10;
910 error = sysctl_handle_opaque(oidp, &temp, 8*sizeof(int), req);
912 ACPI_SERIAL_END(ibm);
917 acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context)
919 int event, arg, type;
920 device_t dev = context;
921 struct acpi_ibm_softc *sc = device_get_softc(dev);
923 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
926 device_printf(dev, "Unknown notify\n");
929 acpi_GetInteger(acpi_get_handle(dev), IBM_NAME_EVENTS_GET, &event);
935 type = (event >> 12) & 0xf;
939 if (!(sc->events_availmask & (1 << (arg - 1)))) {
940 device_printf(dev, "Unknown key %d\n", arg);
945 acpi_UserNotify("IBM", h, (arg & 0xff));