thinkpad_acpi.c 64 KB

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