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