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