Merge with Linux 2.4.0-test5-pre3.
[linux-2.6/linux-mips.git] / arch / i386 / kernel / apm.c
blobb1debd5fe54e69cd585a37a110508c5c44a53214
1 /* -*- linux-c -*-
2 * APM BIOS driver for Linux
3 * Copyright 1994-2000 Stephen Rothwell (sfr@linuxcare.com)
5 * Initial development of this driver was funded by NEC Australia P/L
6 * and NEC Corporation
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * October 1995, Rik Faith (faith@cs.unc.edu):
19 * Minor enhancements and updates (to the patch set) for 1.3.x
20 * Documentation
21 * January 1996, Rik Faith (faith@cs.unc.edu):
22 * Make /proc/apm easy to format (bump driver version)
23 * March 1996, Rik Faith (faith@cs.unc.edu):
24 * Prohibit APM BIOS calls unless apm_enabled.
25 * (Thanks to Ulrich Windl <Ulrich.Windl@rz.uni-regensburg.de>)
26 * April 1996, Stephen Rothwell (Stephen.Rothwell@canb.auug.org.au)
27 * Version 1.0 and 1.1
28 * May 1996, Version 1.2
29 * Feb 1998, Version 1.3
30 * Feb 1998, Version 1.4
31 * Aug 1998, Version 1.5
32 * Sep 1998, Version 1.6
33 * Nov 1998, Version 1.7
34 * Jan 1999, Version 1.8
35 * Jan 1999, Version 1.9
36 * Oct 1999, Version 1.10
37 * Nov 1999, Version 1.11
38 * Jan 2000, Version 1.12
39 * Feb 2000, Version 1.13
41 * History:
42 * 0.6b: first version in official kernel, Linux 1.3.46
43 * 0.7: changed /proc/apm format, Linux 1.3.58
44 * 0.8: fixed gcc 2.7.[12] compilation problems, Linux 1.3.59
45 * 0.9: only call bios if bios is present, Linux 1.3.72
46 * 1.0: use fixed device number, consolidate /proc/apm into this file,
47 * Linux 1.3.85
48 * 1.1: support user-space standby and suspend, power off after system
49 * halted, Linux 1.3.98
50 * 1.2: When resetting RTC after resume, take care so that the time
51 * is only incorrect by 30-60mS (vs. 1S previously) (Gabor J. Toth
52 * <jtoth@princeton.edu>); improve interaction between
53 * screen-blanking and gpm (Stephen Rothwell); Linux 1.99.4
54 * 1.2a:Simple change to stop mysterious bug reports with SMP also added
55 * levels to the printk calls. APM is not defined for SMP machines.
56 * The new replacment for it is, but Linux doesn't yet support this.
57 * Alan Cox Linux 2.1.55
58 * 1.3: Set up a valid data descriptor 0x40 for buggy BIOS's
59 * 1.4: Upgraded to support APM 1.2. Integrated ThinkPad suspend patch by
60 * Dean Gaudet <dgaudet@arctic.org>.
61 * C. Scott Ananian <cananian@alumni.princeton.edu> Linux 2.1.87
62 * 1.5: Fix segment register reloading (in case of bad segments saved
63 * across BIOS call).
64 * Stephen Rothwell
65 * 1.6: Cope with complier/assembler differences.
66 * Only try to turn off the first display device.
67 * Fix OOPS at power off with no APM BIOS by Jan Echternach
68 * <echter@informatik.uni-rostock.de>
69 * Stephen Rothwell
70 * 1.7: Modify driver's cached copy of the disabled/disengaged flags
71 * to reflect current state of APM BIOS.
72 * Chris Rankin <rankinc@bellsouth.net>
73 * Reset interrupt 0 timer to 100Hz after suspend
74 * Chad Miller <cmiller@surfsouth.com>
75 * Add CONFIG_APM_IGNORE_SUSPEND_BOUNCE
76 * Richard Gooch <rgooch@atnf.csiro.au>
77 * Allow boot time disabling of APM
78 * Make boot messages far less verbose by default
79 * Make asm safer
80 * Stephen Rothwell
81 * 1.8: Add CONFIG_APM_RTC_IS_GMT
82 * Richard Gooch <rgooch@atnf.csiro.au>
83 * change APM_NOINTS to CONFIG_APM_ALLOW_INTS
84 * remove dependency on CONFIG_PROC_FS
85 * Stephen Rothwell
86 * 1.9: Fix small typo. <laslo@ilo.opole.pl>
87 * Try to cope with BIOS's that need to have all display
88 * devices blanked and not just the first one.
89 * Ross Paterson <ross@soi.city.ac.uk>
90 * Fix segment limit setting it has always been wrong as
91 * the segments needed to have byte granularity.
92 * Mark a few things __init.
93 * Add hack to allow power off of SMP systems by popular request.
94 * Use CONFIG_SMP instead of __SMP__
95 * Ignore BOUNCES for three seconds.
96 * Stephen Rothwell
97 * 1.10: Fix for Thinkpad return code.
98 * Merge 2.2 and 2.3 drivers.
99 * Remove APM dependencies in arch/i386/kernel/process.c
100 * Remove APM dependencies in drivers/char/sysrq.c
101 * Reset time across standby.
102 * Allow more inititialisation on SMP.
103 * Remove CONFIG_APM_POWER_OFF and make it boot time
104 * configurable (default on).
105 * Make debug only a boot time parameter (remove APM_DEBUG).
106 * Try to blank all devices on any error.
107 * 1.11: Remove APM dependencies in drivers/char/console.c
108 * Check nr_running to detect if we are idle (from
109 * Borislav Deianov <borislav@lix.polytechnique.fr>)
110 * Fix for bioses that don't zero the top part of the
111 * entrypoint offset (Mario Sitta <sitta@al.unipmn.it>)
112 * (reported by Panos Katsaloulis <teras@writeme.com>).
113 * Real mode power off patch (Walter Hofmann
114 * <Walter.Hofmann@physik.stud.uni-erlangen.de>).
115 * 1.12: Remove CONFIG_SMP as the compiler will optimize
116 * the code away anyway (smp_num_cpus == 1 in UP)
117 * noted by Artur Skawina <skawina@geocities.com>.
118 * Make power off under SMP work again.
119 * Fix thinko with initial engaging of BIOS.
120 * Make sure power off only happens on CPU 0
121 * (Paul "Rusty" Russell <rusty@linuxcare.com>).
122 * Do error notification to user mode if BIOS calls fail.
123 * Move entrypoint offset fix to ...boot/setup.S
124 * where it belongs (Cosmos <gis88564@cis.nctu.edu.tw>).
125 * Remove smp-power-off. SMP users must now specify
126 * "apm=power-off" on the kernel command line. Suggested
127 * by Jim Avera <jima@hal.com>, modified by Alan Cox
128 * <alan@lxorguk.ukuu.org.uk>.
129 * Register the /proc/apm entry even on SMP so that
130 * scripts that check for it before doing power off
131 * work (Jim Avera <jima@hal.com>).
132 * 1.13: Changes for new pm_ interfaces (Andy Henroid
133 * <andy_henroid@yahoo.com>).
134 * Modularize the code.
135 * Fix the Thinkpad (again) :-( (CONFIG_APM_IGNORE_MULTIPLE_SUSPENDS
136 * is now the way life works).
137 * Fix thinko in suspend() (wrong return).
138 * Notify drivers on critical suspend.
139 * Make kapmd absorb more idle time (Pavel Machek <pavel@suse.cz>
140 * modified by sfr).
141 * Disable interrupts while we are suspended (Andy Henroid
142 * <andy_henroid@yahoo.com> fixed by sfr).
143 * Make power off work on SMP again (Tony Hoyle
144 * <tmh@magenta-logic.com> and <zlatko@iskon.hr>) modified by sfr.
145 * Remove CONFIG_APM_SUSPEND_BOUNCE. The bounce ignore
146 * interval is now configurable.
148 * APM 1.1 Reference:
150 * Intel Corporation, Microsoft Corporation. Advanced Power Management
151 * (APM) BIOS Interface Specification, Revision 1.1, September 1993.
152 * Intel Order Number 241704-001. Microsoft Part Number 781-110-X01.
154 * [This document is available free from Intel by calling 800.628.8686 (fax
155 * 916.356.6100) or 800.548.4725; or via anonymous ftp from
156 * ftp://ftp.intel.com/pub/IAL/software_specs/apmv11.doc. It is also
157 * available from Microsoft by calling 206.882.8080.]
159 * APM 1.2 Reference:
160 * Intel Corporation, Microsoft Corporation. Advanced Power Management
161 * (APM) BIOS Interface Specification, Revision 1.2, February 1996.
163 * [This document is available from Microsoft at:
164 * http://www.microsoft.com/hwdev/busbios/amp_12.htm]
167 #include <linux/config.h>
168 #include <linux/module.h>
170 #include <linux/poll.h>
171 #include <linux/types.h>
172 #include <linux/stddef.h>
173 #include <linux/timer.h>
174 #include <linux/fcntl.h>
175 #include <linux/malloc.h>
176 #include <linux/stat.h>
177 #include <linux/proc_fs.h>
178 #include <linux/miscdevice.h>
179 #include <linux/apm_bios.h>
180 #include <linux/init.h>
181 #include <linux/sched.h>
182 #include <linux/pm.h>
183 #include <linux/kernel.h>
184 #include <linux/smp_lock.h>
186 #include <asm/system.h>
187 #include <asm/uaccess.h>
188 #include <asm/desc.h>
190 extern unsigned long get_cmos_time(void);
191 extern void machine_real_restart(unsigned char *, int);
193 #ifdef CONFIG_MAGIC_SYSRQ
194 extern void (*sysrq_power_off)(void);
195 #endif
196 #if defined(CONFIG_APM_DISPLAY_BLANK) && defined(CONFIG_VT)
197 extern int (*console_blank_hook)(int);
198 #endif
201 * The apm_bios device is one of the misc char devices.
202 * This is its minor number.
204 #define APM_MINOR_DEV 134
207 * See Documentation/Config.help for the configuration options.
209 * Various options can be changed at boot time as follows:
210 * (We allow underscores for compatibility with the modules code)
211 * apm=on/off enable/disable APM
212 * [no-]debug log some debugging messages
213 * [no-]power[-_]off power off on shutdown
216 /* KNOWN PROBLEM MACHINES:
218 * U: TI 4000M TravelMate: BIOS is *NOT* APM compliant
219 * [Confirmed by TI representative]
220 * ?: ACER 486DX4/75: uses dseg 0040, in violation of APM specification
221 * [Confirmed by BIOS disassembly]
222 * [This may work now ...]
223 * P: Toshiba 1950S: battery life information only gets updated after resume
224 * P: Midwest Micro Soundbook Elite DX2/66 monochrome: screen blanking
225 * broken in BIOS [Reported by Garst R. Reese <reese@isn.net>]
227 * Legend: U = unusable with APM patches
228 * P = partially usable with APM patches
232 * Define to always call the APM BIOS busy routine even if the clock was
233 * not slowed by the idle routine.
235 #define ALWAYS_CALL_BUSY
238 * Define to make the APM BIOS calls zero all data segment registers (so
239 * that an incorrect BIOS implementation will cause a kernel panic if it
240 * tries to write to arbitrary memory).
242 #define APM_ZERO_SEGS
245 * Define to make all _set_limit calls use 64k limits. The APM 1.1 BIOS is
246 * supposed to provide limit information that it recognizes. Many machines
247 * do this correctly, but many others do not restrict themselves to their
248 * claimed limit. When this happens, they will cause a segmentation
249 * violation in the kernel at boot time. Most BIOS's, however, will
250 * respect a 64k limit, so we use that. If you want to be pedantic and
251 * hold your BIOS to its claims, then undefine this.
253 #define APM_RELAX_SEGMENTS
256 * Define to re-initialize the interrupt 0 timer to 100 Hz after a suspend.
257 * This patched by Chad Miller <cmiller@surfsouth.com>, original code by
258 * David Chen <chen@ctpa04.mit.edu>
260 #undef INIT_TIMER_AFTER_SUSPEND
262 #ifdef INIT_TIMER_AFTER_SUSPEND
263 #include <linux/timex.h>
264 #include <asm/io.h>
265 #include <linux/delay.h>
266 #endif
269 * Need to poll the APM BIOS every second
271 #define APM_CHECK_TIMEOUT (HZ)
274 * Ignore suspend events for this amount of time after a resume
276 #define DEFAULT_BOUNCE_INTERVAL (3 * HZ)
279 * Save a segment register away
281 #define savesegment(seg, where) \
282 __asm__ __volatile__("movl %%" #seg ",%0" : "=m" (where))
285 * Maximum number of events stored
287 #define APM_MAX_EVENTS 20
290 * The per-file APM data
292 struct apm_user {
293 int magic;
294 struct apm_user * next;
295 int suser: 1;
296 int suspend_wait: 1;
297 int suspend_result;
298 int suspends_pending;
299 int standbys_pending;
300 int suspends_read;
301 int standbys_read;
302 int event_head;
303 int event_tail;
304 apm_event_t events[APM_MAX_EVENTS];
308 * The magic number in apm_user
310 #define APM_BIOS_MAGIC 0x4101
313 * Local variables
315 static struct {
316 unsigned long offset;
317 unsigned short segment;
318 } apm_bios_entry;
319 #ifdef CONFIG_APM_CPU_IDLE
320 static int clock_slowed;
321 #endif
322 static int suspends_pending;
323 static int standbys_pending;
324 static int waiting_for_resume;
325 static int ignore_normal_resume;
326 static int bounce_interval = DEFAULT_BOUNCE_INTERVAL;
328 #ifdef CONFIG_APM_RTC_IS_GMT
329 # define clock_cmos_diff 0
330 # define got_clock_diff 1
331 #else
332 static long clock_cmos_diff;
333 static int got_clock_diff;
334 #endif
335 static int debug;
336 static int apm_disabled;
337 #ifdef CONFIG_SMP
338 static int power_off;
339 #else
340 static int power_off = 1;
341 #endif
342 static int exit_kapmd;
343 static int kapmd_running;
345 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);
346 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);
347 static struct apm_user * user_list = NULL;
349 static char driver_version[] = "1.13"; /* no spaces */
351 static char * apm_event_name[] = {
352 "system standby",
353 "system suspend",
354 "normal resume",
355 "critical resume",
356 "low battery",
357 "power status change",
358 "update time",
359 "critical suspend",
360 "user standby",
361 "user suspend",
362 "system standby resume",
363 "capabilities change"
365 #define NR_APM_EVENT_NAME \
366 (sizeof(apm_event_name) / sizeof(apm_event_name[0]))
368 typedef struct lookup_t {
369 int key;
370 char * msg;
371 } lookup_t;
373 static const lookup_t error_table[] = {
374 /* N/A { APM_SUCCESS, "Operation succeeded" }, */
375 { APM_DISABLED, "Power management disabled" },
376 { APM_CONNECTED, "Real mode interface already connected" },
377 { APM_NOT_CONNECTED, "Interface not connected" },
378 { APM_16_CONNECTED, "16 bit interface already connected" },
379 /* N/A { APM_16_UNSUPPORTED, "16 bit interface not supported" }, */
380 { APM_32_CONNECTED, "32 bit interface already connected" },
381 { APM_32_UNSUPPORTED, "32 bit interface not supported" },
382 { APM_BAD_DEVICE, "Unrecognized device ID" },
383 { APM_BAD_PARAM, "Parameter out of range" },
384 { APM_NOT_ENGAGED, "Interface not engaged" },
385 { APM_BAD_FUNCTION, "Function not supported" },
386 { APM_RESUME_DISABLED, "Resume timer disabled" },
387 { APM_BAD_STATE, "Unable to enter requested state" },
388 /* N/A { APM_NO_EVENTS, "No events pending" }, */
389 { APM_NO_ERROR, "BIOS did not set a return code" },
390 { APM_NOT_PRESENT, "No APM present" }
392 #define ERROR_COUNT (sizeof(error_table)/sizeof(lookup_t))
395 * These are the actual BIOS calls. Depending on APM_ZERO_SEGS and
396 * CONFIG_APM_ALLOW_INTS, we are being really paranoid here! Not only
397 * are interrupts disabled, but all the segment registers (except SS)
398 * are saved and zeroed this means that if the BIOS tries to reference
399 * any data without explicitly loading the segment registers, the kernel
400 * will fault immediately rather than have some unforeseen circumstances
401 * for the rest of the kernel. And it will be very obvious! :-) Doing
402 * this depends on CS referring to the same physical memory as DS so that
403 * DS can be zeroed before the call. Unfortunately, we can't do anything
404 * about the stack segment/pointer. Also, we tell the compiler that
405 * everything could change.
407 * Also, we KNOW that for the non error case of apm_bios_call, there
408 * is no useful data returned in the low order 8 bits of eax.
410 #ifndef CONFIG_APM_ALLOW_INTS
411 # define APM_DO_CLI __cli()
412 #else
413 # define APM_DO_CLI
414 #endif
415 #ifdef APM_ZERO_SEGS
416 # define APM_DECL_SEGS \
417 unsigned int saved_fs; unsigned int saved_gs;
418 # define APM_DO_SAVE_SEGS \
419 savesegment(fs, saved_fs); savesegment(gs, saved_gs)
420 # define APM_DO_ZERO_SEGS \
421 "pushl %%ds\n\t" \
422 "pushl %%es\n\t" \
423 "xorl %%edx, %%edx\n\t" \
424 "mov %%dx, %%ds\n\t" \
425 "mov %%dx, %%es\n\t" \
426 "mov %%dx, %%fs\n\t" \
427 "mov %%dx, %%gs\n\t"
428 # define APM_DO_POP_SEGS \
429 "popl %%es\n\t" \
430 "popl %%ds\n\t"
431 # define APM_DO_RESTORE_SEGS \
432 loadsegment(fs, saved_fs); loadsegment(gs, saved_gs)
433 #else
434 # define APM_DECL_SEGS
435 # define APM_DO_SAVE_SEGS
436 # define APM_DO_ZERO_SEGS
437 # define APM_DO_POP_SEGS
438 # define APM_DO_RESTORE_SEGS
439 #endif
441 static u8 apm_bios_call(u32 func, u32 ebx_in, u32 ecx_in,
442 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, u32 *esi)
444 APM_DECL_SEGS
445 unsigned long flags;
447 __save_flags(flags);
448 APM_DO_CLI;
449 APM_DO_SAVE_SEGS;
451 * N.B. We do NOT need a cld after the BIOS call
452 * because we always save and restore the flags.
454 __asm__ __volatile__(APM_DO_ZERO_SEGS
455 "pushl %%edi\n\t"
456 "pushl %%ebp\n\t"
457 "lcall %%cs:" SYMBOL_NAME_STR(apm_bios_entry) "\n\t"
458 "setc %%al\n\t"
459 "popl %%ebp\n\t"
460 "popl %%edi\n\t"
461 APM_DO_POP_SEGS
462 : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx),
463 "=S" (*esi)
464 : "a" (func), "b" (ebx_in), "c" (ecx_in)
465 : "memory", "cc");
466 APM_DO_RESTORE_SEGS;
467 __restore_flags(flags);
468 return *eax & 0xff;
472 * This version only returns one value (usually an error code)
475 static u8 apm_bios_call_simple(u32 func, u32 ebx_in, u32 ecx_in, u32 *eax)
477 u8 error;
478 APM_DECL_SEGS
479 unsigned long flags;
481 __save_flags(flags);
482 APM_DO_CLI;
483 APM_DO_SAVE_SEGS;
485 int cx, dx, si;
488 * N.B. We do NOT need a cld after the BIOS call
489 * because we always save and restore the flags.
491 __asm__ __volatile__(APM_DO_ZERO_SEGS
492 "pushl %%edi\n\t"
493 "pushl %%ebp\n\t"
494 "lcall %%cs:" SYMBOL_NAME_STR(apm_bios_entry) "\n\t"
495 "setc %%bl\n\t"
496 "popl %%ebp\n\t"
497 "popl %%edi\n\t"
498 APM_DO_POP_SEGS
499 : "=a" (*eax), "=b" (error), "=c" (cx), "=d" (dx),
500 "=S" (si)
501 : "a" (func), "b" (ebx_in), "c" (ecx_in)
502 : "memory", "cc");
504 APM_DO_RESTORE_SEGS;
505 __restore_flags(flags);
506 return error;
509 static int __init apm_driver_version(u_short *val)
511 u32 eax;
513 if (apm_bios_call_simple(APM_FUNC_VERSION, 0, *val, &eax))
514 return (eax >> 8) & 0xff;
515 *val = eax;
516 return APM_SUCCESS;
519 static int apm_get_event(apm_event_t *event, apm_eventinfo_t *info)
521 u32 eax;
522 u32 ebx;
523 u32 ecx;
524 u32 dummy;
526 if (apm_bios_call(APM_FUNC_GET_EVENT, 0, 0, &eax, &ebx, &ecx,
527 &dummy, &dummy))
528 return (eax >> 8) & 0xff;
529 *event = ebx;
530 if (apm_bios_info.version < 0x0102)
531 *info = ~0; /* indicate info not valid */
532 else
533 *info = ecx;
534 return APM_SUCCESS;
537 static int set_power_state(u_short what, u_short state)
539 u32 eax;
541 if (apm_bios_call_simple(APM_FUNC_SET_STATE, what, state, &eax))
542 return (eax >> 8) & 0xff;
543 return APM_SUCCESS;
546 static int apm_set_power_state(u_short state)
548 return set_power_state(APM_DEVICE_ALL, state);
551 #ifdef CONFIG_APM_CPU_IDLE
552 static int apm_do_idle(void)
554 u32 dummy;
556 if (apm_bios_call_simple(APM_FUNC_IDLE, 0, 0, &dummy))
557 return 0;
559 #ifdef ALWAYS_CALL_BUSY
560 clock_slowed = 1;
561 #else
562 clock_slowed = (apm_bios_info.flags & APM_IDLE_SLOWS_CLOCK) != 0;
563 #endif
564 return 1;
567 static void apm_do_busy(void)
569 u32 dummy;
571 if (clock_slowed) {
572 (void) apm_bios_call_simple(APM_FUNC_BUSY, 0, 0, &dummy);
573 clock_slowed = 0;
577 #if 0
578 extern int hlt_counter;
581 * If no process has been interested in this
582 * CPU for some time, we want to wake up the
583 * power management thread - we probably want
584 * to conserve power.
586 #define HARD_IDLE_TIMEOUT (HZ/3)
588 /* This should wake up kapmd and ask it to slow the CPU */
589 #define powermanagement_idle() do { } while (0)
592 * This is the idle thing.
594 static void apm_cpu_idle(void)
596 unsigned int start_idle;
598 start_idle = jiffies;
599 while (1) {
600 if (!current->need_resched) {
601 if (jiffies - start_idle < HARD_IDLE_TIMEOUT) {
602 if (!current_cpu_data.hlt_works_ok)
603 continue;
604 if (hlt_counter)
605 continue;
606 __cli();
607 if (!current->need_resched)
608 safe_halt();
609 else
610 __sti();
611 continue;
615 * Ok, do some power management - we've been idle for too long
617 powermanagement_idle();
620 schedule();
621 check_pgt_cache();
622 start_idle = jiffies;
625 #endif
626 #endif
628 #ifdef CONFIG_SMP
629 static int apm_magic(void * unused)
631 while (1)
632 schedule();
634 #endif
636 static void apm_power_off(void)
638 #ifdef CONFIG_APM_REAL_MODE_POWER_OFF
639 unsigned char po_bios_call[] = {
640 0xb8, 0x00, 0x10, /* movw $0x1000,ax */
641 0x8e, 0xd0, /* movw ax,ss */
642 0xbc, 0x00, 0xf0, /* movw $0xf000,sp */
643 0xb8, 0x07, 0x53, /* movw $0x5307,ax */
644 0xbb, 0x01, 0x00, /* movw $0x0001,bx */
645 0xb9, 0x03, 0x00, /* movw $0x0003,cx */
646 0xcd, 0x15 /* int $0x15 */
648 #endif
651 * This may be called on an SMP machine.
653 #ifdef CONFIG_SMP
654 /* Some bioses don't like being called from CPU != 0 */
655 while (cpu_number_map(smp_processor_id()) != 0) {
656 kernel_thread(apm_magic, NULL,
657 CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD);
658 schedule();
660 #endif
661 #ifdef CONFIG_APM_REAL_MODE_POWER_OFF
662 machine_real_restart(po_bios_call, sizeof(po_bios_call));
663 #else
664 (void) apm_set_power_state(APM_STATE_OFF);
665 #endif
668 #ifdef CONFIG_APM_DO_ENABLE
669 static int apm_enable_power_management(int enable)
671 u32 eax;
673 if ((enable == 0) && (apm_bios_info.flags & APM_BIOS_DISENGAGED))
674 return APM_NOT_ENGAGED;
675 if (apm_bios_call_simple(APM_FUNC_ENABLE_PM, APM_DEVICE_BALL,
676 enable, &eax))
677 return (eax >> 8) & 0xff;
678 if (enable)
679 apm_bios_info.flags &= ~APM_BIOS_DISABLED;
680 else
681 apm_bios_info.flags |= APM_BIOS_DISABLED;
682 return APM_SUCCESS;
684 #endif
686 static int apm_get_power_status(u_short *status, u_short *bat, u_short *life)
688 u32 eax;
689 u32 ebx;
690 u32 ecx;
691 u32 edx;
692 u32 dummy;
694 if (apm_bios_call(APM_FUNC_GET_STATUS, APM_DEVICE_ALL, 0,
695 &eax, &ebx, &ecx, &edx, &dummy))
696 return (eax >> 8) & 0xff;
697 *status = ebx;
698 *bat = ecx;
699 *life = edx;
700 return APM_SUCCESS;
703 #if 0
704 static int apm_get_battery_status(u_short which, u_short *status,
705 u_short *bat, u_short *life, u_short *nbat)
707 u32 eax;
708 u32 ebx;
709 u32 ecx;
710 u32 edx;
711 u32 esi;
713 if (apm_bios_info.version < 0x0102) {
714 /* pretend we only have one battery. */
715 if (which != 1)
716 return APM_BAD_DEVICE;
717 *nbat = 1;
718 return apm_get_power_status(status, bat, life);
721 if (apm_bios_call(APM_FUNC_GET_STATUS, (0x8000 | (which)), 0, &eax,
722 &ebx, &ecx, &edx, &esi))
723 return (eax >> 8) & 0xff;
724 *status = ebx;
725 *bat = ecx;
726 *life = edx;
727 *nbat = esi;
728 return APM_SUCCESS;
730 #endif
732 static int apm_engage_power_management(u_short device, int enable)
734 u32 eax;
736 if ((enable == 0) && (device == APM_DEVICE_ALL)
737 && (apm_bios_info.flags & APM_BIOS_DISABLED))
738 return APM_DISABLED;
739 if (apm_bios_call_simple(APM_FUNC_ENGAGE_PM, device, enable, &eax))
740 return (eax >> 8) & 0xff;
741 if (device == APM_DEVICE_ALL) {
742 if (enable)
743 apm_bios_info.flags &= ~APM_BIOS_DISENGAGED;
744 else
745 apm_bios_info.flags |= APM_BIOS_DISENGAGED;
747 return APM_SUCCESS;
750 static void apm_error(char *str, int err)
752 int i;
754 for (i = 0; i < ERROR_COUNT; i++)
755 if (error_table[i].key == err) break;
756 if (i < ERROR_COUNT)
757 printk(KERN_NOTICE "apm: %s: %s\n", str, error_table[i].msg);
758 else
759 printk(KERN_NOTICE "apm: %s: unknown error code %#2.2x\n",
760 str, err);
763 #if defined(CONFIG_APM_DISPLAY_BLANK) && defined(CONFIG_VT)
764 static int apm_console_blank(int blank)
766 int error;
767 u_short state;
769 state = blank ? APM_STATE_STANDBY : APM_STATE_READY;
770 /* Blank the first display device */
771 error = set_power_state(0x100, state);
772 if ((error != APM_SUCCESS) && (error != APM_NO_ERROR)) {
773 /* try to blank them all instead */
774 error = set_power_state(0x1ff, state);
775 if ((error != APM_SUCCESS) && (error != APM_NO_ERROR))
776 /* try to blank device one instead */
777 error = set_power_state(0x101, state);
779 if ((error == APM_SUCCESS) || (error == APM_NO_ERROR))
780 return 1;
781 apm_error("set display", error);
782 return 0;
784 #endif
786 static int queue_empty(struct apm_user *as)
788 return as->event_head == as->event_tail;
791 static apm_event_t get_queued_event(struct apm_user *as)
793 as->event_tail = (as->event_tail + 1) % APM_MAX_EVENTS;
794 return as->events[as->event_tail];
797 static void queue_event(apm_event_t event, struct apm_user *sender)
799 struct apm_user * as;
801 if (user_list == NULL)
802 return;
803 for (as = user_list; as != NULL; as = as->next) {
804 if (as == sender)
805 continue;
806 as->event_head = (as->event_head + 1) % APM_MAX_EVENTS;
807 if (as->event_head == as->event_tail) {
808 static int notified;
810 if (notified++ == 0)
811 printk(KERN_ERR "apm: an event queue overflowed\n");
812 as->event_tail = (as->event_tail + 1) % APM_MAX_EVENTS;
814 as->events[as->event_head] = event;
815 if (!as->suser)
816 continue;
817 switch (event) {
818 case APM_SYS_SUSPEND:
819 case APM_USER_SUSPEND:
820 as->suspends_pending++;
821 suspends_pending++;
822 break;
824 case APM_SYS_STANDBY:
825 case APM_USER_STANDBY:
826 as->standbys_pending++;
827 standbys_pending++;
828 break;
831 wake_up_interruptible(&apm_waitqueue);
834 static void set_time(void)
836 unsigned long flags;
838 if (got_clock_diff) { /* Must know time zone in order to set clock */
839 save_flags(flags);
840 cli();
841 CURRENT_TIME = get_cmos_time() + clock_cmos_diff;
842 restore_flags(flags);
846 static void get_time_diff(void)
848 #ifndef CONFIG_APM_RTC_IS_GMT
849 unsigned long flags;
852 * Estimate time zone so that set_time can update the clock
854 save_flags(flags);
855 clock_cmos_diff = -get_cmos_time();
856 cli();
857 clock_cmos_diff += CURRENT_TIME;
858 got_clock_diff = 1;
859 restore_flags(flags);
860 #endif
863 static void reinit_timer(void)
865 #ifdef INIT_TIMER_AFTER_SUSPEND
866 unsigned long flags;
868 save_flags(flags);
869 cli();
870 /* set the clock to 100 Hz */
871 outb_p(0x34,0x43); /* binary, mode 2, LSB/MSB, ch 0 */
872 udelay(10);
873 outb_p(LATCH & 0xff , 0x40); /* LSB */
874 udelay(10);
875 outb(LATCH >> 8 , 0x40); /* MSB */
876 udelay(10);
877 restore_flags(flags);
878 #endif
881 static int send_event(apm_event_t event)
883 switch (event) {
884 case APM_SYS_SUSPEND:
885 case APM_CRITICAL_SUSPEND:
886 case APM_USER_SUSPEND:
887 /* map all suspends to ACPI D3 */
888 if (pm_send_all(PM_SUSPEND, (void *)3)) {
889 if (event == APM_CRITICAL_SUSPEND) {
890 printk(KERN_CRIT "apm: Critical suspend was vetoed, expect armageddon\n" );
891 return 0;
893 if (apm_bios_info.version > 0x100)
894 apm_set_power_state(APM_STATE_REJECT);
895 return 0;
897 break;
898 case APM_NORMAL_RESUME:
899 case APM_CRITICAL_RESUME:
900 /* map all resumes to ACPI D0 */
901 (void) pm_send_all(PM_RESUME, (void *)0);
902 break;
905 return 1;
908 static int suspend(void)
910 int err;
911 struct apm_user *as;
913 get_time_diff();
914 cli();
915 err = apm_set_power_state(APM_STATE_SUSPEND);
916 reinit_timer();
917 set_time();
918 if (err == APM_NO_ERROR)
919 err = APM_SUCCESS;
920 if (err != APM_SUCCESS)
921 apm_error("suspend", err);
922 send_event(APM_NORMAL_RESUME);
923 sti();
924 queue_event(APM_NORMAL_RESUME, NULL);
925 for (as = user_list; as != NULL; as = as->next) {
926 as->suspend_wait = 0;
927 as->suspend_result = ((err == APM_SUCCESS) ? 0 : -EIO);
929 ignore_normal_resume = 1;
930 wake_up_interruptible(&apm_suspend_waitqueue);
931 return err;
934 static void standby(void)
936 int err;
938 get_time_diff();
939 err = apm_set_power_state(APM_STATE_STANDBY);
940 if ((err != APM_SUCCESS) && (err != APM_NO_ERROR))
941 apm_error("standby", err);
944 static apm_event_t get_event(void)
946 int error;
947 apm_event_t event;
948 apm_eventinfo_t info;
950 static int notified;
952 /* we don't use the eventinfo */
953 error = apm_get_event(&event, &info);
954 if (error == APM_SUCCESS)
955 return event;
957 if ((error != APM_NO_EVENTS) && (notified++ == 0))
958 apm_error("get_event", error);
960 return 0;
963 static void check_events(void)
965 apm_event_t event;
966 static unsigned long last_resume;
967 static int ignore_bounce;
969 while ((event = get_event()) != 0) {
970 if (debug) {
971 if (event <= NR_APM_EVENT_NAME)
972 printk(KERN_DEBUG "apm: received %s notify\n",
973 apm_event_name[event - 1]);
974 else
975 printk(KERN_DEBUG "apm: received unknown "
976 "event 0x%02x\n", event);
978 if (ignore_bounce
979 && ((jiffies - last_resume) > bounce_interval))
980 ignore_bounce = 0;
981 if (ignore_normal_resume && (event != APM_NORMAL_RESUME))
982 ignore_normal_resume = 0;
984 switch (event) {
985 case APM_SYS_STANDBY:
986 case APM_USER_STANDBY:
987 if (send_event(event)) {
988 queue_event(event, NULL);
989 if (standbys_pending <= 0)
990 standby();
992 break;
994 case APM_USER_SUSPEND:
995 #ifdef CONFIG_APM_IGNORE_USER_SUSPEND
996 if (apm_bios_info.version > 0x100)
997 apm_set_power_state(APM_STATE_REJECT);
998 break;
999 #endif
1000 case APM_SYS_SUSPEND:
1001 if (ignore_bounce) {
1002 if (apm_bios_info.version > 0x100)
1003 apm_set_power_state(APM_STATE_REJECT);
1004 break;
1007 * If we are already processing a SUSPEND,
1008 * then further SUSPEND events from the BIOS
1009 * will be ignored. We also return here to
1010 * cope with the fact that the Thinkpads keep
1011 * sending a SUSPEND event until something else
1012 * happens!
1014 if (waiting_for_resume)
1015 return;
1016 if (send_event(event)) {
1017 queue_event(event, NULL);
1018 waiting_for_resume = 1;
1019 if (suspends_pending <= 0)
1020 (void) suspend();
1022 break;
1024 case APM_NORMAL_RESUME:
1025 case APM_CRITICAL_RESUME:
1026 case APM_STANDBY_RESUME:
1027 waiting_for_resume = 0;
1028 last_resume = jiffies;
1029 ignore_bounce = 1;
1030 if ((event != APM_NORMAL_RESUME)
1031 || (ignore_normal_resume == 0)) {
1032 set_time();
1033 send_event(event);
1034 queue_event(event, NULL);
1036 break;
1038 case APM_CAPABILITY_CHANGE:
1039 case APM_LOW_BATTERY:
1040 case APM_POWER_STATUS_CHANGE:
1041 send_event(event);
1042 queue_event(event, NULL);
1043 break;
1045 case APM_UPDATE_TIME:
1046 set_time();
1047 break;
1049 case APM_CRITICAL_SUSPEND:
1050 send_event(event);
1052 * We can only hope it worked - we are not allowed
1053 * to reject a critical suspend.
1055 (void) suspend();
1056 break;
1061 static void apm_event_handler(void)
1063 static int pending_count = 4;
1064 int err;
1066 if ((standbys_pending > 0) || (suspends_pending > 0)) {
1067 if ((apm_bios_info.version > 0x100) && (pending_count-- <= 0)) {
1068 pending_count = 4;
1069 if (debug)
1070 printk(KERN_DEBUG "apm: setting state busy\n");
1071 err = apm_set_power_state(APM_STATE_BUSY);
1072 if (err)
1073 apm_error("busy", err);
1075 } else
1076 pending_count = 4;
1077 check_events();
1081 * This is the APM thread main loop.
1083 * Check whether we're the only running process to
1084 * decide if we should just power down.
1087 #define system_idle() (nr_running == 1)
1089 static void apm_mainloop(void)
1091 int timeout = HZ;
1092 DECLARE_WAITQUEUE(wait, current);
1094 add_wait_queue(&apm_waitqueue, &wait);
1095 set_current_state(TASK_INTERRUPTIBLE);
1096 for (;;) {
1097 /* Nothing to do, just sleep for the timeout */
1098 timeout = 2*timeout;
1099 if (timeout > APM_CHECK_TIMEOUT)
1100 timeout = APM_CHECK_TIMEOUT;
1101 schedule_timeout(timeout);
1102 if (exit_kapmd)
1103 break;
1106 * Ok, check all events, check for idle (and mark us sleeping
1107 * so as not to count towards the load average)..
1109 set_current_state(TASK_INTERRUPTIBLE);
1110 apm_event_handler();
1111 #ifdef CONFIG_APM_CPU_IDLE
1112 if (!system_idle())
1113 continue;
1114 if (apm_do_idle()) {
1115 unsigned long start = jiffies;
1116 while ((!exit_kapmd) && system_idle()) {
1117 apm_do_idle();
1118 if ((jiffies - start) > APM_CHECK_TIMEOUT) {
1119 apm_event_handler();
1120 start = jiffies;
1123 apm_do_busy();
1124 apm_event_handler();
1125 timeout = 1;
1127 #endif
1131 static int check_apm_user(struct apm_user *as, const char *func)
1133 if ((as == NULL) || (as->magic != APM_BIOS_MAGIC)) {
1134 printk(KERN_ERR "apm: %s passed bad filp\n", func);
1135 return 1;
1137 return 0;
1140 static ssize_t do_read(struct file *fp, char *buf, size_t count, loff_t *ppos)
1142 struct apm_user * as;
1143 int i;
1144 apm_event_t event;
1145 DECLARE_WAITQUEUE(wait, current);
1147 as = fp->private_data;
1148 if (check_apm_user(as, "read"))
1149 return -EIO;
1150 if (count < sizeof(apm_event_t))
1151 return -EINVAL;
1152 if (queue_empty(as)) {
1153 if (fp->f_flags & O_NONBLOCK)
1154 return -EAGAIN;
1155 add_wait_queue(&apm_waitqueue, &wait);
1156 repeat:
1157 set_current_state(TASK_INTERRUPTIBLE);
1158 if (queue_empty(as) && !signal_pending(current)) {
1159 schedule();
1160 goto repeat;
1162 set_current_state(TASK_RUNNING);
1163 remove_wait_queue(&apm_waitqueue, &wait);
1165 i = count;
1166 while ((i >= sizeof(event)) && !queue_empty(as)) {
1167 event = get_queued_event(as);
1168 if (copy_to_user(buf, &event, sizeof(event))) {
1169 if (i < count)
1170 break;
1171 return -EFAULT;
1173 switch (event) {
1174 case APM_SYS_SUSPEND:
1175 case APM_USER_SUSPEND:
1176 as->suspends_read++;
1177 break;
1179 case APM_SYS_STANDBY:
1180 case APM_USER_STANDBY:
1181 as->standbys_read++;
1182 break;
1184 buf += sizeof(event);
1185 i -= sizeof(event);
1187 if (i < count)
1188 return count - i;
1189 if (signal_pending(current))
1190 return -ERESTARTSYS;
1191 return 0;
1194 static unsigned int do_poll(struct file *fp, poll_table * wait)
1196 struct apm_user * as;
1198 as = fp->private_data;
1199 if (check_apm_user(as, "poll"))
1200 return 0;
1201 poll_wait(fp, &apm_waitqueue, wait);
1202 if (!queue_empty(as))
1203 return POLLIN | POLLRDNORM;
1204 return 0;
1207 static int do_ioctl(struct inode * inode, struct file *filp,
1208 u_int cmd, u_long arg)
1210 struct apm_user * as;
1211 DECLARE_WAITQUEUE(wait, current);
1213 as = filp->private_data;
1214 if (check_apm_user(as, "ioctl"))
1215 return -EIO;
1216 if (!as->suser)
1217 return -EPERM;
1218 switch (cmd) {
1219 case APM_IOC_STANDBY:
1220 if (as->standbys_read > 0) {
1221 as->standbys_read--;
1222 as->standbys_pending--;
1223 standbys_pending--;
1224 } else if (!send_event(APM_USER_STANDBY))
1225 return -EAGAIN;
1226 else
1227 queue_event(APM_USER_STANDBY, as);
1228 if (standbys_pending <= 0)
1229 standby();
1230 break;
1231 case APM_IOC_SUSPEND:
1232 if (as->suspends_read > 0) {
1233 as->suspends_read--;
1234 as->suspends_pending--;
1235 suspends_pending--;
1236 } else if (!send_event(APM_USER_SUSPEND))
1237 return -EAGAIN;
1238 else
1239 queue_event(APM_USER_SUSPEND, as);
1240 if (suspends_pending <= 0) {
1241 if (suspend() != APM_SUCCESS)
1242 return -EIO;
1243 } else {
1244 as->suspend_wait = 1;
1245 add_wait_queue(&apm_suspend_waitqueue, &wait);
1246 while (1) {
1247 set_current_state(TASK_INTERRUPTIBLE);
1248 if ((as->suspend_wait == 0)
1249 || signal_pending(current))
1250 break;
1251 schedule();
1253 set_current_state(TASK_RUNNING);
1254 remove_wait_queue(&apm_suspend_waitqueue, &wait);
1255 return as->suspend_result;
1257 break;
1258 default:
1259 return -EINVAL;
1261 return 0;
1264 static int do_release(struct inode * inode, struct file * filp)
1266 struct apm_user * as;
1268 as = filp->private_data;
1269 if (check_apm_user(as, "release"))
1270 return 0;
1271 filp->private_data = NULL;
1272 lock_kernel();
1273 if (as->standbys_pending > 0) {
1274 standbys_pending -= as->standbys_pending;
1275 if (standbys_pending <= 0)
1276 standby();
1278 if (as->suspends_pending > 0) {
1279 suspends_pending -= as->suspends_pending;
1280 if (suspends_pending <= 0)
1281 (void) suspend();
1283 if (user_list == as)
1284 user_list = as->next;
1285 else {
1286 struct apm_user * as1;
1288 for (as1 = user_list;
1289 (as1 != NULL) && (as1->next != as);
1290 as1 = as1->next)
1292 if (as1 == NULL)
1293 printk(KERN_ERR "apm: filp not in user list\n");
1294 else
1295 as1->next = as->next;
1297 unlock_kernel();
1298 kfree(as);
1299 return 0;
1302 static int do_open(struct inode * inode, struct file * filp)
1304 struct apm_user * as;
1306 as = (struct apm_user *)kmalloc(sizeof(*as), GFP_KERNEL);
1307 if (as == NULL) {
1308 printk(KERN_ERR "apm: cannot allocate struct of size %d bytes\n",
1309 sizeof(*as));
1310 return -ENOMEM;
1312 as->magic = APM_BIOS_MAGIC;
1313 as->event_tail = as->event_head = 0;
1314 as->suspends_pending = as->standbys_pending = 0;
1315 as->suspends_read = as->standbys_read = 0;
1317 * XXX - this is a tiny bit broken, when we consider BSD
1318 * process accounting. If the device is opened by root, we
1319 * instantly flag that we used superuser privs. Who knows,
1320 * we might close the device immediately without doing a
1321 * privileged operation -- cevans
1323 as->suser = capable(CAP_SYS_ADMIN);
1324 as->next = user_list;
1325 user_list = as;
1326 filp->private_data = as;
1327 return 0;
1330 static int apm_get_info(char *buf, char **start, off_t fpos, int length)
1332 char * p;
1333 unsigned short bx;
1334 unsigned short cx;
1335 unsigned short dx;
1336 unsigned short error;
1337 unsigned short ac_line_status = 0xff;
1338 unsigned short battery_status = 0xff;
1339 unsigned short battery_flag = 0xff;
1340 int percentage = -1;
1341 int time_units = -1;
1342 char *units = "?";
1344 p = buf;
1346 if ((smp_num_cpus == 1) &&
1347 !(error = apm_get_power_status(&bx, &cx, &dx))) {
1348 ac_line_status = (bx >> 8) & 0xff;
1349 battery_status = bx & 0xff;
1350 if ((cx & 0xff) != 0xff)
1351 percentage = cx & 0xff;
1353 if (apm_bios_info.version > 0x100) {
1354 battery_flag = (cx >> 8) & 0xff;
1355 if (dx != 0xffff) {
1356 units = (dx & 0x8000) ? "min" : "sec";
1357 time_units = dx & 0x7fff;
1361 /* Arguments, with symbols from linux/apm_bios.h. Information is
1362 from the Get Power Status (0x0a) call unless otherwise noted.
1364 0) Linux driver version (this will change if format changes)
1365 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
1366 2) APM flags from APM Installation Check (0x00):
1367 bit 0: APM_16_BIT_SUPPORT
1368 bit 1: APM_32_BIT_SUPPORT
1369 bit 2: APM_IDLE_SLOWS_CLOCK
1370 bit 3: APM_BIOS_DISABLED
1371 bit 4: APM_BIOS_DISENGAGED
1372 3) AC line status
1373 0x00: Off-line
1374 0x01: On-line
1375 0x02: On backup power (BIOS >= 1.1 only)
1376 0xff: Unknown
1377 4) Battery status
1378 0x00: High
1379 0x01: Low
1380 0x02: Critical
1381 0x03: Charging
1382 0x04: Selected battery not present (BIOS >= 1.2 only)
1383 0xff: Unknown
1384 5) Battery flag
1385 bit 0: High
1386 bit 1: Low
1387 bit 2: Critical
1388 bit 3: Charging
1389 bit 7: No system battery
1390 0xff: Unknown
1391 6) Remaining battery life (percentage of charge):
1392 0-100: valid
1393 -1: Unknown
1394 7) Remaining battery life (time units):
1395 Number of remaining minutes or seconds
1396 -1: Unknown
1397 8) min = minutes; sec = seconds */
1399 p += sprintf(p, "%s %d.%d 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
1400 driver_version,
1401 (apm_bios_info.version >> 8) & 0xff,
1402 apm_bios_info.version & 0xff,
1403 apm_bios_info.flags,
1404 ac_line_status,
1405 battery_status,
1406 battery_flag,
1407 percentage,
1408 time_units,
1409 units);
1411 return p - buf;
1414 static int apm(void *unused)
1416 unsigned short bx;
1417 unsigned short cx;
1418 unsigned short dx;
1419 unsigned short error;
1420 char * power_stat;
1421 char * bat_stat;
1423 kapmd_running = 1;
1425 exit_files(current); /* daemonize doesn't do exit_files */
1426 daemonize();
1428 strcpy(current->comm, "kapmd");
1429 sigfillset(&current->blocked);
1430 current->tty = NULL; /* get rid of controlling tty */
1432 if (apm_bios_info.version > 0x100) {
1434 * We only support BIOSs up to version 1.2
1436 if (apm_bios_info.version > 0x0102)
1437 apm_bios_info.version = 0x0102;
1438 if (apm_driver_version(&apm_bios_info.version) != APM_SUCCESS) {
1439 /* Fall back to an APM 1.0 connection. */
1440 apm_bios_info.version = 0x100;
1443 if (debug && (smp_num_cpus == 1)) {
1444 printk(KERN_INFO "apm: Connection version %d.%d\n",
1445 (apm_bios_info.version >> 8) & 0xff,
1446 apm_bios_info.version & 0xff);
1448 error = apm_get_power_status(&bx, &cx, &dx);
1449 if (error)
1450 printk(KERN_INFO "apm: power status not available\n");
1451 else {
1452 switch ((bx >> 8) & 0xff) {
1453 case 0: power_stat = "off line"; break;
1454 case 1: power_stat = "on line"; break;
1455 case 2: power_stat = "on backup power"; break;
1456 default: power_stat = "unknown"; break;
1458 switch (bx & 0xff) {
1459 case 0: bat_stat = "high"; break;
1460 case 1: bat_stat = "low"; break;
1461 case 2: bat_stat = "critical"; break;
1462 case 3: bat_stat = "charging"; break;
1463 default: bat_stat = "unknown"; break;
1465 printk(KERN_INFO
1466 "apm: AC %s, battery status %s, battery life ",
1467 power_stat, bat_stat);
1468 if ((cx & 0xff) == 0xff)
1469 printk("unknown\n");
1470 else
1471 printk("%d%%\n", cx & 0xff);
1472 if (apm_bios_info.version > 0x100) {
1473 printk(KERN_INFO
1474 "apm: battery flag 0x%02x, battery life ",
1475 (cx >> 8) & 0xff);
1476 if (dx == 0xffff)
1477 printk("unknown\n");
1478 else
1479 printk("%d %s\n", dx & 0x7fff,
1480 (dx & 0x8000) ?
1481 "minutes" : "seconds");
1486 #ifdef CONFIG_APM_DO_ENABLE
1487 if (apm_bios_info.flags & APM_BIOS_DISABLED) {
1489 * This call causes my NEC UltraLite Versa 33/C to hang if it
1490 * is booted with PM disabled but not in the docking station.
1491 * Unfortunate ...
1493 error = apm_enable_power_management(1);
1494 if (error) {
1495 apm_error("enable power management", error);
1496 return -1;
1499 #endif
1500 if ((apm_bios_info.flags & APM_BIOS_DISENGAGED)
1501 && (apm_bios_info.version > 0x0100)) {
1502 error = apm_engage_power_management(APM_DEVICE_ALL, 1);
1503 if (error) {
1504 apm_error("engage power management", error);
1505 return -1;
1509 /* Install our power off handler.. */
1510 if (power_off)
1511 pm_power_off = apm_power_off;
1512 #ifdef CONFIG_MAGIC_SYSRQ
1513 sysrq_power_off = apm_power_off;
1514 #endif
1515 if (smp_num_cpus == 1) {
1516 #if defined(CONFIG_APM_DISPLAY_BLANK) && defined(CONFIG_VT)
1517 console_blank_hook = apm_console_blank;
1518 #endif
1519 apm_mainloop();
1520 #if defined(CONFIG_APM_DISPLAY_BLANK) && defined(CONFIG_VT)
1521 console_blank_hook = NULL;
1522 #endif
1524 kapmd_running = 0;
1526 return 0;
1529 #ifndef MODULE
1530 static int __init apm_setup(char *str)
1532 int invert;
1534 while ((str != NULL) && (*str != '\0')) {
1535 if (strncmp(str, "off", 3) == 0)
1536 apm_disabled = 1;
1537 if (strncmp(str, "on", 2) == 0)
1538 apm_disabled = 0;
1539 invert = (strncmp(str, "no-", 3) == 0);
1540 if (invert)
1541 str += 3;
1542 if (strncmp(str, "debug", 5) == 0)
1543 debug = !invert;
1544 if ((strncmp(str, "power-off", 9) == 0) ||
1545 (strncmp(str, "power_off", 9) == 0))
1546 power_off = !invert;
1547 if ((strncmp(str, "bounce-interval=", 16) == 0) ||
1548 (strncmp(str, "bounce_interval=", 16) == 0))
1549 bounce_interval = simple_strtol(str + 16, NULL, 0);
1550 str = strchr(str, ',');
1551 if (str != NULL)
1552 str += strspn(str, ", \t");
1554 return 1;
1557 __setup("apm=", apm_setup);
1558 #endif
1560 static struct file_operations apm_bios_fops = {
1561 owner: THIS_MODULE,
1562 read: do_read,
1563 poll: do_poll,
1564 ioctl: do_ioctl,
1565 open: do_open,
1566 release: do_release,
1569 static struct miscdevice apm_device = {
1570 APM_MINOR_DEV,
1571 "apm_bios",
1572 &apm_bios_fops
1575 #define APM_INIT_ERROR_RETURN return -1
1578 * Just start the APM thread. We do NOT want to do APM BIOS
1579 * calls from anything but the APM thread, if for no other reason
1580 * than the fact that we don't trust the APM BIOS. This way,
1581 * most common APM BIOS problems that lead to protection errors
1582 * etc will have at least some level of being contained...
1584 * In short, if something bad happens, at least we have a choice
1585 * of just killing the apm thread..
1587 static int __init apm_init(void)
1589 if (apm_bios_info.version == 0) {
1590 printk(KERN_INFO "apm: BIOS not found.\n");
1591 APM_INIT_ERROR_RETURN;
1593 printk(KERN_INFO
1594 "apm: BIOS version %d.%d Flags 0x%02x (Driver version %s)\n",
1595 ((apm_bios_info.version >> 8) & 0xff),
1596 (apm_bios_info.version & 0xff),
1597 apm_bios_info.flags,
1598 driver_version);
1599 if ((apm_bios_info.flags & APM_32_BIT_SUPPORT) == 0) {
1600 printk(KERN_INFO "apm: no 32 bit BIOS support\n");
1601 APM_INIT_ERROR_RETURN;
1605 * Fix for the Compaq Contura 3/25c which reports BIOS version 0.1
1606 * but is reportedly a 1.0 BIOS.
1608 if (apm_bios_info.version == 0x001)
1609 apm_bios_info.version = 0x100;
1611 /* BIOS < 1.2 doesn't set cseg_16_len */
1612 if (apm_bios_info.version < 0x102)
1613 apm_bios_info.cseg_16_len = 0; /* 64k */
1615 if (debug) {
1616 printk(KERN_INFO "apm: entry %x:%lx cseg16 %x dseg %x",
1617 apm_bios_info.cseg, apm_bios_info.offset,
1618 apm_bios_info.cseg_16, apm_bios_info.dseg);
1619 if (apm_bios_info.version > 0x100)
1620 printk(" cseg len %x, dseg len %x",
1621 apm_bios_info.cseg_len,
1622 apm_bios_info.dseg_len);
1623 if (apm_bios_info.version > 0x101)
1624 printk(" cseg16 len %x", apm_bios_info.cseg_16_len);
1625 printk("\n");
1628 if (apm_disabled) {
1629 printk(KERN_NOTICE "apm: disabled on user request.\n");
1630 APM_INIT_ERROR_RETURN;
1632 if ((smp_num_cpus > 1) && !power_off) {
1633 printk(KERN_NOTICE "apm: disabled - APM is not SMP safe.\n");
1634 APM_INIT_ERROR_RETURN;
1636 if (PM_IS_ACTIVE()) {
1637 printk(KERN_NOTICE "apm: overridden by ACPI.\n");
1638 APM_INIT_ERROR_RETURN;
1640 pm_active = 1;
1643 * Set up a segment that references the real mode segment 0x40
1644 * that extends up to the end of page zero (that we have reserved).
1645 * This is for buggy BIOS's that refer to (real mode) segment 0x40
1646 * even though they are called in protected mode.
1648 set_base(gdt[APM_40 >> 3],
1649 __va((unsigned long)0x40 << 4));
1650 _set_limit((char *)&gdt[APM_40 >> 3], 4095 - (0x40 << 4));
1652 apm_bios_entry.offset = apm_bios_info.offset;
1653 apm_bios_entry.segment = APM_CS;
1654 set_base(gdt[APM_CS >> 3],
1655 __va((unsigned long)apm_bios_info.cseg << 4));
1656 set_base(gdt[APM_CS_16 >> 3],
1657 __va((unsigned long)apm_bios_info.cseg_16 << 4));
1658 set_base(gdt[APM_DS >> 3],
1659 __va((unsigned long)apm_bios_info.dseg << 4));
1660 #ifndef APM_RELAX_SEGMENTS
1661 if (apm_bios_info.version == 0x100) {
1662 #endif
1663 /* For ASUS motherboard, Award BIOS rev 110 (and others?) */
1664 _set_limit((char *)&gdt[APM_CS >> 3], 64 * 1024 - 1);
1665 /* For some unknown machine. */
1666 _set_limit((char *)&gdt[APM_CS_16 >> 3], 64 * 1024 - 1);
1667 /* For the DEC Hinote Ultra CT475 (and others?) */
1668 _set_limit((char *)&gdt[APM_DS >> 3], 64 * 1024 - 1);
1669 #ifndef APM_RELAX_SEGMENTS
1670 } else {
1671 _set_limit((char *)&gdt[APM_CS >> 3],
1672 (apm_bios_info.cseg_len - 1) & 0xffff);
1673 _set_limit((char *)&gdt[APM_CS_16 >> 3],
1674 (apm_bios_info.cseg_16_len - 1) & 0xffff);
1675 _set_limit((char *)&gdt[APM_DS >> 3],
1676 (apm_bios_info.dseg_len - 1) & 0xffff);
1678 #endif
1680 create_proc_info_entry("apm", 0, NULL, apm_get_info);
1682 kernel_thread(apm, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD);
1684 if (smp_num_cpus > 1) {
1685 printk(KERN_NOTICE
1686 "apm: disabled - APM is not SMP safe (power off active).\n");
1687 APM_INIT_ERROR_RETURN;
1690 misc_register(&apm_device);
1692 return 0;
1695 static void __exit apm_exit(void)
1697 misc_deregister(&apm_device);
1698 remove_proc_entry("apm", NULL);
1699 #ifdef CONFIG_MAGIC_SYSRQ
1700 sysrq_power_off = NULL;
1701 #endif
1702 if (power_off)
1703 pm_power_off = NULL;
1704 exit_kapmd = 1;
1705 while (kapmd_running)
1706 schedule();
1707 pm_active = 0;
1710 module_init(apm_init);
1711 module_exit(apm_exit);
1713 MODULE_AUTHOR("Stephen Rothwell");
1714 MODULE_DESCRIPTION("Advanced Power Management");
1715 MODULE_PARM(debug, "i");
1716 MODULE_PARM_DESC(debug, "Enable debug mode");
1717 MODULE_PARM(power_off, "i");
1718 MODULE_PARM_DESC(power_off, "Enable power off");
1719 MODULE_PARM(bounce_interval, "i");
1720 MODULE_PARM_DESC(bounce_interval, "Set the number of ticks to ignore suspend bounces");
1722 EXPORT_NO_SYMBOLS;