wmi_dell.c revision 1.3.2.2 1 1.3.2.2 uebayasi /* $NetBSD: wmi_dell.c,v 1.3.2.2 2010/04/30 14:43:07 uebayasi Exp $ */
2 1.3.2.2 uebayasi
3 1.3.2.2 uebayasi /*-
4 1.3.2.2 uebayasi * Copyright (c) 2009, 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_dell.c,v 1.3.2.2 2010/04/30 14:43:07 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
39 1.3.2.2 uebayasi #include <dev/acpi/acpireg.h>
40 1.3.2.2 uebayasi #include <dev/acpi/acpivar.h>
41 1.3.2.2 uebayasi #include <dev/acpi/wmi/wmi_acpivar.h>
42 1.3.2.2 uebayasi
43 1.3.2.2 uebayasi #include <dev/sysmon/sysmonvar.h>
44 1.3.2.2 uebayasi
45 1.3.2.2 uebayasi #define _COMPONENT ACPI_RESOURCE_COMPONENT
46 1.3.2.2 uebayasi ACPI_MODULE_NAME ("wmi_dell")
47 1.3.2.2 uebayasi
48 1.3.2.2 uebayasi #define WMI_DELL_HOTKEY_BRIGHTNESS_DOWN 0xE005
49 1.3.2.2 uebayasi #define WMI_DELL_HOTKEY_BRIGHTNESS_UP 0xE006
50 1.3.2.2 uebayasi #define WMI_DELL_HOTKEY_DISPLAY_CYCLE 0xE00B
51 1.3.2.2 uebayasi #define WMI_DELL_HOTKEY_VOLUME_MUTE 0xE020
52 1.3.2.2 uebayasi #define WMI_DELL_HOTKEY_VOLUME_DOWN 0xE02E
53 1.3.2.2 uebayasi #define WMI_DELL_HOTKEY_VOLUME_UP 0xE030
54 1.3.2.2 uebayasi /* WMI_DELL_HOTKEY_UNKNOWN 0xXXXX */
55 1.3.2.2 uebayasi
56 1.3.2.2 uebayasi #define WMI_DELL_PSW_DISPLAY_CYCLE 0
57 1.3.2.2 uebayasi #define WMI_DELL_PSW_COUNT 1
58 1.3.2.2 uebayasi
59 1.3.2.2 uebayasi #define WMI_DELL_GUID_EVENT "9DBB5994-A997-11DA-B012-B622A1EF5492"
60 1.3.2.2 uebayasi
61 1.3.2.2 uebayasi struct wmi_dell_softc {
62 1.3.2.2 uebayasi device_t sc_dev;
63 1.3.2.2 uebayasi device_t sc_parent;
64 1.3.2.2 uebayasi struct sysmon_pswitch sc_smpsw[WMI_DELL_PSW_COUNT];
65 1.3.2.2 uebayasi bool sc_smpsw_valid;
66 1.3.2.2 uebayasi };
67 1.3.2.2 uebayasi
68 1.3.2.2 uebayasi static int wmi_dell_match(device_t, cfdata_t, void *);
69 1.3.2.2 uebayasi static void wmi_dell_attach(device_t, device_t, void *);
70 1.3.2.2 uebayasi static int wmi_dell_detach(device_t, int);
71 1.3.2.2 uebayasi static void wmi_dell_notify_handler(ACPI_HANDLE, uint32_t, void *);
72 1.3.2.2 uebayasi static bool wmi_dell_suspend(device_t, const pmf_qual_t *);
73 1.3.2.2 uebayasi static bool wmi_dell_resume(device_t, const pmf_qual_t *);
74 1.3.2.2 uebayasi
75 1.3.2.2 uebayasi CFATTACH_DECL_NEW(wmidell, sizeof(struct wmi_dell_softc),
76 1.3.2.2 uebayasi wmi_dell_match, wmi_dell_attach, wmi_dell_detach, NULL);
77 1.3.2.2 uebayasi
78 1.3.2.2 uebayasi static int
79 1.3.2.2 uebayasi wmi_dell_match(device_t parent, cfdata_t match, void *aux)
80 1.3.2.2 uebayasi {
81 1.3.2.2 uebayasi return acpi_wmi_guid_match(parent, WMI_DELL_GUID_EVENT);
82 1.3.2.2 uebayasi }
83 1.3.2.2 uebayasi
84 1.3.2.2 uebayasi static void
85 1.3.2.2 uebayasi wmi_dell_attach(device_t parent, device_t self, void *aux)
86 1.3.2.2 uebayasi {
87 1.3.2.2 uebayasi struct wmi_dell_softc *sc = device_private(self);
88 1.3.2.2 uebayasi ACPI_STATUS rv;
89 1.3.2.2 uebayasi int e;
90 1.3.2.2 uebayasi
91 1.3.2.2 uebayasi sc->sc_dev = self;
92 1.3.2.2 uebayasi sc->sc_parent = parent;
93 1.3.2.2 uebayasi sc->sc_smpsw_valid = true;
94 1.3.2.2 uebayasi
95 1.3.2.2 uebayasi rv = acpi_wmi_event_register(parent, wmi_dell_notify_handler);
96 1.3.2.2 uebayasi
97 1.3.2.2 uebayasi if (ACPI_FAILURE(rv)) {
98 1.3.2.2 uebayasi aprint_error(": failed to install WMI notify handler\n");
99 1.3.2.2 uebayasi return;
100 1.3.2.2 uebayasi }
101 1.3.2.2 uebayasi
102 1.3.2.2 uebayasi aprint_naive("\n");
103 1.3.2.2 uebayasi aprint_normal(": Dell WMI mappings\n");
104 1.3.2.2 uebayasi
105 1.3.2.2 uebayasi sc->sc_smpsw[WMI_DELL_PSW_DISPLAY_CYCLE].smpsw_name =
106 1.3.2.2 uebayasi PSWITCH_HK_DISPLAY_CYCLE;
107 1.3.2.2 uebayasi
108 1.3.2.2 uebayasi sc->sc_smpsw[WMI_DELL_PSW_DISPLAY_CYCLE].smpsw_type =
109 1.3.2.2 uebayasi PSWITCH_TYPE_HOTKEY;
110 1.3.2.2 uebayasi
111 1.3.2.2 uebayasi e = sysmon_pswitch_register(&sc->sc_smpsw[WMI_DELL_PSW_DISPLAY_CYCLE]);
112 1.3.2.2 uebayasi
113 1.3.2.2 uebayasi if (e != 0)
114 1.3.2.2 uebayasi sc->sc_smpsw_valid = false;
115 1.3.2.2 uebayasi
116 1.3.2.2 uebayasi (void)pmf_device_register(self, wmi_dell_suspend, wmi_dell_resume);
117 1.3.2.2 uebayasi }
118 1.3.2.2 uebayasi
119 1.3.2.2 uebayasi static int
120 1.3.2.2 uebayasi wmi_dell_detach(device_t self, int flags)
121 1.3.2.2 uebayasi {
122 1.3.2.2 uebayasi struct wmi_dell_softc *sc = device_private(self);
123 1.3.2.2 uebayasi device_t parent = sc->sc_parent;
124 1.3.2.2 uebayasi int i;
125 1.3.2.2 uebayasi
126 1.3.2.2 uebayasi (void)pmf_device_deregister(self);
127 1.3.2.2 uebayasi (void)acpi_wmi_event_deregister(parent);
128 1.3.2.2 uebayasi
129 1.3.2.2 uebayasi if (sc->sc_smpsw_valid != true)
130 1.3.2.2 uebayasi return 0;
131 1.3.2.2 uebayasi
132 1.3.2.2 uebayasi for (i = 0; i < __arraycount(sc->sc_smpsw); i++)
133 1.3.2.2 uebayasi sysmon_pswitch_unregister(&sc->sc_smpsw[i]);
134 1.3.2.2 uebayasi
135 1.3.2.2 uebayasi return 0;
136 1.3.2.2 uebayasi }
137 1.3.2.2 uebayasi
138 1.3.2.2 uebayasi static bool
139 1.3.2.2 uebayasi wmi_dell_suspend(device_t self, const pmf_qual_t *qual)
140 1.3.2.2 uebayasi {
141 1.3.2.2 uebayasi struct wmi_dell_softc *sc = device_private(self);
142 1.3.2.2 uebayasi device_t parent = sc->sc_parent;
143 1.3.2.2 uebayasi
144 1.3.2.2 uebayasi (void)acpi_wmi_event_deregister(parent);
145 1.3.2.2 uebayasi
146 1.3.2.2 uebayasi return true;
147 1.3.2.2 uebayasi }
148 1.3.2.2 uebayasi
149 1.3.2.2 uebayasi static bool
150 1.3.2.2 uebayasi wmi_dell_resume(device_t self, const pmf_qual_t *qual)
151 1.3.2.2 uebayasi {
152 1.3.2.2 uebayasi struct wmi_dell_softc *sc = device_private(self);
153 1.3.2.2 uebayasi device_t parent = sc->sc_parent;
154 1.3.2.2 uebayasi
155 1.3.2.2 uebayasi (void)acpi_wmi_event_register(parent, wmi_dell_notify_handler);
156 1.3.2.2 uebayasi
157 1.3.2.2 uebayasi return true;
158 1.3.2.2 uebayasi }
159 1.3.2.2 uebayasi
160 1.3.2.2 uebayasi static void
161 1.3.2.2 uebayasi wmi_dell_notify_handler(ACPI_HANDLE hdl, uint32_t evt, void *aux)
162 1.3.2.2 uebayasi {
163 1.3.2.2 uebayasi struct wmi_dell_softc *sc;
164 1.3.2.2 uebayasi device_t self = aux;
165 1.3.2.2 uebayasi ACPI_OBJECT *obj;
166 1.3.2.2 uebayasi ACPI_BUFFER buf;
167 1.3.2.2 uebayasi ACPI_STATUS rv;
168 1.3.2.2 uebayasi uint32_t val;
169 1.3.2.2 uebayasi
170 1.3.2.2 uebayasi sc = device_private(self);
171 1.3.2.2 uebayasi rv = acpi_wmi_event_get(sc->sc_parent, evt, &buf);
172 1.3.2.2 uebayasi
173 1.3.2.2 uebayasi if (ACPI_FAILURE(rv))
174 1.3.2.2 uebayasi goto out;
175 1.3.2.2 uebayasi
176 1.3.2.2 uebayasi obj = buf.Pointer;
177 1.3.2.2 uebayasi
178 1.3.2.2 uebayasi if (obj->Type != ACPI_TYPE_BUFFER) {
179 1.3.2.2 uebayasi rv = AE_TYPE;
180 1.3.2.2 uebayasi goto out;
181 1.3.2.2 uebayasi }
182 1.3.2.2 uebayasi
183 1.3.2.2 uebayasi val = obj->Buffer.Pointer[1] & 0xFFFF;
184 1.3.2.2 uebayasi
185 1.3.2.2 uebayasi switch (val) {
186 1.3.2.2 uebayasi
187 1.3.2.2 uebayasi case WMI_DELL_HOTKEY_BRIGHTNESS_DOWN:
188 1.3.2.2 uebayasi pmf_event_inject(NULL, PMFE_DISPLAY_BRIGHTNESS_DOWN);
189 1.3.2.2 uebayasi break;
190 1.3.2.2 uebayasi
191 1.3.2.2 uebayasi case WMI_DELL_HOTKEY_BRIGHTNESS_UP:
192 1.3.2.2 uebayasi pmf_event_inject(NULL, PMFE_DISPLAY_BRIGHTNESS_UP);
193 1.3.2.2 uebayasi break;
194 1.3.2.2 uebayasi
195 1.3.2.2 uebayasi case WMI_DELL_HOTKEY_DISPLAY_CYCLE:
196 1.3.2.2 uebayasi
197 1.3.2.2 uebayasi if (sc->sc_smpsw_valid != true) {
198 1.3.2.2 uebayasi rv = AE_ABORT_METHOD;
199 1.3.2.2 uebayasi break;
200 1.3.2.2 uebayasi }
201 1.3.2.2 uebayasi
202 1.3.2.2 uebayasi sysmon_pswitch_event(&sc->sc_smpsw[WMI_DELL_PSW_DISPLAY_CYCLE],
203 1.3.2.2 uebayasi PSWITCH_EVENT_PRESSED);
204 1.3.2.2 uebayasi break;
205 1.3.2.2 uebayasi
206 1.3.2.2 uebayasi case WMI_DELL_HOTKEY_VOLUME_MUTE:
207 1.3.2.2 uebayasi pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_TOGGLE);
208 1.3.2.2 uebayasi break;
209 1.3.2.2 uebayasi
210 1.3.2.2 uebayasi case WMI_DELL_HOTKEY_VOLUME_DOWN:
211 1.3.2.2 uebayasi pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
212 1.3.2.2 uebayasi break;
213 1.3.2.2 uebayasi
214 1.3.2.2 uebayasi case WMI_DELL_HOTKEY_VOLUME_UP:
215 1.3.2.2 uebayasi pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
216 1.3.2.2 uebayasi break;
217 1.3.2.2 uebayasi
218 1.3.2.2 uebayasi default:
219 1.3.2.2 uebayasi aprint_debug_dev(sc->sc_dev,
220 1.3.2.2 uebayasi "unknown key 0x%02X for event 0x%02X\n", val, evt);
221 1.3.2.2 uebayasi break;
222 1.3.2.2 uebayasi }
223 1.3.2.2 uebayasi
224 1.3.2.2 uebayasi out:
225 1.3.2.2 uebayasi if (buf.Pointer != NULL)
226 1.3.2.2 uebayasi ACPI_FREE(buf.Pointer);
227 1.3.2.2 uebayasi
228 1.3.2.2 uebayasi if (ACPI_FAILURE(rv))
229 1.3.2.2 uebayasi aprint_error_dev(sc->sc_dev, "failed to get data for "
230 1.3.2.2 uebayasi "event 0x%02X: %s\n", evt, AcpiFormatException(rv));
231 1.3.2.2 uebayasi }
232