hid: move idle+protocol from usb-hid to hid too.
authorGerd Hoffmann <kraxel@redhat.com>
Fri, 15 Jul 2011 13:52:33 +0000 (15:52 +0200)
committerGerd Hoffmann <kraxel@redhat.com>
Thu, 4 Aug 2011 13:51:51 +0000 (15:51 +0200)
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
hw/hid.c
hw/hid.h
hw/usb-hid.c

index 1893ae59f0763e3509209971cc700d0def782e93..7b5ef5fc92aa4bd3c14cc6061f6e368afb093e1d 100644 (file)
--- a/hw/hid.c
+++ b/hw/hid.c
@@ -24,6 +24,7 @@
  */
 #include "hw.h"
 #include "console.h"
+#include "qemu-timer.h"
 #include "hid.h"
 
 #define HID_USAGE_ERROR_ROLLOVER        0x01
@@ -73,6 +74,11 @@ bool hid_has_events(HIDState *hs)
     return hs->n > 0;
 }
 
+void hid_set_next_idle(HIDState *hs, int64_t curtime)
+{
+    hs->next_idle_clock = curtime + (get_ticks_per_sec() * hs->idle * 4) / 1000;
+}
+
 static void hid_pointer_event_clear(HIDPointerEvent *e, int buttons)
 {
     e->xdx = e->ydy = e->dz = 0;
@@ -365,6 +371,8 @@ void hid_reset(HIDState *hs)
     }
     hs->head = 0;
     hs->n = 0;
+    hs->protocol = 1;
+    hs->idle = 0;
 }
 
 void hid_free(HIDState *hs)
index 99910c3a86ace3a7c92fa7ee104961512400f921..4a8fa5b63f536af9793b8fbca89a6223e4de79b6 100644 (file)
--- a/hw/hid.h
+++ b/hw/hid.h
@@ -39,6 +39,9 @@ struct HIDState {
     uint32_t head; /* index into circular queue */
     uint32_t n;
     int kind;
+    int32_t protocol;
+    uint8_t idle;
+    int64_t next_idle_clock;
     HIDEventFunc event;
 };
 
@@ -47,6 +50,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);
 int hid_pointer_poll(HIDState *hs, uint8_t *buf, int len);
 int hid_keyboard_poll(HIDState *hs, uint8_t *buf, int len);
 int hid_keyboard_write(HIDState *hs, uint8_t *buf, int len);
index 48ce7439888f5921c72057d9e6e537bce2513946..e5d57de8889f6c5a44e743d6769eea6cb0d992b1 100644 (file)
@@ -45,9 +45,6 @@
 typedef struct USBHIDState {
     USBDevice dev;
     HIDState hid;
-    int32_t protocol;
-    uint8_t idle;
-    int64_t next_idle_clock;
     void *datain_opaque;
     void (*datain)(void *);
 } USBHIDState;
@@ -377,13 +374,6 @@ static void usb_hid_handle_reset(USBDevice *dev)
     USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
 
     hid_reset(&us->hid);
-    us->protocol = 1;
-    us->idle = 0;
-}
-
-static void usb_hid_set_next_idle(USBHIDState *s, int64_t curtime)
-{
-    s->next_idle_clock = curtime + (get_ticks_per_sec() * s->idle * 4) / 1000;
 }
 
 static int usb_hid_handle_control(USBDevice *dev, USBPacket *p,
@@ -448,22 +438,22 @@ static int usb_hid_handle_control(USBDevice *dev, USBPacket *p,
             goto fail;
         }
         ret = 1;
-        data[0] = us->protocol;
+        data[0] = hs->protocol;
         break;
     case SET_PROTOCOL:
         if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
             goto fail;
         }
         ret = 0;
-        us->protocol = value;
+        hs->protocol = value;
         break;
     case GET_IDLE:
         ret = 1;
-        data[0] = us->idle;
+        data[0] = hs->idle;
         break;
     case SET_IDLE:
-        us->idle = (uint8_t) (value >> 8);
-        usb_hid_set_next_idle(us, qemu_get_clock_ns(vm_clock));
+        hs->idle = (uint8_t) (value >> 8);
+        hid_set_next_idle(hs, qemu_get_clock_ns(vm_clock));
         ret = 0;
         break;
     default:
@@ -486,10 +476,10 @@ static int usb_hid_handle_data(USBDevice *dev, USBPacket *p)
         if (p->devep == 1) {
             int64_t curtime = qemu_get_clock_ns(vm_clock);
             if (!hid_has_events(hs) &&
-                (!us->idle || us->next_idle_clock - curtime > 0)) {
+                (!hs->idle || hs->next_idle_clock - curtime > 0)) {
                 return USB_RET_NAK;
             }
-            usb_hid_set_next_idle(us, curtime);
+            hid_set_next_idle(hs, curtime);
             if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
                 ret = hid_pointer_poll(hs, buf, p->iov.size);
             } else if (hs->kind == HID_KEYBOARD) {
@@ -552,8 +542,8 @@ static int usb_hid_post_load(void *opaque, int version_id)
 {
     USBHIDState *s = opaque;
 
-    if (s->idle) {
-        usb_hid_set_next_idle(s, qemu_get_clock_ns(vm_clock));
+    if (s->hid.idle) {
+        hid_set_next_idle(&s->hid, qemu_get_clock_ns(vm_clock));
     }
     return 0;
 }
@@ -581,8 +571,8 @@ static const VMStateDescription vmstate_usb_ptr = {
                              vmstate_usb_ptr_queue, HIDPointerEvent),
         VMSTATE_UINT32(hid.head, USBHIDState),
         VMSTATE_UINT32(hid.n, USBHIDState),
-        VMSTATE_INT32(protocol, USBHIDState),
-        VMSTATE_UINT8(idle, USBHIDState),
+        VMSTATE_INT32(hid.protocol, USBHIDState),
+        VMSTATE_UINT8(hid.idle, USBHIDState),
         VMSTATE_END_OF_LIST()
     }
 };
@@ -601,8 +591,8 @@ static const VMStateDescription vmstate_usb_kbd = {
         VMSTATE_UINT8(hid.kbd.leds, USBHIDState),
         VMSTATE_UINT8_ARRAY(hid.kbd.key, USBHIDState, 16),
         VMSTATE_INT32(hid.kbd.keys, USBHIDState),
-        VMSTATE_INT32(protocol, USBHIDState),
-        VMSTATE_UINT8(idle, USBHIDState),
+        VMSTATE_INT32(hid.protocol, USBHIDState),
+        VMSTATE_UINT8(hid.idle, USBHIDState),
         VMSTATE_END_OF_LIST()
     }
 };