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