thinkpad_acpi.c 74 KB

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