fixed failure to construct full config descriptor; handle linux's inability to deal...
authorpete-cs <pete-cs@12e2690d-a6be-4b82-a7b7-67c4a43b65c8>
Sun, 16 Jun 2013 00:12:56 +0000 (00:12 +0000)
committerpete-cs <pete-cs@12e2690d-a6be-4b82-a7b7-67c4a43b65c8>
Sun, 16 Jun 2013 00:12:56 +0000 (00:12 +0000)
git-svn-id: https://svn.code.sf.net/p/goodfet/code/trunk@1599 12e2690d-a6be-4b82-a7b7-67c4a43b65c8

client/USBConfiguration.py
client/USBDevice.py
client/USBInterface.py
client/USBKeyboard.py

index cadf106..98a3c99 100644 (file)
@@ -9,6 +9,9 @@ class USBConfiguration:
         self.configuration_string_index = 0
         self.interfaces                 = interfaces
 
         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
 
     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()
 
         for i in self.interfaces:
             interface_descriptors += i.get_descriptor()
 
-        total_len = len(interface_descriptors) + 7
+        total_len = len(interface_descriptors) + 9
 
         d = bytes([
 
         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,
                 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
         ])
 
         return d + interface_descriptors
index 84e8b1f..e25fea8 100644 (file)
@@ -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:
         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:
 
         # 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):
             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):
             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()
     
             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')
         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
             d = bytearray([
                     len(s) + 2,     # length of descriptor in bytes
                     3               # descriptor type 3 == string
index 0cedd6b..449b557 100644 (file)
@@ -26,7 +26,8 @@ class USBInterface:
         self.descriptors[USB.desc_type_interface] = self.get_descriptor
 
         self.request_handlers = {
         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)
         }
 
     # 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")
 
             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):
 
     # Table 9-12 of USB 2.0 spec (pdf page 296)
     def get_descriptor(self):
 
index 64f6ec0..6e459e4 100644 (file)
@@ -70,7 +70,7 @@ class USBKeyboardDevice(USBDevice):
     def __init__(self, maxusb_app, verbose=0):
         config = USBConfiguration(
                 1,                                          # index
     def __init__(self, maxusb_app, verbose=0):
         config = USBConfiguration(
                 1,                                          # index
-                "Maxim Emulated Keyboard Configuration",    # string desc
+                "Emulated Keyboard",    # string desc
                 [ USBKeyboardInterface() ]                  # interfaces
         )
 
                 [ USBKeyboardInterface() ]                  # interfaces
         )