From patchwork Mon Nov 9 03:36:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1396496 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=T7silO9Q; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CTxY95S1Hz9sRK for ; Mon, 9 Nov 2020 14:38:37 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 762D98256C; Mon, 9 Nov 2020 04:37:12 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="T7silO9Q"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3F3C782563; Mon, 9 Nov 2020 04:37:11 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-io1-xd43.google.com (mail-io1-xd43.google.com [IPv6:2607:f8b0:4864:20::d43]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 4B00582544 for ; Mon, 9 Nov 2020 04:37:01 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-io1-xd43.google.com with SMTP id o11so8371112ioo.11 for ; Sun, 08 Nov 2020 19:37:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dA6KUp8E36D/foQdJkX1zxvfMjainGFUR3WRG0jDOFY=; b=T7silO9Qhb1UJHFWUjenb3QAdzcPb24WEJ3oVfMQ0j/9KIIrXLM0x4FYkgIsRA1B35 VaNEn15b+TkTRJnJoEjNi0Mo3ITPgLRfDZZZpKF7IHNauDlTSTEH42okTB8W3UeWdfmx b8RZtDH04yAUSOKkrTktvxKsOhXEeJQupubWc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=dA6KUp8E36D/foQdJkX1zxvfMjainGFUR3WRG0jDOFY=; b=gh7hmbdxbEfObNC/dLsdxbZSfKk0rf08cyLECPprWk0LaFsT6/7pEKnbGjcg3Xg9Vs rr80X6Ff3j4DSqbL/2XbrK7a32IzzQbjaoHCUpdJU/r12dyMudnBAnzz4G8RnE5cDPmq e0BiarVTF87pU2tEUmrzI/VkPM+EM33GYJH2G1Df5V+qDfrFOjK7x90erDtL4fAF3jw2 59wulKdY0ffnByMDUdrioLgFFXbflu3JNtfdAPAtEYWSeOZ4LpxMzA7smUD+5V/5XAHC s3xLfc+ULVFDga/PiGUj3v7LK8QXCgmjD1SblcrsTwZJTbzRSOlezN1U+IauXiDd83mM 8kvw== X-Gm-Message-State: AOAM532QEeUAhfsePqV1N5/sGicEyibWfRv1jAgt9fQqVNIh1aSrkE5y 3DwQvn0AlxvEvjDS9QW+YGNaRDNNU8/pFw== X-Google-Smtp-Source: ABdhPJxdpeO1ZtJxPs66Si2n+oel6UJjm9ffU2SWHgw+VGEdiPApsp8s4wgGoEOtwPQ/gk/OFgRZUw== X-Received: by 2002:a05:6602:5de:: with SMTP id w30mr4974222iox.64.1604893014996; Sun, 08 Nov 2020 19:36:54 -0800 (PST) Received: from localhost.localdomain (c-73-14-175-90.hsd1.co.comcast.net. [73.14.175.90]) by smtp.gmail.com with ESMTPSA id r3sm5098792iot.21.2020.11.08.19.36.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Nov 2020 19:36:54 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Simon Glass , Andy Shevchenko , Bin Meng , Heinrich Schuchardt , Walter Lozano Subject: [PATCH 3/3] sandbox: serial: Update to use membuff Date: Sun, 8 Nov 2020 20:36:50 -0700 Message-Id: <20201108203644.3.I124d1e372667a17aa3f63684be7b8fd0cb03c6e8@changeid> X-Mailer: git-send-email 2.29.2.222.g5d2a92d10f8-goog In-Reply-To: <20201109033650.1860135-1-sjg@chromium.org> References: <20201109033650.1860135-1-sjg@chromium.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.3 at phobos.denx.de X-Virus-Status: Clean Rather than implementing our own circular queue, use membuff. This allows us to read multiple bytes at once into the serial input. Signed-off-by: Simon Glass --- drivers/serial/sandbox.c | 48 ++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/drivers/serial/sandbox.c b/drivers/serial/sandbox.c index 8c74c3a1b8c..8e269a205d7 100644 --- a/drivers/serial/sandbox.c +++ b/drivers/serial/sandbox.c @@ -21,24 +21,18 @@ DECLARE_GLOBAL_DATA_PTR; -/* - * - * serial_buf: A buffer that holds keyboard characters for the - * Sandbox U-Boot. - * - * invariants: - * serial_buf_write == serial_buf_read -> empty buffer - * (serial_buf_write + 1) % 16 == serial_buf_read -> full buffer - */ -static unsigned char serial_buf[16]; -static unsigned int serial_buf_write; -static unsigned int serial_buf_read; - struct sandbox_serial_platdata { int colour; /* Text colour to use for output, -1 for none */ }; +/** + * struct sandbox_serial_priv - Private data for this driver + * + * @buf: holds input characters available to be read by this driver + */ struct sandbox_serial_priv { + struct membuff buf; + char serial_buf[16]; bool start_of_line; }; @@ -71,6 +65,7 @@ static int sandbox_serial_probe(struct udevice *dev) if (state->term_raw != STATE_TERM_RAW) disable_ctrlc(1); + membuff_init(&priv->buf, priv->serial_buf, sizeof(priv->serial_buf)); return 0; } @@ -104,16 +99,12 @@ static int sandbox_serial_putc(struct udevice *dev, const char ch) return 0; } -static unsigned int increment_buffer_index(unsigned int index) -{ - return (index + 1) % ARRAY_SIZE(serial_buf); -} - static int sandbox_serial_pending(struct udevice *dev, bool input) { - const unsigned int next_index = - increment_buffer_index(serial_buf_write); + struct sandbox_serial_priv *priv = dev_get_priv(dev); ssize_t count; + char *data; + int avail; if (!input) return 0; @@ -121,26 +112,25 @@ static int sandbox_serial_pending(struct udevice *dev, bool input) os_usleep(100); if (!IS_ENABLED(CONFIG_SPL_BUILD)) video_sync_all(); - if (next_index == serial_buf_read) + avail = membuff_putraw(&priv->buf, 100, false, &data); + if (!avail) return 1; /* buffer full */ - count = os_read(0, &serial_buf[serial_buf_write], 1); - if (count == 1) - serial_buf_write = next_index; + count = os_read(0, data, avail); + if (count > 0) + membuff_putraw(&priv->buf, count, true, &data); - return serial_buf_write != serial_buf_read; + return membuff_avail(&priv->buf); } static int sandbox_serial_getc(struct udevice *dev) { - int result; + struct sandbox_serial_priv *priv = dev_get_priv(dev); if (!sandbox_serial_pending(dev, true)) return -EAGAIN; /* buffer empty */ - result = serial_buf[serial_buf_read]; - serial_buf_read = increment_buffer_index(serial_buf_read); - return result; + return membuff_getbyte(&priv->buf); } #ifdef CONFIG_DEBUG_UART_SANDBOX