asus-laptop.c 30 KB

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