USB: fix ratelimit call semantics
[safe/jmp/linux-2.6] / drivers / hwmon / hdaps.c
index 4c56411..e0cf5e6 100644 (file)
@@ -4,9 +4,9 @@
  * Copyright (C) 2005 Robert Love <rml@novell.com>
  * Copyright (C) 2005 Jesper Juhl <jesper.juhl@gmail.com>
  *
- * The HardDisk Active Protection System (hdaps) is present in the IBM ThinkPad
- * T41, T42, T43, R50, R50p, R51, and X40, at least.  It provides a basic
- * two-axis accelerometer and other data, such as the device's temperature.
+ * The HardDisk Active Protection System (hdaps) is present in IBM ThinkPads
+ * starting with the R40, T41, and X40.  It provides a basic two-axis
+ * accelerometer and other data, such as the device's temperature.
  *
  * This driver is based on the document by Mark A. Smith available at
  * http://www.almaden.ibm.com/cs/people/marksmith/tpaps.html and a lot of trial
  */
 
 #include <linux/delay.h>
-#include <linux/device.h>
+#include <linux/platform_device.h>
 #include <linux/input.h>
 #include <linux/kernel.h>
+#include <linux/mutex.h>
 #include <linux/module.h>
 #include <linux/timer.h>
 #include <linux/dmi.h>
+#include <linux/jiffies.h>
+
 #include <asm/io.h>
 
 #define HDAPS_LOW_PORT         0x1600  /* first port used by hdaps */
@@ -41,7 +44,7 @@
 #define HDAPS_PORT_STATE       0x1611  /* device state */
 #define HDAPS_PORT_YPOS                0x1612  /* y-axis position */
 #define        HDAPS_PORT_XPOS         0x1614  /* x-axis position */
-#define HDAPS_PORT_TEMP1       0x1616  /* device temperature, in celcius */
+#define HDAPS_PORT_TEMP1       0x1616  /* device temperature, in Celsius */
 #define HDAPS_PORT_YVAR                0x1617  /* y-axis variance (what is this?) */
 #define HDAPS_PORT_XVAR                0x1619  /* x-axis variance (what is this?) */
 #define HDAPS_PORT_TEMP2       0x161b  /* device temperature (again?) */
 
 #define HDAPS_POLL_PERIOD      (HZ/20) /* poll for input every 1/20s */
 #define HDAPS_INPUT_FUZZ       4       /* input event threshold */
+#define HDAPS_INPUT_FLAT       4
 
 static struct timer_list hdaps_timer;
 static struct platform_device *pdev;
+static struct input_dev *hdaps_idev;
 static unsigned int hdaps_invert;
 static u8 km_activity;
 static int rest_x;
 static int rest_y;
 
