blob: 1d61094d3b5f2b8b1e306c9896cd5f35a3df5fb2 [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 Chary66a71dd2010-01-25 22:50:11 +010052#include <linux/input/sparse-keymap.h>
Corentin Chary85091b72007-01-26 14:04:30 +010053
Corentin Chary91687cc2009-11-28 10:32:34 +010054#define ASUS_LAPTOP_VERSION "0.42"
Corentin Chary85091b72007-01-26 14:04:30 +010055
Corentin Chary50a90c42009-11-30 21:55:12 +010056#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."
Corentin Chary85091b72007-01-26 14:04:30 +010061
Corentin Chary85091b72007-01-26 14:04:30 +010062MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
Corentin Chary50a90c42009-11-30 21:55:12 +010063MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
Corentin Chary85091b72007-01-26 14:04:30 +010064MODULE_LICENSE("GPL");
65
Corentin Charybe966662009-08-29 10:28:29 +020066/*
67 * WAPF defines the behavior of the Fn+Fx wlan key
Corentin Chary185e5af2007-03-11 10:27:33 +010068 * 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
Corentin Charyd0930a22010-01-17 17:21:13 +010080static uint wlan_status = 1;
Corentin Chary0e875f42010-01-10 20:49:26 +010081static uint bluetooth_status = 1;
82
Corentin Charyd0930a22010-01-17 17:21:13 +010083module_param(wlan_status, uint, 0644);
84MODULE_PARM_DESC(wlan_status, "Set the wireless status on boot "
Corentin Chary0e875f42010-01-10 20:49:26 +010085 "(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
Corentin Charybe4ee822009-12-06 16:27:09 +010093/*
94 * Some events we use, same for all Asus
95 */
Corentin Charya539df52010-01-25 22:53:21 +010096#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
Corentin Charybe4ee822009-12-06 16:27:09 +0100100#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 */
Corentin Chary17e78f62010-01-13 22:21:33 +0100113#define WL_RSTS 0x01 /* internal Wifi */
114#define BT_RSTS 0x02 /* internal Bluetooth */
Corentin Charybe4ee822009-12-06 16:27:09 +0100115
Corentin Chary85091b72007-01-26 14:04:30 +0100116#define ASUS_HANDLE(object, paths...) \
117 static acpi_handle object##_handle = NULL; \
118 static char *object##_paths[] = { paths }
119
Corentin Charybe18cda2007-01-26 14:04:35 +0100120/* LED */
Corentin Charyd99b5772010-01-22 21:20:57 +0100121#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) */
Corentin Charybe18cda2007-01-26 14:04:35 +0100126
Corentin Chary722ad972007-01-26 14:04:55 +0100127/* LEDD */
Corentin Charyd99b5772010-01-22 21:20:57 +0100128#define METHOD_LEDD "SLCM"
Corentin Chary722ad972007-01-26 14:04:55 +0100129
Corentin Charybe966662009-08-29 10:28:29 +0200130/*
131 * Bluetooth and WLAN
Corentin Chary4564de12007-01-26 14:04:40 +0100132 * WLED and BLED are not handled like other XLED, because in some dsdt
133 * they also control the WLAN/Bluetooth device.
134 */
Corentin Charyd99b5772010-01-22 21:20:57 +0100135#define METHOD_WLAN "WLED"
136#define METHOD_BLUETOOTH "BLED"
137#define METHOD_WL_STATUS "RSTS"
Corentin Chary4564de12007-01-26 14:04:40 +0100138
Corentin Chary6b7091e2007-01-26 14:04:45 +0100139/* Brightness */
Corentin Charyd99b5772010-01-22 21:20:57 +0100140#define METHOD_BRIGHTNESS_SET "SPLV"
141#define METHOD_BRIGHTNESS_GET "GPLV"
Corentin Chary6b7091e2007-01-26 14:04:45 +0100142
143/* Backlight */
Len Brown8def05f2007-01-30 01:46:43 -0500144ASUS_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 */
Torsten Krah4d0b856e2008-10-17 09:47:57 +0200149 "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
Len Brown8def05f2007-01-30 01:46:43 -0500150 "\\_SB.PCI0.PX40.Q10", /* S1x */
151 "\\Q10"); /* A2x, L2D, L3D, M2E */
Corentin Chary6b7091e2007-01-26 14:04:45 +0100152
Corentin Chary78127b42007-01-26 14:04:49 +0100153/* Display */
Corentin Charyd99b5772010-01-22 21:20:57 +0100154#define METHOD_SWITCH_DISPLAY "SDSP"
Corentin Charybe966662009-08-29 10:28:29 +0200155ASUS_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");
Corentin Chary78127b42007-01-26 14:04:49 +0100178
Corentin Charyd99b5772010-01-22 21:20:57 +0100179#define METHOD_ALS_CONTROL "ALSC" /* Z71A Z71V */
180#define METHOD_ALS_LEVEL "ALSL" /* Z71A Z71V */
Corentin Chary8b857352007-01-26 14:04:58 +0100181
Corentin Charye539c2f2007-05-06 14:47:06 +0200182/* GPS */
183/* R2H use different handle for GPS on/off */
Corentin Charyd99b5772010-01-22 21:20:57 +0100184#define METHOD_GPS_ON "SDON"
185#define METHOD_GPS_OFF "SDOF"
186#define METHOD_GPS_STATUS "GPST"
Corentin Charye539c2f2007-05-06 14:47:06 +0200187
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000188/* Keyboard light */
Corentin Charyd99b5772010-01-22 21:20:57 +0100189#define METHOD_KBD_LIGHT_SET "SLKB"
190#define METHOD_KBD_LIGHT_GET "GLKB"
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000191
Corentin Chary85091b72007-01-26 14:04:30 +0100192/*
Corentin Chary9129d142009-12-01 22:39:41 +0100193 * 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/*
Corentin Chary85091b72007-01-26 14:04:30 +0100221 * This is the main structure, we can use it to store anything interesting
222 * about the hotk device
223 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100224struct asus_laptop {
Corentin Charybe966662009-08-29 10:28:29 +0200225 char *name; /* laptop name */
Corentin Chary600ad522009-11-30 21:42:42 +0100226
Corentin Chary7c247642009-11-30 22:13:54 +0100227 struct acpi_table_header *dsdt_info;
Corentin Chary600ad522009-11-30 21:42:42 +0100228 struct platform_device *platform_device;
Corentin Chary7c247642009-11-30 22:13:54 +0100229 struct acpi_device *device; /* the device we are in */
230 struct backlight_device *backlight_device;
Corentin Chary9129d142009-12-01 22:39:41 +0100231
Corentin Chary7c247642009-11-30 22:13:54 +0100232 struct input_dev *inputdev;
Corentin Chary9129d142009-12-01 22:39:41 +0100233 struct key_entry *keymap;
234
235 struct asus_laptop_leds leds;
Corentin Chary7c247642009-11-30 22:13:54 +0100236
Corentin Charyaa9df932010-01-13 21:49:10 +0100237 int wireless_status;
238 bool have_rsts;
Corentin Chary3e68ae72010-01-13 22:10:39 +0100239 int lcd_state;
Corentin Charyaa9df932010-01-13 21:49:10 +0100240
Corentin Charybe966662009-08-29 10:28:29 +0200241 acpi_handle handle; /* the handle of the hotk device */
Corentin Charybe966662009-08-29 10:28:29 +0200242 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 */
Corentin Chary034ce902009-01-20 16:17:43 +0100246 u16 *keycode_map;
Corentin Chary85091b72007-01-26 14:04:30 +0100247};
248
Corentin Chary9129d142009-12-01 22:39:41 +0100249static const struct key_entry asus_keymap[] = {
Corentin Charya539df52010-01-25 22:53:21 +0100250 /* Lenovo SL Specific keycodes */
Corentin Chary66a71dd2010-01-25 22:50:11 +0100251 {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 } },
Corentin Charya539df52010-01-25 22:53:21 +0100256 /* End of Lenovo SL Specific keycodes */
Corentin Chary66a71dd2010-01-25 22:50:11 +0100257 {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 */
Corentin Chary7f607d72010-01-17 17:37:19 +0100279 {KE_KEY, 0x7E, { KEY_BLUETOOTH } },
280 {KE_KEY, 0x7D, { KEY_BLUETOOTH } },
Corentin Chary66a71dd2010-01-25 22:50:11 +0100281 {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 } },
Corentin Chary034ce902009-01-20 16:17:43 +0100288 {KE_END, 0},
289};
290
Corentin Chary66a71dd2010-01-25 22:50:11 +0100291
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 Charyd99b5772010-01-22 21:20:57 +0100326static 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
Corentin Chary17e78f62010-01-13 22:21:33 +0100352/* Generic LED function */
Corentin Charye5593bf2010-01-17 17:20:11 +0100353static int asus_led_set(struct asus_laptop *asus, char *method,
Corentin Chary17e78f62010-01-13 22:21:33 +0100354 int value)
Corentin Charybe18cda2007-01-26 14:04:35 +0100355{
Corentin Charyd99b5772010-01-22 21:20:57 +0100356 if (!strcmp(method, METHOD_MLED))
Corentin Chary17e78f62010-01-13 22:21:33 +0100357 value = !value;
Corentin Charyd99b5772010-01-22 21:20:57 +0100358 else if (!strcmp(method, METHOD_GLED))
Corentin Chary17e78f62010-01-13 22:21:33 +0100359 value = !value + 1;
360 else
361 value = !!value;
Corentin Charybe18cda2007-01-26 14:04:35 +0100362
Corentin Charye5593bf2010-01-17 17:20:11 +0100363 return write_acpi_int(asus->handle, method, value);
Corentin Charybe18cda2007-01-26 14:04:35 +0100364}
365
Corentin Charybe4ee822009-12-06 16:27:09 +0100366/*
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); \
Corentin Chary75747122010-01-24 11:15:42 +0100374 static void object##_led_update(struct work_struct *ignored);
Corentin Charybe4ee822009-12-06 16:27:09 +0100375
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
Corentin Charybe18cda2007-01-26 14:04:35 +0100383/* /sys/class/led handlers */
Corentin Charyd99b5772010-01-22 21:20:57 +0100384#define ASUS_LED_HANDLER(object, method) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100385 static void object##_led_set(struct led_classdev *led_cdev, \
386 enum led_brightness value) \
387 { \
Corentin Chary9129d142009-12-01 22:39:41 +0100388 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); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100394 } \
Corentin Chary9129d142009-12-01 22:39:41 +0100395 static void object##_led_update(struct work_struct *work) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100396 { \
Corentin Chary9129d142009-12-01 22:39:41 +0100397 struct asus_laptop *asus = work_to_asus(work, object); \
398 \
399 int value = asus->leds.object##_wk; \
Corentin Charyd99b5772010-01-22 21:20:57 +0100400 asus_led_set(asus, method, value); \
Corentin Charyabfa57e2009-08-28 12:56:47 +0000401 } \
402 static enum led_brightness object##_led_get( \
403 struct led_classdev *led_cdev) \
404 { \
405 return led_cdev->brightness; \
Corentin Charybe18cda2007-01-26 14:04:35 +0100406 }
407
Corentin Charyd99b5772010-01-22 21:20:57 +0100408ASUS_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);
Corentin Charybe18cda2007-01-26 14:04:35 +0100413
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000414/*
Corentin Charybe4ee822009-12-06 16:27:09 +0100415 * Keyboard backlight (also a LED)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000416 */
Corentin Charyd99b5772010-01-22 21:20:57 +0100417static int asus_kled_lvl(struct asus_laptop *asus)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000418{
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
Corentin Charyd99b5772010-01-22 21:20:57 +0100429 rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
430 &params, &kblv);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000431 if (ACPI_FAILURE(rv)) {
432 pr_warning("Error reading kled level\n");
Corentin Chary4d441512010-01-13 22:26:24 +0100433 return -ENODEV;
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000434 }
435 return kblv;
436}
437
Corentin Chary4d441512010-01-13 22:26:24 +0100438static int asus_kled_set(struct asus_laptop *asus, int kblv)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000439{
440 if (kblv > 0)
441 kblv = (1 << 7) | (kblv & 0x7F);
442 else
443 kblv = 0;
444
Corentin Charyd99b5772010-01-22 21:20:57 +0100445 if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000446 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{
Corentin Chary9129d142009-12-01 22:39:41 +0100455 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);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000459}
460
Corentin Chary9129d142009-12-01 22:39:41 +0100461static void kled_led_update(struct work_struct *work)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000462{
Corentin Chary9129d142009-12-01 22:39:41 +0100463 struct asus_laptop *asus = work_to_asus(work, kled);
464
Corentin Chary4d441512010-01-13 22:26:24 +0100465 asus_kled_set(asus, asus->leds.kled_wk);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000466}
467
468static enum led_brightness kled_led_get(struct led_classdev *led_cdev)
469{
Corentin Charyd99b5772010-01-22 21:20:57 +0100470 struct asus_laptop *asus = led_to_asus(led_cdev, kled);
471
472 return asus_kled_lvl(asus);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000473}
474
Corentin Charybe4ee822009-12-06 16:27:09 +0100475static void asus_led_exit(struct asus_laptop *asus)
476{
Corentin Chary75747122010-01-24 11:15:42 +0100477 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);
Corentin Charybe4ee822009-12-06 16:27:09 +0100489 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 */
Corentin Charyd99b5772010-01-22 21:20:57 +0100496#define ASUS_LED_REGISTER(asus, object, _name, max, method) \
Corentin Charybe4ee822009-12-06 16:27:09 +0100497 do { \
498 struct led_classdev *ldev = &asus->leds.object; \
Corentin Charyd99b5772010-01-22 21:20:57 +0100499 \
500 if (method && acpi_check_handle(asus->handle, method, NULL)) \
Corentin Charybe4ee822009-12-06 16:27:09 +0100501 break ; \
502 \
503 INIT_WORK(&asus->leds.object##_work, object##_led_update); \
504 ldev->name = "asus::" _name; \
505 ldev->brightness_set = object##_led_set; \
Corentin Chary75747122010-01-24 11:15:42 +0100506 ldev->brightness_get = object##_led_get; \
Corentin Charybe4ee822009-12-06 16:27:09 +0100507 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
Corentin Charyd99b5772010-01-22 21:20:57 +0100527 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);
Corentin Charybe4ee822009-12-06 16:27:09 +0100535error:
536 if (rv)
537 asus_led_exit(asus);
538 return rv;
539}
540
541/*
542 * Backlight device
543 */
Corentin Chary3e68ae72010-01-13 22:10:39 +0100544static int asus_lcd_status(struct asus_laptop *asus)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100545{
Corentin Chary3e68ae72010-01-13 22:10:39 +0100546 return asus->lcd_state;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100547}
548
Corentin Chary3e68ae72010-01-13 22:10:39 +0100549static int asus_lcd_set(struct asus_laptop *asus, int value)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100550{
551 int lcd = 0;
552 acpi_status status = 0;
553
Corentin Chary3e68ae72010-01-13 22:10:39 +0100554 lcd = !!value;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100555
Corentin Chary3e68ae72010-01-13 22:10:39 +0100556 if (lcd == asus_lcd_status(asus))
Corentin Chary6b7091e2007-01-26 14:04:45 +0100557 return 0;
558
Corentin Chary3e68ae72010-01-13 22:10:39 +0100559 if (!lcd_switch_handle)
560 return -ENODEV;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100561
Corentin Chary3e68ae72010-01-13 22:10:39 +0100562 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;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100568 }
569
Corentin Chary3e68ae72010-01-13 22:10:39 +0100570 asus->lcd_state = lcd;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100571 return 0;
572}
573
Corentin Chary9129d142009-12-01 22:39:41 +0100574static void lcd_blank(struct asus_laptop *asus, int blank)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100575{
Corentin Chary7c247642009-11-30 22:13:54 +0100576 struct backlight_device *bd = asus->backlight_device;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100577
Corentin Chary3e68ae72010-01-13 22:10:39 +0100578 asus->lcd_state = (blank == FB_BLANK_UNBLANK);
579
Len Brown8def05f2007-01-30 01:46:43 -0500580 if (bd) {
Richard Purdie599a52d2007-02-10 23:07:48 +0000581 bd->props.power = blank;
Richard Purdie28ee0862007-02-08 22:25:09 +0000582 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100583 }
584}
585
Corentin Chary4d441512010-01-13 22:26:24 +0100586static int asus_read_brightness(struct backlight_device *bd)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100587{
Corentin Charyd99b5772010-01-22 21:20:57 +0100588 struct asus_laptop *asus = bl_get_data(bd);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400589 unsigned long long value;
Corentin Chary9a816852007-03-11 10:25:38 +0100590 acpi_status rv = AE_OK;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100591
Corentin Charyd99b5772010-01-22 21:20:57 +0100592 rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
593 NULL, &value);
Corentin Chary9a816852007-03-11 10:25:38 +0100594 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200595 pr_warning("Error reading brightness\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100596
597 return value;
598}
599
Corentin Chary4d441512010-01-13 22:26:24 +0100600static int asus_set_brightness(struct backlight_device *bd, int value)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100601{
Corentin Charyd99b5772010-01-22 21:20:57 +0100602 struct asus_laptop *asus = bl_get_data(bd);
603
604 if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200605 pr_warning("Error changing brightness\n");
Corentin Charye5b50f62009-11-28 10:19:55 +0100606 return -EIO;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100607 }
Corentin Charye5b50f62009-11-28 10:19:55 +0100608 return 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100609}
610
611static int update_bl_status(struct backlight_device *bd)
612{
Corentin Chary9129d142009-12-01 22:39:41 +0100613 struct asus_laptop *asus = bl_get_data(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100614 int rv;
Richard Purdie599a52d2007-02-10 23:07:48 +0000615 int value = bd->props.brightness;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100616
Corentin Chary4d441512010-01-13 22:26:24 +0100617 rv = asus_set_brightness(bd, value);
Len Brown8def05f2007-01-30 01:46:43 -0500618 if (rv)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100619 return rv;
620
Richard Purdie599a52d2007-02-10 23:07:48 +0000621 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
Corentin Chary3e68ae72010-01-13 22:10:39 +0100622 return asus_lcd_set(asus, value);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100623}
624
Corentin Charybe4ee822009-12-06 16:27:09 +0100625static struct backlight_ops asusbl_ops = {
Corentin Chary4d441512010-01-13 22:26:24 +0100626 .get_brightness = asus_read_brightness,
Corentin Charybe4ee822009-12-06 16:27:09 +0100627 .update_status = update_bl_status,
628};
629
Corentin Charya539df52010-01-25 22:53:21 +0100630static 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
Corentin Charybe4ee822009-12-06 16:27:09 +0100640static int asus_backlight_init(struct asus_laptop *asus)
641{
642 struct backlight_device *bd;
643 struct device *dev = &asus->platform_device->dev;
644
Corentin Charyd99b5772010-01-22 21:20:57 +0100645 if (!acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) &&
646 !acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL) &&
647 lcd_switch_handle) {
Corentin Charybe4ee822009-12-06 16:27:09 +0100648 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;
Corentin Charybe4ee822009-12-06 16:27:09 +0100659 bd->props.power = FB_BLANK_UNBLANK;
Corentin Charyd99b5772010-01-22 21:20:57 +0100660 bd->props.brightness = asus_read_brightness(bd);
Corentin Charybe4ee822009-12-06 16:27:09 +0100661 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);
Corentin Charya539df52010-01-25 22:53:21 +0100670 asus->backlight_device = NULL;
Corentin Charybe4ee822009-12-06 16:27:09 +0100671}
672
Corentin Chary85091b72007-01-26 14:04:30 +0100673/*
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,
Len Brown8def05f2007-01-30 01:46:43 -0500683 struct device_attribute *attr, char *page)
Corentin Chary85091b72007-01-26 14:04:30 +0100684{
Corentin Chary9129d142009-12-01 22:39:41 +0100685 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary85091b72007-01-26 14:04:30 +0100686 int len = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400687 unsigned long long temp;
Corentin Charybe966662009-08-29 10:28:29 +0200688 char buf[16]; /* enough for all info */
Corentin Chary9a816852007-03-11 10:25:38 +0100689 acpi_status rv = AE_OK;
690
Corentin Chary85091b72007-01-26 14:04:30 +0100691 /*
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
Corentin Chary50a90c42009-11-30 21:55:12 +0100696 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
697 len += sprintf(page + len, "Model reference : %s\n", asus->name);
Corentin Chary85091b72007-01-26 14:04:30 +0100698 /*
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 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100704 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100705 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000706 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 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100714 rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
Corentin Chary1d4a3802009-08-28 12:56:46 +0000715 if (!ACPI_FAILURE(rv))
716 len += sprintf(page + len, "HRWS value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100717 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100718 /*
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 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100725 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100726 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000727 len += sprintf(page + len, "ASYM value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100728 (uint) temp);
Corentin Chary7c247642009-11-30 22:13:54 +0100729 if (asus->dsdt_info) {
730 snprintf(buf, 16, "%d", asus->dsdt_info->length);
Corentin Chary85091b72007-01-26 14:04:30 +0100731 len += sprintf(page + len, "DSDT length : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100732 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
Corentin Chary85091b72007-01-26 14:04:30 +0100733 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100734 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100735 len += sprintf(page + len, "DSDT revision : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100736 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100737 len += sprintf(page + len, "OEM id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100738 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100739 len += sprintf(page + len, "OEM table id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100740 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100741 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100742 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100743 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100744 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100745 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
Corentin Chary17e78f62010-01-13 22:21:33 +0100762static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
763 const char *buf, size_t count,
Corentin Charyd99b5772010-01-22 21:20:57 +0100764 const char *method)
Corentin Chary4564de12007-01-26 14:04:40 +0100765{
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
Corentin Charyd99b5772010-01-22 21:20:57 +0100773 if (write_acpi_int(asus->handle, method, value))
Corentin Chary17e78f62010-01-13 22:21:33 +0100774 return -ENODEV;
Corentin Chary4564de12007-01-26 14:04:40 +0100775 return rv;
776}
777
778/*
Corentin Chary722ad972007-01-26 14:04:55 +0100779 * LEDD display
780 */
781static ssize_t show_ledd(struct device *dev,
782 struct device_attribute *attr, char *buf)
783{
Corentin Chary9129d142009-12-01 22:39:41 +0100784 struct asus_laptop *asus = dev_get_drvdata(dev);
785
Corentin Chary50a90c42009-11-30 21:55:12 +0100786 return sprintf(buf, "0x%08x\n", asus->ledd_status);
Corentin Chary722ad972007-01-26 14:04:55 +0100787}
788
789static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
790 const char *buf, size_t count)
791{
Corentin Chary9129d142009-12-01 22:39:41 +0100792 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary722ad972007-01-26 14:04:55 +0100793 int rv, value;
794
795 rv = parse_arg(buf, count, &value);
796 if (rv > 0) {
Corentin Charyd99b5772010-01-22 21:20:57 +0100797 if (write_acpi_int(asus->handle, METHOD_LEDD, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200798 pr_warning("LED display write failed\n");
Corentin Chary722ad972007-01-26 14:04:55 +0100799 else
Corentin Chary50a90c42009-11-30 21:55:12 +0100800 asus->ledd_status = (u32) value;
Corentin Chary722ad972007-01-26 14:04:55 +0100801 }
802 return rv;
803}
804
805/*
Corentin Charyaa9df932010-01-13 21:49:10 +0100806 * 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
Corentin Charyd99b5772010-01-22 21:20:57 +0100816 rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
817 NULL, &status);
Corentin Charyaa9df932010-01-13 21:49:10 +0100818 if (ACPI_FAILURE(rv)) {
819 pr_warning("Error reading Wireless status\n");
820 return -EINVAL;
821 }
822 return !!(status & mask);
823}
824
825/*
Corentin Chary4564de12007-01-26 14:04:40 +0100826 * WLAN
827 */
Corentin Charye5593bf2010-01-17 17:20:11 +0100828static 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
Corentin Chary4564de12007-01-26 14:04:40 +0100837static ssize_t show_wlan(struct device *dev,
838 struct device_attribute *attr, char *buf)
839{
Corentin Chary9129d142009-12-01 22:39:41 +0100840 struct asus_laptop *asus = dev_get_drvdata(dev);
841
Corentin Chary17e78f62010-01-13 22:21:33 +0100842 return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100843}
844
845static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
846 const char *buf, size_t count)
847{
Corentin Chary9129d142009-12-01 22:39:41 +0100848 struct asus_laptop *asus = dev_get_drvdata(dev);
849
Corentin Charyd99b5772010-01-22 21:20:57 +0100850 return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
Corentin Chary4564de12007-01-26 14:04:40 +0100851}
852
853/*
854 * Bluetooth
855 */
Corentin Charye5593bf2010-01-17 17:20:11 +0100856static 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
Corentin Chary4564de12007-01-26 14:04:40 +0100865static ssize_t show_bluetooth(struct device *dev,
866 struct device_attribute *attr, char *buf)
867{
Corentin Chary9129d142009-12-01 22:39:41 +0100868 struct asus_laptop *asus = dev_get_drvdata(dev);
869
Corentin Chary17e78f62010-01-13 22:21:33 +0100870 return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100871}
872
Len Brown8def05f2007-01-30 01:46:43 -0500873static ssize_t store_bluetooth(struct device *dev,
874 struct device_attribute *attr, const char *buf,
875 size_t count)
Corentin Chary4564de12007-01-26 14:04:40 +0100876{
Corentin Chary9129d142009-12-01 22:39:41 +0100877 struct asus_laptop *asus = dev_get_drvdata(dev);
878
Corentin Charyd99b5772010-01-22 21:20:57 +0100879 return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
Corentin Chary4564de12007-01-26 14:04:40 +0100880}
881
Corentin Chary78127b42007-01-26 14:04:49 +0100882/*
883 * Display
884 */
Corentin Chary4d441512010-01-13 22:26:24 +0100885static void asus_set_display(struct asus_laptop *asus, int value)
Corentin Chary78127b42007-01-26 14:04:49 +0100886{
887 /* no sanity check needed for now */
Corentin Charyd99b5772010-01-22 21:20:57 +0100888 if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200889 pr_warning("Error setting display\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100890 return;
891}
892
Corentin Chary9129d142009-12-01 22:39:41 +0100893static int read_display(struct asus_laptop *asus)
Corentin Chary78127b42007-01-26 14:04:49 +0100894{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400895 unsigned long long value = 0;
Corentin Chary9a816852007-03-11 10:25:38 +0100896 acpi_status rv = AE_OK;
Corentin Chary78127b42007-01-26 14:04:49 +0100897
Corentin Charybe966662009-08-29 10:28:29 +0200898 /*
899 * In most of the case, we know how to set the display, but sometime
900 * we can't read it
901 */
Len Brown8def05f2007-01-30 01:46:43 -0500902 if (display_get_handle) {
Corentin Chary9a816852007-03-11 10:25:38 +0100903 rv = acpi_evaluate_integer(display_get_handle, NULL,
904 NULL, &value);
905 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200906 pr_warning("Error reading display status\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100907 }
908
909 value &= 0x0F; /* needed for some models, shouldn't hurt others */
910
911 return value;
912}
Len Brown8def05f2007-01-30 01:46:43 -0500913
Corentin Chary78127b42007-01-26 14:04:49 +0100914/*
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{
Corentin Chary9129d142009-12-01 22:39:41 +0100921 struct asus_laptop *asus = dev_get_drvdata(dev);
922
923 return sprintf(buf, "%d\n", read_display(asus));
Corentin Chary78127b42007-01-26 14:04:49 +0100924}
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{
Corentin Chary9129d142009-12-01 22:39:41 +0100936 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary78127b42007-01-26 14:04:49 +0100937 int rv, value;
938
939 rv = parse_arg(buf, count, &value);
940 if (rv > 0)
Corentin Chary4d441512010-01-13 22:26:24 +0100941 asus_set_display(asus, value);
Corentin Chary78127b42007-01-26 14:04:49 +0100942 return rv;
943}
944
Corentin Chary8b857352007-01-26 14:04:58 +0100945/*
946 * Light Sens
947 */
Corentin Chary4d441512010-01-13 22:26:24 +0100948static void asus_als_switch(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100949{
Corentin Charyd99b5772010-01-22 21:20:57 +0100950 if (write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200951 pr_warning("Error setting light sensor switch\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100952 asus->light_switch = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100953}
954
955static ssize_t show_lssw(struct device *dev,
956 struct device_attribute *attr, char *buf)
957{
Corentin Chary9129d142009-12-01 22:39:41 +0100958 struct asus_laptop *asus = dev_get_drvdata(dev);
959
Corentin Chary50a90c42009-11-30 21:55:12 +0100960 return sprintf(buf, "%d\n", asus->light_switch);
Corentin Chary8b857352007-01-26 14:04:58 +0100961}
962
963static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
964 const char *buf, size_t count)
965{
Corentin Chary9129d142009-12-01 22:39:41 +0100966 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100967 int rv, value;
968
969 rv = parse_arg(buf, count, &value);
970 if (rv > 0)
Corentin Chary4d441512010-01-13 22:26:24 +0100971 asus_als_switch(asus, value ? 1 : 0);
Corentin Chary8b857352007-01-26 14:04:58 +0100972
973 return rv;
974}
975
Corentin Chary4d441512010-01-13 22:26:24 +0100976static void asus_als_level(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100977{
Corentin Charyd99b5772010-01-22 21:20:57 +0100978 if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200979 pr_warning("Error setting light sensor level\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100980 asus->light_level = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100981}
982
983static ssize_t show_lslvl(struct device *dev,
984 struct device_attribute *attr, char *buf)
985{
Corentin Chary9129d142009-12-01 22:39:41 +0100986 struct asus_laptop *asus = dev_get_drvdata(dev);
987
Corentin Chary50a90c42009-11-30 21:55:12 +0100988 return sprintf(buf, "%d\n", asus->light_level);
Corentin Chary8b857352007-01-26 14:04:58 +0100989}
990
991static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
992 const char *buf, size_t count)
993{
Corentin Chary9129d142009-12-01 22:39:41 +0100994 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100995 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 */
Corentin Chary4d441512010-01-13 22:26:24 +01001001 asus_als_level(asus, value);
Corentin Chary8b857352007-01-26 14:04:58 +01001002 }
1003
1004 return rv;
1005}
1006
Corentin Charye539c2f2007-05-06 14:47:06 +02001007/*
1008 * GPS
Corentin Chary6358bf22010-01-13 21:55:44 +01001009 * TODO: use rfkill
Corentin Charye539c2f2007-05-06 14:47:06 +02001010 */
Corentin Chary6358bf22010-01-13 21:55:44 +01001011static int asus_gps_status(struct asus_laptop *asus)
1012{
1013 unsigned long long status;
1014 acpi_status rv = AE_OK;
1015
Corentin Charyd99b5772010-01-22 21:20:57 +01001016 rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
1017 NULL, &status);
Corentin Chary6358bf22010-01-13 21:55:44 +01001018 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{
Corentin Charyd99b5772010-01-22 21:20:57 +01001027 const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
Corentin Chary6358bf22010-01-13 21:55:44 +01001028
Corentin Charyd99b5772010-01-22 21:20:57 +01001029 if (write_acpi_int(asus->handle, meth, 0x02))
Corentin Chary6358bf22010-01-13 21:55:44 +01001030 return -ENODEV;
1031 return 0;
1032}
1033
Corentin Charye539c2f2007-05-06 14:47:06 +02001034static ssize_t show_gps(struct device *dev,
1035 struct device_attribute *attr, char *buf)
1036{
Corentin Chary9129d142009-12-01 22:39:41 +01001037 struct asus_laptop *asus = dev_get_drvdata(dev);
1038
Corentin Chary6358bf22010-01-13 21:55:44 +01001039 return sprintf(buf, "%d\n", asus_gps_status(asus));
Corentin Charye539c2f2007-05-06 14:47:06 +02001040}
1041
1042static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
1043 const char *buf, size_t count)
1044{
Corentin Chary6358bf22010-01-13 21:55:44 +01001045 struct asus_laptop *asus = dev_get_drvdata(dev);
1046 int rv, value;
1047 int ret;
Corentin Chary9129d142009-12-01 22:39:41 +01001048
Corentin Chary6358bf22010-01-13 21:55:44 +01001049 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;
Corentin Charye539c2f2007-05-06 14:47:06 +02001056}
1057
Corentin Chary034ce902009-01-20 16:17:43 +01001058/*
Corentin Charybe4ee822009-12-06 16:27:09 +01001059 * Input device (i.e. hotkeys)
Corentin Chary034ce902009-01-20 16:17:43 +01001060 */
Corentin Charybe4ee822009-12-06 16:27:09 +01001061static void asus_input_notify(struct asus_laptop *asus, int event)
1062{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001063 if (asus->inputdev)
1064 sparse_keymap_report_event(asus->inputdev, event, 1, true);
Corentin Charybe4ee822009-12-06 16:27:09 +01001065}
1066
1067static int asus_input_init(struct asus_laptop *asus)
1068{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001069 struct input_dev *input;
1070 int error;
Corentin Charybe4ee822009-12-06 16:27:09 +01001071
Corentin Chary66a71dd2010-01-25 22:50:11 +01001072 input = input_allocate_device();
1073 if (!input) {
Corentin Charybe4ee822009-12-06 16:27:09 +01001074 pr_info("Unable to allocate input device\n");
1075 return 0;
1076 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001077 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);
Corentin Charybe4ee822009-12-06 16:27:09 +01001082
Corentin Chary66a71dd2010-01-25 22:50:11 +01001083 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;
Corentin Charybe4ee822009-12-06 16:27:09 +01001087 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001088 error = input_register_device(input);
1089 if (error) {
Corentin Charybe4ee822009-12-06 16:27:09 +01001090 pr_info("Unable to register input device\n");
Corentin Chary66a71dd2010-01-25 22:50:11 +01001091 goto err_device;
Corentin Charybe4ee822009-12-06 16:27:09 +01001092 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001093
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;
Corentin Charybe4ee822009-12-06 16:27:09 +01001102}
1103
1104static void asus_input_exit(struct asus_laptop *asus)
1105{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001106 if (asus->inputdev) {
1107 sparse_keymap_free(asus->inputdev);
Corentin Charybe4ee822009-12-06 16:27:09 +01001108 input_unregister_device(asus->inputdev);
Corentin Chary66a71dd2010-01-25 22:50:11 +01001109 }
Corentin Charybe4ee822009-12-06 16:27:09 +01001110}
1111
1112/*
1113 * ACPI driver
1114 */
Corentin Chary600ad522009-11-30 21:42:42 +01001115static void asus_acpi_notify(struct acpi_device *device, u32 event)
Corentin Chary85091b72007-01-26 14:04:30 +01001116{
Corentin Chary9129d142009-12-01 22:39:41 +01001117 struct asus_laptop *asus = acpi_driver_data(device);
Corentin Chary6050c8d2009-02-15 19:30:19 +01001118 u16 count;
Corentin Chary034ce902009-01-20 16:17:43 +01001119
Corentin Chary6b7091e2007-01-26 14:04:45 +01001120 /*
1121 * We need to tell the backlight device when the backlight power is
1122 * switched
1123 */
Corentin Chary3e68ae72010-01-13 22:10:39 +01001124 if (event == ATKD_LCD_ON)
Corentin Chary9129d142009-12-01 22:39:41 +01001125 lcd_blank(asus, FB_BLANK_UNBLANK);
Corentin Chary3e68ae72010-01-13 22:10:39 +01001126 else if (event == ATKD_LCD_OFF)
Corentin Chary9129d142009-12-01 22:39:41 +01001127 lcd_blank(asus, FB_BLANK_POWERDOWN);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001128
Corentin Chary619d8b12009-11-28 10:35:37 +01001129 /* TODO Find a better way to handle events count. */
Corentin Chary50a90c42009-11-30 21:55:12 +01001130 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,
Corentin Chary6050c8d2009-02-15 19:30:19 +01001134 count);
Corentin Chary85091b72007-01-26 14:04:30 +01001135
Corentin Charya539df52010-01-25 22:53:21 +01001136 /* 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 }
Corentin Charybe4ee822009-12-06 16:27:09 +01001146 asus_input_notify(asus, event);
Corentin Chary85091b72007-01-26 14:04:30 +01001147}
1148
1149#define ASUS_CREATE_DEVICE_ATTR(_name) \
1150 struct device_attribute dev_attr_##_name = { \
1151 .attr = { \
1152 .name = __stringify(_name), \
Tejun Heo7b595752007-06-14 03:45:17 +09001153 .mode = 0 }, \
Corentin Chary85091b72007-01-26 14:04:30 +01001154 .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);
Corentin Chary4564de12007-01-26 14:04:40 +01001166static ASUS_CREATE_DEVICE_ATTR(wlan);
1167static ASUS_CREATE_DEVICE_ATTR(bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +01001168static ASUS_CREATE_DEVICE_ATTR(display);
Corentin Chary722ad972007-01-26 14:04:55 +01001169static ASUS_CREATE_DEVICE_ATTR(ledd);
Corentin Chary8b857352007-01-26 14:04:58 +01001170static ASUS_CREATE_DEVICE_ATTR(ls_switch);
1171static ASUS_CREATE_DEVICE_ATTR(ls_level);
Corentin Charye539c2f2007-05-06 14:47:06 +02001172static ASUS_CREATE_DEVICE_ATTR(gps);
Corentin Chary85091b72007-01-26 14:04:30 +01001173
1174static struct attribute *asuspf_attributes[] = {
Len Brown8def05f2007-01-30 01:46:43 -05001175 &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,
Corentin Charye539c2f2007-05-06 14:47:06 +02001182 &dev_attr_gps.attr,
Len Brown8def05f2007-01-30 01:46:43 -05001183 NULL
Corentin Chary85091b72007-01-26 14:04:30 +01001184};
1185
Corentin Chary600ad522009-11-30 21:42:42 +01001186static struct attribute_group platform_attribute_group = {
Len Brown8def05f2007-01-30 01:46:43 -05001187 .attrs = asuspf_attributes
Corentin Chary85091b72007-01-26 14:04:30 +01001188};
1189
Corentin Chary9129d142009-12-01 22:39:41 +01001190static int asus_platform_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001191{
1192 int result;
1193
Corentin Chary50a90c42009-11-30 21:55:12 +01001194 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1195 if (!asus->platform_device)
Corentin Chary600ad522009-11-30 21:42:42 +01001196 return -ENOMEM;
Corentin Chary9129d142009-12-01 22:39:41 +01001197 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001198
Corentin Chary50a90c42009-11-30 21:55:12 +01001199 result = platform_device_add(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001200 if (result)
1201 goto fail_platform_device;
1202
Corentin Chary50a90c42009-11-30 21:55:12 +01001203 result = sysfs_create_group(&asus->platform_device->dev.kobj,
Corentin Chary600ad522009-11-30 21:42:42 +01001204 &platform_attribute_group);
1205 if (result)
1206 goto fail_sysfs;
1207 return 0;
1208
1209fail_sysfs:
Corentin Chary50a90c42009-11-30 21:55:12 +01001210 platform_device_del(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001211fail_platform_device:
Corentin Chary50a90c42009-11-30 21:55:12 +01001212 platform_device_put(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001213 return result;
1214}
1215
Corentin Chary9129d142009-12-01 22:39:41 +01001216static void asus_platform_exit(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001217{
Corentin Chary50a90c42009-11-30 21:55:12 +01001218 sysfs_remove_group(&asus->platform_device->dev.kobj,
Corentin Chary600ad522009-11-30 21:42:42 +01001219 &platform_attribute_group);
Corentin Chary50a90c42009-11-30 21:55:12 +01001220 platform_device_unregister(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001221}
1222
1223static struct platform_driver platform_driver = {
Len Brown8def05f2007-01-30 01:46:43 -05001224 .driver = {
Corentin Chary9129d142009-12-01 22:39:41 +01001225 .name = ASUS_LAPTOP_FILE,
1226 .owner = THIS_MODULE,
1227 }
Corentin Chary85091b72007-01-26 14:04:30 +01001228};
1229
Corentin Chary9129d142009-12-01 22:39:41 +01001230static void asus_laptop_add_fs(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001231{
1232 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
Corentin Chary4564de12007-01-26 14:04:40 +01001233
Corentin Charyd99b5772010-01-22 21:20:57 +01001234 if (!acpi_check_handle(asus->handle, METHOD_WLAN, NULL))
Corentin Chary4564de12007-01-26 14:04:40 +01001235 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
1236
Corentin Charyd99b5772010-01-22 21:20:57 +01001237 if (!acpi_check_handle(asus->handle, METHOD_BLUETOOTH, NULL))
Corentin Chary4564de12007-01-26 14:04:40 +01001238 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
1239 show_bluetooth, store_bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +01001240
Corentin Charyd99b5772010-01-22 21:20:57 +01001241 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 }
Corentin Chary78127b42007-01-26 14:04:49 +01001248
Corentin Charyd99b5772010-01-22 21:20:57 +01001249 if (!acpi_check_handle(asus->handle, METHOD_LEDD, NULL))
Corentin Chary722ad972007-01-26 14:04:55 +01001250 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
1251
Corentin Charyd99b5772010-01-22 21:20:57 +01001252 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1253 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
Corentin Chary8b857352007-01-26 14:04:58 +01001254 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
1255 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
1256 }
Corentin Charye539c2f2007-05-06 14:47:06 +02001257
Corentin Charyd99b5772010-01-22 21:20:57 +01001258 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))
Corentin Charye539c2f2007-05-06 14:47:06 +02001261 ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
Corentin Chary85091b72007-01-26 14:04:30 +01001262}
1263
Len Brown8def05f2007-01-30 01:46:43 -05001264static int asus_handle_init(char *name, acpi_handle * handle,
Corentin Chary85091b72007-01-26 14:04:30 +01001265 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
Corentin Chary85091b72007-01-26 14:04:30 +01001284/*
Corentin Chary50a90c42009-11-30 21:55:12 +01001285 * 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
Corentin Chary85091b72007-01-26 14:04:30 +01001288 */
Corentin Chary7c247642009-11-30 22:13:54 +01001289static int asus_laptop_get_info(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001290{
1291 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Corentin Chary85091b72007-01-26 14:04:30 +01001292 union acpi_object *model = NULL;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001293 unsigned long long bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +01001294 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
Corentin Chary7c247642009-11-30 22:13:54 +01001302 * HID), this bit will be moved.
Corentin Chary85091b72007-01-26 14:04:30 +01001303 */
Corentin Chary7c247642009-11-30 22:13:54 +01001304 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
Corentin Chary85091b72007-01-26 14:04:30 +01001305 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001306 pr_warning("Couldn't get the DSDT table header\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001307
1308 /* We have to write 0 on init this far for all ASUS models */
Corentin Chary50a90c42009-11-30 21:55:12 +01001309 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001310 pr_err("Hotkey initialization failed\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001311 return -ENODEV;
1312 }
1313
1314 /* This needs to be called for some laptops to init properly */
Corentin Chary9a816852007-03-11 10:25:38 +01001315 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001316 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
Corentin Chary9a816852007-03-11 10:25:38 +01001317 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001318 pr_warning("Error calling BSTS\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001319 else if (bsts_result)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001320 pr_notice("BSTS called, 0x%02x returned\n",
Corentin Chary9a816852007-03-11 10:25:38 +01001321 (uint) bsts_result);
Corentin Chary85091b72007-01-26 14:04:30 +01001322
Corentin Chary185e5af2007-03-11 10:27:33 +01001323 /* This too ... */
Corentin Charye5593bf2010-01-17 17:20:11 +01001324 if (write_acpi_int(asus->handle, "CWAP", wapf))
1325 pr_err("Error calling CWAP(%d)\n", wapf);
Corentin Chary85091b72007-01-26 14:04:30 +01001326 /*
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 }
Corentin Chary50a90c42009-11-30 21:55:12 +01001346 asus->name = kstrdup(string, GFP_KERNEL);
1347 if (!asus->name)
Corentin Chary85091b72007-01-26 14:04:30 +01001348 return -ENOMEM;
1349
Len Brown8def05f2007-01-30 01:46:43 -05001350 if (*string)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001351 pr_notice(" %s model detected\n", string);
Corentin Chary85091b72007-01-26 14:04:30 +01001352
Corentin Chary4564de12007-01-26 14:04:40 +01001353 /*
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.
Corentin Chary4564de12007-01-26 14:04:40 +01001357 */
Corentin Chary9a816852007-03-11 10:25:38 +01001358 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001359 acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
Corentin Charyd99b5772010-01-22 21:20:57 +01001360 if (!ACPI_FAILURE(status))
1361 pr_notice(" HRWS returned %x", (int)hwrs_result);
Corentin Chary4564de12007-01-26 14:04:40 +01001362
Corentin Charyd99b5772010-01-22 21:20:57 +01001363 if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
Corentin Charyaa9df932010-01-13 21:49:10 +01001364 asus->have_rsts = true;
Corentin Chary4564de12007-01-26 14:04:40 +01001365
Corentin Charyd99b5772010-01-22 21:20:57 +01001366 /* Scheduled for removal */
Corentin Chary6b7091e2007-01-26 14:04:45 +01001367 ASUS_HANDLE_INIT(lcd_switch);
Corentin Chary78127b42007-01-26 14:04:49 +01001368 ASUS_HANDLE_INIT(display_get);
1369
Corentin Chary85091b72007-01-26 14:04:30 +01001370 kfree(model);
1371
1372 return AE_OK;
1373}
1374
Corentin Chary600ad522009-11-30 21:42:42 +01001375static bool asus_device_present;
1376
Corentin Chary9129d142009-12-01 22:39:41 +01001377static int __devinit asus_acpi_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001378{
1379 int result = 0;
1380
Corentin Chary50a90c42009-11-30 21:55:12 +01001381 result = acpi_bus_get_status(asus->device);
Corentin Chary600ad522009-11-30 21:42:42 +01001382 if (result)
1383 return result;
Corentin Chary50a90c42009-11-30 21:55:12 +01001384 if (!asus->device->status.present) {
Corentin Chary600ad522009-11-30 21:42:42 +01001385 pr_err("Hotkey device not present, aborting\n");
1386 return -ENODEV;
1387 }
1388
Corentin Chary7c247642009-11-30 22:13:54 +01001389 result = asus_laptop_get_info(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001390 if (result)
1391 return result;
1392
Corentin Chary9129d142009-12-01 22:39:41 +01001393 asus_laptop_add_fs(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001394
1395 /* WLED and BLED are on by default */
Corentin Charybe4ee822009-12-06 16:27:09 +01001396 if (bluetooth_status >= 0)
Corentin Charye5593bf2010-01-17 17:20:11 +01001397 asus_bluetooth_set(asus, !!bluetooth_status);
1398
Corentin Charyd0930a22010-01-17 17:21:13 +01001399 if (wlan_status >= 0)
1400 asus_wlan_set(asus, !!wlan_status);
Corentin Chary600ad522009-11-30 21:42:42 +01001401
1402 /* Keyboard Backlight is on by default */
Corentin Charyd99b5772010-01-22 21:20:57 +01001403 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
Corentin Chary4d441512010-01-13 22:26:24 +01001404 asus_kled_set(asus, 1);
Corentin Chary600ad522009-11-30 21:42:42 +01001405
1406 /* LED display is off by default */
Corentin Chary50a90c42009-11-30 21:55:12 +01001407 asus->ledd_status = 0xFFF;
Corentin Chary600ad522009-11-30 21:42:42 +01001408
1409 /* Set initial values of light sensor and level */
Corentin Charybe4ee822009-12-06 16:27:09 +01001410 asus->light_switch = 0; /* Default to light sensor disabled */
1411 asus->light_level = 5; /* level 5 for sensor sensitivity */
Corentin Chary600ad522009-11-30 21:42:42 +01001412
Corentin Charyd99b5772010-01-22 21:20:57 +01001413 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1414 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
Corentin Chary4d441512010-01-13 22:26:24 +01001415 asus_als_switch(asus, asus->light_switch);
Corentin Chary4d441512010-01-13 22:26:24 +01001416 asus_als_level(asus, asus->light_level);
Corentin Charyd99b5772010-01-22 21:20:57 +01001417 }
Corentin Chary600ad522009-11-30 21:42:42 +01001418
1419 /* GPS is on by default */
Corentin Charyd99b5772010-01-22 21:20:57 +01001420 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);
Corentin Chary47ee0e92010-01-24 11:17:15 +01001424
1425 asus->lcd_state = 1; /* LCD should be on when the module load */
Corentin Chary600ad522009-11-30 21:42:42 +01001426 return result;
1427}
1428
1429static int __devinit asus_acpi_add(struct acpi_device *device)
1430{
Corentin Chary9129d142009-12-01 22:39:41 +01001431 struct asus_laptop *asus;
Corentin Chary600ad522009-11-30 21:42:42 +01001432 int result;
1433
1434 pr_notice("Asus Laptop Support version %s\n",
1435 ASUS_LAPTOP_VERSION);
Corentin Chary50a90c42009-11-30 21:55:12 +01001436 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1437 if (!asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001438 return -ENOMEM;
Corentin Chary50a90c42009-11-30 21:55:12 +01001439 asus->handle = device->handle;
1440 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1441 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1442 device->driver_data = asus;
1443 asus->device = device;
Corentin Chary600ad522009-11-30 21:42:42 +01001444
Corentin Chary9129d142009-12-01 22:39:41 +01001445 result = asus_acpi_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001446 if (result)
1447 goto fail_platform;
1448
1449 /*
1450 * Register the platform device first. It is used as a parent for the
1451 * sub-devices below.
1452 */
Corentin Chary9129d142009-12-01 22:39:41 +01001453 result = asus_platform_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001454 if (result)
1455 goto fail_platform;
1456
1457 if (!acpi_video_backlight_support()) {
Corentin Chary9129d142009-12-01 22:39:41 +01001458 result = asus_backlight_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001459 if (result)
1460 goto fail_backlight;
1461 } else
1462 pr_info("Backlight controlled by ACPI video driver\n");
1463
Corentin Chary9129d142009-12-01 22:39:41 +01001464 result = asus_input_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001465 if (result)
1466 goto fail_input;
1467
Corentin Chary9129d142009-12-01 22:39:41 +01001468 result = asus_led_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001469 if (result)
1470 goto fail_led;
1471
1472 asus_device_present = true;
1473 return 0;
1474
1475fail_led:
Corentin Chary9129d142009-12-01 22:39:41 +01001476 asus_input_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001477fail_input:
Corentin Chary9129d142009-12-01 22:39:41 +01001478 asus_backlight_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001479fail_backlight:
Corentin Chary9129d142009-12-01 22:39:41 +01001480 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001481fail_platform:
Corentin Chary50a90c42009-11-30 21:55:12 +01001482 kfree(asus->name);
1483 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001484
1485 return result;
1486}
1487
1488static int asus_acpi_remove(struct acpi_device *device, int type)
1489{
Corentin Chary9129d142009-12-01 22:39:41 +01001490 struct asus_laptop *asus = acpi_driver_data(device);
1491
1492 asus_backlight_exit(asus);
1493 asus_led_exit(asus);
1494 asus_input_exit(asus);
1495 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001496
Corentin Chary50a90c42009-11-30 21:55:12 +01001497 kfree(asus->name);
1498 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001499 return 0;
1500}
1501
1502static const struct acpi_device_id asus_device_ids[] = {
1503 {"ATK0100", 0},
1504 {"ATK0101", 0},
1505 {"", 0},
1506};
1507MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1508
1509static struct acpi_driver asus_acpi_driver = {
Corentin Chary50a90c42009-11-30 21:55:12 +01001510 .name = ASUS_LAPTOP_NAME,
1511 .class = ASUS_LAPTOP_CLASS,
Corentin Chary600ad522009-11-30 21:42:42 +01001512 .owner = THIS_MODULE,
1513 .ids = asus_device_ids,
1514 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1515 .ops = {
1516 .add = asus_acpi_add,
1517 .remove = asus_acpi_remove,
1518 .notify = asus_acpi_notify,
1519 },
1520};
1521
Corentin Chary85091b72007-01-26 14:04:30 +01001522static int __init asus_laptop_init(void)
1523{
1524 int result;
1525
Corentin Chary600ad522009-11-30 21:42:42 +01001526 result = platform_driver_register(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001527 if (result < 0)
1528 return result;
1529
Corentin Chary600ad522009-11-30 21:42:42 +01001530 result = acpi_bus_register_driver(&asus_acpi_driver);
1531 if (result < 0)
1532 goto fail_acpi_driver;
1533 if (!asus_device_present) {
1534 result = -ENODEV;
1535 goto fail_no_device;
Corentin Chary85091b72007-01-26 14:04:30 +01001536 }
Len Brown8def05f2007-01-30 01:46:43 -05001537 return 0;
Corentin Chary85091b72007-01-26 14:04:30 +01001538
Corentin Chary600ad522009-11-30 21:42:42 +01001539fail_no_device:
1540 acpi_bus_unregister_driver(&asus_acpi_driver);
1541fail_acpi_driver:
1542 platform_driver_unregister(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001543 return result;
1544}
1545
Corentin Chary600ad522009-11-30 21:42:42 +01001546static void __exit asus_laptop_exit(void)
1547{
1548 acpi_bus_unregister_driver(&asus_acpi_driver);
1549 platform_driver_unregister(&platform_driver);
1550}
1551
Corentin Chary85091b72007-01-26 14:04:30 +01001552module_init(asus_laptop_init);
1553module_exit(asus_laptop_exit);