Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[linux-2.6] / drivers / acpi / asus_acpi.c
1 /*
2  *  asus_acpi.c - Asus Laptop ACPI Extras
3  *
4  *
5  *  Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  *
22  *  The development page for this driver is located at
23  *  http://sourceforge.net/projects/acpi4asus/
24  *
25  *  Credits:
26  *  Pontus Fuchs   - Helper functions, cleanup
27  *  Johann Wiesner - Small compile fixes
28  *  John Belmonte  - ACPI code for Toshiba laptop was a good starting point.
29  *  �ic Burghard  - LED display support for W1N
30  *
31  */
32
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/types.h>
37 #include <linux/proc_fs.h>
38 #include <linux/backlight.h>
39 #include <acpi/acpi_drivers.h>
40 #include <acpi/acpi_bus.h>
41 #include <asm/uaccess.h>
42
43 #define ASUS_ACPI_VERSION "0.30"
44
45 #define PROC_ASUS       "asus"  //the directory
46 #define PROC_MLED       "mled"
47 #define PROC_WLED       "wled"
48 #define PROC_TLED       "tled"
49 #define PROC_BT         "bluetooth"
50 #define PROC_LEDD       "ledd"
51 #define PROC_INFO       "info"
52 #define PROC_LCD        "lcd"
53 #define PROC_BRN        "brn"
54 #define PROC_DISP       "disp"
55
56 #define ACPI_HOTK_NAME          "Asus Laptop ACPI Extras Driver"
57 #define ACPI_HOTK_CLASS         "hotkey"
58 #define ACPI_HOTK_DEVICE_NAME   "Hotkey"
59
60 /*
61  * Some events we use, same for all Asus
62  */
63 #define BR_UP       0x10
64 #define BR_DOWN     0x20
65
66 /*
67  * Flags for hotk status
68  */
69 #define MLED_ON     0x01        //mail LED
70 #define WLED_ON     0x02        //wireless LED
71 #define TLED_ON     0x04        //touchpad LED
72 #define BT_ON       0x08        //internal Bluetooth
73
74 MODULE_AUTHOR("Julien Lerouge, Karol Kozimor");
75 MODULE_DESCRIPTION(ACPI_HOTK_NAME);
76 MODULE_LICENSE("GPL");
77
78 static uid_t asus_uid;
79 static gid_t asus_gid;
80 module_param(asus_uid, uint, 0);
81 MODULE_PARM_DESC(asus_uid, "UID for entries in /proc/acpi/asus");
82 module_param(asus_gid, uint, 0);
83 MODULE_PARM_DESC(asus_gid, "GID for entries in /proc/acpi/asus");
84
85 /* For each model, all features implemented, 
86  * those marked with R are relative to HOTK, A for absolute */
87 struct model_data {
88         char *name;             //name of the laptop________________A
89         char *mt_mled;          //method to handle mled_____________R
90         char *mled_status;      //node to handle mled reading_______A
91         char *mt_wled;          //method to handle wled_____________R
92         char *wled_status;      //node to handle wled reading_______A
93         char *mt_tled;          //method to handle tled_____________R
94         char *tled_status;      //node to handle tled reading_______A
95         char *mt_ledd;          //method to handle LED display______R
96         char *mt_bt_switch;     //method to switch Bluetooth on/off_R
97         char *bt_status;        //no model currently supports this__?
98         char *mt_lcd_switch;    //method to turn LCD on/off_________A
99         char *lcd_status;       //node to read LCD panel state______A
100         char *brightness_up;    //method to set brightness up_______A
101         char *brightness_down;  //guess what ?______________________A
102         char *brightness_set;   //method to set absolute brightness_R
103         char *brightness_get;   //method to get absolute brightness_R
104         char *brightness_status;        //node to get brightness____________A
105         char *display_set;      //method to set video output________R
106         char *display_get;      //method to get video output________R
107 };
108
109 /*
110  * This is the main structure, we can use it to store anything interesting
111  * about the hotk device
112  */
113 struct asus_hotk {
114         struct acpi_device *device;     //the device we are in
115         acpi_handle handle;     //the handle of the hotk device
116         char status;            //status of the hotk, for LEDs, ...
117         u32 ledd_status;        //status of the LED display
118         struct model_data *methods;     //methods available on the laptop
119         u8 brightness;          //brightness level
120         enum {
121                 A1x = 0,        //A1340D, A1300F
122                 A2x,            //A2500H
123                 A4G,            //A4700G
124                 D1x,            //D1
125                 L2D,            //L2000D
126                 L3C,            //L3800C
127                 L3D,            //L3400D
128                 L3H,            //L3H, L2000E, L5D
129                 L4R,            //L4500R
130                 L5x,            //L5800C 
131                 L8L,            //L8400L
132                 M1A,            //M1300A
133                 M2E,            //M2400E, L4400L
134                 M6N,            //M6800N, W3400N
135                 M6R,            //M6700R, A3000G
136                 P30,            //Samsung P30
137                 S1x,            //S1300A, but also L1400B and M2400A (L84F)
138                 S2x,            //S200 (J1 reported), Victor MP-XP7210
139                 W1N,            //W1000N
140                 W5A,            //W5A
141                 W3V,            //W3030V
142                 xxN,            //M2400N, M3700N, M5200N, M6800N, S1300N, S5200N
143                 A4S,            //Z81sp
144                 //(Centrino)
145                 F3Sa,
146                 END_MODEL
147         } model;                //Models currently supported
148         u16 event_count[128];   //count for each event TODO make this better
149 };
150
151 /* Here we go */
152 #define A1x_PREFIX "\\_SB.PCI0.ISA.EC0."
153 #define L3C_PREFIX "\\_SB.PCI0.PX40.ECD0."
154 #define M1A_PREFIX "\\_SB.PCI0.PX40.EC0."
155 #define P30_PREFIX "\\_SB.PCI0.LPCB.EC0."
156 #define S1x_PREFIX "\\_SB.PCI0.PX40."
157 #define S2x_PREFIX A1x_PREFIX
158 #define xxN_PREFIX "\\_SB.PCI0.SBRG.EC0."
159
160 static struct model_data model_conf[END_MODEL] = {
161         /*
162          * TODO I have seen a SWBX and AIBX method on some models, like L1400B,
163          * it seems to be a kind of switch, but what for ?
164          */
165
166         {
167          .name = "A1x",
168          .mt_mled = "MLED",
169          .mled_status = "\\MAIL",
170          .mt_lcd_switch = A1x_PREFIX "_Q10",
171          .lcd_status = "\\BKLI",
172          .brightness_up = A1x_PREFIX "_Q0E",
173          .brightness_down = A1x_PREFIX "_Q0F"},
174
175         {
176          .name = "A2x",
177          .mt_mled = "MLED",
178          .mt_wled = "WLED",
179          .wled_status = "\\SG66",
180          .mt_lcd_switch = "\\Q10",
181          .lcd_status = "\\BAOF",
182          .brightness_set = "SPLV",
183          .brightness_get = "GPLV",
184          .display_set = "SDSP",
185          .display_get = "\\INFB"},
186
187         {
188          .name = "A4G",
189          .mt_mled = "MLED",
190 /* WLED present, but not controlled by ACPI */
191          .mt_lcd_switch = xxN_PREFIX "_Q10",
192          .brightness_set = "SPLV",
193          .brightness_get = "GPLV",
194          .display_set = "SDSP",
195          .display_get = "\\ADVG"},
196
197         {
198          .name = "D1x",
199          .mt_mled = "MLED",
200          .mt_lcd_switch = "\\Q0D",
201          .lcd_status = "\\GP11",
202          .brightness_up = "\\Q0C",
203          .brightness_down = "\\Q0B",
204          .brightness_status = "\\BLVL",
205          .display_set = "SDSP",
206          .display_get = "\\INFB"},
207
208         {
209          .name = "L2D",
210          .mt_mled = "MLED",
211          .mled_status = "\\SGP6",
212          .mt_wled = "WLED",
213          .wled_status = "\\RCP3",
214          .mt_lcd_switch = "\\Q10",
215          .lcd_status = "\\SGP0",
216          .brightness_up = "\\Q0E",
217          .brightness_down = "\\Q0F",
218          .display_set = "SDSP",
219          .display_get = "\\INFB"},
220
221         {
222          .name = "L3C",
223          .mt_mled = "MLED",
224          .mt_wled = "WLED",
225          .mt_lcd_switch = L3C_PREFIX "_Q10",
226          .lcd_status = "\\GL32",
227          .brightness_set = "SPLV",
228          .brightness_get = "GPLV",
229          .display_set = "SDSP",
230          .display_get = "\\_SB.PCI0.PCI1.VGAC.NMAP"},
231
232         {
233          .name = "L3D",
234          .mt_mled = "MLED",
235          .mled_status = "\\MALD",
236          .mt_wled = "WLED",
237          .mt_lcd_switch = "\\Q10",
238          .lcd_status = "\\BKLG",
239          .brightness_set = "SPLV",
240          .brightness_get = "GPLV",
241          .display_set = "SDSP",
242          .display_get = "\\INFB"},
243
244         {
245          .name = "L3H",
246          .mt_mled = "MLED",
247          .mt_wled = "WLED",
248          .mt_lcd_switch = "EHK",
249          .lcd_status = "\\_SB.PCI0.PM.PBC",
250          .brightness_set = "SPLV",
251          .brightness_get = "GPLV",
252          .display_set = "SDSP",
253          .display_get = "\\INFB"},
254
255         {
256          .name = "L4R",
257          .mt_mled = "MLED",
258          .mt_wled = "WLED",
259          .wled_status = "\\_SB.PCI0.SBRG.SG13",
260          .mt_lcd_switch = xxN_PREFIX "_Q10",
261          .lcd_status = "\\_SB.PCI0.SBSM.SEO4",
262          .brightness_set = "SPLV",
263          .brightness_get = "GPLV",
264          .display_set = "SDSP",
265          .display_get = "\\_SB.PCI0.P0P1.VGA.GETD"},
266
267         {
268          .name = "L5x",
269          .mt_mled = "MLED",
270 /* WLED present, but not controlled by ACPI */
271          .mt_tled = "TLED",
272          .mt_lcd_switch = "\\Q0D",
273          .lcd_status = "\\BAOF",
274          .brightness_set = "SPLV",
275          .brightness_get = "GPLV",
276          .display_set = "SDSP",
277          .display_get = "\\INFB"},
278
279         {
280          .name = "L8L"
281 /* No features, but at least support the hotkeys */
282          },
283
284         {
285          .name = "M1A",
286          .mt_mled = "MLED",
287          .mt_lcd_switch = M1A_PREFIX "Q10",
288          .lcd_status = "\\PNOF",
289          .brightness_up = M1A_PREFIX "Q0E",
290          .brightness_down = M1A_PREFIX "Q0F",
291          .brightness_status = "\\BRIT",
292          .display_set = "SDSP",
293          .display_get = "\\INFB"},
294
295         {
296          .name = "M2E",
297          .mt_mled = "MLED",
298          .mt_wled = "WLED",
299          .mt_lcd_switch = "\\Q10",
300          .lcd_status = "\\GP06",
301          .brightness_set = "SPLV",
302          .brightness_get = "GPLV",
303          .display_set = "SDSP",
304          .display_get = "\\INFB"},
305
306         {
307          .name = "M6N",
308          .mt_mled = "MLED",
309          .mt_wled = "WLED",
310          .wled_status = "\\_SB.PCI0.SBRG.SG13",
311          .mt_lcd_switch = xxN_PREFIX "_Q10",
312          .lcd_status = "\\_SB.BKLT",
313          .brightness_set = "SPLV",
314          .brightness_get = "GPLV",
315          .display_set = "SDSP",
316          .display_get = "\\SSTE"},
317
318         {
319          .name = "M6R",
320          .mt_mled = "MLED",
321          .mt_wled = "WLED",
322          .mt_lcd_switch = xxN_PREFIX "_Q10",
323          .lcd_status = "\\_SB.PCI0.SBSM.SEO4",
324          .brightness_set = "SPLV",
325          .brightness_get = "GPLV",
326          .display_set = "SDSP",
327          .display_get = "\\_SB.PCI0.P0P1.VGA.GETD"},
328
329         {
330          .name = "P30",
331          .mt_wled = "WLED",
332          .mt_lcd_switch = P30_PREFIX "_Q0E",
333          .lcd_status = "\\BKLT",
334          .brightness_up = P30_PREFIX "_Q68",
335          .brightness_down = P30_PREFIX "_Q69",
336          .brightness_get = "GPLV",
337          .display_set = "SDSP",
338          .display_get = "\\DNXT"},
339
340         {
341          .name = "S1x",
342          .mt_mled = "MLED",
343          .mled_status = "\\EMLE",
344          .mt_wled = "WLED",
345          .mt_lcd_switch = S1x_PREFIX "Q10",
346          .lcd_status = "\\PNOF",
347          .brightness_set = "SPLV",
348          .brightness_get = "GPLV"},
349
350         {
351          .name = "S2x",
352          .mt_mled = "MLED",
353          .mled_status = "\\MAIL",
354          .mt_lcd_switch = S2x_PREFIX "_Q10",
355          .lcd_status = "\\BKLI",
356          .brightness_up = S2x_PREFIX "_Q0B",
357          .brightness_down = S2x_PREFIX "_Q0A"},
358
359         {
360          .name = "W1N",
361          .mt_mled = "MLED",
362          .mt_wled = "WLED",
363          .mt_ledd = "SLCM",
364          .mt_lcd_switch = xxN_PREFIX "_Q10",
365          .lcd_status = "\\BKLT",
366          .brightness_set = "SPLV",
367          .brightness_get = "GPLV",
368          .display_set = "SDSP",
369          .display_get = "\\ADVG"},
370
371         {
372          .name = "W5A",
373          .mt_bt_switch = "BLED",
374          .mt_wled = "WLED",
375          .mt_lcd_switch = xxN_PREFIX "_Q10",
376          .brightness_set = "SPLV",
377          .brightness_get = "GPLV",
378          .display_set = "SDSP",
379          .display_get = "\\ADVG"},
380
381         {
382          .name = "W3V",
383          .mt_mled = "MLED",
384          .mt_wled = "WLED",
385          .mt_lcd_switch = xxN_PREFIX "_Q10",
386          .lcd_status = "\\BKLT",
387          .brightness_set = "SPLV",
388          .brightness_get = "GPLV",
389          .display_set = "SDSP",
390          .display_get = "\\INFB"},
391
392        {
393          .name = "xxN",
394          .mt_mled = "MLED",
395 /* WLED present, but not controlled by ACPI */
396          .mt_lcd_switch = xxN_PREFIX "_Q10",
397          .lcd_status = "\\BKLT",
398          .brightness_set = "SPLV",
399          .brightness_get = "GPLV",
400          .display_set = "SDSP",
401         .display_get = "\\ADVG"},
402
403         {
404                 .name              = "A4S",
405                 .brightness_set    = "SPLV",
406                 .brightness_get    = "GPLV",
407                 .mt_bt_switch      = "BLED",
408                 .mt_wled           = "WLED"
409         },
410
411         {
412                 .name           = "F3Sa",
413                 .mt_bt_switch   = "BLED",
414                 .mt_wled        = "WLED",
415                 .mt_mled        = "MLED",
416                 .brightness_get = "GPLV",
417                 .brightness_set = "SPLV",
418                 .mt_lcd_switch  = "\\_SB.PCI0.SBRG.EC0._Q10",
419                 .lcd_status     = "\\_SB.PCI0.SBRG.EC0.RPIN",
420                 .display_get    = "\\ADVG",
421                 .display_set    = "SDSP",
422         },
423
424 };
425
426 /* procdir we use */
427 static struct proc_dir_entry *asus_proc_dir;
428
429 static struct backlight_device *asus_backlight_device;
430
431 /*
432  * This header is made available to allow proper configuration given model,
433  * revision number , ... this info cannot go in struct asus_hotk because it is
434  * available before the hotk
435  */
436 static struct acpi_table_header *asus_info;
437
438 /* The actual device the driver binds to */
439 static struct asus_hotk *hotk;
440
441 /*
442  * The hotkey driver and autoloading declaration
443  */
444 static int asus_hotk_add(struct acpi_device *device);
445 static int asus_hotk_remove(struct acpi_device *device, int type);
446 static const struct acpi_device_id asus_device_ids[] = {
447         {"ATK0100", 0},
448         {"", 0},
449 };
450 MODULE_DEVICE_TABLE(acpi, asus_device_ids);
451
452 static struct acpi_driver asus_hotk_driver = {
453         .name = "asus_acpi",
454         .class = ACPI_HOTK_CLASS,
455         .ids = asus_device_ids,
456         .ops = {
457                 .add = asus_hotk_add,
458                 .remove = asus_hotk_remove,
459                 },
460 };
461
462 /* 
463  * This function evaluates an ACPI method, given an int as parameter, the
464  * method is searched within the scope of the handle, can be NULL. The output
465  * of the method is written is output, which can also be NULL
466  *
467  * returns 1 if write is successful, 0 else. 
468  */
469 static int write_acpi_int(acpi_handle handle, const char *method, int val,
470                           struct acpi_buffer *output)
471 {
472         struct acpi_object_list params; //list of input parameters (an int here)
473         union acpi_object in_obj;       //the only param we use
474         acpi_status status;
475
476         params.count = 1;
477         params.pointer = &in_obj;
478         in_obj.type = ACPI_TYPE_INTEGER;
479         in_obj.integer.value = val;
480
481         status = acpi_evaluate_object(handle, (char *)method, &params, output);
482         return (status == AE_OK);
483 }
484
485 static int read_acpi_int(acpi_handle handle, const char *method, int *val)
486 {
487         struct acpi_buffer output;
488         union acpi_object out_obj;
489         acpi_status status;
490
491         output.length = sizeof(out_obj);
492         output.pointer = &out_obj;
493
494         status = acpi_evaluate_object(handle, (char *)method, NULL, &output);
495         *val = out_obj.integer.value;
496         return (status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER);
497 }
498
499 /*
500  * We write our info in page, we begin at offset off and cannot write more
501  * than count bytes. We set eof to 1 if we handle those 2 values. We return the
502  * number of bytes written in page
503  */
504 static int
505 proc_read_info(char *page, char **start, off_t off, int count, int *eof,
506                void *data)
507 {
508         int len = 0;
509         int temp;
510         char buf[16];           //enough for all info
511         /*
512          * We use the easy way, we don't care of off and count, so we don't set eof
513          * to 1
514          */
515
516         len += sprintf(page, ACPI_HOTK_NAME " " ASUS_ACPI_VERSION "\n");
517         len += sprintf(page + len, "Model reference    : %s\n",
518                        hotk->methods->name);
519         /* 
520          * The SFUN method probably allows the original driver to get the list 
521          * of features supported by a given model. For now, 0x0100 or 0x0800 
522          * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
523          * The significance of others is yet to be found.
524          */
525         if (read_acpi_int(hotk->handle, "SFUN", &temp))
526                 len +=
527                     sprintf(page + len, "SFUN value         : 0x%04x\n", temp);
528         /*
529          * Another value for userspace: the ASYM method returns 0x02 for
530          * battery low and 0x04 for battery critical, its readings tend to be
531          * more accurate than those provided by _BST. 
532          * Note: since not all the laptops provide this method, errors are
533          * silently ignored.
534          */
535         if (read_acpi_int(hotk->handle, "ASYM", &temp))
536                 len +=
537                     sprintf(page + len, "ASYM value         : 0x%04x\n", temp);
538         if (asus_info) {
539                 snprintf(buf, 16, "%d", asus_info->length);
540                 len += sprintf(page + len, "DSDT length        : %s\n", buf);
541                 snprintf(buf, 16, "%d", asus_info->checksum);
542                 len += sprintf(page + len, "DSDT checksum      : %s\n", buf);
543                 snprintf(buf, 16, "%d", asus_info->revision);
544                 len += sprintf(page + len, "DSDT revision      : %s\n", buf);
545                 snprintf(buf, 7, "%s", asus_info->oem_id);
546                 len += sprintf(page + len, "OEM id             : %s\n", buf);
547                 snprintf(buf, 9, "%s", asus_info->oem_table_id);
548                 len += sprintf(page + len, "OEM table id       : %s\n", buf);
549                 snprintf(buf, 16, "%x", asus_info->oem_revision);
550                 len += sprintf(page + len, "OEM revision       : 0x%s\n", buf);
551                 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
552                 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
553                 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
554                 len += sprintf(page + len, "ASL comp revision  : 0x%s\n", buf);
555         }
556
557         return len;
558 }
559
560 /*
561  * /proc handlers
562  * We write our info in page, we begin at offset off and cannot write more
563  * than count bytes. We set eof to 1 if we handle those 2 values. We return the
564  * number of bytes written in page
565  */
566
567 /* Generic LED functions */
568 static int read_led(const char *ledname, int ledmask)
569 {
570         if (ledname) {
571                 int led_status;
572
573                 if (read_acpi_int(NULL, ledname, &led_status))
574                         return led_status;
575                 else
576                         printk(KERN_WARNING "Asus ACPI: Error reading LED "
577                                "status\n");
578         }
579         return (hotk->status & ledmask) ? 1 : 0;
580 }
581
582 static int parse_arg(const char __user * buf, unsigned long count, int *val)
583 {
584         char s[32];
585         if (!count)
586                 return 0;
587         if (count > 31)
588                 return -EINVAL;
589         if (copy_from_user(s, buf, count))
590                 return -EFAULT;
591         s[count] = 0;
592         if (sscanf(s, "%i", val) != 1)
593                 return -EINVAL;
594         return count;
595 }
596
597 /* FIXME: kill extraneous args so it can be called independently */
598 static int
599 write_led(const char __user * buffer, unsigned long count,
600           char *ledname, int ledmask, int invert)
601 {
602         int rv, value;
603         int led_out = 0;
604
605         rv = parse_arg(buffer, count, &value);
606         if (rv > 0)
607                 led_out = value ? 1 : 0;
608
609         hotk->status =
610             (led_out) ? (hotk->status | ledmask) : (hotk->status & ~ledmask);
611
612         if (invert)             /* invert target value */
613                 led_out = !led_out;
614
615         if (!write_acpi_int(hotk->handle, ledname, led_out, NULL))
616                 printk(KERN_WARNING "Asus ACPI: LED (%s) write failed\n",
617                        ledname);
618
619         return rv;
620 }
621
622 /*
623  * Proc handlers for MLED
624  */
625 static int
626 proc_read_mled(char *page, char **start, off_t off, int count, int *eof,
627                void *data)
628 {
629         return sprintf(page, "%d\n",
630                        read_led(hotk->methods->mled_status, MLED_ON));
631 }
632
633 static int
634 proc_write_mled(struct file *file, const char __user * buffer,
635                 unsigned long count, void *data)
636 {
637         return write_led(buffer, count, hotk->methods->mt_mled, MLED_ON, 1);
638 }
639
640 /*
641  * Proc handlers for LED display
642  */
643 static int
644 proc_read_ledd(char *page, char **start, off_t off, int count, int *eof,
645                void *data)
646 {
647         return sprintf(page, "0x%08x\n", hotk->ledd_status);
648 }
649
650 static int
651 proc_write_ledd(struct file *file, const char __user * buffer,
652                 unsigned long count, void *data)
653 {
654         int rv, value;
655
656         rv = parse_arg(buffer, count, &value);
657         if (rv > 0) {
658                 if (!write_acpi_int
659                     (hotk->handle, hotk->methods->mt_ledd, value, NULL))
660                         printk(KERN_WARNING
661                                "Asus ACPI: LED display write failed\n");
662                 else
663                         hotk->ledd_status = (u32) value;
664         }
665         return rv;
666 }
667
668 /*
669  * Proc handlers for WLED
670  */
671 static int
672 proc_read_wled(char *page, char **start, off_t off, int count, int *eof,
673                void *data)
674 {
675         return sprintf(page, "%d\n",
676                        read_led(hotk->methods->wled_status, WLED_ON));
677 }
678
679 static int
680 proc_write_wled(struct file *file, const char __user * buffer,
681                 unsigned long count, void *data)
682 {
683         return write_led(buffer, count, hotk->methods->mt_wled, WLED_ON, 0);
684 }
685
686 /*
687  * Proc handlers for Bluetooth
688  */
689 static int
690 proc_read_bluetooth(char *page, char **start, off_t off, int count, int *eof,
691                     void *data)
692 {
693         return sprintf(page, "%d\n", read_led(hotk->methods->bt_status, BT_ON));
694 }
695
696 static int
697 proc_write_bluetooth(struct file *file, const char __user * buffer,
698                      unsigned long count, void *data)
699 {
700         /* Note: mt_bt_switch controls both internal Bluetooth adapter's 
701            presence and its LED */
702         return write_led(buffer, count, hotk->methods->mt_bt_switch, BT_ON, 0);
703 }
704
705 /*
706  * Proc handlers for TLED
707  */
708 static int
709 proc_read_tled(char *page, char **start, off_t off, int count, int *eof,
710                void *data)
711 {
712         return sprintf(page, "%d\n",
713                        read_led(hotk->methods->tled_status, TLED_ON));
714 }
715
716 static int
717 proc_write_tled(struct file *file, const char __user * buffer,
718                 unsigned long count, void *data)
719 {
720         return write_led(buffer, count, hotk->methods->mt_tled, TLED_ON, 0);
721 }
722
723 static int get_lcd_state(void)
724 {
725         int lcd = 0;
726
727         if (hotk->model == L3H) {
728                 /* L3H and the like have to be handled differently */
729                 acpi_status status = 0;
730                 struct acpi_object_list input;
731                 union acpi_object mt_params[2];
732                 struct acpi_buffer output;
733                 union acpi_object out_obj;
734
735                 input.count = 2;
736                 input.pointer = mt_params;
737                 /* Note: the following values are partly guessed up, but 
738                    otherwise they seem to work */
739                 mt_params[0].type = ACPI_TYPE_INTEGER;
740                 mt_params[0].integer.value = 0x02;
741                 mt_params[1].type = ACPI_TYPE_INTEGER;
742                 mt_params[1].integer.value = 0x02;
743
744                 output.length = sizeof(out_obj);
745                 output.pointer = &out_obj;
746
747                 status =
748                     acpi_evaluate_object(NULL, hotk->methods->lcd_status,
749                                          &input, &output);
750                 if (status != AE_OK)
751                         return -1;
752                 if (out_obj.type == ACPI_TYPE_INTEGER)
753                         /* That's what the AML code does */
754                         lcd = out_obj.integer.value >> 8;
755         } else if (hotk->model == F3Sa) {
756                 unsigned long tmp;
757                 union acpi_object param;
758                 struct acpi_object_list input;
759                 acpi_status status;
760
761                 /* Read pin 11 */
762                 param.type = ACPI_TYPE_INTEGER;
763                 param.integer.value = 0x11;
764                 input.count = 1;
765                 input.pointer = &param;
766
767                 status = acpi_evaluate_integer(NULL, hotk->methods->lcd_status,
768                                                 &input, &tmp);
769                 if (status != AE_OK)
770                         return -1;
771
772                 lcd = tmp;
773         } else {
774                 /* We don't have to check anything if we are here */
775                 if (!read_acpi_int(NULL, hotk->methods->lcd_status, &lcd))
776                         printk(KERN_WARNING
777                                "Asus ACPI: Error reading LCD status\n");
778
779                 if (hotk->model == L2D)
780                         lcd = ~lcd;
781         }
782
783         return (lcd & 1);
784 }
785
786 static int set_lcd_state(int value)
787 {
788         int lcd = 0;
789         acpi_status status = 0;
790
791         lcd = value ? 1 : 0;
792         if (lcd != get_lcd_state()) {
793                 /* switch */
794                 if (hotk->model != L3H) {
795                         status =
796                             acpi_evaluate_object(NULL,
797                                                  hotk->methods->mt_lcd_switch,
798                                                  NULL, NULL);
799                 } else {        /* L3H and the like have to be handled differently */
800                         if (!write_acpi_int
801                             (hotk->handle, hotk->methods->mt_lcd_switch, 0x07,
802                              NULL))
803                                 status = AE_ERROR;
804                         /* L3H's AML executes EHK (0x07) upon Fn+F7 keypress, 
805                            the exact behaviour is simulated here */
806                 }
807                 if (ACPI_FAILURE(status))
808                         printk(KERN_WARNING "Asus ACPI: Error switching LCD\n");
809         }
810         return 0;
811
812 }
813
814 static int
815 proc_read_lcd(char *page, char **start, off_t off, int count, int *eof,
816               void *data)
817 {
818         return sprintf(page, "%d\n", get_lcd_state());
819 }
820
821 static int
822 proc_write_lcd(struct file *file, const char __user * buffer,
823                unsigned long count, void *data)
824 {
825         int rv, value;
826
827         rv = parse_arg(buffer, count, &value);
828         if (rv > 0)
829                 set_lcd_state(value);
830         return rv;
831 }
832
833 static int read_brightness(struct backlight_device *bd)
834 {
835         int value;
836
837         if (hotk->methods->brightness_get) {    /* SPLV/GPLV laptop */
838                 if (!read_acpi_int(hotk->handle, hotk->methods->brightness_get,
839                                    &value))
840                         printk(KERN_WARNING
841                                "Asus ACPI: Error reading brightness\n");
842         } else if (hotk->methods->brightness_status) {  /* For D1 for example */
843                 if (!read_acpi_int(NULL, hotk->methods->brightness_status,
844                                    &value))
845                         printk(KERN_WARNING
846                                "Asus ACPI: Error reading brightness\n");
847         } else                  /* No GPLV method */
848                 value = hotk->brightness;
849         return value;
850 }
851
852 /*
853  * Change the brightness level
854  */
855 static int set_brightness(int value)
856 {
857         acpi_status status = 0;
858         int ret = 0;
859
860         /* SPLV laptop */
861         if (hotk->methods->brightness_set) {
862                 if (!write_acpi_int(hotk->handle, hotk->methods->brightness_set,
863                                     value, NULL))
864                         printk(KERN_WARNING
865                                "Asus ACPI: Error changing brightness\n");
866                         ret = -EIO;
867                 goto out;
868         }
869
870         /* No SPLV method if we are here, act as appropriate */
871         value -= read_brightness(NULL);
872         while (value != 0) {
873                 status = acpi_evaluate_object(NULL, (value > 0) ?
874                                               hotk->methods->brightness_up :
875                                               hotk->methods->brightness_down,
876                                               NULL, NULL);
877                 (value > 0) ? value-- : value++;
878                 if (ACPI_FAILURE(status))
879                         printk(KERN_WARNING
880                                "Asus ACPI: Error changing brightness\n");
881                         ret = -EIO;
882         }
883 out:
884         return ret;
885 }
886
887 static int set_brightness_status(struct backlight_device *bd)
888 {
889         return set_brightness(bd->props.brightness);
890 }
891
892 static int
893 proc_read_brn(char *page, char **start, off_t off, int count, int *eof,
894               void *data)
895 {
896         return sprintf(page, "%d\n", read_brightness(NULL));
897 }
898
899 static int
900 proc_write_brn(struct file *file, const char __user * buffer,
901                unsigned long count, void *data)
902 {
903         int rv, value;
904
905         rv = parse_arg(buffer, count, &value);
906         if (rv > 0) {
907                 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
908                 /* 0 <= value <= 15 */
909                 set_brightness(value);
910         }
911         return rv;
912 }
913
914 static void set_display(int value)
915 {
916         /* no sanity check needed for now */
917         if (!write_acpi_int(hotk->handle, hotk->methods->display_set,
918                             value, NULL))
919                 printk(KERN_WARNING "Asus ACPI: Error setting display\n");
920         return;
921 }
922
923 /*
924  * Now, *this* one could be more user-friendly, but so far, no-one has 
925  * complained. The significance of bits is the same as in proc_write_disp()
926  */
927 static int
928 proc_read_disp(char *page, char **start, off_t off, int count, int *eof,
929                void *data)
930 {
931         int value = 0;
932
933         if (!read_acpi_int(hotk->handle, hotk->methods->display_get, &value))
934                 printk(KERN_WARNING
935                        "Asus ACPI: Error reading display status\n");
936         value &= 0x07;          /* needed for some models, shouldn't hurt others */
937         return sprintf(page, "%d\n", value);
938 }
939
940 /*
941  * Experimental support for display switching. As of now: 1 should activate 
942  * the LCD output, 2 should do for CRT, and 4 for TV-Out. Any combination 
943  * (bitwise) of these will suffice. I never actually tested 3 displays hooked up 
944  * simultaneously, so be warned. See the acpi4asus README for more info.
945  */
946 static int
947 proc_write_disp(struct file *file, const char __user * buffer,
948                 unsigned long count, void *data)
949 {
950         int rv, value;
951
952         rv = parse_arg(buffer, count, &value);
953         if (rv > 0)
954                 set_display(value);
955         return rv;
956 }
957
958 typedef int (proc_readfunc) (char *page, char **start, off_t off, int count,
959                              int *eof, void *data);
960 typedef int (proc_writefunc) (struct file * file, const char __user * buffer,
961                               unsigned long count, void *data);
962
963 static int
964 asus_proc_add(char *name, proc_writefunc * writefunc,
965                      proc_readfunc * readfunc, mode_t mode,
966                      struct acpi_device *device)
967 {
968         struct proc_dir_entry *proc =
969             create_proc_entry(name, mode, acpi_device_dir(device));
970         if (!proc) {
971                 printk(KERN_WARNING "  Unable to create %s fs entry\n", name);
972                 return -1;
973         }
974         proc->write_proc = writefunc;
975         proc->read_proc = readfunc;
976         proc->data = acpi_driver_data(device);
977         proc->owner = THIS_MODULE;
978         proc->uid = asus_uid;
979         proc->gid = asus_gid;
980         return 0;
981 }
982
983 static int asus_hotk_add_fs(struct acpi_device *device)
984 {
985         struct proc_dir_entry *proc;
986         mode_t mode;
987
988         /*
989          * If parameter uid or gid is not changed, keep the default setting for
990          * our proc entries (-rw-rw-rw-) else, it means we care about security,
991          * and then set to -rw-rw----
992          */
993
994         if ((asus_uid == 0) && (asus_gid == 0)) {
995                 mode = S_IFREG | S_IRUGO | S_IWUGO;
996         } else {
997                 mode = S_IFREG | S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP;
998                 printk(KERN_WARNING "  asus_uid and asus_gid parameters are "
999                        "deprecated, use chown and chmod instead!\n");
1000         }
1001
1002         acpi_device_dir(device) = asus_proc_dir;
1003         if (!acpi_device_dir(device))
1004                 return -ENODEV;
1005
1006         proc = create_proc_entry(PROC_INFO, mode, acpi_device_dir(device));
1007         if (proc) {
1008                 proc->read_proc = proc_read_info;
1009                 proc->data = acpi_driver_data(device);
1010                 proc->owner = THIS_MODULE;
1011                 proc->uid = asus_uid;
1012                 proc->gid = asus_gid;
1013         } else {
1014                 printk(KERN_WARNING "  Unable to create " PROC_INFO
1015                        " fs entry\n");
1016         }
1017
1018         if (hotk->methods->mt_wled) {
1019                 asus_proc_add(PROC_WLED, &proc_write_wled, &proc_read_wled,
1020                               mode, device);
1021         }
1022
1023         if (hotk->methods->mt_ledd) {
1024                 asus_proc_add(PROC_LEDD, &proc_write_ledd, &proc_read_ledd,
1025                               mode, device);
1026         }
1027
1028         if (hotk->methods->mt_mled) {
1029                 asus_proc_add(PROC_MLED, &proc_write_mled, &proc_read_mled,
1030                               mode, device);
1031         }
1032
1033         if (hotk->methods->mt_tled) {
1034                 asus_proc_add(PROC_TLED, &proc_write_tled, &proc_read_tled,
1035                               mode, device);
1036         }
1037
1038         if (hotk->methods->mt_bt_switch) {
1039                 asus_proc_add(PROC_BT, &proc_write_bluetooth,
1040                               &proc_read_bluetooth, mode, device);
1041         }
1042
1043         /* 
1044          * We need both read node and write method as LCD switch is also accessible
1045          * from keyboard 
1046          */
1047         if (hotk->methods->mt_lcd_switch && hotk->methods->lcd_status) {
1048                 asus_proc_add(PROC_LCD, &proc_write_lcd, &proc_read_lcd, mode,
1049                               device);
1050         }
1051
1052         if ((hotk->methods->brightness_up && hotk->methods->brightness_down) ||
1053             (hotk->methods->brightness_get && hotk->methods->brightness_set)) {
1054                 asus_proc_add(PROC_BRN, &proc_write_brn, &proc_read_brn, mode,
1055                               device);
1056         }
1057
1058         if (hotk->methods->display_set) {
1059                 asus_proc_add(PROC_DISP, &proc_write_disp, &proc_read_disp,
1060                               mode, device);
1061         }
1062
1063         return 0;
1064 }
1065
1066 static int asus_hotk_remove_fs(struct acpi_device *device)
1067 {
1068         if (acpi_device_dir(device)) {
1069                 remove_proc_entry(PROC_INFO, acpi_device_dir(device));
1070                 if (hotk->methods->mt_wled)
1071                         remove_proc_entry(PROC_WLED, acpi_device_dir(device));
1072                 if (hotk->methods->mt_mled)
1073                         remove_proc_entry(PROC_MLED, acpi_device_dir(device));
1074                 if (hotk->methods->mt_tled)
1075                         remove_proc_entry(PROC_TLED, acpi_device_dir(device));
1076                 if (hotk->methods->mt_ledd)
1077                         remove_proc_entry(PROC_LEDD, acpi_device_dir(device));
1078                 if (hotk->methods->mt_bt_switch)
1079                         remove_proc_entry(PROC_BT, acpi_device_dir(device));
1080                 if (hotk->methods->mt_lcd_switch && hotk->methods->lcd_status)
1081                         remove_proc_entry(PROC_LCD, acpi_device_dir(device));
1082                 if ((hotk->methods->brightness_up
1083                      && hotk->methods->brightness_down)
1084                     || (hotk->methods->brightness_get
1085                         && hotk->methods->brightness_set))
1086                         remove_proc_entry(PROC_BRN, acpi_device_dir(device));
1087                 if (hotk->methods->display_set)
1088                         remove_proc_entry(PROC_DISP, acpi_device_dir(device));
1089         }
1090         return 0;
1091 }
1092
1093 static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
1094 {
1095         /* TODO Find a better way to handle events count. */
1096         if (!hotk)
1097                 return;
1098
1099         if ((event & ~((u32) BR_UP)) < 16) {
1100                 hotk->brightness = (event & ~((u32) BR_UP));
1101         } else if ((event & ~((u32) BR_DOWN)) < 16) {
1102                 hotk->brightness = (event & ~((u32) BR_DOWN));
1103         }
1104
1105         acpi_bus_generate_proc_event(hotk->device, event,
1106                                 hotk->event_count[event % 128]++);
1107
1108         return;
1109 }
1110
1111 /*
1112  * Match the model string to the list of supported models. Return END_MODEL if
1113  * no match or model is NULL.
1114  */
1115 static int asus_model_match(char *model)
1116 {
1117         if (model == NULL)
1118                 return END_MODEL;
1119
1120         if (strncmp(model, "L3D", 3) == 0)
1121                 return L3D;
1122         else if (strncmp(model, "L2E", 3) == 0 ||
1123                  strncmp(model, "L3H", 3) == 0 || strncmp(model, "L5D", 3) == 0)
1124                 return L3H;
1125         else if (strncmp(model, "L3", 2) == 0 || strncmp(model, "L2B", 3) == 0)
1126                 return L3C;
1127         else if (strncmp(model, "L8L", 3) == 0)
1128                 return L8L;
1129         else if (strncmp(model, "L4R", 3) == 0)
1130                 return L4R;
1131         else if (strncmp(model, "M6N", 3) == 0 || strncmp(model, "W3N", 3) == 0)
1132                 return M6N;
1133         else if (strncmp(model, "M6R", 3) == 0 || strncmp(model, "A3G", 3) == 0)
1134                 return M6R;
1135         else if (strncmp(model, "M2N", 3) == 0 ||
1136                  strncmp(model, "M3N", 3) == 0 ||
1137                  strncmp(model, "M5N", 3) == 0 ||
1138                  strncmp(model, "M6N", 3) == 0 ||
1139                  strncmp(model, "S1N", 3) == 0 ||
1140                  strncmp(model, "S5N", 3) == 0 || strncmp(model, "W1N", 3) == 0)
1141                 return xxN;
1142         else if (strncmp(model, "M1", 2) == 0)
1143                 return M1A;
1144         else if (strncmp(model, "M2", 2) == 0 || strncmp(model, "L4E", 3) == 0)
1145                 return M2E;
1146         else if (strncmp(model, "L2", 2) == 0)
1147                 return L2D;
1148         else if (strncmp(model, "L8", 2) == 0)
1149                 return S1x;
1150         else if (strncmp(model, "D1", 2) == 0)
1151                 return D1x;
1152         else if (strncmp(model, "A1", 2) == 0)
1153                 return A1x;
1154         else if (strncmp(model, "A2", 2) == 0)
1155                 return A2x;
1156         else if (strncmp(model, "J1", 2) == 0)
1157                 return S2x;
1158         else if (strncmp(model, "L5", 2) == 0)
1159                 return L5x;
1160         else if (strncmp(model, "A4G", 3) == 0)
1161                 return A4G;
1162         else if (strncmp(model, "W1N", 3) == 0)
1163                 return W1N;
1164         else if (strncmp(model, "W3V", 3) == 0)
1165                 return W3V;
1166         else if (strncmp(model, "W5A", 3) == 0)
1167                 return W5A;
1168         else if (strncmp(model, "A4S", 3) == 0)
1169                 return A4S;
1170         else if (strncmp(model, "F3Sa", 4) == 0)
1171                 return F3Sa;
1172         else
1173                 return END_MODEL;
1174 }
1175
1176 /*
1177  * This function is used to initialize the hotk with right values. In this
1178  * method, we can make all the detection we want, and modify the hotk struct
1179  */
1180 static int asus_hotk_get_info(void)
1181 {
1182         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1183         union acpi_object *model = NULL;
1184         int bsts_result;
1185         char *string = NULL;
1186         acpi_status status;
1187
1188         /*
1189          * Get DSDT headers early enough to allow for differentiating between 
1190          * models, but late enough to allow acpi_bus_register_driver() to fail 
1191          * before doing anything ACPI-specific. Should we encounter a machine,
1192          * which needs special handling (i.e. its hotkey device has a different
1193          * HID), this bit will be moved. A global variable asus_info contains
1194          * the DSDT header.
1195          */
1196         status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
1197         if (ACPI_FAILURE(status))
1198                 printk(KERN_WARNING "  Couldn't get the DSDT table header\n");
1199
1200         /* We have to write 0 on init this far for all ASUS models */
1201         if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
1202                 printk(KERN_ERR "  Hotkey initialization failed\n");
1203                 return -ENODEV;
1204         }
1205
1206         /* This needs to be called for some laptops to init properly */
1207         if (!read_acpi_int(hotk->handle, "BSTS", &bsts_result))
1208                 printk(KERN_WARNING "  Error calling BSTS\n");
1209         else if (bsts_result)
1210                 printk(KERN_NOTICE "  BSTS called, 0x%02x returned\n",
1211                        bsts_result);
1212
1213         /*
1214          * Try to match the object returned by INIT to the specific model.
1215          * Handle every possible object (or the lack of thereof) the DSDT 
1216          * writers might throw at us. When in trouble, we pass NULL to 
1217          * asus_model_match() and try something completely different.
1218          */
1219         if (buffer.pointer) {
1220                 model = buffer.pointer;
1221                 switch (model->type) {
1222                 case ACPI_TYPE_STRING:
1223                         string = model->string.pointer;
1224                         break;
1225                 case ACPI_TYPE_BUFFER:
1226                         string = model->buffer.pointer;
1227                         break;
1228                 default:
1229                         kfree(model);
1230                         model = NULL;
1231                         break;
1232                 }
1233         }
1234         hotk->model = asus_model_match(string);
1235         if (hotk->model == END_MODEL) { /* match failed */
1236                 if (asus_info &&
1237                     strncmp(asus_info->oem_table_id, "ODEM", 4) == 0) {
1238                         hotk->model = P30;
1239                         printk(KERN_NOTICE
1240                                "  Samsung P30 detected, supported\n");
1241                 } else {
1242                         hotk->model = M2E;
1243                         printk(KERN_NOTICE "  unsupported model %s, trying "
1244                                "default values\n", string);
1245                         printk(KERN_NOTICE
1246                                "  send /proc/acpi/dsdt to the developers\n");
1247                 }
1248                 hotk->methods = &model_conf[hotk->model];
1249                 return AE_OK;
1250         }
1251         hotk->methods = &model_conf[hotk->model];
1252         printk(KERN_NOTICE "  %s model detected, supported\n", string);
1253
1254         /* Sort of per-model blacklist */
1255         if (strncmp(string, "L2B", 3) == 0)
1256                 hotk->methods->lcd_status = NULL;
1257         /* L2B is similar enough to L3C to use its settings, with this only 
1258            exception */
1259         else if (strncmp(string, "A3G", 3) == 0)
1260                 hotk->methods->lcd_status = "\\BLFG";
1261         /* A3G is like M6R */
1262         else if (strncmp(string, "S5N", 3) == 0 ||
1263                  strncmp(string, "M5N", 3) == 0 ||
1264                  strncmp(string, "W3N", 3) == 0)
1265                 hotk->methods->mt_mled = NULL;
1266         /* S5N, M5N and W3N have no MLED */
1267         else if (strncmp(string, "L5D", 3) == 0)
1268                 hotk->methods->mt_wled = NULL;
1269         /* L5D's WLED is not controlled by ACPI */
1270         else if (strncmp(string, "M2N", 3) == 0 ||
1271                  strncmp(string, "W3V", 3) == 0 ||
1272                  strncmp(string, "S1N", 3) == 0)
1273                 hotk->methods->mt_wled = "WLED";
1274         /* M2N, S1N and W3V have a usable WLED */
1275         else if (asus_info) {
1276                 if (strncmp(asus_info->oem_table_id, "L1", 2) == 0)
1277                         hotk->methods->mled_status = NULL;
1278                 /* S1300A reports L84F, but L1400B too, account for that */
1279         }
1280
1281         kfree(model);
1282
1283         return AE_OK;
1284 }
1285
1286 static int asus_hotk_check(void)
1287 {
1288         int result = 0;
1289
1290         result = acpi_bus_get_status(hotk->device);
1291         if (result)
1292                 return result;
1293
1294         if (hotk->device->status.present) {
1295                 result = asus_hotk_get_info();
1296         } else {
1297                 printk(KERN_ERR "  Hotkey device not present, aborting\n");
1298                 return -EINVAL;
1299         }
1300
1301         return result;
1302 }
1303
1304 static int asus_hotk_found;
1305
1306 static int asus_hotk_add(struct acpi_device *device)
1307 {
1308         acpi_status status = AE_OK;
1309         int result;
1310
1311         if (!device)
1312                 return -EINVAL;
1313
1314         printk(KERN_NOTICE "Asus Laptop ACPI Extras version %s\n",
1315                ASUS_ACPI_VERSION);
1316
1317         hotk = kzalloc(sizeof(struct asus_hotk), GFP_KERNEL);
1318         if (!hotk)
1319                 return -ENOMEM;
1320
1321         hotk->handle = device->handle;
1322         strcpy(acpi_device_name(device), ACPI_HOTK_DEVICE_NAME);
1323         strcpy(acpi_device_class(device), ACPI_HOTK_CLASS);
1324         acpi_driver_data(device) = hotk;
1325         hotk->device = device;
1326
1327         result = asus_hotk_check();
1328         if (result)
1329                 goto end;
1330
1331         result = asus_hotk_add_fs(device);
1332         if (result)
1333                 goto end;
1334
1335         /*
1336          * We install the handler, it will receive the hotk in parameter, so, we
1337          * could add other data to the hotk struct
1338          */
1339         status = acpi_install_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
1340                                              asus_hotk_notify, hotk);
1341         if (ACPI_FAILURE(status))
1342                 printk(KERN_ERR "  Error installing notify handler\n");
1343
1344         /* For laptops without GPLV: init the hotk->brightness value */
1345         if ((!hotk->methods->brightness_get)
1346             && (!hotk->methods->brightness_status)
1347             && (hotk->methods->brightness_up && hotk->methods->brightness_down)) {
1348                 status =
1349                     acpi_evaluate_object(NULL, hotk->methods->brightness_down,
1350                                          NULL, NULL);
1351                 if (ACPI_FAILURE(status))
1352                         printk(KERN_WARNING "  Error changing brightness\n");
1353                 else {
1354                         status =
1355                             acpi_evaluate_object(NULL,
1356                                                  hotk->methods->brightness_up,
1357                                                  NULL, NULL);
1358                         if (ACPI_FAILURE(status))
1359                                 printk(KERN_WARNING "  Strange, error changing"
1360                                        " brightness\n");
1361                 }
1362         }
1363
1364         asus_hotk_found = 1;
1365
1366         /* LED display is off by default */
1367         hotk->ledd_status = 0xFFF;
1368
1369       end:
1370         if (result) {
1371                 kfree(hotk);
1372         }
1373
1374         return result;
1375 }
1376
1377 static int asus_hotk_remove(struct acpi_device *device, int type)
1378 {
1379         acpi_status status = 0;
1380
1381         if (!device || !acpi_driver_data(device))
1382                 return -EINVAL;
1383
1384         status = acpi_remove_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
1385                                             asus_hotk_notify);
1386         if (ACPI_FAILURE(status))
1387                 printk(KERN_ERR "Asus ACPI: Error removing notify handler\n");
1388
1389         asus_hotk_remove_fs(device);
1390
1391         kfree(hotk);
1392
1393         return 0;
1394 }
1395
1396 static struct backlight_ops asus_backlight_data = {
1397         .get_brightness = read_brightness,
1398         .update_status  = set_brightness_status,
1399 };
1400
1401 static void asus_acpi_exit(void)
1402 {
1403         if (asus_backlight_device)
1404                 backlight_device_unregister(asus_backlight_device);
1405
1406         acpi_bus_unregister_driver(&asus_hotk_driver);
1407         remove_proc_entry(PROC_ASUS, acpi_root_dir);
1408
1409         return;
1410 }
1411
1412 static int __init asus_acpi_init(void)
1413 {
1414         int result;
1415
1416         if (acpi_disabled)
1417                 return -ENODEV;
1418
1419         asus_proc_dir = proc_mkdir(PROC_ASUS, acpi_root_dir);
1420         if (!asus_proc_dir) {
1421                 printk(KERN_ERR "Asus ACPI: Unable to create /proc entry\n");
1422                 return -ENODEV;
1423         }
1424         asus_proc_dir->owner = THIS_MODULE;
1425
1426         result = acpi_bus_register_driver(&asus_hotk_driver);
1427         if (result < 0) {
1428                 remove_proc_entry(PROC_ASUS, acpi_root_dir);
1429                 return result;
1430         }
1431
1432         /*
1433          * This is a bit of a kludge.  We only want this module loaded
1434          * for ASUS systems, but there's currently no way to probe the
1435          * ACPI namespace for ASUS HIDs.  So we just return failure if
1436          * we didn't find one, which will cause the module to be
1437          * unloaded.
1438          */
1439         if (!asus_hotk_found) {
1440                 acpi_bus_unregister_driver(&asus_hotk_driver);
1441                 remove_proc_entry(PROC_ASUS, acpi_root_dir);
1442                 return -ENODEV;
1443         }
1444
1445         asus_backlight_device = backlight_device_register("asus",NULL,NULL,
1446                                                           &asus_backlight_data);
1447         if (IS_ERR(asus_backlight_device)) {
1448                 printk(KERN_ERR "Could not register asus backlight device\n");
1449                 asus_backlight_device = NULL;
1450                 asus_acpi_exit();
1451                 return -ENODEV;
1452         }
1453         asus_backlight_device->props.max_brightness = 15;
1454
1455         return 0;
1456 }
1457
1458 module_init(asus_acpi_init);
1459 module_exit(asus_acpi_exit);