-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathUSBCSInterface.py
More file actions
313 lines (256 loc) · 11.4 KB
/
USBCSInterface.py
File metadata and controls
313 lines (256 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# USBCSInterface.py
#
# Contains class definition for USBCSInterface.
from USB import *
class USBCSInterface:
name = "USB class-specific interface"
def __init__(self, maxusb_app, cs_config, usbclass, sub, proto, verbose=0, descriptors={}):
self.maxusb_app = maxusb_app
self.usbclass = usbclass
self.sub = sub
self.proto = proto
self.cs_config = cs_config
self.verbose = verbose
self.descriptors = descriptors
self.descriptors[USB.desc_type_cs_interface] = self.get_descriptor
self.request_handlers = {
6 : self.handle_get_descriptor_request,
11 : self.handle_set_interface_request
}
# USB 2.0 specification, section 9.4.3 (p 281 of pdf)
# HACK: blatant copypasta from USBDevice pains me deeply
def handle_get_descriptor_request(self, req):
dtype = (req.value >> 8) & 0xff
dindex = req.value & 0xff
lang = req.index
n = req.length
response = None
if self.verbose > 2:
print(self.name, ("received GET_DESCRIPTOR req %d, index %d, " \
+ "language 0x%04x, length %d") \
% (dtype, dindex, lang, n))
# TODO: handle KeyError
response = self.descriptors[dtype]
if callable(response):
response = response(dindex)
if response:
n = min(n, len(response))
self.configuration.device.maxusb_app.send_on_endpoint(0, response[:n])
if self.verbose > 5:
print(self.name, "sent", n, "bytes in response")
def handle_set_interface_request(self, req):
if self.verbose > 0:
print(self.name, "received SET_INTERFACE request")
self.configuration.device.maxusb_app.stall_ep0()
# Table 9-12 of USB 2.0 spec (pdf page 296)
def get_descriptor(self):
d = b''
######################### CDC class ####################################################################
if self.cs_config[0] == 0x00 and self.usbclass == 2:
bDescriptorType = 36 # CS_INTERFACE
bDescriptorSubtype = 0x00 # Header Functional Descriptor
bcdCDC = self.cs_config[1]
d = bytearray([
bDescriptorType,
bDescriptorSubtype,
(bcdCDC >> 8) & 0xff,
bcdCDC & 0xff,
])
config_length = bytes ([len(d)+1])
d = config_length + d
if self.cs_config[0] == 0x01 and self.usbclass == 2:
bDescriptorType = 36 # CS_INTERFACE
bDescriptorSubtype = 0x01 # Call Management Functional Descriptor
bmCapabilities = self.cs_config[1]
bDataInterface = self.cs_config[2]
d = bytearray([
bDescriptorType,
bDescriptorSubtype,
bmCapabilities,
bDataInterface
])
config_length = bytes ([len(d)+1])
d = config_length + d
if self.cs_config[0] == 0x02 and self.usbclass == 2:
bDescriptorType = 36 # CS_INTERFACE
bDescriptorSubtype = 0x02 # Abstract Control Management Functional Descriptor
bmCapabilities = self.cs_config[1]
d = bytearray([
bDescriptorType,
bDescriptorSubtype,
bmCapabilities
])
config_length = bytes ([len(d)+1])
d = config_length + d
if self.cs_config[0] == 0x06 and self.usbclass == 2:
bDescriptorType = 36 # CS_INTERFACE
bDescriptorSubtype = 0x06 # Abstract Control Management Functional Descriptor
bControlInterface = self.cs_config[1]
bSubordinateInterface = self.cs_config[2]
d = bytearray([
bDescriptorType,
bDescriptorSubtype,
bControlInterface,
bSubordinateInterface
])
config_length = bytes ([len(d)+1])
d = config_length + d
if self.cs_config[0] == 0x0f and self.usbclass == 2 and self.sub == 6:
bDescriptorType = 36 # CS_INTERFACE
bDescriptorSubtype = 0x0f # Ethernet Networking Functional Descriptor
iMACAddress = self.cs_config[1]
bmEthernetStatistics = self.cs_config[2]
wMaxSegmentSize = self.cs_config[3]
wNumberMCFilters = self.cs_config[4]
bNumberPowerFilters = self.cs_config[5]
d = bytearray([
bDescriptorType,
bDescriptorSubtype,
iMACAddress,
(bmEthernetStatistics >> 24) & 0xff,
(bmEthernetStatistics >> 16) & 0xff,
(bmEthernetStatistics >> 8) & 0xff,
bmEthernetStatistics & 0xff,
(wMaxSegmentSize >> 8) & 0xff,
wMaxSegmentSize & 0xff,
(wNumberMCFilters >> 8) & 0xff,
wNumberMCFilters & 0xff,
bNumberPowerFilters
])
config_length = bytes ([len(d)+1])
d = config_length + d
########################## Audio class #################################################################
if self.cs_config[0] == 0x01 and self.usbclass == 1 and self.sub == 1 and self.proto == 0: # HEADER
bDescriptorType = 36 # CS_INTERFACE
bDescriptorSubtype = 0x01 #HEADER
bcdADC = self.cs_config[1]
wTotalLength = self.cs_config[2]
bInCollection = self.cs_config[3]
baInterfaceNr1 = self.cs_config[4]
baInterfaceNr2 = self.cs_config[5] # HACK: hardcoded number of interface
d = bytearray([
bDescriptorType,
bDescriptorSubtype,
(bcdADC >> 8) & 0xff,
bcdADC & 0xff,
wTotalLength & 0xff,
(wTotalLength >> 8) & 0xff,
bInCollection,
baInterfaceNr1,
baInterfaceNr2
])
config_length = bytes ([len(d)+1])
d = config_length + d
elif self.cs_config[0] == 0x02 and self.usbclass == 1 and self.sub == 1 and self.proto == 0: # INPUT_TERMINAL
bDescriptorType = 36 # CS_INTERFACE
bDescriptorSubtype = 0x02 # INPUT_TERMINAL
bTerminalID = self.cs_config[1]
wTerminalType = self.cs_config[2]
bAssocTerminal = self.cs_config[3] # ID of associated output terminal
bNrChannels = self.cs_config[4] # number of logical output channels
wChannelConfig = self.cs_config[5] # spatial location of logical channels: Left front/Right front
iChannelNames = self.cs_config[6] # Index of String descriptor describing name of logical channel
iTerminal = self.cs_config[7] # Index of String descriptor describing input terminal
d = bytearray([
bDescriptorType,
bDescriptorSubtype,
bTerminalID,
wTerminalType & 0xff,
(wTerminalType >> 8) & 0xff,
bAssocTerminal,
bNrChannels,
wChannelConfig & 0xff,
(wChannelConfig >> 8) & 0xff,
iChannelNames,
iTerminal
])
config_length = bytes ([len(d)+1])
d = config_length + d
elif self.cs_config[0] == 0x03 and self.usbclass == 1 and self.sub == 1 and self.proto == 0: # OUTPUT_TERMINAL
bDescriptorType = 36 # CS_INTERFACE
bDescriptorSubtype = 0x03 # OUTPUT_TERMINAL
bTerminalID = self.cs_config[1]
wTerminalType = self.cs_config[2]
bAssocTerminal = self.cs_config[3] # ID of associated output terminal
bSourceID = self.cs_config[4] # ID of the terminal to which this terminal is connected
iTerminal = self.cs_config[5] # Index of String descriptor describing input terminal
d = bytearray([
bDescriptorType,
bDescriptorSubtype,
bTerminalID,
wTerminalType & 0xff,
(wTerminalType >> 8) & 0xff,
bAssocTerminal,
bSourceID,
iTerminal
])
config_length = bytes ([len(d)+1])
d = config_length + d
elif self.cs_config[0] == 0x06 and self.usbclass == 1 and self.sub == 1 and self.proto == 0: # FEATURE_UNIT
bDescriptorType = 36 # CS_INTERFACE
bDescriptorSubtype = 0x06 # FEATURE_UNIT
bUnitID = self.cs_config[1]
bsourceID = self.cs_config[2]
bControlSize = self.cs_config[3]
bmaControls0 = self.cs_config[4]
bmaControls1 = self.cs_config[5]
bmaControls2 = self.cs_config[6]
iFeature = self.cs_config[7]
d = bytearray([
bDescriptorType,
bDescriptorSubtype,
bUnitID,
bsourceID,
bControlSize,
bmaControls0,
bmaControls1,
bmaControls2,
iFeature
])
config_length = bytes ([len(d)+1])
d = config_length + d
elif self.cs_config[0] == 0x01 and self.usbclass == 1 and self.sub == 2 and self.proto == 0: # AS_GENERAL
bDescriptorType = 36 # CS_INTERFACE
bDescriptorSubtype = 0x01 # AS_GENERAL
bTerminalLink = self.cs_config[1]
bDelay = self.cs_config[2]
wFormatTag = self.cs_config[3]
d = bytearray([
bDescriptorType,
bDescriptorSubtype,
bTerminalLink,
bDelay,
wFormatTag & 0xff,
(wFormatTag >> 8) & 0xff,
])
config_length = bytes ([len(d)+1])
d = config_length + d
elif self.cs_config[0] == 0x02 and self.usbclass == 1 and self.sub == 2 and self.proto == 0: # FORMAT_TYPE
bDescriptorType = 36 # CS_INTERFACE
bDescriptorSubtype = 0x02 # FORMAT_TYPE
bFormatType = self.cs_config[1]
bNrChannels = self.cs_config[2]
bSubFrameSize = self.cs_config[3]
bBitResolution = self.cs_config[4]
bSamFreqType = self.cs_config[5]
tSamFreq1 = self.cs_config[6]
tSamFreq2 = self.cs_config[7]
d = bytearray([
bDescriptorType,
bDescriptorSubtype,
bFormatType,
bNrChannels,
bSubFrameSize,
bBitResolution,
bSamFreqType,
(tSamFreq1 >> 16) & 0xff,
(tSamFreq1 >> 8) & 0xff,
tSamFreq1 & 0xff,
(tSamFreq2 >> 16) & 0xff,
(tSamFreq2 >> 8) & 0xff,
tSamFreq2 & 0xff
])
config_length = bytes ([len(d)+1])
d = config_length + d
############################# end Audio class ##########################################
return d