apm.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420
  1. /* -*- linux-c -*-
  2. * APM BIOS driver for Linux
  3. * Copyright 1994-2001 Stephen Rothwell (sfr@canb.auug.org.au)
  4. *
  5. * Initial development of this driver was funded by NEC Australia P/L
  6. * and NEC Corporation
  7. *
  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.
  12. *
  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.
  17. *
  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 (sfr@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
  40. * Nov 2000, Version 1.14
  41. * Oct 2001, Version 1.15
  42. * Jan 2002, Version 1.16
  43. * Oct 2002, Version 1.16ac
  44. *
  45. * History:
  46. * 0.6b: first version in official kernel, Linux 1.3.46
  47. * 0.7: changed /proc/apm format, Linux 1.3.58
  48. * 0.8: fixed gcc 2.7.[12] compilation problems, Linux 1.3.59
  49. * 0.9: only call bios if bios is present, Linux 1.3.72
  50. * 1.0: use fixed device number, consolidate /proc/apm into this file,
  51. * Linux 1.3.85
  52. * 1.1: support user-space standby and suspend, power off after system
  53. * halted, Linux 1.3.98
  54. * 1.2: When resetting RTC after resume, take care so that the time
  55. * is only incorrect by 30-60mS (vs. 1S previously) (Gabor J. Toth
  56. * <jtoth@princeton.edu>); improve interaction between
  57. * screen-blanking and gpm (Stephen Rothwell); Linux 1.99.4
  58. * 1.2a:Simple change to stop mysterious bug reports with SMP also added
  59. * levels to the printk calls. APM is not defined for SMP machines.
  60. * The new replacment for it is, but Linux doesn't yet support this.
  61. * Alan Cox Linux 2.1.55
  62. * 1.3: Set up a valid data descriptor 0x40 for buggy BIOS's
  63. * 1.4: Upgraded to support APM 1.2. Integrated ThinkPad suspend patch by
  64. * Dean Gaudet <dgaudet@arctic.org>.
  65. * C. Scott Ananian <cananian@alumni.princeton.edu> Linux 2.1.87
  66. * 1.5: Fix segment register reloading (in case of bad segments saved
  67. * across BIOS call).
  68. * Stephen Rothwell
  69. * 1.6: Cope with complier/assembler differences.
  70. * Only try to turn off the first display device.
  71. * Fix OOPS at power off with no APM BIOS by Jan Echternach
  72. * <echter@informatik.uni-rostock.de>
  73. * Stephen Rothwell
  74. * 1.7: Modify driver's cached copy of the disabled/disengaged flags
  75. * to reflect current state of APM BIOS.
  76. * Chris Rankin <rankinc@bellsouth.net>
  77. * Reset interrupt 0 timer to 100Hz after suspend
  78. * Chad Miller <cmiller@surfsouth.com>
  79. * Add CONFIG_APM_IGNORE_SUSPEND_BOUNCE
  80. * Richard Gooch <rgooch@atnf.csiro.au>
  81. * Allow boot time disabling of APM
  82. * Make boot messages far less verbose by default
  83. * Make asm safer
  84. * Stephen Rothwell
  85. * 1.8: Add CONFIG_APM_RTC_IS_GMT
  86. * Richard Gooch <rgooch@atnf.csiro.au>
  87. * change APM_NOINTS to CONFIG_APM_ALLOW_INTS
  88. * remove dependency on CONFIG_PROC_FS
  89. * Stephen Rothwell
  90. * 1.9: Fix small typo. <laslo@wodip.opole.pl>
  91. * Try to cope with BIOS's that need to have all display
  92. * devices blanked and not just the first one.
  93. * Ross Paterson <ross@soi.city.ac.uk>
  94. * Fix segment limit setting it has always been wrong as
  95. * the segments needed to have byte granularity.
  96. * Mark a few things __init.
  97. * Add hack to allow power off of SMP systems by popular request.
  98. * Use CONFIG_SMP instead of __SMP__
  99. * Ignore BOUNCES for three seconds.
  100. * Stephen Rothwell
  101. * 1.10: Fix for Thinkpad return code.
  102. * Merge 2.2 and 2.3 drivers.
  103. * Remove APM dependencies in arch/i386/kernel/process.c
  104. * Remove APM dependencies in drivers/char/sysrq.c
  105. * Reset time across standby.
  106. * Allow more inititialisation on SMP.
  107. * Remove CONFIG_APM_POWER_OFF and make it boot time
  108. * configurable (default on).
  109. * Make debug only a boot time parameter (remove APM_DEBUG).
  110. * Try to blank all devices on any error.
  111. * 1.11: Remove APM dependencies in drivers/char/console.c
  112. * Check nr_running to detect if we are idle (from
  113. * Borislav Deianov <borislav@lix.polytechnique.fr>)
  114. * Fix for bioses that don't zero the top part of the
  115. * entrypoint offset (Mario Sitta <sitta@al.unipmn.it>)
  116. * (reported by Panos Katsaloulis <teras@writeme.com>).
  117. * Real mode power off patch (Walter Hofmann
  118. * <Walter.Hofmann@physik.stud.uni-erlangen.de>).
  119. * 1.12: Remove CONFIG_SMP as the compiler will optimize
  120. * the code away anyway (smp_num_cpus == 1 in UP)
  121. * noted by Artur Skawina <skawina@geocities.com>.
  122. * Make power off under SMP work again.
  123. * Fix thinko with initial engaging of BIOS.
  124. * Make sure power off only happens on CPU 0
  125. * (Paul "Rusty" Russell <rusty@rustcorp.com.au>).
  126. * Do error notification to user mode if BIOS calls fail.
  127. * Move entrypoint offset fix to ...boot/setup.S
  128. * where it belongs (Cosmos <gis88564@cis.nctu.edu.tw>).
  129. * Remove smp-power-off. SMP users must now specify
  130. * "apm=power-off" on the kernel command line. Suggested
  131. * by Jim Avera <jima@hal.com>, modified by Alan Cox
  132. * <alan@lxorguk.ukuu.org.uk>.
  133. * Register the /proc/apm entry even on SMP so that
  134. * scripts that check for it before doing power off
  135. * work (Jim Avera <jima@hal.com>).
  136. * 1.13: Changes for new pm_ interfaces (Andy Henroid
  137. * <andy_henroid@yahoo.com>).
  138. * Modularize the code.
  139. * Fix the Thinkpad (again) :-( (CONFIG_APM_IGNORE_MULTIPLE_SUSPENDS
  140. * is now the way life works).
  141. * Fix thinko in suspend() (wrong return).
  142. * Notify drivers on critical suspend.
  143. * Make kapmd absorb more idle time (Pavel Machek <pavel@suse.cz>
  144. * modified by sfr).
  145. * Disable interrupts while we are suspended (Andy Henroid
  146. * <andy_henroid@yahoo.com> fixed by sfr).
  147. * Make power off work on SMP again (Tony Hoyle
  148. * <tmh@magenta-logic.com> and <zlatko@iskon.hr>) modified by sfr.
  149. * Remove CONFIG_APM_SUSPEND_BOUNCE. The bounce ignore
  150. * interval is now configurable.
  151. * 1.14: Make connection version persist across module unload/load.
  152. * Enable and engage power management earlier.
  153. * Disengage power management on module unload.
  154. * Changed to use the sysrq-register hack for registering the
  155. * power off function called by magic sysrq based upon discussions
  156. * in irc://irc.openprojects.net/#kernelnewbies
  157. * (Crutcher Dunnavant <crutcher+kernel@datastacks.com>).
  158. * Make CONFIG_APM_REAL_MODE_POWER_OFF run time configurable.
  159. * (Arjan van de Ven <arjanv@redhat.com>) modified by sfr.
  160. * Work around byte swap bug in one of the Vaio's BIOS's
  161. * (Marc Boucher <marc@mbsi.ca>).
  162. * Exposed the disable flag to dmi so that we can handle known
  163. * broken APM (Alan Cox <alan@redhat.com>).
  164. * 1.14ac: If the BIOS says "I slowed the CPU down" then don't spin
  165. * calling it - instead idle. (Alan Cox <alan@redhat.com>)
  166. * If an APM idle fails log it and idle sensibly
  167. * 1.15: Don't queue events to clients who open the device O_WRONLY.
  168. * Don't expect replies from clients who open the device O_RDONLY.
  169. * (Idea from Thomas Hood)
  170. * Minor waitqueue cleanups. (John Fremlin <chief@bandits.org>)
  171. * 1.16: Fix idle calling. (Andreas Steinmetz <ast@domdv.de> et al.)
  172. * Notify listeners of standby or suspend events before notifying
  173. * drivers. Return EBUSY to ioctl() if suspend is rejected.
  174. * (Russell King <rmk@arm.linux.org.uk> and Thomas Hood)
  175. * Ignore first resume after we generate our own resume event
  176. * after a suspend (Thomas Hood)
  177. * Daemonize now gets rid of our controlling terminal (sfr).
  178. * CONFIG_APM_CPU_IDLE now just affects the default value of
  179. * idle_threshold (sfr).
  180. * Change name of kernel apm daemon (as it no longer idles) (sfr).
  181. * 1.16ac: Fix up SMP support somewhat. You can now force SMP on and we
  182. * make _all_ APM calls on the CPU#0. Fix unsafe sign bug.
  183. * TODO: determine if its "boot CPU" or "CPU0" we want to lock to.
  184. *
  185. * APM 1.1 Reference:
  186. *
  187. * Intel Corporation, Microsoft Corporation. Advanced Power Management
  188. * (APM) BIOS Interface Specification, Revision 1.1, September 1993.
  189. * Intel Order Number 241704-001. Microsoft Part Number 781-110-X01.
  190. *
  191. * [This document is available free from Intel by calling 800.628.8686 (fax
  192. * 916.356.6100) or 800.548.4725; or via anonymous ftp from
  193. * ftp://ftp.intel.com/pub/IAL/software_specs/apmv11.doc. It is also
  194. * available from Microsoft by calling 206.882.8080.]
  195. *
  196. * APM 1.2 Reference:
  197. * Intel Corporation, Microsoft Corporation. Advanced Power Management
  198. * (APM) BIOS Interface Specification, Revision 1.2, February 1996.
  199. *
  200. * [This document is available from Microsoft at:
  201. * http://www.microsoft.com/hwdev/busbios/amp_12.htm]
  202. */
  203. #include <linux/config.h>
  204. #include <linux/module.h>
  205. #include <linux/poll.h>
  206. #include <linux/types.h>
  207. #include <linux/stddef.h>
  208. #include <linux/timer.h>
  209. #include <linux/fcntl.h>
  210. #include <linux/slab.h>
  211. #include <linux/stat.h>
  212. #include <linux/proc_fs.h>
  213. #include <linux/miscdevice.h>
  214. #include <linux/apm_bios.h>
  215. #include <linux/init.h>
  216. #include <linux/time.h>
  217. #include <linux/sched.h>
  218. #include <linux/pm.h>
  219. #include <linux/device.h>
  220. #include <linux/kernel.h>
  221. #include <linux/smp.h>
  222. #include <linux/smp_lock.h>
  223. #include <linux/dmi.h>
  224. #include <linux/suspend.h>
  225. #include <asm/system.h>
  226. #include <asm/uaccess.h>
  227. #include <asm/desc.h>
  228. #include <asm/i8253.h>
  229. #include "io_ports.h"
  230. extern unsigned long get_cmos_time(void);
  231. extern void machine_real_restart(unsigned char *, int);
  232. #if defined(CONFIG_APM_DISPLAY_BLANK) && defined(CONFIG_VT)
  233. extern int (*console_blank_hook)(int);
  234. #endif
  235. /*
  236. * The apm_bios device is one of the misc char devices.
  237. * This is its minor number.
  238. */
  239. #define APM_MINOR_DEV 134
  240. /*
  241. * See Documentation/Config.help for the configuration options.
  242. *
  243. * Various options can be changed at boot time as follows:
  244. * (We allow underscores for compatibility with the modules code)
  245. * apm=on/off enable/disable APM
  246. * [no-]allow[-_]ints allow interrupts during BIOS calls
  247. * [no-]broken[-_]psr BIOS has a broken GetPowerStatus call
  248. * [no-]realmode[-_]power[-_]off switch to real mode before
  249. * powering off
  250. * [no-]debug log some debugging messages
  251. * [no-]power[-_]off power off on shutdown
  252. * [no-]smp Use apm even on an SMP box
  253. * bounce[-_]interval=<n> number of ticks to ignore suspend
  254. * bounces
  255. * idle[-_]threshold=<n> System idle percentage above which to
  256. * make APM BIOS idle calls. Set it to
  257. * 100 to disable.
  258. * idle[-_]period=<n> Period (in 1/100s of a second) over
  259. * which the idle percentage is
  260. * calculated.
  261. */
  262. /* KNOWN PROBLEM MACHINES:
  263. *
  264. * U: TI 4000M TravelMate: BIOS is *NOT* APM compliant
  265. * [Confirmed by TI representative]
  266. * ?: ACER 486DX4/75: uses dseg 0040, in violation of APM specification
  267. * [Confirmed by BIOS disassembly]
  268. * [This may work now ...]
  269. * P: Toshiba 1950S: battery life information only gets updated after resume
  270. * P: Midwest Micro Soundbook Elite DX2/66 monochrome: screen blanking
  271. * broken in BIOS [Reported by Garst R. Reese <reese@isn.net>]
  272. * ?: AcerNote-950: oops on reading /proc/apm - workaround is a WIP
  273. * Neale Banks <neale@lowendale.com.au> December 2000
  274. *
  275. * Legend: U = unusable with APM patches
  276. * P = partially usable with APM patches
  277. */
  278. /*
  279. * Define as 1 to make the driver always call the APM BIOS busy
  280. * routine even if the clock was not reported as slowed by the
  281. * idle routine. Otherwise, define as 0.
  282. */
  283. #define ALWAYS_CALL_BUSY 1
  284. /*
  285. * Define to make the APM BIOS calls zero all data segment registers (so
  286. * that an incorrect BIOS implementation will cause a kernel panic if it
  287. * tries to write to arbitrary memory).
  288. */
  289. #define APM_ZERO_SEGS
  290. #include "apm.h"
  291. /*
  292. * Define to make all _set_limit calls use 64k limits. The APM 1.1 BIOS is
  293. * supposed to provide limit information that it recognizes. Many machines
  294. * do this correctly, but many others do not restrict themselves to their
  295. * claimed limit. When this happens, they will cause a segmentation
  296. * violation in the kernel at boot time. Most BIOS's, however, will
  297. * respect a 64k limit, so we use that. If you want to be pedantic and
  298. * hold your BIOS to its claims, then undefine this.
  299. */
  300. #define APM_RELAX_SEGMENTS
  301. /*
  302. * Define to re-initialize the interrupt 0 timer to 100 Hz after a suspend.
  303. * This patched by Chad Miller <cmiller@surfsouth.com>, original code by
  304. * David Chen <chen@ctpa04.mit.edu>
  305. */
  306. #undef INIT_TIMER_AFTER_SUSPEND
  307. #ifdef INIT_TIMER_AFTER_SUSPEND
  308. #include <linux/timex.h>
  309. #include <asm/io.h>
  310. #include <linux/delay.h>
  311. #endif
  312. /*
  313. * Need to poll the APM BIOS every second
  314. */
  315. #define APM_CHECK_TIMEOUT (HZ)
  316. /*
  317. * Ignore suspend events for this amount of time after a resume
  318. */
  319. #define DEFAULT_BOUNCE_INTERVAL (3 * HZ)
  320. /*
  321. * Maximum number of events stored
  322. */
  323. #define APM_MAX_EVENTS 20
  324. /*
  325. * The per-file APM data
  326. */
  327. struct apm_user {
  328. int magic;
  329. struct apm_user * next;
  330. unsigned int suser: 1;
  331. unsigned int writer: 1;
  332. unsigned int reader: 1;
  333. unsigned int suspend_wait: 1;
  334. int suspend_result;
  335. int suspends_pending;
  336. int standbys_pending;
  337. int suspends_read;
  338. int standbys_read;
  339. int event_head;
  340. int event_tail;
  341. apm_event_t events[APM_MAX_EVENTS];
  342. };
  343. /*
  344. * The magic number in apm_user
  345. */
  346. #define APM_BIOS_MAGIC 0x4101
  347. /*
  348. * idle percentage above which bios idle calls are done
  349. */
  350. #ifdef CONFIG_APM_CPU_IDLE
  351. #define DEFAULT_IDLE_THRESHOLD 95
  352. #else
  353. #define DEFAULT_IDLE_THRESHOLD 100
  354. #endif
  355. #define DEFAULT_IDLE_PERIOD (100 / 3)
  356. /*
  357. * Local variables
  358. */
  359. static struct {
  360. unsigned long offset;
  361. unsigned short segment;
  362. } apm_bios_entry;
  363. static int clock_slowed;
  364. static int idle_threshold = DEFAULT_IDLE_THRESHOLD;
  365. static int idle_period = DEFAULT_IDLE_PERIOD;
  366. static int set_pm_idle;
  367. static int suspends_pending;
  368. static int standbys_pending;
  369. static int ignore_sys_suspend;
  370. static int ignore_normal_resume;
  371. static int bounce_interval = DEFAULT_BOUNCE_INTERVAL;
  372. #ifdef CONFIG_APM_RTC_IS_GMT
  373. # define clock_cmos_diff 0
  374. # define got_clock_diff 1
  375. #else
  376. static long clock_cmos_diff;
  377. static int got_clock_diff;
  378. #endif
  379. static int debug;
  380. static int smp;
  381. static int apm_disabled = -1;
  382. #ifdef CONFIG_SMP
  383. static int power_off;
  384. #else
  385. static int power_off = 1;
  386. #endif
  387. #ifdef CONFIG_APM_REAL_MODE_POWER_OFF
  388. static int realmode_power_off = 1;
  389. #else
  390. static int realmode_power_off;
  391. #endif
  392. static int exit_kapmd;
  393. static int kapmd_running;
  394. #ifdef CONFIG_APM_ALLOW_INTS
  395. static int allow_ints = 1;
  396. #else
  397. static int allow_ints;
  398. #endif
  399. static int broken_psr;
  400. static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);
  401. static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);
  402. static struct apm_user * user_list;
  403. static DEFINE_SPINLOCK(user_list_lock);
  404. static struct desc_struct bad_bios_desc = { 0, 0x00409200 };
  405. static char driver_version[] = "1.16ac"; /* no spaces */
  406. /*
  407. * APM event names taken from the APM 1.2 specification. These are
  408. * the message codes that the BIOS uses to tell us about events
  409. */
  410. static char * apm_event_name[] = {
  411. "system standby",
  412. "system suspend",
  413. "normal resume",
  414. "critical resume",
  415. "low battery",
  416. "power status change",
  417. "update time",
  418. "critical suspend",
  419. "user standby",
  420. "user suspend",
  421. "system standby resume",
  422. "capabilities change"
  423. };
  424. #define NR_APM_EVENT_NAME \
  425. (sizeof(apm_event_name) / sizeof(apm_event_name[0]))
  426. typedef struct lookup_t {
  427. int key;
  428. char * msg;
  429. } lookup_t;
  430. /*
  431. * The BIOS returns a set of standard error codes in AX when the
  432. * carry flag is set.
  433. */
  434. static const lookup_t error_table[] = {
  435. /* N/A { APM_SUCCESS, "Operation succeeded" }, */
  436. { APM_DISABLED, "Power management disabled" },
  437. { APM_CONNECTED, "Real mode interface already connected" },
  438. { APM_NOT_CONNECTED, "Interface not connected" },
  439. { APM_16_CONNECTED, "16 bit interface already connected" },
  440. /* N/A { APM_16_UNSUPPORTED, "16 bit interface not supported" }, */
  441. { APM_32_CONNECTED, "32 bit interface already connected" },
  442. { APM_32_UNSUPPORTED, "32 bit interface not supported" },
  443. { APM_BAD_DEVICE, "Unrecognized device ID" },
  444. { APM_BAD_PARAM, "Parameter out of range" },
  445. { APM_NOT_ENGAGED, "Interface not engaged" },
  446. { APM_BAD_FUNCTION, "Function not supported" },
  447. { APM_RESUME_DISABLED, "Resume timer disabled" },
  448. { APM_BAD_STATE, "Unable to enter requested state" },
  449. /* N/A { APM_NO_EVENTS, "No events pending" }, */
  450. { APM_NO_ERROR, "BIOS did not set a return code" },
  451. { APM_NOT_PRESENT, "No APM present" }
  452. };
  453. #define ERROR_COUNT (sizeof(error_table)/sizeof(lookup_t))
  454. /**
  455. * apm_error - display an APM error
  456. * @str: information string
  457. * @err: APM BIOS return code
  458. *
  459. * Write a meaningful log entry to the kernel log in the event of
  460. * an APM error.
  461. */
  462. static void apm_error(char *str, int err)
  463. {
  464. int i;
  465. for (i = 0; i < ERROR_COUNT; i++)
  466. if (error_table[i].key == err) break;
  467. if (i < ERROR_COUNT)
  468. printk(KERN_NOTICE "apm: %s: %s\n", str, error_table[i].msg);
  469. else
  470. printk(KERN_NOTICE "apm: %s: unknown error code %#2.2x\n",
  471. str, err);
  472. }
  473. /*
  474. * Lock APM functionality to physical CPU 0
  475. */
  476. #ifdef CONFIG_SMP
  477. static cpumask_t apm_save_cpus(void)
  478. {
  479. cpumask_t x = current->cpus_allowed;
  480. /* Some bioses don't like being called from CPU != 0 */
  481. set_cpus_allowed(current, cpumask_of_cpu(0));
  482. BUG_ON(smp_processor_id() != 0);
  483. return x;
  484. }
  485. static inline void apm_restore_cpus(cpumask_t mask)
  486. {
  487. set_cpus_allowed(current, mask);
  488. }
  489. #else
  490. /*
  491. * No CPU lockdown needed on a uniprocessor
  492. */
  493. #define apm_save_cpus() (current->cpus_allowed)
  494. #define apm_restore_cpus(x) (void)(x)
  495. #endif
  496. /*
  497. * These are the actual BIOS calls. Depending on APM_ZERO_SEGS and
  498. * apm_info.allow_ints, we are being really paranoid here! Not only
  499. * are interrupts disabled, but all the segment registers (except SS)
  500. * are saved and zeroed this means that if the BIOS tries to reference
  501. * any data without explicitly loading the segment registers, the kernel
  502. * will fault immediately rather than have some unforeseen circumstances
  503. * for the rest of the kernel. And it will be very obvious! :-) Doing
  504. * this depends on CS referring to the same physical memory as DS so that
  505. * DS can be zeroed before the call. Unfortunately, we can't do anything
  506. * about the stack segment/pointer. Also, we tell the compiler that
  507. * everything could change.
  508. *
  509. * Also, we KNOW that for the non error case of apm_bios_call, there
  510. * is no useful data returned in the low order 8 bits of eax.
  511. */
  512. #define APM_DO_CLI \
  513. if (apm_info.allow_ints) \
  514. local_irq_enable(); \
  515. else \
  516. local_irq_disable();
  517. #ifdef APM_ZERO_SEGS
  518. # define APM_DECL_SEGS \
  519. unsigned int saved_fs; unsigned int saved_gs;
  520. # define APM_DO_SAVE_SEGS \
  521. savesegment(fs, saved_fs); savesegment(gs, saved_gs)
  522. # define APM_DO_RESTORE_SEGS \
  523. loadsegment(fs, saved_fs); loadsegment(gs, saved_gs)
  524. #else
  525. # define APM_DECL_SEGS
  526. # define APM_DO_SAVE_SEGS
  527. # define APM_DO_RESTORE_SEGS
  528. #endif
  529. /**
  530. * apm_bios_call - Make an APM BIOS 32bit call
  531. * @func: APM function to execute
  532. * @ebx_in: EBX register for call entry
  533. * @ecx_in: ECX register for call entry
  534. * @eax: EAX register return
  535. * @ebx: EBX register return
  536. * @ecx: ECX register return
  537. * @edx: EDX register return
  538. * @esi: ESI register return
  539. *
  540. * Make an APM call using the 32bit protected mode interface. The
  541. * caller is responsible for knowing if APM BIOS is configured and
  542. * enabled. This call can disable interrupts for a long period of
  543. * time on some laptops. The return value is in AH and the carry
  544. * flag is loaded into AL. If there is an error, then the error
  545. * code is returned in AH (bits 8-15 of eax) and this function
  546. * returns non-zero.
  547. */
  548. static u8 apm_bios_call(u32 func, u32 ebx_in, u32 ecx_in,
  549. u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, u32 *esi)
  550. {
  551. APM_DECL_SEGS
  552. unsigned long flags;
  553. cpumask_t cpus;
  554. int cpu;
  555. struct desc_struct save_desc_40;
  556. cpus = apm_save_cpus();
  557. cpu = get_cpu();
  558. save_desc_40 = per_cpu(cpu_gdt_table, cpu)[0x40 / 8];
  559. per_cpu(cpu_gdt_table, cpu)[0x40 / 8] = bad_bios_desc;
  560. local_save_flags(flags);
  561. APM_DO_CLI;
  562. APM_DO_SAVE_SEGS;
  563. apm_bios_call_asm(func, ebx_in, ecx_in, eax, ebx, ecx, edx, esi);
  564. APM_DO_RESTORE_SEGS;
  565. local_irq_restore(flags);
  566. per_cpu(cpu_gdt_table, cpu)[0x40 / 8] = save_desc_40;
  567. put_cpu();
  568. apm_restore_cpus(cpus);
  569. return *eax & 0xff;
  570. }
  571. /**
  572. * apm_bios_call_simple - make a simple APM BIOS 32bit call
  573. * @func: APM function to invoke
  574. * @ebx_in: EBX register value for BIOS call
  575. * @ecx_in: ECX register value for BIOS call
  576. * @eax: EAX register on return from the BIOS call
  577. *
  578. * Make a BIOS call that does only returns one value, or just status.
  579. * If there is an error, then the error code is returned in AH
  580. * (bits 8-15 of eax) and this function returns non-zero. This is
  581. * used for simpler BIOS operations. This call may hold interrupts
  582. * off for a long time on some laptops.
  583. */
  584. static u8 apm_bios_call_simple(u32 func, u32 ebx_in, u32 ecx_in, u32 *eax)
  585. {
  586. u8 error;
  587. APM_DECL_SEGS
  588. unsigned long flags;
  589. cpumask_t cpus;
  590. int cpu;
  591. struct desc_struct save_desc_40;
  592. cpus = apm_save_cpus();
  593. cpu = get_cpu();
  594. save_desc_40 = per_cpu(cpu_gdt_table, cpu)[0x40 / 8];
  595. per_cpu(cpu_gdt_table, cpu)[0x40 / 8] = bad_bios_desc;
  596. local_save_flags(flags);
  597. APM_DO_CLI;
  598. APM_DO_SAVE_SEGS;
  599. error = apm_bios_call_simple_asm(func, ebx_in, ecx_in, eax);
  600. APM_DO_RESTORE_SEGS;
  601. local_irq_restore(flags);
  602. __get_cpu_var(cpu_gdt_table)[0x40 / 8] = save_desc_40;
  603. put_cpu();
  604. apm_restore_cpus(cpus);
  605. return error;
  606. }
  607. /**
  608. * apm_driver_version - APM driver version
  609. * @val: loaded with the APM version on return
  610. *
  611. * Retrieve the APM version supported by the BIOS. This is only
  612. * supported for APM 1.1 or higher. An error indicates APM 1.0 is
  613. * probably present.
  614. *
  615. * On entry val should point to a value indicating the APM driver
  616. * version with the high byte being the major and the low byte the
  617. * minor number both in BCD
  618. *
  619. * On return it will hold the BIOS revision supported in the
  620. * same format.
  621. */
  622. static int apm_driver_version(u_short *val)
  623. {
  624. u32 eax;
  625. if (apm_bios_call_simple(APM_FUNC_VERSION, 0, *val, &eax))
  626. return (eax >> 8) & 0xff;
  627. *val = eax;
  628. return APM_SUCCESS;
  629. }
  630. /**
  631. * apm_get_event - get an APM event from the BIOS
  632. * @event: pointer to the event
  633. * @info: point to the event information
  634. *
  635. * The APM BIOS provides a polled information for event
  636. * reporting. The BIOS expects to be polled at least every second
  637. * when events are pending. When a message is found the caller should
  638. * poll until no more messages are present. However, this causes
  639. * problems on some laptops where a suspend event notification is
  640. * not cleared until it is acknowledged.
  641. *
  642. * Additional information is returned in the info pointer, providing
  643. * that APM 1.2 is in use. If no messges are pending the value 0x80
  644. * is returned (No power management events pending).
  645. */
  646. static int apm_get_event(apm_event_t *event, apm_eventinfo_t *info)
  647. {
  648. u32 eax;
  649. u32 ebx;
  650. u32 ecx;
  651. u32 dummy;
  652. if (apm_bios_call(APM_FUNC_GET_EVENT, 0, 0, &eax, &ebx, &ecx,
  653. &dummy, &dummy))
  654. return (eax >> 8) & 0xff;
  655. *event = ebx;
  656. if (apm_info.connection_version < 0x0102)
  657. *info = ~0; /* indicate info not valid */
  658. else
  659. *info = ecx;
  660. return APM_SUCCESS;
  661. }
  662. /**
  663. * set_power_state - set the power management state
  664. * @what: which items to transition
  665. * @state: state to transition to
  666. *
  667. * Request an APM change of state for one or more system devices. The
  668. * processor state must be transitioned last of all. what holds the
  669. * class of device in the upper byte and the device number (0xFF for
  670. * all) for the object to be transitioned.
  671. *
  672. * The state holds the state to transition to, which may in fact
  673. * be an acceptance of a BIOS requested state change.
  674. */
  675. static int set_power_state(u_short what, u_short state)
  676. {
  677. u32 eax;
  678. if (apm_bios_call_simple(APM_FUNC_SET_STATE, what, state, &eax))
  679. return (eax >> 8) & 0xff;
  680. return APM_SUCCESS;
  681. }
  682. /**
  683. * set_system_power_state - set system wide power state
  684. * @state: which state to enter
  685. *
  686. * Transition the entire system into a new APM power state.
  687. */
  688. static int set_system_power_state(u_short state)
  689. {
  690. return set_power_state(APM_DEVICE_ALL, state);
  691. }
  692. /**
  693. * apm_do_idle - perform power saving
  694. *
  695. * This function notifies the BIOS that the processor is (in the view
  696. * of the OS) idle. It returns -1 in the event that the BIOS refuses
  697. * to handle the idle request. On a success the function returns 1
  698. * if the BIOS did clock slowing or 0 otherwise.
  699. */
  700. static int apm_do_idle(void)
  701. {
  702. u32 eax;
  703. if (apm_bios_call_simple(APM_FUNC_IDLE, 0, 0, &eax)) {
  704. static unsigned long t;
  705. /* This always fails on some SMP boards running UP kernels.
  706. * Only report the failure the first 5 times.
  707. */
  708. if (++t < 5)
  709. {
  710. printk(KERN_DEBUG "apm_do_idle failed (%d)\n",
  711. (eax >> 8) & 0xff);
  712. t = jiffies;
  713. }
  714. return -1;
  715. }
  716. clock_slowed = (apm_info.bios.flags & APM_IDLE_SLOWS_CLOCK) != 0;
  717. return clock_slowed;
  718. }
  719. /**
  720. * apm_do_busy - inform the BIOS the CPU is busy
  721. *
  722. * Request that the BIOS brings the CPU back to full performance.
  723. */
  724. static void apm_do_busy(void)
  725. {
  726. u32 dummy;
  727. if (clock_slowed || ALWAYS_CALL_BUSY) {
  728. (void) apm_bios_call_simple(APM_FUNC_BUSY, 0, 0, &dummy);
  729. clock_slowed = 0;
  730. }
  731. }
  732. /*
  733. * If no process has really been interested in
  734. * the CPU for some time, we want to call BIOS
  735. * power management - we probably want
  736. * to conserve power.
  737. */
  738. #define IDLE_CALC_LIMIT (HZ * 100)
  739. #define IDLE_LEAKY_MAX 16
  740. static void (*original_pm_idle)(void);
  741. extern void default_idle(void);
  742. /**
  743. * apm_cpu_idle - cpu idling for APM capable Linux
  744. *
  745. * This is the idling function the kernel executes when APM is available. It
  746. * tries to do BIOS powermanagement based on the average system idle time.
  747. * Furthermore it calls the system default idle routine.
  748. */
  749. static void apm_cpu_idle(void)
  750. {
  751. static int use_apm_idle; /* = 0 */
  752. static unsigned int last_jiffies; /* = 0 */
  753. static unsigned int last_stime; /* = 0 */
  754. int apm_idle_done = 0;
  755. unsigned int jiffies_since_last_check = jiffies - last_jiffies;
  756. unsigned int bucket;
  757. recalc:
  758. if (jiffies_since_last_check > IDLE_CALC_LIMIT) {
  759. use_apm_idle = 0;
  760. last_jiffies = jiffies;
  761. last_stime = current->stime;
  762. } else if (jiffies_since_last_check > idle_period) {
  763. unsigned int idle_percentage;
  764. idle_percentage = current->stime - last_stime;
  765. idle_percentage *= 100;
  766. idle_percentage /= jiffies_since_last_check;
  767. use_apm_idle = (idle_percentage > idle_threshold);
  768. if (apm_info.forbid_idle)
  769. use_apm_idle = 0;
  770. last_jiffies = jiffies;
  771. last_stime = current->stime;
  772. }
  773. bucket = IDLE_LEAKY_MAX;
  774. while (!need_resched()) {
  775. if (use_apm_idle) {
  776. unsigned int t;
  777. t = jiffies;
  778. switch (apm_do_idle()) {
  779. case 0: apm_idle_done = 1;
  780. if (t != jiffies) {
  781. if (bucket) {
  782. bucket = IDLE_LEAKY_MAX;
  783. continue;
  784. }
  785. } else if (bucket) {
  786. bucket--;
  787. continue;
  788. }
  789. break;
  790. case 1: apm_idle_done = 1;
  791. break;
  792. default: /* BIOS refused */
  793. break;
  794. }
  795. }
  796. if (original_pm_idle)
  797. original_pm_idle();
  798. else
  799. default_idle();
  800. jiffies_since_last_check = jiffies - last_jiffies;
  801. if (jiffies_since_last_check > idle_period)
  802. goto recalc;
  803. }
  804. if (apm_idle_done)
  805. apm_do_busy();
  806. }
  807. /**
  808. * apm_power_off - ask the BIOS to power off
  809. *
  810. * Handle the power off sequence. This is the one piece of code we
  811. * will execute even on SMP machines. In order to deal with BIOS
  812. * bugs we support real mode APM BIOS power off calls. We also make
  813. * the SMP call on CPU0 as some systems will only honour this call
  814. * on their first cpu.
  815. */
  816. static void apm_power_off(void)
  817. {
  818. unsigned char po_bios_call[] = {
  819. 0xb8, 0x00, 0x10, /* movw $0x1000,ax */
  820. 0x8e, 0xd0, /* movw ax,ss */
  821. 0xbc, 0x00, 0xf0, /* movw $0xf000,sp */
  822. 0xb8, 0x07, 0x53, /* movw $0x5307,ax */
  823. 0xbb, 0x01, 0x00, /* movw $0x0001,bx */
  824. 0xb9, 0x03, 0x00, /* movw $0x0003,cx */
  825. 0xcd, 0x15 /* int $0x15 */
  826. };
  827. /* Some bioses don't like being called from CPU != 0 */
  828. if (apm_info.realmode_power_off)
  829. {
  830. (void)apm_save_cpus();
  831. machine_real_restart(po_bios_call, sizeof(po_bios_call));
  832. }
  833. else
  834. (void) set_system_power_state(APM_STATE_OFF);
  835. }
  836. #ifdef CONFIG_APM_DO_ENABLE
  837. /**
  838. * apm_enable_power_management - enable BIOS APM power management
  839. * @enable: enable yes/no
  840. *
  841. * Enable or disable the APM BIOS power services.
  842. */
  843. static int apm_enable_power_management(int enable)
  844. {
  845. u32 eax;
  846. if ((enable == 0) && (apm_info.bios.flags & APM_BIOS_DISENGAGED))
  847. return APM_NOT_ENGAGED;
  848. if (apm_bios_call_simple(APM_FUNC_ENABLE_PM, APM_DEVICE_BALL,
  849. enable, &eax))
  850. return (eax >> 8) & 0xff;
  851. if (enable)
  852. apm_info.bios.flags &= ~APM_BIOS_DISABLED;
  853. else
  854. apm_info.bios.flags |= APM_BIOS_DISABLED;
  855. return APM_SUCCESS;
  856. }
  857. #endif
  858. /**
  859. * apm_get_power_status - get current power state
  860. * @status: returned status
  861. * @bat: battery info
  862. * @life: estimated life
  863. *
  864. * Obtain the current power status from the APM BIOS. We return a
  865. * status which gives the rough battery status, and current power
  866. * source. The bat value returned give an estimate as a percentage
  867. * of life and a status value for the battery. The estimated life
  868. * if reported is a lifetime in secodnds/minutes at current powwer
  869. * consumption.
  870. */
  871. static int apm_get_power_status(u_short *status, u_short *bat, u_short *life)
  872. {
  873. u32 eax;
  874. u32 ebx;
  875. u32 ecx;
  876. u32 edx;
  877. u32 dummy;
  878. if (apm_info.get_power_status_broken)
  879. return APM_32_UNSUPPORTED;
  880. if (apm_bios_call(APM_FUNC_GET_STATUS, APM_DEVICE_ALL, 0,
  881. &eax, &ebx, &ecx, &edx, &dummy))
  882. return (eax >> 8) & 0xff;
  883. *status = ebx;
  884. *bat = ecx;
  885. if (apm_info.get_power_status_swabinminutes) {
  886. *life = swab16((u16)edx);
  887. *life |= 0x8000;
  888. } else
  889. *life = edx;
  890. return APM_SUCCESS;
  891. }
  892. #if 0
  893. static int apm_get_battery_status(u_short which, u_short *status,
  894. u_short *bat, u_short *life, u_short *nbat)
  895. {
  896. u32 eax;
  897. u32 ebx;
  898. u32 ecx;
  899. u32 edx;
  900. u32 esi;
  901. if (apm_info.connection_version < 0x0102) {
  902. /* pretend we only have one battery. */
  903. if (which != 1)
  904. return APM_BAD_DEVICE;
  905. *nbat = 1;
  906. return apm_get_power_status(status, bat, life);
  907. }
  908. if (apm_bios_call(APM_FUNC_GET_STATUS, (0x8000 | (which)), 0, &eax,
  909. &ebx, &ecx, &edx, &esi))
  910. return (eax >> 8) & 0xff;
  911. *status = ebx;
  912. *bat = ecx;
  913. *life = edx;
  914. *nbat = esi;
  915. return APM_SUCCESS;
  916. }
  917. #endif
  918. /**
  919. * apm_engage_power_management - enable PM on a device
  920. * @device: identity of device
  921. * @enable: on/off
  922. *
  923. * Activate or deactive power management on either a specific device
  924. * or the entire system (%APM_DEVICE_ALL).
  925. */
  926. static int apm_engage_power_management(u_short device, int enable)
  927. {
  928. u32 eax;
  929. if ((enable == 0) && (device == APM_DEVICE_ALL)
  930. && (apm_info.bios.flags & APM_BIOS_DISABLED))
  931. return APM_DISABLED;
  932. if (apm_bios_call_simple(APM_FUNC_ENGAGE_PM, device, enable, &eax))
  933. return (eax >> 8) & 0xff;
  934. if (device == APM_DEVICE_ALL) {
  935. if (enable)
  936. apm_info.bios.flags &= ~APM_BIOS_DISENGAGED;
  937. else
  938. apm_info.bios.flags |= APM_BIOS_DISENGAGED;
  939. }
  940. return APM_SUCCESS;
  941. }
  942. #if defined(CONFIG_APM_DISPLAY_BLANK) && defined(CONFIG_VT)
  943. /**
  944. * apm_console_blank - blank the display
  945. * @blank: on/off
  946. *
  947. * Attempt to blank the console, firstly by blanking just video device
  948. * zero, and if that fails (some BIOSes don't support it) then it blanks
  949. * all video devices. Typically the BIOS will do laptop backlight and
  950. * monitor powerdown for us.
  951. */
  952. static int apm_console_blank(int blank)
  953. {
  954. int error;
  955. u_short state;
  956. state = blank ? APM_STATE_STANDBY : APM_STATE_READY;
  957. /* Blank the first display device */
  958. error = set_power_state(0x100, state);
  959. if ((error != APM_SUCCESS) && (error != APM_NO_ERROR)) {
  960. /* try to blank them all instead */
  961. error = set_power_state(0x1ff, state);
  962. if ((error != APM_SUCCESS) && (error != APM_NO_ERROR))
  963. /* try to blank device one instead */
  964. error = set_power_state(0x101, state);
  965. }
  966. if ((error == APM_SUCCESS) || (error == APM_NO_ERROR))
  967. return 1;
  968. if (error == APM_NOT_ENGAGED) {
  969. static int tried;
  970. int eng_error;
  971. if (tried++ == 0) {
  972. eng_error = apm_engage_power_management(APM_DEVICE_ALL, 1);
  973. if (eng_error) {
  974. apm_error("set display", error);
  975. apm_error("engage interface", eng_error);
  976. return 0;
  977. } else
  978. return apm_console_blank(blank);
  979. }
  980. }
  981. apm_error("set display", error);
  982. return 0;
  983. }
  984. #endif
  985. static int queue_empty(struct apm_user *as)
  986. {
  987. return as->event_head == as->event_tail;
  988. }
  989. static apm_event_t get_queued_event(struct apm_user *as)
  990. {
  991. as->event_tail = (as->event_tail + 1) % APM_MAX_EVENTS;
  992. return as->events[as->event_tail];
  993. }
  994. static void queue_event(apm_event_t event, struct apm_user *sender)
  995. {
  996. struct apm_user * as;
  997. spin_lock(&user_list_lock);
  998. if (user_list == NULL)
  999. goto out;
  1000. for (as = user_list; as != NULL; as = as->next) {
  1001. if ((as == sender) || (!as->reader))
  1002. continue;
  1003. as->event_head = (as->event_head + 1) % APM_MAX_EVENTS;
  1004. if (as->event_head == as->event_tail) {
  1005. static int notified;
  1006. if (notified++ == 0)
  1007. printk(KERN_ERR "apm: an event queue overflowed\n");
  1008. as->event_tail = (as->event_tail + 1) % APM_MAX_EVENTS;
  1009. }
  1010. as->events[as->event_head] = event;
  1011. if ((!as->suser) || (!as->writer))
  1012. continue;
  1013. switch (event) {
  1014. case APM_SYS_SUSPEND:
  1015. case APM_USER_SUSPEND:
  1016. as->suspends_pending++;
  1017. suspends_pending++;
  1018. break;
  1019. case APM_SYS_STANDBY:
  1020. case APM_USER_STANDBY:
  1021. as->standbys_pending++;
  1022. standbys_pending++;
  1023. break;
  1024. }
  1025. }
  1026. wake_up_interruptible(&apm_waitqueue);
  1027. out:
  1028. spin_unlock(&user_list_lock);
  1029. }
  1030. static void set_time(void)
  1031. {
  1032. if (got_clock_diff) { /* Must know time zone in order to set clock */
  1033. xtime.tv_sec = get_cmos_time() + clock_cmos_diff;
  1034. xtime.tv_nsec = 0;
  1035. }
  1036. }
  1037. static void get_time_diff(void)
  1038. {
  1039. #ifndef CONFIG_APM_RTC_IS_GMT
  1040. /*
  1041. * Estimate time zone so that set_time can update the clock
  1042. */
  1043. clock_cmos_diff = -get_cmos_time();
  1044. clock_cmos_diff += get_seconds();
  1045. got_clock_diff = 1;
  1046. #endif
  1047. }
  1048. static void reinit_timer(void)
  1049. {
  1050. #ifdef INIT_TIMER_AFTER_SUSPEND
  1051. unsigned long flags;
  1052. spin_lock_irqsave(&i8253_lock, flags);
  1053. /* set the clock to 100 Hz */
  1054. outb_p(0x34, PIT_MODE); /* binary, mode 2, LSB/MSB, ch 0 */
  1055. udelay(10);
  1056. outb_p(LATCH & 0xff, PIT_CH0); /* LSB */
  1057. udelay(10);
  1058. outb(LATCH >> 8, PIT_CH0); /* MSB */
  1059. udelay(10);
  1060. spin_unlock_irqrestore(&i8253_lock, flags);
  1061. #endif
  1062. }
  1063. static int suspend(int vetoable)
  1064. {
  1065. int err;
  1066. struct apm_user *as;
  1067. if (pm_send_all(PM_SUSPEND, (void *)3)) {
  1068. /* Vetoed */
  1069. if (vetoable) {
  1070. if (apm_info.connection_version > 0x100)
  1071. set_system_power_state(APM_STATE_REJECT);
  1072. err = -EBUSY;
  1073. ignore_sys_suspend = 0;
  1074. printk(KERN_WARNING "apm: suspend was vetoed.\n");
  1075. goto out;
  1076. }
  1077. printk(KERN_CRIT "apm: suspend was vetoed, but suspending anyway.\n");
  1078. }
  1079. device_suspend(PMSG_SUSPEND);
  1080. local_irq_disable();
  1081. device_power_down(PMSG_SUSPEND);
  1082. /* serialize with the timer interrupt */
  1083. write_seqlock(&xtime_lock);
  1084. /* protect against access to timer chip registers */
  1085. spin_lock(&i8253_lock);
  1086. get_time_diff();
  1087. /*
  1088. * Irq spinlock must be dropped around set_system_power_state.
  1089. * We'll undo any timer changes due to interrupts below.
  1090. */
  1091. spin_unlock(&i8253_lock);
  1092. write_sequnlock(&xtime_lock);
  1093. local_irq_enable();
  1094. save_processor_state();
  1095. err = set_system_power_state(APM_STATE_SUSPEND);
  1096. ignore_normal_resume = 1;
  1097. restore_processor_state();
  1098. local_irq_disable();
  1099. write_seqlock(&xtime_lock);
  1100. spin_lock(&i8253_lock);
  1101. reinit_timer();
  1102. set_time();
  1103. spin_unlock(&i8253_lock);
  1104. write_sequnlock(&xtime_lock);
  1105. if (err == APM_NO_ERROR)
  1106. err = APM_SUCCESS;
  1107. if (err != APM_SUCCESS)
  1108. apm_error("suspend", err);
  1109. err = (err == APM_SUCCESS) ? 0 : -EIO;
  1110. device_power_up();
  1111. local_irq_enable();
  1112. device_resume();
  1113. pm_send_all(PM_RESUME, (void *)0);
  1114. queue_event(APM_NORMAL_RESUME, NULL);
  1115. out:
  1116. spin_lock(&user_list_lock);
  1117. for (as = user_list; as != NULL; as = as->next) {
  1118. as->suspend_wait = 0;
  1119. as->suspend_result = err;
  1120. }
  1121. spin_unlock(&user_list_lock);
  1122. wake_up_interruptible(&apm_suspend_waitqueue);
  1123. return err;
  1124. }
  1125. static void standby(void)
  1126. {
  1127. int err;
  1128. local_irq_disable();
  1129. device_power_down(PMSG_SUSPEND);
  1130. /* serialize with the timer interrupt */
  1131. write_seqlock(&xtime_lock);
  1132. /* If needed, notify drivers here */
  1133. get_time_diff();
  1134. write_sequnlock(&xtime_lock);
  1135. local_irq_enable();
  1136. err = set_system_power_state(APM_STATE_STANDBY);
  1137. if ((err != APM_SUCCESS) && (err != APM_NO_ERROR))
  1138. apm_error("standby", err);
  1139. local_irq_disable();
  1140. device_power_up();
  1141. local_irq_enable();
  1142. }
  1143. static apm_event_t get_event(void)
  1144. {
  1145. int error;
  1146. apm_event_t event;
  1147. apm_eventinfo_t info;
  1148. static int notified;
  1149. /* we don't use the eventinfo */
  1150. error = apm_get_event(&event, &info);
  1151. if (error == APM_SUCCESS)
  1152. return event;
  1153. if ((error != APM_NO_EVENTS) && (notified++ == 0))
  1154. apm_error("get_event", error);
  1155. return 0;
  1156. }
  1157. static void check_events(void)
  1158. {
  1159. apm_event_t event;
  1160. static unsigned long last_resume;
  1161. static int ignore_bounce;
  1162. while ((event = get_event()) != 0) {
  1163. if (debug) {
  1164. if (event <= NR_APM_EVENT_NAME)
  1165. printk(KERN_DEBUG "apm: received %s notify\n",
  1166. apm_event_name[event - 1]);
  1167. else
  1168. printk(KERN_DEBUG "apm: received unknown "
  1169. "event 0x%02x\n", event);
  1170. }
  1171. if (ignore_bounce
  1172. && ((jiffies - last_resume) > bounce_interval))
  1173. ignore_bounce = 0;
  1174. switch (event) {
  1175. case APM_SYS_STANDBY:
  1176. case APM_USER_STANDBY:
  1177. queue_event(event, NULL);
  1178. if (standbys_pending <= 0)
  1179. standby();
  1180. break;
  1181. case APM_USER_SUSPEND:
  1182. #ifdef CONFIG_APM_IGNORE_USER_SUSPEND
  1183. if (apm_info.connection_version > 0x100)
  1184. set_system_power_state(APM_STATE_REJECT);
  1185. break;
  1186. #endif
  1187. case APM_SYS_SUSPEND:
  1188. if (ignore_bounce) {
  1189. if (apm_info.connection_version > 0x100)
  1190. set_system_power_state(APM_STATE_REJECT);
  1191. break;
  1192. }
  1193. /*
  1194. * If we are already processing a SUSPEND,
  1195. * then further SUSPEND events from the BIOS
  1196. * will be ignored. We also return here to
  1197. * cope with the fact that the Thinkpads keep
  1198. * sending a SUSPEND event until something else
  1199. * happens!
  1200. */
  1201. if (ignore_sys_suspend)
  1202. return;
  1203. ignore_sys_suspend = 1;
  1204. queue_event(event, NULL);
  1205. if (suspends_pending <= 0)
  1206. (void) suspend(1);
  1207. break;
  1208. case APM_NORMAL_RESUME:
  1209. case APM_CRITICAL_RESUME:
  1210. case APM_STANDBY_RESUME:
  1211. ignore_sys_suspend = 0;
  1212. last_resume = jiffies;
  1213. ignore_bounce = 1;
  1214. if ((event != APM_NORMAL_RESUME)
  1215. || (ignore_normal_resume == 0)) {
  1216. write_seqlock_irq(&xtime_lock);
  1217. set_time();
  1218. write_sequnlock_irq(&xtime_lock);
  1219. device_resume();
  1220. pm_send_all(PM_RESUME, (void *)0);
  1221. queue_event(event, NULL);
  1222. }
  1223. ignore_normal_resume = 0;
  1224. break;
  1225. case APM_CAPABILITY_CHANGE:
  1226. case APM_LOW_BATTERY:
  1227. case APM_POWER_STATUS_CHANGE:
  1228. queue_event(event, NULL);
  1229. /* If needed, notify drivers here */
  1230. break;
  1231. case APM_UPDATE_TIME:
  1232. write_seqlock_irq(&xtime_lock);
  1233. set_time();
  1234. write_sequnlock_irq(&xtime_lock);
  1235. break;
  1236. case APM_CRITICAL_SUSPEND:
  1237. /*
  1238. * We are not allowed to reject a critical suspend.
  1239. */
  1240. (void) suspend(0);
  1241. break;
  1242. }
  1243. }
  1244. }
  1245. static void apm_event_handler(void)
  1246. {
  1247. static int pending_count = 4;
  1248. int err;
  1249. if ((standbys_pending > 0) || (suspends_pending > 0)) {
  1250. if ((apm_info.connection_version > 0x100) &&
  1251. (pending_count-- <= 0)) {
  1252. pending_count = 4;
  1253. if (debug)
  1254. printk(KERN_DEBUG "apm: setting state busy\n");
  1255. err = set_system_power_state(APM_STATE_BUSY);
  1256. if (err)
  1257. apm_error("busy", err);
  1258. }
  1259. } else
  1260. pending_count = 4;
  1261. check_events();
  1262. }
  1263. /*
  1264. * This is the APM thread main loop.
  1265. */
  1266. static void apm_mainloop(void)
  1267. {
  1268. DECLARE_WAITQUEUE(wait, current);
  1269. add_wait_queue(&apm_waitqueue, &wait);
  1270. set_current_state(TASK_INTERRUPTIBLE);
  1271. for (;;) {
  1272. schedule_timeout(APM_CHECK_TIMEOUT);
  1273. if (exit_kapmd)
  1274. break;
  1275. /*
  1276. * Ok, check all events, check for idle (and mark us sleeping
  1277. * so as not to count towards the load average)..
  1278. */
  1279. set_current_state(TASK_INTERRUPTIBLE);
  1280. apm_event_handler();
  1281. }
  1282. remove_wait_queue(&apm_waitqueue, &wait);
  1283. }
  1284. static int check_apm_user(struct apm_user *as, const char *func)
  1285. {
  1286. if ((as == NULL) || (as->magic != APM_BIOS_MAGIC)) {
  1287. printk(KERN_ERR "apm: %s passed bad filp\n", func);
  1288. return 1;
  1289. }
  1290. return 0;
  1291. }
  1292. static ssize_t do_read(struct file *fp, char __user *buf, size_t count, loff_t *ppos)
  1293. {
  1294. struct apm_user * as;
  1295. int i;
  1296. apm_event_t event;
  1297. as = fp->private_data;
  1298. if (check_apm_user(as, "read"))
  1299. return -EIO;
  1300. if ((int)count < sizeof(apm_event_t))
  1301. return -EINVAL;
  1302. if ((queue_empty(as)) && (fp->f_flags & O_NONBLOCK))
  1303. return -EAGAIN;
  1304. wait_event_interruptible(apm_waitqueue, !queue_empty(as));
  1305. i = count;
  1306. while ((i >= sizeof(event)) && !queue_empty(as)) {
  1307. event = get_queued_event(as);
  1308. if (copy_to_user(buf, &event, sizeof(event))) {
  1309. if (i < count)
  1310. break;
  1311. return -EFAULT;
  1312. }
  1313. switch (event) {
  1314. case APM_SYS_SUSPEND:
  1315. case APM_USER_SUSPEND:
  1316. as->suspends_read++;
  1317. break;
  1318. case APM_SYS_STANDBY:
  1319. case APM_USER_STANDBY:
  1320. as->standbys_read++;
  1321. break;
  1322. }
  1323. buf += sizeof(event);
  1324. i -= sizeof(event);
  1325. }
  1326. if (i < count)
  1327. return count - i;
  1328. if (signal_pending(current))
  1329. return -ERESTARTSYS;
  1330. return 0;
  1331. }
  1332. static unsigned int do_poll(struct file *fp, poll_table * wait)
  1333. {
  1334. struct apm_user * as;
  1335. as = fp->private_data;
  1336. if (check_apm_user(as, "poll"))
  1337. return 0;
  1338. poll_wait(fp, &apm_waitqueue, wait);
  1339. if (!queue_empty(as))
  1340. return POLLIN | POLLRDNORM;
  1341. return 0;
  1342. }
  1343. static int do_ioctl(struct inode * inode, struct file *filp,
  1344. u_int cmd, u_long arg)
  1345. {
  1346. struct apm_user * as;
  1347. as = filp->private_data;
  1348. if (check_apm_user(as, "ioctl"))
  1349. return -EIO;
  1350. if ((!as->suser) || (!as->writer))
  1351. return -EPERM;
  1352. switch (cmd) {
  1353. case APM_IOC_STANDBY:
  1354. if (as->standbys_read > 0) {
  1355. as->standbys_read--;
  1356. as->standbys_pending--;
  1357. standbys_pending--;
  1358. } else
  1359. queue_event(APM_USER_STANDBY, as);
  1360. if (standbys_pending <= 0)
  1361. standby();
  1362. break;
  1363. case APM_IOC_SUSPEND:
  1364. if (as->suspends_read > 0) {
  1365. as->suspends_read--;
  1366. as->suspends_pending--;
  1367. suspends_pending--;
  1368. } else
  1369. queue_event(APM_USER_SUSPEND, as);
  1370. if (suspends_pending <= 0) {
  1371. return suspend(1);
  1372. } else {
  1373. as->suspend_wait = 1;
  1374. wait_event_interruptible(apm_suspend_waitqueue,
  1375. as->suspend_wait == 0);
  1376. return as->suspend_result;
  1377. }
  1378. break;
  1379. default:
  1380. return -EINVAL;
  1381. }
  1382. return 0;
  1383. }
  1384. static int do_release(struct inode * inode, struct file * filp)
  1385. {
  1386. struct apm_user * as;
  1387. as = filp->private_data;
  1388. if (check_apm_user(as, "release"))
  1389. return 0;
  1390. filp->private_data = NULL;
  1391. if (as->standbys_pending > 0) {
  1392. standbys_pending -= as->standbys_pending;
  1393. if (standbys_pending <= 0)
  1394. standby();
  1395. }
  1396. if (as->suspends_pending > 0) {
  1397. suspends_pending -= as->suspends_pending;
  1398. if (suspends_pending <= 0)
  1399. (void) suspend(1);
  1400. }
  1401. spin_lock(&user_list_lock);
  1402. if (user_list == as)
  1403. user_list = as->next;
  1404. else {
  1405. struct apm_user * as1;
  1406. for (as1 = user_list;
  1407. (as1 != NULL) && (as1->next != as);
  1408. as1 = as1->next)
  1409. ;
  1410. if (as1 == NULL)
  1411. printk(KERN_ERR "apm: filp not in user list\n");
  1412. else
  1413. as1->next = as->next;
  1414. }
  1415. spin_unlock(&user_list_lock);
  1416. kfree(as);
  1417. return 0;
  1418. }
  1419. static int do_open(struct inode * inode, struct file * filp)
  1420. {
  1421. struct apm_user * as;
  1422. as = (struct apm_user *)kmalloc(sizeof(*as), GFP_KERNEL);
  1423. if (as == NULL) {
  1424. printk(KERN_ERR "apm: cannot allocate struct of size %d bytes\n",
  1425. sizeof(*as));
  1426. return -ENOMEM;
  1427. }
  1428. as->magic = APM_BIOS_MAGIC;
  1429. as->event_tail = as->event_head = 0;
  1430. as->suspends_pending = as->standbys_pending = 0;
  1431. as->suspends_read = as->standbys_read = 0;
  1432. /*
  1433. * XXX - this is a tiny bit broken, when we consider BSD
  1434. * process accounting. If the device is opened by root, we
  1435. * instantly flag that we used superuser privs. Who knows,
  1436. * we might close the device immediately without doing a
  1437. * privileged operation -- cevans
  1438. */
  1439. as->suser = capable(CAP_SYS_ADMIN);
  1440. as->writer = (filp->f_mode & FMODE_WRITE) == FMODE_WRITE;
  1441. as->reader = (filp->f_mode & FMODE_READ) == FMODE_READ;
  1442. spin_lock(&user_list_lock);
  1443. as->next = user_list;
  1444. user_list = as;
  1445. spin_unlock(&user_list_lock);
  1446. filp->private_data = as;
  1447. return 0;
  1448. }
  1449. static int apm_get_info(char *buf, char **start, off_t fpos, int length)
  1450. {
  1451. char * p;
  1452. unsigned short bx;
  1453. unsigned short cx;
  1454. unsigned short dx;
  1455. int error;
  1456. unsigned short ac_line_status = 0xff;
  1457. unsigned short battery_status = 0xff;
  1458. unsigned short battery_flag = 0xff;
  1459. int percentage = -1;
  1460. int time_units = -1;
  1461. char *units = "?";
  1462. p = buf;
  1463. if ((num_online_cpus() == 1) &&
  1464. !(error = apm_get_power_status(&bx, &cx, &dx))) {
  1465. ac_line_status = (bx >> 8) & 0xff;
  1466. battery_status = bx & 0xff;
  1467. if ((cx & 0xff) != 0xff)
  1468. percentage = cx & 0xff;
  1469. if (apm_info.connection_version > 0x100) {
  1470. battery_flag = (cx >> 8) & 0xff;
  1471. if (dx != 0xffff) {
  1472. units = (dx & 0x8000) ? "min" : "sec";
  1473. time_units = dx & 0x7fff;
  1474. }
  1475. }
  1476. }
  1477. /* Arguments, with symbols from linux/apm_bios.h. Information is
  1478. from the Get Power Status (0x0a) call unless otherwise noted.
  1479. 0) Linux driver version (this will change if format changes)
  1480. 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
  1481. 2) APM flags from APM Installation Check (0x00):
  1482. bit 0: APM_16_BIT_SUPPORT
  1483. bit 1: APM_32_BIT_SUPPORT
  1484. bit 2: APM_IDLE_SLOWS_CLOCK
  1485. bit 3: APM_BIOS_DISABLED
  1486. bit 4: APM_BIOS_DISENGAGED
  1487. 3) AC line status
  1488. 0x00: Off-line
  1489. 0x01: On-line
  1490. 0x02: On backup power (BIOS >= 1.1 only)
  1491. 0xff: Unknown
  1492. 4) Battery status
  1493. 0x00: High
  1494. 0x01: Low
  1495. 0x02: Critical
  1496. 0x03: Charging
  1497. 0x04: Selected battery not present (BIOS >= 1.2 only)
  1498. 0xff: Unknown
  1499. 5) Battery flag
  1500. bit 0: High
  1501. bit 1: Low
  1502. bit 2: Critical
  1503. bit 3: Charging
  1504. bit 7: No system battery
  1505. 0xff: Unknown
  1506. 6) Remaining battery life (percentage of charge):
  1507. 0-100: valid
  1508. -1: Unknown
  1509. 7) Remaining battery life (time units):
  1510. Number of remaining minutes or seconds
  1511. -1: Unknown
  1512. 8) min = minutes; sec = seconds */
  1513. p += sprintf(p, "%s %d.%d 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
  1514. driver_version,
  1515. (apm_info.bios.version >> 8) & 0xff,
  1516. apm_info.bios.version & 0xff,
  1517. apm_info.bios.flags,
  1518. ac_line_status,
  1519. battery_status,
  1520. battery_flag,
  1521. percentage,
  1522. time_units,
  1523. units);
  1524. return p - buf;
  1525. }
  1526. static int apm(void *unused)
  1527. {
  1528. unsigned short bx;
  1529. unsigned short cx;
  1530. unsigned short dx;
  1531. int error;
  1532. char * power_stat;
  1533. char * bat_stat;
  1534. kapmd_running = 1;
  1535. daemonize("kapmd");
  1536. current->flags |= PF_NOFREEZE;
  1537. #ifdef CONFIG_SMP
  1538. /* 2002/08/01 - WT
  1539. * This is to avoid random crashes at boot time during initialization
  1540. * on SMP systems in case of "apm=power-off" mode. Seen on ASUS A7M266D.
  1541. * Some bioses don't like being called from CPU != 0.
  1542. * Method suggested by Ingo Molnar.
  1543. */
  1544. set_cpus_allowed(current, cpumask_of_cpu(0));
  1545. BUG_ON(smp_processor_id() != 0);
  1546. #endif
  1547. if (apm_info.connection_version == 0) {
  1548. apm_info.connection_version = apm_info.bios.version;
  1549. if (apm_info.connection_version > 0x100) {
  1550. /*
  1551. * We only support BIOSs up to version 1.2
  1552. */
  1553. if (apm_info.connection_version > 0x0102)
  1554. apm_info.connection_version = 0x0102;
  1555. error = apm_driver_version(&apm_info.connection_version);
  1556. if (error != APM_SUCCESS) {
  1557. apm_error("driver version", error);
  1558. /* Fall back to an APM 1.0 connection. */
  1559. apm_info.connection_version = 0x100;
  1560. }
  1561. }
  1562. }
  1563. if (debug)
  1564. printk(KERN_INFO "apm: Connection version %d.%d\n",
  1565. (apm_info.connection_version >> 8) & 0xff,
  1566. apm_info.connection_version & 0xff);
  1567. #ifdef CONFIG_APM_DO_ENABLE
  1568. if (apm_info.bios.flags & APM_BIOS_DISABLED) {
  1569. /*
  1570. * This call causes my NEC UltraLite Versa 33/C to hang if it
  1571. * is booted with PM disabled but not in the docking station.
  1572. * Unfortunate ...
  1573. */
  1574. error = apm_enable_power_management(1);
  1575. if (error) {
  1576. apm_error("enable power management", error);
  1577. return -1;
  1578. }
  1579. }
  1580. #endif
  1581. if ((apm_info.bios.flags & APM_BIOS_DISENGAGED)
  1582. && (apm_info.connection_version > 0x0100)) {
  1583. error = apm_engage_power_management(APM_DEVICE_ALL, 1);
  1584. if (error) {
  1585. apm_error("engage power management", error);
  1586. return -1;
  1587. }
  1588. }
  1589. if (debug && (num_online_cpus() == 1 || smp )) {
  1590. error = apm_get_power_status(&bx, &cx, &dx);
  1591. if (error)
  1592. printk(KERN_INFO "apm: power status not available\n");
  1593. else {
  1594. switch ((bx >> 8) & 0xff) {
  1595. case 0: power_stat = "off line"; break;
  1596. case 1: power_stat = "on line"; break;
  1597. case 2: power_stat = "on backup power"; break;
  1598. default: power_stat = "unknown"; break;
  1599. }
  1600. switch (bx & 0xff) {
  1601. case 0: bat_stat = "high"; break;
  1602. case 1: bat_stat = "low"; break;
  1603. case 2: bat_stat = "critical"; break;
  1604. case 3: bat_stat = "charging"; break;
  1605. default: bat_stat = "unknown"; break;
  1606. }
  1607. printk(KERN_INFO
  1608. "apm: AC %s, battery status %s, battery life ",
  1609. power_stat, bat_stat);
  1610. if ((cx & 0xff) == 0xff)
  1611. printk("unknown\n");
  1612. else
  1613. printk("%d%%\n", cx & 0xff);
  1614. if (apm_info.connection_version > 0x100) {
  1615. printk(KERN_INFO
  1616. "apm: battery flag 0x%02x, battery life ",
  1617. (cx >> 8) & 0xff);
  1618. if (dx == 0xffff)
  1619. printk("unknown\n");
  1620. else
  1621. printk("%d %s\n", dx & 0x7fff,
  1622. (dx & 0x8000) ?
  1623. "minutes" : "seconds");
  1624. }
  1625. }
  1626. }
  1627. /* Install our power off handler.. */
  1628. if (power_off)
  1629. pm_power_off = apm_power_off;
  1630. if (num_online_cpus() == 1 || smp) {
  1631. #if defined(CONFIG_APM_DISPLAY_BLANK) && defined(CONFIG_VT)
  1632. console_blank_hook = apm_console_blank;
  1633. #endif
  1634. apm_mainloop();
  1635. #if defined(CONFIG_APM_DISPLAY_BLANK) && defined(CONFIG_VT)
  1636. console_blank_hook = NULL;
  1637. #endif
  1638. }
  1639. kapmd_running = 0;
  1640. return 0;
  1641. }
  1642. #ifndef MODULE
  1643. static int __init apm_setup(char *str)
  1644. {
  1645. int invert;
  1646. while ((str != NULL) && (*str != '\0')) {
  1647. if (strncmp(str, "off", 3) == 0)
  1648. apm_disabled = 1;
  1649. if (strncmp(str, "on", 2) == 0)
  1650. apm_disabled = 0;
  1651. if ((strncmp(str, "bounce-interval=", 16) == 0) ||
  1652. (strncmp(str, "bounce_interval=", 16) == 0))
  1653. bounce_interval = simple_strtol(str + 16, NULL, 0);
  1654. if ((strncmp(str, "idle-threshold=", 15) == 0) ||
  1655. (strncmp(str, "idle_threshold=", 15) == 0))
  1656. idle_threshold = simple_strtol(str + 15, NULL, 0);
  1657. if ((strncmp(str, "idle-period=", 12) == 0) ||
  1658. (strncmp(str, "idle_period=", 12) == 0))
  1659. idle_period = simple_strtol(str + 12, NULL, 0);
  1660. invert = (strncmp(str, "no-", 3) == 0) ||
  1661. (strncmp(str, "no_", 3) == 0);
  1662. if (invert)
  1663. str += 3;
  1664. if (strncmp(str, "debug", 5) == 0)
  1665. debug = !invert;
  1666. if ((strncmp(str, "power-off", 9) == 0) ||
  1667. (strncmp(str, "power_off", 9) == 0))
  1668. power_off = !invert;
  1669. if (strncmp(str, "smp", 3) == 0)
  1670. {
  1671. smp = !invert;
  1672. idle_threshold = 100;
  1673. }
  1674. if ((strncmp(str, "allow-ints", 10) == 0) ||
  1675. (strncmp(str, "allow_ints", 10) == 0))
  1676. apm_info.allow_ints = !invert;
  1677. if ((strncmp(str, "broken-psr", 10) == 0) ||
  1678. (strncmp(str, "broken_psr", 10) == 0))
  1679. apm_info.get_power_status_broken = !invert;
  1680. if ((strncmp(str, "realmode-power-off", 18) == 0) ||
  1681. (strncmp(str, "realmode_power_off", 18) == 0))
  1682. apm_info.realmode_power_off = !invert;
  1683. str = strchr(str, ',');
  1684. if (str != NULL)
  1685. str += strspn(str, ", \t");
  1686. }
  1687. return 1;
  1688. }
  1689. __setup("apm=", apm_setup);
  1690. #endif
  1691. static struct file_operations apm_bios_fops = {
  1692. .owner = THIS_MODULE,
  1693. .read = do_read,
  1694. .poll = do_poll,
  1695. .ioctl = do_ioctl,
  1696. .open = do_open,
  1697. .release = do_release,
  1698. };
  1699. static struct miscdevice apm_device = {
  1700. APM_MINOR_DEV,
  1701. "apm_bios",
  1702. &apm_bios_fops
  1703. };
  1704. /* Simple "print if true" callback */
  1705. static int __init print_if_true(struct dmi_system_id *d)
  1706. {
  1707. printk("%s\n", d->ident);
  1708. return 0;
  1709. }
  1710. /*
  1711. * Some Bioses enable the PS/2 mouse (touchpad) at resume, even if it was
  1712. * disabled before the suspend. Linux used to get terribly confused by that.
  1713. */
  1714. static int __init broken_ps2_resume(struct dmi_system_id *d)
  1715. {
  1716. printk(KERN_INFO "%s machine detected. Mousepad Resume Bug workaround hopefully not needed.\n", d->ident);
  1717. return 0;
  1718. }
  1719. /* Some bioses have a broken protected mode poweroff and need to use realmode */
  1720. static int __init set_realmode_power_off(struct dmi_system_id *d)
  1721. {
  1722. if (apm_info.realmode_power_off == 0) {
  1723. apm_info.realmode_power_off = 1;
  1724. printk(KERN_INFO "%s bios detected. Using realmode poweroff only.\n", d->ident);
  1725. }
  1726. return 0;
  1727. }
  1728. /* Some laptops require interrupts to be enabled during APM calls */
  1729. static int __init set_apm_ints(struct dmi_system_id *d)
  1730. {
  1731. if (apm_info.allow_ints == 0) {
  1732. apm_info.allow_ints = 1;
  1733. printk(KERN_INFO "%s machine detected. Enabling interrupts during APM calls.\n", d->ident);
  1734. }
  1735. return 0;
  1736. }
  1737. /* Some APM bioses corrupt memory or just plain do not work */
  1738. static int __init apm_is_horked(struct dmi_system_id *d)
  1739. {
  1740. if (apm_info.disabled == 0) {
  1741. apm_info.disabled = 1;
  1742. printk(KERN_INFO "%s machine detected. Disabling APM.\n", d->ident);
  1743. }
  1744. return 0;
  1745. }
  1746. static int __init apm_is_horked_d850md(struct dmi_system_id *d)
  1747. {
  1748. if (apm_info.disabled == 0) {
  1749. apm_info.disabled = 1;
  1750. printk(KERN_INFO "%s machine detected. Disabling APM.\n", d->ident);
  1751. printk(KERN_INFO "This bug is fixed in bios P15 which is available for \n");
  1752. printk(KERN_INFO "download from support.intel.com \n");
  1753. }
  1754. return 0;
  1755. }
  1756. /* Some APM bioses hang on APM idle calls */
  1757. static int __init apm_likes_to_melt(struct dmi_system_id *d)
  1758. {
  1759. if (apm_info.forbid_idle == 0) {
  1760. apm_info.forbid_idle = 1;
  1761. printk(KERN_INFO "%s machine detected. Disabling APM idle calls.\n", d->ident);
  1762. }
  1763. return 0;
  1764. }
  1765. /*
  1766. * Check for clue free BIOS implementations who use
  1767. * the following QA technique
  1768. *
  1769. * [ Write BIOS Code ]<------
  1770. * | ^
  1771. * < Does it Compile >----N--
  1772. * |Y ^
  1773. * < Does it Boot Win98 >-N--
  1774. * |Y
  1775. * [Ship It]
  1776. *
  1777. * Phoenix A04 08/24/2000 is known bad (Dell Inspiron 5000e)
  1778. * Phoenix A07 09/29/2000 is known good (Dell Inspiron 5000)
  1779. */
  1780. static int __init broken_apm_power(struct dmi_system_id *d)
  1781. {
  1782. apm_info.get_power_status_broken = 1;
  1783. printk(KERN_WARNING "BIOS strings suggest APM bugs, disabling power status reporting.\n");
  1784. return 0;
  1785. }
  1786. /*
  1787. * This bios swaps the APM minute reporting bytes over (Many sony laptops
  1788. * have this problem).
  1789. */
  1790. static int __init swab_apm_power_in_minutes(struct dmi_system_id *d)
  1791. {
  1792. apm_info.get_power_status_swabinminutes = 1;
  1793. printk(KERN_WARNING "BIOS strings suggest APM reports battery life in minutes and wrong byte order.\n");
  1794. return 0;
  1795. }
  1796. static struct dmi_system_id __initdata apm_dmi_table[] = {
  1797. {
  1798. print_if_true,
  1799. KERN_WARNING "IBM T23 - BIOS 1.03b+ and controller firmware 1.02+ may be needed for Linux APM.",
  1800. { DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
  1801. DMI_MATCH(DMI_BIOS_VERSION, "1AET38WW (1.01b)"), },
  1802. },
  1803. { /* Handle problems with APM on the C600 */
  1804. broken_ps2_resume, "Dell Latitude C600",
  1805. { DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
  1806. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude C600"), },
  1807. },
  1808. { /* Allow interrupts during suspend on Dell Latitude laptops*/
  1809. set_apm_ints, "Dell Latitude",
  1810. { DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  1811. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude C510"), }
  1812. },
  1813. { /* APM crashes */
  1814. apm_is_horked, "Dell Inspiron 2500",
  1815. { DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  1816. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 2500"),
  1817. DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
  1818. DMI_MATCH(DMI_BIOS_VERSION,"A11"), },
  1819. },
  1820. { /* Allow interrupts during suspend on Dell Inspiron laptops*/
  1821. set_apm_ints, "Dell Inspiron", {
  1822. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  1823. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 4000"), },
  1824. },
  1825. { /* Handle problems with APM on Inspiron 5000e */
  1826. broken_apm_power, "Dell Inspiron 5000e",
  1827. { DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  1828. DMI_MATCH(DMI_BIOS_VERSION, "A04"),
  1829. DMI_MATCH(DMI_BIOS_DATE, "08/24/2000"), },
  1830. },
  1831. { /* Handle problems with APM on Inspiron 2500 */
  1832. broken_apm_power, "Dell Inspiron 2500",
  1833. { DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  1834. DMI_MATCH(DMI_BIOS_VERSION, "A12"),
  1835. DMI_MATCH(DMI_BIOS_DATE, "02/04/2002"), },
  1836. },
  1837. { /* APM crashes */
  1838. apm_is_horked, "Dell Dimension 4100",
  1839. { DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  1840. DMI_MATCH(DMI_PRODUCT_NAME, "XPS-Z"),
  1841. DMI_MATCH(DMI_BIOS_VENDOR,"Intel Corp."),
  1842. DMI_MATCH(DMI_BIOS_VERSION,"A11"), },
  1843. },
  1844. { /* Allow interrupts during suspend on Compaq Laptops*/
  1845. set_apm_ints, "Compaq 12XL125",
  1846. { DMI_MATCH(DMI_SYS_VENDOR, "Compaq"),
  1847. DMI_MATCH(DMI_PRODUCT_NAME, "Compaq PC"),
  1848. DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  1849. DMI_MATCH(DMI_BIOS_VERSION,"4.06"), },
  1850. },
  1851. { /* Allow interrupts during APM or the clock goes slow */
  1852. set_apm_ints, "ASUSTeK",
  1853. { DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  1854. DMI_MATCH(DMI_PRODUCT_NAME, "L8400K series Notebook PC"), },
  1855. },
  1856. { /* APM blows on shutdown */
  1857. apm_is_horked, "ABIT KX7-333[R]",
  1858. { DMI_MATCH(DMI_BOARD_VENDOR, "ABIT"),
  1859. DMI_MATCH(DMI_BOARD_NAME, "VT8367-8233A (KX7-333[R])"), },
  1860. },
  1861. { /* APM crashes */
  1862. apm_is_horked, "Trigem Delhi3",
  1863. { DMI_MATCH(DMI_SYS_VENDOR, "TriGem Computer, Inc"),
  1864. DMI_MATCH(DMI_PRODUCT_NAME, "Delhi3"), },
  1865. },
  1866. { /* APM crashes */
  1867. apm_is_horked, "Fujitsu-Siemens",
  1868. { DMI_MATCH(DMI_BIOS_VENDOR, "hoenix/FUJITSU SIEMENS"),
  1869. DMI_MATCH(DMI_BIOS_VERSION, "Version1.01"), },
  1870. },
  1871. { /* APM crashes */
  1872. apm_is_horked_d850md, "Intel D850MD",
  1873. { DMI_MATCH(DMI_BIOS_VENDOR, "Intel Corp."),
  1874. DMI_MATCH(DMI_BIOS_VERSION, "MV85010A.86A.0016.P07.0201251536"), },
  1875. },
  1876. { /* APM crashes */
  1877. apm_is_horked, "Intel D810EMO",
  1878. { DMI_MATCH(DMI_BIOS_VENDOR, "Intel Corp."),
  1879. DMI_MATCH(DMI_BIOS_VERSION, "MO81010A.86A.0008.P04.0004170800"), },
  1880. },
  1881. { /* APM crashes */
  1882. apm_is_horked, "Dell XPS-Z",
  1883. { DMI_MATCH(DMI_BIOS_VENDOR, "Intel Corp."),
  1884. DMI_MATCH(DMI_BIOS_VERSION, "A11"),
  1885. DMI_MATCH(DMI_PRODUCT_NAME, "XPS-Z"), },
  1886. },
  1887. { /* APM crashes */
  1888. apm_is_horked, "Sharp PC-PJ/AX",
  1889. { DMI_MATCH(DMI_SYS_VENDOR, "SHARP"),
  1890. DMI_MATCH(DMI_PRODUCT_NAME, "PC-PJ/AX"),
  1891. DMI_MATCH(DMI_BIOS_VENDOR,"SystemSoft"),
  1892. DMI_MATCH(DMI_BIOS_VERSION,"Version R2.08"), },
  1893. },
  1894. { /* APM crashes */
  1895. apm_is_horked, "Dell Inspiron 2500",
  1896. { DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  1897. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 2500"),
  1898. DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
  1899. DMI_MATCH(DMI_BIOS_VERSION,"A11"), },
  1900. },
  1901. { /* APM idle hangs */
  1902. apm_likes_to_melt, "Jabil AMD",
  1903. { DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
  1904. DMI_MATCH(DMI_BIOS_VERSION, "0AASNP06"), },
  1905. },
  1906. { /* APM idle hangs */
  1907. apm_likes_to_melt, "AMI Bios",
  1908. { DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
  1909. DMI_MATCH(DMI_BIOS_VERSION, "0AASNP05"), },
  1910. },
  1911. { /* Handle problems with APM on Sony Vaio PCG-N505X(DE) */
  1912. swab_apm_power_in_minutes, "Sony VAIO",
  1913. { DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  1914. DMI_MATCH(DMI_BIOS_VERSION, "R0206H"),
  1915. DMI_MATCH(DMI_BIOS_DATE, "08/23/99"), },
  1916. },
  1917. { /* Handle problems with APM on Sony Vaio PCG-N505VX */
  1918. swab_apm_power_in_minutes, "Sony VAIO",
  1919. { DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  1920. DMI_MATCH(DMI_BIOS_VERSION, "W2K06H0"),
  1921. DMI_MATCH(DMI_BIOS_DATE, "02/03/00"), },
  1922. },
  1923. { /* Handle problems with APM on Sony Vaio PCG-XG29 */
  1924. swab_apm_power_in_minutes, "Sony VAIO",
  1925. { DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  1926. DMI_MATCH(DMI_BIOS_VERSION, "R0117A0"),
  1927. DMI_MATCH(DMI_BIOS_DATE, "04/25/00"), },
  1928. },
  1929. { /* Handle problems with APM on Sony Vaio PCG-Z600NE */
  1930. swab_apm_power_in_minutes, "Sony VAIO",
  1931. { DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  1932. DMI_MATCH(DMI_BIOS_VERSION, "R0121Z1"),
  1933. DMI_MATCH(DMI_BIOS_DATE, "05/11/00"), },
  1934. },
  1935. { /* Handle problems with APM on Sony Vaio PCG-Z600NE */
  1936. swab_apm_power_in_minutes, "Sony VAIO",
  1937. { DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  1938. DMI_MATCH(DMI_BIOS_VERSION, "WME01Z1"),
  1939. DMI_MATCH(DMI_BIOS_DATE, "08/11/00"), },
  1940. },
  1941. { /* Handle problems with APM on Sony Vaio PCG-Z600LEK(DE) */
  1942. swab_apm_power_in_minutes, "Sony VAIO",
  1943. { DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  1944. DMI_MATCH(DMI_BIOS_VERSION, "R0206Z3"),
  1945. DMI_MATCH(DMI_BIOS_DATE, "12/25/00"), },
  1946. },
  1947. { /* Handle problems with APM on Sony Vaio PCG-Z505LS */
  1948. swab_apm_power_in_minutes, "Sony VAIO",
  1949. { DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  1950. DMI_MATCH(DMI_BIOS_VERSION, "R0203D0"),
  1951. DMI_MATCH(DMI_BIOS_DATE, "05/12/00"), },
  1952. },
  1953. { /* Handle problems with APM on Sony Vaio PCG-Z505LS */
  1954. swab_apm_power_in_minutes, "Sony VAIO",
  1955. { DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  1956. DMI_MATCH(DMI_BIOS_VERSION, "R0203Z3"),
  1957. DMI_MATCH(DMI_BIOS_DATE, "08/25/00"), },
  1958. },
  1959. { /* Handle problems with APM on Sony Vaio PCG-Z505LS (with updated BIOS) */
  1960. swab_apm_power_in_minutes, "Sony VAIO",
  1961. { DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  1962. DMI_MATCH(DMI_BIOS_VERSION, "R0209Z3"),
  1963. DMI_MATCH(DMI_BIOS_DATE, "05/12/01"), },
  1964. },
  1965. { /* Handle problems with APM on Sony Vaio PCG-F104K */
  1966. swab_apm_power_in_minutes, "Sony VAIO",
  1967. { DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  1968. DMI_MATCH(DMI_BIOS_VERSION, "R0204K2"),
  1969. DMI_MATCH(DMI_BIOS_DATE, "08/28/00"), },
  1970. },
  1971. { /* Handle problems with APM on Sony Vaio PCG-C1VN/C1VE */
  1972. swab_apm_power_in_minutes, "Sony VAIO",
  1973. { DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  1974. DMI_MATCH(DMI_BIOS_VERSION, "R0208P1"),
  1975. DMI_MATCH(DMI_BIOS_DATE, "11/09/00"), },
  1976. },
  1977. { /* Handle problems with APM on Sony Vaio PCG-C1VE */
  1978. swab_apm_power_in_minutes, "Sony VAIO",
  1979. { DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  1980. DMI_MATCH(DMI_BIOS_VERSION, "R0204P1"),
  1981. DMI_MATCH(DMI_BIOS_DATE, "09/12/00"), },
  1982. },
  1983. { /* Handle problems with APM on Sony Vaio PCG-C1VE */
  1984. swab_apm_power_in_minutes, "Sony VAIO",
  1985. { DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  1986. DMI_MATCH(DMI_BIOS_VERSION, "WXPO1Z3"),
  1987. DMI_MATCH(DMI_BIOS_DATE, "10/26/01"), },
  1988. },
  1989. { /* broken PM poweroff bios */
  1990. set_realmode_power_off, "Award Software v4.60 PGMA",
  1991. { DMI_MATCH(DMI_BIOS_VENDOR, "Award Software International, Inc."),
  1992. DMI_MATCH(DMI_BIOS_VERSION, "4.60 PGMA"),
  1993. DMI_MATCH(DMI_BIOS_DATE, "134526184"), },
  1994. },
  1995. /* Generic per vendor APM settings */
  1996. { /* Allow interrupts during suspend on IBM laptops */
  1997. set_apm_ints, "IBM",
  1998. { DMI_MATCH(DMI_SYS_VENDOR, "IBM"), },
  1999. },
  2000. { }
  2001. };
  2002. /*
  2003. * Just start the APM thread. We do NOT want to do APM BIOS
  2004. * calls from anything but the APM thread, if for no other reason
  2005. * than the fact that we don't trust the APM BIOS. This way,
  2006. * most common APM BIOS problems that lead to protection errors
  2007. * etc will have at least some level of being contained...
  2008. *
  2009. * In short, if something bad happens, at least we have a choice
  2010. * of just killing the apm thread..
  2011. */
  2012. static int __init apm_init(void)
  2013. {
  2014. struct proc_dir_entry *apm_proc;
  2015. int ret;
  2016. int i;
  2017. dmi_check_system(apm_dmi_table);
  2018. if (apm_info.bios.version == 0) {
  2019. printk(KERN_INFO "apm: BIOS not found.\n");
  2020. return -ENODEV;
  2021. }
  2022. printk(KERN_INFO
  2023. "apm: BIOS version %d.%d Flags 0x%02x (Driver version %s)\n",
  2024. ((apm_info.bios.version >> 8) & 0xff),
  2025. (apm_info.bios.version & 0xff),
  2026. apm_info.bios.flags,
  2027. driver_version);
  2028. if ((apm_info.bios.flags & APM_32_BIT_SUPPORT) == 0) {
  2029. printk(KERN_INFO "apm: no 32 bit BIOS support\n");
  2030. return -ENODEV;
  2031. }
  2032. if (allow_ints)
  2033. apm_info.allow_ints = 1;
  2034. if (broken_psr)
  2035. apm_info.get_power_status_broken = 1;
  2036. if (realmode_power_off)
  2037. apm_info.realmode_power_off = 1;
  2038. /* User can override, but default is to trust DMI */
  2039. if (apm_disabled != -1)
  2040. apm_info.disabled = apm_disabled;
  2041. /*
  2042. * Fix for the Compaq Contura 3/25c which reports BIOS version 0.1
  2043. * but is reportedly a 1.0 BIOS.
  2044. */
  2045. if (apm_info.bios.version == 0x001)
  2046. apm_info.bios.version = 0x100;
  2047. /* BIOS < 1.2 doesn't set cseg_16_len */
  2048. if (apm_info.bios.version < 0x102)
  2049. apm_info.bios.cseg_16_len = 0; /* 64k */
  2050. if (debug) {
  2051. printk(KERN_INFO "apm: entry %x:%lx cseg16 %x dseg %x",
  2052. apm_info.bios.cseg, apm_info.bios.offset,
  2053. apm_info.bios.cseg_16, apm_info.bios.dseg);
  2054. if (apm_info.bios.version > 0x100)
  2055. printk(" cseg len %x, dseg len %x",
  2056. apm_info.bios.cseg_len,
  2057. apm_info.bios.dseg_len);
  2058. if (apm_info.bios.version > 0x101)
  2059. printk(" cseg16 len %x", apm_info.bios.cseg_16_len);
  2060. printk("\n");
  2061. }
  2062. if (apm_info.disabled) {
  2063. printk(KERN_NOTICE "apm: disabled on user request.\n");
  2064. return -ENODEV;
  2065. }
  2066. if ((num_online_cpus() > 1) && !power_off && !smp) {
  2067. printk(KERN_NOTICE "apm: disabled - APM is not SMP safe.\n");
  2068. apm_info.disabled = 1;
  2069. return -ENODEV;
  2070. }
  2071. if (PM_IS_ACTIVE()) {
  2072. printk(KERN_NOTICE "apm: overridden by ACPI.\n");
  2073. apm_info.disabled = 1;
  2074. return -ENODEV;
  2075. }
  2076. pm_active = 1;
  2077. /*
  2078. * Set up a segment that references the real mode segment 0x40
  2079. * that extends up to the end of page zero (that we have reserved).
  2080. * This is for buggy BIOS's that refer to (real mode) segment 0x40
  2081. * even though they are called in protected mode.
  2082. */
  2083. set_base(bad_bios_desc, __va((unsigned long)0x40 << 4));
  2084. _set_limit((char *)&bad_bios_desc, 4095 - (0x40 << 4));
  2085. apm_bios_entry.offset = apm_info.bios.offset;
  2086. apm_bios_entry.segment = APM_CS;
  2087. for (i = 0; i < NR_CPUS; i++) {
  2088. set_base(per_cpu(cpu_gdt_table, i)[APM_CS >> 3],
  2089. __va((unsigned long)apm_info.bios.cseg << 4));
  2090. set_base(per_cpu(cpu_gdt_table, i)[APM_CS_16 >> 3],
  2091. __va((unsigned long)apm_info.bios.cseg_16 << 4));
  2092. set_base(per_cpu(cpu_gdt_table, i)[APM_DS >> 3],
  2093. __va((unsigned long)apm_info.bios.dseg << 4));
  2094. #ifndef APM_RELAX_SEGMENTS
  2095. if (apm_info.bios.version == 0x100) {
  2096. #endif
  2097. /* For ASUS motherboard, Award BIOS rev 110 (and others?) */
  2098. _set_limit((char *)&per_cpu(cpu_gdt_table, i)[APM_CS >> 3], 64 * 1024 - 1);
  2099. /* For some unknown machine. */
  2100. _set_limit((char *)&per_cpu(cpu_gdt_table, i)[APM_CS_16 >> 3], 64 * 1024 - 1);
  2101. /* For the DEC Hinote Ultra CT475 (and others?) */
  2102. _set_limit((char *)&per_cpu(cpu_gdt_table, i)[APM_DS >> 3], 64 * 1024 - 1);
  2103. #ifndef APM_RELAX_SEGMENTS
  2104. } else {
  2105. _set_limit((char *)&per_cpu(cpu_gdt_table, i)[APM_CS >> 3],
  2106. (apm_info.bios.cseg_len - 1) & 0xffff);
  2107. _set_limit((char *)&per_cpu(cpu_gdt_table, i)[APM_CS_16 >> 3],
  2108. (apm_info.bios.cseg_16_len - 1) & 0xffff);
  2109. _set_limit((char *)&per_cpu(cpu_gdt_table, i)[APM_DS >> 3],
  2110. (apm_info.bios.dseg_len - 1) & 0xffff);
  2111. /* workaround for broken BIOSes */
  2112. if (apm_info.bios.cseg_len <= apm_info.bios.offset)
  2113. _set_limit((char *)&per_cpu(cpu_gdt_table, i)[APM_CS >> 3], 64 * 1024 -1);
  2114. if (apm_info.bios.dseg_len <= 0x40) { /* 0x40 * 4kB == 64kB */
  2115. /* for the BIOS that assumes granularity = 1 */
  2116. per_cpu(cpu_gdt_table, i)[APM_DS >> 3].b |= 0x800000;
  2117. printk(KERN_NOTICE "apm: we set the granularity of dseg.\n");
  2118. }
  2119. }
  2120. #endif
  2121. }
  2122. apm_proc = create_proc_info_entry("apm", 0, NULL, apm_get_info);
  2123. if (apm_proc)
  2124. apm_proc->owner = THIS_MODULE;
  2125. ret = kernel_thread(apm, NULL, CLONE_KERNEL | SIGCHLD);
  2126. if (ret < 0) {
  2127. printk(KERN_ERR "apm: disabled - Unable to start kernel thread.\n");
  2128. return -ENOMEM;
  2129. }
  2130. if (num_online_cpus() > 1 && !smp ) {
  2131. printk(KERN_NOTICE
  2132. "apm: disabled - APM is not SMP safe (power off active).\n");
  2133. return 0;
  2134. }
  2135. misc_register(&apm_device);
  2136. if (HZ != 100)
  2137. idle_period = (idle_period * HZ) / 100;
  2138. if (idle_threshold < 100) {
  2139. original_pm_idle = pm_idle;
  2140. pm_idle = apm_cpu_idle;
  2141. set_pm_idle = 1;
  2142. }
  2143. return 0;
  2144. }
  2145. static void __exit apm_exit(void)
  2146. {
  2147. int error;
  2148. if (set_pm_idle) {
  2149. pm_idle = original_pm_idle;
  2150. /*
  2151. * We are about to unload the current idle thread pm callback
  2152. * (pm_idle), Wait for all processors to update cached/local
  2153. * copies of pm_idle before proceeding.
  2154. */
  2155. cpu_idle_wait();
  2156. }
  2157. if (((apm_info.bios.flags & APM_BIOS_DISENGAGED) == 0)
  2158. && (apm_info.connection_version > 0x0100)) {
  2159. error = apm_engage_power_management(APM_DEVICE_ALL, 0);
  2160. if (error)
  2161. apm_error("disengage power management", error);
  2162. }
  2163. misc_deregister(&apm_device);
  2164. remove_proc_entry("apm", NULL);
  2165. if (power_off)
  2166. pm_power_off = NULL;
  2167. exit_kapmd = 1;
  2168. while (kapmd_running)
  2169. schedule();
  2170. pm_active = 0;
  2171. }
  2172. module_init(apm_init);
  2173. module_exit(apm_exit);
  2174. MODULE_AUTHOR("Stephen Rothwell");
  2175. MODULE_DESCRIPTION("Advanced Power Management");
  2176. MODULE_LICENSE("GPL");
  2177. module_param(debug, bool, 0644);
  2178. MODULE_PARM_DESC(debug, "Enable debug mode");
  2179. module_param(power_off, bool, 0444);
  2180. MODULE_PARM_DESC(power_off, "Enable power off");
  2181. module_param(bounce_interval, int, 0444);
  2182. MODULE_PARM_DESC(bounce_interval,
  2183. "Set the number of ticks to ignore suspend bounces");
  2184. module_param(allow_ints, bool, 0444);
  2185. MODULE_PARM_DESC(allow_ints, "Allow interrupts during BIOS calls");
  2186. module_param(broken_psr, bool, 0444);
  2187. MODULE_PARM_DESC(broken_psr, "BIOS has a broken GetPowerStatus call");
  2188. module_param(realmode_power_off, bool, 0444);
  2189. MODULE_PARM_DESC(realmode_power_off,
  2190. "Switch to real mode before powering off");
  2191. module_param(idle_threshold, int, 0444);
  2192. MODULE_PARM_DESC(idle_threshold,
  2193. "System idle percentage above which to make APM BIOS idle calls");
  2194. module_param(idle_period, int, 0444);
  2195. MODULE_PARM_DESC(idle_period,
  2196. "Period (in sec/100) over which to caculate the idle percentage");
  2197. module_param(smp, bool, 0444);
  2198. MODULE_PARM_DESC(smp,
  2199. "Set this to enable APM use on an SMP platform. Use with caution on older systems");
  2200. MODULE_ALIAS_MISCDEV(APM_MINOR_DEV);