Home | History | Annotate | Line # | Download | only in acpi
thinkpad_acpi.c revision 1.27
      1 /* $NetBSD: thinkpad_acpi.c,v 1.27 2010/02/28 17:22:41 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.27 2010/02/28 17:22:41 jruoho Exp $");
     31 
     32 #include <sys/types.h>
     33 #include <sys/param.h>
     34 #include <sys/malloc.h>
     35 #include <sys/module.h>
     36 #include <sys/buf.h>
     37 #include <sys/callout.h>
     38 #include <sys/kernel.h>
     39 #include <sys/device.h>
     40 #include <sys/pmf.h>
     41 #include <sys/queue.h>
     42 #include <sys/kmem.h>
     43 
     44 #include <dev/acpi/acpireg.h>
     45 #include <dev/acpi/acpivar.h>
     46 #include <dev/acpi/acpi_ecvar.h>
     47 
     48 #if defined(__i386__) || defined(__amd64__)
     49 #include <dev/isa/isareg.h>
     50 #include <machine/pio.h>
     51 #endif
     52 
     53 #define _COMPONENT		ACPI_RESOURCE_COMPONENT
     54 ACPI_MODULE_NAME		("thinkpad_acpi")
     55 
     56 #define	THINKPAD_NTEMPSENSORS	8
     57 #define	THINKPAD_NFANSENSORS	1
     58 #define	THINKPAD_NSENSORS	(THINKPAD_NTEMPSENSORS + THINKPAD_NFANSENSORS)
     59 
     60 typedef struct thinkpad_softc {
     61 	device_t		sc_dev;
     62 	device_t		sc_ecdev;
     63 	struct acpi_devnode	*sc_node;
     64 	ACPI_HANDLE		sc_cmoshdl;
     65 	bool			sc_cmoshdl_valid;
     66 
     67 #define	TP_PSW_SLEEP		0
     68 #define	TP_PSW_HIBERNATE	1
     69 #define	TP_PSW_DISPLAY_CYCLE	2
     70 #define	TP_PSW_LOCK_SCREEN	3
     71 #define	TP_PSW_BATTERY_INFO	4
     72 #define	TP_PSW_EJECT_BUTTON	5
     73 #define	TP_PSW_ZOOM_BUTTON	6
     74 #define	TP_PSW_VENDOR_BUTTON	7
     75 #define	TP_PSW_LAST		8
     76 	struct sysmon_pswitch	sc_smpsw[TP_PSW_LAST];
     77 	bool			sc_smpsw_valid;
     78 
     79 	struct sysmon_envsys	*sc_sme;
     80 	envsys_data_t		sc_sensor[THINKPAD_NSENSORS];
     81 
     82 	int			sc_display_state;
     83 } thinkpad_softc_t;
     84 
     85 /* Hotkey events */
     86 #define	THINKPAD_NOTIFY_FnF1		0x001
     87 #define	THINKPAD_NOTIFY_LockScreen	0x002
     88 #define	THINKPAD_NOTIFY_BatteryInfo	0x003
     89 #define	THINKPAD_NOTIFY_SleepButton	0x004
     90 #define	THINKPAD_NOTIFY_WirelessSwitch	0x005
     91 #define	THINKPAD_NOTIFY_FnF6		0x006
     92 #define	THINKPAD_NOTIFY_DisplayCycle	0x007
     93 #define	THINKPAD_NOTIFY_PointerSwitch	0x008
     94 #define	THINKPAD_NOTIFY_EjectButton	0x009
     95 #define	THINKPAD_NOTIFY_FnF10		0x00a
     96 #define	THINKPAD_NOTIFY_FnF11		0x00b
     97 #define	THINKPAD_NOTIFY_HibernateButton	0x00c
     98 #define	THINKPAD_NOTIFY_BrightnessUp	0x010
     99 #define	THINKPAD_NOTIFY_BrightnessDown	0x011
    100 #define	THINKPAD_NOTIFY_ThinkLight	0x012
    101 #define	THINKPAD_NOTIFY_Zoom		0x014
    102 #define	THINKPAD_NOTIFY_VolumeUp	0x015
    103 #define	THINKPAD_NOTIFY_VolumeDown	0x016
    104 #define	THINKPAD_NOTIFY_VolumeMute	0x017
    105 #define	THINKPAD_NOTIFY_ThinkVantage	0x018
    106 
    107 #define	THINKPAD_CMOS_BRIGHTNESS_UP	0x04
    108 #define	THINKPAD_CMOS_BRIGHTNESS_DOWN	0x05
    109 
    110 #define	THINKPAD_HKEY_VERSION		0x0100
    111 
    112 #define	THINKPAD_DISPLAY_LCD		0x01
    113 #define	THINKPAD_DISPLAY_CRT		0x02
    114 #define	THINKPAD_DISPLAY_DVI		0x08
    115 #define	THINKPAD_DISPLAY_ALL \
    116 	(THINKPAD_DISPLAY_LCD | THINKPAD_DISPLAY_CRT | THINKPAD_DISPLAY_DVI)
    117 
    118 static int	thinkpad_match(device_t, cfdata_t, void *);
    119 static void	thinkpad_attach(device_t, device_t, void *);
    120 static int	thinkpad_detach(device_t, int);
    121 
    122 static ACPI_STATUS thinkpad_mask_init(thinkpad_softc_t *, uint32_t);
    123 static void	thinkpad_notify_handler(ACPI_HANDLE, UINT32, void *);
    124 static void	thinkpad_get_hotkeys(void *);
    125 
    126 static void	thinkpad_sensors_init(thinkpad_softc_t *);
    127 static void	thinkpad_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
    128 static void	thinkpad_temp_refresh(struct sysmon_envsys *, envsys_data_t *);
    129 static void	thinkpad_fan_refresh(struct sysmon_envsys *, envsys_data_t *);
    130 
    131 static void	thinkpad_wireless_toggle(thinkpad_softc_t *);
    132 
    133 static bool	thinkpad_resume(device_t, const pmf_qual_t *);
    134 static void	thinkpad_brightness_up(device_t);
    135 static void	thinkpad_brightness_down(device_t);
    136 static uint8_t	thinkpad_brightness_read(thinkpad_softc_t *sc);
    137 static void	thinkpad_cmos(thinkpad_softc_t *, uint8_t);
    138 
    139 CFATTACH_DECL_NEW(thinkpad, sizeof(thinkpad_softc_t),
    140     thinkpad_match, thinkpad_attach, thinkpad_detach, NULL);
    141 
    142 static const char * const thinkpad_ids[] = {
    143 	"IBM0068",
    144 	NULL
    145 };
    146 
    147 static int
    148 thinkpad_match(device_t parent, cfdata_t match, void *opaque)
    149 {
    150 	struct acpi_attach_args *aa = (struct acpi_attach_args *)opaque;
    151 	ACPI_INTEGER ver;
    152 
    153 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    154 		return 0;
    155 
    156 	if (!acpi_match_hid(aa->aa_node->ad_devinfo, thinkpad_ids))
    157 		return 0;
    158 
    159 	/* We only support hotkey version 0x0100 */
    160 	if (ACPI_FAILURE(acpi_eval_integer(aa->aa_node->ad_handle, "MHKV",
    161 	    &ver)))
    162 		return 0;
    163 
    164 	if (ver != THINKPAD_HKEY_VERSION)
    165 		return 0;
    166 
    167 	/* Cool, looks like we're good to go */
    168 	return 1;
    169 }
    170 
    171 static void
    172 thinkpad_attach(device_t parent, device_t self, void *opaque)
    173 {
    174 	thinkpad_softc_t *sc = device_private(self);
    175 	struct acpi_attach_args *aa = (struct acpi_attach_args *)opaque;
    176 	struct sysmon_pswitch *psw;
    177 	device_t curdev;
    178 	deviter_t di;
    179 	ACPI_STATUS rv;
    180 	ACPI_INTEGER val;
    181 	int i;
    182 
    183 	sc->sc_node = aa->aa_node;
    184 	sc->sc_dev = self;
    185 	sc->sc_display_state = THINKPAD_DISPLAY_LCD;
    186 
    187 	aprint_naive("\n");
    188 	aprint_normal("\n");
    189 
    190 	/* T61 uses \UCMS method for issuing CMOS commands */
    191 	rv = AcpiGetHandle(NULL, "\\UCMS", &sc->sc_cmoshdl);
    192 	if (ACPI_FAILURE(rv))
    193 		sc->sc_cmoshdl_valid = false;
    194 	else {
    195 		aprint_debug_dev(self, "using CMOS at \\UCMS\n");
    196 		sc->sc_cmoshdl_valid = true;
    197 	}
    198 
    199 	sc->sc_ecdev = NULL;
    200 	for (curdev = deviter_first(&di, DEVITER_F_ROOT_FIRST);
    201 	     curdev != NULL; curdev = deviter_next(&di))
    202 		if (device_is_a(curdev, "acpiecdt") ||
    203 		    device_is_a(curdev, "acpiec")) {
    204 			sc->sc_ecdev = curdev;
    205 			break;
    206 		}
    207 	deviter_release(&di);
    208 
    209 	if (sc->sc_ecdev)
    210 		aprint_debug_dev(self, "using EC at %s\n",
    211 		    device_xname(sc->sc_ecdev));
    212 
    213 	/* Get the supported event mask */
    214 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "MHKA", &val);
    215 	if (ACPI_FAILURE(rv)) {
    216 		aprint_error_dev(self, "couldn't evaluate MHKA: %s\n",
    217 		    AcpiFormatException(rv));
    218 		goto fail;
    219 	}
    220 
    221 	/* Enable all supported events */
    222 	rv = thinkpad_mask_init(sc, val);
    223 	if (ACPI_FAILURE(rv)) {
    224 		aprint_error_dev(self, "couldn't set event mask: %s\n",
    225 		    AcpiFormatException(rv));
    226 		goto fail;
    227 	}
    228 
    229 	/* Install notify handler for events */
    230 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
    231 	    ACPI_DEVICE_NOTIFY, thinkpad_notify_handler, sc);
    232 	if (ACPI_FAILURE(rv))
    233 		aprint_error_dev(self, "couldn't install notify handler: %s\n",
    234 		    AcpiFormatException(rv));
    235 
    236 	/* Register power switches with sysmon */
    237 	psw = sc->sc_smpsw;
    238 	sc->sc_smpsw_valid = true;
    239 
    240 	psw[TP_PSW_SLEEP].smpsw_name = device_xname(self);
    241 	psw[TP_PSW_SLEEP].smpsw_type = PSWITCH_TYPE_SLEEP;
    242 #if notyet
    243 	psw[TP_PSW_HIBERNATE].smpsw_name = device_xname(self);
    244 	mpsw[TP_PSW_HIBERNATE].smpsw_type = PSWITCH_TYPE_HIBERNATE;
    245 #endif
    246 	for (i = TP_PSW_DISPLAY_CYCLE; i < TP_PSW_LAST; i++)
    247 		sc->sc_smpsw[i].smpsw_type = PSWITCH_TYPE_HOTKEY;
    248 	psw[TP_PSW_DISPLAY_CYCLE].smpsw_name = PSWITCH_HK_DISPLAY_CYCLE;
    249 	psw[TP_PSW_LOCK_SCREEN].smpsw_name = PSWITCH_HK_LOCK_SCREEN;
    250 	psw[TP_PSW_BATTERY_INFO].smpsw_name = PSWITCH_HK_BATTERY_INFO;
    251 	psw[TP_PSW_EJECT_BUTTON].smpsw_name = PSWITCH_HK_EJECT_BUTTON;
    252 	psw[TP_PSW_ZOOM_BUTTON].smpsw_name = PSWITCH_HK_ZOOM_BUTTON;
    253 	psw[TP_PSW_VENDOR_BUTTON].smpsw_name = PSWITCH_HK_VENDOR_BUTTON;
    254 
    255 	for (i = 0; i < TP_PSW_LAST; i++) {
    256 		/* not supported yet */
    257 		if (i == TP_PSW_HIBERNATE)
    258 			continue;
    259 		if (sysmon_pswitch_register(&sc->sc_smpsw[i]) != 0) {
    260 			aprint_error_dev(self,
    261 			    "couldn't register with sysmon\n");
    262 			sc->sc_smpsw_valid = false;
    263 			break;
    264 		}
    265 	}
    266 
    267 	/* Register temperature and fan sensors with envsys */
    268 	thinkpad_sensors_init(sc);
    269 
    270 fail:
    271 	if (!pmf_device_register(self, NULL, thinkpad_resume))
    272 		aprint_error_dev(self, "couldn't establish power handler\n");
    273 	if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_UP,
    274 	    thinkpad_brightness_up, true))
    275 		aprint_error_dev(self, "couldn't register event handler\n");
    276 	if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_DOWN,
    277 	    thinkpad_brightness_down, true))
    278 		aprint_error_dev(self, "couldn't register event handler\n");
    279 }
    280 
    281 static int
    282 thinkpad_detach(device_t self, int flags)
    283 {
    284 	struct thinkpad_softc *sc = device_private(self);
    285 	ACPI_STATUS rv;
    286 	int i;
    287 
    288 	rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle,
    289 	    ACPI_DEVICE_NOTIFY, thinkpad_notify_handler);
    290 
    291 	if (ACPI_FAILURE(rv))
    292 		return EBUSY;
    293 
    294 	for (i = 0; i < TP_PSW_LAST; i++)
    295 		sysmon_pswitch_unregister(&sc->sc_smpsw[i]);
    296 
    297 	if (sc->sc_sme != NULL)
    298 		sysmon_envsys_unregister(sc->sc_sme);
    299 
    300 	pmf_device_deregister(self);
    301 
    302 	pmf_event_deregister(self, PMFE_DISPLAY_BRIGHTNESS_UP,
    303 	    thinkpad_brightness_up, true);
    304 
    305 	pmf_event_deregister(self, PMFE_DISPLAY_BRIGHTNESS_DOWN,
    306 	    thinkpad_brightness_down, true);
    307 
    308 	return 0;
    309 }
    310 
    311 static void
    312 thinkpad_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
    313 {
    314 	thinkpad_softc_t *sc = (thinkpad_softc_t *)opaque;
    315 	device_t self = sc->sc_dev;
    316 	ACPI_STATUS rv;
    317 
    318 	if (notify != 0x80) {
    319 		aprint_debug_dev(self, "unknown notify 0x%02x\n", notify);
    320 		return;
    321 	}
    322 
    323 	rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, thinkpad_get_hotkeys, sc);
    324 	if (ACPI_FAILURE(rv))
    325 		aprint_error_dev(self, "couldn't queue hotkey handler: %s\n",
    326 		    AcpiFormatException(rv));
    327 }
    328 
    329 static void
    330 thinkpad_get_hotkeys(void *opaque)
    331 {
    332 	thinkpad_softc_t *sc = (thinkpad_softc_t *)opaque;
    333 	device_t self = sc->sc_dev;
    334 	ACPI_STATUS rv;
    335 	ACPI_INTEGER val;
    336 	int type, event;
    337 
    338 	for (;;) {
    339 		rv = acpi_eval_integer(sc->sc_node->ad_handle, "MHKP", &val);
    340 		if (ACPI_FAILURE(rv)) {
    341 			aprint_error_dev(self, "couldn't evaluate MHKP: %s\n",
    342 			    AcpiFormatException(rv));
    343 			return;
    344 		}
    345 
    346 		if (val == 0)
    347 			return;
    348 
    349 		type = (val & 0xf000) >> 12;
    350 		event = val & 0x0fff;
    351 
    352 		if (type != 1)
    353 			/* Only type 1 events are supported for now */
    354 			continue;
    355 
    356 		switch (event) {
    357 		case THINKPAD_NOTIFY_BrightnessUp:
    358 			thinkpad_brightness_up(self);
    359 			break;
    360 		case THINKPAD_NOTIFY_BrightnessDown:
    361 			thinkpad_brightness_down(self);
    362 			break;
    363 		case THINKPAD_NOTIFY_WirelessSwitch:
    364 			thinkpad_wireless_toggle(sc);
    365 			break;
    366 		case THINKPAD_NOTIFY_SleepButton:
    367 			if (sc->sc_smpsw_valid == false)
    368 				break;
    369 			sysmon_pswitch_event(&sc->sc_smpsw[TP_PSW_SLEEP],
    370 			    PSWITCH_EVENT_PRESSED);
    371 			break;
    372 		case THINKPAD_NOTIFY_HibernateButton:
    373 #if notyet
    374 			if (sc->sc_smpsw_valid == false)
    375 				break;
    376 			sysmon_pswitch_event(&sc->sc_smpsw[TP_PSW_HIBERNATE],
    377 			    PSWITCH_EVENT_PRESSED);
    378 #endif
    379 			break;
    380 		case THINKPAD_NOTIFY_DisplayCycle:
    381 			if (sc->sc_smpsw_valid == false)
    382 				break;
    383 			sysmon_pswitch_event(
    384 			    &sc->sc_smpsw[TP_PSW_DISPLAY_CYCLE],
    385 			    PSWITCH_EVENT_PRESSED);
    386 			break;
    387 		case THINKPAD_NOTIFY_LockScreen:
    388 			if (sc->sc_smpsw_valid == false)
    389 				break;
    390 			sysmon_pswitch_event(
    391 			    &sc->sc_smpsw[TP_PSW_LOCK_SCREEN],
    392 			    PSWITCH_EVENT_PRESSED);
    393 			break;
    394 		case THINKPAD_NOTIFY_BatteryInfo:
    395 			if (sc->sc_smpsw_valid == false)
    396 				break;
    397 			sysmon_pswitch_event(
    398 			    &sc->sc_smpsw[TP_PSW_BATTERY_INFO],
    399 			    PSWITCH_EVENT_PRESSED);
    400 			break;
    401 		case THINKPAD_NOTIFY_EjectButton:
    402 			if (sc->sc_smpsw_valid == false)
    403 				break;
    404 			sysmon_pswitch_event(
    405 			    &sc->sc_smpsw[TP_PSW_EJECT_BUTTON],
    406 			    PSWITCH_EVENT_PRESSED);
    407 			break;
    408 		case THINKPAD_NOTIFY_Zoom:
    409 			if (sc->sc_smpsw_valid == false)
    410 				break;
    411 			sysmon_pswitch_event(
    412 			    &sc->sc_smpsw[TP_PSW_ZOOM_BUTTON],
    413 			    PSWITCH_EVENT_PRESSED);
    414 			break;
    415 		case THINKPAD_NOTIFY_ThinkVantage:
    416 			if (sc->sc_smpsw_valid == false)
    417 				break;
    418 			sysmon_pswitch_event(
    419 			    &sc->sc_smpsw[TP_PSW_VENDOR_BUTTON],
    420 			    PSWITCH_EVENT_PRESSED);
    421 			break;
    422 		case THINKPAD_NOTIFY_FnF1:
    423 		case THINKPAD_NOTIFY_FnF6:
    424 		case THINKPAD_NOTIFY_PointerSwitch:
    425 		case THINKPAD_NOTIFY_FnF10:
    426 		case THINKPAD_NOTIFY_FnF11:
    427 		case THINKPAD_NOTIFY_ThinkLight:
    428 		case THINKPAD_NOTIFY_VolumeUp:
    429 		case THINKPAD_NOTIFY_VolumeDown:
    430 		case THINKPAD_NOTIFY_VolumeMute:
    431 			/* XXXJDM we should deliver hotkeys as keycodes */
    432 			break;
    433 		default:
    434 			aprint_debug_dev(self, "notify event 0x%03x\n", event);
    435 			break;
    436 		}
    437 	}
    438 }
    439 
    440 static ACPI_STATUS
    441 thinkpad_mask_init(thinkpad_softc_t *sc, uint32_t mask)
    442 {
    443 	ACPI_OBJECT param[2];
    444 	ACPI_OBJECT_LIST params;
    445 	ACPI_STATUS rv;
    446 	int i;
    447 
    448 	/* Update hotkey mask */
    449 	params.Count = 2;
    450 	params.Pointer = param;
    451 	param[0].Type = param[1].Type = ACPI_TYPE_INTEGER;
    452 
    453 	for (i = 0; i < 32; i++) {
    454 		param[0].Integer.Value = i + 1;
    455 		param[1].Integer.Value = (((1 << i) & mask) != 0);
    456 
    457 		rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "MHKM",
    458 		    &params, NULL);
    459 		if (ACPI_FAILURE(rv))
    460 			return rv;
    461 	}
    462 
    463 	/* Enable hotkey events */
    464 	rv = acpi_eval_set_integer(sc->sc_node->ad_handle, "MHKC", 1);
    465 	if (ACPI_FAILURE(rv)) {
    466 		aprint_error_dev(sc->sc_dev, "couldn't enable hotkeys: %s\n",
    467 		    AcpiFormatException(rv));
    468 		return rv;
    469 	}
    470 
    471 	/* Claim ownership of brightness control */
    472 	(void)acpi_eval_set_integer(sc->sc_node->ad_handle, "PWMS", 0);
    473 
    474 	return AE_OK;
    475 }
    476 
    477 static void
    478 thinkpad_sensors_init(thinkpad_softc_t *sc)
    479 {
    480 	int i, j;
    481 
    482 	if (sc->sc_ecdev == NULL)
    483 		return;	/* no chance of this working */
    484 
    485 	sc->sc_sme = sysmon_envsys_create();
    486 
    487 	for (i = j = 0; i < THINKPAD_NTEMPSENSORS; i++) {
    488 
    489 		sc->sc_sensor[i].units = ENVSYS_STEMP;
    490 
    491 		(void)snprintf(sc->sc_sensor[i].desc,
    492 		    sizeof(sc->sc_sensor[i].desc), "TMP%d", i);
    493 
    494 		if (sysmon_envsys_sensor_attach(sc->sc_sme,
    495 			&sc->sc_sensor[i]) != 0)
    496 			goto fail;
    497 	}
    498 
    499 	for (i = THINKPAD_NTEMPSENSORS; i < THINKPAD_NSENSORS; i++, j++) {
    500 
    501 		sc->sc_sensor[i].units = ENVSYS_SFANRPM;
    502 
    503 		(void)snprintf(sc->sc_sensor[i].desc,
    504 		    sizeof(sc->sc_sensor[i].desc), "FAN%d", j);
    505 
    506 		if (sysmon_envsys_sensor_attach(sc->sc_sme,
    507 			&sc->sc_sensor[i]) != 0)
    508 			goto fail;
    509 	}
    510 
    511 	sc->sc_sme->sme_name = device_xname(sc->sc_dev);
    512 	sc->sc_sme->sme_cookie = sc;
    513 	sc->sc_sme->sme_refresh = thinkpad_sensors_refresh;
    514 
    515 	if (sysmon_envsys_register(sc->sc_sme) != 0)
    516 		goto fail;
    517 
    518 	return;
    519 
    520 fail:
    521 	aprint_error_dev(sc->sc_dev, "failed to initialize sysmon\n");
    522 	sysmon_envsys_destroy(sc->sc_sme);
    523 	sc->sc_sme = NULL;
    524 }
    525 
    526 static void
    527 thinkpad_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    528 {
    529 	switch (edata->units) {
    530 	case ENVSYS_STEMP:
    531 		thinkpad_temp_refresh(sme, edata);
    532 		break;
    533 	case ENVSYS_SFANRPM:
    534 		thinkpad_fan_refresh(sme, edata);
    535 		break;
    536 	default:
    537 		break;
    538 	}
    539 }
    540 
    541 static void
    542 thinkpad_temp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    543 {
    544 	thinkpad_softc_t *sc = sme->sme_cookie;
    545 	char sname[5] = "TMP?";
    546 	ACPI_INTEGER val;
    547 	ACPI_STATUS rv;
    548 	int temp;
    549 
    550 	sname[3] = '0' + edata->sensor;
    551 	rv = acpi_eval_integer(acpiec_get_handle(sc->sc_ecdev), sname, &val);
    552 	if (ACPI_FAILURE(rv)) {
    553 		edata->state = ENVSYS_SINVALID;
    554 		return;
    555 	}
    556 	temp = (int)val;
    557 	if (temp > 127 || temp < -127) {
    558 		edata->state = ENVSYS_SINVALID;
    559 		return;
    560 	}
    561 
    562 	edata->value_cur = temp * 1000000 + 273150000;
    563 	edata->state = ENVSYS_SVALID;
    564 }
    565 
    566 static void
    567 thinkpad_fan_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    568 {
    569 	thinkpad_softc_t *sc = sme->sme_cookie;
    570 	ACPI_INTEGER lo;
    571 	ACPI_INTEGER hi;
    572 	ACPI_STATUS rv;
    573 	int rpm;
    574 
    575 	/*
    576 	 * Read the low byte first to avoid a firmware bug.
    577 	 */
    578 	rv = acpiec_bus_read(sc->sc_ecdev, 0x84, &lo, 1);
    579 	if (ACPI_FAILURE(rv)) {
    580 		edata->state = ENVSYS_SINVALID;
    581 		return;
    582 	}
    583 	rv = acpiec_bus_read(sc->sc_ecdev, 0x85, &hi, 1);
    584 	if (ACPI_FAILURE(rv)) {
    585 		edata->state = ENVSYS_SINVALID;
    586 		return;
    587 	}
    588 	rpm = ((((int)hi) << 8) | ((int)lo));
    589 	if (rpm < 0) {
    590 		edata->state = ENVSYS_SINVALID;
    591 		return;
    592 	}
    593 
    594 	edata->value_cur = rpm;
    595 	if (rpm < edata->value_min || edata->value_min == -1)
    596 		edata->value_min = rpm;
    597 	if (rpm > edata->value_max || edata->value_max == -1)
    598 		edata->value_max = rpm;
    599 	edata->state = ENVSYS_SVALID;
    600 }
    601 
    602 static void
    603 thinkpad_wireless_toggle(thinkpad_softc_t *sc)
    604 {
    605 	/* Ignore return value, as the hardware may not support bluetooth */
    606 	(void)AcpiEvaluateObject(sc->sc_node->ad_handle, "BTGL", NULL, NULL);
    607 }
    608 
    609 static uint8_t
    610 thinkpad_brightness_read(thinkpad_softc_t *sc)
    611 {
    612 #if defined(__i386__) || defined(__amd64__)
    613 	/*
    614 	 * We have two ways to get the current brightness -- either via
    615 	 * magic RTC registers, or using the EC. Since I don't dare mess
    616 	 * with the EC, and Thinkpads are x86-only, this will have to do
    617 	 * for now.
    618 	 */
    619 	outb(IO_RTC, 0x6c);
    620 	return inb(IO_RTC+1) & 7;
    621 #else
    622 	return 0;
    623 #endif
    624 }
    625 
    626 static void
    627 thinkpad_brightness_up(device_t self)
    628 {
    629 	thinkpad_softc_t *sc = device_private(self);
    630 
    631 	if (thinkpad_brightness_read(sc) == 7)
    632 		return;
    633 	thinkpad_cmos(sc, THINKPAD_CMOS_BRIGHTNESS_UP);
    634 }
    635 
    636 static void
    637 thinkpad_brightness_down(device_t self)
    638 {
    639 	thinkpad_softc_t *sc = device_private(self);
    640 
    641 	if (thinkpad_brightness_read(sc) == 0)
    642 		return;
    643 	thinkpad_cmos(sc, THINKPAD_CMOS_BRIGHTNESS_DOWN);
    644 }
    645 
    646 static void
    647 thinkpad_cmos(thinkpad_softc_t *sc, uint8_t cmd)
    648 {
    649 	ACPI_STATUS rv;
    650 
    651 	if (sc->sc_cmoshdl_valid == false)
    652 		return;
    653 
    654 	rv = acpi_eval_set_integer(sc->sc_cmoshdl, NULL, cmd);
    655 	if (ACPI_FAILURE(rv))
    656 		aprint_error_dev(sc->sc_dev, "couldn't evalute CMOS: %s\n",
    657 		    AcpiFormatException(rv));
    658 }
    659 
    660 static bool
    661 thinkpad_resume(device_t dv, const pmf_qual_t *qual)
    662 {
    663 	ACPI_STATUS rv;
    664 	ACPI_HANDLE pubs;
    665 
    666 	rv = AcpiGetHandle(NULL, "\\_SB.PCI0.LPC.EC.PUBS", &pubs);
    667 	if (ACPI_FAILURE(rv))
    668 		return true;	/* not fatal */
    669 
    670 	rv = AcpiEvaluateObject(pubs, "_ON", NULL, NULL);
    671 	if (ACPI_FAILURE(rv))
    672 		aprint_error_dev(dv, "failed to execute PUBS._ON: %s\n",
    673 		    AcpiFormatException(rv));
    674 
    675 	return true;
    676 }
    677 
    678 #ifdef _MODULE
    679 
    680 MODULE(MODULE_CLASS_DRIVER, thinkpad, NULL);
    681 CFDRIVER_DECL(thinkpad, DV_DULL, NULL);
    682 
    683 static int thinkpadloc[] = { -1 };
    684 extern struct cfattach thinkpad_ca;
    685 
    686 static struct cfparent acpiparent = {
    687 	"acpinodebus", NULL, DVUNIT_ANY
    688 };
    689 
    690 static struct cfdata thinkpad_cfdata[] = {
    691 	{
    692 		.cf_name = "thinkpad",
    693 		.cf_atname = "thinkpad",
    694 		.cf_unit = 0,
    695 		.cf_fstate = FSTATE_STAR,
    696 		.cf_loc = thinkpadloc,
    697 		.cf_flags = 0,
    698 		.cf_pspec = &acpiparent,
    699 	},
    700 
    701 	{ NULL }
    702 };
    703 
    704 static int
    705 thinkpad_modcmd(modcmd_t cmd, void *opaque)
    706 {
    707 	int err;
    708 
    709 	switch (cmd) {
    710 
    711 	case MODULE_CMD_INIT:
    712 
    713 		err = config_cfdriver_attach(&thinkpad_cd);
    714 
    715 		if (err != 0)
    716 			return err;
    717 
    718 		err = config_cfattach_attach("thinkpad", &thinkpad_ca);
    719 
    720 		if (err != 0) {
    721 			config_cfdriver_detach(&thinkpad_cd);
    722 			return err;
    723 		}
    724 
    725 		err = config_cfdata_attach(thinkpad_cfdata, 1);
    726 
    727 		if (err != 0) {
    728 			config_cfattach_detach("thinkpad", &thinkpad_ca);
    729 			config_cfdriver_detach(&thinkpad_cd);
    730 			return err;
    731 		}
    732 
    733 		return 0;
    734 
    735 	case MODULE_CMD_FINI:
    736 
    737 		err = config_cfdata_detach(thinkpad_cfdata);
    738 
    739 		if (err != 0)
    740 			return err;
    741 
    742 		config_cfattach_detach("thinkpad", &thinkpad_ca);
    743 		config_cfdriver_detach(&thinkpad_cd);
    744 
    745 		return 0;
    746 
    747 	default:
    748 		return ENOTTY;
    749 	}
    750 }
    751 
    752 #endif	/* _MODULE */
    753