Merge master.kernel.org:/pub/scm/linux/kernel/git/steve/gfs2-2.6-nmw
[pandora-kernel.git] / drivers / hwmon / abituguru.c
index bf2cb0a..b1dc63e 100644 (file)
     etc voltage & frequency control is not supported!
 */
 #include <linux/module.h>
+#include <linux/sched.h>
 #include <linux/init.h>
 #include <linux/slab.h>
 #include <linux/jiffies.h>
 #include <linux/mutex.h>
 #include <linux/err.h>
+#include <linux/delay.h>
 #include <linux/platform_device.h>
 #include <linux/hwmon.h>
 #include <linux/hwmon-sysfs.h>
 #define ABIT_UGURU_SENSOR_BANK1                        0x21 /* 16x volt and temp */
 #define ABIT_UGURU_FAN_PWM                     0x24 /* 3x 5 bytes */
 #define ABIT_UGURU_SENSOR_BANK2                        0x26 /* fans */
+/* max nr of sensors in bank1, a bank1 sensor can be in, temp or nc */
+#define ABIT_UGURU_MAX_BANK1_SENSORS           16
+/* Warning if you increase one of the 2 MAX defines below to 10 or higher you
+   should adjust the belonging _NAMES_LENGTH macro for the 2 digit number! */
 /* max nr of sensors in bank2, currently mb's with max 6 fans are known */
 #define ABIT_UGURU_MAX_BANK2_SENSORS           6
 /* max nr of pwm outputs, currently mb's with max 5 pwm outputs are known */
 #define ABIT_UGURU_IN_SENSOR                   0
 #define ABIT_UGURU_TEMP_SENSOR                 1
 #define ABIT_UGURU_NC                          2
-/* Timeouts / Retries, if these turn out to need a lot of fiddling we could
-   convert them to params. */
-/* 250 was determined by trial and error, 200 works most of the time, but not
-   always. I assume this is cpu-speed independent, since the ISA-bus and not
-   the CPU should be the bottleneck. Note that 250 sometimes is still not
-   enough (only reported on AN7 mb) this is handled by a higher layer. */
-#define ABIT_UGURU_WAIT_TIMEOUT                        250
+/* In many cases we need to wait for the uGuru to reach a certain status, most
+   of the time it will reach this status within 30 - 90 ISA reads, and thus we
+   can best busy wait. This define gives the total amount of reads to try. */
+#define ABIT_UGURU_WAIT_TIMEOUT                        125
+/* However sometimes older versions of the uGuru seem to be distracted and they
+   do not respond for a long time. To handle this we sleep before each of the
+   last ABIT_UGURU_WAIT_TIMEOUT_SLEEP tries. */
+#define ABIT_UGURU_WAIT_TIMEOUT_SLEEP          5
 /* Normally all expected status in abituguru_ready, are reported after the
-   first read, but sometimes not and we need to poll, 5 polls was not enough
-   50 sofar is. */
-#define ABIT_UGURU_READY_TIMEOUT               50
+   first read, but sometimes not and we need to poll. */
+#define ABIT_UGURU_READY_TIMEOUT               5
 /* Maximum 3 retries on timedout reads/writes, delay 200 ms before retrying */
 #define ABIT_UGURU_MAX_RETRIES                 3
 #define ABIT_UGURU_RETRY_DELAY                 (HZ/5)
-/* Maximum 2 timeouts in abituguru_update_device, iow 3 in a row is a error */
+/* Maximum 2 timeouts in abituguru_update_device, iow 3 in a row is an error */
 #define ABIT_UGURU_MAX_TIMEOUTS                        2
