blob: 58a4864096d9f5c910b418a6fa341ce14abf0fa1 [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 Chary85091b72007-01-26 14:04:30 +010052
Corentin Chary91687cc2009-11-28 10:32:34 +010053#define ASUS_LAPTOP_VERSION "0.42"
Corentin Chary85091b72007-01-26 14:04:30 +010054
Corentin Chary50a90c42009-11-30 21:55:12 +010055#define ASUS_LAPTOP_NAME "Asus Laptop Support"
56#define ASUS_LAPTOP_CLASS "hotkey"
57#define ASUS_LAPTOP_DEVICE_NAME "Hotkey"
58#define ASUS_LAPTOP_FILE KBUILD_MODNAME
59#define ASUS_LAPTOP_PREFIX "\\_SB.ATKD."
Corentin Chary85091b72007-01-26 14:04:30 +010060
Corentin Chary2fcc23d2009-06-19 14:52:03 +020061
Corentin Charybe18cda2007-01-26 14:04:35 +010062/*
Corentin Chary6b7091e2007-01-26 14:04:45 +010063 * Some events we use, same for all Asus
64 */
Corentin Chary91687cc2009-11-28 10:32:34 +010065#define ATKD_BR_UP 0x10
66#define ATKD_BR_DOWN 0x20
67#define ATKD_LCD_ON 0x33
68#define ATKD_LCD_OFF 0x34
Corentin Chary6b7091e2007-01-26 14:04:45 +010069
70/*
Corentin Chary4564de12007-01-26 14:04:40 +010071 * Known bits returned by \_SB.ATKD.HWRS
Corentin Charybe18cda2007-01-26 14:04:35 +010072 */
Corentin Chary91687cc2009-11-28 10:32:34 +010073#define WL_HWRS 0x80
74#define BT_HWRS 0x100
Corentin Chary4564de12007-01-26 14:04:40 +010075
76/*
77 * Flags for hotk status
78 * WL_ON and BT_ON are also used for wireless_status()
79 */
Corentin Chary91687cc2009-11-28 10:32:34 +010080#define WL_ON 0x01 /* internal Wifi */
81#define BT_ON 0x02 /* internal Bluetooth */
82#define MLED_ON 0x04 /* mail LED */
83#define TLED_ON 0x08 /* touchpad LED */
84#define RLED_ON 0x10 /* Record LED */
85#define PLED_ON 0x20 /* Phone LED */
86#define GLED_ON 0x40 /* Gaming LED */
87#define LCD_ON 0x80 /* LCD backlight */
88#define GPS_ON 0x100 /* GPS */
89#define KEY_ON 0x200 /* Keyboard backlight */
Corentin Chary85091b72007-01-26 14:04:30 +010090
91MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
Corentin Chary50a90c42009-11-30 21:55:12 +010092MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
Corentin Chary85091b72007-01-26 14:04:30 +010093MODULE_LICENSE("GPL");
94
Corentin Charybe966662009-08-29 10:28:29 +020095/*
96 * WAPF defines the behavior of the Fn+Fx wlan key
Corentin Chary185e5af2007-03-11 10:27:33 +010097 * The significance of values is yet to be found, but
98 * most of the time:
99 * 0x0 will do nothing
100 * 0x1 will allow to control the device with Fn+Fx key.
101 * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
102 * 0x5 like 0x1 or 0x4
103 * So, if something doesn't work as you want, just try other values =)
104 */
105static uint wapf = 1;
106module_param(wapf, uint, 0644);
107MODULE_PARM_DESC(wapf, "WAPF value");
108
Corentin Chary0e875f42010-01-10 20:49:26 +0100109static uint wireless_status = 1;
110static uint bluetooth_status = 1;
111
112module_param(wireless_status, uint, 0644);
113MODULE_PARM_DESC(wireless_status, "Set the wireless status on boot "
114 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
115 "default is 1");
116
117module_param(bluetooth_status, uint, 0644);
118MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
119 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
120 "default is 1");
121
Corentin Chary85091b72007-01-26 14:04:30 +0100122#define ASUS_HANDLE(object, paths...) \
123 static acpi_handle object##_handle = NULL; \
124 static char *object##_paths[] = { paths }
125
Corentin Charybe18cda2007-01-26 14:04:35 +0100126/* LED */
Corentin Chary50a90c42009-11-30 21:55:12 +0100127ASUS_HANDLE(mled_set, ASUS_LAPTOP_PREFIX "MLED");
128ASUS_HANDLE(tled_set, ASUS_LAPTOP_PREFIX "TLED");
129ASUS_HANDLE(rled_set, ASUS_LAPTOP_PREFIX "RLED"); /* W1JC */
130ASUS_HANDLE(pled_set, ASUS_LAPTOP_PREFIX "PLED"); /* A7J */
131ASUS_HANDLE(gled_set, ASUS_LAPTOP_PREFIX "GLED"); /* G1, G2 (probably) */
Corentin Charybe18cda2007-01-26 14:04:35 +0100132
Corentin Chary722ad972007-01-26 14:04:55 +0100133/* LEDD */
Corentin Chary50a90c42009-11-30 21:55:12 +0100134ASUS_HANDLE(ledd_set, ASUS_LAPTOP_PREFIX "SLCM");
Corentin Chary722ad972007-01-26 14:04:55 +0100135
Corentin Charybe966662009-08-29 10:28:29 +0200136/*
137 * Bluetooth and WLAN
Corentin Chary4564de12007-01-26 14:04:40 +0100138 * WLED and BLED are not handled like other XLED, because in some dsdt
139 * they also control the WLAN/Bluetooth device.
140 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100141ASUS_HANDLE(wl_switch, ASUS_LAPTOP_PREFIX "WLED");
142ASUS_HANDLE(bt_switch, ASUS_LAPTOP_PREFIX "BLED");
143ASUS_HANDLE(wireless_status, ASUS_LAPTOP_PREFIX "RSTS"); /* All new models */
Corentin Chary4564de12007-01-26 14:04:40 +0100144
Corentin Chary6b7091e2007-01-26 14:04:45 +0100145/* Brightness */
Corentin Chary50a90c42009-11-30 21:55:12 +0100146ASUS_HANDLE(brightness_set, ASUS_LAPTOP_PREFIX "SPLV");
147ASUS_HANDLE(brightness_get, ASUS_LAPTOP_PREFIX "GPLV");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100148
149/* Backlight */
Len Brown8def05f2007-01-30 01:46:43 -0500150ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
151 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
152 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
153 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
154 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
Torsten Krah4d0b856e2008-10-17 09:47:57 +0200155 "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
Len Brown8def05f2007-01-30 01:46:43 -0500156 "\\_SB.PCI0.PX40.Q10", /* S1x */
157 "\\Q10"); /* A2x, L2D, L3D, M2E */
Corentin Chary6b7091e2007-01-26 14:04:45 +0100158
Corentin Chary78127b42007-01-26 14:04:49 +0100159/* Display */
Corentin Chary50a90c42009-11-30 21:55:12 +0100160ASUS_HANDLE(display_set, ASUS_LAPTOP_PREFIX "SDSP");
Corentin Charybe966662009-08-29 10:28:29 +0200161ASUS_HANDLE(display_get,
162 /* A6B, A6K A6R A7D F3JM L4R M6R A3G M6A M6V VX-1 V6J V6V W3Z */
163 "\\_SB.PCI0.P0P1.VGA.GETD",
164 /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V S5A M5A z33A W1Jc W2V G1 */
165 "\\_SB.PCI0.P0P2.VGA.GETD",
166 /* A6V A6Q */
167 "\\_SB.PCI0.P0P3.VGA.GETD",
168 /* A6T, A6M */
169 "\\_SB.PCI0.P0PA.VGA.GETD",
170 /* L3C */
171 "\\_SB.PCI0.PCI1.VGAC.NMAP",
172 /* Z96F */
173 "\\_SB.PCI0.VGA.GETD",
174 /* A2D */
175 "\\ACTD",
176 /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
177 "\\ADVG",
178 /* P30 */
179 "\\DNXT",
180 /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
181 "\\INFB",
182 /* A3F A6F A3N A3L M6N W3N W6A */
183 "\\SSTE");
Corentin Chary78127b42007-01-26 14:04:49 +0100184
Corentin Chary50a90c42009-11-30 21:55:12 +0100185ASUS_HANDLE(ls_switch, ASUS_LAPTOP_PREFIX "ALSC"); /* Z71A Z71V */
186ASUS_HANDLE(ls_level, ASUS_LAPTOP_PREFIX "ALSL"); /* Z71A Z71V */
Corentin Chary8b857352007-01-26 14:04:58 +0100187
Corentin Charye539c2f2007-05-06 14:47:06 +0200188/* GPS */
189/* R2H use different handle for GPS on/off */
Corentin Chary50a90c42009-11-30 21:55:12 +0100190ASUS_HANDLE(gps_on, ASUS_LAPTOP_PREFIX "SDON"); /* R2H */
191ASUS_HANDLE(gps_off, ASUS_LAPTOP_PREFIX "SDOF"); /* R2H */
192ASUS_HANDLE(gps_status, ASUS_LAPTOP_PREFIX "GPST");
Corentin Charye539c2f2007-05-06 14:47:06 +0200193
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000194/* Keyboard light */
Corentin Chary50a90c42009-11-30 21:55:12 +0100195ASUS_HANDLE(kled_set, ASUS_LAPTOP_PREFIX "SLKB");
196ASUS_HANDLE(kled_get, ASUS_LAPTOP_PREFIX "GLKB");
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000197
Corentin Chary85091b72007-01-26 14:04:30 +0100198/*
Corentin Chary9129d142009-12-01 22:39:41 +0100199 * Define a specific led structure to keep the main structure clean
200 */
201#define ASUS_DEFINE_LED(object) \
202 int object##_wk; \
203 struct work_struct object##_work; \
204 struct led_classdev object;
205
206
207#define led_to_asus(led_cdev, led) \
208 container_of(container_of(led_cdev, struct asus_laptop_leds, \
209 led), \
210 struct asus_laptop, leds)
211#define work_to_asus(work, led) \
212 container_of(container_of(work, struct asus_laptop_leds, \
213 led##_work), \
214 struct asus_laptop, leds)
215
216struct asus_laptop_leds {
217 ASUS_DEFINE_LED(mled)
218 ASUS_DEFINE_LED(tled)
219 ASUS_DEFINE_LED(rled)
220 ASUS_DEFINE_LED(pled)
221 ASUS_DEFINE_LED(gled)
222 ASUS_DEFINE_LED(kled)
223 struct workqueue_struct *workqueue;
224};
225
226/*
Corentin Chary85091b72007-01-26 14:04:30 +0100227 * This is the main structure, we can use it to store anything interesting
228 * about the hotk device
229 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100230struct asus_laptop {
Corentin Charybe966662009-08-29 10:28:29 +0200231 char *name; /* laptop name */
Corentin Chary600ad522009-11-30 21:42:42 +0100232
Corentin Chary7c247642009-11-30 22:13:54 +0100233 struct acpi_table_header *dsdt_info;
Corentin Chary600ad522009-11-30 21:42:42 +0100234 struct platform_device *platform_device;
Corentin Chary7c247642009-11-30 22:13:54 +0100235 struct acpi_device *device; /* the device we are in */
236 struct backlight_device *backlight_device;
Corentin Chary9129d142009-12-01 22:39:41 +0100237
Corentin Chary7c247642009-11-30 22:13:54 +0100238 struct input_dev *inputdev;
Corentin Chary9129d142009-12-01 22:39:41 +0100239 struct key_entry *keymap;
240
241 struct asus_laptop_leds leds;
Corentin Chary7c247642009-11-30 22:13:54 +0100242
Corentin Charybe966662009-08-29 10:28:29 +0200243 acpi_handle handle; /* the handle of the hotk device */
244 char status; /* status of the hotk, for LEDs, ... */
245 u32 ledd_status; /* status of the LED display */
246 u8 light_level; /* light sensor level */
247 u8 light_switch; /* light sensor switch value */
248 u16 event_count[128]; /* count for each event TODO make this better */
Corentin Chary034ce902009-01-20 16:17:43 +0100249 u16 *keycode_map;
Corentin Chary85091b72007-01-26 14:04:30 +0100250};
251
Corentin Chary6b7091e2007-01-26 14:04:45 +0100252/*
253 * The backlight class declaration
254 */
255static int read_brightness(struct backlight_device *bd);
256static int update_bl_status(struct backlight_device *bd);
Richard Purdie599a52d2007-02-10 23:07:48 +0000257static struct backlight_ops asusbl_ops = {
Len Brown8def05f2007-01-30 01:46:43 -0500258 .get_brightness = read_brightness,
259 .update_status = update_bl_status,
Corentin Chary6b7091e2007-01-26 14:04:45 +0100260};
261
Corentin Chary977c3282009-08-28 12:56:49 +0000262#define ASUS_LED(object, ledname, max) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100263 static void object##_led_set(struct led_classdev *led_cdev, \
264 enum led_brightness value); \
Corentin Charyabfa57e2009-08-28 12:56:47 +0000265 static enum led_brightness object##_led_get( \
266 struct led_classdev *led_cdev); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100267 static void object##_led_update(struct work_struct *ignored); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100268 static struct led_classdev object##_led = { \
Richard Purdie6c152be2007-10-31 15:00:07 +0100269 .name = "asus::" ledname, \
Corentin Charybe18cda2007-01-26 14:04:35 +0100270 .brightness_set = object##_led_set, \
Corentin Charyabfa57e2009-08-28 12:56:47 +0000271 .brightness_get = object##_led_get, \
Corentin Chary977c3282009-08-28 12:56:49 +0000272 .max_brightness = max \
Corentin Charybe18cda2007-01-26 14:04:35 +0100273 }
274
Corentin Chary977c3282009-08-28 12:56:49 +0000275ASUS_LED(mled, "mail", 1);
276ASUS_LED(tled, "touchpad", 1);
277ASUS_LED(rled, "record", 1);
278ASUS_LED(pled, "phone", 1);
279ASUS_LED(gled, "gaming", 1);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000280ASUS_LED(kled, "kbd_backlight", 3);
Corentin Charybe18cda2007-01-26 14:04:35 +0100281
Corentin Chary034ce902009-01-20 16:17:43 +0100282struct key_entry {
283 char type;
284 u8 code;
285 u16 keycode;
286};
287
288enum { KE_KEY, KE_END };
289
Corentin Chary9129d142009-12-01 22:39:41 +0100290static const struct key_entry asus_keymap[] = {
Ike Panhc14f8af32009-12-03 07:45:11 +0000291 {KE_KEY, 0x02, KEY_SCREENLOCK},
292 {KE_KEY, 0x05, KEY_WLAN},
293 {KE_KEY, 0x08, KEY_F13},
294 {KE_KEY, 0x17, KEY_ZOOM},
295 {KE_KEY, 0x1f, KEY_BATTERY},
Corentin Chary034ce902009-01-20 16:17:43 +0100296 {KE_KEY, 0x30, KEY_VOLUMEUP},
297 {KE_KEY, 0x31, KEY_VOLUMEDOWN},
298 {KE_KEY, 0x32, KEY_MUTE},
299 {KE_KEY, 0x33, KEY_SWITCHVIDEOMODE},
300 {KE_KEY, 0x34, KEY_SWITCHVIDEOMODE},
301 {KE_KEY, 0x40, KEY_PREVIOUSSONG},
302 {KE_KEY, 0x41, KEY_NEXTSONG},
Corentin Chary309f5fb2009-04-27 09:23:42 +0200303 {KE_KEY, 0x43, KEY_STOPCD},
Corentin Chary034ce902009-01-20 16:17:43 +0100304 {KE_KEY, 0x45, KEY_PLAYPAUSE},
Corentin Chary4644d0e2009-08-28 12:56:52 +0000305 {KE_KEY, 0x4c, KEY_MEDIA},
Corentin Chary034ce902009-01-20 16:17:43 +0100306 {KE_KEY, 0x50, KEY_EMAIL},
307 {KE_KEY, 0x51, KEY_WWW},
Corentin Chary0aa20f72009-08-28 12:56:53 +0000308 {KE_KEY, 0x55, KEY_CALC},
Corentin Chary309f5fb2009-04-27 09:23:42 +0200309 {KE_KEY, 0x5C, KEY_SCREENLOCK}, /* Screenlock */
Corentin Chary034ce902009-01-20 16:17:43 +0100310 {KE_KEY, 0x5D, KEY_WLAN},
Corentin Charyf6413752009-08-28 12:56:48 +0000311 {KE_KEY, 0x5E, KEY_WLAN},
312 {KE_KEY, 0x5F, KEY_WLAN},
313 {KE_KEY, 0x60, KEY_SWITCHVIDEOMODE},
Corentin Chary034ce902009-01-20 16:17:43 +0100314 {KE_KEY, 0x61, KEY_SWITCHVIDEOMODE},
Ike Panhc14f8af32009-12-03 07:45:11 +0000315 {KE_KEY, 0x62, KEY_SWITCHVIDEOMODE},
316 {KE_KEY, 0x63, KEY_SWITCHVIDEOMODE},
Corentin Chary16851f92009-12-03 07:45:10 +0000317 {KE_KEY, 0x6B, KEY_F13}, /* Lock Touchpad */
Corentin Chary034ce902009-01-20 16:17:43 +0100318 {KE_KEY, 0x82, KEY_CAMERA},
Corentin Chary1f0233e2009-12-03 07:45:12 +0000319 {KE_KEY, 0x88, KEY_WLAN },
Corentin Chary309f5fb2009-04-27 09:23:42 +0200320 {KE_KEY, 0x8A, KEY_PROG1},
Corentin Chary034ce902009-01-20 16:17:43 +0100321 {KE_KEY, 0x95, KEY_MEDIA},
322 {KE_KEY, 0x99, KEY_PHONE},
Corentin Charydc795262009-08-28 12:56:51 +0000323 {KE_KEY, 0xc4, KEY_KBDILLUMUP},
324 {KE_KEY, 0xc5, KEY_KBDILLUMDOWN},
Corentin Chary034ce902009-01-20 16:17:43 +0100325 {KE_END, 0},
326};
327
Corentin Chary85091b72007-01-26 14:04:30 +0100328/*
329 * This function evaluates an ACPI method, given an int as parameter, the
330 * method is searched within the scope of the handle, can be NULL. The output
331 * of the method is written is output, which can also be NULL
332 *
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100333 * returns 0 if write is successful, -1 else.
Corentin Chary85091b72007-01-26 14:04:30 +0100334 */
Corentin Charyd8c67322009-11-28 10:27:51 +0100335static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
336 struct acpi_buffer *output)
Corentin Chary85091b72007-01-26 14:04:30 +0100337{
Corentin Charybe966662009-08-29 10:28:29 +0200338 struct acpi_object_list params; /* list of input parameters (an int) */
339 union acpi_object in_obj; /* the only param we use */
Corentin Chary85091b72007-01-26 14:04:30 +0100340 acpi_status status;
341
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100342 if (!handle)
343 return 0;
344
Corentin Chary85091b72007-01-26 14:04:30 +0100345 params.count = 1;
346 params.pointer = &in_obj;
347 in_obj.type = ACPI_TYPE_INTEGER;
348 in_obj.integer.value = val;
349
350 status = acpi_evaluate_object(handle, (char *)method, &params, output);
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100351 if (status == AE_OK)
352 return 0;
353 else
354 return -1;
Corentin Chary85091b72007-01-26 14:04:30 +0100355}
356
Corentin Charyd8c67322009-11-28 10:27:51 +0100357static int write_acpi_int(acpi_handle handle, const char *method, int val)
358{
359 return write_acpi_int_ret(handle, method, val, NULL);
360}
361
Corentin Chary9129d142009-12-01 22:39:41 +0100362static int read_wireless_status(struct asus_laptop *asus, int mask)
Len Brown8def05f2007-01-30 01:46:43 -0500363{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400364 unsigned long long status;
Corentin Chary9a816852007-03-11 10:25:38 +0100365 acpi_status rv = AE_OK;
Corentin Chary4564de12007-01-26 14:04:40 +0100366
367 if (!wireless_status_handle)
Corentin Chary50a90c42009-11-30 21:55:12 +0100368 return (asus->status & mask) ? 1 : 0;
Corentin Chary4564de12007-01-26 14:04:40 +0100369
Corentin Chary9a816852007-03-11 10:25:38 +0100370 rv = acpi_evaluate_integer(wireless_status_handle, NULL, NULL, &status);
371 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200372 pr_warning("Error reading Wireless status\n");
Corentin Chary9a816852007-03-11 10:25:38 +0100373 else
374 return (status & mask) ? 1 : 0;
Corentin Chary4564de12007-01-26 14:04:40 +0100375
Corentin Chary50a90c42009-11-30 21:55:12 +0100376 return (asus->status & mask) ? 1 : 0;
Corentin Chary4564de12007-01-26 14:04:40 +0100377}
378
Corentin Chary9129d142009-12-01 22:39:41 +0100379static int read_gps_status(struct asus_laptop *asus)
Corentin Charyf3985322007-05-06 14:48:22 +0200380{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400381 unsigned long long status;
Corentin Charye539c2f2007-05-06 14:47:06 +0200382 acpi_status rv = AE_OK;
383
384 rv = acpi_evaluate_integer(gps_status_handle, NULL, NULL, &status);
385 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200386 pr_warning("Error reading GPS status\n");
Corentin Charye539c2f2007-05-06 14:47:06 +0200387 else
Corentin Charyf3985322007-05-06 14:48:22 +0200388 return status ? 1 : 0;
Corentin Charye539c2f2007-05-06 14:47:06 +0200389
Corentin Chary50a90c42009-11-30 21:55:12 +0100390 return (asus->status & GPS_ON) ? 1 : 0;
Corentin Charye539c2f2007-05-06 14:47:06 +0200391}
392
Corentin Charybe18cda2007-01-26 14:04:35 +0100393/* Generic LED functions */
Corentin Chary9129d142009-12-01 22:39:41 +0100394static int read_status(struct asus_laptop *asus, int mask)
Corentin Charybe18cda2007-01-26 14:04:35 +0100395{
Corentin Chary4564de12007-01-26 14:04:40 +0100396 /* There is a special method for both wireless devices */
397 if (mask == BT_ON || mask == WL_ON)
Corentin Chary9129d142009-12-01 22:39:41 +0100398 return read_wireless_status(asus, mask);
Corentin Charyf3985322007-05-06 14:48:22 +0200399 else if (mask == GPS_ON)
Corentin Chary9129d142009-12-01 22:39:41 +0100400 return read_gps_status(asus);
Corentin Chary4564de12007-01-26 14:04:40 +0100401
Corentin Chary50a90c42009-11-30 21:55:12 +0100402 return (asus->status & mask) ? 1 : 0;
Corentin Charybe18cda2007-01-26 14:04:35 +0100403}
404
Corentin Chary9129d142009-12-01 22:39:41 +0100405static void write_status(struct asus_laptop *asus, acpi_handle handle,
406 int out, int mask)
Corentin Charybe18cda2007-01-26 14:04:35 +0100407{
Corentin Chary50a90c42009-11-30 21:55:12 +0100408 asus->status = (out) ? (asus->status | mask) : (asus->status & ~mask);
Corentin Charybe18cda2007-01-26 14:04:35 +0100409
Corentin Chary935ffee2007-03-11 10:26:12 +0100410 switch (mask) {
411 case MLED_ON:
Roel Kluine1af14e2008-02-02 21:07:38 +0100412 out = !(out & 0x1);
Corentin Chary935ffee2007-03-11 10:26:12 +0100413 break;
Corentin Charyfdd8d082007-03-11 10:26:48 +0100414 case GLED_ON:
415 out = (out & 0x1) + 1;
416 break;
Corentin Charye539c2f2007-05-06 14:47:06 +0200417 case GPS_ON:
418 handle = (out) ? gps_on_handle : gps_off_handle;
419 out = 0x02;
420 break;
Corentin Chary935ffee2007-03-11 10:26:12 +0100421 default:
422 out &= 0x1;
423 break;
424 }
Corentin Charybe18cda2007-01-26 14:04:35 +0100425
Corentin Charyd8c67322009-11-28 10:27:51 +0100426 if (write_acpi_int(handle, NULL, out))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200427 pr_warning(" write failed %x\n", mask);
Corentin Charybe18cda2007-01-26 14:04:35 +0100428}
429
430/* /sys/class/led handlers */
Corentin Chary935ffee2007-03-11 10:26:12 +0100431#define ASUS_LED_HANDLER(object, mask) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100432 static void object##_led_set(struct led_classdev *led_cdev, \
433 enum led_brightness value) \
434 { \
Corentin Chary9129d142009-12-01 22:39:41 +0100435 struct asus_laptop *asus = \
436 led_to_asus(led_cdev, object); \
437 \
438 asus->leds.object##_wk = (value > 0) ? 1 : 0; \
439 queue_work(asus->leds.workqueue, \
440 &asus->leds.object##_work); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100441 } \
Corentin Chary9129d142009-12-01 22:39:41 +0100442 static void object##_led_update(struct work_struct *work) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100443 { \
Corentin Chary9129d142009-12-01 22:39:41 +0100444 struct asus_laptop *asus = work_to_asus(work, object); \
445 \
446 int value = asus->leds.object##_wk; \
447 write_status(asus, object##_set_handle, value, (mask)); \
Corentin Charyabfa57e2009-08-28 12:56:47 +0000448 } \
449 static enum led_brightness object##_led_get( \
450 struct led_classdev *led_cdev) \
451 { \
452 return led_cdev->brightness; \
Corentin Charybe18cda2007-01-26 14:04:35 +0100453 }
454
Corentin Chary935ffee2007-03-11 10:26:12 +0100455ASUS_LED_HANDLER(mled, MLED_ON);
456ASUS_LED_HANDLER(pled, PLED_ON);
457ASUS_LED_HANDLER(rled, RLED_ON);
458ASUS_LED_HANDLER(tled, TLED_ON);
Corentin Charyfdd8d082007-03-11 10:26:48 +0100459ASUS_LED_HANDLER(gled, GLED_ON);
Corentin Charybe18cda2007-01-26 14:04:35 +0100460
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000461/*
462 * Keyboard backlight
463 */
464static int get_kled_lvl(void)
465{
466 unsigned long long kblv;
467 struct acpi_object_list params;
468 union acpi_object in_obj;
469 acpi_status rv;
470
471 params.count = 1;
472 params.pointer = &in_obj;
473 in_obj.type = ACPI_TYPE_INTEGER;
474 in_obj.integer.value = 2;
475
476 rv = acpi_evaluate_integer(kled_get_handle, NULL, &params, &kblv);
477 if (ACPI_FAILURE(rv)) {
478 pr_warning("Error reading kled level\n");
479 return 0;
480 }
481 return kblv;
482}
483
Corentin Chary9129d142009-12-01 22:39:41 +0100484static int set_kled_lvl(struct asus_laptop *asus, int kblv)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000485{
486 if (kblv > 0)
487 kblv = (1 << 7) | (kblv & 0x7F);
488 else
489 kblv = 0;
490
Corentin Charyd8c67322009-11-28 10:27:51 +0100491 if (write_acpi_int(kled_set_handle, NULL, kblv)) {
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000492 pr_warning("Keyboard LED display write failed\n");
493 return -EINVAL;
494 }
495 return 0;
496}
497
498static void kled_led_set(struct led_classdev *led_cdev,
499 enum led_brightness value)
500{
Corentin Chary9129d142009-12-01 22:39:41 +0100501 struct asus_laptop *asus = led_to_asus(led_cdev, kled);
502
503 asus->leds.kled_wk = value;
504 queue_work(asus->leds.workqueue, &asus->leds.kled_work);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000505}
506
Corentin Chary9129d142009-12-01 22:39:41 +0100507static void kled_led_update(struct work_struct *work)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000508{
Corentin Chary9129d142009-12-01 22:39:41 +0100509 struct asus_laptop *asus = work_to_asus(work, kled);
510
511 set_kled_lvl(asus, asus->leds.kled_wk);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000512}
513
514static enum led_brightness kled_led_get(struct led_classdev *led_cdev)
515{
516 return get_kled_lvl();
517}
518
Corentin Chary9129d142009-12-01 22:39:41 +0100519static int get_lcd_state(struct asus_laptop *asus)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100520{
Corentin Chary9129d142009-12-01 22:39:41 +0100521 return read_status(asus, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100522}
523
Corentin Chary9129d142009-12-01 22:39:41 +0100524static int set_lcd_state(struct asus_laptop *asus, int value)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100525{
526 int lcd = 0;
527 acpi_status status = 0;
528
529 lcd = value ? 1 : 0;
530
Corentin Chary9129d142009-12-01 22:39:41 +0100531 if (lcd == get_lcd_state(asus))
Corentin Chary6b7091e2007-01-26 14:04:45 +0100532 return 0;
533
Len Brown8def05f2007-01-30 01:46:43 -0500534 if (lcd_switch_handle) {
Corentin Chary6b7091e2007-01-26 14:04:45 +0100535 status = acpi_evaluate_object(lcd_switch_handle,
536 NULL, NULL, NULL);
537
538 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200539 pr_warning("Error switching LCD\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100540 }
541
Corentin Chary9129d142009-12-01 22:39:41 +0100542 write_status(asus, NULL, lcd, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100543 return 0;
544}
545
Corentin Chary9129d142009-12-01 22:39:41 +0100546static void lcd_blank(struct asus_laptop *asus, int blank)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100547{
Corentin Chary7c247642009-11-30 22:13:54 +0100548 struct backlight_device *bd = asus->backlight_device;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100549
Len Brown8def05f2007-01-30 01:46:43 -0500550 if (bd) {
Richard Purdie599a52d2007-02-10 23:07:48 +0000551 bd->props.power = blank;
Richard Purdie28ee0862007-02-08 22:25:09 +0000552 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100553 }
554}
555
556static int read_brightness(struct backlight_device *bd)
557{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400558 unsigned long long value;
Corentin Chary9a816852007-03-11 10:25:38 +0100559 acpi_status rv = AE_OK;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100560
Corentin Chary9a816852007-03-11 10:25:38 +0100561 rv = acpi_evaluate_integer(brightness_get_handle, NULL, NULL, &value);
562 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200563 pr_warning("Error reading brightness\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100564
565 return value;
566}
567
568static int set_brightness(struct backlight_device *bd, int value)
569{
Corentin Charyd8c67322009-11-28 10:27:51 +0100570 if (write_acpi_int(brightness_set_handle, NULL, value)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200571 pr_warning("Error changing brightness\n");
Corentin Charye5b50f62009-11-28 10:19:55 +0100572 return -EIO;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100573 }
Corentin Charye5b50f62009-11-28 10:19:55 +0100574 return 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100575}
576
577static int update_bl_status(struct backlight_device *bd)
578{
Corentin Chary9129d142009-12-01 22:39:41 +0100579 struct asus_laptop *asus = bl_get_data(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100580 int rv;
Richard Purdie599a52d2007-02-10 23:07:48 +0000581 int value = bd->props.brightness;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100582
583 rv = set_brightness(bd, value);
Len Brown8def05f2007-01-30 01:46:43 -0500584 if (rv)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100585 return rv;
586
Richard Purdie599a52d2007-02-10 23:07:48 +0000587 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
Corentin Chary9129d142009-12-01 22:39:41 +0100588 return set_lcd_state(asus, value);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100589}
590
Corentin Chary85091b72007-01-26 14:04:30 +0100591/*
592 * Platform device handlers
593 */
594
595/*
596 * We write our info in page, we begin at offset off and cannot write more
597 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
598 * number of bytes written in page
599 */
600static ssize_t show_infos(struct device *dev,
Len Brown8def05f2007-01-30 01:46:43 -0500601 struct device_attribute *attr, char *page)
Corentin Chary85091b72007-01-26 14:04:30 +0100602{
Corentin Chary9129d142009-12-01 22:39:41 +0100603 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary85091b72007-01-26 14:04:30 +0100604 int len = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400605 unsigned long long temp;
Corentin Charybe966662009-08-29 10:28:29 +0200606 char buf[16]; /* enough for all info */
Corentin Chary9a816852007-03-11 10:25:38 +0100607 acpi_status rv = AE_OK;
608
Corentin Chary85091b72007-01-26 14:04:30 +0100609 /*
610 * We use the easy way, we don't care of off and count, so we don't set eof
611 * to 1
612 */
613
Corentin Chary50a90c42009-11-30 21:55:12 +0100614 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
615 len += sprintf(page + len, "Model reference : %s\n", asus->name);
Corentin Chary85091b72007-01-26 14:04:30 +0100616 /*
617 * The SFUN method probably allows the original driver to get the list
618 * of features supported by a given model. For now, 0x0100 or 0x0800
619 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
620 * The significance of others is yet to be found.
621 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100622 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100623 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000624 len += sprintf(page + len, "SFUN value : %#x\n",
625 (uint) temp);
626 /*
627 * The HWRS method return informations about the hardware.
628 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
629 * The significance of others is yet to be found.
630 * If we don't find the method, we assume the device are present.
631 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100632 rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
Corentin Chary1d4a3802009-08-28 12:56:46 +0000633 if (!ACPI_FAILURE(rv))
634 len += sprintf(page + len, "HRWS value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100635 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100636 /*
637 * Another value for userspace: the ASYM method returns 0x02 for
638 * battery low and 0x04 for battery critical, its readings tend to be
639 * more accurate than those provided by _BST.
640 * Note: since not all the laptops provide this method, errors are
641 * silently ignored.
642 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100643 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100644 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000645 len += sprintf(page + len, "ASYM value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100646 (uint) temp);
Corentin Chary7c247642009-11-30 22:13:54 +0100647 if (asus->dsdt_info) {
648 snprintf(buf, 16, "%d", asus->dsdt_info->length);
Corentin Chary85091b72007-01-26 14:04:30 +0100649 len += sprintf(page + len, "DSDT length : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100650 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
Corentin Chary85091b72007-01-26 14:04:30 +0100651 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100652 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100653 len += sprintf(page + len, "DSDT revision : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100654 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100655 len += sprintf(page + len, "OEM id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100656 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100657 len += sprintf(page + len, "OEM table id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100658 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100659 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100660 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100661 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100662 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100663 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
664 }
665
666 return len;
667}
668
669static int parse_arg(const char *buf, unsigned long count, int *val)
670{
671 if (!count)
672 return 0;
673 if (count > 31)
674 return -EINVAL;
675 if (sscanf(buf, "%i", val) != 1)
676 return -EINVAL;
677 return count;
678}
679
Corentin Chary9129d142009-12-01 22:39:41 +0100680static ssize_t store_status(struct asus_laptop *asus,
681 const char *buf, size_t count,
Corentin Chary935ffee2007-03-11 10:26:12 +0100682 acpi_handle handle, int mask)
Corentin Chary4564de12007-01-26 14:04:40 +0100683{
684 int rv, value;
685 int out = 0;
686
687 rv = parse_arg(buf, count, &value);
688 if (rv > 0)
689 out = value ? 1 : 0;
690
Corentin Chary9129d142009-12-01 22:39:41 +0100691 write_status(asus, handle, out, mask);
Corentin Chary4564de12007-01-26 14:04:40 +0100692
693 return rv;
694}
695
696/*
Corentin Chary722ad972007-01-26 14:04:55 +0100697 * LEDD display
698 */
699static ssize_t show_ledd(struct device *dev,
700 struct device_attribute *attr, char *buf)
701{
Corentin Chary9129d142009-12-01 22:39:41 +0100702 struct asus_laptop *asus = dev_get_drvdata(dev);
703
Corentin Chary50a90c42009-11-30 21:55:12 +0100704 return sprintf(buf, "0x%08x\n", asus->ledd_status);
Corentin Chary722ad972007-01-26 14:04:55 +0100705}
706
707static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
708 const char *buf, size_t count)
709{
Corentin Chary9129d142009-12-01 22:39:41 +0100710 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary722ad972007-01-26 14:04:55 +0100711 int rv, value;
712
713 rv = parse_arg(buf, count, &value);
714 if (rv > 0) {
Corentin Charyd8c67322009-11-28 10:27:51 +0100715 if (write_acpi_int(ledd_set_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200716 pr_warning("LED display write failed\n");
Corentin Chary722ad972007-01-26 14:04:55 +0100717 else
Corentin Chary50a90c42009-11-30 21:55:12 +0100718 asus->ledd_status = (u32) value;
Corentin Chary722ad972007-01-26 14:04:55 +0100719 }
720 return rv;
721}
722
723/*
Corentin Chary4564de12007-01-26 14:04:40 +0100724 * WLAN
725 */
726static ssize_t show_wlan(struct device *dev,
727 struct device_attribute *attr, char *buf)
728{
Corentin Chary9129d142009-12-01 22:39:41 +0100729 struct asus_laptop *asus = dev_get_drvdata(dev);
730
731 return sprintf(buf, "%d\n", read_status(asus, WL_ON));
Corentin Chary4564de12007-01-26 14:04:40 +0100732}
733
734static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
735 const char *buf, size_t count)
736{
Corentin Chary9129d142009-12-01 22:39:41 +0100737 struct asus_laptop *asus = dev_get_drvdata(dev);
738
739 return store_status(asus, buf, count, wl_switch_handle, WL_ON);
Corentin Chary4564de12007-01-26 14:04:40 +0100740}
741
742/*
743 * Bluetooth
744 */
745static ssize_t show_bluetooth(struct device *dev,
746 struct device_attribute *attr, char *buf)
747{
Corentin Chary9129d142009-12-01 22:39:41 +0100748 struct asus_laptop *asus = dev_get_drvdata(dev);
749
750 return sprintf(buf, "%d\n", read_status(asus, BT_ON));
Corentin Chary4564de12007-01-26 14:04:40 +0100751}
752
Len Brown8def05f2007-01-30 01:46:43 -0500753static ssize_t store_bluetooth(struct device *dev,
754 struct device_attribute *attr, const char *buf,
755 size_t count)
Corentin Chary4564de12007-01-26 14:04:40 +0100756{
Corentin Chary9129d142009-12-01 22:39:41 +0100757 struct asus_laptop *asus = dev_get_drvdata(dev);
758
759 return store_status(asus, buf, count, bt_switch_handle, BT_ON);
Corentin Chary4564de12007-01-26 14:04:40 +0100760}
761
Corentin Chary78127b42007-01-26 14:04:49 +0100762/*
763 * Display
764 */
Corentin Chary9129d142009-12-01 22:39:41 +0100765static void set_display(struct asus_laptop *asus, int value)
Corentin Chary78127b42007-01-26 14:04:49 +0100766{
767 /* no sanity check needed for now */
Corentin Charyd8c67322009-11-28 10:27:51 +0100768 if (write_acpi_int(display_set_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200769 pr_warning("Error setting display\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100770 return;
771}
772
Corentin Chary9129d142009-12-01 22:39:41 +0100773static int read_display(struct asus_laptop *asus)
Corentin Chary78127b42007-01-26 14:04:49 +0100774{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400775 unsigned long long value = 0;
Corentin Chary9a816852007-03-11 10:25:38 +0100776 acpi_status rv = AE_OK;
Corentin Chary78127b42007-01-26 14:04:49 +0100777
Corentin Charybe966662009-08-29 10:28:29 +0200778 /*
779 * In most of the case, we know how to set the display, but sometime
780 * we can't read it
781 */
Len Brown8def05f2007-01-30 01:46:43 -0500782 if (display_get_handle) {
Corentin Chary9a816852007-03-11 10:25:38 +0100783 rv = acpi_evaluate_integer(display_get_handle, NULL,
784 NULL, &value);
785 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200786 pr_warning("Error reading display status\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100787 }
788
789 value &= 0x0F; /* needed for some models, shouldn't hurt others */
790
791 return value;
792}
Len Brown8def05f2007-01-30 01:46:43 -0500793
Corentin Chary78127b42007-01-26 14:04:49 +0100794/*
795 * Now, *this* one could be more user-friendly, but so far, no-one has
796 * complained. The significance of bits is the same as in store_disp()
797 */
798static ssize_t show_disp(struct device *dev,
799 struct device_attribute *attr, char *buf)
800{
Corentin Chary9129d142009-12-01 22:39:41 +0100801 struct asus_laptop *asus = dev_get_drvdata(dev);
802
803 return sprintf(buf, "%d\n", read_display(asus));
Corentin Chary78127b42007-01-26 14:04:49 +0100804}
805
806/*
807 * Experimental support for display switching. As of now: 1 should activate
808 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
809 * Any combination (bitwise) of these will suffice. I never actually tested 4
810 * displays hooked up simultaneously, so be warned. See the acpi4asus README
811 * for more info.
812 */
813static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
814 const char *buf, size_t count)
815{
Corentin Chary9129d142009-12-01 22:39:41 +0100816 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary78127b42007-01-26 14:04:49 +0100817 int rv, value;
818
819 rv = parse_arg(buf, count, &value);
820 if (rv > 0)
Corentin Chary9129d142009-12-01 22:39:41 +0100821 set_display(asus, value);
Corentin Chary78127b42007-01-26 14:04:49 +0100822 return rv;
823}
824
Corentin Chary8b857352007-01-26 14:04:58 +0100825/*
826 * Light Sens
827 */
Corentin Chary9129d142009-12-01 22:39:41 +0100828static void set_light_sens_switch(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100829{
Corentin Charyd8c67322009-11-28 10:27:51 +0100830 if (write_acpi_int(ls_switch_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200831 pr_warning("Error setting light sensor switch\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100832 asus->light_switch = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100833}
834
835static ssize_t show_lssw(struct device *dev,
836 struct device_attribute *attr, char *buf)
837{
Corentin Chary9129d142009-12-01 22:39:41 +0100838 struct asus_laptop *asus = dev_get_drvdata(dev);
839
Corentin Chary50a90c42009-11-30 21:55:12 +0100840 return sprintf(buf, "%d\n", asus->light_switch);
Corentin Chary8b857352007-01-26 14:04:58 +0100841}
842
843static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
844 const char *buf, size_t count)
845{
Corentin Chary9129d142009-12-01 22:39:41 +0100846 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100847 int rv, value;
848
849 rv = parse_arg(buf, count, &value);
850 if (rv > 0)
Corentin Chary9129d142009-12-01 22:39:41 +0100851 set_light_sens_switch(asus, value ? 1 : 0);
Corentin Chary8b857352007-01-26 14:04:58 +0100852
853 return rv;
854}
855
Corentin Chary9129d142009-12-01 22:39:41 +0100856static void set_light_sens_level(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100857{
Corentin Charyd8c67322009-11-28 10:27:51 +0100858 if (write_acpi_int(ls_level_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200859 pr_warning("Error setting light sensor level\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100860 asus->light_level = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100861}
862
863static ssize_t show_lslvl(struct device *dev,
864 struct device_attribute *attr, char *buf)
865{
Corentin Chary9129d142009-12-01 22:39:41 +0100866 struct asus_laptop *asus = dev_get_drvdata(dev);
867
Corentin Chary50a90c42009-11-30 21:55:12 +0100868 return sprintf(buf, "%d\n", asus->light_level);
Corentin Chary8b857352007-01-26 14:04:58 +0100869}
870
871static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
872 const char *buf, size_t count)
873{
Corentin Chary9129d142009-12-01 22:39:41 +0100874 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100875 int rv, value;
876
877 rv = parse_arg(buf, count, &value);
878 if (rv > 0) {
879 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
880 /* 0 <= value <= 15 */
Corentin Chary9129d142009-12-01 22:39:41 +0100881 set_light_sens_level(asus, value);
Corentin Chary8b857352007-01-26 14:04:58 +0100882 }
883
884 return rv;
885}
886
Corentin Charye539c2f2007-05-06 14:47:06 +0200887/*
888 * GPS
889 */
890static ssize_t show_gps(struct device *dev,
891 struct device_attribute *attr, char *buf)
892{
Corentin Chary9129d142009-12-01 22:39:41 +0100893 struct asus_laptop *asus = dev_get_drvdata(dev);
894
895 return sprintf(buf, "%d\n", read_status(asus, GPS_ON));
Corentin Charye539c2f2007-05-06 14:47:06 +0200896}
897
898static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
899 const char *buf, size_t count)
900{
Corentin Chary9129d142009-12-01 22:39:41 +0100901 struct asus_laptop *asus = dev_get_drvdata(dev);
902
903 return store_status(asus, buf, count, NULL, GPS_ON);
Corentin Charye539c2f2007-05-06 14:47:06 +0200904}
905
Corentin Chary034ce902009-01-20 16:17:43 +0100906/*
907 * Hotkey functions
908 */
Corentin Chary9129d142009-12-01 22:39:41 +0100909static struct key_entry *asus_get_entry_by_scancode(struct asus_laptop *asus,
910 int code)
Corentin Chary034ce902009-01-20 16:17:43 +0100911{
912 struct key_entry *key;
913
Corentin Chary9129d142009-12-01 22:39:41 +0100914 for (key = asus->keymap; key->type != KE_END; key++)
Corentin Chary034ce902009-01-20 16:17:43 +0100915 if (code == key->code)
916 return key;
917
918 return NULL;
919}
920
Corentin Chary9129d142009-12-01 22:39:41 +0100921static struct key_entry *asus_get_entry_by_keycode(struct asus_laptop *asus,
922 int code)
Corentin Chary034ce902009-01-20 16:17:43 +0100923{
924 struct key_entry *key;
925
Corentin Chary9129d142009-12-01 22:39:41 +0100926 for (key = asus->keymap; key->type != KE_END; key++)
Corentin Chary034ce902009-01-20 16:17:43 +0100927 if (code == key->keycode && key->type == KE_KEY)
928 return key;
929
930 return NULL;
931}
932
933static int asus_getkeycode(struct input_dev *dev, int scancode, int *keycode)
934{
Corentin Chary9129d142009-12-01 22:39:41 +0100935 struct asus_laptop *asus = input_get_drvdata(dev);
936 struct key_entry *key = asus_get_entry_by_scancode(asus, scancode);
Corentin Chary034ce902009-01-20 16:17:43 +0100937
938 if (key && key->type == KE_KEY) {
939 *keycode = key->keycode;
940 return 0;
941 }
942
943 return -EINVAL;
944}
945
946static int asus_setkeycode(struct input_dev *dev, int scancode, int keycode)
947{
Corentin Chary9129d142009-12-01 22:39:41 +0100948 struct asus_laptop *asus = input_get_drvdata(dev);
Corentin Chary034ce902009-01-20 16:17:43 +0100949 struct key_entry *key;
950 int old_keycode;
951
952 if (keycode < 0 || keycode > KEY_MAX)
953 return -EINVAL;
954
Corentin Chary9129d142009-12-01 22:39:41 +0100955 key = asus_get_entry_by_scancode(asus, scancode);
Corentin Chary034ce902009-01-20 16:17:43 +0100956 if (key && key->type == KE_KEY) {
957 old_keycode = key->keycode;
958 key->keycode = keycode;
959 set_bit(keycode, dev->keybit);
Corentin Chary9129d142009-12-01 22:39:41 +0100960 if (!asus_get_entry_by_keycode(asus, old_keycode))
Corentin Chary034ce902009-01-20 16:17:43 +0100961 clear_bit(old_keycode, dev->keybit);
962 return 0;
963 }
964
965 return -EINVAL;
966}
967
Corentin Chary600ad522009-11-30 21:42:42 +0100968static void asus_acpi_notify(struct acpi_device *device, u32 event)
Corentin Chary85091b72007-01-26 14:04:30 +0100969{
Corentin Chary9129d142009-12-01 22:39:41 +0100970 struct asus_laptop *asus = acpi_driver_data(device);
Corentin Chary034ce902009-01-20 16:17:43 +0100971 static struct key_entry *key;
Corentin Chary6050c8d2009-02-15 19:30:19 +0100972 u16 count;
Corentin Chary034ce902009-01-20 16:17:43 +0100973
Corentin Chary6b7091e2007-01-26 14:04:45 +0100974 /*
975 * We need to tell the backlight device when the backlight power is
976 * switched
977 */
978 if (event == ATKD_LCD_ON) {
Corentin Chary9129d142009-12-01 22:39:41 +0100979 write_status(asus, NULL, 1, LCD_ON);
980 lcd_blank(asus, FB_BLANK_UNBLANK);
Len Brown8def05f2007-01-30 01:46:43 -0500981 } else if (event == ATKD_LCD_OFF) {
Corentin Chary9129d142009-12-01 22:39:41 +0100982 write_status(asus, NULL, 0, LCD_ON);
983 lcd_blank(asus, FB_BLANK_POWERDOWN);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100984 }
985
Corentin Chary619d8b12009-11-28 10:35:37 +0100986 /* TODO Find a better way to handle events count. */
Corentin Chary50a90c42009-11-30 21:55:12 +0100987 count = asus->event_count[event % 128]++;
988 acpi_bus_generate_proc_event(asus->device, event, count);
989 acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
990 dev_name(&asus->device->dev), event,
Corentin Chary6050c8d2009-02-15 19:30:19 +0100991 count);
Corentin Chary85091b72007-01-26 14:04:30 +0100992
Corentin Chary50a90c42009-11-30 21:55:12 +0100993 if (asus->inputdev) {
Corentin Chary9129d142009-12-01 22:39:41 +0100994 key = asus_get_entry_by_scancode(asus, event);
Corentin Chary034ce902009-01-20 16:17:43 +0100995 if (!key)
996 return ;
997
998 switch (key->type) {
999 case KE_KEY:
Corentin Chary50a90c42009-11-30 21:55:12 +01001000 input_report_key(asus->inputdev, key->keycode, 1);
1001 input_sync(asus->inputdev);
1002 input_report_key(asus->inputdev, key->keycode, 0);
1003 input_sync(asus->inputdev);
Corentin Chary034ce902009-01-20 16:17:43 +01001004 break;
1005 }
1006 }
Corentin Chary85091b72007-01-26 14:04:30 +01001007}
1008
1009#define ASUS_CREATE_DEVICE_ATTR(_name) \
1010 struct device_attribute dev_attr_##_name = { \
1011 .attr = { \
1012 .name = __stringify(_name), \
Tejun Heo7b595752007-06-14 03:45:17 +09001013 .mode = 0 }, \
Corentin Chary85091b72007-01-26 14:04:30 +01001014 .show = NULL, \
1015 .store = NULL, \
1016 }
1017
1018#define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
1019 do { \
1020 dev_attr_##_name.attr.mode = _mode; \
1021 dev_attr_##_name.show = _show; \
1022 dev_attr_##_name.store = _store; \
1023 } while(0)
1024
1025static ASUS_CREATE_DEVICE_ATTR(infos);
Corentin Chary4564de12007-01-26 14:04:40 +01001026static ASUS_CREATE_DEVICE_ATTR(wlan);
1027static ASUS_CREATE_DEVICE_ATTR(bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +01001028static ASUS_CREATE_DEVICE_ATTR(display);
Corentin Chary722ad972007-01-26 14:04:55 +01001029static ASUS_CREATE_DEVICE_ATTR(ledd);
Corentin Chary8b857352007-01-26 14:04:58 +01001030static ASUS_CREATE_DEVICE_ATTR(ls_switch);
1031static ASUS_CREATE_DEVICE_ATTR(ls_level);
Corentin Charye539c2f2007-05-06 14:47:06 +02001032static ASUS_CREATE_DEVICE_ATTR(gps);
Corentin Chary85091b72007-01-26 14:04:30 +01001033
1034static struct attribute *asuspf_attributes[] = {
Len Brown8def05f2007-01-30 01:46:43 -05001035 &dev_attr_infos.attr,
1036 &dev_attr_wlan.attr,
1037 &dev_attr_bluetooth.attr,
1038 &dev_attr_display.attr,
1039 &dev_attr_ledd.attr,
1040 &dev_attr_ls_switch.attr,
1041 &dev_attr_ls_level.attr,
Corentin Charye539c2f2007-05-06 14:47:06 +02001042 &dev_attr_gps.attr,
Len Brown8def05f2007-01-30 01:46:43 -05001043 NULL
Corentin Chary85091b72007-01-26 14:04:30 +01001044};
1045
Corentin Chary600ad522009-11-30 21:42:42 +01001046static struct attribute_group platform_attribute_group = {
Len Brown8def05f2007-01-30 01:46:43 -05001047 .attrs = asuspf_attributes
Corentin Chary85091b72007-01-26 14:04:30 +01001048};
1049
Corentin Chary9129d142009-12-01 22:39:41 +01001050static int asus_platform_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001051{
1052 int result;
1053
Corentin Chary50a90c42009-11-30 21:55:12 +01001054 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1055 if (!asus->platform_device)
Corentin Chary600ad522009-11-30 21:42:42 +01001056 return -ENOMEM;
Corentin Chary9129d142009-12-01 22:39:41 +01001057 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001058
Corentin Chary50a90c42009-11-30 21:55:12 +01001059 result = platform_device_add(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001060 if (result)
1061 goto fail_platform_device;
1062
Corentin Chary50a90c42009-11-30 21:55:12 +01001063 result = sysfs_create_group(&asus->platform_device->dev.kobj,
Corentin Chary600ad522009-11-30 21:42:42 +01001064 &platform_attribute_group);
1065 if (result)
1066 goto fail_sysfs;
1067 return 0;
1068
1069fail_sysfs:
Corentin Chary50a90c42009-11-30 21:55:12 +01001070 platform_device_del(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001071fail_platform_device:
Corentin Chary50a90c42009-11-30 21:55:12 +01001072 platform_device_put(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001073 return result;
1074}
1075
Corentin Chary9129d142009-12-01 22:39:41 +01001076static void asus_platform_exit(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001077{
Corentin Chary50a90c42009-11-30 21:55:12 +01001078 sysfs_remove_group(&asus->platform_device->dev.kobj,
Corentin Chary600ad522009-11-30 21:42:42 +01001079 &platform_attribute_group);
Corentin Chary50a90c42009-11-30 21:55:12 +01001080 platform_device_unregister(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001081}
1082
1083static struct platform_driver platform_driver = {
Len Brown8def05f2007-01-30 01:46:43 -05001084 .driver = {
Corentin Chary9129d142009-12-01 22:39:41 +01001085 .name = ASUS_LAPTOP_FILE,
1086 .owner = THIS_MODULE,
1087 }
Corentin Chary85091b72007-01-26 14:04:30 +01001088};
1089
Corentin Chary9129d142009-12-01 22:39:41 +01001090static void asus_laptop_add_fs(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001091{
1092 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
Corentin Chary4564de12007-01-26 14:04:40 +01001093
1094 if (wl_switch_handle)
1095 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
1096
1097 if (bt_switch_handle)
1098 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
1099 show_bluetooth, store_bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +01001100
1101 if (display_set_handle && display_get_handle)
1102 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
Len Brown8def05f2007-01-30 01:46:43 -05001103 else if (display_set_handle)
Corentin Chary78127b42007-01-26 14:04:49 +01001104 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
1105
Corentin Chary722ad972007-01-26 14:04:55 +01001106 if (ledd_set_handle)
1107 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
1108
Corentin Chary8b857352007-01-26 14:04:58 +01001109 if (ls_switch_handle && ls_level_handle) {
1110 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
1111 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
1112 }
Corentin Charye539c2f2007-05-06 14:47:06 +02001113
Corentin Charyf3985322007-05-06 14:48:22 +02001114 if (gps_status_handle && gps_on_handle && gps_off_handle)
Corentin Charye539c2f2007-05-06 14:47:06 +02001115 ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
Corentin Chary85091b72007-01-26 14:04:30 +01001116}
1117
Len Brown8def05f2007-01-30 01:46:43 -05001118static int asus_handle_init(char *name, acpi_handle * handle,
Corentin Chary85091b72007-01-26 14:04:30 +01001119 char **paths, int num_paths)
1120{
1121 int i;
1122 acpi_status status;
1123
1124 for (i = 0; i < num_paths; i++) {
1125 status = acpi_get_handle(NULL, paths[i], handle);
1126 if (ACPI_SUCCESS(status))
1127 return 0;
1128 }
1129
1130 *handle = NULL;
1131 return -ENODEV;
1132}
1133
1134#define ASUS_HANDLE_INIT(object) \
1135 asus_handle_init(#object, &object##_handle, object##_paths, \
1136 ARRAY_SIZE(object##_paths))
1137
Corentin Chary85091b72007-01-26 14:04:30 +01001138/*
Corentin Chary50a90c42009-11-30 21:55:12 +01001139 * This function is used to initialize the context with right values. In this
1140 * method, we can make all the detection we want, and modify the asus_laptop
1141 * struct
Corentin Chary85091b72007-01-26 14:04:30 +01001142 */
Corentin Chary7c247642009-11-30 22:13:54 +01001143static int asus_laptop_get_info(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001144{
1145 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Corentin Chary85091b72007-01-26 14:04:30 +01001146 union acpi_object *model = NULL;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001147 unsigned long long bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +01001148 char *string = NULL;
1149 acpi_status status;
1150
1151 /*
1152 * Get DSDT headers early enough to allow for differentiating between
1153 * models, but late enough to allow acpi_bus_register_driver() to fail
1154 * before doing anything ACPI-specific. Should we encounter a machine,
1155 * which needs special handling (i.e. its hotkey device has a different
Corentin Chary7c247642009-11-30 22:13:54 +01001156 * HID), this bit will be moved.
Corentin Chary85091b72007-01-26 14:04:30 +01001157 */
Corentin Chary7c247642009-11-30 22:13:54 +01001158 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
Corentin Chary85091b72007-01-26 14:04:30 +01001159 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001160 pr_warning("Couldn't get the DSDT table header\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001161
1162 /* We have to write 0 on init this far for all ASUS models */
Corentin Chary50a90c42009-11-30 21:55:12 +01001163 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001164 pr_err("Hotkey initialization failed\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001165 return -ENODEV;
1166 }
1167
1168 /* This needs to be called for some laptops to init properly */
Corentin Chary9a816852007-03-11 10:25:38 +01001169 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001170 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
Corentin Chary9a816852007-03-11 10:25:38 +01001171 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001172 pr_warning("Error calling BSTS\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001173 else if (bsts_result)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001174 pr_notice("BSTS called, 0x%02x returned\n",
Corentin Chary9a816852007-03-11 10:25:38 +01001175 (uint) bsts_result);
Corentin Chary85091b72007-01-26 14:04:30 +01001176
Corentin Chary185e5af2007-03-11 10:27:33 +01001177 /* This too ... */
Corentin Chary50a90c42009-11-30 21:55:12 +01001178 write_acpi_int(asus->handle, "CWAP", wapf);
Corentin Chary185e5af2007-03-11 10:27:33 +01001179
Corentin Chary85091b72007-01-26 14:04:30 +01001180 /*
1181 * Try to match the object returned by INIT to the specific model.
1182 * Handle every possible object (or the lack of thereof) the DSDT
1183 * writers might throw at us. When in trouble, we pass NULL to
1184 * asus_model_match() and try something completely different.
1185 */
1186 if (buffer.pointer) {
1187 model = buffer.pointer;
1188 switch (model->type) {
1189 case ACPI_TYPE_STRING:
1190 string = model->string.pointer;
1191 break;
1192 case ACPI_TYPE_BUFFER:
1193 string = model->buffer.pointer;
1194 break;
1195 default:
1196 string = "";
1197 break;
1198 }
1199 }
Corentin Chary50a90c42009-11-30 21:55:12 +01001200 asus->name = kstrdup(string, GFP_KERNEL);
1201 if (!asus->name)
Corentin Chary85091b72007-01-26 14:04:30 +01001202 return -ENOMEM;
1203
Len Brown8def05f2007-01-30 01:46:43 -05001204 if (*string)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001205 pr_notice(" %s model detected\n", string);
Corentin Chary85091b72007-01-26 14:04:30 +01001206
Corentin Charybe18cda2007-01-26 14:04:35 +01001207 ASUS_HANDLE_INIT(mled_set);
1208 ASUS_HANDLE_INIT(tled_set);
1209 ASUS_HANDLE_INIT(rled_set);
1210 ASUS_HANDLE_INIT(pled_set);
Corentin Charyfdd8d082007-03-11 10:26:48 +01001211 ASUS_HANDLE_INIT(gled_set);
Corentin Charybe18cda2007-01-26 14:04:35 +01001212
Corentin Chary722ad972007-01-26 14:04:55 +01001213 ASUS_HANDLE_INIT(ledd_set);
1214
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001215 ASUS_HANDLE_INIT(kled_set);
1216 ASUS_HANDLE_INIT(kled_get);
1217
Corentin Chary4564de12007-01-26 14:04:40 +01001218 /*
1219 * The HWRS method return informations about the hardware.
1220 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1221 * The significance of others is yet to be found.
1222 * If we don't find the method, we assume the device are present.
1223 */
Corentin Chary9a816852007-03-11 10:25:38 +01001224 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001225 acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
Corentin Chary9a816852007-03-11 10:25:38 +01001226 if (ACPI_FAILURE(status))
Corentin Chary4564de12007-01-26 14:04:40 +01001227 hwrs_result = WL_HWRS | BT_HWRS;
1228
Len Brown8def05f2007-01-30 01:46:43 -05001229 if (hwrs_result & WL_HWRS)
Corentin Chary4564de12007-01-26 14:04:40 +01001230 ASUS_HANDLE_INIT(wl_switch);
Len Brown8def05f2007-01-30 01:46:43 -05001231 if (hwrs_result & BT_HWRS)
Corentin Chary4564de12007-01-26 14:04:40 +01001232 ASUS_HANDLE_INIT(bt_switch);
1233
1234 ASUS_HANDLE_INIT(wireless_status);
1235
Corentin Chary6b7091e2007-01-26 14:04:45 +01001236 ASUS_HANDLE_INIT(brightness_set);
1237 ASUS_HANDLE_INIT(brightness_get);
1238
1239 ASUS_HANDLE_INIT(lcd_switch);
1240
Corentin Chary78127b42007-01-26 14:04:49 +01001241 ASUS_HANDLE_INIT(display_set);
1242 ASUS_HANDLE_INIT(display_get);
1243
Corentin Charybe966662009-08-29 10:28:29 +02001244 /*
1245 * There is a lot of models with "ALSL", but a few get
1246 * a real light sens, so we need to check it.
1247 */
Corentin Chary832d9952007-05-06 14:47:29 +02001248 if (!ASUS_HANDLE_INIT(ls_switch))
Corentin Chary8b857352007-01-26 14:04:58 +01001249 ASUS_HANDLE_INIT(ls_level);
1250
Corentin Charye539c2f2007-05-06 14:47:06 +02001251 ASUS_HANDLE_INIT(gps_on);
1252 ASUS_HANDLE_INIT(gps_off);
1253 ASUS_HANDLE_INIT(gps_status);
1254
Corentin Chary85091b72007-01-26 14:04:30 +01001255 kfree(model);
1256
1257 return AE_OK;
1258}
1259
Corentin Chary9129d142009-12-01 22:39:41 +01001260static int asus_input_init(struct asus_laptop *asus)
Corentin Chary034ce902009-01-20 16:17:43 +01001261{
1262 const struct key_entry *key;
1263 int result;
1264
Corentin Chary50a90c42009-11-30 21:55:12 +01001265 asus->inputdev = input_allocate_device();
1266 if (!asus->inputdev) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001267 pr_info("Unable to allocate input device\n");
Corentin Chary034ce902009-01-20 16:17:43 +01001268 return 0;
1269 }
Corentin Chary50a90c42009-11-30 21:55:12 +01001270 asus->inputdev->name = "Asus Laptop extra buttons";
Corentin Chary9129d142009-12-01 22:39:41 +01001271 asus->inputdev->dev.parent = &asus->platform_device->dev;
Corentin Chary50a90c42009-11-30 21:55:12 +01001272 asus->inputdev->phys = ASUS_LAPTOP_FILE "/input0";
1273 asus->inputdev->id.bustype = BUS_HOST;
1274 asus->inputdev->getkeycode = asus_getkeycode;
1275 asus->inputdev->setkeycode = asus_setkeycode;
Corentin Chary9129d142009-12-01 22:39:41 +01001276 input_set_drvdata(asus->inputdev, asus);
Corentin Chary034ce902009-01-20 16:17:43 +01001277
Corentin Chary9129d142009-12-01 22:39:41 +01001278 asus->keymap = kmemdup(asus_keymap, sizeof(asus_keymap),
1279 GFP_KERNEL);
1280 for (key = asus->keymap; key->type != KE_END; key++) {
Corentin Chary034ce902009-01-20 16:17:43 +01001281 switch (key->type) {
1282 case KE_KEY:
Corentin Chary50a90c42009-11-30 21:55:12 +01001283 set_bit(EV_KEY, asus->inputdev->evbit);
1284 set_bit(key->keycode, asus->inputdev->keybit);
Corentin Chary034ce902009-01-20 16:17:43 +01001285 break;
1286 }
1287 }
Corentin Chary50a90c42009-11-30 21:55:12 +01001288 result = input_register_device(asus->inputdev);
Corentin Chary034ce902009-01-20 16:17:43 +01001289 if (result) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001290 pr_info("Unable to register input device\n");
Corentin Chary50a90c42009-11-30 21:55:12 +01001291 input_free_device(asus->inputdev);
Corentin Chary034ce902009-01-20 16:17:43 +01001292 }
1293 return result;
1294}
1295
Corentin Chary9129d142009-12-01 22:39:41 +01001296static void asus_backlight_exit(struct asus_laptop *asus)
Corentin Chary6b7091e2007-01-26 14:04:45 +01001297{
Corentin Chary7c247642009-11-30 22:13:54 +01001298 if (asus->backlight_device)
1299 backlight_device_unregister(asus->backlight_device);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001300}
1301
Corentin Chary9129d142009-12-01 22:39:41 +01001302#define ASUS_LED_UNREGISTER(object) \
Guillaume Chazaraine1996a692007-08-16 18:18:53 +02001303 if (object##_led.dev) \
1304 led_classdev_unregister(&object##_led)
Corentin Charybe18cda2007-01-26 14:04:35 +01001305
Corentin Chary9129d142009-12-01 22:39:41 +01001306static void asus_led_exit(struct asus_laptop *asus)
Corentin Charybe18cda2007-01-26 14:04:35 +01001307{
1308 ASUS_LED_UNREGISTER(mled);
1309 ASUS_LED_UNREGISTER(tled);
1310 ASUS_LED_UNREGISTER(pled);
1311 ASUS_LED_UNREGISTER(rled);
Corentin Charyfdd8d082007-03-11 10:26:48 +01001312 ASUS_LED_UNREGISTER(gled);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001313 ASUS_LED_UNREGISTER(kled);
Corentin Chary9129d142009-12-01 22:39:41 +01001314 if (asus->leds.workqueue) {
1315 destroy_workqueue(asus->leds.workqueue);
1316 asus->leds.workqueue = NULL;
1317 }
Corentin Charybe18cda2007-01-26 14:04:35 +01001318}
1319
Corentin Chary9129d142009-12-01 22:39:41 +01001320static void asus_input_exit(struct asus_laptop *asus)
Corentin Chary034ce902009-01-20 16:17:43 +01001321{
Corentin Chary50a90c42009-11-30 21:55:12 +01001322 if (asus->inputdev)
1323 input_unregister_device(asus->inputdev);
Corentin Chary034ce902009-01-20 16:17:43 +01001324}
1325
Corentin Chary9129d142009-12-01 22:39:41 +01001326static int asus_backlight_init(struct asus_laptop *asus)
Corentin Chary6b7091e2007-01-26 14:04:45 +01001327{
1328 struct backlight_device *bd;
Corentin Chary9129d142009-12-01 22:39:41 +01001329 struct device *dev = &asus->platform_device->dev;
Corentin Chary6b7091e2007-01-26 14:04:45 +01001330
Len Brown8def05f2007-01-30 01:46:43 -05001331 if (brightness_set_handle && lcd_switch_handle) {
Corentin Chary50a90c42009-11-30 21:55:12 +01001332 bd = backlight_device_register(ASUS_LAPTOP_FILE, dev,
Corentin Chary9129d142009-12-01 22:39:41 +01001333 asus, &asusbl_ops);
Len Brown8def05f2007-01-30 01:46:43 -05001334 if (IS_ERR(bd)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001335 pr_err("Could not register asus backlight device\n");
Corentin Chary7c247642009-11-30 22:13:54 +01001336 asus->backlight_device = NULL;
Corentin Chary6b7091e2007-01-26 14:04:45 +01001337 return PTR_ERR(bd);
1338 }
1339
Corentin Chary7c247642009-11-30 22:13:54 +01001340 asus->backlight_device = bd;
Corentin Chary6b7091e2007-01-26 14:04:45 +01001341
Richard Purdie599a52d2007-02-10 23:07:48 +00001342 bd->props.max_brightness = 15;
1343 bd->props.brightness = read_brightness(NULL);
1344 bd->props.power = FB_BLANK_UNBLANK;
Richard Purdie28ee0862007-02-08 22:25:09 +00001345 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001346 }
1347 return 0;
1348}
1349
Corentin Chary9129d142009-12-01 22:39:41 +01001350/*
1351 * Ugly macro, need to fix that later
1352 */
1353#define ASUS_LED_REGISTER(asus, object, _name, max) \
1354 do { \
1355 struct led_classdev *ldev = &asus->leds.object; \
1356 if (!object##_set_handle) \
1357 break ; \
1358 \
1359 INIT_WORK(&asus->leds.object##_work, object##_led_update); \
1360 ldev->name = "asus::" _name; \
1361 ldev->brightness_set = object##_led_set; \
1362 ldev->max_brightness = max; \
1363 rv = led_classdev_register(&asus->platform_device->dev, ldev); \
1364 if (rv) \
1365 goto error; \
1366 } while (0)
Corentin Charybe18cda2007-01-26 14:04:35 +01001367
Corentin Chary9129d142009-12-01 22:39:41 +01001368static int asus_led_init(struct asus_laptop *asus)
Corentin Charybe18cda2007-01-26 14:04:35 +01001369{
1370 int rv;
1371
Corentin Chary7c247642009-11-30 22:13:54 +01001372 /*
1373 * Functions that actually update the LED's are called from a
1374 * workqueue. By doing this as separate work rather than when the LED
1375 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
1376 * potentially bad time, such as a timer interrupt.
1377 */
Corentin Chary9129d142009-12-01 22:39:41 +01001378 asus->leds.workqueue = create_singlethread_workqueue("led_workqueue");
1379 if (!asus->leds.workqueue)
1380 return -ENOMEM;
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001381
Corentin Chary9129d142009-12-01 22:39:41 +01001382 ASUS_LED_REGISTER(asus, mled, "mail", 1);
1383 ASUS_LED_REGISTER(asus, tled, "touchpad", 1);
1384 ASUS_LED_REGISTER(asus, rled, "record", 1);
1385 ASUS_LED_REGISTER(asus, pled, "phone", 1);
1386 ASUS_LED_REGISTER(asus, gled, "gaming", 1);
1387 if (kled_set_handle && kled_get_handle)
1388 ASUS_LED_REGISTER(asus, kled, "kbd_backlight", 3);
1389error:
1390 if (rv)
1391 asus_led_exit(asus);
Al Viro3b0d7112007-07-23 11:21:34 +01001392 return rv;
Corentin Charybe18cda2007-01-26 14:04:35 +01001393}
1394
Corentin Chary600ad522009-11-30 21:42:42 +01001395
1396static bool asus_device_present;
1397
Corentin Chary9129d142009-12-01 22:39:41 +01001398static int __devinit asus_acpi_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001399{
1400 int result = 0;
1401
Corentin Chary50a90c42009-11-30 21:55:12 +01001402 result = acpi_bus_get_status(asus->device);
Corentin Chary600ad522009-11-30 21:42:42 +01001403 if (result)
1404 return result;
Corentin Chary50a90c42009-11-30 21:55:12 +01001405 if (!asus->device->status.present) {
Corentin Chary600ad522009-11-30 21:42:42 +01001406 pr_err("Hotkey device not present, aborting\n");
1407 return -ENODEV;
1408 }
1409
Corentin Chary7c247642009-11-30 22:13:54 +01001410 result = asus_laptop_get_info(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001411 if (result)
1412 return result;
1413
Corentin Chary9129d142009-12-01 22:39:41 +01001414 asus_laptop_add_fs(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001415
1416 /* WLED and BLED are on by default */
Corentin Chary9129d142009-12-01 22:39:41 +01001417 write_status(asus, bt_switch_handle, 1, BT_ON);
1418 write_status(asus, wl_switch_handle, 1, WL_ON);
Corentin Chary600ad522009-11-30 21:42:42 +01001419
1420 /* If the h/w switch is off, we need to check the real status */
Corentin Chary9129d142009-12-01 22:39:41 +01001421 write_status(asus, NULL, read_status(asus, BT_ON), BT_ON);
1422 write_status(asus, NULL, read_status(asus, WL_ON), WL_ON);
Corentin Chary600ad522009-11-30 21:42:42 +01001423
1424 /* LCD Backlight is on by default */
Corentin Chary9129d142009-12-01 22:39:41 +01001425 write_status(asus, NULL, 1, LCD_ON);
Corentin Chary600ad522009-11-30 21:42:42 +01001426
1427 /* Keyboard Backlight is on by default */
1428 if (kled_set_handle)
Corentin Chary9129d142009-12-01 22:39:41 +01001429 set_kled_lvl(asus, 1);
Corentin Chary600ad522009-11-30 21:42:42 +01001430
1431 /* LED display is off by default */
Corentin Chary50a90c42009-11-30 21:55:12 +01001432 asus->ledd_status = 0xFFF;
Corentin Chary600ad522009-11-30 21:42:42 +01001433
1434 /* Set initial values of light sensor and level */
1435 hotk->light_switch = 0; /* Default to light sensor disabled */
1436 hotk->light_level = 5; /* level 5 for sensor sensitivity */
1437
1438 if (ls_switch_handle)
Corentin Chary9129d142009-12-01 22:39:41 +01001439 set_light_sens_switch(asus, asus->light_switch);
Corentin Chary600ad522009-11-30 21:42:42 +01001440
1441 if (ls_level_handle)
Corentin Chary9129d142009-12-01 22:39:41 +01001442 set_light_sens_level(asus, asus->light_level);
Corentin Chary600ad522009-11-30 21:42:42 +01001443
1444 /* GPS is on by default */
Corentin Chary9129d142009-12-01 22:39:41 +01001445 write_status(asus, NULL, 1, GPS_ON);
Corentin Chary600ad522009-11-30 21:42:42 +01001446 return result;
1447}
1448
1449static int __devinit asus_acpi_add(struct acpi_device *device)
1450{
Corentin Chary9129d142009-12-01 22:39:41 +01001451 struct asus_laptop *asus;
Corentin Chary600ad522009-11-30 21:42:42 +01001452 int result;
1453
1454 pr_notice("Asus Laptop Support version %s\n",
1455 ASUS_LAPTOP_VERSION);
Corentin Chary50a90c42009-11-30 21:55:12 +01001456 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1457 if (!asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001458 return -ENOMEM;
Corentin Chary50a90c42009-11-30 21:55:12 +01001459 asus->handle = device->handle;
1460 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1461 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1462 device->driver_data = asus;
1463 asus->device = device;
Corentin Chary600ad522009-11-30 21:42:42 +01001464
Corentin Chary9129d142009-12-01 22:39:41 +01001465 result = asus_acpi_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001466 if (result)
1467 goto fail_platform;
1468
1469 /*
1470 * Register the platform device first. It is used as a parent for the
1471 * sub-devices below.
1472 */
Corentin Chary9129d142009-12-01 22:39:41 +01001473 result = asus_platform_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001474 if (result)
1475 goto fail_platform;
1476
1477 if (!acpi_video_backlight_support()) {
Corentin Chary9129d142009-12-01 22:39:41 +01001478 result = asus_backlight_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001479 if (result)
1480 goto fail_backlight;
1481 } else
1482 pr_info("Backlight controlled by ACPI video driver\n");
1483
Corentin Chary9129d142009-12-01 22:39:41 +01001484 result = asus_input_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001485 if (result)
1486 goto fail_input;
1487
Corentin Chary9129d142009-12-01 22:39:41 +01001488 result = asus_led_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001489 if (result)
1490 goto fail_led;
1491
1492 asus_device_present = true;
1493 return 0;
1494
1495fail_led:
Corentin Chary9129d142009-12-01 22:39:41 +01001496 asus_input_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001497fail_input:
Corentin Chary9129d142009-12-01 22:39:41 +01001498 asus_backlight_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001499fail_backlight:
Corentin Chary9129d142009-12-01 22:39:41 +01001500 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001501fail_platform:
Corentin Chary50a90c42009-11-30 21:55:12 +01001502 kfree(asus->name);
1503 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001504
1505 return result;
1506}
1507
1508static int asus_acpi_remove(struct acpi_device *device, int type)
1509{
Corentin Chary9129d142009-12-01 22:39:41 +01001510 struct asus_laptop *asus = acpi_driver_data(device);
1511
1512 asus_backlight_exit(asus);
1513 asus_led_exit(asus);
1514 asus_input_exit(asus);
1515 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001516
Corentin Chary50a90c42009-11-30 21:55:12 +01001517 kfree(asus->name);
1518 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001519 return 0;
1520}
1521
1522static const struct acpi_device_id asus_device_ids[] = {
1523 {"ATK0100", 0},
1524 {"ATK0101", 0},
1525 {"", 0},
1526};
1527MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1528
1529static struct acpi_driver asus_acpi_driver = {
Corentin Chary50a90c42009-11-30 21:55:12 +01001530 .name = ASUS_LAPTOP_NAME,
1531 .class = ASUS_LAPTOP_CLASS,
Corentin Chary600ad522009-11-30 21:42:42 +01001532 .owner = THIS_MODULE,
1533 .ids = asus_device_ids,
1534 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1535 .ops = {
1536 .add = asus_acpi_add,
1537 .remove = asus_acpi_remove,
1538 .notify = asus_acpi_notify,
1539 },
1540};
1541
Corentin Chary85091b72007-01-26 14:04:30 +01001542static int __init asus_laptop_init(void)
1543{
1544 int result;
1545
Corentin Chary600ad522009-11-30 21:42:42 +01001546 result = platform_driver_register(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001547 if (result < 0)
1548 return result;
1549
Corentin Chary600ad522009-11-30 21:42:42 +01001550 result = acpi_bus_register_driver(&asus_acpi_driver);
1551 if (result < 0)
1552 goto fail_acpi_driver;
1553 if (!asus_device_present) {
1554 result = -ENODEV;
1555 goto fail_no_device;
Corentin Chary85091b72007-01-26 14:04:30 +01001556 }
Len Brown8def05f2007-01-30 01:46:43 -05001557 return 0;
Corentin Chary85091b72007-01-26 14:04:30 +01001558
Corentin Chary600ad522009-11-30 21:42:42 +01001559fail_no_device:
1560 acpi_bus_unregister_driver(&asus_acpi_driver);
1561fail_acpi_driver:
1562 platform_driver_unregister(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001563 return result;
1564}
1565
Corentin Chary600ad522009-11-30 21:42:42 +01001566static void __exit asus_laptop_exit(void)
1567{
1568 acpi_bus_unregister_driver(&asus_acpi_driver);
1569 platform_driver_unregister(&platform_driver);
1570}
1571
Corentin Chary85091b72007-01-26 14:04:30 +01001572module_init(asus_laptop_init);
1573module_exit(asus_laptop_exit);