Skip to contentSkip to conversation

display device identifier string for user with info usb

Qemu-devel Lonnie Mendez 1 participant 3 messages May 22, 2006 – Jun 15, 2006 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

The attached patch provides more output to accompany info usb. A
device name is provided which is obtained from product string descriptor. This is to help clear up what device is which as the only information currently provided to the user are the bus and address the device has been given from the guest.

hw/usb.h +1 -0
@@ -127,6 +127,7 @@
     int (*handle_data)(USBDevice *dev, int pid, uint8_t devep,
                        uint8_t *data, int len);
     uint8_t addr;
+    char devname[32];
     
     int state;
     uint8_t setup_buf[8];
hw/usb-hub.c +2 -0
@@ -542,6 +542,8 @@
     s->dev.handle_control = usb_hub_handle_control;
     s->dev.handle_data = usb_hub_handle_data;
 
+    strcpy(s->dev.devname, "QEMU USB Hub");
+
     s->nb_ports = nb_ports;
     for(i = 0; i < s->nb_ports; i++) {
         port = &s->ports[i];
hw/usb-hid.c +4 -0
@@ -515,6 +515,8 @@
     s->dev.handle_data = usb_mouse_handle_data;
     s->kind = USB_TABLET;
 
+    strcpy(s->dev.devname, "QEMU USB Tablet");
+
     return (USBDevice *)s;
 }
 
@@ -533,5 +535,7 @@
     s->dev.handle_data = usb_mouse_handle_data;
     s->kind = USB_MOUSE;
 
+    strcpy(s->dev.devname, "QEMU USB Mouse");
+
     return (USBDevice *)s;
 }
usb-linux.c +23 -4
@@ -44,6 +44,7 @@
                         int vendor_id, int product_id, 
                         const char *product_name, int speed);
 static int usb_host_find_device(int *pbus_num, int *paddr, 
+                                char *product_name, 
                                 const char *devname);
 
 //#define DEBUG
@@ -145,8 +146,9 @@
     char buf[1024];
     int descr_len, dev_descr_len, config_descr_len, nb_interfaces;
     int bus_num, addr;
+    char product_name[32];
 
-    if (usb_host_find_device(&bus_num, &addr, devname) < 0) 
+    if (usb_host_find_device(&bus_num, &addr, product_name, devname) < 0) 
         return NULL;
     
     snprintf(buf, sizeof(buf), USBDEVFS_PATH "/%03d/%03d", 
@@ -230,6 +232,12 @@
     dev->dev.handle_reset = usb_host_handle_reset;
     dev->dev.handle_control = usb_host_handle_control;
     dev->dev.handle_data = usb_host_handle_data;
+
+    if (product_name[0] == '\0')
+        snprintf(dev->dev.devname, 31, "host:%s", devname);
+    else
+        strncpy(dev->dev.devname, product_name, 31);
+
     return (USBDevice *)dev;
 }
 
@@ -337,6 +345,7 @@
     int product_id;
     int bus_num;
     int addr;
