thinkpad_acpi.c 66 KB

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