blob: c08f4c26b9883620fd7914c229512d2627f981a3 [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
Dan Carpenter668f4a02010-04-06 13:44:29 +030084static int wlan_status = 1;
85static int bluetooth_status = 1;
Corentin Charyba1ff5b2010-11-14 17:40:12 +010086static int wimax_status = -1;
87static int wwan_status = -1;
Corentin Chary0e875f42010-01-10 20:49:26 +010088
Axel Linc26d85c2010-07-20 15:19:55 -070089module_param(wlan_status, int, 0444);
Corentin Charyd0930a22010-01-17 17:21:13 +010090MODULE_PARM_DESC(wlan_status, "Set the wireless status on boot "
Corentin Chary0e875f42010-01-10 20:49:26 +010091 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
92 "default is 1");
93
Axel Linc26d85c2010-07-20 15:19:55 -070094module_param(bluetooth_status, int, 0444);
Corentin Chary0e875f42010-01-10 20:49:26 +010095MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
96 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
97 "default is 1");
98
Corentin Charyba1ff5b2010-11-14 17:40:12 +010099module_param(wimax_status, int, 0444);
100MODULE_PARM_DESC(wimax_status, "Set the wireless status on boot "
101 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
102 "default is 1");
103
104module_param(wwan_status, int, 0444);
105MODULE_PARM_DESC(wwan_status, "Set the wireless status on boot "
106 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
107 "default is 1");
108
Corentin Charybe4ee822009-12-06 16:27:09 +0100109/*
110 * Some events we use, same for all Asus
111 */
Corentin Chary060cbce2010-01-28 10:52:40 +0100112#define ATKD_BR_UP 0x10 /* (event & ~ATKD_BR_UP) = brightness level */
113#define ATKD_BR_DOWN 0x20 /* (event & ~ATKD_BR_DOWN) = britghness level */
Corentin Charya539df52010-01-25 22:53:21 +0100114#define ATKD_BR_MIN ATKD_BR_UP
Corentin Chary060cbce2010-01-28 10:52:40 +0100115#define ATKD_BR_MAX (ATKD_BR_DOWN | 0xF) /* 0x2f */
Corentin Charybe4ee822009-12-06 16:27:09 +0100116#define ATKD_LCD_ON 0x33
117#define ATKD_LCD_OFF 0x34
118
119/*
120 * Known bits returned by \_SB.ATKD.HWRS
121 */
122#define WL_HWRS 0x80
123#define BT_HWRS 0x100
124
125/*
126 * Flags for hotk status
127 * WL_ON and BT_ON are also used for wireless_status()
128 */
Corentin Chary17e78f62010-01-13 22:21:33 +0100129#define WL_RSTS 0x01 /* internal Wifi */
130#define BT_RSTS 0x02 /* internal Bluetooth */
Corentin Charyba1ff5b2010-11-14 17:40:12 +0100131#define WM_RSTS 0x08 /* internal wimax */
132#define WW_RSTS 0x20 /* internal wwan */
Corentin Charybe4ee822009-12-06 16:27:09 +0100133
Corentin Charybe18cda2007-01-26 14:04:35 +0100134/* LED */
Corentin Charyd99b5772010-01-22 21:20:57 +0100135#define METHOD_MLED "MLED"
136#define METHOD_TLED "TLED"
137#define METHOD_RLED "RLED" /* W1JC */
138#define METHOD_PLED "PLED" /* A7J */
139#define METHOD_GLED "GLED" /* G1, G2 (probably) */
Corentin Charybe18cda2007-01-26 14:04:35 +0100140
Corentin Chary722ad972007-01-26 14:04:55 +0100141/* LEDD */
Corentin Charyd99b5772010-01-22 21:20:57 +0100142#define METHOD_LEDD "SLCM"
Corentin Chary722ad972007-01-26 14:04:55 +0100143
Corentin Charybe966662009-08-29 10:28:29 +0200144/*
145 * Bluetooth and WLAN
Corentin Chary4564de12007-01-26 14:04:40 +0100146 * WLED and BLED are not handled like other XLED, because in some dsdt
147 * they also control the WLAN/Bluetooth device.
148 */
Corentin Charyd99b5772010-01-22 21:20:57 +0100149#define METHOD_WLAN "WLED"
150#define METHOD_BLUETOOTH "BLED"
Corentin Charyba1ff5b2010-11-14 17:40:12 +0100151
152/* WWAN and WIMAX */
153#define METHOD_WWAN "GSMC"
154#define METHOD_WIMAX "WMXC"
155
Corentin Charyd99b5772010-01-22 21:20:57 +0100156#define METHOD_WL_STATUS "RSTS"
Corentin Chary4564de12007-01-26 14:04:40 +0100157
Corentin Chary6b7091e2007-01-26 14:04:45 +0100158/* Brightness */
Corentin Charyd99b5772010-01-22 21:20:57 +0100159#define METHOD_BRIGHTNESS_SET "SPLV"
160#define METHOD_BRIGHTNESS_GET "GPLV"
Corentin Chary6b7091e2007-01-26 14:04:45 +0100161
Corentin Chary78127b42007-01-26 14:04:49 +0100162/* Display */
Corentin Charyd99b5772010-01-22 21:20:57 +0100163#define METHOD_SWITCH_DISPLAY "SDSP"
Corentin Chary060cbce2010-01-28 10:52:40 +0100164
Corentin Charyd99b5772010-01-22 21:20:57 +0100165#define METHOD_ALS_CONTROL "ALSC" /* Z71A Z71V */
166#define METHOD_ALS_LEVEL "ALSL" /* Z71A Z71V */
Corentin Chary8b857352007-01-26 14:04:58 +0100167
Corentin Charye539c2f2007-05-06 14:47:06 +0200168/* GPS */
169/* R2H use different handle for GPS on/off */
Corentin Charyd99b5772010-01-22 21:20:57 +0100170#define METHOD_GPS_ON "SDON"
171#define METHOD_GPS_OFF "SDOF"
172#define METHOD_GPS_STATUS "GPST"
Corentin Charye539c2f2007-05-06 14:47:06 +0200173
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000174/* Keyboard light */
Corentin Charyd99b5772010-01-22 21:20:57 +0100175#define METHOD_KBD_LIGHT_SET "SLKB"
176#define METHOD_KBD_LIGHT_GET "GLKB"
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000177
Andy Ross8819de72011-10-14 11:13:35 +0200178/* For Pegatron Lucid tablet */
179#define DEVICE_NAME_PEGA "Lucid"
180#define METHOD_PEGA_ENABLE "ENPR"
181#define METHOD_PEGA_DISABLE "DAPR"
182#define METHOD_PEGA_READ "RDLN"
183
Corentin Chary85091b72007-01-26 14:04:30 +0100184/*
Corentin Chary9129d142009-12-01 22:39:41 +0100185 * Define a specific led structure to keep the main structure clean
186 */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100187struct asus_led {
188 int wk;
189 struct work_struct work;
190 struct led_classdev led;
191 struct asus_laptop *asus;
192 const char *method;
Corentin Chary9129d142009-12-01 22:39:41 +0100193};
194
195/*
Corentin Chary85091b72007-01-26 14:04:30 +0100196 * This is the main structure, we can use it to store anything interesting
197 * about the hotk device
198 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100199struct asus_laptop {
Corentin Charybe966662009-08-29 10:28:29 +0200200 char *name; /* laptop name */
Corentin Chary600ad522009-11-30 21:42:42 +0100201
Corentin Chary7c247642009-11-30 22:13:54 +0100202 struct acpi_table_header *dsdt_info;
Corentin Chary600ad522009-11-30 21:42:42 +0100203 struct platform_device *platform_device;
Corentin Chary7c247642009-11-30 22:13:54 +0100204 struct acpi_device *device; /* the device we are in */
205 struct backlight_device *backlight_device;
Corentin Chary9129d142009-12-01 22:39:41 +0100206
Corentin Chary7c247642009-11-30 22:13:54 +0100207 struct input_dev *inputdev;
Corentin Chary9129d142009-12-01 22:39:41 +0100208 struct key_entry *keymap;
209
Corentin Charyaee0afb2010-01-26 21:01:34 +0100210 struct asus_led mled;
211 struct asus_led tled;
212 struct asus_led rled;
213 struct asus_led pled;
214 struct asus_led gled;
215 struct asus_led kled;
216 struct workqueue_struct *led_workqueue;
Corentin Chary7c247642009-11-30 22:13:54 +0100217
Corentin Charyaa9df932010-01-13 21:49:10 +0100218 int wireless_status;
219 bool have_rsts;
Andy Ross8819de72011-10-14 11:13:35 +0200220 bool is_pega_lucid;
Corentin Charyaa9df932010-01-13 21:49:10 +0100221
Corentin Chary18e13112010-01-25 23:29:24 +0100222 struct rfkill *gps_rfkill;
223
Corentin Charybe966662009-08-29 10:28:29 +0200224 acpi_handle handle; /* the handle of the hotk device */
Corentin Charybe966662009-08-29 10:28:29 +0200225 u32 ledd_status; /* status of the LED display */
226 u8 light_level; /* light sensor level */
227 u8 light_switch; /* light sensor switch value */
228 u16 event_count[128]; /* count for each event TODO make this better */
Corentin Chary85091b72007-01-26 14:04:30 +0100229};
230
Corentin Chary9129d142009-12-01 22:39:41 +0100231static const struct key_entry asus_keymap[] = {
Corentin Charya539df52010-01-25 22:53:21 +0100232 /* Lenovo SL Specific keycodes */
Corentin Chary66a71dd2010-01-25 22:50:11 +0100233 {KE_KEY, 0x02, { KEY_SCREENLOCK } },
234 {KE_KEY, 0x05, { KEY_WLAN } },
235 {KE_KEY, 0x08, { KEY_F13 } },
236 {KE_KEY, 0x17, { KEY_ZOOM } },
237 {KE_KEY, 0x1f, { KEY_BATTERY } },
Corentin Charya539df52010-01-25 22:53:21 +0100238 /* End of Lenovo SL Specific keycodes */
Corentin Chary66a71dd2010-01-25 22:50:11 +0100239 {KE_KEY, 0x30, { KEY_VOLUMEUP } },
240 {KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
241 {KE_KEY, 0x32, { KEY_MUTE } },
242 {KE_KEY, 0x33, { KEY_SWITCHVIDEOMODE } },
243 {KE_KEY, 0x34, { KEY_SWITCHVIDEOMODE } },
244 {KE_KEY, 0x40, { KEY_PREVIOUSSONG } },
245 {KE_KEY, 0x41, { KEY_NEXTSONG } },
246 {KE_KEY, 0x43, { KEY_STOPCD } },
247 {KE_KEY, 0x45, { KEY_PLAYPAUSE } },
248 {KE_KEY, 0x4c, { KEY_MEDIA } },
249 {KE_KEY, 0x50, { KEY_EMAIL } },
250 {KE_KEY, 0x51, { KEY_WWW } },
251 {KE_KEY, 0x55, { KEY_CALC } },
252 {KE_KEY, 0x5C, { KEY_SCREENLOCK } }, /* Screenlock */
253 {KE_KEY, 0x5D, { KEY_WLAN } },
254 {KE_KEY, 0x5E, { KEY_WLAN } },
255 {KE_KEY, 0x5F, { KEY_WLAN } },
256 {KE_KEY, 0x60, { KEY_SWITCHVIDEOMODE } },
257 {KE_KEY, 0x61, { KEY_SWITCHVIDEOMODE } },
258 {KE_KEY, 0x62, { KEY_SWITCHVIDEOMODE } },
259 {KE_KEY, 0x63, { KEY_SWITCHVIDEOMODE } },
260 {KE_KEY, 0x6B, { KEY_F13 } }, /* Lock Touchpad */
Corentin Chary7f607d72010-01-17 17:37:19 +0100261 {KE_KEY, 0x7E, { KEY_BLUETOOTH } },
262 {KE_KEY, 0x7D, { KEY_BLUETOOTH } },
Corentin Chary66a71dd2010-01-25 22:50:11 +0100263 {KE_KEY, 0x82, { KEY_CAMERA } },
264 {KE_KEY, 0x88, { KEY_WLAN } },
265 {KE_KEY, 0x8A, { KEY_PROG1 } },
266 {KE_KEY, 0x95, { KEY_MEDIA } },
267 {KE_KEY, 0x99, { KEY_PHONE } },
268 {KE_KEY, 0xc4, { KEY_KBDILLUMUP } },
269 {KE_KEY, 0xc5, { KEY_KBDILLUMDOWN } },
Corentin Charyb58baecd2010-08-24 09:30:45 +0200270 {KE_KEY, 0xb5, { KEY_CALC } },
Corentin Chary034ce902009-01-20 16:17:43 +0100271 {KE_END, 0},
272};
273
Corentin Chary66a71dd2010-01-25 22:50:11 +0100274
Corentin Chary85091b72007-01-26 14:04:30 +0100275/*
276 * This function evaluates an ACPI method, given an int as parameter, the
277 * method is searched within the scope of the handle, can be NULL. The output
278 * of the method is written is output, which can also be NULL
279 *
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100280 * returns 0 if write is successful, -1 else.
Corentin Chary85091b72007-01-26 14:04:30 +0100281 */
Corentin Charyd8c67322009-11-28 10:27:51 +0100282static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
283 struct acpi_buffer *output)
Corentin Chary85091b72007-01-26 14:04:30 +0100284{
Corentin Charybe966662009-08-29 10:28:29 +0200285 struct acpi_object_list params; /* list of input parameters (an int) */
286 union acpi_object in_obj; /* the only param we use */
Corentin Chary85091b72007-01-26 14:04:30 +0100287 acpi_status status;
288
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100289 if (!handle)
Axel Lin9fb866f2010-07-20 15:19:47 -0700290 return -1;
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100291
Corentin Chary85091b72007-01-26 14:04:30 +0100292 params.count = 1;
293 params.pointer = &in_obj;
294 in_obj.type = ACPI_TYPE_INTEGER;
295 in_obj.integer.value = val;
296
297 status = acpi_evaluate_object(handle, (char *)method, &params, output);
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100298 if (status == AE_OK)
299 return 0;
300 else
301 return -1;
Corentin Chary85091b72007-01-26 14:04:30 +0100302}
303
Corentin Charyd8c67322009-11-28 10:27:51 +0100304static int write_acpi_int(acpi_handle handle, const char *method, int val)
305{
306 return write_acpi_int_ret(handle, method, val, NULL);
307}
308
Corentin Charyd99b5772010-01-22 21:20:57 +0100309static int acpi_check_handle(acpi_handle handle, const char *method,
310 acpi_handle *ret)
311{
312 acpi_status status;
313
314 if (method == NULL)
315 return -ENODEV;
316
317 if (ret)
318 status = acpi_get_handle(handle, (char *)method,
319 ret);
320 else {
321 acpi_handle dummy;
322
323 status = acpi_get_handle(handle, (char *)method,
324 &dummy);
325 }
326
327 if (status != AE_OK) {
328 if (ret)
Joe Perches5ad77dc2011-03-29 15:21:35 -0700329 pr_warn("Error finding %s\n", method);
Corentin Charyd99b5772010-01-22 21:20:57 +0100330 return -ENODEV;
331 }
332 return 0;
333}
334
Andy Ross8819de72011-10-14 11:13:35 +0200335static bool asus_check_pega_lucid(struct asus_laptop *asus)
336{
337 return !strcmp(asus->name, DEVICE_NAME_PEGA) &&
338 !acpi_check_handle(asus->handle, METHOD_PEGA_ENABLE, NULL) &&
339 !acpi_check_handle(asus->handle, METHOD_PEGA_DISABLE, NULL) &&
340 !acpi_check_handle(asus->handle, METHOD_PEGA_READ, NULL);
341}
342
Corentin Chary17e78f62010-01-13 22:21:33 +0100343/* Generic LED function */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100344static int asus_led_set(struct asus_laptop *asus, const char *method,
Corentin Chary17e78f62010-01-13 22:21:33 +0100345 int value)
Corentin Charybe18cda2007-01-26 14:04:35 +0100346{
Corentin Charyd99b5772010-01-22 21:20:57 +0100347 if (!strcmp(method, METHOD_MLED))
Corentin Chary17e78f62010-01-13 22:21:33 +0100348 value = !value;
Corentin Charyd99b5772010-01-22 21:20:57 +0100349 else if (!strcmp(method, METHOD_GLED))
Corentin Chary17e78f62010-01-13 22:21:33 +0100350 value = !value + 1;
351 else
352 value = !!value;
Corentin Charybe18cda2007-01-26 14:04:35 +0100353
Corentin Charye5593bf2010-01-17 17:20:11 +0100354 return write_acpi_int(asus->handle, method, value);
Corentin Charybe18cda2007-01-26 14:04:35 +0100355}
356
Corentin Charybe4ee822009-12-06 16:27:09 +0100357/*
358 * LEDs
359 */
Corentin Charybe18cda2007-01-26 14:04:35 +0100360/* /sys/class/led handlers */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100361static void asus_led_cdev_set(struct led_classdev *led_cdev,
362 enum led_brightness value)
363{
364 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
365 struct asus_laptop *asus = led->asus;
Corentin Charybe18cda2007-01-26 14:04:35 +0100366
Corentin Charyaee0afb2010-01-26 21:01:34 +0100367 led->wk = !!value;
368 queue_work(asus->led_workqueue, &led->work);
369}
370
371static void asus_led_cdev_update(struct work_struct *work)
372{
373 struct asus_led *led = container_of(work, struct asus_led, work);
374 struct asus_laptop *asus = led->asus;
375
376 asus_led_set(asus, led->method, led->wk);
377}
378
379static enum led_brightness asus_led_cdev_get(struct led_classdev *led_cdev)
380{
381 return led_cdev->brightness;
382}
Corentin Charybe18cda2007-01-26 14:04:35 +0100383
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000384/*
Corentin Charybe4ee822009-12-06 16:27:09 +0100385 * Keyboard backlight (also a LED)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000386 */
Corentin Charyd99b5772010-01-22 21:20:57 +0100387static int asus_kled_lvl(struct asus_laptop *asus)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000388{
389 unsigned long long kblv;
390 struct acpi_object_list params;
391 union acpi_object in_obj;
392 acpi_status rv;
393
394 params.count = 1;
395 params.pointer = &in_obj;
396 in_obj.type = ACPI_TYPE_INTEGER;
397 in_obj.integer.value = 2;
398
Corentin Charyd99b5772010-01-22 21:20:57 +0100399 rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
400 &params, &kblv);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000401 if (ACPI_FAILURE(rv)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700402 pr_warn("Error reading kled level\n");
Corentin Chary4d441512010-01-13 22:26:24 +0100403 return -ENODEV;
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000404 }
405 return kblv;
406}
407
Corentin Chary4d441512010-01-13 22:26:24 +0100408static int asus_kled_set(struct asus_laptop *asus, int kblv)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000409{
410 if (kblv > 0)
411 kblv = (1 << 7) | (kblv & 0x7F);
412 else
413 kblv = 0;
414
Corentin Charyd99b5772010-01-22 21:20:57 +0100415 if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700416 pr_warn("Keyboard LED display write failed\n");
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000417 return -EINVAL;
418 }
419 return 0;
420}
421
Corentin Charyaee0afb2010-01-26 21:01:34 +0100422static void asus_kled_cdev_set(struct led_classdev *led_cdev,
423 enum led_brightness value)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000424{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100425 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
426 struct asus_laptop *asus = led->asus;
Corentin Chary9129d142009-12-01 22:39:41 +0100427
Corentin Chary060cbce2010-01-28 10:52:40 +0100428 led->wk = value;
Corentin Charyaee0afb2010-01-26 21:01:34 +0100429 queue_work(asus->led_workqueue, &led->work);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000430}
431
Corentin Charyaee0afb2010-01-26 21:01:34 +0100432static void asus_kled_cdev_update(struct work_struct *work)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000433{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100434 struct asus_led *led = container_of(work, struct asus_led, work);
435 struct asus_laptop *asus = led->asus;
Corentin Chary9129d142009-12-01 22:39:41 +0100436
Corentin Charyaee0afb2010-01-26 21:01:34 +0100437 asus_kled_set(asus, led->wk);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000438}
439
Corentin Charyaee0afb2010-01-26 21:01:34 +0100440static enum led_brightness asus_kled_cdev_get(struct led_classdev *led_cdev)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000441{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100442 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
443 struct asus_laptop *asus = led->asus;
Corentin Charyd99b5772010-01-22 21:20:57 +0100444
445 return asus_kled_lvl(asus);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000446}
447
Corentin Charybe4ee822009-12-06 16:27:09 +0100448static void asus_led_exit(struct asus_laptop *asus)
449{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100450 if (asus->mled.led.dev)
451 led_classdev_unregister(&asus->mled.led);
452 if (asus->tled.led.dev)
453 led_classdev_unregister(&asus->tled.led);
454 if (asus->pled.led.dev)
455 led_classdev_unregister(&asus->pled.led);
456 if (asus->rled.led.dev)
457 led_classdev_unregister(&asus->rled.led);
458 if (asus->gled.led.dev)
459 led_classdev_unregister(&asus->gled.led);
460 if (asus->kled.led.dev)
461 led_classdev_unregister(&asus->kled.led);
462 if (asus->led_workqueue) {
463 destroy_workqueue(asus->led_workqueue);
464 asus->led_workqueue = NULL;
Corentin Charybe4ee822009-12-06 16:27:09 +0100465 }
466}
467
468/* Ugly macro, need to fix that later */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100469static int asus_led_register(struct asus_laptop *asus,
470 struct asus_led *led,
471 const char *name, const char *method)
472{
473 struct led_classdev *led_cdev = &led->led;
474
475 if (!method || acpi_check_handle(asus->handle, method, NULL))
Corentin Chary060cbce2010-01-28 10:52:40 +0100476 return 0; /* Led not present */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100477
478 led->asus = asus;
479 led->method = method;
480
481 INIT_WORK(&led->work, asus_led_cdev_update);
482 led_cdev->name = name;
483 led_cdev->brightness_set = asus_led_cdev_set;
484 led_cdev->brightness_get = asus_led_cdev_get;
485 led_cdev->max_brightness = 1;
486 return led_classdev_register(&asus->platform_device->dev, led_cdev);
487}
Corentin Charybe4ee822009-12-06 16:27:09 +0100488
489static int asus_led_init(struct asus_laptop *asus)
490{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100491 int r;
Corentin Charybe4ee822009-12-06 16:27:09 +0100492
493 /*
494 * Functions that actually update the LED's are called from a
495 * workqueue. By doing this as separate work rather than when the LED
496 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
497 * potentially bad time, such as a timer interrupt.
498 */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100499 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
500 if (!asus->led_workqueue)
Corentin Charybe4ee822009-12-06 16:27:09 +0100501 return -ENOMEM;
502
Corentin Charyaee0afb2010-01-26 21:01:34 +0100503 r = asus_led_register(asus, &asus->mled, "asus::mail", METHOD_MLED);
504 if (r)
505 goto error;
506 r = asus_led_register(asus, &asus->tled, "asus::touchpad", METHOD_TLED);
507 if (r)
508 goto error;
509 r = asus_led_register(asus, &asus->rled, "asus::record", METHOD_RLED);
510 if (r)
511 goto error;
512 r = asus_led_register(asus, &asus->pled, "asus::phone", METHOD_PLED);
513 if (r)
514 goto error;
515 r = asus_led_register(asus, &asus->gled, "asus::gaming", METHOD_GLED);
516 if (r)
517 goto error;
Corentin Charyd99b5772010-01-22 21:20:57 +0100518 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
Corentin Charyaee0afb2010-01-26 21:01:34 +0100519 !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL)) {
520 struct asus_led *led = &asus->kled;
521 struct led_classdev *cdev = &led->led;
522
523 led->asus = asus;
524
525 INIT_WORK(&led->work, asus_kled_cdev_update);
526 cdev->name = "asus::kbd_backlight";
527 cdev->brightness_set = asus_kled_cdev_set;
528 cdev->brightness_get = asus_kled_cdev_get;
529 cdev->max_brightness = 3;
530 r = led_classdev_register(&asus->platform_device->dev, cdev);
531 }
Corentin Charybe4ee822009-12-06 16:27:09 +0100532error:
Corentin Charyaee0afb2010-01-26 21:01:34 +0100533 if (r)
Corentin Charybe4ee822009-12-06 16:27:09 +0100534 asus_led_exit(asus);
Corentin Charyaee0afb2010-01-26 21:01:34 +0100535 return r;
Corentin Charybe4ee822009-12-06 16:27:09 +0100536}
537
538/*
539 * Backlight device
540 */
Corentin Chary4d441512010-01-13 22:26:24 +0100541static int asus_read_brightness(struct backlight_device *bd)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100542{
Corentin Charyd99b5772010-01-22 21:20:57 +0100543 struct asus_laptop *asus = bl_get_data(bd);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400544 unsigned long long value;
Corentin Chary9a816852007-03-11 10:25:38 +0100545 acpi_status rv = AE_OK;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100546
Corentin Charyd99b5772010-01-22 21:20:57 +0100547 rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
548 NULL, &value);
Corentin Chary9a816852007-03-11 10:25:38 +0100549 if (ACPI_FAILURE(rv))
Joe Perches5ad77dc2011-03-29 15:21:35 -0700550 pr_warn("Error reading brightness\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100551
552 return value;
553}
554
Corentin Chary4d441512010-01-13 22:26:24 +0100555static int asus_set_brightness(struct backlight_device *bd, int value)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100556{
Corentin Charyd99b5772010-01-22 21:20:57 +0100557 struct asus_laptop *asus = bl_get_data(bd);
558
559 if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700560 pr_warn("Error changing brightness\n");
Corentin Charye5b50f62009-11-28 10:19:55 +0100561 return -EIO;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100562 }
Corentin Charye5b50f62009-11-28 10:19:55 +0100563 return 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100564}
565
566static int update_bl_status(struct backlight_device *bd)
567{
Richard Purdie599a52d2007-02-10 23:07:48 +0000568 int value = bd->props.brightness;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100569
Corentin Chary3b81cf92011-02-06 13:27:31 +0100570 return asus_set_brightness(bd, value);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100571}
572
Lionel Debrouxacc24722010-11-16 14:14:02 +0100573static const struct backlight_ops asusbl_ops = {
Corentin Chary4d441512010-01-13 22:26:24 +0100574 .get_brightness = asus_read_brightness,
Corentin Charybe4ee822009-12-06 16:27:09 +0100575 .update_status = update_bl_status,
576};
577
Corentin Charya539df52010-01-25 22:53:21 +0100578static int asus_backlight_notify(struct asus_laptop *asus)
579{
580 struct backlight_device *bd = asus->backlight_device;
581 int old = bd->props.brightness;
582
583 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
584
585 return old;
586}
587
Corentin Charybe4ee822009-12-06 16:27:09 +0100588static int asus_backlight_init(struct asus_laptop *asus)
589{
590 struct backlight_device *bd;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500591 struct backlight_properties props;
Corentin Charybe4ee822009-12-06 16:27:09 +0100592
Corentin Chary71e687d2010-08-24 09:30:44 +0200593 if (acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) ||
Corentin Chary3b81cf92011-02-06 13:27:31 +0100594 acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL))
Corentin Chary71e687d2010-08-24 09:30:44 +0200595 return 0;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500596
Corentin Chary71e687d2010-08-24 09:30:44 +0200597 memset(&props, 0, sizeof(struct backlight_properties));
598 props.max_brightness = 15;
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700599 props.type = BACKLIGHT_PLATFORM;
Corentin Charybe4ee822009-12-06 16:27:09 +0100600
Corentin Chary71e687d2010-08-24 09:30:44 +0200601 bd = backlight_device_register(ASUS_LAPTOP_FILE,
602 &asus->platform_device->dev, asus,
603 &asusbl_ops, &props);
604 if (IS_ERR(bd)) {
605 pr_err("Could not register asus backlight device\n");
606 asus->backlight_device = NULL;
607 return PTR_ERR(bd);
Corentin Charybe4ee822009-12-06 16:27:09 +0100608 }
Corentin Chary71e687d2010-08-24 09:30:44 +0200609
610 asus->backlight_device = bd;
611 bd->props.brightness = asus_read_brightness(bd);
612 bd->props.power = FB_BLANK_UNBLANK;
613 backlight_update_status(bd);
Corentin Charybe4ee822009-12-06 16:27:09 +0100614 return 0;
615}
616
617static void asus_backlight_exit(struct asus_laptop *asus)
618{
619 if (asus->backlight_device)
620 backlight_device_unregister(asus->backlight_device);
Corentin Charya539df52010-01-25 22:53:21 +0100621 asus->backlight_device = NULL;
Corentin Charybe4ee822009-12-06 16:27:09 +0100622}
623
Corentin Chary85091b72007-01-26 14:04:30 +0100624/*
625 * Platform device handlers
626 */
627
628/*
629 * We write our info in page, we begin at offset off and cannot write more
630 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
631 * number of bytes written in page
632 */
633static ssize_t show_infos(struct device *dev,
Len Brown8def05f2007-01-30 01:46:43 -0500634 struct device_attribute *attr, char *page)
Corentin Chary85091b72007-01-26 14:04:30 +0100635{
Corentin Chary9129d142009-12-01 22:39:41 +0100636 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary85091b72007-01-26 14:04:30 +0100637 int len = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400638 unsigned long long temp;
Corentin Charybe966662009-08-29 10:28:29 +0200639 char buf[16]; /* enough for all info */
Corentin Chary9a816852007-03-11 10:25:38 +0100640 acpi_status rv = AE_OK;
641
Corentin Chary85091b72007-01-26 14:04:30 +0100642 /*
Corentin Chary060cbce2010-01-28 10:52:40 +0100643 * We use the easy way, we don't care of off and count,
644 * so we don't set eof to 1
Corentin Chary85091b72007-01-26 14:04:30 +0100645 */
646
Corentin Chary50a90c42009-11-30 21:55:12 +0100647 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
648 len += sprintf(page + len, "Model reference : %s\n", asus->name);
Corentin Chary85091b72007-01-26 14:04:30 +0100649 /*
650 * The SFUN method probably allows the original driver to get the list
651 * of features supported by a given model. For now, 0x0100 or 0x0800
652 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
653 * The significance of others is yet to be found.
654 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100655 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100656 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000657 len += sprintf(page + len, "SFUN value : %#x\n",
658 (uint) temp);
659 /*
660 * The HWRS method return informations about the hardware.
661 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
662 * The significance of others is yet to be found.
663 * If we don't find the method, we assume the device are present.
664 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100665 rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
Corentin Chary1d4a3802009-08-28 12:56:46 +0000666 if (!ACPI_FAILURE(rv))
667 len += sprintf(page + len, "HRWS value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100668 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100669 /*
670 * Another value for userspace: the ASYM method returns 0x02 for
671 * battery low and 0x04 for battery critical, its readings tend to be
672 * more accurate than those provided by _BST.
673 * Note: since not all the laptops provide this method, errors are
674 * silently ignored.
675 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100676 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100677 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000678 len += sprintf(page + len, "ASYM value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100679 (uint) temp);
Corentin Chary7c247642009-11-30 22:13:54 +0100680 if (asus->dsdt_info) {
681 snprintf(buf, 16, "%d", asus->dsdt_info->length);
Corentin Chary85091b72007-01-26 14:04:30 +0100682 len += sprintf(page + len, "DSDT length : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100683 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
Corentin Chary85091b72007-01-26 14:04:30 +0100684 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100685 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100686 len += sprintf(page + len, "DSDT revision : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100687 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100688 len += sprintf(page + len, "OEM id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100689 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100690 len += sprintf(page + len, "OEM table id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100691 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100692 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100693 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100694 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100695 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100696 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
697 }
698
699 return len;
700}
701
702static int parse_arg(const char *buf, unsigned long count, int *val)
703{
704 if (!count)
705 return 0;
706 if (count > 31)
707 return -EINVAL;
708 if (sscanf(buf, "%i", val) != 1)
709 return -EINVAL;
710 return count;
711}
712
Corentin Chary17e78f62010-01-13 22:21:33 +0100713static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
714 const char *buf, size_t count,
Corentin Charyd99b5772010-01-22 21:20:57 +0100715 const char *method)
Corentin Chary4564de12007-01-26 14:04:40 +0100716{
717 int rv, value;
718 int out = 0;
719
720 rv = parse_arg(buf, count, &value);
721 if (rv > 0)
722 out = value ? 1 : 0;
723
Corentin Charyd99b5772010-01-22 21:20:57 +0100724 if (write_acpi_int(asus->handle, method, value))
Corentin Chary17e78f62010-01-13 22:21:33 +0100725 return -ENODEV;
Corentin Chary4564de12007-01-26 14:04:40 +0100726 return rv;
727}
728
729/*
Corentin Chary722ad972007-01-26 14:04:55 +0100730 * LEDD display
731 */
732static ssize_t show_ledd(struct device *dev,
733 struct device_attribute *attr, char *buf)
734{
Corentin Chary9129d142009-12-01 22:39:41 +0100735 struct asus_laptop *asus = dev_get_drvdata(dev);
736
Corentin Chary50a90c42009-11-30 21:55:12 +0100737 return sprintf(buf, "0x%08x\n", asus->ledd_status);
Corentin Chary722ad972007-01-26 14:04:55 +0100738}
739
740static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
741 const char *buf, size_t count)
742{
Corentin Chary9129d142009-12-01 22:39:41 +0100743 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary722ad972007-01-26 14:04:55 +0100744 int rv, value;
745
746 rv = parse_arg(buf, count, &value);
747 if (rv > 0) {
Axel Lin6a984a02010-07-20 15:19:48 -0700748 if (write_acpi_int(asus->handle, METHOD_LEDD, value)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700749 pr_warn("LED display write failed\n");
Axel Lin6a984a02010-07-20 15:19:48 -0700750 return -ENODEV;
751 }
752 asus->ledd_status = (u32) value;
Corentin Chary722ad972007-01-26 14:04:55 +0100753 }
754 return rv;
755}
756
757/*
Corentin Charyaa9df932010-01-13 21:49:10 +0100758 * Wireless
759 */
760static int asus_wireless_status(struct asus_laptop *asus, int mask)
761{
762 unsigned long long status;
763 acpi_status rv = AE_OK;
764
765 if (!asus->have_rsts)
766 return (asus->wireless_status & mask) ? 1 : 0;
767
Corentin Charyd99b5772010-01-22 21:20:57 +0100768 rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
769 NULL, &status);
Corentin Charyaa9df932010-01-13 21:49:10 +0100770 if (ACPI_FAILURE(rv)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700771 pr_warn("Error reading Wireless status\n");
Corentin Charyaa9df932010-01-13 21:49:10 +0100772 return -EINVAL;
773 }
774 return !!(status & mask);
775}
776
777/*
Corentin Chary4564de12007-01-26 14:04:40 +0100778 * WLAN
779 */
Corentin Charye5593bf2010-01-17 17:20:11 +0100780static int asus_wlan_set(struct asus_laptop *asus, int status)
781{
782 if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700783 pr_warn("Error setting wlan status to %d\n", status);
Corentin Charye5593bf2010-01-17 17:20:11 +0100784 return -EIO;
785 }
786 return 0;
787}
788
Corentin Chary4564de12007-01-26 14:04:40 +0100789static ssize_t show_wlan(struct device *dev,
790 struct device_attribute *attr, char *buf)
791{
Corentin Chary9129d142009-12-01 22:39:41 +0100792 struct asus_laptop *asus = dev_get_drvdata(dev);
793
Corentin Chary17e78f62010-01-13 22:21:33 +0100794 return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100795}
796
797static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
798 const char *buf, size_t count)
799{
Corentin Chary9129d142009-12-01 22:39:41 +0100800 struct asus_laptop *asus = dev_get_drvdata(dev);
801
Corentin Charyd99b5772010-01-22 21:20:57 +0100802 return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
Corentin Chary4564de12007-01-26 14:04:40 +0100803}
804
805/*
806 * Bluetooth
807 */
Corentin Charye5593bf2010-01-17 17:20:11 +0100808static int asus_bluetooth_set(struct asus_laptop *asus, int status)
809{
810 if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700811 pr_warn("Error setting bluetooth status to %d\n", status);
Corentin Charye5593bf2010-01-17 17:20:11 +0100812 return -EIO;
813 }
814 return 0;
815}
816
Corentin Chary4564de12007-01-26 14:04:40 +0100817static ssize_t show_bluetooth(struct device *dev,
818 struct device_attribute *attr, char *buf)
819{
Corentin Chary9129d142009-12-01 22:39:41 +0100820 struct asus_laptop *asus = dev_get_drvdata(dev);
821
Corentin Chary17e78f62010-01-13 22:21:33 +0100822 return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100823}
824
Len Brown8def05f2007-01-30 01:46:43 -0500825static ssize_t store_bluetooth(struct device *dev,
826 struct device_attribute *attr, const char *buf,
827 size_t count)
Corentin Chary4564de12007-01-26 14:04:40 +0100828{
Corentin Chary9129d142009-12-01 22:39:41 +0100829 struct asus_laptop *asus = dev_get_drvdata(dev);
830
Corentin Charyd99b5772010-01-22 21:20:57 +0100831 return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
Corentin Chary4564de12007-01-26 14:04:40 +0100832}
833
Corentin Chary78127b42007-01-26 14:04:49 +0100834/*
Corentin Charyba1ff5b2010-11-14 17:40:12 +0100835 * Wimax
836 */
837static int asus_wimax_set(struct asus_laptop *asus, int status)
838{
839 if (write_acpi_int(asus->handle, METHOD_WIMAX, !!status)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700840 pr_warn("Error setting wimax status to %d\n", status);
Corentin Charyba1ff5b2010-11-14 17:40:12 +0100841 return -EIO;
842 }
843 return 0;
844}
845
846static ssize_t show_wimax(struct device *dev,
847 struct device_attribute *attr, char *buf)
848{
849 struct asus_laptop *asus = dev_get_drvdata(dev);
850
851 return sprintf(buf, "%d\n", asus_wireless_status(asus, WM_RSTS));
852}
853
854static ssize_t store_wimax(struct device *dev,
855 struct device_attribute *attr, const char *buf,
856 size_t count)
857{
858 struct asus_laptop *asus = dev_get_drvdata(dev);
859
860 return sysfs_acpi_set(asus, buf, count, METHOD_WIMAX);
861}
862
863/*
864 * Wwan
865 */
866static int asus_wwan_set(struct asus_laptop *asus, int status)
867{
868 if (write_acpi_int(asus->handle, METHOD_WWAN, !!status)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700869 pr_warn("Error setting wwan status to %d\n", status);
Corentin Charyba1ff5b2010-11-14 17:40:12 +0100870 return -EIO;
871 }
872 return 0;
873}
874
875static ssize_t show_wwan(struct device *dev,
876 struct device_attribute *attr, char *buf)
877{
878 struct asus_laptop *asus = dev_get_drvdata(dev);
879
880 return sprintf(buf, "%d\n", asus_wireless_status(asus, WW_RSTS));
881}
882
883static ssize_t store_wwan(struct device *dev,
884 struct device_attribute *attr, const char *buf,
885 size_t count)
886{
887 struct asus_laptop *asus = dev_get_drvdata(dev);
888
889 return sysfs_acpi_set(asus, buf, count, METHOD_WWAN);
890}
891
892/*
Corentin Chary78127b42007-01-26 14:04:49 +0100893 * Display
894 */
Corentin Chary4d441512010-01-13 22:26:24 +0100895static void asus_set_display(struct asus_laptop *asus, int value)
Corentin Chary78127b42007-01-26 14:04:49 +0100896{
897 /* no sanity check needed for now */
Corentin Charyd99b5772010-01-22 21:20:57 +0100898 if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
Joe Perches5ad77dc2011-03-29 15:21:35 -0700899 pr_warn("Error setting display\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100900 return;
901}
902
Corentin Chary78127b42007-01-26 14:04:49 +0100903/*
904 * Experimental support for display switching. As of now: 1 should activate
905 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
906 * Any combination (bitwise) of these will suffice. I never actually tested 4
907 * displays hooked up simultaneously, so be warned. See the acpi4asus README
908 * for more info.
909 */
910static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
911 const char *buf, size_t count)
912{
Corentin Chary9129d142009-12-01 22:39:41 +0100913 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary78127b42007-01-26 14:04:49 +0100914 int rv, value;
915
916 rv = parse_arg(buf, count, &value);
917 if (rv > 0)
Corentin Chary4d441512010-01-13 22:26:24 +0100918 asus_set_display(asus, value);
Corentin Chary78127b42007-01-26 14:04:49 +0100919 return rv;
920}
921
Corentin Chary8b857352007-01-26 14:04:58 +0100922/*
923 * Light Sens
924 */
Corentin Chary4d441512010-01-13 22:26:24 +0100925static void asus_als_switch(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100926{
Corentin Charyd99b5772010-01-22 21:20:57 +0100927 if (write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value))
Joe Perches5ad77dc2011-03-29 15:21:35 -0700928 pr_warn("Error setting light sensor switch\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100929 asus->light_switch = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100930}
931
932static ssize_t show_lssw(struct device *dev,
933 struct device_attribute *attr, char *buf)
934{
Corentin Chary9129d142009-12-01 22:39:41 +0100935 struct asus_laptop *asus = dev_get_drvdata(dev);
936
Corentin Chary50a90c42009-11-30 21:55:12 +0100937 return sprintf(buf, "%d\n", asus->light_switch);
Corentin Chary8b857352007-01-26 14:04:58 +0100938}
939
940static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
941 const char *buf, size_t count)
942{
Corentin Chary9129d142009-12-01 22:39:41 +0100943 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100944 int rv, value;
945
946 rv = parse_arg(buf, count, &value);
947 if (rv > 0)
Corentin Chary4d441512010-01-13 22:26:24 +0100948 asus_als_switch(asus, value ? 1 : 0);
Corentin Chary8b857352007-01-26 14:04:58 +0100949
950 return rv;
951}
952
Corentin Chary4d441512010-01-13 22:26:24 +0100953static void asus_als_level(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100954{
Corentin Charyd99b5772010-01-22 21:20:57 +0100955 if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
Joe Perches5ad77dc2011-03-29 15:21:35 -0700956 pr_warn("Error setting light sensor level\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100957 asus->light_level = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100958}
959
960static ssize_t show_lslvl(struct device *dev,
961 struct device_attribute *attr, char *buf)
962{
Corentin Chary9129d142009-12-01 22:39:41 +0100963 struct asus_laptop *asus = dev_get_drvdata(dev);
964
Corentin Chary50a90c42009-11-30 21:55:12 +0100965 return sprintf(buf, "%d\n", asus->light_level);
Corentin Chary8b857352007-01-26 14:04:58 +0100966}
967
968static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
969 const char *buf, size_t count)
970{
Corentin Chary9129d142009-12-01 22:39:41 +0100971 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100972 int rv, value;
973
974 rv = parse_arg(buf, count, &value);
975 if (rv > 0) {
976 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
977 /* 0 <= value <= 15 */
Corentin Chary4d441512010-01-13 22:26:24 +0100978 asus_als_level(asus, value);
Corentin Chary8b857352007-01-26 14:04:58 +0100979 }
980
981 return rv;
982}
983
Corentin Charye539c2f2007-05-06 14:47:06 +0200984/*
985 * GPS
986 */
Corentin Chary6358bf22010-01-13 21:55:44 +0100987static int asus_gps_status(struct asus_laptop *asus)
988{
989 unsigned long long status;
990 acpi_status rv = AE_OK;
991
Corentin Charyd99b5772010-01-22 21:20:57 +0100992 rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
993 NULL, &status);
Corentin Chary6358bf22010-01-13 21:55:44 +0100994 if (ACPI_FAILURE(rv)) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700995 pr_warn("Error reading GPS status\n");
Corentin Chary6358bf22010-01-13 21:55:44 +0100996 return -ENODEV;
997 }
998 return !!status;
999}
1000
1001static int asus_gps_switch(struct asus_laptop *asus, int status)
1002{
Corentin Charyd99b5772010-01-22 21:20:57 +01001003 const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
Corentin Chary6358bf22010-01-13 21:55:44 +01001004
Corentin Charyd99b5772010-01-22 21:20:57 +01001005 if (write_acpi_int(asus->handle, meth, 0x02))
Corentin Chary6358bf22010-01-13 21:55:44 +01001006 return -ENODEV;
1007 return 0;
1008}
1009
Corentin Charye539c2f2007-05-06 14:47:06 +02001010static ssize_t show_gps(struct device *dev,
1011 struct device_attribute *attr, char *buf)
1012{
Corentin Chary9129d142009-12-01 22:39:41 +01001013 struct asus_laptop *asus = dev_get_drvdata(dev);
1014
Corentin Chary6358bf22010-01-13 21:55:44 +01001015 return sprintf(buf, "%d\n", asus_gps_status(asus));
Corentin Charye539c2f2007-05-06 14:47:06 +02001016}
1017
1018static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
1019 const char *buf, size_t count)
1020{
Corentin Chary060cbce2010-01-28 10:52:40 +01001021 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary6358bf22010-01-13 21:55:44 +01001022 int rv, value;
1023 int ret;
Corentin Chary9129d142009-12-01 22:39:41 +01001024
Corentin Chary6358bf22010-01-13 21:55:44 +01001025 rv = parse_arg(buf, count, &value);
1026 if (rv <= 0)
1027 return -EINVAL;
1028 ret = asus_gps_switch(asus, !!value);
1029 if (ret)
1030 return ret;
Corentin Chary18e13112010-01-25 23:29:24 +01001031 rfkill_set_sw_state(asus->gps_rfkill, !value);
Corentin Chary6358bf22010-01-13 21:55:44 +01001032 return rv;
Corentin Charye539c2f2007-05-06 14:47:06 +02001033}
1034
Corentin Chary034ce902009-01-20 16:17:43 +01001035/*
Corentin Chary18e13112010-01-25 23:29:24 +01001036 * rfkill
1037 */
1038static int asus_gps_rfkill_set(void *data, bool blocked)
1039{
Corentin Chary23f45c32010-08-24 09:30:46 +02001040 struct asus_laptop *asus = data;
Corentin Chary18e13112010-01-25 23:29:24 +01001041
Corentin Chary23f45c32010-08-24 09:30:46 +02001042 return asus_gps_switch(asus, !blocked);
Corentin Chary18e13112010-01-25 23:29:24 +01001043}
1044
1045static const struct rfkill_ops asus_gps_rfkill_ops = {
1046 .set_block = asus_gps_rfkill_set,
1047};
1048
1049static void asus_rfkill_exit(struct asus_laptop *asus)
1050{
1051 if (asus->gps_rfkill) {
1052 rfkill_unregister(asus->gps_rfkill);
1053 rfkill_destroy(asus->gps_rfkill);
1054 asus->gps_rfkill = NULL;
1055 }
1056}
1057
1058static int asus_rfkill_init(struct asus_laptop *asus)
1059{
1060 int result;
1061
1062 if (acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) ||
1063 acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) ||
1064 acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
1065 return 0;
1066
1067 asus->gps_rfkill = rfkill_alloc("asus-gps", &asus->platform_device->dev,
1068 RFKILL_TYPE_GPS,
Corentin Chary23f45c32010-08-24 09:30:46 +02001069 &asus_gps_rfkill_ops, asus);
Corentin Chary18e13112010-01-25 23:29:24 +01001070 if (!asus->gps_rfkill)
1071 return -EINVAL;
1072
1073 result = rfkill_register(asus->gps_rfkill);
1074 if (result) {
1075 rfkill_destroy(asus->gps_rfkill);
1076 asus->gps_rfkill = NULL;
1077 }
1078
1079 return result;
1080}
1081
1082/*
Corentin Charybe4ee822009-12-06 16:27:09 +01001083 * Input device (i.e. hotkeys)
Corentin Chary034ce902009-01-20 16:17:43 +01001084 */
Corentin Charybe4ee822009-12-06 16:27:09 +01001085static void asus_input_notify(struct asus_laptop *asus, int event)
1086{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001087 if (asus->inputdev)
1088 sparse_keymap_report_event(asus->inputdev, event, 1, true);
Corentin Charybe4ee822009-12-06 16:27:09 +01001089}
1090
1091static int asus_input_init(struct asus_laptop *asus)
1092{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001093 struct input_dev *input;
1094 int error;
Corentin Charybe4ee822009-12-06 16:27:09 +01001095
Corentin Chary66a71dd2010-01-25 22:50:11 +01001096 input = input_allocate_device();
1097 if (!input) {
Corentin Charybe4ee822009-12-06 16:27:09 +01001098 pr_info("Unable to allocate input device\n");
Axel Lin45036ae2010-07-05 15:29:00 +08001099 return -ENOMEM;
Corentin Charybe4ee822009-12-06 16:27:09 +01001100 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001101 input->name = "Asus Laptop extra buttons";
1102 input->phys = ASUS_LAPTOP_FILE "/input0";
1103 input->id.bustype = BUS_HOST;
1104 input->dev.parent = &asus->platform_device->dev;
Corentin Charybe4ee822009-12-06 16:27:09 +01001105
Corentin Chary66a71dd2010-01-25 22:50:11 +01001106 error = sparse_keymap_setup(input, asus_keymap, NULL);
1107 if (error) {
1108 pr_err("Unable to setup input device keymap\n");
Axel Lin45036ae2010-07-05 15:29:00 +08001109 goto err_free_dev;
Corentin Charybe4ee822009-12-06 16:27:09 +01001110 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001111 error = input_register_device(input);
1112 if (error) {
Corentin Charybe4ee822009-12-06 16:27:09 +01001113 pr_info("Unable to register input device\n");
Axel Lin45036ae2010-07-05 15:29:00 +08001114 goto err_free_keymap;
Corentin Charybe4ee822009-12-06 16:27:09 +01001115 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001116
1117 asus->inputdev = input;
1118 return 0;
1119
Axel Lin45036ae2010-07-05 15:29:00 +08001120err_free_keymap:
Corentin Chary66a71dd2010-01-25 22:50:11 +01001121 sparse_keymap_free(input);
Axel Lin45036ae2010-07-05 15:29:00 +08001122err_free_dev:
Corentin Chary66a71dd2010-01-25 22:50:11 +01001123 input_free_device(input);
1124 return error;
Corentin Charybe4ee822009-12-06 16:27:09 +01001125}
1126
1127static void asus_input_exit(struct asus_laptop *asus)
1128{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001129 if (asus->inputdev) {
1130 sparse_keymap_free(asus->inputdev);
Corentin Charybe4ee822009-12-06 16:27:09 +01001131 input_unregister_device(asus->inputdev);
Corentin Chary66a71dd2010-01-25 22:50:11 +01001132 }
Corentin Chary71e687d2010-08-24 09:30:44 +02001133 asus->inputdev = NULL;
Corentin Charybe4ee822009-12-06 16:27:09 +01001134}
1135
1136/*
1137 * ACPI driver
1138 */
Corentin Chary600ad522009-11-30 21:42:42 +01001139static void asus_acpi_notify(struct acpi_device *device, u32 event)
Corentin Chary85091b72007-01-26 14:04:30 +01001140{
Corentin Chary9129d142009-12-01 22:39:41 +01001141 struct asus_laptop *asus = acpi_driver_data(device);
Corentin Chary6050c8d2009-02-15 19:30:19 +01001142 u16 count;
Corentin Chary034ce902009-01-20 16:17:43 +01001143
Corentin Chary619d8b12009-11-28 10:35:37 +01001144 /* TODO Find a better way to handle events count. */
Corentin Chary50a90c42009-11-30 21:55:12 +01001145 count = asus->event_count[event % 128]++;
1146 acpi_bus_generate_proc_event(asus->device, event, count);
1147 acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
1148 dev_name(&asus->device->dev), event,
Corentin Chary6050c8d2009-02-15 19:30:19 +01001149 count);
Corentin Chary85091b72007-01-26 14:04:30 +01001150
Corentin Charya539df52010-01-25 22:53:21 +01001151 /* Brightness events are special */
1152 if (event >= ATKD_BR_MIN && event <= ATKD_BR_MAX) {
1153
1154 /* Ignore them completely if the acpi video driver is used */
1155 if (asus->backlight_device != NULL) {
1156 /* Update the backlight device. */
1157 asus_backlight_notify(asus);
1158 }
1159 return ;
1160 }
Corentin Charybe4ee822009-12-06 16:27:09 +01001161 asus_input_notify(asus, event);
Corentin Chary85091b72007-01-26 14:04:30 +01001162}
1163
Corentin Chary2a1fd642010-01-26 21:02:23 +01001164static DEVICE_ATTR(infos, S_IRUGO, show_infos, NULL);
1165static DEVICE_ATTR(wlan, S_IRUGO | S_IWUSR, show_wlan, store_wlan);
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001166static DEVICE_ATTR(bluetooth, S_IRUGO | S_IWUSR,
1167 show_bluetooth, store_bluetooth);
Corentin Charyba1ff5b2010-11-14 17:40:12 +01001168static DEVICE_ATTR(wimax, S_IRUGO | S_IWUSR, show_wimax, store_wimax);
1169static DEVICE_ATTR(wwan, S_IRUGO | S_IWUSR, show_wwan, store_wwan);
Corentin Chary3b81cf92011-02-06 13:27:31 +01001170static DEVICE_ATTR(display, S_IWUSR, NULL, store_disp);
Corentin Chary2a1fd642010-01-26 21:02:23 +01001171static DEVICE_ATTR(ledd, S_IRUGO | S_IWUSR, show_ledd, store_ledd);
1172static DEVICE_ATTR(ls_level, S_IRUGO | S_IWUSR, show_lslvl, store_lslvl);
1173static DEVICE_ATTR(ls_switch, S_IRUGO | S_IWUSR, show_lssw, store_lssw);
1174static DEVICE_ATTR(gps, S_IRUGO | S_IWUSR, show_gps, store_gps);
1175
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001176static struct attribute *asus_attributes[] = {
1177 &dev_attr_infos.attr,
1178 &dev_attr_wlan.attr,
1179 &dev_attr_bluetooth.attr,
Corentin Charyba1ff5b2010-11-14 17:40:12 +01001180 &dev_attr_wimax.attr,
1181 &dev_attr_wwan.attr,
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001182 &dev_attr_display.attr,
1183 &dev_attr_ledd.attr,
1184 &dev_attr_ls_level.attr,
1185 &dev_attr_ls_switch.attr,
1186 &dev_attr_gps.attr,
1187 NULL
1188};
Corentin Chary2a1fd642010-01-26 21:02:23 +01001189
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001190static mode_t asus_sysfs_is_visible(struct kobject *kobj,
1191 struct attribute *attr,
1192 int idx)
1193{
1194 struct device *dev = container_of(kobj, struct device, kobj);
1195 struct platform_device *pdev = to_platform_device(dev);
1196 struct asus_laptop *asus = platform_get_drvdata(pdev);
1197 acpi_handle handle = asus->handle;
1198 bool supported;
1199
1200 if (attr == &dev_attr_wlan.attr) {
1201 supported = !acpi_check_handle(handle, METHOD_WLAN, NULL);
1202
1203 } else if (attr == &dev_attr_bluetooth.attr) {
1204 supported = !acpi_check_handle(handle, METHOD_BLUETOOTH, NULL);
1205
1206 } else if (attr == &dev_attr_display.attr) {
1207 supported = !acpi_check_handle(handle, METHOD_SWITCH_DISPLAY, NULL);
1208
Corentin Charyba1ff5b2010-11-14 17:40:12 +01001209 } else if (attr == &dev_attr_wimax.attr) {
1210 supported =
1211 !acpi_check_handle(asus->handle, METHOD_WIMAX, NULL);
1212
1213 } else if (attr == &dev_attr_wwan.attr) {
1214 supported = !acpi_check_handle(asus->handle, METHOD_WWAN, NULL);
1215
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001216 } else if (attr == &dev_attr_ledd.attr) {
1217 supported = !acpi_check_handle(handle, METHOD_LEDD, NULL);
1218
1219 } else if (attr == &dev_attr_ls_switch.attr ||
1220 attr == &dev_attr_ls_level.attr) {
1221 supported = !acpi_check_handle(handle, METHOD_ALS_CONTROL, NULL) &&
1222 !acpi_check_handle(handle, METHOD_ALS_LEVEL, NULL);
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001223 } else if (attr == &dev_attr_gps.attr) {
1224 supported = !acpi_check_handle(handle, METHOD_GPS_ON, NULL) &&
1225 !acpi_check_handle(handle, METHOD_GPS_OFF, NULL) &&
1226 !acpi_check_handle(handle, METHOD_GPS_STATUS, NULL);
1227 } else {
1228 supported = true;
1229 }
1230
1231 return supported ? attr->mode : 0;
Corentin Chary2a1fd642010-01-26 21:02:23 +01001232}
1233
Corentin Chary2a1fd642010-01-26 21:02:23 +01001234
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001235static const struct attribute_group asus_attr_group = {
1236 .is_visible = asus_sysfs_is_visible,
1237 .attrs = asus_attributes,
1238};
Corentin Chary85091b72007-01-26 14:04:30 +01001239
Corentin Chary9129d142009-12-01 22:39:41 +01001240static int asus_platform_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001241{
Corentin Chary71e687d2010-08-24 09:30:44 +02001242 int result;
Corentin Chary600ad522009-11-30 21:42:42 +01001243
Corentin Chary50a90c42009-11-30 21:55:12 +01001244 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1245 if (!asus->platform_device)
Corentin Chary600ad522009-11-30 21:42:42 +01001246 return -ENOMEM;
Corentin Chary9129d142009-12-01 22:39:41 +01001247 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001248
Corentin Chary71e687d2010-08-24 09:30:44 +02001249 result = platform_device_add(asus->platform_device);
1250 if (result)
Corentin Chary600ad522009-11-30 21:42:42 +01001251 goto fail_platform_device;
1252
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001253 result = sysfs_create_group(&asus->platform_device->dev.kobj,
1254 &asus_attr_group);
Corentin Chary71e687d2010-08-24 09:30:44 +02001255 if (result)
Corentin Chary600ad522009-11-30 21:42:42 +01001256 goto fail_sysfs;
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001257
Corentin Chary600ad522009-11-30 21:42:42 +01001258 return 0;
1259
1260fail_sysfs:
Corentin Chary50a90c42009-11-30 21:55:12 +01001261 platform_device_del(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001262fail_platform_device:
Corentin Chary50a90c42009-11-30 21:55:12 +01001263 platform_device_put(asus->platform_device);
Corentin Chary71e687d2010-08-24 09:30:44 +02001264 return result;
Corentin Chary600ad522009-11-30 21:42:42 +01001265}
1266
Corentin Chary9129d142009-12-01 22:39:41 +01001267static void asus_platform_exit(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001268{
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001269 sysfs_remove_group(&asus->platform_device->dev.kobj, &asus_attr_group);
Corentin Chary50a90c42009-11-30 21:55:12 +01001270 platform_device_unregister(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001271}
1272
1273static struct platform_driver platform_driver = {
Len Brown8def05f2007-01-30 01:46:43 -05001274 .driver = {
Corentin Chary9129d142009-12-01 22:39:41 +01001275 .name = ASUS_LAPTOP_FILE,
1276 .owner = THIS_MODULE,
1277 }
Corentin Chary85091b72007-01-26 14:04:30 +01001278};
1279
Corentin Chary85091b72007-01-26 14:04:30 +01001280/*
Corentin Chary50a90c42009-11-30 21:55:12 +01001281 * This function is used to initialize the context with right values. In this
1282 * method, we can make all the detection we want, and modify the asus_laptop
1283 * struct
Corentin Chary85091b72007-01-26 14:04:30 +01001284 */
Corentin Chary7c247642009-11-30 22:13:54 +01001285static int asus_laptop_get_info(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001286{
1287 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Corentin Chary85091b72007-01-26 14:04:30 +01001288 union acpi_object *model = NULL;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001289 unsigned long long bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +01001290 char *string = NULL;
1291 acpi_status status;
1292
1293 /*
1294 * Get DSDT headers early enough to allow for differentiating between
1295 * models, but late enough to allow acpi_bus_register_driver() to fail
1296 * before doing anything ACPI-specific. Should we encounter a machine,
1297 * which needs special handling (i.e. its hotkey device has a different
Corentin Chary7c247642009-11-30 22:13:54 +01001298 * HID), this bit will be moved.
Corentin Chary85091b72007-01-26 14:04:30 +01001299 */
Corentin Chary7c247642009-11-30 22:13:54 +01001300 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
Corentin Chary85091b72007-01-26 14:04:30 +01001301 if (ACPI_FAILURE(status))
Joe Perches5ad77dc2011-03-29 15:21:35 -07001302 pr_warn("Couldn't get the DSDT table header\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001303
1304 /* We have to write 0 on init this far for all ASUS models */
Corentin Chary50a90c42009-11-30 21:55:12 +01001305 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001306 pr_err("Hotkey initialization failed\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001307 return -ENODEV;
1308 }
1309
1310 /* This needs to be called for some laptops to init properly */
Corentin Chary9a816852007-03-11 10:25:38 +01001311 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001312 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
Corentin Chary9a816852007-03-11 10:25:38 +01001313 if (ACPI_FAILURE(status))
Joe Perches5ad77dc2011-03-29 15:21:35 -07001314 pr_warn("Error calling BSTS\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001315 else if (bsts_result)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001316 pr_notice("BSTS called, 0x%02x returned\n",
Corentin Chary9a816852007-03-11 10:25:38 +01001317 (uint) bsts_result);
Corentin Chary85091b72007-01-26 14:04:30 +01001318
Corentin Chary185e5af2007-03-11 10:27:33 +01001319 /* This too ... */
Corentin Charye5593bf2010-01-17 17:20:11 +01001320 if (write_acpi_int(asus->handle, "CWAP", wapf))
1321 pr_err("Error calling CWAP(%d)\n", wapf);
Corentin Chary85091b72007-01-26 14:04:30 +01001322 /*
1323 * Try to match the object returned by INIT to the specific model.
1324 * Handle every possible object (or the lack of thereof) the DSDT
1325 * writers might throw at us. When in trouble, we pass NULL to
1326 * asus_model_match() and try something completely different.
1327 */
1328 if (buffer.pointer) {
1329 model = buffer.pointer;
1330 switch (model->type) {
1331 case ACPI_TYPE_STRING:
1332 string = model->string.pointer;
1333 break;
1334 case ACPI_TYPE_BUFFER:
1335 string = model->buffer.pointer;
1336 break;
1337 default:
1338 string = "";
1339 break;
1340 }
1341 }
Corentin Chary50a90c42009-11-30 21:55:12 +01001342 asus->name = kstrdup(string, GFP_KERNEL);
Axel Lind8eca112010-06-29 11:09:47 +08001343 if (!asus->name) {
1344 kfree(buffer.pointer);
Corentin Chary85091b72007-01-26 14:04:30 +01001345 return -ENOMEM;
Axel Lind8eca112010-06-29 11:09:47 +08001346 }
Corentin Chary85091b72007-01-26 14:04:30 +01001347
Len Brown8def05f2007-01-30 01:46:43 -05001348 if (*string)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001349 pr_notice(" %s model detected\n", string);
Corentin Chary85091b72007-01-26 14:04:30 +01001350
Corentin Chary4564de12007-01-26 14:04:40 +01001351 /*
1352 * The HWRS method return informations about the hardware.
Corentin Charyba1ff5b2010-11-14 17:40:12 +01001353 * 0x80 bit is for WLAN, 0x100 for Bluetooth,
1354 * 0x40 for WWAN, 0x10 for WIMAX.
Corentin Chary4564de12007-01-26 14:04:40 +01001355 * The significance of others is yet to be found.
Corentin Chary4564de12007-01-26 14:04:40 +01001356 */
Corentin Chary9a816852007-03-11 10:25:38 +01001357 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001358 acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
Corentin Charyd99b5772010-01-22 21:20:57 +01001359 if (!ACPI_FAILURE(status))
1360 pr_notice(" HRWS returned %x", (int)hwrs_result);
Corentin Chary4564de12007-01-26 14:04:40 +01001361
Corentin Charyd99b5772010-01-22 21:20:57 +01001362 if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
Corentin Charyaa9df932010-01-13 21:49:10 +01001363 asus->have_rsts = true;
Corentin Chary4564de12007-01-26 14:04:40 +01001364
Corentin Chary85091b72007-01-26 14:04:30 +01001365 kfree(model);
1366
1367 return AE_OK;
1368}
1369
Corentin Chary9129d142009-12-01 22:39:41 +01001370static int __devinit asus_acpi_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001371{
1372 int result = 0;
1373
Corentin Chary50a90c42009-11-30 21:55:12 +01001374 result = acpi_bus_get_status(asus->device);
Corentin Chary600ad522009-11-30 21:42:42 +01001375 if (result)
1376 return result;
Corentin Chary50a90c42009-11-30 21:55:12 +01001377 if (!asus->device->status.present) {
Corentin Chary600ad522009-11-30 21:42:42 +01001378 pr_err("Hotkey device not present, aborting\n");
1379 return -ENODEV;
1380 }
1381
Corentin Chary7c247642009-11-30 22:13:54 +01001382 result = asus_laptop_get_info(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001383 if (result)
1384 return result;
1385
Corentin Chary600ad522009-11-30 21:42:42 +01001386 /* WLED and BLED are on by default */
Corentin Charybe4ee822009-12-06 16:27:09 +01001387 if (bluetooth_status >= 0)
Corentin Charye5593bf2010-01-17 17:20:11 +01001388 asus_bluetooth_set(asus, !!bluetooth_status);
1389
Corentin Charyd0930a22010-01-17 17:21:13 +01001390 if (wlan_status >= 0)
1391 asus_wlan_set(asus, !!wlan_status);
Corentin Chary600ad522009-11-30 21:42:42 +01001392
Corentin Charyba1ff5b2010-11-14 17:40:12 +01001393 if (wimax_status >= 0)
1394 asus_wimax_set(asus, !!wimax_status);
1395
1396 if (wwan_status >= 0)
1397 asus_wwan_set(asus, !!wwan_status);
1398
Corentin Chary600ad522009-11-30 21:42:42 +01001399 /* Keyboard Backlight is on by default */
Corentin Charyd99b5772010-01-22 21:20:57 +01001400 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
Corentin Chary4d441512010-01-13 22:26:24 +01001401 asus_kled_set(asus, 1);
Corentin Chary600ad522009-11-30 21:42:42 +01001402
1403 /* LED display is off by default */
Corentin Chary50a90c42009-11-30 21:55:12 +01001404 asus->ledd_status = 0xFFF;
Corentin Chary600ad522009-11-30 21:42:42 +01001405
1406 /* Set initial values of light sensor and level */
Corentin Charybe4ee822009-12-06 16:27:09 +01001407 asus->light_switch = 0; /* Default to light sensor disabled */
1408 asus->light_level = 5; /* level 5 for sensor sensitivity */
Corentin Chary600ad522009-11-30 21:42:42 +01001409
Corentin Charyd99b5772010-01-22 21:20:57 +01001410 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1411 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
Corentin Chary4d441512010-01-13 22:26:24 +01001412 asus_als_switch(asus, asus->light_switch);
Corentin Chary4d441512010-01-13 22:26:24 +01001413 asus_als_level(asus, asus->light_level);
Corentin Charyd99b5772010-01-22 21:20:57 +01001414 }
Corentin Chary600ad522009-11-30 21:42:42 +01001415
Corentin Chary600ad522009-11-30 21:42:42 +01001416 return result;
1417}
1418
Corentin Charyaf96f872011-02-06 13:27:30 +01001419static void __devinit asus_dmi_check(void)
1420{
1421 const char *model;
1422
1423 model = dmi_get_system_info(DMI_PRODUCT_NAME);
1424 if (!model)
1425 return;
1426
1427 /* On L1400B WLED control the sound card, don't mess with it ... */
1428 if (strncmp(model, "L1400B", 6) == 0) {
1429 wlan_status = -1;
1430 }
1431}
1432
Corentin Chary71e687d2010-08-24 09:30:44 +02001433static bool asus_device_present;
1434
Corentin Chary600ad522009-11-30 21:42:42 +01001435static int __devinit asus_acpi_add(struct acpi_device *device)
1436{
Corentin Chary9129d142009-12-01 22:39:41 +01001437 struct asus_laptop *asus;
Corentin Chary600ad522009-11-30 21:42:42 +01001438 int result;
1439
1440 pr_notice("Asus Laptop Support version %s\n",
1441 ASUS_LAPTOP_VERSION);
Corentin Chary50a90c42009-11-30 21:55:12 +01001442 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1443 if (!asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001444 return -ENOMEM;
Corentin Chary50a90c42009-11-30 21:55:12 +01001445 asus->handle = device->handle;
1446 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1447 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1448 device->driver_data = asus;
1449 asus->device = device;
Corentin Chary600ad522009-11-30 21:42:42 +01001450
Corentin Charyaf96f872011-02-06 13:27:30 +01001451 asus_dmi_check();
1452
Corentin Chary9129d142009-12-01 22:39:41 +01001453 result = asus_acpi_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001454 if (result)
1455 goto fail_platform;
1456
1457 /*
Andy Ross8819de72011-10-14 11:13:35 +02001458 * Need platform type detection first, then the platform
1459 * device. It is used as a parent for the sub-devices below.
Corentin Chary600ad522009-11-30 21:42:42 +01001460 */
Andy Ross8819de72011-10-14 11:13:35 +02001461 asus->is_pega_lucid = asus_check_pega_lucid(asus);
Corentin Chary9129d142009-12-01 22:39:41 +01001462 result = asus_platform_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001463 if (result)
1464 goto fail_platform;
1465
1466 if (!acpi_video_backlight_support()) {
Corentin Chary9129d142009-12-01 22:39:41 +01001467 result = asus_backlight_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001468 if (result)
1469 goto fail_backlight;
1470 } else
1471 pr_info("Backlight controlled by ACPI video driver\n");
1472
Corentin Chary9129d142009-12-01 22:39:41 +01001473 result = asus_input_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001474 if (result)
1475 goto fail_input;
1476
Corentin Chary9129d142009-12-01 22:39:41 +01001477 result = asus_led_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001478 if (result)
1479 goto fail_led;
1480
Corentin Chary18e13112010-01-25 23:29:24 +01001481 result = asus_rfkill_init(asus);
1482 if (result)
1483 goto fail_rfkill;
1484
Corentin Chary600ad522009-11-30 21:42:42 +01001485 asus_device_present = true;
1486 return 0;
1487
Corentin Chary18e13112010-01-25 23:29:24 +01001488fail_rfkill:
1489 asus_led_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001490fail_led:
Corentin Chary9129d142009-12-01 22:39:41 +01001491 asus_input_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001492fail_input:
Corentin Chary9129d142009-12-01 22:39:41 +01001493 asus_backlight_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001494fail_backlight:
Corentin Chary9129d142009-12-01 22:39:41 +01001495 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001496fail_platform:
Corentin Chary50a90c42009-11-30 21:55:12 +01001497 kfree(asus->name);
1498 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001499
1500 return result;
1501}
1502
1503static int asus_acpi_remove(struct acpi_device *device, int type)
1504{
Corentin Chary9129d142009-12-01 22:39:41 +01001505 struct asus_laptop *asus = acpi_driver_data(device);
1506
1507 asus_backlight_exit(asus);
Corentin Chary18e13112010-01-25 23:29:24 +01001508 asus_rfkill_exit(asus);
Corentin Chary9129d142009-12-01 22:39:41 +01001509 asus_led_exit(asus);
1510 asus_input_exit(asus);
1511 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001512
Corentin Chary50a90c42009-11-30 21:55:12 +01001513 kfree(asus->name);
1514 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001515 return 0;
1516}
1517
1518static const struct acpi_device_id asus_device_ids[] = {
1519 {"ATK0100", 0},
1520 {"ATK0101", 0},
1521 {"", 0},
1522};
1523MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1524
1525static struct acpi_driver asus_acpi_driver = {
Corentin Chary50a90c42009-11-30 21:55:12 +01001526 .name = ASUS_LAPTOP_NAME,
1527 .class = ASUS_LAPTOP_CLASS,
Corentin Chary600ad522009-11-30 21:42:42 +01001528 .owner = THIS_MODULE,
1529 .ids = asus_device_ids,
1530 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1531 .ops = {
1532 .add = asus_acpi_add,
1533 .remove = asus_acpi_remove,
1534 .notify = asus_acpi_notify,
1535 },
1536};
1537
Corentin Chary85091b72007-01-26 14:04:30 +01001538static int __init asus_laptop_init(void)
1539{
1540 int result;
1541
Corentin Chary600ad522009-11-30 21:42:42 +01001542 result = platform_driver_register(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001543 if (result < 0)
1544 return result;
1545
Corentin Chary600ad522009-11-30 21:42:42 +01001546 result = acpi_bus_register_driver(&asus_acpi_driver);
1547 if (result < 0)
1548 goto fail_acpi_driver;
1549 if (!asus_device_present) {
1550 result = -ENODEV;
1551 goto fail_no_device;
Corentin Chary85091b72007-01-26 14:04:30 +01001552 }
Len Brown8def05f2007-01-30 01:46:43 -05001553 return 0;
Corentin Chary85091b72007-01-26 14:04:30 +01001554
Corentin Chary600ad522009-11-30 21:42:42 +01001555fail_no_device:
1556 acpi_bus_unregister_driver(&asus_acpi_driver);
1557fail_acpi_driver:
1558 platform_driver_unregister(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001559 return result;
1560}
1561
Corentin Chary600ad522009-11-30 21:42:42 +01001562static void __exit asus_laptop_exit(void)
1563{
1564 acpi_bus_unregister_driver(&asus_acpi_driver);
1565 platform_driver_unregister(&platform_driver);
1566}
1567
Corentin Chary85091b72007-01-26 14:04:30 +01001568module_init(asus_laptop_init);
1569module_exit(asus_laptop_exit);