thinkpad_acpi.c 75 KB

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