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"
180 #define METHOD_PEGA_ENABLE "ENPR"
181 #define METHOD_PEGA_DISABLE "DAPR"
182 #define METHOD_PEGA_READ "RDLN"
185 * Define a specific led structure to keep the main structure clean
189 struct work_struct work;
190 struct led_classdev led;
191 struct asus_laptop *asus;
196 * This is the main structure, we can use it to store anything interesting
197 * about the hotk device
200 char *name; /* laptop name */
202 struct acpi_table_header *dsdt_info;
203 struct platform_device *platform_device;
204 struct acpi_device *device; /* the device we are in */
205 struct backlight_device *backlight_device;
207 struct input_dev *inputdev;
208 struct key_entry *keymap;
210 struct asus_led mled;
211 struct asus_led tled;
212 struct asus_led rled;
213 struct asus_led pled;
214 struct asus_led gled;
215 struct asus_led kled;
216 struct workqueue_struct *led_workqueue;
222 struct rfkill *gps_rfkill;
224 acpi_handle handle; /* the handle of the hotk device */
225 u32 ledd_status; /* status of the LED display */
226 u8 light_level; /* light sensor level */
227 u8 light_switch; /* light sensor switch value */
228 u16 event_count[128]; /* count for each event TODO make this better */
231 static const struct key_entry asus_keymap[] = {
232 /* Lenovo SL Specific keycodes */
233 {KE_KEY, 0x02, { KEY_SCREENLOCK } },
234 {KE_KEY, 0x05, { KEY_WLAN } },
235 {KE_KEY, 0x08, { KEY_F13 } },
236 {KE_KEY, 0x17, { KEY_ZOOM } },
237 {KE_KEY, 0x1f, { KEY_BATTERY } },
238 /* End of Lenovo SL Specific keycodes */
239 {KE_KEY, 0x30, { KEY_VOLUMEUP } },
240 {KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
241 {KE_KEY, 0x32, { KEY_MUTE } },
242 {KE_KEY, 0x33, { KEY_SWITCHVIDEOMODE } },
243 {KE_KEY, 0x34, { KEY_SWITCHVIDEOMODE } },
244 {KE_KEY, 0x40, { KEY_PREVIOUSSONG } },
245 {KE_KEY, 0x41, { KEY_NEXTSONG } },
246 {KE_KEY, 0x43, { KEY_STOPCD } },
247 {KE_KEY, 0x45, { KEY_PLAYPAUSE } },
248 {KE_KEY, 0x4c, { KEY_MEDIA } },
249 {KE_KEY, 0x50, { KEY_EMAIL } },
250 {KE_KEY, 0x51, { KEY_WWW } },
251 {KE_KEY, 0x55, { KEY_CALC } },
252 {KE_KEY, 0x5C, { KEY_SCREENLOCK } }, /* Screenlock */
253 {KE_KEY, 0x5D, { KEY_WLAN } },
254 {KE_KEY, 0x5E, { KEY_WLAN } },
255 {KE_KEY, 0x5F, { KEY_WLAN } },
256 {KE_KEY, 0x60, { KEY_SWITCHVIDEOMODE } },
257 {KE_KEY, 0x61, { KEY_SWITCHVIDEOMODE } },
258 {KE_KEY, 0x62, { KEY_SWITCHVIDEOMODE } },
259 {KE_KEY, 0x63, { KEY_SWITCHVIDEOMODE } },
260 {KE_KEY, 0x6B, { KEY_F13 } }, /* Lock Touchpad */
261 {KE_KEY, 0x7E, { KEY_BLUETOOTH } },
262 {KE_KEY, 0x7D, { KEY_BLUETOOTH } },
263 {KE_KEY, 0x82, { KEY_CAMERA } },
264 {KE_KEY, 0x88, { KEY_WLAN } },
265 {KE_KEY, 0x8A, { KEY_PROG1 } },
266 {KE_KEY, 0x95, { KEY_MEDIA } },
267 {KE_KEY, 0x99, { KEY_PHONE } },
268 {KE_KEY, 0xc4, { KEY_KBDILLUMUP } },
269 {KE_KEY, 0xc5, { KEY_KBDILLUMDOWN } },
270 {KE_KEY, 0xb5, { KEY_CALC } },
276 * This function evaluates an ACPI method, given an int as parameter, the
277 * method is searched within the scope of the handle, can be NULL. The output
278 * of the method is written is output, which can also be NULL
280 * returns 0 if write is successful, -1 else.
282 static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
283 struct acpi_buffer *output)
285 struct acpi_object_list params; /* list of input parameters (an int) */
286 union acpi_object in_obj; /* the only param we use */
293 params.pointer = &in_obj;
294 in_obj.type = ACPI_TYPE_INTEGER;
295 in_obj.integer.value = val;
297 status = acpi_evaluate_object(handle, (char *)method, ¶ms, output);
304 static int write_acpi_int(acpi_handle handle, const char *method, int val)
306 return write_acpi_int_ret(handle, method, val, NULL);
309 static int acpi_check_handle(acpi_handle handle, const char *method,
318 status = acpi_get_handle(handle, (char *)method,
323 status = acpi_get_handle(handle, (char *)method,
327 if (status != AE_OK) {
329 pr_warn("Error finding %s\n", method);
335 static bool asus_check_pega_lucid(struct asus_laptop *asus)
337 return !strcmp(asus->name, DEVICE_NAME_PEGA) &&
338 !acpi_check_handle(asus->handle, METHOD_PEGA_ENABLE, NULL) &&
339 !acpi_check_handle(asus->handle, METHOD_PEGA_DISABLE, NULL) &&
340 !acpi_check_handle(asus->handle, METHOD_PEGA_READ, NULL);
343 /* Generic LED function */
344 static int asus_led_set(struct asus_laptop *asus, const char *method,
347 if (!strcmp(method, METHOD_MLED))
349 else if (!strcmp(method, METHOD_GLED))
354 return write_acpi_int(asus->handle, method, value);
360 /* /sys/class/led handlers */
361 static void asus_led_cdev_set(struct led_classdev *led_cdev,
362 enum led_brightness value)
364 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
365 struct asus_laptop *asus = led->asus;
368 queue_work(asus->led_workqueue, &led->work);
371 static void asus_led_cdev_update(struct work_struct *work)
373 struct asus_led *led = container_of(work, struct asus_led, work);
374 struct asus_laptop *asus = led->asus;
376 asus_led_set(asus, led->method, led->wk);
379 static enum led_brightness asus_led_cdev_get(struct led_classdev *led_cdev)
381 return led_cdev->brightness;
385 * Keyboard backlight (also a LED)
387 static int asus_kled_lvl(struct asus_laptop *asus)
389 unsigned long long kblv;
390 struct acpi_object_list params;
391 union acpi_object in_obj;
395 params.pointer = &in_obj;
396 in_obj.type = ACPI_TYPE_INTEGER;
397 in_obj.integer.value = 2;
399 rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
401 if (ACPI_FAILURE(rv)) {
402 pr_warn("Error reading kled level\n");
408 static int asus_kled_set(struct asus_laptop *asus, int kblv)
411 kblv = (1 << 7) | (kblv & 0x7F);
415 if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
416 pr_warn("Keyboard LED display write failed\n");
422 static void asus_kled_cdev_set(struct led_classdev *led_cdev,
423 enum led_brightness value)
425 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
426 struct asus_laptop *asus = led->asus;
429 queue_work(asus->led_workqueue, &led->work);
432 static void asus_kled_cdev_update(struct work_struct *work)
434 struct asus_led *led = container_of(work, struct asus_led, work);
435 struct asus_laptop *asus = led->asus;
437 asus_kled_set(asus, led->wk);
440 static enum led_brightness asus_kled_cdev_get(struct led_classdev *led_cdev)
442 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
443 struct asus_laptop *asus = led->asus;
445 return asus_kled_lvl(asus);
448 static void asus_led_exit(struct asus_laptop *asus)
450 if (asus->mled.led.dev)
451 led_classdev_unregister(&asus->mled.led);
452 if (asus->tled.led.dev)
453 led_classdev_unregister(&asus->tled.led);
454 if (asus->pled.led.dev)
455 led_classdev_unregister(&asus->pled.led);
456 if (asus->rled.led.dev)
457 led_classdev_unregister(&asus->rled.led);
458 if (asus->gled.led.dev)
459 led_classdev_unregister(&asus->gled.led);
460 if (asus->kled.led.dev)
461 led_classdev_unregister(&asus->kled.led);
462 if (asus->led_workqueue) {
463 destroy_workqueue(asus->led_workqueue);
464 asus->led_workqueue = NULL;
468 /* Ugly macro, need to fix that later */
469 static int asus_led_register(struct asus_laptop *asus,
470 struct asus_led *led,
471 const char *name, const char *method)
473 struct led_classdev *led_cdev = &led->led;
475 if (!method || acpi_check_handle(asus->handle, method, NULL))
476 return 0; /* Led not present */
479 led->method = method;
481 INIT_WORK(&led->work, asus_led_cdev_update);
482 led_cdev->name = name;
483 led_cdev->brightness_set = asus_led_cdev_set;
484 led_cdev->brightness_get = asus_led_cdev_get;
485 led_cdev->max_brightness = 1;
486 return led_classdev_register(&asus->platform_device->dev, led_cdev);
489 static int asus_led_init(struct asus_laptop *asus)
494 * Functions that actually update the LED's are called from a
495 * workqueue. By doing this as separate work rather than when the LED
496 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
497 * potentially bad time, such as a timer interrupt.
499 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
500 if (!asus->led_workqueue)
503 r = asus_led_register(asus, &asus->mled, "asus::mail", METHOD_MLED);
506 r = asus_led_register(asus, &asus->tled, "asus::touchpad", METHOD_TLED);
509 r = asus_led_register(asus, &asus->rled, "asus::record", METHOD_RLED);
512 r = asus_led_register(asus, &asus->pled, "asus::phone", METHOD_PLED);
515 r = asus_led_register(asus, &asus->gled, "asus::gaming", METHOD_GLED);
518 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
519 !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL)) {
520 struct asus_led *led = &asus->kled;
521 struct led_classdev *cdev = &led->led;
525 INIT_WORK(&led->work, asus_kled_cdev_update);
526 cdev->name = "asus::kbd_backlight";
527 cdev->brightness_set = asus_kled_cdev_set;
528 cdev->brightness_get = asus_kled_cdev_get;
529 cdev->max_brightness = 3;
530 r = led_classdev_register(&asus->platform_device->dev, cdev);
541 static int asus_read_brightness(struct backlight_device *bd)
543 struct asus_laptop *asus = bl_get_data(bd);
544 unsigned long long value;
545 acpi_status rv = AE_OK;
547 rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
549 if (ACPI_FAILURE(rv))
550 pr_warn("Error reading brightness\n");
555 static int asus_set_brightness(struct backlight_device *bd, int value)
557 struct asus_laptop *asus = bl_get_data(bd);
559 if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
560 pr_warn("Error changing brightness\n");
566 static int update_bl_status(struct backlight_device *bd)
568 int value = bd->props.brightness;
570 return asus_set_brightness(bd, value);
573 static const struct backlight_ops asusbl_ops = {
574 .get_brightness = asus_read_brightness,
575 .update_status = update_bl_status,
578 static int asus_backlight_notify(struct asus_laptop *asus)
580 struct backlight_device *bd = asus->backlight_device;
581 int old = bd->props.brightness;
583 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
588 static int asus_backlight_init(struct asus_laptop *asus)
590 struct backlight_device *bd;
591 struct backlight_properties props;
593 if (acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) ||
594 acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL))
597 memset(&props, 0, sizeof(struct backlight_properties));
598 props.max_brightness = 15;
599 props.type = BACKLIGHT_PLATFORM;
601 bd = backlight_device_register(ASUS_LAPTOP_FILE,
602 &asus->platform_device->dev, asus,
603 &asusbl_ops, &props);
605 pr_err("Could not register asus backlight device\n");
606 asus->backlight_device = NULL;
610 asus->backlight_device = bd;
611 bd->props.brightness = asus_read_brightness(bd);
612 bd->props.power = FB_BLANK_UNBLANK;
613 backlight_update_status(bd);
617 static void asus_backlight_exit(struct asus_laptop *asus)
619 if (asus->backlight_device)
620 backlight_device_unregister(asus->backlight_device);
621 asus->backlight_device = NULL;
625 * Platform device handlers
629 * We write our info in page, we begin at offset off and cannot write more
630 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
631 * number of bytes written in page
633 static ssize_t show_infos(struct device *dev,
634 struct device_attribute *attr, char *page)
636 struct asus_laptop *asus = dev_get_drvdata(dev);
638 unsigned long long temp;
639 char buf[16]; /* enough for all info */
640 acpi_status rv = AE_OK;
643 * We use the easy way, we don't care of off and count,
644 * so we don't set eof to 1
647 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
648 len += sprintf(page + len, "Model reference : %s\n", asus->name);
650 * The SFUN method probably allows the original driver to get the list
651 * of features supported by a given model. For now, 0x0100 or 0x0800
652 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
653 * The significance of others is yet to be found.
655 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
656 if (!ACPI_FAILURE(rv))
657 len += sprintf(page + len, "SFUN value : %#x\n",
660 * The HWRS method return informations about the hardware.
661 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
662 * The significance of others is yet to be found.
663 * If we don't find the method, we assume the device are present.
665 rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
666 if (!ACPI_FAILURE(rv))
667 len += sprintf(page + len, "HRWS value : %#x\n",
670 * Another value for userspace: the ASYM method returns 0x02 for
671 * battery low and 0x04 for battery critical, its readings tend to be
672 * more accurate than those provided by _BST.
673 * Note: since not all the laptops provide this method, errors are
676 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
677 if (!ACPI_FAILURE(rv))
678 len += sprintf(page + len, "ASYM value : %#x\n",
680 if (asus->dsdt_info) {
681 snprintf(buf, 16, "%d", asus->dsdt_info->length);
682 len += sprintf(page + len, "DSDT length : %s\n", buf);
683 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
684 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
685 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
686 len += sprintf(page + len, "DSDT revision : %s\n", buf);
687 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
688 len += sprintf(page + len, "OEM id : %s\n", buf);
689 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
690 len += sprintf(page + len, "OEM table id : %s\n", buf);
691 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
692 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
693 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
694 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
695 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
696 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
702 static int parse_arg(const char *buf, unsigned long count, int *val)
708 if (sscanf(buf, "%i", val) != 1)
713 static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
714 const char *buf, size_t count,
720 rv = parse_arg(buf, count, &value);
724 if (write_acpi_int(asus->handle, method, value))
732 static ssize_t show_ledd(struct device *dev,
733 struct device_attribute *attr, char *buf)
735 struct asus_laptop *asus = dev_get_drvdata(dev);
737 return sprintf(buf, "0x%08x\n", asus->ledd_status);
740 static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
741 const char *buf, size_t count)
743 struct asus_laptop *asus = dev_get_drvdata(dev);
746 rv = parse_arg(buf, count, &value);
748 if (write_acpi_int(asus->handle, METHOD_LEDD, value)) {
749 pr_warn("LED display write failed\n");
752 asus->ledd_status = (u32) value;
760 static int asus_wireless_status(struct asus_laptop *asus, int mask)
762 unsigned long long status;
763 acpi_status rv = AE_OK;
765 if (!asus->have_rsts)
766 return (asus->wireless_status & mask) ? 1 : 0;
768 rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
770 if (ACPI_FAILURE(rv)) {
771 pr_warn("Error reading Wireless status\n");
774 return !!(status & mask);
780 static int asus_wlan_set(struct asus_laptop *asus, int status)
782 if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
783 pr_warn("Error setting wlan status to %d\n", status);
789 static ssize_t show_wlan(struct device *dev,
790 struct device_attribute *attr, char *buf)
792 struct asus_laptop *asus = dev_get_drvdata(dev);
794 return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
797 static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
798 const char *buf, size_t count)
800 struct asus_laptop *asus = dev_get_drvdata(dev);
802 return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
808 static int asus_bluetooth_set(struct asus_laptop *asus, int status)
810 if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
811 pr_warn("Error setting bluetooth status to %d\n", status);
817 static ssize_t show_bluetooth(struct device *dev,
818 struct device_attribute *attr, char *buf)
820 struct asus_laptop *asus = dev_get_drvdata(dev);
822 return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
825 static ssize_t store_bluetooth(struct device *dev,
826 struct device_attribute *attr, const char *buf,
829 struct asus_laptop *asus = dev_get_drvdata(dev);
831 return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
837 static int asus_wimax_set(struct asus_laptop *asus, int status)
839 if (write_acpi_int(asus->handle, METHOD_WIMAX, !!status)) {
840 pr_warn("Error setting wimax status to %d\n", status);
846 static ssize_t show_wimax(struct device *dev,
847 struct device_attribute *attr, char *buf)
849 struct asus_laptop *asus = dev_get_drvdata(dev);
851 return sprintf(buf, "%d\n", asus_wireless_status(asus, WM_RSTS));
854 static ssize_t store_wimax(struct device *dev,
855 struct device_attribute *attr, const char *buf,
858 struct asus_laptop *asus = dev_get_drvdata(dev);
860 return sysfs_acpi_set(asus, buf, count, METHOD_WIMAX);
866 static int asus_wwan_set(struct asus_laptop *asus, int status)
868 if (write_acpi_int(asus->handle, METHOD_WWAN, !!status)) {
869 pr_warn("Error setting wwan status to %d\n", status);
875 static ssize_t show_wwan(struct device *dev,
876 struct device_attribute *attr, char *buf)
878 struct asus_laptop *asus = dev_get_drvdata(dev);
880 return sprintf(buf, "%d\n", asus_wireless_status(asus, WW_RSTS));
883 static ssize_t store_wwan(struct device *dev,
884 struct device_attribute *attr, const char *buf,
887 struct asus_laptop *asus = dev_get_drvdata(dev);
889 return sysfs_acpi_set(asus, buf, count, METHOD_WWAN);
895 static void asus_set_display(struct asus_laptop *asus, int value)
897 /* no sanity check needed for now */
898 if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
899 pr_warn("Error setting display\n");
904 * Experimental support for display switching. As of now: 1 should activate
905 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
906 * Any combination (bitwise) of these will suffice. I never actually tested 4
907 * displays hooked up simultaneously, so be warned. See the acpi4asus README
910 static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
911 const char *buf, size_t count)
913 struct asus_laptop *asus = dev_get_drvdata(dev);
916 rv = parse_arg(buf, count, &value);
918 asus_set_display(asus, value);
925 static void asus_als_switch(struct asus_laptop *asus, int value)
927 if (write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value))
928 pr_warn("Error setting light sensor switch\n");
929 asus->light_switch = value;
932 static ssize_t show_lssw(struct device *dev,
933 struct device_attribute *attr, char *buf)
935 struct asus_laptop *asus = dev_get_drvdata(dev);
937 return sprintf(buf, "%d\n", asus->light_switch);
940 static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
941 const char *buf, size_t count)
943 struct asus_laptop *asus = dev_get_drvdata(dev);
946 rv = parse_arg(buf, count, &value);
948 asus_als_switch(asus, value ? 1 : 0);
953 static void asus_als_level(struct asus_laptop *asus, int value)
955 if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
956 pr_warn("Error setting light sensor level\n");
957 asus->light_level = value;
960 static ssize_t show_lslvl(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_level);
968 static ssize_t store_lslvl(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 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
977 /* 0 <= value <= 15 */
978 asus_als_level(asus, value);
987 static int asus_gps_status(struct asus_laptop *asus)
989 unsigned long long status;
990 acpi_status rv = AE_OK;
992 rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
994 if (ACPI_FAILURE(rv)) {
995 pr_warn("Error reading GPS status\n");
1001 static int asus_gps_switch(struct asus_laptop *asus, int status)
1003 const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
1005 if (write_acpi_int(asus->handle, meth, 0x02))
1010 static ssize_t show_gps(struct device *dev,
1011 struct device_attribute *attr, char *buf)
1013 struct asus_laptop *asus = dev_get_drvdata(dev);
1015 return sprintf(buf, "%d\n", asus_gps_status(asus));
1018 static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
1019 const char *buf, size_t count)
1021 struct asus_laptop *asus = dev_get_drvdata(dev);
1025 rv = parse_arg(buf, count, &value);
1028 ret = asus_gps_switch(asus, !!value);
1031 rfkill_set_sw_state(asus->gps_rfkill, !value);
1038 static int asus_gps_rfkill_set(void *data, bool blocked)
1040 struct asus_laptop *asus = data;
1042 return asus_gps_switch(asus, !blocked);
1045 static const struct rfkill_ops asus_gps_rfkill_ops = {
1046 .set_block = asus_gps_rfkill_set,
1049 static void asus_rfkill_exit(struct asus_laptop *asus)
1051 if (asus->gps_rfkill) {
1052 rfkill_unregister(asus->gps_rfkill);
1053 rfkill_destroy(asus->gps_rfkill);
1054 asus->gps_rfkill = NULL;
1058 static int asus_rfkill_init(struct asus_laptop *asus)
1062 if (acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) ||
1063 acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) ||
1064 acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
1067 asus->gps_rfkill = rfkill_alloc("asus-gps", &asus->platform_device->dev,
1069 &asus_gps_rfkill_ops, asus);
1070 if (!asus->gps_rfkill)
1073 result = rfkill_register(asus->gps_rfkill);
1075 rfkill_destroy(asus->gps_rfkill);
1076 asus->gps_rfkill = NULL;
1083 * Input device (i.e. hotkeys)
1085 static void asus_input_notify(struct asus_laptop *asus, int event)
1088 sparse_keymap_report_event(asus->inputdev, event, 1, true);
1091 static int asus_input_init(struct asus_laptop *asus)
1093 struct input_dev *input;
1096 input = input_allocate_device();
1098 pr_info("Unable to allocate input device\n");
1101 input->name = "Asus Laptop extra buttons";
1102 input->phys = ASUS_LAPTOP_FILE "/input0";
1103 input->id.bustype = BUS_HOST;
1104 input->dev.parent = &asus->platform_device->dev;
1106 error = sparse_keymap_setup(input, asus_keymap, NULL);
1108 pr_err("Unable to setup input device keymap\n");
1111 error = input_register_device(input);
1113 pr_info("Unable to register input device\n");
1114 goto err_free_keymap;
1117 asus->inputdev = input;
1121 sparse_keymap_free(input);
1123 input_free_device(input);
1127 static void asus_input_exit(struct asus_laptop *asus)
1129 if (asus->inputdev) {
1130 sparse_keymap_free(asus->inputdev);
1131 input_unregister_device(asus->inputdev);
1133 asus->inputdev = NULL;
1139 static void asus_acpi_notify(struct acpi_device *device, u32 event)
1141 struct asus_laptop *asus = acpi_driver_data(device);
1144 /* TODO Find a better way to handle events count. */
1145 count = asus->event_count[event % 128]++;
1146 acpi_bus_generate_proc_event(asus->device, event, count);
1147 acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
1148 dev_name(&asus->device->dev), event,
1151 /* Brightness events are special */
1152 if (event >= ATKD_BR_MIN && event <= ATKD_BR_MAX) {
1154 /* Ignore them completely if the acpi video driver is used */
1155 if (asus->backlight_device != NULL) {
1156 /* Update the backlight device. */
1157 asus_backlight_notify(asus);
1161 asus_input_notify(asus, event);
1164 static DEVICE_ATTR(infos, S_IRUGO, show_infos, NULL);
1165 static DEVICE_ATTR(wlan, S_IRUGO | S_IWUSR, show_wlan, store_wlan);
1166 static DEVICE_ATTR(bluetooth, S_IRUGO | S_IWUSR,
1167 show_bluetooth, store_bluetooth);
1168 static DEVICE_ATTR(wimax, S_IRUGO | S_IWUSR, show_wimax, store_wimax);
1169 static DEVICE_ATTR(wwan, S_IRUGO | S_IWUSR, show_wwan, store_wwan);
1170 static DEVICE_ATTR(display, S_IWUSR, NULL, store_disp);
1171 static DEVICE_ATTR(ledd, S_IRUGO | S_IWUSR, show_ledd, store_ledd);
1172 static DEVICE_ATTR(ls_level, S_IRUGO | S_IWUSR, show_lslvl, store_lslvl);
1173 static DEVICE_ATTR(ls_switch, S_IRUGO | S_IWUSR, show_lssw, store_lssw);
1174 static DEVICE_ATTR(gps, S_IRUGO | S_IWUSR, show_gps, store_gps);
1176 static struct attribute *asus_attributes[] = {
1177 &dev_attr_infos.attr,
1178 &dev_attr_wlan.attr,
1179 &dev_attr_bluetooth.attr,
1180 &dev_attr_wimax.attr,
1181 &dev_attr_wwan.attr,
1182 &dev_attr_display.attr,
1183 &dev_attr_ledd.attr,
1184 &dev_attr_ls_level.attr,
1185 &dev_attr_ls_switch.attr,
1190 static mode_t asus_sysfs_is_visible(struct kobject *kobj,
1191 struct attribute *attr,
1194 struct device *dev = container_of(kobj, struct device, kobj);
1195 struct platform_device *pdev = to_platform_device(dev);
1196 struct asus_laptop *asus = platform_get_drvdata(pdev);
1197 acpi_handle handle = asus->handle;
1200 if (attr == &dev_attr_wlan.attr) {
1201 supported = !acpi_check_handle(handle, METHOD_WLAN, NULL);
1203 } else if (attr == &dev_attr_bluetooth.attr) {
1204 supported = !acpi_check_handle(handle, METHOD_BLUETOOTH, NULL);
1206 } else if (attr == &dev_attr_display.attr) {
1207 supported = !acpi_check_handle(handle, METHOD_SWITCH_DISPLAY, NULL);
1209 } else if (attr == &dev_attr_wimax.attr) {
1211 !acpi_check_handle(asus->handle, METHOD_WIMAX, NULL);
1213 } else if (attr == &dev_attr_wwan.attr) {
1214 supported = !acpi_check_handle(asus->handle, METHOD_WWAN, NULL);
1216 } else if (attr == &dev_attr_ledd.attr) {
1217 supported = !acpi_check_handle(handle, METHOD_LEDD, NULL);
1219 } else if (attr == &dev_attr_ls_switch.attr ||
1220 attr == &dev_attr_ls_level.attr) {
1221 supported = !acpi_check_handle(handle, METHOD_ALS_CONTROL, NULL) &&
1222 !acpi_check_handle(handle, METHOD_ALS_LEVEL, NULL);
1223 } else if (attr == &dev_attr_gps.attr) {
1224 supported = !acpi_check_handle(handle, METHOD_GPS_ON, NULL) &&
1225 !acpi_check_handle(handle, METHOD_GPS_OFF, NULL) &&
1226 !acpi_check_handle(handle, METHOD_GPS_STATUS, NULL);
1231 return supported ? attr->mode : 0;
1235 static const struct attribute_group asus_attr_group = {
1236 .is_visible = asus_sysfs_is_visible,
1237 .attrs = asus_attributes,
1240 static int asus_platform_init(struct asus_laptop *asus)
1244 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1245 if (!asus->platform_device)
1247 platform_set_drvdata(asus->platform_device, asus);
1249 result = platform_device_add(asus->platform_device);
1251 goto fail_platform_device;
1253 result = sysfs_create_group(&asus->platform_device->dev.kobj,
1261 platform_device_del(asus->platform_device);
1262 fail_platform_device:
1263 platform_device_put(asus->platform_device);
1267 static void asus_platform_exit(struct asus_laptop *asus)
1269 sysfs_remove_group(&asus->platform_device->dev.kobj, &asus_attr_group);
1270 platform_device_unregister(asus->platform_device);
1273 static struct platform_driver platform_driver = {
1275 .name = ASUS_LAPTOP_FILE,
1276 .owner = THIS_MODULE,
1281 * This function is used to initialize the context with right values. In this
1282 * method, we can make all the detection we want, and modify the asus_laptop
1285 static int asus_laptop_get_info(struct asus_laptop *asus)
1287 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1288 union acpi_object *model = NULL;
1289 unsigned long long bsts_result, hwrs_result;
1290 char *string = NULL;
1294 * Get DSDT headers early enough to allow for differentiating between
1295 * models, but late enough to allow acpi_bus_register_driver() to fail
1296 * before doing anything ACPI-specific. Should we encounter a machine,
1297 * which needs special handling (i.e. its hotkey device has a different
1298 * HID), this bit will be moved.
1300 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
1301 if (ACPI_FAILURE(status))
1302 pr_warn("Couldn't get the DSDT table header\n");
1304 /* We have to write 0 on init this far for all ASUS models */
1305 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
1306 pr_err("Hotkey initialization failed\n");
1310 /* This needs to be called for some laptops to init properly */
1312 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
1313 if (ACPI_FAILURE(status))
1314 pr_warn("Error calling BSTS\n");
1315 else if (bsts_result)
1316 pr_notice("BSTS called, 0x%02x returned\n",
1317 (uint) bsts_result);
1320 if (write_acpi_int(asus->handle, "CWAP", wapf))
1321 pr_err("Error calling CWAP(%d)\n", wapf);
1323 * Try to match the object returned by INIT to the specific model.
1324 * Handle every possible object (or the lack of thereof) the DSDT
1325 * writers might throw at us. When in trouble, we pass NULL to
1326 * asus_model_match() and try something completely different.
1328 if (buffer.pointer) {
1329 model = buffer.pointer;
1330 switch (model->type) {
1331 case ACPI_TYPE_STRING:
1332 string = model->string.pointer;
1334 case ACPI_TYPE_BUFFER:
1335 string = model->buffer.pointer;
1342 asus->name = kstrdup(string, GFP_KERNEL);
1344 kfree(buffer.pointer);
1349 pr_notice(" %s model detected\n", string);
1352 * The HWRS method return informations about the hardware.
1353 * 0x80 bit is for WLAN, 0x100 for Bluetooth,
1354 * 0x40 for WWAN, 0x10 for WIMAX.
1355 * The significance of others is yet to be found.
1358 acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
1359 if (!ACPI_FAILURE(status))
1360 pr_notice(" HRWS returned %x", (int)hwrs_result);
1362 if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
1363 asus->have_rsts = true;
1370 static int __devinit asus_acpi_init(struct asus_laptop *asus)
1374 result = acpi_bus_get_status(asus->device);
1377 if (!asus->device->status.present) {
1378 pr_err("Hotkey device not present, aborting\n");
1382 result = asus_laptop_get_info(asus);
1386 /* WLED and BLED are on by default */
1387 if (bluetooth_status >= 0)
1388 asus_bluetooth_set(asus, !!bluetooth_status);
1390 if (wlan_status >= 0)
1391 asus_wlan_set(asus, !!wlan_status);
1393 if (wimax_status >= 0)
1394 asus_wimax_set(asus, !!wimax_status);
1396 if (wwan_status >= 0)
1397 asus_wwan_set(asus, !!wwan_status);
1399 /* Keyboard Backlight is on by default */
1400 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
1401 asus_kled_set(asus, 1);
1403 /* LED display is off by default */
1404 asus->ledd_status = 0xFFF;
1406 /* Set initial values of light sensor and level */
1407 asus->light_switch = 0; /* Default to light sensor disabled */
1408 asus->light_level = 5; /* level 5 for sensor sensitivity */
1410 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1411 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
1412 asus_als_switch(asus, asus->light_switch);
1413 asus_als_level(asus, asus->light_level);
1419 static void __devinit asus_dmi_check(void)
1423 model = dmi_get_system_info(DMI_PRODUCT_NAME);
1427 /* On L1400B WLED control the sound card, don't mess with it ... */
1428 if (strncmp(model, "L1400B", 6) == 0) {
1433 static bool asus_device_present;
1435 static int __devinit asus_acpi_add(struct acpi_device *device)
1437 struct asus_laptop *asus;
1440 pr_notice("Asus Laptop Support version %s\n",
1441 ASUS_LAPTOP_VERSION);
1442 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1445 asus->handle = device->handle;
1446 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1447 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1448 device->driver_data = asus;
1449 asus->device = device;
1453 result = asus_acpi_init(asus);
1458 * Need platform type detection first, then the platform
1459 * device. It is used as a parent for the sub-devices below.
1461 asus->is_pega_lucid = asus_check_pega_lucid(asus);
1462 result = asus_platform_init(asus);
1466 if (!acpi_video_backlight_support()) {
1467 result = asus_backlight_init(asus);
1469 goto fail_backlight;
1471 pr_info("Backlight controlled by ACPI video driver\n");
1473 result = asus_input_init(asus);
1477 result = asus_led_init(asus);
1481 result = asus_rfkill_init(asus);
1485 asus_device_present = true;
1489 asus_led_exit(asus);
1491 asus_input_exit(asus);
1493 asus_backlight_exit(asus);
1495 asus_platform_exit(asus);
1503 static int asus_acpi_remove(struct acpi_device *device, int type)
1505 struct asus_laptop *asus = acpi_driver_data(device);
1507 asus_backlight_exit(asus);
1508 asus_rfkill_exit(asus);
1509 asus_led_exit(asus);
1510 asus_input_exit(asus);
1511 asus_platform_exit(asus);
1518 static const struct acpi_device_id asus_device_ids[] = {
1523 MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1525 static struct acpi_driver asus_acpi_driver = {
1526 .name = ASUS_LAPTOP_NAME,
1527 .class = ASUS_LAPTOP_CLASS,
1528 .owner = THIS_MODULE,
1529 .ids = asus_device_ids,
1530 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1532 .add = asus_acpi_add,
1533 .remove = asus_acpi_remove,
1534 .notify = asus_acpi_notify,
1538 static int __init asus_laptop_init(void)
1542 result = platform_driver_register(&platform_driver);
1546 result = acpi_bus_register_driver(&asus_acpi_driver);
1548 goto fail_acpi_driver;
1549 if (!asus_device_present) {
1551 goto fail_no_device;
1556 acpi_bus_unregister_driver(&asus_acpi_driver);
1558 platform_driver_unregister(&platform_driver);
1562 static void __exit asus_laptop_exit(void)
1564 acpi_bus_unregister_driver(&asus_acpi_driver);
1565 platform_driver_unregister(&platform_driver);
1568 module_init(asus_laptop_init);
1569 module_exit(asus_laptop_exit);