asus-laptop.c 32 KB

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