blob: 3a36a294e91b2ce731586950edb4ac5d4e9da8b2 [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 Chary85091b72007-01-26 14:04:30 +010036#include <linux/kernel.h>
37#include <linux/module.h>
38#include <linux/init.h>
39#include <linux/types.h>
40#include <linux/err.h>
41#include <linux/proc_fs.h>
Corentin Chary6b7091e2007-01-26 14:04:45 +010042#include <linux/backlight.h>
43#include <linux/fb.h>
Corentin Charybe18cda2007-01-26 14:04:35 +010044#include <linux/leds.h>
Corentin Chary85091b72007-01-26 14:04:30 +010045#include <linux/platform_device.h>
46#include <acpi/acpi_drivers.h>
47#include <acpi/acpi_bus.h>
48#include <asm/uaccess.h>
49
Corentin Charyf3985322007-05-06 14:48:22 +020050#define ASUS_LAPTOP_VERSION "0.42"
Corentin Chary85091b72007-01-26 14:04:30 +010051
52#define ASUS_HOTK_NAME "Asus Laptop Support"
53#define ASUS_HOTK_CLASS "hotkey"
54#define ASUS_HOTK_DEVICE_NAME "Hotkey"
Corentin Chary85091b72007-01-26 14:04:30 +010055#define ASUS_HOTK_FILE "asus-laptop"
56#define ASUS_HOTK_PREFIX "\\_SB.ATKD."
57
Corentin Charybe18cda2007-01-26 14:04:35 +010058/*
Corentin Chary6b7091e2007-01-26 14:04:45 +010059 * Some events we use, same for all Asus
60 */
61#define ATKD_BR_UP 0x10
62#define ATKD_BR_DOWN 0x20
63#define ATKD_LCD_ON 0x33
64#define ATKD_LCD_OFF 0x34
65
66/*
Corentin Chary4564de12007-01-26 14:04:40 +010067 * Known bits returned by \_SB.ATKD.HWRS
Corentin Charybe18cda2007-01-26 14:04:35 +010068 */
Corentin Chary4564de12007-01-26 14:04:40 +010069#define WL_HWRS 0x80
70#define BT_HWRS 0x100
71
72/*
73 * Flags for hotk status
74 * WL_ON and BT_ON are also used for wireless_status()
75 */
76#define WL_ON 0x01 //internal Wifi
77#define BT_ON 0x02 //internal Bluetooth
Corentin Charybe18cda2007-01-26 14:04:35 +010078#define MLED_ON 0x04 //mail LED
79#define TLED_ON 0x08 //touchpad LED
Len Brown8def05f2007-01-30 01:46:43 -050080#define RLED_ON 0x10 //Record LED
81#define PLED_ON 0x20 //Phone LED
Corentin Charyfdd8d082007-03-11 10:26:48 +010082#define GLED_ON 0x40 //Gaming LED
83#define LCD_ON 0x80 //LCD backlight
Corentin Charyf3985322007-05-06 14:48:22 +020084#define GPS_ON 0x100 //GPS
Corentin Charybe18cda2007-01-26 14:04:35 +010085
Corentin Chary85091b72007-01-26 14:04:30 +010086#define ASUS_LOG ASUS_HOTK_FILE ": "
87#define ASUS_ERR KERN_ERR ASUS_LOG
88#define ASUS_WARNING KERN_WARNING ASUS_LOG
89#define ASUS_NOTICE KERN_NOTICE ASUS_LOG
90#define ASUS_INFO KERN_INFO ASUS_LOG
91#define ASUS_DEBUG KERN_DEBUG ASUS_LOG
92
93MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
94MODULE_DESCRIPTION(ASUS_HOTK_NAME);
95MODULE_LICENSE("GPL");
96
Corentin Chary185e5af2007-03-11 10:27:33 +010097/* WAPF defines the behavior of the Fn+Fx wlan key
98 * The significance of values is yet to be found, but
99 * most of the time:
100 * 0x0 will do nothing
101 * 0x1 will allow to control the device with Fn+Fx key.
102 * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
103 * 0x5 like 0x1 or 0x4
104 * So, if something doesn't work as you want, just try other values =)
105 */
106static uint wapf = 1;
107module_param(wapf, uint, 0644);
108MODULE_PARM_DESC(wapf, "WAPF value");
109
Corentin Chary85091b72007-01-26 14:04:30 +0100110#define ASUS_HANDLE(object, paths...) \
111 static acpi_handle object##_handle = NULL; \
112 static char *object##_paths[] = { paths }
113
Corentin Charybe18cda2007-01-26 14:04:35 +0100114/* LED */
115ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
116ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
Len Brown8def05f2007-01-30 01:46:43 -0500117ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
118ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
Corentin Charyfdd8d082007-03-11 10:26:48 +0100119ASUS_HANDLE(gled_set, ASUS_HOTK_PREFIX "GLED"); /* G1, G2 (probably) */
Corentin Charybe18cda2007-01-26 14:04:35 +0100120
Corentin Chary722ad972007-01-26 14:04:55 +0100121/* LEDD */
122ASUS_HANDLE(ledd_set, ASUS_HOTK_PREFIX "SLCM");
123
Corentin Chary4564de12007-01-26 14:04:40 +0100124/* Bluetooth and WLAN
125 * WLED and BLED are not handled like other XLED, because in some dsdt
126 * they also control the WLAN/Bluetooth device.
127 */
128ASUS_HANDLE(wl_switch, ASUS_HOTK_PREFIX "WLED");
129ASUS_HANDLE(bt_switch, ASUS_HOTK_PREFIX "BLED");
Len Brown8def05f2007-01-30 01:46:43 -0500130ASUS_HANDLE(wireless_status, ASUS_HOTK_PREFIX "RSTS"); /* All new models */
Corentin Chary4564de12007-01-26 14:04:40 +0100131
Corentin Chary6b7091e2007-01-26 14:04:45 +0100132/* Brightness */
133ASUS_HANDLE(brightness_set, ASUS_HOTK_PREFIX "SPLV");
134ASUS_HANDLE(brightness_get, ASUS_HOTK_PREFIX "GPLV");
135
136/* Backlight */
Len Brown8def05f2007-01-30 01:46:43 -0500137ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
138 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
139 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
140 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
141 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
142 "\\_SB.PCI0.PX40.Q10", /* S1x */
143 "\\Q10"); /* A2x, L2D, L3D, M2E */
Corentin Chary6b7091e2007-01-26 14:04:45 +0100144
Corentin Chary78127b42007-01-26 14:04:49 +0100145/* Display */
146ASUS_HANDLE(display_set, ASUS_HOTK_PREFIX "SDSP");
Len Brown8def05f2007-01-30 01:46:43 -0500147ASUS_HANDLE(display_get, "\\_SB.PCI0.P0P1.VGA.GETD", /* A6B, A6K A6R A7D F3JM L4R M6R A3G
148 M6A M6V VX-1 V6J V6V W3Z */
149 "\\_SB.PCI0.P0P2.VGA.GETD", /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V
Corentin Charyf3985322007-05-06 14:48:22 +0200150 S5A M5A z33A W1Jc W2V G1 */
Len Brown8def05f2007-01-30 01:46:43 -0500151 "\\_SB.PCI0.P0P3.VGA.GETD", /* A6V A6Q */
152 "\\_SB.PCI0.P0PA.VGA.GETD", /* A6T, A6M */
153 "\\_SB.PCI0.PCI1.VGAC.NMAP", /* L3C */
154 "\\_SB.PCI0.VGA.GETD", /* Z96F */
155 "\\ACTD", /* A2D */
156 "\\ADVG", /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
157 "\\DNXT", /* P30 */
158 "\\INFB", /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
159 "\\SSTE"); /* A3F A6F A3N A3L M6N W3N W6A */
Corentin Chary78127b42007-01-26 14:04:49 +0100160
Len Brown8def05f2007-01-30 01:46:43 -0500161ASUS_HANDLE(ls_switch, ASUS_HOTK_PREFIX "ALSC"); /* Z71A Z71V */
162ASUS_HANDLE(ls_level, ASUS_HOTK_PREFIX "ALSL"); /* Z71A Z71V */
Corentin Chary8b857352007-01-26 14:04:58 +0100163
Corentin Charye539c2f2007-05-06 14:47:06 +0200164/* GPS */
165/* R2H use different handle for GPS on/off */
Corentin Charyf3985322007-05-06 14:48:22 +0200166ASUS_HANDLE(gps_on, ASUS_HOTK_PREFIX "SDON"); /* R2H */
167ASUS_HANDLE(gps_off, ASUS_HOTK_PREFIX "SDOF"); /* R2H */
Corentin Charye539c2f2007-05-06 14:47:06 +0200168ASUS_HANDLE(gps_status, ASUS_HOTK_PREFIX "GPST");
169
Corentin Chary85091b72007-01-26 14:04:30 +0100170/*
171 * This is the main structure, we can use it to store anything interesting
172 * about the hotk device
173 */
174struct asus_hotk {
Len Brown8def05f2007-01-30 01:46:43 -0500175 char *name; //laptop name
Corentin Chary85091b72007-01-26 14:04:30 +0100176 struct acpi_device *device; //the device we are in
177 acpi_handle handle; //the handle of the hotk device
178 char status; //status of the hotk, for LEDs, ...
Corentin Chary722ad972007-01-26 14:04:55 +0100179 u32 ledd_status; //status of the LED display
Len Brown8def05f2007-01-30 01:46:43 -0500180 u8 light_level; //light sensor level
181 u8 light_switch; //light sensor switch value
Corentin Chary85091b72007-01-26 14:04:30 +0100182 u16 event_count[128]; //count for each event TODO make this better
183};
184
185/*
186 * This header is made available to allow proper configuration given model,
187 * revision number , ... this info cannot go in struct asus_hotk because it is
188 * available before the hotk
189 */
190static struct acpi_table_header *asus_info;
191
192/* The actual device the driver binds to */
193static struct asus_hotk *hotk;
194
195/*
196 * The hotkey driver declaration
197 */
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200198static const struct acpi_device_id asus_device_ids[] = {
199 {"ATK0100", 0},
200 {"", 0},
201};
202MODULE_DEVICE_TABLE(acpi, asus_device_ids);
203
Corentin Chary85091b72007-01-26 14:04:30 +0100204static int asus_hotk_add(struct acpi_device *device);
205static int asus_hotk_remove(struct acpi_device *device, int type);
206static struct acpi_driver asus_hotk_driver = {
207 .name = ASUS_HOTK_NAME,
208 .class = ASUS_HOTK_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200209 .ids = asus_device_ids,
Corentin Chary85091b72007-01-26 14:04:30 +0100210 .ops = {
211 .add = asus_hotk_add,
212 .remove = asus_hotk_remove,
213 },
214};
215
Corentin Chary6b7091e2007-01-26 14:04:45 +0100216/* The backlight device /sys/class/backlight */
217static struct backlight_device *asus_backlight_device;
218
219/*
220 * The backlight class declaration
221 */
222static int read_brightness(struct backlight_device *bd);
223static int update_bl_status(struct backlight_device *bd);
Richard Purdie599a52d2007-02-10 23:07:48 +0000224static struct backlight_ops asusbl_ops = {
Len Brown8def05f2007-01-30 01:46:43 -0500225 .get_brightness = read_brightness,
226 .update_status = update_bl_status,
Corentin Chary6b7091e2007-01-26 14:04:45 +0100227};
228
Corentin Charybe18cda2007-01-26 14:04:35 +0100229/* These functions actually update the LED's, and are called from a
230 * workqueue. By doing this as separate work rather than when the LED
231 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
232 * potentially bad time, such as a timer interrupt. */
233static struct workqueue_struct *led_workqueue;
234
235#define ASUS_LED(object, ledname) \
236 static void object##_led_set(struct led_classdev *led_cdev, \
237 enum led_brightness value); \
238 static void object##_led_update(struct work_struct *ignored); \
239 static int object##_led_wk; \
Adrian Bunkf110ef52007-02-20 01:07:25 +0100240 static DECLARE_WORK(object##_led_work, object##_led_update); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100241 static struct led_classdev object##_led = { \
Richard Purdie6c152be2007-10-31 15:00:07 +0100242 .name = "asus::" ledname, \
Corentin Charybe18cda2007-01-26 14:04:35 +0100243 .brightness_set = object##_led_set, \
244 }
245
246ASUS_LED(mled, "mail");
247ASUS_LED(tled, "touchpad");
248ASUS_LED(rled, "record");
249ASUS_LED(pled, "phone");
Corentin Charyfdd8d082007-03-11 10:26:48 +0100250ASUS_LED(gled, "gaming");
Corentin Charybe18cda2007-01-26 14:04:35 +0100251
Corentin Chary85091b72007-01-26 14:04:30 +0100252/*
253 * This function evaluates an ACPI method, given an int as parameter, the
254 * method is searched within the scope of the handle, can be NULL. The output
255 * of the method is written is output, which can also be NULL
256 *
257 * returns 1 if write is successful, 0 else.
258 */
259static int write_acpi_int(acpi_handle handle, const char *method, int val,
260 struct acpi_buffer *output)
261{
262 struct acpi_object_list params; //list of input parameters (an int here)
263 union acpi_object in_obj; //the only param we use
264 acpi_status status;
265
266 params.count = 1;
267 params.pointer = &in_obj;
268 in_obj.type = ACPI_TYPE_INTEGER;
269 in_obj.integer.value = val;
270
271 status = acpi_evaluate_object(handle, (char *)method, &params, output);
272 return (status == AE_OK);
273}
274
Len Brown8def05f2007-01-30 01:46:43 -0500275static int read_wireless_status(int mask)
276{
Corentin Chary9a816852007-03-11 10:25:38 +0100277 ulong status;
278 acpi_status rv = AE_OK;
Corentin Chary4564de12007-01-26 14:04:40 +0100279
280 if (!wireless_status_handle)
281 return (hotk->status & mask) ? 1 : 0;
282
Corentin Chary9a816852007-03-11 10:25:38 +0100283 rv = acpi_evaluate_integer(wireless_status_handle, NULL, NULL, &status);
284 if (ACPI_FAILURE(rv))
Corentin Chary4564de12007-01-26 14:04:40 +0100285 printk(ASUS_WARNING "Error reading Wireless status\n");
Corentin Chary9a816852007-03-11 10:25:38 +0100286 else
287 return (status & mask) ? 1 : 0;
Corentin Chary4564de12007-01-26 14:04:40 +0100288
289 return (hotk->status & mask) ? 1 : 0;
290}
291
Corentin Charyf3985322007-05-06 14:48:22 +0200292static int read_gps_status(void)
293{
294 ulong status;
Corentin Charye539c2f2007-05-06 14:47:06 +0200295 acpi_status rv = AE_OK;
296
297 rv = acpi_evaluate_integer(gps_status_handle, NULL, NULL, &status);
298 if (ACPI_FAILURE(rv))
Corentin Charyf3985322007-05-06 14:48:22 +0200299 printk(ASUS_WARNING "Error reading GPS status\n");
Corentin Charye539c2f2007-05-06 14:47:06 +0200300 else
Corentin Charyf3985322007-05-06 14:48:22 +0200301 return status ? 1 : 0;
Corentin Charye539c2f2007-05-06 14:47:06 +0200302
Corentin Charyf3985322007-05-06 14:48:22 +0200303 return (hotk->status & GPS_ON) ? 1 : 0;
Corentin Charye539c2f2007-05-06 14:47:06 +0200304}
305
Corentin Charybe18cda2007-01-26 14:04:35 +0100306/* Generic LED functions */
307static int read_status(int mask)
308{
Corentin Chary4564de12007-01-26 14:04:40 +0100309 /* There is a special method for both wireless devices */
310 if (mask == BT_ON || mask == WL_ON)
311 return read_wireless_status(mask);
Corentin Charyf3985322007-05-06 14:48:22 +0200312 else if (mask == GPS_ON)
313 return read_gps_status();
Corentin Chary4564de12007-01-26 14:04:40 +0100314
Corentin Charybe18cda2007-01-26 14:04:35 +0100315 return (hotk->status & mask) ? 1 : 0;
316}
317
Corentin Chary935ffee2007-03-11 10:26:12 +0100318static void write_status(acpi_handle handle, int out, int mask)
Corentin Charybe18cda2007-01-26 14:04:35 +0100319{
320 hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
321
Corentin Chary935ffee2007-03-11 10:26:12 +0100322 switch (mask) {
323 case MLED_ON:
Corentin Charybe18cda2007-01-26 14:04:35 +0100324 out = !out & 0x1;
Corentin Chary935ffee2007-03-11 10:26:12 +0100325 break;
Corentin Charyfdd8d082007-03-11 10:26:48 +0100326 case GLED_ON:
327 out = (out & 0x1) + 1;
328 break;
Corentin Charye539c2f2007-05-06 14:47:06 +0200329 case GPS_ON:
330 handle = (out) ? gps_on_handle : gps_off_handle;
331 out = 0x02;
332 break;
Corentin Chary935ffee2007-03-11 10:26:12 +0100333 default:
334 out &= 0x1;
335 break;
336 }
Corentin Charybe18cda2007-01-26 14:04:35 +0100337
338 if (handle && !write_acpi_int(handle, NULL, out, NULL))
Corentin Chary935ffee2007-03-11 10:26:12 +0100339 printk(ASUS_WARNING " write failed %x\n", mask);
Corentin Charybe18cda2007-01-26 14:04:35 +0100340}
341
342/* /sys/class/led handlers */
Corentin Chary935ffee2007-03-11 10:26:12 +0100343#define ASUS_LED_HANDLER(object, mask) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100344 static void object##_led_set(struct led_classdev *led_cdev, \
345 enum led_brightness value) \
346 { \
347 object##_led_wk = value; \
348 queue_work(led_workqueue, &object##_led_work); \
349 } \
350 static void object##_led_update(struct work_struct *ignored) \
351 { \
352 int value = object##_led_wk; \
Corentin Chary935ffee2007-03-11 10:26:12 +0100353 write_status(object##_set_handle, value, (mask)); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100354 }
355
Corentin Chary935ffee2007-03-11 10:26:12 +0100356ASUS_LED_HANDLER(mled, MLED_ON);
357ASUS_LED_HANDLER(pled, PLED_ON);
358ASUS_LED_HANDLER(rled, RLED_ON);
359ASUS_LED_HANDLER(tled, TLED_ON);
Corentin Charyfdd8d082007-03-11 10:26:48 +0100360ASUS_LED_HANDLER(gled, GLED_ON);
Corentin Charybe18cda2007-01-26 14:04:35 +0100361
Corentin Chary6b7091e2007-01-26 14:04:45 +0100362static int get_lcd_state(void)
363{
364 return read_status(LCD_ON);
365}
366
367static int set_lcd_state(int value)
368{
369 int lcd = 0;
370 acpi_status status = 0;
371
372 lcd = value ? 1 : 0;
373
374 if (lcd == get_lcd_state())
375 return 0;
376
Len Brown8def05f2007-01-30 01:46:43 -0500377 if (lcd_switch_handle) {
Corentin Chary6b7091e2007-01-26 14:04:45 +0100378 status = acpi_evaluate_object(lcd_switch_handle,
379 NULL, NULL, NULL);
380
381 if (ACPI_FAILURE(status))
382 printk(ASUS_WARNING "Error switching LCD\n");
383 }
384
Corentin Chary935ffee2007-03-11 10:26:12 +0100385 write_status(NULL, lcd, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100386 return 0;
387}
388
389static void lcd_blank(int blank)
390{
391 struct backlight_device *bd = asus_backlight_device;
392
Len Brown8def05f2007-01-30 01:46:43 -0500393 if (bd) {
Richard Purdie599a52d2007-02-10 23:07:48 +0000394 bd->props.power = blank;
Richard Purdie28ee0862007-02-08 22:25:09 +0000395 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100396 }
397}
398
399static int read_brightness(struct backlight_device *bd)
400{
Corentin Chary9a816852007-03-11 10:25:38 +0100401 ulong value;
402 acpi_status rv = AE_OK;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100403
Corentin Chary9a816852007-03-11 10:25:38 +0100404 rv = acpi_evaluate_integer(brightness_get_handle, NULL, NULL, &value);
405 if (ACPI_FAILURE(rv))
Corentin Chary6b7091e2007-01-26 14:04:45 +0100406 printk(ASUS_WARNING "Error reading brightness\n");
407
408 return value;
409}
410
411static int set_brightness(struct backlight_device *bd, int value)
412{
413 int ret = 0;
414
415 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
416 /* 0 <= value <= 15 */
417
418 if (!write_acpi_int(brightness_set_handle, NULL, value, NULL)) {
419 printk(ASUS_WARNING "Error changing brightness\n");
420 ret = -EIO;
421 }
422
423 return ret;
424}
425
426static int update_bl_status(struct backlight_device *bd)
427{
428 int rv;
Richard Purdie599a52d2007-02-10 23:07:48 +0000429 int value = bd->props.brightness;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100430
431 rv = set_brightness(bd, value);
Len Brown8def05f2007-01-30 01:46:43 -0500432 if (rv)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100433 return rv;
434
Richard Purdie599a52d2007-02-10 23:07:48 +0000435 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100436 return set_lcd_state(value);
437}
438
Corentin Chary85091b72007-01-26 14:04:30 +0100439/*
440 * Platform device handlers
441 */
442
443/*
444 * We write our info in page, we begin at offset off and cannot write more
445 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
446 * number of bytes written in page
447 */
448static ssize_t show_infos(struct device *dev,
Len Brown8def05f2007-01-30 01:46:43 -0500449 struct device_attribute *attr, char *page)
Corentin Chary85091b72007-01-26 14:04:30 +0100450{
451 int len = 0;
Corentin Chary9a816852007-03-11 10:25:38 +0100452 ulong temp;
Corentin Chary85091b72007-01-26 14:04:30 +0100453 char buf[16]; //enough for all info
Corentin Chary9a816852007-03-11 10:25:38 +0100454 acpi_status rv = AE_OK;
455
Corentin Chary85091b72007-01-26 14:04:30 +0100456 /*
457 * We use the easy way, we don't care of off and count, so we don't set eof
458 * to 1
459 */
460
461 len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
462 len += sprintf(page + len, "Model reference : %s\n", hotk->name);
463 /*
464 * The SFUN method probably allows the original driver to get the list
465 * of features supported by a given model. For now, 0x0100 or 0x0800
466 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
467 * The significance of others is yet to be found.
468 */
Corentin Chary9a816852007-03-11 10:25:38 +0100469 rv = acpi_evaluate_integer(hotk->handle, "SFUN", NULL, &temp);
470 if (!ACPI_FAILURE(rv))
471 len += sprintf(page + len, "SFUN value : 0x%04x\n",
472 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100473 /*
474 * Another value for userspace: the ASYM method returns 0x02 for
475 * battery low and 0x04 for battery critical, its readings tend to be
476 * more accurate than those provided by _BST.
477 * Note: since not all the laptops provide this method, errors are
478 * silently ignored.
479 */
Corentin Chary9a816852007-03-11 10:25:38 +0100480 rv = acpi_evaluate_integer(hotk->handle, "ASYM", NULL, &temp);
481 if (!ACPI_FAILURE(rv))
482 len += sprintf(page + len, "ASYM value : 0x%04x\n",
483 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100484 if (asus_info) {
485 snprintf(buf, 16, "%d", asus_info->length);
486 len += sprintf(page + len, "DSDT length : %s\n", buf);
487 snprintf(buf, 16, "%d", asus_info->checksum);
488 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
489 snprintf(buf, 16, "%d", asus_info->revision);
490 len += sprintf(page + len, "DSDT revision : %s\n", buf);
491 snprintf(buf, 7, "%s", asus_info->oem_id);
492 len += sprintf(page + len, "OEM id : %s\n", buf);
493 snprintf(buf, 9, "%s", asus_info->oem_table_id);
494 len += sprintf(page + len, "OEM table id : %s\n", buf);
495 snprintf(buf, 16, "%x", asus_info->oem_revision);
496 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
497 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
498 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
499 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
500 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
501 }
502
503 return len;
504}
505
506static int parse_arg(const char *buf, unsigned long count, int *val)
507{
508 if (!count)
509 return 0;
510 if (count > 31)
511 return -EINVAL;
512 if (sscanf(buf, "%i", val) != 1)
513 return -EINVAL;
514 return count;
515}
516
Corentin Chary4564de12007-01-26 14:04:40 +0100517static ssize_t store_status(const char *buf, size_t count,
Corentin Chary935ffee2007-03-11 10:26:12 +0100518 acpi_handle handle, int mask)
Corentin Chary4564de12007-01-26 14:04:40 +0100519{
520 int rv, value;
521 int out = 0;
522
523 rv = parse_arg(buf, count, &value);
524 if (rv > 0)
525 out = value ? 1 : 0;
526
Corentin Chary935ffee2007-03-11 10:26:12 +0100527 write_status(handle, out, mask);
Corentin Chary4564de12007-01-26 14:04:40 +0100528
529 return rv;
530}
531
532/*
Corentin Chary722ad972007-01-26 14:04:55 +0100533 * LEDD display
534 */
535static ssize_t show_ledd(struct device *dev,
536 struct device_attribute *attr, char *buf)
537{
538 return sprintf(buf, "0x%08x\n", hotk->ledd_status);
539}
540
541static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
542 const char *buf, size_t count)
543{
544 int rv, value;
545
546 rv = parse_arg(buf, count, &value);
547 if (rv > 0) {
548 if (!write_acpi_int(ledd_set_handle, NULL, value, NULL))
549 printk(ASUS_WARNING "LED display write failed\n");
550 else
551 hotk->ledd_status = (u32) value;
552 }
553 return rv;
554}
555
556/*
Corentin Chary4564de12007-01-26 14:04:40 +0100557 * WLAN
558 */
559static ssize_t show_wlan(struct device *dev,
560 struct device_attribute *attr, char *buf)
561{
562 return sprintf(buf, "%d\n", read_status(WL_ON));
563}
564
565static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
566 const char *buf, size_t count)
567{
Corentin Chary935ffee2007-03-11 10:26:12 +0100568 return store_status(buf, count, wl_switch_handle, WL_ON);
Corentin Chary4564de12007-01-26 14:04:40 +0100569}
570
571/*
572 * Bluetooth
573 */
574static ssize_t show_bluetooth(struct device *dev,
575 struct device_attribute *attr, char *buf)
576{
577 return sprintf(buf, "%d\n", read_status(BT_ON));
578}
579
Len Brown8def05f2007-01-30 01:46:43 -0500580static ssize_t store_bluetooth(struct device *dev,
581 struct device_attribute *attr, const char *buf,
582 size_t count)
Corentin Chary4564de12007-01-26 14:04:40 +0100583{
Corentin Chary935ffee2007-03-11 10:26:12 +0100584 return store_status(buf, count, bt_switch_handle, BT_ON);
Corentin Chary4564de12007-01-26 14:04:40 +0100585}
586
Corentin Chary78127b42007-01-26 14:04:49 +0100587/*
588 * Display
589 */
590static void set_display(int value)
591{
592 /* no sanity check needed for now */
593 if (!write_acpi_int(display_set_handle, NULL, value, NULL))
594 printk(ASUS_WARNING "Error setting display\n");
595 return;
596}
597
598static int read_display(void)
599{
Corentin Chary9a816852007-03-11 10:25:38 +0100600 ulong value = 0;
601 acpi_status rv = AE_OK;
Corentin Chary78127b42007-01-26 14:04:49 +0100602
603 /* In most of the case, we know how to set the display, but sometime
604 we can't read it */
Len Brown8def05f2007-01-30 01:46:43 -0500605 if (display_get_handle) {
Corentin Chary9a816852007-03-11 10:25:38 +0100606 rv = acpi_evaluate_integer(display_get_handle, NULL,
607 NULL, &value);
608 if (ACPI_FAILURE(rv))
Corentin Chary78127b42007-01-26 14:04:49 +0100609 printk(ASUS_WARNING "Error reading display status\n");
610 }
611
612 value &= 0x0F; /* needed for some models, shouldn't hurt others */
613
614 return value;
615}
Len Brown8def05f2007-01-30 01:46:43 -0500616
Corentin Chary78127b42007-01-26 14:04:49 +0100617/*
618 * Now, *this* one could be more user-friendly, but so far, no-one has
619 * complained. The significance of bits is the same as in store_disp()
620 */
621static ssize_t show_disp(struct device *dev,
622 struct device_attribute *attr, char *buf)
623{
624 return sprintf(buf, "%d\n", read_display());
625}
626
627/*
628 * Experimental support for display switching. As of now: 1 should activate
629 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
630 * Any combination (bitwise) of these will suffice. I never actually tested 4
631 * displays hooked up simultaneously, so be warned. See the acpi4asus README
632 * for more info.
633 */
634static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
635 const char *buf, size_t count)
636{
637 int rv, value;
638
639 rv = parse_arg(buf, count, &value);
640 if (rv > 0)
641 set_display(value);
642 return rv;
643}
644
Corentin Chary8b857352007-01-26 14:04:58 +0100645/*
646 * Light Sens
647 */
648static void set_light_sens_switch(int value)
649{
650 if (!write_acpi_int(ls_switch_handle, NULL, value, NULL))
651 printk(ASUS_WARNING "Error setting light sensor switch\n");
652 hotk->light_switch = value;
653}
654
655static ssize_t show_lssw(struct device *dev,
656 struct device_attribute *attr, char *buf)
657{
658 return sprintf(buf, "%d\n", hotk->light_switch);
659}
660
661static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
662 const char *buf, size_t count)
663{
664 int rv, value;
665
666 rv = parse_arg(buf, count, &value);
667 if (rv > 0)
668 set_light_sens_switch(value ? 1 : 0);
669
670 return rv;
671}
672
673static void set_light_sens_level(int value)
674{
675 if (!write_acpi_int(ls_level_handle, NULL, value, NULL))
676 printk(ASUS_WARNING "Error setting light sensor level\n");
677 hotk->light_level = value;
678}
679
680static ssize_t show_lslvl(struct device *dev,
681 struct device_attribute *attr, char *buf)
682{
683 return sprintf(buf, "%d\n", hotk->light_level);
684}
685
686static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
687 const char *buf, size_t count)
688{
689 int rv, value;
690
691 rv = parse_arg(buf, count, &value);
692 if (rv > 0) {
693 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
694 /* 0 <= value <= 15 */
695 set_light_sens_level(value);
696 }
697
698 return rv;
699}
700
Corentin Charye539c2f2007-05-06 14:47:06 +0200701/*
702 * GPS
703 */
704static ssize_t show_gps(struct device *dev,
705 struct device_attribute *attr, char *buf)
706{
707 return sprintf(buf, "%d\n", read_status(GPS_ON));
708}
709
710static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
711 const char *buf, size_t count)
712{
Corentin Charyf3985322007-05-06 14:48:22 +0200713 return store_status(buf, count, NULL, GPS_ON);
Corentin Charye539c2f2007-05-06 14:47:06 +0200714}
715
Corentin Chary85091b72007-01-26 14:04:30 +0100716static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
717{
718 /* TODO Find a better way to handle events count. */
719 if (!hotk)
720 return;
721
Corentin Chary6b7091e2007-01-26 14:04:45 +0100722 /*
723 * We need to tell the backlight device when the backlight power is
724 * switched
725 */
726 if (event == ATKD_LCD_ON) {
Corentin Chary935ffee2007-03-11 10:26:12 +0100727 write_status(NULL, 1, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100728 lcd_blank(FB_BLANK_UNBLANK);
Len Brown8def05f2007-01-30 01:46:43 -0500729 } else if (event == ATKD_LCD_OFF) {
Corentin Chary935ffee2007-03-11 10:26:12 +0100730 write_status(NULL, 0, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100731 lcd_blank(FB_BLANK_POWERDOWN);
732 }
733
Len Brown14e04fb2007-08-23 15:20:26 -0400734 acpi_bus_generate_proc_event(hotk->device, event,
Corentin Chary85091b72007-01-26 14:04:30 +0100735 hotk->event_count[event % 128]++);
736
737 return;
738}
739
740#define ASUS_CREATE_DEVICE_ATTR(_name) \
741 struct device_attribute dev_attr_##_name = { \
742 .attr = { \
743 .name = __stringify(_name), \
Tejun Heo7b595752007-06-14 03:45:17 +0900744 .mode = 0 }, \
Corentin Chary85091b72007-01-26 14:04:30 +0100745 .show = NULL, \
746 .store = NULL, \
747 }
748
749#define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
750 do { \
751 dev_attr_##_name.attr.mode = _mode; \
752 dev_attr_##_name.show = _show; \
753 dev_attr_##_name.store = _store; \
754 } while(0)
755
756static ASUS_CREATE_DEVICE_ATTR(infos);
Corentin Chary4564de12007-01-26 14:04:40 +0100757static ASUS_CREATE_DEVICE_ATTR(wlan);
758static ASUS_CREATE_DEVICE_ATTR(bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +0100759static ASUS_CREATE_DEVICE_ATTR(display);
Corentin Chary722ad972007-01-26 14:04:55 +0100760static ASUS_CREATE_DEVICE_ATTR(ledd);
Corentin Chary8b857352007-01-26 14:04:58 +0100761static ASUS_CREATE_DEVICE_ATTR(ls_switch);
762static ASUS_CREATE_DEVICE_ATTR(ls_level);
Corentin Charye539c2f2007-05-06 14:47:06 +0200763static ASUS_CREATE_DEVICE_ATTR(gps);
Corentin Chary85091b72007-01-26 14:04:30 +0100764
765static struct attribute *asuspf_attributes[] = {
Len Brown8def05f2007-01-30 01:46:43 -0500766 &dev_attr_infos.attr,
767 &dev_attr_wlan.attr,
768 &dev_attr_bluetooth.attr,
769 &dev_attr_display.attr,
770 &dev_attr_ledd.attr,
771 &dev_attr_ls_switch.attr,
772 &dev_attr_ls_level.attr,
Corentin Charye539c2f2007-05-06 14:47:06 +0200773 &dev_attr_gps.attr,
Len Brown8def05f2007-01-30 01:46:43 -0500774 NULL
Corentin Chary85091b72007-01-26 14:04:30 +0100775};
776
777static struct attribute_group asuspf_attribute_group = {
Len Brown8def05f2007-01-30 01:46:43 -0500778 .attrs = asuspf_attributes
Corentin Chary85091b72007-01-26 14:04:30 +0100779};
780
781static struct platform_driver asuspf_driver = {
Len Brown8def05f2007-01-30 01:46:43 -0500782 .driver = {
783 .name = ASUS_HOTK_FILE,
784 .owner = THIS_MODULE,
785 }
Corentin Chary85091b72007-01-26 14:04:30 +0100786};
787
788static struct platform_device *asuspf_device;
789
Corentin Chary85091b72007-01-26 14:04:30 +0100790static void asus_hotk_add_fs(void)
791{
792 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
Corentin Chary4564de12007-01-26 14:04:40 +0100793
794 if (wl_switch_handle)
795 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
796
797 if (bt_switch_handle)
798 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
799 show_bluetooth, store_bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +0100800
801 if (display_set_handle && display_get_handle)
802 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
Len Brown8def05f2007-01-30 01:46:43 -0500803 else if (display_set_handle)
Corentin Chary78127b42007-01-26 14:04:49 +0100804 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
805
Corentin Chary722ad972007-01-26 14:04:55 +0100806 if (ledd_set_handle)
807 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
808
Corentin Chary8b857352007-01-26 14:04:58 +0100809 if (ls_switch_handle && ls_level_handle) {
810 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
811 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
812 }
Corentin Charye539c2f2007-05-06 14:47:06 +0200813
Corentin Charyf3985322007-05-06 14:48:22 +0200814 if (gps_status_handle && gps_on_handle && gps_off_handle)
Corentin Charye539c2f2007-05-06 14:47:06 +0200815 ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
Corentin Chary85091b72007-01-26 14:04:30 +0100816}
817
Len Brown8def05f2007-01-30 01:46:43 -0500818static int asus_handle_init(char *name, acpi_handle * handle,
Corentin Chary85091b72007-01-26 14:04:30 +0100819 char **paths, int num_paths)
820{
821 int i;
822 acpi_status status;
823
824 for (i = 0; i < num_paths; i++) {
825 status = acpi_get_handle(NULL, paths[i], handle);
826 if (ACPI_SUCCESS(status))
827 return 0;
828 }
829
830 *handle = NULL;
831 return -ENODEV;
832}
833
834#define ASUS_HANDLE_INIT(object) \
835 asus_handle_init(#object, &object##_handle, object##_paths, \
836 ARRAY_SIZE(object##_paths))
837
Corentin Chary85091b72007-01-26 14:04:30 +0100838/*
839 * This function is used to initialize the hotk with right values. In this
840 * method, we can make all the detection we want, and modify the hotk struct
841 */
842static int asus_hotk_get_info(void)
843{
844 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Corentin Chary85091b72007-01-26 14:04:30 +0100845 union acpi_object *model = NULL;
Corentin Chary9a816852007-03-11 10:25:38 +0100846 ulong bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +0100847 char *string = NULL;
848 acpi_status status;
849
850 /*
851 * Get DSDT headers early enough to allow for differentiating between
852 * models, but late enough to allow acpi_bus_register_driver() to fail
853 * before doing anything ACPI-specific. Should we encounter a machine,
854 * which needs special handling (i.e. its hotkey device has a different
855 * HID), this bit will be moved. A global variable asus_info contains
856 * the DSDT header.
857 */
Len Brown894d79b2007-02-03 02:13:53 -0500858 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
Corentin Chary85091b72007-01-26 14:04:30 +0100859 if (ACPI_FAILURE(status))
860 printk(ASUS_WARNING "Couldn't get the DSDT table header\n");
Corentin Chary85091b72007-01-26 14:04:30 +0100861
862 /* We have to write 0 on init this far for all ASUS models */
863 if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
864 printk(ASUS_ERR "Hotkey initialization failed\n");
865 return -ENODEV;
866 }
867
868 /* This needs to be called for some laptops to init properly */
Corentin Chary9a816852007-03-11 10:25:38 +0100869 status =
870 acpi_evaluate_integer(hotk->handle, "BSTS", NULL, &bsts_result);
871 if (ACPI_FAILURE(status))
Corentin Chary85091b72007-01-26 14:04:30 +0100872 printk(ASUS_WARNING "Error calling BSTS\n");
873 else if (bsts_result)
874 printk(ASUS_NOTICE "BSTS called, 0x%02x returned\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100875 (uint) bsts_result);
Corentin Chary85091b72007-01-26 14:04:30 +0100876
Corentin Chary185e5af2007-03-11 10:27:33 +0100877 /* This too ... */
878 write_acpi_int(hotk->handle, "CWAP", wapf, NULL);
879
Corentin Chary85091b72007-01-26 14:04:30 +0100880 /*
881 * Try to match the object returned by INIT to the specific model.
882 * Handle every possible object (or the lack of thereof) the DSDT
883 * writers might throw at us. When in trouble, we pass NULL to
884 * asus_model_match() and try something completely different.
885 */
886 if (buffer.pointer) {
887 model = buffer.pointer;
888 switch (model->type) {
889 case ACPI_TYPE_STRING:
890 string = model->string.pointer;
891 break;
892 case ACPI_TYPE_BUFFER:
893 string = model->buffer.pointer;
894 break;
895 default:
896 string = "";
897 break;
898 }
899 }
900 hotk->name = kstrdup(string, GFP_KERNEL);
901 if (!hotk->name)
902 return -ENOMEM;
903
Len Brown8def05f2007-01-30 01:46:43 -0500904 if (*string)
Corentin Chary85091b72007-01-26 14:04:30 +0100905 printk(ASUS_NOTICE " %s model detected\n", string);
906
Corentin Charybe18cda2007-01-26 14:04:35 +0100907 ASUS_HANDLE_INIT(mled_set);
908 ASUS_HANDLE_INIT(tled_set);
909 ASUS_HANDLE_INIT(rled_set);
910 ASUS_HANDLE_INIT(pled_set);
Corentin Charyfdd8d082007-03-11 10:26:48 +0100911 ASUS_HANDLE_INIT(gled_set);
Corentin Charybe18cda2007-01-26 14:04:35 +0100912
Corentin Chary722ad972007-01-26 14:04:55 +0100913 ASUS_HANDLE_INIT(ledd_set);
914
Corentin Chary4564de12007-01-26 14:04:40 +0100915 /*
916 * The HWRS method return informations about the hardware.
917 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
918 * The significance of others is yet to be found.
919 * If we don't find the method, we assume the device are present.
920 */
Corentin Chary9a816852007-03-11 10:25:38 +0100921 status =
922 acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &hwrs_result);
923 if (ACPI_FAILURE(status))
Corentin Chary4564de12007-01-26 14:04:40 +0100924 hwrs_result = WL_HWRS | BT_HWRS;
925
Len Brown8def05f2007-01-30 01:46:43 -0500926 if (hwrs_result & WL_HWRS)
Corentin Chary4564de12007-01-26 14:04:40 +0100927 ASUS_HANDLE_INIT(wl_switch);
Len Brown8def05f2007-01-30 01:46:43 -0500928 if (hwrs_result & BT_HWRS)
Corentin Chary4564de12007-01-26 14:04:40 +0100929 ASUS_HANDLE_INIT(bt_switch);
930
931 ASUS_HANDLE_INIT(wireless_status);
932
Corentin Chary6b7091e2007-01-26 14:04:45 +0100933 ASUS_HANDLE_INIT(brightness_set);
934 ASUS_HANDLE_INIT(brightness_get);
935
936 ASUS_HANDLE_INIT(lcd_switch);
937
Corentin Chary78127b42007-01-26 14:04:49 +0100938 ASUS_HANDLE_INIT(display_set);
939 ASUS_HANDLE_INIT(display_get);
940
Corentin Chary8b857352007-01-26 14:04:58 +0100941 /* There is a lot of models with "ALSL", but a few get
942 a real light sens, so we need to check it. */
Corentin Chary832d9952007-05-06 14:47:29 +0200943 if (!ASUS_HANDLE_INIT(ls_switch))
Corentin Chary8b857352007-01-26 14:04:58 +0100944 ASUS_HANDLE_INIT(ls_level);
945
Corentin Charye539c2f2007-05-06 14:47:06 +0200946 ASUS_HANDLE_INIT(gps_on);
947 ASUS_HANDLE_INIT(gps_off);
948 ASUS_HANDLE_INIT(gps_status);
949
Corentin Chary85091b72007-01-26 14:04:30 +0100950 kfree(model);
951
952 return AE_OK;
953}
954
955static int asus_hotk_check(void)
956{
957 int result = 0;
958
959 result = acpi_bus_get_status(hotk->device);
960 if (result)
961 return result;
962
963 if (hotk->device->status.present) {
964 result = asus_hotk_get_info();
965 } else {
966 printk(ASUS_ERR "Hotkey device not present, aborting\n");
967 return -EINVAL;
968 }
969
970 return result;
971}
972
973static int asus_hotk_found;
974
975static int asus_hotk_add(struct acpi_device *device)
976{
977 acpi_status status = AE_OK;
978 int result;
979
980 if (!device)
981 return -EINVAL;
982
983 printk(ASUS_NOTICE "Asus Laptop Support version %s\n",
984 ASUS_LAPTOP_VERSION);
985
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700986 hotk = kzalloc(sizeof(struct asus_hotk), GFP_KERNEL);
Corentin Chary85091b72007-01-26 14:04:30 +0100987 if (!hotk)
988 return -ENOMEM;
Corentin Chary85091b72007-01-26 14:04:30 +0100989
990 hotk->handle = device->handle;
991 strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
992 strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
993 acpi_driver_data(device) = hotk;
994 hotk->device = device;
995
996 result = asus_hotk_check();
997 if (result)
998 goto end;
999
1000 asus_hotk_add_fs();
1001
1002 /*
1003 * We install the handler, it will receive the hotk in parameter, so, we
1004 * could add other data to the hotk struct
1005 */
Corentin Charya9dbcb32007-05-06 14:46:41 +02001006 status = acpi_install_notify_handler(hotk->handle, ACPI_ALL_NOTIFY,
Corentin Chary85091b72007-01-26 14:04:30 +01001007 asus_hotk_notify, hotk);
1008 if (ACPI_FAILURE(status))
1009 printk(ASUS_ERR "Error installing notify handler\n");
1010
1011 asus_hotk_found = 1;
1012
Corentin Chary6b7091e2007-01-26 14:04:45 +01001013 /* WLED and BLED are on by default */
Corentin Chary935ffee2007-03-11 10:26:12 +01001014 write_status(bt_switch_handle, 1, BT_ON);
1015 write_status(wl_switch_handle, 1, WL_ON);
1016
1017 /* If the h/w switch is off, we need to check the real status */
1018 write_status(NULL, read_status(BT_ON), BT_ON);
1019 write_status(NULL, read_status(WL_ON), WL_ON);
Corentin Chary4564de12007-01-26 14:04:40 +01001020
Corentin Chary6b7091e2007-01-26 14:04:45 +01001021 /* LCD Backlight is on by default */
Corentin Chary935ffee2007-03-11 10:26:12 +01001022 write_status(NULL, 1, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001023
Corentin Chary722ad972007-01-26 14:04:55 +01001024 /* LED display is off by default */
1025 hotk->ledd_status = 0xFFF;
1026
Len Brown8def05f2007-01-30 01:46:43 -05001027 /* Set initial values of light sensor and level */
1028 hotk->light_switch = 1; /* Default to light sensor disabled */
1029 hotk->light_level = 0; /* level 5 for sensor sensitivity */
Corentin Chary8b857352007-01-26 14:04:58 +01001030
Len Brown8def05f2007-01-30 01:46:43 -05001031 if (ls_switch_handle)
1032 set_light_sens_switch(hotk->light_switch);
Corentin Chary8b857352007-01-26 14:04:58 +01001033
Len Brown8def05f2007-01-30 01:46:43 -05001034 if (ls_level_handle)
1035 set_light_sens_level(hotk->light_level);
Corentin Chary8b857352007-01-26 14:04:58 +01001036
Corentin Charye539c2f2007-05-06 14:47:06 +02001037 /* GPS is on by default */
1038 write_status(NULL, 1, GPS_ON);
1039
Corentin Chary85091b72007-01-26 14:04:30 +01001040 end:
1041 if (result) {
1042 kfree(hotk->name);
1043 kfree(hotk);
1044 }
1045
1046 return result;
1047}
1048
1049static int asus_hotk_remove(struct acpi_device *device, int type)
1050{
1051 acpi_status status = 0;
1052
1053 if (!device || !acpi_driver_data(device))
1054 return -EINVAL;
1055
Corentin Charya9dbcb32007-05-06 14:46:41 +02001056 status = acpi_remove_notify_handler(hotk->handle, ACPI_ALL_NOTIFY,
Corentin Chary85091b72007-01-26 14:04:30 +01001057 asus_hotk_notify);
1058 if (ACPI_FAILURE(status))
1059 printk(ASUS_ERR "Error removing notify handler\n");
1060
1061 kfree(hotk->name);
1062 kfree(hotk);
1063
1064 return 0;
1065}
1066
Corentin Chary6b7091e2007-01-26 14:04:45 +01001067static void asus_backlight_exit(void)
1068{
Len Brown8def05f2007-01-30 01:46:43 -05001069 if (asus_backlight_device)
Corentin Chary6b7091e2007-01-26 14:04:45 +01001070 backlight_device_unregister(asus_backlight_device);
1071}
1072
Corentin Charybe18cda2007-01-26 14:04:35 +01001073#define ASUS_LED_UNREGISTER(object) \
Guillaume Chazaraine1996a692007-08-16 18:18:53 +02001074 if (object##_led.dev) \
1075 led_classdev_unregister(&object##_led)
Corentin Charybe18cda2007-01-26 14:04:35 +01001076
1077static void asus_led_exit(void)
1078{
Al Viro3b0d7112007-07-23 11:21:34 +01001079 destroy_workqueue(led_workqueue);
Corentin Charybe18cda2007-01-26 14:04:35 +01001080 ASUS_LED_UNREGISTER(mled);
1081 ASUS_LED_UNREGISTER(tled);
1082 ASUS_LED_UNREGISTER(pled);
1083 ASUS_LED_UNREGISTER(rled);
Corentin Charyfdd8d082007-03-11 10:26:48 +01001084 ASUS_LED_UNREGISTER(gled);
Corentin Charybe18cda2007-01-26 14:04:35 +01001085}
1086
Corentin Chary85091b72007-01-26 14:04:30 +01001087static void __exit asus_laptop_exit(void)
1088{
Corentin Chary6b7091e2007-01-26 14:04:45 +01001089 asus_backlight_exit();
Corentin Charybe18cda2007-01-26 14:04:35 +01001090 asus_led_exit();
1091
Corentin Chary85091b72007-01-26 14:04:30 +01001092 acpi_bus_unregister_driver(&asus_hotk_driver);
Len Brown8def05f2007-01-30 01:46:43 -05001093 sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
1094 platform_device_unregister(asuspf_device);
1095 platform_driver_unregister(&asuspf_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001096}
1097
Len Brown8def05f2007-01-30 01:46:43 -05001098static int asus_backlight_init(struct device *dev)
Corentin Chary6b7091e2007-01-26 14:04:45 +01001099{
1100 struct backlight_device *bd;
1101
Len Brown8def05f2007-01-30 01:46:43 -05001102 if (brightness_set_handle && lcd_switch_handle) {
1103 bd = backlight_device_register(ASUS_HOTK_FILE, dev,
Richard Purdie599a52d2007-02-10 23:07:48 +00001104 NULL, &asusbl_ops);
Len Brown8def05f2007-01-30 01:46:43 -05001105 if (IS_ERR(bd)) {
Corentin Chary6b7091e2007-01-26 14:04:45 +01001106 printk(ASUS_ERR
1107 "Could not register asus backlight device\n");
1108 asus_backlight_device = NULL;
1109 return PTR_ERR(bd);
1110 }
1111
1112 asus_backlight_device = bd;
1113
Richard Purdie599a52d2007-02-10 23:07:48 +00001114 bd->props.max_brightness = 15;
1115 bd->props.brightness = read_brightness(NULL);
1116 bd->props.power = FB_BLANK_UNBLANK;
Richard Purdie28ee0862007-02-08 22:25:09 +00001117 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001118 }
1119 return 0;
1120}
1121
Corentin Charybe18cda2007-01-26 14:04:35 +01001122static int asus_led_register(acpi_handle handle,
Len Brown8def05f2007-01-30 01:46:43 -05001123 struct led_classdev *ldev, struct device *dev)
Corentin Charybe18cda2007-01-26 14:04:35 +01001124{
Len Brown8def05f2007-01-30 01:46:43 -05001125 if (!handle)
Corentin Charybe18cda2007-01-26 14:04:35 +01001126 return 0;
1127
1128 return led_classdev_register(dev, ldev);
1129}
Len Brown8def05f2007-01-30 01:46:43 -05001130
Corentin Charybe18cda2007-01-26 14:04:35 +01001131#define ASUS_LED_REGISTER(object, device) \
1132 asus_led_register(object##_set_handle, &object##_led, device)
1133
Len Brown8def05f2007-01-30 01:46:43 -05001134static int asus_led_init(struct device *dev)
Corentin Charybe18cda2007-01-26 14:04:35 +01001135{
1136 int rv;
1137
1138 rv = ASUS_LED_REGISTER(mled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001139 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001140 goto out;
Corentin Charybe18cda2007-01-26 14:04:35 +01001141
1142 rv = ASUS_LED_REGISTER(tled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001143 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001144 goto out1;
Corentin Charybe18cda2007-01-26 14:04:35 +01001145
1146 rv = ASUS_LED_REGISTER(rled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001147 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001148 goto out2;
Corentin Charybe18cda2007-01-26 14:04:35 +01001149
1150 rv = ASUS_LED_REGISTER(pled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001151 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001152 goto out3;
Corentin Charybe18cda2007-01-26 14:04:35 +01001153
Corentin Charyfdd8d082007-03-11 10:26:48 +01001154 rv = ASUS_LED_REGISTER(gled, dev);
1155 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001156 goto out4;
Corentin Charyfdd8d082007-03-11 10:26:48 +01001157
Corentin Charybe18cda2007-01-26 14:04:35 +01001158 led_workqueue = create_singlethread_workqueue("led_workqueue");
Len Brown8def05f2007-01-30 01:46:43 -05001159 if (!led_workqueue)
Al Viro3b0d7112007-07-23 11:21:34 +01001160 goto out5;
Corentin Charybe18cda2007-01-26 14:04:35 +01001161
1162 return 0;
Al Viro3b0d7112007-07-23 11:21:34 +01001163out5:
1164 rv = -ENOMEM;
1165 ASUS_LED_UNREGISTER(gled);
1166out4:
1167 ASUS_LED_UNREGISTER(pled);
1168out3:
1169 ASUS_LED_UNREGISTER(rled);
1170out2:
1171 ASUS_LED_UNREGISTER(tled);
1172out1:
1173 ASUS_LED_UNREGISTER(mled);
1174out:
1175 return rv;
Corentin Charybe18cda2007-01-26 14:04:35 +01001176}
1177
Corentin Chary85091b72007-01-26 14:04:30 +01001178static int __init asus_laptop_init(void)
1179{
Corentin Charybe18cda2007-01-26 14:04:35 +01001180 struct device *dev;
Corentin Chary85091b72007-01-26 14:04:30 +01001181 int result;
1182
1183 if (acpi_disabled)
1184 return -ENODEV;
1185
Corentin Chary85091b72007-01-26 14:04:30 +01001186 result = acpi_bus_register_driver(&asus_hotk_driver);
1187 if (result < 0)
1188 return result;
1189
1190 /*
1191 * This is a bit of a kludge. We only want this module loaded
1192 * for ASUS systems, but there's currently no way to probe the
1193 * ACPI namespace for ASUS HIDs. So we just return failure if
1194 * we didn't find one, which will cause the module to be
1195 * unloaded.
1196 */
1197 if (!asus_hotk_found) {
1198 acpi_bus_unregister_driver(&asus_hotk_driver);
1199 return -ENODEV;
1200 }
1201
Corentin Charybe18cda2007-01-26 14:04:35 +01001202 dev = acpi_get_physical_device(hotk->device->handle);
1203
Corentin Chary6b7091e2007-01-26 14:04:45 +01001204 result = asus_backlight_init(dev);
Len Brown8def05f2007-01-30 01:46:43 -05001205 if (result)
Corentin Chary6b7091e2007-01-26 14:04:45 +01001206 goto fail_backlight;
1207
Corentin Charybe18cda2007-01-26 14:04:35 +01001208 result = asus_led_init(dev);
Len Brown8def05f2007-01-30 01:46:43 -05001209 if (result)
Corentin Charybe18cda2007-01-26 14:04:35 +01001210 goto fail_led;
1211
Len Brown8def05f2007-01-30 01:46:43 -05001212 /* Register platform stuff */
Corentin Chary85091b72007-01-26 14:04:30 +01001213 result = platform_driver_register(&asuspf_driver);
Len Brown8def05f2007-01-30 01:46:43 -05001214 if (result)
1215 goto fail_platform_driver;
Corentin Chary85091b72007-01-26 14:04:30 +01001216
Len Brown8def05f2007-01-30 01:46:43 -05001217 asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
1218 if (!asuspf_device) {
1219 result = -ENOMEM;
1220 goto fail_platform_device1;
1221 }
Corentin Chary85091b72007-01-26 14:04:30 +01001222
Len Brown8def05f2007-01-30 01:46:43 -05001223 result = platform_device_add(asuspf_device);
1224 if (result)
1225 goto fail_platform_device2;
Corentin Chary85091b72007-01-26 14:04:30 +01001226
Len Brown8def05f2007-01-30 01:46:43 -05001227 result = sysfs_create_group(&asuspf_device->dev.kobj,
Corentin Chary85091b72007-01-26 14:04:30 +01001228 &asuspf_attribute_group);
Len Brown8def05f2007-01-30 01:46:43 -05001229 if (result)
1230 goto fail_sysfs;
Corentin Chary85091b72007-01-26 14:04:30 +01001231
Len Brown8def05f2007-01-30 01:46:43 -05001232 return 0;
Corentin Chary85091b72007-01-26 14:04:30 +01001233
Len Brown8def05f2007-01-30 01:46:43 -05001234 fail_sysfs:
1235 platform_device_del(asuspf_device);
Corentin Chary85091b72007-01-26 14:04:30 +01001236
Len Brown8def05f2007-01-30 01:46:43 -05001237 fail_platform_device2:
Corentin Chary85091b72007-01-26 14:04:30 +01001238 platform_device_put(asuspf_device);
1239
Len Brown8def05f2007-01-30 01:46:43 -05001240 fail_platform_device1:
1241 platform_driver_unregister(&asuspf_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001242
Len Brown8def05f2007-01-30 01:46:43 -05001243 fail_platform_driver:
Corentin Charybe18cda2007-01-26 14:04:35 +01001244 asus_led_exit();
1245
Len Brown8def05f2007-01-30 01:46:43 -05001246 fail_led:
Corentin Chary6b7091e2007-01-26 14:04:45 +01001247 asus_backlight_exit();
1248
Len Brown8def05f2007-01-30 01:46:43 -05001249 fail_backlight:
Corentin Chary85091b72007-01-26 14:04:30 +01001250
1251 return result;
1252}
1253
1254module_init(asus_laptop_init);
1255module_exit(asus_laptop_exit);