apm_32.c 69 KB

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