Pull sony into release branch
[pandora-kernel.git] / drivers / misc / asus-laptop.c
index bd963c6..861c399 100644 (file)
@@ -79,9 +79,9 @@
 #define BT_ON       0x02       //internal Bluetooth
 #define MLED_ON     0x04       //mail LED
 #define TLED_ON     0x08       //touchpad LED
-#define RLED_ON     0x10        //Record LED
-#define PLED_ON     0x20        //Phone LED
-#define LCD_ON      0x40        //LCD backlight
+#define RLED_ON     0x10       //Record LED
+#define PLED_ON     0x20       //Phone LED
+#define LCD_ON      0x40       //LCD backlight
 
 #define ASUS_LOG    ASUS_HOTK_FILE ": "
 #define ASUS_ERR    KERN_ERR    ASUS_LOG
@@ -101,8 +101,8 @@ MODULE_LICENSE("GPL");
 /* LED */
 ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
 ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
-ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
-ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
+ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED");        /* W1JC */
+ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED");        /* A7J */
 
 /* LEDD */
 ASUS_HANDLE(ledd_set, ASUS_HOTK_PREFIX "SLCM");
@@ -113,48 +113,52 @@ ASUS_HANDLE(ledd_set, ASUS_HOTK_PREFIX "SLCM");
  */
 ASUS_HANDLE(wl_switch, ASUS_HOTK_PREFIX "WLED");
 ASUS_HANDLE(bt_switch, ASUS_HOTK_PREFIX "BLED");
-ASUS_HANDLE(wireless_status, ASUS_HOTK_PREFIX "RSTS"); /* All new models */
+ASUS_HANDLE(wireless_status, ASUS_HOTK_PREFIX "RSTS"); /* All new models */
 
 /* Brightness */
 ASUS_HANDLE(brightness_set, ASUS_HOTK_PREFIX "SPLV");
 ASUS_HANDLE(brightness_get, ASUS_HOTK_PREFIX "GPLV");
 
 /* Backlight */
-ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
-           "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
-           "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
-           "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
-           "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
-           "\\_SB.PCI0.PX40.Q10", /* S1x */
-           "\\Q10");  /* A2x, L2D, L3D, M2E */
+ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10",    /* All new models */
+           "\\_SB.PCI0.ISA.EC0._Q10",  /* A1x */
+           "\\_SB.PCI0.PX40.ECD0._Q10",        /* L3C */
+           "\\_SB.PCI0.PX40.EC0.Q10",  /* M1A */
+           "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
+           "\\_SB.PCI0.PX40.Q10",      /* S1x */
+           "\\Q10");           /* A2x, L2D, L3D, M2E */
 
 /* Display */
 ASUS_HANDLE(display_set, ASUS_HOTK_PREFIX "SDSP");
-ASUS_HANDLE(display_get,
-           "\\_SB.PCI0.P0P1.VGA.GETD", /*  A6B, A6K A6R A7D F3JM L4R M6R A3G
-                                           M6A M6V VX-1 V6J V6V W3Z */
-           "\\_SB.PCI0.P0P2.VGA.GETD", /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V
+ASUS_HANDLE(display_get, "\\_SB.PCI0.P0P1.VGA.GETD",   /*  A6B, A6K A6R A7D F3JM L4R M6R A3G
+                                                          M6A M6V VX-1 V6J V6V W3Z */
+           "\\_SB.PCI0.P0P2.VGA.GETD", /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V
                                           S5A M5A z33A W1Jc W2V */
-           "\\_SB.PCI0.P0P3.VGA.GETD", /* A6V A6Q */
-           "\\_SB.PCI0.P0PA.VGA.GETD", /* A6T, A6M */
-           "\\_SB.PCI0.PCI1.VGAC.NMAP", /* L3C */
-           "\\_SB.PCI0.VGA.GETD", /* Z96F */
-           "\\ACTD", /* A2D */
-           "\\ADVG", /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
-           "\\DNXT", /* P30 */
-           "\\INFB", /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
-           "\\SSTE"); /* A3F A6F A3N A3L M6N W3N W6A */
+           "\\_SB.PCI0.P0P3.VGA.GETD", /* A6V A6Q */
+           "\\_SB.PCI0.P0PA.VGA.GETD", /* A6T, A6M */
+           "\\_SB.PCI0.PCI1.VGAC.NMAP",        /* L3C */
+           "\\_SB.PCI0.VGA.GETD",      /* Z96F */
+           "\\ACTD",           /* A2D */
+           "\\ADVG",           /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
+           "\\DNXT",           /* P30 */
+           "\\INFB",           /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
+           "\\SSTE");          /* A3F A6F A3N A3L M6N W3N W6A */
+
+ASUS_HANDLE(ls_switch, ASUS_HOTK_PREFIX "ALSC");       /* Z71A Z71V */
+ASUS_HANDLE(ls_level, ASUS_HOTK_PREFIX "ALSL");        /* Z71A Z71V */
 
 /*
  * This is the main structure, we can use it to store anything interesting
  * about the hotk device
  */
 struct asus_hotk {
-       char *name; //laptop name
+       char *name;             //laptop name
        struct acpi_device *device;     //the device we are in
        acpi_handle handle;     //the handle of the hotk device
        char status;            //status of the hotk, for LEDs, ...
        u32 ledd_status;        //status of the LED display
+       u8 light_level;         //light sensor level
+       u8 light_switch;        //light sensor switch value
        u16 event_count[128];   //count for each event TODO make this better
 };
 
