apm_32.c 70 KB

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