hpcapm.c revision 1.2 1 1.2 perry /* $NetBSD: hpcapm.c,v 1.2 2005/02/27 00:26:59 perry Exp $ */
2 1.1 uch
3 1.1 uch /*
4 1.1 uch * Copyright (c) 2000 Takemura Shin
5 1.1 uch * Copyright (c) 2000-2001 SATO Kazumi
6 1.1 uch * All rights reserved.
7 1.1 uch *
8 1.1 uch * Redistribution and use in source and binary forms, with or without
9 1.1 uch * modification, are permitted provided that the following conditions
10 1.1 uch * are met:
11 1.1 uch * 1. Redistributions of source code must retain the above copyright
12 1.1 uch * notice, this list of conditions and the following disclaimer.
13 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 uch * notice, this list of conditions and the following disclaimer in the
15 1.1 uch * documentation and/or other materials provided with the distribution.
16 1.1 uch *
17 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 1.1 uch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 uch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 uch * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 1.1 uch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 uch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 uch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 uch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 uch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 uch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 uch * SUCH DAMAGE.
28 1.1 uch *
29 1.1 uch */
30 1.1 uch
31 1.1 uch #include <sys/cdefs.h>
32 1.2 perry __KERNEL_RCSID(0, "$NetBSD: hpcapm.c,v 1.2 2005/02/27 00:26:59 perry Exp $");
33 1.1 uch
34 1.1 uch #include <sys/param.h>
35 1.1 uch #include <sys/device.h>
36 1.1 uch #include <sys/kernel.h>
37 1.1 uch #include <sys/systm.h>
38 1.1 uch
39 1.1 uch #include <dev/hpc/apm/apmvar.h>
40 1.1 uch
41 1.1 uch #include <machine/bus.h>
42 1.1 uch #include <machine/autoconf.h>
43 1.1 uch #include <machine/config_hook.h>
44 1.1 uch #include <machine/platid.h>
45 1.1 uch #include <machine/platid_mask.h>
46 1.1 uch
47 1.1 uch #define HPCAPMDEBUG
48 1.1 uch #ifdef HPCAPMDEBUG
49 1.1 uch #ifndef HPCAPMDEBUG_CONF
50 1.1 uch #define HPCAPMDEBUG_CONF 1
51 1.1 uch #endif
52 1.1 uch int hpcapm_debug = HPCAPMDEBUG_CONF;
53 1.1 uch #define DPRINTF(arg) do { if (hpcapm_debug) printf arg; } while(0);
54 1.1 uch #define DPRINTFN(n, arg) do { if (hpcapm_debug > (n)) printf arg; } while (0);
55 1.1 uch #else
56 1.1 uch #define DPRINTF(arg) do { } while (0);
57 1.1 uch #define DPRINTFN(n, arg) do { } while (0);
58 1.1 uch #endif
59 1.1 uch
60 1.1 uch /* Definition of the driver for autoconfig. */
61 1.1 uch static int hpcapm_match(struct device *, struct cfdata *, void *);
62 1.1 uch static void hpcapm_attach(struct device *, struct device *, void *);
63 1.1 uch static int hpcapm_hook(void *, int, long, void *);
64 1.1 uch
65 1.1 uch static void hpcapm_disconnect(void *);
66 1.1 uch static void hpcapm_enable(void *, int);
67 1.1 uch static int hpcapm_set_powstate(void *, u_int, u_int);
68 1.1 uch static int hpcapm_get_powstat(void *, struct apm_power_info *);
69 1.1 uch static int hpcapm_get_event(void *, u_int *, u_int *);
70 1.1 uch static void hpcapm_cpu_busy(void *);
71 1.1 uch static void hpcapm_cpu_idle(void *);
72 1.1 uch static void hpcapm_get_capabilities(void *, u_int *, u_int *);
73 1.1 uch
74 1.1 uch struct apmhpc_softc {
75 1.1 uch struct device sc_dev;
76 1.1 uch void *sc_apmdev;
77 1.1 uch volatile unsigned int events;
78 1.1 uch volatile int power_state;
79 1.1 uch volatile int battery_state;
80 1.1 uch volatile int ac_state;
81 1.1 uch config_hook_tag sc_standby_hook;
82 1.1 uch config_hook_tag sc_suspend_hook;
83 1.1 uch config_hook_tag sc_battery_hook;
84 1.1 uch config_hook_tag sc_ac_hook;
85 1.1 uch int battery_life;
86 1.1 uch int minutes_left;
87 1.1 uch };
88 1.1 uch
89 1.1 uch CFATTACH_DECL(hpcapm, sizeof (struct apmhpc_softc),
90 1.1 uch hpcapm_match, hpcapm_attach, NULL, NULL);
91 1.1 uch
92 1.1 uch struct apm_accessops hpcapm_accessops = {
93 1.1 uch hpcapm_disconnect,
94 1.1 uch hpcapm_enable,
95 1.1 uch hpcapm_set_powstate,
96 1.1 uch hpcapm_get_powstat,
97 1.1 uch hpcapm_get_event,
98 1.1 uch hpcapm_cpu_busy,
99 1.1 uch hpcapm_cpu_idle,
100 1.1 uch hpcapm_get_capabilities,
101 1.1 uch };
102 1.1 uch
103 1.1 uch extern struct cfdriver hpcapm_cd;
104 1.1 uch
105 1.1 uch static int
106 1.1 uch hpcapm_match(struct device *parent, struct cfdata *cf, void *aux)
107 1.1 uch {
108 1.1 uch struct mainbus_attach_args *ma = aux;
109 1.1 uch
110 1.1 uch if (strcmp(ma->ma_name, hpcapm_cd.cd_name) != 0) {
111 1.1 uch return (0);
112 1.1 uch }
113 1.1 uch return (1);
114 1.1 uch }
115 1.1 uch
116 1.1 uch static void
117 1.1 uch hpcapm_attach(struct device *parent, struct device *self, void *aux)
118 1.1 uch {
119 1.1 uch struct apmhpc_softc *sc;
120 1.1 uch struct apmdev_attach_args aaa;
121 1.1 uch
122 1.1 uch sc = (struct apmhpc_softc *)self;
123 1.1 uch printf(": pseudo power management module\n");
124 1.1 uch
125 1.1 uch sc->events = 0;
126 1.1 uch sc->power_state = APM_SYS_READY;
127 1.1 uch sc->battery_state = APM_BATT_FLAG_UNKNOWN;
128 1.1 uch sc->ac_state = APM_AC_UNKNOWN;
129 1.1 uch sc->battery_life = APM_BATT_LIFE_UNKNOWN;
130 1.1 uch sc->minutes_left = 0;
131 1.1 uch sc->sc_standby_hook = config_hook(CONFIG_HOOK_PMEVENT,
132 1.1 uch CONFIG_HOOK_PMEVENT_STANDBYREQ,
133 1.1 uch CONFIG_HOOK_EXCLUSIVE,
134 1.1 uch hpcapm_hook, sc);
135 1.1 uch sc->sc_suspend_hook = config_hook(CONFIG_HOOK_PMEVENT,
136 1.1 uch CONFIG_HOOK_PMEVENT_SUSPENDREQ,
137 1.1 uch CONFIG_HOOK_EXCLUSIVE,
138 1.1 uch hpcapm_hook, sc);
139 1.1 uch
140 1.1 uch sc->sc_battery_hook = config_hook(CONFIG_HOOK_PMEVENT,
141 1.1 uch CONFIG_HOOK_PMEVENT_BATTERY,
142 1.1 uch CONFIG_HOOK_SHARE,
143 1.1 uch hpcapm_hook, sc);
144 1.1 uch
145 1.1 uch sc->sc_battery_hook = config_hook(CONFIG_HOOK_PMEVENT,
146 1.1 uch CONFIG_HOOK_PMEVENT_AC,
147 1.1 uch CONFIG_HOOK_SHARE,
148 1.1 uch hpcapm_hook, sc);
149 1.1 uch
150 1.1 uch aaa.accessops = &hpcapm_accessops;
151 1.1 uch aaa.accesscookie = sc;
152 1.1 uch aaa.apm_detail = 0x0102;
153 1.1 uch
154 1.1 uch sc->sc_apmdev = config_found(self, &aaa, apmprint);
155 1.1 uch }
156 1.1 uch
157 1.1 uch static int
158 1.1 uch hpcapm_hook(void *ctx, int type, long id, void *msg)
159 1.1 uch {
160 1.1 uch struct apmhpc_softc *sc;
161 1.1 uch int s;
162 1.1 uch int charge;
163 1.1 uch int message;
164 1.1 uch
165 1.1 uch sc = ctx;
166 1.1 uch
167 1.1 uch if (type != CONFIG_HOOK_PMEVENT)
168 1.2 perry return 1;
169 1.1 uch
170 1.1 uch if (CONFIG_HOOK_VALUEP(msg))
171 1.1 uch message = (int)msg;
172 1.1 uch else
173 1.1 uch message = *(int *)msg;
174 1.1 uch
175 1.1 uch s = splhigh();
176 1.1 uch switch (id) {
177 1.1 uch case CONFIG_HOOK_PMEVENT_STANDBYREQ:
178 1.1 uch if (sc->power_state != APM_SYS_STANDBY) {
179 1.1 uch sc->events |= (1 << APM_USER_STANDBY_REQ);
180 1.1 uch } else {
181 1.1 uch sc->events |= (1 << APM_NORMAL_RESUME);
182 1.1 uch }
183 1.1 uch break;
184 1.1 uch case CONFIG_HOOK_PMEVENT_SUSPENDREQ:
185 1.1 uch if (sc->power_state != APM_SYS_SUSPEND) {
186 1.1 uch DPRINTF(("hpcapm: suspend req\n"));
187 1.1 uch sc->events |= (1 << APM_USER_SUSPEND_REQ);
188 1.1 uch } else {
189 1.1 uch sc->events |= (1 << APM_NORMAL_RESUME);
190 1.1 uch }
191 1.1 uch break;
192 1.2 perry case CONFIG_HOOK_PMEVENT_BATTERY:
193 1.1 uch switch (message) {
194 1.1 uch case CONFIG_HOOK_BATT_CRITICAL:
195 1.1 uch DPRINTF(("hpcapm: battery state critical\n"));
196 1.1 uch charge = sc->battery_state&APM_BATT_FLAG_CHARGING;
197 1.1 uch sc->battery_state = APM_BATT_FLAG_CRITICAL;
198 1.1 uch sc->battery_state |= charge;
199 1.1 uch sc->battery_life = 0;
200 1.1 uch break;
201 1.1 uch case CONFIG_HOOK_BATT_LOW:
202 1.1 uch DPRINTF(("hpcapm: battery state low\n"));
203 1.1 uch charge = sc->battery_state&APM_BATT_FLAG_CHARGING;
204 1.1 uch sc->battery_state = APM_BATT_FLAG_LOW;
205 1.1 uch sc->battery_state |= charge;
206 1.1 uch break;
207 1.1 uch case CONFIG_HOOK_BATT_HIGH:
208 1.1 uch DPRINTF(("hpcapm: battery state high\n"));
209 1.1 uch charge = sc->battery_state&APM_BATT_FLAG_CHARGING;
210 1.1 uch sc->battery_state = APM_BATT_FLAG_HIGH;
211 1.1 uch sc->battery_state |= charge;
212 1.1 uch break;
213 1.1 uch case CONFIG_HOOK_BATT_10P:
214 1.1 uch DPRINTF(("hpcapm: battery life 10%%\n"));
215 1.1 uch sc->battery_life = 10;
216 1.1 uch break;
217 1.1 uch case CONFIG_HOOK_BATT_20P:
218 1.1 uch DPRINTF(("hpcapm: battery life 20%%\n"));
219 1.1 uch sc->battery_life = 20;
220 1.1 uch break;
221 1.1 uch case CONFIG_HOOK_BATT_30P:
222 1.1 uch DPRINTF(("hpcapm: battery life 30%%\n"));
223 1.1 uch sc->battery_life = 30;
224 1.1 uch break;
225 1.1 uch case CONFIG_HOOK_BATT_40P:
226 1.1 uch DPRINTF(("hpcapm: battery life 40%%\n"));
227 1.1 uch sc->battery_life = 40;
228 1.1 uch break;
229 1.1 uch case CONFIG_HOOK_BATT_50P:
230 1.1 uch DPRINTF(("hpcapm: battery life 50%%\n"));
231 1.1 uch sc->battery_life = 50;
232 1.1 uch break;
233 1.1 uch case CONFIG_HOOK_BATT_60P:
234 1.1 uch DPRINTF(("hpcapm: battery life 60%%\n"));
235 1.1 uch sc->battery_life = 60;
236 1.1 uch break;
237 1.1 uch case CONFIG_HOOK_BATT_70P:
238 1.1 uch DPRINTF(("hpcapm: battery life 70%%\n"));
239 1.1 uch sc->battery_life = 70;
240 1.1 uch break;
241 1.1 uch case CONFIG_HOOK_BATT_80P:
242 1.1 uch DPRINTF(("hpcapm: battery life 80%%\n"));
243 1.1 uch sc->battery_life = 80;
244 1.1 uch break;
245 1.1 uch case CONFIG_HOOK_BATT_90P:
246 1.1 uch DPRINTF(("hpcapm: battery life 90%%\n"));
247 1.1 uch sc->battery_life = 90;
248 1.1 uch break;
249 1.1 uch case CONFIG_HOOK_BATT_100P:
250 1.1 uch DPRINTF(("hpcapm: battery life 100%%\n"));
251 1.1 uch sc->battery_life = 100;
252 1.1 uch break;
253 1.1 uch case CONFIG_HOOK_BATT_UNKNOWN:
254 1.1 uch DPRINTF(("hpcapm: battery state unknown\n"));
255 1.1 uch sc->battery_state = APM_BATT_FLAG_UNKNOWN;
256 1.1 uch sc->battery_life = APM_BATT_LIFE_UNKNOWN;
257 1.1 uch break;
258 1.1 uch case CONFIG_HOOK_BATT_NO_SYSTEM_BATTERY:
259 1.1 uch DPRINTF(("hpcapm: battery state no system battery?\n"));
260 1.1 uch sc->battery_state = APM_BATT_FLAG_NO_SYSTEM_BATTERY;
261 1.1 uch sc->battery_life = APM_BATT_LIFE_UNKNOWN;
262 1.1 uch break;
263 1.1 uch }
264 1.1 uch break;
265 1.1 uch case CONFIG_HOOK_PMEVENT_AC:
266 1.1 uch switch (message) {
267 1.1 uch case CONFIG_HOOK_AC_OFF:
268 1.1 uch DPRINTF(("hpcapm: ac not connect\n"));
269 1.1 uch sc->battery_state &= ~APM_BATT_FLAG_CHARGING;
270 1.1 uch sc->ac_state = APM_AC_OFF;
271 1.1 uch break;
272 1.1 uch case CONFIG_HOOK_AC_ON_CHARGE:
273 1.1 uch DPRINTF(("hpcapm: charging\n"));
274 1.1 uch sc->battery_state |= APM_BATT_FLAG_CHARGING;
275 1.1 uch sc->ac_state = APM_AC_ON;
276 1.1 uch break;
277 1.1 uch case CONFIG_HOOK_AC_ON_NOCHARGE:
278 1.1 uch DPRINTF(("hpcapm: ac connect\n"));
279 1.1 uch sc->battery_state &= ~APM_BATT_FLAG_CHARGING;
280 1.1 uch sc->ac_state = APM_AC_ON;
281 1.1 uch break;
282 1.1 uch case CONFIG_HOOK_AC_UNKNOWN:
283 1.1 uch sc->ac_state = APM_AC_UNKNOWN;
284 1.1 uch break;
285 1.1 uch }
286 1.1 uch break;
287 1.1 uch }
288 1.1 uch splx(s);
289 1.1 uch
290 1.1 uch return (0);
291 1.1 uch }
292 1.1 uch
293 1.1 uch static void
294 1.1 uch hpcapm_disconnect(void *scx)
295 1.1 uch {
296 1.1 uch struct apmhpc_softc *sc;
297 1.1 uch
298 1.1 uch sc = scx;
299 1.1 uch }
300 1.1 uch
301 1.1 uch static void
302 1.1 uch hpcapm_enable(void *scx, int onoff)
303 1.1 uch {
304 1.1 uch struct apmhpc_softc *sc;
305 1.1 uch
306 1.1 uch sc = scx;
307 1.1 uch }
308 1.1 uch
309 1.1 uch static int
310 1.1 uch hpcapm_set_powstate(void *scx, u_int devid, u_int powstat)
311 1.1 uch {
312 1.1 uch struct apmhpc_softc *sc;
313 1.1 uch int s;
314 1.1 uch
315 1.1 uch sc = scx;
316 1.1 uch
317 1.1 uch if (devid != APM_DEV_ALLDEVS)
318 1.1 uch return APM_ERR_UNRECOG_DEV;
319 1.1 uch
320 1.1 uch switch (powstat) {
321 1.1 uch case APM_SYS_READY:
322 1.1 uch DPRINTF(("hpcapm: set power state READY\n"));
323 1.1 uch sc->power_state = APM_SYS_READY;
324 1.1 uch break;
325 1.1 uch case APM_SYS_STANDBY:
326 1.1 uch DPRINTF(("hpcapm: set power state STANDBY\n"));
327 1.1 uch s = splhigh();
328 1.2 perry config_hook_call(CONFIG_HOOK_PMEVENT,
329 1.1 uch CONFIG_HOOK_PMEVENT_HARDPOWER,
330 1.1 uch (void *)PWR_STANDBY);
331 1.1 uch sc->power_state = APM_SYS_STANDBY;
332 1.1 uch machine_standby();
333 1.2 perry config_hook_call_reverse(CONFIG_HOOK_PMEVENT,
334 1.1 uch CONFIG_HOOK_PMEVENT_HARDPOWER,
335 1.1 uch (void *)PWR_RESUME);
336 1.1 uch DPRINTF(("hpcapm: resume\n"));
337 1.1 uch splx(s);
338 1.1 uch break;
339 1.1 uch case APM_SYS_SUSPEND:
340 1.1 uch DPRINTF(("hpcapm: set power state SUSPEND...\n"));
341 1.1 uch s = splhigh();
342 1.2 perry config_hook_call(CONFIG_HOOK_PMEVENT,
343 1.1 uch CONFIG_HOOK_PMEVENT_HARDPOWER,
344 1.1 uch (void *)PWR_SUSPEND);
345 1.1 uch sc->power_state = APM_SYS_SUSPEND;
346 1.1 uch machine_sleep();
347 1.2 perry config_hook_call_reverse(CONFIG_HOOK_PMEVENT,
348 1.1 uch CONFIG_HOOK_PMEVENT_HARDPOWER,
349 1.1 uch (void *)PWR_RESUME);
350 1.1 uch DPRINTF(("hpcapm: resume\n"));
351 1.1 uch splx(s);
352 1.1 uch break;
353 1.1 uch case APM_SYS_OFF:
354 1.1 uch DPRINTF(("hpcapm: set power state OFF\n"));
355 1.1 uch sc->power_state = APM_SYS_OFF;
356 1.1 uch break;
357 1.1 uch case APM_LASTREQ_INPROG:
358 1.1 uch /*DPRINTF(("hpcapm: set power state INPROG\n"));
359 1.1 uch */
360 1.1 uch break;
361 1.1 uch case APM_LASTREQ_REJECTED:
362 1.1 uch DPRINTF(("hpcapm: set power state REJECTED\n"));
363 1.1 uch break;
364 1.1 uch }
365 1.1 uch
366 1.1 uch return (0);
367 1.1 uch }
368 1.1 uch
369 1.1 uch static int
370 1.1 uch hpcapm_get_powstat(void *scx, struct apm_power_info *pinfo)
371 1.1 uch {
372 1.1 uch struct apmhpc_softc *sc;
373 1.1 uch
374 1.1 uch sc = scx;
375 1.1 uch
376 1.1 uch pinfo->ac_state = sc->ac_state;
377 1.1 uch pinfo->battery_state = sc->battery_state;
378 1.1 uch pinfo->battery_life = sc->battery_life;
379 1.1 uch return (0);
380 1.1 uch }
381 1.1 uch
382 1.1 uch static int
383 1.1 uch hpcapm_get_event(void *scx, u_int *event_type, u_int *event_info)
384 1.1 uch {
385 1.1 uch struct apmhpc_softc *sc;
386 1.1 uch int s, i;
387 1.1 uch
388 1.1 uch sc = scx;
389 1.1 uch s = splhigh();
390 1.1 uch for (i = APM_STANDBY_REQ; i <= APM_CAP_CHANGE; i++) {
391 1.1 uch if (sc->events & (1 << i)) {
392 1.1 uch sc->events &= ~(1 << i);
393 1.1 uch *event_type = i;
394 1.1 uch if (*event_type == APM_NORMAL_RESUME ||
395 1.1 uch *event_type == APM_CRIT_RESUME) {
396 1.1 uch /* pccard power off in the suspend state */
397 1.1 uch *event_info = 1;
398 1.1 uch sc->power_state = APM_SYS_READY;
399 1.1 uch } else
400 1.1 uch *event_info = 0;
401 1.1 uch return (0);
402 1.1 uch }
403 1.1 uch }
404 1.1 uch splx(s);
405 1.1 uch
406 1.1 uch return APM_ERR_NOEVENTS;
407 1.1 uch }
408 1.1 uch
409 1.1 uch static void
410 1.1 uch hpcapm_cpu_busy(void *scx)
411 1.1 uch {
412 1.1 uch struct apmhpc_softc *sc;
413 1.1 uch
414 1.1 uch sc = scx;
415 1.1 uch }
416 1.1 uch
417 1.1 uch static void
418 1.1 uch hpcapm_cpu_idle(void *scx)
419 1.1 uch {
420 1.1 uch struct apmhpc_softc *sc;
421 1.1 uch
422 1.1 uch sc = scx;
423 1.1 uch }
424 1.1 uch
425 1.1 uch static void
426 1.1 uch hpcapm_get_capabilities(void *scx, u_int *numbatts, u_int *capflags)
427 1.1 uch {
428 1.1 uch struct apmhpc_softc *sc;
429 1.1 uch
430 1.1 uch *numbatts = 0;
431 1.1 uch *capflags = APM_GLOBAL_STANDBY | APM_GLOBAL_SUSPEND;
432 1.1 uch
433 1.1 uch sc = scx;
434 1.1 uch }
435