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