Linux Audio

Check our new training course

Loading...
v3.1
   1/*
   2    abituguru.c Copyright (c) 2005-2006 Hans de Goede <hdegoede@redhat.com>
   3
   4    This program is free software; you can redistribute it and/or modify
   5    it under the terms of the GNU General Public License as published by
   6    the Free Software Foundation; either version 2 of the License, or
   7    (at your option) any later version.
   8
   9    This program is distributed in the hope that it will be useful,
  10    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12    GNU General Public License for more details.
  13
  14    You should have received a copy of the GNU General Public License
  15    along with this program; if not, write to the Free Software
  16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17*/
  18/*
  19    This driver supports the sensor part of the first and second revision of
  20    the custom Abit uGuru chip found on Abit uGuru motherboards. Note: because
  21    of lack of specs the CPU/RAM voltage & frequency control is not supported!
  22*/
  23
  24#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25
  26#include <linux/module.h>
  27#include <linux/sched.h>
  28#include <linux/init.h>
  29#include <linux/slab.h>
  30#include <linux/jiffies.h>
  31#include <linux/mutex.h>
  32#include <linux/err.h>
  33#include <linux/delay.h>
  34#include <linux/platform_device.h>
  35#include <linux/hwmon.h>
  36#include <linux/hwmon-sysfs.h>
  37#include <linux/dmi.h>
  38#include <linux/io.h>
  39
  40/* Banks */
  41#define ABIT_UGURU_ALARM_BANK			0x20 /* 1x 3 bytes */
  42#define ABIT_UGURU_SENSOR_BANK1			0x21 /* 16x volt and temp */
  43#define ABIT_UGURU_FAN_PWM			0x24 /* 3x 5 bytes */
  44#define ABIT_UGURU_SENSOR_BANK2			0x26 /* fans */
  45/* max nr of sensors in bank1, a bank1 sensor can be in, temp or nc */
  46#define ABIT_UGURU_MAX_BANK1_SENSORS		16
  47/* Warning if you increase one of the 2 MAX defines below to 10 or higher you
  48   should adjust the belonging _NAMES_LENGTH macro for the 2 digit number! */
 
 
  49/* max nr of sensors in bank2, currently mb's with max 6 fans are known */
  50#define ABIT_UGURU_MAX_BANK2_SENSORS		6
  51/* max nr of pwm outputs, currently mb's with max 5 pwm outputs are known */
  52#define ABIT_UGURU_MAX_PWMS			5
  53/* uGuru sensor bank 1 flags */			     /* Alarm if: */
  54#define ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE	0x01 /*  temp over warn */
  55#define ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE	0x02 /*  volt over max */
  56#define ABIT_UGURU_VOLT_LOW_ALARM_ENABLE	0x04 /*  volt under min */
  57#define ABIT_UGURU_TEMP_HIGH_ALARM_FLAG		0x10 /* temp is over warn */
  58#define ABIT_UGURU_VOLT_HIGH_ALARM_FLAG		0x20 /* volt is over max */
  59#define ABIT_UGURU_VOLT_LOW_ALARM_FLAG		0x40 /* volt is under min */
  60/* uGuru sensor bank 2 flags */			     /* Alarm if: */
  61#define ABIT_UGURU_FAN_LOW_ALARM_ENABLE		0x01 /*   fan under min */
  62/* uGuru sensor bank common flags */
  63#define ABIT_UGURU_BEEP_ENABLE			0x08 /* beep if alarm */
  64#define ABIT_UGURU_SHUTDOWN_ENABLE		0x80 /* shutdown if alarm */
  65/* uGuru fan PWM (speed control) flags */
  66#define ABIT_UGURU_FAN_PWM_ENABLE		0x80 /* enable speed control */
  67/* Values used for conversion */
  68#define ABIT_UGURU_FAN_MAX			15300 /* RPM */
  69/* Bank1 sensor types */
  70#define ABIT_UGURU_IN_SENSOR			0
  71#define ABIT_UGURU_TEMP_SENSOR			1
  72#define ABIT_UGURU_NC				2
  73/* In many cases we need to wait for the uGuru to reach a certain status, most
  74   of the time it will reach this status within 30 - 90 ISA reads, and thus we
  75   can best busy wait. This define gives the total amount of reads to try. */
 
 
  76#define ABIT_UGURU_WAIT_TIMEOUT			125
  77/* However sometimes older versions of the uGuru seem to be distracted and they
  78   do not respond for a long time. To handle this we sleep before each of the
  79   last ABIT_UGURU_WAIT_TIMEOUT_SLEEP tries. */
 
 
  80#define ABIT_UGURU_WAIT_TIMEOUT_SLEEP		5
  81/* Normally all expected status in abituguru_ready, are reported after the
  82   first read, but sometimes not and we need to poll. */
 
 
  83#define ABIT_UGURU_READY_TIMEOUT		5
  84/* Maximum 3 retries on timedout reads/writes, delay 200 ms before retrying */
  85#define ABIT_UGURU_MAX_RETRIES			3
  86#define ABIT_UGURU_RETRY_DELAY			(HZ/5)
  87/* Maximum 2 timeouts in abituguru_update_device, iow 3 in a row is an error */
  88#define ABIT_UGURU_MAX_TIMEOUTS			2
  89/* utility macros */
  90#define ABIT_UGURU_NAME				"abituguru"
  91#define ABIT_UGURU_DEBUG(level, format, arg...)				\
  92	if (level <= verbose)						\
  93		printk(KERN_DEBUG ABIT_UGURU_NAME ": "	format , ## arg)
  94/* Macros to help calculate the sysfs_names array length */
  95/* sum of strlen of: in??_input\0, in??_{min,max}\0, in??_{min,max}_alarm\0,
  96   in??_{min,max}_alarm_enable\0, in??_beep\0, in??_shutdown\0 */
 
 
  97#define ABITUGURU_IN_NAMES_LENGTH	(11 + 2 * 9 + 2 * 15 + 2 * 22 + 10 + 14)
  98/* sum of strlen of: temp??_input\0, temp??_max\0, temp??_crit\0,
  99   temp??_alarm\0, temp??_alarm_enable\0, temp??_beep\0, temp??_shutdown\0 */
 
 
 100#define ABITUGURU_TEMP_NAMES_LENGTH	(13 + 11 + 12 + 13 + 20 + 12 + 16)
 101/* sum of strlen of: fan?_input\0, fan?_min\0, fan?_alarm\0,
 102   fan?_alarm_enable\0, fan?_beep\0, fan?_shutdown\0 */
 
 
 103#define ABITUGURU_FAN_NAMES_LENGTH	(11 + 9 + 11 + 18 + 10 + 14)
 104/* sum of strlen of: pwm?_enable\0, pwm?_auto_channels_temp\0,
 105   pwm?_auto_point{1,2}_pwm\0, pwm?_auto_point{1,2}_temp\0 */
 
 
 106#define ABITUGURU_PWM_NAMES_LENGTH	(12 + 24 + 2 * 21 + 2 * 22)
 107/* IN_NAMES_LENGTH > TEMP_NAMES_LENGTH so assume all bank1 sensors are in */
 108#define ABITUGURU_SYSFS_NAMES_LENGTH	( \
 109	ABIT_UGURU_MAX_BANK1_SENSORS * ABITUGURU_IN_NAMES_LENGTH + \
 110	ABIT_UGURU_MAX_BANK2_SENSORS * ABITUGURU_FAN_NAMES_LENGTH + \
 111	ABIT_UGURU_MAX_PWMS * ABITUGURU_PWM_NAMES_LENGTH)
 112
 113/* All the macros below are named identical to the oguru and oguru2 programs
 114   reverse engineered by Olle Sandberg, hence the names might not be 100%
 115   logical. I could come up with better names, but I prefer keeping the names
 116   identical so that this driver can be compared with his work more easily. */
 
 
 117/* Two i/o-ports are used by uGuru */
 118#define ABIT_UGURU_BASE				0x00E0
 119/* Used to tell uGuru what to read and to read the actual data */
 120#define ABIT_UGURU_CMD				0x00
 121/* Mostly used to check if uGuru is busy */
 122#define ABIT_UGURU_DATA				0x04
 123#define ABIT_UGURU_REGION_LENGTH		5
 124/* uGuru status' */
 125#define ABIT_UGURU_STATUS_WRITE			0x00 /* Ready to be written */
 126#define ABIT_UGURU_STATUS_READ			0x01 /* Ready to be read */
 127#define ABIT_UGURU_STATUS_INPUT			0x08 /* More input */
 128#define ABIT_UGURU_STATUS_READY			0x09 /* Ready to be written */
 129
 130/* Constants */
 131/* in (Volt) sensors go up to 3494 mV, temp to 255000 millidegrees Celsius */
 132static const int abituguru_bank1_max_value[2] = { 3494, 255000 };
 133/* Min / Max allowed values for sensor2 (fan) alarm threshold, these values
 134   correspond to 300-3000 RPM */
 
 
 135static const u8 abituguru_bank2_min_threshold = 5;
 136static const u8 abituguru_bank2_max_threshold = 50;
 137/* Register 0 is a bitfield, 1 and 2 are pwm settings (255 = 100%), 3 and 4
 138   are temperature trip points. */
 
 
 139static const int abituguru_pwm_settings_multiplier[5] = { 0, 1, 1, 1000, 1000 };
 140/* Min / Max allowed values for pwm_settings. Note: pwm1 (CPU fan) is a
 141   special case the minium allowed pwm% setting for this is 30% (77) on
 142   some MB's this special case is handled in the code! */
 
 
 143static const u8 abituguru_pwm_min[5] = { 0, 170, 170, 25, 25 };
 144static const u8 abituguru_pwm_max[5] = { 0, 255, 255, 75, 75 };
 145
 146
 147/* Insmod parameters */
 148static int force;
 149module_param(force, bool, 0);
 150MODULE_PARM_DESC(force, "Set to one to force detection.");
 151static int bank1_types[ABIT_UGURU_MAX_BANK1_SENSORS] = { -1, -1, -1, -1, -1,
 152	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
 153module_param_array(bank1_types, int, NULL, 0);
 154MODULE_PARM_DESC(bank1_types, "Bank1 sensortype autodetection override:\n"
 155	"   -1 autodetect\n"
 156	"    0 volt sensor\n"
 157	"    1 temp sensor\n"
 158	"    2 not connected");
 159static int fan_sensors;
 160module_param(fan_sensors, int, 0);
 161MODULE_PARM_DESC(fan_sensors, "Number of fan sensors on the uGuru "
 162	"(0 = autodetect)");
 163static int pwms;
 164module_param(pwms, int, 0);
 165MODULE_PARM_DESC(pwms, "Number of PWMs on the uGuru "
 166	"(0 = autodetect)");
 167
 168/* Default verbose is 2, since this driver is still in the testing phase */
 169static int verbose = 2;
 170module_param(verbose, int, 0644);
 171MODULE_PARM_DESC(verbose, "How verbose should the driver be? (0-3):\n"
 172	"   0 normal output\n"
 173	"   1 + verbose error reporting\n"
 174	"   2 + sensors type probing info\n"
 175	"   3 + retryable error reporting");
 176
 177
 178/* For the Abit uGuru, we need to keep some data in memory.
 179   The structure is dynamically allocated, at the same time when a new
 180   abituguru device is allocated. */
 
 
 181struct abituguru_data {
 182	struct device *hwmon_dev;	/* hwmon registered device */
 183	struct mutex update_lock;	/* protect access to data and uGuru */
 184	unsigned long last_updated;	/* In jiffies */
 185	unsigned short addr;		/* uguru base address */
 186	char uguru_ready;		/* is the uguru in ready state? */
 187	unsigned char update_timeouts;	/* number of update timeouts since last
 188					   successful update */
 189
 190	/* The sysfs attr and their names are generated automatically, for bank1
 191	   we cannot use a predefined array because we don't know beforehand
 192	   of a sensor is a volt or a temp sensor, for bank2 and the pwms its
 193	   easier todo things the same way.  For in sensors we have 9 (temp 7)
 194	   sysfs entries per sensor, for bank2 and pwms 6. */
 
 
 
 
 195	struct sensor_device_attribute_2 sysfs_attr[
 196		ABIT_UGURU_MAX_BANK1_SENSORS * 9 +
 197		ABIT_UGURU_MAX_BANK2_SENSORS * 6 + ABIT_UGURU_MAX_PWMS * 6];
 198	/* Buffer to store the dynamically generated sysfs names */
 199	char sysfs_names[ABITUGURU_SYSFS_NAMES_LENGTH];
 200
 201	/* Bank 1 data */
 202	/* number of and addresses of [0] in, [1] temp sensors */
 203	u8 bank1_sensors[2];
 204	u8 bank1_address[2][ABIT_UGURU_MAX_BANK1_SENSORS];
 205	u8 bank1_value[ABIT_UGURU_MAX_BANK1_SENSORS];
 206	/* This array holds 3 entries per sensor for the bank 1 sensor settings
 207	   (flags, min, max for voltage / flags, warn, shutdown for temp). */
 
 
 208	u8 bank1_settings[ABIT_UGURU_MAX_BANK1_SENSORS][3];
 209	/* Maximum value for each sensor used for scaling in mV/millidegrees
 210	   Celsius. */
 
 
 211	int bank1_max_value[ABIT_UGURU_MAX_BANK1_SENSORS];
 212
 213	/* Bank 2 data, ABIT_UGURU_MAX_BANK2_SENSORS entries for bank2 */
 214	u8 bank2_sensors; /* actual number of bank2 sensors found */
 215	u8 bank2_value[ABIT_UGURU_MAX_BANK2_SENSORS];
 216	u8 bank2_settings[ABIT_UGURU_MAX_BANK2_SENSORS][2]; /* flags, min */
 217
 218	/* Alarms 2 bytes for bank1, 1 byte for bank2 */
 219	u8 alarms[3];
 220
 221	/* Fan PWM (speed control) 5 bytes per PWM */
 222	u8 pwms; /* actual number of pwms found */
 223	u8 pwm_settings[ABIT_UGURU_MAX_PWMS][5];
 224};
 225
 226static const char *never_happen = "This should never happen.";
 227static const char *report_this =
 228	"Please report this to the abituguru maintainer (see MAINTAINERS)";
 229
 230/* wait till the uguru is in the specified state */
 231static int abituguru_wait(struct abituguru_data *data, u8 state)
 232{
 233	int timeout = ABIT_UGURU_WAIT_TIMEOUT;
 234
 235	while (inb_p(data->addr + ABIT_UGURU_DATA) != state) {
 236		timeout--;
 237		if (timeout == 0)
 238			return -EBUSY;
 239		/* sleep a bit before our last few tries, see the comment on
 240		   this where ABIT_UGURU_WAIT_TIMEOUT_SLEEP is defined. */
 
 
 241		if (timeout <= ABIT_UGURU_WAIT_TIMEOUT_SLEEP)
 242			msleep(0);
 243	}
 244	return 0;
 245}
 246
 247/* Put the uguru in ready for input state */
 248static int abituguru_ready(struct abituguru_data *data)
 249{
 250	int timeout = ABIT_UGURU_READY_TIMEOUT;
 251
 252	if (data->uguru_ready)
 253		return 0;
 254
 255	/* Reset? / Prepare for next read/write cycle */
 256	outb(0x00, data->addr + ABIT_UGURU_DATA);
 257
 258	/* Wait till the uguru is ready */
 259	if (abituguru_wait(data, ABIT_UGURU_STATUS_READY)) {
 260		ABIT_UGURU_DEBUG(1,
 261			"timeout exceeded waiting for ready state\n");
 262		return -EIO;
 263	}
 264
 265	/* Cmd port MUST be read now and should contain 0xAC */
 266	while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
 267		timeout--;
 268		if (timeout == 0) {
 269			ABIT_UGURU_DEBUG(1,
 270			   "CMD reg does not hold 0xAC after ready command\n");
 271			return -EIO;
 272		}
 273		msleep(0);
 274	}
 275
 276	/* After this the ABIT_UGURU_DATA port should contain
 277	   ABIT_UGURU_STATUS_INPUT */
 
 
 278	timeout = ABIT_UGURU_READY_TIMEOUT;
 279	while (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT) {
 280		timeout--;
 281		if (timeout == 0) {
 282			ABIT_UGURU_DEBUG(1,
 283				"state != more input after ready command\n");
 284			return -EIO;
 285		}
 286		msleep(0);
 287	}
 288
 289	data->uguru_ready = 1;
 290	return 0;
 291}
 292
 293/* Send the bank and then sensor address to the uGuru for the next read/write
 294   cycle. This function gets called as the first part of a read/write by
 295   abituguru_read and abituguru_write. This function should never be
 296   called by any other function. */
 
 
 297static int abituguru_send_address(struct abituguru_data *data,
 298	u8 bank_addr, u8 sensor_addr, int retries)
 299{
 300	/* assume the caller does error handling itself if it has not requested
 301	   any retries, and thus be quiet. */
 
 
 302	int report_errors = retries;
 303
 304	for (;;) {
 305		/* Make sure the uguru is ready and then send the bank address,
 306		   after this the uguru is no longer "ready". */
 
 
 307		if (abituguru_ready(data) != 0)
 308			return -EIO;
 309		outb(bank_addr, data->addr + ABIT_UGURU_DATA);
 310		data->uguru_ready = 0;
 311
 312		/* Wait till the uguru is ABIT_UGURU_STATUS_INPUT state again
 313		   and send the sensor addr */
 
 
 314		if (abituguru_wait(data, ABIT_UGURU_STATUS_INPUT)) {
 315			if (retries) {
 316				ABIT_UGURU_DEBUG(3, "timeout exceeded "
 317					"waiting for more input state, %d "
 318					"tries remaining\n", retries);
 319				set_current_state(TASK_UNINTERRUPTIBLE);
 320				schedule_timeout(ABIT_UGURU_RETRY_DELAY);
 321				retries--;
 322				continue;
 323			}
 324			if (report_errors)
 325				ABIT_UGURU_DEBUG(1, "timeout exceeded "
 326					"waiting for more input state "
 327					"(bank: %d)\n", (int)bank_addr);
 328			return -EBUSY;
 329		}
 330		outb(sensor_addr, data->addr + ABIT_UGURU_CMD);
 331		return 0;
 332	}
 333}
 334
 335/* Read count bytes from sensor sensor_addr in bank bank_addr and store the
 336   result in buf, retry the send address part of the read retries times. */
 
 
 337static int abituguru_read(struct abituguru_data *data,
 338	u8 bank_addr, u8 sensor_addr, u8 *buf, int count, int retries)
 339{
 340	int i;
 341
 342	/* Send the address */
 343	i = abituguru_send_address(data, bank_addr, sensor_addr, retries);
 344	if (i)
 345		return i;
 346
 347	/* And read the data */
 348	for (i = 0; i < count; i++) {
 349		if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
 350			ABIT_UGURU_DEBUG(retries ? 1 : 3,
 351				"timeout exceeded waiting for "
 352				"read state (bank: %d, sensor: %d)\n",
 353				(int)bank_addr, (int)sensor_addr);
 354			break;
 355		}
 356		buf[i] = inb(data->addr + ABIT_UGURU_CMD);
 357	}
 358
 359	/* Last put the chip back in ready state */
 360	abituguru_ready(data);
 361
 362	return i;
 363}
 364
 365/* Write count bytes from buf to sensor sensor_addr in bank bank_addr, the send
 366   address part of the write is always retried ABIT_UGURU_MAX_RETRIES times. */
 
 
 367static int abituguru_write(struct abituguru_data *data,
 368	u8 bank_addr, u8 sensor_addr, u8 *buf, int count)
 369{
 370	/* We use the ready timeout as we have to wait for 0xAC just like the
 371	   ready function */
 
 
 372	int i, timeout = ABIT_UGURU_READY_TIMEOUT;
 373
 374	/* Send the address */
 375	i = abituguru_send_address(data, bank_addr, sensor_addr,
 376		ABIT_UGURU_MAX_RETRIES);
 377	if (i)
 378		return i;
 379
 380	/* And write the data */
 381	for (i = 0; i < count; i++) {
 382		if (abituguru_wait(data, ABIT_UGURU_STATUS_WRITE)) {
 383			ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for "
 384				"write state (bank: %d, sensor: %d)\n",
 385				(int)bank_addr, (int)sensor_addr);
 386			break;
 387		}
 388		outb(buf[i], data->addr + ABIT_UGURU_CMD);
 389	}
 390
 391	/* Now we need to wait till the chip is ready to be read again,
 392	   so that we can read 0xAC as confirmation that our write has
 393	   succeeded. */
 
 
 394	if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
 395		ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for read state "
 396			"after write (bank: %d, sensor: %d)\n", (int)bank_addr,
 397			(int)sensor_addr);
 398		return -EIO;
 399	}
 400
 401	/* Cmd port MUST be read now and should contain 0xAC */
 402	while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
 403		timeout--;
 404		if (timeout == 0) {
 405			ABIT_UGURU_DEBUG(1, "CMD reg does not hold 0xAC after "
 406				"write (bank: %d, sensor: %d)\n",
 407				(int)bank_addr, (int)sensor_addr);
 408			return -EIO;
 409		}
 410		msleep(0);
 411	}
 412
 413	/* Last put the chip back in ready state */
 414	abituguru_ready(data);
 415
 416	return i;
 417}
 418
 419/* Detect sensor type. Temp and Volt sensors are enabled with
 420   different masks and will ignore enable masks not meant for them.
 421   This enables us to test what kind of sensor we're dealing with.
 422   By setting the alarm thresholds so that we will always get an
 423   alarm for sensor type X and then enabling the sensor as sensor type
 424   X, if we then get an alarm it is a sensor of type X. */
 
 
 425static int __devinit
 426abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
 427				   u8 sensor_addr)
 428{
 429	u8 val, test_flag, buf[3];
 430	int i, ret = -ENODEV; /* error is the most common used retval :| */
 431
 432	/* If overriden by the user return the user selected type */
 433	if (bank1_types[sensor_addr] >= ABIT_UGURU_IN_SENSOR &&
 434			bank1_types[sensor_addr] <= ABIT_UGURU_NC) {
 435		ABIT_UGURU_DEBUG(2, "assuming sensor type %d for bank1 sensor "
 436			"%d because of \"bank1_types\" module param\n",
 437			bank1_types[sensor_addr], (int)sensor_addr);
 438		return bank1_types[sensor_addr];
 439	}
 440
 441	/* First read the sensor and the current settings */
 442	if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, sensor_addr, &val,
 443			1, ABIT_UGURU_MAX_RETRIES) != 1)
 444		return -ENODEV;
 445
 446	/* Test val is sane / usable for sensor type detection. */
 447	if ((val < 10u) || (val > 250u)) {
 448		pr_warn("bank1-sensor: %d reading (%d) too close to limits, "
 449			"unable to determine sensor type, skipping sensor\n",
 450			(int)sensor_addr, (int)val);
 451		/* assume no sensor is there for sensors for which we can't
 452		   determine the sensor type because their reading is too close
 453		   to their limits, this usually means no sensor is there. */
 
 
 454		return ABIT_UGURU_NC;
 455	}
 456
 457	ABIT_UGURU_DEBUG(2, "testing bank1 sensor %d\n", (int)sensor_addr);
 458	/* Volt sensor test, enable volt low alarm, set min value ridicously
 459	   high, or vica versa if the reading is very high. If its a volt
 460	   sensor this should always give us an alarm. */
 
 
 461	if (val <= 240u) {
 462		buf[0] = ABIT_UGURU_VOLT_LOW_ALARM_ENABLE;
 463		buf[1] = 245;
 464		buf[2] = 250;
 465		test_flag = ABIT_UGURU_VOLT_LOW_ALARM_FLAG;
 466	} else {
 467		buf[0] = ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE;
 468		buf[1] = 5;
 469		buf[2] = 10;
 470		test_flag = ABIT_UGURU_VOLT_HIGH_ALARM_FLAG;
 471	}
 472
 473	if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
 474			buf, 3) != 3)
 475		goto abituguru_detect_bank1_sensor_type_exit;
 476	/* Now we need 20 ms to give the uguru time to read the sensors
 477	   and raise a voltage alarm */
 
 
 478	set_current_state(TASK_UNINTERRUPTIBLE);
 479	schedule_timeout(HZ/50);
 480	/* Check for alarm and check the alarm is a volt low alarm. */
 481	if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
 482			ABIT_UGURU_MAX_RETRIES) != 3)
 483		goto abituguru_detect_bank1_sensor_type_exit;
 484	if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
 485		if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
 486				sensor_addr, buf, 3,
 487				ABIT_UGURU_MAX_RETRIES) != 3)
 488			goto abituguru_detect_bank1_sensor_type_exit;
 489		if (buf[0] & test_flag) {
 490			ABIT_UGURU_DEBUG(2, "  found volt sensor\n");
 491			ret = ABIT_UGURU_IN_SENSOR;
 492			goto abituguru_detect_bank1_sensor_type_exit;
 493		} else
 494			ABIT_UGURU_DEBUG(2, "  alarm raised during volt "
 495				"sensor test, but volt range flag not set\n");
 496	} else
 497		ABIT_UGURU_DEBUG(2, "  alarm not raised during volt sensor "
 498			"test\n");
 499
 500	/* Temp sensor test, enable sensor as a temp sensor, set beep value
 501	   ridicously low (but not too low, otherwise uguru ignores it).
 502	   If its a temp sensor this should always give us an alarm. */
 
 
 503	buf[0] = ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE;
 504	buf[1] = 5;
 505	buf[2] = 10;
 506	if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
 507			buf, 3) != 3)
 508		goto abituguru_detect_bank1_sensor_type_exit;
 509	/* Now we need 50 ms to give the uguru time to read the sensors
 510	   and raise a temp alarm */
 
 
 511	set_current_state(TASK_UNINTERRUPTIBLE);
 512	schedule_timeout(HZ/20);
 513	/* Check for alarm and check the alarm is a temp high alarm. */
 514	if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
 515			ABIT_UGURU_MAX_RETRIES) != 3)
 516		goto abituguru_detect_bank1_sensor_type_exit;
 517	if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
 518		if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
 519				sensor_addr, buf, 3,
 520				ABIT_UGURU_MAX_RETRIES) != 3)
 521			goto abituguru_detect_bank1_sensor_type_exit;
 522		if (buf[0] & ABIT_UGURU_TEMP_HIGH_ALARM_FLAG) {
 523			ABIT_UGURU_DEBUG(2, "  found temp sensor\n");
 524			ret = ABIT_UGURU_TEMP_SENSOR;
 525			goto abituguru_detect_bank1_sensor_type_exit;
 526		} else
 527			ABIT_UGURU_DEBUG(2, "  alarm raised during temp "
 528				"sensor test, but temp high flag not set\n");
 529	} else
 530		ABIT_UGURU_DEBUG(2, "  alarm not raised during temp sensor "
 531			"test\n");
 532
 533	ret = ABIT_UGURU_NC;
 534abituguru_detect_bank1_sensor_type_exit:
 535	/* Restore original settings, failing here is really BAD, it has been
 536	   reported that some BIOS-es hang when entering the uGuru menu with
 537	   invalid settings present in the uGuru, so we try this 3 times. */
 
 
 538	for (i = 0; i < 3; i++)
 539		if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
 540				sensor_addr, data->bank1_settings[sensor_addr],
 541				3) == 3)
 542			break;
 543	if (i == 3) {
 544		pr_err("Fatal error could not restore original settings. %s %s\n",
 545		       never_happen, report_this);
 546		return -ENODEV;
 547	}
 548	return ret;
 549}
 550
 551/* These functions try to find out how many sensors there are in bank2 and how
 552   many pwms there are. The purpose of this is to make sure that we don't give
 553   the user the possibility to change settings for non-existent sensors / pwm.
 554   The uGuru will happily read / write whatever memory happens to be after the
 555   memory storing the PWM settings when reading/writing to a PWM which is not
 556   there. Notice even if we detect a PWM which doesn't exist we normally won't
 557   write to it, unless the user tries to change the settings.
 558
 559   Although the uGuru allows reading (settings) from non existing bank2
 560   sensors, my version of the uGuru does seem to stop writing to them, the
 561   write function above aborts in this case with:
 562   "CMD reg does not hold 0xAC after write"
 563
 564   Notice these 2 tests are non destructive iow read-only tests, otherwise
 565   they would defeat their purpose. Although for the bank2_sensors detection a
 566   read/write test would be feasible because of the reaction above, I've
 567   however opted to stay on the safe side. */
 
 
 568static void __devinit
 569abituguru_detect_no_bank2_sensors(struct abituguru_data *data)
 570{
 571	int i;
 572
 573	if (fan_sensors > 0 && fan_sensors <= ABIT_UGURU_MAX_BANK2_SENSORS) {
 574		data->bank2_sensors = fan_sensors;
 575		ABIT_UGURU_DEBUG(2, "assuming %d fan sensors because of "
 576			"\"fan_sensors\" module param\n",
 577			(int)data->bank2_sensors);
 578		return;
 579	}
 580
 581	ABIT_UGURU_DEBUG(2, "detecting number of fan sensors\n");
 582	for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
 583		/* 0x89 are the known used bits:
 584		   -0x80 enable shutdown
 585		   -0x08 enable beep
 586		   -0x01 enable alarm
 587		   All other bits should be 0, but on some motherboards
 588		   0x40 (bit 6) is also high for some of the fans?? */
 
 
 589		if (data->bank2_settings[i][0] & ~0xC9) {
 590			ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
 591				"to be a fan sensor: settings[0] = %02X\n",
 592				i, (unsigned int)data->bank2_settings[i][0]);
 593			break;
 594		}
 595
 596		/* check if the threshold is within the allowed range */
 597		if (data->bank2_settings[i][1] <
 598				abituguru_bank2_min_threshold) {
 599			ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
 600				"to be a fan sensor: the threshold (%d) is "
 601				"below the minimum (%d)\n", i,
 602				(int)data->bank2_settings[i][1],
 603				(int)abituguru_bank2_min_threshold);
 604			break;
 605		}
 606		if (data->bank2_settings[i][1] >
 607				abituguru_bank2_max_threshold) {
 608			ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
 609				"to be a fan sensor: the threshold (%d) is "
 610				"above the maximum (%d)\n", i,
 611				(int)data->bank2_settings[i][1],
 612				(int)abituguru_bank2_max_threshold);
 613			break;
 614		}
 615	}
 616
 617	data->bank2_sensors = i;
 618	ABIT_UGURU_DEBUG(2, " found: %d fan sensors\n",
 619		(int)data->bank2_sensors);
 620}
 621
 622static void __devinit
 623abituguru_detect_no_pwms(struct abituguru_data *data)
 624{
 625	int i, j;
 626
 627	if (pwms > 0 && pwms <= ABIT_UGURU_MAX_PWMS) {
 628		data->pwms = pwms;
 629		ABIT_UGURU_DEBUG(2, "assuming %d PWM outputs because of "
 630			"\"pwms\" module param\n", (int)data->pwms);
 631		return;
 632	}
 633
 634	ABIT_UGURU_DEBUG(2, "detecting number of PWM outputs\n");
 635	for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
 636		/* 0x80 is the enable bit and the low
 637		   nibble is which temp sensor to use,
 638		   the other bits should be 0 */
 
 
 639		if (data->pwm_settings[i][0] & ~0x8F) {
 640			ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
 641				"to be a pwm channel: settings[0] = %02X\n",
 642				i, (unsigned int)data->pwm_settings[i][0]);
 643			break;
 644		}
 645
 646		/* the low nibble must correspond to one of the temp sensors
 647		   we've found */
 
 
 648		for (j = 0; j < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR];
 649				j++) {
 650			if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][j] ==
 651					(data->pwm_settings[i][0] & 0x0F))
 652				break;
 653		}
 654		if (j == data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
 655			ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
 656				"to be a pwm channel: %d is not a valid temp "
 657				"sensor address\n", i,
 658				data->pwm_settings[i][0] & 0x0F);
 659			break;
 660		}
 661
 662		/* check if all other settings are within the allowed range */
 663		for (j = 1; j < 5; j++) {
 664			u8 min;
 665			/* special case pwm1 min pwm% */
 666			if ((i == 0) && ((j == 1) || (j == 2)))
 667				min = 77;
 668			else
 669				min = abituguru_pwm_min[j];
 670			if (data->pwm_settings[i][j] < min) {
 671				ABIT_UGURU_DEBUG(2, "  pwm channel %d does "
 672					"not seem to be a pwm channel: "
 673					"setting %d (%d) is below the minimum "
 674					"value (%d)\n", i, j,
 675					(int)data->pwm_settings[i][j],
 676					(int)min);
 677				goto abituguru_detect_no_pwms_exit;
 678			}
 679			if (data->pwm_settings[i][j] > abituguru_pwm_max[j]) {
 680				ABIT_UGURU_DEBUG(2, "  pwm channel %d does "
 681					"not seem to be a pwm channel: "
 682					"setting %d (%d) is above the maximum "
 683					"value (%d)\n", i, j,
 684					(int)data->pwm_settings[i][j],
 685					(int)abituguru_pwm_max[j]);
 686				goto abituguru_detect_no_pwms_exit;
 687			}
 688		}
 689
 690		/* check that min temp < max temp and min pwm < max pwm */
 691		if (data->pwm_settings[i][1] >= data->pwm_settings[i][2]) {
 692			ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
 693				"to be a pwm channel: min pwm (%d) >= "
 694				"max pwm (%d)\n", i,
 695				(int)data->pwm_settings[i][1],
 696				(int)data->pwm_settings[i][2]);
 697			break;
 698		}
 699		if (data->pwm_settings[i][3] >= data->pwm_settings[i][4]) {
 700			ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
 701				"to be a pwm channel: min temp (%d) >= "
 702				"max temp (%d)\n", i,
 703				(int)data->pwm_settings[i][3],
 704				(int)data->pwm_settings[i][4]);
 705			break;
 706		}
 707	}
 708
 709abituguru_detect_no_pwms_exit:
 710	data->pwms = i;
 711	ABIT_UGURU_DEBUG(2, " found: %d PWM outputs\n", (int)data->pwms);
 712}
 713
 714/* Following are the sysfs callback functions. These functions expect:
 715   sensor_device_attribute_2->index:   sensor address/offset in the bank
 716   sensor_device_attribute_2->nr:      register offset, bitmask or NA. */
 
 
 717static struct abituguru_data *abituguru_update_device(struct device *dev);
 718
 719static ssize_t show_bank1_value(struct device *dev,
 720	struct device_attribute *devattr, char *buf)
 721{
 722	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 723	struct abituguru_data *data = abituguru_update_device(dev);
 724	if (!data)
 725		return -EIO;
 726	return sprintf(buf, "%d\n", (data->bank1_value[attr->index] *
 727		data->bank1_max_value[attr->index] + 128) / 255);
 728}
 729
 730static ssize_t show_bank1_setting(struct device *dev,
 731	struct device_attribute *devattr, char *buf)
 732{
 733	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 734	struct abituguru_data *data = dev_get_drvdata(dev);
 735	return sprintf(buf, "%d\n",
 736		(data->bank1_settings[attr->index][attr->nr] *
 737		data->bank1_max_value[attr->index] + 128) / 255);
 738}
 739
 740static ssize_t show_bank2_value(struct device *dev,
 741	struct device_attribute *devattr, char *buf)
 742{
 743	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 744	struct abituguru_data *data = abituguru_update_device(dev);
 745	if (!data)
 746		return -EIO;
 747	return sprintf(buf, "%d\n", (data->bank2_value[attr->index] *
 748		ABIT_UGURU_FAN_MAX + 128) / 255);
 749}
 750
 751static ssize_t show_bank2_setting(struct device *dev,
 752	struct device_attribute *devattr, char *buf)
 753{
 754	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 755	struct abituguru_data *data = dev_get_drvdata(dev);
 756	return sprintf(buf, "%d\n",
 757		(data->bank2_settings[attr->index][attr->nr] *
 758		ABIT_UGURU_FAN_MAX + 128) / 255);
 759}
 760
 761static ssize_t store_bank1_setting(struct device *dev, struct device_attribute
 762	*devattr, const char *buf, size_t count)
 763{
 764	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 765	struct abituguru_data *data = dev_get_drvdata(dev);
 766	u8 val = (simple_strtoul(buf, NULL, 10) * 255 +
 767		data->bank1_max_value[attr->index]/2) /
 
 
 
 
 
 
 
 768		data->bank1_max_value[attr->index];
 769	ssize_t ret = count;
 
 770
 771	mutex_lock(&data->update_lock);
 772	if (data->bank1_settings[attr->index][attr->nr] != val) {
 773		u8 orig_val = data->bank1_settings[attr->index][attr->nr];
 774		data->bank1_settings[attr->index][attr->nr] = val;
 775		if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
 776				attr->index, data->bank1_settings[attr->index],
 777				3) <= attr->nr) {
 778			data->bank1_settings[attr->index][attr->nr] = orig_val;
 779			ret = -EIO;
 780		}
 781	}
 782	mutex_unlock(&data->update_lock);
 783	return ret;
 784}
 785
 786static ssize_t store_bank2_setting(struct device *dev, struct device_attribute
 787	*devattr, const char *buf, size_t count)
 788{
 789	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 790	struct abituguru_data *data = dev_get_drvdata(dev);
 791	u8 val = (simple_strtoul(buf, NULL, 10)*255 + ABIT_UGURU_FAN_MAX/2) /
 792		ABIT_UGURU_FAN_MAX;
 793	ssize_t ret = count;
 
 
 
 
 
 
 794
 795	/* this check can be done before taking the lock */
 796	if ((val < abituguru_bank2_min_threshold) ||
 797			(val > abituguru_bank2_max_threshold))
 798		return -EINVAL;
 799
 800	mutex_lock(&data->update_lock);
 801	if (data->bank2_settings[attr->index][attr->nr] != val) {
 802		u8 orig_val = data->bank2_settings[attr->index][attr->nr];
 803		data->bank2_settings[attr->index][attr->nr] = val;
 804		if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK2 + 2,
 805				attr->index, data->bank2_settings[attr->index],
 806				2) <= attr->nr) {
 807			data->bank2_settings[attr->index][attr->nr] = orig_val;
 808			ret = -EIO;
 809		}
 810	}
 811	mutex_unlock(&data->update_lock);
 812	return ret;
 813}
 814
 815static ssize_t show_bank1_alarm(struct device *dev,
 816	struct device_attribute *devattr, char *buf)
 817{
 818	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 819	struct abituguru_data *data = abituguru_update_device(dev);
 820	if (!data)
 821		return -EIO;
 822	/* See if the alarm bit for this sensor is set, and if the
 823	   alarm matches the type of alarm we're looking for (for volt
 824	   it can be either low or high). The type is stored in a few
 825	   readonly bits in the settings part of the relevant sensor.
 826	   The bitmask of the type is passed to us in attr->nr. */
 
 
 827	if ((data->alarms[attr->index / 8] & (0x01 << (attr->index % 8))) &&
 828			(data->bank1_settings[attr->index][0] & attr->nr))
 829		return sprintf(buf, "1\n");
 830	else
 831		return sprintf(buf, "0\n");
 832}
 833
 834static ssize_t show_bank2_alarm(struct device *dev,
 835	struct device_attribute *devattr, char *buf)
 836{
 837	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 838	struct abituguru_data *data = abituguru_update_device(dev);
 839	if (!data)
 840		return -EIO;
 841	if (data->alarms[2] & (0x01 << attr->index))
 842		return sprintf(buf, "1\n");
 843	else
 844		return sprintf(buf, "0\n");
 845}
 846
 847static ssize_t show_bank1_mask(struct device *dev,
 848	struct device_attribute *devattr, char *buf)
 849{
 850	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 851	struct abituguru_data *data = dev_get_drvdata(dev);
 852	if (data->bank1_settings[attr->index][0] & attr->nr)
 853		return sprintf(buf, "1\n");
 854	else
 855		return sprintf(buf, "0\n");
 856}
 857
 858static ssize_t show_bank2_mask(struct device *dev,
 859	struct device_attribute *devattr, char *buf)
 860{
 861	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 862	struct abituguru_data *data = dev_get_drvdata(dev);
 863	if (data->bank2_settings[attr->index][0] & attr->nr)
 864		return sprintf(buf, "1\n");
 865	else
 866		return sprintf(buf, "0\n");
 867}
 868
 869static ssize_t store_bank1_mask(struct device *dev,
 870	struct device_attribute *devattr, const char *buf, size_t count)
 871{
 872	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 873	struct abituguru_data *data = dev_get_drvdata(dev);
 874	int mask = simple_strtoul(buf, NULL, 10);
 875	ssize_t ret = count;
 876	u8 orig_val;
 
 
 
 
 
 877
 
 878	mutex_lock(&data->update_lock);
 879	orig_val = data->bank1_settings[attr->index][0];
 880
 881	if (mask)
 882		data->bank1_settings[attr->index][0] |= attr->nr;
 883	else
 884		data->bank1_settings[attr->index][0] &= ~attr->nr;
 885
 886	if ((data->bank1_settings[attr->index][0] != orig_val) &&
 887			(abituguru_write(data,
 888			ABIT_UGURU_SENSOR_BANK1 + 2, attr->index,
 889			data->bank1_settings[attr->index], 3) < 1)) {
 890		data->bank1_settings[attr->index][0] = orig_val;
 891		ret = -EIO;
 892	}
 893	mutex_unlock(&data->update_lock);
 894	return ret;
 895}
 896
 897static ssize_t store_bank2_mask(struct device *dev,
 898	struct device_attribute *devattr, const char *buf, size_t count)
 899{
 900	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 901	struct abituguru_data *data = dev_get_drvdata(dev);
 902	int mask = simple_strtoul(buf, NULL, 10);
 903	ssize_t ret = count;
 904	u8 orig_val;
 
 
 
 
 
 905
 
 906	mutex_lock(&data->update_lock);
 907	orig_val = data->bank2_settings[attr->index][0];
 908
 909	if (mask)
 910		data->bank2_settings[attr->index][0] |= attr->nr;
 911	else
 912		data->bank2_settings[attr->index][0] &= ~attr->nr;
 913
 914	if ((data->bank2_settings[attr->index][0] != orig_val) &&
 915			(abituguru_write(data,
 916			ABIT_UGURU_SENSOR_BANK2 + 2, attr->index,
 917			data->bank2_settings[attr->index], 2) < 1)) {
 918		data->bank2_settings[attr->index][0] = orig_val;
 919		ret = -EIO;
 920	}
 921	mutex_unlock(&data->update_lock);
 922	return ret;
 923}
 924
 925/* Fan PWM (speed control) */
 926static ssize_t show_pwm_setting(struct device *dev,
 927	struct device_attribute *devattr, char *buf)
 928{
 929	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 930	struct abituguru_data *data = dev_get_drvdata(dev);
 931	return sprintf(buf, "%d\n", data->pwm_settings[attr->index][attr->nr] *
 932		abituguru_pwm_settings_multiplier[attr->nr]);
 933}
 934
 935static ssize_t store_pwm_setting(struct device *dev, struct device_attribute
 936	*devattr, const char *buf, size_t count)
 937{
 938	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 939	struct abituguru_data *data = dev_get_drvdata(dev);
 940	u8 min, val = (simple_strtoul(buf, NULL, 10) +
 941		abituguru_pwm_settings_multiplier[attr->nr]/2) /
 942		abituguru_pwm_settings_multiplier[attr->nr];
 943	ssize_t ret = count;
 
 
 
 
 
 
 
 944
 945	/* special case pwm1 min pwm% */
 946	if ((attr->index == 0) && ((attr->nr == 1) || (attr->nr == 2)))
 947		min = 77;
 948	else
 949		min = abituguru_pwm_min[attr->nr];
 950
 951	/* this check can be done before taking the lock */
 952	if ((val < min) || (val > abituguru_pwm_max[attr->nr]))
 953		return -EINVAL;
 954
 955	mutex_lock(&data->update_lock);
 956	/* this check needs to be done after taking the lock */
 957	if ((attr->nr & 1) &&
 958			(val >= data->pwm_settings[attr->index][attr->nr + 1]))
 959		ret = -EINVAL;
 960	else if (!(attr->nr & 1) &&
 961			(val <= data->pwm_settings[attr->index][attr->nr - 1]))
 962		ret = -EINVAL;
 963	else if (data->pwm_settings[attr->index][attr->nr] != val) {
 964		u8 orig_val = data->pwm_settings[attr->index][attr->nr];
 965		data->pwm_settings[attr->index][attr->nr] = val;
 966		if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
 967				attr->index, data->pwm_settings[attr->index],
 968				5) <= attr->nr) {
 969			data->pwm_settings[attr->index][attr->nr] =
 970				orig_val;
 971			ret = -EIO;
 972		}
 973	}
 974	mutex_unlock(&data->update_lock);
 975	return ret;
 976}
 977
 978static ssize_t show_pwm_sensor(struct device *dev,
 979	struct device_attribute *devattr, char *buf)
 980{
 981	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 982	struct abituguru_data *data = dev_get_drvdata(dev);
 983	int i;
 984	/* We need to walk to the temp sensor addresses to find what
 985	   the userspace id of the configured temp sensor is. */
 
 
 986	for (i = 0; i < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]; i++)
 987		if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][i] ==
 988				(data->pwm_settings[attr->index][0] & 0x0F))
 989			return sprintf(buf, "%d\n", i+1);
 990
 991	return -ENXIO;
 992}
 993
 994static ssize_t store_pwm_sensor(struct device *dev, struct device_attribute
 995	*devattr, const char *buf, size_t count)
 996{
 997	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 998	struct abituguru_data *data = dev_get_drvdata(dev);
 999	unsigned long val = simple_strtoul(buf, NULL, 10) - 1;
1000	ssize_t ret = count;
 
 
 
 
 
 
1001
 
 
 
 
 
1002	mutex_lock(&data->update_lock);
1003	if (val < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
1004		u8 orig_val = data->pwm_settings[attr->index][0];
1005		u8 address = data->bank1_address[ABIT_UGURU_TEMP_SENSOR][val];
1006		data->pwm_settings[attr->index][0] &= 0xF0;
1007		data->pwm_settings[attr->index][0] |= address;
1008		if (data->pwm_settings[attr->index][0] != orig_val) {
1009			if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
1010					attr->index,
1011					data->pwm_settings[attr->index],
1012					5) < 1) {
1013				data->pwm_settings[attr->index][0] = orig_val;
1014				ret = -EIO;
1015			}
1016		}
1017	}
1018	else
1019		ret = -EINVAL;
1020	mutex_unlock(&data->update_lock);
1021	return ret;
1022}
1023
1024static ssize_t show_pwm_enable(struct device *dev,
1025	struct device_attribute *devattr, char *buf)
1026{
1027	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1028	struct abituguru_data *data = dev_get_drvdata(dev);
1029	int res = 0;
1030	if (data->pwm_settings[attr->index][0] & ABIT_UGURU_FAN_PWM_ENABLE)
1031		res = 2;
1032	return sprintf(buf, "%d\n", res);
1033}
1034
1035static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
1036	*devattr, const char *buf, size_t count)
1037{
1038	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1039	struct abituguru_data *data = dev_get_drvdata(dev);
1040	u8 orig_val, user_val = simple_strtoul(buf, NULL, 10);
1041	ssize_t ret = count;
 
 
 
 
 
1042
 
1043	mutex_lock(&data->update_lock);
1044	orig_val = data->pwm_settings[attr->index][0];
1045	switch (user_val) {
1046		case 0:
1047			data->pwm_settings[attr->index][0] &=
1048				~ABIT_UGURU_FAN_PWM_ENABLE;
1049			break;
1050		case 2:
1051			data->pwm_settings[attr->index][0] |=
1052				ABIT_UGURU_FAN_PWM_ENABLE;
1053			break;
1054		default:
1055			ret = -EINVAL;
1056	}
1057	if ((data->pwm_settings[attr->index][0] != orig_val) &&
1058			(abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
1059			attr->index, data->pwm_settings[attr->index],
1060			5) < 1)) {
1061		data->pwm_settings[attr->index][0] = orig_val;
1062		ret = -EIO;
1063	}
1064	mutex_unlock(&data->update_lock);
1065	return ret;
1066}
1067
1068static ssize_t show_name(struct device *dev,
1069	struct device_attribute *devattr, char *buf)
1070{
1071	return sprintf(buf, "%s\n", ABIT_UGURU_NAME);
1072}
1073
1074/* Sysfs attr templates, the real entries are generated automatically. */
1075static const
1076struct sensor_device_attribute_2 abituguru_sysfs_bank1_templ[2][9] = {
1077	{
1078	SENSOR_ATTR_2(in%d_input, 0444, show_bank1_value, NULL, 0, 0),
1079	SENSOR_ATTR_2(in%d_min, 0644, show_bank1_setting,
1080		store_bank1_setting, 1, 0),
1081	SENSOR_ATTR_2(in%d_min_alarm, 0444, show_bank1_alarm, NULL,
1082		ABIT_UGURU_VOLT_LOW_ALARM_FLAG, 0),
1083	SENSOR_ATTR_2(in%d_max, 0644, show_bank1_setting,
1084		store_bank1_setting, 2, 0),
1085	SENSOR_ATTR_2(in%d_max_alarm, 0444, show_bank1_alarm, NULL,
1086		ABIT_UGURU_VOLT_HIGH_ALARM_FLAG, 0),
1087	SENSOR_ATTR_2(in%d_beep, 0644, show_bank1_mask,
1088		store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1089	SENSOR_ATTR_2(in%d_shutdown, 0644, show_bank1_mask,
1090		store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1091	SENSOR_ATTR_2(in%d_min_alarm_enable, 0644, show_bank1_mask,
1092		store_bank1_mask, ABIT_UGURU_VOLT_LOW_ALARM_ENABLE, 0),
1093	SENSOR_ATTR_2(in%d_max_alarm_enable, 0644, show_bank1_mask,
1094		store_bank1_mask, ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE, 0),
1095	}, {
1096	SENSOR_ATTR_2(temp%d_input, 0444, show_bank1_value, NULL, 0, 0),
1097	SENSOR_ATTR_2(temp%d_alarm, 0444, show_bank1_alarm, NULL,
1098		ABIT_UGURU_TEMP_HIGH_ALARM_FLAG, 0),
1099	SENSOR_ATTR_2(temp%d_max, 0644, show_bank1_setting,
1100		store_bank1_setting, 1, 0),
1101	SENSOR_ATTR_2(temp%d_crit, 0644, show_bank1_setting,
1102		store_bank1_setting, 2, 0),
1103	SENSOR_ATTR_2(temp%d_beep, 0644, show_bank1_mask,
1104		store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1105	SENSOR_ATTR_2(temp%d_shutdown, 0644, show_bank1_mask,
1106		store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1107	SENSOR_ATTR_2(temp%d_alarm_enable, 0644, show_bank1_mask,
1108		store_bank1_mask, ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE, 0),
1109	}
1110};
1111
1112static const struct sensor_device_attribute_2 abituguru_sysfs_fan_templ[6] = {
1113	SENSOR_ATTR_2(fan%d_input, 0444, show_bank2_value, NULL, 0, 0),
1114	SENSOR_ATTR_2(fan%d_alarm, 0444, show_bank2_alarm, NULL, 0, 0),
1115	SENSOR_ATTR_2(fan%d_min, 0644, show_bank2_setting,
1116		store_bank2_setting, 1, 0),
1117	SENSOR_ATTR_2(fan%d_beep, 0644, show_bank2_mask,
1118		store_bank2_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1119	SENSOR_ATTR_2(fan%d_shutdown, 0644, show_bank2_mask,
1120		store_bank2_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1121	SENSOR_ATTR_2(fan%d_alarm_enable, 0644, show_bank2_mask,
1122		store_bank2_mask, ABIT_UGURU_FAN_LOW_ALARM_ENABLE, 0),
1123};
1124
1125static const struct sensor_device_attribute_2 abituguru_sysfs_pwm_templ[6] = {
1126	SENSOR_ATTR_2(pwm%d_enable, 0644, show_pwm_enable,
1127		store_pwm_enable, 0, 0),
1128	SENSOR_ATTR_2(pwm%d_auto_channels_temp, 0644, show_pwm_sensor,
1129		store_pwm_sensor, 0, 0),
1130	SENSOR_ATTR_2(pwm%d_auto_point1_pwm, 0644, show_pwm_setting,
1131		store_pwm_setting, 1, 0),
1132	SENSOR_ATTR_2(pwm%d_auto_point2_pwm, 0644, show_pwm_setting,
1133		store_pwm_setting, 2, 0),
1134	SENSOR_ATTR_2(pwm%d_auto_point1_temp, 0644, show_pwm_setting,
1135		store_pwm_setting, 3, 0),
1136	SENSOR_ATTR_2(pwm%d_auto_point2_temp, 0644, show_pwm_setting,
1137		store_pwm_setting, 4, 0),
1138};
1139
1140static struct sensor_device_attribute_2 abituguru_sysfs_attr[] = {
1141	SENSOR_ATTR_2(name, 0444, show_name, NULL, 0, 0),
1142};
1143
1144static int __devinit abituguru_probe(struct platform_device *pdev)
1145{
1146	struct abituguru_data *data;
1147	int i, j, used, sysfs_names_free, sysfs_attr_i, res = -ENODEV;
1148	char *sysfs_filename;
1149
1150	/* El weirdo probe order, to keep the sysfs order identical to the
1151	   BIOS and window-appliction listing order. */
 
 
1152	const u8 probe_order[ABIT_UGURU_MAX_BANK1_SENSORS] = {
1153		0x00, 0x01, 0x03, 0x04, 0x0A, 0x08, 0x0E, 0x02,
1154		0x09, 0x06, 0x05, 0x0B, 0x0F, 0x0D, 0x07, 0x0C };
1155
1156	if (!(data = kzalloc(sizeof(struct abituguru_data), GFP_KERNEL)))
 
1157		return -ENOMEM;
1158
1159	data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
1160	mutex_init(&data->update_lock);
1161	platform_set_drvdata(pdev, data);
1162
1163	/* See if the uGuru is ready */
1164	if (inb_p(data->addr + ABIT_UGURU_DATA) == ABIT_UGURU_STATUS_INPUT)
1165		data->uguru_ready = 1;
1166
1167	/* Completely read the uGuru this has 2 purposes:
1168	   - testread / see if one really is there.
1169	   - make an in memory copy of all the uguru settings for future use. */
 
 
1170	if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1171			data->alarms, 3, ABIT_UGURU_MAX_RETRIES) != 3)
1172		goto abituguru_probe_error;
1173
1174	for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1175		if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, i,
1176				&data->bank1_value[i], 1,
1177				ABIT_UGURU_MAX_RETRIES) != 1)
1178			goto abituguru_probe_error;
1179		if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1+1, i,
1180				data->bank1_settings[i], 3,
1181				ABIT_UGURU_MAX_RETRIES) != 3)
1182			goto abituguru_probe_error;
1183	}
1184	/* Note: We don't know how many bank2 sensors / pwms there really are,
1185	   but in order to "detect" this we need to read the maximum amount
1186	   anyways. If we read sensors/pwms not there we'll just read crap
1187	   this can't hurt. We need the detection because we don't want
1188	   unwanted writes, which will hurt! */
 
 
1189	for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
1190		if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
1191				&data->bank2_value[i], 1,
1192				ABIT_UGURU_MAX_RETRIES) != 1)
1193			goto abituguru_probe_error;
1194		if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2+1, i,
1195				data->bank2_settings[i], 2,
1196				ABIT_UGURU_MAX_RETRIES) != 2)
1197			goto abituguru_probe_error;
1198	}
1199	for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
1200		if (abituguru_read(data, ABIT_UGURU_FAN_PWM, i,
1201				data->pwm_settings[i], 5,
1202				ABIT_UGURU_MAX_RETRIES) != 5)
1203			goto abituguru_probe_error;
1204	}
1205	data->last_updated = jiffies;
1206
1207	/* Detect sensor types and fill the sysfs attr for bank1 */
1208	sysfs_attr_i = 0;
1209	sysfs_filename = data->sysfs_names;
1210	sysfs_names_free = ABITUGURU_SYSFS_NAMES_LENGTH;
1211	for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1212		res = abituguru_detect_bank1_sensor_type(data, probe_order[i]);
1213		if (res < 0)
1214			goto abituguru_probe_error;
1215		if (res == ABIT_UGURU_NC)
1216			continue;
1217
1218		/* res 1 (temp) sensors have 7 sysfs entries, 0 (in) 9 */
1219		for (j = 0; j < (res ? 7 : 9); j++) {
1220			used = snprintf(sysfs_filename, sysfs_names_free,
1221				abituguru_sysfs_bank1_templ[res][j].dev_attr.
1222				attr.name, data->bank1_sensors[res] + res)
1223				+ 1;
1224			data->sysfs_attr[sysfs_attr_i] =
1225				abituguru_sysfs_bank1_templ[res][j];
1226			data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1227				sysfs_filename;
1228			data->sysfs_attr[sysfs_attr_i].index = probe_order[i];
1229			sysfs_filename += used;
1230			sysfs_names_free -= used;
1231			sysfs_attr_i++;
1232		}
1233		data->bank1_max_value[probe_order[i]] =
1234			abituguru_bank1_max_value[res];
1235		data->bank1_address[res][data->bank1_sensors[res]] =
1236			probe_order[i];
1237		data->bank1_sensors[res]++;
1238	}
1239	/* Detect number of sensors and fill the sysfs attr for bank2 (fans) */
1240	abituguru_detect_no_bank2_sensors(data);
1241	for (i = 0; i < data->bank2_sensors; i++) {
1242		for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_fan_templ); j++) {
1243			used = snprintf(sysfs_filename, sysfs_names_free,
1244				abituguru_sysfs_fan_templ[j].dev_attr.attr.name,
1245				i + 1) + 1;
1246			data->sysfs_attr[sysfs_attr_i] =
1247				abituguru_sysfs_fan_templ[j];
1248			data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1249				sysfs_filename;
1250			data->sysfs_attr[sysfs_attr_i].index = i;
1251			sysfs_filename += used;
1252			sysfs_names_free -= used;
1253			sysfs_attr_i++;
1254		}
1255	}
1256	/* Detect number of sensors and fill the sysfs attr for pwms */
1257	abituguru_detect_no_pwms(data);
1258	for (i = 0; i < data->pwms; i++) {
1259		for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_pwm_templ); j++) {
1260			used = snprintf(sysfs_filename, sysfs_names_free,
1261				abituguru_sysfs_pwm_templ[j].dev_attr.attr.name,
1262				i + 1) + 1;
1263			data->sysfs_attr[sysfs_attr_i] =
1264				abituguru_sysfs_pwm_templ[j];
1265			data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1266				sysfs_filename;
1267			data->sysfs_attr[sysfs_attr_i].index = i;
1268			sysfs_filename += used;
1269			sysfs_names_free -= used;
1270			sysfs_attr_i++;
1271		}
1272	}
1273	/* Fail safe check, this should never happen! */
1274	if (sysfs_names_free < 0) {
1275		pr_err("Fatal error ran out of space for sysfs attr names. %s %s",
1276		       never_happen, report_this);
1277		res = -ENAMETOOLONG;
1278		goto abituguru_probe_error;
1279	}
1280	pr_info("found Abit uGuru\n");
1281
1282	/* Register sysfs hooks */
1283	for (i = 0; i < sysfs_attr_i; i++)
1284		if (device_create_file(&pdev->dev,
1285				&data->sysfs_attr[i].dev_attr))
1286			goto abituguru_probe_error;
1287	for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1288		if (device_create_file(&pdev->dev,
1289				&abituguru_sysfs_attr[i].dev_attr))
1290			goto abituguru_probe_error;
1291
1292	data->hwmon_dev = hwmon_device_register(&pdev->dev);
1293	if (!IS_ERR(data->hwmon_dev))
1294		return 0; /* success */
1295
1296	res = PTR_ERR(data->hwmon_dev);
1297abituguru_probe_error:
1298	for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
1299		device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
1300	for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1301		device_remove_file(&pdev->dev,
1302			&abituguru_sysfs_attr[i].dev_attr);
1303	platform_set_drvdata(pdev, NULL);
1304	kfree(data);
1305	return res;
1306}
1307
1308static int __devexit abituguru_remove(struct platform_device *pdev)
1309{
1310	int i;
1311	struct abituguru_data *data = platform_get_drvdata(pdev);
1312
1313	hwmon_device_unregister(data->hwmon_dev);
1314	for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
1315		device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
1316	for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1317		device_remove_file(&pdev->dev,
1318			&abituguru_sysfs_attr[i].dev_attr);
1319	platform_set_drvdata(pdev, NULL);
1320	kfree(data);
1321
1322	return 0;
1323}
1324
1325static struct abituguru_data *abituguru_update_device(struct device *dev)
1326{
1327	int i, err;
1328	struct abituguru_data *data = dev_get_drvdata(dev);
1329	/* fake a complete successful read if no update necessary. */
1330	char success = 1;
1331
1332	mutex_lock(&data->update_lock);
1333	if (time_after(jiffies, data->last_updated + HZ)) {
1334		success = 0;
1335		if ((err = abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1336				data->alarms, 3, 0)) != 3)
 
1337			goto LEAVE_UPDATE;
1338		for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1339			if ((err = abituguru_read(data,
1340					ABIT_UGURU_SENSOR_BANK1, i,
1341					&data->bank1_value[i], 1, 0)) != 1)
1342				goto LEAVE_UPDATE;
1343			if ((err = abituguru_read(data,
1344					ABIT_UGURU_SENSOR_BANK1 + 1, i,
1345					data->bank1_settings[i], 3, 0)) != 3)
1346				goto LEAVE_UPDATE;
1347		}
1348		for (i = 0; i < data->bank2_sensors; i++)
1349			if ((err = abituguru_read(data,
1350					ABIT_UGURU_SENSOR_BANK2, i,
1351					&data->bank2_value[i], 1, 0)) != 1)
1352				goto LEAVE_UPDATE;
 