+    char product_name[32];
 } FindDeviceState;
 
 static int usb_host_find_device_scan(void *opaque, int bus_num, int addr, 
@@ -345,8 +354,11 @@
                                      const char *product_name, int speed)
 {
     FindDeviceState *s = opaque;
-    if (vendor_id == s->vendor_id &&
-        product_id == s->product_id) {
+    if ((vendor_id == s->vendor_id &&
+        product_id == s->product_id) ||
+        (bus_num == s->bus_num &&
+        addr == s->addr)) {
+        strncpy(s->product_name, product_name, 31);
         s->bus_num = bus_num;
         s->addr = addr;
         return 1;
@@ -358,7 +370,8 @@
 /* the syntax is : 
    'bus.addr' (decimal numbers) or 
    'vendor_id:product_id' (hexa numbers) */
-static int usb_host_find_device(int *pbus_num, int *paddr, 
+static int usb_host_find_device(int *pbus_num, int *paddr,
+                                char *product_name,
                                 const char *devname)
 {
     const char *p;
@@ -369,6 +382,11 @@
     if (p) {
         *pbus_num = strtoul(devname, NULL, 0);
         *paddr = strtoul(p + 1, NULL, 0);
+        fs.bus_num = *pbus_num;
+        fs.addr = *paddr;
+        ret = usb_host_scan(&fs, usb_host_find_device_scan);
+        if (ret)
+            strcpy(product_name, fs.product_name);
         return 0;
     }
     p = strchr(devname, ':');
@@ -379,6 +397,7 @@
         if (ret) {
             *pbus_num = fs.bus_num;
             *paddr = fs.addr;
+            strcpy(product_name, fs.product_name);
             return 0;
         }
     }
#1 lore raw

Whoops. Wrong patch - missing 1 chunk - please see attached.

hw/usb.h +1 -0
@@ -127,6 +127,7 @@
     int (*handle_data)(USBDevice *dev, int pid, uint8_t devep,
                        uint8_t *data, int len);
     uint8_t addr;
+    char devname[32];
     
     int state;
     uint8_t setup_buf[8];
hw/usb-hub.c +2 -0
@@ -542,6 +542,8 @@
     s->dev.handle_control = usb_hub_handle_control;
     s->dev.handle_data = usb_hub_handle_data;
 
+    strcpy(s->dev.devname, "QEMU USB Hub");
+
     s->nb_ports = nb_ports;
     for(i = 0; i < s->nb_ports; i++) {
         port = &s->ports[i];
hw/usb-hid.c +4 -0
@@ -515,6 +515,8 @@
     s->dev.handle_data = usb_mouse_handle_data;
     s->kind = USB_TABLET;
 
+    strcpy(s->dev.devname, "QEMU USB Tablet");
+
     return (USBDevice *)s;
 }
 
@@ -533,5 +535,7 @@
     s->dev.handle_data = usb_mouse_handle_data;
     s->kind = USB_MOUSE;
 
+    strcpy(s->dev.devname, "QEMU USB Mouse");
+
     return (USBDevice *)s;
 }
usb-linux.c +23 -4
@@ -44,6 +44,7 @@
                         int vendor_id, int product_id, 
                         const char *product_name, int speed);
 static int usb_host_find_device(int *pbus_num, int *paddr, 
+                                char *product_name, 
                                 const char *devname);
 
 //#define DEBUG
@@ -145,8 +146,9 @@
     char buf[1024];
     int descr_len, dev_descr_len, config_descr_len, nb_interfaces;
     int bus_num, addr;
+    char product_name[32];
 
-    if (usb_host_find_device(&bus_num, &addr, devname) < 0) 
+    if (usb_host_find_device(&bus_num, &addr, product_name, devname) < 0) 
         return NULL;
     
     snprintf(buf, sizeof(buf), USBDEVFS_PATH "/%03d/%03d", 
@@ -230,6 +232,12 @@
     dev->dev.handle_reset = usb_host_handle_reset;
     dev->dev.handle_control = usb_host_handle_control;
     dev->dev.handle_data = usb_host_handle_data;
+
+    if (product_name[0] == '\0')
+        snprintf(dev->dev.devname, 31, "host:%s", devname);
+    else
+        strncpy(dev->dev.devname, product_name, 31);
+
     return (USBDevice *)dev;
 }
 
@@ -337,6 +345,7 @@
     int product_id;
     int bus_num;
     int addr;
+    char product_name[32];
 } FindDeviceState;
 
 static int usb_host_find_device_scan(void *opaque, int bus_num, int addr, 
@@ -345,8 +354,11 @@
                                      const char *product_name, int speed)
 {
     FindDeviceState *s = opaque;
-    if (vendor_id == s->vendor_id &&
-        product_id == s->product_id) {
+    if ((vendor_id == s->vendor_id &&
+        product_id == s->product_id) ||
+        (bus_num == s->bus_num &&
+        addr == s->addr)) {
+        strncpy(s->product_name, product_name, 31);
         s->bus_num = bus_num;
         s->addr = addr;
         return 1;
@@ -358,7 +370,8 @@
 /* the syntax is : 
    'bus.addr' (decimal numbers) or 
    'vendor_id:product_id' (hexa numbers) */
-static int usb_host_find_device(int *pbus_num, int *paddr, 
+static int usb_host_find_device(int *pbus_num, int *paddr,
+                                char *product_name,
                                 const char *devname)
 {
     const char *p;
@@ -369,6 +382,11 @@
     if (p) {
         *pbus_num = strtoul(devname, NULL, 0);
         *paddr = strtoul(p + 1, NULL, 0);
+        fs.bus_num = *pbus_num;
+        fs.addr = *paddr;
+        ret = usb_host_scan(&fs, usb_host_find_device_scan);
+        if (ret)
+            strcpy(product_name, fs.product_name);
         return 0;
     }
     p = strchr(devname, ':');
@@ -379,6 +397,7 @@
         if (ret) {
             *pbus_num = fs.bus_num;
             *paddr = fs.addr;
+            strcpy(product_name, fs.product_name);
             return 0;
         }
     }
vl.c +2 -2
@@ -3387,8 +3422,8 @@
             speed_str = "?"; 
             break;
         }
-        term_printf("  Device %d.%d, speed %s Mb/s\n", 
-                    0, dev->addr, speed_str);
+        term_printf("  Device %d.%d, Speed %s Mb/s, Product %s\n", 
+                    0, dev->addr, speed_str, dev->devname);
     }
 }
 

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 5 lines · Fabrice Bellard

OK for this patch, but the following must be changed:

1) All instances of strcpy and strncpy must be replaced by pstrcpy.

2) product_name_size must be passed as parameter to usb_host_find_device().

