asus-laptop: add backlight support
[pandora-kernel.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 /*
129  * This is the main structure, we can use it to store anything interesting
130  * about the hotk device
131  */
132 struct asus_hotk {
133         char *name; //laptop name
134         struct acpi_device *device;     //the device we are in
135         acpi_handle handle;     //the handle of the hotk device
136         char status;            //status of the hotk, for LEDs, ...
137         u16 event_count[128];   //count for each event TODO make this better
138 };
139
140 /*
141  * This header is made available to allow proper configuration given model,
142  * revision number , ... this info cannot go in struct asus_hotk because it is
143  * available before the hotk
144  */
145 static struct acpi_table_header *asus_info;
146
147 /* The actual device the driver binds to */
148 static struct asus_hotk *hotk;
149
150 /*
151  * The hotkey driver declaration
152  */
153 static int asus_hotk_add(struct acpi_device *device);
154 static int asus_hotk_remove(struct acpi_device *device, int type);
155 static struct acpi_driver asus_hotk_driver = {
156         .name = ASUS_HOTK_NAME,
157         .class = ASUS_HOTK_CLASS,
158         .ids = ASUS_HOTK_HID,
159         .ops = {
160                 .add = asus_hotk_add,
161                 .remove = asus_hotk_remove,
162                 },
163 };
164
165 /* The backlight device /sys/class/backlight */
166 static struct backlight_device *asus_backlight_device;
167
168 /*
169  * The backlight class declaration
170  */
171 static int read_brightness(struct backlight_device *bd);
172 static int update_bl_status(struct backlight_device *bd);
173 static struct backlight_properties asusbl_data = {
174                .owner          = THIS_MODULE,
175                .get_brightness = read_brightness,
176                .update_status  = update_bl_status,
177                .max_brightness = 15,
178 };
179
180 /* These functions actually update the LED's, and are called from a
181  * workqueue. By doing this as separate work rather than when the LED
182  * subsystem asks, we avoid messing with the Asus ACPI stuff during a
183  * potentially bad time, such as a timer interrupt. */
184 static struct workqueue_struct *led_workqueue;
185
186 #define ASUS_LED(object, ledname)                                       \
187         static void object##_led_set(struct led_classdev *led_cdev,     \
188                                      enum led_brightness value);        \
189         static void object##_led_update(struct work_struct *ignored);   \
190         static int object##_led_wk;                                     \
191         DECLARE_WORK(object##_led_work, object##_led_update);           \
192         static struct led_classdev object##_led = {                     \
193                 .name           = "asus:" ledname,                      \
194                 .brightness_set = object##_led_set,                     \
195         }
196
197 ASUS_LED(mled, "mail");
198 ASUS_LED(tled, "touchpad");
199 ASUS_LED(rled, "record");
200 ASUS_LED(pled, "phone");
201
202 /*
203  * This function evaluates an ACPI method, given an int as parameter, the
204  * method is searched within the scope of the handle, can be NULL. The output
205  * of the method is written is output, which can also be NULL
206  *
207  * returns 1 if write is successful, 0 else.
208  */
209 static int write_acpi_int(acpi_handle handle, const char *method, int val,
210                           struct acpi_buffer *output)
211 {
212         struct acpi_object_list params; //list of input parameters (an int here)
213         union acpi_object in_obj;       //the only param we use
214         acpi_status status;
215
216         params.count = 1;
217         params.pointer = &in_obj;
218         in_obj.type = ACPI_TYPE_INTEGER;
219         in_obj.integer.value = val;
220
221         status = acpi_evaluate_object(handle, (char *)method, &params, output);
222         return (status == AE_OK);
223 }
224
225 static int read_acpi_int(acpi_handle handle, const char *method, int *val,
226                          struct acpi_object_list *params)
227 {
228         struct acpi_buffer output;
229         union acpi_object out_obj;
230         acpi_status status;
231
232         output.length = sizeof(out_obj);
233         output.pointer = &out_obj;
234
235         status = acpi_evaluate_object(handle, (char *)method, params, &output);
236         *val = out_obj.integer.value;
237         return (status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER);
238 }
239
240 static int read_wireless_status(int mask) {
241         int status;
242
243         if (!wireless_status_handle)
244                 return (hotk->status & mask) ? 1 : 0;
245
246         if (read_acpi_int(wireless_status_handle, NULL, &status, NULL)) {
247                 return (status & mask) ? 1 : 0;
248         } else
249                 printk(ASUS_WARNING "Error reading Wireless status\n");
250
251         return (hotk->status & mask) ? 1 : 0;
252 }
253
254 /* Generic LED functions */
255 static int read_status(int mask)
256 {
257         /* There is a special method for both wireless devices */
258         if (mask == BT_ON || mask == WL_ON)
259                 return read_wireless_status(mask);
260
261         return (hotk->status & mask) ? 1 : 0;
262 }
263
264 static void write_status(acpi_handle handle, int out, int mask,
265                          int invert)
266 {
267         hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
268
269         if (invert)             /* invert target value */
270                 out = !out & 0x1;
271
272         if (handle && !write_acpi_int(handle, NULL, out, NULL))
273                 printk(ASUS_WARNING " write failed\n");
274 }
275
276 /* /sys/class/led handlers */
277 #define ASUS_LED_HANDLER(object, mask, invert)                          \
278         static void object##_led_set(struct led_classdev *led_cdev,     \
279                                      enum led_brightness value)         \
280         {                                                               \
281                 object##_led_wk = value;                                \
282                 queue_work(led_workqueue, &object##_led_work);          \
283         }                                                               \
284         static void object##_led_update(struct work_struct *ignored)    \
285         {                                                               \
286                 int value = object##_led_wk;                            \
287                 write_status(object##_set_handle, value, (mask), (invert)); \
288         }
289
290 ASUS_LED_HANDLER(mled, MLED_ON, 1);
291 ASUS_LED_HANDLER(pled, PLED_ON, 0);
292 ASUS_LED_HANDLER(rled, RLED_ON, 0);
293 ASUS_LED_HANDLER(tled, TLED_ON, 0);
294
295 static int get_lcd_state(void)
296 {
297         return read_status(LCD_ON);
298 }
299
300 static int set_lcd_state(int value)
301 {
302         int lcd = 0;
303         acpi_status status = 0;
304
305         lcd = value ? 1 : 0;
306
307         if (lcd == get_lcd_state())
308                 return 0;
309
310         if(lcd_switch_handle) {
311                 status = acpi_evaluate_object(lcd_switch_handle,
312                                               NULL, NULL, NULL);
313
314                 if (ACPI_FAILURE(status))
315                         printk(ASUS_WARNING "Error switching LCD\n");
316         }
317
318         write_status(NULL, lcd, LCD_ON, 0);
319         return 0;
320 }
321
322 static void lcd_blank(int blank)
323 {
324         struct backlight_device *bd = asus_backlight_device;
325
326         if(bd) {
327                 down(&bd->sem);
328                 if(likely(bd->props)) {
329                         bd->props->power = blank;
330                         if(likely(bd->props->update_status))
331                            bd->props->update_status(bd);
332                 }
333                 up(&bd->sem);
334         }
335 }
336
337 static int read_brightness(struct backlight_device *bd)
338 {
339         int value;
340
341         if (!read_acpi_int(brightness_get_handle, NULL, &value, NULL))
342                 printk(ASUS_WARNING "Error reading brightness\n");
343
344         return value;
345 }
346
347 static int set_brightness(struct backlight_device *bd, int value)
348 {
349         int ret = 0;
350
351         value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
352         /* 0 <= value <= 15 */
353
354         if (!write_acpi_int(brightness_set_handle, NULL, value, NULL)) {
355                 printk(ASUS_WARNING "Error changing brightness\n");
356                 ret = -EIO;
357         }
358
359         return ret;
360 }
361
362 static int update_bl_status(struct backlight_device *bd)
363 {
364         int rv;
365         int value = bd->props->brightness;
366
367         rv = set_brightness(bd, value);
368         if(rv)
369                 return rv;
370
371         value = (bd->props->power == FB_BLANK_UNBLANK) ? 1 : 0;
372         return set_lcd_state(value);
373 }
374
375 /*
376  * Platform device handlers
377  */
378
379 /*
380  * We write our info in page, we begin at offset off and cannot write more
381  * than count bytes. We set eof to 1 if we handle those 2 values. We return the
382  * number of bytes written in page
383  */
384 static ssize_t show_infos(struct device *dev,
385                          struct device_attribute *attr, char *page)
386 {
387         int len = 0;
388         int temp;
389         char buf[16];           //enough for all info
390         /*
391          * We use the easy way, we don't care of off and count, so we don't set eof
392          * to 1
393          */
394
395         len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
396         len += sprintf(page + len, "Model reference    : %s\n", hotk->name);
397         /*
398          * The SFUN method probably allows the original driver to get the list
399          * of features supported by a given model. For now, 0x0100 or 0x0800
400          * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
401          * The significance of others is yet to be found.
402          */
403         if (read_acpi_int(hotk->handle, "SFUN", &temp, NULL))
404                 len +=
405                     sprintf(page + len, "SFUN value         : 0x%04x\n", temp);
406         /*
407          * Another value for userspace: the ASYM method returns 0x02 for
408          * battery low and 0x04 for battery critical, its readings tend to be
409          * more accurate than those provided by _BST.
410          * Note: since not all the laptops provide this method, errors are
411          * silently ignored.
412          */
413         if (read_acpi_int(hotk->handle, "ASYM", &temp, NULL))
414                 len +=
415                     sprintf(page + len, "ASYM value         : 0x%04x\n", temp);
416         if (asus_info) {
417                 snprintf(buf, 16, "%d", asus_info->length);
418                 len += sprintf(page + len, "DSDT length        : %s\n", buf);
419                 snprintf(buf, 16, "%d", asus_info->checksum);
420                 len += sprintf(page + len, "DSDT checksum      : %s\n", buf);
421                 snprintf(buf, 16, "%d", asus_info->revision);
422                 len += sprintf(page + len, "DSDT revision      : %s\n", buf);
423                 snprintf(buf, 7, "%s", asus_info->oem_id);
424                 len += sprintf(page + len, "OEM id             : %s\n", buf);
425                 snprintf(buf, 9, "%s", asus_info->oem_table_id);
426                 len += sprintf(page + len, "OEM table id       : %s\n", buf);
427                 snprintf(buf, 16, "%x", asus_info->oem_revision);
428                 len += sprintf(page + len, "OEM revision       : 0x%s\n", buf);
429                 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
430                 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
431                 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
432                 len += sprintf(page + len, "ASL comp revision  : 0x%s\n", buf);
433         }
434
435         return len;
436 }
437
438 static int parse_arg(const char *buf, unsigned long count, int *val)
439 {
440         if (!count)
441                 return 0;
442         if (count > 31)
443                 return -EINVAL;
444         if (sscanf(buf, "%i", val) != 1)
445                 return -EINVAL;
446         return count;
447 }
448
449 static ssize_t store_status(const char *buf, size_t count,
450                             acpi_handle handle, int mask, int invert)
451 {
452         int rv, value;
453         int out = 0;
454
455         rv = parse_arg(buf, count, &value);
456         if (rv > 0)
457                 out = value ? 1 : 0;
458
459         write_status(handle, out, mask, invert);
460
461         return rv;
462 }
463
464 /*
465  * WLAN
466  */
467 static ssize_t show_wlan(struct device *dev,
468                          struct device_attribute *attr, char *buf)
469 {
470         return sprintf(buf, "%d\n", read_status(WL_ON));
471 }
472
473 static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
474                           const char *buf, size_t count)
475 {
476         return store_status(buf, count, wl_switch_handle, WL_ON, 0);
477 }
478
479 /*
480  * Bluetooth
481  */
482 static ssize_t show_bluetooth(struct device *dev,
483                               struct device_attribute *attr, char *buf)
484 {
485         return sprintf(buf, "%d\n", read_status(BT_ON));
486 }
487
488 static ssize_t store_bluetooth(struct device *dev, struct device_attribute *attr,
489                                const char *buf, size_t count)
490 {
491         return store_status(buf, count, bt_switch_handle, BT_ON, 0);
492 }
493
494 static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
495 {
496         /* TODO Find a better way to handle events count. */
497         if (!hotk)
498                 return;
499
500         /*
501          * We need to tell the backlight device when the backlight power is
502          * switched
503          */
504         if (event == ATKD_LCD_ON) {
505                 write_status(NULL, 1, LCD_ON, 0);
506                 lcd_blank(FB_BLANK_UNBLANK);
507         } else if(event == ATKD_LCD_OFF) {
508                 write_status(NULL, 0, LCD_ON, 0);
509                 lcd_blank(FB_BLANK_POWERDOWN);
510         }
511
512         acpi_bus_generate_event(hotk->device, event,
513                                 hotk->event_count[event % 128]++);
514
515         return;
516 }
517
518 #define ASUS_CREATE_DEVICE_ATTR(_name)                                  \
519         struct device_attribute dev_attr_##_name = {                    \
520                 .attr = {                                               \
521                         .name = __stringify(_name),                     \
522                         .mode = 0,                                      \
523                         .owner = THIS_MODULE },                         \
524                 .show   = NULL,                                         \
525                 .store  = NULL,                                         \
526         }
527
528 #define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store)               \
529         do {                                                            \
530                 dev_attr_##_name.attr.mode = _mode;                     \
531                 dev_attr_##_name.show = _show;                          \
532                 dev_attr_##_name.store = _store;                        \
533         } while(0)
534
535 static ASUS_CREATE_DEVICE_ATTR(infos);
536 static ASUS_CREATE_DEVICE_ATTR(wlan);
537 static ASUS_CREATE_DEVICE_ATTR(bluetooth);
538
539 static struct attribute *asuspf_attributes[] = {
540         &dev_attr_infos.attr,
541         &dev_attr_wlan.attr,
542         &dev_attr_bluetooth.attr,
543         NULL
544 };
545
546 static struct attribute_group asuspf_attribute_group = {
547         .attrs = asuspf_attributes
548 };
549
550 static struct platform_driver asuspf_driver = {
551         .driver = {
552                 .name = ASUS_HOTK_FILE,
553                 .owner = THIS_MODULE,
554         }
555 };
556
557 static struct platform_device *asuspf_device;
558
559
560 static void asus_hotk_add_fs(void)
561 {
562         ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
563
564         if (wl_switch_handle)
565                 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
566
567         if (bt_switch_handle)
568                 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
569                                      show_bluetooth, store_bluetooth);
570 }
571
572 static int asus_handle_init(char *name, acpi_handle *handle,
573                             char **paths, int num_paths)
574 {
575         int i;
576         acpi_status status;
577
578         for (i = 0; i < num_paths; i++) {
579                 status = acpi_get_handle(NULL, paths[i], handle);
580                 if (ACPI_SUCCESS(status))
581                         return 0;
582         }
583
584         *handle = NULL;
585         return -ENODEV;
586 }
587
588 #define ASUS_HANDLE_INIT(object)                                        \
589         asus_handle_init(#object, &object##_handle, object##_paths,     \
590                          ARRAY_SIZE(object##_paths))
591
592
593 /*
594  * This function is used to initialize the hotk with right values. In this
595  * method, we can make all the detection we want, and modify the hotk struct
596  */
597 static int asus_hotk_get_info(void)
598 {
599         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
600         struct acpi_buffer dsdt = { ACPI_ALLOCATE_BUFFER, NULL };
601         union acpi_object *model = NULL;
602         int bsts_result, hwrs_result;
603         char *string = NULL;
604         acpi_status status;
605
606         /*
607          * Get DSDT headers early enough to allow for differentiating between
608          * models, but late enough to allow acpi_bus_register_driver() to fail
609          * before doing anything ACPI-specific. Should we encounter a machine,
610          * which needs special handling (i.e. its hotkey device has a different
611          * HID), this bit will be moved. A global variable asus_info contains
612          * the DSDT header.
613          */
614         status = acpi_get_table(ACPI_TABLE_ID_DSDT, 1, &dsdt);
615         if (ACPI_FAILURE(status))
616                 printk(ASUS_WARNING "Couldn't get the DSDT table header\n");
617         else
618                 asus_info = dsdt.pointer;
619
620         /* We have to write 0 on init this far for all ASUS models */
621         if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
622                 printk(ASUS_ERR "Hotkey initialization failed\n");
623                 return -ENODEV;
624         }
625
626         /* This needs to be called for some laptops to init properly */
627         if (!read_acpi_int(hotk->handle, "BSTS", &bsts_result, NULL))
628                 printk(ASUS_WARNING "Error calling BSTS\n");
629         else if (bsts_result)
630                 printk(ASUS_NOTICE "BSTS called, 0x%02x returned\n",
631                        bsts_result);
632
633         /*
634          * Try to match the object returned by INIT to the specific model.
635          * Handle every possible object (or the lack of thereof) the DSDT
636          * writers might throw at us. When in trouble, we pass NULL to
637          * asus_model_match() and try something completely different.
638          */
639         if (buffer.pointer) {
640                 model = buffer.pointer;
641                 switch (model->type) {
642                 case ACPI_TYPE_STRING:
643                         string = model->string.pointer;
644                         break;
645                 case ACPI_TYPE_BUFFER:
646                         string = model->buffer.pointer;
647                         break;
648                 default:
649                         string = "";
650                         break;
651                 }
652         }
653         hotk->name = kstrdup(string, GFP_KERNEL);
654         if (!hotk->name)
655                 return -ENOMEM;
656
657         if(*string)
658                 printk(ASUS_NOTICE "  %s model detected\n", string);
659
660         ASUS_HANDLE_INIT(mled_set);
661         ASUS_HANDLE_INIT(tled_set);
662         ASUS_HANDLE_INIT(rled_set);
663         ASUS_HANDLE_INIT(pled_set);
664
665         /*
666          * The HWRS method return informations about the hardware.
667          * 0x80 bit is for WLAN, 0x100 for Bluetooth.
668          * The significance of others is yet to be found.
669          * If we don't find the method, we assume the device are present.
670          */
671         if (!read_acpi_int(hotk->handle, "HRWS", &hwrs_result, NULL))
672                 hwrs_result = WL_HWRS | BT_HWRS;
673
674         if(hwrs_result & WL_HWRS)
675                 ASUS_HANDLE_INIT(wl_switch);
676         if(hwrs_result & BT_HWRS)
677                 ASUS_HANDLE_INIT(bt_switch);
678
679         ASUS_HANDLE_INIT(wireless_status);
680
681         ASUS_HANDLE_INIT(brightness_set);
682         ASUS_HANDLE_INIT(brightness_get);
683
684         ASUS_HANDLE_INIT(lcd_switch);
685
686         kfree(model);
687
688         return AE_OK;
689 }
690
691 static int asus_hotk_check(void)
692 {
693         int result = 0;
694
695         result = acpi_bus_get_status(hotk->device);
696         if (result)
697                 return result;
698
699         if (hotk->device->status.present) {
700                 result = asus_hotk_get_info();
701         } else {
702                 printk(ASUS_ERR "Hotkey device not present, aborting\n");
703                 return -EINVAL;
704         }
705
706         return result;
707 }
708
709 static int asus_hotk_found;
710
711 static int asus_hotk_add(struct acpi_device *device)
712 {
713         acpi_status status = AE_OK;
714         int result;
715
716         if (!device)
717                 return -EINVAL;
718
719         printk(ASUS_NOTICE "Asus Laptop Support version %s\n",
720                ASUS_LAPTOP_VERSION);
721
722         hotk = kmalloc(sizeof(struct asus_hotk), GFP_KERNEL);
723         if (!hotk)
724                 return -ENOMEM;
725         memset(hotk, 0, sizeof(struct asus_hotk));
726
727         hotk->handle = device->handle;
728         strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
729         strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
730         acpi_driver_data(device) = hotk;
731         hotk->device = device;
732
733         result = asus_hotk_check();
734         if (result)
735                 goto end;
736
737         asus_hotk_add_fs();
738
739         /*
740          * We install the handler, it will receive the hotk in parameter, so, we
741          * could add other data to the hotk struct
742          */
743         status = acpi_install_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
744                                              asus_hotk_notify, hotk);
745         if (ACPI_FAILURE(status))
746                 printk(ASUS_ERR "Error installing notify handler\n");
747
748         asus_hotk_found = 1;
749
750         /* WLED and BLED are on by default */
751         write_status(bt_switch_handle, 1, BT_ON, 0);
752         write_status(wl_switch_handle, 1, WL_ON, 0);
753
754         /* LCD Backlight is on by default */
755         write_status(NULL, 1, LCD_ON, 0);
756
757       end:
758         if (result) {
759                 kfree(hotk->name);
760                 kfree(hotk);
761         }
762
763         return result;
764 }
765
766 static int asus_hotk_remove(struct acpi_device *device, int type)
767 {
768         acpi_status status = 0;
769
770         if (!device || !acpi_driver_data(device))
771                 return -EINVAL;
772
773         status = acpi_remove_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
774                                             asus_hotk_notify);
775         if (ACPI_FAILURE(status))
776                 printk(ASUS_ERR "Error removing notify handler\n");
777
778         kfree(hotk->name);
779         kfree(hotk);
780
781         return 0;
782 }
783
784 static void asus_backlight_exit(void)
785 {
786         if(asus_backlight_device)
787                 backlight_device_unregister(asus_backlight_device);
788 }
789
790 #define  ASUS_LED_UNREGISTER(object)                            \
791         if(object##_led.class_dev                               \
792            && !IS_ERR(object##_led.class_dev))                  \
793                 led_classdev_unregister(&object##_led)
794
795 static void asus_led_exit(void)
796 {
797         ASUS_LED_UNREGISTER(mled);
798         ASUS_LED_UNREGISTER(tled);
799         ASUS_LED_UNREGISTER(pled);
800         ASUS_LED_UNREGISTER(rled);
801
802         destroy_workqueue(led_workqueue);
803 }
804
805 static void __exit asus_laptop_exit(void)
806 {
807         asus_backlight_exit();
808         asus_led_exit();
809
810         acpi_bus_unregister_driver(&asus_hotk_driver);
811         sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
812         platform_device_unregister(asuspf_device);
813         platform_driver_unregister(&asuspf_driver);
814
815         kfree(asus_info);
816 }
817
818 static int asus_backlight_init(struct device * dev)
819 {
820         struct backlight_device *bd;
821
822         if(brightness_set_handle && lcd_switch_handle) {
823                 bd = backlight_device_register (ASUS_HOTK_FILE, dev,
824                                                 NULL, &asusbl_data);
825                 if (IS_ERR (bd)) {
826                         printk(ASUS_ERR
827                                "Could not register asus backlight device\n");
828                         asus_backlight_device = NULL;
829                         return PTR_ERR(bd);
830                 }
831
832                 asus_backlight_device = bd;
833
834                 down(&bd->sem);
835                 if(likely(bd->props)) {
836                         bd->props->brightness = read_brightness(NULL);
837                         bd->props->power = FB_BLANK_UNBLANK;
838                         if(likely(bd->props->update_status))
839                            bd->props->update_status(bd);
840                 }
841                 up(&bd->sem);
842         }
843         return 0;
844 }
845
846 static int asus_led_register(acpi_handle handle,
847                              struct led_classdev * ldev,
848                              struct device * dev)
849 {
850         if(!handle)
851                 return 0;
852
853         return led_classdev_register(dev, ldev);
854 }
855 #define ASUS_LED_REGISTER(object, device)                               \
856         asus_led_register(object##_set_handle, &object##_led, device)
857
858 static int asus_led_init(struct device * dev)
859 {
860         int rv;
861
862         rv = ASUS_LED_REGISTER(mled, dev);
863         if(rv)
864                 return rv;
865
866         rv = ASUS_LED_REGISTER(tled, dev);
867         if(rv)
868                 return rv;
869
870         rv = ASUS_LED_REGISTER(rled, dev);
871         if(rv)
872                 return rv;
873
874         rv = ASUS_LED_REGISTER(pled, dev);
875         if(rv)
876                 return rv;
877
878         led_workqueue = create_singlethread_workqueue("led_workqueue");
879         if(!led_workqueue)
880                 return -ENOMEM;
881
882         return 0;
883 }
884
885 static int __init asus_laptop_init(void)
886 {
887         struct device *dev;
888         int result;
889
890         if (acpi_disabled)
891                 return -ENODEV;
892
893         if (!acpi_specific_hotkey_enabled) {
894                 printk(ASUS_ERR "Using generic hotkey driver\n");
895                 return -ENODEV;
896         }
897
898         result = acpi_bus_register_driver(&asus_hotk_driver);
899         if (result < 0)
900                 return result;
901
902         /*
903          * This is a bit of a kludge.  We only want this module loaded
904          * for ASUS systems, but there's currently no way to probe the
905          * ACPI namespace for ASUS HIDs.  So we just return failure if
906          * we didn't find one, which will cause the module to be
907          * unloaded.
908          */
909         if (!asus_hotk_found) {
910                 acpi_bus_unregister_driver(&asus_hotk_driver);
911                 return -ENODEV;
912         }
913
914         dev = acpi_get_physical_device(hotk->device->handle);
915
916         result = asus_backlight_init(dev);
917         if(result)
918                 goto fail_backlight;
919
920         result = asus_led_init(dev);
921         if(result)
922                 goto fail_led;
923
924         /* Register platform stuff */
925         result = platform_driver_register(&asuspf_driver);
926         if (result)
927                 goto fail_platform_driver;
928
929         asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
930         if (!asuspf_device) {
931                 result = -ENOMEM;
932                 goto fail_platform_device1;
933         }
934
935         result = platform_device_add(asuspf_device);
936         if (result)
937                 goto fail_platform_device2;
938
939         result = sysfs_create_group(&asuspf_device->dev.kobj,
940                                     &asuspf_attribute_group);
941         if (result)
942                 goto fail_sysfs;
943
944         return 0;
945
946 fail_sysfs:
947         platform_device_del(asuspf_device);
948
949 fail_platform_device2:
950         platform_device_put(asuspf_device);
951
952 fail_platform_device1:
953         platform_driver_unregister(&asuspf_driver);
954
955 fail_platform_driver:
956         asus_led_exit();
957
958 fail_led:
959         asus_backlight_exit();
960
961 fail_backlight:
962
963         return result;
964 }
965
966 module_init(asus_laptop_init);
967 module_exit(asus_laptop_exit);