Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/agpgart
[sfrench/cifs-2.6.git] / drivers / misc / asus-laptop.c
1 /*
2  *  asus-laptop.c - Asus Laptop Support
3  *
4  *
5  *  Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
6  *  Copyright (C) 2006-2007 Corentin Chary
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  *
23  *  The development page for this driver is located at
24  *  http://sourceforge.net/projects/acpi4asus/
25  *
26  *  Credits:
27  *  Pontus Fuchs   - Helper functions, cleanup
28  *  Johann Wiesner - Small compile fixes
29  *  John Belmonte  - ACPI code for Toshiba laptop was a good starting point.
30  *  Eric Burghard  - LED display support for W1N
31  *  Josh Green     - Light Sens support
32  *  Thomas Tuttle  - His first patch for led support was very helpfull
33  *
34  */
35
36 #include <linux/autoconf.h>
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/types.h>
41 #include <linux/err.h>
42 #include <linux/proc_fs.h>
43 #include <linux/backlight.h>
44 #include <linux/fb.h>
45 #include <linux/leds.h>
46 #include <linux/platform_device.h>
47 #include <acpi/acpi_drivers.h>
48 #include <acpi/acpi_bus.h>
49 #include <asm/uaccess.h>
50
51 #define ASUS_LAPTOP_VERSION "0.41"
52
53 #define ASUS_HOTK_NAME          "Asus Laptop Support"
54 #define ASUS_HOTK_CLASS         "hotkey"
55 #define ASUS_HOTK_DEVICE_NAME   "Hotkey"
56 #define ASUS_HOTK_HID           "ATK0100"
57 #define ASUS_HOTK_FILE          "asus-laptop"
58 #define ASUS_HOTK_PREFIX        "\\_SB.ATKD."
59
60 /*
61  * Some events we use, same for all Asus
62  */
63 #define ATKD_BR_UP       0x10
64 #define ATKD_BR_DOWN     0x20
65 #define ATKD_LCD_ON      0x33
66 #define ATKD_LCD_OFF     0x34
67
68 /*
69  * Known bits returned by \_SB.ATKD.HWRS
70  */
71 #define WL_HWRS     0x80
72 #define BT_HWRS     0x100
73
74 /*
75  * Flags for hotk status
76  * WL_ON and BT_ON are also used for wireless_status()
77  */
78 #define WL_ON       0x01        //internal Wifi
79 #define BT_ON       0x02        //internal Bluetooth
80 #define MLED_ON     0x04        //mail LED
81 #define TLED_ON     0x08        //touchpad LED
82 #define RLED_ON     0x10        //Record LED
83 #define PLED_ON     0x20        //Phone LED
84 #define GLED_ON     0x40        //Gaming LED
85 #define LCD_ON      0x80        //LCD backlight
86
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
93
94 MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
95 MODULE_DESCRIPTION(ASUS_HOTK_NAME);
96 MODULE_LICENSE("GPL");
97
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 =)
106  */
107 static uint wapf = 1;
108 module_param(wapf, uint, 0644);
109 MODULE_PARM_DESC(wapf, "WAPF value");
110
111 #define ASUS_HANDLE(object, paths...)                                   \
112         static acpi_handle  object##_handle = NULL;                     \
113         static char *object##_paths[] = { paths }
114
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) */
121
122 /* LEDD */
123 ASUS_HANDLE(ledd_set, ASUS_HOTK_PREFIX "SLCM");
124
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.
128  */
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 */
132
133 /* Brightness */
134 ASUS_HANDLE(brightness_set, ASUS_HOTK_PREFIX "SPLV");
135 ASUS_HANDLE(brightness_get, ASUS_HOTK_PREFIX "GPLV");
136
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.PX40.Q10",      /* S1x */
144             "\\Q10");           /* A2x, L2D, L3D, M2E */
145
146 /* Display */
147 ASUS_HANDLE(display_set, ASUS_HOTK_PREFIX "SDSP");
148 ASUS_HANDLE(display_get, "\\_SB.PCI0.P0P1.VGA.GETD",    /*  A6B, A6K A6R A7D F3JM L4R M6R A3G
149                                                            M6A M6V VX-1 V6J V6V W3Z */
150             "\\_SB.PCI0.P0P2.VGA.GETD", /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V
151                                            S5A M5A z33A W1Jc W2V */
152             "\\_SB.PCI0.P0P3.VGA.GETD", /* A6V A6Q */
153             "\\_SB.PCI0.P0PA.VGA.GETD", /* A6T, A6M */
154             "\\_SB.PCI0.PCI1.VGAC.NMAP",        /* L3C */
155             "\\_SB.PCI0.VGA.GETD",      /* Z96F */
156             "\\ACTD",           /* A2D */
157             "\\ADVG",           /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
158             "\\DNXT",           /* P30 */
159             "\\INFB",           /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
160             "\\SSTE");          /* A3F A6F A3N A3L M6N W3N W6A */
161
162 ASUS_HANDLE(ls_switch, ASUS_HOTK_PREFIX "ALSC");        /* Z71A Z71V */
163 ASUS_HANDLE(ls_level, ASUS_HOTK_PREFIX "ALSL"); /* Z71A Z71V */
164
165 /*
166  * This is the main structure, we can use it to store anything interesting
167  * about the hotk device
168  */
169 struct asus_hotk {
170         char *name;             //laptop name
171         struct acpi_device *device;     //the device we are in
172         acpi_handle handle;     //the handle of the hotk device
173         char status;            //status of the hotk, for LEDs, ...
174         u32 ledd_status;        //status of the LED display
175         u8 light_level;         //light sensor level
176         u8 light_switch;        //light sensor switch value
177         u16 event_count[128];   //count for each event TODO make this better
178 };
179
180 /*
181  * This header is made available to allow proper configuration given model,
182  * revision number , ... this info cannot go in struct asus_hotk because it is
183  * available before the hotk
184  */
185 static struct acpi_table_header *asus_info;
186
187 /* The actual device the driver binds to */
188 static struct asus_hotk *hotk;
189
190 /*
191  * The hotkey driver declaration
192  */
193 static int asus_hotk_add(struct acpi_device *device);
194 static int asus_hotk_remove(struct acpi_device *device, int type);
195 static struct acpi_driver asus_hotk_driver = {
196         .name = ASUS_HOTK_NAME,
197         .class = ASUS_HOTK_CLASS,
198         .ids = ASUS_HOTK_HID,
199         .ops = {
200                 .add = asus_hotk_add,
201                 .remove = asus_hotk_remove,
202                 },
203 };
204
205 /* The backlight device /sys/class/backlight */
206 static struct backlight_device *asus_backlight_device;
207
208 /*
209  * The backlight class declaration
210  */
211 static int read_brightness(struct backlight_device *bd);
212 static int update_bl_status(struct backlight_device *bd);
213 static struct backlight_ops asusbl_ops = {
214         .get_brightness = read_brightness,
215         .update_status = update_bl_status,
216 };
217
218 /* These functions actually update the LED's, and are called from a
219  * workqueue. By doing this as separate work rather than when the LED
220  * subsystem asks, we avoid messing with the Asus ACPI stuff during a
221  * potentially bad time, such as a timer interrupt. */
222 static struct workqueue_struct *led_workqueue;
223
224 #define ASUS_LED(object, ledname)                                       \
225         static void object##_led_set(struct led_classdev *led_cdev,     \
226                                      enum led_brightness value);        \
227         static void object##_led_update(struct work_struct *ignored);   \
228         static int object##_led_wk;                                     \
229         static DECLARE_WORK(object##_led_work, object##_led_update);    \
230         static struct led_classdev object##_led = {                     \
231                 .name           = "asus:" ledname,                      \
232                 .brightness_set = object##_led_set,                     \
233         }
234
235 ASUS_LED(mled, "mail");
236 ASUS_LED(tled, "touchpad");
237 ASUS_LED(rled, "record");
238 ASUS_LED(pled, "phone");
239 ASUS_LED(gled, "gaming");
240
241 /*
242  * This function evaluates an ACPI method, given an int as parameter, the
243  * method is searched within the scope of the handle, can be NULL. The output
244  * of the method is written is output, which can also be NULL
245  *
246  * returns 1 if write is successful, 0 else.
247  */
248 static int write_acpi_int(acpi_handle handle, const char *method, int val,
249                           struct acpi_buffer *output)
250 {
251         struct acpi_object_list params; //list of input parameters (an int here)
252         union acpi_object in_obj;       //the only param we use
253         acpi_status status;
254
255         params.count = 1;
256         params.pointer = &in_obj;
257         in_obj.type = ACPI_TYPE_INTEGER;
258         in_obj.integer.value = val;
259
260         status = acpi_evaluate_object(handle, (char *)method, &params, output);
261         return (status == AE_OK);
262 }
263
264 static int read_wireless_status(int mask)
265 {
266         ulong status;
267         acpi_status rv = AE_OK;
268
269         if (!wireless_status_handle)
270                 return (hotk->status & mask) ? 1 : 0;
271
272         rv = acpi_evaluate_integer(wireless_status_handle, NULL, NULL, &status);
273         if (ACPI_FAILURE(rv))
274                 printk(ASUS_WARNING "Error reading Wireless status\n");
275         else
276                 return (status & mask) ? 1 : 0;
277
278         return (hotk->status & mask) ? 1 : 0;
279 }
280
281 /* Generic LED functions */
282 static int read_status(int mask)
283 {
284         /* There is a special method for both wireless devices */
285         if (mask == BT_ON || mask == WL_ON)
286                 return read_wireless_status(mask);
287
288         return (hotk->status & mask) ? 1 : 0;
289 }
290
291 static void write_status(acpi_handle handle, int out, int mask)
292 {
293         hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
294
295         switch (mask) {
296         case MLED_ON:
297                 out = !out & 0x1;
298                 break;
299         case GLED_ON:
300                 out = (out & 0x1) + 1;
301                 break;
302         default:
303                 out &= 0x1;
304                 break;
305         }
306
307         if (handle && !write_acpi_int(handle, NULL, out, NULL))
308                 printk(ASUS_WARNING " write failed %x\n", mask);
309 }
310
311 /* /sys/class/led handlers */
312 #define ASUS_LED_HANDLER(object, mask)                                  \
313         static void object##_led_set(struct led_classdev *led_cdev,     \
314                                      enum led_brightness value)         \
315         {                                                               \
316                 object##_led_wk = value;                                \
317                 queue_work(led_workqueue, &object##_led_work);          \
318         }                                                               \
319         static void object##_led_update(struct work_struct *ignored)    \
320         {                                                               \
321                 int value = object##_led_wk;                            \
322                 write_status(object##_set_handle, value, (mask));       \
323         }
324
325 ASUS_LED_HANDLER(mled, MLED_ON);
326 ASUS_LED_HANDLER(pled, PLED_ON);
327 ASUS_LED_HANDLER(rled, RLED_ON);
328 ASUS_LED_HANDLER(tled, TLED_ON);
329 ASUS_LED_HANDLER(gled, GLED_ON);
330
331 static int get_lcd_state(void)
332 {
333         return read_status(LCD_ON);
334 }
335
336 static int set_lcd_state(int value)
337 {
338         int lcd = 0;
339         acpi_status status = 0;
340
341         lcd = value ? 1 : 0;
342
343         if (lcd == get_lcd_state())
344                 return 0;
345
346         if (lcd_switch_handle) {
347                 status = acpi_evaluate_object(lcd_switch_handle,
348                                               NULL, NULL, NULL);
349
350                 if (ACPI_FAILURE(status))
351                         printk(ASUS_WARNING "Error switching LCD\n");
352         }
353
354         write_status(NULL, lcd, LCD_ON);
355         return 0;
356 }
357
358 static void lcd_blank(int blank)
359 {
360         struct backlight_device *bd = asus_backlight_device;
361
362         if (bd) {
363                 bd->props.power = blank;
364                 backlight_update_status(bd);
365         }
366 }
367
368 static int read_brightness(struct backlight_device *bd)
369 {
370         ulong value;
371         acpi_status rv = AE_OK;
372
373         rv = acpi_evaluate_integer(brightness_get_handle, NULL, NULL, &value);
374         if (ACPI_FAILURE(rv))
375                 printk(ASUS_WARNING "Error reading brightness\n");
376
377         return value;
378 }
379
380 static int set_brightness(struct backlight_device *bd, int value)
381 {
382         int ret = 0;
383
384         value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
385         /* 0 <= value <= 15 */
386
387         if (!write_acpi_int(brightness_set_handle, NULL, value, NULL)) {
388                 printk(ASUS_WARNING "Error changing brightness\n");
389                 ret = -EIO;
390         }
391
392         return ret;
393 }
394
395 static int update_bl_status(struct backlight_device *bd)
396 {
397         int rv;
398         int value = bd->props.brightness;
399
400         rv = set_brightness(bd, value);
401         if (rv)
402                 return rv;
403
404         value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
405         return set_lcd_state(value);
406 }
407
408 /*
409  * Platform device handlers
410  */
411
412 /*
413  * We write our info in page, we begin at offset off and cannot write more
414  * than count bytes. We set eof to 1 if we handle those 2 values. We return the
415  * number of bytes written in page
416  */
417 static ssize_t show_infos(struct device *dev,
418                           struct device_attribute *attr, char *page)
419 {
420         int len = 0;
421         ulong temp;
422         char buf[16];           //enough for all info
423         acpi_status rv = AE_OK;
424
425         /*
426          * We use the easy way, we don't care of off and count, so we don't set eof
427          * to 1
428          */
429
430         len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
431         len += sprintf(page + len, "Model reference    : %s\n", hotk->name);
432         /*
433          * The SFUN method probably allows the original driver to get the list
434          * of features supported by a given model. For now, 0x0100 or 0x0800
435          * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
436          * The significance of others is yet to be found.
437          */
438         rv = acpi_evaluate_integer(hotk->handle, "SFUN", NULL, &temp);
439         if (!ACPI_FAILURE(rv))
440                 len += sprintf(page + len, "SFUN value         : 0x%04x\n",
441                                (uint) temp);
442         /*
443          * Another value for userspace: the ASYM method returns 0x02 for
444          * battery low and 0x04 for battery critical, its readings tend to be
445          * more accurate than those provided by _BST.
446          * Note: since not all the laptops provide this method, errors are
447          * silently ignored.
448          */
449         rv = acpi_evaluate_integer(hotk->handle, "ASYM", NULL, &temp);
450         if (!ACPI_FAILURE(rv))
451                 len += sprintf(page + len, "ASYM value         : 0x%04x\n",
452                                (uint) temp);
453         if (asus_info) {
454                 snprintf(buf, 16, "%d", asus_info->length);
455                 len += sprintf(page + len, "DSDT length        : %s\n", buf);
456                 snprintf(buf, 16, "%d", asus_info->checksum);
457                 len += sprintf(page + len, "DSDT checksum      : %s\n", buf);
458                 snprintf(buf, 16, "%d", asus_info->revision);
459                 len += sprintf(page + len, "DSDT revision      : %s\n", buf);
460                 snprintf(buf, 7, "%s", asus_info->oem_id);
461                 len += sprintf(page + len, "OEM id             : %s\n", buf);
462                 snprintf(buf, 9, "%s", asus_info->oem_table_id);
463                 len += sprintf(page + len, "OEM table id       : %s\n", buf);
464                 snprintf(buf, 16, "%x", asus_info->oem_revision);
465                 len += sprintf(page + len, "OEM revision       : 0x%s\n", buf);
466                 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
467                 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
468                 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
469                 len += sprintf(page + len, "ASL comp revision  : 0x%s\n", buf);
470         }
471
472         return len;
473 }
474
475 static int parse_arg(const char *buf, unsigned long count, int *val)
476 {
477         if (!count)
478                 return 0;
479         if (count > 31)
480                 return -EINVAL;
481         if (sscanf(buf, "%i", val) != 1)
482                 return -EINVAL;
483         return count;
484 }
485
486 static ssize_t store_status(const char *buf, size_t count,
487                             acpi_handle handle, int mask)
488 {
489         int rv, value;
490         int out = 0;
491
492         rv = parse_arg(buf, count, &value);
493         if (rv > 0)
494                 out = value ? 1 : 0;
495
496         write_status(handle, out, mask);
497
498         return rv;
499 }
500
501 /*
502  * LEDD display
503  */
504 static ssize_t show_ledd(struct device *dev,
505                          struct device_attribute *attr, char *buf)
506 {
507         return sprintf(buf, "0x%08x\n", hotk->ledd_status);
508 }
509
510 static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
511                           const char *buf, size_t count)
512 {
513         int rv, value;
514
515         rv = parse_arg(buf, count, &value);
516         if (rv > 0) {
517                 if (!write_acpi_int(ledd_set_handle, NULL, value, NULL))
518                         printk(ASUS_WARNING "LED display write failed\n");
519                 else
520                         hotk->ledd_status = (u32) value;
521         }
522         return rv;
523 }
524
525 /*
526  * WLAN
527  */
528 static ssize_t show_wlan(struct device *dev,
529                          struct device_attribute *attr, char *buf)
530 {
531         return sprintf(buf, "%d\n", read_status(WL_ON));
532 }
533
534 static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
535                           const char *buf, size_t count)
536 {
537         return store_status(buf, count, wl_switch_handle, WL_ON);
538 }
539
540 /*
541  * Bluetooth
542  */
543 static ssize_t show_bluetooth(struct device *dev,
544                               struct device_attribute *attr, char *buf)
545 {
546         return sprintf(buf, "%d\n", read_status(BT_ON));
547 }
548
549 static ssize_t store_bluetooth(struct device *dev,
550                                struct device_attribute *attr, const char *buf,
551                                size_t count)
552 {
553         return store_status(buf, count, bt_switch_handle, BT_ON);
554 }
555
556 /*
557  * Display
558  */
559 static void set_display(int value)
560 {
561         /* no sanity check needed for now */
562         if (!write_acpi_int(display_set_handle, NULL, value, NULL))
563                 printk(ASUS_WARNING "Error setting display\n");
564         return;
565 }
566
567 static int read_display(void)
568 {
569         ulong value = 0;
570         acpi_status rv = AE_OK;
571
572         /* In most of the case, we know how to set the display, but sometime
573            we can't read it */
574         if (display_get_handle) {
575                 rv = acpi_evaluate_integer(display_get_handle, NULL,
576                                            NULL, &value);
577                 if (ACPI_FAILURE(rv))
578                         printk(ASUS_WARNING "Error reading display status\n");
579         }
580
581         value &= 0x0F;          /* needed for some models, shouldn't hurt others */
582
583         return value;
584 }
585
586 /*
587  * Now, *this* one could be more user-friendly, but so far, no-one has
588  * complained. The significance of bits is the same as in store_disp()
589  */
590 static ssize_t show_disp(struct device *dev,
591                          struct device_attribute *attr, char *buf)
592 {
593         return sprintf(buf, "%d\n", read_display());
594 }
595
596 /*
597  * Experimental support for display switching. As of now: 1 should activate
598  * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
599  * Any combination (bitwise) of these will suffice. I never actually tested 4
600  * displays hooked up simultaneously, so be warned. See the acpi4asus README
601  * for more info.
602  */
603 static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
604                           const char *buf, size_t count)
605 {
606         int rv, value;
607
608         rv = parse_arg(buf, count, &value);
609         if (rv > 0)
610                 set_display(value);
611         return rv;
612 }
613
614 /*
615  * Light Sens
616  */
617 static void set_light_sens_switch(int value)
618 {
619         if (!write_acpi_int(ls_switch_handle, NULL, value, NULL))
620                 printk(ASUS_WARNING "Error setting light sensor switch\n");
621         hotk->light_switch = value;
622 }
623
624 static ssize_t show_lssw(struct device *dev,
625                          struct device_attribute *attr, char *buf)
626 {
627         return sprintf(buf, "%d\n", hotk->light_switch);
628 }
629
630 static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
631                           const char *buf, size_t count)
632 {
633         int rv, value;
634
635         rv = parse_arg(buf, count, &value);
636         if (rv > 0)
637                 set_light_sens_switch(value ? 1 : 0);
638
639         return rv;
640 }
641
642 static void set_light_sens_level(int value)
643 {
644         if (!write_acpi_int(ls_level_handle, NULL, value, NULL))
645                 printk(ASUS_WARNING "Error setting light sensor level\n");
646         hotk->light_level = value;
647 }
648
649 static ssize_t show_lslvl(struct device *dev,
650                           struct device_attribute *attr, char *buf)
651 {
652         return sprintf(buf, "%d\n", hotk->light_level);
653 }
654
655 static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
656                            const char *buf, size_t count)
657 {
658         int rv, value;
659
660         rv = parse_arg(buf, count, &value);
661         if (rv > 0) {
662                 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
663                 /* 0 <= value <= 15 */
664                 set_light_sens_level(value);
665         }
666
667         return rv;
668 }
669
670 static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
671 {
672         /* TODO Find a better way to handle events count. */
673         if (!hotk)
674                 return;
675
676         /*
677          * We need to tell the backlight device when the backlight power is
678          * switched
679          */
680         if (event == ATKD_LCD_ON) {
681                 write_status(NULL, 1, LCD_ON);
682                 lcd_blank(FB_BLANK_UNBLANK);
683         } else if (event == ATKD_LCD_OFF) {
684                 write_status(NULL, 0, LCD_ON);
685                 lcd_blank(FB_BLANK_POWERDOWN);
686         }
687
688         acpi_bus_generate_event(hotk->device, event,
689                                 hotk->event_count[event % 128]++);
690
691         return;
692 }
693
694 #define ASUS_CREATE_DEVICE_ATTR(_name)                                  \
695         struct device_attribute dev_attr_##_name = {                    \
696                 .attr = {                                               \
697                         .name = __stringify(_name),                     \
698                         .mode = 0,                                      \
699                         .owner = THIS_MODULE },                         \
700                 .show   = NULL,                                         \
701                 .store  = NULL,                                         \
702         }
703
704 #define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store)               \
705         do {                                                            \
706                 dev_attr_##_name.attr.mode = _mode;                     \
707                 dev_attr_##_name.show = _show;                          \
708                 dev_attr_##_name.store = _store;                        \
709         } while(0)
710
711 static ASUS_CREATE_DEVICE_ATTR(infos);
712 static ASUS_CREATE_DEVICE_ATTR(wlan);
713 static ASUS_CREATE_DEVICE_ATTR(bluetooth);
714 static ASUS_CREATE_DEVICE_ATTR(display);
715 static ASUS_CREATE_DEVICE_ATTR(ledd);
716 static ASUS_CREATE_DEVICE_ATTR(ls_switch);
717 static ASUS_CREATE_DEVICE_ATTR(ls_level);
718
719 static struct attribute *asuspf_attributes[] = {
720         &dev_attr_infos.attr,
721         &dev_attr_wlan.attr,
722         &dev_attr_bluetooth.attr,
723         &dev_attr_display.attr,
724         &dev_attr_ledd.attr,
725         &dev_attr_ls_switch.attr,
726         &dev_attr_ls_level.attr,
727         NULL
728 };
729
730 static struct attribute_group asuspf_attribute_group = {
731         .attrs = asuspf_attributes
732 };
733
734 static struct platform_driver asuspf_driver = {
735         .driver = {
736                    .name = ASUS_HOTK_FILE,
737                    .owner = THIS_MODULE,
738                    }
739 };
740
741 static struct platform_device *asuspf_device;
742
743 static void asus_hotk_add_fs(void)
744 {
745         ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
746
747         if (wl_switch_handle)
748                 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
749
750         if (bt_switch_handle)
751                 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
752                                      show_bluetooth, store_bluetooth);
753
754         if (display_set_handle && display_get_handle)
755                 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
756         else if (display_set_handle)
757                 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
758
759         if (ledd_set_handle)
760                 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
761
762         if (ls_switch_handle && ls_level_handle) {
763                 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
764                 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
765         }
766 }
767
768 static int asus_handle_init(char *name, acpi_handle * handle,
769                             char **paths, int num_paths)
770 {
771         int i;
772         acpi_status status;
773
774         for (i = 0; i < num_paths; i++) {
775                 status = acpi_get_handle(NULL, paths[i], handle);
776                 if (ACPI_SUCCESS(status))
777                         return 0;
778         }
779
780         *handle = NULL;
781         return -ENODEV;
782 }
783
784 #define ASUS_HANDLE_INIT(object)                                        \
785         asus_handle_init(#object, &object##_handle, object##_paths,     \
786                          ARRAY_SIZE(object##_paths))
787
788 /*
789  * This function is used to initialize the hotk with right values. In this
790  * method, we can make all the detection we want, and modify the hotk struct
791  */
792 static int asus_hotk_get_info(void)
793 {
794         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
795         union acpi_object *model = NULL;
796         ulong bsts_result, hwrs_result;
797         char *string = NULL;
798         acpi_status status;
799
800         /*
801          * Get DSDT headers early enough to allow for differentiating between
802          * models, but late enough to allow acpi_bus_register_driver() to fail
803          * before doing anything ACPI-specific. Should we encounter a machine,
804          * which needs special handling (i.e. its hotkey device has a different
805          * HID), this bit will be moved. A global variable asus_info contains
806          * the DSDT header.
807          */
808         status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
809         if (ACPI_FAILURE(status))
810                 printk(ASUS_WARNING "Couldn't get the DSDT table header\n");
811
812         /* We have to write 0 on init this far for all ASUS models */
813         if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
814                 printk(ASUS_ERR "Hotkey initialization failed\n");
815                 return -ENODEV;
816         }
817
818         /* This needs to be called for some laptops to init properly */
819         status =
820             acpi_evaluate_integer(hotk->handle, "BSTS", NULL, &bsts_result);
821         if (ACPI_FAILURE(status))
822                 printk(ASUS_WARNING "Error calling BSTS\n");
823         else if (bsts_result)
824                 printk(ASUS_NOTICE "BSTS called, 0x%02x returned\n",
825                        (uint) bsts_result);
826
827         /* This too ... */
828         write_acpi_int(hotk->handle, "CWAP", wapf, NULL);
829
830         /*
831          * Try to match the object returned by INIT to the specific model.
832          * Handle every possible object (or the lack of thereof) the DSDT
833          * writers might throw at us. When in trouble, we pass NULL to
834          * asus_model_match() and try something completely different.
835          */
836         if (buffer.pointer) {
837                 model = buffer.pointer;
838                 switch (model->type) {
839                 case ACPI_TYPE_STRING:
840                         string = model->string.pointer;
841                         break;
842                 case ACPI_TYPE_BUFFER:
843                         string = model->buffer.pointer;
844                         break;
845                 default:
846                         string = "";
847                         break;
848                 }
849         }
850         hotk->name = kstrdup(string, GFP_KERNEL);
851         if (!hotk->name)
852                 return -ENOMEM;
853
854         if (*string)
855                 printk(ASUS_NOTICE "  %s model detected\n", string);
856
857         ASUS_HANDLE_INIT(mled_set);
858         ASUS_HANDLE_INIT(tled_set);
859         ASUS_HANDLE_INIT(rled_set);
860         ASUS_HANDLE_INIT(pled_set);
861         ASUS_HANDLE_INIT(gled_set);
862
863         ASUS_HANDLE_INIT(ledd_set);
864
865         /*
866          * The HWRS method return informations about the hardware.
867          * 0x80 bit is for WLAN, 0x100 for Bluetooth.
868          * The significance of others is yet to be found.
869          * If we don't find the method, we assume the device are present.
870          */
871         status =
872             acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &hwrs_result);
873         if (ACPI_FAILURE(status))
874                 hwrs_result = WL_HWRS | BT_HWRS;
875
876         if (hwrs_result & WL_HWRS)
877                 ASUS_HANDLE_INIT(wl_switch);
878         if (hwrs_result & BT_HWRS)
879                 ASUS_HANDLE_INIT(bt_switch);
880
881         ASUS_HANDLE_INIT(wireless_status);
882
883         ASUS_HANDLE_INIT(brightness_set);
884         ASUS_HANDLE_INIT(brightness_get);
885
886         ASUS_HANDLE_INIT(lcd_switch);
887
888         ASUS_HANDLE_INIT(display_set);
889         ASUS_HANDLE_INIT(display_get);
890
891         /* There is a lot of models with "ALSL", but a few get
892            a real light sens, so we need to check it. */
893         if (ASUS_HANDLE_INIT(ls_switch))
894                 ASUS_HANDLE_INIT(ls_level);
895
896         kfree(model);
897
898         return AE_OK;
899 }
900
901 static int asus_hotk_check(void)
902 {
903         int result = 0;
904
905         result = acpi_bus_get_status(hotk->device);
906         if (result)
907                 return result;
908
909         if (hotk->device->status.present) {
910                 result = asus_hotk_get_info();
911         } else {
912                 printk(ASUS_ERR "Hotkey device not present, aborting\n");
913                 return -EINVAL;
914         }
915
916         return result;
917 }
918
919 static int asus_hotk_found;
920
921 static int asus_hotk_add(struct acpi_device *device)
922 {
923         acpi_status status = AE_OK;
924         int result;
925
926         if (!device)
927                 return -EINVAL;
928
929         printk(ASUS_NOTICE "Asus Laptop Support version %s\n",
930                ASUS_LAPTOP_VERSION);
931
932         hotk = kmalloc(sizeof(struct asus_hotk), GFP_KERNEL);
933         if (!hotk)
934                 return -ENOMEM;
935         memset(hotk, 0, sizeof(struct asus_hotk));
936
937         hotk->handle = device->handle;
938         strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
939         strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
940         acpi_driver_data(device) = hotk;
941         hotk->device = device;
942
943         result = asus_hotk_check();
944         if (result)
945                 goto end;
946
947         asus_hotk_add_fs();
948
949         /*
950          * We install the handler, it will receive the hotk in parameter, so, we
951          * could add other data to the hotk struct
952          */
953         status = acpi_install_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
954                                              asus_hotk_notify, hotk);
955         if (ACPI_FAILURE(status))
956                 printk(ASUS_ERR "Error installing notify handler\n");
957
958         asus_hotk_found = 1;
959
960         /* WLED and BLED are on by default */
961         write_status(bt_switch_handle, 1, BT_ON);
962         write_status(wl_switch_handle, 1, WL_ON);
963
964         /* If the h/w switch is off, we need to check the real status */
965         write_status(NULL, read_status(BT_ON), BT_ON);
966         write_status(NULL, read_status(WL_ON), WL_ON);
967
968         /* LCD Backlight is on by default */
969         write_status(NULL, 1, LCD_ON);
970
971         /* LED display is off by default */
972         hotk->ledd_status = 0xFFF;
973
974         /* Set initial values of light sensor and level */
975         hotk->light_switch = 1; /* Default to light sensor disabled */
976         hotk->light_level = 0;  /* level 5 for sensor sensitivity */
977
978         if (ls_switch_handle)
979                 set_light_sens_switch(hotk->light_switch);
980
981         if (ls_level_handle)
982                 set_light_sens_level(hotk->light_level);
983
984       end:
985         if (result) {
986                 kfree(hotk->name);
987                 kfree(hotk);
988         }
989
990         return result;
991 }
992
993 static int asus_hotk_remove(struct acpi_device *device, int type)
994 {
995         acpi_status status = 0;
996
997         if (!device || !acpi_driver_data(device))
998                 return -EINVAL;
999
1000         status = acpi_remove_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
1001                                             asus_hotk_notify);
1002         if (ACPI_FAILURE(status))
1003                 printk(ASUS_ERR "Error removing notify handler\n");
1004
1005         kfree(hotk->name);
1006         kfree(hotk);
1007
1008         return 0;
1009 }
1010
1011 static void asus_backlight_exit(void)
1012 {
1013         if (asus_backlight_device)
1014                 backlight_device_unregister(asus_backlight_device);
1015 }
1016
1017 #define  ASUS_LED_UNREGISTER(object)                            \
1018         if(object##_led.class_dev                               \
1019            && !IS_ERR(object##_led.class_dev))                  \
1020                 led_classdev_unregister(&object##_led)
1021
1022 static void asus_led_exit(void)
1023 {
1024         ASUS_LED_UNREGISTER(mled);
1025         ASUS_LED_UNREGISTER(tled);
1026         ASUS_LED_UNREGISTER(pled);
1027         ASUS_LED_UNREGISTER(rled);
1028         ASUS_LED_UNREGISTER(gled);
1029
1030         destroy_workqueue(led_workqueue);
1031 }
1032
1033 static void __exit asus_laptop_exit(void)
1034 {
1035         asus_backlight_exit();
1036         asus_led_exit();
1037
1038         acpi_bus_unregister_driver(&asus_hotk_driver);
1039         sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
1040         platform_device_unregister(asuspf_device);
1041         platform_driver_unregister(&asuspf_driver);
1042 }
1043
1044 static int asus_backlight_init(struct device *dev)
1045 {
1046         struct backlight_device *bd;
1047
1048         if (brightness_set_handle && lcd_switch_handle) {
1049                 bd = backlight_device_register(ASUS_HOTK_FILE, dev,
1050                                                NULL, &asusbl_ops);
1051                 if (IS_ERR(bd)) {
1052                         printk(ASUS_ERR
1053                                "Could not register asus backlight device\n");
1054                         asus_backlight_device = NULL;
1055                         return PTR_ERR(bd);
1056                 }
1057
1058                 asus_backlight_device = bd;
1059
1060                 bd->props.max_brightness = 15;
1061                 bd->props.brightness = read_brightness(NULL);
1062                 bd->props.power = FB_BLANK_UNBLANK;
1063                 backlight_update_status(bd);
1064         }
1065         return 0;
1066 }
1067
1068 static int asus_led_register(acpi_handle handle,
1069                              struct led_classdev *ldev, struct device *dev)
1070 {
1071         if (!handle)
1072                 return 0;
1073
1074         return led_classdev_register(dev, ldev);
1075 }
1076
1077 #define ASUS_LED_REGISTER(object, device)                               \
1078         asus_led_register(object##_set_handle, &object##_led, device)
1079
1080 static int asus_led_init(struct device *dev)
1081 {
1082         int rv;
1083
1084         rv = ASUS_LED_REGISTER(mled, dev);
1085         if (rv)
1086                 return rv;
1087
1088         rv = ASUS_LED_REGISTER(tled, dev);
1089         if (rv)
1090                 return rv;
1091
1092         rv = ASUS_LED_REGISTER(rled, dev);
1093         if (rv)
1094                 return rv;
1095
1096         rv = ASUS_LED_REGISTER(pled, dev);
1097         if (rv)
1098                 return rv;
1099
1100         rv = ASUS_LED_REGISTER(gled, dev);
1101         if (rv)
1102                 return rv;
1103
1104         led_workqueue = create_singlethread_workqueue("led_workqueue");
1105         if (!led_workqueue)
1106                 return -ENOMEM;
1107
1108         return 0;
1109 }
1110
1111 static int __init asus_laptop_init(void)
1112 {
1113         struct device *dev;
1114         int result;
1115
1116         if (acpi_disabled)
1117                 return -ENODEV;
1118
1119         result = acpi_bus_register_driver(&asus_hotk_driver);
1120         if (result < 0)
1121                 return result;
1122
1123         /*
1124          * This is a bit of a kludge.  We only want this module loaded
1125          * for ASUS systems, but there's currently no way to probe the
1126          * ACPI namespace for ASUS HIDs.  So we just return failure if
1127          * we didn't find one, which will cause the module to be
1128          * unloaded.
1129          */
1130         if (!asus_hotk_found) {
1131                 acpi_bus_unregister_driver(&asus_hotk_driver);
1132                 return -ENODEV;
1133         }
1134
1135         dev = acpi_get_physical_device(hotk->device->handle);
1136
1137         result = asus_backlight_init(dev);
1138         if (result)
1139                 goto fail_backlight;
1140
1141         result = asus_led_init(dev);
1142         if (result)
1143                 goto fail_led;
1144
1145         /* Register platform stuff */
1146         result = platform_driver_register(&asuspf_driver);
1147         if (result)
1148                 goto fail_platform_driver;
1149
1150         asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
1151         if (!asuspf_device) {
1152                 result = -ENOMEM;
1153                 goto fail_platform_device1;
1154         }
1155
1156         result = platform_device_add(asuspf_device);
1157         if (result)
1158                 goto fail_platform_device2;
1159
1160         result = sysfs_create_group(&asuspf_device->dev.kobj,
1161                                     &asuspf_attribute_group);
1162         if (result)
1163                 goto fail_sysfs;
1164
1165         return 0;
1166
1167       fail_sysfs:
1168         platform_device_del(asuspf_device);
1169
1170       fail_platform_device2:
1171         platform_device_put(asuspf_device);
1172
1173       fail_platform_device1:
1174         platform_driver_unregister(&asuspf_driver);
1175
1176       fail_platform_driver:
1177         asus_led_exit();
1178
1179       fail_led:
1180         asus_backlight_exit();
1181
1182       fail_backlight:
1183
1184         return result;
1185 }
1186
1187 module_init(asus_laptop_init);
1188 module_exit(asus_laptop_exit);