lo. Attached is something I hope you will find to be more clean. I
used a define for product name size since this is used in two places to define an array size (in usb_host_device_open() and the FindDeviceState struct).

hw/usb.h +1 -0
@@ -128,6 +128,7 @@
     int (*handle_data)(USBDevice *dev, int pid, uint8_t devep,
                        uint8_t *data, int len);
     uint8_t addr;
+    char devname[32];
     
     int state;
     uint8_t setup_buf[8];
hw/usb-hub.c +2 -0
@@ -544,6 +544,8 @@
     s->dev.handle_control = usb_hub_handle_control;
     s->dev.handle_data = usb_hub_handle_data;
 
+    pstrcpy(s->dev.devname, sizeof(s->dev.devname), "QEMU USB Hub");
+
     s->nb_ports = nb_ports;
     for(i = 0; i < s->nb_ports; i++) {
         port = &s->ports[i];
hw/usb-hid.c +4 -0
@@ -521,6 +521,8 @@
     s->dev.handle_data = usb_mouse_handle_data;
     s->kind = USB_TABLET;
 
+    pstrcpy(s->dev.devname, sizeof(s->dev.devname), "QEMU USB Tablet");
+
     return (USBDevice *)s;
 }
 
@@ -539,5 +541,7 @@
     s->dev.handle_data = usb_mouse_handle_data;
     s->kind = USB_MOUSE;
 
+    pstrcpy(s->dev.devname, sizeof(s->dev.devname), "QEMU USB Mouse");
+
     return (USBDevice *)s;
 }
hw/usb-msd.c +5 -0
@@ -374,6 +374,7 @@
 {
     MSDState *s;
     BlockDriverState *bdrv;
+    char msd_info_str[32];
 
     s = qemu_mallocz(sizeof(MSDState));
     if (!s)
@@ -389,6 +390,10 @@
     s->dev.handle_control = usb_msd_handle_control;
     s->dev.handle_data = usb_msd_handle_data;
 
+    snprintf(msd_info_str, sizeof(msd_info_str), "QEMU USB MSD(%.17s)",
+             filename);
+    pstrcpy(s->dev.devname, sizeof(s->dev.devname), msd_info_str);
+
     s->scsi_dev = scsi_disk_init(bdrv, usb_msd_command_complete, s);
     usb_msd_handle_reset((USBDevice *)s, 0);
     return (USBDevice *)s;
vl.c +2 -2
@@ -3412,8 +3447,8 @@
             speed_str = "?"; 
             break;
         }
