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