Patchwork [2/3] usb: add public usb_device_by_id

login
register
mail settings
Submitter Alon Levy
Date Oct. 19, 2010, 10:33 a.m.
Message ID <1287484411-13611-3-git-send-email-alevy@redhat.com>
Download mbox | patch
Permalink /patch/68310/
State New
Headers show

Comments

Alon Levy - Oct. 19, 2010, 10:33 a.m.
---
 hw/usb-bus.c |   16 ++++++++++++++++
 hw/usb.h     |    1 +
 2 files changed, 17 insertions(+), 0 deletions(-)
Gerd Hoffmann - Oct. 19, 2010, 1:09 p.m.
> +USBDevice *usb_device_by_id(const char* id)
> +{
> +    USBBus *bus;
> +    DeviceState *qdev;
> +    USBDevice *dev;
> +
> +    QTAILQ_FOREACH(bus,&busses, next) {
> +        qdev = qdev_find_recursive(&bus->qbus, id);
> +        if (qdev != NULL) {
> +            dev = DO_UPCAST(USBDevice, qdev, qdev);
> +            return dev;
> +        }
> +    }

You don't need qdev_find_recursive here.  Have a look at the usb_info() 
code to see how to loop over all usb devices.  Then compare id with 
USBDevice->qdev.id.

cheers,
   Gerd

Patch

diff --git a/hw/usb-bus.c b/hw/usb-bus.c
index b692503..d732bd3 100644
--- a/hw/usb-bus.c
+++ b/hw/usb-bus.c
@@ -189,6 +189,22 @@  int usb_device_detach(USBDevice *dev)
     return 0;
 }
 
+USBDevice *usb_device_by_id(const char* id)
+{
+    USBBus *bus;
+    DeviceState *qdev;
+    USBDevice *dev;
+
+    QTAILQ_FOREACH(bus, &busses, next) {
+        qdev = qdev_find_recursive(&bus->qbus, id);
+        if (qdev != NULL) {
+            dev = DO_UPCAST(USBDevice, qdev, qdev);
+            return dev;
+        }
+    }
+    return NULL;
+}
+
 int usb_device_delete_addr(int busnr, int addr)
 {
     USBBus *bus;
diff --git a/hw/usb.h b/hw/usb.h
index 00d2802..e70fccd 100644
--- a/hw/usb.h
+++ b/hw/usb.h
@@ -317,6 +317,7 @@  void usb_unregister_port(USBBus *bus, USBPort *port);
 int usb_device_attach(USBDevice *dev);
 int usb_device_detach(USBDevice *dev);
 int usb_device_delete_addr(int busnr, int addr);
+USBDevice *usb_device_by_id(const char* id);
 
 static inline USBBus *usb_bus_from_device(USBDevice *d)
 {