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