asus_acpi.c revision 1.10 1 /* $NetBSD: asus_acpi.c,v 1.10 2009/08/04 23:23:39 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2007, 2008, 2009 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: asus_acpi.c,v 1.10 2009/08/04 23:23:39 jmcneill Exp $");
31
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/malloc.h>
35 #include <sys/buf.h>
36 #include <sys/callout.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/sysctl.h>
40
41 #include <dev/acpi/acpivar.h>
42
43 struct asus_softc {
44 device_t sc_dev;
45 struct acpi_devnode *sc_node;
46
47 #define ASUS_PSW_DISPLAY_CYCLE 0
48 #define ASUS_PSW_LAST 1
49 struct sysmon_pswitch sc_smpsw[ASUS_PSW_LAST];
50 bool sc_smpsw_valid;
51
52 struct sysmon_envsys *sc_sme;
53 #define ASUS_SENSOR_FAN 0
54 #define ASUS_SENSOR_LAST 1
55 envsys_data_t sc_sensor[ASUS_SENSOR_LAST];
56
57 ACPI_INTEGER sc_brightness;
58 ACPI_INTEGER sc_cfvnum;
59
60 struct sysctllog *sc_log;
61 int sc_cfv_mib;
62 int sc_cfvnum_mib;
63 };
64
65 #define ASUS_NOTIFY_WirelessSwitch 0x10
66 #define ASUS_NOTIFY_BrightnessLow 0x20
67 #define ASUS_NOTIFY_BrightnessHigh 0x2f
68 #define ASUS_NOTIFY_DisplayCycle 0x30
69 #define ASUS_NOTIFY_WindowSwitch 0x12 /* XXXJDM ?? */
70 #define ASUS_NOTIFY_VolumeMute 0x13
71 #define ASUS_NOTIFY_VolumeDown 0x14
72 #define ASUS_NOTIFY_VolumeUp 0x15
73
74 #define ASUS_METHOD_SDSP "SDSP"
75 #define ASUS_SDSP_LCD 0x01
76 #define ASUS_SDSP_CRT 0x02
77 #define ASUS_SDSP_TV 0x04
78 #define ASUS_SDSP_DVI 0x08
79 #define ASUS_SDSP_ALL \
80 (ASUS_SDSP_LCD | ASUS_SDSP_CRT | ASUS_SDSP_TV | ASUS_SDSP_DVI)
81 #define ASUS_METHOD_PBLG "PBLG"
82 #define ASUS_METHOD_PBLS "PBLS"
83 #define ASUS_METHOD_CFVS "CFVS"
84 #define ASUS_METHOD_CFVG "CFVG"
85
86 #define ASUS_EC_METHOD_FAN_RPMH "\\_SB.PCI0.SBRG.EC0.SC05"
87 #define ASUS_EC_METHOD_FAN_RPML "\\_SB.PCI0.SBRG.EC0.SC06"
88
89 static int asus_match(device_t, cfdata_t, void *);
90 static void asus_attach(device_t, device_t, void *);
91 static int asus_detach(device_t, int);
92
93 static void asus_notify_handler(ACPI_HANDLE, UINT32, void *);
94
95 static void asus_init(device_t);
96 static bool asus_suspend(device_t PMF_FN_PROTO);
97 static bool asus_resume(device_t PMF_FN_PROTO);
98
99 static void asus_sysctl_setup(struct asus_softc *);
100
101 static void asus_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
102 static bool asus_get_fan_speed(struct asus_softc *, uint32_t *);
103
104 CFATTACH_DECL_NEW(asus, sizeof(struct asus_softc),
105 asus_match, asus_attach, asus_detach, NULL);
106
107 static const char * const asus_ids[] = {
108 "ASUS010",
109 NULL
110 };
111
112 static int
113 asus_match(device_t parent, cfdata_t match, void *opaque)
114 {
115 struct acpi_attach_args *aa = opaque;
116
117 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
118 return 0;
119
120 return acpi_match_hid(aa->aa_node->ad_devinfo, asus_ids);
121 }
122
123 static void
124 asus_attach(device_t parent, device_t self, void *opaque)
125 {
126 struct asus_softc *sc = device_private(self);
127 struct acpi_attach_args *aa = opaque;
128 ACPI_STATUS rv;
129
130 sc->sc_node = aa->aa_node;
131 sc->sc_dev = self;
132
133 aprint_naive("\n");
134 aprint_normal("\n");
135
136 asus_init(self);
137 asus_sysctl_setup(sc);
138
139 sc->sc_smpsw_valid = true;
140 sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE].smpsw_name =
141 PSWITCH_HK_DISPLAY_CYCLE;
142 sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE].smpsw_type =
143 PSWITCH_TYPE_HOTKEY;
144 if (sysmon_pswitch_register(&sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE])) {
145 aprint_error_dev(self, "couldn't register with sysmon\n");
146 sc->sc_smpsw_valid = false;
147 }
148
149 if (asus_get_fan_speed(sc, NULL) == false)
150 goto nosensors;
151
152 sc->sc_sme = sysmon_envsys_create();
153
154 strcpy(sc->sc_sensor[ASUS_SENSOR_FAN].desc, "fan");
155 sc->sc_sensor[ASUS_SENSOR_FAN].units = ENVSYS_SFANRPM;
156 sysmon_envsys_sensor_attach(sc->sc_sme,
157 &sc->sc_sensor[ASUS_SENSOR_FAN]);
158
159 sc->sc_sme->sme_name = device_xname(self);
160 sc->sc_sme->sme_cookie = sc;
161 sc->sc_sme->sme_refresh = asus_sensors_refresh;
162 sc->sc_sme->sme_flags = SME_POLL_ONLY;
163
164 if (sysmon_envsys_register(sc->sc_sme)) {
165 aprint_error_dev(self, "couldn't register with envsys\n");
166 sysmon_envsys_destroy(sc->sc_sme);
167 sc->sc_sme = NULL;
168 }
169 nosensors:
170
171 rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle, ACPI_ALL_NOTIFY,
172 asus_notify_handler, sc);
173 if (ACPI_FAILURE(rv))
174 aprint_error_dev(self, "couldn't install notify handler: %s\n",
175 AcpiFormatException(rv));
176
177 if (!pmf_device_register(self, asus_suspend, asus_resume))
178 aprint_error_dev(self, "couldn't establish power handler\n");
179 }
180
181 static int
182 asus_detach(device_t self, int flags)
183 {
184 struct asus_softc *sc = device_private(self);
185 int i;
186
187 if (sc->sc_smpsw_valid)
188 for (i = 0; i < ASUS_PSW_LAST; i++)
189 sysmon_pswitch_unregister(&sc->sc_smpsw[i]);
190
191 if (sc->sc_sme)
192 sysmon_envsys_unregister(sc->sc_sme);
193 if (sc->sc_log)
194 sysctl_teardown(&sc->sc_log);
195 pmf_device_deregister(self);
196
197 return 0;
198 }
199
200 static void
201 asus_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
202 {
203 struct asus_softc *sc = opaque;
204
205 if (notify >= ASUS_NOTIFY_BrightnessLow &&
206 notify <= ASUS_NOTIFY_BrightnessHigh) {
207 aprint_debug_dev(sc->sc_dev, "brightness %d percent\n",
208 (notify & 0xf) * 100 / 0xf);
209 return;
210 }
211
212 switch (notify) {
213 case ASUS_NOTIFY_WirelessSwitch: /* handled by AML */
214 case ASUS_NOTIFY_WindowSwitch: /* XXXJDM what is this? */
215 break;
216 case ASUS_NOTIFY_DisplayCycle:
217 if (sc->sc_smpsw_valid == false)
218 break;
219 sysmon_pswitch_event(&sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE],
220 PSWITCH_EVENT_PRESSED);
221 break;
222 case ASUS_NOTIFY_VolumeMute:
223 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_TOGGLE);
224 break;
225 case ASUS_NOTIFY_VolumeDown:
226 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
227 break;
228 case ASUS_NOTIFY_VolumeUp:
229 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
230 break;
231 default:
232 aprint_debug_dev(sc->sc_dev, "unknown event 0x%02x\n", notify);
233 break;
234 }
235 }
236
237 static void
238 asus_init(device_t self)
239 {
240 struct asus_softc *sc = device_private(self);
241 ACPI_STATUS rv;
242 ACPI_OBJECT param;
243 ACPI_OBJECT_LIST params;
244 ACPI_BUFFER ret;
245 ACPI_INTEGER cfv;
246
247 ret.Pointer = NULL;
248 ret.Length = ACPI_ALLOCATE_BUFFER;
249 param.Type = ACPI_TYPE_INTEGER;
250 param.Integer.Value = 0x40; /* disable ASL display switching */
251 params.Pointer = ¶m;
252 params.Count = 1;
253
254 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "INIT",
255 ¶ms, &ret);
256 if (ACPI_FAILURE(rv))
257 aprint_error_dev(self, "couldn't evaluate INIT: %s\n",
258 AcpiFormatException(rv));
259
260 if (ret.Pointer)
261 AcpiOsFree(ret.Pointer);
262
263 rv = acpi_eval_integer(sc->sc_node->ad_handle, ASUS_METHOD_CFVG, &cfv);
264 if (ACPI_FAILURE(rv))
265 return;
266
267 sc->sc_cfvnum = (cfv >> 8) & 0xff;
268 }
269
270 static bool
271 asus_suspend(device_t self PMF_FN_ARGS)
272 {
273 struct asus_softc *sc = device_private(self);
274 ACPI_STATUS rv;
275
276 /* capture display brightness when we're sleeping */
277 rv = acpi_eval_integer(sc->sc_node->ad_handle, ASUS_METHOD_PBLG,
278 &sc->sc_brightness);
279 if (ACPI_FAILURE(rv))
280 aprint_error_dev(sc->sc_dev, "couldn't evaluate PBLG: %s\n",
281 AcpiFormatException(rv));
282
283 return true;
284 }
285
286 static bool
287 asus_resume(device_t self PMF_FN_ARGS)
288 {
289 struct asus_softc *sc = device_private(self);
290 ACPI_STATUS rv;
291 ACPI_OBJECT param;
292 ACPI_OBJECT_LIST params;
293 ACPI_BUFFER ret;
294
295 asus_init(self);
296
297 /* restore previous display brightness */
298 ret.Pointer = NULL;
299 ret.Length = ACPI_ALLOCATE_BUFFER;
300 param.Type = ACPI_TYPE_INTEGER;
301 param.Integer.Value = sc->sc_brightness;
302 params.Pointer = ¶m;
303 params.Count = 1;
304
305 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, ASUS_METHOD_PBLS,
306 ¶ms, &ret);
307 if (ACPI_FAILURE(rv))
308 aprint_error_dev(self, "couldn't evaluate PBLS: %s\n",
309 AcpiFormatException(rv));
310
311 return true;
312 }
313
314 static int
315 asus_sysctl_verify(SYSCTLFN_ARGS)
316 {
317 struct sysctlnode node;
318 struct asus_softc *sc;
319 ACPI_STATUS rv;
320 ACPI_INTEGER cfv;
321 ACPI_OBJECT param, retval;
322 ACPI_OBJECT_LIST params;
323 ACPI_BUFFER ret;
324 int err, tmp;
325
326 node = *rnode;
327 sc = rnode->sysctl_data;
328 if (node.sysctl_num == sc->sc_cfv_mib) {
329 rv = acpi_eval_integer(sc->sc_node->ad_handle,
330 ASUS_METHOD_CFVG, &cfv);
331 if (ACPI_FAILURE(rv))
332 return ENXIO;
333 tmp = cfv & 0xff;
334 node.sysctl_data = &tmp;
335 err = sysctl_lookup(SYSCTLFN_CALL(&node));
336 if (err || newp == NULL)
337 return err;
338
339 if (tmp < 0 || tmp >= sc->sc_cfvnum)
340 return EINVAL;
341
342 ret.Pointer = &retval;
343 ret.Length = sizeof(retval);
344 param.Type = ACPI_TYPE_INTEGER;
345 param.Integer.Value = tmp;
346 params.Pointer = ¶m;
347 params.Count = 1;
348
349 rv = AcpiEvaluateObject(sc->sc_node->ad_handle,
350 ASUS_METHOD_CFVS, ¶ms, &ret);
351 if (ACPI_FAILURE(rv))
352 return ENXIO;
353 }
354
355 return 0;
356 }
357
358 static void
359 asus_sysctl_setup(struct asus_softc *sc)
360 {
361 const struct sysctlnode *node, *node_cfv, *node_ncfv;
362 int err, node_mib;
363
364 if (sc->sc_cfvnum == 0)
365 return;
366
367 err = sysctl_createv(&sc->sc_log, 0, NULL, NULL, 0,
368 CTLTYPE_NODE, "hw", NULL, NULL, 0, NULL, 0, CTL_HW, CTL_EOL);
369 if (err)
370 goto sysctl_err;
371 err = sysctl_createv(&sc->sc_log, 0, NULL, &node, 0,
372 CTLTYPE_NODE, device_xname(sc->sc_dev), NULL, NULL, 0,
373 NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
374 if (err)
375 goto sysctl_err;
376 node_mib = node->sysctl_num;
377 err = sysctl_createv(&sc->sc_log, 0, NULL, &node_ncfv,
378 CTLFLAG_READONLY, CTLTYPE_INT, "ncfv",
379 SYSCTL_DESCR("Number of CPU frequency/voltage modes"),
380 NULL, 0, &sc->sc_cfvnum, 0,
381 CTL_HW, node_mib, CTL_CREATE, CTL_EOL);
382 if (err)
383 goto sysctl_err;
384 sc->sc_cfvnum_mib = node_ncfv->sysctl_num;
385 err = sysctl_createv(&sc->sc_log, 0, NULL, &node_cfv,
386 CTLFLAG_READWRITE, CTLTYPE_INT, "cfv",
387 SYSCTL_DESCR("Current CPU frequency/voltage mode"),
388 asus_sysctl_verify, 0, sc, 0,
389 CTL_HW, node_mib, CTL_CREATE, CTL_EOL);
390 if (err)
391 goto sysctl_err;
392 sc->sc_cfv_mib = node_cfv->sysctl_num;
393
394 return;
395 sysctl_err:
396 aprint_error_dev(sc->sc_dev, "failed to add sysctl nodes. (%d)\n", err);
397 }
398
399 static void
400 asus_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
401 {
402 struct asus_softc *sc = sme->sme_cookie;
403 uint32_t rpm;
404
405 switch (edata->sensor) {
406 case ASUS_SENSOR_FAN:
407 if (asus_get_fan_speed(sc, &rpm)) {
408 edata->value_cur = rpm;
409 edata->state = ENVSYS_SVALID;
410 } else
411 edata->state = ENVSYS_SINVALID;
412 break;
413 }
414 }
415
416 static bool
417 asus_get_fan_speed(struct asus_softc *sc, uint32_t *speed)
418 {
419 ACPI_INTEGER rpmh, rpml;
420 ACPI_STATUS rv;
421
422 rv = acpi_eval_integer(sc->sc_node->ad_handle,
423 ASUS_EC_METHOD_FAN_RPMH, &rpmh);
424 if (ACPI_FAILURE(rv))
425 return false;
426 rv = acpi_eval_integer(sc->sc_node->ad_handle,
427 ASUS_EC_METHOD_FAN_RPML, &rpml);
428 if (ACPI_FAILURE(rv))
429 return false;
430
431 if (speed)
432 *speed = (rpmh << 8) | rpml;
433 return true;
434 }
435