acpi_acad.c revision 1.1.2.2 1 1.1.2.2 fvdl /* $NetBSD: acpi_acad.c,v 1.1.2.2 2001/10/01 12:44:14 fvdl Exp $ */
2 1.1.2.2 fvdl
3 1.1.2.2 fvdl /*
4 1.1.2.2 fvdl * Copyright 2001 Wasabi Systems, Inc.
5 1.1.2.2 fvdl * All rights reserved.
6 1.1.2.2 fvdl *
7 1.1.2.2 fvdl * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.1.2.2 fvdl *
9 1.1.2.2 fvdl * Redistribution and use in source and binary forms, with or without
10 1.1.2.2 fvdl * modification, are permitted provided that the following conditions
11 1.1.2.2 fvdl * are met:
12 1.1.2.2 fvdl * 1. Redistributions of source code must retain the above copyright
13 1.1.2.2 fvdl * notice, this list of conditions and the following disclaimer.
14 1.1.2.2 fvdl * 2. Redistributions in binary form must reproduce the above copyright
15 1.1.2.2 fvdl * notice, this list of conditions and the following disclaimer in the
16 1.1.2.2 fvdl * documentation and/or other materials provided with the distribution.
17 1.1.2.2 fvdl * 3. All advertising materials mentioning features or use of this software
18 1.1.2.2 fvdl * must display the following acknowledgement:
19 1.1.2.2 fvdl * This product includes software developed for the NetBSD Project by
20 1.1.2.2 fvdl * Wasabi Systems, Inc.
21 1.1.2.2 fvdl * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1.2.2 fvdl * or promote products derived from this software without specific prior
23 1.1.2.2 fvdl * written permission.
24 1.1.2.2 fvdl *
25 1.1.2.2 fvdl * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1.2.2 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1.2.2 fvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1.2.2 fvdl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1.2.2 fvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1.2.2 fvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1.2.2 fvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1.2.2 fvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1.2.2 fvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1.2.2 fvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1.2.2 fvdl * POSSIBILITY OF SUCH DAMAGE.
36 1.1.2.2 fvdl */
37 1.1.2.2 fvdl
38 1.1.2.2 fvdl /*
39 1.1.2.2 fvdl * ACPI AC Adapter driver.
40 1.1.2.2 fvdl */
41 1.1.2.2 fvdl
42 1.1.2.2 fvdl #include <sys/param.h>
43 1.1.2.2 fvdl #include <sys/systm.h>
44 1.1.2.2 fvdl #include <sys/device.h>
45 1.1.2.2 fvdl
46 1.1.2.2 fvdl #include <dev/acpi/acpica.h>
47 1.1.2.2 fvdl #include <dev/acpi/acpireg.h>
48 1.1.2.2 fvdl #include <dev/acpi/acpivar.h>
49 1.1.2.2 fvdl
50 1.1.2.2 fvdl struct acpiacad_softc {
51 1.1.2.2 fvdl struct device sc_dev; /* base device glue */
52 1.1.2.2 fvdl struct acpi_devnode *sc_node; /* our ACPI devnode */
53 1.1.2.2 fvdl int sc_flags; /* see below */
54 1.1.2.2 fvdl int sc_status; /* power status */
55 1.1.2.2 fvdl };
56 1.1.2.2 fvdl
57 1.1.2.2 fvdl #define AACAD_F_VERBOSE 0x01 /* verbose events */
58 1.1.2.2 fvdl
59 1.1.2.2 fvdl int acpiacad_match(struct device *, struct cfdata *, void *);
60 1.1.2.2 fvdl void acpiacad_attach(struct device *, struct device *, void *);
61 1.1.2.2 fvdl
62 1.1.2.2 fvdl struct cfattach acpiacad_ca = {
63 1.1.2.2 fvdl sizeof(struct acpiacad_softc), acpiacad_match, acpiacad_attach,
64 1.1.2.2 fvdl };
65 1.1.2.2 fvdl
66 1.1.2.2 fvdl void acpiacad_get_status(void *);
67 1.1.2.2 fvdl void acpiacad_notify_handler(ACPI_HANDLE, UINT32, void *context);
68 1.1.2.2 fvdl
69 1.1.2.2 fvdl /*
70 1.1.2.2 fvdl * acpiacad_match:
71 1.1.2.2 fvdl *
72 1.1.2.2 fvdl * Autoconfiguration `match' routine.
73 1.1.2.2 fvdl */
74 1.1.2.2 fvdl int
75 1.1.2.2 fvdl acpiacad_match(struct device *parent, struct cfdata *match, void *aux)
76 1.1.2.2 fvdl {
77 1.1.2.2 fvdl struct acpi_attach_args *aa = aux;
78 1.1.2.2 fvdl
79 1.1.2.2 fvdl if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
80 1.1.2.2 fvdl return (0);
81 1.1.2.2 fvdl
82 1.1.2.2 fvdl if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "ACPI0003") == 0)
83 1.1.2.2 fvdl return (1);
84 1.1.2.2 fvdl
85 1.1.2.2 fvdl return (0);
86 1.1.2.2 fvdl }
87 1.1.2.2 fvdl
88 1.1.2.2 fvdl /*
89 1.1.2.2 fvdl * acpiacad_attach:
90 1.1.2.2 fvdl *
91 1.1.2.2 fvdl * Autoconfiguration `attach' routine.
92 1.1.2.2 fvdl */
93 1.1.2.2 fvdl void
94 1.1.2.2 fvdl acpiacad_attach(struct device *parent, struct device *self, void *aux)
95 1.1.2.2 fvdl {
96 1.1.2.2 fvdl struct acpiacad_softc *sc = (void *) self;
97 1.1.2.2 fvdl struct acpi_attach_args *aa = aux;
98 1.1.2.2 fvdl ACPI_STATUS rv;
99 1.1.2.2 fvdl
100 1.1.2.2 fvdl printf(": ACPI AC Adapter\n");
101 1.1.2.2 fvdl
102 1.1.2.2 fvdl sc->sc_node = aa->aa_node;
103 1.1.2.2 fvdl
104 1.1.2.2 fvdl rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
105 1.1.2.2 fvdl ACPI_DEVICE_NOTIFY, acpiacad_notify_handler, sc);
106 1.1.2.2 fvdl if (rv != AE_OK) {
107 1.1.2.2 fvdl printf("%s: unable to register DEVICE NOTIFY handler: %d\n",
108 1.1.2.2 fvdl sc->sc_dev.dv_xname, rv);
109 1.1.2.2 fvdl return;
110 1.1.2.2 fvdl }
111 1.1.2.2 fvdl
112 1.1.2.2 fvdl /* XXX See acpiacad_notify_handler() */
113 1.1.2.2 fvdl rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
114 1.1.2.2 fvdl ACPI_SYSTEM_NOTIFY, acpiacad_notify_handler, sc);
115 1.1.2.2 fvdl if (rv != AE_OK) {
116 1.1.2.2 fvdl printf("%s: unable to register SYSTEM NOTIFY handler: %d\n",
117 1.1.2.2 fvdl sc->sc_dev.dv_xname, rv);
118 1.1.2.2 fvdl return;
119 1.1.2.2 fvdl }
120 1.1.2.2 fvdl
121 1.1.2.2 fvdl /* Display the current state. */
122 1.1.2.2 fvdl sc->sc_flags = AACAD_F_VERBOSE;
123 1.1.2.2 fvdl acpiacad_get_status(sc);
124 1.1.2.2 fvdl
125 1.1.2.2 fvdl /*
126 1.1.2.2 fvdl * XXX Hook into sysmon here.
127 1.1.2.2 fvdl */
128 1.1.2.2 fvdl }
129 1.1.2.2 fvdl
130 1.1.2.2 fvdl /*
131 1.1.2.2 fvdl * acpiacad_get_status:
132 1.1.2.2 fvdl *
133 1.1.2.2 fvdl * Get, and possibly display, the current AC line status.
134 1.1.2.2 fvdl */
135 1.1.2.2 fvdl void
136 1.1.2.2 fvdl acpiacad_get_status(void *arg)
137 1.1.2.2 fvdl {
138 1.1.2.2 fvdl struct acpiacad_softc *sc = arg;
139 1.1.2.2 fvdl
140 1.1.2.2 fvdl if (acpi_eval_integer(sc->sc_node->ad_handle, "_PSR",
141 1.1.2.2 fvdl &sc->sc_status) != AE_OK)
142 1.1.2.2 fvdl return;
143 1.1.2.2 fvdl
144 1.1.2.2 fvdl if (sc->sc_flags & AACAD_F_VERBOSE)
145 1.1.2.2 fvdl printf("%s: AC adapter %sconnected\n",
146 1.1.2.2 fvdl sc->sc_dev.dv_xname, sc->sc_status == 0 ? "not " : "");
147 1.1.2.2 fvdl }
148 1.1.2.2 fvdl
149 1.1.2.2 fvdl /*
150 1.1.2.2 fvdl * acpiacad_notify_handler:
151 1.1.2.2 fvdl *
152 1.1.2.2 fvdl * Callback from ACPI interrupt handler to notify us of an event.
153 1.1.2.2 fvdl */
154 1.1.2.2 fvdl void
155 1.1.2.2 fvdl acpiacad_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
156 1.1.2.2 fvdl {
157 1.1.2.2 fvdl struct acpiacad_softc *sc = context;
158 1.1.2.2 fvdl int rv;
159 1.1.2.2 fvdl
160 1.1.2.2 fvdl switch (notify) {
161 1.1.2.2 fvdl /*
162 1.1.2.2 fvdl * XXX So, BusCheck is not exactly what I would expect,
163 1.1.2.2 fvdl * but at least my IBM T21 sends it on AC adapter status
164 1.1.2.2 fvdl * change. --thorpej (at) wasabisystems.com
165 1.1.2.2 fvdl */
166 1.1.2.2 fvdl case ACPI_NOTIFY_BusCheck:
167 1.1.2.2 fvdl case ACPI_NOTIFY_PowerSourceStatusChanged:
168 1.1.2.2 fvdl #ifdef ACPI_ACAD_DEBUG
169 1.1.2.2 fvdl printf("%s: received notify message: 0x%x\n",
170 1.1.2.2 fvdl sc->sc_dev.dv_xname, notify);
171 1.1.2.2 fvdl #endif
172 1.1.2.2 fvdl rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
173 1.1.2.2 fvdl acpiacad_get_status, sc);
174 1.1.2.2 fvdl if (rv != AE_OK)
175 1.1.2.2 fvdl printf("%s: unable to queue status check: %d\n",
176 1.1.2.2 fvdl sc->sc_dev.dv_xname, rv);
177 1.1.2.2 fvdl break;
178 1.1.2.2 fvdl
179 1.1.2.2 fvdl default:
180 1.1.2.2 fvdl printf("%s: received unknown notify message: 0x%x\n",
181 1.1.2.2 fvdl sc->sc_dev.dv_xname, notify);
182 1.1.2.2 fvdl }
183 1.1.2.2 fvdl }
184