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