ibm_acpi.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775
  1. /*
  2. * ibm_acpi.c - IBM ThinkPad ACPI Extras
  3. *
  4. *
  5. * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
  6. * Copyright (C) 2006 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #define IBM_VERSION "0.13"
  23. /*
  24. * Changelog:
  25. *
  26. * 2006-11-22 0.13 new maintainer
  27. * changelog now lives in git commit history, and will
  28. * not be updated further in-file.
  29. *
  30. * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
  31. * 2005-03-17 0.11 support for 600e, 770x
  32. * thanks to Jamie Lentin <lentinj@dial.pipex.com>
  33. * support for 770e, G41
  34. * G40 and G41 don't have a thinklight
  35. * temperatures no longer experimental
  36. * experimental brightness control
  37. * experimental volume control
  38. * experimental fan enable/disable
  39. * 2005-01-16 0.10 fix module loading on R30, R31
  40. * 2005-01-16 0.9 support for 570, R30, R31
  41. * ultrabay support on A22p, A3x
  42. * limit arg for cmos, led, beep, drop experimental status
  43. * more capable led control on A21e, A22p, T20-22, X20
  44. * experimental temperatures and fan speed
  45. * experimental embedded controller register dump
  46. * mark more functions as __init, drop incorrect __exit
  47. * use MODULE_VERSION
  48. * thanks to Henrik Brix Andersen <brix@gentoo.org>
  49. * fix parameter passing on module loading
  50. * thanks to Rusty Russell <rusty@rustcorp.com.au>
  51. * thanks to Jim Radford <radford@blackbean.org>
  52. * 2004-11-08 0.8 fix init error case, don't return from a macro
  53. * thanks to Chris Wright <chrisw@osdl.org>
  54. * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
  55. * fix led control on A21e
  56. * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
  57. * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
  58. * proc file format changed
  59. * video_switch command
  60. * experimental cmos control
  61. * experimental led control
  62. * experimental acpi sounds
  63. * 2004-09-16 0.4 support for module parameters
  64. * hotkey mask can be prefixed by 0x
  65. * video output switching
  66. * video expansion control
  67. * ultrabay eject support
  68. * removed lcd brightness/on/off control, didn't work
  69. * 2004-08-17 0.3 support for R40
  70. * lcd off, brightness control
  71. * thinklight on/off
  72. * 2004-08-14 0.2 support for T series, X20
  73. * bluetooth enable/disable
  74. * hotkey events disabled by default
  75. * removed fan control, currently useless
  76. * 2004-08-09 0.1 initial release, support for X series
  77. */
  78. #include <linux/kernel.h>
  79. #include <linux/module.h>
  80. #include <linux/init.h>
  81. #include <linux/types.h>
  82. #include <linux/string.h>
  83. #include <linux/proc_fs.h>
  84. #include <linux/backlight.h>
  85. #include <asm/uaccess.h>
  86. #include <linux/dmi.h>
  87. #include <linux/jiffies.h>
  88. #include <linux/workqueue.h>
  89. #include <acpi/acpi_drivers.h>
  90. #include <acpi/acnamesp.h>
  91. #define IBM_NAME "ibm"
  92. #define IBM_DESC "IBM ThinkPad ACPI Extras"
  93. #define IBM_FILE "ibm_acpi"
  94. #define IBM_URL "http://ibm-acpi.sf.net/"
  95. MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
  96. MODULE_DESCRIPTION(IBM_DESC);
  97. MODULE_VERSION(IBM_VERSION);
  98. MODULE_LICENSE("GPL");
  99. #define IBM_DIR IBM_NAME
  100. #define IBM_LOG IBM_FILE ": "
  101. #define IBM_ERR KERN_ERR IBM_LOG
  102. #define IBM_NOTICE KERN_NOTICE IBM_LOG
  103. #define IBM_INFO KERN_INFO IBM_LOG
  104. #define IBM_DEBUG KERN_DEBUG IBM_LOG
  105. #define IBM_MAX_ACPI_ARGS 3
  106. #define __unused __attribute__ ((unused))
  107. static int experimental;
  108. module_param(experimental, int, 0);
  109. static acpi_handle root_handle = NULL;
  110. #define IBM_HANDLE(object, parent, paths...) \
  111. static acpi_handle object##_handle; \
  112. static acpi_handle *object##_parent = &parent##_handle; \
  113. static char *object##_path; \
  114. static char *object##_paths[] = { paths }
  115. IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
  116. "\\_SB.PCI.ISA.EC", /* 570 */
  117. "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
  118. "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
  119. "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
  120. "\\_SB.PCI0.ICH3.EC0", /* R31 */
  121. "\\_SB.PCI0.LPC.EC", /* all others */
  122. );
  123. IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
  124. "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
  125. "\\_SB.PCI0.VID0", /* 770e */
  126. "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
  127. "\\_SB.PCI0.AGP.VID", /* all others */
  128. ); /* R30, R31 */
  129. IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
  130. IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
  131. "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
  132. "\\CMS", /* R40, R40e */
  133. ); /* all others */
  134. #ifdef CONFIG_ACPI_IBM_DOCK
  135. IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
  136. "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
  137. "\\_SB.PCI0.PCI1.DOCK", /* all others */
  138. "\\_SB.PCI.ISA.SLCE", /* 570 */
  139. ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
  140. #endif
  141. #ifdef CONFIG_ACPI_IBM_BAY
  142. IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
  143. "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
  144. "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
  145. "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
  146. ); /* A21e, R30, R31 */
  147. IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
  148. "_EJ0", /* all others */
  149. ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
  150. IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
  151. "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
  152. ); /* all others */
  153. IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
  154. "_EJ0", /* 770x */
  155. ); /* all others */
  156. #endif
  157. /* don't list other alternatives as we install a notify handler on the 570 */
  158. IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
  159. IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
  160. "^HKEY", /* R30, R31 */
  161. "HKEY", /* all others */
  162. ); /* 570 */
  163. IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
  164. IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
  165. IBM_HANDLE(led, ec, "SLED", /* 570 */
  166. "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  167. "LED", /* all others */
  168. ); /* R30, R31 */
  169. IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  170. IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
  171. IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
  172. IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  173. IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
  174. "\\FSPD", /* 600e/x, 770e, 770x */
  175. ); /* all others */
  176. IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
  177. "JFNS", /* 770x-JL */
  178. ); /* all others */
  179. #define IBM_HKEY_HID "IBM0068"
  180. #define IBM_PCI_HID "PNP0A03"
  181. enum thermal_access_mode {
  182. IBMACPI_THERMAL_NONE = 0, /* No thermal support */
  183. IBMACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
  184. IBMACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
  185. IBMACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
  186. IBMACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
  187. };
  188. #define IBMACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
  189. struct ibm_thermal_sensors_struct {
  190. s32 temp[IBMACPI_MAX_THERMAL_SENSORS];
  191. };
  192. /*
  193. * FAN ACCESS MODES
  194. *
  195. * IBMACPI_FAN_RD_ACPI_GFAN:
  196. * ACPI GFAN method: returns fan level
  197. *
  198. * see IBMACPI_FAN_WR_ACPI_SFAN
  199. * EC 0x2f not available if GFAN exists
  200. *
  201. * IBMACPI_FAN_WR_ACPI_SFAN:
  202. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  203. *
  204. * EC 0x2f might be available *for reading*, but never for writing.
  205. *
  206. * IBMACPI_FAN_WR_TPEC:
  207. * ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
  208. * on almost all ThinkPads
  209. *
  210. * Fan speed changes of any sort (including those caused by the
  211. * disengaged mode) are usually done slowly by the firmware as the
  212. * maximum ammount of fan duty cycle change per second seems to be
  213. * limited.
  214. *
  215. * Reading is not available if GFAN exists.
  216. * Writing is not available if SFAN exists.
  217. *
  218. * Bits
  219. * 7 automatic mode engaged;
  220. * (default operation mode of the ThinkPad)
  221. * fan level is ignored in this mode.
  222. * 6 disengage mode (takes precedence over bit 7);
  223. * not available on all thinkpads. May disable
  224. * the tachometer, and speeds up fan to 100% duty-cycle,
  225. * which speeds it up far above the standard RPM
  226. * levels. It is not impossible that it could cause
  227. * hardware damage.
  228. * 5-3 unused in some models. Extra bits for fan level
  229. * in others, but still useless as all values above
  230. * 7 map to the same speed as level 7 in these models.
  231. * 2-0 fan level (0..7 usually)
  232. * 0x00 = stop
  233. * 0x07 = max (set when temperatures critical)
  234. * Some ThinkPads may have other levels, see
  235. * IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  236. *
  237. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  238. * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
  239. * does so, its initial value is meaningless (0x07).
  240. *
  241. * For firmware bugs, refer to:
  242. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  243. *
  244. * ----
  245. *
  246. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  247. * Main fan tachometer reading (in RPM)
  248. *
  249. * This register is present on all ThinkPads with a new-style EC, and
  250. * it is known not to be present on the A21m/e, and T22, as there is
  251. * something else in offset 0x84 according to the ACPI DSDT. Other
  252. * ThinkPads from this same time period (and earlier) probably lack the
  253. * tachometer as well.
  254. *
  255. * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
  256. * was never fixed by IBM to report the EC firmware version string
  257. * probably support the tachometer (like the early X models), so
  258. * detecting it is quite hard. We need more data to know for sure.
  259. *
  260. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  261. * might result.
  262. *
  263. * FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
  264. * register is not invalidated in ThinkPads that disable tachometer
  265. * readings. Thus, the tachometer readings go stale.
  266. *
  267. * For firmware bugs, refer to:
  268. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  269. *
  270. * IBMACPI_FAN_WR_ACPI_FANS:
  271. * ThinkPad X31, X40, X41. Not available in the X60.
  272. *
  273. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  274. * high speed. ACPI DSDT seems to map these three speeds to levels
  275. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  276. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  277. *
  278. * The speeds are stored on handles
  279. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  280. *
  281. * There are three default speed sets, acessible as handles:
  282. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  283. *
  284. * ACPI DSDT switches which set is in use depending on various
  285. * factors.
  286. *
  287. * IBMACPI_FAN_WR_TPEC is also available and should be used to
  288. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  289. * but the ACPI tables just mention level 7.
  290. */
  291. enum fan_status_access_mode {
  292. IBMACPI_FAN_NONE = 0, /* No fan status or control */
  293. IBMACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
  294. IBMACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
  295. };
  296. enum fan_control_access_mode {
  297. IBMACPI_FAN_WR_NONE = 0, /* No fan control */
  298. IBMACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
  299. IBMACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
  300. IBMACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
  301. };
  302. enum fan_control_commands {
  303. IBMACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
  304. IBMACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
  305. IBMACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
  306. * and also watchdog cmd */
  307. };
  308. enum { /* Fan control constants */
  309. fan_status_offset = 0x2f, /* EC register 0x2f */
  310. fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
  311. * 0x84 must be read before 0x85 */
  312. IBMACPI_FAN_EC_DISENGAGED = 0x40, /* EC mode: tachometer
  313. * disengaged */
  314. IBMACPI_FAN_EC_AUTO = 0x80, /* EC mode: auto fan
  315. * control */
  316. };
  317. static char *ibm_thinkpad_ec_found = NULL;
  318. struct ibm_struct {
  319. char *name;
  320. char param[32];
  321. char *hid;
  322. struct acpi_driver *driver;
  323. int (*init) (void);
  324. int (*read) (char *);
  325. int (*write) (char *);
  326. void (*exit) (void);
  327. void (*notify) (struct ibm_struct *, u32);
  328. acpi_handle *handle;
  329. int type;
  330. struct acpi_device *device;
  331. int driver_registered;
  332. int proc_created;
  333. int init_called;
  334. int notify_installed;
  335. int experimental;
  336. };
  337. static struct proc_dir_entry *proc_dir = NULL;
  338. static struct backlight_device *ibm_backlight_device = NULL;
  339. #define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
  340. #define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
  341. #define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
  342. static int acpi_evalf(acpi_handle handle,
  343. void *res, char *method, char *fmt, ...)
  344. {
  345. char *fmt0 = fmt;
  346. struct acpi_object_list params;
  347. union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
  348. struct acpi_buffer result, *resultp;
  349. union acpi_object out_obj;
  350. acpi_status status;
  351. va_list ap;
  352. char res_type;
  353. int success;
  354. int quiet;
  355. if (!*fmt) {
  356. printk(IBM_ERR "acpi_evalf() called with empty format\n");
  357. return 0;
  358. }
  359. if (*fmt == 'q') {
  360. quiet = 1;
  361. fmt++;
  362. } else
  363. quiet = 0;
  364. res_type = *(fmt++);
  365. params.count = 0;
  366. params.pointer = &in_objs[0];
  367. va_start(ap, fmt);
  368. while (*fmt) {
  369. char c = *(fmt++);
  370. switch (c) {
  371. case 'd': /* int */
  372. in_objs[params.count].integer.value = va_arg(ap, int);
  373. in_objs[params.count++].type = ACPI_TYPE_INTEGER;
  374. break;
  375. /* add more types as needed */
  376. default:
  377. printk(IBM_ERR "acpi_evalf() called "
  378. "with invalid format character '%c'\n", c);
  379. return 0;
  380. }
  381. }
  382. va_end(ap);
  383. if (res_type != 'v') {
  384. result.length = sizeof(out_obj);
  385. result.pointer = &out_obj;
  386. resultp = &result;
  387. } else
  388. resultp = NULL;
  389. status = acpi_evaluate_object(handle, method, &params, resultp);
  390. switch (res_type) {
  391. case 'd': /* int */
  392. if (res)
  393. *(int *)res = out_obj.integer.value;
  394. success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
  395. break;
  396. case 'v': /* void */
  397. success = status == AE_OK;
  398. break;
  399. /* add more types as needed */
  400. default:
  401. printk(IBM_ERR "acpi_evalf() called "
  402. "with invalid format character '%c'\n", res_type);
  403. return 0;
  404. }
  405. if (!success && !quiet)
  406. printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
  407. method, fmt0, status);
  408. return success;
  409. }
  410. static void __unused acpi_print_int(acpi_handle handle, char *method)
  411. {
  412. int i;
  413. if (acpi_evalf(handle, &i, method, "d"))
  414. printk(IBM_INFO "%s = 0x%x\n", method, i);
  415. else
  416. printk(IBM_ERR "error calling %s\n", method);
  417. }
  418. static char *next_cmd(char **cmds)
  419. {
  420. char *start = *cmds;
  421. char *end;
  422. while ((end = strchr(start, ',')) && end == start)
  423. start = end + 1;
  424. if (!end)
  425. return NULL;
  426. *end = 0;
  427. *cmds = end + 1;
  428. return start;
  429. }
  430. static int driver_init(void)
  431. {
  432. printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
  433. printk(IBM_INFO "%s\n", IBM_URL);
  434. return 0;
  435. }
  436. static int driver_read(char *p)
  437. {
  438. int len = 0;
  439. len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
  440. len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
  441. return len;
  442. }
  443. static int hotkey_supported;
  444. static int hotkey_mask_supported;
  445. static int hotkey_orig_status;
  446. static int hotkey_orig_mask;
  447. static int hotkey_get(int *status, int *mask)
  448. {
  449. if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
  450. return 0;
  451. if (hotkey_mask_supported)
  452. if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
  453. return 0;
  454. return 1;
  455. }
  456. static int hotkey_set(int status, int mask)
  457. {
  458. int i;
  459. if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
  460. return 0;
  461. if (hotkey_mask_supported)
  462. for (i = 0; i < 32; i++) {
  463. int bit = ((1 << i) & mask) != 0;
  464. if (!acpi_evalf(hkey_handle,
  465. NULL, "MHKM", "vdd", i + 1, bit))
  466. return 0;
  467. }
  468. return 1;
  469. }
  470. static int hotkey_init(void)
  471. {
  472. /* hotkey not supported on 570 */
  473. hotkey_supported = hkey_handle != NULL;
  474. if (hotkey_supported) {
  475. /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  476. A30, R30, R31, T20-22, X20-21, X22-24 */
  477. hotkey_mask_supported =
  478. acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
  479. if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
  480. return -ENODEV;
  481. }
  482. return 0;
  483. }
  484. static int hotkey_read(char *p)
  485. {
  486. int status, mask;
  487. int len = 0;
  488. if (!hotkey_supported) {
  489. len += sprintf(p + len, "status:\t\tnot supported\n");
  490. return len;
  491. }
  492. if (!hotkey_get(&status, &mask))
  493. return -EIO;
  494. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
  495. if (hotkey_mask_supported) {
  496. len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
  497. len += sprintf(p + len,
  498. "commands:\tenable, disable, reset, <mask>\n");
  499. } else {
  500. len += sprintf(p + len, "mask:\t\tnot supported\n");
  501. len += sprintf(p + len, "commands:\tenable, disable, reset\n");
  502. }
  503. return len;
  504. }
  505. static int hotkey_write(char *buf)
  506. {
  507. int status, mask;
  508. char *cmd;
  509. int do_cmd = 0;
  510. if (!hotkey_supported)
  511. return -ENODEV;
  512. if (!hotkey_get(&status, &mask))
  513. return -EIO;
  514. while ((cmd = next_cmd(&buf))) {
  515. if (strlencmp(cmd, "enable") == 0) {
  516. status = 1;
  517. } else if (strlencmp(cmd, "disable") == 0) {
  518. status = 0;
  519. } else if (strlencmp(cmd, "reset") == 0) {
  520. status = hotkey_orig_status;
  521. mask = hotkey_orig_mask;
  522. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  523. /* mask set */
  524. } else if (sscanf(cmd, "%x", &mask) == 1) {
  525. /* mask set */
  526. } else
  527. return -EINVAL;
  528. do_cmd = 1;
  529. }
  530. if (do_cmd && !hotkey_set(status, mask))
  531. return -EIO;
  532. return 0;
  533. }
  534. static void hotkey_exit(void)
  535. {
  536. if (hotkey_supported)
  537. hotkey_set(hotkey_orig_status, hotkey_orig_mask);
  538. }
  539. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  540. {
  541. int hkey;
  542. if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
  543. acpi_bus_generate_event(ibm->device, event, hkey);
  544. else {
  545. printk(IBM_ERR "unknown hotkey event %d\n", event);
  546. acpi_bus_generate_event(ibm->device, event, 0);
  547. }
  548. }
  549. static int bluetooth_supported;
  550. static int bluetooth_init(void)
  551. {
  552. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  553. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  554. bluetooth_supported = hkey_handle &&
  555. acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
  556. return 0;
  557. }
  558. static int bluetooth_status(void)
  559. {
  560. int status;
  561. if (!bluetooth_supported ||
  562. !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  563. status = 0;
  564. return status;
  565. }
  566. static int bluetooth_read(char *p)
  567. {
  568. int len = 0;
  569. int status = bluetooth_status();
  570. if (!bluetooth_supported)
  571. len += sprintf(p + len, "status:\t\tnot supported\n");
  572. else if (!(status & 1))
  573. len += sprintf(p + len, "status:\t\tnot installed\n");
  574. else {
  575. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
  576. len += sprintf(p + len, "commands:\tenable, disable\n");
  577. }
  578. return len;
  579. }
  580. static int bluetooth_write(char *buf)
  581. {
  582. int status = bluetooth_status();
  583. char *cmd;
  584. int do_cmd = 0;
  585. if (!bluetooth_supported)
  586. return -ENODEV;
  587. while ((cmd = next_cmd(&buf))) {
  588. if (strlencmp(cmd, "enable") == 0) {
  589. status |= 2;
  590. } else if (strlencmp(cmd, "disable") == 0) {
  591. status &= ~2;
  592. } else
  593. return -EINVAL;
  594. do_cmd = 1;
  595. }
  596. if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  597. return -EIO;
  598. return 0;
  599. }
  600. static int wan_supported;
  601. static int wan_init(void)
  602. {
  603. wan_supported = hkey_handle &&
  604. acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
  605. return 0;
  606. }
  607. static int wan_status(void)
  608. {
  609. int status;
  610. if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  611. status = 0;
  612. return status;
  613. }
  614. static int wan_read(char *p)
  615. {
  616. int len = 0;
  617. int status = wan_status();
  618. if (!wan_supported)
  619. len += sprintf(p + len, "status:\t\tnot supported\n");
  620. else if (!(status & 1))
  621. len += sprintf(p + len, "status:\t\tnot installed\n");
  622. else {
  623. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
  624. len += sprintf(p + len, "commands:\tenable, disable\n");
  625. }
  626. return len;
  627. }
  628. static int wan_write(char *buf)
  629. {
  630. int status = wan_status();
  631. char *cmd;
  632. int do_cmd = 0;
  633. if (!wan_supported)
  634. return -ENODEV;
  635. while ((cmd = next_cmd(&buf))) {
  636. if (strlencmp(cmd, "enable") == 0) {
  637. status |= 2;
  638. } else if (strlencmp(cmd, "disable") == 0) {
  639. status &= ~2;
  640. } else
  641. return -EINVAL;
  642. do_cmd = 1;
  643. }
  644. if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  645. return -EIO;
  646. return 0;
  647. }
  648. enum video_access_mode {
  649. IBMACPI_VIDEO_NONE = 0,
  650. IBMACPI_VIDEO_570, /* 570 */
  651. IBMACPI_VIDEO_770, /* 600e/x, 770e, 770x */
  652. IBMACPI_VIDEO_NEW, /* all others */
  653. };
  654. static enum video_access_mode video_supported;
  655. static int video_orig_autosw;
  656. static int video_init(void)
  657. {
  658. int ivga;
  659. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  660. /* G41, assume IVGA doesn't change */
  661. vid_handle = vid2_handle;
  662. if (!vid_handle)
  663. /* video switching not supported on R30, R31 */
  664. video_supported = IBMACPI_VIDEO_NONE;
  665. else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  666. /* 570 */
  667. video_supported = IBMACPI_VIDEO_570;
  668. else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  669. /* 600e/x, 770e, 770x */
  670. video_supported = IBMACPI_VIDEO_770;
  671. else
  672. /* all others */
  673. video_supported = IBMACPI_VIDEO_NEW;
  674. return 0;
  675. }
  676. static int video_status(void)
  677. {
  678. int status = 0;
  679. int i;
  680. if (video_supported == IBMACPI_VIDEO_570) {
  681. if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
  682. status = i & 3;
  683. } else if (video_supported == IBMACPI_VIDEO_770) {
  684. if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
  685. status |= 0x01 * i;
  686. if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
  687. status |= 0x02 * i;
  688. } else if (video_supported == IBMACPI_VIDEO_NEW) {
  689. acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
  690. if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
  691. status |= 0x02 * i;
  692. acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
  693. if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
  694. status |= 0x01 * i;
  695. if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
  696. status |= 0x08 * i;
  697. }
  698. return status;
  699. }
  700. static int video_autosw(void)
  701. {
  702. int autosw = 0;
  703. if (video_supported == IBMACPI_VIDEO_570)
  704. acpi_evalf(vid_handle, &autosw, "SWIT", "d");
  705. else if (video_supported == IBMACPI_VIDEO_770 ||
  706. video_supported == IBMACPI_VIDEO_NEW)
  707. acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
  708. return autosw & 1;
  709. }
  710. static int video_read(char *p)
  711. {
  712. int status = video_status();
  713. int autosw = video_autosw();
  714. int len = 0;
  715. if (!video_supported) {
  716. len += sprintf(p + len, "status:\t\tnot supported\n");
  717. return len;
  718. }
  719. len += sprintf(p + len, "status:\t\tsupported\n");
  720. len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
  721. len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
  722. if (video_supported == IBMACPI_VIDEO_NEW)
  723. len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
  724. len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
  725. len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
  726. len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
  727. if (video_supported == IBMACPI_VIDEO_NEW)
  728. len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
  729. len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
  730. len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
  731. return len;
  732. }
  733. static int video_switch(void)
  734. {
  735. int autosw = video_autosw();
  736. int ret;
  737. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
  738. return -EIO;
  739. ret = video_supported == IBMACPI_VIDEO_570 ?
  740. acpi_evalf(ec_handle, NULL, "_Q16", "v") :
  741. acpi_evalf(vid_handle, NULL, "VSWT", "v");
  742. acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
  743. return ret;
  744. }
  745. static int video_expand(void)
  746. {
  747. if (video_supported == IBMACPI_VIDEO_570)
  748. return acpi_evalf(ec_handle, NULL, "_Q17", "v");
  749. else if (video_supported == IBMACPI_VIDEO_770)
  750. return acpi_evalf(vid_handle, NULL, "VEXP", "v");
  751. else
  752. return acpi_evalf(NULL, NULL, "\\VEXP", "v");
  753. }
  754. static int video_switch2(int status)
  755. {
  756. int ret;
  757. if (video_supported == IBMACPI_VIDEO_570) {
  758. ret = acpi_evalf(NULL, NULL,
  759. "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
  760. } else if (video_supported == IBMACPI_VIDEO_770) {
  761. int autosw = video_autosw();
  762. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
  763. return -EIO;
  764. ret = acpi_evalf(vid_handle, NULL,
  765. "ASWT", "vdd", status * 0x100, 0);
  766. acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
  767. } else {
  768. ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  769. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  770. }
  771. return ret;
  772. }
  773. static int video_write(char *buf)
  774. {
  775. char *cmd;
  776. int enable, disable, status;
  777. if (!video_supported)
  778. return -ENODEV;
  779. enable = disable = 0;
  780. while ((cmd = next_cmd(&buf))) {
  781. if (strlencmp(cmd, "lcd_enable") == 0) {
  782. enable |= 0x01;
  783. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  784. disable |= 0x01;
  785. } else if (strlencmp(cmd, "crt_enable") == 0) {
  786. enable |= 0x02;
  787. } else if (strlencmp(cmd, "crt_disable") == 0) {
  788. disable |= 0x02;
  789. } else if (video_supported == IBMACPI_VIDEO_NEW &&
  790. strlencmp(cmd, "dvi_enable") == 0) {
  791. enable |= 0x08;
  792. } else if (video_supported == IBMACPI_VIDEO_NEW &&
  793. strlencmp(cmd, "dvi_disable") == 0) {
  794. disable |= 0x08;
  795. } else if (strlencmp(cmd, "auto_enable") == 0) {
  796. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
  797. return -EIO;
  798. } else if (strlencmp(cmd, "auto_disable") == 0) {
  799. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
  800. return -EIO;
  801. } else if (strlencmp(cmd, "video_switch") == 0) {
  802. if (!video_switch())
  803. return -EIO;
  804. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  805. if (!video_expand())
  806. return -EIO;
  807. } else
  808. return -EINVAL;
  809. }
  810. if (enable || disable) {
  811. status = (video_status() & 0x0f & ~disable) | enable;
  812. if (!video_switch2(status))
  813. return -EIO;
  814. }
  815. return 0;
  816. }
  817. static void video_exit(void)
  818. {
  819. acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
  820. }
  821. static int light_supported;
  822. static int light_status_supported;
  823. static int light_init(void)
  824. {
  825. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  826. light_supported = (cmos_handle || lght_handle) && !ledb_handle;
  827. if (light_supported)
  828. /* light status not supported on
  829. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  830. light_status_supported = acpi_evalf(ec_handle, NULL,
  831. "KBLT", "qv");
  832. return 0;
  833. }
  834. static int light_read(char *p)
  835. {
  836. int len = 0;
  837. int status = 0;
  838. if (!light_supported) {
  839. len += sprintf(p + len, "status:\t\tnot supported\n");
  840. } else if (!light_status_supported) {
  841. len += sprintf(p + len, "status:\t\tunknown\n");
  842. len += sprintf(p + len, "commands:\ton, off\n");
  843. } else {
  844. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  845. return -EIO;
  846. len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
  847. len += sprintf(p + len, "commands:\ton, off\n");
  848. }
  849. return len;
  850. }
  851. static int light_write(char *buf)
  852. {
  853. int cmos_cmd, lght_cmd;
  854. char *cmd;
  855. int success;
  856. if (!light_supported)
  857. return -ENODEV;
  858. while ((cmd = next_cmd(&buf))) {
  859. if (strlencmp(cmd, "on") == 0) {
  860. cmos_cmd = 0x0c;
  861. lght_cmd = 1;
  862. } else if (strlencmp(cmd, "off") == 0) {
  863. cmos_cmd = 0x0d;
  864. lght_cmd = 0;
  865. } else
  866. return -EINVAL;
  867. success = cmos_handle ?
  868. acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
  869. acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
  870. if (!success)
  871. return -EIO;
  872. }
  873. return 0;
  874. }
  875. #if defined(CONFIG_ACPI_IBM_DOCK) || defined(CONFIG_ACPI_IBM_BAY)
  876. static int _sta(acpi_handle handle)
  877. {
  878. int status;
  879. if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
  880. status = 0;
  881. return status;
  882. }
  883. #endif
  884. #ifdef CONFIG_ACPI_IBM_DOCK
  885. #define dock_docked() (_sta(dock_handle) & 1)
  886. static int dock_read(char *p)
  887. {
  888. int len = 0;
  889. int docked = dock_docked();
  890. if (!dock_handle)
  891. len += sprintf(p + len, "status:\t\tnot supported\n");
  892. else if (!docked)
  893. len += sprintf(p + len, "status:\t\tundocked\n");
  894. else {
  895. len += sprintf(p + len, "status:\t\tdocked\n");
  896. len += sprintf(p + len, "commands:\tdock, undock\n");
  897. }
  898. return len;
  899. }
  900. static int dock_write(char *buf)
  901. {
  902. char *cmd;
  903. if (!dock_docked())
  904. return -ENODEV;
  905. while ((cmd = next_cmd(&buf))) {
  906. if (strlencmp(cmd, "undock") == 0) {
  907. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
  908. !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
  909. return -EIO;
  910. } else if (strlencmp(cmd, "dock") == 0) {
  911. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
  912. return -EIO;
  913. } else
  914. return -EINVAL;
  915. }
  916. return 0;
  917. }
  918. static void dock_notify(struct ibm_struct *ibm, u32 event)
  919. {
  920. int docked = dock_docked();
  921. int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
  922. if (event == 1 && !pci) /* 570 */
  923. acpi_bus_generate_event(ibm->device, event, 1); /* button */
  924. else if (event == 1 && pci) /* 570 */
  925. acpi_bus_generate_event(ibm->device, event, 3); /* dock */
  926. else if (event == 3 && docked)
  927. acpi_bus_generate_event(ibm->device, event, 1); /* button */
  928. else if (event == 3 && !docked)
  929. acpi_bus_generate_event(ibm->device, event, 2); /* undock */
  930. else if (event == 0 && docked)
  931. acpi_bus_generate_event(ibm->device, event, 3); /* dock */
  932. else {
  933. printk(IBM_ERR "unknown dock event %d, status %d\n",
  934. event, _sta(dock_handle));
  935. acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
  936. }
  937. }
  938. #endif
  939. #ifdef CONFIG_ACPI_IBM_BAY
  940. static int bay_status_supported;
  941. static int bay_status2_supported;
  942. static int bay_eject_supported;
  943. static int bay_eject2_supported;
  944. static int bay_init(void)
  945. {
  946. bay_status_supported = bay_handle &&
  947. acpi_evalf(bay_handle, NULL, "_STA", "qv");
  948. bay_status2_supported = bay2_handle &&
  949. acpi_evalf(bay2_handle, NULL, "_STA", "qv");
  950. bay_eject_supported = bay_handle && bay_ej_handle &&
  951. (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
  952. bay_eject2_supported = bay2_handle && bay2_ej_handle &&
  953. (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
  954. return 0;
  955. }
  956. #define bay_occupied(b) (_sta(b##_handle) & 1)
  957. static int bay_read(char *p)
  958. {
  959. int len = 0;
  960. int occupied = bay_occupied(bay);
  961. int occupied2 = bay_occupied(bay2);
  962. int eject, eject2;
  963. len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
  964. (occupied ? "occupied" : "unoccupied") :
  965. "not supported");
  966. if (bay_status2_supported)
  967. len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
  968. "occupied" : "unoccupied");
  969. eject = bay_eject_supported && occupied;
  970. eject2 = bay_eject2_supported && occupied2;
  971. if (eject && eject2)
  972. len += sprintf(p + len, "commands:\teject, eject2\n");
  973. else if (eject)
  974. len += sprintf(p + len, "commands:\teject\n");
  975. else if (eject2)
  976. len += sprintf(p + len, "commands:\teject2\n");
  977. return len;
  978. }
  979. static int bay_write(char *buf)
  980. {
  981. char *cmd;
  982. if (!bay_eject_supported && !bay_eject2_supported)
  983. return -ENODEV;
  984. while ((cmd = next_cmd(&buf))) {
  985. if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
  986. if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
  987. return -EIO;
  988. } else if (bay_eject2_supported &&
  989. strlencmp(cmd, "eject2") == 0) {
  990. if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
  991. return -EIO;
  992. } else
  993. return -EINVAL;
  994. }
  995. return 0;
  996. }
  997. static void bay_notify(struct ibm_struct *ibm, u32 event)
  998. {
  999. acpi_bus_generate_event(ibm->device, event, 0);
  1000. }
  1001. #endif
  1002. static int cmos_read(char *p)
  1003. {
  1004. int len = 0;
  1005. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  1006. R30, R31, T20-22, X20-21 */
  1007. if (!cmos_handle)
  1008. len += sprintf(p + len, "status:\t\tnot supported\n");
  1009. else {
  1010. len += sprintf(p + len, "status:\t\tsupported\n");
  1011. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
  1012. }
  1013. return len;
  1014. }
  1015. static int cmos_eval(int cmos_cmd)
  1016. {
  1017. if (cmos_handle)
  1018. return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
  1019. else
  1020. return 1;
  1021. }
  1022. static int cmos_write(char *buf)
  1023. {
  1024. char *cmd;
  1025. int cmos_cmd;
  1026. if (!cmos_handle)
  1027. return -EINVAL;
  1028. while ((cmd = next_cmd(&buf))) {
  1029. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  1030. cmos_cmd >= 0 && cmos_cmd <= 21) {
  1031. /* cmos_cmd set */
  1032. } else
  1033. return -EINVAL;
  1034. if (!cmos_eval(cmos_cmd))
  1035. return -EIO;
  1036. }
  1037. return 0;
  1038. }
  1039. enum led_access_mode {
  1040. IBMACPI_LED_NONE = 0,
  1041. IBMACPI_LED_570, /* 570 */
  1042. IBMACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1043. IBMACPI_LED_NEW, /* all others */
  1044. };
  1045. static enum led_access_mode led_supported;
  1046. static int led_init(void)
  1047. {
  1048. if (!led_handle)
  1049. /* led not supported on R30, R31 */
  1050. led_supported = IBMACPI_LED_NONE;
  1051. else if (strlencmp(led_path, "SLED") == 0)
  1052. /* 570 */
  1053. led_supported = IBMACPI_LED_570;
  1054. else if (strlencmp(led_path, "SYSL") == 0)
  1055. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1056. led_supported = IBMACPI_LED_OLD;
  1057. else
  1058. /* all others */
  1059. led_supported = IBMACPI_LED_NEW;
  1060. return 0;
  1061. }
  1062. #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
  1063. static int led_read(char *p)
  1064. {
  1065. int len = 0;
  1066. if (!led_supported) {
  1067. len += sprintf(p + len, "status:\t\tnot supported\n");
  1068. return len;
  1069. }
  1070. len += sprintf(p + len, "status:\t\tsupported\n");
  1071. if (led_supported == IBMACPI_LED_570) {
  1072. /* 570 */
  1073. int i, status;
  1074. for (i = 0; i < 8; i++) {
  1075. if (!acpi_evalf(ec_handle,
  1076. &status, "GLED", "dd", 1 << i))
  1077. return -EIO;
  1078. len += sprintf(p + len, "%d:\t\t%s\n",
  1079. i, led_status(status));
  1080. }
  1081. }
  1082. len += sprintf(p + len, "commands:\t"
  1083. "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
  1084. return len;
  1085. }
  1086. /* off, on, blink */
  1087. static const int led_sled_arg1[] = { 0, 1, 3 };
  1088. static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
  1089. static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
  1090. static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
  1091. #define EC_HLCL 0x0c
  1092. #define EC_HLBL 0x0d
  1093. #define EC_HLMS 0x0e
  1094. static int led_write(char *buf)
  1095. {
  1096. char *cmd;
  1097. int led, ind, ret;
  1098. if (!led_supported)
  1099. return -ENODEV;
  1100. while ((cmd = next_cmd(&buf))) {
  1101. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
  1102. return -EINVAL;
  1103. if (strstr(cmd, "off")) {
  1104. ind = 0;
  1105. } else if (strstr(cmd, "on")) {
  1106. ind = 1;
  1107. } else if (strstr(cmd, "blink")) {
  1108. ind = 2;
  1109. } else
  1110. return -EINVAL;
  1111. if (led_supported == IBMACPI_LED_570) {
  1112. /* 570 */
  1113. led = 1 << led;
  1114. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  1115. led, led_sled_arg1[ind]))
  1116. return -EIO;
  1117. } else if (led_supported == IBMACPI_LED_OLD) {
  1118. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  1119. led = 1 << led;
  1120. ret = ec_write(EC_HLMS, led);
  1121. if (ret >= 0)
  1122. ret =
  1123. ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
  1124. if (ret >= 0)
  1125. ret =
  1126. ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
  1127. if (ret < 0)
  1128. return ret;
  1129. } else {
  1130. /* all others */
  1131. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  1132. led, led_led_arg1[ind]))
  1133. return -EIO;
  1134. }
  1135. }
  1136. return 0;
  1137. }
  1138. static int beep_read(char *p)
  1139. {
  1140. int len = 0;
  1141. if (!beep_handle)
  1142. len += sprintf(p + len, "status:\t\tnot supported\n");
  1143. else {
  1144. len += sprintf(p + len, "status:\t\tsupported\n");
  1145. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  1146. }
  1147. return len;
  1148. }
  1149. static int beep_write(char *buf)
  1150. {
  1151. char *cmd;
  1152. int beep_cmd;
  1153. if (!beep_handle)
  1154. return -ENODEV;
  1155. while ((cmd = next_cmd(&buf))) {
  1156. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  1157. beep_cmd >= 0 && beep_cmd <= 17) {
  1158. /* beep_cmd set */
  1159. } else
  1160. return -EINVAL;
  1161. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
  1162. return -EIO;
  1163. }
  1164. return 0;
  1165. }
  1166. static int acpi_ec_read(int i, u8 * p)
  1167. {
  1168. int v;
  1169. if (ecrd_handle) {
  1170. if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
  1171. return 0;
  1172. *p = v;
  1173. } else {
  1174. if (ec_read(i, p) < 0)
  1175. return 0;
  1176. }
  1177. return 1;
  1178. }
  1179. static int acpi_ec_write(int i, u8 v)
  1180. {
  1181. if (ecwr_handle) {
  1182. if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
  1183. return 0;
  1184. } else {
  1185. if (ec_write(i, v) < 0)
  1186. return 0;
  1187. }
  1188. return 1;
  1189. }
  1190. static enum thermal_access_mode thermal_read_mode;
  1191. static int thermal_init(void)
  1192. {
  1193. u8 t, ta1, ta2;
  1194. int i;
  1195. int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  1196. if (ibm_thinkpad_ec_found && experimental) {
  1197. /*
  1198. * Direct EC access mode: sensors at registers
  1199. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  1200. * non-implemented, thermal sensors return 0x80 when
  1201. * not available
  1202. */
  1203. ta1 = ta2 = 0;
  1204. for (i = 0; i < 8; i++) {
  1205. if (likely(acpi_ec_read(0x78 + i, &t))) {
  1206. ta1 |= t;
  1207. } else {
  1208. ta1 = 0;
  1209. break;
  1210. }
  1211. if (likely(acpi_ec_read(0xC0 + i, &t))) {
  1212. ta2 |= t;
  1213. } else {
  1214. ta1 = 0;
  1215. break;
  1216. }
  1217. }
  1218. if (ta1 == 0) {
  1219. /* This is sheer paranoia, but we handle it anyway */
  1220. if (acpi_tmp7) {
  1221. printk(IBM_ERR
  1222. "ThinkPad ACPI EC access misbehaving, "
  1223. "falling back to ACPI TMPx access mode\n");
  1224. thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
  1225. } else {
  1226. printk(IBM_ERR
  1227. "ThinkPad ACPI EC access misbehaving, "
  1228. "disabling thermal sensors access\n");
  1229. thermal_read_mode = IBMACPI_THERMAL_NONE;
  1230. }
  1231. } else {
  1232. thermal_read_mode =
  1233. (ta2 != 0) ?
  1234. IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
  1235. }
  1236. } else if (acpi_tmp7) {
  1237. if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  1238. /* 600e/x, 770e, 770x */
  1239. thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
  1240. } else {
  1241. /* Standard ACPI TMPx access, max 8 sensors */
  1242. thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
  1243. }
  1244. } else {
  1245. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  1246. thermal_read_mode = IBMACPI_THERMAL_NONE;
  1247. }
  1248. return 0;
  1249. }
  1250. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  1251. {
  1252. int i, t;
  1253. s8 tmp;
  1254. char tmpi[] = "TMPi";
  1255. if (!s)
  1256. return -EINVAL;
  1257. switch (thermal_read_mode) {
  1258. #if IBMACPI_MAX_THERMAL_SENSORS >= 16
  1259. case IBMACPI_THERMAL_TPEC_16:
  1260. for (i = 0; i < 8; i++) {
  1261. if (!acpi_ec_read(0xC0 + i, &tmp))
  1262. return -EIO;
  1263. s->temp[i + 8] = tmp * 1000;
  1264. }
  1265. /* fallthrough */
  1266. #endif
  1267. case IBMACPI_THERMAL_TPEC_8:
  1268. for (i = 0; i < 8; i++) {
  1269. if (!acpi_ec_read(0x78 + i, &tmp))
  1270. return -EIO;
  1271. s->temp[i] = tmp * 1000;
  1272. }
  1273. return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
  1274. case IBMACPI_THERMAL_ACPI_UPDT:
  1275. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  1276. return -EIO;
  1277. for (i = 0; i < 8; i++) {
  1278. tmpi[3] = '0' + i;
  1279. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1280. return -EIO;
  1281. s->temp[i] = (t - 2732) * 100;
  1282. }
  1283. return 8;
  1284. case IBMACPI_THERMAL_ACPI_TMP07:
  1285. for (i = 0; i < 8; i++) {
  1286. tmpi[3] = '0' + i;
  1287. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1288. return -EIO;
  1289. s->temp[i] = t * 1000;
  1290. }
  1291. return 8;
  1292. case IBMACPI_THERMAL_NONE:
  1293. default:
  1294. return 0;
  1295. }
  1296. }
  1297. static int thermal_read(char *p)
  1298. {
  1299. int len = 0;
  1300. int n, i;
  1301. struct ibm_thermal_sensors_struct t;
  1302. n = thermal_get_sensors(&t);
  1303. if (unlikely(n < 0))
  1304. return n;
  1305. len += sprintf(p + len, "temperatures:\t");
  1306. if (n > 0) {
  1307. for (i = 0; i < (n - 1); i++)
  1308. len += sprintf(p + len, "%d ", t.temp[i] / 1000);
  1309. len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
  1310. } else
  1311. len += sprintf(p + len, "not supported\n");
  1312. return len;
  1313. }
  1314. static u8 ecdump_regs[256];
  1315. static int ecdump_read(char *p)
  1316. {
  1317. int len = 0;
  1318. int i, j;
  1319. u8 v;
  1320. len += sprintf(p + len, "EC "
  1321. " +00 +01 +02 +03 +04 +05 +06 +07"
  1322. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  1323. for (i = 0; i < 256; i += 16) {
  1324. len += sprintf(p + len, "EC 0x%02x:", i);
  1325. for (j = 0; j < 16; j++) {
  1326. if (!acpi_ec_read(i + j, &v))
  1327. break;
  1328. if (v != ecdump_regs[i + j])
  1329. len += sprintf(p + len, " *%02x", v);
  1330. else
  1331. len += sprintf(p + len, " %02x", v);
  1332. ecdump_regs[i + j] = v;
  1333. }
  1334. len += sprintf(p + len, "\n");
  1335. if (j != 16)
  1336. break;
  1337. }
  1338. /* These are way too dangerous to advertise openly... */
  1339. #if 0
  1340. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  1341. " (<offset> is 00-ff, <value> is 00-ff)\n");
  1342. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  1343. " (<offset> is 00-ff, <value> is 0-255)\n");
  1344. #endif
  1345. return len;
  1346. }
  1347. static int ecdump_write(char *buf)
  1348. {
  1349. char *cmd;
  1350. int i, v;
  1351. while ((cmd = next_cmd(&buf))) {
  1352. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  1353. /* i and v set */
  1354. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  1355. /* i and v set */
  1356. } else
  1357. return -EINVAL;
  1358. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  1359. if (!acpi_ec_write(i, v))
  1360. return -EIO;
  1361. } else
  1362. return -EINVAL;
  1363. }
  1364. return 0;
  1365. }
  1366. static int brightness_offset = 0x31;
  1367. static int brightness_get(struct backlight_device *bd)
  1368. {
  1369. u8 level;
  1370. if (!acpi_ec_read(brightness_offset, &level))
  1371. return -EIO;
  1372. level &= 0x7;
  1373. return level;
  1374. }
  1375. static int brightness_read(char *p)
  1376. {
  1377. int len = 0;
  1378. int level;
  1379. if ((level = brightness_get(NULL)) < 0) {
  1380. len += sprintf(p + len, "level:\t\tunreadable\n");
  1381. } else {
  1382. len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
  1383. len += sprintf(p + len, "commands:\tup, down\n");
  1384. len += sprintf(p + len, "commands:\tlevel <level>"
  1385. " (<level> is 0-7)\n");
  1386. }
  1387. return len;
  1388. }
  1389. #define BRIGHTNESS_UP 4
  1390. #define BRIGHTNESS_DOWN 5
  1391. static int brightness_set(int value)
  1392. {
  1393. int cmos_cmd, inc, i;
  1394. int current_value = brightness_get(NULL);
  1395. value &= 7;
  1396. cmos_cmd = value > current_value ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
  1397. inc = value > current_value ? 1 : -1;
  1398. for (i = current_value; i != value; i += inc) {
  1399. if (!cmos_eval(cmos_cmd))
  1400. return -EIO;
  1401. if (!acpi_ec_write(brightness_offset, i + inc))
  1402. return -EIO;
  1403. }
  1404. return 0;
  1405. }
  1406. static int brightness_write(char *buf)
  1407. {
  1408. int level;
  1409. int new_level;
  1410. char *cmd;
  1411. while ((cmd = next_cmd(&buf))) {
  1412. if ((level = brightness_get(NULL)) < 0)
  1413. return level;
  1414. level &= 7;
  1415. if (strlencmp(cmd, "up") == 0) {
  1416. new_level = level == 7 ? 7 : level + 1;
  1417. } else if (strlencmp(cmd, "down") == 0) {
  1418. new_level = level == 0 ? 0 : level - 1;
  1419. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  1420. new_level >= 0 && new_level <= 7) {
  1421. /* new_level set */
  1422. } else
  1423. return -EINVAL;
  1424. brightness_set(new_level);
  1425. }
  1426. return 0;
  1427. }
  1428. static int brightness_update_status(struct backlight_device *bd)
  1429. {
  1430. return brightness_set(bd->props->brightness);
  1431. }
  1432. static struct backlight_properties ibm_backlight_data = {
  1433. .owner = THIS_MODULE,
  1434. .get_brightness = brightness_get,
  1435. .update_status = brightness_update_status,
  1436. .max_brightness = 7,
  1437. };
  1438. static int brightness_init(void)
  1439. {
  1440. ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
  1441. &ibm_backlight_data);
  1442. if (IS_ERR(ibm_backlight_device)) {
  1443. printk(IBM_ERR "Could not register backlight device\n");
  1444. return PTR_ERR(ibm_backlight_device);
  1445. }
  1446. return 0;
  1447. }
  1448. static void brightness_exit(void)
  1449. {
  1450. if (ibm_backlight_device) {
  1451. backlight_device_unregister(ibm_backlight_device);
  1452. ibm_backlight_device = NULL;
  1453. }
  1454. }
  1455. static int volume_offset = 0x30;
  1456. static int volume_read(char *p)
  1457. {
  1458. int len = 0;
  1459. u8 level;
  1460. if (!acpi_ec_read(volume_offset, &level)) {
  1461. len += sprintf(p + len, "level:\t\tunreadable\n");
  1462. } else {
  1463. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  1464. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  1465. len += sprintf(p + len, "commands:\tup, down, mute\n");
  1466. len += sprintf(p + len, "commands:\tlevel <level>"
  1467. " (<level> is 0-15)\n");
  1468. }
  1469. return len;
  1470. }
  1471. #define VOLUME_DOWN 0
  1472. #define VOLUME_UP 1
  1473. #define VOLUME_MUTE 2
  1474. static int volume_write(char *buf)
  1475. {
  1476. int cmos_cmd, inc, i;
  1477. u8 level, mute;
  1478. int new_level, new_mute;
  1479. char *cmd;
  1480. while ((cmd = next_cmd(&buf))) {
  1481. if (!acpi_ec_read(volume_offset, &level))
  1482. return -EIO;
  1483. new_mute = mute = level & 0x40;
  1484. new_level = level = level & 0xf;
  1485. if (strlencmp(cmd, "up") == 0) {
  1486. if (mute)
  1487. new_mute = 0;
  1488. else
  1489. new_level = level == 15 ? 15 : level + 1;
  1490. } else if (strlencmp(cmd, "down") == 0) {
  1491. if (mute)
  1492. new_mute = 0;
  1493. else
  1494. new_level = level == 0 ? 0 : level - 1;
  1495. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  1496. new_level >= 0 && new_level <= 15) {
  1497. /* new_level set */
  1498. } else if (strlencmp(cmd, "mute") == 0) {
  1499. new_mute = 0x40;
  1500. } else
  1501. return -EINVAL;
  1502. if (new_level != level) { /* mute doesn't change */
  1503. cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
  1504. inc = new_level > level ? 1 : -1;
  1505. if (mute && (!cmos_eval(cmos_cmd) ||
  1506. !acpi_ec_write(volume_offset, level)))
  1507. return -EIO;
  1508. for (i = level; i != new_level; i += inc)
  1509. if (!cmos_eval(cmos_cmd) ||
  1510. !acpi_ec_write(volume_offset, i + inc))
  1511. return -EIO;
  1512. if (mute && (!cmos_eval(VOLUME_MUTE) ||
  1513. !acpi_ec_write(volume_offset,
  1514. new_level + mute)))
  1515. return -EIO;
  1516. }
  1517. if (new_mute != mute) { /* level doesn't change */
  1518. cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
  1519. if (!cmos_eval(cmos_cmd) ||
  1520. !acpi_ec_write(volume_offset, level + new_mute))
  1521. return -EIO;
  1522. }
  1523. }
  1524. return 0;
  1525. }
  1526. static enum fan_status_access_mode fan_status_access_mode;
  1527. static enum fan_control_access_mode fan_control_access_mode;
  1528. static enum fan_control_commands fan_control_commands;
  1529. static int fan_control_status_known;
  1530. static u8 fan_control_initial_status;
  1531. static void fan_watchdog_fire(void *ignored);
  1532. static int fan_watchdog_maxinterval;
  1533. static DECLARE_WORK(fan_watchdog_task, fan_watchdog_fire, NULL);
  1534. static int fan_init(void)
  1535. {
  1536. fan_status_access_mode = IBMACPI_FAN_NONE;
  1537. fan_control_access_mode = IBMACPI_FAN_WR_NONE;
  1538. fan_control_commands = 0;
  1539. fan_control_status_known = 1;
  1540. fan_watchdog_maxinterval = 0;
  1541. if (gfan_handle) {
  1542. /* 570, 600e/x, 770e, 770x */
  1543. fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
  1544. } else {
  1545. /* all other ThinkPads: note that even old-style
  1546. * ThinkPad ECs supports the fan control register */
  1547. if (likely(acpi_ec_read(fan_status_offset,
  1548. &fan_control_initial_status))) {
  1549. fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
  1550. /* In some ThinkPads, neither the EC nor the ACPI
  1551. * DSDT initialize the fan status, and it ends up
  1552. * being set to 0x07 when it *could* be either
  1553. * 0x07 or 0x80.
  1554. *
  1555. * Enable for TP-1Y (T43), TP-78 (R51e),
  1556. * TP-76 (R52), TP-70 (T43, R52), which are known
  1557. * to be buggy. */
  1558. if (fan_control_initial_status == 0x07 &&
  1559. ibm_thinkpad_ec_found &&
  1560. ((ibm_thinkpad_ec_found[0] == '1' &&
  1561. ibm_thinkpad_ec_found[1] == 'Y') ||
  1562. (ibm_thinkpad_ec_found[0] == '7' &&
  1563. (ibm_thinkpad_ec_found[1] == '6' ||
  1564. ibm_thinkpad_ec_found[1] == '8' ||
  1565. ibm_thinkpad_ec_found[1] == '0'))
  1566. )) {
  1567. printk(IBM_NOTICE
  1568. "fan_init: initial fan status is "
  1569. "unknown, assuming it is in auto "
  1570. "mode\n");
  1571. fan_control_status_known = 0;
  1572. }
  1573. } else {
  1574. printk(IBM_ERR
  1575. "ThinkPad ACPI EC access misbehaving, "
  1576. "fan status and control unavailable\n");
  1577. return 0;
  1578. }
  1579. }
  1580. if (sfan_handle) {
  1581. /* 570, 770x-JL */
  1582. fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
  1583. fan_control_commands |=
  1584. IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
  1585. } else {
  1586. if (!gfan_handle) {
  1587. /* gfan without sfan means no fan control */
  1588. /* all other models implement TP EC 0x2f control */
  1589. if (fans_handle) {
  1590. /* X31, X40, X41 */
  1591. fan_control_access_mode =
  1592. IBMACPI_FAN_WR_ACPI_FANS;
  1593. fan_control_commands |=
  1594. IBMACPI_FAN_CMD_SPEED |
  1595. IBMACPI_FAN_CMD_LEVEL |
  1596. IBMACPI_FAN_CMD_ENABLE;
  1597. } else {
  1598. fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
  1599. fan_control_commands |=
  1600. IBMACPI_FAN_CMD_LEVEL |
  1601. IBMACPI_FAN_CMD_ENABLE;
  1602. }
  1603. }
  1604. }
  1605. return 0;
  1606. }
  1607. static int fan_get_status(u8 *status)
  1608. {
  1609. u8 s;
  1610. /* TODO:
  1611. * Add IBMACPI_FAN_RD_ACPI_FANS ? */
  1612. switch (fan_status_access_mode) {
  1613. case IBMACPI_FAN_RD_ACPI_GFAN:
  1614. /* 570, 600e/x, 770e, 770x */
  1615. if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
  1616. return -EIO;
  1617. if (likely(status))
  1618. *status = s & 0x07;
  1619. break;
  1620. case IBMACPI_FAN_RD_TPEC:
  1621. /* all except 570, 600e/x, 770e, 770x */
  1622. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  1623. return -EIO;
  1624. if (likely(status))
  1625. *status = s;
  1626. break;
  1627. default:
  1628. return -ENXIO;
  1629. }
  1630. return 0;
  1631. }
  1632. static int fan_get_speed(unsigned int *speed)
  1633. {
  1634. u8 hi, lo;
  1635. switch (fan_status_access_mode) {
  1636. case IBMACPI_FAN_RD_TPEC:
  1637. /* all except 570, 600e/x, 770e, 770x */
  1638. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  1639. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  1640. return -EIO;
  1641. if (likely(speed))
  1642. *speed = (hi << 8) | lo;
  1643. break;
  1644. default:
  1645. return -ENXIO;
  1646. }
  1647. return 0;
  1648. }
  1649. static void fan_exit(void)
  1650. {
  1651. cancel_delayed_work(&fan_watchdog_task);
  1652. flush_scheduled_work();
  1653. }
  1654. static void fan_watchdog_reset(void)
  1655. {
  1656. static int fan_watchdog_active = 0;
  1657. if (fan_watchdog_active)
  1658. cancel_delayed_work(&fan_watchdog_task);
  1659. if (fan_watchdog_maxinterval > 0) {
  1660. fan_watchdog_active = 1;
  1661. if (!schedule_delayed_work(&fan_watchdog_task,
  1662. msecs_to_jiffies(fan_watchdog_maxinterval
  1663. * 1000))) {
  1664. printk(IBM_ERR "failed to schedule the fan watchdog, "
  1665. "watchdog will not trigger\n");
  1666. }
  1667. } else
  1668. fan_watchdog_active = 0;
  1669. }
  1670. static int fan_read(char *p)
  1671. {
  1672. int len = 0;
  1673. int rc;
  1674. u8 status;
  1675. unsigned int speed = 0;
  1676. switch (fan_status_access_mode) {
  1677. case IBMACPI_FAN_RD_ACPI_GFAN:
  1678. /* 570, 600e/x, 770e, 770x */
  1679. if ((rc = fan_get_status(&status)) < 0)
  1680. return rc;
  1681. len += sprintf(p + len, "status:\t\t%s\n"
  1682. "level:\t\t%d\n",
  1683. (status != 0) ? "enabled" : "disabled", status);
  1684. break;
  1685. case IBMACPI_FAN_RD_TPEC:
  1686. /* all except 570, 600e/x, 770e, 770x */
  1687. if ((rc = fan_get_status(&status)) < 0)
  1688. return rc;
  1689. if (unlikely(!fan_control_status_known)) {
  1690. if (status != fan_control_initial_status)
  1691. fan_control_status_known = 1;
  1692. else
  1693. /* Return most likely status. In fact, it
  1694. * might be the only possible status */
  1695. status = IBMACPI_FAN_EC_AUTO;
  1696. }
  1697. len += sprintf(p + len, "status:\t\t%s\n",
  1698. (status != 0) ? "enabled" : "disabled");
  1699. /* No ThinkPad boots on disengaged mode, we can safely
  1700. * assume the tachometer is online if fan control status
  1701. * was unknown */
  1702. if ((rc = fan_get_speed(&speed)) < 0)
  1703. return rc;
  1704. len += sprintf(p + len, "speed:\t\t%d\n", speed);
  1705. if (status & IBMACPI_FAN_EC_DISENGAGED)
  1706. /* Disengaged mode takes precedence */
  1707. len += sprintf(p + len, "level:\t\tdisengaged\n");
  1708. else if (status & IBMACPI_FAN_EC_AUTO)
  1709. len += sprintf(p + len, "level:\t\tauto\n");
  1710. else
  1711. len += sprintf(p + len, "level:\t\t%d\n", status);
  1712. break;
  1713. case IBMACPI_FAN_NONE:
  1714. default:
  1715. len += sprintf(p + len, "status:\t\tnot supported\n");
  1716. }
  1717. if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
  1718. len += sprintf(p + len, "commands:\tlevel <level>");
  1719. switch (fan_control_access_mode) {
  1720. case IBMACPI_FAN_WR_ACPI_SFAN:
  1721. len += sprintf(p + len, " (<level> is 0-7)\n");
  1722. break;
  1723. default:
  1724. len += sprintf(p + len, " (<level> is 0-7, "
  1725. "auto, disengaged)\n");
  1726. break;
  1727. }
  1728. }
  1729. if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
  1730. len += sprintf(p + len, "commands:\tenable, disable\n"
  1731. "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
  1732. "1-120 (seconds))\n");
  1733. if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
  1734. len += sprintf(p + len, "commands:\tspeed <speed>"
  1735. " (<speed> is 0-65535)\n");
  1736. return len;
  1737. }
  1738. static int fan_set_level(int level)
  1739. {
  1740. switch (fan_control_access_mode) {
  1741. case IBMACPI_FAN_WR_ACPI_SFAN:
  1742. if (level >= 0 && level <= 7) {
  1743. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  1744. return -EIO;
  1745. } else
  1746. return -EINVAL;
  1747. break;
  1748. case IBMACPI_FAN_WR_ACPI_FANS:
  1749. case IBMACPI_FAN_WR_TPEC:
  1750. if ((level != IBMACPI_FAN_EC_AUTO) &&
  1751. (level != IBMACPI_FAN_EC_DISENGAGED) &&
  1752. ((level < 0) || (level > 7)))
  1753. return -EINVAL;
  1754. if (!acpi_ec_write(fan_status_offset, level))
  1755. return -EIO;
  1756. else
  1757. fan_control_status_known = 1;
  1758. break;
  1759. default:
  1760. return -ENXIO;
  1761. }
  1762. return 0;
  1763. }
  1764. static int fan_set_enable(void)
  1765. {
  1766. u8 s;
  1767. int rc;
  1768. switch (fan_control_access_mode) {
  1769. case IBMACPI_FAN_WR_ACPI_FANS:
  1770. case IBMACPI_FAN_WR_TPEC:
  1771. if ((rc = fan_get_status(&s)) < 0)
  1772. return rc;
  1773. /* Don't go out of emergency fan mode */
  1774. if (s != 7)
  1775. s = IBMACPI_FAN_EC_AUTO;
  1776. if (!acpi_ec_write(fan_status_offset, s))
  1777. return -EIO;
  1778. else
  1779. fan_control_status_known = 1;
  1780. break;
  1781. case IBMACPI_FAN_WR_ACPI_SFAN:
  1782. if ((rc = fan_get_status(&s)) < 0)
  1783. return rc;
  1784. s &= 0x07;
  1785. /* Set fan to at least level 4 */
  1786. if (s < 4)
  1787. s = 4;
  1788. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  1789. return -EIO;
  1790. break;
  1791. default:
  1792. return -ENXIO;
  1793. }
  1794. return 0;
  1795. }
  1796. static int fan_set_disable(void)
  1797. {
  1798. switch (fan_control_access_mode) {
  1799. case IBMACPI_FAN_WR_ACPI_FANS:
  1800. case IBMACPI_FAN_WR_TPEC:
  1801. if (!acpi_ec_write(fan_status_offset, 0x00))
  1802. return -EIO;
  1803. else
  1804. fan_control_status_known = 1;
  1805. break;
  1806. case IBMACPI_FAN_WR_ACPI_SFAN:
  1807. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  1808. return -EIO;
  1809. break;
  1810. default:
  1811. return -ENXIO;
  1812. }
  1813. return 0;
  1814. }
  1815. static int fan_set_speed(int speed)
  1816. {
  1817. switch (fan_control_access_mode) {
  1818. case IBMACPI_FAN_WR_ACPI_FANS:
  1819. if (speed >= 0 && speed <= 65535) {
  1820. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  1821. speed, speed, speed))
  1822. return -EIO;
  1823. } else
  1824. return -EINVAL;
  1825. break;
  1826. default:
  1827. return -ENXIO;
  1828. }
  1829. return 0;
  1830. }
  1831. static int fan_write_cmd_level(const char *cmd, int *rc)
  1832. {
  1833. int level;
  1834. if (strlencmp(cmd, "level auto") == 0)
  1835. level = IBMACPI_FAN_EC_AUTO;
  1836. else if (strlencmp(cmd, "level disengaged") == 0)
  1837. level = IBMACPI_FAN_EC_DISENGAGED;
  1838. else if (sscanf(cmd, "level %d", &level) != 1)
  1839. return 0;
  1840. if ((*rc = fan_set_level(level)) == -ENXIO)
  1841. printk(IBM_ERR "level command accepted for unsupported "
  1842. "access mode %d", fan_control_access_mode);
  1843. return 1;
  1844. }
  1845. static int fan_write_cmd_enable(const char *cmd, int *rc)
  1846. {
  1847. if (strlencmp(cmd, "enable") != 0)
  1848. return 0;
  1849. if ((*rc = fan_set_enable()) == -ENXIO)
  1850. printk(IBM_ERR "enable command accepted for unsupported "
  1851. "access mode %d", fan_control_access_mode);
  1852. return 1;
  1853. }
  1854. static int fan_write_cmd_disable(const char *cmd, int *rc)
  1855. {
  1856. if (strlencmp(cmd, "disable") != 0)
  1857. return 0;
  1858. if ((*rc = fan_set_disable()) == -ENXIO)
  1859. printk(IBM_ERR "disable command accepted for unsupported "
  1860. "access mode %d", fan_control_access_mode);
  1861. return 1;
  1862. }
  1863. static int fan_write_cmd_speed(const char *cmd, int *rc)
  1864. {
  1865. int speed;
  1866. /* TODO:
  1867. * Support speed <low> <medium> <high> ? */
  1868. if (sscanf(cmd, "speed %d", &speed) != 1)
  1869. return 0;
  1870. if ((*rc = fan_set_speed(speed)) == -ENXIO)
  1871. printk(IBM_ERR "speed command accepted for unsupported "
  1872. "access mode %d", fan_control_access_mode);
  1873. return 1;
  1874. }
  1875. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  1876. {
  1877. int interval;
  1878. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  1879. return 0;
  1880. if (interval < 0 || interval > 120)
  1881. *rc = -EINVAL;
  1882. else
  1883. fan_watchdog_maxinterval = interval;
  1884. return 1;
  1885. }
  1886. static int fan_write(char *buf)
  1887. {
  1888. char *cmd;
  1889. int rc = 0;
  1890. while (!rc && (cmd = next_cmd(&buf))) {
  1891. if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
  1892. fan_write_cmd_level(cmd, &rc)) &&
  1893. !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
  1894. (fan_write_cmd_enable(cmd, &rc) ||
  1895. fan_write_cmd_disable(cmd, &rc) ||
  1896. fan_write_cmd_watchdog(cmd, &rc))) &&
  1897. !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
  1898. fan_write_cmd_speed(cmd, &rc))
  1899. )
  1900. rc = -EINVAL;
  1901. else if (!rc)
  1902. fan_watchdog_reset();
  1903. }
  1904. return rc;
  1905. }
  1906. static void fan_watchdog_fire(void *ignored)
  1907. {
  1908. printk(IBM_NOTICE "fan watchdog: enabling fan\n");
  1909. if (fan_set_enable()) {
  1910. printk(IBM_ERR "fan watchdog: error while enabling fan\n");
  1911. /* reschedule for later */
  1912. fan_watchdog_reset();
  1913. }
  1914. }
  1915. static struct ibm_struct ibms[] = {
  1916. {
  1917. .name = "driver",
  1918. .init = driver_init,
  1919. .read = driver_read,
  1920. },
  1921. {
  1922. .name = "hotkey",
  1923. .hid = IBM_HKEY_HID,
  1924. .init = hotkey_init,
  1925. .read = hotkey_read,
  1926. .write = hotkey_write,
  1927. .exit = hotkey_exit,
  1928. .notify = hotkey_notify,
  1929. .handle = &hkey_handle,
  1930. .type = ACPI_DEVICE_NOTIFY,
  1931. },
  1932. {
  1933. .name = "bluetooth",
  1934. .init = bluetooth_init,
  1935. .read = bluetooth_read,
  1936. .write = bluetooth_write,
  1937. },
  1938. {
  1939. .name = "wan",
  1940. .init = wan_init,
  1941. .read = wan_read,
  1942. .write = wan_write,
  1943. .experimental = 1,
  1944. },
  1945. {
  1946. .name = "video",
  1947. .init = video_init,
  1948. .read = video_read,
  1949. .write = video_write,
  1950. .exit = video_exit,
  1951. },
  1952. {
  1953. .name = "light",
  1954. .init = light_init,
  1955. .read = light_read,
  1956. .write = light_write,
  1957. },
  1958. #ifdef CONFIG_ACPI_IBM_DOCK
  1959. {
  1960. .name = "dock",
  1961. .read = dock_read,
  1962. .write = dock_write,
  1963. .notify = dock_notify,
  1964. .handle = &dock_handle,
  1965. .type = ACPI_SYSTEM_NOTIFY,
  1966. },
  1967. {
  1968. .name = "dock",
  1969. .hid = IBM_PCI_HID,
  1970. .notify = dock_notify,
  1971. .handle = &pci_handle,
  1972. .type = ACPI_SYSTEM_NOTIFY,
  1973. },
  1974. #endif
  1975. #ifdef CONFIG_ACPI_IBM_BAY
  1976. {
  1977. .name = "bay",
  1978. .init = bay_init,
  1979. .read = bay_read,
  1980. .write = bay_write,
  1981. .notify = bay_notify,
  1982. .handle = &bay_handle,
  1983. .type = ACPI_SYSTEM_NOTIFY,
  1984. },
  1985. #endif
  1986. {
  1987. .name = "cmos",
  1988. .read = cmos_read,
  1989. .write = cmos_write,
  1990. },
  1991. {
  1992. .name = "led",
  1993. .init = led_init,
  1994. .read = led_read,
  1995. .write = led_write,
  1996. },
  1997. {
  1998. .name = "beep",
  1999. .read = beep_read,
  2000. .write = beep_write,
  2001. },
  2002. {
  2003. .name = "thermal",
  2004. .init = thermal_init,
  2005. .read = thermal_read,
  2006. },
  2007. {
  2008. .name = "ecdump",
  2009. .read = ecdump_read,
  2010. .write = ecdump_write,
  2011. .experimental = 1,
  2012. },
  2013. {
  2014. .name = "brightness",
  2015. .read = brightness_read,
  2016. .write = brightness_write,
  2017. .init = brightness_init,
  2018. .exit = brightness_exit,
  2019. },
  2020. {
  2021. .name = "volume",
  2022. .read = volume_read,
  2023. .write = volume_write,
  2024. },
  2025. {
  2026. .name = "fan",
  2027. .read = fan_read,
  2028. .write = fan_write,
  2029. .init = fan_init,
  2030. .exit = fan_exit,
  2031. .experimental = 1,
  2032. },
  2033. };
  2034. static int dispatch_read(char *page, char **start, off_t off, int count,
  2035. int *eof, void *data)
  2036. {
  2037. struct ibm_struct *ibm = (struct ibm_struct *)data;
  2038. int len;
  2039. if (!ibm || !ibm->read)
  2040. return -EINVAL;
  2041. len = ibm->read(page);
  2042. if (len < 0)
  2043. return len;
  2044. if (len <= off + count)
  2045. *eof = 1;
  2046. *start = page + off;
  2047. len -= off;
  2048. if (len > count)
  2049. len = count;
  2050. if (len < 0)
  2051. len = 0;
  2052. return len;
  2053. }
  2054. static int dispatch_write(struct file *file, const char __user * userbuf,
  2055. unsigned long count, void *data)
  2056. {
  2057. struct ibm_struct *ibm = (struct ibm_struct *)data;
  2058. char *kernbuf;
  2059. int ret;
  2060. if (!ibm || !ibm->write)
  2061. return -EINVAL;
  2062. kernbuf = kmalloc(count + 2, GFP_KERNEL);
  2063. if (!kernbuf)
  2064. return -ENOMEM;
  2065. if (copy_from_user(kernbuf, userbuf, count)) {
  2066. kfree(kernbuf);
  2067. return -EFAULT;
  2068. }
  2069. kernbuf[count] = 0;
  2070. strcat(kernbuf, ",");
  2071. ret = ibm->write(kernbuf);
  2072. if (ret == 0)
  2073. ret = count;
  2074. kfree(kernbuf);
  2075. return ret;
  2076. }
  2077. static void dispatch_notify(acpi_handle handle, u32 event, void *data)
  2078. {
  2079. struct ibm_struct *ibm = (struct ibm_struct *)data;
  2080. if (!ibm || !ibm->notify)
  2081. return;
  2082. ibm->notify(ibm, event);
  2083. }
  2084. static int __init setup_notify(struct ibm_struct *ibm)
  2085. {
  2086. acpi_status status;
  2087. int ret;
  2088. if (!*ibm->handle)
  2089. return 0;
  2090. ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
  2091. if (ret < 0) {
  2092. printk(IBM_ERR "%s device not present\n", ibm->name);
  2093. return 0;
  2094. }
  2095. acpi_driver_data(ibm->device) = ibm;
  2096. sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
  2097. status = acpi_install_notify_handler(*ibm->handle, ibm->type,
  2098. dispatch_notify, ibm);
  2099. if (ACPI_FAILURE(status)) {
  2100. printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
  2101. ibm->name, status);
  2102. return -ENODEV;
  2103. }
  2104. ibm->notify_installed = 1;
  2105. return 0;
  2106. }
  2107. static int __init ibm_device_add(struct acpi_device *device)
  2108. {
  2109. return 0;
  2110. }
  2111. static int __init register_driver(struct ibm_struct *ibm)
  2112. {
  2113. int ret;
  2114. ibm->driver = kmalloc(sizeof(struct acpi_driver), GFP_KERNEL);
  2115. if (!ibm->driver) {
  2116. printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
  2117. return -1;
  2118. }
  2119. memset(ibm->driver, 0, sizeof(struct acpi_driver));
  2120. sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
  2121. ibm->driver->ids = ibm->hid;
  2122. ibm->driver->ops.add = &ibm_device_add;
  2123. ret = acpi_bus_register_driver(ibm->driver);
  2124. if (ret < 0) {
  2125. printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
  2126. ibm->hid, ret);
  2127. kfree(ibm->driver);
  2128. }
  2129. return ret;
  2130. }
  2131. static int __init ibm_init(struct ibm_struct *ibm)
  2132. {
  2133. int ret;
  2134. struct proc_dir_entry *entry;
  2135. if (ibm->experimental && !experimental)
  2136. return 0;
  2137. if (ibm->hid) {
  2138. ret = register_driver(ibm);
  2139. if (ret < 0)
  2140. return ret;
  2141. ibm->driver_registered = 1;
  2142. }
  2143. if (ibm->init) {
  2144. ret = ibm->init();
  2145. if (ret != 0)
  2146. return ret;
  2147. ibm->init_called = 1;
  2148. }
  2149. if (ibm->read) {
  2150. entry = create_proc_entry(ibm->name,
  2151. S_IFREG | S_IRUGO | S_IWUSR,
  2152. proc_dir);
  2153. if (!entry) {
  2154. printk(IBM_ERR "unable to create proc entry %s\n",
  2155. ibm->name);
  2156. return -ENODEV;
  2157. }
  2158. entry->owner = THIS_MODULE;
  2159. entry->data = ibm;
  2160. entry->read_proc = &dispatch_read;
  2161. if (ibm->write)
  2162. entry->write_proc = &dispatch_write;
  2163. ibm->proc_created = 1;
  2164. }
  2165. if (ibm->notify) {
  2166. ret = setup_notify(ibm);
  2167. if (ret < 0)
  2168. return ret;
  2169. }
  2170. return 0;
  2171. }
  2172. static void ibm_exit(struct ibm_struct *ibm)
  2173. {
  2174. if (ibm->notify_installed)
  2175. acpi_remove_notify_handler(*ibm->handle, ibm->type,
  2176. dispatch_notify);
  2177. if (ibm->proc_created)
  2178. remove_proc_entry(ibm->name, proc_dir);
  2179. if (ibm->init_called && ibm->exit)
  2180. ibm->exit();
  2181. if (ibm->driver_registered) {
  2182. acpi_bus_unregister_driver(ibm->driver);
  2183. kfree(ibm->driver);
  2184. }
  2185. }
  2186. static void __init ibm_handle_init(char *name,
  2187. acpi_handle * handle, acpi_handle parent,
  2188. char **paths, int num_paths, char **path)
  2189. {
  2190. int i;
  2191. acpi_status status;
  2192. for (i = 0; i < num_paths; i++) {
  2193. status = acpi_get_handle(parent, paths[i], handle);
  2194. if (ACPI_SUCCESS(status)) {
  2195. *path = paths[i];
  2196. return;
  2197. }
  2198. }
  2199. *handle = NULL;
  2200. }
  2201. #define IBM_HANDLE_INIT(object) \
  2202. ibm_handle_init(#object, &object##_handle, *object##_parent, \
  2203. object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
  2204. static int set_ibm_param(const char *val, struct kernel_param *kp)
  2205. {
  2206. unsigned int i;
  2207. for (i = 0; i < ARRAY_SIZE(ibms); i++)
  2208. if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
  2209. if (strlen(val) > sizeof(ibms[i].param) - 2)
  2210. return -ENOSPC;
  2211. strcpy(ibms[i].param, val);
  2212. strcat(ibms[i].param, ",");
  2213. return 0;
  2214. }
  2215. return -EINVAL;
  2216. }
  2217. #define IBM_PARAM(feature) \
  2218. module_param_call(feature, set_ibm_param, NULL, NULL, 0)
  2219. IBM_PARAM(hotkey);
  2220. IBM_PARAM(bluetooth);
  2221. IBM_PARAM(video);
  2222. IBM_PARAM(light);
  2223. #ifdef CONFIG_ACPI_IBM_DOCK
  2224. IBM_PARAM(dock);
  2225. #endif
  2226. #ifdef CONFIG_ACPI_IBM_BAY
  2227. IBM_PARAM(bay);
  2228. #endif
  2229. IBM_PARAM(cmos);
  2230. IBM_PARAM(led);
  2231. IBM_PARAM(beep);
  2232. IBM_PARAM(ecdump);
  2233. IBM_PARAM(brightness);
  2234. IBM_PARAM(volume);
  2235. IBM_PARAM(fan);
  2236. static void acpi_ibm_exit(void)
  2237. {
  2238. int i;
  2239. for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
  2240. ibm_exit(&ibms[i]);
  2241. remove_proc_entry(IBM_DIR, acpi_root_dir);
  2242. if (ibm_thinkpad_ec_found)
  2243. kfree(ibm_thinkpad_ec_found);
  2244. }
  2245. static char* __init check_dmi_for_ec(void)
  2246. {
  2247. struct dmi_device *dev = NULL;
  2248. char ec_fw_string[18];
  2249. /*
  2250. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  2251. * X32 or newer, all Z series; Some models must have an
  2252. * up-to-date BIOS or they will not be detected.
  2253. *
  2254. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  2255. */
  2256. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  2257. if (sscanf(dev->name,
  2258. "IBM ThinkPad Embedded Controller -[%17c",
  2259. ec_fw_string) == 1) {
  2260. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  2261. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  2262. return kstrdup(ec_fw_string, GFP_KERNEL);
  2263. }
  2264. }
  2265. return NULL;
  2266. }
  2267. static int __init acpi_ibm_init(void)
  2268. {
  2269. int ret, i;
  2270. if (acpi_disabled)
  2271. return -ENODEV;
  2272. if (!acpi_specific_hotkey_enabled) {
  2273. printk(IBM_ERR "using generic hotkey driver\n");
  2274. return -ENODEV;
  2275. }
  2276. /* ec is required because many other handles are relative to it */
  2277. IBM_HANDLE_INIT(ec);
  2278. if (!ec_handle) {
  2279. printk(IBM_ERR "ec object not found\n");
  2280. return -ENODEV;
  2281. }
  2282. /* Models with newer firmware report the EC in DMI */
  2283. ibm_thinkpad_ec_found = check_dmi_for_ec();
  2284. if (ibm_thinkpad_ec_found)
  2285. printk(IBM_INFO "ThinkPad EC firmware %s\n",
  2286. ibm_thinkpad_ec_found);
  2287. /* these handles are not required */
  2288. IBM_HANDLE_INIT(vid);
  2289. IBM_HANDLE_INIT(vid2);
  2290. IBM_HANDLE_INIT(ledb);
  2291. IBM_HANDLE_INIT(led);
  2292. IBM_HANDLE_INIT(hkey);
  2293. IBM_HANDLE_INIT(lght);
  2294. IBM_HANDLE_INIT(cmos);
  2295. #ifdef CONFIG_ACPI_IBM_DOCK
  2296. IBM_HANDLE_INIT(dock);
  2297. #endif
  2298. IBM_HANDLE_INIT(pci);
  2299. #ifdef CONFIG_ACPI_IBM_BAY
  2300. IBM_HANDLE_INIT(bay);
  2301. if (bay_handle)
  2302. IBM_HANDLE_INIT(bay_ej);
  2303. IBM_HANDLE_INIT(bay2);
  2304. if (bay2_handle)
  2305. IBM_HANDLE_INIT(bay2_ej);
  2306. #endif
  2307. IBM_HANDLE_INIT(beep);
  2308. IBM_HANDLE_INIT(ecrd);
  2309. IBM_HANDLE_INIT(ecwr);
  2310. IBM_HANDLE_INIT(fans);
  2311. IBM_HANDLE_INIT(gfan);
  2312. IBM_HANDLE_INIT(sfan);
  2313. proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
  2314. if (!proc_dir) {
  2315. printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
  2316. return -ENODEV;
  2317. }
  2318. proc_dir->owner = THIS_MODULE;
  2319. for (i = 0; i < ARRAY_SIZE(ibms); i++) {
  2320. ret = ibm_init(&ibms[i]);
  2321. if (ret >= 0 && *ibms[i].param)
  2322. ret = ibms[i].write(ibms[i].param);
  2323. if (ret < 0) {
  2324. acpi_ibm_exit();
  2325. return ret;
  2326. }
  2327. }
  2328. return 0;
  2329. }
  2330. module_init(acpi_ibm_init);
  2331. module_exit(acpi_ibm_exit);