blob: d0d5ee9b358f239a2db3e03a33e485e80bae7304 [file] [log] [blame]
Corentin Chary85091b72007-01-26 14:04:30 +01001/*
2 * asus-laptop.c - Asus Laptop Support
3 *
4 *
5 * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
6 * Copyright (C) 2006 Corentin Chary
7 *
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.
12 *
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.
17 *
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
21 *
22 *
23 * The development page for this driver is located at
24 * http://sourceforge.net/projects/acpi4asus/
25 *
26 * Credits:
27 * Pontus Fuchs - Helper functions, cleanup
28 * Johann Wiesner - Small compile fixes
29 * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
30 * Eric Burghard - LED display support for W1N
31 * Josh Green - Light Sens support
32 * Thomas Tuttle - His first patch for led support was very helpfull
33 *
34 */
35
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>
Corentin Charybe18cda2007-01-26 14:04:35 +010043#include <linux/leds.h>
Corentin Chary85091b72007-01-26 14:04:30 +010044#include <linux/platform_device.h>
45#include <acpi/acpi_drivers.h>
46#include <acpi/acpi_bus.h>
47#include <asm/uaccess.h>
48
49#define ASUS_LAPTOP_VERSION "0.40"
50
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."
57
Corentin Charybe18cda2007-01-26 14:04:35 +010058/*
59 * Flags for hotk status
60 */
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
65
Corentin Chary85091b72007-01-26 14:04:30 +010066#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
72
73MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
74MODULE_DESCRIPTION(ASUS_HOTK_NAME);
75MODULE_LICENSE("GPL");
76
77#define ASUS_HANDLE(object, paths...) \
78 static acpi_handle object##_handle = NULL; \
79 static char *object##_paths[] = { paths }
80
Corentin Charybe18cda2007-01-26 14:04:35 +010081/* LED */
82ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
83ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
84ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
85ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
86
Corentin Chary85091b72007-01-26 14:04:30 +010087/*
88 * This is the main structure, we can use it to store anything interesting
89 * about the hotk device
90 */
91struct asus_hotk {
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
97};
98
99/*
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
103 */
104static struct acpi_table_header *asus_info;
105
106/* The actual device the driver binds to */
107static struct asus_hotk *hotk;
108
109/*
110 * The hotkey driver declaration
111 */
112static int asus_hotk_add(struct acpi_device *device);
113static int asus_hotk_remove(struct acpi_device *device, int type);
114static struct acpi_driver asus_hotk_driver = {
115 .name = ASUS_HOTK_NAME,
116 .class = ASUS_HOTK_CLASS,
117 .ids = ASUS_HOTK_HID,
118 .ops = {
119 .add = asus_hotk_add,
120 .remove = asus_hotk_remove,
121 },
122};
123
Corentin Charybe18cda2007-01-26 14:04:35 +0100124/* 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. */
128static struct workqueue_struct *led_workqueue;
129
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, \
139 }
140
141ASUS_LED(mled, "mail");
142ASUS_LED(tled, "touchpad");
143ASUS_LED(rled, "record");
144ASUS_LED(pled, "phone");
145
Corentin Chary85091b72007-01-26 14:04:30 +0100146/*
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
150 *
151 * returns 1 if write is successful, 0 else.
152 */
153static int write_acpi_int(acpi_handle handle, const char *method, int val,
154 struct acpi_buffer *output)
155{
156 struct acpi_object_list params; //list of input parameters (an int here)
157 union acpi_object in_obj; //the only param we use
158 acpi_status status;
159
160 params.count = 1;
161 params.pointer = &in_obj;
162 in_obj.type = ACPI_TYPE_INTEGER;
163 in_obj.integer.value = val;
164
165 status = acpi_evaluate_object(handle, (char *)method, &params, output);
166 return (status == AE_OK);
167}
168
169static int read_acpi_int(acpi_handle handle, const char *method, int *val,
170 struct acpi_object_list *params)
171{
172 struct acpi_buffer output;
173 union acpi_object out_obj;
174 acpi_status status;
175
176 output.length = sizeof(out_obj);
177 output.pointer = &out_obj;
178
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);
182}
183
Corentin Charybe18cda2007-01-26 14:04:35 +0100184/* Generic LED functions */
185static int read_status(int mask)
186{
187 return (hotk->status & mask) ? 1 : 0;
188}
189
190static void write_status(acpi_handle handle, int out, int mask,
191 int invert)
192{
193 hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
194
195 if (invert) /* invert target value */
196 out = !out & 0x1;
197
198 if (handle && !write_acpi_int(handle, NULL, out, NULL))
199 printk(ASUS_WARNING " write failed\n");
200}
201
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) \
206 { \
207 object##_led_wk = value; \
208 queue_work(led_workqueue, &object##_led_work); \
209 } \
210 static void object##_led_update(struct work_struct *ignored) \
211 { \
212 int value = object##_led_wk; \
213 write_status(object##_set_handle, value, (mask), (invert)); \
214 }
215
216ASUS_LED_HANDLER(mled, MLED_ON, 1);
217ASUS_LED_HANDLER(pled, PLED_ON, 0);
218ASUS_LED_HANDLER(rled, RLED_ON, 0);
219ASUS_LED_HANDLER(tled, TLED_ON, 0);
220
Corentin Chary85091b72007-01-26 14:04:30 +0100221/*
222 * Platform device handlers
223 */
224
225/*
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
229 */
230static ssize_t show_infos(struct device *dev,
231 struct device_attribute *attr, char *page)
232{
233 int len = 0;
234 int temp;
235 char buf[16]; //enough for all info
236 /*
237 * We use the easy way, we don't care of off and count, so we don't set eof
238 * to 1
239 */
240
241 len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
242 len += sprintf(page + len, "Model reference : %s\n", hotk->name);
243 /*
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.
248 */
249 if (read_acpi_int(hotk->handle, "SFUN", &temp, NULL))
250 len +=
251 sprintf(page + len, "SFUN value : 0x%04x\n", temp);
252 /*
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
257 * silently ignored.
258 */
259 if (read_acpi_int(hotk->handle, "ASYM", &temp, NULL))
260 len +=
261 sprintf(page + len, "ASYM value : 0x%04x\n", temp);
262 if (asus_info) {
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);
279 }
280
281 return len;
282}
283
284static int parse_arg(const char *buf, unsigned long count, int *val)
285{
286 if (!count)
287 return 0;
288 if (count > 31)
289 return -EINVAL;
290 if (sscanf(buf, "%i", val) != 1)
291 return -EINVAL;
292 return count;
293}
294
295static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
296{
297 /* TODO Find a better way to handle events count. */
298 if (!hotk)
299 return;
300
301 acpi_bus_generate_event(hotk->device, event,
302 hotk->event_count[event % 128]++);
303
304 return;
305}
306
307#define ASUS_CREATE_DEVICE_ATTR(_name) \
308 struct device_attribute dev_attr_##_name = { \
309 .attr = { \
310 .name = __stringify(_name), \
311 .mode = 0, \
312 .owner = THIS_MODULE }, \
313 .show = NULL, \
314 .store = NULL, \
315 }
316
317#define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
318 do { \
319 dev_attr_##_name.attr.mode = _mode; \
320 dev_attr_##_name.show = _show; \
321 dev_attr_##_name.store = _store; \
322 } while(0)
323
324static ASUS_CREATE_DEVICE_ATTR(infos);
325
326static struct attribute *asuspf_attributes[] = {
327 &dev_attr_infos.attr,
328 NULL
329};
330
331static struct attribute_group asuspf_attribute_group = {
332 .attrs = asuspf_attributes
333};
334
335static struct platform_driver asuspf_driver = {
336 .driver = {
337 .name = ASUS_HOTK_FILE,
338 .owner = THIS_MODULE,
339 }
340};
341
342static struct platform_device *asuspf_device;
343
344
345static void asus_hotk_add_fs(void)
346{
347 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
348}
349
350static int asus_handle_init(char *name, acpi_handle *handle,
351 char **paths, int num_paths)
352{
353 int i;
354 acpi_status status;
355
356 for (i = 0; i < num_paths; i++) {
357 status = acpi_get_handle(NULL, paths[i], handle);
358 if (ACPI_SUCCESS(status))
359 return 0;
360 }
361
362 *handle = NULL;
363 return -ENODEV;
364}
365
366#define ASUS_HANDLE_INIT(object) \
367 asus_handle_init(#object, &object##_handle, object##_paths, \
368 ARRAY_SIZE(object##_paths))
369
370
371/*
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
374 */
375static int asus_hotk_get_info(void)
376{
377 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
378 struct acpi_buffer dsdt = { ACPI_ALLOCATE_BUFFER, NULL };
379 union acpi_object *model = NULL;
380 int bsts_result;
381 char *string = NULL;
382 acpi_status status;
383
384 /*
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
390 * the DSDT header.
391 */
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");
395 else
396 asus_info = dsdt.pointer;
397
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");
401 return -ENODEV;
402 }
403
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",
409 bsts_result);
410
411 /*
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.
416 */
417 if (buffer.pointer) {
418 model = buffer.pointer;
419 switch (model->type) {
420 case ACPI_TYPE_STRING:
421 string = model->string.pointer;
422 break;
423 case ACPI_TYPE_BUFFER:
424 string = model->buffer.pointer;
425 break;
426 default:
427 string = "";
428 break;
429 }
430 }
431 hotk->name = kstrdup(string, GFP_KERNEL);
432 if (!hotk->name)
433 return -ENOMEM;
434
435 if(*string)
436 printk(ASUS_NOTICE " %s model detected\n", string);
437
Corentin Charybe18cda2007-01-26 14:04:35 +0100438 ASUS_HANDLE_INIT(mled_set);
439 ASUS_HANDLE_INIT(tled_set);
440 ASUS_HANDLE_INIT(rled_set);
441 ASUS_HANDLE_INIT(pled_set);
442
Corentin Chary85091b72007-01-26 14:04:30 +0100443 kfree(model);
444
445 return AE_OK;
446}
447
448static int asus_hotk_check(void)
449{
450 int result = 0;
451
452 result = acpi_bus_get_status(hotk->device);
453 if (result)
454 return result;
455
456 if (hotk->device->status.present) {
457 result = asus_hotk_get_info();
458 } else {
459 printk(ASUS_ERR "Hotkey device not present, aborting\n");
460 return -EINVAL;
461 }
462
463 return result;
464}
465
466static int asus_hotk_found;
467
468static int asus_hotk_add(struct acpi_device *device)
469{
470 acpi_status status = AE_OK;
471 int result;
472
473 if (!device)
474 return -EINVAL;
475
476 printk(ASUS_NOTICE "Asus Laptop Support version %s\n",
477 ASUS_LAPTOP_VERSION);
478
479 hotk = kmalloc(sizeof(struct asus_hotk), GFP_KERNEL);
480 if (!hotk)
481 return -ENOMEM;
482 memset(hotk, 0, sizeof(struct asus_hotk));
483
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;
489
490 result = asus_hotk_check();
491 if (result)
492 goto end;
493
494 asus_hotk_add_fs();
495
496 /*
497 * We install the handler, it will receive the hotk in parameter, so, we
498 * could add other data to the hotk struct
499 */
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");
504
505 asus_hotk_found = 1;
506
507 end:
508 if (result) {
509 kfree(hotk->name);
510 kfree(hotk);
511 }
512
513 return result;
514}
515
516static int asus_hotk_remove(struct acpi_device *device, int type)
517{
518 acpi_status status = 0;
519
520 if (!device || !acpi_driver_data(device))
521 return -EINVAL;
522
523 status = acpi_remove_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
524 asus_hotk_notify);
525 if (ACPI_FAILURE(status))
526 printk(ASUS_ERR "Error removing notify handler\n");
527
528 kfree(hotk->name);
529 kfree(hotk);
530
531 return 0;
532}
533
Corentin Charybe18cda2007-01-26 14:04:35 +0100534#define ASUS_LED_UNREGISTER(object) \
535 if(object##_led.class_dev \
536 && !IS_ERR(object##_led.class_dev)) \
537 led_classdev_unregister(&object##_led)
538
539static void asus_led_exit(void)
540{
541 ASUS_LED_UNREGISTER(mled);
542 ASUS_LED_UNREGISTER(tled);
543 ASUS_LED_UNREGISTER(pled);
544 ASUS_LED_UNREGISTER(rled);
545
546 destroy_workqueue(led_workqueue);
547}
548
Corentin Chary85091b72007-01-26 14:04:30 +0100549static void __exit asus_laptop_exit(void)
550{
Corentin Charybe18cda2007-01-26 14:04:35 +0100551 asus_led_exit();
552
Corentin Chary85091b72007-01-26 14:04:30 +0100553 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);
557
558 kfree(asus_info);
559}
560
Corentin Charybe18cda2007-01-26 14:04:35 +0100561static int asus_led_register(acpi_handle handle,
562 struct led_classdev * ldev,
563 struct device * dev)
564{
565 if(!handle)
566 return 0;
567
568 return led_classdev_register(dev, ldev);
569}
570#define ASUS_LED_REGISTER(object, device) \
571 asus_led_register(object##_set_handle, &object##_led, device)
572
573static int asus_led_init(struct device * dev)
574{
575 int rv;
576
577 rv = ASUS_LED_REGISTER(mled, dev);
578 if(rv)
579 return rv;
580
581 rv = ASUS_LED_REGISTER(tled, dev);
582 if(rv)
583 return rv;
584
585 rv = ASUS_LED_REGISTER(rled, dev);
586 if(rv)
587 return rv;
588
589 rv = ASUS_LED_REGISTER(pled, dev);
590 if(rv)
591 return rv;
592
593 led_workqueue = create_singlethread_workqueue("led_workqueue");
594 if(!led_workqueue)
595 return -ENOMEM;
596
597 return 0;
598}
599
Corentin Chary85091b72007-01-26 14:04:30 +0100600static int __init asus_laptop_init(void)
601{
Corentin Charybe18cda2007-01-26 14:04:35 +0100602 struct device *dev;
Corentin Chary85091b72007-01-26 14:04:30 +0100603 int result;
604
605 if (acpi_disabled)
606 return -ENODEV;
607
608 if (!acpi_specific_hotkey_enabled) {
609 printk(ASUS_ERR "Using generic hotkey driver\n");
610 return -ENODEV;
611 }
612
613 result = acpi_bus_register_driver(&asus_hotk_driver);
614 if (result < 0)
615 return result;
616
617 /*
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
622 * unloaded.
623 */
624 if (!asus_hotk_found) {
625 acpi_bus_unregister_driver(&asus_hotk_driver);
626 return -ENODEV;
627 }
628
Corentin Charybe18cda2007-01-26 14:04:35 +0100629 dev = acpi_get_physical_device(hotk->device->handle);
630
631 result = asus_led_init(dev);
632 if(result)
633 goto fail_led;
634
Corentin Chary85091b72007-01-26 14:04:30 +0100635 /* Register platform stuff */
636 result = platform_driver_register(&asuspf_driver);
637 if (result)
638 goto fail_platform_driver;
639
640 asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
641 if (!asuspf_device) {
642 result = -ENOMEM;
643 goto fail_platform_device1;
644 }
645
646 result = platform_device_add(asuspf_device);
647 if (result)
648 goto fail_platform_device2;
649
650 result = sysfs_create_group(&asuspf_device->dev.kobj,
651 &asuspf_attribute_group);
652 if (result)
653 goto fail_sysfs;
654
655 return 0;
656
657fail_sysfs:
658 platform_device_del(asuspf_device);
659
660fail_platform_device2:
661 platform_device_put(asuspf_device);
662
663fail_platform_device1:
664 platform_driver_unregister(&asuspf_driver);
665
666fail_platform_driver:
Corentin Charybe18cda2007-01-26 14:04:35 +0100667 asus_led_exit();
668
669fail_led:
Corentin Chary85091b72007-01-26 14:04:30 +0100670
671 return result;
672}
673
674module_init(asus_laptop_init);
675module_exit(asus_laptop_exit);