acpi_acad.c revision 1.1.4.5 1 1.1.4.5 thorpej /* $NetBSD: acpi_acad.c,v 1.1.4.5 2003/01/03 17:01:11 thorpej Exp $ */
2 1.1.4.2 nathanw
3 1.1.4.2 nathanw /*
4 1.1.4.2 nathanw * Copyright 2001 Wasabi Systems, Inc.
5 1.1.4.2 nathanw * All rights reserved.
6 1.1.4.2 nathanw *
7 1.1.4.2 nathanw * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.1.4.2 nathanw *
9 1.1.4.2 nathanw * Redistribution and use in source and binary forms, with or without
10 1.1.4.2 nathanw * modification, are permitted provided that the following conditions
11 1.1.4.2 nathanw * are met:
12 1.1.4.2 nathanw * 1. Redistributions of source code must retain the above copyright
13 1.1.4.2 nathanw * notice, this list of conditions and the following disclaimer.
14 1.1.4.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
15 1.1.4.2 nathanw * notice, this list of conditions and the following disclaimer in the
16 1.1.4.2 nathanw * documentation and/or other materials provided with the distribution.
17 1.1.4.2 nathanw * 3. All advertising materials mentioning features or use of this software
18 1.1.4.2 nathanw * must display the following acknowledgement:
19 1.1.4.2 nathanw * This product includes software developed for the NetBSD Project by
20 1.1.4.2 nathanw * Wasabi Systems, Inc.
21 1.1.4.2 nathanw * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1.4.2 nathanw * or promote products derived from this software without specific prior
23 1.1.4.2 nathanw * written permission.
24 1.1.4.2 nathanw *
25 1.1.4.2 nathanw * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1.4.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1.4.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1.4.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1.4.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1.4.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1.4.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1.4.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1.4.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1.4.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1.4.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
36 1.1.4.2 nathanw */
37 1.1.4.2 nathanw
38 1.1.4.2 nathanw /*
39 1.1.4.2 nathanw * ACPI AC Adapter driver.
40 1.1.4.2 nathanw */
41 1.1.4.2 nathanw
42 1.1.4.3 nathanw #include <sys/cdefs.h>
43 1.1.4.5 thorpej __KERNEL_RCSID(0, "$NetBSD: acpi_acad.c,v 1.1.4.5 2003/01/03 17:01:11 thorpej Exp $");
44 1.1.4.3 nathanw
45 1.1.4.2 nathanw #include <sys/param.h>
46 1.1.4.2 nathanw #include <sys/systm.h>
47 1.1.4.2 nathanw #include <sys/device.h>
48 1.1.4.2 nathanw
49 1.1.4.2 nathanw #include <dev/acpi/acpica.h>
50 1.1.4.2 nathanw #include <dev/acpi/acpireg.h>
51 1.1.4.2 nathanw #include <dev/acpi/acpivar.h>
52 1.1.4.2 nathanw
53 1.1.4.5 thorpej #include <dev/sysmon/sysmonvar.h>
54 1.1.4.5 thorpej
55 1.1.4.5 thorpej #define ACPIACAD_NSENSORS 2
56 1.1.4.5 thorpej
57 1.1.4.5 thorpej /* sensor indexes */
58 1.1.4.5 thorpej #define ACPIACAD_CONNECTED 0
59 1.1.4.5 thorpej #define ACPIACAD_DISCONNECTED 1
60 1.1.4.5 thorpej
61 1.1.4.2 nathanw struct acpiacad_softc {
62 1.1.4.2 nathanw struct device sc_dev; /* base device glue */
63 1.1.4.2 nathanw struct acpi_devnode *sc_node; /* our ACPI devnode */
64 1.1.4.2 nathanw int sc_flags; /* see below */
65 1.1.4.2 nathanw int sc_status; /* power status */
66 1.1.4.5 thorpej struct sysmon_envsys sc_sysmon;
67 1.1.4.5 thorpej struct envsys_basic_info sc_info[ACPIACAD_NSENSORS];
68 1.1.4.5 thorpej struct envsys_tre_data sc_data[ACPIACAD_NSENSORS];
69 1.1.4.5 thorpej };
70 1.1.4.5 thorpej
71 1.1.4.5 thorpej const struct envsys_range acpiacad_range[] = {
72 1.1.4.5 thorpej { 0, 2, ENVSYS_INDICATOR },
73 1.1.4.5 thorpej { 1, 0, -1},
74 1.1.4.2 nathanw };
75 1.1.4.2 nathanw
76 1.1.4.2 nathanw #define AACAD_F_VERBOSE 0x01 /* verbose events */
77 1.1.4.2 nathanw
78 1.1.4.2 nathanw int acpiacad_match(struct device *, struct cfdata *, void *);
79 1.1.4.2 nathanw void acpiacad_attach(struct device *, struct device *, void *);
80 1.1.4.2 nathanw
81 1.1.4.4 nathanw CFATTACH_DECL(acpiacad, sizeof(struct acpiacad_softc),
82 1.1.4.4 nathanw acpiacad_match, acpiacad_attach, NULL, NULL);
83 1.1.4.2 nathanw
84 1.1.4.2 nathanw void acpiacad_get_status(void *);
85 1.1.4.2 nathanw void acpiacad_notify_handler(ACPI_HANDLE, UINT32, void *context);
86 1.1.4.5 thorpej static void acpiacad_init_envsys(struct acpiacad_softc *sc);
87 1.1.4.5 thorpej static int acpiacad_gtredata(struct sysmon_envsys *, struct envsys_tre_data *);
88 1.1.4.5 thorpej static int acpiacad_streinfo(struct sysmon_envsys *, struct envsys_basic_info *);
89 1.1.4.2 nathanw
90 1.1.4.2 nathanw /*
91 1.1.4.2 nathanw * acpiacad_match:
92 1.1.4.2 nathanw *
93 1.1.4.2 nathanw * Autoconfiguration `match' routine.
94 1.1.4.2 nathanw */
95 1.1.4.2 nathanw int
96 1.1.4.2 nathanw acpiacad_match(struct device *parent, struct cfdata *match, void *aux)
97 1.1.4.2 nathanw {
98 1.1.4.2 nathanw struct acpi_attach_args *aa = aux;
99 1.1.4.2 nathanw
100 1.1.4.2 nathanw if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
101 1.1.4.2 nathanw return (0);
102 1.1.4.2 nathanw
103 1.1.4.2 nathanw if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "ACPI0003") == 0)
104 1.1.4.2 nathanw return (1);
105 1.1.4.2 nathanw
106 1.1.4.2 nathanw return (0);
107 1.1.4.2 nathanw }
108 1.1.4.2 nathanw
109 1.1.4.2 nathanw /*
110 1.1.4.2 nathanw * acpiacad_attach:
111 1.1.4.2 nathanw *
112 1.1.4.2 nathanw * Autoconfiguration `attach' routine.
113 1.1.4.2 nathanw */
114 1.1.4.2 nathanw void
115 1.1.4.2 nathanw acpiacad_attach(struct device *parent, struct device *self, void *aux)
116 1.1.4.2 nathanw {
117 1.1.4.2 nathanw struct acpiacad_softc *sc = (void *) self;
118 1.1.4.2 nathanw struct acpi_attach_args *aa = aux;
119 1.1.4.2 nathanw ACPI_STATUS rv;
120 1.1.4.2 nathanw
121 1.1.4.2 nathanw printf(": ACPI AC Adapter\n");
122 1.1.4.2 nathanw
123 1.1.4.2 nathanw sc->sc_node = aa->aa_node;
124 1.1.4.2 nathanw
125 1.1.4.2 nathanw rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
126 1.1.4.2 nathanw ACPI_DEVICE_NOTIFY, acpiacad_notify_handler, sc);
127 1.1.4.2 nathanw if (rv != AE_OK) {
128 1.1.4.2 nathanw printf("%s: unable to register DEVICE NOTIFY handler: %d\n",
129 1.1.4.2 nathanw sc->sc_dev.dv_xname, rv);
130 1.1.4.2 nathanw return;
131 1.1.4.2 nathanw }
132 1.1.4.2 nathanw
133 1.1.4.2 nathanw /* XXX See acpiacad_notify_handler() */
134 1.1.4.2 nathanw rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
135 1.1.4.2 nathanw ACPI_SYSTEM_NOTIFY, acpiacad_notify_handler, sc);
136 1.1.4.2 nathanw if (rv != AE_OK) {
137 1.1.4.2 nathanw printf("%s: unable to register SYSTEM NOTIFY handler: %d\n",
138 1.1.4.2 nathanw sc->sc_dev.dv_xname, rv);
139 1.1.4.2 nathanw return;
140 1.1.4.2 nathanw }
141 1.1.4.2 nathanw
142 1.1.4.2 nathanw /* Display the current state. */
143 1.1.4.2 nathanw sc->sc_flags = AACAD_F_VERBOSE;
144 1.1.4.2 nathanw acpiacad_get_status(sc);
145 1.1.4.5 thorpej acpiacad_init_envsys(sc);
146 1.1.4.2 nathanw }
147 1.1.4.2 nathanw
148 1.1.4.2 nathanw /*
149 1.1.4.2 nathanw * acpiacad_get_status:
150 1.1.4.2 nathanw *
151 1.1.4.2 nathanw * Get, and possibly display, the current AC line status.
152 1.1.4.2 nathanw */
153 1.1.4.2 nathanw void
154 1.1.4.2 nathanw acpiacad_get_status(void *arg)
155 1.1.4.2 nathanw {
156 1.1.4.2 nathanw struct acpiacad_softc *sc = arg;
157 1.1.4.2 nathanw
158 1.1.4.2 nathanw if (acpi_eval_integer(sc->sc_node->ad_handle, "_PSR",
159 1.1.4.2 nathanw &sc->sc_status) != AE_OK)
160 1.1.4.2 nathanw return;
161 1.1.4.2 nathanw
162 1.1.4.5 thorpej sc->sc_data[ACPIACAD_CONNECTED].cur.data_s = !!(sc->sc_status);
163 1.1.4.5 thorpej sc->sc_data[ACPIACAD_DISCONNECTED].cur.data_s = !(sc->sc_status);
164 1.1.4.5 thorpej
165 1.1.4.2 nathanw if (sc->sc_flags & AACAD_F_VERBOSE)
166 1.1.4.2 nathanw printf("%s: AC adapter %sconnected\n",
167 1.1.4.2 nathanw sc->sc_dev.dv_xname, sc->sc_status == 0 ? "not " : "");
168 1.1.4.2 nathanw }
169 1.1.4.2 nathanw
170 1.1.4.2 nathanw /*
171 1.1.4.2 nathanw * acpiacad_notify_handler:
172 1.1.4.2 nathanw *
173 1.1.4.2 nathanw * Callback from ACPI interrupt handler to notify us of an event.
174 1.1.4.2 nathanw */
175 1.1.4.2 nathanw void
176 1.1.4.2 nathanw acpiacad_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
177 1.1.4.2 nathanw {
178 1.1.4.2 nathanw struct acpiacad_softc *sc = context;
179 1.1.4.2 nathanw int rv;
180 1.1.4.2 nathanw
181 1.1.4.2 nathanw switch (notify) {
182 1.1.4.2 nathanw /*
183 1.1.4.2 nathanw * XXX So, BusCheck is not exactly what I would expect,
184 1.1.4.2 nathanw * but at least my IBM T21 sends it on AC adapter status
185 1.1.4.2 nathanw * change. --thorpej (at) wasabisystems.com
186 1.1.4.2 nathanw */
187 1.1.4.2 nathanw case ACPI_NOTIFY_BusCheck:
188 1.1.4.2 nathanw case ACPI_NOTIFY_PowerSourceStatusChanged:
189 1.1.4.2 nathanw #ifdef ACPI_ACAD_DEBUG
190 1.1.4.2 nathanw printf("%s: received notify message: 0x%x\n",
191 1.1.4.2 nathanw sc->sc_dev.dv_xname, notify);
192 1.1.4.2 nathanw #endif
193 1.1.4.2 nathanw rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
194 1.1.4.2 nathanw acpiacad_get_status, sc);
195 1.1.4.2 nathanw if (rv != AE_OK)
196 1.1.4.2 nathanw printf("%s: unable to queue status check: %d\n",
197 1.1.4.2 nathanw sc->sc_dev.dv_xname, rv);
198 1.1.4.2 nathanw break;
199 1.1.4.2 nathanw
200 1.1.4.2 nathanw default:
201 1.1.4.2 nathanw printf("%s: received unknown notify message: 0x%x\n",
202 1.1.4.2 nathanw sc->sc_dev.dv_xname, notify);
203 1.1.4.2 nathanw }
204 1.1.4.2 nathanw }
205 1.1.4.5 thorpej
206 1.1.4.5 thorpej static void
207 1.1.4.5 thorpej acpiacad_init_envsys(struct acpiacad_softc *sc)
208 1.1.4.5 thorpej {
209 1.1.4.5 thorpej int i;
210 1.1.4.5 thorpej
211 1.1.4.5 thorpej sc->sc_sysmon.sme_ranges = acpiacad_range;
212 1.1.4.5 thorpej
213 1.1.4.5 thorpej for (i=0; i<ACPIACAD_NSENSORS; i++) {
214 1.1.4.5 thorpej sc->sc_data[i].sensor = sc->sc_info[i].sensor = i;
215 1.1.4.5 thorpej sc->sc_data[i].validflags |= (ENVSYS_FVALID | ENVSYS_FCURVALID);
216 1.1.4.5 thorpej sc->sc_info[i].validflags = ENVSYS_FVALID;
217 1.1.4.5 thorpej sc->sc_data[i].warnflags = 0;
218 1.1.4.5 thorpej }
219 1.1.4.5 thorpej
220 1.1.4.5 thorpej #define INITDATA(index, unit, string) \
221 1.1.4.5 thorpej sc->sc_data[index].units = unit; \
222 1.1.4.5 thorpej sc->sc_info[index].units = unit; \
223 1.1.4.5 thorpej snprintf(sc->sc_info[index].desc, sizeof(sc->sc_info->desc), \
224 1.1.4.5 thorpej "%s %s", sc->sc_dev.dv_xname, string); \
225 1.1.4.5 thorpej
226 1.1.4.5 thorpej INITDATA(ACPIACAD_CONNECTED, ENVSYS_INDICATOR, "connected");
227 1.1.4.5 thorpej INITDATA(ACPIACAD_DISCONNECTED, ENVSYS_INDICATOR, "disconnected");
228 1.1.4.5 thorpej
229 1.1.4.5 thorpej sc->sc_sysmon.sme_sensor_info = sc->sc_info;
230 1.1.4.5 thorpej sc->sc_sysmon.sme_sensor_data = sc->sc_data;
231 1.1.4.5 thorpej sc->sc_sysmon.sme_cookie = sc;
232 1.1.4.5 thorpej sc->sc_sysmon.sme_gtredata = acpiacad_gtredata;
233 1.1.4.5 thorpej sc->sc_sysmon.sme_streinfo = acpiacad_streinfo;
234 1.1.4.5 thorpej sc->sc_sysmon.sme_nsensors = ACPIACAD_NSENSORS;
235 1.1.4.5 thorpej sc->sc_sysmon.sme_envsys_version = 1000;
236 1.1.4.5 thorpej
237 1.1.4.5 thorpej if (sysmon_envsys_register(&sc->sc_sysmon))
238 1.1.4.5 thorpej printf("%s: unable to register with sysmon\n",
239 1.1.4.5 thorpej sc->sc_dev.dv_xname);
240 1.1.4.5 thorpej }
241 1.1.4.5 thorpej
242 1.1.4.5 thorpej
243 1.1.4.5 thorpej int
244 1.1.4.5 thorpej acpiacad_gtredata(struct sysmon_envsys *sme, struct envsys_tre_data *tred)
245 1.1.4.5 thorpej {
246 1.1.4.5 thorpej struct acpiacad_softc *sc = sme->sme_cookie;
247 1.1.4.5 thorpej
248 1.1.4.5 thorpej /* XXX locking */
249 1.1.4.5 thorpej *tred = sc->sc_data[tred->sensor];
250 1.1.4.5 thorpej /* XXX locking */
251 1.1.4.5 thorpej
252 1.1.4.5 thorpej return (0);
253 1.1.4.5 thorpej }
254 1.1.4.5 thorpej
255 1.1.4.5 thorpej
256 1.1.4.5 thorpej int
257 1.1.4.5 thorpej acpiacad_streinfo(struct sysmon_envsys *sme, struct envsys_basic_info *binfo)
258 1.1.4.5 thorpej {
259 1.1.4.5 thorpej
260 1.1.4.5 thorpej /* XXX Not implemented */
261 1.1.4.5 thorpej binfo->validflags = 0;
262 1.1.4.5 thorpej
263 1.1.4.5 thorpej return (0);
264 1.1.4.5 thorpej }
265