From nobody Tue Feb 10 16:57:58 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1512683729171710.6281558715594; Thu, 7 Dec 2017 13:55:29 -0800 (PST) Received: from localhost ([::1]:34539 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eN48Y-00057V-G3 for importer@patchew.org; Thu, 07 Dec 2017 16:55:22 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51303) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eN3zz-000406-Pp for qemu-devel@nongnu.org; Thu, 07 Dec 2017 16:46:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eN3zx-0006K3-GV for qemu-devel@nongnu.org; Thu, 07 Dec 2017 16:46:31 -0500 Received: from mail-pg0-x242.google.com ([2607:f8b0:400e:c05::242]:45556) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eN3zx-0006JR-97 for qemu-devel@nongnu.org; Thu, 07 Dec 2017 16:46:29 -0500 Received: by mail-pg0-x242.google.com with SMTP id m25so5377697pgv.12 for ; Thu, 07 Dec 2017 13:46:29 -0800 (PST) Received: from serve.minyard.net ([47.184.168.85]) by smtp.gmail.com with ESMTPSA id y5sm11252936pfd.63.2017.12.07.13.46.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 07 Dec 2017 13:46:26 -0800 (PST) Received: from t430.minyard.net (t430m.minyard.net [192.168.27.3]) by serve.minyard.net (Postfix) with ESMTPA id E46A6901; Thu, 7 Dec 2017 15:46:23 -0600 (CST) Received: by t430.minyard.net (Postfix, from userid 1000) id DD0E130007F; Thu, 7 Dec 2017 15:46:22 -0600 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=WHrimqKmkjyLEmn1gNtbYkUZxVq1HyjZWMrrEDTcNTo=; b=r3sDHMMEGkTrX1ZIiggpcNqmw4GvsgMNBHzq7FynVANLU1UtsJUext7bsPIRbJLbCz RakA5FOcYgrzQne01p9w00FDMUlXMFYHjlxW/ytq5txXrVeQ/2o6I5/ifEpQViHuPYUh wFEbFxzkZdV5qGadlMWJ66W0gy5X8GrtTm7byhKDc0OrsVwMcCo85HE11XTyYyhq6w0u tJ5OVgTIQbrTLKbGnRGIJ62iky8Po8k1ZmcGgoBOej24+zMmcJVYVF0Q2XhReyzMBcsn xcwZuIxJ71GgsJYISlVwNUjfw8Vf24NtKAlo+qXV+EXpwZW+wfv9waDhMDGcNKLZYlkM 4syA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=WHrimqKmkjyLEmn1gNtbYkUZxVq1HyjZWMrrEDTcNTo=; b=SYrkvTkBkynSd94ZUSJ34bJEBMkMFdpkbS4EmL3mtUXxU/rs6dEGYHD5m83sq+fAyW 72RnvT2EPctx7QEw7Lw7PMlET7wgXy8f2yBofG1IoJc3jPmSzOqIq/7+HrcjTcXLBxI6 +Zv7fX3YV26joIlLV1LcqfYtSccsaOoCHVrXp4bpV6L7C3nnsAjRwh4b3ZzA2BCODING 6LJtMwsCvIqijyHdWUtJrPlwuCdSEee9A7tc8obIvGRPMnkKTXz9StGskg2yE9v8MP5m nqejYuN2XaKj6RlbMemM/ebiln/lew0THSzJK/m4USH+uYCjB9q/SnaEcOVgHO1p+bhd sSnw== X-Gm-Message-State: AJaThX4VWsmnSd7X0uRODCI5837VGQoOrVqcroq07WnN8X6cXtKZQ/Ll Ru4jPCQNy+vhkzEWzsxDIA+QubQ= X-Google-Smtp-Source: AGs4zMZZDZUSlMs/TP9MDmz7RFdX85/7yPyGrqrGdpgwnfcxsRqFa0ZCHRMGzaIRTDyraTxNvO0N4w== X-Received: by 10.99.152.68 with SMTP id l4mr27256195pgo.208.1512683188127; Thu, 07 Dec 2017 13:46:28 -0800 (PST) From: minyard@acm.org To: qemu-devel@nongnu.org Date: Thu, 7 Dec 2017 15:46:09 -0600 Message-Id: <1512683181-8420-3-git-send-email-minyard@acm.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1512683181-8420-1-git-send-email-minyard@acm.org> References: <1512683181-8420-1-git-send-email-minyard@acm.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::242 Subject: [Qemu-devel] [PATCH 02/14] i2c:pm_smbus: Fix the semantics of block I2C transfers X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Corey Minyard , Paolo Bonzini , "Michael S . Tsirkin" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Corey Minyard The I2C block transfer commands was not implemented correctly, it read a length byte and such like it was an smbus transfer. So fix the smbus_read_block() and smbus_write_block() functions so they can properly handle I2C transfers, and normal SMBus transfers (for upcoming changes). Pass in a transfer size and a bool to know whether to use the size byte (like SMBus) or use the length given (like I2C). Signed-off-by: Corey Minyard Cc: Michael S. Tsirkin Cc: Paolo Bonzini --- hw/i2c/pm_smbus.c | 10 ++++++++-- hw/i2c/smbus.c | 37 ++++++++++++++++++++++++------------- include/hw/i2c/smbus.h | 17 +++++++++++++++-- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/hw/i2c/pm_smbus.c b/hw/i2c/pm_smbus.c index 92c3aebd..679edbc 100644 --- a/hw/i2c/pm_smbus.c +++ b/hw/i2c/pm_smbus.c @@ -115,10 +115,16 @@ static void smb_transaction(PMSMBus *s) break; case PROT_I2C_BLOCK_DATA: if (read) { - ret =3D smbus_read_block(bus, addr, cmd, s->smb_data); + int xfersize =3D s->smb_data0; + if (xfersize > sizeof(s->smb_data)) { + xfersize =3D sizeof(s->smb_data); + } + ret =3D smbus_read_block(bus, addr, s->smb_data1, s->smb_data, + xfersize, false, true); goto data8; } else { - ret =3D smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_= data0); + ret =3D smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_= data0, + false); goto done; } break; diff --git a/hw/i2c/smbus.c b/hw/i2c/smbus.c index 2d1b79a..4b0e264 100644 --- a/hw/i2c/smbus.c +++ b/hw/i2c/smbus.c @@ -301,33 +301,42 @@ int smbus_write_word(I2CBus *bus, uint8_t addr, uint8= _t command, uint16_t data) return 0; } =20 -int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *= data) +int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *= data, + int len, bool recv_len, bool send_cmd) { - int len; + int rlen; int i; =20 - if (i2c_start_transfer(bus, addr, 0)) { - return -1; + if (send_cmd) { + if (i2c_start_transfer(bus, addr, 0)) { + return -1; + } + i2c_send(bus, command); } - i2c_send(bus, command); if (i2c_start_transfer(bus, addr, 1)) { - i2c_end_transfer(bus); + if (send_cmd) { + i2c_end_transfer(bus); + } return -1; } - len =3D i2c_recv(bus); - if (len > 32) { - len =3D 0; + if (recv_len) { + rlen =3D i2c_recv(bus); + } else { + rlen =3D len; } - for (i =3D 0; i < len; i++) { + if (rlen > len) { + rlen =3D 0; + } + for (i =3D 0; i < rlen; i++) { data[i] =3D i2c_recv(bus); } i2c_nack(bus); i2c_end_transfer(bus); - return len; + return rlen; } =20 int smbus_write_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t = *data, - int len) + int len, bool send_len) { int i; =20 @@ -338,7 +347,9 @@ int smbus_write_block(I2CBus *bus, uint8_t addr, uint8_= t command, uint8_t *data, return -1; } i2c_send(bus, command); - i2c_send(bus, len); + if (send_len) { + i2c_send(bus, len); + } for (i =3D 0; i < len; i++) { i2c_send(bus, data[i]); } diff --git a/include/hw/i2c/smbus.h b/include/hw/i2c/smbus.h index 544bbc1..f1b8078 100644 --- a/include/hw/i2c/smbus.h +++ b/include/hw/i2c/smbus.h @@ -73,9 +73,22 @@ int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t c= ommand); int smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t d= ata); int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command); int smbus_write_word(I2CBus *bus, uint8_t addr, uint8_t command, uint16_t = data); -int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *= data); + +/* + * Do a block transfer from an I2C device. If recv_len is set, then the + * first received byte is a length field and is used to know how much data + * to receive. Otherwise receive "len" bytes. If send_cmd is set, send + * the command byte first before receiving the data. + */ +int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *= data, + int len, bool recv_len, bool send_cmd); + +/* + * Do a block transfer to an I2C device. If send_len is set, send the + * "len" value before the data. + */ int smbus_write_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t = *data, - int len); + int len, bool send_len); =20 void smbus_eeprom_init(I2CBus *smbus, int nb_eeprom, const uint8_t *eeprom_spd, int size); --=20 2.7.4