acpi_acad.c revision 1.41 1 /* $NetBSD: acpi_acad.c,v 1.41 2010/02/28 17:22:41 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 /*
39 * ACPI AC Adapter driver.
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: acpi_acad.c,v 1.41 2010/02/28 17:22:41 jruoho Exp $");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
48 #include <sys/module.h>
49 #include <sys/mutex.h>
50
51 #include <dev/acpi/acpica.h>
52 #include <dev/acpi/acpireg.h>
53 #include <dev/acpi/acpivar.h>
54
55 #include <dev/sysmon/sysmonvar.h>
56
57 #define _COMPONENT ACPI_ACAD_COMPONENT
58 ACPI_MODULE_NAME ("acpi_acad")
59
60 struct acpiacad_softc {
61 struct acpi_devnode *sc_node;
62 struct sysmon_envsys *sc_sme;
63 struct sysmon_pswitch sc_smpsw;
64 envsys_data_t sc_sensor;
65 kmutex_t sc_mutex;
66 int sc_status;
67 };
68
69 static const char * const acad_hid[] = {
70 "ACPI0003",
71 NULL
72 };
73
74 static int acpiacad_match(device_t, cfdata_t, void *);
75 static void acpiacad_attach(device_t, device_t, void *);
76 static int acpiacad_detach(device_t, int);
77 static bool acpiacad_resume(device_t, const pmf_qual_t *);
78 static void acpiacad_get_status(void *);
79 static void acpiacad_notify_handler(ACPI_HANDLE, uint32_t, void *);
80 static void acpiacad_init_envsys(device_t);
81
82 CFATTACH_DECL_NEW(acpiacad, sizeof(struct acpiacad_softc),
83 acpiacad_match, acpiacad_attach, acpiacad_detach, NULL);
84
85 /*
86 * acpiacad_match:
87 *
88 * Autoconfiguration `match' routine.
89 */
90 static int
91 acpiacad_match(device_t parent, cfdata_t match, void *aux)
92 {
93 struct acpi_attach_args *aa = aux;
94
95 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
96 return 0;
97
98 return acpi_match_hid(aa->aa_node->ad_devinfo, acad_hid);
99 }
100
101 /*
102 * acpiacad_attach:
103 *
104 * Autoconfiguration `attach' routine.
105 */
106 static void
107 acpiacad_attach(device_t parent, device_t self, void *aux)
108 {
109 struct acpiacad_softc *sc = device_private(self);
110 struct acpi_attach_args *aa = aux;
111 ACPI_STATUS rv;
112
113 aprint_naive(": ACPI AC Adapter\n");
114 aprint_normal(": ACPI AC Adapter\n");
115
116 sc->sc_sme = NULL;
117 sc->sc_status = -1;
118 sc->sc_node = aa->aa_node;
119
120 mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
121
122 sc->sc_smpsw.smpsw_name = device_xname(self);
123 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_ACADAPTER;
124
125 (void)sysmon_pswitch_register(&sc->sc_smpsw);
126 (void)pmf_device_register(self, NULL, acpiacad_resume);
127
128 rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
129 ACPI_ALL_NOTIFY, acpiacad_notify_handler, self);
130
131 if (ACPI_SUCCESS(rv))
132 acpiacad_init_envsys(self);
133 }
134
135 /*
136 * acpiacad_detach:
137 *
138 * Autoconfiguration `detach' routine.
139 */
140 static int
141 acpiacad_detach(device_t self, int flags)
142 {
143 struct acpiacad_softc *sc = device_private(self);
144 ACPI_STATUS rv;
145
146 rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle,
147 ACPI_ALL_NOTIFY, acpiacad_notify_handler);
148
149 if (ACPI_FAILURE(rv))
150 return EBUSY;
151
152 mutex_destroy(&sc->sc_mutex);
153
154 if (sc->sc_sme != NULL)
155 sysmon_envsys_unregister(sc->sc_sme);
156
157 pmf_device_deregister(self);
158 sysmon_pswitch_unregister(&sc->sc_smpsw);
159
160 return 0;
161 }
162
163 /*
164 * acpiacad_resume:
165 *
166 * Queue a new status check.
167 */
168 static bool
169 acpiacad_resume(device_t dv, const pmf_qual_t *qual)
170 {
171
172 (void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpiacad_get_status, dv);
173
174 return true;
175 }
176
177 /*
178 * acpiacad_get_status:
179 *
180 * Get, and possibly display, the current AC line status.
181 */
182 static void
183 acpiacad_get_status(void *arg)
184 {
185 device_t dv = arg;
186 struct acpiacad_softc *sc = device_private(dv);
187 ACPI_INTEGER status;
188 ACPI_STATUS rv;
189
190 mutex_enter(&sc->sc_mutex);
191
192 rv = acpi_eval_integer(sc->sc_node->ad_handle, "_PSR", &status);
193
194 if (ACPI_FAILURE(rv))
195 goto fail;
196
197 if (status != 0 && status != 1) {
198 rv = AE_BAD_VALUE;
199 goto fail;
200 }
201
202 if (sc->sc_status != status) {
203
204 /*
205 * If status has changed, send the event:
206 *
207 * PSWITCH_EVENT_PRESSED : _PSR = 1 : AC online.
208 * PSWITCH_EVENT_RELEASED : _PSR = 0 : AC offline.
209 */
210 sysmon_pswitch_event(&sc->sc_smpsw, (status != 0) ?
211 PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
212
213 aprint_debug_dev(dv, "AC adapter %sconnected\n",
214 status == 0 ? "not " : "");
215 }
216
217 sc->sc_status = status;
218 sc->sc_sensor.state = ENVSYS_SVALID;
219 sc->sc_sensor.value_cur = sc->sc_status;
220
221 mutex_exit(&sc->sc_mutex);
222
223 return;
224
225 fail:
226 sc->sc_status = -1;
227 sc->sc_sensor.state = ENVSYS_SINVALID;
228
229 aprint_debug_dev(dv, "failed to evaluate _PSR: %s\n",
230 AcpiFormatException(rv));
231
232 mutex_exit(&sc->sc_mutex);
233 }
234
235 /*
236 * acpiacad_notify_handler:
237 *
238 * Callback from ACPI interrupt handler to notify us of an event.
239 */
240 static void
241 acpiacad_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context)
242 {
243 static const int handler = OSL_NOTIFY_HANDLER;
244 device_t dv = context;
245
246 switch (notify) {
247 /*
248 * XXX So, BusCheck is not exactly what I would expect,
249 * but at least my IBM T21 sends it on AC adapter status
250 * change. --thorpej (at) wasabisystems.com
251 */
252 /*
253 * XXX My Acer TravelMate 291 sends DeviceCheck on AC
254 * adapter status change.
255 * --rpaulo (at) NetBSD.org
256 */
257 /*
258 * XXX Sony VAIO VGN-N250E sends BatteryInformationChanged on AC
259 * adapter status change.
260 * --jmcneill (at) NetBSD.org
261 */
262 case ACPI_NOTIFY_BusCheck:
263 case ACPI_NOTIFY_DeviceCheck:
264 case ACPI_NOTIFY_PowerSourceStatusChanged:
265 case ACPI_NOTIFY_BatteryInformationChanged:
266 (void)AcpiOsExecute(handler, acpiacad_get_status, dv);
267 break;
268
269 default:
270 aprint_error_dev(dv, "unknown notify 0x%02X\n", notify);
271 }
272 }
273
274 static void
275 acpiacad_init_envsys(device_t dv)
276 {
277 struct acpiacad_softc *sc = device_private(dv);
278
279 sc->sc_sme = sysmon_envsys_create();
280
281 sc->sc_sensor.state = ENVSYS_SINVALID;
282 sc->sc_sensor.units = ENVSYS_INDICATOR;
283
284 (void)strlcpy(sc->sc_sensor.desc, "connected", ENVSYS_DESCLEN);
285
286 if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor) != 0)
287 goto fail;
288
289 sc->sc_sme->sme_name = device_xname(dv);
290 sc->sc_sme->sme_class = SME_CLASS_ACADAPTER;
291 sc->sc_sme->sme_flags = SME_DISABLE_REFRESH;
292
293 if (sysmon_envsys_register(sc->sc_sme) != 0)
294 goto fail;
295
296 (void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpiacad_get_status, dv);
297
298 return;
299
300 fail:
301 aprint_error_dev(dv, "failed to initialize sysmon\n");
302 sysmon_envsys_destroy(sc->sc_sme);
303 sc->sc_sme = NULL;
304 }
305
306 #ifdef _MODULE
307
308 MODULE(MODULE_CLASS_DRIVER, acpiacad, NULL);
309 CFDRIVER_DECL(acpiacad, DV_DULL, NULL);
310
311 static int acpiacadloc[] = { -1 };
312 extern struct cfattach acpiacad_ca;
313
314 static struct cfparent acpiparent = {
315 "acpinodebus", NULL, DVUNIT_ANY
316 };
317
318 static struct cfdata acpiacad_cfdata[] = {
319 {
320 .cf_name = "acpiacad",
321 .cf_atname = "acpiacad",
322 .cf_unit = 0,
323 .cf_fstate = FSTATE_STAR,
324 .cf_loc = acpiacadloc,
325 .cf_flags = 0,
326 .cf_pspec = &acpiparent,
327 },
328
329 { NULL }
330 };
331
332 static int
333 acpiacad_modcmd(modcmd_t cmd, void *context)
334 {
335 int err;
336
337 switch (cmd) {
338
339 case MODULE_CMD_INIT:
340
341 err = config_cfdriver_attach(&acpiacad_cd);
342
343 if (err != 0)
344 return err;
345
346 err = config_cfattach_attach("acpiacad", &acpiacad_ca);
347
348 if (err != 0) {
349 config_cfdriver_detach(&acpiacad_cd);
350 return err;
351 }
352
353 err = config_cfdata_attach(acpiacad_cfdata, 1);
354
355 if (err != 0) {
356 config_cfattach_detach("acpiacad", &acpiacad_ca);
357 config_cfdriver_detach(&acpiacad_cd);
358 return err;
359 }
360
361 return 0;
362
363 case MODULE_CMD_FINI:
364
365 err = config_cfdata_detach(acpiacad_cfdata);
366
367 if (err != 0)
368 return err;
369
370 config_cfattach_detach("acpiacad", &acpiacad_ca);
371 config_cfdriver_detach(&acpiacad_cd);
372
373 return 0;
374
375 default:
376 return ENOTTY;
377 }
378 }
379
380 #endif /* _MODULE */
381