xref: /netbsd-src/sys/dev/acpi/dalb_acpi.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: dalb_acpi.c,v 1.15 2010/04/24 19:36:14 jruoho Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 Christoph Egger <cegger@netbsd.org>
5  * Copyright (c) 2008 Jared D. McNeill <jmcneill@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.15 2010/04/24 19:36:14 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/device.h>
39 #include <sys/systm.h>
40 
41 #include <dev/acpi/acpireg.h>
42 #include <dev/acpi/acpivar.h>
43 
44 #define _COMPONENT          ACPI_RESOURCE_COMPONENT
45 ACPI_MODULE_NAME            ("dalb_acpi")
46 
47 #define DALB_ID_INVALID		-1
48 
49 struct acpi_dalb_softc {
50 	device_t sc_dev;
51 	struct acpi_devnode *sc_node;
52 
53 	ACPI_INTEGER sc_usageid;
54 
55 	/* There's one PNP0C32 ACPI device for each button.
56 	 * Therefore, one instance is enough. */
57 	struct sysmon_pswitch sc_smpsw;
58 	bool sc_smpsw_valid;
59 };
60 
61 static int	acpi_dalb_match(device_t, cfdata_t, void *);
62 static void	acpi_dalb_attach(device_t, device_t, void *);
63 static int	acpi_dalb_detach(device_t, int);
64 static void	acpi_dalb_notify_handler(ACPI_HANDLE, uint32_t, void *);
65 static bool	acpi_dalb_resume(device_t, const pmf_qual_t *);
66 
67 static void	acpi_dalb_get_wakeup_hotkeys(void *opaque);
68 static void	acpi_dalb_get_runtime_hotkeys(void *opaque);
69 
70 CFATTACH_DECL_NEW(acpidalb, sizeof(struct acpi_dalb_softc),
71     acpi_dalb_match, acpi_dalb_attach, acpi_dalb_detach, NULL);
72 
73 static const char * const acpi_dalb_ids[] = {
74         "PNP0C32", /* Direct Application Launch Button */
75         NULL
76 };
77 
78 #define DALB_SYSTEM_WAKEUP	0x02
79 #define DALB_SYSTEM_RUNTIME	0x80
80 
81 static int
82 acpi_dalb_match(device_t parent, cfdata_t match, void *aux)
83 {
84 	struct acpi_attach_args *aa = aux;
85 
86 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
87 		return 0;
88 
89 	return acpi_match_hid(aa->aa_node->ad_devinfo, acpi_dalb_ids);
90 }
91 
92 static void
93 acpi_dalb_sysmon_init(struct acpi_dalb_softc *sc)
94 {
95 	sc->sc_smpsw_valid = true;
96 	sc->sc_smpsw.smpsw_name = device_xname(sc->sc_dev);
97 	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_HOTKEY;
98 	if (sysmon_pswitch_register(&sc->sc_smpsw)) {
99 		aprint_error_dev(sc->sc_dev,
100 			"couldn't register sleep with sysmon\n");
101 		sc->sc_smpsw_valid = false;
102 	}
103 }
104 
105 
106 static void
107 acpi_dalb_init(device_t dev)
108 {
109 	struct acpi_dalb_softc *sc = device_private(dev);
110 	ACPI_OBJECT *obj;
111 	ACPI_STATUS rv;
112 	ACPI_BUFFER ret;
113 
114 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "GHID", &ret);
115 
116 	if (ACPI_FAILURE(rv) || ret.Pointer == NULL) {
117 		aprint_error_dev(dev,
118 			"couldn't enable notify handler: (%s)\n",
119 			AcpiFormatException(rv));
120 		return;
121 	}
122 
123 	obj = ret.Pointer;
124 
125 	if (obj->Type != ACPI_TYPE_BUFFER) {
126 		sc->sc_usageid = DALB_ID_INVALID;
127 		aprint_debug_dev(dev, "invalid ACPI type: %u\n", obj->Type);
128 		goto out;
129 	}
130 
131 	switch (obj->Buffer.Length) {
132 	case 1:
133 		sc->sc_usageid = *(uint8_t *)obj->Buffer.Pointer;
134 		break;
135 	case 2:
136 		sc->sc_usageid = le16toh(*(uint16_t *)obj->Buffer.Pointer);
137 		break;
138 	case 4:
139 		sc->sc_usageid = le32toh(*(uint32_t *)obj->Buffer.Pointer);
140 		break;
141 	default:
142 		aprint_debug_dev(dev, "unhandled ret.Length: 0x%lx\n",
143 			(unsigned long)obj->Buffer.Length);
144 		sc->sc_usageid = DALB_ID_INVALID;
145 		break;
146 	}
147 
148 out:
149 	ACPI_FREE(ret.Pointer);
150 }
151 
152 static void
153 acpi_dalb_attach(device_t parent, device_t self, void *aux)
154 {
155 	struct acpi_dalb_softc *sc = device_private(self);
156 	struct acpi_attach_args *aa = aux;
157 
158 	aprint_naive("\n");
159 	aprint_normal(": Direct Application Launch Button\n");
160 
161 	sc->sc_dev = self;
162 	sc->sc_node = aa->aa_node;
163 
164 	config_interrupts(self, acpi_dalb_init);
165 
166 	(void)pmf_device_register(self, NULL, acpi_dalb_resume);
167 	(void)acpi_register_notify(sc->sc_node, acpi_dalb_notify_handler);
168 
169 	sc->sc_smpsw_valid = false;
170 	acpi_dalb_sysmon_init(sc);
171 }
172 
173 static int
174 acpi_dalb_detach(device_t self, int flags)
175 {
176 	struct acpi_dalb_softc *sc = device_private(self);
177 
178 	pmf_device_deregister(self);
179 	acpi_deregister_notify(sc->sc_node);
180 	sysmon_pswitch_unregister(&sc->sc_smpsw);
181 
182 	return 0;
183 }
184 
185 static void
186 acpi_dalb_notify_handler(ACPI_HANDLE hdl, uint32_t notify, void *opaque)
187 {
188 	device_t dev = opaque;
189 	struct acpi_dalb_softc *sc = device_private(dev);
190 	ACPI_STATUS rv;
191 
192 	switch (notify) {
193 	case DALB_SYSTEM_WAKEUP:
194 		rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
195 			acpi_dalb_get_wakeup_hotkeys, dev);
196 		break;
197 	case DALB_SYSTEM_RUNTIME:
198 		rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
199 			acpi_dalb_get_runtime_hotkeys, dev);
200 		break;
201 
202 	default:
203 		aprint_error_dev(dev,
204 			"unknown notification event 0x%x from button 0x%x\n",
205 			notify, (uint32_t)sc->sc_usageid);
206 		return;
207 	}
208 
209 	if (ACPI_FAILURE(rv))
210 		aprint_error_dev(dev, "couldn't queue hotkey handler: %s\n",
211 			AcpiFormatException(rv));
212 }
213 
214 static void
215 acpi_dalb_get_wakeup_hotkeys(void *opaque)
216 {
217 	device_t dev = opaque;
218 	struct acpi_dalb_softc *sc = device_private(dev);
219 
220 	if (!sc->sc_smpsw_valid)
221 		return;
222 
223 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
224 		"invoking %s (wakeup)\n", sc->sc_smpsw.smpsw_name));
225 
226 	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
227 }
228 
229 static void
230 acpi_dalb_get_runtime_hotkeys(void *opaque)
231 {
232 	device_t dev = opaque;
233 	struct acpi_dalb_softc *sc = device_private(dev);
234 
235 	if (!sc->sc_smpsw_valid)
236 		return;
237 
238 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
239 		"invoking %s (runtime)\n", sc->sc_smpsw.smpsw_name));
240 
241 	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
242 }
243 
244 static bool
245 acpi_dalb_resume(device_t dev, const pmf_qual_t *qual)
246 {
247 	struct acpi_dalb_softc *sc = device_private(dev);
248 	ACPI_STATUS rv;
249 	ACPI_BUFFER ret;
250 
251 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "GHID", &ret);
252 	if (ACPI_FAILURE(rv)) {
253 		aprint_error_dev(dev, "couldn't evaluate GHID: %s\n",
254 		    AcpiFormatException(rv));
255 		return false;
256 	}
257 	if (ret.Pointer)
258 		ACPI_FREE(ret.Pointer);
259 
260 	return true;
261 }
262