Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * abituguru.c Copyright (c) 2005-2006 Hans de Goede <hdegoede@redhat.com>
0004  */
0005 /*
0006  * This driver supports the sensor part of the first and second revision of
0007  * the custom Abit uGuru chip found on Abit uGuru motherboards. Note: because
0008  * of lack of specs the CPU/RAM voltage & frequency control is not supported!
0009  */
0010 
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012 
0013 #include <linux/module.h>
0014 #include <linux/sched.h>
0015 #include <linux/init.h>
0016 #include <linux/slab.h>
0017 #include <linux/jiffies.h>
0018 #include <linux/mutex.h>
0019 #include <linux/err.h>
0020 #include <linux/delay.h>
0021 #include <linux/platform_device.h>
0022 #include <linux/hwmon.h>
0023 #include <linux/hwmon-sysfs.h>
0024 #include <linux/dmi.h>
0025 #include <linux/io.h>
0026 
0027 /* Banks */
0028 #define ABIT_UGURU_ALARM_BANK           0x20 /* 1x 3 bytes */
0029 #define ABIT_UGURU_SENSOR_BANK1         0x21 /* 16x volt and temp */
0030 #define ABIT_UGURU_FAN_PWM          0x24 /* 3x 5 bytes */
0031 #define ABIT_UGURU_SENSOR_BANK2         0x26 /* fans */
0032 /* max nr of sensors in bank1, a bank1 sensor can be in, temp or nc */
0033 #define ABIT_UGURU_MAX_BANK1_SENSORS        16
0034 /*
0035  * Warning if you increase one of the 2 MAX defines below to 10 or higher you
0036  * should adjust the belonging _NAMES_LENGTH macro for the 2 digit number!
0037  */
0038 /* max nr of sensors in bank2, currently mb's with max 6 fans are known */
0039 #define ABIT_UGURU_MAX_BANK2_SENSORS        6
0040 /* max nr of pwm outputs, currently mb's with max 5 pwm outputs are known */
0041 #define ABIT_UGURU_MAX_PWMS         5
0042 /* uGuru sensor bank 1 flags */              /* Alarm if: */
0043 #define ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE   0x01 /*  temp over warn */
0044 #define ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE   0x02 /*  volt over max */
0045 #define ABIT_UGURU_VOLT_LOW_ALARM_ENABLE    0x04 /*  volt under min */
0046 #define ABIT_UGURU_TEMP_HIGH_ALARM_FLAG     0x10 /* temp is over warn */
0047 #define ABIT_UGURU_VOLT_HIGH_ALARM_FLAG     0x20 /* volt is over max */
0048 #define ABIT_UGURU_VOLT_LOW_ALARM_FLAG      0x40 /* volt is under min */
0049 /* uGuru sensor bank 2 flags */              /* Alarm if: */
0050 #define ABIT_UGURU_FAN_LOW_ALARM_ENABLE     0x01 /*   fan under min */
0051 /* uGuru sensor bank common flags */
0052 #define ABIT_UGURU_BEEP_ENABLE          0x08 /* beep if alarm */
0053 #define ABIT_UGURU_SHUTDOWN_ENABLE      0x80 /* shutdown if alarm */
0054 /* uGuru fan PWM (speed control) flags */
0055 #define ABIT_UGURU_FAN_PWM_ENABLE       0x80 /* enable speed control */
0056 /* Values used for conversion */
0057 #define ABIT_UGURU_FAN_MAX          15300 /* RPM */
0058 /* Bank1 sensor types */
0059 #define ABIT_UGURU_IN_SENSOR            0
0060 #define ABIT_UGURU_TEMP_SENSOR          1
0061 #define ABIT_UGURU_NC               2
0062 /*
0063  * In many cases we need to wait for the uGuru to reach a certain status, most
0064  * of the time it will reach this status within 30 - 90 ISA reads, and thus we
0065  * can best busy wait. This define gives the total amount of reads to try.
0066  */
0067 #define ABIT_UGURU_WAIT_TIMEOUT         125
0068 /*
0069  * However sometimes older versions of the uGuru seem to be distracted and they
0070  * do not respond for a long time. To handle this we sleep before each of the
0071  * last ABIT_UGURU_WAIT_TIMEOUT_SLEEP tries.
0072  */
0073 #define ABIT_UGURU_WAIT_TIMEOUT_SLEEP       5
0074 /*
0075  * Normally all expected status in abituguru_ready, are reported after the
0076  * first read, but sometimes not and we need to poll.
0077  */
0078 #define ABIT_UGURU_READY_TIMEOUT        5
0079 /* Maximum 3 retries on timedout reads/writes, delay 200 ms before retrying */
0080 #define ABIT_UGURU_MAX_RETRIES          3
0081 #define ABIT_UGURU_RETRY_DELAY          (HZ/5)
0082 /* Maximum 2 timeouts in abituguru_update_device, iow 3 in a row is an error */
0083 #define ABIT_UGURU_MAX_TIMEOUTS         2
0084 /* utility macros */
0085 #define ABIT_UGURU_NAME             "abituguru"
0086 #define ABIT_UGURU_DEBUG(level, format, arg...)     \
0087     do {                        \
0088         if (level <= verbose)           \
0089             pr_debug(format , ## arg);  \
0090     } while (0)
0091 
0092 /* Macros to help calculate the sysfs_names array length */
0093 /*
0094  * sum of strlen of: in??_input\0, in??_{min,max}\0, in??_{min,max}_alarm\0,
0095  * in??_{min,max}_alarm_enable\0, in??_beep\0, in??_shutdown\0
0096  */
0097 #define ABITUGURU_IN_NAMES_LENGTH   (11 + 2 * 9 + 2 * 15 + 2 * 22 + 10 + 14)
0098 /*
0099  * sum of strlen of: temp??_input\0, temp??_max\0, temp??_crit\0,
0100  * temp??_alarm\0, temp??_alarm_enable\0, temp??_beep\0, temp??_shutdown\0
0101  */
0102 #define ABITUGURU_TEMP_NAMES_LENGTH (13 + 11 + 12 + 13 + 20 + 12 + 16)
0103 /*
0104  * sum of strlen of: fan?_input\0, fan?_min\0, fan?_alarm\0,
0105  * fan?_alarm_enable\0, fan?_beep\0, fan?_shutdown\0
0106  */
0107 #define ABITUGURU_FAN_NAMES_LENGTH  (11 + 9 + 11 + 18 + 10 + 14)
0108 /*
0109  * sum of strlen of: pwm?_enable\0, pwm?_auto_channels_temp\0,
0110  * pwm?_auto_point{1,2}_pwm\0, pwm?_auto_point{1,2}_temp\0
0111  */
0112 #define ABITUGURU_PWM_NAMES_LENGTH  (12 + 24 + 2 * 21 + 2 * 22)
0113 /* IN_NAMES_LENGTH > TEMP_NAMES_LENGTH so assume all bank1 sensors are in */
0114 #define ABITUGURU_SYSFS_NAMES_LENGTH    ( \
0115     ABIT_UGURU_MAX_BANK1_SENSORS * ABITUGURU_IN_NAMES_LENGTH + \
0116     ABIT_UGURU_MAX_BANK2_SENSORS * ABITUGURU_FAN_NAMES_LENGTH + \
0117     ABIT_UGURU_MAX_PWMS * ABITUGURU_PWM_NAMES_LENGTH)
0118 
0119 /*
0120  * All the macros below are named identical to the oguru and oguru2 programs
0121  * reverse engineered by Olle Sandberg, hence the names might not be 100%
0122  * logical. I could come up with better names, but I prefer keeping the names
0123  * identical so that this driver can be compared with his work more easily.
0124  */
0125 /* Two i/o-ports are used by uGuru */
0126 #define ABIT_UGURU_BASE             0x00E0
0127 /* Used to tell uGuru what to read and to read the actual data */
0128 #define ABIT_UGURU_CMD              0x00
0129 /* Mostly used to check if uGuru is busy */
0130 #define ABIT_UGURU_DATA             0x04
0131 #define ABIT_UGURU_REGION_LENGTH        5
0132 /* uGuru status' */
0133 #define ABIT_UGURU_STATUS_WRITE         0x00 /* Ready to be written */
0134 #define ABIT_UGURU_STATUS_READ          0x01 /* Ready to be read */
0135 #define ABIT_UGURU_STATUS_INPUT         0x08 /* More input */
0136 #define ABIT_UGURU_STATUS_READY         0x09 /* Ready to be written */
0137 
0138 /* Constants */
0139 /* in (Volt) sensors go up to 3494 mV, temp to 255000 millidegrees Celsius */
0140 static const int abituguru_bank1_max_value[2] = { 3494, 255000 };
0141 /*
0142  * Min / Max allowed values for sensor2 (fan) alarm threshold, these values
0143  * correspond to 300-3000 RPM
0144  */
0145 static const u8 abituguru_bank2_min_threshold = 5;
0146 static const u8 abituguru_bank2_max_threshold = 50;
0147 /*
0148  * Register 0 is a bitfield, 1 and 2 are pwm settings (255 = 100%), 3 and 4
0149  * are temperature trip points.
0150  */
0151 static const int abituguru_pwm_settings_multiplier[5] = { 0, 1, 1, 1000, 1000 };
0152 /*
0153  * Min / Max allowed values for pwm_settings. Note: pwm1 (CPU fan) is a
0154  * special case the minimum allowed pwm% setting for this is 30% (77) on
0155  * some MB's this special case is handled in the code!
0156  */
0157 static const u8 abituguru_pwm_min[5] = { 0, 170, 170, 25, 25 };
0158 static const u8 abituguru_pwm_max[5] = { 0, 255, 255, 75, 75 };
0159 
0160 
0161 /* Insmod parameters */
0162 static bool force;
0163 module_param(force, bool, 0);
0164 MODULE_PARM_DESC(force, "Set to one to force detection.");
0165 static int bank1_types[ABIT_UGURU_MAX_BANK1_SENSORS] = { -1, -1, -1, -1, -1,
0166     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
0167 module_param_array(bank1_types, int, NULL, 0);
0168 MODULE_PARM_DESC(bank1_types, "Bank1 sensortype autodetection override:\n"
0169     "   -1 autodetect\n"
0170     "    0 volt sensor\n"
0171     "    1 temp sensor\n"
0172     "    2 not connected");
0173 static int fan_sensors;
0174 module_param(fan_sensors, int, 0);
0175 MODULE_PARM_DESC(fan_sensors, "Number of fan sensors on the uGuru "
0176     "(0 = autodetect)");
0177 static int pwms;
0178 module_param(pwms, int, 0);
0179 MODULE_PARM_DESC(pwms, "Number of PWMs on the uGuru "
0180     "(0 = autodetect)");
0181 
0182 /* Default verbose is 2, since this driver is still in the testing phase */
0183 static int verbose = 2;
0184 module_param(verbose, int, 0644);
0185 MODULE_PARM_DESC(verbose, "How verbose should the driver be? (0-3):\n"
0186     "   0 normal output\n"
0187     "   1 + verbose error reporting\n"
0188     "   2 + sensors type probing info\n"
0189     "   3 + retryable error reporting");
0190 
0191 
0192 /*
0193  * For the Abit uGuru, we need to keep some data in memory.
0194  * The structure is dynamically allocated, at the same time when a new
0195  * abituguru device is allocated.
0196  */
0197 struct abituguru_data {
0198     struct device *hwmon_dev;   /* hwmon registered device */
0199     struct mutex update_lock;   /* protect access to data and uGuru */
0200     unsigned long last_updated; /* In jiffies */
0201     unsigned short addr;        /* uguru base address */
0202     char uguru_ready;       /* is the uguru in ready state? */
0203     unsigned char update_timeouts;  /*
0204                      * number of update timeouts since last
0205                      * successful update
0206                      */
0207 
0208     /*
0209      * The sysfs attr and their names are generated automatically, for bank1
0210      * we cannot use a predefined array because we don't know beforehand
0211      * of a sensor is a volt or a temp sensor, for bank2 and the pwms its
0212      * easier todo things the same way.  For in sensors we have 9 (temp 7)
0213      * sysfs entries per sensor, for bank2 and pwms 6.
0214      */
0215     struct sensor_device_attribute_2 sysfs_attr[
0216         ABIT_UGURU_MAX_BANK1_SENSORS * 9 +
0217         ABIT_UGURU_MAX_BANK2_SENSORS * 6 + ABIT_UGURU_MAX_PWMS * 6];
0218     /* Buffer to store the dynamically generated sysfs names */
0219     char sysfs_names[ABITUGURU_SYSFS_NAMES_LENGTH];
0220 
0221     /* Bank 1 data */
0222     /* number of and addresses of [0] in, [1] temp sensors */
0223     u8 bank1_sensors[2];
0224     u8 bank1_address[2][ABIT_UGURU_MAX_BANK1_SENSORS];
0225     u8 bank1_value[ABIT_UGURU_MAX_BANK1_SENSORS];
0226     /*
0227      * This array holds 3 entries per sensor for the bank 1 sensor settings
0228      * (flags, min, max for voltage / flags, warn, shutdown for temp).
0229      */
0230     u8 bank1_settings[ABIT_UGURU_MAX_BANK1_SENSORS][3];
0231     /*
0232      * Maximum value for each sensor used for scaling in mV/millidegrees
0233      * Celsius.
0234      */
0235     int bank1_max_value[ABIT_UGURU_MAX_BANK1_SENSORS];
0236 
0237     /* Bank 2 data, ABIT_UGURU_MAX_BANK2_SENSORS entries for bank2 */
0238     u8 bank2_sensors; /* actual number of bank2 sensors found */
0239     u8 bank2_value[ABIT_UGURU_MAX_BANK2_SENSORS];
0240     u8 bank2_settings[ABIT_UGURU_MAX_BANK2_SENSORS][2]; /* flags, min */
0241 
0242     /* Alarms 2 bytes for bank1, 1 byte for bank2 */
0243     u8 alarms[3];
0244 
0245     /* Fan PWM (speed control) 5 bytes per PWM */
0246     u8 pwms; /* actual number of pwms found */
0247     u8 pwm_settings[ABIT_UGURU_MAX_PWMS][5];
0248 };
0249 
0250 static const char *never_happen = "This should never happen.";
0251 static const char *report_this =
0252     "Please report this to the abituguru maintainer (see MAINTAINERS)";
0253 
0254 /* wait till the uguru is in the specified state */
0255 static int abituguru_wait(struct abituguru_data *data, u8 state)
0256 {
0257     int timeout = ABIT_UGURU_WAIT_TIMEOUT;
0258 
0259     while (inb_p(data->addr + ABIT_UGURU_DATA) != state) {
0260         timeout--;
0261         if (timeout == 0)
0262             return -EBUSY;
0263         /*
0264          * sleep a bit before our last few tries, see the comment on
0265          * this where ABIT_UGURU_WAIT_TIMEOUT_SLEEP is defined.
0266          */
0267         if (timeout <= ABIT_UGURU_WAIT_TIMEOUT_SLEEP)
0268             msleep(0);
0269     }
0270     return 0;
0271 }
0272 
0273 /* Put the uguru in ready for input state */
0274 static int abituguru_ready(struct abituguru_data *data)
0275 {
0276     int timeout = ABIT_UGURU_READY_TIMEOUT;
0277 
0278     if (data->uguru_ready)
0279         return 0;
0280 
0281     /* Reset? / Prepare for next read/write cycle */
0282     outb(0x00, data->addr + ABIT_UGURU_DATA);
0283 
0284     /* Wait till the uguru is ready */
0285     if (abituguru_wait(data, ABIT_UGURU_STATUS_READY)) {
0286         ABIT_UGURU_DEBUG(1,
0287             "timeout exceeded waiting for ready state\n");
0288         return -EIO;
0289     }
0290 
0291     /* Cmd port MUST be read now and should contain 0xAC */
0292     while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
0293         timeout--;
0294         if (timeout == 0) {
0295             ABIT_UGURU_DEBUG(1,
0296                "CMD reg does not hold 0xAC after ready command\n");
0297             return -EIO;
0298         }
0299         msleep(0);
0300     }
0301 
0302     /*
0303      * After this the ABIT_UGURU_DATA port should contain
0304      * ABIT_UGURU_STATUS_INPUT
0305      */
0306     timeout = ABIT_UGURU_READY_TIMEOUT;
0307     while (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT) {
0308         timeout--;
0309         if (timeout == 0) {
0310             ABIT_UGURU_DEBUG(1,
0311                 "state != more input after ready command\n");
0312             return -EIO;
0313         }
0314         msleep(0);
0315     }
0316 
0317     data->uguru_ready = 1;
0318     return 0;
0319 }
0320 
0321 /*
0322  * Send the bank and then sensor address to the uGuru for the next read/write
0323  * cycle. This function gets called as the first part of a read/write by
0324  * abituguru_read and abituguru_write. This function should never be
0325  * called by any other function.
0326  */
0327 static int abituguru_send_address(struct abituguru_data *data,
0328     u8 bank_addr, u8 sensor_addr, int retries)
0329 {
0330     /*
0331      * assume the caller does error handling itself if it has not requested
0332      * any retries, and thus be quiet.
0333      */
0334     int report_errors = retries;
0335 
0336     for (;;) {
0337         /*
0338          * Make sure the uguru is ready and then send the bank address,
0339          * after this the uguru is no longer "ready".
0340          */
0341         if (abituguru_ready(data) != 0)
0342             return -EIO;
0343         outb(bank_addr, data->addr + ABIT_UGURU_DATA);
0344         data->uguru_ready = 0;
0345 
0346         /*
0347          * Wait till the uguru is ABIT_UGURU_STATUS_INPUT state again
0348          * and send the sensor addr
0349          */
0350         if (abituguru_wait(data, ABIT_UGURU_STATUS_INPUT)) {
0351             if (retries) {
0352                 ABIT_UGURU_DEBUG(3, "timeout exceeded "
0353                     "waiting for more input state, %d "
0354                     "tries remaining\n", retries);
0355                 set_current_state(TASK_UNINTERRUPTIBLE);
0356                 schedule_timeout(ABIT_UGURU_RETRY_DELAY);
0357                 retries--;
0358                 continue;
0359             }
0360             if (report_errors)
0361                 ABIT_UGURU_DEBUG(1, "timeout exceeded "
0362                     "waiting for more input state "
0363                     "(bank: %d)\n", (int)bank_addr);
0364             return -EBUSY;
0365         }
0366         outb(sensor_addr, data->addr + ABIT_UGURU_CMD);
0367         return 0;
0368     }
0369 }
0370 
0371 /*
0372  * Read count bytes from sensor sensor_addr in bank bank_addr and store the
0373  * result in buf, retry the send address part of the read retries times.
0374  */
0375 static int abituguru_read(struct abituguru_data *data,
0376     u8 bank_addr, u8 sensor_addr, u8 *buf, int count, int retries)
0377 {
0378     int i;
0379 
0380     /* Send the address */
0381     i = abituguru_send_address(data, bank_addr, sensor_addr, retries);
0382     if (i)
0383         return i;
0384 
0385     /* And read the data */
0386     for (i = 0; i < count; i++) {
0387         if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
0388             ABIT_UGURU_DEBUG(retries ? 1 : 3,
0389                 "timeout exceeded waiting for "
0390                 "read state (bank: %d, sensor: %d)\n",
0391                 (int)bank_addr, (int)sensor_addr);
0392             break;
0393         }
0394         buf[i] = inb(data->addr + ABIT_UGURU_CMD);
0395     }
0396 
0397     /* Last put the chip back in ready state */
0398     abituguru_ready(data);
0399 
0400     return i;
0401 }
0402 
0403 /*
0404  * Write count bytes from buf to sensor sensor_addr in bank bank_addr, the send
0405  * address part of the write is always retried ABIT_UGURU_MAX_RETRIES times.
0406  */
0407 static int abituguru_write(struct abituguru_data *data,
0408     u8 bank_addr, u8 sensor_addr, u8 *buf, int count)
0409 {
0410     /*
0411      * We use the ready timeout as we have to wait for 0xAC just like the
0412      * ready function
0413      */
0414     int i, timeout = ABIT_UGURU_READY_TIMEOUT;
0415 
0416     /* Send the address */
0417     i = abituguru_send_address(data, bank_addr, sensor_addr,
0418         ABIT_UGURU_MAX_RETRIES);
0419     if (i)
0420         return i;
0421 
0422     /* And write the data */
0423     for (i = 0; i < count; i++) {
0424         if (abituguru_wait(data, ABIT_UGURU_STATUS_WRITE)) {
0425             ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for "
0426                 "write state (bank: %d, sensor: %d)\n",
0427                 (int)bank_addr, (int)sensor_addr);
0428             break;
0429         }
0430         outb(buf[i], data->addr + ABIT_UGURU_CMD);
0431     }
0432 
0433     /*
0434      * Now we need to wait till the chip is ready to be read again,
0435      * so that we can read 0xAC as confirmation that our write has
0436      * succeeded.
0437      */
0438     if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
0439         ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for read state "
0440             "after write (bank: %d, sensor: %d)\n", (int)bank_addr,
0441             (int)sensor_addr);
0442         return -EIO;
0443     }
0444 
0445     /* Cmd port MUST be read now and should contain 0xAC */
0446     while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
0447         timeout--;
0448         if (timeout == 0) {
0449             ABIT_UGURU_DEBUG(1, "CMD reg does not hold 0xAC after "
0450                 "write (bank: %d, sensor: %d)\n",
0451                 (int)bank_addr, (int)sensor_addr);
0452             return -EIO;
0453         }
0454         msleep(0);
0455     }
0456 
0457     /* Last put the chip back in ready state */
0458     abituguru_ready(data);
0459 
0460     return i;
0461 }
0462 
0463 /*
0464  * Detect sensor type. Temp and Volt sensors are enabled with
0465  * different masks and will ignore enable masks not meant for them.
0466  * This enables us to test what kind of sensor we're dealing with.
0467  * By setting the alarm thresholds so that we will always get an
0468  * alarm for sensor type X and then enabling the sensor as sensor type
0469  * X, if we then get an alarm it is a sensor of type X.
0470  */
0471 static int
0472 abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
0473                    u8 sensor_addr)
0474 {
0475     u8 val, test_flag, buf[3];
0476     int i, ret = -ENODEV; /* error is the most common used retval :| */
0477 
0478     /* If overriden by the user return the user selected type */
0479     if (bank1_types[sensor_addr] >= ABIT_UGURU_IN_SENSOR &&
0480             bank1_types[sensor_addr] <= ABIT_UGURU_NC) {
0481         ABIT_UGURU_DEBUG(2, "assuming sensor type %d for bank1 sensor "
0482             "%d because of \"bank1_types\" module param\n",
0483             bank1_types[sensor_addr], (int)sensor_addr);
0484         return bank1_types[sensor_addr];
0485     }
0486 
0487     /* First read the sensor and the current settings */
0488     if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, sensor_addr, &val,
0489             1, ABIT_UGURU_MAX_RETRIES) != 1)
0490         return -ENODEV;
0491 
0492     /* Test val is sane / usable for sensor type detection. */
0493     if ((val < 10u) || (val > 250u)) {
0494         pr_warn("bank1-sensor: %d reading (%d) too close to limits, "
0495             "unable to determine sensor type, skipping sensor\n",
0496             (int)sensor_addr, (int)val);
0497         /*
0498          * assume no sensor is there for sensors for which we can't
0499          * determine the sensor type because their reading is too close
0500          * to their limits, this usually means no sensor is there.
0501          */
0502         return ABIT_UGURU_NC;
0503     }
0504 
0505     ABIT_UGURU_DEBUG(2, "testing bank1 sensor %d\n", (int)sensor_addr);
0506     /*
0507      * Volt sensor test, enable volt low alarm, set min value ridiculously
0508      * high, or vica versa if the reading is very high. If its a volt
0509      * sensor this should always give us an alarm.
0510      */
0511     if (val <= 240u) {
0512         buf[0] = ABIT_UGURU_VOLT_LOW_ALARM_ENABLE;
0513         buf[1] = 245;
0514         buf[2] = 250;
0515         test_flag = ABIT_UGURU_VOLT_LOW_ALARM_FLAG;
0516     } else {
0517         buf[0] = ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE;
0518         buf[1] = 5;
0519         buf[2] = 10;
0520         test_flag = ABIT_UGURU_VOLT_HIGH_ALARM_FLAG;
0521     }
0522 
0523     if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
0524             buf, 3) != 3)
0525         goto abituguru_detect_bank1_sensor_type_exit;
0526     /*
0527      * Now we need 20 ms to give the uguru time to read the sensors
0528      * and raise a voltage alarm
0529      */
0530     set_current_state(TASK_UNINTERRUPTIBLE);
0531     schedule_timeout(HZ/50);
0532     /* Check for alarm and check the alarm is a volt low alarm. */
0533     if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
0534             ABIT_UGURU_MAX_RETRIES) != 3)
0535         goto abituguru_detect_bank1_sensor_type_exit;
0536     if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
0537         if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
0538                 sensor_addr, buf, 3,
0539                 ABIT_UGURU_MAX_RETRIES) != 3)
0540             goto abituguru_detect_bank1_sensor_type_exit;
0541         if (buf[0] & test_flag) {
0542             ABIT_UGURU_DEBUG(2, "  found volt sensor\n");
0543             ret = ABIT_UGURU_IN_SENSOR;
0544             goto abituguru_detect_bank1_sensor_type_exit;
0545         } else
0546             ABIT_UGURU_DEBUG(2, "  alarm raised during volt "
0547                 "sensor test, but volt range flag not set\n");
0548     } else
0549         ABIT_UGURU_DEBUG(2, "  alarm not raised during volt sensor "
0550             "test\n");
0551 
0552     /*
0553      * Temp sensor test, enable sensor as a temp sensor, set beep value
0554      * ridiculously low (but not too low, otherwise uguru ignores it).
0555      * If its a temp sensor this should always give us an alarm.
0556      */
0557     buf[0] = ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE;
0558     buf[1] = 5;
0559     buf[2] = 10;
0560     if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
0561             buf, 3) != 3)
0562         goto abituguru_detect_bank1_sensor_type_exit;
0563     /*
0564      * Now we need 50 ms to give the uguru time to read the sensors
0565      * and raise a temp alarm
0566      */
0567     set_current_state(TASK_UNINTERRUPTIBLE);
0568     schedule_timeout(HZ/20);
0569     /* Check for alarm and check the alarm is a temp high alarm. */
0570     if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
0571             ABIT_UGURU_MAX_RETRIES) != 3)
0572         goto abituguru_detect_bank1_sensor_type_exit;
0573     if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
0574         if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
0575                 sensor_addr, buf, 3,
0576                 ABIT_UGURU_MAX_RETRIES) != 3)
0577             goto abituguru_detect_bank1_sensor_type_exit;
0578         if (buf[0] & ABIT_UGURU_TEMP_HIGH_ALARM_FLAG) {
0579             ABIT_UGURU_DEBUG(2, "  found temp sensor\n");
0580             ret = ABIT_UGURU_TEMP_SENSOR;
0581             goto abituguru_detect_bank1_sensor_type_exit;
0582         } else
0583             ABIT_UGURU_DEBUG(2, "  alarm raised during temp "
0584                 "sensor test, but temp high flag not set\n");
0585     } else
0586         ABIT_UGURU_DEBUG(2, "  alarm not raised during temp sensor "
0587             "test\n");
0588 
0589     ret = ABIT_UGURU_NC;
0590 abituguru_detect_bank1_sensor_type_exit:
0591     /*
0592      * Restore original settings, failing here is really BAD, it has been
0593      * reported that some BIOS-es hang when entering the uGuru menu with
0594      * invalid settings present in the uGuru, so we try this 3 times.
0595      */
0596     for (i = 0; i < 3; i++)
0597         if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
0598                 sensor_addr, data->bank1_settings[sensor_addr],
0599                 3) == 3)
0600             break;
0601     if (i == 3) {
0602         pr_err("Fatal error could not restore original settings. %s %s\n",
0603                never_happen, report_this);
0604         return -ENODEV;
0605     }
0606     return ret;
0607 }
0608 
0609 /*
0610  * These functions try to find out how many sensors there are in bank2 and how
0611  * many pwms there are. The purpose of this is to make sure that we don't give
0612  * the user the possibility to change settings for non-existent sensors / pwm.
0613  * The uGuru will happily read / write whatever memory happens to be after the
0614  * memory storing the PWM settings when reading/writing to a PWM which is not
0615  * there. Notice even if we detect a PWM which doesn't exist we normally won't
0616  * write to it, unless the user tries to change the settings.
0617  *
0618  * Although the uGuru allows reading (settings) from non existing bank2
0619  * sensors, my version of the uGuru does seem to stop writing to them, the
0620  * write function above aborts in this case with:
0621  * "CMD reg does not hold 0xAC after write"
0622  *
0623  * Notice these 2 tests are non destructive iow read-only tests, otherwise
0624  * they would defeat their purpose. Although for the bank2_sensors detection a
0625  * read/write test would be feasible because of the reaction above, I've
0626  * however opted to stay on the safe side.
0627  */
0628 static void
0629 abituguru_detect_no_bank2_sensors(struct abituguru_data *data)
0630 {
0631     int i;
0632 
0633     if (fan_sensors > 0 && fan_sensors <= ABIT_UGURU_MAX_BANK2_SENSORS) {
0634         data->bank2_sensors = fan_sensors;
0635         ABIT_UGURU_DEBUG(2, "assuming %d fan sensors because of "
0636             "\"fan_sensors\" module param\n",
0637             (int)data->bank2_sensors);
0638         return;
0639     }
0640 
0641     ABIT_UGURU_DEBUG(2, "detecting number of fan sensors\n");
0642     for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
0643         /*
0644          * 0x89 are the known used bits:
0645          * -0x80 enable shutdown
0646          * -0x08 enable beep
0647          * -0x01 enable alarm
0648          * All other bits should be 0, but on some motherboards
0649          * 0x40 (bit 6) is also high for some of the fans??
0650          */
0651         if (data->bank2_settings[i][0] & ~0xC9) {
0652             ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
0653                 "to be a fan sensor: settings[0] = %02X\n",
0654                 i, (unsigned int)data->bank2_settings[i][0]);
0655             break;
0656         }
0657 
0658         /* check if the threshold is within the allowed range */
0659         if (data->bank2_settings[i][1] <
0660                 abituguru_bank2_min_threshold) {
0661             ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
0662                 "to be a fan sensor: the threshold (%d) is "
0663                 "below the minimum (%d)\n", i,
0664                 (int)data->bank2_settings[i][1],
0665                 (int)abituguru_bank2_min_threshold);
0666             break;
0667         }
0668         if (data->bank2_settings[i][1] >
0669                 abituguru_bank2_max_threshold) {
0670             ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
0671                 "to be a fan sensor: the threshold (%d) is "
0672                 "above the maximum (%d)\n", i,
0673                 (int)data->bank2_settings[i][1],
0674                 (int)abituguru_bank2_max_threshold);
0675             break;
0676         }
0677     }
0678 
0679     data->bank2_sensors = i;
0680     ABIT_UGURU_DEBUG(2, " found: %d fan sensors\n",
0681         (int)data->bank2_sensors);
0682 }
0683 
0684 static void
0685 abituguru_detect_no_pwms(struct abituguru_data *data)
0686 {
0687     int i, j;
0688 
0689     if (pwms > 0 && pwms <= ABIT_UGURU_MAX_PWMS) {
0690         data->pwms = pwms;
0691         ABIT_UGURU_DEBUG(2, "assuming %d PWM outputs because of "
0692             "\"pwms\" module param\n", (int)data->pwms);
0693         return;
0694     }
0695 
0696     ABIT_UGURU_DEBUG(2, "detecting number of PWM outputs\n");
0697     for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
0698         /*
0699          * 0x80 is the enable bit and the low
0700          * nibble is which temp sensor to use,
0701          * the other bits should be 0
0702          */
0703         if (data->pwm_settings[i][0] & ~0x8F) {
0704             ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
0705                 "to be a pwm channel: settings[0] = %02X\n",
0706                 i, (unsigned int)data->pwm_settings[i][0]);
0707             break;
0708         }
0709 
0710         /*
0711          * the low nibble must correspond to one of the temp sensors
0712          * we've found
0713          */
0714         for (j = 0; j < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR];
0715                 j++) {
0716             if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][j] ==
0717                     (data->pwm_settings[i][0] & 0x0F))
0718                 break;
0719         }
0720         if (j == data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
0721             ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
0722                 "to be a pwm channel: %d is not a valid temp "
0723                 "sensor address\n", i,
0724                 data->pwm_settings[i][0] & 0x0F);
0725             break;
0726         }
0727 
0728         /* check if all other settings are within the allowed range */
0729         for (j = 1; j < 5; j++) {
0730             u8 min;
0731             /* special case pwm1 min pwm% */
0732             if ((i == 0) && ((j == 1) || (j == 2)))
0733                 min = 77;
0734             else
0735                 min = abituguru_pwm_min[j];
0736             if (data->pwm_settings[i][j] < min) {
0737                 ABIT_UGURU_DEBUG(2, "  pwm channel %d does "
0738                     "not seem to be a pwm channel: "
0739                     "setting %d (%d) is below the minimum "
0740                     "value (%d)\n", i, j,
0741                     (int)data->pwm_settings[i][j],
0742                     (int)min);
0743                 goto abituguru_detect_no_pwms_exit;
0744             }
0745             if (data->pwm_settings[i][j] > abituguru_pwm_max[j]) {
0746                 ABIT_UGURU_DEBUG(2, "  pwm channel %d does "
0747                     "not seem to be a pwm channel: "
0748                     "setting %d (%d) is above the maximum "
0749                     "value (%d)\n", i, j,
0750                     (int)data->pwm_settings[i][j],
0751                     (int)abituguru_pwm_max[j]);
0752                 goto abituguru_detect_no_pwms_exit;
0753             }
0754         }
0755 
0756         /* check that min temp < max temp and min pwm < max pwm */
0757         if (data->pwm_settings[i][1] >= data->pwm_settings[i][2]) {
0758             ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
0759                 "to be a pwm channel: min pwm (%d) >= "
0760                 "max pwm (%d)\n", i,
0761                 (int)data->pwm_settings[i][1],
0762                 (int)data->pwm_settings[i][2]);
0763             break;
0764         }
0765         if (data->pwm_settings[i][3] >= data->pwm_settings[i][4]) {
0766             ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
0767                 "to be a pwm channel: min temp (%d) >= "
0768                 "max temp (%d)\n", i,
0769                 (int)data->pwm_settings[i][3],
0770                 (int)data->pwm_settings[i][4]);
0771             break;
0772         }
0773     }
0774 
0775 abituguru_detect_no_pwms_exit:
0776     data->pwms = i;
0777     ABIT_UGURU_DEBUG(2, " found: %d PWM outputs\n", (int)data->pwms);
0778 }
0779 
0780 /*
0781  * Following are the sysfs callback functions. These functions expect:
0782  * sensor_device_attribute_2->index:   sensor address/offset in the bank
0783  * sensor_device_attribute_2->nr:      register offset, bitmask or NA.
0784  */
0785 static struct abituguru_data *abituguru_update_device(struct device *dev);
0786 
0787 static ssize_t show_bank1_value(struct device *dev,
0788     struct device_attribute *devattr, char *buf)
0789 {
0790     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
0791     struct abituguru_data *data = abituguru_update_device(dev);
0792     if (!data)
0793         return -EIO;
0794     return sprintf(buf, "%d\n", (data->bank1_value[attr->index] *
0795         data->bank1_max_value[attr->index] + 128) / 255);
0796 }
0797 
0798 static ssize_t show_bank1_setting(struct device *dev,
0799     struct device_attribute *devattr, char *buf)
0800 {
0801     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
0802     struct abituguru_data *data = dev_get_drvdata(dev);
0803     return sprintf(buf, "%d\n",
0804         (data->bank1_settings[attr->index][attr->nr] *
0805         data->bank1_max_value[attr->index] + 128) / 255);
0806 }
0807 
0808 static ssize_t show_bank2_value(struct device *dev,
0809     struct device_attribute *devattr, char *buf)
0810 {
0811     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
0812     struct abituguru_data *data = abituguru_update_device(dev);
0813     if (!data)
0814         return -EIO;
0815     return sprintf(buf, "%d\n", (data->bank2_value[attr->index] *
0816         ABIT_UGURU_FAN_MAX + 128) / 255);
0817 }
0818 
0819 static ssize_t show_bank2_setting(struct device *dev,
0820     struct device_attribute *devattr, char *buf)
0821 {
0822     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
0823     struct abituguru_data *data = dev_get_drvdata(dev);
0824     return sprintf(buf, "%d\n",
0825         (data->bank2_settings[attr->index][attr->nr] *
0826         ABIT_UGURU_FAN_MAX + 128) / 255);
0827 }
0828 
0829 static ssize_t store_bank1_setting(struct device *dev, struct device_attribute
0830     *devattr, const char *buf, size_t count)
0831 {
0832     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
0833     struct abituguru_data *data = dev_get_drvdata(dev);
0834     unsigned long val;
0835     ssize_t ret;
0836 
0837     ret = kstrtoul(buf, 10, &val);
0838     if (ret)
0839         return ret;
0840 
0841     ret = count;
0842     val = (val * 255 + data->bank1_max_value[attr->index] / 2) /
0843         data->bank1_max_value[attr->index];
0844     if (val > 255)
0845         return -EINVAL;
0846 
0847     mutex_lock(&data->update_lock);
0848     if (data->bank1_settings[attr->index][attr->nr] != val) {
0849         u8 orig_val = data->bank1_settings[attr->index][attr->nr];
0850         data->bank1_settings[attr->index][attr->nr] = val;
0851         if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
0852                 attr->index, data->bank1_settings[attr->index],
0853                 3) <= attr->nr) {
0854             data->bank1_settings[attr->index][attr->nr] = orig_val;
0855             ret = -EIO;
0856         }
0857     }
0858     mutex_unlock(&data->update_lock);
0859     return ret;
0860 }
0861 
0862 static ssize_t store_bank2_setting(struct device *dev, struct device_attribute
0863     *devattr, const char *buf, size_t count)
0864 {
0865     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
0866     struct abituguru_data *data = dev_get_drvdata(dev);
0867     unsigned long val;
0868     ssize_t ret;
0869 
0870     ret = kstrtoul(buf, 10, &val);
0871     if (ret)
0872         return ret;
0873 
0874     ret = count;
0875     val = (val * 255 + ABIT_UGURU_FAN_MAX / 2) / ABIT_UGURU_FAN_MAX;
0876 
0877     /* this check can be done before taking the lock */
0878     if (val < abituguru_bank2_min_threshold ||
0879             val > abituguru_bank2_max_threshold)
0880         return -EINVAL;
0881 
0882     mutex_lock(&data->update_lock);
0883     if (data->bank2_settings[attr->index][attr->nr] != val) {
0884         u8 orig_val = data->bank2_settings[attr->index][attr->nr];
0885         data->bank2_settings[attr->index][attr->nr] = val;
0886         if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK2 + 2,
0887                 attr->index, data->bank2_settings[attr->index],
0888                 2) <= attr->nr) {
0889             data->bank2_settings[attr->index][attr->nr] = orig_val;
0890             ret = -EIO;
0891         }
0892     }
0893     mutex_unlock(&data->update_lock);
0894     return ret;
0895 }
0896 
0897 static ssize_t show_bank1_alarm(struct device *dev,
0898     struct device_attribute *devattr, char *buf)
0899 {
0900     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
0901     struct abituguru_data *data = abituguru_update_device(dev);
0902     if (!data)
0903         return -EIO;
0904     /*
0905      * See if the alarm bit for this sensor is set, and if the
0906      * alarm matches the type of alarm we're looking for (for volt
0907      * it can be either low or high). The type is stored in a few
0908      * readonly bits in the settings part of the relevant sensor.
0909      * The bitmask of the type is passed to us in attr->nr.
0910      */
0911     if ((data->alarms[attr->index / 8] & (0x01 << (attr->index % 8))) &&
0912             (data->bank1_settings[attr->index][0] & attr->nr))
0913         return sprintf(buf, "1\n");
0914     else
0915         return sprintf(buf, "0\n");
0916 }
0917 
0918 static ssize_t show_bank2_alarm(struct device *dev,
0919     struct device_attribute *devattr, char *buf)
0920 {
0921     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
0922     struct abituguru_data *data = abituguru_update_device(dev);
0923     if (!data)
0924         return -EIO;
0925     if (data->alarms[2] & (0x01 << attr->index))
0926         return sprintf(buf, "1\n");
0927     else
0928         return sprintf(buf, "0\n");
0929 }
0930 
0931 static ssize_t show_bank1_mask(struct device *dev,
0932     struct device_attribute *devattr, char *buf)
0933 {
0934     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
0935     struct abituguru_data *data = dev_get_drvdata(dev);
0936     if (data->bank1_settings[attr->index][0] & attr->nr)
0937         return sprintf(buf, "1\n");
0938     else
0939         return sprintf(buf, "0\n");
0940 }
0941 
0942 static ssize_t show_bank2_mask(struct device *dev,
0943     struct device_attribute *devattr, char *buf)
0944 {
0945     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
0946     struct abituguru_data *data = dev_get_drvdata(dev);
0947     if (data->bank2_settings[attr->index][0] & attr->nr)
0948         return sprintf(buf, "1\n");
0949     else
0950         return sprintf(buf, "0\n");
0951 }
0952 
0953 static ssize_t store_bank1_mask(struct device *dev,
0954     struct device_attribute *devattr, const char *buf, size_t count)
0955 {
0956     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
0957     struct abituguru_data *data = dev_get_drvdata(dev);
0958     ssize_t ret;
0959     u8 orig_val;
0960     unsigned long mask;
0961 
0962     ret = kstrtoul(buf, 10, &mask);
0963     if (ret)
0964         return ret;
0965 
0966     ret = count;
0967     mutex_lock(&data->update_lock);
0968     orig_val = data->bank1_settings[attr->index][0];
0969 
0970     if (mask)
0971         data->bank1_settings[attr->index][0] |= attr->nr;
0972     else
0973         data->bank1_settings[attr->index][0] &= ~attr->nr;
0974 
0975     if ((data->bank1_settings[attr->index][0] != orig_val) &&
0976             (abituguru_write(data,
0977             ABIT_UGURU_SENSOR_BANK1 + 2, attr->index,
0978             data->bank1_settings[attr->index], 3) < 1)) {
0979         data->bank1_settings[attr->index][0] = orig_val;
0980         ret = -EIO;
0981     }
0982     mutex_unlock(&data->update_lock);
0983     return ret;
0984 }
0985 
0986 static ssize_t store_bank2_mask(struct device *dev,
0987     struct device_attribute *devattr, const char *buf, size_t count)
0988 {
0989     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
0990     struct abituguru_data *data = dev_get_drvdata(dev);
0991     ssize_t ret;
0992     u8 orig_val;
0993     unsigned long mask;
0994 
0995     ret = kstrtoul(buf, 10, &mask);
0996     if (ret)
0997         return ret;
0998 
0999     ret = count;
1000     mutex_lock(&data->update_lock);
1001     orig_val = data->bank2_settings[attr->index][0];
1002 
1003     if (mask)
1004         data->bank2_settings[attr->index][0] |= attr->nr;
1005     else
1006         data->bank2_settings[attr->index][0] &= ~attr->nr;
1007 
1008     if ((data->bank2_settings[attr->index][0] != orig_val) &&
1009             (abituguru_write(data,
1010             ABIT_UGURU_SENSOR_BANK2 + 2, attr->index,
1011             data->bank2_settings[attr->index], 2) < 1)) {
1012         data->bank2_settings[attr->index][0] = orig_val;
1013         ret = -EIO;
1014     }
1015     mutex_unlock(&data->update_lock);
1016     return ret;
1017 }
1018 
1019 /* Fan PWM (speed control) */
1020 static ssize_t show_pwm_setting(struct device *dev,
1021     struct device_attribute *devattr, char *buf)
1022 {
1023     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1024     struct abituguru_data *data = dev_get_drvdata(dev);
1025     return sprintf(buf, "%d\n", data->pwm_settings[attr->index][attr->nr] *
1026         abituguru_pwm_settings_multiplier[attr->nr]);
1027 }
1028 
1029 static ssize_t store_pwm_setting(struct device *dev, struct device_attribute
1030     *devattr, const char *buf, size_t count)
1031 {
1032     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1033     struct abituguru_data *data = dev_get_drvdata(dev);
1034     u8 min;
1035     unsigned long val;
1036     ssize_t ret;
1037 
1038     ret = kstrtoul(buf, 10, &val);
1039     if (ret)
1040         return ret;
1041 
1042     ret = count;
1043     val = (val + abituguru_pwm_settings_multiplier[attr->nr] / 2) /
1044                 abituguru_pwm_settings_multiplier[attr->nr];
1045 
1046     /* special case pwm1 min pwm% */
1047     if ((attr->index == 0) && ((attr->nr == 1) || (attr->nr == 2)))
1048         min = 77;
1049     else
1050         min = abituguru_pwm_min[attr->nr];
1051 
1052     /* this check can be done before taking the lock */
1053     if (val < min || val > abituguru_pwm_max[attr->nr])
1054         return -EINVAL;
1055 
1056     mutex_lock(&data->update_lock);
1057     /* this check needs to be done after taking the lock */
1058     if ((attr->nr & 1) &&
1059             (val >= data->pwm_settings[attr->index][attr->nr + 1]))
1060         ret = -EINVAL;
1061     else if (!(attr->nr & 1) &&
1062             (val <= data->pwm_settings[attr->index][attr->nr - 1]))
1063         ret = -EINVAL;
1064     else if (data->pwm_settings[attr->index][attr->nr] != val) {
1065         u8 orig_val = data->pwm_settings[attr->index][attr->nr];
1066         data->pwm_settings[attr->index][attr->nr] = val;
1067         if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
1068                 attr->index, data->pwm_settings[attr->index],
1069                 5) <= attr->nr) {
1070             data->pwm_settings[attr->index][attr->nr] =
1071                 orig_val;
1072             ret = -EIO;
1073         }
1074     }
1075     mutex_unlock(&data->update_lock);
1076     return ret;
1077 }
1078 
1079 static ssize_t show_pwm_sensor(struct device *dev,
1080     struct device_attribute *devattr, char *buf)
1081 {
1082     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1083     struct abituguru_data *data = dev_get_drvdata(dev);
1084     int i;
1085     /*
1086      * We need to walk to the temp sensor addresses to find what
1087      * the userspace id of the configured temp sensor is.
1088      */
1089     for (i = 0; i < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]; i++)
1090         if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][i] ==
1091                 (data->pwm_settings[attr->index][0] & 0x0F))
1092             return sprintf(buf, "%d\n", i+1);
1093 
1094     return -ENXIO;
1095 }
1096 
1097 static ssize_t store_pwm_sensor(struct device *dev, struct device_attribute
1098     *devattr, const char *buf, size_t count)
1099 {
1100     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1101     struct abituguru_data *data = dev_get_drvdata(dev);
1102     ssize_t ret;
1103     unsigned long val;
1104     u8 orig_val;
1105     u8 address;
1106 
1107     ret = kstrtoul(buf, 10, &val);
1108     if (ret)
1109         return ret;
1110 
1111     if (val == 0 || val > data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR])
1112         return -EINVAL;
1113 
1114     val -= 1;
1115     ret = count;
1116     mutex_lock(&data->update_lock);
1117     orig_val = data->pwm_settings[attr->index][0];
1118     address = data->bank1_address[ABIT_UGURU_TEMP_SENSOR][val];
1119     data->pwm_settings[attr->index][0] &= 0xF0;
1120     data->pwm_settings[attr->index][0] |= address;
1121     if (data->pwm_settings[attr->index][0] != orig_val) {
1122         if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1, attr->index,
1123                     data->pwm_settings[attr->index], 5) < 1) {
1124             data->pwm_settings[attr->index][0] = orig_val;
1125             ret = -EIO;
1126         }
1127     }
1128     mutex_unlock(&data->update_lock);
1129     return ret;
1130 }
1131 
1132 static ssize_t show_pwm_enable(struct device *dev,
1133     struct device_attribute *devattr, char *buf)
1134 {
1135     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1136     struct abituguru_data *data = dev_get_drvdata(dev);
1137     int res = 0;
1138     if (data->pwm_settings[attr->index][0] & ABIT_UGURU_FAN_PWM_ENABLE)
1139         res = 2;
1140     return sprintf(buf, "%d\n", res);
1141 }
1142 
1143 static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
1144     *devattr, const char *buf, size_t count)
1145 {
1146     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1147     struct abituguru_data *data = dev_get_drvdata(dev);
1148     u8 orig_val;
1149     ssize_t ret;
1150     unsigned long user_val;
1151 
1152     ret = kstrtoul(buf, 10, &user_val);
1153     if (ret)
1154         return ret;
1155 
1156     ret = count;
1157     mutex_lock(&data->update_lock);
1158     orig_val = data->pwm_settings[attr->index][0];
1159     switch (user_val) {
1160     case 0:
1161         data->pwm_settings[attr->index][0] &=
1162             ~ABIT_UGURU_FAN_PWM_ENABLE;
1163         break;
1164     case 2:
1165         data->pwm_settings[attr->index][0] |= ABIT_UGURU_FAN_PWM_ENABLE;
1166         break;
1167     default:
1168         ret = -EINVAL;
1169     }
1170     if ((data->pwm_settings[attr->index][0] != orig_val) &&
1171             (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
1172             attr->index, data->pwm_settings[attr->index],
1173             5) < 1)) {
1174         data->pwm_settings[attr->index][0] = orig_val;
1175         ret = -EIO;
1176     }
1177     mutex_unlock(&data->update_lock);
1178     return ret;
1179 }
1180 
1181 static ssize_t show_name(struct device *dev,
1182     struct device_attribute *devattr, char *buf)
1183 {
1184     return sprintf(buf, "%s\n", ABIT_UGURU_NAME);
1185 }
1186 
1187 /* Sysfs attr templates, the real entries are generated automatically. */
1188 static const
1189 struct sensor_device_attribute_2 abituguru_sysfs_bank1_templ[2][9] = {
1190     {
1191     SENSOR_ATTR_2(in%d_input, 0444, show_bank1_value, NULL, 0, 0),
1192     SENSOR_ATTR_2(in%d_min, 0644, show_bank1_setting,
1193         store_bank1_setting, 1, 0),
1194     SENSOR_ATTR_2(in%d_min_alarm, 0444, show_bank1_alarm, NULL,
1195         ABIT_UGURU_VOLT_LOW_ALARM_FLAG, 0),
1196     SENSOR_ATTR_2(in%d_max, 0644, show_bank1_setting,
1197         store_bank1_setting, 2, 0),
1198     SENSOR_ATTR_2(in%d_max_alarm, 0444, show_bank1_alarm, NULL,
1199         ABIT_UGURU_VOLT_HIGH_ALARM_FLAG, 0),
1200     SENSOR_ATTR_2(in%d_beep, 0644, show_bank1_mask,
1201         store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1202     SENSOR_ATTR_2(in%d_shutdown, 0644, show_bank1_mask,
1203         store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1204     SENSOR_ATTR_2(in%d_min_alarm_enable, 0644, show_bank1_mask,
1205         store_bank1_mask, ABIT_UGURU_VOLT_LOW_ALARM_ENABLE, 0),
1206     SENSOR_ATTR_2(in%d_max_alarm_enable, 0644, show_bank1_mask,
1207         store_bank1_mask, ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE, 0),
1208     }, {
1209     SENSOR_ATTR_2(temp%d_input, 0444, show_bank1_value, NULL, 0, 0),
1210     SENSOR_ATTR_2(temp%d_alarm, 0444, show_bank1_alarm, NULL,
1211         ABIT_UGURU_TEMP_HIGH_ALARM_FLAG, 0),
1212     SENSOR_ATTR_2(temp%d_max, 0644, show_bank1_setting,
1213         store_bank1_setting, 1, 0),
1214     SENSOR_ATTR_2(temp%d_crit, 0644, show_bank1_setting,
1215         store_bank1_setting, 2, 0),
1216     SENSOR_ATTR_2(temp%d_beep, 0644, show_bank1_mask,
1217         store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1218     SENSOR_ATTR_2(temp%d_shutdown, 0644, show_bank1_mask,
1219         store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1220     SENSOR_ATTR_2(temp%d_alarm_enable, 0644, show_bank1_mask,
1221         store_bank1_mask, ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE, 0),
1222     }
1223 };
1224 
1225 static const struct sensor_device_attribute_2 abituguru_sysfs_fan_templ[6] = {
1226     SENSOR_ATTR_2(fan%d_input, 0444, show_bank2_value, NULL, 0, 0),
1227     SENSOR_ATTR_2(fan%d_alarm, 0444, show_bank2_alarm, NULL, 0, 0),
1228     SENSOR_ATTR_2(fan%d_min, 0644, show_bank2_setting,
1229         store_bank2_setting, 1, 0),
1230     SENSOR_ATTR_2(fan%d_beep, 0644, show_bank2_mask,
1231         store_bank2_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1232     SENSOR_ATTR_2(fan%d_shutdown, 0644, show_bank2_mask,
1233         store_bank2_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1234     SENSOR_ATTR_2(fan%d_alarm_enable, 0644, show_bank2_mask,
1235         store_bank2_mask, ABIT_UGURU_FAN_LOW_ALARM_ENABLE, 0),
1236 };
1237 
1238 static const struct sensor_device_attribute_2 abituguru_sysfs_pwm_templ[6] = {
1239     SENSOR_ATTR_2(pwm%d_enable, 0644, show_pwm_enable,
1240         store_pwm_enable, 0, 0),
1241     SENSOR_ATTR_2(pwm%d_auto_channels_temp, 0644, show_pwm_sensor,
1242         store_pwm_sensor, 0, 0),
1243     SENSOR_ATTR_2(pwm%d_auto_point1_pwm, 0644, show_pwm_setting,
1244         store_pwm_setting, 1, 0),
1245     SENSOR_ATTR_2(pwm%d_auto_point2_pwm, 0644, show_pwm_setting,
1246         store_pwm_setting, 2, 0),
1247     SENSOR_ATTR_2(pwm%d_auto_point1_temp, 0644, show_pwm_setting,
1248         store_pwm_setting, 3, 0),
1249     SENSOR_ATTR_2(pwm%d_auto_point2_temp, 0644, show_pwm_setting,
1250         store_pwm_setting, 4, 0),
1251 };
1252 
1253 static struct sensor_device_attribute_2 abituguru_sysfs_attr[] = {
1254     SENSOR_ATTR_2(name, 0444, show_name, NULL, 0, 0),
1255 };
1256 
1257 static int abituguru_probe(struct platform_device *pdev)
1258 {
1259     struct abituguru_data *data;
1260     int i, j, used, sysfs_names_free, sysfs_attr_i, res = -ENODEV;
1261     char *sysfs_filename;
1262 
1263     /*
1264      * El weirdo probe order, to keep the sysfs order identical to the
1265      * BIOS and window-appliction listing order.
1266      */
1267     static const u8 probe_order[ABIT_UGURU_MAX_BANK1_SENSORS] = {
1268         0x00, 0x01, 0x03, 0x04, 0x0A, 0x08, 0x0E, 0x02,
1269         0x09, 0x06, 0x05, 0x0B, 0x0F, 0x0D, 0x07, 0x0C };
1270 
1271     data = devm_kzalloc(&pdev->dev, sizeof(struct abituguru_data),
1272                 GFP_KERNEL);
1273     if (!data)
1274         return -ENOMEM;
1275 
1276     data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
1277     mutex_init(&data->update_lock);
1278     platform_set_drvdata(pdev, data);
1279 
1280     /* See if the uGuru is ready */
1281     if (inb_p(data->addr + ABIT_UGURU_DATA) == ABIT_UGURU_STATUS_INPUT)
1282         data->uguru_ready = 1;
1283 
1284     /*
1285      * Completely read the uGuru this has 2 purposes:
1286      * - testread / see if one really is there.
1287      * - make an in memory copy of all the uguru settings for future use.
1288      */
1289     if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1290             data->alarms, 3, ABIT_UGURU_MAX_RETRIES) != 3)
1291         goto abituguru_probe_error;
1292 
1293     for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1294         if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, i,
1295                 &data->bank1_value[i], 1,
1296                 ABIT_UGURU_MAX_RETRIES) != 1)
1297             goto abituguru_probe_error;
1298         if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1+1, i,
1299                 data->bank1_settings[i], 3,
1300                 ABIT_UGURU_MAX_RETRIES) != 3)
1301             goto abituguru_probe_error;
1302     }
1303     /*
1304      * Note: We don't know how many bank2 sensors / pwms there really are,
1305      * but in order to "detect" this we need to read the maximum amount
1306      * anyways. If we read sensors/pwms not there we'll just read crap
1307      * this can't hurt. We need the detection because we don't want
1308      * unwanted writes, which will hurt!
1309      */
1310     for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
1311         if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
1312                 &data->bank2_value[i], 1,
1313                 ABIT_UGURU_MAX_RETRIES) != 1)
1314             goto abituguru_probe_error;
1315         if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2+1, i,
1316                 data->bank2_settings[i], 2,
1317                 ABIT_UGURU_MAX_RETRIES) != 2)
1318             goto abituguru_probe_error;
1319     }
1320     for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
1321         if (abituguru_read(data, ABIT_UGURU_FAN_PWM, i,
1322                 data->pwm_settings[i], 5,
1323                 ABIT_UGURU_MAX_RETRIES) != 5)
1324             goto abituguru_probe_error;
1325     }
1326     data->last_updated = jiffies;
1327 
1328     /* Detect sensor types and fill the sysfs attr for bank1 */
1329     sysfs_attr_i = 0;
1330     sysfs_filename = data->sysfs_names;
1331     sysfs_names_free = ABITUGURU_SYSFS_NAMES_LENGTH;
1332     for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1333         res = abituguru_detect_bank1_sensor_type(data, probe_order[i]);
1334         if (res < 0)
1335             goto abituguru_probe_error;
1336         if (res == ABIT_UGURU_NC)
1337             continue;
1338 
1339         /* res 1 (temp) sensors have 7 sysfs entries, 0 (in) 9 */
1340         for (j = 0; j < (res ? 7 : 9); j++) {
1341             used = snprintf(sysfs_filename, sysfs_names_free,
1342                 abituguru_sysfs_bank1_templ[res][j].dev_attr.
1343                 attr.name, data->bank1_sensors[res] + res)
1344                 + 1;
1345             data->sysfs_attr[sysfs_attr_i] =
1346                 abituguru_sysfs_bank1_templ[res][j];
1347             data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1348                 sysfs_filename;
1349             data->sysfs_attr[sysfs_attr_i].index = probe_order[i];
1350             sysfs_filename += used;
1351             sysfs_names_free -= used;
1352             sysfs_attr_i++;
1353         }
1354         data->bank1_max_value[probe_order[i]] =
1355             abituguru_bank1_max_value[res];
1356         data->bank1_address[res][data->bank1_sensors[res]] =
1357             probe_order[i];
1358         data->bank1_sensors[res]++;
1359     }
1360     /* Detect number of sensors and fill the sysfs attr for bank2 (fans) */
1361     abituguru_detect_no_bank2_sensors(data);
1362     for (i = 0; i < data->bank2_sensors; i++) {
1363         for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_fan_templ); j++) {
1364             used = snprintf(sysfs_filename, sysfs_names_free,
1365                 abituguru_sysfs_fan_templ[j].dev_attr.attr.name,
1366                 i + 1) + 1;
1367             data->sysfs_attr[sysfs_attr_i] =
1368                 abituguru_sysfs_fan_templ[j];
1369             data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1370                 sysfs_filename;
1371             data->sysfs_attr[sysfs_attr_i].index = i;
1372             sysfs_filename += used;
1373             sysfs_names_free -= used;
1374             sysfs_attr_i++;
1375         }
1376     }
1377     /* Detect number of sensors and fill the sysfs attr for pwms */
1378     abituguru_detect_no_pwms(data);
1379     for (i = 0; i < data->pwms; i++) {
1380         for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_pwm_templ); j++) {
1381             used = snprintf(sysfs_filename, sysfs_names_free,
1382                 abituguru_sysfs_pwm_templ[j].dev_attr.attr.name,
1383                 i + 1) + 1;
1384             data->sysfs_attr[sysfs_attr_i] =
1385                 abituguru_sysfs_pwm_templ[j];
1386             data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1387                 sysfs_filename;
1388             data->sysfs_attr[sysfs_attr_i].index = i;
1389             sysfs_filename += used;
1390             sysfs_names_free -= used;
1391             sysfs_attr_i++;
1392         }
1393     }
1394     /* Fail safe check, this should never happen! */
1395     if (sysfs_names_free < 0) {
1396         pr_err("Fatal error ran out of space for sysfs attr names. %s %s",
1397                never_happen, report_this);
1398         res = -ENAMETOOLONG;
1399         goto abituguru_probe_error;
1400     }
1401     pr_info("found Abit uGuru\n");
1402 
1403     /* Register sysfs hooks */
1404     for (i = 0; i < sysfs_attr_i; i++) {
1405         res = device_create_file(&pdev->dev,
1406                      &data->sysfs_attr[i].dev_attr);
1407         if (res)
1408             goto abituguru_probe_error;
1409     }
1410     for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++) {
1411         res = device_create_file(&pdev->dev,
1412                      &abituguru_sysfs_attr[i].dev_attr);
1413         if (res)
1414             goto abituguru_probe_error;
1415     }
1416 
1417     data->hwmon_dev = hwmon_device_register(&pdev->dev);
1418     if (!IS_ERR(data->hwmon_dev))
1419         return 0; /* success */
1420 
1421     res = PTR_ERR(data->hwmon_dev);
1422 abituguru_probe_error:
1423     for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
1424         device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
1425     for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1426         device_remove_file(&pdev->dev,
1427             &abituguru_sysfs_attr[i].dev_attr);
1428     return res;
1429 }
1430 
1431 static int abituguru_remove(struct platform_device *pdev)
1432 {
1433     int i;
1434     struct abituguru_data *data = platform_get_drvdata(pdev);
1435 
1436     hwmon_device_unregister(data->hwmon_dev);
1437     for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
1438         device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
1439     for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1440         device_remove_file(&pdev->dev,
1441             &abituguru_sysfs_attr[i].dev_attr);
1442 
1443     return 0;
1444 }
1445 
1446 static struct abituguru_data *abituguru_update_device(struct device *dev)
1447 {
1448     int i, err;
1449     struct abituguru_data *data = dev_get_drvdata(dev);
1450     /* fake a complete successful read if no update necessary. */
1451     char success = 1;
1452 
1453     mutex_lock(&data->update_lock);
1454     if (time_after(jiffies, data->last_updated + HZ)) {
1455         success = 0;
1456         err = abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1457                      data->alarms, 3, 0);
1458         if (err != 3)
1459             goto LEAVE_UPDATE;
1460         for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1461             err = abituguru_read(data, ABIT_UGURU_SENSOR_BANK1,
1462                          i, &data->bank1_value[i], 1, 0);
1463             if (err != 1)
1464                 goto LEAVE_UPDATE;
1465             err = abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
1466                          i, data->bank1_settings[i], 3, 0);
1467             if (err != 3)
1468                 goto LEAVE_UPDATE;
1469         }
1470         for (i = 0; i < data->bank2_sensors; i++) {
1471             err = abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
1472                          &data->bank2_value[i], 1, 0);
1473             if (err != 1)
1474                 goto LEAVE_UPDATE;
1475         }
1476         /* success! */
1477         success = 1;
1478         data->update_timeouts = 0;
1479 LEAVE_UPDATE:
1480         /* handle timeout condition */
1481         if (!success && (err == -EBUSY || err >= 0)) {
1482             /* No overflow please */
1483             if (data->update_timeouts < 255u)
1484                 data->update_timeouts++;
1485             if (data->update_timeouts <= ABIT_UGURU_MAX_TIMEOUTS) {
1486                 ABIT_UGURU_DEBUG(3, "timeout exceeded, will "
1487                     "try again next update\n");
1488                 /* Just a timeout, fake a successful read */
1489                 success = 1;
1490             } else
1491                 ABIT_UGURU_DEBUG(1, "timeout exceeded %d "
1492                     "times waiting for more input state\n",
1493                     (int)data->update_timeouts);
1494         }
1495         /* On success set last_updated */
1496         if (success)
1497             data->last_updated = jiffies;
1498     }
1499     mutex_unlock(&data->update_lock);
1500 
1501     if (success)
1502         return data;
1503     else
1504         return NULL;
1505 }
1506 
1507 #ifdef CONFIG_PM_SLEEP
1508 static int abituguru_suspend(struct device *dev)
1509 {
1510     struct abituguru_data *data = dev_get_drvdata(dev);
1511     /*
1512      * make sure all communications with the uguru are done and no new
1513      * ones are started
1514      */
1515     mutex_lock(&data->update_lock);
1516     return 0;
1517 }
1518 
1519 static int abituguru_resume(struct device *dev)
1520 {
1521     struct abituguru_data *data = dev_get_drvdata(dev);
1522     /* See if the uGuru is still ready */
1523     if (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT)
1524         data->uguru_ready = 0;
1525     mutex_unlock(&data->update_lock);
1526     return 0;
1527 }
1528 
1529 static SIMPLE_DEV_PM_OPS(abituguru_pm, abituguru_suspend, abituguru_resume);
1530 #define ABIT_UGURU_PM   (&abituguru_pm)
1531 #else
1532 #define ABIT_UGURU_PM   NULL
1533 #endif /* CONFIG_PM */
1534 
1535 static struct platform_driver abituguru_driver = {
1536     .driver = {
1537         .name   = ABIT_UGURU_NAME,
1538         .pm = ABIT_UGURU_PM,
1539     },
1540     .probe      = abituguru_probe,
1541     .remove     = abituguru_remove,
1542 };
1543 
1544 static int __init abituguru_detect(void)
1545 {
1546     /*
1547      * See if there is an uguru there. After a reboot uGuru will hold 0x00
1548      * at DATA and 0xAC, when this driver has already been loaded once
1549      * DATA will hold 0x08. For most uGuru's CMD will hold 0xAC in either
1550      * scenario but some will hold 0x00.
1551      * Some uGuru's initially hold 0x09 at DATA and will only hold 0x08
1552      * after reading CMD first, so CMD must be read first!
1553      */
1554     u8 cmd_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_CMD);
1555     u8 data_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_DATA);
1556     if (((data_val == 0x00) || (data_val == 0x08)) &&
1557         ((cmd_val == 0x00) || (cmd_val == 0xAC)))
1558         return ABIT_UGURU_BASE;
1559 
1560     ABIT_UGURU_DEBUG(2, "no Abit uGuru found, data = 0x%02X, cmd = "
1561         "0x%02X\n", (unsigned int)data_val, (unsigned int)cmd_val);
1562 
1563     if (force) {
1564         pr_info("Assuming Abit uGuru is present because of \"force\" parameter\n");
1565         return ABIT_UGURU_BASE;
1566     }
1567 
1568     /* No uGuru found */
1569     return -ENODEV;
1570 }
1571 
1572 static struct platform_device *abituguru_pdev;
1573 
1574 static int __init abituguru_init(void)
1575 {
1576     int address, err;
1577     struct resource res = { .flags = IORESOURCE_IO };
1578     const char *board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1579 
1580     /* safety check, refuse to load on non Abit motherboards */
1581     if (!force && (!board_vendor ||
1582             strcmp(board_vendor, "http://www.abit.com.tw/")))
1583         return -ENODEV;
1584 
1585     address = abituguru_detect();
1586     if (address < 0)
1587         return address;
1588 
1589     err = platform_driver_register(&abituguru_driver);
1590     if (err)
1591         goto exit;
1592 
1593     abituguru_pdev = platform_device_alloc(ABIT_UGURU_NAME, address);
1594     if (!abituguru_pdev) {
1595         pr_err("Device allocation failed\n");
1596         err = -ENOMEM;
1597         goto exit_driver_unregister;
1598     }
1599 
1600     res.start = address;
1601     res.end = address + ABIT_UGURU_REGION_LENGTH - 1;
1602     res.name = ABIT_UGURU_NAME;
1603 
1604     err = platform_device_add_resources(abituguru_pdev, &res, 1);
1605     if (err) {
1606         pr_err("Device resource addition failed (%d)\n", err);
1607         goto exit_device_put;
1608     }
1609 
1610     err = platform_device_add(abituguru_pdev);
1611     if (err) {
1612         pr_err("Device addition failed (%d)\n", err);
1613         goto exit_device_put;
1614     }
1615 
1616     return 0;
1617 
1618 exit_device_put:
1619     platform_device_put(abituguru_pdev);
1620 exit_driver_unregister:
1621     platform_driver_unregister(&abituguru_driver);
1622 exit:
1623     return err;
1624 }
1625 
1626 static void __exit abituguru_exit(void)
1627 {
1628     platform_device_unregister(abituguru_pdev);
1629     platform_driver_unregister(&abituguru_driver);
1630 }
1631 
1632 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
1633 MODULE_DESCRIPTION("Abit uGuru Sensor device");
1634 MODULE_LICENSE("GPL");
1635 
1636 module_init(abituguru_init);
1637 module_exit(abituguru_exit);