blob: e416867c07255fd0961ea5cd614b0fe9797405c4 [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
Andy Ross8819de72011-10-14 11:13:35 +02007 * Copyright (C) 2011 Wind River Systems
Corentin Chary85091b72007-01-26 14:04:30 +01008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 *
24 * The development page for this driver is located at
25 * http://sourceforge.net/projects/acpi4asus/
26 *
27 * Credits:
28 * Pontus Fuchs - Helper functions, cleanup
29 * Johann Wiesner - Small compile fixes
30 * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
31 * Eric Burghard - LED display support for W1N
32 * Josh Green - Light Sens support
Lucas De Marchic8440332011-03-17 17:18:22 -030033 * Thomas Tuttle - His first patch for led support was very helpful
Corentin Charye539c2f2007-05-06 14:47:06 +020034 * Sam Lin - GPS support
Corentin Chary85091b72007-01-26 14:04:30 +010035 */
36
Corentin Chary2fcc23d2009-06-19 14:52:03 +020037#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
Corentin Chary85091b72007-01-26 14:04:30 +010039#include <linux/kernel.h>
40#include <linux/module.h>
41#include <linux/init.h>
42#include <linux/types.h>
43#include <linux/err.h>
44#include <linux/proc_fs.h>
Corentin Chary6b7091e2007-01-26 14:04:45 +010045#include <linux/backlight.h>
46#include <linux/fb.h>
Corentin Charybe18cda2007-01-26 14:04:35 +010047#include <linux/leds.h>
Corentin Chary85091b72007-01-26 14:04:30 +010048#include <linux/platform_device.h>
Corentin Chary060cbce2010-01-28 10:52:40 +010049#include <linux/uaccess.h>
Corentin Chary034ce902009-01-20 16:17:43 +010050#include <linux/input.h>
Corentin Chary66a71dd2010-01-25 22:50:11 +010051#include <linux/input/sparse-keymap.h>
Andy Ross8819de72011-10-14 11:13:35 +020052#include <linux/input-polldev.h>
Corentin Chary18e13112010-01-25 23:29:24 +010053#include <linux/rfkill.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Corentin Charyaf96f872011-02-06 13:27:30 +010055#include <linux/dmi.h>
Corentin Chary060cbce2010-01-28 10:52:40 +010056#include <acpi/acpi_drivers.h>
57#include <acpi/acpi_bus.h>
Corentin Chary85091b72007-01-26 14:04:30 +010058
Corentin Chary91687cc2009-11-28 10:32:34 +010059#define ASUS_LAPTOP_VERSION "0.42"
Corentin Chary85091b72007-01-26 14:04:30 +010060
Corentin Chary50a90c42009-11-30 21:55:12 +010061#define ASUS_LAPTOP_NAME "Asus Laptop Support"
62#define ASUS_LAPTOP_CLASS "hotkey"
63#define ASUS_LAPTOP_DEVICE_NAME "Hotkey"
64#define ASUS_LAPTOP_FILE KBUILD_MODNAME
65#define ASUS_LAPTOP_PREFIX "\\_SB.ATKD."
Corentin Chary85091b72007-01-26 14:04:30 +010066
Corentin Chary85091b72007-01-26 14:04:30 +010067MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
Corentin Chary50a90c42009-11-30 21:55:12 +010068MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
Corentin Chary85091b72007-01-26 14:04:30 +010069MODULE_LICENSE("GPL");
70
Corentin Charybe966662009-08-29 10:28:29 +020071/*
72 * WAPF defines the behavior of the Fn+Fx wlan key
Corentin Chary185e5af2007-03-11 10:27:33 +010073 * The significance of values is yet to be found, but
74 * most of the time:
Corentin Charyfddbfed2011-07-01 11:34:39 +020075 * Bit | Bluetooth | WLAN
76 * 0 | Hardware | Hardware
77 * 1 | Hardware | Software
78 * 4 | Software | Software
Corentin Chary185e5af2007-03-11 10:27:33 +010079 */
80static uint wapf = 1;
Axel Linc26d85c2010-07-20 15:19:55 -070081module_param(wapf, uint, 0444);
Corentin Chary185e5af2007-03-11 10:27:33 +010082MODULE_PARM_DESC(wapf, "WAPF value");
83
Corentin Chary774b0672011-12-15 08:27:34 +010084static char *wled_type = "unknown";
85static char *bled_type = "unknown";
86
87module_param(wled_type, charp, 0444);
88MODULE_PARM_DESC(wlan_status, "Set the wled type on boot "
89 "(unknown, led or rfkill). "
90 "default is unknown");
91
92module_param(bled_type, charp, 0444);
93MODULE_PARM_DESC(bled_type, "Set the bled type on boot "
94 "(unknown, led or rfkill). "
95 "default is unknown");
96
Dan Carpenter668f4a02010-04-06 13:44:29 +030097static int wlan_status = 1;
98static int bluetooth_status = 1;
Corentin Charyba1ff5b2010-11-14 17:40:12 +010099static int wimax_status = -1;
100static int wwan_status = -1;
Andy Rossabec04d2011-10-14 11:13:37 +0200101static int als_status;
Corentin Chary0e875f42010-01-10 20:49:26 +0100102
Axel Linc26d85c2010-07-20 15:19:55 -0700103module_param(wlan_status, int, 0444);
Corentin Charyd0930a22010-01-17 17:21:13 +0100104MODULE_PARM_DESC(wlan_status, "Set the wireless status on boot "
Corentin Chary0e875f42010-01-10 20:49:26 +0100105 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
Corentin Chary16cbf932011-10-14 11:13:40 +0200106 "default is -1");
Corentin Chary0e875f42010-01-10 20:49:26 +0100107
Axel Linc26d85c2010-07-20 15:19:55 -0700108module_param(bluetooth_status, int, 0444);
Corentin Chary0e875f42010-01-10 20:49:26 +0100109MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
110 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
Corentin Chary16cbf932011-10-14 11:13:40 +0200111 "default is -1");
Corentin Chary0e875f42010-01-10 20:49:26 +0100112
Corentin Charyba1ff5b2010-11-14 17:40:12 +0100113module_param(wimax_status, int, 0444);
114MODULE_PARM_DESC(wimax_status, "Set the wireless status on boot "
115 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
Corentin Chary16cbf932011-10-14 11:13:40 +0200116 "default is -1");
Corentin Charyba1ff5b2010-11-14 17:40:12 +0100117
118module_param(wwan_status, int, 0444);
119MODULE_PARM_DESC(wwan_status, "Set the wireless status on boot "
120 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
Corentin Chary16cbf932011-10-14 11:13:40 +0200121 "default is -1");
Corentin Charyba1ff5b2010-11-14 17:40:12 +0100122
Andy Rossabec04d2011-10-14 11:13:37 +0200123module_param(als_status, int, 0444);
124MODULE_PARM_DESC(als_status, "Set the ALS status on boot "
125 "(0 = disabled, 1 = enabled). "
126 "default is 0");
127
Corentin Charybe4ee822009-12-06 16:27:09 +0100128/*
129 * Some events we use, same for all Asus
130 */
Corentin Chary060cbce2010-01-28 10:52:40 +0100131#define ATKD_BR_UP 0x10 /* (event & ~ATKD_BR_UP) = brightness level */
132#define ATKD_BR_DOWN 0x20 /* (event & ~ATKD_BR_DOWN) = britghness level */
Corentin Charya539df52010-01-25 22:53:21 +0100133#define ATKD_BR_MIN ATKD_BR_UP
Corentin Chary060cbce2010-01-28 10:52:40 +0100134#define ATKD_BR_MAX (ATKD_BR_DOWN | 0xF) /* 0x2f */
Corentin Charybe4ee822009-12-06 16:27:09 +0100135#define ATKD_LCD_ON 0x33
136#define ATKD_LCD_OFF 0x34
137
138/*
139 * Known bits returned by \_SB.ATKD.HWRS
140 */
141#define WL_HWRS 0x80
142#define BT_HWRS 0x100
143
144/*
145 * Flags for hotk status
146 * WL_ON and BT_ON are also used for wireless_status()
147 */
Corentin Chary17e78f62010-01-13 22:21:33 +0100148#define WL_RSTS 0x01 /* internal Wifi */
149#define BT_RSTS 0x02 /* internal Bluetooth */
Corentin Charyba1ff5b2010-11-14 17:40:12 +0100150#define WM_RSTS 0x08 /* internal wimax */
151#define WW_RSTS 0x20 /* internal wwan */
Corentin Charybe4ee822009-12-06 16:27:09 +0100152
Corentin Chary774b0672011-12-15 08:27:34 +0100153/* WLED and BLED type */
154#define TYPE_UNKNOWN 0
155#define TYPE_LED 1
156#define TYPE_RFKILL 2
157
Corentin Charybe18cda2007-01-26 14:04:35 +0100158/* LED */
Corentin Charyd99b5772010-01-22 21:20:57 +0100159#define METHOD_MLED "MLED"
160#define METHOD_TLED "TLED"
161#define METHOD_RLED "RLED" /* W1JC */
162#define METHOD_PLED "PLED" /* A7J */
163#define METHOD_GLED "GLED" /* G1, G2 (probably) */
Corentin Charybe18cda2007-01-26 14:04:35 +0100164
Corentin Chary722ad972007-01-26 14:04:55 +0100165/* LEDD */
Corentin Charyd99b5772010-01-22 21:20:57 +0100166#define METHOD_LEDD "SLCM"
Corentin Chary722ad972007-01-26 14:04:55 +0100167
Corentin Charybe966662009-08-29 10:28:29 +0200168/*
169 * Bluetooth and WLAN
Corentin Chary4564de12007-01-26 14:04:40 +0100170 * WLED and BLED are not handled like other XLED, because in some dsdt
171 * they also control the WLAN/Bluetooth device.
172 */
Corentin Charyd99b5772010-01-22 21:20:57 +0100173#define METHOD_WLAN "WLED"
174#define METHOD_BLUETOOTH "BLED"
Corentin Charyba1ff5b2010-11-14 17:40:12 +0100175
176/* WWAN and WIMAX */
177#define METHOD_WWAN "GSMC"
178#define METHOD_WIMAX "WMXC"
179
Corentin Charyd99b5772010-01-22 21:20:57 +0100180#define METHOD_WL_STATUS "RSTS"
Corentin Chary4564de12007-01-26 14:04:40 +0100181
Corentin Chary6b7091e2007-01-26 14:04:45 +0100182/* Brightness */
Corentin Charyd99b5772010-01-22 21:20:57 +0100183#define METHOD_BRIGHTNESS_SET "SPLV"
184#define METHOD_BRIGHTNESS_GET "GPLV"
Corentin Chary6b7091e2007-01-26 14:04:45 +0100185
Corentin Chary78127b42007-01-26 14:04:49 +0100186/* Display */
Corentin Charyd99b5772010-01-22 21:20:57 +0100187#define METHOD_SWITCH_DISPLAY "SDSP"
Corentin Chary060cbce2010-01-28 10:52:40 +0100188
Corentin Charyd99b5772010-01-22 21:20:57 +0100189#define METHOD_ALS_CONTROL "ALSC" /* Z71A Z71V */
190#define METHOD_ALS_LEVEL "ALSL" /* Z71A Z71V */
Corentin Chary8b857352007-01-26 14:04:58 +0100191
Corentin Charye539c2f2007-05-06 14:47:06 +0200192/* GPS */
193/* R2H use different handle for GPS on/off */
Corentin Charyd99b5772010-01-22 21:20:57 +0100194#define METHOD_GPS_ON "SDON"
195#define METHOD_GPS_OFF "SDOF"
196#define METHOD_GPS_STATUS "GPST"
Corentin Charye539c2f2007-05-06 14:47:06 +0200197
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000198/* Keyboard light */
Corentin Charyd99b5772010-01-22 21:20:57 +0100199#define METHOD_KBD_LIGHT_SET "SLKB"
200#define METHOD_KBD_LIGHT_GET "GLKB"
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000201
Andy Ross8819de72011-10-14 11:13:35 +0200202/* For Pegatron Lucid tablet */
203#define DEVICE_NAME_PEGA "Lucid"
Andy Ross33989ba2011-10-14 11:13:36 +0200204
Andy Ross8819de72011-10-14 11:13:35 +0200205#define METHOD_PEGA_ENABLE "ENPR"
206#define METHOD_PEGA_DISABLE "DAPR"
Anisse Astier14908392011-10-14 11:13:42 +0200207#define PEGA_WLAN 0x00
208#define PEGA_BLUETOOTH 0x01
209#define PEGA_WWAN 0x02
Andy Ross33989ba2011-10-14 11:13:36 +0200210#define PEGA_ALS 0x04
211#define PEGA_ALS_POWER 0x05
212
Andy Ross8819de72011-10-14 11:13:35 +0200213#define METHOD_PEGA_READ "RDLN"
Andy Ross33989ba2011-10-14 11:13:36 +0200214#define PEGA_READ_ALS_H 0x02
215#define PEGA_READ_ALS_L 0x03
Andy Ross8819de72011-10-14 11:13:35 +0200216
Andy Rossb23910c2011-10-14 11:13:38 +0200217#define PEGA_ACCEL_NAME "pega_accel"
218#define PEGA_ACCEL_DESC "Pegatron Lucid Tablet Accelerometer"
219#define METHOD_XLRX "XLRX"
220#define METHOD_XLRY "XLRY"
221#define METHOD_XLRZ "XLRZ"
222#define PEGA_ACC_CLAMP 512 /* 1G accel is reported as ~256, so clamp to 2G */
223#define PEGA_ACC_RETRIES 3
224
Corentin Chary85091b72007-01-26 14:04:30 +0100225/*
Corentin Chary9129d142009-12-01 22:39:41 +0100226 * Define a specific led structure to keep the main structure clean
227 */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100228struct asus_led {
229 int wk;
230 struct work_struct work;
231 struct led_classdev led;
232 struct asus_laptop *asus;
233 const char *method;
Corentin Chary9129d142009-12-01 22:39:41 +0100234};
235
236/*
Anisse Astier14908392011-10-14 11:13:42 +0200237 * Same thing for rfkill
238 */
Corentin Chary40969c72011-12-15 08:27:33 +0100239struct asus_rfkill {
Corentin Chary774b0672011-12-15 08:27:34 +0100240 /* type of control. Maps to PEGA_* values or *_RSTS */
241 int control_id;
Anisse Astier14908392011-10-14 11:13:42 +0200242 struct rfkill *rfkill;
243 struct asus_laptop *asus;
244};
245
246/*
Corentin Chary85091b72007-01-26 14:04:30 +0100247 * This is the main structure, we can use it to store anything interesting
248 * about the hotk device
249 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100250struct asus_laptop {
Corentin Charybe966662009-08-29 10:28:29 +0200251 char *name; /* laptop name */
Corentin Chary600ad522009-11-30 21:42:42 +0100252
Corentin Chary7c247642009-11-30 22:13:54 +0100253 struct acpi_table_header *dsdt_info;
Corentin Chary600ad522009-11-30 21:42:42 +0100254 struct platform_device *platform_device;
Corentin Chary7c247642009-11-30 22:13:54 +0100255 struct acpi_device *device; /* the device we are in */
256 struct backlight_device *backlight_device;
Corentin Chary9129d142009-12-01 22:39:41 +0100257
Corentin Chary7c247642009-11-30 22:13:54 +0100258 struct input_dev *inputdev;
Corentin Chary9129d142009-12-01 22:39:41 +0100259 struct key_entry *keymap;
Andy Rossb23910c2011-10-14 11:13:38 +0200260 struct input_polled_dev *pega_accel_poll;
Corentin Chary9129d142009-12-01 22:39:41 +0100261
Corentin Chary774b0672011-12-15 08:27:34 +0100262 struct asus_led wled;
263 struct asus_led bled;
Corentin Charyaee0afb2010-01-26 21:01:34 +0100264 struct asus_led mled;
265 struct asus_led tled;
266 struct asus_led rled;
267 struct asus_led pled;
268 struct asus_led gled;
269 struct asus_led kled;
270 struct workqueue_struct *led_workqueue;
Corentin Chary7c247642009-11-30 22:13:54 +0100271
Corentin Chary774b0672011-12-15 08:27:34 +0100272 int wled_type;
273 int bled_type;
Corentin Charyaa9df932010-01-13 21:49:10 +0100274 int wireless_status;
275 bool have_rsts;
Andy Ross8819de72011-10-14 11:13:35 +0200276 bool is_pega_lucid;
Andy Rossb23910c2011-10-14 11:13:38 +0200277 bool pega_acc_live;
278 int pega_acc_x;
279 int pega_acc_y;
280 int pega_acc_z;
Corentin Charyaa9df932010-01-13 21:49:10 +0100281
Corentin Chary40969c72011-12-15 08:27:33 +0100282 struct asus_rfkill wlan;
283 struct asus_rfkill bluetooth;
284 struct asus_rfkill wwan;
285 struct asus_rfkill gps;
Anisse Astier14908392011-10-14 11:13:42 +0200286
Corentin Charybe966662009-08-29 10:28:29 +0200287 acpi_handle handle; /* the handle of the hotk device */
Corentin Charybe966662009-08-29 10:28:29 +0200288 u32 ledd_status; /* status of the LED display */
289 u8 light_level; /* light sensor level */
290 u8 light_switch; /* light sensor switch value */
291 u16 event_count[128]; /* count for each event TODO make this better */
Corentin Chary85091b72007-01-26 14:04:30 +0100292};
293
Corentin Chary9129d142009-12-01 22:39:41 +0100294static const struct key_entry asus_keymap[] = {
Corentin Charya539df52010-01-25 22:53:21 +0100295 /* Lenovo SL Specific keycodes */
Corentin Chary66a71dd2010-01-25 22:50:11 +0100296 {KE_KEY, 0x02, { KEY_SCREENLOCK } },
297 {KE_KEY, 0x05, { KEY_WLAN } },
298 {KE_KEY, 0x08, { KEY_F13 } },
299 {KE_KEY, 0x17, { KEY_ZOOM } },
300 {KE_KEY, 0x1f, { KEY_BATTERY } },
Corentin Charya539df52010-01-25 22:53:21 +0100301 /* End of Lenovo SL Specific keycodes */
Corentin Chary66a71dd2010-01-25 22:50:11 +0100302 {KE_KEY, 0x30, { KEY_VOLUMEUP } },
303 {KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
304 {KE_KEY, 0x32, { KEY_MUTE } },
305 {KE_KEY, 0x33, { KEY_SWITCHVIDEOMODE } },
306 {KE_KEY, 0x34, { KEY_SWITCHVIDEOMODE } },
307 {KE_KEY, 0x40, { KEY_PREVIOUSSONG } },
308 {KE_KEY, 0x41, { KEY_NEXTSONG } },
309 {KE_KEY, 0x43, { KEY_STOPCD } },
310 {KE_KEY, 0x45, { KEY_PLAYPAUSE } },
311 {KE_KEY, 0x4c, { KEY_MEDIA } },
312 {KE_KEY, 0x50, { KEY_EMAIL } },
313 {KE_KEY, 0x51, { KEY_WWW } },
314 {KE_KEY, 0x55, { KEY_CALC } },
315 {KE_KEY, 0x5C, { KEY_SCREENLOCK } }, /* Screenlock */
316 {KE_KEY, 0x5D, { KEY_WLAN } },
317 {KE_KEY, 0x5E, { KEY_WLAN } },
318 {KE_KEY, 0x5F, { KEY_WLAN } },
319 {KE_KEY, 0x60, { KEY_SWITCHVIDEOMODE } },
320 {KE_KEY, 0x61, { KEY_SWITCHVIDEOMODE } },
321 {KE_KEY, 0x62, { KEY_SWITCHVIDEOMODE } },
322 {KE_KEY, 0x63, { KEY_SWITCHVIDEOMODE } },
323 {KE_KEY, 0x6B, { KEY_F13 } }, /* Lock Touchpad */
Corentin Chary7f607d72010-01-17 17:37:19 +0100324 {KE_KEY, 0x7E, { KEY_BLUETOOTH } },
325 {KE_KEY, 0x7D, { KEY_BLUETOOTH } },
Corentin Chary66a71dd2010-01-25 22:50:11 +0100326 {KE_KEY, 0x82, { KEY_CAMERA } },
327 {KE_KEY, 0x88, { KEY_WLAN } },
328 {KE_KEY, 0x8A, { KEY_PROG1 } },
329 {KE_KEY, 0x95, { KEY_MEDIA } },
330 {KE_KEY, 0x99, { KEY_PHONE } },
331 {KE_KEY, 0xc4, { KEY_KBDILLUMUP } },
332 {KE_KEY, 0xc5, { KEY_KBDILLUMDOWN } },
Corentin Charyb58baecd2010-08-24 09:30:45 +0200333 {KE_KEY, 0xb5, { KEY_CALC } },
Corentin Chary034ce902009-01-20 16:17:43 +0100334 {KE_END, 0},
335};
336
Corentin Chary66a71dd2010-01-25 22:50:11 +0100337
Corentin Chary85091b72007-01-26 14:04:30 +0100338/*
339 * This function evaluates an ACPI method, given an int as parameter, the
340 * method is searched within the scope of the handle, can be NULL. The output
341 * of the method is written is output, which can also be NULL
342 *
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100343 * returns 0 if write is successful, -1 else.
Corentin Chary85091b72007-01-26 14:04:30 +0100344 */
Corentin Charyd8c67322009-11-28 10:27:51 +0100345static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
346 struct acpi_buffer *output)
Corentin Chary85091b72007-01-26 14:04:30 +0100347{
Corentin Charybe966662009-08-29 10:28:29 +0200348 struct acpi_object_list params; /* list of input parameters (an int) */
349 union acpi_object in_obj; /* the only param we use */
Corentin Chary85091b72007-01-26 14:04:30 +0100350 acpi_status status;
351
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100352 if (!handle)
Axel Lin9fb866f2010-07-20 15:19:47 -0700353 return -1;
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100354
Corentin Chary85091b72007-01-26 14:04:30 +0100355 params.count = 1;
356 params.pointer = &in_obj;
357 in_obj.type = ACPI_TYPE_INTEGER;
358 in_obj.integer.value = val;
359
360 status = acpi_evaluate_object(handle, (char *)method, &params, output);
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100361 if (status == AE_OK)
362 return 0;
363 else
364 return -1;
Corentin Chary85091b72007-01-26 14:04:30 +0100365}
366
Corentin Charyd8c67322009-11-28 10:27:51 +0100367static int write_acpi_int(acpi_handle handle, const char *method, int val)
368{
369 return write_acpi_int_ret(handle, method, val, NULL);
370}
371
Corentin Charyd99b5772010-01-22 21:20:57 +0100372static int acpi_check_handle(acpi_handle handle, const char *method,
373 acpi_handle *ret)
374{
375 acpi_status status;
376
377 if (method == NULL)
378 return -ENODEV;
379
380 if (ret)
381 status = acpi_get_handle(handle, (char *)method,
382 ret);
383 else {
384 acpi_handle dummy;
385
386 status = acpi_get_handle(handle, (char *)method,
387 &dummy);
388 }
389
390 if (status != AE_OK) {
391 if (ret)
Joe Perches5ad77dc2011-03-29 15:21:35 -0700392 pr_warn("Error finding %s\n", method);
Corentin Charyd99b5772010-01-22 21:20:57 +0100393 return -ENODEV;
394 }
395 return 0;
396}
397
Andy Ross8819de72011-10-14 11:13:35 +0200398static bool asus_check_pega_lucid(struct asus_laptop *asus)
399{
400 return !strcmp(asus->name, DEVICE_NAME_PEGA) &&
401 !acpi_check_handle(asus->handle, METHOD_PEGA_ENABLE, NULL) &&
402 !acpi_check_handle(asus->handle, METHOD_PEGA_DISABLE, NULL) &&
403 !acpi_check_handle(asus->handle, METHOD_PEGA_READ, NULL);
404}
405
Andy Ross33989ba2011-10-14 11:13:36 +0200406static int asus_pega_lucid_set(struct asus_laptop *asus, int unit, bool enable)
407{
408 char *method = enable ? METHOD_PEGA_ENABLE : METHOD_PEGA_DISABLE;
409 return write_acpi_int(asus->handle, method, unit);
410}
411
Andy Rossb23910c2011-10-14 11:13:38 +0200412static int pega_acc_axis(struct asus_laptop *asus, int curr, char *method)
413{
414 int i, delta;
415 unsigned long long val;
416 for (i = 0; i < PEGA_ACC_RETRIES; i++) {
417 acpi_evaluate_integer(asus->handle, method, NULL, &val);
418
419 /* The output is noisy. From reading the ASL
420 * dissassembly, timeout errors are returned with 1's
421 * in the high word, and the lack of locking around
422 * thei hi/lo byte reads means that a transition
423 * between (for example) -1 and 0 could be read as
424 * 0xff00 or 0x00ff. */
425 delta = abs(curr - (short)val);
426 if (delta < 128 && !(val & ~0xffff))
427 break;
428 }
429 return clamp_val((short)val, -PEGA_ACC_CLAMP, PEGA_ACC_CLAMP);
430}
431
432static void pega_accel_poll(struct input_polled_dev *ipd)
433{
434 struct device *parent = ipd->input->dev.parent;
435 struct asus_laptop *asus = dev_get_drvdata(parent);
436
437 /* In some cases, the very first call to poll causes a
438 * recursive fault under the polldev worker. This is
439 * apparently related to very early userspace access to the
440 * device, and perhaps a firmware bug. Fake the first report. */
441 if (!asus->pega_acc_live) {
442 asus->pega_acc_live = true;
443 input_report_abs(ipd->input, ABS_X, 0);
444 input_report_abs(ipd->input, ABS_Y, 0);
445 input_report_abs(ipd->input, ABS_Z, 0);
446 input_sync(ipd->input);
447 return;
448 }
449
450 asus->pega_acc_x = pega_acc_axis(asus, asus->pega_acc_x, METHOD_XLRX);
451 asus->pega_acc_y = pega_acc_axis(asus, asus->pega_acc_y, METHOD_XLRY);
452 asus->pega_acc_z = pega_acc_axis(asus, asus->pega_acc_z, METHOD_XLRZ);
453
454 /* Note transform, convert to "right/up/out" in the native
455 * landscape orientation (i.e. the vector is the direction of
456 * "real up" in the device's cartiesian coordinates). */
457 input_report_abs(ipd->input, ABS_X, -asus->pega_acc_x);
458 input_report_abs(ipd->input, ABS_Y, -asus->pega_acc_y);
459 input_report_abs(ipd->input, ABS_Z, asus->pega_acc_z);
460 input_sync(ipd->input);
461}
462
463static void pega_accel_exit(struct asus_laptop *asus)
464{
465 if (asus->pega_accel_poll) {
466 input_unregister_polled_device(asus->pega_accel_poll);
467 input_free_polled_device(asus->pega_accel_poll);
468 }
469 asus->pega_accel_poll = NULL;
470}
471
472static int pega_accel_init(struct asus_laptop *asus)
473{
474 int err;
475 struct input_polled_dev *ipd;
476
477 if (!asus->is_pega_lucid)
478 return -ENODEV;
479
480 if (acpi_check_handle(asus->handle, METHOD_XLRX, NULL) ||
481 acpi_check_handle(asus->handle, METHOD_XLRY, NULL) ||
482 acpi_check_handle(asus->handle, METHOD_XLRZ, NULL))
483 return -ENODEV;
484
485 ipd = input_allocate_polled_device();
486 if (!ipd)
487 return -ENOMEM;
488
489 ipd->poll = pega_accel_poll;
490 ipd->poll_interval = 125;
491 ipd->poll_interval_min = 50;
492 ipd->poll_interval_max = 2000;
493
494 ipd->input->name = PEGA_ACCEL_DESC;
495 ipd->input->phys = PEGA_ACCEL_NAME "/input0";
496 ipd->input->dev.parent = &asus->platform_device->dev;
497 ipd->input->id.bustype = BUS_HOST;
498
499 set_bit(EV_ABS, ipd->input->evbit);
500 input_set_abs_params(ipd->input, ABS_X,
501 -PEGA_ACC_CLAMP, PEGA_ACC_CLAMP, 0, 0);
502 input_set_abs_params(ipd->input, ABS_Y,
503 -PEGA_ACC_CLAMP, PEGA_ACC_CLAMP, 0, 0);
504 input_set_abs_params(ipd->input, ABS_Z,
505 -PEGA_ACC_CLAMP, PEGA_ACC_CLAMP, 0, 0);
506
507 err = input_register_polled_device(ipd);
508 if (err)
509 goto exit;
510
511 asus->pega_accel_poll = ipd;
512 return 0;
513
514exit:
515 input_free_polled_device(ipd);
516 return err;
517}
518
Corentin Chary17e78f62010-01-13 22:21:33 +0100519/* Generic LED function */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100520static int asus_led_set(struct asus_laptop *asus, const char *method,
Corentin Chary17e78f62010-01-13 22:21:33 +0100521 int value)
Corentin Charybe18cda2007-01-26 14:04:35 +0100522{
Corentin Charyd99b5772010-01-22 21:20:57 +0100523 if (!strcmp(method, METHOD_MLED))
Corentin Chary17e78f62010-01-13 22:21:33 +0100524 value = !value;
Corentin Charyd99b5772010-01-22 21:20:57 +0100525 else if (!strcmp(method, METHOD_GLED))
Corentin Chary17e78f62010-01-13 22:21:33 +0100526 value = !value + 1;
527 else
528 value = !!value;
Corentin Charybe18cda2007-01-26 14:04:35 +0100529
Corentin Charye5593bf2010-01-17 17:20:11 +0100530 return write_acpi_int(asus->handle, method, value);
Corentin Charybe18cda2007-01-26 14:04:35 +0100531}
532
Corentin Charybe4ee822009-12-06 16:27:09 +0100533/*
534 * LEDs
535 */
Corentin Charybe18cda2007-01-26 14:04:35 +0100536/* /sys/class/led handlers */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100537static void asus_led_cdev_set(struct led_classdev *led_cdev,
538 enum led_brightness value)
539{
540 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
541 struct asus_laptop *asus = led->asus;
Corentin Charybe18cda2007-01-26 14:04:35 +0100542
Corentin Charyaee0afb2010-01-26 21:01:34 +0100543 led->wk = !!value;
544 queue_work(asus->led_workqueue, &led->work);
545}
546
547static void asus_led_cdev_update(struct work_struct *work)
548{
549 struct asus_led *led = container_of(work, struct asus_led, work);
550 struct asus_laptop *asus = led->asus;
551
552 asus_led_set(asus, led->method, led->wk);
553}
554
555static enum led_brightness asus_led_cdev_get(struct led_classdev *led_cdev)
556{
557 return led_cdev->brightness;
558}
Corentin Charybe18cda2007-01-26 14:04:35 +0100559
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000560/*
Corentin Charybe4ee822009-12-06 16:27:09 +0100561 * Keyboard backlight (also a LED)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000562 */
Corentin Charyd99b5772010-01-22 21:20:57 +0100563static int asus_kled_lvl(struct asus_laptop *asus)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000564{
565 unsigned long long kblv;
566 struct acpi_object_list params;
567 union acpi_object in_obj;
568 acpi_status rv;
569
570 params.count = 1;
571 params.pointer = &in_obj;
572 in_obj.type = ACPI_TYPE_INTEGER;
573 in_obj.integer.value = 2;
574
Corentin Charyd99b5772010-01-22 21:20:57 +0100575 rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
576 &params, &kblv);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000577 if (ACPI_FAILURE(rv)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700578 pr_warn("Error reading kled level\n");
Corentin Chary4d441512010-01-13 22:26:24 +0100579 return -ENODEV;
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000580 }
581 return kblv;
582}
583
Corentin Chary4d441512010-01-13 22:26:24 +0100584static int asus_kled_set(struct asus_laptop *asus, int kblv)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000585{
586 if (kblv > 0)
587 kblv = (1 << 7) | (kblv & 0x7F);
588 else
589 kblv = 0;
590
Corentin Charyd99b5772010-01-22 21:20:57 +0100591 if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700592 pr_warn("Keyboard LED display write failed\n");
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000593 return -EINVAL;
594 }
595 return 0;
596}
597
Corentin Charyaee0afb2010-01-26 21:01:34 +0100598static void asus_kled_cdev_set(struct led_classdev *led_cdev,
599 enum led_brightness value)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000600{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100601 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
602 struct asus_laptop *asus = led->asus;
Corentin Chary9129d142009-12-01 22:39:41 +0100603
Corentin Chary060cbce2010-01-28 10:52:40 +0100604 led->wk = value;
Corentin Charyaee0afb2010-01-26 21:01:34 +0100605 queue_work(asus->led_workqueue, &led->work);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000606}
607
Corentin Charyaee0afb2010-01-26 21:01:34 +0100608static void asus_kled_cdev_update(struct work_struct *work)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000609{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100610 struct asus_led *led = container_of(work, struct asus_led, work);
611 struct asus_laptop *asus = led->asus;
Corentin Chary9129d142009-12-01 22:39:41 +0100612
Corentin Charyaee0afb2010-01-26 21:01:34 +0100613 asus_kled_set(asus, led->wk);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000614}
615
Corentin Charyaee0afb2010-01-26 21:01:34 +0100616static enum led_brightness asus_kled_cdev_get(struct led_classdev *led_cdev)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000617{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100618 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
619 struct asus_laptop *asus = led->asus;
Corentin Charyd99b5772010-01-22 21:20:57 +0100620
621 return asus_kled_lvl(asus);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000622}
623
Corentin Charybe4ee822009-12-06 16:27:09 +0100624static void asus_led_exit(struct asus_laptop *asus)
625{
Corentin Chary774b0672011-12-15 08:27:34 +0100626 if (!IS_ERR_OR_NULL(asus->wled.led.dev))
627 led_classdev_unregister(&asus->wled.led);
628 if (!IS_ERR_OR_NULL(asus->bled.led.dev))
629 led_classdev_unregister(&asus->bled.led);
Axel Lin8fcf71a2011-08-08 17:17:18 +0800630 if (!IS_ERR_OR_NULL(asus->mled.led.dev))
Corentin Charyaee0afb2010-01-26 21:01:34 +0100631 led_classdev_unregister(&asus->mled.led);
Axel Lin8fcf71a2011-08-08 17:17:18 +0800632 if (!IS_ERR_OR_NULL(asus->tled.led.dev))
Corentin Charyaee0afb2010-01-26 21:01:34 +0100633 led_classdev_unregister(&asus->tled.led);
Axel Lin8fcf71a2011-08-08 17:17:18 +0800634 if (!IS_ERR_OR_NULL(asus->pled.led.dev))
Corentin Charyaee0afb2010-01-26 21:01:34 +0100635 led_classdev_unregister(&asus->pled.led);
Axel Lin8fcf71a2011-08-08 17:17:18 +0800636 if (!IS_ERR_OR_NULL(asus->rled.led.dev))
Corentin Charyaee0afb2010-01-26 21:01:34 +0100637 led_classdev_unregister(&asus->rled.led);
Axel Lin8fcf71a2011-08-08 17:17:18 +0800638 if (!IS_ERR_OR_NULL(asus->gled.led.dev))
Corentin Charyaee0afb2010-01-26 21:01:34 +0100639 led_classdev_unregister(&asus->gled.led);
Axel Lin8fcf71a2011-08-08 17:17:18 +0800640 if (!IS_ERR_OR_NULL(asus->kled.led.dev))
Corentin Charyaee0afb2010-01-26 21:01:34 +0100641 led_classdev_unregister(&asus->kled.led);
642 if (asus->led_workqueue) {
643 destroy_workqueue(asus->led_workqueue);
644 asus->led_workqueue = NULL;
Corentin Charybe4ee822009-12-06 16:27:09 +0100645 }
646}
647
648/* Ugly macro, need to fix that later */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100649static int asus_led_register(struct asus_laptop *asus,
650 struct asus_led *led,
651 const char *name, const char *method)
652{
653 struct led_classdev *led_cdev = &led->led;
654
655 if (!method || acpi_check_handle(asus->handle, method, NULL))
Corentin Chary060cbce2010-01-28 10:52:40 +0100656 return 0; /* Led not present */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100657
658 led->asus = asus;
659 led->method = method;
660
661 INIT_WORK(&led->work, asus_led_cdev_update);
662 led_cdev->name = name;
663 led_cdev->brightness_set = asus_led_cdev_set;
664 led_cdev->brightness_get = asus_led_cdev_get;
665 led_cdev->max_brightness = 1;
666 return led_classdev_register(&asus->platform_device->dev, led_cdev);
667}
Corentin Charybe4ee822009-12-06 16:27:09 +0100668
669static int asus_led_init(struct asus_laptop *asus)
670{
Corentin Chary774b0672011-12-15 08:27:34 +0100671 int r = 0;
Corentin Charybe4ee822009-12-06 16:27:09 +0100672
673 /*
Corentin Chary8d38e422011-10-14 11:13:39 +0200674 * The Pegatron Lucid has no physical leds, but all methods are
675 * available in the DSDT...
676 */
677 if (asus->is_pega_lucid)
678 return 0;
679
680 /*
Corentin Charybe4ee822009-12-06 16:27:09 +0100681 * Functions that actually update the LED's are called from a
682 * workqueue. By doing this as separate work rather than when the LED
683 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
684 * potentially bad time, such as a timer interrupt.
685 */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100686 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
687 if (!asus->led_workqueue)
Corentin Charybe4ee822009-12-06 16:27:09 +0100688 return -ENOMEM;
689
Corentin Chary774b0672011-12-15 08:27:34 +0100690 if (asus->wled_type == TYPE_LED)
691 r = asus_led_register(asus, &asus->wled, "asus::wlan",
692 METHOD_WLAN);
693 if (r)
694 goto error;
695 if (asus->bled_type == TYPE_LED)
696 r = asus_led_register(asus, &asus->bled, "asus::bluetooth",
697 METHOD_BLUETOOTH);
698 if (r)
699 goto error;
Corentin Charyaee0afb2010-01-26 21:01:34 +0100700 r = asus_led_register(asus, &asus->mled, "asus::mail", METHOD_MLED);
701 if (r)
702 goto error;
703 r = asus_led_register(asus, &asus->tled, "asus::touchpad", METHOD_TLED);
704 if (r)
705 goto error;
706 r = asus_led_register(asus, &asus->rled, "asus::record", METHOD_RLED);
707 if (r)
708 goto error;
709 r = asus_led_register(asus, &asus->pled, "asus::phone", METHOD_PLED);
710 if (r)
711 goto error;
712 r = asus_led_register(asus, &asus->gled, "asus::gaming", METHOD_GLED);
713 if (r)
714 goto error;
Corentin Charyd99b5772010-01-22 21:20:57 +0100715 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
Corentin Charyaee0afb2010-01-26 21:01:34 +0100716 !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL)) {
717 struct asus_led *led = &asus->kled;
718 struct led_classdev *cdev = &led->led;
719
720 led->asus = asus;
721
722 INIT_WORK(&led->work, asus_kled_cdev_update);
723 cdev->name = "asus::kbd_backlight";
724 cdev->brightness_set = asus_kled_cdev_set;
725 cdev->brightness_get = asus_kled_cdev_get;
726 cdev->max_brightness = 3;
727 r = led_classdev_register(&asus->platform_device->dev, cdev);
728 }
Corentin Charybe4ee822009-12-06 16:27:09 +0100729error:
Corentin Charyaee0afb2010-01-26 21:01:34 +0100730 if (r)
Corentin Charybe4ee822009-12-06 16:27:09 +0100731 asus_led_exit(asus);
Corentin Charyaee0afb2010-01-26 21:01:34 +0100732 return r;
Corentin Charybe4ee822009-12-06 16:27:09 +0100733}
734
735/*
736 * Backlight device
737 */
Corentin Chary4d441512010-01-13 22:26:24 +0100738static int asus_read_brightness(struct backlight_device *bd)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100739{
Corentin Charyd99b5772010-01-22 21:20:57 +0100740 struct asus_laptop *asus = bl_get_data(bd);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400741 unsigned long long value;
Corentin Chary9a816852007-03-11 10:25:38 +0100742 acpi_status rv = AE_OK;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100743
Corentin Charyd99b5772010-01-22 21:20:57 +0100744 rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
745 NULL, &value);
Corentin Chary9a816852007-03-11 10:25:38 +0100746 if (ACPI_FAILURE(rv))
Joe Perches5ad77dc2011-03-29 15:21:35 -0700747 pr_warn("Error reading brightness\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100748
749 return value;
750}
751
Corentin Chary4d441512010-01-13 22:26:24 +0100752static int asus_set_brightness(struct backlight_device *bd, int value)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100753{
Corentin Charyd99b5772010-01-22 21:20:57 +0100754 struct asus_laptop *asus = bl_get_data(bd);
755
756 if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700757 pr_warn("Error changing brightness\n");
Corentin Charye5b50f62009-11-28 10:19:55 +0100758 return -EIO;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100759 }
Corentin Charye5b50f62009-11-28 10:19:55 +0100760 return 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100761}
762
763static int update_bl_status(struct backlight_device *bd)
764{
Richard Purdie599a52d2007-02-10 23:07:48 +0000765 int value = bd->props.brightness;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100766
Corentin Chary3b81cf92011-02-06 13:27:31 +0100767 return asus_set_brightness(bd, value);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100768}
769
Lionel Debrouxacc24722010-11-16 14:14:02 +0100770static const struct backlight_ops asusbl_ops = {
Corentin Chary4d441512010-01-13 22:26:24 +0100771 .get_brightness = asus_read_brightness,
Corentin Charybe4ee822009-12-06 16:27:09 +0100772 .update_status = update_bl_status,
773};
774
Corentin Charya539df52010-01-25 22:53:21 +0100775static int asus_backlight_notify(struct asus_laptop *asus)
776{
777 struct backlight_device *bd = asus->backlight_device;
778 int old = bd->props.brightness;
779
780 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
781
782 return old;
783}
784
Corentin Charybe4ee822009-12-06 16:27:09 +0100785static int asus_backlight_init(struct asus_laptop *asus)
786{
787 struct backlight_device *bd;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500788 struct backlight_properties props;
Corentin Charybe4ee822009-12-06 16:27:09 +0100789
Corentin Chary71e687d2010-08-24 09:30:44 +0200790 if (acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) ||
Corentin Chary3b81cf92011-02-06 13:27:31 +0100791 acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL))
Corentin Chary71e687d2010-08-24 09:30:44 +0200792 return 0;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500793
Corentin Chary71e687d2010-08-24 09:30:44 +0200794 memset(&props, 0, sizeof(struct backlight_properties));
795 props.max_brightness = 15;
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700796 props.type = BACKLIGHT_PLATFORM;
Corentin Charybe4ee822009-12-06 16:27:09 +0100797
Corentin Chary71e687d2010-08-24 09:30:44 +0200798 bd = backlight_device_register(ASUS_LAPTOP_FILE,
799 &asus->platform_device->dev, asus,
800 &asusbl_ops, &props);
801 if (IS_ERR(bd)) {
802 pr_err("Could not register asus backlight device\n");
803 asus->backlight_device = NULL;
804 return PTR_ERR(bd);
Corentin Charybe4ee822009-12-06 16:27:09 +0100805 }
Corentin Chary71e687d2010-08-24 09:30:44 +0200806
807 asus->backlight_device = bd;
808 bd->props.brightness = asus_read_brightness(bd);
809 bd->props.power = FB_BLANK_UNBLANK;
810 backlight_update_status(bd);
Corentin Charybe4ee822009-12-06 16:27:09 +0100811 return 0;
812}
813
814static void asus_backlight_exit(struct asus_laptop *asus)
815{
816 if (asus->backlight_device)
817 backlight_device_unregister(asus->backlight_device);
Corentin Charya539df52010-01-25 22:53:21 +0100818 asus->backlight_device = NULL;
Corentin Charybe4ee822009-12-06 16:27:09 +0100819}
820
Corentin Chary85091b72007-01-26 14:04:30 +0100821/*
822 * Platform device handlers
823 */
824
825/*
826 * We write our info in page, we begin at offset off and cannot write more
827 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
828 * number of bytes written in page
829 */
830static ssize_t show_infos(struct device *dev,
Len Brown8def05f2007-01-30 01:46:43 -0500831 struct device_attribute *attr, char *page)
Corentin Chary85091b72007-01-26 14:04:30 +0100832{
Corentin Chary9129d142009-12-01 22:39:41 +0100833 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary85091b72007-01-26 14:04:30 +0100834 int len = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400835 unsigned long long temp;
Corentin Charybe966662009-08-29 10:28:29 +0200836 char buf[16]; /* enough for all info */
Corentin Chary9a816852007-03-11 10:25:38 +0100837 acpi_status rv = AE_OK;
838
Corentin Chary85091b72007-01-26 14:04:30 +0100839 /*
Corentin Chary060cbce2010-01-28 10:52:40 +0100840 * We use the easy way, we don't care of off and count,
841 * so we don't set eof to 1
Corentin Chary85091b72007-01-26 14:04:30 +0100842 */
843
Corentin Chary50a90c42009-11-30 21:55:12 +0100844 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
845 len += sprintf(page + len, "Model reference : %s\n", asus->name);
Corentin Chary85091b72007-01-26 14:04:30 +0100846 /*
847 * The SFUN method probably allows the original driver to get the list
848 * of features supported by a given model. For now, 0x0100 or 0x0800
849 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
850 * The significance of others is yet to be found.
851 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100852 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100853 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000854 len += sprintf(page + len, "SFUN value : %#x\n",
855 (uint) temp);
856 /*
857 * The HWRS method return informations about the hardware.
858 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
859 * The significance of others is yet to be found.
860 * If we don't find the method, we assume the device are present.
861 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100862 rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
Corentin Chary1d4a3802009-08-28 12:56:46 +0000863 if (!ACPI_FAILURE(rv))
864 len += sprintf(page + len, "HRWS value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100865 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100866 /*
867 * Another value for userspace: the ASYM method returns 0x02 for
868 * battery low and 0x04 for battery critical, its readings tend to be
869 * more accurate than those provided by _BST.
870 * Note: since not all the laptops provide this method, errors are
871 * silently ignored.
872 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100873 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100874 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000875 len += sprintf(page + len, "ASYM value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100876 (uint) temp);
Corentin Chary7c247642009-11-30 22:13:54 +0100877 if (asus->dsdt_info) {
878 snprintf(buf, 16, "%d", asus->dsdt_info->length);
Corentin Chary85091b72007-01-26 14:04:30 +0100879 len += sprintf(page + len, "DSDT length : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100880 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
Corentin Chary85091b72007-01-26 14:04:30 +0100881 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100882 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100883 len += sprintf(page + len, "DSDT revision : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100884 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100885 len += sprintf(page + len, "OEM id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100886 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100887 len += sprintf(page + len, "OEM table id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100888 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100889 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100890 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100891 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100892 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100893 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
894 }
895
896 return len;
897}
898
899static int parse_arg(const char *buf, unsigned long count, int *val)
900{
901 if (!count)
902 return 0;
903 if (count > 31)
904 return -EINVAL;
905 if (sscanf(buf, "%i", val) != 1)
906 return -EINVAL;
907 return count;
908}
909
Corentin Chary17e78f62010-01-13 22:21:33 +0100910static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
911 const char *buf, size_t count,
Corentin Charyd99b5772010-01-22 21:20:57 +0100912 const char *method)
Corentin Chary4564de12007-01-26 14:04:40 +0100913{
914 int rv, value;
915 int out = 0;
916
917 rv = parse_arg(buf, count, &value);
918 if (rv > 0)
919 out = value ? 1 : 0;
920
Corentin Charyd99b5772010-01-22 21:20:57 +0100921 if (write_acpi_int(asus->handle, method, value))
Corentin Chary17e78f62010-01-13 22:21:33 +0100922 return -ENODEV;
Corentin Chary4564de12007-01-26 14:04:40 +0100923 return rv;
924}
925
926/*
Corentin Chary722ad972007-01-26 14:04:55 +0100927 * LEDD display
928 */
929static ssize_t show_ledd(struct device *dev,
930 struct device_attribute *attr, char *buf)
931{
Corentin Chary9129d142009-12-01 22:39:41 +0100932 struct asus_laptop *asus = dev_get_drvdata(dev);
933
Corentin Chary50a90c42009-11-30 21:55:12 +0100934 return sprintf(buf, "0x%08x\n", asus->ledd_status);
Corentin Chary722ad972007-01-26 14:04:55 +0100935}
936
937static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
938 const char *buf, size_t count)
939{
Corentin Chary9129d142009-12-01 22:39:41 +0100940 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary722ad972007-01-26 14:04:55 +0100941 int rv, value;
942
943 rv = parse_arg(buf, count, &value);
944 if (rv > 0) {
Axel Lin6a984a02010-07-20 15:19:48 -0700945 if (write_acpi_int(asus->handle, METHOD_LEDD, value)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700946 pr_warn("LED display write failed\n");
Axel Lin6a984a02010-07-20 15:19:48 -0700947 return -ENODEV;
948 }
949 asus->ledd_status = (u32) value;
Corentin Chary722ad972007-01-26 14:04:55 +0100950 }
951 return rv;
952}
953
954/*
Corentin Charyaa9df932010-01-13 21:49:10 +0100955 * Wireless
956 */
957static int asus_wireless_status(struct asus_laptop *asus, int mask)
958{
959 unsigned long long status;
960 acpi_status rv = AE_OK;
961
962 if (!asus->have_rsts)
963 return (asus->wireless_status & mask) ? 1 : 0;
964
Corentin Charyd99b5772010-01-22 21:20:57 +0100965 rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
966 NULL, &status);
Corentin Charyaa9df932010-01-13 21:49:10 +0100967 if (ACPI_FAILURE(rv)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700968 pr_warn("Error reading Wireless status\n");
Corentin Charyaa9df932010-01-13 21:49:10 +0100969 return -EINVAL;
970 }
971 return !!(status & mask);
972}
973
974/*
Corentin Chary4564de12007-01-26 14:04:40 +0100975 * WLAN
976 */
Corentin Charye5593bf2010-01-17 17:20:11 +0100977static int asus_wlan_set(struct asus_laptop *asus, int status)
978{
979 if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700980 pr_warn("Error setting wlan status to %d\n", status);
Corentin Charye5593bf2010-01-17 17:20:11 +0100981 return -EIO;
982 }
983 return 0;
984}
985
Corentin Chary4564de12007-01-26 14:04:40 +0100986static ssize_t show_wlan(struct device *dev,
987 struct device_attribute *attr, char *buf)
988{
Corentin Chary9129d142009-12-01 22:39:41 +0100989 struct asus_laptop *asus = dev_get_drvdata(dev);
990
Corentin Chary17e78f62010-01-13 22:21:33 +0100991 return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100992}
993
994static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
995 const char *buf, size_t count)
996{
Corentin Chary9129d142009-12-01 22:39:41 +0100997 struct asus_laptop *asus = dev_get_drvdata(dev);
998
Corentin Charyd99b5772010-01-22 21:20:57 +0100999 return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
Corentin Chary4564de12007-01-26 14:04:40 +01001000}
1001
Corentin Chary774b0672011-12-15 08:27:34 +01001002/*e
Corentin Chary4564de12007-01-26 14:04:40 +01001003 * Bluetooth
1004 */
Corentin Charye5593bf2010-01-17 17:20:11 +01001005static int asus_bluetooth_set(struct asus_laptop *asus, int status)
1006{
1007 if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -07001008 pr_warn("Error setting bluetooth status to %d\n", status);
Corentin Charye5593bf2010-01-17 17:20:11 +01001009 return -EIO;
1010 }
1011 return 0;
1012}
1013
Corentin Chary4564de12007-01-26 14:04:40 +01001014static ssize_t show_bluetooth(struct device *dev,
1015 struct device_attribute *attr, char *buf)
1016{
Corentin Chary9129d142009-12-01 22:39:41 +01001017 struct asus_laptop *asus = dev_get_drvdata(dev);
1018
Corentin Chary17e78f62010-01-13 22:21:33 +01001019 return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +01001020}
1021
Len Brown8def05f2007-01-30 01:46:43 -05001022static ssize_t store_bluetooth(struct device *dev,
1023 struct device_attribute *attr, const char *buf,
1024 size_t count)
Corentin Chary4564de12007-01-26 14:04:40 +01001025{
Corentin Chary9129d142009-12-01 22:39:41 +01001026 struct asus_laptop *asus = dev_get_drvdata(dev);
1027
Corentin Charyd99b5772010-01-22 21:20:57 +01001028 return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
Corentin Chary4564de12007-01-26 14:04:40 +01001029}
1030
Corentin Chary78127b42007-01-26 14:04:49 +01001031/*
Corentin Charyba1ff5b2010-11-14 17:40:12 +01001032 * Wimax
1033 */
1034static int asus_wimax_set(struct asus_laptop *asus, int status)
1035{
1036 if (write_acpi_int(asus->handle, METHOD_WIMAX, !!status)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -07001037 pr_warn("Error setting wimax status to %d\n", status);
Corentin Charyba1ff5b2010-11-14 17:40:12 +01001038 return -EIO;
1039 }
1040 return 0;
1041}
1042
1043static ssize_t show_wimax(struct device *dev,
1044 struct device_attribute *attr, char *buf)
1045{
1046 struct asus_laptop *asus = dev_get_drvdata(dev);
1047
1048 return sprintf(buf, "%d\n", asus_wireless_status(asus, WM_RSTS));
1049}
1050
1051static ssize_t store_wimax(struct device *dev,
1052 struct device_attribute *attr, const char *buf,
1053 size_t count)
1054{
1055 struct asus_laptop *asus = dev_get_drvdata(dev);
1056
1057 return sysfs_acpi_set(asus, buf, count, METHOD_WIMAX);
1058}
1059
1060/*
1061 * Wwan
1062 */
1063static int asus_wwan_set(struct asus_laptop *asus, int status)
1064{
1065 if (write_acpi_int(asus->handle, METHOD_WWAN, !!status)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -07001066 pr_warn("Error setting wwan status to %d\n", status);
Corentin Charyba1ff5b2010-11-14 17:40:12 +01001067 return -EIO;
1068 }
1069 return 0;
1070}
1071
1072static ssize_t show_wwan(struct device *dev,
1073 struct device_attribute *attr, char *buf)
1074{
1075 struct asus_laptop *asus = dev_get_drvdata(dev);
1076
1077 return sprintf(buf, "%d\n", asus_wireless_status(asus, WW_RSTS));
1078}
1079
1080static ssize_t store_wwan(struct device *dev,
1081 struct device_attribute *attr, const char *buf,
1082 size_t count)
1083{
1084 struct asus_laptop *asus = dev_get_drvdata(dev);
1085
1086 return sysfs_acpi_set(asus, buf, count, METHOD_WWAN);
1087}
1088
1089/*
Corentin Chary78127b42007-01-26 14:04:49 +01001090 * Display
1091 */
Corentin Chary4d441512010-01-13 22:26:24 +01001092static void asus_set_display(struct asus_laptop *asus, int value)
Corentin Chary78127b42007-01-26 14:04:49 +01001093{
1094 /* no sanity check needed for now */
Corentin Charyd99b5772010-01-22 21:20:57 +01001095 if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
Joe Perches5ad77dc2011-03-29 15:21:35 -07001096 pr_warn("Error setting display\n");
Corentin Chary78127b42007-01-26 14:04:49 +01001097 return;
1098}
1099
Corentin Chary78127b42007-01-26 14:04:49 +01001100/*
1101 * Experimental support for display switching. As of now: 1 should activate
1102 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
1103 * Any combination (bitwise) of these will suffice. I never actually tested 4
1104 * displays hooked up simultaneously, so be warned. See the acpi4asus README
1105 * for more info.
1106 */
1107static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
1108 const char *buf, size_t count)
1109{
Corentin Chary9129d142009-12-01 22:39:41 +01001110 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary78127b42007-01-26 14:04:49 +01001111 int rv, value;
1112
1113 rv = parse_arg(buf, count, &value);
1114 if (rv > 0)
Corentin Chary4d441512010-01-13 22:26:24 +01001115 asus_set_display(asus, value);
Corentin Chary78127b42007-01-26 14:04:49 +01001116 return rv;
1117}
1118
Corentin Chary8b857352007-01-26 14:04:58 +01001119/*
1120 * Light Sens
1121 */
Corentin Chary4d441512010-01-13 22:26:24 +01001122static void asus_als_switch(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +01001123{
Andy Ross33989ba2011-10-14 11:13:36 +02001124 int ret;
1125
1126 if (asus->is_pega_lucid) {
1127 ret = asus_pega_lucid_set(asus, PEGA_ALS, value);
1128 if (!ret)
1129 ret = asus_pega_lucid_set(asus, PEGA_ALS_POWER, value);
1130 } else {
1131 ret = write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value);
1132 }
1133 if (ret)
1134 pr_warning("Error setting light sensor switch\n");
1135
Corentin Chary50a90c42009-11-30 21:55:12 +01001136 asus->light_switch = value;
Corentin Chary8b857352007-01-26 14:04:58 +01001137}
1138
1139static ssize_t show_lssw(struct device *dev,
1140 struct device_attribute *attr, char *buf)
1141{
Corentin Chary9129d142009-12-01 22:39:41 +01001142 struct asus_laptop *asus = dev_get_drvdata(dev);
1143
Corentin Chary50a90c42009-11-30 21:55:12 +01001144 return sprintf(buf, "%d\n", asus->light_switch);
Corentin Chary8b857352007-01-26 14:04:58 +01001145}
1146
1147static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
1148 const char *buf, size_t count)
1149{
Corentin Chary9129d142009-12-01 22:39:41 +01001150 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +01001151 int rv, value;
1152
1153 rv = parse_arg(buf, count, &value);
1154 if (rv > 0)
Corentin Chary4d441512010-01-13 22:26:24 +01001155 asus_als_switch(asus, value ? 1 : 0);
Corentin Chary8b857352007-01-26 14:04:58 +01001156
1157 return rv;
1158}
1159
Corentin Chary4d441512010-01-13 22:26:24 +01001160static void asus_als_level(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +01001161{
Corentin Charyd99b5772010-01-22 21:20:57 +01001162 if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
Joe Perches5ad77dc2011-03-29 15:21:35 -07001163 pr_warn("Error setting light sensor level\n");
Corentin Chary50a90c42009-11-30 21:55:12 +01001164 asus->light_level = value;
Corentin Chary8b857352007-01-26 14:04:58 +01001165}
1166
1167static ssize_t show_lslvl(struct device *dev,
1168 struct device_attribute *attr, char *buf)
1169{
Corentin Chary9129d142009-12-01 22:39:41 +01001170 struct asus_laptop *asus = dev_get_drvdata(dev);
1171
Corentin Chary50a90c42009-11-30 21:55:12 +01001172 return sprintf(buf, "%d\n", asus->light_level);
Corentin Chary8b857352007-01-26 14:04:58 +01001173}
1174
1175static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
1176 const char *buf, size_t count)
1177{
Corentin Chary9129d142009-12-01 22:39:41 +01001178 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +01001179 int rv, value;
1180
1181 rv = parse_arg(buf, count, &value);
1182 if (rv > 0) {
1183 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
1184 /* 0 <= value <= 15 */
Corentin Chary4d441512010-01-13 22:26:24 +01001185 asus_als_level(asus, value);
Corentin Chary8b857352007-01-26 14:04:58 +01001186 }
1187
1188 return rv;
1189}
1190
Andy Ross33989ba2011-10-14 11:13:36 +02001191static int pega_int_read(struct asus_laptop *asus, int arg, int *result)
1192{
1193 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1194 int err = write_acpi_int_ret(asus->handle, METHOD_PEGA_READ, arg,
1195 &buffer);
1196 if (!err) {
1197 union acpi_object *obj = buffer.pointer;
1198 if (obj && obj->type == ACPI_TYPE_INTEGER)
1199 *result = obj->integer.value;
1200 else
1201 err = -EIO;
1202 }
1203 return err;
1204}
1205
1206static ssize_t show_lsvalue(struct device *dev,
1207 struct device_attribute *attr, char *buf)
1208{
1209 struct asus_laptop *asus = dev_get_drvdata(dev);
1210 int err, hi, lo;
1211
1212 err = pega_int_read(asus, PEGA_READ_ALS_H, &hi);
1213 if (!err)
1214 err = pega_int_read(asus, PEGA_READ_ALS_L, &lo);
1215 if (!err)
1216 return sprintf(buf, "%d\n", 10 * hi + lo);
1217 return err;
1218}
1219
Corentin Charye539c2f2007-05-06 14:47:06 +02001220/*
1221 * GPS
1222 */
Corentin Chary6358bf22010-01-13 21:55:44 +01001223static int asus_gps_status(struct asus_laptop *asus)
1224{
1225 unsigned long long status;
1226 acpi_status rv = AE_OK;
1227
Corentin Charyd99b5772010-01-22 21:20:57 +01001228 rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
1229 NULL, &status);
Corentin Chary6358bf22010-01-13 21:55:44 +01001230 if (ACPI_FAILURE(rv)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -07001231 pr_warn("Error reading GPS status\n");
Corentin Chary6358bf22010-01-13 21:55:44 +01001232 return -ENODEV;
1233 }
1234 return !!status;
1235}
1236
1237static int asus_gps_switch(struct asus_laptop *asus, int status)
1238{
Corentin Charyd99b5772010-01-22 21:20:57 +01001239 const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
Corentin Chary6358bf22010-01-13 21:55:44 +01001240
Corentin Charyd99b5772010-01-22 21:20:57 +01001241 if (write_acpi_int(asus->handle, meth, 0x02))
Corentin Chary6358bf22010-01-13 21:55:44 +01001242 return -ENODEV;
1243 return 0;
1244}
1245
Corentin Charye539c2f2007-05-06 14:47:06 +02001246static ssize_t show_gps(struct device *dev,
1247 struct device_attribute *attr, char *buf)
1248{
Corentin Chary9129d142009-12-01 22:39:41 +01001249 struct asus_laptop *asus = dev_get_drvdata(dev);
1250
Corentin Chary6358bf22010-01-13 21:55:44 +01001251 return sprintf(buf, "%d\n", asus_gps_status(asus));
Corentin Charye539c2f2007-05-06 14:47:06 +02001252}
1253
1254static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
1255 const char *buf, size_t count)
1256{
Corentin Chary060cbce2010-01-28 10:52:40 +01001257 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary6358bf22010-01-13 21:55:44 +01001258 int rv, value;
1259 int ret;
Corentin Chary9129d142009-12-01 22:39:41 +01001260
Corentin Chary6358bf22010-01-13 21:55:44 +01001261 rv = parse_arg(buf, count, &value);
1262 if (rv <= 0)
1263 return -EINVAL;
1264 ret = asus_gps_switch(asus, !!value);
1265 if (ret)
1266 return ret;
Corentin Chary40969c72011-12-15 08:27:33 +01001267 rfkill_set_sw_state(asus->gps.rfkill, !value);
Corentin Chary6358bf22010-01-13 21:55:44 +01001268 return rv;
Corentin Charye539c2f2007-05-06 14:47:06 +02001269}
1270
Corentin Chary034ce902009-01-20 16:17:43 +01001271/*
Corentin Chary18e13112010-01-25 23:29:24 +01001272 * rfkill
1273 */
1274static int asus_gps_rfkill_set(void *data, bool blocked)
1275{
Corentin Chary23f45c32010-08-24 09:30:46 +02001276 struct asus_laptop *asus = data;
Corentin Chary18e13112010-01-25 23:29:24 +01001277
Corentin Chary23f45c32010-08-24 09:30:46 +02001278 return asus_gps_switch(asus, !blocked);
Corentin Chary18e13112010-01-25 23:29:24 +01001279}
1280
1281static const struct rfkill_ops asus_gps_rfkill_ops = {
1282 .set_block = asus_gps_rfkill_set,
1283};
1284
Corentin Chary774b0672011-12-15 08:27:34 +01001285static int asus_rfkill_set(void *data, bool blocked)
1286{
1287 struct asus_rfkill *rfk = data;
1288 struct asus_laptop *asus = rfk->asus;
1289
1290 if (rfk->control_id == WL_RSTS)
1291 return asus_wlan_set(asus, !blocked);
1292 else if (rfk->control_id == BT_RSTS)
1293 return asus_bluetooth_set(asus, !blocked);
1294
1295 return -EINVAL;
1296}
1297
1298static const struct rfkill_ops asus_rfkill_ops = {
1299 .set_block = asus_rfkill_set,
1300};
1301
Corentin Chary40969c72011-12-15 08:27:33 +01001302static void asus_rfkill_terminate(struct asus_rfkill *rfk)
1303{
1304 if (!rfk->rfkill)
1305 return ;
1306
1307 rfkill_unregister(rfk->rfkill);
1308 rfkill_destroy(rfk->rfkill);
1309 rfk->rfkill = NULL;
1310}
1311
Corentin Chary18e13112010-01-25 23:29:24 +01001312static void asus_rfkill_exit(struct asus_laptop *asus)
1313{
Corentin Chary40969c72011-12-15 08:27:33 +01001314 asus_rfkill_terminate(&asus->wwan);
1315 asus_rfkill_terminate(&asus->bluetooth);
1316 asus_rfkill_terminate(&asus->wlan);
1317 asus_rfkill_terminate(&asus->gps);
Corentin Chary18e13112010-01-25 23:29:24 +01001318}
1319
Corentin Chary774b0672011-12-15 08:27:34 +01001320static int asus_rfkill_setup(struct asus_laptop *asus, struct asus_rfkill *rfk,
1321 const char *name, int control_id, int type,
1322 const struct rfkill_ops *ops)
Corentin Chary18e13112010-01-25 23:29:24 +01001323{
1324 int result;
1325
Corentin Chary774b0672011-12-15 08:27:34 +01001326 rfk->control_id = control_id;
1327 rfk->asus = asus;
1328 rfk->rfkill = rfkill_alloc(name, &asus->platform_device->dev,
1329 type, ops, rfk);
1330 if (!rfk->rfkill)
Corentin Chary18e13112010-01-25 23:29:24 +01001331 return -EINVAL;
1332
Corentin Chary774b0672011-12-15 08:27:34 +01001333 result = rfkill_register(rfk->rfkill);
Corentin Chary18e13112010-01-25 23:29:24 +01001334 if (result) {
Corentin Chary774b0672011-12-15 08:27:34 +01001335 rfkill_destroy(rfk->rfkill);
1336 rfk->rfkill = NULL;
Corentin Chary18e13112010-01-25 23:29:24 +01001337 }
1338
1339 return result;
1340}
1341
Corentin Chary774b0672011-12-15 08:27:34 +01001342static int asus_rfkill_init(struct asus_laptop *asus)
1343{
1344 int result = 0;
1345
1346 if (!acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) &&
1347 !acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) &&
1348 !acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
1349 result = asus_rfkill_setup(asus, &asus->gps, "asus-gps",
1350 -1, RFKILL_TYPE_GPS,
1351 &asus_gps_rfkill_ops);
1352 if (result)
1353 goto exit;
1354
1355
1356 if (asus->wled_type == TYPE_RFKILL)
1357 result = asus_rfkill_setup(asus, &asus->wlan, "asus-wlan",
1358 WL_RSTS, RFKILL_TYPE_WLAN,
1359 &asus_rfkill_ops);
1360 if (result)
1361 goto exit;
1362
1363 if (asus->bled_type == TYPE_RFKILL)
1364 result = asus_rfkill_setup(asus, &asus->bluetooth,
1365 "asus-bluetooth", BT_RSTS,
1366 RFKILL_TYPE_BLUETOOTH,
1367 &asus_rfkill_ops);
1368 if (result)
1369 goto exit;
1370
1371exit:
1372 if (result)
1373 asus_rfkill_exit(asus);
1374
1375 return result;
1376}
1377
Anisse Astier14908392011-10-14 11:13:42 +02001378static int pega_rfkill_set(void *data, bool blocked)
1379{
Corentin Chary40969c72011-12-15 08:27:33 +01001380 struct asus_rfkill *rfk = data;
Anisse Astier14908392011-10-14 11:13:42 +02001381
Corentin Chary40969c72011-12-15 08:27:33 +01001382 int ret = asus_pega_lucid_set(rfk->asus, rfk->control_id, !blocked);
Anisse Astier14908392011-10-14 11:13:42 +02001383 return ret;
1384}
1385
1386static const struct rfkill_ops pega_rfkill_ops = {
1387 .set_block = pega_rfkill_set,
1388};
1389
Corentin Chary40969c72011-12-15 08:27:33 +01001390static int pega_rfkill_setup(struct asus_laptop *asus, struct asus_rfkill *rfk,
1391 const char *name, int controlid, int rfkill_type)
Anisse Astier14908392011-10-14 11:13:42 +02001392{
Corentin Chary774b0672011-12-15 08:27:34 +01001393 return asus_rfkill_setup(asus, rfk, name, controlid, rfkill_type,
1394 &pega_rfkill_ops);
Anisse Astier14908392011-10-14 11:13:42 +02001395}
1396
1397static int pega_rfkill_init(struct asus_laptop *asus)
1398{
1399 int ret = 0;
1400
1401 if(!asus->is_pega_lucid)
1402 return -ENODEV;
1403
Corentin Chary40969c72011-12-15 08:27:33 +01001404 ret = pega_rfkill_setup(asus, &asus->wlan, "pega-wlan",
1405 PEGA_WLAN, RFKILL_TYPE_WLAN);
Anisse Astier14908392011-10-14 11:13:42 +02001406 if(ret)
Corentin Chary40969c72011-12-15 08:27:33 +01001407 goto exit;
Anisse Astier14908392011-10-14 11:13:42 +02001408
Corentin Chary40969c72011-12-15 08:27:33 +01001409 ret = pega_rfkill_setup(asus, &asus->bluetooth, "pega-bt",
1410 PEGA_BLUETOOTH, RFKILL_TYPE_BLUETOOTH);
1411 if(ret)
1412 goto exit;
1413
1414 ret = pega_rfkill_setup(asus, &asus->wwan, "pega-wwan",
1415 PEGA_WWAN, RFKILL_TYPE_WWAN);
1416
1417exit:
1418 if (ret)
1419 asus_rfkill_exit(asus);
Anisse Astier14908392011-10-14 11:13:42 +02001420
1421 return ret;
1422}
1423
Corentin Chary18e13112010-01-25 23:29:24 +01001424/*
Corentin Charybe4ee822009-12-06 16:27:09 +01001425 * Input device (i.e. hotkeys)
Corentin Chary034ce902009-01-20 16:17:43 +01001426 */
Corentin Charybe4ee822009-12-06 16:27:09 +01001427static void asus_input_notify(struct asus_laptop *asus, int event)
1428{
Corentin Charye0ac9132011-12-15 08:27:31 +01001429 if (!asus->inputdev)
1430 return ;
1431 if (!sparse_keymap_report_event(asus->inputdev, event, 1, true))
1432 pr_info("Unknown key %x pressed\n", event);
Corentin Charybe4ee822009-12-06 16:27:09 +01001433}
1434
1435static int asus_input_init(struct asus_laptop *asus)
1436{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001437 struct input_dev *input;
1438 int error;
Corentin Charybe4ee822009-12-06 16:27:09 +01001439
Corentin Chary66a71dd2010-01-25 22:50:11 +01001440 input = input_allocate_device();
1441 if (!input) {
Corentin Chary40969c72011-12-15 08:27:33 +01001442 pr_warn("Unable to allocate input device\n");
Axel Lin45036ae2010-07-05 15:29:00 +08001443 return -ENOMEM;
Corentin Charybe4ee822009-12-06 16:27:09 +01001444 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001445 input->name = "Asus Laptop extra buttons";
1446 input->phys = ASUS_LAPTOP_FILE "/input0";
1447 input->id.bustype = BUS_HOST;
1448 input->dev.parent = &asus->platform_device->dev;
Corentin Charybe4ee822009-12-06 16:27:09 +01001449
Corentin Chary66a71dd2010-01-25 22:50:11 +01001450 error = sparse_keymap_setup(input, asus_keymap, NULL);
1451 if (error) {
1452 pr_err("Unable to setup input device keymap\n");
Axel Lin45036ae2010-07-05 15:29:00 +08001453 goto err_free_dev;
Corentin Charybe4ee822009-12-06 16:27:09 +01001454 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001455 error = input_register_device(input);
1456 if (error) {
Corentin Chary40969c72011-12-15 08:27:33 +01001457 pr_warn("Unable to register input device\n");
Axel Lin45036ae2010-07-05 15:29:00 +08001458 goto err_free_keymap;
Corentin Charybe4ee822009-12-06 16:27:09 +01001459 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001460
1461 asus->inputdev = input;
1462 return 0;
1463
Axel Lin45036ae2010-07-05 15:29:00 +08001464err_free_keymap:
Corentin Chary66a71dd2010-01-25 22:50:11 +01001465 sparse_keymap_free(input);
Axel Lin45036ae2010-07-05 15:29:00 +08001466err_free_dev:
Corentin Chary66a71dd2010-01-25 22:50:11 +01001467 input_free_device(input);
1468 return error;
Corentin Charybe4ee822009-12-06 16:27:09 +01001469}
1470
1471static void asus_input_exit(struct asus_laptop *asus)
1472{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001473 if (asus->inputdev) {
1474 sparse_keymap_free(asus->inputdev);
Corentin Charybe4ee822009-12-06 16:27:09 +01001475 input_unregister_device(asus->inputdev);
Corentin Chary66a71dd2010-01-25 22:50:11 +01001476 }
Corentin Chary71e687d2010-08-24 09:30:44 +02001477 asus->inputdev = NULL;
Corentin Charybe4ee822009-12-06 16:27:09 +01001478}
1479
1480/*
1481 * ACPI driver
1482 */
Corentin Chary600ad522009-11-30 21:42:42 +01001483static void asus_acpi_notify(struct acpi_device *device, u32 event)
Corentin Chary85091b72007-01-26 14:04:30 +01001484{
Corentin Chary9129d142009-12-01 22:39:41 +01001485 struct asus_laptop *asus = acpi_driver_data(device);
Corentin Chary6050c8d2009-02-15 19:30:19 +01001486 u16 count;
Corentin Chary034ce902009-01-20 16:17:43 +01001487
Corentin Chary619d8b12009-11-28 10:35:37 +01001488 /* TODO Find a better way to handle events count. */
Corentin Chary50a90c42009-11-30 21:55:12 +01001489 count = asus->event_count[event % 128]++;
1490 acpi_bus_generate_proc_event(asus->device, event, count);
1491 acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
1492 dev_name(&asus->device->dev), event,
Corentin Chary6050c8d2009-02-15 19:30:19 +01001493 count);
Corentin Chary85091b72007-01-26 14:04:30 +01001494
Corentin Charya539df52010-01-25 22:53:21 +01001495 /* Brightness events are special */
1496 if (event >= ATKD_BR_MIN && event <= ATKD_BR_MAX) {
1497
1498 /* Ignore them completely if the acpi video driver is used */
1499 if (asus->backlight_device != NULL) {
1500 /* Update the backlight device. */
1501 asus_backlight_notify(asus);
1502 }
1503 return ;
1504 }
Anisse Astierb93f8282011-10-14 11:13:41 +02001505
1506 /* Accelerometer "coarse orientation change" event */
1507 if (asus->pega_accel_poll && event == 0xEA) {
1508 kobject_uevent(&asus->pega_accel_poll->input->dev.kobj,
1509 KOBJ_CHANGE);
1510 return ;
1511 }
1512
Corentin Charybe4ee822009-12-06 16:27:09 +01001513 asus_input_notify(asus, event);
Corentin Chary85091b72007-01-26 14:04:30 +01001514}
1515
Corentin Chary2a1fd642010-01-26 21:02:23 +01001516static DEVICE_ATTR(infos, S_IRUGO, show_infos, NULL);
1517static DEVICE_ATTR(wlan, S_IRUGO | S_IWUSR, show_wlan, store_wlan);
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001518static DEVICE_ATTR(bluetooth, S_IRUGO | S_IWUSR,
1519 show_bluetooth, store_bluetooth);
Corentin Charyba1ff5b2010-11-14 17:40:12 +01001520static DEVICE_ATTR(wimax, S_IRUGO | S_IWUSR, show_wimax, store_wimax);
1521static DEVICE_ATTR(wwan, S_IRUGO | S_IWUSR, show_wwan, store_wwan);
Corentin Chary3b81cf92011-02-06 13:27:31 +01001522static DEVICE_ATTR(display, S_IWUSR, NULL, store_disp);
Corentin Chary2a1fd642010-01-26 21:02:23 +01001523static DEVICE_ATTR(ledd, S_IRUGO | S_IWUSR, show_ledd, store_ledd);
Andy Ross33989ba2011-10-14 11:13:36 +02001524static DEVICE_ATTR(ls_value, S_IRUGO, show_lsvalue, NULL);
Corentin Chary2a1fd642010-01-26 21:02:23 +01001525static DEVICE_ATTR(ls_level, S_IRUGO | S_IWUSR, show_lslvl, store_lslvl);
1526static DEVICE_ATTR(ls_switch, S_IRUGO | S_IWUSR, show_lssw, store_lssw);
1527static DEVICE_ATTR(gps, S_IRUGO | S_IWUSR, show_gps, store_gps);
1528
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001529static struct attribute *asus_attributes[] = {
1530 &dev_attr_infos.attr,
1531 &dev_attr_wlan.attr,
1532 &dev_attr_bluetooth.attr,
Corentin Charyba1ff5b2010-11-14 17:40:12 +01001533 &dev_attr_wimax.attr,
1534 &dev_attr_wwan.attr,
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001535 &dev_attr_display.attr,
1536 &dev_attr_ledd.attr,
Andy Ross33989ba2011-10-14 11:13:36 +02001537 &dev_attr_ls_value.attr,
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001538 &dev_attr_ls_level.attr,
1539 &dev_attr_ls_switch.attr,
1540 &dev_attr_gps.attr,
1541 NULL
1542};
Corentin Chary2a1fd642010-01-26 21:02:23 +01001543
Al Viro587a1f12011-07-23 23:11:19 -04001544static umode_t asus_sysfs_is_visible(struct kobject *kobj,
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001545 struct attribute *attr,
1546 int idx)
1547{
1548 struct device *dev = container_of(kobj, struct device, kobj);
1549 struct platform_device *pdev = to_platform_device(dev);
1550 struct asus_laptop *asus = platform_get_drvdata(pdev);
1551 acpi_handle handle = asus->handle;
1552 bool supported;
1553
Andy Ross33989ba2011-10-14 11:13:36 +02001554 if (asus->is_pega_lucid) {
1555 /* no ls_level interface on the Lucid */
1556 if (attr == &dev_attr_ls_switch.attr)
1557 supported = true;
1558 else if (attr == &dev_attr_ls_level.attr)
1559 supported = false;
1560 else
1561 goto normal;
1562
1563 return supported;
1564 }
1565
1566normal:
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001567 if (attr == &dev_attr_wlan.attr) {
1568 supported = !acpi_check_handle(handle, METHOD_WLAN, NULL);
1569
1570 } else if (attr == &dev_attr_bluetooth.attr) {
1571 supported = !acpi_check_handle(handle, METHOD_BLUETOOTH, NULL);
1572
1573 } else if (attr == &dev_attr_display.attr) {
1574 supported = !acpi_check_handle(handle, METHOD_SWITCH_DISPLAY, NULL);
1575
Corentin Charyba1ff5b2010-11-14 17:40:12 +01001576 } else if (attr == &dev_attr_wimax.attr) {
1577 supported =
1578 !acpi_check_handle(asus->handle, METHOD_WIMAX, NULL);
1579
1580 } else if (attr == &dev_attr_wwan.attr) {
1581 supported = !acpi_check_handle(asus->handle, METHOD_WWAN, NULL);
1582
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001583 } else if (attr == &dev_attr_ledd.attr) {
1584 supported = !acpi_check_handle(handle, METHOD_LEDD, NULL);
1585
1586 } else if (attr == &dev_attr_ls_switch.attr ||
1587 attr == &dev_attr_ls_level.attr) {
1588 supported = !acpi_check_handle(handle, METHOD_ALS_CONTROL, NULL) &&
Andy Ross33989ba2011-10-14 11:13:36 +02001589 !acpi_check_handle(handle, METHOD_ALS_LEVEL, NULL);
1590 } else if (attr == &dev_attr_ls_value.attr) {
1591 supported = asus->is_pega_lucid;
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001592 } else if (attr == &dev_attr_gps.attr) {
1593 supported = !acpi_check_handle(handle, METHOD_GPS_ON, NULL) &&
1594 !acpi_check_handle(handle, METHOD_GPS_OFF, NULL) &&
1595 !acpi_check_handle(handle, METHOD_GPS_STATUS, NULL);
1596 } else {
1597 supported = true;
1598 }
1599
1600 return supported ? attr->mode : 0;
Corentin Chary2a1fd642010-01-26 21:02:23 +01001601}
1602
Corentin Chary2a1fd642010-01-26 21:02:23 +01001603
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001604static const struct attribute_group asus_attr_group = {
1605 .is_visible = asus_sysfs_is_visible,
1606 .attrs = asus_attributes,
1607};
Corentin Chary85091b72007-01-26 14:04:30 +01001608
Corentin Chary9129d142009-12-01 22:39:41 +01001609static int asus_platform_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001610{
Corentin Chary71e687d2010-08-24 09:30:44 +02001611 int result;
Corentin Chary600ad522009-11-30 21:42:42 +01001612
Corentin Chary50a90c42009-11-30 21:55:12 +01001613 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1614 if (!asus->platform_device)
Corentin Chary600ad522009-11-30 21:42:42 +01001615 return -ENOMEM;
Corentin Chary9129d142009-12-01 22:39:41 +01001616 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001617
Corentin Chary71e687d2010-08-24 09:30:44 +02001618 result = platform_device_add(asus->platform_device);
1619 if (result)
Corentin Chary600ad522009-11-30 21:42:42 +01001620 goto fail_platform_device;
1621
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001622 result = sysfs_create_group(&asus->platform_device->dev.kobj,
1623 &asus_attr_group);
Corentin Chary71e687d2010-08-24 09:30:44 +02001624 if (result)
Corentin Chary600ad522009-11-30 21:42:42 +01001625 goto fail_sysfs;
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001626
Corentin Chary600ad522009-11-30 21:42:42 +01001627 return 0;
1628
1629fail_sysfs:
Corentin Chary50a90c42009-11-30 21:55:12 +01001630 platform_device_del(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001631fail_platform_device:
Corentin Chary50a90c42009-11-30 21:55:12 +01001632 platform_device_put(asus->platform_device);
Corentin Chary71e687d2010-08-24 09:30:44 +02001633 return result;
Corentin Chary600ad522009-11-30 21:42:42 +01001634}
1635
Corentin Chary9129d142009-12-01 22:39:41 +01001636static void asus_platform_exit(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001637{
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001638 sysfs_remove_group(&asus->platform_device->dev.kobj, &asus_attr_group);
Corentin Chary50a90c42009-11-30 21:55:12 +01001639 platform_device_unregister(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001640}
1641
1642static struct platform_driver platform_driver = {
Len Brown8def05f2007-01-30 01:46:43 -05001643 .driver = {
Corentin Chary9129d142009-12-01 22:39:41 +01001644 .name = ASUS_LAPTOP_FILE,
1645 .owner = THIS_MODULE,
Andy Rossb23910c2011-10-14 11:13:38 +02001646 },
Corentin Chary85091b72007-01-26 14:04:30 +01001647};
1648
Corentin Chary85091b72007-01-26 14:04:30 +01001649/*
Corentin Chary50a90c42009-11-30 21:55:12 +01001650 * This function is used to initialize the context with right values. In this
1651 * method, we can make all the detection we want, and modify the asus_laptop
1652 * struct
Corentin Chary85091b72007-01-26 14:04:30 +01001653 */
Corentin Chary7c247642009-11-30 22:13:54 +01001654static int asus_laptop_get_info(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001655{
1656 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Corentin Chary85091b72007-01-26 14:04:30 +01001657 union acpi_object *model = NULL;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001658 unsigned long long bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +01001659 char *string = NULL;
1660 acpi_status status;
1661
1662 /*
1663 * Get DSDT headers early enough to allow for differentiating between
1664 * models, but late enough to allow acpi_bus_register_driver() to fail
1665 * before doing anything ACPI-specific. Should we encounter a machine,
1666 * which needs special handling (i.e. its hotkey device has a different
Corentin Chary7c247642009-11-30 22:13:54 +01001667 * HID), this bit will be moved.
Corentin Chary85091b72007-01-26 14:04:30 +01001668 */
Corentin Chary7c247642009-11-30 22:13:54 +01001669 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
Corentin Chary85091b72007-01-26 14:04:30 +01001670 if (ACPI_FAILURE(status))
Joe Perches5ad77dc2011-03-29 15:21:35 -07001671 pr_warn("Couldn't get the DSDT table header\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001672
1673 /* We have to write 0 on init this far for all ASUS models */
Corentin Chary50a90c42009-11-30 21:55:12 +01001674 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001675 pr_err("Hotkey initialization failed\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001676 return -ENODEV;
1677 }
1678
1679 /* This needs to be called for some laptops to init properly */
Corentin Chary9a816852007-03-11 10:25:38 +01001680 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001681 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
Corentin Chary9a816852007-03-11 10:25:38 +01001682 if (ACPI_FAILURE(status))
Joe Perches5ad77dc2011-03-29 15:21:35 -07001683 pr_warn("Error calling BSTS\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001684 else if (bsts_result)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001685 pr_notice("BSTS called, 0x%02x returned\n",
Corentin Chary9a816852007-03-11 10:25:38 +01001686 (uint) bsts_result);
Corentin Chary85091b72007-01-26 14:04:30 +01001687
Corentin Chary185e5af2007-03-11 10:27:33 +01001688 /* This too ... */
Corentin Charye5593bf2010-01-17 17:20:11 +01001689 if (write_acpi_int(asus->handle, "CWAP", wapf))
1690 pr_err("Error calling CWAP(%d)\n", wapf);
Corentin Chary85091b72007-01-26 14:04:30 +01001691 /*
1692 * Try to match the object returned by INIT to the specific model.
1693 * Handle every possible object (or the lack of thereof) the DSDT
1694 * writers might throw at us. When in trouble, we pass NULL to
1695 * asus_model_match() and try something completely different.
1696 */
1697 if (buffer.pointer) {
1698 model = buffer.pointer;
1699 switch (model->type) {
1700 case ACPI_TYPE_STRING:
1701 string = model->string.pointer;
1702 break;
1703 case ACPI_TYPE_BUFFER:
1704 string = model->buffer.pointer;
1705 break;
1706 default:
1707 string = "";
1708 break;
1709 }
1710 }
Corentin Chary50a90c42009-11-30 21:55:12 +01001711 asus->name = kstrdup(string, GFP_KERNEL);
Axel Lind8eca112010-06-29 11:09:47 +08001712 if (!asus->name) {
1713 kfree(buffer.pointer);
Corentin Chary85091b72007-01-26 14:04:30 +01001714 return -ENOMEM;
Axel Lind8eca112010-06-29 11:09:47 +08001715 }
Corentin Chary85091b72007-01-26 14:04:30 +01001716
Len Brown8def05f2007-01-30 01:46:43 -05001717 if (*string)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001718 pr_notice(" %s model detected\n", string);
Corentin Chary85091b72007-01-26 14:04:30 +01001719
Corentin Chary4564de12007-01-26 14:04:40 +01001720 /*
1721 * The HWRS method return informations about the hardware.
Corentin Charyba1ff5b2010-11-14 17:40:12 +01001722 * 0x80 bit is for WLAN, 0x100 for Bluetooth,
1723 * 0x40 for WWAN, 0x10 for WIMAX.
Corentin Chary4564de12007-01-26 14:04:40 +01001724 * The significance of others is yet to be found.
Corentin Chary4564de12007-01-26 14:04:40 +01001725 */
Corentin Chary9a816852007-03-11 10:25:38 +01001726 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001727 acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
Corentin Charyd99b5772010-01-22 21:20:57 +01001728 if (!ACPI_FAILURE(status))
1729 pr_notice(" HRWS returned %x", (int)hwrs_result);
Corentin Chary4564de12007-01-26 14:04:40 +01001730
Corentin Charyd99b5772010-01-22 21:20:57 +01001731 if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
Corentin Charyaa9df932010-01-13 21:49:10 +01001732 asus->have_rsts = true;
Corentin Chary4564de12007-01-26 14:04:40 +01001733
Corentin Chary85091b72007-01-26 14:04:30 +01001734 kfree(model);
1735
1736 return AE_OK;
1737}
1738
Corentin Chary9129d142009-12-01 22:39:41 +01001739static int __devinit asus_acpi_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001740{
1741 int result = 0;
1742
Corentin Chary50a90c42009-11-30 21:55:12 +01001743 result = acpi_bus_get_status(asus->device);
Corentin Chary600ad522009-11-30 21:42:42 +01001744 if (result)
1745 return result;
Corentin Chary50a90c42009-11-30 21:55:12 +01001746 if (!asus->device->status.present) {
Corentin Chary600ad522009-11-30 21:42:42 +01001747 pr_err("Hotkey device not present, aborting\n");
1748 return -ENODEV;
1749 }
1750
Corentin Chary7c247642009-11-30 22:13:54 +01001751 result = asus_laptop_get_info(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001752 if (result)
1753 return result;
1754
Corentin Chary774b0672011-12-15 08:27:34 +01001755 if (!strcmp(bled_type, "led"))
1756 asus->bled_type = TYPE_LED;
1757 else if (!strcmp(bled_type, "rfkill"))
1758 asus->bled_type = TYPE_RFKILL;
1759
1760 if (!strcmp(wled_type, "led"))
1761 asus->wled_type = TYPE_LED;
1762 else if (!strcmp(wled_type, "rfkill"))
1763 asus->wled_type = TYPE_RFKILL;
1764
Corentin Charybe4ee822009-12-06 16:27:09 +01001765 if (bluetooth_status >= 0)
Corentin Charye5593bf2010-01-17 17:20:11 +01001766 asus_bluetooth_set(asus, !!bluetooth_status);
1767
Corentin Charyd0930a22010-01-17 17:21:13 +01001768 if (wlan_status >= 0)
1769 asus_wlan_set(asus, !!wlan_status);
Corentin Chary600ad522009-11-30 21:42:42 +01001770
Corentin Charyba1ff5b2010-11-14 17:40:12 +01001771 if (wimax_status >= 0)
1772 asus_wimax_set(asus, !!wimax_status);
1773
1774 if (wwan_status >= 0)
1775 asus_wwan_set(asus, !!wwan_status);
1776
Corentin Chary600ad522009-11-30 21:42:42 +01001777 /* Keyboard Backlight is on by default */
Corentin Charyd99b5772010-01-22 21:20:57 +01001778 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
Corentin Chary4d441512010-01-13 22:26:24 +01001779 asus_kled_set(asus, 1);
Corentin Chary600ad522009-11-30 21:42:42 +01001780
1781 /* LED display is off by default */
Corentin Chary50a90c42009-11-30 21:55:12 +01001782 asus->ledd_status = 0xFFF;
Corentin Chary600ad522009-11-30 21:42:42 +01001783
1784 /* Set initial values of light sensor and level */
Andy Rossabec04d2011-10-14 11:13:37 +02001785 asus->light_switch = !!als_status;
Corentin Charybe4ee822009-12-06 16:27:09 +01001786 asus->light_level = 5; /* level 5 for sensor sensitivity */
Corentin Chary600ad522009-11-30 21:42:42 +01001787
Andy Ross33989ba2011-10-14 11:13:36 +02001788 if (asus->is_pega_lucid) {
1789 asus_als_switch(asus, asus->light_switch);
1790 } else if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1791 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
Corentin Chary4d441512010-01-13 22:26:24 +01001792 asus_als_switch(asus, asus->light_switch);
Corentin Chary4d441512010-01-13 22:26:24 +01001793 asus_als_level(asus, asus->light_level);
Corentin Charyd99b5772010-01-22 21:20:57 +01001794 }
Corentin Chary600ad522009-11-30 21:42:42 +01001795
Corentin Chary600ad522009-11-30 21:42:42 +01001796 return result;
1797}
1798
Corentin Charyaf96f872011-02-06 13:27:30 +01001799static void __devinit asus_dmi_check(void)
1800{
1801 const char *model;
1802
1803 model = dmi_get_system_info(DMI_PRODUCT_NAME);
1804 if (!model)
1805 return;
1806
1807 /* On L1400B WLED control the sound card, don't mess with it ... */
1808 if (strncmp(model, "L1400B", 6) == 0) {
1809 wlan_status = -1;
1810 }
1811}
1812
Corentin Chary71e687d2010-08-24 09:30:44 +02001813static bool asus_device_present;
1814
Corentin Chary600ad522009-11-30 21:42:42 +01001815static int __devinit asus_acpi_add(struct acpi_device *device)
1816{
Corentin Chary9129d142009-12-01 22:39:41 +01001817 struct asus_laptop *asus;
Corentin Chary600ad522009-11-30 21:42:42 +01001818 int result;
1819
1820 pr_notice("Asus Laptop Support version %s\n",
1821 ASUS_LAPTOP_VERSION);
Corentin Chary50a90c42009-11-30 21:55:12 +01001822 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1823 if (!asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001824 return -ENOMEM;
Corentin Chary50a90c42009-11-30 21:55:12 +01001825 asus->handle = device->handle;
1826 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1827 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1828 device->driver_data = asus;
1829 asus->device = device;
Corentin Chary600ad522009-11-30 21:42:42 +01001830
Corentin Charyaf96f872011-02-06 13:27:30 +01001831 asus_dmi_check();
1832
Corentin Chary9129d142009-12-01 22:39:41 +01001833 result = asus_acpi_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001834 if (result)
1835 goto fail_platform;
1836
1837 /*
Andy Ross8819de72011-10-14 11:13:35 +02001838 * Need platform type detection first, then the platform
1839 * device. It is used as a parent for the sub-devices below.
Corentin Chary600ad522009-11-30 21:42:42 +01001840 */
Andy Ross8819de72011-10-14 11:13:35 +02001841 asus->is_pega_lucid = asus_check_pega_lucid(asus);
Corentin Chary9129d142009-12-01 22:39:41 +01001842 result = asus_platform_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001843 if (result)
1844 goto fail_platform;
1845
1846 if (!acpi_video_backlight_support()) {
Corentin Chary9129d142009-12-01 22:39:41 +01001847 result = asus_backlight_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001848 if (result)
1849 goto fail_backlight;
1850 } else
1851 pr_info("Backlight controlled by ACPI video driver\n");
1852
Corentin Chary9129d142009-12-01 22:39:41 +01001853 result = asus_input_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001854 if (result)
1855 goto fail_input;
1856
Corentin Chary9129d142009-12-01 22:39:41 +01001857 result = asus_led_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001858 if (result)
1859 goto fail_led;
1860
Corentin Chary18e13112010-01-25 23:29:24 +01001861 result = asus_rfkill_init(asus);
1862 if (result)
1863 goto fail_rfkill;
1864
Andy Rossb23910c2011-10-14 11:13:38 +02001865 result = pega_accel_init(asus);
1866 if (result && result != -ENODEV)
1867 goto fail_pega_accel;
1868
Anisse Astier14908392011-10-14 11:13:42 +02001869 result = pega_rfkill_init(asus);
1870 if (result && result != -ENODEV)
1871 goto fail_pega_rfkill;
1872
Corentin Chary600ad522009-11-30 21:42:42 +01001873 asus_device_present = true;
1874 return 0;
1875
Anisse Astier14908392011-10-14 11:13:42 +02001876fail_pega_rfkill:
1877 pega_accel_exit(asus);
Andy Rossb23910c2011-10-14 11:13:38 +02001878fail_pega_accel:
1879 asus_rfkill_exit(asus);
Corentin Chary18e13112010-01-25 23:29:24 +01001880fail_rfkill:
1881 asus_led_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001882fail_led:
Corentin Chary9129d142009-12-01 22:39:41 +01001883 asus_input_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001884fail_input:
Corentin Chary9129d142009-12-01 22:39:41 +01001885 asus_backlight_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001886fail_backlight:
Corentin Chary9129d142009-12-01 22:39:41 +01001887 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001888fail_platform:
Corentin Chary50a90c42009-11-30 21:55:12 +01001889 kfree(asus->name);
1890 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001891
1892 return result;
1893}
1894
1895static int asus_acpi_remove(struct acpi_device *device, int type)
1896{
Corentin Chary9129d142009-12-01 22:39:41 +01001897 struct asus_laptop *asus = acpi_driver_data(device);
1898
1899 asus_backlight_exit(asus);
Corentin Chary18e13112010-01-25 23:29:24 +01001900 asus_rfkill_exit(asus);
Corentin Chary9129d142009-12-01 22:39:41 +01001901 asus_led_exit(asus);
1902 asus_input_exit(asus);
Andy Rossb23910c2011-10-14 11:13:38 +02001903 pega_accel_exit(asus);
Corentin Chary9129d142009-12-01 22:39:41 +01001904 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001905
Corentin Chary50a90c42009-11-30 21:55:12 +01001906 kfree(asus->name);
1907 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001908 return 0;
1909}
1910
1911static const struct acpi_device_id asus_device_ids[] = {
1912 {"ATK0100", 0},
1913 {"ATK0101", 0},
1914 {"", 0},
1915};
1916MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1917
1918static struct acpi_driver asus_acpi_driver = {
Corentin Chary50a90c42009-11-30 21:55:12 +01001919 .name = ASUS_LAPTOP_NAME,
1920 .class = ASUS_LAPTOP_CLASS,
Corentin Chary600ad522009-11-30 21:42:42 +01001921 .owner = THIS_MODULE,
1922 .ids = asus_device_ids,
1923 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1924 .ops = {
1925 .add = asus_acpi_add,
1926 .remove = asus_acpi_remove,
1927 .notify = asus_acpi_notify,
1928 },
1929};
1930
Corentin Chary85091b72007-01-26 14:04:30 +01001931static int __init asus_laptop_init(void)
1932{
1933 int result;
1934
Corentin Chary600ad522009-11-30 21:42:42 +01001935 result = platform_driver_register(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001936 if (result < 0)
1937 return result;
1938
Corentin Chary600ad522009-11-30 21:42:42 +01001939 result = acpi_bus_register_driver(&asus_acpi_driver);
1940 if (result < 0)
1941 goto fail_acpi_driver;
1942 if (!asus_device_present) {
1943 result = -ENODEV;
1944 goto fail_no_device;
Corentin Chary85091b72007-01-26 14:04:30 +01001945 }
Len Brown8def05f2007-01-30 01:46:43 -05001946 return 0;
Corentin Chary85091b72007-01-26 14:04:30 +01001947
Corentin Chary600ad522009-11-30 21:42:42 +01001948fail_no_device:
1949 acpi_bus_unregister_driver(&asus_acpi_driver);
1950fail_acpi_driver:
1951 platform_driver_unregister(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001952 return result;
1953}
1954
Corentin Chary600ad522009-11-30 21:42:42 +01001955static void __exit asus_laptop_exit(void)
1956{
1957 acpi_bus_unregister_driver(&asus_acpi_driver);
1958 platform_driver_unregister(&platform_driver);
1959}
1960
Corentin Chary85091b72007-01-26 14:04:30 +01001961module_init(asus_laptop_init);
1962module_exit(asus_laptop_exit);