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