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