]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/platform/x86/asus-laptop.c
asus-laptop: Platform detection for Pegatron Lucid
[mv-sheeva.git] / drivers / platform / x86 / asus-laptop.c
1 /*
2  *  asus-laptop.c - Asus Laptop Support
3  *
4  *
5  *  Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
6  *  Copyright (C) 2006-2007 Corentin Chary
7  *  Copyright (C) 2011 Wind River Systems
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  *
24  *  The development page for this driver is located at
25  *  http://sourceforge.net/projects/acpi4asus/
26  *
27  *  Credits:
28  *  Pontus Fuchs   - Helper functions, cleanup
29  *  Johann Wiesner - Small compile fixes
30  *  John Belmonte  - ACPI code for Toshiba laptop was a good starting point.
31  *  Eric Burghard  - LED display support for W1N
32  *  Josh Green     - Light Sens support
33  *  Thomas Tuttle  - His first patch for led support was very helpful
34  *  Sam Lin        - GPS support
35  */
36
37 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/types.h>
43 #include <linux/err.h>
44 #include <linux/proc_fs.h>
45 #include <linux/backlight.h>
46 #include <linux/fb.h>
47 #include <linux/leds.h>
48 #include <linux/platform_device.h>
49 #include <linux/uaccess.h>
50 #include <linux/input.h>
51 #include <linux/input/sparse-keymap.h>
52 #include <linux/input-polldev.h>
53 #include <linux/rfkill.h>
54 #include <linux/slab.h>
55 #include <linux/dmi.h>
56 #include <acpi/acpi_drivers.h>
57 #include <acpi/acpi_bus.h>
58
59 #define ASUS_LAPTOP_VERSION     "0.42"
60
61 #define ASUS_LAPTOP_NAME        "Asus Laptop Support"
62 #define ASUS_LAPTOP_CLASS       "hotkey"
63 #define ASUS_LAPTOP_DEVICE_NAME "Hotkey"
64 #define ASUS_LAPTOP_FILE        KBUILD_MODNAME
65 #define ASUS_LAPTOP_PREFIX      "\\_SB.ATKD."
66
67 MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
68 MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
69 MODULE_LICENSE("GPL");
70
71 /*
72  * WAPF defines the behavior of the Fn+Fx wlan key
73  * The significance of values is yet to be found, but
74  * most of the time:
75  * Bit | Bluetooth | WLAN
76  *  0  | Hardware  | Hardware
77  *  1  | Hardware  | Software
78  *  4  | Software  | Software
79  */
80 static uint wapf = 1;
81 module_param(wapf, uint, 0444);
82 MODULE_PARM_DESC(wapf, "WAPF value");
83
84 static int wlan_status = 1;
85 static int bluetooth_status = 1;
86 static int wimax_status = -1;
87 static int wwan_status = -1;
88
89 module_param(wlan_status, int, 0444);
90 MODULE_PARM_DESC(wlan_status, "Set the wireless status on boot "
91                  "(0 = disabled, 1 = enabled, -1 = don't do anything). "
92                  "default is 1");
93
94 module_param(bluetooth_status, int, 0444);
95 MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
96                  "(0 = disabled, 1 = enabled, -1 = don't do anything). "
97                  "default is 1");
98
99 module_param(wimax_status, int, 0444);
100 MODULE_PARM_DESC(wimax_status, "Set the wireless status on boot "
101                  "(0 = disabled, 1 = enabled, -1 = don't do anything). "
102                  "default is 1");
103
104 module_param(wwan_status, int, 0444);
105 MODULE_PARM_DESC(wwan_status, "Set the wireless status on boot "
106                  "(0 = disabled, 1 = enabled, -1 = don't do anything). "
107                  "default is 1");
108
109 /*
110  * Some events we use, same for all Asus
111  */
112 #define ATKD_BR_UP      0x10    /* (event & ~ATKD_BR_UP) = brightness level */
113 #define ATKD_BR_DOWN    0x20    /* (event & ~ATKD_BR_DOWN) = britghness level */
114 #define ATKD_BR_MIN     ATKD_BR_UP
115 #define ATKD_BR_MAX     (ATKD_BR_DOWN | 0xF)    /* 0x2f */
116 #define ATKD_LCD_ON     0x33
117 #define ATKD_LCD_OFF    0x34
118
119 /*
120  * Known bits returned by \_SB.ATKD.HWRS
121  */
122 #define WL_HWRS         0x80
123 #define BT_HWRS         0x100
124
125 /*
126  * Flags for hotk status
127  * WL_ON and BT_ON are also used for wireless_status()
128  */
129 #define WL_RSTS         0x01    /* internal Wifi */
130 #define BT_RSTS         0x02    /* internal Bluetooth */
131 #define WM_RSTS         0x08    /* internal wimax */
132 #define WW_RSTS         0x20    /* internal wwan */
133
134 /* LED */
135 #define METHOD_MLED             "MLED"
136 #define METHOD_TLED             "TLED"
137 #define METHOD_RLED             "RLED"  /* W1JC */
138 #define METHOD_PLED             "PLED"  /* A7J */
139 #define METHOD_GLED             "GLED"  /* G1, G2 (probably) */
140
141 /* LEDD */
142 #define METHOD_LEDD             "SLCM"
143
144 /*
145  * Bluetooth and WLAN
146  * WLED and BLED are not handled like other XLED, because in some dsdt
147  * they also control the WLAN/Bluetooth device.
148  */
149 #define METHOD_WLAN             "WLED"
150 #define METHOD_BLUETOOTH        "BLED"
151
152 /* WWAN and WIMAX */
153 #define METHOD_WWAN             "GSMC"
154 #define METHOD_WIMAX            "WMXC"
155
156 #define METHOD_WL_STATUS        "RSTS"
157
158 /* Brightness */
159 #define METHOD_BRIGHTNESS_SET   "SPLV"
160 #define METHOD_BRIGHTNESS_GET   "GPLV"
161
162 /* Display */
163 #define METHOD_SWITCH_DISPLAY   "SDSP"
164
165 #define METHOD_ALS_CONTROL      "ALSC" /* Z71A Z71V */
166 #define METHOD_ALS_LEVEL        "ALSL" /* Z71A Z71V */
167
168 /* GPS */
169 /* R2H use different handle for GPS on/off */
170 #define METHOD_GPS_ON           "SDON"
171 #define METHOD_GPS_OFF          "SDOF"
172 #define METHOD_GPS_STATUS       "GPST"
173
174 /* Keyboard light */
175 #define METHOD_KBD_LIGHT_SET    "SLKB"
176 #define METHOD_KBD_LIGHT_GET    "GLKB"
177
178 /* For Pegatron Lucid tablet */
179 #define DEVICE_NAME_PEGA        "Lucid"
180 #define METHOD_PEGA_ENABLE      "ENPR"
181 #define METHOD_PEGA_DISABLE     "DAPR"
182 #define METHOD_PEGA_READ        "RDLN"
183
184 /*
185  * Define a specific led structure to keep the main structure clean
186  */
187 struct asus_led {
188         int wk;
189         struct work_struct work;
190         struct led_classdev led;
191         struct asus_laptop *asus;
192         const char *method;
193 };
194
195 /*
196  * This is the main structure, we can use it to store anything interesting
197  * about the hotk device
198  */
199 struct asus_laptop {
200         char *name;             /* laptop name */
201
202         struct acpi_table_header *dsdt_info;
203         struct platform_device *platform_device;
204         struct acpi_device *device;             /* the device we are in */
205         struct backlight_device *backlight_device;
206
207         struct input_dev *inputdev;
208         struct key_entry *keymap;
209
210         struct asus_led mled;
211         struct asus_led tled;
212         struct asus_led rled;
213         struct asus_led pled;
214         struct asus_led gled;
215         struct asus_led kled;
216         struct workqueue_struct *led_workqueue;
217
218         int wireless_status;
219         bool have_rsts;
220         bool is_pega_lucid;
221
222         struct rfkill *gps_rfkill;
223
224         acpi_handle handle;     /* the handle of the hotk device */
225         u32 ledd_status;        /* status of the LED display */
226         u8 light_level;         /* light sensor level */
227         u8 light_switch;        /* light sensor switch value */
228         u16 event_count[128];   /* count for each event TODO make this better */
229 };
230
231 static const struct key_entry asus_keymap[] = {
232         /* Lenovo SL Specific keycodes */
233         {KE_KEY, 0x02, { KEY_SCREENLOCK } },
234         {KE_KEY, 0x05, { KEY_WLAN } },
235         {KE_KEY, 0x08, { KEY_F13 } },
236         {KE_KEY, 0x17, { KEY_ZOOM } },
237         {KE_KEY, 0x1f, { KEY_BATTERY } },
238         /* End of Lenovo SL Specific keycodes */
239         {KE_KEY, 0x30, { KEY_VOLUMEUP } },
240         {KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
241         {KE_KEY, 0x32, { KEY_MUTE } },
242         {KE_KEY, 0x33, { KEY_SWITCHVIDEOMODE } },
243         {KE_KEY, 0x34, { KEY_SWITCHVIDEOMODE } },
244         {KE_KEY, 0x40, { KEY_PREVIOUSSONG } },
245         {KE_KEY, 0x41, { KEY_NEXTSONG } },
246         {KE_KEY, 0x43, { KEY_STOPCD } },
247         {KE_KEY, 0x45, { KEY_PLAYPAUSE } },
248         {KE_KEY, 0x4c, { KEY_MEDIA } },
249         {KE_KEY, 0x50, { KEY_EMAIL } },
250         {KE_KEY, 0x51, { KEY_WWW } },
251         {KE_KEY, 0x55, { KEY_CALC } },
252         {KE_KEY, 0x5C, { KEY_SCREENLOCK } },  /* Screenlock */
253         {KE_KEY, 0x5D, { KEY_WLAN } },
254         {KE_KEY, 0x5E, { KEY_WLAN } },
255         {KE_KEY, 0x5F, { KEY_WLAN } },
256         {KE_KEY, 0x60, { KEY_SWITCHVIDEOMODE } },
257         {KE_KEY, 0x61, { KEY_SWITCHVIDEOMODE } },
258         {KE_KEY, 0x62, { KEY_SWITCHVIDEOMODE } },
259         {KE_KEY, 0x63, { KEY_SWITCHVIDEOMODE } },
260         {KE_KEY, 0x6B, { KEY_F13 } }, /* Lock Touchpad */
261         {KE_KEY, 0x7E, { KEY_BLUETOOTH } },
262         {KE_KEY, 0x7D, { KEY_BLUETOOTH } },
263         {KE_KEY, 0x82, { KEY_CAMERA } },
264         {KE_KEY, 0x88, { KEY_WLAN  } },
265         {KE_KEY, 0x8A, { KEY_PROG1 } },
266         {KE_KEY, 0x95, { KEY_MEDIA } },
267         {KE_KEY, 0x99, { KEY_PHONE } },
268         {KE_KEY, 0xc4, { KEY_KBDILLUMUP } },
269         {KE_KEY, 0xc5, { KEY_KBDILLUMDOWN } },
270         {KE_KEY, 0xb5, { KEY_CALC } },
271         {KE_END, 0},
272 };
273
274
275 /*
276  * This function evaluates an ACPI method, given an int as parameter, the
277  * method is searched within the scope of the handle, can be NULL. The output
278  * of the method is written is output, which can also be NULL
279  *
280  * returns 0 if write is successful, -1 else.
281  */
282 static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
283                               struct acpi_buffer *output)
284 {
285         struct acpi_object_list params; /* list of input parameters (an int) */
286         union acpi_object in_obj;       /* the only param we use */
287         acpi_status status;
288
289         if (!handle)
290                 return -1;
291
292         params.count = 1;
293         params.pointer = &in_obj;
294         in_obj.type = ACPI_TYPE_INTEGER;
295         in_obj.integer.value = val;
296
297         status = acpi_evaluate_object(handle, (char *)method, &params, output);
298         if (status == AE_OK)
299                 return 0;
300         else
301                 return -1;
302 }
303
304 static int write_acpi_int(acpi_handle handle, const char *method, int val)
305 {
306         return write_acpi_int_ret(handle, method, val, NULL);
307 }
308
309 static int acpi_check_handle(acpi_handle handle, const char *method,
310                              acpi_handle *ret)
311 {
312         acpi_status status;
313
314         if (method == NULL)
315                 return -ENODEV;
316
317         if (ret)
318                 status = acpi_get_handle(handle, (char *)method,
319                                          ret);
320         else {
321                 acpi_handle dummy;
322
323                 status = acpi_get_handle(handle, (char *)method,
324                                          &dummy);
325         }
326
327         if (status != AE_OK) {
328                 if (ret)
329                         pr_warn("Error finding %s\n", method);
330                 return -ENODEV;
331         }
332         return 0;
333 }
334
335 static bool asus_check_pega_lucid(struct asus_laptop *asus)
336 {
337         return !strcmp(asus->name, DEVICE_NAME_PEGA) &&
338            !acpi_check_handle(asus->handle, METHOD_PEGA_ENABLE, NULL) &&
339            !acpi_check_handle(asus->handle, METHOD_PEGA_DISABLE, NULL) &&
340            !acpi_check_handle(asus->handle, METHOD_PEGA_READ, NULL);
341 }
342
343 /* Generic LED function */
344 static int asus_led_set(struct asus_laptop *asus, const char *method,
345                          int value)
346 {
347         if (!strcmp(method, METHOD_MLED))
348                 value = !value;
349         else if (!strcmp(method, METHOD_GLED))
350                 value = !value + 1;
351         else
352                 value = !!value;
353
354         return write_acpi_int(asus->handle, method, value);
355 }
356
357 /*
358  * LEDs
359  */
360 /* /sys/class/led handlers */
361 static void asus_led_cdev_set(struct led_classdev *led_cdev,
362                          enum led_brightness value)
363 {
364         struct asus_led *led = container_of(led_cdev, struct asus_led, led);
365         struct asus_laptop *asus = led->asus;
366
367         led->wk = !!value;
368         queue_work(asus->led_workqueue, &led->work);
369 }
370
371 static void asus_led_cdev_update(struct work_struct *work)
372 {
373         struct asus_led *led = container_of(work, struct asus_led, work);
374         struct asus_laptop *asus = led->asus;
375
376         asus_led_set(asus, led->method, led->wk);
377 }
378
379 static enum led_brightness asus_led_cdev_get(struct led_classdev *led_cdev)
380 {
381         return led_cdev->brightness;
382 }
383
384 /*
385  * Keyboard backlight (also a LED)
386  */
387 static int asus_kled_lvl(struct asus_laptop *asus)
388 {
389         unsigned long long kblv;
390         struct acpi_object_list params;
391         union acpi_object in_obj;
392         acpi_status rv;
393
394         params.count = 1;
395         params.pointer = &in_obj;
396         in_obj.type = ACPI_TYPE_INTEGER;
397         in_obj.integer.value = 2;
398
399         rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
400                                    &params, &kblv);
401         if (ACPI_FAILURE(rv)) {
402                 pr_warn("Error reading kled level\n");
403                 return -ENODEV;
404         }
405         return kblv;
406 }
407
408 static int asus_kled_set(struct asus_laptop *asus, int kblv)
409 {
410         if (kblv > 0)
411                 kblv = (1 << 7) | (kblv & 0x7F);
412         else
413                 kblv = 0;
414
415         if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
416                 pr_warn("Keyboard LED display write failed\n");
417                 return -EINVAL;
418         }
419         return 0;
420 }
421
422 static void asus_kled_cdev_set(struct led_classdev *led_cdev,
423                               enum led_brightness value)
424 {
425         struct asus_led *led = container_of(led_cdev, struct asus_led, led);
426         struct asus_laptop *asus = led->asus;
427
428         led->wk = value;
429         queue_work(asus->led_workqueue, &led->work);
430 }
431
432 static void asus_kled_cdev_update(struct work_struct *work)
433 {
434         struct asus_led *led = container_of(work, struct asus_led, work);
435         struct asus_laptop *asus = led->asus;
436
437         asus_kled_set(asus, led->wk);
438 }
439
440 static enum led_brightness asus_kled_cdev_get(struct led_classdev *led_cdev)
441 {
442         struct asus_led *led = container_of(led_cdev, struct asus_led, led);
443         struct asus_laptop *asus = led->asus;
444
445         return asus_kled_lvl(asus);
446 }
447
448 static void asus_led_exit(struct asus_laptop *asus)
449 {
450         if (asus->mled.led.dev)
451                 led_classdev_unregister(&asus->mled.led);
452         if (asus->tled.led.dev)
453                 led_classdev_unregister(&asus->tled.led);
454         if (asus->pled.led.dev)
455                 led_classdev_unregister(&asus->pled.led);
456         if (asus->rled.led.dev)
457                 led_classdev_unregister(&asus->rled.led);
458         if (asus->gled.led.dev)
459                 led_classdev_unregister(&asus->gled.led);
460         if (asus->kled.led.dev)
461                 led_classdev_unregister(&asus->kled.led);
462         if (asus->led_workqueue) {
463                 destroy_workqueue(asus->led_workqueue);
464                 asus->led_workqueue = NULL;
465         }
466 }
467
468 /*  Ugly macro, need to fix that later */
469 static int asus_led_register(struct asus_laptop *asus,
470                              struct asus_led *led,
471                              const char *name, const char *method)
472 {
473         struct led_classdev *led_cdev = &led->led;
474
475         if (!method || acpi_check_handle(asus->handle, method, NULL))
476                 return 0; /* Led not present */
477
478         led->asus = asus;
479         led->method = method;
480
481         INIT_WORK(&led->work, asus_led_cdev_update);
482         led_cdev->name = name;
483         led_cdev->brightness_set = asus_led_cdev_set;
484         led_cdev->brightness_get = asus_led_cdev_get;
485         led_cdev->max_brightness = 1;
486         return led_classdev_register(&asus->platform_device->dev, led_cdev);
487 }
488
489 static int asus_led_init(struct asus_laptop *asus)
490 {
491         int r;
492
493         /*
494          * Functions that actually update the LED's are called from a
495          * workqueue. By doing this as separate work rather than when the LED
496          * subsystem asks, we avoid messing with the Asus ACPI stuff during a
497          * potentially bad time, such as a timer interrupt.
498          */
499         asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
500         if (!asus->led_workqueue)
501                 return -ENOMEM;
502
503         r = asus_led_register(asus, &asus->mled, "asus::mail", METHOD_MLED);
504         if (r)
505                 goto error;
506         r = asus_led_register(asus, &asus->tled, "asus::touchpad", METHOD_TLED);
507         if (r)
508                 goto error;
509         r = asus_led_register(asus, &asus->rled, "asus::record", METHOD_RLED);
510         if (r)
511                 goto error;
512         r = asus_led_register(asus, &asus->pled, "asus::phone", METHOD_PLED);
513         if (r)
514                 goto error;
515         r = asus_led_register(asus, &asus->gled, "asus::gaming", METHOD_GLED);
516         if (r)
517                 goto error;
518         if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
519             !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL)) {
520                 struct asus_led *led = &asus->kled;
521                 struct led_classdev *cdev = &led->led;
522
523                 led->asus = asus;
524
525                 INIT_WORK(&led->work, asus_kled_cdev_update);
526                 cdev->name = "asus::kbd_backlight";
527                 cdev->brightness_set = asus_kled_cdev_set;
528                 cdev->brightness_get = asus_kled_cdev_get;
529                 cdev->max_brightness = 3;
530                 r = led_classdev_register(&asus->platform_device->dev, cdev);
531         }
532 error:
533         if (r)
534                 asus_led_exit(asus);
535         return r;
536 }
537
538 /*
539  * Backlight device
540  */
541 static int asus_read_brightness(struct backlight_device *bd)
542 {
543         struct asus_laptop *asus = bl_get_data(bd);
544         unsigned long long value;
545         acpi_status rv = AE_OK;
546
547         rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
548                                    NULL, &value);
549         if (ACPI_FAILURE(rv))
550                 pr_warn("Error reading brightness\n");
551
552         return value;
553 }
554
555 static int asus_set_brightness(struct backlight_device *bd, int value)
556 {
557         struct asus_laptop *asus = bl_get_data(bd);
558
559         if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
560                 pr_warn("Error changing brightness\n");
561                 return -EIO;
562         }
563         return 0;
564 }
565
566 static int update_bl_status(struct backlight_device *bd)
567 {
568         int value = bd->props.brightness;
569
570         return asus_set_brightness(bd, value);
571 }
572
573 static const struct backlight_ops asusbl_ops = {
574         .get_brightness = asus_read_brightness,
575         .update_status = update_bl_status,
576 };
577
578 static int asus_backlight_notify(struct asus_laptop *asus)
579 {
580         struct backlight_device *bd = asus->backlight_device;
581         int old = bd->props.brightness;
582
583         backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
584
585         return old;
586 }
587
588 static int asus_backlight_init(struct asus_laptop *asus)
589 {
590         struct backlight_device *bd;
591         struct backlight_properties props;
592
593         if (acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) ||
594             acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL))
595                 return 0;
596
597         memset(&props, 0, sizeof(struct backlight_properties));
598         props.max_brightness = 15;
599         props.type = BACKLIGHT_PLATFORM;
600
601         bd = backlight_device_register(ASUS_LAPTOP_FILE,
602                                        &asus->platform_device->dev, asus,
603                                        &asusbl_ops, &props);
604         if (IS_ERR(bd)) {
605                 pr_err("Could not register asus backlight device\n");
606                 asus->backlight_device = NULL;
607                 return PTR_ERR(bd);
608         }
609
610         asus->backlight_device = bd;
611         bd->props.brightness = asus_read_brightness(bd);
612         bd->props.power = FB_BLANK_UNBLANK;
613         backlight_update_status(bd);
614         return 0;
615 }
616
617 static void asus_backlight_exit(struct asus_laptop *asus)
618 {
619         if (asus->backlight_device)
620                 backlight_device_unregister(asus->backlight_device);
621         asus->backlight_device = NULL;
622 }
623
624 /*
625  * Platform device handlers
626  */
627
628 /*
629  * We write our info in page, we begin at offset off and cannot write more
630  * than count bytes. We set eof to 1 if we handle those 2 values. We return the
631  * number of bytes written in page
632  */
633 static ssize_t show_infos(struct device *dev,
634                           struct device_attribute *attr, char *page)
635 {
636         struct asus_laptop *asus = dev_get_drvdata(dev);
637         int len = 0;
638         unsigned long long temp;
639         char buf[16];           /* enough for all info */
640         acpi_status rv = AE_OK;
641
642         /*
643          * We use the easy way, we don't care of off and count,
644          * so we don't set eof to 1
645          */
646
647         len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
648         len += sprintf(page + len, "Model reference    : %s\n", asus->name);
649         /*
650          * The SFUN method probably allows the original driver to get the list
651          * of features supported by a given model. For now, 0x0100 or 0x0800
652          * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
653          * The significance of others is yet to be found.
654          */
655         rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
656         if (!ACPI_FAILURE(rv))
657                 len += sprintf(page + len, "SFUN value         : %#x\n",
658                                (uint) temp);
659         /*
660          * The HWRS method return informations about the hardware.
661          * 0x80 bit is for WLAN, 0x100 for Bluetooth.
662          * The significance of others is yet to be found.
663          * If we don't find the method, we assume the device are present.
664          */
665         rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
666         if (!ACPI_FAILURE(rv))
667                 len += sprintf(page + len, "HRWS value         : %#x\n",
668                                (uint) temp);
669         /*
670          * Another value for userspace: the ASYM method returns 0x02 for
671          * battery low and 0x04 for battery critical, its readings tend to be
672          * more accurate than those provided by _BST.
673          * Note: since not all the laptops provide this method, errors are
674          * silently ignored.
675          */
676         rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
677         if (!ACPI_FAILURE(rv))
678                 len += sprintf(page + len, "ASYM value         : %#x\n",
679                                (uint) temp);
680         if (asus->dsdt_info) {
681                 snprintf(buf, 16, "%d", asus->dsdt_info->length);
682                 len += sprintf(page + len, "DSDT length        : %s\n", buf);
683                 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
684                 len += sprintf(page + len, "DSDT checksum      : %s\n", buf);
685                 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
686                 len += sprintf(page + len, "DSDT revision      : %s\n", buf);
687                 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
688                 len += sprintf(page + len, "OEM id             : %s\n", buf);
689                 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
690                 len += sprintf(page + len, "OEM table id       : %s\n", buf);
691                 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
692                 len += sprintf(page + len, "OEM revision       : 0x%s\n", buf);
693                 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
694                 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
695                 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
696                 len += sprintf(page + len, "ASL comp revision  : 0x%s\n", buf);
697         }
698
699         return len;
700 }
701
702 static int parse_arg(const char *buf, unsigned long count, int *val)
703 {
704         if (!count)
705                 return 0;
706         if (count > 31)
707                 return -EINVAL;
708         if (sscanf(buf, "%i", val) != 1)
709                 return -EINVAL;
710         return count;
711 }
712
713 static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
714                               const char *buf, size_t count,
715                               const char *method)
716 {
717         int rv, value;
718         int out = 0;
719
720         rv = parse_arg(buf, count, &value);
721         if (rv > 0)
722                 out = value ? 1 : 0;
723
724         if (write_acpi_int(asus->handle, method, value))
725                 return -ENODEV;
726         return rv;
727 }
728
729 /*
730  * LEDD display
731  */
732 static ssize_t show_ledd(struct device *dev,
733                          struct device_attribute *attr, char *buf)
734 {
735         struct asus_laptop *asus = dev_get_drvdata(dev);
736
737         return sprintf(buf, "0x%08x\n", asus->ledd_status);
738 }
739
740 static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
741                           const char *buf, size_t count)
742 {
743         struct asus_laptop *asus = dev_get_drvdata(dev);
744         int rv, value;
745
746         rv = parse_arg(buf, count, &value);
747         if (rv > 0) {
748                 if (write_acpi_int(asus->handle, METHOD_LEDD, value)) {
749                         pr_warn("LED display write failed\n");
750                         return -ENODEV;
751                 }
752                 asus->ledd_status = (u32) value;
753         }
754         return rv;
755 }
756
757 /*
758  * Wireless
759  */
760 static int asus_wireless_status(struct asus_laptop *asus, int mask)
761 {
762         unsigned long long status;
763         acpi_status rv = AE_OK;
764
765         if (!asus->have_rsts)
766                 return (asus->wireless_status & mask) ? 1 : 0;
767
768         rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
769                                    NULL, &status);
770         if (ACPI_FAILURE(rv)) {
771                 pr_warn("Error reading Wireless status\n");
772                 return -EINVAL;
773         }
774         return !!(status & mask);
775 }
776
777 /*
778  * WLAN
779  */
780 static int asus_wlan_set(struct asus_laptop *asus, int status)
781 {
782         if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
783                 pr_warn("Error setting wlan status to %d\n", status);
784                 return -EIO;
785         }
786         return 0;
787 }
788
789 static ssize_t show_wlan(struct device *dev,
790                          struct device_attribute *attr, char *buf)
791 {
792         struct asus_laptop *asus = dev_get_drvdata(dev);
793
794         return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
795 }
796
797 static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
798                           const char *buf, size_t count)
799 {
800         struct asus_laptop *asus = dev_get_drvdata(dev);
801
802         return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
803 }
804
805 /*
806  * Bluetooth
807  */
808 static int asus_bluetooth_set(struct asus_laptop *asus, int status)
809 {
810         if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
811                 pr_warn("Error setting bluetooth status to %d\n", status);
812                 return -EIO;
813         }
814         return 0;
815 }
816
817 static ssize_t show_bluetooth(struct device *dev,
818                               struct device_attribute *attr, char *buf)
819 {
820         struct asus_laptop *asus = dev_get_drvdata(dev);
821
822         return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
823 }
824
825 static ssize_t store_bluetooth(struct device *dev,
826                                struct device_attribute *attr, const char *buf,
827                                size_t count)
828 {
829         struct asus_laptop *asus = dev_get_drvdata(dev);
830
831         return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
832 }
833
834 /*
835  * Wimax
836  */
837 static int asus_wimax_set(struct asus_laptop *asus, int status)
838 {
839         if (write_acpi_int(asus->handle, METHOD_WIMAX, !!status)) {
840                 pr_warn("Error setting wimax status to %d\n", status);
841                 return -EIO;
842         }
843         return 0;
844 }
845
846 static ssize_t show_wimax(struct device *dev,
847                               struct device_attribute *attr, char *buf)
848 {
849         struct asus_laptop *asus = dev_get_drvdata(dev);
850
851         return sprintf(buf, "%d\n", asus_wireless_status(asus, WM_RSTS));
852 }
853
854 static ssize_t store_wimax(struct device *dev,
855                                struct device_attribute *attr, const char *buf,
856                                size_t count)
857 {
858         struct asus_laptop *asus = dev_get_drvdata(dev);
859
860         return sysfs_acpi_set(asus, buf, count, METHOD_WIMAX);
861 }
862
863 /*
864  * Wwan
865  */
866 static int asus_wwan_set(struct asus_laptop *asus, int status)
867 {
868         if (write_acpi_int(asus->handle, METHOD_WWAN, !!status)) {
869                 pr_warn("Error setting wwan status to %d\n", status);
870                 return -EIO;
871         }
872         return 0;
873 }
874
875 static ssize_t show_wwan(struct device *dev,
876                               struct device_attribute *attr, char *buf)
877 {
878         struct asus_laptop *asus = dev_get_drvdata(dev);
879
880         return sprintf(buf, "%d\n", asus_wireless_status(asus, WW_RSTS));
881 }
882
883 static ssize_t store_wwan(struct device *dev,
884                                struct device_attribute *attr, const char *buf,
885                                size_t count)
886 {
887         struct asus_laptop *asus = dev_get_drvdata(dev);
888
889         return sysfs_acpi_set(asus, buf, count, METHOD_WWAN);
890 }
891
892 /*
893  * Display
894  */
895 static void asus_set_display(struct asus_laptop *asus, int value)
896 {
897         /* no sanity check needed for now */
898         if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
899                 pr_warn("Error setting display\n");
900         return;
901 }
902
903 /*
904  * Experimental support for display switching. As of now: 1 should activate
905  * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
906  * Any combination (bitwise) of these will suffice. I never actually tested 4
907  * displays hooked up simultaneously, so be warned. See the acpi4asus README
908  * for more info.
909  */
910 static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
911                           const char *buf, size_t count)
912 {
913         struct asus_laptop *asus = dev_get_drvdata(dev);
914         int rv, value;
915
916         rv = parse_arg(buf, count, &value);
917         if (rv > 0)
918                 asus_set_display(asus, value);
919         return rv;
920 }
921
922 /*
923  * Light Sens
924  */
925 static void asus_als_switch(struct asus_laptop *asus, int value)
926 {
927         if (write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value))
928                 pr_warn("Error setting light sensor switch\n");
929         asus->light_switch = value;
930 }
931
932 static ssize_t show_lssw(struct device *dev,
933                          struct device_attribute *attr, char *buf)
934 {
935         struct asus_laptop *asus = dev_get_drvdata(dev);
936
937         return sprintf(buf, "%d\n", asus->light_switch);
938 }
939
940 static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
941                           const char *buf, size_t count)
942 {
943         struct asus_laptop *asus = dev_get_drvdata(dev);
944         int rv, value;
945
946         rv = parse_arg(buf, count, &value);
947         if (rv > 0)
948                 asus_als_switch(asus, value ? 1 : 0);
949
950         return rv;
951 }
952
953 static void asus_als_level(struct asus_laptop *asus, int value)
954 {
955         if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
956                 pr_warn("Error setting light sensor level\n");
957         asus->light_level = value;
958 }
959
960 static ssize_t show_lslvl(struct device *dev,
961                           struct device_attribute *attr, char *buf)
962 {
963         struct asus_laptop *asus = dev_get_drvdata(dev);
964
965         return sprintf(buf, "%d\n", asus->light_level);
966 }
967
968 static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
969                            const char *buf, size_t count)
970 {
971         struct asus_laptop *asus = dev_get_drvdata(dev);
972         int rv, value;
973
974         rv = parse_arg(buf, count, &value);
975         if (rv > 0) {
976                 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
977                 /* 0 <= value <= 15 */
978                 asus_als_level(asus, value);
979         }
980
981         return rv;
982 }
983
984 /*
985  * GPS
986  */
987 static int asus_gps_status(struct asus_laptop *asus)
988 {
989         unsigned long long status;
990         acpi_status rv = AE_OK;
991
992         rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
993                                    NULL, &status);
994         if (ACPI_FAILURE(rv)) {
995                 pr_warn("Error reading GPS status\n");
996                 return -ENODEV;
997         }
998         return !!status;
999 }
1000
1001 static int asus_gps_switch(struct asus_laptop *asus, int status)
1002 {
1003         const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
1004
1005         if (write_acpi_int(asus->handle, meth, 0x02))
1006                 return -ENODEV;
1007         return 0;
1008 }
1009
1010 static ssize_t show_gps(struct device *dev,
1011                         struct device_attribute *attr, char *buf)
1012 {
1013         struct asus_laptop *asus = dev_get_drvdata(dev);
1014
1015         return sprintf(buf, "%d\n", asus_gps_status(asus));
1016 }
1017
1018 static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
1019                          const char *buf, size_t count)
1020 {
1021         struct asus_laptop *asus = dev_get_drvdata(dev);
1022         int rv, value;
1023         int ret;
1024
1025         rv = parse_arg(buf, count, &value);
1026         if (rv <= 0)
1027                 return -EINVAL;
1028         ret = asus_gps_switch(asus, !!value);
1029         if (ret)
1030                 return ret;
1031         rfkill_set_sw_state(asus->gps_rfkill, !value);
1032         return rv;
1033 }
1034
1035 /*
1036  * rfkill
1037  */
1038 static int asus_gps_rfkill_set(void *data, bool blocked)
1039 {
1040         struct asus_laptop *asus = data;
1041
1042         return asus_gps_switch(asus, !blocked);
1043 }
1044
1045 static const struct rfkill_ops asus_gps_rfkill_ops = {
1046         .set_block = asus_gps_rfkill_set,
1047 };
1048
1049 static void asus_rfkill_exit(struct asus_laptop *asus)
1050 {
1051         if (asus->gps_rfkill) {
1052                 rfkill_unregister(asus->gps_rfkill);
1053                 rfkill_destroy(asus->gps_rfkill);
1054                 asus->gps_rfkill = NULL;
1055         }
1056 }
1057
1058 static int asus_rfkill_init(struct asus_laptop *asus)
1059 {
1060         int result;
1061
1062         if (acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) ||
1063             acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) ||
1064             acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
1065                 return 0;
1066
1067         asus->gps_rfkill = rfkill_alloc("asus-gps", &asus->platform_device->dev,
1068                                         RFKILL_TYPE_GPS,
1069                                         &asus_gps_rfkill_ops, asus);
1070         if (!asus->gps_rfkill)
1071                 return -EINVAL;
1072
1073         result = rfkill_register(asus->gps_rfkill);
1074         if (result) {
1075                 rfkill_destroy(asus->gps_rfkill);
1076                 asus->gps_rfkill = NULL;
1077         }
1078
1079         return result;
1080 }
1081
1082 /*
1083  * Input device (i.e. hotkeys)
1084  */
1085 static void asus_input_notify(struct asus_laptop *asus, int event)
1086 {
1087         if (asus->inputdev)
1088                 sparse_keymap_report_event(asus->inputdev, event, 1, true);
1089 }
1090
1091 static int asus_input_init(struct asus_laptop *asus)
1092 {
1093         struct input_dev *input;
1094         int error;
1095
1096         input = input_allocate_device();
1097         if (!input) {
1098                 pr_info("Unable to allocate input device\n");
1099                 return -ENOMEM;
1100         }
1101         input->name = "Asus Laptop extra buttons";
1102         input->phys = ASUS_LAPTOP_FILE "/input0";
1103         input->id.bustype = BUS_HOST;
1104         input->dev.parent = &asus->platform_device->dev;
1105
1106         error = sparse_keymap_setup(input, asus_keymap, NULL);
1107         if (error) {
1108                 pr_err("Unable to setup input device keymap\n");
1109                 goto err_free_dev;
1110         }
1111         error = input_register_device(input);
1112         if (error) {
1113                 pr_info("Unable to register input device\n");
1114                 goto err_free_keymap;
1115         }
1116
1117         asus->inputdev = input;
1118         return 0;
1119
1120 err_free_keymap:
1121         sparse_keymap_free(input);
1122 err_free_dev:
1123         input_free_device(input);
1124         return error;
1125 }
1126
1127 static void asus_input_exit(struct asus_laptop *asus)
1128 {
1129         if (asus->inputdev) {
1130                 sparse_keymap_free(asus->inputdev);
1131                 input_unregister_device(asus->inputdev);
1132         }
1133         asus->inputdev = NULL;
1134 }
1135
1136 /*
1137  * ACPI driver
1138  */
1139 static void asus_acpi_notify(struct acpi_device *device, u32 event)
1140 {
1141         struct asus_laptop *asus = acpi_driver_data(device);
1142         u16 count;
1143
1144         /* TODO Find a better way to handle events count. */
1145         count = asus->event_count[event % 128]++;
1146         acpi_bus_generate_proc_event(asus->device, event, count);
1147         acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
1148                                         dev_name(&asus->device->dev), event,
1149                                         count);
1150
1151         /* Brightness events are special */
1152         if (event >= ATKD_BR_MIN && event <= ATKD_BR_MAX) {
1153
1154                 /* Ignore them completely if the acpi video driver is used */
1155                 if (asus->backlight_device != NULL) {
1156                         /* Update the backlight device. */
1157                         asus_backlight_notify(asus);
1158                 }
1159                 return ;
1160         }
1161         asus_input_notify(asus, event);
1162 }
1163
1164 static DEVICE_ATTR(infos, S_IRUGO, show_infos, NULL);
1165 static DEVICE_ATTR(wlan, S_IRUGO | S_IWUSR, show_wlan, store_wlan);
1166 static DEVICE_ATTR(bluetooth, S_IRUGO | S_IWUSR,
1167                    show_bluetooth, store_bluetooth);
1168 static DEVICE_ATTR(wimax, S_IRUGO | S_IWUSR, show_wimax, store_wimax);
1169 static DEVICE_ATTR(wwan, S_IRUGO | S_IWUSR, show_wwan, store_wwan);
1170 static DEVICE_ATTR(display, S_IWUSR, NULL, store_disp);
1171 static DEVICE_ATTR(ledd, S_IRUGO | S_IWUSR, show_ledd, store_ledd);
1172 static DEVICE_ATTR(ls_level, S_IRUGO | S_IWUSR, show_lslvl, store_lslvl);
1173 static DEVICE_ATTR(ls_switch, S_IRUGO | S_IWUSR, show_lssw, store_lssw);
1174 static DEVICE_ATTR(gps, S_IRUGO | S_IWUSR, show_gps, store_gps);
1175
1176 static struct attribute *asus_attributes[] = {
1177         &dev_attr_infos.attr,
1178         &dev_attr_wlan.attr,
1179         &dev_attr_bluetooth.attr,
1180         &dev_attr_wimax.attr,
1181         &dev_attr_wwan.attr,
1182         &dev_attr_display.attr,
1183         &dev_attr_ledd.attr,
1184         &dev_attr_ls_level.attr,
1185         &dev_attr_ls_switch.attr,
1186         &dev_attr_gps.attr,
1187         NULL
1188 };
1189
1190 static mode_t asus_sysfs_is_visible(struct kobject *kobj,
1191                                     struct attribute *attr,
1192                                     int idx)
1193 {
1194         struct device *dev = container_of(kobj, struct device, kobj);
1195         struct platform_device *pdev = to_platform_device(dev);
1196         struct asus_laptop *asus = platform_get_drvdata(pdev);
1197         acpi_handle handle = asus->handle;
1198         bool supported;
1199
1200         if (attr == &dev_attr_wlan.attr) {
1201                 supported = !acpi_check_handle(handle, METHOD_WLAN, NULL);
1202
1203         } else if (attr == &dev_attr_bluetooth.attr) {
1204                 supported = !acpi_check_handle(handle, METHOD_BLUETOOTH, NULL);
1205
1206         } else if (attr == &dev_attr_display.attr) {
1207                 supported = !acpi_check_handle(handle, METHOD_SWITCH_DISPLAY, NULL);
1208
1209         } else if (attr == &dev_attr_wimax.attr) {
1210                 supported =
1211                         !acpi_check_handle(asus->handle, METHOD_WIMAX, NULL);
1212
1213         } else if (attr == &dev_attr_wwan.attr) {
1214                 supported = !acpi_check_handle(asus->handle, METHOD_WWAN, NULL);
1215
1216         } else if (attr == &dev_attr_ledd.attr) {
1217                 supported = !acpi_check_handle(handle, METHOD_LEDD, NULL);
1218
1219         } else if (attr == &dev_attr_ls_switch.attr ||
1220                    attr == &dev_attr_ls_level.attr) {
1221                 supported = !acpi_check_handle(handle, METHOD_ALS_CONTROL, NULL) &&
1222                             !acpi_check_handle(handle, METHOD_ALS_LEVEL, NULL);
1223         } else if (attr == &dev_attr_gps.attr) {
1224                 supported = !acpi_check_handle(handle, METHOD_GPS_ON, NULL) &&
1225                             !acpi_check_handle(handle, METHOD_GPS_OFF, NULL) &&
1226                             !acpi_check_handle(handle, METHOD_GPS_STATUS, NULL);
1227         } else {
1228                 supported = true;
1229         }
1230
1231         return supported ? attr->mode : 0;
1232 }
1233
1234
1235 static const struct attribute_group asus_attr_group = {
1236         .is_visible     = asus_sysfs_is_visible,
1237         .attrs          = asus_attributes,
1238 };
1239
1240 static int asus_platform_init(struct asus_laptop *asus)
1241 {
1242         int result;
1243
1244         asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1245         if (!asus->platform_device)
1246                 return -ENOMEM;
1247         platform_set_drvdata(asus->platform_device, asus);
1248
1249         result = platform_device_add(asus->platform_device);
1250         if (result)
1251                 goto fail_platform_device;
1252
1253         result = sysfs_create_group(&asus->platform_device->dev.kobj,
1254                                     &asus_attr_group);
1255         if (result)
1256                 goto fail_sysfs;
1257
1258         return 0;
1259
1260 fail_sysfs:
1261         platform_device_del(asus->platform_device);
1262 fail_platform_device:
1263         platform_device_put(asus->platform_device);
1264         return result;
1265 }
1266
1267 static void asus_platform_exit(struct asus_laptop *asus)
1268 {
1269         sysfs_remove_group(&asus->platform_device->dev.kobj, &asus_attr_group);
1270         platform_device_unregister(asus->platform_device);
1271 }
1272
1273 static struct platform_driver platform_driver = {
1274         .driver = {
1275                 .name = ASUS_LAPTOP_FILE,
1276                 .owner = THIS_MODULE,
1277         }
1278 };
1279
1280 /*
1281  * This function is used to initialize the context with right values. In this
1282  * method, we can make all the detection we want, and modify the asus_laptop
1283  * struct
1284  */
1285 static int asus_laptop_get_info(struct asus_laptop *asus)
1286 {
1287         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1288         union acpi_object *model = NULL;
1289         unsigned long long bsts_result, hwrs_result;
1290         char *string = NULL;
1291         acpi_status status;
1292
1293         /*
1294          * Get DSDT headers early enough to allow for differentiating between
1295          * models, but late enough to allow acpi_bus_register_driver() to fail
1296          * before doing anything ACPI-specific. Should we encounter a machine,
1297          * which needs special handling (i.e. its hotkey device has a different
1298          * HID), this bit will be moved.
1299          */
1300         status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
1301         if (ACPI_FAILURE(status))
1302                 pr_warn("Couldn't get the DSDT table header\n");
1303
1304         /* We have to write 0 on init this far for all ASUS models */
1305         if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
1306                 pr_err("Hotkey initialization failed\n");
1307                 return -ENODEV;
1308         }
1309
1310         /* This needs to be called for some laptops to init properly */
1311         status =
1312             acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
1313         if (ACPI_FAILURE(status))
1314                 pr_warn("Error calling BSTS\n");
1315         else if (bsts_result)
1316                 pr_notice("BSTS called, 0x%02x returned\n",
1317                        (uint) bsts_result);
1318
1319         /* This too ... */
1320         if (write_acpi_int(asus->handle, "CWAP", wapf))
1321                 pr_err("Error calling CWAP(%d)\n", wapf);
1322         /*
1323          * Try to match the object returned by INIT to the specific model.
1324          * Handle every possible object (or the lack of thereof) the DSDT
1325          * writers might throw at us. When in trouble, we pass NULL to
1326          * asus_model_match() and try something completely different.
1327          */
1328         if (buffer.pointer) {
1329                 model = buffer.pointer;
1330                 switch (model->type) {
1331                 case ACPI_TYPE_STRING:
1332                         string = model->string.pointer;
1333                         break;
1334                 case ACPI_TYPE_BUFFER:
1335                         string = model->buffer.pointer;
1336                         break;
1337                 default:
1338                         string = "";
1339                         break;
1340                 }
1341         }
1342         asus->name = kstrdup(string, GFP_KERNEL);
1343         if (!asus->name) {
1344                 kfree(buffer.pointer);
1345                 return -ENOMEM;
1346         }
1347
1348         if (*string)
1349                 pr_notice("  %s model detected\n", string);
1350
1351         /*
1352          * The HWRS method return informations about the hardware.
1353          * 0x80 bit is for WLAN, 0x100 for Bluetooth,
1354          * 0x40 for WWAN, 0x10 for WIMAX.
1355          * The significance of others is yet to be found.
1356          */
1357         status =
1358             acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
1359         if (!ACPI_FAILURE(status))
1360                 pr_notice("  HRWS returned %x", (int)hwrs_result);
1361
1362         if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
1363                 asus->have_rsts = true;
1364
1365         kfree(model);
1366
1367         return AE_OK;
1368 }
1369
1370 static int __devinit asus_acpi_init(struct asus_laptop *asus)
1371 {
1372         int result = 0;
1373
1374         result = acpi_bus_get_status(asus->device);
1375         if (result)
1376                 return result;
1377         if (!asus->device->status.present) {
1378                 pr_err("Hotkey device not present, aborting\n");
1379                 return -ENODEV;
1380         }
1381
1382         result = asus_laptop_get_info(asus);
1383         if (result)
1384                 return result;
1385
1386         /* WLED and BLED are on by default */
1387         if (bluetooth_status >= 0)
1388                 asus_bluetooth_set(asus, !!bluetooth_status);
1389
1390         if (wlan_status >= 0)
1391                 asus_wlan_set(asus, !!wlan_status);
1392
1393         if (wimax_status >= 0)
1394                 asus_wimax_set(asus, !!wimax_status);
1395
1396         if (wwan_status >= 0)
1397                 asus_wwan_set(asus, !!wwan_status);
1398
1399         /* Keyboard Backlight is on by default */
1400         if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
1401                 asus_kled_set(asus, 1);
1402
1403         /* LED display is off by default */
1404         asus->ledd_status = 0xFFF;
1405
1406         /* Set initial values of light sensor and level */
1407         asus->light_switch = 0; /* Default to light sensor disabled */
1408         asus->light_level = 5;  /* level 5 for sensor sensitivity */
1409
1410         if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1411             !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
1412                 asus_als_switch(asus, asus->light_switch);
1413                 asus_als_level(asus, asus->light_level);
1414         }
1415
1416         return result;
1417 }
1418
1419 static void __devinit asus_dmi_check(void)
1420 {
1421         const char *model;
1422
1423         model = dmi_get_system_info(DMI_PRODUCT_NAME);
1424         if (!model)
1425                 return;
1426
1427         /* On L1400B WLED control the sound card, don't mess with it ... */
1428         if (strncmp(model, "L1400B", 6) == 0) {
1429                 wlan_status = -1;
1430         }
1431 }
1432
1433 static bool asus_device_present;
1434
1435 static int __devinit asus_acpi_add(struct acpi_device *device)
1436 {
1437         struct asus_laptop *asus;
1438         int result;
1439
1440         pr_notice("Asus Laptop Support version %s\n",
1441                   ASUS_LAPTOP_VERSION);
1442         asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1443         if (!asus)
1444                 return -ENOMEM;
1445         asus->handle = device->handle;
1446         strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1447         strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1448         device->driver_data = asus;
1449         asus->device = device;
1450
1451         asus_dmi_check();
1452
1453         result = asus_acpi_init(asus);
1454         if (result)
1455                 goto fail_platform;
1456
1457         /*
1458          * Need platform type detection first, then the platform
1459          * device.  It is used as a parent for the sub-devices below.
1460          */
1461         asus->is_pega_lucid = asus_check_pega_lucid(asus);
1462         result = asus_platform_init(asus);
1463         if (result)
1464                 goto fail_platform;
1465
1466         if (!acpi_video_backlight_support()) {
1467                 result = asus_backlight_init(asus);
1468                 if (result)
1469                         goto fail_backlight;
1470         } else
1471                 pr_info("Backlight controlled by ACPI video driver\n");
1472
1473         result = asus_input_init(asus);
1474         if (result)
1475                 goto fail_input;
1476
1477         result = asus_led_init(asus);
1478         if (result)
1479                 goto fail_led;
1480
1481         result = asus_rfkill_init(asus);
1482         if (result)
1483                 goto fail_rfkill;
1484
1485         asus_device_present = true;
1486         return 0;
1487
1488 fail_rfkill:
1489         asus_led_exit(asus);
1490 fail_led:
1491         asus_input_exit(asus);
1492 fail_input:
1493         asus_backlight_exit(asus);
1494 fail_backlight:
1495         asus_platform_exit(asus);
1496 fail_platform:
1497         kfree(asus->name);
1498         kfree(asus);
1499
1500         return result;
1501 }
1502
1503 static int asus_acpi_remove(struct acpi_device *device, int type)
1504 {
1505         struct asus_laptop *asus = acpi_driver_data(device);
1506
1507         asus_backlight_exit(asus);
1508         asus_rfkill_exit(asus);
1509         asus_led_exit(asus);
1510         asus_input_exit(asus);
1511         asus_platform_exit(asus);
1512
1513         kfree(asus->name);
1514         kfree(asus);
1515         return 0;
1516 }
1517
1518 static const struct acpi_device_id asus_device_ids[] = {
1519         {"ATK0100", 0},
1520         {"ATK0101", 0},
1521         {"", 0},
1522 };
1523 MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1524
1525 static struct acpi_driver asus_acpi_driver = {
1526         .name = ASUS_LAPTOP_NAME,
1527         .class = ASUS_LAPTOP_CLASS,
1528         .owner = THIS_MODULE,
1529         .ids = asus_device_ids,
1530         .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1531         .ops = {
1532                 .add = asus_acpi_add,
1533                 .remove = asus_acpi_remove,
1534                 .notify = asus_acpi_notify,
1535                 },
1536 };
1537
1538 static int __init asus_laptop_init(void)
1539 {
1540         int result;
1541
1542         result = platform_driver_register(&platform_driver);
1543         if (result < 0)
1544                 return result;
1545
1546         result = acpi_bus_register_driver(&asus_acpi_driver);
1547         if (result < 0)
1548                 goto fail_acpi_driver;
1549         if (!asus_device_present) {
1550                 result = -ENODEV;
1551                 goto fail_no_device;
1552         }
1553         return 0;
1554
1555 fail_no_device:
1556         acpi_bus_unregister_driver(&asus_acpi_driver);
1557 fail_acpi_driver:
1558         platform_driver_unregister(&platform_driver);
1559         return result;
1560 }
1561
1562 static void __exit asus_laptop_exit(void)
1563 {
1564         acpi_bus_unregister_driver(&asus_acpi_driver);
1565         platform_driver_unregister(&platform_driver);
1566 }
1567
1568 module_init(asus_laptop_init);
1569 module_exit(asus_laptop_exit);