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
7 * Copyright (C) 2011 Wind River Systems
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * The development page for this driver is located at
25 * http://sourceforge.net/projects/acpi4asus/
28 * Pontus Fuchs - Helper functions, cleanup
29 * Johann Wiesner - Small compile fixes
30 * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
31 * Eric Burghard - LED display support for W1N
32 * Josh Green - Light Sens support
33 * Thomas Tuttle - His first patch for led support was very helpful
34 * Sam Lin - GPS support
37 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/types.h>
43 #include <linux/err.h>
44 #include <linux/proc_fs.h>
45 #include <linux/backlight.h>
47 #include <linux/leds.h>
48 #include <linux/platform_device.h>
49 #include <linux/uaccess.h>
50 #include <linux/input.h>
51 #include <linux/input/sparse-keymap.h>
52 #include <linux/input-polldev.h>
53 #include <linux/rfkill.h>
54 #include <linux/slab.h>
55 #include <linux/dmi.h>
56 #include <acpi/acpi_drivers.h>
57 #include <acpi/acpi_bus.h>
59 #define ASUS_LAPTOP_VERSION "0.42"
61 #define ASUS_LAPTOP_NAME "Asus Laptop Support"
62 #define ASUS_LAPTOP_CLASS "hotkey"
63 #define ASUS_LAPTOP_DEVICE_NAME "Hotkey"
64 #define ASUS_LAPTOP_FILE KBUILD_MODNAME
65 #define ASUS_LAPTOP_PREFIX "\\_SB.ATKD."
67 MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
68 MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
69 MODULE_LICENSE("GPL");
72 * WAPF defines the behavior of the Fn+Fx wlan key
73 * The significance of values is yet to be found, but
75 * Bit | Bluetooth | WLAN
76 * 0 | Hardware | Hardware
77 * 1 | Hardware | Software
78 * 4 | Software | Software
81 module_param(wapf, uint, 0444);
82 MODULE_PARM_DESC(wapf, "WAPF value");
84 static int wlan_status = 1;
85 static int bluetooth_status = 1;
86 static int wimax_status = -1;
87 static int wwan_status = -1;
89 module_param(wlan_status, int, 0444);
90 MODULE_PARM_DESC(wlan_status, "Set the wireless status on boot "
91 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
94 module_param(bluetooth_status, int, 0444);
95 MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
96 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
99 module_param(wimax_status, int, 0444);
100 MODULE_PARM_DESC(wimax_status, "Set the wireless status on boot "
101 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
104 module_param(wwan_status, int, 0444);
105 MODULE_PARM_DESC(wwan_status, "Set the wireless status on boot "
106 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
110 * Some events we use, same for all Asus
112 #define ATKD_BR_UP 0x10 /* (event & ~ATKD_BR_UP) = brightness level */
113 #define ATKD_BR_DOWN 0x20 /* (event & ~ATKD_BR_DOWN) = britghness level */
114 #define ATKD_BR_MIN ATKD_BR_UP
115 #define ATKD_BR_MAX (ATKD_BR_DOWN | 0xF) /* 0x2f */
116 #define ATKD_LCD_ON 0x33
117 #define ATKD_LCD_OFF 0x34
120 * Known bits returned by \_SB.ATKD.HWRS
123 #define BT_HWRS 0x100
126 * Flags for hotk status
127 * WL_ON and BT_ON are also used for wireless_status()
129 #define WL_RSTS 0x01 /* internal Wifi */
130 #define BT_RSTS 0x02 /* internal Bluetooth */
131 #define WM_RSTS 0x08 /* internal wimax */
132 #define WW_RSTS 0x20 /* internal wwan */
135 #define METHOD_MLED "MLED"
136 #define METHOD_TLED "TLED"
137 #define METHOD_RLED "RLED" /* W1JC */
138 #define METHOD_PLED "PLED" /* A7J */
139 #define METHOD_GLED "GLED" /* G1, G2 (probably) */
142 #define METHOD_LEDD "SLCM"
146 * WLED and BLED are not handled like other XLED, because in some dsdt
147 * they also control the WLAN/Bluetooth device.
149 #define METHOD_WLAN "WLED"
150 #define METHOD_BLUETOOTH "BLED"
153 #define METHOD_WWAN "GSMC"
154 #define METHOD_WIMAX "WMXC"
156 #define METHOD_WL_STATUS "RSTS"
159 #define METHOD_BRIGHTNESS_SET "SPLV"
160 #define METHOD_BRIGHTNESS_GET "GPLV"
163 #define METHOD_SWITCH_DISPLAY "SDSP"
165 #define METHOD_ALS_CONTROL "ALSC" /* Z71A Z71V */
166 #define METHOD_ALS_LEVEL "ALSL" /* Z71A Z71V */
169 /* R2H use different handle for GPS on/off */
170 #define METHOD_GPS_ON "SDON"
171 #define METHOD_GPS_OFF "SDOF"
172 #define METHOD_GPS_STATUS "GPST"
175 #define METHOD_KBD_LIGHT_SET "SLKB"
176 #define METHOD_KBD_LIGHT_GET "GLKB"
178 /* For Pegatron Lucid tablet */
179 #define DEVICE_NAME_PEGA "Lucid"
181 #define METHOD_PEGA_ENABLE "ENPR"
182 #define METHOD_PEGA_DISABLE "DAPR"
183 #define PEGA_ALS 0x04
184 #define PEGA_ALS_POWER 0x05
186 #define METHOD_PEGA_READ "RDLN"
187 #define PEGA_READ_ALS_H 0x02
188 #define PEGA_READ_ALS_L 0x03
191 * Define a specific led structure to keep the main structure clean
195 struct work_struct work;
196 struct led_classdev led;
197 struct asus_laptop *asus;
202 * This is the main structure, we can use it to store anything interesting
203 * about the hotk device
206 char *name; /* laptop name */
208 struct acpi_table_header *dsdt_info;
209 struct platform_device *platform_device;
210 struct acpi_device *device; /* the device we are in */
211 struct backlight_device *backlight_device;
213 struct input_dev *inputdev;
214 struct key_entry *keymap;
216 struct asus_led mled;
217 struct asus_led tled;
218 struct asus_led rled;
219 struct asus_led pled;
220 struct asus_led gled;
221 struct asus_led kled;
222 struct workqueue_struct *led_workqueue;
228 struct rfkill *gps_rfkill;
230 acpi_handle handle; /* the handle of the hotk device */
231 u32 ledd_status; /* status of the LED display */
232 u8 light_level; /* light sensor level */
233 u8 light_switch; /* light sensor switch value */
234 u16 event_count[128]; /* count for each event TODO make this better */
237 static const struct key_entry asus_keymap[] = {
238 /* Lenovo SL Specific keycodes */
239 {KE_KEY, 0x02, { KEY_SCREENLOCK } },
240 {KE_KEY, 0x05, { KEY_WLAN } },
241 {KE_KEY, 0x08, { KEY_F13 } },
242 {KE_KEY, 0x17, { KEY_ZOOM } },
243 {KE_KEY, 0x1f, { KEY_BATTERY } },
244 /* End of Lenovo SL Specific keycodes */
245 {KE_KEY, 0x30, { KEY_VOLUMEUP } },
246 {KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
247 {KE_KEY, 0x32, { KEY_MUTE } },
248 {KE_KEY, 0x33, { KEY_SWITCHVIDEOMODE } },
249 {KE_KEY, 0x34, { KEY_SWITCHVIDEOMODE } },
250 {KE_KEY, 0x40, { KEY_PREVIOUSSONG } },
251 {KE_KEY, 0x41, { KEY_NEXTSONG } },
252 {KE_KEY, 0x43, { KEY_STOPCD } },
253 {KE_KEY, 0x45, { KEY_PLAYPAUSE } },
254 {KE_KEY, 0x4c, { KEY_MEDIA } },
255 {KE_KEY, 0x50, { KEY_EMAIL } },
256 {KE_KEY, 0x51, { KEY_WWW } },
257 {KE_KEY, 0x55, { KEY_CALC } },
258 {KE_KEY, 0x5C, { KEY_SCREENLOCK } }, /* Screenlock */
259 {KE_KEY, 0x5D, { KEY_WLAN } },
260 {KE_KEY, 0x5E, { KEY_WLAN } },
261 {KE_KEY, 0x5F, { KEY_WLAN } },
262 {KE_KEY, 0x60, { KEY_SWITCHVIDEOMODE } },
263 {KE_KEY, 0x61, { KEY_SWITCHVIDEOMODE } },
264 {KE_KEY, 0x62, { KEY_SWITCHVIDEOMODE } },
265 {KE_KEY, 0x63, { KEY_SWITCHVIDEOMODE } },
266 {KE_KEY, 0x6B, { KEY_F13 } }, /* Lock Touchpad */
267 {KE_KEY, 0x7E, { KEY_BLUETOOTH } },
268 {KE_KEY, 0x7D, { KEY_BLUETOOTH } },
269 {KE_KEY, 0x82, { KEY_CAMERA } },
270 {KE_KEY, 0x88, { KEY_WLAN } },
271 {KE_KEY, 0x8A, { KEY_PROG1 } },
272 {KE_KEY, 0x95, { KEY_MEDIA } },
273 {KE_KEY, 0x99, { KEY_PHONE } },
274 {KE_KEY, 0xc4, { KEY_KBDILLUMUP } },
275 {KE_KEY, 0xc5, { KEY_KBDILLUMDOWN } },
276 {KE_KEY, 0xb5, { KEY_CALC } },
282 * This function evaluates an ACPI method, given an int as parameter, the
283 * method is searched within the scope of the handle, can be NULL. The output
284 * of the method is written is output, which can also be NULL
286 * returns 0 if write is successful, -1 else.
288 static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
289 struct acpi_buffer *output)
291 struct acpi_object_list params; /* list of input parameters (an int) */
292 union acpi_object in_obj; /* the only param we use */
299 params.pointer = &in_obj;
300 in_obj.type = ACPI_TYPE_INTEGER;
301 in_obj.integer.value = val;
303 status = acpi_evaluate_object(handle, (char *)method, ¶ms, output);
310 static int write_acpi_int(acpi_handle handle, const char *method, int val)
312 return write_acpi_int_ret(handle, method, val, NULL);
315 static int acpi_check_handle(acpi_handle handle, const char *method,
324 status = acpi_get_handle(handle, (char *)method,
329 status = acpi_get_handle(handle, (char *)method,
333 if (status != AE_OK) {
335 pr_warn("Error finding %s\n", method);
341 static bool asus_check_pega_lucid(struct asus_laptop *asus)
343 return !strcmp(asus->name, DEVICE_NAME_PEGA) &&
344 !acpi_check_handle(asus->handle, METHOD_PEGA_ENABLE, NULL) &&
345 !acpi_check_handle(asus->handle, METHOD_PEGA_DISABLE, NULL) &&
346 !acpi_check_handle(asus->handle, METHOD_PEGA_READ, NULL);
349 static int asus_pega_lucid_set(struct asus_laptop *asus, int unit, bool enable)
351 char *method = enable ? METHOD_PEGA_ENABLE : METHOD_PEGA_DISABLE;
352 return write_acpi_int(asus->handle, method, unit);
355 /* Generic LED function */
356 static int asus_led_set(struct asus_laptop *asus, const char *method,
359 if (!strcmp(method, METHOD_MLED))
361 else if (!strcmp(method, METHOD_GLED))
366 return write_acpi_int(asus->handle, method, value);
372 /* /sys/class/led handlers */
373 static void asus_led_cdev_set(struct led_classdev *led_cdev,
374 enum led_brightness value)
376 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
377 struct asus_laptop *asus = led->asus;
380 queue_work(asus->led_workqueue, &led->work);
383 static void asus_led_cdev_update(struct work_struct *work)
385 struct asus_led *led = container_of(work, struct asus_led, work);
386 struct asus_laptop *asus = led->asus;
388 asus_led_set(asus, led->method, led->wk);
391 static enum led_brightness asus_led_cdev_get(struct led_classdev *led_cdev)
393 return led_cdev->brightness;
397 * Keyboard backlight (also a LED)
399 static int asus_kled_lvl(struct asus_laptop *asus)
401 unsigned long long kblv;
402 struct acpi_object_list params;
403 union acpi_object in_obj;
407 params.pointer = &in_obj;
408 in_obj.type = ACPI_TYPE_INTEGER;
409 in_obj.integer.value = 2;
411 rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
413 if (ACPI_FAILURE(rv)) {
414 pr_warn("Error reading kled level\n");
420 static int asus_kled_set(struct asus_laptop *asus, int kblv)
423 kblv = (1 << 7) | (kblv & 0x7F);
427 if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
428 pr_warn("Keyboard LED display write failed\n");
434 static void asus_kled_cdev_set(struct led_classdev *led_cdev,
435 enum led_brightness value)
437 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
438 struct asus_laptop *asus = led->asus;
441 queue_work(asus->led_workqueue, &led->work);
444 static void asus_kled_cdev_update(struct work_struct *work)
446 struct asus_led *led = container_of(work, struct asus_led, work);
447 struct asus_laptop *asus = led->asus;
449 asus_kled_set(asus, led->wk);
452 static enum led_brightness asus_kled_cdev_get(struct led_classdev *led_cdev)
454 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
455 struct asus_laptop *asus = led->asus;
457 return asus_kled_lvl(asus);
460 static void asus_led_exit(struct asus_laptop *asus)
462 if (!IS_ERR_OR_NULL(asus->mled.led.dev))
463 led_classdev_unregister(&asus->mled.led);
464 if (!IS_ERR_OR_NULL(asus->tled.led.dev))
465 led_classdev_unregister(&asus->tled.led);
466 if (!IS_ERR_OR_NULL(asus->pled.led.dev))
467 led_classdev_unregister(&asus->pled.led);
468 if (!IS_ERR_OR_NULL(asus->rled.led.dev))
469 led_classdev_unregister(&asus->rled.led);
470 if (!IS_ERR_OR_NULL(asus->gled.led.dev))
471 led_classdev_unregister(&asus->gled.led);
472 if (!IS_ERR_OR_NULL(asus->kled.led.dev))
473 led_classdev_unregister(&asus->kled.led);
474 if (asus->led_workqueue) {
475 destroy_workqueue(asus->led_workqueue);
476 asus->led_workqueue = NULL;
480 /* Ugly macro, need to fix that later */
481 static int asus_led_register(struct asus_laptop *asus,
482 struct asus_led *led,
483 const char *name, const char *method)
485 struct led_classdev *led_cdev = &led->led;
487 if (!method || acpi_check_handle(asus->handle, method, NULL))
488 return 0; /* Led not present */
491 led->method = method;
493 INIT_WORK(&led->work, asus_led_cdev_update);
494 led_cdev->name = name;
495 led_cdev->brightness_set = asus_led_cdev_set;
496 led_cdev->brightness_get = asus_led_cdev_get;
497 led_cdev->max_brightness = 1;
498 return led_classdev_register(&asus->platform_device->dev, led_cdev);
501 static int asus_led_init(struct asus_laptop *asus)
506 * Functions that actually update the LED's are called from a
507 * workqueue. By doing this as separate work rather than when the LED
508 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
509 * potentially bad time, such as a timer interrupt.
511 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
512 if (!asus->led_workqueue)
515 r = asus_led_register(asus, &asus->mled, "asus::mail", METHOD_MLED);
518 r = asus_led_register(asus, &asus->tled, "asus::touchpad", METHOD_TLED);
521 r = asus_led_register(asus, &asus->rled, "asus::record", METHOD_RLED);
524 r = asus_led_register(asus, &asus->pled, "asus::phone", METHOD_PLED);
527 r = asus_led_register(asus, &asus->gled, "asus::gaming", METHOD_GLED);
530 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
531 !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL)) {
532 struct asus_led *led = &asus->kled;
533 struct led_classdev *cdev = &led->led;
537 INIT_WORK(&led->work, asus_kled_cdev_update);
538 cdev->name = "asus::kbd_backlight";
539 cdev->brightness_set = asus_kled_cdev_set;
540 cdev->brightness_get = asus_kled_cdev_get;
541 cdev->max_brightness = 3;
542 r = led_classdev_register(&asus->platform_device->dev, cdev);
553 static int asus_read_brightness(struct backlight_device *bd)
555 struct asus_laptop *asus = bl_get_data(bd);
556 unsigned long long value;
557 acpi_status rv = AE_OK;
559 rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
561 if (ACPI_FAILURE(rv))
562 pr_warn("Error reading brightness\n");
567 static int asus_set_brightness(struct backlight_device *bd, int value)
569 struct asus_laptop *asus = bl_get_data(bd);
571 if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
572 pr_warn("Error changing brightness\n");
578 static int update_bl_status(struct backlight_device *bd)
580 int value = bd->props.brightness;
582 return asus_set_brightness(bd, value);
585 static const struct backlight_ops asusbl_ops = {
586 .get_brightness = asus_read_brightness,
587 .update_status = update_bl_status,
590 static int asus_backlight_notify(struct asus_laptop *asus)
592 struct backlight_device *bd = asus->backlight_device;
593 int old = bd->props.brightness;
595 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
600 static int asus_backlight_init(struct asus_laptop *asus)
602 struct backlight_device *bd;
603 struct backlight_properties props;
605 if (acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) ||
606 acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL))
609 memset(&props, 0, sizeof(struct backlight_properties));
610 props.max_brightness = 15;
611 props.type = BACKLIGHT_PLATFORM;
613 bd = backlight_device_register(ASUS_LAPTOP_FILE,
614 &asus->platform_device->dev, asus,
615 &asusbl_ops, &props);
617 pr_err("Could not register asus backlight device\n");
618 asus->backlight_device = NULL;
622 asus->backlight_device = bd;
623 bd->props.brightness = asus_read_brightness(bd);
624 bd->props.power = FB_BLANK_UNBLANK;
625 backlight_update_status(bd);
629 static void asus_backlight_exit(struct asus_laptop *asus)
631 if (asus->backlight_device)
632 backlight_device_unregister(asus->backlight_device);
633 asus->backlight_device = NULL;
637 * Platform device handlers
641 * We write our info in page, we begin at offset off and cannot write more
642 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
643 * number of bytes written in page
645 static ssize_t show_infos(struct device *dev,
646 struct device_attribute *attr, char *page)
648 struct asus_laptop *asus = dev_get_drvdata(dev);
650 unsigned long long temp;
651 char buf[16]; /* enough for all info */
652 acpi_status rv = AE_OK;
655 * We use the easy way, we don't care of off and count,
656 * so we don't set eof to 1
659 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
660 len += sprintf(page + len, "Model reference : %s\n", asus->name);
662 * The SFUN method probably allows the original driver to get the list
663 * of features supported by a given model. For now, 0x0100 or 0x0800
664 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
665 * The significance of others is yet to be found.
667 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
668 if (!ACPI_FAILURE(rv))
669 len += sprintf(page + len, "SFUN value : %#x\n",
672 * The HWRS method return informations about the hardware.
673 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
674 * The significance of others is yet to be found.
675 * If we don't find the method, we assume the device are present.
677 rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
678 if (!ACPI_FAILURE(rv))
679 len += sprintf(page + len, "HRWS value : %#x\n",
682 * Another value for userspace: the ASYM method returns 0x02 for
683 * battery low and 0x04 for battery critical, its readings tend to be
684 * more accurate than those provided by _BST.
685 * Note: since not all the laptops provide this method, errors are
688 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
689 if (!ACPI_FAILURE(rv))
690 len += sprintf(page + len, "ASYM value : %#x\n",
692 if (asus->dsdt_info) {
693 snprintf(buf, 16, "%d", asus->dsdt_info->length);
694 len += sprintf(page + len, "DSDT length : %s\n", buf);
695 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
696 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
697 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
698 len += sprintf(page + len, "DSDT revision : %s\n", buf);
699 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
700 len += sprintf(page + len, "OEM id : %s\n", buf);
701 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
702 len += sprintf(page + len, "OEM table id : %s\n", buf);
703 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
704 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
705 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
706 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
707 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
708 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
714 static int parse_arg(const char *buf, unsigned long count, int *val)
720 if (sscanf(buf, "%i", val) != 1)
725 static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
726 const char *buf, size_t count,
732 rv = parse_arg(buf, count, &value);
736 if (write_acpi_int(asus->handle, method, value))
744 static ssize_t show_ledd(struct device *dev,
745 struct device_attribute *attr, char *buf)
747 struct asus_laptop *asus = dev_get_drvdata(dev);
749 return sprintf(buf, "0x%08x\n", asus->ledd_status);
752 static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
753 const char *buf, size_t count)
755 struct asus_laptop *asus = dev_get_drvdata(dev);
758 rv = parse_arg(buf, count, &value);
760 if (write_acpi_int(asus->handle, METHOD_LEDD, value)) {
761 pr_warn("LED display write failed\n");
764 asus->ledd_status = (u32) value;
772 static int asus_wireless_status(struct asus_laptop *asus, int mask)
774 unsigned long long status;
775 acpi_status rv = AE_OK;
777 if (!asus->have_rsts)
778 return (asus->wireless_status & mask) ? 1 : 0;
780 rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
782 if (ACPI_FAILURE(rv)) {
783 pr_warn("Error reading Wireless status\n");
786 return !!(status & mask);
792 static int asus_wlan_set(struct asus_laptop *asus, int status)
794 if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
795 pr_warn("Error setting wlan status to %d\n", status);
801 static ssize_t show_wlan(struct device *dev,
802 struct device_attribute *attr, char *buf)
804 struct asus_laptop *asus = dev_get_drvdata(dev);
806 return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
809 static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
810 const char *buf, size_t count)
812 struct asus_laptop *asus = dev_get_drvdata(dev);
814 return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
820 static int asus_bluetooth_set(struct asus_laptop *asus, int status)
822 if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
823 pr_warn("Error setting bluetooth status to %d\n", status);
829 static ssize_t show_bluetooth(struct device *dev,
830 struct device_attribute *attr, char *buf)
832 struct asus_laptop *asus = dev_get_drvdata(dev);
834 return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
837 static ssize_t store_bluetooth(struct device *dev,
838 struct device_attribute *attr, const char *buf,
841 struct asus_laptop *asus = dev_get_drvdata(dev);
843 return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
849 static int asus_wimax_set(struct asus_laptop *asus, int status)
851 if (write_acpi_int(asus->handle, METHOD_WIMAX, !!status)) {
852 pr_warn("Error setting wimax status to %d\n", status);
858 static ssize_t show_wimax(struct device *dev,
859 struct device_attribute *attr, char *buf)
861 struct asus_laptop *asus = dev_get_drvdata(dev);
863 return sprintf(buf, "%d\n", asus_wireless_status(asus, WM_RSTS));
866 static ssize_t store_wimax(struct device *dev,
867 struct device_attribute *attr, const char *buf,
870 struct asus_laptop *asus = dev_get_drvdata(dev);
872 return sysfs_acpi_set(asus, buf, count, METHOD_WIMAX);
878 static int asus_wwan_set(struct asus_laptop *asus, int status)
880 if (write_acpi_int(asus->handle, METHOD_WWAN, !!status)) {
881 pr_warn("Error setting wwan status to %d\n", status);
887 static ssize_t show_wwan(struct device *dev,
888 struct device_attribute *attr, char *buf)
890 struct asus_laptop *asus = dev_get_drvdata(dev);
892 return sprintf(buf, "%d\n", asus_wireless_status(asus, WW_RSTS));
895 static ssize_t store_wwan(struct device *dev,
896 struct device_attribute *attr, const char *buf,
899 struct asus_laptop *asus = dev_get_drvdata(dev);
901 return sysfs_acpi_set(asus, buf, count, METHOD_WWAN);
907 static void asus_set_display(struct asus_laptop *asus, int value)
909 /* no sanity check needed for now */
910 if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
911 pr_warn("Error setting display\n");
916 * Experimental support for display switching. As of now: 1 should activate
917 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
918 * Any combination (bitwise) of these will suffice. I never actually tested 4
919 * displays hooked up simultaneously, so be warned. See the acpi4asus README
922 static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
923 const char *buf, size_t count)
925 struct asus_laptop *asus = dev_get_drvdata(dev);
928 rv = parse_arg(buf, count, &value);
930 asus_set_display(asus, value);
937 static void asus_als_switch(struct asus_laptop *asus, int value)
941 if (asus->is_pega_lucid) {
942 ret = asus_pega_lucid_set(asus, PEGA_ALS, value);
944 ret = asus_pega_lucid_set(asus, PEGA_ALS_POWER, value);
946 ret = write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value);
949 pr_warning("Error setting light sensor switch\n");
951 asus->light_switch = value;
954 static ssize_t show_lssw(struct device *dev,
955 struct device_attribute *attr, char *buf)
957 struct asus_laptop *asus = dev_get_drvdata(dev);
959 return sprintf(buf, "%d\n", asus->light_switch);
962 static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
963 const char *buf, size_t count)
965 struct asus_laptop *asus = dev_get_drvdata(dev);
968 rv = parse_arg(buf, count, &value);
970 asus_als_switch(asus, value ? 1 : 0);
975 static void asus_als_level(struct asus_laptop *asus, int value)
977 if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
978 pr_warn("Error setting light sensor level\n");
979 asus->light_level = value;
982 static ssize_t show_lslvl(struct device *dev,
983 struct device_attribute *attr, char *buf)
985 struct asus_laptop *asus = dev_get_drvdata(dev);
987 return sprintf(buf, "%d\n", asus->light_level);
990 static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
991 const char *buf, size_t count)
993 struct asus_laptop *asus = dev_get_drvdata(dev);
996 rv = parse_arg(buf, count, &value);
998 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
999 /* 0 <= value <= 15 */
1000 asus_als_level(asus, value);
1006 static int pega_int_read(struct asus_laptop *asus, int arg, int *result)
1008 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1009 int err = write_acpi_int_ret(asus->handle, METHOD_PEGA_READ, arg,
1012 union acpi_object *obj = buffer.pointer;
1013 if (obj && obj->type == ACPI_TYPE_INTEGER)
1014 *result = obj->integer.value;
1021 static ssize_t show_lsvalue(struct device *dev,
1022 struct device_attribute *attr, char *buf)
1024 struct asus_laptop *asus = dev_get_drvdata(dev);
1027 err = pega_int_read(asus, PEGA_READ_ALS_H, &hi);
1029 err = pega_int_read(asus, PEGA_READ_ALS_L, &lo);
1031 return sprintf(buf, "%d\n", 10 * hi + lo);
1038 static int asus_gps_status(struct asus_laptop *asus)
1040 unsigned long long status;
1041 acpi_status rv = AE_OK;
1043 rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
1045 if (ACPI_FAILURE(rv)) {
1046 pr_warn("Error reading GPS status\n");
1052 static int asus_gps_switch(struct asus_laptop *asus, int status)
1054 const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
1056 if (write_acpi_int(asus->handle, meth, 0x02))
1061 static ssize_t show_gps(struct device *dev,
1062 struct device_attribute *attr, char *buf)
1064 struct asus_laptop *asus = dev_get_drvdata(dev);
1066 return sprintf(buf, "%d\n", asus_gps_status(asus));
1069 static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
1070 const char *buf, size_t count)
1072 struct asus_laptop *asus = dev_get_drvdata(dev);
1076 rv = parse_arg(buf, count, &value);
1079 ret = asus_gps_switch(asus, !!value);
1082 rfkill_set_sw_state(asus->gps_rfkill, !value);
1089 static int asus_gps_rfkill_set(void *data, bool blocked)
1091 struct asus_laptop *asus = data;
1093 return asus_gps_switch(asus, !blocked);
1096 static const struct rfkill_ops asus_gps_rfkill_ops = {
1097 .set_block = asus_gps_rfkill_set,
1100 static void asus_rfkill_exit(struct asus_laptop *asus)
1102 if (asus->gps_rfkill) {
1103 rfkill_unregister(asus->gps_rfkill);
1104 rfkill_destroy(asus->gps_rfkill);
1105 asus->gps_rfkill = NULL;
1109 static int asus_rfkill_init(struct asus_laptop *asus)
1113 if (acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) ||
1114 acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) ||
1115 acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
1118 asus->gps_rfkill = rfkill_alloc("asus-gps", &asus->platform_device->dev,
1120 &asus_gps_rfkill_ops, asus);
1121 if (!asus->gps_rfkill)
1124 result = rfkill_register(asus->gps_rfkill);
1126 rfkill_destroy(asus->gps_rfkill);
1127 asus->gps_rfkill = NULL;
1134 * Input device (i.e. hotkeys)
1136 static void asus_input_notify(struct asus_laptop *asus, int event)
1139 sparse_keymap_report_event(asus->inputdev, event, 1, true);
1142 static int asus_input_init(struct asus_laptop *asus)
1144 struct input_dev *input;
1147 input = input_allocate_device();
1149 pr_info("Unable to allocate input device\n");
1152 input->name = "Asus Laptop extra buttons";
1153 input->phys = ASUS_LAPTOP_FILE "/input0";
1154 input->id.bustype = BUS_HOST;
1155 input->dev.parent = &asus->platform_device->dev;
1157 error = sparse_keymap_setup(input, asus_keymap, NULL);
1159 pr_err("Unable to setup input device keymap\n");
1162 error = input_register_device(input);
1164 pr_info("Unable to register input device\n");
1165 goto err_free_keymap;
1168 asus->inputdev = input;
1172 sparse_keymap_free(input);
1174 input_free_device(input);
1178 static void asus_input_exit(struct asus_laptop *asus)
1180 if (asus->inputdev) {
1181 sparse_keymap_free(asus->inputdev);
1182 input_unregister_device(asus->inputdev);
1184 asus->inputdev = NULL;
1190 static void asus_acpi_notify(struct acpi_device *device, u32 event)
1192 struct asus_laptop *asus = acpi_driver_data(device);
1195 /* TODO Find a better way to handle events count. */
1196 count = asus->event_count[event % 128]++;
1197 acpi_bus_generate_proc_event(asus->device, event, count);
1198 acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
1199 dev_name(&asus->device->dev), event,
1202 /* Brightness events are special */
1203 if (event >= ATKD_BR_MIN && event <= ATKD_BR_MAX) {
1205 /* Ignore them completely if the acpi video driver is used */
1206 if (asus->backlight_device != NULL) {
1207 /* Update the backlight device. */
1208 asus_backlight_notify(asus);
1212 asus_input_notify(asus, event);
1215 static DEVICE_ATTR(infos, S_IRUGO, show_infos, NULL);
1216 static DEVICE_ATTR(wlan, S_IRUGO | S_IWUSR, show_wlan, store_wlan);
1217 static DEVICE_ATTR(bluetooth, S_IRUGO | S_IWUSR,
1218 show_bluetooth, store_bluetooth);
1219 static DEVICE_ATTR(wimax, S_IRUGO | S_IWUSR, show_wimax, store_wimax);
1220 static DEVICE_ATTR(wwan, S_IRUGO | S_IWUSR, show_wwan, store_wwan);
1221 static DEVICE_ATTR(display, S_IWUSR, NULL, store_disp);
1222 static DEVICE_ATTR(ledd, S_IRUGO | S_IWUSR, show_ledd, store_ledd);
1223 static DEVICE_ATTR(ls_value, S_IRUGO, show_lsvalue, NULL);
1224 static DEVICE_ATTR(ls_level, S_IRUGO | S_IWUSR, show_lslvl, store_lslvl);
1225 static DEVICE_ATTR(ls_switch, S_IRUGO | S_IWUSR, show_lssw, store_lssw);
1226 static DEVICE_ATTR(gps, S_IRUGO | S_IWUSR, show_gps, store_gps);
1228 static struct attribute *asus_attributes[] = {
1229 &dev_attr_infos.attr,
1230 &dev_attr_wlan.attr,
1231 &dev_attr_bluetooth.attr,
1232 &dev_attr_wimax.attr,
1233 &dev_attr_wwan.attr,
1234 &dev_attr_display.attr,
1235 &dev_attr_ledd.attr,
1236 &dev_attr_ls_value.attr,
1237 &dev_attr_ls_level.attr,
1238 &dev_attr_ls_switch.attr,
1243 static mode_t asus_sysfs_is_visible(struct kobject *kobj,
1244 struct attribute *attr,
1247 struct device *dev = container_of(kobj, struct device, kobj);
1248 struct platform_device *pdev = to_platform_device(dev);
1249 struct asus_laptop *asus = platform_get_drvdata(pdev);
1250 acpi_handle handle = asus->handle;
1253 if (asus->is_pega_lucid) {
1254 /* no ls_level interface on the Lucid */
1255 if (attr == &dev_attr_ls_switch.attr)
1257 else if (attr == &dev_attr_ls_level.attr)
1266 if (attr == &dev_attr_wlan.attr) {
1267 supported = !acpi_check_handle(handle, METHOD_WLAN, NULL);
1269 } else if (attr == &dev_attr_bluetooth.attr) {
1270 supported = !acpi_check_handle(handle, METHOD_BLUETOOTH, NULL);
1272 } else if (attr == &dev_attr_display.attr) {
1273 supported = !acpi_check_handle(handle, METHOD_SWITCH_DISPLAY, NULL);
1275 } else if (attr == &dev_attr_wimax.attr) {
1277 !acpi_check_handle(asus->handle, METHOD_WIMAX, NULL);
1279 } else if (attr == &dev_attr_wwan.attr) {
1280 supported = !acpi_check_handle(asus->handle, METHOD_WWAN, NULL);
1282 } else if (attr == &dev_attr_ledd.attr) {
1283 supported = !acpi_check_handle(handle, METHOD_LEDD, NULL);
1285 } else if (attr == &dev_attr_ls_switch.attr ||
1286 attr == &dev_attr_ls_level.attr) {
1287 supported = !acpi_check_handle(handle, METHOD_ALS_CONTROL, NULL) &&
1288 !acpi_check_handle(handle, METHOD_ALS_LEVEL, NULL);
1289 } else if (attr == &dev_attr_ls_value.attr) {
1290 supported = asus->is_pega_lucid;
1291 } else if (attr == &dev_attr_gps.attr) {
1292 supported = !acpi_check_handle(handle, METHOD_GPS_ON, NULL) &&
1293 !acpi_check_handle(handle, METHOD_GPS_OFF, NULL) &&
1294 !acpi_check_handle(handle, METHOD_GPS_STATUS, NULL);
1299 return supported ? attr->mode : 0;
1303 static const struct attribute_group asus_attr_group = {
1304 .is_visible = asus_sysfs_is_visible,
1305 .attrs = asus_attributes,
1308 static int asus_platform_init(struct asus_laptop *asus)
1312 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1313 if (!asus->platform_device)
1315 platform_set_drvdata(asus->platform_device, asus);
1317 result = platform_device_add(asus->platform_device);
1319 goto fail_platform_device;
1321 result = sysfs_create_group(&asus->platform_device->dev.kobj,
1329 platform_device_del(asus->platform_device);
1330 fail_platform_device:
1331 platform_device_put(asus->platform_device);
1335 static void asus_platform_exit(struct asus_laptop *asus)
1337 sysfs_remove_group(&asus->platform_device->dev.kobj, &asus_attr_group);
1338 platform_device_unregister(asus->platform_device);
1341 static struct platform_driver platform_driver = {
1343 .name = ASUS_LAPTOP_FILE,
1344 .owner = THIS_MODULE,
1349 * This function is used to initialize the context with right values. In this
1350 * method, we can make all the detection we want, and modify the asus_laptop
1353 static int asus_laptop_get_info(struct asus_laptop *asus)
1355 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1356 union acpi_object *model = NULL;
1357 unsigned long long bsts_result, hwrs_result;
1358 char *string = NULL;
1362 * Get DSDT headers early enough to allow for differentiating between
1363 * models, but late enough to allow acpi_bus_register_driver() to fail
1364 * before doing anything ACPI-specific. Should we encounter a machine,
1365 * which needs special handling (i.e. its hotkey device has a different
1366 * HID), this bit will be moved.
1368 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
1369 if (ACPI_FAILURE(status))
1370 pr_warn("Couldn't get the DSDT table header\n");
1372 /* We have to write 0 on init this far for all ASUS models */
1373 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
1374 pr_err("Hotkey initialization failed\n");
1378 /* This needs to be called for some laptops to init properly */
1380 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
1381 if (ACPI_FAILURE(status))
1382 pr_warn("Error calling BSTS\n");
1383 else if (bsts_result)
1384 pr_notice("BSTS called, 0x%02x returned\n",
1385 (uint) bsts_result);
1388 if (write_acpi_int(asus->handle, "CWAP", wapf))
1389 pr_err("Error calling CWAP(%d)\n", wapf);
1391 * Try to match the object returned by INIT to the specific model.
1392 * Handle every possible object (or the lack of thereof) the DSDT
1393 * writers might throw at us. When in trouble, we pass NULL to
1394 * asus_model_match() and try something completely different.
1396 if (buffer.pointer) {
1397 model = buffer.pointer;
1398 switch (model->type) {
1399 case ACPI_TYPE_STRING:
1400 string = model->string.pointer;
1402 case ACPI_TYPE_BUFFER:
1403 string = model->buffer.pointer;
1410 asus->name = kstrdup(string, GFP_KERNEL);
1412 kfree(buffer.pointer);
1417 pr_notice(" %s model detected\n", string);
1420 * The HWRS method return informations about the hardware.
1421 * 0x80 bit is for WLAN, 0x100 for Bluetooth,
1422 * 0x40 for WWAN, 0x10 for WIMAX.
1423 * The significance of others is yet to be found.
1426 acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
1427 if (!ACPI_FAILURE(status))
1428 pr_notice(" HRWS returned %x", (int)hwrs_result);
1430 if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
1431 asus->have_rsts = true;
1438 static int __devinit asus_acpi_init(struct asus_laptop *asus)
1442 result = acpi_bus_get_status(asus->device);
1445 if (!asus->device->status.present) {
1446 pr_err("Hotkey device not present, aborting\n");
1450 result = asus_laptop_get_info(asus);
1454 /* WLED and BLED are on by default */
1455 if (bluetooth_status >= 0)
1456 asus_bluetooth_set(asus, !!bluetooth_status);
1458 if (wlan_status >= 0)
1459 asus_wlan_set(asus, !!wlan_status);
1461 if (wimax_status >= 0)
1462 asus_wimax_set(asus, !!wimax_status);
1464 if (wwan_status >= 0)
1465 asus_wwan_set(asus, !!wwan_status);
1467 /* Keyboard Backlight is on by default */
1468 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
1469 asus_kled_set(asus, 1);
1471 /* LED display is off by default */
1472 asus->ledd_status = 0xFFF;
1474 /* Set initial values of light sensor and level */
1475 asus->light_switch = 0; /* Default to light sensor disabled */
1476 asus->light_level = 5; /* level 5 for sensor sensitivity */
1478 if (asus->is_pega_lucid) {
1479 asus_als_switch(asus, asus->light_switch);
1480 } else if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1481 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
1482 asus_als_switch(asus, asus->light_switch);
1483 asus_als_level(asus, asus->light_level);
1489 static void __devinit asus_dmi_check(void)
1493 model = dmi_get_system_info(DMI_PRODUCT_NAME);
1497 /* On L1400B WLED control the sound card, don't mess with it ... */
1498 if (strncmp(model, "L1400B", 6) == 0) {
1503 static bool asus_device_present;
1505 static int __devinit asus_acpi_add(struct acpi_device *device)
1507 struct asus_laptop *asus;
1510 pr_notice("Asus Laptop Support version %s\n",
1511 ASUS_LAPTOP_VERSION);
1512 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1515 asus->handle = device->handle;
1516 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1517 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1518 device->driver_data = asus;
1519 asus->device = device;
1523 result = asus_acpi_init(asus);
1528 * Need platform type detection first, then the platform
1529 * device. It is used as a parent for the sub-devices below.
1531 asus->is_pega_lucid = asus_check_pega_lucid(asus);
1532 result = asus_platform_init(asus);
1536 if (!acpi_video_backlight_support()) {
1537 result = asus_backlight_init(asus);
1539 goto fail_backlight;
1541 pr_info("Backlight controlled by ACPI video driver\n");
1543 result = asus_input_init(asus);
1547 result = asus_led_init(asus);
1551 result = asus_rfkill_init(asus);
1555 asus_device_present = true;
1559 asus_led_exit(asus);
1561 asus_input_exit(asus);
1563 asus_backlight_exit(asus);
1565 asus_platform_exit(asus);
1573 static int asus_acpi_remove(struct acpi_device *device, int type)
1575 struct asus_laptop *asus = acpi_driver_data(device);
1577 asus_backlight_exit(asus);
1578 asus_rfkill_exit(asus);
1579 asus_led_exit(asus);
1580 asus_input_exit(asus);
1581 asus_platform_exit(asus);
1588 static const struct acpi_device_id asus_device_ids[] = {
1593 MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1595 static struct acpi_driver asus_acpi_driver = {
1596 .name = ASUS_LAPTOP_NAME,
1597 .class = ASUS_LAPTOP_CLASS,
1598 .owner = THIS_MODULE,
1599 .ids = asus_device_ids,
1600 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1602 .add = asus_acpi_add,
1603 .remove = asus_acpi_remove,
1604 .notify = asus_acpi_notify,
1608 static int __init asus_laptop_init(void)
1612 result = platform_driver_register(&platform_driver);
1616 result = acpi_bus_register_driver(&asus_acpi_driver);
1618 goto fail_acpi_driver;
1619 if (!asus_device_present) {
1621 goto fail_no_device;
1626 acpi_bus_unregister_driver(&asus_acpi_driver);
1628 platform_driver_unregister(&platform_driver);
1632 static void __exit asus_laptop_exit(void)
1634 acpi_bus_unregister_driver(&asus_acpi_driver);
1635 platform_driver_unregister(&platform_driver);
1638 module_init(asus_laptop_init);
1639 module_exit(asus_laptop_exit);