Patchwork [PATCHv3,10/14] Add get_dev_path callback for usb bus.

login
register
mail settings
Submitter Gleb Natapov
Date Nov. 10, 2010, 5:14 p.m.
Message ID <1289409261-5418-11-git-send-email-gleb@redhat.com>
Download mbox | patch
Permalink /patch/70655/
State New
Headers show

Comments

Gleb Natapov - Nov. 10, 2010, 5:14 p.m.
Signed-off-by: Gleb Natapov <gleb@redhat.com>
---
 hw/usb-bus.c |   35 +++++++++++++++++++++++++++++++++++
 1 files changed, 35 insertions(+), 0 deletions(-)
Blue Swirl - Nov. 10, 2010, 6:37 p.m.
On Wed, Nov 10, 2010 at 5:14 PM, Gleb Natapov <gleb@redhat.com> wrote:
>
> Signed-off-by: Gleb Natapov <gleb@redhat.com>
> ---
>  hw/usb-bus.c |   35 +++++++++++++++++++++++++++++++++++
>  1 files changed, 35 insertions(+), 0 deletions(-)
>
> diff --git a/hw/usb-bus.c b/hw/usb-bus.c
> index 256b881..6292282 100644
> --- a/hw/usb-bus.c
> +++ b/hw/usb-bus.c
> @@ -5,11 +5,13 @@
>  #include "monitor.h"
>
>  static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
> +static char *usbbus_get_fw_dev_path(DeviceState *dev);
>
>  static struct BusInfo usb_bus_info = {
>     .name      = "USB",
>     .size      = sizeof(USBBus),
>     .print_dev = usb_bus_dev_print,
> +    .get_fw_dev_path = usbbus_get_fw_dev_path,
>  };
>  static int next_usb_bus = 0;
>  static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
> @@ -307,3 +309,36 @@ USBDevice *usbdevice_create(const char *cmdline)
>     }
>     return usb->usbdevice_init(params);
>  }
> +
> +static int usbbus_get_fw_dev_path_helper(USBDevice *d, USBBus *bus, char *p,
> +                                         int len)
> +{
> +    int l = 0;
> +    USBPort *port;
> +
> +    QTAILQ_FOREACH(port, &bus->used, next) {
> +        if (port->dev != d)

Braces.

> +            continue;
> +
> +        if (port->pdev) {
> +            l = usbbus_get_fw_dev_path_helper(port->pdev, bus, p, len);
> +        }
> +        l += snprintf(p + l, len - l, "%s@%x/", qdev_fw_name(&d->qdev),
> +                      port->index);
> +        return l;
> +    }
> +    return 0;

With this return value...

> +}
> +
> +static char *usbbus_get_fw_dev_path(DeviceState *dev)
> +{
> +    USBDevice *d = (USBDevice*)dev;
> +    USBBus *bus = usb_bus_from_device(d);
> +    char path[100];
> +    int l;
> +
> +    l = usbbus_get_fw_dev_path_helper(d, bus, path, sizeof(path));
> +    path[l-1] = '\0';

...this statement will have undesirable effects...

> +
> +    return strdup(path);

...like this.
Gleb Natapov - Nov. 10, 2010, 7:11 p.m.
On Wed, Nov 10, 2010 at 06:37:45PM +0000, Blue Swirl wrote:
> On Wed, Nov 10, 2010 at 5:14 PM, Gleb Natapov <gleb@redhat.com> wrote:
> >
> > Signed-off-by: Gleb Natapov <gleb@redhat.com>
> > ---
> >  hw/usb-bus.c |   35 +++++++++++++++++++++++++++++++++++
> >  1 files changed, 35 insertions(+), 0 deletions(-)
> >
> > diff --git a/hw/usb-bus.c b/hw/usb-bus.c
> > index 256b881..6292282 100644
> > --- a/hw/usb-bus.c
> > +++ b/hw/usb-bus.c
> > @@ -5,11 +5,13 @@
> >  #include "monitor.h"
> >
> >  static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
> > +static char *usbbus_get_fw_dev_path(DeviceState *dev);
> >
> >  static struct BusInfo usb_bus_info = {
> >     .name      = "USB",
> >     .size      = sizeof(USBBus),
> >     .print_dev = usb_bus_dev_print,
> > +    .get_fw_dev_path = usbbus_get_fw_dev_path,
> >  };
> >  static int next_usb_bus = 0;
> >  static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
> > @@ -307,3 +309,36 @@ USBDevice *usbdevice_create(const char *cmdline)
> >     }
> >     return usb->usbdevice_init(params);
> >  }
> > +
> > +static int usbbus_get_fw_dev_path_helper(USBDevice *d, USBBus *bus, char *p,
> > +                                         int len)
> > +{
> > +    int l = 0;
> > +    USBPort *port;
> > +
> > +    QTAILQ_FOREACH(port, &bus->used, next) {
> > +        if (port->dev != d)
> 
> Braces.
> 
> > +            continue;
> > +
> > +        if (port->pdev) {
> > +            l = usbbus_get_fw_dev_path_helper(port->pdev, bus, p, len);
> > +        }
> > +        l += snprintf(p + l, len - l, "%s@%x/", qdev_fw_name(&d->qdev),
> > +                      port->index);
> > +        return l;
> > +    }
> > +    return 0;
> 
> With this return value...
> 
> > +}
This can only happen if device is not attached to parent bus, so we
should never get here I think. I do not know USB bus enough to be sure.
May be we should abort here.

