]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/platform/x86/asus-laptop.c
parisc: sba_iommu: fix build bug when CONFIG_PARISC_AGP=y
[mirror_ubuntu-artful-kernel.git] / drivers / platform / x86 / asus-laptop.c
CommitLineData
85091b71
CC
1/*
2 * asus-laptop.c - Asus Laptop Support
3 *
4 *
5 * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
8ec555c2 6 * Copyright (C) 2006-2007 Corentin Chary
85091b71
CC
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 *
23 * The development page for this driver is located at
24 * http://sourceforge.net/projects/acpi4asus/
25 *
26 * Credits:
27 * Pontus Fuchs - Helper functions, cleanup
28 * Johann Wiesner - Small compile fixes
29 * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
30 * Eric Burghard - LED display support for W1N
31 * Josh Green - Light Sens support
32 * Thomas Tuttle - His first patch for led support was very helpfull
e539c2f6 33 * Sam Lin - GPS support
85091b71
CC
34 */
35
85091b71
CC
36#include <linux/kernel.h>
37#include <linux/module.h>
38#include <linux/init.h>
39#include <linux/types.h>
40#include <linux/err.h>
41#include <linux/proc_fs.h>
6b7091e7
CC
42#include <linux/backlight.h>
43#include <linux/fb.h>
be18cdab 44#include <linux/leds.h>
85091b71
CC
45#include <linux/platform_device.h>
46#include <acpi/acpi_drivers.h>
47#include <acpi/acpi_bus.h>
48#include <asm/uaccess.h>
034ce90a 49#include <linux/input.h>
85091b71 50
f3985327 51#define ASUS_LAPTOP_VERSION "0.42"
85091b71
CC
52
53#define ASUS_HOTK_NAME "Asus Laptop Support"
54#define ASUS_HOTK_CLASS "hotkey"
55#define ASUS_HOTK_DEVICE_NAME "Hotkey"
85091b71
CC
56#define ASUS_HOTK_FILE "asus-laptop"
57#define ASUS_HOTK_PREFIX "\\_SB.ATKD."
58
6b7091e7
CC
59/*
60 * Some events we use, same for all Asus
61 */
62#define ATKD_BR_UP 0x10
63#define ATKD_BR_DOWN 0x20
64#define ATKD_LCD_ON 0x33
65#define ATKD_LCD_OFF 0x34
66
4564de17
CC
67/*
68 * Known bits returned by \_SB.ATKD.HWRS
69 */
70#define WL_HWRS 0x80
71#define BT_HWRS 0x100
72
be18cdab
CC
73/*
74 * Flags for hotk status
4564de17 75 * WL_ON and BT_ON are also used for wireless_status()
be18cdab 76 */
4564de17
CC
77#define WL_ON 0x01 //internal Wifi
78#define BT_ON 0x02 //internal Bluetooth
be18cdab
CC
79#define MLED_ON 0x04 //mail LED
80#define TLED_ON 0x08 //touchpad LED
8def05fa
LB
81#define RLED_ON 0x10 //Record LED
82#define PLED_ON 0x20 //Phone LED
fdd8d080
CC
83#define GLED_ON 0x40 //Gaming LED
84#define LCD_ON 0x80 //LCD backlight
f3985327 85#define GPS_ON 0x100 //GPS
be18cdab 86
85091b71
CC
87#define ASUS_LOG ASUS_HOTK_FILE ": "
88#define ASUS_ERR KERN_ERR ASUS_LOG
89#define ASUS_WARNING KERN_WARNING ASUS_LOG
90#define ASUS_NOTICE KERN_NOTICE ASUS_LOG
91#define ASUS_INFO KERN_INFO ASUS_LOG
92#define ASUS_DEBUG KERN_DEBUG ASUS_LOG
93
94MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
95MODULE_DESCRIPTION(ASUS_HOTK_NAME);
96MODULE_LICENSE("GPL");
97
185e5af9
CC
98/* WAPF defines the behavior of the Fn+Fx wlan key
99 * The significance of values is yet to be found, but
100 * most of the time:
101 * 0x0 will do nothing
102 * 0x1 will allow to control the device with Fn+Fx key.
103 * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
104 * 0x5 like 0x1 or 0x4
105 * So, if something doesn't work as you want, just try other values =)
106 */
107static uint wapf = 1;
108module_param(wapf, uint, 0644);
109MODULE_PARM_DESC(wapf, "WAPF value");
110
85091b71
CC
111#define ASUS_HANDLE(object, paths...) \
112 static acpi_handle object##_handle = NULL; \
113 static char *object##_paths[] = { paths }
114
be18cdab
CC
115/* LED */
116ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
117ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
8def05fa
LB
118ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
119ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
fdd8d080 120ASUS_HANDLE(gled_set, ASUS_HOTK_PREFIX "GLED"); /* G1, G2 (probably) */
be18cdab 121
722ad971
CC
122/* LEDD */
123ASUS_HANDLE(ledd_set, ASUS_HOTK_PREFIX "SLCM");
124
4564de17
CC
125/* Bluetooth and WLAN
126 * WLED and BLED are not handled like other XLED, because in some dsdt
127 * they also control the WLAN/Bluetooth device.
128 */
129ASUS_HANDLE(wl_switch, ASUS_HOTK_PREFIX "WLED");
130ASUS_HANDLE(bt_switch, ASUS_HOTK_PREFIX "BLED");
8def05fa 131ASUS_HANDLE(wireless_status, ASUS_HOTK_PREFIX "RSTS"); /* All new models */
4564de17 132
6b7091e7
CC
133/* Brightness */
134ASUS_HANDLE(brightness_set, ASUS_HOTK_PREFIX "SPLV");
135ASUS_HANDLE(brightness_get, ASUS_HOTK_PREFIX "GPLV");
136
137/* Backlight */
8def05fa
LB
138ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
139 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
140 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
141 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
142 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
4d0b856e 143 "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
8def05fa
LB
144 "\\_SB.PCI0.PX40.Q10", /* S1x */
145 "\\Q10"); /* A2x, L2D, L3D, M2E */
6b7091e7 146
78127b4a
CC
147/* Display */
148ASUS_HANDLE(display_set, ASUS_HOTK_PREFIX "SDSP");
8def05fa
LB
149ASUS_HANDLE(display_get, "\\_SB.PCI0.P0P1.VGA.GETD", /* A6B, A6K A6R A7D F3JM L4R M6R A3G
150 M6A M6V VX-1 V6J V6V W3Z */
151 "\\_SB.PCI0.P0P2.VGA.GETD", /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V
f3985327 152 S5A M5A z33A W1Jc W2V G1 */
8def05fa
LB
153 "\\_SB.PCI0.P0P3.VGA.GETD", /* A6V A6Q */
154 "\\_SB.PCI0.P0PA.VGA.GETD", /* A6T, A6M */
155 "\\_SB.PCI0.PCI1.VGAC.NMAP", /* L3C */
156 "\\_SB.PCI0.VGA.GETD", /* Z96F */
157 "\\ACTD", /* A2D */
158 "\\ADVG", /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
159 "\\DNXT", /* P30 */
160 "\\INFB", /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
161 "\\SSTE"); /* A3F A6F A3N A3L M6N W3N W6A */
162
163ASUS_HANDLE(ls_switch, ASUS_HOTK_PREFIX "ALSC"); /* Z71A Z71V */
164ASUS_HANDLE(ls_level, ASUS_HOTK_PREFIX "ALSL"); /* Z71A Z71V */
8b857353 165
e539c2f6
CC
166/* GPS */
167/* R2H use different handle for GPS on/off */
f3985327
CC
168ASUS_HANDLE(gps_on, ASUS_HOTK_PREFIX "SDON"); /* R2H */
169ASUS_HANDLE(gps_off, ASUS_HOTK_PREFIX "SDOF"); /* R2H */
e539c2f6
CC
170ASUS_HANDLE(gps_status, ASUS_HOTK_PREFIX "GPST");
171
85091b71
CC
172/*
173 * This is the main structure, we can use it to store anything interesting
174 * about the hotk device
175 */
176struct asus_hotk {
8def05fa 177 char *name; //laptop name
85091b71
CC
178 struct acpi_device *device; //the device we are in
179 acpi_handle handle; //the handle of the hotk device
180 char status; //status of the hotk, for LEDs, ...
722ad971 181 u32 ledd_status; //status of the LED display
8def05fa
LB
182 u8 light_level; //light sensor level
183 u8 light_switch; //light sensor switch value
85091b71 184 u16 event_count[128]; //count for each event TODO make this better
034ce90a
CC
185 struct input_dev *inputdev;
186 u16 *keycode_map;
85091b71
CC
187};
188
189/*
190 * This header is made available to allow proper configuration given model,
191 * revision number , ... this info cannot go in struct asus_hotk because it is
192 * available before the hotk
193 */
194static struct acpi_table_header *asus_info;
195
196/* The actual device the driver binds to */
197static struct asus_hotk *hotk;
198
199/*
200 * The hotkey driver declaration
201 */
1ba90e3a
TR
202static const struct acpi_device_id asus_device_ids[] = {
203 {"ATK0100", 0},
204 {"", 0},
205};
206MODULE_DEVICE_TABLE(acpi, asus_device_ids);
207
85091b71
CC
208static int asus_hotk_add(struct acpi_device *device);
209static int asus_hotk_remove(struct acpi_device *device, int type);
210static struct acpi_driver asus_hotk_driver = {
211 .name = ASUS_HOTK_NAME,
212 .class = ASUS_HOTK_CLASS,
1ba90e3a 213 .ids = asus_device_ids,
85091b71
CC
214 .ops = {
215 .add = asus_hotk_add,
216 .remove = asus_hotk_remove,
217 },
218};
219
6b7091e7
CC
220/* The backlight device /sys/class/backlight */
221static struct backlight_device *asus_backlight_device;
222
223/*
224 * The backlight class declaration
225 */
226static int read_brightness(struct backlight_device *bd);
227static int update_bl_status(struct backlight_device *bd);
599a52d1 228static struct backlight_ops asusbl_ops = {
8def05fa
LB
229 .get_brightness = read_brightness,
230 .update_status = update_bl_status,
6b7091e7
CC
231};
232
be18cdab
CC
233/* These functions actually update the LED's, and are called from a
234 * workqueue. By doing this as separate work rather than when the LED
235 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
236 * potentially bad time, such as a timer interrupt. */
237static struct workqueue_struct *led_workqueue;
238
239#define ASUS_LED(object, ledname) \
240 static void object##_led_set(struct led_classdev *led_cdev, \
241 enum led_brightness value); \
242 static void object##_led_update(struct work_struct *ignored); \
243 static int object##_led_wk; \
f110ef58 244 static DECLARE_WORK(object##_led_work, object##_led_update); \
be18cdab 245 static struct led_classdev object##_led = { \
6c152bee 246 .name = "asus::" ledname, \
be18cdab
CC
247 .brightness_set = object##_led_set, \
248 }
249
250ASUS_LED(mled, "mail");
251ASUS_LED(tled, "touchpad");
252ASUS_LED(rled, "record");
253ASUS_LED(pled, "phone");
fdd8d080 254ASUS_LED(gled, "gaming");
be18cdab 255
034ce90a
CC
256struct key_entry {
257 char type;
258 u8 code;
259 u16 keycode;
260};
261
262enum { KE_KEY, KE_END };
263
264static struct key_entry asus_keymap[] = {
265 {KE_KEY, 0x30, KEY_VOLUMEUP},
266 {KE_KEY, 0x31, KEY_VOLUMEDOWN},
267 {KE_KEY, 0x32, KEY_MUTE},
268 {KE_KEY, 0x33, KEY_SWITCHVIDEOMODE},
269 {KE_KEY, 0x34, KEY_SWITCHVIDEOMODE},
270 {KE_KEY, 0x40, KEY_PREVIOUSSONG},
271 {KE_KEY, 0x41, KEY_NEXTSONG},
272 {KE_KEY, 0x43, KEY_STOP},
273 {KE_KEY, 0x45, KEY_PLAYPAUSE},
274 {KE_KEY, 0x50, KEY_EMAIL},
275 {KE_KEY, 0x51, KEY_WWW},
276 {KE_KEY, 0x5C, BTN_EXTRA}, /* Performance */
277 {KE_KEY, 0x5D, KEY_WLAN},
278 {KE_KEY, 0x61, KEY_SWITCHVIDEOMODE},
279 {KE_KEY, 0x6B, BTN_TOUCH}, /* Lock Mouse */
280 {KE_KEY, 0x82, KEY_CAMERA},
281 {KE_KEY, 0x8A, KEY_TV},
282 {KE_KEY, 0x95, KEY_MEDIA},
283 {KE_KEY, 0x99, KEY_PHONE},
284 {KE_END, 0},
285};
286
85091b71
CC
287/*
288 * This function evaluates an ACPI method, given an int as parameter, the
289 * method is searched within the scope of the handle, can be NULL. The output
290 * of the method is written is output, which can also be NULL
291 *
f8d1c94b 292 * returns 0 if write is successful, -1 else.
85091b71
CC
293 */
294static int write_acpi_int(acpi_handle handle, const char *method, int val,
295 struct acpi_buffer *output)
296{
297 struct acpi_object_list params; //list of input parameters (an int here)
298 union acpi_object in_obj; //the only param we use
299 acpi_status status;
300
f8d1c94b
CC
301 if (!handle)
302 return 0;
303
85091b71
CC
304 params.count = 1;
305 params.pointer = &in_obj;
306 in_obj.type = ACPI_TYPE_INTEGER;
307 in_obj.integer.value = val;
308
309 status = acpi_evaluate_object(handle, (char *)method, &params, output);
f8d1c94b
CC
310 if (status == AE_OK)
311 return 0;
312 else
313 return -1;
85091b71
CC
314}
315
8def05fa
LB
316static int read_wireless_status(int mask)
317{
27663c58 318 unsigned long long status;
9a816850 319 acpi_status rv = AE_OK;
4564de17
CC
320
321 if (!wireless_status_handle)
322 return (hotk->status & mask) ? 1 : 0;
323
9a816850
CC
324 rv = acpi_evaluate_integer(wireless_status_handle, NULL, NULL, &status);
325 if (ACPI_FAILURE(rv))
4564de17 326 printk(ASUS_WARNING "Error reading Wireless status\n");
9a816850
CC
327 else
328 return (status & mask) ? 1 : 0;
4564de17
CC
329
330 return (hotk->status & mask) ? 1 : 0;
331}
332
f3985327
CC
333static int read_gps_status(void)
334{
27663c58 335 unsigned long long status;
e539c2f6
CC
336 acpi_status rv = AE_OK;
337
338 rv = acpi_evaluate_integer(gps_status_handle, NULL, NULL, &status);
339 if (ACPI_FAILURE(rv))
f3985327 340 printk(ASUS_WARNING "Error reading GPS status\n");
e539c2f6 341 else
f3985327 342 return status ? 1 : 0;
e539c2f6 343
f3985327 344 return (hotk->status & GPS_ON) ? 1 : 0;
e539c2f6
CC
345}
346
be18cdab
CC
347/* Generic LED functions */
348static int read_status(int mask)
349{
4564de17
CC
350 /* There is a special method for both wireless devices */
351 if (mask == BT_ON || mask == WL_ON)
352 return read_wireless_status(mask);
f3985327
CC
353 else if (mask == GPS_ON)
354 return read_gps_status();
4564de17 355
be18cdab
CC
356 return (hotk->status & mask) ? 1 : 0;
357}
358
935ffeec 359static void write_status(acpi_handle handle, int out, int mask)
be18cdab
CC
360{
361 hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
362
935ffeec
CC
363 switch (mask) {
364 case MLED_ON:
e1af14e4 365 out = !(out & 0x1);
935ffeec 366 break;
fdd8d080
CC
367 case GLED_ON:
368 out = (out & 0x1) + 1;
369 break;
e539c2f6
CC
370 case GPS_ON:
371 handle = (out) ? gps_on_handle : gps_off_handle;
372 out = 0x02;
373 break;
935ffeec
CC
374 default:
375 out &= 0x1;
376 break;
377 }
be18cdab 378
f8d1c94b 379 if (write_acpi_int(handle, NULL, out, NULL))
935ffeec 380 printk(ASUS_WARNING " write failed %x\n", mask);
be18cdab
CC
381}
382
383/* /sys/class/led handlers */
935ffeec 384#define ASUS_LED_HANDLER(object, mask) \
be18cdab
CC
385 static void object##_led_set(struct led_classdev *led_cdev, \
386 enum led_brightness value) \
387 { \
e3deda9c 388 object##_led_wk = (value > 0) ? 1 : 0; \
be18cdab
CC
389 queue_work(led_workqueue, &object##_led_work); \
390 } \
391 static void object##_led_update(struct work_struct *ignored) \
392 { \
393 int value = object##_led_wk; \
935ffeec 394 write_status(object##_set_handle, value, (mask)); \
be18cdab
CC
395 }
396
935ffeec
CC
397ASUS_LED_HANDLER(mled, MLED_ON);
398ASUS_LED_HANDLER(pled, PLED_ON);
399ASUS_LED_HANDLER(rled, RLED_ON);
400ASUS_LED_HANDLER(tled, TLED_ON);
fdd8d080 401ASUS_LED_HANDLER(gled, GLED_ON);
be18cdab 402
6b7091e7
CC
403static int get_lcd_state(void)
404{
405 return read_status(LCD_ON);
406}
407
408static int set_lcd_state(int value)
409{
410 int lcd = 0;
411 acpi_status status = 0;
412
413 lcd = value ? 1 : 0;
414
415 if (lcd == get_lcd_state())
416 return 0;
417
8def05fa 418 if (lcd_switch_handle) {
6b7091e7
CC
419 status = acpi_evaluate_object(lcd_switch_handle,
420 NULL, NULL, NULL);
421
422 if (ACPI_FAILURE(status))
423 printk(ASUS_WARNING "Error switching LCD\n");
424 }
425
935ffeec 426 write_status(NULL, lcd, LCD_ON);
6b7091e7
CC
427 return 0;
428}
429
430static void lcd_blank(int blank)
431{
432 struct backlight_device *bd = asus_backlight_device;
433
8def05fa 434 if (bd) {
599a52d1 435 bd->props.power = blank;
28ee086d 436 backlight_update_status(bd);
6b7091e7
CC
437 }
438}
439
440static int read_brightness(struct backlight_device *bd)
441{
27663c58 442 unsigned long long value;
9a816850 443 acpi_status rv = AE_OK;
6b7091e7 444
9a816850
CC
445 rv = acpi_evaluate_integer(brightness_get_handle, NULL, NULL, &value);
446 if (ACPI_FAILURE(rv))
6b7091e7
CC
447 printk(ASUS_WARNING "Error reading brightness\n");
448
449 return value;
450}
451
452static int set_brightness(struct backlight_device *bd, int value)
453{
454 int ret = 0;
455
456 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
457 /* 0 <= value <= 15 */
458
f8d1c94b 459 if (write_acpi_int(brightness_set_handle, NULL, value, NULL)) {
6b7091e7
CC
460 printk(ASUS_WARNING "Error changing brightness\n");
461 ret = -EIO;
462 }
463
464 return ret;
465}
466
467static int update_bl_status(struct backlight_device *bd)
468{
469 int rv;
599a52d1 470 int value = bd->props.brightness;
6b7091e7
CC
471
472 rv = set_brightness(bd, value);
8def05fa 473 if (rv)
6b7091e7
CC
474 return rv;
475
599a52d1 476 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
6b7091e7
CC
477 return set_lcd_state(value);
478}
479
85091b71
CC
480/*
481 * Platform device handlers
482 */
483
484/*
485 * We write our info in page, we begin at offset off and cannot write more
486 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
487 * number of bytes written in page
488 */
489static ssize_t show_infos(struct device *dev,
8def05fa 490 struct device_attribute *attr, char *page)
85091b71
CC
491{
492 int len = 0;
27663c58 493 unsigned long long temp;
85091b71 494 char buf[16]; //enough for all info
9a816850
CC
495 acpi_status rv = AE_OK;
496
85091b71
CC
497 /*
498 * We use the easy way, we don't care of off and count, so we don't set eof
499 * to 1
500 */
501
502 len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
503 len += sprintf(page + len, "Model reference : %s\n", hotk->name);
504 /*
505 * The SFUN method probably allows the original driver to get the list
506 * of features supported by a given model. For now, 0x0100 or 0x0800
507 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
508 * The significance of others is yet to be found.
509 */
9a816850
CC
510 rv = acpi_evaluate_integer(hotk->handle, "SFUN", NULL, &temp);
511 if (!ACPI_FAILURE(rv))
512 len += sprintf(page + len, "SFUN value : 0x%04x\n",
513 (uint) temp);
85091b71
CC
514 /*
515 * Another value for userspace: the ASYM method returns 0x02 for
516 * battery low and 0x04 for battery critical, its readings tend to be
517 * more accurate than those provided by _BST.
518 * Note: since not all the laptops provide this method, errors are
519 * silently ignored.
520 */
9a816850
CC
521 rv = acpi_evaluate_integer(hotk->handle, "ASYM", NULL, &temp);
522 if (!ACPI_FAILURE(rv))
523 len += sprintf(page + len, "ASYM value : 0x%04x\n",
524 (uint) temp);
85091b71
CC
525 if (asus_info) {
526 snprintf(buf, 16, "%d", asus_info->length);
527 len += sprintf(page + len, "DSDT length : %s\n", buf);
528 snprintf(buf, 16, "%d", asus_info->checksum);
529 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
530 snprintf(buf, 16, "%d", asus_info->revision);
531 len += sprintf(page + len, "DSDT revision : %s\n", buf);
532 snprintf(buf, 7, "%s", asus_info->oem_id);
533 len += sprintf(page + len, "OEM id : %s\n", buf);
534 snprintf(buf, 9, "%s", asus_info->oem_table_id);
535 len += sprintf(page + len, "OEM table id : %s\n", buf);
536 snprintf(buf, 16, "%x", asus_info->oem_revision);
537 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
538 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
539 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
540 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
541 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
542 }
543
544 return len;
545}
546
547static int parse_arg(const char *buf, unsigned long count, int *val)
548{
549 if (!count)
550 return 0;
551 if (count > 31)
552 return -EINVAL;
553 if (sscanf(buf, "%i", val) != 1)
554 return -EINVAL;
555 return count;
556}
557
4564de17 558static ssize_t store_status(const char *buf, size_t count,
935ffeec 559 acpi_handle handle, int mask)
4564de17
CC
560{
561 int rv, value;
562 int out = 0;
563
564 rv = parse_arg(buf, count, &value);
565 if (rv > 0)
566 out = value ? 1 : 0;
567
935ffeec 568 write_status(handle, out, mask);
4564de17
CC
569
570 return rv;
571}
572
722ad971
CC
573/*
574 * LEDD display
575 */
576static ssize_t show_ledd(struct device *dev,
577 struct device_attribute *attr, char *buf)
578{
579 return sprintf(buf, "0x%08x\n", hotk->ledd_status);
580}
581
582static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
583 const char *buf, size_t count)
584{
585 int rv, value;
586
587 rv = parse_arg(buf, count, &value);
588 if (rv > 0) {
f8d1c94b 589 if (write_acpi_int(ledd_set_handle, NULL, value, NULL))
722ad971
CC
590 printk(ASUS_WARNING "LED display write failed\n");
591 else
592 hotk->ledd_status = (u32) value;
593 }
594 return rv;
595}
596
4564de17
CC
597/*
598 * WLAN
599 */
600static ssize_t show_wlan(struct device *dev,
601 struct device_attribute *attr, char *buf)
602{
603 return sprintf(buf, "%d\n", read_status(WL_ON));
604}
605
606static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
607 const char *buf, size_t count)
608{
935ffeec 609 return store_status(buf, count, wl_switch_handle, WL_ON);
4564de17
CC
610}
611
612/*
613 * Bluetooth
614 */
615static ssize_t show_bluetooth(struct device *dev,
616 struct device_attribute *attr, char *buf)
617{
618 return sprintf(buf, "%d\n", read_status(BT_ON));
619}
620
8def05fa
LB
621static ssize_t store_bluetooth(struct device *dev,
622 struct device_attribute *attr, const char *buf,
623 size_t count)
4564de17 624{
935ffeec 625 return store_status(buf, count, bt_switch_handle, BT_ON);
4564de17
CC
626}
627
78127b4a
CC
628/*
629 * Display
630 */
631static void set_display(int value)
632{
633 /* no sanity check needed for now */
f8d1c94b 634 if (write_acpi_int(display_set_handle, NULL, value, NULL))
78127b4a
CC
635 printk(ASUS_WARNING "Error setting display\n");
636 return;
637}
638
639static int read_display(void)
640{
27663c58 641 unsigned long long value = 0;
9a816850 642 acpi_status rv = AE_OK;
78127b4a
CC
643
644 /* In most of the case, we know how to set the display, but sometime
645 we can't read it */
8def05fa 646 if (display_get_handle) {
9a816850
CC
647 rv = acpi_evaluate_integer(display_get_handle, NULL,
648 NULL, &value);
649 if (ACPI_FAILURE(rv))
78127b4a
CC
650 printk(ASUS_WARNING "Error reading display status\n");
651 }
652
653 value &= 0x0F; /* needed for some models, shouldn't hurt others */
654
655 return value;
656}
8def05fa 657
78127b4a
CC
658/*
659 * Now, *this* one could be more user-friendly, but so far, no-one has
660 * complained. The significance of bits is the same as in store_disp()
661 */
662static ssize_t show_disp(struct device *dev,
663 struct device_attribute *attr, char *buf)
664{
665 return sprintf(buf, "%d\n", read_display());
666}
667
668/*
669 * Experimental support for display switching. As of now: 1 should activate
670 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
671 * Any combination (bitwise) of these will suffice. I never actually tested 4
672 * displays hooked up simultaneously, so be warned. See the acpi4asus README
673 * for more info.
674 */
675static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
676 const char *buf, size_t count)
677{
678 int rv, value;
679
680 rv = parse_arg(buf, count, &value);
681 if (rv > 0)
682 set_display(value);
683 return rv;
684}
685
8b857353
CC
686/*
687 * Light Sens
688 */
689static void set_light_sens_switch(int value)
690{
f8d1c94b 691 if (write_acpi_int(ls_switch_handle, NULL, value, NULL))
8b857353
CC
692 printk(ASUS_WARNING "Error setting light sensor switch\n");
693 hotk->light_switch = value;
694}
695
696static ssize_t show_lssw(struct device *dev,
697 struct device_attribute *attr, char *buf)
698{
699 return sprintf(buf, "%d\n", hotk->light_switch);
700}
701
702static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
703 const char *buf, size_t count)
704{
705 int rv, value;
706
707 rv = parse_arg(buf, count, &value);
708 if (rv > 0)
709 set_light_sens_switch(value ? 1 : 0);
710
711 return rv;
712}
713
714static void set_light_sens_level(int value)
715{
f8d1c94b 716 if (write_acpi_int(ls_level_handle, NULL, value, NULL))
8b857353
CC
717 printk(ASUS_WARNING "Error setting light sensor level\n");
718 hotk->light_level = value;
719}
720
721static ssize_t show_lslvl(struct device *dev,
722 struct device_attribute *attr, char *buf)
723{
724 return sprintf(buf, "%d\n", hotk->light_level);
725}
726
727static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
728 const char *buf, size_t count)
729{
730 int rv, value;
731
732 rv = parse_arg(buf, count, &value);
733 if (rv > 0) {
734 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
735 /* 0 <= value <= 15 */
736 set_light_sens_level(value);
737 }
738
739 return rv;
740}
741
e539c2f6
CC
742/*
743 * GPS
744 */
745static ssize_t show_gps(struct device *dev,
746 struct device_attribute *attr, char *buf)
747{
748 return sprintf(buf, "%d\n", read_status(GPS_ON));
749}
750
751static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
752 const char *buf, size_t count)
753{
f3985327 754 return store_status(buf, count, NULL, GPS_ON);
e539c2f6
CC
755}
756
034ce90a
CC
757/*
758 * Hotkey functions
759 */
760static struct key_entry *asus_get_entry_by_scancode(int code)
761{
762 struct key_entry *key;
763
764 for (key = asus_keymap; key->type != KE_END; key++)
765 if (code == key->code)
766 return key;
767
768 return NULL;
769}
770
771static struct key_entry *asus_get_entry_by_keycode(int code)
772{
773 struct key_entry *key;
774
775 for (key = asus_keymap; key->type != KE_END; key++)
776 if (code == key->keycode && key->type == KE_KEY)
777 return key;
778
779 return NULL;
780}
781
782static int asus_getkeycode(struct input_dev *dev, int scancode, int *keycode)
783{
784 struct key_entry *key = asus_get_entry_by_scancode(scancode);
785
786 if (key && key->type == KE_KEY) {
787 *keycode = key->keycode;
788 return 0;
789 }
790
791 return -EINVAL;
792}
793
794static int asus_setkeycode(struct input_dev *dev, int scancode, int keycode)
795{
796 struct key_entry *key;
797 int old_keycode;
798
799 if (keycode < 0 || keycode > KEY_MAX)
800 return -EINVAL;
801
802 key = asus_get_entry_by_scancode(scancode);
803 if (key && key->type == KE_KEY) {
804 old_keycode = key->keycode;
805 key->keycode = keycode;
806 set_bit(keycode, dev->keybit);
807 if (!asus_get_entry_by_keycode(old_keycode))
808 clear_bit(old_keycode, dev->keybit);
809 return 0;
810 }
811
812 return -EINVAL;
813}
814
85091b71
CC
815static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
816{
034ce90a
CC
817 static struct key_entry *key;
818
85091b71
CC
819 /* TODO Find a better way to handle events count. */
820 if (!hotk)
821 return;
822
6b7091e7
CC
823 /*
824 * We need to tell the backlight device when the backlight power is
825 * switched
826 */
827 if (event == ATKD_LCD_ON) {
935ffeec 828 write_status(NULL, 1, LCD_ON);
6b7091e7 829 lcd_blank(FB_BLANK_UNBLANK);
8def05fa 830 } else if (event == ATKD_LCD_OFF) {
935ffeec 831 write_status(NULL, 0, LCD_ON);
6b7091e7
CC
832 lcd_blank(FB_BLANK_POWERDOWN);
833 }
834
2a7dc0d8
CC
835 acpi_bus_generate_netlink_event(hotk->device->pnp.device_class,
836 dev_name(&hotk->device->dev), event,
837 hotk->event_count[event % 128]++);
85091b71 838
034ce90a
CC
839 if (hotk->inputdev) {
840 key = asus_get_entry_by_scancode(event);
841 if (!key)
842 return ;
843
844 switch (key->type) {
845 case KE_KEY:
846 input_report_key(hotk->inputdev, key->keycode, 1);
847 input_sync(hotk->inputdev);
848 input_report_key(hotk->inputdev, key->keycode, 0);
849 input_sync(hotk->inputdev);
850 break;
851 }
852 }
85091b71
CC
853}
854
855#define ASUS_CREATE_DEVICE_ATTR(_name) \
856 struct device_attribute dev_attr_##_name = { \
857 .attr = { \
858 .name = __stringify(_name), \
7b595756 859 .mode = 0 }, \
85091b71
CC
860 .show = NULL, \
861 .store = NULL, \
862 }
863
864#define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
865 do { \
866 dev_attr_##_name.attr.mode = _mode; \
867 dev_attr_##_name.show = _show; \
868 dev_attr_##_name.store = _store; \
869 } while(0)
870
871static ASUS_CREATE_DEVICE_ATTR(infos);
4564de17
CC
872static ASUS_CREATE_DEVICE_ATTR(wlan);
873static ASUS_CREATE_DEVICE_ATTR(bluetooth);
78127b4a 874static ASUS_CREATE_DEVICE_ATTR(display);
722ad971 875static ASUS_CREATE_DEVICE_ATTR(ledd);
8b857353
CC
876static ASUS_CREATE_DEVICE_ATTR(ls_switch);
877static ASUS_CREATE_DEVICE_ATTR(ls_level);
e539c2f6 878static ASUS_CREATE_DEVICE_ATTR(gps);
85091b71
CC
879
880static struct attribute *asuspf_attributes[] = {
8def05fa
LB
881 &dev_attr_infos.attr,
882 &dev_attr_wlan.attr,
883 &dev_attr_bluetooth.attr,
884 &dev_attr_display.attr,
885 &dev_attr_ledd.attr,
886 &dev_attr_ls_switch.attr,
887 &dev_attr_ls_level.attr,
e539c2f6 888 &dev_attr_gps.attr,
8def05fa 889 NULL
85091b71
CC
890};
891
892static struct attribute_group asuspf_attribute_group = {
8def05fa 893 .attrs = asuspf_attributes
85091b71
CC
894};
895
896static struct platform_driver asuspf_driver = {
8def05fa
LB
897 .driver = {
898 .name = ASUS_HOTK_FILE,
899 .owner = THIS_MODULE,
900 }
85091b71
CC
901};
902
903static struct platform_device *asuspf_device;
904
85091b71
CC
905static void asus_hotk_add_fs(void)
906{
907 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
4564de17
CC
908
909 if (wl_switch_handle)
910 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
911
912 if (bt_switch_handle)
913 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
914 show_bluetooth, store_bluetooth);
78127b4a
CC
915
916 if (display_set_handle && display_get_handle)
917 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
8def05fa 918 else if (display_set_handle)
78127b4a
CC
919 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
920
722ad971
CC
921 if (ledd_set_handle)
922 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
923
8b857353
CC
924 if (ls_switch_handle && ls_level_handle) {
925 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
926 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
927 }
e539c2f6 928
f3985327 929 if (gps_status_handle && gps_on_handle && gps_off_handle)
e539c2f6 930 ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
85091b71
CC
931}
932
8def05fa 933static int asus_handle_init(char *name, acpi_handle * handle,
85091b71
CC
934 char **paths, int num_paths)
935{
936 int i;
937 acpi_status status;
938
939 for (i = 0; i < num_paths; i++) {
940 status = acpi_get_handle(NULL, paths[i], handle);
941 if (ACPI_SUCCESS(status))
942 return 0;
943 }
944
945 *handle = NULL;
946 return -ENODEV;
947}
948
949#define ASUS_HANDLE_INIT(object) \
950 asus_handle_init(#object, &object##_handle, object##_paths, \
951 ARRAY_SIZE(object##_paths))
952
85091b71
CC
953/*
954 * This function is used to initialize the hotk with right values. In this
955 * method, we can make all the detection we want, and modify the hotk struct
956 */
957static int asus_hotk_get_info(void)
958{
959 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
85091b71 960 union acpi_object *model = NULL;
27663c58 961 unsigned long long bsts_result, hwrs_result;
85091b71
CC
962 char *string = NULL;
963 acpi_status status;
964
965 /*
966 * Get DSDT headers early enough to allow for differentiating between
967 * models, but late enough to allow acpi_bus_register_driver() to fail
968 * before doing anything ACPI-specific. Should we encounter a machine,
969 * which needs special handling (i.e. its hotkey device has a different
970 * HID), this bit will be moved. A global variable asus_info contains
971 * the DSDT header.
972 */
894d79be 973 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
85091b71
CC
974 if (ACPI_FAILURE(status))
975 printk(ASUS_WARNING "Couldn't get the DSDT table header\n");
85091b71
CC
976
977 /* We have to write 0 on init this far for all ASUS models */
f8d1c94b 978 if (write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
85091b71
CC
979 printk(ASUS_ERR "Hotkey initialization failed\n");
980 return -ENODEV;
981 }
982
983 /* This needs to be called for some laptops to init properly */
9a816850
CC
984 status =
985 acpi_evaluate_integer(hotk->handle, "BSTS", NULL, &bsts_result);
986 if (ACPI_FAILURE(status))
85091b71
CC
987 printk(ASUS_WARNING "Error calling BSTS\n");
988 else if (bsts_result)
989 printk(ASUS_NOTICE "BSTS called, 0x%02x returned\n",
9a816850 990 (uint) bsts_result);
85091b71 991
185e5af9
CC
992 /* This too ... */
993 write_acpi_int(hotk->handle, "CWAP", wapf, NULL);
994
85091b71
CC
995 /*
996 * Try to match the object returned by INIT to the specific model.
997 * Handle every possible object (or the lack of thereof) the DSDT
998 * writers might throw at us. When in trouble, we pass NULL to
999 * asus_model_match() and try something completely different.
1000 */
1001 if (buffer.pointer) {
1002 model = buffer.pointer;
1003 switch (model->type) {
1004 case ACPI_TYPE_STRING:
1005 string = model->string.pointer;
1006 break;
1007 case ACPI_TYPE_BUFFER:
1008 string = model->buffer.pointer;
1009 break;
1010 default:
1011 string = "";
1012 break;
1013 }
1014 }
1015 hotk->name = kstrdup(string, GFP_KERNEL);
1016 if (!hotk->name)
1017 return -ENOMEM;
1018
8def05fa 1019 if (*string)
85091b71
CC
1020 printk(ASUS_NOTICE " %s model detected\n", string);
1021
be18cdab
CC
1022 ASUS_HANDLE_INIT(mled_set);
1023 ASUS_HANDLE_INIT(tled_set);
1024 ASUS_HANDLE_INIT(rled_set);
1025 ASUS_HANDLE_INIT(pled_set);
fdd8d080 1026 ASUS_HANDLE_INIT(gled_set);
be18cdab 1027
722ad971
CC
1028 ASUS_HANDLE_INIT(ledd_set);
1029
4564de17
CC
1030 /*
1031 * The HWRS method return informations about the hardware.
1032 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1033 * The significance of others is yet to be found.
1034 * If we don't find the method, we assume the device are present.
1035 */
9a816850
CC
1036 status =
1037 acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &hwrs_result);
1038 if (ACPI_FAILURE(status))
4564de17
CC
1039 hwrs_result = WL_HWRS | BT_HWRS;
1040
8def05fa 1041 if (hwrs_result & WL_HWRS)
4564de17 1042 ASUS_HANDLE_INIT(wl_switch);
8def05fa 1043 if (hwrs_result & BT_HWRS)
4564de17
CC
1044 ASUS_HANDLE_INIT(bt_switch);
1045
1046 ASUS_HANDLE_INIT(wireless_status);
1047
6b7091e7
CC
1048 ASUS_HANDLE_INIT(brightness_set);
1049 ASUS_HANDLE_INIT(brightness_get);
1050
1051 ASUS_HANDLE_INIT(lcd_switch);
1052
78127b4a
CC
1053 ASUS_HANDLE_INIT(display_set);
1054 ASUS_HANDLE_INIT(display_get);
1055
8b857353
CC
1056 /* There is a lot of models with "ALSL", but a few get
1057 a real light sens, so we need to check it. */
832d9950 1058 if (!ASUS_HANDLE_INIT(ls_switch))
8b857353
CC
1059 ASUS_HANDLE_INIT(ls_level);
1060
e539c2f6
CC
1061 ASUS_HANDLE_INIT(gps_on);
1062 ASUS_HANDLE_INIT(gps_off);
1063 ASUS_HANDLE_INIT(gps_status);
1064
85091b71
CC
1065 kfree(model);
1066
1067 return AE_OK;
1068}
1069
034ce90a
CC
1070static int asus_input_init(void)
1071{
1072 const struct key_entry *key;
1073 int result;
1074
1075 hotk->inputdev = input_allocate_device();
1076 if (!hotk->inputdev) {
1077 printk(ASUS_INFO "Unable to allocate input device\n");
1078 return 0;
1079 }
1080 hotk->inputdev->name = "Asus Laptop extra buttons";
1081 hotk->inputdev->phys = ASUS_HOTK_FILE "/input0";
1082 hotk->inputdev->id.bustype = BUS_HOST;
1083 hotk->inputdev->getkeycode = asus_getkeycode;
1084 hotk->inputdev->setkeycode = asus_setkeycode;
1085
1086 for (key = asus_keymap; key->type != KE_END; key++) {
1087 switch (key->type) {
1088 case KE_KEY:
1089 set_bit(EV_KEY, hotk->inputdev->evbit);
1090 set_bit(key->keycode, hotk->inputdev->keybit);
1091 break;
1092 }
1093 }
1094 result = input_register_device(hotk->inputdev);
1095 if (result) {
1096 printk(ASUS_INFO "Unable to register input device\n");
1097 input_free_device(hotk->inputdev);
1098 }
1099 return result;
1100}
1101
85091b71
CC
1102static int asus_hotk_check(void)
1103{
1104 int result = 0;
1105
1106 result = acpi_bus_get_status(hotk->device);
1107 if (result)
1108 return result;
1109
1110 if (hotk->device->status.present) {
1111 result = asus_hotk_get_info();
1112 } else {
1113 printk(ASUS_ERR "Hotkey device not present, aborting\n");
1114 return -EINVAL;
1115 }
1116
1117 return result;
1118}
1119
1120static int asus_hotk_found;
1121
1122static int asus_hotk_add(struct acpi_device *device)
1123{
1124 acpi_status status = AE_OK;
1125 int result;
1126
1127 if (!device)
1128 return -EINVAL;
1129
1130 printk(ASUS_NOTICE "Asus Laptop Support version %s\n",
1131 ASUS_LAPTOP_VERSION);
1132
dd00cc48 1133 hotk = kzalloc(sizeof(struct asus_hotk), GFP_KERNEL);
85091b71
CC
1134 if (!hotk)
1135 return -ENOMEM;
85091b71
CC
1136
1137 hotk->handle = device->handle;
1138 strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
1139 strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
db89b4f0 1140 device->driver_data = hotk;
85091b71
CC
1141 hotk->device = device;
1142
1143 result = asus_hotk_check();
1144 if (result)
1145 goto end;
1146
1147 asus_hotk_add_fs();
1148
1149 /*
1150 * We install the handler, it will receive the hotk in parameter, so, we
1151 * could add other data to the hotk struct
1152 */
a9dbcb37 1153 status = acpi_install_notify_handler(hotk->handle, ACPI_ALL_NOTIFY,
85091b71
CC
1154 asus_hotk_notify, hotk);
1155 if (ACPI_FAILURE(status))
1156 printk(ASUS_ERR "Error installing notify handler\n");
1157
1158 asus_hotk_found = 1;
1159
6b7091e7 1160 /* WLED and BLED are on by default */
935ffeec
CC
1161 write_status(bt_switch_handle, 1, BT_ON);
1162 write_status(wl_switch_handle, 1, WL_ON);
1163
1164 /* If the h/w switch is off, we need to check the real status */
1165 write_status(NULL, read_status(BT_ON), BT_ON);
1166 write_status(NULL, read_status(WL_ON), WL_ON);
4564de17 1167
6b7091e7 1168 /* LCD Backlight is on by default */
935ffeec 1169 write_status(NULL, 1, LCD_ON);
6b7091e7 1170
722ad971
CC
1171 /* LED display is off by default */
1172 hotk->ledd_status = 0xFFF;
1173
8def05fa
LB
1174 /* Set initial values of light sensor and level */
1175 hotk->light_switch = 1; /* Default to light sensor disabled */
1176 hotk->light_level = 0; /* level 5 for sensor sensitivity */
8b857353 1177
8def05fa
LB
1178 if (ls_switch_handle)
1179 set_light_sens_switch(hotk->light_switch);
8b857353 1180
8def05fa
LB
1181 if (ls_level_handle)
1182 set_light_sens_level(hotk->light_level);
8b857353 1183
e539c2f6
CC
1184 /* GPS is on by default */
1185 write_status(NULL, 1, GPS_ON);
1186
ed6f4421 1187end:
85091b71
CC
1188 if (result) {
1189 kfree(hotk->name);
1190 kfree(hotk);
1191 }
1192
1193 return result;
1194}
1195
1196static int asus_hotk_remove(struct acpi_device *device, int type)
1197{
1198 acpi_status status = 0;
1199
1200 if (!device || !acpi_driver_data(device))
1201 return -EINVAL;
1202
a9dbcb37 1203 status = acpi_remove_notify_handler(hotk->handle, ACPI_ALL_NOTIFY,
85091b71
CC
1204 asus_hotk_notify);
1205 if (ACPI_FAILURE(status))
1206 printk(ASUS_ERR "Error removing notify handler\n");
1207
1208 kfree(hotk->name);
1209 kfree(hotk);
1210
1211 return 0;
1212}
1213
6b7091e7
CC
1214static void asus_backlight_exit(void)
1215{
8def05fa 1216 if (asus_backlight_device)
6b7091e7
CC
1217 backlight_device_unregister(asus_backlight_device);
1218}
1219
be18cdab 1220#define ASUS_LED_UNREGISTER(object) \
e1996a69
GC
1221 if (object##_led.dev) \
1222 led_classdev_unregister(&object##_led)
be18cdab
CC
1223
1224static void asus_led_exit(void)
1225{
3b0d7117 1226 destroy_workqueue(led_workqueue);
be18cdab
CC
1227 ASUS_LED_UNREGISTER(mled);
1228 ASUS_LED_UNREGISTER(tled);
1229 ASUS_LED_UNREGISTER(pled);
1230 ASUS_LED_UNREGISTER(rled);
fdd8d080 1231 ASUS_LED_UNREGISTER(gled);
be18cdab
CC
1232}
1233
034ce90a
CC
1234static void asus_input_exit(void)
1235{
1236 if (hotk->inputdev)
1237 input_unregister_device(hotk->inputdev);
1238}
1239
85091b71
CC
1240static void __exit asus_laptop_exit(void)
1241{
6b7091e7 1242 asus_backlight_exit();
be18cdab 1243 asus_led_exit();
034ce90a 1244 asus_input_exit();
be18cdab 1245
85091b71 1246 acpi_bus_unregister_driver(&asus_hotk_driver);
8def05fa
LB
1247 sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
1248 platform_device_unregister(asuspf_device);
1249 platform_driver_unregister(&asuspf_driver);
85091b71
CC
1250}
1251
8def05fa 1252static int asus_backlight_init(struct device *dev)
6b7091e7
CC
1253{
1254 struct backlight_device *bd;
1255
8def05fa
LB
1256 if (brightness_set_handle && lcd_switch_handle) {
1257 bd = backlight_device_register(ASUS_HOTK_FILE, dev,
599a52d1 1258 NULL, &asusbl_ops);
8def05fa 1259 if (IS_ERR(bd)) {
6b7091e7
CC
1260 printk(ASUS_ERR
1261 "Could not register asus backlight device\n");
1262 asus_backlight_device = NULL;
1263 return PTR_ERR(bd);
1264 }
1265
1266 asus_backlight_device = bd;
1267
599a52d1
RP
1268 bd->props.max_brightness = 15;
1269 bd->props.brightness = read_brightness(NULL);
1270 bd->props.power = FB_BLANK_UNBLANK;
28ee086d 1271 backlight_update_status(bd);
6b7091e7
CC
1272 }
1273 return 0;
1274}
1275
be18cdab 1276static int asus_led_register(acpi_handle handle,
8def05fa 1277 struct led_classdev *ldev, struct device *dev)
be18cdab 1278{
8def05fa 1279 if (!handle)
be18cdab
CC
1280 return 0;
1281
1282 return led_classdev_register(dev, ldev);
1283}
8def05fa 1284
be18cdab
CC
1285#define ASUS_LED_REGISTER(object, device) \
1286 asus_led_register(object##_set_handle, &object##_led, device)
1287
8def05fa 1288static int asus_led_init(struct device *dev)
be18cdab
CC
1289{
1290 int rv;
1291
1292 rv = ASUS_LED_REGISTER(mled, dev);
8def05fa 1293 if (rv)
3b0d7117 1294 goto out;
be18cdab
CC
1295
1296 rv = ASUS_LED_REGISTER(tled, dev);
8def05fa 1297 if (rv)
3b0d7117 1298 goto out1;
be18cdab
CC
1299
1300 rv = ASUS_LED_REGISTER(rled, dev);
8def05fa 1301 if (rv)
3b0d7117 1302 goto out2;
be18cdab
CC
1303
1304 rv = ASUS_LED_REGISTER(pled, dev);
8def05fa 1305 if (rv)
3b0d7117 1306 goto out3;
be18cdab 1307
fdd8d080
CC
1308 rv = ASUS_LED_REGISTER(gled, dev);
1309 if (rv)
3b0d7117 1310 goto out4;
fdd8d080 1311
be18cdab 1312 led_workqueue = create_singlethread_workqueue("led_workqueue");
8def05fa 1313 if (!led_workqueue)
3b0d7117 1314 goto out5;
be18cdab
CC
1315
1316 return 0;
3b0d7117
AV
1317out5:
1318 rv = -ENOMEM;
1319 ASUS_LED_UNREGISTER(gled);
1320out4:
1321 ASUS_LED_UNREGISTER(pled);
1322out3:
1323 ASUS_LED_UNREGISTER(rled);
1324out2:
1325 ASUS_LED_UNREGISTER(tled);
1326out1:
1327 ASUS_LED_UNREGISTER(mled);
1328out:
1329 return rv;
be18cdab
CC
1330}
1331
85091b71
CC
1332static int __init asus_laptop_init(void)
1333{
be18cdab 1334 struct device *dev;
85091b71
CC
1335 int result;
1336
1337 if (acpi_disabled)
1338 return -ENODEV;
1339
85091b71
CC
1340 result = acpi_bus_register_driver(&asus_hotk_driver);
1341 if (result < 0)
1342 return result;
1343
1344 /*
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.
1350 */
1351 if (!asus_hotk_found) {
1352 acpi_bus_unregister_driver(&asus_hotk_driver);
1353 return -ENODEV;
1354 }
1355
be18cdab
CC
1356 dev = acpi_get_physical_device(hotk->device->handle);
1357
6766fec3
TR
1358 if (!acpi_video_backlight_support()) {
1359 result = asus_backlight_init(dev);
1360 if (result)
1361 goto fail_backlight;
1362 } else
1363 printk(ASUS_INFO "Brightness ignored, must be controlled by "
1364 "ACPI video driver\n");
6b7091e7 1365
034ce90a
CC
1366 result = asus_input_init();
1367 if (result)
1368 goto fail_input;
1369
be18cdab 1370 result = asus_led_init(dev);
8def05fa 1371 if (result)
be18cdab
CC
1372 goto fail_led;
1373
8def05fa 1374 /* Register platform stuff */
85091b71 1375 result = platform_driver_register(&asuspf_driver);
8def05fa
LB
1376 if (result)
1377 goto fail_platform_driver;
85091b71 1378
8def05fa
LB
1379 asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
1380 if (!asuspf_device) {
1381 result = -ENOMEM;
1382 goto fail_platform_device1;
1383 }
85091b71 1384
8def05fa
LB
1385 result = platform_device_add(asuspf_device);
1386 if (result)
1387 goto fail_platform_device2;
85091b71 1388
8def05fa 1389 result = sysfs_create_group(&asuspf_device->dev.kobj,
85091b71 1390 &asuspf_attribute_group);
8def05fa
LB
1391 if (result)
1392 goto fail_sysfs;
85091b71 1393
8def05fa 1394 return 0;
85091b71 1395
ed6f4421 1396fail_sysfs:
8def05fa 1397 platform_device_del(asuspf_device);
85091b71 1398
ed6f4421 1399fail_platform_device2:
85091b71
CC
1400 platform_device_put(asuspf_device);
1401
ed6f4421 1402fail_platform_device1:
8def05fa 1403 platform_driver_unregister(&asuspf_driver);
85091b71 1404
ed6f4421 1405fail_platform_driver:
be18cdab
CC
1406 asus_led_exit();
1407
ed6f4421 1408fail_led:
034ce90a
CC
1409 asus_input_exit();
1410
ed6f4421 1411fail_input:
6b7091e7
CC
1412 asus_backlight_exit();
1413
ed6f4421 1414fail_backlight:
85091b71
CC
1415
1416 return result;
1417}
1418
1419module_init(asus_laptop_init);
1420module_exit(asus_laptop_exit);