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