Working toward decrypting 15.4 packets in CCSPI.
[goodfet] / client / GoodFETMAXUSB.py
index a918810..b35ba51 100644 (file)
@@ -203,6 +203,7 @@ bmHXFRDNIRQ     =0x80
 
 class GoodFETMAXUSB(GoodFET):
     MAXUSBAPP=0x40;
+    usbverbose=True;
     
     def service_irqs(self):
         """Handle USB interrupt events."""
@@ -210,23 +211,19 @@ class GoodFETMAXUSB(GoodFET):
         usbirq=self.rreg(rUSBIRQ);
         
         
-        # Note *very* well that these interrupts are handled in order.
-        # You might need to alter the order or swap the elif
-        # statements for if statements.
-        
         #Are we being asked for setup data?
         if(epirq&bmSUDAVIRQ): #Setup Data Requested
             self.wreg(rEPIRQ,bmSUDAVIRQ); #Clear the bit
             self.do_SETUP();
         if(epirq&bmOUT1DAVIRQ): #OUT1-OUT packet
             self.do_OUT1();
-            self.wregAS(rEPIRQ,bmOUT1DAVIRQ); #Clear the bit *AFTER* servicing.
+            self.wreg(rEPIRQ,bmOUT1DAVIRQ); #Clear the bit *AFTER* servicing.
         if(epirq&bmIN3BAVIRQ): #IN3-IN packet
             self.do_IN3();
-            self.wreg(rEPIRQ,bmIN3BAVIRQ); #Clear the bit
+            #self.wreg(rEPIRQ,bmIN3BAVIRQ); #Clear the bit
         if(epirq&bmIN2BAVIRQ): #IN2 packet
             self.do_IN2();
-            self.wreg(rEPIRQ,bmIN2BAVIRQ); #Clear the bit
+            #self.wreg(rEPIRQ,bmIN2BAVIRQ); #Clear the bit
         #else:
         #    print "No idea how to service this IRQ: %02x" % epirq;
     def do_IN2(self):
@@ -235,7 +232,7 @@ class GoodFETMAXUSB(GoodFET):
         """Overload this."""
     def do_OUT1(self):
         """Overload this."""
