ACPI: asus_acpi: handle internal Bluetooth / support W5A
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / acpi / asus_acpi.c
blobf39aef10973652d49fa964f12a9d8a7589f256bb
1 /*
2 * asus_acpi.c - Asus Laptop ACPI Extras
5 * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
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.
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.
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
22 * The development page for this driver is located at
23 * http://sourceforge.net/projects/acpi4asus/
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 * Éric Burghard - LED display support for W1N
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/types.h>
37 #include <linux/proc_fs.h>
38 #include <acpi/acpi_drivers.h>
39 #include <acpi/acpi_bus.h>
40 #include <asm/uaccess.h>
42 #define ASUS_ACPI_VERSION "0.30"
44 #define PROC_ASUS "asus" //the directory
45 #define PROC_MLED "mled"
46 #define PROC_WLED "wled"
47 #define PROC_TLED "tled"
48 #define PROC_BT "bluetooth"
49 #define PROC_LEDD "ledd"
50 #define PROC_INFO "info"
51 #define PROC_LCD "lcd"
52 #define PROC_BRN "brn"
53 #define PROC_DISP "disp"
55 #define ACPI_HOTK_NAME "Asus Laptop ACPI Extras Driver"
56 #define ACPI_HOTK_CLASS "hotkey"
57 #define ACPI_HOTK_DEVICE_NAME "Hotkey"
58 #define ACPI_HOTK_HID "ATK0100"
61 * Some events we use, same for all Asus
63 #define BR_UP 0x10
64 #define BR_DOWN 0x20
67 * Flags for hotk status
69 #define MLED_ON 0x01 //mail LED
70 #define WLED_ON 0x02 //wireless LED
71 #define TLED_ON 0x04 //touchpad LED
72 #define BT_ON 0x08 //internal Bluetooth
74 MODULE_AUTHOR("Julien Lerouge, Karol Kozimor");
75 MODULE_DESCRIPTION(ACPI_HOTK_NAME);
76 MODULE_LICENSE("GPL");
78 static uid_t asus_uid;
79 static gid_t asus_gid;
80 module_param(asus_uid, uint, 0);
81 MODULE_PARM_DESC(asus_uid, "UID for entries in /proc/acpi/asus.\n");
82 module_param(asus_gid, uint, 0);
83 MODULE_PARM_DESC(asus_gid, "GID for entries in /proc/acpi/asus.\n");
85 /* For each model, all features implemented,
86 * those marked with R are relative to HOTK, A for absolute */
87 struct model_data {
88 char *name; //name of the laptop________________A
89 char *mt_mled; //method to handle mled_____________R
90 char *mled_status; //node to handle mled reading_______A
91 char *mt_wled; //method to handle wled_____________R
92 char *wled_status; //node to handle wled reading_______A
93 char *mt_tled; //method to handle tled_____________R
94 char *tled_status; //node to handle tled reading_______A
95 char *mt_ledd; //method to handle LED display______R
96 char *mt_bt_switch; //method to switch Bluetooth on/off_R
97 char *bt_status; //no model currently supports this__?
98 char *mt_lcd_switch; //method to turn LCD on/off_________A
99 char *lcd_status; //node to read LCD panel state______A
100 char *brightness_up; //method to set brightness up_______A
101 char *brightness_down; //guess what ?______________________A
102 char *brightness_set; //method to set absolute brightness_R
103 char *brightness_get; //method to get absolute brightness_R
104 char *brightness_status; //node to get brightness____________A
105 char *display_set; //method to set video output________R
106 char *display_get; //method to get video output________R
110 * This is the main structure, we can use it to store anything interesting
111 * about the hotk device
113 struct asus_hotk {
114 struct acpi_device *device; //the device we are in
115 acpi_handle handle; //the handle of the hotk device
116 char status; //status of the hotk, for LEDs, ...
117 u32 ledd_status; //status of the LED display
118 struct model_data *methods; //methods available on the laptop
119 u8 brightness; //brightness level
120 enum {
121 A1x = 0, //A1340D, A1300F
122 A2x, //A2500H
123 A4G, //A4700G
124 D1x, //D1
125 L2D, //L2000D
126 L3C, //L3800C
127 L3D, //L3400D
128 L3H, //L3H, but also L2000E
129 L4R, //L4500R
130 L5x, //L5800C
131 L8L, //L8400L
132 M1A, //M1300A
133 M2E, //M2400E, L4400L
134 M6N, //M6800N, W3400N
135 M6R, //M6700R, A3000G
136 P30, //Samsung P30
137 S1x, //S1300A, but also L1400B and M2400A (L84F)
138 S2x, //S200 (J1 reported), Victor MP-XP7210
139 W1N, //W1000N
140 W5A, //W5A
141 xxN, //M2400N, M3700N, M5200N, M6800N, S1300N, S5200N
142 //(Centrino)
143 END_MODEL
144 } model; //Models currently supported
145 u16 event_count[128]; //count for each event TODO make this better
148 /* Here we go */
149 #define A1x_PREFIX "\\_SB.PCI0.ISA.EC0."
150 #define L3C_PREFIX "\\_SB.PCI0.PX40.ECD0."
151 #define M1A_PREFIX "\\_SB.PCI0.PX40.EC0."
152 #define P30_PREFIX "\\_SB.PCI0.LPCB.EC0."
153 #define S1x_PREFIX "\\_SB.PCI0.PX40."
154 #define S2x_PREFIX A1x_PREFIX
155 #define xxN_PREFIX "\\_SB.PCI0.SBRG.EC0."
157 static struct model_data model_conf[END_MODEL] = {
159 * TODO I have seen a SWBX and AIBX method on some models, like L1400B,
160 * it seems to be a kind of switch, but what for ?
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"},
173 .name = "A2x",
174 .mt_mled = "MLED",
175 .mt_wled = "WLED",
176 .wled_status = "\\SG66",
177 .mt_lcd_switch = "\\Q10",
178 .lcd_status = "\\BAOF",
179 .brightness_set = "SPLV",
180 .brightness_get = "GPLV",
181 .display_set = "SDSP",
182 .display_get = "\\INFB"},
185 .name = "A4G",
186 .mt_mled = "MLED",
187 /* WLED present, but not controlled by ACPI */
188 .mt_lcd_switch = xxN_PREFIX "_Q10",
189 .brightness_set = "SPLV",
190 .brightness_get = "GPLV",
191 .display_set = "SDSP",
192 .display_get = "\\ADVG"},
195 .name = "D1x",
196 .mt_mled = "MLED",
197 .mt_lcd_switch = "\\Q0D",
198 .lcd_status = "\\GP11",
199 .brightness_up = "\\Q0C",
200 .brightness_down = "\\Q0B",
201 .brightness_status = "\\BLVL",
202 .display_set = "SDSP",
203 .display_get = "\\INFB"},
206 .name = "L2D",
207 .mt_mled = "MLED",
208 .mled_status = "\\SGP6",
209 .mt_wled = "WLED",
210 .wled_status = "\\RCP3",
211 .mt_lcd_switch = "\\Q10",
212 .lcd_status = "\\SGP0",
213 .brightness_up = "\\Q0E",
214 .brightness_down = "\\Q0F",
215 .display_set = "SDSP",
216 .display_get = "\\INFB"},
219 .name = "L3C",
220 .mt_mled = "MLED",
221 .mt_wled = "WLED",
222 .mt_lcd_switch = L3C_PREFIX "_Q10",
223 .lcd_status = "\\GL32",
224 .brightness_set = "SPLV",
225 .brightness_get = "GPLV",
226 .display_set = "SDSP",
227 .display_get = "\\_SB.PCI0.PCI1.VGAC.NMAP"},
230 .name = "L3D",
231 .mt_mled = "MLED",
232 .mled_status = "\\MALD",
233 .mt_wled = "WLED",
234 .mt_lcd_switch = "\\Q10",
235 .lcd_status = "\\BKLG",
236 .brightness_set = "SPLV",
237 .brightness_get = "GPLV",
238 .display_set = "SDSP",
239 .display_get = "\\INFB"},
242 .name = "L3H",
243 .mt_mled = "MLED",
244 .mt_wled = "WLED",
245 .mt_lcd_switch = "EHK",
246 .lcd_status = "\\_SB.PCI0.PM.PBC",
247 .brightness_set = "SPLV",
248 .brightness_get = "GPLV",
249 .display_set = "SDSP",
250 .display_get = "\\INFB"},
253 .name = "L4R",
254 .mt_mled = "MLED",
255 .mt_wled = "WLED",
256 .wled_status = "\\_SB.PCI0.SBRG.SG13",
257 .mt_lcd_switch = xxN_PREFIX "_Q10",
258 .lcd_status = "\\_SB.PCI0.SBSM.SEO4",
259 .brightness_set = "SPLV",
260 .brightness_get = "GPLV",
261 .display_set = "SDSP",
262 .display_get = "\\_SB.PCI0.P0P1.VGA.GETD"},
265 .name = "L5x",
266 .mt_mled = "MLED",
267 /* WLED present, but not controlled by ACPI */
268 .mt_tled = "TLED",
269 .mt_lcd_switch = "\\Q0D",
270 .lcd_status = "\\BAOF",
271 .brightness_set = "SPLV",
272 .brightness_get = "GPLV",
273 .display_set = "SDSP",
274 .display_get = "\\INFB"},
277 .name = "L8L"
278 /* No features, but at least support the hotkeys */
282 .name = "M1A",
283 .mt_mled = "MLED",
284 .mt_lcd_switch = M1A_PREFIX "Q10",
285 .lcd_status = "\\PNOF",
286 .brightness_up = M1A_PREFIX "Q0E",
287 .brightness_down = M1A_PREFIX "Q0F",
288 .brightness_status = "\\BRIT",
289 .display_set = "SDSP",
290 .display_get = "\\INFB"},
293 .name = "M2E",
294 .mt_mled = "MLED",
295 .mt_wled = "WLED",
296 .mt_lcd_switch = "\\Q10",
297 .lcd_status = "\\GP06",
298 .brightness_set = "SPLV",
299 .brightness_get = "GPLV",
300 .display_set = "SDSP",
301 .display_get = "\\INFB"},
304 .name = "M6N",
305 .mt_mled = "MLED",
306 .mt_wled = "WLED",
307 .wled_status = "\\_SB.PCI0.SBRG.SG13",
308 .mt_lcd_switch = xxN_PREFIX "_Q10",
309 .lcd_status = "\\_SB.BKLT",
310 .brightness_set = "SPLV",
311 .brightness_get = "GPLV",
312 .display_set = "SDSP",
313 .display_get = "\\_SB.PCI0.P0P1.VGA.GETD"},
315 .name = "M6R",
316 .mt_mled = "MLED",
317 .mt_wled = "WLED",
318 .mt_lcd_switch = xxN_PREFIX "_Q10",
319 .lcd_status = "\\_SB.PCI0.SBSM.SEO4",
320 .brightness_set = "SPLV",
321 .brightness_get = "GPLV",
322 .display_set = "SDSP",
323 .display_get = "\\SSTE"},
326 .name = "P30",
327 .mt_wled = "WLED",
328 .mt_lcd_switch = P30_PREFIX "_Q0E",
329 .lcd_status = "\\BKLT",
330 .brightness_up = P30_PREFIX "_Q68",
331 .brightness_down = P30_PREFIX "_Q69",
332 .brightness_get = "GPLV",
333 .display_set = "SDSP",
334 .display_get = "\\DNXT"},
337 .name = "S1x",
338 .mt_mled = "MLED",
339 .mled_status = "\\EMLE",
340 .mt_wled = "WLED",
341 .mt_lcd_switch = S1x_PREFIX "Q10",
342 .lcd_status = "\\PNOF",
343 .brightness_set = "SPLV",
344 .brightness_get = "GPLV"},
347 .name = "S2x",
348 .mt_mled = "MLED",
349 .mled_status = "\\MAIL",
350 .mt_lcd_switch = S2x_PREFIX "_Q10",
351 .lcd_status = "\\BKLI",
352 .brightness_up = S2x_PREFIX "_Q0B",
353 .brightness_down = S2x_PREFIX "_Q0A"},
356 .name = "W1N",
357 .mt_mled = "MLED",
358 .mt_wled = "WLED",
359 .mt_ledd = "SLCM",
360 .mt_lcd_switch = xxN_PREFIX "_Q10",
361 .lcd_status = "\\BKLT",
362 .brightness_set = "SPLV",
363 .brightness_get = "GPLV",
364 .display_set = "SDSP",
365 .display_get = "\\ADVG"},
368 .name = "W5A",
369 .mt_bt_switch = "BLED",
370 .mt_wled = "WLED",
371 .mt_lcd_switch = xxN_PREFIX "_Q10",
372 .brightness_set = "SPLV",
373 .brightness_get = "GPLV",
374 .display_set = "SDSP",
375 .display_get = "\\ADVG"},
378 .name = "xxN",
379 .mt_mled = "MLED",
380 /* WLED present, but not controlled by ACPI */
381 .mt_lcd_switch = xxN_PREFIX "_Q10",
382 .lcd_status = "\\BKLT",
383 .brightness_set = "SPLV",
384 .brightness_get = "GPLV",
385 .display_set = "SDSP",
386 .display_get = "\\ADVG"}
389 /* procdir we use */
390 static struct proc_dir_entry *asus_proc_dir;
393 * This header is made available to allow proper configuration given model,
394 * revision number , ... this info cannot go in struct asus_hotk because it is
395 * available before the hotk
397 static struct acpi_table_header *asus_info;
399 /* The actual device the driver binds to */
400 static struct asus_hotk *hotk;
403 * The hotkey driver declaration
405 static int asus_hotk_add(struct acpi_device *device);
406 static int asus_hotk_remove(struct acpi_device *device, int type);
407 static struct acpi_driver asus_hotk_driver = {
408 .name = ACPI_HOTK_NAME,
409 .class = ACPI_HOTK_CLASS,
410 .ids = ACPI_HOTK_HID,
411 .ops = {
412 .add = asus_hotk_add,
413 .remove = asus_hotk_remove,
418 * This function evaluates an ACPI method, given an int as parameter, the
419 * method is searched within the scope of the handle, can be NULL. The output
420 * of the method is written is output, which can also be NULL
422 * returns 1 if write is successful, 0 else.
424 static int write_acpi_int(acpi_handle handle, const char *method, int val,
425 struct acpi_buffer *output)
427 struct acpi_object_list params; //list of input parameters (an int here)
428 union acpi_object in_obj; //the only param we use
429 acpi_status status;
431 params.count = 1;
432 params.pointer = &in_obj;
433 in_obj.type = ACPI_TYPE_INTEGER;
434 in_obj.integer.value = val;
436 status = acpi_evaluate_object(handle, (char *)method, &params, output);
437 return (status == AE_OK);
440 static int read_acpi_int(acpi_handle handle, const char *method, int *val)
442 struct acpi_buffer output;
443 union acpi_object out_obj;
444 acpi_status status;
446 output.length = sizeof(out_obj);
447 output.pointer = &out_obj;
449 status = acpi_evaluate_object(handle, (char *)method, NULL, &output);
450 *val = out_obj.integer.value;
451 return (status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER);
455 * We write our info in page, we begin at offset off and cannot write more
456 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
457 * number of bytes written in page
459 static int
460 proc_read_info(char *page, char **start, off_t off, int count, int *eof,
461 void *data)
463 int len = 0;
464 int temp;
465 char buf[16]; //enough for all info
467 * We use the easy way, we don't care of off and count, so we don't set eof
468 * to 1
471 len += sprintf(page, ACPI_HOTK_NAME " " ASUS_ACPI_VERSION "\n");
472 len += sprintf(page + len, "Model reference : %s\n",
473 hotk->methods->name);
475 * The SFUN method probably allows the original driver to get the list
476 * of features supported by a given model. For now, 0x0100 or 0x0800
477 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
478 * The significance of others is yet to be found.
480 if (read_acpi_int(hotk->handle, "SFUN", &temp))
481 len +=
482 sprintf(page + len, "SFUN value : 0x%04x\n", temp);
484 * Another value for userspace: the ASYM method returns 0x02 for
485 * battery low and 0x04 for battery critical, its readings tend to be
486 * more accurate than those provided by _BST.
487 * Note: since not all the laptops provide this method, errors are
488 * silently ignored.
490 if (read_acpi_int(hotk->handle, "ASYM", &temp))
491 len +=
492 sprintf(page + len, "ASYM value : 0x%04x\n", temp);
493 if (asus_info) {
494 snprintf(buf, 16, "%d", asus_info->length);
495 len += sprintf(page + len, "DSDT length : %s\n", buf);
496 snprintf(buf, 16, "%d", asus_info->checksum);
497 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
498 snprintf(buf, 16, "%d", asus_info->revision);
499 len += sprintf(page + len, "DSDT revision : %s\n", buf);
500 snprintf(buf, 7, "%s", asus_info->oem_id);
501 len += sprintf(page + len, "OEM id : %s\n", buf);
502 snprintf(buf, 9, "%s", asus_info->oem_table_id);
503 len += sprintf(page + len, "OEM table id : %s\n", buf);
504 snprintf(buf, 16, "%x", asus_info->oem_revision);
505 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
506 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
507 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
508 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
509 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
512 return len;
516 * /proc handlers
517 * We write our info in page, we begin at offset off and cannot write more
518 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
519 * number of bytes written in page
522 /* Generic LED functions */
523 static int read_led(const char *ledname, int ledmask)
525 if (ledname) {
526 int led_status;
528 if (read_acpi_int(NULL, ledname, &led_status))
529 return led_status;
530 else
531 printk(KERN_WARNING "Asus ACPI: Error reading LED "
532 "status\n");
534 return (hotk->status & ledmask) ? 1 : 0;
537 static int parse_arg(const char __user * buf, unsigned long count, int *val)
539 char s[32];
540 if (!count)
541 return 0;
542 if (count > 31)
543 return -EINVAL;
544 if (copy_from_user(s, buf, count))
545 return -EFAULT;
546 s[count] = 0;
547 if (sscanf(s, "%i", val) != 1)
548 return -EINVAL;
549 return count;
552 /* FIXME: kill extraneous args so it can be called independently */
553 static int
554 write_led(const char __user * buffer, unsigned long count,
555 char *ledname, int ledmask, int invert)
557 int value;
558 int led_out = 0;
560 count = parse_arg(buffer, count, &value);
561 if (count > 0)
562 led_out = value ? 1 : 0;
564 hotk->status =
565 (led_out) ? (hotk->status | ledmask) : (hotk->status & ~ledmask);
567 if (invert) /* invert target value */
568 led_out = !led_out & 0x1;
570 if (!write_acpi_int(hotk->handle, ledname, led_out, NULL))
571 printk(KERN_WARNING "Asus ACPI: LED (%s) write failed\n",
572 ledname);
574 return count;
578 * Proc handlers for MLED
580 static int
581 proc_read_mled(char *page, char **start, off_t off, int count, int *eof,
582 void *data)
584 return sprintf(page, "%d\n",
585 read_led(hotk->methods->mled_status, MLED_ON));
588 static int
589 proc_write_mled(struct file *file, const char __user * buffer,
590 unsigned long count, void *data)
592 return write_led(buffer, count, hotk->methods->mt_mled, MLED_ON, 1);
596 * Proc handlers for LED display
598 static int
599 proc_read_ledd(char *page, char **start, off_t off, int count, int *eof,
600 void *data)
602 return sprintf(page, "0x%08x\n", hotk->ledd_status);
605 static int
606 proc_write_ledd(struct file *file, const char __user * buffer,
607 unsigned long count, void *data)
609 int value;
611 count = parse_arg(buffer, count, &value);
612 if (count > 0) {
613 if (!write_acpi_int
614 (hotk->handle, hotk->methods->mt_ledd, value, NULL))
615 printk(KERN_WARNING
616 "Asus ACPI: LED display write failed\n");
617 else
618 hotk->ledd_status = (u32) value;
619 } else if (count < 0)
620 printk(KERN_WARNING "Asus ACPI: Error reading user input\n");
622 return count;
626 * Proc handlers for WLED
628 static int
629 proc_read_wled(char *page, char **start, off_t off, int count, int *eof,
630 void *data)
632 return sprintf(page, "%d\n",
633 read_led(hotk->methods->wled_status, WLED_ON));
636 static int
637 proc_write_wled(struct file *file, const char __user * buffer,
638 unsigned long count, void *data)
640 return write_led(buffer, count, hotk->methods->mt_wled, WLED_ON, 0);
644 * Proc handlers for Bluetooth
646 static int
647 proc_read_bluetooth(char *page, char **start, off_t off, int count, int *eof,
648 void *data)
650 return sprintf(page, "%d\n", read_led(hotk->methods->bt_status, BT_ON));
653 static int
654 proc_write_bluetooth(struct file *file, const char __user * buffer,
655 unsigned long count, void *data)
657 /* Note: mt_bt_switch controls both internal Bluetooth adapter's
658 presence and its LED */
659 return write_led(buffer, count, hotk->methods->mt_bt_switch, BT_ON, 0);
663 * Proc handlers for TLED
665 static int
666 proc_read_tled(char *page, char **start, off_t off, int count, int *eof,
667 void *data)
669 return sprintf(page, "%d\n",
670 read_led(hotk->methods->tled_status, TLED_ON));
673 static int
674 proc_write_tled(struct file *file, const char __user * buffer,
675 unsigned long count, void *data)
677 return write_led(buffer, count, hotk->methods->mt_tled, TLED_ON, 0);
680 static int get_lcd_state(void)
682 int lcd = 0;
684 if (hotk->model != L3H) {
685 /* We don't have to check anything if we are here */
686 if (!read_acpi_int(NULL, hotk->methods->lcd_status, &lcd))
687 printk(KERN_WARNING
688 "Asus ACPI: Error reading LCD status\n");
690 if (hotk->model == L2D)
691 lcd = ~lcd;
692 } else { /* L3H and the like have to be handled differently */
693 acpi_status status = 0;
694 struct acpi_object_list input;
695 union acpi_object mt_params[2];
696 struct acpi_buffer output;
697 union acpi_object out_obj;
699 input.count = 2;
700 input.pointer = mt_params;
701 /* Note: the following values are partly guessed up, but
702 otherwise they seem to work */
703 mt_params[0].type = ACPI_TYPE_INTEGER;
704 mt_params[0].integer.value = 0x02;
705 mt_params[1].type = ACPI_TYPE_INTEGER;
706 mt_params[1].integer.value = 0x02;
708 output.length = sizeof(out_obj);
709 output.pointer = &out_obj;
711 status =
712 acpi_evaluate_object(NULL, hotk->methods->lcd_status,
713 &input, &output);
714 if (status != AE_OK)
715 return -1;
716 if (out_obj.type == ACPI_TYPE_INTEGER)
717 /* That's what the AML code does */
718 lcd = out_obj.integer.value >> 8;
721 return (lcd & 1);
724 static int set_lcd_state(int value)
726 int lcd = 0;
727 acpi_status status = 0;
729 lcd = value ? 1 : 0;
730 if (lcd != get_lcd_state()) {
731 /* switch */
732 if (hotk->model != L3H) {
733 status =
734 acpi_evaluate_object(NULL,
735 hotk->methods->mt_lcd_switch,
736 NULL, NULL);
737 } else { /* L3H and the like have to be handled differently */
738 if (!write_acpi_int
739 (hotk->handle, hotk->methods->mt_lcd_switch, 0x07,
740 NULL))
741 status = AE_ERROR;
742 /* L3H's AML executes EHK (0x07) upon Fn+F7 keypress,
743 the exact behaviour is simulated here */
745 if (ACPI_FAILURE(status))
746 printk(KERN_WARNING "Asus ACPI: Error switching LCD\n");
748 return 0;
752 static int
753 proc_read_lcd(char *page, char **start, off_t off, int count, int *eof,
754 void *data)
756 return sprintf(page, "%d\n", get_lcd_state());
759 static int
760 proc_write_lcd(struct file *file, const char __user * buffer,
761 unsigned long count, void *data)
763 int value;
765 count = parse_arg(buffer, count, &value);
766 if (count > 0)
767 set_lcd_state(value);
768 return count;
771 static int read_brightness(void)
773 int value;
775 if (hotk->methods->brightness_get) { /* SPLV/GPLV laptop */
776 if (!read_acpi_int(hotk->handle, hotk->methods->brightness_get,
777 &value))
778 printk(KERN_WARNING
779 "Asus ACPI: Error reading brightness\n");
780 } else if (hotk->methods->brightness_status) { /* For D1 for example */
781 if (!read_acpi_int(NULL, hotk->methods->brightness_status,
782 &value))
783 printk(KERN_WARNING
784 "Asus ACPI: Error reading brightness\n");
785 } else /* No GPLV method */
786 value = hotk->brightness;
787 return value;
791 * Change the brightness level
793 static void set_brightness(int value)
795 acpi_status status = 0;
797 /* SPLV laptop */
798 if (hotk->methods->brightness_set) {
799 if (!write_acpi_int(hotk->handle, hotk->methods->brightness_set,
800 value, NULL))
801 printk(KERN_WARNING
802 "Asus ACPI: Error changing brightness\n");
803 return;
806 /* No SPLV method if we are here, act as appropriate */
807 value -= read_brightness();
808 while (value != 0) {
809 status = acpi_evaluate_object(NULL, (value > 0) ?
810 hotk->methods->brightness_up :
811 hotk->methods->brightness_down,
812 NULL, NULL);
813 (value > 0) ? value-- : value++;
814 if (ACPI_FAILURE(status))
815 printk(KERN_WARNING
816 "Asus ACPI: Error changing brightness\n");
818 return;
821 static int
822 proc_read_brn(char *page, char **start, off_t off, int count, int *eof,
823 void *data)
825 return sprintf(page, "%d\n", read_brightness());
828 static int
829 proc_write_brn(struct file *file, const char __user * buffer,
830 unsigned long count, void *data)
832 int value;
834 count = parse_arg(buffer, count, &value);
835 if (count > 0) {
836 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
837 /* 0 <= value <= 15 */
838 set_brightness(value);
839 } else if (count < 0) {
840 printk(KERN_WARNING "Asus ACPI: Error reading user input\n");
843 return count;
846 static void set_display(int value)
848 /* no sanity check needed for now */
849 if (!write_acpi_int(hotk->handle, hotk->methods->display_set,
850 value, NULL))
851 printk(KERN_WARNING "Asus ACPI: Error setting display\n");
852 return;
856 * Now, *this* one could be more user-friendly, but so far, no-one has
857 * complained. The significance of bits is the same as in proc_write_disp()
859 static int
860 proc_read_disp(char *page, char **start, off_t off, int count, int *eof,
861 void *data)
863 int value = 0;
865 if (!read_acpi_int(hotk->handle, hotk->methods->display_get, &value))
866 printk(KERN_WARNING
867 "Asus ACPI: Error reading display status\n");
868 value &= 0x07; /* needed for some models, shouldn't hurt others */
869 return sprintf(page, "%d\n", value);
873 * Experimental support for display switching. As of now: 1 should activate
874 * the LCD output, 2 should do for CRT, and 4 for TV-Out. Any combination
875 * (bitwise) of these will suffice. I never actually tested 3 displays hooked up
876 * simultaneously, so be warned. See the acpi4asus README for more info.
878 static int
879 proc_write_disp(struct file *file, const char __user * buffer,
880 unsigned long count, void *data)
882 int value;
884 count = parse_arg(buffer, count, &value);
885 if (count > 0)
886 set_display(value);
887 else if (count < 0)
888 printk(KERN_WARNING "Asus ACPI: Error reading user input\n");
890 return count;
893 typedef int (proc_readfunc) (char *page, char **start, off_t off, int count,
894 int *eof, void *data);
895 typedef int (proc_writefunc) (struct file * file, const char __user * buffer,
896 unsigned long count, void *data);
898 static int
899 asus_proc_add(char *name, proc_writefunc * writefunc,
900 proc_readfunc * readfunc, mode_t mode,
901 struct acpi_device *device)
903 struct proc_dir_entry *proc =
904 create_proc_entry(name, mode, acpi_device_dir(device));
905 if (!proc) {
906 printk(KERN_WARNING " Unable to create %s fs entry\n", name);
907 return -1;
909 proc->write_proc = writefunc;
910 proc->read_proc = readfunc;
911 proc->data = acpi_driver_data(device);
912 proc->owner = THIS_MODULE;
913 proc->uid = asus_uid;
914 proc->gid = asus_gid;
915 return 0;
918 static int asus_hotk_add_fs(struct acpi_device *device)
920 struct proc_dir_entry *proc;
921 mode_t mode;
924 * If parameter uid or gid is not changed, keep the default setting for
925 * our proc entries (-rw-rw-rw-) else, it means we care about security,
926 * and then set to -rw-rw----
929 if ((asus_uid == 0) && (asus_gid == 0)) {
930 mode = S_IFREG | S_IRUGO | S_IWUGO;
931 } else {
932 mode = S_IFREG | S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP;
933 printk(KERN_WARNING " asus_uid and asus_gid parameters are "
934 "deprecated, use chown and chmod instead!\n");
937 acpi_device_dir(device) = asus_proc_dir;
938 if (!acpi_device_dir(device))
939 return -ENODEV;
941 proc = create_proc_entry(PROC_INFO, mode, acpi_device_dir(device));
942 if (proc) {
943 proc->read_proc = proc_read_info;
944 proc->data = acpi_driver_data(device);
945 proc->owner = THIS_MODULE;
946 proc->uid = asus_uid;
947 proc->gid = asus_gid;
948 } else {
949 printk(KERN_WARNING " Unable to create " PROC_INFO
950 " fs entry\n");
953 if (hotk->methods->mt_wled) {
954 asus_proc_add(PROC_WLED, &proc_write_wled, &proc_read_wled,
955 mode, device);
958 if (hotk->methods->mt_ledd) {
959 asus_proc_add(PROC_LEDD, &proc_write_ledd, &proc_read_ledd,
960 mode, device);
963 if (hotk->methods->mt_mled) {
964 asus_proc_add(PROC_MLED, &proc_write_mled, &proc_read_mled,
965 mode, device);
968 if (hotk->methods->mt_tled) {
969 asus_proc_add(PROC_TLED, &proc_write_tled, &proc_read_tled,
970 mode, device);
973 if (hotk->methods->mt_bt_switch) {
974 asus_proc_add(PROC_BT, &proc_write_bluetooth,
975 &proc_read_bluetooth, mode, device);
979 * We need both read node and write method as LCD switch is also accessible
980 * from keyboard
982 if (hotk->methods->mt_lcd_switch && hotk->methods->lcd_status) {
983 asus_proc_add(PROC_LCD, &proc_write_lcd, &proc_read_lcd, mode,
984 device);
987 if ((hotk->methods->brightness_up && hotk->methods->brightness_down) ||
988 (hotk->methods->brightness_get && hotk->methods->brightness_set)) {
989 asus_proc_add(PROC_BRN, &proc_write_brn, &proc_read_brn, mode,
990 device);
993 if (hotk->methods->display_set) {
994 asus_proc_add(PROC_DISP, &proc_write_disp, &proc_read_disp,
995 mode, device);
998 return 0;
1001 static int asus_hotk_remove_fs(struct acpi_device *device)
1003 if (acpi_device_dir(device)) {
1004 remove_proc_entry(PROC_INFO, acpi_device_dir(device));
1005 if (hotk->methods->mt_wled)
1006 remove_proc_entry(PROC_WLED, acpi_device_dir(device));
1007 if (hotk->methods->mt_mled)
1008 remove_proc_entry(PROC_MLED, acpi_device_dir(device));
1009 if (hotk->methods->mt_tled)
1010 remove_proc_entry(PROC_TLED, acpi_device_dir(device));
1011 if (hotk->methods->mt_ledd)
1012 remove_proc_entry(PROC_LEDD, acpi_device_dir(device));
1013 if (hotk->methods->mt_bt_switch)
1014 remove_proc_entry(PROC_BT, acpi_device_dir(device));
1015 if (hotk->methods->mt_lcd_switch && hotk->methods->lcd_status)
1016 remove_proc_entry(PROC_LCD, acpi_device_dir(device));
1017 if ((hotk->methods->brightness_up
1018 && hotk->methods->brightness_down)
1019 || (hotk->methods->brightness_get
1020 && hotk->methods->brightness_set))
1021 remove_proc_entry(PROC_BRN, acpi_device_dir(device));
1022 if (hotk->methods->display_set)
1023 remove_proc_entry(PROC_DISP, acpi_device_dir(device));
1025 return 0;
1028 static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
1030 /* TODO Find a better way to handle events count. */
1031 if (!hotk)
1032 return;
1034 if ((event & ~((u32) BR_UP)) < 16) {
1035 hotk->brightness = (event & ~((u32) BR_UP));
1036 } else if ((event & ~((u32) BR_DOWN)) < 16) {
1037 hotk->brightness = (event & ~((u32) BR_DOWN));
1040 acpi_bus_generate_event(hotk->device, event,
1041 hotk->event_count[event % 128]++);
1043 return;
1047 * This function is used to initialize the hotk with right values. In this
1048 * method, we can make all the detection we want, and modify the hotk struct
1050 static int asus_hotk_get_info(void)
1052 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1053 struct acpi_buffer dsdt = { ACPI_ALLOCATE_BUFFER, NULL };
1054 union acpi_object *model = NULL;
1055 int bsts_result;
1056 acpi_status status;
1059 * Get DSDT headers early enough to allow for differentiating between
1060 * models, but late enough to allow acpi_bus_register_driver() to fail
1061 * before doing anything ACPI-specific. Should we encounter a machine,
1062 * which needs special handling (i.e. its hotkey device has a different
1063 * HID), this bit will be moved. A global variable asus_info contains
1064 * the DSDT header.
1066 status = acpi_get_table(ACPI_TABLE_ID_DSDT, 1, &dsdt);
1067 if (ACPI_FAILURE(status))
1068 printk(KERN_WARNING " Couldn't get the DSDT table header\n");
1069 else
1070 asus_info = (struct acpi_table_header *)dsdt.pointer;
1072 /* We have to write 0 on init this far for all ASUS models */
1073 if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
1074 printk(KERN_ERR " Hotkey initialization failed\n");
1075 return -ENODEV;
1078 /* This needs to be called for some laptops to init properly */
1079 if (!read_acpi_int(hotk->handle, "BSTS", &bsts_result))
1080 printk(KERN_WARNING " Error calling BSTS\n");
1081 else if (bsts_result)
1082 printk(KERN_NOTICE " BSTS called, 0x%02x returned\n",
1083 bsts_result);
1085 /* This is unlikely with implicit return */
1086 if (buffer.pointer == NULL)
1087 return -EINVAL;
1089 model = (union acpi_object *)buffer.pointer;
1091 * Samsung P30 has a device with a valid _HID whose INIT does not
1092 * return anything. It used to be possible to catch this exception,
1093 * but the implicit return code will now happily confuse the
1094 * driver. We assume that every ACPI_TYPE_STRING is a valid model
1095 * identifier but it's still possible to get completely bogus data.
1097 if (model->type == ACPI_TYPE_STRING) {
1098 printk(KERN_NOTICE " %s model detected, ",
1099 model->string.pointer);
1100 } else {
1101 if (asus_info && /* Samsung P30 */
1102 strncmp(asus_info->oem_table_id, "ODEM", 4) == 0) {
1103 hotk->model = P30;
1104 printk(KERN_NOTICE
1105 " Samsung P30 detected, supported\n");
1106 } else {
1107 hotk->model = M2E;
1108 printk(KERN_WARNING " no string returned by INIT\n");
1109 printk(KERN_WARNING " trying default values, supply "
1110 "the developers with your DSDT\n");
1112 hotk->methods = &model_conf[hotk->model];
1114 kfree(model);
1116 return AE_OK;
1119 hotk->model = END_MODEL;
1120 if (strncmp(model->string.pointer, "L3D", 3) == 0)
1121 hotk->model = L3D;
1122 else if (strncmp(model->string.pointer, "L3H", 3) == 0 ||
1123 strncmp(model->string.pointer, "L2E", 3) == 0)
1124 hotk->model = L3H;
1125 else if (strncmp(model->string.pointer, "L3", 2) == 0 ||
1126 strncmp(model->string.pointer, "L2B", 3) == 0)
1127 hotk->model = L3C;
1128 else if (strncmp(model->string.pointer, "L8L", 3) == 0)
1129 hotk->model = L8L;
1130 else if (strncmp(model->string.pointer, "L4R", 3) == 0)
1131 hotk->model = L4R;
1132 else if (strncmp(model->string.pointer, "M6N", 3) == 0 ||
1133 strncmp(model->string.pointer, "W3N", 3) == 0)
1134 hotk->model = M6N;
1135 else if (strncmp(model->string.pointer, "M6R", 3) == 0 ||
1136 strncmp(model->string.pointer, "A3G", 3) == 0)
1137 hotk->model = M6R;
1138 else if (strncmp(model->string.pointer, "M2N", 3) == 0 ||
1139 strncmp(model->string.pointer, "M3N", 3) == 0 ||
1140 strncmp(model->string.pointer, "M5N", 3) == 0 ||
1141 strncmp(model->string.pointer, "M6N", 3) == 0 ||
1142 strncmp(model->string.pointer, "S1N", 3) == 0 ||
1143 strncmp(model->string.pointer, "S5N", 3) == 0)
1144 hotk->model = xxN;
1145 else if (strncmp(model->string.pointer, "M1", 2) == 0)
1146 hotk->model = M1A;
1147 else if (strncmp(model->string.pointer, "M2", 2) == 0 ||
1148 strncmp(model->string.pointer, "L4E", 3) == 0)
1149 hotk->model = M2E;
1150 else if (strncmp(model->string.pointer, "L2", 2) == 0)
1151 hotk->model = L2D;
1152 else if (strncmp(model->string.pointer, "L8", 2) == 0)
1153 hotk->model = S1x;
1154 else if (strncmp(model->string.pointer, "D1", 2) == 0)
1155 hotk->model = D1x;
1156 else if (strncmp(model->string.pointer, "A1", 2) == 0)
1157 hotk->model = A1x;
1158 else if (strncmp(model->string.pointer, "A2", 2) == 0)
1159 hotk->model = A2x;
1160 else if (strncmp(model->string.pointer, "J1", 2) == 0)
1161 hotk->model = S2x;
1162 else if (strncmp(model->string.pointer, "L5", 2) == 0)
1163 hotk->model = L5x;
1164 else if (strncmp(model->string.pointer, "A4G", 3) == 0)
1165 hotk->model = A4G;
1166 else if (strncmp(model->string.pointer, "W1N", 3) == 0)
1167 hotk->model = W1N;
1168 else if (strncmp(model->string.pointer, "W5A", 3) == 0)
1169 hotk->model = W5A;
1171 if (hotk->model == END_MODEL) {
1172 printk("unsupported, trying default values, supply the "
1173 "developers with your DSDT\n");
1174 hotk->model = M2E;
1175 } else {
1176 printk("supported\n");
1179 hotk->methods = &model_conf[hotk->model];
1181 /* Sort of per-model blacklist */
1182 if (strncmp(model->string.pointer, "L2B", 3) == 0)
1183 hotk->methods->lcd_status = NULL;
1184 /* L2B is similar enough to L3C to use its settings, with this only
1185 exception */
1186 else if (strncmp(model->string.pointer, "A3G", 3) == 0)
1187 hotk->methods->lcd_status = "\\BLFG";
1188 /* A3G is like M6R */
1189 else if (strncmp(model->string.pointer, "S5N", 3) == 0 ||
1190 strncmp(model->string.pointer, "M5N", 3) == 0 ||
1191 strncmp(model->string.pointer, "W3N", 3) == 0)
1192 hotk->methods->mt_mled = NULL;
1193 /* S5N, M5N and W3N have no MLED */
1194 else if (strncmp(model->string.pointer, "M2N", 3) == 0)
1195 hotk->methods->mt_wled = "WLED";
1196 /* M2N has a usable WLED */
1197 else if (asus_info) {
1198 if (strncmp(asus_info->oem_table_id, "L1", 2) == 0)
1199 hotk->methods->mled_status = NULL;
1200 /* S1300A reports L84F, but L1400B too, account for that */
1203 kfree(model);
1205 return AE_OK;
1208 static int asus_hotk_check(void)
1210 int result = 0;
1212 result = acpi_bus_get_status(hotk->device);
1213 if (result)
1214 return result;
1216 if (hotk->device->status.present) {
1217 result = asus_hotk_get_info();
1218 } else {
1219 printk(KERN_ERR " Hotkey device not present, aborting\n");
1220 return -EINVAL;
1223 return result;
1226 static int asus_hotk_found;
1228 static int asus_hotk_add(struct acpi_device *device)
1230 acpi_status status = AE_OK;
1231 int result;
1233 if (!device)
1234 return -EINVAL;
1236 printk(KERN_NOTICE "Asus Laptop ACPI Extras version %s\n",
1237 ASUS_ACPI_VERSION);
1239 hotk =
1240 (struct asus_hotk *)kmalloc(sizeof(struct asus_hotk), GFP_KERNEL);
1241 if (!hotk)
1242 return -ENOMEM;
1243 memset(hotk, 0, sizeof(struct asus_hotk));
1245 hotk->handle = device->handle;
1246 strcpy(acpi_device_name(device), ACPI_HOTK_DEVICE_NAME);
1247 strcpy(acpi_device_class(device), ACPI_HOTK_CLASS);
1248 acpi_driver_data(device) = hotk;
1249 hotk->device = device;
1251 result = asus_hotk_check();
1252 if (result)
1253 goto end;
1255 result = asus_hotk_add_fs(device);
1256 if (result)
1257 goto end;
1260 * We install the handler, it will receive the hotk in parameter, so, we
1261 * could add other data to the hotk struct
1263 status = acpi_install_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
1264 asus_hotk_notify, hotk);
1265 if (ACPI_FAILURE(status))
1266 printk(KERN_ERR " Error installing notify handler\n");
1268 /* For laptops without GPLV: init the hotk->brightness value */
1269 if ((!hotk->methods->brightness_get)
1270 && (!hotk->methods->brightness_status)
1271 && (hotk->methods->brightness_up && hotk->methods->brightness_down)) {
1272 status =
1273 acpi_evaluate_object(NULL, hotk->methods->brightness_down,
1274 NULL, NULL);
1275 if (ACPI_FAILURE(status))
1276 printk(KERN_WARNING " Error changing brightness\n");
1277 else {
1278 status =
1279 acpi_evaluate_object(NULL,
1280 hotk->methods->brightness_up,
1281 NULL, NULL);
1282 if (ACPI_FAILURE(status))
1283 printk(KERN_WARNING " Strange, error changing"
1284 " brightness\n");
1288 asus_hotk_found = 1;
1290 /* LED display is off by default */
1291 hotk->ledd_status = 0xFFF;
1293 end:
1294 if (result) {
1295 kfree(hotk);
1298 return result;
1301 static int asus_hotk_remove(struct acpi_device *device, int type)
1303 acpi_status status = 0;
1305 if (!device || !acpi_driver_data(device))
1306 return -EINVAL;
1308 status = acpi_remove_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
1309 asus_hotk_notify);
1310 if (ACPI_FAILURE(status))
1311 printk(KERN_ERR "Asus ACPI: Error removing notify handler\n");
1313 asus_hotk_remove_fs(device);
1315 kfree(hotk);
1317 return 0;
1320 static int __init asus_acpi_init(void)
1322 int result;
1324 if (acpi_disabled)
1325 return -ENODEV;
1327 if (!acpi_specific_hotkey_enabled) {
1328 printk(KERN_ERR "Using generic hotkey driver\n");
1329 return -ENODEV;
1331 asus_proc_dir = proc_mkdir(PROC_ASUS, acpi_root_dir);
1332 if (!asus_proc_dir) {
1333 printk(KERN_ERR "Asus ACPI: Unable to create /proc entry\n");
1334 return -ENODEV;
1336 asus_proc_dir->owner = THIS_MODULE;
1338 result = acpi_bus_register_driver(&asus_hotk_driver);
1339 if (result < 0) {
1340 remove_proc_entry(PROC_ASUS, acpi_root_dir);
1341 return result;
1345 * This is a bit of a kludge. We only want this module loaded
1346 * for ASUS systems, but there's currently no way to probe the
1347 * ACPI namespace for ASUS HIDs. So we just return failure if
1348 * we didn't find one, which will cause the module to be
1349 * unloaded.
1351 if (!asus_hotk_found) {
1352 acpi_bus_unregister_driver(&asus_hotk_driver);
1353 remove_proc_entry(PROC_ASUS, acpi_root_dir);
1354 return result;
1357 return 0;
1360 static void __exit asus_acpi_exit(void)
1362 acpi_bus_unregister_driver(&asus_hotk_driver);
1363 remove_proc_entry(PROC_ASUS, acpi_root_dir);
1365 kfree(asus_info);
1367 return;
1370 module_init(asus_acpi_init);
1371 module_exit(asus_acpi_exit);