ibm_acpi.c 64 KB

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