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