turn ftdi driver into echo server
[goodfet] / client / GoodFETMAXUSB.py
1 #!/usr/bin/env python
2 # GoodFET Client Library for Maxim USB Chips.
3
4 # (C) 2012 Travis Goodspeed <travis at radiantmachines.com>
5 #
6 # This code is being rewritten and refactored.  You've been warned!
7
8
9 import sys, time, string, cStringIO, struct, glob, os;
10 import warnings
11
12 from GoodFET import GoodFET;
13
14 warnings.warn(
15 """This library will soon be deprecated in favor of the USB*.py libraries."""
16 )
17
18 #Handy registers.
19 rEP0FIFO=0
20 rEP1OUTFIFO=1
21 rEP2INFIFO=2
22 rEP3INFIFO=3
23 rSUDFIFO=4
24 rEP0BC=5
25 rEP1OUTBC=6
26 rEP2INBC=7
27 rEP3INBC=8
28 rEPSTALLS=9
29 rCLRTOGS=10
30 rEPIRQ=11
31 rEPIEN=12
32 rUSBIRQ=13
33 rUSBIEN=14
34 rUSBCTL=15
35 rCPUCTL=16
36 rPINCTL=17
37 rREVISION=18
38 rFNADDR=19
39 rIOPINS=20
40 rIOPINS1=20  #Same as rIOPINS
41 rIOPINS2=21
42 rHIRQ=25
43 rHIEN=26
44 rMODE=27
45 rPERADDR=28
46 rHCTL=29
47 rHXFR=30
48 rHRSL=31
49
50 #Host mode registers.
51 rRCVFIFO =1
52 rSNDFIFO =2
53 rRCVBC   =6
54 rSNDBC   =7
55 rHIRQ    =25
56
57
58 # R11 EPIRQ register bits
59 bmSUDAVIRQ =0x20
60 bmIN3BAVIRQ =0x10
61 bmIN2BAVIRQ =0x08
62 bmOUT1DAVIRQ= 0x04
63 bmOUT0DAVIRQ= 0x02
64 bmIN0BAVIRQ =0x01
65
66 # R12 EPIEN register bits
67 bmSUDAVIE   =0x20
68 bmIN3BAVIE  =0x10
69 bmIN2BAVIE  =0x08
70 bmOUT1DAVIE =0x04
71 bmOUT0DAVIE =0x02
72 bmIN0BAVIE  =0x01
73
74
75
76
77 # ************************
78 # Standard USB Requests
79 SR_GET_STATUS           =0x00   # Get Status
80 SR_CLEAR_FEATURE        =0x01   # Clear Feature
81 SR_RESERVED             =0x02   # Reserved
82 SR_SET_FEATURE          =0x03   # Set Feature
83 SR_SET_ADDRESS          =0x05   # Set Address
84 SR_GET_DESCRIPTOR       =0x06   # Get Descriptor
85 SR_SET_DESCRIPTOR       =0x07   # Set Descriptor
86 SR_GET_CONFIGURATION    =0x08   # Get Configuration
87 SR_SET_CONFIGURATION    =0x09   # Set Configuration
88 SR_GET_INTERFACE        =0x0a   # Get Interface
89 SR_SET_INTERFACE        =0x0b   # Set Interface
90
91 # Get Descriptor codes  
92 GD_DEVICE               =0x01   # Get device descriptor: Device
93 GD_CONFIGURATION        =0x02   # Get device descriptor: Configuration
94 GD_STRING               =0x03   # Get device descriptor: String
95 GD_HID                  =0x21   # Get descriptor: HID
96 GD_REPORT               =0x22   # Get descriptor: Report
97
98 # SETUP packet header offsets
99 bmRequestType           =0
100 bRequest                =1
101 wValueL                 =2
102 wValueH                 =3
103 wIndexL                 =4
104 wIndexH                 =5
105 wLengthL                =6
106 wLengthH                =7
107
108 # HID bRequest values
109 GET_REPORT              =1
110 GET_IDLE                =2
111 GET_PROTOCOL            =3
112 SET_REPORT              =9
113 SET_IDLE                =0x0A
114 SET_PROTOCOL            =0x0B
115 INPUT_REPORT            =1
116
117 # PINCTL bits
118 bmEP3INAK   =0x80
119 bmEP2INAK   =0x40
120 bmEP1INAK   =0x20
121 bmFDUPSPI   =0x10
122 bmINTLEVEL  =0x08
123 bmPOSINT    =0x04
124 bmGPXB      =0x02
125 bmGPXA      =0x01
126
127 # rUSBCTL bits
128 bmHOSCSTEN  =0x80
129 bmVBGATE    =0x40
130 bmCHIPRES   =0x20
131 bmPWRDOWN   =0x10
132 bmCONNECT   =0x08
133 bmSIGRWU    =0x04
134
135 # USBIRQ bits
136 bmURESDNIRQ =0x80
137 bmVBUSIRQ   =0x40
138 bmNOVBUSIRQ =0x20
139 bmSUSPIRQ   =0x10
140 bmURESIRQ   =0x08
141 bmBUSACTIRQ =0x04
142 bmRWUDNIRQ  =0x02
143 bmOSCOKIRQ  =0x01
144
145 # MODE bits
146 bmHOST          =0x01
147 bmLOWSPEED      =0x02
148 bmHUBPRE        =0x04
149 bmSOFKAENAB     =0x08
150 bmSEPIRQ        =0x10
151 bmDELAYISO      =0x20
152 bmDMPULLDN      =0x40
153 bmDPPULLDN      =0x80
154
155 # PERADDR/HCTL bits
156 bmBUSRST        =0x01
157 bmFRMRST        =0x02
158 bmSAMPLEBUS     =0x04
159 bmSIGRSM        =0x08
160 bmRCVTOG0       =0x10
161 bmRCVTOG1       =0x20
162 bmSNDTOG0       =0x40
163 bmSNDTOG1       =0x80
164
165 # rHXFR bits
166 # Host XFR token values for writing the HXFR register (R30).
167 # OR this bit field with the endpoint number in bits 3:0
168 tokSETUP  =0x10  # HS=0, ISO=0, OUTNIN=0, SETUP=1
169 tokIN     =0x00  # HS=0, ISO=0, OUTNIN=0, SETUP=0
170 tokOUT    =0x20  # HS=0, ISO=0, OUTNIN=1, SETUP=0
171 tokINHS   =0x80  # HS=1, ISO=0, OUTNIN=0, SETUP=0
172 tokOUTHS  =0xA0  # HS=1, ISO=0, OUTNIN=1, SETUP=0 
173 tokISOIN  =0x40  # HS=0, ISO=1, OUTNIN=0, SETUP=0
174 tokISOOUT =0x60  # HS=0, ISO=1, OUTNIN=1, SETUP=0
175
176 # rRSL bits
177 bmRCVTOGRD   =0x10
178 bmSNDTOGRD   =0x20
179 bmKSTATUS    =0x40
180 bmJSTATUS    =0x80
181 # Host error result codes, the 4 LSB's in the HRSL register.
182 hrSUCCESS   =0x00
183 hrBUSY      =0x01
184 hrBADREQ    =0x02
185 hrUNDEF     =0x03
186 hrNAK       =0x04
187 hrSTALL     =0x05
188 hrTOGERR    =0x06
189 hrWRONGPID  =0x07
190 hrBADBC     =0x08
191 hrPIDERR    =0x09
192 hrPKTERR    =0x0A
193 hrCRCERR    =0x0B
194 hrKERR      =0x0C
195 hrJERR      =0x0D
196 hrTIMEOUT   =0x0E
197 hrBABBLE    =0x0F
198
199 # HIRQ bits
200 bmBUSEVENTIRQ   =0x01   # indicates BUS Reset Done or BUS Resume     
201 bmRWUIRQ        =0x02
202 bmRCVDAVIRQ     =0x04
203 bmSNDBAVIRQ     =0x08
204 bmSUSDNIRQ      =0x10
205 bmCONDETIRQ     =0x20
206 bmFRAMEIRQ      =0x40
207 bmHXFRDNIRQ     =0x80
208
209 class GoodFETMAXUSB(GoodFET):
210     MAXUSBAPP=0x40;
211     usbverbose=False;
212     
213     def service_irqs(self):
214         """Handle USB interrupt events."""
215         epirq=self.rreg(rEPIRQ);
216         usbirq=self.rreg(rUSBIRQ);
217         
218         
219         #Are we being asked for setup data?
220         if(epirq&bmSUDAVIRQ): #Setup Data Requested
221             self.wreg(rEPIRQ,bmSUDAVIRQ); #Clear the bit
222             self.do_SETUP();
223         if(epirq&bmOUT1DAVIRQ): #OUT1-OUT packet
224             self.do_OUT1();
225             self.wreg(rEPIRQ,bmOUT1DAVIRQ); #Clear the bit *AFTER* servicing.
226         if(epirq&bmIN3BAVIRQ): #IN3-IN packet
227             self.do_IN3();
228             #self.wreg(rEPIRQ,bmIN3BAVIRQ); #Clear the bit
229         if(epirq&bmIN2BAVIRQ): #IN2 packet
230             self.do_IN2();
231             #self.wreg(rEPIRQ,bmIN2BAVIRQ); #Clear the bit
232         #else:
233         #    print "No idea how to service this IRQ: %02x" % epirq;
234     def do_IN2(self):
235         """Overload this."""
236     def do_IN3(self):
237         """Overload this."""
238     def do_OUT1(self):
239         """Overload this."""
240         if self.usbverbose: print "Ignoring an OUT1 interrupt.";
241     def setup2str(self,SUD):
242         """Converts the header of a setup packet to a string."""
243         return "bmRequestType=0x%02x, bRequest=0x%02x, wValue=0x%04x, wIndex=0x%04x, wLength=0x%04x" % (
244                 ord(SUD[0]), ord(SUD[1]),
245                 ord(SUD[2])+(ord(SUD[3])<<8),
246                 ord(SUD[4])+(ord(SUD[5])<<8),
247                 ord(SUD[6])+(ord(SUD[7])<<8)
248                 );
249     
250     def MAXUSBsetup(self):
251         """Move the FET into the MAXUSB application."""
252         self.writecmd(self.MAXUSBAPP,0x10,0,self.data); #MAXUSB/SETUP
253         self.writecmd(self.MAXUSBAPP,0x10,0,self.data); #MAXUSB/SETUP
254         self.writecmd(self.MAXUSBAPP,0x10,0,self.data); #MAXUSB/SETUP
255         print "Connected to MAX342x Rev. %x" % (self.rreg(rREVISION));
256         self.wreg(rPINCTL,0x18); #Set duplex and negative INT level.
257         
258     def MAXUSBtrans8(self,byte):
259         """Read and write 8 bits by MAXUSB."""
260         data=self.MAXUSBtrans([byte]);
261         return ord(data[0]);
262     
263     def MAXUSBtrans(self,data):
264         """Exchange data by MAXUSB."""
265         self.data=data;
266         self.writecmd(self.MAXUSBAPP,0x00,len(data),data);
267         return self.data;
268
269     def rreg(self,reg):
270         """Peek 8 bits from a register."""
271         data=[reg<<3,0];
272         self.writecmd(self.MAXUSBAPP,0x00,len(data),data);
273         return ord(self.data[1]);
274     def rregAS(self,reg):
275         """Peek 8 bits from a register, setting AS."""
276         data=[(reg<<3)|1,0];
277         self.writecmd(self.MAXUSBAPP,0x00,len(data),data);
278         return ord(self.data[1]);
279     def wreg(self,reg,value):
280         """Poke 8 bits into a register."""
281         data=[(reg<<3)|2,value];
282         self.writecmd(self.MAXUSBAPP,0x00,len(data),data);        
283         return value;
284     def wregAS(self,reg,value):
285         """Poke 8 bits into a register, setting AS."""
286         data=[(reg<<3)|3,value];
287         self.writecmd(self.MAXUSBAPP,0x00,len(data),data);        
288         return value;
289     def readbytes(self,reg,length):
290         """Peek some bytes from a register."""
291         data=[(reg<<3)]+range(0,length);
292         self.writecmd(self.MAXUSBAPP,0x00,len(data),data);
293         toret=self.data[1:len(self.data)];
294         ashex="";
295         for foo in toret:
296             ashex=ashex+(" %02x"%ord(foo));
297         if self.usbverbose: print "GET   %02x==%s" % (reg,ashex);
298         return toret;
299     def readbytesAS(self,reg,length):
300         """Peek some bytes from a register, acking prior transfer."""
301         data=[(reg<<3)|1]+range(0,length);
302         self.writecmd(self.MAXUSBAPP,0x00,len(data),data);
303         toret=self.data[1:len(self.data)];
304         ashex="";
305         for foo in toret:
306             ashex=ashex+(" %02x"%ord(foo));
307         if self.usbverbose: print "GETAS %02x==%s" % (reg,ashex);
308         return toret;
309     def fifo_ep3in_tx(self,data):
310         """Sends the data out of EP3 in 64-byte chunks."""
311         #Wait for the buffer to be free before starting.
312         while not(self.rreg(rEPIRQ)&bmIN3BAVIRQ): pass;
313         
314         count=len(data);
315         pos=0;
316         while count>0:
317             #Send 64-byte chunks or the remainder.
318             c=min(count,64);
319             self.writebytes(rEP3INFIFO,
320                             data[pos:pos+c]);
321             self.wregAS(rEP3INBC,c);
322             count=count-c;
323             pos=pos+c;
324             
325             #Wait for the buffer to be free before continuing.
326             while not(self.rreg(rEPIRQ)&bmIN3BAVIRQ): pass;
327             
328         return;
329         
330     def ctl_write_nd(self,request):
331         """Control Write with no data stage.  Assumes PERADDR is set
332         and the SUDFIFO contains the 8 setup bytes.  Returns with
333         result code = HRSLT[3:0] (HRSL register).  If there is an
334         error, the 4MSBits of the returned value indicate the stage 1
335         or 2."""
336         
337         # 1. Send the SETUP token and 8 setup bytes. 
338         # Should ACK immediately.
339         self.writebytes(rSUDFIFO,request);
340         resultcode=self.send_packet(tokSETUP,0); #SETUP packet to EP0.
341         if resultcode: return resultcode;
342         
343         # 2. No data stage, so the last operation is to send an IN
344         # token to the peripheral as the STATUS (handhsake) stage of
345         # this control transfer.  We should get NAK or the DATA1 PID.
346         # When we get back to the DATA1 PID the 3421 automatically
347         # sends the closing NAK.
348         resultcode=self.send_packet(tokINHS,0); #Function takes care of retries.
349         if resultcode: return resultcode;
350         
351         return 0;
352         
353         
354     def ctl_read(self,request):
355         """Control read transfer, used in Host mode."""
356         resultcode=0;
357         bytes_to_read=request[6]+256*request[7];
358         
359         ##SETUP packet
360         self.writebytes(rSUDFIFO,request);     #Load the FIFO
361         resultcode=self.send_packet(tokSETUP,0); #SETUP packet to EP0
362         if resultcode:
363             print "Failed to get ACK on SETUP request in ctl_read()."
364             return resultcode;
365         
366         self.wreg(rHCTL,bmRCVTOG1);              #FIRST data packet in CTL transfer uses DATA1 toggle.
367         resultcode=self.IN_Transfer(0,bytes_to_read);
368         if resultcode:
369             print "Failed on IN Transfer in ctl_read()";
370             return resultcode;
371         
372         self.IN_nak_count=self.nak_count;
373         
374         #The OUT status stage.
375         resultcode=self.send_packet(tokOUTHS,0);
376         if resultcode:
377             print "Failed on OUT Status stage in ctl_read()";
378             return resultcode;
379         
380         return 0; #Success
381     
382     xfrdata=[]; #Ugly variable used only by a few functions.  FIXME
383     def IN_Transfer(self,endpoint,INbytes):
384         """Does an IN transfer to an endpoint, used for Host mode."""
385         xfrsize=INbytes;
386         xfrlen=0;
387         self.xfrdata=[];
388         
389         while 1:
390             resultcode=self.send_packet(tokIN,endpoint); #IN packet to EP. NAKS taken care of.
391             if resultcode: return resultcode;
392             
393             pktsize=self.rreg(rRCVBC); #Numer of RXed bytes.
394             
395             #Very innefficient, move this to C if performance is needed.
396             for j in range(0,pktsize):
397                 self.xfrdata=self.xfrdata+[self.rreg(rRCVFIFO)];
398             xfrsize=self.xfrdata[0];
399             self.wreg(rHIRQ,bmRCVDAVIRQ); #Clear IRQ
400             xfrlen=xfrlen+pktsize; #Add byte count to total transfer length.
401             
402             #print "%i / %i" % (xfrlen,xfrsize)
403             
404             #Packet is complete if:
405             # 1. The device sent a short packet, <maxPacketSize
406             # 2. INbytes have been transfered.
407             if (pktsize<self.maxPacketSize) or (xfrlen>=xfrsize):
408                 self.last_transfer_size=xfrlen;
409                 ashex="";
410                 for foo in self.xfrdata:
411                     ashex=ashex+(" %02x"%foo);
412                 #print "INPACKET EP%i==%s (0x%02x bytes remain)" % (endpoint,ashex,xfrsize);
413                 return resultcode;
414
415     RETRY_LIMIT=3;
416     NAK_LIMIT=300;
417     def send_packet(self,token,endpoint):
418         """Send a packet to an endpoint as the Host, taking care of NAKs.
419         Don't use this for device code."""
420         self.retry_count=0;
421         self.nak_count=0;
422         
423         #Repeat until NAK_LIMIT or RETRY_LIMIT is reached.
424         while self.nak_count<self.NAK_LIMIT and self.retry_count<self.RETRY_LIMIT:
425             self.wreg(rHXFR,(token|endpoint)); #launch the transfer
426             while not (self.rreg(rHIRQ) & bmHXFRDNIRQ):
427                 # wait for the completion IRQ
428                 pass;
429             self.wreg(rHIRQ,bmHXFRDNIRQ);           #Clear IRQ
430             resultcode = (self.rreg(rHRSL) & 0x0F); # get the result
431             if (resultcode==hrNAK):
432                 self.nak_count=self.nak_count+1;
433             elif (resultcode==hrTIMEOUT):
434                 self.retry_count=self.retry_count+1;
435             else:
436                 #Success!
437                 return resultcode;
438         return resultcode;
439             
440     def writebytes(self,reg,tosend):
441         """Poke some bytes into a register."""
442         data="";
443         if type(tosend)==str:
444             data=chr((reg<<3)|3)+tosend;
445             if self.usbverbose: print "PUT %02x:=%s (0x%02x bytes)" % (reg,tosend,len(data))
446         else:
447             data=[(reg<<3)|3]+tosend;
448             ashex="";
449             for foo in tosend:
450                 ashex=ashex+(" %02x"%foo);
451             if self.usbverbose: print "PUT %02x:=%s (0x%02x bytes)" % (reg,ashex,len(data))
452         self.writecmd(self.MAXUSBAPP,0x00,len(data),data);
453     def usb_connect(self):
454         """Connect the USB port."""
455         
456         #disconnect D+ pullup if host turns off VBUS
457         self.wreg(rUSBCTL,bmVBGATE|bmCONNECT);
458     def usb_disconnect(self):
459         """Disconnect the USB port."""
460         self.wreg(rUSBCTL,bmVBGATE);
461     def STALL_EP0(self,SUD=None):
462         """Stall for an unknown SETUP event."""
463         if SUD==None:
464             print "Stalling EP0.";
465         else:
466             print "Stalling EPO for %s" % self.setup2str(SUD);
467         self.wreg(rEPSTALLS,0x23); #All three stall bits.
468     def SETBIT(self,reg,val):
469         """Set a bit in a register."""
470         self.wreg(reg,self.rreg(reg)|val);
471     def vbus_on(self):
472         """Turn on the target device."""
473         self.wreg(rIOPINS2,(self.rreg(rIOPINS2)|0x08));
474     def vbus_off(self):
475         """Turn off the target device's power."""
476         self.wreg(rIOPINS2,0x00);
477     def reset_host(self):
478         """Resets the chip into host mode."""
479         self.wreg(rUSBCTL,bmCHIPRES); #Stop the oscillator.
480         self.wreg(rUSBCTL,0x00);      #restart it.
481         
482         #FIXME: Why does the OSC line never settle?
483         #Code works without it.
484         
485         #print "Waiting for PLL to sabilize.";
486         #while self.rreg(rUSBIRQ)&bmOSCOKIRQ:
487         #    #Hang until the PLL stabilizes.
488         #    pass;
489         #print "Stable.";
490
491 class GoodFETMAXUSBHost(GoodFETMAXUSB):
492     """This is a class for implemented a minimal USB host.
493     It's intended for fuzzing, rather than for daily use."""
494     def hostinit(self):
495         """Initialize the MAX3421 as a USB Host."""
496         self.usb_connect();
497         print "Enabling host mode.";
498         self.wreg(rPINCTL,(bmFDUPSPI|bmPOSINT));
499         print "Resetting host.";
500         self.reset_host();
501         self.vbus_off();
502         time.sleep(0.2);
503         print "Powering host.";
504         self.vbus_on();
505         
506         #self.hostrun();
507     def hostrun(self):
508         """Run as a minimal host and dump the config tables."""
509         while 1:
510             self.detect_device();
511             time.sleep(0.2);
512             self.enumerate_device();
513             self.wait_for_disconnect();
514     def detect_device(self):
515         """Waits for a device to be inserted and then returns."""
516         busstate=0;
517         
518         #Activate host mode and turn on 15K pulldown resistors on D+ and D-.
519         self.wreg(rMODE,(bmDPPULLDN|bmDMPULLDN|bmHOST));
520         #Clear connection detect IRQ.
521         self.wreg(rHIRQ,bmCONDETIRQ);
522         
523         print "Waiting for a device connection.";
524         while busstate==0:
525             self.wreg(rHCTL,bmSAMPLEBUS); #Update JSTATUS and KSTATUS bits.
526             busstate=self.rreg(rHRSL) & (bmJSTATUS|bmKSTATUS);
527             
528         if busstate==bmJSTATUS:
529             print "Detected Full-Speed Device.";
530             self.wreg(rMODE,(bmDPPULLDN|bmDMPULLDN|bmHOST|bmSOFKAENAB));
531         elif busstate==bmKSTATUS:
532             print "Detected Low-Speed Device.";
533             self.wreg(rMODE,(bmDPPULLDN|bmDMPULLDN|bmHOST|bmLOWSPEED|bmSOFKAENAB));
534         else:
535             print "Not sure whether this is Full-Speed or Low-Speed.  Please investigate.";
536     def wait_for_disconnect(self):
537         """Wait for a device to be disconnected."""
538         print "Waiting for a device disconnect.";
539         
540         self.wreg(rHIRQ,bmCONDETIRQ); #Clear disconnect IRQ
541         while not (self.rreg(rHIRQ) & bmCONDETIRQ):
542             #Wait for IRQ to change.
543             pass;
544         
545         #Turn off markers.
546         self.wreg(rMODE,bmDPPULLDN|bmDMPULLDN|bmHOST);
547         print "Device disconnected.";
548         self.wreg(rIOPINS2,(self.rreg(rIOPINS2) & ~0x04)); #HL1_OFF
549         self.wreg(rIOPINS1,(self.rreg(rIOPINS1) & ~0x02)); #HL4_OFF
550
551     def enumerate_device(self):
552         """Enumerates a device on the present port."""
553         
554         Set_Address_to_7 = [0x00,0x05,0x07,0x00,0x00,0x00,0x00,0x00];
555         Get_Descriptor_Device = [0x80,0x06,0x00,0x01,0x00,0x00,0x00,0x00]; #len filled in
556         Get_Descriptor_Config = [0x80,0x06,0x00,0x02,0x00,0x00,0x00,0x00];
557         
558         
559         print "Issuing USB bus reset.";
560         self.wreg(rHCTL,bmBUSRST);
561         while self.rreg(rHCTL) & bmBUSRST:
562             #Wait for reset to complete.
563             pass;
564         
565         time.sleep(0.2);
566         
567         #Get the device descriptor.
568         self.wreg(rPERADDR,0); #First request to address 0.
569         self.maxPacketSize=8; #Only safe value for first check.
570         Get_Descriptor_Device[6]=8; # wLengthL
571         Get_Descriptor_Device[7]=0; # wLengthH
572         
573         print "Fetching 8 bytes of Device Descriptor.";
574         self.ctl_read(Get_Descriptor_Device); # Get device descriptor into self.xfrdata;
575         self.maxPacketSize=self.xfrdata[7];
576         print "EP0 maxPacketSize is %02i bytes." % self.maxPacketSize;
577         
578         # Issue another USB bus reset
579         print "Resetting the bus again."
580         self.wreg(rHCTL,bmBUSRST);
581         while self.rreg(rHCTL) & bmBUSRST:
582             #Wait for reset to complete.
583             pass;
584         time.sleep(0.2);
585         
586         # Set_Address to 7 (Note: this request goes to address 0, already set in PERADDR register).
587         print "Setting address to 0x07";
588         HR = self.ctl_write_nd(Set_Address_to_7);   # CTL-Write, no data stage
589         #if(print_error(HR)) return;
590         
591         time.sleep(0.002);           # Device gets 2 msec recovery time
592         self.wreg(rPERADDR,7);       # now all transfers go to addr 7
593         
594         
595         #Get the device descriptor at the assigned address.
596         Get_Descriptor_Device[6]=0x12; #Fill in real descriptor length.
597         print "Fetching Device Descriptor."
598         self.ctl_read(Get_Descriptor_Device); #Result in self.xfrdata;
599         
600         self.descriptor=self.xfrdata;
601         self.VID        = self.xfrdata[8] + 256*self.xfrdata[9];
602         self.PID        = self.xfrdata[10]+ 256*self.xfrdata[11];
603         iMFG    = self.xfrdata[14];
604         iPROD   = self.xfrdata[15];
605         iSERIAL = self.xfrdata[16];
606         
607         self.manufacturer=self.getDescriptorString(iMFG);
608         self.product=self.getDescriptorString(iPROD);
609         self.serial=self.getDescriptorString(iSERIAL);
610         
611         self.printstrings();
612         
613     def printstrings(self):
614         print "Vendor  ID is %04x." % self.VID;
615         print "Product ID is %04x." % self.PID;
616         print "Manufacturer: %s" % self.manufacturer;
617         print "Product:      %s" % self.product;
618         print "Serial:       %s" % self.serial;
619         
620     def getDescriptorString(self, index):
621         """Grabs a string from the descriptor string table."""
622         # Get_Descriptor-String template. Code fills in idx at str[2].
623         Get_Descriptor_String = [0x80,0x06,index,0x03,0x00,0x00,0x40,0x00];
624         
625         if index==0: return "MISSING STRING";
626         
627         status=self.ctl_read(Get_Descriptor_String);
628         if status: return None;
629         
630         #Since we've got a string
631         toret="";
632         for c in self.xfrdata[2:len(self.xfrdata)]:
633             if c>0: toret=toret+chr(c);
634         return toret;
635 class GoodFETMAXUSBDevice(GoodFETMAXUSB):
636     
637     def send_descriptor(self,SUD):
638         """Send the USB descriptors based upon the setup data."""
639         desclen=0;
640         reqlen=ord(SUD[wLengthL])+256*ord(SUD[wLengthH]); #16-bit length
641         desctype=ord(SUD[wValueH]);
642         
643         if desctype==GD_DEVICE:
644             desclen=self.DD[0];
645             ddata=self.DD;
646         elif desctype==GD_CONFIGURATION:
647             desclen=self.CD[2];
648             ddata=self.CD;
649         elif desctype==GD_STRING:
650             desclen=ord(self.strDesc[ord(SUD[wValueL])][0]);
651             ddata=self.strDesc[ord(SUD[wValueL])];
652         elif desctype==GD_HID:
653             #Don't know how to do this yet.
654             pass;
655         elif desctype==GD_REPORT:
656             desclen=self.CD[25];
657             ddata=self.RepD;
658         #TODO Configuration, String, Hid, and Report
659         
660         if desclen>0:
661             #Reduce desclen if asked for fewer bytes.
662             desclen=min(reqlen,desclen);
663             #Send those bytes.
664             self.writebytes(rEP0FIFO,ddata[0:desclen]);
665             self.wregAS(rEP0BC,desclen);
666         else:
667             print "Stalling in send_descriptor() for lack of handler for %02x." % desctype;
668             self.STALL_EP0(SUD);
669     def set_configuration(self,SUD):
670         """Set the configuration."""
671         bmSUSPIE=0x10;
672         configval=ord(SUD[wValueL]);
673         if(configval>0):
674             self.SETBIT(rUSBIEN,bmSUSPIE);
675         self.rregAS(rFNADDR);
676 class GoodFETMAXUSBHID(GoodFETMAXUSBDevice):
677     """This is an example HID keyboard driver, loosely based on the
678     MAX3420 examples."""
679     def hidinit(self):
680         """Initialize a USB HID device."""
681         self.usb_disconnect();
682         self.usb_connect();
683         
684         self.hidrun();
685         
686     def hidrun(self):
687         """Main loop of the USB HID emulator."""
688         print "Starting a HID device.  This won't return.";
689         while 1:
690             self.service_irqs();
691     def do_SETUP(self):
692         """Handle USB Enumeration"""
693         
694         #Grab the SETUP packet from the buffer.
695         SUD=self.readbytes(rSUDFIFO,8);
696         
697         #Parse the SETUP packet
698         print "Handling a setup packet of %s" % self.setup2str(SUD);
699         
700         self.OsLastConfigType=ord(SUD[bmRequestType]);
701         self.typepos=0;
702         setuptype=(ord(SUD[bmRequestType])&0x60);
703         if setuptype==0x00:
704             self.std_request(SUD);
705         elif setuptype==0x20:
706             self.class_request(SUD);
707         elif setuptype==0x40:
708             self.vendor_request(SUD);
709         else:
710             print "Unknown request type 0x%02x." % ord(SUD[bmRequestType])
711             self.STALL_EP0(SUD);
712     def class_request(self,SUD):
713         """Handle a class request."""
714         print "Stalling a class request.";
715         self.STALL_EP0(SUD);
716     def vendor_request(self,SUD):
717         print "Stalling a vendor request.";
718         self.STALL_EP0(SUD);
719     def std_request(self,SUD):
720         """Handles a standard setup request."""
721         setuptype=ord(SUD[bRequest]);
722         if setuptype==SR_GET_DESCRIPTOR: self.send_descriptor(SUD);
723         #elif setuptype==SR_SET_FEATURE:
724         #    self.rregAS(rFNADDR);
725         #    # self.feature(1);
726         elif setuptype==SR_SET_CONFIGURATION: self.set_configuration(SUD);
727         elif setuptype==SR_GET_STATUS: self.get_status(SUD);
728         elif setuptype==SR_SET_ADDRESS: self.rregAS(rFNADDR);
729         elif setuptype==SR_GET_INTERFACE: self.get_interface(SUD);
730         else:
731             print "Stalling Unknown standard setup request type %02x" % setuptype;
732             self.STALL_EP0(SUD);
733     
734     def get_interface(self,SUD):
735         """Handles a setup request for SR_GET_INTERFACE."""
736         if ord(SUD[wIndexL]==0):
737             self.wreg(rEP0FIFO,0);
738             self.wregAS(rEP0BC,1);
739         else:
740             self.STALL_EP0(SUD);
741     
742     OsLastConfigType=-1;
743 #Device Descriptor
744     DD=[0x12,                   # bLength = 18d
745         0x01,                   # bDescriptorType = Device (1)
746         0x00,0x01,              # bcdUSB(L/H) USB spec rev (BCD)
747         0x00,0x00,0x00,         # bDeviceClass, bDeviceSubClass, bDeviceProtocol
748         0x40,                   # bMaxPacketSize0 EP0 is 64 bytes
749         0x6A,0x0B,              # idVendor(L/H)--Maxim is 0B6A
750         0x46,0x53,              # idProduct(L/H)--5346
751         0x34,0x12,              # bcdDevice--1234
752         1,2,3,                  # iManufacturer, iProduct, iSerialNumber
753         1];
754 #Configuration Descriptor
755     CD=[0x09,                   # bLength
756         0x02,                   # bDescriptorType = Config
757         0x22,0x00,              # wTotalLength(L/H) = 34 bytes
758         0x01,                   # bNumInterfaces
759         0x01,                   # bConfigValue
760         0x00,                   # iConfiguration
761         0xE0,                   # bmAttributes. b7=1 b6=self-powered b5=RWU supported
762         0x01,                   # MaxPower is 2 ma
763 # INTERFACE Descriptor
764         0x09,                   # length = 9
765         0x04,                   # type = IF
766         0x00,                   # IF #0
767         0x00,                   # bAlternate Setting
768         0x01,                   # bNum Endpoints
769         0x03,                   # bInterfaceClass = HID
770         0x00,0x00,              # bInterfaceSubClass, bInterfaceProtocol
771         0x00,                   # iInterface
772 # HID Descriptor--It's at CD[18]
773         0x09,                   # bLength
774         0x21,                   # bDescriptorType = HID
775         0x10,0x01,              # bcdHID(L/H) Rev 1.1
776         0x00,                   # bCountryCode (none)
777         0x01,                   # bNumDescriptors (one report descriptor)
778         0x22,                   # bDescriptorType       (report)
779         43,0,                   # CD[25]: wDescriptorLength(L/H) (report descriptor size is 43 bytes)
780 # Endpoint Descriptor
781         0x07,                   # bLength
782         0x05,                   # bDescriptorType (Endpoint)
783         0x83,                   # bEndpointAddress (EP3-IN)             
784         0x03,                   # bmAttributes  (interrupt)
785         64,0,                   # wMaxPacketSize (64)
786         10];
787     strDesc=[
788 # STRING descriptor 0--Language string
789 "\x04\x03\x09\x04",
790 # [
791 #         0x04,                 # bLength
792 #       0x03,                   # bDescriptorType = string
793 #       0x09,0x04               # wLANGID(L/H) = English-United Sates
794 # ],
795 # STRING descriptor 1--Manufacturer ID
796 "\x0c\x03M\x00a\x00x\x00i\x00m\x00",
797 # [
798 #         12,                   # bLength
799 #       0x03,                   # bDescriptorType = string
800 #       'M',0,'a',0,'x',0,'i',0,'m',0 # text in Unicode
801 # ], 
802 # STRING descriptor 2 - Product ID
803 "\x18\x03M\x00A\x00X\x003\x004\x002\x000\x00E\x00 \x00E\x00n\x00u\x00m\x00 \x00C\x00o\x00d\x00e\x00",
804 # [     24,                     # bLength
805 #       0x03,                   # bDescriptorType = string
806 #       'M',0,'A',0,'X',0,'3',0,'4',0,'2',0,'0',0,'E',0,' ',0,
807 #         'E',0,'n',0,'u',0,'m',0,' ',0,'C',0,'o',0,'d',0,'e',0
808 # ],
809
810
811 # STRING descriptor 3 - Serial Number ID
812 "\x14\x03S\x00/\x00N\x00 \x003\x004\x002\x000\x00E\x00"
813 # [       20,                   # bLength
814 #       0x03,                   # bDescriptorType = string
815 #       'S',0,                          
816 #       '/',0,
817 #       'N',0,
818 #       ' ',0,
819 #       '3',0,
820 #       '4',0,
821 #       '2',0,
822 #       '0',0,
823 #         'E',0,
824 # ]
825 ];
826     RepD=[
827         0x05,0x01,              # Usage Page (generic desktop)
828         0x09,0x06,              # Usage (keyboard)
829         0xA1,0x01,              # Collection
830         0x05,0x07,              #   Usage Page 7 (keyboard/keypad)
831         0x19,0xE0,              #   Usage Minimum = 224
832         0x29,0xE7,              #   Usage Maximum = 231
833         0x15,0x00,              #   Logical Minimum = 0
834         0x25,0x01,              #   Logical Maximum = 1
835         0x75,0x01,              #   Report Size = 1
836         0x95,0x08,              #   Report Count = 8
837         0x81,0x02,              #  Input(Data,Variable,Absolute)
838         0x95,0x01,              #   Report Count = 1
839         0x75,0x08,              #   Report Size = 8
840         0x81,0x01,              #  Input(Constant)
841         0x19,0x00,              #   Usage Minimum = 0
842         0x29,0x65,              #   Usage Maximum = 101
843         0x15,0x00,              #   Logical Minimum = 0,
844         0x25,0x65,              #   Logical Maximum = 101
845         0x75,0x08,              #   Report Size = 8
846         0x95,0x01,              #   Report Count = 1
847         0x81,0x00,              #  Input(Data,Variable,Array)
848         0xC0]
849
850     def get_status(self,SUD):
851         """Get the USB Setup Status."""
852         testbyte=ord(SUD[bmRequestType])
853         
854         #Toward Device
855         if testbyte==0x80:
856             self.wreg(rEP0FIFO,0x03); #Enable RWU and self-powered
857             self.wreg(rEP0FIFO,0x00); #Second byte is always zero.
858             self.wregAS(rEP0BC,2);    #Load byte count, arm transfer, and ack CTL.
859         #Toward Interface
860         elif testbyte==0x81:
861             self.wreg(rEP0FIFO,0x00);
862             self.wreg(rEP0FIFO,0x00); #Second byte is always zero.
863             self.wregAS(rEP0BC,2);
864         #Toward Endpoint
865         elif testbyte==0x82:
866             if(ord(SUD[wIndexL])==0x83):
867                 self.wreg(rEP0FIFO,0x01); #Stall EP3
868                 self.wreg(rEP0FIFO,0x00); #Second byte is always zero.
869                 self.wregAS(rEP0BC,2);
870             else:
871                 self.STALL_EP0(SUD);
872         else:
873             self.STALL_EP0(SUD);
874
875     typepos=0;
876     typestrings={
877         -1   : "Python does USB HID!\n",                # Unidentified OS.  This is the default typestring.
878         0x00 : "OSX Hosts don't recognize Maxim keyboards.\n",  # We have to identify as an Apple keyboard to get arround the unknown keyboard error.
879         0xA1 : "Python does USB HID on Linux!\n",
880         0x81 : "                                                                                             Python does USB HID on Windows!\n",        # Windows requires a bit of a delay.  Maybe we can watch for a keyboard reset command?
881     }
882     def typestring(self):
883         if self.typestrings.has_key(self.OsLastConfigType):
884             return self.typestrings[self.OsLastConfigType];
885         else:
886             return self.typestrings[-1];
887     # http://www.win.tue.nl/~aeb/linux/kbd/scancodes-14.html
888     # Escape=0x29 Backsp=0x2A Space=0x2C CapsLock=0x39 Menu=0x65
889     keymaps={
890         'en_US'  :[ '    abcdefghijklmnopqrstuvwxyz1234567890\n\e\7f\t -=[]\\\\;\'`,./',
891                     '''    \ 1\ 2\ 3\ 4\ 5\ 6\a\b  \v\f\r\ e\ f\10 \12 \14\15\16\17\18\19\1a''',        # LeftCtrl
892                     '    ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()     {}?+||:"~<>?', # LeftShift
893                     '', # LeftCtrl & LeftShift
894                     '    \ea\eb\ec'], # LeftAlt
895         'Dvorak' :[ '    axje.uidchtnmbrl\'poygk,qf;1234567890\n\e\7f\t []/=\\\\s-`wvz',
896                     '''    \ 1\18 \ 5 \15 \ 4\ 3\b\14\ e\r\ 2\12\f \10\ f\19\a\v  \ 6                         \17\16\1a''',     # LeftCtrl
897                     '    AXJE UIDCHTNMBRL"POYGK<QF:!@#$%^&*()     {}?+||S_~WVZ', # LeftShift
898                     '', # LeftCtrl & LeftShift
899                     '    \ea\ex\ej'], # LeftAlt
900     }
901     layout='en_US';
902     def keymap(self):
903         return self.keymaps[self.layout];
904     modifiers={
905         'None':         0b00000000,
906         'LeftCtrl':     0b00000001,
907         'LeftShift':    0b00000010,
908         'LeftAlt':      0b00000100,
909         'LeftGUI':      0b00001000,
910         'RightCtrl':    0b00010000,
911         'RightShift':   0b00100000,
912         'RightAlt':     0b01000000,
913         'RightGUI':     0b10000000
914     }
915
916     def asc2hid(self,ascii):
917         """Translate ASCII to an USB keycode."""
918         if type(ascii)!=str:
919             return (0,0);               # Send NoEvent if not passed a character
920         if ascii==' ':
921             return (0,0x2C);            # space
922         for modset in self.keymap():
923             keycode=modset.find(ascii);
924             if keycode != -1:
925                 modifier = self.keymap().index(modset)
926                 return (modifier, keycode);
927         return (0,0);
928     def type_IN3(self):
929         """Type next letter in buffer."""
930         string=self.typestring();
931         if self.typepos>=len(string):
932             self.typeletter(0);         # Send NoEvent to indicate key-up
933             exit(0);
934             self.typepos=0;             # Repeat typestring forever!
935             # This would be a great place to enable a typethrough mode so the host operator can control the target
936         else:
937             if self.usbverbose:
938                 sys.stdout.write(string[self.typepos]);
939                 sys.stdout.flush();
940             self.typeletter(string[self.typepos]);
941             self.typepos+=1;
942         return;
943     def typeletter(self,key):
944         """Type a letter on IN3.  Zero for keyup."""
945         mod=0;
946         if type(key)==str:
947             (mod, key) = self.asc2hid(key);
948         self.wreg(rEP3INFIFO,mod);
949         self.wreg(rEP3INFIFO,0);
950         self.wreg(rEP3INFIFO,key);
951         self.wreg(rEP3INBC,3);
952     def do_IN3(self):
953         """Handle IN3 event."""
954         #Don't bother clearing interrupt flag, that's done by sending the reply.
955         if self.OsLastConfigType != -1: # Wait for some configuration before stuffing keycodes down the pipe
956             self.type_IN3();
957