thinkpad_acpi.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956
  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. static int __init cmos_init(struct ibm_init_struct *iibm)
  1428. {
  1429. vdbg_printk(TPACPI_DBG_INIT,
  1430. "initializing cmos commands subdriver\n");
  1431. IBM_ACPIHANDLE_INIT(cmos);
  1432. vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
  1433. str_supported(cmos_handle != NULL));
  1434. return (cmos_handle)? 0 : 1;
  1435. }
  1436. static int cmos_read(char *p)
  1437. {
  1438. int len = 0;
  1439. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  1440. R30, R31, T20-22, X20-21 */
  1441. if (!cmos_handle)
  1442. len += sprintf(p + len, "status:\t\tnot supported\n");
  1443. else {
  1444. len += sprintf(p + len, "status:\t\tsupported\n");
  1445. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
  1446. }
  1447. return len;
  1448. }
  1449. static int cmos_write(char *buf)
  1450. {
  1451. char *cmd;
  1452. int cmos_cmd, res;
  1453. while ((cmd = next_cmd(&buf))) {
  1454. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  1455. cmos_cmd >= 0 && cmos_cmd <= 21) {
  1456. /* cmos_cmd set */
  1457. } else
  1458. return -EINVAL;
  1459. res = issue_thinkpad_cmos_command(cmos_cmd);
  1460. if (res)
  1461. return res;
  1462. }
  1463. return 0;
  1464. }
  1465. static struct ibm_struct cmos_driver_data = {
  1466. .name = "cmos",
  1467. .read = cmos_read,
  1468. .write = cmos_write,
  1469. };
  1470. /*************************************************************************
  1471. * LED subdriver
  1472. */
  1473. static enum led_access_mode led_supported;
  1474. IBM_HANDLE(led, ec, "SLED", /* 570 */
  1475. "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1476. "LED", /* all others */
  1477. ); /* R30, R31 */
  1478. static int __init led_init(struct ibm_init_struct *iibm)
  1479. {
  1480. vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
  1481. IBM_ACPIHANDLE_INIT(led);
  1482. if (!led_handle)
  1483. /* led not supported on R30, R31 */
  1484. led_supported = TPACPI_LED_NONE;
  1485. else if (strlencmp(led_path, "SLED") == 0)
  1486. /* 570 */
  1487. led_supported = TPACPI_LED_570;
  1488. else if (strlencmp(led_path, "SYSL") == 0)
  1489. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1490. led_supported = TPACPI_LED_OLD;
  1491. else
  1492. /* all others */
  1493. led_supported = TPACPI_LED_NEW;
  1494. vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
  1495. str_supported(led_supported), led_supported);
  1496. return (led_supported != TPACPI_LED_NONE)? 0 : 1;
  1497. }
  1498. #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
  1499. static int led_read(char *p)
  1500. {
  1501. int len = 0;
  1502. if (!led_supported) {
  1503. len += sprintf(p + len, "status:\t\tnot supported\n");
  1504. return len;
  1505. }
  1506. len += sprintf(p + len, "status:\t\tsupported\n");
  1507. if (led_supported == TPACPI_LED_570) {
  1508. /* 570 */
  1509. int i, status;
  1510. for (i = 0; i < 8; i++) {
  1511. if (!acpi_evalf(ec_handle,
  1512. &status, "GLED", "dd", 1 << i))
  1513. return -EIO;
  1514. len += sprintf(p + len, "%d:\t\t%s\n",
  1515. i, led_status(status));
  1516. }
  1517. }
  1518. len += sprintf(p + len, "commands:\t"
  1519. "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
  1520. return len;
  1521. }
  1522. /* off, on, blink */
  1523. static const int led_sled_arg1[] = { 0, 1, 3 };
  1524. static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
  1525. static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
  1526. static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
  1527. static int led_write(char *buf)
  1528. {
  1529. char *cmd;
  1530. int led, ind, ret;
  1531. if (!led_supported)
  1532. return -ENODEV;
  1533. while ((cmd = next_cmd(&buf))) {
  1534. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
  1535. return -EINVAL;
  1536. if (strstr(cmd, "off")) {
  1537. ind = 0;
  1538. } else if (strstr(cmd, "on")) {
  1539. ind = 1;
  1540. } else if (strstr(cmd, "blink")) {
  1541. ind = 2;
  1542. } else
  1543. return -EINVAL;
  1544. if (led_supported == TPACPI_LED_570) {
  1545. /* 570 */
  1546. led = 1 << led;
  1547. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  1548. led, led_sled_arg1[ind]))
  1549. return -EIO;
  1550. } else if (led_supported == TPACPI_LED_OLD) {
  1551. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  1552. led = 1 << led;
  1553. ret = ec_write(TPACPI_LED_EC_HLMS, led);
  1554. if (ret >= 0)
  1555. ret =
  1556. ec_write(TPACPI_LED_EC_HLBL,
  1557. led * led_exp_hlbl[ind]);
  1558. if (ret >= 0)
  1559. ret =
  1560. ec_write(TPACPI_LED_EC_HLCL,
  1561. led * led_exp_hlcl[ind]);
  1562. if (ret < 0)
  1563. return ret;
  1564. } else {
  1565. /* all others */
  1566. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  1567. led, led_led_arg1[ind]))
  1568. return -EIO;
  1569. }
  1570. }
  1571. return 0;
  1572. }
  1573. static struct ibm_struct led_driver_data = {
  1574. .name = "led",
  1575. .read = led_read,
  1576. .write = led_write,
  1577. };
  1578. /*************************************************************************
  1579. * Beep subdriver
  1580. */
  1581. IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  1582. static int __init beep_init(struct ibm_init_struct *iibm)
  1583. {
  1584. vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
  1585. IBM_ACPIHANDLE_INIT(beep);
  1586. vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
  1587. str_supported(beep_handle != NULL));
  1588. return (beep_handle)? 0 : 1;
  1589. }
  1590. static int beep_read(char *p)
  1591. {
  1592. int len = 0;
  1593. if (!beep_handle)
  1594. len += sprintf(p + len, "status:\t\tnot supported\n");
  1595. else {
  1596. len += sprintf(p + len, "status:\t\tsupported\n");
  1597. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  1598. }
  1599. return len;
  1600. }
  1601. static int beep_write(char *buf)
  1602. {
  1603. char *cmd;
  1604. int beep_cmd;
  1605. if (!beep_handle)
  1606. return -ENODEV;
  1607. while ((cmd = next_cmd(&buf))) {
  1608. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  1609. beep_cmd >= 0 && beep_cmd <= 17) {
  1610. /* beep_cmd set */
  1611. } else
  1612. return -EINVAL;
  1613. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
  1614. return -EIO;
  1615. }
  1616. return 0;
  1617. }
  1618. static struct ibm_struct beep_driver_data = {
  1619. .name = "beep",
  1620. .read = beep_read,
  1621. .write = beep_write,
  1622. };
  1623. /*************************************************************************
  1624. * Thermal subdriver
  1625. */
  1626. static enum thermal_access_mode thermal_read_mode;
  1627. /* sysfs temp##_input -------------------------------------------------- */
  1628. static ssize_t thermal_temp_input_show(struct device *dev,
  1629. struct device_attribute *attr,
  1630. char *buf)
  1631. {
  1632. struct sensor_device_attribute *sensor_attr =
  1633. to_sensor_dev_attr(attr);
  1634. int idx = sensor_attr->index;
  1635. s32 value;
  1636. int res;
  1637. res = thermal_get_sensor(idx, &value);
  1638. if (res)
  1639. return res;
  1640. if (value == TP_EC_THERMAL_TMP_NA * 1000)
  1641. return -ENXIO;
  1642. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  1643. }
  1644. #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
  1645. SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
  1646. static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
  1647. THERMAL_SENSOR_ATTR_TEMP(1, 0),
  1648. THERMAL_SENSOR_ATTR_TEMP(2, 1),
  1649. THERMAL_SENSOR_ATTR_TEMP(3, 2),
  1650. THERMAL_SENSOR_ATTR_TEMP(4, 3),
  1651. THERMAL_SENSOR_ATTR_TEMP(5, 4),
  1652. THERMAL_SENSOR_ATTR_TEMP(6, 5),
  1653. THERMAL_SENSOR_ATTR_TEMP(7, 6),
  1654. THERMAL_SENSOR_ATTR_TEMP(8, 7),
  1655. THERMAL_SENSOR_ATTR_TEMP(9, 8),
  1656. THERMAL_SENSOR_ATTR_TEMP(10, 9),
  1657. THERMAL_SENSOR_ATTR_TEMP(11, 10),
  1658. THERMAL_SENSOR_ATTR_TEMP(12, 11),
  1659. THERMAL_SENSOR_ATTR_TEMP(13, 12),
  1660. THERMAL_SENSOR_ATTR_TEMP(14, 13),
  1661. THERMAL_SENSOR_ATTR_TEMP(15, 14),
  1662. THERMAL_SENSOR_ATTR_TEMP(16, 15),
  1663. };
  1664. #define THERMAL_ATTRS(X) \
  1665. &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
  1666. static struct attribute *thermal_temp_input_attr[] = {
  1667. THERMAL_ATTRS(8),
  1668. THERMAL_ATTRS(9),
  1669. THERMAL_ATTRS(10),
  1670. THERMAL_ATTRS(11),
  1671. THERMAL_ATTRS(12),
  1672. THERMAL_ATTRS(13),
  1673. THERMAL_ATTRS(14),
  1674. THERMAL_ATTRS(15),
  1675. THERMAL_ATTRS(0),
  1676. THERMAL_ATTRS(1),
  1677. THERMAL_ATTRS(2),
  1678. THERMAL_ATTRS(3),
  1679. THERMAL_ATTRS(4),
  1680. THERMAL_ATTRS(5),
  1681. THERMAL_ATTRS(6),
  1682. THERMAL_ATTRS(7),
  1683. NULL
  1684. };
  1685. static const struct attribute_group thermal_temp_input16_group = {
  1686. .attrs = thermal_temp_input_attr
  1687. };
  1688. static const struct attribute_group thermal_temp_input8_group = {
  1689. .attrs = &thermal_temp_input_attr[8]
  1690. };
  1691. #undef THERMAL_SENSOR_ATTR_TEMP
  1692. #undef THERMAL_ATTRS
  1693. /* --------------------------------------------------------------------- */
  1694. static int __init thermal_init(struct ibm_init_struct *iibm)
  1695. {
  1696. u8 t, ta1, ta2;
  1697. int i;
  1698. int acpi_tmp7;
  1699. int res;
  1700. vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
  1701. acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  1702. if (ibm_thinkpad_ec_found && experimental) {
  1703. /*
  1704. * Direct EC access mode: sensors at registers
  1705. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  1706. * non-implemented, thermal sensors return 0x80 when
  1707. * not available
  1708. */
  1709. ta1 = ta2 = 0;
  1710. for (i = 0; i < 8; i++) {
  1711. if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
  1712. ta1 |= t;
  1713. } else {
  1714. ta1 = 0;
  1715. break;
  1716. }
  1717. if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
  1718. ta2 |= t;
  1719. } else {
  1720. ta1 = 0;
  1721. break;
  1722. }
  1723. }
  1724. if (ta1 == 0) {
  1725. /* This is sheer paranoia, but we handle it anyway */
  1726. if (acpi_tmp7) {
  1727. printk(IBM_ERR
  1728. "ThinkPad ACPI EC access misbehaving, "
  1729. "falling back to ACPI TMPx access mode\n");
  1730. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  1731. } else {
  1732. printk(IBM_ERR
  1733. "ThinkPad ACPI EC access misbehaving, "
  1734. "disabling thermal sensors access\n");
  1735. thermal_read_mode = TPACPI_THERMAL_NONE;
  1736. }
  1737. } else {
  1738. thermal_read_mode =
  1739. (ta2 != 0) ?
  1740. TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
  1741. }
  1742. } else if (acpi_tmp7) {
  1743. if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  1744. /* 600e/x, 770e, 770x */
  1745. thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
  1746. } else {
  1747. /* Standard ACPI TMPx access, max 8 sensors */
  1748. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  1749. }
  1750. } else {
  1751. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  1752. thermal_read_mode = TPACPI_THERMAL_NONE;
  1753. }
  1754. vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
  1755. str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
  1756. thermal_read_mode);
  1757. switch(thermal_read_mode) {
  1758. case TPACPI_THERMAL_TPEC_16:
  1759. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  1760. &thermal_temp_input16_group);
  1761. if (res)
  1762. return res;
  1763. break;
  1764. case TPACPI_THERMAL_TPEC_8:
  1765. case TPACPI_THERMAL_ACPI_TMP07:
  1766. case TPACPI_THERMAL_ACPI_UPDT:
  1767. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  1768. &thermal_temp_input8_group);
  1769. if (res)
  1770. return res;
  1771. break;
  1772. case TPACPI_THERMAL_NONE:
  1773. default:
  1774. return 1;
  1775. }
  1776. return 0;
  1777. }
  1778. static void thermal_exit(void)
  1779. {
  1780. switch(thermal_read_mode) {
  1781. case TPACPI_THERMAL_TPEC_16:
  1782. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  1783. &thermal_temp_input16_group);
  1784. break;
  1785. case TPACPI_THERMAL_TPEC_8:
  1786. case TPACPI_THERMAL_ACPI_TMP07:
  1787. case TPACPI_THERMAL_ACPI_UPDT:
  1788. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  1789. &thermal_temp_input16_group);
  1790. break;
  1791. case TPACPI_THERMAL_NONE:
  1792. default:
  1793. break;
  1794. }
  1795. }
  1796. /* idx is zero-based */
  1797. static int thermal_get_sensor(int idx, s32 *value)
  1798. {
  1799. int t;
  1800. s8 tmp;
  1801. char tmpi[5];
  1802. t = TP_EC_THERMAL_TMP0;
  1803. switch (thermal_read_mode) {
  1804. #if TPACPI_MAX_THERMAL_SENSORS >= 16
  1805. case TPACPI_THERMAL_TPEC_16:
  1806. if (idx >= 8 && idx <= 15) {
  1807. t = TP_EC_THERMAL_TMP8;
  1808. idx -= 8;
  1809. }
  1810. /* fallthrough */
  1811. #endif
  1812. case TPACPI_THERMAL_TPEC_8:
  1813. if (idx <= 7) {
  1814. if (!acpi_ec_read(t + idx, &tmp))
  1815. return -EIO;
  1816. *value = tmp * 1000;
  1817. return 0;
  1818. }
  1819. break;
  1820. case TPACPI_THERMAL_ACPI_UPDT:
  1821. if (idx <= 7) {
  1822. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  1823. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  1824. return -EIO;
  1825. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1826. return -EIO;
  1827. *value = (t - 2732) * 100;
  1828. return 0;
  1829. }
  1830. break;
  1831. case TPACPI_THERMAL_ACPI_TMP07:
  1832. if (idx <= 7) {
  1833. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  1834. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1835. return -EIO;
  1836. *value = t * 1000;
  1837. return 0;
  1838. }
  1839. break;
  1840. case TPACPI_THERMAL_NONE:
  1841. default:
  1842. return -ENOSYS;
  1843. }
  1844. return -EINVAL;
  1845. }
  1846. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  1847. {
  1848. int res, i;
  1849. int n;
  1850. n = 8;
  1851. i = 0;
  1852. if (!s)
  1853. return -EINVAL;
  1854. if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
  1855. n = 16;
  1856. for(i = 0 ; i < n; i++) {
  1857. res = thermal_get_sensor(i, &s->temp[i]);
  1858. if (res)
  1859. return res;
  1860. }
  1861. return n;
  1862. }
  1863. static int thermal_read(char *p)
  1864. {
  1865. int len = 0;
  1866. int n, i;
  1867. struct ibm_thermal_sensors_struct t;
  1868. n = thermal_get_sensors(&t);
  1869. if (unlikely(n < 0))
  1870. return n;
  1871. len += sprintf(p + len, "temperatures:\t");
  1872. if (n > 0) {
  1873. for (i = 0; i < (n - 1); i++)
  1874. len += sprintf(p + len, "%d ", t.temp[i] / 1000);
  1875. len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
  1876. } else
  1877. len += sprintf(p + len, "not supported\n");
  1878. return len;
  1879. }
  1880. static struct ibm_struct thermal_driver_data = {
  1881. .name = "thermal",
  1882. .read = thermal_read,
  1883. .exit = thermal_exit,
  1884. };
  1885. /*************************************************************************
  1886. * EC Dump subdriver
  1887. */
  1888. static u8 ecdump_regs[256];
  1889. static int ecdump_read(char *p)
  1890. {
  1891. int len = 0;
  1892. int i, j;
  1893. u8 v;
  1894. len += sprintf(p + len, "EC "
  1895. " +00 +01 +02 +03 +04 +05 +06 +07"
  1896. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  1897. for (i = 0; i < 256; i += 16) {
  1898. len += sprintf(p + len, "EC 0x%02x:", i);
  1899. for (j = 0; j < 16; j++) {
  1900. if (!acpi_ec_read(i + j, &v))
  1901. break;
  1902. if (v != ecdump_regs[i + j])
  1903. len += sprintf(p + len, " *%02x", v);
  1904. else
  1905. len += sprintf(p + len, " %02x", v);
  1906. ecdump_regs[i + j] = v;
  1907. }
  1908. len += sprintf(p + len, "\n");
  1909. if (j != 16)
  1910. break;
  1911. }
  1912. /* These are way too dangerous to advertise openly... */
  1913. #if 0
  1914. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  1915. " (<offset> is 00-ff, <value> is 00-ff)\n");
  1916. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  1917. " (<offset> is 00-ff, <value> is 0-255)\n");
  1918. #endif
  1919. return len;
  1920. }
  1921. static int ecdump_write(char *buf)
  1922. {
  1923. char *cmd;
  1924. int i, v;
  1925. while ((cmd = next_cmd(&buf))) {
  1926. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  1927. /* i and v set */
  1928. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  1929. /* i and v set */
  1930. } else
  1931. return -EINVAL;
  1932. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  1933. if (!acpi_ec_write(i, v))
  1934. return -EIO;
  1935. } else
  1936. return -EINVAL;
  1937. }
  1938. return 0;
  1939. }
  1940. static struct ibm_struct ecdump_driver_data = {
  1941. .name = "ecdump",
  1942. .read = ecdump_read,
  1943. .write = ecdump_write,
  1944. .flags.experimental = 1,
  1945. };
  1946. /*************************************************************************
  1947. * Backlight/brightness subdriver
  1948. */
  1949. static struct backlight_device *ibm_backlight_device = NULL;
  1950. static struct backlight_ops ibm_backlight_data = {
  1951. .get_brightness = brightness_get,
  1952. .update_status = brightness_update_status,
  1953. };
  1954. static int __init brightness_init(struct ibm_init_struct *iibm)
  1955. {
  1956. int b;
  1957. vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
  1958. b = brightness_get(NULL);
  1959. if (b < 0)
  1960. return b;
  1961. ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
  1962. &ibm_backlight_data);
  1963. if (IS_ERR(ibm_backlight_device)) {
  1964. printk(IBM_ERR "Could not register backlight device\n");
  1965. return PTR_ERR(ibm_backlight_device);
  1966. }
  1967. vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
  1968. ibm_backlight_device->props.max_brightness = 7;
  1969. ibm_backlight_device->props.brightness = b;
  1970. backlight_update_status(ibm_backlight_device);
  1971. return 0;
  1972. }
  1973. static void brightness_exit(void)
  1974. {
  1975. if (ibm_backlight_device) {
  1976. vdbg_printk(TPACPI_DBG_EXIT,
  1977. "calling backlight_device_unregister()\n");
  1978. backlight_device_unregister(ibm_backlight_device);
  1979. ibm_backlight_device = NULL;
  1980. }
  1981. }
  1982. static int brightness_update_status(struct backlight_device *bd)
  1983. {
  1984. return brightness_set(
  1985. (bd->props.fb_blank == FB_BLANK_UNBLANK &&
  1986. bd->props.power == FB_BLANK_UNBLANK) ?
  1987. bd->props.brightness : 0);
  1988. }
  1989. static int brightness_get(struct backlight_device *bd)
  1990. {
  1991. u8 level;
  1992. if (!acpi_ec_read(brightness_offset, &level))
  1993. return -EIO;
  1994. level &= 0x7;
  1995. return level;
  1996. }
  1997. static int brightness_set(int value)
  1998. {
  1999. int cmos_cmd, inc, i;
  2000. int current_value = brightness_get(NULL);
  2001. value &= 7;
  2002. cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
  2003. inc = value > current_value ? 1 : -1;
  2004. for (i = current_value; i != value; i += inc) {
  2005. if (issue_thinkpad_cmos_command(cmos_cmd))
  2006. return -EIO;
  2007. if (!acpi_ec_write(brightness_offset, i + inc))
  2008. return -EIO;
  2009. }
  2010. return 0;
  2011. }
  2012. static int brightness_read(char *p)
  2013. {
  2014. int len = 0;
  2015. int level;
  2016. if ((level = brightness_get(NULL)) < 0) {
  2017. len += sprintf(p + len, "level:\t\tunreadable\n");
  2018. } else {
  2019. len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
  2020. len += sprintf(p + len, "commands:\tup, down\n");
  2021. len += sprintf(p + len, "commands:\tlevel <level>"
  2022. " (<level> is 0-7)\n");
  2023. }
  2024. return len;
  2025. }
  2026. static int brightness_write(char *buf)
  2027. {
  2028. int level;
  2029. int new_level;
  2030. char *cmd;
  2031. while ((cmd = next_cmd(&buf))) {
  2032. if ((level = brightness_get(NULL)) < 0)
  2033. return level;
  2034. level &= 7;
  2035. if (strlencmp(cmd, "up") == 0) {
  2036. new_level = level == 7 ? 7 : level + 1;
  2037. } else if (strlencmp(cmd, "down") == 0) {
  2038. new_level = level == 0 ? 0 : level - 1;
  2039. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  2040. new_level >= 0 && new_level <= 7) {
  2041. /* new_level set */
  2042. } else
  2043. return -EINVAL;
  2044. brightness_set(new_level);
  2045. }
  2046. return 0;
  2047. }
  2048. static struct ibm_struct brightness_driver_data = {
  2049. .name = "brightness",
  2050. .read = brightness_read,
  2051. .write = brightness_write,
  2052. .exit = brightness_exit,
  2053. };
  2054. /*************************************************************************
  2055. * Volume subdriver
  2056. */
  2057. static int volume_read(char *p)
  2058. {
  2059. int len = 0;
  2060. u8 level;
  2061. if (!acpi_ec_read(volume_offset, &level)) {
  2062. len += sprintf(p + len, "level:\t\tunreadable\n");
  2063. } else {
  2064. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  2065. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  2066. len += sprintf(p + len, "commands:\tup, down, mute\n");
  2067. len += sprintf(p + len, "commands:\tlevel <level>"
  2068. " (<level> is 0-15)\n");
  2069. }
  2070. return len;
  2071. }
  2072. static int volume_write(char *buf)
  2073. {
  2074. int cmos_cmd, inc, i;
  2075. u8 level, mute;
  2076. int new_level, new_mute;
  2077. char *cmd;
  2078. while ((cmd = next_cmd(&buf))) {
  2079. if (!acpi_ec_read(volume_offset, &level))
  2080. return -EIO;
  2081. new_mute = mute = level & 0x40;
  2082. new_level = level = level & 0xf;
  2083. if (strlencmp(cmd, "up") == 0) {
  2084. if (mute)
  2085. new_mute = 0;
  2086. else
  2087. new_level = level == 15 ? 15 : level + 1;
  2088. } else if (strlencmp(cmd, "down") == 0) {
  2089. if (mute)
  2090. new_mute = 0;
  2091. else
  2092. new_level = level == 0 ? 0 : level - 1;
  2093. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  2094. new_level >= 0 && new_level <= 15) {
  2095. /* new_level set */
  2096. } else if (strlencmp(cmd, "mute") == 0) {
  2097. new_mute = 0x40;
  2098. } else
  2099. return -EINVAL;
  2100. if (new_level != level) { /* mute doesn't change */
  2101. cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
  2102. inc = new_level > level ? 1 : -1;
  2103. if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
  2104. !acpi_ec_write(volume_offset, level)))
  2105. return -EIO;
  2106. for (i = level; i != new_level; i += inc)
  2107. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  2108. !acpi_ec_write(volume_offset, i + inc))
  2109. return -EIO;
  2110. if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
  2111. !acpi_ec_write(volume_offset,
  2112. new_level + mute)))
  2113. return -EIO;
  2114. }
  2115. if (new_mute != mute) { /* level doesn't change */
  2116. cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
  2117. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  2118. !acpi_ec_write(volume_offset, level + new_mute))
  2119. return -EIO;
  2120. }
  2121. }
  2122. return 0;
  2123. }
  2124. static struct ibm_struct volume_driver_data = {
  2125. .name = "volume",
  2126. .read = volume_read,
  2127. .write = volume_write,
  2128. };
  2129. /*************************************************************************
  2130. * Fan subdriver
  2131. */
  2132. /*
  2133. * FAN ACCESS MODES
  2134. *
  2135. * TPACPI_FAN_RD_ACPI_GFAN:
  2136. * ACPI GFAN method: returns fan level
  2137. *
  2138. * see TPACPI_FAN_WR_ACPI_SFAN
  2139. * EC 0x2f (HFSP) not available if GFAN exists
  2140. *
  2141. * TPACPI_FAN_WR_ACPI_SFAN:
  2142. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  2143. *
  2144. * EC 0x2f (HFSP) might be available *for reading*, but do not use
  2145. * it for writing.
  2146. *
  2147. * TPACPI_FAN_WR_TPEC:
  2148. * ThinkPad EC register 0x2f (HFSP): fan control loop mode
  2149. * Supported on almost all ThinkPads
  2150. *
  2151. * Fan speed changes of any sort (including those caused by the
  2152. * disengaged mode) are usually done slowly by the firmware as the
  2153. * maximum ammount of fan duty cycle change per second seems to be
  2154. * limited.
  2155. *
  2156. * Reading is not available if GFAN exists.
  2157. * Writing is not available if SFAN exists.
  2158. *
  2159. * Bits
  2160. * 7 automatic mode engaged;
  2161. * (default operation mode of the ThinkPad)
  2162. * fan level is ignored in this mode.
  2163. * 6 full speed mode (takes precedence over bit 7);
  2164. * not available on all thinkpads. May disable
  2165. * the tachometer while the fan controller ramps up
  2166. * the speed (which can take up to a few *minutes*).
  2167. * Speeds up fan to 100% duty-cycle, which is far above
  2168. * the standard RPM levels. It is not impossible that
  2169. * it could cause hardware damage.
  2170. * 5-3 unused in some models. Extra bits for fan level
  2171. * in others, but still useless as all values above
  2172. * 7 map to the same speed as level 7 in these models.
  2173. * 2-0 fan level (0..7 usually)
  2174. * 0x00 = stop
  2175. * 0x07 = max (set when temperatures critical)
  2176. * Some ThinkPads may have other levels, see
  2177. * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  2178. *
  2179. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  2180. * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
  2181. * does so, its initial value is meaningless (0x07).
  2182. *
  2183. * For firmware bugs, refer to:
  2184. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  2185. *
  2186. * ----
  2187. *
  2188. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  2189. * Main fan tachometer reading (in RPM)
  2190. *
  2191. * This register is present on all ThinkPads with a new-style EC, and
  2192. * it is known not to be present on the A21m/e, and T22, as there is
  2193. * something else in offset 0x84 according to the ACPI DSDT. Other
  2194. * ThinkPads from this same time period (and earlier) probably lack the
  2195. * tachometer as well.
  2196. *
  2197. * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
  2198. * was never fixed by IBM to report the EC firmware version string
  2199. * probably support the tachometer (like the early X models), so
  2200. * detecting it is quite hard. We need more data to know for sure.
  2201. *
  2202. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  2203. * might result.
  2204. *
  2205. * FIRMWARE BUG: may go stale while the EC is switching to full speed
  2206. * mode.
  2207. *
  2208. * For firmware bugs, refer to:
  2209. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  2210. *
  2211. * TPACPI_FAN_WR_ACPI_FANS:
  2212. * ThinkPad X31, X40, X41. Not available in the X60.
  2213. *
  2214. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  2215. * high speed. ACPI DSDT seems to map these three speeds to levels
  2216. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  2217. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  2218. *
  2219. * The speeds are stored on handles
  2220. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  2221. *
  2222. * There are three default speed sets, acessible as handles:
  2223. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  2224. *
  2225. * ACPI DSDT switches which set is in use depending on various
  2226. * factors.
  2227. *
  2228. * TPACPI_FAN_WR_TPEC is also available and should be used to
  2229. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  2230. * but the ACPI tables just mention level 7.
  2231. */
  2232. static enum fan_status_access_mode fan_status_access_mode;
  2233. static enum fan_control_access_mode fan_control_access_mode;
  2234. static enum fan_control_commands fan_control_commands;
  2235. static u8 fan_control_initial_status;
  2236. static u8 fan_control_desired_level;
  2237. static void fan_watchdog_fire(struct work_struct *ignored);
  2238. static int fan_watchdog_maxinterval;
  2239. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  2240. IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  2241. IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
  2242. "\\FSPD", /* 600e/x, 770e, 770x */
  2243. ); /* all others */
  2244. IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
  2245. "JFNS", /* 770x-JL */
  2246. ); /* all others */
  2247. /*
  2248. * SYSFS fan layout: hwmon compatible (device)
  2249. *
  2250. * pwm*_enable:
  2251. * 0: "disengaged" mode
  2252. * 1: manual mode
  2253. * 2: native EC "auto" mode (recommended, hardware default)
  2254. *
  2255. * pwm*: set speed in manual mode, ignored otherwise.
  2256. * 0 is level 0; 255 is level 7. Intermediate points done with linear
  2257. * interpolation.
  2258. *
  2259. * fan*_input: tachometer reading, RPM
  2260. *
  2261. *
  2262. * SYSFS fan layout: extensions
  2263. *
  2264. * fan_watchdog (driver):
  2265. * fan watchdog interval in seconds, 0 disables (default), max 120
  2266. */
  2267. /* sysfs fan pwm1_enable ----------------------------------------------- */
  2268. static ssize_t fan_pwm1_enable_show(struct device *dev,
  2269. struct device_attribute *attr,
  2270. char *buf)
  2271. {
  2272. int res, mode;
  2273. u8 status;
  2274. res = fan_get_status_safe(&status);
  2275. if (res)
  2276. return res;
  2277. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  2278. if (status != fan_control_initial_status) {
  2279. tp_features.fan_ctrl_status_undef = 0;
  2280. } else {
  2281. /* Return most likely status. In fact, it
  2282. * might be the only possible status */
  2283. status = TP_EC_FAN_AUTO;
  2284. }
  2285. }
  2286. if (status & TP_EC_FAN_FULLSPEED) {
  2287. mode = 0;
  2288. } else if (status & TP_EC_FAN_AUTO) {
  2289. mode = 2;
  2290. } else
  2291. mode = 1;
  2292. return snprintf(buf, PAGE_SIZE, "%d\n", mode);
  2293. }
  2294. static ssize_t fan_pwm1_enable_store(struct device *dev,
  2295. struct device_attribute *attr,
  2296. const char *buf, size_t count)
  2297. {
  2298. unsigned long t;
  2299. int res, level;
  2300. if (parse_strtoul(buf, 2, &t))
  2301. return -EINVAL;
  2302. switch (t) {
  2303. case 0:
  2304. level = TP_EC_FAN_FULLSPEED;
  2305. break;
  2306. case 1:
  2307. level = TPACPI_FAN_LAST_LEVEL;
  2308. break;
  2309. case 2:
  2310. level = TP_EC_FAN_AUTO;
  2311. break;
  2312. case 3:
  2313. /* reserved for software-controlled auto mode */
  2314. return -ENOSYS;
  2315. default:
  2316. return -EINVAL;
  2317. }
  2318. res = fan_set_level_safe(level);
  2319. if (res < 0)
  2320. return res;
  2321. fan_watchdog_reset();
  2322. return count;
  2323. }
  2324. static struct device_attribute dev_attr_fan_pwm1_enable =
  2325. __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  2326. fan_pwm1_enable_show, fan_pwm1_enable_store);
  2327. /* sysfs fan pwm1 ------------------------------------------------------ */
  2328. static ssize_t fan_pwm1_show(struct device *dev,
  2329. struct device_attribute *attr,
  2330. char *buf)
  2331. {
  2332. int res;
  2333. u8 status;
  2334. res = fan_get_status_safe(&status);
  2335. if (res)
  2336. return res;
  2337. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  2338. if (status != fan_control_initial_status) {
  2339. tp_features.fan_ctrl_status_undef = 0;
  2340. } else {
  2341. status = TP_EC_FAN_AUTO;
  2342. }
  2343. }
  2344. if ((status &
  2345. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
  2346. status = fan_control_desired_level;
  2347. if (status > 7)
  2348. status = 7;
  2349. return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
  2350. }
  2351. static ssize_t fan_pwm1_store(struct device *dev,
  2352. struct device_attribute *attr,
  2353. const char *buf, size_t count)
  2354. {
  2355. unsigned long s;
  2356. int rc;
  2357. u8 status, newlevel;
  2358. if (parse_strtoul(buf, 255, &s))
  2359. return -EINVAL;
  2360. /* scale down from 0-255 to 0-7 */
  2361. newlevel = (s >> 5) & 0x07;
  2362. rc = mutex_lock_interruptible(&fan_mutex);
  2363. if (rc < 0)
  2364. return rc;
  2365. rc = fan_get_status(&status);
  2366. if (!rc && (status &
  2367. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  2368. rc = fan_set_level(newlevel);
  2369. if (!rc)
  2370. fan_update_desired_level(newlevel);
  2371. fan_watchdog_reset();
  2372. }
  2373. mutex_unlock(&fan_mutex);
  2374. return (rc)? rc : count;
  2375. }
  2376. static struct device_attribute dev_attr_fan_pwm1 =
  2377. __ATTR(pwm1, S_IWUSR | S_IRUGO,
  2378. fan_pwm1_show, fan_pwm1_store);
  2379. /* sysfs fan fan1_input ------------------------------------------------ */
  2380. static ssize_t fan_fan1_input_show(struct device *dev,
  2381. struct device_attribute *attr,
  2382. char *buf)
  2383. {
  2384. int res;
  2385. unsigned int speed;
  2386. res = fan_get_speed(&speed);
  2387. if (res < 0)
  2388. return res;
  2389. return snprintf(buf, PAGE_SIZE, "%u\n", speed);
  2390. }
  2391. static struct device_attribute dev_attr_fan_fan1_input =
  2392. __ATTR(fan1_input, S_IRUGO,
  2393. fan_fan1_input_show, NULL);
  2394. /* sysfs fan fan_watchdog (driver) ------------------------------------- */
  2395. static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
  2396. char *buf)
  2397. {
  2398. return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
  2399. }
  2400. static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
  2401. const char *buf, size_t count)
  2402. {
  2403. unsigned long t;
  2404. if (parse_strtoul(buf, 120, &t))
  2405. return -EINVAL;
  2406. fan_watchdog_maxinterval = t;
  2407. fan_watchdog_reset();
  2408. return count;
  2409. }
  2410. static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
  2411. fan_fan_watchdog_show, fan_fan_watchdog_store);
  2412. /* --------------------------------------------------------------------- */
  2413. static struct attribute *fan_attributes[] = {
  2414. &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
  2415. &dev_attr_fan_fan1_input.attr,
  2416. NULL
  2417. };
  2418. static const struct attribute_group fan_attr_group = {
  2419. .attrs = fan_attributes,
  2420. };
  2421. static int __init fan_init(struct ibm_init_struct *iibm)
  2422. {
  2423. int rc;
  2424. vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
  2425. mutex_init(&fan_mutex);
  2426. fan_status_access_mode = TPACPI_FAN_NONE;
  2427. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  2428. fan_control_commands = 0;
  2429. fan_watchdog_maxinterval = 0;
  2430. tp_features.fan_ctrl_status_undef = 0;
  2431. fan_control_desired_level = 7;
  2432. IBM_ACPIHANDLE_INIT(fans);
  2433. IBM_ACPIHANDLE_INIT(gfan);
  2434. IBM_ACPIHANDLE_INIT(sfan);
  2435. if (gfan_handle) {
  2436. /* 570, 600e/x, 770e, 770x */
  2437. fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
  2438. } else {
  2439. /* all other ThinkPads: note that even old-style
  2440. * ThinkPad ECs supports the fan control register */
  2441. if (likely(acpi_ec_read(fan_status_offset,
  2442. &fan_control_initial_status))) {
  2443. fan_status_access_mode = TPACPI_FAN_RD_TPEC;
  2444. /* In some ThinkPads, neither the EC nor the ACPI
  2445. * DSDT initialize the fan status, and it ends up
  2446. * being set to 0x07 when it *could* be either
  2447. * 0x07 or 0x80.
  2448. *
  2449. * Enable for TP-1Y (T43), TP-78 (R51e),
  2450. * TP-76 (R52), TP-70 (T43, R52), which are known
  2451. * to be buggy. */
  2452. if (fan_control_initial_status == 0x07 &&
  2453. ibm_thinkpad_ec_found &&
  2454. ((ibm_thinkpad_ec_found[0] == '1' &&
  2455. ibm_thinkpad_ec_found[1] == 'Y') ||
  2456. (ibm_thinkpad_ec_found[0] == '7' &&
  2457. (ibm_thinkpad_ec_found[1] == '6' ||
  2458. ibm_thinkpad_ec_found[1] == '8' ||
  2459. ibm_thinkpad_ec_found[1] == '0'))
  2460. )) {
  2461. printk(IBM_NOTICE
  2462. "fan_init: initial fan status is "
  2463. "unknown, assuming it is in auto "
  2464. "mode\n");
  2465. tp_features.fan_ctrl_status_undef = 1;
  2466. }
  2467. } else {
  2468. printk(IBM_ERR
  2469. "ThinkPad ACPI EC access misbehaving, "
  2470. "fan status and control unavailable\n");
  2471. return 1;
  2472. }
  2473. }
  2474. if (sfan_handle) {
  2475. /* 570, 770x-JL */
  2476. fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
  2477. fan_control_commands |=
  2478. TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
  2479. } else {
  2480. if (!gfan_handle) {
  2481. /* gfan without sfan means no fan control */
  2482. /* all other models implement TP EC 0x2f control */
  2483. if (fans_handle) {
  2484. /* X31, X40, X41 */
  2485. fan_control_access_mode =
  2486. TPACPI_FAN_WR_ACPI_FANS;
  2487. fan_control_commands |=
  2488. TPACPI_FAN_CMD_SPEED |
  2489. TPACPI_FAN_CMD_LEVEL |
  2490. TPACPI_FAN_CMD_ENABLE;
  2491. } else {
  2492. fan_control_access_mode = TPACPI_FAN_WR_TPEC;
  2493. fan_control_commands |=
  2494. TPACPI_FAN_CMD_LEVEL |
  2495. TPACPI_FAN_CMD_ENABLE;
  2496. }
  2497. }
  2498. }
  2499. vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
  2500. str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
  2501. fan_control_access_mode != TPACPI_FAN_WR_NONE),
  2502. fan_status_access_mode, fan_control_access_mode);
  2503. /* update fan_control_desired_level */
  2504. if (fan_status_access_mode != TPACPI_FAN_NONE)
  2505. fan_get_status_safe(NULL);
  2506. if (fan_status_access_mode != TPACPI_FAN_NONE ||
  2507. fan_control_access_mode != TPACPI_FAN_WR_NONE) {
  2508. rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  2509. &fan_attr_group);
  2510. if (!(rc < 0))
  2511. rc = driver_create_file(&tpacpi_pdriver.driver,
  2512. &driver_attr_fan_watchdog);
  2513. if (rc < 0)
  2514. return rc;
  2515. return 0;
  2516. } else
  2517. return 1;
  2518. }
  2519. /*
  2520. * Call with fan_mutex held
  2521. */
  2522. static void fan_update_desired_level(u8 status)
  2523. {
  2524. if ((status &
  2525. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  2526. if (status > 7)
  2527. fan_control_desired_level = 7;
  2528. else
  2529. fan_control_desired_level = status;
  2530. }
  2531. }
  2532. static int fan_get_status(u8 *status)
  2533. {
  2534. u8 s;
  2535. /* TODO:
  2536. * Add TPACPI_FAN_RD_ACPI_FANS ? */
  2537. switch (fan_status_access_mode) {
  2538. case TPACPI_FAN_RD_ACPI_GFAN:
  2539. /* 570, 600e/x, 770e, 770x */
  2540. if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
  2541. return -EIO;
  2542. if (likely(status))
  2543. *status = s & 0x07;
  2544. break;
  2545. case TPACPI_FAN_RD_TPEC:
  2546. /* all except 570, 600e/x, 770e, 770x */
  2547. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  2548. return -EIO;
  2549. if (likely(status))
  2550. *status = s;
  2551. break;
  2552. default:
  2553. return -ENXIO;
  2554. }
  2555. return 0;
  2556. }
  2557. static int fan_get_status_safe(u8 *status)
  2558. {
  2559. int rc;
  2560. u8 s;
  2561. rc = mutex_lock_interruptible(&fan_mutex);
  2562. if (rc < 0)
  2563. return rc;
  2564. rc = fan_get_status(&s);
  2565. if (!rc)
  2566. fan_update_desired_level(s);
  2567. mutex_unlock(&fan_mutex);
  2568. if (status)
  2569. *status = s;
  2570. return rc;
  2571. }
  2572. static void fan_exit(void)
  2573. {
  2574. vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
  2575. /* FIXME: can we really do this unconditionally? */
  2576. sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
  2577. driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
  2578. cancel_delayed_work(&fan_watchdog_task);
  2579. flush_scheduled_work();
  2580. }
  2581. static int fan_get_speed(unsigned int *speed)
  2582. {
  2583. u8 hi, lo;
  2584. switch (fan_status_access_mode) {
  2585. case TPACPI_FAN_RD_TPEC:
  2586. /* all except 570, 600e/x, 770e, 770x */
  2587. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  2588. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  2589. return -EIO;
  2590. if (likely(speed))
  2591. *speed = (hi << 8) | lo;
  2592. break;
  2593. default:
  2594. return -ENXIO;
  2595. }
  2596. return 0;
  2597. }
  2598. static void fan_watchdog_fire(struct work_struct *ignored)
  2599. {
  2600. int rc;
  2601. printk(IBM_NOTICE "fan watchdog: enabling fan\n");
  2602. rc = fan_set_enable();
  2603. if (rc < 0) {
  2604. printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
  2605. "will try again later...\n", -rc);
  2606. /* reschedule for later */
  2607. fan_watchdog_reset();
  2608. }
  2609. }
  2610. static void fan_watchdog_reset(void)
  2611. {
  2612. static int fan_watchdog_active = 0;
  2613. if (fan_watchdog_active)
  2614. cancel_delayed_work(&fan_watchdog_task);
  2615. if (fan_watchdog_maxinterval > 0) {
  2616. fan_watchdog_active = 1;
  2617. if (!schedule_delayed_work(&fan_watchdog_task,
  2618. msecs_to_jiffies(fan_watchdog_maxinterval
  2619. * 1000))) {
  2620. printk(IBM_ERR "failed to schedule the fan watchdog, "
  2621. "watchdog will not trigger\n");
  2622. }
  2623. } else
  2624. fan_watchdog_active = 0;
  2625. }
  2626. static int fan_set_level(int level)
  2627. {
  2628. switch (fan_control_access_mode) {
  2629. case TPACPI_FAN_WR_ACPI_SFAN:
  2630. if (level >= 0 && level <= 7) {
  2631. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  2632. return -EIO;
  2633. } else
  2634. return -EINVAL;
  2635. break;
  2636. case TPACPI_FAN_WR_ACPI_FANS:
  2637. case TPACPI_FAN_WR_TPEC:
  2638. if ((level != TP_EC_FAN_AUTO) &&
  2639. (level != TP_EC_FAN_FULLSPEED) &&
  2640. ((level < 0) || (level > 7)))
  2641. return -EINVAL;
  2642. if (!acpi_ec_write(fan_status_offset, level))
  2643. return -EIO;
  2644. else
  2645. tp_features.fan_ctrl_status_undef = 0;
  2646. break;
  2647. default:
  2648. return -ENXIO;
  2649. }
  2650. return 0;
  2651. }
  2652. static int fan_set_level_safe(int level)
  2653. {
  2654. int rc;
  2655. rc = mutex_lock_interruptible(&fan_mutex);
  2656. if (rc < 0)
  2657. return rc;
  2658. if (level == TPACPI_FAN_LAST_LEVEL)
  2659. level = fan_control_desired_level;
  2660. rc = fan_set_level(level);
  2661. if (!rc)
  2662. fan_update_desired_level(level);
  2663. mutex_unlock(&fan_mutex);
  2664. return rc;
  2665. }
  2666. static int fan_set_enable(void)
  2667. {
  2668. u8 s;
  2669. int rc;
  2670. rc = mutex_lock_interruptible(&fan_mutex);
  2671. if (rc < 0)
  2672. return rc;
  2673. switch (fan_control_access_mode) {
  2674. case TPACPI_FAN_WR_ACPI_FANS:
  2675. case TPACPI_FAN_WR_TPEC:
  2676. rc = fan_get_status(&s);
  2677. if (rc < 0)
  2678. break;
  2679. /* Don't go out of emergency fan mode */
  2680. if (s != 7)
  2681. s = TP_EC_FAN_AUTO;
  2682. if (!acpi_ec_write(fan_status_offset, s))
  2683. rc = -EIO;
  2684. else {
  2685. tp_features.fan_ctrl_status_undef = 0;
  2686. rc = 0;
  2687. }
  2688. break;
  2689. case TPACPI_FAN_WR_ACPI_SFAN:
  2690. rc = fan_get_status(&s);
  2691. if (rc < 0)
  2692. break;
  2693. s &= 0x07;
  2694. /* Set fan to at least level 4 */
  2695. if (s < 4)
  2696. s = 4;
  2697. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  2698. rc= -EIO;
  2699. else
  2700. rc = 0;
  2701. break;
  2702. default:
  2703. rc = -ENXIO;
  2704. }
  2705. mutex_unlock(&fan_mutex);
  2706. return rc;
  2707. }
  2708. static int fan_set_disable(void)
  2709. {
  2710. int rc;
  2711. rc = mutex_lock_interruptible(&fan_mutex);
  2712. if (rc < 0)
  2713. return rc;
  2714. rc = 0;
  2715. switch (fan_control_access_mode) {
  2716. case TPACPI_FAN_WR_ACPI_FANS:
  2717. case TPACPI_FAN_WR_TPEC:
  2718. if (!acpi_ec_write(fan_status_offset, 0x00))
  2719. rc = -EIO;
  2720. else {
  2721. fan_control_desired_level = 0;
  2722. tp_features.fan_ctrl_status_undef = 0;
  2723. }
  2724. break;
  2725. case TPACPI_FAN_WR_ACPI_SFAN:
  2726. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  2727. rc = -EIO;
  2728. else
  2729. fan_control_desired_level = 0;
  2730. break;
  2731. default:
  2732. rc = -ENXIO;
  2733. }
  2734. mutex_unlock(&fan_mutex);
  2735. return rc;
  2736. }
  2737. static int fan_set_speed(int speed)
  2738. {
  2739. int rc;
  2740. rc = mutex_lock_interruptible(&fan_mutex);
  2741. if (rc < 0)
  2742. return rc;
  2743. rc = 0;
  2744. switch (fan_control_access_mode) {
  2745. case TPACPI_FAN_WR_ACPI_FANS:
  2746. if (speed >= 0 && speed <= 65535) {
  2747. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  2748. speed, speed, speed))
  2749. rc = -EIO;
  2750. } else
  2751. rc = -EINVAL;
  2752. break;
  2753. default:
  2754. rc = -ENXIO;
  2755. }
  2756. mutex_unlock(&fan_mutex);
  2757. return rc;
  2758. }
  2759. static int fan_read(char *p)
  2760. {
  2761. int len = 0;
  2762. int rc;
  2763. u8 status;
  2764. unsigned int speed = 0;
  2765. switch (fan_status_access_mode) {
  2766. case TPACPI_FAN_RD_ACPI_GFAN:
  2767. /* 570, 600e/x, 770e, 770x */
  2768. if ((rc = fan_get_status_safe(&status)) < 0)
  2769. return rc;
  2770. len += sprintf(p + len, "status:\t\t%s\n"
  2771. "level:\t\t%d\n",
  2772. (status != 0) ? "enabled" : "disabled", status);
  2773. break;
  2774. case TPACPI_FAN_RD_TPEC:
  2775. /* all except 570, 600e/x, 770e, 770x */
  2776. if ((rc = fan_get_status_safe(&status)) < 0)
  2777. return rc;
  2778. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  2779. if (status != fan_control_initial_status)
  2780. tp_features.fan_ctrl_status_undef = 0;
  2781. else
  2782. /* Return most likely status. In fact, it
  2783. * might be the only possible status */
  2784. status = TP_EC_FAN_AUTO;
  2785. }
  2786. len += sprintf(p + len, "status:\t\t%s\n",
  2787. (status != 0) ? "enabled" : "disabled");
  2788. if ((rc = fan_get_speed(&speed)) < 0)
  2789. return rc;
  2790. len += sprintf(p + len, "speed:\t\t%d\n", speed);
  2791. if (status & TP_EC_FAN_FULLSPEED)
  2792. /* Disengaged mode takes precedence */
  2793. len += sprintf(p + len, "level:\t\tdisengaged\n");
  2794. else if (status & TP_EC_FAN_AUTO)
  2795. len += sprintf(p + len, "level:\t\tauto\n");
  2796. else
  2797. len += sprintf(p + len, "level:\t\t%d\n", status);
  2798. break;
  2799. case TPACPI_FAN_NONE:
  2800. default:
  2801. len += sprintf(p + len, "status:\t\tnot supported\n");
  2802. }
  2803. if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
  2804. len += sprintf(p + len, "commands:\tlevel <level>");
  2805. switch (fan_control_access_mode) {
  2806. case TPACPI_FAN_WR_ACPI_SFAN:
  2807. len += sprintf(p + len, " (<level> is 0-7)\n");
  2808. break;
  2809. default:
  2810. len += sprintf(p + len, " (<level> is 0-7, "
  2811. "auto, disengaged, full-speed)\n");
  2812. break;
  2813. }
  2814. }
  2815. if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
  2816. len += sprintf(p + len, "commands:\tenable, disable\n"
  2817. "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
  2818. "1-120 (seconds))\n");
  2819. if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
  2820. len += sprintf(p + len, "commands:\tspeed <speed>"
  2821. " (<speed> is 0-65535)\n");
  2822. return len;
  2823. }
  2824. static int fan_write_cmd_level(const char *cmd, int *rc)
  2825. {
  2826. int level;
  2827. if (strlencmp(cmd, "level auto") == 0)
  2828. level = TP_EC_FAN_AUTO;
  2829. else if ((strlencmp(cmd, "level disengaged") == 0) |
  2830. (strlencmp(cmd, "level full-speed") == 0))
  2831. level = TP_EC_FAN_FULLSPEED;
  2832. else if (sscanf(cmd, "level %d", &level) != 1)
  2833. return 0;
  2834. if ((*rc = fan_set_level_safe(level)) == -ENXIO)
  2835. printk(IBM_ERR "level command accepted for unsupported "
  2836. "access mode %d", fan_control_access_mode);
  2837. return 1;
  2838. }
  2839. static int fan_write_cmd_enable(const char *cmd, int *rc)
  2840. {
  2841. if (strlencmp(cmd, "enable") != 0)
  2842. return 0;
  2843. if ((*rc = fan_set_enable()) == -ENXIO)
  2844. printk(IBM_ERR "enable command accepted for unsupported "
  2845. "access mode %d", fan_control_access_mode);
  2846. return 1;
  2847. }
  2848. static int fan_write_cmd_disable(const char *cmd, int *rc)
  2849. {
  2850. if (strlencmp(cmd, "disable") != 0)
  2851. return 0;
  2852. if ((*rc = fan_set_disable()) == -ENXIO)
  2853. printk(IBM_ERR "disable command accepted for unsupported "
  2854. "access mode %d", fan_control_access_mode);
  2855. return 1;
  2856. }
  2857. static int fan_write_cmd_speed(const char *cmd, int *rc)
  2858. {
  2859. int speed;
  2860. /* TODO:
  2861. * Support speed <low> <medium> <high> ? */
  2862. if (sscanf(cmd, "speed %d", &speed) != 1)
  2863. return 0;
  2864. if ((*rc = fan_set_speed(speed)) == -ENXIO)
  2865. printk(IBM_ERR "speed command accepted for unsupported "
  2866. "access mode %d", fan_control_access_mode);
  2867. return 1;
  2868. }
  2869. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  2870. {
  2871. int interval;
  2872. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  2873. return 0;
  2874. if (interval < 0 || interval > 120)
  2875. *rc = -EINVAL;
  2876. else
  2877. fan_watchdog_maxinterval = interval;
  2878. return 1;
  2879. }
  2880. static int fan_write(char *buf)
  2881. {
  2882. char *cmd;
  2883. int rc = 0;
  2884. while (!rc && (cmd = next_cmd(&buf))) {
  2885. if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
  2886. fan_write_cmd_level(cmd, &rc)) &&
  2887. !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
  2888. (fan_write_cmd_enable(cmd, &rc) ||
  2889. fan_write_cmd_disable(cmd, &rc) ||
  2890. fan_write_cmd_watchdog(cmd, &rc))) &&
  2891. !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
  2892. fan_write_cmd_speed(cmd, &rc))
  2893. )
  2894. rc = -EINVAL;
  2895. else if (!rc)
  2896. fan_watchdog_reset();
  2897. }
  2898. return rc;
  2899. }
  2900. static struct ibm_struct fan_driver_data = {
  2901. .name = "fan",
  2902. .read = fan_read,
  2903. .write = fan_write,
  2904. .exit = fan_exit,
  2905. .flags.experimental = 1,
  2906. };
  2907. /****************************************************************************
  2908. ****************************************************************************
  2909. *
  2910. * Infrastructure
  2911. *
  2912. ****************************************************************************
  2913. ****************************************************************************/
  2914. /* /proc support */
  2915. static struct proc_dir_entry *proc_dir = NULL;
  2916. /* Subdriver registry */
  2917. static LIST_HEAD(tpacpi_all_drivers);
  2918. /*
  2919. * Module and infrastructure proble, init and exit handling
  2920. */
  2921. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  2922. static const char * __init str_supported(int is_supported)
  2923. {
  2924. static char text_unsupported[] __initdata = "not supported";
  2925. return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
  2926. }
  2927. #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
  2928. static int __init ibm_init(struct ibm_init_struct *iibm)
  2929. {
  2930. int ret;
  2931. struct ibm_struct *ibm = iibm->data;
  2932. struct proc_dir_entry *entry;
  2933. BUG_ON(ibm == NULL);
  2934. INIT_LIST_HEAD(&ibm->all_drivers);
  2935. if (ibm->flags.experimental && !experimental)
  2936. return 0;
  2937. dbg_printk(TPACPI_DBG_INIT,
  2938. "probing for %s\n", ibm->name);
  2939. if (iibm->init) {
  2940. ret = iibm->init(iibm);
  2941. if (ret > 0)
  2942. return 0; /* probe failed */
  2943. if (ret)
  2944. return ret;
  2945. ibm->flags.init_called = 1;
  2946. }
  2947. if (ibm->acpi) {
  2948. if (ibm->acpi->hid) {
  2949. ret = register_tpacpi_subdriver(ibm);
  2950. if (ret)
  2951. goto err_out;
  2952. }
  2953. if (ibm->acpi->notify) {
  2954. ret = setup_acpi_notify(ibm);
  2955. if (ret == -ENODEV) {
  2956. printk(IBM_NOTICE "disabling subdriver %s\n",
  2957. ibm->name);
  2958. ret = 0;
  2959. goto err_out;
  2960. }
  2961. if (ret < 0)
  2962. goto err_out;
  2963. }
  2964. }
  2965. dbg_printk(TPACPI_DBG_INIT,
  2966. "%s installed\n", ibm->name);
  2967. if (ibm->read) {
  2968. entry = create_proc_entry(ibm->name,
  2969. S_IFREG | S_IRUGO | S_IWUSR,
  2970. proc_dir);
  2971. if (!entry) {
  2972. printk(IBM_ERR "unable to create proc entry %s\n",
  2973. ibm->name);
  2974. ret = -ENODEV;
  2975. goto err_out;
  2976. }
  2977. entry->owner = THIS_MODULE;
  2978. entry->data = ibm;
  2979. entry->read_proc = &dispatch_procfs_read;
  2980. if (ibm->write)
  2981. entry->write_proc = &dispatch_procfs_write;
  2982. ibm->flags.proc_created = 1;
  2983. }
  2984. list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
  2985. return 0;
  2986. err_out:
  2987. dbg_printk(TPACPI_DBG_INIT,
  2988. "%s: at error exit path with result %d\n",
  2989. ibm->name, ret);
  2990. ibm_exit(ibm);
  2991. return (ret < 0)? ret : 0;
  2992. }
  2993. static void ibm_exit(struct ibm_struct *ibm)
  2994. {
  2995. dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
  2996. list_del_init(&ibm->all_drivers);
  2997. if (ibm->flags.acpi_notify_installed) {
  2998. dbg_printk(TPACPI_DBG_EXIT,
  2999. "%s: acpi_remove_notify_handler\n", ibm->name);
  3000. BUG_ON(!ibm->acpi);
  3001. acpi_remove_notify_handler(*ibm->acpi->handle,
  3002. ibm->acpi->type,
  3003. dispatch_acpi_notify);
  3004. ibm->flags.acpi_notify_installed = 0;
  3005. ibm->flags.acpi_notify_installed = 0;
  3006. }
  3007. if (ibm->flags.proc_created) {
  3008. dbg_printk(TPACPI_DBG_EXIT,
  3009. "%s: remove_proc_entry\n", ibm->name);
  3010. remove_proc_entry(ibm->name, proc_dir);
  3011. ibm->flags.proc_created = 0;
  3012. }
  3013. if (ibm->flags.acpi_driver_registered) {
  3014. dbg_printk(TPACPI_DBG_EXIT,
  3015. "%s: acpi_bus_unregister_driver\n", ibm->name);
  3016. BUG_ON(!ibm->acpi);
  3017. acpi_bus_unregister_driver(ibm->acpi->driver);
  3018. kfree(ibm->acpi->driver);
  3019. ibm->acpi->driver = NULL;
  3020. ibm->flags.acpi_driver_registered = 0;
  3021. }
  3022. if (ibm->flags.init_called && ibm->exit) {
  3023. ibm->exit();
  3024. ibm->flags.init_called = 0;
  3025. }
  3026. dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
  3027. }
  3028. /* Probing */
  3029. static char *ibm_thinkpad_ec_found = NULL;
  3030. static char* __init check_dmi_for_ec(void)
  3031. {
  3032. struct dmi_device *dev = NULL;
  3033. char ec_fw_string[18];
  3034. /*
  3035. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  3036. * X32 or newer, all Z series; Some models must have an
  3037. * up-to-date BIOS or they will not be detected.
  3038. *
  3039. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  3040. */
  3041. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  3042. if (sscanf(dev->name,
  3043. "IBM ThinkPad Embedded Controller -[%17c",
  3044. ec_fw_string) == 1) {
  3045. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  3046. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  3047. return kstrdup(ec_fw_string, GFP_KERNEL);
  3048. }
  3049. }
  3050. return NULL;
  3051. }
  3052. static int __init probe_for_thinkpad(void)
  3053. {
  3054. int is_thinkpad;
  3055. if (acpi_disabled)
  3056. return -ENODEV;
  3057. /*
  3058. * Non-ancient models have better DMI tagging, but very old models
  3059. * don't.
  3060. */
  3061. is_thinkpad = dmi_name_in_vendors("ThinkPad");
  3062. /* ec is required because many other handles are relative to it */
  3063. IBM_ACPIHANDLE_INIT(ec);
  3064. if (!ec_handle) {
  3065. if (is_thinkpad)
  3066. printk(IBM_ERR
  3067. "Not yet supported ThinkPad detected!\n");
  3068. return -ENODEV;
  3069. }
  3070. /*
  3071. * Risks a regression on very old machines, but reduces potential
  3072. * false positives a damn great deal
  3073. */
  3074. if (!is_thinkpad)
  3075. is_thinkpad = dmi_name_in_vendors("IBM");
  3076. if (!is_thinkpad && !force_load)
  3077. return -ENODEV;
  3078. return 0;
  3079. }
  3080. /* Module init, exit, parameters */
  3081. static struct ibm_init_struct ibms_init[] __initdata = {
  3082. {
  3083. .init = thinkpad_acpi_driver_init,
  3084. .data = &thinkpad_acpi_driver_data,
  3085. },
  3086. {
  3087. .init = hotkey_init,
  3088. .data = &hotkey_driver_data,
  3089. },
  3090. {
  3091. .init = bluetooth_init,
  3092. .data = &bluetooth_driver_data,
  3093. },
  3094. {
  3095. .init = wan_init,
  3096. .data = &wan_driver_data,
  3097. },
  3098. {
  3099. .init = video_init,
  3100. .data = &video_driver_data,
  3101. },
  3102. {
  3103. .init = light_init,
  3104. .data = &light_driver_data,
  3105. },
  3106. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  3107. {
  3108. .init = dock_init,
  3109. .data = &dock_driver_data[0],
  3110. },
  3111. {
  3112. .data = &dock_driver_data[1],
  3113. },
  3114. #endif
  3115. #ifdef CONFIG_THINKPAD_ACPI_BAY
  3116. {
  3117. .init = bay_init,
  3118. .data = &bay_driver_data,
  3119. },
  3120. #endif
  3121. {
  3122. .init = cmos_init,
  3123. .data = &cmos_driver_data,
  3124. },
  3125. {
  3126. .init = led_init,
  3127. .data = &led_driver_data,
  3128. },
  3129. {
  3130. .init = beep_init,
  3131. .data = &beep_driver_data,
  3132. },
  3133. {
  3134. .init = thermal_init,
  3135. .data = &thermal_driver_data,
  3136. },
  3137. {
  3138. .data = &ecdump_driver_data,
  3139. },
  3140. {
  3141. .init = brightness_init,
  3142. .data = &brightness_driver_data,
  3143. },
  3144. {
  3145. .data = &volume_driver_data,
  3146. },
  3147. {
  3148. .init = fan_init,
  3149. .data = &fan_driver_data,
  3150. },
  3151. };
  3152. static int __init set_ibm_param(const char *val, struct kernel_param *kp)
  3153. {
  3154. unsigned int i;
  3155. struct ibm_struct *ibm;
  3156. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  3157. ibm = ibms_init[i].data;
  3158. BUG_ON(ibm == NULL);
  3159. if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
  3160. if (strlen(val) > sizeof(ibms_init[i].param) - 2)
  3161. return -ENOSPC;
  3162. strcpy(ibms_init[i].param, val);
  3163. strcat(ibms_init[i].param, ",");
  3164. return 0;
  3165. }
  3166. }
  3167. return -EINVAL;
  3168. }
  3169. static int experimental;
  3170. module_param(experimental, int, 0);
  3171. static u32 dbg_level;
  3172. module_param_named(debug, dbg_level, uint, 0);
  3173. static int force_load;
  3174. module_param(force_load, int, 0);
  3175. #define IBM_PARAM(feature) \
  3176. module_param_call(feature, set_ibm_param, NULL, NULL, 0)
  3177. IBM_PARAM(hotkey);
  3178. IBM_PARAM(bluetooth);
  3179. IBM_PARAM(video);
  3180. IBM_PARAM(light);
  3181. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  3182. IBM_PARAM(dock);
  3183. #endif
  3184. #ifdef CONFIG_THINKPAD_ACPI_BAY
  3185. IBM_PARAM(bay);
  3186. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  3187. IBM_PARAM(cmos);
  3188. IBM_PARAM(led);
  3189. IBM_PARAM(beep);
  3190. IBM_PARAM(ecdump);
  3191. IBM_PARAM(brightness);
  3192. IBM_PARAM(volume);
  3193. IBM_PARAM(fan);
  3194. static int __init thinkpad_acpi_module_init(void)
  3195. {
  3196. int ret, i;
  3197. /* Driver-level probe */
  3198. ret = probe_for_thinkpad();
  3199. if (ret)
  3200. return ret;
  3201. /* Driver initialization */
  3202. ibm_thinkpad_ec_found = check_dmi_for_ec();
  3203. IBM_ACPIHANDLE_INIT(ecrd);
  3204. IBM_ACPIHANDLE_INIT(ecwr);
  3205. proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
  3206. if (!proc_dir) {
  3207. printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
  3208. thinkpad_acpi_module_exit();
  3209. return -ENODEV;
  3210. }
  3211. proc_dir->owner = THIS_MODULE;
  3212. ret = platform_driver_register(&tpacpi_pdriver);
  3213. if (ret) {
  3214. printk(IBM_ERR "unable to register platform driver\n");
  3215. thinkpad_acpi_module_exit();
  3216. return ret;
  3217. }
  3218. ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
  3219. if (ret) {
  3220. printk(IBM_ERR "unable to create sysfs driver attributes\n");
  3221. thinkpad_acpi_module_exit();
  3222. return ret;
  3223. }
  3224. /* Device initialization */
  3225. tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
  3226. NULL, 0);
  3227. if (IS_ERR(tpacpi_pdev)) {
  3228. ret = PTR_ERR(tpacpi_pdev);
  3229. tpacpi_pdev = NULL;
  3230. printk(IBM_ERR "unable to register platform device\n");
  3231. thinkpad_acpi_module_exit();
  3232. return ret;
  3233. }
  3234. tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
  3235. if (IS_ERR(tpacpi_hwmon)) {
  3236. ret = PTR_ERR(tpacpi_hwmon);
  3237. tpacpi_hwmon = NULL;
  3238. printk(IBM_ERR "unable to register hwmon device\n");
  3239. thinkpad_acpi_module_exit();
  3240. return ret;
  3241. }
  3242. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  3243. ret = ibm_init(&ibms_init[i]);
  3244. if (ret >= 0 && *ibms_init[i].param)
  3245. ret = ibms_init[i].data->write(ibms_init[i].param);
  3246. if (ret < 0) {
  3247. thinkpad_acpi_module_exit();
  3248. return ret;
  3249. }
  3250. }
  3251. return 0;
  3252. }
  3253. static void thinkpad_acpi_module_exit(void)
  3254. {
  3255. struct ibm_struct *ibm, *itmp;
  3256. list_for_each_entry_safe_reverse(ibm, itmp,
  3257. &tpacpi_all_drivers,
  3258. all_drivers) {
  3259. ibm_exit(ibm);
  3260. }
  3261. dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
  3262. if (tpacpi_hwmon)
  3263. hwmon_device_unregister(tpacpi_hwmon);
  3264. if (tpacpi_pdev)
  3265. platform_device_unregister(tpacpi_pdev);
  3266. tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
  3267. platform_driver_unregister(&tpacpi_pdriver);
  3268. if (proc_dir)
  3269. remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
  3270. kfree(ibm_thinkpad_ec_found);
  3271. }
  3272. module_init(thinkpad_acpi_module_init);
  3273. module_exit(thinkpad_acpi_module_exit);