-static DECLARE_MUTEX(hdaps_sem);
+static DEFINE_MUTEX(hdaps_mtx);
 
 /*
- * __get_latch - Get the value from a given port.  Callers must hold hdaps_sem.
+ * __get_latch - Get the value from a given port.  Callers must hold hdaps_mtx.
  */
 static inline u8 __get_latch(u16 port)
 {
@@ -80,7 +85,7 @@ static inline u8 __get_latch(u16 port)
 
 /*
  * __check_latch - Check a port latch for a given value.  Returns zero if the
- * port contains the given value.  Callers must hold hdaps_sem.
+ * port contains the given value.  Callers must hold hdaps_mtx.
  */
 static inline int __check_latch(u16 port, u8 val)
 {
@@ -91,7 +96,7 @@ static inline int __check_latch(u16 port, u8 val)
 
 /*
  * __wait_latch - Wait up to 100us for a port latch to get a certain value,
- * returning zero if the value is obtained.  Callers must hold hdaps_sem.
+ * returning zero if the value is obtained.  Callers must hold hdaps_mtx.
  */
 static int __wait_latch(u16 port, u8 val)
 {
@@ -108,7 +113,7 @@ static int __wait_latch(u16 port, u8 val)
 
 /*
  * __device_refresh - request a refresh from the accelerometer.  Does not wait
- * for refresh to complete.  Callers must hold hdaps_sem.
+ * for refresh to complete.  Callers must hold hdaps_mtx.
  */
 static void __device_refresh(void)
 {
@@ -122,7 +127,7 @@ static void __device_refresh(void)
 /*
  * __device_refresh_sync - request a synchronous refresh from the
  * accelerometer.  We wait for the refresh to complete.  Returns zero if
- * successful and nonzero on error.  Callers must hold hdaps_sem.
+ * successful and nonzero on error.  Callers must hold hdaps_mtx.
  */
 static int __device_refresh_sync(void)
 {
@@ -132,7 +137,7 @@ static int __device_refresh_sync(void)
 
 /*
  * __device_complete - indicate to the accelerometer that we are done reading
- * data, and then initiate an async refresh.  Callers must hold hdaps_sem.
+ * data, and then initiate an async refresh.  Callers must hold hdaps_mtx.
  */
 static inline void __device_complete(void)
 {
@@ -150,7 +155,7 @@ static int hdaps_readb_one(unsigned int port, u8 *val)
 {
        int ret;
 
-       down(&hdaps_sem);
+       mutex_lock(&hdaps_mtx);
 
        /* do a sync refresh -- we need to be sure that we read fresh data */
        ret = __device_refresh_sync();
@@ -161,7 +166,7 @@ static int hdaps_readb_one(unsigned int port, u8 *val)
        __device_complete();
 
 out:
-       up(&hdaps_sem);
+       mutex_unlock(&hdaps_mtx);
        return ret;
 }
 
@@ -196,9 +201,9 @@ static int hdaps_read_pair(unsigned int port1, unsigned int port2,
 {
        int ret;
 
-       down(&hdaps_sem);
+       mutex_lock(&hdaps_mtx);
        ret = __hdaps_read_pair(port1, port2, val1, val2);
-       up(&hdaps_sem);
+       mutex_unlock(&hdaps_mtx);
 
        return ret;
 }
@@ -211,7 +216,7 @@ static int hdaps_device_init(void)
 {
        int total, ret = -ENXIO;
 
-       down(&hdaps_sem);
+       mutex_lock(&hdaps_mtx);
 
        outb(0x13, 0x1610);
        outb(0x01, 0x161f);
@@ -277,14 +282,14 @@ static int hdaps_device_init(void)
        }
 
 out:
-       up(&hdaps_sem);
+       mutex_unlock(&hdaps_mtx);
        return ret;
 }
 
 
 /* Device model stuff */
 
-static int hdaps_probe(struct device *dev)
+static int hdaps_probe(struct platform_device *dev)
 {
        int ret;
 
@@ -296,35 +301,22 @@ static int hdaps_probe(struct device *dev)
        return 0;
 }
 
-static int hdaps_resume(struct device *dev, u32 level)
+static int hdaps_resume(struct platform_device *dev)
 {
-       if (level == RESUME_ENABLE)
-               return hdaps_device_init();
-       return 0;
+       return hdaps_device_init();
 }
 
-static struct device_driver hdaps_driver = {
-       .name = "hdaps",
-       .bus = &platform_bus_type,
-       .owner = THIS_MODULE,
+static struct platform_driver hdaps_driver = {
        .probe = hdaps_probe,
-       .resume = hdaps_resume
-};
-
-/* Input class stuff */
-
-static struct input_dev hdaps_idev = {
-       .name = "hdaps",
-       .evbit = { BIT(EV_ABS) },
-       .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
-       .absmin  = { [ABS_X] = -256, [ABS_Y] = -256 },
-       .absmax  = { [ABS_X] = 256, [ABS_Y] = 256 },
-       .absfuzz = { [ABS_X] = HDAPS_INPUT_FUZZ, [ABS_Y] = HDAPS_INPUT_FUZZ },
-       .absflat = { [ABS_X] = HDAPS_INPUT_FUZZ, [ABS_Y] = HDAPS_INPUT_FUZZ },
+       .resume = hdaps_resume,
+       .driver = {
+               .name = "hdaps",
+               .owner = THIS_MODULE,
+       },
 };
 
 /*
- * hdaps_calibrate - Set our "resting" values.  Callers must hold hdaps_sem.
+ * hdaps_calibrate - Set our "resting" values.  Callers must hold hdaps_mtx.
  */
 static void hdaps_calibrate(void)
 {
@@ -336,7 +328,7 @@ static void hdaps_mousedev_poll(unsigned long unused)
        int x, y;
 
        /* Cannot sleep.  Try nonblockingly.  If we fail, try again later. */
-       if (down_trylock(&hdaps_sem)) {
+       if (mutex_trylock(&hdaps_mtx)) {
                mod_timer(&hdaps_timer,jiffies + HDAPS_POLL_PERIOD);
                return;
        }
@@ -344,14 +336,14 @@ static void hdaps_mousedev_poll(unsigned long unused)
        if (__hdaps_read_pair(HDAPS_PORT_XPOS, HDAPS_PORT_YPOS, &x, &y))
                goto out;
 
-       input_report_abs(&hdaps_idev, ABS_X, x - rest_x);
-       input_report_abs(&hdaps_idev, ABS_Y, y - rest_y);
-       input_sync(&hdaps_idev);
+       input_report_abs(hdaps_idev, ABS_X, x - rest_x);
+       input_report_abs(hdaps_idev, ABS_Y, y - rest_y);
+       input_sync(hdaps_idev);
 
        mod_timer(&hdaps_timer, jiffies + HDAPS_POLL_PERIOD);
 
 out:
-       up(&hdaps_sem);
+       mutex_unlock(&hdaps_mtx);
 }
 
 
@@ -431,9 +423,9 @@ static ssize_t hdaps_calibrate_store(struct device *dev,
                                     struct device_attribute *attr,
                                     const char *buf, size_t count)
 {
-       down(&hdaps_sem);
+       mutex_lock(&hdaps_mtx);
        hdaps_calibrate();
-       up(&hdaps_sem);
+       mutex_unlock(&hdaps_mtx);
 
        return count;
 }
@@ -487,66 +479,68 @@ static struct attribute_group hdaps_attribute_group = {
 
 /* Module stuff */
 
-/*
- * XXX: We should be able to return nonzero and halt the detection process.
- * But there is a bug in dmi_check_system() where a nonzero return from the
- * first match will result in a return of failure from dmi_check_system().
- * I fixed this; the patch is 2.6-git.  Once in a released tree, we can make
- * hdaps_dmi_match_invert() return hdaps_dmi_match(), which in turn returns 1.
- */
-static int hdaps_dmi_match(struct dmi_system_id *id)
+/* hdaps_dmi_match - found a match.  return one, short-circuiting the hunt. */
+static int __init hdaps_dmi_match(struct dmi_system_id *id)
 {
        printk(KERN_INFO "hdaps: %s detected.\n", id->ident);
-       return 0;
+       return 1;
 }
 
-static int hdaps_dmi_match_invert(struct dmi_system_id *id)
+/* hdaps_dmi_match_invert - found an inverted match. */
+static int __init hdaps_dmi_match_invert(struct dmi_system_id *id)
 {
        hdaps_invert = 1;
        printk(KERN_INFO "hdaps: inverting axis readings.\n");
-       return 0;
+       return hdaps_dmi_match(id);
 }
 
-#define HDAPS_DMI_MATCH_NORMAL(model)  {               \
-       .ident = "IBM " model,                          \
+#define HDAPS_DMI_MATCH_NORMAL(vendor, model) {                \
+       .ident = vendor " " model,                      \
        .callback = hdaps_dmi_match,                    \
        .matches = {                                    \
-               DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),     \
+               DMI_MATCH(DMI_BOARD_VENDOR, vendor),    \
                DMI_MATCH(DMI_PRODUCT_VERSION, model)   \
        }                                               \
 }
 
-#define HDAPS_DMI_MATCH_INVERT(model)  {               \
-       .ident = "IBM " model,                          \
+#define HDAPS_DMI_MATCH_INVERT(vendor, model) {                \
+       .ident = vendor " " model,                      \
        .callback = hdaps_dmi_match_invert,             \
        .matches = {                                    \
-               DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),     \
+               DMI_MATCH(DMI_BOARD_VENDOR, vendor),    \
                DMI_MATCH(DMI_PRODUCT_VERSION, model)   \
        }                                               \
 }
 
+/* Note that HDAPS_DMI_MATCH_NORMAL("ThinkPad T42") would match
+   "ThinkPad T42p", so the order of the entries matters.
+   If your ThinkPad is not recognized, please update to latest
+   BIOS. This is especially the case for some R52 ThinkPads. */
+static struct dmi_system_id __initdata hdaps_whitelist[] = {
+       HDAPS_DMI_MATCH_INVERT("IBM", "ThinkPad R50p"),
+       HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad R50"),
+       HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad R51"),
+       HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad R52"),
+       HDAPS_DMI_MATCH_INVERT("IBM", "ThinkPad T41p"),
+       HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad T41"),
+       HDAPS_DMI_MATCH_INVERT("IBM", "ThinkPad T42p"),
+       HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad T42"),
+       HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad T43"),
+       HDAPS_DMI_MATCH_INVERT("LENOVO", "ThinkPad T60"),
+       HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad X40"),
+       HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad X41"),
+       HDAPS_DMI_MATCH_INVERT("LENOVO", "ThinkPad X60"),
+       HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad Z60m"),
+       { .ident = NULL }
+};
+
 static int __init hdaps_init(void)
 {
        int ret;
 
-       /* Note that DMI_MATCH(...,"ThinkPad T42") will match "ThinkPad T42p" */
-       struct dmi_system_id hdaps_whitelist[] = {
-               HDAPS_DMI_MATCH_INVERT("ThinkPad R50p"),
-               HDAPS_DMI_MATCH_NORMAL("ThinkPad R50"),
-               HDAPS_DMI_MATCH_NORMAL("ThinkPad R51"),
-               HDAPS_DMI_MATCH_INVERT("ThinkPad T41p"),
-               HDAPS_DMI_MATCH_NORMAL("ThinkPad T41"),
-               HDAPS_DMI_MATCH_INVERT("ThinkPad T42p"),
-               HDAPS_DMI_MATCH_NORMAL("ThinkPad T42"),
-               HDAPS_DMI_MATCH_NORMAL("ThinkPad T43"),
-               HDAPS_DMI_MATCH_NORMAL("ThinkPad X40"),
-               HDAPS_DMI_MATCH_NORMAL("ThinkPad X41 Tablet"),
-               { .ident = NULL }
-       };
-
        if (!dmi_check_system(hdaps_whitelist)) {
                printk(KERN_WARNING "hdaps: supported laptop not found!\n");
-               ret = -ENXIO;
+               ret = -ENODEV;
                goto out;
        }
 
@@ -555,7 +549,7 @@ static int __init hdaps_init(void)
                goto out;
        }
 
-       ret = driver_register(&hdaps_driver);
+       ret = platform_driver_register(&hdaps_driver);
        if (ret)
                goto out_region;
 
@@ -569,12 +563,27 @@ static int __init hdaps_init(void)
        if (ret)
                goto out_device;
 
+       hdaps_idev = input_allocate_device();
+       if (!hdaps_idev) {
+               ret = -ENOMEM;
+               goto out_group;
+       }
+
        /* initial calibrate for the input device */
        hdaps_calibrate();
 
        /* initialize the input class */
-       hdaps_idev.dev = &pdev->dev;
-       input_register_device(&hdaps_idev);
+       hdaps_idev->name = "hdaps";
+       hdaps_idev->dev.parent = &pdev->dev;
+       hdaps_idev->evbit[0] = BIT(EV_ABS);
+       input_set_abs_params(hdaps_idev, ABS_X,
+                       -256, 256, HDAPS_INPUT_FUZZ, HDAPS_INPUT_FLAT);
+       input_set_abs_params(hdaps_idev, ABS_Y,
+                       -256, 256, HDAPS_INPUT_FUZZ, HDAPS_INPUT_FLAT);
+
+       ret = input_register_device(hdaps_idev);
+       if (ret)
+               goto out_idev;
 
        /* start up our timer for the input device */
        init_timer(&hdaps_timer);
@@ -585,10 +594,14 @@ static int __init hdaps_init(void)
        printk(KERN_INFO "hdaps: driver successfully loaded.\n");
        return 0;
 
+out_idev:
+       input_free_device(hdaps_idev);
+out_group:
+       sysfs_remove_group(&pdev->dev.kobj, &hdaps_attribute_group);
 out_device:
        platform_device_unregister(pdev);
 out_driver:
-       driver_unregister(&hdaps_driver);
+       platform_driver_unregister(&hdaps_driver);
 out_region:
        release_region(HDAPS_LOW_PORT, HDAPS_NR_PORTS);
 out:
@@ -599,10 +612,10 @@ out:
 static void __exit hdaps_exit(void)
 {
        del_timer_sync(&hdaps_timer);
-       input_unregister_device(&hdaps_idev);
+       input_unregister_device(hdaps_idev);
        sysfs_remove_group(&pdev->dev.kobj, &hdaps_attribute_group);
        platform_device_unregister(pdev);
-       driver_unregister(&hdaps_driver);
+       platform_driver_unregister(&hdaps_driver);
        release_region(HDAPS_LOW_PORT, HDAPS_NR_PORTS);
 
        printk(KERN_INFO "hdaps: driver unloaded.\n");