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