Skip to content

Commit 7e908b7

Browse files
committed
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()
2 parents 0c04511 + 60268b0 commit 7e908b7

5 files changed

Lines changed: 115 additions & 72 deletions

File tree

drivers/hwmon/amd_energy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ static umode_t amd_energy_is_visible(const void *_data,
171171
enum hwmon_sensor_types type,
172172
u32 attr, int channel)
173173
{
174-
return 0444;
174+
return 0440;
175175
}
176176

177177
static int energy_accumulator(void *p)

drivers/hwmon/applesmc.c

Lines changed: 82 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <linux/hwmon.h>
3333
#include <linux/workqueue.h>
3434
#include <linux/err.h>
35+
#include <linux/bits.h>
3536

3637
/* data port used by Apple SMC */
3738
#define APPLESMC_DATA_PORT 0x300
@@ -42,10 +43,13 @@
4243

4344
#define APPLESMC_MAX_DATA_LENGTH 32
4445

45-
/* wait up to 128 ms for a status change. */
46-
#define APPLESMC_MIN_WAIT 0x0010
47-
#define APPLESMC_RETRY_WAIT 0x0100
48-
#define APPLESMC_MAX_WAIT 0x20000
46+
/* Apple SMC status bits */
47+
#define SMC_STATUS_AWAITING_DATA BIT(0) /* SMC has data waiting to be read */
48+
#define SMC_STATUS_IB_CLOSED BIT(1) /* Will ignore any input */
49+
#define SMC_STATUS_BUSY BIT(2) /* Command in progress */
50+
51+
/* Initial wait is 8us */
52+
#define APPLESMC_MIN_WAIT 0x0008
4953

5054
#define APPLESMC_READ_CMD 0x10
5155
#define APPLESMC_WRITE_CMD 0x11
@@ -151,65 +155,84 @@ static unsigned int key_at_index;
151155
static struct workqueue_struct *applesmc_led_wq;
152156

153157
/*
154-
* wait_read - Wait for a byte to appear on SMC port. Callers must
155-
* hold applesmc_lock.
158+
* Wait for specific status bits with a mask on the SMC.
159+
* Used before all transactions.
160+
* This does 10 fast loops of 8us then exponentially backs off for a
161+
* minimum total wait of 262ms. Depending on usleep_range this could
162+
* run out past 500ms.
156163
*/
157-
static int wait_read(void)
164+
165+
static int wait_status(u8 val, u8 mask)
158166
{
159-
unsigned long end = jiffies + (APPLESMC_MAX_WAIT * HZ) / USEC_PER_SEC;
160167
u8 status;
161168
int us;
169+
int i;
162170

163-
for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
164-
usleep_range(us, us * 16);
171+
us = APPLESMC_MIN_WAIT;
172+
for (i = 0; i < 24 ; i++) {
165173
status = inb(APPLESMC_CMD_PORT);
166-
/* read: wait for smc to settle */
167-
if (status & 0x01)
174+
if ((status & mask) == val)
168175
return 0;
169-
/* timeout: give up */
170-
if (time_after(jiffies, end))
171-
break;
176+
usleep_range(us, us * 2);
177+
if (i > 9)
178+
us <<= 1;
172179
}
173-
174-
pr_warn("wait_read() fail: 0x%02x\n", status);
175180
return -EIO;
176181
}
177182

178-
/*
179-
* send_byte - Write to SMC port, retrying when necessary. Callers
180-
* must hold applesmc_lock.
181-
*/
183+
/* send_byte - Write to SMC data port. Callers must hold applesmc_lock. */
184+
182185
static int send_byte(u8 cmd, u16 port)
183186
{
184-
u8 status;
185-
int us;
186-
unsigned long end = jiffies + (APPLESMC_MAX_WAIT * HZ) / USEC_PER_SEC;
187+
int status;
188+
189+
status = wait_status(0, SMC_STATUS_IB_CLOSED);
190+
if (status)
191+
return status;
192+
/*
193+
* This needs to be a separate read looking for bit 0x04
194+
* after bit 0x02 falls. If consolidated with the wait above
195+
* this extra read may not happen if status returns both
196+
* simultaneously and this would appear to be required.
197+
*/
198+
status = wait_status(SMC_STATUS_BUSY, SMC_STATUS_BUSY);
199+
if (status)
200+
return status;
187201

188202
outb(cmd, port);
189-
for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
190-
usleep_range(us, us * 16);
191-
status = inb(APPLESMC_CMD_PORT);
192-
/* write: wait for smc to settle */
193-
if (status & 0x02)
194-
continue;
195-
/* ready: cmd accepted, return */
196-
if (status & 0x04)
197-
return 0;
198-
/* timeout: give up */
199-
if (time_after(jiffies, end))
200-
break;
201-
/* busy: long wait and resend */
202-
udelay(APPLESMC_RETRY_WAIT);
203-
outb(cmd, port);
204-
}
205-
206-
pr_warn("send_byte(0x%02x, 0x%04x) fail: 0x%02x\n", cmd, port, status);
207-
return -EIO;
203+
return 0;
208204
}
209205