-        term_printf("  Device %d.%d, speed %s Mb/s\n", 
-                    0, dev->addr, speed_str);
+        term_printf("  Device %d.%d, Speed %s Mb/s, Product %s\n", 
+                    0, dev->addr, speed_str, dev->devname);
     }
 }
 
usb-linux.c +25 -3
@@ -44,11 +44,13 @@
                         int vendor_id, int product_id, 
                         const char *product_name, int speed);
 static int usb_host_find_device(int *pbus_num, int *paddr, 
+                                char *product_name, 
                                 const char *devname);
 
 //#define DEBUG
 
 #define USBDEVFS_PATH "/proc/bus/usb"
+#define PRODUCT_NAME_SZ 32
 
 typedef struct USBHostDevice {
     USBDevice dev;
@@ -145,8 +147,9 @@
     char buf[1024];
     int descr_len, dev_descr_len, config_descr_len, nb_interfaces;
     int bus_num, addr;
+    char product_name[PRODUCT_NAME_SZ];
 
-    if (usb_host_find_device(&bus_num, &addr, devname) < 0) 
+    if (usb_host_find_device(&bus_num, &addr, product_name, devname) < 0) 
         return NULL;
     
     snprintf(buf, sizeof(buf), USBDEVFS_PATH "/%03d/%03d", 
@@ -230,6 +233,14 @@
     dev->dev.handle_reset = usb_host_handle_reset;
     dev->dev.handle_control = usb_host_handle_control;
     dev->dev.handle_data = usb_host_handle_data;
+
+    if (product_name[0] == '\0')
+        snprintf(dev->dev.devname, sizeof(dev->dev.devname),
+                 "host:%s", devname);
+    else
+        pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
+                product_name);
+
     return (USBDevice *)dev;
 }
 
@@ -337,6 +348,7 @@
     int product_id;
     int bus_num;
     int addr;
+    char product_name[PRODUCT_NAME_SZ];
 } FindDeviceState;
 
 static int usb_host_find_device_scan(void *opaque, int bus_num, int addr, 
@@ -345,8 +357,11 @@
                                      const char *product_name, int speed)
 {
     FindDeviceState *s = opaque;
-    if (vendor_id == s->vendor_id &&
-        product_id == s->product_id) {
+    if ((vendor_id == s->vendor_id &&
+        product_id == s->product_id) ||
+        (bus_num == s->bus_num &&
+        addr == s->addr)) {
+        pstrcpy(s->product_name, PRODUCT_NAME_SZ, product_name);
         s->bus_num = bus_num;
         s->addr = addr;
         return 1;
@@ -359,6 +374,7 @@
    'bus.addr' (decimal numbers) or 
    'vendor_id:product_id' (hexa numbers) */
 static int usb_host_find_device(int *pbus_num, int *paddr, 
+                                char *product_name, 
                                 const char *devname)
 {
     const char *p;
@@ -369,6 +385,11 @@
     if (p) {
         *pbus_num = strtoul(devname, NULL, 0);
         *paddr = strtoul(p + 1, NULL, 0);
+        fs.bus_num = *pbus_num;
+        fs.addr = *paddr;
+        ret = usb_host_scan(&fs, usb_host_find_device_scan);
+        if (ret)
+            pstrcpy(product_name, PRODUCT_NAME_SZ, fs.product_name);
         return 0;
     }
     p = strchr(devname, ':');
@@ -379,6 +400,7 @@
         if (ret) {
             *pbus_num = fs.bus_num;
             *paddr = fs.addr;
+            pstrcpy(product_name, PRODUCT_NAME_SZ, fs.product_name);
             return 0;
         }
     }