xref: /netbsd-src/sys/dev/acpi/acpi_acad.c (revision 8a8f936f250a330d54f8a24ed0e92aadf9743a7b)
1 /*	$NetBSD: acpi_acad.c,v 1.1 2001/09/29 05:35:06 thorpej 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/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 
46 #include <dev/acpi/acpica.h>
47 #include <dev/acpi/acpireg.h>
48 #include <dev/acpi/acpivar.h>
49 
50 struct acpiacad_softc {
51 	struct device sc_dev;		/* base device glue */
52 	struct acpi_devnode *sc_node;	/* our ACPI devnode */
53 	int sc_flags;			/* see below */
54 	int sc_status;			/* power status */
55 };
56 
57 #define	AACAD_F_VERBOSE		0x01	/* verbose events */
58 
59 int	acpiacad_match(struct device *, struct cfdata *, void *);
60 void	acpiacad_attach(struct device *, struct device *, void *);
61 
62 struct cfattach acpiacad_ca = {
63 	sizeof(struct acpiacad_softc), acpiacad_match, acpiacad_attach,
64 };
65 
66 void	acpiacad_get_status(void *);
67 void	acpiacad_notify_handler(ACPI_HANDLE, UINT32, void *context);
68 
69 /*
70  * acpiacad_match:
71  *
72  *	Autoconfiguration `match' routine.
73  */
74 int
75 acpiacad_match(struct device *parent, struct cfdata *match, void *aux)
76 {
77 	struct acpi_attach_args *aa = aux;
78 
79 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
80 		return (0);
81 
82 	if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "ACPI0003") == 0)
83 		return (1);
84 
85 	return (0);
86 }
87 
88 /*
89  * acpiacad_attach:
90  *
91  *	Autoconfiguration `attach' routine.
92  */
93 void
94 acpiacad_attach(struct device *parent, struct device *self, void *aux)
95 {
96 	struct acpiacad_softc *sc = (void *) self;
97 	struct acpi_attach_args *aa = aux;
98 	ACPI_STATUS rv;
99 
100 	printf(": ACPI AC Adapter\n");
101 
102 	sc->sc_node = aa->aa_node;
103 
104 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
105 	    ACPI_DEVICE_NOTIFY, acpiacad_notify_handler, sc);
106 	if (rv != AE_OK) {
107 		printf("%s: unable to register DEVICE NOTIFY handler: %d\n",
108 		    sc->sc_dev.dv_xname, rv);
109 		return;
110 	}
111 
112 	/* XXX See acpiacad_notify_handler() */
113 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
114 	    ACPI_SYSTEM_NOTIFY, acpiacad_notify_handler, sc);
115 	if (rv != AE_OK) {
116 		printf("%s: unable to register SYSTEM NOTIFY handler: %d\n",
117 		    sc->sc_dev.dv_xname, rv);
118 		return;
119 	}
120 
121 	/* Display the current state. */
122 	sc->sc_flags = AACAD_F_VERBOSE;
123 	acpiacad_get_status(sc);
124 
125 	/*
126 	 * XXX Hook into sysmon here.
127 	 */
128 }
129 
130 /*
131  * acpiacad_get_status:
132  *
133  *	Get, and possibly display, the current AC line status.
134  */
135 void
136 acpiacad_get_status(void *arg)
137 {
138 	struct acpiacad_softc *sc = arg;
139 
140 	if (acpi_eval_integer(sc->sc_node->ad_handle, "_PSR",
141 	    &sc->sc_status) != AE_OK)
142 		return;
143 
144 	if (sc->sc_flags & AACAD_F_VERBOSE)
145 		printf("%s: AC adapter %sconnected\n",
146 		    sc->sc_dev.dv_xname, sc->sc_status == 0 ? "not " : "");
147 }
148 
149 /*
150  * acpiacad_notify_handler:
151  *
152  *	Callback from ACPI interrupt handler to notify us of an event.
153  */
154 void
155 acpiacad_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
156 {
157 	struct acpiacad_softc *sc = context;
158 	int rv;
159 
160 	switch (notify) {
161 	/*
162 	 * XXX So, BusCheck is not exactly what I would expect,
163 	 * but at least my IBM T21 sends it on AC adapter status
164 	 * change.  --thorpej@wasabisystems.com
165 	 */
166 	case ACPI_NOTIFY_BusCheck:
167 	case ACPI_NOTIFY_PowerSourceStatusChanged:
168 #ifdef ACPI_ACAD_DEBUG
169 		printf("%s: received notify message: 0x%x\n",
170 		    sc->sc_dev.dv_xname, notify);
171 #endif
172 		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
173 		    acpiacad_get_status, sc);
174 		if (rv != AE_OK)
175 			printf("%s: unable to queue status check: %d\n",
176 			    sc->sc_dev.dv_xname, rv);
177 		break;
178 
179 	default:
180 		printf("%s: received unknown notify message: 0x%x\n",
181 		    sc->sc_dev.dv_xname, notify);
182 	}
183 }
184