thinkpad_acpi.c 65 KB

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