]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/hwmon/applesmc.c
hwmon: applesmc: allow for variable ALV0 and ALV1 package length
[linux-2.6-omap-h63xx.git] / drivers / hwmon / applesmc.c
index b06b8e090a278e9164c8603d7fa1d311be17317e..de696b67a4fcb71d49cf7f53839f50eb56d86023 100644 (file)
@@ -57,8 +57,8 @@
 
 #define KEY_COUNT_KEY          "#KEY" /* r-o ui32 */
 
-#define LIGHT_SENSOR_LEFT_KEY  "ALV0" /* r-o {alv (6 bytes) */
-#define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6 bytes) */
+#define LIGHT_SENSOR_LEFT_KEY  "ALV0" /* r-o {alv (6-10 bytes) */
+#define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6-10 bytes) */
 #define BACKLIGHT_KEY          "LKSB" /* w-o {lkb (2 bytes) */
 
 #define CLAMSHELL_KEY          "MSLD" /* r-o ui8 (unused) */
@@ -163,7 +163,7 @@ static unsigned int key_at_index;
 static struct workqueue_struct *applesmc_led_wq;
 
 /*
- * __wait_status - Wait up to 2ms for the status port to get a certain value
+ * __wait_status - Wait up to 10ms for the status port to get a certain value
  * (masked with 0x0f), returning zero if the value is obtained.  Callers must
  * hold applesmc_lock.
  */
@@ -173,7 +173,7 @@ static int __wait_status(u8 val)
 
        val = val & APPLESMC_STATUS_MASK;
 
-       for (i = 0; i < 200; i++) {
+       for (i = 0; i < 1000; i++) {
                if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
                        if (debug)
                                printk(KERN_DEBUG
@@ -190,6 +190,26 @@ static int __wait_status(u8 val)
        return -EIO;
 }
 
+/*
+ * special treatment of command port - on newer macbooks, it seems necessary
+ * to resend the command byte before polling the status again. Callers must
+ * hold applesmc_lock.
+ */
+static int send_command(u8 cmd)
+{
+       int i;
+       for (i = 0; i < 1000; i++) {
+               outb(cmd, APPLESMC_CMD_PORT);
+               udelay(5);
+               if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c)
+                       return 0;
+               udelay(5);
+       }
+       printk(KERN_WARNING "applesmc: command failed: %x -> %x\n",
+               cmd, inb(APPLESMC_CMD_PORT));
+       return -EIO;
+}
+
 /*
  * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
  * Returns zero on success or a negative error on failure. Callers must
@@ -205,8 +225,7 @@ static int applesmc_read_key(const char* key, u8* buffer, u8 len)
                return -EINVAL;
        }
 
-       outb(APPLESMC_READ_CMD, APPLESMC_CMD_PORT);
-       if (__wait_status(0x0c))
+       if (send_command(APPLESMC_READ_CMD))
                return -EIO;
 
        for (i = 0; i < 4; i++) {
@@ -249,8 +268,7 @@ static int applesmc_write_key(const char* key, u8* buffer, u8 len)
                return -EINVAL;
        }
 
-       outb(APPLESMC_WRITE_CMD, APPLESMC_CMD_PORT);
-       if (__wait_status(0x0c))
+       if (send_command(APPLESMC_WRITE_CMD))
                return -EIO;
 
        for (i = 0; i < 4; i++) {
@@ -284,8 +302,7 @@ static int applesmc_get_key_at_index(int index, char* key)
        readkey[2] = index >> 8;
        readkey[3] = index;
 
-       outb(APPLESMC_GET_KEY_BY_INDEX_CMD, APPLESMC_CMD_PORT);
-       if (__wait_status(0x0c))
+       if (send_command(APPLESMC_GET_KEY_BY_INDEX_CMD))
                return -EIO;
 
        for (i = 0; i < 4; i++) {
@@ -315,8 +332,7 @@ static int applesmc_get_key_type(char* key, char* type)
 {
        int i;
 
-       outb(APPLESMC_GET_KEY_TYPE_CMD, APPLESMC_CMD_PORT);
-       if (__wait_status(0x0c))
+       if (send_command(APPLESMC_GET_KEY_TYPE_CMD))
                return -EIO;
 
        for (i = 0; i < 4; i++) {
@@ -325,7 +341,7 @@ static int applesmc_get_key_type(char* key, char* type)
                        return -EIO;
        }
 
-       outb(5, APPLESMC_DATA_PORT);
+       outb(6, APPLESMC_DATA_PORT);
 
        for (i = 0; i < 6; i++) {
                if (__wait_status(0x05))
@@ -527,17 +543,27 @@ out:
 static ssize_t applesmc_light_show(struct device *dev,
                                struct device_attribute *attr, char *sysfsbuf)
 {
+       static int data_length;
        int ret;
        u8 left = 0, right = 0;
-       u8 buffer[6];
+       u8 buffer[10], query[6];
 
        mutex_lock(&applesmc_lock);
 
-       ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, 6);
+       if (!data_length) {
+               ret = applesmc_get_key_type(LIGHT_SENSOR_LEFT_KEY, query);
+               if (ret)
+                       goto out;
+               data_length = clamp_val(query[0], 0, 10);
+               printk(KERN_INFO "applesmc: light sensor data length set to "
+                       "%d\n", data_length);
+       }
+
+       ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
        left = buffer[2];
        if (ret)
                goto out;
-       ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, 6);
+       ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
        right = buffer[2];
 
 out: