blob: 4f3c4674498b40c0bf7c90996b248b35931d6856 [file] [log] [blame]
Corentin Chary85091b72007-01-26 14:04:30 +01001/*
2 * asus-laptop.c - Asus Laptop Support
3 *
4 *
5 * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
Corentin Chary8ec555c2007-03-11 10:28:03 +01006 * Copyright (C) 2006-2007 Corentin Chary
Corentin Chary85091b72007-01-26 14:04:30 +01007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 *
23 * The development page for this driver is located at
24 * http://sourceforge.net/projects/acpi4asus/
25 *
26 * Credits:
27 * Pontus Fuchs - Helper functions, cleanup
28 * Johann Wiesner - Small compile fixes
29 * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
30 * Eric Burghard - LED display support for W1N
31 * Josh Green - Light Sens support
32 * Thomas Tuttle - His first patch for led support was very helpfull
Corentin Charye539c2f2007-05-06 14:47:06 +020033 * Sam Lin - GPS support
Corentin Chary85091b72007-01-26 14:04:30 +010034 */
35
Corentin Chary2fcc23d2009-06-19 14:52:03 +020036#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37
Corentin Chary85091b72007-01-26 14:04:30 +010038#include <linux/kernel.h>
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/types.h>
42#include <linux/err.h>
43#include <linux/proc_fs.h>
Corentin Chary6b7091e2007-01-26 14:04:45 +010044#include <linux/backlight.h>
45#include <linux/fb.h>
Corentin Charybe18cda2007-01-26 14:04:35 +010046#include <linux/leds.h>
Corentin Chary85091b72007-01-26 14:04:30 +010047#include <linux/platform_device.h>
48#include <acpi/acpi_drivers.h>
49#include <acpi/acpi_bus.h>
50#include <asm/uaccess.h>
Corentin Chary034ce902009-01-20 16:17:43 +010051#include <linux/input.h>
Corentin Chary66a71dd2010-01-25 22:50:11 +010052#include <linux/input/sparse-keymap.h>
Corentin Chary85091b72007-01-26 14:04:30 +010053
Corentin Chary91687cc2009-11-28 10:32:34 +010054#define ASUS_LAPTOP_VERSION "0.42"
Corentin Chary85091b72007-01-26 14:04:30 +010055
Corentin Chary50a90c42009-11-30 21:55:12 +010056#define ASUS_LAPTOP_NAME "Asus Laptop Support"
57#define ASUS_LAPTOP_CLASS "hotkey"
58#define ASUS_LAPTOP_DEVICE_NAME "Hotkey"
59#define ASUS_LAPTOP_FILE KBUILD_MODNAME
60#define ASUS_LAPTOP_PREFIX "\\_SB.ATKD."
Corentin Chary85091b72007-01-26 14:04:30 +010061
Corentin Chary85091b72007-01-26 14:04:30 +010062MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
Corentin Chary50a90c42009-11-30 21:55:12 +010063MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
Corentin Chary85091b72007-01-26 14:04:30 +010064MODULE_LICENSE("GPL");
65
Corentin Charybe966662009-08-29 10:28:29 +020066/*
67 * WAPF defines the behavior of the Fn+Fx wlan key
Corentin Chary185e5af2007-03-11 10:27:33 +010068 * The significance of values is yet to be found, but
69 * most of the time:
70 * 0x0 will do nothing
71 * 0x1 will allow to control the device with Fn+Fx key.
72 * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
73 * 0x5 like 0x1 or 0x4
74 * So, if something doesn't work as you want, just try other values =)
75 */
76static uint wapf = 1;
77module_param(wapf, uint, 0644);
78MODULE_PARM_DESC(wapf, "WAPF value");
79
Corentin Charyd0930a22010-01-17 17:21:13 +010080static uint wlan_status = 1;
Corentin Chary0e875f42010-01-10 20:49:26 +010081static uint bluetooth_status = 1;
82
Corentin Charyd0930a22010-01-17 17:21:13 +010083module_param(wlan_status, uint, 0644);
84MODULE_PARM_DESC(wlan_status, "Set the wireless status on boot "
Corentin Chary0e875f42010-01-10 20:49:26 +010085 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
86 "default is 1");
87
88module_param(bluetooth_status, uint, 0644);
89MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
90 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
91 "default is 1");
92
Corentin Charybe4ee822009-12-06 16:27:09 +010093/*
94 * Some events we use, same for all Asus
95 */
96#define ATKD_BR_UP 0x10
97#define ATKD_BR_DOWN 0x20
98#define ATKD_LCD_ON 0x33
99#define ATKD_LCD_OFF 0x34
100
101/*
102 * Known bits returned by \_SB.ATKD.HWRS
103 */
104#define WL_HWRS 0x80
105#define BT_HWRS 0x100
106
107/*
108 * Flags for hotk status
109 * WL_ON and BT_ON are also used for wireless_status()
110 */
Corentin Chary17e78f62010-01-13 22:21:33 +0100111#define WL_RSTS 0x01 /* internal Wifi */
112#define BT_RSTS 0x02 /* internal Bluetooth */
Corentin Charybe4ee822009-12-06 16:27:09 +0100113
Corentin Chary85091b72007-01-26 14:04:30 +0100114#define ASUS_HANDLE(object, paths...) \
115 static acpi_handle object##_handle = NULL; \
116 static char *object##_paths[] = { paths }
117
Corentin Charybe18cda2007-01-26 14:04:35 +0100118/* LED */
Corentin Charyd99b5772010-01-22 21:20:57 +0100119#define METHOD_MLED "MLED"
120#define METHOD_TLED "TLED"
121#define METHOD_RLED "RLED" /* W1JC */
122#define METHOD_PLED "PLED" /* A7J */
123#define METHOD_GLED "GLED" /* G1, G2 (probably) */
Corentin Charybe18cda2007-01-26 14:04:35 +0100124
Corentin Chary722ad972007-01-26 14:04:55 +0100125/* LEDD */
Corentin Charyd99b5772010-01-22 21:20:57 +0100126#define METHOD_LEDD "SLCM"
Corentin Chary722ad972007-01-26 14:04:55 +0100127
Corentin Charybe966662009-08-29 10:28:29 +0200128/*
129 * Bluetooth and WLAN
Corentin Chary4564de12007-01-26 14:04:40 +0100130 * WLED and BLED are not handled like other XLED, because in some dsdt
131 * they also control the WLAN/Bluetooth device.
132 */
Corentin Charyd99b5772010-01-22 21:20:57 +0100133#define METHOD_WLAN "WLED"
134#define METHOD_BLUETOOTH "BLED"
135#define METHOD_WL_STATUS "RSTS"
Corentin Chary4564de12007-01-26 14:04:40 +0100136
Corentin Chary6b7091e2007-01-26 14:04:45 +0100137/* Brightness */
Corentin Charyd99b5772010-01-22 21:20:57 +0100138#define METHOD_BRIGHTNESS_SET "SPLV"
139#define METHOD_BRIGHTNESS_GET "GPLV"
Corentin Chary6b7091e2007-01-26 14:04:45 +0100140
141/* Backlight */
Len Brown8def05f2007-01-30 01:46:43 -0500142ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
143 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
144 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
145 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
146 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
Torsten Krah4d0b856e2008-10-17 09:47:57 +0200147 "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
Len Brown8def05f2007-01-30 01:46:43 -0500148 "\\_SB.PCI0.PX40.Q10", /* S1x */
149 "\\Q10"); /* A2x, L2D, L3D, M2E */
Corentin Chary6b7091e2007-01-26 14:04:45 +0100150
Corentin Chary78127b42007-01-26 14:04:49 +0100151/* Display */
Corentin Charyd99b5772010-01-22 21:20:57 +0100152#define METHOD_SWITCH_DISPLAY "SDSP"
Corentin Charybe966662009-08-29 10:28:29 +0200153ASUS_HANDLE(display_get,
154 /* A6B, A6K A6R A7D F3JM L4R M6R A3G M6A M6V VX-1 V6J V6V W3Z */
155 "\\_SB.PCI0.P0P1.VGA.GETD",
156 /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V S5A M5A z33A W1Jc W2V G1 */
157 "\\_SB.PCI0.P0P2.VGA.GETD",
158 /* A6V A6Q */
159 "\\_SB.PCI0.P0P3.VGA.GETD",
160 /* A6T, A6M */
161 "\\_SB.PCI0.P0PA.VGA.GETD",
162 /* L3C */
163 "\\_SB.PCI0.PCI1.VGAC.NMAP",
164 /* Z96F */
165 "\\_SB.PCI0.VGA.GETD",
166 /* A2D */
167 "\\ACTD",
168 /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
169 "\\ADVG",
170 /* P30 */
171 "\\DNXT",
172 /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
173 "\\INFB",
174 /* A3F A6F A3N A3L M6N W3N W6A */
175 "\\SSTE");
Corentin Chary78127b42007-01-26 14:04:49 +0100176
Corentin Charyd99b5772010-01-22 21:20:57 +0100177#define METHOD_ALS_CONTROL "ALSC" /* Z71A Z71V */
178#define METHOD_ALS_LEVEL "ALSL" /* Z71A Z71V */
Corentin Chary8b857352007-01-26 14:04:58 +0100179
Corentin Charye539c2f2007-05-06 14:47:06 +0200180/* GPS */
181/* R2H use different handle for GPS on/off */
Corentin Charyd99b5772010-01-22 21:20:57 +0100182#define METHOD_GPS_ON "SDON"
183#define METHOD_GPS_OFF "SDOF"
184#define METHOD_GPS_STATUS "GPST"
Corentin Charye539c2f2007-05-06 14:47:06 +0200185
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000186/* Keyboard light */
Corentin Charyd99b5772010-01-22 21:20:57 +0100187#define METHOD_KBD_LIGHT_SET "SLKB"
188#define METHOD_KBD_LIGHT_GET "GLKB"
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000189
Corentin Chary85091b72007-01-26 14:04:30 +0100190/*
Corentin Chary9129d142009-12-01 22:39:41 +0100191 * Define a specific led structure to keep the main structure clean
192 */
193#define ASUS_DEFINE_LED(object) \
194 int object##_wk; \
195 struct work_struct object##_work; \
196 struct led_classdev object;
197
198
199#define led_to_asus(led_cdev, led) \
200 container_of(container_of(led_cdev, struct asus_laptop_leds, \
201 led), \
202 struct asus_laptop, leds)
203#define work_to_asus(work, led) \
204 container_of(container_of(work, struct asus_laptop_leds, \
205 led##_work), \
206 struct asus_laptop, leds)
207
208struct asus_laptop_leds {
209 ASUS_DEFINE_LED(mled)
210 ASUS_DEFINE_LED(tled)
211 ASUS_DEFINE_LED(rled)
212 ASUS_DEFINE_LED(pled)
213 ASUS_DEFINE_LED(gled)
214 ASUS_DEFINE_LED(kled)
215 struct workqueue_struct *workqueue;
216};
217
218/*
Corentin Chary85091b72007-01-26 14:04:30 +0100219 * This is the main structure, we can use it to store anything interesting
220 * about the hotk device
221 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100222struct asus_laptop {
Corentin Charybe966662009-08-29 10:28:29 +0200223 char *name; /* laptop name */
Corentin Chary600ad522009-11-30 21:42:42 +0100224
Corentin Chary7c247642009-11-30 22:13:54 +0100225 struct acpi_table_header *dsdt_info;
Corentin Chary600ad522009-11-30 21:42:42 +0100226 struct platform_device *platform_device;
Corentin Chary7c247642009-11-30 22:13:54 +0100227 struct acpi_device *device; /* the device we are in */
228 struct backlight_device *backlight_device;
Corentin Chary9129d142009-12-01 22:39:41 +0100229
Corentin Chary7c247642009-11-30 22:13:54 +0100230 struct input_dev *inputdev;
Corentin Chary9129d142009-12-01 22:39:41 +0100231 struct key_entry *keymap;
232
233 struct asus_laptop_leds leds;
Corentin Chary7c247642009-11-30 22:13:54 +0100234
Corentin Charyaa9df932010-01-13 21:49:10 +0100235 int wireless_status;
236 bool have_rsts;
Corentin Chary3e68ae72010-01-13 22:10:39 +0100237 int lcd_state;
Corentin Charyaa9df932010-01-13 21:49:10 +0100238
Corentin Charybe966662009-08-29 10:28:29 +0200239 acpi_handle handle; /* the handle of the hotk device */
Corentin Charybe966662009-08-29 10:28:29 +0200240 u32 ledd_status; /* status of the LED display */
241 u8 light_level; /* light sensor level */
242 u8 light_switch; /* light sensor switch value */
243 u16 event_count[128]; /* count for each event TODO make this better */
Corentin Chary034ce902009-01-20 16:17:43 +0100244 u16 *keycode_map;
Corentin Chary85091b72007-01-26 14:04:30 +0100245};
246
Corentin Chary9129d142009-12-01 22:39:41 +0100247static const struct key_entry asus_keymap[] = {
Corentin Chary66a71dd2010-01-25 22:50:11 +0100248 {KE_KEY, 0x02, { KEY_SCREENLOCK } },
249 {KE_KEY, 0x05, { KEY_WLAN } },
250 {KE_KEY, 0x08, { KEY_F13 } },
251 {KE_KEY, 0x17, { KEY_ZOOM } },
252 {KE_KEY, 0x1f, { KEY_BATTERY } },
253 {KE_KEY, 0x30, { KEY_VOLUMEUP } },
254 {KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
255 {KE_KEY, 0x32, { KEY_MUTE } },
256 {KE_KEY, 0x33, { KEY_SWITCHVIDEOMODE } },
257 {KE_KEY, 0x34, { KEY_SWITCHVIDEOMODE } },
258 {KE_KEY, 0x40, { KEY_PREVIOUSSONG } },
259 {KE_KEY, 0x41, { KEY_NEXTSONG } },
260 {KE_KEY, 0x43, { KEY_STOPCD } },
261 {KE_KEY, 0x45, { KEY_PLAYPAUSE } },
262 {KE_KEY, 0x4c, { KEY_MEDIA } },
263 {KE_KEY, 0x50, { KEY_EMAIL } },
264 {KE_KEY, 0x51, { KEY_WWW } },
265 {KE_KEY, 0x55, { KEY_CALC } },
266 {KE_KEY, 0x5C, { KEY_SCREENLOCK } }, /* Screenlock */
267 {KE_KEY, 0x5D, { KEY_WLAN } },
268 {KE_KEY, 0x5E, { KEY_WLAN } },
269 {KE_KEY, 0x5F, { KEY_WLAN } },
270 {KE_KEY, 0x60, { KEY_SWITCHVIDEOMODE } },
271 {KE_KEY, 0x61, { KEY_SWITCHVIDEOMODE } },
272 {KE_KEY, 0x62, { KEY_SWITCHVIDEOMODE } },
273 {KE_KEY, 0x63, { KEY_SWITCHVIDEOMODE } },
274 {KE_KEY, 0x6B, { KEY_F13 } }, /* Lock Touchpad */
Corentin Chary7f607d712010-01-17 17:37:19 +0100275 {KE_KEY, 0x7E, { KEY_BLUETOOTH } },
276 {KE_KEY, 0x7D, { KEY_BLUETOOTH } },
Corentin Chary66a71dd2010-01-25 22:50:11 +0100277 {KE_KEY, 0x82, { KEY_CAMERA } },
278 {KE_KEY, 0x88, { KEY_WLAN } },
279 {KE_KEY, 0x8A, { KEY_PROG1 } },
280 {KE_KEY, 0x95, { KEY_MEDIA } },
281 {KE_KEY, 0x99, { KEY_PHONE } },
282 {KE_KEY, 0xc4, { KEY_KBDILLUMUP } },
283 {KE_KEY, 0xc5, { KEY_KBDILLUMDOWN } },
Corentin Chary034ce902009-01-20 16:17:43 +0100284 {KE_END, 0},
285};
286
Corentin Chary66a71dd2010-01-25 22:50:11 +0100287
Corentin Chary85091b72007-01-26 14:04:30 +0100288/*
289 * This function evaluates an ACPI method, given an int as parameter, the
290 * method is searched within the scope of the handle, can be NULL. The output
291 * of the method is written is output, which can also be NULL
292 *
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100293 * returns 0 if write is successful, -1 else.
Corentin Chary85091b72007-01-26 14:04:30 +0100294 */
Corentin Charyd8c67322009-11-28 10:27:51 +0100295static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
296 struct acpi_buffer *output)
Corentin Chary85091b72007-01-26 14:04:30 +0100297{
Corentin Charybe966662009-08-29 10:28:29 +0200298 struct acpi_object_list params; /* list of input parameters (an int) */
299 union acpi_object in_obj; /* the only param we use */
Corentin Chary85091b72007-01-26 14:04:30 +0100300 acpi_status status;
301
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100302 if (!handle)
303 return 0;
304
Corentin Chary85091b72007-01-26 14:04:30 +0100305 params.count = 1;
306 params.pointer = &in_obj;
307 in_obj.type = ACPI_TYPE_INTEGER;
308 in_obj.integer.value = val;
309
310 status = acpi_evaluate_object(handle, (char *)method, &params, output);
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100311 if (status == AE_OK)
312 return 0;
313 else
314 return -1;
Corentin Chary85091b72007-01-26 14:04:30 +0100315}
316
Corentin Charyd8c67322009-11-28 10:27:51 +0100317static int write_acpi_int(acpi_handle handle, const char *method, int val)
318{
319 return write_acpi_int_ret(handle, method, val, NULL);
320}
321
Corentin Charyd99b5772010-01-22 21:20:57 +0100322static int acpi_check_handle(acpi_handle handle, const char *method,
323 acpi_handle *ret)
324{
325 acpi_status status;
326
327 if (method == NULL)
328 return -ENODEV;
329
330 if (ret)
331 status = acpi_get_handle(handle, (char *)method,
332 ret);
333 else {
334 acpi_handle dummy;
335
336 status = acpi_get_handle(handle, (char *)method,
337 &dummy);
338 }
339
340 if (status != AE_OK) {
341 if (ret)
342 pr_warning("Error finding %s\n", method);
343 return -ENODEV;
344 }
345 return 0;
346}
347
Corentin Chary17e78f62010-01-13 22:21:33 +0100348/* Generic LED function */
Corentin Charye5593bf2010-01-17 17:20:11 +0100349static int asus_led_set(struct asus_laptop *asus, char *method,
Corentin Chary17e78f62010-01-13 22:21:33 +0100350 int value)
Corentin Charybe18cda2007-01-26 14:04:35 +0100351{
Corentin Charyd99b5772010-01-22 21:20:57 +0100352 if (!strcmp(method, METHOD_MLED))
Corentin Chary17e78f62010-01-13 22:21:33 +0100353 value = !value;
Corentin Charyd99b5772010-01-22 21:20:57 +0100354 else if (!strcmp(method, METHOD_GLED))
Corentin Chary17e78f62010-01-13 22:21:33 +0100355 value = !value + 1;
356 else
357 value = !!value;
Corentin Charybe18cda2007-01-26 14:04:35 +0100358
Corentin Charye5593bf2010-01-17 17:20:11 +0100359 return write_acpi_int(asus->handle, method, value);
Corentin Charybe18cda2007-01-26 14:04:35 +0100360}
361
Corentin Charybe4ee822009-12-06 16:27:09 +0100362/*
363 * LEDs
364 */
365#define ASUS_LED(object, ledname, max) \
366 static void object##_led_set(struct led_classdev *led_cdev, \
367 enum led_brightness value); \
368 static enum led_brightness object##_led_get( \
369 struct led_classdev *led_cdev); \
370 static void object##_led_update(struct work_struct *ignored); \
371 static struct led_classdev object##_led = { \
372 .name = "asus::" ledname, \
373 .brightness_set = object##_led_set, \
374 .brightness_get = object##_led_get, \
375 .max_brightness = max \
376 }
377
378ASUS_LED(mled, "mail", 1);
379ASUS_LED(tled, "touchpad", 1);
380ASUS_LED(rled, "record", 1);
381ASUS_LED(pled, "phone", 1);
382ASUS_LED(gled, "gaming", 1);
383ASUS_LED(kled, "kbd_backlight", 3);
384
Corentin Charybe18cda2007-01-26 14:04:35 +0100385/* /sys/class/led handlers */
Corentin Charyd99b5772010-01-22 21:20:57 +0100386#define ASUS_LED_HANDLER(object, method) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100387 static void object##_led_set(struct led_classdev *led_cdev, \
388 enum led_brightness value) \
389 { \
Corentin Chary9129d142009-12-01 22:39:41 +0100390 struct asus_laptop *asus = \
391 led_to_asus(led_cdev, object); \
392 \
393 asus->leds.object##_wk = (value > 0) ? 1 : 0; \
394 queue_work(asus->leds.workqueue, \
395 &asus->leds.object##_work); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100396 } \
Corentin Chary9129d142009-12-01 22:39:41 +0100397 static void object##_led_update(struct work_struct *work) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100398 { \
Corentin Chary9129d142009-12-01 22:39:41 +0100399 struct asus_laptop *asus = work_to_asus(work, object); \
400 \
401 int value = asus->leds.object##_wk; \
Corentin Charyd99b5772010-01-22 21:20:57 +0100402 asus_led_set(asus, method, value); \
Corentin Charyabfa57e2009-08-28 12:56:47 +0000403 } \
404 static enum led_brightness object##_led_get( \
405 struct led_classdev *led_cdev) \
406 { \
407 return led_cdev->brightness; \
Corentin Charybe18cda2007-01-26 14:04:35 +0100408 }
409
Corentin Charyd99b5772010-01-22 21:20:57 +0100410ASUS_LED_HANDLER(mled, METHOD_MLED);
411ASUS_LED_HANDLER(pled, METHOD_PLED);
412ASUS_LED_HANDLER(rled, METHOD_RLED);
413ASUS_LED_HANDLER(tled, METHOD_TLED);
414ASUS_LED_HANDLER(gled, METHOD_GLED);
Corentin Charybe18cda2007-01-26 14:04:35 +0100415
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000416/*
Corentin Charybe4ee822009-12-06 16:27:09 +0100417 * Keyboard backlight (also a LED)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000418 */
Corentin Charyd99b5772010-01-22 21:20:57 +0100419static int asus_kled_lvl(struct asus_laptop *asus)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000420{
421 unsigned long long kblv;
422 struct acpi_object_list params;
423 union acpi_object in_obj;
424 acpi_status rv;
425
426 params.count = 1;
427 params.pointer = &in_obj;
428 in_obj.type = ACPI_TYPE_INTEGER;
429 in_obj.integer.value = 2;
430
Corentin Charyd99b5772010-01-22 21:20:57 +0100431 rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
432 &params, &kblv);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000433 if (ACPI_FAILURE(rv)) {
434 pr_warning("Error reading kled level\n");
Corentin Chary4d441512010-01-13 22:26:24 +0100435 return -ENODEV;
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000436 }
437 return kblv;
438}
439
Corentin Chary4d441512010-01-13 22:26:24 +0100440static int asus_kled_set(struct asus_laptop *asus, int kblv)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000441{
442 if (kblv > 0)
443 kblv = (1 << 7) | (kblv & 0x7F);
444 else
445 kblv = 0;
446
Corentin Charyd99b5772010-01-22 21:20:57 +0100447 if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000448 pr_warning("Keyboard LED display write failed\n");
449 return -EINVAL;
450 }
451 return 0;
452}
453
454static void kled_led_set(struct led_classdev *led_cdev,
455 enum led_brightness value)
456{
Corentin Chary9129d142009-12-01 22:39:41 +0100457 struct asus_laptop *asus = led_to_asus(led_cdev, kled);
458
459 asus->leds.kled_wk = value;
460 queue_work(asus->leds.workqueue, &asus->leds.kled_work);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000461}
462
Corentin Chary9129d142009-12-01 22:39:41 +0100463static void kled_led_update(struct work_struct *work)
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000464{
Corentin Chary9129d142009-12-01 22:39:41 +0100465 struct asus_laptop *asus = work_to_asus(work, kled);
466
Corentin Chary4d441512010-01-13 22:26:24 +0100467 asus_kled_set(asus, asus->leds.kled_wk);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000468}
469
470static enum led_brightness kled_led_get(struct led_classdev *led_cdev)
471{
Corentin Charyd99b5772010-01-22 21:20:57 +0100472 struct asus_laptop *asus = led_to_asus(led_cdev, kled);
473
474 return asus_kled_lvl(asus);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000475}
476
Corentin Charybe4ee822009-12-06 16:27:09 +0100477#define ASUS_LED_UNREGISTER(object) \
478 if (object##_led.dev) \
479 led_classdev_unregister(&object##_led)
480
481static void asus_led_exit(struct asus_laptop *asus)
482{
483 ASUS_LED_UNREGISTER(mled);
484 ASUS_LED_UNREGISTER(tled);
485 ASUS_LED_UNREGISTER(pled);
486 ASUS_LED_UNREGISTER(rled);
487 ASUS_LED_UNREGISTER(gled);
488 ASUS_LED_UNREGISTER(kled);
489 if (asus->leds.workqueue) {
490 destroy_workqueue(asus->leds.workqueue);
491 asus->leds.workqueue = NULL;
492 }
493}
494
495/* Ugly macro, need to fix that later */
Corentin Charyd99b5772010-01-22 21:20:57 +0100496#define ASUS_LED_REGISTER(asus, object, _name, max, method) \
Corentin Charybe4ee822009-12-06 16:27:09 +0100497 do { \
498 struct led_classdev *ldev = &asus->leds.object; \
Corentin Charyd99b5772010-01-22 21:20:57 +0100499 \
500 if (method && acpi_check_handle(asus->handle, method, NULL)) \
Corentin Charybe4ee822009-12-06 16:27:09 +0100501 break ; \
502 \
503 INIT_WORK(&asus->leds.object##_work, object##_led_update); \
504 ldev->name = "asus::" _name; \
505 ldev->brightness_set = object##_led_set; \
506 ldev->max_brightness = max; \
507 rv = led_classdev_register(&asus->platform_device->dev, ldev); \
508 if (rv) \
509 goto error; \
510 } while (0)
511
512static int asus_led_init(struct asus_laptop *asus)
513{
514 int rv;
515
516 /*
517 * Functions that actually update the LED's are called from a
518 * workqueue. By doing this as separate work rather than when the LED
519 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
520 * potentially bad time, such as a timer interrupt.
521 */
522 asus->leds.workqueue = create_singlethread_workqueue("led_workqueue");
523 if (!asus->leds.workqueue)
524 return -ENOMEM;
525
Corentin Charyd99b5772010-01-22 21:20:57 +0100526 ASUS_LED_REGISTER(asus, mled, "mail", 1, METHOD_MLED);
527 ASUS_LED_REGISTER(asus, tled, "touchpad", 1, METHOD_TLED);
528 ASUS_LED_REGISTER(asus, rled, "record", 1, METHOD_RLED);
529 ASUS_LED_REGISTER(asus, pled, "phone", 1, METHOD_PLED);
530 ASUS_LED_REGISTER(asus, gled, "gaming", 1, METHOD_GLED);
531 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
532 !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL))
533 ASUS_LED_REGISTER(asus, kled, "kbd_backlight", 3, NULL);
Corentin Charybe4ee822009-12-06 16:27:09 +0100534error:
535 if (rv)
536 asus_led_exit(asus);
537 return rv;
538}
539
540/*
541 * Backlight device
542 */
Corentin Chary3e68ae72010-01-13 22:10:39 +0100543static int asus_lcd_status(struct asus_laptop *asus)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100544{
Corentin Chary3e68ae72010-01-13 22:10:39 +0100545 return asus->lcd_state;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100546}
547
Corentin Chary3e68ae72010-01-13 22:10:39 +0100548static int asus_lcd_set(struct asus_laptop *asus, int value)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100549{
550 int lcd = 0;
551 acpi_status status = 0;
552
Corentin Chary3e68ae72010-01-13 22:10:39 +0100553 lcd = !!value;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100554
Corentin Chary3e68ae72010-01-13 22:10:39 +0100555 if (lcd == asus_lcd_status(asus))
Corentin Chary6b7091e2007-01-26 14:04:45 +0100556 return 0;
557
Corentin Chary3e68ae72010-01-13 22:10:39 +0100558 if (!lcd_switch_handle)
559 return -ENODEV;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100560
Corentin Chary3e68ae72010-01-13 22:10:39 +0100561 status = acpi_evaluate_object(lcd_switch_handle,
562 NULL, NULL, NULL);
563
564 if (ACPI_FAILURE(status)) {
565 pr_warning("Error switching LCD\n");
566 return -ENODEV;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100567 }
568
Corentin Chary3e68ae72010-01-13 22:10:39 +0100569 asus->lcd_state = lcd;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100570 return 0;
571}
572
Corentin Chary9129d142009-12-01 22:39:41 +0100573static void lcd_blank(struct asus_laptop *asus, int blank)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100574{
Corentin Chary7c247642009-11-30 22:13:54 +0100575 struct backlight_device *bd = asus->backlight_device;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100576
Corentin Chary3e68ae72010-01-13 22:10:39 +0100577 asus->lcd_state = (blank == FB_BLANK_UNBLANK);
578
Len Brown8def05f2007-01-30 01:46:43 -0500579 if (bd) {
Richard Purdie599a52d2007-02-10 23:07:48 +0000580 bd->props.power = blank;
Richard Purdie28ee0862007-02-08 22:25:09 +0000581 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100582 }
583}
584
Corentin Chary4d441512010-01-13 22:26:24 +0100585static int asus_read_brightness(struct backlight_device *bd)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100586{
Corentin Charyd99b5772010-01-22 21:20:57 +0100587 struct asus_laptop *asus = bl_get_data(bd);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400588 unsigned long long value;
Corentin Chary9a816852007-03-11 10:25:38 +0100589 acpi_status rv = AE_OK;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100590
Corentin Charyd99b5772010-01-22 21:20:57 +0100591 rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
592 NULL, &value);
Corentin Chary9a816852007-03-11 10:25:38 +0100593 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200594 pr_warning("Error reading brightness\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100595
596 return value;
597}
598
Corentin Chary4d441512010-01-13 22:26:24 +0100599static int asus_set_brightness(struct backlight_device *bd, int value)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100600{
Corentin Charyd99b5772010-01-22 21:20:57 +0100601 struct asus_laptop *asus = bl_get_data(bd);
602
603 if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200604 pr_warning("Error changing brightness\n");
Corentin Charye5b50f62009-11-28 10:19:55 +0100605 return -EIO;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100606 }
Corentin Charye5b50f62009-11-28 10:19:55 +0100607 return 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100608}
609
610static int update_bl_status(struct backlight_device *bd)
611{
Corentin Chary9129d142009-12-01 22:39:41 +0100612 struct asus_laptop *asus = bl_get_data(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100613 int rv;
Richard Purdie599a52d2007-02-10 23:07:48 +0000614 int value = bd->props.brightness;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100615
Corentin Chary4d441512010-01-13 22:26:24 +0100616 rv = asus_set_brightness(bd, value);
Len Brown8def05f2007-01-30 01:46:43 -0500617 if (rv)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100618 return rv;
619
Richard Purdie599a52d2007-02-10 23:07:48 +0000620 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
Corentin Chary3e68ae72010-01-13 22:10:39 +0100621 return asus_lcd_set(asus, value);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100622}
623
Corentin Charybe4ee822009-12-06 16:27:09 +0100624static struct backlight_ops asusbl_ops = {
Corentin Chary4d441512010-01-13 22:26:24 +0100625 .get_brightness = asus_read_brightness,
Corentin Charybe4ee822009-12-06 16:27:09 +0100626 .update_status = update_bl_status,
627};
628
629static int asus_backlight_init(struct asus_laptop *asus)
630{
631 struct backlight_device *bd;
632 struct device *dev = &asus->platform_device->dev;
633
Corentin Charyd99b5772010-01-22 21:20:57 +0100634 if (!acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) &&
635 !acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL) &&
636 lcd_switch_handle) {
Corentin Charybe4ee822009-12-06 16:27:09 +0100637 bd = backlight_device_register(ASUS_LAPTOP_FILE, dev,
638 asus, &asusbl_ops);
639 if (IS_ERR(bd)) {
640 pr_err("Could not register asus backlight device\n");
641 asus->backlight_device = NULL;
642 return PTR_ERR(bd);
643 }
644
645 asus->backlight_device = bd;
646
647 bd->props.max_brightness = 15;
Corentin Charybe4ee822009-12-06 16:27:09 +0100648 bd->props.power = FB_BLANK_UNBLANK;
Corentin Charyd99b5772010-01-22 21:20:57 +0100649 bd->props.brightness = asus_read_brightness(bd);
Corentin Charybe4ee822009-12-06 16:27:09 +0100650 backlight_update_status(bd);
651 }
652 return 0;
653}
654
655static void asus_backlight_exit(struct asus_laptop *asus)
656{
657 if (asus->backlight_device)
658 backlight_device_unregister(asus->backlight_device);
659}
660
Corentin Chary85091b72007-01-26 14:04:30 +0100661/*
662 * Platform device handlers
663 */
664
665/*
666 * We write our info in page, we begin at offset off and cannot write more
667 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
668 * number of bytes written in page
669 */
670static ssize_t show_infos(struct device *dev,
Len Brown8def05f2007-01-30 01:46:43 -0500671 struct device_attribute *attr, char *page)
Corentin Chary85091b72007-01-26 14:04:30 +0100672{
Corentin Chary9129d142009-12-01 22:39:41 +0100673 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary85091b72007-01-26 14:04:30 +0100674 int len = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400675 unsigned long long temp;
Corentin Charybe966662009-08-29 10:28:29 +0200676 char buf[16]; /* enough for all info */
Corentin Chary9a816852007-03-11 10:25:38 +0100677 acpi_status rv = AE_OK;
678
Corentin Chary85091b72007-01-26 14:04:30 +0100679 /*
680 * We use the easy way, we don't care of off and count, so we don't set eof
681 * to 1
682 */
683
Corentin Chary50a90c42009-11-30 21:55:12 +0100684 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
685 len += sprintf(page + len, "Model reference : %s\n", asus->name);
Corentin Chary85091b72007-01-26 14:04:30 +0100686 /*
687 * The SFUN method probably allows the original driver to get the list
688 * of features supported by a given model. For now, 0x0100 or 0x0800
689 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
690 * The significance of others is yet to be found.
691 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100692 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
Corentin Chary9a816852007-03-11 10:25:38 +0100693 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000694 len += sprintf(page + len, "SFUN value : %#x\n",
695 (uint) temp);
696 /*
697 * The HWRS method return informations about the hardware.
698 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
699 * The significance of others is yet to be found.
700 * If we don't find the method, we assume the device are present.
701 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100702 rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
Corentin Chary1d4a3802009-08-28 12:56:46 +0000703 if (!ACPI_FAILURE(rv))
704 len += sprintf(page + len, "HRWS value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100705 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100706 /*
707 * Another value for userspace: the ASYM method returns 0x02 for
708 * battery low and 0x04 for battery critical, its readings tend to be
709 * more accurate than those provided by _BST.
710 * Note: since not all the laptops provide this method, errors are
711 * silently ignored.
712 */
Corentin Chary50a90c42009-11-30 21:55:12 +0100713 rv = acpi_evaluate_integer(asus->handle, "ASYM", 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, "ASYM value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100716 (uint) temp);
Corentin Chary7c247642009-11-30 22:13:54 +0100717 if (asus->dsdt_info) {
718 snprintf(buf, 16, "%d", asus->dsdt_info->length);
Corentin Chary85091b72007-01-26 14:04:30 +0100719 len += sprintf(page + len, "DSDT length : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100720 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
Corentin Chary85091b72007-01-26 14:04:30 +0100721 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100722 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100723 len += sprintf(page + len, "DSDT revision : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100724 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100725 len += sprintf(page + len, "OEM id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100726 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100727 len += sprintf(page + len, "OEM table id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100728 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100729 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100730 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
Corentin Chary85091b72007-01-26 14:04:30 +0100731 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
Corentin Chary7c247642009-11-30 22:13:54 +0100732 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
Corentin Chary85091b72007-01-26 14:04:30 +0100733 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
734 }
735
736 return len;
737}
738
739static int parse_arg(const char *buf, unsigned long count, int *val)
740{
741 if (!count)
742 return 0;
743 if (count > 31)
744 return -EINVAL;
745 if (sscanf(buf, "%i", val) != 1)
746 return -EINVAL;
747 return count;
748}
749
Corentin Chary17e78f62010-01-13 22:21:33 +0100750static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
751 const char *buf, size_t count,
Corentin Charyd99b5772010-01-22 21:20:57 +0100752 const char *method)
Corentin Chary4564de12007-01-26 14:04:40 +0100753{
754 int rv, value;
755 int out = 0;
756
757 rv = parse_arg(buf, count, &value);
758 if (rv > 0)
759 out = value ? 1 : 0;
760
Corentin Charyd99b5772010-01-22 21:20:57 +0100761 if (write_acpi_int(asus->handle, method, value))
Corentin Chary17e78f62010-01-13 22:21:33 +0100762 return -ENODEV;
Corentin Chary4564de12007-01-26 14:04:40 +0100763 return rv;
764}
765
766/*
Corentin Chary722ad972007-01-26 14:04:55 +0100767 * LEDD display
768 */
769static ssize_t show_ledd(struct device *dev,
770 struct device_attribute *attr, char *buf)
771{
Corentin Chary9129d142009-12-01 22:39:41 +0100772 struct asus_laptop *asus = dev_get_drvdata(dev);
773
Corentin Chary50a90c42009-11-30 21:55:12 +0100774 return sprintf(buf, "0x%08x\n", asus->ledd_status);
Corentin Chary722ad972007-01-26 14:04:55 +0100775}
776
777static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
778 const char *buf, size_t count)
779{
Corentin Chary9129d142009-12-01 22:39:41 +0100780 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary722ad972007-01-26 14:04:55 +0100781 int rv, value;
782
783 rv = parse_arg(buf, count, &value);
784 if (rv > 0) {
Corentin Charyd99b5772010-01-22 21:20:57 +0100785 if (write_acpi_int(asus->handle, METHOD_LEDD, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200786 pr_warning("LED display write failed\n");
Corentin Chary722ad972007-01-26 14:04:55 +0100787 else
Corentin Chary50a90c42009-11-30 21:55:12 +0100788 asus->ledd_status = (u32) value;
Corentin Chary722ad972007-01-26 14:04:55 +0100789 }
790 return rv;
791}
792
793/*
Corentin Charyaa9df932010-01-13 21:49:10 +0100794 * Wireless
795 */
796static int asus_wireless_status(struct asus_laptop *asus, int mask)
797{
798 unsigned long long status;
799 acpi_status rv = AE_OK;
800
801 if (!asus->have_rsts)
802 return (asus->wireless_status & mask) ? 1 : 0;
803
Corentin Charyd99b5772010-01-22 21:20:57 +0100804 rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
805 NULL, &status);
Corentin Charyaa9df932010-01-13 21:49:10 +0100806 if (ACPI_FAILURE(rv)) {
807 pr_warning("Error reading Wireless status\n");
808 return -EINVAL;
809 }
810 return !!(status & mask);
811}
812
813/*
Corentin Chary4564de12007-01-26 14:04:40 +0100814 * WLAN
815 */
Corentin Charye5593bf2010-01-17 17:20:11 +0100816static int asus_wlan_set(struct asus_laptop *asus, int status)
817{
818 if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
819 pr_warning("Error setting wlan status to %d", status);
820 return -EIO;
821 }
822 return 0;
823}
824
Corentin Chary4564de12007-01-26 14:04:40 +0100825static ssize_t show_wlan(struct device *dev,
826 struct device_attribute *attr, char *buf)
827{
Corentin Chary9129d142009-12-01 22:39:41 +0100828 struct asus_laptop *asus = dev_get_drvdata(dev);
829
Corentin Chary17e78f62010-01-13 22:21:33 +0100830 return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100831}
832
833static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
834 const char *buf, size_t count)
835{
Corentin Chary9129d142009-12-01 22:39:41 +0100836 struct asus_laptop *asus = dev_get_drvdata(dev);
837
Corentin Charyd99b5772010-01-22 21:20:57 +0100838 return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
Corentin Chary4564de12007-01-26 14:04:40 +0100839}
840
841/*
842 * Bluetooth
843 */
Corentin Charye5593bf2010-01-17 17:20:11 +0100844static int asus_bluetooth_set(struct asus_laptop *asus, int status)
845{
846 if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
847 pr_warning("Error setting bluetooth status to %d", status);
848 return -EIO;
849 }
850 return 0;
851}
852
Corentin Chary4564de12007-01-26 14:04:40 +0100853static ssize_t show_bluetooth(struct device *dev,
854 struct device_attribute *attr, char *buf)
855{
Corentin Chary9129d142009-12-01 22:39:41 +0100856 struct asus_laptop *asus = dev_get_drvdata(dev);
857
Corentin Chary17e78f62010-01-13 22:21:33 +0100858 return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
Corentin Chary4564de12007-01-26 14:04:40 +0100859}
860
Len Brown8def05f2007-01-30 01:46:43 -0500861static ssize_t store_bluetooth(struct device *dev,
862 struct device_attribute *attr, const char *buf,
863 size_t count)
Corentin Chary4564de12007-01-26 14:04:40 +0100864{
Corentin Chary9129d142009-12-01 22:39:41 +0100865 struct asus_laptop *asus = dev_get_drvdata(dev);
866
Corentin Charyd99b5772010-01-22 21:20:57 +0100867 return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
Corentin Chary4564de12007-01-26 14:04:40 +0100868}
869
Corentin Chary78127b42007-01-26 14:04:49 +0100870/*
871 * Display
872 */
Corentin Chary4d441512010-01-13 22:26:24 +0100873static void asus_set_display(struct asus_laptop *asus, int value)
Corentin Chary78127b42007-01-26 14:04:49 +0100874{
875 /* no sanity check needed for now */
Corentin Charyd99b5772010-01-22 21:20:57 +0100876 if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200877 pr_warning("Error setting display\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100878 return;
879}
880
Corentin Chary9129d142009-12-01 22:39:41 +0100881static int read_display(struct asus_laptop *asus)
Corentin Chary78127b42007-01-26 14:04:49 +0100882{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400883 unsigned long long value = 0;
Corentin Chary9a816852007-03-11 10:25:38 +0100884 acpi_status rv = AE_OK;
Corentin Chary78127b42007-01-26 14:04:49 +0100885
Corentin Charybe966662009-08-29 10:28:29 +0200886 /*
887 * In most of the case, we know how to set the display, but sometime
888 * we can't read it
889 */
Len Brown8def05f2007-01-30 01:46:43 -0500890 if (display_get_handle) {
Corentin Chary9a816852007-03-11 10:25:38 +0100891 rv = acpi_evaluate_integer(display_get_handle, NULL,
892 NULL, &value);
893 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200894 pr_warning("Error reading display status\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100895 }
896
897 value &= 0x0F; /* needed for some models, shouldn't hurt others */
898
899 return value;
900}
Len Brown8def05f2007-01-30 01:46:43 -0500901
Corentin Chary78127b42007-01-26 14:04:49 +0100902/*
903 * Now, *this* one could be more user-friendly, but so far, no-one has
904 * complained. The significance of bits is the same as in store_disp()
905 */
906static ssize_t show_disp(struct device *dev,
907 struct device_attribute *attr, char *buf)
908{
Corentin Chary9129d142009-12-01 22:39:41 +0100909 struct asus_laptop *asus = dev_get_drvdata(dev);
910
911 return sprintf(buf, "%d\n", read_display(asus));
Corentin Chary78127b42007-01-26 14:04:49 +0100912}
913
914/*
915 * Experimental support for display switching. As of now: 1 should activate
916 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
917 * Any combination (bitwise) of these will suffice. I never actually tested 4
918 * displays hooked up simultaneously, so be warned. See the acpi4asus README
919 * for more info.
920 */
921static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
922 const char *buf, size_t count)
923{
Corentin Chary9129d142009-12-01 22:39:41 +0100924 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary78127b42007-01-26 14:04:49 +0100925 int rv, value;
926
927 rv = parse_arg(buf, count, &value);
928 if (rv > 0)
Corentin Chary4d441512010-01-13 22:26:24 +0100929 asus_set_display(asus, value);
Corentin Chary78127b42007-01-26 14:04:49 +0100930 return rv;
931}
932
Corentin Chary8b857352007-01-26 14:04:58 +0100933/*
934 * Light Sens
935 */
Corentin Chary4d441512010-01-13 22:26:24 +0100936static void asus_als_switch(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100937{
Corentin Charyd99b5772010-01-22 21:20:57 +0100938 if (write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200939 pr_warning("Error setting light sensor switch\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100940 asus->light_switch = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100941}
942
943static ssize_t show_lssw(struct device *dev,
944 struct device_attribute *attr, char *buf)
945{
Corentin Chary9129d142009-12-01 22:39:41 +0100946 struct asus_laptop *asus = dev_get_drvdata(dev);
947
Corentin Chary50a90c42009-11-30 21:55:12 +0100948 return sprintf(buf, "%d\n", asus->light_switch);
Corentin Chary8b857352007-01-26 14:04:58 +0100949}
950
951static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
952 const char *buf, size_t count)
953{
Corentin Chary9129d142009-12-01 22:39:41 +0100954 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100955 int rv, value;
956
957 rv = parse_arg(buf, count, &value);
958 if (rv > 0)
Corentin Chary4d441512010-01-13 22:26:24 +0100959 asus_als_switch(asus, value ? 1 : 0);
Corentin Chary8b857352007-01-26 14:04:58 +0100960
961 return rv;
962}
963
Corentin Chary4d441512010-01-13 22:26:24 +0100964static void asus_als_level(struct asus_laptop *asus, int value)
Corentin Chary8b857352007-01-26 14:04:58 +0100965{
Corentin Charyd99b5772010-01-22 21:20:57 +0100966 if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200967 pr_warning("Error setting light sensor level\n");
Corentin Chary50a90c42009-11-30 21:55:12 +0100968 asus->light_level = value;
Corentin Chary8b857352007-01-26 14:04:58 +0100969}
970
971static ssize_t show_lslvl(struct device *dev,
972 struct device_attribute *attr, char *buf)
973{
Corentin Chary9129d142009-12-01 22:39:41 +0100974 struct asus_laptop *asus = dev_get_drvdata(dev);
975
Corentin Chary50a90c42009-11-30 21:55:12 +0100976 return sprintf(buf, "%d\n", asus->light_level);
Corentin Chary8b857352007-01-26 14:04:58 +0100977}
978
979static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
980 const char *buf, size_t count)
981{
Corentin Chary9129d142009-12-01 22:39:41 +0100982 struct asus_laptop *asus = dev_get_drvdata(dev);
Corentin Chary8b857352007-01-26 14:04:58 +0100983 int rv, value;
984
985 rv = parse_arg(buf, count, &value);
986 if (rv > 0) {
987 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
988 /* 0 <= value <= 15 */
Corentin Chary4d441512010-01-13 22:26:24 +0100989 asus_als_level(asus, value);
Corentin Chary8b857352007-01-26 14:04:58 +0100990 }
991
992 return rv;
993}
994
Corentin Charye539c2f2007-05-06 14:47:06 +0200995/*
996 * GPS
Corentin Chary6358bf22010-01-13 21:55:44 +0100997 * TODO: use rfkill
Corentin Charye539c2f2007-05-06 14:47:06 +0200998 */
Corentin Chary6358bf22010-01-13 21:55:44 +0100999static int asus_gps_status(struct asus_laptop *asus)
1000{
1001 unsigned long long status;
1002 acpi_status rv = AE_OK;
1003
Corentin Charyd99b5772010-01-22 21:20:57 +01001004 rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
1005 NULL, &status);
Corentin Chary6358bf22010-01-13 21:55:44 +01001006 if (ACPI_FAILURE(rv)) {
1007 pr_warning("Error reading GPS status\n");
1008 return -ENODEV;
1009 }
1010 return !!status;
1011}
1012
1013static int asus_gps_switch(struct asus_laptop *asus, int status)
1014{
Corentin Charyd99b5772010-01-22 21:20:57 +01001015 const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
Corentin Chary6358bf22010-01-13 21:55:44 +01001016
Corentin Charyd99b5772010-01-22 21:20:57 +01001017 if (write_acpi_int(asus->handle, meth, 0x02))
Corentin Chary6358bf22010-01-13 21:55:44 +01001018 return -ENODEV;
1019 return 0;
1020}
1021
Corentin Charye539c2f2007-05-06 14:47:06 +02001022static ssize_t show_gps(struct device *dev,
1023 struct device_attribute *attr, char *buf)
1024{
Corentin Chary9129d142009-12-01 22:39:41 +01001025 struct asus_laptop *asus = dev_get_drvdata(dev);
1026
Corentin Chary6358bf22010-01-13 21:55:44 +01001027 return sprintf(buf, "%d\n", asus_gps_status(asus));
Corentin Charye539c2f2007-05-06 14:47:06 +02001028}
1029
1030static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
1031 const char *buf, size_t count)
1032{
Corentin Chary6358bf22010-01-13 21:55:44 +01001033 struct asus_laptop *asus = dev_get_drvdata(dev);
1034 int rv, value;
1035 int ret;
Corentin Chary9129d142009-12-01 22:39:41 +01001036
Corentin Chary6358bf22010-01-13 21:55:44 +01001037 rv = parse_arg(buf, count, &value);
1038 if (rv <= 0)
1039 return -EINVAL;
1040 ret = asus_gps_switch(asus, !!value);
1041 if (ret)
1042 return ret;
1043 return rv;
Corentin Charye539c2f2007-05-06 14:47:06 +02001044}
1045
Corentin Chary034ce902009-01-20 16:17:43 +01001046/*
Corentin Charybe4ee822009-12-06 16:27:09 +01001047 * Input device (i.e. hotkeys)
Corentin Chary034ce902009-01-20 16:17:43 +01001048 */
Corentin Charybe4ee822009-12-06 16:27:09 +01001049static void asus_input_notify(struct asus_laptop *asus, int event)
1050{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001051 if (asus->inputdev)
1052 sparse_keymap_report_event(asus->inputdev, event, 1, true);
Corentin Charybe4ee822009-12-06 16:27:09 +01001053}
1054
1055static int asus_input_init(struct asus_laptop *asus)
1056{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001057 struct input_dev *input;
1058 int error;
Corentin Charybe4ee822009-12-06 16:27:09 +01001059
Corentin Chary66a71dd2010-01-25 22:50:11 +01001060 input = input_allocate_device();
1061 if (!input) {
Corentin Charybe4ee822009-12-06 16:27:09 +01001062 pr_info("Unable to allocate input device\n");
1063 return 0;
1064 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001065 input->name = "Asus Laptop extra buttons";
1066 input->phys = ASUS_LAPTOP_FILE "/input0";
1067 input->id.bustype = BUS_HOST;
1068 input->dev.parent = &asus->platform_device->dev;
1069 input_set_drvdata(input, asus);
Corentin Charybe4ee822009-12-06 16:27:09 +01001070
Corentin Chary66a71dd2010-01-25 22:50:11 +01001071 error = sparse_keymap_setup(input, asus_keymap, NULL);
1072 if (error) {
1073 pr_err("Unable to setup input device keymap\n");
1074 goto err_keymap;
Corentin Charybe4ee822009-12-06 16:27:09 +01001075 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001076 error = input_register_device(input);
1077 if (error) {
Corentin Charybe4ee822009-12-06 16:27:09 +01001078 pr_info("Unable to register input device\n");
Corentin Chary66a71dd2010-01-25 22:50:11 +01001079 goto err_device;
Corentin Charybe4ee822009-12-06 16:27:09 +01001080 }
Corentin Chary66a71dd2010-01-25 22:50:11 +01001081
1082 asus->inputdev = input;
1083 return 0;
1084
1085err_keymap:
1086 sparse_keymap_free(input);
1087err_device:
1088 input_free_device(input);
1089 return error;
Corentin Charybe4ee822009-12-06 16:27:09 +01001090}
1091
1092static void asus_input_exit(struct asus_laptop *asus)
1093{
Corentin Chary66a71dd2010-01-25 22:50:11 +01001094 if (asus->inputdev) {
1095 sparse_keymap_free(asus->inputdev);
Corentin Charybe4ee822009-12-06 16:27:09 +01001096 input_unregister_device(asus->inputdev);
Corentin Chary66a71dd2010-01-25 22:50:11 +01001097 }
Corentin Charybe4ee822009-12-06 16:27:09 +01001098}
1099
1100/*
1101 * ACPI driver
1102 */
Corentin Chary600ad522009-11-30 21:42:42 +01001103static void asus_acpi_notify(struct acpi_device *device, u32 event)
Corentin Chary85091b72007-01-26 14:04:30 +01001104{
Corentin Chary9129d142009-12-01 22:39:41 +01001105 struct asus_laptop *asus = acpi_driver_data(device);
Corentin Chary6050c8d2009-02-15 19:30:19 +01001106 u16 count;
Corentin Chary034ce902009-01-20 16:17:43 +01001107
Corentin Chary6b7091e2007-01-26 14:04:45 +01001108 /*
1109 * We need to tell the backlight device when the backlight power is
1110 * switched
1111 */
Corentin Chary3e68ae72010-01-13 22:10:39 +01001112 if (event == ATKD_LCD_ON)
Corentin Chary9129d142009-12-01 22:39:41 +01001113 lcd_blank(asus, FB_BLANK_UNBLANK);
Corentin Chary3e68ae72010-01-13 22:10:39 +01001114 else if (event == ATKD_LCD_OFF)
Corentin Chary9129d142009-12-01 22:39:41 +01001115 lcd_blank(asus, FB_BLANK_POWERDOWN);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001116
Corentin Chary619d8b12009-11-28 10:35:37 +01001117 /* TODO Find a better way to handle events count. */
Corentin Chary50a90c42009-11-30 21:55:12 +01001118 count = asus->event_count[event % 128]++;
1119 acpi_bus_generate_proc_event(asus->device, event, count);
1120 acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
1121 dev_name(&asus->device->dev), event,
Corentin Chary6050c8d2009-02-15 19:30:19 +01001122 count);
Corentin Chary85091b72007-01-26 14:04:30 +01001123
Corentin Charybe4ee822009-12-06 16:27:09 +01001124 asus_input_notify(asus, event);
Corentin Chary85091b72007-01-26 14:04:30 +01001125}
1126
1127#define ASUS_CREATE_DEVICE_ATTR(_name) \
1128 struct device_attribute dev_attr_##_name = { \
1129 .attr = { \
1130 .name = __stringify(_name), \
Tejun Heo7b595752007-06-14 03:45:17 +09001131 .mode = 0 }, \
Corentin Chary85091b72007-01-26 14:04:30 +01001132 .show = NULL, \
1133 .store = NULL, \
1134 }
1135
1136#define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
1137 do { \
1138 dev_attr_##_name.attr.mode = _mode; \
1139 dev_attr_##_name.show = _show; \
1140 dev_attr_##_name.store = _store; \
1141 } while(0)
1142
1143static ASUS_CREATE_DEVICE_ATTR(infos);
Corentin Chary4564de12007-01-26 14:04:40 +01001144static ASUS_CREATE_DEVICE_ATTR(wlan);
1145static ASUS_CREATE_DEVICE_ATTR(bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +01001146static ASUS_CREATE_DEVICE_ATTR(display);
Corentin Chary722ad972007-01-26 14:04:55 +01001147static ASUS_CREATE_DEVICE_ATTR(ledd);
Corentin Chary8b857352007-01-26 14:04:58 +01001148static ASUS_CREATE_DEVICE_ATTR(ls_switch);
1149static ASUS_CREATE_DEVICE_ATTR(ls_level);
Corentin Charye539c2f2007-05-06 14:47:06 +02001150static ASUS_CREATE_DEVICE_ATTR(gps);
Corentin Chary85091b72007-01-26 14:04:30 +01001151
1152static struct attribute *asuspf_attributes[] = {
Len Brown8def05f2007-01-30 01:46:43 -05001153 &dev_attr_infos.attr,
1154 &dev_attr_wlan.attr,
1155 &dev_attr_bluetooth.attr,
1156 &dev_attr_display.attr,
1157 &dev_attr_ledd.attr,
1158 &dev_attr_ls_switch.attr,
1159 &dev_attr_ls_level.attr,
Corentin Charye539c2f2007-05-06 14:47:06 +02001160 &dev_attr_gps.attr,
Len Brown8def05f2007-01-30 01:46:43 -05001161 NULL
Corentin Chary85091b72007-01-26 14:04:30 +01001162};
1163
Corentin Chary600ad522009-11-30 21:42:42 +01001164static struct attribute_group platform_attribute_group = {
Len Brown8def05f2007-01-30 01:46:43 -05001165 .attrs = asuspf_attributes
Corentin Chary85091b72007-01-26 14:04:30 +01001166};
1167
Corentin Chary9129d142009-12-01 22:39:41 +01001168static int asus_platform_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001169{
1170 int result;
1171
Corentin Chary50a90c42009-11-30 21:55:12 +01001172 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1173 if (!asus->platform_device)
Corentin Chary600ad522009-11-30 21:42:42 +01001174 return -ENOMEM;
Corentin Chary9129d142009-12-01 22:39:41 +01001175 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001176
Corentin Chary50a90c42009-11-30 21:55:12 +01001177 result = platform_device_add(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001178 if (result)
1179 goto fail_platform_device;
1180
Corentin Chary50a90c42009-11-30 21:55:12 +01001181 result = sysfs_create_group(&asus->platform_device->dev.kobj,
Corentin Chary600ad522009-11-30 21:42:42 +01001182 &platform_attribute_group);
1183 if (result)
1184 goto fail_sysfs;
1185 return 0;
1186
1187fail_sysfs:
Corentin Chary50a90c42009-11-30 21:55:12 +01001188 platform_device_del(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001189fail_platform_device:
Corentin Chary50a90c42009-11-30 21:55:12 +01001190 platform_device_put(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001191 return result;
1192}
1193
Corentin Chary9129d142009-12-01 22:39:41 +01001194static void asus_platform_exit(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001195{
Corentin Chary50a90c42009-11-30 21:55:12 +01001196 sysfs_remove_group(&asus->platform_device->dev.kobj,
Corentin Chary600ad522009-11-30 21:42:42 +01001197 &platform_attribute_group);
Corentin Chary50a90c42009-11-30 21:55:12 +01001198 platform_device_unregister(asus->platform_device);
Corentin Chary600ad522009-11-30 21:42:42 +01001199}
1200
1201static struct platform_driver platform_driver = {
Len Brown8def05f2007-01-30 01:46:43 -05001202 .driver = {
Corentin Chary9129d142009-12-01 22:39:41 +01001203 .name = ASUS_LAPTOP_FILE,
1204 .owner = THIS_MODULE,
1205 }
Corentin Chary85091b72007-01-26 14:04:30 +01001206};
1207
Corentin Chary9129d142009-12-01 22:39:41 +01001208static void asus_laptop_add_fs(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001209{
1210 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
Corentin Chary4564de12007-01-26 14:04:40 +01001211
Corentin Charyd99b5772010-01-22 21:20:57 +01001212 if (!acpi_check_handle(asus->handle, METHOD_WLAN, NULL))
Corentin Chary4564de12007-01-26 14:04:40 +01001213 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
1214
Corentin Charyd99b5772010-01-22 21:20:57 +01001215 if (!acpi_check_handle(asus->handle, METHOD_BLUETOOTH, NULL))
Corentin Chary4564de12007-01-26 14:04:40 +01001216 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
1217 show_bluetooth, store_bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +01001218
Corentin Charyd99b5772010-01-22 21:20:57 +01001219 if (!acpi_check_handle(asus->handle, METHOD_SWITCH_DISPLAY, NULL)) {
1220 if (display_get_handle)
1221 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp,
1222 store_disp);
1223 else
1224 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
1225 }
Corentin Chary78127b42007-01-26 14:04:49 +01001226
Corentin Charyd99b5772010-01-22 21:20:57 +01001227 if (!acpi_check_handle(asus->handle, METHOD_LEDD, NULL))
Corentin Chary722ad972007-01-26 14:04:55 +01001228 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
1229
Corentin Charyd99b5772010-01-22 21:20:57 +01001230 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1231 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
Corentin Chary8b857352007-01-26 14:04:58 +01001232 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
1233 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
1234 }
Corentin Charye539c2f2007-05-06 14:47:06 +02001235
Corentin Charyd99b5772010-01-22 21:20:57 +01001236 if (!acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) &&
1237 !acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) &&
1238 !acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
Corentin Charye539c2f2007-05-06 14:47:06 +02001239 ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
Corentin Chary85091b72007-01-26 14:04:30 +01001240}
1241
Len Brown8def05f2007-01-30 01:46:43 -05001242static int asus_handle_init(char *name, acpi_handle * handle,
Corentin Chary85091b72007-01-26 14:04:30 +01001243 char **paths, int num_paths)
1244{
1245 int i;
1246 acpi_status status;
1247
1248 for (i = 0; i < num_paths; i++) {
1249 status = acpi_get_handle(NULL, paths[i], handle);
1250 if (ACPI_SUCCESS(status))
1251 return 0;
1252 }
1253
1254 *handle = NULL;
1255 return -ENODEV;
1256}
1257
1258#define ASUS_HANDLE_INIT(object) \
1259 asus_handle_init(#object, &object##_handle, object##_paths, \
1260 ARRAY_SIZE(object##_paths))
1261
Corentin Chary85091b72007-01-26 14:04:30 +01001262/*
Corentin Chary50a90c42009-11-30 21:55:12 +01001263 * This function is used to initialize the context with right values. In this
1264 * method, we can make all the detection we want, and modify the asus_laptop
1265 * struct
Corentin Chary85091b72007-01-26 14:04:30 +01001266 */
Corentin Chary7c247642009-11-30 22:13:54 +01001267static int asus_laptop_get_info(struct asus_laptop *asus)
Corentin Chary85091b72007-01-26 14:04:30 +01001268{
1269 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Corentin Chary85091b72007-01-26 14:04:30 +01001270 union acpi_object *model = NULL;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001271 unsigned long long bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +01001272 char *string = NULL;
1273 acpi_status status;
1274
1275 /*
1276 * Get DSDT headers early enough to allow for differentiating between
1277 * models, but late enough to allow acpi_bus_register_driver() to fail
1278 * before doing anything ACPI-specific. Should we encounter a machine,
1279 * which needs special handling (i.e. its hotkey device has a different
Corentin Chary7c247642009-11-30 22:13:54 +01001280 * HID), this bit will be moved.
Corentin Chary85091b72007-01-26 14:04:30 +01001281 */
Corentin Chary7c247642009-11-30 22:13:54 +01001282 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
Corentin Chary85091b72007-01-26 14:04:30 +01001283 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001284 pr_warning("Couldn't get the DSDT table header\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001285
1286 /* We have to write 0 on init this far for all ASUS models */
Corentin Chary50a90c42009-11-30 21:55:12 +01001287 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001288 pr_err("Hotkey initialization failed\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001289 return -ENODEV;
1290 }
1291
1292 /* This needs to be called for some laptops to init properly */
Corentin Chary9a816852007-03-11 10:25:38 +01001293 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001294 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
Corentin Chary9a816852007-03-11 10:25:38 +01001295 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001296 pr_warning("Error calling BSTS\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001297 else if (bsts_result)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001298 pr_notice("BSTS called, 0x%02x returned\n",
Corentin Chary9a816852007-03-11 10:25:38 +01001299 (uint) bsts_result);
Corentin Chary85091b72007-01-26 14:04:30 +01001300
Corentin Chary185e5af2007-03-11 10:27:33 +01001301 /* This too ... */
Corentin Charye5593bf2010-01-17 17:20:11 +01001302 if (write_acpi_int(asus->handle, "CWAP", wapf))
1303 pr_err("Error calling CWAP(%d)\n", wapf);
Corentin Chary85091b72007-01-26 14:04:30 +01001304 /*
1305 * Try to match the object returned by INIT to the specific model.
1306 * Handle every possible object (or the lack of thereof) the DSDT
1307 * writers might throw at us. When in trouble, we pass NULL to
1308 * asus_model_match() and try something completely different.
1309 */
1310 if (buffer.pointer) {
1311 model = buffer.pointer;
1312 switch (model->type) {
1313 case ACPI_TYPE_STRING:
1314 string = model->string.pointer;
1315 break;
1316 case ACPI_TYPE_BUFFER:
1317 string = model->buffer.pointer;
1318 break;
1319 default:
1320 string = "";
1321 break;
1322 }
1323 }
Corentin Chary50a90c42009-11-30 21:55:12 +01001324 asus->name = kstrdup(string, GFP_KERNEL);
1325 if (!asus->name)
Corentin Chary85091b72007-01-26 14:04:30 +01001326 return -ENOMEM;
1327
Len Brown8def05f2007-01-30 01:46:43 -05001328 if (*string)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001329 pr_notice(" %s model detected\n", string);
Corentin Chary85091b72007-01-26 14:04:30 +01001330
Corentin Chary4564de12007-01-26 14:04:40 +01001331 /*
1332 * The HWRS method return informations about the hardware.
1333 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1334 * The significance of others is yet to be found.
Corentin Chary4564de12007-01-26 14:04:40 +01001335 */
Corentin Chary9a816852007-03-11 10:25:38 +01001336 status =
Corentin Chary50a90c42009-11-30 21:55:12 +01001337 acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
Corentin Charyd99b5772010-01-22 21:20:57 +01001338 if (!ACPI_FAILURE(status))
1339 pr_notice(" HRWS returned %x", (int)hwrs_result);
Corentin Chary4564de12007-01-26 14:04:40 +01001340
Corentin Charyd99b5772010-01-22 21:20:57 +01001341 if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
Corentin Charyaa9df932010-01-13 21:49:10 +01001342 asus->have_rsts = true;
Corentin Chary4564de12007-01-26 14:04:40 +01001343
Corentin Charyd99b5772010-01-22 21:20:57 +01001344 /* Scheduled for removal */
Corentin Chary6b7091e2007-01-26 14:04:45 +01001345 ASUS_HANDLE_INIT(lcd_switch);
Corentin Chary78127b42007-01-26 14:04:49 +01001346 ASUS_HANDLE_INIT(display_get);
1347
Corentin Chary85091b72007-01-26 14:04:30 +01001348 kfree(model);
1349
1350 return AE_OK;
1351}
1352
Corentin Chary600ad522009-11-30 21:42:42 +01001353static bool asus_device_present;
1354
Corentin Chary9129d142009-12-01 22:39:41 +01001355static int __devinit asus_acpi_init(struct asus_laptop *asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001356{
1357 int result = 0;
1358
Corentin Chary50a90c42009-11-30 21:55:12 +01001359 result = acpi_bus_get_status(asus->device);
Corentin Chary600ad522009-11-30 21:42:42 +01001360 if (result)
1361 return result;
Corentin Chary50a90c42009-11-30 21:55:12 +01001362 if (!asus->device->status.present) {
Corentin Chary600ad522009-11-30 21:42:42 +01001363 pr_err("Hotkey device not present, aborting\n");
1364 return -ENODEV;
1365 }
1366
Corentin Chary7c247642009-11-30 22:13:54 +01001367 result = asus_laptop_get_info(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001368 if (result)
1369 return result;
1370
Corentin Chary9129d142009-12-01 22:39:41 +01001371 asus_laptop_add_fs(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001372
1373 /* WLED and BLED are on by default */
Corentin Charybe4ee822009-12-06 16:27:09 +01001374 if (bluetooth_status >= 0)
Corentin Charye5593bf2010-01-17 17:20:11 +01001375 asus_bluetooth_set(asus, !!bluetooth_status);
1376
Corentin Charyd0930a22010-01-17 17:21:13 +01001377 if (wlan_status >= 0)
1378 asus_wlan_set(asus, !!wlan_status);
Corentin Chary600ad522009-11-30 21:42:42 +01001379
1380 /* Keyboard Backlight is on by default */
Corentin Charyd99b5772010-01-22 21:20:57 +01001381 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
Corentin Chary4d441512010-01-13 22:26:24 +01001382 asus_kled_set(asus, 1);
Corentin Chary600ad522009-11-30 21:42:42 +01001383
1384 /* LED display is off by default */
Corentin Chary50a90c42009-11-30 21:55:12 +01001385 asus->ledd_status = 0xFFF;
Corentin Chary600ad522009-11-30 21:42:42 +01001386
1387 /* Set initial values of light sensor and level */
Corentin Charybe4ee822009-12-06 16:27:09 +01001388 asus->light_switch = 0; /* Default to light sensor disabled */
1389 asus->light_level = 5; /* level 5 for sensor sensitivity */
Corentin Chary600ad522009-11-30 21:42:42 +01001390
Corentin Charyd99b5772010-01-22 21:20:57 +01001391 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1392 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
Corentin Chary4d441512010-01-13 22:26:24 +01001393 asus_als_switch(asus, asus->light_switch);
Corentin Chary4d441512010-01-13 22:26:24 +01001394 asus_als_level(asus, asus->light_level);
Corentin Charyd99b5772010-01-22 21:20:57 +01001395 }
Corentin Chary600ad522009-11-30 21:42:42 +01001396
1397 /* GPS is on by default */
Corentin Charyd99b5772010-01-22 21:20:57 +01001398 if (!acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) &&
1399 !acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) &&
1400 !acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
1401 asus_gps_switch(asus, 1);
Corentin Chary600ad522009-11-30 21:42:42 +01001402 return result;
1403}
1404
1405static int __devinit asus_acpi_add(struct acpi_device *device)
1406{
Corentin Chary9129d142009-12-01 22:39:41 +01001407 struct asus_laptop *asus;
Corentin Chary600ad522009-11-30 21:42:42 +01001408 int result;
1409
1410 pr_notice("Asus Laptop Support version %s\n",
1411 ASUS_LAPTOP_VERSION);
Corentin Chary50a90c42009-11-30 21:55:12 +01001412 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1413 if (!asus)
Corentin Chary600ad522009-11-30 21:42:42 +01001414 return -ENOMEM;
Corentin Chary50a90c42009-11-30 21:55:12 +01001415 asus->handle = device->handle;
1416 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1417 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1418 device->driver_data = asus;
1419 asus->device = device;
Corentin Chary600ad522009-11-30 21:42:42 +01001420
Corentin Chary9129d142009-12-01 22:39:41 +01001421 result = asus_acpi_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001422 if (result)
1423 goto fail_platform;
1424
1425 /*
1426 * Register the platform device first. It is used as a parent for the
1427 * sub-devices below.
1428 */
Corentin Chary9129d142009-12-01 22:39:41 +01001429 result = asus_platform_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001430 if (result)
1431 goto fail_platform;
1432
1433 if (!acpi_video_backlight_support()) {
Corentin Chary9129d142009-12-01 22:39:41 +01001434 result = asus_backlight_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001435 if (result)
1436 goto fail_backlight;
1437 } else
1438 pr_info("Backlight controlled by ACPI video driver\n");
1439
Corentin Chary9129d142009-12-01 22:39:41 +01001440 result = asus_input_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001441 if (result)
1442 goto fail_input;
1443
Corentin Chary9129d142009-12-01 22:39:41 +01001444 result = asus_led_init(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001445 if (result)
1446 goto fail_led;
1447
1448 asus_device_present = true;
1449 return 0;
1450
1451fail_led:
Corentin Chary9129d142009-12-01 22:39:41 +01001452 asus_input_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001453fail_input:
Corentin Chary9129d142009-12-01 22:39:41 +01001454 asus_backlight_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001455fail_backlight:
Corentin Chary9129d142009-12-01 22:39:41 +01001456 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001457fail_platform:
Corentin Chary50a90c42009-11-30 21:55:12 +01001458 kfree(asus->name);
1459 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001460
1461 return result;
1462}
1463
1464static int asus_acpi_remove(struct acpi_device *device, int type)
1465{
Corentin Chary9129d142009-12-01 22:39:41 +01001466 struct asus_laptop *asus = acpi_driver_data(device);
1467
1468 asus_backlight_exit(asus);
1469 asus_led_exit(asus);
1470 asus_input_exit(asus);
1471 asus_platform_exit(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001472
Corentin Chary50a90c42009-11-30 21:55:12 +01001473 kfree(asus->name);
1474 kfree(asus);
Corentin Chary600ad522009-11-30 21:42:42 +01001475 return 0;
1476}
1477
1478static const struct acpi_device_id asus_device_ids[] = {
1479 {"ATK0100", 0},
1480 {"ATK0101", 0},
1481 {"", 0},
1482};
1483MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1484
1485static struct acpi_driver asus_acpi_driver = {
Corentin Chary50a90c42009-11-30 21:55:12 +01001486 .name = ASUS_LAPTOP_NAME,
1487 .class = ASUS_LAPTOP_CLASS,
Corentin Chary600ad522009-11-30 21:42:42 +01001488 .owner = THIS_MODULE,
1489 .ids = asus_device_ids,
1490 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1491 .ops = {
1492 .add = asus_acpi_add,
1493 .remove = asus_acpi_remove,
1494 .notify = asus_acpi_notify,
1495 },
1496};
1497
Corentin Chary85091b72007-01-26 14:04:30 +01001498static int __init asus_laptop_init(void)
1499{
1500 int result;
1501
Corentin Chary600ad522009-11-30 21:42:42 +01001502 result = platform_driver_register(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001503 if (result < 0)
1504 return result;
1505
Corentin Chary600ad522009-11-30 21:42:42 +01001506 result = acpi_bus_register_driver(&asus_acpi_driver);
1507 if (result < 0)
1508 goto fail_acpi_driver;
1509 if (!asus_device_present) {
1510 result = -ENODEV;
1511 goto fail_no_device;
Corentin Chary85091b72007-01-26 14:04:30 +01001512 }
Len Brown8def05f2007-01-30 01:46:43 -05001513 return 0;
Corentin Chary85091b72007-01-26 14:04:30 +01001514
Corentin Chary600ad522009-11-30 21:42:42 +01001515fail_no_device:
1516 acpi_bus_unregister_driver(&asus_acpi_driver);
1517fail_acpi_driver:
1518 platform_driver_unregister(&platform_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001519 return result;
1520}
1521
Corentin Chary600ad522009-11-30 21:42:42 +01001522static void __exit asus_laptop_exit(void)
1523{
1524 acpi_bus_unregister_driver(&asus_acpi_driver);
1525 platform_driver_unregister(&platform_driver);
1526}
1527
Corentin Chary85091b72007-01-26 14:04:30 +01001528module_init(asus_laptop_init);
1529module_exit(asus_laptop_exit);