acpi_acad.c revision 1.26 1 /* $NetBSD: acpi_acad.c,v 1.26 2007/10/10 23:25:40 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.26 2007/10/10 23:25:40 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 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 acpiacad_init_envsys(sc);
158 }
159
160 /*
161 * acpiacad_get_status:
162 *
163 * Get, and possibly display, the current AC line status.
164 */
165 static void
166 acpiacad_get_status(void *arg)
167 {
168 struct acpiacad_softc *sc = arg;
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 printf("%s: AC adapter %sconnected\n",
200 sc->sc_dev.dv_xname, 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 struct acpiacad_softc *sc = context;
225 int rv;
226
227 switch (notify) {
228 /*
229 * XXX So, BusCheck is not exactly what I would expect,
230 * but at least my IBM T21 sends it on AC adapter status
231 * change. --thorpej (at) wasabisystems.com
232 */
233 /*
234 * XXX My Acer TravelMate 291 sends DeviceCheck on AC
235 * adapter status change.
236 * --rpaulo (at) NetBSD.org
237 */
238 /*
239 * XXX Sony VAIO VGN-N250E sends BatteryInformationChanged on AC
240 * adapter status change.
241 * --jmcneill (at) NetBSD.org
242 */
243 case ACPI_NOTIFY_BusCheck:
244 case ACPI_NOTIFY_DeviceCheck:
245 case ACPI_NOTIFY_PowerSourceStatusChanged:
246 case ACPI_NOTIFY_BatteryInformationChanged:
247 mutex_enter(&sc->sc_mtx);
248 acpiacad_clear_status(sc);
249 mutex_exit(&sc->sc_mtx);
250 if (sc->sc_status == -1 || !sc->sc_notifysent) {
251 rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
252 acpiacad_get_status, sc);
253 if (ACPI_FAILURE(rv))
254 printf("%s: unable to queue status check: %s\n",
255 sc->sc_dev.dv_xname,
256 AcpiFormatException(rv));
257 sc->sc_notifysent = 1;
258 #ifdef ACPI_ACAD_DEBUG
259 printf("%s: received notify message: 0x%x\n",
260 sc->sc_dev.dv_xname, notify);
261 #endif
262 }
263 break;
264
265 default:
266 printf("%s: received unknown notify message: 0x%x\n",
267 sc->sc_dev.dv_xname, notify);
268 }
269 }
270
271 static void
272 acpiacad_init_envsys(struct acpiacad_softc *sc)
273 {
274 sc->sc_data[0].sensor = 0;
275 sc->sc_data[0].state = ENVSYS_SVALID;
276 sc->sc_data[0].units = ENVSYS_INDICATOR;
277 snprintf(sc->sc_data[0].desc, sizeof(sc->sc_data->desc),
278 "%s %s", sc->sc_dev.dv_xname, "connected");
279
280 sc->sc_sysmon.sme_sensor_data = sc->sc_data;
281 sc->sc_sysmon.sme_name = sc->sc_dev.dv_xname;
282 sc->sc_sysmon.sme_cookie = sc;
283 sc->sc_sysmon.sme_gtredata = acpiacad_gtredata;
284 sc->sc_sysmon.sme_nsensors = 1;
285 sc->sc_sysmon.sme_class = SME_CLASS_ACADAPTER;
286
287 if (sysmon_envsys_register(&sc->sc_sysmon))
288 aprint_error("%s: unable to register with sysmon\n",
289 sc->sc_dev.dv_xname);
290 }
291
292 static int
293 acpiacad_gtredata(struct sysmon_envsys *sme, envsys_data_t *edata)
294 {
295 struct acpiacad_softc *sc = sme->sme_cookie;
296
297 if (!AACAD_ISSET(sc, AACAD_F_AVAILABLE))
298 acpiacad_get_status(sc);
299
300 return 0;
301 }
302