apm_32.c 69 KB

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