xref: /netbsd-src/sys/dev/acpi/acpi_acad.c (revision 62a8debe1dc62962e18a1c918def78666141273b)
1 /*	$NetBSD: acpi_acad.c,v 1.42 2010/03/05 14:00:16 jruoho 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.42 2010/03/05 14:00:16 jruoho Exp $");
44 
45 #include <sys/param.h>
46 #include <sys/device.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/systm.h>
50 
51 #include <dev/acpi/acpireg.h>
52 #include <dev/acpi/acpivar.h>
53 
54 #define _COMPONENT		 ACPI_ACAD_COMPONENT
55 ACPI_MODULE_NAME		 ("acpi_acad")
56 
57 struct acpiacad_softc {
58 	struct acpi_devnode	*sc_node;
59 	struct sysmon_envsys	*sc_sme;
60 	struct sysmon_pswitch	 sc_smpsw;
61 	envsys_data_t		 sc_sensor;
62 	kmutex_t		 sc_mutex;
63 	int			 sc_status;
64 };
65 
66 static const char * const acad_hid[] = {
67 	"ACPI0003",
68 	NULL
69 };
70 
71 static int	acpiacad_match(device_t, cfdata_t, void *);
72 static void	acpiacad_attach(device_t, device_t, void *);
73 static int	acpiacad_detach(device_t, int);
74 static bool	acpiacad_resume(device_t, const pmf_qual_t *);
75 static void	acpiacad_get_status(void *);
76 static void	acpiacad_notify_handler(ACPI_HANDLE, uint32_t, void *);
77 static void	acpiacad_init_envsys(device_t);
78 
79 CFATTACH_DECL_NEW(acpiacad, sizeof(struct acpiacad_softc),
80     acpiacad_match, acpiacad_attach, acpiacad_detach, NULL);
81 
82 /*
83  * acpiacad_match:
84  *
85  *	Autoconfiguration `match' routine.
86  */
87 static int
88 acpiacad_match(device_t parent, cfdata_t match, void *aux)
89 {
90 	struct acpi_attach_args *aa = aux;
91 
92 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
93 		return 0;
94 
95 	return acpi_match_hid(aa->aa_node->ad_devinfo, acad_hid);
96 }
97 
98 /*
99  * acpiacad_attach:
100  *
101  *	Autoconfiguration `attach' routine.
102  */
103 static void
104 acpiacad_attach(device_t parent, device_t self, void *aux)
105 {
106 	struct acpiacad_softc *sc = device_private(self);
107 	struct acpi_attach_args *aa = aux;
108 	ACPI_STATUS rv;
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 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
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 
125 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
126 	    ACPI_ALL_NOTIFY, acpiacad_notify_handler, self);
127 
128 	if (ACPI_SUCCESS(rv))
129 		acpiacad_init_envsys(self);
130 }
131 
132 /*
133  * acpiacad_detach:
134  *
135  *	Autoconfiguration `detach' routine.
136  */
137 static int
138 acpiacad_detach(device_t self, int flags)
139 {
140 	struct acpiacad_softc *sc = device_private(self);
141 	ACPI_STATUS rv;
142 
143 	rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle,
144 	    ACPI_ALL_NOTIFY, acpiacad_notify_handler);
145 
146 	if (ACPI_FAILURE(rv))
147 		return EBUSY;
148 
149 	mutex_destroy(&sc->sc_mutex);
150 
151 	if (sc->sc_sme != NULL)
152 		sysmon_envsys_unregister(sc->sc_sme);
153 
154 	pmf_device_deregister(self);
155 	sysmon_pswitch_unregister(&sc->sc_smpsw);
156 
157 	return 0;
158 }
159 
160 /*
161  * acpiacad_resume:
162  *
163  * 	Queue a new status check.
164  */
165 static bool
166 acpiacad_resume(device_t dv, const pmf_qual_t *qual)
167 {
168 
169 	(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpiacad_get_status, dv);
170 
171 	return true;
172 }
173 
174 /*
175  * acpiacad_get_status:
176  *
177  *	Get, and possibly display, the current AC line status.
178  */
179 static void
180 acpiacad_get_status(void *arg)
181 {
182 	device_t dv = arg;
183 	struct acpiacad_softc *sc = device_private(dv);
184 	ACPI_INTEGER status;
185 	ACPI_STATUS rv;
186 
187 	mutex_enter(&sc->sc_mutex);
188 
189 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_PSR", &status);
190 
191 	if (ACPI_FAILURE(rv))
192 		goto fail;
193 
194 	if (status != 0 && status != 1) {
195 		rv = AE_BAD_VALUE;
196 		goto fail;
197 	}
198 
199 	if (sc->sc_status != status) {
200 
201 		/*
202 		 * If status has changed, send the event:
203 		 *
204 		 * PSWITCH_EVENT_PRESSED  : _PSR = 1 : AC online.
205 		 * PSWITCH_EVENT_RELEASED : _PSR = 0 : AC offline.
206 		 */
207 		sysmon_pswitch_event(&sc->sc_smpsw, (status != 0) ?
208 	    	    PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
209 
210 		aprint_debug_dev(dv, "AC adapter %sconnected\n",
211 		    status == 0 ? "not " : "");
212 	}
213 
214 	sc->sc_status = status;
215 	sc->sc_sensor.state = ENVSYS_SVALID;
216 	sc->sc_sensor.value_cur = sc->sc_status;
217 
218 	mutex_exit(&sc->sc_mutex);
219 
220 	return;
221 
222 fail:
223 	sc->sc_status = -1;
224 	sc->sc_sensor.state = ENVSYS_SINVALID;
225 
226 	aprint_debug_dev(dv, "failed to evaluate _PSR: %s\n",
227 	    AcpiFormatException(rv));
228 
229 	mutex_exit(&sc->sc_mutex);
230 }
231 
232 /*
233  * acpiacad_notify_handler:
234  *
235  *	Callback from ACPI interrupt handler to notify us of an event.
236  */
237 static void
238 acpiacad_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context)
239 {
240 	static const int handler = OSL_NOTIFY_HANDLER;
241 	device_t dv = context;
242 
243 	switch (notify) {
244 	/*
245 	 * XXX So, BusCheck is not exactly what I would expect,
246 	 * but at least my IBM T21 sends it on AC adapter status
247 	 * change.  --thorpej@wasabisystems.com
248 	 */
249 	/*
250 	 * XXX My Acer TravelMate 291 sends DeviceCheck on AC
251 	 * adapter status change.
252 	 *  --rpaulo@NetBSD.org
253 	 */
254 	/*
255 	 * XXX Sony VAIO VGN-N250E sends BatteryInformationChanged on AC
256 	 * adapter status change.
257 	 *  --jmcneill@NetBSD.org
258 	 */
259 	case ACPI_NOTIFY_BusCheck:
260 	case ACPI_NOTIFY_DeviceCheck:
261 	case ACPI_NOTIFY_PowerSourceStatusChanged:
262 	case ACPI_NOTIFY_BatteryInformationChanged:
263 		(void)AcpiOsExecute(handler, acpiacad_get_status, dv);
264 		break;
265 
266 	default:
267 		aprint_error_dev(dv, "unknown notify 0x%02X\n", notify);
268 	}
269 }
270 
271 static void
272 acpiacad_init_envsys(device_t dv)
273 {
274 	struct acpiacad_softc *sc = device_private(dv);
275 
276 	sc->sc_sme = sysmon_envsys_create();
277 
278 	sc->sc_sensor.state = ENVSYS_SINVALID;
279 	sc->sc_sensor.units = ENVSYS_INDICATOR;
280 
281  	(void)strlcpy(sc->sc_sensor.desc, "connected", ENVSYS_DESCLEN);
282 
283 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor) != 0)
284 		goto fail;
285 
286 	sc->sc_sme->sme_name = device_xname(dv);
287 	sc->sc_sme->sme_class = SME_CLASS_ACADAPTER;
288 	sc->sc_sme->sme_flags = SME_DISABLE_REFRESH;
289 
290 	if (sysmon_envsys_register(sc->sc_sme) != 0)
291 		goto fail;
292 
293 	(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpiacad_get_status, dv);
294 
295 	return;
296 
297 fail:
298 	aprint_error_dev(dv, "failed to initialize sysmon\n");
299 	sysmon_envsys_destroy(sc->sc_sme);
300 	sc->sc_sme = NULL;
301 }
302 
303 #ifdef _MODULE
304 
305 MODULE(MODULE_CLASS_DRIVER, acpiacad, NULL);
306 CFDRIVER_DECL(acpiacad, DV_DULL, NULL);
307 
308 static int acpiacadloc[] = { -1 };
309 extern struct cfattach acpiacad_ca;
310 
311 static struct cfparent acpiparent = {
312 	"acpinodebus", NULL, DVUNIT_ANY
313 };
314 
315 static struct cfdata acpiacad_cfdata[] = {
316 	{
317 		.cf_name = "acpiacad",
318 		.cf_atname = "acpiacad",
319 		.cf_unit = 0,
320 		.cf_fstate = FSTATE_STAR,
321 		.cf_loc = acpiacadloc,
322 		.cf_flags = 0,
323 		.cf_pspec = &acpiparent,
324 	},
325 
326 	{ NULL }
327 };
328 
329 static int
330 acpiacad_modcmd(modcmd_t cmd, void *context)
331 {
332 	int err;
333 
334 	switch (cmd) {
335 
336 	case MODULE_CMD_INIT:
337 
338 		err = config_cfdriver_attach(&acpiacad_cd);
339 
340 		if (err != 0)
341 			return err;
342 
343 		err = config_cfattach_attach("acpiacad", &acpiacad_ca);
344 
345 		if (err != 0) {
346 			config_cfdriver_detach(&acpiacad_cd);
347 			return err;
348 		}
349 
350 		err = config_cfdata_attach(acpiacad_cfdata, 1);
351 
352 		if (err != 0) {
353 			config_cfattach_detach("acpiacad", &acpiacad_ca);
354 			config_cfdriver_detach(&acpiacad_cd);
355 			return err;
356 		}
357 
358 		return 0;
359 
360 	case MODULE_CMD_FINI:
361 
362 		err = config_cfdata_detach(acpiacad_cfdata);
363 
364 		if (err != 0)
365 			return err;
366 
367 		config_cfattach_detach("acpiacad", &acpiacad_ca);
368 		config_cfdriver_detach(&acpiacad_cd);
369 
370 		return 0;
371 
372 	default:
373 		return ENOTTY;
374 	}
375 }
376 
377 #endif	/* _MODULE */
378