thinkpad_acpi.c 92 KB

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