Power Supply: fix race in device_create
[safe/jmp/linux-2.6] / drivers / misc / asus-laptop.c
index b39419c..7c6dfd0 100644 (file)
@@ -33,7 +33,6 @@
  *  Sam Lin        - GPS support
  */
 
-#include <linux/autoconf.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/init.h>
 #include <acpi/acpi_bus.h>
 #include <asm/uaccess.h>
 
-#define ASUS_LAPTOP_VERSION "0.41"
+#define ASUS_LAPTOP_VERSION "0.42"
 
 #define ASUS_HOTK_NAME          "Asus Laptop Support"
 #define ASUS_HOTK_CLASS         "hotkey"
 #define ASUS_HOTK_DEVICE_NAME   "Hotkey"
-#define ASUS_HOTK_HID           "ATK0100"
 #define ASUS_HOTK_FILE          "asus-laptop"
 #define ASUS_HOTK_PREFIX        "\\_SB.ATKD."
 
@@ -83,7 +81,7 @@
 #define PLED_ON     0x20       //Phone LED
 #define GLED_ON     0x40       //Gaming LED
 #define LCD_ON      0x80       //LCD backlight
-#define GPS_ON      0x100       //GPS
+#define GPS_ON      0x100      //GPS
 
 #define ASUS_LOG    ASUS_HOTK_FILE ": "
 #define ASUS_ERR    KERN_ERR    ASUS_LOG
@@ -149,7 +147,7 @@ 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
-                                          S5A M5A z33A W1Jc W2V */
+                                          S5A M5A z33A W1Jc W2V G1 */
            "\\_SB.PCI0.P0P3.VGA.GETD", /* A6V A6Q */
            "\\_SB.PCI0.P0PA.VGA.GETD", /* A6T, A6M */
            "\\_SB.PCI0.PCI1.VGAC.NMAP",        /* L3C */
@@ -165,8 +163,8 @@ ASUS_HANDLE(ls_level, ASUS_HOTK_PREFIX "ALSL");     /* Z71A Z71V */
 
 /* GPS */
 /* R2H use different handle for GPS on/off */
-ASUS_HANDLE(gps_on, ASUS_HOTK_PREFIX "SDON"); /* R2H */
-ASUS_HANDLE(gps_off, ASUS_HOTK_PREFIX "SDOF"); /* R2H */
+ASUS_HANDLE(gps_on, ASUS_HOTK_PREFIX "SDON");  /* R2H */
+ASUS_HANDLE(gps_off, ASUS_HOTK_PREFIX "SDOF"); /* R2H */
 ASUS_HANDLE(gps_status, ASUS_HOTK_PREFIX "GPST");
 
 /*
@@ -197,12 +195,18 @@ static struct asus_hotk *hotk;
 /*
  * The hotkey driver declaration
  */
