3 #USB Mass Storage Emulator
5 #with thanks to Brandon Wilson and his Linky project.
12 from GoodFETMAXUSB import *;
14 class GoodFETMAXUSBMass(GoodFETMAXUSB):
15 """This emulates a USB Mass Storage device."""
17 """Initialize a USB Mass Storage device."""
18 self.usb_disconnect();
24 """Main loop of the USB Mass Storage emulator."""
25 print "Starting a Mass Storage device. This doesn't work yet.";
29 """Handle USB Enumeration"""
31 #Grab the SETUP packet from the buffer.
32 SUD=self.readbytes(rSUDFIFO,8);
34 #Parse the SETUP packet
35 print "Handling a setup packet of %s" % self.setup2str(SUD);
36 setuptype=(ord(SUD[bmRequestType])&0x60);
38 self.std_request(SUD);
40 print "XXXXXXXXXX Got a class request."
41 self.class_request(SUD);
43 self.vendor_request(SUD);
45 print "Unknown bmRequestType=0x%02x." % ord(SUD[bmRequestType])
47 def class_request(self,SUD):
48 """Handle a class request."""
49 print "Stalling a class request.";
51 def vendor_request(self,SUD):
52 """Handle a vendor request."""
53 request=ord(SUD[bRequest]);
54 print "Why the hell is there a vendor request?";
55 #self.wreg(rEP0FIFO,0);
56 self.wregAS(rEP0BC,0);
57 def std_request(self,SUD):
58 """Handles a standard setup request."""
59 setuptype=ord(SUD[bRequest]);
60 if setuptype==SR_GET_DESCRIPTOR: self.send_descriptor(SUD);
61 #elif setuptype==SR_SET_FEATURE: self.feature(1);
62 elif setuptype==SR_SET_CONFIGURATION: self.set_configuration(SUD);
63 elif setuptype==SR_GET_STATUS: self.get_status(SUD);
64 elif setuptype==SR_SET_ADDRESS: self.rregAS(rFNADDR);
65 elif setuptype==SR_GET_INTERFACE: self.get_interface(SUD);
67 print "Stalling Unknown standard setup request type %02x" % setuptype;
70 def get_interface(self,SUD):
71 """Handles a setup request for SR_GET_INTERFACE."""
72 if ord(SUD[wIndexL]==0):
73 self.wreg(rEP0FIFO,0);
74 self.wregAS(rEP0BC,1);
85 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
87 0x50, 0x51, #SDCZ2 Cruzer Mini Flash Drive (thin)
88 0x00, 0x03, 0x00, 0x00, 0x00, 0x01
92 #Configuration Descriptor
98 0x00, 0x01, 0x01, 0x00, 0xE0, 0x00, 0x09, 0x04, 0x00, 0x00,
100 0x08, #Mass Storage Bulk Only
105 0x07, 0x05, 0x83, 0x02, 0x40, 0x00, 0x00,
107 0x07, 0x05, 0x01, 0x02, 0x40, 0x00, 0x00
111 # STRING descriptor 0--Language string
115 # 0x03, # bDescriptorType = string
116 # 0x09,0x04 # wLANGID(L/H) = English-United Sates
118 # STRING descriptor 1--Manufacturer ID
119 "\x10\x03G\x00o\x00o\x00d\x00F\x00E\x00T\x00",
120 # STRING descriptor 2 - Product ID
121 "\x1C\x03M\x00A\x00S\x00S\x00 \x00E\x00m\x00u\x00l\x00a\x00t\x00o\x00r\x00",
122 # STRING descriptor 3 - Serial Number ID
123 "\x14\x03S\x00/\x00N\x00 \x003\x004\x002\x000\x00E\x00"
126 def send_descriptor(self,SUD):
127 """Send the USB descriptors based upon the setup data."""
129 reqlen=ord(SUD[wLengthL])+256*ord(SUD[wLengthH]); #16-bit length
130 desctype=ord(SUD[wValueH]);
132 if desctype==GD_DEVICE:
135 elif desctype==GD_CONFIGURATION:
138 elif desctype==GD_STRING:
139 desclen=self.strDesc[ord(SUD[wValueL])][0];
140 ddata=self.strDesc[ord(SUD[wValueL])];
142 #TODO Configuration, String, Hid, and Report
145 sendlen=min(reqlen,desclen);
146 self.writebytes(rEP0FIFO,ddata);
147 self.wregAS(rEP0BC,sendlen);
149 print "Stalling in send_descriptor() for lack of handler for %02x." % desctype;
151 def set_configuration(self,SUD):
152 """Set the configuration."""
154 configval=ord(SUD[wValueL]);
156 self.SETBIT(rUSBIEN,bmSUSPIE);
157 self.rregAS(rFNADDR);
158 def get_status(self,SUD):
159 """Get the USB Setup Status."""
160 testbyte=ord(SUD[bmRequestType])
164 self.wreg(rEP0FIFO,0x03); #Enable RWU and self-powered
165 self.wreg(rEP0FIFO,0x00); #Second byte is always zero.
166 self.wregAS(rEP0BC,2); #Load byte count, arm transfer, and ack CTL.
169 self.wreg(rEP0FIFO,0x00);
170 self.wreg(rEP0FIFO,0x00); #Second byte is always zero.
171 self.wregAS(rEP0BC,2);
174 if(ord(SUD[wIndexL])==0x83):
175 print "This code almost certainly doesn't work.";
176 self.wreg(rEP0FIFO,0x01); #Stall EP3
177 self.wreg(rEP0FIFO,0x00); #Second byte is always zero.
178 self.wregAS(rEP0BC,2);
183 def service_irqs(self):
184 """Handle USB interrupt events."""
186 epirq=self.rreg(rEPIRQ);
187 usbirq=self.rreg(rUSBIRQ);
189 #Are we being asked for setup data?
190 if(epirq&bmSUDAVIRQ): #Setup Data Requested
191 self.wreg(rEPIRQ,bmSUDAVIRQ); #Clear the bit
193 elif(epirq&bmIN3BAVIRQ): #EN3-IN packet
195 #self.wreg(rEPIRQ,bmIN3BAVIRQ); #Clear the bit
196 elif(epirq&bmOUT1DAVIRQ): #OUT1-OUT packet
198 self.wregAS(rEPIRQ,bmOUT1DAVIRQ); #Clear the bit *AFTER* servicing.
202 typestring="GoodFET emulates Mass properly, if you can read this!\n";
206 """Handle IN3 input event."""
207 #Don't bother clearing interrupt flag, that's done by sending the reply.
209 print "Got an input event, no idea what to do about it.";
210 self.wreg(rEP3INFIFO,0x01); #Modem Status
211 self.wreg(rEP3INFIFO,0x00); #Line Status
212 self.wreg(rEP3INFIFO,0x00);
213 self.wregAS(rEP3INBC,3);
215 """Handle an OUT1 output event."""
216 print "Got an output event, printing the result.";
217 l=self.rreg(rEP1OUTBC);
218 frame=self.readbytesAS(rEP1OUTFIFO,l);
219 print "Mass OUT: %s" % frame[1:len(frame)];
222 #Initialize FET and set baud rate
223 client=GoodFETMAXUSBMass();
227 client.MAXUSBsetup();