asus_acpi.c revision 1.22 1 /* $NetBSD: asus_acpi.c,v 1.22 2011/02/16 09:05:12 jruoho 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.22 2011/02/16 09:05:12 jruoho Exp $");
31
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/module.h>
35 #include <sys/sysctl.h>
36 #include <sys/systm.h>
37
38 #include <dev/acpi/acpireg.h>
39 #include <dev/acpi/acpivar.h>
40
41 #define _COMPONENT ACPI_RESOURCE_COMPONENT
42 ACPI_MODULE_NAME ("asus_acpi")
43
44 struct asus_softc {
45 device_t sc_dev;
46 struct acpi_devnode *sc_node;
47
48 #define ASUS_PSW_DISPLAY_CYCLE 0
49 #define ASUS_PSW_LAST 1
50 struct sysmon_pswitch sc_smpsw[ASUS_PSW_LAST];
51 bool sc_smpsw_valid;
52
53 struct sysmon_envsys *sc_sme;
54 #define ASUS_SENSOR_FAN 0
55 #define ASUS_SENSOR_LAST 1
56 envsys_data_t sc_sensor[ASUS_SENSOR_LAST];
57
58 int32_t sc_brightness;
59 ACPI_INTEGER sc_cfvnum;
60
61 struct sysctllog *sc_log;
62 int sc_cfv_mib;
63 int sc_cfvnum_mib;
64 };
65
66 #define ASUS_NOTIFY_WirelessSwitch 0x10
67 #define ASUS_NOTIFY_BrightnessLow 0x20
68 #define ASUS_NOTIFY_BrightnessHigh 0x2f
69 #define ASUS_NOTIFY_DisplayCycle 0x30
70 #define ASUS_NOTIFY_WindowSwitch 0x12 /* XXXJDM ?? */
71 #define ASUS_NOTIFY_VolumeMute 0x13
72 #define ASUS_NOTIFY_VolumeDown 0x14
73 #define ASUS_NOTIFY_VolumeUp 0x15
74
75 #define ASUS_METHOD_SDSP "SDSP"
76 #define ASUS_SDSP_LCD 0x01
77 #define ASUS_SDSP_CRT 0x02
78 #define ASUS_SDSP_TV 0x04
79 #define ASUS_SDSP_DVI 0x08
80 #define ASUS_SDSP_ALL \
81 (ASUS_SDSP_LCD | ASUS_SDSP_CRT | ASUS_SDSP_TV | ASUS_SDSP_DVI)
82 #define ASUS_METHOD_PBLG "PBLG"
83 #define ASUS_METHOD_PBLS "PBLS"
84 #define ASUS_METHOD_CFVS "CFVS"
85 #define ASUS_METHOD_CFVG "CFVG"
86
87 #define ASUS_EC_METHOD_FAN_RPMH "\\_SB.PCI0.SBRG.EC0.SC05"
88 #define ASUS_EC_METHOD_FAN_RPML "\\_SB.PCI0.SBRG.EC0.SC06"
89
90 static int asus_match(device_t, cfdata_t, void *);
91 static void asus_attach(device_t, device_t, void *);
92 static int asus_detach(device_t, int);
93
94 static void asus_notify_handler(ACPI_HANDLE, uint32_t, void *);
95
96 static void asus_init(device_t);
97 static bool asus_suspend(device_t, const pmf_qual_t *);
98 static bool asus_resume(device_t, const pmf_qual_t *);
99
100 static void asus_sysctl_setup(struct asus_softc *);
101
102 static void asus_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
103 static bool asus_get_fan_speed(struct asus_softc *, uint32_t *);
104
105 CFATTACH_DECL_NEW(asus, sizeof(struct asus_softc),
106 asus_match, asus_attach, asus_detach, NULL);
107
108 static const char * const asus_ids[] = {
109 "ASUS010",
110 NULL
111 };
112
113 static int
114 asus_match(device_t parent, cfdata_t match, void *opaque)
115 {
116 struct acpi_attach_args *aa = opaque;
117
118 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
119 return 0;
120
121 return acpi_match_hid(aa->aa_node->ad_devinfo, asus_ids);
122 }
123
124 static void
125 asus_attach(device_t parent, device_t self, void *opaque)
126 {
127 struct asus_softc *sc = device_private(self);
128 struct acpi_attach_args *aa = opaque;
129
130 sc->sc_dev = self;
131 sc->sc_node = aa->aa_node;
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 out;
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
170 out:
171 (void)pmf_device_register(self, asus_suspend, asus_resume);
172 (void)acpi_register_notify(sc->sc_node, asus_notify_handler);
173 }
174
175 static int
176 asus_detach(device_t self, int flags)
177 {
178 struct asus_softc *sc = device_private(self);
179 int i;
180
181 acpi_deregister_notify(sc->sc_node);
182
183 if (sc->sc_smpsw_valid != false) {
184
185 for (i = 0; i < ASUS_PSW_LAST; i++)
186 sysmon_pswitch_unregister(&sc->sc_smpsw[i]);
187 }
188
189 if (sc->sc_sme != NULL)
190 sysmon_envsys_unregister(sc->sc_sme);
191
192 if (sc->sc_log != NULL)
193 sysctl_teardown(&sc->sc_log);
194
195 pmf_device_deregister(self);
196
197 return 0;
198 }
199
200 static void
201 asus_notify_handler(ACPI_HANDLE hdl, uint32_t notify, void *opaque)
202 {
203 struct asus_softc *sc;
204 device_t self = opaque;
205
206 sc = device_private(self);
207
208 if (notify >= ASUS_NOTIFY_BrightnessLow &&
209 notify <= ASUS_NOTIFY_BrightnessHigh) {
210 aprint_debug_dev(sc->sc_dev, "brightness %d percent\n",
211 (notify & 0xf) * 100 / 0xf);
212 return;
213 }
214
215 switch (notify) {
216 case ASUS_NOTIFY_WirelessSwitch: /* handled by AML */
217 case ASUS_NOTIFY_WindowSwitch: /* XXXJDM what is this? */
218 break;
219 case ASUS_NOTIFY_DisplayCycle:
220 if (sc->sc_smpsw_valid == false)
221 break;
222 sysmon_pswitch_event(&sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE],
223 PSWITCH_EVENT_PRESSED);
224 break;
225 case ASUS_NOTIFY_VolumeMute:
226 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_TOGGLE);
227 break;
228 case ASUS_NOTIFY_VolumeDown:
229 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
230 break;
231 case ASUS_NOTIFY_VolumeUp:
232 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
233 break;
234 default:
235 aprint_debug_dev(sc->sc_dev, "unknown event 0x%02x\n", notify);
236 break;
237 }
238 }
239
240 static void
241 asus_init(device_t self)
242 {
243 struct asus_softc *sc = device_private(self);
244 ACPI_INTEGER cfv;
245 ACPI_STATUS rv;
246
247 /* Disable ASL display switching. */
248 rv = acpi_eval_set_integer(sc->sc_node->ad_handle, "INIT", 0x40);
249
250 if (ACPI_FAILURE(rv))
251 aprint_error_dev(self, "couldn't evaluate INIT: %s\n",
252 AcpiFormatException(rv));
253
254 rv = acpi_eval_integer(sc->sc_node->ad_handle, ASUS_METHOD_CFVG, &cfv);
255
256 if (ACPI_FAILURE(rv))
257 return;
258
259 sc->sc_cfvnum = (cfv >> 8) & 0xff;
260 }
261
262 static bool
263 asus_suspend(device_t self, const pmf_qual_t *qual)
264 {
265 struct asus_softc *sc = device_private(self);
266 ACPI_INTEGER val = 0;
267 ACPI_STATUS rv;
268
269 /* Capture display brightness. */
270 rv = acpi_eval_integer(sc->sc_node->ad_handle, ASUS_METHOD_PBLG, &val);
271
272 if (ACPI_FAILURE(rv) || val > INT32_MAX)
273 sc->sc_brightness = -1;
274 else
275 sc->sc_brightness = val;
276
277 return true;
278 }
279
280 static bool
281 asus_resume(device_t self, const pmf_qual_t *qual)
282 {
283 struct asus_softc *sc = device_private(self);
284 ACPI_STATUS rv;
285
286 asus_init(self);
287
288 if (sc->sc_brightness < 0)
289 return true;
290
291 /* Restore previous display brightness. */
292 rv = acpi_eval_set_integer(sc->sc_node->ad_handle, ASUS_METHOD_PBLS,
293 sc->sc_brightness);
294
295 if (ACPI_FAILURE(rv))
296 aprint_error_dev(self, "couldn't evaluate PBLS: %s\n",
297 AcpiFormatException(rv));
298
299 return true;
300 }
301
302 static int
303 asus_sysctl_verify(SYSCTLFN_ARGS)
304 {
305 struct sysctlnode node;
306 struct asus_softc *sc;
307 ACPI_INTEGER cfv;
308 ACPI_STATUS rv;
309 int err, tmp;
310
311 node = *rnode;
312 sc = rnode->sysctl_data;
313 if (node.sysctl_num == sc->sc_cfv_mib) {
314 rv = acpi_eval_integer(sc->sc_node->ad_handle,
315 ASUS_METHOD_CFVG, &cfv);
316 if (ACPI_FAILURE(rv))
317 return ENXIO;
318 tmp = cfv & 0xff;
319 node.sysctl_data = &tmp;
320 err = sysctl_lookup(SYSCTLFN_CALL(&node));
321 if (err || newp == NULL)
322 return err;
323
324 if (tmp < 0 || (uint64_t)tmp >= sc->sc_cfvnum)
325 return EINVAL;
326
327 rv = acpi_eval_set_integer(sc->sc_node->ad_handle,
328 ASUS_METHOD_CFVS, tmp);
329
330 if (ACPI_FAILURE(rv))
331 return ENXIO;
332 }
333
334 return 0;
335 }
336
337 static void
338 asus_sysctl_setup(struct asus_softc *sc)
339 {
340 const struct sysctlnode *node, *node_cfv, *node_ncfv;
341 int err, node_mib;
342
343 if (sc->sc_cfvnum == 0)
344 return;
345
346 err = sysctl_createv(&sc->sc_log, 0, NULL, NULL, 0,
347 CTLTYPE_NODE, "hw", NULL, NULL, 0, NULL, 0, CTL_HW, CTL_EOL);
348 if (err)
349 goto sysctl_err;
350 err = sysctl_createv(&sc->sc_log, 0, NULL, &node, 0,
351 CTLTYPE_NODE, device_xname(sc->sc_dev), NULL, NULL, 0,
352 NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
353 if (err)
354 goto sysctl_err;
355 node_mib = node->sysctl_num;
356 err = sysctl_createv(&sc->sc_log, 0, NULL, &node_ncfv,
357 CTLFLAG_READONLY, CTLTYPE_INT, "ncfv",
358 SYSCTL_DESCR("Number of CPU frequency/voltage modes"),
359 NULL, 0, &sc->sc_cfvnum, 0,
360 CTL_HW, node_mib, CTL_CREATE, CTL_EOL);
361 if (err)
362 goto sysctl_err;
363 sc->sc_cfvnum_mib = node_ncfv->sysctl_num;
364 err = sysctl_createv(&sc->sc_log, 0, NULL, &node_cfv,
365 CTLFLAG_READWRITE, CTLTYPE_INT, "cfv",
366 SYSCTL_DESCR("Current CPU frequency/voltage mode"),
367 asus_sysctl_verify, 0, sc, 0,
368 CTL_HW, node_mib, CTL_CREATE, CTL_EOL);
369 if (err)
370 goto sysctl_err;
371 sc->sc_cfv_mib = node_cfv->sysctl_num;
372
373 return;
374 sysctl_err:
375 aprint_error_dev(sc->sc_dev, "failed to add sysctl nodes. (%d)\n", err);
376 }
377
378 static void
379 asus_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
380 {
381 struct asus_softc *sc = sme->sme_cookie;
382 uint32_t rpm;
383
384 switch (edata->sensor) {
385 case ASUS_SENSOR_FAN:
386 if (asus_get_fan_speed(sc, &rpm)) {
387 edata->value_cur = rpm;
388 edata->state = ENVSYS_SVALID;
389 } else
390 edata->state = ENVSYS_SINVALID;
391 break;
392 }
393 }
394
395 static bool
396 asus_get_fan_speed(struct asus_softc *sc, uint32_t *speed)
397 {
398 ACPI_INTEGER rpmh, rpml;
399 ACPI_STATUS rv;
400
401 rv = acpi_eval_integer(sc->sc_node->ad_handle,
402 ASUS_EC_METHOD_FAN_RPMH, &rpmh);
403 if (ACPI_FAILURE(rv))
404 return false;
405 rv = acpi_eval_integer(sc->sc_node->ad_handle,
406 ASUS_EC_METHOD_FAN_RPML, &rpml);
407 if (ACPI_FAILURE(rv))
408 return false;
409
410 if (speed)
411 *speed = (rpmh << 8) | rpml;
412 return true;
413 }
414
415 MODULE(MODULE_CLASS_DRIVER, asus, NULL);
416
417 #ifdef _MODULE
418 #include "ioconf.c"
419 #endif
420
421 static int
422 asus_modcmd(modcmd_t cmd, void *aux)
423 {
424 int rv = 0;
425
426 switch (cmd) {
427
428 case MODULE_CMD_INIT:
429
430 #ifdef _MODULE
431 rv = config_init_component(cfdriver_ioconf_asus,
432 cfattach_ioconf_asus, cfdata_ioconf_asus);
433 #endif
434 break;
435
436 case MODULE_CMD_FINI:
437
438 #ifdef _MODULE
439 rv = config_fini_component(cfdriver_ioconf_asus,
440 cfattach_ioconf_asus, cfdata_ioconf_asus);
441 #endif
442 break;
443
444 default:
445 rv = ENOTTY;
446 }
447
448 return rv;
449 }
450