From patchwork Tue Jul 12 08:21:37 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 647225 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rpZjC6jRTz9sBX for ; Tue, 12 Jul 2016 18:23:11 +1000 (AEST) Received: from localhost ([::1]:38354 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bMsyD-0008Jx-LZ for incoming@patchwork.ozlabs.org; Tue, 12 Jul 2016 04:23:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40291) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bMsx1-0007RT-7T for qemu-devel@nongnu.org; Tue, 12 Jul 2016 04:21:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bMswy-00049L-AY for qemu-devel@nongnu.org; Tue, 12 Jul 2016 04:21:54 -0400 Received: from mx1.redhat.com ([209.132.183.28]:60369) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bMswy-00049E-2s for qemu-devel@nongnu.org; Tue, 12 Jul 2016 04:21:52 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (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 B80E67F093 for ; Tue, 12 Jul 2016 08:21:51 +0000 (UTC) Received: from nilsson.home.kraxel.org (ovpn-116-18.ams2.redhat.com [10.36.116.18]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u6C8Ll9R001192; Tue, 12 Jul 2016 04:21:50 -0400 Received: by nilsson.home.kraxel.org (Postfix, from userid 500) id 7875380F71; Tue, 12 Jul 2016 10:21:45 +0200 (CEST) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Tue, 12 Jul 2016 10:21:37 +0200 Message-Id: <1468311703-27209-3-git-send-email-kraxel@redhat.com> In-Reply-To: <1468311703-27209-1-git-send-email-kraxel@redhat.com> References: <1468311703-27209-1-git-send-email-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 12 Jul 2016 08:21:51 +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] [PULL 2/8] 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 Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 Acked-by: Paolo Bonzini Message-id: 1467625375-31774-3-git-send-email-kraxel@redhat.com --- 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);