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