abituguru.c
来自「linux 内核源代码」· C语言 代码 · 共 1,516 行 · 第 1/4 页
C
1,516 行
/* abituguru.c Copyright (c) 2005-2006 Hans de Goede <j.w.r.degoede@hhs.nl> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*//* This driver supports the sensor part of the first and second revision of the custom Abit uGuru chip found on Abit uGuru motherboards. Note: because of lack of specs the CPU/RAM voltage & frequency control is not supported!*/#include <linux/module.h>#include <linux/sched.h>#include <linux/init.h>#include <linux/slab.h>#include <linux/jiffies.h>#include <linux/mutex.h>#include <linux/err.h>#include <linux/delay.h>#include <linux/platform_device.h>#include <linux/hwmon.h>#include <linux/hwmon-sysfs.h>#include <linux/dmi.h>#include <asm/io.h>/* Banks */#define ABIT_UGURU_ALARM_BANK 0x20 /* 1x 3 bytes */#define ABIT_UGURU_SENSOR_BANK1 0x21 /* 16x volt and temp */#define ABIT_UGURU_FAN_PWM 0x24 /* 3x 5 bytes */#define ABIT_UGURU_SENSOR_BANK2 0x26 /* fans *//* max nr of sensors in bank1, a bank1 sensor can be in, temp or nc */#define ABIT_UGURU_MAX_BANK1_SENSORS 16/* Warning if you increase one of the 2 MAX defines below to 10 or higher you should adjust the belonging _NAMES_LENGTH macro for the 2 digit number! *//* max nr of sensors in bank2, currently mb's with max 6 fans are known */#define ABIT_UGURU_MAX_BANK2_SENSORS 6/* max nr of pwm outputs, currently mb's with max 5 pwm outputs are known */#define ABIT_UGURU_MAX_PWMS 5/* uGuru sensor bank 1 flags */ /* Alarm if: */#define ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE 0x01 /* temp over warn */#define ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE 0x02 /* volt over max */#define ABIT_UGURU_VOLT_LOW_ALARM_ENABLE 0x04 /* volt under min */#define ABIT_UGURU_TEMP_HIGH_ALARM_FLAG 0x10 /* temp is over warn */#define ABIT_UGURU_VOLT_HIGH_ALARM_FLAG 0x20 /* volt is over max */#define ABIT_UGURU_VOLT_LOW_ALARM_FLAG 0x40 /* volt is under min *//* uGuru sensor bank 2 flags */ /* Alarm if: */#define ABIT_UGURU_FAN_LOW_ALARM_ENABLE 0x01 /* fan under min *//* uGuru sensor bank common flags */#define ABIT_UGURU_BEEP_ENABLE 0x08 /* beep if alarm */#define ABIT_UGURU_SHUTDOWN_ENABLE 0x80 /* shutdown if alarm *//* uGuru fan PWM (speed control) flags */#define ABIT_UGURU_FAN_PWM_ENABLE 0x80 /* enable speed control *//* Values used for conversion */#define ABIT_UGURU_FAN_MAX 15300 /* RPM *//* Bank1 sensor types */#define ABIT_UGURU_IN_SENSOR 0#define ABIT_UGURU_TEMP_SENSOR 1#define ABIT_UGURU_NC 2/* In many cases we need to wait for the uGuru to reach a certain status, most of the time it will reach this status within 30 - 90 ISA reads, and thus we can best busy wait. This define gives the total amount of reads to try. */#define ABIT_UGURU_WAIT_TIMEOUT 125/* However sometimes older versions of the uGuru seem to be distracted and they do not respond for a long time. To handle this we sleep before each of the last ABIT_UGURU_WAIT_TIMEOUT_SLEEP tries. */#define ABIT_UGURU_WAIT_TIMEOUT_SLEEP 5/* Normally all expected status in abituguru_ready, are reported after the first read, but sometimes not and we need to poll. */#define ABIT_UGURU_READY_TIMEOUT 5/* Maximum 3 retries on timedout reads/writes, delay 200 ms before retrying */#define ABIT_UGURU_MAX_RETRIES 3#define ABIT_UGURU_RETRY_DELAY (HZ/5)/* Maximum 2 timeouts in abituguru_update_device, iow 3 in a row is an error */#define ABIT_UGURU_MAX_TIMEOUTS 2/* utility macros */#define ABIT_UGURU_NAME "abituguru"#define ABIT_UGURU_DEBUG(level, format, arg...) \ if (level <= verbose) \ printk(KERN_DEBUG ABIT_UGURU_NAME ": " format , ## arg)/* Macros to help calculate the sysfs_names array length *//* sum of strlen of: in??_input\0, in??_{min,max}\0, in??_{min,max}_alarm\0, in??_{min,max}_alarm_enable\0, in??_beep\0, in??_shutdown\0 */#define ABITUGURU_IN_NAMES_LENGTH (11 + 2 * 9 + 2 * 15 + 2 * 22 + 10 + 14)/* sum of strlen of: temp??_input\0, temp??_max\0, temp??_crit\0, temp??_alarm\0, temp??_alarm_enable\0, temp??_beep\0, temp??_shutdown\0 */#define ABITUGURU_TEMP_NAMES_LENGTH (13 + 11 + 12 + 13 + 20 + 12 + 16)/* sum of strlen of: fan?_input\0, fan?_min\0, fan?_alarm\0, fan?_alarm_enable\0, fan?_beep\0, fan?_shutdown\0 */#define ABITUGURU_FAN_NAMES_LENGTH (11 + 9 + 11 + 18 + 10 + 14)/* sum of strlen of: pwm?_enable\0, pwm?_auto_channels_temp\0, pwm?_auto_point{1,2}_pwm\0, pwm?_auto_point{1,2}_temp\0 */#define ABITUGURU_PWM_NAMES_LENGTH (12 + 24 + 2 * 21 + 2 * 22)/* IN_NAMES_LENGTH > TEMP_NAMES_LENGTH so assume all bank1 sensors are in */#define ABITUGURU_SYSFS_NAMES_LENGTH ( \ ABIT_UGURU_MAX_BANK1_SENSORS * ABITUGURU_IN_NAMES_LENGTH + \ ABIT_UGURU_MAX_BANK2_SENSORS * ABITUGURU_FAN_NAMES_LENGTH + \ ABIT_UGURU_MAX_PWMS * ABITUGURU_PWM_NAMES_LENGTH)/* All the macros below are named identical to the oguru and oguru2 programs reverse engineered by Olle Sandberg, hence the names might not be 100% logical. I could come up with better names, but I prefer keeping the names identical so that this driver can be compared with his work more easily. *//* Two i/o-ports are used by uGuru */#define ABIT_UGURU_BASE 0x00E0/* Used to tell uGuru what to read and to read the actual data */#define ABIT_UGURU_CMD 0x00/* Mostly used to check if uGuru is busy */#define ABIT_UGURU_DATA 0x04#define ABIT_UGURU_REGION_LENGTH 5/* uGuru status' */#define ABIT_UGURU_STATUS_WRITE 0x00 /* Ready to be written */#define ABIT_UGURU_STATUS_READ 0x01 /* Ready to be read */#define ABIT_UGURU_STATUS_INPUT 0x08 /* More input */#define ABIT_UGURU_STATUS_READY 0x09 /* Ready to be written *//* Constants *//* in (Volt) sensors go up to 3494 mV, temp to 255000 millidegrees Celsius */static const int abituguru_bank1_max_value[2] = { 3494, 255000 };/* Min / Max allowed values for sensor2 (fan) alarm threshold, these values correspond to 300-3000 RPM */static const u8 abituguru_bank2_min_threshold = 5;static const u8 abituguru_bank2_max_threshold = 50;/* Register 0 is a bitfield, 1 and 2 are pwm settings (255 = 100%), 3 and 4 are temperature trip points. */static const int abituguru_pwm_settings_multiplier[5] = { 0, 1, 1, 1000, 1000 };/* Min / Max allowed values for pwm_settings. Note: pwm1 (CPU fan) is a special case the minium allowed pwm% setting for this is 30% (77) on some MB's this special case is handled in the code! */static const u8 abituguru_pwm_min[5] = { 0, 170, 170, 25, 25 };static const u8 abituguru_pwm_max[5] = { 0, 255, 255, 75, 75 };/* Insmod parameters */static int force;module_param(force, bool, 0);MODULE_PARM_DESC(force, "Set to one to force detection.");static int bank1_types[ABIT_UGURU_MAX_BANK1_SENSORS] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };module_param_array(bank1_types, int, NULL, 0);MODULE_PARM_DESC(bank1_types, "Bank1 sensortype autodetection override:\n" " -1 autodetect\n" " 0 volt sensor\n" " 1 temp sensor\n" " 2 not connected");static int fan_sensors;module_param(fan_sensors, int, 0);MODULE_PARM_DESC(fan_sensors, "Number of fan sensors on the uGuru " "(0 = autodetect)");static int pwms;module_param(pwms, int, 0);MODULE_PARM_DESC(pwms, "Number of PWMs on the uGuru " "(0 = autodetect)");/* Default verbose is 2, since this driver is still in the testing phase */static int verbose = 2;module_param(verbose, int, 0644);MODULE_PARM_DESC(verbose, "How verbose should the driver be? (0-3):\n" " 0 normal output\n" " 1 + verbose error reporting\n" " 2 + sensors type probing info\n" " 3 + retryable error reporting");/* For the Abit uGuru, we need to keep some data in memory. The structure is dynamically allocated, at the same time when a new abituguru device is allocated. */struct abituguru_data { struct device *hwmon_dev; /* hwmon registered device */ struct mutex update_lock; /* protect access to data and uGuru */ unsigned long last_updated; /* In jiffies */ unsigned short addr; /* uguru base address */ char uguru_ready; /* is the uguru in ready state? */ unsigned char update_timeouts; /* number of update timeouts since last successful update */ /* The sysfs attr and their names are generated automatically, for bank1 we cannot use a predefined array because we don't know beforehand of a sensor is a volt or a temp sensor, for bank2 and the pwms its easier todo things the same way. For in sensors we have 9 (temp 7) sysfs entries per sensor, for bank2 and pwms 6. */ struct sensor_device_attribute_2 sysfs_attr[ ABIT_UGURU_MAX_BANK1_SENSORS * 9 + ABIT_UGURU_MAX_BANK2_SENSORS * 6 + ABIT_UGURU_MAX_PWMS * 6]; /* Buffer to store the dynamically generated sysfs names */ char sysfs_names[ABITUGURU_SYSFS_NAMES_LENGTH]; /* Bank 1 data */ /* number of and addresses of [0] in, [1] temp sensors */ u8 bank1_sensors[2]; u8 bank1_address[2][ABIT_UGURU_MAX_BANK1_SENSORS]; u8 bank1_value[ABIT_UGURU_MAX_BANK1_SENSORS]; /* This array holds 3 entries per sensor for the bank 1 sensor settings (flags, min, max for voltage / flags, warn, shutdown for temp). */ u8 bank1_settings[ABIT_UGURU_MAX_BANK1_SENSORS][3]; /* Maximum value for each sensor used for scaling in mV/millidegrees Celsius. */ int bank1_max_value[ABIT_UGURU_MAX_BANK1_SENSORS]; /* Bank 2 data, ABIT_UGURU_MAX_BANK2_SENSORS entries for bank2 */ u8 bank2_sensors; /* actual number of bank2 sensors found */ u8 bank2_value[ABIT_UGURU_MAX_BANK2_SENSORS]; u8 bank2_settings[ABIT_UGURU_MAX_BANK2_SENSORS][2]; /* flags, min */ /* Alarms 2 bytes for bank1, 1 byte for bank2 */ u8 alarms[3]; /* Fan PWM (speed control) 5 bytes per PWM */ u8 pwms; /* actual number of pwms found */ u8 pwm_settings[ABIT_UGURU_MAX_PWMS][5];};/* wait till the uguru is in the specified state */static int abituguru_wait(struct abituguru_data *data, u8 state){ int timeout = ABIT_UGURU_WAIT_TIMEOUT; while (inb_p(data->addr + ABIT_UGURU_DATA) != state) { timeout--; if (timeout == 0) return -EBUSY; /* sleep a bit before our last few tries, see the comment on this where ABIT_UGURU_WAIT_TIMEOUT_SLEEP is defined. */ if (timeout <= ABIT_UGURU_WAIT_TIMEOUT_SLEEP) msleep(0); } return 0;}/* Put the uguru in ready for input state */static int abituguru_ready(struct abituguru_data *data){ int timeout = ABIT_UGURU_READY_TIMEOUT; if (data->uguru_ready) return 0; /* Reset? / Prepare for next read/write cycle */ outb(0x00, data->addr + ABIT_UGURU_DATA); /* Wait till the uguru is ready */ if (abituguru_wait(data, ABIT_UGURU_STATUS_READY)) { ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for ready state\n"); return -EIO; } /* Cmd port MUST be read now and should contain 0xAC */ while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) { timeout--; if (timeout == 0) { ABIT_UGURU_DEBUG(1, "CMD reg does not hold 0xAC after ready command\n"); return -EIO; } msleep(0); } /* After this the ABIT_UGURU_DATA port should contain ABIT_UGURU_STATUS_INPUT */ timeout = ABIT_UGURU_READY_TIMEOUT; while (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT) { timeout--; if (timeout == 0) { ABIT_UGURU_DEBUG(1, "state != more input after ready command\n"); return -EIO; } msleep(0); } data->uguru_ready = 1; return 0;}/* Send the bank and then sensor address to the uGuru for the next read/write cycle. This function gets called as the first part of a read/write by abituguru_read and abituguru_write. This function should never be called by any other function. */static int abituguru_send_address(struct abituguru_data *data, u8 bank_addr, u8 sensor_addr, int retries){ /* assume the caller does error handling itself if it has not requested any retries, and thus be quiet. */ int report_errors = retries; for (;;) { /* Make sure the uguru is ready and then send the bank address, after this the uguru is no longer "ready". */ if (abituguru_ready(data) != 0) return -EIO; outb(bank_addr, data->addr + ABIT_UGURU_DATA); data->uguru_ready = 0; /* Wait till the uguru is ABIT_UGURU_STATUS_INPUT state again and send the sensor addr */ if (abituguru_wait(data, ABIT_UGURU_STATUS_INPUT)) { if (retries) { ABIT_UGURU_DEBUG(3, "timeout exceeded " "waiting for more input state, %d " "tries remaining\n", retries); set_current_state(TASK_UNINTERRUPTIBLE); schedule_timeout(ABIT_UGURU_RETRY_DELAY); retries--; continue; } if (report_errors) ABIT_UGURU_DEBUG(1, "timeout exceeded " "waiting for more input state " "(bank: %d)\n", (int)bank_addr); return -EBUSY; } outb(sensor_addr, data->addr + ABIT_UGURU_CMD); return 0; }}/* Read count bytes from sensor sensor_addr in bank bank_addr and store the result in buf, retry the send address part of the read retries times. */static int abituguru_read(struct abituguru_data *data, u8 bank_addr, u8 sensor_addr, u8 *buf, int count, int retries){ int i; /* Send the address */ i = abituguru_send_address(data, bank_addr, sensor_addr, retries); if (i) return i; /* And read the data */ for (i = 0; i < count; i++) { if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) { ABIT_UGURU_DEBUG(retries ? 1 : 3, "timeout exceeded waiting for " "read state (bank: %d, sensor: %d)\n", (int)bank_addr, (int)sensor_addr); break; } buf[i] = inb(data->addr + ABIT_UGURU_CMD); } /* Last put the chip back in ready state */ abituguru_ready(data); return i;}/* Write count bytes from buf to sensor sensor_addr in bank bank_addr, the send address part of the write is always retried ABIT_UGURU_MAX_RETRIES times. */static int abituguru_write(struct abituguru_data *data, u8 bank_addr, u8 sensor_addr, u8 *buf, int count){ /* We use the ready timeout as we have to wait for 0xAC just like the ready function */ int i, timeout = ABIT_UGURU_READY_TIMEOUT; /* Send the address */ i = abituguru_send_address(data, bank_addr, sensor_addr, ABIT_UGURU_MAX_RETRIES); if (i) return i; /* And write the data */ for (i = 0; i < count; i++) { if (abituguru_wait(data, ABIT_UGURU_STATUS_WRITE)) { ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for " "write state (bank: %d, sensor: %d)\n", (int)bank_addr, (int)sensor_addr); break;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?