-
-/* All the variables below are named identical to the oguru and oguru2 programs
+/* utility macros */
+#define ABIT_UGURU_NAME                                "abituguru"
+#define ABIT_UGURU_DEBUG(level, format, arg...)                                \
+       if (level <= verbose)                                           \
+               printk(KERN_DEBUG ABIT_UGURU_NAME ": "  format , ## arg)
+/* Macros to help calculate the sysfs_names array length */
+/* sum of strlen of: in??_input\0, in??_{min,max}\0, in??_{min,max}_alarm\0,
+   in??_{min,max}_alarm_enable\0, in??_beep\0, in??_shutdown\0 */
+#define ABITUGURU_IN_NAMES_LENGTH      (11 + 2 * 9 + 2 * 15 + 2 * 22 + 10 + 14)
+/* sum of strlen of: temp??_input\0, temp??_max\0, temp??_crit\0,
+   temp??_alarm\0, temp??_alarm_enable\0, temp??_beep\0, temp??_shutdown\0 */
+#define ABITUGURU_TEMP_NAMES_LENGTH    (13 + 11 + 12 + 13 + 20 + 12 + 16)
+/* sum of strlen of: fan?_input\0, fan?_min\0, fan?_alarm\0,
+   fan?_alarm_enable\0, fan?_beep\0, fan?_shutdown\0 */
+#define ABITUGURU_FAN_NAMES_LENGTH     (11 + 9 + 11 + 18 + 10 + 14)
+/* sum of strlen of: pwm?_enable\0, pwm?_auto_channels_temp\0,
+   pwm?_auto_point{1,2}_pwm\0, pwm?_auto_point{1,2}_temp\0 */
+#define ABITUGURU_PWM_NAMES_LENGTH     (12 + 24 + 2 * 21 + 2 * 22)
+/* IN_NAMES_LENGTH > TEMP_NAMES_LENGTH so assume all bank1 sensors are in */
+#define ABITUGURU_SYSFS_NAMES_LENGTH   ( \
+       ABIT_UGURU_MAX_BANK1_SENSORS * ABITUGURU_IN_NAMES_LENGTH + \
+       ABIT_UGURU_MAX_BANK2_SENSORS * ABITUGURU_FAN_NAMES_LENGTH + \
+       ABIT_UGURU_MAX_PWMS * ABITUGURU_PWM_NAMES_LENGTH)
+
+/* All the macros below are named identical to the oguru and oguru2 programs
    reverse engineered by Olle Sandberg, hence the names might not be 100%
    logical. I could come up with better names, but I prefer keeping the names
    identical so that this driver can be compared with his work more easily. */
 #define ABIT_UGURU_STATUS_READ                 0x01 /* Ready to be read */
 #define ABIT_UGURU_STATUS_INPUT                        0x08 /* More input */
 #define ABIT_UGURU_STATUS_READY                        0x09 /* Ready to be written */
