1 /* $NetBSD: hpcapm.c,v 1.17 2009/05/12 12:13:49 cegger Exp $ */
4 * Copyright (c) 2000 Takemura Shin
5 * Copyright (c) 2000-2001 SATO Kazumi
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: hpcapm.c,v 1.17 2009/05/12 12:13:49 cegger Exp $");
35 #include "opt_hpcapm.h"
38 #include <sys/param.h>
39 #include <sys/device.h>
40 #include <sys/kernel.h>
41 #include <sys/systm.h>
42 #include <sys/selinfo.h> /* XXX: for apm_softc that is exposed here */
44 #include <dev/hpc/apm/apmvar.h>
47 #include <machine/config_hook.h>
48 #include <machine/platid.h>
49 #include <machine/platid_mask.h>
52 #ifndef HPCAPMDEBUG_CONF
53 #define HPCAPMDEBUG_CONF 1
55 int hpcapm_debug
= HPCAPMDEBUG_CONF
;
56 #define DPRINTF(arg) do { if (hpcapm_debug) printf arg; } while(0);
57 #define DPRINTFN(n, arg) do { if (hpcapm_debug > (n)) printf arg; } while (0);
59 #define DPRINTF(arg) do { } while (0);
60 #define DPRINTFN(n, arg) do { } while (0);
63 /* Definition of the driver for autoconfig. */
64 static int hpcapm_match(device_t
, cfdata_t
, void *);
65 static void hpcapm_attach(device_t
, device_t
, void *);
66 static int hpcapm_hook(void *, int, long, void *);
68 static void hpcapm_disconnect(void *);
69 static void hpcapm_enable(void *, int);
70 static int hpcapm_set_powstate(void *, u_int
, u_int
);
71 static int hpcapm_get_powstat(void *, u_int
, struct apm_power_info
*);
72 static int hpcapm_get_event(void *, u_int
*, u_int
*);
73 static void hpcapm_cpu_busy(void *);
74 static void hpcapm_cpu_idle(void *);
75 static void hpcapm_get_capabilities(void *, u_int
*, u_int
*);
80 volatile unsigned int events
;
81 volatile int power_state
;
82 volatile int battery_flags
;
83 volatile int ac_state
;
84 config_hook_tag sc_standby_hook
;
85 config_hook_tag sc_suspend_hook
;
86 config_hook_tag sc_battery_hook
;
87 config_hook_tag sc_ac_hook
;
92 CFATTACH_DECL(hpcapm
, sizeof (struct apmhpc_softc
),
93 hpcapm_match
, hpcapm_attach
, NULL
, NULL
);
95 struct apm_accessops hpcapm_accessops
= {
103 hpcapm_get_capabilities
,
106 extern struct cfdriver hpcapm_cd
;
109 hpcapm_match(device_t parent
,
110 cfdata_t cf
, void *aux
)
117 hpcapm_attach(device_t parent
,
118 device_t self
, void *aux
)
120 struct apmhpc_softc
*sc
;
121 struct apmdev_attach_args aaa
;
123 sc
= device_private(self
);
124 printf(": pseudo power management module\n");
127 sc
->power_state
= APM_SYS_READY
;
128 sc
->battery_flags
= APM_BATT_FLAG_UNKNOWN
;
129 sc
->ac_state
= APM_AC_UNKNOWN
;
130 sc
->battery_life
= APM_BATT_LIFE_UNKNOWN
;
131 sc
->minutes_left
= 0;
132 sc
->sc_standby_hook
= config_hook(CONFIG_HOOK_PMEVENT
,
133 CONFIG_HOOK_PMEVENT_STANDBYREQ
,
134 CONFIG_HOOK_EXCLUSIVE
,
136 sc
->sc_suspend_hook
= config_hook(CONFIG_HOOK_PMEVENT
,
137 CONFIG_HOOK_PMEVENT_SUSPENDREQ
,
138 CONFIG_HOOK_EXCLUSIVE
,
141 sc
->sc_battery_hook
= config_hook(CONFIG_HOOK_PMEVENT
,
142 CONFIG_HOOK_PMEVENT_BATTERY
,
146 sc
->sc_ac_hook
= config_hook(CONFIG_HOOK_PMEVENT
,
147 CONFIG_HOOK_PMEVENT_AC
,
151 aaa
.accessops
= &hpcapm_accessops
;
152 aaa
.accesscookie
= sc
;
153 aaa
.apm_detail
= 0x0102;
155 sc
->sc_apmdev
= config_found_ia(self
, "apmdevif", &aaa
, apmprint
);
157 if (!pmf_device_register(self
, NULL
, NULL
))
158 aprint_error_dev(self
, "unable to establish power handler\n");
162 hpcapm_hook(void *ctx
, int type
, long id
, void *msg
)
164 struct apmhpc_softc
*sc
;
171 if (type
!= CONFIG_HOOK_PMEVENT
)
174 if (CONFIG_HOOK_VALUEP(msg
))
177 message
= *(int *)msg
;
181 case CONFIG_HOOK_PMEVENT_STANDBYREQ
:
182 if (sc
->power_state
!= APM_SYS_STANDBY
) {
183 sc
->events
|= (1 << APM_USER_STANDBY_REQ
);
185 sc
->events
|= (1 << APM_NORMAL_RESUME
);
188 case CONFIG_HOOK_PMEVENT_SUSPENDREQ
:
189 if (sc
->power_state
!= APM_SYS_SUSPEND
) {
190 DPRINTF(("hpcapm: suspend request\n"));
191 sc
->events
|= (1 << APM_USER_SUSPEND_REQ
);
193 sc
->events
|= (1 << APM_NORMAL_RESUME
);
196 case CONFIG_HOOK_PMEVENT_BATTERY
:
198 case CONFIG_HOOK_BATT_CRITICAL
:
199 DPRINTF(("hpcapm: battery state critical\n"));
200 charge
= sc
->battery_flags
& APM_BATT_FLAG_CHARGING
;
201 sc
->battery_flags
= APM_BATT_FLAG_CRITICAL
;
202 sc
->battery_flags
|= charge
;
203 sc
->battery_life
= 0;
205 case CONFIG_HOOK_BATT_LOW
:
206 DPRINTF(("hpcapm: battery state low\n"));
207 charge
= sc
->battery_flags
& APM_BATT_FLAG_CHARGING
;
208 sc
->battery_flags
= APM_BATT_FLAG_LOW
;
209 sc
->battery_flags
|= charge
;
211 case CONFIG_HOOK_BATT_HIGH
:
212 DPRINTF(("hpcapm: battery state high\n"));
213 charge
= sc
->battery_flags
& APM_BATT_FLAG_CHARGING
;
214 sc
->battery_flags
= APM_BATT_FLAG_HIGH
;
215 sc
->battery_flags
|= charge
;
217 case CONFIG_HOOK_BATT_10P
:
218 DPRINTF(("hpcapm: battery life 10%%\n"));
219 sc
->battery_life
= 10;
221 case CONFIG_HOOK_BATT_20P
:
222 DPRINTF(("hpcapm: battery life 20%%\n"));
223 sc
->battery_life
= 20;
225 case CONFIG_HOOK_BATT_30P
:
226 DPRINTF(("hpcapm: battery life 30%%\n"));
227 sc
->battery_life
= 30;
229 case CONFIG_HOOK_BATT_40P
:
230 DPRINTF(("hpcapm: battery life 40%%\n"));
231 sc
->battery_life
= 40;
233 case CONFIG_HOOK_BATT_50P
:
234 DPRINTF(("hpcapm: battery life 50%%\n"));
235 sc
->battery_life
= 50;
237 case CONFIG_HOOK_BATT_60P
:
238 DPRINTF(("hpcapm: battery life 60%%\n"));
239 sc
->battery_life
= 60;
241 case CONFIG_HOOK_BATT_70P
:
242 DPRINTF(("hpcapm: battery life 70%%\n"));
243 sc
->battery_life
= 70;
245 case CONFIG_HOOK_BATT_80P
:
246 DPRINTF(("hpcapm: battery life 80%%\n"));
247 sc
->battery_life
= 80;
249 case CONFIG_HOOK_BATT_90P
:
250 DPRINTF(("hpcapm: battery life 90%%\n"));
251 sc
->battery_life
= 90;
253 case CONFIG_HOOK_BATT_100P
:
254 DPRINTF(("hpcapm: battery life 100%%\n"));
255 sc
->battery_life
= 100;
257 case CONFIG_HOOK_BATT_UNKNOWN
:
258 DPRINTF(("hpcapm: battery state unknown\n"));
259 sc
->battery_flags
= APM_BATT_FLAG_UNKNOWN
;
260 sc
->battery_life
= APM_BATT_LIFE_UNKNOWN
;
262 case CONFIG_HOOK_BATT_NO_SYSTEM_BATTERY
:
263 DPRINTF(("hpcapm: battery state no system battery?\n"));
264 sc
->battery_flags
= APM_BATT_FLAG_NO_SYSTEM_BATTERY
;
265 sc
->battery_life
= APM_BATT_LIFE_UNKNOWN
;
269 case CONFIG_HOOK_PMEVENT_AC
:
271 case CONFIG_HOOK_AC_OFF
:
272 DPRINTF(("hpcapm: ac not connected\n"));
273 sc
->battery_flags
&= ~APM_BATT_FLAG_CHARGING
;
274 sc
->ac_state
= APM_AC_OFF
;
276 case CONFIG_HOOK_AC_ON_CHARGE
:
277 DPRINTF(("hpcapm: charging\n"));
278 sc
->battery_flags
|= APM_BATT_FLAG_CHARGING
;
279 sc
->ac_state
= APM_AC_ON
;
281 case CONFIG_HOOK_AC_ON_NOCHARGE
:
282 DPRINTF(("hpcapm: ac connected\n"));
283 sc
->battery_flags
&= ~APM_BATT_FLAG_CHARGING
;
284 sc
->ac_state
= APM_AC_ON
;
286 case CONFIG_HOOK_AC_UNKNOWN
:
287 sc
->ac_state
= APM_AC_UNKNOWN
;
298 hpcapm_disconnect(void *scx
)
300 struct apmhpc_softc
*sc
;
306 hpcapm_enable(void *scx
, int onoff
)
308 struct apmhpc_softc
*sc
;
314 hpcapm_set_powstate(void *scx
, u_int devid
, u_int powstat
)
316 struct apmhpc_softc
*sc
;
321 if (devid
!= APM_DEV_ALLDEVS
)
322 return APM_ERR_UNRECOG_DEV
;
326 DPRINTF(("hpcapm: set power state READY\n"));
327 sc
->power_state
= APM_SYS_READY
;
329 case APM_SYS_STANDBY
:
330 DPRINTF(("hpcapm: set power state STANDBY\n"));
332 config_hook_call(CONFIG_HOOK_PMEVENT
,
333 CONFIG_HOOK_PMEVENT_HARDPOWER
,
334 (void *)PWR_STANDBY
);
335 sc
->power_state
= APM_SYS_STANDBY
;
337 config_hook_call_reverse(CONFIG_HOOK_PMEVENT
,
338 CONFIG_HOOK_PMEVENT_HARDPOWER
,
340 DPRINTF(("hpcapm: resume\n"));
343 case APM_SYS_SUSPEND
:
344 DPRINTF(("hpcapm: set power state SUSPEND...\n"));
346 config_hook_call(CONFIG_HOOK_PMEVENT
,
347 CONFIG_HOOK_PMEVENT_HARDPOWER
,
348 (void *)PWR_SUSPEND
);
349 sc
->power_state
= APM_SYS_SUSPEND
;
351 config_hook_call_reverse(CONFIG_HOOK_PMEVENT
,
352 CONFIG_HOOK_PMEVENT_HARDPOWER
,
354 DPRINTF(("hpcapm: resume\n"));
358 DPRINTF(("hpcapm: set power state OFF\n"));
359 sc
->power_state
= APM_SYS_OFF
;
361 case APM_LASTREQ_INPROG
:
362 /*DPRINTF(("hpcapm: set power state INPROG\n"));
365 case APM_LASTREQ_REJECTED
:
366 DPRINTF(("hpcapm: set power state REJECTED\n"));
374 hpcapm_get_powstat(void *scx
, u_int batteryid
, struct apm_power_info
*pinfo
)
376 struct apmhpc_softc
*sc
;
382 pinfo
->batteryid
= 0;
383 pinfo
->minutes_valid
= 0;
384 pinfo
->minutes_left
= 0;
385 pinfo
->battery_state
= APM_BATT_UNKNOWN
; /* XXX: ignored */
387 if (config_hook_call(CONFIG_HOOK_GET
,
388 CONFIG_HOOK_ACADAPTER
, &val
) != -1)
389 pinfo
->ac_state
= val
;
391 pinfo
->ac_state
= sc
->ac_state
;
393 if (config_hook_call(CONFIG_HOOK_GET
,
394 CONFIG_HOOK_CHARGE
, &val
) != -1)
395 pinfo
->battery_flags
= val
;
397 pinfo
->battery_flags
= sc
->battery_flags
;
399 if (config_hook_call(CONFIG_HOOK_GET
,
400 CONFIG_HOOK_BATTERYVAL
, &val
) != -1)
401 pinfo
->battery_life
= val
;
403 pinfo
->battery_life
= sc
->battery_life
;
409 hpcapm_get_event(void *scx
, u_int
*event_type
, u_int
*event_info
)
411 struct apmhpc_softc
*sc
;
416 for (i
= APM_STANDBY_REQ
; i
<= APM_CAP_CHANGE
; i
++) {
417 if (sc
->events
& (1 << i
)) {
418 sc
->events
&= ~(1 << i
);
420 if (*event_type
== APM_NORMAL_RESUME
||
421 *event_type
== APM_CRIT_RESUME
) {
422 /* pccard power off in the suspend state */
424 sc
->power_state
= APM_SYS_READY
;
432 return APM_ERR_NOEVENTS
;
436 hpcapm_cpu_busy(void *scx
)
438 struct apmhpc_softc
*sc
;
444 hpcapm_cpu_idle(void *scx
)
446 struct apmhpc_softc
*sc
;
452 hpcapm_get_capabilities(void *scx
, u_int
*numbatts
, u_int
*capflags
)
454 struct apmhpc_softc
*sc
;
457 *capflags
= APM_GLOBAL_STANDBY
| APM_GLOBAL_SUSPEND
;