]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/misc/asus-laptop.c
979daa6755a73ff514be160e08763f58b0e69525
[linux-2.6-omap-h63xx.git] / drivers / misc / asus-laptop.c
1 /*
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>
43 #include <linux/backlight.h>
44 #include <linux/fb.h>
45 #include <linux/leds.h>
46 #include <linux/platform_device.h>
47 #include <acpi/acpi_drivers.h>
48 #include <acpi/acpi_bus.h>
49 #include <asm/uaccess.h>
50
51 #define ASUS_LAPTOP_VERSION "0.40"
52
53 #define ASUS_HOTK_NAME          "Asus Laptop Support"
54 #define ASUS_HOTK_CLASS         "hotkey"
55 #define ASUS_HOTK_DEVICE_NAME   "Hotkey"
56 #define ASUS_HOTK_HID           "ATK0100"
57 #define ASUS_HOTK_FILE          "asus-laptop"
58 #define ASUS_HOTK_PREFIX        "\\_SB.ATKD."
59
60 /*
61  * Some events we use, same for all Asus
62  */
63 #define ATKD_BR_UP       0x10
64 #define ATKD_BR_DOWN     0x20
65 #define ATKD_LCD_ON      0x33
66 #define ATKD_LCD_OFF     0x34
67
68 /*
69  * Known bits returned by \_SB.ATKD.HWRS
70  */
71 #define WL_HWRS     0x80
72 #define BT_HWRS     0x100
73
74 /*
75  * Flags for hotk status
76  * WL_ON and BT_ON are also used for wireless_status()
77  */
78 #define WL_ON       0x01        //internal Wifi
79 #define BT_ON       0x02        //internal Bluetooth
80 #define MLED_ON     0x04        //mail LED
81 #define TLED_ON     0x08        //touchpad LED
82 #define RLED_ON     0x10        //Record LED
83 #define PLED_ON     0x20        //Phone LED
84 #define LCD_ON      0x40        //LCD backlight
85
86 #define ASUS_LOG    ASUS_HOTK_FILE ": "
87 #define ASUS_ERR    KERN_ERR    ASUS_LOG
88 #define ASUS_WARNING    KERN_WARNING    ASUS_LOG
89 #define ASUS_NOTICE KERN_NOTICE ASUS_LOG
90 #define ASUS_INFO   KERN_INFO   ASUS_LOG
91 #define ASUS_DEBUG  KERN_DEBUG  ASUS_LOG
92
93 MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
94 MODULE_DESCRIPTION(ASUS_HOTK_NAME);
95 MODULE_LICENSE("GPL");
96
97 #define ASUS_HANDLE(object, paths...)                                   \
98         static acpi_handle  object##_handle = NULL;                     \
99         static char *object##_paths[] = { paths }
100
101 /* LED */
102 ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
103 ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
104 ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
105 ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
106
107 /* Bluetooth and WLAN
108  * WLED and BLED are not handled like other XLED, because in some dsdt
109  * they also control the WLAN/Bluetooth device.
110  */
111 ASUS_HANDLE(wl_switch, ASUS_HOTK_PREFIX "WLED");
112 ASUS_HANDLE(bt_switch, ASUS_HOTK_PREFIX "BLED");
113 ASUS_HANDLE(wireless_status, ASUS_HOTK_PREFIX "RSTS"); /* All new models */
114
115 /* Brightness */
116 ASUS_HANDLE(brightness_set, ASUS_HOTK_PREFIX "SPLV");
117 ASUS_HANDLE(brightness_get, ASUS_HOTK_PREFIX "GPLV");
118
119 /* Backlight */
120 ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
121             "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
122             "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
123             "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
124             "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
125             "\\_SB.PCI0.PX40.Q10", /* S1x */
126             "\\Q10");  /* A2x, L2D, L3D, M2E */
127
128 /* Display */
129 ASUS_HANDLE(display_set, ASUS_HOTK_PREFIX "SDSP");
130 ASUS_HANDLE(display_get,
131             "\\_SB.PCI0.P0P1.VGA.GETD", /*  A6B, A6K A6R A7D F3JM L4R M6R A3G
132                                             M6A M6V VX-1 V6J V6V W3Z */
133             "\\_SB.PCI0.P0P2.VGA.GETD", /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V
134                                            S5A M5A z33A W1Jc W2V */
135             "\\_SB.PCI0.P0P3.VGA.GETD", /* A6V A6Q */
136             "\\_SB.PCI0.P0PA.VGA.GETD", /* A6T, A6M */
137             "\\_SB.PCI0.PCI1.VGAC.NMAP", /* L3C */
138             "\\_SB.PCI0.VGA.GETD", /* Z96F */
139             "\\ACTD", /* A2D */
140             "\\ADVG", /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
141             "\\DNXT", /* P30 */
142             "\\INFB", /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
143             "\\SSTE"); /* A3F A6F A3N A3L M6N W3N W6A */
144
145 /*
146  * This is the main structure, we can use it to store anything interesting
147  * about the hotk device
148  */
149 struct asus_hotk {
150         char *name; //laptop name
151         struct acpi_device *device;     //the device we are in
152         acpi_handle handle;     //the handle of the hotk device
153         char status;            //status of the hotk, for LEDs, ...
154         u16 event_count[128];   //count for each event TODO make this better
155 };
156
157 /*
158  * This header is made available to allow proper configuration given model,
159  * revision number , ... this info cannot go in struct asus_hotk because it is
160  * available before the hotk
161  */
162 static struct acpi_table_header *asus_info;
163
164 /* The actual device the driver binds to */
165 static struct asus_hotk *hotk;
166
167 /*
168  * The hotkey driver declaration
169  */
170 static int asus_hotk_add(struct acpi_device *device);
171 static int asus_hotk_remove(struct acpi_device *device, int type);
172 static struct acpi_driver asus_hotk_driver = {
173         .name = ASUS_HOTK_NAME,
174         .class = ASUS_HOTK_CLASS,
175         .ids = ASUS_HOTK_HID,
176         .ops = {
177                 .add = asus_hotk_add,
178                 .remove = asus_hotk_remove,
179                 },
180 };
181
182 /* The backlight device /sys/class/backlight */
183 static struct backlight_device *asus_backlight_device;
184
185 /*
186  * The backlight class declaration
187  */
188 static int read_brightness(struct backlight_device *bd);
189 static int update_bl_status(struct backlight_device *bd);
190 static struct backlight_properties asusbl_data = {
191                .owner          = THIS_MODULE,
192                .get_brightness = read_brightness,
193                .update_status  = update_bl_status,
194                .max_brightness = 15,
195 };
196
197 /* These functions actually update the LED's, and are called from a
198  * workqueue. By doing this as separate work rather than when the LED
199  * subsystem asks, we avoid messing with the Asus ACPI stuff during a
200  * potentially bad time, such as a timer interrupt. */
201 static struct workqueue_struct *led_workqueue;
202
203 #define ASUS_LED(object, ledname)                                       \
204         static void object##_led_set(struct led_classdev *led_cdev,     \
205                                      enum led_brightness value);        \
206         static void object##_led_update(struct work_struct *ignored);   \
207         static int object##_led_wk;                                     \
208         DECLARE_WORK(object##_led_work, object##_led_update);           \
209         static struct led_classdev object##_led = {                     \
210                 .name           = "asus:" ledname,                      \
211                 .brightness_set = object##_led_set,                     \
212         }
213
214 ASUS_LED(mled, "mail");
215 ASUS_LED(tled, "touchpad");
216 ASUS_LED(rled, "record");
217 ASUS_LED(pled, "phone");
218
219 /*
220  * This function evaluates an ACPI method, given an int as parameter, the
221  * method is searched within the scope of the handle, can be NULL. The output
222  * of the method is written is output, which can also be NULL
223  *
224  * returns 1 if write is successful, 0 else.
225  */
226 static int write_acpi_int(acpi_handle handle, const char *method, int val,
227                           struct acpi_buffer *output)
228 {
229         struct acpi_object_list params; //list of input parameters (an int here)
230         union acpi_object in_obj;       //the only param we use
231         acpi_status status;
232
233         params.count = 1;
234         params.pointer = &in_obj;
235         in_obj.type = ACPI_TYPE_INTEGER;
236         in_obj.integer.value = val;
237
238         status = acpi_evaluate_object(handle, (char *)method, &params, output);
239         return (status == AE_OK);
240 }
241
242 static int read_acpi_int(acpi_handle handle, const char *method, int *val,
243                          struct acpi_object_list *params)
244 {
245         struct acpi_buffer output;
246         union acpi_object out_obj;
247         acpi_status status;
248
249         output.length = sizeof(out_obj);
250         output.pointer = &out_obj;
251
252         status = acpi_evaluate_object(handle, (char *)method, params, &output);
253         *val = out_obj.integer.value;
254         return (status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER);
255 }
256
257 static int read_wireless_status(int mask) {
258         int status;
259
260         if (!wireless_status_handle)
261                 return (hotk->status & mask) ? 1 : 0;
262
263         if (read_acpi_int(wireless_status_handle, NULL, &status, NULL)) {
264                 return (status & mask) ? 1 : 0;
265         } else
266                 printk(ASUS_WARNING "Error reading Wireless status\n");
267
268         return (hotk->status & mask) ? 1 : 0;
269 }
270
271 /* Generic LED functions */
272 static int read_status(int mask)
273 {
274         /* There is a special method for both wireless devices */
275         if (mask == BT_ON || mask == WL_ON)
276                 return read_wireless_status(mask);
277
278         return (hotk->status & mask) ? 1 : 0;
279 }
280
281 static void write_status(acpi_handle handle, int out, int mask,
282                          int invert)
283 {
284         hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
285
286         if (invert)             /* invert target value */
287                 out = !out & 0x1;
288
289         if (handle && !write_acpi_int(handle, NULL, out, NULL))
290                 printk(ASUS_WARNING " write failed\n");
291 }
292
293 /* /sys/class/led handlers */
294 #define ASUS_LED_HANDLER(object, mask, invert)                          \
295         static void object##_led_set(struct led_classdev *led_cdev,     \
296                                      enum led_brightness value)         \
297         {                                                               \
298                 object##_led_wk = value;                                \
299                 queue_work(led_workqueue, &object##_led_work);          \
300         }                                                               \
301         static void object##_led_update(struct work_struct *ignored)    \
302         {                                                               \
303                 int value = object##_led_wk;                            \
304                 write_status(object##_set_handle, value, (mask), (invert)); \
305         }
306
307 ASUS_LED_HANDLER(mled, MLED_ON, 1);
308 ASUS_LED_HANDLER(pled, PLED_ON, 0);
309 ASUS_LED_HANDLER(rled, RLED_ON, 0);
310 ASUS_LED_HANDLER(tled, TLED_ON, 0);
311
312 static int get_lcd_state(void)
313 {
314         return read_status(LCD_ON);
315 }
316
317 static int set_lcd_state(int value)
318 {
319         int lcd = 0;
320         acpi_status status = 0;
321
322         lcd = value ? 1 : 0;
323
324         if (lcd == get_lcd_state())
325                 return 0;
326
327         if(lcd_switch_handle) {
328                 status = acpi_evaluate_object(lcd_switch_handle,
329                                               NULL, NULL, NULL);
330
331                 if (ACPI_FAILURE(status))
332                         printk(ASUS_WARNING "Error switching LCD\n");
333         }
334
335         write_status(NULL, lcd, LCD_ON, 0);
336         return 0;
337 }
338
339 static void lcd_blank(int blank)
340 {
341         struct backlight_device *bd = asus_backlight_device;
342
343         if(bd) {
344                 down(&bd->sem);
345                 if(likely(bd->props)) {
346                         bd->props->power = blank;
347                         if(likely(bd->props->update_status))
348                            bd->props->update_status(bd);
349                 }
350                 up(&bd->sem);
351         }
352 }
353
354 static int read_brightness(struct backlight_device *bd)
355 {
356         int value;
357
358         if (!read_acpi_int(brightness_get_handle, NULL, &value, NULL))
359                 printk(ASUS_WARNING "Error reading brightness\n");
360
361         return value;
362 }
363
364 static int set_brightness(struct backlight_device *bd, int value)
365 {
366         int ret = 0;
367
368         value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
369         /* 0 <= value <= 15 */
370
371         if (!write_acpi_int(brightness_set_handle, NULL, value, NULL)) {
372                 printk(ASUS_WARNING "Error changing brightness\n");
373                 ret = -EIO;
374         }
375
376         return ret;
377 }
378
379 static int update_bl_status(struct backlight_device *bd)
380 {
381         int rv;
382         int value = bd->props->brightness;
383
384         rv = set_brightness(bd, value);
385         if(rv)
386                 return rv;
387
388         value = (bd->props->power == FB_BLANK_UNBLANK) ? 1 : 0;
389         return set_lcd_state(value);
390 }
391
392 /*
393  * Platform device handlers
394  */
395
396 /*
397  * We write our info in page, we begin at offset off and cannot write more
398  * than count bytes. We set eof to 1 if we handle those 2 values. We return the
399  * number of bytes written in page
400  */
401 static ssize_t show_infos(struct device *dev,
402                          struct device_attribute *attr, char *page)
403 {
404         int len = 0;
405         int temp;
406         char buf[16];           //enough for all info
407         /*
408          * We use the easy way, we don't care of off and count, so we don't set eof
409          * to 1
410          */
411
412         len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
413         len += sprintf(page + len, "Model reference    : %s\n", hotk->name);
414         /*
415          * The SFUN method probably allows the original driver to get the list
416          * of features supported by a given model. For now, 0x0100 or 0x0800
417          * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
418          * The significance of others is yet to be found.
419          */
420         if (read_acpi_int(hotk->handle, "SFUN", &temp, NULL))
421                 len +=
422                     sprintf(page + len, "SFUN value         : 0x%04x\n", temp);
423         /*
424          * Another value for userspace: the ASYM method returns 0x02 for
425          * battery low and 0x04 for battery critical, its readings tend to be
426          * more accurate than those provided by _BST.
427          * Note: since not all the laptops provide this method, errors are
428          * silently ignored.
429          */
430         if (read_acpi_int(hotk->handle, "ASYM", &temp, NULL))
431                 len +=
432                     sprintf(page + len, "ASYM value         : 0x%04x\n", temp);
433         if (asus_info) {
434                 snprintf(buf, 16, "%d", asus_info->length);
435                 len += sprintf(page + len, "DSDT length        : %s\n", buf);
436                 snprintf(buf, 16, "%d", asus_info->checksum);
437                 len += sprintf(page + len, "DSDT checksum      : %s\n", buf);
438                 snprintf(buf, 16, "%d", asus_info->revision);
439                 len += sprintf(page + len, "DSDT revision      : %s\n", buf);
440                 snprintf(buf, 7, "%s", asus_info->oem_id);
441                 len += sprintf(page + len, "OEM id             : %s\n", buf);
442                 snprintf(buf, 9, "%s", asus_info->oem_table_id);
443                 len += sprintf(page + len, "OEM table id       : %s\n", buf);
444                 snprintf(buf, 16, "%x", asus_info->oem_revision);
445                 len += sprintf(page + len, "OEM revision       : 0x%s\n", buf);
446                 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
447                 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
448                 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
449                 len += sprintf(page + len, "ASL comp revision  : 0x%s\n", buf);
450         }
451
452         return len;
453 }
454
455 static int parse_arg(const char *buf, unsigned long count, int *val)
456 {
457         if (!count)
458                 return 0;
459         if (count > 31)
460                 return -EINVAL;
461         if (sscanf(buf, "%i", val) != 1)
462                 return -EINVAL;
463         return count;
464 }
465
466 static ssize_t store_status(const char *buf, size_t count,
467                             acpi_handle handle, int mask, int invert)
468 {
469         int rv, value;
470         int out = 0;
471
472         rv = parse_arg(buf, count, &value);
473         if (rv > 0)
474                 out = value ? 1 : 0;
475
476         write_status(handle, out, mask, invert);
477
478         return rv;
479 }
480
481 /*
482  * WLAN
483  */
484 static ssize_t show_wlan(struct device *dev,
485                          struct device_attribute *attr, char *buf)
486 {
487         return sprintf(buf, "%d\n", read_status(WL_ON));
488 }
489
490 static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
491                           const char *buf, size_t count)
492 {
493         return store_status(buf, count, wl_switch_handle, WL_ON, 0);
494 }
495
496 /*
497  * Bluetooth
498  */
499 static ssize_t show_bluetooth(struct device *dev,
500                               struct device_attribute *attr, char *buf)
501 {
502         return sprintf(buf, "%d\n", read_status(BT_ON));
503 }
504
505 static ssize_t store_bluetooth(struct device *dev, struct device_attribute *attr,
506                                const char *buf, size_t count)
507 {
508         return store_status(buf, count, bt_switch_handle, BT_ON, 0);
509 }
510
511 /*
512  * Display
513  */
514 static void set_display(int value)
515 {
516         /* no sanity check needed for now */
517         if (!write_acpi_int(display_set_handle, NULL, value, NULL))
518                 printk(ASUS_WARNING "Error setting display\n");
519         return;
520 }
521
522 static int read_display(void)
523 {
524         int value = 0;
525
526         /* In most of the case, we know how to set the display, but sometime
527            we can't read it */
528         if(display_get_handle) {
529                 if (!read_acpi_int(display_get_handle, NULL, &value, NULL))
530                         printk(ASUS_WARNING "Error reading display status\n");
531         }
532
533         value &= 0x0F;          /* needed for some models, shouldn't hurt others */
534
535         return value;
536 }
537 /*
538  * Now, *this* one could be more user-friendly, but so far, no-one has
539  * complained. The significance of bits is the same as in store_disp()
540  */
541 static ssize_t show_disp(struct device *dev,
542                          struct device_attribute *attr, char *buf)
543 {
544         return sprintf(buf, "%d\n", read_display());
545 }
546
547 /*
548  * Experimental support for display switching. As of now: 1 should activate
549  * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
550  * Any combination (bitwise) of these will suffice. I never actually tested 4
551  * displays hooked up simultaneously, so be warned. See the acpi4asus README
552  * for more info.
553  */
554 static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
555                           const char *buf, size_t count)
556 {
557         int rv, value;
558
559         rv = parse_arg(buf, count, &value);
560         if (rv > 0)
561                 set_display(value);
562         return rv;
563 }
564
565 static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
566 {
567         /* TODO Find a better way to handle events count. */
568         if (!hotk)
569                 return;
570
571         /*
572          * We need to tell the backlight device when the backlight power is
573          * switched
574          */
575         if (event == ATKD_LCD_ON) {
576                 write_status(NULL, 1, LCD_ON, 0);
577                 lcd_blank(FB_BLANK_UNBLANK);
578         } else if(event == ATKD_LCD_OFF) {
579                 write_status(NULL, 0, LCD_ON, 0);
580                 lcd_blank(FB_BLANK_POWERDOWN);
581         }
582
583         acpi_bus_generate_event(hotk->device, event,
584                                 hotk->event_count[event % 128]++);
585
586         return;
587 }
588
589 #define ASUS_CREATE_DEVICE_ATTR(_name)                                  \
590         struct device_attribute dev_attr_##_name = {                    \
591                 .attr = {                                               \
592                         .name = __stringify(_name),                     \
593                         .mode = 0,                                      \
594                         .owner = THIS_MODULE },                         \
595                 .show   = NULL,                                         \
596                 .store  = NULL,                                         \
597         }
598
599 #define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store)               \
600         do {                                                            \
601                 dev_attr_##_name.attr.mode = _mode;                     \
602                 dev_attr_##_name.show = _show;                          \
603                 dev_attr_##_name.store = _store;                        \
604         } while(0)
605
606 static ASUS_CREATE_DEVICE_ATTR(infos);
607 static ASUS_CREATE_DEVICE_ATTR(wlan);
608 static ASUS_CREATE_DEVICE_ATTR(bluetooth);
609 static ASUS_CREATE_DEVICE_ATTR(display);
610
611 static struct attribute *asuspf_attributes[] = {
612         &dev_attr_infos.attr,
613         &dev_attr_wlan.attr,
614         &dev_attr_bluetooth.attr,
615         &dev_attr_display.attr,
616         NULL
617 };
618
619 static struct attribute_group asuspf_attribute_group = {
620         .attrs = asuspf_attributes
621 };
622
623 static struct platform_driver asuspf_driver = {
624         .driver = {
625                 .name = ASUS_HOTK_FILE,
626                 .owner = THIS_MODULE,
627         }
628 };
629
630 static struct platform_device *asuspf_device;
631
632
633 static void asus_hotk_add_fs(void)
634 {
635         ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
636
637         if (wl_switch_handle)
638                 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
639
640         if (bt_switch_handle)
641                 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
642                                      show_bluetooth, store_bluetooth);
643
644         if (display_set_handle && display_get_handle)
645                 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
646         else if(display_set_handle)
647                 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
648
649 }
650
651 static int asus_handle_init(char *name, acpi_handle *handle,
652                             char **paths, int num_paths)
653 {
654         int i;
655         acpi_status status;
656
657         for (i = 0; i < num_paths; i++) {
658                 status = acpi_get_handle(NULL, paths[i], handle);
659                 if (ACPI_SUCCESS(status))
660                         return 0;
661         }
662
663         *handle = NULL;
664         return -ENODEV;
665 }
666
667 #define ASUS_HANDLE_INIT(object)                                        \
668         asus_handle_init(#object, &object##_handle, object##_paths,     \
669                          ARRAY_SIZE(object##_paths))
670
671
672 /*
673  * This function is used to initialize the hotk with right values. In this
674  * method, we can make all the detection we want, and modify the hotk struct
675  */
676 static int asus_hotk_get_info(void)
677 {
678         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
679         struct acpi_buffer dsdt = { ACPI_ALLOCATE_BUFFER, NULL };
680         union acpi_object *model = NULL;
681         int bsts_result, hwrs_result;
682         char *string = NULL;
683         acpi_status status;
684
685         /*
686          * Get DSDT headers early enough to allow for differentiating between
687          * models, but late enough to allow acpi_bus_register_driver() to fail
688          * before doing anything ACPI-specific. Should we encounter a machine,
689          * which needs special handling (i.e. its hotkey device has a different
690          * HID), this bit will be moved. A global variable asus_info contains
691          * the DSDT header.
692          */
693         status = acpi_get_table(ACPI_TABLE_ID_DSDT, 1, &dsdt);
694         if (ACPI_FAILURE(status))
695                 printk(ASUS_WARNING "Couldn't get the DSDT table header\n");
696         else
697                 asus_info = dsdt.pointer;
698
699         /* We have to write 0 on init this far for all ASUS models */
700         if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
701                 printk(ASUS_ERR "Hotkey initialization failed\n");
702                 return -ENODEV;
703         }
704
705         /* This needs to be called for some laptops to init properly */
706         if (!read_acpi_int(hotk->handle, "BSTS", &bsts_result, NULL))
707                 printk(ASUS_WARNING "Error calling BSTS\n");
708         else if (bsts_result)
709                 printk(ASUS_NOTICE "BSTS called, 0x%02x returned\n",
710                        bsts_result);
711
712         /*
713          * Try to match the object returned by INIT to the specific model.
714          * Handle every possible object (or the lack of thereof) the DSDT
715          * writers might throw at us. When in trouble, we pass NULL to
716          * asus_model_match() and try something completely different.
717          */
718         if (buffer.pointer) {
719                 model = buffer.pointer;
720                 switch (model->type) {
721                 case ACPI_TYPE_STRING:
722                         string = model->string.pointer;
723                         break;
724                 case ACPI_TYPE_BUFFER:
725                         string = model->buffer.pointer;
726                         break;
727                 default:
728                         string = "";
729                         break;
730                 }
731         }
732         hotk->name = kstrdup(string, GFP_KERNEL);
733         if (!hotk->name)
734                 return -ENOMEM;
735
736         if(*string)
737                 printk(ASUS_NOTICE "  %s model detected\n", string);
738
739         ASUS_HANDLE_INIT(mled_set);
740         ASUS_HANDLE_INIT(tled_set);
741         ASUS_HANDLE_INIT(rled_set);
742         ASUS_HANDLE_INIT(pled_set);
743
744         /*
745          * The HWRS method return informations about the hardware.
746          * 0x80 bit is for WLAN, 0x100 for Bluetooth.
747          * The significance of others is yet to be found.
748          * If we don't find the method, we assume the device are present.
749          */
750         if (!read_acpi_int(hotk->handle, "HRWS", &hwrs_result, NULL))
751                 hwrs_result = WL_HWRS | BT_HWRS;
752
753         if(hwrs_result & WL_HWRS)
754                 ASUS_HANDLE_INIT(wl_switch);
755         if(hwrs_result & BT_HWRS)
756                 ASUS_HANDLE_INIT(bt_switch);
757
758         ASUS_HANDLE_INIT(wireless_status);
759
760         ASUS_HANDLE_INIT(brightness_set);
761         ASUS_HANDLE_INIT(brightness_get);
762
763         ASUS_HANDLE_INIT(lcd_switch);
764
765         ASUS_HANDLE_INIT(display_set);
766         ASUS_HANDLE_INIT(display_get);
767
768         kfree(model);
769
770         return AE_OK;
771 }
772
773 static int asus_hotk_check(void)
774 {
775         int result = 0;
776
777         result = acpi_bus_get_status(hotk->device);
778         if (result)
779                 return result;
780
781         if (hotk->device->status.present) {
782                 result = asus_hotk_get_info();
783         } else {
784                 printk(ASUS_ERR "Hotkey device not present, aborting\n");
785                 return -EINVAL;
786         }
787
788         return result;
789 }
790
791 static int asus_hotk_found;
792
793 static int asus_hotk_add(struct acpi_device *device)
794 {
795         acpi_status status = AE_OK;
796         int result;
797
798         if (!device)
799                 return -EINVAL;
800
801         printk(ASUS_NOTICE "Asus Laptop Support version %s\n",
802                ASUS_LAPTOP_VERSION);
803
804         hotk = kmalloc(sizeof(struct asus_hotk), GFP_KERNEL);
805         if (!hotk)
806                 return -ENOMEM;
807         memset(hotk, 0, sizeof(struct asus_hotk));
808
809         hotk->handle = device->handle;
810         strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
811         strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
812         acpi_driver_data(device) = hotk;
813         hotk->device = device;
814
815         result = asus_hotk_check();
816         if (result)
817                 goto end;
818
819         asus_hotk_add_fs();
820
821         /*
822          * We install the handler, it will receive the hotk in parameter, so, we
823          * could add other data to the hotk struct
824          */
825         status = acpi_install_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
826                                              asus_hotk_notify, hotk);
827         if (ACPI_FAILURE(status))
828                 printk(ASUS_ERR "Error installing notify handler\n");
829
830         asus_hotk_found = 1;
831
832         /* WLED and BLED are on by default */
833         write_status(bt_switch_handle, 1, BT_ON, 0);
834         write_status(wl_switch_handle, 1, WL_ON, 0);
835
836         /* LCD Backlight is on by default */
837         write_status(NULL, 1, LCD_ON, 0);
838
839       end:
840         if (result) {
841                 kfree(hotk->name);
842                 kfree(hotk);
843         }
844
845         return result;
846 }
847
848 static int asus_hotk_remove(struct acpi_device *device, int type)
849 {
850         acpi_status status = 0;
851
852         if (!device || !acpi_driver_data(device))
853                 return -EINVAL;
854
855         status = acpi_remove_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
856                                             asus_hotk_notify);
857         if (ACPI_FAILURE(status))
858                 printk(ASUS_ERR "Error removing notify handler\n");
859
860         kfree(hotk->name);
861         kfree(hotk);
862
863         return 0;
864 }
865
866 static void asus_backlight_exit(void)
867 {
868         if(asus_backlight_device)
869                 backlight_device_unregister(asus_backlight_device);
870 }
871
872 #define  ASUS_LED_UNREGISTER(object)                            \
873         if(object##_led.class_dev                               \
874            && !IS_ERR(object##_led.class_dev))                  \
875                 led_classdev_unregister(&object##_led)
876
877 static void asus_led_exit(void)
878 {
879         ASUS_LED_UNREGISTER(mled);
880         ASUS_LED_UNREGISTER(tled);
881         ASUS_LED_UNREGISTER(pled);
882         ASUS_LED_UNREGISTER(rled);
883
884         destroy_workqueue(led_workqueue);
885 }
886
887 static void __exit asus_laptop_exit(void)
888 {
889         asus_backlight_exit();
890         asus_led_exit();
891
892         acpi_bus_unregister_driver(&asus_hotk_driver);
893         sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
894         platform_device_unregister(asuspf_device);
895         platform_driver_unregister(&asuspf_driver);
896
897         kfree(asus_info);
898 }
899
900 static int asus_backlight_init(struct device * dev)
901 {
902         struct backlight_device *bd;
903
904         if(brightness_set_handle && lcd_switch_handle) {
905                 bd = backlight_device_register (ASUS_HOTK_FILE, dev,
906                                                 NULL, &asusbl_data);
907                 if (IS_ERR (bd)) {
908                         printk(ASUS_ERR
909                                "Could not register asus backlight device\n");
910                         asus_backlight_device = NULL;
911                         return PTR_ERR(bd);
912                 }
913
914                 asus_backlight_device = bd;
915
916                 down(&bd->sem);
917                 if(likely(bd->props)) {
918                         bd->props->brightness = read_brightness(NULL);
919                         bd->props->power = FB_BLANK_UNBLANK;
920                         if(likely(bd->props->update_status))
921                            bd->props->update_status(bd);
922                 }
923                 up(&bd->sem);
924         }
925         return 0;
926 }
927
928 static int asus_led_register(acpi_handle handle,
929                              struct led_classdev * ldev,
930                              struct device * dev)
931 {
932         if(!handle)
933                 return 0;
934
935         return led_classdev_register(dev, ldev);
936 }
937 #define ASUS_LED_REGISTER(object, device)                               \
938         asus_led_register(object##_set_handle, &object##_led, device)
939
940 static int asus_led_init(struct device * dev)
941 {
942         int rv;
943
944         rv = ASUS_LED_REGISTER(mled, dev);
945         if(rv)
946                 return rv;
947
948         rv = ASUS_LED_REGISTER(tled, dev);
949         if(rv)
950                 return rv;
951
952         rv = ASUS_LED_REGISTER(rled, dev);
953         if(rv)
954                 return rv;
955
956         rv = ASUS_LED_REGISTER(pled, dev);
957         if(rv)
958                 return rv;
959
960         led_workqueue = create_singlethread_workqueue("led_workqueue");
961         if(!led_workqueue)
962                 return -ENOMEM;
963
964         return 0;
965 }
966
967 static int __init asus_laptop_init(void)
968 {
969         struct device *dev;
970         int result;
971
972         if (acpi_disabled)
973                 return -ENODEV;
974
975         if (!acpi_specific_hotkey_enabled) {
976                 printk(ASUS_ERR "Using generic hotkey driver\n");
977                 return -ENODEV;
978         }
979
980         result = acpi_bus_register_driver(&asus_hotk_driver);
981         if (result < 0)
982                 return result;
983
984         /*
985          * This is a bit of a kludge.  We only want this module loaded
986          * for ASUS systems, but there's currently no way to probe the
987          * ACPI namespace for ASUS HIDs.  So we just return failure if
988          * we didn't find one, which will cause the module to be
989          * unloaded.
990          */
991         if (!asus_hotk_found) {
992                 acpi_bus_unregister_driver(&asus_hotk_driver);
993                 return -ENODEV;
994         }
995
996         dev = acpi_get_physical_device(hotk->device->handle);
997
998         result = asus_backlight_init(dev);
999         if(result)
1000                 goto fail_backlight;
1001
1002         result = asus_led_init(dev);
1003         if(result)
1004                 goto fail_led;
1005
1006         /* Register platform stuff */
1007         result = platform_driver_register(&asuspf_driver);
1008         if (result)
1009                 goto fail_platform_driver;
1010
1011         asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
1012         if (!asuspf_device) {
1013                 result = -ENOMEM;
1014                 goto fail_platform_device1;
1015         }
1016
1017         result = platform_device_add(asuspf_device);
1018         if (result)
1019                 goto fail_platform_device2;
1020
1021         result = sysfs_create_group(&asuspf_device->dev.kobj,
1022                                     &asuspf_attribute_group);
1023         if (result)
1024                 goto fail_sysfs;
1025
1026         return 0;
1027
1028 fail_sysfs:
1029         platform_device_del(asuspf_device);
1030
1031 fail_platform_device2:
1032         platform_device_put(asuspf_device);
1033
1034 fail_platform_device1:
1035         platform_driver_unregister(&asuspf_driver);
1036
1037 fail_platform_driver:
1038         asus_led_exit();
1039
1040 fail_led:
1041         asus_backlight_exit();
1042
1043 fail_backlight:
1044
1045         return result;
1046 }
1047
1048 module_init(asus_laptop_init);
1049 module_exit(asus_laptop_exit);