thinkpad_acpi.c 72 KB

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