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