acpi_acad.c revision 1.23 1 1.23 xtraeme /* $NetBSD: acpi_acad.c,v 1.23 2007/07/01 07:37:12 xtraeme Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright 2001 Wasabi Systems, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.1 thorpej *
9 1.1 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1 thorpej * modification, are permitted provided that the following conditions
11 1.1 thorpej * are met:
12 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer.
14 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.1 thorpej * documentation and/or other materials provided with the distribution.
17 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.1 thorpej * must display the following acknowledgement:
19 1.1 thorpej * This product includes software developed for the NetBSD Project by
20 1.1 thorpej * Wasabi Systems, Inc.
21 1.1 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 thorpej * or promote products derived from this software without specific prior
23 1.1 thorpej * written permission.
24 1.1 thorpej *
25 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
36 1.1 thorpej */
37 1.1 thorpej
38 1.7 tshiozak #if 0
39 1.7 tshiozak #define ACPI_ACAD_DEBUG
40 1.7 tshiozak #endif
41 1.7 tshiozak
42 1.1 thorpej /*
43 1.1 thorpej * ACPI AC Adapter driver.
44 1.1 thorpej */
45 1.2 lukem
46 1.2 lukem #include <sys/cdefs.h>
47 1.23 xtraeme __KERNEL_RCSID(0, "$NetBSD: acpi_acad.c,v 1.23 2007/07/01 07:37:12 xtraeme Exp $");
48 1.1 thorpej
49 1.1 thorpej #include <sys/param.h>
50 1.1 thorpej #include <sys/systm.h>
51 1.1 thorpej #include <sys/device.h>
52 1.23 xtraeme #include <sys/mutex.h>
53 1.1 thorpej
54 1.1 thorpej #include <dev/acpi/acpica.h>
55 1.1 thorpej #include <dev/acpi/acpireg.h>
56 1.1 thorpej #include <dev/acpi/acpivar.h>
57 1.1 thorpej
58 1.6 explorer #include <dev/sysmon/sysmonvar.h>
59 1.6 explorer
60 1.6 explorer #define ACPIACAD_NSENSORS 2
61 1.6 explorer
62 1.6 explorer /* sensor indexes */
63 1.6 explorer #define ACPIACAD_CONNECTED 0
64 1.6 explorer #define ACPIACAD_DISCONNECTED 1
65 1.6 explorer
66 1.1 thorpej struct acpiacad_softc {
67 1.1 thorpej struct device sc_dev; /* base device glue */
68 1.1 thorpej struct acpi_devnode *sc_node; /* our ACPI devnode */
69 1.1 thorpej int sc_flags; /* see below */
70 1.23 xtraeme int sc_status; /* status changed/not changed */
71 1.23 xtraeme int sc_notifysent; /* notify message sent */
72 1.7 tshiozak
73 1.6 explorer struct sysmon_envsys sc_sysmon;
74 1.16 kochi struct sysmon_pswitch sc_smpsw; /* our sysmon glue */
75 1.23 xtraeme struct envsys_data sc_data[ACPIACAD_NSENSORS];
76 1.7 tshiozak
77 1.23 xtraeme kmutex_t sc_mtx;
78 1.1 thorpej };
79 1.1 thorpej
80 1.10 kochi static const char * const acad_hid[] = {
81 1.10 kochi "ACPI0003",
82 1.10 kochi NULL
83 1.10 kochi };
84 1.10 kochi
85 1.1 thorpej #define AACAD_F_VERBOSE 0x01 /* verbose events */
86 1.7 tshiozak #define AACAD_F_AVAILABLE 0x02 /* information is available */
87 1.23 xtraeme #define AACAD_F_STCHANGED 0x04 /* status changed */
88 1.7 tshiozak
89 1.7 tshiozak #define AACAD_SET(sc, f) (void)((sc)->sc_flags |= (f))
90 1.7 tshiozak #define AACAD_CLEAR(sc, f) (void)((sc)->sc_flags &= ~(f))
91 1.7 tshiozak #define AACAD_ISSET(sc, f) ((sc)->sc_flags & (f))
92 1.7 tshiozak
93 1.7 tshiozak static int acpiacad_match(struct device *, struct cfdata *, void *);
94 1.7 tshiozak static void acpiacad_attach(struct device *, struct device *, void *);
95 1.1 thorpej
96 1.4 thorpej CFATTACH_DECL(acpiacad, sizeof(struct acpiacad_softc),
97 1.5 thorpej acpiacad_match, acpiacad_attach, NULL, NULL);
98 1.1 thorpej
99 1.7 tshiozak static void acpiacad_get_status(void *);
100 1.7 tshiozak static void acpiacad_clear_status(struct acpiacad_softc *);
101 1.7 tshiozak static void acpiacad_notify_handler(ACPI_HANDLE, UINT32, void *);
102 1.7 tshiozak static void acpiacad_init_envsys(struct acpiacad_softc *);
103 1.23 xtraeme static int acpiacad_gtredata(struct sysmon_envsys *, envsys_data_t *);
104 1.1 thorpej
105 1.1 thorpej /*
106 1.1 thorpej * acpiacad_match:
107 1.1 thorpej *
108 1.1 thorpej * Autoconfiguration `match' routine.
109 1.1 thorpej */
110 1.15 kochi static int
111 1.23 xtraeme acpiacad_match(struct device *parent, struct cfdata *match, void *aux)
112 1.1 thorpej {
113 1.1 thorpej struct acpi_attach_args *aa = aux;
114 1.1 thorpej
115 1.1 thorpej if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
116 1.14 kochi return 0;
117 1.1 thorpej
118 1.14 kochi return acpi_match_hid(aa->aa_node->ad_devinfo, acad_hid);
119 1.1 thorpej }
120 1.1 thorpej
121 1.1 thorpej /*
122 1.1 thorpej * acpiacad_attach:
123 1.1 thorpej *
124 1.1 thorpej * Autoconfiguration `attach' routine.
125 1.1 thorpej */
126 1.15 kochi static void
127 1.21 christos acpiacad_attach(struct device *parent, struct device *self, void *aux)
128 1.1 thorpej {
129 1.1 thorpej struct acpiacad_softc *sc = (void *) self;
130 1.1 thorpej struct acpi_attach_args *aa = aux;
131 1.1 thorpej ACPI_STATUS rv;
132 1.1 thorpej
133 1.18 kochi aprint_naive(": ACPI AC Adapter\n");
134 1.18 kochi aprint_normal(": ACPI AC Adapter\n");
135 1.1 thorpej
136 1.1 thorpej sc->sc_node = aa->aa_node;
137 1.23 xtraeme mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_NONE);
138 1.1 thorpej
139 1.16 kochi sc->sc_smpsw.smpsw_name = sc->sc_dev.dv_xname;
140 1.16 kochi sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_ACADAPTER;
141 1.16 kochi if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
142 1.18 kochi aprint_error("%s: unable to register with sysmon\n",
143 1.16 kochi sc->sc_dev.dv_xname);
144 1.16 kochi return;
145 1.16 kochi }
146 1.16 kochi
147 1.23 xtraeme sc->sc_status = -1;
148 1.1 thorpej
149 1.1 thorpej rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
150 1.23 xtraeme ACPI_ALL_NOTIFY, acpiacad_notify_handler, sc);
151 1.12 mycroft if (ACPI_FAILURE(rv)) {
152 1.23 xtraeme aprint_error("%s: unable to register DEVICE and SYSTEM "
153 1.23 xtraeme "NOTIFY handler: %s\n", sc->sc_dev.dv_xname,
154 1.23 xtraeme AcpiFormatException(rv));
155 1.1 thorpej return;
156 1.1 thorpej }
157 1.1 thorpej
158 1.7 tshiozak #ifdef ACPI_ACAD_DEBUG
159 1.1 thorpej /* Display the current state. */
160 1.1 thorpej sc->sc_flags = AACAD_F_VERBOSE;
161 1.7 tshiozak #endif
162 1.7 tshiozak
163 1.6 explorer acpiacad_init_envsys(sc);
164 1.1 thorpej }
165 1.1 thorpej
166 1.1 thorpej /*
167 1.1 thorpej * acpiacad_get_status:
168 1.1 thorpej *
169 1.1 thorpej * Get, and possibly display, the current AC line status.
170 1.1 thorpej */
171 1.15 kochi static void
172 1.1 thorpej acpiacad_get_status(void *arg)
173 1.1 thorpej {
174 1.1 thorpej struct acpiacad_softc *sc = arg;
175 1.13 kanaoka ACPI_INTEGER status;
176 1.12 mycroft ACPI_STATUS rv;
177 1.1 thorpej
178 1.12 mycroft rv = acpi_eval_integer(sc->sc_node->ad_handle, "_PSR", &status);
179 1.12 mycroft if (ACPI_FAILURE(rv))
180 1.1 thorpej return;
181 1.1 thorpej
182 1.23 xtraeme mutex_enter(&sc->sc_mtx);
183 1.23 xtraeme if (sc->sc_status != status) {
184 1.23 xtraeme sc->sc_status = status;
185 1.23 xtraeme if (status) {
186 1.23 xtraeme sc->sc_data[ACPIACAD_CONNECTED].state = ENVSYS_SVALID;
187 1.23 xtraeme sc->sc_data[ACPIACAD_CONNECTED].value_cur = 1;
188 1.23 xtraeme } else {
189 1.23 xtraeme sc->sc_data[ACPIACAD_DISCONNECTED].state =
190 1.23 xtraeme ENVSYS_SVALID;
191 1.23 xtraeme sc->sc_data[ACPIACAD_DISCONNECTED].value_cur = 1;
192 1.23 xtraeme }
193 1.23 xtraeme AACAD_SET(sc, AACAD_F_STCHANGED);
194 1.23 xtraeme sc->sc_notifysent = 0;
195 1.23 xtraeme }
196 1.23 xtraeme
197 1.7 tshiozak AACAD_SET(sc, AACAD_F_AVAILABLE);
198 1.16 kochi /*
199 1.23 xtraeme * If status has changed, send the event.
200 1.23 xtraeme *
201 1.16 kochi * PSWITCH_EVENT_RELEASED : AC offline
202 1.16 kochi * PSWITCH_EVENT_PRESSED : AC online
203 1.16 kochi */
204 1.23 xtraeme if (AACAD_ISSET(sc, AACAD_F_STCHANGED)) {
205 1.23 xtraeme sysmon_pswitch_event(&sc->sc_smpsw, status ?
206 1.23 xtraeme PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
207 1.23 xtraeme if (AACAD_ISSET(sc, AACAD_F_VERBOSE))
208 1.23 xtraeme printf("%s: AC adapter %sconnected\n",
209 1.23 xtraeme sc->sc_dev.dv_xname, status == 0 ? "not " : "");
210 1.23 xtraeme }
211 1.23 xtraeme mutex_exit(&sc->sc_mtx);
212 1.7 tshiozak }
213 1.7 tshiozak
214 1.7 tshiozak /*
215 1.7 tshiozak * Clear status
216 1.7 tshiozak */
217 1.15 kochi static void
218 1.7 tshiozak acpiacad_clear_status(struct acpiacad_softc *sc)
219 1.7 tshiozak {
220 1.23 xtraeme sc->sc_data[ACPIACAD_CONNECTED].state = ENVSYS_SINVALID;
221 1.23 xtraeme sc->sc_data[ACPIACAD_DISCONNECTED].state = ENVSYS_SINVALID;
222 1.7 tshiozak AACAD_CLEAR(sc, AACAD_F_AVAILABLE);
223 1.23 xtraeme AACAD_CLEAR(sc, AACAD_F_STCHANGED);
224 1.1 thorpej }
225 1.1 thorpej
226 1.1 thorpej /*
227 1.1 thorpej * acpiacad_notify_handler:
228 1.1 thorpej *
229 1.1 thorpej * Callback from ACPI interrupt handler to notify us of an event.
230 1.1 thorpej */
231 1.15 kochi static void
232 1.23 xtraeme acpiacad_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
233 1.1 thorpej {
234 1.1 thorpej struct acpiacad_softc *sc = context;
235 1.23 xtraeme int rv;
236 1.1 thorpej
237 1.1 thorpej switch (notify) {
238 1.1 thorpej /*
239 1.1 thorpej * XXX So, BusCheck is not exactly what I would expect,
240 1.1 thorpej * but at least my IBM T21 sends it on AC adapter status
241 1.1 thorpej * change. --thorpej (at) wasabisystems.com
242 1.1 thorpej */
243 1.19 rpaulo /*
244 1.19 rpaulo * XXX My Acer TravelMate 291 sends DeviceCheck on AC
245 1.19 rpaulo * adapter status change.
246 1.19 rpaulo * --rpaulo (at) NetBSD.org
247 1.19 rpaulo */
248 1.22 jmcneill /*
249 1.22 jmcneill * XXX Sony VAIO VGN-N250E sends BatteryInformationChanged on AC
250 1.22 jmcneill * adapter status change.
251 1.22 jmcneill * --jmcneill (at) NetBSD.org
252 1.22 jmcneill */
253 1.1 thorpej case ACPI_NOTIFY_BusCheck:
254 1.19 rpaulo case ACPI_NOTIFY_DeviceCheck:
255 1.1 thorpej case ACPI_NOTIFY_PowerSourceStatusChanged:
256 1.22 jmcneill case ACPI_NOTIFY_BatteryInformationChanged:
257 1.23 xtraeme mutex_enter(&sc->sc_mtx);
258 1.23 xtraeme acpiacad_clear_status(sc);
259 1.23 xtraeme mutex_exit(&sc->sc_mtx);
260 1.23 xtraeme if (sc->sc_status == -1 || !sc->sc_notifysent) {
261 1.23 xtraeme rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
262 1.23 xtraeme acpiacad_get_status, sc);
263 1.23 xtraeme if (ACPI_FAILURE(rv))
264 1.23 xtraeme printf("%s: unable to queue status check: %s\n",
265 1.23 xtraeme sc->sc_dev.dv_xname,
266 1.23 xtraeme AcpiFormatException(rv));
267 1.23 xtraeme sc->sc_notifysent = 1;
268 1.1 thorpej #ifdef ACPI_ACAD_DEBUG
269 1.23 xtraeme printf("%s: received notify message: 0x%x\n",
270 1.23 xtraeme sc->sc_dev.dv_xname, notify);
271 1.1 thorpej #endif
272 1.23 xtraeme }
273 1.1 thorpej break;
274 1.1 thorpej
275 1.1 thorpej default:
276 1.1 thorpej printf("%s: received unknown notify message: 0x%x\n",
277 1.1 thorpej sc->sc_dev.dv_xname, notify);
278 1.1 thorpej }
279 1.6 explorer }
280 1.6 explorer
281 1.15 kochi static void
282 1.6 explorer acpiacad_init_envsys(struct acpiacad_softc *sc)
283 1.6 explorer {
284 1.6 explorer int i;
285 1.6 explorer
286 1.23 xtraeme for (i = 0; i < ACPIACAD_NSENSORS; i++) {
287 1.23 xtraeme sc->sc_data[i].sensor = i;
288 1.23 xtraeme sc->sc_data[i].state = ENVSYS_SVALID;
289 1.6 explorer }
290 1.6 explorer
291 1.6 explorer #define INITDATA(index, unit, string) \
292 1.6 explorer sc->sc_data[index].units = unit; \
293 1.23 xtraeme snprintf(sc->sc_data[index].desc, sizeof(sc->sc_data->desc), \
294 1.6 explorer "%s %s", sc->sc_dev.dv_xname, string); \
295 1.6 explorer
296 1.6 explorer INITDATA(ACPIACAD_CONNECTED, ENVSYS_INDICATOR, "connected");
297 1.6 explorer INITDATA(ACPIACAD_DISCONNECTED, ENVSYS_INDICATOR, "disconnected");
298 1.6 explorer
299 1.6 explorer sc->sc_sysmon.sme_sensor_data = sc->sc_data;
300 1.23 xtraeme sc->sc_sysmon.sme_name = sc->sc_dev.dv_xname;
301 1.6 explorer sc->sc_sysmon.sme_cookie = sc;
302 1.6 explorer sc->sc_sysmon.sme_gtredata = acpiacad_gtredata;
303 1.6 explorer sc->sc_sysmon.sme_nsensors = ACPIACAD_NSENSORS;
304 1.6 explorer
305 1.6 explorer if (sysmon_envsys_register(&sc->sc_sysmon))
306 1.18 kochi aprint_error("%s: unable to register with sysmon\n",
307 1.6 explorer sc->sc_dev.dv_xname);
308 1.6 explorer }
309 1.6 explorer
310 1.15 kochi static int
311 1.23 xtraeme acpiacad_gtredata(struct sysmon_envsys *sme, envsys_data_t *edata)
312 1.6 explorer {
313 1.6 explorer struct acpiacad_softc *sc = sme->sme_cookie;
314 1.7 tshiozak
315 1.7 tshiozak if (!AACAD_ISSET(sc, AACAD_F_AVAILABLE))
316 1.7 tshiozak acpiacad_get_status(sc);
317 1.6 explorer
318 1.14 kochi return 0;
319 1.1 thorpej }
320