ibm_acpi.c 54 KB

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