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 <linux/uaccess.h>
49 #include <linux/input.h>
50 #include <linux/input/sparse-keymap.h>
51 #include <linux/rfkill.h>
52 #include <linux/slab.h>
53 #include <acpi/acpi_drivers.h>
54 #include <acpi/acpi_bus.h>
56 #define ASUS_LAPTOP_VERSION "0.42"
58 #define ASUS_LAPTOP_NAME "Asus Laptop Support"
59 #define ASUS_LAPTOP_CLASS "hotkey"
60 #define ASUS_LAPTOP_DEVICE_NAME "Hotkey"
61 #define ASUS_LAPTOP_FILE KBUILD_MODNAME
62 #define ASUS_LAPTOP_PREFIX "\\_SB.ATKD."
64 MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
65 MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
66 MODULE_LICENSE("GPL");
69 * WAPF defines the behavior of the Fn+Fx wlan key
70 * The significance of values is yet to be found, but
73 * 0x1 will allow to control the device with Fn+Fx key.
74 * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
76 * So, if something doesn't work as you want, just try other values =)
79 module_param(wapf, uint, 0444);
80 MODULE_PARM_DESC(wapf, "WAPF value");
82 static int wlan_status = 1;
83 static int bluetooth_status = 1;
85 module_param(wlan_status, int, 0444);
86 MODULE_PARM_DESC(wlan_status, "Set the wireless status on boot "
87 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
90 module_param(bluetooth_status, int, 0444);
91 MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
92 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
96 * Some events we use, same for all Asus
98 #define ATKD_BR_UP 0x10 /* (event & ~ATKD_BR_UP) = brightness level */
99 #define ATKD_BR_DOWN 0x20 /* (event & ~ATKD_BR_DOWN) = britghness level */
100 #define ATKD_BR_MIN ATKD_BR_UP
101 #define ATKD_BR_MAX (ATKD_BR_DOWN | 0xF) /* 0x2f */
102 #define ATKD_LCD_ON 0x33
103 #define ATKD_LCD_OFF 0x34
106 * Known bits returned by \_SB.ATKD.HWRS
109 #define BT_HWRS 0x100
112 * Flags for hotk status
113 * WL_ON and BT_ON are also used for wireless_status()
115 #define WL_RSTS 0x01 /* internal Wifi */
116 #define BT_RSTS 0x02 /* internal Bluetooth */
119 #define METHOD_MLED "MLED"
120 #define METHOD_TLED "TLED"
121 #define METHOD_RLED "RLED" /* W1JC */
122 #define METHOD_PLED "PLED" /* A7J */
123 #define METHOD_GLED "GLED" /* G1, G2 (probably) */
126 #define METHOD_LEDD "SLCM"
130 * WLED and BLED are not handled like other XLED, because in some dsdt
131 * they also control the WLAN/Bluetooth device.
133 #define METHOD_WLAN "WLED"
134 #define METHOD_BLUETOOTH "BLED"
135 #define METHOD_WL_STATUS "RSTS"
138 #define METHOD_BRIGHTNESS_SET "SPLV"
139 #define METHOD_BRIGHTNESS_GET "GPLV"
142 static acpi_handle lcd_switch_handle;
143 static char *lcd_switch_paths[] = {
144 "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
145 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
146 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
147 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
148 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
149 "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
150 "\\_SB.PCI0.PX40.Q10", /* S1x */
151 "\\Q10"}; /* A2x, L2D, L3D, M2E */
154 #define METHOD_SWITCH_DISPLAY "SDSP"
156 static acpi_handle display_get_handle;
157 static char *display_get_paths[] = {
158 /* A6B, A6K A6R A7D F3JM L4R M6R A3G M6A M6V VX-1 V6J V6V W3Z */
159 "\\_SB.PCI0.P0P1.VGA.GETD",
160 /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V S5A M5A z33A W1Jc W2V G1 */
161 "\\_SB.PCI0.P0P2.VGA.GETD",
163 "\\_SB.PCI0.P0P3.VGA.GETD",
165 "\\_SB.PCI0.P0PA.VGA.GETD",
167 "\\_SB.PCI0.PCI1.VGAC.NMAP",
169 "\\_SB.PCI0.VGA.GETD",
172 /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
176 /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
178 /* A3F A6F A3N A3L M6N W3N W6A */
181 #define METHOD_ALS_CONTROL "ALSC" /* Z71A Z71V */
182 #define METHOD_ALS_LEVEL "ALSL" /* Z71A Z71V */
185 /* R2H use different handle for GPS on/off */
186 #define METHOD_GPS_ON "SDON"
187 #define METHOD_GPS_OFF "SDOF"
188 #define METHOD_GPS_STATUS "GPST"
191 #define METHOD_KBD_LIGHT_SET "SLKB"
192 #define METHOD_KBD_LIGHT_GET "GLKB"
195 * Define a specific led structure to keep the main structure clean
199 struct work_struct work;
200 struct led_classdev led;
201 struct asus_laptop *asus;
206 * This is the main structure, we can use it to store anything interesting
207 * about the hotk device
210 char *name; /* laptop name */
212 struct acpi_table_header *dsdt_info;
213 struct platform_device *platform_device;
214 struct acpi_device *device; /* the device we are in */
215 struct backlight_device *backlight_device;
217 struct input_dev *inputdev;
218 struct key_entry *keymap;
220 struct asus_led mled;
221 struct asus_led tled;
222 struct asus_led rled;
223 struct asus_led pled;
224 struct asus_led gled;
225 struct asus_led kled;
226 struct workqueue_struct *led_workqueue;
232 struct rfkill *gps_rfkill;
234 acpi_handle handle; /* the handle of the hotk device */
235 u32 ledd_status; /* status of the LED display */
236 u8 light_level; /* light sensor level */
237 u8 light_switch; /* light sensor switch value */
238 u16 event_count[128]; /* count for each event TODO make this better */
241 static const struct key_entry asus_keymap[] = {
242 /* Lenovo SL Specific keycodes */
243 {KE_KEY, 0x02, { KEY_SCREENLOCK } },
244 {KE_KEY, 0x05, { KEY_WLAN } },
245 {KE_KEY, 0x08, { KEY_F13 } },
246 {KE_KEY, 0x17, { KEY_ZOOM } },
247 {KE_KEY, 0x1f, { KEY_BATTERY } },
248 /* End of Lenovo SL Specific keycodes */
249 {KE_KEY, 0x30, { KEY_VOLUMEUP } },
250 {KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
251 {KE_KEY, 0x32, { KEY_MUTE } },
252 {KE_KEY, 0x33, { KEY_SWITCHVIDEOMODE } },
253 {KE_KEY, 0x34, { KEY_SWITCHVIDEOMODE } },
254 {KE_KEY, 0x40, { KEY_PREVIOUSSONG } },
255 {KE_KEY, 0x41, { KEY_NEXTSONG } },
256 {KE_KEY, 0x43, { KEY_STOPCD } },
257 {KE_KEY, 0x45, { KEY_PLAYPAUSE } },
258 {KE_KEY, 0x4c, { KEY_MEDIA } },
259 {KE_KEY, 0x50, { KEY_EMAIL } },
260 {KE_KEY, 0x51, { KEY_WWW } },
261 {KE_KEY, 0x55, { KEY_CALC } },
262 {KE_KEY, 0x5C, { KEY_SCREENLOCK } }, /* Screenlock */
263 {KE_KEY, 0x5D, { KEY_WLAN } },
264 {KE_KEY, 0x5E, { KEY_WLAN } },
265 {KE_KEY, 0x5F, { KEY_WLAN } },
266 {KE_KEY, 0x60, { KEY_SWITCHVIDEOMODE } },
267 {KE_KEY, 0x61, { KEY_SWITCHVIDEOMODE } },
268 {KE_KEY, 0x62, { KEY_SWITCHVIDEOMODE } },
269 {KE_KEY, 0x63, { KEY_SWITCHVIDEOMODE } },
270 {KE_KEY, 0x6B, { KEY_F13 } }, /* Lock Touchpad */
271 {KE_KEY, 0x7E, { KEY_BLUETOOTH } },
272 {KE_KEY, 0x7D, { KEY_BLUETOOTH } },
273 {KE_KEY, 0x82, { KEY_CAMERA } },
274 {KE_KEY, 0x88, { KEY_WLAN } },
275 {KE_KEY, 0x8A, { KEY_PROG1 } },
276 {KE_KEY, 0x95, { KEY_MEDIA } },
277 {KE_KEY, 0x99, { KEY_PHONE } },
278 {KE_KEY, 0xc4, { KEY_KBDILLUMUP } },
279 {KE_KEY, 0xc5, { KEY_KBDILLUMDOWN } },
280 {KE_KEY, 0xb5, { KEY_CALC } },
286 * This function evaluates an ACPI method, given an int as parameter, the
287 * method is searched within the scope of the handle, can be NULL. The output
288 * of the method is written is output, which can also be NULL
290 * returns 0 if write is successful, -1 else.
292 static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
293 struct acpi_buffer *output)
295 struct acpi_object_list params; /* list of input parameters (an int) */
296 union acpi_object in_obj; /* the only param we use */
303 params.pointer = &in_obj;
304 in_obj.type = ACPI_TYPE_INTEGER;
305 in_obj.integer.value = val;
307 status = acpi_evaluate_object(handle, (char *)method, ¶ms, output);
314 static int write_acpi_int(acpi_handle handle, const char *method, int val)
316 return write_acpi_int_ret(handle, method, val, NULL);
319 static int acpi_check_handle(acpi_handle handle, const char *method,
328 status = acpi_get_handle(handle, (char *)method,
333 status = acpi_get_handle(handle, (char *)method,
337 if (status != AE_OK) {
339 pr_warning("Error finding %s\n", method);
345 /* Generic LED function */
346 static int asus_led_set(struct asus_laptop *asus, const char *method,
349 if (!strcmp(method, METHOD_MLED))
351 else if (!strcmp(method, METHOD_GLED))
356 return write_acpi_int(asus->handle, method, value);
362 /* /sys/class/led handlers */
363 static void asus_led_cdev_set(struct led_classdev *led_cdev,
364 enum led_brightness value)
366 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
367 struct asus_laptop *asus = led->asus;
370 queue_work(asus->led_workqueue, &led->work);
373 static void asus_led_cdev_update(struct work_struct *work)
375 struct asus_led *led = container_of(work, struct asus_led, work);
376 struct asus_laptop *asus = led->asus;
378 asus_led_set(asus, led->method, led->wk);
381 static enum led_brightness asus_led_cdev_get(struct led_classdev *led_cdev)
383 return led_cdev->brightness;
387 * Keyboard backlight (also a LED)
389 static int asus_kled_lvl(struct asus_laptop *asus)
391 unsigned long long kblv;
392 struct acpi_object_list params;
393 union acpi_object in_obj;
397 params.pointer = &in_obj;
398 in_obj.type = ACPI_TYPE_INTEGER;
399 in_obj.integer.value = 2;
401 rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
403 if (ACPI_FAILURE(rv)) {
404 pr_warning("Error reading kled level\n");
410 static int asus_kled_set(struct asus_laptop *asus, int kblv)
413 kblv = (1 << 7) | (kblv & 0x7F);
417 if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
418 pr_warning("Keyboard LED display write failed\n");
424 static void asus_kled_cdev_set(struct led_classdev *led_cdev,
425 enum led_brightness value)
427 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
428 struct asus_laptop *asus = led->asus;
431 queue_work(asus->led_workqueue, &led->work);
434 static void asus_kled_cdev_update(struct work_struct *work)
436 struct asus_led *led = container_of(work, struct asus_led, work);
437 struct asus_laptop *asus = led->asus;
439 asus_kled_set(asus, led->wk);
442 static enum led_brightness asus_kled_cdev_get(struct led_classdev *led_cdev)
444 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
445 struct asus_laptop *asus = led->asus;
447 return asus_kled_lvl(asus);
450 static void asus_led_exit(struct asus_laptop *asus)
452 if (asus->mled.led.dev)
453 led_classdev_unregister(&asus->mled.led);
454 if (asus->tled.led.dev)
455 led_classdev_unregister(&asus->tled.led);
456 if (asus->pled.led.dev)
457 led_classdev_unregister(&asus->pled.led);
458 if (asus->rled.led.dev)
459 led_classdev_unregister(&asus->rled.led);
460 if (asus->gled.led.dev)
461 led_classdev_unregister(&asus->gled.led);
462 if (asus->kled.led.dev)
463 led_classdev_unregister(&asus->kled.led);
464 if (asus->led_workqueue) {
465 destroy_workqueue(asus->led_workqueue);
466 asus->led_workqueue = NULL;
470 /* Ugly macro, need to fix that later */
471 static int asus_led_register(struct asus_laptop *asus,
472 struct asus_led *led,
473 const char *name, const char *method)
475 struct led_classdev *led_cdev = &led->led;
477 if (!method || acpi_check_handle(asus->handle, method, NULL))
478 return 0; /* Led not present */
481 led->method = method;
483 INIT_WORK(&led->work, asus_led_cdev_update);
484 led_cdev->name = name;
485 led_cdev->brightness_set = asus_led_cdev_set;
486 led_cdev->brightness_get = asus_led_cdev_get;
487 led_cdev->max_brightness = 1;
488 return led_classdev_register(&asus->platform_device->dev, led_cdev);
491 static int asus_led_init(struct asus_laptop *asus)
496 * Functions that actually update the LED's are called from a
497 * workqueue. By doing this as separate work rather than when the LED
498 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
499 * potentially bad time, such as a timer interrupt.
501 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
502 if (!asus->led_workqueue)
505 r = asus_led_register(asus, &asus->mled, "asus::mail", METHOD_MLED);
508 r = asus_led_register(asus, &asus->tled, "asus::touchpad", METHOD_TLED);
511 r = asus_led_register(asus, &asus->rled, "asus::record", METHOD_RLED);
514 r = asus_led_register(asus, &asus->pled, "asus::phone", METHOD_PLED);
517 r = asus_led_register(asus, &asus->gled, "asus::gaming", METHOD_GLED);
520 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
521 !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL)) {
522 struct asus_led *led = &asus->kled;
523 struct led_classdev *cdev = &led->led;
527 INIT_WORK(&led->work, asus_kled_cdev_update);
528 cdev->name = "asus::kbd_backlight";
529 cdev->brightness_set = asus_kled_cdev_set;
530 cdev->brightness_get = asus_kled_cdev_get;
531 cdev->max_brightness = 3;
532 r = led_classdev_register(&asus->platform_device->dev, cdev);
543 static int asus_lcd_status(struct asus_laptop *asus)
545 return asus->lcd_state;
548 static int asus_lcd_set(struct asus_laptop *asus, int value)
551 acpi_status status = 0;
555 if (lcd == asus_lcd_status(asus))
558 if (!lcd_switch_handle)
561 status = acpi_evaluate_object(lcd_switch_handle,
564 if (ACPI_FAILURE(status)) {
565 pr_warning("Error switching LCD\n");
569 asus->lcd_state = lcd;
573 static void lcd_blank(struct asus_laptop *asus, int blank)
575 struct backlight_device *bd = asus->backlight_device;
577 asus->lcd_state = (blank == FB_BLANK_UNBLANK);
580 bd->props.power = blank;
581 backlight_update_status(bd);
585 static int asus_read_brightness(struct backlight_device *bd)
587 struct asus_laptop *asus = bl_get_data(bd);
588 unsigned long long value;
589 acpi_status rv = AE_OK;
591 rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
593 if (ACPI_FAILURE(rv))
594 pr_warning("Error reading brightness\n");
599 static int asus_set_brightness(struct backlight_device *bd, int value)
601 struct asus_laptop *asus = bl_get_data(bd);
603 if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
604 pr_warning("Error changing brightness\n");
610 static int update_bl_status(struct backlight_device *bd)
612 struct asus_laptop *asus = bl_get_data(bd);
614 int value = bd->props.brightness;
616 rv = asus_set_brightness(bd, value);
620 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
621 return asus_lcd_set(asus, value);
624 static struct backlight_ops asusbl_ops = {
625 .get_brightness = asus_read_brightness,
626 .update_status = update_bl_status,
629 static int asus_backlight_notify(struct asus_laptop *asus)
631 struct backlight_device *bd = asus->backlight_device;
632 int old = bd->props.brightness;
634 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
639 static int asus_backlight_init(struct asus_laptop *asus)
641 struct backlight_device *bd;
642 struct backlight_properties props;
644 if (acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) ||
645 acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL) ||
649 memset(&props, 0, sizeof(struct backlight_properties));
650 props.max_brightness = 15;
652 bd = backlight_device_register(ASUS_LAPTOP_FILE,
653 &asus->platform_device->dev, asus,
654 &asusbl_ops, &props);
656 pr_err("Could not register asus backlight device\n");
657 asus->backlight_device = NULL;
661 asus->backlight_device = bd;
662 bd->props.brightness = asus_read_brightness(bd);
663 bd->props.power = FB_BLANK_UNBLANK;
664 backlight_update_status(bd);
668 static void asus_backlight_exit(struct asus_laptop *asus)
670 if (asus->backlight_device)
671 backlight_device_unregister(asus->backlight_device);
672 asus->backlight_device = NULL;
676 * Platform device handlers
680 * We write our info in page, we begin at offset off and cannot write more
681 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
682 * number of bytes written in page
684 static ssize_t show_infos(struct device *dev,
685 struct device_attribute *attr, char *page)
687 struct asus_laptop *asus = dev_get_drvdata(dev);
689 unsigned long long temp;
690 char buf[16]; /* enough for all info */
691 acpi_status rv = AE_OK;
694 * We use the easy way, we don't care of off and count,
695 * so we don't set eof to 1
698 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
699 len += sprintf(page + len, "Model reference : %s\n", asus->name);
701 * The SFUN method probably allows the original driver to get the list
702 * of features supported by a given model. For now, 0x0100 or 0x0800
703 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
704 * The significance of others is yet to be found.
706 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
707 if (!ACPI_FAILURE(rv))
708 len += sprintf(page + len, "SFUN value : %#x\n",
711 * The HWRS method return informations about the hardware.
712 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
713 * The significance of others is yet to be found.
714 * If we don't find the method, we assume the device are present.
716 rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
717 if (!ACPI_FAILURE(rv))
718 len += sprintf(page + len, "HRWS value : %#x\n",
721 * Another value for userspace: the ASYM method returns 0x02 for
722 * battery low and 0x04 for battery critical, its readings tend to be
723 * more accurate than those provided by _BST.
724 * Note: since not all the laptops provide this method, errors are
727 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
728 if (!ACPI_FAILURE(rv))
729 len += sprintf(page + len, "ASYM value : %#x\n",
731 if (asus->dsdt_info) {
732 snprintf(buf, 16, "%d", asus->dsdt_info->length);
733 len += sprintf(page + len, "DSDT length : %s\n", buf);
734 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
735 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
736 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
737 len += sprintf(page + len, "DSDT revision : %s\n", buf);
738 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
739 len += sprintf(page + len, "OEM id : %s\n", buf);
740 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
741 len += sprintf(page + len, "OEM table id : %s\n", buf);
742 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
743 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
744 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
745 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
746 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
747 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
753 static int parse_arg(const char *buf, unsigned long count, int *val)
759 if (sscanf(buf, "%i", val) != 1)
764 static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
765 const char *buf, size_t count,
771 rv = parse_arg(buf, count, &value);
775 if (write_acpi_int(asus->handle, method, value))
783 static ssize_t show_ledd(struct device *dev,
784 struct device_attribute *attr, char *buf)
786 struct asus_laptop *asus = dev_get_drvdata(dev);
788 return sprintf(buf, "0x%08x\n", asus->ledd_status);
791 static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
792 const char *buf, size_t count)
794 struct asus_laptop *asus = dev_get_drvdata(dev);
797 rv = parse_arg(buf, count, &value);
799 if (write_acpi_int(asus->handle, METHOD_LEDD, value)) {
800 pr_warning("LED display write failed\n");
803 asus->ledd_status = (u32) value;
811 static int asus_wireless_status(struct asus_laptop *asus, int mask)
813 unsigned long long status;
814 acpi_status rv = AE_OK;
816 if (!asus->have_rsts)
817 return (asus->wireless_status & mask) ? 1 : 0;
819 rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
821 if (ACPI_FAILURE(rv)) {
822 pr_warning("Error reading Wireless status\n");
825 return !!(status & mask);
831 static int asus_wlan_set(struct asus_laptop *asus, int status)
833 if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
834 pr_warning("Error setting wlan status to %d", status);
840 static ssize_t show_wlan(struct device *dev,
841 struct device_attribute *attr, char *buf)
843 struct asus_laptop *asus = dev_get_drvdata(dev);
845 return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
848 static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
849 const char *buf, size_t count)
851 struct asus_laptop *asus = dev_get_drvdata(dev);
853 return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
859 static int asus_bluetooth_set(struct asus_laptop *asus, int status)
861 if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
862 pr_warning("Error setting bluetooth status to %d", status);
868 static ssize_t show_bluetooth(struct device *dev,
869 struct device_attribute *attr, char *buf)
871 struct asus_laptop *asus = dev_get_drvdata(dev);
873 return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
876 static ssize_t store_bluetooth(struct device *dev,
877 struct device_attribute *attr, const char *buf,
880 struct asus_laptop *asus = dev_get_drvdata(dev);
882 return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
888 static void asus_set_display(struct asus_laptop *asus, int value)
890 /* no sanity check needed for now */
891 if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
892 pr_warning("Error setting display\n");
896 static int read_display(struct asus_laptop *asus)
898 unsigned long long value = 0;
899 acpi_status rv = AE_OK;
902 * In most of the case, we know how to set the display, but sometime
905 if (display_get_handle) {
906 rv = acpi_evaluate_integer(display_get_handle, NULL,
908 if (ACPI_FAILURE(rv))
909 pr_warning("Error reading display status\n");
912 value &= 0x0F; /* needed for some models, shouldn't hurt others */
918 * Now, *this* one could be more user-friendly, but so far, no-one has
919 * complained. The significance of bits is the same as in store_disp()
921 static ssize_t show_disp(struct device *dev,
922 struct device_attribute *attr, char *buf)
924 struct asus_laptop *asus = dev_get_drvdata(dev);
926 if (!display_get_handle)
928 return sprintf(buf, "%d\n", read_display(asus));
932 * Experimental support for display switching. As of now: 1 should activate
933 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
934 * Any combination (bitwise) of these will suffice. I never actually tested 4
935 * displays hooked up simultaneously, so be warned. See the acpi4asus README
938 static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
939 const char *buf, size_t count)
941 struct asus_laptop *asus = dev_get_drvdata(dev);
944 rv = parse_arg(buf, count, &value);
946 asus_set_display(asus, value);
953 static void asus_als_switch(struct asus_laptop *asus, int value)
955 if (write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value))
956 pr_warning("Error setting light sensor switch\n");
957 asus->light_switch = value;
960 static ssize_t show_lssw(struct device *dev,
961 struct device_attribute *attr, char *buf)
963 struct asus_laptop *asus = dev_get_drvdata(dev);
965 return sprintf(buf, "%d\n", asus->light_switch);
968 static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
969 const char *buf, size_t count)
971 struct asus_laptop *asus = dev_get_drvdata(dev);
974 rv = parse_arg(buf, count, &value);
976 asus_als_switch(asus, value ? 1 : 0);
981 static void asus_als_level(struct asus_laptop *asus, int value)
983 if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
984 pr_warning("Error setting light sensor level\n");
985 asus->light_level = value;
988 static ssize_t show_lslvl(struct device *dev,
989 struct device_attribute *attr, char *buf)
991 struct asus_laptop *asus = dev_get_drvdata(dev);
993 return sprintf(buf, "%d\n", asus->light_level);
996 static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
997 const char *buf, size_t count)
999 struct asus_laptop *asus = dev_get_drvdata(dev);
1002 rv = parse_arg(buf, count, &value);
1004 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
1005 /* 0 <= value <= 15 */
1006 asus_als_level(asus, value);
1015 static int asus_gps_status(struct asus_laptop *asus)
1017 unsigned long long status;
1018 acpi_status rv = AE_OK;
1020 rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
1022 if (ACPI_FAILURE(rv)) {
1023 pr_warning("Error reading GPS status\n");
1029 static int asus_gps_switch(struct asus_laptop *asus, int status)
1031 const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
1033 if (write_acpi_int(asus->handle, meth, 0x02))
1038 static ssize_t show_gps(struct device *dev,
1039 struct device_attribute *attr, char *buf)
1041 struct asus_laptop *asus = dev_get_drvdata(dev);
1043 return sprintf(buf, "%d\n", asus_gps_status(asus));
1046 static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
1047 const char *buf, size_t count)
1049 struct asus_laptop *asus = dev_get_drvdata(dev);
1053 rv = parse_arg(buf, count, &value);
1056 ret = asus_gps_switch(asus, !!value);
1059 rfkill_set_sw_state(asus->gps_rfkill, !value);
1066 static int asus_gps_rfkill_set(void *data, bool blocked)
1068 struct asus_laptop *asus = data;
1070 return asus_gps_switch(asus, !blocked);
1073 static const struct rfkill_ops asus_gps_rfkill_ops = {
1074 .set_block = asus_gps_rfkill_set,
1077 static void asus_rfkill_exit(struct asus_laptop *asus)
1079 if (asus->gps_rfkill) {
1080 rfkill_unregister(asus->gps_rfkill);
1081 rfkill_destroy(asus->gps_rfkill);
1082 asus->gps_rfkill = NULL;
1086 static int asus_rfkill_init(struct asus_laptop *asus)
1090 if (acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) ||
1091 acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) ||
1092 acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
1095 asus->gps_rfkill = rfkill_alloc("asus-gps", &asus->platform_device->dev,
1097 &asus_gps_rfkill_ops, asus);
1098 if (!asus->gps_rfkill)
1101 result = rfkill_register(asus->gps_rfkill);
1103 rfkill_destroy(asus->gps_rfkill);
1104 asus->gps_rfkill = NULL;
1111 * Input device (i.e. hotkeys)
1113 static void asus_input_notify(struct asus_laptop *asus, int event)
1116 sparse_keymap_report_event(asus->inputdev, event, 1, true);
1119 static int asus_input_init(struct asus_laptop *asus)
1121 struct input_dev *input;
1124 input = input_allocate_device();
1126 pr_info("Unable to allocate input device\n");
1129 input->name = "Asus Laptop extra buttons";
1130 input->phys = ASUS_LAPTOP_FILE "/input0";
1131 input->id.bustype = BUS_HOST;
1132 input->dev.parent = &asus->platform_device->dev;
1134 error = sparse_keymap_setup(input, asus_keymap, NULL);
1136 pr_err("Unable to setup input device keymap\n");
1139 error = input_register_device(input);
1141 pr_info("Unable to register input device\n");
1142 goto err_free_keymap;
1145 asus->inputdev = input;
1149 sparse_keymap_free(input);
1151 input_free_device(input);
1155 static void asus_input_exit(struct asus_laptop *asus)
1157 if (asus->inputdev) {
1158 sparse_keymap_free(asus->inputdev);
1159 input_unregister_device(asus->inputdev);
1161 asus->inputdev = NULL;
1167 static void asus_acpi_notify(struct acpi_device *device, u32 event)
1169 struct asus_laptop *asus = acpi_driver_data(device);
1173 * We need to tell the backlight device when the backlight power is
1176 if (event == ATKD_LCD_ON)
1177 lcd_blank(asus, FB_BLANK_UNBLANK);
1178 else if (event == ATKD_LCD_OFF)
1179 lcd_blank(asus, FB_BLANK_POWERDOWN);
1181 /* TODO Find a better way to handle events count. */
1182 count = asus->event_count[event % 128]++;
1183 acpi_bus_generate_proc_event(asus->device, event, count);
1184 acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
1185 dev_name(&asus->device->dev), event,
1188 /* Brightness events are special */
1189 if (event >= ATKD_BR_MIN && event <= ATKD_BR_MAX) {
1191 /* Ignore them completely if the acpi video driver is used */
1192 if (asus->backlight_device != NULL) {
1193 /* Update the backlight device. */
1194 asus_backlight_notify(asus);
1198 asus_input_notify(asus, event);
1201 static DEVICE_ATTR(infos, S_IRUGO, show_infos, NULL);
1202 static DEVICE_ATTR(wlan, S_IRUGO | S_IWUSR, show_wlan, store_wlan);
1203 static DEVICE_ATTR(bluetooth, S_IRUGO | S_IWUSR,
1204 show_bluetooth, store_bluetooth);
1205 static DEVICE_ATTR(display, S_IRUGO | S_IWUSR, show_disp, store_disp);
1206 static DEVICE_ATTR(ledd, S_IRUGO | S_IWUSR, show_ledd, store_ledd);
1207 static DEVICE_ATTR(ls_level, S_IRUGO | S_IWUSR, show_lslvl, store_lslvl);
1208 static DEVICE_ATTR(ls_switch, S_IRUGO | S_IWUSR, show_lssw, store_lssw);
1209 static DEVICE_ATTR(gps, S_IRUGO | S_IWUSR, show_gps, store_gps);
1211 static struct attribute *asus_attributes[] = {
1212 &dev_attr_infos.attr,
1213 &dev_attr_wlan.attr,
1214 &dev_attr_bluetooth.attr,
1215 &dev_attr_display.attr,
1216 &dev_attr_ledd.attr,
1217 &dev_attr_ls_level.attr,
1218 &dev_attr_ls_switch.attr,
1223 static mode_t asus_sysfs_is_visible(struct kobject *kobj,
1224 struct attribute *attr,
1227 struct device *dev = container_of(kobj, struct device, kobj);
1228 struct platform_device *pdev = to_platform_device(dev);
1229 struct asus_laptop *asus = platform_get_drvdata(pdev);
1230 acpi_handle handle = asus->handle;
1233 if (attr == &dev_attr_wlan.attr) {
1234 supported = !acpi_check_handle(handle, METHOD_WLAN, NULL);
1236 } else if (attr == &dev_attr_bluetooth.attr) {
1237 supported = !acpi_check_handle(handle, METHOD_BLUETOOTH, NULL);
1239 } else if (attr == &dev_attr_display.attr) {
1240 supported = !acpi_check_handle(handle, METHOD_SWITCH_DISPLAY, NULL);
1242 } else if (attr == &dev_attr_ledd.attr) {
1243 supported = !acpi_check_handle(handle, METHOD_LEDD, NULL);
1245 } else if (attr == &dev_attr_ls_switch.attr ||
1246 attr == &dev_attr_ls_level.attr) {
1247 supported = !acpi_check_handle(handle, METHOD_ALS_CONTROL, NULL) &&
1248 !acpi_check_handle(handle, METHOD_ALS_LEVEL, NULL);
1250 } else if (attr == &dev_attr_gps.attr) {
1251 supported = !acpi_check_handle(handle, METHOD_GPS_ON, NULL) &&
1252 !acpi_check_handle(handle, METHOD_GPS_OFF, NULL) &&
1253 !acpi_check_handle(handle, METHOD_GPS_STATUS, NULL);
1258 return supported ? attr->mode : 0;
1262 static const struct attribute_group asus_attr_group = {
1263 .is_visible = asus_sysfs_is_visible,
1264 .attrs = asus_attributes,
1267 static int asus_platform_init(struct asus_laptop *asus)
1271 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1272 if (!asus->platform_device)
1274 platform_set_drvdata(asus->platform_device, asus);
1276 result = platform_device_add(asus->platform_device);
1278 goto fail_platform_device;
1280 result = sysfs_create_group(&asus->platform_device->dev.kobj,
1288 platform_device_del(asus->platform_device);
1289 fail_platform_device:
1290 platform_device_put(asus->platform_device);
1294 static void asus_platform_exit(struct asus_laptop *asus)
1296 sysfs_remove_group(&asus->platform_device->dev.kobj, &asus_attr_group);
1297 platform_device_unregister(asus->platform_device);
1300 static struct platform_driver platform_driver = {
1302 .name = ASUS_LAPTOP_FILE,
1303 .owner = THIS_MODULE,
1307 static int asus_handle_init(char *name, acpi_handle * handle,
1308 char **paths, int num_paths)
1313 for (i = 0; i < num_paths; i++) {
1314 status = acpi_get_handle(NULL, paths[i], handle);
1315 if (ACPI_SUCCESS(status))
1323 #define ASUS_HANDLE_INIT(object) \
1324 asus_handle_init(#object, &object##_handle, object##_paths, \
1325 ARRAY_SIZE(object##_paths))
1328 * This function is used to initialize the context with right values. In this
1329 * method, we can make all the detection we want, and modify the asus_laptop
1332 static int asus_laptop_get_info(struct asus_laptop *asus)
1334 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1335 union acpi_object *model = NULL;
1336 unsigned long long bsts_result, hwrs_result;
1337 char *string = NULL;
1341 * Get DSDT headers early enough to allow for differentiating between
1342 * models, but late enough to allow acpi_bus_register_driver() to fail
1343 * before doing anything ACPI-specific. Should we encounter a machine,
1344 * which needs special handling (i.e. its hotkey device has a different
1345 * HID), this bit will be moved.
1347 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
1348 if (ACPI_FAILURE(status))
1349 pr_warning("Couldn't get the DSDT table header\n");
1351 /* We have to write 0 on init this far for all ASUS models */
1352 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
1353 pr_err("Hotkey initialization failed\n");
1357 /* This needs to be called for some laptops to init properly */
1359 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
1360 if (ACPI_FAILURE(status))
1361 pr_warning("Error calling BSTS\n");
1362 else if (bsts_result)
1363 pr_notice("BSTS called, 0x%02x returned\n",
1364 (uint) bsts_result);
1367 if (write_acpi_int(asus->handle, "CWAP", wapf))
1368 pr_err("Error calling CWAP(%d)\n", wapf);
1370 * Try to match the object returned by INIT to the specific model.
1371 * Handle every possible object (or the lack of thereof) the DSDT
1372 * writers might throw at us. When in trouble, we pass NULL to
1373 * asus_model_match() and try something completely different.
1375 if (buffer.pointer) {
1376 model = buffer.pointer;
1377 switch (model->type) {
1378 case ACPI_TYPE_STRING:
1379 string = model->string.pointer;
1381 case ACPI_TYPE_BUFFER:
1382 string = model->buffer.pointer;
1389 asus->name = kstrdup(string, GFP_KERNEL);
1391 kfree(buffer.pointer);
1396 pr_notice(" %s model detected\n", string);
1399 * The HWRS method return informations about the hardware.
1400 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1401 * The significance of others is yet to be found.
1404 acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
1405 if (!ACPI_FAILURE(status))
1406 pr_notice(" HRWS returned %x", (int)hwrs_result);
1408 if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
1409 asus->have_rsts = true;
1411 /* Scheduled for removal */
1412 ASUS_HANDLE_INIT(lcd_switch);
1413 ASUS_HANDLE_INIT(display_get);
1420 static int __devinit asus_acpi_init(struct asus_laptop *asus)
1424 result = acpi_bus_get_status(asus->device);
1427 if (!asus->device->status.present) {
1428 pr_err("Hotkey device not present, aborting\n");
1432 result = asus_laptop_get_info(asus);
1436 /* WLED and BLED are on by default */
1437 if (bluetooth_status >= 0)
1438 asus_bluetooth_set(asus, !!bluetooth_status);
1440 if (wlan_status >= 0)
1441 asus_wlan_set(asus, !!wlan_status);
1443 /* Keyboard Backlight is on by default */
1444 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
1445 asus_kled_set(asus, 1);
1447 /* LED display is off by default */
1448 asus->ledd_status = 0xFFF;
1450 /* Set initial values of light sensor and level */
1451 asus->light_switch = 0; /* Default to light sensor disabled */
1452 asus->light_level = 5; /* level 5 for sensor sensitivity */
1454 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1455 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
1456 asus_als_switch(asus, asus->light_switch);
1457 asus_als_level(asus, asus->light_level);
1460 asus->lcd_state = 1; /* LCD should be on when the module load */
1464 static bool asus_device_present;
1466 static int __devinit asus_acpi_add(struct acpi_device *device)
1468 struct asus_laptop *asus;
1471 pr_notice("Asus Laptop Support version %s\n",
1472 ASUS_LAPTOP_VERSION);
1473 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1476 asus->handle = device->handle;
1477 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1478 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1479 device->driver_data = asus;
1480 asus->device = device;
1482 result = asus_acpi_init(asus);
1487 * Register the platform device first. It is used as a parent for the
1488 * sub-devices below.
1490 result = asus_platform_init(asus);
1494 if (!acpi_video_backlight_support()) {
1495 result = asus_backlight_init(asus);
1497 goto fail_backlight;
1499 pr_info("Backlight controlled by ACPI video driver\n");
1501 result = asus_input_init(asus);
1505 result = asus_led_init(asus);
1509 result = asus_rfkill_init(asus);
1513 asus_device_present = true;
1517 asus_led_exit(asus);
1519 asus_input_exit(asus);
1521 asus_backlight_exit(asus);
1523 asus_platform_exit(asus);
1531 static int asus_acpi_remove(struct acpi_device *device, int type)
1533 struct asus_laptop *asus = acpi_driver_data(device);
1535 asus_backlight_exit(asus);
1536 asus_rfkill_exit(asus);
1537 asus_led_exit(asus);
1538 asus_input_exit(asus);
1539 asus_platform_exit(asus);
1546 static const struct acpi_device_id asus_device_ids[] = {
1551 MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1553 static struct acpi_driver asus_acpi_driver = {
1554 .name = ASUS_LAPTOP_NAME,
1555 .class = ASUS_LAPTOP_CLASS,
1556 .owner = THIS_MODULE,
1557 .ids = asus_device_ids,
1558 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1560 .add = asus_acpi_add,
1561 .remove = asus_acpi_remove,
1562 .notify = asus_acpi_notify,
1566 static int __init asus_laptop_init(void)
1570 result = platform_driver_register(&platform_driver);
1574 result = acpi_bus_register_driver(&asus_acpi_driver);
1576 goto fail_acpi_driver;
1577 if (!asus_device_present) {
1579 goto fail_no_device;
1584 acpi_bus_unregister_driver(&asus_acpi_driver);
1586 platform_driver_unregister(&platform_driver);
1590 static void __exit asus_laptop_exit(void)
1592 acpi_bus_unregister_driver(&asus_acpi_driver);
1593 platform_driver_unregister(&platform_driver);
1596 module_init(asus_laptop_init);
1597 module_exit(asus_laptop_exit);