acpi_acad.c revision 1.38 1 1.38 jruoho /* $NetBSD: acpi_acad.c,v 1.38 2010/01/31 07:34:10 jruoho Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright 2001 Wasabi Systems, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.1 thorpej *
9 1.1 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1 thorpej * modification, are permitted provided that the following conditions
11 1.1 thorpej * are met:
12 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer.
14 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.1 thorpej * documentation and/or other materials provided with the distribution.
17 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.1 thorpej * must display the following acknowledgement:
19 1.1 thorpej * This product includes software developed for the NetBSD Project by
20 1.1 thorpej * Wasabi Systems, Inc.
21 1.1 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 thorpej * or promote products derived from this software without specific prior
23 1.1 thorpej * written permission.
24 1.1 thorpej *
25 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
36 1.1 thorpej */
37 1.1 thorpej
38 1.7 tshiozak #if 0
39 1.7 tshiozak #define ACPI_ACAD_DEBUG
40 1.7 tshiozak #endif
41 1.7 tshiozak
42 1.1 thorpej /*
43 1.1 thorpej * ACPI AC Adapter driver.
44 1.1 thorpej */
45 1.2 lukem
46 1.2 lukem #include <sys/cdefs.h>
47 1.38 jruoho __KERNEL_RCSID(0, "$NetBSD: acpi_acad.c,v 1.38 2010/01/31 07:34:10 jruoho Exp $");
48 1.1 thorpej
49 1.1 thorpej #include <sys/param.h>
50 1.1 thorpej #include <sys/systm.h>
51 1.1 thorpej #include <sys/device.h>
52 1.23 xtraeme #include <sys/mutex.h>
53 1.1 thorpej
54 1.1 thorpej #include <dev/acpi/acpica.h>
55 1.1 thorpej #include <dev/acpi/acpireg.h>
56 1.1 thorpej #include <dev/acpi/acpivar.h>
57 1.1 thorpej
58 1.6 explorer #include <dev/sysmon/sysmonvar.h>
59 1.6 explorer
60 1.37 jruoho #define _COMPONENT ACPI_ACAD_COMPONENT
61 1.37 jruoho ACPI_MODULE_NAME ("acpi_acad")
62 1.37 jruoho
63 1.1 thorpej struct acpiacad_softc {
64 1.1 thorpej struct acpi_devnode *sc_node; /* our ACPI devnode */
65 1.1 thorpej int sc_flags; /* see below */
66 1.23 xtraeme int sc_status; /* status changed/not changed */
67 1.23 xtraeme int sc_notifysent; /* notify message sent */
68 1.7 tshiozak
69 1.29 xtraeme struct sysmon_envsys *sc_sme;
70 1.16 kochi struct sysmon_pswitch sc_smpsw; /* our sysmon glue */
71 1.29 xtraeme envsys_data_t sc_sensor;
72 1.7 tshiozak
73 1.23 xtraeme kmutex_t sc_mtx;
74 1.1 thorpej };
75 1.1 thorpej
76 1.10 kochi static const char * const acad_hid[] = {
77 1.10 kochi "ACPI0003",
78 1.10 kochi NULL
79 1.10 kochi };
80 1.10 kochi
81 1.1 thorpej #define AACAD_F_VERBOSE 0x01 /* verbose events */
82 1.7 tshiozak #define AACAD_F_AVAILABLE 0x02 /* information is available */
83 1.23 xtraeme #define AACAD_F_STCHANGED 0x04 /* status changed */
84 1.7 tshiozak
85 1.7 tshiozak #define AACAD_SET(sc, f) (void)((sc)->sc_flags |= (f))
86 1.7 tshiozak #define AACAD_CLEAR(sc, f) (void)((sc)->sc_flags &= ~(f))
87 1.7 tshiozak #define AACAD_ISSET(sc, f) ((sc)->sc_flags & (f))
88 1.7 tshiozak
89 1.38 jruoho static int acpiacad_match(device_t, cfdata_t, void *);
90 1.27 joerg static void acpiacad_attach(device_t, device_t, void *);
91 1.38 jruoho static int acpiacad_detach(device_t, int);
92 1.7 tshiozak static void acpiacad_get_status(void *);
93 1.7 tshiozak static void acpiacad_clear_status(struct acpiacad_softc *);
94 1.7 tshiozak static void acpiacad_notify_handler(ACPI_HANDLE, UINT32, void *);
95 1.27 joerg static void acpiacad_init_envsys(device_t);
96 1.29 xtraeme static void acpiacad_refresh(struct sysmon_envsys *, envsys_data_t *);
97 1.36 dyoung static bool acpiacad_resume(device_t, pmf_qual_t);
98 1.1 thorpej
99 1.38 jruoho CFATTACH_DECL_NEW(acpiacad, sizeof(struct acpiacad_softc),
100 1.38 jruoho acpiacad_match, acpiacad_attach, acpiacad_detach, NULL);
101 1.38 jruoho
102 1.1 thorpej /*
103 1.1 thorpej * acpiacad_match:
104 1.1 thorpej *
105 1.1 thorpej * Autoconfiguration `match' routine.
106 1.1 thorpej */
107 1.15 kochi static int
108 1.34 cegger acpiacad_match(device_t parent, cfdata_t match, void *aux)
109 1.1 thorpej {
110 1.1 thorpej struct acpi_attach_args *aa = aux;
111 1.1 thorpej
112 1.1 thorpej if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
113 1.14 kochi return 0;
114 1.1 thorpej
115 1.14 kochi return acpi_match_hid(aa->aa_node->ad_devinfo, acad_hid);
116 1.1 thorpej }
117 1.1 thorpej
118 1.1 thorpej /*
119 1.1 thorpej * acpiacad_attach:
120 1.1 thorpej *
121 1.1 thorpej * Autoconfiguration `attach' routine.
122 1.1 thorpej */
123 1.15 kochi static void
124 1.27 joerg acpiacad_attach(device_t parent, device_t self, void *aux)
125 1.1 thorpej {
126 1.27 joerg struct acpiacad_softc *sc = device_private(self);
127 1.1 thorpej struct acpi_attach_args *aa = aux;
128 1.1 thorpej ACPI_STATUS rv;
129 1.1 thorpej
130 1.18 kochi aprint_naive(": ACPI AC Adapter\n");
131 1.18 kochi aprint_normal(": ACPI AC Adapter\n");
132 1.1 thorpej
133 1.1 thorpej sc->sc_node = aa->aa_node;
134 1.30 ad mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
135 1.1 thorpej
136 1.27 joerg sc->sc_smpsw.smpsw_name = device_xname(self);
137 1.16 kochi sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_ACADAPTER;
138 1.16 kochi if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
139 1.27 joerg aprint_error_dev(self, "unable to register with sysmon\n");
140 1.16 kochi return;
141 1.16 kochi }
142 1.16 kochi
143 1.38 jruoho sc->sc_sme = NULL;
144 1.23 xtraeme sc->sc_status = -1;
145 1.1 thorpej
146 1.1 thorpej rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
147 1.27 joerg ACPI_ALL_NOTIFY, acpiacad_notify_handler, self);
148 1.12 mycroft if (ACPI_FAILURE(rv)) {
149 1.27 joerg aprint_error_dev(self, "unable to register DEVICE and SYSTEM "
150 1.27 joerg "NOTIFY handler: %s\n", AcpiFormatException(rv));
151 1.1 thorpej return;
152 1.1 thorpej }
153 1.1 thorpej
154 1.7 tshiozak #ifdef ACPI_ACAD_DEBUG
155 1.1 thorpej /* Display the current state. */
156 1.1 thorpej sc->sc_flags = AACAD_F_VERBOSE;
157 1.7 tshiozak #endif
158 1.7 tshiozak
159 1.32 xtraeme if (!pmf_device_register(self, NULL, acpiacad_resume))
160 1.31 jmcneill aprint_error_dev(self, "couldn't establish power handler\n");
161 1.31 jmcneill
162 1.27 joerg acpiacad_init_envsys(self);
163 1.1 thorpej }
164 1.1 thorpej
165 1.1 thorpej /*
166 1.38 jruoho * acpiacad_detach:
167 1.38 jruoho *
168 1.38 jruoho * Autoconfiguration `detach' routine.
169 1.38 jruoho */
170 1.38 jruoho static int
171 1.38 jruoho acpiacad_detach(device_t self, int flags)
172 1.38 jruoho {
173 1.38 jruoho struct acpiacad_softc *sc = device_private(self);
174 1.38 jruoho ACPI_STATUS rv;
175 1.38 jruoho
176 1.38 jruoho rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle,
177 1.38 jruoho ACPI_ALL_NOTIFY, acpiacad_notify_handler);
178 1.38 jruoho
179 1.38 jruoho if (ACPI_FAILURE(rv))
180 1.38 jruoho return EBUSY;
181 1.38 jruoho
182 1.38 jruoho mutex_destroy(&sc->sc_mtx);
183 1.38 jruoho
184 1.38 jruoho if (sc->sc_sme != NULL)
185 1.38 jruoho sysmon_envsys_unregister(sc->sc_sme);
186 1.38 jruoho
187 1.38 jruoho pmf_device_deregister(self);
188 1.38 jruoho sysmon_pswitch_unregister(&sc->sc_smpsw);
189 1.38 jruoho
190 1.38 jruoho return 0;
191 1.38 jruoho }
192 1.38 jruoho
193 1.38 jruoho /*
194 1.32 xtraeme * acpiacad_resume:
195 1.32 xtraeme *
196 1.32 xtraeme * Clear status after resuming to fetch new status.
197 1.32 xtraeme */
198 1.32 xtraeme static bool
199 1.36 dyoung acpiacad_resume(device_t dv, pmf_qual_t qual)
200 1.32 xtraeme {
201 1.32 xtraeme struct acpiacad_softc *sc = device_private(dv);
202 1.32 xtraeme
203 1.32 xtraeme acpiacad_clear_status(sc);
204 1.32 xtraeme return true;
205 1.32 xtraeme }
206 1.32 xtraeme
207 1.32 xtraeme /*
208 1.1 thorpej * acpiacad_get_status:
209 1.1 thorpej *
210 1.1 thorpej * Get, and possibly display, the current AC line status.
211 1.1 thorpej */
212 1.15 kochi static void
213 1.1 thorpej acpiacad_get_status(void *arg)
214 1.1 thorpej {
215 1.27 joerg device_t dv = arg;
216 1.27 joerg struct acpiacad_softc *sc = device_private(dv);
217 1.13 kanaoka ACPI_INTEGER status;
218 1.12 mycroft ACPI_STATUS rv;
219 1.1 thorpej
220 1.12 mycroft rv = acpi_eval_integer(sc->sc_node->ad_handle, "_PSR", &status);
221 1.12 mycroft if (ACPI_FAILURE(rv))
222 1.1 thorpej return;
223 1.1 thorpej
224 1.23 xtraeme mutex_enter(&sc->sc_mtx);
225 1.33 xtraeme sc->sc_notifysent = 0;
226 1.23 xtraeme if (sc->sc_status != status) {
227 1.23 xtraeme sc->sc_status = status;
228 1.25 xtraeme if (status)
229 1.29 xtraeme sc->sc_sensor.value_cur = 1;
230 1.25 xtraeme else
231 1.29 xtraeme sc->sc_sensor.value_cur = 0;
232 1.23 xtraeme AACAD_SET(sc, AACAD_F_STCHANGED);
233 1.23 xtraeme }
234 1.23 xtraeme
235 1.29 xtraeme sc->sc_sensor.state = ENVSYS_SVALID;
236 1.7 tshiozak AACAD_SET(sc, AACAD_F_AVAILABLE);
237 1.16 kochi /*
238 1.23 xtraeme * If status has changed, send the event.
239 1.23 xtraeme *
240 1.16 kochi * PSWITCH_EVENT_RELEASED : AC offline
241 1.16 kochi * PSWITCH_EVENT_PRESSED : AC online
242 1.16 kochi */
243 1.23 xtraeme if (AACAD_ISSET(sc, AACAD_F_STCHANGED)) {
244 1.23 xtraeme sysmon_pswitch_event(&sc->sc_smpsw, status ?
245 1.23 xtraeme PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
246 1.23 xtraeme if (AACAD_ISSET(sc, AACAD_F_VERBOSE))
247 1.27 joerg aprint_verbose_dev(dv, "AC adapter %sconnected\n",
248 1.27 joerg status == 0 ? "not " : "");
249 1.23 xtraeme }
250 1.23 xtraeme mutex_exit(&sc->sc_mtx);
251 1.7 tshiozak }
252 1.7 tshiozak
253 1.7 tshiozak /*
254 1.7 tshiozak * Clear status
255 1.7 tshiozak */
256 1.15 kochi static void
257 1.7 tshiozak acpiacad_clear_status(struct acpiacad_softc *sc)
258 1.7 tshiozak {
259 1.29 xtraeme sc->sc_sensor.state = ENVSYS_SINVALID;
260 1.7 tshiozak AACAD_CLEAR(sc, AACAD_F_AVAILABLE);
261 1.23 xtraeme AACAD_CLEAR(sc, AACAD_F_STCHANGED);
262 1.1 thorpej }
263 1.1 thorpej
264 1.1 thorpej /*
265 1.1 thorpej * acpiacad_notify_handler:
266 1.1 thorpej *
267 1.1 thorpej * Callback from ACPI interrupt handler to notify us of an event.
268 1.1 thorpej */
269 1.15 kochi static void
270 1.23 xtraeme acpiacad_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
271 1.1 thorpej {
272 1.27 joerg device_t dv = context;
273 1.27 joerg struct acpiacad_softc *sc = device_private(dv);
274 1.35 jmcneill ACPI_STATUS rv;
275 1.1 thorpej
276 1.1 thorpej switch (notify) {
277 1.1 thorpej /*
278 1.1 thorpej * XXX So, BusCheck is not exactly what I would expect,
279 1.1 thorpej * but at least my IBM T21 sends it on AC adapter status
280 1.1 thorpej * change. --thorpej (at) wasabisystems.com
281 1.1 thorpej */
282 1.19 rpaulo /*
283 1.19 rpaulo * XXX My Acer TravelMate 291 sends DeviceCheck on AC
284 1.19 rpaulo * adapter status change.
285 1.19 rpaulo * --rpaulo (at) NetBSD.org
286 1.19 rpaulo */
287 1.22 jmcneill /*
288 1.22 jmcneill * XXX Sony VAIO VGN-N250E sends BatteryInformationChanged on AC
289 1.22 jmcneill * adapter status change.
290 1.22 jmcneill * --jmcneill (at) NetBSD.org
291 1.22 jmcneill */
292 1.1 thorpej case ACPI_NOTIFY_BusCheck:
293 1.19 rpaulo case ACPI_NOTIFY_DeviceCheck:
294 1.1 thorpej case ACPI_NOTIFY_PowerSourceStatusChanged:
295 1.22 jmcneill case ACPI_NOTIFY_BatteryInformationChanged:
296 1.23 xtraeme mutex_enter(&sc->sc_mtx);
297 1.23 xtraeme acpiacad_clear_status(sc);
298 1.23 xtraeme mutex_exit(&sc->sc_mtx);
299 1.23 xtraeme if (sc->sc_status == -1 || !sc->sc_notifysent) {
300 1.31 jmcneill rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
301 1.27 joerg acpiacad_get_status, dv);
302 1.23 xtraeme if (ACPI_FAILURE(rv))
303 1.27 joerg aprint_error_dev(dv,
304 1.27 joerg "unable to queue status check: %s\n",
305 1.23 xtraeme AcpiFormatException(rv));
306 1.23 xtraeme sc->sc_notifysent = 1;
307 1.1 thorpej #ifdef ACPI_ACAD_DEBUG
308 1.27 joerg aprint_debug_dev(dv, "received notify message: 0x%x\n",
309 1.27 joerg notify);
310 1.1 thorpej #endif
311 1.23 xtraeme }
312 1.1 thorpej break;
313 1.1 thorpej
314 1.1 thorpej default:
315 1.27 joerg aprint_error_dev(dv, "received unknown notify message: 0x%x\n",
316 1.27 joerg notify);
317 1.1 thorpej }
318 1.6 explorer }
319 1.6 explorer
320 1.15 kochi static void
321 1.27 joerg acpiacad_init_envsys(device_t dv)
322 1.6 explorer {
323 1.27 joerg struct acpiacad_softc *sc = device_private(dv);
324 1.27 joerg
325 1.29 xtraeme sc->sc_sme = sysmon_envsys_create();
326 1.29 xtraeme sc->sc_sensor.state = ENVSYS_SVALID;
327 1.29 xtraeme sc->sc_sensor.units = ENVSYS_INDICATOR;
328 1.29 xtraeme strlcpy(sc->sc_sensor.desc, "connected", sizeof(sc->sc_sensor.desc));
329 1.29 xtraeme
330 1.38 jruoho if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor) != 0)
331 1.38 jruoho goto fail;
332 1.6 explorer
333 1.29 xtraeme sc->sc_sme->sme_name = device_xname(dv);
334 1.29 xtraeme sc->sc_sme->sme_cookie = dv;
335 1.29 xtraeme sc->sc_sme->sme_refresh = acpiacad_refresh;
336 1.29 xtraeme sc->sc_sme->sme_class = SME_CLASS_ACADAPTER;
337 1.32 xtraeme sc->sc_sme->sme_flags = SME_INIT_REFRESH;
338 1.29 xtraeme
339 1.38 jruoho if (sysmon_envsys_register(sc->sc_sme) != 0)
340 1.38 jruoho goto fail;
341 1.38 jruoho
342 1.38 jruoho return;
343 1.38 jruoho
344 1.38 jruoho fail:
345 1.38 jruoho aprint_error_dev(dv, "failed to initialize sysmon\n");
346 1.38 jruoho sysmon_envsys_destroy(sc->sc_sme);
347 1.38 jruoho sc->sc_sme = NULL;
348 1.6 explorer }
349 1.6 explorer
350 1.29 xtraeme static void
351 1.29 xtraeme acpiacad_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
352 1.6 explorer {
353 1.27 joerg device_t dv = sme->sme_cookie;
354 1.27 joerg struct acpiacad_softc *sc = device_private(dv);
355 1.7 tshiozak
356 1.7 tshiozak if (!AACAD_ISSET(sc, AACAD_F_AVAILABLE))
357 1.27 joerg acpiacad_get_status(dv);
358 1.1 thorpej }
359