From patchwork Tue Aug 21 17:02:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 144774 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp5572276ljj; Tue, 21 Aug 2018 10:57:29 -0700 (PDT) X-Google-Smtp-Source: AA+uWPxtd5ojuu6r0l4KwqEzFcnhFNg4MMDOOFcOkullHOrqB7kNBwMoGUWbBl8ni8CswmhtRe23 X-Received: by 2002:a0c:80a8:: with SMTP id 37-v6mr46628612qvb.13.1534874248915; Tue, 21 Aug 2018 10:57:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534874248; cv=none; d=google.com; s=arc-20160816; b=WfqZ8eu+zXQpkWGI9q1728gkvhBhH/JVRirwrncQFTqE4GbwpcqW1j6Shspq4aKMWf bcyKVFEr8W6vI9UaCGb/GdI3II/ujH52jVF1JthUR23MzaplnBsx+q0gmBtZhOECvmWQ W8ENqOsMkXzGTYpqJih+bu+R9g5DvzDO+BrX8qfTTg5N+W1TpC+p7z1PHD+7pGlerEDd htjid7zhITErrR3QF4R9bp3ooBc+lsf881Z7qN3VnyIfbIwJzIoYHq2alVJfjCtkB8n+ 7gY7pZ/mqqxSrocTtEkprJ5ZqVgFqM6reRbS1SlAAqdMww61pjZFqy3lWW5n4YDyuT22 XtMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:to:from:dkim-signature:arc-authentication-results; bh=T9Tg1DqLpdiuJyn7x0X6skkgxb66ALKSzVdNbjeRKMM=; b=pm2rc10NSWUpbbfkDM5X6txvb8wXs2api0ME6lF8sItQYjf+JgqMFCSEitJRAFxV+6 FV5EvygS+VXbl/R84nwNUkk58Fr9wtkp2E9gTMEmlEkZC2/Y6Y2M7fp/Cc5CYSCtExAK c1zFQDd0P7TTm8Z2zdYEHPTE16orzbHH+mfJy9mifbkkd2jGiLsr+PJM5/7YfHJW14W/ 9kqv0jfMITRhDYluCObJuvnGHi+Cr5Jd8xfyoeoORrZ9uqlLsW7m0N+sAUw0eDwLypwV Unu431ZXXTOM9ve9bVAd6rVmAMDj0Voqb/gCgWyW/N0VELo9/To7XLMYsekPg2etSOLf u7aQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=B65llAQV; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id v19-v6si2955217qkf.38.2018.08.21.10.57.28 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 21 Aug 2018 10:57:28 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=B65llAQV; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from localhost ([::1]:55225 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fsAuG-0006Ze-B3 for patch@linaro.org; Tue, 21 Aug 2018 13:57:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34072) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fsA57-0002aA-04 for qemu-devel@nongnu.org; Tue, 21 Aug 2018 13:04:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fsA4o-0007Dd-NT for qemu-devel@nongnu.org; Tue, 21 Aug 2018 13:04:31 -0400 Received: from mail-wm0-x232.google.com ([2a00:1450:400c:c09::232]:53648) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fsA4o-00072z-0s for qemu-devel@nongnu.org; Tue, 21 Aug 2018 13:04:18 -0400 Received: by mail-wm0-x232.google.com with SMTP id s9-v6so3626775wmh.3 for ; Tue, 21 Aug 2018 10:04:11 -0700 (PDT) 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=T9Tg1DqLpdiuJyn7x0X6skkgxb66ALKSzVdNbjeRKMM=; b=B65llAQV3zHrFEnhwT2UQJlkunezvNnGMHgL1Gy3/I74vvl7s1nL+oK8I+VKjGfXQD bkv74lEe/ggMivSO6rKy8uHVLrWe+bgf3eS4nx+IUL+3zSuVZezwalmkQ1FaQJsQgYiB aRcSV3nRR6NJJihK+kgurRIkJrprUuyq4oXnTOgp4jVNG3iYVmBNz1+HJHdhbWgjjZfy Ch1mTr/d/EJIz2yQsO4zNJzovIMPDlXTpY2zsZwfzZfzsyisbwkWKYO+NP9BHetq4AxY oZLDJi+3/iGJUwF21F76yDhdOZVwDafvZZ8xEMqBpXMFQXCHMQCl30O+mQfxpeLMIiQl O35Q== 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=T9Tg1DqLpdiuJyn7x0X6skkgxb66ALKSzVdNbjeRKMM=; b=NVixMfMz3bujFSiQYxjgyhi3IUxok4BDABfgZ9wXJJphVuMNHJx+M0d5Tn0+0NLRaF bT0ydkAJhNkno0mweYCZ2/4t5p7kV23ZuabnBF0HTi3NNL35D11zznmFH4EgV+7oBUw9 xeXX5cQUtg+f0Vw0FEYZ/OW/UGeRg0hMGFKv9LnF/eOa0AG6IkjOwJavZtweu30egDC4 9uB3/8+9UyQp4CHGXoeAlz7ey0RfHhk9bMYRIIJRWI+lPOmgDgfGP3u/hKgk0xdpZMCd Vn3oUm5atbt6GNhKIaiNrzvIrFF+yUBDLFZzi9AHkRNBxNCyOzmDxEA2/TsPH8HMHqmQ G6mw== X-Gm-Message-State: APzg51DkkKV0270p8Iz/Lj+BL0gjWiWNbpEsr2DQ9c2AGjKb+JmkfFjW TYgQLpkXRiHWncUMPkwxdBCZ1+F9 X-Received: by 2002:a1c:f913:: with SMTP id x19-v6mr136501wmh.63.1534871050018; Tue, 21 Aug 2018 10:04:10 -0700 (PDT) Received: from 640k.lan (dynamic-adsl-78-12-184-244.clienti.tiscali.it. [78.12.184.244]) by smtp.gmail.com with ESMTPSA id v6-v6sm2608955wmc.43.2018.08.21.10.04.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Aug 2018 10:04:09 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 21 Aug 2018 19:02:35 +0200 Message-Id: <1534870966-9287-64-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1534870966-9287-1-git-send-email-pbonzini@redhat.com> References: <1534870966-9287-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::232 Subject: [Qemu-devel] [PULL 63/74] 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 , "Michael S. Tsirkin" Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" 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 Message-Id: <1534796770-10295-3-git-send-email-minyard@acm.org> Signed-off-by: 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(-) -- 1.8.3.1 diff --git a/hw/i2c/pm_smbus.c b/hw/i2c/pm_smbus.c index 83c2377..f1fe889 100644 --- a/hw/i2c/pm_smbus.c +++ b/hw/i2c/pm_smbus.c @@ -117,10 +117,16 @@ static void smb_transaction(PMSMBus *s) break; case PROT_I2C_BLOCK_DATA: if (read) { - ret = smbus_read_block(bus, addr, cmd, s->smb_data); + int xfersize = s->smb_data0; + if (xfersize > sizeof(s->smb_data)) { + xfersize = sizeof(s->smb_data); + } + ret = smbus_read_block(bus, addr, s->smb_data1, s->smb_data, + xfersize, false, true); goto data8; } else { - ret = smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_data0); + ret = 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 587ce1a..6ff77c5 100644 --- a/hw/i2c/smbus.c +++ b/hw/i2c/smbus.c @@ -293,33 +293,42 @@ int smbus_write_word(I2CBus *bus, uint8_t addr, uint8_t command, uint16_t data) return 0; } -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; - 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 = i2c_recv(bus); - if (len > 32) { - len = 0; + if (recv_len) { + rlen = i2c_recv(bus); + } else { + rlen = len; } - for (i = 0; i < len; i++) { + if (rlen > len) { + rlen = 0; + } + for (i = 0; i < rlen; i++) { data[i] = i2c_recv(bus); } i2c_nack(bus); i2c_end_transfer(bus); - return len; + return rlen; } int smbus_write_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data, - int len) + int len, bool send_len) { int i; @@ -330,7 +339,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 = 0; i < len; i++) { i2c_send(bus, data[i]); } diff --git a/include/hw/i2c/smbus.h b/include/hw/i2c/smbus.h index 4fdba02..d8b1b9e 100644 --- a/include/hw/i2c/smbus.h +++ b/include/hw/i2c/smbus.h @@ -72,9 +72,22 @@ int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command); int smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t data); 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); void smbus_eeprom_init_one(I2CBus *smbus, uint8_t address, uint8_t *eeprom_buf); void smbus_eeprom_init(I2CBus *smbus, int nb_eeprom,