1353		/* success! */
1354		success = 1;
1355		data->update_timeouts = 0;
1356LEAVE_UPDATE:
1357		/* handle timeout condition */
1358		if (!success && (err == -EBUSY || err >= 0)) {
1359			/* No overflow please */
1360			if (data->update_timeouts < 255u)
1361				data->update_timeouts++;
1362			if (data->update_timeouts <= ABIT_UGURU_MAX_TIMEOUTS) {
1363				ABIT_UGURU_DEBUG(3, "timeout exceeded, will "
1364					"try again next update\n");
1365				/* Just a timeout, fake a successful read */
1366				success = 1;
1367			} else
1368				ABIT_UGURU_DEBUG(1, "timeout exceeded %d "
1369					"times waiting for more input state\n",
1370					(int)data->update_timeouts);
1371		}
1372		/* On success set last_updated */
1373		if (success)
1374			data->last_updated = jiffies;
1375	}
1376	mutex_unlock(&data->update_lock);
1377
1378	if (success)
1379		return data;
1380	else
1381		return NULL;
1382}
1383
1384#ifdef CONFIG_PM
1385static int abituguru_suspend(struct platform_device *pdev, pm_message_t state)
1386{
1387	struct abituguru_data *data = platform_get_drvdata(pdev);
1388	/* make sure all communications with the uguru are done and no new
1389	   ones are started */
 
 
1390	mutex_lock(&data->update_lock);
1391	return 0;
1392}
1393
1394static int abituguru_resume(struct platform_device *pdev)
1395{
1396	struct abituguru_data *data = platform_get_drvdata(pdev);
1397	/* See if the uGuru is still ready */
1398	if (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT)
1399		data->uguru_ready = 0;
1400	mutex_unlock(&data->update_lock);
1401	return 0;
1402}
1403#else
1404#define abituguru_suspend	NULL
1405#define abituguru_resume	NULL
1406#endif /* CONFIG_PM */
1407
1408static struct platform_driver abituguru_driver = {
1409	.driver = {
1410		.owner	= THIS_MODULE,
1411		.name	= ABIT_UGURU_NAME,
1412	},
1413	.probe		= abituguru_probe,
1414	.remove		= __devexit_p(abituguru_remove),
1415	.suspend	= abituguru_suspend,
1416	.resume		= abituguru_resume,
1417};
1418
1419static int __init abituguru_detect(void)
1420{
1421	/* See if there is an uguru there. After a reboot uGuru will hold 0x00
1422	   at DATA and 0xAC, when this driver has already been loaded once
1423	   DATA will hold 0x08. For most uGuru's CMD will hold 0xAC in either
1424	   scenario but some will hold 0x00.
1425	   Some uGuru's initially hold 0x09 at DATA and will only hold 0x08
1426	   after reading CMD first, so CMD must be read first! */
 
 
1427	u8 cmd_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_CMD);
1428	u8 data_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_DATA);
1429	if (((data_val == 0x00) || (data_val == 0x08)) &&
1430	    ((cmd_val == 0x00) || (cmd_val == 0xAC)))
1431		return ABIT_UGURU_BASE;
1432
1433	ABIT_UGURU_DEBUG(2, "no Abit uGuru found, data = 0x%02X, cmd = "
1434		"0x%02X\n", (unsigned int)data_val, (unsigned int)cmd_val);
1435
1436	if (force) {
1437		pr_info("Assuming Abit uGuru is present because of \"force\" parameter\n");
1438		return ABIT_UGURU_BASE;
1439	}
1440
1441	/* No uGuru found */
1442	return -ENODEV;
1443}
1444
1445static struct platform_device *abituguru_pdev;
1446
1447static int __init abituguru_init(void)
1448{
1449	int address, err;
1450	struct resource res = { .flags = IORESOURCE_IO };
1451	const char *board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1452
1453	/* safety check, refuse to load on non Abit motherboards */
1454	if (!force && (!board_vendor ||
1455			strcmp(board_vendor, "http://www.abit.com.tw/")))
1456		return -ENODEV;
1457
1458	address = abituguru_detect();
1459	if (address < 0)
1460		return address;
1461
1462	err = platform_driver_register(&abituguru_driver);
1463	if (err)
1464		goto exit;
1465
1466	abituguru_pdev = platform_device_alloc(ABIT_UGURU_NAME, address);
1467	if (!abituguru_pdev) {
1468		pr_err("Device allocation failed\n");
1469		err = -ENOMEM;
1470		goto exit_driver_unregister;
1471	}
1472
1473	res.start = address;
1474	res.end = address + ABIT_UGURU_REGION_LENGTH - 1;
1475	res.name = ABIT_UGURU_NAME;
1476
1477	err = platform_device_add_resources(abituguru_pdev, &res, 1);
1478	if (err) {
1479		pr_err("Device resource addition failed (%d)\n", err);
1480		goto exit_device_put;
1481	}
1482
1483	err = platform_device_add(abituguru_pdev);
1484	if (err) {
1485		pr_err("Device addition failed (%d)\n", err);
1486		goto exit_device_put;
1487	}
1488
1489	return 0;
1490
1491exit_device_put:
1492	platform_device_put(abituguru_pdev);
1493exit_driver_unregister:
1494	platform_driver_unregister(&abituguru_driver);
1495exit:
1496	return err;
1497}
1498
1499static void __exit abituguru_exit(void)
1500{
1501	platform_device_unregister(abituguru_pdev);
1502	platform_driver_unregister(&abituguru_driver);
1503}
1504
1505MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
1506MODULE_DESCRIPTION("Abit uGuru Sensor device");
1507MODULE_LICENSE("GPL");
1508
1509module_init(abituguru_init);
1510module_exit(abituguru_exit);
v3.5.6
   1/*
   2 * abituguru.c Copyright (c) 2005-2006 Hans de Goede <hdegoede@redhat.com>
   3 *
   4 * This program is free software; you can redistribute it and/or modify
   5 * it under the terms of the GNU General Public License as published by
   6 * the Free Software Foundation; either version 2 of the License, or
   7 * (at your option) any later version.
   8 *
   9 * This program is distributed in the hope that it will be useful,
  10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12 * GNU General Public License for more details.
  13 *
  14 * You should have received a copy of the GNU General Public License
  15 * along with this program; if not, write to the Free Software
  16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17 */
  18/*
  19 * This driver supports the sensor part of the first and second revision of
  20 * the custom Abit uGuru chip found on Abit uGuru motherboards. Note: because
  21 * of lack of specs the CPU/RAM voltage & frequency control is not supported!
  22 */
  23
  24#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25
  26#include <linux/module.h>
  27#include <linux/sched.h>
  28#include <linux/init.h>
  29#include <linux/slab.h>
  30#include <linux/jiffies.h>
  31#include <linux/mutex.h>
  32#include <linux/err.h>
  33#include <linux/delay.h>
  34#include <linux/platform_device.h>
  35#include <linux/hwmon.h>
  36#include <linux/hwmon-sysfs.h>
  37#include <linux/dmi.h>
  38#include <linux/io.h>
  39
  40/* Banks */
  41#define ABIT_UGURU_ALARM_BANK			0x20 /* 1x 3 bytes */
  42#define ABIT_UGURU_SENSOR_BANK1			0x21 /* 16x volt and temp */
  43#define ABIT_UGURU_FAN_PWM			0x24 /* 3x 5 bytes */
  44#define ABIT_UGURU_SENSOR_BANK2			0x26 /* fans */
  45/* max nr of sensors in bank1, a bank1 sensor can be in, temp or nc */
  46#define ABIT_UGURU_MAX_BANK1_SENSORS		16
  47/*
  48 * Warning if you increase one of the 2 MAX defines below to 10 or higher you
  49 * should adjust the belonging _NAMES_LENGTH macro for the 2 digit number!
  50 */
  51/* max nr of sensors in bank2, currently mb's with max 6 fans are known */
  52#define ABIT_UGURU_MAX_BANK2_SENSORS		6
  53/* max nr of pwm outputs, currently mb's with max 5 pwm outputs are known */
  54#define ABIT_UGURU_MAX_PWMS			5
  55/* uGuru sensor bank 1 flags */			     /* Alarm if: */
  56#define ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE	0x01 /*  temp over warn */
  57#define ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE	0x02 /*  volt over max */
  58#define ABIT_UGURU_VOLT_LOW_ALARM_ENABLE	0x04 /*  volt under min */
  59#define ABIT_UGURU_TEMP_HIGH_ALARM_FLAG		0x10 /* temp is over warn */
  60#define ABIT_UGURU_VOLT_HIGH_ALARM_FLAG		0x20 /* volt is over max */
  61#define ABIT_UGURU_VOLT_LOW_ALARM_FLAG		0x40 /* volt is under min */
  62/* uGuru sensor bank 2 flags */			     /* Alarm if: */
  63#define ABIT_UGURU_FAN_LOW_ALARM_ENABLE		0x01 /*   fan under min */
  64/* uGuru sensor bank common flags */
  65#define ABIT_UGURU_BEEP_ENABLE			0x08 /* beep if alarm */
  66#define ABIT_UGURU_SHUTDOWN_ENABLE		0x80 /* shutdown if alarm */
  67/* uGuru fan PWM (speed control) flags */
  68#define ABIT_UGURU_FAN_PWM_ENABLE		0x80 /* enable speed control */
  69/* Values used for conversion */
  70#define ABIT_UGURU_FAN_MAX			15300 /* RPM */
  71/* Bank1 sensor types */
  72#define ABIT_UGURU_IN_SENSOR			0
  73#define ABIT_UGURU_TEMP_SENSOR			1
  74#define ABIT_UGURU_NC				2
  75/*
  76 * In many cases we need to wait for the uGuru to reach a certain status, most
  77 * of the time it will reach this status within 30 - 90 ISA reads, and thus we
  78 * can best busy wait. This define gives the total amount of reads to try.
  79 */
  80#define ABIT_UGURU_WAIT_TIMEOUT			125
  81/*
  82 * However sometimes older versions of the uGuru seem to be distracted and they
  83 * do not respond for a long time. To handle this we sleep before each of the
  84 * last ABIT_UGURU_WAIT_TIMEOUT_SLEEP tries.
  85 */
  86#define ABIT_UGURU_WAIT_TIMEOUT_SLEEP		5
  87/*
  88 * Normally all expected status in abituguru_ready, are reported after the
  89 * first read, but sometimes not and we need to poll.
  90 */
  91#define ABIT_UGURU_READY_TIMEOUT		5
  92/* Maximum 3 retries on timedout reads/writes, delay 200 ms before retrying */
  93#define ABIT_UGURU_MAX_RETRIES			3
  94#define ABIT_UGURU_RETRY_DELAY			(HZ/5)
  95/* Maximum 2 timeouts in abituguru_update_device, iow 3 in a row is an error */
  96#define ABIT_UGURU_MAX_TIMEOUTS			2
  97/* utility macros */
  98#define ABIT_UGURU_NAME				"abituguru"
  99#define ABIT_UGURU_DEBUG(level, format, arg...)				\
 100	if (level <= verbose)						\
 101		printk(KERN_DEBUG ABIT_UGURU_NAME ": "	format , ## arg)
 102/* Macros to help calculate the sysfs_names array length */
 103/*
 104 * sum of strlen of: in??_input\0, in??_{min,max}\0, in??_{min,max}_alarm\0,
 105 * in??_{min,max}_alarm_enable\0, in??_beep\0, in??_shutdown\0
 106 */
 107#define ABITUGURU_IN_NAMES_LENGTH	(11 + 2 * 9 + 2 * 15 + 2 * 22 + 10 + 14)
 108/*
 109 * sum of strlen of: temp??_input\0, temp??_max\0, temp??_crit\0,
 110 * temp??_alarm\0, temp??_alarm_enable\0, temp??_beep\0, temp??_shutdown\0
 111 */
 112#define ABITUGURU_TEMP_NAMES_LENGTH	(13 + 11 + 12 + 13 + 20 + 12 + 16)
 113/*
 114 * sum of strlen of: fan?_input\0, fan?_min\0, fan?_alarm\0,
 115 * fan?_alarm_enable\0, fan?_beep\0, fan?_shutdown\0
 116 */
 117#define ABITUGURU_FAN_NAMES_LENGTH	(11 + 9 + 11 + 18 + 10 + 14)
 118/*
 119 * sum of strlen of: pwm?_enable\0, pwm?_auto_channels_temp\0,
 120 * pwm?_auto_point{1,2}_pwm\0, pwm?_auto_point{1,2}_temp\0
 121 */
 122#define ABITUGURU_PWM_NAMES_LENGTH	(12 + 24 + 2 * 21 + 2 * 22)
 123/* IN_NAMES_LENGTH > TEMP_NAMES_LENGTH so assume all bank1 sensors are in */
 124#define ABITUGURU_SYSFS_NAMES_LENGTH	( \
 125	ABIT_UGURU_MAX_BANK1_SENSORS * ABITUGURU_IN_NAMES_LENGTH + \
 126	ABIT_UGURU_MAX_BANK2_SENSORS * ABITUGURU_FAN_NAMES_LENGTH + \
 127	ABIT_UGURU_MAX_PWMS * ABITUGURU_PWM_NAMES_LENGTH)
 128
 129/*
 130 * All the macros below are named identical to the oguru and oguru2 programs
 131 * reverse engineered by Olle Sandberg, hence the names might not be 100%
 132 * logical. I could come up with better names, but I prefer keeping the names
 133 * identical so that this driver can be compared with his work more easily.
 134 */
 135/* Two i/o-ports are used by uGuru */
 136#define ABIT_UGURU_BASE				0x00E0
 137/* Used to tell uGuru what to read and to read the actual data */
 138#define ABIT_UGURU_CMD				0x00
 139/* Mostly used to check if uGuru is busy */
 140#define ABIT_UGURU_DATA				0x04
 141#define ABIT_UGURU_REGION_LENGTH		5
 142/* uGuru status' */
 143#define ABIT_UGURU_STATUS_WRITE			0x00 /* Ready to be written */
 144#define ABIT_UGURU_STATUS_READ			0x01 /* Ready to be read */
 145#define ABIT_UGURU_STATUS_INPUT			0x08 /* More input */
 146#define ABIT_UGURU_STATUS_READY			0x09 /* Ready to be written */
 147
 148/* Constants */
 149/* in (Volt) sensors go up to 3494 mV, temp to 255000 millidegrees Celsius */
 150static const int abituguru_bank1_max_value[2] = { 3494, 255000 };
 151/*
 152 * Min / Max allowed values for sensor2 (fan) alarm threshold, these values
 153 * correspond to 300-3000 RPM
 154 */
 155static const u8 abituguru_bank2_min_threshold = 5;
 156static const u8 abituguru_bank2_max_threshold = 50;
 157/*
 158 * Register 0 is a bitfield, 1 and 2 are pwm settings (255 = 100%), 3 and 4
 159 * are temperature trip points.
 160 */
 161static const int abituguru_pwm_settings_multiplier[5] = { 0, 1, 1, 1000, 1000 };
 162/*
 163 * Min / Max allowed values for pwm_settings. Note: pwm1 (CPU fan) is a
 164 * special case the minium allowed pwm% setting for this is 30% (77) on
 165 * some MB's this special case is handled in the code!
 166 */
 167static const u8 abituguru_pwm_min[5] = { 0, 170, 170, 25, 25 };
 168static const u8 abituguru_pwm_max[5] = { 0, 255, 255, 75, 75 };
 169
 170
 171/* Insmod parameters */
 172static bool force;
 173module_param(force, bool, 0);
 174MODULE_PARM_DESC(force, "Set to one to force detection.");
 175static int bank1_types[ABIT_UGURU_MAX_BANK1_SENSORS] = { -1, -1, -1, -1, -1,
 176	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
 177module_param_array(bank1_types, int, NULL, 0);
 178MODULE_PARM_DESC(bank1_types, "Bank1 sensortype autodetection override:\n"
 179	"   -1 autodetect\n"
 180	"    0 volt sensor\n"
 181	"    1 temp sensor\n"
 182	"    2 not connected");
 183static int fan_sensors;
 184module_param(fan_sensors, int, 0);
 185MODULE_PARM_DESC(fan_sensors, "Number of fan sensors on the uGuru "
 186	"(0 = autodetect)");
 187static int pwms;
 188module_param(pwms, int, 0);
 189MODULE_PARM_DESC(pwms, "Number of PWMs on the uGuru "
 190	"(0 = autodetect)");
 191
 192/* Default verbose is 2, since this driver is still in the testing phase */
 193static int verbose = 2;
 194module_param(verbose, int, 0644);
 195MODULE_PARM_DESC(verbose, "How verbose should the driver be? (0-3):\n"
 196	"   0 normal output\n"
 197	"   1 + verbose error reporting\n"
 198	"   2 + sensors type probing info\n"
 199	"   3 + retryable error reporting");
 200
 201
 202/*
 203 * For the Abit uGuru, we need to keep some data in memory.
 204 * The structure is dynamically allocated, at the same time when a new
 205 * abituguru device is allocated.
 206 */
 207struct abituguru_data {
 208	struct device *hwmon_dev;	/* hwmon registered device */
 209	struct mutex update_lock;	/* protect access to data and uGuru */
 210	unsigned long last_updated;	/* In jiffies */
 211	unsigned short addr;		/* uguru base address */
 212	char uguru_ready;		/* is the uguru in ready state? */
 213	unsigned char update_timeouts;	/*
 214					 * number of update timeouts since last
 215					 * successful update
 216					 */
 217
 218	/*
 219	 * The sysfs attr and their names are generated automatically, for bank1
 220	 * we cannot use a predefined array because we don't know beforehand
 221	 * of a sensor is a volt or a temp sensor, for bank2 and the pwms its
 222	 * easier todo things the same way.  For in sensors we have 9 (temp 7)
 223	 * sysfs entries per sensor, for bank2 and pwms 6.
 224	 */
 225	struct sensor_device_attribute_2 sysfs_attr[
 226		ABIT_UGURU_MAX_BANK1_SENSORS * 9 +
 227		ABIT_UGURU_MAX_BANK2_SENSORS * 6 + ABIT_UGURU_MAX_PWMS * 6];
 228	/* Buffer to store the dynamically generated sysfs names */
 229	char sysfs_names[ABITUGURU_SYSFS_NAMES_LENGTH];
 230
 231	/* Bank 1 data */
 232	/* number of and addresses of [0] in, [1] temp sensors */
 233	u8 bank1_sensors[2];
 234	u8 bank1_address[2][ABIT_UGURU_MAX_BANK1_SENSORS];
 235	u8 bank1_value[ABIT_UGURU_MAX_BANK1_SENSORS];
 236	/*
 237	 * This array holds 3 entries per sensor for the bank 1 sensor settings
 238	 * (flags, min, max for voltage / flags, warn, shutdown for temp).
 239	 */
 240	u8 bank1_settings[ABIT_UGURU_MAX_BANK1_SENSORS][3];
 241	/*
 242	 * Maximum value for each sensor used for scaling in mV/millidegrees
 243	 * Celsius.
 244	 */
 245	int bank1_max_value[ABIT_UGURU_MAX_BANK1_SENSORS];
 246
 247	/* Bank 2 data, ABIT_UGURU_MAX_BANK2_SENSORS entries for bank2 */
 248	u8 bank2_sensors; /* actual number of bank2 sensors found */
 249	u8 bank2_value[ABIT_UGURU_MAX_BANK2_SENSORS];
 250	u8 bank2_settings[ABIT_UGURU_MAX_BANK2_SENSORS][2]; /* flags, min */
 251
 252	/* Alarms 2 bytes for bank1, 1 byte for bank2 */
 253	u8 alarms[3];
 254
 255	/* Fan PWM (speed control) 5 bytes per PWM */
 256	u8 pwms; /* actual number of pwms found */
 257	u8 pwm_settings[ABIT_UGURU_MAX_PWMS][5];
 258};
 259
 260static const char *never_happen = "This should never happen.";
 261static const char *report_this =
 262	"Please report this to the abituguru maintainer (see MAINTAINERS)";
 263
 264/* wait till the uguru is in the specified state */
 265static int abituguru_wait(struct abituguru_data *data, u8 state)
 266{
 267	int timeout = ABIT_UGURU_WAIT_TIMEOUT;
 268
 269	while (inb_p(data->addr + ABIT_UGURU_DATA) != state) {
 270		timeout--;
 271		if (timeout == 0)
 272			return -EBUSY;
 273		/*
 274		 * sleep a bit before our last few tries, see the comment on
 275		 * this where ABIT_UGURU_WAIT_TIMEOUT_SLEEP is defined.
 276		 */
 277		if (timeout <= ABIT_UGURU_WAIT_TIMEOUT_SLEEP)
 278			msleep(0);
 279	}
 280	return 0;
 281}
 282
 283/* Put the uguru in ready for input state */
 284static int abituguru_ready(struct abituguru_data *data)
 285{
 286	int timeout = ABIT_UGURU_READY_TIMEOUT;
 287
 288	if (data->uguru_ready)
 289		return 0;
 290
 291	/* Reset? / Prepare for next read/write cycle */
 292	outb(0x00, data->addr + ABIT_UGURU_DATA);
 293
 294	/* Wait till the uguru is ready */
 295	if (abituguru_wait(data, ABIT_UGURU_STATUS_READY)) {
 296		ABIT_UGURU_DEBUG(1,
 297			"timeout exceeded waiting for ready state\n");
 298		return -EIO;
 299	}
 300
 301	/* Cmd port MUST be read now and should contain 0xAC */
 302	while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
 303		timeout--;
 304		if (timeout == 0) {
 305			ABIT_UGURU_DEBUG(1,
 306			   "CMD reg does not hold 0xAC after ready command\n");
 307			return -EIO;
 308		}
 309		msleep(0);
 310	}
 311
 312	/*
 313	 * After this the ABIT_UGURU_DATA port should contain
 314	 * ABIT_UGURU_STATUS_INPUT
 315	 */
 316	timeout = ABIT_UGURU_READY_TIMEOUT;
 317	while (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT) {
 318		timeout--;
 319		if (timeout == 0) {
 320			ABIT_UGURU_DEBUG(1,
 321				"state != more input after ready command\n");
 322			return -EIO;
 323		}
 324		msleep(0);
 325	}
 326
 327	data->uguru_ready = 1;
 328	return 0;
 329}
 330
 331/*
 332 * Send the bank and then sensor address to the uGuru for the next read/write
 333 * cycle. This function gets called as the first part of a read/write by
 334 * abituguru_read and abituguru_write. This function should never be
 335 * called by any other function.
 336 */
 337static int abituguru_send_address(struct abituguru_data *data,
 338	u8 bank_addr, u8 sensor_addr, int retries)
 339{
 340	/*
 341	 * assume the caller does error handling itself if it has not requested
 342	 * any retries, and thus be quiet.
 343	 */
 344	int report_errors = retries;
 345
 346	for (;;) {
 347		/*
 348		 * Make sure the uguru is ready and then send the bank address,
 349		 * after this the uguru is no longer "ready".
 350		 */
 351		if (abituguru_ready(data) != 0)
 352			return -EIO;
 353		outb(bank_addr, data->addr + ABIT_UGURU_DATA);
 354		data->uguru_ready = 0;
 355
 356		/*
 357		 * Wait till the uguru is ABIT_UGURU_STATUS_INPUT state again
 358		 * and send the sensor addr
 359		 */
 360		if (abituguru_wait(data, ABIT_UGURU_STATUS_INPUT)) {
 361			if (retries) {
 362				ABIT_UGURU_DEBUG(3, "timeout exceeded "
 363					"waiting for more input state, %d "
 364					"tries remaining\n", retries);
 365				set_current_state(TASK_UNINTERRUPTIBLE);
 366				schedule_timeout(ABIT_UGURU_RETRY_DELAY);
 367				retries--;
 368				continue;
 369			}
 370			if (report_errors)
 371				ABIT_UGURU_DEBUG(1, "timeout exceeded "
 372					"waiting for more input state "
 373					"(bank: %d)\n", (int)bank_addr);
 374			return -EBUSY;
 375		}
 376		outb(sensor_addr, data->addr + ABIT_UGURU_CMD);
 377		return 0;
 378	}
 379}
 380
 381/*
 382 * Read count bytes from sensor sensor_addr in bank bank_addr and store the
 383 * result in buf, retry the send address part of the read retries times.
 384 */
 385static int abituguru_read(struct abituguru_data *data,
 386	u8 bank_addr, u8 sensor_addr, u8 *buf, int count, int retries)
 387{
 388	int i;
 389
 390	/* Send the address */
 391	i = abituguru_send_address(data, bank_addr, sensor_addr, retries);
 392	if (i)
 393		return i;
 394
 395	/* And read the data */
 396	for (i = 0; i < count; i++) {
 397		if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
 398			ABIT_UGURU_DEBUG(retries ? 1 : 3,
 399				"timeout exceeded waiting for "
 400				"read state (bank: %d, sensor: %d)\n",
 401				(int)bank_addr, (int)sensor_addr);
 402			break;
 403		}
 404		buf[i] = inb(data->addr + ABIT_UGURU_CMD);
 405	}
 406
 407	/* Last put the chip back in ready state */
 408	abituguru_ready(data);
 409
 410	return i;
 411}
 412
 413/*
 414 * Write count bytes from buf to sensor sensor_addr in bank bank_addr, the send
 415 * address part of the write is always retried ABIT_UGURU_MAX_RETRIES times.
 416 */
 417static int abituguru_write(struct abituguru_data *data,
 418	u8 bank_addr, u8 sensor_addr, u8 *buf, int count)
 419{
 420	/*
 421	 * We use the ready timeout as we have to wait for 0xAC just like the
 422	 * ready function
 423	 */
 424	int i, timeout = ABIT_UGURU_READY_TIMEOUT;
 425
 426	/* Send the address */
 427	i = abituguru_send_address(data, bank_addr, sensor_addr,
 428		ABIT_UGURU_MAX_RETRIES);
 429	if (i)
 430		return i;
 431
 432	/* And write the data */
 433	for (i = 0; i < count; i++) {
 434		if (abituguru_wait(data, ABIT_UGURU_STATUS_WRITE)) {
 435			ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for "
 436				"write state (bank: %d, sensor: %d)\n",
 437				(int)bank_addr, (int)sensor_addr);
 438			break;
 439		}
 440		outb(buf[i], data->addr + ABIT_UGURU_CMD);
 441	}
 442
 443	/*
 444	 * Now we need to wait till the chip is ready to be read again,
 445	 * so that we can read 0xAC as confirmation that our write has
 446	 * succeeded.
 447	 */
 448	if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
 449		ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for read state "
 450			"after write (bank: %d, sensor: %d)\n", (int)bank_addr,
 451			(int)sensor_addr);
 452		return -EIO;
 453	}
 454
 455	/* Cmd port MUST be read now and should contain 0xAC */
 456	while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
 457		timeout--;
 458		if (timeout == 0) {
 459			ABIT_UGURU_DEBUG(1, "CMD reg does not hold 0xAC after "
 460				"write (bank: %d, sensor: %d)\n",
 461				(int)bank_addr, (int)sensor_addr);
 462			return -EIO;
 463		}
 464		msleep(0);
 465	}
 466
 467	/* Last put the chip back in ready state */
 468	abituguru_ready(data);
 469
 470	return i;
 471}
 472
 473/*
 474 * Detect sensor type. Temp and Volt sensors are enabled with
 475 * different masks and will ignore enable masks not meant for them.
 476 * This enables us to test what kind of sensor we're dealing with.
 477 * By setting the alarm thresholds so that we will always get an
 478 * alarm for sensor type X and then enabling the sensor as sensor type
 479 * X, if we then get an alarm it is a sensor of type X.
 480 */
 481static int __devinit
 482abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
 483				   u8 sensor_addr)
 484{
 485	u8 val, test_flag, buf[3];
 486	int i, ret = -ENODEV; /* error is the most common used retval :| */
 487
 488	/* If overriden by the user return the user selected type */
 489	if (bank1_types[sensor_addr] >= ABIT_UGURU_IN_SENSOR &&
 490			bank1_types[sensor_addr] <= ABIT_UGURU_NC) {
 491		ABIT_UGURU_DEBUG(2, "assuming sensor type %d for bank1 sensor "
 492			"%d because of \"bank1_types\" module param\n",
 493			bank1_types[sensor_addr], (int)sensor_addr);
 494		return bank1_types[sensor_addr];
 495	}
 496
 497	/* First read the sensor and the current settings */
 498	if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, sensor_addr, &val,
 499			1, ABIT_UGURU_MAX_RETRIES) != 1)
 500		return -ENODEV;
 501
 502	/* Test val is sane / usable for sensor type detection. */
 503	if ((val < 10u) || (val > 250u)) {
 504		pr_warn("bank1-sensor: %d reading (%d) too close to limits, "
 505			"unable to determine sensor type, skipping sensor\n",
 506			(int)sensor_addr, (int)val);
 507		/*
 508		 * assume no sensor is there for sensors for which we can't
 509		 * determine the sensor type because their reading is too close
 510		 * to their limits, this usually means no sensor is there.
 511		 */
 512		return ABIT_UGURU_NC;
 513	}
 514
 515	ABIT_UGURU_DEBUG(2, "testing bank1 sensor %d\n", (int)sensor_addr);
 516	/*
 517	 * Volt sensor test, enable volt low alarm, set min value ridicously
 518	 * high, or vica versa if the reading is very high. If its a volt
 519	 * sensor this should always give us an alarm.
 520	 */
 521	if (val <= 240u) {
 522		buf[0] = ABIT_UGURU_VOLT_LOW_ALARM_ENABLE;
 523		buf[1] = 245;
 524		buf[2] = 250;
 525		test_flag = ABIT_UGURU_VOLT_LOW_ALARM_FLAG;
 526	} else {
 527		buf[0] = ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE;
 528		buf[1] = 5;
 529		buf[2] = 10;
 530		test_flag = ABIT_UGURU_VOLT_HIGH_ALARM_FLAG;
 531	}
 532
 533	if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
 534			buf, 3) != 3)
 535		goto abituguru_detect_bank1_sensor_type_exit;
 536	/*
 537	 * Now we need 20 ms to give the uguru time to read the sensors
 538	 * and raise a voltage alarm
 539	 */
 540	set_current_state(TASK_UNINTERRUPTIBLE);
 541	schedule_timeout(HZ/50);
 542	/* Check for alarm and check the alarm is a volt low alarm. */
 543	if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
 544			ABIT_UGURU_MAX_RETRIES) != 3)
 545		goto abituguru_detect_bank1_sensor_type_exit;
 546	if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
 547		if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
 548				sensor_addr, buf, 3,
 549				ABIT_UGURU_MAX_RETRIES) != 3)
 550			goto abituguru_detect_bank1_sensor_type_exit;
 551		if (buf[0] & test_flag) {
 552			ABIT_UGURU_DEBUG(2, "  found volt sensor\n");
 553			ret = ABIT_UGURU_IN_SENSOR;
 554			goto abituguru_detect_bank1_sensor_type_exit;
 555		} else
 556			ABIT_UGURU_DEBUG(2, "  alarm raised during volt "
 557				"sensor test, but volt range flag not set\n");
 558	} else
 559		ABIT_UGURU_DEBUG(2, "  alarm not raised during volt sensor "
 560			"test\n");
 561
 562	/*
 563	 * Temp sensor test, enable sensor as a temp sensor, set beep value
 564	 * ridicously low (but not too low, otherwise uguru ignores it).
 565	 * If its a temp sensor this should always give us an alarm.
 566	 */
 567	buf[0] = ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE;
 568	buf[1] = 5;
 569	buf[2] = 10;
 570	if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
 571			buf, 3) != 3)
 572		goto abituguru_detect_bank1_sensor_type_exit;
 573	/*
 574	 * Now we need 50 ms to give the uguru time to read the sensors
 575	 * and raise a temp alarm
 576	 */
 577	set_current_state(TASK_UNINTERRUPTIBLE);
 578	schedule_timeout(HZ/20);
 579	/* Check for alarm and check the alarm is a temp high alarm. */
 580	if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
 581			ABIT_UGURU_MAX_RETRIES) != 3)
 582		goto abituguru_detect_bank1_sensor_type_exit;
 583	if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
 584		if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
 585				sensor_addr, buf, 3,
 586				ABIT_UGURU_MAX_RETRIES) != 3)
 587			goto abituguru_detect_bank1_sensor_type_exit;
 588		if (buf[0] & ABIT_UGURU_TEMP_HIGH_ALARM_FLAG) {
 589			ABIT_UGURU_DEBUG(2, "  found temp sensor\n");
 590			ret = ABIT_UGURU_TEMP_SENSOR;
 591			goto abituguru_detect_bank1_sensor_type_exit;
 592		} else
 593			ABIT_UGURU_DEBUG(2, "  alarm raised during temp "
 594				"sensor test, but temp high flag not set\n");
 595	} else
 596		ABIT_UGURU_DEBUG(2, "  alarm not raised during temp sensor "
 597			"test\n");
 598
 599	ret = ABIT_UGURU_NC;
 600abituguru_detect_bank1_sensor_type_exit:
 601	/*
 602	 * Restore original settings, failing here is really BAD, it has been
 603	 * reported that some BIOS-es hang when entering the uGuru menu with
 604	 * invalid settings present in the uGuru, so we try this 3 times.
 605	 */
 606	for (i = 0; i < 3; i++)
 607		if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
 608				sensor_addr, data->bank1_settings[sensor_addr],
 609				3) == 3)
 610			break;
 611	if (i == 3) {
 612		pr_err("Fatal error could not restore original settings. %s %s\n",
 613		       never_happen, report_this);
 614		return -ENODEV;
 615	}
 616	return ret;
 617}
 618
 619/*
 620 * These functions try to find out how many sensors there are in bank2 and how
 621 * many pwms there are. The purpose of this is to make sure that we don't give
 622 * the user the possibility to change settings for non-existent sensors / pwm.
 623 * The uGuru will happily read / write whatever memory happens to be after the
 624 * memory storing the PWM settings when reading/writing to a PWM which is not
 625 * there. Notice even if we detect a PWM which doesn't exist we normally won't
 626 * write to it, unless the user tries to change the settings.
 627 *
 628 * Although the uGuru allows reading (settings) from non existing bank2
 629 * sensors, my version of the uGuru does seem to stop writing to them, the
 630 * write function above aborts in this case with:
 631 * "CMD reg does not hold 0xAC after write"
 632 *
 633 * Notice these 2 tests are non destructive iow read-only tests, otherwise
 634 * they would defeat their purpose. Although for the bank2_sensors detection a
 635 * read/write test would be feasible because of the reaction above, I've
 636 * however opted to stay on the safe side.
 637 */
 638static void __devinit
 639abituguru_detect_no_bank2_sensors(struct abituguru_data *data)
 640{
 641	int i;
 642
 643	if (fan_sensors > 0 && fan_sensors <= ABIT_UGURU_MAX_BANK2_SENSORS) {
 644		data->bank2_sensors = fan_sensors;
 645		ABIT_UGURU_DEBUG(2, "assuming %d fan sensors because of "
 646			"\"fan_sensors\" module param\n",
 647			(int)data->bank2_sensors);
 648		return;
 649	}
 650
 651	ABIT_UGURU_DEBUG(2, "detecting number of fan sensors\n");
 652	for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
 653		/*
 654		 * 0x89 are the known used bits:
 655		 * -0x80 enable shutdown
 656		 * -0x08 enable beep
 657		 * -0x01 enable alarm
 658		 * All other bits should be 0, but on some motherboards
 659		 * 0x40 (bit 6) is also high for some of the fans??
 660		 */
 661		if (data->bank2_settings[i][0] & ~0xC9) {
 662			ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
 663				"to be a fan sensor: settings[0] = %02X\n",
 664				i, (unsigned int)data->bank2_settings[i][0]);
 665			break;
 666		}
 667
 668		/* check if the threshold is within the allowed range */
 669		if (data->bank2_settings[i][1] <
 670				abituguru_bank2_min_threshold) {
 671			ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
 672				"to be a fan sensor: the threshold (%d) is "
 673				"below the minimum (%d)\n", i,
 674				(int)data->bank2_settings[i][1],
 675				(int)abituguru_bank2_min_threshold);
 676			break;
 677		}
 678		if (data->bank2_settings[i][1] >
 679				abituguru_bank2_max_threshold) {
 680			ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
 681				"to be a fan sensor: the threshold (%d) is "
 682				"above the maximum (%d)\n", i,
 683				(int)data->bank2_settings[i][1],
 684				(int)abituguru_bank2_max_threshold);
 685			break;
 686		}
 687	}
 688
 689	data->bank2_sensors = i;
 690	ABIT_UGURU_DEBUG(2, " found: %d fan sensors\n",
 691		(int)data->bank2_sensors);
 692}
 693
 694static void __devinit
 695abituguru_detect_no_pwms(struct abituguru_data *data)
 696{
 697	int i, j;
 698
 699	if (pwms > 0 && pwms <= ABIT_UGURU_MAX_PWMS) {
 700		data->pwms = pwms;
 701		ABIT_UGURU_DEBUG(2, "assuming %d PWM outputs because of "
 702			"\"pwms\" module param\n", (int)data->pwms);
 703		return;
 704	}
 705
 706	ABIT_UGURU_DEBUG(2, "detecting number of PWM outputs\n");
 707	for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
 708		/*
 709		 * 0x80 is the enable bit and the low
 710		 * nibble is which temp sensor to use,
 711		 * the other bits should be 0
 712		 */
 713		if (data->pwm_settings[i][0] & ~0x8F) {
 714			ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
 715				"to be a pwm channel: settings[0] = %02X\n",
 716				i, (unsigned int)data->pwm_settings[i][0]);
 717			break;
 718		}
 719
 720		/*
 721		 * the low nibble must correspond to one of the temp sensors
 722		 * we've found
 723		 */
 724		for (j = 0; j < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR];
 725				j++) {
 726			if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][j] ==
 727					(data->pwm_settings[i][0] & 0x0F))
 728				break;
 729		}
 730		if (j == data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
 731			ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
 732				"to be a pwm channel: %d is not a valid temp "
 733				"sensor address\n", i,
 734				data->pwm_settings[i][0] & 0x0F);
 735			break;
 736		}
 737
 738		/* check if all other settings are within the allowed range */
 739		for (j = 1; j < 5; j++) {
 740			u8 min;
 741			/* special case pwm1 min pwm% */
 742			if ((i == 0) && ((j == 1) || (j == 2)))
 743				min = 77;
 744			else
 745				min = abituguru_pwm_min[j];
 746			if (data->pwm_settings[i][j] < min) {
 747				ABIT_UGURU_DEBUG(2, "  pwm channel %d does "
 748					"not seem to be a pwm channel: "
 749					"setting %d (%d) is below the minimum "
 750					"value (%d)\n", i, j,
 751					(int)data->pwm_settings[i][j],
 752					(int)min);
 753				goto abituguru_detect_no_pwms_exit;
 754			}
 755			if (data->pwm_settings[i][j] > abituguru_pwm_max[j]) {
 756				ABIT_UGURU_DEBUG(2, "  pwm channel %d does "
 757					"not seem to be a pwm channel: "
 758					"setting %d (%d) is above the maximum "
 759					"value (%d)\n", i, j,
 760					(int)data->pwm_settings[i][j],
 761					(int)abituguru_pwm_max[j]);
 762				goto abituguru_detect_no_pwms_exit;
 763			}
 764		}
 765
 766		/* check that min temp < max temp and min pwm < max pwm */
 767		if (data->pwm_settings[i][1] >= data->pwm_settings[i][2]) {
 768			ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
 769				"to be a pwm channel: min pwm (%d) >= "
 770				"max pwm (%d)\n", i,
 771				(int)data->pwm_settings[i][1],
 772				(int)data->pwm_settings[i][2]);
 773			break;
 774		}
 775		if (data->pwm_settings[i][3] >= data->pwm_settings[i][4]) {
 776			ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
 777				"to be a pwm channel: min temp (%d) >= "
 778				"max temp (%d)\n", i,
 779				(int)data->pwm_settings[i][3],
 780				(int)data->pwm_settings[i][4]);
 781			break;
 782		}
 783	}
 784
 785abituguru_detect_no_pwms_exit:
 786	data->pwms = i;
 787	ABIT_UGURU_DEBUG(2, " found: %d PWM outputs\n", (int)data->pwms);
 788}
 789
 790/*
 791 * Following are the sysfs callback functions. These functions expect:
 792 * sensor_device_attribute_2->index:   sensor address/offset in the bank
 793 * sensor_device_attribute_2->nr:      register offset, bitmask or NA.
 794 */
 795static struct abituguru_data *abituguru_update_device(struct device *dev);
 796
 797static ssize_t show_bank1_value(struct device *dev,
 798	struct device_attribute *devattr, char *buf)
 799{
 800	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 801	struct abituguru_data *data = abituguru_update_device(dev);
 802	if (!data)
 803		return -EIO;
 804	return sprintf(buf, "%d\n", (data->bank1_value[attr->index] *
 805		data->bank1_max_value[attr->index] + 128) / 255);
 806}
 807
 808static ssize_t show_bank1_setting(struct device *dev,
 809	struct device_attribute *devattr, char *buf)
 810{
 811	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 812	struct abituguru_data *data = dev_get_drvdata(dev);
 813	return sprintf(buf, "%d\n",
 814		(data->bank1_settings[attr->index][attr->nr] *
 815		data->bank1_max_value[attr->index] + 128) / 255);
 816}
 817
 818static ssize_t show_bank2_value(struct device *dev,
 819	struct device_attribute *devattr, char *buf)
 820{
 821	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 822	struct abituguru_data *data = abituguru_update_device(dev);
 823	if (!data)
 824		return -EIO;
 825	return sprintf(buf, "%d\n", (data->bank2_value[attr->index] *
 826		ABIT_UGURU_FAN_MAX + 128) / 255);
 827}
 828
 829static ssize_t show_bank2_setting(struct device *dev,
 830	struct device_attribute *devattr, char *buf)
 831{
 832	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 833	struct abituguru_data *data = dev_get_drvdata(dev);
 834	return sprintf(buf, "%d\n",
 835		(data->bank2_settings[attr->index][attr->nr] *
 836		ABIT_UGURU_FAN_MAX + 128) / 255);
 837}
 838
 839static ssize_t store_bank1_setting(struct device *dev, struct device_attribute
 840	*devattr, const char *buf, size_t count)
 841{
 842	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 843	struct abituguru_data *data = dev_get_drvdata(dev);
 844	unsigned long val;
 845	ssize_t ret;
 846
 847	ret = kstrtoul(buf, 10, &val);
 848	if (ret)
 849		return ret;
 850
 851	ret = count;
 852	val = (val * 255 + data->bank1_max_value[attr->index] / 2) /
 853		data->bank1_max_value[attr->index];
 854	if (val > 255)
 855		return -EINVAL;
 856
 857	mutex_lock(&data->update_lock);
 858	if (data->bank1_settings[attr->index][attr->nr] != val) {
 859		u8 orig_val = data->bank1_settings[attr->index][attr->nr];
 860		data->bank1_settings[attr->index][attr->nr] = val;
 861		if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
 862				attr->index, data->bank1_settings[attr->index],
 863				3) <= attr->nr) {
 864			data->bank1_settings[attr->index][attr->nr] = orig_val;
 865			ret = -EIO;
 866		}
 867	}
 868	mutex_unlock(&data->update_lock);
 869	return ret;
 870}
 871
 872static ssize_t store_bank2_setting(struct device *dev, struct device_attribute
 873	*devattr, const char *buf, size_t count)
 874{
 875	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 876	struct abituguru_data *data = dev_get_drvdata(dev);
 877	unsigned long val;
 878	ssize_t ret;
 879
 880	ret = kstrtoul(buf, 10, &val);
 881	if (ret)
 882		return ret;
 883
 884	ret = count;
 885	val = (val * 255 + ABIT_UGURU_FAN_MAX / 2) / ABIT_UGURU_FAN_MAX;
 886
 887	/* this check can be done before taking the lock */
 888	if (val < abituguru_bank2_min_threshold ||
 889			val > abituguru_bank2_max_threshold)
 890		return -EINVAL;
 891
 892	mutex_lock(&data->update_lock);
 893	if (data->bank2_settings[attr->index][attr->nr] != val) {
 894		u8 orig_val = data->bank2_settings[attr->index][attr->nr];
 895		data->bank2_settings[attr->index][attr->nr] = val;
 896		if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK2 + 2,
 897				attr->index, data->bank2_settings[attr->index],
 898				2) <= attr->nr) {
 899			data->bank2_settings[attr->index][attr->nr] = orig_val;
 900			ret = -EIO;
 901		}
 902	}
 903	mutex_unlock(&data->update_lock);
 904	return ret;
 905}
 906
 907static ssize_t show_bank1_alarm(struct device *dev,
 908	struct device_attribute *devattr, char *buf)
 909{
 910	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 911	struct abituguru_data *data = abituguru_update_device(dev);
 912	if (!data)
 913		return -EIO;
 914	/*
 915	 * See if the alarm bit for this sensor is set, and if the
 916	 * alarm matches the type of alarm we're looking for (for volt
 917	 * it can be either low or high). The type is stored in a few
 918	 * readonly bits in the settings part of the relevant sensor.
 919	 * The bitmask of the type is passed to us in attr->nr.
 920	 */
 921	if ((data->alarms[attr->index / 8] & (0x01 << (attr->index % 8))) &&
 922			(data->bank1_settings[attr->index][0] & attr->nr))
 923		return sprintf(buf, "1\n");
 924	else
 925		return sprintf(buf, "0\n");
 926}
 927
 928static ssize_t show_bank2_alarm(struct device *dev,
 929	struct device_attribute *devattr, char *buf)
 930{
 931	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 932	struct abituguru_data *data = abituguru_update_device(dev);
 933	if (!data)
 934		return -EIO;
 935	if (data->alarms[2] & (0x01 << attr->index))
 936		return sprintf(buf, "1\n");
 937	else
 938		return sprintf(buf, "0\n");
 939}
 940
 941static ssize_t show_bank1_mask(struct device *dev,
 942	struct device_attribute *devattr, char *buf)
 943{
 944	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 945	struct abituguru_data *data = dev_get_drvdata(dev);
 946	if (data->bank1_settings[attr->index][0] & attr->nr)
 947		return sprintf(buf, "1\n");
 948	else
 949		return sprintf(buf, "0\n");
 950}
 951
 952static ssize_t show_bank2_mask(struct device *dev,
 953	struct device_attribute *devattr, char *buf)
 954{
 955	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 956	struct abituguru_data *data = dev_get_drvdata(dev);
 957	if (data->bank2_settings[attr->index][0] & attr->nr)
 958		return sprintf(buf, "1\n");
 959	else
 960		return sprintf(buf, "0\n");
 961}
 962
 963static ssize_t store_bank1_mask(struct device *dev,
 964	struct device_attribute *devattr, const char *buf, size_t count)
 965{
 966	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 967	struct abituguru_data *data = dev_get_drvdata(dev);
 968	ssize_t ret;
 
 969	u8 orig_val;
 970	unsigned long mask;
 971
 972	ret = kstrtoul(buf, 10, &mask);
 973	if (ret)
 974		return ret;
 975
 976	ret = count;
 977	mutex_lock(&data->update_lock);
 978	orig_val = data->bank1_settings[attr->index][0];
 979
 980	if (mask)
 981		data->bank1_settings[attr->index][0] |= attr->nr;
 982	else
 983		data->bank1_settings[attr->index][0] &= ~attr->nr;
 984
 985	if ((data->bank1_settings[attr->index][0] != orig_val) &&
 986			(abituguru_write(data,
 987			ABIT_UGURU_SENSOR_BANK1 + 2, attr->index,
 988			data->bank1_settings[attr->index], 3) < 1)) {
 989		data->bank1_settings[attr->index][0] = orig_val;
 990		ret = -EIO;
 991	}
 992	mutex_unlock(&data->update_lock);
 993	return ret;
 994}
 995
 996static ssize_t store_bank2_mask(struct device *dev,
 997	struct device_attribute *devattr, const char *buf, size_t count)
 998{
 999	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1000	struct abituguru_data *data = dev_get_drvdata(dev);
1001	ssize_t ret;
 
1002	u8 orig_val;
1003	unsigned long mask;
1004
1005	ret = kstrtoul(buf, 10, &mask);
1006	if (ret)
1007		return ret;
1008
1009	ret = count;
1010	mutex_lock(&data->update_lock);
1011	orig_val = data->bank2_settings[attr->index][0];
1012
1013	if (mask)
1014		data->bank2_settings[attr->index][0] |= attr->nr;
1015	else
1016		data->bank2_settings[attr->index][0] &= ~attr->nr;
1017
1018	if ((data->bank2_settings[attr->index][0] != orig_val) &&
1019			(abituguru_write(data,
1020			ABIT_UGURU_SENSOR_BANK2 + 2, attr->index,
1021			data->bank2_settings[attr->index], 2) < 1)) {
1022		data->bank2_settings[attr->index][0] = orig_val;
1023		ret = -EIO;
1024	}
1025	mutex_unlock(&data->update_lock);
1026	return ret;
1027}
1028
1029/* Fan PWM (speed control) */
1030static ssize_t show_pwm_setting(struct device *dev,
1031	struct device_attribute *devattr, char *buf)
1032{
1033	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1034	struct abituguru_data *data = dev_get_drvdata(dev);
1035	return sprintf(buf, "%d\n", data->pwm_settings[attr->index][attr->nr] *
1036		abituguru_pwm_settings_multiplier[attr->nr]);
1037}
1038
1039static ssize_t store_pwm_setting(struct device *dev, struct device_attribute
1040	*devattr, const char *buf, size_t count)
1041{
1042	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1043	struct abituguru_data *data = dev_get_drvdata(dev);
1044	u8 min;
1045	unsigned long val;
1046	ssize_t ret;
1047
1048	ret = kstrtoul(buf, 10, &val);
1049	if (ret)
1050		return ret;
1051
1052	ret = count;
1053	val = (val + abituguru_pwm_settings_multiplier[attr->nr] / 2) /
1054				abituguru_pwm_settings_multiplier[attr->nr];
1055
1056	/* special case pwm1 min pwm% */
1057	if ((attr->index == 0) && ((attr->nr == 1) || (attr->nr == 2)))
1058		min = 77;
1059	else
1060		min = abituguru_pwm_min[attr->nr];
1061
1062	/* this check can be done before taking the lock */
1063	if (val < min || val > abituguru_pwm_max[attr->nr])
1064		return -EINVAL;
1065
1066	mutex_lock(&data->update_lock);
1067	/* this check needs to be done after taking the lock */
1068	if ((attr->nr & 1) &&
1069			(val >= data->pwm_settings[attr->index][attr->nr + 1]))
1070		ret = -EINVAL;
1071	else if (!(attr->nr & 1) &&
1072			(val <= data->pwm_settings[attr->index][attr->nr - 1]))
1073		ret = -EINVAL;
1074	else if (data->pwm_settings[attr->index][attr->nr] != val) {
1075		u8 orig_val = data->pwm_settings[attr->index][attr->nr];
1076		data->pwm_settings[attr->index][attr->nr] = val;
1077		if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
1078				attr->index, data->pwm_settings[attr->index],
1079				5) <= attr->nr) {
1080			data->pwm_settings[attr->index][attr->nr] =
1081				orig_val;
1082			ret = -EIO;
1083		}
1084	}
1085	mutex_unlock(&data->update_lock);
1086	return ret;
1087}
1088
1089static ssize_t show_pwm_sensor(struct device *dev,
1090	struct device_attribute *devattr, char *buf)
1091{
1092	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1093	struct abituguru_data *data = dev_get_drvdata(dev);
1094	int i;
1095	/*
1096	 * We need to walk to the temp sensor addresses to find what
1097	 * the userspace id of the configured temp sensor is.
1098	 */
1099	for (i = 0; i < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]; i++)
1100		if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][i] ==
1101				(data->pwm_settings[attr->index][0] & 0x0F))
1102			return sprintf(buf, "%d\n", i+1);
1103
1104	return -ENXIO;
1105}
1106
1107static ssize_t store_pwm_sensor(struct device *dev, struct device_attribute
1108	*devattr, const char *buf, size_t count)
1109{
1110	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1111	struct abituguru_data *data = dev_get_drvdata(dev);
1112	ssize_t ret;
1113	unsigned long val;
1114	u8 orig_val;
1115	u8 address;
1116
1117	ret = kstrtoul(buf, 10, &val);
1118	if (ret)
1119		return ret;
1120
1121	if (val == 0 || val > data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR])
1122		return -EINVAL;
1123
1124	val -= 1;
1125	ret = count;
1126	mutex_lock(&data->update_lock);
1127	orig_val = data->pwm_settings[attr->index][0];
1128	address = data->bank1_address[ABIT_UGURU_TEMP_SENSOR][val];
1129	data->pwm_settings[attr->index][0] &= 0xF0;
1130	data->pwm_settings[attr->index][0] |= address;
1131	if (data->pwm_settings[attr->index][0] != orig_val) {
1132		if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1, attr->index,
1133				    data->pwm_settings[attr->index], 5) < 1) {
1134			data->pwm_settings[attr->index][0] = orig_val;
1135			ret = -EIO;
 
 
 
 
1136		}
1137	}
 
 
1138	mutex_unlock(&data->update_lock);
1139	return ret;
1140}
1141
1142static ssize_t show_pwm_enable(struct device *dev,
1143	struct device_attribute *devattr, char *buf)
1144{
1145	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1146	struct abituguru_data *data = dev_get_drvdata(dev);
1147	int res = 0;
1148	if (data->pwm_settings[attr->index][0] & ABIT_UGURU_FAN_PWM_ENABLE)
1149		res = 2;
1150	return sprintf(buf, "%d\n", res);
1151}
1152
1153static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
1154	*devattr, const char *buf, size_t count)
1155{
1156	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1157	struct abituguru_data *data = dev_get_drvdata(dev);
1158	u8 orig_val;
1159	ssize_t ret;
1160	unsigned long user_val;
1161
1162	ret = kstrtoul(buf, 10, &user_val);
1163	if (ret)
1164		return ret;
1165
1166	ret = count;
1167	mutex_lock(&data->update_lock);
1168	orig_val = data->pwm_settings[attr->index][0];
1169	switch (user_val) {
1170	case 0:
1171		data->pwm_settings[attr->index][0] &=
1172			~ABIT_UGURU_FAN_PWM_ENABLE;
1173		break;
1174	case 2:
1175		data->pwm_settings[attr->index][0] |= ABIT_UGURU_FAN_PWM_ENABLE;
1176		break;
1177	default:
1178		ret = -EINVAL;
 
1179	}
1180	if ((data->pwm_settings[attr->index][0] != orig_val) &&
1181			(abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
1182			attr->index, data->pwm_settings[attr->index],
1183			5) < 1)) {
1184		data->pwm_settings[attr->index][0] = orig_val;
1185		ret = -EIO;
1186	}
1187	mutex_unlock(&data->update_lock);
1188	return ret;
1189}
1190
1191static ssize_t show_name(struct device *dev,
1192	struct device_attribute *devattr, char *buf)
1193{
1194	return sprintf(buf, "%s\n", ABIT_UGURU_NAME);
1195}
1196
1197/* Sysfs attr templates, the real entries are generated automatically. */
1198static const
1199struct sensor_device_attribute_2 abituguru_sysfs_bank1_templ[2][9] = {
1200	{
1201	SENSOR_ATTR_2(in%d_input, 0444, show_bank1_value, NULL, 0, 0),
1202	SENSOR_ATTR_2(in%d_min, 0644, show_bank1_setting,
1203		store_bank1_setting, 1, 0),
1204	SENSOR_ATTR_2(in%d_min_alarm, 0444, show_bank1_alarm, NULL,
1205		ABIT_UGURU_VOLT_LOW_ALARM_FLAG, 0),
1206	SENSOR_ATTR_2(in%d_max, 0644, show_bank1_setting,
1207		store_bank1_setting, 2, 0),
1208	SENSOR_ATTR_2(in%d_max_alarm, 0444, show_bank1_alarm, NULL,
1209		ABIT_UGURU_VOLT_HIGH_ALARM_FLAG, 0),
1210	SENSOR_ATTR_2(in%d_beep, 0644, show_bank1_mask,
1211		store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1212	SENSOR_ATTR_2(in%d_shutdown, 0644, show_bank1_mask,
1213		store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1214	SENSOR_ATTR_2(in%d_min_alarm_enable, 0644, show_bank1_mask,
1215		store_bank1_mask, ABIT_UGURU_VOLT_LOW_ALARM_ENABLE, 0),
1216	SENSOR_ATTR_2(in%d_max_alarm_enable, 0644, show_bank1_mask,
1217		store_bank1_mask, ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE, 0),
1218	}, {
1219	SENSOR_ATTR_2(temp%d_input, 0444, show_bank1_value, NULL, 0, 0),
1220	SENSOR_ATTR_2(temp%d_alarm, 0444, show_bank1_alarm, NULL,
1221		ABIT_UGURU_TEMP_HIGH_ALARM_FLAG, 0),
1222	SENSOR_ATTR_2(temp%d_max, 0644, show_bank1_setting,
1223		store_bank1_setting, 1, 0),
1224	SENSOR_ATTR_2(temp%d_crit, 0644, show_bank1_setting,
1225		store_bank1_setting, 2, 0),
1226	SENSOR_ATTR_2(temp%d_beep, 0644, show_bank1_mask,
1227		store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1228	SENSOR_ATTR_2(temp%d_shutdown, 0644, show_bank1_mask,
1229		store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1230	SENSOR_ATTR_2(temp%d_alarm_enable, 0644, show_bank1_mask,
1231		store_bank1_mask, ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE, 0),
1232	}
1233};
1234
1235static const struct sensor_device_attribute_2 abituguru_sysfs_fan_templ[6] = {
1236	SENSOR_ATTR_2(fan%d_input, 0444, show_bank2_value, NULL, 0, 0),
1237	SENSOR_ATTR_2(fan%d_alarm, 0444, show_bank2_alarm, NULL, 0, 0),
1238	SENSOR_ATTR_2(fan%d_min, 0644, show_bank2_setting,
1239		store_bank2_setting, 1, 0),
1240	SENSOR_ATTR_2(fan%d_beep, 0644, show_bank2_mask,
1241		store_bank2_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1242	SENSOR_ATTR_2(fan%d_shutdown, 0644, show_bank2_mask,
1243		store_bank2_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1244	SENSOR_ATTR_2(fan%d_alarm_enable, 0644, show_bank2_mask,
1245		store_bank2_mask, ABIT_UGURU_FAN_LOW_ALARM_ENABLE, 0),
1246};
1247
1248static const struct sensor_device_attribute_2 abituguru_sysfs_pwm_templ[6] = {
1249	SENSOR_ATTR_2(pwm%d_enable, 0644, show_pwm_enable,
1250		store_pwm_enable, 0, 0),
1251	SENSOR_ATTR_2(pwm%d_auto_channels_temp, 0644, show_pwm_sensor,
1252		store_pwm_sensor, 0, 0),
1253	SENSOR_ATTR_2(pwm%d_auto_point1_pwm, 0644, show_pwm_setting,
1254		store_pwm_setting, 1, 0),
1255	SENSOR_ATTR_2(pwm%d_auto_point2_pwm, 0644, show_pwm_setting,
1256		store_pwm_setting, 2, 0),
1257	SENSOR_ATTR_2(pwm%d_auto_point1_temp, 0644, show_pwm_setting,
1258		store_pwm_setting, 3, 0),
1259	SENSOR_ATTR_2(pwm%d_auto_point2_temp, 0644, show_pwm_setting,
1260		store_pwm_setting, 4, 0),
1261};
1262
1263static struct sensor_device_attribute_2 abituguru_sysfs_attr[] = {
1264	SENSOR_ATTR_2(name, 0444, show_name, NULL, 0, 0),
1265};
1266
1267static int __devinit abituguru_probe(struct platform_device *pdev)
1268{
1269	struct abituguru_data *data;
1270	int i, j, used, sysfs_names_free, sysfs_attr_i, res = -ENODEV;
1271	char *sysfs_filename;
1272
1273	/*
1274	 * El weirdo probe order, to keep the sysfs order identical to the
1275	 * BIOS and window-appliction listing order.
1276	 */
1277	const u8 probe_order[ABIT_UGURU_MAX_BANK1_SENSORS] = {
1278		0x00, 0x01, 0x03, 0x04, 0x0A, 0x08, 0x0E, 0x02,
1279		0x09, 0x06, 0x05, 0x0B, 0x0F, 0x0D, 0x07, 0x0C };
1280
1281	data = kzalloc(sizeof(struct abituguru_data), GFP_KERNEL);
1282	if (!data)
1283		return -ENOMEM;
1284
1285	data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
1286	mutex_init(&data->update_lock);
1287	platform_set_drvdata(pdev, data);
1288
1289	/* See if the uGuru is ready */
1290	if (inb_p(data->addr + ABIT_UGURU_DATA) == ABIT_UGURU_STATUS_INPUT)
1291		data->uguru_ready = 1;
1292
1293	/*
1294	 * Completely read the uGuru this has 2 purposes:
1295	 * - testread / see if one really is there.
1296	 * - make an in memory copy of all the uguru settings for future use.
1297	 */
1298	if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1299			data->alarms, 3, ABIT_UGURU_MAX_RETRIES) != 3)
1300		goto abituguru_probe_error;
1301
1302	for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1303		if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, i,
1304				&data->bank1_value[i], 1,
1305				ABIT_UGURU_MAX_RETRIES) != 1)
1306			goto abituguru_probe_error;
1307		if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1+1, i,
1308				data->bank1_settings[i], 3,
1309				ABIT_UGURU_MAX_RETRIES) != 3)
1310			goto abituguru_probe_error;
1311	}
1312	/*
1313	 * Note: We don't know how many bank2 sensors / pwms there really are,
1314	 * but in order to "detect" this we need to read the maximum amount
1315	 * anyways. If we read sensors/pwms not there we'll just read crap
1316	 * this can't hurt. We need the detection because we don't want
1317	 * unwanted writes, which will hurt!
1318	 */
1319	for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
1320		if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
1321				&data->bank2_value[i], 1,
1322				ABIT_UGURU_MAX_RETRIES) != 1)
1323			goto abituguru_probe_error;
1324		if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2+1, i,
1325				data->bank2_settings[i], 2,
1326				ABIT_UGURU_MAX_RETRIES) != 2)
1327			goto abituguru_probe_error;
1328	}
1329	for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
1330		if (abituguru_read(data, ABIT_UGURU_FAN_PWM, i,
1331				data->pwm_settings[i], 5,
1332				ABIT_UGURU_MAX_RETRIES) != 5)
1333			goto abituguru_probe_error;
1334	}
1335	data->last_updated = jiffies;
1336
1337	/* Detect sensor types and fill the sysfs attr for bank1 */
1338	sysfs_attr_i = 0;
1339	sysfs_filename = data->sysfs_names;
1340	sysfs_names_free = ABITUGURU_SYSFS_NAMES_LENGTH;
1341	for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1342		res = abituguru_detect_bank1_sensor_type(data, probe_order[i]);
1343		if (res < 0)
1344			goto abituguru_probe_error;
1345		if (res == ABIT_UGURU_NC)
1346			continue;
1347
1348		/* res 1 (temp) sensors have 7 sysfs entries, 0 (in) 9 */
1349		for (j = 0; j < (res ? 7 : 9); j++) {
1350			used = snprintf(sysfs_filename, sysfs_names_free,
1351				abituguru_sysfs_bank1_templ[res][j].dev_attr.
1352				attr.name, data->bank1_sensors[res] + res)
1353				+ 1;
1354			data->sysfs_attr[sysfs_attr_i] =
1355				abituguru_sysfs_bank1_templ[res][j];
1356			data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1357				sysfs_filename;
1358			data->sysfs_attr[sysfs_attr_i].index = probe_order[i];
1359			sysfs_filename += used;
1360			sysfs_names_free -= used;
1361			sysfs_attr_i++;
1362		}
1363		data->bank1_max_value[probe_order[i]] =
1364			abituguru_bank1_max_value[res];
1365		data->bank1_address[res][data->bank1_sensors[res]] =
1366			probe_order[i];
1367		data->bank1_sensors[res]++;
1368	}
1369	/* Detect number of sensors and fill the sysfs attr for bank2 (fans) */
1370	abituguru_detect_no_bank2_sensors(data);
1371	for (i = 0; i < data->bank2_sensors; i++) {
1372		for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_fan_templ); j++) {
1373			used = snprintf(sysfs_filename, sysfs_names_free,
1374				abituguru_sysfs_fan_templ[j].dev_attr.attr.name,
1375				i + 1) + 1;
1376			data->sysfs_attr[sysfs_attr_i] =
1377				abituguru_sysfs_fan_templ[j];
1378			data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1379				sysfs_filename;
1380			data->sysfs_attr[sysfs_attr_i].index = i;
1381			sysfs_filename += used;
1382			sysfs_names_free -= used;
1383			sysfs_attr_i++;
1384		}
1385	}
1386	/* Detect number of sensors and fill the sysfs attr for pwms */
1387	abituguru_detect_no_pwms(data);
1388	for (i = 0; i < data->pwms; i++) {
1389		for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_pwm_templ); j++) {
1390			used = snprintf(sysfs_filename, sysfs_names_free,
1391				abituguru_sysfs_pwm_templ[j].dev_attr.attr.name,
1392				i + 1) + 1;
1393			data->sysfs_attr[sysfs_attr_i] =
1394				abituguru_sysfs_pwm_templ[j];
1395			data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1396				sysfs_filename;
1397			data->sysfs_attr[sysfs_attr_i].index = i;
1398			sysfs_filename += used;
1399			sysfs_names_free -= used;
1400			sysfs_attr_i++;
1401		}
1402	}
1403	/* Fail safe check, this should never happen! */
1404	if (sysfs_names_free < 0) {
1405		pr_err("Fatal error ran out of space for sysfs attr names. %s %s",
1406		       never_happen, report_this);
1407		res = -ENAMETOOLONG;
1408		goto abituguru_probe_error;
1409	}
1410	pr_info("found Abit uGuru\n");
1411
1412	/* Register sysfs hooks */
1413	for (i = 0; i < sysfs_attr_i; i++)
1414		if (device_create_file(&pdev->dev,
1415				&data->sysfs_attr[i].dev_attr))
1416			goto abituguru_probe_error;
1417	for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1418		if (device_create_file(&pdev->dev,
1419				&abituguru_sysfs_attr[i].dev_attr))
1420			goto abituguru_probe_error;
1421
1422	data->hwmon_dev = hwmon_device_register(&pdev->dev);
1423	if (!IS_ERR(data->hwmon_dev))
1424		return 0; /* success */
1425
1426	res = PTR_ERR(data->hwmon_dev);
1427abituguru_probe_error:
1428	for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
1429		device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
1430	for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1431		device_remove_file(&pdev->dev,
1432			&abituguru_sysfs_attr[i].dev_attr);
1433	platform_set_drvdata(pdev, NULL);
1434	kfree(data);
1435	return res;
1436}
1437
1438static int __devexit abituguru_remove(struct platform_device *pdev)
1439{
1440	int i;
1441	struct abituguru_data *data = platform_get_drvdata(pdev);
1442
1443	hwmon_device_unregister(data->hwmon_dev);
1444	for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
1445		device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
1446	for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1447		device_remove_file(&pdev->dev,
1448			&abituguru_sysfs_attr[i].dev_attr);
1449	platform_set_drvdata(pdev, NULL);
1450	kfree(data);
1451
1452	return 0;
1453}
1454
1455static struct abituguru_data *abituguru_update_device(struct device *dev)
1456{
1457	int i, err;
1458	struct abituguru_data *data = dev_get_drvdata(dev);
1459	/* fake a complete successful read if no update necessary. */
1460	char success = 1;
1461
1462	mutex_lock(&data->update_lock);
1463	if (time_after(jiffies, data->last_updated + HZ)) {
1464		success = 0;
1465		err = abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1466				     data->alarms, 3, 0);
1467		if (err != 3)
1468			goto LEAVE_UPDATE;
1469		for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1470			err = abituguru_read(data, ABIT_UGURU_SENSOR_BANK1,
1471					     i, &data->bank1_value[i], 1, 0);
1472			if (err != 1)
1473				goto LEAVE_UPDATE;
1474			err = abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
1475					     i, data->bank1_settings[i], 3, 0);
1476			if (err != 3)
1477				goto LEAVE_UPDATE;
1478		}
1479		for (i = 0; i < data->bank2_sensors; i++) {
1480			err = abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
1481					     &data->bank2_value[i], 1, 0);
1482			if (err != 1)
1483				goto LEAVE_UPDATE;
1484		}
1485		/* success! */
1486		success = 1;
1487		data->update_timeouts = 0;
1488LEAVE_UPDATE:
1489		/* handle timeout condition */
1490		if (!success && (err == -EBUSY || err >= 0)) {
1491			/* No overflow please */
1492			if (data->update_timeouts < 255u)
1493				data->update_timeouts++;
1494			if (data->update_timeouts <= ABIT_UGURU_MAX_TIMEOUTS) {
1495				ABIT_UGURU_DEBUG(3, "timeout exceeded, will "
1496					"try again next update\n");
1497				/* Just a timeout, fake a successful read */
1498				success = 1;
1499			} else
1500				ABIT_UGURU_DEBUG(1, "timeout exceeded %d "
1501					"times waiting for more input state\n",
1502					(int)data->update_timeouts);
1503		}
1504		/* On success set last_updated */
1505		if (success)
1506			data->last_updated = jiffies;
1507	}
1508	mutex_unlock(&data->update_lock);
1509
1510	if (success)
1511		return data;
1512	else
1513		return NULL;
1514}
1515
1516#ifdef CONFIG_PM
1517static int abituguru_suspend(struct platform_device *pdev, pm_message_t state)
1518{
1519	struct abituguru_data *data = platform_get_drvdata(pdev);
1520	/*
1521	 * make sure all communications with the uguru are done and no new
1522	 * ones are started
1523	 */
1524	mutex_lock(&data->update_lock);
1525	return 0;
1526}
1527
1528static int abituguru_resume(struct platform_device *pdev)
1529{
1530	struct abituguru_data *data = platform_get_drvdata(pdev);
1531	/* See if the uGuru is still ready */
1532	if (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT)
1533		data->uguru_ready = 0;
1534	mutex_unlock(&data->update_lock);
1535	return 0;
1536}
1537#else
1538#define abituguru_suspend	NULL
1539#define abituguru_resume	NULL
1540#endif /* CONFIG_PM */
1541
1542static struct platform_driver abituguru_driver = {
1543	.driver = {
1544		.owner	= THIS_MODULE,
1545		.name	= ABIT_UGURU_NAME,
1546	},
1547	.probe		= abituguru_probe,
1548	.remove		= __devexit_p(abituguru_remove),
1549	.suspend	= abituguru_suspend,
1550	.resume		= abituguru_resume,
1551};
1552
1553static int __init abituguru_detect(void)
1554{
1555	/*
1556	 * See if there is an uguru there. After a reboot uGuru will hold 0x00
1557	 * at DATA and 0xAC, when this driver has already been loaded once
1558	 * DATA will hold 0x08. For most uGuru's CMD will hold 0xAC in either
1559	 * scenario but some will hold 0x00.
1560	 * Some uGuru's initially hold 0x09 at DATA and will only hold 0x08
1561	 * after reading CMD first, so CMD must be read first!
1562	 */
1563	u8 cmd_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_CMD);
1564	u8 data_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_DATA);
1565	if (((data_val == 0x00) || (data_val == 0x08)) &&
1566	    ((cmd_val == 0x00) || (cmd_val == 0xAC)))
1567		return ABIT_UGURU_BASE;
1568
1569	ABIT_UGURU_DEBUG(2, "no Abit uGuru found, data = 0x%02X, cmd = "
1570		"0x%02X\n", (unsigned int)data_val, (unsigned int)cmd_val);
1571
1572	if (force) {
1573		pr_info("Assuming Abit uGuru is present because of \"force\" parameter\n");
1574		return ABIT_UGURU_BASE;
1575	}
1576
1577	/* No uGuru found */
1578	return -ENODEV;
1579}
1580
1581static struct platform_device *abituguru_pdev;
1582
1583static int __init abituguru_init(void)
1584{
1585	int address, err;
1586	struct resource res = { .flags = IORESOURCE_IO };
1587	const char *board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1588
1589	/* safety check, refuse to load on non Abit motherboards */
1590	if (!force && (!board_vendor ||
1591			strcmp(board_vendor, "http://www.abit.com.tw/")))
1592		return -ENODEV;
1593
1594	address = abituguru_detect();
1595	if (address < 0)
1596		return address;
1597
1598	err = platform_driver_register(&abituguru_driver);
1599	if (err)
1600		goto exit;
1601
1602	abituguru_pdev = platform_device_alloc(ABIT_UGURU_NAME, address);
1603	if (!abituguru_pdev) {
1604		pr_err("Device allocation failed\n");
1605		err = -ENOMEM;
1606		goto exit_driver_unregister;
1607	}
1608
1609	res.start = address;
1610	res.end = address + ABIT_UGURU_REGION_LENGTH - 1;
1611	res.name = ABIT_UGURU_NAME;
1612
1613	err = platform_device_add_resources(abituguru_pdev, &res, 1);
1614	if (err) {
1615		pr_err("Device resource addition failed (%d)\n", err);
1616		goto exit_device_put;
1617	}
1618
1619	err = platform_device_add(abituguru_pdev);
1620	if (err) {
1621		pr_err("Device addition failed (%d)\n", err);
1622		goto exit_device_put;
1623	}
1624
1625	return 0;
1626
1627exit_device_put:
1628	platform_device_put(abituguru_pdev);
1629exit_driver_unregister:
1630	platform_driver_unregister(&abituguru_driver);
1631exit:
1632	return err;
1633}
1634
1635static void __exit abituguru_exit(void)
1636{
1637	platform_device_unregister(abituguru_pdev);
1638	platform_driver_unregister(&abituguru_driver);
1639}
1640
1641MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
1642MODULE_DESCRIPTION("Abit uGuru Sensor device");
1643MODULE_LICENSE("GPL");
1644
1645module_init(abituguru_init);
1646module_exit(abituguru_exit);