2 * asus-laptop.c - Asus Laptop Support
5 * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
6 * Copyright (C) 2006 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
36 #include <linux/autoconf.h>
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/types.h>
41 #include <linux/err.h>
42 #include <linux/proc_fs.h>
43 #include <linux/leds.h>
44 #include <linux/platform_device.h>
45 #include <acpi/acpi_drivers.h>
46 #include <acpi/acpi_bus.h>
47 #include <asm/uaccess.h>
49 #define ASUS_LAPTOP_VERSION "0.40"
51 #define ASUS_HOTK_NAME "Asus Laptop Support"
52 #define ASUS_HOTK_CLASS "hotkey"
53 #define ASUS_HOTK_DEVICE_NAME "Hotkey"
54 #define ASUS_HOTK_HID "ATK0100"
55 #define ASUS_HOTK_FILE "asus-laptop"
56 #define ASUS_HOTK_PREFIX "\\_SB.ATKD."
59 * Known bits returned by \_SB.ATKD.HWRS
65 * Flags for hotk status
66 * WL_ON and BT_ON are also used for wireless_status()
68 #define WL_ON 0x01 //internal Wifi
69 #define BT_ON 0x02 //internal Bluetooth
70 #define MLED_ON 0x04 //mail LED
71 #define TLED_ON 0x08 //touchpad LED
72 #define RLED_ON 0x10 //Record LED
73 #define PLED_ON 0x20 //Phone LED
75 #define ASUS_LOG ASUS_HOTK_FILE ": "
76 #define ASUS_ERR KERN_ERR ASUS_LOG
77 #define ASUS_WARNING KERN_WARNING ASUS_LOG
78 #define ASUS_NOTICE KERN_NOTICE ASUS_LOG
79 #define ASUS_INFO KERN_INFO ASUS_LOG
80 #define ASUS_DEBUG KERN_DEBUG ASUS_LOG
82 MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
83 MODULE_DESCRIPTION(ASUS_HOTK_NAME);
84 MODULE_LICENSE("GPL");
86 #define ASUS_HANDLE(object, paths...) \
87 static acpi_handle object##_handle = NULL; \
88 static char *object##_paths[] = { paths }
91 ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
92 ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
93 ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
94 ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
97 * WLED and BLED are not handled like other XLED, because in some dsdt
98 * they also control the WLAN/Bluetooth device.
100 ASUS_HANDLE(wl_switch, ASUS_HOTK_PREFIX "WLED");
101 ASUS_HANDLE(bt_switch, ASUS_HOTK_PREFIX "BLED");
102 ASUS_HANDLE(wireless_status, ASUS_HOTK_PREFIX "RSTS"); /* All new models */
105 * This is the main structure, we can use it to store anything interesting
106 * about the hotk device
109 char *name; //laptop name
110 struct acpi_device *device; //the device we are in
111 acpi_handle handle; //the handle of the hotk device
112 char status; //status of the hotk, for LEDs, ...
113 u16 event_count[128]; //count for each event TODO make this better
117 * This header is made available to allow proper configuration given model,
118 * revision number , ... this info cannot go in struct asus_hotk because it is
119 * available before the hotk
121 static struct acpi_table_header *asus_info;
123 /* The actual device the driver binds to */
124 static struct asus_hotk *hotk;
127 * The hotkey driver declaration
129 static int asus_hotk_add(struct acpi_device *device);
130 static int asus_hotk_remove(struct acpi_device *device, int type);
131 static struct acpi_driver asus_hotk_driver = {
132 .name = ASUS_HOTK_NAME,
133 .class = ASUS_HOTK_CLASS,
134 .ids = ASUS_HOTK_HID,
136 .add = asus_hotk_add,
137 .remove = asus_hotk_remove,
141 /* These functions actually update the LED's, and are called from a
142 * workqueue. By doing this as separate work rather than when the LED
143 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
144 * potentially bad time, such as a timer interrupt. */
145 static struct workqueue_struct *led_workqueue;
147 #define ASUS_LED(object, ledname) \
148 static void object##_led_set(struct led_classdev *led_cdev, \
149 enum led_brightness value); \
150 static void object##_led_update(struct work_struct *ignored); \
151 static int object##_led_wk; \
152 DECLARE_WORK(object##_led_work, object##_led_update); \
153 static struct led_classdev object##_led = { \
154 .name = "asus:" ledname, \
155 .brightness_set = object##_led_set, \
158 ASUS_LED(mled, "mail");
159 ASUS_LED(tled, "touchpad");
160 ASUS_LED(rled, "record");
161 ASUS_LED(pled, "phone");
164 * This function evaluates an ACPI method, given an int as parameter, the
165 * method is searched within the scope of the handle, can be NULL. The output
166 * of the method is written is output, which can also be NULL
168 * returns 1 if write is successful, 0 else.
170 static int write_acpi_int(acpi_handle handle, const char *method, int val,
171 struct acpi_buffer *output)
173 struct acpi_object_list params; //list of input parameters (an int here)
174 union acpi_object in_obj; //the only param we use
178 params.pointer = &in_obj;
179 in_obj.type = ACPI_TYPE_INTEGER;
180 in_obj.integer.value = val;
182 status = acpi_evaluate_object(handle, (char *)method, ¶ms, output);
183 return (status == AE_OK);
186 static int read_acpi_int(acpi_handle handle, const char *method, int *val,
187 struct acpi_object_list *params)
189 struct acpi_buffer output;
190 union acpi_object out_obj;
193 output.length = sizeof(out_obj);
194 output.pointer = &out_obj;
196 status = acpi_evaluate_object(handle, (char *)method, params, &output);
197 *val = out_obj.integer.value;
198 return (status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER);
201 static int read_wireless_status(int mask) {
204 if (!wireless_status_handle)
205 return (hotk->status & mask) ? 1 : 0;
207 if (read_acpi_int(wireless_status_handle, NULL, &status, NULL)) {
208 return (status & mask) ? 1 : 0;
210 printk(ASUS_WARNING "Error reading Wireless status\n");
212 return (hotk->status & mask) ? 1 : 0;
215 /* Generic LED functions */
216 static int read_status(int mask)
218 /* There is a special method for both wireless devices */
219 if (mask == BT_ON || mask == WL_ON)
220 return read_wireless_status(mask);
222 return (hotk->status & mask) ? 1 : 0;
225 static void write_status(acpi_handle handle, int out, int mask,
228 hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
230 if (invert) /* invert target value */
233 if (handle && !write_acpi_int(handle, NULL, out, NULL))
234 printk(ASUS_WARNING " write failed\n");
237 /* /sys/class/led handlers */
238 #define ASUS_LED_HANDLER(object, mask, invert) \
239 static void object##_led_set(struct led_classdev *led_cdev, \
240 enum led_brightness value) \
242 object##_led_wk = value; \
243 queue_work(led_workqueue, &object##_led_work); \
245 static void object##_led_update(struct work_struct *ignored) \
247 int value = object##_led_wk; \
248 write_status(object##_set_handle, value, (mask), (invert)); \
251 ASUS_LED_HANDLER(mled, MLED_ON, 1);
252 ASUS_LED_HANDLER(pled, PLED_ON, 0);
253 ASUS_LED_HANDLER(rled, RLED_ON, 0);
254 ASUS_LED_HANDLER(tled, TLED_ON, 0);
257 * Platform device handlers
261 * We write our info in page, we begin at offset off and cannot write more
262 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
263 * number of bytes written in page
265 static ssize_t show_infos(struct device *dev,
266 struct device_attribute *attr, char *page)
270 char buf[16]; //enough for all info
272 * We use the easy way, we don't care of off and count, so we don't set eof
276 len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
277 len += sprintf(page + len, "Model reference : %s\n", hotk->name);
279 * The SFUN method probably allows the original driver to get the list
280 * of features supported by a given model. For now, 0x0100 or 0x0800
281 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
282 * The significance of others is yet to be found.
284 if (read_acpi_int(hotk->handle, "SFUN", &temp, NULL))
286 sprintf(page + len, "SFUN value : 0x%04x\n", temp);
288 * Another value for userspace: the ASYM method returns 0x02 for
289 * battery low and 0x04 for battery critical, its readings tend to be
290 * more accurate than those provided by _BST.
291 * Note: since not all the laptops provide this method, errors are
294 if (read_acpi_int(hotk->handle, "ASYM", &temp, NULL))
296 sprintf(page + len, "ASYM value : 0x%04x\n", temp);
298 snprintf(buf, 16, "%d", asus_info->length);
299 len += sprintf(page + len, "DSDT length : %s\n", buf);
300 snprintf(buf, 16, "%d", asus_info->checksum);
301 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
302 snprintf(buf, 16, "%d", asus_info->revision);
303 len += sprintf(page + len, "DSDT revision : %s\n", buf);
304 snprintf(buf, 7, "%s", asus_info->oem_id);
305 len += sprintf(page + len, "OEM id : %s\n", buf);
306 snprintf(buf, 9, "%s", asus_info->oem_table_id);
307 len += sprintf(page + len, "OEM table id : %s\n", buf);
308 snprintf(buf, 16, "%x", asus_info->oem_revision);
309 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
310 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
311 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
312 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
313 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
319 static int parse_arg(const char *buf, unsigned long count, int *val)
325 if (sscanf(buf, "%i", val) != 1)
330 static ssize_t store_status(const char *buf, size_t count,
331 acpi_handle handle, int mask, int invert)
336 rv = parse_arg(buf, count, &value);
340 write_status(handle, out, mask, invert);
348 static ssize_t show_wlan(struct device *dev,
349 struct device_attribute *attr, char *buf)
351 return sprintf(buf, "%d\n", read_status(WL_ON));
354 static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
355 const char *buf, size_t count)
357 return store_status(buf, count, wl_switch_handle, WL_ON, 0);
363 static ssize_t show_bluetooth(struct device *dev,
364 struct device_attribute *attr, char *buf)
366 return sprintf(buf, "%d\n", read_status(BT_ON));
369 static ssize_t store_bluetooth(struct device *dev, struct device_attribute *attr,
370 const char *buf, size_t count)
372 return store_status(buf, count, bt_switch_handle, BT_ON, 0);
375 static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
377 /* TODO Find a better way to handle events count. */
381 acpi_bus_generate_event(hotk->device, event,
382 hotk->event_count[event % 128]++);
387 #define ASUS_CREATE_DEVICE_ATTR(_name) \
388 struct device_attribute dev_attr_##_name = { \
390 .name = __stringify(_name), \
392 .owner = THIS_MODULE }, \
397 #define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
399 dev_attr_##_name.attr.mode = _mode; \
400 dev_attr_##_name.show = _show; \
401 dev_attr_##_name.store = _store; \
404 static ASUS_CREATE_DEVICE_ATTR(infos);
405 static ASUS_CREATE_DEVICE_ATTR(wlan);
406 static ASUS_CREATE_DEVICE_ATTR(bluetooth);
408 static struct attribute *asuspf_attributes[] = {
409 &dev_attr_infos.attr,
411 &dev_attr_bluetooth.attr,
415 static struct attribute_group asuspf_attribute_group = {
416 .attrs = asuspf_attributes
419 static struct platform_driver asuspf_driver = {
421 .name = ASUS_HOTK_FILE,
422 .owner = THIS_MODULE,
426 static struct platform_device *asuspf_device;
429 static void asus_hotk_add_fs(void)
431 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
433 if (wl_switch_handle)
434 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
436 if (bt_switch_handle)
437 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
438 show_bluetooth, store_bluetooth);
441 static int asus_handle_init(char *name, acpi_handle *handle,
442 char **paths, int num_paths)
447 for (i = 0; i < num_paths; i++) {
448 status = acpi_get_handle(NULL, paths[i], handle);
449 if (ACPI_SUCCESS(status))
457 #define ASUS_HANDLE_INIT(object) \
458 asus_handle_init(#object, &object##_handle, object##_paths, \
459 ARRAY_SIZE(object##_paths))
463 * This function is used to initialize the hotk with right values. In this
464 * method, we can make all the detection we want, and modify the hotk struct
466 static int asus_hotk_get_info(void)
468 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
469 struct acpi_buffer dsdt = { ACPI_ALLOCATE_BUFFER, NULL };
470 union acpi_object *model = NULL;
471 int bsts_result, hwrs_result;
476 * Get DSDT headers early enough to allow for differentiating between
477 * models, but late enough to allow acpi_bus_register_driver() to fail
478 * before doing anything ACPI-specific. Should we encounter a machine,
479 * which needs special handling (i.e. its hotkey device has a different
480 * HID), this bit will be moved. A global variable asus_info contains
483 status = acpi_get_table(ACPI_TABLE_ID_DSDT, 1, &dsdt);
484 if (ACPI_FAILURE(status))
485 printk(ASUS_WARNING "Couldn't get the DSDT table header\n");
487 asus_info = dsdt.pointer;
489 /* We have to write 0 on init this far for all ASUS models */
490 if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
491 printk(ASUS_ERR "Hotkey initialization failed\n");
495 /* This needs to be called for some laptops to init properly */
496 if (!read_acpi_int(hotk->handle, "BSTS", &bsts_result, NULL))
497 printk(ASUS_WARNING "Error calling BSTS\n");
498 else if (bsts_result)
499 printk(ASUS_NOTICE "BSTS called, 0x%02x returned\n",
503 * Try to match the object returned by INIT to the specific model.
504 * Handle every possible object (or the lack of thereof) the DSDT
505 * writers might throw at us. When in trouble, we pass NULL to
506 * asus_model_match() and try something completely different.
508 if (buffer.pointer) {
509 model = buffer.pointer;
510 switch (model->type) {
511 case ACPI_TYPE_STRING:
512 string = model->string.pointer;
514 case ACPI_TYPE_BUFFER:
515 string = model->buffer.pointer;
522 hotk->name = kstrdup(string, GFP_KERNEL);
527 printk(ASUS_NOTICE " %s model detected\n", string);
529 ASUS_HANDLE_INIT(mled_set);
530 ASUS_HANDLE_INIT(tled_set);
531 ASUS_HANDLE_INIT(rled_set);
532 ASUS_HANDLE_INIT(pled_set);
535 * The HWRS method return informations about the hardware.
536 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
537 * The significance of others is yet to be found.
538 * If we don't find the method, we assume the device are present.
540 if (!read_acpi_int(hotk->handle, "HRWS", &hwrs_result, NULL))
541 hwrs_result = WL_HWRS | BT_HWRS;
543 if(hwrs_result & WL_HWRS)
544 ASUS_HANDLE_INIT(wl_switch);
545 if(hwrs_result & BT_HWRS)
546 ASUS_HANDLE_INIT(bt_switch);
548 ASUS_HANDLE_INIT(wireless_status);
555 static int asus_hotk_check(void)
559 result = acpi_bus_get_status(hotk->device);
563 if (hotk->device->status.present) {
564 result = asus_hotk_get_info();
566 printk(ASUS_ERR "Hotkey device not present, aborting\n");
573 static int asus_hotk_found;
575 static int asus_hotk_add(struct acpi_device *device)
577 acpi_status status = AE_OK;
583 printk(ASUS_NOTICE "Asus Laptop Support version %s\n",
584 ASUS_LAPTOP_VERSION);
586 hotk = kmalloc(sizeof(struct asus_hotk), GFP_KERNEL);
589 memset(hotk, 0, sizeof(struct asus_hotk));
591 hotk->handle = device->handle;
592 strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
593 strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
594 acpi_driver_data(device) = hotk;
595 hotk->device = device;
597 result = asus_hotk_check();
604 * We install the handler, it will receive the hotk in parameter, so, we
605 * could add other data to the hotk struct
607 status = acpi_install_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
608 asus_hotk_notify, hotk);
609 if (ACPI_FAILURE(status))
610 printk(ASUS_ERR "Error installing notify handler\n");
614 /* WLED and BLED are on by default */
615 write_status(bt_switch_handle, 1, BT_ON, 0);
616 write_status(wl_switch_handle, 1, WL_ON, 0);
627 static int asus_hotk_remove(struct acpi_device *device, int type)
629 acpi_status status = 0;
631 if (!device || !acpi_driver_data(device))
634 status = acpi_remove_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
636 if (ACPI_FAILURE(status))
637 printk(ASUS_ERR "Error removing notify handler\n");
645 #define ASUS_LED_UNREGISTER(object) \
646 if(object##_led.class_dev \
647 && !IS_ERR(object##_led.class_dev)) \
648 led_classdev_unregister(&object##_led)
650 static void asus_led_exit(void)
652 ASUS_LED_UNREGISTER(mled);
653 ASUS_LED_UNREGISTER(tled);
654 ASUS_LED_UNREGISTER(pled);
655 ASUS_LED_UNREGISTER(rled);
657 destroy_workqueue(led_workqueue);
660 static void __exit asus_laptop_exit(void)
664 acpi_bus_unregister_driver(&asus_hotk_driver);
665 sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
666 platform_device_unregister(asuspf_device);
667 platform_driver_unregister(&asuspf_driver);
672 static int asus_led_register(acpi_handle handle,
673 struct led_classdev * ldev,
679 return led_classdev_register(dev, ldev);
681 #define ASUS_LED_REGISTER(object, device) \
682 asus_led_register(object##_set_handle, &object##_led, device)
684 static int asus_led_init(struct device * dev)
688 rv = ASUS_LED_REGISTER(mled, dev);
692 rv = ASUS_LED_REGISTER(tled, dev);
696 rv = ASUS_LED_REGISTER(rled, dev);
700 rv = ASUS_LED_REGISTER(pled, dev);
704 led_workqueue = create_singlethread_workqueue("led_workqueue");
711 static int __init asus_laptop_init(void)
719 if (!acpi_specific_hotkey_enabled) {
720 printk(ASUS_ERR "Using generic hotkey driver\n");
724 result = acpi_bus_register_driver(&asus_hotk_driver);
729 * This is a bit of a kludge. We only want this module loaded
730 * for ASUS systems, but there's currently no way to probe the
731 * ACPI namespace for ASUS HIDs. So we just return failure if
732 * we didn't find one, which will cause the module to be
735 if (!asus_hotk_found) {
736 acpi_bus_unregister_driver(&asus_hotk_driver);
740 dev = acpi_get_physical_device(hotk->device->handle);
742 result = asus_led_init(dev);
746 /* Register platform stuff */
747 result = platform_driver_register(&asuspf_driver);
749 goto fail_platform_driver;
751 asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
752 if (!asuspf_device) {
754 goto fail_platform_device1;
757 result = platform_device_add(asuspf_device);
759 goto fail_platform_device2;
761 result = sysfs_create_group(&asuspf_device->dev.kobj,
762 &asuspf_attribute_group);
769 platform_device_del(asuspf_device);
771 fail_platform_device2:
772 platform_device_put(asuspf_device);
774 fail_platform_device1:
775 platform_driver_unregister(&asuspf_driver);
777 fail_platform_driver:
785 module_init(asus_laptop_init);
786 module_exit(asus_laptop_exit);