]> cvs.zerfleddert.de Git - proxmark3-svn/blob - common/usb.c
Some new commands (threshold and fskdemod) that act on the graph window
[proxmark3-svn] / common / usb.c
1 //-----------------------------------------------------------------------------
2 // My USB driver. This has to be common, because it exists in both the
3 // bootrom and the application.
4 // Jonathan Westhues, split Aug 14 2005
5 //-----------------------------------------------------------------------------
6 #include <proxmark3.h>
7
8 #define min(a, b) (((a) > (b)) ? (b) : (a))
9
10 #define USB_REPORT_PACKET_SIZE 64
11
12 typedef struct PACKED {
13 BYTE bmRequestType;
14 BYTE bRequest;
15 WORD wValue;
16 WORD wIndex;
17 WORD wLength;
18 } UsbSetupData;
19
20 #define USB_REQUEST_GET_STATUS 0
21 #define USB_REQUEST_CLEAR_FEATURE 1
22 #define USB_REQUEST_SET_FEATURE 3
23 #define USB_REQUEST_SET_ADDRESS 5
24 #define USB_REQUEST_GET_DESCRIPTOR 6
25 #define USB_REQUEST_SET_DESCRIPTOR 7
26 #define USB_REQUEST_GET_CONFIGURATION 8
27 #define USB_REQUEST_SET_CONFIGURATION 9
28 #define USB_REQUEST_GET_INTERFACE 10
29 #define USB_REQUEST_SET_INTERFACE 11
30 #define USB_REQUEST_SYNC_FRAME 12
31
32 #define USB_DESCRIPTOR_TYPE_DEVICE 1
33 #define USB_DESCRIPTOR_TYPE_CONFIGURATION 2
34 #define USB_DESCRIPTOR_TYPE_STRING 3
35 #define USB_DESCRIPTOR_TYPE_INTERFACE 4
36 #define USB_DESCRIPTOR_TYPE_ENDPOINT 5
37 #define USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER 6
38 #define USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONF 7
39 #define USB_DESCRIPTOR_TYPE_INTERFACE_POWER 8
40 #define USB_DESCRIPTOR_TYPE_HID 0x21
41 #define USB_DESCRIPTOR_TYPE_HID_REPORT 0x22
42
43 #define USB_DEVICE_CLASS_HID 0x03
44
45 static const BYTE HidReportDescriptor[] = {
46 0x06,0xA0,0xFF, // Usage Page (vendor defined) FFA0
47 0x09,0x01, // Usage (vendor defined)
48 0xA1,0x01, // Collection (Application)
49 0x09,0x02, // Usage (vendor defined)
50 0xA1,0x00, // Collection (Physical)
51 0x06,0xA1,0xFF, // Usage Page (vendor defined)
52
53 //The,input report
54 0x09,0x03, // usage - vendor defined
55 0x09,0x04, // usage - vendor defined
56 0x15,0x80, // Logical Minimum (-128)
57 0x25,0x7F, // Logical Maximum (127)
58 0x35,0x00, // Physical Minimum (0)
59 0x45,0xFF, // Physical Maximum (255)
60 0x75,0x08, // Report Size (8) (bits)
61 0x95,0x40, // Report Count (64) (fields)
62 0x81,0x02, // Input (Data,Variable,Absolute)
63
64 //The,output report
65 0x09,0x05, // usage - vendor defined
66 0x09,0x06, // usage - vendor defined
67 0x15,0x80, // Logical Minimum (-128)
68 0x25,0x7F, // Logical Maximum (127)
69 0x35,0x00, // Physical Minimum (0)
70 0x45,0xFF, // Physical Maximum (255)
71 0x75,0x08, // Report Size (8) (bits)
72 0x95,0x40, // Report Count (64) (fields)
73 0x91,0x02, // Output (Data,Variable,Absolute)
74
75 0xC0, // End Collection
76
77 0xC0, // End Collection
78 };
79
80 static const BYTE DeviceDescriptor[] = {
81 0x12, // Descriptor length (18 bytes)
82 0x01, // Descriptor type (Device)
83 0x10,0x01, // Complies with USB Spec. Release (0110h = release 1.10)
84 0x00, // Class code (0)
85 0x00, // Subclass code (0)
86 0x00, // Protocol (No specific protocol)
87 0x08, // Maximum packet size for Endpoint 0 (8 bytes)
88 0xc4,0x9a, // Vendor ID (random numbers)
89 0x8f,0x4b, // Product ID (random numbers)
90 0x01,0x00, // Device release number (0001)
91 0x01, // Manufacturer string descriptor index
92 0x02, // Product string descriptor index
93 0x00, // Serial Number string descriptor index (None)
94 0x01, // Number of possible configurations (1)
95 };
96
97 static const BYTE ConfigurationDescriptor[] = {
98 0x09, // Descriptor length (9 bytes)
99 0x02, // Descriptor type (Configuration)
100 0x29,0x00, // Total data length (41 bytes)
101 0x01, // Interface supported (1)
102 0x01, // Configuration value (1)
103 0x00, // Index of string descriptor (None)
104 0x80, // Configuration (Bus powered)
105 250, // Maximum power consumption (500mA)
106
107 //interface
108 0x09, // Descriptor length (9 bytes)
109 0x04, // Descriptor type (Interface)
110 0x00, // Number of interface (0)
111 0x00, // Alternate setting (0)
112 0x02, // Number of interface endpoint (2)
113 0x03, // Class code (HID)
114 0x00, // Subclass code ()
115 0x00, // Protocol code ()
116 0x00, // Index of string()
117
118 // class
119 0x09, // Descriptor length (9 bytes)
120 0x21, // Descriptor type (HID)
121 0x00,0x01, // HID class release number (1.00)
122 0x00, // Localized country code (None)
123 0x01, // # of HID class dscrptr to follow (1)
124 0x22, // Report descriptor type (HID)
125 // Total length of report descriptor
126 sizeof(HidReportDescriptor),0x00,
127
128 // endpoint 1
129 0x07, // Descriptor length (7 bytes)
130 0x05, // Descriptor type (Endpoint)
131 0x01, // Encoded address (Respond to OUT)
132 0x03, // Endpoint attribute (Interrupt transfer)
133 0x08,0x00, // Maximum packet size (8 bytes)
134 0x01, // Polling interval (1 ms)
135
136 // endpoint 2
137 0x07, // Descriptor length (7 bytes)
138 0x05, // Descriptor type (Endpoint)
139 0x82, // Encoded address (Respond to IN)
140 0x03, // Endpoint attribute (Interrupt transfer)
141 0x08,0x00, // Maximum packet size (8 bytes)
142 0x01, // Polling interval (1 ms)
143 };
144
145 static const BYTE StringDescriptor0[] = {
146 0x04, // Length
147 0x03, // Type is string
148 0x09, // English
149 0x04, // US
150 };
151
152 static const BYTE StringDescriptor1[] = {
153 24, // Length
154 0x03, // Type is string
155 'J', 0x00,
156 '.', 0x00,
157 ' ', 0x00,
158 'W', 0x00,
159 'e', 0x00,
160 's', 0x00,
161 't', 0x00,
162 'h', 0x00,
163 'u', 0x00,
164 'e', 0x00,
165 's', 0x00,
166 };
167
168 static const BYTE StringDescriptor2[] = {
169 54, // Length
170 0x03, // Type is string
171 'P', 0x00,
172 'r', 0x00,
173 'o', 0x00,
174 'x', 0x00,
175 'M', 0x00,
176 'a', 0x00,
177 'r', 0x00,
178 'k', 0x00,
179 '-', 0x00,
180 '3', 0x00,
181 ' ', 0x00,
182 'R', 0x00,
183 'F', 0x00,
184 'I', 0x00,
185 'D', 0x00,
186 ' ', 0x00,
187 'I', 0x00,
188 'n', 0x00,
189 's', 0x00,
190 't', 0x00,
191 'r', 0x00,
192 'u', 0x00,
193 'm', 0x00,
194 'e', 0x00,
195 'n', 0x00,
196 't', 0x00,
197 };
198
199 static const BYTE * const StringDescriptors[] = {
200 StringDescriptor0,
201 StringDescriptor1,
202 StringDescriptor2,
203 };
204
205
206 static BYTE UsbBuffer[64];
207 static int UsbSoFarCount;
208
209 static BYTE CurrentConfiguration;
210
211 static void UsbSendEp0(const BYTE *data, int len)
212 {
213 int thisTime, i;
214
215 do {
216 thisTime = min(len, 8);
217 len -= thisTime;
218
219 for(i = 0; i < thisTime; i++) {
220 UDP_ENDPOINT_FIFO(0) = *data;
221 data++;
222 }
223
224 if(UDP_ENDPOINT_CSR(0) & UDP_CSR_TX_PACKET_ACKED) {
225 UDP_ENDPOINT_CSR(0) &= ~UDP_CSR_TX_PACKET_ACKED;
226 while(UDP_ENDPOINT_CSR(0) & UDP_CSR_TX_PACKET_ACKED)
227 ;
228 }
229
230 UDP_ENDPOINT_CSR(0) |= UDP_CSR_TX_PACKET;
231
232 do {
233 if(UDP_ENDPOINT_CSR(0) & UDP_CSR_RX_PACKET_RECEIVED_BANK_0) {
234 // This means that the host is trying to write to us, so
235 // abandon our write to them.
236 UDP_ENDPOINT_CSR(0) &= ~UDP_CSR_RX_PACKET_RECEIVED_BANK_0;
237 return;
238 }
239 } while(!(UDP_ENDPOINT_CSR(0) & UDP_CSR_TX_PACKET_ACKED));
240 } while(len > 0);
241
242 if(UDP_ENDPOINT_CSR(0) & UDP_CSR_TX_PACKET_ACKED) {
243 UDP_ENDPOINT_CSR(0) &= ~UDP_CSR_TX_PACKET_ACKED;
244 while(UDP_ENDPOINT_CSR(0) & UDP_CSR_TX_PACKET_ACKED)
245 ;
246 }
247 }
248
249 static void UsbSendZeroLength(void)
250 {
251 UDP_ENDPOINT_CSR(0) |= UDP_CSR_TX_PACKET;
252
253 while(!(UDP_ENDPOINT_CSR(0) & UDP_CSR_TX_PACKET_ACKED))
254 ;
255
256 UDP_ENDPOINT_CSR(0) &= ~UDP_CSR_TX_PACKET_ACKED;
257
258 while(UDP_ENDPOINT_CSR(0) & UDP_CSR_TX_PACKET_ACKED)
259 ;
260 }
261
262 static void HandleRxdSetupData(void)
263 {
264 int i;
265 UsbSetupData usd;
266
267 for(i = 0; i < sizeof(usd); i++) {
268 ((BYTE *)&usd)[i] = UDP_ENDPOINT_FIFO(0);
269 }
270
271 if(usd.bmRequestType & 0x80) {
272 UDP_ENDPOINT_CSR(0) |= UDP_CSR_CONTROL_DATA_DIR;
273 while(!(UDP_ENDPOINT_CSR(0) & UDP_CSR_CONTROL_DATA_DIR))
274 ;
275 }
276
277 UDP_ENDPOINT_CSR(0) &= ~UDP_CSR_RX_HAVE_READ_SETUP_DATA;
278 while(UDP_ENDPOINT_CSR(0) & UDP_CSR_RX_HAVE_READ_SETUP_DATA)
279 ;
280
281 switch(usd.bRequest) {
282 case USB_REQUEST_GET_DESCRIPTOR:
283 if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_DEVICE) {
284 UsbSendEp0((BYTE *)&DeviceDescriptor,
285 min(sizeof(DeviceDescriptor), usd.wLength));
286 } else if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_CONFIGURATION) {
287 UsbSendEp0((BYTE *)&ConfigurationDescriptor,
288 min(sizeof(ConfigurationDescriptor), usd.wLength));
289 } else if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_STRING) {
290 const BYTE *s = StringDescriptors[usd.wValue & 0xff];
291 UsbSendEp0(s, min(s[0], usd.wLength));
292 } else if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_HID_REPORT) {
293 UsbSendEp0((BYTE *)&HidReportDescriptor,
294 min(sizeof(HidReportDescriptor), usd.wLength));
295 } else {
296 *((DWORD *)0x00200000) = usd.wValue;
297 }
298 break;
299
300 case USB_REQUEST_SET_ADDRESS:
301 UsbSendZeroLength();
302 UDP_FUNCTION_ADDR = UDP_FUNCTION_ADDR_ENABLED | usd.wValue ;
303 if(usd.wValue != 0) {
304 UDP_GLOBAL_STATE = UDP_GLOBAL_STATE_ADDRESSED;
305 } else {
306 UDP_GLOBAL_STATE = 0;
307 }
308 break;
309
310 case USB_REQUEST_GET_CONFIGURATION:
311 UsbSendEp0(&CurrentConfiguration, sizeof(CurrentConfiguration));
312 break;
313
314 case USB_REQUEST_GET_STATUS: {
315 if(usd.bmRequestType & 0x80) {
316 WORD w = 0;
317 UsbSendEp0((BYTE *)&w, sizeof(w));
318 }
319 break;
320 }
321 case USB_REQUEST_SET_CONFIGURATION:
322 CurrentConfiguration = usd.wValue;
323 if(CurrentConfiguration) {
324 UDP_GLOBAL_STATE = UDP_GLOBAL_STATE_CONFIGURED;
325 UDP_ENDPOINT_CSR(1) = UDP_CSR_ENABLE_EP |
326 UDP_CSR_EPTYPE_INTERRUPT_OUT;
327 UDP_ENDPOINT_CSR(2) = UDP_CSR_ENABLE_EP |
328 UDP_CSR_EPTYPE_INTERRUPT_IN;
329 } else {
330 UDP_GLOBAL_STATE = UDP_GLOBAL_STATE_ADDRESSED;
331 UDP_ENDPOINT_CSR(1) = 0;
332 UDP_ENDPOINT_CSR(2) = 0;
333 }
334 UsbSendZeroLength();
335 break;
336
337 case USB_REQUEST_GET_INTERFACE: {
338 BYTE b = 0;
339 UsbSendEp0(&b, sizeof(b));
340 break;
341 }
342
343 case USB_REQUEST_SET_INTERFACE:
344 UsbSendZeroLength();
345 break;
346
347 case USB_REQUEST_CLEAR_FEATURE:
348 case USB_REQUEST_SET_FEATURE:
349 case USB_REQUEST_SET_DESCRIPTOR:
350 case USB_REQUEST_SYNC_FRAME:
351 default:
352 break;
353 }
354 }
355
356 void UsbSendPacket(BYTE *packet, int len)
357 {
358 int i, thisTime;
359
360 while(len > 0) {
361 thisTime = min(len, 8);
362
363 for(i = 0; i < thisTime; i++) {
364 UDP_ENDPOINT_FIFO(2) = packet[i];
365 }
366 UDP_ENDPOINT_CSR(2) |= UDP_CSR_TX_PACKET;
367
368 while(!(UDP_ENDPOINT_CSR(2) & UDP_CSR_TX_PACKET_ACKED))
369 ;
370 UDP_ENDPOINT_CSR(2) &= ~UDP_CSR_TX_PACKET_ACKED;
371
372 while(UDP_ENDPOINT_CSR(2) & UDP_CSR_TX_PACKET_ACKED)
373 ;
374
375 len -= thisTime;
376 packet += thisTime;
377 }
378 }
379
380 static void HandleRxdData(void)
381 {
382 int i, len;
383
384 if(UDP_ENDPOINT_CSR(1) & UDP_CSR_RX_PACKET_RECEIVED_BANK_0) {
385 len = UDP_CSR_BYTES_RECEIVED(UDP_ENDPOINT_CSR(1));
386
387 for(i = 0; i < len; i++) {
388 UsbBuffer[UsbSoFarCount] = UDP_ENDPOINT_FIFO(1);
389 UsbSoFarCount++;
390 }
391
392 UDP_ENDPOINT_CSR(1) &= ~UDP_CSR_RX_PACKET_RECEIVED_BANK_0;
393 while(UDP_ENDPOINT_CSR(1) & UDP_CSR_RX_PACKET_RECEIVED_BANK_0)
394 ;
395
396 if(UsbSoFarCount >= 64) {
397 UsbPacketReceived(UsbBuffer, UsbSoFarCount);
398 UsbSoFarCount = 0;
399 }
400 }
401
402 if(UDP_ENDPOINT_CSR(1) & UDP_CSR_RX_PACKET_RECEIVED_BANK_1) {
403 len = UDP_CSR_BYTES_RECEIVED(UDP_ENDPOINT_CSR(1));
404
405 for(i = 0; i < len; i++) {
406 UsbBuffer[UsbSoFarCount] = UDP_ENDPOINT_FIFO(1);
407 UsbSoFarCount++;
408 }
409
410 UDP_ENDPOINT_CSR(1) &= ~UDP_CSR_RX_PACKET_RECEIVED_BANK_1;
411 while(UDP_ENDPOINT_CSR(1) & UDP_CSR_RX_PACKET_RECEIVED_BANK_1)
412 ;
413
414 if(UsbSoFarCount >= 64) {
415 UsbPacketReceived(UsbBuffer, UsbSoFarCount);
416 UsbSoFarCount = 0;
417 }
418 }
419 }
420
421 void UsbStart(void)
422 {
423 volatile int i;
424
425 UsbSoFarCount = 0;
426
427 USB_D_PLUS_PULLUP_OFF();
428
429 for(i = 0; i < 1000000; i++)
430 ;
431
432 USB_D_PLUS_PULLUP_ON();
433
434 if(UDP_INTERRUPT_STATUS & UDP_INTERRUPT_END_OF_BUS_RESET) {
435 UDP_INTERRUPT_CLEAR = UDP_INTERRUPT_END_OF_BUS_RESET;
436 }
437 }
438
439 BOOL UsbPoll(BOOL blinkLeds)
440 {
441 BOOL ret = FALSE;
442
443 if(UDP_INTERRUPT_STATUS & UDP_INTERRUPT_END_OF_BUS_RESET) {
444 UDP_INTERRUPT_CLEAR = UDP_INTERRUPT_END_OF_BUS_RESET;
445
446 // following a reset we should be ready to receive a setup packet
447 UDP_RESET_ENDPOINT = 0xf;
448 UDP_RESET_ENDPOINT = 0;
449
450 UDP_FUNCTION_ADDR = UDP_FUNCTION_ADDR_ENABLED;
451
452 UDP_ENDPOINT_CSR(0) = UDP_CSR_EPTYPE_CONTROL | UDP_CSR_ENABLE_EP;
453
454 CurrentConfiguration = 0;
455
456 ret = TRUE;
457 }
458
459 if(UDP_INTERRUPT_STATUS & UDP_INTERRUPT_ENDPOINT(0)) {
460 if(UDP_ENDPOINT_CSR(0) & UDP_CSR_RX_HAVE_READ_SETUP_DATA) {
461 HandleRxdSetupData();
462 ret = TRUE;
463 }
464 }
465
466 if(UDP_INTERRUPT_STATUS & UDP_INTERRUPT_ENDPOINT(1)) {
467 HandleRxdData();
468 ret = TRUE;
469 }
470
471 return ret;
472 }
Impressum, Datenschutz