ibm_acpi.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774
  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(struct work_struct *ignored);
  1532. static int fan_watchdog_maxinterval;
  1533. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  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(struct work_struct *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 = 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 = 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 = 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 = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
  2115. if (!ibm->driver) {
  2116. printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
  2117. return -1;
  2118. }
  2119. sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
  2120. ibm->driver->ids = ibm->hid;
  2121. ibm->driver->ops.add = &ibm_device_add;
  2122. ret = acpi_bus_register_driver(ibm->driver);
  2123. if (ret < 0) {
  2124. printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
  2125. ibm->hid, ret);
  2126. kfree(ibm->driver);
  2127. }
  2128. return ret;
  2129. }
  2130. static int __init ibm_init(struct ibm_struct *ibm)
  2131. {
  2132. int ret;
  2133. struct proc_dir_entry *entry;
  2134. if (ibm->experimental && !experimental)
  2135. return 0;
  2136. if (ibm->hid) {
  2137. ret = register_driver(ibm);
  2138. if (ret < 0)
  2139. return ret;
  2140. ibm->driver_registered = 1;
  2141. }
  2142. if (ibm->init) {
  2143. ret = ibm->init();
  2144. if (ret != 0)
  2145. return ret;
  2146. ibm->init_called = 1;
  2147. }
  2148. if (ibm->read) {
  2149. entry = create_proc_entry(ibm->name,
  2150. S_IFREG | S_IRUGO | S_IWUSR,
  2151. proc_dir);
  2152. if (!entry) {
  2153. printk(IBM_ERR "unable to create proc entry %s\n",
  2154. ibm->name);
  2155. return -ENODEV;
  2156. }
  2157. entry->owner = THIS_MODULE;
  2158. entry->data = ibm;
  2159. entry->read_proc = &dispatch_read;
  2160. if (ibm->write)
  2161. entry->write_proc = &dispatch_write;
  2162. ibm->proc_created = 1;
  2163. }
  2164. if (ibm->notify) {
  2165. ret = setup_notify(ibm);
  2166. if (ret < 0)
  2167. return ret;
  2168. }
  2169. return 0;
  2170. }
  2171. static void ibm_exit(struct ibm_struct *ibm)
  2172. {
  2173. if (ibm->notify_installed)
  2174. acpi_remove_notify_handler(*ibm->handle, ibm->type,
  2175. dispatch_notify);
  2176. if (ibm->proc_created)
  2177. remove_proc_entry(ibm->name, proc_dir);
  2178. if (ibm->init_called && ibm->exit)
  2179. ibm->exit();
  2180. if (ibm->driver_registered) {
  2181. acpi_bus_unregister_driver(ibm->driver);
  2182. kfree(ibm->driver);
  2183. }
  2184. }
  2185. static void __init ibm_handle_init(char *name,
  2186. acpi_handle * handle, acpi_handle parent,
  2187. char **paths, int num_paths, char **path)
  2188. {
  2189. int i;
  2190. acpi_status status;
  2191. for (i = 0; i < num_paths; i++) {
  2192. status = acpi_get_handle(parent, paths[i], handle);
  2193. if (ACPI_SUCCESS(status)) {
  2194. *path = paths[i];
  2195. return;
  2196. }
  2197. }
  2198. *handle = NULL;
  2199. }
  2200. #define IBM_HANDLE_INIT(object) \
  2201. ibm_handle_init(#object, &object##_handle, *object##_parent, \
  2202. object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
  2203. static int set_ibm_param(const char *val, struct kernel_param *kp)
  2204. {
  2205. unsigned int i;
  2206. for (i = 0; i < ARRAY_SIZE(ibms); i++)
  2207. if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
  2208. if (strlen(val) > sizeof(ibms[i].param) - 2)
  2209. return -ENOSPC;
  2210. strcpy(ibms[i].param, val);
  2211. strcat(ibms[i].param, ",");
  2212. return 0;
  2213. }
  2214. return -EINVAL;
  2215. }
  2216. #define IBM_PARAM(feature) \
  2217. module_param_call(feature, set_ibm_param, NULL, NULL, 0)
  2218. IBM_PARAM(hotkey);
  2219. IBM_PARAM(bluetooth);
  2220. IBM_PARAM(video);
  2221. IBM_PARAM(light);
  2222. #ifdef CONFIG_ACPI_IBM_DOCK
  2223. IBM_PARAM(dock);
  2224. #endif
  2225. #ifdef CONFIG_ACPI_IBM_BAY
  2226. IBM_PARAM(bay);
  2227. #endif
  2228. IBM_PARAM(cmos);
  2229. IBM_PARAM(led);
  2230. IBM_PARAM(beep);
  2231. IBM_PARAM(ecdump);
  2232. IBM_PARAM(brightness);
  2233. IBM_PARAM(volume);
  2234. IBM_PARAM(fan);
  2235. static void acpi_ibm_exit(void)
  2236. {
  2237. int i;
  2238. for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
  2239. ibm_exit(&ibms[i]);
  2240. remove_proc_entry(IBM_DIR, acpi_root_dir);
  2241. if (ibm_thinkpad_ec_found)
  2242. kfree(ibm_thinkpad_ec_found);
  2243. }
  2244. static char* __init check_dmi_for_ec(void)
  2245. {
  2246. struct dmi_device *dev = NULL;
  2247. char ec_fw_string[18];
  2248. /*
  2249. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  2250. * X32 or newer, all Z series; Some models must have an
  2251. * up-to-date BIOS or they will not be detected.
  2252. *
  2253. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  2254. */
  2255. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  2256. if (sscanf(dev->name,
  2257. "IBM ThinkPad Embedded Controller -[%17c",
  2258. ec_fw_string) == 1) {
  2259. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  2260. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  2261. return kstrdup(ec_fw_string, GFP_KERNEL);
  2262. }
  2263. }
  2264. return NULL;
  2265. }
  2266. static int __init acpi_ibm_init(void)
  2267. {
  2268. int ret, i;
  2269. if (acpi_disabled)
  2270. return -ENODEV;
  2271. if (!acpi_specific_hotkey_enabled) {
  2272. printk(IBM_ERR "using generic hotkey driver\n");
  2273. return -ENODEV;
  2274. }
  2275. /* ec is required because many other handles are relative to it */
  2276. IBM_HANDLE_INIT(ec);
  2277. if (!ec_handle) {
  2278. printk(IBM_ERR "ec object not found\n");
  2279. return -ENODEV;
  2280. }
  2281. /* Models with newer firmware report the EC in DMI */
  2282. ibm_thinkpad_ec_found = check_dmi_for_ec();
  2283. if (ibm_thinkpad_ec_found)
  2284. printk(IBM_INFO "ThinkPad EC firmware %s\n",
  2285. ibm_thinkpad_ec_found);
  2286. /* these handles are not required */
  2287. IBM_HANDLE_INIT(vid);
  2288. IBM_HANDLE_INIT(vid2);
  2289. IBM_HANDLE_INIT(ledb);
  2290. IBM_HANDLE_INIT(led);
  2291. IBM_HANDLE_INIT(hkey);
  2292. IBM_HANDLE_INIT(lght);
  2293. IBM_HANDLE_INIT(cmos);
  2294. #ifdef CONFIG_ACPI_IBM_DOCK
  2295. IBM_HANDLE_INIT(dock);
  2296. #endif
  2297. IBM_HANDLE_INIT(pci);
  2298. #ifdef CONFIG_ACPI_IBM_BAY
  2299. IBM_HANDLE_INIT(bay);
  2300. if (bay_handle)
  2301. IBM_HANDLE_INIT(bay_ej);
  2302. IBM_HANDLE_INIT(bay2);
  2303. if (bay2_handle)
  2304. IBM_HANDLE_INIT(bay2_ej);
  2305. #endif
  2306. IBM_HANDLE_INIT(beep);
  2307. IBM_HANDLE_INIT(ecrd);
  2308. IBM_HANDLE_INIT(ecwr);
  2309. IBM_HANDLE_INIT(fans);
  2310. IBM_HANDLE_INIT(gfan);
  2311. IBM_HANDLE_INIT(sfan);
  2312. proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
  2313. if (!proc_dir) {
  2314. printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
  2315. return -ENODEV;
  2316. }
  2317. proc_dir->owner = THIS_MODULE;
  2318. for (i = 0; i < ARRAY_SIZE(ibms); i++) {
  2319. ret = ibm_init(&ibms[i]);
  2320. if (ret >= 0 && *ibms[i].param)
  2321. ret = ibms[i].write(ibms[i].param);
  2322. if (ret < 0) {
  2323. acpi_ibm_exit();
  2324. return ret;
  2325. }
  2326. }
  2327. return 0;
  2328. }
  2329. module_init(acpi_ibm_init);
  2330. module_exit(acpi_ibm_exit);