blob: c53b3ff7978a52cd339db953f65a307c128f66a7 [file] [log] [blame]
Corentin Chary85091b72007-01-26 14:04:30 +01001/*
2 * asus-laptop.c - Asus Laptop Support
3 *
4 *
5 * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
Corentin Chary8ec555c2007-03-11 10:28:03 +01006 * Copyright (C) 2006-2007 Corentin Chary
Corentin Chary85091b72007-01-26 14:04:30 +01007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 *
23 * The development page for this driver is located at
24 * http://sourceforge.net/projects/acpi4asus/
25 *
26 * Credits:
27 * Pontus Fuchs - Helper functions, cleanup
28 * Johann Wiesner - Small compile fixes
29 * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
30 * Eric Burghard - LED display support for W1N
31 * Josh Green - Light Sens support
Lucas De Marchic8440332011-03-17 17:18:22 -030032 * Thomas Tuttle - His first patch for led support was very helpful
Corentin Charye539c2f2007-05-06 14:47:06 +020033 * Sam Lin - GPS support
Corentin Chary85091b72007-01-26 14:04:30 +010034 */
35
Corentin Chary2fcc23d2009-06-19 14:52:03 +020036#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37
Corentin Chary85091b72007-01-26 14:04:30 +010038#include <linux/kernel.h>
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/types.h>
42#include <linux/err.h>
43#include <linux/proc_fs.h>
Corentin Chary6b7091e2007-01-26 14:04:45 +010044#include <linux/backlight.h>
45#include <linux/fb.h>
Corentin Charybe18cda2007-01-26 14:04:35 +010046#include <linux/leds.h>
Corentin Chary85091b72007-01-26 14:04:30 +010047#include <linux/platform_device.h>
Corentin Chary060cbce2010-01-28 10:52:40 +010048#include <linux/uaccess.h>
Corentin Chary034ce902009-01-20 16:17:43 +010049#include <linux/input.h>
Corentin Chary66a71dd2010-01-25 22:50:11 +010050#include <linux/input/sparse-keymap.h>
Corentin Chary18e13112010-01-25 23:29:24 +010051#include <linux/rfkill.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Corentin Charyaf96f872011-02-06 13:27:30 +010053#include <linux/dmi.h>
Corentin Chary060cbce2010-01-28 10:52:40 +010054#include <acpi/acpi_drivers.h>
55#include <acpi/acpi_bus.h>
Corentin Chary85091b72007-01-26 14:04:30 +010056
Corentin Chary91687cc2009-11-28 10:32:34 +010057#define ASUS_LAPTOP_VERSION "0.42"
Corentin Chary85091b72007-01-26 14:04:30 +010058
Corentin Chary50a90c42009-11-30 21:55:12 +010059#define ASUS_LAPTOP_NAME "Asus Laptop Support"
60#define ASUS_LAPTOP_CLASS "hotkey"
61#define ASUS_LAPTOP_DEVICE_NAME "Hotkey"
62#define ASUS_LAPTOP_FILE KBUILD_MODNAME
63#define ASUS_LAPTOP_PREFIX "\\_SB.ATKD."
Corentin Chary85091b72007-01-26 14:04:30 +010064
Corentin Chary85091b72007-01-26 14:04:30 +010065MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
Corentin Chary50a90c42009-11-30 21:55:12 +010066MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
Corentin Chary85091b72007-01-26 14:04:30 +010067MODULE_LICENSE("GPL");
68
Corentin Charybe966662009-08-29 10:28:29 +020069/*
70 * WAPF defines the behavior of the Fn+Fx wlan key
Corentin Chary185e5af2007-03-11 10:27:33 +010071 * The significance of values is yet to be found, but
72 * most of the time:
73 * 0x0 will do nothing
74 * 0x1 will allow to control the device with Fn+Fx key.
75 * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
76 * 0x5 like 0x1 or 0x4
77 * So, if something doesn't work as you want, just try other values =)
78 */
79static uint wapf = 1;
Axel Linc26d85c2010-07-20 15:19:55 -070080module_param(wapf, uint, 0444);
Corentin Chary185e5af2007-03-11 10:27:33 +010081MODULE_PARM_DESC(wapf, "WAPF value");
82
Dan Carpenter668f4a02010-04-06 13:44:29 +030083static int wlan_status = 1;
84static int bluetooth_status = 1;
Corentin Charyba1ff5b2010-11-14 17:40:12 +010085static int wimax_status = -1;
86static int wwan_status = -1;
Corentin Chary0e875f42010-01-10 20:49:26 +010087
Axel Linc26d85c2010-07-20 15:19:55 -070088module_param(wlan_status, int, 0444);
Corentin Charyd0930a22010-01-17 17:21:13 +010089MODULE_PARM_DESC(wlan_status, "Set the wireless status on boot "
Corentin Chary0e875f42010-01-10 20:49:26 +010090 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
91 "default is 1");
92
Axel Linc26d85c2010-07-20 15:19:55 -070093module_param(bluetooth_status, int, 0444);
Corentin Chary0e875f42010-01-10 20:49:26 +010094MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
95 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
96 "default is 1");
97
Corentin Charyba1ff5b2010-11-14 17:40:12 +010098module_param(wimax_status, int, 0444);
99MODULE_PARM_DESC(wimax_status, "Set the wireless status on boot "
100 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
101 "default is 1");
102
103module_param(wwan_status, int, 0444);
104MODULE_PARM_DESC(wwan_status, "Set the wireless status on boot "
105 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
106 "default is 1");
107
Corentin Charybe4ee822009-12-06 16:27:09 +0100108/*
109 * Some events we use, same for all Asus
110 */
Corentin Chary060cbce2010-01-28 10:52:40 +0100111#define ATKD_BR_UP 0x10 /* (event & ~ATKD_BR_UP) = brightness level */
112#define ATKD_BR_DOWN 0x20 /* (event & ~ATKD_BR_DOWN) = britghness level */
Corentin Charya539df52010-01-25 22:53:21 +0100113#define ATKD_BR_MIN ATKD_BR_UP
Corentin Chary060cbce2010-01-28 10:52:40 +0100114#define ATKD_BR_MAX (ATKD_BR_DOWN | 0xF) /* 0x2f */
Corentin Charybe4ee822009-12-06 16:27:09 +0100115#define ATKD_LCD_ON 0x33
116#define ATKD_LCD_OFF 0x34
117
118/*
119 * Known bits returned by \_SB.ATKD.HWRS
120 */
121#define WL_HWRS 0x80
122#define BT_HWRS 0x100
123
124/*
125 * Flags for hotk status
126 * WL_ON and BT_ON are also used for wireless_status()
127 */
Corentin Chary17e78f62010-01-13 22:21:33 +0100128#define WL_RSTS 0x01 /* internal Wifi */
129#define BT_RSTS 0x02 /* internal Bluetooth */
Corentin Charyba1ff5b2010-11-14 17:40:12 +0100130#define WM_RSTS 0x08 /* internal wimax */
131#define WW_RSTS 0x20 /* internal wwan */
Corentin Charybe4ee822009-12-06 16:27:09 +0100132
Corentin Charybe18cda2007-01-26 14:04:35 +0100133/* LED */
Corentin Charyd99b5772010-01-22 21:20:57 +0100134#define METHOD_MLED "MLED"
135#define METHOD_TLED "TLED"
136#define METHOD_RLED "RLED" /* W1JC */
137#define METHOD_PLED "PLED" /* A7J */
138#define METHOD_GLED "GLED" /* G1, G2 (probably) */
Corentin Charybe18cda2007-01-26 14:04:35 +0100139
Corentin Chary722ad972007-01-26 14:04:55 +0100140/* LEDD */
Corentin Charyd99b5772010-01-22 21:20:57 +0100141#define METHOD_LEDD "SLCM"
Corentin Chary722ad972007-01-26 14:04:55 +0100142
Corentin Charybe966662009-08-29 10:28:29 +0200143/*
144 * Bluetooth and WLAN
Corentin Chary4564de12007-01-26 14:04:40 +0100145 * WLED and BLED are not handled like other XLED, because in some dsdt
146 * they also control the WLAN/Bluetooth device.
147 */
Corentin Charyd99b5772010-01-22 21:20:57 +0100148#define METHOD_WLAN "WLED"
149#define METHOD_BLUETOOTH "BLED"
Corentin Charyba1ff5b2010-11-14 17:40:12 +0100150
151/* WWAN and WIMAX */
152#define METHOD_WWAN "GSMC"
153#define METHOD_WIMAX "WMXC"
154
Corentin Charyd99b5772010-01-22 21:20:57 +0100155#define METHOD_WL_STATUS "RSTS"
Corentin Chary4564de12007-01-26 14:04:40 +0100156
Corentin Chary6b7091e2007-01-26 14:04:45 +0100157/* Brightness */
Corentin Charyd99b5772010-01-22 21:20:57 +0100158#define METHOD_BRIGHTNESS_SET "SPLV"
159#define METHOD_BRIGHTNESS_GET "GPLV"
Corentin Chary6b7091e2007-01-26 14:04:45 +0100160
Corentin Chary78127b42007-01-26 14:04:49 +0100161/* Display */
Corentin Charyd99b5772010-01-22 21:20:57 +0100162#define METHOD_SWITCH_DISPLAY "SDSP"
Corentin Chary060cbce2010-01-28 10:52:40 +0100163
Corentin Charyd99b5772010-01-22 21:20:57 +0100164#define METHOD_ALS_CONTROL "ALSC" /* Z71A Z71V */
165#define METHOD_ALS_LEVEL "ALSL" /* Z71A Z71V */
Corentin Chary8b857352007-01-26 14:04:58 +0100166
Corentin Charye539c2f2007-05-06 14:47:06 +0200167/* GPS */
168/* R2H use different handle for GPS on/off */
Corentin Charyd99b5772010-01-22 21:20:57 +0100169#define METHOD_GPS_ON "SDON"
170#define METHOD_GPS_OFF "SDOF"
171#define METHOD_GPS_STATUS "GPST"
Corentin Charye539c2f2007-05-06 14:47:06 +0200172
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000173/* Keyboard light */
Corentin Charyd99b5772010-01-22 21:20:57 +0100174#define METHOD_KBD_LIGHT_SET "SLKB"
175#define METHOD_KBD_LIGHT_GET "GLKB"
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000176
Corentin Chary85091b72007-01-26 14:04:30 +0100177/*
Corentin Chary9129d142009-12-01 22:39:41 +0100178 * Define a specific led structure to keep the main structure clean
179 */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100180struct asus_led {
181 int wk;
182 struct work_struct work;
183 struct led_classdev led;
184 struct asus_laptop *asus;
185 const char *method;
Corentin Chary9129d142009-12-01 22:39:41 +0100186};
187
188/*
Corentin Chary85091b72007-01-26 14:04:30 +0100189 * This is the main structure, we can use it to store anything interesting
190 * about the hotk device
191 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100192struct asus_laptop {
Corentin Charybe966662009-08-29 10:28:29 +0200193 char *name; /* laptop name */
Corentin Chary600ad522009-11-30 21:42:42 +0100194
Corentin Chary7c247642009-11-30 22:13:54 +0100195 struct acpi_table_header *dsdt_info;
Corentin Chary600ad522009-11-30 21:42:42 +0100196 struct platform_device *platform_device;
Corentin Chary7c247642009-11-30 22:13:54 +0100197 struct acpi_device *device; /* the device we are in */
198 struct backlight_device *backlight_device;
Corentin Chary9129d142009-12-01 22:39:41 +0100199
Corentin Chary7c247642009-11-30 22:13:54 +0100200 struct input_dev *inputdev;
Corentin Chary9129d142009-12-01 22:39:41 +0100201 struct key_entry *keymap;
202
Corentin Charyaee0afb2010-01-26 21:01:34 +0100203 struct asus_led mled;
204 struct asus_led tled;
205 struct asus_led rled;
206 struct asus_led pled;
207 struct asus_led gled;
208 struct asus_led kled;
209 struct workqueue_struct *led_workqueue;
Corentin Chary7c247642009-11-30 22:13:54 +0100210
Corentin Charyaa9df932010-01-13 21:49:10 +0100211 int wireless_status;
212 bool have_rsts;
213
Corentin Chary18e13112010-01-25 23:29:24 +0100214 struct rfkill *gps_rfkill;
215
Corentin Charybe966662009-08-29 10:28:29 +0200216 acpi_handle handle; /* the handle of the hotk device */
Corentin Charybe966662009-08-29 10:28:29 +0200217 u32 ledd_status; /* status of the LED display */
218 u8 light_level; /* light sensor level */
219 u8 light_switch; /* light sensor switch value */
220 u16 event_count[128]; /* count for each event TODO make this better */
Corentin Chary85091b72007-01-26 14:04:30 +0100221};
222
Corentin Chary9129d142009-12-01 22:39:41 +0100223static const struct key_entry asus_keymap[] = {
Corentin Charya539df52010-01-25 22:53:21 +0100224 /* Lenovo SL Specific keycodes */
Corentin Chary66a71dd2010-01-25 22:50:11 +0100225 {KE_KEY, 0x02, { KEY_SCREENLOCK } },
226 {KE_KEY, 0x05, { KEY_WLAN } },
227 {KE_KEY, 0x08, { KEY_F13 } },
228 {KE_KEY, 0x17, { KEY_ZOOM } },
229 {KE_KEY, 0x1f, { KEY_BATTERY } },
Corentin Charya539df52010-01-25 22:53:21 +0100230 /* End of Lenovo SL Specific keycodes */
Corentin Chary66a71dd2010-01-25 22:50:11 +0100231 {KE_KEY, 0x30, { KEY_VOLUMEUP } },
232 {KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
233 {KE_KEY, 0x32, { KEY_MUTE } },
234 {KE_KEY, 0x33, { KEY_SWITCHVIDEOMODE } },
235 {KE_KEY, 0x34, { KEY_SWITCHVIDEOMODE } },
236 {KE_KEY, 0x40, { KEY_PREVIOUSSONG } },
237 {KE_KEY, 0x41, { KEY_NEXTSONG } },
238 {KE_KEY, 0x43, { KEY_STOPCD } },
239 {KE_KEY, 0x45, { KEY_PLAYPAUSE } },
240 {KE_KEY, 0x4c, { KEY_MEDIA } },
241 {KE_KEY, 0x50, { KEY_EMAIL } },
242 {KE_KEY, 0x51, { KEY_WWW } },
243 {KE_KEY, 0x55, { KEY_CALC } },
244 {KE_KEY, 0x5C, { KEY_SCREENLOCK } }, /* Screenlock */
245 {KE_KEY, 0x5D, { KEY_WLAN } },
246 {KE_KEY, 0x5E, { KEY_WLAN } },
247 {KE_KEY, 0x5F, { KEY_WLAN } },
248 {KE_KEY, 0x60, { KEY_SWITCHVIDEOMODE } },
249 {KE_KEY, 0x61, { KEY_SWITCHVIDEOMODE } },
250 {KE_KEY, 0x62, { KEY_SWITCHVIDEOMODE } },
251 {KE_KEY, 0x63, { KEY_SWITCHVIDEOMODE } },
252 {KE_KEY, 0x6B, { KEY_F13 } }, /* Lock Touchpad */
Corentin Chary7f607d72010-01-17 17:37:19 +0100253 {KE_KEY, 0x7E, { KEY_BLUETOOTH } },
254 {KE_KEY, 0x7D, { KEY_BLUETOOTH } },
Corentin Chary66a71dd2010-01-25 22:50:11 +0100255 {KE_KEY, 0x82, { KEY_CAMERA } },
256 {KE_KEY, 0x88, { KEY_WLAN } },
257 {KE_KEY, 0x8A, { KEY_PROG1 } },
258 {KE_KEY, 0x95, { KEY_MEDIA } },
259 {KE_KEY, 0x99, { KEY_PHONE } },
260 {KE_KEY, 0xc4, { KEY_KBDILLUMUP } },
261 {KE_KEY, 0xc5, { KEY_KBDILLUMDOWN } },
Corentin Charyb58baecd2010-08-24 09:30:45 +0200262 {KE_KEY, 0xb5, { KEY_CALC } },
Corentin Chary034ce902009-01-20 16:17:43 +0100263 {KE_END, 0},
264};
265
Corentin Chary66a71dd2010-01-25 22:50:11 +0100266
Corentin Chary85091b72007-01-26 14:04:30 +0100267/*
268 * This function evaluates an ACPI method, given an int as parameter, the
269 * method is searched within the scope of the handle, can be NULL. The output
270 * of the method is written is output, which can also be NULL
271 *
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100272 * returns 0 if write is successful, -1 else.
Corentin Chary85091b72007-01-26 14:04:30 +0100273 */
Corentin Charyd8c67322009-11-28 10:27:51 +0100274static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
275 struct acpi_buffer *output)
Corentin Chary85091b72007-01-26 14:04:30 +0100276{
Corentin Charybe966662009-08-29 10:28:29 +0200277 struct acpi_object_list params; /* list of input parameters (an int) */
278 union acpi_object in_obj; /* the only param we use */
Corentin Chary85091b72007-01-26 14:04:30 +0100279 acpi_status status;
280
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100281 if (!handle)
Axel Lin9fb866f2010-07-20 15:19:47 -0700282 return -1;
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100283
Corentin Chary85091b72007-01-26 14:04:30 +0100284 params.count = 1;
285 params.pointer = &in_obj;
286 in_obj.type = ACPI_TYPE_INTEGER;
287 in_obj.integer.value = val;
288
289 status = acpi_evaluate_object(handle, (char *)method, &params, output);
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100290 if (status == AE_OK)
291 return 0;
292 else
293 return -1;
Corentin Chary85091b72007-01-26 14:04:30 +0100294}
295
Corentin Charyd8c67322009-11-28 10:27:51 +0100296static int write_acpi_int(acpi_handle handle, const char *method, int val)
297{
298 return write_acpi_int_ret(handle, method, val, NULL);
299}
300
Corentin Charyd99b5772010-01-22 21:20:57 +0100301static int acpi_check_handle(acpi_handle handle, const char *method,
302 acpi_handle *ret)
303{
304 acpi_status status;
305
306 if (method == NULL)
307 return -ENODEV;
308
309 if (ret)
310 status = acpi_get_handle(handle, (char *)method,
311 ret);
312 else {
313 acpi_handle dummy;
314
315 status = acpi_get_handle(handle, (char *)method,
316 &dummy);
317 }
318
319 if (status != AE_OK) {
320 if (ret)
321 pr_warning("Error finding %s\n", method);
322 return -ENODEV;
323 }
324 return 0;
325}
326
Corentin Chary17e78f62010-01-13 22:21:33 +0100327/* Generic LED function */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100328static int asus_led_set(struct asus_laptop *asus, const char *method,
Corentin Chary17e78f62010-01-13 22:21:33 +0100329 int value)
Corentin Charybe18cda2007-01-26 14:04:35 +0100330{
Corentin Charyd99b5772010-01-22 21:20:57 +0100331 if (!strcmp(method, METHOD_MLED))
Corentin Chary17e78f62010-01-13 22:21:33 +0100332 value = !value;
Corentin Charyd99b5772010-01-22 21:20:57 +0100333 else if (!strcmp(method, METHOD_GLED))
Corentin Chary17e78f62010-01-13 22:21:33 +0100334 value = !value + 1;
335 else
336 value = !!value;
Corentin Charybe18cda2007-01-26 14:04:35 +0100337
Corentin Charye5593bf2010-01-17 17:20:11 +0100338 return write_acpi_int(asus->handle, method, value);
Corentin Charybe18cda2007-01-26 14:04:35 +0100339}
340
Corentin Charybe4ee822009-12-06 16:27:09 +0100341/*
342 * LEDs
343 */
Corentin Charybe18cda2007-01-26 14:04:35 +0100344/* /sys/class/led handlers */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100345static void asus_led_cdev_set(struct led_classdev *led_cdev,
346 enum led_brightness value)
347{
348 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
349 struct asus_laptop *asus = led->asus;
Corentin Charybe18cda2007-01-26 14:04:35 +0100350
Corentin Charyaee0afb2010-01-26 21:01:34 +0100351 led->wk = !!value;
352 queue_work(asus->led_workqueue, &led->work);
353}
354
355static void asus_led_cdev_update(struct work_struct *work)
356{
357 struct asus_led *led = container_of(work, struct asus_led, work);
358 struct asus_laptop *asus = led->asus;
359
360 asus_led_set(asus, led->method, led->wk);
361}
362
363static enum led_brightness asus_led_cdev_get(struct led_classdev *led_cdev)
364{
365 return led_cdev->brightness;
366}
Corentin Charybe18cda2007-01-26 14:04:35 +0100367
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000368/*
Corentin Charybe4ee822009-12-06 16:27:09 +0100369 * Keyboard backlight (also a LED)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000370 */
Corentin Charyd99b5772010-01-22 21:20:57 +0100371static int asus_kled_lvl(struct asus_laptop *asus)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000372{
373 unsigned long long kblv;
374 struct acpi_object_list params;
375 union acpi_object in_obj;
376 acpi_status rv;
377
378 params.count = 1;
379 params.pointer = &in_obj;
380 in_obj.type = ACPI_TYPE_INTEGER;
381 in_obj.integer.value = 2;
382
Corentin Charyd99b5772010-01-22 21:20:57 +0100383 rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
384 &params, &kblv);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000385 if (ACPI_FAILURE(rv)) {
386 pr_warning("Error reading kled level\n");
Corentin Chary4d441512010-01-13 22:26:24 +0100387 return -ENODEV;
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000388 }
389 return kblv;
390}
391
Corentin Chary4d441512010-01-13 22:26:24 +0100392static int asus_kled_set(struct asus_laptop *asus, int kblv)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000393{
394 if (kblv > 0)
395 kblv = (1 << 7) | (kblv & 0x7F);
396 else
397 kblv = 0;
398
Corentin Charyd99b5772010-01-22 21:20:57 +0100399 if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000400 pr_warning("Keyboard LED display write failed\n");
401 return -EINVAL;
402 }
403 return 0;
404}
405
Corentin Charyaee0afb2010-01-26 21:01:34 +0100406static void asus_kled_cdev_set(struct led_classdev *led_cdev,
407 enum led_brightness value)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000408{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100409 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
410 struct asus_laptop *asus = led->asus;
Corentin Chary9129d142009-12-01 22:39:41 +0100411
Corentin Chary060cbce2010-01-28 10:52:40 +0100412 led->wk = value;
Corentin Charyaee0afb2010-01-26 21:01:34 +0100413 queue_work(asus->led_workqueue, &led->work);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000414}
415
Corentin Charyaee0afb2010-01-26 21:01:34 +0100416static void asus_kled_cdev_update(struct work_struct *work)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000417{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100418 struct asus_led *led = container_of(work, struct asus_led, work);
419 struct asus_laptop *asus = led->asus;
Corentin Chary9129d142009-12-01 22:39:41 +0100420
Corentin Charyaee0afb2010-01-26 21:01:34 +0100421 asus_kled_set(asus, led->wk);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000422}
423
Corentin Charyaee0afb2010-01-26 21:01:34 +0100424static enum led_brightness asus_kled_cdev_get(struct led_classdev *led_cdev)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000425{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100426 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
427 struct asus_laptop *asus = led->asus;
Corentin Charyd99b5772010-01-22 21:20:57 +0100428
429 return asus_kled_lvl(asus);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000430}
431
Corentin Charybe4ee822009-12-06 16:27:09 +0100432static void asus_led_exit(struct asus_laptop *asus)
433{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100434 if (asus->mled.led.dev)
435 led_classdev_unregister(&asus->mled.led);
436 if (asus->tled.led.dev)
437 led_classdev_unregister(&asus->tled.led);
438 if (asus->pled.led.dev)
439 led_classdev_unregister(&asus->pled.led);
440 if (asus->rled.led.dev)
441 led_classdev_unregister(&asus->rled.led);
442 if (asus->gled.led.dev)
443 led_classdev_unregister(&asus->gled.led);
444 if (asus->kled.led.dev)
445 led_classdev_unregister(&asus->kled.led);
446 if (asus->led_workqueue) {
447 destroy_workqueue(asus->led_workqueue);
448 asus->led_workqueue = NULL;
Corentin Charybe4ee822009-12-06 16:27:09 +0100449 }
450}
451
452/* Ugly macro, need to fix that later */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100453static int asus_led_register(struct asus_laptop *asus,
454 struct asus_led *led,
455 const char *name, const char *method)
456{
457 struct led_classdev *led_cdev = &led->led;
458
459 if (!method || acpi_check_handle(asus->handle, method, NULL))
Corentin Chary060cbce2010-01-28 10:52:40 +0100460 return 0; /* Led not present */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100461
462 led->asus = asus;
463 led->method = method;
464
465 INIT_WORK(&led->work, asus_led_cdev_update);
466 led_cdev->name = name;
467 led_cdev->brightness_set = asus_led_cdev_set;
468 led_cdev->brightness_get = asus_led_cdev_get;
469 led_cdev->max_brightness = 1;
470 return led_classdev_register(&asus->platform_device->dev, led_cdev);
471}
Corentin Charybe4ee822009-12-06 16:27:09 +0100472
473static int asus_led_init(struct asus_laptop *asus)
474{
Corentin Charyaee0afb2010-01-26 21:01:34 +0100475 int r;
Corentin Charybe4ee822009-12-06 16:27:09 +0100476
477 /*
478 * Functions that actually update the LED's are called from a
479 * workqueue. By doing this as separate work rather than when the LED
480 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
481 * potentially bad time, such as a timer interrupt.
482 */
Corentin Charyaee0afb2010-01-26 21:01:34 +0100483 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
484 if (!asus->led_workqueue)
Corentin Charybe4ee822009-12-06 16:27:09 +0100485 return -ENOMEM;
486
Corentin Charyaee0afb2010-01-26 21:01:34 +0100487 r = asus_led_register(asus, &asus->mled, "asus::mail", METHOD_MLED);
488 if (r)
489 goto error;
490 r = asus_led_register(asus, &asus->tled, "asus::touchpad", METHOD_TLED);
491 if (r)
492 goto error;
493 r = asus_led_register(asus, &asus->rled, "asus::record", METHOD_RLED);
494 if (r)
495 goto error;
496 r = asus_led_register(asus, &asus->pled, "asus::phone", METHOD_PLED);
497 if (r)
498 goto error;
499 r = asus_led_register(asus, &asus->gled, "asus::gaming", METHOD_GLED);
500 if (r)
501 goto error;
Corentin Charyd99b5772010-01-22 21:20:57 +0100502 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
Corentin Charyaee0afb2010-01-26 21:01:34 +0100503 !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL)) {
504 struct asus_led *led = &asus->kled;
505 struct led_classdev *cdev = &led->led;
506
507 led->asus = asus;
508
509 INIT_WORK(&led->work, asus_kled_cdev_update);
510 cdev->name = "asus::kbd_backlight";
511 cdev->brightness_set = asus_kled_cdev_set;
512 cdev->brightness_get = asus_kled_cdev_get;
513 cdev->max_brightness = 3;
514 r = led_classdev_register(&asus->platform_device->dev, cdev);
515 }
Corentin Charybe4ee822009-12-06 16:27:09 +0100516error:
Corentin Charyaee0afb2010-01-26 21:01:34 +0100517 if (r)
Corentin Charybe4ee822009-12-06 16:27:09 +0100518 asus_led_exit(asus);
Corentin Charyaee0afb2010-01-26 21:01:34 +0100519 return r;
Corentin Charybe4ee822009-12-06 16:27:09 +0100520}
521
522/*
523 * Backlight device
524 */
Corentin Chary4d441512010-01-13 22:26:24 +0100525static int asus_read_brightness(struct backlight_device *bd)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100526{
Corentin Charyd99b5772010-01-22 21:20:57 +0100527 struct asus_laptop *asus = bl_get_data(bd);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400528 unsigned long long value;
Corentin Chary9a816852007-03-11 10:25:38 +0100529 acpi_status rv = AE_OK;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100530
Corentin Charyd99b5772010-01-22 21:20:57 +0100531 rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
532 NULL, &value);
Corentin Chary9a816852007-03-11 10:25:38 +0100533 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200534 pr_warning("Error reading brightness\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100535
536 return value;
537}
538
Corentin Chary4d441512010-01-13 22:26:24 +0100539static int asus_set_brightness(struct backlight_device *bd, int value)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100540{
Corentin Charyd99b5772010-01-22 21:20:57 +0100541 struct asus_laptop *asus = bl_get_data(bd);
542
543 if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200544 pr_warning("Error changing brightness\n");
Corentin Charye5b50f62009-11-28 10:19:55 +0100545 return -EIO;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100546 }
Corentin Charye5b50f62009-11-28 10:19:55 +0100547 return 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100548}
549
550static int update_bl_status(struct backlight_device *bd)
551{
Richard Purdie599a52d2007-02-10 23:07:48 +0000552 int value = bd->props.brightness;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100553
Corentin Chary3b81cf92011-02-06 13:27:31 +0100554 return asus_set_brightness(bd, value);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100555}
556
Lionel Debrouxacc24722010-11-16 14:14:02 +0100557static const struct backlight_ops asusbl_ops = {
Corentin Chary4d441512010-01-13 22:26:24 +0100558 .get_brightness = asus_read_brightness,
Corentin Charybe4ee822009-12-06 16:27:09 +0100559 .update_status = update_bl_status,
560};
561
Corentin Charya539df52010-01-25 22:53:21 +0100562static int asus_backlight_notify(struct asus_laptop *asus)
563{
564 struct backlight_device *bd = asus->backlight_device;
565 int old = bd->props.brightness;
566
567 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
568
569 return old;
570}
571
Corentin Charybe4ee822009-12-06 16:27:09 +0100572static int asus_backlight_init(struct asus_laptop *asus)
573{
574 struct backlight_device *bd;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500575 struct backlight_properties props;
Corentin Charybe4ee822009-12-06 16:27:09 +0100576
Corentin Chary71e687d2010-08-24 09:30:44 +0200577 if (acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) ||
Corentin Chary3b81cf92011-02-06 13:27:31 +0100578 acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL))
Corentin Chary71e687d2010-08-24 09:30:44 +0200579 return 0;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500580
Corentin Chary71e687d2010-08-24 09:30:44 +0200581 memset(&props, 0, sizeof(struct backlight_properties));
582 props.max_brightness = 15;
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700583 props.type = BACKLIGHT_PLATFORM;
Corentin Charybe4ee822009-12-06 16:27:09 +0100584
Corentin Chary71e687d2010-08-24 09:30:44 +0200585 bd = backlight_device_register(ASUS_LAPTOP_FILE,
586 &asus->platform_device->dev, asus,
587 &asusbl_ops, &props);
588 if (IS_ERR(bd)) {
589 pr_err("Could not register asus backlight device\n");
590 asus->backlight_device = NULL;
591 return PTR_ERR(bd);
Corentin Charybe4ee822009-12-06 16:27:09 +0100592 }
Corentin Chary71e687d2010-08-24 09:30:44 +0200593
594 asus->backlight_device = bd;
595 bd->props.brightness = asus_read_brightness(bd);
596 bd->props.power = FB_BLANK_UNBLANK;
597 backlight_update_status(bd);
Corentin Charybe4ee822009-12-06 16:27:09 +0100598 return 0;
599}
600
601static void asus_backlight_exit(struct asus_laptop *asus)
602{
603 if (asus->backlight_device)
604 backlight_device_unregister(asus->backlight_device);
Corentin Charya539df52010-01-25 22:53:21 +0100605 asus->backlight_device = NULL;
Corentin Charybe4ee822009-12-06 16:27:09 +0100606}
607
Corentin Chary85091b72007-01-26 14:04:30 +0100608/*
609 * Platform device handlers
610 */
611
612/*
613 * We write our info in page, we begin at offset off and cannot write more
614 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
615 * number of bytes written in page
616 */
617static ssize_t show_infos(struct device *dev,
Len Brown8def05f2007-01-30 01:46:43 -0500618 struct device_attribute *attr, char *page)
Corentin Chary85091b72007-01-26 14:04:30 +0100619{
Corentin Chary9129d142009-12-01 22:39:41 +0100620 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary85091b72007-01-26 14:04:30 +0100621 int len = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400622 unsigned long long temp;
Corentin Charybe966662009-08-29 10:28:29 +0200623 char buf[16]; /* enough for all info */
Corentin Chary9a816852007-03-11 10:25:38 +0100624 acpi_status rv = AE_OK;
625
Corentin Chary85091b72007-01-26 14:04:30 +0100626 /*
Corentin Chary060cbce2010-01-28 10:52:40 +0100627 * We use the easy way, we don't care of off and count,
628 * so we don't set eof to 1
Corentin Chary85091b72007-01-26 14:04:30 +0100629 */
630
Corentin Chary50a90c42009-11-30 21:55:12 +0100631 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
632 len += sprintf(page + len, "Model reference : %s\n", asus->name);
Corentin Chary85091b72007-01-26 14:04:30 +0100633 /*
634 * The SFUN method probably allows the original driver to get the list
635 * of features supported by a given model. For now, 0x0100 or 0x0800
636 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
637 * The significance of others is yet to be found.
638 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100639 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100640 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000641 len += sprintf(page + len, "SFUN value : %#x\n",
642 (uint) temp);
643 /*
644 * The HWRS method return informations about the hardware.
645 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
646 * The significance of others is yet to be found.
647 * If we don't find the method, we assume the device are present.
648 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100649 rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
Corentin Chary1d4a3802009-08-28 12:56:46 +0000650 if (!ACPI_FAILURE(rv))
651 len += sprintf(page + len, "HRWS value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100652 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100653 /*
654 * Another value for userspace: the ASYM method returns 0x02 for
655 * battery low and 0x04 for battery critical, its readings tend to be
656 * more accurate than those provided by _BST.
657 * Note: since not all the laptops provide this method, errors are
658 * silently ignored.
659 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100660 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100661 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000662 len += sprintf(page + len, "ASYM value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100663 (uint) temp);
Corentin Chary7c247642009-11-30 22:13:54 +0100664 if (asus->dsdt_info) {
665 snprintf(buf, 16, "%d", asus->dsdt_info->length);
Corentin Chary85091b72007-01-26 14:04:30 +0100666 len += sprintf(page + len, "DSDT length : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100667 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
Corentin Chary85091b72007-01-26 14:04:30 +0100668 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100669 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100670 len += sprintf(page + len, "DSDT revision : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100671 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100672 len += sprintf(page + len, "OEM id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100673 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100674 len += sprintf(page + len, "OEM table id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100675 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100676 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100677 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100678 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100679 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100680 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
681 }
682
683 return len;
684}
685
686static int parse_arg(const char *buf, unsigned long count, int *val)
687{
688 if (!count)
689 return 0;
690 if (count > 31)
691 return -EINVAL;
692 if (sscanf(buf, "%i", val) != 1)
693 return -EINVAL;
694 return count;
695}
696
Corentin Chary17e78f62010-01-13 22:21:33 +0100697static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
698 const char *buf, size_t count,
Corentin Charyd99b5772010-01-22 21:20:57 +0100699 const char *method)
Corentin Chary4564de12007-01-26 14:04:40 +0100700{
701 int rv, value;
702 int out = 0;
703
704 rv = parse_arg(buf, count, &value);
705 if (rv > 0)
706 out = value ? 1 : 0;
707
Corentin Charyd99b5772010-01-22 21:20:57 +0100708 if (write_acpi_int(asus->handle, method, value))
Corentin Chary17e78f62010-01-13 22:21:33 +0100709 return -ENODEV;
Corentin Chary4564de12007-01-26 14:04:40 +0100710 return rv;
711}
712
713/*
Corentin Chary722ad972007-01-26 14:04:55 +0100714 * LEDD display
715 */
716static ssize_t show_ledd(struct device *dev,
717 struct device_attribute *attr, char *buf)
718{
Corentin Chary9129d142009-12-01 22:39:41 +0100719 struct asus_laptop *asus = dev_get_drvdata(dev);
720
Corentin Chary50a90c42009-11-30 21:55:12 +0100721 return sprintf(buf, "0x%08x\n", asus->ledd_status);
Corentin Chary722ad972007-01-26 14:04:55 +0100722}
723
724static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
725 const char *buf, size_t count)
726{
Corentin Chary9129d142009-12-01 22:39:41 +0100727 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary722ad972007-01-26 14:04:55 +0100728 int rv, value;
729
730 rv = parse_arg(buf, count, &value);
731 if (rv > 0) {
Axel Lin6a984a02010-07-20 15:19:48 -0700732 if (write_acpi_int(asus->handle, METHOD_LEDD, value)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200733 pr_warning("LED display write failed\n");
Axel Lin6a984a02010-07-20 15:19:48 -0700734 return -ENODEV;
735 }
736 asus->ledd_status = (u32) value;
Corentin Chary722ad972007-01-26 14:04:55 +0100737 }
738 return rv;
739}
740
741/*
Corentin Charyaa9df932010-01-13 21:49:10 +0100742 * Wireless
743 */
744static int asus_wireless_status(struct asus_laptop *asus, int mask)
745{
746 unsigned long long status;
747 acpi_status rv = AE_OK;
748
749 if (!asus->have_rsts)
750 return (asus->wireless_status & mask) ? 1 : 0;
751
Corentin Charyd99b5772010-01-22 21:20:57 +0100752 rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
753 NULL, &status);
Corentin Charyaa9df932010-01-13 21:49:10 +0100754 if (ACPI_FAILURE(rv)) {
755 pr_warning("Error reading Wireless status\n");
756 return -EINVAL;
757 }
758 return !!(status & mask);
759}
760
761/*
Corentin Chary4564de12007-01-26 14:04:40 +0100762 * WLAN
763 */
Corentin Charye5593bf2010-01-17 17:20:11 +0100764static int asus_wlan_set(struct asus_laptop *asus, int status)
765{
766 if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
767 pr_warning("Error setting wlan status to %d", status);
768 return -EIO;
769 }
770 return 0;
771}
772
Corentin Chary4564de12007-01-26 14:04:40 +0100773static ssize_t show_wlan(struct device *dev,
774 struct device_attribute *attr, char *buf)
775{
Corentin Chary9129d142009-12-01 22:39:41 +0100776 struct asus_laptop *asus = dev_get_drvdata(dev);
777
Corentin Chary17e78f62010-01-13 22:21:33 +0100778 return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100779}
780
781static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
782 const char *buf, size_t count)
783{
Corentin Chary9129d142009-12-01 22:39:41 +0100784 struct asus_laptop *asus = dev_get_drvdata(dev);
785
Corentin Charyd99b5772010-01-22 21:20:57 +0100786 return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
Corentin Chary4564de12007-01-26 14:04:40 +0100787}
788
789/*
790 * Bluetooth
791 */
Corentin Charye5593bf2010-01-17 17:20:11 +0100792static int asus_bluetooth_set(struct asus_laptop *asus, int status)
793{
794 if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
795 pr_warning("Error setting bluetooth status to %d", status);
796 return -EIO;
797 }
798 return 0;
799}
800
Corentin Chary4564de12007-01-26 14:04:40 +0100801static ssize_t show_bluetooth(struct device *dev,
802 struct device_attribute *attr, char *buf)
803{
Corentin Chary9129d142009-12-01 22:39:41 +0100804 struct asus_laptop *asus = dev_get_drvdata(dev);
805
Corentin Chary17e78f62010-01-13 22:21:33 +0100806 return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100807}
808
Len Brown8def05f2007-01-30 01:46:43 -0500809static ssize_t store_bluetooth(struct device *dev,
810 struct device_attribute *attr, const char *buf,
811 size_t count)
Corentin Chary4564de12007-01-26 14:04:40 +0100812{
Corentin Chary9129d142009-12-01 22:39:41 +0100813 struct asus_laptop *asus = dev_get_drvdata(dev);
814
Corentin Charyd99b5772010-01-22 21:20:57 +0100815 return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
Corentin Chary4564de12007-01-26 14:04:40 +0100816}
817
Corentin Chary78127b42007-01-26 14:04:49 +0100818/*
Corentin Charyba1ff5b2010-11-14 17:40:12 +0100819 * Wimax
820 */
821static int asus_wimax_set(struct asus_laptop *asus, int status)
822{
823 if (write_acpi_int(asus->handle, METHOD_WIMAX, !!status)) {
824 pr_warning("Error setting wimax status to %d", status);
825 return -EIO;
826 }
827 return 0;
828}
829
830static ssize_t show_wimax(struct device *dev,
831 struct device_attribute *attr, char *buf)
832{
833 struct asus_laptop *asus = dev_get_drvdata(dev);
834
835 return sprintf(buf, "%d\n", asus_wireless_status(asus, WM_RSTS));
836}
837
838static ssize_t store_wimax(struct device *dev,
839 struct device_attribute *attr, const char *buf,
840 size_t count)
841{
842 struct asus_laptop *asus = dev_get_drvdata(dev);
843
844 return sysfs_acpi_set(asus, buf, count, METHOD_WIMAX);
845}
846
847/*
848 * Wwan
849 */
850static int asus_wwan_set(struct asus_laptop *asus, int status)
851{
852 if (write_acpi_int(asus->handle, METHOD_WWAN, !!status)) {
853 pr_warning("Error setting wwan status to %d", status);
854 return -EIO;
855 }
856 return 0;
857}
858
859static ssize_t show_wwan(struct device *dev,
860 struct device_attribute *attr, char *buf)
861{
862 struct asus_laptop *asus = dev_get_drvdata(dev);
863
864 return sprintf(buf, "%d\n", asus_wireless_status(asus, WW_RSTS));
865}
866
867static ssize_t store_wwan(struct device *dev,
868 struct device_attribute *attr, const char *buf,
869 size_t count)
870{
871 struct asus_laptop *asus = dev_get_drvdata(dev);
872
873 return sysfs_acpi_set(asus, buf, count, METHOD_WWAN);
874}
875
876/*
Corentin Chary78127b42007-01-26 14:04:49 +0100877 * Display
878 */
Corentin Chary4d441512010-01-13 22:26:24 +0100879static void asus_set_display(struct asus_laptop *asus, int value)
Corentin Chary78127b42007-01-26 14:04:49 +0100880{
881 /* no sanity check needed for now */
Corentin Charyd99b5772010-01-22 21:20:57 +0100882 if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200883 pr_warning("Error setting display\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100884 return;
885}
886
Corentin Chary78127b42007-01-26 14:04:49 +0100887/*
888 * Experimental support for display switching. As of now: 1 should activate
889 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
890 * Any combination (bitwise) of these will suffice. I never actually tested 4
891 * displays hooked up simultaneously, so be warned. See the acpi4asus README
892 * for more info.
893 */
894static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
895 const char *buf, size_t count)
896{
Corentin Chary9129d142009-12-01 22:39:41 +0100897 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary78127b42007-01-26 14:04:49 +0100898 int rv, value;
899
900 rv = parse_arg(buf, count, &value);
901 if (rv > 0)
Corentin Chary4d441512010-01-13 22:26:24 +0100902 asus_set_display(asus, value);
Corentin Chary78127b42007-01-26 14:04:49 +0100903 return rv;
904}
905
Corentin Chary8b857352007-01-26 14:04:58 +0100906/*
907 * Light Sens
908 */
Corentin Chary4d441512010-01-13 22:26:24 +0100909static void asus_als_switch(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100910{
Corentin Charyd99b5772010-01-22 21:20:57 +0100911 if (write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200912 pr_warning("Error setting light sensor switch\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100913 asus->light_switch = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100914}
915
916static ssize_t show_lssw(struct device *dev,
917 struct device_attribute *attr, char *buf)
918{
Corentin Chary9129d142009-12-01 22:39:41 +0100919 struct asus_laptop *asus = dev_get_drvdata(dev);
920
Corentin Chary50a90c42009-11-30 21:55:12 +0100921 return sprintf(buf, "%d\n", asus->light_switch);
Corentin Chary8b857352007-01-26 14:04:58 +0100922}
923
924static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
925 const char *buf, size_t count)
926{
Corentin Chary9129d142009-12-01 22:39:41 +0100927 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100928 int rv, value;
929
930 rv = parse_arg(buf, count, &value);
931 if (rv > 0)
Corentin Chary4d441512010-01-13 22:26:24 +0100932 asus_als_switch(asus, value ? 1 : 0);
Corentin Chary8b857352007-01-26 14:04:58 +0100933
934 return rv;
935}
936
Corentin Chary4d441512010-01-13 22:26:24 +0100937static void asus_als_level(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100938{
Corentin Charyd99b5772010-01-22 21:20:57 +0100939 if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200940 pr_warning("Error setting light sensor level\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100941 asus->light_level = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100942}
943
944static ssize_t show_lslvl(struct device *dev,
945 struct device_attribute *attr, char *buf)
946{
Corentin Chary9129d142009-12-01 22:39:41 +0100947 struct asus_laptop *asus = dev_get_drvdata(dev);
948
Corentin Chary50a90c42009-11-30 21:55:12 +0100949 return sprintf(buf, "%d\n", asus->light_level);
Corentin Chary8b857352007-01-26 14:04:58 +0100950}
951
952static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
953 const char *buf, size_t count)
954{
Corentin Chary9129d142009-12-01 22:39:41 +0100955 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100956 int rv, value;
957
958 rv = parse_arg(buf, count, &value);
959 if (rv > 0) {
960 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
961 /* 0 <= value <= 15 */
Corentin Chary4d441512010-01-13 22:26:24 +0100962 asus_als_level(asus, value);
Corentin Chary8b857352007-01-26 14:04:58 +0100963 }
964
965 return rv;
966}
967
Corentin Charye539c2f2007-05-06 14:47:06 +0200968/*
969 * GPS
970 */
Corentin Chary6358bf22010-01-13 21:55:44 +0100971static int asus_gps_status(struct asus_laptop *asus)
972{
973 unsigned long long status;
974 acpi_status rv = AE_OK;
975
Corentin Charyd99b5772010-01-22 21:20:57 +0100976 rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
977 NULL, &status);
Corentin Chary6358bf22010-01-13 21:55:44 +0100978 if (ACPI_FAILURE(rv)) {
979 pr_warning("Error reading GPS status\n");
980 return -ENODEV;
981 }
982 return !!status;
983}
984
985static int asus_gps_switch(struct asus_laptop *asus, int status)
986{
Corentin Charyd99b5772010-01-22 21:20:57 +0100987 const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
Corentin Chary6358bf22010-01-13 21:55:44 +0100988
Corentin Charyd99b5772010-01-22 21:20:57 +0100989 if (write_acpi_int(asus->handle, meth, 0x02))
Corentin Chary6358bf22010-01-13 21:55:44 +0100990 return -ENODEV;
991 return 0;
992}
993
Corentin Charye539c2f2007-05-06 14:47:06 +0200994static ssize_t show_gps(struct device *dev,
995 struct device_attribute *attr, char *buf)
996{
Corentin Chary9129d142009-12-01 22:39:41 +0100997 struct asus_laptop *asus = dev_get_drvdata(dev);
998
Corentin Chary6358bf22010-01-13 21:55:44 +0100999 return sprintf(buf, "%d\n", asus_gps_status(asus));
Corentin Charye539c2f2007-05-06 14:47:06 +02001000}
1001
1002static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
1003 const char *buf, size_t count)
1004{
Corentin Chary060cbce2010-01-28 10:52:40 +01001005 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary6358bf22010-01-13 21:55:44 +01001006 int rv, value;
1007 int ret;
Corentin Chary9129d142009-12-01 22:39:41 +01001008
Corentin Chary6358bf22010-01-13 21:55:44 +01001009 rv = parse_arg(buf, count, &value);
1010 if (rv <= 0)
1011 return -EINVAL;
1012 ret = asus_gps_switch(asus, !!value);
1013 if (ret)
1014 return ret;
Corentin Chary18e13112010-01-25 23:29:24 +01001015 rfkill_set_sw_state(asus->gps_rfkill, !value);
Corentin Chary6358bf22010-01-13 21:55:44 +01001016 return rv;
Corentin Charye539c2f2007-05-06 14:47:06 +02001017}
1018
Corentin Chary034ce902009-01-20 16:17:43 +01001019/*
Corentin Chary18e13112010-01-25 23:29:24 +01001020 * rfkill
1021 */
1022static int asus_gps_rfkill_set(void *data, bool blocked)
1023{
Corentin Chary23f45c32010-08-24 09:30:46 +02001024 struct asus_laptop *asus = data;
Corentin Chary18e13112010-01-25 23:29:24 +01001025
Corentin Chary23f45c32010-08-24 09:30:46 +02001026 return asus_gps_switch(asus, !blocked);
Corentin Chary18e13112010-01-25 23:29:24 +01001027}
1028
1029static const struct rfkill_ops asus_gps_rfkill_ops = {
1030 .set_block = asus_gps_rfkill_set,
1031};
1032
1033static void asus_rfkill_exit(struct asus_laptop *asus)
1034{
1035 if (asus->gps_rfkill) {
1036 rfkill_unregister(asus->gps_rfkill);
1037 rfkill_destroy(asus->gps_rfkill);
1038 asus->gps_rfkill = NULL;
1039 }
1040}
1041
1042static int asus_rfkill_init(struct asus_laptop *asus)
1043{
1044 int result;
1045
1046 if (acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) ||
1047 acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) ||
1048 acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
1049 return 0;
1050
1051 asus->gps_rfkill = rfkill_alloc("asus-gps", &asus->platform_device->dev,
1052 RFKILL_TYPE_GPS,
Corentin Chary23f45c32010-08-24 09:30:46 +02001053 &asus_gps_rfkill_ops, asus);
Corentin Chary18e13112010-01-25 23:29:24 +01001054 if (!asus->gps_rfkill)
1055 return -EINVAL;
1056
1057 result = rfkill_register(asus->gps_rfkill);
1058 if (result) {
1059 rfkill_destroy(asus->gps_rfkill);
1060 asus->gps_rfkill = NULL;
1061 }
1062
1063 return result;
1064}
1065
1066/*
Corentin Charybe4ee822009-12-06 16:27:09 +01001067 * Input device (i.e. hotkeys)
Corentin Chary034ce902009-01-20 16:17:43 +01001068 */
Corentin Charybe4ee822009-12-06 16:27:09 +01001069static void asus_input_notify(struct asus_laptop *asus, int event)
1070{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001071 if (asus->inputdev)
1072 sparse_keymap_report_event(asus->inputdev, event, 1, true);
Corentin Charybe4ee822009-12-06 16:27:09 +01001073}
1074
1075static int asus_input_init(struct asus_laptop *asus)
1076{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001077 struct input_dev *input;
1078 int error;
Corentin Charybe4ee822009-12-06 16:27:09 +01001079
Corentin Chary66a71dd2010-01-25 22:50:11 +01001080 input = input_allocate_device();
1081 if (!input) {
Corentin Charybe4ee822009-12-06 16:27:09 +01001082 pr_info("Unable to allocate input device\n");
Axel Lin45036ae2010-07-05 15:29:00 +08001083 return -ENOMEM;
Corentin Charybe4ee822009-12-06 16:27:09 +01001084 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001085 input->name = "Asus Laptop extra buttons";
1086 input->phys = ASUS_LAPTOP_FILE "/input0";
1087 input->id.bustype = BUS_HOST;
1088 input->dev.parent = &asus->platform_device->dev;
Corentin Charybe4ee822009-12-06 16:27:09 +01001089
Corentin Chary66a71dd2010-01-25 22:50:11 +01001090 error = sparse_keymap_setup(input, asus_keymap, NULL);
1091 if (error) {
1092 pr_err("Unable to setup input device keymap\n");
Axel Lin45036ae2010-07-05 15:29:00 +08001093 goto err_free_dev;
Corentin Charybe4ee822009-12-06 16:27:09 +01001094 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001095 error = input_register_device(input);
1096 if (error) {
Corentin Charybe4ee822009-12-06 16:27:09 +01001097 pr_info("Unable to register input device\n");
Axel Lin45036ae2010-07-05 15:29:00 +08001098 goto err_free_keymap;
Corentin Charybe4ee822009-12-06 16:27:09 +01001099 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001100
1101 asus->inputdev = input;
1102 return 0;
1103
Axel Lin45036ae2010-07-05 15:29:00 +08001104err_free_keymap:
Corentin Chary66a71dd2010-01-25 22:50:11 +01001105 sparse_keymap_free(input);
Axel Lin45036ae2010-07-05 15:29:00 +08001106err_free_dev:
Corentin Chary66a71dd2010-01-25 22:50:11 +01001107 input_free_device(input);
1108 return error;
Corentin Charybe4ee822009-12-06 16:27:09 +01001109}
1110
1111static void asus_input_exit(struct asus_laptop *asus)
1112{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001113 if (asus->inputdev) {
1114 sparse_keymap_free(asus->inputdev);
Corentin Charybe4ee822009-12-06 16:27:09 +01001115 input_unregister_device(asus->inputdev);
Corentin Chary66a71dd2010-01-25 22:50:11 +01001116 }
Corentin Chary71e687d2010-08-24 09:30:44 +02001117 asus->inputdev = NULL;
Corentin Charybe4ee822009-12-06 16:27:09 +01001118}
1119
1120/*
1121 * ACPI driver
1122 */
Corentin Chary600ad522009-11-30 21:42:42 +01001123static void asus_acpi_notify(struct acpi_device *device, u32 event)
Corentin Chary85091b72007-01-26 14:04:30 +01001124{
Corentin Chary9129d142009-12-01 22:39:41 +01001125 struct asus_laptop *asus = acpi_driver_data(device);
Corentin Chary6050c8d2009-02-15 19:30:19 +01001126 u16 count;
Corentin Chary034ce902009-01-20 16:17:43 +01001127
Corentin Chary619d8b12009-11-28 10:35:37 +01001128 /* TODO Find a better way to handle events count. */
Corentin Chary50a90c42009-11-30 21:55:12 +01001129 count = asus->event_count[event % 128]++;
1130 acpi_bus_generate_proc_event(asus->device, event, count);
1131 acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
1132 dev_name(&asus->device->dev), event,
Corentin Chary6050c8d2009-02-15 19:30:19 +01001133 count);
Corentin Chary85091b72007-01-26 14:04:30 +01001134
Corentin Charya539df52010-01-25 22:53:21 +01001135 /* Brightness events are special */
1136 if (event >= ATKD_BR_MIN && event <= ATKD_BR_MAX) {
1137
1138 /* Ignore them completely if the acpi video driver is used */
1139 if (asus->backlight_device != NULL) {
1140 /* Update the backlight device. */
1141 asus_backlight_notify(asus);
1142 }
1143 return ;
1144 }
Corentin Charybe4ee822009-12-06 16:27:09 +01001145 asus_input_notify(asus, event);
Corentin Chary85091b72007-01-26 14:04:30 +01001146}
1147
Corentin Chary2a1fd642010-01-26 21:02:23 +01001148static DEVICE_ATTR(infos, S_IRUGO, show_infos, NULL);
1149static DEVICE_ATTR(wlan, S_IRUGO | S_IWUSR, show_wlan, store_wlan);
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001150static DEVICE_ATTR(bluetooth, S_IRUGO | S_IWUSR,
1151 show_bluetooth, store_bluetooth);
Corentin Charyba1ff5b2010-11-14 17:40:12 +01001152static DEVICE_ATTR(wimax, S_IRUGO | S_IWUSR, show_wimax, store_wimax);
1153static DEVICE_ATTR(wwan, S_IRUGO | S_IWUSR, show_wwan, store_wwan);
Corentin Chary3b81cf92011-02-06 13:27:31 +01001154static DEVICE_ATTR(display, S_IWUSR, NULL, store_disp);
Corentin Chary2a1fd642010-01-26 21:02:23 +01001155static DEVICE_ATTR(ledd, S_IRUGO | S_IWUSR, show_ledd, store_ledd);
1156static DEVICE_ATTR(ls_level, S_IRUGO | S_IWUSR, show_lslvl, store_lslvl);
1157static DEVICE_ATTR(ls_switch, S_IRUGO | S_IWUSR, show_lssw, store_lssw);
1158static DEVICE_ATTR(gps, S_IRUGO | S_IWUSR, show_gps, store_gps);
1159
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001160static struct attribute *asus_attributes[] = {
1161 &dev_attr_infos.attr,
1162 &dev_attr_wlan.attr,
1163 &dev_attr_bluetooth.attr,
Corentin Charyba1ff5b2010-11-14 17:40:12 +01001164 &dev_attr_wimax.attr,
1165 &dev_attr_wwan.attr,
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001166 &dev_attr_display.attr,
1167 &dev_attr_ledd.attr,
1168 &dev_attr_ls_level.attr,
1169 &dev_attr_ls_switch.attr,
1170 &dev_attr_gps.attr,
1171 NULL
1172};
Corentin Chary2a1fd642010-01-26 21:02:23 +01001173
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001174static mode_t asus_sysfs_is_visible(struct kobject *kobj,
1175 struct attribute *attr,
1176 int idx)
1177{
1178 struct device *dev = container_of(kobj, struct device, kobj);
1179 struct platform_device *pdev = to_platform_device(dev);
1180 struct asus_laptop *asus = platform_get_drvdata(pdev);
1181 acpi_handle handle = asus->handle;
1182 bool supported;
1183
1184 if (attr == &dev_attr_wlan.attr) {
1185 supported = !acpi_check_handle(handle, METHOD_WLAN, NULL);
1186
1187 } else if (attr == &dev_attr_bluetooth.attr) {
1188 supported = !acpi_check_handle(handle, METHOD_BLUETOOTH, NULL);
1189
1190 } else if (attr == &dev_attr_display.attr) {
1191 supported = !acpi_check_handle(handle, METHOD_SWITCH_DISPLAY, NULL);
1192
Corentin Charyba1ff5b2010-11-14 17:40:12 +01001193 } else if (attr == &dev_attr_wimax.attr) {
1194 supported =
1195 !acpi_check_handle(asus->handle, METHOD_WIMAX, NULL);
1196
1197 } else if (attr == &dev_attr_wwan.attr) {
1198 supported = !acpi_check_handle(asus->handle, METHOD_WWAN, NULL);
1199
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001200 } else if (attr == &dev_attr_ledd.attr) {
1201 supported = !acpi_check_handle(handle, METHOD_LEDD, NULL);
1202
1203 } else if (attr == &dev_attr_ls_switch.attr ||
1204 attr == &dev_attr_ls_level.attr) {
1205 supported = !acpi_check_handle(handle, METHOD_ALS_CONTROL, NULL) &&
1206 !acpi_check_handle(handle, METHOD_ALS_LEVEL, NULL);
1207
1208 } else if (attr == &dev_attr_gps.attr) {
1209 supported = !acpi_check_handle(handle, METHOD_GPS_ON, NULL) &&
1210 !acpi_check_handle(handle, METHOD_GPS_OFF, NULL) &&
1211 !acpi_check_handle(handle, METHOD_GPS_STATUS, NULL);
1212 } else {
1213 supported = true;
1214 }
1215
1216 return supported ? attr->mode : 0;
Corentin Chary2a1fd642010-01-26 21:02:23 +01001217}
1218
Corentin Chary2a1fd642010-01-26 21:02:23 +01001219
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001220static const struct attribute_group asus_attr_group = {
1221 .is_visible = asus_sysfs_is_visible,
1222 .attrs = asus_attributes,
1223};
Corentin Chary85091b72007-01-26 14:04:30 +01001224
Corentin Chary9129d142009-12-01 22:39:41 +01001225static int asus_platform_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001226{
Corentin Chary71e687d2010-08-24 09:30:44 +02001227 int result;
Corentin Chary600ad522009-11-30 21:42:42 +01001228
Corentin Chary50a90c42009-11-30 21:55:12 +01001229 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1230 if (!asus->platform_device)
Corentin Chary600ad522009-11-30 21:42:42 +01001231 return -ENOMEM;
Corentin Chary9129d142009-12-01 22:39:41 +01001232 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001233
Corentin Chary71e687d2010-08-24 09:30:44 +02001234 result = platform_device_add(asus->platform_device);
1235 if (result)
Corentin Chary600ad522009-11-30 21:42:42 +01001236 goto fail_platform_device;
1237
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001238 result = sysfs_create_group(&asus->platform_device->dev.kobj,
1239 &asus_attr_group);
Corentin Chary71e687d2010-08-24 09:30:44 +02001240 if (result)
Corentin Chary600ad522009-11-30 21:42:42 +01001241 goto fail_sysfs;
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001242
Corentin Chary600ad522009-11-30 21:42:42 +01001243 return 0;
1244
1245fail_sysfs:
Corentin Chary50a90c42009-11-30 21:55:12 +01001246 platform_device_del(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001247fail_platform_device:
Corentin Chary50a90c42009-11-30 21:55:12 +01001248 platform_device_put(asus->platform_device);
Corentin Chary71e687d2010-08-24 09:30:44 +02001249 return result;
Corentin Chary600ad522009-11-30 21:42:42 +01001250}
1251
Corentin Chary9129d142009-12-01 22:39:41 +01001252static void asus_platform_exit(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001253{
Dmitry Torokhovac9b1e52010-08-26 00:12:19 -07001254 sysfs_remove_group(&asus->platform_device->dev.kobj, &asus_attr_group);
Corentin Chary50a90c42009-11-30 21:55:12 +01001255 platform_device_unregister(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001256}
1257
1258static struct platform_driver platform_driver = {
Len Brown8def05f2007-01-30 01:46:43 -05001259 .driver = {
Corentin Chary9129d142009-12-01 22:39:41 +01001260 .name = ASUS_LAPTOP_FILE,
1261 .owner = THIS_MODULE,
1262 }
Corentin Chary85091b72007-01-26 14:04:30 +01001263};
1264
Corentin Chary85091b72007-01-26 14:04:30 +01001265/*
Corentin Chary50a90c42009-11-30 21:55:12 +01001266 * This function is used to initialize the context with right values. In this
1267 * method, we can make all the detection we want, and modify the asus_laptop
1268 * struct
Corentin Chary85091b72007-01-26 14:04:30 +01001269 */
Corentin Chary7c247642009-11-30 22:13:54 +01001270static int asus_laptop_get_info(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001271{
1272 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Corentin Chary85091b72007-01-26 14:04:30 +01001273 union acpi_object *model = NULL;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001274 unsigned long long bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +01001275 char *string = NULL;
1276 acpi_status status;
1277
1278 /*
1279 * Get DSDT headers early enough to allow for differentiating between
1280 * models, but late enough to allow acpi_bus_register_driver() to fail
1281 * before doing anything ACPI-specific. Should we encounter a machine,
1282 * which needs special handling (i.e. its hotkey device has a different
Corentin Chary7c247642009-11-30 22:13:54 +01001283 * HID), this bit will be moved.
Corentin Chary85091b72007-01-26 14:04:30 +01001284 */
Corentin Chary7c247642009-11-30 22:13:54 +01001285 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
Corentin Chary85091b72007-01-26 14:04:30 +01001286 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001287 pr_warning("Couldn't get the DSDT table header\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001288
1289 /* We have to write 0 on init this far for all ASUS models */
Corentin Chary50a90c42009-11-30 21:55:12 +01001290 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001291 pr_err("Hotkey initialization failed\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001292 return -ENODEV;
1293 }
1294
1295 /* This needs to be called for some laptops to init properly */
Corentin Chary9a816852007-03-11 10:25:38 +01001296 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001297 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
Corentin Chary9a816852007-03-11 10:25:38 +01001298 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001299 pr_warning("Error calling BSTS\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001300 else if (bsts_result)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001301 pr_notice("BSTS called, 0x%02x returned\n",
Corentin Chary9a816852007-03-11 10:25:38 +01001302 (uint) bsts_result);
Corentin Chary85091b72007-01-26 14:04:30 +01001303
Corentin Chary185e5af2007-03-11 10:27:33 +01001304 /* This too ... */
Corentin Charye5593bf2010-01-17 17:20:11 +01001305 if (write_acpi_int(asus->handle, "CWAP", wapf))
1306 pr_err("Error calling CWAP(%d)\n", wapf);
Corentin Chary85091b72007-01-26 14:04:30 +01001307 /*
1308 * Try to match the object returned by INIT to the specific model.
1309 * Handle every possible object (or the lack of thereof) the DSDT
1310 * writers might throw at us. When in trouble, we pass NULL to
1311 * asus_model_match() and try something completely different.
1312 */
1313 if (buffer.pointer) {
1314 model = buffer.pointer;
1315 switch (model->type) {
1316 case ACPI_TYPE_STRING:
1317 string = model->string.pointer;
1318 break;
1319 case ACPI_TYPE_BUFFER:
1320 string = model->buffer.pointer;
1321 break;
1322 default:
1323 string = "";
1324 break;
1325 }
1326 }
Corentin Chary50a90c42009-11-30 21:55:12 +01001327 asus->name = kstrdup(string, GFP_KERNEL);
Axel Lind8eca112010-06-29 11:09:47 +08001328 if (!asus->name) {
1329 kfree(buffer.pointer);
Corentin Chary85091b72007-01-26 14:04:30 +01001330 return -ENOMEM;
Axel Lind8eca112010-06-29 11:09:47 +08001331 }
Corentin Chary85091b72007-01-26 14:04:30 +01001332
Len Brown8def05f2007-01-30 01:46:43 -05001333 if (*string)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001334 pr_notice(" %s model detected\n", string);
Corentin Chary85091b72007-01-26 14:04:30 +01001335
Corentin Chary4564de12007-01-26 14:04:40 +01001336 /*
1337 * The HWRS method return informations about the hardware.
Corentin Charyba1ff5b2010-11-14 17:40:12 +01001338 * 0x80 bit is for WLAN, 0x100 for Bluetooth,
1339 * 0x40 for WWAN, 0x10 for WIMAX.
Corentin Chary4564de12007-01-26 14:04:40 +01001340 * The significance of others is yet to be found.
Corentin Chary4564de12007-01-26 14:04:40 +01001341 */
Corentin Chary9a816852007-03-11 10:25:38 +01001342 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001343 acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
Corentin Charyd99b5772010-01-22 21:20:57 +01001344 if (!ACPI_FAILURE(status))
1345 pr_notice(" HRWS returned %x", (int)hwrs_result);
Corentin Chary4564de12007-01-26 14:04:40 +01001346
Corentin Charyd99b5772010-01-22 21:20:57 +01001347 if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
Corentin Charyaa9df932010-01-13 21:49:10 +01001348 asus->have_rsts = true;
Corentin Chary4564de12007-01-26 14:04:40 +01001349
Corentin Chary85091b72007-01-26 14:04:30 +01001350 kfree(model);
1351
1352 return AE_OK;
1353}
1354
Corentin Chary9129d142009-12-01 22:39:41 +01001355static int __devinit asus_acpi_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001356{
1357 int result = 0;
1358
Corentin Chary50a90c42009-11-30 21:55:12 +01001359 result = acpi_bus_get_status(asus->device);
Corentin Chary600ad522009-11-30 21:42:42 +01001360 if (result)
1361 return result;
Corentin Chary50a90c42009-11-30 21:55:12 +01001362 if (!asus->device->status.present) {
Corentin Chary600ad522009-11-30 21:42:42 +01001363 pr_err("Hotkey device not present, aborting\n");
1364 return -ENODEV;
1365 }
1366
Corentin Chary7c247642009-11-30 22:13:54 +01001367 result = asus_laptop_get_info(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001368 if (result)
1369 return result;
1370
Corentin Chary600ad522009-11-30 21:42:42 +01001371 /* WLED and BLED are on by default */
Corentin Charybe4ee822009-12-06 16:27:09 +01001372 if (bluetooth_status >= 0)
Corentin Charye5593bf2010-01-17 17:20:11 +01001373 asus_bluetooth_set(asus, !!bluetooth_status);
1374
Corentin Charyd0930a22010-01-17 17:21:13 +01001375 if (wlan_status >= 0)
1376 asus_wlan_set(asus, !!wlan_status);
Corentin Chary600ad522009-11-30 21:42:42 +01001377
Corentin Charyba1ff5b2010-11-14 17:40:12 +01001378 if (wimax_status >= 0)
1379 asus_wimax_set(asus, !!wimax_status);
1380
1381 if (wwan_status >= 0)
1382 asus_wwan_set(asus, !!wwan_status);
1383
Corentin Chary600ad522009-11-30 21:42:42 +01001384 /* Keyboard Backlight is on by default */
Corentin Charyd99b5772010-01-22 21:20:57 +01001385 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
Corentin Chary4d441512010-01-13 22:26:24 +01001386 asus_kled_set(asus, 1);
Corentin Chary600ad522009-11-30 21:42:42 +01001387
1388 /* LED display is off by default */
Corentin Chary50a90c42009-11-30 21:55:12 +01001389 asus->ledd_status = 0xFFF;
Corentin Chary600ad522009-11-30 21:42:42 +01001390
1391 /* Set initial values of light sensor and level */
Corentin Charybe4ee822009-12-06 16:27:09 +01001392 asus->light_switch = 0; /* Default to light sensor disabled */
1393 asus->light_level = 5; /* level 5 for sensor sensitivity */
Corentin Chary600ad522009-11-30 21:42:42 +01001394
Corentin Charyd99b5772010-01-22 21:20:57 +01001395 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1396 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
Corentin Chary4d441512010-01-13 22:26:24 +01001397 asus_als_switch(asus, asus->light_switch);
Corentin Chary4d441512010-01-13 22:26:24 +01001398 asus_als_level(asus, asus->light_level);
Corentin Charyd99b5772010-01-22 21:20:57 +01001399 }
Corentin Chary600ad522009-11-30 21:42:42 +01001400
Corentin Chary600ad522009-11-30 21:42:42 +01001401 return result;
1402}
1403
Corentin Charyaf96f872011-02-06 13:27:30 +01001404static void __devinit asus_dmi_check(void)
1405{
1406 const char *model;
1407
1408 model = dmi_get_system_info(DMI_PRODUCT_NAME);
1409 if (!model)
1410 return;
1411
1412 /* On L1400B WLED control the sound card, don't mess with it ... */
1413 if (strncmp(model, "L1400B", 6) == 0) {
1414 wlan_status = -1;
1415 }
1416}
1417
Corentin Chary71e687d2010-08-24 09:30:44 +02001418static bool asus_device_present;
1419
Corentin Chary600ad522009-11-30 21:42:42 +01001420static int __devinit asus_acpi_add(struct acpi_device *device)
1421{
Corentin Chary9129d142009-12-01 22:39:41 +01001422 struct asus_laptop *asus;
Corentin Chary600ad522009-11-30 21:42:42 +01001423 int result;
1424
1425 pr_notice("Asus Laptop Support version %s\n",
1426 ASUS_LAPTOP_VERSION);
Corentin Chary50a90c42009-11-30 21:55:12 +01001427 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1428 if (!asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001429 return -ENOMEM;
Corentin Chary50a90c42009-11-30 21:55:12 +01001430 asus->handle = device->handle;
1431 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1432 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1433 device->driver_data = asus;
1434 asus->device = device;
Corentin Chary600ad522009-11-30 21:42:42 +01001435
Corentin Charyaf96f872011-02-06 13:27:30 +01001436 asus_dmi_check();
1437
Corentin Chary9129d142009-12-01 22:39:41 +01001438 result = asus_acpi_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001439 if (result)
1440 goto fail_platform;
1441
1442 /*
1443 * Register the platform device first. It is used as a parent for the
1444 * sub-devices below.
1445 */
Corentin Chary9129d142009-12-01 22:39:41 +01001446 result = asus_platform_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001447 if (result)
1448 goto fail_platform;
1449
1450 if (!acpi_video_backlight_support()) {
Corentin Chary9129d142009-12-01 22:39:41 +01001451 result = asus_backlight_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001452 if (result)
1453 goto fail_backlight;
1454 } else
1455 pr_info("Backlight controlled by ACPI video driver\n");
1456
Corentin Chary9129d142009-12-01 22:39:41 +01001457 result = asus_input_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001458 if (result)
1459 goto fail_input;
1460
Corentin Chary9129d142009-12-01 22:39:41 +01001461 result = asus_led_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001462 if (result)
1463 goto fail_led;
1464
Corentin Chary18e13112010-01-25 23:29:24 +01001465 result = asus_rfkill_init(asus);
1466 if (result)
1467 goto fail_rfkill;
1468
Corentin Chary600ad522009-11-30 21:42:42 +01001469 asus_device_present = true;
1470 return 0;
1471
Corentin Chary18e13112010-01-25 23:29:24 +01001472fail_rfkill:
1473 asus_led_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001474fail_led:
Corentin Chary9129d142009-12-01 22:39:41 +01001475 asus_input_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001476fail_input:
Corentin Chary9129d142009-12-01 22:39:41 +01001477 asus_backlight_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001478fail_backlight:
Corentin Chary9129d142009-12-01 22:39:41 +01001479 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001480fail_platform:
Corentin Chary50a90c42009-11-30 21:55:12 +01001481 kfree(asus->name);
1482 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001483
1484 return result;
1485}
1486
1487static int asus_acpi_remove(struct acpi_device *device, int type)
1488{
Corentin Chary9129d142009-12-01 22:39:41 +01001489 struct asus_laptop *asus = acpi_driver_data(device);
1490
1491 asus_backlight_exit(asus);
Corentin Chary18e13112010-01-25 23:29:24 +01001492 asus_rfkill_exit(asus);
Corentin Chary9129d142009-12-01 22:39:41 +01001493 asus_led_exit(asus);
1494 asus_input_exit(asus);
1495 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001496
Corentin Chary50a90c42009-11-30 21:55:12 +01001497 kfree(asus->name);
1498 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001499 return 0;
1500}
1501
1502static const struct acpi_device_id asus_device_ids[] = {
1503 {"ATK0100", 0},
1504 {"ATK0101", 0},
1505 {"", 0},
1506};
1507MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1508
1509static struct acpi_driver asus_acpi_driver = {
Corentin Chary50a90c42009-11-30 21:55:12 +01001510 .name = ASUS_LAPTOP_NAME,
1511 .class = ASUS_LAPTOP_CLASS,
Corentin Chary600ad522009-11-30 21:42:42 +01001512 .owner = THIS_MODULE,
1513 .ids = asus_device_ids,
1514 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1515 .ops = {
1516 .add = asus_acpi_add,
1517 .remove = asus_acpi_remove,
1518 .notify = asus_acpi_notify,
1519 },
1520};
1521
Corentin Chary85091b72007-01-26 14:04:30 +01001522static int __init asus_laptop_init(void)
1523{
1524 int result;
1525
Corentin Chary600ad522009-11-30 21:42:42 +01001526 result = platform_driver_register(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001527 if (result < 0)
1528 return result;
1529
Corentin Chary600ad522009-11-30 21:42:42 +01001530 result = acpi_bus_register_driver(&asus_acpi_driver);
1531 if (result < 0)
1532 goto fail_acpi_driver;
1533 if (!asus_device_present) {
1534 result = -ENODEV;
1535 goto fail_no_device;
Corentin Chary85091b72007-01-26 14:04:30 +01001536 }
Len Brown8def05f2007-01-30 01:46:43 -05001537 return 0;
Corentin Chary85091b72007-01-26 14:04:30 +01001538
Corentin Chary600ad522009-11-30 21:42:42 +01001539fail_no_device:
1540 acpi_bus_unregister_driver(&asus_acpi_driver);
1541fail_acpi_driver:
1542 platform_driver_unregister(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001543 return result;
1544}
1545
Corentin Chary600ad522009-11-30 21:42:42 +01001546static void __exit asus_laptop_exit(void)
1547{
1548 acpi_bus_unregister_driver(&asus_acpi_driver);
1549 platform_driver_unregister(&platform_driver);
1550}
1551
Corentin Chary85091b72007-01-26 14:04:30 +01001552module_init(asus_laptop_init);
1553module_exit(asus_laptop_exit);