From 85180b93353582dd0cb8c344b6170192c858f86d Mon Sep 17 00:00:00 2001 From: pete-cs Date: Sun, 16 Jun 2013 00:12:56 +0000 Subject: [PATCH] fixed failure to construct full config descriptor; handle linux's inability to deal with correctly-encoded utf-16 strings; handle previously-unhandled requests sent by linux host git-svn-id: https://svn.code.sf.net/p/goodfet/code/trunk@1599 12e2690d-a6be-4b82-a7b7-67c4a43b65c8 --- client/USBConfiguration.py | 11 ++++++++--- client/USBDevice.py | 15 ++++++++++++--- client/USBInterface.py | 6 +++++- client/USBKeyboard.py | 2 +- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/client/USBConfiguration.py b/client/USBConfiguration.py index cadf106..98a3c99 100644 --- a/client/USBConfiguration.py +++ b/client/USBConfiguration.py @@ -9,6 +9,9 @@ class USBConfiguration: self.configuration_string_index = 0 self.interfaces = interfaces + self.attributes = 0xe0 + self.max_power = 0x01 + def set_configuration_string_index(self, i): self.configuration_string_index = i @@ -17,16 +20,18 @@ class USBConfiguration: for i in self.interfaces: interface_descriptors += i.get_descriptor() - total_len = len(interface_descriptors) + 7 + total_len = len(interface_descriptors) + 9 d = bytes([ - 7, # length of descriptor in bytes + 9, # length of descriptor in bytes 2, # descriptor type 2 == configuration total_len & 0xff, (total_len >> 8) & 0xff, len(self.interfaces), self.configuration_index, - self.configuration_string_index + self.configuration_string_index, + self.attributes, + self.max_power ]) return d + interface_descriptors diff --git a/client/USBDevice.py b/client/USBDevice.py index 84e8b1f..e25fea8 100644 --- a/client/USBDevice.py +++ b/client/USBDevice.py @@ -133,7 +133,11 @@ class USBDevice: elif req.get_recipient() == USB.request_recipient_interface: recipient = self.configuration.interfaces[req.index] elif req.get_recipient() == USB.request_recipient_endpoint: - recipient = self.configuration.endpoints[req.index] + try: + recipient = self.endpoints[req.index] + except KeyError: + self.maxusb_app.stall_ep0() + return # and then the type if req.get_type() == USB.request_type_standard: @@ -148,12 +152,12 @@ class USBDevice: self.maxusb_app.stall_ep0() def handle_data_available(self, ep_num, data): - if self.ready and ep_num in self.endpoints: + if self.state == USB.state_configured and ep_num in self.endpoints: endpoint = self.endpoints[ep_num] endpoint.handler(data) def handle_buffer_available(self, ep_num): - if self.ready and ep_num in self.endpoints: + if self.state == USB.state_configured and ep_num in self.endpoints: endpoint = self.endpoints[ep_num] endpoint.handler() @@ -230,6 +234,11 @@ class USBDevice: else: # string descriptors start at 1 s = self.strings[num-1].encode('utf-16') + + # Linux doesn't like the leading 2-byte Byte Order Mark (BOM); + # FreeBSD is okay without it + s = s[2:] + d = bytearray([ len(s) + 2, # length of descriptor in bytes 3 # descriptor type 3 == string diff --git a/client/USBInterface.py b/client/USBInterface.py index 0cedd6b..449b557 100644 --- a/client/USBInterface.py +++ b/client/USBInterface.py @@ -26,7 +26,8 @@ class USBInterface: self.descriptors[USB.desc_type_interface] = self.get_descriptor self.request_handlers = { - 6 : self.handle_get_descriptor_request + 6 : self.handle_get_descriptor_request, + 11 : self.handle_set_interface_request } # USB 2.0 specification, section 9.4.3 (p 281 of pdf) @@ -56,6 +57,9 @@ class USBInterface: if self.verbose > 5: print(self.name, "sent", n, "bytes in response") + def handle_set_interface_request(self, req): + self.device.maxusb_app.stall_ep0() + # Table 9-12 of USB 2.0 spec (pdf page 296) def get_descriptor(self): diff --git a/client/USBKeyboard.py b/client/USBKeyboard.py index 64f6ec0..6e459e4 100644 --- a/client/USBKeyboard.py +++ b/client/USBKeyboard.py @@ -70,7 +70,7 @@ class USBKeyboardDevice(USBDevice): def __init__(self, maxusb_app, verbose=0): config = USBConfiguration( 1, # index - "Maxim Emulated Keyboard Configuration", # string desc + "Emulated Keyboard", # string desc [ USBKeyboardInterface() ] # interfaces ) -- 2.20.1