blob: 940ce3d632291511458c0ac03ee82c1043118d18 [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 Chary85091b72007-01-26 14:04:30 +010061MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
Corentin Chary50a90c42009-11-30 21:55:12 +010062MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
Corentin Chary85091b72007-01-26 14:04:30 +010063MODULE_LICENSE("GPL");
64
Corentin Charybe966662009-08-29 10:28:29 +020065/*
66 * WAPF defines the behavior of the Fn+Fx wlan key
Corentin Chary185e5af2007-03-11 10:27:33 +010067 * The significance of values is yet to be found, but
68 * most of the time:
69 * 0x0 will do nothing
70 * 0x1 will allow to control the device with Fn+Fx key.
71 * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
72 * 0x5 like 0x1 or 0x4
73 * So, if something doesn't work as you want, just try other values =)
74 */
75static uint wapf = 1;
76module_param(wapf, uint, 0644);
77MODULE_PARM_DESC(wapf, "WAPF value");
78
Corentin Chary0e875f42010-01-10 20:49:26 +010079static uint wireless_status = 1;
80static uint bluetooth_status = 1;
81
82module_param(wireless_status, uint, 0644);
83MODULE_PARM_DESC(wireless_status, "Set the wireless status on boot "
84 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
85 "default is 1");
86
87module_param(bluetooth_status, uint, 0644);
88MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
89 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
90 "default is 1");
91
Corentin Charybe4ee822009-12-06 16:27:09 +010092/*
93 * Some events we use, same for all Asus
94 */
95#define ATKD_BR_UP 0x10
96#define ATKD_BR_DOWN 0x20
97#define ATKD_LCD_ON 0x33
98#define ATKD_LCD_OFF 0x34
99
100/*
101 * Known bits returned by \_SB.ATKD.HWRS
102 */
103#define WL_HWRS 0x80
104#define BT_HWRS 0x100
105
106/*
107 * Flags for hotk status
108 * WL_ON and BT_ON are also used for wireless_status()
109 */
110#define WL_ON 0x01 /* internal Wifi */
111#define BT_ON 0x02 /* internal Bluetooth */
112#define MLED_ON 0x04 /* mail LED */
113#define TLED_ON 0x08 /* touchpad LED */
114#define RLED_ON 0x10 /* Record LED */
115#define PLED_ON 0x20 /* Phone LED */
116#define GLED_ON 0x40 /* Gaming LED */
117#define LCD_ON 0x80 /* LCD backlight */
118#define GPS_ON 0x100 /* GPS */
119#define KEY_ON 0x200 /* Keyboard backlight */
120
Corentin Chary85091b72007-01-26 14:04:30 +0100121#define ASUS_HANDLE(object, paths...) \
122 static acpi_handle object##_handle = NULL; \
123 static char *object##_paths[] = { paths }
124
Corentin Charybe18cda2007-01-26 14:04:35 +0100125/* LED */
Corentin Chary50a90c42009-11-30 21:55:12 +0100126ASUS_HANDLE(mled_set, ASUS_LAPTOP_PREFIX "MLED");
127ASUS_HANDLE(tled_set, ASUS_LAPTOP_PREFIX "TLED");
128ASUS_HANDLE(rled_set, ASUS_LAPTOP_PREFIX "RLED"); /* W1JC */
129ASUS_HANDLE(pled_set, ASUS_LAPTOP_PREFIX "PLED"); /* A7J */
130ASUS_HANDLE(gled_set, ASUS_LAPTOP_PREFIX "GLED"); /* G1, G2 (probably) */
Corentin Charybe18cda2007-01-26 14:04:35 +0100131
Corentin Chary722ad972007-01-26 14:04:55 +0100132/* LEDD */
Corentin Chary50a90c42009-11-30 21:55:12 +0100133ASUS_HANDLE(ledd_set, ASUS_LAPTOP_PREFIX "SLCM");
Corentin Chary722ad972007-01-26 14:04:55 +0100134
Corentin Charybe966662009-08-29 10:28:29 +0200135/*
136 * Bluetooth and WLAN
Corentin Chary4564de12007-01-26 14:04:40 +0100137 * WLED and BLED are not handled like other XLED, because in some dsdt
138 * they also control the WLAN/Bluetooth device.
139 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100140ASUS_HANDLE(wl_switch, ASUS_LAPTOP_PREFIX "WLED");
141ASUS_HANDLE(bt_switch, ASUS_LAPTOP_PREFIX "BLED");
142ASUS_HANDLE(wireless_status, ASUS_LAPTOP_PREFIX "RSTS"); /* All new models */
Corentin Chary4564de12007-01-26 14:04:40 +0100143
Corentin Chary6b7091e2007-01-26 14:04:45 +0100144/* Brightness */
Corentin Chary50a90c42009-11-30 21:55:12 +0100145ASUS_HANDLE(brightness_set, ASUS_LAPTOP_PREFIX "SPLV");
146ASUS_HANDLE(brightness_get, ASUS_LAPTOP_PREFIX "GPLV");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100147
148/* Backlight */
Len Brown8def05f2007-01-30 01:46:43 -0500149ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
150 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
151 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
152 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
153 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
Torsten Krah4d0b856e2008-10-17 09:47:57 +0200154 "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
Len Brown8def05f2007-01-30 01:46:43 -0500155 "\\_SB.PCI0.PX40.Q10", /* S1x */
156 "\\Q10"); /* A2x, L2D, L3D, M2E */
Corentin Chary6b7091e2007-01-26 14:04:45 +0100157
Corentin Chary78127b42007-01-26 14:04:49 +0100158/* Display */
Corentin Chary50a90c42009-11-30 21:55:12 +0100159ASUS_HANDLE(display_set, ASUS_LAPTOP_PREFIX "SDSP");
Corentin Charybe966662009-08-29 10:28:29 +0200160ASUS_HANDLE(display_get,
161 /* A6B, A6K A6R A7D F3JM L4R M6R A3G M6A M6V VX-1 V6J V6V W3Z */
162 "\\_SB.PCI0.P0P1.VGA.GETD",
163 /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V S5A M5A z33A W1Jc W2V G1 */
164 "\\_SB.PCI0.P0P2.VGA.GETD",
165 /* A6V A6Q */
166 "\\_SB.PCI0.P0P3.VGA.GETD",
167 /* A6T, A6M */
168 "\\_SB.PCI0.P0PA.VGA.GETD",
169 /* L3C */
170 "\\_SB.PCI0.PCI1.VGAC.NMAP",
171 /* Z96F */
172 "\\_SB.PCI0.VGA.GETD",
173 /* A2D */
174 "\\ACTD",
175 /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
176 "\\ADVG",
177 /* P30 */
178 "\\DNXT",
179 /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
180 "\\INFB",
181 /* A3F A6F A3N A3L M6N W3N W6A */
182 "\\SSTE");
Corentin Chary78127b42007-01-26 14:04:49 +0100183
Corentin Chary50a90c42009-11-30 21:55:12 +0100184ASUS_HANDLE(ls_switch, ASUS_LAPTOP_PREFIX "ALSC"); /* Z71A Z71V */
185ASUS_HANDLE(ls_level, ASUS_LAPTOP_PREFIX "ALSL"); /* Z71A Z71V */
Corentin Chary8b857352007-01-26 14:04:58 +0100186
Corentin Charye539c2f2007-05-06 14:47:06 +0200187/* GPS */
188/* R2H use different handle for GPS on/off */
Corentin Chary50a90c42009-11-30 21:55:12 +0100189ASUS_HANDLE(gps_on, ASUS_LAPTOP_PREFIX "SDON"); /* R2H */
190ASUS_HANDLE(gps_off, ASUS_LAPTOP_PREFIX "SDOF"); /* R2H */
191ASUS_HANDLE(gps_status, ASUS_LAPTOP_PREFIX "GPST");
Corentin Charye539c2f2007-05-06 14:47:06 +0200192
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000193/* Keyboard light */
Corentin Chary50a90c42009-11-30 21:55:12 +0100194ASUS_HANDLE(kled_set, ASUS_LAPTOP_PREFIX "SLKB");
195ASUS_HANDLE(kled_get, ASUS_LAPTOP_PREFIX "GLKB");
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000196
Corentin Chary85091b72007-01-26 14:04:30 +0100197/*
Corentin Chary9129d142009-12-01 22:39:41 +0100198 * Define a specific led structure to keep the main structure clean
199 */
200#define ASUS_DEFINE_LED(object) \
201 int object##_wk; \
202 struct work_struct object##_work; \
203 struct led_classdev object;
204
205
206#define led_to_asus(led_cdev, led) \
207 container_of(container_of(led_cdev, struct asus_laptop_leds, \
208 led), \
209 struct asus_laptop, leds)
210#define work_to_asus(work, led) \
211 container_of(container_of(work, struct asus_laptop_leds, \
212 led##_work), \
213 struct asus_laptop, leds)
214
215struct asus_laptop_leds {
216 ASUS_DEFINE_LED(mled)
217 ASUS_DEFINE_LED(tled)
218 ASUS_DEFINE_LED(rled)
219 ASUS_DEFINE_LED(pled)
220 ASUS_DEFINE_LED(gled)
221 ASUS_DEFINE_LED(kled)
222 struct workqueue_struct *workqueue;
223};
224
225/*
Corentin Chary85091b72007-01-26 14:04:30 +0100226 * This is the main structure, we can use it to store anything interesting
227 * about the hotk device
228 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100229struct asus_laptop {
Corentin Charybe966662009-08-29 10:28:29 +0200230 char *name; /* laptop name */
Corentin Chary600ad522009-11-30 21:42:42 +0100231
Corentin Chary7c247642009-11-30 22:13:54 +0100232 struct acpi_table_header *dsdt_info;
Corentin Chary600ad522009-11-30 21:42:42 +0100233 struct platform_device *platform_device;
Corentin Chary7c247642009-11-30 22:13:54 +0100234 struct acpi_device *device; /* the device we are in */
235 struct backlight_device *backlight_device;
Corentin Chary9129d142009-12-01 22:39:41 +0100236
Corentin Chary7c247642009-11-30 22:13:54 +0100237 struct input_dev *inputdev;
Corentin Chary9129d142009-12-01 22:39:41 +0100238 struct key_entry *keymap;
239
240 struct asus_laptop_leds leds;
Corentin Chary7c247642009-11-30 22:13:54 +0100241
Corentin Charybe966662009-08-29 10:28:29 +0200242 acpi_handle handle; /* the handle of the hotk device */
243 char status; /* status of the hotk, for LEDs, ... */
244 u32 ledd_status; /* status of the LED display */
245 u8 light_level; /* light sensor level */
246 u8 light_switch; /* light sensor switch value */
247 u16 event_count[128]; /* count for each event TODO make this better */
Corentin Chary034ce902009-01-20 16:17:43 +0100248 u16 *keycode_map;
Corentin Chary85091b72007-01-26 14:04:30 +0100249};
250
Corentin Chary034ce902009-01-20 16:17:43 +0100251struct key_entry {
252 char type;
253 u8 code;
254 u16 keycode;
255};
256
257enum { KE_KEY, KE_END };
258
Corentin Chary9129d142009-12-01 22:39:41 +0100259static const struct key_entry asus_keymap[] = {
Ike Panhc14f8af32009-12-03 07:45:11 +0000260 {KE_KEY, 0x02, KEY_SCREENLOCK},
261 {KE_KEY, 0x05, KEY_WLAN},
262 {KE_KEY, 0x08, KEY_F13},
263 {KE_KEY, 0x17, KEY_ZOOM},
264 {KE_KEY, 0x1f, KEY_BATTERY},
Corentin Chary034ce902009-01-20 16:17:43 +0100265 {KE_KEY, 0x30, KEY_VOLUMEUP},
266 {KE_KEY, 0x31, KEY_VOLUMEDOWN},
267 {KE_KEY, 0x32, KEY_MUTE},
268 {KE_KEY, 0x33, KEY_SWITCHVIDEOMODE},
269 {KE_KEY, 0x34, KEY_SWITCHVIDEOMODE},
270 {KE_KEY, 0x40, KEY_PREVIOUSSONG},
271 {KE_KEY, 0x41, KEY_NEXTSONG},
Corentin Chary309f5fb2009-04-27 09:23:42 +0200272 {KE_KEY, 0x43, KEY_STOPCD},
Corentin Chary034ce902009-01-20 16:17:43 +0100273 {KE_KEY, 0x45, KEY_PLAYPAUSE},
Corentin Chary4644d0e2009-08-28 12:56:52 +0000274 {KE_KEY, 0x4c, KEY_MEDIA},
Corentin Chary034ce902009-01-20 16:17:43 +0100275 {KE_KEY, 0x50, KEY_EMAIL},
276 {KE_KEY, 0x51, KEY_WWW},
Corentin Chary0aa20f72009-08-28 12:56:53 +0000277 {KE_KEY, 0x55, KEY_CALC},
Corentin Chary309f5fb2009-04-27 09:23:42 +0200278 {KE_KEY, 0x5C, KEY_SCREENLOCK}, /* Screenlock */
Corentin Chary034ce902009-01-20 16:17:43 +0100279 {KE_KEY, 0x5D, KEY_WLAN},
Corentin Charyf6413752009-08-28 12:56:48 +0000280 {KE_KEY, 0x5E, KEY_WLAN},
281 {KE_KEY, 0x5F, KEY_WLAN},
282 {KE_KEY, 0x60, KEY_SWITCHVIDEOMODE},
Corentin Chary034ce902009-01-20 16:17:43 +0100283 {KE_KEY, 0x61, KEY_SWITCHVIDEOMODE},
Ike Panhc14f8af32009-12-03 07:45:11 +0000284 {KE_KEY, 0x62, KEY_SWITCHVIDEOMODE},
285 {KE_KEY, 0x63, KEY_SWITCHVIDEOMODE},
Corentin Chary16851f92009-12-03 07:45:10 +0000286 {KE_KEY, 0x6B, KEY_F13}, /* Lock Touchpad */
Corentin Chary034ce902009-01-20 16:17:43 +0100287 {KE_KEY, 0x82, KEY_CAMERA},
Corentin Chary1f0233e2009-12-03 07:45:12 +0000288 {KE_KEY, 0x88, KEY_WLAN },
Corentin Chary309f5fb2009-04-27 09:23:42 +0200289 {KE_KEY, 0x8A, KEY_PROG1},
Corentin Chary034ce902009-01-20 16:17:43 +0100290 {KE_KEY, 0x95, KEY_MEDIA},
291 {KE_KEY, 0x99, KEY_PHONE},
Corentin Charydc795262009-08-28 12:56:51 +0000292 {KE_KEY, 0xc4, KEY_KBDILLUMUP},
293 {KE_KEY, 0xc5, KEY_KBDILLUMDOWN},
Corentin Chary034ce902009-01-20 16:17:43 +0100294 {KE_END, 0},
295};
296
Corentin Chary85091b72007-01-26 14:04:30 +0100297/*
298 * This function evaluates an ACPI method, given an int as parameter, the
299 * method is searched within the scope of the handle, can be NULL. The output
300 * of the method is written is output, which can also be NULL
301 *
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100302 * returns 0 if write is successful, -1 else.
Corentin Chary85091b72007-01-26 14:04:30 +0100303 */
Corentin Charyd8c67322009-11-28 10:27:51 +0100304static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
305 struct acpi_buffer *output)
Corentin Chary85091b72007-01-26 14:04:30 +0100306{
Corentin Charybe966662009-08-29 10:28:29 +0200307 struct acpi_object_list params; /* list of input parameters (an int) */
308 union acpi_object in_obj; /* the only param we use */
Corentin Chary85091b72007-01-26 14:04:30 +0100309 acpi_status status;
310
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100311 if (!handle)
312 return 0;
313
Corentin Chary85091b72007-01-26 14:04:30 +0100314 params.count = 1;
315 params.pointer = &in_obj;
316 in_obj.type = ACPI_TYPE_INTEGER;
317 in_obj.integer.value = val;
318
319 status = acpi_evaluate_object(handle, (char *)method, &params, output);
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100320 if (status == AE_OK)
321 return 0;
322 else
323 return -1;
Corentin Chary85091b72007-01-26 14:04:30 +0100324}
325
Corentin Charyd8c67322009-11-28 10:27:51 +0100326static int write_acpi_int(acpi_handle handle, const char *method, int val)
327{
328 return write_acpi_int_ret(handle, method, val, NULL);
329}
330
Corentin Chary9129d142009-12-01 22:39:41 +0100331static int read_wireless_status(struct asus_laptop *asus, int mask)
Len Brown8def05f2007-01-30 01:46:43 -0500332{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400333 unsigned long long status;
Corentin Chary9a816852007-03-11 10:25:38 +0100334 acpi_status rv = AE_OK;
Corentin Chary4564de12007-01-26 14:04:40 +0100335
336 if (!wireless_status_handle)
Corentin Chary50a90c42009-11-30 21:55:12 +0100337 return (asus->status & mask) ? 1 : 0;
Corentin Chary4564de12007-01-26 14:04:40 +0100338
Corentin Chary9a816852007-03-11 10:25:38 +0100339 rv = acpi_evaluate_integer(wireless_status_handle, NULL, NULL, &status);
340 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200341 pr_warning("Error reading Wireless status\n");
Corentin Chary9a816852007-03-11 10:25:38 +0100342 else
343 return (status & mask) ? 1 : 0;
Corentin Chary4564de12007-01-26 14:04:40 +0100344
Corentin Chary50a90c42009-11-30 21:55:12 +0100345 return (asus->status & mask) ? 1 : 0;
Corentin Chary4564de12007-01-26 14:04:40 +0100346}
347
Corentin Chary9129d142009-12-01 22:39:41 +0100348static int read_gps_status(struct asus_laptop *asus)
Corentin Charyf3985322007-05-06 14:48:22 +0200349{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400350 unsigned long long status;
Corentin Charye539c2f2007-05-06 14:47:06 +0200351 acpi_status rv = AE_OK;
352
353 rv = acpi_evaluate_integer(gps_status_handle, NULL, NULL, &status);
354 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200355 pr_warning("Error reading GPS status\n");
Corentin Charye539c2f2007-05-06 14:47:06 +0200356 else
Corentin Charyf3985322007-05-06 14:48:22 +0200357 return status ? 1 : 0;
Corentin Charye539c2f2007-05-06 14:47:06 +0200358
Corentin Chary50a90c42009-11-30 21:55:12 +0100359 return (asus->status & GPS_ON) ? 1 : 0;
Corentin Charye539c2f2007-05-06 14:47:06 +0200360}
361
Corentin Charybe18cda2007-01-26 14:04:35 +0100362/* Generic LED functions */
Corentin Chary9129d142009-12-01 22:39:41 +0100363static int read_status(struct asus_laptop *asus, int mask)
Corentin Charybe18cda2007-01-26 14:04:35 +0100364{
Corentin Chary4564de12007-01-26 14:04:40 +0100365 /* There is a special method for both wireless devices */
366 if (mask == BT_ON || mask == WL_ON)
Corentin Chary9129d142009-12-01 22:39:41 +0100367 return read_wireless_status(asus, mask);
Corentin Charyf3985322007-05-06 14:48:22 +0200368 else if (mask == GPS_ON)
Corentin Chary9129d142009-12-01 22:39:41 +0100369 return read_gps_status(asus);
Corentin Chary4564de12007-01-26 14:04:40 +0100370
Corentin Chary50a90c42009-11-30 21:55:12 +0100371 return (asus->status & mask) ? 1 : 0;
Corentin Charybe18cda2007-01-26 14:04:35 +0100372}
373
Corentin Chary9129d142009-12-01 22:39:41 +0100374static void write_status(struct asus_laptop *asus, acpi_handle handle,
375 int out, int mask)
Corentin Charybe18cda2007-01-26 14:04:35 +0100376{
Corentin Chary50a90c42009-11-30 21:55:12 +0100377 asus->status = (out) ? (asus->status | mask) : (asus->status & ~mask);
Corentin Charybe18cda2007-01-26 14:04:35 +0100378
Corentin Chary935ffee2007-03-11 10:26:12 +0100379 switch (mask) {
380 case MLED_ON:
Roel Kluine1af14e2008-02-02 21:07:38 +0100381 out = !(out & 0x1);
Corentin Chary935ffee2007-03-11 10:26:12 +0100382 break;
Corentin Charyfdd8d082007-03-11 10:26:48 +0100383 case GLED_ON:
384 out = (out & 0x1) + 1;
385 break;
Corentin Charye539c2f2007-05-06 14:47:06 +0200386 case GPS_ON:
387 handle = (out) ? gps_on_handle : gps_off_handle;
388 out = 0x02;
389 break;
Corentin Chary935ffee2007-03-11 10:26:12 +0100390 default:
391 out &= 0x1;
392 break;
393 }
Corentin Charybe18cda2007-01-26 14:04:35 +0100394
Corentin Charyd8c67322009-11-28 10:27:51 +0100395 if (write_acpi_int(handle, NULL, out))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200396 pr_warning(" write failed %x\n", mask);
Corentin Charybe18cda2007-01-26 14:04:35 +0100397}
398
Corentin Charybe4ee822009-12-06 16:27:09 +0100399/*
400 * LEDs
401 */
402#define ASUS_LED(object, ledname, max) \
403 static void object##_led_set(struct led_classdev *led_cdev, \
404 enum led_brightness value); \
405 static enum led_brightness object##_led_get( \
406 struct led_classdev *led_cdev); \
407 static void object##_led_update(struct work_struct *ignored); \
408 static struct led_classdev object##_led = { \
409 .name = "asus::" ledname, \
410 .brightness_set = object##_led_set, \
411 .brightness_get = object##_led_get, \
412 .max_brightness = max \
413 }
414
415ASUS_LED(mled, "mail", 1);
416ASUS_LED(tled, "touchpad", 1);
417ASUS_LED(rled, "record", 1);
418ASUS_LED(pled, "phone", 1);
419ASUS_LED(gled, "gaming", 1);
420ASUS_LED(kled, "kbd_backlight", 3);
421
Corentin Charybe18cda2007-01-26 14:04:35 +0100422/* /sys/class/led handlers */
Corentin Chary935ffee2007-03-11 10:26:12 +0100423#define ASUS_LED_HANDLER(object, mask) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100424 static void object##_led_set(struct led_classdev *led_cdev, \
425 enum led_brightness value) \
426 { \
Corentin Chary9129d142009-12-01 22:39:41 +0100427 struct asus_laptop *asus = \
428 led_to_asus(led_cdev, object); \
429 \
430 asus->leds.object##_wk = (value > 0) ? 1 : 0; \
431 queue_work(asus->leds.workqueue, \
432 &asus->leds.object##_work); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100433 } \
Corentin Chary9129d142009-12-01 22:39:41 +0100434 static void object##_led_update(struct work_struct *work) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100435 { \
Corentin Chary9129d142009-12-01 22:39:41 +0100436 struct asus_laptop *asus = work_to_asus(work, object); \
437 \
438 int value = asus->leds.object##_wk; \
439 write_status(asus, object##_set_handle, value, (mask)); \
Corentin Charyabfa57e2009-08-28 12:56:47 +0000440 } \
441 static enum led_brightness object##_led_get( \
442 struct led_classdev *led_cdev) \
443 { \
444 return led_cdev->brightness; \
Corentin Charybe18cda2007-01-26 14:04:35 +0100445 }
446
Corentin Chary935ffee2007-03-11 10:26:12 +0100447ASUS_LED_HANDLER(mled, MLED_ON);
448ASUS_LED_HANDLER(pled, PLED_ON);
449ASUS_LED_HANDLER(rled, RLED_ON);
450ASUS_LED_HANDLER(tled, TLED_ON);
Corentin Charyfdd8d082007-03-11 10:26:48 +0100451ASUS_LED_HANDLER(gled, GLED_ON);
Corentin Charybe18cda2007-01-26 14:04:35 +0100452
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000453/*
Corentin Charybe4ee822009-12-06 16:27:09 +0100454 * Keyboard backlight (also a LED)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000455 */
456static int get_kled_lvl(void)
457{
458 unsigned long long kblv;
459 struct acpi_object_list params;
460 union acpi_object in_obj;
461 acpi_status rv;
462
463 params.count = 1;
464 params.pointer = &in_obj;
465 in_obj.type = ACPI_TYPE_INTEGER;
466 in_obj.integer.value = 2;
467
468 rv = acpi_evaluate_integer(kled_get_handle, NULL, &params, &kblv);
469 if (ACPI_FAILURE(rv)) {
470 pr_warning("Error reading kled level\n");
471 return 0;
472 }
473 return kblv;
474}
475
Corentin Chary9129d142009-12-01 22:39:41 +0100476static int set_kled_lvl(struct asus_laptop *asus, int kblv)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000477{
478 if (kblv > 0)
479 kblv = (1 << 7) | (kblv & 0x7F);
480 else
481 kblv = 0;
482
Corentin Charyd8c67322009-11-28 10:27:51 +0100483 if (write_acpi_int(kled_set_handle, NULL, kblv)) {
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000484 pr_warning("Keyboard LED display write failed\n");
485 return -EINVAL;
486 }
487 return 0;
488}
489
490static void kled_led_set(struct led_classdev *led_cdev,
491 enum led_brightness value)
492{
Corentin Chary9129d142009-12-01 22:39:41 +0100493 struct asus_laptop *asus = led_to_asus(led_cdev, kled);
494
495 asus->leds.kled_wk = value;
496 queue_work(asus->leds.workqueue, &asus->leds.kled_work);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000497}
498
Corentin Chary9129d142009-12-01 22:39:41 +0100499static void kled_led_update(struct work_struct *work)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000500{
Corentin Chary9129d142009-12-01 22:39:41 +0100501 struct asus_laptop *asus = work_to_asus(work, kled);
502
503 set_kled_lvl(asus, asus->leds.kled_wk);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000504}
505
506static enum led_brightness kled_led_get(struct led_classdev *led_cdev)
507{
508 return get_kled_lvl();
509}
510
Corentin Charybe4ee822009-12-06 16:27:09 +0100511#define ASUS_LED_UNREGISTER(object) \
512 if (object##_led.dev) \
513 led_classdev_unregister(&object##_led)
514
515static void asus_led_exit(struct asus_laptop *asus)
516{
517 ASUS_LED_UNREGISTER(mled);
518 ASUS_LED_UNREGISTER(tled);
519 ASUS_LED_UNREGISTER(pled);
520 ASUS_LED_UNREGISTER(rled);
521 ASUS_LED_UNREGISTER(gled);
522 ASUS_LED_UNREGISTER(kled);
523 if (asus->leds.workqueue) {
524 destroy_workqueue(asus->leds.workqueue);
525 asus->leds.workqueue = NULL;
526 }
527}
528
529/* Ugly macro, need to fix that later */
530#define ASUS_LED_REGISTER(asus, object, _name, max) \
531 do { \
532 struct led_classdev *ldev = &asus->leds.object; \
533 if (!object##_set_handle) \
534 break ; \
535 \
536 INIT_WORK(&asus->leds.object##_work, object##_led_update); \
537 ldev->name = "asus::" _name; \
538 ldev->brightness_set = object##_led_set; \
539 ldev->max_brightness = max; \
540 rv = led_classdev_register(&asus->platform_device->dev, ldev); \
541 if (rv) \
542 goto error; \
543 } while (0)
544
545static int asus_led_init(struct asus_laptop *asus)
546{
547 int rv;
548
549 /*
550 * Functions that actually update the LED's are called from a
551 * workqueue. By doing this as separate work rather than when the LED
552 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
553 * potentially bad time, such as a timer interrupt.
554 */
555 asus->leds.workqueue = create_singlethread_workqueue("led_workqueue");
556 if (!asus->leds.workqueue)
557 return -ENOMEM;
558
559 ASUS_LED_REGISTER(asus, mled, "mail", 1);
560 ASUS_LED_REGISTER(asus, tled, "touchpad", 1);
561 ASUS_LED_REGISTER(asus, rled, "record", 1);
562 ASUS_LED_REGISTER(asus, pled, "phone", 1);
563 ASUS_LED_REGISTER(asus, gled, "gaming", 1);
564 if (kled_set_handle && kled_get_handle)
565 ASUS_LED_REGISTER(asus, kled, "kbd_backlight", 3);
566error:
567 if (rv)
568 asus_led_exit(asus);
569 return rv;
570}
571
572/*
573 * Backlight device
574 */
Corentin Chary9129d142009-12-01 22:39:41 +0100575static int get_lcd_state(struct asus_laptop *asus)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100576{
Corentin Chary9129d142009-12-01 22:39:41 +0100577 return read_status(asus, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100578}
579
Corentin Chary9129d142009-12-01 22:39:41 +0100580static int set_lcd_state(struct asus_laptop *asus, int value)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100581{
582 int lcd = 0;
583 acpi_status status = 0;
584
585 lcd = value ? 1 : 0;
586
Corentin Chary9129d142009-12-01 22:39:41 +0100587 if (lcd == get_lcd_state(asus))
Corentin Chary6b7091e2007-01-26 14:04:45 +0100588 return 0;
589
Len Brown8def05f2007-01-30 01:46:43 -0500590 if (lcd_switch_handle) {
Corentin Chary6b7091e2007-01-26 14:04:45 +0100591 status = acpi_evaluate_object(lcd_switch_handle,
592 NULL, NULL, NULL);
593
594 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200595 pr_warning("Error switching LCD\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100596 }
597
Corentin Chary9129d142009-12-01 22:39:41 +0100598 write_status(asus, NULL, lcd, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100599 return 0;
600}
601
Corentin Chary9129d142009-12-01 22:39:41 +0100602static void lcd_blank(struct asus_laptop *asus, int blank)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100603{
Corentin Chary7c247642009-11-30 22:13:54 +0100604 struct backlight_device *bd = asus->backlight_device;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100605
Len Brown8def05f2007-01-30 01:46:43 -0500606 if (bd) {
Richard Purdie599a52d2007-02-10 23:07:48 +0000607 bd->props.power = blank;
Richard Purdie28ee0862007-02-08 22:25:09 +0000608 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100609 }
610}
611
612static int read_brightness(struct backlight_device *bd)
613{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400614 unsigned long long value;
Corentin Chary9a816852007-03-11 10:25:38 +0100615 acpi_status rv = AE_OK;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100616
Corentin Chary9a816852007-03-11 10:25:38 +0100617 rv = acpi_evaluate_integer(brightness_get_handle, NULL, NULL, &value);
618 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200619 pr_warning("Error reading brightness\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100620
621 return value;
622}
623
624static int set_brightness(struct backlight_device *bd, int value)
625{
Corentin Charyd8c67322009-11-28 10:27:51 +0100626 if (write_acpi_int(brightness_set_handle, NULL, value)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200627 pr_warning("Error changing brightness\n");
Corentin Charye5b50f62009-11-28 10:19:55 +0100628 return -EIO;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100629 }
Corentin Charye5b50f62009-11-28 10:19:55 +0100630 return 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100631}
632
633static int update_bl_status(struct backlight_device *bd)
634{
Corentin Chary9129d142009-12-01 22:39:41 +0100635 struct asus_laptop *asus = bl_get_data(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100636 int rv;
Richard Purdie599a52d2007-02-10 23:07:48 +0000637 int value = bd->props.brightness;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100638
639 rv = set_brightness(bd, value);
Len Brown8def05f2007-01-30 01:46:43 -0500640 if (rv)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100641 return rv;
642
Richard Purdie599a52d2007-02-10 23:07:48 +0000643 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
Corentin Chary9129d142009-12-01 22:39:41 +0100644 return set_lcd_state(asus, value);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100645}
646
Corentin Charybe4ee822009-12-06 16:27:09 +0100647static struct backlight_ops asusbl_ops = {
648 .get_brightness = read_brightness,
649 .update_status = update_bl_status,
650};
651
652static int asus_backlight_init(struct asus_laptop *asus)
653{
654 struct backlight_device *bd;
655 struct device *dev = &asus->platform_device->dev;
656
657 if (brightness_set_handle && lcd_switch_handle) {
658 bd = backlight_device_register(ASUS_LAPTOP_FILE, dev,
659 asus, &asusbl_ops);
660 if (IS_ERR(bd)) {
661 pr_err("Could not register asus backlight device\n");
662 asus->backlight_device = NULL;
663 return PTR_ERR(bd);
664 }
665
666 asus->backlight_device = bd;
667
668 bd->props.max_brightness = 15;
669 bd->props.brightness = read_brightness(NULL);
670 bd->props.power = FB_BLANK_UNBLANK;
671 backlight_update_status(bd);
672 }
673 return 0;
674}
675
676static void asus_backlight_exit(struct asus_laptop *asus)
677{
678 if (asus->backlight_device)
679 backlight_device_unregister(asus->backlight_device);
680}
681
Corentin Chary85091b72007-01-26 14:04:30 +0100682/*
683 * Platform device handlers
684 */
685
686/*
687 * We write our info in page, we begin at offset off and cannot write more
688 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
689 * number of bytes written in page
690 */
691static ssize_t show_infos(struct device *dev,
Len Brown8def05f2007-01-30 01:46:43 -0500692 struct device_attribute *attr, char *page)
Corentin Chary85091b72007-01-26 14:04:30 +0100693{
Corentin Chary9129d142009-12-01 22:39:41 +0100694 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary85091b72007-01-26 14:04:30 +0100695 int len = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400696 unsigned long long temp;
Corentin Charybe966662009-08-29 10:28:29 +0200697 char buf[16]; /* enough for all info */
Corentin Chary9a816852007-03-11 10:25:38 +0100698 acpi_status rv = AE_OK;
699
Corentin Chary85091b72007-01-26 14:04:30 +0100700 /*
701 * We use the easy way, we don't care of off and count, so we don't set eof
702 * to 1
703 */
704
Corentin Chary50a90c42009-11-30 21:55:12 +0100705 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
706 len += sprintf(page + len, "Model reference : %s\n", asus->name);
Corentin Chary85091b72007-01-26 14:04:30 +0100707 /*
708 * The SFUN method probably allows the original driver to get the list
709 * of features supported by a given model. For now, 0x0100 or 0x0800
710 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
711 * The significance of others is yet to be found.
712 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100713 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100714 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000715 len += sprintf(page + len, "SFUN value : %#x\n",
716 (uint) temp);
717 /*
718 * The HWRS method return informations about the hardware.
719 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
720 * The significance of others is yet to be found.
721 * If we don't find the method, we assume the device are present.
722 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100723 rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
Corentin Chary1d4a3802009-08-28 12:56:46 +0000724 if (!ACPI_FAILURE(rv))
725 len += sprintf(page + len, "HRWS value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100726 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100727 /*
728 * Another value for userspace: the ASYM method returns 0x02 for
729 * battery low and 0x04 for battery critical, its readings tend to be
730 * more accurate than those provided by _BST.
731 * Note: since not all the laptops provide this method, errors are
732 * silently ignored.
733 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100734 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100735 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000736 len += sprintf(page + len, "ASYM value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100737 (uint) temp);
Corentin Chary7c247642009-11-30 22:13:54 +0100738 if (asus->dsdt_info) {
739 snprintf(buf, 16, "%d", asus->dsdt_info->length);
Corentin Chary85091b72007-01-26 14:04:30 +0100740 len += sprintf(page + len, "DSDT length : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100741 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
Corentin Chary85091b72007-01-26 14:04:30 +0100742 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100743 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100744 len += sprintf(page + len, "DSDT revision : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100745 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100746 len += sprintf(page + len, "OEM id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100747 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100748 len += sprintf(page + len, "OEM table id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100749 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100750 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100751 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100752 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100753 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100754 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
755 }
756
757 return len;
758}
759
760static int parse_arg(const char *buf, unsigned long count, int *val)
761{
762 if (!count)
763 return 0;
764 if (count > 31)
765 return -EINVAL;
766 if (sscanf(buf, "%i", val) != 1)
767 return -EINVAL;
768 return count;
769}
770
Corentin Chary9129d142009-12-01 22:39:41 +0100771static ssize_t store_status(struct asus_laptop *asus,
772 const char *buf, size_t count,
Corentin Chary935ffee2007-03-11 10:26:12 +0100773 acpi_handle handle, int mask)
Corentin Chary4564de12007-01-26 14:04:40 +0100774{
775 int rv, value;
776 int out = 0;
777
778 rv = parse_arg(buf, count, &value);
779 if (rv > 0)
780 out = value ? 1 : 0;
781
Corentin Chary9129d142009-12-01 22:39:41 +0100782 write_status(asus, handle, out, mask);
Corentin Chary4564de12007-01-26 14:04:40 +0100783
784 return rv;
785}
786
787/*
Corentin Chary722ad972007-01-26 14:04:55 +0100788 * LEDD display
789 */
790static ssize_t show_ledd(struct device *dev,
791 struct device_attribute *attr, char *buf)
792{
Corentin Chary9129d142009-12-01 22:39:41 +0100793 struct asus_laptop *asus = dev_get_drvdata(dev);
794
Corentin Chary50a90c42009-11-30 21:55:12 +0100795 return sprintf(buf, "0x%08x\n", asus->ledd_status);
Corentin Chary722ad972007-01-26 14:04:55 +0100796}
797
798static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
799 const char *buf, size_t count)
800{
Corentin Chary9129d142009-12-01 22:39:41 +0100801 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary722ad972007-01-26 14:04:55 +0100802 int rv, value;
803
804 rv = parse_arg(buf, count, &value);
805 if (rv > 0) {
Corentin Charyd8c67322009-11-28 10:27:51 +0100806 if (write_acpi_int(ledd_set_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200807 pr_warning("LED display write failed\n");
Corentin Chary722ad972007-01-26 14:04:55 +0100808 else
Corentin Chary50a90c42009-11-30 21:55:12 +0100809 asus->ledd_status = (u32) value;
Corentin Chary722ad972007-01-26 14:04:55 +0100810 }
811 return rv;
812}
813
814/*
Corentin Chary4564de12007-01-26 14:04:40 +0100815 * WLAN
816 */
817static ssize_t show_wlan(struct device *dev,
818 struct device_attribute *attr, char *buf)
819{
Corentin Chary9129d142009-12-01 22:39:41 +0100820 struct asus_laptop *asus = dev_get_drvdata(dev);
821
822 return sprintf(buf, "%d\n", read_status(asus, WL_ON));
Corentin Chary4564de12007-01-26 14:04:40 +0100823}
824
825static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
826 const char *buf, size_t count)
827{
Corentin Chary9129d142009-12-01 22:39:41 +0100828 struct asus_laptop *asus = dev_get_drvdata(dev);
829
830 return store_status(asus, buf, count, wl_switch_handle, WL_ON);
Corentin Chary4564de12007-01-26 14:04:40 +0100831}
832
833/*
834 * Bluetooth
835 */
836static ssize_t show_bluetooth(struct device *dev,
837 struct device_attribute *attr, char *buf)
838{
Corentin Chary9129d142009-12-01 22:39:41 +0100839 struct asus_laptop *asus = dev_get_drvdata(dev);
840
841 return sprintf(buf, "%d\n", read_status(asus, BT_ON));
Corentin Chary4564de12007-01-26 14:04:40 +0100842}
843
Len Brown8def05f2007-01-30 01:46:43 -0500844static ssize_t store_bluetooth(struct device *dev,
845 struct device_attribute *attr, const char *buf,
846 size_t count)
Corentin Chary4564de12007-01-26 14:04:40 +0100847{
Corentin Chary9129d142009-12-01 22:39:41 +0100848 struct asus_laptop *asus = dev_get_drvdata(dev);
849
850 return store_status(asus, buf, count, bt_switch_handle, BT_ON);
Corentin Chary4564de12007-01-26 14:04:40 +0100851}
852
Corentin Chary78127b42007-01-26 14:04:49 +0100853/*
854 * Display
855 */
Corentin Chary9129d142009-12-01 22:39:41 +0100856static void set_display(struct asus_laptop *asus, int value)
Corentin Chary78127b42007-01-26 14:04:49 +0100857{
858 /* no sanity check needed for now */
Corentin Charyd8c67322009-11-28 10:27:51 +0100859 if (write_acpi_int(display_set_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200860 pr_warning("Error setting display\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100861 return;
862}
863
Corentin Chary9129d142009-12-01 22:39:41 +0100864static int read_display(struct asus_laptop *asus)
Corentin Chary78127b42007-01-26 14:04:49 +0100865{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400866 unsigned long long value = 0;
Corentin Chary9a816852007-03-11 10:25:38 +0100867 acpi_status rv = AE_OK;
Corentin Chary78127b42007-01-26 14:04:49 +0100868
Corentin Charybe966662009-08-29 10:28:29 +0200869 /*
870 * In most of the case, we know how to set the display, but sometime
871 * we can't read it
872 */
Len Brown8def05f2007-01-30 01:46:43 -0500873 if (display_get_handle) {
Corentin Chary9a816852007-03-11 10:25:38 +0100874 rv = acpi_evaluate_integer(display_get_handle, NULL,
875 NULL, &value);
876 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200877 pr_warning("Error reading display status\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100878 }
879
880 value &= 0x0F; /* needed for some models, shouldn't hurt others */
881
882 return value;
883}
Len Brown8def05f2007-01-30 01:46:43 -0500884
Corentin Chary78127b42007-01-26 14:04:49 +0100885/*
886 * Now, *this* one could be more user-friendly, but so far, no-one has
887 * complained. The significance of bits is the same as in store_disp()
888 */
889static ssize_t show_disp(struct device *dev,
890 struct device_attribute *attr, char *buf)
891{
Corentin Chary9129d142009-12-01 22:39:41 +0100892 struct asus_laptop *asus = dev_get_drvdata(dev);
893
894 return sprintf(buf, "%d\n", read_display(asus));
Corentin Chary78127b42007-01-26 14:04:49 +0100895}
896
897/*
898 * Experimental support for display switching. As of now: 1 should activate
899 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
900 * Any combination (bitwise) of these will suffice. I never actually tested 4
901 * displays hooked up simultaneously, so be warned. See the acpi4asus README
902 * for more info.
903 */
904static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
905 const char *buf, size_t count)
906{
Corentin Chary9129d142009-12-01 22:39:41 +0100907 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary78127b42007-01-26 14:04:49 +0100908 int rv, value;
909
910 rv = parse_arg(buf, count, &value);
911 if (rv > 0)
Corentin Chary9129d142009-12-01 22:39:41 +0100912 set_display(asus, value);
Corentin Chary78127b42007-01-26 14:04:49 +0100913 return rv;
914}
915
Corentin Chary8b857352007-01-26 14:04:58 +0100916/*
917 * Light Sens
918 */
Corentin Chary9129d142009-12-01 22:39:41 +0100919static void set_light_sens_switch(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100920{
Corentin Charyd8c67322009-11-28 10:27:51 +0100921 if (write_acpi_int(ls_switch_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200922 pr_warning("Error setting light sensor switch\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100923 asus->light_switch = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100924}
925
926static ssize_t show_lssw(struct device *dev,
927 struct device_attribute *attr, char *buf)
928{
Corentin Chary9129d142009-12-01 22:39:41 +0100929 struct asus_laptop *asus = dev_get_drvdata(dev);
930
Corentin Chary50a90c42009-11-30 21:55:12 +0100931 return sprintf(buf, "%d\n", asus->light_switch);
Corentin Chary8b857352007-01-26 14:04:58 +0100932}
933
934static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
935 const char *buf, size_t count)
936{
Corentin Chary9129d142009-12-01 22:39:41 +0100937 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100938 int rv, value;
939
940 rv = parse_arg(buf, count, &value);
941 if (rv > 0)
Corentin Chary9129d142009-12-01 22:39:41 +0100942 set_light_sens_switch(asus, value ? 1 : 0);
Corentin Chary8b857352007-01-26 14:04:58 +0100943
944 return rv;
945}
946
Corentin Chary9129d142009-12-01 22:39:41 +0100947static void set_light_sens_level(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100948{
Corentin Charyd8c67322009-11-28 10:27:51 +0100949 if (write_acpi_int(ls_level_handle, NULL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200950 pr_warning("Error setting light sensor level\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100951 asus->light_level = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100952}
953
954static ssize_t show_lslvl(struct device *dev,
955 struct device_attribute *attr, char *buf)
956{
Corentin Chary9129d142009-12-01 22:39:41 +0100957 struct asus_laptop *asus = dev_get_drvdata(dev);
958
Corentin Chary50a90c42009-11-30 21:55:12 +0100959 return sprintf(buf, "%d\n", asus->light_level);
Corentin Chary8b857352007-01-26 14:04:58 +0100960}
961
962static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
963 const char *buf, size_t count)
964{
Corentin Chary9129d142009-12-01 22:39:41 +0100965 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100966 int rv, value;
967
968 rv = parse_arg(buf, count, &value);
969 if (rv > 0) {
970 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
971 /* 0 <= value <= 15 */
Corentin Chary9129d142009-12-01 22:39:41 +0100972 set_light_sens_level(asus, value);
Corentin Chary8b857352007-01-26 14:04:58 +0100973 }
974
975 return rv;
976}
977
Corentin Charye539c2f2007-05-06 14:47:06 +0200978/*
979 * GPS
980 */
981static ssize_t show_gps(struct device *dev,
982 struct device_attribute *attr, char *buf)
983{
Corentin Chary9129d142009-12-01 22:39:41 +0100984 struct asus_laptop *asus = dev_get_drvdata(dev);
985
986 return sprintf(buf, "%d\n", read_status(asus, GPS_ON));
Corentin Charye539c2f2007-05-06 14:47:06 +0200987}
988
989static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
990 const char *buf, size_t count)
991{
Corentin Chary9129d142009-12-01 22:39:41 +0100992 struct asus_laptop *asus = dev_get_drvdata(dev);
993
994 return store_status(asus, buf, count, NULL, GPS_ON);
Corentin Charye539c2f2007-05-06 14:47:06 +0200995}
996
Corentin Chary034ce902009-01-20 16:17:43 +0100997/*
Corentin Charybe4ee822009-12-06 16:27:09 +0100998 * Input device (i.e. hotkeys)
Corentin Chary034ce902009-01-20 16:17:43 +0100999 */
Corentin Chary9129d142009-12-01 22:39:41 +01001000static struct key_entry *asus_get_entry_by_scancode(struct asus_laptop *asus,
1001 int code)
Corentin Chary034ce902009-01-20 16:17:43 +01001002{
1003 struct key_entry *key;
1004
Corentin Chary9129d142009-12-01 22:39:41 +01001005 for (key = asus->keymap; key->type != KE_END; key++)
Corentin Chary034ce902009-01-20 16:17:43 +01001006 if (code == key->code)
1007 return key;
1008
1009 return NULL;
1010}
1011
Corentin Chary9129d142009-12-01 22:39:41 +01001012static struct key_entry *asus_get_entry_by_keycode(struct asus_laptop *asus,
1013 int code)
Corentin Chary034ce902009-01-20 16:17:43 +01001014{
1015 struct key_entry *key;
1016
Corentin Chary9129d142009-12-01 22:39:41 +01001017 for (key = asus->keymap; key->type != KE_END; key++)
Corentin Chary034ce902009-01-20 16:17:43 +01001018 if (code == key->keycode && key->type == KE_KEY)
1019 return key;
1020
1021 return NULL;
1022}
1023
1024static int asus_getkeycode(struct input_dev *dev, int scancode, int *keycode)
1025{
Corentin Chary9129d142009-12-01 22:39:41 +01001026 struct asus_laptop *asus = input_get_drvdata(dev);
1027 struct key_entry *key = asus_get_entry_by_scancode(asus, scancode);
Corentin Chary034ce902009-01-20 16:17:43 +01001028
1029 if (key && key->type == KE_KEY) {
1030 *keycode = key->keycode;
1031 return 0;
1032 }
1033
1034 return -EINVAL;
1035}
1036
1037static int asus_setkeycode(struct input_dev *dev, int scancode, int keycode)
1038{
Corentin Chary9129d142009-12-01 22:39:41 +01001039 struct asus_laptop *asus = input_get_drvdata(dev);
Corentin Chary034ce902009-01-20 16:17:43 +01001040 struct key_entry *key;
1041 int old_keycode;
1042
1043 if (keycode < 0 || keycode > KEY_MAX)
1044 return -EINVAL;
1045
Corentin Chary9129d142009-12-01 22:39:41 +01001046 key = asus_get_entry_by_scancode(asus, scancode);
Corentin Chary034ce902009-01-20 16:17:43 +01001047 if (key && key->type == KE_KEY) {
1048 old_keycode = key->keycode;
1049 key->keycode = keycode;
1050 set_bit(keycode, dev->keybit);
Corentin Chary9129d142009-12-01 22:39:41 +01001051 if (!asus_get_entry_by_keycode(asus, old_keycode))
Corentin Chary034ce902009-01-20 16:17:43 +01001052 clear_bit(old_keycode, dev->keybit);
1053 return 0;
1054 }
1055
1056 return -EINVAL;
1057}
1058
Corentin Charybe4ee822009-12-06 16:27:09 +01001059static void asus_input_notify(struct asus_laptop *asus, int event)
1060{
1061 struct key_entry *key;
1062
1063 key = asus_get_entry_by_scancode(asus, event);
1064 if (!key)
1065 return ;
1066
1067 switch (key->type) {
1068 case KE_KEY:
1069 input_report_key(asus->inputdev, key->keycode, 1);
1070 input_sync(asus->inputdev);
1071 input_report_key(asus->inputdev, key->keycode, 0);
1072 input_sync(asus->inputdev);
1073 break;
1074 }
1075}
1076
1077static int asus_input_init(struct asus_laptop *asus)
1078{
1079 const struct key_entry *key;
1080 int result;
1081
1082 asus->inputdev = input_allocate_device();
1083 if (!asus->inputdev) {
1084 pr_info("Unable to allocate input device\n");
1085 return 0;
1086 }
1087 asus->inputdev->name = "Asus Laptop extra buttons";
1088 asus->inputdev->dev.parent = &asus->platform_device->dev;
1089 asus->inputdev->phys = ASUS_LAPTOP_FILE "/input0";
1090 asus->inputdev->id.bustype = BUS_HOST;
1091 asus->inputdev->getkeycode = asus_getkeycode;
1092 asus->inputdev->setkeycode = asus_setkeycode;
1093 input_set_drvdata(asus->inputdev, asus);
1094
1095 asus->keymap = kmemdup(asus_keymap, sizeof(asus_keymap),
1096 GFP_KERNEL);
1097 for (key = asus->keymap; key->type != KE_END; key++) {
1098 switch (key->type) {
1099 case KE_KEY:
1100 set_bit(EV_KEY, asus->inputdev->evbit);
1101 set_bit(key->keycode, asus->inputdev->keybit);
1102 break;
1103 }
1104 }
1105 result = input_register_device(asus->inputdev);
1106 if (result) {
1107 pr_info("Unable to register input device\n");
1108 input_free_device(asus->inputdev);
1109 }
1110 return result;
1111}
1112
1113static void asus_input_exit(struct asus_laptop *asus)
1114{
1115 if (asus->inputdev)
1116 input_unregister_device(asus->inputdev);
1117}
1118
1119/*
1120 * ACPI driver
1121 */
Corentin Chary600ad522009-11-30 21:42:42 +01001122static void asus_acpi_notify(struct acpi_device *device, u32 event)
Corentin Chary85091b72007-01-26 14:04:30 +01001123{
Corentin Chary9129d142009-12-01 22:39:41 +01001124 struct asus_laptop *asus = acpi_driver_data(device);
Corentin Chary6050c8d2009-02-15 19:30:19 +01001125 u16 count;
Corentin Chary034ce902009-01-20 16:17:43 +01001126
Corentin Chary6b7091e2007-01-26 14:04:45 +01001127 /*
1128 * We need to tell the backlight device when the backlight power is
1129 * switched
1130 */
1131 if (event == ATKD_LCD_ON) {
Corentin Chary9129d142009-12-01 22:39:41 +01001132 write_status(asus, NULL, 1, LCD_ON);
1133 lcd_blank(asus, FB_BLANK_UNBLANK);
Len Brown8def05f2007-01-30 01:46:43 -05001134 } else if (event == ATKD_LCD_OFF) {
Corentin Chary9129d142009-12-01 22:39:41 +01001135 write_status(asus, NULL, 0, LCD_ON);
1136 lcd_blank(asus, FB_BLANK_POWERDOWN);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001137 }
1138
Corentin Chary619d8b12009-11-28 10:35:37 +01001139 /* TODO Find a better way to handle events count. */
Corentin Chary50a90c42009-11-30 21:55:12 +01001140 count = asus->event_count[event % 128]++;
1141 acpi_bus_generate_proc_event(asus->device, event, count);
1142 acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
1143 dev_name(&asus->device->dev), event,
Corentin Chary6050c8d2009-02-15 19:30:19 +01001144 count);
Corentin Chary85091b72007-01-26 14:04:30 +01001145
Corentin Charybe4ee822009-12-06 16:27:09 +01001146 asus_input_notify(asus, event);
Corentin Chary85091b72007-01-26 14:04:30 +01001147}
1148
1149#define ASUS_CREATE_DEVICE_ATTR(_name) \
1150 struct device_attribute dev_attr_##_name = { \
1151 .attr = { \
1152 .name = __stringify(_name), \
Tejun Heo7b595752007-06-14 03:45:17 +09001153 .mode = 0 }, \
Corentin Chary85091b72007-01-26 14:04:30 +01001154 .show = NULL, \
1155 .store = NULL, \
1156 }
1157
1158#define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
1159 do { \
1160 dev_attr_##_name.attr.mode = _mode; \
1161 dev_attr_##_name.show = _show; \
1162 dev_attr_##_name.store = _store; \
1163 } while(0)
1164
1165static ASUS_CREATE_DEVICE_ATTR(infos);
Corentin Chary4564de12007-01-26 14:04:40 +01001166static ASUS_CREATE_DEVICE_ATTR(wlan);
1167static ASUS_CREATE_DEVICE_ATTR(bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +01001168static ASUS_CREATE_DEVICE_ATTR(display);
Corentin Chary722ad972007-01-26 14:04:55 +01001169static ASUS_CREATE_DEVICE_ATTR(ledd);
Corentin Chary8b857352007-01-26 14:04:58 +01001170static ASUS_CREATE_DEVICE_ATTR(ls_switch);
1171static ASUS_CREATE_DEVICE_ATTR(ls_level);
Corentin Charye539c2f2007-05-06 14:47:06 +02001172static ASUS_CREATE_DEVICE_ATTR(gps);
Corentin Chary85091b72007-01-26 14:04:30 +01001173
1174static struct attribute *asuspf_attributes[] = {
Len Brown8def05f2007-01-30 01:46:43 -05001175 &dev_attr_infos.attr,
1176 &dev_attr_wlan.attr,
1177 &dev_attr_bluetooth.attr,
1178 &dev_attr_display.attr,
1179 &dev_attr_ledd.attr,
1180 &dev_attr_ls_switch.attr,
1181 &dev_attr_ls_level.attr,
Corentin Charye539c2f2007-05-06 14:47:06 +02001182 &dev_attr_gps.attr,
Len Brown8def05f2007-01-30 01:46:43 -05001183 NULL
Corentin Chary85091b72007-01-26 14:04:30 +01001184};
1185
Corentin Chary600ad522009-11-30 21:42:42 +01001186static struct attribute_group platform_attribute_group = {
Len Brown8def05f2007-01-30 01:46:43 -05001187 .attrs = asuspf_attributes
Corentin Chary85091b72007-01-26 14:04:30 +01001188};
1189
Corentin Chary9129d142009-12-01 22:39:41 +01001190static int asus_platform_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001191{
1192 int result;
1193
Corentin Chary50a90c42009-11-30 21:55:12 +01001194 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1195 if (!asus->platform_device)
Corentin Chary600ad522009-11-30 21:42:42 +01001196 return -ENOMEM;
Corentin Chary9129d142009-12-01 22:39:41 +01001197 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001198
Corentin Chary50a90c42009-11-30 21:55:12 +01001199 result = platform_device_add(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001200 if (result)
1201 goto fail_platform_device;
1202
Corentin Chary50a90c42009-11-30 21:55:12 +01001203 result = sysfs_create_group(&asus->platform_device->dev.kobj,
Corentin Chary600ad522009-11-30 21:42:42 +01001204 &platform_attribute_group);
1205 if (result)
1206 goto fail_sysfs;
1207 return 0;
1208
1209fail_sysfs:
Corentin Chary50a90c42009-11-30 21:55:12 +01001210 platform_device_del(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001211fail_platform_device:
Corentin Chary50a90c42009-11-30 21:55:12 +01001212 platform_device_put(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001213 return result;
1214}
1215
Corentin Chary9129d142009-12-01 22:39:41 +01001216static void asus_platform_exit(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001217{
Corentin Chary50a90c42009-11-30 21:55:12 +01001218 sysfs_remove_group(&asus->platform_device->dev.kobj,
Corentin Chary600ad522009-11-30 21:42:42 +01001219 &platform_attribute_group);
Corentin Chary50a90c42009-11-30 21:55:12 +01001220 platform_device_unregister(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001221}
1222
1223static struct platform_driver platform_driver = {
Len Brown8def05f2007-01-30 01:46:43 -05001224 .driver = {
Corentin Chary9129d142009-12-01 22:39:41 +01001225 .name = ASUS_LAPTOP_FILE,
1226 .owner = THIS_MODULE,
1227 }
Corentin Chary85091b72007-01-26 14:04:30 +01001228};
1229
Corentin Chary9129d142009-12-01 22:39:41 +01001230static void asus_laptop_add_fs(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001231{
1232 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
Corentin Chary4564de12007-01-26 14:04:40 +01001233
1234 if (wl_switch_handle)
1235 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
1236
1237 if (bt_switch_handle)
1238 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
1239 show_bluetooth, store_bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +01001240
1241 if (display_set_handle && display_get_handle)
1242 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
Len Brown8def05f2007-01-30 01:46:43 -05001243 else if (display_set_handle)
Corentin Chary78127b42007-01-26 14:04:49 +01001244 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
1245
Corentin Chary722ad972007-01-26 14:04:55 +01001246 if (ledd_set_handle)
1247 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
1248
Corentin Chary8b857352007-01-26 14:04:58 +01001249 if (ls_switch_handle && ls_level_handle) {
1250 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
1251 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
1252 }
Corentin Charye539c2f2007-05-06 14:47:06 +02001253
Corentin Charyf3985322007-05-06 14:48:22 +02001254 if (gps_status_handle && gps_on_handle && gps_off_handle)
Corentin Charye539c2f2007-05-06 14:47:06 +02001255 ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
Corentin Chary85091b72007-01-26 14:04:30 +01001256}
1257
Len Brown8def05f2007-01-30 01:46:43 -05001258static int asus_handle_init(char *name, acpi_handle * handle,
Corentin Chary85091b72007-01-26 14:04:30 +01001259 char **paths, int num_paths)
1260{
1261 int i;
1262 acpi_status status;
1263
1264 for (i = 0; i < num_paths; i++) {
1265 status = acpi_get_handle(NULL, paths[i], handle);
1266 if (ACPI_SUCCESS(status))
1267 return 0;
1268 }
1269
1270 *handle = NULL;
1271 return -ENODEV;
1272}
1273
1274#define ASUS_HANDLE_INIT(object) \
1275 asus_handle_init(#object, &object##_handle, object##_paths, \
1276 ARRAY_SIZE(object##_paths))
1277
Corentin Chary85091b72007-01-26 14:04:30 +01001278/*
Corentin Chary50a90c42009-11-30 21:55:12 +01001279 * This function is used to initialize the context with right values. In this
1280 * method, we can make all the detection we want, and modify the asus_laptop
1281 * struct
Corentin Chary85091b72007-01-26 14:04:30 +01001282 */
Corentin Chary7c247642009-11-30 22:13:54 +01001283static int asus_laptop_get_info(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001284{
1285 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Corentin Chary85091b72007-01-26 14:04:30 +01001286 union acpi_object *model = NULL;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001287 unsigned long long bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +01001288 char *string = NULL;
1289 acpi_status status;
1290
1291 /*
1292 * Get DSDT headers early enough to allow for differentiating between
1293 * models, but late enough to allow acpi_bus_register_driver() to fail
1294 * before doing anything ACPI-specific. Should we encounter a machine,
1295 * which needs special handling (i.e. its hotkey device has a different
Corentin Chary7c247642009-11-30 22:13:54 +01001296 * HID), this bit will be moved.
Corentin Chary85091b72007-01-26 14:04:30 +01001297 */
Corentin Chary7c247642009-11-30 22:13:54 +01001298 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
Corentin Chary85091b72007-01-26 14:04:30 +01001299 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001300 pr_warning("Couldn't get the DSDT table header\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001301
1302 /* We have to write 0 on init this far for all ASUS models */
Corentin Chary50a90c42009-11-30 21:55:12 +01001303 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001304 pr_err("Hotkey initialization failed\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001305 return -ENODEV;
1306 }
1307
1308 /* This needs to be called for some laptops to init properly */
Corentin Chary9a816852007-03-11 10:25:38 +01001309 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001310 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
Corentin Chary9a816852007-03-11 10:25:38 +01001311 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001312 pr_warning("Error calling BSTS\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001313 else if (bsts_result)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001314 pr_notice("BSTS called, 0x%02x returned\n",
Corentin Chary9a816852007-03-11 10:25:38 +01001315 (uint) bsts_result);
Corentin Chary85091b72007-01-26 14:04:30 +01001316
Corentin Chary185e5af2007-03-11 10:27:33 +01001317 /* This too ... */
Corentin Chary50a90c42009-11-30 21:55:12 +01001318 write_acpi_int(asus->handle, "CWAP", wapf);
Corentin Chary185e5af2007-03-11 10:27:33 +01001319
Corentin Chary85091b72007-01-26 14:04:30 +01001320 /*
1321 * Try to match the object returned by INIT to the specific model.
1322 * Handle every possible object (or the lack of thereof) the DSDT
1323 * writers might throw at us. When in trouble, we pass NULL to
1324 * asus_model_match() and try something completely different.
1325 */
1326 if (buffer.pointer) {
1327 model = buffer.pointer;
1328 switch (model->type) {
1329 case ACPI_TYPE_STRING:
1330 string = model->string.pointer;
1331 break;
1332 case ACPI_TYPE_BUFFER:
1333 string = model->buffer.pointer;
1334 break;
1335 default:
1336 string = "";
1337 break;
1338 }
1339 }
Corentin Chary50a90c42009-11-30 21:55:12 +01001340 asus->name = kstrdup(string, GFP_KERNEL);
1341 if (!asus->name)
Corentin Chary85091b72007-01-26 14:04:30 +01001342 return -ENOMEM;
1343
Len Brown8def05f2007-01-30 01:46:43 -05001344 if (*string)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001345 pr_notice(" %s model detected\n", string);
Corentin Chary85091b72007-01-26 14:04:30 +01001346
Corentin Charybe18cda2007-01-26 14:04:35 +01001347 ASUS_HANDLE_INIT(mled_set);
1348 ASUS_HANDLE_INIT(tled_set);
1349 ASUS_HANDLE_INIT(rled_set);
1350 ASUS_HANDLE_INIT(pled_set);
Corentin Charyfdd8d082007-03-11 10:26:48 +01001351 ASUS_HANDLE_INIT(gled_set);
Corentin Charybe18cda2007-01-26 14:04:35 +01001352
Corentin Chary722ad972007-01-26 14:04:55 +01001353 ASUS_HANDLE_INIT(ledd_set);
1354
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001355 ASUS_HANDLE_INIT(kled_set);
1356 ASUS_HANDLE_INIT(kled_get);
1357
Corentin Chary4564de12007-01-26 14:04:40 +01001358 /*
1359 * The HWRS method return informations about the hardware.
1360 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1361 * The significance of others is yet to be found.
1362 * If we don't find the method, we assume the device are present.
1363 */
Corentin Chary9a816852007-03-11 10:25:38 +01001364 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001365 acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
Corentin Chary9a816852007-03-11 10:25:38 +01001366 if (ACPI_FAILURE(status))
Corentin Chary4564de12007-01-26 14:04:40 +01001367 hwrs_result = WL_HWRS | BT_HWRS;
1368
Len Brown8def05f2007-01-30 01:46:43 -05001369 if (hwrs_result & WL_HWRS)
Corentin Chary4564de12007-01-26 14:04:40 +01001370 ASUS_HANDLE_INIT(wl_switch);
Len Brown8def05f2007-01-30 01:46:43 -05001371 if (hwrs_result & BT_HWRS)
Corentin Chary4564de12007-01-26 14:04:40 +01001372 ASUS_HANDLE_INIT(bt_switch);
1373
1374 ASUS_HANDLE_INIT(wireless_status);
1375
Corentin Chary6b7091e2007-01-26 14:04:45 +01001376 ASUS_HANDLE_INIT(brightness_set);
1377 ASUS_HANDLE_INIT(brightness_get);
1378
1379 ASUS_HANDLE_INIT(lcd_switch);
1380
Corentin Chary78127b42007-01-26 14:04:49 +01001381 ASUS_HANDLE_INIT(display_set);
1382 ASUS_HANDLE_INIT(display_get);
1383
Corentin Charybe966662009-08-29 10:28:29 +02001384 /*
1385 * There is a lot of models with "ALSL", but a few get
1386 * a real light sens, so we need to check it.
1387 */
Corentin Chary832d9952007-05-06 14:47:29 +02001388 if (!ASUS_HANDLE_INIT(ls_switch))
Corentin Chary8b857352007-01-26 14:04:58 +01001389 ASUS_HANDLE_INIT(ls_level);
1390
Corentin Charye539c2f2007-05-06 14:47:06 +02001391 ASUS_HANDLE_INIT(gps_on);
1392 ASUS_HANDLE_INIT(gps_off);
1393 ASUS_HANDLE_INIT(gps_status);
1394
Corentin Chary85091b72007-01-26 14:04:30 +01001395 kfree(model);
1396
1397 return AE_OK;
1398}
1399
Corentin Chary600ad522009-11-30 21:42:42 +01001400static bool asus_device_present;
1401
Corentin Chary9129d142009-12-01 22:39:41 +01001402static int __devinit asus_acpi_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001403{
1404 int result = 0;
1405
Corentin Chary50a90c42009-11-30 21:55:12 +01001406 result = acpi_bus_get_status(asus->device);
Corentin Chary600ad522009-11-30 21:42:42 +01001407 if (result)
1408 return result;
Corentin Chary50a90c42009-11-30 21:55:12 +01001409 if (!asus->device->status.present) {
Corentin Chary600ad522009-11-30 21:42:42 +01001410 pr_err("Hotkey device not present, aborting\n");
1411 return -ENODEV;
1412 }
1413
Corentin Chary7c247642009-11-30 22:13:54 +01001414 result = asus_laptop_get_info(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001415 if (result)
1416 return result;
1417
Corentin Chary9129d142009-12-01 22:39:41 +01001418 asus_laptop_add_fs(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001419
1420 /* WLED and BLED are on by default */
Corentin Charybe4ee822009-12-06 16:27:09 +01001421 if (bluetooth_status >= 0)
1422 write_status(asus, bt_switch_handle, !!bluetooth_status, BT_ON);
1423 if (wireless_status >= 0)
1424 write_status(asus, wl_switch_handle, !!wireless_status, WL_ON);
Corentin Chary600ad522009-11-30 21:42:42 +01001425
1426 /* If the h/w switch is off, we need to check the real status */
Corentin Chary9129d142009-12-01 22:39:41 +01001427 write_status(asus, NULL, read_status(asus, BT_ON), BT_ON);
1428 write_status(asus, NULL, read_status(asus, WL_ON), WL_ON);
Corentin Chary600ad522009-11-30 21:42:42 +01001429
1430 /* LCD Backlight is on by default */
Corentin Chary9129d142009-12-01 22:39:41 +01001431 write_status(asus, NULL, 1, LCD_ON);
Corentin Chary600ad522009-11-30 21:42:42 +01001432
1433 /* Keyboard Backlight is on by default */
1434 if (kled_set_handle)
Corentin Chary9129d142009-12-01 22:39:41 +01001435 set_kled_lvl(asus, 1);
Corentin Chary600ad522009-11-30 21:42:42 +01001436
1437 /* LED display is off by default */
Corentin Chary50a90c42009-11-30 21:55:12 +01001438 asus->ledd_status = 0xFFF;
Corentin Chary600ad522009-11-30 21:42:42 +01001439
1440 /* Set initial values of light sensor and level */
Corentin Charybe4ee822009-12-06 16:27:09 +01001441 asus->light_switch = 0; /* Default to light sensor disabled */
1442 asus->light_level = 5; /* level 5 for sensor sensitivity */
Corentin Chary600ad522009-11-30 21:42:42 +01001443
1444 if (ls_switch_handle)
Corentin Chary9129d142009-12-01 22:39:41 +01001445 set_light_sens_switch(asus, asus->light_switch);
Corentin Chary600ad522009-11-30 21:42:42 +01001446
1447 if (ls_level_handle)
Corentin Chary9129d142009-12-01 22:39:41 +01001448 set_light_sens_level(asus, asus->light_level);
Corentin Chary600ad522009-11-30 21:42:42 +01001449
1450 /* GPS is on by default */
Corentin Chary9129d142009-12-01 22:39:41 +01001451 write_status(asus, NULL, 1, GPS_ON);
Corentin Chary600ad522009-11-30 21:42:42 +01001452 return result;
1453}
1454
1455static int __devinit asus_acpi_add(struct acpi_device *device)
1456{
Corentin Chary9129d142009-12-01 22:39:41 +01001457 struct asus_laptop *asus;
Corentin Chary600ad522009-11-30 21:42:42 +01001458 int result;
1459
1460 pr_notice("Asus Laptop Support version %s\n",
1461 ASUS_LAPTOP_VERSION);
Corentin Chary50a90c42009-11-30 21:55:12 +01001462 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1463 if (!asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001464 return -ENOMEM;
Corentin Chary50a90c42009-11-30 21:55:12 +01001465 asus->handle = device->handle;
1466 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1467 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1468 device->driver_data = asus;
1469 asus->device = device;
Corentin Chary600ad522009-11-30 21:42:42 +01001470
Corentin Chary9129d142009-12-01 22:39:41 +01001471 result = asus_acpi_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001472 if (result)
1473 goto fail_platform;
1474
1475 /*
1476 * Register the platform device first. It is used as a parent for the
1477 * sub-devices below.
1478 */
Corentin Chary9129d142009-12-01 22:39:41 +01001479 result = asus_platform_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001480 if (result)
1481 goto fail_platform;
1482
1483 if (!acpi_video_backlight_support()) {
Corentin Chary9129d142009-12-01 22:39:41 +01001484 result = asus_backlight_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001485 if (result)
1486 goto fail_backlight;
1487 } else
1488 pr_info("Backlight controlled by ACPI video driver\n");
1489
Corentin Chary9129d142009-12-01 22:39:41 +01001490 result = asus_input_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001491 if (result)
1492 goto fail_input;
1493
Corentin Chary9129d142009-12-01 22:39:41 +01001494 result = asus_led_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001495 if (result)
1496 goto fail_led;
1497
1498 asus_device_present = true;
1499 return 0;
1500
1501fail_led:
Corentin Chary9129d142009-12-01 22:39:41 +01001502 asus_input_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001503fail_input:
Corentin Chary9129d142009-12-01 22:39:41 +01001504 asus_backlight_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001505fail_backlight:
Corentin Chary9129d142009-12-01 22:39:41 +01001506 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001507fail_platform:
Corentin Chary50a90c42009-11-30 21:55:12 +01001508 kfree(asus->name);
1509 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001510
1511 return result;
1512}
1513
1514static int asus_acpi_remove(struct acpi_device *device, int type)
1515{
Corentin Chary9129d142009-12-01 22:39:41 +01001516 struct asus_laptop *asus = acpi_driver_data(device);
1517
1518 asus_backlight_exit(asus);
1519 asus_led_exit(asus);
1520 asus_input_exit(asus);
1521 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001522
Corentin Chary50a90c42009-11-30 21:55:12 +01001523 kfree(asus->name);
1524 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001525 return 0;
1526}
1527
1528static const struct acpi_device_id asus_device_ids[] = {
1529 {"ATK0100", 0},
1530 {"ATK0101", 0},
1531 {"", 0},
1532};
1533MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1534
1535static struct acpi_driver asus_acpi_driver = {
Corentin Chary50a90c42009-11-30 21:55:12 +01001536 .name = ASUS_LAPTOP_NAME,
1537 .class = ASUS_LAPTOP_CLASS,
Corentin Chary600ad522009-11-30 21:42:42 +01001538 .owner = THIS_MODULE,
1539 .ids = asus_device_ids,
1540 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1541 .ops = {
1542 .add = asus_acpi_add,
1543 .remove = asus_acpi_remove,
1544 .notify = asus_acpi_notify,
1545 },
1546};
1547
Corentin Chary85091b72007-01-26 14:04:30 +01001548static int __init asus_laptop_init(void)
1549{
1550 int result;
1551
Corentin Chary600ad522009-11-30 21:42:42 +01001552 result = platform_driver_register(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001553 if (result < 0)
1554 return result;
1555
Corentin Chary600ad522009-11-30 21:42:42 +01001556 result = acpi_bus_register_driver(&asus_acpi_driver);
1557 if (result < 0)
1558 goto fail_acpi_driver;
1559 if (!asus_device_present) {
1560 result = -ENODEV;
1561 goto fail_no_device;
Corentin Chary85091b72007-01-26 14:04:30 +01001562 }
Len Brown8def05f2007-01-30 01:46:43 -05001563 return 0;
Corentin Chary85091b72007-01-26 14:04:30 +01001564
Corentin Chary600ad522009-11-30 21:42:42 +01001565fail_no_device:
1566 acpi_bus_unregister_driver(&asus_acpi_driver);
1567fail_acpi_driver:
1568 platform_driver_unregister(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001569 return result;
1570}
1571
Corentin Chary600ad522009-11-30 21:42:42 +01001572static void __exit asus_laptop_exit(void)
1573{
1574 acpi_bus_unregister_driver(&asus_acpi_driver);
1575 platform_driver_unregister(&platform_driver);
1576}
1577
Corentin Chary85091b72007-01-26 14:04:30 +01001578module_init(asus_laptop_init);
1579module_exit(asus_laptop_exit);