blob: 7058cb56f3385817acc7cc4e0c652177632bf02c [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 Charyf3985322007-05-06 14:48:22 +020053#define ASUS_LAPTOP_VERSION "0.42"
Corentin Chary85091b72007-01-26 14:04:30 +010054
55#define ASUS_HOTK_NAME "Asus Laptop Support"
56#define ASUS_HOTK_CLASS "hotkey"
57#define ASUS_HOTK_DEVICE_NAME "Hotkey"
Corentin Chary2fcc23d2009-06-19 14:52:03 +020058#define ASUS_HOTK_FILE KBUILD_MODNAME
Corentin Chary85091b72007-01-26 14:04:30 +010059#define ASUS_HOTK_PREFIX "\\_SB.ATKD."
60
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 */
65#define ATKD_BR_UP 0x10
66#define ATKD_BR_DOWN 0x20
67#define ATKD_LCD_ON 0x33
68#define ATKD_LCD_OFF 0x34
69
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 Chary4564de12007-01-26 14:04:40 +010073#define WL_HWRS 0x80
74#define BT_HWRS 0x100
75
76/*
77 * Flags for hotk status
78 * WL_ON and BT_ON are also used for wireless_status()
79 */
Corentin Charybe966662009-08-29 10:28:29 +020080#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 Charybe18cda2007-01-26 14:04:35 +010090
Corentin Chary85091b72007-01-26 14:04:30 +010091#define ASUS_LOG ASUS_HOTK_FILE ": "
92#define ASUS_ERR KERN_ERR ASUS_LOG
93#define ASUS_WARNING KERN_WARNING ASUS_LOG
94#define ASUS_NOTICE KERN_NOTICE ASUS_LOG
95#define ASUS_INFO KERN_INFO ASUS_LOG
96#define ASUS_DEBUG KERN_DEBUG ASUS_LOG
97
98MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
99MODULE_DESCRIPTION(ASUS_HOTK_NAME);
100MODULE_LICENSE("GPL");
101
Corentin Charybe966662009-08-29 10:28:29 +0200102/*
103 * WAPF defines the behavior of the Fn+Fx wlan key
Corentin Chary185e5af2007-03-11 10:27:33 +0100104 * The significance of values is yet to be found, but
105 * most of the time:
106 * 0x0 will do nothing
107 * 0x1 will allow to control the device with Fn+Fx key.
108 * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
109 * 0x5 like 0x1 or 0x4
110 * So, if something doesn't work as you want, just try other values =)
111 */
112static uint wapf = 1;
113module_param(wapf, uint, 0644);
114MODULE_PARM_DESC(wapf, "WAPF value");
115
Corentin Chary0e875f42010-01-10 20:49:26 +0100116static uint wireless_status = 1;
117static uint bluetooth_status = 1;
118
119module_param(wireless_status, uint, 0644);
120MODULE_PARM_DESC(wireless_status, "Set the wireless status on boot "
121 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
122 "default is 1");
123
124module_param(bluetooth_status, uint, 0644);
125MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
126 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
127 "default is 1");
128
Corentin Chary85091b72007-01-26 14:04:30 +0100129#define ASUS_HANDLE(object, paths...) \
130 static acpi_handle object##_handle = NULL; \
131 static char *object##_paths[] = { paths }
132
Corentin Charybe18cda2007-01-26 14:04:35 +0100133/* LED */
134ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
135ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
Len Brown8def05f2007-01-30 01:46:43 -0500136ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
137ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
Corentin Charyfdd8d082007-03-11 10:26:48 +0100138ASUS_HANDLE(gled_set, ASUS_HOTK_PREFIX "GLED"); /* G1, G2 (probably) */
Corentin Charybe18cda2007-01-26 14:04:35 +0100139
Corentin Chary722ad972007-01-26 14:04:55 +0100140/* LEDD */
141ASUS_HANDLE(ledd_set, ASUS_HOTK_PREFIX "SLCM");
142
Corentin Charybe966662009-08-29 10:28:29 +0200143/*
144 * Bluetooth and WLAN
Corentin Chary4564de12007-01-26 14:04:40 +0100145 * WLED and BLED are not handled like other XLED, because in some dsdt
146 * they also control the WLAN/Bluetooth device.
147 */
148ASUS_HANDLE(wl_switch, ASUS_HOTK_PREFIX "WLED");
149ASUS_HANDLE(bt_switch, ASUS_HOTK_PREFIX "BLED");
Len Brown8def05f2007-01-30 01:46:43 -0500150ASUS_HANDLE(wireless_status, ASUS_HOTK_PREFIX "RSTS"); /* All new models */
Corentin Chary4564de12007-01-26 14:04:40 +0100151
Corentin Chary6b7091e2007-01-26 14:04:45 +0100152/* Brightness */
153ASUS_HANDLE(brightness_set, ASUS_HOTK_PREFIX "SPLV");
154ASUS_HANDLE(brightness_get, ASUS_HOTK_PREFIX "GPLV");
155
156/* Backlight */
Len Brown8def05f2007-01-30 01:46:43 -0500157ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
158 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
159 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
160 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
161 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
Torsten Krah4d0b856e2008-10-17 09:47:57 +0200162 "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
Len Brown8def05f2007-01-30 01:46:43 -0500163 "\\_SB.PCI0.PX40.Q10", /* S1x */
164 "\\Q10"); /* A2x, L2D, L3D, M2E */
Corentin Chary6b7091e2007-01-26 14:04:45 +0100165
Corentin Chary78127b42007-01-26 14:04:49 +0100166/* Display */
167ASUS_HANDLE(display_set, ASUS_HOTK_PREFIX "SDSP");
Corentin Charybe966662009-08-29 10:28:29 +0200168ASUS_HANDLE(display_get,
169 /* A6B, A6K A6R A7D F3JM L4R M6R A3G M6A M6V VX-1 V6J V6V W3Z */
170 "\\_SB.PCI0.P0P1.VGA.GETD",
171 /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V S5A M5A z33A W1Jc W2V G1 */
172 "\\_SB.PCI0.P0P2.VGA.GETD",
173 /* A6V A6Q */
174 "\\_SB.PCI0.P0P3.VGA.GETD",
175 /* A6T, A6M */
176 "\\_SB.PCI0.P0PA.VGA.GETD",
177 /* L3C */
178 "\\_SB.PCI0.PCI1.VGAC.NMAP",
179 /* Z96F */
180 "\\_SB.PCI0.VGA.GETD",
181 /* A2D */
182 "\\ACTD",
183 /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
184 "\\ADVG",
185 /* P30 */
186 "\\DNXT",
187 /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
188 "\\INFB",
189 /* A3F A6F A3N A3L M6N W3N W6A */
190 "\\SSTE");
Corentin Chary78127b42007-01-26 14:04:49 +0100191
Corentin Charybe966662009-08-29 10:28:29 +0200192ASUS_HANDLE(ls_switch, ASUS_HOTK_PREFIX "ALSC"); /* Z71A Z71V */
193ASUS_HANDLE(ls_level, ASUS_HOTK_PREFIX "ALSL"); /* Z71A Z71V */
Corentin Chary8b857352007-01-26 14:04:58 +0100194
Corentin Charye539c2f2007-05-06 14:47:06 +0200195/* GPS */
196/* R2H use different handle for GPS on/off */
Corentin Charyf3985322007-05-06 14:48:22 +0200197ASUS_HANDLE(gps_on, ASUS_HOTK_PREFIX "SDON"); /* R2H */
198ASUS_HANDLE(gps_off, ASUS_HOTK_PREFIX "SDOF"); /* R2H */
Corentin Charye539c2f2007-05-06 14:47:06 +0200199ASUS_HANDLE(gps_status, ASUS_HOTK_PREFIX "GPST");
200
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000201/* Keyboard light */
202ASUS_HANDLE(kled_set, ASUS_HOTK_PREFIX "SLKB");
203ASUS_HANDLE(kled_get, ASUS_HOTK_PREFIX "GLKB");
204
Corentin Chary85091b72007-01-26 14:04:30 +0100205/*
206 * This is the main structure, we can use it to store anything interesting
207 * about the hotk device
208 */
209struct asus_hotk {
Corentin Charybe966662009-08-29 10:28:29 +0200210 char *name; /* laptop name */
211 struct acpi_device *device; /* the device we are in */
212 acpi_handle handle; /* the handle of the hotk device */
213 char status; /* status of the hotk, for LEDs, ... */
214 u32 ledd_status; /* status of the LED display */
215 u8 light_level; /* light sensor level */
216 u8 light_switch; /* light sensor switch value */
217 u16 event_count[128]; /* count for each event TODO make this better */
Corentin Chary034ce902009-01-20 16:17:43 +0100218 struct input_dev *inputdev;
219 u16 *keycode_map;
Corentin Chary85091b72007-01-26 14:04:30 +0100220};
221
222/*
223 * This header is made available to allow proper configuration given model,
224 * revision number , ... this info cannot go in struct asus_hotk because it is
225 * available before the hotk
226 */
227static struct acpi_table_header *asus_info;
228
229/* The actual device the driver binds to */
230static struct asus_hotk *hotk;
231
232/*
233 * The hotkey driver declaration
234 */
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200235static const struct acpi_device_id asus_device_ids[] = {
236 {"ATK0100", 0},
Ike Panhc14f8af32009-12-03 07:45:11 +0000237 {"ATK0101", 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200238 {"", 0},
239};
240MODULE_DEVICE_TABLE(acpi, asus_device_ids);
241
Corentin Chary85091b72007-01-26 14:04:30 +0100242static int asus_hotk_add(struct acpi_device *device);
243static int asus_hotk_remove(struct acpi_device *device, int type);
Bjorn Helgaas586ed162009-04-30 09:35:53 -0600244static void asus_hotk_notify(struct acpi_device *device, u32 event);
245
Corentin Chary85091b72007-01-26 14:04:30 +0100246static struct acpi_driver asus_hotk_driver = {
247 .name = ASUS_HOTK_NAME,
248 .class = ASUS_HOTK_CLASS,
Alan Jenkinsedf62452009-12-03 07:44:51 +0000249 .owner = THIS_MODULE,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200250 .ids = asus_device_ids,
Bjorn Helgaas586ed162009-04-30 09:35:53 -0600251 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
Corentin Chary85091b72007-01-26 14:04:30 +0100252 .ops = {
253 .add = asus_hotk_add,
254 .remove = asus_hotk_remove,
Bjorn Helgaas586ed162009-04-30 09:35:53 -0600255 .notify = asus_hotk_notify,
Corentin Chary85091b72007-01-26 14:04:30 +0100256 },
257};
258
Corentin Chary6b7091e2007-01-26 14:04:45 +0100259/* The backlight device /sys/class/backlight */
260static struct backlight_device *asus_backlight_device;
261
262/*
263 * The backlight class declaration
264 */
265static int read_brightness(struct backlight_device *bd);
266static int update_bl_status(struct backlight_device *bd);
Richard Purdie599a52d2007-02-10 23:07:48 +0000267static struct backlight_ops asusbl_ops = {
Len Brown8def05f2007-01-30 01:46:43 -0500268 .get_brightness = read_brightness,
269 .update_status = update_bl_status,
Corentin Chary6b7091e2007-01-26 14:04:45 +0100270};
271
Corentin Charybe966662009-08-29 10:28:29 +0200272/*
273 * These functions actually update the LED's, and are called from a
Corentin Charybe18cda2007-01-26 14:04:35 +0100274 * workqueue. By doing this as separate work rather than when the LED
275 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
Corentin Charybe966662009-08-29 10:28:29 +0200276 * potentially bad time, such as a timer interrupt.
277 */
Corentin Charybe18cda2007-01-26 14:04:35 +0100278static struct workqueue_struct *led_workqueue;
279
Corentin Chary977c3282009-08-28 12:56:49 +0000280#define ASUS_LED(object, ledname, max) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100281 static void object##_led_set(struct led_classdev *led_cdev, \
282 enum led_brightness value); \
Corentin Charyabfa57e2009-08-28 12:56:47 +0000283 static enum led_brightness object##_led_get( \
284 struct led_classdev *led_cdev); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100285 static void object##_led_update(struct work_struct *ignored); \
286 static int object##_led_wk; \
Adrian Bunkf110ef52007-02-20 01:07:25 +0100287 static DECLARE_WORK(object##_led_work, object##_led_update); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100288 static struct led_classdev object##_led = { \
Richard Purdie6c152be2007-10-31 15:00:07 +0100289 .name = "asus::" ledname, \
Corentin Charybe18cda2007-01-26 14:04:35 +0100290 .brightness_set = object##_led_set, \
Corentin Charyabfa57e2009-08-28 12:56:47 +0000291 .brightness_get = object##_led_get, \
Corentin Chary977c3282009-08-28 12:56:49 +0000292 .max_brightness = max \
Corentin Charybe18cda2007-01-26 14:04:35 +0100293 }
294
Corentin Chary977c3282009-08-28 12:56:49 +0000295ASUS_LED(mled, "mail", 1);
296ASUS_LED(tled, "touchpad", 1);
297ASUS_LED(rled, "record", 1);
298ASUS_LED(pled, "phone", 1);
299ASUS_LED(gled, "gaming", 1);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000300ASUS_LED(kled, "kbd_backlight", 3);
Corentin Charybe18cda2007-01-26 14:04:35 +0100301
Corentin Chary034ce902009-01-20 16:17:43 +0100302struct key_entry {
303 char type;
304 u8 code;
305 u16 keycode;
306};
307
308enum { KE_KEY, KE_END };
309
310static struct key_entry asus_keymap[] = {
Ike Panhc14f8af32009-12-03 07:45:11 +0000311 {KE_KEY, 0x02, KEY_SCREENLOCK},
312 {KE_KEY, 0x05, KEY_WLAN},
313 {KE_KEY, 0x08, KEY_F13},
314 {KE_KEY, 0x17, KEY_ZOOM},
315 {KE_KEY, 0x1f, KEY_BATTERY},
Corentin Chary034ce902009-01-20 16:17:43 +0100316 {KE_KEY, 0x30, KEY_VOLUMEUP},
317 {KE_KEY, 0x31, KEY_VOLUMEDOWN},
318 {KE_KEY, 0x32, KEY_MUTE},
319 {KE_KEY, 0x33, KEY_SWITCHVIDEOMODE},
320 {KE_KEY, 0x34, KEY_SWITCHVIDEOMODE},
321 {KE_KEY, 0x40, KEY_PREVIOUSSONG},
322 {KE_KEY, 0x41, KEY_NEXTSONG},
Corentin Chary309f5fb2009-04-27 09:23:42 +0200323 {KE_KEY, 0x43, KEY_STOPCD},
Corentin Chary034ce902009-01-20 16:17:43 +0100324 {KE_KEY, 0x45, KEY_PLAYPAUSE},
Corentin Chary4644d0e2009-08-28 12:56:52 +0000325 {KE_KEY, 0x4c, KEY_MEDIA},
Corentin Chary034ce902009-01-20 16:17:43 +0100326 {KE_KEY, 0x50, KEY_EMAIL},
327 {KE_KEY, 0x51, KEY_WWW},
Corentin Chary0aa20f72009-08-28 12:56:53 +0000328 {KE_KEY, 0x55, KEY_CALC},
Corentin Chary309f5fb2009-04-27 09:23:42 +0200329 {KE_KEY, 0x5C, KEY_SCREENLOCK}, /* Screenlock */
Corentin Chary034ce902009-01-20 16:17:43 +0100330 {KE_KEY, 0x5D, KEY_WLAN},
Corentin Charyf6413752009-08-28 12:56:48 +0000331 {KE_KEY, 0x5E, KEY_WLAN},
332 {KE_KEY, 0x5F, KEY_WLAN},
333 {KE_KEY, 0x60, KEY_SWITCHVIDEOMODE},
Corentin Chary034ce902009-01-20 16:17:43 +0100334 {KE_KEY, 0x61, KEY_SWITCHVIDEOMODE},
Ike Panhc14f8af32009-12-03 07:45:11 +0000335 {KE_KEY, 0x62, KEY_SWITCHVIDEOMODE},
336 {KE_KEY, 0x63, KEY_SWITCHVIDEOMODE},
Corentin Chary16851f92009-12-03 07:45:10 +0000337 {KE_KEY, 0x6B, KEY_F13}, /* Lock Touchpad */
Corentin Chary034ce902009-01-20 16:17:43 +0100338 {KE_KEY, 0x82, KEY_CAMERA},
Corentin Chary1f0233e2009-12-03 07:45:12 +0000339 {KE_KEY, 0x88, KEY_WLAN },
Corentin Chary309f5fb2009-04-27 09:23:42 +0200340 {KE_KEY, 0x8A, KEY_PROG1},
Corentin Chary034ce902009-01-20 16:17:43 +0100341 {KE_KEY, 0x95, KEY_MEDIA},
342 {KE_KEY, 0x99, KEY_PHONE},
Corentin Charydc795262009-08-28 12:56:51 +0000343 {KE_KEY, 0xc4, KEY_KBDILLUMUP},
344 {KE_KEY, 0xc5, KEY_KBDILLUMDOWN},
Corentin Chary034ce902009-01-20 16:17:43 +0100345 {KE_END, 0},
346};
347
Corentin Chary85091b72007-01-26 14:04:30 +0100348/*
349 * This function evaluates an ACPI method, given an int as parameter, the
350 * method is searched within the scope of the handle, can be NULL. The output
351 * of the method is written is output, which can also be NULL
352 *
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100353 * returns 0 if write is successful, -1 else.
Corentin Chary85091b72007-01-26 14:04:30 +0100354 */
Corentin Charyd8c67322009-11-28 10:27:51 +0100355static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
356 struct acpi_buffer *output)
Corentin Chary85091b72007-01-26 14:04:30 +0100357{
Corentin Charybe966662009-08-29 10:28:29 +0200358 struct acpi_object_list params; /* list of input parameters (an int) */
359 union acpi_object in_obj; /* the only param we use */
Corentin Chary85091b72007-01-26 14:04:30 +0100360 acpi_status status;
361
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100362 if (!handle)
363 return 0;
364
Corentin Chary85091b72007-01-26 14:04:30 +0100365 params.count = 1;
366 params.pointer = &in_obj;
367 in_obj.type = ACPI_TYPE_INTEGER;
368 in_obj.integer.value = val;
369
370 status = acpi_evaluate_object(handle, (char *)method, &params, output);
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100371 if (status == AE_OK)
372 return 0;
373 else
374 return -1;
Corentin Chary85091b72007-01-26 14:04:30 +0100375}
376
Corentin Charyd8c67322009-11-28 10:27:51 +0100377static int write_acpi_int(acpi_handle handle, const char *method, int val)
378{
379 return write_acpi_int_ret(handle, method, val, NULL);
380}
381
Len Brown8def05f2007-01-30 01:46:43 -0500382static int read_wireless_status(int mask)
383{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400384 unsigned long long status;
Corentin Chary9a816852007-03-11 10:25:38 +0100385 acpi_status rv = AE_OK;
Corentin Chary4564de12007-01-26 14:04:40 +0100386
387 if (!wireless_status_handle)
388 return (hotk->status & mask) ? 1 : 0;
389
Corentin Chary9a816852007-03-11 10:25:38 +0100390 rv = acpi_evaluate_integer(wireless_status_handle, NULL, NULL, &status);
391 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200392 pr_warning("Error reading Wireless status\n");
Corentin Chary9a816852007-03-11 10:25:38 +0100393 else
394 return (status & mask) ? 1 : 0;
Corentin Chary4564de12007-01-26 14:04:40 +0100395
396 return (hotk->status & mask) ? 1 : 0;
397}
398
Corentin Charyf3985322007-05-06 14:48:22 +0200399static int read_gps_status(void)
400{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400401 unsigned long long status;
Corentin Charye539c2f2007-05-06 14:47:06 +0200402 acpi_status rv = AE_OK;
403
404 rv = acpi_evaluate_integer(gps_status_handle, NULL, NULL, &status);
405 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200406 pr_warning("Error reading GPS status\n");
Corentin Charye539c2f2007-05-06 14:47:06 +0200407 else
Corentin Charyf3985322007-05-06 14:48:22 +0200408 return status ? 1 : 0;
Corentin Charye539c2f2007-05-06 14:47:06 +0200409
Corentin Charyf3985322007-05-06 14:48:22 +0200410 return (hotk->status & GPS_ON) ? 1 : 0;
Corentin Charye539c2f2007-05-06 14:47:06 +0200411}
412
Corentin Charybe18cda2007-01-26 14:04:35 +0100413/* Generic LED functions */
414static int read_status(int mask)
415{
Corentin Chary4564de12007-01-26 14:04:40 +0100416 /* There is a special method for both wireless devices */
417 if (mask == BT_ON || mask == WL_ON)
418 return read_wireless_status(mask);
Corentin Charyf3985322007-05-06 14:48:22 +0200419 else if (mask == GPS_ON)
420 return read_gps_status();
Corentin Chary4564de12007-01-26 14:04:40 +0100421
Corentin Charybe18cda2007-01-26 14:04:35 +0100422 return (hotk->status & mask) ? 1 : 0;
423}
424
Corentin Chary935ffee2007-03-11 10:26:12 +0100425static void write_status(acpi_handle handle, int out, int mask)
Corentin Charybe18cda2007-01-26 14:04:35 +0100426{
427 hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
428
Corentin Chary935ffee2007-03-11 10:26:12 +0100429 switch (mask) {
430 case MLED_ON:
Roel Kluine1af14e2008-02-02 21:07:38 +0100431 out = !(out & 0x1);
Corentin Chary935ffee2007-03-11 10:26:12 +0100432 break;
Corentin Charyfdd8d082007-03-11 10:26:48 +0100433 case GLED_ON:
434 out = (out & 0x1) + 1;
435 break;
Corentin Charye539c2f2007-05-06 14:47:06 +0200436 case GPS_ON:
437 handle = (out) ? gps_on_handle : gps_off_handle;
438 out = 0x02;
439 break;
Corentin Chary935ffee2007-03-11 10:26:12 +0100440 default:
441 out &= 0x1;
442 break;
443 }
Corentin Charybe18cda2007-01-26 14:04:35 +0100444
Corentin Charyd8c67322009-11-28 10:27:51 +0100445 if (write_acpi_int(handle, NULL, out))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200446 pr_warning(" write failed %x\n", mask);
Corentin Charybe18cda2007-01-26 14:04:35 +0100447}
448
449/* /sys/class/led handlers */
Corentin Chary935ffee2007-03-11 10:26:12 +0100450#define ASUS_LED_HANDLER(object, mask) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100451 static void object##_led_set(struct led_classdev *led_cdev, \
452 enum led_brightness value) \
453 { \
Corentin Charye3deda92008-09-24 10:35:55 +0200454 object##_led_wk = (value > 0) ? 1 : 0; \
Corentin Charybe18cda2007-01-26 14:04:35 +0100455 queue_work(led_workqueue, &object##_led_work); \
456 } \
457 static void object##_led_update(struct work_struct *ignored) \
458 { \
459 int value = object##_led_wk; \
Corentin Chary935ffee2007-03-11 10:26:12 +0100460 write_status(object##_set_handle, value, (mask)); \
Corentin Charyabfa57e2009-08-28 12:56:47 +0000461 } \
462 static enum led_brightness object##_led_get( \
463 struct led_classdev *led_cdev) \
464 { \
465 return led_cdev->brightness; \
Corentin Charybe18cda2007-01-26 14:04:35 +0100466 }
467
Corentin Chary935ffee2007-03-11 10:26:12 +0100468ASUS_LED_HANDLER(mled, MLED_ON);
469ASUS_LED_HANDLER(pled, PLED_ON);
470ASUS_LED_HANDLER(rled, RLED_ON);
471ASUS_LED_HANDLER(tled, TLED_ON);
Corentin Charyfdd8d082007-03-11 10:26:48 +0100472ASUS_LED_HANDLER(gled, GLED_ON);
Corentin Charybe18cda2007-01-26 14:04:35 +0100473
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000474/*
475 * Keyboard backlight
476 */
477static int get_kled_lvl(void)
478{
479 unsigned long long kblv;
480 struct acpi_object_list params;
481 union acpi_object in_obj;
482 acpi_status rv;
483
484 params.count = 1;
485 params.pointer = &in_obj;
486 in_obj.type = ACPI_TYPE_INTEGER;
487 in_obj.integer.value = 2;
488
489 rv = acpi_evaluate_integer(kled_get_handle, NULL, &params, &kblv);
490 if (ACPI_FAILURE(rv)) {
491 pr_warning("Error reading kled level\n");
492 return 0;
493 }
494 return kblv;
495}
496
497static int set_kled_lvl(int kblv)
498{
499 if (kblv > 0)
500 kblv = (1 << 7) | (kblv & 0x7F);
501 else
502 kblv = 0;
503
Corentin Charyd8c67322009-11-28 10:27:51 +0100504 if (write_acpi_int(kled_set_handle, NULL, kblv)) {
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000505 pr_warning("Keyboard LED display write failed\n");
506 return -EINVAL;
507 }
508 return 0;
509}
510
511static void kled_led_set(struct led_classdev *led_cdev,
512 enum led_brightness value)
513{
514 kled_led_wk = value;
515 queue_work(led_workqueue, &kled_led_work);
516}
517
518static void kled_led_update(struct work_struct *ignored)
519{
520 set_kled_lvl(kled_led_wk);
521}
522
523static enum led_brightness kled_led_get(struct led_classdev *led_cdev)
524{
525 return get_kled_lvl();
526}
527
Corentin Chary6b7091e2007-01-26 14:04:45 +0100528static int get_lcd_state(void)
529{
530 return read_status(LCD_ON);
531}
532
533static int set_lcd_state(int value)
534{
535 int lcd = 0;
536 acpi_status status = 0;
537
538 lcd = value ? 1 : 0;
539
540 if (lcd == get_lcd_state())
541 return 0;
542
Len Brown8def05f2007-01-30 01:46:43 -0500543 if (lcd_switch_handle) {
Corentin Chary6b7091e2007-01-26 14:04:45 +0100544 status = acpi_evaluate_object(lcd_switch_handle,
545 NULL, NULL, NULL);
546
547 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200548 pr_warning("Error switching LCD\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100549 }
550
Corentin Chary935ffee2007-03-11 10:26:12 +0100551 write_status(NULL, lcd, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100552 return 0;
553}
554
555static void lcd_blank(int blank)
556{
557 struct backlight_device *bd = asus_backlight_device;
558
Len Brown8def05f2007-01-30 01:46:43 -0500559 if (bd) {
Richard Purdie599a52d2007-02-10 23:07:48 +0000560 bd->props.power = blank;
Richard Purdie28ee0862007-02-08 22:25:09 +0000561 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100562 }
563}
564
565static int read_brightness(struct backlight_device *bd)
566{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400567 unsigned long long value;
Corentin Chary9a816852007-03-11 10:25:38 +0100568 acpi_status rv = AE_OK;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100569
Corentin Chary9a816852007-03-11 10:25:38 +0100570 rv = acpi_evaluate_integer(brightness_get_handle, NULL, NULL, &value);
571 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200572 pr_warning("Error reading brightness\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100573
574 return value;
575}
576
577static int set_brightness(struct backlight_device *bd, int value)
578{
Corentin Charyd8c67322009-11-28 10:27:51 +0100579 if (write_acpi_int(brightness_set_handle, NULL, value)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200580 pr_warning("Error changing brightness\n");
Corentin Charye5b50f62009-11-28 10:19:55 +0100581 return -EIO;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100582 }
Corentin Charye5b50f62009-11-28 10:19:55 +0100583 return 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100584}
585
586static int update_bl_status(struct backlight_device *bd)
587{
588 int rv;
Richard Purdie599a52d2007-02-10 23:07:48 +0000589 int value = bd->props.brightness;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100590
591 rv = set_brightness(bd, value);
Len Brown8def05f2007-01-30 01:46:43 -0500592 if (rv)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100593 return rv;
594
Richard Purdie599a52d2007-02-10 23:07:48 +0000595 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100596 return set_lcd_state(value);
597}
598
Corentin Chary85091b72007-01-26 14:04:30 +0100599/*
600 * Platform device handlers
601 */
602
603/*
604 * We write our info in page, we begin at offset off and cannot write more
605 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
606 * number of bytes written in page
607 */
608static ssize_t show_infos(struct device *dev,
Len Brown8def05f2007-01-30 01:46:43 -0500609 struct device_attribute *attr, char *page)
Corentin Chary85091b72007-01-26 14:04:30 +0100610{
611 int len = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400612 unsigned long long temp;
Corentin Charybe966662009-08-29 10:28:29 +0200613 char buf[16]; /* enough for all info */
Corentin Chary9a816852007-03-11 10:25:38 +0100614 acpi_status rv = AE_OK;
615
Corentin Chary85091b72007-01-26 14:04:30 +0100616 /*
617 * We use the easy way, we don't care of off and count, so we don't set eof
618 * to 1
619 */
620
621 len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
622 len += sprintf(page + len, "Model reference : %s\n", hotk->name);
623 /*
624 * The SFUN method probably allows the original driver to get the list
625 * of features supported by a given model. For now, 0x0100 or 0x0800
626 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
627 * The significance of others is yet to be found.
628 */
Corentin Chary9a816852007-03-11 10:25:38 +0100629 rv = acpi_evaluate_integer(hotk->handle, "SFUN", NULL, &temp);
630 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000631 len += sprintf(page + len, "SFUN value : %#x\n",
632 (uint) temp);
633 /*
634 * The HWRS method return informations about the hardware.
635 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
636 * The significance of others is yet to be found.
637 * If we don't find the method, we assume the device are present.
638 */
639 rv = acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &temp);
640 if (!ACPI_FAILURE(rv))
641 len += sprintf(page + len, "HRWS value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100642 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100643 /*
644 * Another value for userspace: the ASYM method returns 0x02 for
645 * battery low and 0x04 for battery critical, its readings tend to be
646 * more accurate than those provided by _BST.
647 * Note: since not all the laptops provide this method, errors are
648 * silently ignored.
649 */
Corentin Chary9a816852007-03-11 10:25:38 +0100650 rv = acpi_evaluate_integer(hotk->handle, "ASYM", NULL, &temp);
651 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000652 len += sprintf(page + len, "ASYM value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100653 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100654 if (asus_info) {
655 snprintf(buf, 16, "%d", asus_info->length);
656 len += sprintf(page + len, "DSDT length : %s\n", buf);
657 snprintf(buf, 16, "%d", asus_info->checksum);
658 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
659 snprintf(buf, 16, "%d", asus_info->revision);
660 len += sprintf(page + len, "DSDT revision : %s\n", buf);
661 snprintf(buf, 7, "%s", asus_info->oem_id);
662 len += sprintf(page + len, "OEM id : %s\n", buf);
663 snprintf(buf, 9, "%s", asus_info->oem_table_id);
664 len += sprintf(page + len, "OEM table id : %s\n", buf);
665 snprintf(buf, 16, "%x", asus_info->oem_revision);
666 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
667 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
668 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
669 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
670 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
671 }
672
673 return len;
674}
675
676static int parse_arg(const char *buf, unsigned long count, int *val)
677{
678 if (!count)
679 return 0;
680 if (count > 31)
681 return -EINVAL;
682 if (sscanf(buf, "%i", val) != 1)
683 return -EINVAL;
684 return count;
685}
686
Corentin Chary4564de12007-01-26 14:04:40 +0100687static ssize_t store_status(const char *buf, size_t count,
Corentin Chary935ffee2007-03-11 10:26:12 +0100688 acpi_handle handle, int mask)
Corentin Chary4564de12007-01-26 14:04:40 +0100689{
690 int rv, value;
691 int out = 0;
692
693 rv = parse_arg(buf, count, &value);
694 if (rv > 0)
695 out = value ? 1 : 0;
696
Corentin Chary935ffee2007-03-11 10:26:12 +0100697 write_status(handle, out, mask);
Corentin Chary4564de12007-01-26 14:04:40 +0100698
699 return rv;
700}
701
702/*
Corentin Chary722ad972007-01-26 14:04:55 +0100703 * LEDD display
704 */
705static ssize_t show_ledd(struct device *dev,
706 struct device_attribute *attr, char *buf)
707{
708 return sprintf(buf, "0x%08x\n", hotk->ledd_status);
709}
710
711static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
712 const char *buf, size_t count)
713{
714 int rv, value;
715
716 rv = parse_arg(buf, count, &value);
717 if (rv > 0) {
Corentin Charyd8c67322009-11-28 10:27:51 +0100718 if (write_acpi_int(ledd_set_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200719 pr_warning("LED display write failed\n");
Corentin Chary722ad972007-01-26 14:04:55 +0100720 else
721 hotk->ledd_status = (u32) value;
722 }
723 return rv;
724}
725
726/*
Corentin Chary4564de12007-01-26 14:04:40 +0100727 * WLAN
728 */
729static ssize_t show_wlan(struct device *dev,
730 struct device_attribute *attr, char *buf)
731{
732 return sprintf(buf, "%d\n", read_status(WL_ON));
733}
734
735static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
736 const char *buf, size_t count)
737{
Corentin Chary935ffee2007-03-11 10:26:12 +0100738 return store_status(buf, count, wl_switch_handle, WL_ON);
Corentin Chary4564de12007-01-26 14:04:40 +0100739}
740
741/*
742 * Bluetooth
743 */
744static ssize_t show_bluetooth(struct device *dev,
745 struct device_attribute *attr, char *buf)
746{
747 return sprintf(buf, "%d\n", read_status(BT_ON));
748}
749
Len Brown8def05f2007-01-30 01:46:43 -0500750static ssize_t store_bluetooth(struct device *dev,
751 struct device_attribute *attr, const char *buf,
752 size_t count)
Corentin Chary4564de12007-01-26 14:04:40 +0100753{
Corentin Chary935ffee2007-03-11 10:26:12 +0100754 return store_status(buf, count, bt_switch_handle, BT_ON);
Corentin Chary4564de12007-01-26 14:04:40 +0100755}
756
Corentin Chary78127b42007-01-26 14:04:49 +0100757/*
758 * Display
759 */
760static void set_display(int value)
761{
762 /* no sanity check needed for now */
Corentin Charyd8c67322009-11-28 10:27:51 +0100763 if (write_acpi_int(display_set_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200764 pr_warning("Error setting display\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100765 return;
766}
767
768static int read_display(void)
769{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400770 unsigned long long value = 0;
Corentin Chary9a816852007-03-11 10:25:38 +0100771 acpi_status rv = AE_OK;
Corentin Chary78127b42007-01-26 14:04:49 +0100772
Corentin Charybe966662009-08-29 10:28:29 +0200773 /*
774 * In most of the case, we know how to set the display, but sometime
775 * we can't read it
776 */
Len Brown8def05f2007-01-30 01:46:43 -0500777 if (display_get_handle) {
Corentin Chary9a816852007-03-11 10:25:38 +0100778 rv = acpi_evaluate_integer(display_get_handle, NULL,
779 NULL, &value);
780 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200781 pr_warning("Error reading display status\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100782 }
783
784 value &= 0x0F; /* needed for some models, shouldn't hurt others */
785
786 return value;
787}
Len Brown8def05f2007-01-30 01:46:43 -0500788
Corentin Chary78127b42007-01-26 14:04:49 +0100789/*
790 * Now, *this* one could be more user-friendly, but so far, no-one has
791 * complained. The significance of bits is the same as in store_disp()
792 */
793static ssize_t show_disp(struct device *dev,
794 struct device_attribute *attr, char *buf)
795{
796 return sprintf(buf, "%d\n", read_display());
797}
798
799/*
800 * Experimental support for display switching. As of now: 1 should activate
801 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
802 * Any combination (bitwise) of these will suffice. I never actually tested 4
803 * displays hooked up simultaneously, so be warned. See the acpi4asus README
804 * for more info.
805 */
806static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
807 const char *buf, size_t count)
808{
809 int rv, value;
810
811 rv = parse_arg(buf, count, &value);
812 if (rv > 0)
813 set_display(value);
814 return rv;
815}
816
Corentin Chary8b857352007-01-26 14:04:58 +0100817/*
818 * Light Sens
819 */
820static void set_light_sens_switch(int value)
821{
Corentin Charyd8c67322009-11-28 10:27:51 +0100822 if (write_acpi_int(ls_switch_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200823 pr_warning("Error setting light sensor switch\n");
Corentin Chary8b857352007-01-26 14:04:58 +0100824 hotk->light_switch = value;
825}
826
827static ssize_t show_lssw(struct device *dev,
828 struct device_attribute *attr, char *buf)
829{
830 return sprintf(buf, "%d\n", hotk->light_switch);
831}
832
833static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
834 const char *buf, size_t count)
835{
836 int rv, value;
837
838 rv = parse_arg(buf, count, &value);
839 if (rv > 0)
840 set_light_sens_switch(value ? 1 : 0);
841
842 return rv;
843}
844
845static void set_light_sens_level(int value)
846{
Corentin Charyd8c67322009-11-28 10:27:51 +0100847 if (write_acpi_int(ls_level_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200848 pr_warning("Error setting light sensor level\n");
Corentin Chary8b857352007-01-26 14:04:58 +0100849 hotk->light_level = value;
850}
851
852static ssize_t show_lslvl(struct device *dev,
853 struct device_attribute *attr, char *buf)
854{
855 return sprintf(buf, "%d\n", hotk->light_level);
856}
857
858static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
859 const char *buf, size_t count)
860{
861 int rv, value;
862
863 rv = parse_arg(buf, count, &value);
864 if (rv > 0) {
865 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
866 /* 0 <= value <= 15 */
867 set_light_sens_level(value);
868 }
869
870 return rv;
871}
872
Corentin Charye539c2f2007-05-06 14:47:06 +0200873/*
874 * GPS
875 */
876static ssize_t show_gps(struct device *dev,
877 struct device_attribute *attr, char *buf)
878{
879 return sprintf(buf, "%d\n", read_status(GPS_ON));
880}
881
882static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
883 const char *buf, size_t count)
884{
Corentin Charyf3985322007-05-06 14:48:22 +0200885 return store_status(buf, count, NULL, GPS_ON);
Corentin Charye539c2f2007-05-06 14:47:06 +0200886}
887
Corentin Chary034ce902009-01-20 16:17:43 +0100888/*
889 * Hotkey functions
890 */
891static struct key_entry *asus_get_entry_by_scancode(int code)
892{
893 struct key_entry *key;
894
895 for (key = asus_keymap; key->type != KE_END; key++)
896 if (code == key->code)
897 return key;
898
899 return NULL;
900}
901
902static struct key_entry *asus_get_entry_by_keycode(int code)
903{
904 struct key_entry *key;
905
906 for (key = asus_keymap; key->type != KE_END; key++)
907 if (code == key->keycode && key->type == KE_KEY)
908 return key;
909
910 return NULL;
911}
912
913static int asus_getkeycode(struct input_dev *dev, int scancode, int *keycode)
914{
915 struct key_entry *key = asus_get_entry_by_scancode(scancode);
916
917 if (key && key->type == KE_KEY) {
918 *keycode = key->keycode;
919 return 0;
920 }
921
922 return -EINVAL;
923}
924
925static int asus_setkeycode(struct input_dev *dev, int scancode, int keycode)
926{
927 struct key_entry *key;
928 int old_keycode;
929
930 if (keycode < 0 || keycode > KEY_MAX)
931 return -EINVAL;
932
933 key = asus_get_entry_by_scancode(scancode);
934 if (key && key->type == KE_KEY) {
935 old_keycode = key->keycode;
936 key->keycode = keycode;
937 set_bit(keycode, dev->keybit);
938 if (!asus_get_entry_by_keycode(old_keycode))
939 clear_bit(old_keycode, dev->keybit);
940 return 0;
941 }
942
943 return -EINVAL;
944}
945
Bjorn Helgaas586ed162009-04-30 09:35:53 -0600946static void asus_hotk_notify(struct acpi_device *device, u32 event)
Corentin Chary85091b72007-01-26 14:04:30 +0100947{
Corentin Chary034ce902009-01-20 16:17:43 +0100948 static struct key_entry *key;
Corentin Chary6050c8d2009-02-15 19:30:19 +0100949 u16 count;
Corentin Chary034ce902009-01-20 16:17:43 +0100950
Corentin Chary85091b72007-01-26 14:04:30 +0100951 /* TODO Find a better way to handle events count. */
952 if (!hotk)
953 return;
954
Corentin Chary6b7091e2007-01-26 14:04:45 +0100955 /*
956 * We need to tell the backlight device when the backlight power is
957 * switched
958 */
959 if (event == ATKD_LCD_ON) {
Corentin Chary935ffee2007-03-11 10:26:12 +0100960 write_status(NULL, 1, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100961 lcd_blank(FB_BLANK_UNBLANK);
Len Brown8def05f2007-01-30 01:46:43 -0500962 } else if (event == ATKD_LCD_OFF) {
Corentin Chary935ffee2007-03-11 10:26:12 +0100963 write_status(NULL, 0, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100964 lcd_blank(FB_BLANK_POWERDOWN);
965 }
966
Corentin Chary6050c8d2009-02-15 19:30:19 +0100967 count = hotk->event_count[event % 128]++;
968 acpi_bus_generate_proc_event(hotk->device, event, count);
Corentin Chary2a7dc0d2009-01-20 16:17:42 +0100969 acpi_bus_generate_netlink_event(hotk->device->pnp.device_class,
970 dev_name(&hotk->device->dev), event,
Corentin Chary6050c8d2009-02-15 19:30:19 +0100971 count);
Corentin Chary85091b72007-01-26 14:04:30 +0100972
Corentin Chary034ce902009-01-20 16:17:43 +0100973 if (hotk->inputdev) {
974 key = asus_get_entry_by_scancode(event);
975 if (!key)
976 return ;
977
978 switch (key->type) {
979 case KE_KEY:
980 input_report_key(hotk->inputdev, key->keycode, 1);
981 input_sync(hotk->inputdev);
982 input_report_key(hotk->inputdev, key->keycode, 0);
983 input_sync(hotk->inputdev);
984 break;
985 }
986 }
Corentin Chary85091b72007-01-26 14:04:30 +0100987}
988
989#define ASUS_CREATE_DEVICE_ATTR(_name) \
990 struct device_attribute dev_attr_##_name = { \
991 .attr = { \
992 .name = __stringify(_name), \
Tejun Heo7b595752007-06-14 03:45:17 +0900993 .mode = 0 }, \
Corentin Chary85091b72007-01-26 14:04:30 +0100994 .show = NULL, \
995 .store = NULL, \
996 }
997
998#define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
999 do { \
1000 dev_attr_##_name.attr.mode = _mode; \
1001 dev_attr_##_name.show = _show; \
1002 dev_attr_##_name.store = _store; \
1003 } while(0)
1004
1005static ASUS_CREATE_DEVICE_ATTR(infos);
Corentin Chary4564de12007-01-26 14:04:40 +01001006static ASUS_CREATE_DEVICE_ATTR(wlan);
1007static ASUS_CREATE_DEVICE_ATTR(bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +01001008static ASUS_CREATE_DEVICE_ATTR(display);
Corentin Chary722ad972007-01-26 14:04:55 +01001009static ASUS_CREATE_DEVICE_ATTR(ledd);
Corentin Chary8b857352007-01-26 14:04:58 +01001010static ASUS_CREATE_DEVICE_ATTR(ls_switch);
1011static ASUS_CREATE_DEVICE_ATTR(ls_level);
Corentin Charye539c2f2007-05-06 14:47:06 +02001012static ASUS_CREATE_DEVICE_ATTR(gps);
Corentin Chary85091b72007-01-26 14:04:30 +01001013
1014static struct attribute *asuspf_attributes[] = {
Len Brown8def05f2007-01-30 01:46:43 -05001015 &dev_attr_infos.attr,
1016 &dev_attr_wlan.attr,
1017 &dev_attr_bluetooth.attr,
1018 &dev_attr_display.attr,
1019 &dev_attr_ledd.attr,
1020 &dev_attr_ls_switch.attr,
1021 &dev_attr_ls_level.attr,
Corentin Charye539c2f2007-05-06 14:47:06 +02001022 &dev_attr_gps.attr,
Len Brown8def05f2007-01-30 01:46:43 -05001023 NULL
Corentin Chary85091b72007-01-26 14:04:30 +01001024};
1025
1026static struct attribute_group asuspf_attribute_group = {
Len Brown8def05f2007-01-30 01:46:43 -05001027 .attrs = asuspf_attributes
Corentin Chary85091b72007-01-26 14:04:30 +01001028};
1029
1030static struct platform_driver asuspf_driver = {
Len Brown8def05f2007-01-30 01:46:43 -05001031 .driver = {
1032 .name = ASUS_HOTK_FILE,
1033 .owner = THIS_MODULE,
1034 }
Corentin Chary85091b72007-01-26 14:04:30 +01001035};
1036
1037static struct platform_device *asuspf_device;
1038
Corentin Chary85091b72007-01-26 14:04:30 +01001039static void asus_hotk_add_fs(void)
1040{
1041 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
Corentin Chary4564de12007-01-26 14:04:40 +01001042
1043 if (wl_switch_handle)
1044 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
1045
1046 if (bt_switch_handle)
1047 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
1048 show_bluetooth, store_bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +01001049
1050 if (display_set_handle && display_get_handle)
1051 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
Len Brown8def05f2007-01-30 01:46:43 -05001052 else if (display_set_handle)
Corentin Chary78127b42007-01-26 14:04:49 +01001053 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
1054
Corentin Chary722ad972007-01-26 14:04:55 +01001055 if (ledd_set_handle)
1056 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
1057
Corentin Chary8b857352007-01-26 14:04:58 +01001058 if (ls_switch_handle && ls_level_handle) {
1059 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
1060 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
1061 }
Corentin Charye539c2f2007-05-06 14:47:06 +02001062
Corentin Charyf3985322007-05-06 14:48:22 +02001063 if (gps_status_handle && gps_on_handle && gps_off_handle)
Corentin Charye539c2f2007-05-06 14:47:06 +02001064 ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
Corentin Chary85091b72007-01-26 14:04:30 +01001065}
1066
Len Brown8def05f2007-01-30 01:46:43 -05001067static int asus_handle_init(char *name, acpi_handle * handle,
Corentin Chary85091b72007-01-26 14:04:30 +01001068 char **paths, int num_paths)
1069{
1070 int i;
1071 acpi_status status;
1072
1073 for (i = 0; i < num_paths; i++) {
1074 status = acpi_get_handle(NULL, paths[i], handle);
1075 if (ACPI_SUCCESS(status))
1076 return 0;
1077 }
1078
1079 *handle = NULL;
1080 return -ENODEV;
1081}
1082
1083#define ASUS_HANDLE_INIT(object) \
1084 asus_handle_init(#object, &object##_handle, object##_paths, \
1085 ARRAY_SIZE(object##_paths))
1086
Corentin Chary85091b72007-01-26 14:04:30 +01001087/*
1088 * This function is used to initialize the hotk with right values. In this
1089 * method, we can make all the detection we want, and modify the hotk struct
1090 */
1091static int asus_hotk_get_info(void)
1092{
1093 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Corentin Chary85091b72007-01-26 14:04:30 +01001094 union acpi_object *model = NULL;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001095 unsigned long long bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +01001096 char *string = NULL;
1097 acpi_status status;
1098
1099 /*
1100 * Get DSDT headers early enough to allow for differentiating between
1101 * models, but late enough to allow acpi_bus_register_driver() to fail
1102 * before doing anything ACPI-specific. Should we encounter a machine,
1103 * which needs special handling (i.e. its hotkey device has a different
1104 * HID), this bit will be moved. A global variable asus_info contains
1105 * the DSDT header.
1106 */
Len Brown894d79b2007-02-03 02:13:53 -05001107 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
Corentin Chary85091b72007-01-26 14:04:30 +01001108 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001109 pr_warning("Couldn't get the DSDT table header\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001110
1111 /* We have to write 0 on init this far for all ASUS models */
Corentin Charyd8c67322009-11-28 10:27:51 +01001112 if (write_acpi_int_ret(hotk->handle, "INIT", 0, &buffer)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001113 pr_err("Hotkey initialization failed\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001114 return -ENODEV;
1115 }
1116
1117 /* This needs to be called for some laptops to init properly */
Corentin Chary9a816852007-03-11 10:25:38 +01001118 status =
1119 acpi_evaluate_integer(hotk->handle, "BSTS", NULL, &bsts_result);
1120 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001121 pr_warning("Error calling BSTS\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001122 else if (bsts_result)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001123 pr_notice("BSTS called, 0x%02x returned\n",
Corentin Chary9a816852007-03-11 10:25:38 +01001124 (uint) bsts_result);
Corentin Chary85091b72007-01-26 14:04:30 +01001125
Corentin Chary185e5af2007-03-11 10:27:33 +01001126 /* This too ... */
Corentin Charyd8c67322009-11-28 10:27:51 +01001127 write_acpi_int(hotk->handle, "CWAP", wapf);
Corentin Chary185e5af2007-03-11 10:27:33 +01001128
Corentin Chary85091b72007-01-26 14:04:30 +01001129 /*
1130 * Try to match the object returned by INIT to the specific model.
1131 * Handle every possible object (or the lack of thereof) the DSDT
1132 * writers might throw at us. When in trouble, we pass NULL to
1133 * asus_model_match() and try something completely different.
1134 */
1135 if (buffer.pointer) {
1136 model = buffer.pointer;
1137 switch (model->type) {
1138 case ACPI_TYPE_STRING:
1139 string = model->string.pointer;
1140 break;
1141 case ACPI_TYPE_BUFFER:
1142 string = model->buffer.pointer;
1143 break;
1144 default:
1145 string = "";
1146 break;
1147 }
1148 }
1149 hotk->name = kstrdup(string, GFP_KERNEL);
1150 if (!hotk->name)
1151 return -ENOMEM;
1152
Len Brown8def05f2007-01-30 01:46:43 -05001153 if (*string)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001154 pr_notice(" %s model detected\n", string);
Corentin Chary85091b72007-01-26 14:04:30 +01001155
Corentin Charybe18cda2007-01-26 14:04:35 +01001156 ASUS_HANDLE_INIT(mled_set);
1157 ASUS_HANDLE_INIT(tled_set);
1158 ASUS_HANDLE_INIT(rled_set);
1159 ASUS_HANDLE_INIT(pled_set);
Corentin Charyfdd8d082007-03-11 10:26:48 +01001160 ASUS_HANDLE_INIT(gled_set);
Corentin Charybe18cda2007-01-26 14:04:35 +01001161
Corentin Chary722ad972007-01-26 14:04:55 +01001162 ASUS_HANDLE_INIT(ledd_set);
1163
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001164 ASUS_HANDLE_INIT(kled_set);
1165 ASUS_HANDLE_INIT(kled_get);
1166
Corentin Chary4564de12007-01-26 14:04:40 +01001167 /*
1168 * The HWRS method return informations about the hardware.
1169 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1170 * The significance of others is yet to be found.
1171 * If we don't find the method, we assume the device are present.
1172 */
Corentin Chary9a816852007-03-11 10:25:38 +01001173 status =
1174 acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &hwrs_result);
1175 if (ACPI_FAILURE(status))
Corentin Chary4564de12007-01-26 14:04:40 +01001176 hwrs_result = WL_HWRS | BT_HWRS;
1177
Len Brown8def05f2007-01-30 01:46:43 -05001178 if (hwrs_result & WL_HWRS)
Corentin Chary4564de12007-01-26 14:04:40 +01001179 ASUS_HANDLE_INIT(wl_switch);
Len Brown8def05f2007-01-30 01:46:43 -05001180 if (hwrs_result & BT_HWRS)
Corentin Chary4564de12007-01-26 14:04:40 +01001181 ASUS_HANDLE_INIT(bt_switch);
1182
1183 ASUS_HANDLE_INIT(wireless_status);
1184
Corentin Chary6b7091e2007-01-26 14:04:45 +01001185 ASUS_HANDLE_INIT(brightness_set);
1186 ASUS_HANDLE_INIT(brightness_get);
1187
1188 ASUS_HANDLE_INIT(lcd_switch);
1189
Corentin Chary78127b42007-01-26 14:04:49 +01001190 ASUS_HANDLE_INIT(display_set);
1191 ASUS_HANDLE_INIT(display_get);
1192
Corentin Charybe966662009-08-29 10:28:29 +02001193 /*
1194 * There is a lot of models with "ALSL", but a few get
1195 * a real light sens, so we need to check it.
1196 */
Corentin Chary832d9952007-05-06 14:47:29 +02001197 if (!ASUS_HANDLE_INIT(ls_switch))
Corentin Chary8b857352007-01-26 14:04:58 +01001198 ASUS_HANDLE_INIT(ls_level);
1199
Corentin Charye539c2f2007-05-06 14:47:06 +02001200 ASUS_HANDLE_INIT(gps_on);
1201 ASUS_HANDLE_INIT(gps_off);
1202 ASUS_HANDLE_INIT(gps_status);
1203
Corentin Chary85091b72007-01-26 14:04:30 +01001204 kfree(model);
1205
1206 return AE_OK;
1207}
1208
Corentin Chary034ce902009-01-20 16:17:43 +01001209static int asus_input_init(void)
1210{
1211 const struct key_entry *key;
1212 int result;
1213
1214 hotk->inputdev = input_allocate_device();
1215 if (!hotk->inputdev) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001216 pr_info("Unable to allocate input device\n");
Corentin Chary034ce902009-01-20 16:17:43 +01001217 return 0;
1218 }
1219 hotk->inputdev->name = "Asus Laptop extra buttons";
1220 hotk->inputdev->phys = ASUS_HOTK_FILE "/input0";
1221 hotk->inputdev->id.bustype = BUS_HOST;
1222 hotk->inputdev->getkeycode = asus_getkeycode;
1223 hotk->inputdev->setkeycode = asus_setkeycode;
1224
1225 for (key = asus_keymap; key->type != KE_END; key++) {
1226 switch (key->type) {
1227 case KE_KEY:
1228 set_bit(EV_KEY, hotk->inputdev->evbit);
1229 set_bit(key->keycode, hotk->inputdev->keybit);
1230 break;
1231 }
1232 }
1233 result = input_register_device(hotk->inputdev);
1234 if (result) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001235 pr_info("Unable to register input device\n");
Corentin Chary034ce902009-01-20 16:17:43 +01001236 input_free_device(hotk->inputdev);
1237 }
1238 return result;
1239}
1240
Corentin Chary85091b72007-01-26 14:04:30 +01001241static int asus_hotk_check(void)
1242{
1243 int result = 0;
1244
1245 result = acpi_bus_get_status(hotk->device);
1246 if (result)
1247 return result;
1248
1249 if (hotk->device->status.present) {
1250 result = asus_hotk_get_info();
1251 } else {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001252 pr_err("Hotkey device not present, aborting\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001253 return -EINVAL;
1254 }
1255
1256 return result;
1257}
1258
1259static int asus_hotk_found;
1260
1261static int asus_hotk_add(struct acpi_device *device)
1262{
Corentin Chary85091b72007-01-26 14:04:30 +01001263 int result;
1264
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001265 pr_notice("Asus Laptop Support version %s\n",
Corentin Chary85091b72007-01-26 14:04:30 +01001266 ASUS_LAPTOP_VERSION);
1267
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001268 hotk = kzalloc(sizeof(struct asus_hotk), GFP_KERNEL);
Corentin Chary85091b72007-01-26 14:04:30 +01001269 if (!hotk)
1270 return -ENOMEM;
Corentin Chary85091b72007-01-26 14:04:30 +01001271
1272 hotk->handle = device->handle;
1273 strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
1274 strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001275 device->driver_data = hotk;
Corentin Chary85091b72007-01-26 14:04:30 +01001276 hotk->device = device;
1277
1278 result = asus_hotk_check();
1279 if (result)
1280 goto end;
1281
1282 asus_hotk_add_fs();
1283
Corentin Chary85091b72007-01-26 14:04:30 +01001284 asus_hotk_found = 1;
1285
Corentin Chary6b7091e2007-01-26 14:04:45 +01001286 /* WLED and BLED are on by default */
Corentin Chary0e875f42010-01-10 20:49:26 +01001287 if (bluetooth_status != -1)
1288 write_status(bt_switch_handle, !!bluetooth_status, BT_ON);
1289 if (wireless_status != -1)
1290 write_status(wl_switch_handle, !!wireless_status, WL_ON);
Corentin Chary935ffee2007-03-11 10:26:12 +01001291
1292 /* If the h/w switch is off, we need to check the real status */
1293 write_status(NULL, read_status(BT_ON), BT_ON);
1294 write_status(NULL, read_status(WL_ON), WL_ON);
Corentin Chary4564de12007-01-26 14:04:40 +01001295
Corentin Chary6b7091e2007-01-26 14:04:45 +01001296 /* LCD Backlight is on by default */
Corentin Chary935ffee2007-03-11 10:26:12 +01001297 write_status(NULL, 1, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001298
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001299 /* Keyboard Backlight is on by default */
1300 if (kled_set_handle)
1301 set_kled_lvl(1);
1302
Corentin Chary722ad972007-01-26 14:04:55 +01001303 /* LED display is off by default */
1304 hotk->ledd_status = 0xFFF;
1305
Len Brown8def05f2007-01-30 01:46:43 -05001306 /* Set initial values of light sensor and level */
Corentin Charyd951d4c2009-12-07 22:05:50 +01001307 hotk->light_switch = 0; /* Default to light sensor disabled */
1308 hotk->light_level = 5; /* level 5 for sensor sensitivity */
Corentin Chary8b857352007-01-26 14:04:58 +01001309
Len Brown8def05f2007-01-30 01:46:43 -05001310 if (ls_switch_handle)
1311 set_light_sens_switch(hotk->light_switch);
Corentin Chary8b857352007-01-26 14:04:58 +01001312
Len Brown8def05f2007-01-30 01:46:43 -05001313 if (ls_level_handle)
1314 set_light_sens_level(hotk->light_level);
Corentin Chary8b857352007-01-26 14:04:58 +01001315
Corentin Charye539c2f2007-05-06 14:47:06 +02001316 /* GPS is on by default */
1317 write_status(NULL, 1, GPS_ON);
1318
Corentin Charyed6f4422009-01-20 16:17:45 +01001319end:
Corentin Chary85091b72007-01-26 14:04:30 +01001320 if (result) {
1321 kfree(hotk->name);
1322 kfree(hotk);
1323 }
1324
1325 return result;
1326}
1327
1328static int asus_hotk_remove(struct acpi_device *device, int type)
1329{
Corentin Chary85091b72007-01-26 14:04:30 +01001330 kfree(hotk->name);
1331 kfree(hotk);
1332
1333 return 0;
1334}
1335
Corentin Chary6b7091e2007-01-26 14:04:45 +01001336static void asus_backlight_exit(void)
1337{
Len Brown8def05f2007-01-30 01:46:43 -05001338 if (asus_backlight_device)
Corentin Chary6b7091e2007-01-26 14:04:45 +01001339 backlight_device_unregister(asus_backlight_device);
1340}
1341
Corentin Charybe18cda2007-01-26 14:04:35 +01001342#define ASUS_LED_UNREGISTER(object) \
Guillaume Chazaraine1996a692007-08-16 18:18:53 +02001343 if (object##_led.dev) \
1344 led_classdev_unregister(&object##_led)
Corentin Charybe18cda2007-01-26 14:04:35 +01001345
1346static void asus_led_exit(void)
1347{
Al Viro3b0d7112007-07-23 11:21:34 +01001348 destroy_workqueue(led_workqueue);
Corentin Charybe18cda2007-01-26 14:04:35 +01001349 ASUS_LED_UNREGISTER(mled);
1350 ASUS_LED_UNREGISTER(tled);
1351 ASUS_LED_UNREGISTER(pled);
1352 ASUS_LED_UNREGISTER(rled);
Corentin Charyfdd8d082007-03-11 10:26:48 +01001353 ASUS_LED_UNREGISTER(gled);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001354 ASUS_LED_UNREGISTER(kled);
Corentin Charybe18cda2007-01-26 14:04:35 +01001355}
1356
Corentin Chary034ce902009-01-20 16:17:43 +01001357static void asus_input_exit(void)
1358{
1359 if (hotk->inputdev)
1360 input_unregister_device(hotk->inputdev);
1361}
1362
Corentin Chary85091b72007-01-26 14:04:30 +01001363static void __exit asus_laptop_exit(void)
1364{
Corentin Chary6b7091e2007-01-26 14:04:45 +01001365 asus_backlight_exit();
Corentin Charybe18cda2007-01-26 14:04:35 +01001366 asus_led_exit();
Corentin Chary034ce902009-01-20 16:17:43 +01001367 asus_input_exit();
Corentin Charybe18cda2007-01-26 14:04:35 +01001368
Corentin Chary85091b72007-01-26 14:04:30 +01001369 acpi_bus_unregister_driver(&asus_hotk_driver);
Len Brown8def05f2007-01-30 01:46:43 -05001370 sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
1371 platform_device_unregister(asuspf_device);
1372 platform_driver_unregister(&asuspf_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001373}
1374
Len Brown8def05f2007-01-30 01:46:43 -05001375static int asus_backlight_init(struct device *dev)
Corentin Chary6b7091e2007-01-26 14:04:45 +01001376{
1377 struct backlight_device *bd;
1378
Len Brown8def05f2007-01-30 01:46:43 -05001379 if (brightness_set_handle && lcd_switch_handle) {
1380 bd = backlight_device_register(ASUS_HOTK_FILE, dev,
Richard Purdie599a52d2007-02-10 23:07:48 +00001381 NULL, &asusbl_ops);
Len Brown8def05f2007-01-30 01:46:43 -05001382 if (IS_ERR(bd)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001383 pr_err("Could not register asus backlight device\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +01001384 asus_backlight_device = NULL;
1385 return PTR_ERR(bd);
1386 }
1387
1388 asus_backlight_device = bd;
1389
Richard Purdie599a52d2007-02-10 23:07:48 +00001390 bd->props.max_brightness = 15;
1391 bd->props.brightness = read_brightness(NULL);
1392 bd->props.power = FB_BLANK_UNBLANK;
Richard Purdie28ee0862007-02-08 22:25:09 +00001393 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001394 }
1395 return 0;
1396}
1397
Corentin Charybe18cda2007-01-26 14:04:35 +01001398static int asus_led_register(acpi_handle handle,
Len Brown8def05f2007-01-30 01:46:43 -05001399 struct led_classdev *ldev, struct device *dev)
Corentin Charybe18cda2007-01-26 14:04:35 +01001400{
Len Brown8def05f2007-01-30 01:46:43 -05001401 if (!handle)
Corentin Charybe18cda2007-01-26 14:04:35 +01001402 return 0;
1403
1404 return led_classdev_register(dev, ldev);
1405}
Len Brown8def05f2007-01-30 01:46:43 -05001406
Corentin Charybe18cda2007-01-26 14:04:35 +01001407#define ASUS_LED_REGISTER(object, device) \
1408 asus_led_register(object##_set_handle, &object##_led, device)
1409
Len Brown8def05f2007-01-30 01:46:43 -05001410static int asus_led_init(struct device *dev)
Corentin Charybe18cda2007-01-26 14:04:35 +01001411{
1412 int rv;
1413
1414 rv = ASUS_LED_REGISTER(mled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001415 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001416 goto out;
Corentin Charybe18cda2007-01-26 14:04:35 +01001417
1418 rv = ASUS_LED_REGISTER(tled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001419 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001420 goto out1;
Corentin Charybe18cda2007-01-26 14:04:35 +01001421
1422 rv = ASUS_LED_REGISTER(rled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001423 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001424 goto out2;
Corentin Charybe18cda2007-01-26 14:04:35 +01001425
1426 rv = ASUS_LED_REGISTER(pled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001427 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001428 goto out3;
Corentin Charybe18cda2007-01-26 14:04:35 +01001429
Corentin Charyfdd8d082007-03-11 10:26:48 +01001430 rv = ASUS_LED_REGISTER(gled, dev);
1431 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001432 goto out4;
Corentin Charyfdd8d082007-03-11 10:26:48 +01001433
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001434 if (kled_set_handle && kled_get_handle)
1435 rv = ASUS_LED_REGISTER(kled, dev);
1436 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001437 goto out5;
Corentin Charybe18cda2007-01-26 14:04:35 +01001438
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001439 led_workqueue = create_singlethread_workqueue("led_workqueue");
1440 if (!led_workqueue)
1441 goto out6;
1442
Corentin Charybe18cda2007-01-26 14:04:35 +01001443 return 0;
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001444out6:
Al Viro3b0d7112007-07-23 11:21:34 +01001445 rv = -ENOMEM;
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001446 ASUS_LED_UNREGISTER(kled);
1447out5:
Al Viro3b0d7112007-07-23 11:21:34 +01001448 ASUS_LED_UNREGISTER(gled);
1449out4:
1450 ASUS_LED_UNREGISTER(pled);
1451out3:
1452 ASUS_LED_UNREGISTER(rled);
1453out2:
1454 ASUS_LED_UNREGISTER(tled);
1455out1:
1456 ASUS_LED_UNREGISTER(mled);
1457out:
1458 return rv;
Corentin Charybe18cda2007-01-26 14:04:35 +01001459}
1460
Corentin Chary85091b72007-01-26 14:04:30 +01001461static int __init asus_laptop_init(void)
1462{
1463 int result;
1464
Corentin Chary85091b72007-01-26 14:04:30 +01001465 result = acpi_bus_register_driver(&asus_hotk_driver);
1466 if (result < 0)
1467 return result;
1468
1469 /*
1470 * This is a bit of a kludge. We only want this module loaded
1471 * for ASUS systems, but there's currently no way to probe the
1472 * ACPI namespace for ASUS HIDs. So we just return failure if
1473 * we didn't find one, which will cause the module to be
1474 * unloaded.
1475 */
1476 if (!asus_hotk_found) {
1477 acpi_bus_unregister_driver(&asus_hotk_driver);
1478 return -ENODEV;
1479 }
1480
Corentin Chary034ce902009-01-20 16:17:43 +01001481 result = asus_input_init();
1482 if (result)
1483 goto fail_input;
1484
Len Brown8def05f2007-01-30 01:46:43 -05001485 /* Register platform stuff */
Corentin Chary85091b72007-01-26 14:04:30 +01001486 result = platform_driver_register(&asuspf_driver);
Len Brown8def05f2007-01-30 01:46:43 -05001487 if (result)
1488 goto fail_platform_driver;
Corentin Chary85091b72007-01-26 14:04:30 +01001489
Len Brown8def05f2007-01-30 01:46:43 -05001490 asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
1491 if (!asuspf_device) {
1492 result = -ENOMEM;
1493 goto fail_platform_device1;
1494 }
Corentin Chary85091b72007-01-26 14:04:30 +01001495
Len Brown8def05f2007-01-30 01:46:43 -05001496 result = platform_device_add(asuspf_device);
1497 if (result)
1498 goto fail_platform_device2;
Corentin Chary85091b72007-01-26 14:04:30 +01001499
Len Brown8def05f2007-01-30 01:46:43 -05001500 result = sysfs_create_group(&asuspf_device->dev.kobj,
Corentin Chary85091b72007-01-26 14:04:30 +01001501 &asuspf_attribute_group);
Len Brown8def05f2007-01-30 01:46:43 -05001502 if (result)
1503 goto fail_sysfs;
Corentin Chary85091b72007-01-26 14:04:30 +01001504
Corentin Chary116bf2e2009-06-16 19:28:46 +00001505 result = asus_led_init(&asuspf_device->dev);
1506 if (result)
1507 goto fail_led;
1508
1509 if (!acpi_video_backlight_support()) {
1510 result = asus_backlight_init(&asuspf_device->dev);
1511 if (result)
1512 goto fail_backlight;
1513 } else
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001514 pr_info("Brightness ignored, must be controlled by "
Corentin Chary116bf2e2009-06-16 19:28:46 +00001515 "ACPI video driver\n");
1516
Len Brown8def05f2007-01-30 01:46:43 -05001517 return 0;
Corentin Chary85091b72007-01-26 14:04:30 +01001518
Corentin Chary116bf2e2009-06-16 19:28:46 +00001519fail_backlight:
1520 asus_led_exit();
1521
1522fail_led:
1523 sysfs_remove_group(&asuspf_device->dev.kobj,
1524 &asuspf_attribute_group);
1525
Corentin Charyed6f4422009-01-20 16:17:45 +01001526fail_sysfs:
Len Brown8def05f2007-01-30 01:46:43 -05001527 platform_device_del(asuspf_device);
Corentin Chary85091b72007-01-26 14:04:30 +01001528
Corentin Charyed6f4422009-01-20 16:17:45 +01001529fail_platform_device2:
Corentin Chary85091b72007-01-26 14:04:30 +01001530 platform_device_put(asuspf_device);
1531
Corentin Charyed6f4422009-01-20 16:17:45 +01001532fail_platform_device1:
Len Brown8def05f2007-01-30 01:46:43 -05001533 platform_driver_unregister(&asuspf_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001534
Corentin Charyed6f4422009-01-20 16:17:45 +01001535fail_platform_driver:
Corentin Chary034ce902009-01-20 16:17:43 +01001536 asus_input_exit();
1537
Corentin Charyed6f4422009-01-20 16:17:45 +01001538fail_input:
Corentin Chary85091b72007-01-26 14:04:30 +01001539
1540 return result;
1541}
1542
1543module_init(asus_laptop_init);
1544module_exit(asus_laptop_exit);