-/* utility macros */
-#define ABIT_UGURU_NAME                                "abituguru"
-#define ABIT_UGURU_DEBUG(level, format, arg...)                                \
-       if (level <= verbose)                                           \
-               printk(KERN_DEBUG ABIT_UGURU_NAME ": "  format , ## arg)
 
 /* Constants */
 /* in (Volt) sensors go up to 3494 mV, temp to 255000 millidegrees Celsius */
@@ -120,6 +144,14 @@ static const u8 abituguru_pwm_max[5] = { 0, 255, 255, 75, 75 };
 static int force;
 module_param(force, bool, 0);
 MODULE_PARM_DESC(force, "Set to one to force detection.");
+static int bank1_types[ABIT_UGURU_MAX_BANK1_SENSORS] = { -1, -1, -1, -1, -1,
+       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
+module_param_array(bank1_types, int, NULL, 0);
+MODULE_PARM_DESC(bank1_types, "Bank1 sensortype autodetection override:\n"
+       "   -1 autodetect\n"
+       "    0 volt sensor\n"
+       "    1 temp sensor\n"
+       "    2 not connected");
 static int fan_sensors;
 module_param(fan_sensors, int, 0);
 MODULE_PARM_DESC(fan_sensors, "Number of fan sensors on the uGuru "
@@ -156,24 +188,23 @@ struct abituguru_data {
           of a sensor is a volt or a temp sensor, for bank2 and the pwms its
           easier todo things the same way.  For in sensors we have 9 (temp 7)
           sysfs entries per sensor, for bank2 and pwms 6. */
-       struct sensor_device_attribute_2 sysfs_attr[16 * 9 +
+       struct sensor_device_attribute_2 sysfs_attr[
+               ABIT_UGURU_MAX_BANK1_SENSORS * 9 +
                ABIT_UGURU_MAX_BANK2_SENSORS * 6 + ABIT_UGURU_MAX_PWMS * 6];
-       /* Buffer to store the dynamically generated sysfs names, we need 2120
-          bytes for bank1 (worst case scenario of 16 in sensors), 444 bytes
-          for fan1-6 and 738 bytes for pwm1-6 + some room to spare in case I
-          miscounted :) */
-       char bank1_names[3400];
+       /* Buffer to store the dynamically generated sysfs names */
+       char sysfs_names[ABITUGURU_SYSFS_NAMES_LENGTH];
 
        /* Bank 1 data */
-       u8 bank1_sensors[2];    /* number of [0] in, [1] temp sensors */
-       u8 bank1_address[2][16];/* addresses of [0] in, [1] temp sensors */
-       u8 bank1_value[16];
-       /* This array holds 16 x 3 entries for all the bank 1 sensor settings
+       /* number of and addresses of [0] in, [1] temp sensors */
+       u8 bank1_sensors[2];
+       u8 bank1_address[2][ABIT_UGURU_MAX_BANK1_SENSORS];
+       u8 bank1_value[ABIT_UGURU_MAX_BANK1_SENSORS];
+       /* This array holds 3 entries per sensor for the bank 1 sensor settings
           (flags, min, max for voltage / flags, warn, shutdown for temp). */
-       u8 bank1_settings[16][3];
+       u8 bank1_settings[ABIT_UGURU_MAX_BANK1_SENSORS][3];
        /* Maximum value for each sensor used for scaling in mV/millidegrees
           Celsius. */
-       int bank1_max_value[16];
+       int bank1_max_value[ABIT_UGURU_MAX_BANK1_SENSORS];
 
        /* Bank 2 data, ABIT_UGURU_MAX_BANK2_SENSORS entries for bank2 */
        u8 bank2_sensors; /* actual number of bank2 sensors found */
@@ -197,6 +228,10 @@ static int abituguru_wait(struct abituguru_data *data, u8 state)
                timeout--;
                if (timeout == 0)
                        return -EBUSY;
+               /* sleep a bit before our last few tries, see the comment on
+                  this where ABIT_UGURU_WAIT_TIMEOUT_SLEEP is defined. */
+               if (timeout <= ABIT_UGURU_WAIT_TIMEOUT_SLEEP)
+                       msleep(0);
        }
        return 0;
 }
@@ -227,6 +262,7 @@ static int abituguru_ready(struct abituguru_data *data)
                           "CMD reg does not hold 0xAC after ready command\n");
                        return -EIO;
                }
+               msleep(0);
        }
 
        /* After this the ABIT_UGURU_DATA port should contain
@@ -239,6 +275,7 @@ static int abituguru_ready(struct abituguru_data *data)
                                "state != more input after ready command\n");
                        return -EIO;
                }
+               msleep(0);
        }
 
        data->uguru_ready = 1;
@@ -302,7 +339,8 @@ static int abituguru_read(struct abituguru_data *data,
        /* And read the data */
        for (i = 0; i < count; i++) {
                if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
-                       ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for "
+                       ABIT_UGURU_DEBUG(retries ? 1 : 3,
+                               "timeout exceeded waiting for "
                                "read state (bank: %d, sensor: %d)\n",
                                (int)bank_addr, (int)sensor_addr);
                        break;
@@ -321,7 +359,9 @@ static int abituguru_read(struct abituguru_data *data,
 static int abituguru_write(struct abituguru_data *data,
        u8 bank_addr, u8 sensor_addr, u8 *buf, int count)
 {
-       int i;
+       /* We use the ready timeout as we have to wait for 0xAC just like the
+          ready function */
+       int i, timeout = ABIT_UGURU_READY_TIMEOUT;
 
        /* Send the address */
        i = abituguru_send_address(data, bank_addr, sensor_addr,
@@ -341,7 +381,8 @@ static int abituguru_write(struct abituguru_data *data,
        }
 
        /* Now we need to wait till the chip is ready to be read again,
-          don't ask why */
+          so that we can read 0xAC as confirmation that our write has
+          succeeded. */
        if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
                ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for read state "
                        "after write (bank: %d, sensor: %d)\n", (int)bank_addr,
@@ -350,11 +391,15 @@ static int abituguru_write(struct abituguru_data *data,
        }
 
        /* Cmd port MUST be read now and should contain 0xAC */
-       if (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
-               ABIT_UGURU_DEBUG(1, "CMD reg does not hold 0xAC after write "
-                       "(bank: %d, sensor: %d)\n", (int)bank_addr,
-                       (int)sensor_addr);
-               return -EIO;
+       while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
+               timeout--;
+               if (timeout == 0) {
+                       ABIT_UGURU_DEBUG(1, "CMD reg does not hold 0xAC after "
+                               "write (bank: %d, sensor: %d)\n",
+                               (int)bank_addr, (int)sensor_addr);
+                       return -EIO;
+               }
+               msleep(0);
        }
 
        /* Last put the chip back in ready state */
@@ -374,12 +419,21 @@ abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
                                   u8 sensor_addr)
 {
        u8 val, buf[3];
-       int ret = ABIT_UGURU_NC;
+       int i, ret = -ENODEV; /* error is the most common used retval :| */
+
+       /* If overriden by the user return the user selected type */
+       if (bank1_types[sensor_addr] >= ABIT_UGURU_IN_SENSOR &&
+                       bank1_types[sensor_addr] <= ABIT_UGURU_NC) {
+               ABIT_UGURU_DEBUG(2, "assuming sensor type %d for bank1 sensor "
+                       "%d because of \"bank1_types\" module param\n",
+                       bank1_types[sensor_addr], (int)sensor_addr);
+               return bank1_types[sensor_addr];
+       }
 
        /* First read the sensor and the current settings */
        if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, sensor_addr, &val,
                        1, ABIT_UGURU_MAX_RETRIES) != 1)
-               return -EIO;
+               return -ENODEV;
 
        /* Test val is sane / usable for sensor type detection. */
        if ((val < 10u) || (val > 240u)) {
@@ -401,7 +455,7 @@ abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
        buf[2] = 250;
        if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
                        buf, 3) != 3)
-               return -EIO;
+               goto abituguru_detect_bank1_sensor_type_exit;
        /* Now we need 20 ms to give the uguru time to read the sensors
           and raise a voltage alarm */
        set_current_state(TASK_UNINTERRUPTIBLE);
@@ -409,21 +463,16 @@ abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
        /* Check for alarm and check the alarm is a volt low alarm. */
        if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
                        ABIT_UGURU_MAX_RETRIES) != 3)
