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