@@ -192,10 +196,10 @@ static struct backlight_device *asus_backlight_device;
 static int read_brightness(struct backlight_device *bd);
 static int update_bl_status(struct backlight_device *bd);
 static struct backlight_properties asusbl_data = {
-              .owner          = THIS_MODULE,
-              .get_brightness = read_brightness,
-              .update_status  = update_bl_status,
-              .max_brightness = 15,
+       .owner = THIS_MODULE,
+       .get_brightness = read_brightness,
+       .update_status = update_bl_status,
+       .max_brightness = 15,
 };
 
 /* These functions actually update the LED's, and are called from a
@@ -258,7 +262,8 @@ static int read_acpi_int(acpi_handle handle, const char *method, int *val,
        return (status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER);
 }
 
-static int read_wireless_status(int mask) {
+static int read_wireless_status(int mask)
+{
        int status;
 
        if (!wireless_status_handle)
@@ -282,8 +287,7 @@ static int read_status(int mask)
        return (hotk->status & mask) ? 1 : 0;
 }
 
-static void write_status(acpi_handle handle, int out, int mask,
-                        int invert)
+static void write_status(acpi_handle handle, int out, int mask, int invert)
 {
        hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
 
@@ -328,7 +332,7 @@ static int set_lcd_state(int value)
        if (lcd == get_lcd_state())
                return 0;
 
-       if(lcd_switch_handle) {
+       if (lcd_switch_handle) {
                status = acpi_evaluate_object(lcd_switch_handle,
                                              NULL, NULL, NULL);
 
@@ -344,12 +348,12 @@ static void lcd_blank(int blank)
 {
        struct backlight_device *bd = asus_backlight_device;
 
-       if(bd) {
+       if (bd) {
                down(&bd->sem);
-               if(likely(bd->props)) {
+               if (likely(bd->props)) {
                        bd->props->power = blank;
-                       if(likely(bd->props->update_status))
-                          bd->props->update_status(bd);
+                       if (likely(bd->props->update_status))
+                               bd->props->update_status(bd);
                }
                up(&bd->sem);
        }
@@ -386,7 +390,7 @@ static int update_bl_status(struct backlight_device *bd)
        int value = bd->props->brightness;
 
        rv = set_brightness(bd, value);
-       if(rv)
+       if (rv)
                return rv;
 
        value = (bd->props->power == FB_BLANK_UNBLANK) ? 1 : 0;
@@ -403,7 +407,7 @@ static int update_bl_status(struct backlight_device *bd)
  * number of bytes written in page
  */
 static ssize_t show_infos(struct device *dev,
-                        struct device_attribute *attr, char *page)
+                         struct device_attribute *attr, char *page)
 {
        int len = 0;
        int temp;
@@ -530,8 +534,9 @@ static ssize_t show_bluetooth(struct device *dev,
        return sprintf(buf, "%d\n", read_status(BT_ON));
 }
 
-static ssize_t store_bluetooth(struct device *dev, struct device_attribute *attr,
-                              const char *buf, size_t count)
+static ssize_t store_bluetooth(struct device *dev,
+                              struct device_attribute *attr, const char *buf,
+                              size_t count)
 {
        return store_status(buf, count, bt_switch_handle, BT_ON, 0);
 }
@@ -553,7 +558,7 @@ static int read_display(void)
 
        /* In most of the case, we know how to set the display, but sometime
           we can't read it */
-       if(display_get_handle) {
+       if (display_get_handle) {
                if (!read_acpi_int(display_get_handle, NULL, &value, NULL))
                        printk(ASUS_WARNING "Error reading display status\n");
        }
@@ -562,6 +567,7 @@ static int read_display(void)
 
        return value;
 }
+
 /*
  * Now, *this* one could be more user-friendly, but so far, no-one has
  * complained. The significance of bits is the same as in store_disp()
@@ -590,6 +596,62 @@ static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
        return rv;
 }
 
+/*
+ * Light Sens
+ */
+static void set_light_sens_switch(int value)
+{
+       if (!write_acpi_int(ls_switch_handle, NULL, value, NULL))
+               printk(ASUS_WARNING "Error setting light sensor switch\n");
+       hotk->light_switch = value;
+}
+
+static ssize_t show_lssw(struct device *dev,
+                        struct device_attribute *attr, char *buf)
+{
+       return sprintf(buf, "%d\n", hotk->light_switch);
+}
+
+static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
+                         const char *buf, size_t count)
+{
+       int rv, value;
+
+       rv = parse_arg(buf, count, &value);
+       if (rv > 0)
+               set_light_sens_switch(value ? 1 : 0);
+
+       return rv;
+}
+
+static void set_light_sens_level(int value)
+{
+       if (!write_acpi_int(ls_level_handle, NULL, value, NULL))
+               printk(ASUS_WARNING "Error setting light sensor level\n");
+       hotk->light_level = value;
+}
+
+static ssize_t show_lslvl(struct device *dev,
+                         struct device_attribute *attr, char *buf)
+{
+       return sprintf(buf, "%d\n", hotk->light_level);
+}
+
+static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
+                          const char *buf, size_t count)
+{
+       int rv, value;
+
+       rv = parse_arg(buf, count, &value);
+       if (rv > 0) {
+               value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
+               /* 0 <= value <= 15 */
+               set_light_sens_level(value);
+       }
+
+       return rv;
+}
+
 static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
 {
        /* TODO Find a better way to handle events count. */
@@ -603,7 +665,7 @@ static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
        if (event == ATKD_LCD_ON) {
                write_status(NULL, 1, LCD_ON, 0);
                lcd_blank(FB_BLANK_UNBLANK);
-       } else if(event == ATKD_LCD_OFF) {
+       } else if (event == ATKD_LCD_OFF) {
                write_status(NULL, 0, LCD_ON, 0);
                lcd_blank(FB_BLANK_POWERDOWN);
        }
@@ -636,30 +698,33 @@ static ASUS_CREATE_DEVICE_ATTR(wlan);
 static ASUS_CREATE_DEVICE_ATTR(bluetooth);
 static ASUS_CREATE_DEVICE_ATTR(display);
 static ASUS_CREATE_DEVICE_ATTR(ledd);
+static ASUS_CREATE_DEVICE_ATTR(ls_switch);
+static ASUS_CREATE_DEVICE_ATTR(ls_level);
 
 static struct attribute *asuspf_attributes[] = {
-        &dev_attr_infos.attr,
-        &dev_attr_wlan.attr,
-        &dev_attr_bluetooth.attr,
-        &dev_attr_display.attr,
-        &dev_attr_ledd.attr,
-        NULL
+       &dev_attr_infos.attr,
+       &dev_attr_wlan.attr,
+       &dev_attr_bluetooth.attr,
+       &dev_attr_display.attr,
+       &dev_attr_ledd.attr,
+       &dev_attr_ls_switch.attr,
+       &dev_attr_ls_level.attr,
+       NULL
 };
 
 static struct attribute_group asuspf_attribute_group = {
-        .attrs = asuspf_attributes
+       .attrs = asuspf_attributes
 };
 
 static struct platform_driver asuspf_driver = {
-        .driver = {
-                .name = ASUS_HOTK_FILE,
-                .owner = THIS_MODULE,
-        }
+       .driver = {
+                  .name = ASUS_HOTK_FILE,
+                  .owner = THIS_MODULE,
+                  }
 };
 
 static struct platform_device *asuspf_device;
 
-
 static void asus_hotk_add_fs(void)
 {
        ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
@@ -673,15 +738,19 @@ static void asus_hotk_add_fs(void)
 
        if (display_set_handle && display_get_handle)
                ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
-       else if(display_set_handle)
+       else if (display_set_handle)
                ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
 
        if (ledd_set_handle)
                ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
 
+       if (ls_switch_handle && ls_level_handle) {
+               ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
+               ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
+       }
 }
 
-static int asus_handle_init(char *name, acpi_handle *handle,
+static int asus_handle_init(char *name, acpi_handle * handle,
                            char **paths, int num_paths)
 {
        int i;
@@ -701,7 +770,6 @@ static int asus_handle_init(char *name, acpi_handle *handle,
        asus_handle_init(#object, &object##_handle, object##_paths,     \
                         ARRAY_SIZE(object##_paths))
 
-
 /*
  * This function is used to initialize the hotk with right values. In this
  * method, we can make all the detection we want, and modify the hotk struct
@@ -709,7 +777,6 @@ static int asus_handle_init(char *name, acpi_handle *handle,
 static int asus_hotk_get_info(void)
 {
        struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
-       struct acpi_buffer dsdt = { ACPI_ALLOCATE_BUFFER, NULL };
        union acpi_object *model = NULL;
        int bsts_result, hwrs_result;
        char *string = NULL;
@@ -723,11 +790,9 @@ static int asus_hotk_get_info(void)
         * HID), this bit will be moved. A global variable asus_info contains
         * the DSDT header.
         */
-       status = acpi_get_table(ACPI_TABLE_ID_DSDT, 1, &dsdt);
+       status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
        if (ACPI_FAILURE(status))
                printk(ASUS_WARNING "Couldn't get the DSDT table header\n");
-       else
-               asus_info = dsdt.pointer;
 
        /* We have to write 0 on init this far for all ASUS models */
        if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
@@ -766,7 +831,7 @@ static int asus_hotk_get_info(void)
        if (!hotk->name)
                return -ENOMEM;
 
-       if(*string)
+       if (*string)
                printk(ASUS_NOTICE "  %s model detected\n", string);
 
        ASUS_HANDLE_INIT(mled_set);
@@ -785,9 +850,9 @@ static int asus_hotk_get_info(void)
        if (!read_acpi_int(hotk->handle, "HRWS", &hwrs_result, NULL))
                hwrs_result = WL_HWRS | BT_HWRS;
 
-       if(hwrs_result & WL_HWRS)
+       if (hwrs_result & WL_HWRS)
                ASUS_HANDLE_INIT(wl_switch);
-       if(hwrs_result & BT_HWRS)
+       if (hwrs_result & BT_HWRS)
                ASUS_HANDLE_INIT(bt_switch);
 
        ASUS_HANDLE_INIT(wireless_status);
@@ -800,6 +865,11 @@ static int asus_hotk_get_info(void)
        ASUS_HANDLE_INIT(display_set);
        ASUS_HANDLE_INIT(display_get);
 
+       /* There is a lot of models with "ALSL", but a few get
+          a real light sens, so we need to check it. */
+       if (ASUS_HANDLE_INIT(ls_switch))
+               ASUS_HANDLE_INIT(ls_level);
+
        kfree(model);
 
        return AE_OK;
@@ -874,6 +944,16 @@ static int asus_hotk_add(struct acpi_device *device)
        /* LED display is off by default */
        hotk->ledd_status = 0xFFF;
 
+       /* Set initial values of light sensor and level */
+       hotk->light_switch = 1; /* Default to light sensor disabled */
+       hotk->light_level = 0;  /* level 5 for sensor sensitivity */
+
+       if (ls_switch_handle)
+               set_light_sens_switch(hotk->light_switch);
+
+       if (ls_level_handle)
+               set_light_sens_level(hotk->light_level);
+
       end:
        if (result) {
                kfree(hotk->name);
@@ -903,7 +983,7 @@ static int asus_hotk_remove(struct acpi_device *device, int type)
 
 static void asus_backlight_exit(void)
 {
-       if(asus_backlight_device)
+       if (asus_backlight_device)
                backlight_device_unregister(asus_backlight_device);
 }
 
@@ -928,21 +1008,19 @@ static void __exit asus_laptop_exit(void)
        asus_led_exit();
 
        acpi_bus_unregister_driver(&asus_hotk_driver);
-        sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
-        platform_device_unregister(asuspf_device);
-        platform_driver_unregister(&asuspf_driver);
-
-       kfree(asus_info);
+       sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
+       platform_device_unregister(asuspf_device);
+       platform_driver_unregister(&asuspf_driver);
 }
 
-static int asus_backlight_init(struct device * dev)
+static int asus_backlight_init(struct device *dev)
 {
        struct backlight_device *bd;
 
-       if(brightness_set_handle && lcd_switch_handle) {
-               bd = backlight_device_register (ASUS_HOTK_FILE, dev,
-                                               NULL, &asusbl_data);
-               if (IS_ERR (bd)) {
+       if (brightness_set_handle && lcd_switch_handle) {
+               bd = backlight_device_register(ASUS_HOTK_FILE, dev,
+                                              NULL, &asusbl_data);
+               if (IS_ERR(bd)) {
                        printk(ASUS_ERR
                               "Could not register asus backlight device\n");
                        asus_backlight_device = NULL;
@@ -952,11 +1030,11 @@ static int asus_backlight_init(struct device * dev)
                asus_backlight_device = bd;
 
                down(&bd->sem);
-               if(likely(bd->props)) {
+               if (likely(bd->props)) {
                        bd->props->brightness = read_brightness(NULL);
                        bd->props->power = FB_BLANK_UNBLANK;
-                       if(likely(bd->props->update_status))
-                          bd->props->update_status(bd);
+                       if (likely(bd->props->update_status))
+                               bd->props->update_status(bd);
                }
                up(&bd->sem);
        }
@@ -964,39 +1042,39 @@ static int asus_backlight_init(struct device * dev)
 }
 
 static int asus_led_register(acpi_handle handle,
-                            struct led_classdev * ldev,
-                            struct device * dev)
+                            struct led_classdev *ldev, struct device *dev)
 {
-       if(!handle)
+       if (!handle)
                return 0;
 
        return led_classdev_register(dev, ldev);
 }
+
 #define ASUS_LED_REGISTER(object, device)                              \
        asus_led_register(object##_set_handle, &object##_led, device)
 
-static int asus_led_init(struct device * dev)
+static int asus_led_init(struct device *dev)
 {
        int rv;
 
        rv = ASUS_LED_REGISTER(mled, dev);
-       if(rv)
+       if (rv)
                return rv;
 
        rv = ASUS_LED_REGISTER(tled, dev);
-       if(rv)
+       if (rv)
                return rv;
 
        rv = ASUS_LED_REGISTER(rled, dev);
-       if(rv)
+       if (rv)
                return rv;
 
        rv = ASUS_LED_REGISTER(pled, dev);
-       if(rv)
+       if (rv)
                return rv;
 
        led_workqueue = create_singlethread_workqueue("led_workqueue");
-       if(!led_workqueue)
+       if (!led_workqueue)
                return -ENOMEM;
 
        return 0;
@@ -1034,51 +1112,51 @@ static int __init asus_laptop_init(void)
        dev = acpi_get_physical_device(hotk->device->handle);
 
        result = asus_backlight_init(dev);
-       if(result)
+       if (result)
                goto fail_backlight;
 
        result = asus_led_init(dev);
-       if(result)
+       if (result)
                goto fail_led;
 
-        /* Register platform stuff */
+       /* Register platform stuff */
        result = platform_driver_register(&asuspf_driver);
-        if (result)
-                goto fail_platform_driver;
+       if (result)
+               goto fail_platform_driver;
 
-        asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
-        if (!asuspf_device) {
-                result = -ENOMEM;
-                goto fail_platform_device1;
-        }
+       asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
+       if (!asuspf_device) {
+               result = -ENOMEM;
+               goto fail_platform_device1;
+       }
 
-        result = platform_device_add(asuspf_device);
-        if (result)
-                goto fail_platform_device2;
+       result = platform_device_add(asuspf_device);
+       if (result)
+               goto fail_platform_device2;
 
-        result = sysfs_create_group(&asuspf_device->dev.kobj,
+       result = sysfs_create_group(&asuspf_device->dev.kobj,
                                    &asuspf_attribute_group);
-        if (result)
-                goto fail_sysfs;
+       if (result)
+               goto fail_sysfs;
 
-        return 0;
+       return 0;
 
-fail_sysfs:
-        platform_device_del(asuspf_device);
+      fail_sysfs:
+       platform_device_del(asuspf_device);
 
-fail_platform_device2:
+      fail_platform_device2:
        platform_device_put(asuspf_device);
 
-fail_platform_device1:
-        platform_driver_unregister(&asuspf_driver);
+      fail_platform_device1:
+       platform_driver_unregister(&asuspf_driver);
 
-fail_platform_driver:
+      fail_platform_driver:
        asus_led_exit();
 
-fail_led:
+      fail_led:
        asus_backlight_exit();
 
-fail_backlight:
+      fail_backlight:
 
        return result;
 }