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