blob: 4af5709f83179aa62f44a11e69684f6713ab93c4 [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>
Corentin Chary060cbce2010-01-28 10:52:40 +010048#include <linux/uaccess.h>
Corentin Chary034ce902009-01-20 16:17:43 +010049#include <linux/input.h>
Corentin Chary66a71dd2010-01-25 22:50:11 +010050#include <linux/input/sparse-keymap.h>
Corentin Chary18e13112010-01-25 23:29:24 +010051#include <linux/rfkill.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Corentin Chary060cbce2010-01-28 10:52:40 +010053#include <acpi/acpi_drivers.h>
54#include <acpi/acpi_bus.h>
Corentin Chary85091b72007-01-26 14:04:30 +010055
Corentin Chary91687cc2009-11-28 10:32:34 +010056#define ASUS_LAPTOP_VERSION "0.42"
Corentin Chary85091b72007-01-26 14:04:30 +010057
Corentin Chary50a90c42009-11-30 21:55:12 +010058#define ASUS_LAPTOP_NAME "Asus Laptop Support"
59#define ASUS_LAPTOP_CLASS "hotkey"
60#define ASUS_LAPTOP_DEVICE_NAME "Hotkey"
61#define ASUS_LAPTOP_FILE KBUILD_MODNAME
62#define ASUS_LAPTOP_PREFIX "\\_SB.ATKD."
Corentin Chary85091b72007-01-26 14:04:30 +010063
Corentin Chary85091b72007-01-26 14:04:30 +010064MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
Corentin Chary50a90c42009-11-30 21:55:12 +010065MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
Corentin Chary85091b72007-01-26 14:04:30 +010066MODULE_LICENSE("GPL");
67
Corentin Charybe966662009-08-29 10:28:29 +020068/*
69 * WAPF defines the behavior of the Fn+Fx wlan key
Corentin Chary185e5af2007-03-11 10:27:33 +010070 * The significance of values is yet to be found, but
71 * most of the time:
72 * 0x0 will do nothing
73 * 0x1 will allow to control the device with Fn+Fx key.
74 * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
75 * 0x5 like 0x1 or 0x4
76 * So, if something doesn't work as you want, just try other values =)
77 */
78static uint wapf = 1;
79module_param(wapf, uint, 0644);
80MODULE_PARM_DESC(wapf, "WAPF value");
81
Dan Carpenter668f4a02010-04-06 13:44:29 +030082static int wlan_status = 1;
83static int bluetooth_status = 1;
Corentin Chary0e875f42010-01-10 20:49:26 +010084
Dan Carpenter668f4a02010-04-06 13:44:29 +030085module_param(wlan_status, int, 0644);
Corentin Charyd0930a22010-01-17 17:21:13 +010086MODULE_PARM_DESC(wlan_status, "Set the wireless status on boot "
Corentin Chary0e875f42010-01-10 20:49:26 +010087 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
88 "default is 1");
89
Dan Carpenter668f4a02010-04-06 13:44:29 +030090module_param(bluetooth_status, int, 0644);
Corentin Chary0e875f42010-01-10 20:49:26 +010091MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
92 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
93 "default is 1");
94
Corentin Charybe4ee822009-12-06 16:27:09 +010095/*
96 * Some events we use, same for all Asus
97 */
Corentin Chary060cbce2010-01-28 10:52:40 +010098#define ATKD_BR_UP 0x10 /* (event & ~ATKD_BR_UP) = brightness level */
99#define ATKD_BR_DOWN 0x20 /* (event & ~ATKD_BR_DOWN) = britghness level */
Corentin Charya539df52010-01-25 22:53:21 +0100100#define ATKD_BR_MIN ATKD_BR_UP
Corentin Chary060cbce2010-01-28 10:52:40 +0100101#define ATKD_BR_MAX (ATKD_BR_DOWN | 0xF) /* 0x2f */
Corentin Charybe4ee822009-12-06 16:27:09 +0100102#define ATKD_LCD_ON 0x33
103#define ATKD_LCD_OFF 0x34
104
105/*
106 * Known bits returned by \_SB.ATKD.HWRS
107 */
108#define WL_HWRS 0x80
109#define BT_HWRS 0x100
110
111/*
112 * Flags for hotk status
113 * WL_ON and BT_ON are also used for wireless_status()
114 */
Corentin Chary17e78f62010-01-13 22:21:33 +0100115#define WL_RSTS 0x01 /* internal Wifi */
116#define BT_RSTS 0x02 /* internal Bluetooth */
Corentin Charybe4ee822009-12-06 16:27:09 +0100117
Corentin Charybe18cda2007-01-26 14:04:35 +0100118/* LED */
Corentin Charyd99b5772010-01-22 21:20:57 +0100119#define METHOD_MLED "MLED"
120#define METHOD_TLED "TLED"
121#define METHOD_RLED "RLED" /* W1JC */
122#define METHOD_PLED "PLED" /* A7J */
123#define METHOD_GLED "GLED" /* G1, G2 (probably) */
Corentin Charybe18cda2007-01-26 14:04:35 +0100124
Corentin Chary722ad972007-01-26 14:04:55 +0100125/* LEDD */
Corentin Charyd99b5772010-01-22 21:20:57 +0100126#define METHOD_LEDD "SLCM"
Corentin Chary722ad972007-01-26 14:04:55 +0100127
Corentin Charybe966662009-08-29 10:28:29 +0200128/*
129 * Bluetooth and WLAN
Corentin Chary4564de12007-01-26 14:04:40 +0100130 * WLED and BLED are not handled like other XLED, because in some dsdt
131 * they also control the WLAN/Bluetooth device.
132 */
Corentin Charyd99b5772010-01-22 21:20:57 +0100133#define METHOD_WLAN "WLED"
134#define METHOD_BLUETOOTH "BLED"
135#define METHOD_WL_STATUS "RSTS"
Corentin Chary4564de12007-01-26 14:04:40 +0100136
Corentin Chary6b7091e2007-01-26 14:04:45 +0100137/* Brightness */
Corentin Charyd99b5772010-01-22 21:20:57 +0100138#define METHOD_BRIGHTNESS_SET "SPLV"
139#define METHOD_BRIGHTNESS_GET "GPLV"
Corentin Chary6b7091e2007-01-26 14:04:45 +0100140
141/* Backlight */
Corentin Chary060cbce2010-01-28 10:52:40 +0100142static acpi_handle lcd_switch_handle;
Corentin Chary16721512010-03-02 22:24:12 +0100143static char *lcd_switch_paths[] = {
Corentin Chary060cbce2010-01-28 10:52:40 +0100144 "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
145 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
146 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
147 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
148 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
149 "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
150 "\\_SB.PCI0.PX40.Q10", /* S1x */
151 "\\Q10"}; /* A2x, L2D, L3D, M2E */
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 Chary060cbce2010-01-28 10:52:40 +0100155
156static acpi_handle display_get_handle;
Corentin Chary16721512010-03-02 22:24:12 +0100157static char *display_get_paths[] = {
Corentin Chary060cbce2010-01-28 10:52:40 +0100158 /* A6B, A6K A6R A7D F3JM L4R M6R A3G M6A M6V VX-1 V6J V6V W3Z */
159 "\\_SB.PCI0.P0P1.VGA.GETD",
160 /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V S5A M5A z33A W1Jc W2V G1 */
161 "\\_SB.PCI0.P0P2.VGA.GETD",
162 /* A6V A6Q */
163 "\\_SB.PCI0.P0P3.VGA.GETD",
164 /* A6T, A6M */
165 "\\_SB.PCI0.P0PA.VGA.GETD",
166 /* L3C */
167 "\\_SB.PCI0.PCI1.VGAC.NMAP",
168 /* Z96F */
169 "\\_SB.PCI0.VGA.GETD",
170 /* A2D */
171 "\\ACTD",
172 /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
173 "\\ADVG",
174 /* P30 */
175 "\\DNXT",
176 /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
177 "\\INFB",
178 /* A3F A6F A3N A3L M6N W3N W6A */
179 "\\SSTE"};
Corentin Chary78127b42007-01-26 14:04:49 +0100180
Corentin Charyd99b5772010-01-22 21:20:57 +0100181#define METHOD_ALS_CONTROL "ALSC" /* Z71A Z71V */
182#define METHOD_ALS_LEVEL "ALSL" /* Z71A Z71V */
Corentin Chary8b857352007-01-26 14:04:58 +0100183
Corentin Charye539c2f2007-05-06 14:47:06 +0200184/* GPS */
185/* R2H use different handle for GPS on/off */
Corentin Charyd99b5772010-01-22 21:20:57 +0100186#define METHOD_GPS_ON "SDON"
187#define METHOD_GPS_OFF "SDOF"
188#define METHOD_GPS_STATUS "GPST"
Corentin Charye539c2f2007-05-06 14:47:06 +0200189
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000190/* Keyboard light */
Corentin Charyd99b5772010-01-22 21:20:57 +0100191#define METHOD_KBD_LIGHT_SET "SLKB"
192#define METHOD_KBD_LIGHT_GET "GLKB"
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000193
Corentin Chary85091b72007-01-26 14:04:30 +0100194/*
Corentin Chary9129d142009-12-01 22:39:41 +0100195 * Define a specific led structure to keep the main structure clean
196 */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100197struct asus_led {
198 int wk;
199 struct work_struct work;
200 struct led_classdev led;
201 struct asus_laptop *asus;
202 const char *method;
Corentin Chary9129d142009-12-01 22:39:41 +0100203};
204
205/*
Corentin Chary85091b72007-01-26 14:04:30 +0100206 * This is the main structure, we can use it to store anything interesting
207 * about the hotk device
208 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100209struct asus_laptop {
Corentin Charybe966662009-08-29 10:28:29 +0200210 char *name; /* laptop name */
Corentin Chary600ad522009-11-30 21:42:42 +0100211
Corentin Chary7c247642009-11-30 22:13:54 +0100212 struct acpi_table_header *dsdt_info;
Corentin Chary600ad522009-11-30 21:42:42 +0100213 struct platform_device *platform_device;
Corentin Chary7c247642009-11-30 22:13:54 +0100214 struct acpi_device *device; /* the device we are in */
215 struct backlight_device *backlight_device;
Corentin Chary9129d142009-12-01 22:39:41 +0100216
Corentin Chary7c247642009-11-30 22:13:54 +0100217 struct input_dev *inputdev;
Corentin Chary9129d142009-12-01 22:39:41 +0100218 struct key_entry *keymap;
219
Corentin Charyaee0afb2010-01-26 21:01:34 +0100220 struct asus_led mled;
221 struct asus_led tled;
222 struct asus_led rled;
223 struct asus_led pled;
224 struct asus_led gled;
225 struct asus_led kled;
226 struct workqueue_struct *led_workqueue;
Corentin Chary7c247642009-11-30 22:13:54 +0100227
Corentin Charyaa9df932010-01-13 21:49:10 +0100228 int wireless_status;
229 bool have_rsts;
Corentin Chary3e68ae72010-01-13 22:10:39 +0100230 int lcd_state;
Corentin Charyaa9df932010-01-13 21:49:10 +0100231
Corentin Chary18e13112010-01-25 23:29:24 +0100232 struct rfkill *gps_rfkill;
233
Corentin Charybe966662009-08-29 10:28:29 +0200234 acpi_handle handle; /* the handle of the hotk device */
Corentin Charybe966662009-08-29 10:28:29 +0200235 u32 ledd_status; /* status of the LED display */
236 u8 light_level; /* light sensor level */
237 u8 light_switch; /* light sensor switch value */
238 u16 event_count[128]; /* count for each event TODO make this better */
Corentin Chary034ce902009-01-20 16:17:43 +0100239 u16 *keycode_map;
Corentin Chary85091b72007-01-26 14:04:30 +0100240};
241
Corentin Chary9129d142009-12-01 22:39:41 +0100242static const struct key_entry asus_keymap[] = {
Corentin Charya539df52010-01-25 22:53:21 +0100243 /* Lenovo SL Specific keycodes */
Corentin Chary66a71dd2010-01-25 22:50:11 +0100244 {KE_KEY, 0x02, { KEY_SCREENLOCK } },
245 {KE_KEY, 0x05, { KEY_WLAN } },
246 {KE_KEY, 0x08, { KEY_F13 } },
247 {KE_KEY, 0x17, { KEY_ZOOM } },
248 {KE_KEY, 0x1f, { KEY_BATTERY } },
Corentin Charya539df52010-01-25 22:53:21 +0100249 /* End of Lenovo SL Specific keycodes */
Corentin Chary66a71dd2010-01-25 22:50:11 +0100250 {KE_KEY, 0x30, { KEY_VOLUMEUP } },
251 {KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
252 {KE_KEY, 0x32, { KEY_MUTE } },
253 {KE_KEY, 0x33, { KEY_SWITCHVIDEOMODE } },
254 {KE_KEY, 0x34, { KEY_SWITCHVIDEOMODE } },
255 {KE_KEY, 0x40, { KEY_PREVIOUSSONG } },
256 {KE_KEY, 0x41, { KEY_NEXTSONG } },
257 {KE_KEY, 0x43, { KEY_STOPCD } },
258 {KE_KEY, 0x45, { KEY_PLAYPAUSE } },
259 {KE_KEY, 0x4c, { KEY_MEDIA } },
260 {KE_KEY, 0x50, { KEY_EMAIL } },
261 {KE_KEY, 0x51, { KEY_WWW } },
262 {KE_KEY, 0x55, { KEY_CALC } },
263 {KE_KEY, 0x5C, { KEY_SCREENLOCK } }, /* Screenlock */
264 {KE_KEY, 0x5D, { KEY_WLAN } },
265 {KE_KEY, 0x5E, { KEY_WLAN } },
266 {KE_KEY, 0x5F, { KEY_WLAN } },
267 {KE_KEY, 0x60, { KEY_SWITCHVIDEOMODE } },
268 {KE_KEY, 0x61, { KEY_SWITCHVIDEOMODE } },
269 {KE_KEY, 0x62, { KEY_SWITCHVIDEOMODE } },
270 {KE_KEY, 0x63, { KEY_SWITCHVIDEOMODE } },
271 {KE_KEY, 0x6B, { KEY_F13 } }, /* Lock Touchpad */
Corentin Chary7f607d72010-01-17 17:37:19 +0100272 {KE_KEY, 0x7E, { KEY_BLUETOOTH } },
273 {KE_KEY, 0x7D, { KEY_BLUETOOTH } },
Corentin Chary66a71dd2010-01-25 22:50:11 +0100274 {KE_KEY, 0x82, { KEY_CAMERA } },
275 {KE_KEY, 0x88, { KEY_WLAN } },
276 {KE_KEY, 0x8A, { KEY_PROG1 } },
277 {KE_KEY, 0x95, { KEY_MEDIA } },
278 {KE_KEY, 0x99, { KEY_PHONE } },
279 {KE_KEY, 0xc4, { KEY_KBDILLUMUP } },
280 {KE_KEY, 0xc5, { KEY_KBDILLUMDOWN } },
Corentin Chary034ce902009-01-20 16:17:43 +0100281 {KE_END, 0},
282};
283
Corentin Chary66a71dd2010-01-25 22:50:11 +0100284
Corentin Chary85091b72007-01-26 14:04:30 +0100285/*
286 * This function evaluates an ACPI method, given an int as parameter, the
287 * method is searched within the scope of the handle, can be NULL. The output
288 * of the method is written is output, which can also be NULL
289 *
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100290 * returns 0 if write is successful, -1 else.
Corentin Chary85091b72007-01-26 14:04:30 +0100291 */
Corentin Charyd8c67322009-11-28 10:27:51 +0100292static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
293 struct acpi_buffer *output)
Corentin Chary85091b72007-01-26 14:04:30 +0100294{
Corentin Charybe966662009-08-29 10:28:29 +0200295 struct acpi_object_list params; /* list of input parameters (an int) */
296 union acpi_object in_obj; /* the only param we use */
Corentin Chary85091b72007-01-26 14:04:30 +0100297 acpi_status status;
298
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100299 if (!handle)
300 return 0;
301
Corentin Chary85091b72007-01-26 14:04:30 +0100302 params.count = 1;
303 params.pointer = &in_obj;
304 in_obj.type = ACPI_TYPE_INTEGER;
305 in_obj.integer.value = val;
306
307 status = acpi_evaluate_object(handle, (char *)method, &params, output);
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100308 if (status == AE_OK)
309 return 0;
310 else
311 return -1;
Corentin Chary85091b72007-01-26 14:04:30 +0100312}
313
Corentin Charyd8c67322009-11-28 10:27:51 +0100314static int write_acpi_int(acpi_handle handle, const char *method, int val)
315{
316 return write_acpi_int_ret(handle, method, val, NULL);
317}
318
Corentin Charyd99b5772010-01-22 21:20:57 +0100319static int acpi_check_handle(acpi_handle handle, const char *method,
320 acpi_handle *ret)
321{
322 acpi_status status;
323
324 if (method == NULL)
325 return -ENODEV;
326
327 if (ret)
328 status = acpi_get_handle(handle, (char *)method,
329 ret);
330 else {
331 acpi_handle dummy;
332
333 status = acpi_get_handle(handle, (char *)method,
334 &dummy);
335 }
336
337 if (status != AE_OK) {
338 if (ret)
339 pr_warning("Error finding %s\n", method);
340 return -ENODEV;
341 }
342 return 0;
343}
344
Corentin Chary17e78f62010-01-13 22:21:33 +0100345/* Generic LED function */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100346static int asus_led_set(struct asus_laptop *asus, const char *method,
Corentin Chary17e78f62010-01-13 22:21:33 +0100347 int value)
Corentin Charybe18cda2007-01-26 14:04:35 +0100348{
Corentin Charyd99b5772010-01-22 21:20:57 +0100349 if (!strcmp(method, METHOD_MLED))
Corentin Chary17e78f62010-01-13 22:21:33 +0100350 value = !value;
Corentin Charyd99b5772010-01-22 21:20:57 +0100351 else if (!strcmp(method, METHOD_GLED))
Corentin Chary17e78f62010-01-13 22:21:33 +0100352 value = !value + 1;
353 else
354 value = !!value;
Corentin Charybe18cda2007-01-26 14:04:35 +0100355
Corentin Charye5593bf2010-01-17 17:20:11 +0100356 return write_acpi_int(asus->handle, method, value);
Corentin Charybe18cda2007-01-26 14:04:35 +0100357}
358
Corentin Charybe4ee822009-12-06 16:27:09 +0100359/*
360 * LEDs
361 */
Corentin Charybe18cda2007-01-26 14:04:35 +0100362/* /sys/class/led handlers */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100363static void asus_led_cdev_set(struct led_classdev *led_cdev,
364 enum led_brightness value)
365{
366 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
367 struct asus_laptop *asus = led->asus;
Corentin Charybe18cda2007-01-26 14:04:35 +0100368
Corentin Charyaee0afb2010-01-26 21:01:34 +0100369 led->wk = !!value;
370 queue_work(asus->led_workqueue, &led->work);
371}
372
373static void asus_led_cdev_update(struct work_struct *work)
374{
375 struct asus_led *led = container_of(work, struct asus_led, work);
376 struct asus_laptop *asus = led->asus;
377
378 asus_led_set(asus, led->method, led->wk);
379}
380
381static enum led_brightness asus_led_cdev_get(struct led_classdev *led_cdev)
382{
383 return led_cdev->brightness;
384}
Corentin Charybe18cda2007-01-26 14:04:35 +0100385
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000386/*
Corentin Charybe4ee822009-12-06 16:27:09 +0100387 * Keyboard backlight (also a LED)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000388 */
Corentin Charyd99b5772010-01-22 21:20:57 +0100389static int asus_kled_lvl(struct asus_laptop *asus)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000390{
391 unsigned long long kblv;
392 struct acpi_object_list params;
393 union acpi_object in_obj;
394 acpi_status rv;
395
396 params.count = 1;
397 params.pointer = &in_obj;
398 in_obj.type = ACPI_TYPE_INTEGER;
399 in_obj.integer.value = 2;
400
Corentin Charyd99b5772010-01-22 21:20:57 +0100401 rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
402 &params, &kblv);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000403 if (ACPI_FAILURE(rv)) {
404 pr_warning("Error reading kled level\n");
Corentin Chary4d441512010-01-13 22:26:24 +0100405 return -ENODEV;
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000406 }
407 return kblv;
408}
409
Corentin Chary4d441512010-01-13 22:26:24 +0100410static int asus_kled_set(struct asus_laptop *asus, int kblv)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000411{
412 if (kblv > 0)
413 kblv = (1 << 7) | (kblv & 0x7F);
414 else
415 kblv = 0;
416
Corentin Charyd99b5772010-01-22 21:20:57 +0100417 if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000418 pr_warning("Keyboard LED display write failed\n");
419 return -EINVAL;
420 }
421 return 0;
422}
423
Corentin Charyaee0afb2010-01-26 21:01:34 +0100424static void asus_kled_cdev_set(struct led_classdev *led_cdev,
425 enum led_brightness value)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000426{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100427 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
428 struct asus_laptop *asus = led->asus;
Corentin Chary9129d142009-12-01 22:39:41 +0100429
Corentin Chary060cbce2010-01-28 10:52:40 +0100430 led->wk = value;
Corentin Charyaee0afb2010-01-26 21:01:34 +0100431 queue_work(asus->led_workqueue, &led->work);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000432}
433
Corentin Charyaee0afb2010-01-26 21:01:34 +0100434static void asus_kled_cdev_update(struct work_struct *work)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000435{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100436 struct asus_led *led = container_of(work, struct asus_led, work);
437 struct asus_laptop *asus = led->asus;
Corentin Chary9129d142009-12-01 22:39:41 +0100438
Corentin Charyaee0afb2010-01-26 21:01:34 +0100439 asus_kled_set(asus, led->wk);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000440}
441
Corentin Charyaee0afb2010-01-26 21:01:34 +0100442static enum led_brightness asus_kled_cdev_get(struct led_classdev *led_cdev)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000443{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100444 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
445 struct asus_laptop *asus = led->asus;
Corentin Charyd99b5772010-01-22 21:20:57 +0100446
447 return asus_kled_lvl(asus);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000448}
449
Corentin Charybe4ee822009-12-06 16:27:09 +0100450static void asus_led_exit(struct asus_laptop *asus)
451{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100452 if (asus->mled.led.dev)
453 led_classdev_unregister(&asus->mled.led);
454 if (asus->tled.led.dev)
455 led_classdev_unregister(&asus->tled.led);
456 if (asus->pled.led.dev)
457 led_classdev_unregister(&asus->pled.led);
458 if (asus->rled.led.dev)
459 led_classdev_unregister(&asus->rled.led);
460 if (asus->gled.led.dev)
461 led_classdev_unregister(&asus->gled.led);
462 if (asus->kled.led.dev)
463 led_classdev_unregister(&asus->kled.led);
464 if (asus->led_workqueue) {
465 destroy_workqueue(asus->led_workqueue);
466 asus->led_workqueue = NULL;
Corentin Charybe4ee822009-12-06 16:27:09 +0100467 }
468}
469
470/* Ugly macro, need to fix that later */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100471static int asus_led_register(struct asus_laptop *asus,
472 struct asus_led *led,
473 const char *name, const char *method)
474{
475 struct led_classdev *led_cdev = &led->led;
476
477 if (!method || acpi_check_handle(asus->handle, method, NULL))
Corentin Chary060cbce2010-01-28 10:52:40 +0100478 return 0; /* Led not present */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100479
480 led->asus = asus;
481 led->method = method;
482
483 INIT_WORK(&led->work, asus_led_cdev_update);
484 led_cdev->name = name;
485 led_cdev->brightness_set = asus_led_cdev_set;
486 led_cdev->brightness_get = asus_led_cdev_get;
487 led_cdev->max_brightness = 1;
488 return led_classdev_register(&asus->platform_device->dev, led_cdev);
489}
Corentin Charybe4ee822009-12-06 16:27:09 +0100490
491static int asus_led_init(struct asus_laptop *asus)
492{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100493 int r;
Corentin Charybe4ee822009-12-06 16:27:09 +0100494
495 /*
496 * Functions that actually update the LED's are called from a
497 * workqueue. By doing this as separate work rather than when the LED
498 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
499 * potentially bad time, such as a timer interrupt.
500 */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100501 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
502 if (!asus->led_workqueue)
Corentin Charybe4ee822009-12-06 16:27:09 +0100503 return -ENOMEM;
504
Corentin Charyaee0afb2010-01-26 21:01:34 +0100505 r = asus_led_register(asus, &asus->mled, "asus::mail", METHOD_MLED);
506 if (r)
507 goto error;
508 r = asus_led_register(asus, &asus->tled, "asus::touchpad", METHOD_TLED);
509 if (r)
510 goto error;
511 r = asus_led_register(asus, &asus->rled, "asus::record", METHOD_RLED);
512 if (r)
513 goto error;
514 r = asus_led_register(asus, &asus->pled, "asus::phone", METHOD_PLED);
515 if (r)
516 goto error;
517 r = asus_led_register(asus, &asus->gled, "asus::gaming", METHOD_GLED);
518 if (r)
519 goto error;
Corentin Charyd99b5772010-01-22 21:20:57 +0100520 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
Corentin Charyaee0afb2010-01-26 21:01:34 +0100521 !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL)) {
522 struct asus_led *led = &asus->kled;
523 struct led_classdev *cdev = &led->led;
524
525 led->asus = asus;
526
527 INIT_WORK(&led->work, asus_kled_cdev_update);
528 cdev->name = "asus::kbd_backlight";
529 cdev->brightness_set = asus_kled_cdev_set;
530 cdev->brightness_get = asus_kled_cdev_get;
531 cdev->max_brightness = 3;
532 r = led_classdev_register(&asus->platform_device->dev, cdev);
533 }
Corentin Charybe4ee822009-12-06 16:27:09 +0100534error:
Corentin Charyaee0afb2010-01-26 21:01:34 +0100535 if (r)
Corentin Charybe4ee822009-12-06 16:27:09 +0100536 asus_led_exit(asus);
Corentin Charyaee0afb2010-01-26 21:01:34 +0100537 return r;
Corentin Charybe4ee822009-12-06 16:27:09 +0100538}
539
540/*
541 * Backlight device
542 */
Corentin Chary3e68ae72010-01-13 22:10:39 +0100543static int asus_lcd_status(struct asus_laptop *asus)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100544{
Corentin Chary3e68ae72010-01-13 22:10:39 +0100545 return asus->lcd_state;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100546}
547
Corentin Chary3e68ae72010-01-13 22:10:39 +0100548static int asus_lcd_set(struct asus_laptop *asus, int value)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100549{
550 int lcd = 0;
551 acpi_status status = 0;
552
Corentin Chary3e68ae72010-01-13 22:10:39 +0100553 lcd = !!value;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100554
Corentin Chary3e68ae72010-01-13 22:10:39 +0100555 if (lcd == asus_lcd_status(asus))
Corentin Chary6b7091e2007-01-26 14:04:45 +0100556 return 0;
557
Corentin Chary3e68ae72010-01-13 22:10:39 +0100558 if (!lcd_switch_handle)
559 return -ENODEV;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100560
Corentin Chary3e68ae72010-01-13 22:10:39 +0100561 status = acpi_evaluate_object(lcd_switch_handle,
562 NULL, NULL, NULL);
563
564 if (ACPI_FAILURE(status)) {
565 pr_warning("Error switching LCD\n");
566 return -ENODEV;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100567 }
568
Corentin Chary3e68ae72010-01-13 22:10:39 +0100569 asus->lcd_state = lcd;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100570 return 0;
571}
572
Corentin Chary9129d142009-12-01 22:39:41 +0100573static void lcd_blank(struct asus_laptop *asus, int blank)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100574{
Corentin Chary7c247642009-11-30 22:13:54 +0100575 struct backlight_device *bd = asus->backlight_device;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100576
Corentin Chary3e68ae72010-01-13 22:10:39 +0100577 asus->lcd_state = (blank == FB_BLANK_UNBLANK);
578
Len Brown8def05f2007-01-30 01:46:43 -0500579 if (bd) {
Richard Purdie599a52d2007-02-10 23:07:48 +0000580 bd->props.power = blank;
Richard Purdie28ee0862007-02-08 22:25:09 +0000581 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100582 }
583}
584
Corentin Chary4d441512010-01-13 22:26:24 +0100585static int asus_read_brightness(struct backlight_device *bd)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100586{
Corentin Charyd99b5772010-01-22 21:20:57 +0100587 struct asus_laptop *asus = bl_get_data(bd);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400588 unsigned long long value;
Corentin Chary9a816852007-03-11 10:25:38 +0100589 acpi_status rv = AE_OK;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100590
Corentin Charyd99b5772010-01-22 21:20:57 +0100591 rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
592 NULL, &value);
Corentin Chary9a816852007-03-11 10:25:38 +0100593 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200594 pr_warning("Error reading brightness\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100595
596 return value;
597}
598
Corentin Chary4d441512010-01-13 22:26:24 +0100599static int asus_set_brightness(struct backlight_device *bd, int value)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100600{
Corentin Charyd99b5772010-01-22 21:20:57 +0100601 struct asus_laptop *asus = bl_get_data(bd);
602
603 if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200604 pr_warning("Error changing brightness\n");
Corentin Charye5b50f62009-11-28 10:19:55 +0100605 return -EIO;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100606 }
Corentin Charye5b50f62009-11-28 10:19:55 +0100607 return 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100608}
609
610static int update_bl_status(struct backlight_device *bd)
611{
Corentin Chary9129d142009-12-01 22:39:41 +0100612 struct asus_laptop *asus = bl_get_data(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100613 int rv;
Richard Purdie599a52d2007-02-10 23:07:48 +0000614 int value = bd->props.brightness;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100615
Corentin Chary4d441512010-01-13 22:26:24 +0100616 rv = asus_set_brightness(bd, value);
Len Brown8def05f2007-01-30 01:46:43 -0500617 if (rv)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100618 return rv;
619
Richard Purdie599a52d2007-02-10 23:07:48 +0000620 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
Corentin Chary3e68ae72010-01-13 22:10:39 +0100621 return asus_lcd_set(asus, value);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100622}
623
Corentin Charybe4ee822009-12-06 16:27:09 +0100624static struct backlight_ops asusbl_ops = {
Corentin Chary4d441512010-01-13 22:26:24 +0100625 .get_brightness = asus_read_brightness,
Corentin Charybe4ee822009-12-06 16:27:09 +0100626 .update_status = update_bl_status,
627};
628
Corentin Charya539df52010-01-25 22:53:21 +0100629static int asus_backlight_notify(struct asus_laptop *asus)
630{
631 struct backlight_device *bd = asus->backlight_device;
632 int old = bd->props.brightness;
633
634 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
635
636 return old;
637}
638
Corentin Charybe4ee822009-12-06 16:27:09 +0100639static int asus_backlight_init(struct asus_laptop *asus)
640{
641 struct backlight_device *bd;
642 struct device *dev = &asus->platform_device->dev;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500643 struct backlight_properties props;
Corentin Charybe4ee822009-12-06 16:27:09 +0100644
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) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500648 memset(&props, 0, sizeof(struct backlight_properties));
649 props.max_brightness = 15;
650
Corentin Charybe4ee822009-12-06 16:27:09 +0100651 bd = backlight_device_register(ASUS_LAPTOP_FILE, dev,
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500652 asus, &asusbl_ops, &props);
Corentin Charybe4ee822009-12-06 16:27:09 +0100653 if (IS_ERR(bd)) {
654 pr_err("Could not register asus backlight device\n");
655 asus->backlight_device = NULL;
656 return PTR_ERR(bd);
657 }
658
659 asus->backlight_device = bd;
660
Corentin Charybe4ee822009-12-06 16:27:09 +0100661 bd->props.power = FB_BLANK_UNBLANK;
Corentin Charyd99b5772010-01-22 21:20:57 +0100662 bd->props.brightness = asus_read_brightness(bd);
Corentin Charybe4ee822009-12-06 16:27:09 +0100663 backlight_update_status(bd);
664 }
665 return 0;
666}
667
668static void asus_backlight_exit(struct asus_laptop *asus)
669{
670 if (asus->backlight_device)
671 backlight_device_unregister(asus->backlight_device);
Corentin Charya539df52010-01-25 22:53:21 +0100672 asus->backlight_device = NULL;
Corentin Charybe4ee822009-12-06 16:27:09 +0100673}
674
Corentin Chary85091b72007-01-26 14:04:30 +0100675/*
676 * Platform device handlers
677 */
678
679/*
680 * We write our info in page, we begin at offset off and cannot write more
681 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
682 * number of bytes written in page
683 */
684static ssize_t show_infos(struct device *dev,
Len Brown8def05f2007-01-30 01:46:43 -0500685 struct device_attribute *attr, char *page)
Corentin Chary85091b72007-01-26 14:04:30 +0100686{
Corentin Chary9129d142009-12-01 22:39:41 +0100687 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary85091b72007-01-26 14:04:30 +0100688 int len = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400689 unsigned long long temp;
Corentin Charybe966662009-08-29 10:28:29 +0200690 char buf[16]; /* enough for all info */
Corentin Chary9a816852007-03-11 10:25:38 +0100691 acpi_status rv = AE_OK;
692
Corentin Chary85091b72007-01-26 14:04:30 +0100693 /*
Corentin Chary060cbce2010-01-28 10:52:40 +0100694 * We use the easy way, we don't care of off and count,
695 * so we don't set eof to 1
Corentin Chary85091b72007-01-26 14:04:30 +0100696 */
697
Corentin Chary50a90c42009-11-30 21:55:12 +0100698 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
699 len += sprintf(page + len, "Model reference : %s\n", asus->name);
Corentin Chary85091b72007-01-26 14:04:30 +0100700 /*
701 * The SFUN method probably allows the original driver to get the list
702 * of features supported by a given model. For now, 0x0100 or 0x0800
703 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
704 * The significance of others is yet to be found.
705 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100706 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100707 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000708 len += sprintf(page + len, "SFUN value : %#x\n",
709 (uint) temp);
710 /*
711 * The HWRS method return informations about the hardware.
712 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
713 * The significance of others is yet to be found.
714 * If we don't find the method, we assume the device are present.
715 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100716 rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
Corentin Chary1d4a3802009-08-28 12:56:46 +0000717 if (!ACPI_FAILURE(rv))
718 len += sprintf(page + len, "HRWS value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100719 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100720 /*
721 * Another value for userspace: the ASYM method returns 0x02 for
722 * battery low and 0x04 for battery critical, its readings tend to be
723 * more accurate than those provided by _BST.
724 * Note: since not all the laptops provide this method, errors are
725 * silently ignored.
726 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100727 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100728 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000729 len += sprintf(page + len, "ASYM value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100730 (uint) temp);
Corentin Chary7c247642009-11-30 22:13:54 +0100731 if (asus->dsdt_info) {
732 snprintf(buf, 16, "%d", asus->dsdt_info->length);
Corentin Chary85091b72007-01-26 14:04:30 +0100733 len += sprintf(page + len, "DSDT length : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100734 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
Corentin Chary85091b72007-01-26 14:04:30 +0100735 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100736 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100737 len += sprintf(page + len, "DSDT revision : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100738 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100739 len += sprintf(page + len, "OEM id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100740 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100741 len += sprintf(page + len, "OEM table id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100742 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100743 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100744 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100745 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100746 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100747 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
748 }
749
750 return len;
751}
752
753static int parse_arg(const char *buf, unsigned long count, int *val)
754{
755 if (!count)
756 return 0;
757 if (count > 31)
758 return -EINVAL;
759 if (sscanf(buf, "%i", val) != 1)
760 return -EINVAL;
761 return count;
762}
763
Corentin Chary17e78f62010-01-13 22:21:33 +0100764static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
765 const char *buf, size_t count,
Corentin Charyd99b5772010-01-22 21:20:57 +0100766 const char *method)
Corentin Chary4564de12007-01-26 14:04:40 +0100767{
768 int rv, value;
769 int out = 0;
770
771 rv = parse_arg(buf, count, &value);
772 if (rv > 0)
773 out = value ? 1 : 0;
774
Corentin Charyd99b5772010-01-22 21:20:57 +0100775 if (write_acpi_int(asus->handle, method, value))
Corentin Chary17e78f62010-01-13 22:21:33 +0100776 return -ENODEV;
Corentin Chary4564de12007-01-26 14:04:40 +0100777 return rv;
778}
779
780/*
Corentin Chary722ad972007-01-26 14:04:55 +0100781 * LEDD display
782 */
783static ssize_t show_ledd(struct device *dev,
784 struct device_attribute *attr, char *buf)
785{
Corentin Chary9129d142009-12-01 22:39:41 +0100786 struct asus_laptop *asus = dev_get_drvdata(dev);
787
Corentin Chary50a90c42009-11-30 21:55:12 +0100788 return sprintf(buf, "0x%08x\n", asus->ledd_status);
Corentin Chary722ad972007-01-26 14:04:55 +0100789}
790
791static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
792 const char *buf, size_t count)
793{
Corentin Chary9129d142009-12-01 22:39:41 +0100794 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary722ad972007-01-26 14:04:55 +0100795 int rv, value;
796
797 rv = parse_arg(buf, count, &value);
798 if (rv > 0) {
Corentin Charyd99b5772010-01-22 21:20:57 +0100799 if (write_acpi_int(asus->handle, METHOD_LEDD, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200800 pr_warning("LED display write failed\n");
Corentin Chary722ad972007-01-26 14:04:55 +0100801 else
Corentin Chary50a90c42009-11-30 21:55:12 +0100802 asus->ledd_status = (u32) value;
Corentin Chary722ad972007-01-26 14:04:55 +0100803 }
804 return rv;
805}
806
807/*
Corentin Charyaa9df932010-01-13 21:49:10 +0100808 * Wireless
809 */
810static int asus_wireless_status(struct asus_laptop *asus, int mask)
811{
812 unsigned long long status;
813 acpi_status rv = AE_OK;
814
815 if (!asus->have_rsts)
816 return (asus->wireless_status & mask) ? 1 : 0;
817
Corentin Charyd99b5772010-01-22 21:20:57 +0100818 rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
819 NULL, &status);
Corentin Charyaa9df932010-01-13 21:49:10 +0100820 if (ACPI_FAILURE(rv)) {
821 pr_warning("Error reading Wireless status\n");
822 return -EINVAL;
823 }
824 return !!(status & mask);
825}
826
827/*
Corentin Chary4564de12007-01-26 14:04:40 +0100828 * WLAN
829 */
Corentin Charye5593bf2010-01-17 17:20:11 +0100830static int asus_wlan_set(struct asus_laptop *asus, int status)
831{
832 if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
833 pr_warning("Error setting wlan status to %d", status);
834 return -EIO;
835 }
836 return 0;
837}
838
Corentin Chary4564de12007-01-26 14:04:40 +0100839static ssize_t show_wlan(struct device *dev,
840 struct device_attribute *attr, char *buf)
841{
Corentin Chary9129d142009-12-01 22:39:41 +0100842 struct asus_laptop *asus = dev_get_drvdata(dev);
843
Corentin Chary17e78f62010-01-13 22:21:33 +0100844 return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100845}
846
847static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
848 const char *buf, size_t count)
849{
Corentin Chary9129d142009-12-01 22:39:41 +0100850 struct asus_laptop *asus = dev_get_drvdata(dev);
851
Corentin Charyd99b5772010-01-22 21:20:57 +0100852 return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
Corentin Chary4564de12007-01-26 14:04:40 +0100853}
854
855/*
856 * Bluetooth
857 */
Corentin Charye5593bf2010-01-17 17:20:11 +0100858static int asus_bluetooth_set(struct asus_laptop *asus, int status)
859{
860 if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
861 pr_warning("Error setting bluetooth status to %d", status);
862 return -EIO;
863 }
864 return 0;
865}
866
Corentin Chary4564de12007-01-26 14:04:40 +0100867static ssize_t show_bluetooth(struct device *dev,
868 struct device_attribute *attr, char *buf)
869{
Corentin Chary9129d142009-12-01 22:39:41 +0100870 struct asus_laptop *asus = dev_get_drvdata(dev);
871
Corentin Chary17e78f62010-01-13 22:21:33 +0100872 return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100873}
874
Len Brown8def05f2007-01-30 01:46:43 -0500875static ssize_t store_bluetooth(struct device *dev,
876 struct device_attribute *attr, const char *buf,
877 size_t count)
Corentin Chary4564de12007-01-26 14:04:40 +0100878{
Corentin Chary9129d142009-12-01 22:39:41 +0100879 struct asus_laptop *asus = dev_get_drvdata(dev);
880
Corentin Charyd99b5772010-01-22 21:20:57 +0100881 return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
Corentin Chary4564de12007-01-26 14:04:40 +0100882}
883
Corentin Chary78127b42007-01-26 14:04:49 +0100884/*
885 * Display
886 */
Corentin Chary4d441512010-01-13 22:26:24 +0100887static void asus_set_display(struct asus_laptop *asus, int value)
Corentin Chary78127b42007-01-26 14:04:49 +0100888{
889 /* no sanity check needed for now */
Corentin Charyd99b5772010-01-22 21:20:57 +0100890 if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200891 pr_warning("Error setting display\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100892 return;
893}
894
Corentin Chary9129d142009-12-01 22:39:41 +0100895static int read_display(struct asus_laptop *asus)
Corentin Chary78127b42007-01-26 14:04:49 +0100896{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400897 unsigned long long value = 0;
Corentin Chary9a816852007-03-11 10:25:38 +0100898 acpi_status rv = AE_OK;
Corentin Chary78127b42007-01-26 14:04:49 +0100899
Corentin Charybe966662009-08-29 10:28:29 +0200900 /*
901 * In most of the case, we know how to set the display, but sometime
902 * we can't read it
903 */
Len Brown8def05f2007-01-30 01:46:43 -0500904 if (display_get_handle) {
Corentin Chary9a816852007-03-11 10:25:38 +0100905 rv = acpi_evaluate_integer(display_get_handle, NULL,
906 NULL, &value);
907 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200908 pr_warning("Error reading display status\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100909 }
910
Corentin Chary060cbce2010-01-28 10:52:40 +0100911 value &= 0x0F; /* needed for some models, shouldn't hurt others */
Corentin Chary78127b42007-01-26 14:04:49 +0100912
913 return value;
914}
Len Brown8def05f2007-01-30 01:46:43 -0500915
Corentin Chary78127b42007-01-26 14:04:49 +0100916/*
917 * Now, *this* one could be more user-friendly, but so far, no-one has
918 * complained. The significance of bits is the same as in store_disp()
919 */
920static ssize_t show_disp(struct device *dev,
921 struct device_attribute *attr, char *buf)
922{
Corentin Chary9129d142009-12-01 22:39:41 +0100923 struct asus_laptop *asus = dev_get_drvdata(dev);
924
Corentin Chary2a1fd642010-01-26 21:02:23 +0100925 if (!display_get_handle)
926 return -ENODEV;
Corentin Chary9129d142009-12-01 22:39:41 +0100927 return sprintf(buf, "%d\n", read_display(asus));
Corentin Chary78127b42007-01-26 14:04:49 +0100928}
929
930/*
931 * Experimental support for display switching. As of now: 1 should activate
932 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
933 * Any combination (bitwise) of these will suffice. I never actually tested 4
934 * displays hooked up simultaneously, so be warned. See the acpi4asus README
935 * for more info.
936 */
937static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
938 const char *buf, size_t count)
939{
Corentin Chary9129d142009-12-01 22:39:41 +0100940 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary78127b42007-01-26 14:04:49 +0100941 int rv, value;
942
943 rv = parse_arg(buf, count, &value);
944 if (rv > 0)
Corentin Chary4d441512010-01-13 22:26:24 +0100945 asus_set_display(asus, value);
Corentin Chary78127b42007-01-26 14:04:49 +0100946 return rv;
947}
948
Corentin Chary8b857352007-01-26 14:04:58 +0100949/*
950 * Light Sens
951 */
Corentin Chary4d441512010-01-13 22:26:24 +0100952static void asus_als_switch(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100953{
Corentin Charyd99b5772010-01-22 21:20:57 +0100954 if (write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200955 pr_warning("Error setting light sensor switch\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100956 asus->light_switch = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100957}
958
959static ssize_t show_lssw(struct device *dev,
960 struct device_attribute *attr, char *buf)
961{
Corentin Chary9129d142009-12-01 22:39:41 +0100962 struct asus_laptop *asus = dev_get_drvdata(dev);
963
Corentin Chary50a90c42009-11-30 21:55:12 +0100964 return sprintf(buf, "%d\n", asus->light_switch);
Corentin Chary8b857352007-01-26 14:04:58 +0100965}
966
967static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
968 const char *buf, size_t count)
969{
Corentin Chary9129d142009-12-01 22:39:41 +0100970 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100971 int rv, value;
972
973 rv = parse_arg(buf, count, &value);
974 if (rv > 0)
Corentin Chary4d441512010-01-13 22:26:24 +0100975 asus_als_switch(asus, value ? 1 : 0);
Corentin Chary8b857352007-01-26 14:04:58 +0100976
977 return rv;
978}
979
Corentin Chary4d441512010-01-13 22:26:24 +0100980static void asus_als_level(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100981{
Corentin Charyd99b5772010-01-22 21:20:57 +0100982 if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200983 pr_warning("Error setting light sensor level\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100984 asus->light_level = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100985}
986
987static ssize_t show_lslvl(struct device *dev,
988 struct device_attribute *attr, char *buf)
989{
Corentin Chary9129d142009-12-01 22:39:41 +0100990 struct asus_laptop *asus = dev_get_drvdata(dev);
991
Corentin Chary50a90c42009-11-30 21:55:12 +0100992 return sprintf(buf, "%d\n", asus->light_level);
Corentin Chary8b857352007-01-26 14:04:58 +0100993}
994
995static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
996 const char *buf, size_t count)
997{
Corentin Chary9129d142009-12-01 22:39:41 +0100998 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100999 int rv, value;
1000
1001 rv = parse_arg(buf, count, &value);
1002 if (rv > 0) {
1003 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
1004 /* 0 <= value <= 15 */
Corentin Chary4d441512010-01-13 22:26:24 +01001005 asus_als_level(asus, value);
Corentin Chary8b857352007-01-26 14:04:58 +01001006 }
1007
1008 return rv;
1009}
1010
Corentin Charye539c2f2007-05-06 14:47:06 +02001011/*
1012 * GPS
1013 */
Corentin Chary6358bf22010-01-13 21:55:44 +01001014static int asus_gps_status(struct asus_laptop *asus)
1015{
1016 unsigned long long status;
1017 acpi_status rv = AE_OK;
1018
Corentin Charyd99b5772010-01-22 21:20:57 +01001019 rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
1020 NULL, &status);
Corentin Chary6358bf22010-01-13 21:55:44 +01001021 if (ACPI_FAILURE(rv)) {
1022 pr_warning("Error reading GPS status\n");
1023 return -ENODEV;
1024 }
1025 return !!status;
1026}
1027
1028static int asus_gps_switch(struct asus_laptop *asus, int status)
1029{
Corentin Charyd99b5772010-01-22 21:20:57 +01001030 const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
Corentin Chary6358bf22010-01-13 21:55:44 +01001031
Corentin Charyd99b5772010-01-22 21:20:57 +01001032 if (write_acpi_int(asus->handle, meth, 0x02))
Corentin Chary6358bf22010-01-13 21:55:44 +01001033 return -ENODEV;
1034 return 0;
1035}
1036
Corentin Charye539c2f2007-05-06 14:47:06 +02001037static ssize_t show_gps(struct device *dev,
1038 struct device_attribute *attr, char *buf)
1039{
Corentin Chary9129d142009-12-01 22:39:41 +01001040 struct asus_laptop *asus = dev_get_drvdata(dev);
1041
Corentin Chary6358bf22010-01-13 21:55:44 +01001042 return sprintf(buf, "%d\n", asus_gps_status(asus));
Corentin Charye539c2f2007-05-06 14:47:06 +02001043}
1044
1045static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
1046 const char *buf, size_t count)
1047{
Corentin Chary060cbce2010-01-28 10:52:40 +01001048 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary6358bf22010-01-13 21:55:44 +01001049 int rv, value;
1050 int ret;
Corentin Chary9129d142009-12-01 22:39:41 +01001051
Corentin Chary6358bf22010-01-13 21:55:44 +01001052 rv = parse_arg(buf, count, &value);
1053 if (rv <= 0)
1054 return -EINVAL;
1055 ret = asus_gps_switch(asus, !!value);
1056 if (ret)
1057 return ret;
Corentin Chary18e13112010-01-25 23:29:24 +01001058 rfkill_set_sw_state(asus->gps_rfkill, !value);
Corentin Chary6358bf22010-01-13 21:55:44 +01001059 return rv;
Corentin Charye539c2f2007-05-06 14:47:06 +02001060}
1061
Corentin Chary034ce902009-01-20 16:17:43 +01001062/*
Corentin Chary18e13112010-01-25 23:29:24 +01001063 * rfkill
1064 */
1065static int asus_gps_rfkill_set(void *data, bool blocked)
1066{
1067 acpi_handle handle = data;
1068
1069 return asus_gps_switch(handle, !blocked);
1070}
1071
1072static const struct rfkill_ops asus_gps_rfkill_ops = {
1073 .set_block = asus_gps_rfkill_set,
1074};
1075
1076static void asus_rfkill_exit(struct asus_laptop *asus)
1077{
1078 if (asus->gps_rfkill) {
1079 rfkill_unregister(asus->gps_rfkill);
1080 rfkill_destroy(asus->gps_rfkill);
1081 asus->gps_rfkill = NULL;
1082 }
1083}
1084
1085static int asus_rfkill_init(struct asus_laptop *asus)
1086{
1087 int result;
1088
1089 if (acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) ||
1090 acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) ||
1091 acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
1092 return 0;
1093
1094 asus->gps_rfkill = rfkill_alloc("asus-gps", &asus->platform_device->dev,
1095 RFKILL_TYPE_GPS,
1096 &asus_gps_rfkill_ops, NULL);
1097 if (!asus->gps_rfkill)
1098 return -EINVAL;
1099
1100 result = rfkill_register(asus->gps_rfkill);
1101 if (result) {
1102 rfkill_destroy(asus->gps_rfkill);
1103 asus->gps_rfkill = NULL;
1104 }
1105
1106 return result;
1107}
1108
1109/*
Corentin Charybe4ee822009-12-06 16:27:09 +01001110 * Input device (i.e. hotkeys)
Corentin Chary034ce902009-01-20 16:17:43 +01001111 */
Corentin Charybe4ee822009-12-06 16:27:09 +01001112static void asus_input_notify(struct asus_laptop *asus, int event)
1113{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001114 if (asus->inputdev)
1115 sparse_keymap_report_event(asus->inputdev, event, 1, true);
Corentin Charybe4ee822009-12-06 16:27:09 +01001116}
1117
1118static int asus_input_init(struct asus_laptop *asus)
1119{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001120 struct input_dev *input;
1121 int error;
Corentin Charybe4ee822009-12-06 16:27:09 +01001122
Corentin Chary66a71dd2010-01-25 22:50:11 +01001123 input = input_allocate_device();
1124 if (!input) {
Corentin Charybe4ee822009-12-06 16:27:09 +01001125 pr_info("Unable to allocate input device\n");
1126 return 0;
1127 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001128 input->name = "Asus Laptop extra buttons";
1129 input->phys = ASUS_LAPTOP_FILE "/input0";
1130 input->id.bustype = BUS_HOST;
1131 input->dev.parent = &asus->platform_device->dev;
1132 input_set_drvdata(input, asus);
Corentin Charybe4ee822009-12-06 16:27:09 +01001133
Corentin Chary66a71dd2010-01-25 22:50:11 +01001134 error = sparse_keymap_setup(input, asus_keymap, NULL);
1135 if (error) {
1136 pr_err("Unable to setup input device keymap\n");
1137 goto err_keymap;
Corentin Charybe4ee822009-12-06 16:27:09 +01001138 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001139 error = input_register_device(input);
1140 if (error) {
Corentin Charybe4ee822009-12-06 16:27:09 +01001141 pr_info("Unable to register input device\n");
Corentin Chary66a71dd2010-01-25 22:50:11 +01001142 goto err_device;
Corentin Charybe4ee822009-12-06 16:27:09 +01001143 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001144
1145 asus->inputdev = input;
1146 return 0;
1147
1148err_keymap:
1149 sparse_keymap_free(input);
1150err_device:
1151 input_free_device(input);
1152 return error;
Corentin Charybe4ee822009-12-06 16:27:09 +01001153}
1154
1155static void asus_input_exit(struct asus_laptop *asus)
1156{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001157 if (asus->inputdev) {
1158 sparse_keymap_free(asus->inputdev);
Corentin Charybe4ee822009-12-06 16:27:09 +01001159 input_unregister_device(asus->inputdev);
Corentin Chary66a71dd2010-01-25 22:50:11 +01001160 }
Corentin Charybe4ee822009-12-06 16:27:09 +01001161}
1162
1163/*
1164 * ACPI driver
1165 */
Corentin Chary600ad522009-11-30 21:42:42 +01001166static void asus_acpi_notify(struct acpi_device *device, u32 event)
Corentin Chary85091b72007-01-26 14:04:30 +01001167{
Corentin Chary9129d142009-12-01 22:39:41 +01001168 struct asus_laptop *asus = acpi_driver_data(device);
Corentin Chary6050c8d2009-02-15 19:30:19 +01001169 u16 count;
Corentin Chary034ce902009-01-20 16:17:43 +01001170
Corentin Chary6b7091e2007-01-26 14:04:45 +01001171 /*
1172 * We need to tell the backlight device when the backlight power is
1173 * switched
1174 */
Corentin Chary3e68ae72010-01-13 22:10:39 +01001175 if (event == ATKD_LCD_ON)
Corentin Chary9129d142009-12-01 22:39:41 +01001176 lcd_blank(asus, FB_BLANK_UNBLANK);
Corentin Chary3e68ae72010-01-13 22:10:39 +01001177 else if (event == ATKD_LCD_OFF)
Corentin Chary9129d142009-12-01 22:39:41 +01001178 lcd_blank(asus, FB_BLANK_POWERDOWN);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001179
Corentin Chary619d8b12009-11-28 10:35:37 +01001180 /* TODO Find a better way to handle events count. */
Corentin Chary50a90c42009-11-30 21:55:12 +01001181 count = asus->event_count[event % 128]++;
1182 acpi_bus_generate_proc_event(asus->device, event, count);
1183 acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
1184 dev_name(&asus->device->dev), event,
Corentin Chary6050c8d2009-02-15 19:30:19 +01001185 count);
Corentin Chary85091b72007-01-26 14:04:30 +01001186
Corentin Charya539df52010-01-25 22:53:21 +01001187 /* Brightness events are special */
1188 if (event >= ATKD_BR_MIN && event <= ATKD_BR_MAX) {
1189
1190 /* Ignore them completely if the acpi video driver is used */
1191 if (asus->backlight_device != NULL) {
1192 /* Update the backlight device. */
1193 asus_backlight_notify(asus);
1194 }
1195 return ;
1196 }
Corentin Charybe4ee822009-12-06 16:27:09 +01001197 asus_input_notify(asus, event);
Corentin Chary85091b72007-01-26 14:04:30 +01001198}
1199
Corentin Chary2a1fd642010-01-26 21:02:23 +01001200static DEVICE_ATTR(infos, S_IRUGO, show_infos, NULL);
1201static DEVICE_ATTR(wlan, S_IRUGO | S_IWUSR, show_wlan, store_wlan);
1202static DEVICE_ATTR(bluetooth, S_IRUGO | S_IWUSR, show_bluetooth,
1203 store_bluetooth);
1204static DEVICE_ATTR(display, S_IRUGO | S_IWUSR, show_disp, store_disp);
1205static DEVICE_ATTR(ledd, S_IRUGO | S_IWUSR, show_ledd, store_ledd);
1206static DEVICE_ATTR(ls_level, S_IRUGO | S_IWUSR, show_lslvl, store_lslvl);
1207static DEVICE_ATTR(ls_switch, S_IRUGO | S_IWUSR, show_lssw, store_lssw);
1208static DEVICE_ATTR(gps, S_IRUGO | S_IWUSR, show_gps, store_gps);
1209
1210static void asus_sysfs_exit(struct asus_laptop *asus)
1211{
1212 struct platform_device *device = asus->platform_device;
1213
1214 device_remove_file(&device->dev, &dev_attr_infos);
1215 device_remove_file(&device->dev, &dev_attr_wlan);
1216 device_remove_file(&device->dev, &dev_attr_bluetooth);
1217 device_remove_file(&device->dev, &dev_attr_display);
1218 device_remove_file(&device->dev, &dev_attr_ledd);
1219 device_remove_file(&device->dev, &dev_attr_ls_switch);
1220 device_remove_file(&device->dev, &dev_attr_ls_level);
1221 device_remove_file(&device->dev, &dev_attr_gps);
1222}
1223
1224static int asus_sysfs_init(struct asus_laptop *asus)
1225{
1226 struct platform_device *device = asus->platform_device;
1227 int err;
1228
1229 err = device_create_file(&device->dev, &dev_attr_infos);
1230 if (err)
1231 return err;
1232
1233 if (!acpi_check_handle(asus->handle, METHOD_WLAN, NULL)) {
1234 err = device_create_file(&device->dev, &dev_attr_wlan);
1235 if (err)
1236 return err;
Corentin Chary85091b72007-01-26 14:04:30 +01001237 }
1238
Corentin Chary2a1fd642010-01-26 21:02:23 +01001239 if (!acpi_check_handle(asus->handle, METHOD_BLUETOOTH, NULL)) {
1240 err = device_create_file(&device->dev, &dev_attr_bluetooth);
1241 if (err)
1242 return err;
1243 }
Corentin Chary85091b72007-01-26 14:04:30 +01001244
Corentin Chary2a1fd642010-01-26 21:02:23 +01001245 if (!acpi_check_handle(asus->handle, METHOD_SWITCH_DISPLAY, NULL)) {
1246 err = device_create_file(&device->dev, &dev_attr_display);
1247 if (err)
1248 return err;
1249 }
Corentin Chary85091b72007-01-26 14:04:30 +01001250
Corentin Chary2a1fd642010-01-26 21:02:23 +01001251 if (!acpi_check_handle(asus->handle, METHOD_LEDD, NULL)) {
1252 err = device_create_file(&device->dev, &dev_attr_ledd);
1253 if (err)
1254 return err;
1255 }
Corentin Chary85091b72007-01-26 14:04:30 +01001256
Corentin Chary2a1fd642010-01-26 21:02:23 +01001257 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1258 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
1259 err = device_create_file(&device->dev, &dev_attr_ls_switch);
1260 if (err)
1261 return err;
1262 err = device_create_file(&device->dev, &dev_attr_ls_level);
1263 if (err)
1264 return err;
1265 }
1266
1267 if (!acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) &&
1268 !acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) &&
1269 !acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL)) {
1270 err = device_create_file(&device->dev, &dev_attr_gps);
1271 if (err)
1272 return err;
1273 }
1274
1275 return err;
1276}
Corentin Chary85091b72007-01-26 14:04:30 +01001277
Corentin Chary9129d142009-12-01 22:39:41 +01001278static int asus_platform_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001279{
Corentin Chary2a1fd642010-01-26 21:02:23 +01001280 int err;
Corentin Chary600ad522009-11-30 21:42:42 +01001281
Corentin Chary50a90c42009-11-30 21:55:12 +01001282 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1283 if (!asus->platform_device)
Corentin Chary600ad522009-11-30 21:42:42 +01001284 return -ENOMEM;
Corentin Chary9129d142009-12-01 22:39:41 +01001285 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001286
Corentin Chary2a1fd642010-01-26 21:02:23 +01001287 err = platform_device_add(asus->platform_device);
1288 if (err)
Corentin Chary600ad522009-11-30 21:42:42 +01001289 goto fail_platform_device;
1290
Corentin Chary2a1fd642010-01-26 21:02:23 +01001291 err = asus_sysfs_init(asus);
1292 if (err)
Corentin Chary600ad522009-11-30 21:42:42 +01001293 goto fail_sysfs;
1294 return 0;
1295
1296fail_sysfs:
Corentin Chary2a1fd642010-01-26 21:02:23 +01001297 asus_sysfs_exit(asus);
Corentin Chary50a90c42009-11-30 21:55:12 +01001298 platform_device_del(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001299fail_platform_device:
Corentin Chary50a90c42009-11-30 21:55:12 +01001300 platform_device_put(asus->platform_device);
Corentin Chary2a1fd642010-01-26 21:02:23 +01001301 return err;
Corentin Chary600ad522009-11-30 21:42:42 +01001302}
1303
Corentin Chary9129d142009-12-01 22:39:41 +01001304static void asus_platform_exit(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001305{
Corentin Chary2a1fd642010-01-26 21:02:23 +01001306 asus_sysfs_exit(asus);
Corentin Chary50a90c42009-11-30 21:55:12 +01001307 platform_device_unregister(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001308}
1309
1310static struct platform_driver platform_driver = {
Len Brown8def05f2007-01-30 01:46:43 -05001311 .driver = {
Corentin Chary9129d142009-12-01 22:39:41 +01001312 .name = ASUS_LAPTOP_FILE,
1313 .owner = THIS_MODULE,
1314 }
Corentin Chary85091b72007-01-26 14:04:30 +01001315};
1316
Len Brown8def05f2007-01-30 01:46:43 -05001317static int asus_handle_init(char *name, acpi_handle * handle,
Corentin Chary85091b72007-01-26 14:04:30 +01001318 char **paths, int num_paths)
1319{
1320 int i;
1321 acpi_status status;
1322
1323 for (i = 0; i < num_paths; i++) {
1324 status = acpi_get_handle(NULL, paths[i], handle);
1325 if (ACPI_SUCCESS(status))
1326 return 0;
1327 }
1328
1329 *handle = NULL;
1330 return -ENODEV;
1331}
1332
1333#define ASUS_HANDLE_INIT(object) \
1334 asus_handle_init(#object, &object##_handle, object##_paths, \
1335 ARRAY_SIZE(object##_paths))
1336
Corentin Chary85091b72007-01-26 14:04:30 +01001337/*
Corentin Chary50a90c42009-11-30 21:55:12 +01001338 * This function is used to initialize the context with right values. In this
1339 * method, we can make all the detection we want, and modify the asus_laptop
1340 * struct
Corentin Chary85091b72007-01-26 14:04:30 +01001341 */
Corentin Chary7c247642009-11-30 22:13:54 +01001342static int asus_laptop_get_info(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001343{
1344 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Corentin Chary85091b72007-01-26 14:04:30 +01001345 union acpi_object *model = NULL;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001346 unsigned long long bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +01001347 char *string = NULL;
1348 acpi_status status;
1349
1350 /*
1351 * Get DSDT headers early enough to allow for differentiating between
1352 * models, but late enough to allow acpi_bus_register_driver() to fail
1353 * before doing anything ACPI-specific. Should we encounter a machine,
1354 * which needs special handling (i.e. its hotkey device has a different
Corentin Chary7c247642009-11-30 22:13:54 +01001355 * HID), this bit will be moved.
Corentin Chary85091b72007-01-26 14:04:30 +01001356 */
Corentin Chary7c247642009-11-30 22:13:54 +01001357 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
Corentin Chary85091b72007-01-26 14:04:30 +01001358 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001359 pr_warning("Couldn't get the DSDT table header\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001360
1361 /* We have to write 0 on init this far for all ASUS models */
Corentin Chary50a90c42009-11-30 21:55:12 +01001362 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001363 pr_err("Hotkey initialization failed\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001364 return -ENODEV;
1365 }
1366
1367 /* This needs to be called for some laptops to init properly */
Corentin Chary9a816852007-03-11 10:25:38 +01001368 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001369 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
Corentin Chary9a816852007-03-11 10:25:38 +01001370 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001371 pr_warning("Error calling BSTS\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001372 else if (bsts_result)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001373 pr_notice("BSTS called, 0x%02x returned\n",
Corentin Chary9a816852007-03-11 10:25:38 +01001374 (uint) bsts_result);
Corentin Chary85091b72007-01-26 14:04:30 +01001375
Corentin Chary185e5af2007-03-11 10:27:33 +01001376 /* This too ... */
Corentin Charye5593bf2010-01-17 17:20:11 +01001377 if (write_acpi_int(asus->handle, "CWAP", wapf))
1378 pr_err("Error calling CWAP(%d)\n", wapf);
Corentin Chary85091b72007-01-26 14:04:30 +01001379 /*
1380 * Try to match the object returned by INIT to the specific model.
1381 * Handle every possible object (or the lack of thereof) the DSDT
1382 * writers might throw at us. When in trouble, we pass NULL to
1383 * asus_model_match() and try something completely different.
1384 */
1385 if (buffer.pointer) {
1386 model = buffer.pointer;
1387 switch (model->type) {
1388 case ACPI_TYPE_STRING:
1389 string = model->string.pointer;
1390 break;
1391 case ACPI_TYPE_BUFFER:
1392 string = model->buffer.pointer;
1393 break;
1394 default:
1395 string = "";
1396 break;
1397 }
1398 }
Corentin Chary50a90c42009-11-30 21:55:12 +01001399 asus->name = kstrdup(string, GFP_KERNEL);
Axel Lind8eca112010-06-29 11:09:47 +08001400 if (!asus->name) {
1401 kfree(buffer.pointer);
Corentin Chary85091b72007-01-26 14:04:30 +01001402 return -ENOMEM;
Axel Lind8eca112010-06-29 11:09:47 +08001403 }
Corentin Chary85091b72007-01-26 14:04:30 +01001404
Len Brown8def05f2007-01-30 01:46:43 -05001405 if (*string)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001406 pr_notice(" %s model detected\n", string);
Corentin Chary85091b72007-01-26 14:04:30 +01001407
Corentin Chary4564de12007-01-26 14:04:40 +01001408 /*
1409 * The HWRS method return informations about the hardware.
1410 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1411 * The significance of others is yet to be found.
Corentin Chary4564de12007-01-26 14:04:40 +01001412 */
Corentin Chary9a816852007-03-11 10:25:38 +01001413 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001414 acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
Corentin Charyd99b5772010-01-22 21:20:57 +01001415 if (!ACPI_FAILURE(status))
1416 pr_notice(" HRWS returned %x", (int)hwrs_result);
Corentin Chary4564de12007-01-26 14:04:40 +01001417
Corentin Charyd99b5772010-01-22 21:20:57 +01001418 if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
Corentin Charyaa9df932010-01-13 21:49:10 +01001419 asus->have_rsts = true;
Corentin Chary4564de12007-01-26 14:04:40 +01001420
Corentin Charyd99b5772010-01-22 21:20:57 +01001421 /* Scheduled for removal */
Corentin Chary6b7091e2007-01-26 14:04:45 +01001422 ASUS_HANDLE_INIT(lcd_switch);
Corentin Chary78127b42007-01-26 14:04:49 +01001423 ASUS_HANDLE_INIT(display_get);
1424
Corentin Chary85091b72007-01-26 14:04:30 +01001425 kfree(model);
1426
1427 return AE_OK;
1428}
1429
Corentin Chary600ad522009-11-30 21:42:42 +01001430static bool asus_device_present;
1431
Corentin Chary9129d142009-12-01 22:39:41 +01001432static int __devinit asus_acpi_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001433{
1434 int result = 0;
1435
Corentin Chary50a90c42009-11-30 21:55:12 +01001436 result = acpi_bus_get_status(asus->device);
Corentin Chary600ad522009-11-30 21:42:42 +01001437 if (result)
1438 return result;
Corentin Chary50a90c42009-11-30 21:55:12 +01001439 if (!asus->device->status.present) {
Corentin Chary600ad522009-11-30 21:42:42 +01001440 pr_err("Hotkey device not present, aborting\n");
1441 return -ENODEV;
1442 }
1443
Corentin Chary7c247642009-11-30 22:13:54 +01001444 result = asus_laptop_get_info(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001445 if (result)
1446 return result;
1447
Corentin Chary600ad522009-11-30 21:42:42 +01001448 /* WLED and BLED are on by default */
Corentin Charybe4ee822009-12-06 16:27:09 +01001449 if (bluetooth_status >= 0)
Corentin Charye5593bf2010-01-17 17:20:11 +01001450 asus_bluetooth_set(asus, !!bluetooth_status);
1451
Corentin Charyd0930a22010-01-17 17:21:13 +01001452 if (wlan_status >= 0)
1453 asus_wlan_set(asus, !!wlan_status);
Corentin Chary600ad522009-11-30 21:42:42 +01001454
1455 /* Keyboard Backlight is on by default */
Corentin Charyd99b5772010-01-22 21:20:57 +01001456 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
Corentin Chary4d441512010-01-13 22:26:24 +01001457 asus_kled_set(asus, 1);
Corentin Chary600ad522009-11-30 21:42:42 +01001458
1459 /* LED display is off by default */
Corentin Chary50a90c42009-11-30 21:55:12 +01001460 asus->ledd_status = 0xFFF;
Corentin Chary600ad522009-11-30 21:42:42 +01001461
1462 /* Set initial values of light sensor and level */
Corentin Charybe4ee822009-12-06 16:27:09 +01001463 asus->light_switch = 0; /* Default to light sensor disabled */
1464 asus->light_level = 5; /* level 5 for sensor sensitivity */
Corentin Chary600ad522009-11-30 21:42:42 +01001465
Corentin Charyd99b5772010-01-22 21:20:57 +01001466 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1467 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
Corentin Chary4d441512010-01-13 22:26:24 +01001468 asus_als_switch(asus, asus->light_switch);
Corentin Chary4d441512010-01-13 22:26:24 +01001469 asus_als_level(asus, asus->light_level);
Corentin Charyd99b5772010-01-22 21:20:57 +01001470 }
Corentin Chary600ad522009-11-30 21:42:42 +01001471
Corentin Chary47ee0e92010-01-24 11:17:15 +01001472 asus->lcd_state = 1; /* LCD should be on when the module load */
Corentin Chary600ad522009-11-30 21:42:42 +01001473 return result;
1474}
1475
1476static int __devinit asus_acpi_add(struct acpi_device *device)
1477{
Corentin Chary9129d142009-12-01 22:39:41 +01001478 struct asus_laptop *asus;
Corentin Chary600ad522009-11-30 21:42:42 +01001479 int result;
1480
1481 pr_notice("Asus Laptop Support version %s\n",
1482 ASUS_LAPTOP_VERSION);
Corentin Chary50a90c42009-11-30 21:55:12 +01001483 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1484 if (!asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001485 return -ENOMEM;
Corentin Chary50a90c42009-11-30 21:55:12 +01001486 asus->handle = device->handle;
1487 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1488 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1489 device->driver_data = asus;
1490 asus->device = device;
Corentin Chary600ad522009-11-30 21:42:42 +01001491
Corentin Chary9129d142009-12-01 22:39:41 +01001492 result = asus_acpi_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001493 if (result)
1494 goto fail_platform;
1495
1496 /*
1497 * Register the platform device first. It is used as a parent for the
1498 * sub-devices below.
1499 */
Corentin Chary9129d142009-12-01 22:39:41 +01001500 result = asus_platform_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001501 if (result)
1502 goto fail_platform;
1503
1504 if (!acpi_video_backlight_support()) {
Corentin Chary9129d142009-12-01 22:39:41 +01001505 result = asus_backlight_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001506 if (result)
1507 goto fail_backlight;
1508 } else
1509 pr_info("Backlight controlled by ACPI video driver\n");
1510
Corentin Chary9129d142009-12-01 22:39:41 +01001511 result = asus_input_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001512 if (result)
1513 goto fail_input;
1514
Corentin Chary9129d142009-12-01 22:39:41 +01001515 result = asus_led_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001516 if (result)
1517 goto fail_led;
1518
Corentin Chary18e13112010-01-25 23:29:24 +01001519 result = asus_rfkill_init(asus);
1520 if (result)
1521 goto fail_rfkill;
1522
Corentin Chary600ad522009-11-30 21:42:42 +01001523 asus_device_present = true;
1524 return 0;
1525
Corentin Chary18e13112010-01-25 23:29:24 +01001526fail_rfkill:
1527 asus_led_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001528fail_led:
Corentin Chary9129d142009-12-01 22:39:41 +01001529 asus_input_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001530fail_input:
Corentin Chary9129d142009-12-01 22:39:41 +01001531 asus_backlight_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001532fail_backlight:
Corentin Chary9129d142009-12-01 22:39:41 +01001533 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001534fail_platform:
Corentin Chary50a90c42009-11-30 21:55:12 +01001535 kfree(asus->name);
1536 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001537
1538 return result;
1539}
1540
1541static int asus_acpi_remove(struct acpi_device *device, int type)
1542{
Corentin Chary9129d142009-12-01 22:39:41 +01001543 struct asus_laptop *asus = acpi_driver_data(device);
1544
1545 asus_backlight_exit(asus);
Corentin Chary18e13112010-01-25 23:29:24 +01001546 asus_rfkill_exit(asus);
Corentin Chary9129d142009-12-01 22:39:41 +01001547 asus_led_exit(asus);
1548 asus_input_exit(asus);
1549 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001550
Corentin Chary50a90c42009-11-30 21:55:12 +01001551 kfree(asus->name);
1552 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001553 return 0;
1554}
1555
1556static const struct acpi_device_id asus_device_ids[] = {
1557 {"ATK0100", 0},
1558 {"ATK0101", 0},
1559 {"", 0},
1560};
1561MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1562
1563static struct acpi_driver asus_acpi_driver = {
Corentin Chary50a90c42009-11-30 21:55:12 +01001564 .name = ASUS_LAPTOP_NAME,
1565 .class = ASUS_LAPTOP_CLASS,
Corentin Chary600ad522009-11-30 21:42:42 +01001566 .owner = THIS_MODULE,
1567 .ids = asus_device_ids,
1568 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1569 .ops = {
1570 .add = asus_acpi_add,
1571 .remove = asus_acpi_remove,
1572 .notify = asus_acpi_notify,
1573 },
1574};
1575
Corentin Chary85091b72007-01-26 14:04:30 +01001576static int __init asus_laptop_init(void)
1577{
1578 int result;
1579
Corentin Chary600ad522009-11-30 21:42:42 +01001580 result = platform_driver_register(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001581 if (result < 0)
1582 return result;
1583
Corentin Chary600ad522009-11-30 21:42:42 +01001584 result = acpi_bus_register_driver(&asus_acpi_driver);
1585 if (result < 0)
1586 goto fail_acpi_driver;
1587 if (!asus_device_present) {
1588 result = -ENODEV;
1589 goto fail_no_device;
Corentin Chary85091b72007-01-26 14:04:30 +01001590 }
Len Brown8def05f2007-01-30 01:46:43 -05001591 return 0;
Corentin Chary85091b72007-01-26 14:04:30 +01001592
Corentin Chary600ad522009-11-30 21:42:42 +01001593fail_no_device:
1594 acpi_bus_unregister_driver(&asus_acpi_driver);
1595fail_acpi_driver:
1596 platform_driver_unregister(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001597 return result;
1598}
1599
Corentin Chary600ad522009-11-30 21:42:42 +01001600static void __exit asus_laptop_exit(void)
1601{
1602 acpi_bus_unregister_driver(&asus_acpi_driver);
1603 platform_driver_unregister(&platform_driver);
1604}
1605
Corentin Chary85091b72007-01-26 14:04:30 +01001606module_init(asus_laptop_init);
1607module_exit(asus_laptop_exit);