ibm_acpi.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781
  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 /* CONFIG_ACPI_IBM_BAY */
  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 ibm_acpi_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. if (ibm_thinkpad_ec_found)
  435. printk(IBM_INFO "ThinkPad EC firmware %s\n",
  436. ibm_thinkpad_ec_found);
  437. return 0;
  438. }
  439. static int driver_read(char *p)
  440. {
  441. int len = 0;
  442. len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
  443. len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
  444. return len;
  445. }
  446. static int hotkey_supported;
  447. static int hotkey_mask_supported;
  448. static int hotkey_orig_status;
  449. static int hotkey_orig_mask;
  450. static int hotkey_get(int *status, int *mask)
  451. {
  452. if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
  453. return 0;
  454. if (hotkey_mask_supported)
  455. if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
  456. return 0;
  457. return 1;
  458. }
  459. static int hotkey_set(int status, int mask)
  460. {
  461. int i;
  462. if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
  463. return 0;
  464. if (hotkey_mask_supported)
  465. for (i = 0; i < 32; i++) {
  466. int bit = ((1 << i) & mask) != 0;
  467. if (!acpi_evalf(hkey_handle,
  468. NULL, "MHKM", "vdd", i + 1, bit))
  469. return 0;
  470. }
  471. return 1;
  472. }
  473. static int hotkey_init(void)
  474. {
  475. /* hotkey not supported on 570 */
  476. hotkey_supported = hkey_handle != NULL;
  477. if (hotkey_supported) {
  478. /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  479. A30, R30, R31, T20-22, X20-21, X22-24 */
  480. hotkey_mask_supported =
  481. acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
  482. if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
  483. return -ENODEV;
  484. }
  485. return 0;
  486. }
  487. static int hotkey_read(char *p)
  488. {
  489. int status, mask;
  490. int len = 0;
  491. if (!hotkey_supported) {
  492. len += sprintf(p + len, "status:\t\tnot supported\n");
  493. return len;
  494. }
  495. if (!hotkey_get(&status, &mask))
  496. return -EIO;
  497. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
  498. if (hotkey_mask_supported) {
  499. len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
  500. len += sprintf(p + len,
  501. "commands:\tenable, disable, reset, <mask>\n");
  502. } else {
  503. len += sprintf(p + len, "mask:\t\tnot supported\n");
  504. len += sprintf(p + len, "commands:\tenable, disable, reset\n");
  505. }
  506. return len;
  507. }
  508. static int hotkey_write(char *buf)
  509. {
  510. int status, mask;
  511. char *cmd;
  512. int do_cmd = 0;
  513. if (!hotkey_supported)
  514. return -ENODEV;
  515. if (!hotkey_get(&status, &mask))
  516. return -EIO;
  517. while ((cmd = next_cmd(&buf))) {
  518. if (strlencmp(cmd, "enable") == 0) {
  519. status = 1;
  520. } else if (strlencmp(cmd, "disable") == 0) {
  521. status = 0;
  522. } else if (strlencmp(cmd, "reset") == 0) {
  523. status = hotkey_orig_status;
  524. mask = hotkey_orig_mask;
  525. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  526. /* mask set */
  527. } else if (sscanf(cmd, "%x", &mask) == 1) {
  528. /* mask set */
  529. } else
  530. return -EINVAL;
  531. do_cmd = 1;
  532. }
  533. if (do_cmd && !hotkey_set(status, mask))
  534. return -EIO;
  535. return 0;
  536. }
  537. static void hotkey_exit(void)
  538. {
  539. if (hotkey_supported)
  540. hotkey_set(hotkey_orig_status, hotkey_orig_mask);
  541. }
  542. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  543. {
  544. int hkey;
  545. if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
  546. acpi_bus_generate_event(ibm->device, event, hkey);
  547. else {
  548. printk(IBM_ERR "unknown hotkey event %d\n", event);
  549. acpi_bus_generate_event(ibm->device, event, 0);
  550. }
  551. }
  552. static int bluetooth_supported;
  553. static int bluetooth_init(void)
  554. {
  555. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  556. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  557. bluetooth_supported = hkey_handle &&
  558. acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
  559. return 0;
  560. }
  561. static int bluetooth_status(void)
  562. {
  563. int status;
  564. if (!bluetooth_supported ||
  565. !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  566. status = 0;
  567. return status;
  568. }
  569. static int bluetooth_read(char *p)
  570. {
  571. int len = 0;
  572. int status = bluetooth_status();
  573. if (!bluetooth_supported)
  574. len += sprintf(p + len, "status:\t\tnot supported\n");
  575. else if (!(status & 1))
  576. len += sprintf(p + len, "status:\t\tnot installed\n");
  577. else {
  578. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
  579. len += sprintf(p + len, "commands:\tenable, disable\n");
  580. }
  581. return len;
  582. }
  583. static int bluetooth_write(char *buf)
  584. {
  585. int status = bluetooth_status();
  586. char *cmd;
  587. int do_cmd = 0;
  588. if (!bluetooth_supported)
  589. return -ENODEV;
  590. while ((cmd = next_cmd(&buf))) {
  591. if (strlencmp(cmd, "enable") == 0) {
  592. status |= 2;
  593. } else if (strlencmp(cmd, "disable") == 0) {
  594. status &= ~2;
  595. } else
  596. return -EINVAL;
  597. do_cmd = 1;
  598. }
  599. if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  600. return -EIO;
  601. return 0;
  602. }
  603. static int wan_supported;
  604. static int wan_init(void)
  605. {
  606. wan_supported = hkey_handle &&
  607. acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
  608. return 0;
  609. }
  610. static int wan_status(void)
  611. {
  612. int status;
  613. if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  614. status = 0;
  615. return status;
  616. }
  617. static int wan_read(char *p)
  618. {
  619. int len = 0;
  620. int status = wan_status();
  621. if (!wan_supported)
  622. len += sprintf(p + len, "status:\t\tnot supported\n");
  623. else if (!(status & 1))
  624. len += sprintf(p + len, "status:\t\tnot installed\n");
  625. else {
  626. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
  627. len += sprintf(p + len, "commands:\tenable, disable\n");
  628. }
  629. return len;
  630. }
  631. static int wan_write(char *buf)
  632. {
  633. int status = wan_status();
  634. char *cmd;
  635. int do_cmd = 0;
  636. if (!wan_supported)
  637. return -ENODEV;
  638. while ((cmd = next_cmd(&buf))) {
  639. if (strlencmp(cmd, "enable") == 0) {
  640. status |= 2;
  641. } else if (strlencmp(cmd, "disable") == 0) {
  642. status &= ~2;
  643. } else
  644. return -EINVAL;
  645. do_cmd = 1;
  646. }
  647. if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  648. return -EIO;
  649. return 0;
  650. }
  651. enum video_access_mode {
  652. IBMACPI_VIDEO_NONE = 0,
  653. IBMACPI_VIDEO_570, /* 570 */
  654. IBMACPI_VIDEO_770, /* 600e/x, 770e, 770x */
  655. IBMACPI_VIDEO_NEW, /* all others */
  656. };
  657. static enum video_access_mode video_supported;
  658. static int video_orig_autosw;
  659. static int video_init(void)
  660. {
  661. int ivga;
  662. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  663. /* G41, assume IVGA doesn't change */
  664. vid_handle = vid2_handle;
  665. if (!vid_handle)
  666. /* video switching not supported on R30, R31 */
  667. video_supported = IBMACPI_VIDEO_NONE;
  668. else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  669. /* 570 */
  670. video_supported = IBMACPI_VIDEO_570;
  671. else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  672. /* 600e/x, 770e, 770x */
  673. video_supported = IBMACPI_VIDEO_770;
  674. else
  675. /* all others */
  676. video_supported = IBMACPI_VIDEO_NEW;
  677. return 0;
  678. }
  679. static int video_status(void)
  680. {
  681. int status = 0;
  682. int i;
  683. if (video_supported == IBMACPI_VIDEO_570) {
  684. if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
  685. status = i & 3;
  686. } else if (video_supported == IBMACPI_VIDEO_770) {
  687. if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
  688. status |= 0x01 * i;
  689. if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
  690. status |= 0x02 * i;
  691. } else if (video_supported == IBMACPI_VIDEO_NEW) {
  692. acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
  693. if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
  694. status |= 0x02 * i;
  695. acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
  696. if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
  697. status |= 0x01 * i;
  698. if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
  699. status |= 0x08 * i;
  700. }
  701. return status;
  702. }
  703. static int video_autosw(void)
  704. {
  705. int autosw = 0;
  706. if (video_supported == IBMACPI_VIDEO_570)
  707. acpi_evalf(vid_handle, &autosw, "SWIT", "d");
  708. else if (video_supported == IBMACPI_VIDEO_770 ||
  709. video_supported == IBMACPI_VIDEO_NEW)
  710. acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
  711. return autosw & 1;
  712. }
  713. static int video_read(char *p)
  714. {
  715. int status = video_status();
  716. int autosw = video_autosw();
  717. int len = 0;
  718. if (!video_supported) {
  719. len += sprintf(p + len, "status:\t\tnot supported\n");
  720. return len;
  721. }
  722. len += sprintf(p + len, "status:\t\tsupported\n");
  723. len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
  724. len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
  725. if (video_supported == IBMACPI_VIDEO_NEW)
  726. len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
  727. len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
  728. len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
  729. len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
  730. if (video_supported == IBMACPI_VIDEO_NEW)
  731. len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
  732. len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
  733. len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
  734. return len;
  735. }
  736. static int video_switch(void)
  737. {
  738. int autosw = video_autosw();
  739. int ret;
  740. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
  741. return -EIO;
  742. ret = video_supported == IBMACPI_VIDEO_570 ?
  743. acpi_evalf(ec_handle, NULL, "_Q16", "v") :
  744. acpi_evalf(vid_handle, NULL, "VSWT", "v");
  745. acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
  746. return ret;
  747. }
  748. static int video_expand(void)
  749. {
  750. if (video_supported == IBMACPI_VIDEO_570)
  751. return acpi_evalf(ec_handle, NULL, "_Q17", "v");
  752. else if (video_supported == IBMACPI_VIDEO_770)
  753. return acpi_evalf(vid_handle, NULL, "VEXP", "v");
  754. else
  755. return acpi_evalf(NULL, NULL, "\\VEXP", "v");
  756. }
  757. static int video_switch2(int status)
  758. {
  759. int ret;
  760. if (video_supported == IBMACPI_VIDEO_570) {
  761. ret = acpi_evalf(NULL, NULL,
  762. "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
  763. } else if (video_supported == IBMACPI_VIDEO_770) {
  764. int autosw = video_autosw();
  765. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
  766. return -EIO;
  767. ret = acpi_evalf(vid_handle, NULL,
  768. "ASWT", "vdd", status * 0x100, 0);
  769. acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
  770. } else {
  771. ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  772. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  773. }
  774. return ret;
  775. }
  776. static int video_write(char *buf)
  777. {
  778. char *cmd;
  779. int enable, disable, status;
  780. if (!video_supported)
  781. return -ENODEV;
  782. enable = disable = 0;
  783. while ((cmd = next_cmd(&buf))) {
  784. if (strlencmp(cmd, "lcd_enable") == 0) {
  785. enable |= 0x01;
  786. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  787. disable |= 0x01;
  788. } else if (strlencmp(cmd, "crt_enable") == 0) {
  789. enable |= 0x02;
  790. } else if (strlencmp(cmd, "crt_disable") == 0) {
  791. disable |= 0x02;
  792. } else if (video_supported == IBMACPI_VIDEO_NEW &&
  793. strlencmp(cmd, "dvi_enable") == 0) {
  794. enable |= 0x08;
  795. } else if (video_supported == IBMACPI_VIDEO_NEW &&
  796. strlencmp(cmd, "dvi_disable") == 0) {
  797. disable |= 0x08;
  798. } else if (strlencmp(cmd, "auto_enable") == 0) {
  799. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
  800. return -EIO;
  801. } else if (strlencmp(cmd, "auto_disable") == 0) {
  802. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
  803. return -EIO;
  804. } else if (strlencmp(cmd, "video_switch") == 0) {
  805. if (!video_switch())
  806. return -EIO;
  807. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  808. if (!video_expand())
  809. return -EIO;
  810. } else
  811. return -EINVAL;
  812. }
  813. if (enable || disable) {
  814. status = (video_status() & 0x0f & ~disable) | enable;
  815. if (!video_switch2(status))
  816. return -EIO;
  817. }
  818. return 0;
  819. }
  820. static void video_exit(void)
  821. {
  822. acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
  823. }
  824. static int light_supported;
  825. static int light_status_supported;
  826. static int light_init(void)
  827. {
  828. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  829. light_supported = (cmos_handle || lght_handle) && !ledb_handle;
  830. if (light_supported)
  831. /* light status not supported on
  832. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  833. light_status_supported = acpi_evalf(ec_handle, NULL,
  834. "KBLT", "qv");
  835. return 0;
  836. }
  837. static int light_read(char *p)
  838. {
  839. int len = 0;
  840. int status = 0;
  841. if (!light_supported) {
  842. len += sprintf(p + len, "status:\t\tnot supported\n");
  843. } else if (!light_status_supported) {
  844. len += sprintf(p + len, "status:\t\tunknown\n");
  845. len += sprintf(p + len, "commands:\ton, off\n");
  846. } else {
  847. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  848. return -EIO;
  849. len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
  850. len += sprintf(p + len, "commands:\ton, off\n");
  851. }
  852. return len;
  853. }
  854. static int light_write(char *buf)
  855. {
  856. int cmos_cmd, lght_cmd;
  857. char *cmd;
  858. int success;
  859. if (!light_supported)
  860. return -ENODEV;
  861. while ((cmd = next_cmd(&buf))) {
  862. if (strlencmp(cmd, "on") == 0) {
  863. cmos_cmd = 0x0c;
  864. lght_cmd = 1;
  865. } else if (strlencmp(cmd, "off") == 0) {
  866. cmos_cmd = 0x0d;
  867. lght_cmd = 0;
  868. } else
  869. return -EINVAL;
  870. success = cmos_handle ?
  871. acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
  872. acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
  873. if (!success)
  874. return -EIO;
  875. }
  876. return 0;
  877. }
  878. #if defined(CONFIG_ACPI_IBM_DOCK) || defined(CONFIG_ACPI_IBM_BAY)
  879. static int _sta(acpi_handle handle)
  880. {
  881. int status;
  882. if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
  883. status = 0;
  884. return status;
  885. }
  886. #endif
  887. #ifdef CONFIG_ACPI_IBM_DOCK
  888. #define dock_docked() (_sta(dock_handle) & 1)
  889. static int dock_read(char *p)
  890. {
  891. int len = 0;
  892. int docked = dock_docked();
  893. if (!dock_handle)
  894. len += sprintf(p + len, "status:\t\tnot supported\n");
  895. else if (!docked)
  896. len += sprintf(p + len, "status:\t\tundocked\n");
  897. else {
  898. len += sprintf(p + len, "status:\t\tdocked\n");
  899. len += sprintf(p + len, "commands:\tdock, undock\n");
  900. }
  901. return len;
  902. }
  903. static int dock_write(char *buf)
  904. {
  905. char *cmd;
  906. if (!dock_docked())
  907. return -ENODEV;
  908. while ((cmd = next_cmd(&buf))) {
  909. if (strlencmp(cmd, "undock") == 0) {
  910. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
  911. !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
  912. return -EIO;
  913. } else if (strlencmp(cmd, "dock") == 0) {
  914. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
  915. return -EIO;
  916. } else
  917. return -EINVAL;
  918. }
  919. return 0;
  920. }
  921. static void dock_notify(struct ibm_struct *ibm, u32 event)
  922. {
  923. int docked = dock_docked();
  924. int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
  925. if (event == 1 && !pci) /* 570 */
  926. acpi_bus_generate_event(ibm->device, event, 1); /* button */
  927. else if (event == 1 && pci) /* 570 */
  928. acpi_bus_generate_event(ibm->device, event, 3); /* dock */
  929. else if (event == 3 && docked)
  930. acpi_bus_generate_event(ibm->device, event, 1); /* button */
  931. else if (event == 3 && !docked)
  932. acpi_bus_generate_event(ibm->device, event, 2); /* undock */
  933. else if (event == 0 && docked)
  934. acpi_bus_generate_event(ibm->device, event, 3); /* dock */
  935. else {
  936. printk(IBM_ERR "unknown dock event %d, status %d\n",
  937. event, _sta(dock_handle));
  938. acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
  939. }
  940. }
  941. #endif
  942. #ifdef CONFIG_ACPI_IBM_BAY
  943. static int bay_status_supported;
  944. static int bay_status2_supported;
  945. static int bay_eject_supported;
  946. static int bay_eject2_supported;
  947. static int bay_init(void)
  948. {
  949. bay_status_supported = bay_handle &&
  950. acpi_evalf(bay_handle, NULL, "_STA", "qv");
  951. bay_status2_supported = bay2_handle &&
  952. acpi_evalf(bay2_handle, NULL, "_STA", "qv");
  953. bay_eject_supported = bay_handle && bay_ej_handle &&
  954. (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
  955. bay_eject2_supported = bay2_handle && bay2_ej_handle &&
  956. (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
  957. return 0;
  958. }
  959. #define bay_occupied(b) (_sta(b##_handle) & 1)
  960. static int bay_read(char *p)
  961. {
  962. int len = 0;
  963. int occupied = bay_occupied(bay);
  964. int occupied2 = bay_occupied(bay2);
  965. int eject, eject2;
  966. len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
  967. (occupied ? "occupied" : "unoccupied") :
  968. "not supported");
  969. if (bay_status2_supported)
  970. len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
  971. "occupied" : "unoccupied");
  972. eject = bay_eject_supported && occupied;
  973. eject2 = bay_eject2_supported && occupied2;
  974. if (eject && eject2)
  975. len += sprintf(p + len, "commands:\teject, eject2\n");
  976. else if (eject)
  977. len += sprintf(p + len, "commands:\teject\n");
  978. else if (eject2)
  979. len += sprintf(p + len, "commands:\teject2\n");
  980. return len;
  981. }
  982. static int bay_write(char *buf)
  983. {
  984. char *cmd;
  985. if (!bay_eject_supported && !bay_eject2_supported)
  986. return -ENODEV;
  987. while ((cmd = next_cmd(&buf))) {
  988. if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
  989. if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
  990. return -EIO;
  991. } else if (bay_eject2_supported &&
  992. strlencmp(cmd, "eject2") == 0) {
  993. if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
  994. return -EIO;
  995. } else
  996. return -EINVAL;
  997. }
  998. return 0;
  999. }
  1000. static void bay_notify(struct ibm_struct *ibm, u32 event)
  1001. {
  1002. acpi_bus_generate_event(ibm->device, event, 0);
  1003. }
  1004. #endif /* CONFIG_ACPI_IBM_BAY */
  1005. static int cmos_read(char *p)
  1006. {
  1007. int len = 0;
  1008. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  1009. R30, R31, T20-22, X20-21 */
  1010. if (!cmos_handle)
  1011. len += sprintf(p + len, "status:\t\tnot supported\n");
  1012. else {
  1013. len += sprintf(p + len, "status:\t\tsupported\n");
  1014. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
  1015. }
  1016. return len;
  1017. }
  1018. static int cmos_eval(int cmos_cmd)
  1019. {
  1020. if (cmos_handle)
  1021. return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
  1022. else
  1023. return 1;
  1024. }
  1025. static int cmos_write(char *buf)
  1026. {
  1027. char *cmd;
  1028. int cmos_cmd;
  1029. if (!cmos_handle)
  1030. return -EINVAL;
  1031. while ((cmd = next_cmd(&buf))) {
  1032. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  1033. cmos_cmd >= 0 && cmos_cmd <= 21) {
  1034. /* cmos_cmd set */
  1035. } else
  1036. return -EINVAL;
  1037. if (!cmos_eval(cmos_cmd))
  1038. return -EIO;
  1039. }
  1040. return 0;
  1041. }
  1042. enum led_access_mode {
  1043. IBMACPI_LED_NONE = 0,
  1044. IBMACPI_LED_570, /* 570 */
  1045. IBMACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1046. IBMACPI_LED_NEW, /* all others */
  1047. };
  1048. static enum led_access_mode led_supported;
  1049. static int led_init(void)
  1050. {
  1051. if (!led_handle)
  1052. /* led not supported on R30, R31 */
  1053. led_supported = IBMACPI_LED_NONE;
  1054. else if (strlencmp(led_path, "SLED") == 0)
  1055. /* 570 */
  1056. led_supported = IBMACPI_LED_570;
  1057. else if (strlencmp(led_path, "SYSL") == 0)
  1058. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1059. led_supported = IBMACPI_LED_OLD;
  1060. else
  1061. /* all others */
  1062. led_supported = IBMACPI_LED_NEW;
  1063. return 0;
  1064. }
  1065. #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
  1066. static int led_read(char *p)
  1067. {
  1068. int len = 0;
  1069. if (!led_supported) {
  1070. len += sprintf(p + len, "status:\t\tnot supported\n");
  1071. return len;
  1072. }
  1073. len += sprintf(p + len, "status:\t\tsupported\n");
  1074. if (led_supported == IBMACPI_LED_570) {
  1075. /* 570 */
  1076. int i, status;
  1077. for (i = 0; i < 8; i++) {
  1078. if (!acpi_evalf(ec_handle,
  1079. &status, "GLED", "dd", 1 << i))
  1080. return -EIO;
  1081. len += sprintf(p + len, "%d:\t\t%s\n",
  1082. i, led_status(status));
  1083. }
  1084. }
  1085. len += sprintf(p + len, "commands:\t"
  1086. "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
  1087. return len;
  1088. }
  1089. /* off, on, blink */
  1090. static const int led_sled_arg1[] = { 0, 1, 3 };
  1091. static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
  1092. static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
  1093. static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
  1094. #define EC_HLCL 0x0c
  1095. #define EC_HLBL 0x0d
  1096. #define EC_HLMS 0x0e
  1097. static int led_write(char *buf)
  1098. {
  1099. char *cmd;
  1100. int led, ind, ret;
  1101. if (!led_supported)
  1102. return -ENODEV;
  1103. while ((cmd = next_cmd(&buf))) {
  1104. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
  1105. return -EINVAL;
  1106. if (strstr(cmd, "off")) {
  1107. ind = 0;
  1108. } else if (strstr(cmd, "on")) {
  1109. ind = 1;
  1110. } else if (strstr(cmd, "blink")) {
  1111. ind = 2;
  1112. } else
  1113. return -EINVAL;
  1114. if (led_supported == IBMACPI_LED_570) {
  1115. /* 570 */
  1116. led = 1 << led;
  1117. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  1118. led, led_sled_arg1[ind]))
  1119. return -EIO;
  1120. } else if (led_supported == IBMACPI_LED_OLD) {
  1121. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  1122. led = 1 << led;
  1123. ret = ec_write(EC_HLMS, led);
  1124. if (ret >= 0)
  1125. ret =
  1126. ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
  1127. if (ret >= 0)
  1128. ret =
  1129. ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
  1130. if (ret < 0)
  1131. return ret;
  1132. } else {
  1133. /* all others */
  1134. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  1135. led, led_led_arg1[ind]))
  1136. return -EIO;
  1137. }
  1138. }
  1139. return 0;
  1140. }
  1141. static int beep_read(char *p)
  1142. {
  1143. int len = 0;
  1144. if (!beep_handle)
  1145. len += sprintf(p + len, "status:\t\tnot supported\n");
  1146. else {
  1147. len += sprintf(p + len, "status:\t\tsupported\n");
  1148. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  1149. }
  1150. return len;
  1151. }
  1152. static int beep_write(char *buf)
  1153. {
  1154. char *cmd;
  1155. int beep_cmd;
  1156. if (!beep_handle)
  1157. return -ENODEV;
  1158. while ((cmd = next_cmd(&buf))) {
  1159. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  1160. beep_cmd >= 0 && beep_cmd <= 17) {
  1161. /* beep_cmd set */
  1162. } else
  1163. return -EINVAL;
  1164. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
  1165. return -EIO;
  1166. }
  1167. return 0;
  1168. }
  1169. static int acpi_ec_read(int i, u8 * p)
  1170. {
  1171. int v;
  1172. if (ecrd_handle) {
  1173. if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
  1174. return 0;
  1175. *p = v;
  1176. } else {
  1177. if (ec_read(i, p) < 0)
  1178. return 0;
  1179. }
  1180. return 1;
  1181. }
  1182. static int acpi_ec_write(int i, u8 v)
  1183. {
  1184. if (ecwr_handle) {
  1185. if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
  1186. return 0;
  1187. } else {
  1188. if (ec_write(i, v) < 0)
  1189. return 0;
  1190. }
  1191. return 1;
  1192. }
  1193. static enum thermal_access_mode thermal_read_mode;
  1194. static int thermal_init(void)
  1195. {
  1196. u8 t, ta1, ta2;
  1197. int i;
  1198. int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  1199. if (ibm_thinkpad_ec_found && experimental) {
  1200. /*
  1201. * Direct EC access mode: sensors at registers
  1202. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  1203. * non-implemented, thermal sensors return 0x80 when
  1204. * not available
  1205. */
  1206. ta1 = ta2 = 0;
  1207. for (i = 0; i < 8; i++) {
  1208. if (likely(acpi_ec_read(0x78 + i, &t))) {
  1209. ta1 |= t;
  1210. } else {
  1211. ta1 = 0;
  1212. break;
  1213. }
  1214. if (likely(acpi_ec_read(0xC0 + i, &t))) {
  1215. ta2 |= t;
  1216. } else {
  1217. ta1 = 0;
  1218. break;
  1219. }
  1220. }
  1221. if (ta1 == 0) {
  1222. /* This is sheer paranoia, but we handle it anyway */
  1223. if (acpi_tmp7) {
  1224. printk(IBM_ERR
  1225. "ThinkPad ACPI EC access misbehaving, "
  1226. "falling back to ACPI TMPx access mode\n");
  1227. thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
  1228. } else {
  1229. printk(IBM_ERR
  1230. "ThinkPad ACPI EC access misbehaving, "
  1231. "disabling thermal sensors access\n");
  1232. thermal_read_mode = IBMACPI_THERMAL_NONE;
  1233. }
  1234. } else {
  1235. thermal_read_mode =
  1236. (ta2 != 0) ?
  1237. IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
  1238. }
  1239. } else if (acpi_tmp7) {
  1240. if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  1241. /* 600e/x, 770e, 770x */
  1242. thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
  1243. } else {
  1244. /* Standard ACPI TMPx access, max 8 sensors */
  1245. thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
  1246. }
  1247. } else {
  1248. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  1249. thermal_read_mode = IBMACPI_THERMAL_NONE;
  1250. }
  1251. return 0;
  1252. }
  1253. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  1254. {
  1255. int i, t;
  1256. s8 tmp;
  1257. char tmpi[] = "TMPi";
  1258. if (!s)
  1259. return -EINVAL;
  1260. switch (thermal_read_mode) {
  1261. #if IBMACPI_MAX_THERMAL_SENSORS >= 16
  1262. case IBMACPI_THERMAL_TPEC_16:
  1263. for (i = 0; i < 8; i++) {
  1264. if (!acpi_ec_read(0xC0 + i, &tmp))
  1265. return -EIO;
  1266. s->temp[i + 8] = tmp * 1000;
  1267. }
  1268. /* fallthrough */
  1269. #endif
  1270. case IBMACPI_THERMAL_TPEC_8:
  1271. for (i = 0; i < 8; i++) {
  1272. if (!acpi_ec_read(0x78 + i, &tmp))
  1273. return -EIO;
  1274. s->temp[i] = tmp * 1000;
  1275. }
  1276. return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
  1277. case IBMACPI_THERMAL_ACPI_UPDT:
  1278. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  1279. return -EIO;
  1280. for (i = 0; i < 8; i++) {
  1281. tmpi[3] = '0' + i;
  1282. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1283. return -EIO;
  1284. s->temp[i] = (t - 2732) * 100;
  1285. }
  1286. return 8;
  1287. case IBMACPI_THERMAL_ACPI_TMP07:
  1288. for (i = 0; i < 8; i++) {
  1289. tmpi[3] = '0' + i;
  1290. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1291. return -EIO;
  1292. s->temp[i] = t * 1000;
  1293. }
  1294. return 8;
  1295. case IBMACPI_THERMAL_NONE:
  1296. default:
  1297. return 0;
  1298. }
  1299. }
  1300. static int thermal_read(char *p)
  1301. {
  1302. int len = 0;
  1303. int n, i;
  1304. struct ibm_thermal_sensors_struct t;
  1305. n = thermal_get_sensors(&t);
  1306. if (unlikely(n < 0))
  1307. return n;
  1308. len += sprintf(p + len, "temperatures:\t");
  1309. if (n > 0) {
  1310. for (i = 0; i < (n - 1); i++)
  1311. len += sprintf(p + len, "%d ", t.temp[i] / 1000);
  1312. len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
  1313. } else
  1314. len += sprintf(p + len, "not supported\n");
  1315. return len;
  1316. }
  1317. static u8 ecdump_regs[256];
  1318. static int ecdump_read(char *p)
  1319. {
  1320. int len = 0;
  1321. int i, j;
  1322. u8 v;
  1323. len += sprintf(p + len, "EC "
  1324. " +00 +01 +02 +03 +04 +05 +06 +07"
  1325. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  1326. for (i = 0; i < 256; i += 16) {
  1327. len += sprintf(p + len, "EC 0x%02x:", i);
  1328. for (j = 0; j < 16; j++) {
  1329. if (!acpi_ec_read(i + j, &v))
  1330. break;
  1331. if (v != ecdump_regs[i + j])
  1332. len += sprintf(p + len, " *%02x", v);
  1333. else
  1334. len += sprintf(p + len, " %02x", v);
  1335. ecdump_regs[i + j] = v;
  1336. }
  1337. len += sprintf(p + len, "\n");
  1338. if (j != 16)
  1339. break;
  1340. }
  1341. /* These are way too dangerous to advertise openly... */
  1342. #if 0
  1343. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  1344. " (<offset> is 00-ff, <value> is 00-ff)\n");
  1345. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  1346. " (<offset> is 00-ff, <value> is 0-255)\n");
  1347. #endif
  1348. return len;
  1349. }
  1350. static int ecdump_write(char *buf)
  1351. {
  1352. char *cmd;
  1353. int i, v;
  1354. while ((cmd = next_cmd(&buf))) {
  1355. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  1356. /* i and v set */
  1357. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  1358. /* i and v set */
  1359. } else
  1360. return -EINVAL;
  1361. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  1362. if (!acpi_ec_write(i, v))
  1363. return -EIO;
  1364. } else
  1365. return -EINVAL;
  1366. }
  1367. return 0;
  1368. }
  1369. static int brightness_offset = 0x31;
  1370. static int brightness_get(struct backlight_device *bd)
  1371. {
  1372. u8 level;
  1373. if (!acpi_ec_read(brightness_offset, &level))
  1374. return -EIO;
  1375. level &= 0x7;
  1376. return level;
  1377. }
  1378. static int brightness_read(char *p)
  1379. {
  1380. int len = 0;
  1381. int level;
  1382. if ((level = brightness_get(NULL)) < 0) {
  1383. len += sprintf(p + len, "level:\t\tunreadable\n");
  1384. } else {
  1385. len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
  1386. len += sprintf(p + len, "commands:\tup, down\n");
  1387. len += sprintf(p + len, "commands:\tlevel <level>"
  1388. " (<level> is 0-7)\n");
  1389. }
  1390. return len;
  1391. }
  1392. #define BRIGHTNESS_UP 4
  1393. #define BRIGHTNESS_DOWN 5
  1394. static int brightness_set(int value)
  1395. {
  1396. int cmos_cmd, inc, i;
  1397. int current_value = brightness_get(NULL);
  1398. value &= 7;
  1399. cmos_cmd = value > current_value ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
  1400. inc = value > current_value ? 1 : -1;
  1401. for (i = current_value; i != value; i += inc) {
  1402. if (!cmos_eval(cmos_cmd))
  1403. return -EIO;
  1404. if (!acpi_ec_write(brightness_offset, i + inc))
  1405. return -EIO;
  1406. }
  1407. return 0;
  1408. }
  1409. static int brightness_write(char *buf)
  1410. {
  1411. int level;
  1412. int new_level;
  1413. char *cmd;
  1414. while ((cmd = next_cmd(&buf))) {
  1415. if ((level = brightness_get(NULL)) < 0)
  1416. return level;
  1417. level &= 7;
  1418. if (strlencmp(cmd, "up") == 0) {
  1419. new_level = level == 7 ? 7 : level + 1;
  1420. } else if (strlencmp(cmd, "down") == 0) {
  1421. new_level = level == 0 ? 0 : level - 1;
  1422. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  1423. new_level >= 0 && new_level <= 7) {
  1424. /* new_level set */
  1425. } else
  1426. return -EINVAL;
  1427. brightness_set(new_level);
  1428. }
  1429. return 0;
  1430. }
  1431. static int brightness_update_status(struct backlight_device *bd)
  1432. {
  1433. return brightness_set(bd->props.brightness);
  1434. }
  1435. static struct backlight_ops ibm_backlight_data = {
  1436. .get_brightness = brightness_get,
  1437. .update_status = brightness_update_status,
  1438. };
  1439. static int brightness_init(void)
  1440. {
  1441. int b;
  1442. b = brightness_get(NULL);
  1443. if (b < 0)
  1444. return b;
  1445. ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
  1446. &ibm_backlight_data);
  1447. if (IS_ERR(ibm_backlight_device)) {
  1448. printk(IBM_ERR "Could not register backlight device\n");
  1449. return PTR_ERR(ibm_backlight_device);
  1450. }
  1451. ibm_backlight_device->props.max_brightness = 7;
  1452. ibm_backlight_device->props.brightness = b;
  1453. backlight_update_status(ibm_backlight_device);
  1454. return 0;
  1455. }
  1456. static void brightness_exit(void)
  1457. {
  1458. if (ibm_backlight_device) {
  1459. backlight_device_unregister(ibm_backlight_device);
  1460. ibm_backlight_device = NULL;
  1461. }
  1462. }
  1463. static int volume_offset = 0x30;
  1464. static int volume_read(char *p)
  1465. {
  1466. int len = 0;
  1467. u8 level;
  1468. if (!acpi_ec_read(volume_offset, &level)) {
  1469. len += sprintf(p + len, "level:\t\tunreadable\n");
  1470. } else {
  1471. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  1472. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  1473. len += sprintf(p + len, "commands:\tup, down, mute\n");
  1474. len += sprintf(p + len, "commands:\tlevel <level>"
  1475. " (<level> is 0-15)\n");
  1476. }
  1477. return len;
  1478. }
  1479. #define VOLUME_DOWN 0
  1480. #define VOLUME_UP 1
  1481. #define VOLUME_MUTE 2
  1482. static int volume_write(char *buf)
  1483. {
  1484. int cmos_cmd, inc, i;
  1485. u8 level, mute;
  1486. int new_level, new_mute;
  1487. char *cmd;
  1488. while ((cmd = next_cmd(&buf))) {
  1489. if (!acpi_ec_read(volume_offset, &level))
  1490. return -EIO;
  1491. new_mute = mute = level & 0x40;
  1492. new_level = level = level & 0xf;
  1493. if (strlencmp(cmd, "up") == 0) {
  1494. if (mute)
  1495. new_mute = 0;
  1496. else
  1497. new_level = level == 15 ? 15 : level + 1;
  1498. } else if (strlencmp(cmd, "down") == 0) {
  1499. if (mute)
  1500. new_mute = 0;
  1501. else
  1502. new_level = level == 0 ? 0 : level - 1;
  1503. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  1504. new_level >= 0 && new_level <= 15) {
  1505. /* new_level set */
  1506. } else if (strlencmp(cmd, "mute") == 0) {
  1507. new_mute = 0x40;
  1508. } else
  1509. return -EINVAL;
  1510. if (new_level != level) { /* mute doesn't change */
  1511. cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
  1512. inc = new_level > level ? 1 : -1;
  1513. if (mute && (!cmos_eval(cmos_cmd) ||
  1514. !acpi_ec_write(volume_offset, level)))
  1515. return -EIO;
  1516. for (i = level; i != new_level; i += inc)
  1517. if (!cmos_eval(cmos_cmd) ||
  1518. !acpi_ec_write(volume_offset, i + inc))
  1519. return -EIO;
  1520. if (mute && (!cmos_eval(VOLUME_MUTE) ||
  1521. !acpi_ec_write(volume_offset,
  1522. new_level + mute)))
  1523. return -EIO;
  1524. }
  1525. if (new_mute != mute) { /* level doesn't change */
  1526. cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
  1527. if (!cmos_eval(cmos_cmd) ||
  1528. !acpi_ec_write(volume_offset, level + new_mute))
  1529. return -EIO;
  1530. }
  1531. }
  1532. return 0;
  1533. }
  1534. static enum fan_status_access_mode fan_status_access_mode;
  1535. static enum fan_control_access_mode fan_control_access_mode;
  1536. static enum fan_control_commands fan_control_commands;
  1537. static int fan_control_status_known;
  1538. static u8 fan_control_initial_status;
  1539. static void fan_watchdog_fire(struct work_struct *ignored);
  1540. static int fan_watchdog_maxinterval;
  1541. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  1542. static int fan_init(void)
  1543. {
  1544. fan_status_access_mode = IBMACPI_FAN_NONE;
  1545. fan_control_access_mode = IBMACPI_FAN_WR_NONE;
  1546. fan_control_commands = 0;
  1547. fan_control_status_known = 1;
  1548. fan_watchdog_maxinterval = 0;
  1549. if (gfan_handle) {
  1550. /* 570, 600e/x, 770e, 770x */
  1551. fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
  1552. } else {
  1553. /* all other ThinkPads: note that even old-style
  1554. * ThinkPad ECs supports the fan control register */
  1555. if (likely(acpi_ec_read(fan_status_offset,
  1556. &fan_control_initial_status))) {
  1557. fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
  1558. /* In some ThinkPads, neither the EC nor the ACPI
  1559. * DSDT initialize the fan status, and it ends up
  1560. * being set to 0x07 when it *could* be either
  1561. * 0x07 or 0x80.
  1562. *
  1563. * Enable for TP-1Y (T43), TP-78 (R51e),
  1564. * TP-76 (R52), TP-70 (T43, R52), which are known
  1565. * to be buggy. */
  1566. if (fan_control_initial_status == 0x07 &&
  1567. ibm_thinkpad_ec_found &&
  1568. ((ibm_thinkpad_ec_found[0] == '1' &&
  1569. ibm_thinkpad_ec_found[1] == 'Y') ||
  1570. (ibm_thinkpad_ec_found[0] == '7' &&
  1571. (ibm_thinkpad_ec_found[1] == '6' ||
  1572. ibm_thinkpad_ec_found[1] == '8' ||
  1573. ibm_thinkpad_ec_found[1] == '0'))
  1574. )) {
  1575. printk(IBM_NOTICE
  1576. "fan_init: initial fan status is "
  1577. "unknown, assuming it is in auto "
  1578. "mode\n");
  1579. fan_control_status_known = 0;
  1580. }
  1581. } else {
  1582. printk(IBM_ERR
  1583. "ThinkPad ACPI EC access misbehaving, "
  1584. "fan status and control unavailable\n");
  1585. return 0;
  1586. }
  1587. }
  1588. if (sfan_handle) {
  1589. /* 570, 770x-JL */
  1590. fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
  1591. fan_control_commands |=
  1592. IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
  1593. } else {
  1594. if (!gfan_handle) {
  1595. /* gfan without sfan means no fan control */
  1596. /* all other models implement TP EC 0x2f control */
  1597. if (fans_handle) {
  1598. /* X31, X40, X41 */
  1599. fan_control_access_mode =
  1600. IBMACPI_FAN_WR_ACPI_FANS;
  1601. fan_control_commands |=
  1602. IBMACPI_FAN_CMD_SPEED |
  1603. IBMACPI_FAN_CMD_LEVEL |
  1604. IBMACPI_FAN_CMD_ENABLE;
  1605. } else {
  1606. fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
  1607. fan_control_commands |=
  1608. IBMACPI_FAN_CMD_LEVEL |
  1609. IBMACPI_FAN_CMD_ENABLE;
  1610. }
  1611. }
  1612. }
  1613. return 0;
  1614. }
  1615. static int fan_get_status(u8 *status)
  1616. {
  1617. u8 s;
  1618. /* TODO:
  1619. * Add IBMACPI_FAN_RD_ACPI_FANS ? */
  1620. switch (fan_status_access_mode) {
  1621. case IBMACPI_FAN_RD_ACPI_GFAN:
  1622. /* 570, 600e/x, 770e, 770x */
  1623. if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
  1624. return -EIO;
  1625. if (likely(status))
  1626. *status = s & 0x07;
  1627. break;
  1628. case IBMACPI_FAN_RD_TPEC:
  1629. /* all except 570, 600e/x, 770e, 770x */
  1630. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  1631. return -EIO;
  1632. if (likely(status))
  1633. *status = s;
  1634. break;
  1635. default:
  1636. return -ENXIO;
  1637. }
  1638. return 0;
  1639. }
  1640. static int fan_get_speed(unsigned int *speed)
  1641. {
  1642. u8 hi, lo;
  1643. switch (fan_status_access_mode) {
  1644. case IBMACPI_FAN_RD_TPEC:
  1645. /* all except 570, 600e/x, 770e, 770x */
  1646. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  1647. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  1648. return -EIO;
  1649. if (likely(speed))
  1650. *speed = (hi << 8) | lo;
  1651. break;
  1652. default:
  1653. return -ENXIO;
  1654. }
  1655. return 0;
  1656. }
  1657. static void fan_exit(void)
  1658. {
  1659. cancel_delayed_work(&fan_watchdog_task);
  1660. flush_scheduled_work();
  1661. }
  1662. static void fan_watchdog_reset(void)
  1663. {
  1664. static int fan_watchdog_active = 0;
  1665. if (fan_watchdog_active)
  1666. cancel_delayed_work(&fan_watchdog_task);
  1667. if (fan_watchdog_maxinterval > 0) {
  1668. fan_watchdog_active = 1;
  1669. if (!schedule_delayed_work(&fan_watchdog_task,
  1670. msecs_to_jiffies(fan_watchdog_maxinterval
  1671. * 1000))) {
  1672. printk(IBM_ERR "failed to schedule the fan watchdog, "
  1673. "watchdog will not trigger\n");
  1674. }
  1675. } else
  1676. fan_watchdog_active = 0;
  1677. }
  1678. static int fan_read(char *p)
  1679. {
  1680. int len = 0;
  1681. int rc;
  1682. u8 status;
  1683. unsigned int speed = 0;
  1684. switch (fan_status_access_mode) {
  1685. case IBMACPI_FAN_RD_ACPI_GFAN:
  1686. /* 570, 600e/x, 770e, 770x */
  1687. if ((rc = fan_get_status(&status)) < 0)
  1688. return rc;
  1689. len += sprintf(p + len, "status:\t\t%s\n"
  1690. "level:\t\t%d\n",
  1691. (status != 0) ? "enabled" : "disabled", status);
  1692. break;
  1693. case IBMACPI_FAN_RD_TPEC:
  1694. /* all except 570, 600e/x, 770e, 770x */
  1695. if ((rc = fan_get_status(&status)) < 0)
  1696. return rc;
  1697. if (unlikely(!fan_control_status_known)) {
  1698. if (status != fan_control_initial_status)
  1699. fan_control_status_known = 1;
  1700. else
  1701. /* Return most likely status. In fact, it
  1702. * might be the only possible status */
  1703. status = IBMACPI_FAN_EC_AUTO;
  1704. }
  1705. len += sprintf(p + len, "status:\t\t%s\n",
  1706. (status != 0) ? "enabled" : "disabled");
  1707. /* No ThinkPad boots on disengaged mode, we can safely
  1708. * assume the tachometer is online if fan control status
  1709. * was unknown */
  1710. if ((rc = fan_get_speed(&speed)) < 0)
  1711. return rc;
  1712. len += sprintf(p + len, "speed:\t\t%d\n", speed);
  1713. if (status & IBMACPI_FAN_EC_DISENGAGED)
  1714. /* Disengaged mode takes precedence */
  1715. len += sprintf(p + len, "level:\t\tdisengaged\n");
  1716. else if (status & IBMACPI_FAN_EC_AUTO)
  1717. len += sprintf(p + len, "level:\t\tauto\n");
  1718. else
  1719. len += sprintf(p + len, "level:\t\t%d\n", status);
  1720. break;
  1721. case IBMACPI_FAN_NONE:
  1722. default:
  1723. len += sprintf(p + len, "status:\t\tnot supported\n");
  1724. }
  1725. if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
  1726. len += sprintf(p + len, "commands:\tlevel <level>");
  1727. switch (fan_control_access_mode) {
  1728. case IBMACPI_FAN_WR_ACPI_SFAN:
  1729. len += sprintf(p + len, " (<level> is 0-7)\n");
  1730. break;
  1731. default:
  1732. len += sprintf(p + len, " (<level> is 0-7, "
  1733. "auto, disengaged)\n");
  1734. break;
  1735. }
  1736. }
  1737. if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
  1738. len += sprintf(p + len, "commands:\tenable, disable\n"
  1739. "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
  1740. "1-120 (seconds))\n");
  1741. if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
  1742. len += sprintf(p + len, "commands:\tspeed <speed>"
  1743. " (<speed> is 0-65535)\n");
  1744. return len;
  1745. }
  1746. static int fan_set_level(int level)
  1747. {
  1748. switch (fan_control_access_mode) {
  1749. case IBMACPI_FAN_WR_ACPI_SFAN:
  1750. if (level >= 0 && level <= 7) {
  1751. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  1752. return -EIO;
  1753. } else
  1754. return -EINVAL;
  1755. break;
  1756. case IBMACPI_FAN_WR_ACPI_FANS:
  1757. case IBMACPI_FAN_WR_TPEC:
  1758. if ((level != IBMACPI_FAN_EC_AUTO) &&
  1759. (level != IBMACPI_FAN_EC_DISENGAGED) &&
  1760. ((level < 0) || (level > 7)))
  1761. return -EINVAL;
  1762. if (!acpi_ec_write(fan_status_offset, level))
  1763. return -EIO;
  1764. else
  1765. fan_control_status_known = 1;
  1766. break;
  1767. default:
  1768. return -ENXIO;
  1769. }
  1770. return 0;
  1771. }
  1772. static int fan_set_enable(void)
  1773. {
  1774. u8 s;
  1775. int rc;
  1776. switch (fan_control_access_mode) {
  1777. case IBMACPI_FAN_WR_ACPI_FANS:
  1778. case IBMACPI_FAN_WR_TPEC:
  1779. if ((rc = fan_get_status(&s)) < 0)
  1780. return rc;
  1781. /* Don't go out of emergency fan mode */
  1782. if (s != 7)
  1783. s = IBMACPI_FAN_EC_AUTO;
  1784. if (!acpi_ec_write(fan_status_offset, s))
  1785. return -EIO;
  1786. else
  1787. fan_control_status_known = 1;
  1788. break;
  1789. case IBMACPI_FAN_WR_ACPI_SFAN:
  1790. if ((rc = fan_get_status(&s)) < 0)
  1791. return rc;
  1792. s &= 0x07;
  1793. /* Set fan to at least level 4 */
  1794. if (s < 4)
  1795. s = 4;
  1796. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  1797. return -EIO;
  1798. break;
  1799. default:
  1800. return -ENXIO;
  1801. }
  1802. return 0;
  1803. }
  1804. static int fan_set_disable(void)
  1805. {
  1806. switch (fan_control_access_mode) {
  1807. case IBMACPI_FAN_WR_ACPI_FANS:
  1808. case IBMACPI_FAN_WR_TPEC:
  1809. if (!acpi_ec_write(fan_status_offset, 0x00))
  1810. return -EIO;
  1811. else
  1812. fan_control_status_known = 1;
  1813. break;
  1814. case IBMACPI_FAN_WR_ACPI_SFAN:
  1815. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  1816. return -EIO;
  1817. break;
  1818. default:
  1819. return -ENXIO;
  1820. }
  1821. return 0;
  1822. }
  1823. static int fan_set_speed(int speed)
  1824. {
  1825. switch (fan_control_access_mode) {
  1826. case IBMACPI_FAN_WR_ACPI_FANS:
  1827. if (speed >= 0 && speed <= 65535) {
  1828. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  1829. speed, speed, speed))
  1830. return -EIO;
  1831. } else
  1832. return -EINVAL;
  1833. break;
  1834. default:
  1835. return -ENXIO;
  1836. }
  1837. return 0;
  1838. }
  1839. static int fan_write_cmd_level(const char *cmd, int *rc)
  1840. {
  1841. int level;
  1842. if (strlencmp(cmd, "level auto") == 0)
  1843. level = IBMACPI_FAN_EC_AUTO;
  1844. else if (strlencmp(cmd, "level disengaged") == 0)
  1845. level = IBMACPI_FAN_EC_DISENGAGED;
  1846. else if (sscanf(cmd, "level %d", &level) != 1)
  1847. return 0;
  1848. if ((*rc = fan_set_level(level)) == -ENXIO)
  1849. printk(IBM_ERR "level command accepted for unsupported "
  1850. "access mode %d", fan_control_access_mode);
  1851. return 1;
  1852. }
  1853. static int fan_write_cmd_enable(const char *cmd, int *rc)
  1854. {
  1855. if (strlencmp(cmd, "enable") != 0)
  1856. return 0;
  1857. if ((*rc = fan_set_enable()) == -ENXIO)
  1858. printk(IBM_ERR "enable command accepted for unsupported "
  1859. "access mode %d", fan_control_access_mode);
  1860. return 1;
  1861. }
  1862. static int fan_write_cmd_disable(const char *cmd, int *rc)
  1863. {
  1864. if (strlencmp(cmd, "disable") != 0)
  1865. return 0;
  1866. if ((*rc = fan_set_disable()) == -ENXIO)
  1867. printk(IBM_ERR "disable command accepted for unsupported "
  1868. "access mode %d", fan_control_access_mode);
  1869. return 1;
  1870. }
  1871. static int fan_write_cmd_speed(const char *cmd, int *rc)
  1872. {
  1873. int speed;
  1874. /* TODO:
  1875. * Support speed <low> <medium> <high> ? */
  1876. if (sscanf(cmd, "speed %d", &speed) != 1)
  1877. return 0;
  1878. if ((*rc = fan_set_speed(speed)) == -ENXIO)
  1879. printk(IBM_ERR "speed command accepted for unsupported "
  1880. "access mode %d", fan_control_access_mode);
  1881. return 1;
  1882. }
  1883. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  1884. {
  1885. int interval;
  1886. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  1887. return 0;
  1888. if (interval < 0 || interval > 120)
  1889. *rc = -EINVAL;
  1890. else
  1891. fan_watchdog_maxinterval = interval;
  1892. return 1;
  1893. }
  1894. static int fan_write(char *buf)
  1895. {
  1896. char *cmd;
  1897. int rc = 0;
  1898. while (!rc && (cmd = next_cmd(&buf))) {
  1899. if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
  1900. fan_write_cmd_level(cmd, &rc)) &&
  1901. !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
  1902. (fan_write_cmd_enable(cmd, &rc) ||
  1903. fan_write_cmd_disable(cmd, &rc) ||
  1904. fan_write_cmd_watchdog(cmd, &rc))) &&
  1905. !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
  1906. fan_write_cmd_speed(cmd, &rc))
  1907. )
  1908. rc = -EINVAL;
  1909. else if (!rc)
  1910. fan_watchdog_reset();
  1911. }
  1912. return rc;
  1913. }
  1914. static void fan_watchdog_fire(struct work_struct *ignored)
  1915. {
  1916. printk(IBM_NOTICE "fan watchdog: enabling fan\n");
  1917. if (fan_set_enable()) {
  1918. printk(IBM_ERR "fan watchdog: error while enabling fan\n");
  1919. /* reschedule for later */
  1920. fan_watchdog_reset();
  1921. }
  1922. }
  1923. static struct ibm_struct ibms[] = {
  1924. {
  1925. .name = "driver",
  1926. .init = ibm_acpi_driver_init,
  1927. .read = driver_read,
  1928. },
  1929. {
  1930. .name = "hotkey",
  1931. .hid = IBM_HKEY_HID,
  1932. .init = hotkey_init,
  1933. .read = hotkey_read,
  1934. .write = hotkey_write,
  1935. .exit = hotkey_exit,
  1936. .notify = hotkey_notify,
  1937. .handle = &hkey_handle,
  1938. .type = ACPI_DEVICE_NOTIFY,
  1939. },
  1940. {
  1941. .name = "bluetooth",
  1942. .init = bluetooth_init,
  1943. .read = bluetooth_read,
  1944. .write = bluetooth_write,
  1945. },
  1946. {
  1947. .name = "wan",
  1948. .init = wan_init,
  1949. .read = wan_read,
  1950. .write = wan_write,
  1951. .experimental = 1,
  1952. },
  1953. {
  1954. .name = "video",
  1955. .init = video_init,
  1956. .read = video_read,
  1957. .write = video_write,
  1958. .exit = video_exit,
  1959. },
  1960. {
  1961. .name = "light",
  1962. .init = light_init,
  1963. .read = light_read,
  1964. .write = light_write,
  1965. },
  1966. #ifdef CONFIG_ACPI_IBM_DOCK
  1967. {
  1968. .name = "dock",
  1969. .read = dock_read,
  1970. .write = dock_write,
  1971. .notify = dock_notify,
  1972. .handle = &dock_handle,
  1973. .type = ACPI_SYSTEM_NOTIFY,
  1974. },
  1975. {
  1976. .name = "dock",
  1977. .hid = IBM_PCI_HID,
  1978. .notify = dock_notify,
  1979. .handle = &pci_handle,
  1980. .type = ACPI_SYSTEM_NOTIFY,
  1981. },
  1982. #endif
  1983. #ifdef CONFIG_ACPI_IBM_BAY
  1984. {
  1985. .name = "bay",
  1986. .init = bay_init,
  1987. .read = bay_read,
  1988. .write = bay_write,
  1989. .notify = bay_notify,
  1990. .handle = &bay_handle,
  1991. .type = ACPI_SYSTEM_NOTIFY,
  1992. },
  1993. #endif /* CONFIG_ACPI_IBM_BAY */
  1994. {
  1995. .name = "cmos",
  1996. .read = cmos_read,
  1997. .write = cmos_write,
  1998. },
  1999. {
  2000. .name = "led",
  2001. .init = led_init,
  2002. .read = led_read,
  2003. .write = led_write,
  2004. },
  2005. {
  2006. .name = "beep",
  2007. .read = beep_read,
  2008. .write = beep_write,
  2009. },
  2010. {
  2011. .name = "thermal",
  2012. .init = thermal_init,
  2013. .read = thermal_read,
  2014. },
  2015. {
  2016. .name = "ecdump",
  2017. .read = ecdump_read,
  2018. .write = ecdump_write,
  2019. .experimental = 1,
  2020. },
  2021. {
  2022. .name = "brightness",
  2023. .read = brightness_read,
  2024. .write = brightness_write,
  2025. .init = brightness_init,
  2026. .exit = brightness_exit,
  2027. },
  2028. {
  2029. .name = "volume",
  2030. .read = volume_read,
  2031. .write = volume_write,
  2032. },
  2033. {
  2034. .name = "fan",
  2035. .read = fan_read,
  2036. .write = fan_write,
  2037. .init = fan_init,
  2038. .exit = fan_exit,
  2039. .experimental = 1,
  2040. },
  2041. };
  2042. static int dispatch_read(char *page, char **start, off_t off, int count,
  2043. int *eof, void *data)
  2044. {
  2045. struct ibm_struct *ibm = data;
  2046. int len;
  2047. if (!ibm || !ibm->read)
  2048. return -EINVAL;
  2049. len = ibm->read(page);
  2050. if (len < 0)
  2051. return len;
  2052. if (len <= off + count)
  2053. *eof = 1;
  2054. *start = page + off;
  2055. len -= off;
  2056. if (len > count)
  2057. len = count;
  2058. if (len < 0)
  2059. len = 0;
  2060. return len;
  2061. }
  2062. static int dispatch_write(struct file *file, const char __user * userbuf,
  2063. unsigned long count, void *data)
  2064. {
  2065. struct ibm_struct *ibm = data;
  2066. char *kernbuf;
  2067. int ret;
  2068. if (!ibm || !ibm->write)
  2069. return -EINVAL;
  2070. kernbuf = kmalloc(count + 2, GFP_KERNEL);
  2071. if (!kernbuf)
  2072. return -ENOMEM;
  2073. if (copy_from_user(kernbuf, userbuf, count)) {
  2074. kfree(kernbuf);
  2075. return -EFAULT;
  2076. }
  2077. kernbuf[count] = 0;
  2078. strcat(kernbuf, ",");
  2079. ret = ibm->write(kernbuf);
  2080. if (ret == 0)
  2081. ret = count;
  2082. kfree(kernbuf);
  2083. return ret;
  2084. }
  2085. static void dispatch_notify(acpi_handle handle, u32 event, void *data)
  2086. {
  2087. struct ibm_struct *ibm = data;
  2088. if (!ibm || !ibm->notify)
  2089. return;
  2090. ibm->notify(ibm, event);
  2091. }
  2092. static int __init setup_notify(struct ibm_struct *ibm)
  2093. {
  2094. acpi_status status;
  2095. int ret;
  2096. if (!*ibm->handle)
  2097. return 0;
  2098. ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
  2099. if (ret < 0) {
  2100. printk(IBM_ERR "%s device not present\n", ibm->name);
  2101. return 0;
  2102. }
  2103. acpi_driver_data(ibm->device) = ibm;
  2104. sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
  2105. status = acpi_install_notify_handler(*ibm->handle, ibm->type,
  2106. dispatch_notify, ibm);
  2107. if (ACPI_FAILURE(status)) {
  2108. printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
  2109. ibm->name, status);
  2110. return -ENODEV;
  2111. }
  2112. ibm->notify_installed = 1;
  2113. return 0;
  2114. }
  2115. static int __init ibm_device_add(struct acpi_device *device)
  2116. {
  2117. return 0;
  2118. }
  2119. static int __init register_driver(struct ibm_struct *ibm)
  2120. {
  2121. int ret;
  2122. ibm->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
  2123. if (!ibm->driver) {
  2124. printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
  2125. return -1;
  2126. }
  2127. sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
  2128. ibm->driver->ids = ibm->hid;
  2129. ibm->driver->ops.add = &ibm_device_add;
  2130. ret = acpi_bus_register_driver(ibm->driver);
  2131. if (ret < 0) {
  2132. printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
  2133. ibm->hid, ret);
  2134. kfree(ibm->driver);
  2135. }
  2136. return ret;
  2137. }
  2138. static int __init ibm_init(struct ibm_struct *ibm)
  2139. {
  2140. int ret;
  2141. struct proc_dir_entry *entry;
  2142. if (ibm->experimental && !experimental)
  2143. return 0;
  2144. if (ibm->hid) {
  2145. ret = register_driver(ibm);
  2146. if (ret < 0)
  2147. return ret;
  2148. ibm->driver_registered = 1;
  2149. }
  2150. if (ibm->init) {
  2151. ret = ibm->init();
  2152. if (ret != 0)
  2153. return ret;
  2154. ibm->init_called = 1;
  2155. }
  2156. if (ibm->read) {
  2157. entry = create_proc_entry(ibm->name,
  2158. S_IFREG | S_IRUGO | S_IWUSR,
  2159. proc_dir);
  2160. if (!entry) {
  2161. printk(IBM_ERR "unable to create proc entry %s\n",
  2162. ibm->name);
  2163. return -ENODEV;
  2164. }
  2165. entry->owner = THIS_MODULE;
  2166. entry->data = ibm;
  2167. entry->read_proc = &dispatch_read;
  2168. if (ibm->write)
  2169. entry->write_proc = &dispatch_write;
  2170. ibm->proc_created = 1;
  2171. }
  2172. if (ibm->notify) {
  2173. ret = setup_notify(ibm);
  2174. if (ret < 0)
  2175. return ret;
  2176. }
  2177. return 0;
  2178. }
  2179. static void ibm_exit(struct ibm_struct *ibm)
  2180. {
  2181. if (ibm->notify_installed)
  2182. acpi_remove_notify_handler(*ibm->handle, ibm->type,
  2183. dispatch_notify);
  2184. if (ibm->proc_created)
  2185. remove_proc_entry(ibm->name, proc_dir);
  2186. if (ibm->init_called && ibm->exit)
  2187. ibm->exit();
  2188. if (ibm->driver_registered) {
  2189. acpi_bus_unregister_driver(ibm->driver);
  2190. kfree(ibm->driver);
  2191. }
  2192. }
  2193. static void __init ibm_handle_init(char *name,
  2194. acpi_handle * handle, acpi_handle parent,
  2195. char **paths, int num_paths, char **path)
  2196. {
  2197. int i;
  2198. acpi_status status;
  2199. for (i = 0; i < num_paths; i++) {
  2200. status = acpi_get_handle(parent, paths[i], handle);
  2201. if (ACPI_SUCCESS(status)) {
  2202. *path = paths[i];
  2203. return;
  2204. }
  2205. }
  2206. *handle = NULL;
  2207. }
  2208. #define IBM_HANDLE_INIT(object) \
  2209. ibm_handle_init(#object, &object##_handle, *object##_parent, \
  2210. object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
  2211. static int __init set_ibm_param(const char *val, struct kernel_param *kp)
  2212. {
  2213. unsigned int i;
  2214. for (i = 0; i < ARRAY_SIZE(ibms); i++)
  2215. if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
  2216. if (strlen(val) > sizeof(ibms[i].param) - 2)
  2217. return -ENOSPC;
  2218. strcpy(ibms[i].param, val);
  2219. strcat(ibms[i].param, ",");
  2220. return 0;
  2221. }
  2222. return -EINVAL;
  2223. }
  2224. #define IBM_PARAM(feature) \
  2225. module_param_call(feature, set_ibm_param, NULL, NULL, 0)
  2226. IBM_PARAM(hotkey);
  2227. IBM_PARAM(bluetooth);
  2228. IBM_PARAM(video);
  2229. IBM_PARAM(light);
  2230. #ifdef CONFIG_ACPI_IBM_DOCK
  2231. IBM_PARAM(dock);
  2232. #endif
  2233. #ifdef CONFIG_ACPI_IBM_BAY
  2234. IBM_PARAM(bay);
  2235. #endif /* CONFIG_ACPI_IBM_BAY */
  2236. IBM_PARAM(cmos);
  2237. IBM_PARAM(led);
  2238. IBM_PARAM(beep);
  2239. IBM_PARAM(ecdump);
  2240. IBM_PARAM(brightness);
  2241. IBM_PARAM(volume);
  2242. IBM_PARAM(fan);
  2243. static void acpi_ibm_exit(void)
  2244. {
  2245. int i;
  2246. for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
  2247. ibm_exit(&ibms[i]);
  2248. if (proc_dir)
  2249. remove_proc_entry(IBM_DIR, acpi_root_dir);
  2250. if (ibm_thinkpad_ec_found)
  2251. kfree(ibm_thinkpad_ec_found);
  2252. }
  2253. static char* __init check_dmi_for_ec(void)
  2254. {
  2255. struct dmi_device *dev = NULL;
  2256. char ec_fw_string[18];
  2257. /*
  2258. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  2259. * X32 or newer, all Z series; Some models must have an
  2260. * up-to-date BIOS or they will not be detected.
  2261. *
  2262. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  2263. */
  2264. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  2265. if (sscanf(dev->name,
  2266. "IBM ThinkPad Embedded Controller -[%17c",
  2267. ec_fw_string) == 1) {
  2268. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  2269. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  2270. return kstrdup(ec_fw_string, GFP_KERNEL);
  2271. }
  2272. }
  2273. return NULL;
  2274. }
  2275. static int __init acpi_ibm_init(void)
  2276. {
  2277. int ret, i;
  2278. if (acpi_disabled)
  2279. return -ENODEV;
  2280. /* ec is required because many other handles are relative to it */
  2281. IBM_HANDLE_INIT(ec);
  2282. if (!ec_handle) {
  2283. printk(IBM_ERR "ec object not found\n");
  2284. return -ENODEV;
  2285. }
  2286. /* Models with newer firmware report the EC in DMI */
  2287. ibm_thinkpad_ec_found = check_dmi_for_ec();
  2288. /* these handles are not required */
  2289. IBM_HANDLE_INIT(vid);
  2290. IBM_HANDLE_INIT(vid2);
  2291. IBM_HANDLE_INIT(ledb);
  2292. IBM_HANDLE_INIT(led);
  2293. IBM_HANDLE_INIT(hkey);
  2294. IBM_HANDLE_INIT(lght);
  2295. IBM_HANDLE_INIT(cmos);
  2296. #ifdef CONFIG_ACPI_IBM_DOCK
  2297. IBM_HANDLE_INIT(dock);
  2298. #endif
  2299. IBM_HANDLE_INIT(pci);
  2300. #ifdef CONFIG_ACPI_IBM_BAY
  2301. IBM_HANDLE_INIT(bay);
  2302. if (bay_handle)
  2303. IBM_HANDLE_INIT(bay_ej);
  2304. IBM_HANDLE_INIT(bay2);
  2305. if (bay2_handle)
  2306. IBM_HANDLE_INIT(bay2_ej);
  2307. #endif /* CONFIG_ACPI_IBM_BAY */
  2308. IBM_HANDLE_INIT(beep);
  2309. IBM_HANDLE_INIT(ecrd);
  2310. IBM_HANDLE_INIT(ecwr);
  2311. IBM_HANDLE_INIT(fans);
  2312. IBM_HANDLE_INIT(gfan);
  2313. IBM_HANDLE_INIT(sfan);
  2314. proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
  2315. if (!proc_dir) {
  2316. printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
  2317. acpi_ibm_exit();
  2318. return -ENODEV;
  2319. }
  2320. proc_dir->owner = THIS_MODULE;
  2321. for (i = 0; i < ARRAY_SIZE(ibms); i++) {
  2322. ret = ibm_init(&ibms[i]);
  2323. if (ret >= 0 && *ibms[i].param)
  2324. ret = ibms[i].write(ibms[i].param);
  2325. if (ret < 0) {
  2326. acpi_ibm_exit();
  2327. return ret;
  2328. }
  2329. }
  2330. return 0;
  2331. }
  2332. module_init(acpi_ibm_init);
  2333. module_exit(acpi_ibm_exit);