dalb_acpi.c revision 1.3 1 /* $NetBSD: dalb_acpi.c,v 1.3 2009/09/16 10:47:55 mlelstv 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.3 2009/09/16 10:47:55 mlelstv 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 ret.Pointer = NULL;
128 ret.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
129
130 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "GHID", NULL, &ret);
131 if (ACPI_FAILURE(rv) || ret.Pointer == NULL) {
132 aprint_error_dev(dev,
133 "couldn't enable notify handler: (%s)\n",
134 AcpiFormatException(rv));
135 return;
136 }
137
138 obj = (ACPI_OBJECT *)ret.Pointer;
139 if (obj->Type != ACPI_TYPE_BUFFER) {
140 sc->sc_usageid = DALB_ID_INVALID;
141 aprint_debug_dev(dev, "invalid ACPI type: %d\n", obj->Type);
142 goto out;
143 }
144
145 switch (obj->Buffer.Length) {
146 case 1:
147 sc->sc_usageid = *(uint8_t *)obj->Buffer.Pointer;
148 break;
149 case 2:
150 sc->sc_usageid = le16toh(*(uint16_t *)obj->Buffer.Pointer);
151 break;
152 case 4:
153 sc->sc_usageid = le32toh(*(uint32_t *)obj->Buffer.Pointer);
154 break;
155 default:
156 aprint_debug_dev(dev, "unhandled ret.Length: 0x%lx\n",
157 (unsigned long)obj->Buffer.Length);
158 sc->sc_usageid = DALB_ID_INVALID;
159 break;
160 }
161
162 out:
163 ACPI_FREE(ret.Pointer);
164 }
165
166 static void
167 acpi_dalb_attach(device_t parent, device_t self, void *aux)
168 {
169 struct acpi_dalb_softc *sc = device_private(self);
170 struct acpi_attach_args *aa = aux;
171 ACPI_STATUS rv;
172
173 aprint_naive("\n");
174 aprint_normal(": Direct Application Launch Button\n");
175
176 sc->sc_node = aa->aa_node;
177 sc->sc_dev = self;
178
179 config_interrupts(self, acpi_dalb_init);
180
181 /* Install notify handler */
182 rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
183 ACPI_ALL_NOTIFY, acpi_dalb_notify_handler, self);
184 if (ACPI_FAILURE(rv))
185 aprint_error_dev(self,
186 "couldn't install notify handler: (%s)\n",
187 AcpiFormatException(rv));
188
189 sc->sc_smpsw_valid = false;
190 acpi_dalb_sysmon_init(sc);
191
192 if (!pmf_device_register(self, NULL, acpi_dalb_resume))
193 aprint_error_dev(self, "couldn't establish power handler\n");
194 }
195
196 static void
197 acpi_dalb_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
198 {
199 device_t dev = opaque;
200 struct acpi_dalb_softc *sc = device_private(dev);
201 ACPI_STATUS rv;
202
203 switch (notify) {
204 case DALB_SYSTEM_WAKEUP:
205 rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
206 acpi_dalb_get_wakeup_hotkeys, dev);
207 break;
208 case DALB_SYSTEM_RUNTIME:
209 rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
210 acpi_dalb_get_runtime_hotkeys, dev);
211 break;
212
213 default:
214 aprint_error_dev(dev,
215 "unknown notification event 0x%x from button 0x%x\n",
216 notify, (uint32_t)sc->sc_usageid);
217 return;
218 }
219
220 if (ACPI_FAILURE(rv))
221 aprint_error_dev(dev, "couldn't queue hotkey handler: %s\n",
222 AcpiFormatException(rv));
223 }
224
225 static void
226 acpi_dalb_get_wakeup_hotkeys(void *opaque)
227 {
228 device_t dev = opaque;
229 struct acpi_dalb_softc *sc = device_private(dev);
230
231 if (!sc->sc_smpsw_valid)
232 return;
233 DPRINTF(("%s: %s: invoking sysmon_pswitch_event\n",
234 sc->sc_smpsw.smpsw_name, __func__));
235 sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
236 }
237
238 static void
239 acpi_dalb_get_runtime_hotkeys(void *opaque)
240 {
241 device_t dev = opaque;
242 struct acpi_dalb_softc *sc = device_private(dev);
243
244 if (!sc->sc_smpsw_valid)
245 return;
246 DPRINTF(("%s: %s: invoking sysmon_pswitch_event\n",
247 sc->sc_smpsw.smpsw_name, __func__));
248 sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
249 }
250
251 static bool
252 acpi_dalb_resume(device_t dev PMF_FN_ARGS)
253 {
254 struct acpi_dalb_softc *sc = device_private(dev);
255 ACPI_STATUS rv;
256 ACPI_BUFFER ret;
257
258 ret.Pointer = NULL;
259 ret.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
260
261 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "GHID", NULL, &ret);
262 if (ACPI_FAILURE(rv)) {
263 aprint_error_dev(dev, "couldn't evaluate GHID: %s\n",
264 AcpiFormatException(rv));
265 return false;
266 }
267 if (ret.Pointer)
268 ACPI_FREE(ret.Pointer);
269
270 return true;
271 }
272