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