asus-laptop.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. /*
  2. * asus-laptop.c - Asus Laptop Support
  3. *
  4. *
  5. * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
  6. * Copyright (C) 2006-2007 Corentin Chary
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. *
  23. * The development page for this driver is located at
  24. * http://sourceforge.net/projects/acpi4asus/
  25. *
  26. * Credits:
  27. * Pontus Fuchs - Helper functions, cleanup
  28. * Johann Wiesner - Small compile fixes
  29. * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
  30. * Eric Burghard - LED display support for W1N
  31. * Josh Green - Light Sens support
  32. * Thomas Tuttle - His first patch for led support was very helpfull
  33. * Sam Lin - GPS support
  34. */
  35. #include <linux/kernel.h>
  36. #include <linux/module.h>
  37. #include <linux/init.h>
  38. #include <linux/types.h>
  39. #include <linux/err.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/backlight.h>
  42. #include <linux/fb.h>
  43. #include <linux/leds.h>
  44. #include <linux/platform_device.h>
  45. #include <acpi/acpi_drivers.h>
  46. #include <acpi/acpi_bus.h>
  47. #include <asm/uaccess.h>
  48. #define ASUS_LAPTOP_VERSION "0.42"
  49. #define ASUS_HOTK_NAME "Asus Laptop Support"
  50. #define ASUS_HOTK_CLASS "hotkey"
  51. #define ASUS_HOTK_DEVICE_NAME "Hotkey"
  52. #define ASUS_HOTK_FILE "asus-laptop"
  53. #define ASUS_HOTK_PREFIX "\\_SB.ATKD."
  54. /*
  55. * Some events we use, same for all Asus
  56. */
  57. #define ATKD_BR_UP 0x10
  58. #define ATKD_BR_DOWN 0x20
  59. #define ATKD_LCD_ON 0x33
  60. #define ATKD_LCD_OFF 0x34
  61. /*
  62. * Known bits returned by \_SB.ATKD.HWRS
  63. */
  64. #define WL_HWRS 0x80
  65. #define BT_HWRS 0x100
  66. /*
  67. * Flags for hotk status
  68. * WL_ON and BT_ON are also used for wireless_status()
  69. */
  70. #define WL_ON 0x01 //internal Wifi
  71. #define BT_ON 0x02 //internal Bluetooth
  72. #define MLED_ON 0x04 //mail LED
  73. #define TLED_ON 0x08 //touchpad LED
  74. #define RLED_ON 0x10 //Record LED
  75. #define PLED_ON 0x20 //Phone LED
  76. #define GLED_ON 0x40 //Gaming LED
  77. #define LCD_ON 0x80 //LCD backlight
  78. #define GPS_ON 0x100 //GPS
  79. #define ASUS_LOG ASUS_HOTK_FILE ": "
  80. #define ASUS_ERR KERN_ERR ASUS_LOG
  81. #define ASUS_WARNING KERN_WARNING ASUS_LOG
  82. #define ASUS_NOTICE KERN_NOTICE ASUS_LOG
  83. #define ASUS_INFO KERN_INFO ASUS_LOG
  84. #define ASUS_DEBUG KERN_DEBUG ASUS_LOG
  85. MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
  86. MODULE_DESCRIPTION(ASUS_HOTK_NAME);
  87. MODULE_LICENSE("GPL");
  88. /* WAPF defines the behavior of the Fn+Fx wlan key
  89. * The significance of values is yet to be found, but
  90. * most of the time:
  91. * 0x0 will do nothing
  92. * 0x1 will allow to control the device with Fn+Fx key.
  93. * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
  94. * 0x5 like 0x1 or 0x4
  95. * So, if something doesn't work as you want, just try other values =)
  96. */
  97. static uint wapf = 1;
  98. module_param(wapf, uint, 0644);
  99. MODULE_PARM_DESC(wapf, "WAPF value");
  100. #define ASUS_HANDLE(object, paths...) \
  101. static acpi_handle object##_handle = NULL; \
  102. static char *object##_paths[] = { paths }
  103. /* LED */
  104. ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
  105. ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
  106. ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
  107. ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
  108. ASUS_HANDLE(gled_set, ASUS_HOTK_PREFIX "GLED"); /* G1, G2 (probably) */
  109. /* LEDD */
  110. ASUS_HANDLE(ledd_set, ASUS_HOTK_PREFIX "SLCM");
  111. /* Bluetooth and WLAN
  112. * WLED and BLED are not handled like other XLED, because in some dsdt
  113. * they also control the WLAN/Bluetooth device.
  114. */
  115. ASUS_HANDLE(wl_switch, ASUS_HOTK_PREFIX "WLED");
  116. ASUS_HANDLE(bt_switch, ASUS_HOTK_PREFIX "BLED");
  117. ASUS_HANDLE(wireless_status, ASUS_HOTK_PREFIX "RSTS"); /* All new models */
  118. /* Brightness */
  119. ASUS_HANDLE(brightness_set, ASUS_HOTK_PREFIX "SPLV");
  120. ASUS_HANDLE(brightness_get, ASUS_HOTK_PREFIX "GPLV");
  121. /* Backlight */
  122. ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
  123. "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
  124. "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
  125. "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
  126. "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
  127. "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
  128. "\\_SB.PCI0.PX40.Q10", /* S1x */
  129. "\\Q10"); /* A2x, L2D, L3D, M2E */
  130. /* Display */
  131. ASUS_HANDLE(display_set, ASUS_HOTK_PREFIX "SDSP");
  132. ASUS_HANDLE(display_get, "\\_SB.PCI0.P0P1.VGA.GETD", /* A6B, A6K A6R A7D F3JM L4R M6R A3G
  133. M6A M6V VX-1 V6J V6V W3Z */
  134. "\\_SB.PCI0.P0P2.VGA.GETD", /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V
  135. S5A M5A z33A W1Jc W2V G1 */
  136. "\\_SB.PCI0.P0P3.VGA.GETD", /* A6V A6Q */
  137. "\\_SB.PCI0.P0PA.VGA.GETD", /* A6T, A6M */
  138. "\\_SB.PCI0.PCI1.VGAC.NMAP", /* L3C */
  139. "\\_SB.PCI0.VGA.GETD", /* Z96F */
  140. "\\ACTD", /* A2D */
  141. "\\ADVG", /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
  142. "\\DNXT", /* P30 */
  143. "\\INFB", /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
  144. "\\SSTE"); /* A3F A6F A3N A3L M6N W3N W6A */
  145. ASUS_HANDLE(ls_switch, ASUS_HOTK_PREFIX "ALSC"); /* Z71A Z71V */
  146. ASUS_HANDLE(ls_level, ASUS_HOTK_PREFIX "ALSL"); /* Z71A Z71V */
  147. /* GPS */
  148. /* R2H use different handle for GPS on/off */
  149. ASUS_HANDLE(gps_on, ASUS_HOTK_PREFIX "SDON"); /* R2H */
  150. ASUS_HANDLE(gps_off, ASUS_HOTK_PREFIX "SDOF"); /* R2H */
  151. ASUS_HANDLE(gps_status, ASUS_HOTK_PREFIX "GPST");
  152. /*
  153. * This is the main structure, we can use it to store anything interesting
  154. * about the hotk device
  155. */
  156. struct asus_hotk {
  157. char *name; //laptop name
  158. struct acpi_device *device; //the device we are in
  159. acpi_handle handle; //the handle of the hotk device
  160. char status; //status of the hotk, for LEDs, ...
  161. u32 ledd_status; //status of the LED display
  162. u8 light_level; //light sensor level
  163. u8 light_switch; //light sensor switch value
  164. u16 event_count[128]; //count for each event TODO make this better
  165. };
  166. /*
  167. * This header is made available to allow proper configuration given model,
  168. * revision number , ... this info cannot go in struct asus_hotk because it is
  169. * available before the hotk
  170. */
  171. static struct acpi_table_header *asus_info;
  172. /* The actual device the driver binds to */
  173. static struct asus_hotk *hotk;
  174. /*
  175. * The hotkey driver declaration
  176. */
  177. static const struct acpi_device_id asus_device_ids[] = {
  178. {"ATK0100", 0},
  179. {"", 0},
  180. };
  181. MODULE_DEVICE_TABLE(acpi, asus_device_ids);
  182. static int asus_hotk_add(struct acpi_device *device);
  183. static int asus_hotk_remove(struct acpi_device *device, int type);
  184. static struct acpi_driver asus_hotk_driver = {
  185. .name = ASUS_HOTK_NAME,
  186. .class = ASUS_HOTK_CLASS,
  187. .ids = asus_device_ids,
  188. .ops = {
  189. .add = asus_hotk_add,
  190. .remove = asus_hotk_remove,
  191. },
  192. };
  193. /* The backlight device /sys/class/backlight */
  194. static struct backlight_device *asus_backlight_device;
  195. /*
  196. * The backlight class declaration
  197. */
  198. static int read_brightness(struct backlight_device *bd);
  199. static int update_bl_status(struct backlight_device *bd);
  200. static struct backlight_ops asusbl_ops = {
  201. .get_brightness = read_brightness,
  202. .update_status = update_bl_status,
  203. };
  204. /* These functions actually update the LED's, and are called from a
  205. * workqueue. By doing this as separate work rather than when the LED
  206. * subsystem asks, we avoid messing with the Asus ACPI stuff during a
  207. * potentially bad time, such as a timer interrupt. */
  208. static struct workqueue_struct *led_workqueue;
  209. #define ASUS_LED(object, ledname) \
  210. static void object##_led_set(struct led_classdev *led_cdev, \
  211. enum led_brightness value); \
  212. static void object##_led_update(struct work_struct *ignored); \
  213. static int object##_led_wk; \
  214. static DECLARE_WORK(object##_led_work, object##_led_update); \
  215. static struct led_classdev object##_led = { \
  216. .name = "asus::" ledname, \
  217. .brightness_set = object##_led_set, \
  218. }
  219. ASUS_LED(mled, "mail");
  220. ASUS_LED(tled, "touchpad");
  221. ASUS_LED(rled, "record");
  222. ASUS_LED(pled, "phone");
  223. ASUS_LED(gled, "gaming");
  224. /*
  225. * This function evaluates an ACPI method, given an int as parameter, the
  226. * method is searched within the scope of the handle, can be NULL. The output
  227. * of the method is written is output, which can also be NULL
  228. *
  229. * returns 0 if write is successful, -1 else.
  230. */
  231. static int write_acpi_int(acpi_handle handle, const char *method, int val,
  232. struct acpi_buffer *output)
  233. {
  234. struct acpi_object_list params; //list of input parameters (an int here)
  235. union acpi_object in_obj; //the only param we use
  236. acpi_status status;
  237. if (!handle)
  238. return 0;
  239. params.count = 1;
  240. params.pointer = &in_obj;
  241. in_obj.type = ACPI_TYPE_INTEGER;
  242. in_obj.integer.value = val;
  243. status = acpi_evaluate_object(handle, (char *)method, &params, output);
  244. if (status == AE_OK)
  245. return 0;
  246. else
  247. return -1;
  248. }
  249. static int read_wireless_status(int mask)
  250. {
  251. unsigned long long status;
  252. acpi_status rv = AE_OK;
  253. if (!wireless_status_handle)
  254. return (hotk->status & mask) ? 1 : 0;
  255. rv = acpi_evaluate_integer(wireless_status_handle, NULL, NULL, &status);
  256. if (ACPI_FAILURE(rv))
  257. printk(ASUS_WARNING "Error reading Wireless status\n");
  258. else
  259. return (status & mask) ? 1 : 0;
  260. return (hotk->status & mask) ? 1 : 0;
  261. }
  262. static int read_gps_status(void)
  263. {
  264. unsigned long long status;
  265. acpi_status rv = AE_OK;
  266. rv = acpi_evaluate_integer(gps_status_handle, NULL, NULL, &status);
  267. if (ACPI_FAILURE(rv))
  268. printk(ASUS_WARNING "Error reading GPS status\n");
  269. else
  270. return status ? 1 : 0;
  271. return (hotk->status & GPS_ON) ? 1 : 0;
  272. }
  273. /* Generic LED functions */
  274. static int read_status(int mask)
  275. {
  276. /* There is a special method for both wireless devices */
  277. if (mask == BT_ON || mask == WL_ON)
  278. return read_wireless_status(mask);
  279. else if (mask == GPS_ON)
  280. return read_gps_status();
  281. return (hotk->status & mask) ? 1 : 0;
  282. }
  283. static void write_status(acpi_handle handle, int out, int mask)
  284. {
  285. hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
  286. switch (mask) {
  287. case MLED_ON:
  288. out = !(out & 0x1);
  289. break;
  290. case GLED_ON:
  291. out = (out & 0x1) + 1;
  292. break;
  293. case GPS_ON:
  294. handle = (out) ? gps_on_handle : gps_off_handle;
  295. out = 0x02;
  296. break;
  297. default:
  298. out &= 0x1;
  299. break;
  300. }
  301. if (write_acpi_int(handle, NULL, out, NULL))
  302. printk(ASUS_WARNING " write failed %x\n", mask);
  303. }
  304. /* /sys/class/led handlers */
  305. #define ASUS_LED_HANDLER(object, mask) \
  306. static void object##_led_set(struct led_classdev *led_cdev, \
  307. enum led_brightness value) \
  308. { \
  309. object##_led_wk = (value > 0) ? 1 : 0; \
  310. queue_work(led_workqueue, &object##_led_work); \
  311. } \
  312. static void object##_led_update(struct work_struct *ignored) \
  313. { \
  314. int value = object##_led_wk; \
  315. write_status(object##_set_handle, value, (mask)); \
  316. }
  317. ASUS_LED_HANDLER(mled, MLED_ON);
  318. ASUS_LED_HANDLER(pled, PLED_ON);
  319. ASUS_LED_HANDLER(rled, RLED_ON);
  320. ASUS_LED_HANDLER(tled, TLED_ON);
  321. ASUS_LED_HANDLER(gled, GLED_ON);
  322. static int get_lcd_state(void)
  323. {
  324. return read_status(LCD_ON);
  325. }
  326. static int set_lcd_state(int value)
  327. {
  328. int lcd = 0;
  329. acpi_status status = 0;
  330. lcd = value ? 1 : 0;
  331. if (lcd == get_lcd_state())
  332. return 0;
  333. if (lcd_switch_handle) {
  334. status = acpi_evaluate_object(lcd_switch_handle,
  335. NULL, NULL, NULL);
  336. if (ACPI_FAILURE(status))
  337. printk(ASUS_WARNING "Error switching LCD\n");
  338. }
  339. write_status(NULL, lcd, LCD_ON);
  340. return 0;
  341. }
  342. static void lcd_blank(int blank)
  343. {
  344. struct backlight_device *bd = asus_backlight_device;
  345. if (bd) {
  346. bd->props.power = blank;
  347. backlight_update_status(bd);
  348. }
  349. }
  350. static int read_brightness(struct backlight_device *bd)
  351. {
  352. unsigned long long value;
  353. acpi_status rv = AE_OK;
  354. rv = acpi_evaluate_integer(brightness_get_handle, NULL, NULL, &value);
  355. if (ACPI_FAILURE(rv))
  356. printk(ASUS_WARNING "Error reading brightness\n");
  357. return value;
  358. }
  359. static int set_brightness(struct backlight_device *bd, int value)
  360. {
  361. int ret = 0;
  362. value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
  363. /* 0 <= value <= 15 */
  364. if (write_acpi_int(brightness_set_handle, NULL, value, NULL)) {
  365. printk(ASUS_WARNING "Error changing brightness\n");
  366. ret = -EIO;
  367. }
  368. return ret;
  369. }
  370. static int update_bl_status(struct backlight_device *bd)
  371. {
  372. int rv;
  373. int value = bd->props.brightness;
  374. rv = set_brightness(bd, value);
  375. if (rv)
  376. return rv;
  377. value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
  378. return set_lcd_state(value);
  379. }
  380. /*
  381. * Platform device handlers
  382. */
  383. /*
  384. * We write our info in page, we begin at offset off and cannot write more
  385. * than count bytes. We set eof to 1 if we handle those 2 values. We return the
  386. * number of bytes written in page
  387. */
  388. static ssize_t show_infos(struct device *dev,
  389. struct device_attribute *attr, char *page)
  390. {
  391. int len = 0;
  392. unsigned long long temp;
  393. char buf[16]; //enough for all info
  394. acpi_status rv = AE_OK;
  395. /*
  396. * We use the easy way, we don't care of off and count, so we don't set eof
  397. * to 1
  398. */
  399. len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
  400. len += sprintf(page + len, "Model reference : %s\n", hotk->name);
  401. /*
  402. * The SFUN method probably allows the original driver to get the list
  403. * of features supported by a given model. For now, 0x0100 or 0x0800
  404. * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
  405. * The significance of others is yet to be found.
  406. */
  407. rv = acpi_evaluate_integer(hotk->handle, "SFUN", NULL, &temp);
  408. if (!ACPI_FAILURE(rv))
  409. len += sprintf(page + len, "SFUN value : 0x%04x\n",
  410. (uint) temp);
  411. /*
  412. * Another value for userspace: the ASYM method returns 0x02 for
  413. * battery low and 0x04 for battery critical, its readings tend to be
  414. * more accurate than those provided by _BST.
  415. * Note: since not all the laptops provide this method, errors are
  416. * silently ignored.
  417. */
  418. rv = acpi_evaluate_integer(hotk->handle, "ASYM", NULL, &temp);
  419. if (!ACPI_FAILURE(rv))
  420. len += sprintf(page + len, "ASYM value : 0x%04x\n",
  421. (uint) temp);
  422. if (asus_info) {
  423. snprintf(buf, 16, "%d", asus_info->length);
  424. len += sprintf(page + len, "DSDT length : %s\n", buf);
  425. snprintf(buf, 16, "%d", asus_info->checksum);
  426. len += sprintf(page + len, "DSDT checksum : %s\n", buf);
  427. snprintf(buf, 16, "%d", asus_info->revision);
  428. len += sprintf(page + len, "DSDT revision : %s\n", buf);
  429. snprintf(buf, 7, "%s", asus_info->oem_id);
  430. len += sprintf(page + len, "OEM id : %s\n", buf);
  431. snprintf(buf, 9, "%s", asus_info->oem_table_id);
  432. len += sprintf(page + len, "OEM table id : %s\n", buf);
  433. snprintf(buf, 16, "%x", asus_info->oem_revision);
  434. len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
  435. snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
  436. len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
  437. snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
  438. len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
  439. }
  440. return len;
  441. }
  442. static int parse_arg(const char *buf, unsigned long count, int *val)
  443. {
  444. if (!count)
  445. return 0;
  446. if (count > 31)
  447. return -EINVAL;
  448. if (sscanf(buf, "%i", val) != 1)
  449. return -EINVAL;
  450. return count;
  451. }
  452. static ssize_t store_status(const char *buf, size_t count,
  453. acpi_handle handle, int mask)
  454. {
  455. int rv, value;
  456. int out = 0;
  457. rv = parse_arg(buf, count, &value);
  458. if (rv > 0)
  459. out = value ? 1 : 0;
  460. write_status(handle, out, mask);
  461. return rv;
  462. }
  463. /*
  464. * LEDD display
  465. */
  466. static ssize_t show_ledd(struct device *dev,
  467. struct device_attribute *attr, char *buf)
  468. {
  469. return sprintf(buf, "0x%08x\n", hotk->ledd_status);
  470. }
  471. static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
  472. const char *buf, size_t count)
  473. {
  474. int rv, value;
  475. rv = parse_arg(buf, count, &value);
  476. if (rv > 0) {
  477. if (write_acpi_int(ledd_set_handle, NULL, value, NULL))
  478. printk(ASUS_WARNING "LED display write failed\n");
  479. else
  480. hotk->ledd_status = (u32) value;
  481. }
  482. return rv;
  483. }
  484. /*
  485. * WLAN
  486. */
  487. static ssize_t show_wlan(struct device *dev,
  488. struct device_attribute *attr, char *buf)
  489. {
  490. return sprintf(buf, "%d\n", read_status(WL_ON));
  491. }
  492. static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
  493. const char *buf, size_t count)
  494. {
  495. return store_status(buf, count, wl_switch_handle, WL_ON);
  496. }
  497. /*
  498. * Bluetooth
  499. */
  500. static ssize_t show_bluetooth(struct device *dev,
  501. struct device_attribute *attr, char *buf)
  502. {
  503. return sprintf(buf, "%d\n", read_status(BT_ON));
  504. }
  505. static ssize_t store_bluetooth(struct device *dev,
  506. struct device_attribute *attr, const char *buf,
  507. size_t count)
  508. {
  509. return store_status(buf, count, bt_switch_handle, BT_ON);
  510. }
  511. /*
  512. * Display
  513. */
  514. static void set_display(int value)
  515. {
  516. /* no sanity check needed for now */
  517. if (write_acpi_int(display_set_handle, NULL, value, NULL))
  518. printk(ASUS_WARNING "Error setting display\n");
  519. return;
  520. }
  521. static int read_display(void)
  522. {
  523. unsigned long long value = 0;
  524. acpi_status rv = AE_OK;
  525. /* In most of the case, we know how to set the display, but sometime
  526. we can't read it */
  527. if (display_get_handle) {
  528. rv = acpi_evaluate_integer(display_get_handle, NULL,
  529. NULL, &value);
  530. if (ACPI_FAILURE(rv))
  531. printk(ASUS_WARNING "Error reading display status\n");
  532. }
  533. value &= 0x0F; /* needed for some models, shouldn't hurt others */
  534. return value;
  535. }
  536. /*
  537. * Now, *this* one could be more user-friendly, but so far, no-one has
  538. * complained. The significance of bits is the same as in store_disp()
  539. */
  540. static ssize_t show_disp(struct device *dev,
  541. struct device_attribute *attr, char *buf)
  542. {
  543. return sprintf(buf, "%d\n", read_display());
  544. }
  545. /*
  546. * Experimental support for display switching. As of now: 1 should activate
  547. * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
  548. * Any combination (bitwise) of these will suffice. I never actually tested 4
  549. * displays hooked up simultaneously, so be warned. See the acpi4asus README
  550. * for more info.
  551. */
  552. static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
  553. const char *buf, size_t count)
  554. {
  555. int rv, value;
  556. rv = parse_arg(buf, count, &value);
  557. if (rv > 0)
  558. set_display(value);
  559. return rv;
  560. }
  561. /*
  562. * Light Sens
  563. */
  564. static void set_light_sens_switch(int value)
  565. {
  566. if (write_acpi_int(ls_switch_handle, NULL, value, NULL))
  567. printk(ASUS_WARNING "Error setting light sensor switch\n");
  568. hotk->light_switch = value;
  569. }
  570. static ssize_t show_lssw(struct device *dev,
  571. struct device_attribute *attr, char *buf)
  572. {
  573. return sprintf(buf, "%d\n", hotk->light_switch);
  574. }
  575. static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
  576. const char *buf, size_t count)
  577. {
  578. int rv, value;
  579. rv = parse_arg(buf, count, &value);
  580. if (rv > 0)
  581. set_light_sens_switch(value ? 1 : 0);
  582. return rv;
  583. }
  584. static void set_light_sens_level(int value)
  585. {
  586. if (write_acpi_int(ls_level_handle, NULL, value, NULL))
  587. printk(ASUS_WARNING "Error setting light sensor level\n");
  588. hotk->light_level = value;
  589. }
  590. static ssize_t show_lslvl(struct device *dev,
  591. struct device_attribute *attr, char *buf)
  592. {
  593. return sprintf(buf, "%d\n", hotk->light_level);
  594. }
  595. static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
  596. const char *buf, size_t count)
  597. {
  598. int rv, value;
  599. rv = parse_arg(buf, count, &value);
  600. if (rv > 0) {
  601. value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
  602. /* 0 <= value <= 15 */
  603. set_light_sens_level(value);
  604. }
  605. return rv;
  606. }
  607. /*
  608. * GPS
  609. */
  610. static ssize_t show_gps(struct device *dev,
  611. struct device_attribute *attr, char *buf)
  612. {
  613. return sprintf(buf, "%d\n", read_status(GPS_ON));
  614. }
  615. static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
  616. const char *buf, size_t count)
  617. {
  618. return store_status(buf, count, NULL, GPS_ON);
  619. }
  620. static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
  621. {
  622. /* TODO Find a better way to handle events count. */
  623. if (!hotk)
  624. return;
  625. /*
  626. * We need to tell the backlight device when the backlight power is
  627. * switched
  628. */
  629. if (event == ATKD_LCD_ON) {
  630. write_status(NULL, 1, LCD_ON);
  631. lcd_blank(FB_BLANK_UNBLANK);
  632. } else if (event == ATKD_LCD_OFF) {
  633. write_status(NULL, 0, LCD_ON);
  634. lcd_blank(FB_BLANK_POWERDOWN);
  635. }
  636. acpi_bus_generate_proc_event(hotk->device, event,
  637. hotk->event_count[event % 128]++);
  638. return;
  639. }
  640. #define ASUS_CREATE_DEVICE_ATTR(_name) \
  641. struct device_attribute dev_attr_##_name = { \
  642. .attr = { \
  643. .name = __stringify(_name), \
  644. .mode = 0 }, \
  645. .show = NULL, \
  646. .store = NULL, \
  647. }
  648. #define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
  649. do { \
  650. dev_attr_##_name.attr.mode = _mode; \
  651. dev_attr_##_name.show = _show; \
  652. dev_attr_##_name.store = _store; \
  653. } while(0)
  654. static ASUS_CREATE_DEVICE_ATTR(infos);
  655. static ASUS_CREATE_DEVICE_ATTR(wlan);
  656. static ASUS_CREATE_DEVICE_ATTR(bluetooth);
  657. static ASUS_CREATE_DEVICE_ATTR(display);
  658. static ASUS_CREATE_DEVICE_ATTR(ledd);
  659. static ASUS_CREATE_DEVICE_ATTR(ls_switch);
  660. static ASUS_CREATE_DEVICE_ATTR(ls_level);
  661. static ASUS_CREATE_DEVICE_ATTR(gps);
  662. static struct attribute *asuspf_attributes[] = {
  663. &dev_attr_infos.attr,
  664. &dev_attr_wlan.attr,
  665. &dev_attr_bluetooth.attr,
  666. &dev_attr_display.attr,
  667. &dev_attr_ledd.attr,
  668. &dev_attr_ls_switch.attr,
  669. &dev_attr_ls_level.attr,
  670. &dev_attr_gps.attr,
  671. NULL
  672. };
  673. static struct attribute_group asuspf_attribute_group = {
  674. .attrs = asuspf_attributes
  675. };
  676. static struct platform_driver asuspf_driver = {
  677. .driver = {
  678. .name = ASUS_HOTK_FILE,
  679. .owner = THIS_MODULE,
  680. }
  681. };
  682. static struct platform_device *asuspf_device;
  683. static void asus_hotk_add_fs(void)
  684. {
  685. ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
  686. if (wl_switch_handle)
  687. ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
  688. if (bt_switch_handle)
  689. ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
  690. show_bluetooth, store_bluetooth);
  691. if (display_set_handle && display_get_handle)
  692. ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
  693. else if (display_set_handle)
  694. ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
  695. if (ledd_set_handle)
  696. ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
  697. if (ls_switch_handle && ls_level_handle) {
  698. ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
  699. ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
  700. }
  701. if (gps_status_handle && gps_on_handle && gps_off_handle)
  702. ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
  703. }
  704. static int asus_handle_init(char *name, acpi_handle * handle,
  705. char **paths, int num_paths)
  706. {
  707. int i;
  708. acpi_status status;
  709. for (i = 0; i < num_paths; i++) {
  710. status = acpi_get_handle(NULL, paths[i], handle);
  711. if (ACPI_SUCCESS(status))
  712. return 0;
  713. }
  714. *handle = NULL;
  715. return -ENODEV;
  716. }
  717. #define ASUS_HANDLE_INIT(object) \
  718. asus_handle_init(#object, &object##_handle, object##_paths, \
  719. ARRAY_SIZE(object##_paths))
  720. /*
  721. * This function is used to initialize the hotk with right values. In this
  722. * method, we can make all the detection we want, and modify the hotk struct
  723. */
  724. static int asus_hotk_get_info(void)
  725. {
  726. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  727. union acpi_object *model = NULL;
  728. unsigned long long bsts_result, hwrs_result;
  729. char *string = NULL;
  730. acpi_status status;
  731. /*
  732. * Get DSDT headers early enough to allow for differentiating between
  733. * models, but late enough to allow acpi_bus_register_driver() to fail
  734. * before doing anything ACPI-specific. Should we encounter a machine,
  735. * which needs special handling (i.e. its hotkey device has a different
  736. * HID), this bit will be moved. A global variable asus_info contains
  737. * the DSDT header.
  738. */
  739. status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
  740. if (ACPI_FAILURE(status))
  741. printk(ASUS_WARNING "Couldn't get the DSDT table header\n");
  742. /* We have to write 0 on init this far for all ASUS models */
  743. if (write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
  744. printk(ASUS_ERR "Hotkey initialization failed\n");
  745. return -ENODEV;
  746. }
  747. /* This needs to be called for some laptops to init properly */
  748. status =
  749. acpi_evaluate_integer(hotk->handle, "BSTS", NULL, &bsts_result);
  750. if (ACPI_FAILURE(status))
  751. printk(ASUS_WARNING "Error calling BSTS\n");
  752. else if (bsts_result)
  753. printk(ASUS_NOTICE "BSTS called, 0x%02x returned\n",
  754. (uint) bsts_result);
  755. /* This too ... */
  756. write_acpi_int(hotk->handle, "CWAP", wapf, NULL);
  757. /*
  758. * Try to match the object returned by INIT to the specific model.
  759. * Handle every possible object (or the lack of thereof) the DSDT
  760. * writers might throw at us. When in trouble, we pass NULL to
  761. * asus_model_match() and try something completely different.
  762. */
  763. if (buffer.pointer) {
  764. model = buffer.pointer;
  765. switch (model->type) {
  766. case ACPI_TYPE_STRING:
  767. string = model->string.pointer;
  768. break;
  769. case ACPI_TYPE_BUFFER:
  770. string = model->buffer.pointer;
  771. break;
  772. default:
  773. string = "";
  774. break;
  775. }
  776. }
  777. hotk->name = kstrdup(string, GFP_KERNEL);
  778. if (!hotk->name)
  779. return -ENOMEM;
  780. if (*string)
  781. printk(ASUS_NOTICE " %s model detected\n", string);
  782. ASUS_HANDLE_INIT(mled_set);
  783. ASUS_HANDLE_INIT(tled_set);
  784. ASUS_HANDLE_INIT(rled_set);
  785. ASUS_HANDLE_INIT(pled_set);
  786. ASUS_HANDLE_INIT(gled_set);
  787. ASUS_HANDLE_INIT(ledd_set);
  788. /*
  789. * The HWRS method return informations about the hardware.
  790. * 0x80 bit is for WLAN, 0x100 for Bluetooth.
  791. * The significance of others is yet to be found.
  792. * If we don't find the method, we assume the device are present.
  793. */
  794. status =
  795. acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &hwrs_result);
  796. if (ACPI_FAILURE(status))
  797. hwrs_result = WL_HWRS | BT_HWRS;
  798. if (hwrs_result & WL_HWRS)
  799. ASUS_HANDLE_INIT(wl_switch);
  800. if (hwrs_result & BT_HWRS)
  801. ASUS_HANDLE_INIT(bt_switch);
  802. ASUS_HANDLE_INIT(wireless_status);
  803. ASUS_HANDLE_INIT(brightness_set);
  804. ASUS_HANDLE_INIT(brightness_get);
  805. ASUS_HANDLE_INIT(lcd_switch);
  806. ASUS_HANDLE_INIT(display_set);
  807. ASUS_HANDLE_INIT(display_get);
  808. /* There is a lot of models with "ALSL", but a few get
  809. a real light sens, so we need to check it. */
  810. if (!ASUS_HANDLE_INIT(ls_switch))
  811. ASUS_HANDLE_INIT(ls_level);
  812. ASUS_HANDLE_INIT(gps_on);
  813. ASUS_HANDLE_INIT(gps_off);
  814. ASUS_HANDLE_INIT(gps_status);
  815. kfree(model);
  816. return AE_OK;
  817. }
  818. static int asus_hotk_check(void)
  819. {
  820. int result = 0;
  821. result = acpi_bus_get_status(hotk->device);
  822. if (result)
  823. return result;
  824. if (hotk->device->status.present) {
  825. result = asus_hotk_get_info();
  826. } else {
  827. printk(ASUS_ERR "Hotkey device not present, aborting\n");
  828. return -EINVAL;
  829. }
  830. return result;
  831. }
  832. static int asus_hotk_found;
  833. static int asus_hotk_add(struct acpi_device *device)
  834. {
  835. acpi_status status = AE_OK;
  836. int result;
  837. if (!device)
  838. return -EINVAL;
  839. printk(ASUS_NOTICE "Asus Laptop Support version %s\n",
  840. ASUS_LAPTOP_VERSION);
  841. hotk = kzalloc(sizeof(struct asus_hotk), GFP_KERNEL);
  842. if (!hotk)
  843. return -ENOMEM;
  844. hotk->handle = device->handle;
  845. strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
  846. strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
  847. device->driver_data = hotk;
  848. hotk->device = device;
  849. result = asus_hotk_check();
  850. if (result)
  851. goto end;
  852. asus_hotk_add_fs();
  853. /*
  854. * We install the handler, it will receive the hotk in parameter, so, we
  855. * could add other data to the hotk struct
  856. */
  857. status = acpi_install_notify_handler(hotk->handle, ACPI_ALL_NOTIFY,
  858. asus_hotk_notify, hotk);
  859. if (ACPI_FAILURE(status))
  860. printk(ASUS_ERR "Error installing notify handler\n");
  861. asus_hotk_found = 1;
  862. /* WLED and BLED are on by default */
  863. write_status(bt_switch_handle, 1, BT_ON);
  864. write_status(wl_switch_handle, 1, WL_ON);
  865. /* If the h/w switch is off, we need to check the real status */
  866. write_status(NULL, read_status(BT_ON), BT_ON);
  867. write_status(NULL, read_status(WL_ON), WL_ON);
  868. /* LCD Backlight is on by default */
  869. write_status(NULL, 1, LCD_ON);
  870. /* LED display is off by default */
  871. hotk->ledd_status = 0xFFF;
  872. /* Set initial values of light sensor and level */
  873. hotk->light_switch = 1; /* Default to light sensor disabled */
  874. hotk->light_level = 0; /* level 5 for sensor sensitivity */
  875. if (ls_switch_handle)
  876. set_light_sens_switch(hotk->light_switch);
  877. if (ls_level_handle)
  878. set_light_sens_level(hotk->light_level);
  879. /* GPS is on by default */
  880. write_status(NULL, 1, GPS_ON);
  881. end:
  882. if (result) {
  883. kfree(hotk->name);
  884. kfree(hotk);
  885. }
  886. return result;
  887. }
  888. static int asus_hotk_remove(struct acpi_device *device, int type)
  889. {
  890. acpi_status status = 0;
  891. if (!device || !acpi_driver_data(device))
  892. return -EINVAL;
  893. status = acpi_remove_notify_handler(hotk->handle, ACPI_ALL_NOTIFY,
  894. asus_hotk_notify);
  895. if (ACPI_FAILURE(status))
  896. printk(ASUS_ERR "Error removing notify handler\n");
  897. kfree(hotk->name);
  898. kfree(hotk);
  899. return 0;
  900. }
  901. static void asus_backlight_exit(void)
  902. {
  903. if (asus_backlight_device)
  904. backlight_device_unregister(asus_backlight_device);
  905. }
  906. #define ASUS_LED_UNREGISTER(object) \
  907. if (object##_led.dev) \
  908. led_classdev_unregister(&object##_led)
  909. static void asus_led_exit(void)
  910. {
  911. destroy_workqueue(led_workqueue);
  912. ASUS_LED_UNREGISTER(mled);
  913. ASUS_LED_UNREGISTER(tled);
  914. ASUS_LED_UNREGISTER(pled);
  915. ASUS_LED_UNREGISTER(rled);
  916. ASUS_LED_UNREGISTER(gled);
  917. }
  918. static void __exit asus_laptop_exit(void)
  919. {
  920. asus_backlight_exit();
  921. asus_led_exit();
  922. acpi_bus_unregister_driver(&asus_hotk_driver);
  923. sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
  924. platform_device_unregister(asuspf_device);
  925. platform_driver_unregister(&asuspf_driver);
  926. }
  927. static int asus_backlight_init(struct device *dev)
  928. {
  929. struct backlight_device *bd;
  930. if (brightness_set_handle && lcd_switch_handle) {
  931. bd = backlight_device_register(ASUS_HOTK_FILE, dev,
  932. NULL, &asusbl_ops);
  933. if (IS_ERR(bd)) {
  934. printk(ASUS_ERR
  935. "Could not register asus backlight device\n");
  936. asus_backlight_device = NULL;
  937. return PTR_ERR(bd);
  938. }
  939. asus_backlight_device = bd;
  940. bd->props.max_brightness = 15;
  941. bd->props.brightness = read_brightness(NULL);
  942. bd->props.power = FB_BLANK_UNBLANK;
  943. backlight_update_status(bd);
  944. }
  945. return 0;
  946. }
  947. static int asus_led_register(acpi_handle handle,
  948. struct led_classdev *ldev, struct device *dev)
  949. {
  950. if (!handle)
  951. return 0;
  952. return led_classdev_register(dev, ldev);
  953. }
  954. #define ASUS_LED_REGISTER(object, device) \
  955. asus_led_register(object##_set_handle, &object##_led, device)
  956. static int asus_led_init(struct device *dev)
  957. {
  958. int rv;
  959. rv = ASUS_LED_REGISTER(mled, dev);
  960. if (rv)
  961. goto out;
  962. rv = ASUS_LED_REGISTER(tled, dev);
  963. if (rv)
  964. goto out1;
  965. rv = ASUS_LED_REGISTER(rled, dev);
  966. if (rv)
  967. goto out2;
  968. rv = ASUS_LED_REGISTER(pled, dev);
  969. if (rv)
  970. goto out3;
  971. rv = ASUS_LED_REGISTER(gled, dev);
  972. if (rv)
  973. goto out4;
  974. led_workqueue = create_singlethread_workqueue("led_workqueue");
  975. if (!led_workqueue)
  976. goto out5;
  977. return 0;
  978. out5:
  979. rv = -ENOMEM;
  980. ASUS_LED_UNREGISTER(gled);
  981. out4:
  982. ASUS_LED_UNREGISTER(pled);
  983. out3:
  984. ASUS_LED_UNREGISTER(rled);
  985. out2:
  986. ASUS_LED_UNREGISTER(tled);
  987. out1:
  988. ASUS_LED_UNREGISTER(mled);
  989. out:
  990. return rv;
  991. }
  992. static int __init asus_laptop_init(void)
  993. {
  994. struct device *dev;
  995. int result;
  996. if (acpi_disabled)
  997. return -ENODEV;
  998. result = acpi_bus_register_driver(&asus_hotk_driver);
  999. if (result < 0)
  1000. return result;
  1001. /*
  1002. * This is a bit of a kludge. We only want this module loaded
  1003. * for ASUS systems, but there's currently no way to probe the
  1004. * ACPI namespace for ASUS HIDs. So we just return failure if
  1005. * we didn't find one, which will cause the module to be
  1006. * unloaded.
  1007. */
  1008. if (!asus_hotk_found) {
  1009. acpi_bus_unregister_driver(&asus_hotk_driver);
  1010. return -ENODEV;
  1011. }
  1012. dev = acpi_get_physical_device(hotk->device->handle);
  1013. if (!acpi_video_backlight_support()) {
  1014. result = asus_backlight_init(dev);
  1015. if (result)
  1016. goto fail_backlight;
  1017. } else
  1018. printk(ASUS_INFO "Brightness ignored, must be controlled by "
  1019. "ACPI video driver\n");
  1020. result = asus_led_init(dev);
  1021. if (result)
  1022. goto fail_led;
  1023. /* Register platform stuff */
  1024. result = platform_driver_register(&asuspf_driver);
  1025. if (result)
  1026. goto fail_platform_driver;
  1027. asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
  1028. if (!asuspf_device) {
  1029. result = -ENOMEM;
  1030. goto fail_platform_device1;
  1031. }
  1032. result = platform_device_add(asuspf_device);
  1033. if (result)
  1034. goto fail_platform_device2;
  1035. result = sysfs_create_group(&asuspf_device->dev.kobj,
  1036. &asuspf_attribute_group);
  1037. if (result)
  1038. goto fail_sysfs;
  1039. return 0;
  1040. fail_sysfs:
  1041. platform_device_del(asuspf_device);
  1042. fail_platform_device2:
  1043. platform_device_put(asuspf_device);
  1044. fail_platform_device1:
  1045. platform_driver_unregister(&asuspf_driver);
  1046. fail_platform_driver:
  1047. asus_led_exit();
  1048. fail_led:
  1049. asus_backlight_exit();
  1050. fail_backlight:
  1051. return result;
  1052. }
  1053. module_init(asus_laptop_init);
  1054. module_exit(asus_laptop_exit);