ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / acpi / asus_acpi.c
1 /*
2  *  asus_acpi.c - Asus Laptop ACPI Extras
3  *
4  *
5  *  Copyright (C) 2002, 2003, 2004 Julien Lerouge, 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  *
30  *  TODO:
31  *  add Fn key status
32  *  Add mode selection on module loading (parameter) -> still necessary?
33  *  Complete display switching -- may require dirty hacks or calling _DOS?
34  */
35
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/types.h>
40 #include <linux/proc_fs.h>
41 #include <acpi/acpi_drivers.h>
42 #include <acpi/acpi_bus.h>
43
44 #define ASUS_ACPI_VERSION "0.28"
45
46 #define PROC_ASUS       "asus"  //the directory
47 #define PROC_MLED       "mled"
48 #define PROC_WLED       "wled"
49 #define PROC_TLED       "tled"
50 #define PROC_INFO       "info"
51 #define PROC_LCD        "lcd"
52 #define PROC_BRN        "brn"
53 #define PROC_DISP       "disp"
54
55 #define ACPI_HOTK_NAME          "Asus Laptop ACPI Extras Driver"
56 #define ACPI_HOTK_CLASS         "hotkey"
57 #define ACPI_HOTK_DEVICE_NAME   "Hotkey"
58 #define ACPI_HOTK_HID           "ATK0100"
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        //is MLED ON ?
70 #define WLED_ON     0x02
71 #define TLED_ON     0x04
72
73 MODULE_AUTHOR("Julien Lerouge, Karol Kozimor");
74 MODULE_DESCRIPTION(ACPI_HOTK_NAME);
75 MODULE_LICENSE("GPL");
76
77
78 static uid_t asus_uid;
79 static gid_t asus_gid;
80 MODULE_PARM(asus_uid, "i");
81 MODULE_PARM_DESC(uid, "UID for entries in /proc/acpi/asus.\n");
82 MODULE_PARM(asus_gid, "i");
83 MODULE_PARM_DESC(gid, "GID for entries in /proc/acpi/asus.\n");
84
85
86 /* For each model, all features implemented, 
87  * those marked with R are relative to HOTK, A for absolute */
88 struct model_data {
89         char *name;              //name of the laptop________________A
90         char *mt_mled;           //method to handle mled_____________R
91         char *mled_status;       //node to handle mled reading_______A
92         char *mt_wled;           //method to handle wled_____________R
93         char *wled_status;       //node to handle wled reading_______A
94         char *mt_tled;           //method to handle tled_____________R
95         char *tled_status;       //node to handle tled reading_______A
96         char *mt_lcd_switch;     //method to turn LCD ON/OFF_________A
97         char *lcd_status;        //node to read LCD panel state______A
98         char *brightness_up;     //method to set brightness up_______A
99         char *brightness_down;   //guess what ?______________________A
100         char *brightness_set;    //method to set absolute brightness_R
101         char *brightness_get;    //method to get absolute brightness_R
102         char *brightness_status; //node to get brightness____________A
103         char *display_set;       //method to set video output________R
104         char *display_get;       //method to get video output________R
105 };
106
107 /*
108  * This is the main structure, we can use it to store anything interesting
109  * about the hotk device
110  */
111 struct asus_hotk {
112         struct acpi_device *device; //the device we are in
113         acpi_handle handle;         //the handle of the hotk device
114         char status;                //status of the hotk, for LEDs, ...
115         struct model_data *methods; //methods available on the laptop
116         u8 brightness;              //brightness level
117         enum {
118                 A1x = 0,  //A1340D, A1300F
119                 A2x,      //A2500H
120                 D1x,      //D1
121                 L2D,      //L2000D
122                 L3C,      //L3800C
123                 L3D,      //L3400D
124                 L3H,      //L3H, but also L2000E
125                 L5x,      //L5800C 
126                 L8L,      //L8400L
127                 M1A,      //M1300A
128                 M2E,      //M2400E, L4400L
129                 P30,      //Samsung P30
130                 S1x,      //S1300A, but also L1400B and M2400A (L84F)
131                 S2x,      //S200 (J1 reported), Victor MP-XP7210
132                 xxN,      //M2400N, M3700N, M6800N, S1300N, S5200N (Centrino)
133                 END_MODEL
134         } model;              //Models currently supported
135         u16 event_count[128]; //count for each event TODO make this better
136 };
137
138 /* Here we go */
139 #define A1x_PREFIX "\\_SB.PCI0.ISA.EC0."
140 #define L3C_PREFIX "\\_SB.PCI0.PX40.ECD0."
141 #define M1A_PREFIX "\\_SB.PCI0.PX40.EC0."
142 #define P30_PREFIX "\\_SB.PCI0.LPCB.EC0."
143 #define S1x_PREFIX "\\_SB.PCI0.PX40."
144 #define S2x_PREFIX A1x_PREFIX
145 #define xxN_PREFIX "\\_SB.PCI0.SBRG.EC0."
146
147 static struct model_data model_conf[END_MODEL] = {
148         /*
149          * Those pathnames are relative to the HOTK / ATKD device :
150          *       - mt_mled
151          *       - mt_wled
152          *       - brightness_set
153          *       - brightness_get
154          *       - display_set
155          *       - display_get
156          *
157          * TODO I have seen a SWBX and AIBX method on some models, like L1400B,
158          * it seems to be a kind of switch, but what for ?
159          *
160          */
161
162         {
163                 .name              = "A1x",
164                 .mt_mled           = "MLED",
165                 .mled_status       = "\\MAIL",
166                 .mt_lcd_switch     = A1x_PREFIX "_Q10",
167                 .lcd_status        = "\\BKLI",
168                 .brightness_up     = A1x_PREFIX "_Q0E",
169                 .brightness_down   = A1x_PREFIX "_Q0F"
170         },
171
172         {
173                 .name              = "A2x",
174                 .mt_mled           = "MLED",
175                 .mt_wled           = "WLED",
176                 .wled_status       = "\\SG66",
177                 .mt_lcd_switch     = "\\Q10",
178                 .lcd_status        = "\\BAOF",
179                 .brightness_set    = "SPLV",
180                 .brightness_get    = "GPLV",
181                 .display_set       = "SDSP",
182                 .display_get       = "\\INFB"
183         },
184
185         {
186                 .name              = "D1x",
187                 .mt_mled           = "MLED",
188                 .mt_lcd_switch     = "\\Q0D",
189                 .lcd_status        = "\\GP11",
190                 .brightness_up     = "\\Q0C",
191                 .brightness_down   = "\\Q0B",
192                 .brightness_status = "\\BLVL",
193                 .display_set       = "SDSP",
194                 .display_get       = "\\INFB"
195         },
196
197         {
198                 .name              = "L2D",
199                 .mt_mled           = "MLED",
200                 .mled_status       = "\\SGP6",
201                 .mt_wled           = "WLED",
202                 .wled_status       = "\\RCP3",
203                 .mt_lcd_switch     = "\\Q10",
204                 .lcd_status        = "\\SGP0",
205                 .brightness_up     = "\\Q0E",
206                 .brightness_down   = "\\Q0F",
207                 .display_set       = "SDSP",
208                 .display_get       = "\\INFB"
209         },
210
211         {
212                 .name              = "L3C",
213                 .mt_mled           = "MLED",
214                 .mt_wled           = "WLED",
215                 .mt_lcd_switch     = L3C_PREFIX "_Q10",
216                 .lcd_status        = "\\GL32",
217                 .brightness_set    = "SPLV",
218                 .brightness_get    = "GPLV",
219                 .display_set       = "SDSP",
220                 .display_get       = "\\_SB.PCI0.PCI1.VGAC.NMAP"
221         },
222
223         {
224                 .name              = "L3D",
225                 .mt_mled           = "MLED",
226                 .mled_status       = "\\MALD",
227                 .mt_wled           = "WLED",
228                 .mt_lcd_switch     = "\\Q10",
229                 .lcd_status        = "\\BKLG",
230                 .brightness_set    = "SPLV",
231                 .brightness_get    = "GPLV",
232                 .display_set       = "SDSP",
233                 .display_get       = "\\INFB"
234         },
235
236         {
237                 .name              = "L3H",
238                 .mt_mled           = "MLED",
239                 .mt_wled           = "WLED",
240                 .mt_lcd_switch     = "EHK",
241                 .lcd_status        = "\\_SB.PCI0.PM.PBC",
242                 .brightness_set    = "SPLV",
243                 .brightness_get    = "GPLV",
244                 .display_set       = "SDSP",
245                 .display_get       = "\\INFB"
246         },
247
248         {
249                 .name              = "L5x",
250                 .mt_mled           = "MLED",
251 /* WLED present, but not controlled by ACPI */
252                 .mt_tled           = "TLED",
253                 .mt_lcd_switch     = "\\Q0D",
254                 .lcd_status        = "\\BAOF",
255                 .brightness_set    = "SPLV",
256                 .brightness_get    = "GPLV",
257                 .display_set       = "SDSP",
258                 .display_get       = "\\INFB"
259         },
260
261         {
262                 .name              = "L8L"
263 /* No features, but at least support the hotkeys */
264         },
265
266         {
267                 .name              = "M1A",
268                 .mt_mled           = "MLED",
269                 .mt_lcd_switch     = M1A_PREFIX "Q10",
270                 .lcd_status        = "\\PNOF",
271                 .brightness_up     = M1A_PREFIX "Q0E",
272                 .brightness_down   = M1A_PREFIX "Q0F",
273                 .brightness_status = "\\BRIT",
274                 .display_set       = "SDSP",
275                 .display_get       = "\\INFB"
276         },
277
278         {
279                 .name              = "M2E",
280                 .mt_mled           = "MLED",
281                 .mt_wled           = "WLED",
282                 .mt_lcd_switch     = "\\Q10",
283                 .lcd_status        = "\\GP06",
284                 .brightness_set    = "SPLV",
285                 .brightness_get    = "GPLV",
286                 .display_set       = "SDSP",
287                 .display_get       = "\\INFB"
288         },
289
290         {
291                 .name              = "P30",
292                 .mt_wled           = "WLED",
293                 .mt_lcd_switch     = P30_PREFIX "_Q0E",
294                 .lcd_status        = "\\BKLT",
295                 .brightness_up     = P30_PREFIX "_Q68",
296                 .brightness_down   = P30_PREFIX "_Q69",
297                 .brightness_get    = "GPLV",
298                 .display_set       = "SDSP",
299                 .display_get       = "\\DNXT"
300         },
301
302         {
303                 .name              = "S1x",
304                 .mt_mled           = "MLED",
305                 .mled_status       = "\\EMLE",
306                 .mt_wled           = "WLED",
307                 .mt_lcd_switch     = S1x_PREFIX "Q10" ,
308                 .lcd_status        = "\\PNOF",
309                 .brightness_set    = "SPLV",
310                 .brightness_get    = "GPLV"
311         },
312
313         {
314                 .name              = "S2x",
315                 .mt_mled           = "MLED",
316                 .mled_status       = "\\MAIL",
317                 .mt_lcd_switch     = S2x_PREFIX "_Q10",
318                 .lcd_status        = "\\BKLI",
319                 .brightness_up     = S2x_PREFIX "_Q0B",
320                 .brightness_down   = S2x_PREFIX "_Q0A"
321         },
322
323         {
324                 .name              = "xxN",
325                 .mt_mled           = "MLED",
326 /* WLED present, but not controlled by ACPI */
327                 .mt_lcd_switch     = xxN_PREFIX "_Q10",
328                 .lcd_status        = "\\BKLT",
329                 .brightness_set    = "SPLV",
330                 .brightness_get    = "GPLV",
331                 .display_set       = "SDSP",
332                 .display_get       = "\\ADVG"
333         }
334 };
335
336 /* procdir we use */
337 static struct proc_dir_entry *asus_proc_dir;
338
339 /*
340  * This header is made available to allow proper configuration given model,
341  * revision number , ... this info cannot go in struct asus_hotk because it is
342  * available before the hotk
343  */
344 static struct acpi_table_header *asus_info;
345
346 /*
347  * The hotkey driver declaration
348  */
349 static int asus_hotk_add(struct acpi_device *device);
350 static int asus_hotk_remove(struct acpi_device *device, int type);
351 static struct acpi_driver asus_hotk_driver = {
352         .name =         ACPI_HOTK_NAME,
353         .class =        ACPI_HOTK_CLASS,
354         .ids =          ACPI_HOTK_HID,
355         .ops =          {
356                                 .add =          asus_hotk_add,
357                                 .remove =       asus_hotk_remove,
358                         },
359 };
360
361 /* 
362  * This function evaluates an ACPI method, given an int as parameter, the
363  * method is searched within the scope of the handle, can be NULL. The output
364  * of the method is written is output, which can also be NULL
365  *
366  * returns 1 if write is successful, 0 else. 
367  */
368 static int write_acpi_int(acpi_handle handle, const char *method, int val,
369                           struct acpi_buffer *output)
370 {
371         struct acpi_object_list params; //list of input parameters (an int here)
372         union acpi_object in_obj;       //the only param we use
373         acpi_status status;
374
375         params.count = 1;
376         params.pointer = &in_obj;
377         in_obj.type = ACPI_TYPE_INTEGER;
378         in_obj.integer.value = val;
379
380         status = acpi_evaluate_object(handle, (char *) method, &params, output);
381         return (status == AE_OK);
382 }
383
384
385 static int read_acpi_int(acpi_handle handle, const char *method, int *val)
386 {
387         struct acpi_buffer output;
388         union acpi_object out_obj;
389         acpi_status status;
390
391         output.length = sizeof(out_obj);
392         output.pointer = &out_obj;
393
394         status = acpi_evaluate_object(handle, (char *) method, NULL, &output);
395         *val = out_obj.integer.value;
396         return (status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER);
397 }
398
399 /*
400  * We write our info in page, we begin at offset off and cannot write more
401  * than count bytes. We set eof to 1 if we handle those 2 values. We return the
402  * number of bytes written in page
403  */
404 static int
405 proc_read_info(char *page, char **start, off_t off, int count, int *eof,
406                 void *data)
407 {
408         int len = 0;
409         int temp;
410         struct asus_hotk *hotk = (struct asus_hotk *) data;
411         char buf[16];           //enough for all info
412         /*
413          * We use the easy way, we don't care of off and count, so we don't set eof
414          * to 1
415          */
416
417         len += sprintf(page, ACPI_HOTK_NAME " " ASUS_ACPI_VERSION "\n");
418         len += sprintf(page + len, "Model reference    : %s\n", 
419                        hotk->methods->name);
420         /* 
421          * The SFUN method probably allows the original driver to get the list 
422          * of features supported by a given model. For now, 0x0100 or 0x0800 
423          * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
424          * The significance of others is yet to be found.
425          */
426         if (read_acpi_int(hotk->handle, "SFUN", &temp))
427                 len += sprintf(page + len, "SFUN value         : 0x%04x\n", temp);
428         /*
429          * Another value for userspace: the ASYM method returns 0x02 for
430          * battery low and 0x04 for battery critical, it's readings tend to be
431          * more accurate than those provided by _BST. 
432          * Note: since not all the laptops provide this method, errors are
433          * silently ignored.
434          */
435         if (read_acpi_int(hotk->handle, "ASYM", &temp))
436                 len += 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
460 /*
461  * /proc handlers
462  * We write our info in page, we begin at offset off and cannot write more
463  * than count bytes. We set eof to 1 if we handle those 2 values. We return the
464  * number of bytes written in page
465  */
466
467 /* Generic LED functions */
468 static int
469 read_led(struct asus_hotk *hotk, const char *ledname, int ledmask)
470 {
471         if (ledname) {
472                 int led_status;
473
474                 if (read_acpi_int(NULL, ledname, &led_status))
475                         return led_status;
476                 else
477                         printk(KERN_WARNING "Asus ACPI: Error reading LED "
478                                "status\n");
479         }
480         return (hotk->status & ledmask) ? 1 : 0;
481 }
482
483
484 /* FIXME: kill extraneous args so it can be called independently */
485 static int
486 write_led(const char *buffer, unsigned long count, struct asus_hotk *hotk, 
487           char *ledname, int ledmask, int invert)
488 {
489         int value;
490         int led_out = 0;
491
492         if (sscanf(buffer, "%i", &value) == 1)
493                 led_out = value ? 1 : 0;
494
495         hotk->status =
496             (led_out) ? (hotk->status | ledmask) : (hotk->status & ~ledmask);
497
498         if (invert) /* invert target value */
499                 led_out = !led_out & 0x1;
500
501         if (!write_acpi_int(hotk->handle, ledname, led_out, NULL))
502                 printk(KERN_WARNING "Asus ACPI: LED (%s) write failed\n", ledname);
503
504         return count;
505 }
506
507
508 /*
509  * Proc handlers for MLED
510  */
511 static int
512 proc_read_mled(char *page, char **start, off_t off, int count, int *eof,
513                void *data)
514 {
515         struct asus_hotk *hotk = (struct asus_hotk *) data;
516         return sprintf(page, "%d\n", read_led(hotk, hotk->methods->mled_status, MLED_ON));
517 }
518
519
520 static int
521 proc_write_mled(struct file *file, const char *buffer,
522                 unsigned long count, void *data)
523 {
524         struct asus_hotk *hotk = (struct asus_hotk *) data;
525         return write_led(buffer, count, hotk, hotk->methods->mt_mled, MLED_ON, 1);
526 }
527
528 /*
529  * Proc handlers for WLED
530  */
531 static int
532 proc_read_wled(char *page, char **start, off_t off, int count, int *eof,
533                void *data)
534 {
535         struct asus_hotk *hotk = (struct asus_hotk *) data;
536         return sprintf(page, "%d\n", read_led(hotk, hotk->methods->wled_status, WLED_ON));
537 }
538
539 static int
540 proc_write_wled(struct file *file, const char *buffer,
541                 unsigned long count, void *data)
542 {
543         struct asus_hotk *hotk = (struct asus_hotk *) data;
544         return write_led(buffer, count, hotk, hotk->methods->mt_wled, WLED_ON, 0);
545 }
546
547 /*
548  * Proc handlers for TLED
549  */
550 static int
551 proc_read_tled(char *page, char **start, off_t off, int count, int *eof,
552                void *data)
553 {
554         struct asus_hotk *hotk = (struct asus_hotk *) data;
555         return sprintf(page, "%d\n", read_led(hotk, hotk->methods->tled_status, TLED_ON));
556 }
557
558 static int
559 proc_write_tled(struct file *file, const char *buffer,
560                 unsigned long count, void *data)
561 {
562         struct asus_hotk *hotk = (struct asus_hotk *) data;
563         return write_led(buffer, count, hotk, hotk->methods->mt_tled, TLED_ON, 0);
564 }
565
566
567
568 static int get_lcd_state(struct asus_hotk *hotk)
569 {
570         int lcd = 0;
571
572         if (hotk->model != L3H) {
573         /* We don't have to check anything if we are here */
574                 if (!read_acpi_int(NULL, hotk->methods->lcd_status, &lcd))
575                         printk(KERN_WARNING "Asus ACPI: Error reading LCD status\n");
576         
577                 if (hotk->model == L2D)
578                         lcd = ~lcd;
579         } else { /* L3H and the like have to be handled differently */
580                 acpi_status status = 0;
581                 struct acpi_object_list input;
582                 union acpi_object mt_params[2];
583                 struct acpi_buffer output;
584                 union acpi_object out_obj;
585                 
586                 input.count = 2;
587                 input.pointer = mt_params;
588                 /* Note: the following values are partly guessed up, but 
589                    otherwise they seem to work */
590                 mt_params[0].type = ACPI_TYPE_INTEGER;
591                 mt_params[0].integer.value = 0x02;
592                 mt_params[1].type = ACPI_TYPE_INTEGER;
593                 mt_params[1].integer.value = 0x02;
594
595                 output.length = sizeof(out_obj);
596                 output.pointer = &out_obj;
597                 
598                 status = acpi_evaluate_object(NULL, hotk->methods->lcd_status, &input, &output);
599                 if (status != AE_OK)
600                         return -1;
601                 if (out_obj.type == ACPI_TYPE_INTEGER)
602                         /* That's what the AML code does */
603                         lcd = out_obj.integer.value >> 8;
604         }
605         
606         return (lcd & 1);
607 }
608
609 static int set_lcd_state(struct asus_hotk *hotk, int value)
610 {
611         int lcd = 0;
612         acpi_status status = 0;
613
614         lcd = value ? 1 : 0;
615         if (lcd != get_lcd_state(hotk)) {
616                 /* switch */
617                 if (hotk->model != L3H) {
618                         status =
619                             acpi_evaluate_object(NULL, hotk->methods->mt_lcd_switch,
620                                                  NULL, NULL);
621                 } else { /* L3H and the like have to be handled differently */
622                         if (!write_acpi_int(hotk->handle, hotk->methods->mt_lcd_switch, 0x07, NULL))
623                                 status = AE_ERROR;
624                         /* L3H's AML executes EHK (0x07) upon Fn+F7 keypress, 
625                            the exact behaviour is simulated here */
626                 }
627                 if (ACPI_FAILURE(status))
628                         printk(KERN_WARNING "Asus ACPI: Error switching LCD\n");
629         }
630         return 0;
631
632 }
633
634 static int
635 proc_read_lcd(char *page, char **start, off_t off, int count, int *eof,
636               void *data)
637 {
638         return sprintf(page, "%d\n", get_lcd_state((struct asus_hotk *) data));
639 }
640
641
642 static int
643 proc_write_lcd(struct file *file, const char *buffer,
644                unsigned long count, void *data)
645 {
646         int value;
647         struct asus_hotk *hotk = (struct asus_hotk *) data;
648         
649         if (sscanf(buffer, "%i", &value) == 1)
650                 set_lcd_state(hotk, value);
651         return count;
652 }
653
654
655 static int read_brightness(struct asus_hotk *hotk)
656 {
657         int value;
658         
659         if(hotk->methods->brightness_get) { /* SPLV/GPLV laptop */
660                 if (!read_acpi_int(hotk->handle, hotk->methods->brightness_get, 
661                                    &value))
662                         printk(KERN_WARNING "Asus ACPI: Error reading brightness\n");
663         } else if (hotk->methods->brightness_status) { /* For D1 for example */
664                 if (!read_acpi_int(NULL, hotk->methods->brightness_status, 
665                                    &value))
666                         printk(KERN_WARNING "Asus ACPI: Error reading brightness\n");
667         } else /* No GPLV method */
668                 value = hotk->brightness;
669         return value;
670 }
671
672 /*
673  * Change the brightness level
674  */
675 static void set_brightness(int value, struct asus_hotk *hotk)
676 {
677         acpi_status status = 0;
678
679         /* SPLV laptop */
680         if(hotk->methods->brightness_set) {
681                 if (!write_acpi_int(hotk->handle, hotk->methods->brightness_set, 
682                                     value, NULL))
683                         printk(KERN_WARNING "Asus ACPI: Error changing brightness\n");
684                 return;
685         }
686
687         /* No SPLV method if we are here, act as appropriate */
688         value -= read_brightness(hotk);
689         while (value != 0) {
690                 status = acpi_evaluate_object(NULL, (value > 0) ? 
691                                               hotk->methods->brightness_up : 
692                                               hotk->methods->brightness_down,
693                                               NULL, NULL);
694                 (value > 0) ? value-- : value++;
695                 if (ACPI_FAILURE(status))
696                         printk(KERN_WARNING "Asus ACPI: Error changing brightness\n");
697         }
698         return;
699 }
700
701 static int
702 proc_read_brn(char *page, char **start, off_t off, int count, int *eof,
703               void *data)
704 {
705         struct asus_hotk *hotk = (struct asus_hotk *) data;
706         return sprintf(page, "%d\n", read_brightness(hotk));
707 }
708
709 static int
710 proc_write_brn(struct file *file, const char *buffer,
711                unsigned long count, void *data)
712 {
713         int value;
714         struct asus_hotk *hotk = (struct asus_hotk *) data;
715
716         if (sscanf(buffer, "%d", &value) == 1) {
717                 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
718                         /* 0 <= value <= 15 */
719                 set_brightness(value, hotk);
720         } else {
721                 printk(KERN_WARNING "Asus ACPI: Error reading user input\n");
722         }
723
724         return count;
725 }
726
727 static void set_display(int value, struct asus_hotk *hotk)
728 {
729         /* no sanity check needed for now */
730         if (!write_acpi_int(hotk->handle, hotk->methods->display_set, 
731                             value, NULL))
732                 printk(KERN_WARNING "Asus ACPI: Error setting display\n");
733         return;
734 }
735
736 /*
737  * Now, *this* one could be more user-friendly, but so far, no-one has 
738  * complained. The significance of bits is the same as in proc_write_disp()
739  */
740 static int
741 proc_read_disp(char *page, char **start, off_t off, int count, int *eof,
742               void *data)
743 {
744         int value = 0;
745         struct asus_hotk *hotk = (struct asus_hotk *) data;
746         
747         if (!read_acpi_int(hotk->handle, hotk->methods->display_get, &value))
748                 printk(KERN_WARNING "Asus ACPI: Error reading display status\n");
749         return sprintf(page, "%d\n", value);
750 }
751
752 /*
753  * Experimental support for display switching. As of now: 1 should activate 
754  * the LCD output, 2 should do for CRT, and 4 for TV-Out. Any combination 
755  * (bitwise) of these will suffice. I never actually tested 3 displays hooked up 
756  * simultaneously, so be warned. See the acpi4asus README for more info.
757  */
758 static int
759 proc_write_disp(struct file *file, const char *buffer,
760                unsigned long count, void *data)
761 {
762         int value;
763         struct asus_hotk *hotk = (struct asus_hotk *) data;
764
765         if (sscanf(buffer, "%d", &value) == 1)
766                 set_display(value, hotk);
767         else {
768                 printk(KERN_WARNING "Asus ACPI: Error reading user input\n");
769         }
770
771         return count;
772 }
773
774
775 typedef int (proc_readfunc)(char *page, char **start, off_t off, int count,
776                              int *eof, void *data);
777 typedef int (proc_writefunc)(struct file *file, const char *buffer,
778                               unsigned long count, void *data);
779
780 static int
781 __init asus_proc_add(char *name, proc_writefunc *writefunc,
782                      proc_readfunc *readfunc, mode_t mode,
783                      struct acpi_device *device)
784 {
785         struct proc_dir_entry *proc = create_proc_entry(name, mode, acpi_device_dir(device));
786         if(!proc) {
787                 printk(KERN_WARNING "  Unable to create %s fs entry\n", name);
788                 return -1;
789         }
790         proc->write_proc = writefunc;
791         proc->read_proc = readfunc;
792         proc->data = acpi_driver_data(device);
793         proc->owner = THIS_MODULE;
794         proc->uid = asus_uid;
795         proc->gid = asus_gid;
796         return 0;
797 }
798
799 static int __init asus_hotk_add_fs(struct acpi_device *device)
800 {
801         struct proc_dir_entry *proc;
802         struct asus_hotk *hotk = acpi_driver_data(device);
803         mode_t mode;
804         
805         /*
806          * If parameter uid or gid is not changed, keep the default setting for
807          * our proc entries (-rw-rw-rw-) else, it means we care about security,
808          * and then set to -rw-rw----
809          */
810
811         if ((asus_uid == 0) && (asus_gid == 0)){
812                 mode = S_IFREG | S_IRUGO | S_IWUGO;
813         } else {
814                 mode = S_IFREG | S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP;
815         }
816
817         acpi_device_dir(device) = asus_proc_dir;
818         if (!acpi_device_dir(device))
819                 return(-ENODEV);
820
821         proc = create_proc_entry(PROC_INFO, mode, acpi_device_dir(device));
822         if (proc) {
823                 proc->read_proc = proc_read_info;
824                 proc->data = acpi_driver_data(device);
825                 proc->owner = THIS_MODULE;
826                 proc->uid = asus_uid;
827                 proc->gid = asus_gid;
828         } else {
829                 printk(KERN_WARNING "  Unable to create " PROC_INFO
830                        " fs entry\n");
831         }
832
833         if (hotk->methods->mt_wled) {
834                 asus_proc_add(PROC_WLED, &proc_write_wled, &proc_read_wled, mode, device);
835         }
836
837         if (hotk->methods->mt_mled) {
838                 asus_proc_add(PROC_MLED, &proc_write_mled, &proc_read_mled, mode, device);
839         }
840
841         if (hotk->methods->mt_tled) {
842                 asus_proc_add(PROC_TLED, &proc_write_tled, &proc_read_tled, mode, device);
843         }
844
845         /* 
846          * We need both read node and write method as LCD switch is also accessible
847          * from keyboard 
848          */
849         if (hotk->methods->mt_lcd_switch && hotk->methods->lcd_status) {
850                 asus_proc_add(PROC_LCD, &proc_write_lcd, &proc_read_lcd, mode, device);
851         }
852         
853         if ((hotk->methods->brightness_up && hotk->methods->brightness_down) ||
854             (hotk->methods->brightness_get && hotk->methods->brightness_get)) {
855                 asus_proc_add(PROC_BRN, &proc_write_brn, &proc_read_brn, mode, device);
856         }
857
858         if (hotk->methods->display_set) {
859                 asus_proc_add(PROC_DISP, &proc_write_disp, &proc_read_disp, mode, device);
860
861         }
862
863         return 0;
864 }
865
866
867 static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
868 {
869         /* TODO Find a better way to handle events count. Here, in data, we receive
870          * the hotk, so we can do anything!
871          */
872         struct asus_hotk *hotk = (struct asus_hotk *) data;
873
874         if (!hotk)
875                 return;
876
877         if ((event & ~((u32) BR_UP)) < 16) {
878                 hotk->brightness = (event & ~((u32) BR_UP));
879         } else if ((event & ~((u32) BR_DOWN)) < 16 ) {
880                 hotk->brightness = (event & ~((u32) BR_DOWN));
881         }
882
883         acpi_bus_generate_event(hotk->device, event,
884                                 hotk->event_count[event % 128]++);
885
886         return;
887 }
888
889 /*
890  * This function is used to initialize the hotk with right values. In this
891  * method, we can make all the detection we want, and modify the hotk struct
892  */
893 static int __init asus_hotk_get_info(struct asus_hotk *hotk)
894 {
895         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
896         struct acpi_buffer dsdt = { ACPI_ALLOCATE_BUFFER, NULL };
897         union acpi_object *model = NULL;
898         int bsts_result;
899         acpi_status status;
900
901         /*
902          * Get DSDT headers early enough to allow for differentiating between 
903          * models, but late enough to allow acpi_bus_register_driver() to fail 
904          * before doing anything ACPI-specific. Should we encounter a machine,
905          * which needs special handling (i.e. its hotkey device has a different
906          * HID), this bit will be moved. A global variable asus_info contains
907          * the DSDT header.
908          */
909         status = acpi_get_table(ACPI_TABLE_DSDT, 1, &dsdt);
910         if (ACPI_FAILURE(status))
911                 printk(KERN_WARNING "  Couldn't get the DSDT table header\n");
912         else
913                 asus_info = (struct acpi_table_header *) dsdt.pointer;
914
915         /* We have to write 0 on init this far for all ASUS models */
916         if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
917                 printk(KERN_ERR "  Hotkey initialization failed\n");
918                 return -ENODEV;
919         }
920
921         /* This needs to be called for some laptops to init properly */
922         if (!read_acpi_int(hotk->handle, "BSTS", &bsts_result))
923                 printk(KERN_WARNING "  Error calling BSTS\n");
924         else if (bsts_result)
925                 printk(KERN_NOTICE "  BSTS called, 0x%02x returned\n", bsts_result);
926
927         /* Samsung P30 has a device with a valid _HID whose INIT does not 
928          * return anything. Catch this one and any similar here */
929         if (buffer.pointer == NULL) {
930                 if (asus_info && /* Samsung P30 */
931                     strncmp(asus_info->oem_table_id, "ODEM", 4) == 0) {
932                         hotk->model = P30;
933                         printk(KERN_NOTICE "  Samsung P30 detected, supported\n");
934                 } else {
935                         hotk->model = M2E;
936                         printk(KERN_WARNING "  no string returned by INIT\n");
937                         printk(KERN_WARNING "  trying default values, supply "
938                                "the developers with your DSDT\n");
939                 }
940                 hotk->methods = &model_conf[hotk->model];
941                 return AE_OK;
942         }
943         
944         model = (union acpi_object *) buffer.pointer;
945         if (model->type == ACPI_TYPE_STRING) {
946                 printk(KERN_NOTICE "  %s model detected, ", model->string.pointer);
947         }
948
949         hotk->model = END_MODEL;
950         if (strncmp(model->string.pointer, "L3D", 3) == 0)
951                 hotk->model = L3D;
952         else if (strncmp(model->string.pointer, "L3H", 3) == 0 ||
953                  strncmp(model->string.pointer, "L2E", 3) == 0)
954                 hotk->model = L3H;
955         else if (strncmp(model->string.pointer, "L3", 2) == 0 ||
956                  strncmp(model->string.pointer, "L2B", 3) == 0)
957                 hotk->model = L3C;
958         else if (strncmp(model->string.pointer, "L8L", 3) == 0)
959                 hotk->model = L8L;
960         else if (strncmp(model->string.pointer, "M2N", 3) == 0 ||
961                  strncmp(model->string.pointer, "M3N", 3) == 0 ||
962                  strncmp(model->string.pointer, "M6N", 3) == 0 ||
963                  strncmp(model->string.pointer, "S1N", 3) == 0 ||
964                  strncmp(model->string.pointer, "S5N", 3) == 0)
965                 hotk->model = xxN;
966         else if (strncmp(model->string.pointer, "M1", 2) == 0)
967                 hotk->model = M1A;
968         else if (strncmp(model->string.pointer, "M2", 2) == 0 ||
969                  strncmp(model->string.pointer, "L4E", 3) == 0)
970                 hotk->model = M2E;
971         else if (strncmp(model->string.pointer, "L2", 2) == 0)
972                 hotk->model = L2D;
973         else if (strncmp(model->string.pointer, "L8", 2) == 0)
974                 hotk->model = S1x;
975         else if (strncmp(model->string.pointer, "D1", 2) == 0)
976                 hotk->model = D1x;
977         else if (strncmp(model->string.pointer, "A1", 2) == 0)
978                 hotk->model = A1x;
979         else if (strncmp(model->string.pointer, "A2", 2) == 0)
980                 hotk->model = A2x;
981         else if (strncmp(model->string.pointer, "J1", 2) == 0)
982                 hotk->model = S2x;
983         else if (strncmp(model->string.pointer, "L5", 2) == 0)
984                 hotk->model = L5x;
985
986         if (hotk->model == END_MODEL) {
987                 /* By default use the same values, as I don't know others */
988                 printk("unsupported, trying default values, supply the "
989                        "developers with your DSDT\n");
990                 hotk->model = M2E;
991         } else {
992                 printk("supported\n");
993         }
994
995         hotk->methods = &model_conf[hotk->model];
996
997         /* Sort of per-model blacklist */
998         if (strncmp(model->string.pointer, "L2B", 3) == 0)
999                 hotk->methods->lcd_status = NULL; 
1000         /* L2B is similar enough to L3C to use its settings, with this only 
1001            exception */
1002         else if (strncmp(model->string.pointer, "S5N", 3) == 0)
1003                 hotk->methods->mt_mled = NULL; 
1004         /* S5N has no MLED */
1005         else if (strncmp(model->string.pointer, "M6N", 3) == 0) {
1006                 hotk->methods->display_get = NULL; //TODO
1007                 hotk->methods->lcd_status = "\\_SB.BKLT";
1008                 hotk->methods->mt_wled = "WLED";
1009                 hotk->methods->wled_status = "\\_SB.PCI0.SBRG.SG13";
1010         /* M6N differs slightly and has a usable WLED */
1011         }
1012         else if (asus_info) {
1013                 if (strncmp(asus_info->oem_table_id, "L1", 2) == 0)
1014                         hotk->methods->mled_status = NULL;
1015         /* S1300A reports L84F, but L1400B too, account for that */
1016         }
1017
1018         acpi_os_free(model);
1019
1020         return AE_OK;
1021 }
1022
1023
1024
1025 static int __init asus_hotk_check(struct asus_hotk *hotk)
1026 {
1027         int result = 0;
1028
1029         if (!hotk)
1030                 return(-EINVAL);
1031
1032         result = acpi_bus_get_status(hotk->device);
1033         if (result)
1034                 return(result);
1035
1036         if (hotk->device->status.present) {
1037                 result = asus_hotk_get_info(hotk);
1038         } else {
1039                 printk(KERN_ERR "  Hotkey device not present, aborting\n");
1040                 return(-EINVAL);
1041         }
1042
1043         return(result);
1044 }
1045
1046
1047
1048 static int __init asus_hotk_add(struct acpi_device *device)
1049 {
1050         struct asus_hotk *hotk = NULL;
1051         acpi_status status = AE_OK;
1052         int result;
1053
1054         if (!device)
1055                 return(-EINVAL);
1056
1057         printk(KERN_NOTICE "Asus Laptop ACPI Extras version %s\n",
1058                ASUS_ACPI_VERSION);
1059
1060         hotk =
1061             (struct asus_hotk *) kmalloc(sizeof(struct asus_hotk), GFP_KERNEL);
1062         if (!hotk)
1063                 return(-ENOMEM);
1064         memset(hotk, 0, sizeof(struct asus_hotk));
1065
1066         hotk->handle = device->handle;
1067         strcpy(acpi_device_name(device), ACPI_HOTK_DEVICE_NAME);
1068         strcpy(acpi_device_class(device), ACPI_HOTK_CLASS);
1069         acpi_driver_data(device) = hotk;
1070         hotk->device = device;
1071
1072
1073         result = asus_hotk_check(hotk);
1074         if (result)
1075                 goto end;
1076
1077         result = asus_hotk_add_fs(device);
1078         if (result)
1079                 goto end;
1080
1081         /*
1082          * We install the handler, it will receive the hotk in parameter, so, we
1083          * could add other data to the hotk struct
1084          */
1085         status = acpi_install_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
1086                                              asus_hotk_notify, hotk);
1087         if (ACPI_FAILURE(status))
1088                 printk(KERN_ERR "  Error installing notify handler\n");
1089
1090         /* For laptops without GPLV: init the hotk->brightness value */
1091         if ((!hotk->methods->brightness_get) && (!hotk->methods->brightness_status) &&
1092             (hotk->methods->brightness_up && hotk->methods->brightness_down)) {
1093                 status = acpi_evaluate_object(NULL, hotk->methods->brightness_down,
1094                                               NULL, NULL);
1095                 if (ACPI_FAILURE(status))
1096                         printk(KERN_WARNING "  Error changing brightness\n");
1097                 else {
1098                         status = acpi_evaluate_object(NULL, hotk->methods->brightness_up,
1099                                                       NULL, NULL);
1100                         if (ACPI_FAILURE(status))
1101                                 printk(KERN_WARNING "  Strange, error changing" 
1102                                        " brightness\n");
1103                 }
1104         }
1105
1106       end:
1107         if (result) {
1108                 kfree(hotk);
1109         }
1110
1111         return(result);
1112 }
1113
1114
1115 static int asus_hotk_remove(struct acpi_device *device, int type)
1116 {
1117         acpi_status status = 0;
1118         struct asus_hotk *hotk = NULL;
1119
1120         if (!device || !acpi_driver_data(device))
1121                 return(-EINVAL);
1122
1123         hotk = (struct asus_hotk *) acpi_driver_data(device);
1124
1125         status = acpi_remove_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
1126                                             asus_hotk_notify);
1127         if (ACPI_FAILURE(status))
1128                 printk(KERN_ERR "Asus ACPI: Error removing notify handler\n");
1129
1130         kfree(hotk);
1131
1132         return(0);
1133 }
1134
1135
1136 static int __init asus_acpi_init(void)
1137 {
1138         int result;
1139
1140         asus_proc_dir = proc_mkdir(PROC_ASUS, acpi_root_dir);
1141         if (!asus_proc_dir) {
1142                 printk(KERN_ERR "Asus ACPI: Unable to create /proc entry\n");
1143                 return(-ENODEV);
1144         }
1145         asus_proc_dir->owner = THIS_MODULE;
1146
1147         result = acpi_bus_register_driver(&asus_hotk_driver);
1148         if (result < 0) {
1149                 remove_proc_entry(PROC_ASUS, acpi_root_dir);
1150                 return(-ENODEV);
1151         }
1152
1153         return(0);
1154 }
1155
1156
1157 static void __exit asus_acpi_exit(void)
1158 {
1159         acpi_bus_unregister_driver(&asus_hotk_driver);
1160         remove_proc_entry(PROC_ASUS, acpi_root_dir);
1161
1162         acpi_os_free(asus_info);
1163
1164         return;
1165 }
1166
1167 module_init(asus_acpi_init);
1168 module_exit(asus_acpi_exit);