ia64/xen-unstable

view tools/python/xen/xend/server/usbif.py @ 6689:7d0fb56b4a91

merge?
author cl349@firebug.cl.cam.ac.uk
date Wed Sep 07 19:01:31 2005 +0000 (2005-09-07)
parents 0e2b1e04d4cb 6d3e8f90c2df
children b2f4823b6ff0 b35215021b32 9af349b055e5 3233e7ecfa9f
line source
1 # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
2 # Copyright (C) 2004 Intel Research Cambridge
3 # Copyright (C) 2004 Mark Williamson <mark.williamson@cl.cam.ac.uk>
4 """Support for virtual USB hubs.
5 """
7 from xen.xend import sxp
8 from xen.xend.XendLogging import log
9 from xen.xend.XendError import XendError
10 from xen.xend.xenstore import DBVar
12 from xen.xend.server.controller import Dev, DevController
14 class UsbBackend:
15 """Handler for the 'back-end' channel to a USB device driver domain
16 on behalf of a front-end domain.
17 """
18 def __init__(self, controller, id, dom):
19 self.controller = controller
20 self.id = id
21 self.destroyed = False
22 self.connected = False
23 self.connecting = False
24 self.frontendDomain = self.controller.getDomain()
25 self.backendDomain = dom
27 def init(self, recreate=False, reboot=False):
28 pass
30 def __str__(self):
31 return ('<UsbifBackend frontend=%d backend=%d id=%d>'
32 % (self.frontendDomain,
33 self.backendDomain,
34 self.id))
36 def connect(self, recreate=False):
37 """Connect the controller to the usbif control interface.
39 @param recreate: true if after xend restart
40 """
41 log.debug("Connecting usbif %s", str(self))
42 if recreate or self.connected or self.connecting:
43 pass
45 def destroy(self, reboot=False):
46 """Disconnect from the usbif control interface and destroy it.
47 """
48 self.destroyed = True
50 def interfaceChanged(self):
51 pass
54 class UsbDev(Dev):
56 __exports__ = Dev.__exports__ + [
57 DBVar('port', ty='int'),
58 DBVar('path', ty='str'),
59 ]
61 def __init__(self, controller, id, config, recreate=False):
62 Dev.__init__(self, controller, id, config, recreate=recreate)
63 self.port = id
64 self.path = None
65 self.frontendDomain = self.getDomain()
66 self.backendDomain = 0
67 self.configure(self.config, recreate=recreate)
69 def init(self, recreate=False, reboot=False):
70 self.destroyed = False
71 self.frontendDomain = self.getDomain()
73 def configure(self, config, change=False, recreate=False):
74 if change:
75 raise XendError("cannot reconfigure usb")
76 #todo: FIXME: Use sxp access methods to get this value.
77 # Must not use direct indexing.
78 self.path = config[1][1]
80 #todo: FIXME: Support configuring the backend domain.
81 ## try:
82 ## self.backendDomain = int(sxp.child_value(config, 'backend', '0'))
83 ## except:
84 ## raise XendError('invalid backend domain')
86 def attach(self, recreate=False, change=False):
87 if recreate:
88 pass
89 else:
90 self.attachBackend()
91 if change:
92 self.interfaceChanged()
94 def sxpr(self):
95 val = ['usb',
96 ['id', self.id],
97 ['port', self.port],
98 ['path', self.path],
99 ]
100 return val
102 def getBackend(self):
103 return self.controller.getBackend(self.backendDomain)
105 def destroy(self, change=False, reboot=False):
106 """Destroy the device. If 'change' is true notify the front-end interface.
108 @param change: change flag
109 """
110 self.destroyed = True
111 log.debug("Destroying usb domain=%d id=%s", self.frontendDomain, self.id)
112 if change:
113 self.interfaceChanged()
115 def interfaceChanged(self):
116 """Tell the back-end to notify the front-end that a device has been
117 added or removed.
118 """
119 self.getBackend().interfaceChanged()
121 def attachBackend(self):
122 """Attach the device to its controller.
124 """
125 self.getBackend().connect()
127 class UsbifController(DevController):
128 """USB device interface controller. Handles all USB devices
129 for a domain.
130 """
132 def __init__(self, vm, recreate=False):
133 """Create a USB device controller.
134 """
135 DevController.__init__(self, vm, recreate=recreate)
136 self.backends = {}
137 self.backendId = 0
139 def init(self, recreate=False, reboot=False):
140 self.destroyed = False
141 if reboot:
142 self.rebootBackends()
143 self.rebootDevices()
145 def sxpr(self):
146 val = ['usbif',
147 ['dom', self.getDomain()]]
148 return val
150 def newDevice(self, id, config, recreate=False):
151 return UsbDev(self, id, config, recreate=recreate)
153 def destroyController(self, reboot=False):
154 """Destroy the controller and all devices.
155 """
156 self.destroyed = True
157 log.debug("Destroying blkif domain=%d", self.getDomain())
158 self.destroyDevices(reboot=reboot)
159 self.destroyBackends(reboot=reboot)
161 def rebootBackends(self):
162 for backend in self.backends.values():
163 backend.init(reboot=True)
165 def getBackendById(self, id):
166 return self.backends.get(id)
168 def getBackendByDomain(self, dom):
169 for backend in self.backends.values():
170 if backend.backendDomain == dom:
171 return backend
172 return None
174 def getBackend(self, dom):
175 backend = self.getBackendByDomain(dom)
176 if backend: return backend
177 backend = UsbBackend(self, self.backendId, dom)
178 self.backendId += 1
179 self.backends[backend.getId()] = backend
180 backend.init()
181 return backend
183 def destroyBackends(self, reboot=False):
184 for backend in self.backends.values():
185 backend.destroy(reboot=reboot)