blob: dfec565ffa8220b189d725d3c9f79f9ab12da915 [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
33 *
34 */
35
36#include <linux/autoconf.h>
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/types.h>
41#include <linux/err.h>
42#include <linux/proc_fs.h>
Corentin Chary6b7091e2007-01-26 14:04:45 +010043#include <linux/backlight.h>
44#include <linux/fb.h>
Corentin Charybe18cda2007-01-26 14:04:35 +010045#include <linux/leds.h>
Corentin Chary85091b72007-01-26 14:04:30 +010046#include <linux/platform_device.h>
47#include <acpi/acpi_drivers.h>
48#include <acpi/acpi_bus.h>
49#include <asm/uaccess.h>
50
Corentin Chary8ec555c2007-03-11 10:28:03 +010051#define ASUS_LAPTOP_VERSION "0.41"
Corentin Chary85091b72007-01-26 14:04:30 +010052
53#define ASUS_HOTK_NAME "Asus Laptop Support"
54#define ASUS_HOTK_CLASS "hotkey"
55#define ASUS_HOTK_DEVICE_NAME "Hotkey"
56#define ASUS_HOTK_HID "ATK0100"
57#define ASUS_HOTK_FILE "asus-laptop"
58#define ASUS_HOTK_PREFIX "\\_SB.ATKD."
59
Corentin Charybe18cda2007-01-26 14:04:35 +010060/*
Corentin Chary6b7091e2007-01-26 14:04:45 +010061 * Some events we use, same for all Asus
62 */
63#define ATKD_BR_UP 0x10
64#define ATKD_BR_DOWN 0x20
65#define ATKD_LCD_ON 0x33
66#define ATKD_LCD_OFF 0x34
67
68/*
Corentin Chary4564de12007-01-26 14:04:40 +010069 * Known bits returned by \_SB.ATKD.HWRS
Corentin Charybe18cda2007-01-26 14:04:35 +010070 */
Corentin Chary4564de12007-01-26 14:04:40 +010071#define WL_HWRS 0x80
72#define BT_HWRS 0x100
73
74/*
75 * Flags for hotk status
76 * WL_ON and BT_ON are also used for wireless_status()
77 */
78#define WL_ON 0x01 //internal Wifi
79#define BT_ON 0x02 //internal Bluetooth
Corentin Charybe18cda2007-01-26 14:04:35 +010080#define MLED_ON 0x04 //mail LED
81#define TLED_ON 0x08 //touchpad LED
Len Brown8def05f2007-01-30 01:46:43 -050082#define RLED_ON 0x10 //Record LED
83#define PLED_ON 0x20 //Phone LED
Corentin Charyfdd8d082007-03-11 10:26:48 +010084#define GLED_ON 0x40 //Gaming LED
85#define LCD_ON 0x80 //LCD backlight
Corentin Charybe18cda2007-01-26 14:04:35 +010086
Corentin Chary85091b72007-01-26 14:04:30 +010087#define ASUS_LOG ASUS_HOTK_FILE ": "
88#define ASUS_ERR KERN_ERR ASUS_LOG
89#define ASUS_WARNING KERN_WARNING ASUS_LOG
90#define ASUS_NOTICE KERN_NOTICE ASUS_LOG
91#define ASUS_INFO KERN_INFO ASUS_LOG
92#define ASUS_DEBUG KERN_DEBUG ASUS_LOG
93
94MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
95MODULE_DESCRIPTION(ASUS_HOTK_NAME);
96MODULE_LICENSE("GPL");
97
Corentin Chary185e5af2007-03-11 10:27:33 +010098/* WAPF defines the behavior of the Fn+Fx wlan key
99 * The significance of values is yet to be found, but
100 * most of the time:
101 * 0x0 will do nothing
102 * 0x1 will allow to control the device with Fn+Fx key.
103 * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
104 * 0x5 like 0x1 or 0x4
105 * So, if something doesn't work as you want, just try other values =)
106 */
107static uint wapf = 1;
108module_param(wapf, uint, 0644);
109MODULE_PARM_DESC(wapf, "WAPF value");
110
Corentin Chary85091b72007-01-26 14:04:30 +0100111#define ASUS_HANDLE(object, paths...) \
112 static acpi_handle object##_handle = NULL; \
113 static char *object##_paths[] = { paths }
114
Corentin Charybe18cda2007-01-26 14:04:35 +0100115/* LED */
116ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
117ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
Len Brown8def05f2007-01-30 01:46:43 -0500118ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
119ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
Corentin Charyfdd8d082007-03-11 10:26:48 +0100120ASUS_HANDLE(gled_set, ASUS_HOTK_PREFIX "GLED"); /* G1, G2 (probably) */
Corentin Charybe18cda2007-01-26 14:04:35 +0100121
Corentin Chary722ad972007-01-26 14:04:55 +0100122/* LEDD */
123ASUS_HANDLE(ledd_set, ASUS_HOTK_PREFIX "SLCM");
124
Corentin Chary4564de12007-01-26 14:04:40 +0100125/* Bluetooth and WLAN
126 * WLED and BLED are not handled like other XLED, because in some dsdt
127 * they also control the WLAN/Bluetooth device.
128 */
129ASUS_HANDLE(wl_switch, ASUS_HOTK_PREFIX "WLED");
130ASUS_HANDLE(bt_switch, ASUS_HOTK_PREFIX "BLED");
Len Brown8def05f2007-01-30 01:46:43 -0500131ASUS_HANDLE(wireless_status, ASUS_HOTK_PREFIX "RSTS"); /* All new models */
Corentin Chary4564de12007-01-26 14:04:40 +0100132
Corentin Chary6b7091e2007-01-26 14:04:45 +0100133/* Brightness */
134ASUS_HANDLE(brightness_set, ASUS_HOTK_PREFIX "SPLV");
135ASUS_HANDLE(brightness_get, ASUS_HOTK_PREFIX "GPLV");
136
137/* Backlight */
Len Brown8def05f2007-01-30 01:46:43 -0500138ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
139 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
140 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
141 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
142 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
143 "\\_SB.PCI0.PX40.Q10", /* S1x */
144 "\\Q10"); /* A2x, L2D, L3D, M2E */
Corentin Chary6b7091e2007-01-26 14:04:45 +0100145
Corentin Chary78127b42007-01-26 14:04:49 +0100146/* Display */
147ASUS_HANDLE(display_set, ASUS_HOTK_PREFIX "SDSP");
Len Brown8def05f2007-01-30 01:46:43 -0500148ASUS_HANDLE(display_get, "\\_SB.PCI0.P0P1.VGA.GETD", /* A6B, A6K A6R A7D F3JM L4R M6R A3G
149 M6A M6V VX-1 V6J V6V W3Z */
150 "\\_SB.PCI0.P0P2.VGA.GETD", /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V
Corentin Chary78127b42007-01-26 14:04:49 +0100151 S5A M5A z33A W1Jc W2V */
Len Brown8def05f2007-01-30 01:46:43 -0500152 "\\_SB.PCI0.P0P3.VGA.GETD", /* A6V A6Q */
153 "\\_SB.PCI0.P0PA.VGA.GETD", /* A6T, A6M */
154 "\\_SB.PCI0.PCI1.VGAC.NMAP", /* L3C */
155 "\\_SB.PCI0.VGA.GETD", /* Z96F */
156 "\\ACTD", /* A2D */
157 "\\ADVG", /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
158 "\\DNXT", /* P30 */
159 "\\INFB", /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
160 "\\SSTE"); /* A3F A6F A3N A3L M6N W3N W6A */
Corentin Chary78127b42007-01-26 14:04:49 +0100161
Len Brown8def05f2007-01-30 01:46:43 -0500162ASUS_HANDLE(ls_switch, ASUS_HOTK_PREFIX "ALSC"); /* Z71A Z71V */
163ASUS_HANDLE(ls_level, ASUS_HOTK_PREFIX "ALSL"); /* Z71A Z71V */
Corentin Chary8b857352007-01-26 14:04:58 +0100164
Corentin Chary85091b72007-01-26 14:04:30 +0100165/*
166 * This is the main structure, we can use it to store anything interesting
167 * about the hotk device
168 */
169struct asus_hotk {
Len Brown8def05f2007-01-30 01:46:43 -0500170 char *name; //laptop name
Corentin Chary85091b72007-01-26 14:04:30 +0100171 struct acpi_device *device; //the device we are in
172 acpi_handle handle; //the handle of the hotk device
173 char status; //status of the hotk, for LEDs, ...
Corentin Chary722ad972007-01-26 14:04:55 +0100174 u32 ledd_status; //status of the LED display
Len Brown8def05f2007-01-30 01:46:43 -0500175 u8 light_level; //light sensor level
176 u8 light_switch; //light sensor switch value
Corentin Chary85091b72007-01-26 14:04:30 +0100177 u16 event_count[128]; //count for each event TODO make this better
178};
179
180/*
181 * This header is made available to allow proper configuration given model,
182 * revision number , ... this info cannot go in struct asus_hotk because it is
183 * available before the hotk
184 */
185static struct acpi_table_header *asus_info;
186
187/* The actual device the driver binds to */
188static struct asus_hotk *hotk;
189
190/*
191 * The hotkey driver declaration
192 */
193static int asus_hotk_add(struct acpi_device *device);
194static int asus_hotk_remove(struct acpi_device *device, int type);
195static struct acpi_driver asus_hotk_driver = {
196 .name = ASUS_HOTK_NAME,
197 .class = ASUS_HOTK_CLASS,
198 .ids = ASUS_HOTK_HID,
199 .ops = {
200 .add = asus_hotk_add,
201 .remove = asus_hotk_remove,
202 },
203};
204
Corentin Chary6b7091e2007-01-26 14:04:45 +0100205/* The backlight device /sys/class/backlight */
206static struct backlight_device *asus_backlight_device;
207
208/*
209 * The backlight class declaration
210 */
211static int read_brightness(struct backlight_device *bd);
212static int update_bl_status(struct backlight_device *bd);
Richard Purdie599a52d2007-02-10 23:07:48 +0000213static struct backlight_ops asusbl_ops = {
Len Brown8def05f2007-01-30 01:46:43 -0500214 .get_brightness = read_brightness,
215 .update_status = update_bl_status,
Corentin Chary6b7091e2007-01-26 14:04:45 +0100216};
217
Corentin Charybe18cda2007-01-26 14:04:35 +0100218/* These functions actually update the LED's, and are called from a
219 * workqueue. By doing this as separate work rather than when the LED
220 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
221 * potentially bad time, such as a timer interrupt. */
222static struct workqueue_struct *led_workqueue;
223
224#define ASUS_LED(object, ledname) \
225 static void object##_led_set(struct led_classdev *led_cdev, \
226 enum led_brightness value); \
227 static void object##_led_update(struct work_struct *ignored); \
228 static int object##_led_wk; \
Adrian Bunkf110ef52007-02-20 01:07:25 +0100229 static DECLARE_WORK(object##_led_work, object##_led_update); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100230 static struct led_classdev object##_led = { \
231 .name = "asus:" ledname, \
232 .brightness_set = object##_led_set, \
233 }
234
235ASUS_LED(mled, "mail");
236ASUS_LED(tled, "touchpad");
237ASUS_LED(rled, "record");
238ASUS_LED(pled, "phone");
Corentin Charyfdd8d082007-03-11 10:26:48 +0100239ASUS_LED(gled, "gaming");
Corentin Charybe18cda2007-01-26 14:04:35 +0100240
Corentin Chary85091b72007-01-26 14:04:30 +0100241/*
242 * This function evaluates an ACPI method, given an int as parameter, the
243 * method is searched within the scope of the handle, can be NULL. The output
244 * of the method is written is output, which can also be NULL
245 *
246 * returns 1 if write is successful, 0 else.
247 */
248static int write_acpi_int(acpi_handle handle, const char *method, int val,
249 struct acpi_buffer *output)
250{
251 struct acpi_object_list params; //list of input parameters (an int here)
252 union acpi_object in_obj; //the only param we use
253 acpi_status status;
254
255 params.count = 1;
256 params.pointer = &in_obj;
257 in_obj.type = ACPI_TYPE_INTEGER;
258 in_obj.integer.value = val;
259
260 status = acpi_evaluate_object(handle, (char *)method, &params, output);
261 return (status == AE_OK);
262}
263
Len Brown8def05f2007-01-30 01:46:43 -0500264static int read_wireless_status(int mask)
265{
Corentin Chary9a816852007-03-11 10:25:38 +0100266 ulong status;
267 acpi_status rv = AE_OK;
Corentin Chary4564de12007-01-26 14:04:40 +0100268
269 if (!wireless_status_handle)
270 return (hotk->status & mask) ? 1 : 0;
271
Corentin Chary9a816852007-03-11 10:25:38 +0100272 rv = acpi_evaluate_integer(wireless_status_handle, NULL, NULL, &status);
273 if (ACPI_FAILURE(rv))
Corentin Chary4564de12007-01-26 14:04:40 +0100274 printk(ASUS_WARNING "Error reading Wireless status\n");
Corentin Chary9a816852007-03-11 10:25:38 +0100275 else
276 return (status & mask) ? 1 : 0;
Corentin Chary4564de12007-01-26 14:04:40 +0100277
278 return (hotk->status & mask) ? 1 : 0;
279}
280
Corentin Charybe18cda2007-01-26 14:04:35 +0100281/* Generic LED functions */
282static int read_status(int mask)
283{
Corentin Chary4564de12007-01-26 14:04:40 +0100284 /* There is a special method for both wireless devices */
285 if (mask == BT_ON || mask == WL_ON)
286 return read_wireless_status(mask);
287
Corentin Charybe18cda2007-01-26 14:04:35 +0100288 return (hotk->status & mask) ? 1 : 0;
289}
290
Corentin Chary935ffee2007-03-11 10:26:12 +0100291static void write_status(acpi_handle handle, int out, int mask)
Corentin Charybe18cda2007-01-26 14:04:35 +0100292{
293 hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
294
Corentin Chary935ffee2007-03-11 10:26:12 +0100295 switch (mask) {
296 case MLED_ON:
Corentin Charybe18cda2007-01-26 14:04:35 +0100297 out = !out & 0x1;
Corentin Chary935ffee2007-03-11 10:26:12 +0100298 break;
Corentin Charyfdd8d082007-03-11 10:26:48 +0100299 case GLED_ON:
300 out = (out & 0x1) + 1;
301 break;
Corentin Chary935ffee2007-03-11 10:26:12 +0100302 default:
303 out &= 0x1;
304 break;
305 }
Corentin Charybe18cda2007-01-26 14:04:35 +0100306
307 if (handle && !write_acpi_int(handle, NULL, out, NULL))
Corentin Chary935ffee2007-03-11 10:26:12 +0100308 printk(ASUS_WARNING " write failed %x\n", mask);
Corentin Charybe18cda2007-01-26 14:04:35 +0100309}
310
311/* /sys/class/led handlers */
Corentin Chary935ffee2007-03-11 10:26:12 +0100312#define ASUS_LED_HANDLER(object, mask) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100313 static void object##_led_set(struct led_classdev *led_cdev, \
314 enum led_brightness value) \
315 { \
316 object##_led_wk = value; \
317 queue_work(led_workqueue, &object##_led_work); \
318 } \
319 static void object##_led_update(struct work_struct *ignored) \
320 { \
321 int value = object##_led_wk; \
Corentin Chary935ffee2007-03-11 10:26:12 +0100322 write_status(object##_set_handle, value, (mask)); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100323 }
324
Corentin Chary935ffee2007-03-11 10:26:12 +0100325ASUS_LED_HANDLER(mled, MLED_ON);
326ASUS_LED_HANDLER(pled, PLED_ON);
327ASUS_LED_HANDLER(rled, RLED_ON);
328ASUS_LED_HANDLER(tled, TLED_ON);
Corentin Charyfdd8d082007-03-11 10:26:48 +0100329ASUS_LED_HANDLER(gled, GLED_ON);
Corentin Charybe18cda2007-01-26 14:04:35 +0100330
Corentin Chary6b7091e2007-01-26 14:04:45 +0100331static int get_lcd_state(void)
332{
333 return read_status(LCD_ON);
334}
335
336static int set_lcd_state(int value)
337{
338 int lcd = 0;
339 acpi_status status = 0;
340
341 lcd = value ? 1 : 0;
342
343 if (lcd == get_lcd_state())
344 return 0;
345
Len Brown8def05f2007-01-30 01:46:43 -0500346 if (lcd_switch_handle) {
Corentin Chary6b7091e2007-01-26 14:04:45 +0100347 status = acpi_evaluate_object(lcd_switch_handle,
348 NULL, NULL, NULL);
349
350 if (ACPI_FAILURE(status))
351 printk(ASUS_WARNING "Error switching LCD\n");
352 }
353
Corentin Chary935ffee2007-03-11 10:26:12 +0100354 write_status(NULL, lcd, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100355 return 0;
356}
357
358static void lcd_blank(int blank)
359{
360 struct backlight_device *bd = asus_backlight_device;
361
Len Brown8def05f2007-01-30 01:46:43 -0500362 if (bd) {
Richard Purdie599a52d2007-02-10 23:07:48 +0000363 bd->props.power = blank;
Richard Purdie28ee0862007-02-08 22:25:09 +0000364 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100365 }
366}
367
368static int read_brightness(struct backlight_device *bd)
369{
Corentin Chary9a816852007-03-11 10:25:38 +0100370 ulong value;
371 acpi_status rv = AE_OK;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100372
Corentin Chary9a816852007-03-11 10:25:38 +0100373 rv = acpi_evaluate_integer(brightness_get_handle, NULL, NULL, &value);
374 if (ACPI_FAILURE(rv))
Corentin Chary6b7091e2007-01-26 14:04:45 +0100375 printk(ASUS_WARNING "Error reading brightness\n");
376
377 return value;
378}
379
380static int set_brightness(struct backlight_device *bd, int value)
381{
382 int ret = 0;
383
384 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
385 /* 0 <= value <= 15 */
386
387 if (!write_acpi_int(brightness_set_handle, NULL, value, NULL)) {
388 printk(ASUS_WARNING "Error changing brightness\n");
389 ret = -EIO;
390 }
391
392 return ret;
393}
394
395static int update_bl_status(struct backlight_device *bd)
396{
397 int rv;
Richard Purdie599a52d2007-02-10 23:07:48 +0000398 int value = bd->props.brightness;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100399
400 rv = set_brightness(bd, value);
Len Brown8def05f2007-01-30 01:46:43 -0500401 if (rv)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100402 return rv;
403
Richard Purdie599a52d2007-02-10 23:07:48 +0000404 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100405 return set_lcd_state(value);
406}
407
Corentin Chary85091b72007-01-26 14:04:30 +0100408/*
409 * Platform device handlers
410 */
411
412/*
413 * We write our info in page, we begin at offset off and cannot write more
414 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
415 * number of bytes written in page
416 */
417static ssize_t show_infos(struct device *dev,
Len Brown8def05f2007-01-30 01:46:43 -0500418 struct device_attribute *attr, char *page)
Corentin Chary85091b72007-01-26 14:04:30 +0100419{
420 int len = 0;
Corentin Chary9a816852007-03-11 10:25:38 +0100421 ulong temp;
Corentin Chary85091b72007-01-26 14:04:30 +0100422 char buf[16]; //enough for all info
Corentin Chary9a816852007-03-11 10:25:38 +0100423 acpi_status rv = AE_OK;
424
Corentin Chary85091b72007-01-26 14:04:30 +0100425 /*
426 * We use the easy way, we don't care of off and count, so we don't set eof
427 * to 1
428 */
429
430 len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
431 len += sprintf(page + len, "Model reference : %s\n", hotk->name);
432 /*
433 * The SFUN method probably allows the original driver to get the list
434 * of features supported by a given model. For now, 0x0100 or 0x0800
435 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
436 * The significance of others is yet to be found.
437 */
Corentin Chary9a816852007-03-11 10:25:38 +0100438 rv = acpi_evaluate_integer(hotk->handle, "SFUN", NULL, &temp);
439 if (!ACPI_FAILURE(rv))
440 len += sprintf(page + len, "SFUN value : 0x%04x\n",
441 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100442 /*
443 * Another value for userspace: the ASYM method returns 0x02 for
444 * battery low and 0x04 for battery critical, its readings tend to be
445 * more accurate than those provided by _BST.
446 * Note: since not all the laptops provide this method, errors are
447 * silently ignored.
448 */
Corentin Chary9a816852007-03-11 10:25:38 +0100449 rv = acpi_evaluate_integer(hotk->handle, "ASYM", NULL, &temp);
450 if (!ACPI_FAILURE(rv))
451 len += sprintf(page + len, "ASYM value : 0x%04x\n",
452 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100453 if (asus_info) {
454 snprintf(buf, 16, "%d", asus_info->length);
455 len += sprintf(page + len, "DSDT length : %s\n", buf);
456 snprintf(buf, 16, "%d", asus_info->checksum);
457 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
458 snprintf(buf, 16, "%d", asus_info->revision);
459 len += sprintf(page + len, "DSDT revision : %s\n", buf);
460 snprintf(buf, 7, "%s", asus_info->oem_id);
461 len += sprintf(page + len, "OEM id : %s\n", buf);
462 snprintf(buf, 9, "%s", asus_info->oem_table_id);
463 len += sprintf(page + len, "OEM table id : %s\n", buf);
464 snprintf(buf, 16, "%x", asus_info->oem_revision);
465 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
466 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
467 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
468 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
469 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
470 }
471
472 return len;
473}
474
475static int parse_arg(const char *buf, unsigned long count, int *val)
476{
477 if (!count)
478 return 0;
479 if (count > 31)
480 return -EINVAL;
481 if (sscanf(buf, "%i", val) != 1)
482 return -EINVAL;
483 return count;
484}
485
Corentin Chary4564de12007-01-26 14:04:40 +0100486static ssize_t store_status(const char *buf, size_t count,
Corentin Chary935ffee2007-03-11 10:26:12 +0100487 acpi_handle handle, int mask)
Corentin Chary4564de12007-01-26 14:04:40 +0100488{
489 int rv, value;
490 int out = 0;
491
492 rv = parse_arg(buf, count, &value);
493 if (rv > 0)
494 out = value ? 1 : 0;
495
Corentin Chary935ffee2007-03-11 10:26:12 +0100496 write_status(handle, out, mask);
Corentin Chary4564de12007-01-26 14:04:40 +0100497
498 return rv;
499}
500
501/*
Corentin Chary722ad972007-01-26 14:04:55 +0100502 * LEDD display
503 */
504static ssize_t show_ledd(struct device *dev,
505 struct device_attribute *attr, char *buf)
506{
507 return sprintf(buf, "0x%08x\n", hotk->ledd_status);
508}
509
510static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
511 const char *buf, size_t count)
512{
513 int rv, value;
514
515 rv = parse_arg(buf, count, &value);
516 if (rv > 0) {
517 if (!write_acpi_int(ledd_set_handle, NULL, value, NULL))
518 printk(ASUS_WARNING "LED display write failed\n");
519 else
520 hotk->ledd_status = (u32) value;
521 }
522 return rv;
523}
524
525/*
Corentin Chary4564de12007-01-26 14:04:40 +0100526 * WLAN
527 */
528static ssize_t show_wlan(struct device *dev,
529 struct device_attribute *attr, char *buf)
530{
531 return sprintf(buf, "%d\n", read_status(WL_ON));
532}
533
534static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
535 const char *buf, size_t count)
536{
Corentin Chary935ffee2007-03-11 10:26:12 +0100537 return store_status(buf, count, wl_switch_handle, WL_ON);
Corentin Chary4564de12007-01-26 14:04:40 +0100538}
539
540/*
541 * Bluetooth
542 */
543static ssize_t show_bluetooth(struct device *dev,
544 struct device_attribute *attr, char *buf)
545{
546 return sprintf(buf, "%d\n", read_status(BT_ON));
547}
548
Len Brown8def05f2007-01-30 01:46:43 -0500549static ssize_t store_bluetooth(struct device *dev,
550 struct device_attribute *attr, const char *buf,
551 size_t count)
Corentin Chary4564de12007-01-26 14:04:40 +0100552{
Corentin Chary935ffee2007-03-11 10:26:12 +0100553 return store_status(buf, count, bt_switch_handle, BT_ON);
Corentin Chary4564de12007-01-26 14:04:40 +0100554}
555
Corentin Chary78127b42007-01-26 14:04:49 +0100556/*
557 * Display
558 */
559static void set_display(int value)
560{
561 /* no sanity check needed for now */
562 if (!write_acpi_int(display_set_handle, NULL, value, NULL))
563 printk(ASUS_WARNING "Error setting display\n");
564 return;
565}
566
567static int read_display(void)
568{
Corentin Chary9a816852007-03-11 10:25:38 +0100569 ulong value = 0;
570 acpi_status rv = AE_OK;
Corentin Chary78127b42007-01-26 14:04:49 +0100571
572 /* In most of the case, we know how to set the display, but sometime
573 we can't read it */
Len Brown8def05f2007-01-30 01:46:43 -0500574 if (display_get_handle) {
Corentin Chary9a816852007-03-11 10:25:38 +0100575 rv = acpi_evaluate_integer(display_get_handle, NULL,
576 NULL, &value);
577 if (ACPI_FAILURE(rv))
Corentin Chary78127b42007-01-26 14:04:49 +0100578 printk(ASUS_WARNING "Error reading display status\n");
579 }
580
581 value &= 0x0F; /* needed for some models, shouldn't hurt others */
582
583 return value;
584}
Len Brown8def05f2007-01-30 01:46:43 -0500585
Corentin Chary78127b42007-01-26 14:04:49 +0100586/*
587 * Now, *this* one could be more user-friendly, but so far, no-one has
588 * complained. The significance of bits is the same as in store_disp()
589 */
590static ssize_t show_disp(struct device *dev,
591 struct device_attribute *attr, char *buf)
592{
593 return sprintf(buf, "%d\n", read_display());
594}
595
596/*
597 * Experimental support for display switching. As of now: 1 should activate
598 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
599 * Any combination (bitwise) of these will suffice. I never actually tested 4
600 * displays hooked up simultaneously, so be warned. See the acpi4asus README
601 * for more info.
602 */
603static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
604 const char *buf, size_t count)
605{
606 int rv, value;
607
608 rv = parse_arg(buf, count, &value);
609 if (rv > 0)
610 set_display(value);
611 return rv;
612}
613
Corentin Chary8b857352007-01-26 14:04:58 +0100614/*
615 * Light Sens
616 */
617static void set_light_sens_switch(int value)
618{
619 if (!write_acpi_int(ls_switch_handle, NULL, value, NULL))
620 printk(ASUS_WARNING "Error setting light sensor switch\n");
621 hotk->light_switch = value;
622}
623
624static ssize_t show_lssw(struct device *dev,
625 struct device_attribute *attr, char *buf)
626{
627 return sprintf(buf, "%d\n", hotk->light_switch);
628}
629
630static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
631 const char *buf, size_t count)
632{
633 int rv, value;
634
635 rv = parse_arg(buf, count, &value);
636 if (rv > 0)
637 set_light_sens_switch(value ? 1 : 0);
638
639 return rv;
640}
641
642static void set_light_sens_level(int value)
643{
644 if (!write_acpi_int(ls_level_handle, NULL, value, NULL))
645 printk(ASUS_WARNING "Error setting light sensor level\n");
646 hotk->light_level = value;
647}
648
649static ssize_t show_lslvl(struct device *dev,
650 struct device_attribute *attr, char *buf)
651{
652 return sprintf(buf, "%d\n", hotk->light_level);
653}
654
655static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
656 const char *buf, size_t count)
657{
658 int rv, value;
659
660 rv = parse_arg(buf, count, &value);
661 if (rv > 0) {
662 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
663 /* 0 <= value <= 15 */
664 set_light_sens_level(value);
665 }
666
667 return rv;
668}
669
Corentin Chary85091b72007-01-26 14:04:30 +0100670static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
671{
672 /* TODO Find a better way to handle events count. */
673 if (!hotk)
674 return;
675
Corentin Chary6b7091e2007-01-26 14:04:45 +0100676 /*
677 * We need to tell the backlight device when the backlight power is
678 * switched
679 */
680 if (event == ATKD_LCD_ON) {
Corentin Chary935ffee2007-03-11 10:26:12 +0100681 write_status(NULL, 1, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100682 lcd_blank(FB_BLANK_UNBLANK);
Len Brown8def05f2007-01-30 01:46:43 -0500683 } else if (event == ATKD_LCD_OFF) {
Corentin Chary935ffee2007-03-11 10:26:12 +0100684 write_status(NULL, 0, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100685 lcd_blank(FB_BLANK_POWERDOWN);
686 }
687
Corentin Chary85091b72007-01-26 14:04:30 +0100688 acpi_bus_generate_event(hotk->device, event,
689 hotk->event_count[event % 128]++);
690
691 return;
692}
693
694#define ASUS_CREATE_DEVICE_ATTR(_name) \
695 struct device_attribute dev_attr_##_name = { \
696 .attr = { \
697 .name = __stringify(_name), \
698 .mode = 0, \
699 .owner = THIS_MODULE }, \
700 .show = NULL, \
701 .store = NULL, \
702 }
703
704#define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
705 do { \
706 dev_attr_##_name.attr.mode = _mode; \
707 dev_attr_##_name.show = _show; \
708 dev_attr_##_name.store = _store; \
709 } while(0)
710
711static ASUS_CREATE_DEVICE_ATTR(infos);
Corentin Chary4564de12007-01-26 14:04:40 +0100712static ASUS_CREATE_DEVICE_ATTR(wlan);
713static ASUS_CREATE_DEVICE_ATTR(bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +0100714static ASUS_CREATE_DEVICE_ATTR(display);
Corentin Chary722ad972007-01-26 14:04:55 +0100715static ASUS_CREATE_DEVICE_ATTR(ledd);
Corentin Chary8b857352007-01-26 14:04:58 +0100716static ASUS_CREATE_DEVICE_ATTR(ls_switch);
717static ASUS_CREATE_DEVICE_ATTR(ls_level);
Corentin Chary85091b72007-01-26 14:04:30 +0100718
719static struct attribute *asuspf_attributes[] = {
Len Brown8def05f2007-01-30 01:46:43 -0500720 &dev_attr_infos.attr,
721 &dev_attr_wlan.attr,
722 &dev_attr_bluetooth.attr,
723 &dev_attr_display.attr,
724 &dev_attr_ledd.attr,
725 &dev_attr_ls_switch.attr,
726 &dev_attr_ls_level.attr,
727 NULL
Corentin Chary85091b72007-01-26 14:04:30 +0100728};
729
730static struct attribute_group asuspf_attribute_group = {
Len Brown8def05f2007-01-30 01:46:43 -0500731 .attrs = asuspf_attributes
Corentin Chary85091b72007-01-26 14:04:30 +0100732};
733
734static struct platform_driver asuspf_driver = {
Len Brown8def05f2007-01-30 01:46:43 -0500735 .driver = {
736 .name = ASUS_HOTK_FILE,
737 .owner = THIS_MODULE,
738 }
Corentin Chary85091b72007-01-26 14:04:30 +0100739};
740
741static struct platform_device *asuspf_device;
742
Corentin Chary85091b72007-01-26 14:04:30 +0100743static void asus_hotk_add_fs(void)
744{
745 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
Corentin Chary4564de12007-01-26 14:04:40 +0100746
747 if (wl_switch_handle)
748 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
749
750 if (bt_switch_handle)
751 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
752 show_bluetooth, store_bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +0100753
754 if (display_set_handle && display_get_handle)
755 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
Len Brown8def05f2007-01-30 01:46:43 -0500756 else if (display_set_handle)
Corentin Chary78127b42007-01-26 14:04:49 +0100757 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
758
Corentin Chary722ad972007-01-26 14:04:55 +0100759 if (ledd_set_handle)
760 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
761
Corentin Chary8b857352007-01-26 14:04:58 +0100762 if (ls_switch_handle && ls_level_handle) {
763 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
764 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
765 }
Corentin Chary85091b72007-01-26 14:04:30 +0100766}
767
Len Brown8def05f2007-01-30 01:46:43 -0500768static int asus_handle_init(char *name, acpi_handle * handle,
Corentin Chary85091b72007-01-26 14:04:30 +0100769 char **paths, int num_paths)
770{
771 int i;
772 acpi_status status;
773
774 for (i = 0; i < num_paths; i++) {
775 status = acpi_get_handle(NULL, paths[i], handle);
776 if (ACPI_SUCCESS(status))
777 return 0;
778 }
779
780 *handle = NULL;
781 return -ENODEV;
782}
783
784#define ASUS_HANDLE_INIT(object) \
785 asus_handle_init(#object, &object##_handle, object##_paths, \
786 ARRAY_SIZE(object##_paths))
787
Corentin Chary85091b72007-01-26 14:04:30 +0100788/*
789 * This function is used to initialize the hotk with right values. In this
790 * method, we can make all the detection we want, and modify the hotk struct
791 */
792static int asus_hotk_get_info(void)
793{
794 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Corentin Chary85091b72007-01-26 14:04:30 +0100795 union acpi_object *model = NULL;
Corentin Chary9a816852007-03-11 10:25:38 +0100796 ulong bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +0100797 char *string = NULL;
798 acpi_status status;
799
800 /*
801 * Get DSDT headers early enough to allow for differentiating between
802 * models, but late enough to allow acpi_bus_register_driver() to fail
803 * before doing anything ACPI-specific. Should we encounter a machine,
804 * which needs special handling (i.e. its hotkey device has a different
805 * HID), this bit will be moved. A global variable asus_info contains
806 * the DSDT header.
807 */
Len Brown894d79b2007-02-03 02:13:53 -0500808 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
Corentin Chary85091b72007-01-26 14:04:30 +0100809 if (ACPI_FAILURE(status))
810 printk(ASUS_WARNING "Couldn't get the DSDT table header\n");
Corentin Chary85091b72007-01-26 14:04:30 +0100811
812 /* We have to write 0 on init this far for all ASUS models */
813 if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
814 printk(ASUS_ERR "Hotkey initialization failed\n");
815 return -ENODEV;
816 }
817
818 /* This needs to be called for some laptops to init properly */
Corentin Chary9a816852007-03-11 10:25:38 +0100819 status =
820 acpi_evaluate_integer(hotk->handle, "BSTS", NULL, &bsts_result);
821 if (ACPI_FAILURE(status))
Corentin Chary85091b72007-01-26 14:04:30 +0100822 printk(ASUS_WARNING "Error calling BSTS\n");
823 else if (bsts_result)
824 printk(ASUS_NOTICE "BSTS called, 0x%02x returned\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100825 (uint) bsts_result);
Corentin Chary85091b72007-01-26 14:04:30 +0100826
Corentin Chary185e5af2007-03-11 10:27:33 +0100827 /* This too ... */
828 write_acpi_int(hotk->handle, "CWAP", wapf, NULL);
829
Corentin Chary85091b72007-01-26 14:04:30 +0100830 /*
831 * Try to match the object returned by INIT to the specific model.
832 * Handle every possible object (or the lack of thereof) the DSDT
833 * writers might throw at us. When in trouble, we pass NULL to
834 * asus_model_match() and try something completely different.
835 */
836 if (buffer.pointer) {
837 model = buffer.pointer;
838 switch (model->type) {
839 case ACPI_TYPE_STRING:
840 string = model->string.pointer;
841 break;
842 case ACPI_TYPE_BUFFER:
843 string = model->buffer.pointer;
844 break;
845 default:
846 string = "";
847 break;
848 }
849 }
850 hotk->name = kstrdup(string, GFP_KERNEL);
851 if (!hotk->name)
852 return -ENOMEM;
853
Len Brown8def05f2007-01-30 01:46:43 -0500854 if (*string)
Corentin Chary85091b72007-01-26 14:04:30 +0100855 printk(ASUS_NOTICE " %s model detected\n", string);
856
Corentin Charybe18cda2007-01-26 14:04:35 +0100857 ASUS_HANDLE_INIT(mled_set);
858 ASUS_HANDLE_INIT(tled_set);
859 ASUS_HANDLE_INIT(rled_set);
860 ASUS_HANDLE_INIT(pled_set);
Corentin Charyfdd8d082007-03-11 10:26:48 +0100861 ASUS_HANDLE_INIT(gled_set);
Corentin Charybe18cda2007-01-26 14:04:35 +0100862
Corentin Chary722ad972007-01-26 14:04:55 +0100863 ASUS_HANDLE_INIT(ledd_set);
864
Corentin Chary4564de12007-01-26 14:04:40 +0100865 /*
866 * The HWRS method return informations about the hardware.
867 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
868 * The significance of others is yet to be found.
869 * If we don't find the method, we assume the device are present.
870 */
Corentin Chary9a816852007-03-11 10:25:38 +0100871 status =
872 acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &hwrs_result);
873 if (ACPI_FAILURE(status))
Corentin Chary4564de12007-01-26 14:04:40 +0100874 hwrs_result = WL_HWRS | BT_HWRS;
875
Len Brown8def05f2007-01-30 01:46:43 -0500876 if (hwrs_result & WL_HWRS)
Corentin Chary4564de12007-01-26 14:04:40 +0100877 ASUS_HANDLE_INIT(wl_switch);
Len Brown8def05f2007-01-30 01:46:43 -0500878 if (hwrs_result & BT_HWRS)
Corentin Chary4564de12007-01-26 14:04:40 +0100879 ASUS_HANDLE_INIT(bt_switch);
880
881 ASUS_HANDLE_INIT(wireless_status);
882
Corentin Chary6b7091e2007-01-26 14:04:45 +0100883 ASUS_HANDLE_INIT(brightness_set);
884 ASUS_HANDLE_INIT(brightness_get);
885
886 ASUS_HANDLE_INIT(lcd_switch);
887
Corentin Chary78127b42007-01-26 14:04:49 +0100888 ASUS_HANDLE_INIT(display_set);
889 ASUS_HANDLE_INIT(display_get);
890
Corentin Chary8b857352007-01-26 14:04:58 +0100891 /* There is a lot of models with "ALSL", but a few get
892 a real light sens, so we need to check it. */
Len Brown8def05f2007-01-30 01:46:43 -0500893 if (ASUS_HANDLE_INIT(ls_switch))
Corentin Chary8b857352007-01-26 14:04:58 +0100894 ASUS_HANDLE_INIT(ls_level);
895
Corentin Chary85091b72007-01-26 14:04:30 +0100896 kfree(model);
897
898 return AE_OK;
899}
900
901static int asus_hotk_check(void)
902{
903 int result = 0;
904
905 result = acpi_bus_get_status(hotk->device);
906 if (result)
907 return result;
908
909 if (hotk->device->status.present) {
910 result = asus_hotk_get_info();
911 } else {
912 printk(ASUS_ERR "Hotkey device not present, aborting\n");
913 return -EINVAL;
914 }
915
916 return result;
917}
918
919static int asus_hotk_found;
920
921static int asus_hotk_add(struct acpi_device *device)
922{
923 acpi_status status = AE_OK;
924 int result;
925
926 if (!device)
927 return -EINVAL;
928
929 printk(ASUS_NOTICE "Asus Laptop Support version %s\n",
930 ASUS_LAPTOP_VERSION);
931
932 hotk = kmalloc(sizeof(struct asus_hotk), GFP_KERNEL);
933 if (!hotk)
934 return -ENOMEM;
935 memset(hotk, 0, sizeof(struct asus_hotk));
936
937 hotk->handle = device->handle;
938 strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
939 strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
940 acpi_driver_data(device) = hotk;
941 hotk->device = device;
942
943 result = asus_hotk_check();
944 if (result)
945 goto end;
946
947 asus_hotk_add_fs();
948
949 /*
950 * We install the handler, it will receive the hotk in parameter, so, we
951 * could add other data to the hotk struct
952 */
Corentin Charya9dbcb32007-05-06 14:46:41 +0200953 status = acpi_install_notify_handler(hotk->handle, ACPI_ALL_NOTIFY,
Corentin Chary85091b72007-01-26 14:04:30 +0100954 asus_hotk_notify, hotk);
955 if (ACPI_FAILURE(status))
956 printk(ASUS_ERR "Error installing notify handler\n");
957
958 asus_hotk_found = 1;
959
Corentin Chary6b7091e2007-01-26 14:04:45 +0100960 /* WLED and BLED are on by default */
Corentin Chary935ffee2007-03-11 10:26:12 +0100961 write_status(bt_switch_handle, 1, BT_ON);
962 write_status(wl_switch_handle, 1, WL_ON);
963
964 /* If the h/w switch is off, we need to check the real status */
965 write_status(NULL, read_status(BT_ON), BT_ON);
966 write_status(NULL, read_status(WL_ON), WL_ON);
Corentin Chary4564de12007-01-26 14:04:40 +0100967
Corentin Chary6b7091e2007-01-26 14:04:45 +0100968 /* LCD Backlight is on by default */
Corentin Chary935ffee2007-03-11 10:26:12 +0100969 write_status(NULL, 1, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100970
Corentin Chary722ad972007-01-26 14:04:55 +0100971 /* LED display is off by default */
972 hotk->ledd_status = 0xFFF;
973
Len Brown8def05f2007-01-30 01:46:43 -0500974 /* Set initial values of light sensor and level */
975 hotk->light_switch = 1; /* Default to light sensor disabled */
976 hotk->light_level = 0; /* level 5 for sensor sensitivity */
Corentin Chary8b857352007-01-26 14:04:58 +0100977
Len Brown8def05f2007-01-30 01:46:43 -0500978 if (ls_switch_handle)
979 set_light_sens_switch(hotk->light_switch);
Corentin Chary8b857352007-01-26 14:04:58 +0100980
Len Brown8def05f2007-01-30 01:46:43 -0500981 if (ls_level_handle)
982 set_light_sens_level(hotk->light_level);
Corentin Chary8b857352007-01-26 14:04:58 +0100983
Corentin Chary85091b72007-01-26 14:04:30 +0100984 end:
985 if (result) {
986 kfree(hotk->name);
987 kfree(hotk);
988 }
989
990 return result;
991}
992
993static int asus_hotk_remove(struct acpi_device *device, int type)
994{
995 acpi_status status = 0;
996
997 if (!device || !acpi_driver_data(device))
998 return -EINVAL;
999
Corentin Charya9dbcb32007-05-06 14:46:41 +02001000 status = acpi_remove_notify_handler(hotk->handle, ACPI_ALL_NOTIFY,
Corentin Chary85091b72007-01-26 14:04:30 +01001001 asus_hotk_notify);
1002 if (ACPI_FAILURE(status))
1003 printk(ASUS_ERR "Error removing notify handler\n");
1004
1005 kfree(hotk->name);
1006 kfree(hotk);
1007
1008 return 0;
1009}
1010
Corentin Chary6b7091e2007-01-26 14:04:45 +01001011static void asus_backlight_exit(void)
1012{
Len Brown8def05f2007-01-30 01:46:43 -05001013 if (asus_backlight_device)
Corentin Chary6b7091e2007-01-26 14:04:45 +01001014 backlight_device_unregister(asus_backlight_device);
1015}
1016
Corentin Charybe18cda2007-01-26 14:04:35 +01001017#define ASUS_LED_UNREGISTER(object) \
1018 if(object##_led.class_dev \
1019 && !IS_ERR(object##_led.class_dev)) \
1020 led_classdev_unregister(&object##_led)
1021
1022static void asus_led_exit(void)
1023{
1024 ASUS_LED_UNREGISTER(mled);
1025 ASUS_LED_UNREGISTER(tled);
1026 ASUS_LED_UNREGISTER(pled);
1027 ASUS_LED_UNREGISTER(rled);
Corentin Charyfdd8d082007-03-11 10:26:48 +01001028 ASUS_LED_UNREGISTER(gled);
Corentin Charybe18cda2007-01-26 14:04:35 +01001029
1030 destroy_workqueue(led_workqueue);
1031}
1032
Corentin Chary85091b72007-01-26 14:04:30 +01001033static void __exit asus_laptop_exit(void)
1034{
Corentin Chary6b7091e2007-01-26 14:04:45 +01001035 asus_backlight_exit();
Corentin Charybe18cda2007-01-26 14:04:35 +01001036 asus_led_exit();
1037
Corentin Chary85091b72007-01-26 14:04:30 +01001038 acpi_bus_unregister_driver(&asus_hotk_driver);
Len Brown8def05f2007-01-30 01:46:43 -05001039 sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
1040 platform_device_unregister(asuspf_device);
1041 platform_driver_unregister(&asuspf_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001042}
1043
Len Brown8def05f2007-01-30 01:46:43 -05001044static int asus_backlight_init(struct device *dev)
Corentin Chary6b7091e2007-01-26 14:04:45 +01001045{
1046 struct backlight_device *bd;
1047
Len Brown8def05f2007-01-30 01:46:43 -05001048 if (brightness_set_handle && lcd_switch_handle) {
1049 bd = backlight_device_register(ASUS_HOTK_FILE, dev,
Richard Purdie599a52d2007-02-10 23:07:48 +00001050 NULL, &asusbl_ops);
Len Brown8def05f2007-01-30 01:46:43 -05001051 if (IS_ERR(bd)) {
Corentin Chary6b7091e2007-01-26 14:04:45 +01001052 printk(ASUS_ERR
1053 "Could not register asus backlight device\n");
1054 asus_backlight_device = NULL;
1055 return PTR_ERR(bd);
1056 }
1057
1058 asus_backlight_device = bd;
1059
Richard Purdie599a52d2007-02-10 23:07:48 +00001060 bd->props.max_brightness = 15;
1061 bd->props.brightness = read_brightness(NULL);
1062 bd->props.power = FB_BLANK_UNBLANK;
Richard Purdie28ee0862007-02-08 22:25:09 +00001063 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001064 }
1065 return 0;
1066}
1067
Corentin Charybe18cda2007-01-26 14:04:35 +01001068static int asus_led_register(acpi_handle handle,
Len Brown8def05f2007-01-30 01:46:43 -05001069 struct led_classdev *ldev, struct device *dev)
Corentin Charybe18cda2007-01-26 14:04:35 +01001070{
Len Brown8def05f2007-01-30 01:46:43 -05001071 if (!handle)
Corentin Charybe18cda2007-01-26 14:04:35 +01001072 return 0;
1073
1074 return led_classdev_register(dev, ldev);
1075}
Len Brown8def05f2007-01-30 01:46:43 -05001076
Corentin Charybe18cda2007-01-26 14:04:35 +01001077#define ASUS_LED_REGISTER(object, device) \
1078 asus_led_register(object##_set_handle, &object##_led, device)
1079
Len Brown8def05f2007-01-30 01:46:43 -05001080static int asus_led_init(struct device *dev)
Corentin Charybe18cda2007-01-26 14:04:35 +01001081{
1082 int rv;
1083
1084 rv = ASUS_LED_REGISTER(mled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001085 if (rv)
Corentin Charybe18cda2007-01-26 14:04:35 +01001086 return rv;
1087
1088 rv = ASUS_LED_REGISTER(tled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001089 if (rv)
Corentin Charybe18cda2007-01-26 14:04:35 +01001090 return rv;
1091
1092 rv = ASUS_LED_REGISTER(rled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001093 if (rv)
Corentin Charybe18cda2007-01-26 14:04:35 +01001094 return rv;
1095
1096 rv = ASUS_LED_REGISTER(pled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001097 if (rv)
Corentin Charybe18cda2007-01-26 14:04:35 +01001098 return rv;
1099
Corentin Charyfdd8d082007-03-11 10:26:48 +01001100 rv = ASUS_LED_REGISTER(gled, dev);
1101 if (rv)
1102 return rv;
1103
Corentin Charybe18cda2007-01-26 14:04:35 +01001104 led_workqueue = create_singlethread_workqueue("led_workqueue");
Len Brown8def05f2007-01-30 01:46:43 -05001105 if (!led_workqueue)
Corentin Charybe18cda2007-01-26 14:04:35 +01001106 return -ENOMEM;
1107
1108 return 0;
1109}
1110
Corentin Chary85091b72007-01-26 14:04:30 +01001111static int __init asus_laptop_init(void)
1112{
Corentin Charybe18cda2007-01-26 14:04:35 +01001113 struct device *dev;
Corentin Chary85091b72007-01-26 14:04:30 +01001114 int result;
1115
1116 if (acpi_disabled)
1117 return -ENODEV;
1118
Corentin Chary85091b72007-01-26 14:04:30 +01001119 result = acpi_bus_register_driver(&asus_hotk_driver);
1120 if (result < 0)
1121 return result;
1122
1123 /*
1124 * This is a bit of a kludge. We only want this module loaded
1125 * for ASUS systems, but there's currently no way to probe the
1126 * ACPI namespace for ASUS HIDs. So we just return failure if
1127 * we didn't find one, which will cause the module to be
1128 * unloaded.
1129 */
1130 if (!asus_hotk_found) {
1131 acpi_bus_unregister_driver(&asus_hotk_driver);
1132 return -ENODEV;
1133 }
1134
Corentin Charybe18cda2007-01-26 14:04:35 +01001135 dev = acpi_get_physical_device(hotk->device->handle);
1136
Corentin Chary6b7091e2007-01-26 14:04:45 +01001137 result = asus_backlight_init(dev);
Len Brown8def05f2007-01-30 01:46:43 -05001138 if (result)
Corentin Chary6b7091e2007-01-26 14:04:45 +01001139 goto fail_backlight;
1140
Corentin Charybe18cda2007-01-26 14:04:35 +01001141 result = asus_led_init(dev);
Len Brown8def05f2007-01-30 01:46:43 -05001142 if (result)
Corentin Charybe18cda2007-01-26 14:04:35 +01001143 goto fail_led;
1144
Len Brown8def05f2007-01-30 01:46:43 -05001145 /* Register platform stuff */
Corentin Chary85091b72007-01-26 14:04:30 +01001146 result = platform_driver_register(&asuspf_driver);
Len Brown8def05f2007-01-30 01:46:43 -05001147 if (result)
1148 goto fail_platform_driver;
Corentin Chary85091b72007-01-26 14:04:30 +01001149
Len Brown8def05f2007-01-30 01:46:43 -05001150 asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
1151 if (!asuspf_device) {
1152 result = -ENOMEM;
1153 goto fail_platform_device1;
1154 }
Corentin Chary85091b72007-01-26 14:04:30 +01001155
Len Brown8def05f2007-01-30 01:46:43 -05001156 result = platform_device_add(asuspf_device);
1157 if (result)
1158 goto fail_platform_device2;
Corentin Chary85091b72007-01-26 14:04:30 +01001159
Len Brown8def05f2007-01-30 01:46:43 -05001160 result = sysfs_create_group(&asuspf_device->dev.kobj,
Corentin Chary85091b72007-01-26 14:04:30 +01001161 &asuspf_attribute_group);
Len Brown8def05f2007-01-30 01:46:43 -05001162 if (result)
1163 goto fail_sysfs;
Corentin Chary85091b72007-01-26 14:04:30 +01001164
Len Brown8def05f2007-01-30 01:46:43 -05001165 return 0;
Corentin Chary85091b72007-01-26 14:04:30 +01001166
Len Brown8def05f2007-01-30 01:46:43 -05001167 fail_sysfs:
1168 platform_device_del(asuspf_device);
Corentin Chary85091b72007-01-26 14:04:30 +01001169
Len Brown8def05f2007-01-30 01:46:43 -05001170 fail_platform_device2:
Corentin Chary85091b72007-01-26 14:04:30 +01001171 platform_device_put(asuspf_device);
1172
Len Brown8def05f2007-01-30 01:46:43 -05001173 fail_platform_device1:
1174 platform_driver_unregister(&asuspf_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001175
Len Brown8def05f2007-01-30 01:46:43 -05001176 fail_platform_driver:
Corentin Charybe18cda2007-01-26 14:04:35 +01001177 asus_led_exit();
1178
Len Brown8def05f2007-01-30 01:46:43 -05001179 fail_led:
Corentin Chary6b7091e2007-01-26 14:04:45 +01001180 asus_backlight_exit();
1181
Len Brown8def05f2007-01-30 01:46:43 -05001182 fail_backlight:
Corentin Chary85091b72007-01-26 14:04:30 +01001183
1184 return result;
1185}
1186
1187module_init(asus_laptop_init);
1188module_exit(asus_laptop_exit);