206+
/* send_command - Write a command to the SMC. Callers must hold applesmc_lock. */
207+
210208
static int send_command(u8 cmd)
211209
{
212-
return send_byte(cmd, APPLESMC_CMD_PORT);
210+
int ret;
211+
212+
ret = wait_status(0, SMC_STATUS_IB_CLOSED);
213+
if (ret)
214+
return ret;
215+
outb(cmd, APPLESMC_CMD_PORT);
216+
return 0;
217+
}
218+
219+
/*
220+
* Based on logic from the Apple driver. This is issued before any interaction
221+
* If busy is stuck high, issue a read command to reset the SMC state machine.
222+
* If busy is stuck high after the command then the SMC is jammed.
223+
*/
224+
225+
static int smc_sane(void)
226+
{
227+
int ret;
228+
229+
ret = wait_status(0, SMC_STATUS_BUSY);
230+
if (!ret)
231+
return ret;
232+
ret = send_command(APPLESMC_READ_CMD);
233+
if (ret)
234+
return ret;
235+
return wait_status(0, SMC_STATUS_BUSY);
213236
}
214237

215238
static int send_argument(const char *key)
@@ -226,6 +249,11 @@ static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
226249
{
227250
u8 status, data = 0;
228251
int i;
252+
int ret;
253+
254+
ret = smc_sane();
255+
if (ret)
256+
return ret;
229257

230258
if (send_command(cmd) || send_argument(key)) {
231259
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)
239267
}
240268

241269
for (i = 0; i < len; i++) {
242-
if (wait_read()) {
270+
if (wait_status(SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY,
271+
SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY)) {
243272
pr_warn("%.4s: read data[%d] fail\n", key, i);
244273
return -EIO;
245274
}
@@ -250,19 +279,24 @@ static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
250279
for (i = 0; i < 16; i++) {
251280
udelay(APPLESMC_MIN_WAIT);
252281
status = inb(APPLESMC_CMD_PORT);
253-
if (!(status & 0x01))
282+
if (!(status & SMC_STATUS_AWAITING_DATA))
254283
break;
255284
data = inb(APPLESMC_DATA_PORT);
256285
}
257286
if (i)
258287
pr_warn("flushed %d bytes, last value is: %d\n", i, data);
259288

260-
return 0;
289+
return wait_status(0, SMC_STATUS_BUSY);
261290
}
262291

263292
static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
264293
{
265294
int i;
295+
int ret;
296+
297+
ret = smc_sane();
298+
if (ret)
299+
return ret;
266300

267301
if (send_command(cmd) || send_argument(key)) {
268302
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)
281315
}
282316
}
283317

284-
return 0;
318+
return wait_status(0, SMC_STATUS_BUSY);
285319
}
286320

287321
static int read_register_count(unsigned int *count)

drivers/hwmon/pmbus/max20730.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ static ssize_t max20730_debugfs_read(struct file *file, char __user *buf,
122122
switch (idx) {
123123
case MAX20730_DEBUGFS_VOUT_MIN:
124124
ret = VOLT_FROM_REG(data->mfr_voutmin * 10000);
125-
len = snprintf(tbuf, DEBUG_FS_DATA_MAX, "%d.%d\n",
126-
ret / 10000, ret % 10000);
125+
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d.%d\n",
126+
ret / 10000, ret % 10000);
127127
break;
128128
case MAX20730_DEBUGFS_FREQUENCY:
129129
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,
141141
ret = 800;
142142
else
143143
ret = 900;
144-
len = snprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
144+
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
145145
break;
146146
case MAX20730_DEBUGFS_PG_DELAY:
147147
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,
223223
case MAX20730_DEBUGFS_OC_PROTECT_MODE:
224224
ret = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_OCPM_MASK)
225225
>> MAX20730_MFR_DEVSET2_OCPM_BIT_POS;
226-
len = snprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
226+
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
227227
break;
228228
case MAX20730_DEBUGFS_SS_TIMING:
229229
val = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_SS_MASK)
@@ -241,50 +241,50 @@ static ssize_t max20730_debugfs_read(struct file *file, char __user *buf,
241241
case MAX20730_DEBUGFS_IMAX:
242242
ret = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_IMAX_MASK)
243243
>> MAX20730_MFR_DEVSET2_IMAX_BIT_POS;
244-
len = snprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
244+
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
245245
break;
246246
case MAX20730_DEBUGFS_OPERATION:
247247
ret = i2c_smbus_read_byte_data(psu->client, PMBUS_OPERATION);
248248
if (ret < 0)
249249
return ret;
250-
len = snprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
250+
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
251251
break;
252252
case MAX20730_DEBUGFS_ON_OFF_CONFIG:
253253
ret = i2c_smbus_read_byte_data(psu->client, PMBUS_ON_OFF_CONFIG);
254254
if (ret < 0)
255255
return ret;
256-
len = snprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
256+
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
257257
break;
258258
case MAX20730_DEBUGFS_SMBALERT_MASK:
259259
ret = i2c_smbus_read_word_data(psu->client,
260260
PMBUS_SMB_ALERT_MASK);
261261
if (ret < 0)
262262
return ret;
263-
len = snprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
263+
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
264264
break;
265265
case MAX20730_DEBUGFS_VOUT_MODE:
266266
ret = i2c_smbus_read_byte_data(psu->client, PMBUS_VOUT_MODE);
267267
if (ret < 0)
268268
return ret;
269-
len = snprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
269+
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
270270
break;
271271
case MAX20730_DEBUGFS_VOUT_COMMAND:
272272
ret = i2c_smbus_read_word_data(psu->client, PMBUS_VOUT_COMMAND);
273273
if (ret < 0)
274274
return ret;
275275

