From patchwork Wed Dec 26 18:56:56 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 208185 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 1C7232C00BF for ; Thu, 27 Dec 2012 05:58:11 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 5B7404A087; Wed, 26 Dec 2012 19:58:07 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id nYG4l2NW7A3L; Wed, 26 Dec 2012 19:58:07 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 0F6524A094; Wed, 26 Dec 2012 19:57:51 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 1D4344A058 for ; Wed, 26 Dec 2012 19:57:41 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ijVOkwFE+-je for ; Wed, 26 Dec 2012 19:57:39 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail-vc0-f201.google.com (mail-vc0-f201.google.com [209.85.220.201]) by theia.denx.de (Postfix) with ESMTPS id 318DD4A08A for ; Wed, 26 Dec 2012 19:57:32 +0100 (CET) Received: by mail-vc0-f201.google.com with SMTP id p1so927292vcq.0 for ; Wed, 26 Dec 2012 10:57:31 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references:x-gm-message-state; bh=KiIO3rtizfQ72+D6YNSUh0xCcKdGFF43UEmLFGkzTVk=; b=RM9SCc/BVhFLKbpUFBUqQ3opH/XbJSkQGCp83MhnMHrIrDSPagc/BcRIK5AlW0MkgW f5f4/rPGO4puVePmpQR7MwIf0aFTKDxCkd6dNP2hbpdCG/Wd0TbnUalDCC7iYwgjQesa fVJ0X1jhIpjoahZ5E5JQTP9Jh+pwdnRO+RnQRXRo6wtczso3M3bBDG7a6SvMiUBBmyJp FmfP9qXFoo7/aLrz133t7f2ze0afka4Sot2wT6XpUQYuDA81ZfIS+MUx/tBRQSSGFdhP CXqNw9PU6dH1g/jMZdJy/wzMiP655G9dtaV0oJvqErZQc/Nq4v5kfoQ0r/CX+P9n9Cyd 4hMg== X-Received: by 10.101.130.39 with SMTP id h39mr3388334ann.17.1356548250968; Wed, 26 Dec 2012 10:57:30 -0800 (PST) Received: from wpzn3.hot.corp.google.com (216-239-44-65.google.com [216.239.44.65]) by gmr-mx.google.com with ESMTPS id s78si2351904yhh.3.2012.12.26.10.57.30 (version=TLSv1/SSLv3 cipher=AES128-SHA); Wed, 26 Dec 2012 10:57:30 -0800 (PST) Received: from kaka.mtv.corp.google.com (kaka.mtv.corp.google.com [172.22.73.79]) by wpzn3.hot.corp.google.com (Postfix) with ESMTP id A09DA100047; Wed, 26 Dec 2012 10:57:30 -0800 (PST) Received: by kaka.mtv.corp.google.com (Postfix, from userid 121222) id 5A03D160D91; Wed, 26 Dec 2012 10:57:30 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Date: Wed, 26 Dec 2012 10:56:56 -0800 Message-Id: <1356548233-5570-4-git-send-email-sjg@chromium.org> X-Mailer: git-send-email 1.7.7.3 In-Reply-To: <1356548233-5570-1-git-send-email-sjg@chromium.org> References: <1356548233-5570-1-git-send-email-sjg@chromium.org> X-Gm-Message-State: ALoCoQlSzIvZtw/D+n9okP9n3Hb+e5hOF3gWw4B2+DPina6r57TZwnGODoB8bYzsrbnXpP6wu01jgAHwjZey6oRMTqtXcCTlLL4r9Cf/LB9KhRNnoOe5DpmQXVj7yLq/eCVyOyX6wMJh/iMuA4FiymO15WzTrSK2NrLcKwLxMY+LBK2zawY+h9chB17KKOMpUH6w4HzKGZzy Cc: Tom Rini , Joe Hershberger , Kumar Gala , Taylor Hutt Subject: [U-Boot] [PATCH 03/20] sandbox: Improve sandbox serial port keyboard interface X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de From: Taylor Hutt Implements the tstc() interface for the serial driver. Multiplexing the console between the serial port and a keyboard uses a polling method of checking if characters are available; this means that the serial console must be non-blocking when attempting to read characters. Signed-off-by: Taylor Hutt Signed-off-by: Simon Glass --- arch/sandbox/cpu/os.c | 8 ++++++++ drivers/serial/sandbox.c | 44 ++++++++++++++++++++++++++++++++++++++------ include/os.h | 10 ++++++++++ 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c index 36637af..3e37c93 100644 --- a/arch/sandbox/cpu/os.c +++ b/arch/sandbox/cpu/os.c @@ -44,6 +44,14 @@ ssize_t os_read(int fd, void *buf, size_t count) return read(fd, buf, count); } +ssize_t os_read_no_block(int fd, void *buf, size_t count) +{ + const int flags = fcntl(fd, F_GETFL, 0); + + fcntl(fd, F_SETFL, flags | O_NONBLOCK); + return os_read(fd, buf, count); +} + ssize_t os_write(int fd, const void *buf, size_t count) { return write(fd, buf, count); diff --git a/drivers/serial/sandbox.c b/drivers/serial/sandbox.c index cb19401..b73520c 100644 --- a/drivers/serial/sandbox.c +++ b/drivers/serial/sandbox.c @@ -30,6 +30,19 @@ #include #include +/* + * + * 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 char serial_buf[16]; +static unsigned int serial_buf_write; +static unsigned int serial_buf_read; + static int sandbox_serial_init(void) { os_tty_raw(0); @@ -50,18 +63,37 @@ static void sandbox_serial_puts(const char *str) os_write(1, str, strlen(str)); } -static int sandbox_serial_getc(void) +static unsigned int increment_buffer_index(unsigned int index) +{ + return (index + 1) % ARRAY_SIZE(serial_buf); +} + +static int sandbox_serial_tstc(void) { - char buf; + const unsigned int next_index = + increment_buffer_index(serial_buf_write); ssize_t count; - count = os_read(0, &buf, 1); - return count == 1 ? buf : 0; + os_usleep(100); + if (next_index == serial_buf_read) + return 1; /* buffer full */ + + count = os_read_no_block(0, &serial_buf[serial_buf_write], 1); + if (count == 1) + serial_buf_write = next_index; + return serial_buf_write != serial_buf_read; } -static int sandbox_serial_tstc(void) +static int sandbox_serial_getc(void) { - return 0; + int result; + + while (!sandbox_serial_tstc()) + ; /* buffer empty */ + + result = serial_buf[serial_buf_read]; + serial_buf_read = increment_buffer_index(serial_buf_read); + return result; } static struct serial_device sandbox_serial_drv = { diff --git a/include/os.h b/include/os.h index 699682a..c452d1b 100644 --- a/include/os.h +++ b/include/os.h @@ -40,6 +40,16 @@ struct sandbox_state; ssize_t os_read(int fd, void *buf, size_t count); /** + * Access to the OS read() system call with non-blocking access + * + * \param fd File descriptor as returned by os_open() + * \param buf Buffer to place data + * \param count Number of bytes to read + * \return number of bytes read, or -1 on error + */ +ssize_t os_read_no_block(int fd, void *buf, size_t count); + +/** * Access to the OS write() system call * * \param fd File descriptor as returned by os_open()