apm_32.c 69 KB

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