-               return -EIO;
+               goto abituguru_detect_bank1_sensor_type_exit;
        if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
                if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
                                sensor_addr, buf, 3,
                                ABIT_UGURU_MAX_RETRIES) != 3)
-                       return -EIO;
+                       goto abituguru_detect_bank1_sensor_type_exit;
                if (buf[0] & ABIT_UGURU_VOLT_LOW_ALARM_FLAG) {
-                       /* Restore original settings */
-                       if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
-                                       sensor_addr,
-                                       data->bank1_settings[sensor_addr],
-                                       3) != 3)
-                               return -EIO;
                        ABIT_UGURU_DEBUG(2, "  found volt sensor\n");
-                       return ABIT_UGURU_IN_SENSOR;
+                       ret = ABIT_UGURU_IN_SENSOR;
+                       goto abituguru_detect_bank1_sensor_type_exit;
                } else
                        ABIT_UGURU_DEBUG(2, "  alarm raised during volt "
                                "sensor test, but volt low flag not set\n");
@@ -439,7 +488,7 @@ abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
        buf[2] = 10;
        if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
                        buf, 3) != 3)
-               return -EIO;
+               goto abituguru_detect_bank1_sensor_type_exit;
        /* Now we need 50 ms to give the uguru time to read the sensors
           and raise a temp alarm */
        set_current_state(TASK_UNINTERRUPTIBLE);
