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 <acpi/acpi_drivers.h>
53 #include <acpi/acpi_bus.h>
55 #define ASUS_LAPTOP_VERSION "0.42"
57 #define ASUS_LAPTOP_NAME "Asus Laptop Support"
58 #define ASUS_LAPTOP_CLASS "hotkey"
59 #define ASUS_LAPTOP_DEVICE_NAME "Hotkey"
60 #define ASUS_LAPTOP_FILE KBUILD_MODNAME
61 #define ASUS_LAPTOP_PREFIX "\\_SB.ATKD."
63 MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
64 MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
65 MODULE_LICENSE("GPL");
68 * WAPF defines the behavior of the Fn+Fx wlan key
69 * The significance of values is yet to be found, but
72 * 0x1 will allow to control the device with Fn+Fx key.
73 * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
75 * So, if something doesn't work as you want, just try other values =)
78 module_param(wapf, uint, 0644);
79 MODULE_PARM_DESC(wapf, "WAPF value");
81 static uint wlan_status = 1;
82 static uint bluetooth_status = 1;
84 module_param(wlan_status, uint, 0644);
85 MODULE_PARM_DESC(wlan_status, "Set the wireless status on boot "
86 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
89 module_param(bluetooth_status, uint, 0644);
90 MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
91 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
95 * Some events we use, same for all Asus
97 #define ATKD_BR_UP 0x10 /* (event & ~ATKD_BR_UP) = brightness level */
98 #define ATKD_BR_DOWN 0x20 /* (event & ~ATKD_BR_DOWN) = britghness level */
99 #define ATKD_BR_MIN ATKD_BR_UP
100 #define ATKD_BR_MAX (ATKD_BR_DOWN | 0xF) /* 0x2f */
101 #define ATKD_LCD_ON 0x33
102 #define ATKD_LCD_OFF 0x34
105 * Known bits returned by \_SB.ATKD.HWRS
108 #define BT_HWRS 0x100
111 * Flags for hotk status
112 * WL_ON and BT_ON are also used for wireless_status()
114 #define WL_RSTS 0x01 /* internal Wifi */
115 #define BT_RSTS 0x02 /* internal Bluetooth */
118 #define METHOD_MLED "MLED"
119 #define METHOD_TLED "TLED"
120 #define METHOD_RLED "RLED" /* W1JC */
121 #define METHOD_PLED "PLED" /* A7J */
122 #define METHOD_GLED "GLED" /* G1, G2 (probably) */
125 #define METHOD_LEDD "SLCM"
129 * WLED and BLED are not handled like other XLED, because in some dsdt
130 * they also control the WLAN/Bluetooth device.
132 #define METHOD_WLAN "WLED"
133 #define METHOD_BLUETOOTH "BLED"
134 #define METHOD_WL_STATUS "RSTS"
137 #define METHOD_BRIGHTNESS_SET "SPLV"
138 #define METHOD_BRIGHTNESS_GET "GPLV"
141 static acpi_handle lcd_switch_handle;
142 static char *lcd_switch_paths[] = {
143 "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
144 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
145 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
146 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
147 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
148 "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
149 "\\_SB.PCI0.PX40.Q10", /* S1x */
150 "\\Q10"}; /* A2x, L2D, L3D, M2E */
153 #define METHOD_SWITCH_DISPLAY "SDSP"
155 static acpi_handle display_get_handle;
156 static char *display_get_paths[] = {
157 /* A6B, A6K A6R A7D F3JM L4R M6R A3G M6A M6V VX-1 V6J V6V W3Z */
158 "\\_SB.PCI0.P0P1.VGA.GETD",
159 /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V S5A M5A z33A W1Jc W2V G1 */
160 "\\_SB.PCI0.P0P2.VGA.GETD",
162 "\\_SB.PCI0.P0P3.VGA.GETD",
164 "\\_SB.PCI0.P0PA.VGA.GETD",
166 "\\_SB.PCI0.PCI1.VGAC.NMAP",
168 "\\_SB.PCI0.VGA.GETD",
171 /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
175 /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
177 /* A3F A6F A3N A3L M6N W3N W6A */
180 #define METHOD_ALS_CONTROL "ALSC" /* Z71A Z71V */
181 #define METHOD_ALS_LEVEL "ALSL" /* Z71A Z71V */
184 /* R2H use different handle for GPS on/off */
185 #define METHOD_GPS_ON "SDON"
186 #define METHOD_GPS_OFF "SDOF"
187 #define METHOD_GPS_STATUS "GPST"
190 #define METHOD_KBD_LIGHT_SET "SLKB"
191 #define METHOD_KBD_LIGHT_GET "GLKB"
194 * Define a specific led structure to keep the main structure clean
198 struct work_struct work;
199 struct led_classdev led;
200 struct asus_laptop *asus;
205 * This is the main structure, we can use it to store anything interesting
206 * about the hotk device
209 char *name; /* laptop name */
211 struct acpi_table_header *dsdt_info;
212 struct platform_device *platform_device;
213 struct acpi_device *device; /* the device we are in */
214 struct backlight_device *backlight_device;
216 struct input_dev *inputdev;
217 struct key_entry *keymap;
219 struct asus_led mled;
220 struct asus_led tled;
221 struct asus_led rled;
222 struct asus_led pled;
223 struct asus_led gled;
224 struct asus_led kled;
225 struct workqueue_struct *led_workqueue;
231 struct rfkill *gps_rfkill;
233 acpi_handle handle; /* the handle of the hotk device */
234 u32 ledd_status; /* status of the LED display */
235 u8 light_level; /* light sensor level */
236 u8 light_switch; /* light sensor switch value */
237 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 } },
285 * This function evaluates an ACPI method, given an int as parameter, the
286 * method is searched within the scope of the handle, can be NULL. The output
287 * of the method is written is output, which can also be NULL
289 * returns 0 if write is successful, -1 else.
291 static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
292 struct acpi_buffer *output)
294 struct acpi_object_list params; /* list of input parameters (an int) */
295 union acpi_object in_obj; /* the only param we use */
302 params.pointer = &in_obj;
303 in_obj.type = ACPI_TYPE_INTEGER;
304 in_obj.integer.value = val;
306 status = acpi_evaluate_object(handle, (char *)method, ¶ms, output);
313 static int write_acpi_int(acpi_handle handle, const char *method, int val)
315 return write_acpi_int_ret(handle, method, val, NULL);
318 static int acpi_check_handle(acpi_handle handle, const char *method,
327 status = acpi_get_handle(handle, (char *)method,
332 status = acpi_get_handle(handle, (char *)method,
336 if (status != AE_OK) {
338 pr_warning("Error finding %s\n", method);
344 /* Generic LED function */
345 static int asus_led_set(struct asus_laptop *asus, const char *method,
348 if (!strcmp(method, METHOD_MLED))
350 else if (!strcmp(method, METHOD_GLED))
355 return write_acpi_int(asus->handle, method, value);
361 /* /sys/class/led handlers */
362 static void asus_led_cdev_set(struct led_classdev *led_cdev,
363 enum led_brightness value)
365 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
366 struct asus_laptop *asus = led->asus;
369 queue_work(asus->led_workqueue, &led->work);
372 static void asus_led_cdev_update(struct work_struct *work)
374 struct asus_led *led = container_of(work, struct asus_led, work);
375 struct asus_laptop *asus = led->asus;
377 asus_led_set(asus, led->method, led->wk);
380 static enum led_brightness asus_led_cdev_get(struct led_classdev *led_cdev)
382 return led_cdev->brightness;
386 * Keyboard backlight (also a LED)
388 static int asus_kled_lvl(struct asus_laptop *asus)
390 unsigned long long kblv;
391 struct acpi_object_list params;
392 union acpi_object in_obj;
396 params.pointer = &in_obj;
397 in_obj.type = ACPI_TYPE_INTEGER;
398 in_obj.integer.value = 2;
400 rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
402 if (ACPI_FAILURE(rv)) {
403 pr_warning("Error reading kled level\n");
409 static int asus_kled_set(struct asus_laptop *asus, int kblv)
412 kblv = (1 << 7) | (kblv & 0x7F);
416 if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
417 pr_warning("Keyboard LED display write failed\n");
423 static void asus_kled_cdev_set(struct led_classdev *led_cdev,
424 enum led_brightness value)
426 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
427 struct asus_laptop *asus = led->asus;
430 queue_work(asus->led_workqueue, &led->work);
433 static void asus_kled_cdev_update(struct work_struct *work)
435 struct asus_led *led = container_of(work, struct asus_led, work);
436 struct asus_laptop *asus = led->asus;
438 asus_kled_set(asus, led->wk);
441 static enum led_brightness asus_kled_cdev_get(struct led_classdev *led_cdev)
443 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
444 struct asus_laptop *asus = led->asus;
446 return asus_kled_lvl(asus);
449 static void asus_led_exit(struct asus_laptop *asus)
451 if (asus->mled.led.dev)
452 led_classdev_unregister(&asus->mled.led);
453 if (asus->tled.led.dev)
454 led_classdev_unregister(&asus->tled.led);
455 if (asus->pled.led.dev)
456 led_classdev_unregister(&asus->pled.led);
457 if (asus->rled.led.dev)
458 led_classdev_unregister(&asus->rled.led);
459 if (asus->gled.led.dev)
460 led_classdev_unregister(&asus->gled.led);
461 if (asus->kled.led.dev)
462 led_classdev_unregister(&asus->kled.led);
463 if (asus->led_workqueue) {
464 destroy_workqueue(asus->led_workqueue);
465 asus->led_workqueue = NULL;
469 /* Ugly macro, need to fix that later */
470 static int asus_led_register(struct asus_laptop *asus,
471 struct asus_led *led,
472 const char *name, const char *method)
474 struct led_classdev *led_cdev = &led->led;
476 if (!method || acpi_check_handle(asus->handle, method, NULL))
477 return 0; /* Led not present */
480 led->method = method;
482 INIT_WORK(&led->work, asus_led_cdev_update);
483 led_cdev->name = name;
484 led_cdev->brightness_set = asus_led_cdev_set;
485 led_cdev->brightness_get = asus_led_cdev_get;
486 led_cdev->max_brightness = 1;
487 return led_classdev_register(&asus->platform_device->dev, led_cdev);
490 static int asus_led_init(struct asus_laptop *asus)
495 * Functions that actually update the LED's are called from a
496 * workqueue. By doing this as separate work rather than when the LED
497 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
498 * potentially bad time, such as a timer interrupt.
500 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
501 if (!asus->led_workqueue)
504 r = asus_led_register(asus, &asus->mled, "asus::mail", METHOD_MLED);
507 r = asus_led_register(asus, &asus->tled, "asus::touchpad", METHOD_TLED);
510 r = asus_led_register(asus, &asus->rled, "asus::record", METHOD_RLED);
513 r = asus_led_register(asus, &asus->pled, "asus::phone", METHOD_PLED);
516 r = asus_led_register(asus, &asus->gled, "asus::gaming", METHOD_GLED);
519 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
520 !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL)) {
521 struct asus_led *led = &asus->kled;
522 struct led_classdev *cdev = &led->led;
526 INIT_WORK(&led->work, asus_kled_cdev_update);
527 cdev->name = "asus::kbd_backlight";
528 cdev->brightness_set = asus_kled_cdev_set;
529 cdev->brightness_get = asus_kled_cdev_get;
530 cdev->max_brightness = 3;
531 r = led_classdev_register(&asus->platform_device->dev, cdev);
542 static int asus_lcd_status(struct asus_laptop *asus)
544 return asus->lcd_state;
547 static int asus_lcd_set(struct asus_laptop *asus, int value)
550 acpi_status status = 0;
554 if (lcd == asus_lcd_status(asus))
557 if (!lcd_switch_handle)
560 status = acpi_evaluate_object(lcd_switch_handle,
563 if (ACPI_FAILURE(status)) {
564 pr_warning("Error switching LCD\n");
568 asus->lcd_state = lcd;
572 static void lcd_blank(struct asus_laptop *asus, int blank)
574 struct backlight_device *bd = asus->backlight_device;
576 asus->lcd_state = (blank == FB_BLANK_UNBLANK);
579 bd->props.power = blank;
580 backlight_update_status(bd);
584 static int asus_read_brightness(struct backlight_device *bd)
586 struct asus_laptop *asus = bl_get_data(bd);
587 unsigned long long value;
588 acpi_status rv = AE_OK;
590 rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
592 if (ACPI_FAILURE(rv))
593 pr_warning("Error reading brightness\n");
598 static int asus_set_brightness(struct backlight_device *bd, int value)
600 struct asus_laptop *asus = bl_get_data(bd);
602 if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
603 pr_warning("Error changing brightness\n");
609 static int update_bl_status(struct backlight_device *bd)
611 struct asus_laptop *asus = bl_get_data(bd);
613 int value = bd->props.brightness;
615 rv = asus_set_brightness(bd, value);
619 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
620 return asus_lcd_set(asus, value);
623 static struct backlight_ops asusbl_ops = {
624 .get_brightness = asus_read_brightness,
625 .update_status = update_bl_status,
628 static int asus_backlight_notify(struct asus_laptop *asus)
630 struct backlight_device *bd = asus->backlight_device;
631 int old = bd->props.brightness;
633 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
638 static int asus_backlight_init(struct asus_laptop *asus)
640 struct backlight_device *bd;
641 struct device *dev = &asus->platform_device->dev;
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) &&
647 memset(&props, 0, sizeof(struct backlight_properties));
648 props.max_brightness = 15;
650 bd = backlight_device_register(ASUS_LAPTOP_FILE, dev,
651 asus, &asusbl_ops, &props);
653 pr_err("Could not register asus backlight device\n");
654 asus->backlight_device = NULL;
658 asus->backlight_device = bd;
660 bd->props.power = FB_BLANK_UNBLANK;
661 bd->props.brightness = asus_read_brightness(bd);
662 backlight_update_status(bd);
667 static void asus_backlight_exit(struct asus_laptop *asus)
669 if (asus->backlight_device)
670 backlight_device_unregister(asus->backlight_device);
671 asus->backlight_device = NULL;
675 * Platform device handlers
679 * We write our info in page, we begin at offset off and cannot write more
680 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
681 * number of bytes written in page
683 static ssize_t show_infos(struct device *dev,
684 struct device_attribute *attr, char *page)
686 struct asus_laptop *asus = dev_get_drvdata(dev);
688 unsigned long long temp;
689 char buf[16]; /* enough for all info */
690 acpi_status rv = AE_OK;
693 * We use the easy way, we don't care of off and count,
694 * so we don't set eof to 1
697 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
698 len += sprintf(page + len, "Model reference : %s\n", asus->name);
700 * The SFUN method probably allows the original driver to get the list
701 * of features supported by a given model. For now, 0x0100 or 0x0800
702 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
703 * The significance of others is yet to be found.
705 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
706 if (!ACPI_FAILURE(rv))
707 len += sprintf(page + len, "SFUN value : %#x\n",
710 * The HWRS method return informations about the hardware.
711 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
712 * The significance of others is yet to be found.
713 * If we don't find the method, we assume the device are present.
715 rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
716 if (!ACPI_FAILURE(rv))
717 len += sprintf(page + len, "HRWS value : %#x\n",
720 * Another value for userspace: the ASYM method returns 0x02 for
721 * battery low and 0x04 for battery critical, its readings tend to be
722 * more accurate than those provided by _BST.
723 * Note: since not all the laptops provide this method, errors are
726 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
727 if (!ACPI_FAILURE(rv))
728 len += sprintf(page + len, "ASYM value : %#x\n",
730 if (asus->dsdt_info) {
731 snprintf(buf, 16, "%d", asus->dsdt_info->length);
732 len += sprintf(page + len, "DSDT length : %s\n", buf);
733 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
734 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
735 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
736 len += sprintf(page + len, "DSDT revision : %s\n", buf);
737 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
738 len += sprintf(page + len, "OEM id : %s\n", buf);
739 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
740 len += sprintf(page + len, "OEM table id : %s\n", buf);
741 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
742 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
743 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
744 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
745 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
746 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
752 static int parse_arg(const char *buf, unsigned long count, int *val)
758 if (sscanf(buf, "%i", val) != 1)
763 static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
764 const char *buf, size_t count,
770 rv = parse_arg(buf, count, &value);
774 if (write_acpi_int(asus->handle, method, value))
782 static ssize_t show_ledd(struct device *dev,
783 struct device_attribute *attr, char *buf)
785 struct asus_laptop *asus = dev_get_drvdata(dev);
787 return sprintf(buf, "0x%08x\n", asus->ledd_status);
790 static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
791 const char *buf, size_t count)
793 struct asus_laptop *asus = dev_get_drvdata(dev);
796 rv = parse_arg(buf, count, &value);
798 if (write_acpi_int(asus->handle, METHOD_LEDD, value))
799 pr_warning("LED display write failed\n");
801 asus->ledd_status = (u32) value;
809 static int asus_wireless_status(struct asus_laptop *asus, int mask)
811 unsigned long long status;
812 acpi_status rv = AE_OK;
814 if (!asus->have_rsts)
815 return (asus->wireless_status & mask) ? 1 : 0;
817 rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
819 if (ACPI_FAILURE(rv)) {
820 pr_warning("Error reading Wireless status\n");
823 return !!(status & mask);
829 static int asus_wlan_set(struct asus_laptop *asus, int status)
831 if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
832 pr_warning("Error setting wlan status to %d", status);
838 static ssize_t show_wlan(struct device *dev,
839 struct device_attribute *attr, char *buf)
841 struct asus_laptop *asus = dev_get_drvdata(dev);
843 return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
846 static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
847 const char *buf, size_t count)
849 struct asus_laptop *asus = dev_get_drvdata(dev);
851 return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
857 static int asus_bluetooth_set(struct asus_laptop *asus, int status)
859 if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
860 pr_warning("Error setting bluetooth status to %d", status);
866 static ssize_t show_bluetooth(struct device *dev,
867 struct device_attribute *attr, char *buf)
869 struct asus_laptop *asus = dev_get_drvdata(dev);
871 return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
874 static ssize_t store_bluetooth(struct device *dev,
875 struct device_attribute *attr, const char *buf,
878 struct asus_laptop *asus = dev_get_drvdata(dev);
880 return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
886 static void asus_set_display(struct asus_laptop *asus, int value)
888 /* no sanity check needed for now */
889 if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
890 pr_warning("Error setting display\n");
894 static int read_display(struct asus_laptop *asus)
896 unsigned long long value = 0;
897 acpi_status rv = AE_OK;
900 * In most of the case, we know how to set the display, but sometime
903 if (display_get_handle) {
904 rv = acpi_evaluate_integer(display_get_handle, NULL,
906 if (ACPI_FAILURE(rv))
907 pr_warning("Error reading display status\n");
910 value &= 0x0F; /* needed for some models, shouldn't hurt others */
916 * Now, *this* one could be more user-friendly, but so far, no-one has
917 * complained. The significance of bits is the same as in store_disp()
919 static ssize_t show_disp(struct device *dev,
920 struct device_attribute *attr, char *buf)
922 struct asus_laptop *asus = dev_get_drvdata(dev);
924 if (!display_get_handle)
926 return sprintf(buf, "%d\n", read_display(asus));
930 * Experimental support for display switching. As of now: 1 should activate
931 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
932 * Any combination (bitwise) of these will suffice. I never actually tested 4
933 * displays hooked up simultaneously, so be warned. See the acpi4asus README
936 static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
937 const char *buf, size_t count)
939 struct asus_laptop *asus = dev_get_drvdata(dev);
942 rv = parse_arg(buf, count, &value);
944 asus_set_display(asus, value);
951 static void asus_als_switch(struct asus_laptop *asus, int value)
953 if (write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value))
954 pr_warning("Error setting light sensor switch\n");
955 asus->light_switch = value;
958 static ssize_t show_lssw(struct device *dev,
959 struct device_attribute *attr, char *buf)
961 struct asus_laptop *asus = dev_get_drvdata(dev);
963 return sprintf(buf, "%d\n", asus->light_switch);
966 static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
967 const char *buf, size_t count)
969 struct asus_laptop *asus = dev_get_drvdata(dev);
972 rv = parse_arg(buf, count, &value);
974 asus_als_switch(asus, value ? 1 : 0);
979 static void asus_als_level(struct asus_laptop *asus, int value)
981 if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
982 pr_warning("Error setting light sensor level\n");
983 asus->light_level = value;
986 static ssize_t show_lslvl(struct device *dev,
987 struct device_attribute *attr, char *buf)
989 struct asus_laptop *asus = dev_get_drvdata(dev);
991 return sprintf(buf, "%d\n", asus->light_level);
994 static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
995 const char *buf, size_t count)
997 struct asus_laptop *asus = dev_get_drvdata(dev);
1000 rv = parse_arg(buf, count, &value);
1002 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
1003 /* 0 <= value <= 15 */
1004 asus_als_level(asus, value);
1013 static int asus_gps_status(struct asus_laptop *asus)
1015 unsigned long long status;
1016 acpi_status rv = AE_OK;
1018 rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
1020 if (ACPI_FAILURE(rv)) {
1021 pr_warning("Error reading GPS status\n");
1027 static int asus_gps_switch(struct asus_laptop *asus, int status)
1029 const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
1031 if (write_acpi_int(asus->handle, meth, 0x02))
1036 static ssize_t show_gps(struct device *dev,
1037 struct device_attribute *attr, char *buf)
1039 struct asus_laptop *asus = dev_get_drvdata(dev);
1041 return sprintf(buf, "%d\n", asus_gps_status(asus));
1044 static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
1045 const char *buf, size_t count)
1047 struct asus_laptop *asus = dev_get_drvdata(dev);
1051 rv = parse_arg(buf, count, &value);
1054 ret = asus_gps_switch(asus, !!value);
1057 rfkill_set_sw_state(asus->gps_rfkill, !value);
1064 static int asus_gps_rfkill_set(void *data, bool blocked)
1066 acpi_handle handle = data;
1068 return asus_gps_switch(handle, !blocked);
1071 static const struct rfkill_ops asus_gps_rfkill_ops = {
1072 .set_block = asus_gps_rfkill_set,
1075 static void asus_rfkill_exit(struct asus_laptop *asus)
1077 if (asus->gps_rfkill) {
1078 rfkill_unregister(asus->gps_rfkill);
1079 rfkill_destroy(asus->gps_rfkill);
1080 asus->gps_rfkill = NULL;
1084 static int asus_rfkill_init(struct asus_laptop *asus)
1088 if (acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) ||
1089 acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) ||
1090 acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
1093 asus->gps_rfkill = rfkill_alloc("asus-gps", &asus->platform_device->dev,
1095 &asus_gps_rfkill_ops, NULL);
1096 if (!asus->gps_rfkill)
1099 result = rfkill_register(asus->gps_rfkill);
1101 rfkill_destroy(asus->gps_rfkill);
1102 asus->gps_rfkill = NULL;
1109 * Input device (i.e. hotkeys)
1111 static void asus_input_notify(struct asus_laptop *asus, int event)
1114 sparse_keymap_report_event(asus->inputdev, event, 1, true);
1117 static int asus_input_init(struct asus_laptop *asus)
1119 struct input_dev *input;
1122 input = input_allocate_device();
1124 pr_info("Unable to allocate input device\n");
1127 input->name = "Asus Laptop extra buttons";
1128 input->phys = ASUS_LAPTOP_FILE "/input0";
1129 input->id.bustype = BUS_HOST;
1130 input->dev.parent = &asus->platform_device->dev;
1131 input_set_drvdata(input, asus);
1133 error = sparse_keymap_setup(input, asus_keymap, NULL);
1135 pr_err("Unable to setup input device keymap\n");
1138 error = input_register_device(input);
1140 pr_info("Unable to register input device\n");
1144 asus->inputdev = input;
1148 sparse_keymap_free(input);
1150 input_free_device(input);
1154 static void asus_input_exit(struct asus_laptop *asus)
1156 if (asus->inputdev) {
1157 sparse_keymap_free(asus->inputdev);
1158 input_unregister_device(asus->inputdev);
1165 static void asus_acpi_notify(struct acpi_device *device, u32 event)
1167 struct asus_laptop *asus = acpi_driver_data(device);
1171 * We need to tell the backlight device when the backlight power is
1174 if (event == ATKD_LCD_ON)
1175 lcd_blank(asus, FB_BLANK_UNBLANK);
1176 else if (event == ATKD_LCD_OFF)
1177 lcd_blank(asus, FB_BLANK_POWERDOWN);
1179 /* TODO Find a better way to handle events count. */
1180 count = asus->event_count[event % 128]++;
1181 acpi_bus_generate_proc_event(asus->device, event, count);
1182 acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
1183 dev_name(&asus->device->dev), event,
1186 /* Brightness events are special */
1187 if (event >= ATKD_BR_MIN && event <= ATKD_BR_MAX) {
1189 /* Ignore them completely if the acpi video driver is used */
1190 if (asus->backlight_device != NULL) {
1191 /* Update the backlight device. */
1192 asus_backlight_notify(asus);
1196 asus_input_notify(asus, event);
1199 static DEVICE_ATTR(infos, S_IRUGO, show_infos, NULL);
1200 static DEVICE_ATTR(wlan, S_IRUGO | S_IWUSR, show_wlan, store_wlan);
1201 static DEVICE_ATTR(bluetooth, S_IRUGO | S_IWUSR, show_bluetooth,
1203 static DEVICE_ATTR(display, S_IRUGO | S_IWUSR, show_disp, store_disp);
1204 static DEVICE_ATTR(ledd, S_IRUGO | S_IWUSR, show_ledd, store_ledd);
1205 static DEVICE_ATTR(ls_level, S_IRUGO | S_IWUSR, show_lslvl, store_lslvl);
1206 static DEVICE_ATTR(ls_switch, S_IRUGO | S_IWUSR, show_lssw, store_lssw);
1207 static DEVICE_ATTR(gps, S_IRUGO | S_IWUSR, show_gps, store_gps);
1209 static void asus_sysfs_exit(struct asus_laptop *asus)
1211 struct platform_device *device = asus->platform_device;
1213 device_remove_file(&device->dev, &dev_attr_infos);
1214 device_remove_file(&device->dev, &dev_attr_wlan);
1215 device_remove_file(&device->dev, &dev_attr_bluetooth);
1216 device_remove_file(&device->dev, &dev_attr_display);
1217 device_remove_file(&device->dev, &dev_attr_ledd);
1218 device_remove_file(&device->dev, &dev_attr_ls_switch);
1219 device_remove_file(&device->dev, &dev_attr_ls_level);
1220 device_remove_file(&device->dev, &dev_attr_gps);
1223 static int asus_sysfs_init(struct asus_laptop *asus)
1225 struct platform_device *device = asus->platform_device;
1228 err = device_create_file(&device->dev, &dev_attr_infos);
1232 if (!acpi_check_handle(asus->handle, METHOD_WLAN, NULL)) {
1233 err = device_create_file(&device->dev, &dev_attr_wlan);
1238 if (!acpi_check_handle(asus->handle, METHOD_BLUETOOTH, NULL)) {
1239 err = device_create_file(&device->dev, &dev_attr_bluetooth);
1244 if (!acpi_check_handle(asus->handle, METHOD_SWITCH_DISPLAY, NULL)) {
1245 err = device_create_file(&device->dev, &dev_attr_display);
1250 if (!acpi_check_handle(asus->handle, METHOD_LEDD, NULL)) {
1251 err = device_create_file(&device->dev, &dev_attr_ledd);
1256 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1257 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
1258 err = device_create_file(&device->dev, &dev_attr_ls_switch);
1261 err = device_create_file(&device->dev, &dev_attr_ls_level);
1266 if (!acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) &&
1267 !acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) &&
1268 !acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL)) {
1269 err = device_create_file(&device->dev, &dev_attr_gps);
1277 static int asus_platform_init(struct asus_laptop *asus)
1281 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1282 if (!asus->platform_device)
1284 platform_set_drvdata(asus->platform_device, asus);
1286 err = platform_device_add(asus->platform_device);
1288 goto fail_platform_device;
1290 err = asus_sysfs_init(asus);
1296 asus_sysfs_exit(asus);
1297 platform_device_del(asus->platform_device);
1298 fail_platform_device:
1299 platform_device_put(asus->platform_device);
1303 static void asus_platform_exit(struct asus_laptop *asus)
1305 asus_sysfs_exit(asus);
1306 platform_device_unregister(asus->platform_device);
1309 static struct platform_driver platform_driver = {
1311 .name = ASUS_LAPTOP_FILE,
1312 .owner = THIS_MODULE,
1316 static int asus_handle_init(char *name, acpi_handle * handle,
1317 char **paths, int num_paths)
1322 for (i = 0; i < num_paths; i++) {
1323 status = acpi_get_handle(NULL, paths[i], handle);
1324 if (ACPI_SUCCESS(status))
1332 #define ASUS_HANDLE_INIT(object) \
1333 asus_handle_init(#object, &object##_handle, object##_paths, \
1334 ARRAY_SIZE(object##_paths))
1337 * This function is used to initialize the context with right values. In this
1338 * method, we can make all the detection we want, and modify the asus_laptop
1341 static int asus_laptop_get_info(struct asus_laptop *asus)
1343 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1344 union acpi_object *model = NULL;
1345 unsigned long long bsts_result, hwrs_result;
1346 char *string = NULL;
1350 * Get DSDT headers early enough to allow for differentiating between
1351 * models, but late enough to allow acpi_bus_register_driver() to fail
1352 * before doing anything ACPI-specific. Should we encounter a machine,
1353 * which needs special handling (i.e. its hotkey device has a different
1354 * HID), this bit will be moved.
1356 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
1357 if (ACPI_FAILURE(status))
1358 pr_warning("Couldn't get the DSDT table header\n");
1360 /* We have to write 0 on init this far for all ASUS models */
1361 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
1362 pr_err("Hotkey initialization failed\n");
1366 /* This needs to be called for some laptops to init properly */
1368 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
1369 if (ACPI_FAILURE(status))
1370 pr_warning("Error calling BSTS\n");
1371 else if (bsts_result)
1372 pr_notice("BSTS called, 0x%02x returned\n",
1373 (uint) bsts_result);
1376 if (write_acpi_int(asus->handle, "CWAP", wapf))
1377 pr_err("Error calling CWAP(%d)\n", wapf);
1379 * Try to match the object returned by INIT to the specific model.
1380 * Handle every possible object (or the lack of thereof) the DSDT
1381 * writers might throw at us. When in trouble, we pass NULL to
1382 * asus_model_match() and try something completely different.
1384 if (buffer.pointer) {
1385 model = buffer.pointer;
1386 switch (model->type) {
1387 case ACPI_TYPE_STRING:
1388 string = model->string.pointer;
1390 case ACPI_TYPE_BUFFER:
1391 string = model->buffer.pointer;
1398 asus->name = kstrdup(string, GFP_KERNEL);
1403 pr_notice(" %s model detected\n", string);
1406 * The HWRS method return informations about the hardware.
1407 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1408 * The significance of others is yet to be found.
1411 acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
1412 if (!ACPI_FAILURE(status))
1413 pr_notice(" HRWS returned %x", (int)hwrs_result);
1415 if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
1416 asus->have_rsts = true;
1418 /* Scheduled for removal */
1419 ASUS_HANDLE_INIT(lcd_switch);
1420 ASUS_HANDLE_INIT(display_get);
1427 static bool asus_device_present;
1429 static int __devinit asus_acpi_init(struct asus_laptop *asus)
1433 result = acpi_bus_get_status(asus->device);
1436 if (!asus->device->status.present) {
1437 pr_err("Hotkey device not present, aborting\n");
1441 result = asus_laptop_get_info(asus);
1445 /* WLED and BLED are on by default */
1446 if (bluetooth_status >= 0)
1447 asus_bluetooth_set(asus, !!bluetooth_status);
1449 if (wlan_status >= 0)
1450 asus_wlan_set(asus, !!wlan_status);
1452 /* Keyboard Backlight is on by default */
1453 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
1454 asus_kled_set(asus, 1);
1456 /* LED display is off by default */
1457 asus->ledd_status = 0xFFF;
1459 /* Set initial values of light sensor and level */
1460 asus->light_switch = 0; /* Default to light sensor disabled */
1461 asus->light_level = 5; /* level 5 for sensor sensitivity */
1463 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1464 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
1465 asus_als_switch(asus, asus->light_switch);
1466 asus_als_level(asus, asus->light_level);
1469 asus->lcd_state = 1; /* LCD should be on when the module load */
1473 static int __devinit asus_acpi_add(struct acpi_device *device)
1475 struct asus_laptop *asus;
1478 pr_notice("Asus Laptop Support version %s\n",
1479 ASUS_LAPTOP_VERSION);
1480 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1483 asus->handle = device->handle;
1484 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1485 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1486 device->driver_data = asus;
1487 asus->device = device;
1489 result = asus_acpi_init(asus);
1494 * Register the platform device first. It is used as a parent for the
1495 * sub-devices below.
1497 result = asus_platform_init(asus);
1501 if (!acpi_video_backlight_support()) {
1502 result = asus_backlight_init(asus);
1504 goto fail_backlight;
1506 pr_info("Backlight controlled by ACPI video driver\n");
1508 result = asus_input_init(asus);
1512 result = asus_led_init(asus);
1516 result = asus_rfkill_init(asus);
1520 asus_device_present = true;
1524 asus_led_exit(asus);
1526 asus_input_exit(asus);
1528 asus_backlight_exit(asus);
1530 asus_platform_exit(asus);
1538 static int asus_acpi_remove(struct acpi_device *device, int type)
1540 struct asus_laptop *asus = acpi_driver_data(device);
1542 asus_backlight_exit(asus);
1543 asus_rfkill_exit(asus);
1544 asus_led_exit(asus);
1545 asus_input_exit(asus);
1546 asus_platform_exit(asus);
1553 static const struct acpi_device_id asus_device_ids[] = {
1558 MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1560 static struct acpi_driver asus_acpi_driver = {
1561 .name = ASUS_LAPTOP_NAME,
1562 .class = ASUS_LAPTOP_CLASS,
1563 .owner = THIS_MODULE,
1564 .ids = asus_device_ids,
1565 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1567 .add = asus_acpi_add,
1568 .remove = asus_acpi_remove,
1569 .notify = asus_acpi_notify,
1573 static int __init asus_laptop_init(void)
1577 result = platform_driver_register(&platform_driver);
1581 result = acpi_bus_register_driver(&asus_acpi_driver);
1583 goto fail_acpi_driver;
1584 if (!asus_device_present) {
1586 goto fail_no_device;
1591 acpi_bus_unregister_driver(&asus_acpi_driver);
1593 platform_driver_unregister(&platform_driver);
1597 static void __exit asus_laptop_exit(void)
1599 acpi_bus_unregister_driver(&asus_acpi_driver);
1600 platform_driver_unregister(&platform_driver);
1603 module_init(asus_laptop_init);
1604 module_exit(asus_laptop_exit);