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/power.h>
55 #include <sys/sysctl.h>
58 #define _COMPONENT ACPI_OEM
59 ACPI_MODULE_NAME("IBM")
61 /* Internal methods */
62 #define ACPI_IBM_METHOD_EVENTS 1
63 #define ACPI_IBM_METHOD_EVENTMASK 2
64 #define ACPI_IBM_METHOD_HOTKEY 3
65 #define ACPI_IBM_METHOD_BRIGHTNESS 4
66 #define ACPI_IBM_METHOD_VOLUME 5
67 #define ACPI_IBM_METHOD_MUTE 6
68 #define ACPI_IBM_METHOD_THINKLIGHT 7
69 #define ACPI_IBM_METHOD_BLUETOOTH 8
70 #define ACPI_IBM_METHOD_WLAN 9
71 #define ACPI_IBM_METHOD_FANSPEED 10
72 #define ACPI_IBM_METHOD_FANLEVEL 11
73 #define ACPI_IBM_METHOD_FANSTATUS 12
74 #define ACPI_IBM_METHOD_THERMAL 13
75 #define ACPI_IBM_METHOD_HANDLEREVENTS 14
78 #define IBM_RTC_HOTKEY1 0x64
79 #define IBM_RTC_MASK_HOME (1 << 0)
80 #define IBM_RTC_MASK_SEARCH (1 << 1)
81 #define IBM_RTC_MASK_MAIL (1 << 2)
82 #define IBM_RTC_MASK_WLAN (1 << 5)
83 #define IBM_RTC_HOTKEY2 0x65
84 #define IBM_RTC_MASK_THINKPAD (1 << 3)
85 #define IBM_RTC_MASK_ZOOM (1 << 5)
86 #define IBM_RTC_MASK_VIDEO (1 << 6)
87 #define IBM_RTC_MASK_HIBERNATE (1 << 7)
88 #define IBM_RTC_THINKLIGHT 0x66
89 #define IBM_RTC_MASK_THINKLIGHT (1 << 4)
90 #define IBM_RTC_SCREENEXPAND 0x67
91 #define IBM_RTC_MASK_SCREENEXPAND (1 << 5)
92 #define IBM_RTC_BRIGHTNESS 0x6c
93 #define IBM_RTC_MASK_BRIGHTNESS (1 << 5)
94 #define IBM_RTC_VOLUME 0x6e
95 #define IBM_RTC_MASK_VOLUME (1 << 7)
97 /* Embedded Controller registers */
98 #define IBM_EC_BRIGHTNESS 0x31
99 #define IBM_EC_MASK_BRI 0x7
100 #define IBM_EC_VOLUME 0x30
101 #define IBM_EC_MASK_VOL 0xf
102 #define IBM_EC_MASK_MUTE (1 << 6)
103 #define IBM_EC_FANSTATUS 0x2F
104 #define IBM_EC_MASK_FANLEVEL 0x3f
105 #define IBM_EC_MASK_FANDISENGAGED (1 << 6)
106 #define IBM_EC_MASK_FANSTATUS (1 << 7)
107 #define IBM_EC_FANSPEED 0x84
110 #define IBM_CMOS_VOLUME_DOWN 0
111 #define IBM_CMOS_VOLUME_UP 1
112 #define IBM_CMOS_VOLUME_MUTE 2
113 #define IBM_CMOS_BRIGHTNESS_UP 4
114 #define IBM_CMOS_BRIGHTNESS_DOWN 5
117 #define IBM_NAME_KEYLIGHT "KBLT"
118 #define IBM_NAME_WLAN_BT_GET "GBDC"
119 #define IBM_NAME_WLAN_BT_SET "SBDC"
120 #define IBM_NAME_MASK_BT (1 << 1)
121 #define IBM_NAME_MASK_WLAN (1 << 2)
122 #define IBM_NAME_THERMAL_GET "TMP7"
123 #define IBM_NAME_THERMAL_UPDT "UPDT"
125 #define IBM_NAME_EVENTS_STATUS_GET "DHKC"
126 #define IBM_NAME_EVENTS_MASK_GET "DHKN"
127 #define IBM_NAME_EVENTS_STATUS_SET "MHKC"
128 #define IBM_NAME_EVENTS_MASK_SET "MHKM"
129 #define IBM_NAME_EVENTS_GET "MHKP"
130 #define IBM_NAME_EVENTS_AVAILMASK "MHKA"
133 #define IBM_EVENT_LCD_BACKLIGHT 0x03
134 #define IBM_EVENT_SUSPEND_TO_RAM 0x04
135 #define IBM_EVENT_BLUETOOTH 0x05
136 #define IBM_EVENT_SCREEN_EXPAND 0x07
137 #define IBM_EVENT_SUSPEND_TO_DISK 0x0c
138 #define IBM_EVENT_BRIGHTNESS_UP 0x10
139 #define IBM_EVENT_BRIGHTNESS_DOWN 0x11
140 #define IBM_EVENT_THINKLIGHT 0x12
141 #define IBM_EVENT_ZOOM 0x14
142 #define IBM_EVENT_VOLUME_UP 0x15
143 #define IBM_EVENT_VOLUME_DOWN 0x16
144 #define IBM_EVENT_MUTE 0x17
145 #define IBM_EVENT_ACCESS_IBM_BUTTON 0x18
147 #define ABS(x) (((x) < 0)? -(x) : (x))
149 struct acpi_ibm_softc {
153 /* Embedded controller */
155 ACPI_HANDLE ec_handle;
158 ACPI_HANDLE cmos_handle;
161 ACPI_HANDLE fan_handle;
164 /* Keylight commands and states */
165 ACPI_HANDLE light_handle;
169 int light_get_supported;
170 int light_set_supported;
172 /* led(4) interface */
173 struct cdev *led_dev;
178 int thermal_updt_supported;
180 unsigned int events_availmask;
181 unsigned int events_initialmask;
182 int events_mask_supported;
185 unsigned int handler_events;
187 struct sysctl_ctx_list *sysctl_ctx;
188 struct sysctl_oid *sysctl_tree;
196 } acpi_ibm_sysctls[] = {
199 .method = ACPI_IBM_METHOD_EVENTS,
200 .description = "ACPI events enable",
204 .method = ACPI_IBM_METHOD_EVENTMASK,
205 .description = "ACPI eventmask",
209 .method = ACPI_IBM_METHOD_HOTKEY,
210 .description = "Key Status",
214 .name = "lcd_brightness",
215 .method = ACPI_IBM_METHOD_BRIGHTNESS,
216 .description = "LCD Brightness",
220 .method = ACPI_IBM_METHOD_VOLUME,
221 .description = "Volume",
225 .method = ACPI_IBM_METHOD_MUTE,
226 .description = "Mute",
229 .name = "thinklight",
230 .method = ACPI_IBM_METHOD_THINKLIGHT,
231 .description = "Thinklight enable",
235 .method = ACPI_IBM_METHOD_BLUETOOTH,
236 .description = "Bluetooth enable",
240 .method = ACPI_IBM_METHOD_WLAN,
241 .description = "WLAN enable",
246 .method = ACPI_IBM_METHOD_FANSPEED,
247 .description = "Fan speed",
252 .method = ACPI_IBM_METHOD_FANLEVEL,
253 .description = "Fan level",
257 .method = ACPI_IBM_METHOD_FANSTATUS,
258 .description = "Fan enable",
264 ACPI_SERIAL_DECL(ibm, "ACPI IBM extras");
266 static int acpi_ibm_probe(device_t dev);
267 static int acpi_ibm_attach(device_t dev);
268 static int acpi_ibm_detach(device_t dev);
269 static int acpi_ibm_resume(device_t dev);
271 static void ibm_led(void *softc, int onoff);
272 static void ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused);
274 static int acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS);
275 static int acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method);
276 static int acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method);
277 static int acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int val);
279 static int acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val);
280 static int acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS);
281 static int acpi_ibm_handlerevents_sysctl(SYSCTL_HANDLER_ARGS);
282 static void acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context);
284 static int acpi_ibm_brightness_set(struct acpi_ibm_softc *sc, int arg);
285 static int acpi_ibm_bluetooth_set(struct acpi_ibm_softc *sc, int arg);
286 static int acpi_ibm_thinklight_set(struct acpi_ibm_softc *sc, int arg);
287 static int acpi_ibm_volume_set(struct acpi_ibm_softc *sc, int arg);
288 static int acpi_ibm_mute_set(struct acpi_ibm_softc *sc, int arg);
290 static device_method_t acpi_ibm_methods[] = {
291 /* Device interface */
292 DEVMETHOD(device_probe, acpi_ibm_probe),
293 DEVMETHOD(device_attach, acpi_ibm_attach),
294 DEVMETHOD(device_detach, acpi_ibm_detach),
295 DEVMETHOD(device_resume, acpi_ibm_resume),
300 static driver_t acpi_ibm_driver = {
303 sizeof(struct acpi_ibm_softc),
306 static devclass_t acpi_ibm_devclass;
308 DRIVER_MODULE(acpi_ibm, acpi, acpi_ibm_driver, acpi_ibm_devclass,
310 MODULE_DEPEND(acpi_ibm, acpi, 1, 1, 1);
311 static char *ibm_ids[] = {"IBM0068", "LEN0068", NULL};
314 ibm_led(void *softc, int onoff)
316 struct acpi_ibm_softc* sc = (struct acpi_ibm_softc*) softc;
318 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
324 sc->led_state = onoff;
326 AcpiOsExecute(OSL_NOTIFY_HANDLER, (void *)ibm_led_task, sc);
330 ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused)
332 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
334 ACPI_SERIAL_BEGIN(ibm);
335 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_THINKLIGHT, sc->led_state);
336 ACPI_SERIAL_END(ibm);
342 acpi_ibm_probe(device_t dev)
344 if (acpi_disabled("ibm") ||
345 ACPI_ID_PROBE(device_get_parent(dev), dev, ibm_ids) == NULL ||
346 device_get_unit(dev) != 0)
349 device_set_desc(dev, "IBM ThinkPad ACPI Extras");
355 acpi_ibm_attach(device_t dev)
357 struct acpi_ibm_softc *sc;
358 devclass_t ec_devclass;
360 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
362 sc = device_get_softc(dev);
364 sc->handle = acpi_get_handle(dev);
366 /* Look for the first embedded controller */
367 if (!(ec_devclass = devclass_find ("acpi_ec"))) {
369 device_printf(dev, "Couldn't find acpi_ec devclass\n");
372 if (!(sc->ec_dev = devclass_get_device(ec_devclass, 0))) {
374 device_printf(dev, "Couldn't find acpi_ec device\n");
377 sc->ec_handle = acpi_get_handle(sc->ec_dev);
379 /* Get the sysctl tree */
380 sc->sysctl_ctx = device_get_sysctl_ctx(dev);
381 sc->sysctl_tree = device_get_sysctl_tree(dev);
383 /* Look for event mask and hook up the nodes */
384 sc->events_mask_supported = ACPI_SUCCESS(acpi_GetInteger(sc->handle,
385 IBM_NAME_EVENTS_MASK_GET, &sc->events_initialmask));
387 if (sc->events_mask_supported) {
388 SYSCTL_ADD_UINT(sc->sysctl_ctx,
389 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
390 "initialmask", CTLFLAG_RD,
391 &sc->events_initialmask, 0, "Initial eventmask");
393 /* The availmask is the bitmask of supported events */
394 if (ACPI_FAILURE(acpi_GetInteger(sc->handle,
395 IBM_NAME_EVENTS_AVAILMASK, &sc->events_availmask)))
396 sc->events_availmask = 0xffffffff;
398 SYSCTL_ADD_UINT(sc->sysctl_ctx,
399 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
400 "availmask", CTLFLAG_RD,
401 &sc->events_availmask, 0, "Mask of supported events");
404 /* Hook up proc nodes */
405 for (int i = 0; acpi_ibm_sysctls[i].name != NULL; i++) {
406 if (!acpi_ibm_sysctl_init(sc, acpi_ibm_sysctls[i].method))
409 if (acpi_ibm_sysctls[i].flag_rdonly != 0) {
410 SYSCTL_ADD_PROC(sc->sysctl_ctx,
411 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
412 acpi_ibm_sysctls[i].name, CTLTYPE_INT | CTLFLAG_RD,
413 sc, i, acpi_ibm_sysctl, "I",
414 acpi_ibm_sysctls[i].description);
416 SYSCTL_ADD_PROC(sc->sysctl_ctx,
417 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
418 acpi_ibm_sysctls[i].name, CTLTYPE_INT | CTLFLAG_RW,
419 sc, i, acpi_ibm_sysctl, "I",
420 acpi_ibm_sysctls[i].description);
424 /* Hook up thermal node */
425 if (acpi_ibm_sysctl_init(sc, ACPI_IBM_METHOD_THERMAL)) {
426 SYSCTL_ADD_PROC(sc->sysctl_ctx,
427 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
428 "thermal", CTLTYPE_INT | CTLFLAG_RD,
429 sc, 0, acpi_ibm_thermal_sysctl, "I",
433 /* Hook up handlerevents node */
434 if (acpi_ibm_sysctl_init(sc, ACPI_IBM_METHOD_HANDLEREVENTS)) {
435 SYSCTL_ADD_PROC(sc->sysctl_ctx,
436 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
437 "handlerevents", CTLTYPE_STRING | CTLFLAG_RW,
438 sc, 0, acpi_ibm_handlerevents_sysctl, "I",
439 "devd(8) events handled by acpi_ibm");
442 /* Handle notifies */
443 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
444 acpi_ibm_notify, dev);
446 /* Hook up light to led(4) */
447 if (sc->light_set_supported)
448 sc->led_dev = led_create_state(ibm_led, sc, "thinklight", sc->light_val);
454 acpi_ibm_detach(device_t dev)
456 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
458 struct acpi_ibm_softc *sc = device_get_softc(dev);
460 /* Disable events and restore eventmask */
461 ACPI_SERIAL_BEGIN(ibm);
462 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTS, 0);
463 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTMASK, sc->events_initialmask);
464 ACPI_SERIAL_END(ibm);
466 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY, acpi_ibm_notify);
468 if (sc->led_dev != NULL)
469 led_destroy(sc->led_dev);
475 acpi_ibm_resume(device_t dev)
477 struct acpi_ibm_softc *sc = device_get_softc(dev);
479 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
481 ACPI_SERIAL_BEGIN(ibm);
482 for (int i = 0; acpi_ibm_sysctls[i].name != NULL; i++) {
485 val = acpi_ibm_sysctl_get(sc, i);
487 if (acpi_ibm_sysctls[i].flag_rdonly != 0)
490 acpi_ibm_sysctl_set(sc, i, val);
492 ACPI_SERIAL_END(ibm);
498 acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val)
501 ACPI_OBJECT_LIST args;
504 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
505 ACPI_SERIAL_ASSERT(ibm);
509 arg[0].Type = ACPI_TYPE_INTEGER;
510 arg[1].Type = ACPI_TYPE_INTEGER;
512 for (int i = 0; i < 32; ++i) {
513 arg[0].Integer.Value = i+1;
514 arg[1].Integer.Value = (((1 << i) & val) != 0);
515 status = AcpiEvaluateObject(sc->handle,
516 IBM_NAME_EVENTS_MASK_SET, &args, NULL);
518 if (ACPI_FAILURE(status))
526 acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS)
528 struct acpi_ibm_softc *sc;
534 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
536 sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
537 function = oidp->oid_arg2;
538 method = acpi_ibm_sysctls[function].method;
540 ACPI_SERIAL_BEGIN(ibm);
541 arg = acpi_ibm_sysctl_get(sc, method);
542 error = sysctl_handle_int(oidp, &arg, 0, req);
545 if (error != 0 || req->newptr == NULL)
549 error = acpi_ibm_sysctl_set(sc, method, arg);
552 ACPI_SERIAL_END(ibm);
557 acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method)
562 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
563 ACPI_SERIAL_ASSERT(ibm);
566 case ACPI_IBM_METHOD_EVENTS:
567 acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_GET, &val);
570 case ACPI_IBM_METHOD_EVENTMASK:
571 if (sc->events_mask_supported)
572 acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_MASK_GET, &val);
575 case ACPI_IBM_METHOD_HOTKEY:
577 * Construct the hotkey as a bitmask as illustrated below.
578 * Note that whenever a key was pressed, the respecting bit
579 * toggles and nothing else changes.
580 * +--+--+-+-+-+-+-+-+-+-+-+-+
581 * |11|10|9|8|7|6|5|4|3|2|1|0|
582 * +--+--+-+-+-+-+-+-+-+-+-+-+
583 * | | | | | | | | | | | |
584 * | | | | | | | | | | | +- Home Button
585 * | | | | | | | | | | +--- Search Button
586 * | | | | | | | | | +----- Mail Button
587 * | | | | | | | | +------- Thinkpad Button
588 * | | | | | | | +--------- Zoom (Fn + Space)
589 * | | | | | | +----------- WLAN Button
590 * | | | | | +------------- Video Button
591 * | | | | +--------------- Hibernate Button
592 * | | | +----------------- Thinklight Button
593 * | | +------------------- Screen expand (Fn + F8)
594 * | +--------------------- Brightness
595 * +------------------------ Volume/Mute
597 key = rtcin(IBM_RTC_HOTKEY1);
598 val = (IBM_RTC_MASK_HOME | IBM_RTC_MASK_SEARCH | IBM_RTC_MASK_MAIL | IBM_RTC_MASK_WLAN) & key;
599 key = rtcin(IBM_RTC_HOTKEY2);
600 val |= (IBM_RTC_MASK_THINKPAD | IBM_RTC_MASK_VIDEO | IBM_RTC_MASK_HIBERNATE) & key;
601 val |= (IBM_RTC_MASK_ZOOM & key) >> 1;
602 key = rtcin(IBM_RTC_THINKLIGHT);
603 val |= (IBM_RTC_MASK_THINKLIGHT & key) << 4;
604 key = rtcin(IBM_RTC_SCREENEXPAND);
605 val |= (IBM_RTC_MASK_THINKLIGHT & key) << 4;
606 key = rtcin(IBM_RTC_BRIGHTNESS);
607 val |= (IBM_RTC_MASK_BRIGHTNESS & key) << 5;
608 key = rtcin(IBM_RTC_VOLUME);
609 val |= (IBM_RTC_MASK_VOLUME & key) << 4;
612 case ACPI_IBM_METHOD_BRIGHTNESS:
613 ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, &val_ec, 1);
614 val = val_ec & IBM_EC_MASK_BRI;
617 case ACPI_IBM_METHOD_VOLUME:
618 ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
619 val = val_ec & IBM_EC_MASK_VOL;
622 case ACPI_IBM_METHOD_MUTE:
623 ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
624 val = ((val_ec & IBM_EC_MASK_MUTE) == IBM_EC_MASK_MUTE);
627 case ACPI_IBM_METHOD_THINKLIGHT:
628 if (sc->light_get_supported)
629 acpi_GetInteger(sc->ec_handle, IBM_NAME_KEYLIGHT, &val);
634 case ACPI_IBM_METHOD_BLUETOOTH:
635 acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &val);
636 sc->wlan_bt_flags = val;
637 val = ((val & IBM_NAME_MASK_BT) != 0);
640 case ACPI_IBM_METHOD_WLAN:
641 acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &val);
642 sc->wlan_bt_flags = val;
643 val = ((val & IBM_NAME_MASK_WLAN) != 0);
646 case ACPI_IBM_METHOD_FANSPEED:
647 if (sc->fan_handle) {
648 if(ACPI_FAILURE(acpi_GetInteger(sc->fan_handle, NULL, &val)))
652 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSPEED, &val_ec, 2);
657 case ACPI_IBM_METHOD_FANLEVEL:
659 * The IBM_EC_FANSTATUS register works as follows:
660 * Bit 0-5 indicate the level at which the fan operates. Only
661 * values between 0 and 7 have an effect. Everything
662 * above 7 is treated the same as level 7
663 * Bit 6 overrides the fan speed limit if set to 1
664 * Bit 7 indicates at which mode the fan operates:
665 * manual (0) or automatic (1)
667 if (!sc->fan_handle) {
668 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
669 val = val_ec & IBM_EC_MASK_FANLEVEL;
673 case ACPI_IBM_METHOD_FANSTATUS:
674 if (!sc->fan_handle) {
675 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
676 val = (val_ec & IBM_EC_MASK_FANSTATUS) == IBM_EC_MASK_FANSTATUS;
687 acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int arg)
693 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
694 ACPI_SERIAL_ASSERT(ibm);
697 case ACPI_IBM_METHOD_EVENTS:
698 if (arg < 0 || arg > 1)
701 status = acpi_SetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_SET, arg);
702 if (ACPI_FAILURE(status))
704 if (sc->events_mask_supported)
705 return acpi_ibm_eventmask_set(sc, sc->events_availmask);
708 case ACPI_IBM_METHOD_EVENTMASK:
709 if (sc->events_mask_supported)
710 return acpi_ibm_eventmask_set(sc, arg);
713 case ACPI_IBM_METHOD_BRIGHTNESS:
714 return acpi_ibm_brightness_set(sc, arg);
717 case ACPI_IBM_METHOD_VOLUME:
718 return acpi_ibm_volume_set(sc, arg);
721 case ACPI_IBM_METHOD_MUTE:
722 return acpi_ibm_mute_set(sc, arg);
725 case ACPI_IBM_METHOD_THINKLIGHT:
726 return acpi_ibm_thinklight_set(sc, arg);
729 case ACPI_IBM_METHOD_BLUETOOTH:
730 return acpi_ibm_bluetooth_set(sc, arg);
733 case ACPI_IBM_METHOD_FANLEVEL:
734 if (arg < 0 || arg > 7)
737 if (!sc->fan_handle) {
738 /* Read the current fanstatus */
739 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
740 val = val_ec & (~IBM_EC_MASK_FANLEVEL);
742 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_FANSTATUS, val | arg, 1);
746 case ACPI_IBM_METHOD_FANSTATUS:
747 if (arg < 0 || arg > 1)
750 if (!sc->fan_handle) {
751 /* Read the current fanstatus */
752 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
754 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_FANSTATUS,
755 (arg == 1) ? (val_ec | IBM_EC_MASK_FANSTATUS) : (val_ec & (~IBM_EC_MASK_FANSTATUS)), 1);
764 acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method)
767 ACPI_OBJECT_TYPE cmos_t;
768 ACPI_HANDLE ledb_handle;
771 case ACPI_IBM_METHOD_EVENTS:
772 /* Events are disabled by default */
775 case ACPI_IBM_METHOD_EVENTMASK:
776 return (sc->events_mask_supported);
778 case ACPI_IBM_METHOD_HOTKEY:
779 case ACPI_IBM_METHOD_BRIGHTNESS:
780 case ACPI_IBM_METHOD_VOLUME:
781 case ACPI_IBM_METHOD_MUTE:
782 /* EC is required here, which was aready checked before */
785 case ACPI_IBM_METHOD_THINKLIGHT:
786 sc->cmos_handle = NULL;
787 sc->light_get_supported = ACPI_SUCCESS(acpi_GetInteger(
788 sc->ec_handle, IBM_NAME_KEYLIGHT, &sc->light_val));
790 if ((ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\UCMS", &sc->light_handle)) ||
791 ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMOS", &sc->light_handle)) ||
792 ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMS", &sc->light_handle))) &&
793 ACPI_SUCCESS(AcpiGetType(sc->light_handle, &cmos_t)) &&
794 cmos_t == ACPI_TYPE_METHOD) {
795 sc->light_cmd_on = 0x0c;
796 sc->light_cmd_off = 0x0d;
797 sc->cmos_handle = sc->light_handle;
799 else if (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\LGHT", &sc->light_handle))) {
800 sc->light_cmd_on = 1;
801 sc->light_cmd_off = 0;
804 sc->light_handle = NULL;
806 sc->light_set_supported = (sc->light_handle &&
807 ACPI_FAILURE(AcpiGetHandle(sc->ec_handle, "LEDB", &ledb_handle)));
809 if (sc->light_get_supported)
812 if (sc->light_set_supported) {
819 case ACPI_IBM_METHOD_BLUETOOTH:
820 case ACPI_IBM_METHOD_WLAN:
821 if (ACPI_SUCCESS(acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &dummy)))
825 case ACPI_IBM_METHOD_FANSPEED:
827 * Some models report the fan speed in levels from 0-7
828 * Newer models report it contiguously
831 (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "GFAN", &sc->fan_handle)) ||
832 ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\FSPD", &sc->fan_handle)));
835 case ACPI_IBM_METHOD_FANLEVEL:
836 case ACPI_IBM_METHOD_FANSTATUS:
838 * Fan status is only supported on those models,
839 * which report fan RPM contiguously, not in levels
845 case ACPI_IBM_METHOD_THERMAL:
846 if (ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_THERMAL_GET, &dummy))) {
847 sc->thermal_updt_supported = ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_THERMAL_UPDT, &dummy));
852 case ACPI_IBM_METHOD_HANDLEREVENTS:
859 acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS)
861 struct acpi_ibm_softc *sc;
863 char temp_cmd[] = "TMP0";
866 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
868 sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
870 ACPI_SERIAL_BEGIN(ibm);
872 for (int i = 0; i < 8; ++i) {
873 temp_cmd[3] = '0' + i;
876 * The TMPx methods seem to return +/- 128 or 0
877 * when the respecting sensor is not available
879 if (ACPI_FAILURE(acpi_GetInteger(sc->ec_handle, temp_cmd,
880 &temp[i])) || ABS(temp[i]) == 128 || temp[i] == 0)
882 else if (sc->thermal_updt_supported)
883 /* Temperature is reported in tenth of Kelvin */
884 temp[i] = (temp[i] - 2732 + 5) / 10;
887 error = sysctl_handle_opaque(oidp, &temp, 8*sizeof(int), req);
889 ACPI_SERIAL_END(ibm);
894 acpi_ibm_handlerevents_sysctl(SYSCTL_HANDLER_ARGS)
896 struct acpi_ibm_softc *sc;
901 unsigned int handler_events;
904 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
906 sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
908 if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
911 ACPI_SERIAL_BEGIN(ibm);
913 /* Get old values if this is a get request. */
914 if (req->newptr == NULL) {
915 for (int i = 0; i < 8 * sizeof(sc->handler_events); i++)
916 if (sc->handler_events & (1 << i))
917 sbuf_printf(&sb, "0x%02x ", i + 1);
918 if (sbuf_len(&sb) == 0)
919 sbuf_printf(&sb, "NONE");
924 strlcpy(temp, sbuf_data(&sb), sizeof(temp));
927 error = sysctl_handle_string(oidp, temp, sizeof(temp), req);
929 /* Check for error or no change */
930 if (error != 0 || req->newptr == NULL)
933 /* If the user is setting a string, parse it. */
944 while (*ep && !isspace(*ep))
951 if (strncmp(cp, "NONE", 4) == 0) {
956 if (l >= 3 && cp[0] == '0' && (cp[1] == 'X' || cp[1] == 'x'))
957 val = strtoul(cp, &ep, 16);
959 val = strtoul(cp, &ep, 10);
961 if (val == 0 || ep == cp || val >= 8 * sizeof(handler_events)) {
963 device_printf(sc->dev, "invalid event code: %s\n", cp);
968 handler_events |= 1 << (val - 1);
973 sc->handler_events = handler_events;
975 ACPI_SERIAL_END(ibm);
980 acpi_ibm_brightness_set(struct acpi_ibm_softc *sc, int arg)
985 ACPI_OBJECT_LIST Args;
988 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
989 ACPI_SERIAL_ASSERT(ibm);
991 if (arg < 0 || arg > 7)
994 /* Read the current brightness */
995 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, &val_ec, 1);
996 if (ACPI_FAILURE(status))
999 if (sc->cmos_handle) {
1000 val = val_ec & IBM_EC_MASK_BRI;
1003 Args.Pointer = &Arg;
1004 Arg.Type = ACPI_TYPE_INTEGER;
1005 Arg.Integer.Value = (arg > val) ? IBM_CMOS_BRIGHTNESS_UP :
1006 IBM_CMOS_BRIGHTNESS_DOWN;
1008 step = (arg > val) ? 1 : -1;
1009 for (int i = val; i != arg; i += step) {
1010 status = AcpiEvaluateObject(sc->cmos_handle, NULL,
1012 if (ACPI_FAILURE(status)) {
1013 /* Record the last value */
1015 ACPI_EC_WRITE(sc->ec_dev,
1016 IBM_EC_BRIGHTNESS, i - step, 1);
1023 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_BRIGHTNESS, arg, 1);
1027 acpi_ibm_bluetooth_set(struct acpi_ibm_softc *sc, int arg)
1031 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1032 ACPI_SERIAL_ASSERT(ibm);
1034 if (arg < 0 || arg > 1)
1037 val = (arg == 1) ? sc->wlan_bt_flags | IBM_NAME_MASK_BT :
1038 sc->wlan_bt_flags & (~IBM_NAME_MASK_BT);
1039 return acpi_SetInteger(sc->handle, IBM_NAME_WLAN_BT_SET, val);
1043 acpi_ibm_thinklight_set(struct acpi_ibm_softc *sc, int arg)
1046 ACPI_OBJECT_LIST Args;
1049 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1050 ACPI_SERIAL_ASSERT(ibm);
1052 if (arg < 0 || arg > 1)
1055 if (sc->light_set_supported) {
1057 Args.Pointer = &Arg;
1058 Arg.Type = ACPI_TYPE_INTEGER;
1059 Arg.Integer.Value = arg ? sc->light_cmd_on : sc->light_cmd_off;
1061 status = AcpiEvaluateObject(sc->light_handle, NULL,
1063 if (ACPI_SUCCESS(status))
1064 sc->light_val = arg;
1072 acpi_ibm_volume_set(struct acpi_ibm_softc *sc, int arg)
1077 ACPI_OBJECT_LIST Args;
1080 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1081 ACPI_SERIAL_ASSERT(ibm);
1083 if (arg < 0 || arg > 14)
1086 /* Read the current volume */
1087 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
1088 if (ACPI_FAILURE(status))
1091 if (sc->cmos_handle) {
1092 val = val_ec & IBM_EC_MASK_VOL;
1095 Args.Pointer = &Arg;
1096 Arg.Type = ACPI_TYPE_INTEGER;
1097 Arg.Integer.Value = (arg > val) ? IBM_CMOS_VOLUME_UP :
1098 IBM_CMOS_VOLUME_DOWN;
1100 step = (arg > val) ? 1 : -1;
1101 for (int i = val; i != arg; i += step) {
1102 status = AcpiEvaluateObject(sc->cmos_handle, NULL,
1104 if (ACPI_FAILURE(status)) {
1105 /* Record the last value */
1108 (val_ec & (~IBM_EC_MASK_VOL));
1109 ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME,
1117 val_ec = arg + (val_ec & (~IBM_EC_MASK_VOL));
1118 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME, val_ec, 1);
1122 acpi_ibm_mute_set(struct acpi_ibm_softc *sc, int arg)
1126 ACPI_OBJECT_LIST Args;
1129 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1130 ACPI_SERIAL_ASSERT(ibm);
1132 if (arg < 0 || arg > 1)
1135 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
1136 if (ACPI_FAILURE(status))
1139 if (sc->cmos_handle) {
1141 Args.Pointer = &Arg;
1142 Arg.Type = ACPI_TYPE_INTEGER;
1143 Arg.Integer.Value = IBM_CMOS_VOLUME_MUTE;
1145 status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
1146 if (ACPI_FAILURE(status))
1150 val_ec = (arg == 1) ? val_ec | IBM_EC_MASK_MUTE :
1151 val_ec & (~IBM_EC_MASK_MUTE);
1152 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME, val_ec, 1);
1156 acpi_ibm_eventhandler(struct acpi_ibm_softc *sc, int arg)
1162 ACPI_SERIAL_BEGIN(ibm);
1164 case IBM_EVENT_SUSPEND_TO_RAM:
1165 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
1168 case IBM_EVENT_BLUETOOTH:
1169 acpi_ibm_bluetooth_set(sc, (sc->wlan_bt_flags == 0));
1172 case IBM_EVENT_BRIGHTNESS_UP:
1173 case IBM_EVENT_BRIGHTNESS_DOWN:
1174 /* Read the current brightness */
1175 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS,
1177 if (ACPI_FAILURE(status))
1180 val = val_ec & IBM_EC_MASK_BRI;
1181 val = (arg == IBM_EVENT_BRIGHTNESS_UP) ? val + 1 : val - 1;
1182 acpi_ibm_brightness_set(sc, val);
1185 case IBM_EVENT_THINKLIGHT:
1186 acpi_ibm_thinklight_set(sc, (sc->light_val == 0));
1189 case IBM_EVENT_VOLUME_UP:
1190 case IBM_EVENT_VOLUME_DOWN:
1191 /* Read the current volume */
1192 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
1193 if (ACPI_FAILURE(status))
1196 val = val_ec & IBM_EC_MASK_VOL;
1197 val = (arg == IBM_EVENT_VOLUME_UP) ? val + 1 : val - 1;
1198 acpi_ibm_volume_set(sc, val);
1201 case IBM_EVENT_MUTE:
1202 /* Read the current value */
1203 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
1204 if (ACPI_FAILURE(status))
1207 val = ((val_ec & IBM_EC_MASK_MUTE) == IBM_EC_MASK_MUTE);
1208 acpi_ibm_mute_set(sc, (val == 0));
1214 ACPI_SERIAL_END(ibm);
1218 acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context)
1220 int event, arg, type;
1221 device_t dev = context;
1222 struct acpi_ibm_softc *sc = device_get_softc(dev);
1224 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
1227 device_printf(dev, "Unknown notify\n");
1230 acpi_GetInteger(acpi_get_handle(dev), IBM_NAME_EVENTS_GET, &event);
1236 type = (event >> 12) & 0xf;
1237 arg = event & 0xfff;
1240 if (!(sc->events_availmask & (1 << (arg - 1)))) {
1241 device_printf(dev, "Unknown key %d\n", arg);
1245 /* Execute event handler */
1246 if (sc->handler_events & (1 << (arg - 1)))
1247 acpi_ibm_eventhandler(sc, (arg & 0xff));
1249 /* Notify devd(8) */
1250 acpi_UserNotify("IBM", h, (arg & 0xff));