@@ -447,15 +496,16 @@ abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
        /* Check for alarm and check the alarm is a temp high alarm. */
        if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
                        ABIT_UGURU_MAX_RETRIES) != 3)
-               return -EIO;
+               goto abituguru_detect_bank1_sensor_type_exit;
        if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
                if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
                                sensor_addr, buf, 3,
                                ABIT_UGURU_MAX_RETRIES) != 3)
-                       return -EIO;
+                       goto abituguru_detect_bank1_sensor_type_exit;
                if (buf[0] & ABIT_UGURU_TEMP_HIGH_ALARM_FLAG) {
-                       ret = ABIT_UGURU_TEMP_SENSOR;
                        ABIT_UGURU_DEBUG(2, "  found temp sensor\n");
+                       ret = ABIT_UGURU_TEMP_SENSOR;
+                       goto abituguru_detect_bank1_sensor_type_exit;
                } else
                        ABIT_UGURU_DEBUG(2, "  alarm raised during temp "
                                "sensor test, but temp high flag not set\n");
@@ -463,11 +513,23 @@ abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
                ABIT_UGURU_DEBUG(2, "  alarm not raised during temp sensor "
                        "test\n");
 
-       /* Restore original settings */
-       if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
-                       data->bank1_settings[sensor_addr], 3) != 3)
-               return -EIO;
-
+       ret = ABIT_UGURU_NC;
+abituguru_detect_bank1_sensor_type_exit:
+       /* Restore original settings, failing here is really BAD, it has been
+          reported that some BIOS-es hang when entering the uGuru menu with
+          invalid settings present in the uGuru, so we try this 3 times. */
+       for (i = 0; i < 3; i++)
+               if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
+                               sensor_addr, data->bank1_settings[sensor_addr],
+                               3) == 3)
+                       break;
+       if (i == 3) {
+               printk(KERN_ERR ABIT_UGURU_NAME
+                       ": Fatal error could not restore original settings. "
+                       "This should never happen please report this to the "
+                       "abituguru maintainer (see MAINTAINERS)\n");
+               return -ENODEV;
+       }
        return ret;
 }
 
