2 * asus-laptop.c - Asus Laptop Support
5 * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
6 * Copyright (C) 2006-2007 Corentin Chary
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.
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.
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
23 * The development page for this driver is located at
24 * http://sourceforge.net/projects/acpi4asus/
27 * Pontus Fuchs - Helper functions, cleanup
28 * Johann Wiesner - Small compile fixes
29 * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
30 * Eric Burghard - LED display support for W1N
31 * Josh Green - Light Sens support
32 * Thomas Tuttle - His first patch for led support was very helpfull
33 * Sam Lin - GPS support
36 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/types.h>
42 #include <linux/err.h>
43 #include <linux/proc_fs.h>
44 #include <linux/backlight.h>
46 #include <linux/leds.h>
47 #include <linux/platform_device.h>
48 #include <acpi/acpi_drivers.h>
49 #include <acpi/acpi_bus.h>
50 #include <asm/uaccess.h>
51 #include <linux/input.h>
53 #define ASUS_LAPTOP_VERSION "0.42"
55 #define ASUS_HOTK_NAME "Asus Laptop Support"
56 #define ASUS_HOTK_CLASS "hotkey"
57 #define ASUS_HOTK_DEVICE_NAME "Hotkey"
58 #define ASUS_HOTK_FILE KBUILD_MODNAME
59 #define ASUS_HOTK_PREFIX "\\_SB.ATKD."
63 * Some events we use, same for all Asus
65 #define ATKD_BR_UP 0x10
66 #define ATKD_BR_DOWN 0x20
67 #define ATKD_LCD_ON 0x33
68 #define ATKD_LCD_OFF 0x34
71 * Known bits returned by \_SB.ATKD.HWRS
77 * Flags for hotk status
78 * WL_ON and BT_ON are also used for wireless_status()
80 #define WL_ON 0x01 //internal Wifi
81 #define BT_ON 0x02 //internal Bluetooth
82 #define MLED_ON 0x04 //mail LED
83 #define TLED_ON 0x08 //touchpad LED
84 #define RLED_ON 0x10 //Record LED
85 #define PLED_ON 0x20 //Phone LED
86 #define GLED_ON 0x40 //Gaming LED
87 #define LCD_ON 0x80 //LCD backlight
88 #define GPS_ON 0x100 //GPS
90 #define ASUS_LOG ASUS_HOTK_FILE ": "
91 #define ASUS_ERR KERN_ERR ASUS_LOG
92 #define ASUS_WARNING KERN_WARNING ASUS_LOG
93 #define ASUS_NOTICE KERN_NOTICE ASUS_LOG
94 #define ASUS_INFO KERN_INFO ASUS_LOG
95 #define ASUS_DEBUG KERN_DEBUG ASUS_LOG
97 MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
98 MODULE_DESCRIPTION(ASUS_HOTK_NAME);
99 MODULE_LICENSE("GPL");
101 /* WAPF defines the behavior of the Fn+Fx wlan key
102 * The significance of values is yet to be found, but
104 * 0x0 will do nothing
105 * 0x1 will allow to control the device with Fn+Fx key.
106 * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
107 * 0x5 like 0x1 or 0x4
108 * So, if something doesn't work as you want, just try other values =)
110 static uint wapf = 1;
111 module_param(wapf, uint, 0644);
112 MODULE_PARM_DESC(wapf, "WAPF value");
114 #define ASUS_HANDLE(object, paths...) \
115 static acpi_handle object##_handle = NULL; \
116 static char *object##_paths[] = { paths }
119 ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
120 ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
121 ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
122 ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
123 ASUS_HANDLE(gled_set, ASUS_HOTK_PREFIX "GLED"); /* G1, G2 (probably) */
126 ASUS_HANDLE(ledd_set, ASUS_HOTK_PREFIX "SLCM");
128 /* Bluetooth and WLAN
129 * WLED and BLED are not handled like other XLED, because in some dsdt
130 * they also control the WLAN/Bluetooth device.
132 ASUS_HANDLE(wl_switch, ASUS_HOTK_PREFIX "WLED");
133 ASUS_HANDLE(bt_switch, ASUS_HOTK_PREFIX "BLED");
134 ASUS_HANDLE(wireless_status, ASUS_HOTK_PREFIX "RSTS"); /* All new models */
137 ASUS_HANDLE(brightness_set, ASUS_HOTK_PREFIX "SPLV");
138 ASUS_HANDLE(brightness_get, ASUS_HOTK_PREFIX "GPLV");
141 ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
142 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
143 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
144 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
145 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
146 "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
147 "\\_SB.PCI0.PX40.Q10", /* S1x */
148 "\\Q10"); /* A2x, L2D, L3D, M2E */
151 ASUS_HANDLE(display_set, ASUS_HOTK_PREFIX "SDSP");
152 ASUS_HANDLE(display_get, "\\_SB.PCI0.P0P1.VGA.GETD", /* A6B, A6K A6R A7D F3JM L4R M6R A3G
153 M6A M6V VX-1 V6J V6V W3Z */
154 "\\_SB.PCI0.P0P2.VGA.GETD", /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V
155 S5A M5A z33A W1Jc W2V G1 */
156 "\\_SB.PCI0.P0P3.VGA.GETD", /* A6V A6Q */
157 "\\_SB.PCI0.P0PA.VGA.GETD", /* A6T, A6M */
158 "\\_SB.PCI0.PCI1.VGAC.NMAP", /* L3C */
159 "\\_SB.PCI0.VGA.GETD", /* Z96F */
161 "\\ADVG", /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
163 "\\INFB", /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
164 "\\SSTE"); /* A3F A6F A3N A3L M6N W3N W6A */
166 ASUS_HANDLE(ls_switch, ASUS_HOTK_PREFIX "ALSC"); /* Z71A Z71V */
167 ASUS_HANDLE(ls_level, ASUS_HOTK_PREFIX "ALSL"); /* Z71A Z71V */
170 /* R2H use different handle for GPS on/off */
171 ASUS_HANDLE(gps_on, ASUS_HOTK_PREFIX "SDON"); /* R2H */
172 ASUS_HANDLE(gps_off, ASUS_HOTK_PREFIX "SDOF"); /* R2H */
173 ASUS_HANDLE(gps_status, ASUS_HOTK_PREFIX "GPST");
176 * This is the main structure, we can use it to store anything interesting
177 * about the hotk device
180 char *name; //laptop name
181 struct acpi_device *device; //the device we are in
182 acpi_handle handle; //the handle of the hotk device
183 char status; //status of the hotk, for LEDs, ...
184 u32 ledd_status; //status of the LED display
185 u8 light_level; //light sensor level
186 u8 light_switch; //light sensor switch value
187 u16 event_count[128]; //count for each event TODO make this better
188 struct input_dev *inputdev;
193 * This header is made available to allow proper configuration given model,
194 * revision number , ... this info cannot go in struct asus_hotk because it is
195 * available before the hotk
197 static struct acpi_table_header *asus_info;
199 /* The actual device the driver binds to */
200 static struct asus_hotk *hotk;
203 * The hotkey driver declaration
205 static const struct acpi_device_id asus_device_ids[] = {
209 MODULE_DEVICE_TABLE(acpi, asus_device_ids);
211 static int asus_hotk_add(struct acpi_device *device);
212 static int asus_hotk_remove(struct acpi_device *device, int type);
213 static void asus_hotk_notify(struct acpi_device *device, u32 event);
215 static struct acpi_driver asus_hotk_driver = {
216 .name = ASUS_HOTK_NAME,
217 .class = ASUS_HOTK_CLASS,
218 .ids = asus_device_ids,
219 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
221 .add = asus_hotk_add,
222 .remove = asus_hotk_remove,
223 .notify = asus_hotk_notify,
227 /* The backlight device /sys/class/backlight */
228 static struct backlight_device *asus_backlight_device;
231 * The backlight class declaration
233 static int read_brightness(struct backlight_device *bd);
234 static int update_bl_status(struct backlight_device *bd);
235 static struct backlight_ops asusbl_ops = {
236 .get_brightness = read_brightness,
237 .update_status = update_bl_status,
240 /* These functions actually update the LED's, and are called from a
241 * workqueue. By doing this as separate work rather than when the LED
242 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
243 * potentially bad time, such as a timer interrupt. */
244 static struct workqueue_struct *led_workqueue;
246 #define ASUS_LED(object, ledname) \
247 static void object##_led_set(struct led_classdev *led_cdev, \
248 enum led_brightness value); \
249 static void object##_led_update(struct work_struct *ignored); \
250 static int object##_led_wk; \
251 static DECLARE_WORK(object##_led_work, object##_led_update); \
252 static struct led_classdev object##_led = { \
253 .name = "asus::" ledname, \
254 .brightness_set = object##_led_set, \
257 ASUS_LED(mled, "mail");
258 ASUS_LED(tled, "touchpad");
259 ASUS_LED(rled, "record");
260 ASUS_LED(pled, "phone");
261 ASUS_LED(gled, "gaming");
269 enum { KE_KEY, KE_END };
271 static struct key_entry asus_keymap[] = {
272 {KE_KEY, 0x30, KEY_VOLUMEUP},
273 {KE_KEY, 0x31, KEY_VOLUMEDOWN},
274 {KE_KEY, 0x32, KEY_MUTE},
275 {KE_KEY, 0x33, KEY_SWITCHVIDEOMODE},
276 {KE_KEY, 0x34, KEY_SWITCHVIDEOMODE},
277 {KE_KEY, 0x40, KEY_PREVIOUSSONG},
278 {KE_KEY, 0x41, KEY_NEXTSONG},
279 {KE_KEY, 0x43, KEY_STOPCD},
280 {KE_KEY, 0x45, KEY_PLAYPAUSE},
281 {KE_KEY, 0x50, KEY_EMAIL},
282 {KE_KEY, 0x51, KEY_WWW},
283 {KE_KEY, 0x5C, KEY_SCREENLOCK}, /* Screenlock */
284 {KE_KEY, 0x5D, KEY_WLAN},
285 {KE_KEY, 0x61, KEY_SWITCHVIDEOMODE},
286 {KE_KEY, 0x6B, BTN_TOUCH}, /* Lock Mouse */
287 {KE_KEY, 0x82, KEY_CAMERA},
288 {KE_KEY, 0x8A, KEY_PROG1},
289 {KE_KEY, 0x95, KEY_MEDIA},
290 {KE_KEY, 0x99, KEY_PHONE},
295 * This function evaluates an ACPI method, given an int as parameter, the
296 * method is searched within the scope of the handle, can be NULL. The output
297 * of the method is written is output, which can also be NULL
299 * returns 0 if write is successful, -1 else.
301 static int write_acpi_int(acpi_handle handle, const char *method, int val,
302 struct acpi_buffer *output)
304 struct acpi_object_list params; //list of input parameters (an int here)
305 union acpi_object in_obj; //the only param we use
312 params.pointer = &in_obj;
313 in_obj.type = ACPI_TYPE_INTEGER;
314 in_obj.integer.value = val;
316 status = acpi_evaluate_object(handle, (char *)method, ¶ms, output);
323 static int read_wireless_status(int mask)
325 unsigned long long status;
326 acpi_status rv = AE_OK;
328 if (!wireless_status_handle)
329 return (hotk->status & mask) ? 1 : 0;
331 rv = acpi_evaluate_integer(wireless_status_handle, NULL, NULL, &status);
332 if (ACPI_FAILURE(rv))
333 pr_warning("Error reading Wireless status\n");
335 return (status & mask) ? 1 : 0;
337 return (hotk->status & mask) ? 1 : 0;
340 static int read_gps_status(void)
342 unsigned long long status;
343 acpi_status rv = AE_OK;
345 rv = acpi_evaluate_integer(gps_status_handle, NULL, NULL, &status);
346 if (ACPI_FAILURE(rv))
347 pr_warning("Error reading GPS status\n");
349 return status ? 1 : 0;
351 return (hotk->status & GPS_ON) ? 1 : 0;
354 /* Generic LED functions */
355 static int read_status(int mask)
357 /* There is a special method for both wireless devices */
358 if (mask == BT_ON || mask == WL_ON)
359 return read_wireless_status(mask);
360 else if (mask == GPS_ON)
361 return read_gps_status();
363 return (hotk->status & mask) ? 1 : 0;
366 static void write_status(acpi_handle handle, int out, int mask)
368 hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
375 out = (out & 0x1) + 1;
378 handle = (out) ? gps_on_handle : gps_off_handle;
386 if (write_acpi_int(handle, NULL, out, NULL))
387 pr_warning(" write failed %x\n", mask);
390 /* /sys/class/led handlers */
391 #define ASUS_LED_HANDLER(object, mask) \
392 static void object##_led_set(struct led_classdev *led_cdev, \
393 enum led_brightness value) \
395 object##_led_wk = (value > 0) ? 1 : 0; \
396 queue_work(led_workqueue, &object##_led_work); \
398 static void object##_led_update(struct work_struct *ignored) \
400 int value = object##_led_wk; \
401 write_status(object##_set_handle, value, (mask)); \
404 ASUS_LED_HANDLER(mled, MLED_ON);
405 ASUS_LED_HANDLER(pled, PLED_ON);
406 ASUS_LED_HANDLER(rled, RLED_ON);
407 ASUS_LED_HANDLER(tled, TLED_ON);
408 ASUS_LED_HANDLER(gled, GLED_ON);
410 static int get_lcd_state(void)
412 return read_status(LCD_ON);
415 static int set_lcd_state(int value)
418 acpi_status status = 0;
422 if (lcd == get_lcd_state())
425 if (lcd_switch_handle) {
426 status = acpi_evaluate_object(lcd_switch_handle,
429 if (ACPI_FAILURE(status))
430 pr_warning("Error switching LCD\n");
433 write_status(NULL, lcd, LCD_ON);
437 static void lcd_blank(int blank)
439 struct backlight_device *bd = asus_backlight_device;
442 bd->props.power = blank;
443 backlight_update_status(bd);
447 static int read_brightness(struct backlight_device *bd)
449 unsigned long long value;
450 acpi_status rv = AE_OK;
452 rv = acpi_evaluate_integer(brightness_get_handle, NULL, NULL, &value);
453 if (ACPI_FAILURE(rv))
454 pr_warning("Error reading brightness\n");
459 static int set_brightness(struct backlight_device *bd, int value)
463 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
464 /* 0 <= value <= 15 */
466 if (write_acpi_int(brightness_set_handle, NULL, value, NULL)) {
467 pr_warning("Error changing brightness\n");
474 static int update_bl_status(struct backlight_device *bd)
477 int value = bd->props.brightness;
479 rv = set_brightness(bd, value);
483 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
484 return set_lcd_state(value);
488 * Platform device handlers
492 * We write our info in page, we begin at offset off and cannot write more
493 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
494 * number of bytes written in page
496 static ssize_t show_infos(struct device *dev,
497 struct device_attribute *attr, char *page)
500 unsigned long long temp;
501 char buf[16]; //enough for all info
502 acpi_status rv = AE_OK;
505 * We use the easy way, we don't care of off and count, so we don't set eof
509 len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
510 len += sprintf(page + len, "Model reference : %s\n", hotk->name);
512 * The SFUN method probably allows the original driver to get the list
513 * of features supported by a given model. For now, 0x0100 or 0x0800
514 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
515 * The significance of others is yet to be found.
517 rv = acpi_evaluate_integer(hotk->handle, "SFUN", NULL, &temp);
518 if (!ACPI_FAILURE(rv))
519 len += sprintf(page + len, "SFUN value : %#x\n",
522 * The HWRS method return informations about the hardware.
523 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
524 * The significance of others is yet to be found.
525 * If we don't find the method, we assume the device are present.
527 rv = acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &temp);
528 if (!ACPI_FAILURE(rv))
529 len += sprintf(page + len, "HRWS value : %#x\n",
532 * Another value for userspace: the ASYM method returns 0x02 for
533 * battery low and 0x04 for battery critical, its readings tend to be
534 * more accurate than those provided by _BST.
535 * Note: since not all the laptops provide this method, errors are
538 rv = acpi_evaluate_integer(hotk->handle, "ASYM", NULL, &temp);
539 if (!ACPI_FAILURE(rv))
540 len += sprintf(page + len, "ASYM value : %#x\n",
543 snprintf(buf, 16, "%d", asus_info->length);
544 len += sprintf(page + len, "DSDT length : %s\n", buf);
545 snprintf(buf, 16, "%d", asus_info->checksum);
546 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
547 snprintf(buf, 16, "%d", asus_info->revision);
548 len += sprintf(page + len, "DSDT revision : %s\n", buf);
549 snprintf(buf, 7, "%s", asus_info->oem_id);
550 len += sprintf(page + len, "OEM id : %s\n", buf);
551 snprintf(buf, 9, "%s", asus_info->oem_table_id);
552 len += sprintf(page + len, "OEM table id : %s\n", buf);
553 snprintf(buf, 16, "%x", asus_info->oem_revision);
554 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
555 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
556 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
557 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
558 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
564 static int parse_arg(const char *buf, unsigned long count, int *val)
570 if (sscanf(buf, "%i", val) != 1)
575 static ssize_t store_status(const char *buf, size_t count,
576 acpi_handle handle, int mask)
581 rv = parse_arg(buf, count, &value);
585 write_status(handle, out, mask);
593 static ssize_t show_ledd(struct device *dev,
594 struct device_attribute *attr, char *buf)
596 return sprintf(buf, "0x%08x\n", hotk->ledd_status);
599 static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
600 const char *buf, size_t count)
604 rv = parse_arg(buf, count, &value);
606 if (write_acpi_int(ledd_set_handle, NULL, value, NULL))
607 pr_warning("LED display write failed\n");
609 hotk->ledd_status = (u32) value;
617 static ssize_t show_wlan(struct device *dev,
618 struct device_attribute *attr, char *buf)
620 return sprintf(buf, "%d\n", read_status(WL_ON));
623 static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
624 const char *buf, size_t count)
626 return store_status(buf, count, wl_switch_handle, WL_ON);
632 static ssize_t show_bluetooth(struct device *dev,
633 struct device_attribute *attr, char *buf)
635 return sprintf(buf, "%d\n", read_status(BT_ON));
638 static ssize_t store_bluetooth(struct device *dev,
639 struct device_attribute *attr, const char *buf,
642 return store_status(buf, count, bt_switch_handle, BT_ON);
648 static void set_display(int value)
650 /* no sanity check needed for now */
651 if (write_acpi_int(display_set_handle, NULL, value, NULL))
652 pr_warning("Error setting display\n");
656 static int read_display(void)
658 unsigned long long value = 0;
659 acpi_status rv = AE_OK;
661 /* In most of the case, we know how to set the display, but sometime
663 if (display_get_handle) {
664 rv = acpi_evaluate_integer(display_get_handle, NULL,
666 if (ACPI_FAILURE(rv))
667 pr_warning("Error reading display status\n");
670 value &= 0x0F; /* needed for some models, shouldn't hurt others */
676 * Now, *this* one could be more user-friendly, but so far, no-one has
677 * complained. The significance of bits is the same as in store_disp()
679 static ssize_t show_disp(struct device *dev,
680 struct device_attribute *attr, char *buf)
682 return sprintf(buf, "%d\n", read_display());
686 * Experimental support for display switching. As of now: 1 should activate
687 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
688 * Any combination (bitwise) of these will suffice. I never actually tested 4
689 * displays hooked up simultaneously, so be warned. See the acpi4asus README
692 static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
693 const char *buf, size_t count)
697 rv = parse_arg(buf, count, &value);
706 static void set_light_sens_switch(int value)
708 if (write_acpi_int(ls_switch_handle, NULL, value, NULL))
709 pr_warning("Error setting light sensor switch\n");
710 hotk->light_switch = value;
713 static ssize_t show_lssw(struct device *dev,
714 struct device_attribute *attr, char *buf)
716 return sprintf(buf, "%d\n", hotk->light_switch);
719 static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
720 const char *buf, size_t count)
724 rv = parse_arg(buf, count, &value);
726 set_light_sens_switch(value ? 1 : 0);
731 static void set_light_sens_level(int value)
733 if (write_acpi_int(ls_level_handle, NULL, value, NULL))
734 pr_warning("Error setting light sensor level\n");
735 hotk->light_level = value;
738 static ssize_t show_lslvl(struct device *dev,
739 struct device_attribute *attr, char *buf)
741 return sprintf(buf, "%d\n", hotk->light_level);
744 static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
745 const char *buf, size_t count)
749 rv = parse_arg(buf, count, &value);
751 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
752 /* 0 <= value <= 15 */
753 set_light_sens_level(value);
762 static ssize_t show_gps(struct device *dev,
763 struct device_attribute *attr, char *buf)
765 return sprintf(buf, "%d\n", read_status(GPS_ON));
768 static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
769 const char *buf, size_t count)
771 return store_status(buf, count, NULL, GPS_ON);
777 static struct key_entry *asus_get_entry_by_scancode(int code)
779 struct key_entry *key;
781 for (key = asus_keymap; key->type != KE_END; key++)
782 if (code == key->code)
788 static struct key_entry *asus_get_entry_by_keycode(int code)
790 struct key_entry *key;
792 for (key = asus_keymap; key->type != KE_END; key++)
793 if (code == key->keycode && key->type == KE_KEY)
799 static int asus_getkeycode(struct input_dev *dev, int scancode, int *keycode)
801 struct key_entry *key = asus_get_entry_by_scancode(scancode);
803 if (key && key->type == KE_KEY) {
804 *keycode = key->keycode;
811 static int asus_setkeycode(struct input_dev *dev, int scancode, int keycode)
813 struct key_entry *key;
816 if (keycode < 0 || keycode > KEY_MAX)
819 key = asus_get_entry_by_scancode(scancode);
820 if (key && key->type == KE_KEY) {
821 old_keycode = key->keycode;
822 key->keycode = keycode;
823 set_bit(keycode, dev->keybit);
824 if (!asus_get_entry_by_keycode(old_keycode))
825 clear_bit(old_keycode, dev->keybit);
832 static void asus_hotk_notify(struct acpi_device *device, u32 event)
834 static struct key_entry *key;
837 /* TODO Find a better way to handle events count. */
842 * We need to tell the backlight device when the backlight power is
845 if (event == ATKD_LCD_ON) {
846 write_status(NULL, 1, LCD_ON);
847 lcd_blank(FB_BLANK_UNBLANK);
848 } else if (event == ATKD_LCD_OFF) {
849 write_status(NULL, 0, LCD_ON);
850 lcd_blank(FB_BLANK_POWERDOWN);
853 count = hotk->event_count[event % 128]++;
854 acpi_bus_generate_proc_event(hotk->device, event, count);
855 acpi_bus_generate_netlink_event(hotk->device->pnp.device_class,
856 dev_name(&hotk->device->dev), event,
859 if (hotk->inputdev) {
860 key = asus_get_entry_by_scancode(event);
866 input_report_key(hotk->inputdev, key->keycode, 1);
867 input_sync(hotk->inputdev);
868 input_report_key(hotk->inputdev, key->keycode, 0);
869 input_sync(hotk->inputdev);
875 #define ASUS_CREATE_DEVICE_ATTR(_name) \
876 struct device_attribute dev_attr_##_name = { \
878 .name = __stringify(_name), \
884 #define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
886 dev_attr_##_name.attr.mode = _mode; \
887 dev_attr_##_name.show = _show; \
888 dev_attr_##_name.store = _store; \
891 static ASUS_CREATE_DEVICE_ATTR(infos);
892 static ASUS_CREATE_DEVICE_ATTR(wlan);
893 static ASUS_CREATE_DEVICE_ATTR(bluetooth);
894 static ASUS_CREATE_DEVICE_ATTR(display);
895 static ASUS_CREATE_DEVICE_ATTR(ledd);
896 static ASUS_CREATE_DEVICE_ATTR(ls_switch);
897 static ASUS_CREATE_DEVICE_ATTR(ls_level);
898 static ASUS_CREATE_DEVICE_ATTR(gps);
900 static struct attribute *asuspf_attributes[] = {
901 &dev_attr_infos.attr,
903 &dev_attr_bluetooth.attr,
904 &dev_attr_display.attr,
906 &dev_attr_ls_switch.attr,
907 &dev_attr_ls_level.attr,
912 static struct attribute_group asuspf_attribute_group = {
913 .attrs = asuspf_attributes
916 static struct platform_driver asuspf_driver = {
918 .name = ASUS_HOTK_FILE,
919 .owner = THIS_MODULE,
923 static struct platform_device *asuspf_device;
925 static void asus_hotk_add_fs(void)
927 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
929 if (wl_switch_handle)
930 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
932 if (bt_switch_handle)
933 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
934 show_bluetooth, store_bluetooth);
936 if (display_set_handle && display_get_handle)
937 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
938 else if (display_set_handle)
939 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
942 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
944 if (ls_switch_handle && ls_level_handle) {
945 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
946 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
949 if (gps_status_handle && gps_on_handle && gps_off_handle)
950 ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
953 static int asus_handle_init(char *name, acpi_handle * handle,
954 char **paths, int num_paths)
959 for (i = 0; i < num_paths; i++) {
960 status = acpi_get_handle(NULL, paths[i], handle);
961 if (ACPI_SUCCESS(status))
969 #define ASUS_HANDLE_INIT(object) \
970 asus_handle_init(#object, &object##_handle, object##_paths, \
971 ARRAY_SIZE(object##_paths))
974 * This function is used to initialize the hotk with right values. In this
975 * method, we can make all the detection we want, and modify the hotk struct
977 static int asus_hotk_get_info(void)
979 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
980 union acpi_object *model = NULL;
981 unsigned long long bsts_result, hwrs_result;
986 * Get DSDT headers early enough to allow for differentiating between
987 * models, but late enough to allow acpi_bus_register_driver() to fail
988 * before doing anything ACPI-specific. Should we encounter a machine,
989 * which needs special handling (i.e. its hotkey device has a different
990 * HID), this bit will be moved. A global variable asus_info contains
993 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
994 if (ACPI_FAILURE(status))
995 pr_warning("Couldn't get the DSDT table header\n");
997 /* We have to write 0 on init this far for all ASUS models */
998 if (write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
999 pr_err("Hotkey initialization failed\n");
1003 /* This needs to be called for some laptops to init properly */
1005 acpi_evaluate_integer(hotk->handle, "BSTS", NULL, &bsts_result);
1006 if (ACPI_FAILURE(status))
1007 pr_warning("Error calling BSTS\n");
1008 else if (bsts_result)
1009 pr_notice("BSTS called, 0x%02x returned\n",
1010 (uint) bsts_result);
1013 write_acpi_int(hotk->handle, "CWAP", wapf, NULL);
1016 * Try to match the object returned by INIT to the specific model.
1017 * Handle every possible object (or the lack of thereof) the DSDT
1018 * writers might throw at us. When in trouble, we pass NULL to
1019 * asus_model_match() and try something completely different.
1021 if (buffer.pointer) {
1022 model = buffer.pointer;
1023 switch (model->type) {
1024 case ACPI_TYPE_STRING:
1025 string = model->string.pointer;
1027 case ACPI_TYPE_BUFFER:
1028 string = model->buffer.pointer;
1035 hotk->name = kstrdup(string, GFP_KERNEL);
1040 pr_notice(" %s model detected\n", string);
1042 ASUS_HANDLE_INIT(mled_set);
1043 ASUS_HANDLE_INIT(tled_set);
1044 ASUS_HANDLE_INIT(rled_set);
1045 ASUS_HANDLE_INIT(pled_set);
1046 ASUS_HANDLE_INIT(gled_set);
1048 ASUS_HANDLE_INIT(ledd_set);
1051 * The HWRS method return informations about the hardware.
1052 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1053 * The significance of others is yet to be found.
1054 * If we don't find the method, we assume the device are present.
1057 acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &hwrs_result);
1058 if (ACPI_FAILURE(status))
1059 hwrs_result = WL_HWRS | BT_HWRS;
1061 if (hwrs_result & WL_HWRS)
1062 ASUS_HANDLE_INIT(wl_switch);
1063 if (hwrs_result & BT_HWRS)
1064 ASUS_HANDLE_INIT(bt_switch);
1066 ASUS_HANDLE_INIT(wireless_status);
1068 ASUS_HANDLE_INIT(brightness_set);
1069 ASUS_HANDLE_INIT(brightness_get);
1071 ASUS_HANDLE_INIT(lcd_switch);
1073 ASUS_HANDLE_INIT(display_set);
1074 ASUS_HANDLE_INIT(display_get);
1076 /* There is a lot of models with "ALSL", but a few get
1077 a real light sens, so we need to check it. */
1078 if (!ASUS_HANDLE_INIT(ls_switch))
1079 ASUS_HANDLE_INIT(ls_level);
1081 ASUS_HANDLE_INIT(gps_on);
1082 ASUS_HANDLE_INIT(gps_off);
1083 ASUS_HANDLE_INIT(gps_status);
1090 static int asus_input_init(void)
1092 const struct key_entry *key;
1095 hotk->inputdev = input_allocate_device();
1096 if (!hotk->inputdev) {
1097 pr_info("Unable to allocate input device\n");
1100 hotk->inputdev->name = "Asus Laptop extra buttons";
1101 hotk->inputdev->phys = ASUS_HOTK_FILE "/input0";
1102 hotk->inputdev->id.bustype = BUS_HOST;
1103 hotk->inputdev->getkeycode = asus_getkeycode;
1104 hotk->inputdev->setkeycode = asus_setkeycode;
1106 for (key = asus_keymap; key->type != KE_END; key++) {
1107 switch (key->type) {
1109 set_bit(EV_KEY, hotk->inputdev->evbit);
1110 set_bit(key->keycode, hotk->inputdev->keybit);
1114 result = input_register_device(hotk->inputdev);
1116 pr_info("Unable to register input device\n");
1117 input_free_device(hotk->inputdev);
1122 static int asus_hotk_check(void)
1126 result = acpi_bus_get_status(hotk->device);
1130 if (hotk->device->status.present) {
1131 result = asus_hotk_get_info();
1133 pr_err("Hotkey device not present, aborting\n");
1140 static int asus_hotk_found;
1142 static int asus_hotk_add(struct acpi_device *device)
1149 pr_notice("Asus Laptop Support version %s\n",
1150 ASUS_LAPTOP_VERSION);
1152 hotk = kzalloc(sizeof(struct asus_hotk), GFP_KERNEL);
1156 hotk->handle = device->handle;
1157 strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
1158 strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
1159 device->driver_data = hotk;
1160 hotk->device = device;
1162 result = asus_hotk_check();
1168 asus_hotk_found = 1;
1170 /* WLED and BLED are on by default */
1171 write_status(bt_switch_handle, 1, BT_ON);
1172 write_status(wl_switch_handle, 1, WL_ON);
1174 /* If the h/w switch is off, we need to check the real status */
1175 write_status(NULL, read_status(BT_ON), BT_ON);
1176 write_status(NULL, read_status(WL_ON), WL_ON);
1178 /* LCD Backlight is on by default */
1179 write_status(NULL, 1, LCD_ON);
1181 /* LED display is off by default */
1182 hotk->ledd_status = 0xFFF;
1184 /* Set initial values of light sensor and level */
1185 hotk->light_switch = 1; /* Default to light sensor disabled */
1186 hotk->light_level = 0; /* level 5 for sensor sensitivity */
1188 if (ls_switch_handle)
1189 set_light_sens_switch(hotk->light_switch);
1191 if (ls_level_handle)
1192 set_light_sens_level(hotk->light_level);
1194 /* GPS is on by default */
1195 write_status(NULL, 1, GPS_ON);
1206 static int asus_hotk_remove(struct acpi_device *device, int type)
1208 if (!device || !acpi_driver_data(device))
1217 static void asus_backlight_exit(void)
1219 if (asus_backlight_device)
1220 backlight_device_unregister(asus_backlight_device);
1223 #define ASUS_LED_UNREGISTER(object) \
1224 if (object##_led.dev) \
1225 led_classdev_unregister(&object##_led)
1227 static void asus_led_exit(void)
1229 destroy_workqueue(led_workqueue);
1230 ASUS_LED_UNREGISTER(mled);
1231 ASUS_LED_UNREGISTER(tled);
1232 ASUS_LED_UNREGISTER(pled);
1233 ASUS_LED_UNREGISTER(rled);
1234 ASUS_LED_UNREGISTER(gled);
1237 static void asus_input_exit(void)
1240 input_unregister_device(hotk->inputdev);
1243 static void __exit asus_laptop_exit(void)
1245 asus_backlight_exit();
1249 acpi_bus_unregister_driver(&asus_hotk_driver);
1250 sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
1251 platform_device_unregister(asuspf_device);
1252 platform_driver_unregister(&asuspf_driver);
1255 static int asus_backlight_init(struct device *dev)
1257 struct backlight_device *bd;
1259 if (brightness_set_handle && lcd_switch_handle) {
1260 bd = backlight_device_register(ASUS_HOTK_FILE, dev,
1263 pr_err("Could not register asus backlight device\n");
1264 asus_backlight_device = NULL;
1268 asus_backlight_device = bd;
1270 bd->props.max_brightness = 15;
1271 bd->props.brightness = read_brightness(NULL);
1272 bd->props.power = FB_BLANK_UNBLANK;
1273 backlight_update_status(bd);
1278 static int asus_led_register(acpi_handle handle,
1279 struct led_classdev *ldev, struct device *dev)
1284 return led_classdev_register(dev, ldev);
1287 #define ASUS_LED_REGISTER(object, device) \
1288 asus_led_register(object##_set_handle, &object##_led, device)
1290 static int asus_led_init(struct device *dev)
1294 rv = ASUS_LED_REGISTER(mled, dev);
1298 rv = ASUS_LED_REGISTER(tled, dev);
1302 rv = ASUS_LED_REGISTER(rled, dev);
1306 rv = ASUS_LED_REGISTER(pled, dev);
1310 rv = ASUS_LED_REGISTER(gled, dev);
1314 led_workqueue = create_singlethread_workqueue("led_workqueue");
1321 ASUS_LED_UNREGISTER(gled);
1323 ASUS_LED_UNREGISTER(pled);
1325 ASUS_LED_UNREGISTER(rled);
1327 ASUS_LED_UNREGISTER(tled);
1329 ASUS_LED_UNREGISTER(mled);
1334 static int __init asus_laptop_init(void)
1341 result = acpi_bus_register_driver(&asus_hotk_driver);
1346 * This is a bit of a kludge. We only want this module loaded
1347 * for ASUS systems, but there's currently no way to probe the
1348 * ACPI namespace for ASUS HIDs. So we just return failure if
1349 * we didn't find one, which will cause the module to be
1352 if (!asus_hotk_found) {
1353 acpi_bus_unregister_driver(&asus_hotk_driver);
1357 result = asus_input_init();
1361 /* Register platform stuff */
1362 result = platform_driver_register(&asuspf_driver);
1364 goto fail_platform_driver;
1366 asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
1367 if (!asuspf_device) {
1369 goto fail_platform_device1;
1372 result = platform_device_add(asuspf_device);
1374 goto fail_platform_device2;
1376 result = sysfs_create_group(&asuspf_device->dev.kobj,
1377 &asuspf_attribute_group);
1381 result = asus_led_init(&asuspf_device->dev);
1385 if (!acpi_video_backlight_support()) {
1386 result = asus_backlight_init(&asuspf_device->dev);
1388 goto fail_backlight;
1390 pr_info("Brightness ignored, must be controlled by "
1391 "ACPI video driver\n");
1399 sysfs_remove_group(&asuspf_device->dev.kobj,
1400 &asuspf_attribute_group);
1403 platform_device_del(asuspf_device);
1405 fail_platform_device2:
1406 platform_device_put(asuspf_device);
1408 fail_platform_device1:
1409 platform_driver_unregister(&asuspf_driver);
1411 fail_platform_driver:
1419 module_init(asus_laptop_init);
1420 module_exit(asus_laptop_exit);