asus-laptop: simplify write_acpi_int
[safe/jmp/linux-2.6] / drivers / platform / x86 / 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-2007 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  *  Sam Lin        - GPS support
34  */
35
36 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/types.h>
42 #include <linux/err.h>
43 #include <linux/proc_fs.h>
44 #include <linux/backlight.h>
45 #include <linux/fb.h>
46 #include <linux/leds.h>
47 #include <linux/platform_device.h>
48 #include <acpi/acpi_drivers.h>
49 #include <acpi/acpi_bus.h>
50 #include <asm/uaccess.h>
51 #include <linux/input.h>
52
53 #define ASUS_LAPTOP_VERSION "0.42"
54
55 #define ASUS_HOTK_NAME          "Asus Laptop Support"
56 #define ASUS_HOTK_CLASS         "hotkey"
57 #define ASUS_HOTK_DEVICE_NAME   "Hotkey"
58 #define ASUS_HOTK_FILE          KBUILD_MODNAME
59 #define ASUS_HOTK_PREFIX        "\\_SB.ATKD."
60
61
62 /*
63  * Some events we use, same for all Asus
64  */
65 #define ATKD_BR_UP       0x10
66 #define ATKD_BR_DOWN     0x20
67 #define ATKD_LCD_ON      0x33
68 #define ATKD_LCD_OFF     0x34
69
70 /*
71  * Known bits returned by \_SB.ATKD.HWRS
72  */
73 #define WL_HWRS     0x80
74 #define BT_HWRS     0x100
75
76 /*
77  * Flags for hotk status
78  * WL_ON and BT_ON are also used for wireless_status()
79  */
80 #define WL_ON       0x01        /* internal Wifi */
81 #define BT_ON       0x02        /* internal Bluetooth */
82 #define MLED_ON     0x04        /* mail LED */
83 #define TLED_ON     0x08        /* touchpad LED */
84 #define RLED_ON     0x10        /* Record LED */
85 #define PLED_ON     0x20        /* Phone LED */
86 #define GLED_ON     0x40        /* Gaming LED */
87 #define LCD_ON      0x80        /* LCD backlight */
88 #define GPS_ON      0x100       /* GPS */
89 #define KEY_ON      0x200       /* Keyboard backlight */
90
91 #define ASUS_LOG    ASUS_HOTK_FILE ": "
92 #define ASUS_ERR    KERN_ERR    ASUS_LOG
93 #define ASUS_WARNING    KERN_WARNING    ASUS_LOG
94 #define ASUS_NOTICE KERN_NOTICE ASUS_LOG
95 #define ASUS_INFO   KERN_INFO   ASUS_LOG
96 #define ASUS_DEBUG  KERN_DEBUG  ASUS_LOG
97
98 MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
99 MODULE_DESCRIPTION(ASUS_HOTK_NAME);
100 MODULE_LICENSE("GPL");
101
102 /*
103  * WAPF defines the behavior of the Fn+Fx wlan key
104  * The significance of values is yet to be found, but
105  * most of the time:
106  * 0x0 will do nothing
107  * 0x1 will allow to control the device with Fn+Fx key.
108  * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
109  * 0x5 like 0x1 or 0x4
110  * So, if something doesn't work as you want, just try other values =)
111  */
112 static uint wapf = 1;
113 module_param(wapf, uint, 0644);
114 MODULE_PARM_DESC(wapf, "WAPF value");
115
116 static uint wireless_status = 1;
117 static uint bluetooth_status = 1;
118
119 module_param(wireless_status, uint, 0644);
120 MODULE_PARM_DESC(wireless_status, "Set the wireless status on boot "
121                  "(0 = disabled, 1 = enabled, -1 = don't do anything). "
122                  "default is 1");
123
124 module_param(bluetooth_status, uint, 0644);
125 MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
126                  "(0 = disabled, 1 = enabled, -1 = don't do anything). "
127                  "default is 1");
128
129 #define ASUS_HANDLE(object, paths...)                                   \
130         static acpi_handle  object##_handle = NULL;                     \
131         static char *object##_paths[] = { paths }
132
133 /* LED */
134 ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
135 ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
136 ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
137 ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
138 ASUS_HANDLE(gled_set, ASUS_HOTK_PREFIX "GLED"); /* G1, G2 (probably) */
139
140 /* LEDD */
141 ASUS_HANDLE(ledd_set, ASUS_HOTK_PREFIX "SLCM");
142
143 /*
144  * Bluetooth and WLAN
145  * WLED and BLED are not handled like other XLED, because in some dsdt
146  * they also control the WLAN/Bluetooth device.
147  */
148 ASUS_HANDLE(wl_switch, ASUS_HOTK_PREFIX "WLED");
149 ASUS_HANDLE(bt_switch, ASUS_HOTK_PREFIX "BLED");
150 ASUS_HANDLE(wireless_status, ASUS_HOTK_PREFIX "RSTS");  /* All new models */
151
152 /* Brightness */
153 ASUS_HANDLE(brightness_set, ASUS_HOTK_PREFIX "SPLV");
154 ASUS_HANDLE(brightness_get, ASUS_HOTK_PREFIX "GPLV");
155
156 /* Backlight */
157 ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10",     /* All new models */
158             "\\_SB.PCI0.ISA.EC0._Q10",  /* A1x */
159             "\\_SB.PCI0.PX40.ECD0._Q10",        /* L3C */
160             "\\_SB.PCI0.PX40.EC0.Q10",  /* M1A */
161             "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
162             "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
163             "\\_SB.PCI0.PX40.Q10",      /* S1x */
164             "\\Q10");           /* A2x, L2D, L3D, M2E */
165
166 /* Display */
167 ASUS_HANDLE(display_set, ASUS_HOTK_PREFIX "SDSP");
168 ASUS_HANDLE(display_get,
169             /* A6B, A6K A6R A7D F3JM L4R M6R A3G M6A M6V VX-1 V6J V6V W3Z */
170             "\\_SB.PCI0.P0P1.VGA.GETD",
171             /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V S5A M5A z33A W1Jc W2V G1 */
172             "\\_SB.PCI0.P0P2.VGA.GETD",
173             /* A6V A6Q */
174             "\\_SB.PCI0.P0P3.VGA.GETD",
175             /* A6T, A6M */
176             "\\_SB.PCI0.P0PA.VGA.GETD",
177             /* L3C */
178             "\\_SB.PCI0.PCI1.VGAC.NMAP",
179             /* Z96F */
180             "\\_SB.PCI0.VGA.GETD",
181             /* A2D */
182             "\\ACTD",
183             /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
184             "\\ADVG",
185             /* P30 */
186             "\\DNXT",
187             /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
188             "\\INFB",
189             /* A3F A6F A3N A3L M6N W3N W6A */
190             "\\SSTE");
191
192 ASUS_HANDLE(ls_switch, ASUS_HOTK_PREFIX "ALSC"); /* Z71A Z71V */
193 ASUS_HANDLE(ls_level, ASUS_HOTK_PREFIX "ALSL");  /* Z71A Z71V */
194
195 /* GPS */
196 /* R2H use different handle for GPS on/off */
197 ASUS_HANDLE(gps_on, ASUS_HOTK_PREFIX "SDON");   /* R2H */
198 ASUS_HANDLE(gps_off, ASUS_HOTK_PREFIX "SDOF");  /* R2H */
199 ASUS_HANDLE(gps_status, ASUS_HOTK_PREFIX "GPST");
200
201 /* Keyboard light */
202 ASUS_HANDLE(kled_set, ASUS_HOTK_PREFIX "SLKB");
203 ASUS_HANDLE(kled_get, ASUS_HOTK_PREFIX "GLKB");
204
205 /*
206  * This is the main structure, we can use it to store anything interesting
207  * about the hotk device
208  */
209 struct asus_hotk {
210         char *name;             /* laptop name */
211         struct acpi_device *device;     /* the device we are in */
212         acpi_handle handle;     /* the handle of the hotk device */
213         char status;            /* status of the hotk, for LEDs, ... */
214         u32 ledd_status;        /* status of the LED display */
215         u8 light_level;         /* light sensor level */
216         u8 light_switch;        /* light sensor switch value */
217         u16 event_count[128];   /* count for each event TODO make this better */
218         struct input_dev *inputdev;
219         u16 *keycode_map;
220 };
221
222 /*
223  * This header is made available to allow proper configuration given model,
224  * revision number , ... this info cannot go in struct asus_hotk because it is
225  * available before the hotk
226  */
227 static struct acpi_table_header *asus_info;
228
229 /* The actual device the driver binds to */
230 static struct asus_hotk *hotk;
231
232 /*
233  * The hotkey driver declaration
234  */
235 static const struct acpi_device_id asus_device_ids[] = {
236         {"ATK0100", 0},
237         {"ATK0101", 0},
238         {"", 0},
239 };
240 MODULE_DEVICE_TABLE(acpi, asus_device_ids);
241
242 static int asus_hotk_add(struct acpi_device *device);
243 static int asus_hotk_remove(struct acpi_device *device, int type);
244 static void asus_hotk_notify(struct acpi_device *device, u32 event);
245
246 static struct acpi_driver asus_hotk_driver = {
247         .name = ASUS_HOTK_NAME,
248         .class = ASUS_HOTK_CLASS,
249         .owner = THIS_MODULE,
250         .ids = asus_device_ids,
251         .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
252         .ops = {
253                 .add = asus_hotk_add,
254                 .remove = asus_hotk_remove,
255                 .notify = asus_hotk_notify,
256                 },
257 };
258
259 /* The backlight device /sys/class/backlight */
260 static struct backlight_device *asus_backlight_device;
261
262 /*
263  * The backlight class declaration
264  */
265 static int read_brightness(struct backlight_device *bd);
266 static int update_bl_status(struct backlight_device *bd);
267 static struct backlight_ops asusbl_ops = {
268         .get_brightness = read_brightness,
269         .update_status = update_bl_status,
270 };
271
272 /*
273  * These functions actually update the LED's, and are called from a
274  * workqueue. By doing this as separate work rather than when the LED
275  * subsystem asks, we avoid messing with the Asus ACPI stuff during a
276  * potentially bad time, such as a timer interrupt.
277  */
278 static struct workqueue_struct *led_workqueue;
279
280 #define ASUS_LED(object, ledname, max)                                  \
281         static void object##_led_set(struct led_classdev *led_cdev,     \
282                                      enum led_brightness value);        \
283         static enum led_brightness object##_led_get(                    \
284                 struct led_classdev *led_cdev);                         \
285         static void object##_led_update(struct work_struct *ignored);   \
286         static int object##_led_wk;                                     \
287         static DECLARE_WORK(object##_led_work, object##_led_update);    \
288         static struct led_classdev object##_led = {                     \
289                 .name           = "asus::" ledname,                     \
290                 .brightness_set = object##_led_set,                     \
291                 .brightness_get = object##_led_get,                     \
292                 .max_brightness = max                                   \
293         }
294
295 ASUS_LED(mled, "mail", 1);
296 ASUS_LED(tled, "touchpad", 1);
297 ASUS_LED(rled, "record", 1);
298 ASUS_LED(pled, "phone", 1);
299 ASUS_LED(gled, "gaming", 1);
300 ASUS_LED(kled, "kbd_backlight", 3);
301
302 struct key_entry {
303         char type;
304         u8 code;
305         u16 keycode;
306 };
307
308 enum { KE_KEY, KE_END };
309
310 static struct key_entry asus_keymap[] = {
311         {KE_KEY, 0x02, KEY_SCREENLOCK},
312         {KE_KEY, 0x05, KEY_WLAN},
313         {KE_KEY, 0x08, KEY_F13},
314         {KE_KEY, 0x17, KEY_ZOOM},
315         {KE_KEY, 0x1f, KEY_BATTERY},
316         {KE_KEY, 0x30, KEY_VOLUMEUP},
317         {KE_KEY, 0x31, KEY_VOLUMEDOWN},
318         {KE_KEY, 0x32, KEY_MUTE},
319         {KE_KEY, 0x33, KEY_SWITCHVIDEOMODE},
320         {KE_KEY, 0x34, KEY_SWITCHVIDEOMODE},
321         {KE_KEY, 0x40, KEY_PREVIOUSSONG},
322         {KE_KEY, 0x41, KEY_NEXTSONG},
323         {KE_KEY, 0x43, KEY_STOPCD},
324         {KE_KEY, 0x45, KEY_PLAYPAUSE},
325         {KE_KEY, 0x4c, KEY_MEDIA},
326         {KE_KEY, 0x50, KEY_EMAIL},
327         {KE_KEY, 0x51, KEY_WWW},
328         {KE_KEY, 0x55, KEY_CALC},
329         {KE_KEY, 0x5C, KEY_SCREENLOCK},  /* Screenlock */
330         {KE_KEY, 0x5D, KEY_WLAN},
331         {KE_KEY, 0x5E, KEY_WLAN},
332         {KE_KEY, 0x5F, KEY_WLAN},
333         {KE_KEY, 0x60, KEY_SWITCHVIDEOMODE},
334         {KE_KEY, 0x61, KEY_SWITCHVIDEOMODE},
335         {KE_KEY, 0x62, KEY_SWITCHVIDEOMODE},
336         {KE_KEY, 0x63, KEY_SWITCHVIDEOMODE},
337         {KE_KEY, 0x6B, KEY_F13}, /* Lock Touchpad */
338         {KE_KEY, 0x82, KEY_CAMERA},
339         {KE_KEY, 0x88, KEY_WLAN },
340         {KE_KEY, 0x8A, KEY_PROG1},
341         {KE_KEY, 0x95, KEY_MEDIA},
342         {KE_KEY, 0x99, KEY_PHONE},
343         {KE_KEY, 0xc4, KEY_KBDILLUMUP},
344         {KE_KEY, 0xc5, KEY_KBDILLUMDOWN},
345         {KE_END, 0},
346 };
347
348 /*
349  * This function evaluates an ACPI method, given an int as parameter, the
350  * method is searched within the scope of the handle, can be NULL. The output
351  * of the method is written is output, which can also be NULL
352  *
353  * returns 0 if write is successful, -1 else.
354  */
355 static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
356                               struct acpi_buffer *output)
357 {
358         struct acpi_object_list params; /* list of input parameters (an int) */
359         union acpi_object in_obj;       /* the only param we use */
360         acpi_status status;
361
362         if (!handle)
363                 return 0;
364
365         params.count = 1;
366         params.pointer = &in_obj;
367         in_obj.type = ACPI_TYPE_INTEGER;
368         in_obj.integer.value = val;
369
370         status = acpi_evaluate_object(handle, (char *)method, &params, output);
371         if (status == AE_OK)
372                 return 0;
373         else
374                 return -1;
375 }
376
377 static int write_acpi_int(acpi_handle handle, const char *method, int val)
378 {
379         return write_acpi_int_ret(handle, method, val, NULL);
380 }
381
382 static int read_wireless_status(int mask)
383 {
384         unsigned long long status;
385         acpi_status rv = AE_OK;
386
387         if (!wireless_status_handle)
388                 return (hotk->status & mask) ? 1 : 0;
389
390         rv = acpi_evaluate_integer(wireless_status_handle, NULL, NULL, &status);
391         if (ACPI_FAILURE(rv))
392                 pr_warning("Error reading Wireless status\n");
393         else
394                 return (status & mask) ? 1 : 0;
395
396         return (hotk->status & mask) ? 1 : 0;
397 }
398
399 static int read_gps_status(void)
400 {
401         unsigned long long status;
402         acpi_status rv = AE_OK;
403
404         rv = acpi_evaluate_integer(gps_status_handle, NULL, NULL, &status);
405         if (ACPI_FAILURE(rv))
406                 pr_warning("Error reading GPS status\n");
407         else
408                 return status ? 1 : 0;
409
410         return (hotk->status & GPS_ON) ? 1 : 0;
411 }
412
413 /* Generic LED functions */
414 static int read_status(int mask)
415 {
416         /* There is a special method for both wireless devices */
417         if (mask == BT_ON || mask == WL_ON)
418                 return read_wireless_status(mask);
419         else if (mask == GPS_ON)
420                 return read_gps_status();
421
422         return (hotk->status & mask) ? 1 : 0;
423 }
424
425 static void write_status(acpi_handle handle, int out, int mask)
426 {
427         hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
428
429         switch (mask) {
430         case MLED_ON:
431                 out = !(out & 0x1);
432                 break;
433         case GLED_ON:
434                 out = (out & 0x1) + 1;
435                 break;
436         case GPS_ON:
437                 handle = (out) ? gps_on_handle : gps_off_handle;
438                 out = 0x02;
439                 break;
440         default:
441                 out &= 0x1;
442                 break;
443         }
444
445         if (write_acpi_int(handle, NULL, out))
446                 pr_warning(" write failed %x\n", mask);
447 }
448
449 /* /sys/class/led handlers */
450 #define ASUS_LED_HANDLER(object, mask)                                  \
451         static void object##_led_set(struct led_classdev *led_cdev,     \
452                                      enum led_brightness value)         \
453         {                                                               \
454                 object##_led_wk = (value > 0) ? 1 : 0;                  \
455                 queue_work(led_workqueue, &object##_led_work);          \
456         }                                                               \
457         static void object##_led_update(struct work_struct *ignored)    \
458         {                                                               \
459                 int value = object##_led_wk;                            \
460                 write_status(object##_set_handle, value, (mask));       \
461         }                                                               \
462         static enum led_brightness object##_led_get(                    \
463                 struct led_classdev *led_cdev)                          \
464         {                                                               \
465                 return led_cdev->brightness;                            \
466         }
467
468 ASUS_LED_HANDLER(mled, MLED_ON);
469 ASUS_LED_HANDLER(pled, PLED_ON);
470 ASUS_LED_HANDLER(rled, RLED_ON);
471 ASUS_LED_HANDLER(tled, TLED_ON);
472 ASUS_LED_HANDLER(gled, GLED_ON);
473
474 /*
475  * Keyboard backlight
476  */
477 static int get_kled_lvl(void)
478 {
479         unsigned long long kblv;
480         struct acpi_object_list params;
481         union acpi_object in_obj;
482         acpi_status rv;
483
484         params.count = 1;
485         params.pointer = &in_obj;
486         in_obj.type = ACPI_TYPE_INTEGER;
487         in_obj.integer.value = 2;
488
489         rv = acpi_evaluate_integer(kled_get_handle, NULL, &params, &kblv);
490         if (ACPI_FAILURE(rv)) {
491                 pr_warning("Error reading kled level\n");
492                 return 0;
493         }
494         return kblv;
495 }
496
497 static int set_kled_lvl(int kblv)
498 {
499         if (kblv > 0)
500                 kblv = (1 << 7) | (kblv & 0x7F);
501         else
502                 kblv = 0;
503
504         if (write_acpi_int(kled_set_handle, NULL, kblv)) {
505                 pr_warning("Keyboard LED display write failed\n");
506                 return -EINVAL;
507         }
508         return 0;
509 }
510
511 static void kled_led_set(struct led_classdev *led_cdev,
512                          enum led_brightness value)
513 {
514         kled_led_wk = value;
515         queue_work(led_workqueue, &kled_led_work);
516 }
517
518 static void kled_led_update(struct work_struct *ignored)
519 {
520         set_kled_lvl(kled_led_wk);
521 }
522
523 static enum led_brightness kled_led_get(struct led_classdev *led_cdev)
524 {
525         return get_kled_lvl();
526 }
527
528 static int get_lcd_state(void)
529 {
530         return read_status(LCD_ON);
531 }
532
533 static int set_lcd_state(int value)
534 {
535         int lcd = 0;
536         acpi_status status = 0;
537
538         lcd = value ? 1 : 0;
539
540         if (lcd == get_lcd_state())
541                 return 0;
542
543         if (lcd_switch_handle) {
544                 status = acpi_evaluate_object(lcd_switch_handle,
545                                               NULL, NULL, NULL);
546
547                 if (ACPI_FAILURE(status))
548                         pr_warning("Error switching LCD\n");
549         }
550
551         write_status(NULL, lcd, LCD_ON);
552         return 0;
553 }
554
555 static void lcd_blank(int blank)
556 {
557         struct backlight_device *bd = asus_backlight_device;
558
559         if (bd) {
560                 bd->props.power = blank;
561                 backlight_update_status(bd);
562         }
563 }
564
565 static int read_brightness(struct backlight_device *bd)
566 {
567         unsigned long long value;
568         acpi_status rv = AE_OK;
569
570         rv = acpi_evaluate_integer(brightness_get_handle, NULL, NULL, &value);
571         if (ACPI_FAILURE(rv))
572                 pr_warning("Error reading brightness\n");
573
574         return value;
575 }
576
577 static int set_brightness(struct backlight_device *bd, int value)
578 {
579         if (write_acpi_int(brightness_set_handle, NULL, value)) {
580                 pr_warning("Error changing brightness\n");
581                 return -EIO;
582         }
583         return 0;
584 }
585
586 static int update_bl_status(struct backlight_device *bd)
587 {
588         int rv;
589         int value = bd->props.brightness;
590
591         rv = set_brightness(bd, value);
592         if (rv)
593                 return rv;
594
595         value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
596         return set_lcd_state(value);
597 }
598
599 /*
600  * Platform device handlers
601  */
602
603 /*
604  * We write our info in page, we begin at offset off and cannot write more
605  * than count bytes. We set eof to 1 if we handle those 2 values. We return the
606  * number of bytes written in page
607  */
608 static ssize_t show_infos(struct device *dev,
609                           struct device_attribute *attr, char *page)
610 {
611         int len = 0;
612         unsigned long long temp;
613         char buf[16];           /* enough for all info */
614         acpi_status rv = AE_OK;
615
616         /*
617          * We use the easy way, we don't care of off and count, so we don't set eof
618          * to 1
619          */
620
621         len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
622         len += sprintf(page + len, "Model reference    : %s\n", hotk->name);
623         /*
624          * The SFUN method probably allows the original driver to get the list
625          * of features supported by a given model. For now, 0x0100 or 0x0800
626          * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
627          * The significance of others is yet to be found.
628          */
629         rv = acpi_evaluate_integer(hotk->handle, "SFUN", NULL, &temp);
630         if (!ACPI_FAILURE(rv))
631                 len += sprintf(page + len, "SFUN value         : %#x\n",
632                                (uint) temp);
633         /*
634          * The HWRS method return informations about the hardware.
635          * 0x80 bit is for WLAN, 0x100 for Bluetooth.
636          * The significance of others is yet to be found.
637          * If we don't find the method, we assume the device are present.
638          */
639         rv = acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &temp);
640         if (!ACPI_FAILURE(rv))
641                 len += sprintf(page + len, "HRWS value         : %#x\n",
642                                (uint) temp);
643         /*
644          * Another value for userspace: the ASYM method returns 0x02 for
645          * battery low and 0x04 for battery critical, its readings tend to be
646          * more accurate than those provided by _BST.
647          * Note: since not all the laptops provide this method, errors are
648          * silently ignored.
649          */
650         rv = acpi_evaluate_integer(hotk->handle, "ASYM", NULL, &temp);
651         if (!ACPI_FAILURE(rv))
652                 len += sprintf(page + len, "ASYM value         : %#x\n",
653                                (uint) temp);
654         if (asus_info) {
655                 snprintf(buf, 16, "%d", asus_info->length);
656                 len += sprintf(page + len, "DSDT length        : %s\n", buf);
657                 snprintf(buf, 16, "%d", asus_info->checksum);
658                 len += sprintf(page + len, "DSDT checksum      : %s\n", buf);
659                 snprintf(buf, 16, "%d", asus_info->revision);
660                 len += sprintf(page + len, "DSDT revision      : %s\n", buf);
661                 snprintf(buf, 7, "%s", asus_info->oem_id);
662                 len += sprintf(page + len, "OEM id             : %s\n", buf);
663                 snprintf(buf, 9, "%s", asus_info->oem_table_id);
664                 len += sprintf(page + len, "OEM table id       : %s\n", buf);
665                 snprintf(buf, 16, "%x", asus_info->oem_revision);
666                 len += sprintf(page + len, "OEM revision       : 0x%s\n", buf);
667                 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
668                 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
669                 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
670                 len += sprintf(page + len, "ASL comp revision  : 0x%s\n", buf);
671         }
672
673         return len;
674 }
675
676 static int parse_arg(const char *buf, unsigned long count, int *val)
677 {
678         if (!count)
679                 return 0;
680         if (count > 31)
681                 return -EINVAL;
682         if (sscanf(buf, "%i", val) != 1)
683                 return -EINVAL;
684         return count;
685 }
686
687 static ssize_t store_status(const char *buf, size_t count,
688                             acpi_handle handle, int mask)
689 {
690         int rv, value;
691         int out = 0;
692
693         rv = parse_arg(buf, count, &value);
694         if (rv > 0)
695                 out = value ? 1 : 0;
696
697         write_status(handle, out, mask);
698
699         return rv;
700 }
701
702 /*
703  * LEDD display
704  */
705 static ssize_t show_ledd(struct device *dev,
706                          struct device_attribute *attr, char *buf)
707 {
708         return sprintf(buf, "0x%08x\n", hotk->ledd_status);
709 }
710
711 static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
712                           const char *buf, size_t count)
713 {
714         int rv, value;
715
716         rv = parse_arg(buf, count, &value);
717         if (rv > 0) {
718                 if (write_acpi_int(ledd_set_handle, NULL, value))
719                         pr_warning("LED display write failed\n");
720                 else
721                         hotk->ledd_status = (u32) value;
722         }
723         return rv;
724 }
725
726 /*
727  * WLAN
728  */
729 static ssize_t show_wlan(struct device *dev,
730                          struct device_attribute *attr, char *buf)
731 {
732         return sprintf(buf, "%d\n", read_status(WL_ON));
733 }
734
735 static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
736                           const char *buf, size_t count)
737 {
738         return store_status(buf, count, wl_switch_handle, WL_ON);
739 }
740
741 /*
742  * Bluetooth
743  */
744 static ssize_t show_bluetooth(struct device *dev,
745                               struct device_attribute *attr, char *buf)
746 {
747         return sprintf(buf, "%d\n", read_status(BT_ON));
748 }
749
750 static ssize_t store_bluetooth(struct device *dev,
751                                struct device_attribute *attr, const char *buf,
752                                size_t count)
753 {
754         return store_status(buf, count, bt_switch_handle, BT_ON);
755 }
756
757 /*
758  * Display
759  */
760 static void set_display(int value)
761 {
762         /* no sanity check needed for now */
763         if (write_acpi_int(display_set_handle, NULL, value))
764                 pr_warning("Error setting display\n");
765         return;
766 }
767
768 static int read_display(void)
769 {
770         unsigned long long value = 0;
771         acpi_status rv = AE_OK;
772
773         /*
774          * In most of the case, we know how to set the display, but sometime
775          * we can't read it
776          */
777         if (display_get_handle) {
778                 rv = acpi_evaluate_integer(display_get_handle, NULL,
779                                            NULL, &value);
780                 if (ACPI_FAILURE(rv))
781                         pr_warning("Error reading display status\n");
782         }
783
784         value &= 0x0F;          /* needed for some models, shouldn't hurt others */
785
786         return value;
787 }
788
789 /*
790  * Now, *this* one could be more user-friendly, but so far, no-one has
791  * complained. The significance of bits is the same as in store_disp()
792  */
793 static ssize_t show_disp(struct device *dev,
794                          struct device_attribute *attr, char *buf)
795 {
796         return sprintf(buf, "%d\n", read_display());
797 }
798
799 /*
800  * Experimental support for display switching. As of now: 1 should activate
801  * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
802  * Any combination (bitwise) of these will suffice. I never actually tested 4
803  * displays hooked up simultaneously, so be warned. See the acpi4asus README
804  * for more info.
805  */
806 static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
807                           const char *buf, size_t count)
808 {
809         int rv, value;
810
811         rv = parse_arg(buf, count, &value);
812         if (rv > 0)
813                 set_display(value);
814         return rv;
815 }
816
817 /*
818  * Light Sens
819  */
820 static void set_light_sens_switch(int value)
821 {
822         if (write_acpi_int(ls_switch_handle, NULL, value))
823                 pr_warning("Error setting light sensor switch\n");
824         hotk->light_switch = value;
825 }
826
827 static ssize_t show_lssw(struct device *dev,
828                          struct device_attribute *attr, char *buf)
829 {
830         return sprintf(buf, "%d\n", hotk->light_switch);
831 }
832
833 static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
834                           const char *buf, size_t count)
835 {
836         int rv, value;
837
838         rv = parse_arg(buf, count, &value);
839         if (rv > 0)
840                 set_light_sens_switch(value ? 1 : 0);
841
842         return rv;
843 }
844
845 static void set_light_sens_level(int value)
846 {
847         if (write_acpi_int(ls_level_handle, NULL, value))
848                 pr_warning("Error setting light sensor level\n");
849         hotk->light_level = value;
850 }
851
852 static ssize_t show_lslvl(struct device *dev,
853                           struct device_attribute *attr, char *buf)
854 {
855         return sprintf(buf, "%d\n", hotk->light_level);
856 }
857
858 static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
859                            const char *buf, size_t count)
860 {
861         int rv, value;
862
863         rv = parse_arg(buf, count, &value);
864         if (rv > 0) {
865                 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
866                 /* 0 <= value <= 15 */
867                 set_light_sens_level(value);
868         }
869
870         return rv;
871 }
872
873 /*
874  * GPS
875  */
876 static ssize_t show_gps(struct device *dev,
877                         struct device_attribute *attr, char *buf)
878 {
879         return sprintf(buf, "%d\n", read_status(GPS_ON));
880 }
881
882 static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
883                          const char *buf, size_t count)
884 {
885         return store_status(buf, count, NULL, GPS_ON);
886 }
887
888 /*
889  * Hotkey functions
890  */
891 static struct key_entry *asus_get_entry_by_scancode(int code)
892 {
893         struct key_entry *key;
894
895         for (key = asus_keymap; key->type != KE_END; key++)
896                 if (code == key->code)
897                         return key;
898
899         return NULL;
900 }
901
902 static struct key_entry *asus_get_entry_by_keycode(int code)
903 {
904         struct key_entry *key;
905
906         for (key = asus_keymap; key->type != KE_END; key++)
907                 if (code == key->keycode && key->type == KE_KEY)
908                         return key;
909
910         return NULL;
911 }
912
913 static int asus_getkeycode(struct input_dev *dev, int scancode, int *keycode)
914 {
915         struct key_entry *key = asus_get_entry_by_scancode(scancode);
916
917         if (key && key->type == KE_KEY) {
918                 *keycode = key->keycode;
919                 return 0;
920         }
921
922         return -EINVAL;
923 }
924
925 static int asus_setkeycode(struct input_dev *dev, int scancode, int keycode)
926 {
927         struct key_entry *key;
928         int old_keycode;
929
930         if (keycode < 0 || keycode > KEY_MAX)
931                 return -EINVAL;
932
933         key = asus_get_entry_by_scancode(scancode);
934         if (key && key->type == KE_KEY) {
935                 old_keycode = key->keycode;
936                 key->keycode = keycode;
937                 set_bit(keycode, dev->keybit);
938                 if (!asus_get_entry_by_keycode(old_keycode))
939                         clear_bit(old_keycode, dev->keybit);
940                 return 0;
941         }
942
943         return -EINVAL;
944 }
945
946 static void asus_hotk_notify(struct acpi_device *device, u32 event)
947 {
948         static struct key_entry *key;
949         u16 count;
950
951         /* TODO Find a better way to handle events count. */
952         if (!hotk)
953                 return;
954
955         /*
956          * We need to tell the backlight device when the backlight power is
957          * switched
958          */
959         if (event == ATKD_LCD_ON) {
960                 write_status(NULL, 1, LCD_ON);
961                 lcd_blank(FB_BLANK_UNBLANK);
962         } else if (event == ATKD_LCD_OFF) {
963                 write_status(NULL, 0, LCD_ON);
964                 lcd_blank(FB_BLANK_POWERDOWN);
965         }
966
967         count = hotk->event_count[event % 128]++;
968         acpi_bus_generate_proc_event(hotk->device, event, count);
969         acpi_bus_generate_netlink_event(hotk->device->pnp.device_class,
970                                         dev_name(&hotk->device->dev), event,
971                                         count);
972
973         if (hotk->inputdev) {
974                 key = asus_get_entry_by_scancode(event);
975                 if (!key)
976                         return ;
977
978                 switch (key->type) {
979                 case KE_KEY:
980                         input_report_key(hotk->inputdev, key->keycode, 1);
981                         input_sync(hotk->inputdev);
982                         input_report_key(hotk->inputdev, key->keycode, 0);
983                         input_sync(hotk->inputdev);
984                         break;
985                 }
986         }
987 }
988
989 #define ASUS_CREATE_DEVICE_ATTR(_name)                                  \
990         struct device_attribute dev_attr_##_name = {                    \
991                 .attr = {                                               \
992                         .name = __stringify(_name),                     \
993                         .mode = 0 },                                    \
994                 .show   = NULL,                                         \
995                 .store  = NULL,                                         \
996         }
997
998 #define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store)               \
999         do {                                                            \
1000                 dev_attr_##_name.attr.mode = _mode;                     \
1001                 dev_attr_##_name.show = _show;                          \
1002                 dev_attr_##_name.store = _store;                        \
1003         } while(0)
1004
1005 static ASUS_CREATE_DEVICE_ATTR(infos);
1006 static ASUS_CREATE_DEVICE_ATTR(wlan);
1007 static ASUS_CREATE_DEVICE_ATTR(bluetooth);
1008 static ASUS_CREATE_DEVICE_ATTR(display);
1009 static ASUS_CREATE_DEVICE_ATTR(ledd);
1010 static ASUS_CREATE_DEVICE_ATTR(ls_switch);
1011 static ASUS_CREATE_DEVICE_ATTR(ls_level);
1012 static ASUS_CREATE_DEVICE_ATTR(gps);
1013
1014 static struct attribute *asuspf_attributes[] = {
1015         &dev_attr_infos.attr,
1016         &dev_attr_wlan.attr,
1017         &dev_attr_bluetooth.attr,
1018         &dev_attr_display.attr,
1019         &dev_attr_ledd.attr,
1020         &dev_attr_ls_switch.attr,
1021         &dev_attr_ls_level.attr,
1022         &dev_attr_gps.attr,
1023         NULL
1024 };
1025
1026 static struct attribute_group asuspf_attribute_group = {
1027         .attrs = asuspf_attributes
1028 };
1029
1030 static struct platform_driver asuspf_driver = {
1031         .driver = {
1032                    .name = ASUS_HOTK_FILE,
1033                    .owner = THIS_MODULE,
1034                    }
1035 };
1036
1037 static struct platform_device *asuspf_device;
1038
1039 static void asus_hotk_add_fs(void)
1040 {
1041         ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
1042
1043         if (wl_switch_handle)
1044                 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
1045
1046         if (bt_switch_handle)
1047                 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
1048                                      show_bluetooth, store_bluetooth);
1049
1050         if (display_set_handle && display_get_handle)
1051                 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
1052         else if (display_set_handle)
1053                 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
1054
1055         if (ledd_set_handle)
1056                 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
1057
1058         if (ls_switch_handle && ls_level_handle) {
1059                 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
1060                 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
1061         }
1062
1063         if (gps_status_handle && gps_on_handle && gps_off_handle)
1064                 ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
1065 }
1066
1067 static int asus_handle_init(char *name, acpi_handle * handle,
1068                             char **paths, int num_paths)
1069 {
1070         int i;
1071         acpi_status status;
1072
1073         for (i = 0; i < num_paths; i++) {
1074                 status = acpi_get_handle(NULL, paths[i], handle);
1075                 if (ACPI_SUCCESS(status))
1076                         return 0;
1077         }
1078
1079         *handle = NULL;
1080         return -ENODEV;
1081 }
1082
1083 #define ASUS_HANDLE_INIT(object)                                        \
1084         asus_handle_init(#object, &object##_handle, object##_paths,     \
1085                          ARRAY_SIZE(object##_paths))
1086
1087 /*
1088  * This function is used to initialize the hotk with right values. In this
1089  * method, we can make all the detection we want, and modify the hotk struct
1090  */
1091 static int asus_hotk_get_info(void)
1092 {
1093         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1094         union acpi_object *model = NULL;
1095         unsigned long long bsts_result, hwrs_result;
1096         char *string = NULL;
1097         acpi_status status;
1098
1099         /*
1100          * Get DSDT headers early enough to allow for differentiating between
1101          * models, but late enough to allow acpi_bus_register_driver() to fail
1102          * before doing anything ACPI-specific. Should we encounter a machine,
1103          * which needs special handling (i.e. its hotkey device has a different
1104          * HID), this bit will be moved. A global variable asus_info contains
1105          * the DSDT header.
1106          */
1107         status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
1108         if (ACPI_FAILURE(status))
1109                 pr_warning("Couldn't get the DSDT table header\n");
1110
1111         /* We have to write 0 on init this far for all ASUS models */
1112         if (write_acpi_int_ret(hotk->handle, "INIT", 0, &buffer)) {
1113                 pr_err("Hotkey initialization failed\n");
1114                 return -ENODEV;
1115         }
1116
1117         /* This needs to be called for some laptops to init properly */
1118         status =
1119             acpi_evaluate_integer(hotk->handle, "BSTS", NULL, &bsts_result);
1120         if (ACPI_FAILURE(status))
1121                 pr_warning("Error calling BSTS\n");
1122         else if (bsts_result)
1123                 pr_notice("BSTS called, 0x%02x returned\n",
1124                        (uint) bsts_result);
1125
1126         /* This too ... */
1127         write_acpi_int(hotk->handle, "CWAP", wapf);
1128
1129         /*
1130          * Try to match the object returned by INIT to the specific model.
1131          * Handle every possible object (or the lack of thereof) the DSDT
1132          * writers might throw at us. When in trouble, we pass NULL to
1133          * asus_model_match() and try something completely different.
1134          */
1135         if (buffer.pointer) {
1136                 model = buffer.pointer;
1137                 switch (model->type) {
1138                 case ACPI_TYPE_STRING:
1139                         string = model->string.pointer;
1140                         break;
1141                 case ACPI_TYPE_BUFFER:
1142                         string = model->buffer.pointer;
1143                         break;
1144                 default:
1145                         string = "";
1146                         break;
1147                 }
1148         }
1149         hotk->name = kstrdup(string, GFP_KERNEL);
1150         if (!hotk->name)
1151                 return -ENOMEM;
1152
1153         if (*string)
1154                 pr_notice("  %s model detected\n", string);
1155
1156         ASUS_HANDLE_INIT(mled_set);
1157         ASUS_HANDLE_INIT(tled_set);
1158         ASUS_HANDLE_INIT(rled_set);
1159         ASUS_HANDLE_INIT(pled_set);
1160         ASUS_HANDLE_INIT(gled_set);
1161
1162         ASUS_HANDLE_INIT(ledd_set);
1163
1164         ASUS_HANDLE_INIT(kled_set);
1165         ASUS_HANDLE_INIT(kled_get);
1166
1167         /*
1168          * The HWRS method return informations about the hardware.
1169          * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1170          * The significance of others is yet to be found.
1171          * If we don't find the method, we assume the device are present.
1172          */
1173         status =
1174             acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &hwrs_result);
1175         if (ACPI_FAILURE(status))
1176                 hwrs_result = WL_HWRS | BT_HWRS;
1177
1178         if (hwrs_result & WL_HWRS)
1179                 ASUS_HANDLE_INIT(wl_switch);
1180         if (hwrs_result & BT_HWRS)
1181                 ASUS_HANDLE_INIT(bt_switch);
1182
1183         ASUS_HANDLE_INIT(wireless_status);
1184
1185         ASUS_HANDLE_INIT(brightness_set);
1186         ASUS_HANDLE_INIT(brightness_get);
1187
1188         ASUS_HANDLE_INIT(lcd_switch);
1189
1190         ASUS_HANDLE_INIT(display_set);
1191         ASUS_HANDLE_INIT(display_get);
1192
1193         /*
1194          * There is a lot of models with "ALSL", but a few get
1195          * a real light sens, so we need to check it.
1196          */
1197         if (!ASUS_HANDLE_INIT(ls_switch))
1198                 ASUS_HANDLE_INIT(ls_level);
1199
1200         ASUS_HANDLE_INIT(gps_on);
1201         ASUS_HANDLE_INIT(gps_off);
1202         ASUS_HANDLE_INIT(gps_status);
1203
1204         kfree(model);
1205
1206         return AE_OK;
1207 }
1208
1209 static int asus_input_init(void)
1210 {
1211         const struct key_entry *key;
1212         int result;
1213
1214         hotk->inputdev = input_allocate_device();
1215         if (!hotk->inputdev) {
1216                 pr_info("Unable to allocate input device\n");
1217                 return 0;
1218         }
1219         hotk->inputdev->name = "Asus Laptop extra buttons";
1220         hotk->inputdev->phys = ASUS_HOTK_FILE "/input0";
1221         hotk->inputdev->id.bustype = BUS_HOST;
1222         hotk->inputdev->getkeycode = asus_getkeycode;
1223         hotk->inputdev->setkeycode = asus_setkeycode;
1224
1225         for (key = asus_keymap; key->type != KE_END; key++) {
1226                 switch (key->type) {
1227                 case KE_KEY:
1228                         set_bit(EV_KEY, hotk->inputdev->evbit);
1229                         set_bit(key->keycode, hotk->inputdev->keybit);
1230                         break;
1231                 }
1232         }
1233         result = input_register_device(hotk->inputdev);
1234         if (result) {
1235                 pr_info("Unable to register input device\n");
1236                 input_free_device(hotk->inputdev);
1237         }
1238         return result;
1239 }
1240
1241 static int asus_hotk_check(void)
1242 {
1243         int result = 0;
1244
1245         result = acpi_bus_get_status(hotk->device);
1246         if (result)
1247                 return result;
1248
1249         if (hotk->device->status.present) {
1250                 result = asus_hotk_get_info();
1251         } else {
1252                 pr_err("Hotkey device not present, aborting\n");
1253                 return -EINVAL;
1254         }
1255
1256         return result;
1257 }
1258
1259 static int asus_hotk_found;
1260
1261 static int asus_hotk_add(struct acpi_device *device)
1262 {
1263         int result;
1264
1265         pr_notice("Asus Laptop Support version %s\n",
1266                ASUS_LAPTOP_VERSION);
1267
1268         hotk = kzalloc(sizeof(struct asus_hotk), GFP_KERNEL);
1269         if (!hotk)
1270                 return -ENOMEM;
1271
1272         hotk->handle = device->handle;
1273         strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
1274         strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
1275         device->driver_data = hotk;
1276         hotk->device = device;
1277
1278         result = asus_hotk_check();
1279         if (result)
1280                 goto end;
1281
1282         asus_hotk_add_fs();
1283
1284         asus_hotk_found = 1;
1285
1286         /* WLED and BLED are on by default */
1287         if (bluetooth_status != -1)
1288                 write_status(bt_switch_handle, !!bluetooth_status, BT_ON);
1289         if (wireless_status != -1)
1290                 write_status(wl_switch_handle, !!wireless_status, WL_ON);
1291
1292         /* If the h/w switch is off, we need to check the real status */
1293         write_status(NULL, read_status(BT_ON), BT_ON);
1294         write_status(NULL, read_status(WL_ON), WL_ON);
1295
1296         /* LCD Backlight is on by default */
1297         write_status(NULL, 1, LCD_ON);
1298
1299         /* Keyboard Backlight is on by default */
1300         if (kled_set_handle)
1301                 set_kled_lvl(1);
1302
1303         /* LED display is off by default */
1304         hotk->ledd_status = 0xFFF;
1305
1306         /* Set initial values of light sensor and level */
1307         hotk->light_switch = 0; /* Default to light sensor disabled */
1308         hotk->light_level = 5;  /* level 5 for sensor sensitivity */
1309
1310         if (ls_switch_handle)
1311                 set_light_sens_switch(hotk->light_switch);
1312
1313         if (ls_level_handle)
1314                 set_light_sens_level(hotk->light_level);
1315
1316         /* GPS is on by default */
1317         write_status(NULL, 1, GPS_ON);
1318
1319 end:
1320         if (result) {
1321                 kfree(hotk->name);
1322                 kfree(hotk);
1323         }
1324
1325         return result;
1326 }
1327
1328 static int asus_hotk_remove(struct acpi_device *device, int type)
1329 {
1330         kfree(hotk->name);
1331         kfree(hotk);
1332
1333         return 0;
1334 }
1335
1336 static void asus_backlight_exit(void)
1337 {
1338         if (asus_backlight_device)
1339                 backlight_device_unregister(asus_backlight_device);
1340 }
1341
1342 #define  ASUS_LED_UNREGISTER(object)                            \
1343         if (object##_led.dev)                                   \
1344                 led_classdev_unregister(&object##_led)
1345
1346 static void asus_led_exit(void)
1347 {
1348         destroy_workqueue(led_workqueue);
1349         ASUS_LED_UNREGISTER(mled);
1350         ASUS_LED_UNREGISTER(tled);
1351         ASUS_LED_UNREGISTER(pled);
1352         ASUS_LED_UNREGISTER(rled);
1353         ASUS_LED_UNREGISTER(gled);
1354         ASUS_LED_UNREGISTER(kled);
1355 }
1356
1357 static void asus_input_exit(void)
1358 {
1359         if (hotk->inputdev)
1360                 input_unregister_device(hotk->inputdev);
1361 }
1362
1363 static void __exit asus_laptop_exit(void)
1364 {
1365         asus_backlight_exit();
1366         asus_led_exit();
1367         asus_input_exit();
1368
1369         acpi_bus_unregister_driver(&asus_hotk_driver);
1370         sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
1371         platform_device_unregister(asuspf_device);
1372         platform_driver_unregister(&asuspf_driver);
1373 }
1374
1375 static int asus_backlight_init(struct device *dev)
1376 {
1377         struct backlight_device *bd;
1378
1379         if (brightness_set_handle && lcd_switch_handle) {
1380                 bd = backlight_device_register(ASUS_HOTK_FILE, dev,
1381                                                NULL, &asusbl_ops);
1382                 if (IS_ERR(bd)) {
1383                         pr_err("Could not register asus backlight device\n");
1384                         asus_backlight_device = NULL;
1385                         return PTR_ERR(bd);
1386                 }
1387
1388                 asus_backlight_device = bd;
1389
1390                 bd->props.max_brightness = 15;
1391                 bd->props.brightness = read_brightness(NULL);
1392                 bd->props.power = FB_BLANK_UNBLANK;
1393                 backlight_update_status(bd);
1394         }
1395         return 0;
1396 }
1397
1398 static int asus_led_register(acpi_handle handle,
1399                              struct led_classdev *ldev, struct device *dev)
1400 {
1401         if (!handle)
1402                 return 0;
1403
1404         return led_classdev_register(dev, ldev);
1405 }
1406
1407 #define ASUS_LED_REGISTER(object, device)                               \
1408         asus_led_register(object##_set_handle, &object##_led, device)
1409
1410 static int asus_led_init(struct device *dev)
1411 {
1412         int rv;
1413
1414         rv = ASUS_LED_REGISTER(mled, dev);
1415         if (rv)
1416                 goto out;
1417
1418         rv = ASUS_LED_REGISTER(tled, dev);
1419         if (rv)
1420                 goto out1;
1421
1422         rv = ASUS_LED_REGISTER(rled, dev);
1423         if (rv)
1424                 goto out2;
1425
1426         rv = ASUS_LED_REGISTER(pled, dev);
1427         if (rv)
1428                 goto out3;
1429
1430         rv = ASUS_LED_REGISTER(gled, dev);
1431         if (rv)
1432                 goto out4;
1433
1434         if (kled_set_handle && kled_get_handle)
1435                 rv = ASUS_LED_REGISTER(kled, dev);
1436         if (rv)
1437                 goto out5;
1438
1439         led_workqueue = create_singlethread_workqueue("led_workqueue");
1440         if (!led_workqueue)
1441                 goto out6;
1442
1443         return 0;
1444 out6:
1445         rv = -ENOMEM;
1446         ASUS_LED_UNREGISTER(kled);
1447 out5:
1448         ASUS_LED_UNREGISTER(gled);
1449 out4:
1450         ASUS_LED_UNREGISTER(pled);
1451 out3:
1452         ASUS_LED_UNREGISTER(rled);
1453 out2:
1454         ASUS_LED_UNREGISTER(tled);
1455 out1:
1456         ASUS_LED_UNREGISTER(mled);
1457 out:
1458         return rv;
1459 }
1460
1461 static int __init asus_laptop_init(void)
1462 {
1463         int result;
1464
1465         result = acpi_bus_register_driver(&asus_hotk_driver);
1466         if (result < 0)
1467                 return result;
1468
1469         /*
1470          * This is a bit of a kludge.  We only want this module loaded
1471          * for ASUS systems, but there's currently no way to probe the
1472          * ACPI namespace for ASUS HIDs.  So we just return failure if
1473          * we didn't find one, which will cause the module to be
1474          * unloaded.
1475          */
1476         if (!asus_hotk_found) {
1477                 acpi_bus_unregister_driver(&asus_hotk_driver);
1478                 return -ENODEV;
1479         }
1480
1481         result = asus_input_init();
1482         if (result)
1483                 goto fail_input;
1484
1485         /* Register platform stuff */
1486         result = platform_driver_register(&asuspf_driver);
1487         if (result)
1488                 goto fail_platform_driver;
1489
1490         asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
1491         if (!asuspf_device) {
1492                 result = -ENOMEM;
1493                 goto fail_platform_device1;
1494         }
1495
1496         result = platform_device_add(asuspf_device);
1497         if (result)
1498                 goto fail_platform_device2;
1499
1500         result = sysfs_create_group(&asuspf_device->dev.kobj,
1501                                     &asuspf_attribute_group);
1502         if (result)
1503                 goto fail_sysfs;
1504
1505         result = asus_led_init(&asuspf_device->dev);
1506         if (result)
1507                 goto fail_led;
1508
1509         if (!acpi_video_backlight_support()) {
1510                 result = asus_backlight_init(&asuspf_device->dev);
1511                 if (result)
1512                         goto fail_backlight;
1513         } else
1514                 pr_info("Brightness ignored, must be controlled by "
1515                        "ACPI video driver\n");
1516
1517         return 0;
1518
1519 fail_backlight:
1520        asus_led_exit();
1521
1522 fail_led:
1523        sysfs_remove_group(&asuspf_device->dev.kobj,
1524                           &asuspf_attribute_group);
1525
1526 fail_sysfs:
1527         platform_device_del(asuspf_device);
1528
1529 fail_platform_device2:
1530         platform_device_put(asuspf_device);
1531
1532 fail_platform_device1:
1533         platform_driver_unregister(&asuspf_driver);
1534
1535 fail_platform_driver:
1536         asus_input_exit();
1537
1538 fail_input:
1539
1540         return result;
1541 }
1542
1543 module_init(asus_laptop_init);
1544 module_exit(asus_laptop_exit);