+static const struct acpi_device_id asus_device_ids[] = {
+       {"ATK0100", 0},
+       {"", 0},
+};
+MODULE_DEVICE_TABLE(acpi, asus_device_ids);
+
 static int asus_hotk_add(struct acpi_device *device);
 static int asus_hotk_remove(struct acpi_device *device, int type);
 static struct acpi_driver asus_hotk_driver = {
        .name = ASUS_HOTK_NAME,
        .class = ASUS_HOTK_CLASS,
-       .ids = ASUS_HOTK_HID,
+       .ids = asus_device_ids,
        .ops = {
                .add = asus_hotk_add,
                .remove = asus_hotk_remove,
@@ -235,7 +239,7 @@ static struct workqueue_struct *led_workqueue;
        static int object##_led_wk;                                     \
        static DECLARE_WORK(object##_led_work, object##_led_update);    \
        static struct led_classdev object##_led = {                     \
-               .name           = "asus:" ledname,                      \
+               .name           = "asus::" ledname,                     \
                .brightness_set = object##_led_set,                     \
        }
 
@@ -250,7 +254,7 @@ ASUS_LED(gled, "gaming");
  * method is searched within the scope of the handle, can be NULL. The output
  * of the method is written is output, which can also be NULL
  *
- * returns 1 if write is successful, 0 else.
+ * returns 0 if write is successful, -1 else.
  */
 static int write_acpi_int(acpi_handle handle, const char *method, int val,
                          struct acpi_buffer *output)
@@ -259,13 +263,19 @@ static int write_acpi_int(acpi_handle handle, const char *method, int val,
        union acpi_object in_obj;       //the only param we use
        acpi_status status;
 
+       if (!handle)
+               return 0;
+
        params.count = 1;
        params.pointer = &in_obj;
        in_obj.type = ACPI_TYPE_INTEGER;
        in_obj.integer.value = val;
 
        status = acpi_evaluate_object(handle, (char *)method, &params, output);
-       return (status == AE_OK);
+       if (status == AE_OK)
+               return 0;
+       else
+               return -1;
 }
 
 static int read_wireless_status(int mask)
@@ -285,17 +295,18 @@ static int read_wireless_status(int mask)
        return (hotk->status & mask) ? 1 : 0;
 }
 
-static int read_gps_status(void) {
-        ulong status;
+static int read_gps_status(void)
+{
+       ulong status;
        acpi_status rv = AE_OK;
 
        rv = acpi_evaluate_integer(gps_status_handle, NULL, NULL, &status);
        if (ACPI_FAILURE(rv))
-                printk(ASUS_WARNING "Error reading GPS status\n");
+               printk(ASUS_WARNING "Error reading GPS status\n");
        else
-                return status ? 1 : 0;
+               return status ? 1 : 0;
 
-        return (hotk->status & GPS_ON) ? 1 : 0;
+       return (hotk->status & GPS_ON) ? 1 : 0;
 }
 
 /* Generic LED functions */
@@ -304,8 +315,8 @@ static int read_status(int mask)
        /* There is a special method for both wireless devices */
        if (mask == BT_ON || mask == WL_ON)
                return read_wireless_status(mask);
-       else if(mask == GPS_ON)
-                return read_gps_status();
+       else if (mask == GPS_ON)
+               return read_gps_status();
 
        return (hotk->status & mask) ? 1 : 0;
 }
@@ -316,7 +327,7 @@ static void write_status(acpi_handle handle, int out, int mask)
 
        switch (mask) {
        case MLED_ON:
-               out = !out & 0x1;
+               out = !(out & 0x1);
                break;
        case GLED_ON:
                out = (out & 0x1) + 1;
@@ -330,7 +341,7 @@ static void write_status(acpi_handle handle, int out, int mask)
                break;
        }
 
-       if (handle && !write_acpi_int(handle, NULL, out, NULL))
+       if (write_acpi_int(handle, NULL, out, NULL))
                printk(ASUS_WARNING " write failed %x\n", mask);
 }
 
@@ -410,7 +421,7 @@ static int set_brightness(struct backlight_device *bd, int value)
        value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
        /* 0 <= value <= 15 */
 
-       if (!write_acpi_int(brightness_set_handle, NULL, value, NULL)) {
+       if (write_acpi_int(brightness_set_handle, NULL, value, NULL)) {
                printk(ASUS_WARNING "Error changing brightness\n");
                ret = -EIO;
        }
@@ -540,7 +551,7 @@ static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
 
        rv = parse_arg(buf, count, &value);
        if (rv > 0) {
-               if (!write_acpi_int(ledd_set_handle, NULL, value, NULL))
+               if (write_acpi_int(ledd_set_handle, NULL, value, NULL))
                        printk(ASUS_WARNING "LED display write failed\n");
                else
                        hotk->ledd_status = (u32) value;
@@ -585,7 +596,7 @@ static ssize_t store_bluetooth(struct device *dev,
 static void set_display(int value)
 {
        /* no sanity check needed for now */
-       if (!write_acpi_int(display_set_handle, NULL, value, NULL))
+       if (write_acpi_int(display_set_handle, NULL, value, NULL))
                printk(ASUS_WARNING "Error setting display\n");
        return;
 }
@@ -642,7 +653,7 @@ static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
  */
 static void set_light_sens_switch(int value)
 {
-       if (!write_acpi_int(ls_switch_handle, NULL, value, NULL))
+       if (write_acpi_int(ls_switch_handle, NULL, value, NULL))
                printk(ASUS_WARNING "Error setting light sensor switch\n");
        hotk->light_switch = value;
 }
@@ -667,7 +678,7 @@ static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
 
 static void set_light_sens_level(int value)
 {
-       if (!write_acpi_int(ls_level_handle, NULL, value, NULL))
+       if (write_acpi_int(ls_level_handle, NULL, value, NULL))
                printk(ASUS_WARNING "Error setting light sensor level\n");
        hotk->light_level = value;
 }
@@ -705,7 +716,7 @@ static ssize_t show_gps(struct device *dev,
 static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
                         const char *buf, size_t count)
 {
-        return store_status(buf, count, NULL, GPS_ON);
+       return store_status(buf, count, NULL, GPS_ON);
 }
 
 static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
@@ -726,7 +737,7 @@ static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
                lcd_blank(FB_BLANK_POWERDOWN);
        }
 
-       acpi_bus_generate_event(hotk->device, event,
+       acpi_bus_generate_proc_event(hotk->device, event,
                                hotk->event_count[event % 128]++);
 
        return;
@@ -736,8 +747,7 @@ static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
        struct device_attribute dev_attr_##_name = {                    \
                .attr = {                                               \
                        .name = __stringify(_name),                     \
-                       .mode = 0,                                      \
-                       .owner = THIS_MODULE },                         \
+                       .mode = 0 },                                    \
                .show   = NULL,                                         \
                .store  = NULL,                                         \
        }
@@ -807,7 +817,7 @@ static void asus_hotk_add_fs(void)
                ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
        }
 