-        print "Ignoring an OUT1 interrupt.";
+        if self.usbverbose: print "Ignoring an OUT1 interrupt.";
     def setup2str(self,SUD):
         """Converts the header of a setup packet to a string."""
         return "bmRequestType=0x%02x, bRequest=0x%02x, wValue=0x%04x, wIndex=0x%04x, wLength=0x%04x" % (
@@ -290,7 +287,7 @@ class GoodFETMAXUSB(GoodFET):
         ashex="";
         for foo in toret:
             ashex=ashex+(" %02x"%ord(foo));
-        print "GET %02x==%s" % (reg,ashex);
+        if self.usbverbose: print "GET   %02x==%s" % (reg,ashex);
         return toret;
     def readbytesAS(self,reg,length):
         """Peek some bytes from a register, acking prior transfer."""
@@ -300,7 +297,7 @@ class GoodFETMAXUSB(GoodFET):
         ashex="";
         for foo in toret:
             ashex=ashex+(" %02x"%ord(foo));
-        print "GET %02x==%s" % (reg,ashex);
+        if self.usbverbose: print "GETAS %02x==%s" % (reg,ashex);
         return toret;
     def ctl_write_nd(self,request):
         """Control Write with no data stage.  Assumes PERADDR is set
@@ -417,13 +414,13 @@ class GoodFETMAXUSB(GoodFET):
         data="";
         if type(tosend)==str:
             data=chr((reg<<3)|3)+tosend;
-            print "PUT %02x:=%s (0x%02x bytes)" % (reg,tosend,len(data))
+            if self.usbverbose: print "PUT %02x:=%s (0x%02x bytes)" % (reg,tosend,len(data))
         else:
             data=[(reg<<3)|3]+tosend;
             ashex="";
             for foo in tosend:
                 ashex=ashex+(" %02x"%foo);
-            print "PUT %02x:=%s (0x%02x bytes)" % (reg,ashex,len(data))
+            if self.usbverbose: print "PUT %02x:=%s (0x%02x bytes)" % (reg,ashex,len(data))
         self.writecmd(self.MAXUSBAPP,0x00,len(data),data);
     def usb_connect(self):
         """Connect the USB port."""
@@ -598,8 +595,48 @@ class GoodFETMAXUSBHost(GoodFETMAXUSB):
         for c in self.xfrdata[2:len(self.xfrdata)]:
             if c>0: toret=toret+chr(c);
         return toret;
+class GoodFETMAXUSBDevice(GoodFETMAXUSB):
+    
+    def send_descriptor(self,SUD):
+        """Send the USB descriptors based upon the setup data."""
+        desclen=0;
+        reqlen=ord(SUD[wLengthL])+256*ord(SUD[wLengthH]); #16-bit length
+        desctype=ord(SUD[wValueH]);
+        
+        if desctype==GD_DEVICE:
+            desclen=self.DD[0];
+            ddata=self.DD;
+        elif desctype==GD_CONFIGURATION:
+            desclen=self.CD[2];
+            ddata=self.CD;
+        elif desctype==GD_STRING:
+            desclen=ord(self.strDesc[ord(SUD[wValueL])][0]);
+            ddata=self.strDesc[ord(SUD[wValueL])];
+        elif desctype==GD_HID:
+            #Don't know how to do this yet.
+            pass;
+        elif desctype==GD_REPORT:
+            desclen=self.CD[25];
+            ddata=self.RepD;
+        #TODO Configuration, String, Hid, and Report
         
-class GoodFETMAXUSBHID(GoodFETMAXUSB):
+        if desclen>0:
+            #Reduce desclen if asked for fewer bytes.
+            desclen=min(reqlen,desclen);
+            #Send those bytes.
+            self.writebytes(rEP0FIFO,ddata[0:desclen]);
+            self.wregAS(rEP0BC,desclen);
+        else:
+            print "Stalling in send_descriptor() for lack of handler for %02x." % desctype;
+            self.STALL_EP0(SUD);
+    def set_configuration(self,SUD):
+        """Set the configuration."""
+        bmSUSPIE=0x10;
+        configval=ord(SUD[wValueL]);
+        if(configval>0):
+            self.SETBIT(rUSBIEN,bmSUSPIE);
+        self.rregAS(rFNADDR);
+class GoodFETMAXUSBHID(GoodFETMAXUSBDevice):
     """This is an example HID keyboard driver, loosely based on the
     MAX3420 examples."""
     def hidinit(self):
@@ -769,41 +806,7 @@ class GoodFETMAXUSBHID(GoodFETMAXUSB):
        0x95,0x01,              #   Report Count = 1
        0x81,0x00,              #  Input(Data,Variable,Array)
        0xC0]
-    def send_descriptor(self,SUD):
-        """Send the USB descriptors based upon the setup data."""
-        desclen=0;
-        reqlen=ord(SUD[wLengthL])+256*ord(SUD[wLengthH]); #16-bit length
-        desctype=ord(SUD[wValueH]);
-        
-        if desctype==GD_DEVICE:
-            desclen=self.DD[0];
-            ddata=self.DD;
-        elif desctype==GD_CONFIGURATION:
-            desclen=self.CD[2];
-            ddata=self.CD;
-        elif desctype==GD_STRING:
-            desclen=self.strDesc[ord(SUD[wValueL])][0];
-            ddata=self.strDesc[ord(SUD[wValueL])];
-        elif desctype==GD_REPORT:
-            desclen=self.CD[25];
-            ddata=self.RepD;
-        
-        #TODO Configuration, String, Hid, and Report
-        
-        if desclen>0:
-            sendlen=min(reqlen,desclen);
-            self.writebytes(rEP0FIFO,ddata);
-            self.wregAS(rEP0BC,sendlen);
-        else:
-            print "Stalling in send_descriptor() for lack of handler for %02x." % desctype;
-            self.STALL_EP0(SUD);
-    def set_configuration(self,SUD):
-        """Set the configuration."""
-        bmSUSPIE=0x10;
-        configval=ord(SUD[wValueL]);
-        if(configval>0):
-            self.SETBIT(rUSBIEN,bmSUSPIE);
-        self.rregAS(rFNADDR);
+
     def get_status(self,SUD):
         """Get the USB Setup Status."""
         testbyte=ord(SUD[bmRequestType])