ibm_acpi.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  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. static int ibm_thinkpad_ec_found;
  203. struct ibm_struct {
  204. char *name;
  205. char param[32];
  206. char *hid;
  207. struct acpi_driver *driver;
  208. int (*init) (void);
  209. int (*read) (char *);
  210. int (*write) (char *);
  211. void (*exit) (void);
  212. void (*notify) (struct ibm_struct *, u32);
  213. acpi_handle *handle;
  214. int type;
  215. struct acpi_device *device;
  216. int driver_registered;
  217. int proc_created;
  218. int init_called;
  219. int notify_installed;
  220. int experimental;
  221. };
  222. static struct proc_dir_entry *proc_dir = NULL;
  223. static struct backlight_device *ibm_backlight_device;
  224. #define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
  225. #define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
  226. #define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
  227. static int acpi_evalf(acpi_handle handle,
  228. void *res, char *method, char *fmt, ...)
  229. {
  230. char *fmt0 = fmt;
  231. struct acpi_object_list params;
  232. union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
  233. struct acpi_buffer result, *resultp;
  234. union acpi_object out_obj;
  235. acpi_status status;
  236. va_list ap;
  237. char res_type;
  238. int success;
  239. int quiet;
  240. if (!*fmt) {
  241. printk(IBM_ERR "acpi_evalf() called with empty format\n");
  242. return 0;
  243. }
  244. if (*fmt == 'q') {
  245. quiet = 1;
  246. fmt++;
  247. } else
  248. quiet = 0;
  249. res_type = *(fmt++);
  250. params.count = 0;
  251. params.pointer = &in_objs[0];
  252. va_start(ap, fmt);
  253. while (*fmt) {
  254. char c = *(fmt++);
  255. switch (c) {
  256. case 'd': /* int */
  257. in_objs[params.count].integer.value = va_arg(ap, int);
  258. in_objs[params.count++].type = ACPI_TYPE_INTEGER;
  259. break;
  260. /* add more types as needed */
  261. default:
  262. printk(IBM_ERR "acpi_evalf() called "
  263. "with invalid format character '%c'\n", c);
  264. return 0;
  265. }
  266. }
  267. va_end(ap);
  268. if (res_type != 'v') {
  269. result.length = sizeof(out_obj);
  270. result.pointer = &out_obj;
  271. resultp = &result;
  272. } else
  273. resultp = NULL;
  274. status = acpi_evaluate_object(handle, method, &params, resultp);
  275. switch (res_type) {
  276. case 'd': /* int */
  277. if (res)
  278. *(int *)res = out_obj.integer.value;
  279. success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
  280. break;
  281. case 'v': /* void */
  282. success = status == AE_OK;
  283. break;
  284. /* add more types as needed */
  285. default:
  286. printk(IBM_ERR "acpi_evalf() called "
  287. "with invalid format character '%c'\n", res_type);
  288. return 0;
  289. }
  290. if (!success && !quiet)
  291. printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
  292. method, fmt0, status);
  293. return success;
  294. }
  295. static void __unused acpi_print_int(acpi_handle handle, char *method)
  296. {
  297. int i;
  298. if (acpi_evalf(handle, &i, method, "d"))
  299. printk(IBM_INFO "%s = 0x%x\n", method, i);
  300. else
  301. printk(IBM_ERR "error calling %s\n", method);
  302. }
  303. static char *next_cmd(char **cmds)
  304. {
  305. char *start = *cmds;
  306. char *end;
  307. while ((end = strchr(start, ',')) && end == start)
  308. start = end + 1;
  309. if (!end)
  310. return NULL;
  311. *end = 0;
  312. *cmds = end + 1;
  313. return start;
  314. }
  315. static int driver_init(void)
  316. {
  317. printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
  318. printk(IBM_INFO "%s\n", IBM_URL);
  319. return 0;
  320. }
  321. static int driver_read(char *p)
  322. {
  323. int len = 0;
  324. len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
  325. len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
  326. return len;
  327. }
  328. static int hotkey_supported;
  329. static int hotkey_mask_supported;
  330. static int hotkey_orig_status;
  331. static int hotkey_orig_mask;
  332. static int hotkey_get(int *status, int *mask)
  333. {
  334. if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
  335. return 0;
  336. if (hotkey_mask_supported)
  337. if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
  338. return 0;
  339. return 1;
  340. }
  341. static int hotkey_set(int status, int mask)
  342. {
  343. int i;
  344. if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
  345. return 0;
  346. if (hotkey_mask_supported)
  347. for (i = 0; i < 32; i++) {
  348. int bit = ((1 << i) & mask) != 0;
  349. if (!acpi_evalf(hkey_handle,
  350. NULL, "MHKM", "vdd", i + 1, bit))
  351. return 0;
  352. }
  353. return 1;
  354. }
  355. static int hotkey_init(void)
  356. {
  357. /* hotkey not supported on 570 */
  358. hotkey_supported = hkey_handle != NULL;
  359. if (hotkey_supported) {
  360. /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  361. A30, R30, R31, T20-22, X20-21, X22-24 */
  362. hotkey_mask_supported =
  363. acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
  364. if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
  365. return -ENODEV;
  366. }
  367. return 0;
  368. }
  369. static int hotkey_read(char *p)
  370. {
  371. int status, mask;
  372. int len = 0;
  373. if (!hotkey_supported) {
  374. len += sprintf(p + len, "status:\t\tnot supported\n");
  375. return len;
  376. }
  377. if (!hotkey_get(&status, &mask))
  378. return -EIO;
  379. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
  380. if (hotkey_mask_supported) {
  381. len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
  382. len += sprintf(p + len,
  383. "commands:\tenable, disable, reset, <mask>\n");
  384. } else {
  385. len += sprintf(p + len, "mask:\t\tnot supported\n");
  386. len += sprintf(p + len, "commands:\tenable, disable, reset\n");
  387. }
  388. return len;
  389. }
  390. static int hotkey_write(char *buf)
  391. {
  392. int status, mask;
  393. char *cmd;
  394. int do_cmd = 0;
  395. if (!hotkey_supported)
  396. return -ENODEV;
  397. if (!hotkey_get(&status, &mask))
  398. return -EIO;
  399. while ((cmd = next_cmd(&buf))) {
  400. if (strlencmp(cmd, "enable") == 0) {
  401. status = 1;
  402. } else if (strlencmp(cmd, "disable") == 0) {
  403. status = 0;
  404. } else if (strlencmp(cmd, "reset") == 0) {
  405. status = hotkey_orig_status;
  406. mask = hotkey_orig_mask;
  407. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  408. /* mask set */
  409. } else if (sscanf(cmd, "%x", &mask) == 1) {
  410. /* mask set */
  411. } else
  412. return -EINVAL;
  413. do_cmd = 1;
  414. }
  415. if (do_cmd && !hotkey_set(status, mask))
  416. return -EIO;
  417. return 0;
  418. }
  419. static void hotkey_exit(void)
  420. {
  421. if (hotkey_supported)
  422. hotkey_set(hotkey_orig_status, hotkey_orig_mask);
  423. }
  424. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  425. {
  426. int hkey;
  427. if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
  428. acpi_bus_generate_event(ibm->device, event, hkey);
  429. else {
  430. printk(IBM_ERR "unknown hotkey event %d\n", event);
  431. acpi_bus_generate_event(ibm->device, event, 0);
  432. }
  433. }
  434. static int bluetooth_supported;
  435. static int bluetooth_init(void)
  436. {
  437. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  438. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  439. bluetooth_supported = hkey_handle &&
  440. acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
  441. return 0;
  442. }
  443. static int bluetooth_status(void)
  444. {
  445. int status;
  446. if (!bluetooth_supported ||
  447. !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  448. status = 0;
  449. return status;
  450. }
  451. static int bluetooth_read(char *p)
  452. {
  453. int len = 0;
  454. int status = bluetooth_status();
  455. if (!bluetooth_supported)
  456. len += sprintf(p + len, "status:\t\tnot supported\n");
  457. else if (!(status & 1))
  458. len += sprintf(p + len, "status:\t\tnot installed\n");
  459. else {
  460. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
  461. len += sprintf(p + len, "commands:\tenable, disable\n");
  462. }
  463. return len;
  464. }
  465. static int bluetooth_write(char *buf)
  466. {
  467. int status = bluetooth_status();
  468. char *cmd;
  469. int do_cmd = 0;
  470. if (!bluetooth_supported)
  471. return -ENODEV;
  472. while ((cmd = next_cmd(&buf))) {
  473. if (strlencmp(cmd, "enable") == 0) {
  474. status |= 2;
  475. } else if (strlencmp(cmd, "disable") == 0) {
  476. status &= ~2;
  477. } else
  478. return -EINVAL;
  479. do_cmd = 1;
  480. }
  481. if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  482. return -EIO;
  483. return 0;
  484. }
  485. static int wan_supported;
  486. static int wan_init(void)
  487. {
  488. wan_supported = hkey_handle &&
  489. acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
  490. return 0;
  491. }
  492. static int wan_status(void)
  493. {
  494. int status;
  495. if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  496. status = 0;
  497. return status;
  498. }
  499. static int wan_read(char *p)
  500. {
  501. int len = 0;
  502. int status = wan_status();
  503. if (!wan_supported)
  504. len += sprintf(p + len, "status:\t\tnot supported\n");
  505. else if (!(status & 1))
  506. len += sprintf(p + len, "status:\t\tnot installed\n");
  507. else {
  508. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
  509. len += sprintf(p + len, "commands:\tenable, disable\n");
  510. }
  511. return len;
  512. }
  513. static int wan_write(char *buf)
  514. {
  515. int status = wan_status();
  516. char *cmd;
  517. int do_cmd = 0;
  518. if (!wan_supported)
  519. return -ENODEV;
  520. while ((cmd = next_cmd(&buf))) {
  521. if (strlencmp(cmd, "enable") == 0) {
  522. status |= 2;
  523. } else if (strlencmp(cmd, "disable") == 0) {
  524. status &= ~2;
  525. } else
  526. return -EINVAL;
  527. do_cmd = 1;
  528. }
  529. if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  530. return -EIO;
  531. return 0;
  532. }
  533. static int video_supported;
  534. static int video_orig_autosw;
  535. #define VIDEO_570 1
  536. #define VIDEO_770 2
  537. #define VIDEO_NEW 3
  538. static int video_init(void)
  539. {
  540. int ivga;
  541. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  542. /* G41, assume IVGA doesn't change */
  543. vid_handle = vid2_handle;
  544. if (!vid_handle)
  545. /* video switching not supported on R30, R31 */
  546. video_supported = 0;
  547. else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  548. /* 570 */
  549. video_supported = VIDEO_570;
  550. else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  551. /* 600e/x, 770e, 770x */
  552. video_supported = VIDEO_770;
  553. else
  554. /* all others */
  555. video_supported = VIDEO_NEW;
  556. return 0;
  557. }
  558. static int video_status(void)
  559. {
  560. int status = 0;
  561. int i;
  562. if (video_supported == VIDEO_570) {
  563. if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
  564. status = i & 3;
  565. } else if (video_supported == VIDEO_770) {
  566. if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
  567. status |= 0x01 * i;
  568. if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
  569. status |= 0x02 * i;
  570. } else if (video_supported == VIDEO_NEW) {
  571. acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
  572. if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
  573. status |= 0x02 * i;
  574. acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
  575. if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
  576. status |= 0x01 * i;
  577. if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
  578. status |= 0x08 * i;
  579. }
  580. return status;
  581. }
  582. static int video_autosw(void)
  583. {
  584. int autosw = 0;
  585. if (video_supported == VIDEO_570)
  586. acpi_evalf(vid_handle, &autosw, "SWIT", "d");
  587. else if (video_supported == VIDEO_770 || video_supported == VIDEO_NEW)
  588. acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
  589. return autosw & 1;
  590. }
  591. static int video_read(char *p)
  592. {
  593. int status = video_status();
  594. int autosw = video_autosw();
  595. int len = 0;
  596. if (!video_supported) {
  597. len += sprintf(p + len, "status:\t\tnot supported\n");
  598. return len;
  599. }
  600. len += sprintf(p + len, "status:\t\tsupported\n");
  601. len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
  602. len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
  603. if (video_supported == VIDEO_NEW)
  604. len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
  605. len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
  606. len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
  607. len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
  608. if (video_supported == VIDEO_NEW)
  609. len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
  610. len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
  611. len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
  612. return len;
  613. }
  614. static int video_switch(void)
  615. {
  616. int autosw = video_autosw();
  617. int ret;
  618. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
  619. return -EIO;
  620. ret = video_supported == VIDEO_570 ?
  621. acpi_evalf(ec_handle, NULL, "_Q16", "v") :
  622. acpi_evalf(vid_handle, NULL, "VSWT", "v");
  623. acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
  624. return ret;
  625. }
  626. static int video_expand(void)
  627. {
  628. if (video_supported == VIDEO_570)
  629. return acpi_evalf(ec_handle, NULL, "_Q17", "v");
  630. else if (video_supported == VIDEO_770)
  631. return acpi_evalf(vid_handle, NULL, "VEXP", "v");
  632. else
  633. return acpi_evalf(NULL, NULL, "\\VEXP", "v");
  634. }
  635. static int video_switch2(int status)
  636. {
  637. int ret;
  638. if (video_supported == VIDEO_570) {
  639. ret = acpi_evalf(NULL, NULL,
  640. "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
  641. } else if (video_supported == VIDEO_770) {
  642. int autosw = video_autosw();
  643. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
  644. return -EIO;
  645. ret = acpi_evalf(vid_handle, NULL,
  646. "ASWT", "vdd", status * 0x100, 0);
  647. acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
  648. } else {
  649. ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  650. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  651. }
  652. return ret;
  653. }
  654. static int video_write(char *buf)
  655. {
  656. char *cmd;
  657. int enable, disable, status;
  658. if (!video_supported)
  659. return -ENODEV;
  660. enable = disable = 0;
  661. while ((cmd = next_cmd(&buf))) {
  662. if (strlencmp(cmd, "lcd_enable") == 0) {
  663. enable |= 0x01;
  664. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  665. disable |= 0x01;
  666. } else if (strlencmp(cmd, "crt_enable") == 0) {
  667. enable |= 0x02;
  668. } else if (strlencmp(cmd, "crt_disable") == 0) {
  669. disable |= 0x02;
  670. } else if (video_supported == VIDEO_NEW &&
  671. strlencmp(cmd, "dvi_enable") == 0) {
  672. enable |= 0x08;
  673. } else if (video_supported == VIDEO_NEW &&
  674. strlencmp(cmd, "dvi_disable") == 0) {
  675. disable |= 0x08;
  676. } else if (strlencmp(cmd, "auto_enable") == 0) {
  677. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
  678. return -EIO;
  679. } else if (strlencmp(cmd, "auto_disable") == 0) {
  680. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
  681. return -EIO;
  682. } else if (strlencmp(cmd, "video_switch") == 0) {
  683. if (!video_switch())
  684. return -EIO;
  685. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  686. if (!video_expand())
  687. return -EIO;
  688. } else
  689. return -EINVAL;
  690. }
  691. if (enable || disable) {
  692. status = (video_status() & 0x0f & ~disable) | enable;
  693. if (!video_switch2(status))
  694. return -EIO;
  695. }
  696. return 0;
  697. }
  698. static void video_exit(void)
  699. {
  700. acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
  701. }
  702. static int light_supported;
  703. static int light_status_supported;
  704. static int light_init(void)
  705. {
  706. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  707. light_supported = (cmos_handle || lght_handle) && !ledb_handle;
  708. if (light_supported)
  709. /* light status not supported on
  710. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  711. light_status_supported = acpi_evalf(ec_handle, NULL,
  712. "KBLT", "qv");
  713. return 0;
  714. }
  715. static int light_read(char *p)
  716. {
  717. int len = 0;
  718. int status = 0;
  719. if (!light_supported) {
  720. len += sprintf(p + len, "status:\t\tnot supported\n");
  721. } else if (!light_status_supported) {
  722. len += sprintf(p + len, "status:\t\tunknown\n");
  723. len += sprintf(p + len, "commands:\ton, off\n");
  724. } else {
  725. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  726. return -EIO;
  727. len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
  728. len += sprintf(p + len, "commands:\ton, off\n");
  729. }
  730. return len;
  731. }
  732. static int light_write(char *buf)
  733. {
  734. int cmos_cmd, lght_cmd;
  735. char *cmd;
  736. int success;
  737. if (!light_supported)
  738. return -ENODEV;
  739. while ((cmd = next_cmd(&buf))) {
  740. if (strlencmp(cmd, "on") == 0) {
  741. cmos_cmd = 0x0c;
  742. lght_cmd = 1;
  743. } else if (strlencmp(cmd, "off") == 0) {
  744. cmos_cmd = 0x0d;
  745. lght_cmd = 0;
  746. } else
  747. return -EINVAL;
  748. success = cmos_handle ?
  749. acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
  750. acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
  751. if (!success)
  752. return -EIO;
  753. }
  754. return 0;
  755. }
  756. static int _sta(acpi_handle handle)
  757. {
  758. int status;
  759. if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
  760. status = 0;
  761. return status;
  762. }
  763. #ifdef CONFIG_ACPI_IBM_DOCK
  764. #define dock_docked() (_sta(dock_handle) & 1)
  765. static int dock_read(char *p)
  766. {
  767. int len = 0;
  768. int docked = dock_docked();
  769. if (!dock_handle)
  770. len += sprintf(p + len, "status:\t\tnot supported\n");
  771. else if (!docked)
  772. len += sprintf(p + len, "status:\t\tundocked\n");
  773. else {
  774. len += sprintf(p + len, "status:\t\tdocked\n");
  775. len += sprintf(p + len, "commands:\tdock, undock\n");
  776. }
  777. return len;
  778. }
  779. static int dock_write(char *buf)
  780. {
  781. char *cmd;
  782. if (!dock_docked())
  783. return -ENODEV;
  784. while ((cmd = next_cmd(&buf))) {
  785. if (strlencmp(cmd, "undock") == 0) {
  786. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
  787. !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
  788. return -EIO;
  789. } else if (strlencmp(cmd, "dock") == 0) {
  790. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
  791. return -EIO;
  792. } else
  793. return -EINVAL;
  794. }
  795. return 0;
  796. }
  797. static void dock_notify(struct ibm_struct *ibm, u32 event)
  798. {
  799. int docked = dock_docked();
  800. int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
  801. if (event == 1 && !pci) /* 570 */
  802. acpi_bus_generate_event(ibm->device, event, 1); /* button */
  803. else if (event == 1 && pci) /* 570 */
  804. acpi_bus_generate_event(ibm->device, event, 3); /* dock */
  805. else if (event == 3 && docked)
  806. acpi_bus_generate_event(ibm->device, event, 1); /* button */
  807. else if (event == 3 && !docked)
  808. acpi_bus_generate_event(ibm->device, event, 2); /* undock */
  809. else if (event == 0 && docked)
  810. acpi_bus_generate_event(ibm->device, event, 3); /* dock */
  811. else {
  812. printk(IBM_ERR "unknown dock event %d, status %d\n",
  813. event, _sta(dock_handle));
  814. acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
  815. }
  816. }
  817. #endif
  818. static int bay_status_supported;
  819. static int bay_status2_supported;
  820. static int bay_eject_supported;
  821. static int bay_eject2_supported;
  822. static int bay_init(void)
  823. {
  824. bay_status_supported = bay_handle &&
  825. acpi_evalf(bay_handle, NULL, "_STA", "qv");
  826. bay_status2_supported = bay2_handle &&
  827. acpi_evalf(bay2_handle, NULL, "_STA", "qv");
  828. bay_eject_supported = bay_handle && bay_ej_handle &&
  829. (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
  830. bay_eject2_supported = bay2_handle && bay2_ej_handle &&
  831. (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
  832. return 0;
  833. }
  834. #define bay_occupied(b) (_sta(b##_handle) & 1)
  835. static int bay_read(char *p)
  836. {
  837. int len = 0;
  838. int occupied = bay_occupied(bay);
  839. int occupied2 = bay_occupied(bay2);
  840. int eject, eject2;
  841. len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
  842. (occupied ? "occupied" : "unoccupied") :
  843. "not supported");
  844. if (bay_status2_supported)
  845. len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
  846. "occupied" : "unoccupied");
  847. eject = bay_eject_supported && occupied;
  848. eject2 = bay_eject2_supported && occupied2;
  849. if (eject && eject2)
  850. len += sprintf(p + len, "commands:\teject, eject2\n");
  851. else if (eject)
  852. len += sprintf(p + len, "commands:\teject\n");
  853. else if (eject2)
  854. len += sprintf(p + len, "commands:\teject2\n");
  855. return len;
  856. }
  857. static int bay_write(char *buf)
  858. {
  859. char *cmd;
  860. if (!bay_eject_supported && !bay_eject2_supported)
  861. return -ENODEV;
  862. while ((cmd = next_cmd(&buf))) {
  863. if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
  864. if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
  865. return -EIO;
  866. } else if (bay_eject2_supported &&
  867. strlencmp(cmd, "eject2") == 0) {
  868. if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
  869. return -EIO;
  870. } else
  871. return -EINVAL;
  872. }
  873. return 0;
  874. }
  875. static void bay_notify(struct ibm_struct *ibm, u32 event)
  876. {
  877. acpi_bus_generate_event(ibm->device, event, 0);
  878. }
  879. static int cmos_read(char *p)
  880. {
  881. int len = 0;
  882. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  883. R30, R31, T20-22, X20-21 */
  884. if (!cmos_handle)
  885. len += sprintf(p + len, "status:\t\tnot supported\n");
  886. else {
  887. len += sprintf(p + len, "status:\t\tsupported\n");
  888. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
  889. }
  890. return len;
  891. }
  892. static int cmos_eval(int cmos_cmd)
  893. {
  894. if (cmos_handle)
  895. return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
  896. else
  897. return 1;
  898. }
  899. static int cmos_write(char *buf)
  900. {
  901. char *cmd;
  902. int cmos_cmd;
  903. if (!cmos_handle)
  904. return -EINVAL;
  905. while ((cmd = next_cmd(&buf))) {
  906. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  907. cmos_cmd >= 0 && cmos_cmd <= 21) {
  908. /* cmos_cmd set */
  909. } else
  910. return -EINVAL;
  911. if (!cmos_eval(cmos_cmd))
  912. return -EIO;
  913. }
  914. return 0;
  915. }
  916. static int led_supported;
  917. #define LED_570 1
  918. #define LED_OLD 2
  919. #define LED_NEW 3
  920. static int led_init(void)
  921. {
  922. if (!led_handle)
  923. /* led not supported on R30, R31 */
  924. led_supported = 0;
  925. else if (strlencmp(led_path, "SLED") == 0)
  926. /* 570 */
  927. led_supported = LED_570;
  928. else if (strlencmp(led_path, "SYSL") == 0)
  929. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  930. led_supported = LED_OLD;
  931. else
  932. /* all others */
  933. led_supported = LED_NEW;
  934. return 0;
  935. }
  936. #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
  937. static int led_read(char *p)
  938. {
  939. int len = 0;
  940. if (!led_supported) {
  941. len += sprintf(p + len, "status:\t\tnot supported\n");
  942. return len;
  943. }
  944. len += sprintf(p + len, "status:\t\tsupported\n");
  945. if (led_supported == LED_570) {
  946. /* 570 */
  947. int i, status;
  948. for (i = 0; i < 8; i++) {
  949. if (!acpi_evalf(ec_handle,
  950. &status, "GLED", "dd", 1 << i))
  951. return -EIO;
  952. len += sprintf(p + len, "%d:\t\t%s\n",
  953. i, led_status(status));
  954. }
  955. }
  956. len += sprintf(p + len, "commands:\t"
  957. "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
  958. return len;
  959. }
  960. /* off, on, blink */
  961. static const int led_sled_arg1[] = { 0, 1, 3 };
  962. static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
  963. static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
  964. static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
  965. #define EC_HLCL 0x0c
  966. #define EC_HLBL 0x0d
  967. #define EC_HLMS 0x0e
  968. static int led_write(char *buf)
  969. {
  970. char *cmd;
  971. int led, ind, ret;
  972. if (!led_supported)
  973. return -ENODEV;
  974. while ((cmd = next_cmd(&buf))) {
  975. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
  976. return -EINVAL;
  977. if (strstr(cmd, "off")) {
  978. ind = 0;
  979. } else if (strstr(cmd, "on")) {
  980. ind = 1;
  981. } else if (strstr(cmd, "blink")) {
  982. ind = 2;
  983. } else
  984. return -EINVAL;
  985. if (led_supported == LED_570) {
  986. /* 570 */
  987. led = 1 << led;
  988. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  989. led, led_sled_arg1[ind]))
  990. return -EIO;
  991. } else if (led_supported == LED_OLD) {
  992. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  993. led = 1 << led;
  994. ret = ec_write(EC_HLMS, led);
  995. if (ret >= 0)
  996. ret =
  997. ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
  998. if (ret >= 0)
  999. ret =
  1000. ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
  1001. if (ret < 0)
  1002. return ret;
  1003. } else {
  1004. /* all others */
  1005. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  1006. led, led_led_arg1[ind]))
  1007. return -EIO;
  1008. }
  1009. }
  1010. return 0;
  1011. }
  1012. static int beep_read(char *p)
  1013. {
  1014. int len = 0;
  1015. if (!beep_handle)
  1016. len += sprintf(p + len, "status:\t\tnot supported\n");
  1017. else {
  1018. len += sprintf(p + len, "status:\t\tsupported\n");
  1019. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  1020. }
  1021. return len;
  1022. }
  1023. static int beep_write(char *buf)
  1024. {
  1025. char *cmd;
  1026. int beep_cmd;
  1027. if (!beep_handle)
  1028. return -ENODEV;
  1029. while ((cmd = next_cmd(&buf))) {
  1030. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  1031. beep_cmd >= 0 && beep_cmd <= 17) {
  1032. /* beep_cmd set */
  1033. } else
  1034. return -EINVAL;
  1035. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
  1036. return -EIO;
  1037. }
  1038. return 0;
  1039. }
  1040. static int acpi_ec_read(int i, u8 * p)
  1041. {
  1042. int v;
  1043. if (ecrd_handle) {
  1044. if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
  1045. return 0;
  1046. *p = v;
  1047. } else {
  1048. if (ec_read(i, p) < 0)
  1049. return 0;
  1050. }
  1051. return 1;
  1052. }
  1053. static int acpi_ec_write(int i, u8 v)
  1054. {
  1055. if (ecwr_handle) {
  1056. if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
  1057. return 0;
  1058. } else {
  1059. if (ec_write(i, v) < 0)
  1060. return 0;
  1061. }
  1062. return 1;
  1063. }
  1064. static enum thermal_access_mode thermal_read_mode;
  1065. static int thermal_init(void)
  1066. {
  1067. u8 t, ta1, ta2;
  1068. int i;
  1069. int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  1070. if (ibm_thinkpad_ec_found && experimental) {
  1071. /*
  1072. * Direct EC access mode: sensors at registers
  1073. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  1074. * non-implemented, thermal sensors return 0x80 when
  1075. * not available
  1076. */
  1077. ta1 = ta2 = 0;
  1078. for (i = 0; i < 8; i++) {
  1079. if (likely(acpi_ec_read(0x78 + i, &t))) {
  1080. ta1 |= t;
  1081. } else {
  1082. ta1 = 0;
  1083. break;
  1084. }
  1085. if (likely(acpi_ec_read(0xC0 + i, &t))) {
  1086. ta2 |= t;
  1087. } else {
  1088. ta1 = 0;
  1089. break;
  1090. }
  1091. }
  1092. if (ta1 == 0) {
  1093. /* This is sheer paranoia, but we handle it anyway */
  1094. if (acpi_tmp7) {
  1095. printk(IBM_ERR
  1096. "ThinkPad ACPI EC access misbehaving, "
  1097. "falling back to ACPI TMPx access mode\n");
  1098. thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
  1099. } else {
  1100. printk(IBM_ERR
  1101. "ThinkPad ACPI EC access misbehaving, "
  1102. "disabling thermal sensors access\n");
  1103. thermal_read_mode = IBMACPI_THERMAL_NONE;
  1104. }
  1105. } else {
  1106. thermal_read_mode =
  1107. (ta2 != 0) ?
  1108. IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
  1109. }
  1110. } else if (acpi_tmp7) {
  1111. if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  1112. /* 600e/x, 770e, 770x */
  1113. thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
  1114. } else {
  1115. /* Standard ACPI TMPx access, max 8 sensors */
  1116. thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
  1117. }
  1118. } else {
  1119. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  1120. thermal_read_mode = IBMACPI_THERMAL_NONE;
  1121. }
  1122. return 0;
  1123. }
  1124. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  1125. {
  1126. int i, t;
  1127. s8 tmp;
  1128. char tmpi[] = "TMPi";
  1129. if (!s)
  1130. return -EINVAL;
  1131. switch (thermal_read_mode) {
  1132. #if IBMACPI_MAX_THERMAL_SENSORS >= 16
  1133. case IBMACPI_THERMAL_TPEC_16:
  1134. for (i = 0; i < 8; i++) {
  1135. if (!acpi_ec_read(0xC0 + i, &tmp))
  1136. return -EIO;
  1137. s->temp[i + 8] = tmp * 1000;
  1138. }
  1139. /* fallthrough */
  1140. #endif
  1141. case IBMACPI_THERMAL_TPEC_8:
  1142. for (i = 0; i < 8; i++) {
  1143. if (!acpi_ec_read(0x78 + i, &tmp))
  1144. return -EIO;
  1145. s->temp[i] = tmp * 1000;
  1146. }
  1147. return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
  1148. case IBMACPI_THERMAL_ACPI_UPDT:
  1149. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  1150. return -EIO;
  1151. for (i = 0; i < 8; i++) {
  1152. tmpi[3] = '0' + i;
  1153. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1154. return -EIO;
  1155. s->temp[i] = (t - 2732) * 100;
  1156. }
  1157. return 8;
  1158. case IBMACPI_THERMAL_ACPI_TMP07:
  1159. for (i = 0; i < 8; i++) {
  1160. tmpi[3] = '0' + i;
  1161. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1162. return -EIO;
  1163. s->temp[i] = t * 1000;
  1164. }
  1165. return 8;
  1166. case IBMACPI_THERMAL_NONE:
  1167. default:
  1168. return 0;
  1169. }
  1170. }
  1171. static int thermal_read(char *p)
  1172. {
  1173. int len = 0;
  1174. int n, i;
  1175. struct ibm_thermal_sensors_struct t;
  1176. n = thermal_get_sensors(&t);
  1177. if (unlikely(n < 0))
  1178. return n;
  1179. len += sprintf(p + len, "temperatures:\t");
  1180. if (n > 0) {
  1181. for (i = 0; i < (n - 1); i++)
  1182. len += sprintf(p + len, "%d ", t.temp[i] / 1000);
  1183. len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
  1184. } else
  1185. len += sprintf(p + len, "not supported\n");
  1186. return len;
  1187. }
  1188. static u8 ecdump_regs[256];
  1189. static int ecdump_read(char *p)
  1190. {
  1191. int len = 0;
  1192. int i, j;
  1193. u8 v;
  1194. len += sprintf(p + len, "EC "
  1195. " +00 +01 +02 +03 +04 +05 +06 +07"
  1196. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  1197. for (i = 0; i < 256; i += 16) {
  1198. len += sprintf(p + len, "EC 0x%02x:", i);
  1199. for (j = 0; j < 16; j++) {
  1200. if (!acpi_ec_read(i + j, &v))
  1201. break;
  1202. if (v != ecdump_regs[i + j])
  1203. len += sprintf(p + len, " *%02x", v);
  1204. else
  1205. len += sprintf(p + len, " %02x", v);
  1206. ecdump_regs[i + j] = v;
  1207. }
  1208. len += sprintf(p + len, "\n");
  1209. if (j != 16)
  1210. break;
  1211. }
  1212. /* These are way too dangerous to advertise openly... */
  1213. #if 0
  1214. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  1215. " (<offset> is 00-ff, <value> is 00-ff)\n");
  1216. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  1217. " (<offset> is 00-ff, <value> is 0-255)\n");
  1218. #endif
  1219. return len;
  1220. }
  1221. static int ecdump_write(char *buf)
  1222. {
  1223. char *cmd;
  1224. int i, v;
  1225. while ((cmd = next_cmd(&buf))) {
  1226. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  1227. /* i and v set */
  1228. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  1229. /* i and v set */
  1230. } else
  1231. return -EINVAL;
  1232. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  1233. if (!acpi_ec_write(i, v))
  1234. return -EIO;
  1235. } else
  1236. return -EINVAL;
  1237. }
  1238. return 0;
  1239. }
  1240. static int brightness_offset = 0x31;
  1241. static int brightness_get(struct backlight_device *bd)
  1242. {
  1243. u8 level;
  1244. if (!acpi_ec_read(brightness_offset, &level))
  1245. return -EIO;
  1246. level &= 0x7;
  1247. return level;
  1248. }
  1249. static int brightness_read(char *p)
  1250. {
  1251. int len = 0;
  1252. int level;
  1253. if ((level = brightness_get(NULL)) < 0) {
  1254. len += sprintf(p + len, "level:\t\tunreadable\n");
  1255. } else {
  1256. len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
  1257. len += sprintf(p + len, "commands:\tup, down\n");
  1258. len += sprintf(p + len, "commands:\tlevel <level>"
  1259. " (<level> is 0-7)\n");
  1260. }
  1261. return len;
  1262. }
  1263. #define BRIGHTNESS_UP 4
  1264. #define BRIGHTNESS_DOWN 5
  1265. static int brightness_set(int value)
  1266. {
  1267. int cmos_cmd, inc, i;
  1268. int current_value = brightness_get(NULL);
  1269. value &= 7;
  1270. cmos_cmd = value > current_value ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
  1271. inc = value > current_value ? 1 : -1;
  1272. for (i = current_value; i != value; i += inc) {
  1273. if (!cmos_eval(cmos_cmd))
  1274. return -EIO;
  1275. if (!acpi_ec_write(brightness_offset, i + inc))
  1276. return -EIO;
  1277. }
  1278. return 0;
  1279. }
  1280. static int brightness_write(char *buf)
  1281. {
  1282. int level;
  1283. int new_level;
  1284. char *cmd;
  1285. while ((cmd = next_cmd(&buf))) {
  1286. if ((level = brightness_get(NULL)) < 0)
  1287. return level;
  1288. level &= 7;
  1289. if (strlencmp(cmd, "up") == 0) {
  1290. new_level = level == 7 ? 7 : level + 1;
  1291. } else if (strlencmp(cmd, "down") == 0) {
  1292. new_level = level == 0 ? 0 : level - 1;
  1293. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  1294. new_level >= 0 && new_level <= 7) {
  1295. /* new_level set */
  1296. } else
  1297. return -EINVAL;
  1298. brightness_set(new_level);
  1299. }
  1300. return 0;
  1301. }
  1302. static int brightness_update_status(struct backlight_device *bd)
  1303. {
  1304. return brightness_set(bd->props->brightness);
  1305. }
  1306. static int volume_offset = 0x30;
  1307. static int volume_read(char *p)
  1308. {
  1309. int len = 0;
  1310. u8 level;
  1311. if (!acpi_ec_read(volume_offset, &level)) {
  1312. len += sprintf(p + len, "level:\t\tunreadable\n");
  1313. } else {
  1314. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  1315. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  1316. len += sprintf(p + len, "commands:\tup, down, mute\n");
  1317. len += sprintf(p + len, "commands:\tlevel <level>"
  1318. " (<level> is 0-15)\n");
  1319. }
  1320. return len;
  1321. }
  1322. #define VOLUME_DOWN 0
  1323. #define VOLUME_UP 1
  1324. #define VOLUME_MUTE 2
  1325. static int volume_write(char *buf)
  1326. {
  1327. int cmos_cmd, inc, i;
  1328. u8 level, mute;
  1329. int new_level, new_mute;
  1330. char *cmd;
  1331. while ((cmd = next_cmd(&buf))) {
  1332. if (!acpi_ec_read(volume_offset, &level))
  1333. return -EIO;
  1334. new_mute = mute = level & 0x40;
  1335. new_level = level = level & 0xf;
  1336. if (strlencmp(cmd, "up") == 0) {
  1337. if (mute)
  1338. new_mute = 0;
  1339. else
  1340. new_level = level == 15 ? 15 : level + 1;
  1341. } else if (strlencmp(cmd, "down") == 0) {
  1342. if (mute)
  1343. new_mute = 0;
  1344. else
  1345. new_level = level == 0 ? 0 : level - 1;
  1346. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  1347. new_level >= 0 && new_level <= 15) {
  1348. /* new_level set */
  1349. } else if (strlencmp(cmd, "mute") == 0) {
  1350. new_mute = 0x40;
  1351. } else
  1352. return -EINVAL;
  1353. if (new_level != level) { /* mute doesn't change */
  1354. cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
  1355. inc = new_level > level ? 1 : -1;
  1356. if (mute && (!cmos_eval(cmos_cmd) ||
  1357. !acpi_ec_write(volume_offset, level)))
  1358. return -EIO;
  1359. for (i = level; i != new_level; i += inc)
  1360. if (!cmos_eval(cmos_cmd) ||
  1361. !acpi_ec_write(volume_offset, i + inc))
  1362. return -EIO;
  1363. if (mute && (!cmos_eval(VOLUME_MUTE) ||
  1364. !acpi_ec_write(volume_offset,
  1365. new_level + mute)))
  1366. return -EIO;
  1367. }
  1368. if (new_mute != mute) { /* level doesn't change */
  1369. cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
  1370. if (!cmos_eval(cmos_cmd) ||
  1371. !acpi_ec_write(volume_offset, level + new_mute))
  1372. return -EIO;
  1373. }
  1374. }
  1375. return 0;
  1376. }
  1377. static int fan_status_offset = 0x2f;
  1378. static int fan_rpm_offset = 0x84;
  1379. static int fan_read(char *p)
  1380. {
  1381. int len = 0;
  1382. int s;
  1383. u8 lo, hi, status;
  1384. if (gfan_handle) {
  1385. /* 570, 600e/x, 770e, 770x */
  1386. if (!acpi_evalf(gfan_handle, &s, NULL, "d"))
  1387. return -EIO;
  1388. len += sprintf(p + len, "level:\t\t%d\n", s);
  1389. } else {
  1390. /* all except 570, 600e/x, 770e, 770x */
  1391. if (!acpi_ec_read(fan_status_offset, &status))
  1392. len += sprintf(p + len, "status:\t\tunreadable\n");
  1393. else
  1394. len += sprintf(p + len, "status:\t\t%s\n",
  1395. enabled(status, 7));
  1396. if (!acpi_ec_read(fan_rpm_offset, &lo) ||
  1397. !acpi_ec_read(fan_rpm_offset + 1, &hi))
  1398. len += sprintf(p + len, "speed:\t\tunreadable\n");
  1399. else
  1400. len += sprintf(p + len, "speed:\t\t%d\n",
  1401. (hi << 8) + lo);
  1402. }
  1403. if (sfan_handle)
  1404. /* 570, 770x-JL */
  1405. len += sprintf(p + len, "commands:\tlevel <level>"
  1406. " (<level> is 0-7)\n");
  1407. if (!gfan_handle)
  1408. /* all except 570, 600e/x, 770e, 770x */
  1409. len += sprintf(p + len, "commands:\tenable, disable\n");
  1410. if (fans_handle)
  1411. /* X31, X40 */
  1412. len += sprintf(p + len, "commands:\tspeed <speed>"
  1413. " (<speed> is 0-65535)\n");
  1414. return len;
  1415. }
  1416. static int fan_write(char *buf)
  1417. {
  1418. char *cmd;
  1419. int level, speed;
  1420. while ((cmd = next_cmd(&buf))) {
  1421. if (sfan_handle &&
  1422. sscanf(cmd, "level %d", &level) == 1 &&
  1423. level >= 0 && level <= 7) {
  1424. /* 570, 770x-JL */
  1425. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  1426. return -EIO;
  1427. } else if (!gfan_handle && strlencmp(cmd, "enable") == 0) {
  1428. /* all except 570, 600e/x, 770e, 770x */
  1429. if (!acpi_ec_write(fan_status_offset, 0x80))
  1430. return -EIO;
  1431. } else if (!gfan_handle && strlencmp(cmd, "disable") == 0) {
  1432. /* all except 570, 600e/x, 770e, 770x */
  1433. if (!acpi_ec_write(fan_status_offset, 0x00))
  1434. return -EIO;
  1435. } else if (fans_handle &&
  1436. sscanf(cmd, "speed %d", &speed) == 1 &&
  1437. speed >= 0 && speed <= 65535) {
  1438. /* X31, X40 */
  1439. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  1440. speed, speed, speed))
  1441. return -EIO;
  1442. } else
  1443. return -EINVAL;
  1444. }
  1445. return 0;
  1446. }
  1447. static struct ibm_struct ibms[] = {
  1448. {
  1449. .name = "driver",
  1450. .init = driver_init,
  1451. .read = driver_read,
  1452. },
  1453. {
  1454. .name = "hotkey",
  1455. .hid = IBM_HKEY_HID,
  1456. .init = hotkey_init,
  1457. .read = hotkey_read,
  1458. .write = hotkey_write,
  1459. .exit = hotkey_exit,
  1460. .notify = hotkey_notify,
  1461. .handle = &hkey_handle,
  1462. .type = ACPI_DEVICE_NOTIFY,
  1463. },
  1464. {
  1465. .name = "bluetooth",
  1466. .init = bluetooth_init,
  1467. .read = bluetooth_read,
  1468. .write = bluetooth_write,
  1469. },
  1470. {
  1471. .name = "wan",
  1472. .init = wan_init,
  1473. .read = wan_read,
  1474. .write = wan_write,
  1475. .experimental = 1,
  1476. },
  1477. {
  1478. .name = "video",
  1479. .init = video_init,
  1480. .read = video_read,
  1481. .write = video_write,
  1482. .exit = video_exit,
  1483. },
  1484. {
  1485. .name = "light",
  1486. .init = light_init,
  1487. .read = light_read,
  1488. .write = light_write,
  1489. },
  1490. #ifdef CONFIG_ACPI_IBM_DOCK
  1491. {
  1492. .name = "dock",
  1493. .read = dock_read,
  1494. .write = dock_write,
  1495. .notify = dock_notify,
  1496. .handle = &dock_handle,
  1497. .type = ACPI_SYSTEM_NOTIFY,
  1498. },
  1499. {
  1500. .name = "dock",
  1501. .hid = IBM_PCI_HID,
  1502. .notify = dock_notify,
  1503. .handle = &pci_handle,
  1504. .type = ACPI_SYSTEM_NOTIFY,
  1505. },
  1506. #endif
  1507. {
  1508. .name = "bay",
  1509. .init = bay_init,
  1510. .read = bay_read,
  1511. .write = bay_write,
  1512. .notify = bay_notify,
  1513. .handle = &bay_handle,
  1514. .type = ACPI_SYSTEM_NOTIFY,
  1515. },
  1516. {
  1517. .name = "cmos",
  1518. .read = cmos_read,
  1519. .write = cmos_write,
  1520. },
  1521. {
  1522. .name = "led",
  1523. .init = led_init,
  1524. .read = led_read,
  1525. .write = led_write,
  1526. },
  1527. {
  1528. .name = "beep",
  1529. .read = beep_read,
  1530. .write = beep_write,
  1531. },
  1532. {
  1533. .name = "thermal",
  1534. .init = thermal_init,
  1535. .read = thermal_read,
  1536. },
  1537. {
  1538. .name = "ecdump",
  1539. .read = ecdump_read,
  1540. .write = ecdump_write,
  1541. .experimental = 1,
  1542. },
  1543. {
  1544. .name = "brightness",
  1545. .read = brightness_read,
  1546. .write = brightness_write,
  1547. },
  1548. {
  1549. .name = "volume",
  1550. .read = volume_read,
  1551. .write = volume_write,
  1552. },
  1553. {
  1554. .name = "fan",
  1555. .read = fan_read,
  1556. .write = fan_write,
  1557. .experimental = 1,
  1558. },
  1559. };
  1560. static int dispatch_read(char *page, char **start, off_t off, int count,
  1561. int *eof, void *data)
  1562. {
  1563. struct ibm_struct *ibm = (struct ibm_struct *)data;
  1564. int len;
  1565. if (!ibm || !ibm->read)
  1566. return -EINVAL;
  1567. len = ibm->read(page);
  1568. if (len < 0)
  1569. return len;
  1570. if (len <= off + count)
  1571. *eof = 1;
  1572. *start = page + off;
  1573. len -= off;
  1574. if (len > count)
  1575. len = count;
  1576. if (len < 0)
  1577. len = 0;
  1578. return len;
  1579. }
  1580. static int dispatch_write(struct file *file, const char __user * userbuf,
  1581. unsigned long count, void *data)
  1582. {
  1583. struct ibm_struct *ibm = (struct ibm_struct *)data;
  1584. char *kernbuf;
  1585. int ret;
  1586. if (!ibm || !ibm->write)
  1587. return -EINVAL;
  1588. kernbuf = kmalloc(count + 2, GFP_KERNEL);
  1589. if (!kernbuf)
  1590. return -ENOMEM;
  1591. if (copy_from_user(kernbuf, userbuf, count)) {
  1592. kfree(kernbuf);
  1593. return -EFAULT;
  1594. }
  1595. kernbuf[count] = 0;
  1596. strcat(kernbuf, ",");
  1597. ret = ibm->write(kernbuf);
  1598. if (ret == 0)
  1599. ret = count;
  1600. kfree(kernbuf);
  1601. return ret;
  1602. }
  1603. static void dispatch_notify(acpi_handle handle, u32 event, void *data)
  1604. {
  1605. struct ibm_struct *ibm = (struct ibm_struct *)data;
  1606. if (!ibm || !ibm->notify)
  1607. return;
  1608. ibm->notify(ibm, event);
  1609. }
  1610. static int __init setup_notify(struct ibm_struct *ibm)
  1611. {
  1612. acpi_status status;
  1613. int ret;
  1614. if (!*ibm->handle)
  1615. return 0;
  1616. ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
  1617. if (ret < 0) {
  1618. printk(IBM_ERR "%s device not present\n", ibm->name);
  1619. return 0;
  1620. }
  1621. acpi_driver_data(ibm->device) = ibm;
  1622. sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
  1623. status = acpi_install_notify_handler(*ibm->handle, ibm->type,
  1624. dispatch_notify, ibm);
  1625. if (ACPI_FAILURE(status)) {
  1626. printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
  1627. ibm->name, status);
  1628. return -ENODEV;
  1629. }
  1630. return 0;
  1631. }
  1632. static int __init ibm_device_add(struct acpi_device *device)
  1633. {
  1634. return 0;
  1635. }
  1636. static int __init register_driver(struct ibm_struct *ibm)
  1637. {
  1638. int ret;
  1639. ibm->driver = kmalloc(sizeof(struct acpi_driver), GFP_KERNEL);
  1640. if (!ibm->driver) {
  1641. printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
  1642. return -1;
  1643. }
  1644. memset(ibm->driver, 0, sizeof(struct acpi_driver));
  1645. sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
  1646. ibm->driver->ids = ibm->hid;
  1647. ibm->driver->ops.add = &ibm_device_add;
  1648. ret = acpi_bus_register_driver(ibm->driver);
  1649. if (ret < 0) {
  1650. printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
  1651. ibm->hid, ret);
  1652. kfree(ibm->driver);
  1653. }
  1654. return ret;
  1655. }
  1656. static int __init ibm_init(struct ibm_struct *ibm)
  1657. {
  1658. int ret;
  1659. struct proc_dir_entry *entry;
  1660. if (ibm->experimental && !experimental)
  1661. return 0;
  1662. if (ibm->hid) {
  1663. ret = register_driver(ibm);
  1664. if (ret < 0)
  1665. return ret;
  1666. ibm->driver_registered = 1;
  1667. }
  1668. if (ibm->init) {
  1669. ret = ibm->init();
  1670. if (ret != 0)
  1671. return ret;
  1672. ibm->init_called = 1;
  1673. }
  1674. if (ibm->read) {
  1675. entry = create_proc_entry(ibm->name,
  1676. S_IFREG | S_IRUGO | S_IWUSR,
  1677. proc_dir);
  1678. if (!entry) {
  1679. printk(IBM_ERR "unable to create proc entry %s\n",
  1680. ibm->name);
  1681. return -ENODEV;
  1682. }
  1683. entry->owner = THIS_MODULE;
  1684. entry->data = ibm;
  1685. entry->read_proc = &dispatch_read;
  1686. if (ibm->write)
  1687. entry->write_proc = &dispatch_write;
  1688. ibm->proc_created = 1;
  1689. }
  1690. if (ibm->notify) {
  1691. ret = setup_notify(ibm);
  1692. if (ret < 0)
  1693. return ret;
  1694. ibm->notify_installed = 1;
  1695. }
  1696. return 0;
  1697. }
  1698. static void ibm_exit(struct ibm_struct *ibm)
  1699. {
  1700. if (ibm->notify_installed)
  1701. acpi_remove_notify_handler(*ibm->handle, ibm->type,
  1702. dispatch_notify);
  1703. if (ibm->proc_created)
  1704. remove_proc_entry(ibm->name, proc_dir);
  1705. if (ibm->init_called && ibm->exit)
  1706. ibm->exit();
  1707. if (ibm->driver_registered) {
  1708. acpi_bus_unregister_driver(ibm->driver);
  1709. kfree(ibm->driver);
  1710. }
  1711. }
  1712. static void __init ibm_handle_init(char *name,
  1713. acpi_handle * handle, acpi_handle parent,
  1714. char **paths, int num_paths, char **path)
  1715. {
  1716. int i;
  1717. acpi_status status;
  1718. for (i = 0; i < num_paths; i++) {
  1719. status = acpi_get_handle(parent, paths[i], handle);
  1720. if (ACPI_SUCCESS(status)) {
  1721. *path = paths[i];
  1722. return;
  1723. }
  1724. }
  1725. *handle = NULL;
  1726. }
  1727. #define IBM_HANDLE_INIT(object) \
  1728. ibm_handle_init(#object, &object##_handle, *object##_parent, \
  1729. object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
  1730. static int set_ibm_param(const char *val, struct kernel_param *kp)
  1731. {
  1732. unsigned int i;
  1733. for (i = 0; i < ARRAY_SIZE(ibms); i++)
  1734. if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
  1735. if (strlen(val) > sizeof(ibms[i].param) - 2)
  1736. return -ENOSPC;
  1737. strcpy(ibms[i].param, val);
  1738. strcat(ibms[i].param, ",");
  1739. return 0;
  1740. }
  1741. return -EINVAL;
  1742. }
  1743. #define IBM_PARAM(feature) \
  1744. module_param_call(feature, set_ibm_param, NULL, NULL, 0)
  1745. IBM_PARAM(hotkey);
  1746. IBM_PARAM(bluetooth);
  1747. IBM_PARAM(video);
  1748. IBM_PARAM(light);
  1749. #ifdef CONFIG_ACPI_IBM_DOCK
  1750. IBM_PARAM(dock);
  1751. #endif
  1752. IBM_PARAM(bay);
  1753. IBM_PARAM(cmos);
  1754. IBM_PARAM(led);
  1755. IBM_PARAM(beep);
  1756. IBM_PARAM(ecdump);
  1757. IBM_PARAM(brightness);
  1758. IBM_PARAM(volume);
  1759. IBM_PARAM(fan);
  1760. static struct backlight_properties ibm_backlight_data = {
  1761. .owner = THIS_MODULE,
  1762. .get_brightness = brightness_get,
  1763. .update_status = brightness_update_status,
  1764. .max_brightness = 7,
  1765. };
  1766. static void acpi_ibm_exit(void)
  1767. {
  1768. int i;
  1769. if (ibm_backlight_device)
  1770. backlight_device_unregister(ibm_backlight_device);
  1771. for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
  1772. ibm_exit(&ibms[i]);
  1773. remove_proc_entry(IBM_DIR, acpi_root_dir);
  1774. }
  1775. static int __init check_dmi_for_ec(void)
  1776. {
  1777. struct dmi_device *dev = NULL;
  1778. /*
  1779. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  1780. * X32 or newer, all Z series; Some models must have an
  1781. * up-to-date BIOS or they will not be detected.
  1782. *
  1783. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  1784. */
  1785. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  1786. if (strstr(dev->name, "IBM ThinkPad Embedded Controller"))
  1787. return 1;
  1788. }
  1789. return 0;
  1790. }
  1791. static int __init acpi_ibm_init(void)
  1792. {
  1793. int ret, i;
  1794. if (acpi_disabled)
  1795. return -ENODEV;
  1796. if (!acpi_specific_hotkey_enabled) {
  1797. printk(IBM_ERR "using generic hotkey driver\n");
  1798. return -ENODEV;
  1799. }
  1800. /* ec is required because many other handles are relative to it */
  1801. IBM_HANDLE_INIT(ec);
  1802. if (!ec_handle) {
  1803. printk(IBM_ERR "ec object not found\n");
  1804. return -ENODEV;
  1805. }
  1806. /* Models with newer firmware report the EC in DMI */
  1807. ibm_thinkpad_ec_found = check_dmi_for_ec();
  1808. /* these handles are not required */
  1809. IBM_HANDLE_INIT(vid);
  1810. IBM_HANDLE_INIT(vid2);
  1811. IBM_HANDLE_INIT(ledb);
  1812. IBM_HANDLE_INIT(led);
  1813. IBM_HANDLE_INIT(hkey);
  1814. IBM_HANDLE_INIT(lght);
  1815. IBM_HANDLE_INIT(cmos);
  1816. #ifdef CONFIG_ACPI_IBM_DOCK
  1817. IBM_HANDLE_INIT(dock);
  1818. #endif
  1819. IBM_HANDLE_INIT(pci);
  1820. IBM_HANDLE_INIT(bay);
  1821. if (bay_handle)
  1822. IBM_HANDLE_INIT(bay_ej);
  1823. IBM_HANDLE_INIT(bay2);
  1824. if (bay2_handle)
  1825. IBM_HANDLE_INIT(bay2_ej);
  1826. IBM_HANDLE_INIT(beep);
  1827. IBM_HANDLE_INIT(ecrd);
  1828. IBM_HANDLE_INIT(ecwr);
  1829. IBM_HANDLE_INIT(fans);
  1830. IBM_HANDLE_INIT(gfan);
  1831. IBM_HANDLE_INIT(sfan);
  1832. proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
  1833. if (!proc_dir) {
  1834. printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
  1835. return -ENODEV;
  1836. }
  1837. proc_dir->owner = THIS_MODULE;
  1838. for (i = 0; i < ARRAY_SIZE(ibms); i++) {
  1839. ret = ibm_init(&ibms[i]);
  1840. if (ret >= 0 && *ibms[i].param)
  1841. ret = ibms[i].write(ibms[i].param);
  1842. if (ret < 0) {
  1843. acpi_ibm_exit();
  1844. return ret;
  1845. }
  1846. }
  1847. ibm_backlight_device = backlight_device_register("ibm", NULL,
  1848. &ibm_backlight_data);
  1849. if (IS_ERR(ibm_backlight_device)) {
  1850. printk(IBM_ERR "Could not register ibm backlight device\n");
  1851. ibm_backlight_device = NULL;
  1852. acpi_ibm_exit();
  1853. }
  1854. return 0;
  1855. }
  1856. module_init(acpi_ibm_init);
  1857. module_exit(acpi_ibm_exit);