> > +
> > +static char *usbbus_get_fw_dev_path(DeviceState *dev)
> > +{
> > +    USBDevice *d = (USBDevice*)dev;
> > +    USBBus *bus = usb_bus_from_device(d);
> > +    char path[100];
> > +    int l;
> > +
> > +    l = usbbus_get_fw_dev_path_helper(d, bus, path, sizeof(path));
> > +    path[l-1] = '\0';
> 
> ...this statement will have undesirable effects...
> 
> > +
> > +    return strdup(path);
> 
> ...like this.

--
			Gleb.

Patch

diff --git a/hw/usb-bus.c b/hw/usb-bus.c
index 256b881..6292282 100644
--- a/hw/usb-bus.c
+++ b/hw/usb-bus.c
@@ -5,11 +5,13 @@ 
 #include "monitor.h"
 
 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
+static char *usbbus_get_fw_dev_path(DeviceState *dev);
 
 static struct BusInfo usb_bus_info = {
     .name      = "USB",
     .size      = sizeof(USBBus),
     .print_dev = usb_bus_dev_print,
+    .get_fw_dev_path = usbbus_get_fw_dev_path,
 };
 static int next_usb_bus = 0;
 static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
@@ -307,3 +309,36 @@  USBDevice *usbdevice_create(const char *cmdline)
     }
     return usb->usbdevice_init(params);
 }
+
+static int usbbus_get_fw_dev_path_helper(USBDevice *d, USBBus *bus, char *p,
+                                         int len)
+{
+    int l = 0;
+    USBPort *port;
+
+    QTAILQ_FOREACH(port, &bus->used, next) {
+        if (port->dev != d)
+            continue;
+
+        if (port->pdev) {
+            l = usbbus_get_fw_dev_path_helper(port->pdev, bus, p, len);
+        }
+        l += snprintf(p + l, len - l, "%s@%x/", qdev_fw_name(&d->qdev),
+                      port->index);
+        return l;
+    }
+    return 0;
+}
+
+static char *usbbus_get_fw_dev_path(DeviceState *dev)
+{
+    USBDevice *d = (USBDevice*)dev;
+    USBBus *bus = usb_bus_from_device(d);
+    char path[100];
+    int l;
+
+    l = usbbus_get_fw_dev_path_helper(d, bus, path, sizeof(path));
+    path[l-1] = '\0';
+
+    return strdup(path);
+}