3 # Contains class definition for USBInterface.
8 name = "generic USB interface"
10 def __init__(self, interface_number, interface_alternate, interface_class,
11 interface_subclass, interface_protocol, interface_string_index,
12 verbose=0, endpoints=[], descriptors={}):
14 self.number = interface_number
15 self.alternate = interface_alternate
16 self.iclass = interface_class
17 self.subclass = interface_subclass
18 self.protocol = interface_protocol
19 self.string_index = interface_string_index
21 self.endpoints = endpoints
22 self.descriptors = descriptors
24 self.verbose = verbose
26 self.descriptors[USB.desc_type_interface] = self.get_descriptor
28 self.request_handlers = {
29 6 : self.handle_get_descriptor_request,
30 11 : self.handle_set_interface_request
33 self.configuration = None
35 for e in self.endpoints:
38 self.device_class = None
40 def set_configuration(self, config):
41 self.configuration = config
43 # USB 2.0 specification, section 9.4.3 (p 281 of pdf)
44 # HACK: blatant copypasta from USBDevice pains me deeply
45 def handle_get_descriptor_request(self, req):
46 dtype = (req.value >> 8) & 0xff
47 dindex = req.value & 0xff
54 print(self.name, ("received GET_DESCRIPTOR req %d, index %d, " \
55 + "language 0x%04x, length %d") \
56 % (dtype, dindex, lang, n))
58 # TODO: handle KeyError
59 response = self.descriptors[dtype]
60 if callable(response):
61 response = response(dindex)
64 n = min(n, len(response))
65 self.configuration.device.maxusb_app.send_on_endpoint(0, response[:n])
68 print(self.name, "sent", n, "bytes in response")
70 def handle_set_interface_request(self, req):
71 self.configuration.device.maxusb_app.stall_ep0()
73 # Table 9-12 of USB 2.0 spec (pdf page 296)
74 def get_descriptor(self):
77 9, # length of descriptor in bytes
78 4, # descriptor type 4 == interface
89 iclass_desc_num = USB.interface_class_to_descriptor_type(self.iclass)
91 d += self.descriptors[iclass_desc_num]
93 for e in self.endpoints:
94 d += e.get_descriptor()