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 * Flags for hotk status
61 #define MLED_ON 0x04 //mail LED
62 #define TLED_ON 0x08 //touchpad LED
63 #define RLED_ON 0x10 //Record LED
64 #define PLED_ON 0x20 //Phone LED
66 #define ASUS_LOG ASUS_HOTK_FILE ": "
67 #define ASUS_ERR KERN_ERR ASUS_LOG
68 #define ASUS_WARNING KERN_WARNING ASUS_LOG
69 #define ASUS_NOTICE KERN_NOTICE ASUS_LOG
70 #define ASUS_INFO KERN_INFO ASUS_LOG
71 #define ASUS_DEBUG KERN_DEBUG ASUS_LOG
73 MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
74 MODULE_DESCRIPTION(ASUS_HOTK_NAME);
75 MODULE_LICENSE("GPL");
77 #define ASUS_HANDLE(object, paths...) \
78 static acpi_handle object##_handle = NULL; \
79 static char *object##_paths[] = { paths }
82 ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
83 ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
84 ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
85 ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
88 * This is the main structure, we can use it to store anything interesting
89 * about the hotk device
92 char *name; //laptop name
93 struct acpi_device *device; //the device we are in
94 acpi_handle handle; //the handle of the hotk device
95 char status; //status of the hotk, for LEDs, ...
96 u16 event_count[128]; //count for each event TODO make this better
100 * This header is made available to allow proper configuration given model,
101 * revision number , ... this info cannot go in struct asus_hotk because it is
102 * available before the hotk
104 static struct acpi_table_header *asus_info;
106 /* The actual device the driver binds to */
107 static struct asus_hotk *hotk;
110 * The hotkey driver declaration
112 static int asus_hotk_add(struct acpi_device *device);
113 static int asus_hotk_remove(struct acpi_device *device, int type);
114 static struct acpi_driver asus_hotk_driver = {
115 .name = ASUS_HOTK_NAME,
116 .class = ASUS_HOTK_CLASS,
117 .ids = ASUS_HOTK_HID,
119 .add = asus_hotk_add,
120 .remove = asus_hotk_remove,
124 /* These functions actually update the LED's, and are called from a
125 * workqueue. By doing this as separate work rather than when the LED
126 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
127 * potentially bad time, such as a timer interrupt. */
128 static struct workqueue_struct *led_workqueue;
130 #define ASUS_LED(object, ledname) \
131 static void object##_led_set(struct led_classdev *led_cdev, \
132 enum led_brightness value); \
133 static void object##_led_update(struct work_struct *ignored); \
134 static int object##_led_wk; \
135 DECLARE_WORK(object##_led_work, object##_led_update); \
136 static struct led_classdev object##_led = { \
137 .name = "asus:" ledname, \
138 .brightness_set = object##_led_set, \
141 ASUS_LED(mled, "mail");
142 ASUS_LED(tled, "touchpad");
143 ASUS_LED(rled, "record");
144 ASUS_LED(pled, "phone");
147 * This function evaluates an ACPI method, given an int as parameter, the
148 * method is searched within the scope of the handle, can be NULL. The output
149 * of the method is written is output, which can also be NULL
151 * returns 1 if write is successful, 0 else.
153 static int write_acpi_int(acpi_handle handle, const char *method, int val,
154 struct acpi_buffer *output)
156 struct acpi_object_list params; //list of input parameters (an int here)
157 union acpi_object in_obj; //the only param we use
161 params.pointer = &in_obj;
162 in_obj.type = ACPI_TYPE_INTEGER;
163 in_obj.integer.value = val;
165 status = acpi_evaluate_object(handle, (char *)method, ¶ms, output);
166 return (status == AE_OK);
169 static int read_acpi_int(acpi_handle handle, const char *method, int *val,
170 struct acpi_object_list *params)
172 struct acpi_buffer output;
173 union acpi_object out_obj;
176 output.length = sizeof(out_obj);
177 output.pointer = &out_obj;
179 status = acpi_evaluate_object(handle, (char *)method, params, &output);
180 *val = out_obj.integer.value;
181 return (status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER);
184 /* Generic LED functions */
185 static int read_status(int mask)
187 return (hotk->status & mask) ? 1 : 0;
190 static void write_status(acpi_handle handle, int out, int mask,
193 hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
195 if (invert) /* invert target value */
198 if (handle && !write_acpi_int(handle, NULL, out, NULL))
199 printk(ASUS_WARNING " write failed\n");
202 /* /sys/class/led handlers */
203 #define ASUS_LED_HANDLER(object, mask, invert) \
204 static void object##_led_set(struct led_classdev *led_cdev, \
205 enum led_brightness value) \
207 object##_led_wk = value; \
208 queue_work(led_workqueue, &object##_led_work); \
210 static void object##_led_update(struct work_struct *ignored) \
212 int value = object##_led_wk; \
213 write_status(object##_set_handle, value, (mask), (invert)); \
216 ASUS_LED_HANDLER(mled, MLED_ON, 1);
217 ASUS_LED_HANDLER(pled, PLED_ON, 0);
218 ASUS_LED_HANDLER(rled, RLED_ON, 0);
219 ASUS_LED_HANDLER(tled, TLED_ON, 0);
222 * Platform device handlers
226 * We write our info in page, we begin at offset off and cannot write more
227 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
228 * number of bytes written in page
230 static ssize_t show_infos(struct device *dev,
231 struct device_attribute *attr, char *page)
235 char buf[16]; //enough for all info
237 * We use the easy way, we don't care of off and count, so we don't set eof
241 len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
242 len += sprintf(page + len, "Model reference : %s\n", hotk->name);
244 * The SFUN method probably allows the original driver to get the list
245 * of features supported by a given model. For now, 0x0100 or 0x0800
246 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
247 * The significance of others is yet to be found.
249 if (read_acpi_int(hotk->handle, "SFUN", &temp, NULL))
251 sprintf(page + len, "SFUN value : 0x%04x\n", temp);
253 * Another value for userspace: the ASYM method returns 0x02 for
254 * battery low and 0x04 for battery critical, its readings tend to be
255 * more accurate than those provided by _BST.
256 * Note: since not all the laptops provide this method, errors are
259 if (read_acpi_int(hotk->handle, "ASYM", &temp, NULL))
261 sprintf(page + len, "ASYM value : 0x%04x\n", temp);
263 snprintf(buf, 16, "%d", asus_info->length);
264 len += sprintf(page + len, "DSDT length : %s\n", buf);
265 snprintf(buf, 16, "%d", asus_info->checksum);
266 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
267 snprintf(buf, 16, "%d", asus_info->revision);
268 len += sprintf(page + len, "DSDT revision : %s\n", buf);
269 snprintf(buf, 7, "%s", asus_info->oem_id);
270 len += sprintf(page + len, "OEM id : %s\n", buf);
271 snprintf(buf, 9, "%s", asus_info->oem_table_id);
272 len += sprintf(page + len, "OEM table id : %s\n", buf);
273 snprintf(buf, 16, "%x", asus_info->oem_revision);
274 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
275 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
276 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
277 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
278 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
284 static int parse_arg(const char *buf, unsigned long count, int *val)
290 if (sscanf(buf, "%i", val) != 1)
295 static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
297 /* TODO Find a better way to handle events count. */
301 acpi_bus_generate_event(hotk->device, event,
302 hotk->event_count[event % 128]++);
307 #define ASUS_CREATE_DEVICE_ATTR(_name) \
308 struct device_attribute dev_attr_##_name = { \
310 .name = __stringify(_name), \
312 .owner = THIS_MODULE }, \
317 #define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
319 dev_attr_##_name.attr.mode = _mode; \
320 dev_attr_##_name.show = _show; \
321 dev_attr_##_name.store = _store; \
324 static ASUS_CREATE_DEVICE_ATTR(infos);
326 static struct attribute *asuspf_attributes[] = {
327 &dev_attr_infos.attr,
331 static struct attribute_group asuspf_attribute_group = {
332 .attrs = asuspf_attributes
335 static struct platform_driver asuspf_driver = {
337 .name = ASUS_HOTK_FILE,
338 .owner = THIS_MODULE,
342 static struct platform_device *asuspf_device;
345 static void asus_hotk_add_fs(void)
347 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
350 static int asus_handle_init(char *name, acpi_handle *handle,
351 char **paths, int num_paths)
356 for (i = 0; i < num_paths; i++) {
357 status = acpi_get_handle(NULL, paths[i], handle);
358 if (ACPI_SUCCESS(status))
366 #define ASUS_HANDLE_INIT(object) \
367 asus_handle_init(#object, &object##_handle, object##_paths, \
368 ARRAY_SIZE(object##_paths))
372 * This function is used to initialize the hotk with right values. In this
373 * method, we can make all the detection we want, and modify the hotk struct
375 static int asus_hotk_get_info(void)
377 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
378 struct acpi_buffer dsdt = { ACPI_ALLOCATE_BUFFER, NULL };
379 union acpi_object *model = NULL;
385 * Get DSDT headers early enough to allow for differentiating between
386 * models, but late enough to allow acpi_bus_register_driver() to fail
387 * before doing anything ACPI-specific. Should we encounter a machine,
388 * which needs special handling (i.e. its hotkey device has a different
389 * HID), this bit will be moved. A global variable asus_info contains
392 status = acpi_get_table(ACPI_TABLE_ID_DSDT, 1, &dsdt);
393 if (ACPI_FAILURE(status))
394 printk(ASUS_WARNING "Couldn't get the DSDT table header\n");
396 asus_info = dsdt.pointer;
398 /* We have to write 0 on init this far for all ASUS models */
399 if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
400 printk(ASUS_ERR "Hotkey initialization failed\n");
404 /* This needs to be called for some laptops to init properly */
405 if (!read_acpi_int(hotk->handle, "BSTS", &bsts_result, NULL))
406 printk(ASUS_WARNING "Error calling BSTS\n");
407 else if (bsts_result)
408 printk(ASUS_NOTICE "BSTS called, 0x%02x returned\n",
412 * Try to match the object returned by INIT to the specific model.
413 * Handle every possible object (or the lack of thereof) the DSDT
414 * writers might throw at us. When in trouble, we pass NULL to
415 * asus_model_match() and try something completely different.
417 if (buffer.pointer) {
418 model = buffer.pointer;
419 switch (model->type) {
420 case ACPI_TYPE_STRING:
421 string = model->string.pointer;
423 case ACPI_TYPE_BUFFER:
424 string = model->buffer.pointer;
431 hotk->name = kstrdup(string, GFP_KERNEL);
436 printk(ASUS_NOTICE " %s model detected\n", string);
438 ASUS_HANDLE_INIT(mled_set);
439 ASUS_HANDLE_INIT(tled_set);
440 ASUS_HANDLE_INIT(rled_set);
441 ASUS_HANDLE_INIT(pled_set);
448 static int asus_hotk_check(void)
452 result = acpi_bus_get_status(hotk->device);
456 if (hotk->device->status.present) {
457 result = asus_hotk_get_info();
459 printk(ASUS_ERR "Hotkey device not present, aborting\n");
466 static int asus_hotk_found;
468 static int asus_hotk_add(struct acpi_device *device)
470 acpi_status status = AE_OK;
476 printk(ASUS_NOTICE "Asus Laptop Support version %s\n",
477 ASUS_LAPTOP_VERSION);
479 hotk = kmalloc(sizeof(struct asus_hotk), GFP_KERNEL);
482 memset(hotk, 0, sizeof(struct asus_hotk));
484 hotk->handle = device->handle;
485 strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
486 strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
487 acpi_driver_data(device) = hotk;
488 hotk->device = device;
490 result = asus_hotk_check();
497 * We install the handler, it will receive the hotk in parameter, so, we
498 * could add other data to the hotk struct
500 status = acpi_install_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
501 asus_hotk_notify, hotk);
502 if (ACPI_FAILURE(status))
503 printk(ASUS_ERR "Error installing notify handler\n");
516 static int asus_hotk_remove(struct acpi_device *device, int type)
518 acpi_status status = 0;
520 if (!device || !acpi_driver_data(device))
523 status = acpi_remove_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
525 if (ACPI_FAILURE(status))
526 printk(ASUS_ERR "Error removing notify handler\n");
534 #define ASUS_LED_UNREGISTER(object) \
535 if(object##_led.class_dev \
536 && !IS_ERR(object##_led.class_dev)) \
537 led_classdev_unregister(&object##_led)
539 static void asus_led_exit(void)
541 ASUS_LED_UNREGISTER(mled);
542 ASUS_LED_UNREGISTER(tled);
543 ASUS_LED_UNREGISTER(pled);
544 ASUS_LED_UNREGISTER(rled);
546 destroy_workqueue(led_workqueue);
549 static void __exit asus_laptop_exit(void)
553 acpi_bus_unregister_driver(&asus_hotk_driver);
554 sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
555 platform_device_unregister(asuspf_device);
556 platform_driver_unregister(&asuspf_driver);
561 static int asus_led_register(acpi_handle handle,
562 struct led_classdev * ldev,
568 return led_classdev_register(dev, ldev);
570 #define ASUS_LED_REGISTER(object, device) \
571 asus_led_register(object##_set_handle, &object##_led, device)
573 static int asus_led_init(struct device * dev)
577 rv = ASUS_LED_REGISTER(mled, dev);
581 rv = ASUS_LED_REGISTER(tled, dev);
585 rv = ASUS_LED_REGISTER(rled, dev);
589 rv = ASUS_LED_REGISTER(pled, dev);
593 led_workqueue = create_singlethread_workqueue("led_workqueue");
600 static int __init asus_laptop_init(void)
608 if (!acpi_specific_hotkey_enabled) {
609 printk(ASUS_ERR "Using generic hotkey driver\n");
613 result = acpi_bus_register_driver(&asus_hotk_driver);
618 * This is a bit of a kludge. We only want this module loaded
619 * for ASUS systems, but there's currently no way to probe the
620 * ACPI namespace for ASUS HIDs. So we just return failure if
621 * we didn't find one, which will cause the module to be
624 if (!asus_hotk_found) {
625 acpi_bus_unregister_driver(&asus_hotk_driver);
629 dev = acpi_get_physical_device(hotk->device->handle);
631 result = asus_led_init(dev);
635 /* Register platform stuff */
636 result = platform_driver_register(&asuspf_driver);
638 goto fail_platform_driver;
640 asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
641 if (!asuspf_device) {
643 goto fail_platform_device1;
646 result = platform_device_add(asuspf_device);
648 goto fail_platform_device2;
650 result = sysfs_create_group(&asuspf_device->dev.kobj,
651 &asuspf_attribute_group);
658 platform_device_del(asuspf_device);
660 fail_platform_device2:
661 platform_device_put(asuspf_device);
663 fail_platform_device1:
664 platform_driver_unregister(&asuspf_driver);
666 fail_platform_driver:
674 module_init(asus_laptop_init);
675 module_exit(asus_laptop_exit);