2 # GoodFET Client Library for Maxim USB Chips.
4 # (C) 2012 Travis Goodspeed <travis at radiantmachines.com>
6 # This code is being rewritten and refactored. You've been warned!
8 import sys, time, string, cStringIO, struct, glob, os;
10 from GoodFET import GoodFET;
35 # R11 EPIRQ register bits
43 # R12 EPIEN register bits
52 # ************************
53 # Standard USB Requests
54 SR_GET_STATUS =0x00 # Get Status
55 SR_CLEAR_FEATURE =0x01 # Clear Feature
56 SR_RESERVED =0x02 # Reserved
57 SR_SET_FEATURE =0x03 # Set Feature
58 SR_SET_ADDRESS =0x05 # Set Address
59 SR_GET_DESCRIPTOR =0x06 # Get Descriptor
60 SR_SET_DESCRIPTOR =0x07 # Set Descriptor
61 SR_GET_CONFIGURATION =0x08 # Get Configuration
62 SR_SET_CONFIGURATION =0x09 # Set Configuration
63 SR_GET_INTERFACE =0x0a # Get Interface
64 SR_SET_INTERFACE =0x0b # Set Interface
66 # Get Descriptor codes
67 GD_DEVICE =0x01 # Get device descriptor: Device
68 GD_CONFIGURATION =0x02 # Get device descriptor: Configuration
69 GD_STRING =0x03 # Get device descriptor: String
70 GD_HID =0x21 # Get descriptor: HID
71 GD_REPORT =0x22 # Get descriptor: Report
73 # SETUP packet offsets
95 class GoodFETMAXUSB(GoodFET):
97 def MAXUSBsetup(self):
98 """Move the FET into the MAXUSB application."""
99 self.writecmd(self.MAXUSBAPP,0x10,0,self.data); #MAXUSB/SETUP
100 print "Connected to MAX342x Rev. %x" % (self.rreg(rREVISION));
101 self.wreg(rPINCTL,0x18); #Set duplex and negative INT level.
103 def MAXUSBtrans8(self,byte):
104 """Read and write 8 bits by MAXUSB."""
105 data=self.MAXUSBtrans([byte]);
108 def MAXUSBtrans(self,data):
109 """Exchange data by MAXUSB."""
111 self.writecmd(self.MAXUSBAPP,0x00,len(data),data);
115 """Peek 8 bits from a register."""
117 self.writecmd(self.MAXUSBAPP,0x00,len(data),data);
118 return ord(self.data[1]);
119 def rregAS(self,reg):
120 """Peek 8 bits from a register, setting AS."""
122 self.writecmd(self.MAXUSBAPP,0x00,len(data),data);
123 return ord(self.data[1]);
124 def wreg(self,reg,value):
125 """Poke 8 bits into a register."""
126 data=[(reg<<3)|2,value];
127 self.writecmd(self.MAXUSBAPP,0x00,len(data),data);
129 def wregAS(self,reg,value):
130 """Poke 8 bits into a register, setting AS."""
131 data=[(reg<<3)|3,value];
132 self.writecmd(self.MAXUSBAPP,0x00,len(data),data);
134 def readbytes(self,reg,length):
135 """Peek some bytes from a register."""
136 data=[(reg<<3)]+range(0,length);
137 self.writecmd(self.MAXUSBAPP,0x00,len(data),data);
138 toret=self.data[1:len(self.data)];
141 ashex=ashex+(" %02x"%ord(foo));
142 print "GET %02x==%s" % (reg,ashex);
144 def writebytes(self,reg,tosend):
145 """Poke some bytes into a register."""
147 if type(tosend)==str:
148 data=chr((reg<<3)|2)+tosend;
150 data=[(reg<<3)|2]+tosend;
153 ashex=ashex+(" %02x"%foo);
154 print "PUT %02x:=%s" % (reg,ashex)
155 self.writecmd(self.MAXUSBAPP,0x00,len(data),data);
156 def usb_connect(self):
157 """Connect the USB port."""
159 #disconnect D+ pullup if host turns off VBUS
160 self.wreg(rUSBCTL,0x48);
162 """Stall for an unknown event."""
164 self.wreg(rEPSTALLS,0x23); #All three stall bits.
165 def SETBIT(self,reg,val):
166 """Set a bit in a register."""
167 self.wreg(reg,self.rreg(reg)|val);
168 class GoodFETMAXUSBHID(GoodFETMAXUSB):
169 """This is an example HID keyboard driver, loosely based on the
172 """Initialize a USB HID device."""
177 """Main loop of the USB HID emulator."""
178 print "Starting a HID device. This won't return.";
182 """Handle USB Enumeration"""
184 #Grab the SETUP packet from the buffer.
185 SUD=self.readbytes(rSUDFIFO,8);
187 #Parse the SETUP packet
188 print "Handling a setup packet type 0x%02x" % ord(SUD[bmRequestType]);
189 setuptype=(ord(SUD[bmRequestType])&0x60);
191 self.std_request(SUD);
192 elif setuptype==0x20:
193 self.class_request(SUD);
194 elif setuptype==0x40:
195 self.vendor_request(SUD);
197 print "Unknown request type 0x%02x." % ord(SUD[bmRequestType])
199 def class_request(self,SUD):
200 """Handle a class request."""
201 print "Stalling a class request.";
203 def vendor_request(self,SUD):
204 print "Stalling a vendor request.";
206 def std_request(self,SUD):
207 """Handles a standard setup request."""
208 setuptype=ord(SUD[bRequest]);
209 if setuptype==SR_GET_DESCRIPTOR: self.send_descriptor(SUD);
210 elif setuptype==SR_SET_FEATURE: self.feature(1);
211 elif setuptype==SR_SET_CONFIGURATION: self.set_configuration(SUD);
212 elif setuptype==SR_GET_STATUS: self.get_status(SUD);
213 elif setuptype==SR_SET_ADDRESS: self.rregAS(rFNADDR);
214 elif setuptype==SR_GET_INTERFACE: self.get_interface(SUD);
216 print "Stalling Unknown standard setup request type %02x" % setuptype;
220 def get_interface(self,SUD):
221 """Handles a setup request for SR_GET_INTERFACE."""
222 if ord(SUD[wIndexL]==0):
223 self.wreg(rEP0FIFO,0);
224 self.wregAS(rEP0BC,1);
229 DD=[0x12, # bLength = 18d
230 0x01, # bDescriptorType = Device (1)
231 0x00,0x01, # bcdUSB(L/H) USB spec rev (BCD)
232 0x00,0x00,0x00, # bDeviceClass, bDeviceSubClass, bDeviceProtocol
233 0x40, # bMaxPacketSize0 EP0 is 64 bytes
234 0x6A,0x0B, # idVendor(L/H)--Maxim is 0B6A
235 0x46,0x53, # idProduct(L/H)--5346
236 0x34,0x12, # bcdDevice--1234
237 1,2,3, # iManufacturer, iProduct, iSerialNumber
239 #Configuration Descriptor
241 0x02, # bDescriptorType = Config
242 0x22,0x00, # wTotalLength(L/H) = 34 bytes
243 0x01, # bNumInterfaces
245 0x00, # iConfiguration
246 0xE0, # bmAttributes. b7=1 b6=self-powered b5=RWU supported
247 0x01, # MaxPower is 2 ma
248 # INTERFACE Descriptor
252 0x00, # bAlternate Setting
253 0x01, # bNum Endpoints
254 0x03, # bInterfaceClass = HID
255 0x00,0x00, # bInterfaceSubClass, bInterfaceProtocol
257 # HID Descriptor--It's at CD[18]
259 0x21, # bDescriptorType = HID
260 0x10,0x01, # bcdHID(L/H) Rev 1.1
261 0x00, # bCountryCode (none)
262 0x01, # bNumDescriptors (one report descriptor)
263 0x22, # bDescriptorType (report)
264 43,0, # CD[25]: wDescriptorLength(L/H) (report descriptor size is 43 bytes)
265 # Endpoint Descriptor
267 0x05, # bDescriptorType (Endpoint)
268 0x83, # bEndpointAddress (EP3-IN)
269 0x03, # bmAttributes (interrupt)
270 64,0, # wMaxPacketSize (64)
273 # STRING descriptor 0--Language string
277 # 0x03, # bDescriptorType = string
278 # 0x09,0x04 # wLANGID(L/H) = English-United Sates
280 # STRING descriptor 1--Manufacturer ID
281 "\x0c\x03M\x00a\x00x\x00i\x00m\x00",
284 # 0x03, # bDescriptorType = string
285 # 'M',0,'a',0,'x',0,'i',0,'m',0 # text in Unicode
287 # STRING descriptor 2 - Product ID
288 "\x18\x03M\x00A\x00X\x003\x004\x002\x000\x00E\x00 \x00E\x00n\x00u\x00m\x00 \x00C\x00o\x00d\x00e\x00",
290 # 0x03, # bDescriptorType = string
291 # 'M',0,'A',0,'X',0,'3',0,'4',0,'2',0,'0',0,'E',0,' ',0,
292 # 'E',0,'n',0,'u',0,'m',0,' ',0,'C',0,'o',0,'d',0,'e',0
296 # STRING descriptor 3 - Serial Number ID
297 "\x14\x03S\x00/\x00N\x00 \x003\x004\x002\x000\x00E\x00"
299 # 0x03, # bDescriptorType = string
312 0x05,0x01, # Usage Page (generic desktop)
313 0x09,0x06, # Usage (keyboard)
314 0xA1,0x01, # Collection
315 0x05,0x07, # Usage Page 7 (keyboard/keypad)
316 0x19,0xE0, # Usage Minimum = 224
317 0x29,0xE7, # Usage Maximum = 231
318 0x15,0x00, # Logical Minimum = 0
319 0x25,0x01, # Logical Maximum = 1
320 0x75,0x01, # Report Size = 1
321 0x95,0x08, # Report Count = 8
322 0x81,0x02, # Input(Data,Variable,Absolute)
323 0x95,0x01, # Report Count = 1
324 0x75,0x08, # Report Size = 8
325 0x81,0x01, # Input(Constant)
326 0x19,0x00, # Usage Minimum = 0
327 0x29,0x65, # Usage Maximum = 101
328 0x15,0x00, # Logical Minimum = 0,
329 0x25,0x65, # Logical Maximum = 101
330 0x75,0x08, # Report Size = 8
331 0x95,0x01, # Report Count = 1
332 0x81,0x00, # Input(Data,Variable,Array)
334 def send_descriptor(self,SUD):
335 """Send the USB descriptors based upon the setup data."""
337 reqlen=ord(SUD[wLengthL])+256*ord(SUD[wLengthH]); #16-bit length
338 desctype=ord(SUD[wValueH]);
340 if desctype==GD_DEVICE:
343 elif desctype==GD_CONFIGURATION:
346 elif desctype==GD_STRING:
347 desclen=self.strDesc[ord(SUD[wValueL])][0];
348 ddata=self.strDesc[ord(SUD[wValueL])];
349 elif desctype==GD_REPORT:
353 #TODO Configuration, String, Hid, and Report
356 sendlen=min(reqlen,desclen);
357 self.writebytes(rEP0FIFO,ddata);
358 self.wregAS(rEP0BC,sendlen);
360 print "Stalling in send_descriptor() for lack of handler for %02x." % desctype;
362 def set_configuration(self,SUD):
363 """Set the configuration."""
365 configval=ord(SUD[wValueL]);
367 self.SETBIT(rUSBIEN,bmSUSPIE);
368 self.rregAS(rFNADDR);
369 def get_status(self,SUD):
370 """Get the USB Setup Status."""
371 testbyte=ord(SUD[bmRequestType])
375 self.wreg(rEP0FIFO,0x03); #Enable RWU and self-powered
376 self.wreg(rEP0FIFO,0x00); #Second byte is always zero.
377 self.wregAS(rEP0BC,2); #Load byte count, arm transfer, and ack CTL.
380 self.wreg(rEP0FIFO,0x00);
381 self.wreg(rEP0FIFO,0x00); #Second byte is always zero.
382 self.wregAS(rEP0BC,2);
385 if(ord(SUD[wIndexL])==0x83):
386 self.wreg(rEP0FIFO,0x01); #Stall EP3
387 self.wreg(rEP0FIFO,0x00); #Second byte is always zero.
388 self.wregAS(rEP0BC,2);
393 def service_irqs(self):
394 """Handle USB interrupt events."""
396 epirq=self.rreg(rEPIRQ);
397 usbirq=self.rreg(rUSBIRQ);
399 #Are we being asked for setup data?
400 if(epirq&bmSUDAVIRQ): #Setup Data Requested
401 self.wreg(rEPIRQ,bmSUDAVIRQ); #Clear the bit
403 if(epirq&bmIN3BAVIRQ): #EN3-IN packet
408 typestring=" Python does USB HID!";
411 def asc2hid(self,ascii):
412 """Translate ASCII to an USB keycode."""
414 if a>='a' and a<='z':
415 return ord(a)-ord('a')+4;
416 elif a>='A' and a<='Z':
417 return ord(a)-ord('A')+4;
423 """Type next letter in buffer."""
424 if self.typepos>=len(self.typestring):
426 elif self.typephase==0:
431 self.typeletter(self.typestring[self.typepos]);
432 self.typepos=self.typepos+1;
434 def typeletter(self,key):
435 """Type a letter on IN3. Zero for keyup."""
436 #if type(key)==str: key=ord(key);
438 self.wreg(rEP3INFIFO,0);
439 self.wreg(rEP3INFIFO,0);
440 self.wreg(rEP3INFIFO,self.asc2hid(key));
441 self.wreg(rEP3INBC,3);
443 """Handle IN3 event."""
444 #Don't bother clearing interrupt flag, that's done by sending the reply.