usb: add usb_find_device()
authorGerd Hoffmann <kraxel@redhat.com>
Tue, 10 Jan 2012 15:59:28 +0000 (16:59 +0100)
committerGerd Hoffmann <kraxel@redhat.com>
Fri, 10 Feb 2012 10:31:48 +0000 (11:31 +0100)
Add usb_find_device().  This function will check whenever a device with
a specific address is connected to the specified port.  Usually this
will just check state and address of the device hooked up to the port,
but in case of a hub it will ask the hub to check all hub ports for a
matching device.

This patch doesn't put the code into use yet, see the following patches
for details.

The master plan is to separate device lookup and packet processing.
Right now the usb code simply walks all devices, calls
usb_handle_packet() on each until one accepts the packet (by returning
something different that USB_RET_NODEV).  I want to have a device lookup
first, then call usb_handle_packet() once, for the device which actually
processes the packet.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
hw/usb-bus.c
hw/usb.c
hw/usb.h

index b7538345842b48892b31193af403f55dbaef3e80..5c05ed58067721d6e159f1bd50806daac725e443 100644 (file)
@@ -74,6 +74,15 @@ static int usb_device_init(USBDevice *dev)
     return 0;
 }
 
+USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr)
+{
+    USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
+    if (klass->find_device) {
+        return klass->find_device(dev, addr);
+    }
+    return NULL;
+}
+
 static void usb_device_handle_destroy(USBDevice *dev)
 {
     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
index 0c261644ecb9e7ea20d52fd3a8b689617408bb32..bacdc81bfc4676a11dce4b4b4fb60de7d8449c1e 100644 (file)
--- a/hw/usb.c
+++ b/hw/usb.c
@@ -295,6 +295,19 @@ int set_usb_string(uint8_t *buf, const char *str)
     return q - buf;
 }
 
+USBDevice *usb_find_device(USBPort *port, uint8_t addr)
+{
+    USBDevice *dev = port->dev;
+
+    if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) {
+        return NULL;
+    }
+    if (dev->addr == addr) {
+        return dev;
+    }
+    return usb_device_find_device(dev, addr);
+}
+
 /* Hand over a packet to a device for processing.  Return value
    USB_RET_ASYNC indicates the processing isn't finished yet, the
    driver will call usb_packet_complete() when done processing it. */
index 61271768bc4982b99c9abb8bd8efe088d0835b79..1beb4b32a9bcea5e1fad18eb3045097500953b18 100644 (file)
--- a/hw/usb.h
+++ b/hw/usb.h
@@ -228,6 +228,12 @@ typedef struct USBDeviceClass {
 
     int (*init)(USBDevice *dev);
 
+    /*
+     * Walk (enabled) downstream ports, check for a matching device.
+     * Only hubs implement this.
+     */
+    USBDevice *(*find_device)(USBDevice *dev, uint8_t addr);
+
     /*
      * Process USB packet.
      * Called by the HC (Host Controller).
@@ -332,6 +338,8 @@ void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes);
 void usb_packet_skip(USBPacket *p, size_t bytes);
 void usb_packet_cleanup(USBPacket *p);
 
+USBDevice *usb_find_device(USBPort *port, uint8_t addr);
+
 int usb_handle_packet(USBDevice *dev, USBPacket *p);
 void usb_packet_complete(USBDevice *dev, USBPacket *p);
 void usb_cancel_packet(USBPacket * p);
@@ -446,6 +454,8 @@ extern const VMStateDescription vmstate_usb_device;
     .offset     = vmstate_offset_value(_state, _field, USBDevice),   \
 }
 
+USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr);
+
 int usb_device_handle_packet(USBDevice *dev, USBPacket *p);
 
 void usb_device_cancel_packet(USBDevice *dev, USBPacket *p);