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