Home | History | Annotate | Line # | Download | only in acpi
thinkpad_acpi.c revision 1.24
      1 /* $NetBSD: thinkpad_acpi.c,v 1.24 2010/01/30 18:35:49 jruoho Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: thinkpad_acpi.c,v 1.24 2010/01/30 18:35:49 jruoho Exp $");
     31 
     32 #include <sys/types.h>
     33 #include <sys/param.h>
     34 #include <sys/malloc.h>
     35 #include <sys/buf.h>
     36 #include <sys/callout.h>
     37 #include <sys/kernel.h>
     38 #include <sys/device.h>
     39 #include <sys/pmf.h>
     40 #include <sys/queue.h>
     41 #include <sys/kmem.h>
     42 
     43 #include <dev/acpi/acpireg.h>
     44 #include <dev/acpi/acpivar.h>
     45 #include <dev/acpi/acpi_ecvar.h>
     46 
     47 #if defined(__i386__) || defined(__amd64__)
     48 #include <dev/isa/isareg.h>
     49 #include <machine/pio.h>
     50 #endif
     51 
     52 #define _COMPONENT		ACPI_RESOURCE_COMPONENT
     53 ACPI_MODULE_NAME		("thinkpad_acpi")
     54 
     55 #define	THINKPAD_NTEMPSENSORS	8
     56 #define	THINKPAD_NFANSENSORS	1
     57 #define	THINKPAD_NSENSORS	(THINKPAD_NTEMPSENSORS + THINKPAD_NFANSENSORS)
     58 
     59 typedef struct thinkpad_softc {
     60 	device_t		sc_dev;
     61 	device_t		sc_ecdev;
     62 	struct acpi_devnode	*sc_node;
     63 	ACPI_HANDLE		sc_cmoshdl;
     64 	bool			sc_cmoshdl_valid;
     65 
     66 #define	TP_PSW_SLEEP		0
     67 #define	TP_PSW_HIBERNATE	1
     68 #define	TP_PSW_DISPLAY_CYCLE	2
     69 #define	TP_PSW_LOCK_SCREEN	3
     70 #define	TP_PSW_BATTERY_INFO	4
     71 #define	TP_PSW_EJECT_BUTTON	5
     72 #define	TP_PSW_ZOOM_BUTTON	6
     73 #define	TP_PSW_VENDOR_BUTTON	7
     74 #define	TP_PSW_LAST		8
     75 	struct sysmon_pswitch	sc_smpsw[TP_PSW_LAST];
     76 	bool			sc_smpsw_valid;
     77 
     78 	struct sysmon_envsys	*sc_sme;
     79 	envsys_data_t		sc_sensor[THINKPAD_NSENSORS];
     80 
     81 	int			sc_display_state;
     82 } thinkpad_softc_t;
     83 
     84 /* Hotkey events */
     85 #define	THINKPAD_NOTIFY_FnF1		0x001
     86 #define	THINKPAD_NOTIFY_LockScreen	0x002
     87 #define	THINKPAD_NOTIFY_BatteryInfo	0x003
     88 #define	THINKPAD_NOTIFY_SleepButton	0x004
     89 #define	THINKPAD_NOTIFY_WirelessSwitch	0x005
     90 #define	THINKPAD_NOTIFY_FnF6		0x006
     91 #define	THINKPAD_NOTIFY_DisplayCycle	0x007
     92 #define	THINKPAD_NOTIFY_PointerSwitch	0x008
     93 #define	THINKPAD_NOTIFY_EjectButton	0x009
     94 #define	THINKPAD_NOTIFY_FnF10		0x00a
     95 #define	THINKPAD_NOTIFY_FnF11		0x00b
     96 #define	THINKPAD_NOTIFY_HibernateButton	0x00c
     97 #define	THINKPAD_NOTIFY_BrightnessUp	0x010
     98 #define	THINKPAD_NOTIFY_BrightnessDown	0x011
     99 #define	THINKPAD_NOTIFY_ThinkLight	0x012
    100 #define	THINKPAD_NOTIFY_Zoom		0x014
    101 #define	THINKPAD_NOTIFY_VolumeUp	0x015
    102 #define	THINKPAD_NOTIFY_VolumeDown	0x016
    103 #define	THINKPAD_NOTIFY_VolumeMute	0x017
    104 #define	THINKPAD_NOTIFY_ThinkVantage	0x018
    105 
    106 #define	THINKPAD_CMOS_BRIGHTNESS_UP	0x04
    107 #define	THINKPAD_CMOS_BRIGHTNESS_DOWN	0x05
    108 
    109 #define	THINKPAD_HKEY_VERSION		0x0100
    110 
    111 #define	THINKPAD_DISPLAY_LCD		0x01
    112 #define	THINKPAD_DISPLAY_CRT		0x02
    113 #define	THINKPAD_DISPLAY_DVI		0x08
    114 #define	THINKPAD_DISPLAY_ALL \
    115 	(THINKPAD_DISPLAY_LCD | THINKPAD_DISPLAY_CRT | THINKPAD_DISPLAY_DVI)
    116 
    117 static int	thinkpad_match(device_t, cfdata_t, void *);
    118 static void	thinkpad_attach(device_t, device_t, void *);
    119 
    120 static ACPI_STATUS thinkpad_mask_init(thinkpad_softc_t *, uint32_t);
    121 static void	thinkpad_notify_handler(ACPI_HANDLE, UINT32, void *);
    122 static void	thinkpad_get_hotkeys(void *);
    123 
    124 static void	thinkpad_sensors_init(thinkpad_softc_t *);
    125 static void	thinkpad_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
    126 static void	thinkpad_temp_refresh(struct sysmon_envsys *, envsys_data_t *);
    127 static void	thinkpad_fan_refresh(struct sysmon_envsys *, envsys_data_t *);
    128 
    129 static void	thinkpad_wireless_toggle(thinkpad_softc_t *);
    130 
    131 static bool	thinkpad_resume(device_t, pmf_qual_t);
    132 static void	thinkpad_brightness_up(device_t);
    133 static void	thinkpad_brightness_down(device_t);
    134 static uint8_t	thinkpad_brightness_read(thinkpad_softc_t *sc);
    135 static void	thinkpad_cmos(thinkpad_softc_t *, uint8_t);
    136 
    137 CFATTACH_DECL_NEW(thinkpad, sizeof(thinkpad_softc_t),
    138     thinkpad_match, thinkpad_attach, NULL, NULL);
    139 
    140 static const char * const thinkpad_ids[] = {
    141 	"IBM0068",
    142 	NULL
    143 };
    144 
    145 static int
    146 thinkpad_match(device_t parent, cfdata_t match, void *opaque)
    147 {
    148 	struct acpi_attach_args *aa = (struct acpi_attach_args *)opaque;
    149 	ACPI_INTEGER ver;
    150 
    151 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    152 		return 0;
    153 
    154 	if (!acpi_match_hid(aa->aa_node->ad_devinfo, thinkpad_ids))
    155 		return 0;
    156 
    157 	/* We only support hotkey version 0x0100 */
    158 	if (ACPI_FAILURE(acpi_eval_integer(aa->aa_node->ad_handle, "MHKV",
    159 	    &ver)))
    160 		return 0;
    161 
    162 	if (ver != THINKPAD_HKEY_VERSION)
    163 		return 0;
    164 
    165 	/* Cool, looks like we're good to go */
    166 	return 1;
    167 }
    168 
    169 static void
    170 thinkpad_attach(device_t parent, device_t self, void *opaque)
    171 {
    172 	thinkpad_softc_t *sc = device_private(self);
    173 	struct acpi_attach_args *aa = (struct acpi_attach_args *)opaque;
    174 	struct sysmon_pswitch *psw;
    175 	device_t curdev;
    176 	deviter_t di;
    177 	ACPI_STATUS rv;
    178 	ACPI_INTEGER val;
    179 	int i;
    180 
    181 	sc->sc_node = aa->aa_node;
    182 	sc->sc_dev = self;
    183 	sc->sc_display_state = THINKPAD_DISPLAY_LCD;
    184 
    185 	aprint_naive("\n");
    186 	aprint_normal("\n");
    187 
    188 	/* T61 uses \UCMS method for issuing CMOS commands */
    189 	rv = AcpiGetHandle(NULL, "\\UCMS", &sc->sc_cmoshdl);
    190 	if (ACPI_FAILURE(rv))
    191 		sc->sc_cmoshdl_valid = false;
    192 	else {
    193 		aprint_debug_dev(self, "using CMOS at \\UCMS\n");
    194 		sc->sc_cmoshdl_valid = true;
    195 	}
    196 
    197 	sc->sc_ecdev = NULL;
    198 	for (curdev = deviter_first(&di, DEVITER_F_ROOT_FIRST);
    199 	     curdev != NULL; curdev = deviter_next(&di))
    200 		if (device_is_a(curdev, "acpiecdt") ||
    201 		    device_is_a(curdev, "acpiec")) {
    202 			sc->sc_ecdev = curdev;
    203 			break;
    204 		}
    205 	deviter_release(&di);
    206 
    207 	if (sc->sc_ecdev)
    208 		aprint_debug_dev(self, "using EC at %s\n",
    209 		    device_xname(sc->sc_ecdev));
    210 
    211 	/* Get the supported event mask */
    212 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "MHKA", &val);
    213 	if (ACPI_FAILURE(rv)) {
    214 		aprint_error_dev(self, "couldn't evaluate MHKA: %s\n",
    215 		    AcpiFormatException(rv));
    216 		goto fail;
    217 	}
    218 
    219 	/* Enable all supported events */
    220 	rv = thinkpad_mask_init(sc, val);
    221 	if (ACPI_FAILURE(rv)) {
    222 		aprint_error_dev(self, "couldn't set event mask: %s\n",
    223 		    AcpiFormatException(rv));
    224 		goto fail;
    225 	}
    226 
    227 	/* Install notify handler for events */
    228 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
    229 	    ACPI_DEVICE_NOTIFY, thinkpad_notify_handler, sc);
    230 	if (ACPI_FAILURE(rv))
    231 		aprint_error_dev(self, "couldn't install notify handler: %s\n",
    232 		    AcpiFormatException(rv));
    233 
    234 	/* Register power switches with sysmon */
    235 	psw = sc->sc_smpsw;
    236 	sc->sc_smpsw_valid = true;
    237 
    238 	psw[TP_PSW_SLEEP].smpsw_name = device_xname(self);
    239 	psw[TP_PSW_SLEEP].smpsw_type = PSWITCH_TYPE_SLEEP;
    240 #if notyet
    241 	psw[TP_PSW_HIBERNATE].smpsw_name = device_xname(self);
    242 	mpsw[TP_PSW_HIBERNATE].smpsw_type = PSWITCH_TYPE_HIBERNATE;
    243 #endif
    244 	for (i = TP_PSW_DISPLAY_CYCLE; i < TP_PSW_LAST; i++)
    245 		sc->sc_smpsw[i].smpsw_type = PSWITCH_TYPE_HOTKEY;
    246 	psw[TP_PSW_DISPLAY_CYCLE].smpsw_name = PSWITCH_HK_DISPLAY_CYCLE;
    247 	psw[TP_PSW_LOCK_SCREEN].smpsw_name = PSWITCH_HK_LOCK_SCREEN;
    248 	psw[TP_PSW_BATTERY_INFO].smpsw_name = PSWITCH_HK_BATTERY_INFO;
    249 	psw[TP_PSW_EJECT_BUTTON].smpsw_name = PSWITCH_HK_EJECT_BUTTON;
    250 	psw[TP_PSW_ZOOM_BUTTON].smpsw_name = PSWITCH_HK_ZOOM_BUTTON;
    251 	psw[TP_PSW_VENDOR_BUTTON].smpsw_name = PSWITCH_HK_VENDOR_BUTTON;
    252 
    253 	for (i = 0; i < TP_PSW_LAST; i++) {
    254 		/* not supported yet */
    255 		if (i == TP_PSW_HIBERNATE)
    256 			continue;
    257 		if (sysmon_pswitch_register(&sc->sc_smpsw[i]) != 0) {
    258 			aprint_error_dev(self,
    259 			    "couldn't register with sysmon\n");
    260 			sc->sc_smpsw_valid = false;
    261 			break;
    262 		}
    263 	}
    264 
    265 	/* Register temperature and fan sensors with envsys */
    266 	thinkpad_sensors_init(sc);
    267 
    268 fail:
    269 	if (!pmf_device_register(self, NULL, thinkpad_resume))
    270 		aprint_error_dev(self, "couldn't establish power handler\n");
    271 	if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_UP,
    272 	    thinkpad_brightness_up, true))
    273 		aprint_error_dev(self, "couldn't register event handler\n");
    274 	if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_DOWN,
    275 	    thinkpad_brightness_down, true))
    276 		aprint_error_dev(self, "couldn't register event handler\n");
    277 }
    278 
    279 static void
    280 thinkpad_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
    281 {
    282 	thinkpad_softc_t *sc = (thinkpad_softc_t *)opaque;
    283 	device_t self = sc->sc_dev;
    284 	ACPI_STATUS rv;
    285 
    286 	if (notify != 0x80) {
    287 		aprint_debug_dev(self, "unknown notify 0x%02x\n", notify);
    288 		return;
    289 	}
    290 
    291 	rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, thinkpad_get_hotkeys, sc);
    292 	if (ACPI_FAILURE(rv))
    293 		aprint_error_dev(self, "couldn't queue hotkey handler: %s\n",
    294 		    AcpiFormatException(rv));
    295 }
    296 
    297 static void
    298 thinkpad_get_hotkeys(void *opaque)
    299 {
    300 	thinkpad_softc_t *sc = (thinkpad_softc_t *)opaque;
    301 	device_t self = sc->sc_dev;
    302 	ACPI_STATUS rv;
    303 	ACPI_INTEGER val;
    304 	int type, event;
    305 
    306 	for (;;) {
    307 		rv = acpi_eval_integer(sc->sc_node->ad_handle, "MHKP", &val);
    308 		if (ACPI_FAILURE(rv)) {
    309 			aprint_error_dev(self, "couldn't evaluate MHKP: %s\n",
    310 			    AcpiFormatException(rv));
    311 			return;
    312 		}
    313 
    314 		if (val == 0)
    315 			return;
    316 
    317 		type = (val & 0xf000) >> 12;
    318 		event = val & 0x0fff;
    319 
    320 		if (type != 1)
    321 			/* Only type 1 events are supported for now */
    322 			continue;
    323 
    324 		switch (event) {
    325 		case THINKPAD_NOTIFY_BrightnessUp:
    326 			thinkpad_brightness_up(self);
    327 			break;
    328 		case THINKPAD_NOTIFY_BrightnessDown:
    329 			thinkpad_brightness_down(self);
    330 			break;
    331 		case THINKPAD_NOTIFY_WirelessSwitch:
    332 			thinkpad_wireless_toggle(sc);
    333 			break;
    334 		case THINKPAD_NOTIFY_SleepButton:
    335 			if (sc->sc_smpsw_valid == false)
    336 				break;
    337 			sysmon_pswitch_event(&sc->sc_smpsw[TP_PSW_SLEEP],
    338 			    PSWITCH_EVENT_PRESSED);
    339 			break;
    340 		case THINKPAD_NOTIFY_HibernateButton:
    341 #if notyet
    342 			if (sc->sc_smpsw_valid == false)
    343 				break;
    344 			sysmon_pswitch_event(&sc->sc_smpsw[TP_PSW_HIBERNATE],
    345 			    PSWITCH_EVENT_PRESSED);
    346 #endif
    347 			break;
    348 		case THINKPAD_NOTIFY_DisplayCycle:
    349 			if (sc->sc_smpsw_valid == false)
    350 				break;
    351 			sysmon_pswitch_event(
    352 			    &sc->sc_smpsw[TP_PSW_DISPLAY_CYCLE],
    353 			    PSWITCH_EVENT_PRESSED);
    354 			break;
    355 		case THINKPAD_NOTIFY_LockScreen:
    356 			if (sc->sc_smpsw_valid == false)
    357 				break;
    358 			sysmon_pswitch_event(
    359 			    &sc->sc_smpsw[TP_PSW_LOCK_SCREEN],
    360 			    PSWITCH_EVENT_PRESSED);
    361 			break;
    362 		case THINKPAD_NOTIFY_BatteryInfo:
    363 			if (sc->sc_smpsw_valid == false)
    364 				break;
    365 			sysmon_pswitch_event(
    366 			    &sc->sc_smpsw[TP_PSW_BATTERY_INFO],
    367 			    PSWITCH_EVENT_PRESSED);
    368 			break;
    369 		case THINKPAD_NOTIFY_EjectButton:
    370 			if (sc->sc_smpsw_valid == false)
    371 				break;
    372 			sysmon_pswitch_event(
    373 			    &sc->sc_smpsw[TP_PSW_EJECT_BUTTON],
    374 			    PSWITCH_EVENT_PRESSED);
    375 			break;
    376 		case THINKPAD_NOTIFY_Zoom:
    377 			if (sc->sc_smpsw_valid == false)
    378 				break;
    379 			sysmon_pswitch_event(
    380 			    &sc->sc_smpsw[TP_PSW_ZOOM_BUTTON],
    381 			    PSWITCH_EVENT_PRESSED);
    382 			break;
    383 		case THINKPAD_NOTIFY_ThinkVantage:
    384 			if (sc->sc_smpsw_valid == false)
    385 				break;
    386 			sysmon_pswitch_event(
    387 			    &sc->sc_smpsw[TP_PSW_VENDOR_BUTTON],
    388 			    PSWITCH_EVENT_PRESSED);
    389 			break;
    390 		case THINKPAD_NOTIFY_FnF1:
    391 		case THINKPAD_NOTIFY_FnF6:
    392 		case THINKPAD_NOTIFY_PointerSwitch:
    393 		case THINKPAD_NOTIFY_FnF10:
    394 		case THINKPAD_NOTIFY_FnF11:
    395 		case THINKPAD_NOTIFY_ThinkLight:
    396 		case THINKPAD_NOTIFY_VolumeUp:
    397 		case THINKPAD_NOTIFY_VolumeDown:
    398 		case THINKPAD_NOTIFY_VolumeMute:
    399 			/* XXXJDM we should deliver hotkeys as keycodes */
    400 			break;
    401 		default:
    402 			aprint_debug_dev(self, "notify event 0x%03x\n", event);
    403 			break;
    404 		}
    405 	}
    406 }
    407 
    408 static ACPI_STATUS
    409 thinkpad_mask_init(thinkpad_softc_t *sc, uint32_t mask)
    410 {
    411 	ACPI_OBJECT param[2];
    412 	ACPI_OBJECT_LIST params;
    413 	ACPI_STATUS rv;
    414 	int i;
    415 
    416 	/* Update hotkey mask */
    417 	params.Count = 2;
    418 	params.Pointer = param;
    419 	param[0].Type = param[1].Type = ACPI_TYPE_INTEGER;
    420 
    421 	for (i = 0; i < 32; i++) {
    422 		param[0].Integer.Value = i + 1;
    423 		param[1].Integer.Value = (((1 << i) & mask) != 0);
    424 
    425 		rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "MHKM",
    426 		    &params, NULL);
    427 		if (ACPI_FAILURE(rv))
    428 			return rv;
    429 	}
    430 
    431 	/* Enable hotkey events */
    432 	rv = acpi_eval_set_integer(sc->sc_node->ad_handle, "MHKC", 1);
    433 	if (ACPI_FAILURE(rv)) {
    434 		aprint_error_dev(sc->sc_dev, "couldn't enable hotkeys: %s\n",
    435 		    AcpiFormatException(rv));
    436 		return rv;
    437 	}
    438 
    439 	/* Claim ownership of brightness control */
    440 	(void)acpi_eval_set_integer(sc->sc_node->ad_handle, "PWMS", 0);
    441 
    442 	return AE_OK;
    443 }
    444 
    445 static void
    446 thinkpad_sensors_init(thinkpad_softc_t *sc)
    447 {
    448 	char sname[5] = "TMP?";
    449 	char fname[5] = "FAN?";
    450 	int i, j, err;
    451 
    452 	if (sc->sc_ecdev == NULL)
    453 		return;	/* no chance of this working */
    454 
    455 	sc->sc_sme = sysmon_envsys_create();
    456 	for (i = 0; i < THINKPAD_NTEMPSENSORS; i++) {
    457 		sname[3] = '0' + i;
    458 		strcpy(sc->sc_sensor[i].desc, sname);
    459 		sc->sc_sensor[i].units = ENVSYS_STEMP;
    460 
    461 		if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[i]))
    462 			aprint_error_dev(sc->sc_dev,
    463 			    "couldn't attach sensor %s\n", sname);
    464 	}
    465 	j = i; /* THINKPAD_NTEMPSENSORS */
    466 	for (; i < (j + THINKPAD_NFANSENSORS); i++) {
    467 		fname[3] = '0' + (i - j);
    468 		strcpy(sc->sc_sensor[i].desc, fname);
    469 		sc->sc_sensor[i].units = ENVSYS_SFANRPM;
    470 
    471 		if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[i]))
    472 			aprint_error_dev(sc->sc_dev,
    473 			    "couldn't attach sensor %s\n", fname);
    474 	}
    475 
    476 	sc->sc_sme->sme_name = device_xname(sc->sc_dev);
    477 	sc->sc_sme->sme_cookie = sc;
    478 	sc->sc_sme->sme_refresh = thinkpad_sensors_refresh;
    479 
    480 	err = sysmon_envsys_register(sc->sc_sme);
    481 	if (err) {
    482 		aprint_error_dev(sc->sc_dev,
    483 		    "couldn't register with sysmon: %d\n", err);
    484 		sysmon_envsys_destroy(sc->sc_sme);
    485 	}
    486 }
    487 
    488 static void
    489 thinkpad_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    490 {
    491 	switch (edata->units) {
    492 	case ENVSYS_STEMP:
    493 		thinkpad_temp_refresh(sme, edata);
    494 		break;
    495 	case ENVSYS_SFANRPM:
    496 		thinkpad_fan_refresh(sme, edata);
    497 		break;
    498 	default:
    499 		break;
    500 	}
    501 }
    502 
    503 static void
    504 thinkpad_temp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    505 {
    506 	thinkpad_softc_t *sc = sme->sme_cookie;
    507 	char sname[5] = "TMP?";
    508 	ACPI_INTEGER val;
    509 	ACPI_STATUS rv;
    510 	int temp;
    511 
    512 	sname[3] = '0' + edata->sensor;
    513 	rv = acpi_eval_integer(acpiec_get_handle(sc->sc_ecdev), sname, &val);
    514 	if (ACPI_FAILURE(rv)) {
    515 		edata->state = ENVSYS_SINVALID;
    516 		return;
    517 	}
    518 	temp = (int)val;
    519 	if (temp > 127 || temp < -127) {
    520 		edata->state = ENVSYS_SINVALID;
    521 		return;
    522 	}
    523 
    524 	edata->value_cur = temp * 1000000 + 273150000;
    525 	edata->state = ENVSYS_SVALID;
    526 }
    527 
    528 static void
    529 thinkpad_fan_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    530 {
    531 	thinkpad_softc_t *sc = sme->sme_cookie;
    532 	ACPI_INTEGER lo;
    533 	ACPI_INTEGER hi;
    534 	ACPI_STATUS rv;
    535 	int rpm;
    536 
    537 	/*
    538 	 * Read the low byte first to avoid a firmware bug.
    539 	 */
    540 	rv = acpiec_bus_read(sc->sc_ecdev, 0x84, &lo, 1);
    541 	if (ACPI_FAILURE(rv)) {
    542 		edata->state = ENVSYS_SINVALID;
    543 		return;
    544 	}
    545 	rv = acpiec_bus_read(sc->sc_ecdev, 0x85, &hi, 1);
    546 	if (ACPI_FAILURE(rv)) {
    547 		edata->state = ENVSYS_SINVALID;
    548 		return;
    549 	}
    550 	rpm = ((((int)hi) << 8) | ((int)lo));
    551 	if (rpm < 0) {
    552 		edata->state = ENVSYS_SINVALID;
    553 		return;
    554 	}
    555 
    556 	edata->value_cur = rpm;
    557 	if (rpm < edata->value_min || edata->value_min == -1)
    558 		edata->value_min = rpm;
    559 	if (rpm > edata->value_max || edata->value_max == -1)
    560 		edata->value_max = rpm;
    561 	edata->state = ENVSYS_SVALID;
    562 }
    563 
    564 static void
    565 thinkpad_wireless_toggle(thinkpad_softc_t *sc)
    566 {
    567 	/* Ignore return value, as the hardware may not support bluetooth */
    568 	(void)AcpiEvaluateObject(sc->sc_node->ad_handle, "BTGL", NULL, NULL);
    569 }
    570 
    571 static uint8_t
    572 thinkpad_brightness_read(thinkpad_softc_t *sc)
    573 {
    574 #if defined(__i386__) || defined(__amd64__)
    575 	/*
    576 	 * We have two ways to get the current brightness -- either via
    577 	 * magic RTC registers, or using the EC. Since I don't dare mess
    578 	 * with the EC, and Thinkpads are x86-only, this will have to do
    579 	 * for now.
    580 	 */
    581 	outb(IO_RTC, 0x6c);
    582 	return inb(IO_RTC+1) & 7;
    583 #else
    584 	return 0;
    585 #endif
    586 }
    587 
    588 static void
    589 thinkpad_brightness_up(device_t self)
    590 {
    591 	thinkpad_softc_t *sc = device_private(self);
    592 
    593 	if (thinkpad_brightness_read(sc) == 7)
    594 		return;
    595 	thinkpad_cmos(sc, THINKPAD_CMOS_BRIGHTNESS_UP);
    596 }
    597 
    598 static void
    599 thinkpad_brightness_down(device_t self)
    600 {
    601 	thinkpad_softc_t *sc = device_private(self);
    602 
    603 	if (thinkpad_brightness_read(sc) == 0)
    604 		return;
    605 	thinkpad_cmos(sc, THINKPAD_CMOS_BRIGHTNESS_DOWN);
    606 }
    607 
    608 static void
    609 thinkpad_cmos(thinkpad_softc_t *sc, uint8_t cmd)
    610 {
    611 	ACPI_STATUS rv;
    612 
    613 	if (sc->sc_cmoshdl_valid == false)
    614 		return;
    615 
    616 	rv = acpi_eval_set_integer(sc->sc_cmoshdl, NULL, cmd);
    617 	if (ACPI_FAILURE(rv))
    618 		aprint_error_dev(sc->sc_dev, "couldn't evalute CMOS: %s\n",
    619 		    AcpiFormatException(rv));
    620 }
    621 
    622 static bool
    623 thinkpad_resume(device_t dv, pmf_qual_t qual)
    624 {
    625 	ACPI_STATUS rv;
    626 	ACPI_HANDLE pubs;
    627 
    628 	rv = AcpiGetHandle(NULL, "\\_SB.PCI0.LPC.EC.PUBS", &pubs);
    629 	if (ACPI_FAILURE(rv))
    630 		return true;	/* not fatal */
    631 
    632 	rv = AcpiEvaluateObject(pubs, "_ON", NULL, NULL);
    633 	if (ACPI_FAILURE(rv))
    634 		aprint_error_dev(dv, "failed to execute PUBS._ON: %s\n",
    635 		    AcpiFormatException(rv));
    636 
    637 	return true;
    638 }
    639