thinkpad_acpi.c 65 KB

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