276276
ret = VOLT_FROM_REG(ret * 10000);
277-
len = snprintf(tbuf, DEBUG_FS_DATA_MAX,
278-
"%d.%d\n", ret / 10000, ret % 10000);
277+
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX,
278+
"%d.%d\n", ret / 10000, ret % 10000);
279279
break;
280280
case MAX20730_DEBUGFS_VOUT_MAX:
281281
ret = i2c_smbus_read_word_data(psu->client, PMBUS_VOUT_MAX);
282282
if (ret < 0)
283283
return ret;
284284

285285
ret = VOLT_FROM_REG(ret * 10000);
286-
len = snprintf(tbuf, DEBUG_FS_DATA_MAX,
287-
"%d.%d\n", ret / 10000, ret % 10000);
286+
len = scnprintf(tbuf, DEBUG_FS_DATA_MAX,
287+
"%d.%d\n", ret / 10000, ret % 10000);
288288
break;
289289
default:
290290
len = strlcpy(tbuf, "Invalid\n", DEBUG_FS_DATA_MAX);

drivers/hwmon/pmbus/pmbus_core.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -941,12 +941,16 @@ static ssize_t pmbus_show_sensor(struct device *dev,
941941
struct i2c_client *client = to_i2c_client(dev->parent);
942942
struct pmbus_sensor *sensor = to_pmbus_sensor(devattr);
943943
struct pmbus_data *data = i2c_get_clientdata(client);
944+
ssize_t ret;
944945

946+
mutex_lock(&data->update_lock);
945947
pmbus_update_sensor_data(client, sensor);
946948
if (sensor->data < 0)
947-
return sensor->data;
948-
949-
return snprintf(buf, PAGE_SIZE, "%lld\n", pmbus_reg2data(data, sensor));
949+
ret = sensor->data;
950+
else
951+
ret = snprintf(buf, PAGE_SIZE, "%lld\n", pmbus_reg2data(data, sensor));
952+
mutex_unlock(&data->update_lock);
953+
return ret;
950954
}
951955

952956
static ssize_t pmbus_set_sensor(struct device *dev,
@@ -2012,8 +2016,11 @@ static ssize_t pmbus_show_samples(struct device *dev,
20122016
int val;
20132017
struct i2c_client *client = to_i2c_client(dev->parent);
20142018
struct pmbus_samples_reg *reg = to_samples_reg(devattr);
2019+
struct pmbus_data *data = i2c_get_clientdata(client);
20152020

2021+
mutex_lock(&data->update_lock);
20162022
val = _pmbus_read_word_data(client, reg->page, 0xff, reg->attr->reg);
2023+
mutex_unlock(&data->update_lock);
20172024
if (val < 0)
20182025
return val;
20192026

drivers/hwmon/pwm-fan.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,18 @@ static irqreturn_t pulse_handler(int irq, void *dev_id)
5454
static void sample_timer(struct timer_list *t)
5555
{
5656
struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
57+
unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start);
5758
int pulses;
58-
u64 tmp;
5959

60-
pulses = atomic_read(&ctx->pulses);
61-
atomic_sub(pulses, &ctx->pulses);
62-
tmp = (u64)pulses * ktime_ms_delta(ktime_get(), ctx->sample_start) * 60;
63-
do_div(tmp, ctx->pulses_per_revolution * 1000);
64-
ctx->rpm = tmp;
60+
if (delta) {
61+
pulses = atomic_read(&ctx->pulses);
62+
atomic_sub(pulses, &ctx->pulses);
63+
ctx->rpm = (unsigned int)(pulses * 1000 * 60) /
64+
(ctx->pulses_per_revolution * delta);
65+
66+
ctx->sample_start = ktime_get();
67+
}
6568

66-
ctx->sample_start = ktime_get();
6769
mod_timer(&ctx->rpm_timer, jiffies + HZ);
6870
}
6971

0 commit comments

Comments
 (0)