asus-laptop.c 30 KB

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