[virt-tools-list] [PATCH v2 2/2] addhardware: use bus, device to distingush usb products

Guannan Ren gren at redhat.com
Tue Apr 23 13:30:35 UTC 2013


When the usb device being attached is unique, the
vendorId and productId is being used in XML.
When there are multiple usb devices with same vendorId
and productId in the host device list, the bus and device
number is being used to attach the device.
---
 tests/nodedev-xml/devxml/usbdev1.xml |  1 +
 virtManager/addhardware.py           | 40 ++++++++++++++++++++++++++++++++++--
 virtinst/VirtualHostDevice.py        |  5 ++---
 3 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/tests/nodedev-xml/devxml/usbdev1.xml b/tests/nodedev-xml/devxml/usbdev1.xml
index 96d5ed8..cf0b50a 100644
--- a/tests/nodedev-xml/devxml/usbdev1.xml
+++ b/tests/nodedev-xml/devxml/usbdev1.xml
@@ -2,5 +2,6 @@
       <source>
         <vendor id='0x0781'/>
         <product id='0x5151'/>
+        <address bus='1' device='4'/>
       </source>
     </hostdev>
diff --git a/virtManager/addhardware.py b/virtManager/addhardware.py
index af36560..95d56cd 100644
--- a/virtManager/addhardware.py
+++ b/virtManager/addhardware.py
@@ -281,7 +281,7 @@ class vmmAddHardware(vmmGObjectUI):
         # Host device list
         # model = [ Description, nodedev name ]
         host_dev = self.widget("host-device")
-        host_dev_model = Gtk.ListStore(str, str)
+        host_dev_model = Gtk.ListStore(str, str, str)
         host_dev.set_model(host_dev_model)
 
         host_col = Gtk.TreeViewColumn()
@@ -577,7 +577,7 @@ class vmmAddHardware(vmmGObjectUI):
                 if dev.name == subdev.parent:
                     prettyname = dev.pretty_name(subdev)
 
-            model.append([prettyname, dev.name])
+            model.append([prettyname, dev.name, devtype])
 
         if len(model) == 0:
             model.append([_("No Devices Available"), None])
@@ -1400,6 +1400,21 @@ class vmmAddHardware(vmmGObjectUI):
         except Exception, e:
             return self.err.val_err(_("Sound device parameter error"), e)
 
+    def get_nodedev_numbers(self, devtype, vendor, product):
+        count = 0
+        devs = self.conn.get_nodedevs(devtype, None)
+
+        for dev in devs:
+            if vendor == dev.vendor_id and \
+                product == dev.product_id:
+                count += 1
+
+        logging.debug("There are %d node devices with "
+                      "vendorId: %s, productId: %s",
+                       count, vendor, product)
+
+        return count
+
     def validate_page_hostdev(self):
         ret = self.get_config_host_device_info()
         nodedev_name = ret and ret[1] or None
@@ -1415,6 +1430,27 @@ class vmmAddHardware(vmmGObjectUI):
         except Exception, e:
             return self.err.val_err(_("Host device parameter error"), e)
 
+        devtype = ret[2]
+        if devtype == "usb_device":
+            vendor = self._dev.vendor
+            product = self._dev.product
+            count = self.get_nodedev_numbers(devtype, vendor, product)
+            if count > 1:
+                self._dev.vendor = None
+                self._dev.product = None
+                return
+
+            if count == 1:
+                self._dev.bus = None
+                self._dev.device = None
+                return
+
+            if not count:
+                raise RuntimeError(_("Could not find USB device "
+                                     "(vendorId: %s, productId: %s) "
+                                     % (vendor, product)))
+
+
     def validate_page_char(self):
         chartype = self.get_char_type()
         modebox = self.widget("char-mode")
diff --git a/virtinst/VirtualHostDevice.py b/virtinst/VirtualHostDevice.py
index f5740d0..ee38242 100644
--- a/virtinst/VirtualHostDevice.py
+++ b/virtinst/VirtualHostDevice.py
@@ -215,9 +215,8 @@ class VirtualHostDeviceUSB(VirtualHostDevice):
         self.vendor = nodedev.vendor_id
         self.product = nodedev.product_id
 
-        if not (self.vendor or self.product):
-            self.bus = nodedev.bus
-            self.device = nodedev.device
+        self.bus = nodedev.bus
+        self.device = nodedev.device
 
     def _get_source_xml(self):
         xml = ""
-- 
1.8.1.4




More information about the virt-tools-list mailing list