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