3 #FTDI USB Device Emulator
10 from GoodFETMAXUSB import *;
12 class GoodFETMAXUSBFTDI(GoodFETMAXUSB):
13 """This emulates the FTDI USB to Serial chips."""
15 """Initialize a USB FTDI device."""
16 self.usb_disconnect();
21 """Main loop of the USB FTDI emulator."""
22 print "Starting a FTDI device. This won't return.";
26 """Handle USB Enumeration"""
28 #Grab the SETUP packet from the buffer.
29 SUD=self.readbytes(rSUDFIFO,8);
31 #Parse the SETUP packet
32 print "Handling a setup packet of %s" % self.setup2str(SUD);
33 setuptype=(ord(SUD[bmRequestType])&0x60);
35 self.std_request(SUD);
37 self.class_request(SUD);
39 self.vendor_request(SUD);
41 print "Unknown request type 0x%02x." % ord(SUD[bmRequestType])
43 def ftdi_request(self,SUD):
44 """Handle an FTDI request."""
46 def class_request(self,SUD):
47 """Handle a class request."""
48 print "Stalling a class request.";
50 def vendor_request(self,SUD):
51 """Handle an FTDI vendor request."""
52 request=ord(SUD[bRequest]);
56 elif request==1: #modem_ctrl
57 valuel=ord(SUD[wValueL])
58 valueh=ord(SUD[wValueH]);
64 if dtren: print "DTR is enabled, value %i" % dtr;
65 if rtsen: print "RTS is enabled, value %i" % rts;
68 elif request==2: #set_flow_ctrl
69 indexh=ord(SUD[wIndexH]);
70 indexl=ord(SUD[wIndexL]);
72 print "SET_FLOW_CTRL to no handshaking.";
74 print "SET_FLOW_CTRL for RTS/CTS handshaking.";
76 print "SET_FLOW_CTRL for DTR/DSR handshaking.";
78 print "SET_FLOW_CTRL for XON/XOFF handshaking.";
81 elif request==3: #set_baud_rate
82 print "Baud rate set to %i." % ord(SUD[wValueL]);
84 elif request==4: #set_data
86 elif request==5: #get_status
87 print "I don't know how to send the status.";
89 elif request==6: #set_event_char
91 elif request==7: #set_error_char
93 elif request==9: #set_latency_timer
94 print "Expected to set latency timer to 0x%02x." % ord(SUD[wValueL]);
96 elif request==0x0a: #get_latency_timer
97 print "Bullshitting a value for the latency timer."
98 #Send some sort of reply.
99 self.wreg(rEP0FIFO,0x01);
100 self.wreg(rEP0FIFO,0x00);
101 self.wregAS(rEP0BC,2);
102 #Don't send reply twice.
106 print "Blindly accepting vendor request";
107 #self.wreg(rEP0FIFO,0);
108 self.wregAS(rEP0BC,0);
109 def std_request(self,SUD):
110 """Handles a standard setup request."""
111 setuptype=ord(SUD[bRequest]);
112 if setuptype==SR_GET_DESCRIPTOR: self.send_descriptor(SUD);
113 elif setuptype==SR_SET_FEATURE: self.feature(1);
114 elif setuptype==SR_SET_CONFIGURATION: self.set_configuration(SUD);
115 elif setuptype==SR_GET_STATUS: self.get_status(SUD);
116 elif setuptype==SR_SET_ADDRESS: self.rregAS(rFNADDR);
117 elif setuptype==SR_GET_INTERFACE: self.get_interface(SUD);
119 print "Stalling Unknown standard setup request type %02x" % setuptype;
122 def get_interface(self,SUD):
123 """Handles a setup request for SR_GET_INTERFACE."""
124 if ord(SUD[wIndexL]==0):
125 self.wreg(rEP0FIFO,0);
126 self.wregAS(rEP0BC,1);
134 DD=[0x12, # bLength = 18d
135 0x01, # bDescriptorType = Device (1)
136 0x00,0x01, # bcdUSB(L/H) USB spec rev (BCD)
137 0x00,0x00,0x00, # bDeviceClass, bDeviceSubClass, bDeviceProtocol
138 0x40, # bMaxPacketSize0 EP0 is 64 bytes
139 0x03,0x04, # idVendor(L/H)--FTDI is 0403
140 0x01,0x60, # idProduct(L/H)--6001
141 0x34,0x12, # bcdDevice--1234
142 1,2,3, # iManufacturer, iProduct, iSerialNumber
144 #Configuration Descriptor
146 0x02, # bDescriptorType = Config
147 0x20,0x00, # wTotalLength(L/H) = 34 bytes (0x22)
148 0x01, # bNumInterfaces
150 0x00, # iConfiguration
151 0xE0, # bmAttributes. b7=1 b6=self-powered b5=RWU supported
152 0x01, # MaxPower is 2 ma
153 # INTERFACE Descriptor
157 0x00, # bAlternate Setting
158 0x02, # bNum Endpoints
159 0xFF, # bInterfaceClass = FF=vendor
160 0xFF,0xFF, # bInterfaceSubClass, bInterfaceProtocol
162 # HID Descriptor--It's at CD[18]
164 # 0x21, # bDescriptorType = HID
165 # 0x10,0x01, # bcdHID(L/H) Rev 1.1
166 # 0x00, # bCountryCode (none)
167 # 0x01, # bNumDescriptors (one report descriptor)
168 # 0x22, # bDescriptorType (report)
169 # 43,0, # CD[25]: wDescriptorLength(L/H) (report descriptor size is 43 bytes)
170 # Endpoint Descriptor
172 0x05, # bDescriptorType (Endpoint)
173 0x83, # bEndpointAddress (EP3-IN)
174 0x02, # bmAttributes (interrupt)
175 64,0, # wMaxPacketSize (64)
177 # Endpoint Descriptor
179 0x05, # bDescriptorType (Endpoint)
180 0x01, # bEndpointAddress (EP1-OUT)
181 0x02, # bmAttributes (interrupt)
182 64,0, # wMaxPacketSize (64)
185 # STRING descriptor 0--Language string
189 # 0x03, # bDescriptorType = string
190 # 0x09,0x04 # wLANGID(L/H) = English-United Sates
192 # STRING descriptor 1--Manufacturer ID
193 "\x0c\x03M\x00a\x00x\x00i\x00m\x00",
196 # 0x03, # bDescriptorType = string
197 # 'M',0,'a',0,'x',0,'i',0,'m',0 # text in Unicode
199 # STRING descriptor 2 - Product ID
200 "\x18\x03M\x00A\x00X\x003\x004\x002\x000\x00E\x00 \x00E\x00n\x00u\x00m\x00 \x00C\x00o\x00d\x00e\x00",
202 # 0x03, # bDescriptorType = string
203 # 'M',0,'A',0,'X',0,'3',0,'4',0,'2',0,'0',0,'E',0,' ',0,
204 # 'E',0,'n',0,'u',0,'m',0,' ',0,'C',0,'o',0,'d',0,'e',0
208 # STRING descriptor 3 - Serial Number ID
209 "\x14\x03S\x00/\x00N\x00 \x003\x004\x002\x000\x00E\x00"
211 # 0x03, # bDescriptorType = string
224 0x05,0x01, # Usage Page (generic desktop)
225 0x09,0x06, # Usage (keyboard)
226 0xA1,0x01, # Collection
227 0x05,0x07, # Usage Page 7 (keyboard/keypad)
228 0x19,0xE0, # Usage Minimum = 224
229 0x29,0xE7, # Usage Maximum = 231
230 0x15,0x00, # Logical Minimum = 0
231 0x25,0x01, # Logical Maximum = 1
232 0x75,0x01, # Report Size = 1
233 0x95,0x08, # Report Count = 8
234 0x81,0x02, # Input(Data,Variable,Absolute)
235 0x95,0x01, # Report Count = 1
236 0x75,0x08, # Report Size = 8
237 0x81,0x01, # Input(Constant)
238 0x19,0x00, # Usage Minimum = 0
239 0x29,0x65, # Usage Maximum = 101
240 0x15,0x00, # Logical Minimum = 0,
241 0x25,0x65, # Logical Maximum = 101
242 0x75,0x08, # Report Size = 8
243 0x95,0x01, # Report Count = 1
244 0x81,0x00, # Input(Data,Variable,Array)
246 def send_descriptor(self,SUD):
247 """Send the USB descriptors based upon the setup data."""
249 reqlen=ord(SUD[wLengthL])+256*ord(SUD[wLengthH]); #16-bit length
250 desctype=ord(SUD[wValueH]);
252 if desctype==GD_DEVICE:
255 elif desctype==GD_CONFIGURATION:
258 elif desctype==GD_STRING:
259 desclen=self.strDesc[ord(SUD[wValueL])][0];
260 ddata=self.strDesc[ord(SUD[wValueL])];
261 elif desctype==GD_REPORT:
265 #TODO Configuration, String, Hid, and Report
268 sendlen=min(reqlen,desclen);
269 self.writebytes(rEP0FIFO,ddata);
270 self.wregAS(rEP0BC,sendlen);
272 print "Stalling in send_descriptor() for lack of handler for %02x." % desctype;
274 def set_configuration(self,SUD):
275 """Set the configuration."""
277 configval=ord(SUD[wValueL]);
279 self.SETBIT(rUSBIEN,bmSUSPIE);
280 self.rregAS(rFNADDR);
281 def get_status(self,SUD):
282 """Get the USB Setup Status."""
283 testbyte=ord(SUD[bmRequestType])
287 self.wreg(rEP0FIFO,0x03); #Enable RWU and self-powered
288 self.wreg(rEP0FIFO,0x00); #Second byte is always zero.
289 self.wregAS(rEP0BC,2); #Load byte count, arm transfer, and ack CTL.
292 self.wreg(rEP0FIFO,0x00);
293 self.wreg(rEP0FIFO,0x00); #Second byte is always zero.
294 self.wregAS(rEP0BC,2);
297 if(ord(SUD[wIndexL])==0x83):
298 self.wreg(rEP0FIFO,0x01); #Stall EP3
299 self.wreg(rEP0FIFO,0x00); #Second byte is always zero.
300 self.wregAS(rEP0BC,2);
305 def service_irqs(self):
306 """Handle USB interrupt events."""
308 epirq=self.rreg(rEPIRQ);
309 usbirq=self.rreg(rUSBIRQ);
311 #Are we being asked for setup data?
312 if(epirq&bmSUDAVIRQ): #Setup Data Requested
313 self.wreg(rEPIRQ,bmSUDAVIRQ); #Clear the bit
315 elif(epirq&bmIN3BAVIRQ): #EN3-IN packet
317 elif(epirq&bmOUT1DAVIRQ): #OUT1-OUT packet
318 self.wreg(rEPIRQ,bmOUT1DAVIRQ); #Clear the bit
324 typestring=" GoodFET emulating FTDI!";
327 def asc2hid(self,ascii):
328 """Translate ASCII to an USB keycode."""
330 if a>='a' and a<='z':
331 return ord(a)-ord('a')+4;
332 elif a>='A' and a<='Z':
333 return ord(a)-ord('A')+4;
339 """Type next letter in buffer."""
340 if self.typepos>=len(self.typestring):
342 elif self.typephase==0:
347 self.typeletter(self.typestring[self.typepos]);
348 self.typepos=self.typepos+1;
350 def typeletter(self,key):
351 """Type a letter on IN3. Zero for keyup."""
352 #if type(key)==str: key=ord(key);
354 self.wreg(rEP3INFIFO,0);
355 self.wreg(rEP3INFIFO,0);
356 self.wreg(rEP3INFIFO,self.asc2hid(key));
357 self.wreg(rEP3INBC,3);
359 """Handle IN3 input event."""
360 #Don't bother clearing interrupt flag, that's done by sending the reply.
361 #print "Got an input event, sending back some garbage that might be right.";
364 """Handle an OUT1 output event."""
365 print "Got an output event, printing the result.";
366 frame=self.readbytes(rEP1OUTFIFO,64);
367 #print "Got %s" % frame;
369 #Initialize FET and set baud rate
370 client=GoodFETMAXUSBFTDI();
374 client.MAXUSBsetup();