From patchwork Wed Feb 20 13:59:43 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Corey Minyard X-Patchwork-Id: 10822511 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 03F2117E9 for ; Wed, 20 Feb 2019 16:40:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DC0F92F25C for ; Wed, 20 Feb 2019 16:40:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CC0202F261; Wed, 20 Feb 2019 16:40:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id BCECA2F25C for ; Wed, 20 Feb 2019 16:40:01 +0000 (UTC) Received: from localhost ([127.0.0.1]:42480 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gwUuf-0006Td-1Z for patchwork-qemu-devel@patchwork.kernel.org; Wed, 20 Feb 2019 11:40:01 -0500 Received: from eggs.gnu.org ([209.51.188.92]:38876) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gwUK8-0006z0-PW for qemu-devel@nongnu.org; Wed, 20 Feb 2019 11:02:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gwUK0-0003LM-1n for qemu-devel@nongnu.org; Wed, 20 Feb 2019 11:02:16 -0500 Received: from mail-ot1-x344.google.com ([2607:f8b0:4864:20::344]:36033) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gwThn-0005pK-TD for qemu-devel@nongnu.org; Wed, 20 Feb 2019 10:22:40 -0500 Received: by mail-ot1-x344.google.com with SMTP id v62so31873013otb.3 for ; Wed, 20 Feb 2019 07:21:15 -0800 (PST) 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=r3vzE3iMO2lxtAlRHJptW6ozanNzYhiqBJrBz7a1j4k=; b=Z/zn9l4WXWBtuAMVo/VdPrLAv1ySTy44czUvOT5qI3iCzoPAz3n/Ixnif+Py8kJWNV yjoDOpGneAlM0j8oJcA8JTwzaml/xNxxO9YnBUWmiZUJkT1ITdOYBxaIPgQEY/KJ/xwk QYQ2gxj1D3AmQWKzL+ofJJYhBkh47hXvHW4Kp04VNer7vZZ18Mwp7kw+m2NytYCC6AME UUEzM10vpEaZn8z3S7uALCJVMUjHkwq6YfIoM/dmIS5Ysq/7cjRcMfghCj6ak3BOFiaJ BTy9vJ7A0m6RhoHpZ6tfnOeNhYd06kvhRuTFykLBDX6Dwunf6+vcJTnVVyapPNRmK4Db uQPA== 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=r3vzE3iMO2lxtAlRHJptW6ozanNzYhiqBJrBz7a1j4k=; b=c0V6lKlzlVTbxk3NQbVuPNneHA631+CyAiQpJZ7oQtTztsk+hLT1qByGiQ9ZQrqsR2 88GzXULADPS0QZxebZ4vwlgCEPexDi5L9I9bduiuB6mlLI+ZF4ctvgxvlh+cub4FC7nB 0jopkFUcLIJuGxrmgMltBIleQI9xh2KswZWat7yiZJHwwhDS9s7V6TUB7Bhcyj6t0ku9 ZbuiJRh7fCE3gkn+IpnZFEk9t/k5Wsb5b9zlbTqUYymrs+htJWgv/tJyXMNGbIUGnrS1 hjWRjyH0Q6luxRq9nyuY3IwVUySvqFRkYVLE+9Nu+jtxVkGC0KldK7lxEnxt33cPcVyy yD3g== X-Gm-Message-State: AHQUAubhMrdkY3RH4zbGTSD0EIXoiYI6eluve55nh5PsPvC6k0vV/Ozz LC1uX5bxBwwgHEMHEatT7gFDuUUiLA== X-Google-Smtp-Source: AHgI3IY7RunNSZ9rOWcCE+lAgLwHofmaaCoGFNrkD3aQiq0znoi/k347ZpRSTXa/2S5hJyoFlOXN8A== X-Received: by 2002:aca:eb01:: with SMTP id j1mr5717665oih.165.1550671214589; Wed, 20 Feb 2019 06:00:14 -0800 (PST) Received: from serve.minyard.net ([47.184.128.64]) by smtp.gmail.com with ESMTPSA id b4sm8936820oti.71.2019.02.20.06.00.10 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 20 Feb 2019 06:00:12 -0800 (PST) Received: from t430.minyard.net (t430m.minyard.net [192.168.27.3]) by serve.minyard.net (Postfix) with ESMTPA id 21B121808F3; Wed, 20 Feb 2019 14:00:09 +0000 (UTC) Received: by t430.minyard.net (Postfix, from userid 1000) id A15A5302A6E; Wed, 20 Feb 2019 08:00:08 -0600 (CST) From: minyard@acm.org To: qemu-devel@nongnu.org Date: Wed, 20 Feb 2019 07:59:43 -0600 Message-Id: <20190220135956.22589-7-minyard@acm.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190220135956.22589-1-minyard@acm.org> References: <20190220135956.22589-1-minyard@acm.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::344 Subject: [Qemu-devel] [PATCH 06/19] i2c:smbus: Simplify write operation 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: Peter Maydell , Corey Minyard , Corey Minyard , "Michael S . Tsirkin" , "Dr . David Alan Gilbert" , Paolo Bonzini Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Corey Minyard There were two different write functions and the SMBus code kept track of the command. Keeping track of the command wasn't useful, in fact it wasn't quite correct for the eeprom_smbus code. And there is no need for two write functions. Just have one write function and the first byte in the buffer is the command. Signed-off-by: Corey Minyard --- hw/i2c/smbus_eeprom.c | 47 ++++++++++++------------------------ hw/i2c/smbus_slave.c | 25 ++++--------------- include/hw/i2c/smbus_slave.h | 21 ++++++++++------ 3 files changed, 34 insertions(+), 59 deletions(-) diff --git a/hw/i2c/smbus_eeprom.c b/hw/i2c/smbus_eeprom.c index d82423aa7e..3f9ed266f8 100644 --- a/hw/i2c/smbus_eeprom.c +++ b/hw/i2c/smbus_eeprom.c @@ -43,16 +43,6 @@ static void eeprom_quick_cmd(SMBusDevice *dev, uint8_t read) #endif } -static void eeprom_send_byte(SMBusDevice *dev, uint8_t val) -{ - SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; -#ifdef DEBUG - printf("eeprom_send_byte: addr=0x%02x val=0x%02x\n", - dev->i2c.address, val); -#endif - eeprom->offset = val; -} - static uint8_t eeprom_receive_byte(SMBusDevice *dev) { SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; @@ -65,34 +55,30 @@ static uint8_t eeprom_receive_byte(SMBusDevice *dev) return val; } -static void eeprom_write_data(SMBusDevice *dev, uint8_t cmd, uint8_t *buf, int len) +static int eeprom_write_data(SMBusDevice *dev, uint8_t *buf, uint8_t len) { SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; - int n; + uint8_t *data = eeprom->data; + #ifdef DEBUG printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n", - dev->i2c.address, cmd, buf[0]); + dev->i2c.address, buf[0], buf[1]); #endif - /* A page write operation is not a valid SMBus command. - It is a block write without a length byte. Fortunately we - get the full block anyway. */ - /* TODO: Should this set the current location? */ - if (cmd + len > 256) - n = 256 - cmd; - else - n = len; - memcpy(eeprom->data + cmd, buf, n); - len -= n; - if (len) - memcpy(eeprom->data, buf + n, len); + /* len is guaranteed to be > 0 */ + eeprom->offset = buf[0]; + buf++; + len--; + + for (; len > 0; len--) { + data[eeprom->offset] = *buf++; + eeprom->offset = (eeprom->offset + 1) % 256; + } + + return 0; } -static uint8_t eeprom_read_data(SMBusDevice *dev, uint8_t cmd, int n) +static uint8_t eeprom_read_data(SMBusDevice *dev, int n) { - SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; - /* If this is the first byte then set the current position. */ - if (n == 0) - eeprom->offset = cmd; /* As with writes, we implement block reads without the SMBus length byte. */ return eeprom_receive_byte(dev); @@ -117,7 +103,6 @@ static void smbus_eeprom_class_initfn(ObjectClass *klass, void *data) dc->realize = smbus_eeprom_realize; sc->quick_cmd = eeprom_quick_cmd; - sc->send_byte = eeprom_send_byte; sc->receive_byte = eeprom_receive_byte; sc->write_data = eeprom_write_data; sc->read_data = eeprom_read_data; diff --git a/hw/i2c/smbus_slave.c b/hw/i2c/smbus_slave.c index 6a89a286e3..92c7a5086c 100644 --- a/hw/i2c/smbus_slave.c +++ b/hw/i2c/smbus_slave.c @@ -54,18 +54,9 @@ static void smbus_do_write(SMBusDevice *dev) { SMBusDeviceClass *sc = SMBUS_DEVICE_GET_CLASS(dev); - if (dev->data_len == 1) { - DPRINTF("Send Byte\n"); - if (sc->send_byte) { - sc->send_byte(dev, dev->data_buf[0]); - } - } else { - dev->command = dev->data_buf[0]; - DPRINTF("Command %d len %d\n", dev->command, dev->data_len - 1); - if (sc->write_data) { - sc->write_data(dev, dev->command, dev->data_buf + 1, - dev->data_len - 1); - } + DPRINTF("Command %d len %d\n", dev->data_buf[0], dev->data_len); + if (sc->write_data) { + sc->write_data(dev, dev->data_buf, dev->data_len); } } @@ -98,13 +89,7 @@ static int smbus_i2c_event(I2CSlave *s, enum i2c_event event) BADF("Read after write with no data\n"); dev->mode = SMBUS_CONFUSED; } else { - if (dev->data_len > 1) { - smbus_do_write(dev); - } else { - dev->command = dev->data_buf[0]; - DPRINTF("%02x: Command %d\n", dev->i2c.address, - dev->command); - } + smbus_do_write(dev); DPRINTF("Read mode\n"); dev->data_len = 0; dev->mode = SMBUS_READ_DATA; @@ -177,7 +162,7 @@ static uint8_t smbus_i2c_recv(I2CSlave *s) break; case SMBUS_READ_DATA: if (sc->read_data) { - ret = sc->read_data(dev, dev->command, dev->data_len); + ret = sc->read_data(dev, dev->data_len); dev->data_len++; } else { ret = 0; diff --git a/include/hw/i2c/smbus_slave.h b/include/hw/i2c/smbus_slave.h index 5ef1c72ad0..fa92201ec6 100644 --- a/include/hw/i2c/smbus_slave.h +++ b/include/hw/i2c/smbus_slave.h @@ -46,18 +46,24 @@ typedef struct SMBusDeviceClass * This may be NULL, quick commands are ignore in that case. */ void (*quick_cmd)(SMBusDevice *dev, uint8_t read); - void (*send_byte)(SMBusDevice *dev, uint8_t val); + uint8_t (*receive_byte)(SMBusDevice *dev); - /* We can't distinguish between a word write and a block write with - length 1, so pass the whole data block including the length byte - (if present). The device is responsible figuring out what type of - command this is. */ - void (*write_data)(SMBusDevice *dev, uint8_t cmd, uint8_t *buf, int len); + + /* + * We can't distinguish between a word write and a block write with + * length 1, so pass the whole data block including the length byte + * (if present). The device is responsible figuring out what type of + * command this is. + * This may be NULL if no data is written to the device. Writes + * will be ignore in that case. + */ + int (*write_data)(SMBusDevice *dev, uint8_t *buf, uint8_t len); + /* Likewise we can't distinguish between different reads, or even know the length of the read until the read is complete, so read data a byte at a time. The device is responsible for adding the length byte on block reads. */ - uint8_t (*read_data)(SMBusDevice *dev, uint8_t cmd, int n); + uint8_t (*read_data)(SMBusDevice *dev, int n); } SMBusDeviceClass; struct SMBusDevice { @@ -68,7 +74,6 @@ struct SMBusDevice { int mode; int data_len; uint8_t data_buf[34]; /* command + len + 32 bytes of data. */ - uint8_t command; }; #endif