1/* $NetBSD: acpi_acad.c,v 1.51 2015/04/23 23:23:00 pgoyette 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/*
39 * ACPI AC Adapter driver.
40 */
41
42#include <sys/cdefs.h>
43__KERNEL_RCSID(0, "$NetBSD: acpi_acad.c,v 1.51 2015/04/23 23:23:00 pgoyette Exp $");
44
45#include <sys/param.h>
46#include <sys/device.h>
47#include <sys/module.h>
48#include <sys/systm.h>
49
50#include <dev/acpi/acpireg.h>
51#include <dev/acpi/acpivar.h>
52
53#define _COMPONENT ACPI_ACAD_COMPONENT
54ACPI_MODULE_NAME ("acpi_acad")
55
56#define ACPI_NOTIFY_ACAD 0x80
57#define ACPI_NOTIFY_ACAD_2 0x81 /* XXX. */
58
59struct acpiacad_softc {
60 struct acpi_devnode *sc_node;
61 struct sysmon_envsys *sc_sme;
62 struct sysmon_pswitch sc_smpsw;
63 envsys_data_t sc_sensor;
64 int sc_status;
65};
66
67static const char * const acad_hid[] = {
68 "ACPI0003",
69 NULL
70};
71
72static int acpiacad_match(device_t, cfdata_t, void *);
73static void acpiacad_attach(device_t, device_t, void *);
74static int acpiacad_detach(device_t, int);
75static bool acpiacad_resume(device_t, const pmf_qual_t *);
76static void acpiacad_get_status(void *);
77static void acpiacad_notify_handler(ACPI_HANDLE, uint32_t, void *);
78static void acpiacad_init_envsys(device_t);
79
80CFATTACH_DECL_NEW(acpiacad, sizeof(struct acpiacad_softc),
81 acpiacad_match, acpiacad_attach, acpiacad_detach, NULL);
82
83/*
84 * acpiacad_match:
85 *
86 * Autoconfiguration `match' routine.
87 */
88static int
89acpiacad_match(device_t parent, cfdata_t match, void *aux)
90{
91 struct acpi_attach_args *aa = aux;
92
93 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
94 return 0;
95
96 return acpi_match_hid(aa->aa_node->ad_devinfo, acad_hid);
97}
98
99/*
100 * acpiacad_attach:
101 *
102 * Autoconfiguration `attach' routine.
103 */
104static void
105acpiacad_attach(device_t parent, device_t self, void *aux)
106{
107 struct acpiacad_softc *sc = device_private(self);
108 struct acpi_attach_args *aa = aux;
109
110 aprint_naive(": ACPI AC Adapter\n");
111 aprint_normal(": ACPI AC Adapter\n");
112
113 sc->sc_sme = NULL;
114 sc->sc_status = -1;
115 sc->sc_node = aa->aa_node;
116
117 acpiacad_init_envsys(self);
118
119 sc->sc_smpsw.smpsw_name = device_xname(self);
120 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_ACADAPTER;
121
122 (void)sysmon_pswitch_register(&sc->sc_smpsw);
123 (void)pmf_device_register(self, NULL, acpiacad_resume);
124 (void)acpi_register_notify(sc->sc_node, acpiacad_notify_handler);
125}
126
127/*
128 * acpiacad_detach:
129 *
130 * Autoconfiguration `detach' routine.
131 */
132static int
133acpiacad_detach(device_t self, int flags)
134{
135 struct acpiacad_softc *sc = device_private(self);
136
137 acpi_deregister_notify(sc->sc_node);
138
139 if (sc->sc_sme != NULL)
140 sysmon_envsys_unregister(sc->sc_sme);
141
142 pmf_device_deregister(self);
143 sysmon_pswitch_unregister(&sc->sc_smpsw);
144
145 return 0;
146}
147
148/*
149 * acpiacad_resume:
150 *
151 * Queue a new status check.
152 */
153static bool
154acpiacad_resume(device_t dv, const pmf_qual_t *qual)
155{
156
157 (void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpiacad_get_status, dv);
158
159 return true;
160}
161
162/*
163 * acpiacad_get_status:
164 *
165 * Get, and possibly display, the current AC line status.
166 */
167static void
168acpiacad_get_status(void *arg)
169{
170 device_t dv = arg;
171 struct acpiacad_softc *sc = device_private(dv);
172 ACPI_INTEGER status;
173 ACPI_STATUS rv;
174
175 rv = acpi_eval_integer(sc->sc_node->ad_handle, "_PSR", &status);
176
177 if (ACPI_FAILURE(rv))
178 goto fail;
179
180 if (status != 0 && status != 1) {
181 rv = AE_BAD_VALUE;
182 goto fail;
183 }
184
185 if (sc->sc_status != (int)status) {
186
187 /*
188 * If status has changed, send the event:
189 *
190 * PSWITCH_EVENT_PRESSED : _PSR = 1 : AC online.
191 * PSWITCH_EVENT_RELEASED : _PSR = 0 : AC offline.
192 */
193 sysmon_pswitch_event(&sc->sc_smpsw, (status != 0) ?
194 PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
195
196 aprint_debug_dev(dv, "AC adapter %sconnected\n",
197 status == 0 ? "not " : "");
198 }
199
200 sc->sc_status = status;
201 sc->sc_sensor.state = ENVSYS_SVALID;
202 sc->sc_sensor.value_cur = sc->sc_status;
203
204 return;
205
206fail:
207 sc->sc_status = -1;
208 sc->sc_sensor.state = ENVSYS_SINVALID;
209
210 aprint_debug_dev(dv, "failed to evaluate _PSR: %s\n",
211 AcpiFormatException(rv));
212}
213
214/*
215 * acpiacad_notify_handler:
216 *
217 * Callback from ACPI interrupt handler to notify us of an event.
218 */
219static void
220acpiacad_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context)
221{
222 static const int handler = OSL_NOTIFY_HANDLER;
223 device_t dv = context;
224
225 switch (notify) {
226 /*
227 * XXX So, BusCheck is not exactly what I would expect,
228 * but at least my IBM T21 sends it on AC adapter status
229 * change. --thorpej@wasabisystems.com
230 */
231 /*
232 * XXX My Acer TravelMate 291 sends DeviceCheck on AC
233 * adapter status change.
234 * --rpaulo@NetBSD.org
235 */
236 /*
237 * XXX Sony VAIO VGN-N250E sends 0x81 on AC adapter status change.
238 * --jmcneill@NetBSD.org
239 */
240 case ACPI_NOTIFY_ACAD:
241 case ACPI_NOTIFY_ACAD_2:
242 case ACPI_NOTIFY_BUS_CHECK:
243 case ACPI_NOTIFY_DEVICE_CHECK:
244 (void)AcpiOsExecute(handler, acpiacad_get_status, dv);
245 break;
246
247 case ACPI_NOTIFY_DEVICE_WAKE:
248 break;
249
250 default:
251 aprint_debug_dev(dv, "unknown notify 0x%02X\n", notify);
252 }
253}
254
255static void
256acpiacad_init_envsys(device_t dv)
257{
258 struct acpiacad_softc *sc = device_private(dv);
259
260 sc->sc_sme = sysmon_envsys_create();
261
262 sc->sc_sensor.state = ENVSYS_SINVALID;
263 sc->sc_sensor.units = ENVSYS_INDICATOR;
264
265 (void)strlcpy(sc->sc_sensor.desc, "connected", ENVSYS_DESCLEN);
266
267 if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor) != 0)
268 goto fail;
269
270 sc->sc_sme->sme_name = device_xname(dv);
271 sc->sc_sme->sme_class = SME_CLASS_ACADAPTER;
272 sc->sc_sme->sme_flags = SME_DISABLE_REFRESH;
273
274 if (sysmon_envsys_register(sc->sc_sme) != 0)
275 goto fail;
276
277 (void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpiacad_get_status, dv);
278
279 return;
280
281fail:
282 aprint_error_dev(dv, "failed to initialize sysmon\n");
283
284 sysmon_envsys_destroy(sc->sc_sme);
285 sc->sc_sme = NULL;
286}
287
288MODULE(MODULE_CLASS_DRIVER, acpiacad, "sysmon_envsys,sysmon_power");
289
290#ifdef _MODULE
291#include "ioconf.c"
292#endif
293
294static int
295acpiacad_modcmd(modcmd_t cmd, void *aux)
296{
297 int rv = 0;
298
299 switch (cmd) {
300
301 case MODULE_CMD_INIT:
302
303#ifdef _MODULE
304 rv = config_init_component(cfdriver_ioconf_acpiacad,
305 cfattach_ioconf_acpiacad, cfdata_ioconf_acpiacad);
306#endif
307 break;
308
309 case MODULE_CMD_FINI:
310
311#ifdef _MODULE
312 rv = config_fini_component(cfdriver_ioconf_acpiacad,
313 cfattach_ioconf_acpiacad, cfdata_ioconf_acpiacad);
314#endif
315 break;
316
317 default:
318 rv = ENOTTY;
319 }
320
321 return rv;
322}
323