From patchwork Tue Jun 14 12:51:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 9175777 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E911B60772 for ; Tue, 14 Jun 2016 13:01:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D8C2927DF9 for ; Tue, 14 Jun 2016 13:01:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CC068280E0; Tue, 14 Jun 2016 13:01:06 +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=-5.9 required=2.0 tests=BAYES_00,FSL_HELO_HOME, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 73FDE27DF9 for ; Tue, 14 Jun 2016 13:01:06 +0000 (UTC) Received: from localhost ([::1]:35087 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bCnxp-00087H-IX for patchwork-qemu-devel@patchwork.kernel.org; Tue, 14 Jun 2016 09:01:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44961) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bCnoo-0000hM-7G for qemu-devel@nongnu.org; Tue, 14 Jun 2016 08:51:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bCnoh-0003RF-49 for qemu-devel@nongnu.org; Tue, 14 Jun 2016 08:51:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:56390) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bCnog-0003Qu-SM for qemu-devel@nongnu.org; Tue, 14 Jun 2016 08:51:39 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C6BA03F0A48; Tue, 14 Jun 2016 12:51:37 +0000 (UTC) Received: from nilsson.home.kraxel.org (ovpn-116-18.ams2.redhat.com [10.36.116.18]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u5ECpaK1020905; Tue, 14 Jun 2016 08:51:37 -0400 Received: by nilsson.home.kraxel.org (Postfix, from userid 500) id 863128209A; Tue, 14 Jun 2016 14:51:36 +0200 (CEST) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Tue, 14 Jun 2016 14:51:25 +0200 Message-Id: <1465908686-14799-2-git-send-email-kraxel@redhat.com> In-Reply-To: <1465908686-14799-1-git-send-email-kraxel@redhat.com> References: <1465908686-14799-1-git-send-email-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 14 Jun 2016 12:51:37 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 2/2] msmouse: fix buffer handling 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: Paolo Bonzini , Gerd Hoffmann , Anatoly Gusev Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP The msmouse chardev backend writes data without checking whenever there is enough space. That happens to work with linux guests, probably by pure luck because the linux driver enables the fifo and the serial port emulation accepts more data than announced via qemu_chr_be_can_write() in that case. Handle this properly by adding a buffer to MouseState. Hook up a CharDriverState->accept_input() handler which feeds the buffer to the serial port. msmouse_event() only fills the buffer now, and calls the accept_input handler too to kick off the transmission. Signed-off-by: Gerd Hoffmann --- backends/msmouse.c | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/backends/msmouse.c b/backends/msmouse.c index 731784f..9ade31b 100644 --- a/backends/msmouse.c +++ b/backends/msmouse.c @@ -32,13 +32,35 @@ typedef struct { CharDriverState *chr; QEMUPutMouseEntry *entry; + uint8_t outbuf[32]; + int outlen; } MouseState; +static void msmouse_chr_accept_input(CharDriverState *chr) +{ + MouseState *mouse = chr->opaque; + int len; + + len = qemu_chr_be_can_write(chr); + if (len > mouse->outlen) { + len = mouse->outlen; + } + if (!len) { + return; + } + + qemu_chr_be_write(chr, mouse->outbuf, len); + mouse->outlen -= len; + if (mouse->outlen) { + memmove(mouse->outbuf, mouse->outbuf + len, mouse->outlen); + } +} + static void msmouse_event(void *opaque, int dx, int dy, int dz, int buttons_state) { CharDriverState *chr = (CharDriverState *)opaque; - + MouseState *mouse = chr->opaque; unsigned char bytes[4] = { 0x40, 0x00, 0x00, 0x00 }; /* Movement deltas */ @@ -51,10 +73,17 @@ static void msmouse_event(void *opaque, bytes[0] |= (buttons_state & 0x02 ? 0x10 : 0x00); bytes[3] |= (buttons_state & 0x04 ? 0x20 : 0x00); - /* We always send the packet of, so that we do not have to keep track - of previous state of the middle button. This can potentially confuse - some very old drivers for two button mice though. */ - qemu_chr_be_write(chr, bytes, 4); + if (mouse->outlen <= sizeof(mouse->outbuf) - 4) { + /* We always send the packet of, so that we do not have to keep track + of previous state of the middle button. This can potentially confuse + some very old drivers for two button mice though. */ + memcpy(mouse->outbuf + mouse->outlen, bytes, 4); + mouse->outlen += 4; + } else { + /* queue full -> drop event */ + } + + msmouse_chr_accept_input(chr); } static int msmouse_chr_write (struct CharDriverState *s, const uint8_t *buf, int len) @@ -84,6 +113,7 @@ static CharDriverState *qemu_chr_open_msmouse(const char *id, chr = qemu_chr_alloc(common, errp); chr->chr_write = msmouse_chr_write; chr->chr_close = msmouse_chr_close; + chr->chr_accept_input = msmouse_chr_accept_input; chr->explicit_be_open = true; mouse = g_new0(MouseState, 1);