asus-laptop.c 27 KB

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