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