acpi_acad.c revision 1.25.6.4 1 /* $NetBSD: acpi_acad.c,v 1.25.6.4 2007/11/04 21:03:21 jmcneill 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.25.6.4 2007/11/04 21:03:21 jmcneill 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 struct acpiacad_softc {
61 struct acpi_devnode *sc_node; /* our ACPI devnode */
62 int sc_flags; /* see below */
63 int sc_status; /* status changed/not changed */
64 int sc_notifysent; /* notify message sent */
65
66 struct sysmon_envsys sc_sysmon;
67 struct sysmon_pswitch sc_smpsw; /* our sysmon glue */
68 struct envsys_data sc_data[1];
69
70 kmutex_t sc_mtx;
71 };
72
73 static const char * const acad_hid[] = {
74 "ACPI0003",
75 NULL
76 };
77
78 #define AACAD_F_VERBOSE 0x01 /* verbose events */
79 #define AACAD_F_AVAILABLE 0x02 /* information is available */
80 #define AACAD_F_STCHANGED 0x04 /* status changed */
81
82 #define AACAD_SET(sc, f) (void)((sc)->sc_flags |= (f))
83 #define AACAD_CLEAR(sc, f) (void)((sc)->sc_flags &= ~(f))
84 #define AACAD_ISSET(sc, f) ((sc)->sc_flags & (f))
85
86 static int acpiacad_match(device_t, struct cfdata *, void *);
87 static void acpiacad_attach(device_t, device_t, void *);
88
89 CFATTACH_DECL_NEW(acpiacad, sizeof(struct acpiacad_softc),
90 acpiacad_match, acpiacad_attach, NULL, NULL);
91
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 int acpiacad_gtredata(struct sysmon_envsys *, envsys_data_t *);
97
98 /*
99 * acpiacad_match:
100 *
101 * Autoconfiguration `match' routine.
102 */
103 static int
104 acpiacad_match(device_t parent, struct cfdata *match, void *aux)
105 {
106 struct acpi_attach_args *aa = aux;
107
108 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
109 return 0;
110
111 return acpi_match_hid(aa->aa_node->ad_devinfo, acad_hid);
112 }
113
114 /*
115 * acpiacad_attach:
116 *
117 * Autoconfiguration `attach' routine.
118 */
119 static void
120 acpiacad_attach(device_t parent, device_t self, void *aux)
121 {
122 struct acpiacad_softc *sc = device_private(self);
123 struct acpi_attach_args *aa = aux;
124 ACPI_STATUS rv;
125
126 aprint_naive(": ACPI AC Adapter\n");
127 aprint_normal(": ACPI AC Adapter\n");
128
129 sc->sc_node = aa->aa_node;
130 mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_NONE);
131
132 sc->sc_smpsw.smpsw_name = device_xname(self);
133 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_ACADAPTER;
134 if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
135 aprint_error_dev(self, "unable to register with sysmon\n");
136 return;
137 }
138
139 sc->sc_status = -1;
140
141 rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
142 ACPI_ALL_NOTIFY, acpiacad_notify_handler, self);
143 if (ACPI_FAILURE(rv)) {
144 aprint_error_dev(self, "unable to register DEVICE and SYSTEM "
145 "NOTIFY handler: %s\n", AcpiFormatException(rv));
146 return;
147 }
148
149 #ifdef ACPI_ACAD_DEBUG
150 /* Display the current state. */
151 sc->sc_flags = AACAD_F_VERBOSE;
152 #endif
153
154 (void)pnp_register(self, pnp_generic_power);
155
156 acpiacad_init_envsys(self);
157 }
158
159 /*
160 * acpiacad_get_status:
161 *
162 * Get, and possibly display, the current AC line status.
163 */
164 static void
165 acpiacad_get_status(void *arg)
166 {
167 device_t dv = arg;
168 struct acpiacad_softc *sc = device_private(dv);
169 ACPI_INTEGER status;
170 ACPI_STATUS rv;
171
172 rv = acpi_eval_integer(sc->sc_node->ad_handle, "_PSR", &status);
173 if (ACPI_FAILURE(rv))
174 return;
175
176 mutex_enter(&sc->sc_mtx);
177 if (sc->sc_status != status) {
178 sc->sc_status = status;
179 if (status)
180 sc->sc_data[0].value_cur = 1;
181 else
182 sc->sc_data[0].value_cur = 0;
183 AACAD_SET(sc, AACAD_F_STCHANGED);
184 sc->sc_notifysent = 0;
185 }
186
187 sc->sc_data[0].state = ENVSYS_SVALID;
188 AACAD_SET(sc, AACAD_F_AVAILABLE);
189 /*
190 * If status has changed, send the event.
191 *
192 * PSWITCH_EVENT_RELEASED : AC offline
193 * PSWITCH_EVENT_PRESSED : AC online
194 */
195 if (AACAD_ISSET(sc, AACAD_F_STCHANGED)) {
196 sysmon_pswitch_event(&sc->sc_smpsw, status ?
197 PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
198 if (AACAD_ISSET(sc, AACAD_F_VERBOSE))
199 aprint_verbose_dev(dv, "AC adapter %sconnected\n",
200 status == 0 ? "not " : "");
201 }
202 mutex_exit(&sc->sc_mtx);
203 }
204
205 /*
206 * Clear status
207 */
208 static void
209 acpiacad_clear_status(struct acpiacad_softc *sc)
210 {
211 sc->sc_data[0].state = ENVSYS_SINVALID;
212 AACAD_CLEAR(sc, AACAD_F_AVAILABLE);
213 AACAD_CLEAR(sc, AACAD_F_STCHANGED);
214 }
215
216 /*
217 * acpiacad_notify_handler:
218 *
219 * Callback from ACPI interrupt handler to notify us of an event.
220 */
221 static void
222 acpiacad_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
223 {
224 device_t dv = context;
225 struct acpiacad_softc *sc = device_private(dv);
226 int rv;
227
228 switch (notify) {
229 /*
230 * XXX So, BusCheck is not exactly what I would expect,
231 * but at least my IBM T21 sends it on AC adapter status
232 * change. --thorpej (at) wasabisystems.com
233 */
234 /*
235 * XXX My Acer TravelMate 291 sends DeviceCheck on AC
236 * adapter status change.
237 * --rpaulo (at) NetBSD.org
238 */
239 /*
240 * XXX Sony VAIO VGN-N250E sends BatteryInformationChanged on AC
241 * adapter status change.
242 * --jmcneill (at) NetBSD.org
243 */
244 case ACPI_NOTIFY_BusCheck:
245 case ACPI_NOTIFY_DeviceCheck:
246 case ACPI_NOTIFY_PowerSourceStatusChanged:
247 case ACPI_NOTIFY_BatteryInformationChanged:
248 mutex_enter(&sc->sc_mtx);
249 acpiacad_clear_status(sc);
250 mutex_exit(&sc->sc_mtx);
251 if (sc->sc_status == -1 || !sc->sc_notifysent) {
252 rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
253 acpiacad_get_status, dv);
254 if (ACPI_FAILURE(rv))
255 aprint_error_dev(dv,
256 "unable to queue status check: %s\n",
257 AcpiFormatException(rv));
258 sc->sc_notifysent = 1;
259 #ifdef ACPI_ACAD_DEBUG
260 aprint_debug_dev(dv, "received notify message: 0x%x\n",
261 notify);
262 #endif
263 }
264 break;
265
266 default:
267 aprint_error_dev(dv, "received unknown notify message: 0x%x\n",
268 notify);
269 }
270 }
271
272 static void
273 acpiacad_init_envsys(device_t dv)
274 {
275 struct acpiacad_softc *sc = device_private(dv);
276
277 sc->sc_data[0].sensor = 0;
278 sc->sc_data[0].state = ENVSYS_SVALID;
279 sc->sc_data[0].units = ENVSYS_INDICATOR;
280 strlcpy(sc->sc_data[0].desc, "connected", sizeof(sc->sc_data[0].desc));
281
282 sc->sc_sysmon.sme_sensor_data = sc->sc_data;
283 sc->sc_sysmon.sme_name = device_xname(dv);
284 sc->sc_sysmon.sme_cookie = dv;
285 sc->sc_sysmon.sme_gtredata = acpiacad_gtredata;
286 sc->sc_sysmon.sme_nsensors = 1;
287 sc->sc_sysmon.sme_class = SME_CLASS_ACADAPTER;
288
289 if (sysmon_envsys_register(&sc->sc_sysmon))
290 aprint_error_dev(dv, "unable to register with sysmon\n");
291 }
292
293 static int
294 acpiacad_gtredata(struct sysmon_envsys *sme, envsys_data_t *edata)
295 {
296 device_t dv = sme->sme_cookie;
297 struct acpiacad_softc *sc = device_private(dv);
298
299 if (!AACAD_ISSET(sc, AACAD_F_AVAILABLE))
300 acpiacad_get_status(dv);
301
302 return 0;
303 }
304