-       if(gps_status_handle && gps_on_handle && gps_off_handle)
+       if (gps_status_handle && gps_on_handle && gps_off_handle)
                ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
 }
 
@@ -856,7 +866,7 @@ static int asus_hotk_get_info(void)
                printk(ASUS_WARNING "Couldn't get the DSDT table header\n");
 
        /* We have to write 0 on init this far for all ASUS models */
-       if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
+       if (write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
                printk(ASUS_ERR "Hotkey initialization failed\n");
                return -ENODEV;
        }
@@ -979,10 +989,9 @@ static int asus_hotk_add(struct acpi_device *device)
        printk(ASUS_NOTICE "Asus Laptop Support version %s\n",
               ASUS_LAPTOP_VERSION);
 
-       hotk = kmalloc(sizeof(struct asus_hotk), GFP_KERNEL);
+       hotk = kzalloc(sizeof(struct asus_hotk), GFP_KERNEL);
        if (!hotk)
                return -ENOMEM;
-       memset(hotk, 0, sizeof(struct asus_hotk));
 
        hotk->handle = device->handle;
        strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
@@ -1068,19 +1077,17 @@ static void asus_backlight_exit(void)
 }
 
 #define  ASUS_LED_UNREGISTER(object)                           \
-       if(object##_led.class_dev                               \
-          && !IS_ERR(object##_led.class_dev))                  \
+       if (object##_led.dev)                                   \
                led_classdev_unregister(&object##_led)
 
 static void asus_led_exit(void)
 {
+       destroy_workqueue(led_workqueue);
        ASUS_LED_UNREGISTER(mled);
        ASUS_LED_UNREGISTER(tled);
        ASUS_LED_UNREGISTER(pled);
        ASUS_LED_UNREGISTER(rled);
        ASUS_LED_UNREGISTER(gled);
-
-       destroy_workqueue(led_workqueue);
 }
 
 static void __exit asus_laptop_exit(void)
@@ -1136,29 +1143,42 @@ static int asus_led_init(struct device *dev)
 
        rv = ASUS_LED_REGISTER(mled, dev);
        if (rv)
-               return rv;
+               goto out;
 
        rv = ASUS_LED_REGISTER(tled, dev);
        if (rv)
-               return rv;
+               goto out1;
 
        rv = ASUS_LED_REGISTER(rled, dev);
        if (rv)
-               return rv;
+               goto out2;
 
        rv = ASUS_LED_REGISTER(pled, dev);
        if (rv)
-               return rv;
+               goto out3;
 
        rv = ASUS_LED_REGISTER(gled, dev);
        if (rv)
-               return rv;
+               goto out4;
 
        led_workqueue = create_singlethread_workqueue("led_workqueue");
        if (!led_workqueue)
-               return -ENOMEM;
+               goto out5;
 
        return 0;
+out5:
+       rv = -ENOMEM;
+       ASUS_LED_UNREGISTER(gled);
+out4:
+       ASUS_LED_UNREGISTER(pled);
+out3:
+       ASUS_LED_UNREGISTER(rled);
+out2:
+       ASUS_LED_UNREGISTER(tled);
+out1:
+       ASUS_LED_UNREGISTER(mled);
+out:
+       return rv;
 }
 
 static int __init asus_laptop_init(void)