1 /*-
2 * Copyright (c) 2000 Takanori Watanabe
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: releng/5.0/sys/dev/acpica/acpi_acad.c 106377 2002-11-03 10:49:24Z iwasaki $
27 */
28
29 #include "opt_acpi.h"
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/bus.h>
33
34 #include <machine/bus.h>
35 #include <machine/resource.h>
36 #include <sys/rman.h>
37 #include <sys/ioccom.h>
38 #include <sys/malloc.h>
39 #include <sys/conf.h>
40 #include <sys/power.h>
41
42 #include "acpi.h"
43 #include <dev/acpica/acpivar.h>
44 #include <dev/acpica/acpiio.h>
45
46 /*
47 * Hooks for the ACPI CA debugging infrastructure
48 */
49 #define _COMPONENT ACPI_AC_ADAPTER
50 ACPI_MODULE_NAME("AC_ADAPTER")
51
52 #define ACPI_DEVICE_CHECK_PNP 0x00
53 #define ACPI_DEVICE_CHECK_EXISTENCE 0x01
54 #define ACPI_POWERSOURCE_STAT_CHANGE 0x80
55
56 static void acpi_acad_get_status(void * );
57 static void acpi_acad_notify_handler(ACPI_HANDLE , UINT32 ,void *);
58 static int acpi_acad_probe(device_t);
59 static int acpi_acad_attach(device_t);
60 static int acpi_acad_ioctl(u_long, caddr_t, void *);
61 static int acpi_acad_sysctl(SYSCTL_HANDLER_ARGS);
62 static void acpi_acad_init_acline(void *arg);
63
64 struct acpi_acad_softc {
65 int status;
66
67 int initializing;
68 };
69
70 static void
71 acpi_acad_get_status(void *context)
72 {
73 int newstatus;
74 device_t dev = context;
75 struct acpi_acad_softc *sc = device_get_softc(dev);
76 ACPI_HANDLE h = acpi_get_handle(dev);
77
78 if (ACPI_FAILURE(acpi_EvaluateInteger(h, "_PSR", &newstatus))) {
79 sc->status = -1;
80 return;
81 }
82
83 if (sc->status != newstatus) {
84 sc->status = newstatus;
85 /* set system power profile based on AC adapter status */
86 power_profile_set_state(sc->status ? POWER_PROFILE_PERFORMANCE : POWER_PROFILE_ECONOMY);
87 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
88 "%s Line\n",(sc->status) ? "On" : "Off");
89 }
90 }
91
92 static void
93 acpi_acad_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
94 {
95 device_t dev = context;
96
97 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
98 "Notify %d\n", notify);
99
100 switch (notify) {
101 case ACPI_DEVICE_CHECK_PNP:
102 case ACPI_DEVICE_CHECK_EXISTENCE:
103 case ACPI_POWERSOURCE_STAT_CHANGE:
104 /*Temporally. It is better to notify policy manager*/
105 AcpiOsQueueForExecution(OSD_PRIORITY_LO,
106 acpi_acad_get_status,context);
107 break;
108 default:
109 break;
110 }
111 }
112
113 static int
114 acpi_acad_probe(device_t dev)
115 {
116
117 if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
118 acpi_MatchHid(dev, "ACPI0003")) {
119
120 /*
121 * Set device description
122 */
123 device_set_desc(dev, "AC adapter");
124 return(0);
125 }
126 return(ENXIO);
127 }
128
129 static int
130 acpi_acad_attach(device_t dev)
131 {
132 int error;
133 struct acpi_acad_softc *sc;
134 struct acpi_softc *acpi_sc;
135
136 ACPI_HANDLE handle = acpi_get_handle(dev);
137 AcpiInstallNotifyHandler(handle,
138 ACPI_DEVICE_NOTIFY,
139 acpi_acad_notify_handler, dev);
140 /*Installing system notify is not so good*/
141 AcpiInstallNotifyHandler(handle,
142 ACPI_SYSTEM_NOTIFY,
143 acpi_acad_notify_handler, dev);
144
145 if ((sc = device_get_softc(dev)) == NULL) {
146 return (ENXIO);
147 }
148 if ((error = acpi_register_ioctl(ACPIIO_ACAD_GET_STATUS,
149 acpi_acad_ioctl, dev)) != 0) {
150 return (error);
151 }
152
153 if (device_get_unit(dev) == 0) {
154 acpi_sc = acpi_device_get_parent_softc(dev);
155 SYSCTL_ADD_PROC(&acpi_sc->acpi_sysctl_ctx,
156 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
157 OID_AUTO, "acline", CTLTYPE_INT | CTLFLAG_RD,
158 &sc->status, 0, acpi_acad_sysctl, "I", "");
159 }
160
161 /* Get initial status after whole system is up. */
162 sc->status = -1;
163 sc->initializing = 0;
164
165 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_acad_init_acline, dev);
166
167 return(0);
168 }
169
170 static device_method_t acpi_acad_methods[] = {
171 /* Device interface */
172 DEVMETHOD(device_probe, acpi_acad_probe),
173 DEVMETHOD(device_attach, acpi_acad_attach),
174
175 {0, 0}
176 };
177
178 static driver_t acpi_acad_driver = {
179 "acpi_acad",
180 acpi_acad_methods,
181 sizeof(struct acpi_acad_softc),
182 };
183
184 static devclass_t acpi_acad_devclass;
185 DRIVER_MODULE(acpi_acad,acpi,acpi_acad_driver,acpi_acad_devclass,0,0);
186
187 static int
188 acpi_acad_ioctl(u_long cmd, caddr_t addr, void *arg)
189 {
190 device_t dev;
191 struct acpi_acad_softc *sc;
192
193 dev = (device_t)arg;
194 if ((sc = device_get_softc(dev)) == NULL) {
195 return(ENXIO);
196 }
197
198 switch (cmd) {
199 case ACPIIO_ACAD_GET_STATUS:
200 acpi_acad_get_status(dev);
201 *(int *)addr = sc->status;
202 break;
203 }
204
205 return(0);
206 }
207
208 static int
209 acpi_acad_sysctl(SYSCTL_HANDLER_ARGS)
210 {
211 int val;
212 int error;
213
214 if (acpi_acad_get_acline(&val)) {
215 return (ENXIO);
216 }
217
218 val = *(u_int *)oidp->oid_arg1;
219 error = sysctl_handle_int(oidp, &val, 0, req);
220 return (error);
221 }
222
223 static void
224 acpi_acad_init_acline(void *arg)
225 {
226 int retry;
227 int status;
228 device_t dev = (device_t)arg;
229 struct acpi_acad_softc *sc = device_get_softc(dev);
230 #define ACPI_ACAD_RETRY_MAX 6
231
232 if (sc->initializing) {
233 return;
234 }
235
236 sc->initializing = 1;
237
238 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
239 "acline initialization start\n");
240
241 status = 0;
242 for (retry = 0; retry < ACPI_ACAD_RETRY_MAX; retry++, AcpiOsSleep(10, 0)) {
243 acpi_acad_get_status(dev);
244 if (status != sc->status)
245 break;
246 }
247
248 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
249 "acline initialization done, tried %d times\n", retry+1);
250
251 sc->initializing = 0;
252 }
253
254 /*
255 * Public interfaces.
256 */
257
258 int
259 acpi_acad_get_acline(int *status)
260 {
261 device_t dev;
262 struct acpi_acad_softc *sc;
263
264 if ((dev = devclass_get_device(acpi_acad_devclass, 0)) == NULL) {
265 return (ENXIO);
266 }
267
268 if ((sc = device_get_softc(dev)) == NULL) {
269 return (ENXIO);
270 }
271
272 acpi_acad_get_status(dev);
273 *status = sc->status;
274
275 return (0);
276 }
277
Cache object: c517e5bdd3697e58a505cd6f93194eaf
|