thinkpad_acpi.c 73 KB

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