blob: 40db9f70148f4b1035861a416224d4b1606bf816 [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
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 Charyf3985322007-05-06 14:48:22 +020051#define ASUS_LAPTOP_VERSION "0.42"
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"
Corentin Chary85091b72007-01-26 14:04:30 +010056#define ASUS_HOTK_FILE "asus-laptop"
57#define ASUS_HOTK_PREFIX "\\_SB.ATKD."
58
Corentin Charybe18cda2007-01-26 14:04:35 +010059/*
Corentin Chary6b7091e2007-01-26 14:04:45 +010060 * Some events we use, same for all Asus
61 */
62#define ATKD_BR_UP 0x10
63#define ATKD_BR_DOWN 0x20
64#define ATKD_LCD_ON 0x33
65#define ATKD_LCD_OFF 0x34
66
67/*
Corentin Chary4564de12007-01-26 14:04:40 +010068 * Known bits returned by \_SB.ATKD.HWRS
Corentin Charybe18cda2007-01-26 14:04:35 +010069 */
Corentin Chary4564de12007-01-26 14:04:40 +010070#define WL_HWRS 0x80
71#define BT_HWRS 0x100
72
73/*
74 * Flags for hotk status
75 * WL_ON and BT_ON are also used for wireless_status()
76 */
77#define WL_ON 0x01 //internal Wifi
78#define BT_ON 0x02 //internal Bluetooth
Corentin Charybe18cda2007-01-26 14:04:35 +010079#define MLED_ON 0x04 //mail LED
80#define TLED_ON 0x08 //touchpad LED
Len Brown8def05f2007-01-30 01:46:43 -050081#define RLED_ON 0x10 //Record LED
82#define PLED_ON 0x20 //Phone LED
Corentin Charyfdd8d082007-03-11 10:26:48 +010083#define GLED_ON 0x40 //Gaming LED
84#define LCD_ON 0x80 //LCD backlight
Corentin Charyf3985322007-05-06 14:48:22 +020085#define GPS_ON 0x100 //GPS
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 Charyf3985322007-05-06 14:48:22 +0200151 S5A M5A z33A W1Jc W2V G1 */
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 Charye539c2f2007-05-06 14:47:06 +0200165/* GPS */
166/* R2H use different handle for GPS on/off */
Corentin Charyf3985322007-05-06 14:48:22 +0200167ASUS_HANDLE(gps_on, ASUS_HOTK_PREFIX "SDON"); /* R2H */
168ASUS_HANDLE(gps_off, ASUS_HOTK_PREFIX "SDOF"); /* R2H */
Corentin Charye539c2f2007-05-06 14:47:06 +0200169ASUS_HANDLE(gps_status, ASUS_HOTK_PREFIX "GPST");
170
Corentin Chary85091b72007-01-26 14:04:30 +0100171/*
172 * This is the main structure, we can use it to store anything interesting
173 * about the hotk device
174 */
175struct asus_hotk {
Len Brown8def05f2007-01-30 01:46:43 -0500176 char *name; //laptop name
Corentin Chary85091b72007-01-26 14:04:30 +0100177 struct acpi_device *device; //the device we are in
178 acpi_handle handle; //the handle of the hotk device
179 char status; //status of the hotk, for LEDs, ...
Corentin Chary722ad972007-01-26 14:04:55 +0100180 u32 ledd_status; //status of the LED display
Len Brown8def05f2007-01-30 01:46:43 -0500181 u8 light_level; //light sensor level
182 u8 light_switch; //light sensor switch value
Corentin Chary85091b72007-01-26 14:04:30 +0100183 u16 event_count[128]; //count for each event TODO make this better
184};
185
186/*
187 * This header is made available to allow proper configuration given model,
188 * revision number , ... this info cannot go in struct asus_hotk because it is
189 * available before the hotk
190 */
191static struct acpi_table_header *asus_info;
192
193/* The actual device the driver binds to */
194static struct asus_hotk *hotk;
195
196/*
197 * The hotkey driver declaration
198 */
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200199static const struct acpi_device_id asus_device_ids[] = {
200 {"ATK0100", 0},
201 {"", 0},
202};
203MODULE_DEVICE_TABLE(acpi, asus_device_ids);
204
Corentin Chary85091b72007-01-26 14:04:30 +0100205static int asus_hotk_add(struct acpi_device *device);
206static int asus_hotk_remove(struct acpi_device *device, int type);
207static struct acpi_driver asus_hotk_driver = {
208 .name = ASUS_HOTK_NAME,
209 .class = ASUS_HOTK_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200210 .ids = asus_device_ids,
Corentin Chary85091b72007-01-26 14:04:30 +0100211 .ops = {
212 .add = asus_hotk_add,
213 .remove = asus_hotk_remove,
214 },
215};
216
Corentin Chary6b7091e2007-01-26 14:04:45 +0100217/* The backlight device /sys/class/backlight */
218static struct backlight_device *asus_backlight_device;
219
220/*
221 * The backlight class declaration
222 */
223static int read_brightness(struct backlight_device *bd);
224static int update_bl_status(struct backlight_device *bd);
Richard Purdie599a52d2007-02-10 23:07:48 +0000225static struct backlight_ops asusbl_ops = {
Len Brown8def05f2007-01-30 01:46:43 -0500226 .get_brightness = read_brightness,
227 .update_status = update_bl_status,
Corentin Chary6b7091e2007-01-26 14:04:45 +0100228};
229
Corentin Charybe18cda2007-01-26 14:04:35 +0100230/* These functions actually update the LED's, and are called from a
231 * workqueue. By doing this as separate work rather than when the LED
232 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
233 * potentially bad time, such as a timer interrupt. */
234static struct workqueue_struct *led_workqueue;
235
236#define ASUS_LED(object, ledname) \
237 static void object##_led_set(struct led_classdev *led_cdev, \
238 enum led_brightness value); \
239 static void object##_led_update(struct work_struct *ignored); \
240 static int object##_led_wk; \
Adrian Bunkf110ef52007-02-20 01:07:25 +0100241 static DECLARE_WORK(object##_led_work, object##_led_update); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100242 static struct led_classdev object##_led = { \
243 .name = "asus:" ledname, \
244 .brightness_set = object##_led_set, \
245 }
246
247ASUS_LED(mled, "mail");
248ASUS_LED(tled, "touchpad");
249ASUS_LED(rled, "record");
250ASUS_LED(pled, "phone");
Corentin Charyfdd8d082007-03-11 10:26:48 +0100251ASUS_LED(gled, "gaming");
Corentin Charybe18cda2007-01-26 14:04:35 +0100252
Corentin Chary85091b72007-01-26 14:04:30 +0100253/*
254 * This function evaluates an ACPI method, given an int as parameter, the
255 * method is searched within the scope of the handle, can be NULL. The output
256 * of the method is written is output, which can also be NULL
257 *
258 * returns 1 if write is successful, 0 else.
259 */
260static int write_acpi_int(acpi_handle handle, const char *method, int val,
261 struct acpi_buffer *output)
262{
263 struct acpi_object_list params; //list of input parameters (an int here)
264 union acpi_object in_obj; //the only param we use
265 acpi_status status;
266
267 params.count = 1;
268 params.pointer = &in_obj;
269 in_obj.type = ACPI_TYPE_INTEGER;
270 in_obj.integer.value = val;
271
272 status = acpi_evaluate_object(handle, (char *)method, &params, output);
273 return (status == AE_OK);
274}
275
Len Brown8def05f2007-01-30 01:46:43 -0500276static int read_wireless_status(int mask)
277{
Corentin Chary9a816852007-03-11 10:25:38 +0100278 ulong status;
279 acpi_status rv = AE_OK;
Corentin Chary4564de12007-01-26 14:04:40 +0100280
281 if (!wireless_status_handle)
282 return (hotk->status & mask) ? 1 : 0;
283
Corentin Chary9a816852007-03-11 10:25:38 +0100284 rv = acpi_evaluate_integer(wireless_status_handle, NULL, NULL, &status);
285 if (ACPI_FAILURE(rv))
Corentin Chary4564de12007-01-26 14:04:40 +0100286 printk(ASUS_WARNING "Error reading Wireless status\n");
Corentin Chary9a816852007-03-11 10:25:38 +0100287 else
288 return (status & mask) ? 1 : 0;
Corentin Chary4564de12007-01-26 14:04:40 +0100289
290 return (hotk->status & mask) ? 1 : 0;
291}
292
Corentin Charyf3985322007-05-06 14:48:22 +0200293static int read_gps_status(void)
294{
295 ulong status;
Corentin Charye539c2f2007-05-06 14:47:06 +0200296 acpi_status rv = AE_OK;
297
298 rv = acpi_evaluate_integer(gps_status_handle, NULL, NULL, &status);
299 if (ACPI_FAILURE(rv))
Corentin Charyf3985322007-05-06 14:48:22 +0200300 printk(ASUS_WARNING "Error reading GPS status\n");
Corentin Charye539c2f2007-05-06 14:47:06 +0200301 else
Corentin Charyf3985322007-05-06 14:48:22 +0200302 return status ? 1 : 0;
Corentin Charye539c2f2007-05-06 14:47:06 +0200303
Corentin Charyf3985322007-05-06 14:48:22 +0200304 return (hotk->status & GPS_ON) ? 1 : 0;
Corentin Charye539c2f2007-05-06 14:47:06 +0200305}
306
Corentin Charybe18cda2007-01-26 14:04:35 +0100307/* Generic LED functions */
308static int read_status(int mask)
309{
Corentin Chary4564de12007-01-26 14:04:40 +0100310 /* There is a special method for both wireless devices */
311 if (mask == BT_ON || mask == WL_ON)
312 return read_wireless_status(mask);
Corentin Charyf3985322007-05-06 14:48:22 +0200313 else if (mask == GPS_ON)
314 return read_gps_status();
Corentin Chary4564de12007-01-26 14:04:40 +0100315
Corentin Charybe18cda2007-01-26 14:04:35 +0100316 return (hotk->status & mask) ? 1 : 0;
317}
318
Corentin Chary935ffee2007-03-11 10:26:12 +0100319static void write_status(acpi_handle handle, int out, int mask)
Corentin Charybe18cda2007-01-26 14:04:35 +0100320{
321 hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
322
Corentin Chary935ffee2007-03-11 10:26:12 +0100323 switch (mask) {
324 case MLED_ON:
Corentin Charybe18cda2007-01-26 14:04:35 +0100325 out = !out & 0x1;
Corentin Chary935ffee2007-03-11 10:26:12 +0100326 break;
Corentin Charyfdd8d082007-03-11 10:26:48 +0100327 case GLED_ON:
328 out = (out & 0x1) + 1;
329 break;
Corentin Charye539c2f2007-05-06 14:47:06 +0200330 case GPS_ON:
331 handle = (out) ? gps_on_handle : gps_off_handle;
332 out = 0x02;
333 break;
Corentin Chary935ffee2007-03-11 10:26:12 +0100334 default:
335 out &= 0x1;
336 break;
337 }
Corentin Charybe18cda2007-01-26 14:04:35 +0100338
339 if (handle && !write_acpi_int(handle, NULL, out, NULL))
Corentin Chary935ffee2007-03-11 10:26:12 +0100340 printk(ASUS_WARNING " write failed %x\n", mask);
Corentin Charybe18cda2007-01-26 14:04:35 +0100341}
342
343/* /sys/class/led handlers */
Corentin Chary935ffee2007-03-11 10:26:12 +0100344#define ASUS_LED_HANDLER(object, mask) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100345 static void object##_led_set(struct led_classdev *led_cdev, \
346 enum led_brightness value) \
347 { \
348 object##_led_wk = value; \
349 queue_work(led_workqueue, &object##_led_work); \
350 } \
351 static void object##_led_update(struct work_struct *ignored) \
352 { \
353 int value = object##_led_wk; \
Corentin Chary935ffee2007-03-11 10:26:12 +0100354 write_status(object##_set_handle, value, (mask)); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100355 }
356
Corentin Chary935ffee2007-03-11 10:26:12 +0100357ASUS_LED_HANDLER(mled, MLED_ON);
358ASUS_LED_HANDLER(pled, PLED_ON);
359ASUS_LED_HANDLER(rled, RLED_ON);
360ASUS_LED_HANDLER(tled, TLED_ON);
Corentin Charyfdd8d082007-03-11 10:26:48 +0100361ASUS_LED_HANDLER(gled, GLED_ON);
Corentin Charybe18cda2007-01-26 14:04:35 +0100362
Corentin Chary6b7091e2007-01-26 14:04:45 +0100363static int get_lcd_state(void)
364{
365 return read_status(LCD_ON);
366}
367
368static int set_lcd_state(int value)
369{
370 int lcd = 0;
371 acpi_status status = 0;
372
373 lcd = value ? 1 : 0;
374
375 if (lcd == get_lcd_state())
376 return 0;
377
Len Brown8def05f2007-01-30 01:46:43 -0500378 if (lcd_switch_handle) {
Corentin Chary6b7091e2007-01-26 14:04:45 +0100379 status = acpi_evaluate_object(lcd_switch_handle,
380 NULL, NULL, NULL);
381
382 if (ACPI_FAILURE(status))
383 printk(ASUS_WARNING "Error switching LCD\n");
384 }
385
Corentin Chary935ffee2007-03-11 10:26:12 +0100386 write_status(NULL, lcd, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100387 return 0;
388}
389
390static void lcd_blank(int blank)
391{
392 struct backlight_device *bd = asus_backlight_device;
393
Len Brown8def05f2007-01-30 01:46:43 -0500394 if (bd) {
Richard Purdie599a52d2007-02-10 23:07:48 +0000395 bd->props.power = blank;
Richard Purdie28ee0862007-02-08 22:25:09 +0000396 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100397 }
398}
399
400static int read_brightness(struct backlight_device *bd)
401{
Corentin Chary9a816852007-03-11 10:25:38 +0100402 ulong value;
403 acpi_status rv = AE_OK;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100404
Corentin Chary9a816852007-03-11 10:25:38 +0100405 rv = acpi_evaluate_integer(brightness_get_handle, NULL, NULL, &value);
406 if (ACPI_FAILURE(rv))
Corentin Chary6b7091e2007-01-26 14:04:45 +0100407 printk(ASUS_WARNING "Error reading brightness\n");
408
409 return value;
410}
411
412static int set_brightness(struct backlight_device *bd, int value)
413{
414 int ret = 0;
415
416 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
417 /* 0 <= value <= 15 */
418
419 if (!write_acpi_int(brightness_set_handle, NULL, value, NULL)) {
420 printk(ASUS_WARNING "Error changing brightness\n");
421 ret = -EIO;
422 }
423
424 return ret;
425}
426
427static int update_bl_status(struct backlight_device *bd)
428{
429 int rv;
Richard Purdie599a52d2007-02-10 23:07:48 +0000430 int value = bd->props.brightness;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100431
432 rv = set_brightness(bd, value);
Len Brown8def05f2007-01-30 01:46:43 -0500433 if (rv)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100434 return rv;
435
Richard Purdie599a52d2007-02-10 23:07:48 +0000436 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100437 return set_lcd_state(value);
438}
439
Corentin Chary85091b72007-01-26 14:04:30 +0100440/*
441 * Platform device handlers
442 */
443
444/*
445 * We write our info in page, we begin at offset off and cannot write more
446 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
447 * number of bytes written in page
448 */
449static ssize_t show_infos(struct device *dev,
Len Brown8def05f2007-01-30 01:46:43 -0500450 struct device_attribute *attr, char *page)
Corentin Chary85091b72007-01-26 14:04:30 +0100451{
452 int len = 0;
Corentin Chary9a816852007-03-11 10:25:38 +0100453 ulong temp;
Corentin Chary85091b72007-01-26 14:04:30 +0100454 char buf[16]; //enough for all info
Corentin Chary9a816852007-03-11 10:25:38 +0100455 acpi_status rv = AE_OK;
456
Corentin Chary85091b72007-01-26 14:04:30 +0100457 /*
458 * We use the easy way, we don't care of off and count, so we don't set eof
459 * to 1
460 */
461
462 len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
463 len += sprintf(page + len, "Model reference : %s\n", hotk->name);
464 /*
465 * The SFUN method probably allows the original driver to get the list
466 * of features supported by a given model. For now, 0x0100 or 0x0800
467 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
468 * The significance of others is yet to be found.
469 */
Corentin Chary9a816852007-03-11 10:25:38 +0100470 rv = acpi_evaluate_integer(hotk->handle, "SFUN", NULL, &temp);
471 if (!ACPI_FAILURE(rv))
472 len += sprintf(page + len, "SFUN value : 0x%04x\n",
473 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100474 /*
475 * Another value for userspace: the ASYM method returns 0x02 for
476 * battery low and 0x04 for battery critical, its readings tend to be
477 * more accurate than those provided by _BST.
478 * Note: since not all the laptops provide this method, errors are
479 * silently ignored.
480 */
Corentin Chary9a816852007-03-11 10:25:38 +0100481 rv = acpi_evaluate_integer(hotk->handle, "ASYM", NULL, &temp);
482 if (!ACPI_FAILURE(rv))
483 len += sprintf(page + len, "ASYM value : 0x%04x\n",
484 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100485 if (asus_info) {
486 snprintf(buf, 16, "%d", asus_info->length);
487 len += sprintf(page + len, "DSDT length : %s\n", buf);
488 snprintf(buf, 16, "%d", asus_info->checksum);
489 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
490 snprintf(buf, 16, "%d", asus_info->revision);
491 len += sprintf(page + len, "DSDT revision : %s\n", buf);
492 snprintf(buf, 7, "%s", asus_info->oem_id);
493 len += sprintf(page + len, "OEM id : %s\n", buf);
494 snprintf(buf, 9, "%s", asus_info->oem_table_id);
495 len += sprintf(page + len, "OEM table id : %s\n", buf);
496 snprintf(buf, 16, "%x", asus_info->oem_revision);
497 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
498 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
499 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
500 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
501 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
502 }
503
504 return len;
505}
506
507static int parse_arg(const char *buf, unsigned long count, int *val)
508{
509 if (!count)
510 return 0;
511 if (count > 31)
512 return -EINVAL;
513 if (sscanf(buf, "%i", val) != 1)
514 return -EINVAL;
515 return count;
516}
517
Corentin Chary4564de12007-01-26 14:04:40 +0100518static ssize_t store_status(const char *buf, size_t count,
Corentin Chary935ffee2007-03-11 10:26:12 +0100519 acpi_handle handle, int mask)
Corentin Chary4564de12007-01-26 14:04:40 +0100520{
521 int rv, value;
522 int out = 0;
523
524 rv = parse_arg(buf, count, &value);
525 if (rv > 0)
526 out = value ? 1 : 0;
527
Corentin Chary935ffee2007-03-11 10:26:12 +0100528 write_status(handle, out, mask);
Corentin Chary4564de12007-01-26 14:04:40 +0100529
530 return rv;
531}
532
533/*
Corentin Chary722ad972007-01-26 14:04:55 +0100534 * LEDD display
535 */
536static ssize_t show_ledd(struct device *dev,
537 struct device_attribute *attr, char *buf)
538{
539 return sprintf(buf, "0x%08x\n", hotk->ledd_status);
540}
541
542static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
543 const char *buf, size_t count)
544{
545 int rv, value;
546
547 rv = parse_arg(buf, count, &value);
548 if (rv > 0) {
549 if (!write_acpi_int(ledd_set_handle, NULL, value, NULL))
550 printk(ASUS_WARNING "LED display write failed\n");
551 else
552 hotk->ledd_status = (u32) value;
553 }
554 return rv;
555}
556
557/*
Corentin Chary4564de12007-01-26 14:04:40 +0100558 * WLAN
559 */
560static ssize_t show_wlan(struct device *dev,
561 struct device_attribute *attr, char *buf)
562{
563 return sprintf(buf, "%d\n", read_status(WL_ON));
564}
565
566static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
567 const char *buf, size_t count)
568{
Corentin Chary935ffee2007-03-11 10:26:12 +0100569 return store_status(buf, count, wl_switch_handle, WL_ON);
Corentin Chary4564de12007-01-26 14:04:40 +0100570}
571
572/*
573 * Bluetooth
574 */
575static ssize_t show_bluetooth(struct device *dev,
576 struct device_attribute *attr, char *buf)
577{
578 return sprintf(buf, "%d\n", read_status(BT_ON));
579}
580
Len Brown8def05f2007-01-30 01:46:43 -0500581static ssize_t store_bluetooth(struct device *dev,
582 struct device_attribute *attr, const char *buf,
583 size_t count)
Corentin Chary4564de12007-01-26 14:04:40 +0100584{
Corentin Chary935ffee2007-03-11 10:26:12 +0100585 return store_status(buf, count, bt_switch_handle, BT_ON);
Corentin Chary4564de12007-01-26 14:04:40 +0100586}
587
Corentin Chary78127b42007-01-26 14:04:49 +0100588/*
589 * Display
590 */
591static void set_display(int value)
592{
593 /* no sanity check needed for now */
594 if (!write_acpi_int(display_set_handle, NULL, value, NULL))
595 printk(ASUS_WARNING "Error setting display\n");
596 return;
597}
598
599static int read_display(void)
600{
Corentin Chary9a816852007-03-11 10:25:38 +0100601 ulong value = 0;
602 acpi_status rv = AE_OK;
Corentin Chary78127b42007-01-26 14:04:49 +0100603
604 /* In most of the case, we know how to set the display, but sometime
605 we can't read it */
Len Brown8def05f2007-01-30 01:46:43 -0500606 if (display_get_handle) {
Corentin Chary9a816852007-03-11 10:25:38 +0100607 rv = acpi_evaluate_integer(display_get_handle, NULL,
608 NULL, &value);
609 if (ACPI_FAILURE(rv))
Corentin Chary78127b42007-01-26 14:04:49 +0100610 printk(ASUS_WARNING "Error reading display status\n");
611 }
612
613 value &= 0x0F; /* needed for some models, shouldn't hurt others */
614
615 return value;
616}
Len Brown8def05f2007-01-30 01:46:43 -0500617
Corentin Chary78127b42007-01-26 14:04:49 +0100618/*
619 * Now, *this* one could be more user-friendly, but so far, no-one has
620 * complained. The significance of bits is the same as in store_disp()
621 */
622static ssize_t show_disp(struct device *dev,
623 struct device_attribute *attr, char *buf)
624{
625 return sprintf(buf, "%d\n", read_display());
626}
627
628/*
629 * Experimental support for display switching. As of now: 1 should activate
630 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
631 * Any combination (bitwise) of these will suffice. I never actually tested 4
632 * displays hooked up simultaneously, so be warned. See the acpi4asus README
633 * for more info.
634 */
635static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
636 const char *buf, size_t count)
637{
638 int rv, value;
639
640 rv = parse_arg(buf, count, &value);
641 if (rv > 0)
642 set_display(value);
643 return rv;
644}
645
Corentin Chary8b857352007-01-26 14:04:58 +0100646/*
647 * Light Sens
648 */
649static void set_light_sens_switch(int value)
650{
651 if (!write_acpi_int(ls_switch_handle, NULL, value, NULL))
652 printk(ASUS_WARNING "Error setting light sensor switch\n");
653 hotk->light_switch = value;
654}
655
656static ssize_t show_lssw(struct device *dev,
657 struct device_attribute *attr, char *buf)
658{
659 return sprintf(buf, "%d\n", hotk->light_switch);
660}
661
662static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
663 const char *buf, size_t count)
664{
665 int rv, value;
666
667 rv = parse_arg(buf, count, &value);
668 if (rv > 0)
669 set_light_sens_switch(value ? 1 : 0);
670
671 return rv;
672}
673
674static void set_light_sens_level(int value)
675{
676 if (!write_acpi_int(ls_level_handle, NULL, value, NULL))
677 printk(ASUS_WARNING "Error setting light sensor level\n");
678 hotk->light_level = value;
679}
680
681static ssize_t show_lslvl(struct device *dev,
682 struct device_attribute *attr, char *buf)
683{
684 return sprintf(buf, "%d\n", hotk->light_level);
685}
686
687static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
688 const char *buf, size_t count)
689{
690 int rv, value;
691
692 rv = parse_arg(buf, count, &value);
693 if (rv > 0) {
694 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
695 /* 0 <= value <= 15 */
696 set_light_sens_level(value);
697 }
698
699 return rv;
700}
701
Corentin Charye539c2f2007-05-06 14:47:06 +0200702/*
703 * GPS
704 */
705static ssize_t show_gps(struct device *dev,
706 struct device_attribute *attr, char *buf)
707{
708 return sprintf(buf, "%d\n", read_status(GPS_ON));
709}
710
711static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
712 const char *buf, size_t count)
713{
Corentin Charyf3985322007-05-06 14:48:22 +0200714 return store_status(buf, count, NULL, GPS_ON);
Corentin Charye539c2f2007-05-06 14:47:06 +0200715}
716
Corentin Chary85091b72007-01-26 14:04:30 +0100717static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
718{
719 /* TODO Find a better way to handle events count. */
720 if (!hotk)
721 return;
722
Corentin Chary6b7091e2007-01-26 14:04:45 +0100723 /*
724 * We need to tell the backlight device when the backlight power is
725 * switched
726 */
727 if (event == ATKD_LCD_ON) {
Corentin Chary935ffee2007-03-11 10:26:12 +0100728 write_status(NULL, 1, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100729 lcd_blank(FB_BLANK_UNBLANK);
Len Brown8def05f2007-01-30 01:46:43 -0500730 } else if (event == ATKD_LCD_OFF) {
Corentin Chary935ffee2007-03-11 10:26:12 +0100731 write_status(NULL, 0, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100732 lcd_blank(FB_BLANK_POWERDOWN);
733 }
734
Corentin Chary85091b72007-01-26 14:04:30 +0100735 acpi_bus_generate_event(hotk->device, event,
736 hotk->event_count[event % 128]++);
737
738 return;
739}
740
741#define ASUS_CREATE_DEVICE_ATTR(_name) \
742 struct device_attribute dev_attr_##_name = { \
743 .attr = { \
744 .name = __stringify(_name), \
Tejun Heo7b595752007-06-14 03:45:17 +0900745 .mode = 0 }, \
Corentin Chary85091b72007-01-26 14:04:30 +0100746 .show = NULL, \
747 .store = NULL, \
748 }
749
750#define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
751 do { \
752 dev_attr_##_name.attr.mode = _mode; \
753 dev_attr_##_name.show = _show; \
754 dev_attr_##_name.store = _store; \
755 } while(0)
756
757static ASUS_CREATE_DEVICE_ATTR(infos);
Corentin Chary4564de12007-01-26 14:04:40 +0100758static ASUS_CREATE_DEVICE_ATTR(wlan);
759static ASUS_CREATE_DEVICE_ATTR(bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +0100760static ASUS_CREATE_DEVICE_ATTR(display);
Corentin Chary722ad972007-01-26 14:04:55 +0100761static ASUS_CREATE_DEVICE_ATTR(ledd);
Corentin Chary8b857352007-01-26 14:04:58 +0100762static ASUS_CREATE_DEVICE_ATTR(ls_switch);
763static ASUS_CREATE_DEVICE_ATTR(ls_level);
Corentin Charye539c2f2007-05-06 14:47:06 +0200764static ASUS_CREATE_DEVICE_ATTR(gps);
Corentin Chary85091b72007-01-26 14:04:30 +0100765
766static struct attribute *asuspf_attributes[] = {
Len Brown8def05f2007-01-30 01:46:43 -0500767 &dev_attr_infos.attr,
768 &dev_attr_wlan.attr,
769 &dev_attr_bluetooth.attr,
770 &dev_attr_display.attr,
771 &dev_attr_ledd.attr,
772 &dev_attr_ls_switch.attr,
773 &dev_attr_ls_level.attr,
Corentin Charye539c2f2007-05-06 14:47:06 +0200774 &dev_attr_gps.attr,
Len Brown8def05f2007-01-30 01:46:43 -0500775 NULL
Corentin Chary85091b72007-01-26 14:04:30 +0100776};
777
778static struct attribute_group asuspf_attribute_group = {
Len Brown8def05f2007-01-30 01:46:43 -0500779 .attrs = asuspf_attributes
Corentin Chary85091b72007-01-26 14:04:30 +0100780};
781
782static struct platform_driver asuspf_driver = {
Len Brown8def05f2007-01-30 01:46:43 -0500783 .driver = {
784 .name = ASUS_HOTK_FILE,
785 .owner = THIS_MODULE,
786 }
Corentin Chary85091b72007-01-26 14:04:30 +0100787};
788
789static struct platform_device *asuspf_device;
790
Corentin Chary85091b72007-01-26 14:04:30 +0100791static void asus_hotk_add_fs(void)
792{
793 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
Corentin Chary4564de12007-01-26 14:04:40 +0100794
795 if (wl_switch_handle)
796 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
797
798 if (bt_switch_handle)
799 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
800 show_bluetooth, store_bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +0100801
802 if (display_set_handle && display_get_handle)
803 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
Len Brown8def05f2007-01-30 01:46:43 -0500804 else if (display_set_handle)
Corentin Chary78127b42007-01-26 14:04:49 +0100805 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
806
Corentin Chary722ad972007-01-26 14:04:55 +0100807 if (ledd_set_handle)
808 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
809
Corentin Chary8b857352007-01-26 14:04:58 +0100810 if (ls_switch_handle && ls_level_handle) {
811 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
812 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
813 }
Corentin Charye539c2f2007-05-06 14:47:06 +0200814
Corentin Charyf3985322007-05-06 14:48:22 +0200815 if (gps_status_handle && gps_on_handle && gps_off_handle)
Corentin Charye539c2f2007-05-06 14:47:06 +0200816 ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
Corentin Chary85091b72007-01-26 14:04:30 +0100817}
818
Len Brown8def05f2007-01-30 01:46:43 -0500819static int asus_handle_init(char *name, acpi_handle * handle,
Corentin Chary85091b72007-01-26 14:04:30 +0100820 char **paths, int num_paths)
821{
822 int i;
823 acpi_status status;
824
825 for (i = 0; i < num_paths; i++) {
826 status = acpi_get_handle(NULL, paths[i], handle);
827 if (ACPI_SUCCESS(status))
828 return 0;
829 }
830
831 *handle = NULL;
832 return -ENODEV;
833}
834
835#define ASUS_HANDLE_INIT(object) \
836 asus_handle_init(#object, &object##_handle, object##_paths, \
837 ARRAY_SIZE(object##_paths))
838
Corentin Chary85091b72007-01-26 14:04:30 +0100839/*
840 * This function is used to initialize the hotk with right values. In this
841 * method, we can make all the detection we want, and modify the hotk struct
842 */
843static int asus_hotk_get_info(void)
844{
845 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Corentin Chary85091b72007-01-26 14:04:30 +0100846 union acpi_object *model = NULL;
Corentin Chary9a816852007-03-11 10:25:38 +0100847 ulong bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +0100848 char *string = NULL;
849 acpi_status status;
850
851 /*
852 * Get DSDT headers early enough to allow for differentiating between
853 * models, but late enough to allow acpi_bus_register_driver() to fail
854 * before doing anything ACPI-specific. Should we encounter a machine,
855 * which needs special handling (i.e. its hotkey device has a different
856 * HID), this bit will be moved. A global variable asus_info contains
857 * the DSDT header.
858 */
Len Brown894d79b2007-02-03 02:13:53 -0500859 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
Corentin Chary85091b72007-01-26 14:04:30 +0100860 if (ACPI_FAILURE(status))
861 printk(ASUS_WARNING "Couldn't get the DSDT table header\n");
Corentin Chary85091b72007-01-26 14:04:30 +0100862
863 /* We have to write 0 on init this far for all ASUS models */
864 if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
865 printk(ASUS_ERR "Hotkey initialization failed\n");
866 return -ENODEV;
867 }
868
869 /* This needs to be called for some laptops to init properly */
Corentin Chary9a816852007-03-11 10:25:38 +0100870 status =
871 acpi_evaluate_integer(hotk->handle, "BSTS", NULL, &bsts_result);
872 if (ACPI_FAILURE(status))
Corentin Chary85091b72007-01-26 14:04:30 +0100873 printk(ASUS_WARNING "Error calling BSTS\n");
874 else if (bsts_result)
875 printk(ASUS_NOTICE "BSTS called, 0x%02x returned\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100876 (uint) bsts_result);
Corentin Chary85091b72007-01-26 14:04:30 +0100877
Corentin Chary185e5af2007-03-11 10:27:33 +0100878 /* This too ... */
879 write_acpi_int(hotk->handle, "CWAP", wapf, NULL);
880
Corentin Chary85091b72007-01-26 14:04:30 +0100881 /*
882 * Try to match the object returned by INIT to the specific model.
883 * Handle every possible object (or the lack of thereof) the DSDT
884 * writers might throw at us. When in trouble, we pass NULL to
885 * asus_model_match() and try something completely different.
886 */
887 if (buffer.pointer) {
888 model = buffer.pointer;
889 switch (model->type) {
890 case ACPI_TYPE_STRING:
891 string = model->string.pointer;
892 break;
893 case ACPI_TYPE_BUFFER:
894 string = model->buffer.pointer;
895 break;
896 default:
897 string = "";
898 break;
899 }
900 }
901 hotk->name = kstrdup(string, GFP_KERNEL);
902 if (!hotk->name)
903 return -ENOMEM;
904
Len Brown8def05f2007-01-30 01:46:43 -0500905 if (*string)
Corentin Chary85091b72007-01-26 14:04:30 +0100906 printk(ASUS_NOTICE " %s model detected\n", string);
907
Corentin Charybe18cda2007-01-26 14:04:35 +0100908 ASUS_HANDLE_INIT(mled_set);
909 ASUS_HANDLE_INIT(tled_set);
910 ASUS_HANDLE_INIT(rled_set);
911 ASUS_HANDLE_INIT(pled_set);
Corentin Charyfdd8d082007-03-11 10:26:48 +0100912 ASUS_HANDLE_INIT(gled_set);
Corentin Charybe18cda2007-01-26 14:04:35 +0100913
Corentin Chary722ad972007-01-26 14:04:55 +0100914 ASUS_HANDLE_INIT(ledd_set);
915
Corentin Chary4564de12007-01-26 14:04:40 +0100916 /*
917 * The HWRS method return informations about the hardware.
918 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
919 * The significance of others is yet to be found.
920 * If we don't find the method, we assume the device are present.
921 */
Corentin Chary9a816852007-03-11 10:25:38 +0100922 status =
923 acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &hwrs_result);
924 if (ACPI_FAILURE(status))
Corentin Chary4564de12007-01-26 14:04:40 +0100925 hwrs_result = WL_HWRS | BT_HWRS;
926
Len Brown8def05f2007-01-30 01:46:43 -0500927 if (hwrs_result & WL_HWRS)
Corentin Chary4564de12007-01-26 14:04:40 +0100928 ASUS_HANDLE_INIT(wl_switch);
Len Brown8def05f2007-01-30 01:46:43 -0500929 if (hwrs_result & BT_HWRS)
Corentin Chary4564de12007-01-26 14:04:40 +0100930 ASUS_HANDLE_INIT(bt_switch);
931
932 ASUS_HANDLE_INIT(wireless_status);
933
Corentin Chary6b7091e2007-01-26 14:04:45 +0100934 ASUS_HANDLE_INIT(brightness_set);
935 ASUS_HANDLE_INIT(brightness_get);
936
937 ASUS_HANDLE_INIT(lcd_switch);
938
Corentin Chary78127b42007-01-26 14:04:49 +0100939 ASUS_HANDLE_INIT(display_set);
940 ASUS_HANDLE_INIT(display_get);
941
Corentin Chary8b857352007-01-26 14:04:58 +0100942 /* There is a lot of models with "ALSL", but a few get
943 a real light sens, so we need to check it. */
Corentin Chary832d9952007-05-06 14:47:29 +0200944 if (!ASUS_HANDLE_INIT(ls_switch))
Corentin Chary8b857352007-01-26 14:04:58 +0100945 ASUS_HANDLE_INIT(ls_level);
946
Corentin Charye539c2f2007-05-06 14:47:06 +0200947 ASUS_HANDLE_INIT(gps_on);
948 ASUS_HANDLE_INIT(gps_off);
949 ASUS_HANDLE_INIT(gps_status);
950
Corentin Chary85091b72007-01-26 14:04:30 +0100951 kfree(model);
952
953 return AE_OK;
954}
955
956static int asus_hotk_check(void)
957{
958 int result = 0;
959
960 result = acpi_bus_get_status(hotk->device);
961 if (result)
962 return result;
963
964 if (hotk->device->status.present) {
965 result = asus_hotk_get_info();
966 } else {
967 printk(ASUS_ERR "Hotkey device not present, aborting\n");
968 return -EINVAL;
969 }
970
971 return result;
972}
973
974static int asus_hotk_found;
975
976static int asus_hotk_add(struct acpi_device *device)
977{
978 acpi_status status = AE_OK;
979 int result;
980
981 if (!device)
982 return -EINVAL;
983
984 printk(ASUS_NOTICE "Asus Laptop Support version %s\n",
985 ASUS_LAPTOP_VERSION);
986
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700987 hotk = kzalloc(sizeof(struct asus_hotk), GFP_KERNEL);
Corentin Chary85091b72007-01-26 14:04:30 +0100988 if (!hotk)
989 return -ENOMEM;
Corentin Chary85091b72007-01-26 14:04:30 +0100990
991 hotk->handle = device->handle;
992 strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
993 strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
994 acpi_driver_data(device) = hotk;
995 hotk->device = device;
996
997 result = asus_hotk_check();
998 if (result)
999 goto end;
1000
1001 asus_hotk_add_fs();
1002
1003 /*
1004 * We install the handler, it will receive the hotk in parameter, so, we
1005 * could add other data to the hotk struct
1006 */
Corentin Charya9dbcb32007-05-06 14:46:41 +02001007 status = acpi_install_notify_handler(hotk->handle, ACPI_ALL_NOTIFY,
Corentin Chary85091b72007-01-26 14:04:30 +01001008 asus_hotk_notify, hotk);
1009 if (ACPI_FAILURE(status))
1010 printk(ASUS_ERR "Error installing notify handler\n");
1011
1012 asus_hotk_found = 1;
1013
Corentin Chary6b7091e2007-01-26 14:04:45 +01001014 /* WLED and BLED are on by default */
Corentin Chary935ffee2007-03-11 10:26:12 +01001015 write_status(bt_switch_handle, 1, BT_ON);
1016 write_status(wl_switch_handle, 1, WL_ON);
1017
1018 /* If the h/w switch is off, we need to check the real status */
1019 write_status(NULL, read_status(BT_ON), BT_ON);
1020 write_status(NULL, read_status(WL_ON), WL_ON);
Corentin Chary4564de12007-01-26 14:04:40 +01001021
Corentin Chary6b7091e2007-01-26 14:04:45 +01001022 /* LCD Backlight is on by default */
Corentin Chary935ffee2007-03-11 10:26:12 +01001023 write_status(NULL, 1, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001024
Corentin Chary722ad972007-01-26 14:04:55 +01001025 /* LED display is off by default */
1026 hotk->ledd_status = 0xFFF;
1027
Len Brown8def05f2007-01-30 01:46:43 -05001028 /* Set initial values of light sensor and level */
1029 hotk->light_switch = 1; /* Default to light sensor disabled */
1030 hotk->light_level = 0; /* level 5 for sensor sensitivity */
Corentin Chary8b857352007-01-26 14:04:58 +01001031
Len Brown8def05f2007-01-30 01:46:43 -05001032 if (ls_switch_handle)
1033 set_light_sens_switch(hotk->light_switch);
Corentin Chary8b857352007-01-26 14:04:58 +01001034
Len Brown8def05f2007-01-30 01:46:43 -05001035 if (ls_level_handle)
1036 set_light_sens_level(hotk->light_level);
Corentin Chary8b857352007-01-26 14:04:58 +01001037
Corentin Charye539c2f2007-05-06 14:47:06 +02001038 /* GPS is on by default */
1039 write_status(NULL, 1, GPS_ON);
1040
Corentin Chary85091b72007-01-26 14:04:30 +01001041 end:
1042 if (result) {
1043 kfree(hotk->name);
1044 kfree(hotk);
1045 }
1046
1047 return result;
1048}
1049
1050static int asus_hotk_remove(struct acpi_device *device, int type)
1051{
1052 acpi_status status = 0;
1053
1054 if (!device || !acpi_driver_data(device))
1055 return -EINVAL;
1056
Corentin Charya9dbcb32007-05-06 14:46:41 +02001057 status = acpi_remove_notify_handler(hotk->handle, ACPI_ALL_NOTIFY,
Corentin Chary85091b72007-01-26 14:04:30 +01001058 asus_hotk_notify);
1059 if (ACPI_FAILURE(status))
1060 printk(ASUS_ERR "Error removing notify handler\n");
1061
1062 kfree(hotk->name);
1063 kfree(hotk);
1064
1065 return 0;
1066}
1067
Corentin Chary6b7091e2007-01-26 14:04:45 +01001068static void asus_backlight_exit(void)
1069{
Len Brown8def05f2007-01-30 01:46:43 -05001070 if (asus_backlight_device)
Corentin Chary6b7091e2007-01-26 14:04:45 +01001071 backlight_device_unregister(asus_backlight_device);
1072}
1073
Corentin Charybe18cda2007-01-26 14:04:35 +01001074#define ASUS_LED_UNREGISTER(object) \
Guillaume Chazaraine1996a62007-08-16 18:18:53 +02001075 if (object##_led.dev) \
1076 led_classdev_unregister(&object##_led)
Corentin Charybe18cda2007-01-26 14:04:35 +01001077
1078static void asus_led_exit(void)
1079{
Al Viro3b0d7112007-07-23 11:21:34 +01001080 destroy_workqueue(led_workqueue);
Corentin Charybe18cda2007-01-26 14:04:35 +01001081 ASUS_LED_UNREGISTER(mled);
1082 ASUS_LED_UNREGISTER(tled);
1083 ASUS_LED_UNREGISTER(pled);
1084 ASUS_LED_UNREGISTER(rled);
Corentin Charyfdd8d082007-03-11 10:26:48 +01001085 ASUS_LED_UNREGISTER(gled);
Corentin Charybe18cda2007-01-26 14:04:35 +01001086}
1087
Corentin Chary85091b72007-01-26 14:04:30 +01001088static void __exit asus_laptop_exit(void)
1089{
Corentin Chary6b7091e2007-01-26 14:04:45 +01001090 asus_backlight_exit();
Corentin Charybe18cda2007-01-26 14:04:35 +01001091 asus_led_exit();
1092
Corentin Chary85091b72007-01-26 14:04:30 +01001093 acpi_bus_unregister_driver(&asus_hotk_driver);
Len Brown8def05f2007-01-30 01:46:43 -05001094 sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
1095 platform_device_unregister(asuspf_device);
1096 platform_driver_unregister(&asuspf_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001097}
1098
Len Brown8def05f2007-01-30 01:46:43 -05001099static int asus_backlight_init(struct device *dev)
Corentin Chary6b7091e2007-01-26 14:04:45 +01001100{
1101 struct backlight_device *bd;
1102
Len Brown8def05f2007-01-30 01:46:43 -05001103 if (brightness_set_handle && lcd_switch_handle) {
1104 bd = backlight_device_register(ASUS_HOTK_FILE, dev,
Richard Purdie599a52d2007-02-10 23:07:48 +00001105 NULL, &asusbl_ops);
Len Brown8def05f2007-01-30 01:46:43 -05001106 if (IS_ERR(bd)) {
Corentin Chary6b7091e2007-01-26 14:04:45 +01001107 printk(ASUS_ERR
1108 "Could not register asus backlight device\n");
1109 asus_backlight_device = NULL;
1110 return PTR_ERR(bd);
1111 }
1112
1113 asus_backlight_device = bd;
1114
Richard Purdie599a52d2007-02-10 23:07:48 +00001115 bd->props.max_brightness = 15;
1116 bd->props.brightness = read_brightness(NULL);
1117 bd->props.power = FB_BLANK_UNBLANK;
Richard Purdie28ee0862007-02-08 22:25:09 +00001118 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001119 }
1120 return 0;
1121}
1122
Corentin Charybe18cda2007-01-26 14:04:35 +01001123static int asus_led_register(acpi_handle handle,
Len Brown8def05f2007-01-30 01:46:43 -05001124 struct led_classdev *ldev, struct device *dev)
Corentin Charybe18cda2007-01-26 14:04:35 +01001125{
Len Brown8def05f2007-01-30 01:46:43 -05001126 if (!handle)
Corentin Charybe18cda2007-01-26 14:04:35 +01001127 return 0;
1128
1129 return led_classdev_register(dev, ldev);
1130}
Len Brown8def05f2007-01-30 01:46:43 -05001131
Corentin Charybe18cda2007-01-26 14:04:35 +01001132#define ASUS_LED_REGISTER(object, device) \
1133 asus_led_register(object##_set_handle, &object##_led, device)
1134
Len Brown8def05f2007-01-30 01:46:43 -05001135static int asus_led_init(struct device *dev)
Corentin Charybe18cda2007-01-26 14:04:35 +01001136{
1137 int rv;
1138
1139 rv = ASUS_LED_REGISTER(mled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001140 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001141 goto out;
Corentin Charybe18cda2007-01-26 14:04:35 +01001142
1143 rv = ASUS_LED_REGISTER(tled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001144 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001145 goto out1;
Corentin Charybe18cda2007-01-26 14:04:35 +01001146
1147 rv = ASUS_LED_REGISTER(rled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001148 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001149 goto out2;
Corentin Charybe18cda2007-01-26 14:04:35 +01001150
1151 rv = ASUS_LED_REGISTER(pled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001152 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001153 goto out3;
Corentin Charybe18cda2007-01-26 14:04:35 +01001154
Corentin Charyfdd8d082007-03-11 10:26:48 +01001155 rv = ASUS_LED_REGISTER(gled, dev);
1156 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001157 goto out4;
Corentin Charyfdd8d082007-03-11 10:26:48 +01001158
Corentin Charybe18cda2007-01-26 14:04:35 +01001159 led_workqueue = create_singlethread_workqueue("led_workqueue");
Len Brown8def05f2007-01-30 01:46:43 -05001160 if (!led_workqueue)
Al Viro3b0d7112007-07-23 11:21:34 +01001161 goto out5;
Corentin Charybe18cda2007-01-26 14:04:35 +01001162
1163 return 0;
Al Viro3b0d7112007-07-23 11:21:34 +01001164out5:
1165 rv = -ENOMEM;
1166 ASUS_LED_UNREGISTER(gled);
1167out4:
1168 ASUS_LED_UNREGISTER(pled);
1169out3:
1170 ASUS_LED_UNREGISTER(rled);
1171out2:
1172 ASUS_LED_UNREGISTER(tled);
1173out1:
1174 ASUS_LED_UNREGISTER(mled);
1175out:
1176 return rv;
Corentin Charybe18cda2007-01-26 14:04:35 +01001177}
1178
Corentin Chary85091b72007-01-26 14:04:30 +01001179static int __init asus_laptop_init(void)
1180{
Corentin Charybe18cda2007-01-26 14:04:35 +01001181 struct device *dev;
Corentin Chary85091b72007-01-26 14:04:30 +01001182 int result;
1183
1184 if (acpi_disabled)
1185 return -ENODEV;
1186
Corentin Chary85091b72007-01-26 14:04:30 +01001187 result = acpi_bus_register_driver(&asus_hotk_driver);
1188 if (result < 0)
1189 return result;
1190
1191 /*
1192 * This is a bit of a kludge. We only want this module loaded
1193 * for ASUS systems, but there's currently no way to probe the
1194 * ACPI namespace for ASUS HIDs. So we just return failure if
1195 * we didn't find one, which will cause the module to be
1196 * unloaded.
1197 */
1198 if (!asus_hotk_found) {
1199 acpi_bus_unregister_driver(&asus_hotk_driver);
1200 return -ENODEV;
1201 }
1202
Corentin Charybe18cda2007-01-26 14:04:35 +01001203 dev = acpi_get_physical_device(hotk->device->handle);
1204
Corentin Chary6b7091e2007-01-26 14:04:45 +01001205 result = asus_backlight_init(dev);
Len Brown8def05f2007-01-30 01:46:43 -05001206 if (result)
Corentin Chary6b7091e2007-01-26 14:04:45 +01001207 goto fail_backlight;
1208
Corentin Charybe18cda2007-01-26 14:04:35 +01001209 result = asus_led_init(dev);
Len Brown8def05f2007-01-30 01:46:43 -05001210 if (result)
Corentin Charybe18cda2007-01-26 14:04:35 +01001211 goto fail_led;
1212
Len Brown8def05f2007-01-30 01:46:43 -05001213 /* Register platform stuff */
Corentin Chary85091b72007-01-26 14:04:30 +01001214 result = platform_driver_register(&asuspf_driver);
Len Brown8def05f2007-01-30 01:46:43 -05001215 if (result)
1216 goto fail_platform_driver;
Corentin Chary85091b72007-01-26 14:04:30 +01001217
Len Brown8def05f2007-01-30 01:46:43 -05001218 asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
1219 if (!asuspf_device) {
1220 result = -ENOMEM;
1221 goto fail_platform_device1;
1222 }
Corentin Chary85091b72007-01-26 14:04:30 +01001223
Len Brown8def05f2007-01-30 01:46:43 -05001224 result = platform_device_add(asuspf_device);
1225 if (result)
1226 goto fail_platform_device2;
Corentin Chary85091b72007-01-26 14:04:30 +01001227
Len Brown8def05f2007-01-30 01:46:43 -05001228 result = sysfs_create_group(&asuspf_device->dev.kobj,
Corentin Chary85091b72007-01-26 14:04:30 +01001229 &asuspf_attribute_group);
Len Brown8def05f2007-01-30 01:46:43 -05001230 if (result)
1231 goto fail_sysfs;
Corentin Chary85091b72007-01-26 14:04:30 +01001232
Len Brown8def05f2007-01-30 01:46:43 -05001233 return 0;
Corentin Chary85091b72007-01-26 14:04:30 +01001234
Len Brown8def05f2007-01-30 01:46:43 -05001235 fail_sysfs:
1236 platform_device_del(asuspf_device);
Corentin Chary85091b72007-01-26 14:04:30 +01001237
Len Brown8def05f2007-01-30 01:46:43 -05001238 fail_platform_device2:
Corentin Chary85091b72007-01-26 14:04:30 +01001239 platform_device_put(asuspf_device);
1240
Len Brown8def05f2007-01-30 01:46:43 -05001241 fail_platform_device1:
1242 platform_driver_unregister(&asuspf_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001243
Len Brown8def05f2007-01-30 01:46:43 -05001244 fail_platform_driver:
Corentin Charybe18cda2007-01-26 14:04:35 +01001245 asus_led_exit();
1246
Len Brown8def05f2007-01-30 01:46:43 -05001247 fail_led:
Corentin Chary6b7091e2007-01-26 14:04:45 +01001248 asus_backlight_exit();
1249
Len Brown8def05f2007-01-30 01:46:43 -05001250 fail_backlight:
Corentin Chary85091b72007-01-26 14:04:30 +01001251
1252 return result;
1253}
1254
1255module_init(asus_laptop_init);
1256module_exit(asus_laptop_exit);