asus-laptop: platform dev as parent for led and backlight
[linux-2.6/mini2440.git] / drivers / platform / x86 / asus-laptop.c
blobfaa87d3e3983088d41f32223af3076624e667c52
1 /*
2 * asus-laptop.c - Asus Laptop Support
5 * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
6 * Copyright (C) 2006-2007 Corentin Chary
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.
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.
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
23 * The development page for this driver is located at
24 * http://sourceforge.net/projects/acpi4asus/
26 * Credits:
27 * Pontus Fuchs - Helper functions, cleanup
28 * Johann Wiesner - Small compile fixes
29 * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
30 * Eric Burghard - LED display support for W1N
31 * Josh Green - Light Sens support
32 * Thomas Tuttle - His first patch for led support was very helpfull
33 * Sam Lin - GPS support
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/types.h>
40 #include <linux/err.h>
41 #include <linux/proc_fs.h>
42 #include <linux/backlight.h>
43 #include <linux/fb.h>
44 #include <linux/leds.h>
45 #include <linux/platform_device.h>
46 #include <acpi/acpi_drivers.h>
47 #include <acpi/acpi_bus.h>
48 #include <asm/uaccess.h>
49 #include <linux/input.h>
51 #define ASUS_LAPTOP_VERSION "0.42"
53 #define ASUS_HOTK_NAME "Asus Laptop Support"
54 #define ASUS_HOTK_CLASS "hotkey"
55 #define ASUS_HOTK_DEVICE_NAME "Hotkey"
56 #define ASUS_HOTK_FILE "asus-laptop"
57 #define ASUS_HOTK_PREFIX "\\_SB.ATKD."
60 * Some events we use, same for all Asus
62 #define ATKD_BR_UP 0x10
63 #define ATKD_BR_DOWN 0x20
64 #define ATKD_LCD_ON 0x33
65 #define ATKD_LCD_OFF 0x34
68 * Known bits returned by \_SB.ATKD.HWRS
70 #define WL_HWRS 0x80
71 #define BT_HWRS 0x100
74 * Flags for hotk status
75 * WL_ON and BT_ON are also used for wireless_status()
77 #define WL_ON 0x01 //internal Wifi
78 #define BT_ON 0x02 //internal Bluetooth
79 #define MLED_ON 0x04 //mail LED
80 #define TLED_ON 0x08 //touchpad LED
81 #define RLED_ON 0x10 //Record LED
82 #define PLED_ON 0x20 //Phone LED
83 #define GLED_ON 0x40 //Gaming LED
84 #define LCD_ON 0x80 //LCD backlight
85 #define GPS_ON 0x100 //GPS
87 #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
94 MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
95 MODULE_DESCRIPTION(ASUS_HOTK_NAME);
96 MODULE_LICENSE("GPL");
98 /* 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 =)
107 static uint wapf = 1;
108 module_param(wapf, uint, 0644);
109 MODULE_PARM_DESC(wapf, "WAPF value");
111 #define ASUS_HANDLE(object, paths...) \
112 static acpi_handle object##_handle = NULL; \
113 static char *object##_paths[] = { paths }
115 /* LED */
116 ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
117 ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
118 ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
119 ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
120 ASUS_HANDLE(gled_set, ASUS_HOTK_PREFIX "GLED"); /* G1, G2 (probably) */
122 /* LEDD */
123 ASUS_HANDLE(ledd_set, ASUS_HOTK_PREFIX "SLCM");
125 /* 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.
129 ASUS_HANDLE(wl_switch, ASUS_HOTK_PREFIX "WLED");
130 ASUS_HANDLE(bt_switch, ASUS_HOTK_PREFIX "BLED");
131 ASUS_HANDLE(wireless_status, ASUS_HOTK_PREFIX "RSTS"); /* All new models */
133 /* Brightness */
134 ASUS_HANDLE(brightness_set, ASUS_HOTK_PREFIX "SPLV");
135 ASUS_HANDLE(brightness_get, ASUS_HOTK_PREFIX "GPLV");
137 /* Backlight */
138 ASUS_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.LPCB.EC0._Q0E", /* P30/P35 */
144 "\\_SB.PCI0.PX40.Q10", /* S1x */
145 "\\Q10"); /* A2x, L2D, L3D, M2E */
147 /* Display */
148 ASUS_HANDLE(display_set, ASUS_HOTK_PREFIX "SDSP");
149 ASUS_HANDLE(display_get, "\\_SB.PCI0.P0P1.VGA.GETD", /* A6B, A6K A6R A7D F3JM L4R M6R A3G
150 M6A M6V VX-1 V6J V6V W3Z */
151 "\\_SB.PCI0.P0P2.VGA.GETD", /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V
152 S5A M5A z33A W1Jc W2V G1 */
153 "\\_SB.PCI0.P0P3.VGA.GETD", /* A6V A6Q */
154 "\\_SB.PCI0.P0PA.VGA.GETD", /* A6T, A6M */
155 "\\_SB.PCI0.PCI1.VGAC.NMAP", /* L3C */
156 "\\_SB.PCI0.VGA.GETD", /* Z96F */
157 "\\ACTD", /* A2D */
158 "\\ADVG", /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
159 "\\DNXT", /* P30 */
160 "\\INFB", /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
161 "\\SSTE"); /* A3F A6F A3N A3L M6N W3N W6A */
163 ASUS_HANDLE(ls_switch, ASUS_HOTK_PREFIX "ALSC"); /* Z71A Z71V */
164 ASUS_HANDLE(ls_level, ASUS_HOTK_PREFIX "ALSL"); /* Z71A Z71V */
166 /* GPS */
167 /* R2H use different handle for GPS on/off */
168 ASUS_HANDLE(gps_on, ASUS_HOTK_PREFIX "SDON"); /* R2H */
169 ASUS_HANDLE(gps_off, ASUS_HOTK_PREFIX "SDOF"); /* R2H */
170 ASUS_HANDLE(gps_status, ASUS_HOTK_PREFIX "GPST");
173 * This is the main structure, we can use it to store anything interesting
174 * about the hotk device
176 struct asus_hotk {
177 char *name; //laptop name
178 struct acpi_device *device; //the device we are in
179 acpi_handle handle; //the handle of the hotk device
180 char status; //status of the hotk, for LEDs, ...
181 u32 ledd_status; //status of the LED display
182 u8 light_level; //light sensor level
183 u8 light_switch; //light sensor switch value
184 u16 event_count[128]; //count for each event TODO make this better
185 struct input_dev *inputdev;
186 u16 *keycode_map;
190 * This header is made available to allow proper configuration given model,
191 * revision number , ... this info cannot go in struct asus_hotk because it is
192 * available before the hotk
194 static struct acpi_table_header *asus_info;
196 /* The actual device the driver binds to */
197 static struct asus_hotk *hotk;
200 * The hotkey driver declaration
202 static const struct acpi_device_id asus_device_ids[] = {
203 {"ATK0100", 0},
204 {"", 0},
206 MODULE_DEVICE_TABLE(acpi, asus_device_ids);
208 static int asus_hotk_add(struct acpi_device *device);
209 static int asus_hotk_remove(struct acpi_device *device, int type);
210 static void asus_hotk_notify(struct acpi_device *device, u32 event);
212 static struct acpi_driver asus_hotk_driver = {
213 .name = ASUS_HOTK_NAME,
214 .class = ASUS_HOTK_CLASS,
215 .ids = asus_device_ids,
216 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
217 .ops = {
218 .add = asus_hotk_add,
219 .remove = asus_hotk_remove,
220 .notify = asus_hotk_notify,
224 /* The backlight device /sys/class/backlight */
225 static struct backlight_device *asus_backlight_device;
228 * The backlight class declaration
230 static int read_brightness(struct backlight_device *bd);
231 static int update_bl_status(struct backlight_device *bd);
232 static struct backlight_ops asusbl_ops = {
233 .get_brightness = read_brightness,
234 .update_status = update_bl_status,
237 /* These functions actually update the LED's, and are called from a
238 * workqueue. By doing this as separate work rather than when the LED
239 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
240 * potentially bad time, such as a timer interrupt. */
241 static struct workqueue_struct *led_workqueue;
243 #define ASUS_LED(object, ledname) \
244 static void object##_led_set(struct led_classdev *led_cdev, \
245 enum led_brightness value); \
246 static void object##_led_update(struct work_struct *ignored); \
247 static int object##_led_wk; \
248 static DECLARE_WORK(object##_led_work, object##_led_update); \
249 static struct led_classdev object##_led = { \
250 .name = "asus::" ledname, \
251 .brightness_set = object##_led_set, \
254 ASUS_LED(mled, "mail");
255 ASUS_LED(tled, "touchpad");
256 ASUS_LED(rled, "record");
257 ASUS_LED(pled, "phone");
258 ASUS_LED(gled, "gaming");
260 struct key_entry {
261 char type;
262 u8 code;
263 u16 keycode;
266 enum { KE_KEY, KE_END };
268 static struct key_entry asus_keymap[] = {
269 {KE_KEY, 0x30, KEY_VOLUMEUP},
270 {KE_KEY, 0x31, KEY_VOLUMEDOWN},
271 {KE_KEY, 0x32, KEY_MUTE},
272 {KE_KEY, 0x33, KEY_SWITCHVIDEOMODE},
273 {KE_KEY, 0x34, KEY_SWITCHVIDEOMODE},
274 {KE_KEY, 0x40, KEY_PREVIOUSSONG},
275 {KE_KEY, 0x41, KEY_NEXTSONG},
276 {KE_KEY, 0x43, KEY_STOPCD},
277 {KE_KEY, 0x45, KEY_PLAYPAUSE},
278 {KE_KEY, 0x50, KEY_EMAIL},
279 {KE_KEY, 0x51, KEY_WWW},
280 {KE_KEY, 0x5C, KEY_SCREENLOCK}, /* Screenlock */
281 {KE_KEY, 0x5D, KEY_WLAN},
282 {KE_KEY, 0x61, KEY_SWITCHVIDEOMODE},
283 {KE_KEY, 0x6B, BTN_TOUCH}, /* Lock Mouse */
284 {KE_KEY, 0x82, KEY_CAMERA},
285 {KE_KEY, 0x8A, KEY_PROG1},
286 {KE_KEY, 0x95, KEY_MEDIA},
287 {KE_KEY, 0x99, KEY_PHONE},
288 {KE_END, 0},
292 * This function evaluates an ACPI method, given an int as parameter, the
293 * method is searched within the scope of the handle, can be NULL. The output
294 * of the method is written is output, which can also be NULL
296 * returns 0 if write is successful, -1 else.
298 static int write_acpi_int(acpi_handle handle, const char *method, int val,
299 struct acpi_buffer *output)
301 struct acpi_object_list params; //list of input parameters (an int here)
302 union acpi_object in_obj; //the only param we use
303 acpi_status status;
305 if (!handle)
306 return 0;
308 params.count = 1;
309 params.pointer = &in_obj;
310 in_obj.type = ACPI_TYPE_INTEGER;
311 in_obj.integer.value = val;
313 status = acpi_evaluate_object(handle, (char *)method, &params, output);
314 if (status == AE_OK)
315 return 0;
316 else
317 return -1;
320 static int read_wireless_status(int mask)
322 unsigned long long status;
323 acpi_status rv = AE_OK;
325 if (!wireless_status_handle)
326 return (hotk->status & mask) ? 1 : 0;
328 rv = acpi_evaluate_integer(wireless_status_handle, NULL, NULL, &status);
329 if (ACPI_FAILURE(rv))
330 printk(ASUS_WARNING "Error reading Wireless status\n");
331 else
332 return (status & mask) ? 1 : 0;
334 return (hotk->status & mask) ? 1 : 0;
337 static int read_gps_status(void)
339 unsigned long long status;
340 acpi_status rv = AE_OK;
342 rv = acpi_evaluate_integer(gps_status_handle, NULL, NULL, &status);
343 if (ACPI_FAILURE(rv))
344 printk(ASUS_WARNING "Error reading GPS status\n");
345 else
346 return status ? 1 : 0;
348 return (hotk->status & GPS_ON) ? 1 : 0;
351 /* Generic LED functions */
352 static int read_status(int mask)
354 /* There is a special method for both wireless devices */
355 if (mask == BT_ON || mask == WL_ON)
356 return read_wireless_status(mask);
357 else if (mask == GPS_ON)
358 return read_gps_status();
360 return (hotk->status & mask) ? 1 : 0;
363 static void write_status(acpi_handle handle, int out, int mask)
365 hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
367 switch (mask) {
368 case MLED_ON:
369 out = !(out & 0x1);
370 break;
371 case GLED_ON:
372 out = (out & 0x1) + 1;
373 break;
374 case GPS_ON:
375 handle = (out) ? gps_on_handle : gps_off_handle;
376 out = 0x02;
377 break;
378 default:
379 out &= 0x1;
380 break;
383 if (write_acpi_int(handle, NULL, out, NULL))
384 printk(ASUS_WARNING " write failed %x\n", mask);
387 /* /sys/class/led handlers */
388 #define ASUS_LED_HANDLER(object, mask) \
389 static void object##_led_set(struct led_classdev *led_cdev, \
390 enum led_brightness value) \
392 object##_led_wk = (value > 0) ? 1 : 0; \
393 queue_work(led_workqueue, &object##_led_work); \
395 static void object##_led_update(struct work_struct *ignored) \
397 int value = object##_led_wk; \
398 write_status(object##_set_handle, value, (mask)); \
401 ASUS_LED_HANDLER(mled, MLED_ON);
402 ASUS_LED_HANDLER(pled, PLED_ON);
403 ASUS_LED_HANDLER(rled, RLED_ON);
404 ASUS_LED_HANDLER(tled, TLED_ON);
405 ASUS_LED_HANDLER(gled, GLED_ON);
407 static int get_lcd_state(void)
409 return read_status(LCD_ON);
412 static int set_lcd_state(int value)
414 int lcd = 0;
415 acpi_status status = 0;
417 lcd = value ? 1 : 0;
419 if (lcd == get_lcd_state())
420 return 0;
422 if (lcd_switch_handle) {
423 status = acpi_evaluate_object(lcd_switch_handle,
424 NULL, NULL, NULL);
426 if (ACPI_FAILURE(status))
427 printk(ASUS_WARNING "Error switching LCD\n");
430 write_status(NULL, lcd, LCD_ON);
431 return 0;
434 static void lcd_blank(int blank)
436 struct backlight_device *bd = asus_backlight_device;
438 if (bd) {
439 bd->props.power = blank;
440 backlight_update_status(bd);
444 static int read_brightness(struct backlight_device *bd)
446 unsigned long long value;
447 acpi_status rv = AE_OK;
449 rv = acpi_evaluate_integer(brightness_get_handle, NULL, NULL, &value);
450 if (ACPI_FAILURE(rv))
451 printk(ASUS_WARNING "Error reading brightness\n");
453 return value;
456 static int set_brightness(struct backlight_device *bd, int value)
458 int ret = 0;
460 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
461 /* 0 <= value <= 15 */
463 if (write_acpi_int(brightness_set_handle, NULL, value, NULL)) {
464 printk(ASUS_WARNING "Error changing brightness\n");
465 ret = -EIO;
468 return ret;
471 static int update_bl_status(struct backlight_device *bd)
473 int rv;
474 int value = bd->props.brightness;
476 rv = set_brightness(bd, value);
477 if (rv)
478 return rv;
480 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
481 return set_lcd_state(value);
485 * Platform device handlers
489 * We write our info in page, we begin at offset off and cannot write more
490 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
491 * number of bytes written in page
493 static ssize_t show_infos(struct device *dev,
494 struct device_attribute *attr, char *page)
496 int len = 0;
497 unsigned long long temp;
498 char buf[16]; //enough for all info
499 acpi_status rv = AE_OK;
502 * We use the easy way, we don't care of off and count, so we don't set eof
503 * to 1
506 len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
507 len += sprintf(page + len, "Model reference : %s\n", hotk->name);
509 * The SFUN method probably allows the original driver to get the list
510 * of features supported by a given model. For now, 0x0100 or 0x0800
511 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
512 * The significance of others is yet to be found.
514 rv = acpi_evaluate_integer(hotk->handle, "SFUN", NULL, &temp);
515 if (!ACPI_FAILURE(rv))
516 len += sprintf(page + len, "SFUN value : 0x%04x\n",
517 (uint) temp);
519 * Another value for userspace: the ASYM method returns 0x02 for
520 * battery low and 0x04 for battery critical, its readings tend to be
521 * more accurate than those provided by _BST.
522 * Note: since not all the laptops provide this method, errors are
523 * silently ignored.
525 rv = acpi_evaluate_integer(hotk->handle, "ASYM", NULL, &temp);
526 if (!ACPI_FAILURE(rv))
527 len += sprintf(page + len, "ASYM value : 0x%04x\n",
528 (uint) temp);
529 if (asus_info) {
530 snprintf(buf, 16, "%d", asus_info->length);
531 len += sprintf(page + len, "DSDT length : %s\n", buf);
532 snprintf(buf, 16, "%d", asus_info->checksum);
533 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
534 snprintf(buf, 16, "%d", asus_info->revision);
535 len += sprintf(page + len, "DSDT revision : %s\n", buf);
536 snprintf(buf, 7, "%s", asus_info->oem_id);
537 len += sprintf(page + len, "OEM id : %s\n", buf);
538 snprintf(buf, 9, "%s", asus_info->oem_table_id);
539 len += sprintf(page + len, "OEM table id : %s\n", buf);
540 snprintf(buf, 16, "%x", asus_info->oem_revision);
541 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
542 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
543 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
544 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
545 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
548 return len;
551 static int parse_arg(const char *buf, unsigned long count, int *val)
553 if (!count)
554 return 0;
555 if (count > 31)
556 return -EINVAL;
557 if (sscanf(buf, "%i", val) != 1)
558 return -EINVAL;
559 return count;
562 static ssize_t store_status(const char *buf, size_t count,
563 acpi_handle handle, int mask)
565 int rv, value;
566 int out = 0;
568 rv = parse_arg(buf, count, &value);
569 if (rv > 0)
570 out = value ? 1 : 0;
572 write_status(handle, out, mask);
574 return rv;
578 * LEDD display
580 static ssize_t show_ledd(struct device *dev,
581 struct device_attribute *attr, char *buf)
583 return sprintf(buf, "0x%08x\n", hotk->ledd_status);
586 static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
587 const char *buf, size_t count)
589 int rv, value;
591 rv = parse_arg(buf, count, &value);
592 if (rv > 0) {
593 if (write_acpi_int(ledd_set_handle, NULL, value, NULL))
594 printk(ASUS_WARNING "LED display write failed\n");
595 else
596 hotk->ledd_status = (u32) value;
598 return rv;
602 * WLAN
604 static ssize_t show_wlan(struct device *dev,
605 struct device_attribute *attr, char *buf)
607 return sprintf(buf, "%d\n", read_status(WL_ON));
610 static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
611 const char *buf, size_t count)
613 return store_status(buf, count, wl_switch_handle, WL_ON);
617 * Bluetooth
619 static ssize_t show_bluetooth(struct device *dev,
620 struct device_attribute *attr, char *buf)
622 return sprintf(buf, "%d\n", read_status(BT_ON));
625 static ssize_t store_bluetooth(struct device *dev,
626 struct device_attribute *attr, const char *buf,
627 size_t count)
629 return store_status(buf, count, bt_switch_handle, BT_ON);
633 * Display
635 static void set_display(int value)
637 /* no sanity check needed for now */
638 if (write_acpi_int(display_set_handle, NULL, value, NULL))
639 printk(ASUS_WARNING "Error setting display\n");
640 return;
643 static int read_display(void)
645 unsigned long long value = 0;
646 acpi_status rv = AE_OK;
648 /* In most of the case, we know how to set the display, but sometime
649 we can't read it */
650 if (display_get_handle) {
651 rv = acpi_evaluate_integer(display_get_handle, NULL,
652 NULL, &value);
653 if (ACPI_FAILURE(rv))
654 printk(ASUS_WARNING "Error reading display status\n");
657 value &= 0x0F; /* needed for some models, shouldn't hurt others */
659 return value;
663 * Now, *this* one could be more user-friendly, but so far, no-one has
664 * complained. The significance of bits is the same as in store_disp()
666 static ssize_t show_disp(struct device *dev,
667 struct device_attribute *attr, char *buf)
669 return sprintf(buf, "%d\n", read_display());
673 * Experimental support for display switching. As of now: 1 should activate
674 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
675 * Any combination (bitwise) of these will suffice. I never actually tested 4
676 * displays hooked up simultaneously, so be warned. See the acpi4asus README
677 * for more info.
679 static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
680 const char *buf, size_t count)
682 int rv, value;
684 rv = parse_arg(buf, count, &value);
685 if (rv > 0)
686 set_display(value);
687 return rv;
691 * Light Sens
693 static void set_light_sens_switch(int value)
695 if (write_acpi_int(ls_switch_handle, NULL, value, NULL))
696 printk(ASUS_WARNING "Error setting light sensor switch\n");
697 hotk->light_switch = value;
700 static ssize_t show_lssw(struct device *dev,
701 struct device_attribute *attr, char *buf)
703 return sprintf(buf, "%d\n", hotk->light_switch);
706 static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
707 const char *buf, size_t count)
709 int rv, value;
711 rv = parse_arg(buf, count, &value);
712 if (rv > 0)
713 set_light_sens_switch(value ? 1 : 0);
715 return rv;
718 static void set_light_sens_level(int value)
720 if (write_acpi_int(ls_level_handle, NULL, value, NULL))
721 printk(ASUS_WARNING "Error setting light sensor level\n");
722 hotk->light_level = value;
725 static ssize_t show_lslvl(struct device *dev,
726 struct device_attribute *attr, char *buf)
728 return sprintf(buf, "%d\n", hotk->light_level);
731 static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
732 const char *buf, size_t count)
734 int rv, value;
736 rv = parse_arg(buf, count, &value);
737 if (rv > 0) {
738 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
739 /* 0 <= value <= 15 */
740 set_light_sens_level(value);
743 return rv;
747 * GPS
749 static ssize_t show_gps(struct device *dev,
750 struct device_attribute *attr, char *buf)
752 return sprintf(buf, "%d\n", read_status(GPS_ON));
755 static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
756 const char *buf, size_t count)
758 return store_status(buf, count, NULL, GPS_ON);
762 * Hotkey functions
764 static struct key_entry *asus_get_entry_by_scancode(int code)
766 struct key_entry *key;
768 for (key = asus_keymap; key->type != KE_END; key++)
769 if (code == key->code)
770 return key;
772 return NULL;
775 static struct key_entry *asus_get_entry_by_keycode(int code)
777 struct key_entry *key;
779 for (key = asus_keymap; key->type != KE_END; key++)
780 if (code == key->keycode && key->type == KE_KEY)
781 return key;
783 return NULL;
786 static int asus_getkeycode(struct input_dev *dev, int scancode, int *keycode)
788 struct key_entry *key = asus_get_entry_by_scancode(scancode);
790 if (key && key->type == KE_KEY) {
791 *keycode = key->keycode;
792 return 0;
795 return -EINVAL;
798 static int asus_setkeycode(struct input_dev *dev, int scancode, int keycode)
800 struct key_entry *key;
801 int old_keycode;
803 if (keycode < 0 || keycode > KEY_MAX)
804 return -EINVAL;
806 key = asus_get_entry_by_scancode(scancode);
807 if (key && key->type == KE_KEY) {
808 old_keycode = key->keycode;
809 key->keycode = keycode;
810 set_bit(keycode, dev->keybit);
811 if (!asus_get_entry_by_keycode(old_keycode))
812 clear_bit(old_keycode, dev->keybit);
813 return 0;
816 return -EINVAL;
819 static void asus_hotk_notify(struct acpi_device *device, u32 event)
821 static struct key_entry *key;
822 u16 count;
824 /* TODO Find a better way to handle events count. */
825 if (!hotk)
826 return;
829 * We need to tell the backlight device when the backlight power is
830 * switched
832 if (event == ATKD_LCD_ON) {
833 write_status(NULL, 1, LCD_ON);
834 lcd_blank(FB_BLANK_UNBLANK);
835 } else if (event == ATKD_LCD_OFF) {
836 write_status(NULL, 0, LCD_ON);
837 lcd_blank(FB_BLANK_POWERDOWN);
840 count = hotk->event_count[event % 128]++;
841 acpi_bus_generate_proc_event(hotk->device, event, count);
842 acpi_bus_generate_netlink_event(hotk->device->pnp.device_class,
843 dev_name(&hotk->device->dev), event,
844 count);
846 if (hotk->inputdev) {
847 key = asus_get_entry_by_scancode(event);
848 if (!key)
849 return ;
851 switch (key->type) {
852 case KE_KEY:
853 input_report_key(hotk->inputdev, key->keycode, 1);
854 input_sync(hotk->inputdev);
855 input_report_key(hotk->inputdev, key->keycode, 0);
856 input_sync(hotk->inputdev);
857 break;
862 #define ASUS_CREATE_DEVICE_ATTR(_name) \
863 struct device_attribute dev_attr_##_name = { \
864 .attr = { \
865 .name = __stringify(_name), \
866 .mode = 0 }, \
867 .show = NULL, \
868 .store = NULL, \
871 #define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
872 do { \
873 dev_attr_##_name.attr.mode = _mode; \
874 dev_attr_##_name.show = _show; \
875 dev_attr_##_name.store = _store; \
876 } while(0)
878 static ASUS_CREATE_DEVICE_ATTR(infos);
879 static ASUS_CREATE_DEVICE_ATTR(wlan);
880 static ASUS_CREATE_DEVICE_ATTR(bluetooth);
881 static ASUS_CREATE_DEVICE_ATTR(display);
882 static ASUS_CREATE_DEVICE_ATTR(ledd);
883 static ASUS_CREATE_DEVICE_ATTR(ls_switch);
884 static ASUS_CREATE_DEVICE_ATTR(ls_level);
885 static ASUS_CREATE_DEVICE_ATTR(gps);
887 static struct attribute *asuspf_attributes[] = {
888 &dev_attr_infos.attr,
889 &dev_attr_wlan.attr,
890 &dev_attr_bluetooth.attr,
891 &dev_attr_display.attr,
892 &dev_attr_ledd.attr,
893 &dev_attr_ls_switch.attr,
894 &dev_attr_ls_level.attr,
895 &dev_attr_gps.attr,
896 NULL
899 static struct attribute_group asuspf_attribute_group = {
900 .attrs = asuspf_attributes
903 static struct platform_driver asuspf_driver = {
904 .driver = {
905 .name = ASUS_HOTK_FILE,
906 .owner = THIS_MODULE,
910 static struct platform_device *asuspf_device;
912 static void asus_hotk_add_fs(void)
914 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
916 if (wl_switch_handle)
917 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
919 if (bt_switch_handle)
920 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
921 show_bluetooth, store_bluetooth);
923 if (display_set_handle && display_get_handle)
924 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
925 else if (display_set_handle)
926 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
928 if (ledd_set_handle)
929 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
931 if (ls_switch_handle && ls_level_handle) {
932 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
933 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
936 if (gps_status_handle && gps_on_handle && gps_off_handle)
937 ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
940 static int asus_handle_init(char *name, acpi_handle * handle,
941 char **paths, int num_paths)
943 int i;
944 acpi_status status;
946 for (i = 0; i < num_paths; i++) {
947 status = acpi_get_handle(NULL, paths[i], handle);
948 if (ACPI_SUCCESS(status))
949 return 0;
952 *handle = NULL;
953 return -ENODEV;
956 #define ASUS_HANDLE_INIT(object) \
957 asus_handle_init(#object, &object##_handle, object##_paths, \
958 ARRAY_SIZE(object##_paths))
961 * This function is used to initialize the hotk with right values. In this
962 * method, we can make all the detection we want, and modify the hotk struct
964 static int asus_hotk_get_info(void)
966 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
967 union acpi_object *model = NULL;
968 unsigned long long bsts_result, hwrs_result;
969 char *string = NULL;
970 acpi_status status;
973 * Get DSDT headers early enough to allow for differentiating between
974 * models, but late enough to allow acpi_bus_register_driver() to fail
975 * before doing anything ACPI-specific. Should we encounter a machine,
976 * which needs special handling (i.e. its hotkey device has a different
977 * HID), this bit will be moved. A global variable asus_info contains
978 * the DSDT header.
980 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
981 if (ACPI_FAILURE(status))
982 printk(ASUS_WARNING "Couldn't get the DSDT table header\n");
984 /* We have to write 0 on init this far for all ASUS models */
985 if (write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
986 printk(ASUS_ERR "Hotkey initialization failed\n");
987 return -ENODEV;
990 /* This needs to be called for some laptops to init properly */
991 status =
992 acpi_evaluate_integer(hotk->handle, "BSTS", NULL, &bsts_result);
993 if (ACPI_FAILURE(status))
994 printk(ASUS_WARNING "Error calling BSTS\n");
995 else if (bsts_result)
996 printk(ASUS_NOTICE "BSTS called, 0x%02x returned\n",
997 (uint) bsts_result);
999 /* This too ... */
1000 write_acpi_int(hotk->handle, "CWAP", wapf, NULL);
1003 * Try to match the object returned by INIT to the specific model.
1004 * Handle every possible object (or the lack of thereof) the DSDT
1005 * writers might throw at us. When in trouble, we pass NULL to
1006 * asus_model_match() and try something completely different.
1008 if (buffer.pointer) {
1009 model = buffer.pointer;
1010 switch (model->type) {
1011 case ACPI_TYPE_STRING:
1012 string = model->string.pointer;
1013 break;
1014 case ACPI_TYPE_BUFFER:
1015 string = model->buffer.pointer;
1016 break;
1017 default:
1018 string = "";
1019 break;
1022 hotk->name = kstrdup(string, GFP_KERNEL);
1023 if (!hotk->name)
1024 return -ENOMEM;
1026 if (*string)
1027 printk(ASUS_NOTICE " %s model detected\n", string);
1029 ASUS_HANDLE_INIT(mled_set);
1030 ASUS_HANDLE_INIT(tled_set);
1031 ASUS_HANDLE_INIT(rled_set);
1032 ASUS_HANDLE_INIT(pled_set);
1033 ASUS_HANDLE_INIT(gled_set);
1035 ASUS_HANDLE_INIT(ledd_set);
1038 * The HWRS method return informations about the hardware.
1039 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1040 * The significance of others is yet to be found.
1041 * If we don't find the method, we assume the device are present.
1043 status =
1044 acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &hwrs_result);
1045 if (ACPI_FAILURE(status))
1046 hwrs_result = WL_HWRS | BT_HWRS;
1048 if (hwrs_result & WL_HWRS)
1049 ASUS_HANDLE_INIT(wl_switch);
1050 if (hwrs_result & BT_HWRS)
1051 ASUS_HANDLE_INIT(bt_switch);
1053 ASUS_HANDLE_INIT(wireless_status);
1055 ASUS_HANDLE_INIT(brightness_set);
1056 ASUS_HANDLE_INIT(brightness_get);
1058 ASUS_HANDLE_INIT(lcd_switch);
1060 ASUS_HANDLE_INIT(display_set);
1061 ASUS_HANDLE_INIT(display_get);
1063 /* There is a lot of models with "ALSL", but a few get
1064 a real light sens, so we need to check it. */
1065 if (!ASUS_HANDLE_INIT(ls_switch))
1066 ASUS_HANDLE_INIT(ls_level);
1068 ASUS_HANDLE_INIT(gps_on);
1069 ASUS_HANDLE_INIT(gps_off);
1070 ASUS_HANDLE_INIT(gps_status);
1072 kfree(model);
1074 return AE_OK;
1077 static int asus_input_init(void)
1079 const struct key_entry *key;
1080 int result;
1082 hotk->inputdev = input_allocate_device();
1083 if (!hotk->inputdev) {
1084 printk(ASUS_INFO "Unable to allocate input device\n");
1085 return 0;
1087 hotk->inputdev->name = "Asus Laptop extra buttons";
1088 hotk->inputdev->phys = ASUS_HOTK_FILE "/input0";
1089 hotk->inputdev->id.bustype = BUS_HOST;
1090 hotk->inputdev->getkeycode = asus_getkeycode;
1091 hotk->inputdev->setkeycode = asus_setkeycode;
1093 for (key = asus_keymap; key->type != KE_END; key++) {
1094 switch (key->type) {
1095 case KE_KEY:
1096 set_bit(EV_KEY, hotk->inputdev->evbit);
1097 set_bit(key->keycode, hotk->inputdev->keybit);
1098 break;
1101 result = input_register_device(hotk->inputdev);
1102 if (result) {
1103 printk(ASUS_INFO "Unable to register input device\n");
1104 input_free_device(hotk->inputdev);
1106 return result;
1109 static int asus_hotk_check(void)
1111 int result = 0;
1113 result = acpi_bus_get_status(hotk->device);
1114 if (result)
1115 return result;
1117 if (hotk->device->status.present) {
1118 result = asus_hotk_get_info();
1119 } else {
1120 printk(ASUS_ERR "Hotkey device not present, aborting\n");
1121 return -EINVAL;
1124 return result;
1127 static int asus_hotk_found;
1129 static int asus_hotk_add(struct acpi_device *device)
1131 int result;
1133 if (!device)
1134 return -EINVAL;
1136 printk(ASUS_NOTICE "Asus Laptop Support version %s\n",
1137 ASUS_LAPTOP_VERSION);
1139 hotk = kzalloc(sizeof(struct asus_hotk), GFP_KERNEL);
1140 if (!hotk)
1141 return -ENOMEM;
1143 hotk->handle = device->handle;
1144 strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
1145 strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
1146 device->driver_data = hotk;
1147 hotk->device = device;
1149 result = asus_hotk_check();
1150 if (result)
1151 goto end;
1153 asus_hotk_add_fs();
1155 asus_hotk_found = 1;
1157 /* WLED and BLED are on by default */
1158 write_status(bt_switch_handle, 1, BT_ON);
1159 write_status(wl_switch_handle, 1, WL_ON);
1161 /* If the h/w switch is off, we need to check the real status */
1162 write_status(NULL, read_status(BT_ON), BT_ON);
1163 write_status(NULL, read_status(WL_ON), WL_ON);
1165 /* LCD Backlight is on by default */
1166 write_status(NULL, 1, LCD_ON);
1168 /* LED display is off by default */
1169 hotk->ledd_status = 0xFFF;
1171 /* Set initial values of light sensor and level */
1172 hotk->light_switch = 1; /* Default to light sensor disabled */
1173 hotk->light_level = 0; /* level 5 for sensor sensitivity */
1175 if (ls_switch_handle)
1176 set_light_sens_switch(hotk->light_switch);
1178 if (ls_level_handle)
1179 set_light_sens_level(hotk->light_level);
1181 /* GPS is on by default */
1182 write_status(NULL, 1, GPS_ON);
1184 end:
1185 if (result) {
1186 kfree(hotk->name);
1187 kfree(hotk);
1190 return result;
1193 static int asus_hotk_remove(struct acpi_device *device, int type)
1195 if (!device || !acpi_driver_data(device))
1196 return -EINVAL;
1198 kfree(hotk->name);
1199 kfree(hotk);
1201 return 0;
1204 static void asus_backlight_exit(void)
1206 if (asus_backlight_device)
1207 backlight_device_unregister(asus_backlight_device);
1210 #define ASUS_LED_UNREGISTER(object) \
1211 if (object##_led.dev) \
1212 led_classdev_unregister(&object##_led)
1214 static void asus_led_exit(void)
1216 destroy_workqueue(led_workqueue);
1217 ASUS_LED_UNREGISTER(mled);
1218 ASUS_LED_UNREGISTER(tled);
1219 ASUS_LED_UNREGISTER(pled);
1220 ASUS_LED_UNREGISTER(rled);
1221 ASUS_LED_UNREGISTER(gled);
1224 static void asus_input_exit(void)
1226 if (hotk->inputdev)
1227 input_unregister_device(hotk->inputdev);
1230 static void __exit asus_laptop_exit(void)
1232 asus_backlight_exit();
1233 asus_led_exit();
1234 asus_input_exit();
1236 acpi_bus_unregister_driver(&asus_hotk_driver);
1237 sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
1238 platform_device_unregister(asuspf_device);
1239 platform_driver_unregister(&asuspf_driver);
1242 static int asus_backlight_init(struct device *dev)
1244 struct backlight_device *bd;
1246 if (brightness_set_handle && lcd_switch_handle) {
1247 bd = backlight_device_register(ASUS_HOTK_FILE, dev,
1248 NULL, &asusbl_ops);
1249 if (IS_ERR(bd)) {
1250 printk(ASUS_ERR
1251 "Could not register asus backlight device\n");
1252 asus_backlight_device = NULL;
1253 return PTR_ERR(bd);
1256 asus_backlight_device = bd;
1258 bd->props.max_brightness = 15;
1259 bd->props.brightness = read_brightness(NULL);
1260 bd->props.power = FB_BLANK_UNBLANK;
1261 backlight_update_status(bd);
1263 return 0;
1266 static int asus_led_register(acpi_handle handle,
1267 struct led_classdev *ldev, struct device *dev)
1269 if (!handle)
1270 return 0;
1272 return led_classdev_register(dev, ldev);
1275 #define ASUS_LED_REGISTER(object, device) \
1276 asus_led_register(object##_set_handle, &object##_led, device)
1278 static int asus_led_init(struct device *dev)
1280 int rv;
1282 rv = ASUS_LED_REGISTER(mled, dev);
1283 if (rv)
1284 goto out;
1286 rv = ASUS_LED_REGISTER(tled, dev);
1287 if (rv)
1288 goto out1;
1290 rv = ASUS_LED_REGISTER(rled, dev);
1291 if (rv)
1292 goto out2;
1294 rv = ASUS_LED_REGISTER(pled, dev);
1295 if (rv)
1296 goto out3;
1298 rv = ASUS_LED_REGISTER(gled, dev);
1299 if (rv)
1300 goto out4;
1302 led_workqueue = create_singlethread_workqueue("led_workqueue");
1303 if (!led_workqueue)
1304 goto out5;
1306 return 0;
1307 out5:
1308 rv = -ENOMEM;
1309 ASUS_LED_UNREGISTER(gled);
1310 out4:
1311 ASUS_LED_UNREGISTER(pled);
1312 out3:
1313 ASUS_LED_UNREGISTER(rled);
1314 out2:
1315 ASUS_LED_UNREGISTER(tled);
1316 out1:
1317 ASUS_LED_UNREGISTER(mled);
1318 out:
1319 return rv;
1322 static int __init asus_laptop_init(void)
1324 int result;
1326 if (acpi_disabled)
1327 return -ENODEV;
1329 result = acpi_bus_register_driver(&asus_hotk_driver);
1330 if (result < 0)
1331 return result;
1334 * This is a bit of a kludge. We only want this module loaded
1335 * for ASUS systems, but there's currently no way to probe the
1336 * ACPI namespace for ASUS HIDs. So we just return failure if
1337 * we didn't find one, which will cause the module to be
1338 * unloaded.
1340 if (!asus_hotk_found) {
1341 acpi_bus_unregister_driver(&asus_hotk_driver);
1342 return -ENODEV;
1345 result = asus_input_init();
1346 if (result)
1347 goto fail_input;
1349 /* Register platform stuff */
1350 result = platform_driver_register(&asuspf_driver);
1351 if (result)
1352 goto fail_platform_driver;
1354 asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
1355 if (!asuspf_device) {
1356 result = -ENOMEM;
1357 goto fail_platform_device1;
1360 result = platform_device_add(asuspf_device);
1361 if (result)
1362 goto fail_platform_device2;
1364 result = sysfs_create_group(&asuspf_device->dev.kobj,
1365 &asuspf_attribute_group);
1366 if (result)
1367 goto fail_sysfs;
1369 result = asus_led_init(&asuspf_device->dev);
1370 if (result)
1371 goto fail_led;
1373 if (!acpi_video_backlight_support()) {
1374 result = asus_backlight_init(&asuspf_device->dev);
1375 if (result)
1376 goto fail_backlight;
1377 } else
1378 printk(ASUS_INFO "Brightness ignored, must be controlled by "
1379 "ACPI video driver\n");
1381 return 0;
1383 fail_backlight:
1384 asus_led_exit();
1386 fail_led:
1387 sysfs_remove_group(&asuspf_device->dev.kobj,
1388 &asuspf_attribute_group);
1390 fail_sysfs:
1391 platform_device_del(asuspf_device);
1393 fail_platform_device2:
1394 platform_device_put(asuspf_device);
1396 fail_platform_device1:
1397 platform_driver_unregister(&asuspf_driver);
1399 fail_platform_driver:
1400 asus_input_exit();
1402 fail_input:
1404 return result;
1407 module_init(asus_laptop_init);
1408 module_exit(asus_laptop_exit);