asus_acpi.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. /*
  2. * asus_acpi.c - Asus Laptop ACPI Extras
  3. *
  4. *
  5. * Copyright (C) 2002, 2003, 2004 Julien Lerouge, Karol Kozimor
  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. *
  22. * The development page for this driver is located at
  23. * http://sourceforge.net/projects/acpi4asus/
  24. *
  25. * Credits:
  26. * Pontus Fuchs - Helper functions, cleanup
  27. * Johann Wiesner - Small compile fixes
  28. * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
  29. *
  30. * TODO:
  31. * add Fn key status
  32. * Add mode selection on module loading (parameter) -> still necessary?
  33. * Complete display switching -- may require dirty hacks or calling _DOS?
  34. */
  35. #include <linux/kernel.h>
  36. #include <linux/module.h>
  37. #include <linux/init.h>
  38. #include <linux/types.h>
  39. #include <linux/proc_fs.h>
  40. #include <acpi/acpi_drivers.h>
  41. #include <acpi/acpi_bus.h>
  42. #include <asm/uaccess.h>
  43. #define ASUS_ACPI_VERSION "0.29"
  44. #define PROC_ASUS "asus" //the directory
  45. #define PROC_MLED "mled"
  46. #define PROC_WLED "wled"
  47. #define PROC_TLED "tled"
  48. #define PROC_INFO "info"
  49. #define PROC_LCD "lcd"
  50. #define PROC_BRN "brn"
  51. #define PROC_DISP "disp"
  52. #define ACPI_HOTK_NAME "Asus Laptop ACPI Extras Driver"
  53. #define ACPI_HOTK_CLASS "hotkey"
  54. #define ACPI_HOTK_DEVICE_NAME "Hotkey"
  55. #define ACPI_HOTK_HID "ATK0100"
  56. /*
  57. * Some events we use, same for all Asus
  58. */
  59. #define BR_UP 0x10
  60. #define BR_DOWN 0x20
  61. /*
  62. * Flags for hotk status
  63. */
  64. #define MLED_ON 0x01 //is MLED ON ?
  65. #define WLED_ON 0x02
  66. #define TLED_ON 0x04
  67. MODULE_AUTHOR("Julien Lerouge, Karol Kozimor");
  68. MODULE_DESCRIPTION(ACPI_HOTK_NAME);
  69. MODULE_LICENSE("GPL");
  70. static uid_t asus_uid;
  71. static gid_t asus_gid;
  72. module_param(asus_uid, uint, 0);
  73. MODULE_PARM_DESC(uid, "UID for entries in /proc/acpi/asus.\n");
  74. module_param(asus_gid, uint, 0);
  75. MODULE_PARM_DESC(gid, "GID for entries in /proc/acpi/asus.\n");
  76. /* For each model, all features implemented,
  77. * those marked with R are relative to HOTK, A for absolute */
  78. struct model_data {
  79. char *name; //name of the laptop________________A
  80. char *mt_mled; //method to handle mled_____________R
  81. char *mled_status; //node to handle mled reading_______A
  82. char *mt_wled; //method to handle wled_____________R
  83. char *wled_status; //node to handle wled reading_______A
  84. char *mt_tled; //method to handle tled_____________R
  85. char *tled_status; //node to handle tled reading_______A
  86. char *mt_lcd_switch; //method to turn LCD ON/OFF_________A
  87. char *lcd_status; //node to read LCD panel state______A
  88. char *brightness_up; //method to set brightness up_______A
  89. char *brightness_down; //guess what ?______________________A
  90. char *brightness_set; //method to set absolute brightness_R
  91. char *brightness_get; //method to get absolute brightness_R
  92. char *brightness_status; //node to get brightness____________A
  93. char *display_set; //method to set video output________R
  94. char *display_get; //method to get video output________R
  95. };
  96. /*
  97. * This is the main structure, we can use it to store anything interesting
  98. * about the hotk device
  99. */
  100. struct asus_hotk {
  101. struct acpi_device *device; //the device we are in
  102. acpi_handle handle; //the handle of the hotk device
  103. char status; //status of the hotk, for LEDs, ...
  104. struct model_data *methods; //methods available on the laptop
  105. u8 brightness; //brightness level
  106. enum {
  107. A1x = 0, //A1340D, A1300F
  108. A2x, //A2500H
  109. D1x, //D1
  110. L2D, //L2000D
  111. L3C, //L3800C
  112. L3D, //L3400D
  113. L3H, //L3H, but also L2000E
  114. L4R, //L4500R
  115. L5x, //L5800C
  116. L8L, //L8400L
  117. M1A, //M1300A
  118. M2E, //M2400E, L4400L
  119. M6N, //M6800N
  120. M6R, //M6700R
  121. P30, //Samsung P30
  122. S1x, //S1300A, but also L1400B and M2400A (L84F)
  123. S2x, //S200 (J1 reported), Victor MP-XP7210
  124. xxN, //M2400N, M3700N, M5200N, S1300N, S5200N, W1OOON
  125. //(Centrino)
  126. END_MODEL
  127. } model; //Models currently supported
  128. u16 event_count[128]; //count for each event TODO make this better
  129. };
  130. /* Here we go */
  131. #define A1x_PREFIX "\\_SB.PCI0.ISA.EC0."
  132. #define L3C_PREFIX "\\_SB.PCI0.PX40.ECD0."
  133. #define M1A_PREFIX "\\_SB.PCI0.PX40.EC0."
  134. #define P30_PREFIX "\\_SB.PCI0.LPCB.EC0."
  135. #define S1x_PREFIX "\\_SB.PCI0.PX40."
  136. #define S2x_PREFIX A1x_PREFIX
  137. #define xxN_PREFIX "\\_SB.PCI0.SBRG.EC0."
  138. static struct model_data model_conf[END_MODEL] = {
  139. /*
  140. * Those pathnames are relative to the HOTK / ATKD device :
  141. * - mt_mled
  142. * - mt_wled
  143. * - brightness_set
  144. * - brightness_get
  145. * - display_set
  146. * - display_get
  147. *
  148. * TODO I have seen a SWBX and AIBX method on some models, like L1400B,
  149. * it seems to be a kind of switch, but what for ?
  150. *
  151. */
  152. {
  153. .name = "A1x",
  154. .mt_mled = "MLED",
  155. .mled_status = "\\MAIL",
  156. .mt_lcd_switch = A1x_PREFIX "_Q10",
  157. .lcd_status = "\\BKLI",
  158. .brightness_up = A1x_PREFIX "_Q0E",
  159. .brightness_down = A1x_PREFIX "_Q0F"
  160. },
  161. {
  162. .name = "A2x",
  163. .mt_mled = "MLED",
  164. .mt_wled = "WLED",
  165. .wled_status = "\\SG66",
  166. .mt_lcd_switch = "\\Q10",
  167. .lcd_status = "\\BAOF",
  168. .brightness_set = "SPLV",
  169. .brightness_get = "GPLV",
  170. .display_set = "SDSP",
  171. .display_get = "\\INFB"
  172. },
  173. {
  174. .name = "D1x",
  175. .mt_mled = "MLED",
  176. .mt_lcd_switch = "\\Q0D",
  177. .lcd_status = "\\GP11",
  178. .brightness_up = "\\Q0C",
  179. .brightness_down = "\\Q0B",
  180. .brightness_status = "\\BLVL",
  181. .display_set = "SDSP",
  182. .display_get = "\\INFB"
  183. },
  184. {
  185. .name = "L2D",
  186. .mt_mled = "MLED",
  187. .mled_status = "\\SGP6",
  188. .mt_wled = "WLED",
  189. .wled_status = "\\RCP3",
  190. .mt_lcd_switch = "\\Q10",
  191. .lcd_status = "\\SGP0",
  192. .brightness_up = "\\Q0E",
  193. .brightness_down = "\\Q0F",
  194. .display_set = "SDSP",
  195. .display_get = "\\INFB"
  196. },
  197. {
  198. .name = "L3C",
  199. .mt_mled = "MLED",
  200. .mt_wled = "WLED",
  201. .mt_lcd_switch = L3C_PREFIX "_Q10",
  202. .lcd_status = "\\GL32",
  203. .brightness_set = "SPLV",
  204. .brightness_get = "GPLV",
  205. .display_set = "SDSP",
  206. .display_get = "\\_SB.PCI0.PCI1.VGAC.NMAP"
  207. },
  208. {
  209. .name = "L3D",
  210. .mt_mled = "MLED",
  211. .mled_status = "\\MALD",
  212. .mt_wled = "WLED",
  213. .mt_lcd_switch = "\\Q10",
  214. .lcd_status = "\\BKLG",
  215. .brightness_set = "SPLV",
  216. .brightness_get = "GPLV",
  217. .display_set = "SDSP",
  218. .display_get = "\\INFB"
  219. },
  220. {
  221. .name = "L3H",
  222. .mt_mled = "MLED",
  223. .mt_wled = "WLED",
  224. .mt_lcd_switch = "EHK",
  225. .lcd_status = "\\_SB.PCI0.PM.PBC",
  226. .brightness_set = "SPLV",
  227. .brightness_get = "GPLV",
  228. .display_set = "SDSP",
  229. .display_get = "\\INFB"
  230. },
  231. {
  232. .name = "L4R",
  233. .mt_mled = "MLED",
  234. .mt_wled = "WLED",
  235. .wled_status = "\\_SB.PCI0.SBRG.SG13",
  236. .mt_lcd_switch = xxN_PREFIX "_Q10",
  237. .lcd_status = "\\_SB.PCI0.SBSM.SEO4",
  238. .brightness_set = "SPLV",
  239. .brightness_get = "GPLV",
  240. .display_set = "SDSP",
  241. .display_get = "\\_SB.PCI0.P0P1.VGA.GETD"
  242. },
  243. {
  244. .name = "L5x",
  245. .mt_mled = "MLED",
  246. /* WLED present, but not controlled by ACPI */
  247. .mt_tled = "TLED",
  248. .mt_lcd_switch = "\\Q0D",
  249. .lcd_status = "\\BAOF",
  250. .brightness_set = "SPLV",
  251. .brightness_get = "GPLV",
  252. .display_set = "SDSP",
  253. .display_get = "\\INFB"
  254. },
  255. {
  256. .name = "L8L"
  257. /* No features, but at least support the hotkeys */
  258. },
  259. {
  260. .name = "M1A",
  261. .mt_mled = "MLED",
  262. .mt_lcd_switch = M1A_PREFIX "Q10",
  263. .lcd_status = "\\PNOF",
  264. .brightness_up = M1A_PREFIX "Q0E",
  265. .brightness_down = M1A_PREFIX "Q0F",
  266. .brightness_status = "\\BRIT",
  267. .display_set = "SDSP",
  268. .display_get = "\\INFB"
  269. },
  270. {
  271. .name = "M2E",
  272. .mt_mled = "MLED",
  273. .mt_wled = "WLED",
  274. .mt_lcd_switch = "\\Q10",
  275. .lcd_status = "\\GP06",
  276. .brightness_set = "SPLV",
  277. .brightness_get = "GPLV",
  278. .display_set = "SDSP",
  279. .display_get = "\\INFB"
  280. },
  281. {
  282. .name = "M6N",
  283. .mt_mled = "MLED",
  284. .mt_wled = "WLED",
  285. .wled_status = "\\_SB.PCI0.SBRG.SG13",
  286. .mt_lcd_switch = xxN_PREFIX "_Q10",
  287. .lcd_status = "\\_SB.BKLT",
  288. .brightness_set = "SPLV",
  289. .brightness_get = "GPLV",
  290. .display_set = "SDSP",
  291. .display_get = "\\SSTE"
  292. },
  293. {
  294. .name = "M6R",
  295. .mt_mled = "MLED",
  296. .mt_wled = "WLED",
  297. .mt_lcd_switch = xxN_PREFIX "_Q10",
  298. .lcd_status = "\\_SB.PCI0.SBSM.SEO4",
  299. .brightness_set = "SPLV",
  300. .brightness_get = "GPLV",
  301. .display_set = "SDSP",
  302. .display_get = "\\SSTE"
  303. },
  304. {
  305. .name = "P30",
  306. .mt_wled = "WLED",
  307. .mt_lcd_switch = P30_PREFIX "_Q0E",
  308. .lcd_status = "\\BKLT",
  309. .brightness_up = P30_PREFIX "_Q68",
  310. .brightness_down = P30_PREFIX "_Q69",
  311. .brightness_get = "GPLV",
  312. .display_set = "SDSP",
  313. .display_get = "\\DNXT"
  314. },
  315. {
  316. .name = "S1x",
  317. .mt_mled = "MLED",
  318. .mled_status = "\\EMLE",
  319. .mt_wled = "WLED",
  320. .mt_lcd_switch = S1x_PREFIX "Q10" ,
  321. .lcd_status = "\\PNOF",
  322. .brightness_set = "SPLV",
  323. .brightness_get = "GPLV"
  324. },
  325. {
  326. .name = "S2x",
  327. .mt_mled = "MLED",
  328. .mled_status = "\\MAIL",
  329. .mt_lcd_switch = S2x_PREFIX "_Q10",
  330. .lcd_status = "\\BKLI",
  331. .brightness_up = S2x_PREFIX "_Q0B",
  332. .brightness_down = S2x_PREFIX "_Q0A"
  333. },
  334. {
  335. .name = "xxN",
  336. .mt_mled = "MLED",
  337. /* WLED present, but not controlled by ACPI */
  338. .mt_lcd_switch = xxN_PREFIX "_Q10",
  339. .lcd_status = "\\BKLT",
  340. .brightness_set = "SPLV",
  341. .brightness_get = "GPLV",
  342. .display_set = "SDSP",
  343. .display_get = "\\ADVG"
  344. }
  345. };
  346. /* procdir we use */
  347. static struct proc_dir_entry *asus_proc_dir;
  348. /*
  349. * This header is made available to allow proper configuration given model,
  350. * revision number , ... this info cannot go in struct asus_hotk because it is
  351. * available before the hotk
  352. */
  353. static struct acpi_table_header *asus_info;
  354. /* The actual device the driver binds to */
  355. static struct asus_hotk *hotk;
  356. /*
  357. * The hotkey driver declaration
  358. */
  359. static int asus_hotk_add(struct acpi_device *device);
  360. static int asus_hotk_remove(struct acpi_device *device, int type);
  361. static struct acpi_driver asus_hotk_driver = {
  362. .name = ACPI_HOTK_NAME,
  363. .class = ACPI_HOTK_CLASS,
  364. .ids = ACPI_HOTK_HID,
  365. .ops = {
  366. .add = asus_hotk_add,
  367. .remove = asus_hotk_remove,
  368. },
  369. };
  370. /*
  371. * This function evaluates an ACPI method, given an int as parameter, the
  372. * method is searched within the scope of the handle, can be NULL. The output
  373. * of the method is written is output, which can also be NULL
  374. *
  375. * returns 1 if write is successful, 0 else.
  376. */
  377. static int write_acpi_int(acpi_handle handle, const char *method, int val,
  378. struct acpi_buffer *output)
  379. {
  380. struct acpi_object_list params; //list of input parameters (an int here)
  381. union acpi_object in_obj; //the only param we use
  382. acpi_status status;
  383. params.count = 1;
  384. params.pointer = &in_obj;
  385. in_obj.type = ACPI_TYPE_INTEGER;
  386. in_obj.integer.value = val;
  387. status = acpi_evaluate_object(handle, (char *) method, &params, output);
  388. return (status == AE_OK);
  389. }
  390. static int read_acpi_int(acpi_handle handle, const char *method, int *val)
  391. {
  392. struct acpi_buffer output;
  393. union acpi_object out_obj;
  394. acpi_status status;
  395. output.length = sizeof(out_obj);
  396. output.pointer = &out_obj;
  397. status = acpi_evaluate_object(handle, (char *) method, NULL, &output);
  398. *val = out_obj.integer.value;
  399. return (status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER);
  400. }
  401. /*
  402. * We write our info in page, we begin at offset off and cannot write more
  403. * than count bytes. We set eof to 1 if we handle those 2 values. We return the
  404. * number of bytes written in page
  405. */
  406. static int
  407. proc_read_info(char *page, char **start, off_t off, int count, int *eof,
  408. void *data)
  409. {
  410. int len = 0;
  411. int temp;
  412. char buf[16]; //enough for all info
  413. /*
  414. * We use the easy way, we don't care of off and count, so we don't set eof
  415. * to 1
  416. */
  417. len += sprintf(page, ACPI_HOTK_NAME " " ASUS_ACPI_VERSION "\n");
  418. len += sprintf(page + len, "Model reference : %s\n",
  419. hotk->methods->name);
  420. /*
  421. * The SFUN method probably allows the original driver to get the list
  422. * of features supported by a given model. For now, 0x0100 or 0x0800
  423. * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
  424. * The significance of others is yet to be found.
  425. */
  426. if (read_acpi_int(hotk->handle, "SFUN", &temp))
  427. len += sprintf(page + len, "SFUN value : 0x%04x\n", temp);
  428. /*
  429. * Another value for userspace: the ASYM method returns 0x02 for
  430. * battery low and 0x04 for battery critical, its readings tend to be
  431. * more accurate than those provided by _BST.
  432. * Note: since not all the laptops provide this method, errors are
  433. * silently ignored.
  434. */
  435. if (read_acpi_int(hotk->handle, "ASYM", &temp))
  436. len += sprintf(page + len, "ASYM value : 0x%04x\n", temp);
  437. if (asus_info) {
  438. snprintf(buf, 16, "%d", asus_info->length);
  439. len += sprintf(page + len, "DSDT length : %s\n", buf);
  440. snprintf(buf, 16, "%d", asus_info->checksum);
  441. len += sprintf(page + len, "DSDT checksum : %s\n", buf);
  442. snprintf(buf, 16, "%d", asus_info->revision);
  443. len += sprintf(page + len, "DSDT revision : %s\n", buf);
  444. snprintf(buf, 7, "%s", asus_info->oem_id);
  445. len += sprintf(page + len, "OEM id : %s\n", buf);
  446. snprintf(buf, 9, "%s", asus_info->oem_table_id);
  447. len += sprintf(page + len, "OEM table id : %s\n", buf);
  448. snprintf(buf, 16, "%x", asus_info->oem_revision);
  449. len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
  450. snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
  451. len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
  452. snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
  453. len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
  454. }
  455. return len;
  456. }
  457. /*
  458. * /proc handlers
  459. * We write our info in page, we begin at offset off and cannot write more
  460. * than count bytes. We set eof to 1 if we handle those 2 values. We return the
  461. * number of bytes written in page
  462. */
  463. /* Generic LED functions */
  464. static int
  465. read_led(const char *ledname, int ledmask)
  466. {
  467. if (ledname) {
  468. int led_status;
  469. if (read_acpi_int(NULL, ledname, &led_status))
  470. return led_status;
  471. else
  472. printk(KERN_WARNING "Asus ACPI: Error reading LED "
  473. "status\n");
  474. }
  475. return (hotk->status & ledmask) ? 1 : 0;
  476. }
  477. static int parse_arg(const char __user *buf, unsigned long count, int *val)
  478. {
  479. char s[32];
  480. if (!count)
  481. return 0;
  482. if (count > 31)
  483. return -EINVAL;
  484. if (copy_from_user(s, buf, count))
  485. return -EFAULT;
  486. s[count] = 0;
  487. if (sscanf(s, "%i", val) != 1)
  488. return -EINVAL;
  489. return count;
  490. }
  491. /* FIXME: kill extraneous args so it can be called independently */
  492. static int
  493. write_led(const char __user *buffer, unsigned long count,
  494. char *ledname, int ledmask, int invert)
  495. {
  496. int value;
  497. int led_out = 0;
  498. count = parse_arg(buffer, count, &value);
  499. if (count > 0)
  500. led_out = value ? 1 : 0;
  501. hotk->status =
  502. (led_out) ? (hotk->status | ledmask) : (hotk->status & ~ledmask);
  503. if (invert) /* invert target value */
  504. led_out = !led_out & 0x1;
  505. if (!write_acpi_int(hotk->handle, ledname, led_out, NULL))
  506. printk(KERN_WARNING "Asus ACPI: LED (%s) write failed\n", ledname);
  507. return count;
  508. }
  509. /*
  510. * Proc handlers for MLED
  511. */
  512. static int
  513. proc_read_mled(char *page, char **start, off_t off, int count, int *eof,
  514. void *data)
  515. {
  516. return sprintf(page, "%d\n", read_led(hotk->methods->mled_status, MLED_ON));
  517. }
  518. static int
  519. proc_write_mled(struct file *file, const char __user *buffer,
  520. unsigned long count, void *data)
  521. {
  522. return write_led(buffer, count, hotk->methods->mt_mled, MLED_ON, 1);
  523. }
  524. /*
  525. * Proc handlers for WLED
  526. */
  527. static int
  528. proc_read_wled(char *page, char **start, off_t off, int count, int *eof,
  529. void *data)
  530. {
  531. return sprintf(page, "%d\n", read_led(hotk->methods->wled_status, WLED_ON));
  532. }
  533. static int
  534. proc_write_wled(struct file *file, const char __user *buffer,
  535. unsigned long count, void *data)
  536. {
  537. return write_led(buffer, count, hotk->methods->mt_wled, WLED_ON, 0);
  538. }
  539. /*
  540. * Proc handlers for TLED
  541. */
  542. static int
  543. proc_read_tled(char *page, char **start, off_t off, int count, int *eof,
  544. void *data)
  545. {
  546. return sprintf(page, "%d\n", read_led(hotk->methods->tled_status, TLED_ON));
  547. }
  548. static int
  549. proc_write_tled(struct file *file, const char __user *buffer,
  550. unsigned long count, void *data)
  551. {
  552. return write_led(buffer, count, hotk->methods->mt_tled, TLED_ON, 0);
  553. }
  554. static int get_lcd_state(void)
  555. {
  556. int lcd = 0;
  557. if (hotk->model != L3H) {
  558. /* We don't have to check anything if we are here */
  559. if (!read_acpi_int(NULL, hotk->methods->lcd_status, &lcd))
  560. printk(KERN_WARNING "Asus ACPI: Error reading LCD status\n");
  561. if (hotk->model == L2D)
  562. lcd = ~lcd;
  563. } else { /* L3H and the like have to be handled differently */
  564. acpi_status status = 0;
  565. struct acpi_object_list input;
  566. union acpi_object mt_params[2];
  567. struct acpi_buffer output;
  568. union acpi_object out_obj;
  569. input.count = 2;
  570. input.pointer = mt_params;
  571. /* Note: the following values are partly guessed up, but
  572. otherwise they seem to work */
  573. mt_params[0].type = ACPI_TYPE_INTEGER;
  574. mt_params[0].integer.value = 0x02;
  575. mt_params[1].type = ACPI_TYPE_INTEGER;
  576. mt_params[1].integer.value = 0x02;
  577. output.length = sizeof(out_obj);
  578. output.pointer = &out_obj;
  579. status = acpi_evaluate_object(NULL, hotk->methods->lcd_status, &input, &output);
  580. if (status != AE_OK)
  581. return -1;
  582. if (out_obj.type == ACPI_TYPE_INTEGER)
  583. /* That's what the AML code does */
  584. lcd = out_obj.integer.value >> 8;
  585. }
  586. return (lcd & 1);
  587. }
  588. static int set_lcd_state(int value)
  589. {
  590. int lcd = 0;
  591. acpi_status status = 0;
  592. lcd = value ? 1 : 0;
  593. if (lcd != get_lcd_state()) {
  594. /* switch */
  595. if (hotk->model != L3H) {
  596. status =
  597. acpi_evaluate_object(NULL, hotk->methods->mt_lcd_switch,
  598. NULL, NULL);
  599. } else { /* L3H and the like have to be handled differently */
  600. if (!write_acpi_int(hotk->handle, hotk->methods->mt_lcd_switch, 0x07, NULL))
  601. status = AE_ERROR;
  602. /* L3H's AML executes EHK (0x07) upon Fn+F7 keypress,
  603. the exact behaviour is simulated here */
  604. }
  605. if (ACPI_FAILURE(status))
  606. printk(KERN_WARNING "Asus ACPI: Error switching LCD\n");
  607. }
  608. return 0;
  609. }
  610. static int
  611. proc_read_lcd(char *page, char **start, off_t off, int count, int *eof,
  612. void *data)
  613. {
  614. return sprintf(page, "%d\n", get_lcd_state());
  615. }
  616. static int
  617. proc_write_lcd(struct file *file, const char __user *buffer,
  618. unsigned long count, void *data)
  619. {
  620. int value;
  621. count = parse_arg(buffer, count, &value);
  622. if (count > 0)
  623. set_lcd_state(value);
  624. return count;
  625. }
  626. static int read_brightness(void)
  627. {
  628. int value;
  629. if(hotk->methods->brightness_get) { /* SPLV/GPLV laptop */
  630. if (!read_acpi_int(hotk->handle, hotk->methods->brightness_get,
  631. &value))
  632. printk(KERN_WARNING "Asus ACPI: Error reading brightness\n");
  633. } else if (hotk->methods->brightness_status) { /* For D1 for example */
  634. if (!read_acpi_int(NULL, hotk->methods->brightness_status,
  635. &value))
  636. printk(KERN_WARNING "Asus ACPI: Error reading brightness\n");
  637. } else /* No GPLV method */
  638. value = hotk->brightness;
  639. return value;
  640. }
  641. /*
  642. * Change the brightness level
  643. */
  644. static void set_brightness(int value)
  645. {
  646. acpi_status status = 0;
  647. /* SPLV laptop */
  648. if(hotk->methods->brightness_set) {
  649. if (!write_acpi_int(hotk->handle, hotk->methods->brightness_set,
  650. value, NULL))
  651. printk(KERN_WARNING "Asus ACPI: Error changing brightness\n");
  652. return;
  653. }
  654. /* No SPLV method if we are here, act as appropriate */
  655. value -= read_brightness();
  656. while (value != 0) {
  657. status = acpi_evaluate_object(NULL, (value > 0) ?
  658. hotk->methods->brightness_up :
  659. hotk->methods->brightness_down,
  660. NULL, NULL);
  661. (value > 0) ? value-- : value++;
  662. if (ACPI_FAILURE(status))
  663. printk(KERN_WARNING "Asus ACPI: Error changing brightness\n");
  664. }
  665. return;
  666. }
  667. static int
  668. proc_read_brn(char *page, char **start, off_t off, int count, int *eof,
  669. void *data)
  670. {
  671. return sprintf(page, "%d\n", read_brightness());
  672. }
  673. static int
  674. proc_write_brn(struct file *file, const char __user *buffer,
  675. unsigned long count, void *data)
  676. {
  677. int value;
  678. count = parse_arg(buffer, count, &value);
  679. if (count > 0) {
  680. value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
  681. /* 0 <= value <= 15 */
  682. set_brightness(value);
  683. } else if (count < 0) {
  684. printk(KERN_WARNING "Asus ACPI: Error reading user input\n");
  685. }
  686. return count;
  687. }
  688. static void set_display(int value)
  689. {
  690. /* no sanity check needed for now */
  691. if (!write_acpi_int(hotk->handle, hotk->methods->display_set,
  692. value, NULL))
  693. printk(KERN_WARNING "Asus ACPI: Error setting display\n");
  694. return;
  695. }
  696. /*
  697. * Now, *this* one could be more user-friendly, but so far, no-one has
  698. * complained. The significance of bits is the same as in proc_write_disp()
  699. */
  700. static int
  701. proc_read_disp(char *page, char **start, off_t off, int count, int *eof,
  702. void *data)
  703. {
  704. int value = 0;
  705. if (!read_acpi_int(hotk->handle, hotk->methods->display_get, &value))
  706. printk(KERN_WARNING "Asus ACPI: Error reading display status\n");
  707. value &= 0x07; /* needed for some models, shouldn't hurt others */
  708. return sprintf(page, "%d\n", value);
  709. }
  710. /*
  711. * Experimental support for display switching. As of now: 1 should activate
  712. * the LCD output, 2 should do for CRT, and 4 for TV-Out. Any combination
  713. * (bitwise) of these will suffice. I never actually tested 3 displays hooked up
  714. * simultaneously, so be warned. See the acpi4asus README for more info.
  715. */
  716. static int
  717. proc_write_disp(struct file *file, const char __user *buffer,
  718. unsigned long count, void *data)
  719. {
  720. int value;
  721. count = parse_arg(buffer, count, &value);
  722. if (count > 0)
  723. set_display(value);
  724. else if (count < 0)
  725. printk(KERN_WARNING "Asus ACPI: Error reading user input\n");
  726. return count;
  727. }
  728. typedef int (proc_readfunc)(char *page, char **start, off_t off, int count,
  729. int *eof, void *data);
  730. typedef int (proc_writefunc)(struct file *file, const char __user *buffer,
  731. unsigned long count, void *data);
  732. static int
  733. __init asus_proc_add(char *name, proc_writefunc *writefunc,
  734. proc_readfunc *readfunc, mode_t mode,
  735. struct acpi_device *device)
  736. {
  737. struct proc_dir_entry *proc = create_proc_entry(name, mode, acpi_device_dir(device));
  738. if(!proc) {
  739. printk(KERN_WARNING " Unable to create %s fs entry\n", name);
  740. return -1;
  741. }
  742. proc->write_proc = writefunc;
  743. proc->read_proc = readfunc;
  744. proc->data = acpi_driver_data(device);
  745. proc->owner = THIS_MODULE;
  746. proc->uid = asus_uid;
  747. proc->gid = asus_gid;
  748. return 0;
  749. }
  750. static int __init asus_hotk_add_fs(struct acpi_device *device)
  751. {
  752. struct proc_dir_entry *proc;
  753. mode_t mode;
  754. /*
  755. * If parameter uid or gid is not changed, keep the default setting for
  756. * our proc entries (-rw-rw-rw-) else, it means we care about security,
  757. * and then set to -rw-rw----
  758. */
  759. if ((asus_uid == 0) && (asus_gid == 0)){
  760. mode = S_IFREG | S_IRUGO | S_IWUGO;
  761. } else {
  762. mode = S_IFREG | S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP;
  763. }
  764. acpi_device_dir(device) = asus_proc_dir;
  765. if (!acpi_device_dir(device))
  766. return -ENODEV;
  767. proc = create_proc_entry(PROC_INFO, mode, acpi_device_dir(device));
  768. if (proc) {
  769. proc->read_proc = proc_read_info;
  770. proc->data = acpi_driver_data(device);
  771. proc->owner = THIS_MODULE;
  772. proc->uid = asus_uid;
  773. proc->gid = asus_gid;
  774. } else {
  775. printk(KERN_WARNING " Unable to create " PROC_INFO
  776. " fs entry\n");
  777. }
  778. if (hotk->methods->mt_wled) {
  779. asus_proc_add(PROC_WLED, &proc_write_wled, &proc_read_wled, mode, device);
  780. }
  781. if (hotk->methods->mt_mled) {
  782. asus_proc_add(PROC_MLED, &proc_write_mled, &proc_read_mled, mode, device);
  783. }
  784. if (hotk->methods->mt_tled) {
  785. asus_proc_add(PROC_TLED, &proc_write_tled, &proc_read_tled, mode, device);
  786. }
  787. /*
  788. * We need both read node and write method as LCD switch is also accessible
  789. * from keyboard
  790. */
  791. if (hotk->methods->mt_lcd_switch && hotk->methods->lcd_status) {
  792. asus_proc_add(PROC_LCD, &proc_write_lcd, &proc_read_lcd, mode, device);
  793. }
  794. if ((hotk->methods->brightness_up && hotk->methods->brightness_down) ||
  795. (hotk->methods->brightness_get && hotk->methods->brightness_set)) {
  796. asus_proc_add(PROC_BRN, &proc_write_brn, &proc_read_brn, mode, device);
  797. }
  798. if (hotk->methods->display_set) {
  799. asus_proc_add(PROC_DISP, &proc_write_disp, &proc_read_disp, mode, device);
  800. }
  801. return 0;
  802. }
  803. static int asus_hotk_remove_fs(struct acpi_device* device)
  804. {
  805. if(acpi_device_dir(device)) {
  806. remove_proc_entry(PROC_INFO,acpi_device_dir(device));
  807. if (hotk->methods->mt_wled)
  808. remove_proc_entry(PROC_WLED,acpi_device_dir(device));
  809. if (hotk->methods->mt_mled)
  810. remove_proc_entry(PROC_MLED,acpi_device_dir(device));
  811. if (hotk->methods->mt_tled)
  812. remove_proc_entry(PROC_TLED,acpi_device_dir(device));
  813. if (hotk->methods->mt_lcd_switch && hotk->methods->lcd_status)
  814. remove_proc_entry(PROC_LCD, acpi_device_dir(device));
  815. if ((hotk->methods->brightness_up && hotk->methods->brightness_down) ||
  816. (hotk->methods->brightness_get && hotk->methods->brightness_set))
  817. remove_proc_entry(PROC_BRN, acpi_device_dir(device));
  818. if (hotk->methods->display_set)
  819. remove_proc_entry(PROC_DISP, acpi_device_dir(device));
  820. }
  821. return 0;
  822. }
  823. static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
  824. {
  825. /* TODO Find a better way to handle events count.*/
  826. if (!hotk)
  827. return;
  828. if ((event & ~((u32) BR_UP)) < 16) {
  829. hotk->brightness = (event & ~((u32) BR_UP));
  830. } else if ((event & ~((u32) BR_DOWN)) < 16 ) {
  831. hotk->brightness = (event & ~((u32) BR_DOWN));
  832. }
  833. acpi_bus_generate_event(hotk->device, event,
  834. hotk->event_count[event % 128]++);
  835. return;
  836. }
  837. /*
  838. * This function is used to initialize the hotk with right values. In this
  839. * method, we can make all the detection we want, and modify the hotk struct
  840. */
  841. static int __init asus_hotk_get_info(void)
  842. {
  843. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  844. struct acpi_buffer dsdt = { ACPI_ALLOCATE_BUFFER, NULL };
  845. union acpi_object *model = NULL;
  846. int bsts_result;
  847. acpi_status status;
  848. /*
  849. * Get DSDT headers early enough to allow for differentiating between
  850. * models, but late enough to allow acpi_bus_register_driver() to fail
  851. * before doing anything ACPI-specific. Should we encounter a machine,
  852. * which needs special handling (i.e. its hotkey device has a different
  853. * HID), this bit will be moved. A global variable asus_info contains
  854. * the DSDT header.
  855. */
  856. status = acpi_get_table(ACPI_TABLE_DSDT, 1, &dsdt);
  857. if (ACPI_FAILURE(status))
  858. printk(KERN_WARNING " Couldn't get the DSDT table header\n");
  859. else
  860. asus_info = (struct acpi_table_header *) dsdt.pointer;
  861. /* We have to write 0 on init this far for all ASUS models */
  862. if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
  863. printk(KERN_ERR " Hotkey initialization failed\n");
  864. return -ENODEV;
  865. }
  866. /* This needs to be called for some laptops to init properly */
  867. if (!read_acpi_int(hotk->handle, "BSTS", &bsts_result))
  868. printk(KERN_WARNING " Error calling BSTS\n");
  869. else if (bsts_result)
  870. printk(KERN_NOTICE " BSTS called, 0x%02x returned\n", bsts_result);
  871. /* Samsung P30 has a device with a valid _HID whose INIT does not
  872. * return anything. Catch this one and any similar here */
  873. if (buffer.pointer == NULL) {
  874. if (asus_info && /* Samsung P30 */
  875. strncmp(asus_info->oem_table_id, "ODEM", 4) == 0) {
  876. hotk->model = P30;
  877. printk(KERN_NOTICE " Samsung P30 detected, supported\n");
  878. } else {
  879. hotk->model = M2E;
  880. printk(KERN_WARNING " no string returned by INIT\n");
  881. printk(KERN_WARNING " trying default values, supply "
  882. "the developers with your DSDT\n");
  883. }
  884. hotk->methods = &model_conf[hotk->model];
  885. return AE_OK;
  886. }
  887. model = (union acpi_object *) buffer.pointer;
  888. if (model->type == ACPI_TYPE_STRING) {
  889. printk(KERN_NOTICE " %s model detected, ", model->string.pointer);
  890. }
  891. hotk->model = END_MODEL;
  892. if (strncmp(model->string.pointer, "L3D", 3) == 0)
  893. hotk->model = L3D;
  894. else if (strncmp(model->string.pointer, "L3H", 3) == 0 ||
  895. strncmp(model->string.pointer, "L2E", 3) == 0)
  896. hotk->model = L3H;
  897. else if (strncmp(model->string.pointer, "L3", 2) == 0 ||
  898. strncmp(model->string.pointer, "L2B", 3) == 0)
  899. hotk->model = L3C;
  900. else if (strncmp(model->string.pointer, "L8L", 3) == 0)
  901. hotk->model = L8L;
  902. else if (strncmp(model->string.pointer, "L4R", 3) == 0)
  903. hotk->model = L4R;
  904. else if (strncmp(model->string.pointer, "M6N", 3) == 0)
  905. hotk->model = M6N;
  906. else if (strncmp(model->string.pointer, "M6R", 3) == 0)
  907. hotk->model = M6R;
  908. else if (strncmp(model->string.pointer, "M2N", 3) == 0 ||
  909. strncmp(model->string.pointer, "M3N", 3) == 0 ||
  910. strncmp(model->string.pointer, "M5N", 3) == 0 ||
  911. strncmp(model->string.pointer, "M6N", 3) == 0 ||
  912. strncmp(model->string.pointer, "S1N", 3) == 0 ||
  913. strncmp(model->string.pointer, "S5N", 3) == 0 ||
  914. strncmp(model->string.pointer, "W1N", 3) == 0)
  915. hotk->model = xxN;
  916. else if (strncmp(model->string.pointer, "M1", 2) == 0)
  917. hotk->model = M1A;
  918. else if (strncmp(model->string.pointer, "M2", 2) == 0 ||
  919. strncmp(model->string.pointer, "L4E", 3) == 0)
  920. hotk->model = M2E;
  921. else if (strncmp(model->string.pointer, "L2", 2) == 0)
  922. hotk->model = L2D;
  923. else if (strncmp(model->string.pointer, "L8", 2) == 0)
  924. hotk->model = S1x;
  925. else if (strncmp(model->string.pointer, "D1", 2) == 0)
  926. hotk->model = D1x;
  927. else if (strncmp(model->string.pointer, "A1", 2) == 0)
  928. hotk->model = A1x;
  929. else if (strncmp(model->string.pointer, "A2", 2) == 0)
  930. hotk->model = A2x;
  931. else if (strncmp(model->string.pointer, "J1", 2) == 0)
  932. hotk->model = S2x;
  933. else if (strncmp(model->string.pointer, "L5", 2) == 0)
  934. hotk->model = L5x;
  935. if (hotk->model == END_MODEL) {
  936. printk("unsupported, trying default values, supply the "
  937. "developers with your DSDT\n");
  938. hotk->model = M2E;
  939. } else {
  940. printk("supported\n");
  941. }
  942. hotk->methods = &model_conf[hotk->model];
  943. /* Sort of per-model blacklist */
  944. if (strncmp(model->string.pointer, "L2B", 3) == 0)
  945. hotk->methods->lcd_status = NULL;
  946. /* L2B is similar enough to L3C to use its settings, with this only
  947. exception */
  948. else if (strncmp(model->string.pointer, "S5N", 3) == 0 ||
  949. strncmp(model->string.pointer, "M5N", 3) == 0)
  950. hotk->methods->mt_mled = NULL;
  951. /* S5N and M5N have no MLED */
  952. else if (strncmp(model->string.pointer, "M2N", 3) == 0 ||
  953. strncmp(model->string.pointer, "W1N", 3) == 0)
  954. hotk->methods->mt_wled = "WLED";
  955. /* M2N and W1N have a usable WLED */
  956. else if (asus_info) {
  957. if (strncmp(asus_info->oem_table_id, "L1", 2) == 0)
  958. hotk->methods->mled_status = NULL;
  959. /* S1300A reports L84F, but L1400B too, account for that */
  960. }
  961. acpi_os_free(model);
  962. return AE_OK;
  963. }
  964. static int __init asus_hotk_check(void)
  965. {
  966. int result = 0;
  967. result = acpi_bus_get_status(hotk->device);
  968. if (result)
  969. return result;
  970. if (hotk->device->status.present) {
  971. result = asus_hotk_get_info();
  972. } else {
  973. printk(KERN_ERR " Hotkey device not present, aborting\n");
  974. return -EINVAL;
  975. }
  976. return result;
  977. }
  978. static int __init asus_hotk_add(struct acpi_device *device)
  979. {
  980. acpi_status status = AE_OK;
  981. int result;
  982. if (!device)
  983. return -EINVAL;
  984. printk(KERN_NOTICE "Asus Laptop ACPI Extras version %s\n",
  985. ASUS_ACPI_VERSION);
  986. hotk =
  987. (struct asus_hotk *) kmalloc(sizeof(struct asus_hotk), GFP_KERNEL);
  988. if (!hotk)
  989. return -ENOMEM;
  990. memset(hotk, 0, sizeof(struct asus_hotk));
  991. hotk->handle = device->handle;
  992. strcpy(acpi_device_name(device), ACPI_HOTK_DEVICE_NAME);
  993. strcpy(acpi_device_class(device), ACPI_HOTK_CLASS);
  994. acpi_driver_data(device) = hotk;
  995. hotk->device = device;
  996. result = asus_hotk_check();
  997. if (result)
  998. goto end;
  999. result = asus_hotk_add_fs(device);
  1000. if (result)
  1001. goto end;
  1002. /*
  1003. * We install the handler, it will receive the hotk in parameter, so, we
  1004. * could add other data to the hotk struct
  1005. */
  1006. status = acpi_install_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
  1007. asus_hotk_notify, hotk);
  1008. if (ACPI_FAILURE(status))
  1009. printk(KERN_ERR " Error installing notify handler\n");
  1010. /* For laptops without GPLV: init the hotk->brightness value */
  1011. if ((!hotk->methods->brightness_get) && (!hotk->methods->brightness_status) &&
  1012. (hotk->methods->brightness_up && hotk->methods->brightness_down)) {
  1013. status = acpi_evaluate_object(NULL, hotk->methods->brightness_down,
  1014. NULL, NULL);
  1015. if (ACPI_FAILURE(status))
  1016. printk(KERN_WARNING " Error changing brightness\n");
  1017. else {
  1018. status = acpi_evaluate_object(NULL, hotk->methods->brightness_up,
  1019. NULL, NULL);
  1020. if (ACPI_FAILURE(status))
  1021. printk(KERN_WARNING " Strange, error changing"
  1022. " brightness\n");
  1023. }
  1024. }
  1025. end:
  1026. if (result) {
  1027. kfree(hotk);
  1028. }
  1029. return result;
  1030. }
  1031. static int asus_hotk_remove(struct acpi_device *device, int type)
  1032. {
  1033. acpi_status status = 0;
  1034. if (!device || !acpi_driver_data(device))
  1035. return -EINVAL;
  1036. status = acpi_remove_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
  1037. asus_hotk_notify);
  1038. if (ACPI_FAILURE(status))
  1039. printk(KERN_ERR "Asus ACPI: Error removing notify handler\n");
  1040. asus_hotk_remove_fs(device);
  1041. kfree(hotk);
  1042. return 0;
  1043. }
  1044. static int __init asus_acpi_init(void)
  1045. {
  1046. int result;
  1047. if (acpi_disabled)
  1048. return -ENODEV;
  1049. asus_proc_dir = proc_mkdir(PROC_ASUS, acpi_root_dir);
  1050. if (!asus_proc_dir) {
  1051. printk(KERN_ERR "Asus ACPI: Unable to create /proc entry\n");
  1052. return -ENODEV;
  1053. }
  1054. asus_proc_dir->owner = THIS_MODULE;
  1055. result = acpi_bus_register_driver(&asus_hotk_driver);
  1056. if (result < 1) {
  1057. acpi_bus_unregister_driver(&asus_hotk_driver);
  1058. remove_proc_entry(PROC_ASUS, acpi_root_dir);
  1059. return -ENODEV;
  1060. }
  1061. return 0;
  1062. }
  1063. static void __exit asus_acpi_exit(void)
  1064. {
  1065. acpi_bus_unregister_driver(&asus_hotk_driver);
  1066. remove_proc_entry(PROC_ASUS, acpi_root_dir);
  1067. acpi_os_free(asus_info);
  1068. return;
  1069. }
  1070. module_init(asus_acpi_init);
  1071. module_exit(asus_acpi_exit);