ibm_acpi.c 65 KB

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