@@ -493,7 +555,7 @@ abituguru_detect_no_bank2_sensors(struct abituguru_data *data)
 {
        int i;
 
-       if (fan_sensors) {
+       if (fan_sensors > 0 && fan_sensors <= ABIT_UGURU_MAX_BANK2_SENSORS) {
                data->bank2_sensors = fan_sensors;
                ABIT_UGURU_DEBUG(2, "assuming %d fan sensors because of "
                        "\"fan_sensors\" module param\n",
@@ -508,9 +570,8 @@ abituguru_detect_no_bank2_sensors(struct abituguru_data *data)
                   -0x08 enable beep
                   -0x01 enable alarm
                   All other bits should be 0, but on some motherboards
-                  0x40 (bit 6) is also high, at least for fan1 */
-               if ((!i && (data->bank2_settings[i][0] & ~0xC9)) ||
-                    (i && (data->bank2_settings[i][0] & ~0x89))) {
+                  0x40 (bit 6) is also high for some of the fans?? */
+               if (data->bank2_settings[i][0] & ~0xC9) {
                        ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
                                "to be a fan sensor: settings[0] = %02X\n",
                                i, (unsigned int)data->bank2_settings[i][0]);
@@ -548,7 +609,7 @@ abituguru_detect_no_pwms(struct abituguru_data *data)
 {
        int i, j;
 
-       if (pwms) {
+       if (pwms > 0 && pwms <= ABIT_UGURU_MAX_PWMS) {
                data->pwms = pwms;
                ABIT_UGURU_DEBUG(2, "assuming %d PWM outputs because of "
                        "\"pwms\" module param\n", (int)data->pwms);
@@ -1061,21 +1122,21 @@ static const struct sensor_device_attribute_2 abituguru_sysfs_pwm_templ[6] = {
                store_pwm_setting, 4, 0),
 };
 
-static const struct sensor_device_attribute_2 abituguru_sysfs_attr[] = {
+static struct sensor_device_attribute_2 abituguru_sysfs_attr[] = {
        SENSOR_ATTR_2(name, 0444, show_name, NULL, 0, 0),
 };
 
 static int __devinit abituguru_probe(struct platform_device *pdev)
 {
        struct abituguru_data *data;
-       int i, j, res;
+       int i, j, used, sysfs_names_free, sysfs_attr_i, res = -ENODEV;
        char *sysfs_filename;
-       int sysfs_attr_i = 0;
 
        /* El weirdo probe order, to keep the sysfs order identical to the
           BIOS and window-appliction listing order. */
-       const u8 probe_order[16] = { 0x00, 0x01, 0x03, 0x04, 0x0A, 0x08, 0x0E,
-               0x02, 0x09, 0x06, 0x05, 0x0B, 0x0F, 0x0D, 0x07, 0x0C };
+       const u8 probe_order[ABIT_UGURU_MAX_BANK1_SENSORS] = {
+               0x00, 0x01, 0x03, 0x04, 0x0A, 0x08, 0x0E, 0x02,
+               0x09, 0x06, 0x05, 0x0B, 0x0F, 0x0D, 0x07, 0x0C };
 
        if (!(data = kzalloc(sizeof(struct abituguru_data), GFP_KERNEL)))
                return -ENOMEM;
@@ -1092,24 +1153,18 @@ static int __devinit abituguru_probe(struct platform_device *pdev)
           - testread / see if one really is there.
           - make an in memory copy of all the uguru settings for future use. */
        if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
-                       data->alarms, 3, ABIT_UGURU_MAX_RETRIES) != 3) {
-               kfree(data);
-               return -ENODEV;
-       }
+                       data->alarms, 3, ABIT_UGURU_MAX_RETRIES) != 3)
+               goto abituguru_probe_error;
 
-       for (i = 0; i < 16; i++) {
+       for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
                if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, i,
                                &data->bank1_value[i], 1,
-                               ABIT_UGURU_MAX_RETRIES) != 1) {
-                       kfree(data);
-                       return -ENODEV;
-               }
+                               ABIT_UGURU_MAX_RETRIES) != 1)
+                       goto abituguru_probe_error;
                if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1+1, i,
                                data->bank1_settings[i], 3,
-                               ABIT_UGURU_MAX_RETRIES) != 3) {
-                       kfree(data);
-                       return -ENODEV;
-               }
+                               ABIT_UGURU_MAX_RETRIES) != 3)
+                       goto abituguru_probe_error;
        }
        /* Note: We don't know how many bank2 sensors / pwms there really are,
           but in order to "detect" this we need to read the maximum amount
@@ -1119,48 +1174,45 @@ static int __devinit abituguru_probe(struct platform_device *pdev)
        for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
                if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
                                &data->bank2_value[i], 1,
-                               ABIT_UGURU_MAX_RETRIES) != 1) {
-                       kfree(data);
-                       return -ENODEV;
-               }
+                               ABIT_UGURU_MAX_RETRIES) != 1)
+                       goto abituguru_probe_error;
                if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2+1, i,
                                data->bank2_settings[i], 2,
-                               ABIT_UGURU_MAX_RETRIES) != 2) {
-                       kfree(data);
-                       return -ENODEV;
-               }
+                               ABIT_UGURU_MAX_RETRIES) != 2)
+                       goto abituguru_probe_error;
        }
        for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
                if (abituguru_read(data, ABIT_UGURU_FAN_PWM, i,
                                data->pwm_settings[i], 5,
-                               ABIT_UGURU_MAX_RETRIES) != 5) {
-                       kfree(data);
-                       return -ENODEV;
-               }
+                               ABIT_UGURU_MAX_RETRIES) != 5)
+                       goto abituguru_probe_error;
        }
        data->last_updated = jiffies;
 
        /* Detect sensor types and fill the sysfs attr for bank1 */
