blob: 8042568528e5aca4aaaddc25a26894d8d15c6b2b [file] [log] [blame]
Corentin Chary85091b72007-01-26 14:04:30 +01001/*
2 * asus-laptop.c - Asus Laptop Support
3 *
4 *
5 * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
Corentin Chary8ec555c2007-03-11 10:28:03 +01006 * Copyright (C) 2006-2007 Corentin Chary
Corentin Chary85091b72007-01-26 14:04:30 +01007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 *
23 * The development page for this driver is located at
24 * http://sourceforge.net/projects/acpi4asus/
25 *
26 * Credits:
27 * Pontus Fuchs - Helper functions, cleanup
28 * Johann Wiesner - Small compile fixes
29 * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
30 * Eric Burghard - LED display support for W1N
31 * Josh Green - Light Sens support
32 * Thomas Tuttle - His first patch for led support was very helpfull
Corentin Charye539c2f2007-05-06 14:47:06 +020033 * Sam Lin - GPS support
Corentin Chary85091b72007-01-26 14:04:30 +010034 */
35
Corentin Chary2fcc23d2009-06-19 14:52:03 +020036#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37
Corentin Chary85091b72007-01-26 14:04:30 +010038#include <linux/kernel.h>
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/types.h>
42#include <linux/err.h>
43#include <linux/proc_fs.h>
Corentin Chary6b7091e2007-01-26 14:04:45 +010044#include <linux/backlight.h>
45#include <linux/fb.h>
Corentin Charybe18cda2007-01-26 14:04:35 +010046#include <linux/leds.h>
Corentin Chary85091b72007-01-26 14:04:30 +010047#include <linux/platform_device.h>
48#include <acpi/acpi_drivers.h>
49#include <acpi/acpi_bus.h>
50#include <asm/uaccess.h>
Corentin Chary034ce902009-01-20 16:17:43 +010051#include <linux/input.h>
Corentin Chary66a71dd2010-01-25 22:50:11 +010052#include <linux/input/sparse-keymap.h>
Corentin Chary85091b72007-01-26 14:04:30 +010053
Corentin Chary91687cc2009-11-28 10:32:34 +010054#define ASUS_LAPTOP_VERSION "0.42"
Corentin Chary85091b72007-01-26 14:04:30 +010055
Corentin Chary50a90c42009-11-30 21:55:12 +010056#define ASUS_LAPTOP_NAME "Asus Laptop Support"
57#define ASUS_LAPTOP_CLASS "hotkey"
58#define ASUS_LAPTOP_DEVICE_NAME "Hotkey"
59#define ASUS_LAPTOP_FILE KBUILD_MODNAME
60#define ASUS_LAPTOP_PREFIX "\\_SB.ATKD."
Corentin Chary85091b72007-01-26 14:04:30 +010061
Corentin Chary85091b72007-01-26 14:04:30 +010062MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
Corentin Chary50a90c42009-11-30 21:55:12 +010063MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
Corentin Chary85091b72007-01-26 14:04:30 +010064MODULE_LICENSE("GPL");
65
Corentin Charybe966662009-08-29 10:28:29 +020066/*
67 * WAPF defines the behavior of the Fn+Fx wlan key
Corentin Chary185e5af2007-03-11 10:27:33 +010068 * The significance of values is yet to be found, but
69 * most of the time:
70 * 0x0 will do nothing
71 * 0x1 will allow to control the device with Fn+Fx key.
72 * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
73 * 0x5 like 0x1 or 0x4
74 * So, if something doesn't work as you want, just try other values =)
75 */
76static uint wapf = 1;
77module_param(wapf, uint, 0644);
78MODULE_PARM_DESC(wapf, "WAPF value");
79
Corentin Charyd0930a22010-01-17 17:21:13 +010080static uint wlan_status = 1;
Corentin Chary0e875f42010-01-10 20:49:26 +010081static uint bluetooth_status = 1;
82
Corentin Charyd0930a22010-01-17 17:21:13 +010083module_param(wlan_status, uint, 0644);
84MODULE_PARM_DESC(wlan_status, "Set the wireless status on boot "
Corentin Chary0e875f42010-01-10 20:49:26 +010085 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
86 "default is 1");
87
88module_param(bluetooth_status, uint, 0644);
89MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
90 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
91 "default is 1");
92
Corentin Charybe4ee822009-12-06 16:27:09 +010093/*
94 * Some events we use, same for all Asus
95 */
96#define ATKD_BR_UP 0x10
97#define ATKD_BR_DOWN 0x20
98#define ATKD_LCD_ON 0x33
99#define ATKD_LCD_OFF 0x34
100
101/*
102 * Known bits returned by \_SB.ATKD.HWRS
103 */
104#define WL_HWRS 0x80
105#define BT_HWRS 0x100
106
107/*
108 * Flags for hotk status
109 * WL_ON and BT_ON are also used for wireless_status()
110 */
Corentin Chary17e78f62010-01-13 22:21:33 +0100111#define WL_RSTS 0x01 /* internal Wifi */
112#define BT_RSTS 0x02 /* internal Bluetooth */
Corentin Charybe4ee822009-12-06 16:27:09 +0100113
Corentin Chary85091b72007-01-26 14:04:30 +0100114#define ASUS_HANDLE(object, paths...) \
115 static acpi_handle object##_handle = NULL; \
116 static char *object##_paths[] = { paths }
117
Corentin Charybe18cda2007-01-26 14:04:35 +0100118/* LED */
Corentin Charyd99b5772010-01-22 21:20:57 +0100119#define METHOD_MLED "MLED"
120#define METHOD_TLED "TLED"
121#define METHOD_RLED "RLED" /* W1JC */
122#define METHOD_PLED "PLED" /* A7J */
123#define METHOD_GLED "GLED" /* G1, G2 (probably) */
Corentin Charybe18cda2007-01-26 14:04:35 +0100124
Corentin Chary722ad972007-01-26 14:04:55 +0100125/* LEDD */
Corentin Charyd99b5772010-01-22 21:20:57 +0100126#define METHOD_LEDD "SLCM"
Corentin Chary722ad972007-01-26 14:04:55 +0100127
Corentin Charybe966662009-08-29 10:28:29 +0200128/*
129 * Bluetooth and WLAN
Corentin Chary4564de12007-01-26 14:04:40 +0100130 * WLED and BLED are not handled like other XLED, because in some dsdt
131 * they also control the WLAN/Bluetooth device.
132 */
Corentin Charyd99b5772010-01-22 21:20:57 +0100133#define METHOD_WLAN "WLED"
134#define METHOD_BLUETOOTH "BLED"
135#define METHOD_WL_STATUS "RSTS"
Corentin Chary4564de12007-01-26 14:04:40 +0100136
Corentin Chary6b7091e2007-01-26 14:04:45 +0100137/* Brightness */
Corentin Charyd99b5772010-01-22 21:20:57 +0100138#define METHOD_BRIGHTNESS_SET "SPLV"
139#define METHOD_BRIGHTNESS_GET "GPLV"
Corentin Chary6b7091e2007-01-26 14:04:45 +0100140
141/* Backlight */
Len Brown8def05f2007-01-30 01:46:43 -0500142ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
143 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
144 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
145 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
146 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
Torsten Krah4d0b856e2008-10-17 09:47:57 +0200147 "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
Len Brown8def05f2007-01-30 01:46:43 -0500148 "\\_SB.PCI0.PX40.Q10", /* S1x */
149 "\\Q10"); /* A2x, L2D, L3D, M2E */
Corentin Chary6b7091e2007-01-26 14:04:45 +0100150
Corentin Chary78127b42007-01-26 14:04:49 +0100151/* Display */
Corentin Charyd99b5772010-01-22 21:20:57 +0100152#define METHOD_SWITCH_DISPLAY "SDSP"
Corentin Charybe966662009-08-29 10:28:29 +0200153ASUS_HANDLE(display_get,
154 /* A6B, A6K A6R A7D F3JM L4R M6R A3G M6A M6V VX-1 V6J V6V W3Z */
155 "\\_SB.PCI0.P0P1.VGA.GETD",
156 /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V S5A M5A z33A W1Jc W2V G1 */
157 "\\_SB.PCI0.P0P2.VGA.GETD",
158 /* A6V A6Q */
159 "\\_SB.PCI0.P0P3.VGA.GETD",
160 /* A6T, A6M */
161 "\\_SB.PCI0.P0PA.VGA.GETD",
162 /* L3C */
163 "\\_SB.PCI0.PCI1.VGAC.NMAP",
164 /* Z96F */
165 "\\_SB.PCI0.VGA.GETD",
166 /* A2D */
167 "\\ACTD",
168 /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
169 "\\ADVG",
170 /* P30 */
171 "\\DNXT",
172 /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
173 "\\INFB",
174 /* A3F A6F A3N A3L M6N W3N W6A */
175 "\\SSTE");
Corentin Chary78127b42007-01-26 14:04:49 +0100176
Corentin Charyd99b5772010-01-22 21:20:57 +0100177#define METHOD_ALS_CONTROL "ALSC" /* Z71A Z71V */
178#define METHOD_ALS_LEVEL "ALSL" /* Z71A Z71V */
Corentin Chary8b857352007-01-26 14:04:58 +0100179
Corentin Charye539c2f2007-05-06 14:47:06 +0200180/* GPS */
181/* R2H use different handle for GPS on/off */
Corentin Charyd99b5772010-01-22 21:20:57 +0100182#define METHOD_GPS_ON "SDON"
183#define METHOD_GPS_OFF "SDOF"
184#define METHOD_GPS_STATUS "GPST"
Corentin Charye539c2f2007-05-06 14:47:06 +0200185
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000186/* Keyboard light */
Corentin Charyd99b5772010-01-22 21:20:57 +0100187#define METHOD_KBD_LIGHT_SET "SLKB"
188#define METHOD_KBD_LIGHT_GET "GLKB"
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000189
Corentin Chary85091b72007-01-26 14:04:30 +0100190/*
Corentin Chary9129d142009-12-01 22:39:41 +0100191 * Define a specific led structure to keep the main structure clean
192 */
193#define ASUS_DEFINE_LED(object) \
194 int object##_wk; \
195 struct work_struct object##_work; \
196 struct led_classdev object;
197
198
199#define led_to_asus(led_cdev, led) \
200 container_of(container_of(led_cdev, struct asus_laptop_leds, \
201 led), \
202 struct asus_laptop, leds)
203#define work_to_asus(work, led) \
204 container_of(container_of(work, struct asus_laptop_leds, \
205 led##_work), \
206 struct asus_laptop, leds)
207
208struct asus_laptop_leds {
209 ASUS_DEFINE_LED(mled)
210 ASUS_DEFINE_LED(tled)
211 ASUS_DEFINE_LED(rled)
212 ASUS_DEFINE_LED(pled)
213 ASUS_DEFINE_LED(gled)
214 ASUS_DEFINE_LED(kled)
215 struct workqueue_struct *workqueue;
216};
217
218/*
Corentin Chary85091b72007-01-26 14:04:30 +0100219 * This is the main structure, we can use it to store anything interesting
220 * about the hotk device
221 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100222struct asus_laptop {
Corentin Charybe966662009-08-29 10:28:29 +0200223 char *name; /* laptop name */
Corentin Chary600ad522009-11-30 21:42:42 +0100224
Corentin Chary7c247642009-11-30 22:13:54 +0100225 struct acpi_table_header *dsdt_info;
Corentin Chary600ad522009-11-30 21:42:42 +0100226 struct platform_device *platform_device;
Corentin Chary7c247642009-11-30 22:13:54 +0100227 struct acpi_device *device; /* the device we are in */
228 struct backlight_device *backlight_device;
Corentin Chary9129d142009-12-01 22:39:41 +0100229
Corentin Chary7c247642009-11-30 22:13:54 +0100230 struct input_dev *inputdev;
Corentin Chary9129d142009-12-01 22:39:41 +0100231 struct key_entry *keymap;
232
233 struct asus_laptop_leds leds;
Corentin Chary7c247642009-11-30 22:13:54 +0100234
Corentin Charyaa9df932010-01-13 21:49:10 +0100235 int wireless_status;
236 bool have_rsts;
Corentin Chary3e68ae72010-01-13 22:10:39 +0100237 int lcd_state;
Corentin Charyaa9df932010-01-13 21:49:10 +0100238
Corentin Charybe966662009-08-29 10:28:29 +0200239 acpi_handle handle; /* the handle of the hotk device */
Corentin Charybe966662009-08-29 10:28:29 +0200240 u32 ledd_status; /* status of the LED display */
241 u8 light_level; /* light sensor level */
242 u8 light_switch; /* light sensor switch value */
243 u16 event_count[128]; /* count for each event TODO make this better */
Corentin Chary034ce902009-01-20 16:17:43 +0100244 u16 *keycode_map;
Corentin Chary85091b72007-01-26 14:04:30 +0100245};
246
Corentin Chary9129d142009-12-01 22:39:41 +0100247static const struct key_entry asus_keymap[] = {
Corentin Chary66a71dd2010-01-25 22:50:11 +0100248 {KE_KEY, 0x02, { KEY_SCREENLOCK } },
249 {KE_KEY, 0x05, { KEY_WLAN } },
250 {KE_KEY, 0x08, { KEY_F13 } },
251 {KE_KEY, 0x17, { KEY_ZOOM } },
252 {KE_KEY, 0x1f, { KEY_BATTERY } },
253 {KE_KEY, 0x30, { KEY_VOLUMEUP } },
254 {KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
255 {KE_KEY, 0x32, { KEY_MUTE } },
256 {KE_KEY, 0x33, { KEY_SWITCHVIDEOMODE } },
257 {KE_KEY, 0x34, { KEY_SWITCHVIDEOMODE } },
258 {KE_KEY, 0x40, { KEY_PREVIOUSSONG } },
259 {KE_KEY, 0x41, { KEY_NEXTSONG } },
260 {KE_KEY, 0x43, { KEY_STOPCD } },
261 {KE_KEY, 0x45, { KEY_PLAYPAUSE } },
262 {KE_KEY, 0x4c, { KEY_MEDIA } },
263 {KE_KEY, 0x50, { KEY_EMAIL } },
264 {KE_KEY, 0x51, { KEY_WWW } },
265 {KE_KEY, 0x55, { KEY_CALC } },
266 {KE_KEY, 0x5C, { KEY_SCREENLOCK } }, /* Screenlock */
267 {KE_KEY, 0x5D, { KEY_WLAN } },
268 {KE_KEY, 0x5E, { KEY_WLAN } },
269 {KE_KEY, 0x5F, { KEY_WLAN } },
270 {KE_KEY, 0x60, { KEY_SWITCHVIDEOMODE } },
271 {KE_KEY, 0x61, { KEY_SWITCHVIDEOMODE } },
272 {KE_KEY, 0x62, { KEY_SWITCHVIDEOMODE } },
273 {KE_KEY, 0x63, { KEY_SWITCHVIDEOMODE } },
274 {KE_KEY, 0x6B, { KEY_F13 } }, /* Lock Touchpad */
275 {KE_KEY, 0x82, { KEY_CAMERA } },
276 {KE_KEY, 0x88, { KEY_WLAN } },
277 {KE_KEY, 0x8A, { KEY_PROG1 } },
278 {KE_KEY, 0x95, { KEY_MEDIA } },
279 {KE_KEY, 0x99, { KEY_PHONE } },
280 {KE_KEY, 0xc4, { KEY_KBDILLUMUP } },
281 {KE_KEY, 0xc5, { KEY_KBDILLUMDOWN } },
Corentin Chary034ce902009-01-20 16:17:43 +0100282 {KE_END, 0},
283};
284
Corentin Chary66a71dd2010-01-25 22:50:11 +0100285
Corentin Chary85091b72007-01-26 14:04:30 +0100286/*
287 * This function evaluates an ACPI method, given an int as parameter, the
288 * method is searched within the scope of the handle, can be NULL. The output
289 * of the method is written is output, which can also be NULL
290 *
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100291 * returns 0 if write is successful, -1 else.
Corentin Chary85091b72007-01-26 14:04:30 +0100292 */
Corentin Charyd8c67322009-11-28 10:27:51 +0100293static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
294 struct acpi_buffer *output)
Corentin Chary85091b72007-01-26 14:04:30 +0100295{
Corentin Charybe966662009-08-29 10:28:29 +0200296 struct acpi_object_list params; /* list of input parameters (an int) */
297 union acpi_object in_obj; /* the only param we use */
Corentin Chary85091b72007-01-26 14:04:30 +0100298 acpi_status status;
299
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100300 if (!handle)
301 return 0;
302
Corentin Chary85091b72007-01-26 14:04:30 +0100303 params.count = 1;
304 params.pointer = &in_obj;
305 in_obj.type = ACPI_TYPE_INTEGER;
306 in_obj.integer.value = val;
307
308 status = acpi_evaluate_object(handle, (char *)method, &params, output);
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100309 if (status == AE_OK)
310 return 0;
311 else
312 return -1;
Corentin Chary85091b72007-01-26 14:04:30 +0100313}
314
Corentin Charyd8c67322009-11-28 10:27:51 +0100315static int write_acpi_int(acpi_handle handle, const char *method, int val)
316{
317 return write_acpi_int_ret(handle, method, val, NULL);
318}
319
Corentin Charyd99b5772010-01-22 21:20:57 +0100320static int acpi_check_handle(acpi_handle handle, const char *method,
321 acpi_handle *ret)
322{
323 acpi_status status;
324
325 if (method == NULL)
326 return -ENODEV;
327
328 if (ret)
329 status = acpi_get_handle(handle, (char *)method,
330 ret);
331 else {
332 acpi_handle dummy;
333
334 status = acpi_get_handle(handle, (char *)method,
335 &dummy);
336 }
337
338 if (status != AE_OK) {
339 if (ret)
340 pr_warning("Error finding %s\n", method);
341 return -ENODEV;
342 }
343 return 0;
344}
345
Corentin Chary17e78f62010-01-13 22:21:33 +0100346/* Generic LED function */
Corentin Charye5593bf2010-01-17 17:20:11 +0100347static int asus_led_set(struct asus_laptop *asus, char *method,
Corentin Chary17e78f62010-01-13 22:21:33 +0100348 int value)
Corentin Charybe18cda2007-01-26 14:04:35 +0100349{
Corentin Charyd99b5772010-01-22 21:20:57 +0100350 if (!strcmp(method, METHOD_MLED))
Corentin Chary17e78f62010-01-13 22:21:33 +0100351 value = !value;
Corentin Charyd99b5772010-01-22 21:20:57 +0100352 else if (!strcmp(method, METHOD_GLED))
Corentin Chary17e78f62010-01-13 22:21:33 +0100353 value = !value + 1;
354 else
355 value = !!value;
Corentin Charybe18cda2007-01-26 14:04:35 +0100356
Corentin Charye5593bf2010-01-17 17:20:11 +0100357 return write_acpi_int(asus->handle, method, value);
Corentin Charybe18cda2007-01-26 14:04:35 +0100358}
359
Corentin Charybe4ee822009-12-06 16:27:09 +0100360/*
361 * LEDs
362 */
363#define ASUS_LED(object, ledname, max) \
364 static void object##_led_set(struct led_classdev *led_cdev, \
365 enum led_brightness value); \
366 static enum led_brightness object##_led_get( \
367 struct led_classdev *led_cdev); \
368 static void object##_led_update(struct work_struct *ignored); \
369 static struct led_classdev object##_led = { \
370 .name = "asus::" ledname, \
371 .brightness_set = object##_led_set, \
372 .brightness_get = object##_led_get, \
373 .max_brightness = max \
374 }
375
376ASUS_LED(mled, "mail", 1);
377ASUS_LED(tled, "touchpad", 1);
378ASUS_LED(rled, "record", 1);
379ASUS_LED(pled, "phone", 1);
380ASUS_LED(gled, "gaming", 1);
381ASUS_LED(kled, "kbd_backlight", 3);
382
Corentin Charybe18cda2007-01-26 14:04:35 +0100383/* /sys/class/led handlers */
Corentin Charyd99b5772010-01-22 21:20:57 +0100384#define ASUS_LED_HANDLER(object, method) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100385 static void object##_led_set(struct led_classdev *led_cdev, \
386 enum led_brightness value) \
387 { \
Corentin Chary9129d142009-12-01 22:39:41 +0100388 struct asus_laptop *asus = \
389 led_to_asus(led_cdev, object); \
390 \
391 asus->leds.object##_wk = (value > 0) ? 1 : 0; \
392 queue_work(asus->leds.workqueue, \
393 &asus->leds.object##_work); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100394 } \
Corentin Chary9129d142009-12-01 22:39:41 +0100395 static void object##_led_update(struct work_struct *work) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100396 { \
Corentin Chary9129d142009-12-01 22:39:41 +0100397 struct asus_laptop *asus = work_to_asus(work, object); \
398 \
399 int value = asus->leds.object##_wk; \
Corentin Charyd99b5772010-01-22 21:20:57 +0100400 asus_led_set(asus, method, value); \
Corentin Charyabfa57e2009-08-28 12:56:47 +0000401 } \
402 static enum led_brightness object##_led_get( \
403 struct led_classdev *led_cdev) \
404 { \
405 return led_cdev->brightness; \
Corentin Charybe18cda2007-01-26 14:04:35 +0100406 }
407
Corentin Charyd99b5772010-01-22 21:20:57 +0100408ASUS_LED_HANDLER(mled, METHOD_MLED);
409ASUS_LED_HANDLER(pled, METHOD_PLED);
410ASUS_LED_HANDLER(rled, METHOD_RLED);
411ASUS_LED_HANDLER(tled, METHOD_TLED);
412ASUS_LED_HANDLER(gled, METHOD_GLED);
Corentin Charybe18cda2007-01-26 14:04:35 +0100413
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000414/*
Corentin Charybe4ee822009-12-06 16:27:09 +0100415 * Keyboard backlight (also a LED)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000416 */
Corentin Charyd99b5772010-01-22 21:20:57 +0100417static int asus_kled_lvl(struct asus_laptop *asus)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000418{
419 unsigned long long kblv;
420 struct acpi_object_list params;
421 union acpi_object in_obj;
422 acpi_status rv;
423
424 params.count = 1;
425 params.pointer = &in_obj;
426 in_obj.type = ACPI_TYPE_INTEGER;
427 in_obj.integer.value = 2;
428
Corentin Charyd99b5772010-01-22 21:20:57 +0100429 rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
430 &params, &kblv);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000431 if (ACPI_FAILURE(rv)) {
432 pr_warning("Error reading kled level\n");
Corentin Chary4d441512010-01-13 22:26:24 +0100433 return -ENODEV;
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000434 }
435 return kblv;
436}
437
Corentin Chary4d441512010-01-13 22:26:24 +0100438static int asus_kled_set(struct asus_laptop *asus, int kblv)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000439{
440 if (kblv > 0)
441 kblv = (1 << 7) | (kblv & 0x7F);
442 else
443 kblv = 0;
444
Corentin Charyd99b5772010-01-22 21:20:57 +0100445 if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000446 pr_warning("Keyboard LED display write failed\n");
447 return -EINVAL;
448 }
449 return 0;
450}
451
452static void kled_led_set(struct led_classdev *led_cdev,
453 enum led_brightness value)
454{
Corentin Chary9129d142009-12-01 22:39:41 +0100455 struct asus_laptop *asus = led_to_asus(led_cdev, kled);
456
457 asus->leds.kled_wk = value;
458 queue_work(asus->leds.workqueue, &asus->leds.kled_work);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000459}
460
Corentin Chary9129d142009-12-01 22:39:41 +0100461static void kled_led_update(struct work_struct *work)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000462{
Corentin Chary9129d142009-12-01 22:39:41 +0100463 struct asus_laptop *asus = work_to_asus(work, kled);
464
Corentin Chary4d441512010-01-13 22:26:24 +0100465 asus_kled_set(asus, asus->leds.kled_wk);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000466}
467
468static enum led_brightness kled_led_get(struct led_classdev *led_cdev)
469{
Corentin Charyd99b5772010-01-22 21:20:57 +0100470 struct asus_laptop *asus = led_to_asus(led_cdev, kled);
471
472 return asus_kled_lvl(asus);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000473}
474
Corentin Charybe4ee822009-12-06 16:27:09 +0100475#define ASUS_LED_UNREGISTER(object) \
476 if (object##_led.dev) \
477 led_classdev_unregister(&object##_led)
478
479static void asus_led_exit(struct asus_laptop *asus)
480{
481 ASUS_LED_UNREGISTER(mled);
482 ASUS_LED_UNREGISTER(tled);
483 ASUS_LED_UNREGISTER(pled);
484 ASUS_LED_UNREGISTER(rled);
485 ASUS_LED_UNREGISTER(gled);
486 ASUS_LED_UNREGISTER(kled);
487 if (asus->leds.workqueue) {
488 destroy_workqueue(asus->leds.workqueue);
489 asus->leds.workqueue = NULL;
490 }
491}
492
493/* Ugly macro, need to fix that later */
Corentin Charyd99b5772010-01-22 21:20:57 +0100494#define ASUS_LED_REGISTER(asus, object, _name, max, method) \
Corentin Charybe4ee822009-12-06 16:27:09 +0100495 do { \
496 struct led_classdev *ldev = &asus->leds.object; \
Corentin Charyd99b5772010-01-22 21:20:57 +0100497 \
498 if (method && acpi_check_handle(asus->handle, method, NULL)) \
Corentin Charybe4ee822009-12-06 16:27:09 +0100499 break ; \
500 \
501 INIT_WORK(&asus->leds.object##_work, object##_led_update); \
502 ldev->name = "asus::" _name; \
503 ldev->brightness_set = object##_led_set; \
504 ldev->max_brightness = max; \
505 rv = led_classdev_register(&asus->platform_device->dev, ldev); \
506 if (rv) \
507 goto error; \
508 } while (0)
509
510static int asus_led_init(struct asus_laptop *asus)
511{
512 int rv;
513
514 /*
515 * Functions that actually update the LED's are called from a
516 * workqueue. By doing this as separate work rather than when the LED
517 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
518 * potentially bad time, such as a timer interrupt.
519 */
520 asus->leds.workqueue = create_singlethread_workqueue("led_workqueue");
521 if (!asus->leds.workqueue)
522 return -ENOMEM;
523
Corentin Charyd99b5772010-01-22 21:20:57 +0100524 ASUS_LED_REGISTER(asus, mled, "mail", 1, METHOD_MLED);
525 ASUS_LED_REGISTER(asus, tled, "touchpad", 1, METHOD_TLED);
526 ASUS_LED_REGISTER(asus, rled, "record", 1, METHOD_RLED);
527 ASUS_LED_REGISTER(asus, pled, "phone", 1, METHOD_PLED);
528 ASUS_LED_REGISTER(asus, gled, "gaming", 1, METHOD_GLED);
529 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
530 !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL))
531 ASUS_LED_REGISTER(asus, kled, "kbd_backlight", 3, NULL);
Corentin Charybe4ee822009-12-06 16:27:09 +0100532error:
533 if (rv)
534 asus_led_exit(asus);
535 return rv;
536}
537
538/*
539 * Backlight device
540 */
Corentin Chary3e68ae72010-01-13 22:10:39 +0100541static int asus_lcd_status(struct asus_laptop *asus)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100542{
Corentin Chary3e68ae72010-01-13 22:10:39 +0100543 return asus->lcd_state;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100544}
545
Corentin Chary3e68ae72010-01-13 22:10:39 +0100546static int asus_lcd_set(struct asus_laptop *asus, int value)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100547{
548 int lcd = 0;
549 acpi_status status = 0;
550
Corentin Chary3e68ae72010-01-13 22:10:39 +0100551 lcd = !!value;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100552
Corentin Chary3e68ae72010-01-13 22:10:39 +0100553 if (lcd == asus_lcd_status(asus))
Corentin Chary6b7091e2007-01-26 14:04:45 +0100554 return 0;
555
Corentin Chary3e68ae72010-01-13 22:10:39 +0100556 if (!lcd_switch_handle)
557 return -ENODEV;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100558
Corentin Chary3e68ae72010-01-13 22:10:39 +0100559 status = acpi_evaluate_object(lcd_switch_handle,
560 NULL, NULL, NULL);
561
562 if (ACPI_FAILURE(status)) {
563 pr_warning("Error switching LCD\n");
564 return -ENODEV;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100565 }
566
Corentin Chary3e68ae72010-01-13 22:10:39 +0100567 asus->lcd_state = lcd;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100568 return 0;
569}
570
Corentin Chary9129d142009-12-01 22:39:41 +0100571static void lcd_blank(struct asus_laptop *asus, int blank)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100572{
Corentin Chary7c247642009-11-30 22:13:54 +0100573 struct backlight_device *bd = asus->backlight_device;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100574
Corentin Chary3e68ae72010-01-13 22:10:39 +0100575 asus->lcd_state = (blank == FB_BLANK_UNBLANK);
576
Len Brown8def05f2007-01-30 01:46:43 -0500577 if (bd) {
Richard Purdie599a52d2007-02-10 23:07:48 +0000578 bd->props.power = blank;
Richard Purdie28ee0862007-02-08 22:25:09 +0000579 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100580 }
581}
582
Corentin Chary4d441512010-01-13 22:26:24 +0100583static int asus_read_brightness(struct backlight_device *bd)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100584{
Corentin Charyd99b5772010-01-22 21:20:57 +0100585 struct asus_laptop *asus = bl_get_data(bd);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400586 unsigned long long value;
Corentin Chary9a816852007-03-11 10:25:38 +0100587 acpi_status rv = AE_OK;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100588
Corentin Charyd99b5772010-01-22 21:20:57 +0100589 rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
590 NULL, &value);
Corentin Chary9a816852007-03-11 10:25:38 +0100591 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200592 pr_warning("Error reading brightness\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100593
594 return value;
595}
596
Corentin Chary4d441512010-01-13 22:26:24 +0100597static int asus_set_brightness(struct backlight_device *bd, int value)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100598{
Corentin Charyd99b5772010-01-22 21:20:57 +0100599 struct asus_laptop *asus = bl_get_data(bd);
600
601 if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200602 pr_warning("Error changing brightness\n");
Corentin Charye5b50f62009-11-28 10:19:55 +0100603 return -EIO;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100604 }
Corentin Charye5b50f62009-11-28 10:19:55 +0100605 return 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100606}
607
608static int update_bl_status(struct backlight_device *bd)
609{
Corentin Chary9129d142009-12-01 22:39:41 +0100610 struct asus_laptop *asus = bl_get_data(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100611 int rv;
Richard Purdie599a52d2007-02-10 23:07:48 +0000612 int value = bd->props.brightness;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100613
Corentin Chary4d441512010-01-13 22:26:24 +0100614 rv = asus_set_brightness(bd, value);
Len Brown8def05f2007-01-30 01:46:43 -0500615 if (rv)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100616 return rv;
617
Richard Purdie599a52d2007-02-10 23:07:48 +0000618 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
Corentin Chary3e68ae72010-01-13 22:10:39 +0100619 return asus_lcd_set(asus, value);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100620}
621
Corentin Charybe4ee822009-12-06 16:27:09 +0100622static struct backlight_ops asusbl_ops = {
Corentin Chary4d441512010-01-13 22:26:24 +0100623 .get_brightness = asus_read_brightness,
Corentin Charybe4ee822009-12-06 16:27:09 +0100624 .update_status = update_bl_status,
625};
626
627static int asus_backlight_init(struct asus_laptop *asus)
628{
629 struct backlight_device *bd;
630 struct device *dev = &asus->platform_device->dev;
631
Corentin Charyd99b5772010-01-22 21:20:57 +0100632 if (!acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) &&
633 !acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL) &&
634 lcd_switch_handle) {
Corentin Charybe4ee822009-12-06 16:27:09 +0100635 bd = backlight_device_register(ASUS_LAPTOP_FILE, dev,
636 asus, &asusbl_ops);
637 if (IS_ERR(bd)) {
638 pr_err("Could not register asus backlight device\n");
639 asus->backlight_device = NULL;
640 return PTR_ERR(bd);
641 }
642
643 asus->backlight_device = bd;
644
645 bd->props.max_brightness = 15;
Corentin Charybe4ee822009-12-06 16:27:09 +0100646 bd->props.power = FB_BLANK_UNBLANK;
Corentin Charyd99b5772010-01-22 21:20:57 +0100647 bd->props.brightness = asus_read_brightness(bd);
Corentin Charybe4ee822009-12-06 16:27:09 +0100648 backlight_update_status(bd);
649 }
650 return 0;
651}
652
653static void asus_backlight_exit(struct asus_laptop *asus)
654{
655 if (asus->backlight_device)
656 backlight_device_unregister(asus->backlight_device);
657}
658
Corentin Chary85091b72007-01-26 14:04:30 +0100659/*
660 * Platform device handlers
661 */
662
663/*
664 * We write our info in page, we begin at offset off and cannot write more
665 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
666 * number of bytes written in page
667 */
668static ssize_t show_infos(struct device *dev,
Len Brown8def05f2007-01-30 01:46:43 -0500669 struct device_attribute *attr, char *page)
Corentin Chary85091b72007-01-26 14:04:30 +0100670{
Corentin Chary9129d142009-12-01 22:39:41 +0100671 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary85091b72007-01-26 14:04:30 +0100672 int len = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400673 unsigned long long temp;
Corentin Charybe966662009-08-29 10:28:29 +0200674 char buf[16]; /* enough for all info */
Corentin Chary9a816852007-03-11 10:25:38 +0100675 acpi_status rv = AE_OK;
676
Corentin Chary85091b72007-01-26 14:04:30 +0100677 /*
678 * We use the easy way, we don't care of off and count, so we don't set eof
679 * to 1
680 */
681
Corentin Chary50a90c42009-11-30 21:55:12 +0100682 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
683 len += sprintf(page + len, "Model reference : %s\n", asus->name);
Corentin Chary85091b72007-01-26 14:04:30 +0100684 /*
685 * The SFUN method probably allows the original driver to get the list
686 * of features supported by a given model. For now, 0x0100 or 0x0800
687 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
688 * The significance of others is yet to be found.
689 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100690 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100691 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000692 len += sprintf(page + len, "SFUN value : %#x\n",
693 (uint) temp);
694 /*
695 * The HWRS method return informations about the hardware.
696 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
697 * The significance of others is yet to be found.
698 * If we don't find the method, we assume the device are present.
699 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100700 rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
Corentin Chary1d4a3802009-08-28 12:56:46 +0000701 if (!ACPI_FAILURE(rv))
702 len += sprintf(page + len, "HRWS value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100703 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100704 /*
705 * Another value for userspace: the ASYM method returns 0x02 for
706 * battery low and 0x04 for battery critical, its readings tend to be
707 * more accurate than those provided by _BST.
708 * Note: since not all the laptops provide this method, errors are
709 * silently ignored.
710 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100711 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100712 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000713 len += sprintf(page + len, "ASYM value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100714 (uint) temp);
Corentin Chary7c247642009-11-30 22:13:54 +0100715 if (asus->dsdt_info) {
716 snprintf(buf, 16, "%d", asus->dsdt_info->length);
Corentin Chary85091b72007-01-26 14:04:30 +0100717 len += sprintf(page + len, "DSDT length : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100718 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
Corentin Chary85091b72007-01-26 14:04:30 +0100719 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100720 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100721 len += sprintf(page + len, "DSDT revision : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100722 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100723 len += sprintf(page + len, "OEM id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100724 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100725 len += sprintf(page + len, "OEM table id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100726 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100727 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100728 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100729 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100730 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100731 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
732 }
733
734 return len;
735}
736
737static int parse_arg(const char *buf, unsigned long count, int *val)
738{
739 if (!count)
740 return 0;
741 if (count > 31)
742 return -EINVAL;
743 if (sscanf(buf, "%i", val) != 1)
744 return -EINVAL;
745 return count;
746}
747
Corentin Chary17e78f62010-01-13 22:21:33 +0100748static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
749 const char *buf, size_t count,
Corentin Charyd99b5772010-01-22 21:20:57 +0100750 const char *method)
Corentin Chary4564de12007-01-26 14:04:40 +0100751{
752 int rv, value;
753 int out = 0;
754
755 rv = parse_arg(buf, count, &value);
756 if (rv > 0)
757 out = value ? 1 : 0;
758
Corentin Charyd99b5772010-01-22 21:20:57 +0100759 if (write_acpi_int(asus->handle, method, value))
Corentin Chary17e78f62010-01-13 22:21:33 +0100760 return -ENODEV;
Corentin Chary4564de12007-01-26 14:04:40 +0100761 return rv;
762}
763
764/*
Corentin Chary722ad972007-01-26 14:04:55 +0100765 * LEDD display
766 */
767static ssize_t show_ledd(struct device *dev,
768 struct device_attribute *attr, char *buf)
769{
Corentin Chary9129d142009-12-01 22:39:41 +0100770 struct asus_laptop *asus = dev_get_drvdata(dev);
771
Corentin Chary50a90c42009-11-30 21:55:12 +0100772 return sprintf(buf, "0x%08x\n", asus->ledd_status);
Corentin Chary722ad972007-01-26 14:04:55 +0100773}
774
775static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
776 const char *buf, size_t count)
777{
Corentin Chary9129d142009-12-01 22:39:41 +0100778 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary722ad972007-01-26 14:04:55 +0100779 int rv, value;
780
781 rv = parse_arg(buf, count, &value);
782 if (rv > 0) {
Corentin Charyd99b5772010-01-22 21:20:57 +0100783 if (write_acpi_int(asus->handle, METHOD_LEDD, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200784 pr_warning("LED display write failed\n");
Corentin Chary722ad972007-01-26 14:04:55 +0100785 else
Corentin Chary50a90c42009-11-30 21:55:12 +0100786 asus->ledd_status = (u32) value;
Corentin Chary722ad972007-01-26 14:04:55 +0100787 }
788 return rv;
789}
790
791/*
Corentin Charyaa9df932010-01-13 21:49:10 +0100792 * Wireless
793 */
794static int asus_wireless_status(struct asus_laptop *asus, int mask)
795{
796 unsigned long long status;
797 acpi_status rv = AE_OK;
798
799 if (!asus->have_rsts)
800 return (asus->wireless_status & mask) ? 1 : 0;
801
Corentin Charyd99b5772010-01-22 21:20:57 +0100802 rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
803 NULL, &status);
Corentin Charyaa9df932010-01-13 21:49:10 +0100804 if (ACPI_FAILURE(rv)) {
805 pr_warning("Error reading Wireless status\n");
806 return -EINVAL;
807 }
808 return !!(status & mask);
809}
810
811/*
Corentin Chary4564de12007-01-26 14:04:40 +0100812 * WLAN
813 */
Corentin Charye5593bf2010-01-17 17:20:11 +0100814static int asus_wlan_set(struct asus_laptop *asus, int status)
815{
816 if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
817 pr_warning("Error setting wlan status to %d", status);
818 return -EIO;
819 }
820 return 0;
821}
822
Corentin Chary4564de12007-01-26 14:04:40 +0100823static ssize_t show_wlan(struct device *dev,
824 struct device_attribute *attr, char *buf)
825{
Corentin Chary9129d142009-12-01 22:39:41 +0100826 struct asus_laptop *asus = dev_get_drvdata(dev);
827
Corentin Chary17e78f62010-01-13 22:21:33 +0100828 return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100829}
830
831static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
832 const char *buf, size_t count)
833{
Corentin Chary9129d142009-12-01 22:39:41 +0100834 struct asus_laptop *asus = dev_get_drvdata(dev);
835
Corentin Charyd99b5772010-01-22 21:20:57 +0100836 return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
Corentin Chary4564de12007-01-26 14:04:40 +0100837}
838
839/*
840 * Bluetooth
841 */
Corentin Charye5593bf2010-01-17 17:20:11 +0100842static int asus_bluetooth_set(struct asus_laptop *asus, int status)
843{
844 if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
845 pr_warning("Error setting bluetooth status to %d", status);
846 return -EIO;
847 }
848 return 0;
849}
850
Corentin Chary4564de12007-01-26 14:04:40 +0100851static ssize_t show_bluetooth(struct device *dev,
852 struct device_attribute *attr, char *buf)
853{
Corentin Chary9129d142009-12-01 22:39:41 +0100854 struct asus_laptop *asus = dev_get_drvdata(dev);
855
Corentin Chary17e78f62010-01-13 22:21:33 +0100856 return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100857}
858
Len Brown8def05f2007-01-30 01:46:43 -0500859static ssize_t store_bluetooth(struct device *dev,
860 struct device_attribute *attr, const char *buf,
861 size_t count)
Corentin Chary4564de12007-01-26 14:04:40 +0100862{
Corentin Chary9129d142009-12-01 22:39:41 +0100863 struct asus_laptop *asus = dev_get_drvdata(dev);
864
Corentin Charyd99b5772010-01-22 21:20:57 +0100865 return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
Corentin Chary4564de12007-01-26 14:04:40 +0100866}
867
Corentin Chary78127b42007-01-26 14:04:49 +0100868/*
869 * Display
870 */
Corentin Chary4d441512010-01-13 22:26:24 +0100871static void asus_set_display(struct asus_laptop *asus, int value)
Corentin Chary78127b42007-01-26 14:04:49 +0100872{
873 /* no sanity check needed for now */
Corentin Charyd99b5772010-01-22 21:20:57 +0100874 if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200875 pr_warning("Error setting display\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100876 return;
877}
878
Corentin Chary9129d142009-12-01 22:39:41 +0100879static int read_display(struct asus_laptop *asus)
Corentin Chary78127b42007-01-26 14:04:49 +0100880{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400881 unsigned long long value = 0;
Corentin Chary9a816852007-03-11 10:25:38 +0100882 acpi_status rv = AE_OK;
Corentin Chary78127b42007-01-26 14:04:49 +0100883
Corentin Charybe966662009-08-29 10:28:29 +0200884 /*
885 * In most of the case, we know how to set the display, but sometime
886 * we can't read it
887 */
Len Brown8def05f2007-01-30 01:46:43 -0500888 if (display_get_handle) {
Corentin Chary9a816852007-03-11 10:25:38 +0100889 rv = acpi_evaluate_integer(display_get_handle, NULL,
890 NULL, &value);
891 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200892 pr_warning("Error reading display status\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100893 }
894
895 value &= 0x0F; /* needed for some models, shouldn't hurt others */
896
897 return value;
898}
Len Brown8def05f2007-01-30 01:46:43 -0500899
Corentin Chary78127b42007-01-26 14:04:49 +0100900/*
901 * Now, *this* one could be more user-friendly, but so far, no-one has
902 * complained. The significance of bits is the same as in store_disp()
903 */
904static ssize_t show_disp(struct device *dev,
905 struct device_attribute *attr, char *buf)
906{
Corentin Chary9129d142009-12-01 22:39:41 +0100907 struct asus_laptop *asus = dev_get_drvdata(dev);
908
909 return sprintf(buf, "%d\n", read_display(asus));
Corentin Chary78127b42007-01-26 14:04:49 +0100910}
911
912/*
913 * Experimental support for display switching. As of now: 1 should activate
914 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
915 * Any combination (bitwise) of these will suffice. I never actually tested 4
916 * displays hooked up simultaneously, so be warned. See the acpi4asus README
917 * for more info.
918 */
919static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
920 const char *buf, size_t count)
921{
Corentin Chary9129d142009-12-01 22:39:41 +0100922 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary78127b42007-01-26 14:04:49 +0100923 int rv, value;
924
925 rv = parse_arg(buf, count, &value);
926 if (rv > 0)
Corentin Chary4d441512010-01-13 22:26:24 +0100927 asus_set_display(asus, value);
Corentin Chary78127b42007-01-26 14:04:49 +0100928 return rv;
929}
930
Corentin Chary8b857352007-01-26 14:04:58 +0100931/*
932 * Light Sens
933 */
Corentin Chary4d441512010-01-13 22:26:24 +0100934static void asus_als_switch(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100935{
Corentin Charyd99b5772010-01-22 21:20:57 +0100936 if (write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200937 pr_warning("Error setting light sensor switch\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100938 asus->light_switch = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100939}
940
941static ssize_t show_lssw(struct device *dev,
942 struct device_attribute *attr, char *buf)
943{
Corentin Chary9129d142009-12-01 22:39:41 +0100944 struct asus_laptop *asus = dev_get_drvdata(dev);
945
Corentin Chary50a90c42009-11-30 21:55:12 +0100946 return sprintf(buf, "%d\n", asus->light_switch);
Corentin Chary8b857352007-01-26 14:04:58 +0100947}
948
949static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
950 const char *buf, size_t count)
951{
Corentin Chary9129d142009-12-01 22:39:41 +0100952 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100953 int rv, value;
954
955 rv = parse_arg(buf, count, &value);
956 if (rv > 0)
Corentin Chary4d441512010-01-13 22:26:24 +0100957 asus_als_switch(asus, value ? 1 : 0);
Corentin Chary8b857352007-01-26 14:04:58 +0100958
959 return rv;
960}
961
Corentin Chary4d441512010-01-13 22:26:24 +0100962static void asus_als_level(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100963{
Corentin Charyd99b5772010-01-22 21:20:57 +0100964 if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200965 pr_warning("Error setting light sensor level\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100966 asus->light_level = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100967}
968
969static ssize_t show_lslvl(struct device *dev,
970 struct device_attribute *attr, char *buf)
971{
Corentin Chary9129d142009-12-01 22:39:41 +0100972 struct asus_laptop *asus = dev_get_drvdata(dev);
973
Corentin Chary50a90c42009-11-30 21:55:12 +0100974 return sprintf(buf, "%d\n", asus->light_level);
Corentin Chary8b857352007-01-26 14:04:58 +0100975}
976
977static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
978 const char *buf, size_t count)
979{
Corentin Chary9129d142009-12-01 22:39:41 +0100980 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100981 int rv, value;
982
983 rv = parse_arg(buf, count, &value);
984 if (rv > 0) {
985 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
986 /* 0 <= value <= 15 */
Corentin Chary4d441512010-01-13 22:26:24 +0100987 asus_als_level(asus, value);
Corentin Chary8b857352007-01-26 14:04:58 +0100988 }
989
990 return rv;
991}
992
Corentin Charye539c2f2007-05-06 14:47:06 +0200993/*
994 * GPS
Corentin Chary6358bf22010-01-13 21:55:44 +0100995 * TODO: use rfkill
Corentin Charye539c2f2007-05-06 14:47:06 +0200996 */
Corentin Chary6358bf22010-01-13 21:55:44 +0100997static int asus_gps_status(struct asus_laptop *asus)
998{
999 unsigned long long status;
1000 acpi_status rv = AE_OK;
1001
Corentin Charyd99b5772010-01-22 21:20:57 +01001002 rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
1003 NULL, &status);
Corentin Chary6358bf22010-01-13 21:55:44 +01001004 if (ACPI_FAILURE(rv)) {
1005 pr_warning("Error reading GPS status\n");
1006 return -ENODEV;
1007 }
1008 return !!status;
1009}
1010
1011static int asus_gps_switch(struct asus_laptop *asus, int status)
1012{
Corentin Charyd99b5772010-01-22 21:20:57 +01001013 const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
Corentin Chary6358bf22010-01-13 21:55:44 +01001014
Corentin Charyd99b5772010-01-22 21:20:57 +01001015 if (write_acpi_int(asus->handle, meth, 0x02))
Corentin Chary6358bf22010-01-13 21:55:44 +01001016 return -ENODEV;
1017 return 0;
1018}
1019
Corentin Charye539c2f2007-05-06 14:47:06 +02001020static ssize_t show_gps(struct device *dev,
1021 struct device_attribute *attr, char *buf)
1022{
Corentin Chary9129d142009-12-01 22:39:41 +01001023 struct asus_laptop *asus = dev_get_drvdata(dev);
1024
Corentin Chary6358bf22010-01-13 21:55:44 +01001025 return sprintf(buf, "%d\n", asus_gps_status(asus));
Corentin Charye539c2f2007-05-06 14:47:06 +02001026}
1027
1028static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
1029 const char *buf, size_t count)
1030{
Corentin Chary6358bf22010-01-13 21:55:44 +01001031 struct asus_laptop *asus = dev_get_drvdata(dev);
1032 int rv, value;
1033 int ret;
Corentin Chary9129d142009-12-01 22:39:41 +01001034
Corentin Chary6358bf22010-01-13 21:55:44 +01001035 rv = parse_arg(buf, count, &value);
1036 if (rv <= 0)
1037 return -EINVAL;
1038 ret = asus_gps_switch(asus, !!value);
1039 if (ret)
1040 return ret;
1041 return rv;
Corentin Charye539c2f2007-05-06 14:47:06 +02001042}
1043
Corentin Chary034ce902009-01-20 16:17:43 +01001044/*
Corentin Charybe4ee822009-12-06 16:27:09 +01001045 * Input device (i.e. hotkeys)
Corentin Chary034ce902009-01-20 16:17:43 +01001046 */
Corentin Charybe4ee822009-12-06 16:27:09 +01001047static void asus_input_notify(struct asus_laptop *asus, int event)
1048{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001049 if (asus->inputdev)
1050 sparse_keymap_report_event(asus->inputdev, event, 1, true);
Corentin Charybe4ee822009-12-06 16:27:09 +01001051}
1052
1053static int asus_input_init(struct asus_laptop *asus)
1054{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001055 struct input_dev *input;
1056 int error;
Corentin Charybe4ee822009-12-06 16:27:09 +01001057
Corentin Chary66a71dd2010-01-25 22:50:11 +01001058 input = input_allocate_device();
1059 if (!input) {
Corentin Charybe4ee822009-12-06 16:27:09 +01001060 pr_info("Unable to allocate input device\n");
1061 return 0;
1062 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001063 input->name = "Asus Laptop extra buttons";
1064 input->phys = ASUS_LAPTOP_FILE "/input0";
1065 input->id.bustype = BUS_HOST;
1066 input->dev.parent = &asus->platform_device->dev;
1067 input_set_drvdata(input, asus);
Corentin Charybe4ee822009-12-06 16:27:09 +01001068
Corentin Chary66a71dd2010-01-25 22:50:11 +01001069 error = sparse_keymap_setup(input, asus_keymap, NULL);
1070 if (error) {
1071 pr_err("Unable to setup input device keymap\n");
1072 goto err_keymap;
Corentin Charybe4ee822009-12-06 16:27:09 +01001073 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001074 error = input_register_device(input);
1075 if (error) {
Corentin Charybe4ee822009-12-06 16:27:09 +01001076 pr_info("Unable to register input device\n");
Corentin Chary66a71dd2010-01-25 22:50:11 +01001077 goto err_device;
Corentin Charybe4ee822009-12-06 16:27:09 +01001078 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001079
1080 asus->inputdev = input;
1081 return 0;
1082
1083err_keymap:
1084 sparse_keymap_free(input);
1085err_device:
1086 input_free_device(input);
1087 return error;
Corentin Charybe4ee822009-12-06 16:27:09 +01001088}
1089
1090static void asus_input_exit(struct asus_laptop *asus)
1091{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001092 if (asus->inputdev) {
1093 sparse_keymap_free(asus->inputdev);
Corentin Charybe4ee822009-12-06 16:27:09 +01001094 input_unregister_device(asus->inputdev);
Corentin Chary66a71dd2010-01-25 22:50:11 +01001095 }
Corentin Charybe4ee822009-12-06 16:27:09 +01001096}
1097
1098/*
1099 * ACPI driver
1100 */
Corentin Chary600ad522009-11-30 21:42:42 +01001101static void asus_acpi_notify(struct acpi_device *device, u32 event)
Corentin Chary85091b72007-01-26 14:04:30 +01001102{
Corentin Chary9129d142009-12-01 22:39:41 +01001103 struct asus_laptop *asus = acpi_driver_data(device);
Corentin Chary6050c8d2009-02-15 19:30:19 +01001104 u16 count;
Corentin Chary034ce902009-01-20 16:17:43 +01001105
Corentin Chary6b7091e2007-01-26 14:04:45 +01001106 /*
1107 * We need to tell the backlight device when the backlight power is
1108 * switched
1109 */
Corentin Chary3e68ae72010-01-13 22:10:39 +01001110 if (event == ATKD_LCD_ON)
Corentin Chary9129d142009-12-01 22:39:41 +01001111 lcd_blank(asus, FB_BLANK_UNBLANK);
Corentin Chary3e68ae72010-01-13 22:10:39 +01001112 else if (event == ATKD_LCD_OFF)
Corentin Chary9129d142009-12-01 22:39:41 +01001113 lcd_blank(asus, FB_BLANK_POWERDOWN);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001114
Corentin Chary619d8b12009-11-28 10:35:37 +01001115 /* TODO Find a better way to handle events count. */
Corentin Chary50a90c42009-11-30 21:55:12 +01001116 count = asus->event_count[event % 128]++;
1117 acpi_bus_generate_proc_event(asus->device, event, count);
1118 acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
1119 dev_name(&asus->device->dev), event,
Corentin Chary6050c8d2009-02-15 19:30:19 +01001120 count);
Corentin Chary85091b72007-01-26 14:04:30 +01001121
Corentin Charybe4ee822009-12-06 16:27:09 +01001122 asus_input_notify(asus, event);
Corentin Chary85091b72007-01-26 14:04:30 +01001123}
1124
1125#define ASUS_CREATE_DEVICE_ATTR(_name) \
1126 struct device_attribute dev_attr_##_name = { \
1127 .attr = { \
1128 .name = __stringify(_name), \
Tejun Heo7b595752007-06-14 03:45:17 +09001129 .mode = 0 }, \
Corentin Chary85091b72007-01-26 14:04:30 +01001130 .show = NULL, \
1131 .store = NULL, \
1132 }
1133
1134#define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
1135 do { \
1136 dev_attr_##_name.attr.mode = _mode; \
1137 dev_attr_##_name.show = _show; \
1138 dev_attr_##_name.store = _store; \
1139 } while(0)
1140
1141static ASUS_CREATE_DEVICE_ATTR(infos);
Corentin Chary4564de12007-01-26 14:04:40 +01001142static ASUS_CREATE_DEVICE_ATTR(wlan);
1143static ASUS_CREATE_DEVICE_ATTR(bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +01001144static ASUS_CREATE_DEVICE_ATTR(display);
Corentin Chary722ad972007-01-26 14:04:55 +01001145static ASUS_CREATE_DEVICE_ATTR(ledd);
Corentin Chary8b857352007-01-26 14:04:58 +01001146static ASUS_CREATE_DEVICE_ATTR(ls_switch);
1147static ASUS_CREATE_DEVICE_ATTR(ls_level);
Corentin Charye539c2f2007-05-06 14:47:06 +02001148static ASUS_CREATE_DEVICE_ATTR(gps);
Corentin Chary85091b72007-01-26 14:04:30 +01001149
1150static struct attribute *asuspf_attributes[] = {
Len Brown8def05f2007-01-30 01:46:43 -05001151 &dev_attr_infos.attr,
1152 &dev_attr_wlan.attr,
1153 &dev_attr_bluetooth.attr,
1154 &dev_attr_display.attr,
1155 &dev_attr_ledd.attr,
1156 &dev_attr_ls_switch.attr,
1157 &dev_attr_ls_level.attr,
Corentin Charye539c2f2007-05-06 14:47:06 +02001158 &dev_attr_gps.attr,
Len Brown8def05f2007-01-30 01:46:43 -05001159 NULL
Corentin Chary85091b72007-01-26 14:04:30 +01001160};
1161
Corentin Chary600ad522009-11-30 21:42:42 +01001162static struct attribute_group platform_attribute_group = {
Len Brown8def05f2007-01-30 01:46:43 -05001163 .attrs = asuspf_attributes
Corentin Chary85091b72007-01-26 14:04:30 +01001164};
1165
Corentin Chary9129d142009-12-01 22:39:41 +01001166static int asus_platform_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001167{
1168 int result;
1169
Corentin Chary50a90c42009-11-30 21:55:12 +01001170 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1171 if (!asus->platform_device)
Corentin Chary600ad522009-11-30 21:42:42 +01001172 return -ENOMEM;
Corentin Chary9129d142009-12-01 22:39:41 +01001173 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001174
Corentin Chary50a90c42009-11-30 21:55:12 +01001175 result = platform_device_add(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001176 if (result)
1177 goto fail_platform_device;
1178
Corentin Chary50a90c42009-11-30 21:55:12 +01001179 result = sysfs_create_group(&asus->platform_device->dev.kobj,
Corentin Chary600ad522009-11-30 21:42:42 +01001180 &platform_attribute_group);
1181 if (result)
1182 goto fail_sysfs;
1183 return 0;
1184
1185fail_sysfs:
Corentin Chary50a90c42009-11-30 21:55:12 +01001186 platform_device_del(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001187fail_platform_device:
Corentin Chary50a90c42009-11-30 21:55:12 +01001188 platform_device_put(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001189 return result;
1190}
1191
Corentin Chary9129d142009-12-01 22:39:41 +01001192static void asus_platform_exit(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001193{
Corentin Chary50a90c42009-11-30 21:55:12 +01001194 sysfs_remove_group(&asus->platform_device->dev.kobj,
Corentin Chary600ad522009-11-30 21:42:42 +01001195 &platform_attribute_group);
Corentin Chary50a90c42009-11-30 21:55:12 +01001196 platform_device_unregister(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001197}
1198
1199static struct platform_driver platform_driver = {
Len Brown8def05f2007-01-30 01:46:43 -05001200 .driver = {
Corentin Chary9129d142009-12-01 22:39:41 +01001201 .name = ASUS_LAPTOP_FILE,
1202 .owner = THIS_MODULE,
1203 }
Corentin Chary85091b72007-01-26 14:04:30 +01001204};
1205
Corentin Chary9129d142009-12-01 22:39:41 +01001206static void asus_laptop_add_fs(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001207{
1208 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
Corentin Chary4564de12007-01-26 14:04:40 +01001209
Corentin Charyd99b5772010-01-22 21:20:57 +01001210 if (!acpi_check_handle(asus->handle, METHOD_WLAN, NULL))
Corentin Chary4564de12007-01-26 14:04:40 +01001211 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
1212
Corentin Charyd99b5772010-01-22 21:20:57 +01001213 if (!acpi_check_handle(asus->handle, METHOD_BLUETOOTH, NULL))
Corentin Chary4564de12007-01-26 14:04:40 +01001214 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
1215 show_bluetooth, store_bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +01001216
Corentin Charyd99b5772010-01-22 21:20:57 +01001217 if (!acpi_check_handle(asus->handle, METHOD_SWITCH_DISPLAY, NULL)) {
1218 if (display_get_handle)
1219 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp,
1220 store_disp);
1221 else
1222 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
1223 }
Corentin Chary78127b42007-01-26 14:04:49 +01001224
Corentin Charyd99b5772010-01-22 21:20:57 +01001225 if (!acpi_check_handle(asus->handle, METHOD_LEDD, NULL))
Corentin Chary722ad972007-01-26 14:04:55 +01001226 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
1227
Corentin Charyd99b5772010-01-22 21:20:57 +01001228 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1229 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
Corentin Chary8b857352007-01-26 14:04:58 +01001230 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
1231 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
1232 }
Corentin Charye539c2f2007-05-06 14:47:06 +02001233
Corentin Charyd99b5772010-01-22 21:20:57 +01001234 if (!acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) &&
1235 !acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) &&
1236 !acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
Corentin Charye539c2f2007-05-06 14:47:06 +02001237 ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
Corentin Chary85091b72007-01-26 14:04:30 +01001238}
1239
Len Brown8def05f2007-01-30 01:46:43 -05001240static int asus_handle_init(char *name, acpi_handle * handle,
Corentin Chary85091b72007-01-26 14:04:30 +01001241 char **paths, int num_paths)
1242{
1243 int i;
1244 acpi_status status;
1245
1246 for (i = 0; i < num_paths; i++) {
1247 status = acpi_get_handle(NULL, paths[i], handle);
1248 if (ACPI_SUCCESS(status))
1249 return 0;
1250 }
1251
1252 *handle = NULL;
1253 return -ENODEV;
1254}
1255
1256#define ASUS_HANDLE_INIT(object) \
1257 asus_handle_init(#object, &object##_handle, object##_paths, \
1258 ARRAY_SIZE(object##_paths))
1259
Corentin Chary85091b72007-01-26 14:04:30 +01001260/*
Corentin Chary50a90c42009-11-30 21:55:12 +01001261 * This function is used to initialize the context with right values. In this
1262 * method, we can make all the detection we want, and modify the asus_laptop
1263 * struct
Corentin Chary85091b72007-01-26 14:04:30 +01001264 */
Corentin Chary7c247642009-11-30 22:13:54 +01001265static int asus_laptop_get_info(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001266{
1267 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Corentin Chary85091b72007-01-26 14:04:30 +01001268 union acpi_object *model = NULL;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001269 unsigned long long bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +01001270 char *string = NULL;
1271 acpi_status status;
1272
1273 /*
1274 * Get DSDT headers early enough to allow for differentiating between
1275 * models, but late enough to allow acpi_bus_register_driver() to fail
1276 * before doing anything ACPI-specific. Should we encounter a machine,
1277 * which needs special handling (i.e. its hotkey device has a different
Corentin Chary7c247642009-11-30 22:13:54 +01001278 * HID), this bit will be moved.
Corentin Chary85091b72007-01-26 14:04:30 +01001279 */
Corentin Chary7c247642009-11-30 22:13:54 +01001280 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
Corentin Chary85091b72007-01-26 14:04:30 +01001281 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001282 pr_warning("Couldn't get the DSDT table header\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001283
1284 /* We have to write 0 on init this far for all ASUS models */
Corentin Chary50a90c42009-11-30 21:55:12 +01001285 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001286 pr_err("Hotkey initialization failed\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001287 return -ENODEV;
1288 }
1289
1290 /* This needs to be called for some laptops to init properly */
Corentin Chary9a816852007-03-11 10:25:38 +01001291 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001292 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
Corentin Chary9a816852007-03-11 10:25:38 +01001293 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001294 pr_warning("Error calling BSTS\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001295 else if (bsts_result)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001296 pr_notice("BSTS called, 0x%02x returned\n",
Corentin Chary9a816852007-03-11 10:25:38 +01001297 (uint) bsts_result);
Corentin Chary85091b72007-01-26 14:04:30 +01001298
Corentin Chary185e5af2007-03-11 10:27:33 +01001299 /* This too ... */
Corentin Charye5593bf2010-01-17 17:20:11 +01001300 if (write_acpi_int(asus->handle, "CWAP", wapf))
1301 pr_err("Error calling CWAP(%d)\n", wapf);
Corentin Chary85091b72007-01-26 14:04:30 +01001302 /*
1303 * Try to match the object returned by INIT to the specific model.
1304 * Handle every possible object (or the lack of thereof) the DSDT
1305 * writers might throw at us. When in trouble, we pass NULL to
1306 * asus_model_match() and try something completely different.
1307 */
1308 if (buffer.pointer) {
1309 model = buffer.pointer;
1310 switch (model->type) {
1311 case ACPI_TYPE_STRING:
1312 string = model->string.pointer;
1313 break;
1314 case ACPI_TYPE_BUFFER:
1315 string = model->buffer.pointer;
1316 break;
1317 default:
1318 string = "";
1319 break;
1320 }
1321 }
Corentin Chary50a90c42009-11-30 21:55:12 +01001322 asus->name = kstrdup(string, GFP_KERNEL);
1323 if (!asus->name)
Corentin Chary85091b72007-01-26 14:04:30 +01001324 return -ENOMEM;
1325
Len Brown8def05f2007-01-30 01:46:43 -05001326 if (*string)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001327 pr_notice(" %s model detected\n", string);
Corentin Chary85091b72007-01-26 14:04:30 +01001328
Corentin Chary4564de12007-01-26 14:04:40 +01001329 /*
1330 * The HWRS method return informations about the hardware.
1331 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1332 * The significance of others is yet to be found.
Corentin Chary4564de12007-01-26 14:04:40 +01001333 */
Corentin Chary9a816852007-03-11 10:25:38 +01001334 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001335 acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
Corentin Charyd99b5772010-01-22 21:20:57 +01001336 if (!ACPI_FAILURE(status))
1337 pr_notice(" HRWS returned %x", (int)hwrs_result);
Corentin Chary4564de12007-01-26 14:04:40 +01001338
Corentin Charyd99b5772010-01-22 21:20:57 +01001339 if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
Corentin Charyaa9df932010-01-13 21:49:10 +01001340 asus->have_rsts = true;
Corentin Chary4564de12007-01-26 14:04:40 +01001341
Corentin Charyd99b5772010-01-22 21:20:57 +01001342 /* Scheduled for removal */
Corentin Chary6b7091e2007-01-26 14:04:45 +01001343 ASUS_HANDLE_INIT(lcd_switch);
Corentin Chary78127b42007-01-26 14:04:49 +01001344 ASUS_HANDLE_INIT(display_get);
1345
Corentin Chary85091b72007-01-26 14:04:30 +01001346 kfree(model);
1347
1348 return AE_OK;
1349}
1350
Corentin Chary600ad522009-11-30 21:42:42 +01001351static bool asus_device_present;
1352
Corentin Chary9129d142009-12-01 22:39:41 +01001353static int __devinit asus_acpi_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001354{
1355 int result = 0;
1356
Corentin Chary50a90c42009-11-30 21:55:12 +01001357 result = acpi_bus_get_status(asus->device);
Corentin Chary600ad522009-11-30 21:42:42 +01001358 if (result)
1359 return result;
Corentin Chary50a90c42009-11-30 21:55:12 +01001360 if (!asus->device->status.present) {
Corentin Chary600ad522009-11-30 21:42:42 +01001361 pr_err("Hotkey device not present, aborting\n");
1362 return -ENODEV;
1363 }
1364
Corentin Chary7c247642009-11-30 22:13:54 +01001365 result = asus_laptop_get_info(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001366 if (result)
1367 return result;
1368
Corentin Chary9129d142009-12-01 22:39:41 +01001369 asus_laptop_add_fs(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001370
1371 /* 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
1378 /* Keyboard Backlight is on by default */
Corentin Charyd99b5772010-01-22 21:20:57 +01001379 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
Corentin Chary4d441512010-01-13 22:26:24 +01001380 asus_kled_set(asus, 1);
Corentin Chary600ad522009-11-30 21:42:42 +01001381
1382 /* LED display is off by default */
Corentin Chary50a90c42009-11-30 21:55:12 +01001383 asus->ledd_status = 0xFFF;
Corentin Chary600ad522009-11-30 21:42:42 +01001384
1385 /* Set initial values of light sensor and level */
Corentin Charybe4ee822009-12-06 16:27:09 +01001386 asus->light_switch = 0; /* Default to light sensor disabled */
1387 asus->light_level = 5; /* level 5 for sensor sensitivity */
Corentin Chary600ad522009-11-30 21:42:42 +01001388
Corentin Charyd99b5772010-01-22 21:20:57 +01001389 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1390 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
Corentin Chary4d441512010-01-13 22:26:24 +01001391 asus_als_switch(asus, asus->light_switch);
Corentin Chary4d441512010-01-13 22:26:24 +01001392 asus_als_level(asus, asus->light_level);
Corentin Charyd99b5772010-01-22 21:20:57 +01001393 }
Corentin Chary600ad522009-11-30 21:42:42 +01001394
1395 /* GPS is on by default */
Corentin Charyd99b5772010-01-22 21:20:57 +01001396 if (!acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) &&
1397 !acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) &&
1398 !acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
1399 asus_gps_switch(asus, 1);
Corentin Chary600ad522009-11-30 21:42:42 +01001400 return result;
1401}
1402
1403static int __devinit asus_acpi_add(struct acpi_device *device)
1404{
Corentin Chary9129d142009-12-01 22:39:41 +01001405 struct asus_laptop *asus;
Corentin Chary600ad522009-11-30 21:42:42 +01001406 int result;
1407
1408 pr_notice("Asus Laptop Support version %s\n",
1409 ASUS_LAPTOP_VERSION);
Corentin Chary50a90c42009-11-30 21:55:12 +01001410 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1411 if (!asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001412 return -ENOMEM;
Corentin Chary50a90c42009-11-30 21:55:12 +01001413 asus->handle = device->handle;
1414 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1415 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1416 device->driver_data = asus;
1417 asus->device = device;
Corentin Chary600ad522009-11-30 21:42:42 +01001418
Corentin Chary9129d142009-12-01 22:39:41 +01001419 result = asus_acpi_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001420 if (result)
1421 goto fail_platform;
1422
1423 /*
1424 * Register the platform device first. It is used as a parent for the
1425 * sub-devices below.
1426 */
Corentin Chary9129d142009-12-01 22:39:41 +01001427 result = asus_platform_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001428 if (result)
1429 goto fail_platform;
1430
1431 if (!acpi_video_backlight_support()) {
Corentin Chary9129d142009-12-01 22:39:41 +01001432 result = asus_backlight_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001433 if (result)
1434 goto fail_backlight;
1435 } else
1436 pr_info("Backlight controlled by ACPI video driver\n");
1437
Corentin Chary9129d142009-12-01 22:39:41 +01001438 result = asus_input_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001439 if (result)
1440 goto fail_input;
1441
Corentin Chary9129d142009-12-01 22:39:41 +01001442 result = asus_led_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001443 if (result)
1444 goto fail_led;
1445
1446 asus_device_present = true;
1447 return 0;
1448
1449fail_led:
Corentin Chary9129d142009-12-01 22:39:41 +01001450 asus_input_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001451fail_input:
Corentin Chary9129d142009-12-01 22:39:41 +01001452 asus_backlight_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001453fail_backlight:
Corentin Chary9129d142009-12-01 22:39:41 +01001454 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001455fail_platform:
Corentin Chary50a90c42009-11-30 21:55:12 +01001456 kfree(asus->name);
1457 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001458
1459 return result;
1460}
1461
1462static int asus_acpi_remove(struct acpi_device *device, int type)
1463{
Corentin Chary9129d142009-12-01 22:39:41 +01001464 struct asus_laptop *asus = acpi_driver_data(device);
1465
1466 asus_backlight_exit(asus);
1467 asus_led_exit(asus);
1468 asus_input_exit(asus);
1469 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001470
Corentin Chary50a90c42009-11-30 21:55:12 +01001471 kfree(asus->name);
1472 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001473 return 0;
1474}
1475
1476static const struct acpi_device_id asus_device_ids[] = {
1477 {"ATK0100", 0},
1478 {"ATK0101", 0},
1479 {"", 0},
1480};
1481MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1482
1483static struct acpi_driver asus_acpi_driver = {
Corentin Chary50a90c42009-11-30 21:55:12 +01001484 .name = ASUS_LAPTOP_NAME,
1485 .class = ASUS_LAPTOP_CLASS,
Corentin Chary600ad522009-11-30 21:42:42 +01001486 .owner = THIS_MODULE,
1487 .ids = asus_device_ids,
1488 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1489 .ops = {
1490 .add = asus_acpi_add,
1491 .remove = asus_acpi_remove,
1492 .notify = asus_acpi_notify,
1493 },
1494};
1495
Corentin Chary85091b72007-01-26 14:04:30 +01001496static int __init asus_laptop_init(void)
1497{
1498 int result;
1499
Corentin Chary600ad522009-11-30 21:42:42 +01001500 result = platform_driver_register(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001501 if (result < 0)
1502 return result;
1503
Corentin Chary600ad522009-11-30 21:42:42 +01001504 result = acpi_bus_register_driver(&asus_acpi_driver);
1505 if (result < 0)
1506 goto fail_acpi_driver;
1507 if (!asus_device_present) {
1508 result = -ENODEV;
1509 goto fail_no_device;
Corentin Chary85091b72007-01-26 14:04:30 +01001510 }
Len Brown8def05f2007-01-30 01:46:43 -05001511 return 0;
Corentin Chary85091b72007-01-26 14:04:30 +01001512
Corentin Chary600ad522009-11-30 21:42:42 +01001513fail_no_device:
1514 acpi_bus_unregister_driver(&asus_acpi_driver);
1515fail_acpi_driver:
1516 platform_driver_unregister(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001517 return result;
1518}
1519
Corentin Chary600ad522009-11-30 21:42:42 +01001520static void __exit asus_laptop_exit(void)
1521{
1522 acpi_bus_unregister_driver(&asus_acpi_driver);
1523 platform_driver_unregister(&platform_driver);
1524}
1525
Corentin Chary85091b72007-01-26 14:04:30 +01001526module_init(asus_laptop_init);
1527module_exit(asus_laptop_exit);