blob: 2505f15cbe6b3d0f3d7700d7525fae77f627d20a [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/*
199 * This is the main structure, we can use it to store anything interesting
200 * about the hotk device
201 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100202struct asus_laptop {
Corentin Charybe966662009-08-29 10:28:29 +0200203 char *name; /* laptop name */
Corentin Chary600ad522009-11-30 21:42:42 +0100204
205 struct platform_device *platform_device;
Corentin Charybe966662009-08-29 10:28:29 +0200206 struct acpi_device *device; /* the device we are in */
207 acpi_handle handle; /* the handle of the hotk device */
208 char status; /* status of the hotk, for LEDs, ... */
209 u32 ledd_status; /* status of the LED display */
210 u8 light_level; /* light sensor level */
211 u8 light_switch; /* light sensor switch value */
212 u16 event_count[128]; /* count for each event TODO make this better */
Corentin Chary034ce902009-01-20 16:17:43 +0100213 struct input_dev *inputdev;
214 u16 *keycode_map;
Corentin Chary85091b72007-01-26 14:04:30 +0100215};
216
217/*
218 * This header is made available to allow proper configuration given model,
Corentin Chary50a90c42009-11-30 21:55:12 +0100219 * revision number , ... this info cannot go in struct asus_laptop because it is
Corentin Chary85091b72007-01-26 14:04:30 +0100220 * available before the hotk
221 */
222static struct acpi_table_header *asus_info;
223
224/* The actual device the driver binds to */
Corentin Chary50a90c42009-11-30 21:55:12 +0100225static struct asus_laptop *asus;
Corentin Chary85091b72007-01-26 14:04:30 +0100226
Corentin Chary6b7091e2007-01-26 14:04:45 +0100227/* The backlight device /sys/class/backlight */
228static struct backlight_device *asus_backlight_device;
229
230/*
231 * The backlight class declaration
232 */
233static int read_brightness(struct backlight_device *bd);
234static int update_bl_status(struct backlight_device *bd);
Richard Purdie599a52d2007-02-10 23:07:48 +0000235static struct backlight_ops asusbl_ops = {
Len Brown8def05f2007-01-30 01:46:43 -0500236 .get_brightness = read_brightness,
237 .update_status = update_bl_status,
Corentin Chary6b7091e2007-01-26 14:04:45 +0100238};
239
Corentin Charybe966662009-08-29 10:28:29 +0200240/*
241 * These functions actually update the LED's, and are called from a
Corentin Charybe18cda2007-01-26 14:04:35 +0100242 * workqueue. By doing this as separate work rather than when the LED
243 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
Corentin Charybe966662009-08-29 10:28:29 +0200244 * potentially bad time, such as a timer interrupt.
245 */
Corentin Charybe18cda2007-01-26 14:04:35 +0100246static struct workqueue_struct *led_workqueue;
247
Corentin Chary977c3282009-08-28 12:56:49 +0000248#define ASUS_LED(object, ledname, max) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100249 static void object##_led_set(struct led_classdev *led_cdev, \
250 enum led_brightness value); \
Corentin Charyabfa57e2009-08-28 12:56:47 +0000251 static enum led_brightness object##_led_get( \
252 struct led_classdev *led_cdev); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100253 static void object##_led_update(struct work_struct *ignored); \
254 static int object##_led_wk; \
Adrian Bunkf110ef52007-02-20 01:07:25 +0100255 static DECLARE_WORK(object##_led_work, object##_led_update); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100256 static struct led_classdev object##_led = { \
Richard Purdie6c152be2007-10-31 15:00:07 +0100257 .name = "asus::" ledname, \
Corentin Charybe18cda2007-01-26 14:04:35 +0100258 .brightness_set = object##_led_set, \
Corentin Charyabfa57e2009-08-28 12:56:47 +0000259 .brightness_get = object##_led_get, \
Corentin Chary977c3282009-08-28 12:56:49 +0000260 .max_brightness = max \
Corentin Charybe18cda2007-01-26 14:04:35 +0100261 }
262
Corentin Chary977c3282009-08-28 12:56:49 +0000263ASUS_LED(mled, "mail", 1);
264ASUS_LED(tled, "touchpad", 1);
265ASUS_LED(rled, "record", 1);
266ASUS_LED(pled, "phone", 1);
267ASUS_LED(gled, "gaming", 1);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000268ASUS_LED(kled, "kbd_backlight", 3);
Corentin Charybe18cda2007-01-26 14:04:35 +0100269
Corentin Chary034ce902009-01-20 16:17:43 +0100270struct key_entry {
271 char type;
272 u8 code;
273 u16 keycode;
274};
275
276enum { KE_KEY, KE_END };
277
278static struct key_entry asus_keymap[] = {
Ike Panhc14f8af32009-12-03 07:45:11 +0000279 {KE_KEY, 0x02, KEY_SCREENLOCK},
280 {KE_KEY, 0x05, KEY_WLAN},
281 {KE_KEY, 0x08, KEY_F13},
282 {KE_KEY, 0x17, KEY_ZOOM},
283 {KE_KEY, 0x1f, KEY_BATTERY},
Corentin Chary034ce902009-01-20 16:17:43 +0100284 {KE_KEY, 0x30, KEY_VOLUMEUP},
285 {KE_KEY, 0x31, KEY_VOLUMEDOWN},
286 {KE_KEY, 0x32, KEY_MUTE},
287 {KE_KEY, 0x33, KEY_SWITCHVIDEOMODE},
288 {KE_KEY, 0x34, KEY_SWITCHVIDEOMODE},
289 {KE_KEY, 0x40, KEY_PREVIOUSSONG},
290 {KE_KEY, 0x41, KEY_NEXTSONG},
Corentin Chary309f5fb2009-04-27 09:23:42 +0200291 {KE_KEY, 0x43, KEY_STOPCD},
Corentin Chary034ce902009-01-20 16:17:43 +0100292 {KE_KEY, 0x45, KEY_PLAYPAUSE},
Corentin Chary4644d0e2009-08-28 12:56:52 +0000293 {KE_KEY, 0x4c, KEY_MEDIA},
Corentin Chary034ce902009-01-20 16:17:43 +0100294 {KE_KEY, 0x50, KEY_EMAIL},
295 {KE_KEY, 0x51, KEY_WWW},
Corentin Chary0aa20f72009-08-28 12:56:53 +0000296 {KE_KEY, 0x55, KEY_CALC},
Corentin Chary309f5fb2009-04-27 09:23:42 +0200297 {KE_KEY, 0x5C, KEY_SCREENLOCK}, /* Screenlock */
Corentin Chary034ce902009-01-20 16:17:43 +0100298 {KE_KEY, 0x5D, KEY_WLAN},
Corentin Charyf6413752009-08-28 12:56:48 +0000299 {KE_KEY, 0x5E, KEY_WLAN},
300 {KE_KEY, 0x5F, KEY_WLAN},
301 {KE_KEY, 0x60, KEY_SWITCHVIDEOMODE},
Corentin Chary034ce902009-01-20 16:17:43 +0100302 {KE_KEY, 0x61, KEY_SWITCHVIDEOMODE},
Ike Panhc14f8af32009-12-03 07:45:11 +0000303 {KE_KEY, 0x62, KEY_SWITCHVIDEOMODE},
304 {KE_KEY, 0x63, KEY_SWITCHVIDEOMODE},
Corentin Chary16851f92009-12-03 07:45:10 +0000305 {KE_KEY, 0x6B, KEY_F13}, /* Lock Touchpad */
Corentin Chary034ce902009-01-20 16:17:43 +0100306 {KE_KEY, 0x82, KEY_CAMERA},
Corentin Chary1f0233e2009-12-03 07:45:12 +0000307 {KE_KEY, 0x88, KEY_WLAN },
Corentin Chary309f5fb2009-04-27 09:23:42 +0200308 {KE_KEY, 0x8A, KEY_PROG1},
Corentin Chary034ce902009-01-20 16:17:43 +0100309 {KE_KEY, 0x95, KEY_MEDIA},
310 {KE_KEY, 0x99, KEY_PHONE},
Corentin Charydc795262009-08-28 12:56:51 +0000311 {KE_KEY, 0xc4, KEY_KBDILLUMUP},
312 {KE_KEY, 0xc5, KEY_KBDILLUMDOWN},
Corentin Chary034ce902009-01-20 16:17:43 +0100313 {KE_END, 0},
314};
315
Corentin Chary85091b72007-01-26 14:04:30 +0100316/*
317 * This function evaluates an ACPI method, given an int as parameter, the
318 * method is searched within the scope of the handle, can be NULL. The output
319 * of the method is written is output, which can also be NULL
320 *
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100321 * returns 0 if write is successful, -1 else.
Corentin Chary85091b72007-01-26 14:04:30 +0100322 */
Corentin Charyd8c67322009-11-28 10:27:51 +0100323static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
324 struct acpi_buffer *output)
Corentin Chary85091b72007-01-26 14:04:30 +0100325{
Corentin Charybe966662009-08-29 10:28:29 +0200326 struct acpi_object_list params; /* list of input parameters (an int) */
327 union acpi_object in_obj; /* the only param we use */
Corentin Chary85091b72007-01-26 14:04:30 +0100328 acpi_status status;
329
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100330 if (!handle)
331 return 0;
332
Corentin Chary85091b72007-01-26 14:04:30 +0100333 params.count = 1;
334 params.pointer = &in_obj;
335 in_obj.type = ACPI_TYPE_INTEGER;
336 in_obj.integer.value = val;
337
338 status = acpi_evaluate_object(handle, (char *)method, &params, output);
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100339 if (status == AE_OK)
340 return 0;
341 else
342 return -1;
Corentin Chary85091b72007-01-26 14:04:30 +0100343}
344
Corentin Charyd8c67322009-11-28 10:27:51 +0100345static int write_acpi_int(acpi_handle handle, const char *method, int val)
346{
347 return write_acpi_int_ret(handle, method, val, NULL);
348}
349
Len Brown8def05f2007-01-30 01:46:43 -0500350static int read_wireless_status(int mask)
351{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400352 unsigned long long status;
Corentin Chary9a816852007-03-11 10:25:38 +0100353 acpi_status rv = AE_OK;
Corentin Chary4564de12007-01-26 14:04:40 +0100354
355 if (!wireless_status_handle)
Corentin Chary50a90c42009-11-30 21:55:12 +0100356 return (asus->status & mask) ? 1 : 0;
Corentin Chary4564de12007-01-26 14:04:40 +0100357
Corentin Chary9a816852007-03-11 10:25:38 +0100358 rv = acpi_evaluate_integer(wireless_status_handle, NULL, NULL, &status);
359 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200360 pr_warning("Error reading Wireless status\n");
Corentin Chary9a816852007-03-11 10:25:38 +0100361 else
362 return (status & mask) ? 1 : 0;
Corentin Chary4564de12007-01-26 14:04:40 +0100363
Corentin Chary50a90c42009-11-30 21:55:12 +0100364 return (asus->status & mask) ? 1 : 0;
Corentin Chary4564de12007-01-26 14:04:40 +0100365}
366
Corentin Charyf3985322007-05-06 14:48:22 +0200367static int read_gps_status(void)
368{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400369 unsigned long long status;
Corentin Charye539c2f2007-05-06 14:47:06 +0200370 acpi_status rv = AE_OK;
371
372 rv = acpi_evaluate_integer(gps_status_handle, NULL, NULL, &status);
373 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200374 pr_warning("Error reading GPS status\n");
Corentin Charye539c2f2007-05-06 14:47:06 +0200375 else
Corentin Charyf3985322007-05-06 14:48:22 +0200376 return status ? 1 : 0;
Corentin Charye539c2f2007-05-06 14:47:06 +0200377
Corentin Chary50a90c42009-11-30 21:55:12 +0100378 return (asus->status & GPS_ON) ? 1 : 0;
Corentin Charye539c2f2007-05-06 14:47:06 +0200379}
380
Corentin Charybe18cda2007-01-26 14:04:35 +0100381/* Generic LED functions */
382static int read_status(int mask)
383{
Corentin Chary4564de12007-01-26 14:04:40 +0100384 /* There is a special method for both wireless devices */
385 if (mask == BT_ON || mask == WL_ON)
386 return read_wireless_status(mask);
Corentin Charyf3985322007-05-06 14:48:22 +0200387 else if (mask == GPS_ON)
388 return read_gps_status();
Corentin Chary4564de12007-01-26 14:04:40 +0100389
Corentin Chary50a90c42009-11-30 21:55:12 +0100390 return (asus->status & mask) ? 1 : 0;
Corentin Charybe18cda2007-01-26 14:04:35 +0100391}
392
Corentin Chary935ffee2007-03-11 10:26:12 +0100393static void write_status(acpi_handle handle, int out, int mask)
Corentin Charybe18cda2007-01-26 14:04:35 +0100394{
Corentin Chary50a90c42009-11-30 21:55:12 +0100395 asus->status = (out) ? (asus->status | mask) : (asus->status & ~mask);
Corentin Charybe18cda2007-01-26 14:04:35 +0100396
Corentin Chary935ffee2007-03-11 10:26:12 +0100397 switch (mask) {
398 case MLED_ON:
Roel Kluine1af14e2008-02-02 21:07:38 +0100399 out = !(out & 0x1);
Corentin Chary935ffee2007-03-11 10:26:12 +0100400 break;
Corentin Charyfdd8d082007-03-11 10:26:48 +0100401 case GLED_ON:
402 out = (out & 0x1) + 1;
403 break;
Corentin Charye539c2f2007-05-06 14:47:06 +0200404 case GPS_ON:
405 handle = (out) ? gps_on_handle : gps_off_handle;
406 out = 0x02;
407 break;
Corentin Chary935ffee2007-03-11 10:26:12 +0100408 default:
409 out &= 0x1;
410 break;
411 }
Corentin Charybe18cda2007-01-26 14:04:35 +0100412
Corentin Charyd8c67322009-11-28 10:27:51 +0100413 if (write_acpi_int(handle, NULL, out))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200414 pr_warning(" write failed %x\n", mask);
Corentin Charybe18cda2007-01-26 14:04:35 +0100415}
416
417/* /sys/class/led handlers */
Corentin Chary935ffee2007-03-11 10:26:12 +0100418#define ASUS_LED_HANDLER(object, mask) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100419 static void object##_led_set(struct led_classdev *led_cdev, \
420 enum led_brightness value) \
421 { \
Corentin Charye3deda92008-09-24 10:35:55 +0200422 object##_led_wk = (value > 0) ? 1 : 0; \
Corentin Charybe18cda2007-01-26 14:04:35 +0100423 queue_work(led_workqueue, &object##_led_work); \
424 } \
425 static void object##_led_update(struct work_struct *ignored) \
426 { \
427 int value = object##_led_wk; \
Corentin Chary935ffee2007-03-11 10:26:12 +0100428 write_status(object##_set_handle, value, (mask)); \
Corentin Charyabfa57e2009-08-28 12:56:47 +0000429 } \
430 static enum led_brightness object##_led_get( \
431 struct led_classdev *led_cdev) \
432 { \
433 return led_cdev->brightness; \
Corentin Charybe18cda2007-01-26 14:04:35 +0100434 }
435
Corentin Chary935ffee2007-03-11 10:26:12 +0100436ASUS_LED_HANDLER(mled, MLED_ON);
437ASUS_LED_HANDLER(pled, PLED_ON);
438ASUS_LED_HANDLER(rled, RLED_ON);
439ASUS_LED_HANDLER(tled, TLED_ON);
Corentin Charyfdd8d082007-03-11 10:26:48 +0100440ASUS_LED_HANDLER(gled, GLED_ON);
Corentin Charybe18cda2007-01-26 14:04:35 +0100441
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000442/*
443 * Keyboard backlight
444 */
445static int get_kled_lvl(void)
446{
447 unsigned long long kblv;
448 struct acpi_object_list params;
449 union acpi_object in_obj;
450 acpi_status rv;
451
452 params.count = 1;
453 params.pointer = &in_obj;
454 in_obj.type = ACPI_TYPE_INTEGER;
455 in_obj.integer.value = 2;
456
457 rv = acpi_evaluate_integer(kled_get_handle, NULL, &params, &kblv);
458 if (ACPI_FAILURE(rv)) {
459 pr_warning("Error reading kled level\n");
460 return 0;
461 }
462 return kblv;
463}
464
465static int set_kled_lvl(int kblv)
466{
467 if (kblv > 0)
468 kblv = (1 << 7) | (kblv & 0x7F);
469 else
470 kblv = 0;
471
Corentin Charyd8c67322009-11-28 10:27:51 +0100472 if (write_acpi_int(kled_set_handle, NULL, kblv)) {
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000473 pr_warning("Keyboard LED display write failed\n");
474 return -EINVAL;
475 }
476 return 0;
477}
478
479static void kled_led_set(struct led_classdev *led_cdev,
480 enum led_brightness value)
481{
482 kled_led_wk = value;
483 queue_work(led_workqueue, &kled_led_work);
484}
485
486static void kled_led_update(struct work_struct *ignored)
487{
488 set_kled_lvl(kled_led_wk);
489}
490
491static enum led_brightness kled_led_get(struct led_classdev *led_cdev)
492{
493 return get_kled_lvl();
494}
495
Corentin Chary6b7091e2007-01-26 14:04:45 +0100496static int get_lcd_state(void)
497{
498 return read_status(LCD_ON);
499}
500
501static int set_lcd_state(int value)
502{
503 int lcd = 0;
504 acpi_status status = 0;
505
506 lcd = value ? 1 : 0;
507
508 if (lcd == get_lcd_state())
509 return 0;
510
Len Brown8def05f2007-01-30 01:46:43 -0500511 if (lcd_switch_handle) {
Corentin Chary6b7091e2007-01-26 14:04:45 +0100512 status = acpi_evaluate_object(lcd_switch_handle,
513 NULL, NULL, NULL);
514
515 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200516 pr_warning("Error switching LCD\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100517 }
518
Corentin Chary935ffee2007-03-11 10:26:12 +0100519 write_status(NULL, lcd, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100520 return 0;
521}
522
523static void lcd_blank(int blank)
524{
525 struct backlight_device *bd = asus_backlight_device;
526
Len Brown8def05f2007-01-30 01:46:43 -0500527 if (bd) {
Richard Purdie599a52d2007-02-10 23:07:48 +0000528 bd->props.power = blank;
Richard Purdie28ee0862007-02-08 22:25:09 +0000529 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100530 }
531}
532
533static int read_brightness(struct backlight_device *bd)
534{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400535 unsigned long long value;
Corentin Chary9a816852007-03-11 10:25:38 +0100536 acpi_status rv = AE_OK;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100537
Corentin Chary9a816852007-03-11 10:25:38 +0100538 rv = acpi_evaluate_integer(brightness_get_handle, NULL, NULL, &value);
539 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200540 pr_warning("Error reading brightness\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100541
542 return value;
543}
544
545static int set_brightness(struct backlight_device *bd, int value)
546{
Corentin Charyd8c67322009-11-28 10:27:51 +0100547 if (write_acpi_int(brightness_set_handle, NULL, value)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200548 pr_warning("Error changing brightness\n");
Corentin Charye5b50f62009-11-28 10:19:55 +0100549 return -EIO;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100550 }
Corentin Charye5b50f62009-11-28 10:19:55 +0100551 return 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100552}
553
554static int update_bl_status(struct backlight_device *bd)
555{
556 int rv;
Richard Purdie599a52d2007-02-10 23:07:48 +0000557 int value = bd->props.brightness;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100558
559 rv = set_brightness(bd, value);
Len Brown8def05f2007-01-30 01:46:43 -0500560 if (rv)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100561 return rv;
562
Richard Purdie599a52d2007-02-10 23:07:48 +0000563 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100564 return set_lcd_state(value);
565}
566
Corentin Chary85091b72007-01-26 14:04:30 +0100567/*
568 * Platform device handlers
569 */
570
571/*
572 * We write our info in page, we begin at offset off and cannot write more
573 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
574 * number of bytes written in page
575 */
576static ssize_t show_infos(struct device *dev,
Len Brown8def05f2007-01-30 01:46:43 -0500577 struct device_attribute *attr, char *page)
Corentin Chary85091b72007-01-26 14:04:30 +0100578{
579 int len = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400580 unsigned long long temp;
Corentin Charybe966662009-08-29 10:28:29 +0200581 char buf[16]; /* enough for all info */
Corentin Chary9a816852007-03-11 10:25:38 +0100582 acpi_status rv = AE_OK;
583
Corentin Chary85091b72007-01-26 14:04:30 +0100584 /*
585 * We use the easy way, we don't care of off and count, so we don't set eof
586 * to 1
587 */
588
Corentin Chary50a90c42009-11-30 21:55:12 +0100589 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
590 len += sprintf(page + len, "Model reference : %s\n", asus->name);
Corentin Chary85091b72007-01-26 14:04:30 +0100591 /*
592 * The SFUN method probably allows the original driver to get the list
593 * of features supported by a given model. For now, 0x0100 or 0x0800
594 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
595 * The significance of others is yet to be found.
596 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100597 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100598 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000599 len += sprintf(page + len, "SFUN value : %#x\n",
600 (uint) temp);
601 /*
602 * The HWRS method return informations about the hardware.
603 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
604 * The significance of others is yet to be found.
605 * If we don't find the method, we assume the device are present.
606 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100607 rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
Corentin Chary1d4a3802009-08-28 12:56:46 +0000608 if (!ACPI_FAILURE(rv))
609 len += sprintf(page + len, "HRWS value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100610 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100611 /*
612 * Another value for userspace: the ASYM method returns 0x02 for
613 * battery low and 0x04 for battery critical, its readings tend to be
614 * more accurate than those provided by _BST.
615 * Note: since not all the laptops provide this method, errors are
616 * silently ignored.
617 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100618 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100619 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000620 len += sprintf(page + len, "ASYM value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100621 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100622 if (asus_info) {
623 snprintf(buf, 16, "%d", asus_info->length);
624 len += sprintf(page + len, "DSDT length : %s\n", buf);
625 snprintf(buf, 16, "%d", asus_info->checksum);
626 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
627 snprintf(buf, 16, "%d", asus_info->revision);
628 len += sprintf(page + len, "DSDT revision : %s\n", buf);
629 snprintf(buf, 7, "%s", asus_info->oem_id);
630 len += sprintf(page + len, "OEM id : %s\n", buf);
631 snprintf(buf, 9, "%s", asus_info->oem_table_id);
632 len += sprintf(page + len, "OEM table id : %s\n", buf);
633 snprintf(buf, 16, "%x", asus_info->oem_revision);
634 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
635 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
636 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
637 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
638 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
639 }
640
641 return len;
642}
643
644static int parse_arg(const char *buf, unsigned long count, int *val)
645{
646 if (!count)
647 return 0;
648 if (count > 31)
649 return -EINVAL;
650 if (sscanf(buf, "%i", val) != 1)
651 return -EINVAL;
652 return count;
653}
654
Corentin Chary4564de12007-01-26 14:04:40 +0100655static ssize_t store_status(const char *buf, size_t count,
Corentin Chary935ffee2007-03-11 10:26:12 +0100656 acpi_handle handle, int mask)
Corentin Chary4564de12007-01-26 14:04:40 +0100657{
658 int rv, value;
659 int out = 0;
660
661 rv = parse_arg(buf, count, &value);
662 if (rv > 0)
663 out = value ? 1 : 0;
664
Corentin Chary935ffee2007-03-11 10:26:12 +0100665 write_status(handle, out, mask);
Corentin Chary4564de12007-01-26 14:04:40 +0100666
667 return rv;
668}
669
670/*
Corentin Chary722ad972007-01-26 14:04:55 +0100671 * LEDD display
672 */
673static ssize_t show_ledd(struct device *dev,
674 struct device_attribute *attr, char *buf)
675{
Corentin Chary50a90c42009-11-30 21:55:12 +0100676 return sprintf(buf, "0x%08x\n", asus->ledd_status);
Corentin Chary722ad972007-01-26 14:04:55 +0100677}
678
679static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
680 const char *buf, size_t count)
681{
682 int rv, value;
683
684 rv = parse_arg(buf, count, &value);
685 if (rv > 0) {
Corentin Charyd8c67322009-11-28 10:27:51 +0100686 if (write_acpi_int(ledd_set_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200687 pr_warning("LED display write failed\n");
Corentin Chary722ad972007-01-26 14:04:55 +0100688 else
Corentin Chary50a90c42009-11-30 21:55:12 +0100689 asus->ledd_status = (u32) value;
Corentin Chary722ad972007-01-26 14:04:55 +0100690 }
691 return rv;
692}
693
694/*
Corentin Chary4564de12007-01-26 14:04:40 +0100695 * WLAN
696 */
697static ssize_t show_wlan(struct device *dev,
698 struct device_attribute *attr, char *buf)
699{
700 return sprintf(buf, "%d\n", read_status(WL_ON));
701}
702
703static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
704 const char *buf, size_t count)
705{
Corentin Chary935ffee2007-03-11 10:26:12 +0100706 return store_status(buf, count, wl_switch_handle, WL_ON);
Corentin Chary4564de12007-01-26 14:04:40 +0100707}
708
709/*
710 * Bluetooth
711 */
712static ssize_t show_bluetooth(struct device *dev,
713 struct device_attribute *attr, char *buf)
714{
715 return sprintf(buf, "%d\n", read_status(BT_ON));
716}
717
Len Brown8def05f2007-01-30 01:46:43 -0500718static ssize_t store_bluetooth(struct device *dev,
719 struct device_attribute *attr, const char *buf,
720 size_t count)
Corentin Chary4564de12007-01-26 14:04:40 +0100721{
Corentin Chary935ffee2007-03-11 10:26:12 +0100722 return store_status(buf, count, bt_switch_handle, BT_ON);
Corentin Chary4564de12007-01-26 14:04:40 +0100723}
724
Corentin Chary78127b42007-01-26 14:04:49 +0100725/*
726 * Display
727 */
728static void set_display(int value)
729{
730 /* no sanity check needed for now */
Corentin Charyd8c67322009-11-28 10:27:51 +0100731 if (write_acpi_int(display_set_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200732 pr_warning("Error setting display\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100733 return;
734}
735
736static int read_display(void)
737{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400738 unsigned long long value = 0;
Corentin Chary9a816852007-03-11 10:25:38 +0100739 acpi_status rv = AE_OK;
Corentin Chary78127b42007-01-26 14:04:49 +0100740
Corentin Charybe966662009-08-29 10:28:29 +0200741 /*
742 * In most of the case, we know how to set the display, but sometime
743 * we can't read it
744 */
Len Brown8def05f2007-01-30 01:46:43 -0500745 if (display_get_handle) {
Corentin Chary9a816852007-03-11 10:25:38 +0100746 rv = acpi_evaluate_integer(display_get_handle, NULL,
747 NULL, &value);
748 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200749 pr_warning("Error reading display status\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100750 }
751
752 value &= 0x0F; /* needed for some models, shouldn't hurt others */
753
754 return value;
755}
Len Brown8def05f2007-01-30 01:46:43 -0500756
Corentin Chary78127b42007-01-26 14:04:49 +0100757/*
758 * Now, *this* one could be more user-friendly, but so far, no-one has
759 * complained. The significance of bits is the same as in store_disp()
760 */
761static ssize_t show_disp(struct device *dev,
762 struct device_attribute *attr, char *buf)
763{
764 return sprintf(buf, "%d\n", read_display());
765}
766
767/*
768 * Experimental support for display switching. As of now: 1 should activate
769 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
770 * Any combination (bitwise) of these will suffice. I never actually tested 4
771 * displays hooked up simultaneously, so be warned. See the acpi4asus README
772 * for more info.
773 */
774static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
775 const char *buf, size_t count)
776{
777 int rv, value;
778
779 rv = parse_arg(buf, count, &value);
780 if (rv > 0)
781 set_display(value);
782 return rv;
783}
784
Corentin Chary8b857352007-01-26 14:04:58 +0100785/*
786 * Light Sens
787 */
788static void set_light_sens_switch(int value)
789{
Corentin Charyd8c67322009-11-28 10:27:51 +0100790 if (write_acpi_int(ls_switch_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200791 pr_warning("Error setting light sensor switch\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100792 asus->light_switch = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100793}
794
795static ssize_t show_lssw(struct device *dev,
796 struct device_attribute *attr, char *buf)
797{
Corentin Chary50a90c42009-11-30 21:55:12 +0100798 return sprintf(buf, "%d\n", asus->light_switch);
Corentin Chary8b857352007-01-26 14:04:58 +0100799}
800
801static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
802 const char *buf, size_t count)
803{
804 int rv, value;
805
806 rv = parse_arg(buf, count, &value);
807 if (rv > 0)
808 set_light_sens_switch(value ? 1 : 0);
809
810 return rv;
811}
812
813static void set_light_sens_level(int value)
814{
Corentin Charyd8c67322009-11-28 10:27:51 +0100815 if (write_acpi_int(ls_level_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200816 pr_warning("Error setting light sensor level\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100817 asus->light_level = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100818}
819
820static ssize_t show_lslvl(struct device *dev,
821 struct device_attribute *attr, char *buf)
822{
Corentin Chary50a90c42009-11-30 21:55:12 +0100823 return sprintf(buf, "%d\n", asus->light_level);
Corentin Chary8b857352007-01-26 14:04:58 +0100824}
825
826static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
827 const char *buf, size_t count)
828{
829 int rv, value;
830
831 rv = parse_arg(buf, count, &value);
832 if (rv > 0) {
833 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
834 /* 0 <= value <= 15 */
835 set_light_sens_level(value);
836 }
837
838 return rv;
839}
840
Corentin Charye539c2f2007-05-06 14:47:06 +0200841/*
842 * GPS
843 */
844static ssize_t show_gps(struct device *dev,
845 struct device_attribute *attr, char *buf)
846{
847 return sprintf(buf, "%d\n", read_status(GPS_ON));
848}
849
850static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
851 const char *buf, size_t count)
852{
Corentin Charyf3985322007-05-06 14:48:22 +0200853 return store_status(buf, count, NULL, GPS_ON);
Corentin Charye539c2f2007-05-06 14:47:06 +0200854}
855
Corentin Chary034ce902009-01-20 16:17:43 +0100856/*
857 * Hotkey functions
858 */
859static struct key_entry *asus_get_entry_by_scancode(int code)
860{
861 struct key_entry *key;
862
863 for (key = asus_keymap; key->type != KE_END; key++)
864 if (code == key->code)
865 return key;
866
867 return NULL;
868}
869
870static struct key_entry *asus_get_entry_by_keycode(int code)
871{
872 struct key_entry *key;
873
874 for (key = asus_keymap; key->type != KE_END; key++)
875 if (code == key->keycode && key->type == KE_KEY)
876 return key;
877
878 return NULL;
879}
880
881static int asus_getkeycode(struct input_dev *dev, int scancode, int *keycode)
882{
883 struct key_entry *key = asus_get_entry_by_scancode(scancode);
884
885 if (key && key->type == KE_KEY) {
886 *keycode = key->keycode;
887 return 0;
888 }
889
890 return -EINVAL;
891}
892
893static int asus_setkeycode(struct input_dev *dev, int scancode, int keycode)
894{
895 struct key_entry *key;
896 int old_keycode;
897
898 if (keycode < 0 || keycode > KEY_MAX)
899 return -EINVAL;
900
901 key = asus_get_entry_by_scancode(scancode);
902 if (key && key->type == KE_KEY) {
903 old_keycode = key->keycode;
904 key->keycode = keycode;
905 set_bit(keycode, dev->keybit);
906 if (!asus_get_entry_by_keycode(old_keycode))
907 clear_bit(old_keycode, dev->keybit);
908 return 0;
909 }
910
911 return -EINVAL;
912}
913
Corentin Chary600ad522009-11-30 21:42:42 +0100914static void asus_acpi_notify(struct acpi_device *device, u32 event)
Corentin Chary85091b72007-01-26 14:04:30 +0100915{
Corentin Chary034ce902009-01-20 16:17:43 +0100916 static struct key_entry *key;
Corentin Chary6050c8d2009-02-15 19:30:19 +0100917 u16 count;
Corentin Chary034ce902009-01-20 16:17:43 +0100918
Corentin Chary6b7091e2007-01-26 14:04:45 +0100919 /*
920 * We need to tell the backlight device when the backlight power is
921 * switched
922 */
923 if (event == ATKD_LCD_ON) {
Corentin Chary935ffee2007-03-11 10:26:12 +0100924 write_status(NULL, 1, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100925 lcd_blank(FB_BLANK_UNBLANK);
Len Brown8def05f2007-01-30 01:46:43 -0500926 } else if (event == ATKD_LCD_OFF) {
Corentin Chary935ffee2007-03-11 10:26:12 +0100927 write_status(NULL, 0, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100928 lcd_blank(FB_BLANK_POWERDOWN);
929 }
930
Corentin Chary619d8b12009-11-28 10:35:37 +0100931 /* TODO Find a better way to handle events count. */
Corentin Chary50a90c42009-11-30 21:55:12 +0100932 count = asus->event_count[event % 128]++;
933 acpi_bus_generate_proc_event(asus->device, event, count);
934 acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
935 dev_name(&asus->device->dev), event,
Corentin Chary6050c8d2009-02-15 19:30:19 +0100936 count);
Corentin Chary85091b72007-01-26 14:04:30 +0100937
Corentin Chary50a90c42009-11-30 21:55:12 +0100938 if (asus->inputdev) {
Corentin Chary034ce902009-01-20 16:17:43 +0100939 key = asus_get_entry_by_scancode(event);
940 if (!key)
941 return ;
942
943 switch (key->type) {
944 case KE_KEY:
Corentin Chary50a90c42009-11-30 21:55:12 +0100945 input_report_key(asus->inputdev, key->keycode, 1);
946 input_sync(asus->inputdev);
947 input_report_key(asus->inputdev, key->keycode, 0);
948 input_sync(asus->inputdev);
Corentin Chary034ce902009-01-20 16:17:43 +0100949 break;
950 }
951 }
Corentin Chary85091b72007-01-26 14:04:30 +0100952}
953
954#define ASUS_CREATE_DEVICE_ATTR(_name) \
955 struct device_attribute dev_attr_##_name = { \
956 .attr = { \
957 .name = __stringify(_name), \
Tejun Heo7b595752007-06-14 03:45:17 +0900958 .mode = 0 }, \
Corentin Chary85091b72007-01-26 14:04:30 +0100959 .show = NULL, \
960 .store = NULL, \
961 }
962
963#define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
964 do { \
965 dev_attr_##_name.attr.mode = _mode; \
966 dev_attr_##_name.show = _show; \
967 dev_attr_##_name.store = _store; \
968 } while(0)
969
970static ASUS_CREATE_DEVICE_ATTR(infos);
Corentin Chary4564de12007-01-26 14:04:40 +0100971static ASUS_CREATE_DEVICE_ATTR(wlan);
972static ASUS_CREATE_DEVICE_ATTR(bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +0100973static ASUS_CREATE_DEVICE_ATTR(display);
Corentin Chary722ad972007-01-26 14:04:55 +0100974static ASUS_CREATE_DEVICE_ATTR(ledd);
Corentin Chary8b857352007-01-26 14:04:58 +0100975static ASUS_CREATE_DEVICE_ATTR(ls_switch);
976static ASUS_CREATE_DEVICE_ATTR(ls_level);
Corentin Charye539c2f2007-05-06 14:47:06 +0200977static ASUS_CREATE_DEVICE_ATTR(gps);
Corentin Chary85091b72007-01-26 14:04:30 +0100978
979static struct attribute *asuspf_attributes[] = {
Len Brown8def05f2007-01-30 01:46:43 -0500980 &dev_attr_infos.attr,
981 &dev_attr_wlan.attr,
982 &dev_attr_bluetooth.attr,
983 &dev_attr_display.attr,
984 &dev_attr_ledd.attr,
985 &dev_attr_ls_switch.attr,
986 &dev_attr_ls_level.attr,
Corentin Charye539c2f2007-05-06 14:47:06 +0200987 &dev_attr_gps.attr,
Len Brown8def05f2007-01-30 01:46:43 -0500988 NULL
Corentin Chary85091b72007-01-26 14:04:30 +0100989};
990
Corentin Chary600ad522009-11-30 21:42:42 +0100991static struct attribute_group platform_attribute_group = {
Len Brown8def05f2007-01-30 01:46:43 -0500992 .attrs = asuspf_attributes
Corentin Chary85091b72007-01-26 14:04:30 +0100993};
994
Corentin Chary600ad522009-11-30 21:42:42 +0100995static int asus_platform_init(void)
996{
997 int result;
998
Corentin Chary50a90c42009-11-30 21:55:12 +0100999 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1000 if (!asus->platform_device)
Corentin Chary600ad522009-11-30 21:42:42 +01001001 return -ENOMEM;
1002
Corentin Chary50a90c42009-11-30 21:55:12 +01001003 result = platform_device_add(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001004 if (result)
1005 goto fail_platform_device;
1006
Corentin Chary50a90c42009-11-30 21:55:12 +01001007 result = sysfs_create_group(&asus->platform_device->dev.kobj,
Corentin Chary600ad522009-11-30 21:42:42 +01001008 &platform_attribute_group);
1009 if (result)
1010 goto fail_sysfs;
1011 return 0;
1012
1013fail_sysfs:
Corentin Chary50a90c42009-11-30 21:55:12 +01001014 platform_device_del(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001015fail_platform_device:
Corentin Chary50a90c42009-11-30 21:55:12 +01001016 platform_device_put(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001017 return result;
1018}
1019
1020static void asus_platform_exit(void)
1021{
Corentin Chary50a90c42009-11-30 21:55:12 +01001022 sysfs_remove_group(&asus->platform_device->dev.kobj,
Corentin Chary600ad522009-11-30 21:42:42 +01001023 &platform_attribute_group);
Corentin Chary50a90c42009-11-30 21:55:12 +01001024 platform_device_unregister(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001025}
1026
1027static struct platform_driver platform_driver = {
Len Brown8def05f2007-01-30 01:46:43 -05001028 .driver = {
Corentin Chary50a90c42009-11-30 21:55:12 +01001029 .name = ASUS_LAPTOP_FILE,
Len Brown8def05f2007-01-30 01:46:43 -05001030 .owner = THIS_MODULE,
1031 }
Corentin Chary85091b72007-01-26 14:04:30 +01001032};
1033
Corentin Chary50a90c42009-11-30 21:55:12 +01001034static void asus_laptop_add_fs(void)
Corentin Chary85091b72007-01-26 14:04:30 +01001035{
1036 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
Corentin Chary4564de12007-01-26 14:04:40 +01001037
1038 if (wl_switch_handle)
1039 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
1040
1041 if (bt_switch_handle)
1042 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
1043 show_bluetooth, store_bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +01001044
1045 if (display_set_handle && display_get_handle)
1046 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
Len Brown8def05f2007-01-30 01:46:43 -05001047 else if (display_set_handle)
Corentin Chary78127b42007-01-26 14:04:49 +01001048 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
1049
Corentin Chary722ad972007-01-26 14:04:55 +01001050 if (ledd_set_handle)
1051 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
1052
Corentin Chary8b857352007-01-26 14:04:58 +01001053 if (ls_switch_handle && ls_level_handle) {
1054 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
1055 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
1056 }
Corentin Charye539c2f2007-05-06 14:47:06 +02001057
Corentin Charyf3985322007-05-06 14:48:22 +02001058 if (gps_status_handle && gps_on_handle && gps_off_handle)
Corentin Charye539c2f2007-05-06 14:47:06 +02001059 ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
Corentin Chary85091b72007-01-26 14:04:30 +01001060}
1061
Len Brown8def05f2007-01-30 01:46:43 -05001062static int asus_handle_init(char *name, acpi_handle * handle,
Corentin Chary85091b72007-01-26 14:04:30 +01001063 char **paths, int num_paths)
1064{
1065 int i;
1066 acpi_status status;
1067
1068 for (i = 0; i < num_paths; i++) {
1069 status = acpi_get_handle(NULL, paths[i], handle);
1070 if (ACPI_SUCCESS(status))
1071 return 0;
1072 }
1073
1074 *handle = NULL;
1075 return -ENODEV;
1076}
1077
1078#define ASUS_HANDLE_INIT(object) \
1079 asus_handle_init(#object, &object##_handle, object##_paths, \
1080 ARRAY_SIZE(object##_paths))
1081
Corentin Chary85091b72007-01-26 14:04:30 +01001082/*
Corentin Chary50a90c42009-11-30 21:55:12 +01001083 * This function is used to initialize the context with right values. In this
1084 * method, we can make all the detection we want, and modify the asus_laptop
1085 * struct
Corentin Chary85091b72007-01-26 14:04:30 +01001086 */
Corentin Chary50a90c42009-11-30 21:55:12 +01001087static int asus_laptop_get_info(void)
Corentin Chary85091b72007-01-26 14:04:30 +01001088{
1089 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Corentin Chary85091b72007-01-26 14:04:30 +01001090 union acpi_object *model = NULL;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001091 unsigned long long bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +01001092 char *string = NULL;
1093 acpi_status status;
1094
1095 /*
1096 * Get DSDT headers early enough to allow for differentiating between
1097 * models, but late enough to allow acpi_bus_register_driver() to fail
1098 * before doing anything ACPI-specific. Should we encounter a machine,
1099 * which needs special handling (i.e. its hotkey device has a different
1100 * HID), this bit will be moved. A global variable asus_info contains
1101 * the DSDT header.
1102 */
Len Brown894d79b2007-02-03 02:13:53 -05001103 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
Corentin Chary85091b72007-01-26 14:04:30 +01001104 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001105 pr_warning("Couldn't get the DSDT table header\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001106
1107 /* We have to write 0 on init this far for all ASUS models */
Corentin Chary50a90c42009-11-30 21:55:12 +01001108 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001109 pr_err("Hotkey initialization failed\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001110 return -ENODEV;
1111 }
1112
1113 /* This needs to be called for some laptops to init properly */
Corentin Chary9a816852007-03-11 10:25:38 +01001114 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001115 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
Corentin Chary9a816852007-03-11 10:25:38 +01001116 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001117 pr_warning("Error calling BSTS\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001118 else if (bsts_result)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001119 pr_notice("BSTS called, 0x%02x returned\n",
Corentin Chary9a816852007-03-11 10:25:38 +01001120 (uint) bsts_result);
Corentin Chary85091b72007-01-26 14:04:30 +01001121
Corentin Chary185e5af2007-03-11 10:27:33 +01001122 /* This too ... */
Corentin Chary50a90c42009-11-30 21:55:12 +01001123 write_acpi_int(asus->handle, "CWAP", wapf);
Corentin Chary185e5af2007-03-11 10:27:33 +01001124
Corentin Chary85091b72007-01-26 14:04:30 +01001125 /*
1126 * Try to match the object returned by INIT to the specific model.
1127 * Handle every possible object (or the lack of thereof) the DSDT
1128 * writers might throw at us. When in trouble, we pass NULL to
1129 * asus_model_match() and try something completely different.
1130 */
1131 if (buffer.pointer) {
1132 model = buffer.pointer;
1133 switch (model->type) {
1134 case ACPI_TYPE_STRING:
1135 string = model->string.pointer;
1136 break;
1137 case ACPI_TYPE_BUFFER:
1138 string = model->buffer.pointer;
1139 break;
1140 default:
1141 string = "";
1142 break;
1143 }
1144 }
Corentin Chary50a90c42009-11-30 21:55:12 +01001145 asus->name = kstrdup(string, GFP_KERNEL);
1146 if (!asus->name)
Corentin Chary85091b72007-01-26 14:04:30 +01001147 return -ENOMEM;
1148
Len Brown8def05f2007-01-30 01:46:43 -05001149 if (*string)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001150 pr_notice(" %s model detected\n", string);
Corentin Chary85091b72007-01-26 14:04:30 +01001151
Corentin Charybe18cda2007-01-26 14:04:35 +01001152 ASUS_HANDLE_INIT(mled_set);
1153 ASUS_HANDLE_INIT(tled_set);
1154 ASUS_HANDLE_INIT(rled_set);
1155 ASUS_HANDLE_INIT(pled_set);
Corentin Charyfdd8d082007-03-11 10:26:48 +01001156 ASUS_HANDLE_INIT(gled_set);
Corentin Charybe18cda2007-01-26 14:04:35 +01001157
Corentin Chary722ad972007-01-26 14:04:55 +01001158 ASUS_HANDLE_INIT(ledd_set);
1159
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001160 ASUS_HANDLE_INIT(kled_set);
1161 ASUS_HANDLE_INIT(kled_get);
1162
Corentin Chary4564de12007-01-26 14:04:40 +01001163 /*
1164 * The HWRS method return informations about the hardware.
1165 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1166 * The significance of others is yet to be found.
1167 * If we don't find the method, we assume the device are present.
1168 */
Corentin Chary9a816852007-03-11 10:25:38 +01001169 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001170 acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
Corentin Chary9a816852007-03-11 10:25:38 +01001171 if (ACPI_FAILURE(status))
Corentin Chary4564de12007-01-26 14:04:40 +01001172 hwrs_result = WL_HWRS | BT_HWRS;
1173
Len Brown8def05f2007-01-30 01:46:43 -05001174 if (hwrs_result & WL_HWRS)
Corentin Chary4564de12007-01-26 14:04:40 +01001175 ASUS_HANDLE_INIT(wl_switch);
Len Brown8def05f2007-01-30 01:46:43 -05001176 if (hwrs_result & BT_HWRS)
Corentin Chary4564de12007-01-26 14:04:40 +01001177 ASUS_HANDLE_INIT(bt_switch);
1178
1179 ASUS_HANDLE_INIT(wireless_status);
1180
Corentin Chary6b7091e2007-01-26 14:04:45 +01001181 ASUS_HANDLE_INIT(brightness_set);
1182 ASUS_HANDLE_INIT(brightness_get);
1183
1184 ASUS_HANDLE_INIT(lcd_switch);
1185
Corentin Chary78127b42007-01-26 14:04:49 +01001186 ASUS_HANDLE_INIT(display_set);
1187 ASUS_HANDLE_INIT(display_get);
1188
Corentin Charybe966662009-08-29 10:28:29 +02001189 /*
1190 * There is a lot of models with "ALSL", but a few get
1191 * a real light sens, so we need to check it.
1192 */
Corentin Chary832d9952007-05-06 14:47:29 +02001193 if (!ASUS_HANDLE_INIT(ls_switch))
Corentin Chary8b857352007-01-26 14:04:58 +01001194 ASUS_HANDLE_INIT(ls_level);
1195
Corentin Charye539c2f2007-05-06 14:47:06 +02001196 ASUS_HANDLE_INIT(gps_on);
1197 ASUS_HANDLE_INIT(gps_off);
1198 ASUS_HANDLE_INIT(gps_status);
1199
Corentin Chary85091b72007-01-26 14:04:30 +01001200 kfree(model);
1201
1202 return AE_OK;
1203}
1204
Corentin Chary600ad522009-11-30 21:42:42 +01001205static int asus_input_init(struct device *dev)
Corentin Chary034ce902009-01-20 16:17:43 +01001206{
1207 const struct key_entry *key;
1208 int result;
1209
Corentin Chary50a90c42009-11-30 21:55:12 +01001210 asus->inputdev = input_allocate_device();
1211 if (!asus->inputdev) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001212 pr_info("Unable to allocate input device\n");
Corentin Chary034ce902009-01-20 16:17:43 +01001213 return 0;
1214 }
Corentin Chary50a90c42009-11-30 21:55:12 +01001215 asus->inputdev->name = "Asus Laptop extra buttons";
1216 asus->inputdev->dev.parent = dev;
1217 asus->inputdev->phys = ASUS_LAPTOP_FILE "/input0";
1218 asus->inputdev->id.bustype = BUS_HOST;
1219 asus->inputdev->getkeycode = asus_getkeycode;
1220 asus->inputdev->setkeycode = asus_setkeycode;
Corentin Chary034ce902009-01-20 16:17:43 +01001221
1222 for (key = asus_keymap; key->type != KE_END; key++) {
1223 switch (key->type) {
1224 case KE_KEY:
Corentin Chary50a90c42009-11-30 21:55:12 +01001225 set_bit(EV_KEY, asus->inputdev->evbit);
1226 set_bit(key->keycode, asus->inputdev->keybit);
Corentin Chary034ce902009-01-20 16:17:43 +01001227 break;
1228 }
1229 }
Corentin Chary50a90c42009-11-30 21:55:12 +01001230 result = input_register_device(asus->inputdev);
Corentin Chary034ce902009-01-20 16:17:43 +01001231 if (result) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001232 pr_info("Unable to register input device\n");
Corentin Chary50a90c42009-11-30 21:55:12 +01001233 input_free_device(asus->inputdev);
Corentin Chary034ce902009-01-20 16:17:43 +01001234 }
1235 return result;
1236}
1237
Corentin Chary6b7091e2007-01-26 14:04:45 +01001238static void asus_backlight_exit(void)
1239{
Len Brown8def05f2007-01-30 01:46:43 -05001240 if (asus_backlight_device)
Corentin Chary6b7091e2007-01-26 14:04:45 +01001241 backlight_device_unregister(asus_backlight_device);
1242}
1243
Corentin Charybe18cda2007-01-26 14:04:35 +01001244#define ASUS_LED_UNREGISTER(object) \
Guillaume Chazaraine1996a692007-08-16 18:18:53 +02001245 if (object##_led.dev) \
1246 led_classdev_unregister(&object##_led)
Corentin Charybe18cda2007-01-26 14:04:35 +01001247
1248static void asus_led_exit(void)
1249{
Al Viro3b0d7112007-07-23 11:21:34 +01001250 destroy_workqueue(led_workqueue);
Corentin Charybe18cda2007-01-26 14:04:35 +01001251 ASUS_LED_UNREGISTER(mled);
1252 ASUS_LED_UNREGISTER(tled);
1253 ASUS_LED_UNREGISTER(pled);
1254 ASUS_LED_UNREGISTER(rled);
Corentin Charyfdd8d082007-03-11 10:26:48 +01001255 ASUS_LED_UNREGISTER(gled);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001256 ASUS_LED_UNREGISTER(kled);
Corentin Charybe18cda2007-01-26 14:04:35 +01001257}
1258
Corentin Chary034ce902009-01-20 16:17:43 +01001259static void asus_input_exit(void)
1260{
Corentin Chary50a90c42009-11-30 21:55:12 +01001261 if (asus->inputdev)
1262 input_unregister_device(asus->inputdev);
Corentin Chary034ce902009-01-20 16:17:43 +01001263}
1264
Len Brown8def05f2007-01-30 01:46:43 -05001265static int asus_backlight_init(struct device *dev)
Corentin Chary6b7091e2007-01-26 14:04:45 +01001266{
1267 struct backlight_device *bd;
1268
Len Brown8def05f2007-01-30 01:46:43 -05001269 if (brightness_set_handle && lcd_switch_handle) {
Corentin Chary50a90c42009-11-30 21:55:12 +01001270 bd = backlight_device_register(ASUS_LAPTOP_FILE, dev,
Richard Purdie599a52d2007-02-10 23:07:48 +00001271 NULL, &asusbl_ops);
Len Brown8def05f2007-01-30 01:46:43 -05001272 if (IS_ERR(bd)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001273 pr_err("Could not register asus backlight device\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +01001274 asus_backlight_device = NULL;
1275 return PTR_ERR(bd);
1276 }
1277
1278 asus_backlight_device = bd;
1279
Richard Purdie599a52d2007-02-10 23:07:48 +00001280 bd->props.max_brightness = 15;
1281 bd->props.brightness = read_brightness(NULL);
1282 bd->props.power = FB_BLANK_UNBLANK;
Richard Purdie28ee0862007-02-08 22:25:09 +00001283 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001284 }
1285 return 0;
1286}
1287
Corentin Charybe18cda2007-01-26 14:04:35 +01001288static int asus_led_register(acpi_handle handle,
Len Brown8def05f2007-01-30 01:46:43 -05001289 struct led_classdev *ldev, struct device *dev)
Corentin Charybe18cda2007-01-26 14:04:35 +01001290{
Len Brown8def05f2007-01-30 01:46:43 -05001291 if (!handle)
Corentin Charybe18cda2007-01-26 14:04:35 +01001292 return 0;
1293
1294 return led_classdev_register(dev, ldev);
1295}
Len Brown8def05f2007-01-30 01:46:43 -05001296
Corentin Charybe18cda2007-01-26 14:04:35 +01001297#define ASUS_LED_REGISTER(object, device) \
1298 asus_led_register(object##_set_handle, &object##_led, device)
1299
Len Brown8def05f2007-01-30 01:46:43 -05001300static int asus_led_init(struct device *dev)
Corentin Charybe18cda2007-01-26 14:04:35 +01001301{
1302 int rv;
1303
1304 rv = ASUS_LED_REGISTER(mled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001305 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001306 goto out;
Corentin Charybe18cda2007-01-26 14:04:35 +01001307
1308 rv = ASUS_LED_REGISTER(tled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001309 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001310 goto out1;
Corentin Charybe18cda2007-01-26 14:04:35 +01001311
1312 rv = ASUS_LED_REGISTER(rled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001313 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001314 goto out2;
Corentin Charybe18cda2007-01-26 14:04:35 +01001315
1316 rv = ASUS_LED_REGISTER(pled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001317 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001318 goto out3;
Corentin Charybe18cda2007-01-26 14:04:35 +01001319
Corentin Charyfdd8d082007-03-11 10:26:48 +01001320 rv = ASUS_LED_REGISTER(gled, dev);
1321 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001322 goto out4;
Corentin Charyfdd8d082007-03-11 10:26:48 +01001323
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001324 if (kled_set_handle && kled_get_handle)
1325 rv = ASUS_LED_REGISTER(kled, dev);
1326 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001327 goto out5;
Corentin Charybe18cda2007-01-26 14:04:35 +01001328
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001329 led_workqueue = create_singlethread_workqueue("led_workqueue");
1330 if (!led_workqueue)
1331 goto out6;
1332
Corentin Charybe18cda2007-01-26 14:04:35 +01001333 return 0;
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001334out6:
Al Viro3b0d7112007-07-23 11:21:34 +01001335 rv = -ENOMEM;
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001336 ASUS_LED_UNREGISTER(kled);
1337out5:
Al Viro3b0d7112007-07-23 11:21:34 +01001338 ASUS_LED_UNREGISTER(gled);
1339out4:
1340 ASUS_LED_UNREGISTER(pled);
1341out3:
1342 ASUS_LED_UNREGISTER(rled);
1343out2:
1344 ASUS_LED_UNREGISTER(tled);
1345out1:
1346 ASUS_LED_UNREGISTER(mled);
1347out:
1348 return rv;
Corentin Charybe18cda2007-01-26 14:04:35 +01001349}
1350
Corentin Chary600ad522009-11-30 21:42:42 +01001351
1352static bool asus_device_present;
1353
1354static int __devinit asus_acpi_init(struct acpi_device *device)
1355{
1356 int result = 0;
1357
Corentin Chary50a90c42009-11-30 21:55:12 +01001358 result = acpi_bus_get_status(asus->device);
Corentin Chary600ad522009-11-30 21:42:42 +01001359 if (result)
1360 return result;
Corentin Chary50a90c42009-11-30 21:55:12 +01001361 if (!asus->device->status.present) {
Corentin Chary600ad522009-11-30 21:42:42 +01001362 pr_err("Hotkey device not present, aborting\n");
1363 return -ENODEV;
1364 }
1365
Corentin Chary50a90c42009-11-30 21:55:12 +01001366 result = asus_laptop_get_info();
Corentin Chary600ad522009-11-30 21:42:42 +01001367 if (result)
1368 return result;
1369
Corentin Chary50a90c42009-11-30 21:55:12 +01001370 asus_laptop_add_fs();
Corentin Chary600ad522009-11-30 21:42:42 +01001371
1372 /* WLED and BLED are on by default */
1373 write_status(bt_switch_handle, 1, BT_ON);
1374 write_status(wl_switch_handle, 1, WL_ON);
1375
1376 /* If the h/w switch is off, we need to check the real status */
1377 write_status(NULL, read_status(BT_ON), BT_ON);
1378 write_status(NULL, read_status(WL_ON), WL_ON);
1379
1380 /* LCD Backlight is on by default */
1381 write_status(NULL, 1, LCD_ON);
1382
1383 /* Keyboard Backlight is on by default */
1384 if (kled_set_handle)
1385 set_kled_lvl(1);
1386
1387 /* LED display is off by default */
Corentin Chary50a90c42009-11-30 21:55:12 +01001388 asus->ledd_status = 0xFFF;
Corentin Chary600ad522009-11-30 21:42:42 +01001389
1390 /* Set initial values of light sensor and level */
1391 hotk->light_switch = 0; /* Default to light sensor disabled */
1392 hotk->light_level = 5; /* level 5 for sensor sensitivity */
1393
1394 if (ls_switch_handle)
Corentin Chary50a90c42009-11-30 21:55:12 +01001395 set_light_sens_switch(asus->light_switch);
Corentin Chary600ad522009-11-30 21:42:42 +01001396
1397 if (ls_level_handle)
Corentin Chary50a90c42009-11-30 21:55:12 +01001398 set_light_sens_level(asus->light_level);
Corentin Chary600ad522009-11-30 21:42:42 +01001399
1400 /* GPS is on by default */
1401 write_status(NULL, 1, GPS_ON);
1402 return result;
1403}
1404
1405static int __devinit asus_acpi_add(struct acpi_device *device)
1406{
1407 int result;
1408
1409 pr_notice("Asus Laptop Support version %s\n",
1410 ASUS_LAPTOP_VERSION);
Corentin Chary50a90c42009-11-30 21:55:12 +01001411 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1412 if (!asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001413 return -ENOMEM;
Corentin Chary50a90c42009-11-30 21:55:12 +01001414 asus->handle = device->handle;
1415 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1416 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1417 device->driver_data = asus;
1418 asus->device = device;
Corentin Chary600ad522009-11-30 21:42:42 +01001419
1420 result = asus_acpi_init(device);
1421 if (result)
1422 goto fail_platform;
1423
1424 /*
1425 * Register the platform device first. It is used as a parent for the
1426 * sub-devices below.
1427 */
1428 result = asus_platform_init();
1429 if (result)
1430 goto fail_platform;
1431
1432 if (!acpi_video_backlight_support()) {
Corentin Chary50a90c42009-11-30 21:55:12 +01001433 result = asus_backlight_init(&asus->platform_device->dev);
Corentin Chary600ad522009-11-30 21:42:42 +01001434 if (result)
1435 goto fail_backlight;
1436 } else
1437 pr_info("Backlight controlled by ACPI video driver\n");
1438
Corentin Chary50a90c42009-11-30 21:55:12 +01001439 result = asus_input_init(&asus->platform_device->dev);
Corentin Chary600ad522009-11-30 21:42:42 +01001440 if (result)
1441 goto fail_input;
1442
Corentin Chary50a90c42009-11-30 21:55:12 +01001443 result = asus_led_init(&asus->platform_device->dev);
Corentin Chary600ad522009-11-30 21:42:42 +01001444 if (result)
1445 goto fail_led;
1446
1447 asus_device_present = true;
1448 return 0;
1449
1450fail_led:
1451 asus_input_exit();
1452fail_input:
1453 asus_backlight_exit();
1454fail_backlight:
1455 asus_platform_exit();
1456fail_platform:
Corentin Chary50a90c42009-11-30 21:55:12 +01001457 kfree(asus->name);
1458 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001459
1460 return result;
1461}
1462
1463static int asus_acpi_remove(struct acpi_device *device, int type)
1464{
1465 asus_backlight_exit();
1466 asus_led_exit();
1467 asus_input_exit();
1468 asus_platform_exit();
1469
Corentin Chary50a90c42009-11-30 21:55:12 +01001470 kfree(asus->name);
1471 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001472 return 0;
1473}
1474
1475static const struct acpi_device_id asus_device_ids[] = {
1476 {"ATK0100", 0},
1477 {"ATK0101", 0},
1478 {"", 0},
1479};
1480MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1481
1482static struct acpi_driver asus_acpi_driver = {
Corentin Chary50a90c42009-11-30 21:55:12 +01001483 .name = ASUS_LAPTOP_NAME,
1484 .class = ASUS_LAPTOP_CLASS,
Corentin Chary600ad522009-11-30 21:42:42 +01001485 .owner = THIS_MODULE,
1486 .ids = asus_device_ids,
1487 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1488 .ops = {
1489 .add = asus_acpi_add,
1490 .remove = asus_acpi_remove,
1491 .notify = asus_acpi_notify,
1492 },
1493};
1494
Corentin Chary85091b72007-01-26 14:04:30 +01001495static int __init asus_laptop_init(void)
1496{
1497 int result;
1498
Corentin Chary600ad522009-11-30 21:42:42 +01001499 result = platform_driver_register(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001500 if (result < 0)
1501 return result;
1502
Corentin Chary600ad522009-11-30 21:42:42 +01001503 result = acpi_bus_register_driver(&asus_acpi_driver);
1504 if (result < 0)
1505 goto fail_acpi_driver;
1506 if (!asus_device_present) {
1507 result = -ENODEV;
1508 goto fail_no_device;
Corentin Chary85091b72007-01-26 14:04:30 +01001509 }
Len Brown8def05f2007-01-30 01:46:43 -05001510 return 0;
Corentin Chary85091b72007-01-26 14:04:30 +01001511
Corentin Chary600ad522009-11-30 21:42:42 +01001512fail_no_device:
1513 acpi_bus_unregister_driver(&asus_acpi_driver);
1514fail_acpi_driver:
1515 platform_driver_unregister(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001516 return result;
1517}
1518
Corentin Chary600ad522009-11-30 21:42:42 +01001519static void __exit asus_laptop_exit(void)
1520{
1521 acpi_bus_unregister_driver(&asus_acpi_driver);
1522 platform_driver_unregister(&platform_driver);
1523}
1524
Corentin Chary85091b72007-01-26 14:04:30 +01001525module_init(asus_laptop_init);
1526module_exit(asus_laptop_exit);