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