wmi_hp.c revision 1.1.2.3 1 1.1.2.2 uebayasi /* $NetBSD: wmi_hp.c,v 1.1.2.3 2010/08/17 06:46:03 uebayasi Exp $ */
2 1.1.2.2 uebayasi
3 1.1.2.2 uebayasi /*-
4 1.1.2.2 uebayasi * Copyright (c) 2009, 2010 The NetBSD Foundation, Inc.
5 1.1.2.2 uebayasi * All rights reserved.
6 1.1.2.2 uebayasi *
7 1.1.2.2 uebayasi * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.2 uebayasi * by Jukka Ruohonen.
9 1.1.2.2 uebayasi *
10 1.1.2.2 uebayasi * Redistribution and use in source and binary forms, with or without
11 1.1.2.2 uebayasi * modification, are permitted provided that the following conditions
12 1.1.2.2 uebayasi * are met:
13 1.1.2.2 uebayasi *
14 1.1.2.2 uebayasi * 1. Redistributions of source code must retain the above copyright
15 1.1.2.2 uebayasi * notice, this list of conditions and the following disclaimer.
16 1.1.2.2 uebayasi * 2. Redistributions in binary form must reproduce the above copyright
17 1.1.2.2 uebayasi * notice, this list of conditions and the following disclaimer in the
18 1.1.2.2 uebayasi * documentation and/or other materials provided with the distribution.
19 1.1.2.2 uebayasi *
20 1.1.2.2 uebayasi * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 1.1.2.2 uebayasi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1.2.2 uebayasi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1.2.2 uebayasi * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 1.1.2.2 uebayasi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1.2.2 uebayasi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1.2.2 uebayasi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1.2.2 uebayasi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1.2.2 uebayasi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1.2.2 uebayasi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1.2.2 uebayasi * SUCH DAMAGE.
31 1.1.2.2 uebayasi */
32 1.1.2.2 uebayasi
33 1.1.2.2 uebayasi /*-
34 1.1.2.2 uebayasi * Copyright (c) 2009 Michael Gmelin <freebsd (at) grem.de>
35 1.1.2.2 uebayasi * All rights reserved.
36 1.1.2.2 uebayasi *
37 1.1.2.2 uebayasi * Redistribution and use in source and binary forms, with or without
38 1.1.2.2 uebayasi * modification, are permitted provided that the following conditions
39 1.1.2.2 uebayasi * are met:
40 1.1.2.2 uebayasi * 1. Redistributions of source code must retain the above copyright
41 1.1.2.2 uebayasi * notice, this list of conditions and the following disclaimer.
42 1.1.2.2 uebayasi * 2. Redistributions in binary form must reproduce the above copyright
43 1.1.2.2 uebayasi * notice, this list of conditions and the following disclaimer in the
44 1.1.2.2 uebayasi * documentation and/or other materials provided with the distribution.
45 1.1.2.2 uebayasi *
46 1.1.2.2 uebayasi * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47 1.1.2.2 uebayasi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 1.1.2.2 uebayasi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 1.1.2.2 uebayasi * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50 1.1.2.2 uebayasi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 1.1.2.2 uebayasi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 1.1.2.2 uebayasi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 1.1.2.2 uebayasi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 1.1.2.2 uebayasi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 1.1.2.2 uebayasi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 1.1.2.2 uebayasi * SUCH DAMAGE.
57 1.1.2.2 uebayasi */
58 1.1.2.2 uebayasi
59 1.1.2.2 uebayasi #include <sys/cdefs.h>
60 1.1.2.2 uebayasi __KERNEL_RCSID(0, "$NetBSD: wmi_hp.c,v 1.1.2.3 2010/08/17 06:46:03 uebayasi Exp $");
61 1.1.2.2 uebayasi
62 1.1.2.2 uebayasi #include <sys/param.h>
63 1.1.2.2 uebayasi #include <sys/device.h>
64 1.1.2.2 uebayasi #include <sys/kmem.h>
65 1.1.2.2 uebayasi
66 1.1.2.2 uebayasi #include <dev/acpi/acpireg.h>
67 1.1.2.2 uebayasi #include <dev/acpi/acpivar.h>
68 1.1.2.2 uebayasi #include <dev/acpi/wmi/wmi_acpivar.h>
69 1.1.2.2 uebayasi
70 1.1.2.2 uebayasi #include <dev/sysmon/sysmonvar.h>
71 1.1.2.2 uebayasi
72 1.1.2.2 uebayasi #define _COMPONENT ACPI_RESOURCE_COMPONENT
73 1.1.2.2 uebayasi ACPI_MODULE_NAME ("wmi_hp")
74 1.1.2.2 uebayasi
75 1.1.2.2 uebayasi #define WMI_HP_METHOD_ARG_READ 0x01
76 1.1.2.2 uebayasi #define WMI_HP_METHOD_ARG_WRITE 0x02
77 1.1.2.2 uebayasi #define WMI_HP_METHOD_ARG_WRITE_SIZE 0x04
78 1.1.2.2 uebayasi #define WMI_HP_METHOD_ARG_MAGIC 0x55434553
79 1.1.2.2 uebayasi #define WMI_HP_METHOD_ARG_SIZE 0x05 * sizeof(uint32_t)
80 1.1.2.2 uebayasi
81 1.1.2.2 uebayasi #define WMI_HP_METHOD_CMD_DISPLAY 0x01
82 1.1.2.2 uebayasi #define WMI_HP_METHOD_CMD_HDDTEMP 0x02
83 1.1.2.2 uebayasi #define WMI_HP_METHOD_CMD_ALS 0x03
84 1.1.2.2 uebayasi #define WMI_HP_METHOD_CMD_DOCK 0x04
85 1.1.2.2 uebayasi #define WMI_HP_METHOD_CMD_SWITCH 0x05
86 1.1.2.2 uebayasi #define WMI_HP_METHOD_CMD_HOTKEY 0x0C
87 1.1.2.2 uebayasi
88 1.1.2.2 uebayasi #define WMI_HP_EVENT_DOCK 0x01
89 1.1.2.2 uebayasi #define WMI_HP_EVENT_HOTKEY 0x04
90 1.1.2.2 uebayasi #define WMI_HP_EVENT_SWITCH 0x05
91 1.1.2.2 uebayasi /* WMI_HP_EVENT_UNKNOWN 0xXX */
92 1.1.2.2 uebayasi
93 1.1.2.2 uebayasi #define WMI_HP_HOTKEY_BRIGHTNESS_UP 0x02
94 1.1.2.2 uebayasi #define WMI_HP_HOTKEY_BRIGHTNESS_DOWN 0x03
95 1.1.2.2 uebayasi /* WMI_HP_HOTKEY_UNKNOWN 0xXX */
96 1.1.2.2 uebayasi
97 1.1.2.2 uebayasi #define WMI_HP_SWITCH_WLAN 0x01
98 1.1.2.2 uebayasi #define WMI_HP_SWITCH_BT 0x02
99 1.1.2.2 uebayasi #define WMI_HP_SWITCH_WWAN 0x04
100 1.1.2.2 uebayasi
101 1.1.2.2 uebayasi #define WMI_HP_SWITCH_ARG_WLAN_OFF 0x100
102 1.1.2.2 uebayasi #define WMI_HP_SWITCH_ARG_WLAN_ON 0x101
103 1.1.2.2 uebayasi #define WMI_HP_SWITCH_ARG_BT_OFF 0x200
104 1.1.2.2 uebayasi #define WMI_HP_SWITCH_ARG_BT_ON 0x202
105 1.1.2.2 uebayasi #define WMI_HP_SWITCH_ARG_WWAN_OFF 0x400
106 1.1.2.2 uebayasi #define WMI_HP_SWITCH_ARG_WWAN_ON 0x404
107 1.1.2.2 uebayasi
108 1.1.2.2 uebayasi #define WMI_HP_SWITCH_MASK_WLAN_ONAIR __BIT(8)
109 1.1.2.2 uebayasi #define WMI_HP_SWITCH_MASK_WLAN_ENABLED __BIT(9)
110 1.1.2.2 uebayasi #define WMI_HP_SWITCH_MASK_WLAN_RADIO __BIT(11)
111 1.1.2.2 uebayasi #define WMI_HP_SWITCH_MASK_BT_ONAIR __BIT(16)
112 1.1.2.2 uebayasi #define WMI_HP_SWITCH_MASK_BT_ENABLED __BIT(17)
113 1.1.2.2 uebayasi #define WMI_HP_SWITCH_MASK_BT_RADIO __BIT(19)
114 1.1.2.2 uebayasi #define WMI_HP_SWITCH_MASK_WWAN_ONAIR __BIT(24)
115 1.1.2.2 uebayasi #define WMI_HP_SWITCH_MASK_WWAN_ENABLED __BIT(25)
116 1.1.2.2 uebayasi #define WMI_HP_SWITCH_MASK_WWAN_RADIO __BIT(27)
117 1.1.2.2 uebayasi
118 1.1.2.2 uebayasi #define WMI_HP_GUID_EVENT "95F24279-4D7B-4334-9387-ACCDC67EF61C"
119 1.1.2.2 uebayasi #define WMI_HP_GUID_METHOD "5FB7F034-2C63-45E9-BE91-3D44E2C707E4"
120 1.1.2.2 uebayasi
121 1.1.2.2 uebayasi #define WMI_HP_SENSOR_WLAN 0
122 1.1.2.2 uebayasi #define WMI_HP_SENSOR_BT 1
123 1.1.2.2 uebayasi #define WMI_HP_SENSOR_WWAN 2
124 1.1.2.2 uebayasi #define WMI_HP_SENSOR_COUNT 3
125 1.1.2.2 uebayasi #define WMI_HP_SENSOR_SIZE 3 * sizeof(envsys_data_t)
126 1.1.2.2 uebayasi
127 1.1.2.2 uebayasi struct wmi_hp_softc {
128 1.1.2.2 uebayasi device_t sc_dev;
129 1.1.2.2 uebayasi device_t sc_parent;
130 1.1.2.2 uebayasi struct sysmon_envsys *sc_sme;
131 1.1.2.2 uebayasi envsys_data_t *sc_sensor;
132 1.1.2.2 uebayasi uint32_t *sc_arg;
133 1.1.2.2 uebayasi uint32_t sc_val;
134 1.1.2.2 uebayasi };
135 1.1.2.2 uebayasi
136 1.1.2.2 uebayasi static int wmi_hp_match(device_t, cfdata_t, void *);
137 1.1.2.2 uebayasi static void wmi_hp_attach(device_t, device_t, void *);
138 1.1.2.2 uebayasi static int wmi_hp_detach(device_t, int);
139 1.1.2.2 uebayasi static bool wmi_hp_suspend(device_t, const pmf_qual_t *);
140 1.1.2.2 uebayasi static bool wmi_hp_resume(device_t, const pmf_qual_t *);
141 1.1.2.2 uebayasi static void wmi_hp_notify_handler(ACPI_HANDLE, uint32_t, void *);
142 1.1.2.2 uebayasi static void wmi_hp_hotkey(void *);
143 1.1.2.2 uebayasi static bool wmi_hp_method(struct wmi_hp_softc *);
144 1.1.2.2 uebayasi static bool wmi_hp_method_read(struct wmi_hp_softc *, uint8_t);
145 1.1.2.2 uebayasi
146 1.1.2.2 uebayasi #if 0
147 1.1.2.2 uebayasi static bool wmi_hp_method_write(struct wmi_hp_softc *, uint8_t, uint32_t);
148 1.1.2.2 uebayasi #endif
149 1.1.2.2 uebayasi
150 1.1.2.2 uebayasi static void wmi_hp_sensor_init(struct wmi_hp_softc *);
151 1.1.2.2 uebayasi static void wmi_hp_sensor_update(void *);
152 1.1.2.2 uebayasi
153 1.1.2.2 uebayasi CFATTACH_DECL_NEW(wmihp, sizeof(struct wmi_hp_softc),
154 1.1.2.2 uebayasi wmi_hp_match, wmi_hp_attach, wmi_hp_detach, NULL);
155 1.1.2.2 uebayasi
156 1.1.2.2 uebayasi static int
157 1.1.2.2 uebayasi wmi_hp_match(device_t parent, cfdata_t match, void *aux)
158 1.1.2.2 uebayasi {
159 1.1.2.2 uebayasi return acpi_wmi_guid_match(parent, WMI_HP_GUID_METHOD);
160 1.1.2.2 uebayasi }
161 1.1.2.2 uebayasi
162 1.1.2.2 uebayasi static void
163 1.1.2.2 uebayasi wmi_hp_attach(device_t parent, device_t self, void *aux)
164 1.1.2.2 uebayasi {
165 1.1.2.2 uebayasi struct wmi_hp_softc *sc = device_private(self);
166 1.1.2.2 uebayasi ACPI_STATUS rv = AE_ERROR;
167 1.1.2.2 uebayasi
168 1.1.2.2 uebayasi sc->sc_dev = self;
169 1.1.2.2 uebayasi sc->sc_parent = parent;
170 1.1.2.2 uebayasi
171 1.1.2.2 uebayasi sc->sc_sme = NULL;
172 1.1.2.2 uebayasi sc->sc_sensor = NULL;
173 1.1.2.2 uebayasi
174 1.1.2.2 uebayasi sc->sc_arg = kmem_alloc(WMI_HP_METHOD_ARG_SIZE, KM_SLEEP);
175 1.1.2.2 uebayasi
176 1.1.2.2 uebayasi if (sc->sc_arg == NULL)
177 1.1.2.2 uebayasi return;
178 1.1.2.2 uebayasi
179 1.1.2.2 uebayasi aprint_naive("\n");
180 1.1.2.2 uebayasi aprint_normal(": HP WMI mappings\n");
181 1.1.2.2 uebayasi
182 1.1.2.2 uebayasi (void)pmf_device_register(sc->sc_dev, wmi_hp_suspend, wmi_hp_resume);
183 1.1.2.2 uebayasi
184 1.1.2.2 uebayasi if (acpi_wmi_guid_match(parent, WMI_HP_GUID_EVENT) != 0)
185 1.1.2.2 uebayasi rv = acpi_wmi_event_register(parent, wmi_hp_notify_handler);
186 1.1.2.2 uebayasi
187 1.1.2.2 uebayasi if (ACPI_FAILURE(rv))
188 1.1.2.2 uebayasi return;
189 1.1.2.2 uebayasi
190 1.1.2.2 uebayasi sc->sc_sensor = kmem_alloc(WMI_HP_SENSOR_SIZE, KM_SLEEP);
191 1.1.2.2 uebayasi
192 1.1.2.2 uebayasi if (sc->sc_sensor == NULL)
193 1.1.2.2 uebayasi return;
194 1.1.2.2 uebayasi
195 1.1.2.2 uebayasi wmi_hp_sensor_init(sc);
196 1.1.2.2 uebayasi }
197 1.1.2.2 uebayasi
198 1.1.2.2 uebayasi static int
199 1.1.2.2 uebayasi wmi_hp_detach(device_t self, int flags)
200 1.1.2.2 uebayasi {
201 1.1.2.2 uebayasi struct wmi_hp_softc *sc = device_private(self);
202 1.1.2.2 uebayasi device_t parent = sc->sc_parent;
203 1.1.2.2 uebayasi
204 1.1.2.2 uebayasi (void)acpi_wmi_event_deregister(parent);
205 1.1.2.2 uebayasi
206 1.1.2.2 uebayasi if (sc->sc_sme != NULL)
207 1.1.2.2 uebayasi sysmon_envsys_unregister(sc->sc_sme);
208 1.1.2.2 uebayasi
209 1.1.2.2 uebayasi if (sc->sc_sensor != NULL)
210 1.1.2.2 uebayasi kmem_free(sc->sc_sensor, WMI_HP_SENSOR_SIZE);
211 1.1.2.2 uebayasi
212 1.1.2.2 uebayasi if (sc->sc_arg != NULL)
213 1.1.2.2 uebayasi kmem_free(sc->sc_arg, WMI_HP_METHOD_ARG_SIZE);
214 1.1.2.2 uebayasi
215 1.1.2.2 uebayasi pmf_device_deregister(self);
216 1.1.2.2 uebayasi
217 1.1.2.2 uebayasi return 0;
218 1.1.2.2 uebayasi }
219 1.1.2.2 uebayasi
220 1.1.2.2 uebayasi static bool
221 1.1.2.2 uebayasi wmi_hp_suspend(device_t self, const pmf_qual_t *qual)
222 1.1.2.2 uebayasi {
223 1.1.2.2 uebayasi struct wmi_hp_softc *sc = device_private(self);
224 1.1.2.2 uebayasi device_t parent = sc->sc_parent;
225 1.1.2.2 uebayasi
226 1.1.2.2 uebayasi if (sc->sc_sensor != NULL)
227 1.1.2.2 uebayasi (void)acpi_wmi_event_deregister(parent);
228 1.1.2.2 uebayasi
229 1.1.2.2 uebayasi return true;
230 1.1.2.2 uebayasi }
231 1.1.2.2 uebayasi
232 1.1.2.2 uebayasi static bool
233 1.1.2.2 uebayasi wmi_hp_resume(device_t self, const pmf_qual_t *qual)
234 1.1.2.2 uebayasi {
235 1.1.2.2 uebayasi struct wmi_hp_softc *sc = device_private(self);
236 1.1.2.2 uebayasi device_t parent = sc->sc_parent;
237 1.1.2.2 uebayasi
238 1.1.2.2 uebayasi if (sc->sc_sensor != NULL)
239 1.1.2.2 uebayasi (void)acpi_wmi_event_register(parent, wmi_hp_notify_handler);
240 1.1.2.2 uebayasi
241 1.1.2.2 uebayasi return true;
242 1.1.2.2 uebayasi }
243 1.1.2.2 uebayasi
244 1.1.2.2 uebayasi static void
245 1.1.2.2 uebayasi wmi_hp_notify_handler(ACPI_HANDLE hdl, uint32_t evt, void *aux)
246 1.1.2.2 uebayasi {
247 1.1.2.2 uebayasi static const int handler = OSL_NOTIFY_HANDLER;
248 1.1.2.2 uebayasi struct wmi_hp_softc *sc;
249 1.1.2.2 uebayasi device_t self = aux;
250 1.1.2.2 uebayasi ACPI_OBJECT *obj;
251 1.1.2.2 uebayasi ACPI_BUFFER buf;
252 1.1.2.2 uebayasi ACPI_STATUS rv;
253 1.1.2.2 uebayasi uint32_t val;
254 1.1.2.2 uebayasi
255 1.1.2.3 uebayasi buf.Pointer = NULL;
256 1.1.2.3 uebayasi
257 1.1.2.2 uebayasi sc = device_private(self);
258 1.1.2.2 uebayasi rv = acpi_wmi_event_get(sc->sc_parent, evt, &buf);
259 1.1.2.2 uebayasi
260 1.1.2.2 uebayasi if (ACPI_FAILURE(rv))
261 1.1.2.2 uebayasi goto out;
262 1.1.2.2 uebayasi
263 1.1.2.2 uebayasi obj = buf.Pointer;
264 1.1.2.2 uebayasi
265 1.1.2.2 uebayasi if (obj->Type != ACPI_TYPE_BUFFER) {
266 1.1.2.2 uebayasi rv = AE_TYPE;
267 1.1.2.2 uebayasi goto out;
268 1.1.2.2 uebayasi }
269 1.1.2.2 uebayasi
270 1.1.2.2 uebayasi if (obj->Buffer.Length != 8) {
271 1.1.2.2 uebayasi rv = AE_LIMIT;
272 1.1.2.2 uebayasi goto out;
273 1.1.2.2 uebayasi }
274 1.1.2.2 uebayasi
275 1.1.2.2 uebayasi val = *((uint8_t *)obj->Buffer.Pointer);
276 1.1.2.2 uebayasi
277 1.1.2.2 uebayasi if (val == 0x00) {
278 1.1.2.2 uebayasi rv = AE_BAD_DATA;
279 1.1.2.2 uebayasi goto out;
280 1.1.2.2 uebayasi }
281 1.1.2.2 uebayasi
282 1.1.2.2 uebayasi switch (val) {
283 1.1.2.2 uebayasi
284 1.1.2.2 uebayasi case WMI_HP_EVENT_SWITCH:
285 1.1.2.2 uebayasi rv = AcpiOsExecute(handler, wmi_hp_sensor_update, self);
286 1.1.2.2 uebayasi break;
287 1.1.2.2 uebayasi
288 1.1.2.2 uebayasi case WMI_HP_EVENT_HOTKEY:
289 1.1.2.2 uebayasi rv = AcpiOsExecute(handler, wmi_hp_hotkey, self);
290 1.1.2.2 uebayasi break;
291 1.1.2.2 uebayasi
292 1.1.2.2 uebayasi case WMI_HP_EVENT_DOCK: /* FALLTHROUGH */
293 1.1.2.2 uebayasi
294 1.1.2.2 uebayasi default:
295 1.1.2.2 uebayasi aprint_debug_dev(sc->sc_dev, "unknown event 0x%02X\n", evt);
296 1.1.2.2 uebayasi break;
297 1.1.2.2 uebayasi }
298 1.1.2.2 uebayasi
299 1.1.2.2 uebayasi out:
300 1.1.2.2 uebayasi if (buf.Pointer != NULL)
301 1.1.2.2 uebayasi ACPI_FREE(buf.Pointer);
302 1.1.2.2 uebayasi
303 1.1.2.2 uebayasi if (ACPI_FAILURE(rv))
304 1.1.2.2 uebayasi aprint_error_dev(sc->sc_dev, "failed to get data for "
305 1.1.2.2 uebayasi "event 0x%02X: %s\n", evt, AcpiFormatException(rv));
306 1.1.2.2 uebayasi }
307 1.1.2.2 uebayasi
308 1.1.2.2 uebayasi static void
309 1.1.2.2 uebayasi wmi_hp_hotkey(void *aux)
310 1.1.2.2 uebayasi {
311 1.1.2.2 uebayasi struct wmi_hp_softc *sc;
312 1.1.2.2 uebayasi device_t self = aux;
313 1.1.2.2 uebayasi
314 1.1.2.2 uebayasi sc = device_private(self);
315 1.1.2.2 uebayasi
316 1.1.2.2 uebayasi if (wmi_hp_method_read(sc, WMI_HP_METHOD_CMD_HOTKEY) != true)
317 1.1.2.2 uebayasi return;
318 1.1.2.2 uebayasi
319 1.1.2.2 uebayasi switch (sc->sc_val) {
320 1.1.2.2 uebayasi
321 1.1.2.2 uebayasi case WMI_HP_HOTKEY_BRIGHTNESS_UP:
322 1.1.2.2 uebayasi pmf_event_inject(NULL, PMFE_DISPLAY_BRIGHTNESS_UP);
323 1.1.2.2 uebayasi break;
324 1.1.2.2 uebayasi
325 1.1.2.2 uebayasi case WMI_HP_HOTKEY_BRIGHTNESS_DOWN:
326 1.1.2.2 uebayasi pmf_event_inject(NULL, PMFE_DISPLAY_BRIGHTNESS_DOWN);
327 1.1.2.2 uebayasi break;
328 1.1.2.2 uebayasi
329 1.1.2.2 uebayasi default:
330 1.1.2.2 uebayasi aprint_debug_dev(self, "unknown hotkey 0x%02x\n", sc->sc_val);
331 1.1.2.2 uebayasi break;
332 1.1.2.2 uebayasi }
333 1.1.2.2 uebayasi }
334 1.1.2.2 uebayasi
335 1.1.2.2 uebayasi static bool
336 1.1.2.2 uebayasi wmi_hp_method(struct wmi_hp_softc *sc)
337 1.1.2.2 uebayasi {
338 1.1.2.2 uebayasi ACPI_BUFFER ibuf, obuf;
339 1.1.2.2 uebayasi ACPI_STATUS rv = AE_OK;
340 1.1.2.2 uebayasi ACPI_OBJECT *obj;
341 1.1.2.2 uebayasi uint32_t cmd, *val;
342 1.1.2.2 uebayasi
343 1.1.2.2 uebayasi cmd = sc->sc_arg[2];
344 1.1.2.2 uebayasi
345 1.1.2.2 uebayasi KDASSERT(cmd != 0);
346 1.1.2.2 uebayasi KDASSERT(sc->sc_arg[0] == WMI_HP_METHOD_ARG_MAGIC);
347 1.1.2.2 uebayasi
348 1.1.2.3 uebayasi obuf.Pointer = NULL;
349 1.1.2.2 uebayasi ibuf.Pointer = sc->sc_arg;
350 1.1.2.2 uebayasi ibuf.Length = WMI_HP_METHOD_ARG_SIZE;
351 1.1.2.2 uebayasi
352 1.1.2.2 uebayasi rv = acpi_wmi_method(sc->sc_parent,
353 1.1.2.2 uebayasi WMI_HP_GUID_METHOD, 0, 3, &ibuf, &obuf);
354 1.1.2.2 uebayasi
355 1.1.2.2 uebayasi if (ACPI_FAILURE(rv))
356 1.1.2.2 uebayasi goto out;
357 1.1.2.2 uebayasi
358 1.1.2.2 uebayasi obj = obuf.Pointer;
359 1.1.2.2 uebayasi
360 1.1.2.2 uebayasi if (obj->Type != ACPI_TYPE_BUFFER) {
361 1.1.2.2 uebayasi rv = AE_TYPE;
362 1.1.2.2 uebayasi goto out;
363 1.1.2.2 uebayasi }
364 1.1.2.2 uebayasi
365 1.1.2.2 uebayasi /*
366 1.1.2.2 uebayasi * val[0] unknown
367 1.1.2.2 uebayasi * val[1] error code
368 1.1.2.2 uebayasi * val[2] return value
369 1.1.2.2 uebayasi */
370 1.1.2.2 uebayasi val = (uint32_t *)obj->Buffer.Pointer;
371 1.1.2.2 uebayasi
372 1.1.2.2 uebayasi if (val[1] != 0) {
373 1.1.2.2 uebayasi rv = AE_ERROR;
374 1.1.2.2 uebayasi goto out;
375 1.1.2.2 uebayasi }
376 1.1.2.2 uebayasi
377 1.1.2.2 uebayasi sc->sc_val = val[2];
378 1.1.2.2 uebayasi
379 1.1.2.2 uebayasi out:
380 1.1.2.2 uebayasi if (obuf.Pointer != NULL)
381 1.1.2.2 uebayasi ACPI_FREE(obuf.Pointer);
382 1.1.2.2 uebayasi
383 1.1.2.2 uebayasi if (ACPI_FAILURE(rv)) {
384 1.1.2.2 uebayasi aprint_debug_dev(sc->sc_dev, "failed to evaluate method "
385 1.1.2.2 uebayasi "(cmd = 0x%02X): %s\n", cmd, AcpiFormatException(rv));
386 1.1.2.2 uebayasi return false;
387 1.1.2.2 uebayasi }
388 1.1.2.2 uebayasi
389 1.1.2.2 uebayasi return true;
390 1.1.2.2 uebayasi }
391 1.1.2.2 uebayasi
392 1.1.2.2 uebayasi static bool
393 1.1.2.2 uebayasi wmi_hp_method_read(struct wmi_hp_softc *sc, uint8_t cmd)
394 1.1.2.2 uebayasi {
395 1.1.2.2 uebayasi
396 1.1.2.2 uebayasi sc->sc_arg[0] = WMI_HP_METHOD_ARG_MAGIC;
397 1.1.2.2 uebayasi sc->sc_arg[1] = WMI_HP_METHOD_ARG_READ;
398 1.1.2.2 uebayasi sc->sc_arg[2] = cmd;
399 1.1.2.2 uebayasi sc->sc_arg[3] = 0;
400 1.1.2.2 uebayasi sc->sc_arg[4] = 0;
401 1.1.2.2 uebayasi
402 1.1.2.2 uebayasi return wmi_hp_method(sc);
403 1.1.2.2 uebayasi }
404 1.1.2.2 uebayasi
405 1.1.2.2 uebayasi #if 0
406 1.1.2.2 uebayasi static bool
407 1.1.2.2 uebayasi wmi_hp_method_write(struct wmi_hp_softc *sc, uint8_t cmd, uint32_t val)
408 1.1.2.2 uebayasi {
409 1.1.2.2 uebayasi
410 1.1.2.2 uebayasi sc->sc_arg[0] = WMI_HP_METHOD_ARG_MAGIC;
411 1.1.2.2 uebayasi sc->sc_arg[1] = WMI_HP_METHOD_ARG_WRITE;
412 1.1.2.2 uebayasi sc->sc_arg[2] = cmd;
413 1.1.2.2 uebayasi sc->sc_arg[3] = WMI_HP_METHOD_ARG_WRITE_SIZE;
414 1.1.2.2 uebayasi sc->sc_arg[4] = val;
415 1.1.2.2 uebayasi
416 1.1.2.2 uebayasi return wmi_hp_method(sc);
417 1.1.2.2 uebayasi }
418 1.1.2.2 uebayasi #endif
419 1.1.2.2 uebayasi
420 1.1.2.2 uebayasi static void
421 1.1.2.2 uebayasi wmi_hp_sensor_init(struct wmi_hp_softc *sc)
422 1.1.2.2 uebayasi {
423 1.1.2.2 uebayasi int i, j, sensor[3];
424 1.1.2.2 uebayasi
425 1.1.2.2 uebayasi const char desc[][ENVSYS_DESCLEN] = {
426 1.1.2.2 uebayasi "wireless", "bluetooth", "mobile"
427 1.1.2.2 uebayasi };
428 1.1.2.2 uebayasi
429 1.1.2.2 uebayasi KDASSERT(sc->sc_sme == NULL);
430 1.1.2.2 uebayasi KDASSERT(sc->sc_sensor != NULL);
431 1.1.2.2 uebayasi
432 1.1.2.2 uebayasi (void)memset(sc->sc_sensor, 0, WMI_HP_SENSOR_SIZE);
433 1.1.2.2 uebayasi
434 1.1.2.2 uebayasi if (wmi_hp_method_read(sc, WMI_HP_METHOD_CMD_SWITCH) != true)
435 1.1.2.2 uebayasi return;
436 1.1.2.2 uebayasi
437 1.1.2.2 uebayasi sc->sc_sme = sysmon_envsys_create();
438 1.1.2.2 uebayasi
439 1.1.2.2 uebayasi sensor[0] = WMI_HP_SWITCH_WLAN;
440 1.1.2.2 uebayasi sensor[1] = WMI_HP_SWITCH_BT;
441 1.1.2.2 uebayasi sensor[2] = WMI_HP_SWITCH_WWAN;
442 1.1.2.2 uebayasi
443 1.1.2.2 uebayasi CTASSERT(WMI_HP_SENSOR_WLAN == 0);
444 1.1.2.2 uebayasi CTASSERT(WMI_HP_SENSOR_BT == 1);
445 1.1.2.2 uebayasi CTASSERT(WMI_HP_SENSOR_WWAN == 2);
446 1.1.2.2 uebayasi
447 1.1.2.2 uebayasi for (i = j = 0; i < 3; i++) {
448 1.1.2.2 uebayasi
449 1.1.2.2 uebayasi if ((sc->sc_val & sensor[i]) == 0)
450 1.1.2.2 uebayasi continue;
451 1.1.2.2 uebayasi
452 1.1.2.2 uebayasi (void)strlcpy(sc->sc_sensor[i].desc, desc[i], ENVSYS_DESCLEN);
453 1.1.2.2 uebayasi
454 1.1.2.2 uebayasi sc->sc_sensor[i].state = ENVSYS_SINVALID;
455 1.1.2.2 uebayasi sc->sc_sensor[i].units = ENVSYS_INDICATOR;
456 1.1.2.2 uebayasi
457 1.1.2.2 uebayasi if (sysmon_envsys_sensor_attach(sc->sc_sme,
458 1.1.2.2 uebayasi &sc->sc_sensor[i]) != 0)
459 1.1.2.2 uebayasi goto fail;
460 1.1.2.2 uebayasi
461 1.1.2.2 uebayasi j++;
462 1.1.2.2 uebayasi }
463 1.1.2.2 uebayasi
464 1.1.2.2 uebayasi if (j == 0)
465 1.1.2.2 uebayasi goto fail;
466 1.1.2.2 uebayasi
467 1.1.2.2 uebayasi sc->sc_sme->sme_flags = SME_DISABLE_REFRESH;
468 1.1.2.2 uebayasi sc->sc_sme->sme_name = device_xname(sc->sc_dev);
469 1.1.2.2 uebayasi
470 1.1.2.2 uebayasi if (sysmon_envsys_register(sc->sc_sme) != 0)
471 1.1.2.2 uebayasi goto fail;
472 1.1.2.2 uebayasi
473 1.1.2.2 uebayasi wmi_hp_sensor_update(sc->sc_dev);
474 1.1.2.2 uebayasi
475 1.1.2.2 uebayasi return;
476 1.1.2.2 uebayasi
477 1.1.2.2 uebayasi fail:
478 1.1.2.2 uebayasi aprint_debug_dev(sc->sc_dev, "failed to initialize sysmon\n");
479 1.1.2.2 uebayasi
480 1.1.2.2 uebayasi sysmon_envsys_destroy(sc->sc_sme);
481 1.1.2.2 uebayasi kmem_free(sc->sc_sensor, WMI_HP_SENSOR_SIZE);
482 1.1.2.2 uebayasi
483 1.1.2.2 uebayasi sc->sc_sme = NULL;
484 1.1.2.2 uebayasi sc->sc_sensor = NULL;
485 1.1.2.2 uebayasi }
486 1.1.2.2 uebayasi
487 1.1.2.2 uebayasi static void
488 1.1.2.2 uebayasi wmi_hp_sensor_update(void *aux)
489 1.1.2.2 uebayasi {
490 1.1.2.2 uebayasi struct wmi_hp_softc *sc;
491 1.1.2.2 uebayasi device_t self = aux;
492 1.1.2.2 uebayasi
493 1.1.2.2 uebayasi sc = device_private(self);
494 1.1.2.2 uebayasi
495 1.1.2.2 uebayasi if (sc->sc_sme == NULL || sc->sc_sensor == NULL)
496 1.1.2.2 uebayasi return;
497 1.1.2.2 uebayasi
498 1.1.2.2 uebayasi if (wmi_hp_method_read(sc, WMI_HP_METHOD_CMD_SWITCH) != true) {
499 1.1.2.2 uebayasi sc->sc_sensor[WMI_HP_SENSOR_WLAN].state = ENVSYS_SINVALID;
500 1.1.2.2 uebayasi sc->sc_sensor[WMI_HP_SENSOR_WWAN].state = ENVSYS_SINVALID;
501 1.1.2.2 uebayasi sc->sc_sensor[WMI_HP_SENSOR_BT].state = ENVSYS_SINVALID;
502 1.1.2.2 uebayasi return;
503 1.1.2.2 uebayasi }
504 1.1.2.2 uebayasi
505 1.1.2.2 uebayasi if ((sc->sc_val & WMI_HP_SWITCH_WLAN) != 0) {
506 1.1.2.2 uebayasi sc->sc_sensor[WMI_HP_SENSOR_WLAN].value_cur = 0;
507 1.1.2.2 uebayasi
508 1.1.2.2 uebayasi if ((sc->sc_val & WMI_HP_SWITCH_MASK_WLAN_ONAIR) != 0)
509 1.1.2.2 uebayasi sc->sc_sensor[WMI_HP_SENSOR_WLAN].value_cur = 1;
510 1.1.2.2 uebayasi
511 1.1.2.2 uebayasi sc->sc_sensor[WMI_HP_SENSOR_WLAN].state = ENVSYS_SVALID;
512 1.1.2.2 uebayasi }
513 1.1.2.2 uebayasi
514 1.1.2.2 uebayasi if ((sc->sc_val & WMI_HP_SWITCH_BT) != 0) {
515 1.1.2.2 uebayasi sc->sc_sensor[WMI_HP_SENSOR_BT].value_cur = 0;
516 1.1.2.2 uebayasi
517 1.1.2.2 uebayasi if ((sc->sc_val & WMI_HP_SWITCH_MASK_BT_ONAIR) != 0)
518 1.1.2.2 uebayasi sc->sc_sensor[WMI_HP_SENSOR_BT].value_cur = 1;
519 1.1.2.2 uebayasi
520 1.1.2.2 uebayasi sc->sc_sensor[WMI_HP_SENSOR_BT].state = ENVSYS_SVALID;
521 1.1.2.2 uebayasi }
522 1.1.2.2 uebayasi
523 1.1.2.2 uebayasi if ((sc->sc_val & WMI_HP_SWITCH_WWAN) != 0) {
524 1.1.2.2 uebayasi sc->sc_sensor[WMI_HP_SENSOR_WWAN].value_cur = 0;
525 1.1.2.2 uebayasi
526 1.1.2.2 uebayasi if ((sc->sc_val & WMI_HP_SWITCH_MASK_WWAN_ONAIR) != 0)
527 1.1.2.2 uebayasi sc->sc_sensor[WMI_HP_SENSOR_WWAN].value_cur = 1;
528 1.1.2.2 uebayasi
529 1.1.2.2 uebayasi sc->sc_sensor[WMI_HP_SENSOR_WWAN].state = ENVSYS_SVALID;
530 1.1.2.2 uebayasi }
531 1.1.2.2 uebayasi }
532