Home | History | Annotate | Line # | Download | only in acpi
thinkpad_acpi.c revision 1.6
      1  1.6  jmcneill /* $NetBSD: thinkpad_acpi.c,v 1.6 2007/12/21 22:14:03 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  * 3. All advertising materials mentioning features or use of this software
     16  1.1  jmcneill  *    must display the following acknowledgement:
     17  1.1  jmcneill  *        This product includes software developed by Jared D. McNeill.
     18  1.1  jmcneill  * 4. Neither the name of The NetBSD Foundation nor the names of its
     19  1.1  jmcneill  *    contributors may be used to endorse or promote products derived
     20  1.1  jmcneill  *    from this software without specific prior written permission.
     21  1.1  jmcneill  *
     22  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     33  1.1  jmcneill  */
     34  1.1  jmcneill 
     35  1.1  jmcneill #include <sys/cdefs.h>
     36  1.6  jmcneill __KERNEL_RCSID(0, "$NetBSD: thinkpad_acpi.c,v 1.6 2007/12/21 22:14:03 jmcneill Exp $");
     37  1.1  jmcneill 
     38  1.1  jmcneill #include <sys/types.h>
     39  1.1  jmcneill #include <sys/param.h>
     40  1.1  jmcneill #include <sys/malloc.h>
     41  1.1  jmcneill #include <sys/buf.h>
     42  1.1  jmcneill #include <sys/callout.h>
     43  1.1  jmcneill #include <sys/kernel.h>
     44  1.1  jmcneill #include <sys/device.h>
     45  1.1  jmcneill #include <sys/pmf.h>
     46  1.1  jmcneill #include <sys/queue.h>
     47  1.1  jmcneill #include <sys/kmem.h>
     48  1.1  jmcneill 
     49  1.1  jmcneill #include <dev/acpi/acpivar.h>
     50  1.5  jmcneill #include <dev/acpi/acpi_ecvar.h>
     51  1.1  jmcneill 
     52  1.1  jmcneill #if defined(__i386__) || defined(__amd64__)
     53  1.1  jmcneill #include <machine/pio.h>
     54  1.1  jmcneill #endif
     55  1.1  jmcneill 
     56  1.5  jmcneill #define THINKPAD_NSENSORS	8
     57  1.5  jmcneill 
     58  1.1  jmcneill typedef struct thinkpad_softc {
     59  1.1  jmcneill 	device_t		sc_dev;
     60  1.5  jmcneill 	device_t		sc_ecdev;
     61  1.1  jmcneill 	struct acpi_devnode	*sc_node;
     62  1.1  jmcneill 	ACPI_HANDLE		sc_cmoshdl;
     63  1.1  jmcneill 	bool			sc_cmoshdl_valid;
     64  1.1  jmcneill 
     65  1.1  jmcneill 	struct sysmon_pswitch	sc_smpsw[2];
     66  1.1  jmcneill #define TP_PSW_SLEEP		0
     67  1.1  jmcneill #define	TP_PSW_HIBERNATE	1
     68  1.1  jmcneill 	bool			sc_smpsw_valid;
     69  1.5  jmcneill 
     70  1.5  jmcneill 	struct sysmon_envsys	*sc_sme;
     71  1.5  jmcneill 	envsys_data_t		sc_sensor[THINKPAD_NSENSORS];
     72  1.1  jmcneill } thinkpad_softc_t;
     73  1.1  jmcneill 
     74  1.1  jmcneill /* Hotkey events */
     75  1.1  jmcneill #define	THINKPAD_NOTIFY_FnF1		0x001
     76  1.1  jmcneill #define	THINKPAD_NOTIFY_LockScreen	0x002
     77  1.1  jmcneill #define	THINKPAD_NOTIFY_BatteryInfo	0x003
     78  1.1  jmcneill #define	THINKPAD_NOTIFY_SleepButton	0x004
     79  1.1  jmcneill #define	THINKPAD_NOTIFY_WirelessSwitch	0x005
     80  1.1  jmcneill #define	THINKPAD_NOTIFY_FnF6		0x006
     81  1.1  jmcneill #define	THINKPAD_NOTIFY_DisplayCycle	0x007
     82  1.1  jmcneill #define	THINKPAD_NOTIFY_PointerSwitch	0x008
     83  1.1  jmcneill #define	THINKPAD_NOTIFY_EjectButton	0x009
     84  1.1  jmcneill #define	THINKPAD_NOTIFY_FnF10		0x00a
     85  1.1  jmcneill #define	THINKPAD_NOTIFY_FnF11		0x00b
     86  1.1  jmcneill #define	THINKPAD_NOTIFY_HibernateButton	0x00c
     87  1.1  jmcneill #define	THINKPAD_NOTIFY_BrightnessUp	0x010
     88  1.1  jmcneill #define	THINKPAD_NOTIFY_BrightnessDown	0x011
     89  1.1  jmcneill #define	THINKPAD_NOTIFY_ThinkLight	0x012
     90  1.1  jmcneill #define	THINKPAD_NOTIFY_Zoom		0x014
     91  1.1  jmcneill #define	THINKPAD_NOTIFY_ThinkVantage	0x018
     92  1.1  jmcneill 
     93  1.1  jmcneill #define	THINKPAD_CMOS_BRIGHTNESS_UP	0x04
     94  1.1  jmcneill #define	THINKPAD_CMOS_BRIGHTNESS_DOWN	0x05
     95  1.1  jmcneill 
     96  1.2  jmcneill #define THINKPAD_HKEY_VERSION		0x0100
     97  1.2  jmcneill 
     98  1.1  jmcneill static int	thinkpad_match(device_t, struct cfdata *, void *);
     99  1.1  jmcneill static void	thinkpad_attach(device_t, device_t, void *);
    100  1.1  jmcneill 
    101  1.1  jmcneill static ACPI_STATUS thinkpad_mask_init(thinkpad_softc_t *, uint32_t);
    102  1.1  jmcneill static void	thinkpad_notify_handler(ACPI_HANDLE, UINT32, void *);
    103  1.4  jmcneill static void	thinkpad_get_hotkeys(void *);
    104  1.1  jmcneill 
    105  1.5  jmcneill static void	thinkpad_temp_init(thinkpad_softc_t *);
    106  1.5  jmcneill static void	thinkpad_temp_refresh(struct sysmon_envsys *, envsys_data_t *);
    107  1.5  jmcneill 
    108  1.6  jmcneill static void	thinkpad_wireless_toggle(thinkpad_softc_t *);
    109  1.6  jmcneill 
    110  1.1  jmcneill static void	thinkpad_brightness_up(device_t);
    111  1.1  jmcneill static void	thinkpad_brightness_down(device_t);
    112  1.1  jmcneill static uint8_t	thinkpad_brightness_read(thinkpad_softc_t *sc);
    113  1.1  jmcneill static void	thinkpad_cmos(thinkpad_softc_t *, uint8_t);
    114  1.1  jmcneill 
    115  1.1  jmcneill CFATTACH_DECL_NEW(thinkpad, sizeof(thinkpad_softc_t),
    116  1.1  jmcneill     thinkpad_match, thinkpad_attach, NULL, NULL);
    117  1.1  jmcneill 
    118  1.1  jmcneill static const char * const thinkpad_ids[] = {
    119  1.1  jmcneill 	"IBM0068",
    120  1.1  jmcneill 	NULL
    121  1.1  jmcneill };
    122  1.1  jmcneill 
    123  1.1  jmcneill static int
    124  1.1  jmcneill thinkpad_match(device_t parent, struct cfdata *match, void *opaque)
    125  1.1  jmcneill {
    126  1.1  jmcneill 	struct acpi_attach_args *aa = (struct acpi_attach_args *)opaque;
    127  1.1  jmcneill 	ACPI_HANDLE hdl;
    128  1.2  jmcneill 	ACPI_INTEGER ver;
    129  1.1  jmcneill 
    130  1.1  jmcneill 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    131  1.1  jmcneill 		return 0;
    132  1.1  jmcneill 
    133  1.1  jmcneill 	if (!acpi_match_hid(aa->aa_node->ad_devinfo, thinkpad_ids))
    134  1.1  jmcneill 		return 0;
    135  1.1  jmcneill 
    136  1.1  jmcneill 	/* No point in attaching if we can't find the CMOS method */
    137  1.1  jmcneill 	if (ACPI_FAILURE(AcpiGetHandle(NULL, "\\UCMS", &hdl)))
    138  1.1  jmcneill 		return 0;
    139  1.1  jmcneill 
    140  1.2  jmcneill 	/* We only support hotkey version 0x0100 */
    141  1.3  jmcneill 	if (ACPI_FAILURE(acpi_eval_integer(aa->aa_node->ad_handle, "MHKV",
    142  1.3  jmcneill 	    &ver)))
    143  1.2  jmcneill 		return 0;
    144  1.2  jmcneill 
    145  1.2  jmcneill 	if (ver != THINKPAD_HKEY_VERSION)
    146  1.2  jmcneill 		return 0;
    147  1.2  jmcneill 
    148  1.1  jmcneill 	/* Cool, looks like we're good to go */
    149  1.1  jmcneill 	return 1;
    150  1.1  jmcneill }
    151  1.1  jmcneill 
    152  1.1  jmcneill static void
    153  1.1  jmcneill thinkpad_attach(device_t parent, device_t self, void *opaque)
    154  1.1  jmcneill {
    155  1.1  jmcneill 	thinkpad_softc_t *sc = device_private(self);
    156  1.1  jmcneill 	struct acpi_attach_args *aa = (struct acpi_attach_args *)opaque;
    157  1.5  jmcneill 	device_t curdev;
    158  1.1  jmcneill 	ACPI_STATUS rv;
    159  1.1  jmcneill 	ACPI_INTEGER val;
    160  1.1  jmcneill 
    161  1.1  jmcneill 	sc->sc_node = aa->aa_node;
    162  1.1  jmcneill 	sc->sc_dev = self;
    163  1.1  jmcneill 
    164  1.1  jmcneill 	aprint_naive("\n");
    165  1.1  jmcneill 	aprint_normal("\n");
    166  1.1  jmcneill 
    167  1.1  jmcneill 	/* T61 uses \UCMS method for issuing CMOS commands */
    168  1.1  jmcneill 	rv = AcpiGetHandle(NULL, "\\UCMS", &sc->sc_cmoshdl);
    169  1.1  jmcneill 	if (ACPI_FAILURE(rv))
    170  1.1  jmcneill 		sc->sc_cmoshdl_valid = false;
    171  1.1  jmcneill 	else {
    172  1.1  jmcneill 		aprint_verbose_dev(self, "using CMOS at \\UCMS\n");
    173  1.1  jmcneill 		sc->sc_cmoshdl_valid = true;
    174  1.1  jmcneill 	}
    175  1.1  jmcneill 
    176  1.5  jmcneill 	sc->sc_ecdev = NULL;
    177  1.5  jmcneill 	TAILQ_FOREACH(curdev, &alldevs, dv_list)
    178  1.5  jmcneill 		if (device_is_a(curdev, "acpiecdt") ||
    179  1.5  jmcneill 		    device_is_a(curdev, "acpiec")) {
    180  1.5  jmcneill 			sc->sc_ecdev = curdev;
    181  1.5  jmcneill 			break;
    182  1.5  jmcneill 		}
    183  1.5  jmcneill 	if (sc->sc_ecdev)
    184  1.5  jmcneill 		aprint_verbose_dev(self, "using EC at %s\n",
    185  1.5  jmcneill 		    device_xname(sc->sc_ecdev));
    186  1.5  jmcneill 
    187  1.1  jmcneill 	/* Get the supported event mask */
    188  1.1  jmcneill 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "MHKA", &val);
    189  1.1  jmcneill 	if (ACPI_FAILURE(rv)) {
    190  1.1  jmcneill 		aprint_error_dev(self, "couldn't evaluate MHKA: %s\n",
    191  1.1  jmcneill 		    AcpiFormatException(rv));
    192  1.1  jmcneill 		goto fail;
    193  1.1  jmcneill 	}
    194  1.1  jmcneill 
    195  1.1  jmcneill 	/* Enable all supported events */
    196  1.1  jmcneill 	rv = thinkpad_mask_init(sc, val);
    197  1.1  jmcneill 	if (ACPI_FAILURE(rv)) {
    198  1.1  jmcneill 		aprint_error_dev(self, "couldn't set event mask: %s\n",
    199  1.1  jmcneill 		    AcpiFormatException(rv));
    200  1.1  jmcneill 		goto fail;
    201  1.1  jmcneill 	}
    202  1.1  jmcneill 
    203  1.1  jmcneill 	/* Install notify handler for events */
    204  1.1  jmcneill 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
    205  1.1  jmcneill 	    ACPI_DEVICE_NOTIFY, thinkpad_notify_handler, sc);
    206  1.1  jmcneill 	if (ACPI_FAILURE(rv))
    207  1.1  jmcneill 		aprint_error_dev(self, "couldn't install notify handler: %s\n",
    208  1.1  jmcneill 		    AcpiFormatException(rv));
    209  1.1  jmcneill 
    210  1.5  jmcneill 	/* Register power switches with sysmon */
    211  1.1  jmcneill 	sc->sc_smpsw_valid = true;
    212  1.1  jmcneill 
    213  1.1  jmcneill 	sc->sc_smpsw[TP_PSW_SLEEP].smpsw_name = device_xname(self);
    214  1.1  jmcneill 	sc->sc_smpsw[TP_PSW_SLEEP].smpsw_type = PSWITCH_TYPE_SLEEP;
    215  1.1  jmcneill #if notyet
    216  1.1  jmcneill 	sc->sc_smpsw[TP_PSW_HIBERNATE].smpsw_name = device_xname(self);
    217  1.1  jmcneill 	sc->sc_smpsw[TP_PSW_HIBERNATE].smpsw_type = PSWITCH_TYPE_HIBERNATE;
    218  1.1  jmcneill #endif
    219  1.1  jmcneill 
    220  1.1  jmcneill 	if (sysmon_pswitch_register(&sc->sc_smpsw[TP_PSW_SLEEP]) != 0) {
    221  1.1  jmcneill 		aprint_error_dev(self, "couldn't register with sysmon\n");
    222  1.1  jmcneill 		sc->sc_smpsw_valid = false;
    223  1.1  jmcneill 	}
    224  1.1  jmcneill #if notyet
    225  1.1  jmcneill 	if (sysmon_pswitch_register(&sc->sc_smpsw[TP_PSW_HIBERNATE]) != 0) {
    226  1.1  jmcneill 		aprint_error_dev(self, "couldn't register with sysmon\n");
    227  1.1  jmcneill 		sc->sc_smpsw_valid = false;
    228  1.1  jmcneill 	}
    229  1.1  jmcneill #endif
    230  1.1  jmcneill 
    231  1.5  jmcneill 	/* Register temperature sensors with envsys */
    232  1.5  jmcneill 	thinkpad_temp_init(sc);
    233  1.5  jmcneill 
    234  1.1  jmcneill fail:
    235  1.1  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    236  1.1  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    237  1.1  jmcneill 	if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_UP,
    238  1.1  jmcneill 	    thinkpad_brightness_up, true))
    239  1.1  jmcneill 		aprint_error_dev(self, "couldn't register event handler\n");
    240  1.1  jmcneill 	if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_DOWN,
    241  1.1  jmcneill 	    thinkpad_brightness_down, true))
    242  1.1  jmcneill 		aprint_error_dev(self, "couldn't register event handler\n");
    243  1.1  jmcneill 
    244  1.1  jmcneill 	return;
    245  1.1  jmcneill }
    246  1.1  jmcneill 
    247  1.1  jmcneill static void
    248  1.1  jmcneill thinkpad_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
    249  1.1  jmcneill {
    250  1.1  jmcneill 	thinkpad_softc_t *sc = (thinkpad_softc_t *)opaque;
    251  1.1  jmcneill 	device_t self = sc->sc_dev;
    252  1.1  jmcneill 	ACPI_STATUS rv;
    253  1.1  jmcneill 
    254  1.1  jmcneill 	if (notify != 0x80) {
    255  1.1  jmcneill 		aprint_debug_dev(self, "unknown notify 0x%02x\n", notify);
    256  1.1  jmcneill 		return;
    257  1.1  jmcneill 	}
    258  1.1  jmcneill 
    259  1.4  jmcneill 	rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, thinkpad_get_hotkeys, sc);
    260  1.4  jmcneill 	if (ACPI_FAILURE(rv))
    261  1.4  jmcneill 		aprint_error_dev(self, "couldn't queue hotkey handler: %s\n",
    262  1.4  jmcneill 		    AcpiFormatException(rv));
    263  1.4  jmcneill }
    264  1.4  jmcneill 
    265  1.4  jmcneill static void
    266  1.4  jmcneill thinkpad_get_hotkeys(void *opaque)
    267  1.4  jmcneill {
    268  1.4  jmcneill 	thinkpad_softc_t *sc = (thinkpad_softc_t *)opaque;
    269  1.4  jmcneill 	device_t self = sc->sc_dev;
    270  1.4  jmcneill 	ACPI_STATUS rv;
    271  1.4  jmcneill 	ACPI_INTEGER val;
    272  1.4  jmcneill 	int type, event;
    273  1.4  jmcneill 
    274  1.2  jmcneill 	for (;;) {
    275  1.2  jmcneill 		rv = acpi_eval_integer(sc->sc_node->ad_handle, "MHKP", &val);
    276  1.2  jmcneill 		if (ACPI_FAILURE(rv)) {
    277  1.2  jmcneill 			aprint_error_dev(self, "couldn't evaluate MHKP: %s\n",
    278  1.2  jmcneill 			    AcpiFormatException(rv));
    279  1.2  jmcneill 			return;
    280  1.2  jmcneill 		}
    281  1.2  jmcneill 
    282  1.2  jmcneill 		if (val == 0)
    283  1.2  jmcneill 			return;
    284  1.2  jmcneill 
    285  1.2  jmcneill 		type = (val & 0xf000) >> 12;
    286  1.2  jmcneill 		event = val & 0x0fff;
    287  1.2  jmcneill 
    288  1.2  jmcneill 		if (type != 1)
    289  1.2  jmcneill 			/* Only type 1 events are supported for now */
    290  1.2  jmcneill 			continue;
    291  1.2  jmcneill 
    292  1.2  jmcneill 		switch (event) {
    293  1.2  jmcneill 		case THINKPAD_NOTIFY_BrightnessUp:
    294  1.2  jmcneill 			thinkpad_brightness_up(self);
    295  1.2  jmcneill 			break;
    296  1.2  jmcneill 		case THINKPAD_NOTIFY_BrightnessDown:
    297  1.2  jmcneill 			thinkpad_brightness_down(self);
    298  1.2  jmcneill 			break;
    299  1.2  jmcneill 		case THINKPAD_NOTIFY_DisplayCycle:
    300  1.1  jmcneill #if notyet
    301  1.2  jmcneill 			pmf_event_inject(NULL, PMFE_DISPLAY_CYCLE);
    302  1.1  jmcneill #endif
    303  1.1  jmcneill 			break;
    304  1.6  jmcneill 		case THINKPAD_NOTIFY_WirelessSwitch:
    305  1.6  jmcneill 			thinkpad_wireless_toggle(sc);
    306  1.6  jmcneill 			break;
    307  1.2  jmcneill 		case THINKPAD_NOTIFY_SleepButton:
    308  1.2  jmcneill 			if (sc->sc_smpsw_valid == false)
    309  1.2  jmcneill 				break;
    310  1.2  jmcneill 			sysmon_pswitch_event(&sc->sc_smpsw[TP_PSW_SLEEP],
    311  1.2  jmcneill 			    PSWITCH_EVENT_PRESSED);
    312  1.2  jmcneill 			break;
    313  1.2  jmcneill 		case THINKPAD_NOTIFY_HibernateButton:
    314  1.1  jmcneill #if notyet
    315  1.2  jmcneill 			if (sc->sc_smpsw_valid == false)
    316  1.2  jmcneill 				break;
    317  1.2  jmcneill 			sysmon_pswitch_event(&sc->sc_smpsw[TP_PSW_HIBERNATE],
    318  1.2  jmcneill 			    PSWITCH_EVENT_PRESSED);
    319  1.6  jmcneill #endif
    320  1.1  jmcneill 			break;
    321  1.2  jmcneill 		case THINKPAD_NOTIFY_FnF1:
    322  1.2  jmcneill 		case THINKPAD_NOTIFY_LockScreen:
    323  1.2  jmcneill 		case THINKPAD_NOTIFY_BatteryInfo:
    324  1.2  jmcneill 		case THINKPAD_NOTIFY_FnF6:
    325  1.2  jmcneill 		case THINKPAD_NOTIFY_PointerSwitch:
    326  1.2  jmcneill 		case THINKPAD_NOTIFY_EjectButton:
    327  1.2  jmcneill 		case THINKPAD_NOTIFY_FnF10:
    328  1.2  jmcneill 		case THINKPAD_NOTIFY_FnF11:
    329  1.2  jmcneill 		case THINKPAD_NOTIFY_ThinkLight:
    330  1.2  jmcneill 		case THINKPAD_NOTIFY_Zoom:
    331  1.2  jmcneill 		case THINKPAD_NOTIFY_ThinkVantage:
    332  1.2  jmcneill 			/* XXXJDM we should deliver hotkeys as keycodes */
    333  1.2  jmcneill 			break;
    334  1.2  jmcneill 		default:
    335  1.2  jmcneill 			aprint_debug_dev(self, "notify event 0x%03x\n", event);
    336  1.2  jmcneill 			break;
    337  1.2  jmcneill 		}
    338  1.1  jmcneill 	}
    339  1.1  jmcneill 
    340  1.1  jmcneill 	return;
    341  1.1  jmcneill }
    342  1.1  jmcneill 
    343  1.1  jmcneill static ACPI_STATUS
    344  1.1  jmcneill thinkpad_mask_init(thinkpad_softc_t *sc, uint32_t mask)
    345  1.1  jmcneill {
    346  1.1  jmcneill 	ACPI_OBJECT param[2];
    347  1.1  jmcneill 	ACPI_OBJECT_LIST params;
    348  1.1  jmcneill 	ACPI_STATUS rv;
    349  1.1  jmcneill 	int i;
    350  1.1  jmcneill 
    351  1.1  jmcneill 	/* Update hotkey mask */
    352  1.1  jmcneill 	params.Count = 2;
    353  1.1  jmcneill 	params.Pointer = param;
    354  1.1  jmcneill 	param[0].Type = param[1].Type = ACPI_TYPE_INTEGER;
    355  1.1  jmcneill 
    356  1.1  jmcneill 	for (i = 0; i < 32; i++) {
    357  1.1  jmcneill 		param[0].Integer.Value = i + 1;
    358  1.1  jmcneill 		param[1].Integer.Value = (((1 << i) & mask) != 0);
    359  1.1  jmcneill 
    360  1.1  jmcneill 		rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "MHKM",
    361  1.1  jmcneill 		    &params, NULL);
    362  1.1  jmcneill 		if (ACPI_FAILURE(rv))
    363  1.1  jmcneill 			return rv;
    364  1.1  jmcneill 	}
    365  1.1  jmcneill 
    366  1.1  jmcneill 	/* Enable hotkey events */
    367  1.1  jmcneill 	params.Count = 1;
    368  1.1  jmcneill 	param[0].Integer.Value = 1;
    369  1.1  jmcneill 	rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "MHKC", &params, NULL);
    370  1.1  jmcneill 	if (ACPI_FAILURE(rv)) {
    371  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't enable hotkeys: %s\n",
    372  1.1  jmcneill 		    AcpiFormatException(rv));
    373  1.1  jmcneill 		return rv;
    374  1.1  jmcneill 	}
    375  1.1  jmcneill 
    376  1.4  jmcneill 	/* Claim ownership of brightness control */
    377  1.4  jmcneill 	param[0].Integer.Value = 0;
    378  1.4  jmcneill 	(void)AcpiEvaluateObject(sc->sc_node->ad_handle, "PWMS", &params, NULL);
    379  1.4  jmcneill 
    380  1.1  jmcneill 	return AE_OK;
    381  1.1  jmcneill }
    382  1.1  jmcneill 
    383  1.5  jmcneill static void
    384  1.5  jmcneill thinkpad_temp_init(thinkpad_softc_t *sc)
    385  1.5  jmcneill {
    386  1.5  jmcneill 	char sname[5] = "TMP?";
    387  1.5  jmcneill 	int i, err;
    388  1.5  jmcneill 
    389  1.5  jmcneill 	if (sc->sc_ecdev == NULL)
    390  1.5  jmcneill 		return;	/* no chance of this working */
    391  1.5  jmcneill 
    392  1.5  jmcneill 	sc->sc_sme = sysmon_envsys_create();
    393  1.5  jmcneill 	for (i = 0; i < THINKPAD_NSENSORS; i++) {
    394  1.5  jmcneill 		sname[3] = '0' + i;
    395  1.5  jmcneill 		strcpy(sc->sc_sensor[i].desc, sname);
    396  1.5  jmcneill 		sc->sc_sensor[i].units = ENVSYS_STEMP;
    397  1.5  jmcneill 
    398  1.5  jmcneill 		if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[i]))
    399  1.5  jmcneill 			aprint_error_dev(sc->sc_dev,
    400  1.5  jmcneill 			    "couldn't attach sensor %s\n", sname);
    401  1.5  jmcneill 	}
    402  1.5  jmcneill 
    403  1.5  jmcneill 	sc->sc_sme->sme_name = device_xname(sc->sc_dev);
    404  1.5  jmcneill 	sc->sc_sme->sme_cookie = sc;
    405  1.5  jmcneill 	sc->sc_sme->sme_refresh = thinkpad_temp_refresh;
    406  1.5  jmcneill 
    407  1.5  jmcneill 	err = sysmon_envsys_register(sc->sc_sme);
    408  1.5  jmcneill 	if (err) {
    409  1.5  jmcneill 		aprint_error_dev(sc->sc_dev,
    410  1.5  jmcneill 		    "couldn't register with sysmon: %d\n", err);
    411  1.5  jmcneill 		sysmon_envsys_destroy(sc->sc_sme);
    412  1.5  jmcneill 	}
    413  1.5  jmcneill }
    414  1.5  jmcneill 
    415  1.5  jmcneill static void
    416  1.5  jmcneill thinkpad_temp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    417  1.5  jmcneill {
    418  1.5  jmcneill 	thinkpad_softc_t *sc = sme->sme_cookie;
    419  1.5  jmcneill 	char sname[5] = "TMP?";
    420  1.5  jmcneill 	ACPI_INTEGER val;
    421  1.5  jmcneill 	ACPI_STATUS rv;
    422  1.5  jmcneill 	int temp;
    423  1.5  jmcneill 
    424  1.5  jmcneill 	sname[3] = '0' + edata->sensor;
    425  1.5  jmcneill 	rv = acpi_eval_integer(acpiec_get_handle(sc->sc_ecdev), sname, &val);
    426  1.5  jmcneill 	if (ACPI_FAILURE(rv)) {
    427  1.5  jmcneill 		edata->state = ENVSYS_SINVALID;
    428  1.5  jmcneill 		return;
    429  1.5  jmcneill 	}
    430  1.5  jmcneill 	temp = (int)val;
    431  1.5  jmcneill 	if (temp > 127 || temp < -127) {
    432  1.5  jmcneill 		edata->state = ENVSYS_SINVALID;
    433  1.5  jmcneill 		return;
    434  1.5  jmcneill 	}
    435  1.5  jmcneill 
    436  1.5  jmcneill 	edata->value_cur = temp * 1000000 + 273150000;
    437  1.5  jmcneill 	edata->state = ENVSYS_SVALID;
    438  1.5  jmcneill }
    439  1.5  jmcneill 
    440  1.6  jmcneill static void
    441  1.6  jmcneill thinkpad_wireless_toggle(thinkpad_softc_t *sc)
    442  1.6  jmcneill {
    443  1.6  jmcneill 	/* XXXJDM this could be much smarter */
    444  1.6  jmcneill 	(void)AcpiEvaluateObject(sc->sc_node->ad_handle, "WTGL", NULL, NULL);
    445  1.6  jmcneill 	(void)AcpiEvaluateObject(sc->sc_node->ad_handle, "BTGL", NULL, NULL);
    446  1.6  jmcneill }
    447  1.6  jmcneill 
    448  1.1  jmcneill static uint8_t
    449  1.1  jmcneill thinkpad_brightness_read(thinkpad_softc_t *sc)
    450  1.1  jmcneill {
    451  1.1  jmcneill #if defined(__i386__) || defined(__amd64__)
    452  1.1  jmcneill 	/*
    453  1.1  jmcneill 	 * We have two ways to get the current brightness -- either via
    454  1.1  jmcneill 	 * magic RTC registers, or using the EC. Since I don't dare mess
    455  1.1  jmcneill 	 * with the EC, and Thinkpads are x86-only, this will have to do
    456  1.1  jmcneill 	 * for now.
    457  1.1  jmcneill 	 */
    458  1.1  jmcneill 	outb(0x70, 0x6c);
    459  1.1  jmcneill 	return inb(0x71) & 7;
    460  1.1  jmcneill #else
    461  1.1  jmcneill 	return 0;
    462  1.1  jmcneill #endif
    463  1.1  jmcneill }
    464  1.1  jmcneill 
    465  1.1  jmcneill static void
    466  1.1  jmcneill thinkpad_brightness_up(device_t self)
    467  1.1  jmcneill {
    468  1.1  jmcneill 	thinkpad_softc_t *sc = device_private(self);
    469  1.1  jmcneill 
    470  1.1  jmcneill 	if (thinkpad_brightness_read(sc) == 7)
    471  1.1  jmcneill 		return;
    472  1.1  jmcneill 	thinkpad_cmos(sc, THINKPAD_CMOS_BRIGHTNESS_UP);
    473  1.1  jmcneill }
    474  1.1  jmcneill 
    475  1.1  jmcneill static void
    476  1.1  jmcneill thinkpad_brightness_down(device_t self)
    477  1.1  jmcneill {
    478  1.1  jmcneill 	thinkpad_softc_t *sc = device_private(self);
    479  1.1  jmcneill 
    480  1.1  jmcneill 	if (thinkpad_brightness_read(sc) == 0)
    481  1.1  jmcneill 		return;
    482  1.1  jmcneill 	thinkpad_cmos(sc, THINKPAD_CMOS_BRIGHTNESS_DOWN);
    483  1.1  jmcneill }
    484  1.1  jmcneill 
    485  1.1  jmcneill static void
    486  1.1  jmcneill thinkpad_cmos(thinkpad_softc_t *sc, uint8_t cmd)
    487  1.1  jmcneill {
    488  1.1  jmcneill 	ACPI_OBJECT param;
    489  1.1  jmcneill 	ACPI_OBJECT_LIST params;
    490  1.1  jmcneill 	ACPI_STATUS rv;
    491  1.1  jmcneill 
    492  1.1  jmcneill 	if (sc->sc_cmoshdl_valid == false)
    493  1.1  jmcneill 		return;
    494  1.1  jmcneill 
    495  1.1  jmcneill 	params.Count = 1;
    496  1.1  jmcneill 	params.Pointer = &param;
    497  1.1  jmcneill 	param.Type = ACPI_TYPE_INTEGER;
    498  1.1  jmcneill 	param.Integer.Value = cmd;
    499  1.1  jmcneill 	rv = AcpiEvaluateObject(sc->sc_cmoshdl, NULL, &params, NULL);
    500  1.1  jmcneill 	if (ACPI_FAILURE(rv))
    501  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't evalute CMOS: %s\n",
    502  1.1  jmcneill 		    AcpiFormatException(rv));
    503  1.1  jmcneill }
    504