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 */
89 #define KEY_ON 0x200 /* Keyboard backlight */
91 #define ASUS_LOG ASUS_HOTK_FILE ": "
92 #define ASUS_ERR KERN_ERR ASUS_LOG
93 #define ASUS_WARNING KERN_WARNING ASUS_LOG
94 #define ASUS_NOTICE KERN_NOTICE ASUS_LOG
95 #define ASUS_INFO KERN_INFO ASUS_LOG
96 #define ASUS_DEBUG KERN_DEBUG ASUS_LOG
98 MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
99 MODULE_DESCRIPTION(ASUS_HOTK_NAME);
100 MODULE_LICENSE("GPL");
103 * WAPF defines the behavior of the Fn+Fx wlan key
104 * The significance of values is yet to be found, but
106 * 0x0 will do nothing
107 * 0x1 will allow to control the device with Fn+Fx key.
108 * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
109 * 0x5 like 0x1 or 0x4
110 * So, if something doesn't work as you want, just try other values =)
112 static uint wapf = 1;
113 module_param(wapf, uint, 0644);
114 MODULE_PARM_DESC(wapf, "WAPF value");
116 static uint wireless_status = 1;
117 static uint bluetooth_status = 1;
119 module_param(wireless_status, uint, 0644);
120 MODULE_PARM_DESC(wireless_status, "Set the wireless status on boot "
121 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
124 module_param(bluetooth_status, uint, 0644);
125 MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
126 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
129 #define ASUS_HANDLE(object, paths...) \
130 static acpi_handle object##_handle = NULL; \
131 static char *object##_paths[] = { paths }
134 ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
135 ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
136 ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
137 ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
138 ASUS_HANDLE(gled_set, ASUS_HOTK_PREFIX "GLED"); /* G1, G2 (probably) */
141 ASUS_HANDLE(ledd_set, ASUS_HOTK_PREFIX "SLCM");
145 * WLED and BLED are not handled like other XLED, because in some dsdt
146 * they also control the WLAN/Bluetooth device.
148 ASUS_HANDLE(wl_switch, ASUS_HOTK_PREFIX "WLED");
149 ASUS_HANDLE(bt_switch, ASUS_HOTK_PREFIX "BLED");
150 ASUS_HANDLE(wireless_status, ASUS_HOTK_PREFIX "RSTS"); /* All new models */
153 ASUS_HANDLE(brightness_set, ASUS_HOTK_PREFIX "SPLV");
154 ASUS_HANDLE(brightness_get, ASUS_HOTK_PREFIX "GPLV");
157 ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
158 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
159 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
160 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
161 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
162 "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
163 "\\_SB.PCI0.PX40.Q10", /* S1x */
164 "\\Q10"); /* A2x, L2D, L3D, M2E */
167 ASUS_HANDLE(display_set, ASUS_HOTK_PREFIX "SDSP");
168 ASUS_HANDLE(display_get,
169 /* A6B, A6K A6R A7D F3JM L4R M6R A3G M6A M6V VX-1 V6J V6V W3Z */
170 "\\_SB.PCI0.P0P1.VGA.GETD",
171 /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V S5A M5A z33A W1Jc W2V G1 */
172 "\\_SB.PCI0.P0P2.VGA.GETD",
174 "\\_SB.PCI0.P0P3.VGA.GETD",
176 "\\_SB.PCI0.P0PA.VGA.GETD",
178 "\\_SB.PCI0.PCI1.VGAC.NMAP",
180 "\\_SB.PCI0.VGA.GETD",
183 /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
187 /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
189 /* A3F A6F A3N A3L M6N W3N W6A */
192 ASUS_HANDLE(ls_switch, ASUS_HOTK_PREFIX "ALSC"); /* Z71A Z71V */
193 ASUS_HANDLE(ls_level, ASUS_HOTK_PREFIX "ALSL"); /* Z71A Z71V */
196 /* R2H use different handle for GPS on/off */
197 ASUS_HANDLE(gps_on, ASUS_HOTK_PREFIX "SDON"); /* R2H */
198 ASUS_HANDLE(gps_off, ASUS_HOTK_PREFIX "SDOF"); /* R2H */
199 ASUS_HANDLE(gps_status, ASUS_HOTK_PREFIX "GPST");
202 ASUS_HANDLE(kled_set, ASUS_HOTK_PREFIX "SLKB");
203 ASUS_HANDLE(kled_get, ASUS_HOTK_PREFIX "GLKB");
206 * This is the main structure, we can use it to store anything interesting
207 * about the hotk device
210 char *name; /* laptop name */
211 struct acpi_device *device; /* the device we are in */
212 acpi_handle handle; /* the handle of the hotk device */
213 char status; /* status of the hotk, for LEDs, ... */
214 u32 ledd_status; /* status of the LED display */
215 u8 light_level; /* light sensor level */
216 u8 light_switch; /* light sensor switch value */
217 u16 event_count[128]; /* count for each event TODO make this better */
218 struct input_dev *inputdev;
223 * This header is made available to allow proper configuration given model,
224 * revision number , ... this info cannot go in struct asus_hotk because it is
225 * available before the hotk
227 static struct acpi_table_header *asus_info;
229 /* The actual device the driver binds to */
230 static struct asus_hotk *hotk;
233 * The hotkey driver declaration
235 static const struct acpi_device_id asus_device_ids[] = {
240 MODULE_DEVICE_TABLE(acpi, asus_device_ids);
242 static int asus_hotk_add(struct acpi_device *device);
243 static int asus_hotk_remove(struct acpi_device *device, int type);
244 static void asus_hotk_notify(struct acpi_device *device, u32 event);
246 static struct acpi_driver asus_hotk_driver = {
247 .name = ASUS_HOTK_NAME,
248 .class = ASUS_HOTK_CLASS,
249 .owner = THIS_MODULE,
250 .ids = asus_device_ids,
251 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
253 .add = asus_hotk_add,
254 .remove = asus_hotk_remove,
255 .notify = asus_hotk_notify,
259 /* The backlight device /sys/class/backlight */
260 static struct backlight_device *asus_backlight_device;
263 * The backlight class declaration
265 static int read_brightness(struct backlight_device *bd);
266 static int update_bl_status(struct backlight_device *bd);
267 static struct backlight_ops asusbl_ops = {
268 .get_brightness = read_brightness,
269 .update_status = update_bl_status,
273 * These functions actually update the LED's, and are called from a
274 * workqueue. By doing this as separate work rather than when the LED
275 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
276 * potentially bad time, such as a timer interrupt.
278 static struct workqueue_struct *led_workqueue;
280 #define ASUS_LED(object, ledname, max) \
281 static void object##_led_set(struct led_classdev *led_cdev, \
282 enum led_brightness value); \
283 static enum led_brightness object##_led_get( \
284 struct led_classdev *led_cdev); \
285 static void object##_led_update(struct work_struct *ignored); \
286 static int object##_led_wk; \
287 static DECLARE_WORK(object##_led_work, object##_led_update); \
288 static struct led_classdev object##_led = { \
289 .name = "asus::" ledname, \
290 .brightness_set = object##_led_set, \
291 .brightness_get = object##_led_get, \
292 .max_brightness = max \
295 ASUS_LED(mled, "mail", 1);
296 ASUS_LED(tled, "touchpad", 1);
297 ASUS_LED(rled, "record", 1);
298 ASUS_LED(pled, "phone", 1);
299 ASUS_LED(gled, "gaming", 1);
300 ASUS_LED(kled, "kbd_backlight", 3);
308 enum { KE_KEY, KE_END };
310 static struct key_entry asus_keymap[] = {
311 {KE_KEY, 0x02, KEY_SCREENLOCK},
312 {KE_KEY, 0x05, KEY_WLAN},
313 {KE_KEY, 0x08, KEY_F13},
314 {KE_KEY, 0x17, KEY_ZOOM},
315 {KE_KEY, 0x1f, KEY_BATTERY},
316 {KE_KEY, 0x30, KEY_VOLUMEUP},
317 {KE_KEY, 0x31, KEY_VOLUMEDOWN},
318 {KE_KEY, 0x32, KEY_MUTE},
319 {KE_KEY, 0x33, KEY_SWITCHVIDEOMODE},
320 {KE_KEY, 0x34, KEY_SWITCHVIDEOMODE},
321 {KE_KEY, 0x40, KEY_PREVIOUSSONG},
322 {KE_KEY, 0x41, KEY_NEXTSONG},
323 {KE_KEY, 0x43, KEY_STOPCD},
324 {KE_KEY, 0x45, KEY_PLAYPAUSE},
325 {KE_KEY, 0x4c, KEY_MEDIA},
326 {KE_KEY, 0x50, KEY_EMAIL},
327 {KE_KEY, 0x51, KEY_WWW},
328 {KE_KEY, 0x55, KEY_CALC},
329 {KE_KEY, 0x5C, KEY_SCREENLOCK}, /* Screenlock */
330 {KE_KEY, 0x5D, KEY_WLAN},
331 {KE_KEY, 0x5E, KEY_WLAN},
332 {KE_KEY, 0x5F, KEY_WLAN},
333 {KE_KEY, 0x60, KEY_SWITCHVIDEOMODE},
334 {KE_KEY, 0x61, KEY_SWITCHVIDEOMODE},
335 {KE_KEY, 0x62, KEY_SWITCHVIDEOMODE},
336 {KE_KEY, 0x63, KEY_SWITCHVIDEOMODE},
337 {KE_KEY, 0x6B, KEY_F13}, /* Lock Touchpad */
338 {KE_KEY, 0x82, KEY_CAMERA},
339 {KE_KEY, 0x88, KEY_WLAN },
340 {KE_KEY, 0x8A, KEY_PROG1},
341 {KE_KEY, 0x95, KEY_MEDIA},
342 {KE_KEY, 0x99, KEY_PHONE},
343 {KE_KEY, 0xc4, KEY_KBDILLUMUP},
344 {KE_KEY, 0xc5, KEY_KBDILLUMDOWN},
349 * This function evaluates an ACPI method, given an int as parameter, the
350 * method is searched within the scope of the handle, can be NULL. The output
351 * of the method is written is output, which can also be NULL
353 * returns 0 if write is successful, -1 else.
355 static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
356 struct acpi_buffer *output)
358 struct acpi_object_list params; /* list of input parameters (an int) */
359 union acpi_object in_obj; /* the only param we use */
366 params.pointer = &in_obj;
367 in_obj.type = ACPI_TYPE_INTEGER;
368 in_obj.integer.value = val;
370 status = acpi_evaluate_object(handle, (char *)method, ¶ms, output);
377 static int write_acpi_int(acpi_handle handle, const char *method, int val)
379 return write_acpi_int_ret(handle, method, val, NULL);
382 static int read_wireless_status(int mask)
384 unsigned long long status;
385 acpi_status rv = AE_OK;
387 if (!wireless_status_handle)
388 return (hotk->status & mask) ? 1 : 0;
390 rv = acpi_evaluate_integer(wireless_status_handle, NULL, NULL, &status);
391 if (ACPI_FAILURE(rv))
392 pr_warning("Error reading Wireless status\n");
394 return (status & mask) ? 1 : 0;
396 return (hotk->status & mask) ? 1 : 0;
399 static int read_gps_status(void)
401 unsigned long long status;
402 acpi_status rv = AE_OK;
404 rv = acpi_evaluate_integer(gps_status_handle, NULL, NULL, &status);
405 if (ACPI_FAILURE(rv))
406 pr_warning("Error reading GPS status\n");
408 return status ? 1 : 0;
410 return (hotk->status & GPS_ON) ? 1 : 0;
413 /* Generic LED functions */
414 static int read_status(int mask)
416 /* There is a special method for both wireless devices */
417 if (mask == BT_ON || mask == WL_ON)
418 return read_wireless_status(mask);
419 else if (mask == GPS_ON)
420 return read_gps_status();
422 return (hotk->status & mask) ? 1 : 0;
425 static void write_status(acpi_handle handle, int out, int mask)
427 hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
434 out = (out & 0x1) + 1;
437 handle = (out) ? gps_on_handle : gps_off_handle;
445 if (write_acpi_int(handle, NULL, out))
446 pr_warning(" write failed %x\n", mask);
449 /* /sys/class/led handlers */
450 #define ASUS_LED_HANDLER(object, mask) \
451 static void object##_led_set(struct led_classdev *led_cdev, \
452 enum led_brightness value) \
454 object##_led_wk = (value > 0) ? 1 : 0; \
455 queue_work(led_workqueue, &object##_led_work); \
457 static void object##_led_update(struct work_struct *ignored) \
459 int value = object##_led_wk; \
460 write_status(object##_set_handle, value, (mask)); \
462 static enum led_brightness object##_led_get( \
463 struct led_classdev *led_cdev) \
465 return led_cdev->brightness; \
468 ASUS_LED_HANDLER(mled, MLED_ON);
469 ASUS_LED_HANDLER(pled, PLED_ON);
470 ASUS_LED_HANDLER(rled, RLED_ON);
471 ASUS_LED_HANDLER(tled, TLED_ON);
472 ASUS_LED_HANDLER(gled, GLED_ON);
477 static int get_kled_lvl(void)
479 unsigned long long kblv;
480 struct acpi_object_list params;
481 union acpi_object in_obj;
485 params.pointer = &in_obj;
486 in_obj.type = ACPI_TYPE_INTEGER;
487 in_obj.integer.value = 2;
489 rv = acpi_evaluate_integer(kled_get_handle, NULL, ¶ms, &kblv);
490 if (ACPI_FAILURE(rv)) {
491 pr_warning("Error reading kled level\n");
497 static int set_kled_lvl(int kblv)
500 kblv = (1 << 7) | (kblv & 0x7F);
504 if (write_acpi_int(kled_set_handle, NULL, kblv)) {
505 pr_warning("Keyboard LED display write failed\n");
511 static void kled_led_set(struct led_classdev *led_cdev,
512 enum led_brightness value)
515 queue_work(led_workqueue, &kled_led_work);
518 static void kled_led_update(struct work_struct *ignored)
520 set_kled_lvl(kled_led_wk);
523 static enum led_brightness kled_led_get(struct led_classdev *led_cdev)
525 return get_kled_lvl();
528 static int get_lcd_state(void)
530 return read_status(LCD_ON);
533 static int set_lcd_state(int value)
536 acpi_status status = 0;
540 if (lcd == get_lcd_state())
543 if (lcd_switch_handle) {
544 status = acpi_evaluate_object(lcd_switch_handle,
547 if (ACPI_FAILURE(status))
548 pr_warning("Error switching LCD\n");
551 write_status(NULL, lcd, LCD_ON);
555 static void lcd_blank(int blank)
557 struct backlight_device *bd = asus_backlight_device;
560 bd->props.power = blank;
561 backlight_update_status(bd);
565 static int read_brightness(struct backlight_device *bd)
567 unsigned long long value;
568 acpi_status rv = AE_OK;
570 rv = acpi_evaluate_integer(brightness_get_handle, NULL, NULL, &value);
571 if (ACPI_FAILURE(rv))
572 pr_warning("Error reading brightness\n");
577 static int set_brightness(struct backlight_device *bd, int value)
579 if (write_acpi_int(brightness_set_handle, NULL, value)) {
580 pr_warning("Error changing brightness\n");
586 static int update_bl_status(struct backlight_device *bd)
589 int value = bd->props.brightness;
591 rv = set_brightness(bd, value);
595 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
596 return set_lcd_state(value);
600 * Platform device handlers
604 * We write our info in page, we begin at offset off and cannot write more
605 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
606 * number of bytes written in page
608 static ssize_t show_infos(struct device *dev,
609 struct device_attribute *attr, char *page)
612 unsigned long long temp;
613 char buf[16]; /* enough for all info */
614 acpi_status rv = AE_OK;
617 * We use the easy way, we don't care of off and count, so we don't set eof
621 len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
622 len += sprintf(page + len, "Model reference : %s\n", hotk->name);
624 * The SFUN method probably allows the original driver to get the list
625 * of features supported by a given model. For now, 0x0100 or 0x0800
626 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
627 * The significance of others is yet to be found.
629 rv = acpi_evaluate_integer(hotk->handle, "SFUN", NULL, &temp);
630 if (!ACPI_FAILURE(rv))
631 len += sprintf(page + len, "SFUN value : %#x\n",
634 * The HWRS method return informations about the hardware.
635 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
636 * The significance of others is yet to be found.
637 * If we don't find the method, we assume the device are present.
639 rv = acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &temp);
640 if (!ACPI_FAILURE(rv))
641 len += sprintf(page + len, "HRWS value : %#x\n",
644 * Another value for userspace: the ASYM method returns 0x02 for
645 * battery low and 0x04 for battery critical, its readings tend to be
646 * more accurate than those provided by _BST.
647 * Note: since not all the laptops provide this method, errors are
650 rv = acpi_evaluate_integer(hotk->handle, "ASYM", NULL, &temp);
651 if (!ACPI_FAILURE(rv))
652 len += sprintf(page + len, "ASYM value : %#x\n",
655 snprintf(buf, 16, "%d", asus_info->length);
656 len += sprintf(page + len, "DSDT length : %s\n", buf);
657 snprintf(buf, 16, "%d", asus_info->checksum);
658 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
659 snprintf(buf, 16, "%d", asus_info->revision);
660 len += sprintf(page + len, "DSDT revision : %s\n", buf);
661 snprintf(buf, 7, "%s", asus_info->oem_id);
662 len += sprintf(page + len, "OEM id : %s\n", buf);
663 snprintf(buf, 9, "%s", asus_info->oem_table_id);
664 len += sprintf(page + len, "OEM table id : %s\n", buf);
665 snprintf(buf, 16, "%x", asus_info->oem_revision);
666 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
667 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
668 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
669 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
670 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
676 static int parse_arg(const char *buf, unsigned long count, int *val)
682 if (sscanf(buf, "%i", val) != 1)
687 static ssize_t store_status(const char *buf, size_t count,
688 acpi_handle handle, int mask)
693 rv = parse_arg(buf, count, &value);
697 write_status(handle, out, mask);
705 static ssize_t show_ledd(struct device *dev,
706 struct device_attribute *attr, char *buf)
708 return sprintf(buf, "0x%08x\n", hotk->ledd_status);
711 static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
712 const char *buf, size_t count)
716 rv = parse_arg(buf, count, &value);
718 if (write_acpi_int(ledd_set_handle, NULL, value))
719 pr_warning("LED display write failed\n");
721 hotk->ledd_status = (u32) value;
729 static ssize_t show_wlan(struct device *dev,
730 struct device_attribute *attr, char *buf)
732 return sprintf(buf, "%d\n", read_status(WL_ON));
735 static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
736 const char *buf, size_t count)
738 return store_status(buf, count, wl_switch_handle, WL_ON);
744 static ssize_t show_bluetooth(struct device *dev,
745 struct device_attribute *attr, char *buf)
747 return sprintf(buf, "%d\n", read_status(BT_ON));
750 static ssize_t store_bluetooth(struct device *dev,
751 struct device_attribute *attr, const char *buf,
754 return store_status(buf, count, bt_switch_handle, BT_ON);
760 static void set_display(int value)
762 /* no sanity check needed for now */
763 if (write_acpi_int(display_set_handle, NULL, value))
764 pr_warning("Error setting display\n");
768 static int read_display(void)
770 unsigned long long value = 0;
771 acpi_status rv = AE_OK;
774 * In most of the case, we know how to set the display, but sometime
777 if (display_get_handle) {
778 rv = acpi_evaluate_integer(display_get_handle, NULL,
780 if (ACPI_FAILURE(rv))
781 pr_warning("Error reading display status\n");
784 value &= 0x0F; /* needed for some models, shouldn't hurt others */
790 * Now, *this* one could be more user-friendly, but so far, no-one has
791 * complained. The significance of bits is the same as in store_disp()
793 static ssize_t show_disp(struct device *dev,
794 struct device_attribute *attr, char *buf)
796 return sprintf(buf, "%d\n", read_display());
800 * Experimental support for display switching. As of now: 1 should activate
801 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
802 * Any combination (bitwise) of these will suffice. I never actually tested 4
803 * displays hooked up simultaneously, so be warned. See the acpi4asus README
806 static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
807 const char *buf, size_t count)
811 rv = parse_arg(buf, count, &value);
820 static void set_light_sens_switch(int value)
822 if (write_acpi_int(ls_switch_handle, NULL, value))
823 pr_warning("Error setting light sensor switch\n");
824 hotk->light_switch = value;
827 static ssize_t show_lssw(struct device *dev,
828 struct device_attribute *attr, char *buf)
830 return sprintf(buf, "%d\n", hotk->light_switch);
833 static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
834 const char *buf, size_t count)
838 rv = parse_arg(buf, count, &value);
840 set_light_sens_switch(value ? 1 : 0);
845 static void set_light_sens_level(int value)
847 if (write_acpi_int(ls_level_handle, NULL, value))
848 pr_warning("Error setting light sensor level\n");
849 hotk->light_level = value;
852 static ssize_t show_lslvl(struct device *dev,
853 struct device_attribute *attr, char *buf)
855 return sprintf(buf, "%d\n", hotk->light_level);
858 static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
859 const char *buf, size_t count)
863 rv = parse_arg(buf, count, &value);
865 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
866 /* 0 <= value <= 15 */
867 set_light_sens_level(value);
876 static ssize_t show_gps(struct device *dev,
877 struct device_attribute *attr, char *buf)
879 return sprintf(buf, "%d\n", read_status(GPS_ON));
882 static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
883 const char *buf, size_t count)
885 return store_status(buf, count, NULL, GPS_ON);
891 static struct key_entry *asus_get_entry_by_scancode(int code)
893 struct key_entry *key;
895 for (key = asus_keymap; key->type != KE_END; key++)
896 if (code == key->code)
902 static struct key_entry *asus_get_entry_by_keycode(int code)
904 struct key_entry *key;
906 for (key = asus_keymap; key->type != KE_END; key++)
907 if (code == key->keycode && key->type == KE_KEY)
913 static int asus_getkeycode(struct input_dev *dev, int scancode, int *keycode)
915 struct key_entry *key = asus_get_entry_by_scancode(scancode);
917 if (key && key->type == KE_KEY) {
918 *keycode = key->keycode;
925 static int asus_setkeycode(struct input_dev *dev, int scancode, int keycode)
927 struct key_entry *key;
930 if (keycode < 0 || keycode > KEY_MAX)
933 key = asus_get_entry_by_scancode(scancode);
934 if (key && key->type == KE_KEY) {
935 old_keycode = key->keycode;
936 key->keycode = keycode;
937 set_bit(keycode, dev->keybit);
938 if (!asus_get_entry_by_keycode(old_keycode))
939 clear_bit(old_keycode, dev->keybit);
946 static void asus_hotk_notify(struct acpi_device *device, u32 event)
948 static struct key_entry *key;
951 /* TODO Find a better way to handle events count. */
956 * We need to tell the backlight device when the backlight power is
959 if (event == ATKD_LCD_ON) {
960 write_status(NULL, 1, LCD_ON);
961 lcd_blank(FB_BLANK_UNBLANK);
962 } else if (event == ATKD_LCD_OFF) {
963 write_status(NULL, 0, LCD_ON);
964 lcd_blank(FB_BLANK_POWERDOWN);
967 count = hotk->event_count[event % 128]++;
968 acpi_bus_generate_proc_event(hotk->device, event, count);
969 acpi_bus_generate_netlink_event(hotk->device->pnp.device_class,
970 dev_name(&hotk->device->dev), event,
973 if (hotk->inputdev) {
974 key = asus_get_entry_by_scancode(event);
980 input_report_key(hotk->inputdev, key->keycode, 1);
981 input_sync(hotk->inputdev);
982 input_report_key(hotk->inputdev, key->keycode, 0);
983 input_sync(hotk->inputdev);
989 #define ASUS_CREATE_DEVICE_ATTR(_name) \
990 struct device_attribute dev_attr_##_name = { \
992 .name = __stringify(_name), \
998 #define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
1000 dev_attr_##_name.attr.mode = _mode; \
1001 dev_attr_##_name.show = _show; \
1002 dev_attr_##_name.store = _store; \
1005 static ASUS_CREATE_DEVICE_ATTR(infos);
1006 static ASUS_CREATE_DEVICE_ATTR(wlan);
1007 static ASUS_CREATE_DEVICE_ATTR(bluetooth);
1008 static ASUS_CREATE_DEVICE_ATTR(display);
1009 static ASUS_CREATE_DEVICE_ATTR(ledd);
1010 static ASUS_CREATE_DEVICE_ATTR(ls_switch);
1011 static ASUS_CREATE_DEVICE_ATTR(ls_level);
1012 static ASUS_CREATE_DEVICE_ATTR(gps);
1014 static struct attribute *asuspf_attributes[] = {
1015 &dev_attr_infos.attr,
1016 &dev_attr_wlan.attr,
1017 &dev_attr_bluetooth.attr,
1018 &dev_attr_display.attr,
1019 &dev_attr_ledd.attr,
1020 &dev_attr_ls_switch.attr,
1021 &dev_attr_ls_level.attr,
1026 static struct attribute_group asuspf_attribute_group = {
1027 .attrs = asuspf_attributes
1030 static struct platform_driver asuspf_driver = {
1032 .name = ASUS_HOTK_FILE,
1033 .owner = THIS_MODULE,
1037 static struct platform_device *asuspf_device;
1039 static void asus_hotk_add_fs(void)
1041 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
1043 if (wl_switch_handle)
1044 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
1046 if (bt_switch_handle)
1047 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
1048 show_bluetooth, store_bluetooth);
1050 if (display_set_handle && display_get_handle)
1051 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
1052 else if (display_set_handle)
1053 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
1055 if (ledd_set_handle)
1056 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
1058 if (ls_switch_handle && ls_level_handle) {
1059 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
1060 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
1063 if (gps_status_handle && gps_on_handle && gps_off_handle)
1064 ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
1067 static int asus_handle_init(char *name, acpi_handle * handle,
1068 char **paths, int num_paths)
1073 for (i = 0; i < num_paths; i++) {
1074 status = acpi_get_handle(NULL, paths[i], handle);
1075 if (ACPI_SUCCESS(status))
1083 #define ASUS_HANDLE_INIT(object) \
1084 asus_handle_init(#object, &object##_handle, object##_paths, \
1085 ARRAY_SIZE(object##_paths))
1088 * This function is used to initialize the hotk with right values. In this
1089 * method, we can make all the detection we want, and modify the hotk struct
1091 static int asus_hotk_get_info(void)
1093 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1094 union acpi_object *model = NULL;
1095 unsigned long long bsts_result, hwrs_result;
1096 char *string = NULL;
1100 * Get DSDT headers early enough to allow for differentiating between
1101 * models, but late enough to allow acpi_bus_register_driver() to fail
1102 * before doing anything ACPI-specific. Should we encounter a machine,
1103 * which needs special handling (i.e. its hotkey device has a different
1104 * HID), this bit will be moved. A global variable asus_info contains
1107 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
1108 if (ACPI_FAILURE(status))
1109 pr_warning("Couldn't get the DSDT table header\n");
1111 /* We have to write 0 on init this far for all ASUS models */
1112 if (write_acpi_int_ret(hotk->handle, "INIT", 0, &buffer)) {
1113 pr_err("Hotkey initialization failed\n");
1117 /* This needs to be called for some laptops to init properly */
1119 acpi_evaluate_integer(hotk->handle, "BSTS", NULL, &bsts_result);
1120 if (ACPI_FAILURE(status))
1121 pr_warning("Error calling BSTS\n");
1122 else if (bsts_result)
1123 pr_notice("BSTS called, 0x%02x returned\n",
1124 (uint) bsts_result);
1127 write_acpi_int(hotk->handle, "CWAP", wapf);
1130 * Try to match the object returned by INIT to the specific model.
1131 * Handle every possible object (or the lack of thereof) the DSDT
1132 * writers might throw at us. When in trouble, we pass NULL to
1133 * asus_model_match() and try something completely different.
1135 if (buffer.pointer) {
1136 model = buffer.pointer;
1137 switch (model->type) {
1138 case ACPI_TYPE_STRING:
1139 string = model->string.pointer;
1141 case ACPI_TYPE_BUFFER:
1142 string = model->buffer.pointer;
1149 hotk->name = kstrdup(string, GFP_KERNEL);
1154 pr_notice(" %s model detected\n", string);
1156 ASUS_HANDLE_INIT(mled_set);
1157 ASUS_HANDLE_INIT(tled_set);
1158 ASUS_HANDLE_INIT(rled_set);
1159 ASUS_HANDLE_INIT(pled_set);
1160 ASUS_HANDLE_INIT(gled_set);
1162 ASUS_HANDLE_INIT(ledd_set);
1164 ASUS_HANDLE_INIT(kled_set);
1165 ASUS_HANDLE_INIT(kled_get);
1168 * The HWRS method return informations about the hardware.
1169 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1170 * The significance of others is yet to be found.
1171 * If we don't find the method, we assume the device are present.
1174 acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &hwrs_result);
1175 if (ACPI_FAILURE(status))
1176 hwrs_result = WL_HWRS | BT_HWRS;
1178 if (hwrs_result & WL_HWRS)
1179 ASUS_HANDLE_INIT(wl_switch);
1180 if (hwrs_result & BT_HWRS)
1181 ASUS_HANDLE_INIT(bt_switch);
1183 ASUS_HANDLE_INIT(wireless_status);
1185 ASUS_HANDLE_INIT(brightness_set);
1186 ASUS_HANDLE_INIT(brightness_get);
1188 ASUS_HANDLE_INIT(lcd_switch);
1190 ASUS_HANDLE_INIT(display_set);
1191 ASUS_HANDLE_INIT(display_get);
1194 * There is a lot of models with "ALSL", but a few get
1195 * a real light sens, so we need to check it.
1197 if (!ASUS_HANDLE_INIT(ls_switch))
1198 ASUS_HANDLE_INIT(ls_level);
1200 ASUS_HANDLE_INIT(gps_on);
1201 ASUS_HANDLE_INIT(gps_off);
1202 ASUS_HANDLE_INIT(gps_status);
1209 static int asus_input_init(void)
1211 const struct key_entry *key;
1214 hotk->inputdev = input_allocate_device();
1215 if (!hotk->inputdev) {
1216 pr_info("Unable to allocate input device\n");
1219 hotk->inputdev->name = "Asus Laptop extra buttons";
1220 hotk->inputdev->phys = ASUS_HOTK_FILE "/input0";
1221 hotk->inputdev->id.bustype = BUS_HOST;
1222 hotk->inputdev->getkeycode = asus_getkeycode;
1223 hotk->inputdev->setkeycode = asus_setkeycode;
1225 for (key = asus_keymap; key->type != KE_END; key++) {
1226 switch (key->type) {
1228 set_bit(EV_KEY, hotk->inputdev->evbit);
1229 set_bit(key->keycode, hotk->inputdev->keybit);
1233 result = input_register_device(hotk->inputdev);
1235 pr_info("Unable to register input device\n");
1236 input_free_device(hotk->inputdev);
1241 static int asus_hotk_check(void)
1245 result = acpi_bus_get_status(hotk->device);
1249 if (hotk->device->status.present) {
1250 result = asus_hotk_get_info();
1252 pr_err("Hotkey device not present, aborting\n");
1259 static int asus_hotk_found;
1261 static int asus_hotk_add(struct acpi_device *device)
1265 pr_notice("Asus Laptop Support version %s\n",
1266 ASUS_LAPTOP_VERSION);
1268 hotk = kzalloc(sizeof(struct asus_hotk), GFP_KERNEL);
1272 hotk->handle = device->handle;
1273 strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
1274 strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
1275 device->driver_data = hotk;
1276 hotk->device = device;
1278 result = asus_hotk_check();
1284 asus_hotk_found = 1;
1286 /* WLED and BLED are on by default */
1287 if (bluetooth_status != -1)
1288 write_status(bt_switch_handle, !!bluetooth_status, BT_ON);
1289 if (wireless_status != -1)
1290 write_status(wl_switch_handle, !!wireless_status, WL_ON);
1292 /* If the h/w switch is off, we need to check the real status */
1293 write_status(NULL, read_status(BT_ON), BT_ON);
1294 write_status(NULL, read_status(WL_ON), WL_ON);
1296 /* LCD Backlight is on by default */
1297 write_status(NULL, 1, LCD_ON);
1299 /* Keyboard Backlight is on by default */
1300 if (kled_set_handle)
1303 /* LED display is off by default */
1304 hotk->ledd_status = 0xFFF;
1306 /* Set initial values of light sensor and level */
1307 hotk->light_switch = 0; /* Default to light sensor disabled */
1308 hotk->light_level = 5; /* level 5 for sensor sensitivity */
1310 if (ls_switch_handle)
1311 set_light_sens_switch(hotk->light_switch);
1313 if (ls_level_handle)
1314 set_light_sens_level(hotk->light_level);
1316 /* GPS is on by default */
1317 write_status(NULL, 1, GPS_ON);
1328 static int asus_hotk_remove(struct acpi_device *device, int type)
1336 static void asus_backlight_exit(void)
1338 if (asus_backlight_device)
1339 backlight_device_unregister(asus_backlight_device);
1342 #define ASUS_LED_UNREGISTER(object) \
1343 if (object##_led.dev) \
1344 led_classdev_unregister(&object##_led)
1346 static void asus_led_exit(void)
1348 destroy_workqueue(led_workqueue);
1349 ASUS_LED_UNREGISTER(mled);
1350 ASUS_LED_UNREGISTER(tled);
1351 ASUS_LED_UNREGISTER(pled);
1352 ASUS_LED_UNREGISTER(rled);
1353 ASUS_LED_UNREGISTER(gled);
1354 ASUS_LED_UNREGISTER(kled);
1357 static void asus_input_exit(void)
1360 input_unregister_device(hotk->inputdev);
1363 static void __exit asus_laptop_exit(void)
1365 asus_backlight_exit();
1369 acpi_bus_unregister_driver(&asus_hotk_driver);
1370 sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
1371 platform_device_unregister(asuspf_device);
1372 platform_driver_unregister(&asuspf_driver);
1375 static int asus_backlight_init(struct device *dev)
1377 struct backlight_device *bd;
1379 if (brightness_set_handle && lcd_switch_handle) {
1380 bd = backlight_device_register(ASUS_HOTK_FILE, dev,
1383 pr_err("Could not register asus backlight device\n");
1384 asus_backlight_device = NULL;
1388 asus_backlight_device = bd;
1390 bd->props.max_brightness = 15;
1391 bd->props.brightness = read_brightness(NULL);
1392 bd->props.power = FB_BLANK_UNBLANK;
1393 backlight_update_status(bd);
1398 static int asus_led_register(acpi_handle handle,
1399 struct led_classdev *ldev, struct device *dev)
1404 return led_classdev_register(dev, ldev);
1407 #define ASUS_LED_REGISTER(object, device) \
1408 asus_led_register(object##_set_handle, &object##_led, device)
1410 static int asus_led_init(struct device *dev)
1414 rv = ASUS_LED_REGISTER(mled, dev);
1418 rv = ASUS_LED_REGISTER(tled, dev);
1422 rv = ASUS_LED_REGISTER(rled, dev);
1426 rv = ASUS_LED_REGISTER(pled, dev);
1430 rv = ASUS_LED_REGISTER(gled, dev);
1434 if (kled_set_handle && kled_get_handle)
1435 rv = ASUS_LED_REGISTER(kled, dev);
1439 led_workqueue = create_singlethread_workqueue("led_workqueue");
1446 ASUS_LED_UNREGISTER(kled);
1448 ASUS_LED_UNREGISTER(gled);
1450 ASUS_LED_UNREGISTER(pled);
1452 ASUS_LED_UNREGISTER(rled);
1454 ASUS_LED_UNREGISTER(tled);
1456 ASUS_LED_UNREGISTER(mled);
1461 static int __init asus_laptop_init(void)
1465 result = acpi_bus_register_driver(&asus_hotk_driver);
1470 * This is a bit of a kludge. We only want this module loaded
1471 * for ASUS systems, but there's currently no way to probe the
1472 * ACPI namespace for ASUS HIDs. So we just return failure if
1473 * we didn't find one, which will cause the module to be
1476 if (!asus_hotk_found) {
1477 acpi_bus_unregister_driver(&asus_hotk_driver);
1481 result = asus_input_init();
1485 /* Register platform stuff */
1486 result = platform_driver_register(&asuspf_driver);
1488 goto fail_platform_driver;
1490 asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
1491 if (!asuspf_device) {
1493 goto fail_platform_device1;
1496 result = platform_device_add(asuspf_device);
1498 goto fail_platform_device2;
1500 result = sysfs_create_group(&asuspf_device->dev.kobj,
1501 &asuspf_attribute_group);
1505 result = asus_led_init(&asuspf_device->dev);
1509 if (!acpi_video_backlight_support()) {
1510 result = asus_backlight_init(&asuspf_device->dev);
1512 goto fail_backlight;
1514 pr_info("Brightness ignored, must be controlled by "
1515 "ACPI video driver\n");
1523 sysfs_remove_group(&asuspf_device->dev.kobj,
1524 &asuspf_attribute_group);
1527 platform_device_del(asuspf_device);
1529 fail_platform_device2:
1530 platform_device_put(asuspf_device);
1532 fail_platform_device1:
1533 platform_driver_unregister(&asuspf_driver);
1535 fail_platform_driver:
1543 module_init(asus_laptop_init);
1544 module_exit(asus_laptop_exit);