blob: 5b912cc5cbee3a581ea3208738957c576a736004 [file] [log] [blame]
Corentin Chary85091b72007-01-26 14:04:30 +01001/*
2 * asus-laptop.c - Asus Laptop Support
3 *
4 *
5 * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
Corentin Chary8ec555c2007-03-11 10:28:03 +01006 * Copyright (C) 2006-2007 Corentin Chary
Corentin Chary85091b72007-01-26 14:04:30 +01007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 *
23 * The development page for this driver is located at
24 * http://sourceforge.net/projects/acpi4asus/
25 *
26 * Credits:
27 * Pontus Fuchs - Helper functions, cleanup
28 * Johann Wiesner - Small compile fixes
29 * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
30 * Eric Burghard - LED display support for W1N
31 * Josh Green - Light Sens support
32 * Thomas Tuttle - His first patch for led support was very helpfull
Corentin Charye539c2f2007-05-06 14:47:06 +020033 * Sam Lin - GPS support
Corentin Chary85091b72007-01-26 14:04:30 +010034 */
35
Corentin Chary2fcc23d2009-06-19 14:52:03 +020036#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37
Corentin Chary85091b72007-01-26 14:04:30 +010038#include <linux/kernel.h>
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/types.h>
42#include <linux/err.h>
43#include <linux/proc_fs.h>
Corentin Chary6b7091e2007-01-26 14:04:45 +010044#include <linux/backlight.h>
45#include <linux/fb.h>
Corentin Charybe18cda2007-01-26 14:04:35 +010046#include <linux/leds.h>
Corentin Chary85091b72007-01-26 14:04:30 +010047#include <linux/platform_device.h>
48#include <acpi/acpi_drivers.h>
49#include <acpi/acpi_bus.h>
50#include <asm/uaccess.h>
Corentin Chary034ce902009-01-20 16:17:43 +010051#include <linux/input.h>
Corentin Chary85091b72007-01-26 14:04:30 +010052
Corentin Charyf3985322007-05-06 14:48:22 +020053#define ASUS_LAPTOP_VERSION "0.42"
Corentin Chary85091b72007-01-26 14:04:30 +010054
55#define ASUS_HOTK_NAME "Asus Laptop Support"
56#define ASUS_HOTK_CLASS "hotkey"
57#define ASUS_HOTK_DEVICE_NAME "Hotkey"
Corentin Chary2fcc23d2009-06-19 14:52:03 +020058#define ASUS_HOTK_FILE KBUILD_MODNAME
Corentin Chary85091b72007-01-26 14:04:30 +010059#define ASUS_HOTK_PREFIX "\\_SB.ATKD."
60
Corentin Chary2fcc23d2009-06-19 14:52:03 +020061
Corentin Charybe18cda2007-01-26 14:04:35 +010062/*
Corentin Chary6b7091e2007-01-26 14:04:45 +010063 * Some events we use, same for all Asus
64 */
65#define ATKD_BR_UP 0x10
66#define ATKD_BR_DOWN 0x20
67#define ATKD_LCD_ON 0x33
68#define ATKD_LCD_OFF 0x34
69
70/*
Corentin Chary4564de12007-01-26 14:04:40 +010071 * Known bits returned by \_SB.ATKD.HWRS
Corentin Charybe18cda2007-01-26 14:04:35 +010072 */
Corentin Chary4564de12007-01-26 14:04:40 +010073#define WL_HWRS 0x80
74#define BT_HWRS 0x100
75
76/*
77 * Flags for hotk status
78 * WL_ON and BT_ON are also used for wireless_status()
79 */
80#define WL_ON 0x01 //internal Wifi
81#define BT_ON 0x02 //internal Bluetooth
Corentin Charybe18cda2007-01-26 14:04:35 +010082#define MLED_ON 0x04 //mail LED
83#define TLED_ON 0x08 //touchpad LED
Len Brown8def05f2007-01-30 01:46:43 -050084#define RLED_ON 0x10 //Record LED
85#define PLED_ON 0x20 //Phone LED
Corentin Charyfdd8d082007-03-11 10:26:48 +010086#define GLED_ON 0x40 //Gaming LED
87#define LCD_ON 0x80 //LCD backlight
Corentin Charyf3985322007-05-06 14:48:22 +020088#define GPS_ON 0x100 //GPS
Corentin Charyb7d3fbc2009-08-28 12:56:50 +000089#define KEY_ON 0x200 //Keyboard backlight
Corentin Charybe18cda2007-01-26 14:04:35 +010090
Corentin Chary85091b72007-01-26 14:04:30 +010091#define ASUS_LOG ASUS_HOTK_FILE ": "
92#define ASUS_ERR KERN_ERR ASUS_LOG
93#define ASUS_WARNING KERN_WARNING ASUS_LOG
94#define ASUS_NOTICE KERN_NOTICE ASUS_LOG
95#define ASUS_INFO KERN_INFO ASUS_LOG
96#define ASUS_DEBUG KERN_DEBUG ASUS_LOG
97
98MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
99MODULE_DESCRIPTION(ASUS_HOTK_NAME);
100MODULE_LICENSE("GPL");
101
Corentin Chary185e5af2007-03-11 10:27:33 +0100102/* WAPF defines the behavior of the Fn+Fx wlan key
103 * The significance of values is yet to be found, but
104 * most of the time:
105 * 0x0 will do nothing
106 * 0x1 will allow to control the device with Fn+Fx key.
107 * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
108 * 0x5 like 0x1 or 0x4
109 * So, if something doesn't work as you want, just try other values =)
110 */
111static uint wapf = 1;
112module_param(wapf, uint, 0644);
113MODULE_PARM_DESC(wapf, "WAPF value");
114
Corentin Chary85091b72007-01-26 14:04:30 +0100115#define ASUS_HANDLE(object, paths...) \
116 static acpi_handle object##_handle = NULL; \
117 static char *object##_paths[] = { paths }
118
Corentin Charybe18cda2007-01-26 14:04:35 +0100119/* LED */
120ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
121ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
Len Brown8def05f2007-01-30 01:46:43 -0500122ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
123ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
Corentin Charyfdd8d082007-03-11 10:26:48 +0100124ASUS_HANDLE(gled_set, ASUS_HOTK_PREFIX "GLED"); /* G1, G2 (probably) */
Corentin Charybe18cda2007-01-26 14:04:35 +0100125
Corentin Chary722ad972007-01-26 14:04:55 +0100126/* LEDD */
127ASUS_HANDLE(ledd_set, ASUS_HOTK_PREFIX "SLCM");
128
Corentin Chary4564de12007-01-26 14:04:40 +0100129/* Bluetooth and WLAN
130 * WLED and BLED are not handled like other XLED, because in some dsdt
131 * they also control the WLAN/Bluetooth device.
132 */
133ASUS_HANDLE(wl_switch, ASUS_HOTK_PREFIX "WLED");
134ASUS_HANDLE(bt_switch, ASUS_HOTK_PREFIX "BLED");
Len Brown8def05f2007-01-30 01:46:43 -0500135ASUS_HANDLE(wireless_status, ASUS_HOTK_PREFIX "RSTS"); /* All new models */
Corentin Chary4564de12007-01-26 14:04:40 +0100136
Corentin Chary6b7091e2007-01-26 14:04:45 +0100137/* Brightness */
138ASUS_HANDLE(brightness_set, ASUS_HOTK_PREFIX "SPLV");
139ASUS_HANDLE(brightness_get, ASUS_HOTK_PREFIX "GPLV");
140
141/* Backlight */
Len Brown8def05f2007-01-30 01:46:43 -0500142ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
143 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
144 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
145 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
146 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
Torsten Krah4d0b856e2008-10-17 09:47:57 +0200147 "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
Len Brown8def05f2007-01-30 01:46:43 -0500148 "\\_SB.PCI0.PX40.Q10", /* S1x */
149 "\\Q10"); /* A2x, L2D, L3D, M2E */
Corentin Chary6b7091e2007-01-26 14:04:45 +0100150
Corentin Chary78127b42007-01-26 14:04:49 +0100151/* Display */
152ASUS_HANDLE(display_set, ASUS_HOTK_PREFIX "SDSP");
Len Brown8def05f2007-01-30 01:46:43 -0500153ASUS_HANDLE(display_get, "\\_SB.PCI0.P0P1.VGA.GETD", /* A6B, A6K A6R A7D F3JM L4R M6R A3G
154 M6A M6V VX-1 V6J V6V W3Z */
155 "\\_SB.PCI0.P0P2.VGA.GETD", /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V
Corentin Charyf3985322007-05-06 14:48:22 +0200156 S5A M5A z33A W1Jc W2V G1 */
Len Brown8def05f2007-01-30 01:46:43 -0500157 "\\_SB.PCI0.P0P3.VGA.GETD", /* A6V A6Q */
158 "\\_SB.PCI0.P0PA.VGA.GETD", /* A6T, A6M */
159 "\\_SB.PCI0.PCI1.VGAC.NMAP", /* L3C */
160 "\\_SB.PCI0.VGA.GETD", /* Z96F */
161 "\\ACTD", /* A2D */
162 "\\ADVG", /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
163 "\\DNXT", /* P30 */
164 "\\INFB", /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
165 "\\SSTE"); /* A3F A6F A3N A3L M6N W3N W6A */
Corentin Chary78127b42007-01-26 14:04:49 +0100166
Len Brown8def05f2007-01-30 01:46:43 -0500167ASUS_HANDLE(ls_switch, ASUS_HOTK_PREFIX "ALSC"); /* Z71A Z71V */
168ASUS_HANDLE(ls_level, ASUS_HOTK_PREFIX "ALSL"); /* Z71A Z71V */
Corentin Chary8b857352007-01-26 14:04:58 +0100169
Corentin Charye539c2f2007-05-06 14:47:06 +0200170/* GPS */
171/* R2H use different handle for GPS on/off */
Corentin Charyf3985322007-05-06 14:48:22 +0200172ASUS_HANDLE(gps_on, ASUS_HOTK_PREFIX "SDON"); /* R2H */
173ASUS_HANDLE(gps_off, ASUS_HOTK_PREFIX "SDOF"); /* R2H */
Corentin Charye539c2f2007-05-06 14:47:06 +0200174ASUS_HANDLE(gps_status, ASUS_HOTK_PREFIX "GPST");
175
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000176/* Keyboard light */
177ASUS_HANDLE(kled_set, ASUS_HOTK_PREFIX "SLKB");
178ASUS_HANDLE(kled_get, ASUS_HOTK_PREFIX "GLKB");
179
Corentin Chary85091b72007-01-26 14:04:30 +0100180/*
181 * This is the main structure, we can use it to store anything interesting
182 * about the hotk device
183 */
184struct asus_hotk {
Len Brown8def05f2007-01-30 01:46:43 -0500185 char *name; //laptop name
Corentin Chary85091b72007-01-26 14:04:30 +0100186 struct acpi_device *device; //the device we are in
187 acpi_handle handle; //the handle of the hotk device
188 char status; //status of the hotk, for LEDs, ...
Corentin Chary722ad972007-01-26 14:04:55 +0100189 u32 ledd_status; //status of the LED display
Len Brown8def05f2007-01-30 01:46:43 -0500190 u8 light_level; //light sensor level
191 u8 light_switch; //light sensor switch value
Corentin Chary85091b72007-01-26 14:04:30 +0100192 u16 event_count[128]; //count for each event TODO make this better
Corentin Chary034ce902009-01-20 16:17:43 +0100193 struct input_dev *inputdev;
194 u16 *keycode_map;
Corentin Chary85091b72007-01-26 14:04:30 +0100195};
196
197/*
198 * This header is made available to allow proper configuration given model,
199 * revision number , ... this info cannot go in struct asus_hotk because it is
200 * available before the hotk
201 */
202static struct acpi_table_header *asus_info;
203
204/* The actual device the driver binds to */
205static struct asus_hotk *hotk;
206
207/*
208 * The hotkey driver declaration
209 */
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200210static const struct acpi_device_id asus_device_ids[] = {
211 {"ATK0100", 0},
212 {"", 0},
213};
214MODULE_DEVICE_TABLE(acpi, asus_device_ids);
215
Corentin Chary85091b72007-01-26 14:04:30 +0100216static int asus_hotk_add(struct acpi_device *device);
217static int asus_hotk_remove(struct acpi_device *device, int type);
Bjorn Helgaas586ed162009-04-30 09:35:53 -0600218static void asus_hotk_notify(struct acpi_device *device, u32 event);
219
Corentin Chary85091b72007-01-26 14:04:30 +0100220static struct acpi_driver asus_hotk_driver = {
221 .name = ASUS_HOTK_NAME,
222 .class = ASUS_HOTK_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200223 .ids = asus_device_ids,
Bjorn Helgaas586ed162009-04-30 09:35:53 -0600224 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
Corentin Chary85091b72007-01-26 14:04:30 +0100225 .ops = {
226 .add = asus_hotk_add,
227 .remove = asus_hotk_remove,
Bjorn Helgaas586ed162009-04-30 09:35:53 -0600228 .notify = asus_hotk_notify,
Corentin Chary85091b72007-01-26 14:04:30 +0100229 },
230};
231
Corentin Chary6b7091e2007-01-26 14:04:45 +0100232/* The backlight device /sys/class/backlight */
233static struct backlight_device *asus_backlight_device;
234
235/*
236 * The backlight class declaration
237 */
238static int read_brightness(struct backlight_device *bd);
239static int update_bl_status(struct backlight_device *bd);
Richard Purdie599a52d2007-02-10 23:07:48 +0000240static struct backlight_ops asusbl_ops = {
Len Brown8def05f2007-01-30 01:46:43 -0500241 .get_brightness = read_brightness,
242 .update_status = update_bl_status,
Corentin Chary6b7091e2007-01-26 14:04:45 +0100243};
244
Corentin Charybe18cda2007-01-26 14:04:35 +0100245/* These functions actually update the LED's, and are called from a
246 * workqueue. By doing this as separate work rather than when the LED
247 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
248 * potentially bad time, such as a timer interrupt. */
249static struct workqueue_struct *led_workqueue;
250
Corentin Chary977c3282009-08-28 12:56:49 +0000251#define ASUS_LED(object, ledname, max) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100252 static void object##_led_set(struct led_classdev *led_cdev, \
253 enum led_brightness value); \
Corentin Charyabfa57e2009-08-28 12:56:47 +0000254 static enum led_brightness object##_led_get( \
255 struct led_classdev *led_cdev); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100256 static void object##_led_update(struct work_struct *ignored); \
257 static int object##_led_wk; \
Adrian Bunkf110ef52007-02-20 01:07:25 +0100258 static DECLARE_WORK(object##_led_work, object##_led_update); \
Corentin Charybe18cda2007-01-26 14:04:35 +0100259 static struct led_classdev object##_led = { \
Richard Purdie6c152be2007-10-31 15:00:07 +0100260 .name = "asus::" ledname, \
Corentin Charybe18cda2007-01-26 14:04:35 +0100261 .brightness_set = object##_led_set, \
Corentin Charyabfa57e2009-08-28 12:56:47 +0000262 .brightness_get = object##_led_get, \
Corentin Chary977c3282009-08-28 12:56:49 +0000263 .max_brightness = max \
Corentin Charybe18cda2007-01-26 14:04:35 +0100264 }
265
Corentin Chary977c3282009-08-28 12:56:49 +0000266ASUS_LED(mled, "mail", 1);
267ASUS_LED(tled, "touchpad", 1);
268ASUS_LED(rled, "record", 1);
269ASUS_LED(pled, "phone", 1);
270ASUS_LED(gled, "gaming", 1);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000271ASUS_LED(kled, "kbd_backlight", 3);
Corentin Charybe18cda2007-01-26 14:04:35 +0100272
Corentin Chary034ce902009-01-20 16:17:43 +0100273struct key_entry {
274 char type;
275 u8 code;
276 u16 keycode;
277};
278
279enum { KE_KEY, KE_END };
280
281static struct key_entry asus_keymap[] = {
282 {KE_KEY, 0x30, KEY_VOLUMEUP},
283 {KE_KEY, 0x31, KEY_VOLUMEDOWN},
284 {KE_KEY, 0x32, KEY_MUTE},
285 {KE_KEY, 0x33, KEY_SWITCHVIDEOMODE},
286 {KE_KEY, 0x34, KEY_SWITCHVIDEOMODE},
287 {KE_KEY, 0x40, KEY_PREVIOUSSONG},
288 {KE_KEY, 0x41, KEY_NEXTSONG},
Corentin Chary309f5fb2009-04-27 09:23:42 +0200289 {KE_KEY, 0x43, KEY_STOPCD},
Corentin Chary034ce902009-01-20 16:17:43 +0100290 {KE_KEY, 0x45, KEY_PLAYPAUSE},
Corentin Chary4644d0e2009-08-28 12:56:52 +0000291 {KE_KEY, 0x4c, KEY_MEDIA},
Corentin Chary034ce902009-01-20 16:17:43 +0100292 {KE_KEY, 0x50, KEY_EMAIL},
293 {KE_KEY, 0x51, KEY_WWW},
Corentin Chary0aa20f72009-08-28 12:56:53 +0000294 {KE_KEY, 0x55, KEY_CALC},
Corentin Chary309f5fb2009-04-27 09:23:42 +0200295 {KE_KEY, 0x5C, KEY_SCREENLOCK}, /* Screenlock */
Corentin Chary034ce902009-01-20 16:17:43 +0100296 {KE_KEY, 0x5D, KEY_WLAN},
Corentin Charyf6413752009-08-28 12:56:48 +0000297 {KE_KEY, 0x5E, KEY_WLAN},
298 {KE_KEY, 0x5F, KEY_WLAN},
299 {KE_KEY, 0x60, KEY_SWITCHVIDEOMODE},
Corentin Chary034ce902009-01-20 16:17:43 +0100300 {KE_KEY, 0x61, KEY_SWITCHVIDEOMODE},
301 {KE_KEY, 0x6B, BTN_TOUCH}, /* Lock Mouse */
302 {KE_KEY, 0x82, KEY_CAMERA},
Corentin Chary309f5fb2009-04-27 09:23:42 +0200303 {KE_KEY, 0x8A, KEY_PROG1},
Corentin Chary034ce902009-01-20 16:17:43 +0100304 {KE_KEY, 0x95, KEY_MEDIA},
305 {KE_KEY, 0x99, KEY_PHONE},
Corentin Charydc795262009-08-28 12:56:51 +0000306 {KE_KEY, 0xc4, KEY_KBDILLUMUP},
307 {KE_KEY, 0xc5, KEY_KBDILLUMDOWN},
Corentin Chary034ce902009-01-20 16:17:43 +0100308 {KE_END, 0},
309};
310
Corentin Chary85091b72007-01-26 14:04:30 +0100311/*
312 * This function evaluates an ACPI method, given an int as parameter, the
313 * method is searched within the scope of the handle, can be NULL. The output
314 * of the method is written is output, which can also be NULL
315 *
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100316 * returns 0 if write is successful, -1 else.
Corentin Chary85091b72007-01-26 14:04:30 +0100317 */
318static int write_acpi_int(acpi_handle handle, const char *method, int val,
319 struct acpi_buffer *output)
320{
321 struct acpi_object_list params; //list of input parameters (an int here)
322 union acpi_object in_obj; //the only param we use
323 acpi_status status;
324
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100325 if (!handle)
326 return 0;
327
Corentin Chary85091b72007-01-26 14:04:30 +0100328 params.count = 1;
329 params.pointer = &in_obj;
330 in_obj.type = ACPI_TYPE_INTEGER;
331 in_obj.integer.value = val;
332
333 status = acpi_evaluate_object(handle, (char *)method, &params, output);
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100334 if (status == AE_OK)
335 return 0;
336 else
337 return -1;
Corentin Chary85091b72007-01-26 14:04:30 +0100338}
339
Len Brown8def05f2007-01-30 01:46:43 -0500340static int read_wireless_status(int mask)
341{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400342 unsigned long long status;
Corentin Chary9a816852007-03-11 10:25:38 +0100343 acpi_status rv = AE_OK;
Corentin Chary4564de12007-01-26 14:04:40 +0100344
345 if (!wireless_status_handle)
346 return (hotk->status & mask) ? 1 : 0;
347
Corentin Chary9a816852007-03-11 10:25:38 +0100348 rv = acpi_evaluate_integer(wireless_status_handle, NULL, NULL, &status);
349 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200350 pr_warning("Error reading Wireless status\n");
Corentin Chary9a816852007-03-11 10:25:38 +0100351 else
352 return (status & mask) ? 1 : 0;
Corentin Chary4564de12007-01-26 14:04:40 +0100353
354 return (hotk->status & mask) ? 1 : 0;
355}
356
Corentin Charyf3985322007-05-06 14:48:22 +0200357static int read_gps_status(void)
358{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400359 unsigned long long status;
Corentin Charye539c2f2007-05-06 14:47:06 +0200360 acpi_status rv = AE_OK;
361
362 rv = acpi_evaluate_integer(gps_status_handle, NULL, NULL, &status);
363 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200364 pr_warning("Error reading GPS status\n");
Corentin Charye539c2f2007-05-06 14:47:06 +0200365 else
Corentin Charyf3985322007-05-06 14:48:22 +0200366 return status ? 1 : 0;
Corentin Charye539c2f2007-05-06 14:47:06 +0200367
Corentin Charyf3985322007-05-06 14:48:22 +0200368 return (hotk->status & GPS_ON) ? 1 : 0;
Corentin Charye539c2f2007-05-06 14:47:06 +0200369}
370
Corentin Charybe18cda2007-01-26 14:04:35 +0100371/* Generic LED functions */
372static int read_status(int mask)
373{
Corentin Chary4564de12007-01-26 14:04:40 +0100374 /* There is a special method for both wireless devices */
375 if (mask == BT_ON || mask == WL_ON)
376 return read_wireless_status(mask);
Corentin Charyf3985322007-05-06 14:48:22 +0200377 else if (mask == GPS_ON)
378 return read_gps_status();
Corentin Chary4564de12007-01-26 14:04:40 +0100379
Corentin Charybe18cda2007-01-26 14:04:35 +0100380 return (hotk->status & mask) ? 1 : 0;
381}
382
Corentin Chary935ffee2007-03-11 10:26:12 +0100383static void write_status(acpi_handle handle, int out, int mask)
Corentin Charybe18cda2007-01-26 14:04:35 +0100384{
385 hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
386
Corentin Chary935ffee2007-03-11 10:26:12 +0100387 switch (mask) {
388 case MLED_ON:
Roel Kluine1af14e2008-02-02 21:07:38 +0100389 out = !(out & 0x1);
Corentin Chary935ffee2007-03-11 10:26:12 +0100390 break;
Corentin Charyfdd8d082007-03-11 10:26:48 +0100391 case GLED_ON:
392 out = (out & 0x1) + 1;
393 break;
Corentin Charye539c2f2007-05-06 14:47:06 +0200394 case GPS_ON:
395 handle = (out) ? gps_on_handle : gps_off_handle;
396 out = 0x02;
397 break;
Corentin Chary935ffee2007-03-11 10:26:12 +0100398 default:
399 out &= 0x1;
400 break;
401 }
Corentin Charybe18cda2007-01-26 14:04:35 +0100402
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100403 if (write_acpi_int(handle, NULL, out, NULL))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200404 pr_warning(" write failed %x\n", mask);
Corentin Charybe18cda2007-01-26 14:04:35 +0100405}
406
407/* /sys/class/led handlers */
Corentin Chary935ffee2007-03-11 10:26:12 +0100408#define ASUS_LED_HANDLER(object, mask) \
Corentin Charybe18cda2007-01-26 14:04:35 +0100409 static void object##_led_set(struct led_classdev *led_cdev, \
410 enum led_brightness value) \
411 { \
Corentin Charye3deda92008-09-24 10:35:55 +0200412 object##_led_wk = (value > 0) ? 1 : 0; \
Corentin Charybe18cda2007-01-26 14:04:35 +0100413 queue_work(led_workqueue, &object##_led_work); \
414 } \
415 static void object##_led_update(struct work_struct *ignored) \
416 { \
417 int value = object##_led_wk; \
Corentin Chary935ffee2007-03-11 10:26:12 +0100418 write_status(object##_set_handle, value, (mask)); \
Corentin Charyabfa57e2009-08-28 12:56:47 +0000419 } \
420 static enum led_brightness object##_led_get( \
421 struct led_classdev *led_cdev) \
422 { \
423 return led_cdev->brightness; \
Corentin Charybe18cda2007-01-26 14:04:35 +0100424 }
425
Corentin Chary935ffee2007-03-11 10:26:12 +0100426ASUS_LED_HANDLER(mled, MLED_ON);
427ASUS_LED_HANDLER(pled, PLED_ON);
428ASUS_LED_HANDLER(rled, RLED_ON);
429ASUS_LED_HANDLER(tled, TLED_ON);
Corentin Charyfdd8d082007-03-11 10:26:48 +0100430ASUS_LED_HANDLER(gled, GLED_ON);
Corentin Charybe18cda2007-01-26 14:04:35 +0100431
Corentin Charyb7d3fbc2009-08-28 12:56:50 +0000432/*
433 * Keyboard backlight
434 */
435static int get_kled_lvl(void)
436{
437 unsigned long long kblv;
438 struct acpi_object_list params;
439 union acpi_object in_obj;
440 acpi_status rv;
441
442 params.count = 1;
443 params.pointer = &in_obj;
444 in_obj.type = ACPI_TYPE_INTEGER;
445 in_obj.integer.value = 2;
446
447 rv = acpi_evaluate_integer(kled_get_handle, NULL, &params, &kblv);
448 if (ACPI_FAILURE(rv)) {
449 pr_warning("Error reading kled level\n");
450 return 0;
451 }
452 return kblv;
453}
454
455static int set_kled_lvl(int kblv)
456{
457 if (kblv > 0)
458 kblv = (1 << 7) | (kblv & 0x7F);
459 else
460 kblv = 0;
461
462 if (write_acpi_int(kled_set_handle, NULL, kblv, NULL)) {
463 pr_warning("Keyboard LED display write failed\n");
464 return -EINVAL;
465 }
466 return 0;
467}
468
469static void kled_led_set(struct led_classdev *led_cdev,
470 enum led_brightness value)
471{
472 kled_led_wk = value;
473 queue_work(led_workqueue, &kled_led_work);
474}
475
476static void kled_led_update(struct work_struct *ignored)
477{
478 set_kled_lvl(kled_led_wk);
479}
480
481static enum led_brightness kled_led_get(struct led_classdev *led_cdev)
482{
483 return get_kled_lvl();
484}
485
Corentin Chary6b7091e2007-01-26 14:04:45 +0100486static int get_lcd_state(void)
487{
488 return read_status(LCD_ON);
489}
490
491static int set_lcd_state(int value)
492{
493 int lcd = 0;
494 acpi_status status = 0;
495
496 lcd = value ? 1 : 0;
497
498 if (lcd == get_lcd_state())
499 return 0;
500
Len Brown8def05f2007-01-30 01:46:43 -0500501 if (lcd_switch_handle) {
Corentin Chary6b7091e2007-01-26 14:04:45 +0100502 status = acpi_evaluate_object(lcd_switch_handle,
503 NULL, NULL, NULL);
504
505 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200506 pr_warning("Error switching LCD\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100507 }
508
Corentin Chary935ffee2007-03-11 10:26:12 +0100509 write_status(NULL, lcd, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100510 return 0;
511}
512
513static void lcd_blank(int blank)
514{
515 struct backlight_device *bd = asus_backlight_device;
516
Len Brown8def05f2007-01-30 01:46:43 -0500517 if (bd) {
Richard Purdie599a52d2007-02-10 23:07:48 +0000518 bd->props.power = blank;
Richard Purdie28ee0862007-02-08 22:25:09 +0000519 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100520 }
521}
522
523static int read_brightness(struct backlight_device *bd)
524{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400525 unsigned long long value;
Corentin Chary9a816852007-03-11 10:25:38 +0100526 acpi_status rv = AE_OK;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100527
Corentin Chary9a816852007-03-11 10:25:38 +0100528 rv = acpi_evaluate_integer(brightness_get_handle, NULL, NULL, &value);
529 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200530 pr_warning("Error reading brightness\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100531
532 return value;
533}
534
535static int set_brightness(struct backlight_device *bd, int value)
536{
537 int ret = 0;
538
539 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
540 /* 0 <= value <= 15 */
541
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100542 if (write_acpi_int(brightness_set_handle, NULL, value, NULL)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200543 pr_warning("Error changing brightness\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +0100544 ret = -EIO;
545 }
546
547 return ret;
548}
549
550static int update_bl_status(struct backlight_device *bd)
551{
552 int rv;
Richard Purdie599a52d2007-02-10 23:07:48 +0000553 int value = bd->props.brightness;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100554
555 rv = set_brightness(bd, value);
Len Brown8def05f2007-01-30 01:46:43 -0500556 if (rv)
Corentin Chary6b7091e2007-01-26 14:04:45 +0100557 return rv;
558
Richard Purdie599a52d2007-02-10 23:07:48 +0000559 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
Corentin Chary6b7091e2007-01-26 14:04:45 +0100560 return set_lcd_state(value);
561}
562
Corentin Chary85091b72007-01-26 14:04:30 +0100563/*
564 * Platform device handlers
565 */
566
567/*
568 * We write our info in page, we begin at offset off and cannot write more
569 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
570 * number of bytes written in page
571 */
572static ssize_t show_infos(struct device *dev,
Len Brown8def05f2007-01-30 01:46:43 -0500573 struct device_attribute *attr, char *page)
Corentin Chary85091b72007-01-26 14:04:30 +0100574{
575 int len = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400576 unsigned long long temp;
Corentin Chary85091b72007-01-26 14:04:30 +0100577 char buf[16]; //enough for all info
Corentin Chary9a816852007-03-11 10:25:38 +0100578 acpi_status rv = AE_OK;
579
Corentin Chary85091b72007-01-26 14:04:30 +0100580 /*
581 * We use the easy way, we don't care of off and count, so we don't set eof
582 * to 1
583 */
584
585 len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
586 len += sprintf(page + len, "Model reference : %s\n", hotk->name);
587 /*
588 * The SFUN method probably allows the original driver to get the list
589 * of features supported by a given model. For now, 0x0100 or 0x0800
590 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
591 * The significance of others is yet to be found.
592 */
Corentin Chary9a816852007-03-11 10:25:38 +0100593 rv = acpi_evaluate_integer(hotk->handle, "SFUN", NULL, &temp);
594 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000595 len += sprintf(page + len, "SFUN value : %#x\n",
596 (uint) temp);
597 /*
598 * The HWRS method return informations about the hardware.
599 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
600 * The significance of others is yet to be found.
601 * If we don't find the method, we assume the device are present.
602 */
603 rv = acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &temp);
604 if (!ACPI_FAILURE(rv))
605 len += sprintf(page + len, "HRWS value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100606 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100607 /*
608 * Another value for userspace: the ASYM method returns 0x02 for
609 * battery low and 0x04 for battery critical, its readings tend to be
610 * more accurate than those provided by _BST.
611 * Note: since not all the laptops provide this method, errors are
612 * silently ignored.
613 */
Corentin Chary9a816852007-03-11 10:25:38 +0100614 rv = acpi_evaluate_integer(hotk->handle, "ASYM", NULL, &temp);
615 if (!ACPI_FAILURE(rv))
Corentin Chary1d4a3802009-08-28 12:56:46 +0000616 len += sprintf(page + len, "ASYM value : %#x\n",
Corentin Chary9a816852007-03-11 10:25:38 +0100617 (uint) temp);
Corentin Chary85091b72007-01-26 14:04:30 +0100618 if (asus_info) {
619 snprintf(buf, 16, "%d", asus_info->length);
620 len += sprintf(page + len, "DSDT length : %s\n", buf);
621 snprintf(buf, 16, "%d", asus_info->checksum);
622 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
623 snprintf(buf, 16, "%d", asus_info->revision);
624 len += sprintf(page + len, "DSDT revision : %s\n", buf);
625 snprintf(buf, 7, "%s", asus_info->oem_id);
626 len += sprintf(page + len, "OEM id : %s\n", buf);
627 snprintf(buf, 9, "%s", asus_info->oem_table_id);
628 len += sprintf(page + len, "OEM table id : %s\n", buf);
629 snprintf(buf, 16, "%x", asus_info->oem_revision);
630 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
631 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
632 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
633 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
634 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
635 }
636
637 return len;
638}
639
640static int parse_arg(const char *buf, unsigned long count, int *val)
641{
642 if (!count)
643 return 0;
644 if (count > 31)
645 return -EINVAL;
646 if (sscanf(buf, "%i", val) != 1)
647 return -EINVAL;
648 return count;
649}
650
Corentin Chary4564de12007-01-26 14:04:40 +0100651static ssize_t store_status(const char *buf, size_t count,
Corentin Chary935ffee2007-03-11 10:26:12 +0100652 acpi_handle handle, int mask)
Corentin Chary4564de12007-01-26 14:04:40 +0100653{
654 int rv, value;
655 int out = 0;
656
657 rv = parse_arg(buf, count, &value);
658 if (rv > 0)
659 out = value ? 1 : 0;
660
Corentin Chary935ffee2007-03-11 10:26:12 +0100661 write_status(handle, out, mask);
Corentin Chary4564de12007-01-26 14:04:40 +0100662
663 return rv;
664}
665
666/*
Corentin Chary722ad972007-01-26 14:04:55 +0100667 * LEDD display
668 */
669static ssize_t show_ledd(struct device *dev,
670 struct device_attribute *attr, char *buf)
671{
672 return sprintf(buf, "0x%08x\n", hotk->ledd_status);
673}
674
675static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
676 const char *buf, size_t count)
677{
678 int rv, value;
679
680 rv = parse_arg(buf, count, &value);
681 if (rv > 0) {
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100682 if (write_acpi_int(ledd_set_handle, NULL, value, NULL))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200683 pr_warning("LED display write failed\n");
Corentin Chary722ad972007-01-26 14:04:55 +0100684 else
685 hotk->ledd_status = (u32) value;
686 }
687 return rv;
688}
689
690/*
Corentin Chary4564de12007-01-26 14:04:40 +0100691 * WLAN
692 */
693static ssize_t show_wlan(struct device *dev,
694 struct device_attribute *attr, char *buf)
695{
696 return sprintf(buf, "%d\n", read_status(WL_ON));
697}
698
699static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
700 const char *buf, size_t count)
701{
Corentin Chary935ffee2007-03-11 10:26:12 +0100702 return store_status(buf, count, wl_switch_handle, WL_ON);
Corentin Chary4564de12007-01-26 14:04:40 +0100703}
704
705/*
706 * Bluetooth
707 */
708static ssize_t show_bluetooth(struct device *dev,
709 struct device_attribute *attr, char *buf)
710{
711 return sprintf(buf, "%d\n", read_status(BT_ON));
712}
713
Len Brown8def05f2007-01-30 01:46:43 -0500714static ssize_t store_bluetooth(struct device *dev,
715 struct device_attribute *attr, const char *buf,
716 size_t count)
Corentin Chary4564de12007-01-26 14:04:40 +0100717{
Corentin Chary935ffee2007-03-11 10:26:12 +0100718 return store_status(buf, count, bt_switch_handle, BT_ON);
Corentin Chary4564de12007-01-26 14:04:40 +0100719}
720
Corentin Chary78127b42007-01-26 14:04:49 +0100721/*
722 * Display
723 */
724static void set_display(int value)
725{
726 /* no sanity check needed for now */
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100727 if (write_acpi_int(display_set_handle, NULL, value, NULL))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200728 pr_warning("Error setting display\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100729 return;
730}
731
732static int read_display(void)
733{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400734 unsigned long long value = 0;
Corentin Chary9a816852007-03-11 10:25:38 +0100735 acpi_status rv = AE_OK;
Corentin Chary78127b42007-01-26 14:04:49 +0100736
737 /* In most of the case, we know how to set the display, but sometime
738 we can't read it */
Len Brown8def05f2007-01-30 01:46:43 -0500739 if (display_get_handle) {
Corentin Chary9a816852007-03-11 10:25:38 +0100740 rv = acpi_evaluate_integer(display_get_handle, NULL,
741 NULL, &value);
742 if (ACPI_FAILURE(rv))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200743 pr_warning("Error reading display status\n");
Corentin Chary78127b42007-01-26 14:04:49 +0100744 }
745
746 value &= 0x0F; /* needed for some models, shouldn't hurt others */
747
748 return value;
749}
Len Brown8def05f2007-01-30 01:46:43 -0500750
Corentin Chary78127b42007-01-26 14:04:49 +0100751/*
752 * Now, *this* one could be more user-friendly, but so far, no-one has
753 * complained. The significance of bits is the same as in store_disp()
754 */
755static ssize_t show_disp(struct device *dev,
756 struct device_attribute *attr, char *buf)
757{
758 return sprintf(buf, "%d\n", read_display());
759}
760
761/*
762 * Experimental support for display switching. As of now: 1 should activate
763 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
764 * Any combination (bitwise) of these will suffice. I never actually tested 4
765 * displays hooked up simultaneously, so be warned. See the acpi4asus README
766 * for more info.
767 */
768static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
769 const char *buf, size_t count)
770{
771 int rv, value;
772
773 rv = parse_arg(buf, count, &value);
774 if (rv > 0)
775 set_display(value);
776 return rv;
777}
778
Corentin Chary8b857352007-01-26 14:04:58 +0100779/*
780 * Light Sens
781 */
782static void set_light_sens_switch(int value)
783{
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100784 if (write_acpi_int(ls_switch_handle, NULL, value, NULL))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200785 pr_warning("Error setting light sensor switch\n");
Corentin Chary8b857352007-01-26 14:04:58 +0100786 hotk->light_switch = value;
787}
788
789static ssize_t show_lssw(struct device *dev,
790 struct device_attribute *attr, char *buf)
791{
792 return sprintf(buf, "%d\n", hotk->light_switch);
793}
794
795static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
796 const char *buf, size_t count)
797{
798 int rv, value;
799
800 rv = parse_arg(buf, count, &value);
801 if (rv > 0)
802 set_light_sens_switch(value ? 1 : 0);
803
804 return rv;
805}
806
807static void set_light_sens_level(int value)
808{
Corentin CHARYf8d1c942008-01-16 16:56:42 +0100809 if (write_acpi_int(ls_level_handle, NULL, value, NULL))
Corentin Chary2fcc23d2009-06-19 14:52:03 +0200810 pr_warning("Error setting light sensor level\n");
Corentin Chary8b857352007-01-26 14:04:58 +0100811 hotk->light_level = value;
812}
813
814static ssize_t show_lslvl(struct device *dev,
815 struct device_attribute *attr, char *buf)
816{
817 return sprintf(buf, "%d\n", hotk->light_level);
818}
819
820static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
821 const char *buf, size_t count)
822{
823 int rv, value;
824
825 rv = parse_arg(buf, count, &value);
826 if (rv > 0) {
827 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
828 /* 0 <= value <= 15 */
829 set_light_sens_level(value);
830 }
831
832 return rv;
833}
834
Corentin Charye539c2f2007-05-06 14:47:06 +0200835/*
836 * GPS
837 */
838static ssize_t show_gps(struct device *dev,
839 struct device_attribute *attr, char *buf)
840{
841 return sprintf(buf, "%d\n", read_status(GPS_ON));
842}
843
844static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
845 const char *buf, size_t count)
846{
Corentin Charyf3985322007-05-06 14:48:22 +0200847 return store_status(buf, count, NULL, GPS_ON);
Corentin Charye539c2f2007-05-06 14:47:06 +0200848}
849
Corentin Chary034ce902009-01-20 16:17:43 +0100850/*
851 * Hotkey functions
852 */
853static struct key_entry *asus_get_entry_by_scancode(int code)
854{
855 struct key_entry *key;
856
857 for (key = asus_keymap; key->type != KE_END; key++)
858 if (code == key->code)
859 return key;
860
861 return NULL;
862}
863
864static struct key_entry *asus_get_entry_by_keycode(int code)
865{
866 struct key_entry *key;
867
868 for (key = asus_keymap; key->type != KE_END; key++)
869 if (code == key->keycode && key->type == KE_KEY)
870 return key;
871
872 return NULL;
873}
874
875static int asus_getkeycode(struct input_dev *dev, int scancode, int *keycode)
876{
877 struct key_entry *key = asus_get_entry_by_scancode(scancode);
878
879 if (key && key->type == KE_KEY) {
880 *keycode = key->keycode;
881 return 0;
882 }
883
884 return -EINVAL;
885}
886
887static int asus_setkeycode(struct input_dev *dev, int scancode, int keycode)
888{
889 struct key_entry *key;
890 int old_keycode;
891
892 if (keycode < 0 || keycode > KEY_MAX)
893 return -EINVAL;
894
895 key = asus_get_entry_by_scancode(scancode);
896 if (key && key->type == KE_KEY) {
897 old_keycode = key->keycode;
898 key->keycode = keycode;
899 set_bit(keycode, dev->keybit);
900 if (!asus_get_entry_by_keycode(old_keycode))
901 clear_bit(old_keycode, dev->keybit);
902 return 0;
903 }
904
905 return -EINVAL;
906}
907
Bjorn Helgaas586ed162009-04-30 09:35:53 -0600908static void asus_hotk_notify(struct acpi_device *device, u32 event)
Corentin Chary85091b72007-01-26 14:04:30 +0100909{
Corentin Chary034ce902009-01-20 16:17:43 +0100910 static struct key_entry *key;
Corentin Chary6050c8d2009-02-15 19:30:19 +0100911 u16 count;
Corentin Chary034ce902009-01-20 16:17:43 +0100912
Corentin Chary85091b72007-01-26 14:04:30 +0100913 /* TODO Find a better way to handle events count. */
914 if (!hotk)
915 return;
916
Corentin Chary6b7091e2007-01-26 14:04:45 +0100917 /*
918 * We need to tell the backlight device when the backlight power is
919 * switched
920 */
921 if (event == ATKD_LCD_ON) {
Corentin Chary935ffee2007-03-11 10:26:12 +0100922 write_status(NULL, 1, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100923 lcd_blank(FB_BLANK_UNBLANK);
Len Brown8def05f2007-01-30 01:46:43 -0500924 } else if (event == ATKD_LCD_OFF) {
Corentin Chary935ffee2007-03-11 10:26:12 +0100925 write_status(NULL, 0, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +0100926 lcd_blank(FB_BLANK_POWERDOWN);
927 }
928
Corentin Chary6050c8d2009-02-15 19:30:19 +0100929 count = hotk->event_count[event % 128]++;
930 acpi_bus_generate_proc_event(hotk->device, event, count);
Corentin Chary2a7dc0d2009-01-20 16:17:42 +0100931 acpi_bus_generate_netlink_event(hotk->device->pnp.device_class,
932 dev_name(&hotk->device->dev), event,
Corentin Chary6050c8d2009-02-15 19:30:19 +0100933 count);
Corentin Chary85091b72007-01-26 14:04:30 +0100934
Corentin Chary034ce902009-01-20 16:17:43 +0100935 if (hotk->inputdev) {
936 key = asus_get_entry_by_scancode(event);
937 if (!key)
938 return ;
939
940 switch (key->type) {
941 case KE_KEY:
942 input_report_key(hotk->inputdev, key->keycode, 1);
943 input_sync(hotk->inputdev);
944 input_report_key(hotk->inputdev, key->keycode, 0);
945 input_sync(hotk->inputdev);
946 break;
947 }
948 }
Corentin Chary85091b72007-01-26 14:04:30 +0100949}
950
951#define ASUS_CREATE_DEVICE_ATTR(_name) \
952 struct device_attribute dev_attr_##_name = { \
953 .attr = { \
954 .name = __stringify(_name), \
Tejun Heo7b595752007-06-14 03:45:17 +0900955 .mode = 0 }, \
Corentin Chary85091b72007-01-26 14:04:30 +0100956 .show = NULL, \
957 .store = NULL, \
958 }
959
960#define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
961 do { \
962 dev_attr_##_name.attr.mode = _mode; \
963 dev_attr_##_name.show = _show; \
964 dev_attr_##_name.store = _store; \
965 } while(0)
966
967static ASUS_CREATE_DEVICE_ATTR(infos);
Corentin Chary4564de12007-01-26 14:04:40 +0100968static ASUS_CREATE_DEVICE_ATTR(wlan);
969static ASUS_CREATE_DEVICE_ATTR(bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +0100970static ASUS_CREATE_DEVICE_ATTR(display);
Corentin Chary722ad972007-01-26 14:04:55 +0100971static ASUS_CREATE_DEVICE_ATTR(ledd);
Corentin Chary8b857352007-01-26 14:04:58 +0100972static ASUS_CREATE_DEVICE_ATTR(ls_switch);
973static ASUS_CREATE_DEVICE_ATTR(ls_level);
Corentin Charye539c2f2007-05-06 14:47:06 +0200974static ASUS_CREATE_DEVICE_ATTR(gps);
Corentin Chary85091b72007-01-26 14:04:30 +0100975
976static struct attribute *asuspf_attributes[] = {
Len Brown8def05f2007-01-30 01:46:43 -0500977 &dev_attr_infos.attr,
978 &dev_attr_wlan.attr,
979 &dev_attr_bluetooth.attr,
980 &dev_attr_display.attr,
981 &dev_attr_ledd.attr,
982 &dev_attr_ls_switch.attr,
983 &dev_attr_ls_level.attr,
Corentin Charye539c2f2007-05-06 14:47:06 +0200984 &dev_attr_gps.attr,
Len Brown8def05f2007-01-30 01:46:43 -0500985 NULL
Corentin Chary85091b72007-01-26 14:04:30 +0100986};
987
988static struct attribute_group asuspf_attribute_group = {
Len Brown8def05f2007-01-30 01:46:43 -0500989 .attrs = asuspf_attributes
Corentin Chary85091b72007-01-26 14:04:30 +0100990};
991
992static struct platform_driver asuspf_driver = {
Len Brown8def05f2007-01-30 01:46:43 -0500993 .driver = {
994 .name = ASUS_HOTK_FILE,
995 .owner = THIS_MODULE,
996 }
Corentin Chary85091b72007-01-26 14:04:30 +0100997};
998
999static struct platform_device *asuspf_device;
1000
Corentin Chary85091b72007-01-26 14:04:30 +01001001static void asus_hotk_add_fs(void)
1002{
1003 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
Corentin Chary4564de12007-01-26 14:04:40 +01001004
1005 if (wl_switch_handle)
1006 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
1007
1008 if (bt_switch_handle)
1009 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
1010 show_bluetooth, store_bluetooth);
Corentin Chary78127b42007-01-26 14:04:49 +01001011
1012 if (display_set_handle && display_get_handle)
1013 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
Len Brown8def05f2007-01-30 01:46:43 -05001014 else if (display_set_handle)
Corentin Chary78127b42007-01-26 14:04:49 +01001015 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
1016
Corentin Chary722ad972007-01-26 14:04:55 +01001017 if (ledd_set_handle)
1018 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
1019
Corentin Chary8b857352007-01-26 14:04:58 +01001020 if (ls_switch_handle && ls_level_handle) {
1021 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
1022 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
1023 }
Corentin Charye539c2f2007-05-06 14:47:06 +02001024
Corentin Charyf3985322007-05-06 14:48:22 +02001025 if (gps_status_handle && gps_on_handle && gps_off_handle)
Corentin Charye539c2f2007-05-06 14:47:06 +02001026 ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
Corentin Chary85091b72007-01-26 14:04:30 +01001027}
1028
Len Brown8def05f2007-01-30 01:46:43 -05001029static int asus_handle_init(char *name, acpi_handle * handle,
Corentin Chary85091b72007-01-26 14:04:30 +01001030 char **paths, int num_paths)
1031{
1032 int i;
1033 acpi_status status;
1034
1035 for (i = 0; i < num_paths; i++) {
1036 status = acpi_get_handle(NULL, paths[i], handle);
1037 if (ACPI_SUCCESS(status))
1038 return 0;
1039 }
1040
1041 *handle = NULL;
1042 return -ENODEV;
1043}
1044
1045#define ASUS_HANDLE_INIT(object) \
1046 asus_handle_init(#object, &object##_handle, object##_paths, \
1047 ARRAY_SIZE(object##_paths))
1048
Corentin Chary85091b72007-01-26 14:04:30 +01001049/*
1050 * This function is used to initialize the hotk with right values. In this
1051 * method, we can make all the detection we want, and modify the hotk struct
1052 */
1053static int asus_hotk_get_info(void)
1054{
1055 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Corentin Chary85091b72007-01-26 14:04:30 +01001056 union acpi_object *model = NULL;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001057 unsigned long long bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +01001058 char *string = NULL;
1059 acpi_status status;
1060
1061 /*
1062 * Get DSDT headers early enough to allow for differentiating between
1063 * models, but late enough to allow acpi_bus_register_driver() to fail
1064 * before doing anything ACPI-specific. Should we encounter a machine,
1065 * which needs special handling (i.e. its hotkey device has a different
1066 * HID), this bit will be moved. A global variable asus_info contains
1067 * the DSDT header.
1068 */
Len Brown894d79b2007-02-03 02:13:53 -05001069 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
Corentin Chary85091b72007-01-26 14:04:30 +01001070 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001071 pr_warning("Couldn't get the DSDT table header\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001072
1073 /* We have to write 0 on init this far for all ASUS models */
Corentin CHARYf8d1c942008-01-16 16:56:42 +01001074 if (write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001075 pr_err("Hotkey initialization failed\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001076 return -ENODEV;
1077 }
1078
1079 /* This needs to be called for some laptops to init properly */
Corentin Chary9a816852007-03-11 10:25:38 +01001080 status =
1081 acpi_evaluate_integer(hotk->handle, "BSTS", NULL, &bsts_result);
1082 if (ACPI_FAILURE(status))
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001083 pr_warning("Error calling BSTS\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001084 else if (bsts_result)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001085 pr_notice("BSTS called, 0x%02x returned\n",
Corentin Chary9a816852007-03-11 10:25:38 +01001086 (uint) bsts_result);
Corentin Chary85091b72007-01-26 14:04:30 +01001087
Corentin Chary185e5af2007-03-11 10:27:33 +01001088 /* This too ... */
1089 write_acpi_int(hotk->handle, "CWAP", wapf, NULL);
1090
Corentin Chary85091b72007-01-26 14:04:30 +01001091 /*
1092 * Try to match the object returned by INIT to the specific model.
1093 * Handle every possible object (or the lack of thereof) the DSDT
1094 * writers might throw at us. When in trouble, we pass NULL to
1095 * asus_model_match() and try something completely different.
1096 */
1097 if (buffer.pointer) {
1098 model = buffer.pointer;
1099 switch (model->type) {
1100 case ACPI_TYPE_STRING:
1101 string = model->string.pointer;
1102 break;
1103 case ACPI_TYPE_BUFFER:
1104 string = model->buffer.pointer;
1105 break;
1106 default:
1107 string = "";
1108 break;
1109 }
1110 }
1111 hotk->name = kstrdup(string, GFP_KERNEL);
1112 if (!hotk->name)
1113 return -ENOMEM;
1114
Len Brown8def05f2007-01-30 01:46:43 -05001115 if (*string)
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001116 pr_notice(" %s model detected\n", string);
Corentin Chary85091b72007-01-26 14:04:30 +01001117
Corentin Charybe18cda2007-01-26 14:04:35 +01001118 ASUS_HANDLE_INIT(mled_set);
1119 ASUS_HANDLE_INIT(tled_set);
1120 ASUS_HANDLE_INIT(rled_set);
1121 ASUS_HANDLE_INIT(pled_set);
Corentin Charyfdd8d082007-03-11 10:26:48 +01001122 ASUS_HANDLE_INIT(gled_set);
Corentin Charybe18cda2007-01-26 14:04:35 +01001123
Corentin Chary722ad972007-01-26 14:04:55 +01001124 ASUS_HANDLE_INIT(ledd_set);
1125
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001126 ASUS_HANDLE_INIT(kled_set);
1127 ASUS_HANDLE_INIT(kled_get);
1128
Corentin Chary4564de12007-01-26 14:04:40 +01001129 /*
1130 * The HWRS method return informations about the hardware.
1131 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1132 * The significance of others is yet to be found.
1133 * If we don't find the method, we assume the device are present.
1134 */
Corentin Chary9a816852007-03-11 10:25:38 +01001135 status =
1136 acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &hwrs_result);
1137 if (ACPI_FAILURE(status))
Corentin Chary4564de12007-01-26 14:04:40 +01001138 hwrs_result = WL_HWRS | BT_HWRS;
1139
Len Brown8def05f2007-01-30 01:46:43 -05001140 if (hwrs_result & WL_HWRS)
Corentin Chary4564de12007-01-26 14:04:40 +01001141 ASUS_HANDLE_INIT(wl_switch);
Len Brown8def05f2007-01-30 01:46:43 -05001142 if (hwrs_result & BT_HWRS)
Corentin Chary4564de12007-01-26 14:04:40 +01001143 ASUS_HANDLE_INIT(bt_switch);
1144
1145 ASUS_HANDLE_INIT(wireless_status);
1146
Corentin Chary6b7091e2007-01-26 14:04:45 +01001147 ASUS_HANDLE_INIT(brightness_set);
1148 ASUS_HANDLE_INIT(brightness_get);
1149
1150 ASUS_HANDLE_INIT(lcd_switch);
1151
Corentin Chary78127b42007-01-26 14:04:49 +01001152 ASUS_HANDLE_INIT(display_set);
1153 ASUS_HANDLE_INIT(display_get);
1154
Corentin Chary8b857352007-01-26 14:04:58 +01001155 /* There is a lot of models with "ALSL", but a few get
1156 a real light sens, so we need to check it. */
Corentin Chary832d9952007-05-06 14:47:29 +02001157 if (!ASUS_HANDLE_INIT(ls_switch))
Corentin Chary8b857352007-01-26 14:04:58 +01001158 ASUS_HANDLE_INIT(ls_level);
1159
Corentin Charye539c2f2007-05-06 14:47:06 +02001160 ASUS_HANDLE_INIT(gps_on);
1161 ASUS_HANDLE_INIT(gps_off);
1162 ASUS_HANDLE_INIT(gps_status);
1163
Corentin Chary85091b72007-01-26 14:04:30 +01001164 kfree(model);
1165
1166 return AE_OK;
1167}
1168
Corentin Chary034ce902009-01-20 16:17:43 +01001169static int asus_input_init(void)
1170{
1171 const struct key_entry *key;
1172 int result;
1173
1174 hotk->inputdev = input_allocate_device();
1175 if (!hotk->inputdev) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001176 pr_info("Unable to allocate input device\n");
Corentin Chary034ce902009-01-20 16:17:43 +01001177 return 0;
1178 }
1179 hotk->inputdev->name = "Asus Laptop extra buttons";
1180 hotk->inputdev->phys = ASUS_HOTK_FILE "/input0";
1181 hotk->inputdev->id.bustype = BUS_HOST;
1182 hotk->inputdev->getkeycode = asus_getkeycode;
1183 hotk->inputdev->setkeycode = asus_setkeycode;
1184
1185 for (key = asus_keymap; key->type != KE_END; key++) {
1186 switch (key->type) {
1187 case KE_KEY:
1188 set_bit(EV_KEY, hotk->inputdev->evbit);
1189 set_bit(key->keycode, hotk->inputdev->keybit);
1190 break;
1191 }
1192 }
1193 result = input_register_device(hotk->inputdev);
1194 if (result) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001195 pr_info("Unable to register input device\n");
Corentin Chary034ce902009-01-20 16:17:43 +01001196 input_free_device(hotk->inputdev);
1197 }
1198 return result;
1199}
1200
Corentin Chary85091b72007-01-26 14:04:30 +01001201static int asus_hotk_check(void)
1202{
1203 int result = 0;
1204
1205 result = acpi_bus_get_status(hotk->device);
1206 if (result)
1207 return result;
1208
1209 if (hotk->device->status.present) {
1210 result = asus_hotk_get_info();
1211 } else {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001212 pr_err("Hotkey device not present, aborting\n");
Corentin Chary85091b72007-01-26 14:04:30 +01001213 return -EINVAL;
1214 }
1215
1216 return result;
1217}
1218
1219static int asus_hotk_found;
1220
1221static int asus_hotk_add(struct acpi_device *device)
1222{
Corentin Chary85091b72007-01-26 14:04:30 +01001223 int result;
1224
1225 if (!device)
1226 return -EINVAL;
1227
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001228 pr_notice("Asus Laptop Support version %s\n",
Corentin Chary85091b72007-01-26 14:04:30 +01001229 ASUS_LAPTOP_VERSION);
1230
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001231 hotk = kzalloc(sizeof(struct asus_hotk), GFP_KERNEL);
Corentin Chary85091b72007-01-26 14:04:30 +01001232 if (!hotk)
1233 return -ENOMEM;
Corentin Chary85091b72007-01-26 14:04:30 +01001234
1235 hotk->handle = device->handle;
1236 strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
1237 strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001238 device->driver_data = hotk;
Corentin Chary85091b72007-01-26 14:04:30 +01001239 hotk->device = device;
1240
1241 result = asus_hotk_check();
1242 if (result)
1243 goto end;
1244
1245 asus_hotk_add_fs();
1246
Corentin Chary85091b72007-01-26 14:04:30 +01001247 asus_hotk_found = 1;
1248
Corentin Chary6b7091e2007-01-26 14:04:45 +01001249 /* WLED and BLED are on by default */
Corentin Chary935ffee2007-03-11 10:26:12 +01001250 write_status(bt_switch_handle, 1, BT_ON);
1251 write_status(wl_switch_handle, 1, WL_ON);
1252
1253 /* If the h/w switch is off, we need to check the real status */
1254 write_status(NULL, read_status(BT_ON), BT_ON);
1255 write_status(NULL, read_status(WL_ON), WL_ON);
Corentin Chary4564de12007-01-26 14:04:40 +01001256
Corentin Chary6b7091e2007-01-26 14:04:45 +01001257 /* LCD Backlight is on by default */
Corentin Chary935ffee2007-03-11 10:26:12 +01001258 write_status(NULL, 1, LCD_ON);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001259
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001260 /* Keyboard Backlight is on by default */
1261 if (kled_set_handle)
1262 set_kled_lvl(1);
1263
Corentin Chary722ad972007-01-26 14:04:55 +01001264 /* LED display is off by default */
1265 hotk->ledd_status = 0xFFF;
1266
Len Brown8def05f2007-01-30 01:46:43 -05001267 /* Set initial values of light sensor and level */
1268 hotk->light_switch = 1; /* Default to light sensor disabled */
1269 hotk->light_level = 0; /* level 5 for sensor sensitivity */
Corentin Chary8b857352007-01-26 14:04:58 +01001270
Len Brown8def05f2007-01-30 01:46:43 -05001271 if (ls_switch_handle)
1272 set_light_sens_switch(hotk->light_switch);
Corentin Chary8b857352007-01-26 14:04:58 +01001273
Len Brown8def05f2007-01-30 01:46:43 -05001274 if (ls_level_handle)
1275 set_light_sens_level(hotk->light_level);
Corentin Chary8b857352007-01-26 14:04:58 +01001276
Corentin Charye539c2f2007-05-06 14:47:06 +02001277 /* GPS is on by default */
1278 write_status(NULL, 1, GPS_ON);
1279
Corentin Charyed6f4422009-01-20 16:17:45 +01001280end:
Corentin Chary85091b72007-01-26 14:04:30 +01001281 if (result) {
1282 kfree(hotk->name);
1283 kfree(hotk);
1284 }
1285
1286 return result;
1287}
1288
1289static int asus_hotk_remove(struct acpi_device *device, int type)
1290{
Corentin Chary85091b72007-01-26 14:04:30 +01001291 if (!device || !acpi_driver_data(device))
1292 return -EINVAL;
1293
Corentin Chary85091b72007-01-26 14:04:30 +01001294 kfree(hotk->name);
1295 kfree(hotk);
1296
1297 return 0;
1298}
1299
Corentin Chary6b7091e2007-01-26 14:04:45 +01001300static void asus_backlight_exit(void)
1301{
Len Brown8def05f2007-01-30 01:46:43 -05001302 if (asus_backlight_device)
Corentin Chary6b7091e2007-01-26 14:04:45 +01001303 backlight_device_unregister(asus_backlight_device);
1304}
1305
Corentin Charybe18cda2007-01-26 14:04:35 +01001306#define ASUS_LED_UNREGISTER(object) \
Guillaume Chazaraine1996a692007-08-16 18:18:53 +02001307 if (object##_led.dev) \
1308 led_classdev_unregister(&object##_led)
Corentin Charybe18cda2007-01-26 14:04:35 +01001309
1310static void asus_led_exit(void)
1311{
Al Viro3b0d7112007-07-23 11:21:34 +01001312 destroy_workqueue(led_workqueue);
Corentin Charybe18cda2007-01-26 14:04:35 +01001313 ASUS_LED_UNREGISTER(mled);
1314 ASUS_LED_UNREGISTER(tled);
1315 ASUS_LED_UNREGISTER(pled);
1316 ASUS_LED_UNREGISTER(rled);
Corentin Charyfdd8d082007-03-11 10:26:48 +01001317 ASUS_LED_UNREGISTER(gled);
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001318 ASUS_LED_UNREGISTER(kled);
Corentin Charybe18cda2007-01-26 14:04:35 +01001319}
1320
Corentin Chary034ce902009-01-20 16:17:43 +01001321static void asus_input_exit(void)
1322{
1323 if (hotk->inputdev)
1324 input_unregister_device(hotk->inputdev);
1325}
1326
Corentin Chary85091b72007-01-26 14:04:30 +01001327static void __exit asus_laptop_exit(void)
1328{
Corentin Chary6b7091e2007-01-26 14:04:45 +01001329 asus_backlight_exit();
Corentin Charybe18cda2007-01-26 14:04:35 +01001330 asus_led_exit();
Corentin Chary034ce902009-01-20 16:17:43 +01001331 asus_input_exit();
Corentin Charybe18cda2007-01-26 14:04:35 +01001332
Corentin Chary85091b72007-01-26 14:04:30 +01001333 acpi_bus_unregister_driver(&asus_hotk_driver);
Len Brown8def05f2007-01-30 01:46:43 -05001334 sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
1335 platform_device_unregister(asuspf_device);
1336 platform_driver_unregister(&asuspf_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001337}
1338
Len Brown8def05f2007-01-30 01:46:43 -05001339static int asus_backlight_init(struct device *dev)
Corentin Chary6b7091e2007-01-26 14:04:45 +01001340{
1341 struct backlight_device *bd;
1342
Len Brown8def05f2007-01-30 01:46:43 -05001343 if (brightness_set_handle && lcd_switch_handle) {
1344 bd = backlight_device_register(ASUS_HOTK_FILE, dev,
Richard Purdie599a52d2007-02-10 23:07:48 +00001345 NULL, &asusbl_ops);
Len Brown8def05f2007-01-30 01:46:43 -05001346 if (IS_ERR(bd)) {
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001347 pr_err("Could not register asus backlight device\n");
Corentin Chary6b7091e2007-01-26 14:04:45 +01001348 asus_backlight_device = NULL;
1349 return PTR_ERR(bd);
1350 }
1351
1352 asus_backlight_device = bd;
1353
Richard Purdie599a52d2007-02-10 23:07:48 +00001354 bd->props.max_brightness = 15;
1355 bd->props.brightness = read_brightness(NULL);
1356 bd->props.power = FB_BLANK_UNBLANK;
Richard Purdie28ee0862007-02-08 22:25:09 +00001357 backlight_update_status(bd);
Corentin Chary6b7091e2007-01-26 14:04:45 +01001358 }
1359 return 0;
1360}
1361
Corentin Charybe18cda2007-01-26 14:04:35 +01001362static int asus_led_register(acpi_handle handle,
Len Brown8def05f2007-01-30 01:46:43 -05001363 struct led_classdev *ldev, struct device *dev)
Corentin Charybe18cda2007-01-26 14:04:35 +01001364{
Len Brown8def05f2007-01-30 01:46:43 -05001365 if (!handle)
Corentin Charybe18cda2007-01-26 14:04:35 +01001366 return 0;
1367
1368 return led_classdev_register(dev, ldev);
1369}
Len Brown8def05f2007-01-30 01:46:43 -05001370
Corentin Charybe18cda2007-01-26 14:04:35 +01001371#define ASUS_LED_REGISTER(object, device) \
1372 asus_led_register(object##_set_handle, &object##_led, device)
1373
Len Brown8def05f2007-01-30 01:46:43 -05001374static int asus_led_init(struct device *dev)
Corentin Charybe18cda2007-01-26 14:04:35 +01001375{
1376 int rv;
1377
1378 rv = ASUS_LED_REGISTER(mled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001379 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001380 goto out;
Corentin Charybe18cda2007-01-26 14:04:35 +01001381
1382 rv = ASUS_LED_REGISTER(tled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001383 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001384 goto out1;
Corentin Charybe18cda2007-01-26 14:04:35 +01001385
1386 rv = ASUS_LED_REGISTER(rled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001387 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001388 goto out2;
Corentin Charybe18cda2007-01-26 14:04:35 +01001389
1390 rv = ASUS_LED_REGISTER(pled, dev);
Len Brown8def05f2007-01-30 01:46:43 -05001391 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001392 goto out3;
Corentin Charybe18cda2007-01-26 14:04:35 +01001393
Corentin Charyfdd8d082007-03-11 10:26:48 +01001394 rv = ASUS_LED_REGISTER(gled, dev);
1395 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001396 goto out4;
Corentin Charyfdd8d082007-03-11 10:26:48 +01001397
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001398 if (kled_set_handle && kled_get_handle)
1399 rv = ASUS_LED_REGISTER(kled, dev);
1400 if (rv)
Al Viro3b0d7112007-07-23 11:21:34 +01001401 goto out5;
Corentin Charybe18cda2007-01-26 14:04:35 +01001402
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001403 led_workqueue = create_singlethread_workqueue("led_workqueue");
1404 if (!led_workqueue)
1405 goto out6;
1406
Corentin Charybe18cda2007-01-26 14:04:35 +01001407 return 0;
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001408out6:
Al Viro3b0d7112007-07-23 11:21:34 +01001409 rv = -ENOMEM;
Corentin Charyb7d3fbc2009-08-28 12:56:50 +00001410 ASUS_LED_UNREGISTER(kled);
1411out5:
Al Viro3b0d7112007-07-23 11:21:34 +01001412 ASUS_LED_UNREGISTER(gled);
1413out4:
1414 ASUS_LED_UNREGISTER(pled);
1415out3:
1416 ASUS_LED_UNREGISTER(rled);
1417out2:
1418 ASUS_LED_UNREGISTER(tled);
1419out1:
1420 ASUS_LED_UNREGISTER(mled);
1421out:
1422 return rv;
Corentin Charybe18cda2007-01-26 14:04:35 +01001423}
1424
Corentin Chary85091b72007-01-26 14:04:30 +01001425static int __init asus_laptop_init(void)
1426{
1427 int result;
1428
1429 if (acpi_disabled)
1430 return -ENODEV;
1431
Corentin Chary85091b72007-01-26 14:04:30 +01001432 result = acpi_bus_register_driver(&asus_hotk_driver);
1433 if (result < 0)
1434 return result;
1435
1436 /*
1437 * This is a bit of a kludge. We only want this module loaded
1438 * for ASUS systems, but there's currently no way to probe the
1439 * ACPI namespace for ASUS HIDs. So we just return failure if
1440 * we didn't find one, which will cause the module to be
1441 * unloaded.
1442 */
1443 if (!asus_hotk_found) {
1444 acpi_bus_unregister_driver(&asus_hotk_driver);
1445 return -ENODEV;
1446 }
1447
Corentin Chary034ce902009-01-20 16:17:43 +01001448 result = asus_input_init();
1449 if (result)
1450 goto fail_input;
1451
Len Brown8def05f2007-01-30 01:46:43 -05001452 /* Register platform stuff */
Corentin Chary85091b72007-01-26 14:04:30 +01001453 result = platform_driver_register(&asuspf_driver);
Len Brown8def05f2007-01-30 01:46:43 -05001454 if (result)
1455 goto fail_platform_driver;
Corentin Chary85091b72007-01-26 14:04:30 +01001456
Len Brown8def05f2007-01-30 01:46:43 -05001457 asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
1458 if (!asuspf_device) {
1459 result = -ENOMEM;
1460 goto fail_platform_device1;
1461 }
Corentin Chary85091b72007-01-26 14:04:30 +01001462
Len Brown8def05f2007-01-30 01:46:43 -05001463 result = platform_device_add(asuspf_device);
1464 if (result)
1465 goto fail_platform_device2;
Corentin Chary85091b72007-01-26 14:04:30 +01001466
Len Brown8def05f2007-01-30 01:46:43 -05001467 result = sysfs_create_group(&asuspf_device->dev.kobj,
Corentin Chary85091b72007-01-26 14:04:30 +01001468 &asuspf_attribute_group);
Len Brown8def05f2007-01-30 01:46:43 -05001469 if (result)
1470 goto fail_sysfs;
Corentin Chary85091b72007-01-26 14:04:30 +01001471
Corentin Chary116bf2e2009-06-16 19:28:46 +00001472 result = asus_led_init(&asuspf_device->dev);
1473 if (result)
1474 goto fail_led;
1475
1476 if (!acpi_video_backlight_support()) {
1477 result = asus_backlight_init(&asuspf_device->dev);
1478 if (result)
1479 goto fail_backlight;
1480 } else
Corentin Chary2fcc23d2009-06-19 14:52:03 +02001481 pr_info("Brightness ignored, must be controlled by "
Corentin Chary116bf2e2009-06-16 19:28:46 +00001482 "ACPI video driver\n");
1483
Len Brown8def05f2007-01-30 01:46:43 -05001484 return 0;
Corentin Chary85091b72007-01-26 14:04:30 +01001485
Corentin Chary116bf2e2009-06-16 19:28:46 +00001486fail_backlight:
1487 asus_led_exit();
1488
1489fail_led:
1490 sysfs_remove_group(&asuspf_device->dev.kobj,
1491 &asuspf_attribute_group);
1492
Corentin Charyed6f4422009-01-20 16:17:45 +01001493fail_sysfs:
Len Brown8def05f2007-01-30 01:46:43 -05001494 platform_device_del(asuspf_device);
Corentin Chary85091b72007-01-26 14:04:30 +01001495
Corentin Charyed6f4422009-01-20 16:17:45 +01001496fail_platform_device2:
Corentin Chary85091b72007-01-26 14:04:30 +01001497 platform_device_put(asuspf_device);
1498
Corentin Charyed6f4422009-01-20 16:17:45 +01001499fail_platform_device1:
Len Brown8def05f2007-01-30 01:46:43 -05001500 platform_driver_unregister(&asuspf_driver);
Corentin Chary85091b72007-01-26 14:04:30 +01001501
Corentin Charyed6f4422009-01-20 16:17:45 +01001502fail_platform_driver:
Corentin Chary034ce902009-01-20 16:17:43 +01001503 asus_input_exit();
1504
Corentin Charyed6f4422009-01-20 16:17:45 +01001505fail_input:
Corentin Chary85091b72007-01-26 14:04:30 +01001506
1507 return result;
1508}
1509
1510module_init(asus_laptop_init);
1511module_exit(asus_laptop_exit);