blob: 67d9228876161f257be85abf9f597bd6a6c55a1c [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;
Axel Linc26d85c2010-07-20 15:19:55 -070079module_param(wapf, uint, 0444);
Corentin Chary185e5af2007-03-11 10:27:33 +010080MODULE_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
Axel Linc26d85c2010-07-20 15:19:55 -070085module_param(wlan_status, int, 0444);
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
Axel Linc26d85c2010-07-20 15:19:55 -070090module_param(bluetooth_status, int, 0444);
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 Charyb58baecd2010-08-24 09:30:45 +0200281 {KE_KEY, 0xb5, { KEY_CALC } },
Corentin Chary034ce902009-01-20 16:17:43 +0100282 {KE_END, 0},
283};
284
Corentin Chary66a71dd2010-01-25 22:50:11 +0100285
Corentin Chary85091b72007-01-26 14:04:30 +0100286/*
287 * This function evaluates an ACPI method, given an int as parameter, the
288 * method is searched within the scope of the handle, can be NULL. The output
289 * of the method is written is output, which can also be NULL
290 *
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100291 * returns 0 if write is successful, -1 else.
Corentin Chary85091b72007-01-26 14:04:30 +0100292 */
Corentin Charyd8c67322009-11-28 10:27:51 +0100293static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
294 struct acpi_buffer *output)
Corentin Chary85091b72007-01-26 14:04:30 +0100295{
Corentin Charybe966662009-08-29 10:28:29 +0200296 struct acpi_object_list params; /* list of input parameters (an int) */
297 union acpi_object in_obj; /* the only param we use */
Corentin Chary85091b72007-01-26 14:04:30 +0100298 acpi_status status;
299
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100300 if (!handle)
Axel Lin9fb866f2010-07-20 15:19:47 -0700301 return -1;
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100302
Corentin Chary85091b72007-01-26 14:04:30 +0100303 params.count = 1;
304 params.pointer = &in_obj;
305 in_obj.type = ACPI_TYPE_INTEGER;
306 in_obj.integer.value = val;
307
308 status = acpi_evaluate_object(handle, (char *)method, &params, output);
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100309 if (status == AE_OK)
310 return 0;
311 else
312 return -1;
Corentin Chary85091b72007-01-26 14:04:30 +0100313}
314
Corentin Charyd8c67322009-11-28 10:27:51 +0100315static int write_acpi_int(acpi_handle handle, const char *method, int val)
316{
317 return write_acpi_int_ret(handle, method, val, NULL);
318}
319
Corentin Charyd99b5772010-01-22 21:20:57 +0100320static int acpi_check_handle(acpi_handle handle, const char *method,
321 acpi_handle *ret)
322{
323 acpi_status status;
324
325 if (method == NULL)
326 return -ENODEV;
327
328 if (ret)
329 status = acpi_get_handle(handle, (char *)method,
330 ret);
331 else {
332 acpi_handle dummy;
333
334 status = acpi_get_handle(handle, (char *)method,
335 &dummy);
336 }
337
338 if (status != AE_OK) {
339 if (ret)
340 pr_warning("Error finding %s\n", method);
341 return -ENODEV;
342 }
343 return 0;
344}
345
Corentin Chary17e78f62010-01-13 22:21:33 +0100346/* Generic LED function */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100347static int asus_led_set(struct asus_laptop *asus, const char *method,
Corentin Chary17e78f62010-01-13 22:21:33 +0100348 int value)
Corentin Charybe18cda2007-01-26 14:04:35 +0100349{
Corentin Charyd99b5772010-01-22 21:20:57 +0100350 if (!strcmp(method, METHOD_MLED))
Corentin Chary17e78f62010-01-13 22:21:33 +0100351 value = !value;
Corentin Charyd99b5772010-01-22 21:20:57 +0100352 else if (!strcmp(method, METHOD_GLED))
Corentin Chary17e78f62010-01-13 22:21:33 +0100353 value = !value + 1;
354 else
355 value = !!value;
Corentin Charybe18cda2007-01-26 14:04:35 +0100356
Corentin Charye5593bf2010-01-17 17:20:11 +0100357 return write_acpi_int(asus->handle, method, value);
Corentin Charybe18cda2007-01-26 14:04:35 +0100358}
359
Corentin Charybe4ee822009-12-06 16:27:09 +0100360/*
361 * LEDs
362 */
Corentin Charybe18cda2007-01-26 14:04:35 +0100363/* /sys/class/led handlers */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100364static void asus_led_cdev_set(struct led_classdev *led_cdev,
365 enum led_brightness value)
366{
367 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
368 struct asus_laptop *asus = led->asus;
Corentin Charybe18cda2007-01-26 14:04:35 +0100369
Corentin Charyaee0afb2010-01-26 21:01:34 +0100370 led->wk = !!value;
371 queue_work(asus->led_workqueue, &led->work);
372}
373
374static void asus_led_cdev_update(struct work_struct *work)
375{
376 struct asus_led *led = container_of(work, struct asus_led, work);
377 struct asus_laptop *asus = led->asus;
378
379 asus_led_set(asus, led->method, led->wk);
380}
381
382static enum led_brightness asus_led_cdev_get(struct led_classdev *led_cdev)
383{
384 return led_cdev->brightness;
385}
Corentin Charybe18cda2007-01-26 14:04:35 +0100386
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000387/*
Corentin Charybe4ee822009-12-06 16:27:09 +0100388 * Keyboard backlight (also a LED)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000389 */
Corentin Charyd99b5772010-01-22 21:20:57 +0100390static int asus_kled_lvl(struct asus_laptop *asus)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000391{
392 unsigned long long kblv;
393 struct acpi_object_list params;
394 union acpi_object in_obj;
395 acpi_status rv;
396
397 params.count = 1;
398 params.pointer = &in_obj;
399 in_obj.type = ACPI_TYPE_INTEGER;
400 in_obj.integer.value = 2;
401
Corentin Charyd99b5772010-01-22 21:20:57 +0100402 rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
403 &params, &kblv);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000404 if (ACPI_FAILURE(rv)) {
405 pr_warning("Error reading kled level\n");
Corentin Chary4d441512010-01-13 22:26:24 +0100406 return -ENODEV;
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000407 }
408 return kblv;
409}
410
Corentin Chary4d441512010-01-13 22:26:24 +0100411static int asus_kled_set(struct asus_laptop *asus, int kblv)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000412{
413 if (kblv > 0)
414 kblv = (1 << 7) | (kblv & 0x7F);
415 else
416 kblv = 0;
417
Corentin Charyd99b5772010-01-22 21:20:57 +0100418 if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000419 pr_warning("Keyboard LED display write failed\n");
420 return -EINVAL;
421 }
422 return 0;
423}
424
Corentin Charyaee0afb2010-01-26 21:01:34 +0100425static void asus_kled_cdev_set(struct led_classdev *led_cdev,
426 enum led_brightness value)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000427{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100428 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
429 struct asus_laptop *asus = led->asus;
Corentin Chary9129d142009-12-01 22:39:41 +0100430
Corentin Chary060cbce2010-01-28 10:52:40 +0100431 led->wk = value;
Corentin Charyaee0afb2010-01-26 21:01:34 +0100432 queue_work(asus->led_workqueue, &led->work);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000433}
434
Corentin Charyaee0afb2010-01-26 21:01:34 +0100435static void asus_kled_cdev_update(struct work_struct *work)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000436{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100437 struct asus_led *led = container_of(work, struct asus_led, work);
438 struct asus_laptop *asus = led->asus;
Corentin Chary9129d142009-12-01 22:39:41 +0100439
Corentin Charyaee0afb2010-01-26 21:01:34 +0100440 asus_kled_set(asus, led->wk);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000441}
442
Corentin Charyaee0afb2010-01-26 21:01:34 +0100443static enum led_brightness asus_kled_cdev_get(struct led_classdev *led_cdev)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000444{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100445 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
446 struct asus_laptop *asus = led->asus;
Corentin Charyd99b5772010-01-22 21:20:57 +0100447
448 return asus_kled_lvl(asus);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000449}
450
Corentin Charybe4ee822009-12-06 16:27:09 +0100451static void asus_led_exit(struct asus_laptop *asus)
452{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100453 if (asus->mled.led.dev)
454 led_classdev_unregister(&asus->mled.led);
455 if (asus->tled.led.dev)
456 led_classdev_unregister(&asus->tled.led);
457 if (asus->pled.led.dev)
458 led_classdev_unregister(&asus->pled.led);
459 if (asus->rled.led.dev)
460 led_classdev_unregister(&asus->rled.led);
461 if (asus->gled.led.dev)
462 led_classdev_unregister(&asus->gled.led);
463 if (asus->kled.led.dev)
464 led_classdev_unregister(&asus->kled.led);
465 if (asus->led_workqueue) {
466 destroy_workqueue(asus->led_workqueue);
467 asus->led_workqueue = NULL;
Corentin Charybe4ee822009-12-06 16:27:09 +0100468 }
469}
470
471/* Ugly macro, need to fix that later */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100472static int asus_led_register(struct asus_laptop *asus,
473 struct asus_led *led,
474 const char *name, const char *method)
475{
476 struct led_classdev *led_cdev = &led->led;
477
478 if (!method || acpi_check_handle(asus->handle, method, NULL))
Corentin Chary060cbce2010-01-28 10:52:40 +0100479 return 0; /* Led not present */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100480
481 led->asus = asus;
482 led->method = method;
483
484 INIT_WORK(&led->work, asus_led_cdev_update);
485 led_cdev->name = name;
486 led_cdev->brightness_set = asus_led_cdev_set;
487 led_cdev->brightness_get = asus_led_cdev_get;
488 led_cdev->max_brightness = 1;
489 return led_classdev_register(&asus->platform_device->dev, led_cdev);
490}
Corentin Charybe4ee822009-12-06 16:27:09 +0100491
492static int asus_led_init(struct asus_laptop *asus)
493{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100494 int r;
Corentin Charybe4ee822009-12-06 16:27:09 +0100495
496 /*
497 * Functions that actually update the LED's are called from a
498 * workqueue. By doing this as separate work rather than when the LED
499 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
500 * potentially bad time, such as a timer interrupt.
501 */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100502 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
503 if (!asus->led_workqueue)
Corentin Charybe4ee822009-12-06 16:27:09 +0100504 return -ENOMEM;
505
Corentin Charyaee0afb2010-01-26 21:01:34 +0100506 r = asus_led_register(asus, &asus->mled, "asus::mail", METHOD_MLED);
507 if (r)
508 goto error;
509 r = asus_led_register(asus, &asus->tled, "asus::touchpad", METHOD_TLED);
510 if (r)
511 goto error;
512 r = asus_led_register(asus, &asus->rled, "asus::record", METHOD_RLED);
513 if (r)
514 goto error;
515 r = asus_led_register(asus, &asus->pled, "asus::phone", METHOD_PLED);
516 if (r)
517 goto error;
518 r = asus_led_register(asus, &asus->gled, "asus::gaming", METHOD_GLED);
519 if (r)
520 goto error;
Corentin Charyd99b5772010-01-22 21:20:57 +0100521 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
Corentin Charyaee0afb2010-01-26 21:01:34 +0100522 !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL)) {
523 struct asus_led *led = &asus->kled;
524 struct led_classdev *cdev = &led->led;
525
526 led->asus = asus;
527
528 INIT_WORK(&led->work, asus_kled_cdev_update);
529 cdev->name = "asus::kbd_backlight";
530 cdev->brightness_set = asus_kled_cdev_set;
531 cdev->brightness_get = asus_kled_cdev_get;
532 cdev->max_brightness = 3;
533 r = led_classdev_register(&asus->platform_device->dev, cdev);
534 }
Corentin Charybe4ee822009-12-06 16:27:09 +0100535error:
Corentin Charyaee0afb2010-01-26 21:01:34 +0100536 if (r)
Corentin Charybe4ee822009-12-06 16:27:09 +0100537 asus_led_exit(asus);
Corentin Charyaee0afb2010-01-26 21:01:34 +0100538 return r;
Corentin Charybe4ee822009-12-06 16:27:09 +0100539}
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;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500643 struct backlight_properties props;
Corentin Charybe4ee822009-12-06 16:27:09 +0100644
Corentin Chary71e687d2010-08-24 09:30:44 +0200645 if (acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) ||
646 acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL) ||
647 !lcd_switch_handle)
648 return 0;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500649
Corentin Chary71e687d2010-08-24 09:30:44 +0200650 memset(&props, 0, sizeof(struct backlight_properties));
651 props.max_brightness = 15;
Corentin Charybe4ee822009-12-06 16:27:09 +0100652
Corentin Chary71e687d2010-08-24 09:30:44 +0200653 bd = backlight_device_register(ASUS_LAPTOP_FILE,
654 &asus->platform_device->dev, asus,
655 &asusbl_ops, &props);
656 if (IS_ERR(bd)) {
657 pr_err("Could not register asus backlight device\n");
658 asus->backlight_device = NULL;
659 return PTR_ERR(bd);
Corentin Charybe4ee822009-12-06 16:27:09 +0100660 }
Corentin Chary71e687d2010-08-24 09:30:44 +0200661
662 asus->backlight_device = bd;
663 bd->props.brightness = asus_read_brightness(bd);
664 bd->props.power = FB_BLANK_UNBLANK;
665 backlight_update_status(bd);
Corentin Charybe4ee822009-12-06 16:27:09 +0100666 return 0;
667}
668
669static void asus_backlight_exit(struct asus_laptop *asus)
670{
671 if (asus->backlight_device)
672 backlight_device_unregister(asus->backlight_device);
Corentin Charya539df52010-01-25 22:53:21 +0100673 asus->backlight_device = NULL;
Corentin Charybe4ee822009-12-06 16:27:09 +0100674}
675
Corentin Chary85091b72007-01-26 14:04:30 +0100676/*
677 * Platform device handlers
678 */
679
680/*
681 * We write our info in page, we begin at offset off and cannot write more
682 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
683 * number of bytes written in page
684 */
685static ssize_t show_infos(struct device *dev,
Len Brown8def05f2007-01-30 01:46:43 -0500686 struct device_attribute *attr, char *page)
Corentin Chary85091b72007-01-26 14:04:30 +0100687{
Corentin Chary9129d142009-12-01 22:39:41 +0100688 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary85091b72007-01-26 14:04:30 +0100689 int len = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400690 unsigned long long temp;
Corentin Charybe966662009-08-29 10:28:29 +0200691 char buf[16]; /* enough for all info */
Corentin Chary9a816852007-03-11 10:25:38 +0100692 acpi_status rv = AE_OK;
693
Corentin Chary85091b72007-01-26 14:04:30 +0100694 /*
Corentin Chary060cbce2010-01-28 10:52:40 +0100695 * We use the easy way, we don't care of off and count,
696 * so we don't set eof to 1
Corentin Chary85091b72007-01-26 14:04:30 +0100697 */
698
Corentin Chary50a90c42009-11-30 21:55:12 +0100699 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
700 len += sprintf(page + len, "Model reference : %s\n", asus->name);
Corentin Chary85091b72007-01-26 14:04:30 +0100701 /*
702 * The SFUN method probably allows the original driver to get the list
703 * of features supported by a given model. For now, 0x0100 or 0x0800
704 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
705 * The significance of others is yet to be found.
706 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100707 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100708 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000709 len += sprintf(page + len, "SFUN value : %#x\n",
710 (uint) temp);
711 /*
712 * The HWRS method return informations about the hardware.
713 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
714 * The significance of others is yet to be found.
715 * If we don't find the method, we assume the device are present.
716 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100717 rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
Corentin Chary1d4a3802009-08-28 12:56:46 +0000718 if (!ACPI_FAILURE(rv))
719 len += sprintf(page + len, "HRWS value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100720 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100721 /*
722 * Another value for userspace: the ASYM method returns 0x02 for
723 * battery low and 0x04 for battery critical, its readings tend to be
724 * more accurate than those provided by _BST.
725 * Note: since not all the laptops provide this method, errors are
726 * silently ignored.
727 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100728 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100729 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000730 len += sprintf(page + len, "ASYM value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100731 (uint) temp);
Corentin Chary7c247642009-11-30 22:13:54 +0100732 if (asus->dsdt_info) {
733 snprintf(buf, 16, "%d", asus->dsdt_info->length);
Corentin Chary85091b72007-01-26 14:04:30 +0100734 len += sprintf(page + len, "DSDT length : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100735 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
Corentin Chary85091b72007-01-26 14:04:30 +0100736 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100737 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100738 len += sprintf(page + len, "DSDT revision : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100739 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100740 len += sprintf(page + len, "OEM id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100741 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100742 len += sprintf(page + len, "OEM table id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100743 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100744 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100745 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100746 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100747 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100748 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
749 }
750
751 return len;
752}
753
754static int parse_arg(const char *buf, unsigned long count, int *val)
755{
756 if (!count)
757 return 0;
758 if (count > 31)
759 return -EINVAL;
760 if (sscanf(buf, "%i", val) != 1)
761 return -EINVAL;
762 return count;
763}
764
Corentin Chary17e78f62010-01-13 22:21:33 +0100765static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
766 const char *buf, size_t count,
Corentin Charyd99b5772010-01-22 21:20:57 +0100767 const char *method)
Corentin Chary4564de12007-01-26 14:04:40 +0100768{
769 int rv, value;
770 int out = 0;
771
772 rv = parse_arg(buf, count, &value);
773 if (rv > 0)
774 out = value ? 1 : 0;
775
Corentin Charyd99b5772010-01-22 21:20:57 +0100776 if (write_acpi_int(asus->handle, method, value))
Corentin Chary17e78f62010-01-13 22:21:33 +0100777 return -ENODEV;
Corentin Chary4564de12007-01-26 14:04:40 +0100778 return rv;
779}
780
781/*
Corentin Chary722ad972007-01-26 14:04:55 +0100782 * LEDD display
783 */
784static ssize_t show_ledd(struct device *dev,
785 struct device_attribute *attr, char *buf)
786{
Corentin Chary9129d142009-12-01 22:39:41 +0100787 struct asus_laptop *asus = dev_get_drvdata(dev);
788
Corentin Chary50a90c42009-11-30 21:55:12 +0100789 return sprintf(buf, "0x%08x\n", asus->ledd_status);
Corentin Chary722ad972007-01-26 14:04:55 +0100790}
791
792static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
793 const char *buf, size_t count)
794{
Corentin Chary9129d142009-12-01 22:39:41 +0100795 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary722ad972007-01-26 14:04:55 +0100796 int rv, value;
797
798 rv = parse_arg(buf, count, &value);
799 if (rv > 0) {
Axel Lin6a984a02010-07-20 15:19:48 -0700800 if (write_acpi_int(asus->handle, METHOD_LEDD, value)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200801 pr_warning("LED display write failed\n");
Axel Lin6a984a02010-07-20 15:19:48 -0700802 return -ENODEV;
803 }
804 asus->ledd_status = (u32) value;
Corentin Chary722ad972007-01-26 14:04:55 +0100805 }
806 return rv;
807}
808
809/*
Corentin Charyaa9df932010-01-13 21:49:10 +0100810 * Wireless
811 */
812static int asus_wireless_status(struct asus_laptop *asus, int mask)
813{
814 unsigned long long status;
815 acpi_status rv = AE_OK;
816
817 if (!asus->have_rsts)
818 return (asus->wireless_status & mask) ? 1 : 0;
819
Corentin Charyd99b5772010-01-22 21:20:57 +0100820 rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
821 NULL, &status);
Corentin Charyaa9df932010-01-13 21:49:10 +0100822 if (ACPI_FAILURE(rv)) {
823 pr_warning("Error reading Wireless status\n");
824 return -EINVAL;
825 }
826 return !!(status & mask);
827}
828
829/*
Corentin Chary4564de12007-01-26 14:04:40 +0100830 * WLAN
831 */
Corentin Charye5593bf2010-01-17 17:20:11 +0100832static int asus_wlan_set(struct asus_laptop *asus, int status)
833{
834 if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
835 pr_warning("Error setting wlan status to %d", status);
836 return -EIO;
837 }
838 return 0;
839}
840
Corentin Chary4564de12007-01-26 14:04:40 +0100841static ssize_t show_wlan(struct device *dev,
842 struct device_attribute *attr, char *buf)
843{
Corentin Chary9129d142009-12-01 22:39:41 +0100844 struct asus_laptop *asus = dev_get_drvdata(dev);
845
Corentin Chary17e78f62010-01-13 22:21:33 +0100846 return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100847}
848
849static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
850 const char *buf, size_t count)
851{
Corentin Chary9129d142009-12-01 22:39:41 +0100852 struct asus_laptop *asus = dev_get_drvdata(dev);
853
Corentin Charyd99b5772010-01-22 21:20:57 +0100854 return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
Corentin Chary4564de12007-01-26 14:04:40 +0100855}
856
857/*
858 * Bluetooth
859 */
Corentin Charye5593bf2010-01-17 17:20:11 +0100860static int asus_bluetooth_set(struct asus_laptop *asus, int status)
861{
862 if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
863 pr_warning("Error setting bluetooth status to %d", status);
864 return -EIO;
865 }
866 return 0;
867}
868
Corentin Chary4564de12007-01-26 14:04:40 +0100869static ssize_t show_bluetooth(struct device *dev,
870 struct device_attribute *attr, char *buf)
871{
Corentin Chary9129d142009-12-01 22:39:41 +0100872 struct asus_laptop *asus = dev_get_drvdata(dev);
873
Corentin Chary17e78f62010-01-13 22:21:33 +0100874 return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100875}
876
Len Brown8def05f2007-01-30 01:46:43 -0500877static ssize_t store_bluetooth(struct device *dev,
878 struct device_attribute *attr, const char *buf,
879 size_t count)
Corentin Chary4564de12007-01-26 14:04:40 +0100880{
Corentin Chary9129d142009-12-01 22:39:41 +0100881 struct asus_laptop *asus = dev_get_drvdata(dev);
882
Corentin Charyd99b5772010-01-22 21:20:57 +0100883 return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
Corentin Chary4564de12007-01-26 14:04:40 +0100884}
885
Corentin Chary78127b42007-01-26 14:04:49 +0100886/*
887 * Display
888 */
Corentin Chary4d441512010-01-13 22:26:24 +0100889static void asus_set_display(struct asus_laptop *asus, int value)
Corentin Chary78127b42007-01-26 14:04:49 +0100890{
891 /* no sanity check needed for now */
Corentin Charyd99b5772010-01-22 21:20:57 +0100892 if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200893 pr_warning("Error setting display\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100894 return;
895}
896
Corentin Chary9129d142009-12-01 22:39:41 +0100897static int read_display(struct asus_laptop *asus)
Corentin Chary78127b42007-01-26 14:04:49 +0100898{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400899 unsigned long long value = 0;
Corentin Chary9a816852007-03-11 10:25:38 +0100900 acpi_status rv = AE_OK;
Corentin Chary78127b42007-01-26 14:04:49 +0100901
Corentin Charybe966662009-08-29 10:28:29 +0200902 /*
903 * In most of the case, we know how to set the display, but sometime
904 * we can't read it
905 */
Len Brown8def05f2007-01-30 01:46:43 -0500906 if (display_get_handle) {
Corentin Chary9a816852007-03-11 10:25:38 +0100907 rv = acpi_evaluate_integer(display_get_handle, NULL,
908 NULL, &value);
909 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200910 pr_warning("Error reading display status\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100911 }
912
Corentin Chary060cbce2010-01-28 10:52:40 +0100913 value &= 0x0F; /* needed for some models, shouldn't hurt others */
Corentin Chary78127b42007-01-26 14:04:49 +0100914
915 return value;
916}
Len Brown8def05f2007-01-30 01:46:43 -0500917
Corentin Chary78127b42007-01-26 14:04:49 +0100918/*
919 * Now, *this* one could be more user-friendly, but so far, no-one has
920 * complained. The significance of bits is the same as in store_disp()
921 */
922static ssize_t show_disp(struct device *dev,
923 struct device_attribute *attr, char *buf)
924{
Corentin Chary9129d142009-12-01 22:39:41 +0100925 struct asus_laptop *asus = dev_get_drvdata(dev);
926
Corentin Chary2a1fd642010-01-26 21:02:23 +0100927 if (!display_get_handle)
928 return -ENODEV;
Corentin Chary9129d142009-12-01 22:39:41 +0100929 return sprintf(buf, "%d\n", read_display(asus));
Corentin Chary78127b42007-01-26 14:04:49 +0100930}
931
932/*
933 * Experimental support for display switching. As of now: 1 should activate
934 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
935 * Any combination (bitwise) of these will suffice. I never actually tested 4
936 * displays hooked up simultaneously, so be warned. See the acpi4asus README
937 * for more info.
938 */
939static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
940 const char *buf, size_t count)
941{
Corentin Chary9129d142009-12-01 22:39:41 +0100942 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary78127b42007-01-26 14:04:49 +0100943 int rv, value;
944
945 rv = parse_arg(buf, count, &value);
946 if (rv > 0)
Corentin Chary4d441512010-01-13 22:26:24 +0100947 asus_set_display(asus, value);
Corentin Chary78127b42007-01-26 14:04:49 +0100948 return rv;
949}
950
Corentin Chary8b857352007-01-26 14:04:58 +0100951/*
952 * Light Sens
953 */
Corentin Chary4d441512010-01-13 22:26:24 +0100954static void asus_als_switch(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100955{
Corentin Charyd99b5772010-01-22 21:20:57 +0100956 if (write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200957 pr_warning("Error setting light sensor switch\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100958 asus->light_switch = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100959}
960
961static ssize_t show_lssw(struct device *dev,
962 struct device_attribute *attr, char *buf)
963{
Corentin Chary9129d142009-12-01 22:39:41 +0100964 struct asus_laptop *asus = dev_get_drvdata(dev);
965
Corentin Chary50a90c42009-11-30 21:55:12 +0100966 return sprintf(buf, "%d\n", asus->light_switch);
Corentin Chary8b857352007-01-26 14:04:58 +0100967}
968
969static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
970 const char *buf, size_t count)
971{
Corentin Chary9129d142009-12-01 22:39:41 +0100972 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100973 int rv, value;
974
975 rv = parse_arg(buf, count, &value);
976 if (rv > 0)
Corentin Chary4d441512010-01-13 22:26:24 +0100977 asus_als_switch(asus, value ? 1 : 0);
Corentin Chary8b857352007-01-26 14:04:58 +0100978
979 return rv;
980}
981
Corentin Chary4d441512010-01-13 22:26:24 +0100982static void asus_als_level(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100983{
Corentin Charyd99b5772010-01-22 21:20:57 +0100984 if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200985 pr_warning("Error setting light sensor level\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100986 asus->light_level = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100987}
988
989static ssize_t show_lslvl(struct device *dev,
990 struct device_attribute *attr, char *buf)
991{
Corentin Chary9129d142009-12-01 22:39:41 +0100992 struct asus_laptop *asus = dev_get_drvdata(dev);
993
Corentin Chary50a90c42009-11-30 21:55:12 +0100994 return sprintf(buf, "%d\n", asus->light_level);
Corentin Chary8b857352007-01-26 14:04:58 +0100995}
996
997static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
998 const char *buf, size_t count)
999{
Corentin Chary9129d142009-12-01 22:39:41 +01001000 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +01001001 int rv, value;
1002
1003 rv = parse_arg(buf, count, &value);
1004 if (rv > 0) {
1005 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
1006 /* 0 <= value <= 15 */
Corentin Chary4d441512010-01-13 22:26:24 +01001007 asus_als_level(asus, value);
Corentin Chary8b857352007-01-26 14:04:58 +01001008 }
1009
1010 return rv;
1011}
1012
Corentin Charye539c2f2007-05-06 14:47:06 +02001013/*
1014 * GPS
1015 */
Corentin Chary6358bf22010-01-13 21:55:44 +01001016static int asus_gps_status(struct asus_laptop *asus)
1017{
1018 unsigned long long status;
1019 acpi_status rv = AE_OK;
1020
Corentin Charyd99b5772010-01-22 21:20:57 +01001021 rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
1022 NULL, &status);
Corentin Chary6358bf22010-01-13 21:55:44 +01001023 if (ACPI_FAILURE(rv)) {
1024 pr_warning("Error reading GPS status\n");
1025 return -ENODEV;
1026 }
1027 return !!status;
1028}
1029
1030static int asus_gps_switch(struct asus_laptop *asus, int status)
1031{
Corentin Charyd99b5772010-01-22 21:20:57 +01001032 const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
Corentin Chary6358bf22010-01-13 21:55:44 +01001033
Corentin Charyd99b5772010-01-22 21:20:57 +01001034 if (write_acpi_int(asus->handle, meth, 0x02))
Corentin Chary6358bf22010-01-13 21:55:44 +01001035 return -ENODEV;
1036 return 0;
1037}
1038
Corentin Charye539c2f2007-05-06 14:47:06 +02001039static ssize_t show_gps(struct device *dev,
1040 struct device_attribute *attr, char *buf)
1041{
Corentin Chary9129d142009-12-01 22:39:41 +01001042 struct asus_laptop *asus = dev_get_drvdata(dev);
1043
Corentin Chary6358bf22010-01-13 21:55:44 +01001044 return sprintf(buf, "%d\n", asus_gps_status(asus));
Corentin Charye539c2f2007-05-06 14:47:06 +02001045}
1046
1047static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
1048 const char *buf, size_t count)
1049{
Corentin Chary060cbce2010-01-28 10:52:40 +01001050 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary6358bf22010-01-13 21:55:44 +01001051 int rv, value;
1052 int ret;
Corentin Chary9129d142009-12-01 22:39:41 +01001053
Corentin Chary6358bf22010-01-13 21:55:44 +01001054 rv = parse_arg(buf, count, &value);
1055 if (rv <= 0)
1056 return -EINVAL;
1057 ret = asus_gps_switch(asus, !!value);
1058 if (ret)
1059 return ret;
Corentin Chary18e13112010-01-25 23:29:24 +01001060 rfkill_set_sw_state(asus->gps_rfkill, !value);
Corentin Chary6358bf22010-01-13 21:55:44 +01001061 return rv;
Corentin Charye539c2f2007-05-06 14:47:06 +02001062}
1063
Corentin Chary034ce902009-01-20 16:17:43 +01001064/*
Corentin Chary18e13112010-01-25 23:29:24 +01001065 * rfkill
1066 */
1067static int asus_gps_rfkill_set(void *data, bool blocked)
1068{
Corentin Chary23f45c32010-08-24 09:30:46 +02001069 struct asus_laptop *asus = data;
Corentin Chary18e13112010-01-25 23:29:24 +01001070
Corentin Chary23f45c32010-08-24 09:30:46 +02001071 return asus_gps_switch(asus, !blocked);
Corentin Chary18e13112010-01-25 23:29:24 +01001072}
1073
1074static const struct rfkill_ops asus_gps_rfkill_ops = {
1075 .set_block = asus_gps_rfkill_set,
1076};
1077
1078static void asus_rfkill_exit(struct asus_laptop *asus)
1079{
1080 if (asus->gps_rfkill) {
1081 rfkill_unregister(asus->gps_rfkill);
1082 rfkill_destroy(asus->gps_rfkill);
1083 asus->gps_rfkill = NULL;
1084 }
1085}
1086
1087static int asus_rfkill_init(struct asus_laptop *asus)
1088{
1089 int result;
1090
1091 if (acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) ||
1092 acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) ||
1093 acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
1094 return 0;
1095
1096 asus->gps_rfkill = rfkill_alloc("asus-gps", &asus->platform_device->dev,
1097 RFKILL_TYPE_GPS,
Corentin Chary23f45c32010-08-24 09:30:46 +02001098 &asus_gps_rfkill_ops, asus);
Corentin Chary18e13112010-01-25 23:29:24 +01001099 if (!asus->gps_rfkill)
1100 return -EINVAL;
1101
1102 result = rfkill_register(asus->gps_rfkill);
1103 if (result) {
1104 rfkill_destroy(asus->gps_rfkill);
1105 asus->gps_rfkill = NULL;
1106 }
1107
1108 return result;
1109}
1110
1111/*
Corentin Charybe4ee822009-12-06 16:27:09 +01001112 * Input device (i.e. hotkeys)
Corentin Chary034ce902009-01-20 16:17:43 +01001113 */
Corentin Charybe4ee822009-12-06 16:27:09 +01001114static void asus_input_notify(struct asus_laptop *asus, int event)
1115{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001116 if (asus->inputdev)
1117 sparse_keymap_report_event(asus->inputdev, event, 1, true);
Corentin Charybe4ee822009-12-06 16:27:09 +01001118}
1119
1120static int asus_input_init(struct asus_laptop *asus)
1121{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001122 struct input_dev *input;
1123 int error;
Corentin Charybe4ee822009-12-06 16:27:09 +01001124
Corentin Chary66a71dd2010-01-25 22:50:11 +01001125 input = input_allocate_device();
1126 if (!input) {
Corentin Charybe4ee822009-12-06 16:27:09 +01001127 pr_info("Unable to allocate input device\n");
Axel Lin45036ae2010-07-05 15:29:00 +08001128 return -ENOMEM;
Corentin Charybe4ee822009-12-06 16:27:09 +01001129 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001130 input->name = "Asus Laptop extra buttons";
1131 input->phys = ASUS_LAPTOP_FILE "/input0";
1132 input->id.bustype = BUS_HOST;
1133 input->dev.parent = &asus->platform_device->dev;
Corentin Charybe4ee822009-12-06 16:27:09 +01001134
Corentin Chary66a71dd2010-01-25 22:50:11 +01001135 error = sparse_keymap_setup(input, asus_keymap, NULL);
1136 if (error) {
1137 pr_err("Unable to setup input device keymap\n");
Axel Lin45036ae2010-07-05 15:29:00 +08001138 goto err_free_dev;
Corentin Charybe4ee822009-12-06 16:27:09 +01001139 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001140 error = input_register_device(input);
1141 if (error) {
Corentin Charybe4ee822009-12-06 16:27:09 +01001142 pr_info("Unable to register input device\n");
Axel Lin45036ae2010-07-05 15:29:00 +08001143 goto err_free_keymap;
Corentin Charybe4ee822009-12-06 16:27:09 +01001144 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001145
1146 asus->inputdev = input;
1147 return 0;
1148
Axel Lin45036ae2010-07-05 15:29:00 +08001149err_free_keymap:
Corentin Chary66a71dd2010-01-25 22:50:11 +01001150 sparse_keymap_free(input);
Axel Lin45036ae2010-07-05 15:29:00 +08001151err_free_dev:
Corentin Chary66a71dd2010-01-25 22:50:11 +01001152 input_free_device(input);
1153 return error;
Corentin Charybe4ee822009-12-06 16:27:09 +01001154}
1155
1156static void asus_input_exit(struct asus_laptop *asus)
1157{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001158 if (asus->inputdev) {
1159 sparse_keymap_free(asus->inputdev);
Corentin Charybe4ee822009-12-06 16:27:09 +01001160 input_unregister_device(asus->inputdev);
Corentin Chary66a71dd2010-01-25 22:50:11 +01001161 }
Corentin Chary71e687d2010-08-24 09:30:44 +02001162 asus->inputdev = NULL;
Corentin Charybe4ee822009-12-06 16:27:09 +01001163}
1164
1165/*
1166 * ACPI driver
1167 */
Corentin Chary600ad522009-11-30 21:42:42 +01001168static void asus_acpi_notify(struct acpi_device *device, u32 event)
Corentin Chary85091b72007-01-26 14:04:30 +01001169{
Corentin Chary9129d142009-12-01 22:39:41 +01001170 struct asus_laptop *asus = acpi_driver_data(device);
Corentin Chary6050c8d2009-02-15 19:30:19 +01001171 u16 count;
Corentin Chary034ce902009-01-20 16:17:43 +01001172
Corentin Chary6b7091e2007-01-26 14:04:45 +01001173 /*
1174 * We need to tell the backlight device when the backlight power is
1175 * switched
1176 */
Corentin Chary3e68ae72010-01-13 22:10:39 +01001177 if (event == ATKD_LCD_ON)
Corentin Chary9129d142009-12-01 22:39:41 +01001178 lcd_blank(asus, FB_BLANK_UNBLANK);
Corentin Chary3e68ae72010-01-13 22:10:39 +01001179 else if (event == ATKD_LCD_OFF)
Corentin Chary9129d142009-12-01 22:39:41 +01001180 lcd_blank(asus, FB_BLANK_POWERDOWN);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001181
Corentin Chary619d8b12009-11-28 10:35:37 +01001182 /* TODO Find a better way to handle events count. */
Corentin Chary50a90c42009-11-30 21:55:12 +01001183 count = asus->event_count[event % 128]++;
1184 acpi_bus_generate_proc_event(asus->device, event, count);
1185 acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
1186 dev_name(&asus->device->dev), event,
Corentin Chary6050c8d2009-02-15 19:30:19 +01001187 count);
Corentin Chary85091b72007-01-26 14:04:30 +01001188
Corentin Charya539df52010-01-25 22:53:21 +01001189 /* Brightness events are special */
1190 if (event >= ATKD_BR_MIN && event <= ATKD_BR_MAX) {
1191
1192 /* Ignore them completely if the acpi video driver is used */
1193 if (asus->backlight_device != NULL) {
1194 /* Update the backlight device. */
1195 asus_backlight_notify(asus);
1196 }
1197 return ;
1198 }
Corentin Charybe4ee822009-12-06 16:27:09 +01001199 asus_input_notify(asus, event);
Corentin Chary85091b72007-01-26 14:04:30 +01001200}
1201
Corentin Chary2a1fd642010-01-26 21:02:23 +01001202static DEVICE_ATTR(infos, S_IRUGO, show_infos, NULL);
1203static DEVICE_ATTR(wlan, S_IRUGO | S_IWUSR, show_wlan, store_wlan);
1204static DEVICE_ATTR(bluetooth, S_IRUGO | S_IWUSR, show_bluetooth,
1205 store_bluetooth);
1206static DEVICE_ATTR(display, S_IRUGO | S_IWUSR, show_disp, store_disp);
1207static DEVICE_ATTR(ledd, S_IRUGO | S_IWUSR, show_ledd, store_ledd);
1208static DEVICE_ATTR(ls_level, S_IRUGO | S_IWUSR, show_lslvl, store_lslvl);
1209static DEVICE_ATTR(ls_switch, S_IRUGO | S_IWUSR, show_lssw, store_lssw);
1210static DEVICE_ATTR(gps, S_IRUGO | S_IWUSR, show_gps, store_gps);
1211
1212static void asus_sysfs_exit(struct asus_laptop *asus)
1213{
1214 struct platform_device *device = asus->platform_device;
1215
1216 device_remove_file(&device->dev, &dev_attr_infos);
1217 device_remove_file(&device->dev, &dev_attr_wlan);
1218 device_remove_file(&device->dev, &dev_attr_bluetooth);
1219 device_remove_file(&device->dev, &dev_attr_display);
1220 device_remove_file(&device->dev, &dev_attr_ledd);
1221 device_remove_file(&device->dev, &dev_attr_ls_switch);
1222 device_remove_file(&device->dev, &dev_attr_ls_level);
1223 device_remove_file(&device->dev, &dev_attr_gps);
1224}
1225
1226static int asus_sysfs_init(struct asus_laptop *asus)
1227{
1228 struct platform_device *device = asus->platform_device;
1229 int err;
1230
1231 err = device_create_file(&device->dev, &dev_attr_infos);
1232 if (err)
1233 return err;
1234
1235 if (!acpi_check_handle(asus->handle, METHOD_WLAN, NULL)) {
1236 err = device_create_file(&device->dev, &dev_attr_wlan);
1237 if (err)
1238 return err;
Corentin Chary85091b72007-01-26 14:04:30 +01001239 }
1240
Corentin Chary2a1fd642010-01-26 21:02:23 +01001241 if (!acpi_check_handle(asus->handle, METHOD_BLUETOOTH, NULL)) {
1242 err = device_create_file(&device->dev, &dev_attr_bluetooth);
1243 if (err)
1244 return err;
1245 }
Corentin Chary85091b72007-01-26 14:04:30 +01001246
Corentin Chary2a1fd642010-01-26 21:02:23 +01001247 if (!acpi_check_handle(asus->handle, METHOD_SWITCH_DISPLAY, NULL)) {
1248 err = device_create_file(&device->dev, &dev_attr_display);
1249 if (err)
1250 return err;
1251 }
Corentin Chary85091b72007-01-26 14:04:30 +01001252
Corentin Chary2a1fd642010-01-26 21:02:23 +01001253 if (!acpi_check_handle(asus->handle, METHOD_LEDD, NULL)) {
1254 err = device_create_file(&device->dev, &dev_attr_ledd);
1255 if (err)
1256 return err;
1257 }
Corentin Chary85091b72007-01-26 14:04:30 +01001258
Corentin Chary2a1fd642010-01-26 21:02:23 +01001259 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1260 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
1261 err = device_create_file(&device->dev, &dev_attr_ls_switch);
1262 if (err)
1263 return err;
1264 err = device_create_file(&device->dev, &dev_attr_ls_level);
1265 if (err)
1266 return err;
1267 }
1268
1269 if (!acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) &&
1270 !acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) &&
1271 !acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL)) {
1272 err = device_create_file(&device->dev, &dev_attr_gps);
1273 if (err)
1274 return err;
1275 }
1276
1277 return err;
1278}
Corentin Chary85091b72007-01-26 14:04:30 +01001279
Corentin Chary9129d142009-12-01 22:39:41 +01001280static int asus_platform_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001281{
Corentin Chary71e687d2010-08-24 09:30:44 +02001282 int result;
Corentin Chary600ad522009-11-30 21:42:42 +01001283
Corentin Chary50a90c42009-11-30 21:55:12 +01001284 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1285 if (!asus->platform_device)
Corentin Chary600ad522009-11-30 21:42:42 +01001286 return -ENOMEM;
Corentin Chary9129d142009-12-01 22:39:41 +01001287 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001288
Corentin Chary71e687d2010-08-24 09:30:44 +02001289 result = platform_device_add(asus->platform_device);
1290 if (result)
Corentin Chary600ad522009-11-30 21:42:42 +01001291 goto fail_platform_device;
1292
Corentin Chary71e687d2010-08-24 09:30:44 +02001293 result = asus_sysfs_init(asus);
1294 if (result)
Corentin Chary600ad522009-11-30 21:42:42 +01001295 goto fail_sysfs;
1296 return 0;
1297
1298fail_sysfs:
Corentin Chary2a1fd642010-01-26 21:02:23 +01001299 asus_sysfs_exit(asus);
Corentin Chary50a90c42009-11-30 21:55:12 +01001300 platform_device_del(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001301fail_platform_device:
Corentin Chary50a90c42009-11-30 21:55:12 +01001302 platform_device_put(asus->platform_device);
Corentin Chary71e687d2010-08-24 09:30:44 +02001303 return result;
Corentin Chary600ad522009-11-30 21:42:42 +01001304}
1305
Corentin Chary9129d142009-12-01 22:39:41 +01001306static void asus_platform_exit(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001307{
Corentin Chary2a1fd642010-01-26 21:02:23 +01001308 asus_sysfs_exit(asus);
Corentin Chary50a90c42009-11-30 21:55:12 +01001309 platform_device_unregister(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001310}
1311
1312static struct platform_driver platform_driver = {
Len Brown8def05f2007-01-30 01:46:43 -05001313 .driver = {
Corentin Chary9129d142009-12-01 22:39:41 +01001314 .name = ASUS_LAPTOP_FILE,
1315 .owner = THIS_MODULE,
1316 }
Corentin Chary85091b72007-01-26 14:04:30 +01001317};
1318
Len Brown8def05f2007-01-30 01:46:43 -05001319static int asus_handle_init(char *name, acpi_handle * handle,
Corentin Chary85091b72007-01-26 14:04:30 +01001320 char **paths, int num_paths)
1321{
1322 int i;
1323 acpi_status status;
1324
1325 for (i = 0; i < num_paths; i++) {
1326 status = acpi_get_handle(NULL, paths[i], handle);
1327 if (ACPI_SUCCESS(status))
1328 return 0;
1329 }
1330
1331 *handle = NULL;
1332 return -ENODEV;
1333}
1334
1335#define ASUS_HANDLE_INIT(object) \
1336 asus_handle_init(#object, &object##_handle, object##_paths, \
1337 ARRAY_SIZE(object##_paths))
1338
Corentin Chary85091b72007-01-26 14:04:30 +01001339/*
Corentin Chary50a90c42009-11-30 21:55:12 +01001340 * This function is used to initialize the context with right values. In this
1341 * method, we can make all the detection we want, and modify the asus_laptop
1342 * struct
Corentin Chary85091b72007-01-26 14:04:30 +01001343 */
Corentin Chary7c247642009-11-30 22:13:54 +01001344static int asus_laptop_get_info(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001345{
1346 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Corentin Chary85091b72007-01-26 14:04:30 +01001347 union acpi_object *model = NULL;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001348 unsigned long long bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +01001349 char *string = NULL;
1350 acpi_status status;
1351
1352 /*
1353 * Get DSDT headers early enough to allow for differentiating between
1354 * models, but late enough to allow acpi_bus_register_driver() to fail
1355 * before doing anything ACPI-specific. Should we encounter a machine,
1356 * which needs special handling (i.e. its hotkey device has a different
Corentin Chary7c247642009-11-30 22:13:54 +01001357 * HID), this bit will be moved.
Corentin Chary85091b72007-01-26 14:04:30 +01001358 */
Corentin Chary7c247642009-11-30 22:13:54 +01001359 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
Corentin Chary85091b72007-01-26 14:04:30 +01001360 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001361 pr_warning("Couldn't get the DSDT table header\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001362
1363 /* We have to write 0 on init this far for all ASUS models */
Corentin Chary50a90c42009-11-30 21:55:12 +01001364 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001365 pr_err("Hotkey initialization failed\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001366 return -ENODEV;
1367 }
1368
1369 /* This needs to be called for some laptops to init properly */
Corentin Chary9a816852007-03-11 10:25:38 +01001370 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001371 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
Corentin Chary9a816852007-03-11 10:25:38 +01001372 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001373 pr_warning("Error calling BSTS\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001374 else if (bsts_result)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001375 pr_notice("BSTS called, 0x%02x returned\n",
Corentin Chary9a816852007-03-11 10:25:38 +01001376 (uint) bsts_result);
Corentin Chary85091b72007-01-26 14:04:30 +01001377
Corentin Chary185e5af2007-03-11 10:27:33 +01001378 /* This too ... */
Corentin Charye5593bf2010-01-17 17:20:11 +01001379 if (write_acpi_int(asus->handle, "CWAP", wapf))
1380 pr_err("Error calling CWAP(%d)\n", wapf);
Corentin Chary85091b72007-01-26 14:04:30 +01001381 /*
1382 * Try to match the object returned by INIT to the specific model.
1383 * Handle every possible object (or the lack of thereof) the DSDT
1384 * writers might throw at us. When in trouble, we pass NULL to
1385 * asus_model_match() and try something completely different.
1386 */
1387 if (buffer.pointer) {
1388 model = buffer.pointer;
1389 switch (model->type) {
1390 case ACPI_TYPE_STRING:
1391 string = model->string.pointer;
1392 break;
1393 case ACPI_TYPE_BUFFER:
1394 string = model->buffer.pointer;
1395 break;
1396 default:
1397 string = "";
1398 break;
1399 }
1400 }
Corentin Chary50a90c42009-11-30 21:55:12 +01001401 asus->name = kstrdup(string, GFP_KERNEL);
Axel Lind8eca112010-06-29 11:09:47 +08001402 if (!asus->name) {
1403 kfree(buffer.pointer);
Corentin Chary85091b72007-01-26 14:04:30 +01001404 return -ENOMEM;
Axel Lind8eca112010-06-29 11:09:47 +08001405 }
Corentin Chary85091b72007-01-26 14:04:30 +01001406
Len Brown8def05f2007-01-30 01:46:43 -05001407 if (*string)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001408 pr_notice(" %s model detected\n", string);
Corentin Chary85091b72007-01-26 14:04:30 +01001409
Corentin Chary4564de12007-01-26 14:04:40 +01001410 /*
1411 * The HWRS method return informations about the hardware.
1412 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1413 * The significance of others is yet to be found.
Corentin Chary4564de12007-01-26 14:04:40 +01001414 */
Corentin Chary9a816852007-03-11 10:25:38 +01001415 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001416 acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
Corentin Charyd99b5772010-01-22 21:20:57 +01001417 if (!ACPI_FAILURE(status))
1418 pr_notice(" HRWS returned %x", (int)hwrs_result);
Corentin Chary4564de12007-01-26 14:04:40 +01001419
Corentin Charyd99b5772010-01-22 21:20:57 +01001420 if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
Corentin Charyaa9df932010-01-13 21:49:10 +01001421 asus->have_rsts = true;
Corentin Chary4564de12007-01-26 14:04:40 +01001422
Corentin Charyd99b5772010-01-22 21:20:57 +01001423 /* Scheduled for removal */
Corentin Chary6b7091e2007-01-26 14:04:45 +01001424 ASUS_HANDLE_INIT(lcd_switch);
Corentin Chary78127b42007-01-26 14:04:49 +01001425 ASUS_HANDLE_INIT(display_get);
1426
Corentin Chary85091b72007-01-26 14:04:30 +01001427 kfree(model);
1428
1429 return AE_OK;
1430}
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
Corentin Chary71e687d2010-08-24 09:30:44 +02001476static bool asus_device_present;
1477
Corentin Chary600ad522009-11-30 21:42:42 +01001478static int __devinit asus_acpi_add(struct acpi_device *device)
1479{
Corentin Chary9129d142009-12-01 22:39:41 +01001480 struct asus_laptop *asus;
Corentin Chary600ad522009-11-30 21:42:42 +01001481 int result;
1482
1483 pr_notice("Asus Laptop Support version %s\n",
1484 ASUS_LAPTOP_VERSION);
Corentin Chary50a90c42009-11-30 21:55:12 +01001485 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1486 if (!asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001487 return -ENOMEM;
Corentin Chary50a90c42009-11-30 21:55:12 +01001488 asus->handle = device->handle;
1489 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1490 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1491 device->driver_data = asus;
1492 asus->device = device;
Corentin Chary600ad522009-11-30 21:42:42 +01001493
Corentin Chary9129d142009-12-01 22:39:41 +01001494 result = asus_acpi_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001495 if (result)
1496 goto fail_platform;
1497
1498 /*
1499 * Register the platform device first. It is used as a parent for the
1500 * sub-devices below.
1501 */
Corentin Chary9129d142009-12-01 22:39:41 +01001502 result = asus_platform_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001503 if (result)
1504 goto fail_platform;
1505
1506 if (!acpi_video_backlight_support()) {
Corentin Chary9129d142009-12-01 22:39:41 +01001507 result = asus_backlight_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001508 if (result)
1509 goto fail_backlight;
1510 } else
1511 pr_info("Backlight controlled by ACPI video driver\n");
1512
Corentin Chary9129d142009-12-01 22:39:41 +01001513 result = asus_input_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001514 if (result)
1515 goto fail_input;
1516
Corentin Chary9129d142009-12-01 22:39:41 +01001517 result = asus_led_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001518 if (result)
1519 goto fail_led;
1520
Corentin Chary18e13112010-01-25 23:29:24 +01001521 result = asus_rfkill_init(asus);
1522 if (result)
1523 goto fail_rfkill;
1524
Corentin Chary600ad522009-11-30 21:42:42 +01001525 asus_device_present = true;
1526 return 0;
1527
Corentin Chary18e13112010-01-25 23:29:24 +01001528fail_rfkill:
1529 asus_led_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001530fail_led:
Corentin Chary9129d142009-12-01 22:39:41 +01001531 asus_input_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001532fail_input:
Corentin Chary9129d142009-12-01 22:39:41 +01001533 asus_backlight_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001534fail_backlight:
Corentin Chary9129d142009-12-01 22:39:41 +01001535 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001536fail_platform:
Corentin Chary50a90c42009-11-30 21:55:12 +01001537 kfree(asus->name);
1538 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001539
1540 return result;
1541}
1542
1543static int asus_acpi_remove(struct acpi_device *device, int type)
1544{
Corentin Chary9129d142009-12-01 22:39:41 +01001545 struct asus_laptop *asus = acpi_driver_data(device);
1546
1547 asus_backlight_exit(asus);
Corentin Chary18e13112010-01-25 23:29:24 +01001548 asus_rfkill_exit(asus);
Corentin Chary9129d142009-12-01 22:39:41 +01001549 asus_led_exit(asus);
1550 asus_input_exit(asus);
1551 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001552
Corentin Chary50a90c42009-11-30 21:55:12 +01001553 kfree(asus->name);
1554 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001555 return 0;
1556}
1557
1558static const struct acpi_device_id asus_device_ids[] = {
1559 {"ATK0100", 0},
1560 {"ATK0101", 0},
1561 {"", 0},
1562};
1563MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1564
1565static struct acpi_driver asus_acpi_driver = {
Corentin Chary50a90c42009-11-30 21:55:12 +01001566 .name = ASUS_LAPTOP_NAME,
1567 .class = ASUS_LAPTOP_CLASS,
Corentin Chary600ad522009-11-30 21:42:42 +01001568 .owner = THIS_MODULE,
1569 .ids = asus_device_ids,
1570 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1571 .ops = {
1572 .add = asus_acpi_add,
1573 .remove = asus_acpi_remove,
1574 .notify = asus_acpi_notify,
1575 },
1576};
1577
Corentin Chary85091b72007-01-26 14:04:30 +01001578static int __init asus_laptop_init(void)
1579{
1580 int result;
1581
Corentin Chary600ad522009-11-30 21:42:42 +01001582 result = platform_driver_register(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001583 if (result < 0)
1584 return result;
1585
Corentin Chary600ad522009-11-30 21:42:42 +01001586 result = acpi_bus_register_driver(&asus_acpi_driver);
1587 if (result < 0)
1588 goto fail_acpi_driver;
1589 if (!asus_device_present) {
1590 result = -ENODEV;
1591 goto fail_no_device;
Corentin Chary85091b72007-01-26 14:04:30 +01001592 }
Len Brown8def05f2007-01-30 01:46:43 -05001593 return 0;
Corentin Chary85091b72007-01-26 14:04:30 +01001594
Corentin Chary600ad522009-11-30 21:42:42 +01001595fail_no_device:
1596 acpi_bus_unregister_driver(&asus_acpi_driver);
1597fail_acpi_driver:
1598 platform_driver_unregister(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001599 return result;
1600}
1601
Corentin Chary600ad522009-11-30 21:42:42 +01001602static void __exit asus_laptop_exit(void)
1603{
1604 acpi_bus_unregister_driver(&asus_acpi_driver);
1605 platform_driver_unregister(&platform_driver);
1606}
1607
Corentin Chary85091b72007-01-26 14:04:30 +01001608module_init(asus_laptop_init);
1609module_exit(asus_laptop_exit);