asus-laptop.c 30 KB

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