Home | History | Annotate | Line # | Download | only in acpi
dalb_acpi.c revision 1.6
      1  1.6    jruoho /*	$NetBSD: dalb_acpi.c,v 1.6 2010/01/31 19:49:29 jruoho Exp $	*/
      2  1.1    cegger 
      3  1.1    cegger /*-
      4  1.1    cegger  * Copyright (c) 2008 Christoph Egger <cegger (at) netbsd.org>
      5  1.1    cegger  * Copyright (c) 2008 Jared D. McNeill <jmcneill (at) netbsd.org>
      6  1.1    cegger  * All rights reserved.
      7  1.1    cegger  *
      8  1.1    cegger  * Redistribution and use in source and binary forms, with or without
      9  1.1    cegger  * modification, are permitted provided that the following conditions
     10  1.1    cegger  * are met:
     11  1.1    cegger  * 1. Redistributions of source code must retain the above copyright
     12  1.1    cegger  *    notice, this list of conditions and the following disclaimer.
     13  1.1    cegger  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1    cegger  *    notice, this list of conditions and the following disclaimer in the
     15  1.1    cegger  *    documentation and/or other materials provided with the distribution.
     16  1.1    cegger  *
     17  1.1    cegger  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  1.1    cegger  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  1.1    cegger  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  1.1    cegger  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  1.1    cegger  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  1.1    cegger  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  1.1    cegger  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.1    cegger  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  1.1    cegger  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  1.1    cegger  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  1.1    cegger  * POSSIBILITY OF SUCH DAMAGE.
     28  1.1    cegger  */
     29  1.1    cegger #include <sys/cdefs.h>
     30  1.6    jruoho __KERNEL_RCSID(0, "$NetBSD: dalb_acpi.c,v 1.6 2010/01/31 19:49:29 jruoho Exp $");
     31  1.1    cegger 
     32  1.1    cegger /*
     33  1.1    cegger  * Direct Application Launch Button:
     34  1.1    cegger  * http://www.microsoft.com/whdc/system/vista/DirAppLaunch.mspx
     35  1.1    cegger  */
     36  1.1    cegger 
     37  1.1    cegger #include <sys/param.h>
     38  1.1    cegger #include <sys/systm.h>
     39  1.1    cegger #include <sys/device.h>
     40  1.1    cegger #include <sys/proc.h>
     41  1.1    cegger #include <sys/kernel.h>
     42  1.1    cegger #include <sys/callout.h>
     43  1.1    cegger #include <sys/sysctl.h>
     44  1.1    cegger 
     45  1.1    cegger #include <machine/bus.h>
     46  1.1    cegger 
     47  1.1    cegger #include <dev/acpi/acpica.h>
     48  1.1    cegger #include <dev/acpi/acpivar.h>
     49  1.3   mlelstv #include <dev/acpi/acpireg.h>
     50  1.3   mlelstv 
     51  1.3   mlelstv #define _COMPONENT          ACPI_RESOURCE_COMPONENT
     52  1.3   mlelstv ACPI_MODULE_NAME            ("dalb_acpi")
     53  1.1    cegger 
     54  1.1    cegger #define DALB_ID_INVALID		-1
     55  1.1    cegger 
     56  1.1    cegger struct acpi_dalb_softc {
     57  1.1    cegger 	device_t sc_dev;
     58  1.1    cegger 	struct acpi_devnode *sc_node;
     59  1.1    cegger 	struct sysctllog *sc_log;
     60  1.1    cegger 
     61  1.1    cegger 	ACPI_INTEGER sc_usageid;
     62  1.1    cegger 
     63  1.1    cegger 	/* There's one PNP0C32 ACPI device for each button.
     64  1.1    cegger 	 * Therefore, one instance is enough. */
     65  1.1    cegger 	struct sysmon_pswitch sc_smpsw;
     66  1.1    cegger 	bool sc_smpsw_valid;
     67  1.1    cegger };
     68  1.1    cegger 
     69  1.1    cegger static int	acpi_dalb_match(device_t, cfdata_t, void *);
     70  1.1    cegger static void	acpi_dalb_attach(device_t, device_t, void *);
     71  1.6    jruoho static int	acpi_dalb_detach(device_t, int);
     72  1.1    cegger static void	acpi_dalb_notify_handler(ACPI_HANDLE, UINT32, void *);
     73  1.5    dyoung static bool	acpi_dalb_resume(device_t, pmf_qual_t);
     74  1.1    cegger 
     75  1.1    cegger static void	acpi_dalb_get_wakeup_hotkeys(void *opaque);
     76  1.1    cegger static void	acpi_dalb_get_runtime_hotkeys(void *opaque);
     77  1.1    cegger 
     78  1.1    cegger CFATTACH_DECL_NEW(acpidalb, sizeof(struct acpi_dalb_softc),
     79  1.6    jruoho     acpi_dalb_match, acpi_dalb_attach, acpi_dalb_detach, NULL);
     80  1.1    cegger 
     81  1.1    cegger static const char * const acpi_dalb_ids[] = {
     82  1.1    cegger         "PNP0C32", /* Direct Application Launch Button */
     83  1.1    cegger         NULL
     84  1.1    cegger };
     85  1.1    cegger 
     86  1.1    cegger #ifdef DEBUG
     87  1.1    cegger #define DPRINTF(x)	printf x
     88  1.1    cegger #else
     89  1.1    cegger #define DPRINTF(x)
     90  1.1    cegger #endif
     91  1.1    cegger 
     92  1.1    cegger #define DALB_SYSTEM_WAKEUP	0x02
     93  1.1    cegger #define DALB_SYSTEM_RUNTIME	0x80
     94  1.1    cegger 
     95  1.1    cegger static int
     96  1.1    cegger acpi_dalb_match(device_t parent, cfdata_t match, void *aux)
     97  1.1    cegger {
     98  1.1    cegger 	struct acpi_attach_args *aa = aux;
     99  1.1    cegger 
    100  1.1    cegger 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    101  1.1    cegger 		return 0;
    102  1.1    cegger 
    103  1.1    cegger 	return acpi_match_hid(aa->aa_node->ad_devinfo, acpi_dalb_ids);
    104  1.1    cegger }
    105  1.1    cegger 
    106  1.1    cegger static void
    107  1.1    cegger acpi_dalb_sysmon_init(struct acpi_dalb_softc *sc)
    108  1.1    cegger {
    109  1.1    cegger 	sc->sc_smpsw_valid = true;
    110  1.1    cegger 	sc->sc_smpsw.smpsw_name = device_xname(sc->sc_dev);
    111  1.1    cegger 	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_HOTKEY;
    112  1.1    cegger 	if (sysmon_pswitch_register(&sc->sc_smpsw)) {
    113  1.1    cegger 		aprint_error_dev(sc->sc_dev,
    114  1.1    cegger 			"couldn't register sleep with sysmon\n");
    115  1.1    cegger 		sc->sc_smpsw_valid = false;
    116  1.1    cegger 	}
    117  1.1    cegger }
    118  1.1    cegger 
    119  1.1    cegger 
    120  1.1    cegger static void
    121  1.1    cegger acpi_dalb_init(device_t dev)
    122  1.1    cegger {
    123  1.1    cegger 	struct acpi_dalb_softc *sc = device_private(dev);
    124  1.1    cegger 	ACPI_OBJECT *obj;
    125  1.1    cegger 	ACPI_STATUS rv;
    126  1.1    cegger 	ACPI_BUFFER ret;
    127  1.1    cegger 
    128  1.4    jruoho 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "GHID", &ret);
    129  1.1    cegger 	if (ACPI_FAILURE(rv) || ret.Pointer == NULL) {
    130  1.1    cegger 		aprint_error_dev(dev,
    131  1.1    cegger 			"couldn't enable notify handler: (%s)\n",
    132  1.1    cegger 			AcpiFormatException(rv));
    133  1.1    cegger 		return;
    134  1.1    cegger 	}
    135  1.1    cegger 
    136  1.1    cegger 	obj = (ACPI_OBJECT *)ret.Pointer;
    137  1.1    cegger 	if (obj->Type != ACPI_TYPE_BUFFER) {
    138  1.1    cegger 		sc->sc_usageid = DALB_ID_INVALID;
    139  1.1    cegger 		aprint_debug_dev(dev, "invalid ACPI type: %d\n", obj->Type);
    140  1.1    cegger 		goto out;
    141  1.1    cegger 	}
    142  1.1    cegger 
    143  1.1    cegger 	switch (obj->Buffer.Length) {
    144  1.1    cegger 	case 1:
    145  1.1    cegger 		sc->sc_usageid = *(uint8_t *)obj->Buffer.Pointer;
    146  1.1    cegger 		break;
    147  1.1    cegger 	case 2:
    148  1.1    cegger 		sc->sc_usageid = le16toh(*(uint16_t *)obj->Buffer.Pointer);
    149  1.1    cegger 		break;
    150  1.1    cegger 	case 4:
    151  1.1    cegger 		sc->sc_usageid = le32toh(*(uint32_t *)obj->Buffer.Pointer);
    152  1.1    cegger 		break;
    153  1.1    cegger 	default:
    154  1.1    cegger 		aprint_debug_dev(dev, "unhandled ret.Length: 0x%lx\n",
    155  1.1    cegger 			(unsigned long)obj->Buffer.Length);
    156  1.1    cegger 		sc->sc_usageid = DALB_ID_INVALID;
    157  1.1    cegger 		break;
    158  1.1    cegger 	}
    159  1.1    cegger 
    160  1.1    cegger out:
    161  1.3   mlelstv 	ACPI_FREE(ret.Pointer);
    162  1.1    cegger }
    163  1.1    cegger 
    164  1.1    cegger static void
    165  1.1    cegger acpi_dalb_attach(device_t parent, device_t self, void *aux)
    166  1.1    cegger {
    167  1.1    cegger 	struct acpi_dalb_softc *sc = device_private(self);
    168  1.1    cegger 	struct acpi_attach_args *aa = aux;
    169  1.1    cegger 	ACPI_STATUS rv;
    170  1.1    cegger 
    171  1.1    cegger 	aprint_naive("\n");
    172  1.1    cegger 	aprint_normal(": Direct Application Launch Button\n");
    173  1.1    cegger 
    174  1.1    cegger 	sc->sc_node = aa->aa_node;
    175  1.1    cegger 	sc->sc_dev = self;
    176  1.1    cegger 
    177  1.1    cegger 	config_interrupts(self, acpi_dalb_init);
    178  1.1    cegger 
    179  1.1    cegger 	/* Install notify handler */
    180  1.1    cegger 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
    181  1.1    cegger 		ACPI_ALL_NOTIFY, acpi_dalb_notify_handler, self);
    182  1.1    cegger 	if (ACPI_FAILURE(rv))
    183  1.1    cegger 		aprint_error_dev(self,
    184  1.1    cegger 			"couldn't install notify handler: (%s)\n",
    185  1.1    cegger 			AcpiFormatException(rv));
    186  1.1    cegger 
    187  1.1    cegger 	sc->sc_smpsw_valid = false;
    188  1.1    cegger 	acpi_dalb_sysmon_init(sc);
    189  1.1    cegger 
    190  1.1    cegger 	if (!pmf_device_register(self, NULL, acpi_dalb_resume))
    191  1.1    cegger 		aprint_error_dev(self, "couldn't establish power handler\n");
    192  1.1    cegger }
    193  1.1    cegger 
    194  1.6    jruoho static int
    195  1.6    jruoho acpi_dalb_detach(device_t self, int flags)
    196  1.6    jruoho {
    197  1.6    jruoho 	struct acpi_dalb_softc *sc = device_private(self);
    198  1.6    jruoho 	ACPI_STATUS rv;
    199  1.6    jruoho 
    200  1.6    jruoho 	rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle,
    201  1.6    jruoho 	    ACPI_ALL_NOTIFY, acpi_dalb_notify_handler);
    202  1.6    jruoho 
    203  1.6    jruoho 	if (ACPI_FAILURE(rv))
    204  1.6    jruoho 		return EBUSY;
    205  1.6    jruoho 
    206  1.6    jruoho 	pmf_device_deregister(self);
    207  1.6    jruoho 	sysmon_pswitch_unregister(&sc->sc_smpsw);
    208  1.6    jruoho 
    209  1.6    jruoho 	return 0;
    210  1.6    jruoho }
    211  1.6    jruoho 
    212  1.1    cegger static void
    213  1.1    cegger acpi_dalb_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
    214  1.1    cegger {
    215  1.1    cegger 	device_t dev = opaque;
    216  1.1    cegger 	struct acpi_dalb_softc *sc = device_private(dev);
    217  1.1    cegger 	ACPI_STATUS rv;
    218  1.1    cegger 
    219  1.1    cegger 	switch (notify) {
    220  1.1    cegger 	case DALB_SYSTEM_WAKEUP:
    221  1.1    cegger 		rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
    222  1.1    cegger 			acpi_dalb_get_wakeup_hotkeys, dev);
    223  1.1    cegger 		break;
    224  1.1    cegger 	case DALB_SYSTEM_RUNTIME:
    225  1.1    cegger 		rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
    226  1.1    cegger 			acpi_dalb_get_runtime_hotkeys, dev);
    227  1.1    cegger 		break;
    228  1.1    cegger 
    229  1.1    cegger 	default:
    230  1.1    cegger 		aprint_error_dev(dev,
    231  1.1    cegger 			"unknown notification event 0x%x from button 0x%x\n",
    232  1.1    cegger 			notify, (uint32_t)sc->sc_usageid);
    233  1.1    cegger 		return;
    234  1.1    cegger 	}
    235  1.1    cegger 
    236  1.1    cegger 	if (ACPI_FAILURE(rv))
    237  1.1    cegger 		aprint_error_dev(dev, "couldn't queue hotkey handler: %s\n",
    238  1.1    cegger 			AcpiFormatException(rv));
    239  1.1    cegger }
    240  1.1    cegger 
    241  1.1    cegger static void
    242  1.1    cegger acpi_dalb_get_wakeup_hotkeys(void *opaque)
    243  1.1    cegger {
    244  1.1    cegger 	device_t dev = opaque;
    245  1.1    cegger 	struct acpi_dalb_softc *sc = device_private(dev);
    246  1.1    cegger 
    247  1.1    cegger 	if (!sc->sc_smpsw_valid)
    248  1.1    cegger 		return;
    249  1.1    cegger 	DPRINTF(("%s: %s: invoking sysmon_pswitch_event\n",
    250  1.1    cegger 		sc->sc_smpsw.smpsw_name, __func__));
    251  1.1    cegger 	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
    252  1.1    cegger }
    253  1.1    cegger 
    254  1.1    cegger static void
    255  1.1    cegger acpi_dalb_get_runtime_hotkeys(void *opaque)
    256  1.1    cegger {
    257  1.1    cegger 	device_t dev = opaque;
    258  1.1    cegger 	struct acpi_dalb_softc *sc = device_private(dev);
    259  1.1    cegger 
    260  1.1    cegger 	if (!sc->sc_smpsw_valid)
    261  1.1    cegger 		return;
    262  1.1    cegger 	DPRINTF(("%s: %s: invoking sysmon_pswitch_event\n",
    263  1.1    cegger 		sc->sc_smpsw.smpsw_name, __func__));
    264  1.1    cegger 	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
    265  1.1    cegger }
    266  1.1    cegger 
    267  1.1    cegger static bool
    268  1.5    dyoung acpi_dalb_resume(device_t dev, pmf_qual_t qual)
    269  1.1    cegger {
    270  1.2  jmcneill 	struct acpi_dalb_softc *sc = device_private(dev);
    271  1.2  jmcneill 	ACPI_STATUS rv;
    272  1.2  jmcneill 	ACPI_BUFFER ret;
    273  1.2  jmcneill 
    274  1.4    jruoho 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "GHID", &ret);
    275  1.2  jmcneill 	if (ACPI_FAILURE(rv)) {
    276  1.2  jmcneill 		aprint_error_dev(dev, "couldn't evaluate GHID: %s\n",
    277  1.2  jmcneill 		    AcpiFormatException(rv));
    278  1.2  jmcneill 		return false;
    279  1.2  jmcneill 	}
    280  1.2  jmcneill 	if (ret.Pointer)
    281  1.3   mlelstv 		ACPI_FREE(ret.Pointer);
    282  1.2  jmcneill 
    283  1.1    cegger 	return true;
    284  1.1    cegger }
    285