wmi_msi.c revision 1.3.2.2 1 1.3.2.2 uebayasi /* $NetBSD: wmi_msi.c,v 1.3.2.2 2010/11/06 08:08:28 uebayasi Exp $ */
2 1.3.2.2 uebayasi
3 1.3.2.2 uebayasi /*-
4 1.3.2.2 uebayasi * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 1.3.2.2 uebayasi * All rights reserved.
6 1.3.2.2 uebayasi *
7 1.3.2.2 uebayasi * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.2 uebayasi * by Jukka Ruohonen.
9 1.3.2.2 uebayasi *
10 1.3.2.2 uebayasi * Redistribution and use in source and binary forms, with or without
11 1.3.2.2 uebayasi * modification, are permitted provided that the following conditions
12 1.3.2.2 uebayasi * are met:
13 1.3.2.2 uebayasi *
14 1.3.2.2 uebayasi * 1. Redistributions of source code must retain the above copyright
15 1.3.2.2 uebayasi * notice, this list of conditions and the following disclaimer.
16 1.3.2.2 uebayasi * 2. Redistributions in binary form must reproduce the above copyright
17 1.3.2.2 uebayasi * notice, this list of conditions and the following disclaimer in the
18 1.3.2.2 uebayasi * documentation and/or other materials provided with the distribution.
19 1.3.2.2 uebayasi *
20 1.3.2.2 uebayasi * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 1.3.2.2 uebayasi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.3.2.2 uebayasi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.3.2.2 uebayasi * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 1.3.2.2 uebayasi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.3.2.2 uebayasi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.3.2.2 uebayasi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.3.2.2 uebayasi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.3.2.2 uebayasi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.3.2.2 uebayasi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.3.2.2 uebayasi * SUCH DAMAGE.
31 1.3.2.2 uebayasi */
32 1.3.2.2 uebayasi
33 1.3.2.2 uebayasi #include <sys/cdefs.h>
34 1.3.2.2 uebayasi __KERNEL_RCSID(0, "$NetBSD: wmi_msi.c,v 1.3.2.2 2010/11/06 08:08:28 uebayasi Exp $");
35 1.3.2.2 uebayasi
36 1.3.2.2 uebayasi #include <sys/param.h>
37 1.3.2.2 uebayasi #include <sys/device.h>
38 1.3.2.2 uebayasi #include <sys/module.h>
39 1.3.2.2 uebayasi
40 1.3.2.2 uebayasi #include <dev/acpi/acpireg.h>
41 1.3.2.2 uebayasi #include <dev/acpi/acpivar.h>
42 1.3.2.2 uebayasi #include <dev/acpi/wmi/wmi_acpivar.h>
43 1.3.2.2 uebayasi
44 1.3.2.2 uebayasi #define _COMPONENT ACPI_RESOURCE_COMPONENT
45 1.3.2.2 uebayasi ACPI_MODULE_NAME ("wmi_msi")
46 1.3.2.2 uebayasi
47 1.3.2.2 uebayasi #define WMI_MSI_HOTKEY_BRIGHTNESS_UP 0xD0
48 1.3.2.2 uebayasi #define WMI_MSI_HOTKEY_BRIGHTNESS_DOWN 0xD1
49 1.3.2.2 uebayasi #define WMI_MSI_HOTKEY_VOLUME_UP 0xD2
50 1.3.2.2 uebayasi #define WMI_MSI_HOTKEY_VOLUME_DOWN 0xD3
51 1.3.2.2 uebayasi /* WMI_MSI_HOTKEY_UNKNOWN 0xXXXX */
52 1.3.2.2 uebayasi
53 1.3.2.2 uebayasi #define WMI_MSI_GUID_EVENT "B6F3EEF2-3D2F-49DC-9DE3-85BCE18C62F2"
54 1.3.2.2 uebayasi
55 1.3.2.2 uebayasi struct wmi_msi_softc {
56 1.3.2.2 uebayasi device_t sc_dev;
57 1.3.2.2 uebayasi device_t sc_parent;
58 1.3.2.2 uebayasi };
59 1.3.2.2 uebayasi
60 1.3.2.2 uebayasi static int wmi_msi_match(device_t, cfdata_t, void *);
61 1.3.2.2 uebayasi static void wmi_msi_attach(device_t, device_t, void *);
62 1.3.2.2 uebayasi static int wmi_msi_detach(device_t, int);
63 1.3.2.2 uebayasi static void wmi_msi_notify_handler(ACPI_HANDLE, uint32_t, void *);
64 1.3.2.2 uebayasi static bool wmi_msi_suspend(device_t, const pmf_qual_t *);
65 1.3.2.2 uebayasi static bool wmi_msi_resume(device_t, const pmf_qual_t *);
66 1.3.2.2 uebayasi
67 1.3.2.2 uebayasi CFATTACH_DECL_NEW(wmimsi, sizeof(struct wmi_msi_softc),
68 1.3.2.2 uebayasi wmi_msi_match, wmi_msi_attach, wmi_msi_detach, NULL);
69 1.3.2.2 uebayasi
70 1.3.2.2 uebayasi static int
71 1.3.2.2 uebayasi wmi_msi_match(device_t parent, cfdata_t match, void *aux)
72 1.3.2.2 uebayasi {
73 1.3.2.2 uebayasi return acpi_wmi_guid_match(parent, WMI_MSI_GUID_EVENT);
74 1.3.2.2 uebayasi }
75 1.3.2.2 uebayasi
76 1.3.2.2 uebayasi static void
77 1.3.2.2 uebayasi wmi_msi_attach(device_t parent, device_t self, void *aux)
78 1.3.2.2 uebayasi {
79 1.3.2.2 uebayasi struct wmi_msi_softc *sc = device_private(self);
80 1.3.2.2 uebayasi ACPI_STATUS rv;
81 1.3.2.2 uebayasi
82 1.3.2.2 uebayasi sc->sc_dev = self;
83 1.3.2.2 uebayasi sc->sc_parent = parent;
84 1.3.2.2 uebayasi
85 1.3.2.2 uebayasi rv = acpi_wmi_event_register(parent, wmi_msi_notify_handler);
86 1.3.2.2 uebayasi
87 1.3.2.2 uebayasi if (ACPI_FAILURE(rv)) {
88 1.3.2.2 uebayasi aprint_error(": failed to install WMI notify handler\n");
89 1.3.2.2 uebayasi return;
90 1.3.2.2 uebayasi }
91 1.3.2.2 uebayasi
92 1.3.2.2 uebayasi aprint_naive("\n");
93 1.3.2.2 uebayasi aprint_normal(": MSI WMI mappings\n");
94 1.3.2.2 uebayasi
95 1.3.2.2 uebayasi (void)pmf_device_register(self, wmi_msi_suspend, wmi_msi_resume);
96 1.3.2.2 uebayasi }
97 1.3.2.2 uebayasi
98 1.3.2.2 uebayasi static int
99 1.3.2.2 uebayasi wmi_msi_detach(device_t self, int flags)
100 1.3.2.2 uebayasi {
101 1.3.2.2 uebayasi struct wmi_msi_softc *sc = device_private(self);
102 1.3.2.2 uebayasi device_t parent = sc->sc_parent;
103 1.3.2.2 uebayasi
104 1.3.2.2 uebayasi (void)pmf_device_deregister(self);
105 1.3.2.2 uebayasi (void)acpi_wmi_event_deregister(parent);
106 1.3.2.2 uebayasi
107 1.3.2.2 uebayasi return 0;
108 1.3.2.2 uebayasi }
109 1.3.2.2 uebayasi
110 1.3.2.2 uebayasi static bool
111 1.3.2.2 uebayasi wmi_msi_suspend(device_t self, const pmf_qual_t *qual)
112 1.3.2.2 uebayasi {
113 1.3.2.2 uebayasi struct wmi_msi_softc *sc = device_private(self);
114 1.3.2.2 uebayasi device_t parent = sc->sc_parent;
115 1.3.2.2 uebayasi
116 1.3.2.2 uebayasi (void)acpi_wmi_event_deregister(parent);
117 1.3.2.2 uebayasi
118 1.3.2.2 uebayasi return true;
119 1.3.2.2 uebayasi }
120 1.3.2.2 uebayasi
121 1.3.2.2 uebayasi static bool
122 1.3.2.2 uebayasi wmi_msi_resume(device_t self, const pmf_qual_t *qual)
123 1.3.2.2 uebayasi {
124 1.3.2.2 uebayasi struct wmi_msi_softc *sc = device_private(self);
125 1.3.2.2 uebayasi device_t parent = sc->sc_parent;
126 1.3.2.2 uebayasi
127 1.3.2.2 uebayasi (void)acpi_wmi_event_register(parent, wmi_msi_notify_handler);
128 1.3.2.2 uebayasi
129 1.3.2.2 uebayasi return true;
130 1.3.2.2 uebayasi }
131 1.3.2.2 uebayasi
132 1.3.2.2 uebayasi static void
133 1.3.2.2 uebayasi wmi_msi_notify_handler(ACPI_HANDLE hdl, uint32_t evt, void *aux)
134 1.3.2.2 uebayasi {
135 1.3.2.2 uebayasi struct wmi_msi_softc *sc;
136 1.3.2.2 uebayasi device_t self = aux;
137 1.3.2.2 uebayasi ACPI_OBJECT *obj;
138 1.3.2.2 uebayasi ACPI_BUFFER buf;
139 1.3.2.2 uebayasi ACPI_STATUS rv;
140 1.3.2.2 uebayasi uint32_t val;
141 1.3.2.2 uebayasi
142 1.3.2.2 uebayasi buf.Pointer = NULL;
143 1.3.2.2 uebayasi
144 1.3.2.2 uebayasi sc = device_private(self);
145 1.3.2.2 uebayasi rv = acpi_wmi_event_get(sc->sc_parent, evt, &buf);
146 1.3.2.2 uebayasi
147 1.3.2.2 uebayasi if (ACPI_FAILURE(rv))
148 1.3.2.2 uebayasi goto out;
149 1.3.2.2 uebayasi
150 1.3.2.2 uebayasi obj = buf.Pointer;
151 1.3.2.2 uebayasi
152 1.3.2.2 uebayasi if (obj->Type != ACPI_TYPE_INTEGER) {
153 1.3.2.2 uebayasi rv = AE_TYPE;
154 1.3.2.2 uebayasi goto out;
155 1.3.2.2 uebayasi }
156 1.3.2.2 uebayasi
157 1.3.2.2 uebayasi if (obj->Integer.Value > UINT32_MAX) {
158 1.3.2.2 uebayasi rv = AE_AML_NUMERIC_OVERFLOW;
159 1.3.2.2 uebayasi goto out;
160 1.3.2.2 uebayasi }
161 1.3.2.2 uebayasi
162 1.3.2.2 uebayasi val = obj->Integer.Value;
163 1.3.2.2 uebayasi
164 1.3.2.2 uebayasi switch (val) {
165 1.3.2.2 uebayasi
166 1.3.2.2 uebayasi case WMI_MSI_HOTKEY_BRIGHTNESS_DOWN:
167 1.3.2.2 uebayasi pmf_event_inject(NULL, PMFE_DISPLAY_BRIGHTNESS_DOWN);
168 1.3.2.2 uebayasi break;
169 1.3.2.2 uebayasi
170 1.3.2.2 uebayasi case WMI_MSI_HOTKEY_BRIGHTNESS_UP:
171 1.3.2.2 uebayasi pmf_event_inject(NULL, PMFE_DISPLAY_BRIGHTNESS_UP);
172 1.3.2.2 uebayasi break;
173 1.3.2.2 uebayasi
174 1.3.2.2 uebayasi case WMI_MSI_HOTKEY_VOLUME_DOWN:
175 1.3.2.2 uebayasi pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
176 1.3.2.2 uebayasi break;
177 1.3.2.2 uebayasi
178 1.3.2.2 uebayasi case WMI_MSI_HOTKEY_VOLUME_UP:
179 1.3.2.2 uebayasi pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
180 1.3.2.2 uebayasi break;
181 1.3.2.2 uebayasi
182 1.3.2.2 uebayasi default:
183 1.3.2.2 uebayasi aprint_normal_dev(sc->sc_dev,
184 1.3.2.2 uebayasi "unknown key 0x%02X for event 0x%02X\n", val, evt);
185 1.3.2.2 uebayasi break;
186 1.3.2.2 uebayasi }
187 1.3.2.2 uebayasi
188 1.3.2.2 uebayasi out:
189 1.3.2.2 uebayasi if (buf.Pointer != NULL)
190 1.3.2.2 uebayasi ACPI_FREE(buf.Pointer);
191 1.3.2.2 uebayasi
192 1.3.2.2 uebayasi if (ACPI_FAILURE(rv))
193 1.3.2.2 uebayasi aprint_error_dev(sc->sc_dev, "failed to get data for "
194 1.3.2.2 uebayasi "event 0x%02X: %s\n", evt, AcpiFormatException(rv));
195 1.3.2.2 uebayasi }
196 1.3.2.2 uebayasi
197 1.3.2.2 uebayasi #ifdef _MODULE
198 1.3.2.2 uebayasi
199 1.3.2.2 uebayasi MODULE(MODULE_CLASS_DRIVER, wmimsi, NULL);
200 1.3.2.2 uebayasi CFDRIVER_DECL(wmimsi, DV_DULL, NULL);
201 1.3.2.2 uebayasi
202 1.3.2.2 uebayasi static int wmimsiloc[] = { -1 };
203 1.3.2.2 uebayasi extern struct cfattach wmimsi_ca;
204 1.3.2.2 uebayasi
205 1.3.2.2 uebayasi static struct cfparent wmiparent = {
206 1.3.2.2 uebayasi "acpiwmibus", NULL, DVUNIT_ANY
207 1.3.2.2 uebayasi };
208 1.3.2.2 uebayasi
209 1.3.2.2 uebayasi static struct cfdata wmimsi_cfdata[] = {
210 1.3.2.2 uebayasi {
211 1.3.2.2 uebayasi .cf_name = "wmimsi",
212 1.3.2.2 uebayasi .cf_atname = "wmimsi",
213 1.3.2.2 uebayasi .cf_unit = 0,
214 1.3.2.2 uebayasi .cf_fstate = FSTATE_STAR,
215 1.3.2.2 uebayasi .cf_loc = wmimsiloc,
216 1.3.2.2 uebayasi .cf_flags = 0,
217 1.3.2.2 uebayasi .cf_pspec = &wmiparent,
218 1.3.2.2 uebayasi },
219 1.3.2.2 uebayasi
220 1.3.2.2 uebayasi { NULL, NULL, 0, 0, NULL, 0, NULL }
221 1.3.2.2 uebayasi };
222 1.3.2.2 uebayasi
223 1.3.2.2 uebayasi static int
224 1.3.2.2 uebayasi wmimsi_modcmd(modcmd_t cmd, void *opaque)
225 1.3.2.2 uebayasi {
226 1.3.2.2 uebayasi int err;
227 1.3.2.2 uebayasi
228 1.3.2.2 uebayasi switch (cmd) {
229 1.3.2.2 uebayasi
230 1.3.2.2 uebayasi case MODULE_CMD_INIT:
231 1.3.2.2 uebayasi
232 1.3.2.2 uebayasi err = config_cfdriver_attach(&wmimsi_cd);
233 1.3.2.2 uebayasi
234 1.3.2.2 uebayasi if (err != 0)
235 1.3.2.2 uebayasi return err;
236 1.3.2.2 uebayasi
237 1.3.2.2 uebayasi err = config_cfattach_attach("wmimsi", &wmimsi_ca);
238 1.3.2.2 uebayasi
239 1.3.2.2 uebayasi if (err != 0) {
240 1.3.2.2 uebayasi config_cfdriver_detach(&wmimsi_cd);
241 1.3.2.2 uebayasi return err;
242 1.3.2.2 uebayasi }
243 1.3.2.2 uebayasi
244 1.3.2.2 uebayasi err = config_cfdata_attach(wmimsi_cfdata, 1);
245 1.3.2.2 uebayasi
246 1.3.2.2 uebayasi if (err != 0) {
247 1.3.2.2 uebayasi config_cfattach_detach("wmimsi", &wmimsi_ca);
248 1.3.2.2 uebayasi config_cfdriver_detach(&wmimsi_cd);
249 1.3.2.2 uebayasi return err;
250 1.3.2.2 uebayasi }
251 1.3.2.2 uebayasi
252 1.3.2.2 uebayasi return 0;
253 1.3.2.2 uebayasi
254 1.3.2.2 uebayasi case MODULE_CMD_FINI:
255 1.3.2.2 uebayasi
256 1.3.2.2 uebayasi err = config_cfdata_detach(wmimsi_cfdata);
257 1.3.2.2 uebayasi
258 1.3.2.2 uebayasi if (err != 0)
259 1.3.2.2 uebayasi return err;
260 1.3.2.2 uebayasi
261 1.3.2.2 uebayasi config_cfattach_detach("wmimsi", &wmimsi_ca);
262 1.3.2.2 uebayasi config_cfdriver_detach(&wmimsi_cd);
263 1.3.2.2 uebayasi
264 1.3.2.2 uebayasi return 0;
265 1.3.2.2 uebayasi
266 1.3.2.2 uebayasi default:
267 1.3.2.2 uebayasi return ENOTTY;
268 1.3.2.2 uebayasi }
269 1.3.2.2 uebayasi }
270 1.3.2.2 uebayasi
271 1.3.2.2 uebayasi #endif /* _MODULE */
272