blob: 2349e88bbc75326e9090996e5954051e51a1306b [file] [log] [blame]
Corentin Chary85091b72007-01-26 14:04:30 +01001/*
2 * asus-laptop.c - Asus Laptop Support
3 *
4 *
5 * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
Corentin Chary8ec555c2007-03-11 10:28:03 +01006 * Copyright (C) 2006-2007 Corentin Chary
Corentin Chary85091b72007-01-26 14:04:30 +01007 *
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
Corentin Charye539c2f2007-05-06 14:47:06 +020033 * Sam Lin - GPS support
Corentin Chary85091b72007-01-26 14:04:30 +010034 */
35
Corentin Chary2fcc23d2009-06-19 14:52:03 +020036#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37
Corentin Chary85091b72007-01-26 14:04:30 +010038#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>
Corentin Chary6b7091e2007-01-26 14:04:45 +010044#include <linux/backlight.h>
45#include <linux/fb.h>
Corentin Charybe18cda2007-01-26 14:04:35 +010046#include <linux/leds.h>
Corentin Chary85091b72007-01-26 14:04:30 +010047#include <linux/platform_device.h>
48#include <acpi/acpi_drivers.h>
49#include <acpi/acpi_bus.h>
50#include <asm/uaccess.h>
Corentin Chary034ce902009-01-20 16:17:43 +010051#include <linux/input.h>
Corentin Chary85091b72007-01-26 14:04:30 +010052
Corentin Chary91687cc2009-11-28 10:32:34 +010053#define ASUS_LAPTOP_VERSION "0.42"
Corentin Chary85091b72007-01-26 14:04:30 +010054
Corentin Chary50a90c42009-11-30 21:55:12 +010055#define ASUS_LAPTOP_NAME "Asus Laptop Support"
56#define ASUS_LAPTOP_CLASS "hotkey"
57#define ASUS_LAPTOP_DEVICE_NAME "Hotkey"
58#define ASUS_LAPTOP_FILE KBUILD_MODNAME
59#define ASUS_LAPTOP_PREFIX "\\_SB.ATKD."
Corentin Chary85091b72007-01-26 14:04:30 +010060
Corentin Chary85091b72007-01-26 14:04:30 +010061MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
Corentin Chary50a90c42009-11-30 21:55:12 +010062MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
Corentin Chary85091b72007-01-26 14:04:30 +010063MODULE_LICENSE("GPL");
64
Corentin Charybe966662009-08-29 10:28:29 +020065/*
66 * WAPF defines the behavior of the Fn+Fx wlan key
Corentin Chary185e5af2007-03-11 10:27:33 +010067 * The significance of values is yet to be found, but
68 * most of the time:
69 * 0x0 will do nothing
70 * 0x1 will allow to control the device with Fn+Fx key.
71 * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
72 * 0x5 like 0x1 or 0x4
73 * So, if something doesn't work as you want, just try other values =)
74 */
75static uint wapf = 1;
76module_param(wapf, uint, 0644);
77MODULE_PARM_DESC(wapf, "WAPF value");
78
Corentin Chary0e875f42010-01-10 20:49:26 +010079static uint wireless_status = 1;
80static uint bluetooth_status = 1;
81
82module_param(wireless_status, uint, 0644);
83MODULE_PARM_DESC(wireless_status, "Set the wireless status on boot "
84 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
85 "default is 1");
86
87module_param(bluetooth_status, uint, 0644);
88MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
89 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
90 "default is 1");
91
Corentin Charybe4ee822009-12-06 16:27:09 +010092/*
93 * Some events we use, same for all Asus
94 */
95#define ATKD_BR_UP 0x10
96#define ATKD_BR_DOWN 0x20
97#define ATKD_LCD_ON 0x33
98#define ATKD_LCD_OFF 0x34
99
100/*
101 * Known bits returned by \_SB.ATKD.HWRS
102 */
103#define WL_HWRS 0x80
104#define BT_HWRS 0x100
105
106/*
107 * Flags for hotk status
108 * WL_ON and BT_ON are also used for wireless_status()
109 */
Corentin Chary17e78f62010-01-13 22:21:33 +0100110#define WL_RSTS 0x01 /* internal Wifi */
111#define BT_RSTS 0x02 /* internal Bluetooth */
Corentin Charybe4ee822009-12-06 16:27:09 +0100112
Corentin Chary85091b72007-01-26 14:04:30 +0100113#define ASUS_HANDLE(object, paths...) \
114 static acpi_handle object##_handle = NULL; \
115 static char *object##_paths[] = { paths }
116
Corentin Charybe18cda2007-01-26 14:04:35 +0100117/* LED */
Corentin Chary50a90c42009-11-30 21:55:12 +0100118ASUS_HANDLE(mled_set, ASUS_LAPTOP_PREFIX "MLED");
119ASUS_HANDLE(tled_set, ASUS_LAPTOP_PREFIX "TLED");
120ASUS_HANDLE(rled_set, ASUS_LAPTOP_PREFIX "RLED"); /* W1JC */
121ASUS_HANDLE(pled_set, ASUS_LAPTOP_PREFIX "PLED"); /* A7J */
122ASUS_HANDLE(gled_set, ASUS_LAPTOP_PREFIX "GLED"); /* G1, G2 (probably) */
Corentin Charybe18cda2007-01-26 14:04:35 +0100123
Corentin Chary722ad972007-01-26 14:04:55 +0100124/* LEDD */
Corentin Chary50a90c42009-11-30 21:55:12 +0100125ASUS_HANDLE(ledd_set, ASUS_LAPTOP_PREFIX "SLCM");
Corentin Chary722ad972007-01-26 14:04:55 +0100126
Corentin Charybe966662009-08-29 10:28:29 +0200127/*
128 * Bluetooth and WLAN
Corentin Chary4564de12007-01-26 14:04:40 +0100129 * WLED and BLED are not handled like other XLED, because in some dsdt
130 * they also control the WLAN/Bluetooth device.
131 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100132ASUS_HANDLE(wl_switch, ASUS_LAPTOP_PREFIX "WLED");
133ASUS_HANDLE(bt_switch, ASUS_LAPTOP_PREFIX "BLED");
134ASUS_HANDLE(wireless_status, ASUS_LAPTOP_PREFIX "RSTS"); /* All new models */
Corentin Chary4564de12007-01-26 14:04:40 +0100135
Corentin Chary6b7091e2007-01-26 14:04:45 +0100136/* Brightness */
Corentin Chary50a90c42009-11-30 21:55:12 +0100137ASUS_HANDLE(brightness_set, ASUS_LAPTOP_PREFIX "SPLV");
138ASUS_HANDLE(brightness_get, ASUS_LAPTOP_PREFIX "GPLV");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100139
140/* Backlight */
Len Brown8def05f2007-01-30 01:46:43 -0500141ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
142 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
143 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
144 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
145 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
Torsten Krah4d0b856e2008-10-17 09:47:57 +0200146 "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
Len Brown8def05f2007-01-30 01:46:43 -0500147 "\\_SB.PCI0.PX40.Q10", /* S1x */
148 "\\Q10"); /* A2x, L2D, L3D, M2E */
Corentin Chary6b7091e2007-01-26 14:04:45 +0100149
Corentin Chary78127b42007-01-26 14:04:49 +0100150/* Display */
Corentin Chary50a90c42009-11-30 21:55:12 +0100151ASUS_HANDLE(display_set, ASUS_LAPTOP_PREFIX "SDSP");
Corentin Charybe966662009-08-29 10:28:29 +0200152ASUS_HANDLE(display_get,
153 /* A6B, A6K A6R A7D F3JM L4R M6R A3G M6A M6V VX-1 V6J V6V W3Z */
154 "\\_SB.PCI0.P0P1.VGA.GETD",
155 /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V S5A M5A z33A W1Jc W2V G1 */
156 "\\_SB.PCI0.P0P2.VGA.GETD",
157 /* A6V A6Q */
158 "\\_SB.PCI0.P0P3.VGA.GETD",
159 /* A6T, A6M */
160 "\\_SB.PCI0.P0PA.VGA.GETD",
161 /* L3C */
162 "\\_SB.PCI0.PCI1.VGAC.NMAP",
163 /* Z96F */
164 "\\_SB.PCI0.VGA.GETD",
165 /* A2D */
166 "\\ACTD",
167 /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
168 "\\ADVG",
169 /* P30 */
170 "\\DNXT",
171 /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
172 "\\INFB",
173 /* A3F A6F A3N A3L M6N W3N W6A */
174 "\\SSTE");
Corentin Chary78127b42007-01-26 14:04:49 +0100175
Corentin Chary50a90c42009-11-30 21:55:12 +0100176ASUS_HANDLE(ls_switch, ASUS_LAPTOP_PREFIX "ALSC"); /* Z71A Z71V */
177ASUS_HANDLE(ls_level, ASUS_LAPTOP_PREFIX "ALSL"); /* Z71A Z71V */
Corentin Chary8b857352007-01-26 14:04:58 +0100178
Corentin Charye539c2f2007-05-06 14:47:06 +0200179/* GPS */
180/* R2H use different handle for GPS on/off */
Corentin Chary50a90c42009-11-30 21:55:12 +0100181ASUS_HANDLE(gps_on, ASUS_LAPTOP_PREFIX "SDON"); /* R2H */
182ASUS_HANDLE(gps_off, ASUS_LAPTOP_PREFIX "SDOF"); /* R2H */
183ASUS_HANDLE(gps_status, ASUS_LAPTOP_PREFIX "GPST");
Corentin Charye539c2f2007-05-06 14:47:06 +0200184
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000185/* Keyboard light */
Corentin Chary50a90c42009-11-30 21:55:12 +0100186ASUS_HANDLE(kled_set, ASUS_LAPTOP_PREFIX "SLKB");
187ASUS_HANDLE(kled_get, ASUS_LAPTOP_PREFIX "GLKB");
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000188
Corentin Chary85091b72007-01-26 14:04:30 +0100189/*
Corentin Chary9129d142009-12-01 22:39:41 +0100190 * Define a specific led structure to keep the main structure clean
191 */
192#define ASUS_DEFINE_LED(object) \
193 int object##_wk; \
194 struct work_struct object##_work; \
195 struct led_classdev object;
196
197
198#define led_to_asus(led_cdev, led) \
199 container_of(container_of(led_cdev, struct asus_laptop_leds, \
200 led), \
201 struct asus_laptop, leds)
202#define work_to_asus(work, led) \
203 container_of(container_of(work, struct asus_laptop_leds, \
204 led##_work), \
205 struct asus_laptop, leds)
206
207struct asus_laptop_leds {
208 ASUS_DEFINE_LED(mled)
209 ASUS_DEFINE_LED(tled)
210 ASUS_DEFINE_LED(rled)
211 ASUS_DEFINE_LED(pled)
212 ASUS_DEFINE_LED(gled)
213 ASUS_DEFINE_LED(kled)
214 struct workqueue_struct *workqueue;
215};
216
217/*
Corentin Chary85091b72007-01-26 14:04:30 +0100218 * This is the main structure, we can use it to store anything interesting
219 * about the hotk device
220 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100221struct asus_laptop {
Corentin Charybe966662009-08-29 10:28:29 +0200222 char *name; /* laptop name */
Corentin Chary600ad522009-11-30 21:42:42 +0100223
Corentin Chary7c247642009-11-30 22:13:54 +0100224 struct acpi_table_header *dsdt_info;
Corentin Chary600ad522009-11-30 21:42:42 +0100225 struct platform_device *platform_device;
Corentin Chary7c247642009-11-30 22:13:54 +0100226 struct acpi_device *device; /* the device we are in */
227 struct backlight_device *backlight_device;
Corentin Chary9129d142009-12-01 22:39:41 +0100228
Corentin Chary7c247642009-11-30 22:13:54 +0100229 struct input_dev *inputdev;
Corentin Chary9129d142009-12-01 22:39:41 +0100230 struct key_entry *keymap;
231
232 struct asus_laptop_leds leds;
Corentin Chary7c247642009-11-30 22:13:54 +0100233
Corentin Charyaa9df932010-01-13 21:49:10 +0100234 int wireless_status;
235 bool have_rsts;
Corentin Chary3e68ae72010-01-13 22:10:39 +0100236 int lcd_state;
Corentin Charyaa9df932010-01-13 21:49:10 +0100237
Corentin Charybe966662009-08-29 10:28:29 +0200238 acpi_handle handle; /* the handle of the hotk device */
Corentin Charybe966662009-08-29 10:28:29 +0200239 u32 ledd_status; /* status of the LED display */
240 u8 light_level; /* light sensor level */
241 u8 light_switch; /* light sensor switch value */
242 u16 event_count[128]; /* count for each event TODO make this better */
Corentin Chary034ce902009-01-20 16:17:43 +0100243 u16 *keycode_map;
Corentin Chary85091b72007-01-26 14:04:30 +0100244};
245
Corentin Chary034ce902009-01-20 16:17:43 +0100246struct key_entry {
247 char type;
248 u8 code;
249 u16 keycode;
250};
251
252enum { KE_KEY, KE_END };
253
Corentin Chary9129d142009-12-01 22:39:41 +0100254static const struct key_entry asus_keymap[] = {
Ike Panhc14f8af32009-12-03 07:45:11 +0000255 {KE_KEY, 0x02, KEY_SCREENLOCK},
256 {KE_KEY, 0x05, KEY_WLAN},
257 {KE_KEY, 0x08, KEY_F13},
258 {KE_KEY, 0x17, KEY_ZOOM},
259 {KE_KEY, 0x1f, KEY_BATTERY},
Corentin Chary034ce902009-01-20 16:17:43 +0100260 {KE_KEY, 0x30, KEY_VOLUMEUP},
261 {KE_KEY, 0x31, KEY_VOLUMEDOWN},
262 {KE_KEY, 0x32, KEY_MUTE},
263 {KE_KEY, 0x33, KEY_SWITCHVIDEOMODE},
264 {KE_KEY, 0x34, KEY_SWITCHVIDEOMODE},
265 {KE_KEY, 0x40, KEY_PREVIOUSSONG},
266 {KE_KEY, 0x41, KEY_NEXTSONG},
Corentin Chary309f5fb2009-04-27 09:23:42 +0200267 {KE_KEY, 0x43, KEY_STOPCD},
Corentin Chary034ce902009-01-20 16:17:43 +0100268 {KE_KEY, 0x45, KEY_PLAYPAUSE},
Corentin Chary4644d0e2009-08-28 12:56:52 +0000269 {KE_KEY, 0x4c, KEY_MEDIA},
Corentin Chary034ce902009-01-20 16:17:43 +0100270 {KE_KEY, 0x50, KEY_EMAIL},
271 {KE_KEY, 0x51, KEY_WWW},
Corentin Chary0aa20f72009-08-28 12:56:53 +0000272 {KE_KEY, 0x55, KEY_CALC},
Corentin Chary309f5fb2009-04-27 09:23:42 +0200273 {KE_KEY, 0x5C, KEY_SCREENLOCK}, /* Screenlock */
Corentin Chary034ce902009-01-20 16:17:43 +0100274 {KE_KEY, 0x5D, KEY_WLAN},
Corentin Charyf6413752009-08-28 12:56:48 +0000275 {KE_KEY, 0x5E, KEY_WLAN},
276 {KE_KEY, 0x5F, KEY_WLAN},
277 {KE_KEY, 0x60, KEY_SWITCHVIDEOMODE},
Corentin Chary034ce902009-01-20 16:17:43 +0100278 {KE_KEY, 0x61, KEY_SWITCHVIDEOMODE},
Ike Panhc14f8af32009-12-03 07:45:11 +0000279 {KE_KEY, 0x62, KEY_SWITCHVIDEOMODE},
280 {KE_KEY, 0x63, KEY_SWITCHVIDEOMODE},
Corentin Chary16851f92009-12-03 07:45:10 +0000281 {KE_KEY, 0x6B, KEY_F13}, /* Lock Touchpad */
Corentin Chary034ce902009-01-20 16:17:43 +0100282 {KE_KEY, 0x82, KEY_CAMERA},
Corentin Chary1f0233e2009-12-03 07:45:12 +0000283 {KE_KEY, 0x88, KEY_WLAN },
Corentin Chary309f5fb2009-04-27 09:23:42 +0200284 {KE_KEY, 0x8A, KEY_PROG1},
Corentin Chary034ce902009-01-20 16:17:43 +0100285 {KE_KEY, 0x95, KEY_MEDIA},
286 {KE_KEY, 0x99, KEY_PHONE},
Corentin Charydc795262009-08-28 12:56:51 +0000287 {KE_KEY, 0xc4, KEY_KBDILLUMUP},
288 {KE_KEY, 0xc5, KEY_KBDILLUMDOWN},
Corentin Chary034ce902009-01-20 16:17:43 +0100289 {KE_END, 0},
290};
291
Corentin Chary85091b72007-01-26 14:04:30 +0100292/*
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 *
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100297 * returns 0 if write is successful, -1 else.
Corentin Chary85091b72007-01-26 14:04:30 +0100298 */
Corentin Charyd8c67322009-11-28 10:27:51 +0100299static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
300 struct acpi_buffer *output)
Corentin Chary85091b72007-01-26 14:04:30 +0100301{
Corentin Charybe966662009-08-29 10:28:29 +0200302 struct acpi_object_list params; /* list of input parameters (an int) */
303 union acpi_object in_obj; /* the only param we use */
Corentin Chary85091b72007-01-26 14:04:30 +0100304 acpi_status status;
305
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100306 if (!handle)
307 return 0;
308
Corentin Chary85091b72007-01-26 14:04:30 +0100309 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);
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100315 if (status == AE_OK)
316 return 0;
317 else
318 return -1;
Corentin Chary85091b72007-01-26 14:04:30 +0100319}
320
Corentin Charyd8c67322009-11-28 10:27:51 +0100321static 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
Corentin Chary17e78f62010-01-13 22:21:33 +0100326/* Generic LED function */
327static void asus_led_set(struct asus_laptop *asus, acpi_handle handle,
328 int value)
Corentin Charybe18cda2007-01-26 14:04:35 +0100329{
Corentin Chary17e78f62010-01-13 22:21:33 +0100330 if (handle == mled_set_handle)
331 value = !value;
332 else if (handle == gled_set_handle)
333 value = !value + 1;
334 else
335 value = !!value;
Corentin Charybe18cda2007-01-26 14:04:35 +0100336
Corentin Chary17e78f62010-01-13 22:21:33 +0100337 write_acpi_int(handle, NULL, value);
Corentin Charybe18cda2007-01-26 14:04:35 +0100338}
339
Corentin Charybe4ee822009-12-06 16:27:09 +0100340/*
341 * LEDs
342 */
343#define ASUS_LED(object, ledname, max) \
344 static void object##_led_set(struct led_classdev *led_cdev, \
345 enum led_brightness value); \
346 static enum led_brightness object##_led_get( \
347 struct led_classdev *led_cdev); \
348 static void object##_led_update(struct work_struct *ignored); \
349 static struct led_classdev object##_led = { \
350 .name = "asus::" ledname, \
351 .brightness_set = object##_led_set, \
352 .brightness_get = object##_led_get, \
353 .max_brightness = max \
354 }
355
356ASUS_LED(mled, "mail", 1);
357ASUS_LED(tled, "touchpad", 1);
358ASUS_LED(rled, "record", 1);
359ASUS_LED(pled, "phone", 1);
360ASUS_LED(gled, "gaming", 1);
361ASUS_LED(kled, "kbd_backlight", 3);
362
Corentin Charybe18cda2007-01-26 14:04:35 +0100363/* /sys/class/led handlers */
Corentin Chary935ffee2007-03-11 10:26:12 +0100364#define ASUS_LED_HANDLER(object, mask) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100365 static void object##_led_set(struct led_classdev *led_cdev, \
366 enum led_brightness value) \
367 { \
Corentin Chary9129d142009-12-01 22:39:41 +0100368 struct asus_laptop *asus = \
369 led_to_asus(led_cdev, object); \
370 \
371 asus->leds.object##_wk = (value > 0) ? 1 : 0; \
372 queue_work(asus->leds.workqueue, \
373 &asus->leds.object##_work); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100374 } \
Corentin Chary9129d142009-12-01 22:39:41 +0100375 static void object##_led_update(struct work_struct *work) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100376 { \
Corentin Chary9129d142009-12-01 22:39:41 +0100377 struct asus_laptop *asus = work_to_asus(work, object); \
378 \
379 int value = asus->leds.object##_wk; \
Corentin Chary17e78f62010-01-13 22:21:33 +0100380 asus_led_set(asus, object##_set_handle, value); \
Corentin Charyabfa57e2009-08-28 12:56:47 +0000381 } \
382 static enum led_brightness object##_led_get( \
383 struct led_classdev *led_cdev) \
384 { \
385 return led_cdev->brightness; \
Corentin Charybe18cda2007-01-26 14:04:35 +0100386 }
387
Corentin Chary935ffee2007-03-11 10:26:12 +0100388ASUS_LED_HANDLER(mled, MLED_ON);
389ASUS_LED_HANDLER(pled, PLED_ON);
390ASUS_LED_HANDLER(rled, RLED_ON);
391ASUS_LED_HANDLER(tled, TLED_ON);
Corentin Charyfdd8d082007-03-11 10:26:48 +0100392ASUS_LED_HANDLER(gled, GLED_ON);
Corentin Charybe18cda2007-01-26 14:04:35 +0100393
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000394/*
Corentin Charybe4ee822009-12-06 16:27:09 +0100395 * Keyboard backlight (also a LED)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000396 */
397static int get_kled_lvl(void)
398{
399 unsigned long long kblv;
400 struct acpi_object_list params;
401 union acpi_object in_obj;
402 acpi_status rv;
403
404 params.count = 1;
405 params.pointer = &in_obj;
406 in_obj.type = ACPI_TYPE_INTEGER;
407 in_obj.integer.value = 2;
408
409 rv = acpi_evaluate_integer(kled_get_handle, NULL, &params, &kblv);
410 if (ACPI_FAILURE(rv)) {
411 pr_warning("Error reading kled level\n");
412 return 0;
413 }
414 return kblv;
415}
416
Corentin Chary9129d142009-12-01 22:39:41 +0100417static int set_kled_lvl(struct asus_laptop *asus, int kblv)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000418{
419 if (kblv > 0)
420 kblv = (1 << 7) | (kblv & 0x7F);
421 else
422 kblv = 0;
423
Corentin Charyd8c67322009-11-28 10:27:51 +0100424 if (write_acpi_int(kled_set_handle, NULL, kblv)) {
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000425 pr_warning("Keyboard LED display write failed\n");
426 return -EINVAL;
427 }
428 return 0;
429}
430
431static void kled_led_set(struct led_classdev *led_cdev,
432 enum led_brightness value)
433{
Corentin Chary9129d142009-12-01 22:39:41 +0100434 struct asus_laptop *asus = led_to_asus(led_cdev, kled);
435
436 asus->leds.kled_wk = value;
437 queue_work(asus->leds.workqueue, &asus->leds.kled_work);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000438}
439
Corentin Chary9129d142009-12-01 22:39:41 +0100440static void kled_led_update(struct work_struct *work)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000441{
Corentin Chary9129d142009-12-01 22:39:41 +0100442 struct asus_laptop *asus = work_to_asus(work, kled);
443
444 set_kled_lvl(asus, asus->leds.kled_wk);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000445}
446
447static enum led_brightness kled_led_get(struct led_classdev *led_cdev)
448{
449 return get_kled_lvl();
450}
451
Corentin Charybe4ee822009-12-06 16:27:09 +0100452#define ASUS_LED_UNREGISTER(object) \
453 if (object##_led.dev) \
454 led_classdev_unregister(&object##_led)
455
456static void asus_led_exit(struct asus_laptop *asus)
457{
458 ASUS_LED_UNREGISTER(mled);
459 ASUS_LED_UNREGISTER(tled);
460 ASUS_LED_UNREGISTER(pled);
461 ASUS_LED_UNREGISTER(rled);
462 ASUS_LED_UNREGISTER(gled);
463 ASUS_LED_UNREGISTER(kled);
464 if (asus->leds.workqueue) {
465 destroy_workqueue(asus->leds.workqueue);
466 asus->leds.workqueue = NULL;
467 }
468}
469
470/* Ugly macro, need to fix that later */
471#define ASUS_LED_REGISTER(asus, object, _name, max) \
472 do { \
473 struct led_classdev *ldev = &asus->leds.object; \
474 if (!object##_set_handle) \
475 break ; \
476 \
477 INIT_WORK(&asus->leds.object##_work, object##_led_update); \
478 ldev->name = "asus::" _name; \
479 ldev->brightness_set = object##_led_set; \
480 ldev->max_brightness = max; \
481 rv = led_classdev_register(&asus->platform_device->dev, ldev); \
482 if (rv) \
483 goto error; \
484 } while (0)
485
486static int asus_led_init(struct asus_laptop *asus)
487{
488 int rv;
489
490 /*
491 * Functions that actually update the LED's are called from a
492 * workqueue. By doing this as separate work rather than when the LED
493 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
494 * potentially bad time, such as a timer interrupt.
495 */
496 asus->leds.workqueue = create_singlethread_workqueue("led_workqueue");
497 if (!asus->leds.workqueue)
498 return -ENOMEM;
499
500 ASUS_LED_REGISTER(asus, mled, "mail", 1);
501 ASUS_LED_REGISTER(asus, tled, "touchpad", 1);
502 ASUS_LED_REGISTER(asus, rled, "record", 1);
503 ASUS_LED_REGISTER(asus, pled, "phone", 1);
504 ASUS_LED_REGISTER(asus, gled, "gaming", 1);
505 if (kled_set_handle && kled_get_handle)
506 ASUS_LED_REGISTER(asus, kled, "kbd_backlight", 3);
507error:
508 if (rv)
509 asus_led_exit(asus);
510 return rv;
511}
512
513/*
514 * Backlight device
515 */
Corentin Chary3e68ae72010-01-13 22:10:39 +0100516static int asus_lcd_status(struct asus_laptop *asus)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100517{
Corentin Chary3e68ae72010-01-13 22:10:39 +0100518 return asus->lcd_state;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100519}
520
Corentin Chary3e68ae72010-01-13 22:10:39 +0100521static int asus_lcd_set(struct asus_laptop *asus, int value)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100522{
523 int lcd = 0;
524 acpi_status status = 0;
525
Corentin Chary3e68ae72010-01-13 22:10:39 +0100526 lcd = !!value;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100527
Corentin Chary3e68ae72010-01-13 22:10:39 +0100528 if (lcd == asus_lcd_status(asus))
Corentin Chary6b7091e2007-01-26 14:04:45 +0100529 return 0;
530
Corentin Chary3e68ae72010-01-13 22:10:39 +0100531 if (!lcd_switch_handle)
532 return -ENODEV;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100533
Corentin Chary3e68ae72010-01-13 22:10:39 +0100534 status = acpi_evaluate_object(lcd_switch_handle,
535 NULL, NULL, NULL);
536
537 if (ACPI_FAILURE(status)) {
538 pr_warning("Error switching LCD\n");
539 return -ENODEV;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100540 }
541
Corentin Chary3e68ae72010-01-13 22:10:39 +0100542 asus->lcd_state = lcd;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100543 return 0;
544}
545
Corentin Chary9129d142009-12-01 22:39:41 +0100546static void lcd_blank(struct asus_laptop *asus, int blank)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100547{
Corentin Chary7c247642009-11-30 22:13:54 +0100548 struct backlight_device *bd = asus->backlight_device;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100549
Corentin Chary3e68ae72010-01-13 22:10:39 +0100550 asus->lcd_state = (blank == FB_BLANK_UNBLANK);
551
Len Brown8def05f2007-01-30 01:46:43 -0500552 if (bd) {
Richard Purdie599a52d2007-02-10 23:07:48 +0000553 bd->props.power = blank;
Richard Purdie28ee0862007-02-08 22:25:09 +0000554 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100555 }
556}
557
558static int read_brightness(struct backlight_device *bd)
559{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400560 unsigned long long value;
Corentin Chary9a816852007-03-11 10:25:38 +0100561 acpi_status rv = AE_OK;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100562
Corentin Chary9a816852007-03-11 10:25:38 +0100563 rv = acpi_evaluate_integer(brightness_get_handle, NULL, NULL, &value);
564 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200565 pr_warning("Error reading brightness\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100566
567 return value;
568}
569
570static int set_brightness(struct backlight_device *bd, int value)
571{
Corentin Charyd8c67322009-11-28 10:27:51 +0100572 if (write_acpi_int(brightness_set_handle, NULL, value)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200573 pr_warning("Error changing brightness\n");
Corentin Charye5b50f62009-11-28 10:19:55 +0100574 return -EIO;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100575 }
Corentin Charye5b50f62009-11-28 10:19:55 +0100576 return 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100577}
578
579static int update_bl_status(struct backlight_device *bd)
580{
Corentin Chary9129d142009-12-01 22:39:41 +0100581 struct asus_laptop *asus = bl_get_data(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100582 int rv;
Richard Purdie599a52d2007-02-10 23:07:48 +0000583 int value = bd->props.brightness;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100584
585 rv = set_brightness(bd, value);
Len Brown8def05f2007-01-30 01:46:43 -0500586 if (rv)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100587 return rv;
588
Richard Purdie599a52d2007-02-10 23:07:48 +0000589 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
Corentin Chary3e68ae72010-01-13 22:10:39 +0100590 return asus_lcd_set(asus, value);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100591}
592
Corentin Charybe4ee822009-12-06 16:27:09 +0100593static struct backlight_ops asusbl_ops = {
594 .get_brightness = read_brightness,
595 .update_status = update_bl_status,
596};
597
598static int asus_backlight_init(struct asus_laptop *asus)
599{
600 struct backlight_device *bd;
601 struct device *dev = &asus->platform_device->dev;
602
603 if (brightness_set_handle && lcd_switch_handle) {
604 bd = backlight_device_register(ASUS_LAPTOP_FILE, dev,
605 asus, &asusbl_ops);
606 if (IS_ERR(bd)) {
607 pr_err("Could not register asus backlight device\n");
608 asus->backlight_device = NULL;
609 return PTR_ERR(bd);
610 }
611
612 asus->backlight_device = bd;
613
614 bd->props.max_brightness = 15;
615 bd->props.brightness = read_brightness(NULL);
616 bd->props.power = FB_BLANK_UNBLANK;
617 backlight_update_status(bd);
618 }
619 return 0;
620}
621
622static void asus_backlight_exit(struct asus_laptop *asus)
623{
624 if (asus->backlight_device)
625 backlight_device_unregister(asus->backlight_device);
626}
627
Corentin Chary85091b72007-01-26 14:04:30 +0100628/*
629 * Platform device handlers
630 */
631
632/*
633 * We write our info in page, we begin at offset off and cannot write more
634 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
635 * number of bytes written in page
636 */
637static ssize_t show_infos(struct device *dev,
Len Brown8def05f2007-01-30 01:46:43 -0500638 struct device_attribute *attr, char *page)
Corentin Chary85091b72007-01-26 14:04:30 +0100639{
Corentin Chary9129d142009-12-01 22:39:41 +0100640 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary85091b72007-01-26 14:04:30 +0100641 int len = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400642 unsigned long long temp;
Corentin Charybe966662009-08-29 10:28:29 +0200643 char buf[16]; /* enough for all info */
Corentin Chary9a816852007-03-11 10:25:38 +0100644 acpi_status rv = AE_OK;
645
Corentin Chary85091b72007-01-26 14:04:30 +0100646 /*
647 * We use the easy way, we don't care of off and count, so we don't set eof
648 * to 1
649 */
650
Corentin Chary50a90c42009-11-30 21:55:12 +0100651 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
652 len += sprintf(page + len, "Model reference : %s\n", asus->name);
Corentin Chary85091b72007-01-26 14:04:30 +0100653 /*
654 * The SFUN method probably allows the original driver to get the list
655 * of features supported by a given model. For now, 0x0100 or 0x0800
656 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
657 * The significance of others is yet to be found.
658 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100659 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100660 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000661 len += sprintf(page + len, "SFUN value : %#x\n",
662 (uint) temp);
663 /*
664 * The HWRS method return informations about the hardware.
665 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
666 * The significance of others is yet to be found.
667 * If we don't find the method, we assume the device are present.
668 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100669 rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
Corentin Chary1d4a3802009-08-28 12:56:46 +0000670 if (!ACPI_FAILURE(rv))
671 len += sprintf(page + len, "HRWS value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100672 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100673 /*
674 * Another value for userspace: the ASYM method returns 0x02 for
675 * battery low and 0x04 for battery critical, its readings tend to be
676 * more accurate than those provided by _BST.
677 * Note: since not all the laptops provide this method, errors are
678 * silently ignored.
679 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100680 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100681 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000682 len += sprintf(page + len, "ASYM value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100683 (uint) temp);
Corentin Chary7c247642009-11-30 22:13:54 +0100684 if (asus->dsdt_info) {
685 snprintf(buf, 16, "%d", asus->dsdt_info->length);
Corentin Chary85091b72007-01-26 14:04:30 +0100686 len += sprintf(page + len, "DSDT length : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100687 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
Corentin Chary85091b72007-01-26 14:04:30 +0100688 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100689 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100690 len += sprintf(page + len, "DSDT revision : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100691 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100692 len += sprintf(page + len, "OEM id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100693 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100694 len += sprintf(page + len, "OEM table id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100695 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100696 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100697 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100698 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100699 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100700 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
701 }
702
703 return len;
704}
705
706static int parse_arg(const char *buf, unsigned long count, int *val)
707{
708 if (!count)
709 return 0;
710 if (count > 31)
711 return -EINVAL;
712 if (sscanf(buf, "%i", val) != 1)
713 return -EINVAL;
714 return count;
715}
716
Corentin Chary17e78f62010-01-13 22:21:33 +0100717static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
718 const char *buf, size_t count,
719 acpi_handle handle)
Corentin Chary4564de12007-01-26 14:04:40 +0100720{
721 int rv, value;
722 int out = 0;
723
724 rv = parse_arg(buf, count, &value);
725 if (rv > 0)
726 out = value ? 1 : 0;
727
Corentin Chary17e78f62010-01-13 22:21:33 +0100728 if (write_acpi_int(handle, NULL, value))
729 return -ENODEV;
Corentin Chary4564de12007-01-26 14:04:40 +0100730 return rv;
731}
732
733/*
Corentin Chary722ad972007-01-26 14:04:55 +0100734 * LEDD display
735 */
736static ssize_t show_ledd(struct device *dev,
737 struct device_attribute *attr, char *buf)
738{
Corentin Chary9129d142009-12-01 22:39:41 +0100739 struct asus_laptop *asus = dev_get_drvdata(dev);
740
Corentin Chary50a90c42009-11-30 21:55:12 +0100741 return sprintf(buf, "0x%08x\n", asus->ledd_status);
Corentin Chary722ad972007-01-26 14:04:55 +0100742}
743
744static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
745 const char *buf, size_t count)
746{
Corentin Chary9129d142009-12-01 22:39:41 +0100747 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary722ad972007-01-26 14:04:55 +0100748 int rv, value;
749
750 rv = parse_arg(buf, count, &value);
751 if (rv > 0) {
Corentin Charyd8c67322009-11-28 10:27:51 +0100752 if (write_acpi_int(ledd_set_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200753 pr_warning("LED display write failed\n");
Corentin Chary722ad972007-01-26 14:04:55 +0100754 else
Corentin Chary50a90c42009-11-30 21:55:12 +0100755 asus->ledd_status = (u32) value;
Corentin Chary722ad972007-01-26 14:04:55 +0100756 }
757 return rv;
758}
759
760/*
Corentin Charyaa9df932010-01-13 21:49:10 +0100761 * Wireless
762 */
763static int asus_wireless_status(struct asus_laptop *asus, int mask)
764{
765 unsigned long long status;
766 acpi_status rv = AE_OK;
767
768 if (!asus->have_rsts)
769 return (asus->wireless_status & mask) ? 1 : 0;
770
771 rv = acpi_evaluate_integer(wireless_status_handle, NULL, NULL, &status);
772 if (ACPI_FAILURE(rv)) {
773 pr_warning("Error reading Wireless status\n");
774 return -EINVAL;
775 }
776 return !!(status & mask);
777}
778
779/*
Corentin Chary4564de12007-01-26 14:04:40 +0100780 * WLAN
781 */
782static ssize_t show_wlan(struct device *dev,
783 struct device_attribute *attr, char *buf)
784{
Corentin Chary9129d142009-12-01 22:39:41 +0100785 struct asus_laptop *asus = dev_get_drvdata(dev);
786
Corentin Chary17e78f62010-01-13 22:21:33 +0100787 return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100788}
789
790static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
791 const char *buf, size_t count)
792{
Corentin Chary9129d142009-12-01 22:39:41 +0100793 struct asus_laptop *asus = dev_get_drvdata(dev);
794
Corentin Chary17e78f62010-01-13 22:21:33 +0100795 return sysfs_acpi_set(asus, buf, count, wl_switch_handle);
Corentin Chary4564de12007-01-26 14:04:40 +0100796}
797
798/*
799 * Bluetooth
800 */
801static ssize_t show_bluetooth(struct device *dev,
802 struct device_attribute *attr, char *buf)
803{
Corentin Chary9129d142009-12-01 22:39:41 +0100804 struct asus_laptop *asus = dev_get_drvdata(dev);
805
Corentin Chary17e78f62010-01-13 22:21:33 +0100806 return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100807}
808
Len Brown8def05f2007-01-30 01:46:43 -0500809static ssize_t store_bluetooth(struct device *dev,
810 struct device_attribute *attr, const char *buf,
811 size_t count)
Corentin Chary4564de12007-01-26 14:04:40 +0100812{
Corentin Chary9129d142009-12-01 22:39:41 +0100813 struct asus_laptop *asus = dev_get_drvdata(dev);
814
Corentin Chary17e78f62010-01-13 22:21:33 +0100815 return sysfs_acpi_set(asus, buf, count, bt_switch_handle);
Corentin Chary4564de12007-01-26 14:04:40 +0100816}
817
Corentin Chary78127b42007-01-26 14:04:49 +0100818/*
819 * Display
820 */
Corentin Chary9129d142009-12-01 22:39:41 +0100821static void set_display(struct asus_laptop *asus, int value)
Corentin Chary78127b42007-01-26 14:04:49 +0100822{
823 /* no sanity check needed for now */
Corentin Charyd8c67322009-11-28 10:27:51 +0100824 if (write_acpi_int(display_set_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200825 pr_warning("Error setting display\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100826 return;
827}
828
Corentin Chary9129d142009-12-01 22:39:41 +0100829static int read_display(struct asus_laptop *asus)
Corentin Chary78127b42007-01-26 14:04:49 +0100830{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400831 unsigned long long value = 0;
Corentin Chary9a816852007-03-11 10:25:38 +0100832 acpi_status rv = AE_OK;
Corentin Chary78127b42007-01-26 14:04:49 +0100833
Corentin Charybe966662009-08-29 10:28:29 +0200834 /*
835 * In most of the case, we know how to set the display, but sometime
836 * we can't read it
837 */
Len Brown8def05f2007-01-30 01:46:43 -0500838 if (display_get_handle) {
Corentin Chary9a816852007-03-11 10:25:38 +0100839 rv = acpi_evaluate_integer(display_get_handle, NULL,
840 NULL, &value);
841 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200842 pr_warning("Error reading display status\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100843 }
844
845 value &= 0x0F; /* needed for some models, shouldn't hurt others */
846
847 return value;
848}
Len Brown8def05f2007-01-30 01:46:43 -0500849
Corentin Chary78127b42007-01-26 14:04:49 +0100850/*
851 * Now, *this* one could be more user-friendly, but so far, no-one has
852 * complained. The significance of bits is the same as in store_disp()
853 */
854static ssize_t show_disp(struct device *dev,
855 struct device_attribute *attr, char *buf)
856{
Corentin Chary9129d142009-12-01 22:39:41 +0100857 struct asus_laptop *asus = dev_get_drvdata(dev);
858
859 return sprintf(buf, "%d\n", read_display(asus));
Corentin Chary78127b42007-01-26 14:04:49 +0100860}
861
862/*
863 * Experimental support for display switching. As of now: 1 should activate
864 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
865 * Any combination (bitwise) of these will suffice. I never actually tested 4
866 * displays hooked up simultaneously, so be warned. See the acpi4asus README
867 * for more info.
868 */
869static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
870 const char *buf, size_t count)
871{
Corentin Chary9129d142009-12-01 22:39:41 +0100872 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary78127b42007-01-26 14:04:49 +0100873 int rv, value;
874
875 rv = parse_arg(buf, count, &value);
876 if (rv > 0)
Corentin Chary9129d142009-12-01 22:39:41 +0100877 set_display(asus, value);
Corentin Chary78127b42007-01-26 14:04:49 +0100878 return rv;
879}
880
Corentin Chary8b857352007-01-26 14:04:58 +0100881/*
882 * Light Sens
883 */
Corentin Chary9129d142009-12-01 22:39:41 +0100884static void set_light_sens_switch(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100885{
Corentin Charyd8c67322009-11-28 10:27:51 +0100886 if (write_acpi_int(ls_switch_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200887 pr_warning("Error setting light sensor switch\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100888 asus->light_switch = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100889}
890
891static ssize_t show_lssw(struct device *dev,
892 struct device_attribute *attr, char *buf)
893{
Corentin Chary9129d142009-12-01 22:39:41 +0100894 struct asus_laptop *asus = dev_get_drvdata(dev);
895
Corentin Chary50a90c42009-11-30 21:55:12 +0100896 return sprintf(buf, "%d\n", asus->light_switch);
Corentin Chary8b857352007-01-26 14:04:58 +0100897}
898
899static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
900 const char *buf, size_t count)
901{
Corentin Chary9129d142009-12-01 22:39:41 +0100902 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100903 int rv, value;
904
905 rv = parse_arg(buf, count, &value);
906 if (rv > 0)
Corentin Chary9129d142009-12-01 22:39:41 +0100907 set_light_sens_switch(asus, value ? 1 : 0);
Corentin Chary8b857352007-01-26 14:04:58 +0100908
909 return rv;
910}
911
Corentin Chary9129d142009-12-01 22:39:41 +0100912static void set_light_sens_level(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100913{
Corentin Charyd8c67322009-11-28 10:27:51 +0100914 if (write_acpi_int(ls_level_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200915 pr_warning("Error setting light sensor level\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100916 asus->light_level = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100917}
918
919static ssize_t show_lslvl(struct device *dev,
920 struct device_attribute *attr, char *buf)
921{
Corentin Chary9129d142009-12-01 22:39:41 +0100922 struct asus_laptop *asus = dev_get_drvdata(dev);
923
Corentin Chary50a90c42009-11-30 21:55:12 +0100924 return sprintf(buf, "%d\n", asus->light_level);
Corentin Chary8b857352007-01-26 14:04:58 +0100925}
926
927static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
928 const char *buf, size_t count)
929{
Corentin Chary9129d142009-12-01 22:39:41 +0100930 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100931 int rv, value;
932
933 rv = parse_arg(buf, count, &value);
934 if (rv > 0) {
935 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
936 /* 0 <= value <= 15 */
Corentin Chary9129d142009-12-01 22:39:41 +0100937 set_light_sens_level(asus, value);
Corentin Chary8b857352007-01-26 14:04:58 +0100938 }
939
940 return rv;
941}
942
Corentin Charye539c2f2007-05-06 14:47:06 +0200943/*
944 * GPS
Corentin Chary6358bf22010-01-13 21:55:44 +0100945 * TODO: use rfkill
Corentin Charye539c2f2007-05-06 14:47:06 +0200946 */
Corentin Chary6358bf22010-01-13 21:55:44 +0100947static int asus_gps_status(struct asus_laptop *asus)
948{
949 unsigned long long status;
950 acpi_status rv = AE_OK;
951
952 rv = acpi_evaluate_integer(gps_status_handle, NULL, NULL, &status);
953 if (ACPI_FAILURE(rv)) {
954 pr_warning("Error reading GPS status\n");
955 return -ENODEV;
956 }
957 return !!status;
958}
959
960static int asus_gps_switch(struct asus_laptop *asus, int status)
961{
962 acpi_handle handle = status ? gps_on_handle : gps_off_handle;
963
964 if (write_acpi_int(handle, NULL, 0x02))
965 return -ENODEV;
966 return 0;
967}
968
Corentin Charye539c2f2007-05-06 14:47:06 +0200969static ssize_t show_gps(struct device *dev,
970 struct device_attribute *attr, char *buf)
971{
Corentin Chary9129d142009-12-01 22:39:41 +0100972 struct asus_laptop *asus = dev_get_drvdata(dev);
973
Corentin Chary6358bf22010-01-13 21:55:44 +0100974 return sprintf(buf, "%d\n", asus_gps_status(asus));
Corentin Charye539c2f2007-05-06 14:47:06 +0200975}
976
977static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
978 const char *buf, size_t count)
979{
Corentin Chary6358bf22010-01-13 21:55:44 +0100980 struct asus_laptop *asus = dev_get_drvdata(dev);
981 int rv, value;
982 int ret;
Corentin Chary9129d142009-12-01 22:39:41 +0100983
Corentin Chary6358bf22010-01-13 21:55:44 +0100984 rv = parse_arg(buf, count, &value);
985 if (rv <= 0)
986 return -EINVAL;
987 ret = asus_gps_switch(asus, !!value);
988 if (ret)
989 return ret;
990 return rv;
Corentin Charye539c2f2007-05-06 14:47:06 +0200991}
992
Corentin Chary034ce902009-01-20 16:17:43 +0100993/*
Corentin Charybe4ee822009-12-06 16:27:09 +0100994 * Input device (i.e. hotkeys)
Corentin Chary034ce902009-01-20 16:17:43 +0100995 */
Corentin Chary9129d142009-12-01 22:39:41 +0100996static struct key_entry *asus_get_entry_by_scancode(struct asus_laptop *asus,
997 int code)
Corentin Chary034ce902009-01-20 16:17:43 +0100998{
999 struct key_entry *key;
1000
Corentin Chary9129d142009-12-01 22:39:41 +01001001 for (key = asus->keymap; key->type != KE_END; key++)
Corentin Chary034ce902009-01-20 16:17:43 +01001002 if (code == key->code)
1003 return key;
1004
1005 return NULL;
1006}
1007
Corentin Chary9129d142009-12-01 22:39:41 +01001008static struct key_entry *asus_get_entry_by_keycode(struct asus_laptop *asus,
1009 int code)
Corentin Chary034ce902009-01-20 16:17:43 +01001010{
1011 struct key_entry *key;
1012
Corentin Chary9129d142009-12-01 22:39:41 +01001013 for (key = asus->keymap; key->type != KE_END; key++)
Corentin Chary034ce902009-01-20 16:17:43 +01001014 if (code == key->keycode && key->type == KE_KEY)
1015 return key;
1016
1017 return NULL;
1018}
1019
1020static int asus_getkeycode(struct input_dev *dev, int scancode, int *keycode)
1021{
Corentin Chary9129d142009-12-01 22:39:41 +01001022 struct asus_laptop *asus = input_get_drvdata(dev);
1023 struct key_entry *key = asus_get_entry_by_scancode(asus, scancode);
Corentin Chary034ce902009-01-20 16:17:43 +01001024
1025 if (key && key->type == KE_KEY) {
1026 *keycode = key->keycode;
1027 return 0;
1028 }
1029
1030 return -EINVAL;
1031}
1032
1033static int asus_setkeycode(struct input_dev *dev, int scancode, int keycode)
1034{
Corentin Chary9129d142009-12-01 22:39:41 +01001035 struct asus_laptop *asus = input_get_drvdata(dev);
Corentin Chary034ce902009-01-20 16:17:43 +01001036 struct key_entry *key;
1037 int old_keycode;
1038
1039 if (keycode < 0 || keycode > KEY_MAX)
1040 return -EINVAL;
1041
Corentin Chary9129d142009-12-01 22:39:41 +01001042 key = asus_get_entry_by_scancode(asus, scancode);
Corentin Chary034ce902009-01-20 16:17:43 +01001043 if (key && key->type == KE_KEY) {
1044 old_keycode = key->keycode;
1045 key->keycode = keycode;
1046 set_bit(keycode, dev->keybit);
Corentin Chary9129d142009-12-01 22:39:41 +01001047 if (!asus_get_entry_by_keycode(asus, old_keycode))
Corentin Chary034ce902009-01-20 16:17:43 +01001048 clear_bit(old_keycode, dev->keybit);
1049 return 0;
1050 }
1051
1052 return -EINVAL;
1053}
1054
Corentin Charybe4ee822009-12-06 16:27:09 +01001055static void asus_input_notify(struct asus_laptop *asus, int event)
1056{
1057 struct key_entry *key;
1058
1059 key = asus_get_entry_by_scancode(asus, event);
1060 if (!key)
1061 return ;
1062
1063 switch (key->type) {
1064 case KE_KEY:
1065 input_report_key(asus->inputdev, key->keycode, 1);
1066 input_sync(asus->inputdev);
1067 input_report_key(asus->inputdev, key->keycode, 0);
1068 input_sync(asus->inputdev);
1069 break;
1070 }
1071}
1072
1073static int asus_input_init(struct asus_laptop *asus)
1074{
1075 const struct key_entry *key;
1076 int result;
1077
1078 asus->inputdev = input_allocate_device();
1079 if (!asus->inputdev) {
1080 pr_info("Unable to allocate input device\n");
1081 return 0;
1082 }
1083 asus->inputdev->name = "Asus Laptop extra buttons";
1084 asus->inputdev->dev.parent = &asus->platform_device->dev;
1085 asus->inputdev->phys = ASUS_LAPTOP_FILE "/input0";
1086 asus->inputdev->id.bustype = BUS_HOST;
1087 asus->inputdev->getkeycode = asus_getkeycode;
1088 asus->inputdev->setkeycode = asus_setkeycode;
1089 input_set_drvdata(asus->inputdev, asus);
1090
1091 asus->keymap = kmemdup(asus_keymap, sizeof(asus_keymap),
1092 GFP_KERNEL);
1093 for (key = asus->keymap; key->type != KE_END; key++) {
1094 switch (key->type) {
1095 case KE_KEY:
1096 set_bit(EV_KEY, asus->inputdev->evbit);
1097 set_bit(key->keycode, asus->inputdev->keybit);
1098 break;
1099 }
1100 }
1101 result = input_register_device(asus->inputdev);
1102 if (result) {
1103 pr_info("Unable to register input device\n");
1104 input_free_device(asus->inputdev);
1105 }
1106 return result;
1107}
1108
1109static void asus_input_exit(struct asus_laptop *asus)
1110{
1111 if (asus->inputdev)
1112 input_unregister_device(asus->inputdev);
1113}
1114
1115/*
1116 * ACPI driver
1117 */
Corentin Chary600ad522009-11-30 21:42:42 +01001118static void asus_acpi_notify(struct acpi_device *device, u32 event)
Corentin Chary85091b72007-01-26 14:04:30 +01001119{
Corentin Chary9129d142009-12-01 22:39:41 +01001120 struct asus_laptop *asus = acpi_driver_data(device);
Corentin Chary6050c8d2009-02-15 19:30:19 +01001121 u16 count;
Corentin Chary034ce902009-01-20 16:17:43 +01001122
Corentin Chary6b7091e2007-01-26 14:04:45 +01001123 /*
1124 * We need to tell the backlight device when the backlight power is
1125 * switched
1126 */
Corentin Chary3e68ae72010-01-13 22:10:39 +01001127 if (event == ATKD_LCD_ON)
Corentin Chary9129d142009-12-01 22:39:41 +01001128 lcd_blank(asus, FB_BLANK_UNBLANK);
Corentin Chary3e68ae72010-01-13 22:10:39 +01001129 else if (event == ATKD_LCD_OFF)
Corentin Chary9129d142009-12-01 22:39:41 +01001130 lcd_blank(asus, FB_BLANK_POWERDOWN);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001131
Corentin Chary619d8b12009-11-28 10:35:37 +01001132 /* TODO Find a better way to handle events count. */
Corentin Chary50a90c42009-11-30 21:55:12 +01001133 count = asus->event_count[event % 128]++;
1134 acpi_bus_generate_proc_event(asus->device, event, count);
1135 acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
1136 dev_name(&asus->device->dev), event,
Corentin Chary6050c8d2009-02-15 19:30:19 +01001137 count);
Corentin Chary85091b72007-01-26 14:04:30 +01001138
Corentin Charybe4ee822009-12-06 16:27:09 +01001139 asus_input_notify(asus, event);
Corentin Chary85091b72007-01-26 14:04:30 +01001140}
1141
1142#define ASUS_CREATE_DEVICE_ATTR(_name) \
1143 struct device_attribute dev_attr_##_name = { \
1144 .attr = { \
1145 .name = __stringify(_name), \
Tejun Heo7b595752007-06-14 03:45:17 +09001146 .mode = 0 }, \
Corentin Chary85091b72007-01-26 14:04:30 +01001147 .show = NULL, \
1148 .store = NULL, \
1149 }
1150
1151#define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
1152 do { \
1153 dev_attr_##_name.attr.mode = _mode; \
1154 dev_attr_##_name.show = _show; \
1155 dev_attr_##_name.store = _store; \
1156 } while(0)
1157
1158static ASUS_CREATE_DEVICE_ATTR(infos);
Corentin Chary4564de12007-01-26 14:04:40 +01001159static ASUS_CREATE_DEVICE_ATTR(wlan);
1160static ASUS_CREATE_DEVICE_ATTR(bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +01001161static ASUS_CREATE_DEVICE_ATTR(display);
Corentin Chary722ad972007-01-26 14:04:55 +01001162static ASUS_CREATE_DEVICE_ATTR(ledd);
Corentin Chary8b857352007-01-26 14:04:58 +01001163static ASUS_CREATE_DEVICE_ATTR(ls_switch);
1164static ASUS_CREATE_DEVICE_ATTR(ls_level);
Corentin Charye539c2f2007-05-06 14:47:06 +02001165static ASUS_CREATE_DEVICE_ATTR(gps);
Corentin Chary85091b72007-01-26 14:04:30 +01001166
1167static struct attribute *asuspf_attributes[] = {
Len Brown8def05f2007-01-30 01:46:43 -05001168 &dev_attr_infos.attr,
1169 &dev_attr_wlan.attr,
1170 &dev_attr_bluetooth.attr,
1171 &dev_attr_display.attr,
1172 &dev_attr_ledd.attr,
1173 &dev_attr_ls_switch.attr,
1174 &dev_attr_ls_level.attr,
Corentin Charye539c2f2007-05-06 14:47:06 +02001175 &dev_attr_gps.attr,
Len Brown8def05f2007-01-30 01:46:43 -05001176 NULL
Corentin Chary85091b72007-01-26 14:04:30 +01001177};
1178
Corentin Chary600ad522009-11-30 21:42:42 +01001179static struct attribute_group platform_attribute_group = {
Len Brown8def05f2007-01-30 01:46:43 -05001180 .attrs = asuspf_attributes
Corentin Chary85091b72007-01-26 14:04:30 +01001181};
1182
Corentin Chary9129d142009-12-01 22:39:41 +01001183static int asus_platform_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001184{
1185 int result;
1186
Corentin Chary50a90c42009-11-30 21:55:12 +01001187 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1188 if (!asus->platform_device)
Corentin Chary600ad522009-11-30 21:42:42 +01001189 return -ENOMEM;
Corentin Chary9129d142009-12-01 22:39:41 +01001190 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001191
Corentin Chary50a90c42009-11-30 21:55:12 +01001192 result = platform_device_add(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001193 if (result)
1194 goto fail_platform_device;
1195
Corentin Chary50a90c42009-11-30 21:55:12 +01001196 result = sysfs_create_group(&asus->platform_device->dev.kobj,
Corentin Chary600ad522009-11-30 21:42:42 +01001197 &platform_attribute_group);
1198 if (result)
1199 goto fail_sysfs;
1200 return 0;
1201
1202fail_sysfs:
Corentin Chary50a90c42009-11-30 21:55:12 +01001203 platform_device_del(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001204fail_platform_device:
Corentin Chary50a90c42009-11-30 21:55:12 +01001205 platform_device_put(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001206 return result;
1207}
1208
Corentin Chary9129d142009-12-01 22:39:41 +01001209static void asus_platform_exit(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001210{
Corentin Chary50a90c42009-11-30 21:55:12 +01001211 sysfs_remove_group(&asus->platform_device->dev.kobj,
Corentin Chary600ad522009-11-30 21:42:42 +01001212 &platform_attribute_group);
Corentin Chary50a90c42009-11-30 21:55:12 +01001213 platform_device_unregister(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001214}
1215
1216static struct platform_driver platform_driver = {
Len Brown8def05f2007-01-30 01:46:43 -05001217 .driver = {
Corentin Chary9129d142009-12-01 22:39:41 +01001218 .name = ASUS_LAPTOP_FILE,
1219 .owner = THIS_MODULE,
1220 }
Corentin Chary85091b72007-01-26 14:04:30 +01001221};
1222
Corentin Chary9129d142009-12-01 22:39:41 +01001223static void asus_laptop_add_fs(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001224{
1225 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
Corentin Chary4564de12007-01-26 14:04:40 +01001226
1227 if (wl_switch_handle)
1228 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
1229
1230 if (bt_switch_handle)
1231 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
1232 show_bluetooth, store_bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +01001233
1234 if (display_set_handle && display_get_handle)
1235 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
Len Brown8def05f2007-01-30 01:46:43 -05001236 else if (display_set_handle)
Corentin Chary78127b42007-01-26 14:04:49 +01001237 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
1238
Corentin Chary722ad972007-01-26 14:04:55 +01001239 if (ledd_set_handle)
1240 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
1241
Corentin Chary8b857352007-01-26 14:04:58 +01001242 if (ls_switch_handle && ls_level_handle) {
1243 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
1244 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
1245 }
Corentin Charye539c2f2007-05-06 14:47:06 +02001246
Corentin Charyf3985322007-05-06 14:48:22 +02001247 if (gps_status_handle && gps_on_handle && gps_off_handle)
Corentin Charye539c2f2007-05-06 14:47:06 +02001248 ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
Corentin Chary85091b72007-01-26 14:04:30 +01001249}
1250
Len Brown8def05f2007-01-30 01:46:43 -05001251static int asus_handle_init(char *name, acpi_handle * handle,
Corentin Chary85091b72007-01-26 14:04:30 +01001252 char **paths, int num_paths)
1253{
1254 int i;
1255 acpi_status status;
1256
1257 for (i = 0; i < num_paths; i++) {
1258 status = acpi_get_handle(NULL, paths[i], handle);
1259 if (ACPI_SUCCESS(status))
1260 return 0;
1261 }
1262
1263 *handle = NULL;
1264 return -ENODEV;
1265}
1266
1267#define ASUS_HANDLE_INIT(object) \
1268 asus_handle_init(#object, &object##_handle, object##_paths, \
1269 ARRAY_SIZE(object##_paths))
1270
Corentin Chary85091b72007-01-26 14:04:30 +01001271/*
Corentin Chary50a90c42009-11-30 21:55:12 +01001272 * This function is used to initialize the context with right values. In this
1273 * method, we can make all the detection we want, and modify the asus_laptop
1274 * struct
Corentin Chary85091b72007-01-26 14:04:30 +01001275 */
Corentin Chary7c247642009-11-30 22:13:54 +01001276static int asus_laptop_get_info(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001277{
1278 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Corentin Chary85091b72007-01-26 14:04:30 +01001279 union acpi_object *model = NULL;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001280 unsigned long long bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +01001281 char *string = NULL;
1282 acpi_status status;
1283
1284 /*
1285 * Get DSDT headers early enough to allow for differentiating between
1286 * models, but late enough to allow acpi_bus_register_driver() to fail
1287 * before doing anything ACPI-specific. Should we encounter a machine,
1288 * which needs special handling (i.e. its hotkey device has a different
Corentin Chary7c247642009-11-30 22:13:54 +01001289 * HID), this bit will be moved.
Corentin Chary85091b72007-01-26 14:04:30 +01001290 */
Corentin Chary7c247642009-11-30 22:13:54 +01001291 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
Corentin Chary85091b72007-01-26 14:04:30 +01001292 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001293 pr_warning("Couldn't get the DSDT table header\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001294
1295 /* We have to write 0 on init this far for all ASUS models */
Corentin Chary50a90c42009-11-30 21:55:12 +01001296 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001297 pr_err("Hotkey initialization failed\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001298 return -ENODEV;
1299 }
1300
1301 /* This needs to be called for some laptops to init properly */
Corentin Chary9a816852007-03-11 10:25:38 +01001302 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001303 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
Corentin Chary9a816852007-03-11 10:25:38 +01001304 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001305 pr_warning("Error calling BSTS\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001306 else if (bsts_result)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001307 pr_notice("BSTS called, 0x%02x returned\n",
Corentin Chary9a816852007-03-11 10:25:38 +01001308 (uint) bsts_result);
Corentin Chary85091b72007-01-26 14:04:30 +01001309
Corentin Chary185e5af2007-03-11 10:27:33 +01001310 /* This too ... */
Corentin Chary50a90c42009-11-30 21:55:12 +01001311 write_acpi_int(asus->handle, "CWAP", wapf);
Corentin Chary185e5af2007-03-11 10:27:33 +01001312
Corentin Chary85091b72007-01-26 14:04:30 +01001313 /*
1314 * Try to match the object returned by INIT to the specific model.
1315 * Handle every possible object (or the lack of thereof) the DSDT
1316 * writers might throw at us. When in trouble, we pass NULL to
1317 * asus_model_match() and try something completely different.
1318 */
1319 if (buffer.pointer) {
1320 model = buffer.pointer;
1321 switch (model->type) {
1322 case ACPI_TYPE_STRING:
1323 string = model->string.pointer;
1324 break;
1325 case ACPI_TYPE_BUFFER:
1326 string = model->buffer.pointer;
1327 break;
1328 default:
1329 string = "";
1330 break;
1331 }
1332 }
Corentin Chary50a90c42009-11-30 21:55:12 +01001333 asus->name = kstrdup(string, GFP_KERNEL);
1334 if (!asus->name)
Corentin Chary85091b72007-01-26 14:04:30 +01001335 return -ENOMEM;
1336
Len Brown8def05f2007-01-30 01:46:43 -05001337 if (*string)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001338 pr_notice(" %s model detected\n", string);
Corentin Chary85091b72007-01-26 14:04:30 +01001339
Corentin Charybe18cda2007-01-26 14:04:35 +01001340 ASUS_HANDLE_INIT(mled_set);
1341 ASUS_HANDLE_INIT(tled_set);
1342 ASUS_HANDLE_INIT(rled_set);
1343 ASUS_HANDLE_INIT(pled_set);
Corentin Charyfdd8d082007-03-11 10:26:48 +01001344 ASUS_HANDLE_INIT(gled_set);
Corentin Charybe18cda2007-01-26 14:04:35 +01001345
Corentin Chary722ad972007-01-26 14:04:55 +01001346 ASUS_HANDLE_INIT(ledd_set);
1347
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001348 ASUS_HANDLE_INIT(kled_set);
1349 ASUS_HANDLE_INIT(kled_get);
1350
Corentin Chary4564de12007-01-26 14:04:40 +01001351 /*
1352 * The HWRS method return informations about the hardware.
1353 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1354 * The significance of others is yet to be found.
1355 * If we don't find the method, we assume the device are present.
1356 */
Corentin Chary9a816852007-03-11 10:25:38 +01001357 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001358 acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
Corentin Chary9a816852007-03-11 10:25:38 +01001359 if (ACPI_FAILURE(status))
Corentin Chary4564de12007-01-26 14:04:40 +01001360 hwrs_result = WL_HWRS | BT_HWRS;
1361
Len Brown8def05f2007-01-30 01:46:43 -05001362 if (hwrs_result & WL_HWRS)
Corentin Chary4564de12007-01-26 14:04:40 +01001363 ASUS_HANDLE_INIT(wl_switch);
Len Brown8def05f2007-01-30 01:46:43 -05001364 if (hwrs_result & BT_HWRS)
Corentin Chary4564de12007-01-26 14:04:40 +01001365 ASUS_HANDLE_INIT(bt_switch);
1366
Corentin Charyaa9df932010-01-13 21:49:10 +01001367 if (!ASUS_HANDLE_INIT(wireless_status))
1368 asus->have_rsts = true;
Corentin Chary4564de12007-01-26 14:04:40 +01001369
Corentin Chary6b7091e2007-01-26 14:04:45 +01001370 ASUS_HANDLE_INIT(brightness_set);
1371 ASUS_HANDLE_INIT(brightness_get);
1372
1373 ASUS_HANDLE_INIT(lcd_switch);
1374
Corentin Chary78127b42007-01-26 14:04:49 +01001375 ASUS_HANDLE_INIT(display_set);
1376 ASUS_HANDLE_INIT(display_get);
1377
Corentin Charybe966662009-08-29 10:28:29 +02001378 /*
1379 * There is a lot of models with "ALSL", but a few get
1380 * a real light sens, so we need to check it.
1381 */
Corentin Chary832d9952007-05-06 14:47:29 +02001382 if (!ASUS_HANDLE_INIT(ls_switch))
Corentin Chary8b857352007-01-26 14:04:58 +01001383 ASUS_HANDLE_INIT(ls_level);
1384
Corentin Charye539c2f2007-05-06 14:47:06 +02001385 ASUS_HANDLE_INIT(gps_on);
1386 ASUS_HANDLE_INIT(gps_off);
1387 ASUS_HANDLE_INIT(gps_status);
1388
Corentin Chary85091b72007-01-26 14:04:30 +01001389 kfree(model);
1390
1391 return AE_OK;
1392}
1393
Corentin Chary600ad522009-11-30 21:42:42 +01001394static bool asus_device_present;
1395
Corentin Chary9129d142009-12-01 22:39:41 +01001396static int __devinit asus_acpi_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001397{
1398 int result = 0;
1399
Corentin Chary50a90c42009-11-30 21:55:12 +01001400 result = acpi_bus_get_status(asus->device);
Corentin Chary600ad522009-11-30 21:42:42 +01001401 if (result)
1402 return result;
Corentin Chary50a90c42009-11-30 21:55:12 +01001403 if (!asus->device->status.present) {
Corentin Chary600ad522009-11-30 21:42:42 +01001404 pr_err("Hotkey device not present, aborting\n");
1405 return -ENODEV;
1406 }
1407
Corentin Chary7c247642009-11-30 22:13:54 +01001408 result = asus_laptop_get_info(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001409 if (result)
1410 return result;
1411
Corentin Chary9129d142009-12-01 22:39:41 +01001412 asus_laptop_add_fs(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001413
1414 /* WLED and BLED are on by default */
Corentin Charybe4ee822009-12-06 16:27:09 +01001415 if (bluetooth_status >= 0)
Corentin Chary17e78f62010-01-13 22:21:33 +01001416 write_acpi_int(bt_switch_handle, NULL, !!bluetooth_status);
Corentin Charybe4ee822009-12-06 16:27:09 +01001417 if (wireless_status >= 0)
Corentin Chary17e78f62010-01-13 22:21:33 +01001418 write_acpi_int(wl_switch_handle, NULL, !!wireless_status);
Corentin Chary600ad522009-11-30 21:42:42 +01001419
1420 /* Keyboard Backlight is on by default */
1421 if (kled_set_handle)
Corentin Chary9129d142009-12-01 22:39:41 +01001422 set_kled_lvl(asus, 1);
Corentin Chary600ad522009-11-30 21:42:42 +01001423
1424 /* LED display is off by default */
Corentin Chary50a90c42009-11-30 21:55:12 +01001425 asus->ledd_status = 0xFFF;
Corentin Chary600ad522009-11-30 21:42:42 +01001426
1427 /* Set initial values of light sensor and level */
Corentin Charybe4ee822009-12-06 16:27:09 +01001428 asus->light_switch = 0; /* Default to light sensor disabled */
1429 asus->light_level = 5; /* level 5 for sensor sensitivity */
Corentin Chary600ad522009-11-30 21:42:42 +01001430
1431 if (ls_switch_handle)
Corentin Chary9129d142009-12-01 22:39:41 +01001432 set_light_sens_switch(asus, asus->light_switch);
Corentin Chary600ad522009-11-30 21:42:42 +01001433
1434 if (ls_level_handle)
Corentin Chary9129d142009-12-01 22:39:41 +01001435 set_light_sens_level(asus, asus->light_level);
Corentin Chary600ad522009-11-30 21:42:42 +01001436
1437 /* GPS is on by default */
Corentin Chary6358bf22010-01-13 21:55:44 +01001438 asus_gps_switch(asus, 1);
Corentin Chary600ad522009-11-30 21:42:42 +01001439 return result;
1440}
1441
1442static int __devinit asus_acpi_add(struct acpi_device *device)
1443{
Corentin Chary9129d142009-12-01 22:39:41 +01001444 struct asus_laptop *asus;
Corentin Chary600ad522009-11-30 21:42:42 +01001445 int result;
1446
1447 pr_notice("Asus Laptop Support version %s\n",
1448 ASUS_LAPTOP_VERSION);
Corentin Chary50a90c42009-11-30 21:55:12 +01001449 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1450 if (!asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001451 return -ENOMEM;
Corentin Chary50a90c42009-11-30 21:55:12 +01001452 asus->handle = device->handle;
1453 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1454 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1455 device->driver_data = asus;
1456 asus->device = device;
Corentin Chary600ad522009-11-30 21:42:42 +01001457
Corentin Chary9129d142009-12-01 22:39:41 +01001458 result = asus_acpi_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001459 if (result)
1460 goto fail_platform;
1461
1462 /*
1463 * Register the platform device first. It is used as a parent for the
1464 * sub-devices below.
1465 */
Corentin Chary9129d142009-12-01 22:39:41 +01001466 result = asus_platform_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001467 if (result)
1468 goto fail_platform;
1469
1470 if (!acpi_video_backlight_support()) {
Corentin Chary9129d142009-12-01 22:39:41 +01001471 result = asus_backlight_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001472 if (result)
1473 goto fail_backlight;
1474 } else
1475 pr_info("Backlight controlled by ACPI video driver\n");
1476
Corentin Chary9129d142009-12-01 22:39:41 +01001477 result = asus_input_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001478 if (result)
1479 goto fail_input;
1480
Corentin Chary9129d142009-12-01 22:39:41 +01001481 result = asus_led_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001482 if (result)
1483 goto fail_led;
1484
1485 asus_device_present = true;
1486 return 0;
1487
1488fail_led:
Corentin Chary9129d142009-12-01 22:39:41 +01001489 asus_input_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001490fail_input:
Corentin Chary9129d142009-12-01 22:39:41 +01001491 asus_backlight_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001492fail_backlight:
Corentin Chary9129d142009-12-01 22:39:41 +01001493 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001494fail_platform:
Corentin Chary50a90c42009-11-30 21:55:12 +01001495 kfree(asus->name);
1496 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001497
1498 return result;
1499}
1500
1501static int asus_acpi_remove(struct acpi_device *device, int type)
1502{
Corentin Chary9129d142009-12-01 22:39:41 +01001503 struct asus_laptop *asus = acpi_driver_data(device);
1504
1505 asus_backlight_exit(asus);
1506 asus_led_exit(asus);
1507 asus_input_exit(asus);
1508 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001509
Corentin Chary50a90c42009-11-30 21:55:12 +01001510 kfree(asus->name);
1511 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001512 return 0;
1513}
1514
1515static const struct acpi_device_id asus_device_ids[] = {
1516 {"ATK0100", 0},
1517 {"ATK0101", 0},
1518 {"", 0},
1519};
1520MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1521
1522static struct acpi_driver asus_acpi_driver = {
Corentin Chary50a90c42009-11-30 21:55:12 +01001523 .name = ASUS_LAPTOP_NAME,
1524 .class = ASUS_LAPTOP_CLASS,
Corentin Chary600ad522009-11-30 21:42:42 +01001525 .owner = THIS_MODULE,
1526 .ids = asus_device_ids,
1527 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1528 .ops = {
1529 .add = asus_acpi_add,
1530 .remove = asus_acpi_remove,
1531 .notify = asus_acpi_notify,
1532 },
1533};
1534
Corentin Chary85091b72007-01-26 14:04:30 +01001535static int __init asus_laptop_init(void)
1536{
1537 int result;
1538
Corentin Chary600ad522009-11-30 21:42:42 +01001539 result = platform_driver_register(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001540 if (result < 0)
1541 return result;
1542
Corentin Chary600ad522009-11-30 21:42:42 +01001543 result = acpi_bus_register_driver(&asus_acpi_driver);
1544 if (result < 0)
1545 goto fail_acpi_driver;
1546 if (!asus_device_present) {
1547 result = -ENODEV;
1548 goto fail_no_device;
Corentin Chary85091b72007-01-26 14:04:30 +01001549 }
Len Brown8def05f2007-01-30 01:46:43 -05001550 return 0;
Corentin Chary85091b72007-01-26 14:04:30 +01001551
Corentin Chary600ad522009-11-30 21:42:42 +01001552fail_no_device:
1553 acpi_bus_unregister_driver(&asus_acpi_driver);
1554fail_acpi_driver:
1555 platform_driver_unregister(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001556 return result;
1557}
1558
Corentin Chary600ad522009-11-30 21:42:42 +01001559static void __exit asus_laptop_exit(void)
1560{
1561 acpi_bus_unregister_driver(&asus_acpi_driver);
1562 platform_driver_unregister(&platform_driver);
1563}
1564
Corentin Chary85091b72007-01-26 14:04:30 +01001565module_init(asus_laptop_init);
1566module_exit(asus_laptop_exit);