thinkpad_acpi.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034
  1. /*
  2. * thinkpad_acpi.c - ThinkPad ACPI Extras
  3. *
  4. *
  5. * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
  6. * Copyright (C) 2006-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301, USA.
  22. */
  23. #define IBM_VERSION "0.14"
  24. #define TPACPI_SYSFS_VERSION 0x000100
  25. /*
  26. * Changelog:
  27. * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
  28. * drivers/misc.
  29. *
  30. * 2006-11-22 0.13 new maintainer
  31. * changelog now lives in git commit history, and will
  32. * not be updated further in-file.
  33. *
  34. * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
  35. * 2005-03-17 0.11 support for 600e, 770x
  36. * thanks to Jamie Lentin <lentinj@dial.pipex.com>
  37. * support for 770e, G41
  38. * G40 and G41 don't have a thinklight
  39. * temperatures no longer experimental
  40. * experimental brightness control
  41. * experimental volume control
  42. * experimental fan enable/disable
  43. * 2005-01-16 0.10 fix module loading on R30, R31
  44. * 2005-01-16 0.9 support for 570, R30, R31
  45. * ultrabay support on A22p, A3x
  46. * limit arg for cmos, led, beep, drop experimental status
  47. * more capable led control on A21e, A22p, T20-22, X20
  48. * experimental temperatures and fan speed
  49. * experimental embedded controller register dump
  50. * mark more functions as __init, drop incorrect __exit
  51. * use MODULE_VERSION
  52. * thanks to Henrik Brix Andersen <brix@gentoo.org>
  53. * fix parameter passing on module loading
  54. * thanks to Rusty Russell <rusty@rustcorp.com.au>
  55. * thanks to Jim Radford <radford@blackbean.org>
  56. * 2004-11-08 0.8 fix init error case, don't return from a macro
  57. * thanks to Chris Wright <chrisw@osdl.org>
  58. * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
  59. * fix led control on A21e
  60. * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
  61. * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
  62. * proc file format changed
  63. * video_switch command
  64. * experimental cmos control
  65. * experimental led control
  66. * experimental acpi sounds
  67. * 2004-09-16 0.4 support for module parameters
  68. * hotkey mask can be prefixed by 0x
  69. * video output switching
  70. * video expansion control
  71. * ultrabay eject support
  72. * removed lcd brightness/on/off control, didn't work
  73. * 2004-08-17 0.3 support for R40
  74. * lcd off, brightness control
  75. * thinklight on/off
  76. * 2004-08-14 0.2 support for T series, X20
  77. * bluetooth enable/disable
  78. * hotkey events disabled by default
  79. * removed fan control, currently useless
  80. * 2004-08-09 0.1 initial release, support for X series
  81. */
  82. #include "thinkpad_acpi.h"
  83. MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
  84. MODULE_DESCRIPTION(IBM_DESC);
  85. MODULE_VERSION(IBM_VERSION);
  86. MODULE_LICENSE("GPL");
  87. /* Please remove this in year 2009 */
  88. MODULE_ALIAS("ibm_acpi");
  89. #define __unused __attribute__ ((unused))
  90. /****************************************************************************
  91. ****************************************************************************
  92. *
  93. * ACPI Helpers and device model
  94. *
  95. ****************************************************************************
  96. ****************************************************************************/
  97. /*************************************************************************
  98. * ACPI basic handles
  99. */
  100. static acpi_handle root_handle = NULL;
  101. #define IBM_HANDLE(object, parent, paths...) \
  102. static acpi_handle object##_handle; \
  103. static acpi_handle *object##_parent = &parent##_handle; \
  104. static char *object##_path; \
  105. static char *object##_paths[] = { paths }
  106. IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
  107. "\\_SB.PCI.ISA.EC", /* 570 */
  108. "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
  109. "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
  110. "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
  111. "\\_SB.PCI0.ICH3.EC0", /* R31 */
  112. "\\_SB.PCI0.LPC.EC", /* all others */
  113. );
  114. IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
  115. IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
  116. /*************************************************************************
  117. * Misc ACPI handles
  118. */
  119. IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
  120. "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
  121. "\\CMS", /* R40, R40e */
  122. ); /* all others */
  123. IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
  124. "^HKEY", /* R30, R31 */
  125. "HKEY", /* all others */
  126. ); /* 570 */
  127. /*************************************************************************
  128. * ACPI helpers
  129. */
  130. static int acpi_evalf(acpi_handle handle,
  131. void *res, char *method, char *fmt, ...)
  132. {
  133. char *fmt0 = fmt;
  134. struct acpi_object_list params;
  135. union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
  136. struct acpi_buffer result, *resultp;
  137. union acpi_object out_obj;
  138. acpi_status status;
  139. va_list ap;
  140. char res_type;
  141. int success;
  142. int quiet;
  143. if (!*fmt) {
  144. printk(IBM_ERR "acpi_evalf() called with empty format\n");
  145. return 0;
  146. }
  147. if (*fmt == 'q') {
  148. quiet = 1;
  149. fmt++;
  150. } else
  151. quiet = 0;
  152. res_type = *(fmt++);
  153. params.count = 0;
  154. params.pointer = &in_objs[0];
  155. va_start(ap, fmt);
  156. while (*fmt) {
  157. char c = *(fmt++);
  158. switch (c) {
  159. case 'd': /* int */
  160. in_objs[params.count].integer.value = va_arg(ap, int);
  161. in_objs[params.count++].type = ACPI_TYPE_INTEGER;
  162. break;
  163. /* add more types as needed */
  164. default:
  165. printk(IBM_ERR "acpi_evalf() called "
  166. "with invalid format character '%c'\n", c);
  167. return 0;
  168. }
  169. }
  170. va_end(ap);
  171. if (res_type != 'v') {
  172. result.length = sizeof(out_obj);
  173. result.pointer = &out_obj;
  174. resultp = &result;
  175. } else
  176. resultp = NULL;
  177. status = acpi_evaluate_object(handle, method, &params, resultp);
  178. switch (res_type) {
  179. case 'd': /* int */
  180. if (res)
  181. *(int *)res = out_obj.integer.value;
  182. success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
  183. break;
  184. case 'v': /* void */
  185. success = status == AE_OK;
  186. break;
  187. /* add more types as needed */
  188. default:
  189. printk(IBM_ERR "acpi_evalf() called "
  190. "with invalid format character '%c'\n", res_type);
  191. return 0;
  192. }
  193. if (!success && !quiet)
  194. printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
  195. method, fmt0, status);
  196. return success;
  197. }
  198. static void __unused acpi_print_int(acpi_handle handle, char *method)
  199. {
  200. int i;
  201. if (acpi_evalf(handle, &i, method, "d"))
  202. printk(IBM_INFO "%s = 0x%x\n", method, i);
  203. else
  204. printk(IBM_ERR "error calling %s\n", method);
  205. }
  206. static int acpi_ec_read(int i, u8 * p)
  207. {
  208. int v;
  209. if (ecrd_handle) {
  210. if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
  211. return 0;
  212. *p = v;
  213. } else {
  214. if (ec_read(i, p) < 0)
  215. return 0;
  216. }
  217. return 1;
  218. }
  219. static int acpi_ec_write(int i, u8 v)
  220. {
  221. if (ecwr_handle) {
  222. if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
  223. return 0;
  224. } else {
  225. if (ec_write(i, v) < 0)
  226. return 0;
  227. }
  228. return 1;
  229. }
  230. static int _sta(acpi_handle handle)
  231. {
  232. int status;
  233. if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
  234. status = 0;
  235. return status;
  236. }
  237. static int issue_thinkpad_cmos_command(int cmos_cmd)
  238. {
  239. if (!cmos_handle)
  240. return -ENXIO;
  241. if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
  242. return -EIO;
  243. return 0;
  244. }
  245. /*************************************************************************
  246. * ACPI device model
  247. */
  248. static void drv_acpi_handle_init(char *name,
  249. acpi_handle *handle, acpi_handle parent,
  250. char **paths, int num_paths, char **path)
  251. {
  252. int i;
  253. acpi_status status;
  254. for (i = 0; i < num_paths; i++) {
  255. status = acpi_get_handle(parent, paths[i], handle);
  256. if (ACPI_SUCCESS(status)) {
  257. *path = paths[i];
  258. return;
  259. }
  260. }
  261. *handle = NULL;
  262. }
  263. static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
  264. {
  265. struct ibm_struct *ibm = data;
  266. if (!ibm || !ibm->acpi || !ibm->acpi->notify)
  267. return;
  268. ibm->acpi->notify(ibm, event);
  269. }
  270. static int __init setup_acpi_notify(struct ibm_struct *ibm)
  271. {
  272. acpi_status status;
  273. int ret;
  274. BUG_ON(!ibm->acpi);
  275. if (!*ibm->acpi->handle)
  276. return 0;
  277. dbg_printk(TPACPI_DBG_INIT,
  278. "setting up ACPI notify for %s\n", ibm->name);
  279. ret = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
  280. if (ret < 0) {
  281. printk(IBM_ERR "%s device not present\n", ibm->name);
  282. return -ENODEV;
  283. }
  284. acpi_driver_data(ibm->acpi->device) = ibm;
  285. sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
  286. IBM_ACPI_EVENT_PREFIX,
  287. ibm->name);
  288. status = acpi_install_notify_handler(*ibm->acpi->handle,
  289. ibm->acpi->type, dispatch_acpi_notify, ibm);
  290. if (ACPI_FAILURE(status)) {
  291. if (status == AE_ALREADY_EXISTS) {
  292. printk(IBM_NOTICE "another device driver is already handling %s events\n",
  293. ibm->name);
  294. } else {
  295. printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
  296. ibm->name, status);
  297. }
  298. return -ENODEV;
  299. }
  300. ibm->flags.acpi_notify_installed = 1;
  301. return 0;
  302. }
  303. static int __init tpacpi_device_add(struct acpi_device *device)
  304. {
  305. return 0;
  306. }
  307. static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
  308. {
  309. int ret;
  310. dbg_printk(TPACPI_DBG_INIT,
  311. "registering %s as an ACPI driver\n", ibm->name);
  312. BUG_ON(!ibm->acpi);
  313. ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
  314. if (!ibm->acpi->driver) {
  315. printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
  316. return -ENOMEM;
  317. }
  318. sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
  319. ibm->acpi->driver->ids = ibm->acpi->hid;
  320. ibm->acpi->driver->ops.add = &tpacpi_device_add;
  321. ret = acpi_bus_register_driver(ibm->acpi->driver);
  322. if (ret < 0) {
  323. printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
  324. ibm->acpi->hid, ret);
  325. kfree(ibm->acpi->driver);
  326. ibm->acpi->driver = NULL;
  327. } else if (!ret)
  328. ibm->flags.acpi_driver_registered = 1;
  329. return ret;
  330. }
  331. /****************************************************************************
  332. ****************************************************************************
  333. *
  334. * Procfs Helpers
  335. *
  336. ****************************************************************************
  337. ****************************************************************************/
  338. static int dispatch_procfs_read(char *page, char **start, off_t off,
  339. int count, int *eof, void *data)
  340. {
  341. struct ibm_struct *ibm = data;
  342. int len;
  343. if (!ibm || !ibm->read)
  344. return -EINVAL;
  345. len = ibm->read(page);
  346. if (len < 0)
  347. return len;
  348. if (len <= off + count)
  349. *eof = 1;
  350. *start = page + off;
  351. len -= off;
  352. if (len > count)
  353. len = count;
  354. if (len < 0)
  355. len = 0;
  356. return len;
  357. }
  358. static int dispatch_procfs_write(struct file *file,
  359. const char __user * userbuf,
  360. unsigned long count, void *data)
  361. {
  362. struct ibm_struct *ibm = data;
  363. char *kernbuf;
  364. int ret;
  365. if (!ibm || !ibm->write)
  366. return -EINVAL;
  367. kernbuf = kmalloc(count + 2, GFP_KERNEL);
  368. if (!kernbuf)
  369. return -ENOMEM;
  370. if (copy_from_user(kernbuf, userbuf, count)) {
  371. kfree(kernbuf);
  372. return -EFAULT;
  373. }
  374. kernbuf[count] = 0;
  375. strcat(kernbuf, ",");
  376. ret = ibm->write(kernbuf);
  377. if (ret == 0)
  378. ret = count;
  379. kfree(kernbuf);
  380. return ret;
  381. }
  382. static char *next_cmd(char **cmds)
  383. {
  384. char *start = *cmds;
  385. char *end;
  386. while ((end = strchr(start, ',')) && end == start)
  387. start = end + 1;
  388. if (!end)
  389. return NULL;
  390. *end = 0;
  391. *cmds = end + 1;
  392. return start;
  393. }
  394. /****************************************************************************
  395. ****************************************************************************
  396. *
  397. * Device model: hwmon and platform
  398. *
  399. ****************************************************************************
  400. ****************************************************************************/
  401. static struct platform_device *tpacpi_pdev = NULL;
  402. static struct class_device *tpacpi_hwmon = NULL;
  403. static struct platform_driver tpacpi_pdriver = {
  404. .driver = {
  405. .name = IBM_DRVR_NAME,
  406. .owner = THIS_MODULE,
  407. },
  408. };
  409. /*************************************************************************
  410. * thinkpad-acpi driver attributes
  411. */
  412. /* interface_version --------------------------------------------------- */
  413. static ssize_t tpacpi_driver_interface_version_show(
  414. struct device_driver *drv,
  415. char *buf)
  416. {
  417. return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
  418. }
  419. static DRIVER_ATTR(interface_version, S_IRUGO,
  420. tpacpi_driver_interface_version_show, NULL);
  421. /* debug_level --------------------------------------------------------- */
  422. static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
  423. char *buf)
  424. {
  425. return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
  426. }
  427. static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
  428. const char *buf, size_t count)
  429. {
  430. unsigned long t;
  431. if (parse_strtoul(buf, 0xffff, &t))
  432. return -EINVAL;
  433. dbg_level = t;
  434. return count;
  435. }
  436. static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
  437. tpacpi_driver_debug_show, tpacpi_driver_debug_store);
  438. /* version ------------------------------------------------------------- */
  439. static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
  440. char *buf)
  441. {
  442. return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
  443. }
  444. static DRIVER_ATTR(version, S_IRUGO,
  445. tpacpi_driver_version_show, NULL);
  446. /* --------------------------------------------------------------------- */
  447. static struct driver_attribute* tpacpi_driver_attributes[] = {
  448. &driver_attr_debug_level, &driver_attr_version,
  449. &driver_attr_interface_version,
  450. };
  451. static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
  452. {
  453. int i, res;
  454. i = 0;
  455. res = 0;
  456. while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
  457. res = driver_create_file(drv, tpacpi_driver_attributes[i]);
  458. i++;
  459. }
  460. return res;
  461. }
  462. static void tpacpi_remove_driver_attributes(struct device_driver *drv)
  463. {
  464. int i;
  465. for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
  466. driver_remove_file(drv, tpacpi_driver_attributes[i]);
  467. }
  468. /*************************************************************************
  469. * sysfs support helpers
  470. */
  471. struct attribute_set_obj {
  472. struct attribute_set s;
  473. struct attribute *a;
  474. } __attribute__((packed));
  475. static struct attribute_set *create_attr_set(unsigned int max_members,
  476. const char* name)
  477. {
  478. struct attribute_set_obj *sobj;
  479. if (max_members == 0)
  480. return NULL;
  481. /* Allocates space for implicit NULL at the end too */
  482. sobj = kzalloc(sizeof(struct attribute_set_obj) +
  483. max_members * sizeof(struct attribute *),
  484. GFP_KERNEL);
  485. if (!sobj)
  486. return NULL;
  487. sobj->s.max_members = max_members;
  488. sobj->s.group.attrs = &sobj->a;
  489. sobj->s.group.name = name;
  490. return &sobj->s;
  491. }
  492. /* not multi-threaded safe, use it in a single thread per set */
  493. static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
  494. {
  495. if (!s || !attr)
  496. return -EINVAL;
  497. if (s->members >= s->max_members)
  498. return -ENOMEM;
  499. s->group.attrs[s->members] = attr;
  500. s->members++;
  501. return 0;
  502. }
  503. static int add_many_to_attr_set(struct attribute_set* s,
  504. struct attribute **attr,
  505. unsigned int count)
  506. {
  507. int i, res;
  508. for (i = 0; i < count; i++) {
  509. res = add_to_attr_set(s, attr[i]);
  510. if (res)
  511. return res;
  512. }
  513. return 0;
  514. }
  515. static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
  516. {
  517. sysfs_remove_group(kobj, &s->group);
  518. destroy_attr_set(s);
  519. }
  520. static int parse_strtoul(const char *buf,
  521. unsigned long max, unsigned long *value)
  522. {
  523. char *endp;
  524. *value = simple_strtoul(buf, &endp, 0);
  525. while (*endp && isspace(*endp))
  526. endp++;
  527. if (*endp || *value > max)
  528. return -EINVAL;
  529. return 0;
  530. }
  531. /****************************************************************************
  532. ****************************************************************************
  533. *
  534. * Subdrivers
  535. *
  536. ****************************************************************************
  537. ****************************************************************************/
  538. /*************************************************************************
  539. * thinkpad-acpi init subdriver
  540. */
  541. static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
  542. {
  543. printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
  544. printk(IBM_INFO "%s\n", IBM_URL);
  545. if (ibm_thinkpad_ec_found)
  546. printk(IBM_INFO "ThinkPad EC firmware %s\n",
  547. ibm_thinkpad_ec_found);
  548. return 0;
  549. }
  550. static int thinkpad_acpi_driver_read(char *p)
  551. {
  552. int len = 0;
  553. len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
  554. len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
  555. return len;
  556. }
  557. static struct ibm_struct thinkpad_acpi_driver_data = {
  558. .name = "driver",
  559. .read = thinkpad_acpi_driver_read,
  560. };
  561. /*************************************************************************
  562. * Hotkey subdriver
  563. */
  564. static int hotkey_orig_status;
  565. static int hotkey_orig_mask;
  566. static int __init hotkey_init(struct ibm_init_struct *iibm)
  567. {
  568. int res;
  569. vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
  570. IBM_ACPIHANDLE_INIT(hkey);
  571. mutex_init(&hotkey_mutex);
  572. /* hotkey not supported on 570 */
  573. tp_features.hotkey = hkey_handle != NULL;
  574. vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
  575. str_supported(tp_features.hotkey));
  576. if (tp_features.hotkey) {
  577. /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  578. A30, R30, R31, T20-22, X20-21, X22-24 */
  579. tp_features.hotkey_mask =
  580. acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
  581. vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
  582. str_supported(tp_features.hotkey_mask));
  583. res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
  584. if (res)
  585. return res;
  586. }
  587. return (tp_features.hotkey)? 0 : 1;
  588. }
  589. static void hotkey_exit(void)
  590. {
  591. int res;
  592. if (tp_features.hotkey) {
  593. dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
  594. res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
  595. if (res)
  596. printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
  597. }
  598. }
  599. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  600. {
  601. int hkey;
  602. if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
  603. acpi_bus_generate_event(ibm->acpi->device, event, hkey);
  604. else {
  605. printk(IBM_ERR "unknown hotkey event %d\n", event);
  606. acpi_bus_generate_event(ibm->acpi->device, event, 0);
  607. }
  608. }
  609. /*
  610. * Call with hotkey_mutex held
  611. */
  612. static int hotkey_get(int *status, int *mask)
  613. {
  614. if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
  615. return -EIO;
  616. if (tp_features.hotkey_mask)
  617. if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
  618. return -EIO;
  619. return 0;
  620. }
  621. /*
  622. * Call with hotkey_mutex held
  623. */
  624. static int hotkey_set(int status, int mask)
  625. {
  626. int i;
  627. if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
  628. return -EIO;
  629. if (tp_features.hotkey_mask)
  630. for (i = 0; i < 32; i++) {
  631. int bit = ((1 << i) & mask) != 0;
  632. if (!acpi_evalf(hkey_handle,
  633. NULL, "MHKM", "vdd", i + 1, bit))
  634. return -EIO;
  635. }
  636. return 0;
  637. }
  638. static int hotkey_read(char *p)
  639. {
  640. int res, status, mask;
  641. int len = 0;
  642. if (!tp_features.hotkey) {
  643. len += sprintf(p + len, "status:\t\tnot supported\n");
  644. return len;
  645. }
  646. res = mutex_lock_interruptible(&hotkey_mutex);
  647. if (res < 0)
  648. return res;
  649. res = hotkey_get(&status, &mask);
  650. mutex_unlock(&hotkey_mutex);
  651. if (res)
  652. return res;
  653. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
  654. if (tp_features.hotkey_mask) {
  655. len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
  656. len += sprintf(p + len,
  657. "commands:\tenable, disable, reset, <mask>\n");
  658. } else {
  659. len += sprintf(p + len, "mask:\t\tnot supported\n");
  660. len += sprintf(p + len, "commands:\tenable, disable, reset\n");
  661. }
  662. return len;
  663. }
  664. static int hotkey_write(char *buf)
  665. {
  666. int res, status, mask;
  667. char *cmd;
  668. int do_cmd = 0;
  669. if (!tp_features.hotkey)
  670. return -ENODEV;
  671. res = mutex_lock_interruptible(&hotkey_mutex);
  672. if (res < 0)
  673. return res;
  674. res = hotkey_get(&status, &mask);
  675. if (res)
  676. goto errexit;
  677. res = 0;
  678. while ((cmd = next_cmd(&buf))) {
  679. if (strlencmp(cmd, "enable") == 0) {
  680. status = 1;
  681. } else if (strlencmp(cmd, "disable") == 0) {
  682. status = 0;
  683. } else if (strlencmp(cmd, "reset") == 0) {
  684. status = hotkey_orig_status;
  685. mask = hotkey_orig_mask;
  686. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  687. /* mask set */
  688. } else if (sscanf(cmd, "%x", &mask) == 1) {
  689. /* mask set */
  690. } else {
  691. res = -EINVAL;
  692. goto errexit;
  693. }
  694. do_cmd = 1;
  695. }
  696. if (do_cmd)
  697. res = hotkey_set(status, mask);
  698. errexit:
  699. mutex_unlock(&hotkey_mutex);
  700. return res;
  701. }
  702. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
  703. .hid = IBM_HKEY_HID,
  704. .notify = hotkey_notify,
  705. .handle = &hkey_handle,
  706. .type = ACPI_DEVICE_NOTIFY,
  707. };
  708. static struct ibm_struct hotkey_driver_data = {
  709. .name = "hotkey",
  710. .read = hotkey_read,
  711. .write = hotkey_write,
  712. .exit = hotkey_exit,
  713. .acpi = &ibm_hotkey_acpidriver,
  714. };
  715. /*************************************************************************
  716. * Bluetooth subdriver
  717. */
  718. static int __init bluetooth_init(struct ibm_init_struct *iibm)
  719. {
  720. int status = 0;
  721. vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
  722. IBM_ACPIHANDLE_INIT(hkey);
  723. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  724. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  725. tp_features.bluetooth = hkey_handle &&
  726. acpi_evalf(hkey_handle, &status, "GBDC", "qd");
  727. vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
  728. str_supported(tp_features.bluetooth),
  729. status);
  730. if (tp_features.bluetooth &&
  731. !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
  732. /* no bluetooth hardware present in system */
  733. tp_features.bluetooth = 0;
  734. dbg_printk(TPACPI_DBG_INIT,
  735. "bluetooth hardware not installed\n");
  736. }
  737. return (tp_features.bluetooth)? 0 : 1;
  738. }
  739. static int bluetooth_get_radiosw(void)
  740. {
  741. int status;
  742. if (!tp_features.bluetooth)
  743. return -ENODEV;
  744. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  745. return -EIO;
  746. return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
  747. }
  748. static int bluetooth_set_radiosw(int radio_on)
  749. {
  750. int status;
  751. if (!tp_features.bluetooth)
  752. return -ENODEV;
  753. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  754. return -EIO;
  755. if (radio_on)
  756. status |= TP_ACPI_BLUETOOTH_RADIOSSW;
  757. else
  758. status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
  759. if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  760. return -EIO;
  761. return 0;
  762. }
  763. static int bluetooth_read(char *p)
  764. {
  765. int len = 0;
  766. int status = bluetooth_get_radiosw();
  767. if (!tp_features.bluetooth)
  768. len += sprintf(p + len, "status:\t\tnot supported\n");
  769. else {
  770. len += sprintf(p + len, "status:\t\t%s\n",
  771. (status)? "enabled" : "disabled");
  772. len += sprintf(p + len, "commands:\tenable, disable\n");
  773. }
  774. return len;
  775. }
  776. static int bluetooth_write(char *buf)
  777. {
  778. char *cmd;
  779. if (!tp_features.bluetooth)
  780. return -ENODEV;
  781. while ((cmd = next_cmd(&buf))) {
  782. if (strlencmp(cmd, "enable") == 0) {
  783. bluetooth_set_radiosw(1);
  784. } else if (strlencmp(cmd, "disable") == 0) {
  785. bluetooth_set_radiosw(0);
  786. } else
  787. return -EINVAL;
  788. }
  789. return 0;
  790. }
  791. static struct ibm_struct bluetooth_driver_data = {
  792. .name = "bluetooth",
  793. .read = bluetooth_read,
  794. .write = bluetooth_write,
  795. };
  796. /*************************************************************************
  797. * Wan subdriver
  798. */
  799. static int __init wan_init(struct ibm_init_struct *iibm)
  800. {
  801. int status = 0;
  802. vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
  803. IBM_ACPIHANDLE_INIT(hkey);
  804. tp_features.wan = hkey_handle &&
  805. acpi_evalf(hkey_handle, &status, "GWAN", "qd");
  806. vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
  807. str_supported(tp_features.wan),
  808. status);
  809. if (tp_features.wan &&
  810. !(status & TP_ACPI_WANCARD_HWPRESENT)) {
  811. /* no wan hardware present in system */
  812. tp_features.wan = 0;
  813. dbg_printk(TPACPI_DBG_INIT,
  814. "wan hardware not installed\n");
  815. }
  816. return (tp_features.wan)? 0 : 1;
  817. }
  818. static int wan_get_radiosw(void)
  819. {
  820. int status;
  821. if (!tp_features.wan)
  822. return -ENODEV;
  823. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  824. return -EIO;
  825. return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
  826. }
  827. static int wan_set_radiosw(int radio_on)
  828. {
  829. int status;
  830. if (!tp_features.wan)
  831. return -ENODEV;
  832. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  833. return -EIO;
  834. if (radio_on)
  835. status |= TP_ACPI_WANCARD_RADIOSSW;
  836. else
  837. status &= ~TP_ACPI_WANCARD_RADIOSSW;
  838. if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  839. return -EIO;
  840. return 0;
  841. }
  842. static int wan_read(char *p)
  843. {
  844. int len = 0;
  845. int status = wan_get_radiosw();
  846. if (!tp_features.wan)
  847. len += sprintf(p + len, "status:\t\tnot supported\n");
  848. else {
  849. len += sprintf(p + len, "status:\t\t%s\n",
  850. (status)? "enabled" : "disabled");
  851. len += sprintf(p + len, "commands:\tenable, disable\n");
  852. }
  853. return len;
  854. }
  855. static int wan_write(char *buf)
  856. {
  857. char *cmd;
  858. if (!tp_features.wan)
  859. return -ENODEV;
  860. while ((cmd = next_cmd(&buf))) {
  861. if (strlencmp(cmd, "enable") == 0) {
  862. wan_set_radiosw(1);
  863. } else if (strlencmp(cmd, "disable") == 0) {
  864. wan_set_radiosw(0);
  865. } else
  866. return -EINVAL;
  867. }
  868. return 0;
  869. }
  870. static struct ibm_struct wan_driver_data = {
  871. .name = "wan",
  872. .read = wan_read,
  873. .write = wan_write,
  874. .flags.experimental = 1,
  875. };
  876. /*************************************************************************
  877. * Video subdriver
  878. */
  879. static enum video_access_mode video_supported;
  880. static int video_orig_autosw;
  881. IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
  882. "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
  883. "\\_SB.PCI0.VID0", /* 770e */
  884. "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
  885. "\\_SB.PCI0.AGP.VID", /* all others */
  886. ); /* R30, R31 */
  887. IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
  888. static int __init video_init(struct ibm_init_struct *iibm)
  889. {
  890. int ivga;
  891. vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
  892. IBM_ACPIHANDLE_INIT(vid);
  893. IBM_ACPIHANDLE_INIT(vid2);
  894. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  895. /* G41, assume IVGA doesn't change */
  896. vid_handle = vid2_handle;
  897. if (!vid_handle)
  898. /* video switching not supported on R30, R31 */
  899. video_supported = TPACPI_VIDEO_NONE;
  900. else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  901. /* 570 */
  902. video_supported = TPACPI_VIDEO_570;
  903. else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  904. /* 600e/x, 770e, 770x */
  905. video_supported = TPACPI_VIDEO_770;
  906. else
  907. /* all others */
  908. video_supported = TPACPI_VIDEO_NEW;
  909. vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
  910. str_supported(video_supported != TPACPI_VIDEO_NONE),
  911. video_supported);
  912. return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
  913. }
  914. static void video_exit(void)
  915. {
  916. dbg_printk(TPACPI_DBG_EXIT,
  917. "restoring original video autoswitch mode\n");
  918. if (video_autosw_set(video_orig_autosw))
  919. printk(IBM_ERR "error while trying to restore original "
  920. "video autoswitch mode\n");
  921. }
  922. static int video_outputsw_get(void)
  923. {
  924. int status = 0;
  925. int i;
  926. switch (video_supported) {
  927. case TPACPI_VIDEO_570:
  928. if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
  929. TP_ACPI_VIDEO_570_PHSCMD))
  930. return -EIO;
  931. status = i & TP_ACPI_VIDEO_570_PHSMASK;
  932. break;
  933. case TPACPI_VIDEO_770:
  934. if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
  935. return -EIO;
  936. if (i)
  937. status |= TP_ACPI_VIDEO_S_LCD;
  938. if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
  939. return -EIO;
  940. if (i)
  941. status |= TP_ACPI_VIDEO_S_CRT;
  942. break;
  943. case TPACPI_VIDEO_NEW:
  944. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
  945. !acpi_evalf(NULL, &i, "\\VCDC", "d"))
  946. return -EIO;
  947. if (i)
  948. status |= TP_ACPI_VIDEO_S_CRT;
  949. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
  950. !acpi_evalf(NULL, &i, "\\VCDL", "d"))
  951. return -EIO;
  952. if (i)
  953. status |= TP_ACPI_VIDEO_S_LCD;
  954. if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
  955. return -EIO;
  956. if (i)
  957. status |= TP_ACPI_VIDEO_S_DVI;
  958. break;
  959. default:
  960. return -ENOSYS;
  961. }
  962. return status;
  963. }
  964. static int video_outputsw_set(int status)
  965. {
  966. int autosw;
  967. int res = 0;
  968. switch (video_supported) {
  969. case TPACPI_VIDEO_570:
  970. res = acpi_evalf(NULL, NULL,
  971. "\\_SB.PHS2", "vdd",
  972. TP_ACPI_VIDEO_570_PHS2CMD,
  973. status | TP_ACPI_VIDEO_570_PHS2SET);
  974. break;
  975. case TPACPI_VIDEO_770:
  976. autosw = video_autosw_get();
  977. if (autosw < 0)
  978. return autosw;
  979. res = video_autosw_set(1);
  980. if (res)
  981. return res;
  982. res = acpi_evalf(vid_handle, NULL,
  983. "ASWT", "vdd", status * 0x100, 0);
  984. if (!autosw && video_autosw_set(autosw)) {
  985. printk(IBM_ERR "video auto-switch left enabled due to error\n");
  986. return -EIO;
  987. }
  988. break;
  989. case TPACPI_VIDEO_NEW:
  990. res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  991. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  992. break;
  993. default:
  994. return -ENOSYS;
  995. }
  996. return (res)? 0 : -EIO;
  997. }
  998. static int video_autosw_get(void)
  999. {
  1000. int autosw = 0;
  1001. switch (video_supported) {
  1002. case TPACPI_VIDEO_570:
  1003. if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
  1004. return -EIO;
  1005. break;
  1006. case TPACPI_VIDEO_770:
  1007. case TPACPI_VIDEO_NEW:
  1008. if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
  1009. return -EIO;
  1010. break;
  1011. default:
  1012. return -ENOSYS;
  1013. }
  1014. return autosw & 1;
  1015. }
  1016. static int video_autosw_set(int enable)
  1017. {
  1018. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
  1019. return -EIO;
  1020. return 0;
  1021. }
  1022. static int video_outputsw_cycle(void)
  1023. {
  1024. int autosw = video_autosw_get();
  1025. int res;
  1026. if (autosw < 0)
  1027. return autosw;
  1028. switch (video_supported) {
  1029. case TPACPI_VIDEO_570:
  1030. res = video_autosw_set(1);
  1031. if (res)
  1032. return res;
  1033. res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
  1034. break;
  1035. case TPACPI_VIDEO_770:
  1036. case TPACPI_VIDEO_NEW:
  1037. res = video_autosw_set(1);
  1038. if (res)
  1039. return res;
  1040. res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
  1041. break;
  1042. default:
  1043. return -ENOSYS;
  1044. }
  1045. if (!autosw && video_autosw_set(autosw)) {
  1046. printk(IBM_ERR "video auto-switch left enabled due to error\n");
  1047. return -EIO;
  1048. }
  1049. return (res)? 0 : -EIO;
  1050. }
  1051. static int video_expand_toggle(void)
  1052. {
  1053. switch (video_supported) {
  1054. case TPACPI_VIDEO_570:
  1055. return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
  1056. 0 : -EIO;
  1057. case TPACPI_VIDEO_770:
  1058. return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
  1059. 0 : -EIO;
  1060. case TPACPI_VIDEO_NEW:
  1061. return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
  1062. 0 : -EIO;
  1063. default:
  1064. return -ENOSYS;
  1065. }
  1066. /* not reached */
  1067. }
  1068. static int video_read(char *p)
  1069. {
  1070. int status, autosw;
  1071. int len = 0;
  1072. if (video_supported == TPACPI_VIDEO_NONE) {
  1073. len += sprintf(p + len, "status:\t\tnot supported\n");
  1074. return len;
  1075. }
  1076. status = video_outputsw_get();
  1077. if (status < 0)
  1078. return status;
  1079. autosw = video_autosw_get();
  1080. if (autosw < 0)
  1081. return autosw;
  1082. len += sprintf(p + len, "status:\t\tsupported\n");
  1083. len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
  1084. len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
  1085. if (video_supported == TPACPI_VIDEO_NEW)
  1086. len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
  1087. len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
  1088. len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
  1089. len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
  1090. if (video_supported == TPACPI_VIDEO_NEW)
  1091. len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
  1092. len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
  1093. len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
  1094. return len;
  1095. }
  1096. static int video_write(char *buf)
  1097. {
  1098. char *cmd;
  1099. int enable, disable, status;
  1100. int res;
  1101. if (video_supported == TPACPI_VIDEO_NONE)
  1102. return -ENODEV;
  1103. enable = 0;
  1104. disable = 0;
  1105. while ((cmd = next_cmd(&buf))) {
  1106. if (strlencmp(cmd, "lcd_enable") == 0) {
  1107. enable |= TP_ACPI_VIDEO_S_LCD;
  1108. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  1109. disable |= TP_ACPI_VIDEO_S_LCD;
  1110. } else if (strlencmp(cmd, "crt_enable") == 0) {
  1111. enable |= TP_ACPI_VIDEO_S_CRT;
  1112. } else if (strlencmp(cmd, "crt_disable") == 0) {
  1113. disable |= TP_ACPI_VIDEO_S_CRT;
  1114. } else if (video_supported == TPACPI_VIDEO_NEW &&
  1115. strlencmp(cmd, "dvi_enable") == 0) {
  1116. enable |= TP_ACPI_VIDEO_S_DVI;
  1117. } else if (video_supported == TPACPI_VIDEO_NEW &&
  1118. strlencmp(cmd, "dvi_disable") == 0) {
  1119. disable |= TP_ACPI_VIDEO_S_DVI;
  1120. } else if (strlencmp(cmd, "auto_enable") == 0) {
  1121. res = video_autosw_set(1);
  1122. if (res)
  1123. return res;
  1124. } else if (strlencmp(cmd, "auto_disable") == 0) {
  1125. res = video_autosw_set(0);
  1126. if (res)
  1127. return res;
  1128. } else if (strlencmp(cmd, "video_switch") == 0) {
  1129. res = video_outputsw_cycle();
  1130. if (res)
  1131. return res;
  1132. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  1133. res = video_expand_toggle();
  1134. if (res)
  1135. return res;
  1136. } else
  1137. return -EINVAL;
  1138. }
  1139. if (enable || disable) {
  1140. status = video_outputsw_get();
  1141. if (status < 0)
  1142. return status;
  1143. res = video_outputsw_set((status & ~disable) | enable);
  1144. if (res)
  1145. return res;
  1146. }
  1147. return 0;
  1148. }
  1149. static struct ibm_struct video_driver_data = {
  1150. .name = "video",
  1151. .read = video_read,
  1152. .write = video_write,
  1153. .exit = video_exit,
  1154. };
  1155. /*************************************************************************
  1156. * Light (thinklight) subdriver
  1157. */
  1158. IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
  1159. IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
  1160. static int __init light_init(struct ibm_init_struct *iibm)
  1161. {
  1162. vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
  1163. IBM_ACPIHANDLE_INIT(ledb);
  1164. IBM_ACPIHANDLE_INIT(lght);
  1165. IBM_ACPIHANDLE_INIT(cmos);
  1166. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  1167. tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
  1168. if (tp_features.light)
  1169. /* light status not supported on
  1170. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  1171. tp_features.light_status =
  1172. acpi_evalf(ec_handle, NULL, "KBLT", "qv");
  1173. vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
  1174. str_supported(tp_features.light));
  1175. return (tp_features.light)? 0 : 1;
  1176. }
  1177. static int light_read(char *p)
  1178. {
  1179. int len = 0;
  1180. int status = 0;
  1181. if (!tp_features.light) {
  1182. len += sprintf(p + len, "status:\t\tnot supported\n");
  1183. } else if (!tp_features.light_status) {
  1184. len += sprintf(p + len, "status:\t\tunknown\n");
  1185. len += sprintf(p + len, "commands:\ton, off\n");
  1186. } else {
  1187. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  1188. return -EIO;
  1189. len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
  1190. len += sprintf(p + len, "commands:\ton, off\n");
  1191. }
  1192. return len;
  1193. }
  1194. static int light_write(char *buf)
  1195. {
  1196. int cmos_cmd, lght_cmd;
  1197. char *cmd;
  1198. int success;
  1199. if (!tp_features.light)
  1200. return -ENODEV;
  1201. while ((cmd = next_cmd(&buf))) {
  1202. if (strlencmp(cmd, "on") == 0) {
  1203. cmos_cmd = 0x0c;
  1204. lght_cmd = 1;
  1205. } else if (strlencmp(cmd, "off") == 0) {
  1206. cmos_cmd = 0x0d;
  1207. lght_cmd = 0;
  1208. } else
  1209. return -EINVAL;
  1210. success = cmos_handle ?
  1211. acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
  1212. acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
  1213. if (!success)
  1214. return -EIO;
  1215. }
  1216. return 0;
  1217. }
  1218. static struct ibm_struct light_driver_data = {
  1219. .name = "light",
  1220. .read = light_read,
  1221. .write = light_write,
  1222. };
  1223. /*************************************************************************
  1224. * Dock subdriver
  1225. */
  1226. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  1227. IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
  1228. "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
  1229. "\\_SB.PCI0.PCI1.DOCK", /* all others */
  1230. "\\_SB.PCI.ISA.SLCE", /* 570 */
  1231. ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
  1232. /* don't list other alternatives as we install a notify handler on the 570 */
  1233. IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
  1234. #define dock_docked() (_sta(dock_handle) & 1)
  1235. static int __init dock_init(struct ibm_init_struct *iibm)
  1236. {
  1237. vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
  1238. IBM_ACPIHANDLE_INIT(dock);
  1239. IBM_ACPIHANDLE_INIT(pci);
  1240. vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
  1241. str_supported(dock_handle != NULL));
  1242. return (dock_handle)? 0 : 1;
  1243. }
  1244. static void dock_notify(struct ibm_struct *ibm, u32 event)
  1245. {
  1246. int docked = dock_docked();
  1247. int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
  1248. if (event == 1 && !pci) /* 570 */
  1249. acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
  1250. else if (event == 1 && pci) /* 570 */
  1251. acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
  1252. else if (event == 3 && docked)
  1253. acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
  1254. else if (event == 3 && !docked)
  1255. acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
  1256. else if (event == 0 && docked)
  1257. acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
  1258. else {
  1259. printk(IBM_ERR "unknown dock event %d, status %d\n",
  1260. event, _sta(dock_handle));
  1261. acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
  1262. }
  1263. }
  1264. static int dock_read(char *p)
  1265. {
  1266. int len = 0;
  1267. int docked = dock_docked();
  1268. if (!dock_handle)
  1269. len += sprintf(p + len, "status:\t\tnot supported\n");
  1270. else if (!docked)
  1271. len += sprintf(p + len, "status:\t\tundocked\n");
  1272. else {
  1273. len += sprintf(p + len, "status:\t\tdocked\n");
  1274. len += sprintf(p + len, "commands:\tdock, undock\n");
  1275. }
  1276. return len;
  1277. }
  1278. static int dock_write(char *buf)
  1279. {
  1280. char *cmd;
  1281. if (!dock_docked())
  1282. return -ENODEV;
  1283. while ((cmd = next_cmd(&buf))) {
  1284. if (strlencmp(cmd, "undock") == 0) {
  1285. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
  1286. !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
  1287. return -EIO;
  1288. } else if (strlencmp(cmd, "dock") == 0) {
  1289. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
  1290. return -EIO;
  1291. } else
  1292. return -EINVAL;
  1293. }
  1294. return 0;
  1295. }
  1296. static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
  1297. {
  1298. .notify = dock_notify,
  1299. .handle = &dock_handle,
  1300. .type = ACPI_SYSTEM_NOTIFY,
  1301. },
  1302. {
  1303. .hid = IBM_PCI_HID,
  1304. .notify = dock_notify,
  1305. .handle = &pci_handle,
  1306. .type = ACPI_SYSTEM_NOTIFY,
  1307. },
  1308. };
  1309. static struct ibm_struct dock_driver_data[2] = {
  1310. {
  1311. .name = "dock",
  1312. .read = dock_read,
  1313. .write = dock_write,
  1314. .acpi = &ibm_dock_acpidriver[0],
  1315. },
  1316. {
  1317. .name = "dock",
  1318. .acpi = &ibm_dock_acpidriver[1],
  1319. },
  1320. };
  1321. #endif /* CONFIG_THINKPAD_ACPI_DOCK */
  1322. /*************************************************************************
  1323. * Bay subdriver
  1324. */
  1325. #ifdef CONFIG_THINKPAD_ACPI_BAY
  1326. IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
  1327. "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
  1328. "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
  1329. "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
  1330. ); /* A21e, R30, R31 */
  1331. IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
  1332. "_EJ0", /* all others */
  1333. ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
  1334. IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
  1335. "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
  1336. ); /* all others */
  1337. IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
  1338. "_EJ0", /* 770x */
  1339. ); /* all others */
  1340. static int __init bay_init(struct ibm_init_struct *iibm)
  1341. {
  1342. vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
  1343. IBM_ACPIHANDLE_INIT(bay);
  1344. if (bay_handle)
  1345. IBM_ACPIHANDLE_INIT(bay_ej);
  1346. IBM_ACPIHANDLE_INIT(bay2);
  1347. if (bay2_handle)
  1348. IBM_ACPIHANDLE_INIT(bay2_ej);
  1349. tp_features.bay_status = bay_handle &&
  1350. acpi_evalf(bay_handle, NULL, "_STA", "qv");
  1351. tp_features.bay_status2 = bay2_handle &&
  1352. acpi_evalf(bay2_handle, NULL, "_STA", "qv");
  1353. tp_features.bay_eject = bay_handle && bay_ej_handle &&
  1354. (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
  1355. tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
  1356. (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
  1357. vdbg_printk(TPACPI_DBG_INIT,
  1358. "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
  1359. str_supported(tp_features.bay_status),
  1360. str_supported(tp_features.bay_eject),
  1361. str_supported(tp_features.bay_status2),
  1362. str_supported(tp_features.bay_eject2));
  1363. return (tp_features.bay_status || tp_features.bay_eject ||
  1364. tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
  1365. }
  1366. static void bay_notify(struct ibm_struct *ibm, u32 event)
  1367. {
  1368. acpi_bus_generate_event(ibm->acpi->device, event, 0);
  1369. }
  1370. #define bay_occupied(b) (_sta(b##_handle) & 1)
  1371. static int bay_read(char *p)
  1372. {
  1373. int len = 0;
  1374. int occupied = bay_occupied(bay);
  1375. int occupied2 = bay_occupied(bay2);
  1376. int eject, eject2;
  1377. len += sprintf(p + len, "status:\t\t%s\n",
  1378. tp_features.bay_status ?
  1379. (occupied ? "occupied" : "unoccupied") :
  1380. "not supported");
  1381. if (tp_features.bay_status2)
  1382. len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
  1383. "occupied" : "unoccupied");
  1384. eject = tp_features.bay_eject && occupied;
  1385. eject2 = tp_features.bay_eject2 && occupied2;
  1386. if (eject && eject2)
  1387. len += sprintf(p + len, "commands:\teject, eject2\n");
  1388. else if (eject)
  1389. len += sprintf(p + len, "commands:\teject\n");
  1390. else if (eject2)
  1391. len += sprintf(p + len, "commands:\teject2\n");
  1392. return len;
  1393. }
  1394. static int bay_write(char *buf)
  1395. {
  1396. char *cmd;
  1397. if (!tp_features.bay_eject && !tp_features.bay_eject2)
  1398. return -ENODEV;
  1399. while ((cmd = next_cmd(&buf))) {
  1400. if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
  1401. if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
  1402. return -EIO;
  1403. } else if (tp_features.bay_eject2 &&
  1404. strlencmp(cmd, "eject2") == 0) {
  1405. if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
  1406. return -EIO;
  1407. } else
  1408. return -EINVAL;
  1409. }
  1410. return 0;
  1411. }
  1412. static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
  1413. .notify = bay_notify,
  1414. .handle = &bay_handle,
  1415. .type = ACPI_SYSTEM_NOTIFY,
  1416. };
  1417. static struct ibm_struct bay_driver_data = {
  1418. .name = "bay",
  1419. .read = bay_read,
  1420. .write = bay_write,
  1421. .acpi = &ibm_bay_acpidriver,
  1422. };
  1423. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  1424. /*************************************************************************
  1425. * CMOS subdriver
  1426. */
  1427. /* sysfs cmos_command -------------------------------------------------- */
  1428. static ssize_t cmos_command_store(struct device *dev,
  1429. struct device_attribute *attr,
  1430. const char *buf, size_t count)
  1431. {
  1432. unsigned long cmos_cmd;
  1433. int res;
  1434. if (parse_strtoul(buf, 21, &cmos_cmd))
  1435. return -EINVAL;
  1436. res = issue_thinkpad_cmos_command(cmos_cmd);
  1437. return (res)? res : count;
  1438. }
  1439. static struct device_attribute dev_attr_cmos_command =
  1440. __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
  1441. /* --------------------------------------------------------------------- */
  1442. static int __init cmos_init(struct ibm_init_struct *iibm)
  1443. {
  1444. int res;
  1445. vdbg_printk(TPACPI_DBG_INIT,
  1446. "initializing cmos commands subdriver\n");
  1447. IBM_ACPIHANDLE_INIT(cmos);
  1448. vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
  1449. str_supported(cmos_handle != NULL));
  1450. res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  1451. if (res)
  1452. return res;
  1453. return (cmos_handle)? 0 : 1;
  1454. }
  1455. static void cmos_exit(void)
  1456. {
  1457. device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  1458. }
  1459. static int cmos_read(char *p)
  1460. {
  1461. int len = 0;
  1462. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  1463. R30, R31, T20-22, X20-21 */
  1464. if (!cmos_handle)
  1465. len += sprintf(p + len, "status:\t\tnot supported\n");
  1466. else {
  1467. len += sprintf(p + len, "status:\t\tsupported\n");
  1468. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
  1469. }
  1470. return len;
  1471. }
  1472. static int cmos_write(char *buf)
  1473. {
  1474. char *cmd;
  1475. int cmos_cmd, res;
  1476. while ((cmd = next_cmd(&buf))) {
  1477. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  1478. cmos_cmd >= 0 && cmos_cmd <= 21) {
  1479. /* cmos_cmd set */
  1480. } else
  1481. return -EINVAL;
  1482. res = issue_thinkpad_cmos_command(cmos_cmd);
  1483. if (res)
  1484. return res;
  1485. }
  1486. return 0;
  1487. }
  1488. static struct ibm_struct cmos_driver_data = {
  1489. .name = "cmos",
  1490. .read = cmos_read,
  1491. .write = cmos_write,
  1492. .exit = cmos_exit,
  1493. };
  1494. /*************************************************************************
  1495. * LED subdriver
  1496. */
  1497. static enum led_access_mode led_supported;
  1498. IBM_HANDLE(led, ec, "SLED", /* 570 */
  1499. "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1500. "LED", /* all others */
  1501. ); /* R30, R31 */
  1502. static int __init led_init(struct ibm_init_struct *iibm)
  1503. {
  1504. vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
  1505. IBM_ACPIHANDLE_INIT(led);
  1506. if (!led_handle)
  1507. /* led not supported on R30, R31 */
  1508. led_supported = TPACPI_LED_NONE;
  1509. else if (strlencmp(led_path, "SLED") == 0)
  1510. /* 570 */
  1511. led_supported = TPACPI_LED_570;
  1512. else if (strlencmp(led_path, "SYSL") == 0)
  1513. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1514. led_supported = TPACPI_LED_OLD;
  1515. else
  1516. /* all others */
  1517. led_supported = TPACPI_LED_NEW;
  1518. vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
  1519. str_supported(led_supported), led_supported);
  1520. return (led_supported != TPACPI_LED_NONE)? 0 : 1;
  1521. }
  1522. #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
  1523. static int led_read(char *p)
  1524. {
  1525. int len = 0;
  1526. if (!led_supported) {
  1527. len += sprintf(p + len, "status:\t\tnot supported\n");
  1528. return len;
  1529. }
  1530. len += sprintf(p + len, "status:\t\tsupported\n");
  1531. if (led_supported == TPACPI_LED_570) {
  1532. /* 570 */
  1533. int i, status;
  1534. for (i = 0; i < 8; i++) {
  1535. if (!acpi_evalf(ec_handle,
  1536. &status, "GLED", "dd", 1 << i))
  1537. return -EIO;
  1538. len += sprintf(p + len, "%d:\t\t%s\n",
  1539. i, led_status(status));
  1540. }
  1541. }
  1542. len += sprintf(p + len, "commands:\t"
  1543. "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
  1544. return len;
  1545. }
  1546. /* off, on, blink */
  1547. static const int led_sled_arg1[] = { 0, 1, 3 };
  1548. static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
  1549. static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
  1550. static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
  1551. static int led_write(char *buf)
  1552. {
  1553. char *cmd;
  1554. int led, ind, ret;
  1555. if (!led_supported)
  1556. return -ENODEV;
  1557. while ((cmd = next_cmd(&buf))) {
  1558. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
  1559. return -EINVAL;
  1560. if (strstr(cmd, "off")) {
  1561. ind = 0;
  1562. } else if (strstr(cmd, "on")) {
  1563. ind = 1;
  1564. } else if (strstr(cmd, "blink")) {
  1565. ind = 2;
  1566. } else
  1567. return -EINVAL;
  1568. if (led_supported == TPACPI_LED_570) {
  1569. /* 570 */
  1570. led = 1 << led;
  1571. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  1572. led, led_sled_arg1[ind]))
  1573. return -EIO;
  1574. } else if (led_supported == TPACPI_LED_OLD) {
  1575. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  1576. led = 1 << led;
  1577. ret = ec_write(TPACPI_LED_EC_HLMS, led);
  1578. if (ret >= 0)
  1579. ret =
  1580. ec_write(TPACPI_LED_EC_HLBL,
  1581. led * led_exp_hlbl[ind]);
  1582. if (ret >= 0)
  1583. ret =
  1584. ec_write(TPACPI_LED_EC_HLCL,
  1585. led * led_exp_hlcl[ind]);
  1586. if (ret < 0)
  1587. return ret;
  1588. } else {
  1589. /* all others */
  1590. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  1591. led, led_led_arg1[ind]))
  1592. return -EIO;
  1593. }
  1594. }
  1595. return 0;
  1596. }
  1597. static struct ibm_struct led_driver_data = {
  1598. .name = "led",
  1599. .read = led_read,
  1600. .write = led_write,
  1601. };
  1602. /*************************************************************************
  1603. * Beep subdriver
  1604. */
  1605. IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  1606. static int __init beep_init(struct ibm_init_struct *iibm)
  1607. {
  1608. vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
  1609. IBM_ACPIHANDLE_INIT(beep);
  1610. vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
  1611. str_supported(beep_handle != NULL));
  1612. return (beep_handle)? 0 : 1;
  1613. }
  1614. static int beep_read(char *p)
  1615. {
  1616. int len = 0;
  1617. if (!beep_handle)
  1618. len += sprintf(p + len, "status:\t\tnot supported\n");
  1619. else {
  1620. len += sprintf(p + len, "status:\t\tsupported\n");
  1621. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  1622. }
  1623. return len;
  1624. }
  1625. static int beep_write(char *buf)
  1626. {
  1627. char *cmd;
  1628. int beep_cmd;
  1629. if (!beep_handle)
  1630. return -ENODEV;
  1631. while ((cmd = next_cmd(&buf))) {
  1632. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  1633. beep_cmd >= 0 && beep_cmd <= 17) {
  1634. /* beep_cmd set */
  1635. } else
  1636. return -EINVAL;
  1637. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
  1638. return -EIO;
  1639. }
  1640. return 0;
  1641. }
  1642. static struct ibm_struct beep_driver_data = {
  1643. .name = "beep",
  1644. .read = beep_read,
  1645. .write = beep_write,
  1646. };
  1647. /*************************************************************************
  1648. * Thermal subdriver
  1649. */
  1650. static enum thermal_access_mode thermal_read_mode;
  1651. /* sysfs temp##_input -------------------------------------------------- */
  1652. static ssize_t thermal_temp_input_show(struct device *dev,
  1653. struct device_attribute *attr,
  1654. char *buf)
  1655. {
  1656. struct sensor_device_attribute *sensor_attr =
  1657. to_sensor_dev_attr(attr);
  1658. int idx = sensor_attr->index;
  1659. s32 value;
  1660. int res;
  1661. res = thermal_get_sensor(idx, &value);
  1662. if (res)
  1663. return res;
  1664. if (value == TP_EC_THERMAL_TMP_NA * 1000)
  1665. return -ENXIO;
  1666. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  1667. }
  1668. #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
  1669. SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
  1670. static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
  1671. THERMAL_SENSOR_ATTR_TEMP(1, 0),
  1672. THERMAL_SENSOR_ATTR_TEMP(2, 1),
  1673. THERMAL_SENSOR_ATTR_TEMP(3, 2),
  1674. THERMAL_SENSOR_ATTR_TEMP(4, 3),
  1675. THERMAL_SENSOR_ATTR_TEMP(5, 4),
  1676. THERMAL_SENSOR_ATTR_TEMP(6, 5),
  1677. THERMAL_SENSOR_ATTR_TEMP(7, 6),
  1678. THERMAL_SENSOR_ATTR_TEMP(8, 7),
  1679. THERMAL_SENSOR_ATTR_TEMP(9, 8),
  1680. THERMAL_SENSOR_ATTR_TEMP(10, 9),
  1681. THERMAL_SENSOR_ATTR_TEMP(11, 10),
  1682. THERMAL_SENSOR_ATTR_TEMP(12, 11),
  1683. THERMAL_SENSOR_ATTR_TEMP(13, 12),
  1684. THERMAL_SENSOR_ATTR_TEMP(14, 13),
  1685. THERMAL_SENSOR_ATTR_TEMP(15, 14),
  1686. THERMAL_SENSOR_ATTR_TEMP(16, 15),
  1687. };
  1688. #define THERMAL_ATTRS(X) \
  1689. &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
  1690. static struct attribute *thermal_temp_input_attr[] = {
  1691. THERMAL_ATTRS(8),
  1692. THERMAL_ATTRS(9),
  1693. THERMAL_ATTRS(10),
  1694. THERMAL_ATTRS(11),
  1695. THERMAL_ATTRS(12),
  1696. THERMAL_ATTRS(13),
  1697. THERMAL_ATTRS(14),
  1698. THERMAL_ATTRS(15),
  1699. THERMAL_ATTRS(0),
  1700. THERMAL_ATTRS(1),
  1701. THERMAL_ATTRS(2),
  1702. THERMAL_ATTRS(3),
  1703. THERMAL_ATTRS(4),
  1704. THERMAL_ATTRS(5),
  1705. THERMAL_ATTRS(6),
  1706. THERMAL_ATTRS(7),
  1707. NULL
  1708. };
  1709. static const struct attribute_group thermal_temp_input16_group = {
  1710. .attrs = thermal_temp_input_attr
  1711. };
  1712. static const struct attribute_group thermal_temp_input8_group = {
  1713. .attrs = &thermal_temp_input_attr[8]
  1714. };
  1715. #undef THERMAL_SENSOR_ATTR_TEMP
  1716. #undef THERMAL_ATTRS
  1717. /* --------------------------------------------------------------------- */
  1718. static int __init thermal_init(struct ibm_init_struct *iibm)
  1719. {
  1720. u8 t, ta1, ta2;
  1721. int i;
  1722. int acpi_tmp7;
  1723. int res;
  1724. vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
  1725. acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  1726. if (ibm_thinkpad_ec_found && experimental) {
  1727. /*
  1728. * Direct EC access mode: sensors at registers
  1729. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  1730. * non-implemented, thermal sensors return 0x80 when
  1731. * not available
  1732. */
  1733. ta1 = ta2 = 0;
  1734. for (i = 0; i < 8; i++) {
  1735. if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
  1736. ta1 |= t;
  1737. } else {
  1738. ta1 = 0;
  1739. break;
  1740. }
  1741. if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
  1742. ta2 |= t;
  1743. } else {
  1744. ta1 = 0;
  1745. break;
  1746. }
  1747. }
  1748. if (ta1 == 0) {
  1749. /* This is sheer paranoia, but we handle it anyway */
  1750. if (acpi_tmp7) {
  1751. printk(IBM_ERR
  1752. "ThinkPad ACPI EC access misbehaving, "
  1753. "falling back to ACPI TMPx access mode\n");
  1754. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  1755. } else {
  1756. printk(IBM_ERR
  1757. "ThinkPad ACPI EC access misbehaving, "
  1758. "disabling thermal sensors access\n");
  1759. thermal_read_mode = TPACPI_THERMAL_NONE;
  1760. }
  1761. } else {
  1762. thermal_read_mode =
  1763. (ta2 != 0) ?
  1764. TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
  1765. }
  1766. } else if (acpi_tmp7) {
  1767. if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  1768. /* 600e/x, 770e, 770x */
  1769. thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
  1770. } else {
  1771. /* Standard ACPI TMPx access, max 8 sensors */
  1772. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  1773. }
  1774. } else {
  1775. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  1776. thermal_read_mode = TPACPI_THERMAL_NONE;
  1777. }
  1778. vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
  1779. str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
  1780. thermal_read_mode);
  1781. switch(thermal_read_mode) {
  1782. case TPACPI_THERMAL_TPEC_16:
  1783. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  1784. &thermal_temp_input16_group);
  1785. if (res)
  1786. return res;
  1787. break;
  1788. case TPACPI_THERMAL_TPEC_8:
  1789. case TPACPI_THERMAL_ACPI_TMP07:
  1790. case TPACPI_THERMAL_ACPI_UPDT:
  1791. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  1792. &thermal_temp_input8_group);
  1793. if (res)
  1794. return res;
  1795. break;
  1796. case TPACPI_THERMAL_NONE:
  1797. default:
  1798. return 1;
  1799. }
  1800. return 0;
  1801. }
  1802. static void thermal_exit(void)
  1803. {
  1804. switch(thermal_read_mode) {
  1805. case TPACPI_THERMAL_TPEC_16:
  1806. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  1807. &thermal_temp_input16_group);
  1808. break;
  1809. case TPACPI_THERMAL_TPEC_8:
  1810. case TPACPI_THERMAL_ACPI_TMP07:
  1811. case TPACPI_THERMAL_ACPI_UPDT:
  1812. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  1813. &thermal_temp_input16_group);
  1814. break;
  1815. case TPACPI_THERMAL_NONE:
  1816. default:
  1817. break;
  1818. }
  1819. }
  1820. /* idx is zero-based */
  1821. static int thermal_get_sensor(int idx, s32 *value)
  1822. {
  1823. int t;
  1824. s8 tmp;
  1825. char tmpi[5];
  1826. t = TP_EC_THERMAL_TMP0;
  1827. switch (thermal_read_mode) {
  1828. #if TPACPI_MAX_THERMAL_SENSORS >= 16
  1829. case TPACPI_THERMAL_TPEC_16:
  1830. if (idx >= 8 && idx <= 15) {
  1831. t = TP_EC_THERMAL_TMP8;
  1832. idx -= 8;
  1833. }
  1834. /* fallthrough */
  1835. #endif
  1836. case TPACPI_THERMAL_TPEC_8:
  1837. if (idx <= 7) {
  1838. if (!acpi_ec_read(t + idx, &tmp))
  1839. return -EIO;
  1840. *value = tmp * 1000;
  1841. return 0;
  1842. }
  1843. break;
  1844. case TPACPI_THERMAL_ACPI_UPDT:
  1845. if (idx <= 7) {
  1846. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  1847. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  1848. return -EIO;
  1849. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1850. return -EIO;
  1851. *value = (t - 2732) * 100;
  1852. return 0;
  1853. }
  1854. break;
  1855. case TPACPI_THERMAL_ACPI_TMP07:
  1856. if (idx <= 7) {
  1857. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  1858. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1859. return -EIO;
  1860. *value = t * 1000;
  1861. return 0;
  1862. }
  1863. break;
  1864. case TPACPI_THERMAL_NONE:
  1865. default:
  1866. return -ENOSYS;
  1867. }
  1868. return -EINVAL;
  1869. }
  1870. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  1871. {
  1872. int res, i;
  1873. int n;
  1874. n = 8;
  1875. i = 0;
  1876. if (!s)
  1877. return -EINVAL;
  1878. if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
  1879. n = 16;
  1880. for(i = 0 ; i < n; i++) {
  1881. res = thermal_get_sensor(i, &s->temp[i]);
  1882. if (res)
  1883. return res;
  1884. }
  1885. return n;
  1886. }
  1887. static int thermal_read(char *p)
  1888. {
  1889. int len = 0;
  1890. int n, i;
  1891. struct ibm_thermal_sensors_struct t;
  1892. n = thermal_get_sensors(&t);
  1893. if (unlikely(n < 0))
  1894. return n;
  1895. len += sprintf(p + len, "temperatures:\t");
  1896. if (n > 0) {
  1897. for (i = 0; i < (n - 1); i++)
  1898. len += sprintf(p + len, "%d ", t.temp[i] / 1000);
  1899. len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
  1900. } else
  1901. len += sprintf(p + len, "not supported\n");
  1902. return len;
  1903. }
  1904. static struct ibm_struct thermal_driver_data = {
  1905. .name = "thermal",
  1906. .read = thermal_read,
  1907. .exit = thermal_exit,
  1908. };
  1909. /*************************************************************************
  1910. * EC Dump subdriver
  1911. */
  1912. static u8 ecdump_regs[256];
  1913. static int ecdump_read(char *p)
  1914. {
  1915. int len = 0;
  1916. int i, j;
  1917. u8 v;
  1918. len += sprintf(p + len, "EC "
  1919. " +00 +01 +02 +03 +04 +05 +06 +07"
  1920. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  1921. for (i = 0; i < 256; i += 16) {
  1922. len += sprintf(p + len, "EC 0x%02x:", i);
  1923. for (j = 0; j < 16; j++) {
  1924. if (!acpi_ec_read(i + j, &v))
  1925. break;
  1926. if (v != ecdump_regs[i + j])
  1927. len += sprintf(p + len, " *%02x", v);
  1928. else
  1929. len += sprintf(p + len, " %02x", v);
  1930. ecdump_regs[i + j] = v;
  1931. }
  1932. len += sprintf(p + len, "\n");
  1933. if (j != 16)
  1934. break;
  1935. }
  1936. /* These are way too dangerous to advertise openly... */
  1937. #if 0
  1938. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  1939. " (<offset> is 00-ff, <value> is 00-ff)\n");
  1940. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  1941. " (<offset> is 00-ff, <value> is 0-255)\n");
  1942. #endif
  1943. return len;
  1944. }
  1945. static int ecdump_write(char *buf)
  1946. {
  1947. char *cmd;
  1948. int i, v;
  1949. while ((cmd = next_cmd(&buf))) {
  1950. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  1951. /* i and v set */
  1952. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  1953. /* i and v set */
  1954. } else
  1955. return -EINVAL;
  1956. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  1957. if (!acpi_ec_write(i, v))
  1958. return -EIO;
  1959. } else
  1960. return -EINVAL;
  1961. }
  1962. return 0;
  1963. }
  1964. static struct ibm_struct ecdump_driver_data = {
  1965. .name = "ecdump",
  1966. .read = ecdump_read,
  1967. .write = ecdump_write,
  1968. .flags.experimental = 1,
  1969. };
  1970. /*************************************************************************
  1971. * Backlight/brightness subdriver
  1972. */
  1973. static struct backlight_device *ibm_backlight_device = NULL;
  1974. static struct backlight_ops ibm_backlight_data = {
  1975. .get_brightness = brightness_get,
  1976. .update_status = brightness_update_status,
  1977. };
  1978. static int __init brightness_init(struct ibm_init_struct *iibm)
  1979. {
  1980. int b;
  1981. vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
  1982. b = brightness_get(NULL);
  1983. if (b < 0)
  1984. return b;
  1985. ibm_backlight_device = backlight_device_register(
  1986. TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
  1987. &ibm_backlight_data);
  1988. if (IS_ERR(ibm_backlight_device)) {
  1989. printk(IBM_ERR "Could not register backlight device\n");
  1990. return PTR_ERR(ibm_backlight_device);
  1991. }
  1992. vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
  1993. ibm_backlight_device->props.max_brightness = 7;
  1994. ibm_backlight_device->props.brightness = b;
  1995. backlight_update_status(ibm_backlight_device);
  1996. return 0;
  1997. }
  1998. static void brightness_exit(void)
  1999. {
  2000. if (ibm_backlight_device) {
  2001. vdbg_printk(TPACPI_DBG_EXIT,
  2002. "calling backlight_device_unregister()\n");
  2003. backlight_device_unregister(ibm_backlight_device);
  2004. ibm_backlight_device = NULL;
  2005. }
  2006. }
  2007. static int brightness_update_status(struct backlight_device *bd)
  2008. {
  2009. return brightness_set(
  2010. (bd->props.fb_blank == FB_BLANK_UNBLANK &&
  2011. bd->props.power == FB_BLANK_UNBLANK) ?
  2012. bd->props.brightness : 0);
  2013. }
  2014. static int brightness_get(struct backlight_device *bd)
  2015. {
  2016. u8 level;
  2017. if (!acpi_ec_read(brightness_offset, &level))
  2018. return -EIO;
  2019. level &= 0x7;
  2020. return level;
  2021. }
  2022. static int brightness_set(int value)
  2023. {
  2024. int cmos_cmd, inc, i;
  2025. int current_value = brightness_get(NULL);
  2026. value &= 7;
  2027. cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
  2028. inc = value > current_value ? 1 : -1;
  2029. for (i = current_value; i != value; i += inc) {
  2030. if (issue_thinkpad_cmos_command(cmos_cmd))
  2031. return -EIO;
  2032. if (!acpi_ec_write(brightness_offset, i + inc))
  2033. return -EIO;
  2034. }
  2035. return 0;
  2036. }
  2037. static int brightness_read(char *p)
  2038. {
  2039. int len = 0;
  2040. int level;
  2041. if ((level = brightness_get(NULL)) < 0) {
  2042. len += sprintf(p + len, "level:\t\tunreadable\n");
  2043. } else {
  2044. len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
  2045. len += sprintf(p + len, "commands:\tup, down\n");
  2046. len += sprintf(p + len, "commands:\tlevel <level>"
  2047. " (<level> is 0-7)\n");
  2048. }
  2049. return len;
  2050. }
  2051. static int brightness_write(char *buf)
  2052. {
  2053. int level;
  2054. int new_level;
  2055. char *cmd;
  2056. while ((cmd = next_cmd(&buf))) {
  2057. if ((level = brightness_get(NULL)) < 0)
  2058. return level;
  2059. level &= 7;
  2060. if (strlencmp(cmd, "up") == 0) {
  2061. new_level = level == 7 ? 7 : level + 1;
  2062. } else if (strlencmp(cmd, "down") == 0) {
  2063. new_level = level == 0 ? 0 : level - 1;
  2064. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  2065. new_level >= 0 && new_level <= 7) {
  2066. /* new_level set */
  2067. } else
  2068. return -EINVAL;
  2069. brightness_set(new_level);
  2070. }
  2071. return 0;
  2072. }
  2073. static struct ibm_struct brightness_driver_data = {
  2074. .name = "brightness",
  2075. .read = brightness_read,
  2076. .write = brightness_write,
  2077. .exit = brightness_exit,
  2078. };
  2079. /*************************************************************************
  2080. * Volume subdriver
  2081. */
  2082. static int volume_read(char *p)
  2083. {
  2084. int len = 0;
  2085. u8 level;
  2086. if (!acpi_ec_read(volume_offset, &level)) {
  2087. len += sprintf(p + len, "level:\t\tunreadable\n");
  2088. } else {
  2089. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  2090. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  2091. len += sprintf(p + len, "commands:\tup, down, mute\n");
  2092. len += sprintf(p + len, "commands:\tlevel <level>"
  2093. " (<level> is 0-15)\n");
  2094. }
  2095. return len;
  2096. }
  2097. static int volume_write(char *buf)
  2098. {
  2099. int cmos_cmd, inc, i;
  2100. u8 level, mute;
  2101. int new_level, new_mute;
  2102. char *cmd;
  2103. while ((cmd = next_cmd(&buf))) {
  2104. if (!acpi_ec_read(volume_offset, &level))
  2105. return -EIO;
  2106. new_mute = mute = level & 0x40;
  2107. new_level = level = level & 0xf;
  2108. if (strlencmp(cmd, "up") == 0) {
  2109. if (mute)
  2110. new_mute = 0;
  2111. else
  2112. new_level = level == 15 ? 15 : level + 1;
  2113. } else if (strlencmp(cmd, "down") == 0) {
  2114. if (mute)
  2115. new_mute = 0;
  2116. else
  2117. new_level = level == 0 ? 0 : level - 1;
  2118. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  2119. new_level >= 0 && new_level <= 15) {
  2120. /* new_level set */
  2121. } else if (strlencmp(cmd, "mute") == 0) {
  2122. new_mute = 0x40;
  2123. } else
  2124. return -EINVAL;
  2125. if (new_level != level) { /* mute doesn't change */
  2126. cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
  2127. inc = new_level > level ? 1 : -1;
  2128. if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
  2129. !acpi_ec_write(volume_offset, level)))
  2130. return -EIO;
  2131. for (i = level; i != new_level; i += inc)
  2132. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  2133. !acpi_ec_write(volume_offset, i + inc))
  2134. return -EIO;
  2135. if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
  2136. !acpi_ec_write(volume_offset,
  2137. new_level + mute)))
  2138. return -EIO;
  2139. }
  2140. if (new_mute != mute) { /* level doesn't change */
  2141. cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
  2142. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  2143. !acpi_ec_write(volume_offset, level + new_mute))
  2144. return -EIO;
  2145. }
  2146. }
  2147. return 0;
  2148. }
  2149. static struct ibm_struct volume_driver_data = {
  2150. .name = "volume",
  2151. .read = volume_read,
  2152. .write = volume_write,
  2153. };
  2154. /*************************************************************************
  2155. * Fan subdriver
  2156. */
  2157. /*
  2158. * FAN ACCESS MODES
  2159. *
  2160. * TPACPI_FAN_RD_ACPI_GFAN:
  2161. * ACPI GFAN method: returns fan level
  2162. *
  2163. * see TPACPI_FAN_WR_ACPI_SFAN
  2164. * EC 0x2f (HFSP) not available if GFAN exists
  2165. *
  2166. * TPACPI_FAN_WR_ACPI_SFAN:
  2167. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  2168. *
  2169. * EC 0x2f (HFSP) might be available *for reading*, but do not use
  2170. * it for writing.
  2171. *
  2172. * TPACPI_FAN_WR_TPEC:
  2173. * ThinkPad EC register 0x2f (HFSP): fan control loop mode
  2174. * Supported on almost all ThinkPads
  2175. *
  2176. * Fan speed changes of any sort (including those caused by the
  2177. * disengaged mode) are usually done slowly by the firmware as the
  2178. * maximum ammount of fan duty cycle change per second seems to be
  2179. * limited.
  2180. *
  2181. * Reading is not available if GFAN exists.
  2182. * Writing is not available if SFAN exists.
  2183. *
  2184. * Bits
  2185. * 7 automatic mode engaged;
  2186. * (default operation mode of the ThinkPad)
  2187. * fan level is ignored in this mode.
  2188. * 6 full speed mode (takes precedence over bit 7);
  2189. * not available on all thinkpads. May disable
  2190. * the tachometer while the fan controller ramps up
  2191. * the speed (which can take up to a few *minutes*).
  2192. * Speeds up fan to 100% duty-cycle, which is far above
  2193. * the standard RPM levels. It is not impossible that
  2194. * it could cause hardware damage.
  2195. * 5-3 unused in some models. Extra bits for fan level
  2196. * in others, but still useless as all values above
  2197. * 7 map to the same speed as level 7 in these models.
  2198. * 2-0 fan level (0..7 usually)
  2199. * 0x00 = stop
  2200. * 0x07 = max (set when temperatures critical)
  2201. * Some ThinkPads may have other levels, see
  2202. * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  2203. *
  2204. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  2205. * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
  2206. * does so, its initial value is meaningless (0x07).
  2207. *
  2208. * For firmware bugs, refer to:
  2209. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  2210. *
  2211. * ----
  2212. *
  2213. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  2214. * Main fan tachometer reading (in RPM)
  2215. *
  2216. * This register is present on all ThinkPads with a new-style EC, and
  2217. * it is known not to be present on the A21m/e, and T22, as there is
  2218. * something else in offset 0x84 according to the ACPI DSDT. Other
  2219. * ThinkPads from this same time period (and earlier) probably lack the
  2220. * tachometer as well.
  2221. *
  2222. * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
  2223. * was never fixed by IBM to report the EC firmware version string
  2224. * probably support the tachometer (like the early X models), so
  2225. * detecting it is quite hard. We need more data to know for sure.
  2226. *
  2227. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  2228. * might result.
  2229. *
  2230. * FIRMWARE BUG: may go stale while the EC is switching to full speed
  2231. * mode.
  2232. *
  2233. * For firmware bugs, refer to:
  2234. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  2235. *
  2236. * TPACPI_FAN_WR_ACPI_FANS:
  2237. * ThinkPad X31, X40, X41. Not available in the X60.
  2238. *
  2239. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  2240. * high speed. ACPI DSDT seems to map these three speeds to levels
  2241. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  2242. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  2243. *
  2244. * The speeds are stored on handles
  2245. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  2246. *
  2247. * There are three default speed sets, acessible as handles:
  2248. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  2249. *
  2250. * ACPI DSDT switches which set is in use depending on various
  2251. * factors.
  2252. *
  2253. * TPACPI_FAN_WR_TPEC is also available and should be used to
  2254. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  2255. * but the ACPI tables just mention level 7.
  2256. */
  2257. static enum fan_status_access_mode fan_status_access_mode;
  2258. static enum fan_control_access_mode fan_control_access_mode;
  2259. static enum fan_control_commands fan_control_commands;
  2260. static u8 fan_control_initial_status;
  2261. static u8 fan_control_desired_level;
  2262. static void fan_watchdog_fire(struct work_struct *ignored);
  2263. static int fan_watchdog_maxinterval;
  2264. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  2265. IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  2266. IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
  2267. "\\FSPD", /* 600e/x, 770e, 770x */
  2268. ); /* all others */
  2269. IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
  2270. "JFNS", /* 770x-JL */
  2271. ); /* all others */
  2272. /*
  2273. * SYSFS fan layout: hwmon compatible (device)
  2274. *
  2275. * pwm*_enable:
  2276. * 0: "disengaged" mode
  2277. * 1: manual mode
  2278. * 2: native EC "auto" mode (recommended, hardware default)
  2279. *
  2280. * pwm*: set speed in manual mode, ignored otherwise.
  2281. * 0 is level 0; 255 is level 7. Intermediate points done with linear
  2282. * interpolation.
  2283. *
  2284. * fan*_input: tachometer reading, RPM
  2285. *
  2286. *
  2287. * SYSFS fan layout: extensions
  2288. *
  2289. * fan_watchdog (driver):
  2290. * fan watchdog interval in seconds, 0 disables (default), max 120
  2291. */
  2292. /* sysfs fan pwm1_enable ----------------------------------------------- */
  2293. static ssize_t fan_pwm1_enable_show(struct device *dev,
  2294. struct device_attribute *attr,
  2295. char *buf)
  2296. {
  2297. int res, mode;
  2298. u8 status;
  2299. res = fan_get_status_safe(&status);
  2300. if (res)
  2301. return res;
  2302. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  2303. if (status != fan_control_initial_status) {
  2304. tp_features.fan_ctrl_status_undef = 0;
  2305. } else {
  2306. /* Return most likely status. In fact, it
  2307. * might be the only possible status */
  2308. status = TP_EC_FAN_AUTO;
  2309. }
  2310. }
  2311. if (status & TP_EC_FAN_FULLSPEED) {
  2312. mode = 0;
  2313. } else if (status & TP_EC_FAN_AUTO) {
  2314. mode = 2;
  2315. } else
  2316. mode = 1;
  2317. return snprintf(buf, PAGE_SIZE, "%d\n", mode);
  2318. }
  2319. static ssize_t fan_pwm1_enable_store(struct device *dev,
  2320. struct device_attribute *attr,
  2321. const char *buf, size_t count)
  2322. {
  2323. unsigned long t;
  2324. int res, level;
  2325. if (parse_strtoul(buf, 2, &t))
  2326. return -EINVAL;
  2327. switch (t) {
  2328. case 0:
  2329. level = TP_EC_FAN_FULLSPEED;
  2330. break;
  2331. case 1:
  2332. level = TPACPI_FAN_LAST_LEVEL;
  2333. break;
  2334. case 2:
  2335. level = TP_EC_FAN_AUTO;
  2336. break;
  2337. case 3:
  2338. /* reserved for software-controlled auto mode */
  2339. return -ENOSYS;
  2340. default:
  2341. return -EINVAL;
  2342. }
  2343. res = fan_set_level_safe(level);
  2344. if (res == -ENXIO)
  2345. return -EINVAL;
  2346. else if (res < 0)
  2347. return res;
  2348. fan_watchdog_reset();
  2349. return count;
  2350. }
  2351. static struct device_attribute dev_attr_fan_pwm1_enable =
  2352. __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  2353. fan_pwm1_enable_show, fan_pwm1_enable_store);
  2354. /* sysfs fan pwm1 ------------------------------------------------------ */
  2355. static ssize_t fan_pwm1_show(struct device *dev,
  2356. struct device_attribute *attr,
  2357. char *buf)
  2358. {
  2359. int res;
  2360. u8 status;
  2361. res = fan_get_status_safe(&status);
  2362. if (res)
  2363. return res;
  2364. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  2365. if (status != fan_control_initial_status) {
  2366. tp_features.fan_ctrl_status_undef = 0;
  2367. } else {
  2368. status = TP_EC_FAN_AUTO;
  2369. }
  2370. }
  2371. if ((status &
  2372. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
  2373. status = fan_control_desired_level;
  2374. if (status > 7)
  2375. status = 7;
  2376. return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
  2377. }
  2378. static ssize_t fan_pwm1_store(struct device *dev,
  2379. struct device_attribute *attr,
  2380. const char *buf, size_t count)
  2381. {
  2382. unsigned long s;
  2383. int rc;
  2384. u8 status, newlevel;
  2385. if (parse_strtoul(buf, 255, &s))
  2386. return -EINVAL;
  2387. /* scale down from 0-255 to 0-7 */
  2388. newlevel = (s >> 5) & 0x07;
  2389. rc = mutex_lock_interruptible(&fan_mutex);
  2390. if (rc < 0)
  2391. return rc;
  2392. rc = fan_get_status(&status);
  2393. if (!rc && (status &
  2394. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  2395. rc = fan_set_level(newlevel);
  2396. if (rc == -ENXIO)
  2397. rc = -EINVAL;
  2398. else if (!rc) {
  2399. fan_update_desired_level(newlevel);
  2400. fan_watchdog_reset();
  2401. }
  2402. }
  2403. mutex_unlock(&fan_mutex);
  2404. return (rc)? rc : count;
  2405. }
  2406. static struct device_attribute dev_attr_fan_pwm1 =
  2407. __ATTR(pwm1, S_IWUSR | S_IRUGO,
  2408. fan_pwm1_show, fan_pwm1_store);
  2409. /* sysfs fan fan1_input ------------------------------------------------ */
  2410. static ssize_t fan_fan1_input_show(struct device *dev,
  2411. struct device_attribute *attr,
  2412. char *buf)
  2413. {
  2414. int res;
  2415. unsigned int speed;
  2416. res = fan_get_speed(&speed);
  2417. if (res < 0)
  2418. return res;
  2419. return snprintf(buf, PAGE_SIZE, "%u\n", speed);
  2420. }
  2421. static struct device_attribute dev_attr_fan_fan1_input =
  2422. __ATTR(fan1_input, S_IRUGO,
  2423. fan_fan1_input_show, NULL);
  2424. /* sysfs fan fan_watchdog (driver) ------------------------------------- */
  2425. static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
  2426. char *buf)
  2427. {
  2428. return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
  2429. }
  2430. static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
  2431. const char *buf, size_t count)
  2432. {
  2433. unsigned long t;
  2434. if (parse_strtoul(buf, 120, &t))
  2435. return -EINVAL;
  2436. if (!fan_control_allowed)
  2437. return -EPERM;
  2438. fan_watchdog_maxinterval = t;
  2439. fan_watchdog_reset();
  2440. return count;
  2441. }
  2442. static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
  2443. fan_fan_watchdog_show, fan_fan_watchdog_store);
  2444. /* --------------------------------------------------------------------- */
  2445. static struct attribute *fan_attributes[] = {
  2446. &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
  2447. &dev_attr_fan_fan1_input.attr,
  2448. NULL
  2449. };
  2450. static const struct attribute_group fan_attr_group = {
  2451. .attrs = fan_attributes,
  2452. };
  2453. static int __init fan_init(struct ibm_init_struct *iibm)
  2454. {
  2455. int rc;
  2456. vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
  2457. mutex_init(&fan_mutex);
  2458. fan_status_access_mode = TPACPI_FAN_NONE;
  2459. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  2460. fan_control_commands = 0;
  2461. fan_watchdog_maxinterval = 0;
  2462. tp_features.fan_ctrl_status_undef = 0;
  2463. fan_control_desired_level = 7;
  2464. IBM_ACPIHANDLE_INIT(fans);
  2465. IBM_ACPIHANDLE_INIT(gfan);
  2466. IBM_ACPIHANDLE_INIT(sfan);
  2467. if (gfan_handle) {
  2468. /* 570, 600e/x, 770e, 770x */
  2469. fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
  2470. } else {
  2471. /* all other ThinkPads: note that even old-style
  2472. * ThinkPad ECs supports the fan control register */
  2473. if (likely(acpi_ec_read(fan_status_offset,
  2474. &fan_control_initial_status))) {
  2475. fan_status_access_mode = TPACPI_FAN_RD_TPEC;
  2476. /* In some ThinkPads, neither the EC nor the ACPI
  2477. * DSDT initialize the fan status, and it ends up
  2478. * being set to 0x07 when it *could* be either
  2479. * 0x07 or 0x80.
  2480. *
  2481. * Enable for TP-1Y (T43), TP-78 (R51e),
  2482. * TP-76 (R52), TP-70 (T43, R52), which are known
  2483. * to be buggy. */
  2484. if (fan_control_initial_status == 0x07 &&
  2485. ibm_thinkpad_ec_found &&
  2486. ((ibm_thinkpad_ec_found[0] == '1' &&
  2487. ibm_thinkpad_ec_found[1] == 'Y') ||
  2488. (ibm_thinkpad_ec_found[0] == '7' &&
  2489. (ibm_thinkpad_ec_found[1] == '6' ||
  2490. ibm_thinkpad_ec_found[1] == '8' ||
  2491. ibm_thinkpad_ec_found[1] == '0'))
  2492. )) {
  2493. printk(IBM_NOTICE
  2494. "fan_init: initial fan status is "
  2495. "unknown, assuming it is in auto "
  2496. "mode\n");
  2497. tp_features.fan_ctrl_status_undef = 1;
  2498. }
  2499. } else {
  2500. printk(IBM_ERR
  2501. "ThinkPad ACPI EC access misbehaving, "
  2502. "fan status and control unavailable\n");
  2503. return 1;
  2504. }
  2505. }
  2506. if (sfan_handle) {
  2507. /* 570, 770x-JL */
  2508. fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
  2509. fan_control_commands |=
  2510. TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
  2511. } else {
  2512. if (!gfan_handle) {
  2513. /* gfan without sfan means no fan control */
  2514. /* all other models implement TP EC 0x2f control */
  2515. if (fans_handle) {
  2516. /* X31, X40, X41 */
  2517. fan_control_access_mode =
  2518. TPACPI_FAN_WR_ACPI_FANS;
  2519. fan_control_commands |=
  2520. TPACPI_FAN_CMD_SPEED |
  2521. TPACPI_FAN_CMD_LEVEL |
  2522. TPACPI_FAN_CMD_ENABLE;
  2523. } else {
  2524. fan_control_access_mode = TPACPI_FAN_WR_TPEC;
  2525. fan_control_commands |=
  2526. TPACPI_FAN_CMD_LEVEL |
  2527. TPACPI_FAN_CMD_ENABLE;
  2528. }
  2529. }
  2530. }
  2531. vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
  2532. str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
  2533. fan_control_access_mode != TPACPI_FAN_WR_NONE),
  2534. fan_status_access_mode, fan_control_access_mode);
  2535. /* fan control master switch */
  2536. if (!fan_control_allowed) {
  2537. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  2538. fan_control_commands = 0;
  2539. dbg_printk(TPACPI_DBG_INIT,
  2540. "fan control features disabled by parameter\n");
  2541. }
  2542. /* update fan_control_desired_level */
  2543. if (fan_status_access_mode != TPACPI_FAN_NONE)
  2544. fan_get_status_safe(NULL);
  2545. if (fan_status_access_mode != TPACPI_FAN_NONE ||
  2546. fan_control_access_mode != TPACPI_FAN_WR_NONE) {
  2547. rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  2548. &fan_attr_group);
  2549. if (!(rc < 0))
  2550. rc = driver_create_file(&tpacpi_pdriver.driver,
  2551. &driver_attr_fan_watchdog);
  2552. if (rc < 0)
  2553. return rc;
  2554. return 0;
  2555. } else
  2556. return 1;
  2557. }
  2558. /*
  2559. * Call with fan_mutex held
  2560. */
  2561. static void fan_update_desired_level(u8 status)
  2562. {
  2563. if ((status &
  2564. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  2565. if (status > 7)
  2566. fan_control_desired_level = 7;
  2567. else
  2568. fan_control_desired_level = status;
  2569. }
  2570. }
  2571. static int fan_get_status(u8 *status)
  2572. {
  2573. u8 s;
  2574. /* TODO:
  2575. * Add TPACPI_FAN_RD_ACPI_FANS ? */
  2576. switch (fan_status_access_mode) {
  2577. case TPACPI_FAN_RD_ACPI_GFAN:
  2578. /* 570, 600e/x, 770e, 770x */
  2579. if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
  2580. return -EIO;
  2581. if (likely(status))
  2582. *status = s & 0x07;
  2583. break;
  2584. case TPACPI_FAN_RD_TPEC:
  2585. /* all except 570, 600e/x, 770e, 770x */
  2586. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  2587. return -EIO;
  2588. if (likely(status))
  2589. *status = s;
  2590. break;
  2591. default:
  2592. return -ENXIO;
  2593. }
  2594. return 0;
  2595. }
  2596. static int fan_get_status_safe(u8 *status)
  2597. {
  2598. int rc;
  2599. u8 s;
  2600. rc = mutex_lock_interruptible(&fan_mutex);
  2601. if (rc < 0)
  2602. return rc;
  2603. rc = fan_get_status(&s);
  2604. if (!rc)
  2605. fan_update_desired_level(s);
  2606. mutex_unlock(&fan_mutex);
  2607. if (status)
  2608. *status = s;
  2609. return rc;
  2610. }
  2611. static void fan_exit(void)
  2612. {
  2613. vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
  2614. /* FIXME: can we really do this unconditionally? */
  2615. sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
  2616. driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
  2617. cancel_delayed_work(&fan_watchdog_task);
  2618. flush_scheduled_work();
  2619. }
  2620. static int fan_get_speed(unsigned int *speed)
  2621. {
  2622. u8 hi, lo;
  2623. switch (fan_status_access_mode) {
  2624. case TPACPI_FAN_RD_TPEC:
  2625. /* all except 570, 600e/x, 770e, 770x */
  2626. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  2627. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  2628. return -EIO;
  2629. if (likely(speed))
  2630. *speed = (hi << 8) | lo;
  2631. break;
  2632. default:
  2633. return -ENXIO;
  2634. }
  2635. return 0;
  2636. }
  2637. static void fan_watchdog_fire(struct work_struct *ignored)
  2638. {
  2639. int rc;
  2640. printk(IBM_NOTICE "fan watchdog: enabling fan\n");
  2641. rc = fan_set_enable();
  2642. if (rc < 0) {
  2643. printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
  2644. "will try again later...\n", -rc);
  2645. /* reschedule for later */
  2646. fan_watchdog_reset();
  2647. }
  2648. }
  2649. static void fan_watchdog_reset(void)
  2650. {
  2651. static int fan_watchdog_active = 0;
  2652. if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
  2653. return;
  2654. if (fan_watchdog_active)
  2655. cancel_delayed_work(&fan_watchdog_task);
  2656. if (fan_watchdog_maxinterval > 0) {
  2657. fan_watchdog_active = 1;
  2658. if (!schedule_delayed_work(&fan_watchdog_task,
  2659. msecs_to_jiffies(fan_watchdog_maxinterval
  2660. * 1000))) {
  2661. printk(IBM_ERR "failed to schedule the fan watchdog, "
  2662. "watchdog will not trigger\n");
  2663. }
  2664. } else
  2665. fan_watchdog_active = 0;
  2666. }
  2667. static int fan_set_level(int level)
  2668. {
  2669. if (!fan_control_allowed)
  2670. return -EPERM;
  2671. switch (fan_control_access_mode) {
  2672. case TPACPI_FAN_WR_ACPI_SFAN:
  2673. if (level >= 0 && level <= 7) {
  2674. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  2675. return -EIO;
  2676. } else
  2677. return -EINVAL;
  2678. break;
  2679. case TPACPI_FAN_WR_ACPI_FANS:
  2680. case TPACPI_FAN_WR_TPEC:
  2681. if ((level != TP_EC_FAN_AUTO) &&
  2682. (level != TP_EC_FAN_FULLSPEED) &&
  2683. ((level < 0) || (level > 7)))
  2684. return -EINVAL;
  2685. /* safety net should the EC not support AUTO
  2686. * or FULLSPEED mode bits and just ignore them */
  2687. if (level & TP_EC_FAN_FULLSPEED)
  2688. level |= 7; /* safety min speed 7 */
  2689. else if (level & TP_EC_FAN_FULLSPEED)
  2690. level |= 4; /* safety min speed 4 */
  2691. if (!acpi_ec_write(fan_status_offset, level))
  2692. return -EIO;
  2693. else
  2694. tp_features.fan_ctrl_status_undef = 0;
  2695. break;
  2696. default:
  2697. return -ENXIO;
  2698. }
  2699. return 0;
  2700. }
  2701. static int fan_set_level_safe(int level)
  2702. {
  2703. int rc;
  2704. if (!fan_control_allowed)
  2705. return -EPERM;
  2706. rc = mutex_lock_interruptible(&fan_mutex);
  2707. if (rc < 0)
  2708. return rc;
  2709. if (level == TPACPI_FAN_LAST_LEVEL)
  2710. level = fan_control_desired_level;
  2711. rc = fan_set_level(level);
  2712. if (!rc)
  2713. fan_update_desired_level(level);
  2714. mutex_unlock(&fan_mutex);
  2715. return rc;
  2716. }
  2717. static int fan_set_enable(void)
  2718. {
  2719. u8 s;
  2720. int rc;
  2721. if (!fan_control_allowed)
  2722. return -EPERM;
  2723. rc = mutex_lock_interruptible(&fan_mutex);
  2724. if (rc < 0)
  2725. return rc;
  2726. switch (fan_control_access_mode) {
  2727. case TPACPI_FAN_WR_ACPI_FANS:
  2728. case TPACPI_FAN_WR_TPEC:
  2729. rc = fan_get_status(&s);
  2730. if (rc < 0)
  2731. break;
  2732. /* Don't go out of emergency fan mode */
  2733. if (s != 7) {
  2734. s &= 0x07;
  2735. s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
  2736. }
  2737. if (!acpi_ec_write(fan_status_offset, s))
  2738. rc = -EIO;
  2739. else {
  2740. tp_features.fan_ctrl_status_undef = 0;
  2741. rc = 0;
  2742. }
  2743. break;
  2744. case TPACPI_FAN_WR_ACPI_SFAN:
  2745. rc = fan_get_status(&s);
  2746. if (rc < 0)
  2747. break;
  2748. s &= 0x07;
  2749. /* Set fan to at least level 4 */
  2750. s |= 4;
  2751. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  2752. rc= -EIO;
  2753. else
  2754. rc = 0;
  2755. break;
  2756. default:
  2757. rc = -ENXIO;
  2758. }
  2759. mutex_unlock(&fan_mutex);
  2760. return rc;
  2761. }
  2762. static int fan_set_disable(void)
  2763. {
  2764. int rc;
  2765. if (!fan_control_allowed)
  2766. return -EPERM;
  2767. rc = mutex_lock_interruptible(&fan_mutex);
  2768. if (rc < 0)
  2769. return rc;
  2770. rc = 0;
  2771. switch (fan_control_access_mode) {
  2772. case TPACPI_FAN_WR_ACPI_FANS:
  2773. case TPACPI_FAN_WR_TPEC:
  2774. if (!acpi_ec_write(fan_status_offset, 0x00))
  2775. rc = -EIO;
  2776. else {
  2777. fan_control_desired_level = 0;
  2778. tp_features.fan_ctrl_status_undef = 0;
  2779. }
  2780. break;
  2781. case TPACPI_FAN_WR_ACPI_SFAN:
  2782. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  2783. rc = -EIO;
  2784. else
  2785. fan_control_desired_level = 0;
  2786. break;
  2787. default:
  2788. rc = -ENXIO;
  2789. }
  2790. mutex_unlock(&fan_mutex);
  2791. return rc;
  2792. }
  2793. static int fan_set_speed(int speed)
  2794. {
  2795. int rc;
  2796. if (!fan_control_allowed)
  2797. return -EPERM;
  2798. rc = mutex_lock_interruptible(&fan_mutex);
  2799. if (rc < 0)
  2800. return rc;
  2801. rc = 0;
  2802. switch (fan_control_access_mode) {
  2803. case TPACPI_FAN_WR_ACPI_FANS:
  2804. if (speed >= 0 && speed <= 65535) {
  2805. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  2806. speed, speed, speed))
  2807. rc = -EIO;
  2808. } else
  2809. rc = -EINVAL;
  2810. break;
  2811. default:
  2812. rc = -ENXIO;
  2813. }
  2814. mutex_unlock(&fan_mutex);
  2815. return rc;
  2816. }
  2817. static int fan_read(char *p)
  2818. {
  2819. int len = 0;
  2820. int rc;
  2821. u8 status;
  2822. unsigned int speed = 0;
  2823. switch (fan_status_access_mode) {
  2824. case TPACPI_FAN_RD_ACPI_GFAN:
  2825. /* 570, 600e/x, 770e, 770x */
  2826. if ((rc = fan_get_status_safe(&status)) < 0)
  2827. return rc;
  2828. len += sprintf(p + len, "status:\t\t%s\n"
  2829. "level:\t\t%d\n",
  2830. (status != 0) ? "enabled" : "disabled", status);
  2831. break;
  2832. case TPACPI_FAN_RD_TPEC:
  2833. /* all except 570, 600e/x, 770e, 770x */
  2834. if ((rc = fan_get_status_safe(&status)) < 0)
  2835. return rc;
  2836. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  2837. if (status != fan_control_initial_status)
  2838. tp_features.fan_ctrl_status_undef = 0;
  2839. else
  2840. /* Return most likely status. In fact, it
  2841. * might be the only possible status */
  2842. status = TP_EC_FAN_AUTO;
  2843. }
  2844. len += sprintf(p + len, "status:\t\t%s\n",
  2845. (status != 0) ? "enabled" : "disabled");
  2846. if ((rc = fan_get_speed(&speed)) < 0)
  2847. return rc;
  2848. len += sprintf(p + len, "speed:\t\t%d\n", speed);
  2849. if (status & TP_EC_FAN_FULLSPEED)
  2850. /* Disengaged mode takes precedence */
  2851. len += sprintf(p + len, "level:\t\tdisengaged\n");
  2852. else if (status & TP_EC_FAN_AUTO)
  2853. len += sprintf(p + len, "level:\t\tauto\n");
  2854. else
  2855. len += sprintf(p + len, "level:\t\t%d\n", status);
  2856. break;
  2857. case TPACPI_FAN_NONE:
  2858. default:
  2859. len += sprintf(p + len, "status:\t\tnot supported\n");
  2860. }
  2861. if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
  2862. len += sprintf(p + len, "commands:\tlevel <level>");
  2863. switch (fan_control_access_mode) {
  2864. case TPACPI_FAN_WR_ACPI_SFAN:
  2865. len += sprintf(p + len, " (<level> is 0-7)\n");
  2866. break;
  2867. default:
  2868. len += sprintf(p + len, " (<level> is 0-7, "
  2869. "auto, disengaged, full-speed)\n");
  2870. break;
  2871. }
  2872. }
  2873. if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
  2874. len += sprintf(p + len, "commands:\tenable, disable\n"
  2875. "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
  2876. "1-120 (seconds))\n");
  2877. if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
  2878. len += sprintf(p + len, "commands:\tspeed <speed>"
  2879. " (<speed> is 0-65535)\n");
  2880. return len;
  2881. }
  2882. static int fan_write_cmd_level(const char *cmd, int *rc)
  2883. {
  2884. int level;
  2885. if (strlencmp(cmd, "level auto") == 0)
  2886. level = TP_EC_FAN_AUTO;
  2887. else if ((strlencmp(cmd, "level disengaged") == 0) |
  2888. (strlencmp(cmd, "level full-speed") == 0))
  2889. level = TP_EC_FAN_FULLSPEED;
  2890. else if (sscanf(cmd, "level %d", &level) != 1)
  2891. return 0;
  2892. if ((*rc = fan_set_level_safe(level)) == -ENXIO)
  2893. printk(IBM_ERR "level command accepted for unsupported "
  2894. "access mode %d", fan_control_access_mode);
  2895. return 1;
  2896. }
  2897. static int fan_write_cmd_enable(const char *cmd, int *rc)
  2898. {
  2899. if (strlencmp(cmd, "enable") != 0)
  2900. return 0;
  2901. if ((*rc = fan_set_enable()) == -ENXIO)
  2902. printk(IBM_ERR "enable command accepted for unsupported "
  2903. "access mode %d", fan_control_access_mode);
  2904. return 1;
  2905. }
  2906. static int fan_write_cmd_disable(const char *cmd, int *rc)
  2907. {
  2908. if (strlencmp(cmd, "disable") != 0)
  2909. return 0;
  2910. if ((*rc = fan_set_disable()) == -ENXIO)
  2911. printk(IBM_ERR "disable command accepted for unsupported "
  2912. "access mode %d", fan_control_access_mode);
  2913. return 1;
  2914. }
  2915. static int fan_write_cmd_speed(const char *cmd, int *rc)
  2916. {
  2917. int speed;
  2918. /* TODO:
  2919. * Support speed <low> <medium> <high> ? */
  2920. if (sscanf(cmd, "speed %d", &speed) != 1)
  2921. return 0;
  2922. if ((*rc = fan_set_speed(speed)) == -ENXIO)
  2923. printk(IBM_ERR "speed command accepted for unsupported "
  2924. "access mode %d", fan_control_access_mode);
  2925. return 1;
  2926. }
  2927. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  2928. {
  2929. int interval;
  2930. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  2931. return 0;
  2932. if (interval < 0 || interval > 120)
  2933. *rc = -EINVAL;
  2934. else
  2935. fan_watchdog_maxinterval = interval;
  2936. return 1;
  2937. }
  2938. static int fan_write(char *buf)
  2939. {
  2940. char *cmd;
  2941. int rc = 0;
  2942. while (!rc && (cmd = next_cmd(&buf))) {
  2943. if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
  2944. fan_write_cmd_level(cmd, &rc)) &&
  2945. !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
  2946. (fan_write_cmd_enable(cmd, &rc) ||
  2947. fan_write_cmd_disable(cmd, &rc) ||
  2948. fan_write_cmd_watchdog(cmd, &rc))) &&
  2949. !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
  2950. fan_write_cmd_speed(cmd, &rc))
  2951. )
  2952. rc = -EINVAL;
  2953. else if (!rc)
  2954. fan_watchdog_reset();
  2955. }
  2956. return rc;
  2957. }
  2958. static struct ibm_struct fan_driver_data = {
  2959. .name = "fan",
  2960. .read = fan_read,
  2961. .write = fan_write,
  2962. .exit = fan_exit,
  2963. };
  2964. /****************************************************************************
  2965. ****************************************************************************
  2966. *
  2967. * Infrastructure
  2968. *
  2969. ****************************************************************************
  2970. ****************************************************************************/
  2971. /* /proc support */
  2972. static struct proc_dir_entry *proc_dir = NULL;
  2973. /* Subdriver registry */
  2974. static LIST_HEAD(tpacpi_all_drivers);
  2975. /*
  2976. * Module and infrastructure proble, init and exit handling
  2977. */
  2978. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  2979. static const char * __init str_supported(int is_supported)
  2980. {
  2981. static char text_unsupported[] __initdata = "not supported";
  2982. return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
  2983. }
  2984. #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
  2985. static int __init ibm_init(struct ibm_init_struct *iibm)
  2986. {
  2987. int ret;
  2988. struct ibm_struct *ibm = iibm->data;
  2989. struct proc_dir_entry *entry;
  2990. BUG_ON(ibm == NULL);
  2991. INIT_LIST_HEAD(&ibm->all_drivers);
  2992. if (ibm->flags.experimental && !experimental)
  2993. return 0;
  2994. dbg_printk(TPACPI_DBG_INIT,
  2995. "probing for %s\n", ibm->name);
  2996. if (iibm->init) {
  2997. ret = iibm->init(iibm);
  2998. if (ret > 0)
  2999. return 0; /* probe failed */
  3000. if (ret)
  3001. return ret;
  3002. ibm->flags.init_called = 1;
  3003. }
  3004. if (ibm->acpi) {
  3005. if (ibm->acpi->hid) {
  3006. ret = register_tpacpi_subdriver(ibm);
  3007. if (ret)
  3008. goto err_out;
  3009. }
  3010. if (ibm->acpi->notify) {
  3011. ret = setup_acpi_notify(ibm);
  3012. if (ret == -ENODEV) {
  3013. printk(IBM_NOTICE "disabling subdriver %s\n",
  3014. ibm->name);
  3015. ret = 0;
  3016. goto err_out;
  3017. }
  3018. if (ret < 0)
  3019. goto err_out;
  3020. }
  3021. }
  3022. dbg_printk(TPACPI_DBG_INIT,
  3023. "%s installed\n", ibm->name);
  3024. if (ibm->read) {
  3025. entry = create_proc_entry(ibm->name,
  3026. S_IFREG | S_IRUGO | S_IWUSR,
  3027. proc_dir);
  3028. if (!entry) {
  3029. printk(IBM_ERR "unable to create proc entry %s\n",
  3030. ibm->name);
  3031. ret = -ENODEV;
  3032. goto err_out;
  3033. }
  3034. entry->owner = THIS_MODULE;
  3035. entry->data = ibm;
  3036. entry->read_proc = &dispatch_procfs_read;
  3037. if (ibm->write)
  3038. entry->write_proc = &dispatch_procfs_write;
  3039. ibm->flags.proc_created = 1;
  3040. }
  3041. list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
  3042. return 0;
  3043. err_out:
  3044. dbg_printk(TPACPI_DBG_INIT,
  3045. "%s: at error exit path with result %d\n",
  3046. ibm->name, ret);
  3047. ibm_exit(ibm);
  3048. return (ret < 0)? ret : 0;
  3049. }
  3050. static void ibm_exit(struct ibm_struct *ibm)
  3051. {
  3052. dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
  3053. list_del_init(&ibm->all_drivers);
  3054. if (ibm->flags.acpi_notify_installed) {
  3055. dbg_printk(TPACPI_DBG_EXIT,
  3056. "%s: acpi_remove_notify_handler\n", ibm->name);
  3057. BUG_ON(!ibm->acpi);
  3058. acpi_remove_notify_handler(*ibm->acpi->handle,
  3059. ibm->acpi->type,
  3060. dispatch_acpi_notify);
  3061. ibm->flags.acpi_notify_installed = 0;
  3062. ibm->flags.acpi_notify_installed = 0;
  3063. }
  3064. if (ibm->flags.proc_created) {
  3065. dbg_printk(TPACPI_DBG_EXIT,
  3066. "%s: remove_proc_entry\n", ibm->name);
  3067. remove_proc_entry(ibm->name, proc_dir);
  3068. ibm->flags.proc_created = 0;
  3069. }
  3070. if (ibm->flags.acpi_driver_registered) {
  3071. dbg_printk(TPACPI_DBG_EXIT,
  3072. "%s: acpi_bus_unregister_driver\n", ibm->name);
  3073. BUG_ON(!ibm->acpi);
  3074. acpi_bus_unregister_driver(ibm->acpi->driver);
  3075. kfree(ibm->acpi->driver);
  3076. ibm->acpi->driver = NULL;
  3077. ibm->flags.acpi_driver_registered = 0;
  3078. }
  3079. if (ibm->flags.init_called && ibm->exit) {
  3080. ibm->exit();
  3081. ibm->flags.init_called = 0;
  3082. }
  3083. dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
  3084. }
  3085. /* Probing */
  3086. static char *ibm_thinkpad_ec_found = NULL;
  3087. static char* __init check_dmi_for_ec(void)
  3088. {
  3089. struct dmi_device *dev = NULL;
  3090. char ec_fw_string[18];
  3091. /*
  3092. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  3093. * X32 or newer, all Z series; Some models must have an
  3094. * up-to-date BIOS or they will not be detected.
  3095. *
  3096. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  3097. */
  3098. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  3099. if (sscanf(dev->name,
  3100. "IBM ThinkPad Embedded Controller -[%17c",
  3101. ec_fw_string) == 1) {
  3102. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  3103. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  3104. return kstrdup(ec_fw_string, GFP_KERNEL);
  3105. }
  3106. }
  3107. return NULL;
  3108. }
  3109. static int __init probe_for_thinkpad(void)
  3110. {
  3111. int is_thinkpad;
  3112. if (acpi_disabled)
  3113. return -ENODEV;
  3114. /*
  3115. * Non-ancient models have better DMI tagging, but very old models
  3116. * don't.
  3117. */
  3118. is_thinkpad = dmi_name_in_vendors("ThinkPad");
  3119. /* ec is required because many other handles are relative to it */
  3120. IBM_ACPIHANDLE_INIT(ec);
  3121. if (!ec_handle) {
  3122. if (is_thinkpad)
  3123. printk(IBM_ERR
  3124. "Not yet supported ThinkPad detected!\n");
  3125. return -ENODEV;
  3126. }
  3127. /*
  3128. * Risks a regression on very old machines, but reduces potential
  3129. * false positives a damn great deal
  3130. */
  3131. if (!is_thinkpad)
  3132. is_thinkpad = dmi_name_in_vendors("IBM");
  3133. if (!is_thinkpad && !force_load)
  3134. return -ENODEV;
  3135. return 0;
  3136. }
  3137. /* Module init, exit, parameters */
  3138. static struct ibm_init_struct ibms_init[] __initdata = {
  3139. {
  3140. .init = thinkpad_acpi_driver_init,
  3141. .data = &thinkpad_acpi_driver_data,
  3142. },
  3143. {
  3144. .init = hotkey_init,
  3145. .data = &hotkey_driver_data,
  3146. },
  3147. {
  3148. .init = bluetooth_init,
  3149. .data = &bluetooth_driver_data,
  3150. },
  3151. {
  3152. .init = wan_init,
  3153. .data = &wan_driver_data,
  3154. },
  3155. {
  3156. .init = video_init,
  3157. .data = &video_driver_data,
  3158. },
  3159. {
  3160. .init = light_init,
  3161. .data = &light_driver_data,
  3162. },
  3163. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  3164. {
  3165. .init = dock_init,
  3166. .data = &dock_driver_data[0],
  3167. },
  3168. {
  3169. .data = &dock_driver_data[1],
  3170. },
  3171. #endif
  3172. #ifdef CONFIG_THINKPAD_ACPI_BAY
  3173. {
  3174. .init = bay_init,
  3175. .data = &bay_driver_data,
  3176. },
  3177. #endif
  3178. {
  3179. .init = cmos_init,
  3180. .data = &cmos_driver_data,
  3181. },
  3182. {
  3183. .init = led_init,
  3184. .data = &led_driver_data,
  3185. },
  3186. {
  3187. .init = beep_init,
  3188. .data = &beep_driver_data,
  3189. },
  3190. {
  3191. .init = thermal_init,
  3192. .data = &thermal_driver_data,
  3193. },
  3194. {
  3195. .data = &ecdump_driver_data,
  3196. },
  3197. {
  3198. .init = brightness_init,
  3199. .data = &brightness_driver_data,
  3200. },
  3201. {
  3202. .data = &volume_driver_data,
  3203. },
  3204. {
  3205. .init = fan_init,
  3206. .data = &fan_driver_data,
  3207. },
  3208. };
  3209. static int __init set_ibm_param(const char *val, struct kernel_param *kp)
  3210. {
  3211. unsigned int i;
  3212. struct ibm_struct *ibm;
  3213. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  3214. ibm = ibms_init[i].data;
  3215. BUG_ON(ibm == NULL);
  3216. if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
  3217. if (strlen(val) > sizeof(ibms_init[i].param) - 2)
  3218. return -ENOSPC;
  3219. strcpy(ibms_init[i].param, val);
  3220. strcat(ibms_init[i].param, ",");
  3221. return 0;
  3222. }
  3223. }
  3224. return -EINVAL;
  3225. }
  3226. static int experimental;
  3227. module_param(experimental, int, 0);
  3228. static u32 dbg_level;
  3229. module_param_named(debug, dbg_level, uint, 0);
  3230. static int force_load;
  3231. module_param(force_load, int, 0);
  3232. static int fan_control_allowed;
  3233. module_param_named(fan_control, fan_control_allowed, int, 0);
  3234. #define IBM_PARAM(feature) \
  3235. module_param_call(feature, set_ibm_param, NULL, NULL, 0)
  3236. IBM_PARAM(hotkey);
  3237. IBM_PARAM(bluetooth);
  3238. IBM_PARAM(video);
  3239. IBM_PARAM(light);
  3240. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  3241. IBM_PARAM(dock);
  3242. #endif
  3243. #ifdef CONFIG_THINKPAD_ACPI_BAY
  3244. IBM_PARAM(bay);
  3245. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  3246. IBM_PARAM(cmos);
  3247. IBM_PARAM(led);
  3248. IBM_PARAM(beep);
  3249. IBM_PARAM(ecdump);
  3250. IBM_PARAM(brightness);
  3251. IBM_PARAM(volume);
  3252. IBM_PARAM(fan);
  3253. static int __init thinkpad_acpi_module_init(void)
  3254. {
  3255. int ret, i;
  3256. /* Driver-level probe */
  3257. ret = probe_for_thinkpad();
  3258. if (ret)
  3259. return ret;
  3260. /* Driver initialization */
  3261. ibm_thinkpad_ec_found = check_dmi_for_ec();
  3262. IBM_ACPIHANDLE_INIT(ecrd);
  3263. IBM_ACPIHANDLE_INIT(ecwr);
  3264. proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
  3265. if (!proc_dir) {
  3266. printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
  3267. thinkpad_acpi_module_exit();
  3268. return -ENODEV;
  3269. }
  3270. proc_dir->owner = THIS_MODULE;
  3271. ret = platform_driver_register(&tpacpi_pdriver);
  3272. if (ret) {
  3273. printk(IBM_ERR "unable to register platform driver\n");
  3274. thinkpad_acpi_module_exit();
  3275. return ret;
  3276. }
  3277. ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
  3278. if (ret) {
  3279. printk(IBM_ERR "unable to create sysfs driver attributes\n");
  3280. thinkpad_acpi_module_exit();
  3281. return ret;
  3282. }
  3283. /* Device initialization */
  3284. tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
  3285. NULL, 0);
  3286. if (IS_ERR(tpacpi_pdev)) {
  3287. ret = PTR_ERR(tpacpi_pdev);
  3288. tpacpi_pdev = NULL;
  3289. printk(IBM_ERR "unable to register platform device\n");
  3290. thinkpad_acpi_module_exit();
  3291. return ret;
  3292. }
  3293. tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
  3294. if (IS_ERR(tpacpi_hwmon)) {
  3295. ret = PTR_ERR(tpacpi_hwmon);
  3296. tpacpi_hwmon = NULL;
  3297. printk(IBM_ERR "unable to register hwmon device\n");
  3298. thinkpad_acpi_module_exit();
  3299. return ret;
  3300. }
  3301. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  3302. ret = ibm_init(&ibms_init[i]);
  3303. if (ret >= 0 && *ibms_init[i].param)
  3304. ret = ibms_init[i].data->write(ibms_init[i].param);
  3305. if (ret < 0) {
  3306. thinkpad_acpi_module_exit();
  3307. return ret;
  3308. }
  3309. }
  3310. return 0;
  3311. }
  3312. static void thinkpad_acpi_module_exit(void)
  3313. {
  3314. struct ibm_struct *ibm, *itmp;
  3315. list_for_each_entry_safe_reverse(ibm, itmp,
  3316. &tpacpi_all_drivers,
  3317. all_drivers) {
  3318. ibm_exit(ibm);
  3319. }
  3320. dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
  3321. if (tpacpi_hwmon)
  3322. hwmon_device_unregister(tpacpi_hwmon);
  3323. if (tpacpi_pdev)
  3324. platform_device_unregister(tpacpi_pdev);
  3325. tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
  3326. platform_driver_unregister(&tpacpi_pdriver);
  3327. if (proc_dir)
  3328. remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
  3329. kfree(ibm_thinkpad_ec_found);
  3330. }
  3331. module_init(thinkpad_acpi_module_init);
  3332. module_exit(thinkpad_acpi_module_exit);