apm_32.c 69 KB

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