Skip to content

Commit 857cc04

Browse files
hkallweitAndi Shyti
authored andcommitted
i2c: i801: Add helper i801_get_block_len
Avoid code duplication and factor out retrieving and checking the block length value to new helper i801_get_block_len(). Signed-off-by: Heiner Kallweit <[email protected]> Reviewed-by: Andi Shyti <[email protected]> Signed-off-by: Andi Shyti <[email protected]>
1 parent 29dae45 commit 857cc04

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

drivers/i2c/busses/i2c-i801.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,18 @@ MODULE_PARM_DESC(disable_features, "Disable selected driver features:\n"
330330
"\t\t 0x10 don't use interrupts\n"
331331
"\t\t 0x20 disable SMBus Host Notify ");
332332

333+
static int i801_get_block_len(struct i801_priv *priv)
334+
{
335+
u8 len = inb_p(SMBHSTDAT0(priv));
336+
337+
if (len < 1 || len > I2C_SMBUS_BLOCK_MAX) {
338+
pci_err(priv->pci_dev, "Illegal SMBus block read size %u\n", len);
339+
return -EPROTO;
340+
}
341+
342+
return len;
343+
}
344+
333345
static int i801_check_and_clear_pec_error(struct i801_priv *priv)
334346
{
335347
u8 status;
@@ -525,12 +537,11 @@ static int i801_block_transaction_by_block(struct i801_priv *priv,
525537

526538
if (read_write == I2C_SMBUS_READ ||
527539
command == I2C_SMBUS_BLOCK_PROC_CALL) {
528-
len = inb_p(SMBHSTDAT0(priv));
529-
if (len < 1 || len > I2C_SMBUS_BLOCK_MAX) {
530-
status = -EPROTO;
540+
status = i801_get_block_len(priv);
541+
if (status < 0)
531542
goto out;
532-
}
533543

544+
len = status;
534545
data->block[0] = len;
535546
for (i = 0; i < len; i++)
536547
data->block[i + 1] = inb_p(SMBBLKDAT(priv));
@@ -549,14 +560,11 @@ static void i801_isr_byte_done(struct i801_priv *priv)
549560
* and read the block length from SMBHSTDAT0.
550561
*/
551562
if (priv->len == SMBUS_LEN_SENTINEL) {
552-
priv->len = inb_p(SMBHSTDAT0(priv));
553-
if (priv->len < 1 || priv->len > I2C_SMBUS_BLOCK_MAX) {
554-
dev_err(&priv->pci_dev->dev,
555-
"Illegal SMBus block read size %d\n",
556-
priv->len);
563+
priv->len = i801_get_block_len(priv);
564+
if (priv->len < 0)
557565
/* FIXME: Recover */
558566
priv->len = I2C_SMBUS_BLOCK_MAX;
559-
}
567+
560568
priv->data[-1] = priv->len;
561569
}
562570

@@ -709,11 +717,8 @@ static int i801_block_transaction_byte_by_byte(struct i801_priv *priv,
709717
* and read the block length from SMBHSTDAT0.
710718
*/
711719
if (len == SMBUS_LEN_SENTINEL) {
712-
len = inb_p(SMBHSTDAT0(priv));
713-
if (len < 1 || len > I2C_SMBUS_BLOCK_MAX) {
714-
dev_err(&priv->pci_dev->dev,
715-
"Illegal SMBus block read size %d\n",
716-
len);
720+
len = i801_get_block_len(priv);
721+
if (len < 0) {
717722
/* Recover */
718723
while (inb_p(SMBHSTSTS(priv)) &
719724
SMBHSTSTS_HOST_BUSY)

0 commit comments

Comments
 (0)