-       sysfs_filename = data->bank1_names;
-       for (i = 0; i < 16; i++) {
+       sysfs_attr_i = 0;
+       sysfs_filename = data->sysfs_names;
+       sysfs_names_free = ABITUGURU_SYSFS_NAMES_LENGTH;
+       for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
                res = abituguru_detect_bank1_sensor_type(data, probe_order[i]);
-               if (res < 0) {
-                       kfree(data);
-                       return -ENODEV;
-               }
+               if (res < 0)
+                       goto abituguru_probe_error;
                if (res == ABIT_UGURU_NC)
                        continue;
 
+               /* res 1 (temp) sensors have 7 sysfs entries, 0 (in) 9 */
                for (j = 0; j < (res ? 7 : 9); j++) {
-                       const char *name_templ = abituguru_sysfs_bank1_templ[
-                               res][j].dev_attr.attr.name;
+                       used = snprintf(sysfs_filename, sysfs_names_free,
+                               abituguru_sysfs_bank1_templ[res][j].dev_attr.
+                               attr.name, data->bank1_sensors[res] + res)
+                               + 1;
                        data->sysfs_attr[sysfs_attr_i] =
                                abituguru_sysfs_bank1_templ[res][j];
                        data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
                                sysfs_filename;
-                       sysfs_filename += sprintf(sysfs_filename, name_templ,
-                               data->bank1_sensors[res] + res) + 1;
                        data->sysfs_attr[sysfs_attr_i].index = probe_order[i];
+                       sysfs_filename += used;
+                       sysfs_names_free -= used;
                        sysfs_attr_i++;
                }
                data->bank1_max_value[probe_order[i]] =
@@ -1172,52 +1224,65 @@ static int __devinit abituguru_probe(struct platform_device *pdev)
        /* Detect number of sensors and fill the sysfs attr for bank2 (fans) */
        abituguru_detect_no_bank2_sensors(data);
        for (i = 0; i < data->bank2_sensors; i++) {
-               for (j = 0; j < 6; j++) {
-                       const char *name_templ = abituguru_sysfs_fan_templ[j].
-                               dev_attr.attr.name;
+               for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_fan_templ); j++) {
+                       used = snprintf(sysfs_filename, sysfs_names_free,
+                               abituguru_sysfs_fan_templ[j].dev_attr.attr.name,
+                               i + 1) + 1;
                        data->sysfs_attr[sysfs_attr_i] =
                                abituguru_sysfs_fan_templ[j];
                        data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
                                sysfs_filename;
-                       sysfs_filename += sprintf(sysfs_filename, name_templ,
-                               i + 1) + 1;
                        data->sysfs_attr[sysfs_attr_i].index = i;
+                       sysfs_filename += used;
+                       sysfs_names_free -= used;
                        sysfs_attr_i++;
                }
        }
        /* Detect number of sensors and fill the sysfs attr for pwms */
        abituguru_detect_no_pwms(data);
        for (i = 0; i < data->pwms; i++) {
-               for (j = 0; j < 6; j++) {
-                       const char *name_templ = abituguru_sysfs_pwm_templ[j].
-                               dev_attr.attr.name;
+               for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_pwm_templ); j++) {
+                       used = snprintf(sysfs_filename, sysfs_names_free,
+                               abituguru_sysfs_pwm_templ[j].dev_attr.attr.name,
+                               i + 1) + 1;
                        data->sysfs_attr[sysfs_attr_i] =
                                abituguru_sysfs_pwm_templ[j];
                        data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
                                sysfs_filename;
-                       sysfs_filename += sprintf(sysfs_filename, name_templ,
-                               i + 1) + 1;
                        data->sysfs_attr[sysfs_attr_i].index = i;
+                       sysfs_filename += used;
+                       sysfs_names_free -= used;
                        sysfs_attr_i++;
                }
        }
