qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Hans de Goede <hdegoede@redhat.com>
To: Gerd Hoffmann <kraxel@redhat.com>
Cc: Hans de Goede <hdegoede@redhat.com>, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 17/26] hid: Change idle handling to use a timer
Date: Fri, 14 Dec 2012 14:35:38 +0100	[thread overview]
Message-ID: <1355492147-5023-18-git-send-email-hdegoede@redhat.com> (raw)
In-Reply-To: <1355492147-5023-1-git-send-email-hdegoede@redhat.com>

This leads to cleaner code in usb-hid, and removes up to a 1000 calls / sec to
qemu_get_clock_ns(vm_clock) if idle-time is set to its default value of 0.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 hw/hid.c         | 43 +++++++++++++++++++++++++++++++++++++------
 hw/hid.h         |  5 +++--
 hw/usb/dev-hid.c |  8 +++-----
 3 files changed, 43 insertions(+), 13 deletions(-)

diff --git a/hw/hid.c b/hw/hid.c
index 03761ab..f8c5393 100644
--- a/hw/hid.c
+++ b/hw/hid.c
@@ -71,12 +71,38 @@ static const uint8_t hid_usage_keys[0x100] = {
 
 bool hid_has_events(HIDState *hs)
 {
-    return hs->n > 0;
+    return hs->n > 0 || hs->idle_pending;
 }
 
-void hid_set_next_idle(HIDState *hs, int64_t curtime)
+static void hid_idle_timer(void *opaque)
 {
-    hs->next_idle_clock = curtime + (get_ticks_per_sec() * hs->idle * 4) / 1000;
+    HIDState *hs = opaque;
+
+    hs->idle_pending = true;
+    hs->event(hs);
+}
+
+static void hid_del_idle_timer(HIDState *hs)
+{
+    if (hs->idle_timer) {
+        qemu_del_timer(hs->idle_timer);
+        qemu_free_timer(hs->idle_timer);
+        hs->idle_timer = NULL;
+    }
+}
+
+void hid_set_next_idle(HIDState *hs)
+{
+    if (hs->idle) {
+        uint64_t expire_time = qemu_get_clock_ns(vm_clock) +
+                               get_ticks_per_sec() * hs->idle * 4 / 1000;
+        if (!hs->idle_timer) {
+            hs->idle_timer = qemu_new_timer_ns(vm_clock, hid_idle_timer, hs);
+        }
+        qemu_mod_timer_ns(hs->idle_timer, expire_time);
+    } else {
+        hid_del_idle_timer(hs);
+    }
 }
 
 static void hid_pointer_event_clear(HIDPointerEvent *e, int buttons)
@@ -232,6 +258,8 @@ int hid_pointer_poll(HIDState *hs, uint8_t *buf, int len)
     int index;
     HIDPointerEvent *e;
 
+    hs->idle_pending = false;
+
     hid_pointer_activate(hs);
 
     /* When the buffer is empty, return the last event.  Relative
@@ -319,6 +347,8 @@ int hid_pointer_poll(HIDState *hs, uint8_t *buf, int len)
 
 int hid_keyboard_poll(HIDState *hs, uint8_t *buf, int len)
 {
+    hs->idle_pending = false;
+
     if (len < 2) {
         return 0;
     }
@@ -377,6 +407,8 @@ void hid_reset(HIDState *hs)
     hs->n = 0;
     hs->protocol = 1;
     hs->idle = 0;
+    hs->idle_pending = false;
+    hid_del_idle_timer(hs);
 }
 
 void hid_free(HIDState *hs)
@@ -390,6 +422,7 @@ void hid_free(HIDState *hs)
         qemu_remove_mouse_event_handler(hs->ptr.eh_entry);
         break;
     }
+    hid_del_idle_timer(hs);
 }
 
 void hid_init(HIDState *hs, int kind, HIDEventFunc event)
@@ -412,9 +445,7 @@ static int hid_post_load(void *opaque, int version_id)
 {
     HIDState *s = opaque;
 
-    if (s->idle) {
-        hid_set_next_idle(s, qemu_get_clock_ns(vm_clock));
-    }
+    hid_set_next_idle(s);
     return 0;
 }
 
diff --git a/hw/hid.h b/hw/hid.h
index 5315cf7..1cae633 100644
--- a/hw/hid.h
+++ b/hw/hid.h
@@ -43,7 +43,8 @@ struct HIDState {
     int kind;
     int32_t protocol;
     uint8_t idle;
-    int64_t next_idle_clock;
+    bool idle_pending;
+    QEMUTimer *idle_timer;
     HIDEventFunc event;
 };
 
@@ -52,7 +53,7 @@ void hid_reset(HIDState *hs);
 void hid_free(HIDState *hs);
 
 bool hid_has_events(HIDState *hs);
-void hid_set_next_idle(HIDState *hs, int64_t curtime);
+void hid_set_next_idle(HIDState *hs);
 void hid_pointer_activate(HIDState *hs);
 int hid_pointer_poll(HIDState *hs, uint8_t *buf, int len);
 int hid_keyboard_poll(HIDState *hs, uint8_t *buf, int len);
diff --git a/hw/usb/dev-hid.c b/hw/usb/dev-hid.c
index 8749128..c452a7e 100644
--- a/hw/usb/dev-hid.c
+++ b/hw/usb/dev-hid.c
@@ -501,7 +501,7 @@ static void usb_hid_handle_control(USBDevice *dev, USBPacket *p,
         break;
     case SET_IDLE:
         hs->idle = (uint8_t) (value >> 8);
-        hid_set_next_idle(hs, qemu_get_clock_ns(vm_clock));
+        hid_set_next_idle(hs);
         if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
             hid_pointer_activate(hs);
         }
@@ -523,16 +523,14 @@ static void usb_hid_handle_data(USBDevice *dev, USBPacket *p)
     switch (p->pid) {
     case USB_TOKEN_IN:
         if (p->ep->nr == 1) {
-            int64_t curtime = qemu_get_clock_ns(vm_clock);
             if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
                 hid_pointer_activate(hs);
             }
-            if (!hid_has_events(hs) &&
-                (!hs->idle || hs->next_idle_clock - curtime > 0)) {
+            if (!hid_has_events(hs)) {
                 p->status = USB_RET_NAK;
                 return;
             }
-            hid_set_next_idle(hs, curtime);
+            hid_set_next_idle(hs);
             if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
                 len = hid_pointer_poll(hs, buf, p->iov.size);
             } else if (hs->kind == HID_KEYBOARD) {
-- 
1.8.0.1

  parent reply	other threads:[~2012-12-14 13:33 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-14 13:35 [Qemu-devel] [PATCH 00/26] usb: Hans' pending qemu usb patches Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 01/26] ehci: Add a ehci_writeback_async_complete_packet helper function Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 02/26] ehci: Add ehci_verify_qh and ehci_verify_qtd helper functions Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 03/26] ehci: Verify guest does not change the token of inflight qtd-s Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 04/26] ehci: Move get / put_dwords upwards Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 05/26] ehci: writeback_async_complete_packet: verify qh and qtd Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 06/26] ehci: Verify qtd for async completed packets Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 07/26] ehci: Add an ehci_get_pid helper function Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 08/26] ehci: Verify a queue's ep direction does not change Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 09/26] ehci: Use uframe precision for interrupt threshold checking Hans de Goede
2012-12-17 13:16   ` Gerd Hoffmann
2012-12-17 14:23     ` Hans de Goede
2012-12-17 14:39       ` Gerd Hoffmann
2012-12-17 14:47         ` Hans de Goede
2012-12-17 14:51           ` Gerd Hoffmann
2012-12-18 10:20             ` Hans de Goede
2012-12-18 11:03               ` Gerd Hoffmann
2012-12-18 11:30                 ` Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 10/26] ehci: Further speedup rescanning if async schedule after raising an interrupt Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 11/26] ehci: Don't call commit_irq after raising PCD Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 12/26] uhci: Fix 1 ms delay in interrupt reporting to the guest Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 13/26] uhci: Fix pending interrupts getting lost on migration Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 14/26] uhci: Add a QH_VALID define Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 15/26] uhci: Limit amount of frames processed in one go Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 16/26] uhci: Maximize how many frames we catch up when behind Hans de Goede
2012-12-14 13:35 ` Hans de Goede [this message]
2012-12-14 13:35 ` [Qemu-devel] [PATCH 18/26] usb: Fix usb_ep_find_packet_by_id Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 19/26] usb: Add an usb_device_ep_stopped USBDevice method Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 20/26] usbredir: Add an usbredir_stop_ep helper function Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 21/26] usbredir: Add USBEP2I and I2USBEP helper macros Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 22/26] usbredir: Add ep_stopped USBDevice method Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 23/26] usbredir: Verify we have 32 bits bulk length cap when redirecting to xhci Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 24/26] usbredir: Add usbredir_init_endpoints() helper Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 25/26] usb-redir: Add debugging to bufpq save / restore Hans de Goede
2012-12-14 13:35 ` [Qemu-devel] [PATCH 26/26] usbredir: Add support for buffered bulk input Hans de Goede
2012-12-18 13:36   ` Gerd Hoffmann
2012-12-19  9:27     ` Hans de Goede
2012-12-19  9:51       ` Gerd Hoffmann
2012-12-19 11:48         ` Hans de Goede

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1355492147-5023-18-git-send-email-hdegoede@redhat.com \
    --to=hdegoede@redhat.com \
    --cc=kraxel@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).