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