Commit 7e908b74 authored by Linus Torvalds's avatar Linus Torvalds

Merge tag 'hwmon-for-v5.10-rc4' of...

Merge tag 'hwmon-for-v5.10-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/groeck/linux-staging

Pull hwmon fixes from Guenter Roeck:

 - Fix potential bufer overflow in pmbus/max20730 driver

 - Fix locking issue in pmbus core

 - Fix regression causing timeouts in applesmc driver

 - Fix RPM calculation in pwm-fan driver

 - Restrict counter visibility in amd_energy driver

* tag 'hwmon-for-v5.10-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/groeck/linux-staging:
  hwmon: (amd_energy) modify the visibility of the counters
  hwmon: (applesmc) Re-work SMC comms
  hwmon: (pwm-fan) Fix RPM calculation
  hwmon: (pmbus) Add mutex locking for sysfs reads
  hwmon: (pmbus/max20730) use scnprintf() instead of snprintf()
parents 0c045111 60268b0e
......@@ -171,7 +171,7 @@ static umode_t amd_energy_is_visible(const void *_data,
enum hwmon_sensor_types type,
u32 attr, int channel)
{
return 0444;
return 0440;
}
static int energy_accumulator(void *p)
......
......@@ -32,6 +32,7 @@
#include <linux/hwmon.h>
#include <linux/workqueue.h>
#include <linux/err.h>
#include <linux/bits.h>
/* data port used by Apple SMC */
#define APPLESMC_DATA_PORT 0x300
......@@ -42,10 +43,13 @@
#define APPLESMC_MAX_DATA_LENGTH 32
/* wait up to 128 ms for a status change. */
#define APPLESMC_MIN_WAIT 0x0010
#define APPLESMC_RETRY_WAIT 0x0100
#define APPLESMC_MAX_WAIT 0x20000
/* Apple SMC status bits */
#define SMC_STATUS_AWAITING_DATA BIT(0) /* SMC has data waiting to be read */
#define SMC_STATUS_IB_CLOSED BIT(1) /* Will ignore any input */
#define SMC_STATUS_BUSY BIT(2) /* Command in progress */
/* Initial wait is 8us */
#define APPLESMC_MIN_WAIT 0x0008
#define APPLESMC_READ_CMD 0x10
#define APPLESMC_WRITE_CMD 0x11
......@@ -151,65 +155,84 @@ static unsigned int key_at_index;
static struct workqueue_struct *applesmc_led_wq;
/*
* wait_read - Wait for a byte to appear on SMC port. Callers must
* hold applesmc_lock.
* Wait for specific status bits with a mask on the SMC.
* Used before all transactions.
* This does 10 fast loops of 8us then exponentially backs off for a
* minimum total wait of 262ms. Depending on usleep_range this could
* run out past 500ms.
*/
static int wait_read(void)
static int wait_status(u8 val, u8 mask)
{
unsigned long end = jiffies + (APPLESMC_MAX_WAIT * HZ) / USEC_PER_SEC;
u8 status;
int us;
int i;
for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
usleep_range(us, us * 16);
us = APPLESMC_MIN_WAIT;
for (i = 0; i < 24 ; i++) {
status = inb(APPLESMC_CMD_PORT);
/* read: wait for smc to settle */
if (status & 0x01)
if ((status & mask) == val)
return 0;
/* timeout: give up */
if (time_after(jiffies, end))
break;
usleep_range(us, us * 2);
if (i > 9)
us <<= 1;
}
pr_warn("wait_read() fail: 0x%02x\n", status);
return -EIO;
}
/*
* send_byte - Write to SMC port, retrying when necessary. Callers
* must hold applesmc_lock.
*/
/* send_byte - Write to SMC data port. Callers must hold applesmc_lock. */
static int send_byte(u8 cmd, u16 port)
{
u8 status;
int us;
unsigned long end = jiffies + (APPLESMC_MAX_WAIT * HZ) / USEC_PER_SEC;
int status;
status = wait_status(0, SMC_STATUS_IB_CLOSED);
if (status)
return status;
/*
* This needs to be a separate read looking for bit 0x04
* after bit 0x02 falls. If consolidated with the wait above
* this extra read may not happen if status returns both
* simultaneously and this would appear to be required.
*/
status = wait_status(SMC_STATUS_BUSY, SMC_STATUS_BUSY);
if (status)
return status;
outb(cmd, port);
for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
usleep_range(us, us * 16);
status = inb(APPLESMC_CMD_PORT);
/* write: wait for smc to settle */
if (status & 0x02)
continue;
/* ready: cmd accepted, return */
if (status & 0x04)
return 0;
/* timeout: give up */
if (time_after(jiffies, end))
break;
/* busy: long wait and resend */
udelay(APPLESMC_RETRY_WAIT);
outb(cmd, port);
}
pr_warn("send_byte(0x%02x, 0x%04x) fail: 0x%02x\n", cmd, port, status);
return -EIO;
return 0;
}
/* send_command - Write a command to the SMC. Callers must hold applesmc_lock. */
static int send_command(u8 cmd)
{
return send_byte(cmd, APPLESMC_CMD_PORT);
int ret;
ret = wait_status(0, SMC_STATUS_IB_CLOSED);
if (ret)
return ret;
outb(cmd, APPLESMC_CMD_PORT);
return 0;
}
/*
* Based on logic from the Apple driver. This is issued before any interaction
* If busy is stuck high, issue a read command to reset the SMC state machine.
* If busy is stuck high after the command then the SMC is jammed.
*/
static int smc_sane(void)
{
int ret;
ret = wait_status(0, SMC_STATUS_BUSY);
if (!ret)
return ret;
ret = send_command(APPLESMC_READ_CMD);
if (ret)
return ret;
return wait_status(0, SMC_STATUS_BUSY);
}
static int send_argument(const char *key)
......@@ -226,6 +249,11 @@ static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
{
u8 status, data = 0;
int i;
int ret;
ret = smc_sane();
if (ret)
return ret;
if (send_command(cmd) || send_argument(key)) {
pr_warn("%.4s: read arg fail\n", key);
......@@ -239,7 +267,8 @@ static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
}
for (i = 0; i < len; i++) {
if (wait_read()) {
if (wait_status(SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY,
SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY)) {
pr_warn("%.4s: read data[%d] fail\n", key, i);
return -EIO;
}
......@@ -250,19 +279,24 @@ static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
for (i = 0; i < 16; i++) {
udelay(APPLESMC_MIN_WAIT);
status = inb(APPLESMC_CMD_PORT);
if (!(status & 0x01))
if (!(status & SMC_STATUS_AWAITING_DATA))
break;
data = inb(APPLESMC_DATA_PORT);
}
if (i)
pr_warn("flushed %d bytes, last value is: %d\n", i, data);
return 0;
return wait_status(0, SMC_STATUS_BUSY);
}
static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
{
int i;
int ret;
ret = smc_sane();
if (ret)
return ret;
if (send_command(cmd) || send_argument(key)) {
pr_warn("%s: write arg fail\n", key);
......@@ -281,7 +315,7 @@ static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
}
}
return 0;
return wait_status(0, SMC_STATUS_BUSY);
}
static int read_register_count(unsigned int *count)
......
......@@ -122,8 +122,8 @@ static ssize_t max20730_debugfs_read(struct file *file, char __user *buf,
switch (idx) {
case MAX20730_DEBUGFS_VOUT_MIN:
ret = VOLT_FROM_REG(data->mfr_voutmin * 10000);
len = snprintf(tbuf, DEBUG_FS_DATA_MAX, "%d.%d\n",
ret / 10000, ret % 10000);
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d.%d\n",
ret / 10000, ret % 10000);
break;
case MAX20730_DEBUGFS_FREQUENCY:
val = (data->mfr_devset1 & MAX20730_MFR_DEVSET1_FSW_MASK)
......@@ -141,7 +141,7 @@ static ssize_t max20730_debugfs_read(struct file *file, char __user *buf,
ret = 800;
else
ret = 900;
len = snprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
break;
case MAX20730_DEBUGFS_PG_DELAY:
val = (data->mfr_devset1 & MAX20730_MFR_DEVSET1_TSTAT_MASK)
......@@ -223,7 +223,7 @@ static ssize_t max20730_debugfs_read(struct file *file, char __user *buf,
case MAX20730_DEBUGFS_OC_PROTECT_MODE:
ret = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_OCPM_MASK)
>> MAX20730_MFR_DEVSET2_OCPM_BIT_POS;
len = snprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
break;
case MAX20730_DEBUGFS_SS_TIMING:
val = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_SS_MASK)
......@@ -241,32 +241,32 @@ static ssize_t max20730_debugfs_read(struct file *file, char __user *buf,
case MAX20730_DEBUGFS_IMAX:
ret = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_IMAX_MASK)
>> MAX20730_MFR_DEVSET2_IMAX_BIT_POS;
len = snprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
break;
case MAX20730_DEBUGFS_OPERATION:
ret = i2c_smbus_read_byte_data(psu->client, PMBUS_OPERATION);
if (ret < 0)
return ret;
len = snprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
break;
case MAX20730_DEBUGFS_ON_OFF_CONFIG:
ret = i2c_smbus_read_byte_data(psu->client, PMBUS_ON_OFF_CONFIG);
if (ret < 0)
return ret;
len = snprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
break;
case MAX20730_DEBUGFS_SMBALERT_MASK:
ret = i2c_smbus_read_word_data(psu->client,
PMBUS_SMB_ALERT_MASK);
if (ret < 0)
return ret;
len = snprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
break;
case MAX20730_DEBUGFS_VOUT_MODE:
ret = i2c_smbus_read_byte_data(psu->client, PMBUS_VOUT_MODE);
if (ret < 0)
return ret;
len = snprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
break;
case MAX20730_DEBUGFS_VOUT_COMMAND:
ret = i2c_smbus_read_word_data(psu->client, PMBUS_VOUT_COMMAND);
......@@ -274,8 +274,8 @@ static ssize_t max20730_debugfs_read(struct file *file, char __user *buf,
return ret;
ret = VOLT_FROM_REG(ret * 10000);
len = snprintf(tbuf, DEBUG_FS_DATA_MAX,
"%d.%d\n", ret / 10000, ret % 10000);
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX,
"%d.%d\n", ret / 10000, ret % 10000);
break;
case MAX20730_DEBUGFS_VOUT_MAX:
ret = i2c_smbus_read_word_data(psu->client, PMBUS_VOUT_MAX);
......@@ -283,8 +283,8 @@ static ssize_t max20730_debugfs_read(struct file *file, char __user *buf,
return ret;
ret = VOLT_FROM_REG(ret * 10000);
len = snprintf(tbuf, DEBUG_FS_DATA_MAX,
"%d.%d\n", ret / 10000, ret % 10000);
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX,
"%d.%d\n", ret / 10000, ret % 10000);
break;
default:
len = strlcpy(tbuf, "Invalid\n", DEBUG_FS_DATA_MAX);
......
......@@ -941,12 +941,16 @@ static ssize_t pmbus_show_sensor(struct device *dev,
struct i2c_client *client = to_i2c_client(dev->parent);
struct pmbus_sensor *sensor = to_pmbus_sensor(devattr);
struct pmbus_data *data = i2c_get_clientdata(client);
ssize_t ret;
mutex_lock(&data->update_lock);
pmbus_update_sensor_data(client, sensor);
if (sensor->data < 0)
return sensor->data;
return snprintf(buf, PAGE_SIZE, "%lld\n", pmbus_reg2data(data, sensor));
ret = sensor->data;
else
ret = snprintf(buf, PAGE_SIZE, "%lld\n", pmbus_reg2data(data, sensor));
mutex_unlock(&data->update_lock);
return ret;
}
static ssize_t pmbus_set_sensor(struct device *dev,
......@@ -2012,8 +2016,11 @@ static ssize_t pmbus_show_samples(struct device *dev,
int val;
struct i2c_client *client = to_i2c_client(dev->parent);
struct pmbus_samples_reg *reg = to_samples_reg(devattr);
struct pmbus_data *data = i2c_get_clientdata(client);
mutex_lock(&data->update_lock);
val = _pmbus_read_word_data(client, reg->page, 0xff, reg->attr->reg);
mutex_unlock(&data->update_lock);
if (val < 0)
return val;
......
......@@ -54,16 +54,18 @@ static irqreturn_t pulse_handler(int irq, void *dev_id)
static void sample_timer(struct timer_list *t)
{
struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start);
int pulses;
u64 tmp;
pulses = atomic_read(&ctx->pulses);
atomic_sub(pulses, &ctx->pulses);
tmp = (u64)pulses * ktime_ms_delta(ktime_get(), ctx->sample_start) * 60;
do_div(tmp, ctx->pulses_per_revolution * 1000);
ctx->rpm = tmp;
if (delta) {
pulses = atomic_read(&ctx->pulses);
atomic_sub(pulses, &ctx->pulses);
ctx->rpm = (unsigned int)(pulses * 1000 * 60) /
(ctx->pulses_per_revolution * delta);
ctx->sample_start = ktime_get();
}
ctx->sample_start = ktime_get();
mod_timer(&ctx->rpm_timer, jiffies + HZ);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment