Support detaching of USB-HID devices
PATCH 1/1 Support detaching of USB-HID devices
The attached patch allows one to detach USB-HID devices with usb_del. It adds the necessary logic to switch back and forth between PS/2 and USB-HID devices. I've tested with Windows and Linux guests (using the evtouch driver). See the changeset commit message for more details.
I wasn't confident on some of the changes (like where I hooked USB device detach) so feedback is greatly appreciated.
Regards,
Anthony Liguori
# HG changeset patch
# User Anthony Liguori <[email protected]> # Node ID 22c4f274525ecf9e084324263f2994a6f0889156 # Parent aeef66a23a99331481a589968d2ba5d6eda49593 Add support for detaching USB devices.
This patch adds the necessary infrastructure to USBDevice so that devices can be notified when they are removed. This patch also introduces stackable mouse event handlers so that the USB mice can remove their mouse event handlers allowing the PS/2 mouse to be restored.
Finally, this patch updates the SDL interface so that the transition from relative mode to absolute and vice versa is transparent to the user.
hw/usb-hid.c +18 -9
@@ -38,7 +38,7 @@int dx, dy, dz, buttons_state;int x, y;int kind; -int mouse_grabbed; +int mouse_id; } USBMouseState;/* mostly the same values as the Bochs USB Mouse device */
@@ -258,9 +258,8 @@ {int dx, dy, dz, b, l; -if (!s->mouse_grabbed) { -qemu_add_mouse_event_handler (usb_mouse_event, s,0 ); - s->mouse_grabbed =1 ; +if (!s->mouse_id) { + s->mouse_id =qemu_add_mouse_event_handler (usb_mouse_event, s,0 ); } dx =int_clamp (s->dx,-128 ,127 );
@@ -294,9 +293,8 @@ {int dz, b, l; -if (!s->mouse_grabbed) { -qemu_add_mouse_event_handler (usb_tablet_event, s,1 ); - s->mouse_grabbed =1 ; +if (!s->mouse_id) { + s->mouse_id =qemu_add_mouse_event_handler (usb_tablet_event, s,1 ); } dz =int_clamp (s->dz,-128 ,127 );
@@ -500,6 +498,15 @@return ret; } +static void usb_mouse_detach (USBDevice *dev) +{ +USBMouseState *s = (USBMouseState *)dev; + +if (s->mouse_id) { +qemu_del_mouse_event_handler (s->mouse_id); + } +} +USBDevice *usb_tablet_init (void ) {USBMouseState *s;
@@ -509,11 +516,12 @@return NULL ; s->dev.speed =USB_SPEED_FULL ; s->dev.handle_packet = usb_generic_handle_packet; - + s->dev.handle_detach = usb_mouse_detach; s->dev.handle_reset = usb_mouse_handle_reset; s->dev.handle_control = usb_mouse_handle_control; s->dev.handle_data = usb_mouse_handle_data; s->kind =USB_TABLET ; + s->mouse_id =0 ;return (USBDevice *)s; }
@@ -527,11 +535,12 @@return NULL ; s->dev.speed =USB_SPEED_FULL ; s->dev.handle_packet = usb_generic_handle_packet; - + s->dev.handle_detach = usb_mouse_detach; s->dev.handle_reset = usb_mouse_handle_reset; s->dev.handle_control = usb_mouse_handle_control; s->dev.handle_data = usb_mouse_handle_data; s->kind =USB_MOUSE ; + s->mouse_id =0 ;return (USBDevice *)s; }
hw/usb.c +4 -0
@@ -354,6 +354,10 @@ }else { dev = port->port.dev;if (dev) { +if (dev->handle_detach) { + dev->handle_detach (dev); + } + port->wPortStatus &= ~PORT_STAT_CONNECTION ; port->wPortChange |=PORT_STAT_C_CONNECTION ;if (port->wPortStatus &PORT_STAT_ENABLE ) {
hw/usb.h +1 -0
@@ -126,6 +126,7 @@ int index, int length, uint8_t *data);int (*handle_data)(USBDevice *dev,int pid,uint8_t devep,uint8_t *data,int len); +void (*handle_detach)(USBDevice *dev);uint8_t addr;int state;
sdl.c +29 -15
80 lines not on this page · raw message
vl.c +91 -16
125 lines not on this page · raw message
vl.h +5 -2
@@ -156,13 +156,16 @@typedef void QEMUPutKBDEvent (void *opaque,int keycode);typedef void QEMUPutMouseEvent (void *opaque,int dx,int dy,int dz,int buttons_state); +typedef void QEMUMouseChangeEvent (void *opaque,int absolute);void qemu_add_kbd_event_handler (QEMUPutKBDEvent *func,void *opaque); -void qemu_add_mouse_event_handler (QEMUPutMouseEvent *func,void *opaque,int absolute); + +int qemu_add_mouse_event_handler (QEMUPutMouseEvent *func,void *opaque,int absolute); +void qemu_del_mouse_event_handler (int handle); +void qemu_add_mouse_change_handler (QEMUMouseChangeEvent *func,void *opaque);void kbd_put_keycode (int keycode);void kbd_mouse_event (int dx,int dy,int dz,int buttons_state); -int kbd_mouse_is_absolute (void );/* keysym is a unicode code except for special keys (see QEMU_KEY_xxx constants) */