-       /* Last add any "generic" entries to sysfs */
-       for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++) {
-               data->sysfs_attr[sysfs_attr_i] = abituguru_sysfs_attr[i];
-               sysfs_attr_i++;
+       /* Fail safe check, this should never happen! */
+       if (sysfs_names_free < 0) {
+               printk(KERN_ERR ABIT_UGURU_NAME ": Fatal error ran out of "
+                      "space for sysfs attr names. This should never "
+                      "happen please report to the abituguru maintainer "
+                      "(see MAINTAINERS)\n");
+               res = -ENAMETOOLONG;
+               goto abituguru_probe_error;
        }
        printk(KERN_INFO ABIT_UGURU_NAME ": found Abit uGuru\n");
 
        /* Register sysfs hooks */
        data->class_dev = hwmon_device_register(&pdev->dev);
        if (IS_ERR(data->class_dev)) {
-               kfree(data);
-               return PTR_ERR(data->class_dev);
+               res = PTR_ERR(data->class_dev);
+               goto abituguru_probe_error;
        }
        for (i = 0; i < sysfs_attr_i; i++)
                device_create_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
+       for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
+               device_create_file(&pdev->dev,
+                       &abituguru_sysfs_attr[i].dev_attr);
 
        return 0;
+
+abituguru_probe_error:
+       kfree(data);
+       return res;
 }
 
 static int __devexit abituguru_remove(struct platform_device *pdev)
@@ -1244,7 +1309,7 @@ static struct abituguru_data *abituguru_update_device(struct device *dev)
                if ((err = abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
                                data->alarms, 3, 0)) != 3)
                        goto LEAVE_UPDATE;
-               for (i = 0; i < 16; i++) {
+               for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
                        if ((err = abituguru_read(data,
                                        ABIT_UGURU_SENSOR_BANK1, i,
                                        &data->bank1_value[i], 1, 0)) != 1)
@@ -1264,7 +1329,7 @@ static struct abituguru_data *abituguru_update_device(struct device *dev)
                data->update_timeouts = 0;
 LEAVE_UPDATE:
                /* handle timeout condition */
-               if (err == -EBUSY) {
+               if (!success && (err == -EBUSY || err >= 0)) {
                        /* No overflow please */
                        if (data->update_timeouts < 255u)
                                data->update_timeouts++;
@@ -1290,13 +1355,39 @@ LEAVE_UPDATE:
                return NULL;
 }
 
+#ifdef CONFIG_PM
+static int abituguru_suspend(struct platform_device *pdev, pm_message_t state)
+{
+       struct abituguru_data *data = platform_get_drvdata(pdev);
+       /* make sure all communications with the uguru are done and no new
+          ones are started */
+       mutex_lock(&data->update_lock);
+       return 0;
+}
+
+static int abituguru_resume(struct platform_device *pdev)
+{
+       struct abituguru_data *data = platform_get_drvdata(pdev);
+       /* See if the uGuru is still ready */
+       if (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT)
+               data->uguru_ready = 0;
+       mutex_unlock(&data->update_lock);
+       return 0;
+}
+#else
+#define abituguru_suspend      NULL
+#define abituguru_resume       NULL
+#endif /* CONFIG_PM */
+
 static struct platform_driver abituguru_driver = {
        .driver = {
                .owner  = THIS_MODULE,
                .name   = ABIT_UGURU_NAME,
        },
-       .probe  = abituguru_probe,
-       .remove = __devexit_p(abituguru_remove),
+       .probe          = abituguru_probe,
+       .remove         = __devexit_p(abituguru_remove),
+       .suspend        = abituguru_suspend,
+       .resume         = abituguru_resume,
 };
 
 static int __init abituguru_detect(void)