asus-laptop.c 31 KB

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