blob: c613a4340db50b078705df357368da37c2311a3e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * asus_acpi.c - Asus Laptop ACPI Extras
3 *
4 *
Karol Kozimora170a532006-06-30 19:03:00 -04005 * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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.
Karol Kozimor42cb8912006-06-30 19:04:00 -040029 * Éric Burghard - LED display support for W1N
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
32
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>
41
Karol Kozimora170a532006-06-30 19:03:00 -040042#define ASUS_ACPI_VERSION "0.30"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#define PROC_ASUS "asus" //the directory
45#define PROC_MLED "mled"
46#define PROC_WLED "wled"
47#define PROC_TLED "tled"
Karol Kozimor42cb8912006-06-30 19:04:00 -040048#define PROC_LEDD "ledd"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define PROC_INFO "info"
50#define PROC_LCD "lcd"
51#define PROC_BRN "brn"
52#define PROC_DISP "disp"
53
54#define ACPI_HOTK_NAME "Asus Laptop ACPI Extras Driver"
55#define ACPI_HOTK_CLASS "hotkey"
56#define ACPI_HOTK_DEVICE_NAME "Hotkey"
57#define ACPI_HOTK_HID "ATK0100"
58
59/*
60 * Some events we use, same for all Asus
61 */
Len Brown4be44fc2005-08-05 00:44:28 -040062#define BR_UP 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define BR_DOWN 0x20
64
65/*
66 * Flags for hotk status
67 */
68#define MLED_ON 0x01 //is MLED ON ?
69#define WLED_ON 0x02
70#define TLED_ON 0x04
71
72MODULE_AUTHOR("Julien Lerouge, Karol Kozimor");
73MODULE_DESCRIPTION(ACPI_HOTK_NAME);
74MODULE_LICENSE("GPL");
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static uid_t asus_uid;
77static gid_t asus_gid;
78module_param(asus_uid, uint, 0);
Karol Kozimoraea19aa2006-01-03 23:05:00 -050079MODULE_PARM_DESC(asus_uid, "UID for entries in /proc/acpi/asus.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070080module_param(asus_gid, uint, 0);
Karol Kozimoraea19aa2006-01-03 23:05:00 -050081MODULE_PARM_DESC(asus_gid, "GID for entries in /proc/acpi/asus.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* For each model, all features implemented,
84 * those marked with R are relative to HOTK, A for absolute */
85struct model_data {
Len Brown4be44fc2005-08-05 00:44:28 -040086 char *name; //name of the laptop________________A
87 char *mt_mled; //method to handle mled_____________R
88 char *mled_status; //node to handle mled reading_______A
89 char *mt_wled; //method to handle wled_____________R
90 char *wled_status; //node to handle wled reading_______A
91 char *mt_tled; //method to handle tled_____________R
92 char *tled_status; //node to handle tled reading_______A
Karol Kozimor42cb8912006-06-30 19:04:00 -040093 char *mt_ledd; //method to handle LED display______R
Len Brown4be44fc2005-08-05 00:44:28 -040094 char *mt_lcd_switch; //method to turn LCD ON/OFF_________A
95 char *lcd_status; //node to read LCD panel state______A
96 char *brightness_up; //method to set brightness up_______A
97 char *brightness_down; //guess what ?______________________A
98 char *brightness_set; //method to set absolute brightness_R
99 char *brightness_get; //method to get absolute brightness_R
100 char *brightness_status; //node to get brightness____________A
101 char *display_set; //method to set video output________R
102 char *display_get; //method to get video output________R
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
105/*
106 * This is the main structure, we can use it to store anything interesting
107 * about the hotk device
108 */
109struct asus_hotk {
Len Brown4be44fc2005-08-05 00:44:28 -0400110 struct acpi_device *device; //the device we are in
111 acpi_handle handle; //the handle of the hotk device
112 char status; //status of the hotk, for LEDs, ...
Karol Kozimor42cb8912006-06-30 19:04:00 -0400113 u32 ledd_status; //status of the LED display
Len Brown4be44fc2005-08-05 00:44:28 -0400114 struct model_data *methods; //methods available on the laptop
115 u8 brightness; //brightness level
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 enum {
Len Brown4be44fc2005-08-05 00:44:28 -0400117 A1x = 0, //A1340D, A1300F
118 A2x, //A2500H
Karol Kozimorf78c5892006-06-30 19:06:00 -0400119 A4G, //A4700G
Len Brown4be44fc2005-08-05 00:44:28 -0400120 D1x, //D1
121 L2D, //L2000D
122 L3C, //L3800C
123 L3D, //L3400D
124 L3H, //L3H, but also L2000E
125 L4R, //L4500R
126 L5x, //L5800C
127 L8L, //L8400L
128 M1A, //M1300A
129 M2E, //M2400E, L4400L
Karol Kozimorc067a782006-06-30 19:05:00 -0400130 M6N, //M6800N, W3400N
Karol Kozimored2cb072006-06-30 19:03:00 -0400131 M6R, //M6700R, A3000G
Len Brown4be44fc2005-08-05 00:44:28 -0400132 P30, //Samsung P30
133 S1x, //S1300A, but also L1400B and M2400A (L84F)
134 S2x, //S200 (J1 reported), Victor MP-XP7210
Karol Kozimor42cb8912006-06-30 19:04:00 -0400135 W1N, //W1000N
136 xxN, //M2400N, M3700N, M5200N, M6800N, S1300N, S5200N
Len Brown4be44fc2005-08-05 00:44:28 -0400137 //(Centrino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 END_MODEL
Len Brown4be44fc2005-08-05 00:44:28 -0400139 } model; //Models currently supported
140 u16 event_count[128]; //count for each event TODO make this better
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
143/* Here we go */
144#define A1x_PREFIX "\\_SB.PCI0.ISA.EC0."
145#define L3C_PREFIX "\\_SB.PCI0.PX40.ECD0."
146#define M1A_PREFIX "\\_SB.PCI0.PX40.EC0."
147#define P30_PREFIX "\\_SB.PCI0.LPCB.EC0."
148#define S1x_PREFIX "\\_SB.PCI0.PX40."
149#define S2x_PREFIX A1x_PREFIX
150#define xxN_PREFIX "\\_SB.PCI0.SBRG.EC0."
151
152static struct model_data model_conf[END_MODEL] = {
Len Brown4be44fc2005-08-05 00:44:28 -0400153 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * TODO I have seen a SWBX and AIBX method on some models, like L1400B,
155 * it seems to be a kind of switch, but what for ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 */
157
158 {
Len Brown4be44fc2005-08-05 00:44:28 -0400159 .name = "A1x",
160 .mt_mled = "MLED",
161 .mled_status = "\\MAIL",
162 .mt_lcd_switch = A1x_PREFIX "_Q10",
163 .lcd_status = "\\BKLI",
164 .brightness_up = A1x_PREFIX "_Q0E",
165 .brightness_down = A1x_PREFIX "_Q0F"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 {
Len Brown4be44fc2005-08-05 00:44:28 -0400168 .name = "A2x",
169 .mt_mled = "MLED",
170 .mt_wled = "WLED",
171 .wled_status = "\\SG66",
172 .mt_lcd_switch = "\\Q10",
173 .lcd_status = "\\BAOF",
174 .brightness_set = "SPLV",
175 .brightness_get = "GPLV",
176 .display_set = "SDSP",
177 .display_get = "\\INFB"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 {
Karol Kozimorf78c5892006-06-30 19:06:00 -0400180 .name = "A4G",
181 .mt_mled = "MLED",
182/* WLED present, but not controlled by ACPI */
183 .mt_lcd_switch = xxN_PREFIX "_Q10",
184 .brightness_set = "SPLV",
185 .brightness_get = "GPLV",
186 .display_set = "SDSP",
187 .display_get = "\\ADVG"},
188
189 {
Len Brown4be44fc2005-08-05 00:44:28 -0400190 .name = "D1x",
191 .mt_mled = "MLED",
192 .mt_lcd_switch = "\\Q0D",
193 .lcd_status = "\\GP11",
194 .brightness_up = "\\Q0C",
195 .brightness_down = "\\Q0B",
196 .brightness_status = "\\BLVL",
197 .display_set = "SDSP",
198 .display_get = "\\INFB"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 {
Len Brown4be44fc2005-08-05 00:44:28 -0400201 .name = "L2D",
202 .mt_mled = "MLED",
203 .mled_status = "\\SGP6",
204 .mt_wled = "WLED",
205 .wled_status = "\\RCP3",
206 .mt_lcd_switch = "\\Q10",
207 .lcd_status = "\\SGP0",
208 .brightness_up = "\\Q0E",
209 .brightness_down = "\\Q0F",
210 .display_set = "SDSP",
211 .display_get = "\\INFB"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 {
Len Brown4be44fc2005-08-05 00:44:28 -0400214 .name = "L3C",
215 .mt_mled = "MLED",
216 .mt_wled = "WLED",
217 .mt_lcd_switch = L3C_PREFIX "_Q10",
218 .lcd_status = "\\GL32",
219 .brightness_set = "SPLV",
220 .brightness_get = "GPLV",
221 .display_set = "SDSP",
222 .display_get = "\\_SB.PCI0.PCI1.VGAC.NMAP"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 {
Len Brown4be44fc2005-08-05 00:44:28 -0400225 .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"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 {
Len Brown4be44fc2005-08-05 00:44:28 -0400237 .name = "L3H",
238 .mt_mled = "MLED",
239 .mt_wled = "WLED",
240 .mt_lcd_switch = "EHK",
241 .lcd_status = "\\_SB.PCI0.PM.PBC",
242 .brightness_set = "SPLV",
243 .brightness_get = "GPLV",
244 .display_set = "SDSP",
245 .display_get = "\\INFB"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 {
Len Brown4be44fc2005-08-05 00:44:28 -0400248 .name = "L4R",
249 .mt_mled = "MLED",
250 .mt_wled = "WLED",
251 .wled_status = "\\_SB.PCI0.SBRG.SG13",
252 .mt_lcd_switch = xxN_PREFIX "_Q10",
253 .lcd_status = "\\_SB.PCI0.SBSM.SEO4",
254 .brightness_set = "SPLV",
255 .brightness_get = "GPLV",
256 .display_set = "SDSP",
257 .display_get = "\\_SB.PCI0.P0P1.VGA.GETD"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 {
Len Brown4be44fc2005-08-05 00:44:28 -0400260 .name = "L5x",
261 .mt_mled = "MLED",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/* WLED present, but not controlled by ACPI */
Len Brown4be44fc2005-08-05 00:44:28 -0400263 .mt_tled = "TLED",
264 .mt_lcd_switch = "\\Q0D",
265 .lcd_status = "\\BAOF",
266 .brightness_set = "SPLV",
267 .brightness_get = "GPLV",
268 .display_set = "SDSP",
269 .display_get = "\\INFB"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 {
Len Brown4be44fc2005-08-05 00:44:28 -0400272 .name = "L8L"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/* No features, but at least support the hotkeys */
Len Brown4be44fc2005-08-05 00:44:28 -0400274 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 {
Len Brown4be44fc2005-08-05 00:44:28 -0400277 .name = "M1A",
278 .mt_mled = "MLED",
279 .mt_lcd_switch = M1A_PREFIX "Q10",
280 .lcd_status = "\\PNOF",
281 .brightness_up = M1A_PREFIX "Q0E",
282 .brightness_down = M1A_PREFIX "Q0F",
283 .brightness_status = "\\BRIT",
284 .display_set = "SDSP",
285 .display_get = "\\INFB"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 {
Len Brown4be44fc2005-08-05 00:44:28 -0400288 .name = "M2E",
289 .mt_mled = "MLED",
290 .mt_wled = "WLED",
291 .mt_lcd_switch = "\\Q10",
292 .lcd_status = "\\GP06",
293 .brightness_set = "SPLV",
294 .brightness_get = "GPLV",
295 .display_set = "SDSP",
296 .display_get = "\\INFB"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 {
Len Brown4be44fc2005-08-05 00:44:28 -0400299 .name = "M6N",
300 .mt_mled = "MLED",
301 .mt_wled = "WLED",
302 .wled_status = "\\_SB.PCI0.SBRG.SG13",
303 .mt_lcd_switch = xxN_PREFIX "_Q10",
304 .lcd_status = "\\_SB.BKLT",
305 .brightness_set = "SPLV",
306 .brightness_get = "GPLV",
307 .display_set = "SDSP",
Karol Kozimorbb84db92006-01-03 23:03:00 -0500308 .display_get = "\\_SB.PCI0.P0P1.VGA.GETD"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 {
Len Brown4be44fc2005-08-05 00:44:28 -0400310 .name = "M6R",
311 .mt_mled = "MLED",
312 .mt_wled = "WLED",
313 .mt_lcd_switch = xxN_PREFIX "_Q10",
314 .lcd_status = "\\_SB.PCI0.SBSM.SEO4",
315 .brightness_set = "SPLV",
316 .brightness_get = "GPLV",
317 .display_set = "SDSP",
318 .display_get = "\\SSTE"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 {
Len Brown4be44fc2005-08-05 00:44:28 -0400321 .name = "P30",
322 .mt_wled = "WLED",
323 .mt_lcd_switch = P30_PREFIX "_Q0E",
324 .lcd_status = "\\BKLT",
325 .brightness_up = P30_PREFIX "_Q68",
326 .brightness_down = P30_PREFIX "_Q69",
327 .brightness_get = "GPLV",
328 .display_set = "SDSP",
329 .display_get = "\\DNXT"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 {
Len Brown4be44fc2005-08-05 00:44:28 -0400332 .name = "S1x",
333 .mt_mled = "MLED",
334 .mled_status = "\\EMLE",
335 .mt_wled = "WLED",
336 .mt_lcd_switch = S1x_PREFIX "Q10",
337 .lcd_status = "\\PNOF",
338 .brightness_set = "SPLV",
339 .brightness_get = "GPLV"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 {
Len Brown4be44fc2005-08-05 00:44:28 -0400342 .name = "S2x",
343 .mt_mled = "MLED",
344 .mled_status = "\\MAIL",
345 .mt_lcd_switch = S2x_PREFIX "_Q10",
346 .lcd_status = "\\BKLI",
347 .brightness_up = S2x_PREFIX "_Q0B",
348 .brightness_down = S2x_PREFIX "_Q0A"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 {
Karol Kozimor42cb8912006-06-30 19:04:00 -0400351 .name = "W1N",
352 .mt_mled = "MLED",
353 .mt_wled = "WLED",
354 .mt_ledd = "SLCM",
355 .mt_lcd_switch = xxN_PREFIX "_Q10",
356 .lcd_status = "\\BKLT",
357 .brightness_set = "SPLV",
358 .brightness_get = "GPLV",
359 .display_set = "SDSP",
360 .display_get = "\\ADVG"},
361
362 {
Len Brown4be44fc2005-08-05 00:44:28 -0400363 .name = "xxN",
364 .mt_mled = "MLED",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/* WLED present, but not controlled by ACPI */
Len Brown4be44fc2005-08-05 00:44:28 -0400366 .mt_lcd_switch = xxN_PREFIX "_Q10",
367 .lcd_status = "\\BKLT",
368 .brightness_set = "SPLV",
369 .brightness_get = "GPLV",
370 .display_set = "SDSP",
371 .display_get = "\\ADVG"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372};
373
374/* procdir we use */
375static struct proc_dir_entry *asus_proc_dir;
376
377/*
378 * This header is made available to allow proper configuration given model,
379 * revision number , ... this info cannot go in struct asus_hotk because it is
380 * available before the hotk
381 */
382static struct acpi_table_header *asus_info;
383
384/* The actual device the driver binds to */
385static struct asus_hotk *hotk;
386
387/*
388 * The hotkey driver declaration
389 */
390static int asus_hotk_add(struct acpi_device *device);
391static int asus_hotk_remove(struct acpi_device *device, int type);
392static struct acpi_driver asus_hotk_driver = {
Len Brown4be44fc2005-08-05 00:44:28 -0400393 .name = ACPI_HOTK_NAME,
394 .class = ACPI_HOTK_CLASS,
395 .ids = ACPI_HOTK_HID,
396 .ops = {
397 .add = asus_hotk_add,
398 .remove = asus_hotk_remove,
399 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400};
401
402/*
403 * This function evaluates an ACPI method, given an int as parameter, the
404 * method is searched within the scope of the handle, can be NULL. The output
405 * of the method is written is output, which can also be NULL
406 *
407 * returns 1 if write is successful, 0 else.
408 */
409static int write_acpi_int(acpi_handle handle, const char *method, int val,
410 struct acpi_buffer *output)
411{
412 struct acpi_object_list params; //list of input parameters (an int here)
413 union acpi_object in_obj; //the only param we use
414 acpi_status status;
415
416 params.count = 1;
417 params.pointer = &in_obj;
418 in_obj.type = ACPI_TYPE_INTEGER;
419 in_obj.integer.value = val;
420
Len Brown4be44fc2005-08-05 00:44:28 -0400421 status = acpi_evaluate_object(handle, (char *)method, &params, output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return (status == AE_OK);
423}
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425static int read_acpi_int(acpi_handle handle, const char *method, int *val)
426{
427 struct acpi_buffer output;
428 union acpi_object out_obj;
429 acpi_status status;
430
431 output.length = sizeof(out_obj);
432 output.pointer = &out_obj;
433
Len Brown4be44fc2005-08-05 00:44:28 -0400434 status = acpi_evaluate_object(handle, (char *)method, NULL, &output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 *val = out_obj.integer.value;
436 return (status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER);
437}
438
439/*
440 * We write our info in page, we begin at offset off and cannot write more
441 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
442 * number of bytes written in page
443 */
444static int
445proc_read_info(char *page, char **start, off_t off, int count, int *eof,
Len Brown4be44fc2005-08-05 00:44:28 -0400446 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 int len = 0;
449 int temp;
450 char buf[16]; //enough for all info
451 /*
452 * We use the easy way, we don't care of off and count, so we don't set eof
453 * to 1
454 */
455
456 len += sprintf(page, ACPI_HOTK_NAME " " ASUS_ACPI_VERSION "\n");
Len Brown4be44fc2005-08-05 00:44:28 -0400457 len += sprintf(page + len, "Model reference : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 hotk->methods->name);
459 /*
460 * The SFUN method probably allows the original driver to get the list
461 * of features supported by a given model. For now, 0x0100 or 0x0800
462 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
463 * The significance of others is yet to be found.
464 */
465 if (read_acpi_int(hotk->handle, "SFUN", &temp))
Len Brown4be44fc2005-08-05 00:44:28 -0400466 len +=
467 sprintf(page + len, "SFUN value : 0x%04x\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /*
469 * Another value for userspace: the ASYM method returns 0x02 for
470 * battery low and 0x04 for battery critical, its readings tend to be
471 * more accurate than those provided by _BST.
472 * Note: since not all the laptops provide this method, errors are
473 * silently ignored.
474 */
475 if (read_acpi_int(hotk->handle, "ASYM", &temp))
Len Brown4be44fc2005-08-05 00:44:28 -0400476 len +=
477 sprintf(page + len, "ASYM value : 0x%04x\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (asus_info) {
479 snprintf(buf, 16, "%d", asus_info->length);
480 len += sprintf(page + len, "DSDT length : %s\n", buf);
481 snprintf(buf, 16, "%d", asus_info->checksum);
482 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
483 snprintf(buf, 16, "%d", asus_info->revision);
484 len += sprintf(page + len, "DSDT revision : %s\n", buf);
485 snprintf(buf, 7, "%s", asus_info->oem_id);
486 len += sprintf(page + len, "OEM id : %s\n", buf);
487 snprintf(buf, 9, "%s", asus_info->oem_table_id);
488 len += sprintf(page + len, "OEM table id : %s\n", buf);
489 snprintf(buf, 16, "%x", asus_info->oem_revision);
490 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
491 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
492 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
493 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
494 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
495 }
496
497 return len;
498}
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500/*
501 * /proc handlers
502 * We write our info in page, we begin at offset off and cannot write more
503 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
504 * number of bytes written in page
505 */
506
507/* Generic LED functions */
Len Brown4be44fc2005-08-05 00:44:28 -0400508static int read_led(const char *ledname, int ledmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 if (ledname) {
511 int led_status;
512
513 if (read_acpi_int(NULL, ledname, &led_status))
514 return led_status;
515 else
516 printk(KERN_WARNING "Asus ACPI: Error reading LED "
517 "status\n");
518 }
519 return (hotk->status & ledmask) ? 1 : 0;
520}
521
Len Brown4be44fc2005-08-05 00:44:28 -0400522static int parse_arg(const char __user * buf, unsigned long count, int *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 char s[32];
525 if (!count)
526 return 0;
527 if (count > 31)
528 return -EINVAL;
529 if (copy_from_user(s, buf, count))
530 return -EFAULT;
531 s[count] = 0;
532 if (sscanf(s, "%i", val) != 1)
533 return -EINVAL;
534 return count;
535}
536
537/* FIXME: kill extraneous args so it can be called independently */
538static int
Len Brown4be44fc2005-08-05 00:44:28 -0400539write_led(const char __user * buffer, unsigned long count,
540 char *ledname, int ledmask, int invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 int value;
543 int led_out = 0;
544
545 count = parse_arg(buffer, count, &value);
546 if (count > 0)
547 led_out = value ? 1 : 0;
548
549 hotk->status =
550 (led_out) ? (hotk->status | ledmask) : (hotk->status & ~ledmask);
551
Len Brown4be44fc2005-08-05 00:44:28 -0400552 if (invert) /* invert target value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 led_out = !led_out & 0x1;
554
555 if (!write_acpi_int(hotk->handle, ledname, led_out, NULL))
Len Brown4be44fc2005-08-05 00:44:28 -0400556 printk(KERN_WARNING "Asus ACPI: LED (%s) write failed\n",
557 ledname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 return count;
560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562/*
563 * Proc handlers for MLED
564 */
565static int
566proc_read_mled(char *page, char **start, off_t off, int count, int *eof,
567 void *data)
568{
Len Brown4be44fc2005-08-05 00:44:28 -0400569 return sprintf(page, "%d\n",
570 read_led(hotk->methods->mled_status, MLED_ON));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573static int
Len Brown4be44fc2005-08-05 00:44:28 -0400574proc_write_mled(struct file *file, const char __user * buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 unsigned long count, void *data)
576{
577 return write_led(buffer, count, hotk->methods->mt_mled, MLED_ON, 1);
578}
579
580/*
Karol Kozimor42cb8912006-06-30 19:04:00 -0400581 * Proc handlers for LED display
582 */
583static int
584proc_read_ledd(char *page, char **start, off_t off, int count, int *eof,
585 void *data)
586{
587 return sprintf(page, "0x%08x\n", hotk->ledd_status);
588}
589
590static int
591proc_write_ledd(struct file *file, const char __user * buffer,
592 unsigned long count, void *data)
593{
594 int value;
595
596 count = parse_arg(buffer, count, &value);
597 if (count > 0) {
598 if (!write_acpi_int
599 (hotk->handle, hotk->methods->mt_ledd, value, NULL))
600 printk(KERN_WARNING
601 "Asus ACPI: LED display write failed\n");
602 else
603 hotk->ledd_status = (u32) value;
604 } else if (count < 0)
605 printk(KERN_WARNING "Asus ACPI: Error reading user input\n");
606
607 return count;
608}
609
610/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * Proc handlers for WLED
612 */
613static int
614proc_read_wled(char *page, char **start, off_t off, int count, int *eof,
615 void *data)
616{
Len Brown4be44fc2005-08-05 00:44:28 -0400617 return sprintf(page, "%d\n",
618 read_led(hotk->methods->wled_status, WLED_ON));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
621static int
Len Brown4be44fc2005-08-05 00:44:28 -0400622proc_write_wled(struct file *file, const char __user * buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 unsigned long count, void *data)
624{
625 return write_led(buffer, count, hotk->methods->mt_wled, WLED_ON, 0);
626}
627
628/*
629 * Proc handlers for TLED
630 */
631static int
632proc_read_tled(char *page, char **start, off_t off, int count, int *eof,
633 void *data)
634{
Len Brown4be44fc2005-08-05 00:44:28 -0400635 return sprintf(page, "%d\n",
636 read_led(hotk->methods->tled_status, TLED_ON));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639static int
Len Brown4be44fc2005-08-05 00:44:28 -0400640proc_write_tled(struct file *file, const char __user * buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 unsigned long count, void *data)
642{
643 return write_led(buffer, count, hotk->methods->mt_tled, TLED_ON, 0);
644}
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646static int get_lcd_state(void)
647{
648 int lcd = 0;
649
650 if (hotk->model != L3H) {
Len Brown4be44fc2005-08-05 00:44:28 -0400651 /* We don't have to check anything if we are here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (!read_acpi_int(NULL, hotk->methods->lcd_status, &lcd))
Len Brown4be44fc2005-08-05 00:44:28 -0400653 printk(KERN_WARNING
654 "Asus ACPI: Error reading LCD status\n");
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (hotk->model == L2D)
657 lcd = ~lcd;
Len Brown4be44fc2005-08-05 00:44:28 -0400658 } else { /* L3H and the like have to be handled differently */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 acpi_status status = 0;
660 struct acpi_object_list input;
661 union acpi_object mt_params[2];
662 struct acpi_buffer output;
663 union acpi_object out_obj;
Len Brown4be44fc2005-08-05 00:44:28 -0400664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 input.count = 2;
666 input.pointer = mt_params;
667 /* Note: the following values are partly guessed up, but
668 otherwise they seem to work */
669 mt_params[0].type = ACPI_TYPE_INTEGER;
670 mt_params[0].integer.value = 0x02;
671 mt_params[1].type = ACPI_TYPE_INTEGER;
672 mt_params[1].integer.value = 0x02;
673
674 output.length = sizeof(out_obj);
675 output.pointer = &out_obj;
Len Brown4be44fc2005-08-05 00:44:28 -0400676
677 status =
678 acpi_evaluate_object(NULL, hotk->methods->lcd_status,
679 &input, &output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (status != AE_OK)
681 return -1;
682 if (out_obj.type == ACPI_TYPE_INTEGER)
683 /* That's what the AML code does */
684 lcd = out_obj.integer.value >> 8;
685 }
Len Brown4be44fc2005-08-05 00:44:28 -0400686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return (lcd & 1);
688}
689
690static int set_lcd_state(int value)
691{
692 int lcd = 0;
693 acpi_status status = 0;
694
695 lcd = value ? 1 : 0;
696 if (lcd != get_lcd_state()) {
697 /* switch */
698 if (hotk->model != L3H) {
699 status =
Len Brown4be44fc2005-08-05 00:44:28 -0400700 acpi_evaluate_object(NULL,
701 hotk->methods->mt_lcd_switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 NULL, NULL);
Len Brown4be44fc2005-08-05 00:44:28 -0400703 } else { /* L3H and the like have to be handled differently */
704 if (!write_acpi_int
705 (hotk->handle, hotk->methods->mt_lcd_switch, 0x07,
706 NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 status = AE_ERROR;
708 /* L3H's AML executes EHK (0x07) upon Fn+F7 keypress,
709 the exact behaviour is simulated here */
710 }
711 if (ACPI_FAILURE(status))
712 printk(KERN_WARNING "Asus ACPI: Error switching LCD\n");
713 }
714 return 0;
715
716}
717
718static int
719proc_read_lcd(char *page, char **start, off_t off, int count, int *eof,
720 void *data)
721{
722 return sprintf(page, "%d\n", get_lcd_state());
723}
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725static int
Len Brown4be44fc2005-08-05 00:44:28 -0400726proc_write_lcd(struct file *file, const char __user * buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 unsigned long count, void *data)
728{
729 int value;
Len Brown4be44fc2005-08-05 00:44:28 -0400730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 count = parse_arg(buffer, count, &value);
732 if (count > 0)
733 set_lcd_state(value);
734 return count;
735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737static int read_brightness(void)
738{
739 int value;
Len Brown4be44fc2005-08-05 00:44:28 -0400740
741 if (hotk->methods->brightness_get) { /* SPLV/GPLV laptop */
742 if (!read_acpi_int(hotk->handle, hotk->methods->brightness_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 &value))
Len Brown4be44fc2005-08-05 00:44:28 -0400744 printk(KERN_WARNING
745 "Asus ACPI: Error reading brightness\n");
746 } else if (hotk->methods->brightness_status) { /* For D1 for example */
747 if (!read_acpi_int(NULL, hotk->methods->brightness_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 &value))
Len Brown4be44fc2005-08-05 00:44:28 -0400749 printk(KERN_WARNING
750 "Asus ACPI: Error reading brightness\n");
751 } else /* No GPLV method */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 value = hotk->brightness;
753 return value;
754}
755
756/*
757 * Change the brightness level
758 */
759static void set_brightness(int value)
760{
761 acpi_status status = 0;
762
763 /* SPLV laptop */
Len Brown4be44fc2005-08-05 00:44:28 -0400764 if (hotk->methods->brightness_set) {
765 if (!write_acpi_int(hotk->handle, hotk->methods->brightness_set,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 value, NULL))
Len Brown4be44fc2005-08-05 00:44:28 -0400767 printk(KERN_WARNING
768 "Asus ACPI: Error changing brightness\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return;
770 }
771
772 /* No SPLV method if we are here, act as appropriate */
773 value -= read_brightness();
774 while (value != 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400775 status = acpi_evaluate_object(NULL, (value > 0) ?
776 hotk->methods->brightness_up :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 hotk->methods->brightness_down,
778 NULL, NULL);
779 (value > 0) ? value-- : value++;
780 if (ACPI_FAILURE(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400781 printk(KERN_WARNING
782 "Asus ACPI: Error changing brightness\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784 return;
785}
786
787static int
788proc_read_brn(char *page, char **start, off_t off, int count, int *eof,
789 void *data)
790{
791 return sprintf(page, "%d\n", read_brightness());
792}
793
794static int
Len Brown4be44fc2005-08-05 00:44:28 -0400795proc_write_brn(struct file *file, const char __user * buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 unsigned long count, void *data)
797{
798 int value;
799
800 count = parse_arg(buffer, count, &value);
801 if (count > 0) {
802 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400803 /* 0 <= value <= 15 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 set_brightness(value);
805 } else if (count < 0) {
806 printk(KERN_WARNING "Asus ACPI: Error reading user input\n");
807 }
808
809 return count;
810}
811
812static void set_display(int value)
813{
814 /* no sanity check needed for now */
Len Brown4be44fc2005-08-05 00:44:28 -0400815 if (!write_acpi_int(hotk->handle, hotk->methods->display_set,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 value, NULL))
817 printk(KERN_WARNING "Asus ACPI: Error setting display\n");
818 return;
819}
820
821/*
822 * Now, *this* one could be more user-friendly, but so far, no-one has
823 * complained. The significance of bits is the same as in proc_write_disp()
824 */
825static int
826proc_read_disp(char *page, char **start, off_t off, int count, int *eof,
Len Brown4be44fc2005-08-05 00:44:28 -0400827 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 int value = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (!read_acpi_int(hotk->handle, hotk->methods->display_get, &value))
Len Brown4be44fc2005-08-05 00:44:28 -0400832 printk(KERN_WARNING
833 "Asus ACPI: Error reading display status\n");
834 value &= 0x07; /* needed for some models, shouldn't hurt others */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return sprintf(page, "%d\n", value);
836}
837
838/*
839 * Experimental support for display switching. As of now: 1 should activate
840 * the LCD output, 2 should do for CRT, and 4 for TV-Out. Any combination
841 * (bitwise) of these will suffice. I never actually tested 3 displays hooked up
842 * simultaneously, so be warned. See the acpi4asus README for more info.
843 */
844static int
Len Brown4be44fc2005-08-05 00:44:28 -0400845proc_write_disp(struct file *file, const char __user * buffer,
846 unsigned long count, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 int value;
849
850 count = parse_arg(buffer, count, &value);
851 if (count > 0)
852 set_display(value);
853 else if (count < 0)
854 printk(KERN_WARNING "Asus ACPI: Error reading user input\n");
855
856 return count;
857}
858
Len Brown4be44fc2005-08-05 00:44:28 -0400859typedef int (proc_readfunc) (char *page, char **start, off_t off, int count,
860 int *eof, void *data);
861typedef int (proc_writefunc) (struct file * file, const char __user * buffer,
862 unsigned long count, void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864static int
Bjorn Helgaas200739c2006-03-28 17:04:00 -0500865asus_proc_add(char *name, proc_writefunc * writefunc,
Len Brown4be44fc2005-08-05 00:44:28 -0400866 proc_readfunc * readfunc, mode_t mode,
867 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Len Brown4be44fc2005-08-05 00:44:28 -0400869 struct proc_dir_entry *proc =
870 create_proc_entry(name, mode, acpi_device_dir(device));
871 if (!proc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 printk(KERN_WARNING " Unable to create %s fs entry\n", name);
873 return -1;
874 }
875 proc->write_proc = writefunc;
876 proc->read_proc = readfunc;
877 proc->data = acpi_driver_data(device);
878 proc->owner = THIS_MODULE;
879 proc->uid = asus_uid;
880 proc->gid = asus_gid;
881 return 0;
882}
883
Bjorn Helgaas200739c2006-03-28 17:04:00 -0500884static int asus_hotk_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 struct proc_dir_entry *proc;
887 mode_t mode;
Len Brown4be44fc2005-08-05 00:44:28 -0400888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 /*
890 * If parameter uid or gid is not changed, keep the default setting for
891 * our proc entries (-rw-rw-rw-) else, it means we care about security,
892 * and then set to -rw-rw----
893 */
894
Len Brown4be44fc2005-08-05 00:44:28 -0400895 if ((asus_uid == 0) && (asus_gid == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 mode = S_IFREG | S_IRUGO | S_IWUGO;
897 } else {
898 mode = S_IFREG | S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP;
Karol Kozimoraea19aa2006-01-03 23:05:00 -0500899 printk(KERN_WARNING " asus_uid and asus_gid parameters are "
900 "deprecated, use chown and chmod instead!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902
903 acpi_device_dir(device) = asus_proc_dir;
904 if (!acpi_device_dir(device))
905 return -ENODEV;
906
907 proc = create_proc_entry(PROC_INFO, mode, acpi_device_dir(device));
908 if (proc) {
909 proc->read_proc = proc_read_info;
910 proc->data = acpi_driver_data(device);
911 proc->owner = THIS_MODULE;
912 proc->uid = asus_uid;
913 proc->gid = asus_gid;
914 } else {
915 printk(KERN_WARNING " Unable to create " PROC_INFO
916 " fs entry\n");
917 }
918
919 if (hotk->methods->mt_wled) {
Len Brown4be44fc2005-08-05 00:44:28 -0400920 asus_proc_add(PROC_WLED, &proc_write_wled, &proc_read_wled,
921 mode, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923
Karol Kozimor42cb8912006-06-30 19:04:00 -0400924 if (hotk->methods->mt_ledd) {
925 asus_proc_add(PROC_LEDD, &proc_write_ledd, &proc_read_ledd,
926 mode, device);
927 }
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (hotk->methods->mt_mled) {
Len Brown4be44fc2005-08-05 00:44:28 -0400930 asus_proc_add(PROC_MLED, &proc_write_mled, &proc_read_mled,
931 mode, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933
934 if (hotk->methods->mt_tled) {
Len Brown4be44fc2005-08-05 00:44:28 -0400935 asus_proc_add(PROC_TLED, &proc_write_tled, &proc_read_tled,
936 mode, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
939 /*
940 * We need both read node and write method as LCD switch is also accessible
941 * from keyboard
942 */
943 if (hotk->methods->mt_lcd_switch && hotk->methods->lcd_status) {
Len Brown4be44fc2005-08-05 00:44:28 -0400944 asus_proc_add(PROC_LCD, &proc_write_lcd, &proc_read_lcd, mode,
945 device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Len Brown4be44fc2005-08-05 00:44:28 -0400947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if ((hotk->methods->brightness_up && hotk->methods->brightness_down) ||
949 (hotk->methods->brightness_get && hotk->methods->brightness_set)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400950 asus_proc_add(PROC_BRN, &proc_write_brn, &proc_read_brn, mode,
951 device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
953
954 if (hotk->methods->display_set) {
Len Brown4be44fc2005-08-05 00:44:28 -0400955 asus_proc_add(PROC_DISP, &proc_write_disp, &proc_read_disp,
956 mode, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958
959 return 0;
960}
961
Len Brown4be44fc2005-08-05 00:44:28 -0400962static int asus_hotk_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Len Brown4be44fc2005-08-05 00:44:28 -0400964 if (acpi_device_dir(device)) {
965 remove_proc_entry(PROC_INFO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (hotk->methods->mt_wled)
Len Brown4be44fc2005-08-05 00:44:28 -0400967 remove_proc_entry(PROC_WLED, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (hotk->methods->mt_mled)
Len Brown4be44fc2005-08-05 00:44:28 -0400969 remove_proc_entry(PROC_MLED, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (hotk->methods->mt_tled)
Len Brown4be44fc2005-08-05 00:44:28 -0400971 remove_proc_entry(PROC_TLED, acpi_device_dir(device));
Karol Kozimor42cb8912006-06-30 19:04:00 -0400972 if (hotk->methods->mt_ledd)
973 remove_proc_entry(PROC_LEDD, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (hotk->methods->mt_lcd_switch && hotk->methods->lcd_status)
975 remove_proc_entry(PROC_LCD, acpi_device_dir(device));
Len Brown4be44fc2005-08-05 00:44:28 -0400976 if ((hotk->methods->brightness_up
977 && hotk->methods->brightness_down)
978 || (hotk->methods->brightness_get
979 && hotk->methods->brightness_set))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 remove_proc_entry(PROC_BRN, acpi_device_dir(device));
981 if (hotk->methods->display_set)
982 remove_proc_entry(PROC_DISP, acpi_device_dir(device));
983 }
984 return 0;
985}
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
988{
Len Brown4be44fc2005-08-05 00:44:28 -0400989 /* TODO Find a better way to handle events count. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (!hotk)
991 return;
992
993 if ((event & ~((u32) BR_UP)) < 16) {
994 hotk->brightness = (event & ~((u32) BR_UP));
Len Brown4be44fc2005-08-05 00:44:28 -0400995 } else if ((event & ~((u32) BR_DOWN)) < 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 hotk->brightness = (event & ~((u32) BR_DOWN));
997 }
998
999 acpi_bus_generate_event(hotk->device, event,
1000 hotk->event_count[event % 128]++);
1001
1002 return;
1003}
1004
1005/*
1006 * This function is used to initialize the hotk with right values. In this
1007 * method, we can make all the detection we want, and modify the hotk struct
1008 */
Bjorn Helgaas200739c2006-03-28 17:04:00 -05001009static int asus_hotk_get_info(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1012 struct acpi_buffer dsdt = { ACPI_ALLOCATE_BUFFER, NULL };
1013 union acpi_object *model = NULL;
1014 int bsts_result;
1015 acpi_status status;
1016
1017 /*
1018 * Get DSDT headers early enough to allow for differentiating between
1019 * models, but late enough to allow acpi_bus_register_driver() to fail
1020 * before doing anything ACPI-specific. Should we encounter a machine,
1021 * which needs special handling (i.e. its hotkey device has a different
1022 * HID), this bit will be moved. A global variable asus_info contains
1023 * the DSDT header.
1024 */
Bob Mooreb229cf92006-04-21 17:15:00 -04001025 status = acpi_get_table(ACPI_TABLE_ID_DSDT, 1, &dsdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (ACPI_FAILURE(status))
1027 printk(KERN_WARNING " Couldn't get the DSDT table header\n");
1028 else
Len Brown4be44fc2005-08-05 00:44:28 -04001029 asus_info = (struct acpi_table_header *)dsdt.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 /* We have to write 0 on init this far for all ASUS models */
1032 if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
1033 printk(KERN_ERR " Hotkey initialization failed\n");
1034 return -ENODEV;
1035 }
1036
1037 /* This needs to be called for some laptops to init properly */
1038 if (!read_acpi_int(hotk->handle, "BSTS", &bsts_result))
1039 printk(KERN_WARNING " Error calling BSTS\n");
1040 else if (bsts_result)
Len Brown4be44fc2005-08-05 00:44:28 -04001041 printk(KERN_NOTICE " BSTS called, 0x%02x returned\n",
1042 bsts_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Karol Kozimorb697b532005-12-22 12:42:00 -05001044 /* This is unlikely with implicit return */
1045 if (buffer.pointer == NULL)
1046 return -EINVAL;
1047
Karol Kozimora170a532006-06-30 19:03:00 -04001048 model = (union acpi_object *)buffer.pointer;
Karol Kozimorb697b532005-12-22 12:42:00 -05001049 /*
1050 * Samsung P30 has a device with a valid _HID whose INIT does not
1051 * return anything. It used to be possible to catch this exception,
1052 * but the implicit return code will now happily confuse the
1053 * driver. We assume that every ACPI_TYPE_STRING is a valid model
1054 * identifier but it's still possible to get completely bogus data.
1055 */
1056 if (model->type == ACPI_TYPE_STRING) {
Karol Kozimora170a532006-06-30 19:03:00 -04001057 printk(KERN_NOTICE " %s model detected, ",
1058 model->string.pointer);
Karol Kozimorb697b532005-12-22 12:42:00 -05001059 } else {
Len Brown4be44fc2005-08-05 00:44:28 -04001060 if (asus_info && /* Samsung P30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 strncmp(asus_info->oem_table_id, "ODEM", 4) == 0) {
1062 hotk->model = P30;
Len Brown4be44fc2005-08-05 00:44:28 -04001063 printk(KERN_NOTICE
1064 " Samsung P30 detected, supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 } else {
1066 hotk->model = M2E;
1067 printk(KERN_WARNING " no string returned by INIT\n");
1068 printk(KERN_WARNING " trying default values, supply "
1069 "the developers with your DSDT\n");
1070 }
1071 hotk->methods = &model_conf[hotk->model];
Karol Kozimora170a532006-06-30 19:03:00 -04001072
Len Brown02438d82006-06-30 03:19:10 -04001073 kfree(model);
Len Brown4be44fc2005-08-05 00:44:28 -04001074
Karol Kozimorb697b532005-12-22 12:42:00 -05001075 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077
1078 hotk->model = END_MODEL;
1079 if (strncmp(model->string.pointer, "L3D", 3) == 0)
1080 hotk->model = L3D;
1081 else if (strncmp(model->string.pointer, "L3H", 3) == 0 ||
1082 strncmp(model->string.pointer, "L2E", 3) == 0)
1083 hotk->model = L3H;
1084 else if (strncmp(model->string.pointer, "L3", 2) == 0 ||
1085 strncmp(model->string.pointer, "L2B", 3) == 0)
1086 hotk->model = L3C;
1087 else if (strncmp(model->string.pointer, "L8L", 3) == 0)
1088 hotk->model = L8L;
1089 else if (strncmp(model->string.pointer, "L4R", 3) == 0)
1090 hotk->model = L4R;
Karol Kozimorc067a782006-06-30 19:05:00 -04001091 else if (strncmp(model->string.pointer, "M6N", 3) == 0 ||
1092 strncmp(model->string.pointer, "W3N", 3) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 hotk->model = M6N;
Karol Kozimored2cb072006-06-30 19:03:00 -04001094 else if (strncmp(model->string.pointer, "M6R", 3) == 0 ||
1095 strncmp(model->string.pointer, "A3G", 3) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 hotk->model = M6R;
1097 else if (strncmp(model->string.pointer, "M2N", 3) == 0 ||
1098 strncmp(model->string.pointer, "M3N", 3) == 0 ||
1099 strncmp(model->string.pointer, "M5N", 3) == 0 ||
1100 strncmp(model->string.pointer, "M6N", 3) == 0 ||
1101 strncmp(model->string.pointer, "S1N", 3) == 0 ||
Karol Kozimor42cb8912006-06-30 19:04:00 -04001102 strncmp(model->string.pointer, "S5N", 3) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 hotk->model = xxN;
1104 else if (strncmp(model->string.pointer, "M1", 2) == 0)
1105 hotk->model = M1A;
1106 else if (strncmp(model->string.pointer, "M2", 2) == 0 ||
1107 strncmp(model->string.pointer, "L4E", 3) == 0)
1108 hotk->model = M2E;
1109 else if (strncmp(model->string.pointer, "L2", 2) == 0)
1110 hotk->model = L2D;
1111 else if (strncmp(model->string.pointer, "L8", 2) == 0)
1112 hotk->model = S1x;
1113 else if (strncmp(model->string.pointer, "D1", 2) == 0)
1114 hotk->model = D1x;
1115 else if (strncmp(model->string.pointer, "A1", 2) == 0)
1116 hotk->model = A1x;
1117 else if (strncmp(model->string.pointer, "A2", 2) == 0)
1118 hotk->model = A2x;
1119 else if (strncmp(model->string.pointer, "J1", 2) == 0)
1120 hotk->model = S2x;
1121 else if (strncmp(model->string.pointer, "L5", 2) == 0)
1122 hotk->model = L5x;
Karol Kozimorf78c5892006-06-30 19:06:00 -04001123 else if (strncmp(model->string.pointer, "A4G", 3) == 0)
1124 hotk->model = A4G;
Karol Kozimor42cb8912006-06-30 19:04:00 -04001125 else if (strncmp(model->string.pointer, "W1N", 3) == 0)
1126 hotk->model = W1N;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 if (hotk->model == END_MODEL) {
1129 printk("unsupported, trying default values, supply the "
1130 "developers with your DSDT\n");
1131 hotk->model = M2E;
1132 } else {
1133 printk("supported\n");
1134 }
1135
1136 hotk->methods = &model_conf[hotk->model];
1137
1138 /* Sort of per-model blacklist */
1139 if (strncmp(model->string.pointer, "L2B", 3) == 0)
Len Brown4be44fc2005-08-05 00:44:28 -04001140 hotk->methods->lcd_status = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 /* L2B is similar enough to L3C to use its settings, with this only
1142 exception */
Karol Kozimored2cb072006-06-30 19:03:00 -04001143 else if (strncmp(model->string.pointer, "A3G", 3) == 0)
1144 hotk->methods->lcd_status = "\\BLFG";
1145 /* A3G is like M6R */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 else if (strncmp(model->string.pointer, "S5N", 3) == 0 ||
Karol Kozimorc067a782006-06-30 19:05:00 -04001147 strncmp(model->string.pointer, "M5N", 3) == 0 ||
1148 strncmp(model->string.pointer, "W3N", 3) == 0)
Len Brown4be44fc2005-08-05 00:44:28 -04001149 hotk->methods->mt_mled = NULL;
Karol Kozimorc067a782006-06-30 19:05:00 -04001150 /* S5N, M5N and W3N have no MLED */
Karol Kozimor42cb8912006-06-30 19:04:00 -04001151 else if (strncmp(model->string.pointer, "M2N", 3) == 0)
Len Brown4be44fc2005-08-05 00:44:28 -04001152 hotk->methods->mt_wled = "WLED";
Karol Kozimor42cb8912006-06-30 19:04:00 -04001153 /* M2N has a usable WLED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 else if (asus_info) {
1155 if (strncmp(asus_info->oem_table_id, "L1", 2) == 0)
1156 hotk->methods->mled_status = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001157 /* S1300A reports L84F, but L1400B too, account for that */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159
Len Brown02438d82006-06-30 03:19:10 -04001160 kfree(model);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 return AE_OK;
1163}
1164
Bjorn Helgaas200739c2006-03-28 17:04:00 -05001165static int asus_hotk_check(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 int result = 0;
1168
1169 result = acpi_bus_get_status(hotk->device);
1170 if (result)
1171 return result;
1172
1173 if (hotk->device->status.present) {
1174 result = asus_hotk_get_info();
1175 } else {
1176 printk(KERN_ERR " Hotkey device not present, aborting\n");
1177 return -EINVAL;
1178 }
1179
1180 return result;
1181}
1182
Bjorn Helgaas578b3332006-03-28 17:04:00 -05001183static int asus_hotk_found;
1184
Bjorn Helgaas200739c2006-03-28 17:04:00 -05001185static int asus_hotk_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
1187 acpi_status status = AE_OK;
1188 int result;
1189
1190 if (!device)
1191 return -EINVAL;
1192
1193 printk(KERN_NOTICE "Asus Laptop ACPI Extras version %s\n",
1194 ASUS_ACPI_VERSION);
1195
1196 hotk =
Len Brown4be44fc2005-08-05 00:44:28 -04001197 (struct asus_hotk *)kmalloc(sizeof(struct asus_hotk), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (!hotk)
1199 return -ENOMEM;
1200 memset(hotk, 0, sizeof(struct asus_hotk));
1201
1202 hotk->handle = device->handle;
1203 strcpy(acpi_device_name(device), ACPI_HOTK_DEVICE_NAME);
1204 strcpy(acpi_device_class(device), ACPI_HOTK_CLASS);
1205 acpi_driver_data(device) = hotk;
1206 hotk->device = device;
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 result = asus_hotk_check();
1209 if (result)
1210 goto end;
1211
1212 result = asus_hotk_add_fs(device);
1213 if (result)
1214 goto end;
1215
1216 /*
1217 * We install the handler, it will receive the hotk in parameter, so, we
1218 * could add other data to the hotk struct
1219 */
1220 status = acpi_install_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
1221 asus_hotk_notify, hotk);
1222 if (ACPI_FAILURE(status))
1223 printk(KERN_ERR " Error installing notify handler\n");
1224
1225 /* For laptops without GPLV: init the hotk->brightness value */
Len Brown4be44fc2005-08-05 00:44:28 -04001226 if ((!hotk->methods->brightness_get)
1227 && (!hotk->methods->brightness_status)
Karol Kozimora170a532006-06-30 19:03:00 -04001228 && (hotk->methods->brightness_up && hotk->methods->brightness_down)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001229 status =
1230 acpi_evaluate_object(NULL, hotk->methods->brightness_down,
1231 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (ACPI_FAILURE(status))
1233 printk(KERN_WARNING " Error changing brightness\n");
1234 else {
Len Brown4be44fc2005-08-05 00:44:28 -04001235 status =
1236 acpi_evaluate_object(NULL,
1237 hotk->methods->brightness_up,
1238 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (ACPI_FAILURE(status))
Len Brown4be44fc2005-08-05 00:44:28 -04001240 printk(KERN_WARNING " Strange, error changing"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 " brightness\n");
1242 }
1243 }
1244
Bjorn Helgaas578b3332006-03-28 17:04:00 -05001245 asus_hotk_found = 1;
1246
Karol Kozimor42cb8912006-06-30 19:04:00 -04001247 /* LED display is off by default */
1248 hotk->ledd_status = 0xFFF;
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 end:
1251 if (result) {
1252 kfree(hotk);
1253 }
1254
1255 return result;
1256}
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258static int asus_hotk_remove(struct acpi_device *device, int type)
1259{
1260 acpi_status status = 0;
1261
1262 if (!device || !acpi_driver_data(device))
1263 return -EINVAL;
1264
1265 status = acpi_remove_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
1266 asus_hotk_notify);
1267 if (ACPI_FAILURE(status))
1268 printk(KERN_ERR "Asus ACPI: Error removing notify handler\n");
1269
1270 asus_hotk_remove_fs(device);
1271
1272 kfree(hotk);
1273
1274 return 0;
1275}
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277static int __init asus_acpi_init(void)
1278{
1279 int result;
1280
1281 if (acpi_disabled)
1282 return -ENODEV;
1283
Len Brown4be44fc2005-08-05 00:44:28 -04001284 if (!acpi_specific_hotkey_enabled) {
Luming Yufb9802f2005-03-18 18:03:45 -05001285 printk(KERN_ERR "Using generic hotkey driver\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001286 return -ENODEV;
Luming Yufb9802f2005-03-18 18:03:45 -05001287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 asus_proc_dir = proc_mkdir(PROC_ASUS, acpi_root_dir);
1289 if (!asus_proc_dir) {
1290 printk(KERN_ERR "Asus ACPI: Unable to create /proc entry\n");
1291 return -ENODEV;
1292 }
1293 asus_proc_dir->owner = THIS_MODULE;
1294
1295 result = acpi_bus_register_driver(&asus_hotk_driver);
Bjorn Helgaas578b3332006-03-28 17:04:00 -05001296 if (result < 0) {
1297 remove_proc_entry(PROC_ASUS, acpi_root_dir);
Andrew Morton5e7d8812006-06-24 19:36:00 -04001298 return result;
Bjorn Helgaas578b3332006-03-28 17:04:00 -05001299 }
1300
1301 /*
1302 * This is a bit of a kludge. We only want this module loaded
1303 * for ASUS systems, but there's currently no way to probe the
1304 * ACPI namespace for ASUS HIDs. So we just return failure if
1305 * we didn't find one, which will cause the module to be
1306 * unloaded.
1307 */
1308 if (!asus_hotk_found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 acpi_bus_unregister_driver(&asus_hotk_driver);
1310 remove_proc_entry(PROC_ASUS, acpi_root_dir);
Andrew Mortonebd5f2c2006-05-13 22:56:00 -04001311 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313
1314 return 0;
1315}
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317static void __exit asus_acpi_exit(void)
1318{
1319 acpi_bus_unregister_driver(&asus_hotk_driver);
1320 remove_proc_entry(PROC_ASUS, acpi_root_dir);
1321
Len Brown02438d82006-06-30 03:19:10 -04001322 kfree(asus_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 return;
1325}
1326
1327module_init(asus_acpi_init);
1328module_exit(asus_acpi_exit);