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