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