blob: ae5302646c4ebc9f8ede41b4c7b1013b0c38a7a8 [file] [log] [blame]
Corentin Chary85091b72007-01-26 14:04:30 +01001/*
2 * asus-laptop.c - Asus Laptop Support
3 *
4 *
5 * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
Corentin Chary8ec555c2007-03-11 10:28:03 +01006 * Copyright (C) 2006-2007 Corentin Chary
Corentin Chary85091b72007-01-26 14:04:30 +01007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 *
23 * The development page for this driver is located at
24 * http://sourceforge.net/projects/acpi4asus/
25 *
26 * Credits:
27 * Pontus Fuchs - Helper functions, cleanup
28 * Johann Wiesner - Small compile fixes
29 * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
30 * Eric Burghard - LED display support for W1N
31 * Josh Green - Light Sens support
32 * Thomas Tuttle - His first patch for led support was very helpfull
Corentin Charye539c2f2007-05-06 14:47:06 +020033 * Sam Lin - GPS support
Corentin Chary85091b72007-01-26 14:04:30 +010034 */
35
Corentin Chary2fcc23d2009-06-19 14:52:03 +020036#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37
Corentin Chary85091b72007-01-26 14:04:30 +010038#include <linux/kernel.h>
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/types.h>
42#include <linux/err.h>
43#include <linux/proc_fs.h>
Corentin Chary6b7091e2007-01-26 14:04:45 +010044#include <linux/backlight.h>
45#include <linux/fb.h>
Corentin Charybe18cda2007-01-26 14:04:35 +010046#include <linux/leds.h>
Corentin Chary85091b72007-01-26 14:04:30 +010047#include <linux/platform_device.h>
48#include <acpi/acpi_drivers.h>
49#include <acpi/acpi_bus.h>
50#include <asm/uaccess.h>
Corentin Chary034ce902009-01-20 16:17:43 +010051#include <linux/input.h>
Corentin Chary66a71dd2010-01-25 22:50:11 +010052#include <linux/input/sparse-keymap.h>
Corentin Chary18e13112010-01-25 23:29:24 +010053#include <linux/rfkill.h>
Corentin Chary85091b72007-01-26 14:04:30 +010054
Corentin Chary91687cc2009-11-28 10:32:34 +010055#define ASUS_LAPTOP_VERSION "0.42"
Corentin Chary85091b72007-01-26 14:04:30 +010056
Corentin Chary50a90c42009-11-30 21:55:12 +010057#define ASUS_LAPTOP_NAME "Asus Laptop Support"
58#define ASUS_LAPTOP_CLASS "hotkey"
59#define ASUS_LAPTOP_DEVICE_NAME "Hotkey"
60#define ASUS_LAPTOP_FILE KBUILD_MODNAME
61#define ASUS_LAPTOP_PREFIX "\\_SB.ATKD."
Corentin Chary85091b72007-01-26 14:04:30 +010062
Corentin Chary85091b72007-01-26 14:04:30 +010063MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
Corentin Chary50a90c42009-11-30 21:55:12 +010064MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
Corentin Chary85091b72007-01-26 14:04:30 +010065MODULE_LICENSE("GPL");
66
Corentin Charybe966662009-08-29 10:28:29 +020067/*
68 * WAPF defines the behavior of the Fn+Fx wlan key
Corentin Chary185e5af2007-03-11 10:27:33 +010069 * The significance of values is yet to be found, but
70 * most of the time:
71 * 0x0 will do nothing
72 * 0x1 will allow to control the device with Fn+Fx key.
73 * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
74 * 0x5 like 0x1 or 0x4
75 * So, if something doesn't work as you want, just try other values =)
76 */
77static uint wapf = 1;
78module_param(wapf, uint, 0644);
79MODULE_PARM_DESC(wapf, "WAPF value");
80
Corentin Charyd0930a22010-01-17 17:21:13 +010081static uint wlan_status = 1;
Corentin Chary0e875f42010-01-10 20:49:26 +010082static uint bluetooth_status = 1;
83
Corentin Charyd0930a22010-01-17 17:21:13 +010084module_param(wlan_status, uint, 0644);
85MODULE_PARM_DESC(wlan_status, "Set the wireless status on boot "
Corentin Chary0e875f42010-01-10 20:49:26 +010086 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
87 "default is 1");
88
89module_param(bluetooth_status, uint, 0644);
90MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
91 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
92 "default is 1");
93
Corentin Charybe4ee822009-12-06 16:27:09 +010094/*
95 * Some events we use, same for all Asus
96 */
Corentin Charya539df52010-01-25 22:53:21 +010097#define ATKD_BR_UP 0x10 // (event & ~ATKD_BR_UP) = brightness level
98#define ATKD_BR_DOWN 0x20 // (event & ~ATKD_BR_DOWN) = britghness level
99#define ATKD_BR_MIN ATKD_BR_UP
100#define ATKD_BR_MAX (ATKD_BR_DOWN | 0xF) // 0x2f
Corentin Charybe4ee822009-12-06 16:27:09 +0100101#define ATKD_LCD_ON 0x33
102#define ATKD_LCD_OFF 0x34
103
104/*
105 * Known bits returned by \_SB.ATKD.HWRS
106 */
107#define WL_HWRS 0x80
108#define BT_HWRS 0x100
109
110/*
111 * Flags for hotk status
112 * WL_ON and BT_ON are also used for wireless_status()
113 */
Corentin Chary17e78f62010-01-13 22:21:33 +0100114#define WL_RSTS 0x01 /* internal Wifi */
115#define BT_RSTS 0x02 /* internal Bluetooth */
Corentin Charybe4ee822009-12-06 16:27:09 +0100116
Corentin Chary85091b72007-01-26 14:04:30 +0100117#define ASUS_HANDLE(object, paths...) \
118 static acpi_handle object##_handle = NULL; \
119 static char *object##_paths[] = { paths }
120
Corentin Charybe18cda2007-01-26 14:04:35 +0100121/* LED */
Corentin Charyd99b5772010-01-22 21:20:57 +0100122#define METHOD_MLED "MLED"
123#define METHOD_TLED "TLED"
124#define METHOD_RLED "RLED" /* W1JC */
125#define METHOD_PLED "PLED" /* A7J */
126#define METHOD_GLED "GLED" /* G1, G2 (probably) */
Corentin Charybe18cda2007-01-26 14:04:35 +0100127
Corentin Chary722ad972007-01-26 14:04:55 +0100128/* LEDD */
Corentin Charyd99b5772010-01-22 21:20:57 +0100129#define METHOD_LEDD "SLCM"
Corentin Chary722ad972007-01-26 14:04:55 +0100130
Corentin Charybe966662009-08-29 10:28:29 +0200131/*
132 * Bluetooth and WLAN
Corentin Chary4564de12007-01-26 14:04:40 +0100133 * WLED and BLED are not handled like other XLED, because in some dsdt
134 * they also control the WLAN/Bluetooth device.
135 */
Corentin Charyd99b5772010-01-22 21:20:57 +0100136#define METHOD_WLAN "WLED"
137#define METHOD_BLUETOOTH "BLED"
138#define METHOD_WL_STATUS "RSTS"
Corentin Chary4564de12007-01-26 14:04:40 +0100139
Corentin Chary6b7091e2007-01-26 14:04:45 +0100140/* Brightness */
Corentin Charyd99b5772010-01-22 21:20:57 +0100141#define METHOD_BRIGHTNESS_SET "SPLV"
142#define METHOD_BRIGHTNESS_GET "GPLV"
Corentin Chary6b7091e2007-01-26 14:04:45 +0100143
144/* Backlight */
Len Brown8def05f2007-01-30 01:46:43 -0500145ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
146 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
147 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
148 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
149 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
Torsten Krah4d0b856e2008-10-17 09:47:57 +0200150 "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
Len Brown8def05f2007-01-30 01:46:43 -0500151 "\\_SB.PCI0.PX40.Q10", /* S1x */
152 "\\Q10"); /* A2x, L2D, L3D, M2E */
Corentin Chary6b7091e2007-01-26 14:04:45 +0100153
Corentin Chary78127b42007-01-26 14:04:49 +0100154/* Display */
Corentin Charyd99b5772010-01-22 21:20:57 +0100155#define METHOD_SWITCH_DISPLAY "SDSP"
Corentin Charybe966662009-08-29 10:28:29 +0200156ASUS_HANDLE(display_get,
157 /* A6B, A6K A6R A7D F3JM L4R M6R A3G M6A M6V VX-1 V6J V6V W3Z */
158 "\\_SB.PCI0.P0P1.VGA.GETD",
159 /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V S5A M5A z33A W1Jc W2V G1 */
160 "\\_SB.PCI0.P0P2.VGA.GETD",
161 /* A6V A6Q */
162 "\\_SB.PCI0.P0P3.VGA.GETD",
163 /* A6T, A6M */
164 "\\_SB.PCI0.P0PA.VGA.GETD",
165 /* L3C */
166 "\\_SB.PCI0.PCI1.VGAC.NMAP",
167 /* Z96F */
168 "\\_SB.PCI0.VGA.GETD",
169 /* A2D */
170 "\\ACTD",
171 /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
172 "\\ADVG",
173 /* P30 */
174 "\\DNXT",
175 /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
176 "\\INFB",
177 /* A3F A6F A3N A3L M6N W3N W6A */
178 "\\SSTE");
Corentin Chary78127b42007-01-26 14:04:49 +0100179
Corentin Charyd99b5772010-01-22 21:20:57 +0100180#define METHOD_ALS_CONTROL "ALSC" /* Z71A Z71V */
181#define METHOD_ALS_LEVEL "ALSL" /* Z71A Z71V */
Corentin Chary8b857352007-01-26 14:04:58 +0100182
Corentin Charye539c2f2007-05-06 14:47:06 +0200183/* GPS */
184/* R2H use different handle for GPS on/off */
Corentin Charyd99b5772010-01-22 21:20:57 +0100185#define METHOD_GPS_ON "SDON"
186#define METHOD_GPS_OFF "SDOF"
187#define METHOD_GPS_STATUS "GPST"
Corentin Charye539c2f2007-05-06 14:47:06 +0200188
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000189/* Keyboard light */
Corentin Charyd99b5772010-01-22 21:20:57 +0100190#define METHOD_KBD_LIGHT_SET "SLKB"
191#define METHOD_KBD_LIGHT_GET "GLKB"
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000192
Corentin Chary85091b72007-01-26 14:04:30 +0100193/*
Corentin Chary9129d142009-12-01 22:39:41 +0100194 * Define a specific led structure to keep the main structure clean
195 */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100196struct asus_led {
197 int wk;
198 struct work_struct work;
199 struct led_classdev led;
200 struct asus_laptop *asus;
201 const char *method;
Corentin Chary9129d142009-12-01 22:39:41 +0100202};
203
204/*
Corentin Chary85091b72007-01-26 14:04:30 +0100205 * This is the main structure, we can use it to store anything interesting
206 * about the hotk device
207 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100208struct asus_laptop {
Corentin Charybe966662009-08-29 10:28:29 +0200209 char *name; /* laptop name */
Corentin Chary600ad522009-11-30 21:42:42 +0100210
Corentin Chary7c247642009-11-30 22:13:54 +0100211 struct acpi_table_header *dsdt_info;
Corentin Chary600ad522009-11-30 21:42:42 +0100212 struct platform_device *platform_device;
Corentin Chary7c247642009-11-30 22:13:54 +0100213 struct acpi_device *device; /* the device we are in */
214 struct backlight_device *backlight_device;
Corentin Chary9129d142009-12-01 22:39:41 +0100215
Corentin Chary7c247642009-11-30 22:13:54 +0100216 struct input_dev *inputdev;
Corentin Chary9129d142009-12-01 22:39:41 +0100217 struct key_entry *keymap;
218
Corentin Charyaee0afb2010-01-26 21:01:34 +0100219 struct asus_led mled;
220 struct asus_led tled;
221 struct asus_led rled;
222 struct asus_led pled;
223 struct asus_led gled;
224 struct asus_led kled;
225 struct workqueue_struct *led_workqueue;
Corentin Chary7c247642009-11-30 22:13:54 +0100226
Corentin Charyaa9df932010-01-13 21:49:10 +0100227 int wireless_status;
228 bool have_rsts;
Corentin Chary3e68ae72010-01-13 22:10:39 +0100229 int lcd_state;
Corentin Charyaa9df932010-01-13 21:49:10 +0100230
Corentin Chary18e13112010-01-25 23:29:24 +0100231 struct rfkill *gps_rfkill;
232
Corentin Charybe966662009-08-29 10:28:29 +0200233 acpi_handle handle; /* the handle of the hotk device */
Corentin Charybe966662009-08-29 10:28:29 +0200234 u32 ledd_status; /* status of the LED display */
235 u8 light_level; /* light sensor level */
236 u8 light_switch; /* light sensor switch value */
237 u16 event_count[128]; /* count for each event TODO make this better */
Corentin Chary034ce902009-01-20 16:17:43 +0100238 u16 *keycode_map;
Corentin Chary85091b72007-01-26 14:04:30 +0100239};
240
Corentin Chary9129d142009-12-01 22:39:41 +0100241static const struct key_entry asus_keymap[] = {
Corentin Charya539df52010-01-25 22:53:21 +0100242 /* Lenovo SL Specific keycodes */
Corentin Chary66a71dd2010-01-25 22:50:11 +0100243 {KE_KEY, 0x02, { KEY_SCREENLOCK } },
244 {KE_KEY, 0x05, { KEY_WLAN } },
245 {KE_KEY, 0x08, { KEY_F13 } },
246 {KE_KEY, 0x17, { KEY_ZOOM } },
247 {KE_KEY, 0x1f, { KEY_BATTERY } },
Corentin Charya539df52010-01-25 22:53:21 +0100248 /* End of Lenovo SL Specific keycodes */
Corentin Chary66a71dd2010-01-25 22:50:11 +0100249 {KE_KEY, 0x30, { KEY_VOLUMEUP } },
250 {KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
251 {KE_KEY, 0x32, { KEY_MUTE } },
252 {KE_KEY, 0x33, { KEY_SWITCHVIDEOMODE } },
253 {KE_KEY, 0x34, { KEY_SWITCHVIDEOMODE } },
254 {KE_KEY, 0x40, { KEY_PREVIOUSSONG } },
255 {KE_KEY, 0x41, { KEY_NEXTSONG } },
256 {KE_KEY, 0x43, { KEY_STOPCD } },
257 {KE_KEY, 0x45, { KEY_PLAYPAUSE } },
258 {KE_KEY, 0x4c, { KEY_MEDIA } },
259 {KE_KEY, 0x50, { KEY_EMAIL } },
260 {KE_KEY, 0x51, { KEY_WWW } },
261 {KE_KEY, 0x55, { KEY_CALC } },
262 {KE_KEY, 0x5C, { KEY_SCREENLOCK } }, /* Screenlock */
263 {KE_KEY, 0x5D, { KEY_WLAN } },
264 {KE_KEY, 0x5E, { KEY_WLAN } },
265 {KE_KEY, 0x5F, { KEY_WLAN } },
266 {KE_KEY, 0x60, { KEY_SWITCHVIDEOMODE } },
267 {KE_KEY, 0x61, { KEY_SWITCHVIDEOMODE } },
268 {KE_KEY, 0x62, { KEY_SWITCHVIDEOMODE } },
269 {KE_KEY, 0x63, { KEY_SWITCHVIDEOMODE } },
270 {KE_KEY, 0x6B, { KEY_F13 } }, /* Lock Touchpad */
Corentin Chary7f607d72010-01-17 17:37:19 +0100271 {KE_KEY, 0x7E, { KEY_BLUETOOTH } },
272 {KE_KEY, 0x7D, { KEY_BLUETOOTH } },
Corentin Chary66a71dd2010-01-25 22:50:11 +0100273 {KE_KEY, 0x82, { KEY_CAMERA } },
274 {KE_KEY, 0x88, { KEY_WLAN } },
275 {KE_KEY, 0x8A, { KEY_PROG1 } },
276 {KE_KEY, 0x95, { KEY_MEDIA } },
277 {KE_KEY, 0x99, { KEY_PHONE } },
278 {KE_KEY, 0xc4, { KEY_KBDILLUMUP } },
279 {KE_KEY, 0xc5, { KEY_KBDILLUMDOWN } },
Corentin Chary034ce902009-01-20 16:17:43 +0100280 {KE_END, 0},
281};
282
Corentin Chary66a71dd2010-01-25 22:50:11 +0100283
Corentin Chary85091b72007-01-26 14:04:30 +0100284/*
285 * This function evaluates an ACPI method, given an int as parameter, the
286 * method is searched within the scope of the handle, can be NULL. The output
287 * of the method is written is output, which can also be NULL
288 *
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100289 * returns 0 if write is successful, -1 else.
Corentin Chary85091b72007-01-26 14:04:30 +0100290 */
Corentin Charyd8c67322009-11-28 10:27:51 +0100291static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
292 struct acpi_buffer *output)
Corentin Chary85091b72007-01-26 14:04:30 +0100293{
Corentin Charybe966662009-08-29 10:28:29 +0200294 struct acpi_object_list params; /* list of input parameters (an int) */
295 union acpi_object in_obj; /* the only param we use */
Corentin Chary85091b72007-01-26 14:04:30 +0100296 acpi_status status;
297
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100298 if (!handle)
299 return 0;
300
Corentin Chary85091b72007-01-26 14:04:30 +0100301 params.count = 1;
302 params.pointer = &in_obj;
303 in_obj.type = ACPI_TYPE_INTEGER;
304 in_obj.integer.value = val;
305
306 status = acpi_evaluate_object(handle, (char *)method, &params, output);
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100307 if (status == AE_OK)
308 return 0;
309 else
310 return -1;
Corentin Chary85091b72007-01-26 14:04:30 +0100311}
312
Corentin Charyd8c67322009-11-28 10:27:51 +0100313static int write_acpi_int(acpi_handle handle, const char *method, int val)
314{
315 return write_acpi_int_ret(handle, method, val, NULL);
316}
317
Corentin Charyd99b5772010-01-22 21:20:57 +0100318static int acpi_check_handle(acpi_handle handle, const char *method,
319 acpi_handle *ret)
320{
321 acpi_status status;
322
323 if (method == NULL)
324 return -ENODEV;
325
326 if (ret)
327 status = acpi_get_handle(handle, (char *)method,
328 ret);
329 else {
330 acpi_handle dummy;
331
332 status = acpi_get_handle(handle, (char *)method,
333 &dummy);
334 }
335
336 if (status != AE_OK) {
337 if (ret)
338 pr_warning("Error finding %s\n", method);
339 return -ENODEV;
340 }
341 return 0;
342}
343
Corentin Chary17e78f62010-01-13 22:21:33 +0100344/* Generic LED function */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100345static int asus_led_set(struct asus_laptop *asus, const char *method,
Corentin Chary17e78f62010-01-13 22:21:33 +0100346 int value)
Corentin Charybe18cda2007-01-26 14:04:35 +0100347{
Corentin Charyd99b5772010-01-22 21:20:57 +0100348 if (!strcmp(method, METHOD_MLED))
Corentin Chary17e78f62010-01-13 22:21:33 +0100349 value = !value;
Corentin Charyd99b5772010-01-22 21:20:57 +0100350 else if (!strcmp(method, METHOD_GLED))
Corentin Chary17e78f62010-01-13 22:21:33 +0100351 value = !value + 1;
352 else
353 value = !!value;
Corentin Charybe18cda2007-01-26 14:04:35 +0100354
Corentin Charye5593bf2010-01-17 17:20:11 +0100355 return write_acpi_int(asus->handle, method, value);
Corentin Charybe18cda2007-01-26 14:04:35 +0100356}
357
Corentin Charybe4ee822009-12-06 16:27:09 +0100358/*
359 * LEDs
360 */
Corentin Charybe18cda2007-01-26 14:04:35 +0100361/* /sys/class/led handlers */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100362static void asus_led_cdev_set(struct led_classdev *led_cdev,
363 enum led_brightness value)
364{
365 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
366 struct asus_laptop *asus = led->asus;
Corentin Charybe18cda2007-01-26 14:04:35 +0100367
Corentin Charyaee0afb2010-01-26 21:01:34 +0100368 led->wk = !!value;
369 queue_work(asus->led_workqueue, &led->work);
370}
371
372static void asus_led_cdev_update(struct work_struct *work)
373{
374 struct asus_led *led = container_of(work, struct asus_led, work);
375 struct asus_laptop *asus = led->asus;
376
377 asus_led_set(asus, led->method, led->wk);
378}
379
380static enum led_brightness asus_led_cdev_get(struct led_classdev *led_cdev)
381{
382 return led_cdev->brightness;
383}
Corentin Charybe18cda2007-01-26 14:04:35 +0100384
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000385/*
Corentin Charybe4ee822009-12-06 16:27:09 +0100386 * Keyboard backlight (also a LED)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000387 */
Corentin Charyd99b5772010-01-22 21:20:57 +0100388static int asus_kled_lvl(struct asus_laptop *asus)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000389{
390 unsigned long long kblv;
391 struct acpi_object_list params;
392 union acpi_object in_obj;
393 acpi_status rv;
394
395 params.count = 1;
396 params.pointer = &in_obj;
397 in_obj.type = ACPI_TYPE_INTEGER;
398 in_obj.integer.value = 2;
399
Corentin Charyd99b5772010-01-22 21:20:57 +0100400 rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
401 &params, &kblv);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000402 if (ACPI_FAILURE(rv)) {
403 pr_warning("Error reading kled level\n");
Corentin Chary4d441512010-01-13 22:26:24 +0100404 return -ENODEV;
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000405 }
406 return kblv;
407}
408
Corentin Chary4d441512010-01-13 22:26:24 +0100409static int asus_kled_set(struct asus_laptop *asus, int kblv)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000410{
411 if (kblv > 0)
412 kblv = (1 << 7) | (kblv & 0x7F);
413 else
414 kblv = 0;
415
Corentin Charyd99b5772010-01-22 21:20:57 +0100416 if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000417 pr_warning("Keyboard LED display write failed\n");
418 return -EINVAL;
419 }
420 return 0;
421}
422
Corentin Charyaee0afb2010-01-26 21:01:34 +0100423static void asus_kled_cdev_set(struct led_classdev *led_cdev,
424 enum led_brightness value)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000425{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100426 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
427 struct asus_laptop *asus = led->asus;
Corentin Chary9129d142009-12-01 22:39:41 +0100428
Corentin Charyaee0afb2010-01-26 21:01:34 +0100429 led->wk = value;
430 queue_work(asus->led_workqueue, &led->work);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000431}
432
Corentin Charyaee0afb2010-01-26 21:01:34 +0100433static void asus_kled_cdev_update(struct work_struct *work)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000434{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100435 struct asus_led *led = container_of(work, struct asus_led, work);
436 struct asus_laptop *asus = led->asus;
Corentin Chary9129d142009-12-01 22:39:41 +0100437
Corentin Charyaee0afb2010-01-26 21:01:34 +0100438 asus_kled_set(asus, led->wk);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000439}
440
Corentin Charyaee0afb2010-01-26 21:01:34 +0100441static enum led_brightness asus_kled_cdev_get(struct led_classdev *led_cdev)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000442{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100443 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
444 struct asus_laptop *asus = led->asus;
Corentin Charyd99b5772010-01-22 21:20:57 +0100445
446 return asus_kled_lvl(asus);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000447}
448
Corentin Charybe4ee822009-12-06 16:27:09 +0100449static void asus_led_exit(struct asus_laptop *asus)
450{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100451 if (asus->mled.led.dev)
452 led_classdev_unregister(&asus->mled.led);
453 if (asus->tled.led.dev)
454 led_classdev_unregister(&asus->tled.led);
455 if (asus->pled.led.dev)
456 led_classdev_unregister(&asus->pled.led);
457 if (asus->rled.led.dev)
458 led_classdev_unregister(&asus->rled.led);
459 if (asus->gled.led.dev)
460 led_classdev_unregister(&asus->gled.led);
461 if (asus->kled.led.dev)
462 led_classdev_unregister(&asus->kled.led);
463 if (asus->led_workqueue) {
464 destroy_workqueue(asus->led_workqueue);
465 asus->led_workqueue = NULL;
Corentin Charybe4ee822009-12-06 16:27:09 +0100466 }
467}
468
469/* Ugly macro, need to fix that later */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100470static int asus_led_register(struct asus_laptop *asus,
471 struct asus_led *led,
472 const char *name, const char *method)
473{
474 struct led_classdev *led_cdev = &led->led;
475
476 if (!method || acpi_check_handle(asus->handle, method, NULL))
477 return 0; /* Led not present */
478
479 led->asus = asus;
480 led->method = method;
481
482 INIT_WORK(&led->work, asus_led_cdev_update);
483 led_cdev->name = name;
484 led_cdev->brightness_set = asus_led_cdev_set;
485 led_cdev->brightness_get = asus_led_cdev_get;
486 led_cdev->max_brightness = 1;
487 return led_classdev_register(&asus->platform_device->dev, led_cdev);
488}
Corentin Charybe4ee822009-12-06 16:27:09 +0100489
490static int asus_led_init(struct asus_laptop *asus)
491{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100492 int r;
Corentin Charybe4ee822009-12-06 16:27:09 +0100493
494 /*
495 * Functions that actually update the LED's are called from a
496 * workqueue. By doing this as separate work rather than when the LED
497 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
498 * potentially bad time, such as a timer interrupt.
499 */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100500 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
501 if (!asus->led_workqueue)
Corentin Charybe4ee822009-12-06 16:27:09 +0100502 return -ENOMEM;
503
Corentin Charyaee0afb2010-01-26 21:01:34 +0100504 r = asus_led_register(asus, &asus->mled, "asus::mail", METHOD_MLED);
505 if (r)
506 goto error;
507 r = asus_led_register(asus, &asus->tled, "asus::touchpad", METHOD_TLED);
508 if (r)
509 goto error;
510 r = asus_led_register(asus, &asus->rled, "asus::record", METHOD_RLED);
511 if (r)
512 goto error;
513 r = asus_led_register(asus, &asus->pled, "asus::phone", METHOD_PLED);
514 if (r)
515 goto error;
516 r = asus_led_register(asus, &asus->gled, "asus::gaming", METHOD_GLED);
517 if (r)
518 goto error;
Corentin Charyd99b5772010-01-22 21:20:57 +0100519 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
Corentin Charyaee0afb2010-01-26 21:01:34 +0100520 !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL)) {
521 struct asus_led *led = &asus->kled;
522 struct led_classdev *cdev = &led->led;
523
524 led->asus = asus;
525
526 INIT_WORK(&led->work, asus_kled_cdev_update);
527 cdev->name = "asus::kbd_backlight";
528 cdev->brightness_set = asus_kled_cdev_set;
529 cdev->brightness_get = asus_kled_cdev_get;
530 cdev->max_brightness = 3;
531 r = led_classdev_register(&asus->platform_device->dev, cdev);
532 }
Corentin Charybe4ee822009-12-06 16:27:09 +0100533error:
Corentin Charyaee0afb2010-01-26 21:01:34 +0100534 if (r)
Corentin Charybe4ee822009-12-06 16:27:09 +0100535 asus_led_exit(asus);
Corentin Charyaee0afb2010-01-26 21:01:34 +0100536 return r;
Corentin Charybe4ee822009-12-06 16:27:09 +0100537}
538
539/*
540 * Backlight device
541 */
Corentin Chary3e68ae72010-01-13 22:10:39 +0100542static int asus_lcd_status(struct asus_laptop *asus)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100543{
Corentin Chary3e68ae72010-01-13 22:10:39 +0100544 return asus->lcd_state;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100545}
546
Corentin Chary3e68ae72010-01-13 22:10:39 +0100547static int asus_lcd_set(struct asus_laptop *asus, int value)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100548{
549 int lcd = 0;
550 acpi_status status = 0;
551
Corentin Chary3e68ae72010-01-13 22:10:39 +0100552 lcd = !!value;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100553
Corentin Chary3e68ae72010-01-13 22:10:39 +0100554 if (lcd == asus_lcd_status(asus))
Corentin Chary6b7091e2007-01-26 14:04:45 +0100555 return 0;
556
Corentin Chary3e68ae72010-01-13 22:10:39 +0100557 if (!lcd_switch_handle)
558 return -ENODEV;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100559
Corentin Chary3e68ae72010-01-13 22:10:39 +0100560 status = acpi_evaluate_object(lcd_switch_handle,
561 NULL, NULL, NULL);
562
563 if (ACPI_FAILURE(status)) {
564 pr_warning("Error switching LCD\n");
565 return -ENODEV;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100566 }
567
Corentin Chary3e68ae72010-01-13 22:10:39 +0100568 asus->lcd_state = lcd;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100569 return 0;
570}
571
Corentin Chary9129d142009-12-01 22:39:41 +0100572static void lcd_blank(struct asus_laptop *asus, int blank)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100573{
Corentin Chary7c247642009-11-30 22:13:54 +0100574 struct backlight_device *bd = asus->backlight_device;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100575
Corentin Chary3e68ae72010-01-13 22:10:39 +0100576 asus->lcd_state = (blank == FB_BLANK_UNBLANK);
577
Len Brown8def05f2007-01-30 01:46:43 -0500578 if (bd) {
Richard Purdie599a52d2007-02-10 23:07:48 +0000579 bd->props.power = blank;
Richard Purdie28ee0862007-02-08 22:25:09 +0000580 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100581 }
582}
583
Corentin Chary4d441512010-01-13 22:26:24 +0100584static int asus_read_brightness(struct backlight_device *bd)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100585{
Corentin Charyd99b5772010-01-22 21:20:57 +0100586 struct asus_laptop *asus = bl_get_data(bd);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400587 unsigned long long value;
Corentin Chary9a816852007-03-11 10:25:38 +0100588 acpi_status rv = AE_OK;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100589
Corentin Charyd99b5772010-01-22 21:20:57 +0100590 rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
591 NULL, &value);
Corentin Chary9a816852007-03-11 10:25:38 +0100592 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200593 pr_warning("Error reading brightness\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100594
595 return value;
596}
597
Corentin Chary4d441512010-01-13 22:26:24 +0100598static int asus_set_brightness(struct backlight_device *bd, int value)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100599{
Corentin Charyd99b5772010-01-22 21:20:57 +0100600 struct asus_laptop *asus = bl_get_data(bd);
601
602 if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200603 pr_warning("Error changing brightness\n");
Corentin Charye5b50f62009-11-28 10:19:55 +0100604 return -EIO;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100605 }
Corentin Charye5b50f62009-11-28 10:19:55 +0100606 return 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100607}
608
609static int update_bl_status(struct backlight_device *bd)
610{
Corentin Chary9129d142009-12-01 22:39:41 +0100611 struct asus_laptop *asus = bl_get_data(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100612 int rv;
Richard Purdie599a52d2007-02-10 23:07:48 +0000613 int value = bd->props.brightness;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100614
Corentin Chary4d441512010-01-13 22:26:24 +0100615 rv = asus_set_brightness(bd, value);
Len Brown8def05f2007-01-30 01:46:43 -0500616 if (rv)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100617 return rv;
618
Richard Purdie599a52d2007-02-10 23:07:48 +0000619 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
Corentin Chary3e68ae72010-01-13 22:10:39 +0100620 return asus_lcd_set(asus, value);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100621}
622
Corentin Charybe4ee822009-12-06 16:27:09 +0100623static struct backlight_ops asusbl_ops = {
Corentin Chary4d441512010-01-13 22:26:24 +0100624 .get_brightness = asus_read_brightness,
Corentin Charybe4ee822009-12-06 16:27:09 +0100625 .update_status = update_bl_status,
626};
627
Corentin Charya539df52010-01-25 22:53:21 +0100628static int asus_backlight_notify(struct asus_laptop *asus)
629{
630 struct backlight_device *bd = asus->backlight_device;
631 int old = bd->props.brightness;
632
633 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
634
635 return old;
636}
637
Corentin Charybe4ee822009-12-06 16:27:09 +0100638static int asus_backlight_init(struct asus_laptop *asus)
639{
640 struct backlight_device *bd;
641 struct device *dev = &asus->platform_device->dev;
642
Corentin Charyd99b5772010-01-22 21:20:57 +0100643 if (!acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) &&
644 !acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL) &&
645 lcd_switch_handle) {
Corentin Charybe4ee822009-12-06 16:27:09 +0100646 bd = backlight_device_register(ASUS_LAPTOP_FILE, dev,
647 asus, &asusbl_ops);
648 if (IS_ERR(bd)) {
649 pr_err("Could not register asus backlight device\n");
650 asus->backlight_device = NULL;
651 return PTR_ERR(bd);
652 }
653
654 asus->backlight_device = bd;
655
656 bd->props.max_brightness = 15;
Corentin Charybe4ee822009-12-06 16:27:09 +0100657 bd->props.power = FB_BLANK_UNBLANK;
Corentin Charyd99b5772010-01-22 21:20:57 +0100658 bd->props.brightness = asus_read_brightness(bd);
Corentin Charybe4ee822009-12-06 16:27:09 +0100659 backlight_update_status(bd);
660 }
661 return 0;
662}
663
664static void asus_backlight_exit(struct asus_laptop *asus)
665{
666 if (asus->backlight_device)
667 backlight_device_unregister(asus->backlight_device);
Corentin Charya539df52010-01-25 22:53:21 +0100668 asus->backlight_device = NULL;
Corentin Charybe4ee822009-12-06 16:27:09 +0100669}
670
Corentin Chary85091b72007-01-26 14:04:30 +0100671/*
672 * Platform device handlers
673 */
674
675/*
676 * We write our info in page, we begin at offset off and cannot write more
677 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
678 * number of bytes written in page
679 */
680static ssize_t show_infos(struct device *dev,
Len Brown8def05f2007-01-30 01:46:43 -0500681 struct device_attribute *attr, char *page)
Corentin Chary85091b72007-01-26 14:04:30 +0100682{
Corentin Chary9129d142009-12-01 22:39:41 +0100683 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary85091b72007-01-26 14:04:30 +0100684 int len = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400685 unsigned long long temp;
Corentin Charybe966662009-08-29 10:28:29 +0200686 char buf[16]; /* enough for all info */
Corentin Chary9a816852007-03-11 10:25:38 +0100687 acpi_status rv = AE_OK;
688
Corentin Chary85091b72007-01-26 14:04:30 +0100689 /*
690 * We use the easy way, we don't care of off and count, so we don't set eof
691 * to 1
692 */
693
Corentin Chary50a90c42009-11-30 21:55:12 +0100694 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
695 len += sprintf(page + len, "Model reference : %s\n", asus->name);
Corentin Chary85091b72007-01-26 14:04:30 +0100696 /*
697 * The SFUN method probably allows the original driver to get the list
698 * of features supported by a given model. For now, 0x0100 or 0x0800
699 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
700 * The significance of others is yet to be found.
701 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100702 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100703 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000704 len += sprintf(page + len, "SFUN value : %#x\n",
705 (uint) temp);
706 /*
707 * The HWRS method return informations about the hardware.
708 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
709 * The significance of others is yet to be found.
710 * If we don't find the method, we assume the device are present.
711 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100712 rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
Corentin Chary1d4a3802009-08-28 12:56:46 +0000713 if (!ACPI_FAILURE(rv))
714 len += sprintf(page + len, "HRWS value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100715 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100716 /*
717 * Another value for userspace: the ASYM method returns 0x02 for
718 * battery low and 0x04 for battery critical, its readings tend to be
719 * more accurate than those provided by _BST.
720 * Note: since not all the laptops provide this method, errors are
721 * silently ignored.
722 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100723 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100724 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000725 len += sprintf(page + len, "ASYM value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100726 (uint) temp);
Corentin Chary7c247642009-11-30 22:13:54 +0100727 if (asus->dsdt_info) {
728 snprintf(buf, 16, "%d", asus->dsdt_info->length);
Corentin Chary85091b72007-01-26 14:04:30 +0100729 len += sprintf(page + len, "DSDT length : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100730 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
Corentin Chary85091b72007-01-26 14:04:30 +0100731 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100732 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100733 len += sprintf(page + len, "DSDT revision : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100734 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100735 len += sprintf(page + len, "OEM id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100736 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100737 len += sprintf(page + len, "OEM table id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100738 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100739 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100740 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100741 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100742 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100743 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
744 }
745
746 return len;
747}
748
749static int parse_arg(const char *buf, unsigned long count, int *val)
750{
751 if (!count)
752 return 0;
753 if (count > 31)
754 return -EINVAL;
755 if (sscanf(buf, "%i", val) != 1)
756 return -EINVAL;
757 return count;
758}
759
Corentin Chary17e78f62010-01-13 22:21:33 +0100760static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
761 const char *buf, size_t count,
Corentin Charyd99b5772010-01-22 21:20:57 +0100762 const char *method)
Corentin Chary4564de12007-01-26 14:04:40 +0100763{
764 int rv, value;
765 int out = 0;
766
767 rv = parse_arg(buf, count, &value);
768 if (rv > 0)
769 out = value ? 1 : 0;
770
Corentin Charyd99b5772010-01-22 21:20:57 +0100771 if (write_acpi_int(asus->handle, method, value))
Corentin Chary17e78f62010-01-13 22:21:33 +0100772 return -ENODEV;
Corentin Chary4564de12007-01-26 14:04:40 +0100773 return rv;
774}
775
776/*
Corentin Chary722ad972007-01-26 14:04:55 +0100777 * LEDD display
778 */
779static ssize_t show_ledd(struct device *dev,
780 struct device_attribute *attr, char *buf)
781{
Corentin Chary9129d142009-12-01 22:39:41 +0100782 struct asus_laptop *asus = dev_get_drvdata(dev);
783
Corentin Chary50a90c42009-11-30 21:55:12 +0100784 return sprintf(buf, "0x%08x\n", asus->ledd_status);
Corentin Chary722ad972007-01-26 14:04:55 +0100785}
786
787static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
788 const char *buf, size_t count)
789{
Corentin Chary9129d142009-12-01 22:39:41 +0100790 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary722ad972007-01-26 14:04:55 +0100791 int rv, value;
792
793 rv = parse_arg(buf, count, &value);
794 if (rv > 0) {
Corentin Charyd99b5772010-01-22 21:20:57 +0100795 if (write_acpi_int(asus->handle, METHOD_LEDD, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200796 pr_warning("LED display write failed\n");
Corentin Chary722ad972007-01-26 14:04:55 +0100797 else
Corentin Chary50a90c42009-11-30 21:55:12 +0100798 asus->ledd_status = (u32) value;
Corentin Chary722ad972007-01-26 14:04:55 +0100799 }
800 return rv;
801}
802
803/*
Corentin Charyaa9df932010-01-13 21:49:10 +0100804 * Wireless
805 */
806static int asus_wireless_status(struct asus_laptop *asus, int mask)
807{
808 unsigned long long status;
809 acpi_status rv = AE_OK;
810
811 if (!asus->have_rsts)
812 return (asus->wireless_status & mask) ? 1 : 0;
813
Corentin Charyd99b5772010-01-22 21:20:57 +0100814 rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
815 NULL, &status);
Corentin Charyaa9df932010-01-13 21:49:10 +0100816 if (ACPI_FAILURE(rv)) {
817 pr_warning("Error reading Wireless status\n");
818 return -EINVAL;
819 }
820 return !!(status & mask);
821}
822
823/*
Corentin Chary4564de12007-01-26 14:04:40 +0100824 * WLAN
825 */
Corentin Charye5593bf2010-01-17 17:20:11 +0100826static int asus_wlan_set(struct asus_laptop *asus, int status)
827{
828 if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
829 pr_warning("Error setting wlan status to %d", status);
830 return -EIO;
831 }
832 return 0;
833}
834
Corentin Chary4564de12007-01-26 14:04:40 +0100835static ssize_t show_wlan(struct device *dev,
836 struct device_attribute *attr, char *buf)
837{
Corentin Chary9129d142009-12-01 22:39:41 +0100838 struct asus_laptop *asus = dev_get_drvdata(dev);
839
Corentin Chary17e78f62010-01-13 22:21:33 +0100840 return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100841}
842
843static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
844 const char *buf, size_t count)
845{
Corentin Chary9129d142009-12-01 22:39:41 +0100846 struct asus_laptop *asus = dev_get_drvdata(dev);
847
Corentin Charyd99b5772010-01-22 21:20:57 +0100848 return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
Corentin Chary4564de12007-01-26 14:04:40 +0100849}
850
851/*
852 * Bluetooth
853 */
Corentin Charye5593bf2010-01-17 17:20:11 +0100854static int asus_bluetooth_set(struct asus_laptop *asus, int status)
855{
856 if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
857 pr_warning("Error setting bluetooth status to %d", status);
858 return -EIO;
859 }
860 return 0;
861}
862
Corentin Chary4564de12007-01-26 14:04:40 +0100863static ssize_t show_bluetooth(struct device *dev,
864 struct device_attribute *attr, char *buf)
865{
Corentin Chary9129d142009-12-01 22:39:41 +0100866 struct asus_laptop *asus = dev_get_drvdata(dev);
867
Corentin Chary17e78f62010-01-13 22:21:33 +0100868 return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100869}
870
Len Brown8def05f2007-01-30 01:46:43 -0500871static ssize_t store_bluetooth(struct device *dev,
872 struct device_attribute *attr, const char *buf,
873 size_t count)
Corentin Chary4564de12007-01-26 14:04:40 +0100874{
Corentin Chary9129d142009-12-01 22:39:41 +0100875 struct asus_laptop *asus = dev_get_drvdata(dev);
876
Corentin Charyd99b5772010-01-22 21:20:57 +0100877 return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
Corentin Chary4564de12007-01-26 14:04:40 +0100878}
879
Corentin Chary78127b42007-01-26 14:04:49 +0100880/*
881 * Display
882 */
Corentin Chary4d441512010-01-13 22:26:24 +0100883static void asus_set_display(struct asus_laptop *asus, int value)
Corentin Chary78127b42007-01-26 14:04:49 +0100884{
885 /* no sanity check needed for now */
Corentin Charyd99b5772010-01-22 21:20:57 +0100886 if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200887 pr_warning("Error setting display\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100888 return;
889}
890
Corentin Chary9129d142009-12-01 22:39:41 +0100891static int read_display(struct asus_laptop *asus)
Corentin Chary78127b42007-01-26 14:04:49 +0100892{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400893 unsigned long long value = 0;
Corentin Chary9a816852007-03-11 10:25:38 +0100894 acpi_status rv = AE_OK;
Corentin Chary78127b42007-01-26 14:04:49 +0100895
Corentin Charybe966662009-08-29 10:28:29 +0200896 /*
897 * In most of the case, we know how to set the display, but sometime
898 * we can't read it
899 */
Len Brown8def05f2007-01-30 01:46:43 -0500900 if (display_get_handle) {
Corentin Chary9a816852007-03-11 10:25:38 +0100901 rv = acpi_evaluate_integer(display_get_handle, NULL,
902 NULL, &value);
903 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200904 pr_warning("Error reading display status\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100905 }
906
907 value &= 0x0F; /* needed for some models, shouldn't hurt others */
908
909 return value;
910}
Len Brown8def05f2007-01-30 01:46:43 -0500911
Corentin Chary78127b42007-01-26 14:04:49 +0100912/*
913 * Now, *this* one could be more user-friendly, but so far, no-one has
914 * complained. The significance of bits is the same as in store_disp()
915 */
916static ssize_t show_disp(struct device *dev,
917 struct device_attribute *attr, char *buf)
918{
Corentin Chary9129d142009-12-01 22:39:41 +0100919 struct asus_laptop *asus = dev_get_drvdata(dev);
920
921 return sprintf(buf, "%d\n", read_display(asus));
Corentin Chary78127b42007-01-26 14:04:49 +0100922}
923
924/*
925 * Experimental support for display switching. As of now: 1 should activate
926 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
927 * Any combination (bitwise) of these will suffice. I never actually tested 4
928 * displays hooked up simultaneously, so be warned. See the acpi4asus README
929 * for more info.
930 */
931static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
932 const char *buf, size_t count)
933{
Corentin Chary9129d142009-12-01 22:39:41 +0100934 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary78127b42007-01-26 14:04:49 +0100935 int rv, value;
936
937 rv = parse_arg(buf, count, &value);
938 if (rv > 0)
Corentin Chary4d441512010-01-13 22:26:24 +0100939 asus_set_display(asus, value);
Corentin Chary78127b42007-01-26 14:04:49 +0100940 return rv;
941}
942
Corentin Chary8b857352007-01-26 14:04:58 +0100943/*
944 * Light Sens
945 */
Corentin Chary4d441512010-01-13 22:26:24 +0100946static void asus_als_switch(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100947{
Corentin Charyd99b5772010-01-22 21:20:57 +0100948 if (write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200949 pr_warning("Error setting light sensor switch\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100950 asus->light_switch = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100951}
952
953static ssize_t show_lssw(struct device *dev,
954 struct device_attribute *attr, char *buf)
955{
Corentin Chary9129d142009-12-01 22:39:41 +0100956 struct asus_laptop *asus = dev_get_drvdata(dev);
957
Corentin Chary50a90c42009-11-30 21:55:12 +0100958 return sprintf(buf, "%d\n", asus->light_switch);
Corentin Chary8b857352007-01-26 14:04:58 +0100959}
960
961static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
962 const char *buf, size_t count)
963{
Corentin Chary9129d142009-12-01 22:39:41 +0100964 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100965 int rv, value;
966
967 rv = parse_arg(buf, count, &value);
968 if (rv > 0)
Corentin Chary4d441512010-01-13 22:26:24 +0100969 asus_als_switch(asus, value ? 1 : 0);
Corentin Chary8b857352007-01-26 14:04:58 +0100970
971 return rv;
972}
973
Corentin Chary4d441512010-01-13 22:26:24 +0100974static void asus_als_level(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100975{
Corentin Charyd99b5772010-01-22 21:20:57 +0100976 if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200977 pr_warning("Error setting light sensor level\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100978 asus->light_level = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100979}
980
981static ssize_t show_lslvl(struct device *dev,
982 struct device_attribute *attr, char *buf)
983{
Corentin Chary9129d142009-12-01 22:39:41 +0100984 struct asus_laptop *asus = dev_get_drvdata(dev);
985
Corentin Chary50a90c42009-11-30 21:55:12 +0100986 return sprintf(buf, "%d\n", asus->light_level);
Corentin Chary8b857352007-01-26 14:04:58 +0100987}
988
989static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
990 const char *buf, size_t count)
991{
Corentin Chary9129d142009-12-01 22:39:41 +0100992 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100993 int rv, value;
994
995 rv = parse_arg(buf, count, &value);
996 if (rv > 0) {
997 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
998 /* 0 <= value <= 15 */
Corentin Chary4d441512010-01-13 22:26:24 +0100999 asus_als_level(asus, value);
Corentin Chary8b857352007-01-26 14:04:58 +01001000 }
1001
1002 return rv;
1003}
1004
Corentin Charye539c2f2007-05-06 14:47:06 +02001005/*
1006 * GPS
1007 */
Corentin Chary6358bf22010-01-13 21:55:44 +01001008static int asus_gps_status(struct asus_laptop *asus)
1009{
1010 unsigned long long status;
1011 acpi_status rv = AE_OK;
1012
Corentin Charyd99b5772010-01-22 21:20:57 +01001013 rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
1014 NULL, &status);
Corentin Chary6358bf22010-01-13 21:55:44 +01001015 if (ACPI_FAILURE(rv)) {
1016 pr_warning("Error reading GPS status\n");
1017 return -ENODEV;
1018 }
1019 return !!status;
1020}
1021
1022static int asus_gps_switch(struct asus_laptop *asus, int status)
1023{
Corentin Charyd99b5772010-01-22 21:20:57 +01001024 const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
Corentin Chary6358bf22010-01-13 21:55:44 +01001025
Corentin Charyd99b5772010-01-22 21:20:57 +01001026 if (write_acpi_int(asus->handle, meth, 0x02))
Corentin Chary6358bf22010-01-13 21:55:44 +01001027 return -ENODEV;
1028 return 0;
1029}
1030
Corentin Charye539c2f2007-05-06 14:47:06 +02001031static ssize_t show_gps(struct device *dev,
1032 struct device_attribute *attr, char *buf)
1033{
Corentin Chary9129d142009-12-01 22:39:41 +01001034 struct asus_laptop *asus = dev_get_drvdata(dev);
1035
Corentin Chary6358bf22010-01-13 21:55:44 +01001036 return sprintf(buf, "%d\n", asus_gps_status(asus));
Corentin Charye539c2f2007-05-06 14:47:06 +02001037}
1038
1039static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
1040 const char *buf, size_t count)
1041{
Corentin Chary6358bf22010-01-13 21:55:44 +01001042 struct asus_laptop *asus = dev_get_drvdata(dev);
1043 int rv, value;
1044 int ret;
Corentin Chary9129d142009-12-01 22:39:41 +01001045
Corentin Chary6358bf22010-01-13 21:55:44 +01001046 rv = parse_arg(buf, count, &value);
1047 if (rv <= 0)
1048 return -EINVAL;
1049 ret = asus_gps_switch(asus, !!value);
1050 if (ret)
1051 return ret;
Corentin Chary18e13112010-01-25 23:29:24 +01001052 rfkill_set_sw_state(asus->gps_rfkill, !value);
Corentin Chary6358bf22010-01-13 21:55:44 +01001053 return rv;
Corentin Charye539c2f2007-05-06 14:47:06 +02001054}
1055
Corentin Chary034ce902009-01-20 16:17:43 +01001056/*
Corentin Chary18e13112010-01-25 23:29:24 +01001057 * rfkill
1058 */
1059static int asus_gps_rfkill_set(void *data, bool blocked)
1060{
1061 acpi_handle handle = data;
1062
1063 return asus_gps_switch(handle, !blocked);
1064}
1065
1066static const struct rfkill_ops asus_gps_rfkill_ops = {
1067 .set_block = asus_gps_rfkill_set,
1068};
1069
1070static void asus_rfkill_exit(struct asus_laptop *asus)
1071{
1072 if (asus->gps_rfkill) {
1073 rfkill_unregister(asus->gps_rfkill);
1074 rfkill_destroy(asus->gps_rfkill);
1075 asus->gps_rfkill = NULL;
1076 }
1077}
1078
1079static int asus_rfkill_init(struct asus_laptop *asus)
1080{
1081 int result;
1082
1083 if (acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) ||
1084 acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) ||
1085 acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
1086 return 0;
1087
1088 asus->gps_rfkill = rfkill_alloc("asus-gps", &asus->platform_device->dev,
1089 RFKILL_TYPE_GPS,
1090 &asus_gps_rfkill_ops, NULL);
1091 if (!asus->gps_rfkill)
1092 return -EINVAL;
1093
1094 result = rfkill_register(asus->gps_rfkill);
1095 if (result) {
1096 rfkill_destroy(asus->gps_rfkill);
1097 asus->gps_rfkill = NULL;
1098 }
1099
1100 return result;
1101}
1102
1103/*
Corentin Charybe4ee822009-12-06 16:27:09 +01001104 * Input device (i.e. hotkeys)
Corentin Chary034ce902009-01-20 16:17:43 +01001105 */
Corentin Charybe4ee822009-12-06 16:27:09 +01001106static void asus_input_notify(struct asus_laptop *asus, int event)
1107{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001108 if (asus->inputdev)
1109 sparse_keymap_report_event(asus->inputdev, event, 1, true);
Corentin Charybe4ee822009-12-06 16:27:09 +01001110}
1111
1112static int asus_input_init(struct asus_laptop *asus)
1113{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001114 struct input_dev *input;
1115 int error;
Corentin Charybe4ee822009-12-06 16:27:09 +01001116
Corentin Chary66a71dd2010-01-25 22:50:11 +01001117 input = input_allocate_device();
1118 if (!input) {
Corentin Charybe4ee822009-12-06 16:27:09 +01001119 pr_info("Unable to allocate input device\n");
1120 return 0;
1121 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001122 input->name = "Asus Laptop extra buttons";
1123 input->phys = ASUS_LAPTOP_FILE "/input0";
1124 input->id.bustype = BUS_HOST;
1125 input->dev.parent = &asus->platform_device->dev;
1126 input_set_drvdata(input, asus);
Corentin Charybe4ee822009-12-06 16:27:09 +01001127
Corentin Chary66a71dd2010-01-25 22:50:11 +01001128 error = sparse_keymap_setup(input, asus_keymap, NULL);
1129 if (error) {
1130 pr_err("Unable to setup input device keymap\n");
1131 goto err_keymap;
Corentin Charybe4ee822009-12-06 16:27:09 +01001132 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001133 error = input_register_device(input);
1134 if (error) {
Corentin Charybe4ee822009-12-06 16:27:09 +01001135 pr_info("Unable to register input device\n");
Corentin Chary66a71dd2010-01-25 22:50:11 +01001136 goto err_device;
Corentin Charybe4ee822009-12-06 16:27:09 +01001137 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001138
1139 asus->inputdev = input;
1140 return 0;
1141
1142err_keymap:
1143 sparse_keymap_free(input);
1144err_device:
1145 input_free_device(input);
1146 return error;
Corentin Charybe4ee822009-12-06 16:27:09 +01001147}
1148
1149static void asus_input_exit(struct asus_laptop *asus)
1150{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001151 if (asus->inputdev) {
1152 sparse_keymap_free(asus->inputdev);
Corentin Charybe4ee822009-12-06 16:27:09 +01001153 input_unregister_device(asus->inputdev);
Corentin Chary66a71dd2010-01-25 22:50:11 +01001154 }
Corentin Charybe4ee822009-12-06 16:27:09 +01001155}
1156
1157/*
1158 * ACPI driver
1159 */
Corentin Chary600ad522009-11-30 21:42:42 +01001160static void asus_acpi_notify(struct acpi_device *device, u32 event)
Corentin Chary85091b72007-01-26 14:04:30 +01001161{
Corentin Chary9129d142009-12-01 22:39:41 +01001162 struct asus_laptop *asus = acpi_driver_data(device);
Corentin Chary6050c8d2009-02-15 19:30:19 +01001163 u16 count;
Corentin Chary034ce902009-01-20 16:17:43 +01001164
Corentin Chary6b7091e2007-01-26 14:04:45 +01001165 /*
1166 * We need to tell the backlight device when the backlight power is
1167 * switched
1168 */
Corentin Chary3e68ae72010-01-13 22:10:39 +01001169 if (event == ATKD_LCD_ON)
Corentin Chary9129d142009-12-01 22:39:41 +01001170 lcd_blank(asus, FB_BLANK_UNBLANK);
Corentin Chary3e68ae72010-01-13 22:10:39 +01001171 else if (event == ATKD_LCD_OFF)
Corentin Chary9129d142009-12-01 22:39:41 +01001172 lcd_blank(asus, FB_BLANK_POWERDOWN);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001173
Corentin Chary619d8b12009-11-28 10:35:37 +01001174 /* TODO Find a better way to handle events count. */
Corentin Chary50a90c42009-11-30 21:55:12 +01001175 count = asus->event_count[event % 128]++;
1176 acpi_bus_generate_proc_event(asus->device, event, count);
1177 acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
1178 dev_name(&asus->device->dev), event,
Corentin Chary6050c8d2009-02-15 19:30:19 +01001179 count);
Corentin Chary85091b72007-01-26 14:04:30 +01001180
Corentin Charya539df52010-01-25 22:53:21 +01001181 /* Brightness events are special */
1182 if (event >= ATKD_BR_MIN && event <= ATKD_BR_MAX) {
1183
1184 /* Ignore them completely if the acpi video driver is used */
1185 if (asus->backlight_device != NULL) {
1186 /* Update the backlight device. */
1187 asus_backlight_notify(asus);
1188 }
1189 return ;
1190 }
Corentin Charybe4ee822009-12-06 16:27:09 +01001191 asus_input_notify(asus, event);
Corentin Chary85091b72007-01-26 14:04:30 +01001192}
1193
1194#define ASUS_CREATE_DEVICE_ATTR(_name) \
1195 struct device_attribute dev_attr_##_name = { \
1196 .attr = { \
1197 .name = __stringify(_name), \
Tejun Heo7b595752007-06-14 03:45:17 +09001198 .mode = 0 }, \
Corentin Chary85091b72007-01-26 14:04:30 +01001199 .show = NULL, \
1200 .store = NULL, \
1201 }
1202
1203#define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
1204 do { \
1205 dev_attr_##_name.attr.mode = _mode; \
1206 dev_attr_##_name.show = _show; \
1207 dev_attr_##_name.store = _store; \
1208 } while(0)
1209
1210static ASUS_CREATE_DEVICE_ATTR(infos);
Corentin Chary4564de12007-01-26 14:04:40 +01001211static ASUS_CREATE_DEVICE_ATTR(wlan);
1212static ASUS_CREATE_DEVICE_ATTR(bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +01001213static ASUS_CREATE_DEVICE_ATTR(display);
Corentin Chary722ad972007-01-26 14:04:55 +01001214static ASUS_CREATE_DEVICE_ATTR(ledd);
Corentin Chary8b857352007-01-26 14:04:58 +01001215static ASUS_CREATE_DEVICE_ATTR(ls_switch);
1216static ASUS_CREATE_DEVICE_ATTR(ls_level);
Corentin Charye539c2f2007-05-06 14:47:06 +02001217static ASUS_CREATE_DEVICE_ATTR(gps);
Corentin Chary85091b72007-01-26 14:04:30 +01001218
1219static struct attribute *asuspf_attributes[] = {
Len Brown8def05f2007-01-30 01:46:43 -05001220 &dev_attr_infos.attr,
1221 &dev_attr_wlan.attr,
1222 &dev_attr_bluetooth.attr,
1223 &dev_attr_display.attr,
1224 &dev_attr_ledd.attr,
1225 &dev_attr_ls_switch.attr,
1226 &dev_attr_ls_level.attr,
Corentin Charye539c2f2007-05-06 14:47:06 +02001227 &dev_attr_gps.attr,
Len Brown8def05f2007-01-30 01:46:43 -05001228 NULL
Corentin Chary85091b72007-01-26 14:04:30 +01001229};
1230
Corentin Chary600ad522009-11-30 21:42:42 +01001231static struct attribute_group platform_attribute_group = {
Len Brown8def05f2007-01-30 01:46:43 -05001232 .attrs = asuspf_attributes
Corentin Chary85091b72007-01-26 14:04:30 +01001233};
1234
Corentin Chary9129d142009-12-01 22:39:41 +01001235static int asus_platform_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001236{
1237 int result;
1238
Corentin Chary50a90c42009-11-30 21:55:12 +01001239 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1240 if (!asus->platform_device)
Corentin Chary600ad522009-11-30 21:42:42 +01001241 return -ENOMEM;
Corentin Chary9129d142009-12-01 22:39:41 +01001242 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001243
Corentin Chary50a90c42009-11-30 21:55:12 +01001244 result = platform_device_add(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001245 if (result)
1246 goto fail_platform_device;
1247
Corentin Chary50a90c42009-11-30 21:55:12 +01001248 result = sysfs_create_group(&asus->platform_device->dev.kobj,
Corentin Chary600ad522009-11-30 21:42:42 +01001249 &platform_attribute_group);
1250 if (result)
1251 goto fail_sysfs;
1252 return 0;
1253
1254fail_sysfs:
Corentin Chary50a90c42009-11-30 21:55:12 +01001255 platform_device_del(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001256fail_platform_device:
Corentin Chary50a90c42009-11-30 21:55:12 +01001257 platform_device_put(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001258 return result;
1259}
1260
Corentin Chary9129d142009-12-01 22:39:41 +01001261static void asus_platform_exit(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001262{
Corentin Chary50a90c42009-11-30 21:55:12 +01001263 sysfs_remove_group(&asus->platform_device->dev.kobj,
Corentin Chary600ad522009-11-30 21:42:42 +01001264 &platform_attribute_group);
Corentin Chary50a90c42009-11-30 21:55:12 +01001265 platform_device_unregister(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001266}
1267
1268static struct platform_driver platform_driver = {
Len Brown8def05f2007-01-30 01:46:43 -05001269 .driver = {
Corentin Chary9129d142009-12-01 22:39:41 +01001270 .name = ASUS_LAPTOP_FILE,
1271 .owner = THIS_MODULE,
1272 }
Corentin Chary85091b72007-01-26 14:04:30 +01001273};
1274
Corentin Chary9129d142009-12-01 22:39:41 +01001275static void asus_laptop_add_fs(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001276{
1277 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
Corentin Chary4564de12007-01-26 14:04:40 +01001278
Corentin Charyd99b5772010-01-22 21:20:57 +01001279 if (!acpi_check_handle(asus->handle, METHOD_WLAN, NULL))
Corentin Chary4564de12007-01-26 14:04:40 +01001280 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
1281
Corentin Charyd99b5772010-01-22 21:20:57 +01001282 if (!acpi_check_handle(asus->handle, METHOD_BLUETOOTH, NULL))
Corentin Chary4564de12007-01-26 14:04:40 +01001283 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
1284 show_bluetooth, store_bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +01001285
Corentin Charyd99b5772010-01-22 21:20:57 +01001286 if (!acpi_check_handle(asus->handle, METHOD_SWITCH_DISPLAY, NULL)) {
1287 if (display_get_handle)
1288 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp,
1289 store_disp);
1290 else
1291 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
1292 }
Corentin Chary78127b42007-01-26 14:04:49 +01001293
Corentin Charyd99b5772010-01-22 21:20:57 +01001294 if (!acpi_check_handle(asus->handle, METHOD_LEDD, NULL))
Corentin Chary722ad972007-01-26 14:04:55 +01001295 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
1296
Corentin Charyd99b5772010-01-22 21:20:57 +01001297 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1298 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
Corentin Chary8b857352007-01-26 14:04:58 +01001299 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
1300 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
1301 }
Corentin Charye539c2f2007-05-06 14:47:06 +02001302
Corentin Charyd99b5772010-01-22 21:20:57 +01001303 if (!acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) &&
1304 !acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) &&
1305 !acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
Corentin Charye539c2f2007-05-06 14:47:06 +02001306 ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
Corentin Chary85091b72007-01-26 14:04:30 +01001307}
1308
Len Brown8def05f2007-01-30 01:46:43 -05001309static int asus_handle_init(char *name, acpi_handle * handle,
Corentin Chary85091b72007-01-26 14:04:30 +01001310 char **paths, int num_paths)
1311{
1312 int i;
1313 acpi_status status;
1314
1315 for (i = 0; i < num_paths; i++) {
1316 status = acpi_get_handle(NULL, paths[i], handle);
1317 if (ACPI_SUCCESS(status))
1318 return 0;
1319 }
1320
1321 *handle = NULL;
1322 return -ENODEV;
1323}
1324
1325#define ASUS_HANDLE_INIT(object) \
1326 asus_handle_init(#object, &object##_handle, object##_paths, \
1327 ARRAY_SIZE(object##_paths))
1328
Corentin Chary85091b72007-01-26 14:04:30 +01001329/*
Corentin Chary50a90c42009-11-30 21:55:12 +01001330 * This function is used to initialize the context with right values. In this
1331 * method, we can make all the detection we want, and modify the asus_laptop
1332 * struct
Corentin Chary85091b72007-01-26 14:04:30 +01001333 */
Corentin Chary7c247642009-11-30 22:13:54 +01001334static int asus_laptop_get_info(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001335{
1336 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Corentin Chary85091b72007-01-26 14:04:30 +01001337 union acpi_object *model = NULL;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001338 unsigned long long bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +01001339 char *string = NULL;
1340 acpi_status status;
1341
1342 /*
1343 * Get DSDT headers early enough to allow for differentiating between
1344 * models, but late enough to allow acpi_bus_register_driver() to fail
1345 * before doing anything ACPI-specific. Should we encounter a machine,
1346 * which needs special handling (i.e. its hotkey device has a different
Corentin Chary7c247642009-11-30 22:13:54 +01001347 * HID), this bit will be moved.
Corentin Chary85091b72007-01-26 14:04:30 +01001348 */
Corentin Chary7c247642009-11-30 22:13:54 +01001349 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
Corentin Chary85091b72007-01-26 14:04:30 +01001350 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001351 pr_warning("Couldn't get the DSDT table header\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001352
1353 /* We have to write 0 on init this far for all ASUS models */
Corentin Chary50a90c42009-11-30 21:55:12 +01001354 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001355 pr_err("Hotkey initialization failed\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001356 return -ENODEV;
1357 }
1358
1359 /* This needs to be called for some laptops to init properly */
Corentin Chary9a816852007-03-11 10:25:38 +01001360 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001361 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
Corentin Chary9a816852007-03-11 10:25:38 +01001362 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001363 pr_warning("Error calling BSTS\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001364 else if (bsts_result)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001365 pr_notice("BSTS called, 0x%02x returned\n",
Corentin Chary9a816852007-03-11 10:25:38 +01001366 (uint) bsts_result);
Corentin Chary85091b72007-01-26 14:04:30 +01001367
Corentin Chary185e5af2007-03-11 10:27:33 +01001368 /* This too ... */
Corentin Charye5593bf2010-01-17 17:20:11 +01001369 if (write_acpi_int(asus->handle, "CWAP", wapf))
1370 pr_err("Error calling CWAP(%d)\n", wapf);
Corentin Chary85091b72007-01-26 14:04:30 +01001371 /*
1372 * Try to match the object returned by INIT to the specific model.
1373 * Handle every possible object (or the lack of thereof) the DSDT
1374 * writers might throw at us. When in trouble, we pass NULL to
1375 * asus_model_match() and try something completely different.
1376 */
1377 if (buffer.pointer) {
1378 model = buffer.pointer;
1379 switch (model->type) {
1380 case ACPI_TYPE_STRING:
1381 string = model->string.pointer;
1382 break;
1383 case ACPI_TYPE_BUFFER:
1384 string = model->buffer.pointer;
1385 break;
1386 default:
1387 string = "";
1388 break;
1389 }
1390 }
Corentin Chary50a90c42009-11-30 21:55:12 +01001391 asus->name = kstrdup(string, GFP_KERNEL);
1392 if (!asus->name)
Corentin Chary85091b72007-01-26 14:04:30 +01001393 return -ENOMEM;
1394
Len Brown8def05f2007-01-30 01:46:43 -05001395 if (*string)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001396 pr_notice(" %s model detected\n", string);
Corentin Chary85091b72007-01-26 14:04:30 +01001397
Corentin Chary4564de12007-01-26 14:04:40 +01001398 /*
1399 * The HWRS method return informations about the hardware.
1400 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1401 * The significance of others is yet to be found.
Corentin Chary4564de12007-01-26 14:04:40 +01001402 */
Corentin Chary9a816852007-03-11 10:25:38 +01001403 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001404 acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
Corentin Charyd99b5772010-01-22 21:20:57 +01001405 if (!ACPI_FAILURE(status))
1406 pr_notice(" HRWS returned %x", (int)hwrs_result);
Corentin Chary4564de12007-01-26 14:04:40 +01001407
Corentin Charyd99b5772010-01-22 21:20:57 +01001408 if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
Corentin Charyaa9df932010-01-13 21:49:10 +01001409 asus->have_rsts = true;
Corentin Chary4564de12007-01-26 14:04:40 +01001410
Corentin Charyd99b5772010-01-22 21:20:57 +01001411 /* Scheduled for removal */
Corentin Chary6b7091e2007-01-26 14:04:45 +01001412 ASUS_HANDLE_INIT(lcd_switch);
Corentin Chary78127b42007-01-26 14:04:49 +01001413 ASUS_HANDLE_INIT(display_get);
1414
Corentin Chary85091b72007-01-26 14:04:30 +01001415 kfree(model);
1416
1417 return AE_OK;
1418}
1419
Corentin Chary600ad522009-11-30 21:42:42 +01001420static bool asus_device_present;
1421
Corentin Chary9129d142009-12-01 22:39:41 +01001422static int __devinit asus_acpi_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001423{
1424 int result = 0;
1425
Corentin Chary50a90c42009-11-30 21:55:12 +01001426 result = acpi_bus_get_status(asus->device);
Corentin Chary600ad522009-11-30 21:42:42 +01001427 if (result)
1428 return result;
Corentin Chary50a90c42009-11-30 21:55:12 +01001429 if (!asus->device->status.present) {
Corentin Chary600ad522009-11-30 21:42:42 +01001430 pr_err("Hotkey device not present, aborting\n");
1431 return -ENODEV;
1432 }
1433
Corentin Chary7c247642009-11-30 22:13:54 +01001434 result = asus_laptop_get_info(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001435 if (result)
1436 return result;
1437
Corentin Chary9129d142009-12-01 22:39:41 +01001438 asus_laptop_add_fs(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001439
1440 /* WLED and BLED are on by default */
Corentin Charybe4ee822009-12-06 16:27:09 +01001441 if (bluetooth_status >= 0)
Corentin Charye5593bf2010-01-17 17:20:11 +01001442 asus_bluetooth_set(asus, !!bluetooth_status);
1443
Corentin Charyd0930a22010-01-17 17:21:13 +01001444 if (wlan_status >= 0)
1445 asus_wlan_set(asus, !!wlan_status);
Corentin Chary600ad522009-11-30 21:42:42 +01001446
1447 /* Keyboard Backlight is on by default */
Corentin Charyd99b5772010-01-22 21:20:57 +01001448 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
Corentin Chary4d441512010-01-13 22:26:24 +01001449 asus_kled_set(asus, 1);
Corentin Chary600ad522009-11-30 21:42:42 +01001450
1451 /* LED display is off by default */
Corentin Chary50a90c42009-11-30 21:55:12 +01001452 asus->ledd_status = 0xFFF;
Corentin Chary600ad522009-11-30 21:42:42 +01001453
1454 /* Set initial values of light sensor and level */
Corentin Charybe4ee822009-12-06 16:27:09 +01001455 asus->light_switch = 0; /* Default to light sensor disabled */
1456 asus->light_level = 5; /* level 5 for sensor sensitivity */
Corentin Chary600ad522009-11-30 21:42:42 +01001457
Corentin Charyd99b5772010-01-22 21:20:57 +01001458 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1459 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
Corentin Chary4d441512010-01-13 22:26:24 +01001460 asus_als_switch(asus, asus->light_switch);
Corentin Chary4d441512010-01-13 22:26:24 +01001461 asus_als_level(asus, asus->light_level);
Corentin Charyd99b5772010-01-22 21:20:57 +01001462 }
Corentin Chary600ad522009-11-30 21:42:42 +01001463
Corentin Chary47ee0e92010-01-24 11:17:15 +01001464 asus->lcd_state = 1; /* LCD should be on when the module load */
Corentin Chary600ad522009-11-30 21:42:42 +01001465 return result;
1466}
1467
1468static int __devinit asus_acpi_add(struct acpi_device *device)
1469{
Corentin Chary9129d142009-12-01 22:39:41 +01001470 struct asus_laptop *asus;
Corentin Chary600ad522009-11-30 21:42:42 +01001471 int result;
1472
1473 pr_notice("Asus Laptop Support version %s\n",
1474 ASUS_LAPTOP_VERSION);
Corentin Chary50a90c42009-11-30 21:55:12 +01001475 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1476 if (!asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001477 return -ENOMEM;
Corentin Chary50a90c42009-11-30 21:55:12 +01001478 asus->handle = device->handle;
1479 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1480 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1481 device->driver_data = asus;
1482 asus->device = device;
Corentin Chary600ad522009-11-30 21:42:42 +01001483
Corentin Chary9129d142009-12-01 22:39:41 +01001484 result = asus_acpi_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001485 if (result)
1486 goto fail_platform;
1487
1488 /*
1489 * Register the platform device first. It is used as a parent for the
1490 * sub-devices below.
1491 */
Corentin Chary9129d142009-12-01 22:39:41 +01001492 result = asus_platform_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001493 if (result)
1494 goto fail_platform;
1495
1496 if (!acpi_video_backlight_support()) {
Corentin Chary9129d142009-12-01 22:39:41 +01001497 result = asus_backlight_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001498 if (result)
1499 goto fail_backlight;
1500 } else
1501 pr_info("Backlight controlled by ACPI video driver\n");
1502
Corentin Chary9129d142009-12-01 22:39:41 +01001503 result = asus_input_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001504 if (result)
1505 goto fail_input;
1506
Corentin Chary9129d142009-12-01 22:39:41 +01001507 result = asus_led_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001508 if (result)
1509 goto fail_led;
1510
Corentin Chary18e13112010-01-25 23:29:24 +01001511 result = asus_rfkill_init(asus);
1512 if (result)
1513 goto fail_rfkill;
1514
Corentin Chary600ad522009-11-30 21:42:42 +01001515 asus_device_present = true;
1516 return 0;
1517
Corentin Chary18e13112010-01-25 23:29:24 +01001518fail_rfkill:
1519 asus_led_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001520fail_led:
Corentin Chary9129d142009-12-01 22:39:41 +01001521 asus_input_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001522fail_input:
Corentin Chary9129d142009-12-01 22:39:41 +01001523 asus_backlight_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001524fail_backlight:
Corentin Chary9129d142009-12-01 22:39:41 +01001525 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001526fail_platform:
Corentin Chary50a90c42009-11-30 21:55:12 +01001527 kfree(asus->name);
1528 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001529
1530 return result;
1531}
1532
1533static int asus_acpi_remove(struct acpi_device *device, int type)
1534{
Corentin Chary9129d142009-12-01 22:39:41 +01001535 struct asus_laptop *asus = acpi_driver_data(device);
1536
1537 asus_backlight_exit(asus);
Corentin Chary18e13112010-01-25 23:29:24 +01001538 asus_rfkill_exit(asus);
Corentin Chary9129d142009-12-01 22:39:41 +01001539 asus_led_exit(asus);
1540 asus_input_exit(asus);
1541 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001542
Corentin Chary50a90c42009-11-30 21:55:12 +01001543 kfree(asus->name);
1544 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001545 return 0;
1546}
1547
1548static const struct acpi_device_id asus_device_ids[] = {
1549 {"ATK0100", 0},
1550 {"ATK0101", 0},
1551 {"", 0},
1552};
1553MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1554
1555static struct acpi_driver asus_acpi_driver = {
Corentin Chary50a90c42009-11-30 21:55:12 +01001556 .name = ASUS_LAPTOP_NAME,
1557 .class = ASUS_LAPTOP_CLASS,
Corentin Chary600ad522009-11-30 21:42:42 +01001558 .owner = THIS_MODULE,
1559 .ids = asus_device_ids,
1560 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1561 .ops = {
1562 .add = asus_acpi_add,
1563 .remove = asus_acpi_remove,
1564 .notify = asus_acpi_notify,
1565 },
1566};
1567
Corentin Chary85091b72007-01-26 14:04:30 +01001568static int __init asus_laptop_init(void)
1569{
1570 int result;
1571
Corentin Chary600ad522009-11-30 21:42:42 +01001572 result = platform_driver_register(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001573 if (result < 0)
1574 return result;
1575
Corentin Chary600ad522009-11-30 21:42:42 +01001576 result = acpi_bus_register_driver(&asus_acpi_driver);
1577 if (result < 0)
1578 goto fail_acpi_driver;
1579 if (!asus_device_present) {
1580 result = -ENODEV;
1581 goto fail_no_device;
Corentin Chary85091b72007-01-26 14:04:30 +01001582 }
Len Brown8def05f2007-01-30 01:46:43 -05001583 return 0;
Corentin Chary85091b72007-01-26 14:04:30 +01001584
Corentin Chary600ad522009-11-30 21:42:42 +01001585fail_no_device:
1586 acpi_bus_unregister_driver(&asus_acpi_driver);
1587fail_acpi_driver:
1588 platform_driver_unregister(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001589 return result;
1590}
1591
Corentin Chary600ad522009-11-30 21:42:42 +01001592static void __exit asus_laptop_exit(void)
1593{
1594 acpi_bus_unregister_driver(&asus_acpi_driver);
1595 platform_driver_unregister(&platform_driver);
1596}
1597
Corentin Chary85091b72007-01-26 14:04:30 +01001598module_init(asus_laptop_init);
1599module_exit(asus_laptop_exit);