asus_acpi.c revision 1.6 1 1.6 jmcneill /* $NetBSD: asus_acpi.c,v 1.6 2008/09/21 21:15:28 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2007, 2008 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.6 jmcneill __KERNEL_RCSID(0, "$NetBSD: asus_acpi.c,v 1.6 2008/09/21 21:15:28 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/types.h>
33 1.1 jmcneill #include <sys/param.h>
34 1.1 jmcneill #include <sys/malloc.h>
35 1.1 jmcneill #include <sys/buf.h>
36 1.1 jmcneill #include <sys/callout.h>
37 1.1 jmcneill #include <sys/kernel.h>
38 1.1 jmcneill #include <sys/device.h>
39 1.1 jmcneill #include <sys/pmf.h>
40 1.1 jmcneill
41 1.1 jmcneill #include <dev/acpi/acpivar.h>
42 1.1 jmcneill
43 1.1 jmcneill typedef struct asus_softc {
44 1.1 jmcneill device_t sc_dev;
45 1.1 jmcneill struct acpi_devnode *sc_node;
46 1.1 jmcneill
47 1.1 jmcneill #define ASUS_PSW_DISPLAY_CYCLE 0
48 1.1 jmcneill #define ASUS_PSW_LAST 1
49 1.1 jmcneill struct sysmon_pswitch sc_smpsw[ASUS_PSW_LAST];
50 1.1 jmcneill bool sc_smpsw_valid;
51 1.6 jmcneill
52 1.6 jmcneill ACPI_INTEGER sc_brightness;
53 1.1 jmcneill } asus_softc_t;
54 1.1 jmcneill
55 1.1 jmcneill #define ASUS_NOTIFY_WirelessSwitch 0x10
56 1.1 jmcneill #define ASUS_NOTIFY_BrightnessLow 0x20
57 1.1 jmcneill #define ASUS_NOTIFY_BrightnessHigh 0x2f
58 1.1 jmcneill #define ASUS_NOTIFY_DisplayCycle 0x30
59 1.1 jmcneill #define ASUS_NOTIFY_WindowSwitch 0x12 /* XXXJDM ?? */
60 1.1 jmcneill #define ASUS_NOTIFY_VolumeMute 0x13
61 1.1 jmcneill #define ASUS_NOTIFY_VolumeDown 0x14
62 1.1 jmcneill #define ASUS_NOTIFY_VolumeUp 0x15
63 1.1 jmcneill
64 1.1 jmcneill #define ASUS_METHOD_SDSP "SDSP"
65 1.1 jmcneill #define ASUS_SDSP_LCD 0x01
66 1.1 jmcneill #define ASUS_SDSP_CRT 0x02
67 1.1 jmcneill #define ASUS_SDSP_TV 0x04
68 1.1 jmcneill #define ASUS_SDSP_DVI 0x08
69 1.1 jmcneill #define ASUS_SDSP_ALL \
70 1.1 jmcneill (ASUS_SDSP_LCD | ASUS_SDSP_CRT | ASUS_SDSP_TV | ASUS_SDSP_DVI)
71 1.6 jmcneill #define ASUS_METHOD_PBLG "PBLG"
72 1.6 jmcneill #define ASUS_METHOD_PBLS "PBLS"
73 1.1 jmcneill
74 1.3 jmcneill static int asus_match(device_t, cfdata_t, void *);
75 1.1 jmcneill static void asus_attach(device_t, device_t, void *);
76 1.1 jmcneill
77 1.1 jmcneill static void asus_notify_handler(ACPI_HANDLE, UINT32, void *);
78 1.1 jmcneill
79 1.1 jmcneill static void asus_init(device_t);
80 1.6 jmcneill static bool asus_suspend(device_t PMF_FN_PROTO);
81 1.1 jmcneill static bool asus_resume(device_t PMF_FN_PROTO);
82 1.1 jmcneill
83 1.1 jmcneill CFATTACH_DECL_NEW(asus, sizeof(asus_softc_t),
84 1.1 jmcneill asus_match, asus_attach, NULL, NULL);
85 1.1 jmcneill
86 1.1 jmcneill static const char * const asus_ids[] = {
87 1.1 jmcneill "ASUS010",
88 1.1 jmcneill NULL
89 1.1 jmcneill };
90 1.1 jmcneill
91 1.1 jmcneill static int
92 1.1 jmcneill asus_match(device_t parent, cfdata_t match, void *opaque)
93 1.1 jmcneill {
94 1.1 jmcneill struct acpi_attach_args *aa = opaque;
95 1.1 jmcneill
96 1.1 jmcneill if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
97 1.1 jmcneill return 0;
98 1.1 jmcneill
99 1.1 jmcneill return acpi_match_hid(aa->aa_node->ad_devinfo, asus_ids);
100 1.1 jmcneill }
101 1.1 jmcneill
102 1.1 jmcneill static void
103 1.1 jmcneill asus_attach(device_t parent, device_t self, void *opaque)
104 1.1 jmcneill {
105 1.1 jmcneill asus_softc_t *sc = device_private(self);
106 1.1 jmcneill struct acpi_attach_args *aa = opaque;
107 1.1 jmcneill ACPI_STATUS rv;
108 1.1 jmcneill
109 1.1 jmcneill sc->sc_node = aa->aa_node;
110 1.1 jmcneill sc->sc_dev = self;
111 1.1 jmcneill
112 1.1 jmcneill aprint_naive("\n");
113 1.1 jmcneill aprint_normal("\n");
114 1.1 jmcneill
115 1.1 jmcneill asus_init(self);
116 1.1 jmcneill
117 1.1 jmcneill sc->sc_smpsw_valid = true;
118 1.1 jmcneill sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE].smpsw_name =
119 1.1 jmcneill PSWITCH_HK_DISPLAY_CYCLE;
120 1.1 jmcneill sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE].smpsw_type =
121 1.1 jmcneill PSWITCH_TYPE_HOTKEY;
122 1.1 jmcneill if (sysmon_pswitch_register(&sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE])) {
123 1.1 jmcneill aprint_error_dev(self, "couldn't register with sysmon\n");
124 1.1 jmcneill sc->sc_smpsw_valid = false;
125 1.1 jmcneill }
126 1.1 jmcneill
127 1.1 jmcneill rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle, ACPI_ALL_NOTIFY,
128 1.1 jmcneill asus_notify_handler, sc);
129 1.1 jmcneill if (ACPI_FAILURE(rv))
130 1.1 jmcneill aprint_error_dev(self, "couldn't install notify handler: %s\n",
131 1.1 jmcneill AcpiFormatException(rv));
132 1.1 jmcneill
133 1.6 jmcneill if (!pmf_device_register(self, asus_suspend, asus_resume))
134 1.1 jmcneill aprint_error_dev(self, "couldn't establish power handler\n");
135 1.1 jmcneill }
136 1.1 jmcneill
137 1.1 jmcneill static void
138 1.1 jmcneill asus_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
139 1.1 jmcneill {
140 1.1 jmcneill asus_softc_t *sc = opaque;
141 1.1 jmcneill
142 1.1 jmcneill if (notify >= ASUS_NOTIFY_BrightnessLow &&
143 1.1 jmcneill notify <= ASUS_NOTIFY_BrightnessHigh) {
144 1.1 jmcneill aprint_debug_dev(sc->sc_dev, "brightness %d percent\n",
145 1.1 jmcneill (notify & 0xf) * 100 / 0xf);
146 1.1 jmcneill return;
147 1.1 jmcneill }
148 1.1 jmcneill
149 1.1 jmcneill switch (notify) {
150 1.1 jmcneill case ASUS_NOTIFY_WirelessSwitch: /* handled by AML */
151 1.1 jmcneill case ASUS_NOTIFY_WindowSwitch: /* XXXJDM what is this? */
152 1.1 jmcneill break;
153 1.1 jmcneill case ASUS_NOTIFY_DisplayCycle:
154 1.1 jmcneill if (sc->sc_smpsw_valid == false)
155 1.1 jmcneill break;
156 1.1 jmcneill sysmon_pswitch_event(&sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE],
157 1.1 jmcneill PSWITCH_EVENT_PRESSED);
158 1.1 jmcneill break;
159 1.1 jmcneill case ASUS_NOTIFY_VolumeMute:
160 1.1 jmcneill pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_TOGGLE);
161 1.1 jmcneill break;
162 1.1 jmcneill case ASUS_NOTIFY_VolumeDown:
163 1.1 jmcneill pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
164 1.1 jmcneill break;
165 1.1 jmcneill case ASUS_NOTIFY_VolumeUp:
166 1.1 jmcneill pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
167 1.1 jmcneill break;
168 1.1 jmcneill default:
169 1.1 jmcneill aprint_debug_dev(sc->sc_dev, "unknown event 0x%02x\n", notify);
170 1.1 jmcneill break;
171 1.1 jmcneill }
172 1.1 jmcneill }
173 1.1 jmcneill
174 1.1 jmcneill static void
175 1.1 jmcneill asus_init(device_t self)
176 1.1 jmcneill {
177 1.1 jmcneill asus_softc_t *sc = device_private(self);
178 1.1 jmcneill ACPI_STATUS rv;
179 1.1 jmcneill ACPI_OBJECT param;
180 1.1 jmcneill ACPI_OBJECT_LIST params;
181 1.1 jmcneill ACPI_BUFFER ret;
182 1.1 jmcneill
183 1.1 jmcneill ret.Pointer = NULL;
184 1.1 jmcneill ret.Length = ACPI_ALLOCATE_BUFFER;
185 1.1 jmcneill param.Type = ACPI_TYPE_INTEGER;
186 1.1 jmcneill param.Integer.Value = 0x40; /* disable ASL display switching */
187 1.1 jmcneill params.Pointer = ¶m;
188 1.1 jmcneill params.Count = 1;
189 1.1 jmcneill
190 1.1 jmcneill rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "INIT",
191 1.1 jmcneill ¶ms, &ret);
192 1.1 jmcneill if (ACPI_FAILURE(rv))
193 1.1 jmcneill aprint_error_dev(self, "couldn't evaluate INIT: %s\n",
194 1.1 jmcneill AcpiFormatException(rv));
195 1.5 jmcneill
196 1.5 jmcneill if (ret.Pointer)
197 1.5 jmcneill AcpiOsFree(ret.Pointer);
198 1.1 jmcneill }
199 1.1 jmcneill
200 1.1 jmcneill static bool
201 1.6 jmcneill asus_suspend(device_t self PMF_FN_ARGS)
202 1.6 jmcneill {
203 1.6 jmcneill asus_softc_t *sc = device_private(self);
204 1.6 jmcneill ACPI_STATUS rv;
205 1.6 jmcneill
206 1.6 jmcneill /* capture display brightness when we're sleeping */
207 1.6 jmcneill rv = acpi_eval_integer(sc->sc_node->ad_handle, ASUS_METHOD_PBLG,
208 1.6 jmcneill &sc->sc_brightness);
209 1.6 jmcneill if (ACPI_FAILURE(rv))
210 1.6 jmcneill aprint_error_dev(sc->sc_dev, "couldn't evaluate PBLG: %s\n",
211 1.6 jmcneill AcpiFormatException(rv));
212 1.6 jmcneill
213 1.6 jmcneill return true;
214 1.6 jmcneill }
215 1.6 jmcneill
216 1.6 jmcneill static bool
217 1.1 jmcneill asus_resume(device_t self PMF_FN_ARGS)
218 1.1 jmcneill {
219 1.1 jmcneill asus_softc_t *sc = device_private(self);
220 1.1 jmcneill ACPI_STATUS rv;
221 1.1 jmcneill ACPI_OBJECT param;
222 1.1 jmcneill ACPI_OBJECT_LIST params;
223 1.1 jmcneill ACPI_BUFFER ret;
224 1.1 jmcneill
225 1.1 jmcneill asus_init(self);
226 1.1 jmcneill
227 1.6 jmcneill /* restore previous display brightness */
228 1.1 jmcneill ret.Pointer = NULL;
229 1.1 jmcneill ret.Length = ACPI_ALLOCATE_BUFFER;
230 1.1 jmcneill param.Type = ACPI_TYPE_INTEGER;
231 1.6 jmcneill param.Integer.Value = sc->sc_brightness;
232 1.1 jmcneill params.Pointer = ¶m;
233 1.1 jmcneill params.Count = 1;
234 1.1 jmcneill
235 1.6 jmcneill rv = AcpiEvaluateObject(sc->sc_node->ad_handle, ASUS_METHOD_PBLS,
236 1.1 jmcneill ¶ms, &ret);
237 1.1 jmcneill if (ACPI_FAILURE(rv))
238 1.6 jmcneill aprint_error_dev(self, "couldn't evaluate PBLS: %s\n",
239 1.1 jmcneill AcpiFormatException(rv));
240 1.1 jmcneill
241 1.1 jmcneill return true;
242 1.1 jmcneill }
243