]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - common/usb.c
added missing files, part2
[proxmark3-svn] / common / usb.c
index cf324f190d82ac91ebb019c62c17940be0b09a7a..e1fc9b25b0dee3aca104dc6c542aafedd6fd275a 100644 (file)
@@ -1,20 +1,25 @@
 //-----------------------------------------------------------------------------
-// My USB driver. This has to be common, because it exists in both the
-// bootrom and the application.
 // Jonathan Westhues, split Aug 14 2005
+//
+// This code is licensed to you under the terms of the GNU GPL, version 2 or,
+// at your option, any later version. See the LICENSE.txt file for the text of
+// the license.
 //-----------------------------------------------------------------------------
-#include <proxmark3.h>
+// The common USB driver used for both the bootloader and the application.
+//-----------------------------------------------------------------------------
+
+#include "proxmark3.h"
 
 #define min(a, b) (((a) > (b)) ? (b) : (a))
 
 #define USB_REPORT_PACKET_SIZE 64
 
 typedef struct PACKED {
-       BYTE    bmRequestType;
-       BYTE    bRequest;
-       WORD    wValue;
-       WORD    wIndex;
-       WORD    wLength;
+       uint8_t         bmRequestType;
+       uint8_t         bRequest;
+       uint16_t        wValue;
+       uint16_t        wIndex;
+       uint16_t        wLength;
 } UsbSetupData;
 
 #define USB_REQUEST_GET_STATUS                                 0
@@ -42,7 +47,7 @@ typedef struct PACKED {
 
 #define USB_DEVICE_CLASS_HID                                   0x03
 
-static const BYTE HidReportDescriptor[] = {
+static const uint8_t HidReportDescriptor[] = {
        0x06,0xA0,0xFF, // Usage Page (vendor defined) FFA0
        0x09,0x01,              // Usage (vendor defined)
        0xA1,0x01,              // Collection (Application)
@@ -77,7 +82,7 @@ static const BYTE HidReportDescriptor[] = {
        0xC0,                   // End Collection
 };
 
-static const BYTE DeviceDescriptor[] = {
+static const uint8_t DeviceDescriptor[] = {
        0x12,                   // Descriptor length (18 bytes)
        0x01,                   // Descriptor type (Device)
        0x10,0x01,              // Complies with USB Spec. Release (0110h = release 1.10)
@@ -90,11 +95,11 @@ static const BYTE DeviceDescriptor[] = {
        0x01,0x00,              // Device release number (0001)
        0x01,                   // Manufacturer string descriptor index
        0x02,                   // Product string descriptor index
-       0x00,                   // Serial Number string descriptor index (None)
+       0x03,                   // Serial Number string descriptor index
        0x01,                   // Number of possible configurations (1)
 };
 
-static const BYTE ConfigurationDescriptor[] = {
+static const uint8_t ConfigurationDescriptor[] = {
        0x09,                   // Descriptor length (9 bytes)
        0x02,                   // Descriptor type (Configuration)
        0x29,0x00,              // Total data length (41 bytes)
@@ -142,14 +147,14 @@ static const BYTE ConfigurationDescriptor[] = {
        0x01,                   // Polling interval (1 ms)
 };
 
-static const BYTE StringDescriptor0[] = {
+static const uint8_t StringDescriptor0[] = {
        0x04,                   // Length
        0x03,                   // Type is string
        0x09,                   // English
        0x04,                   //  US
 };
 
-static const BYTE StringDescriptor1[] = {
+static const uint8_t StringDescriptor1[] = {
        24,                             // Length
        0x03,                   // Type is string
        'J', 0x00,
@@ -165,7 +170,7 @@ static const BYTE StringDescriptor1[] = {
        's', 0x00,
 };
 
-static const BYTE StringDescriptor2[] = {
+static const uint8_t StringDescriptor2[] = {
        54,                             // Length
        0x03,                   // Type is string
        'P', 0x00,
@@ -196,19 +201,35 @@ static const BYTE StringDescriptor2[] = {
        't', 0x00,
 };
 
-static const BYTE * const StringDescriptors[] = {
+// Serial Number
+// TODO: Pick yours! Don't forget to modify the length, if needed.
+static const uint8_t StringDescriptor3[] = {
+       18,             // Length
+       0x03,           // Type is string
+       'C', 0x00,
+       'h', 0x00,
+       'a', 0x00,
+       'n', 0x00,
+       'g', 0x00,
+       'e', 0x00,
+       'M', 0x00,
+       'e', 0x00,
+};
+
+static const uint8_t * const StringDescriptors[] = {
        StringDescriptor0,
        StringDescriptor1,
        StringDescriptor2,
+       StringDescriptor3,
 };
 
 
-static BYTE UsbBuffer[64];
+static uint8_t UsbBuffer[64];
 static int  UsbSoFarCount;
 
-static BYTE CurrentConfiguration;
+static uint8_t CurrentConfiguration;
 
-static void UsbSendEp0(const BYTE *data, int len)
+static void UsbSendEp0(const uint8_t *data, int len)
 {
        int thisTime, i;
 
@@ -278,7 +299,7 @@ static void HandleRxdSetupData(void)
        UsbSetupData usd;
 
        for(i = 0; i < sizeof(usd); i++) {
-               ((BYTE *)&usd)[i] = AT91C_BASE_UDP->UDP_FDR[0];
+               ((uint8_t *)&usd)[i] = AT91C_BASE_UDP->UDP_FDR[0];
        }
 
        if(usd.bmRequestType & 0x80) {
@@ -294,19 +315,19 @@ static void HandleRxdSetupData(void)
        switch(usd.bRequest) {
                case USB_REQUEST_GET_DESCRIPTOR:
                        if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_DEVICE) {
-                               UsbSendEp0((BYTE *)&DeviceDescriptor,
+                               UsbSendEp0((uint8_t *)&DeviceDescriptor,
                                        min(sizeof(DeviceDescriptor), usd.wLength));
                        } else if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_CONFIGURATION) {
-                               UsbSendEp0((BYTE *)&ConfigurationDescriptor,
+                               UsbSendEp0((uint8_t *)&ConfigurationDescriptor,
                                        min(sizeof(ConfigurationDescriptor), usd.wLength));
                        } else if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_STRING) {
-                               const BYTE *s = StringDescriptors[usd.wValue & 0xff];
+                               const uint8_t *s = StringDescriptors[usd.wValue & 0xff];
                                UsbSendEp0(s, min(s[0], usd.wLength));
                        } else if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_HID_REPORT) {
-                               UsbSendEp0((BYTE *)&HidReportDescriptor,
+                               UsbSendEp0((uint8_t *)&HidReportDescriptor,
                                        min(sizeof(HidReportDescriptor), usd.wLength));
                        } else {
-                               *((DWORD *)0x00200000) = usd.wValue;
+                               *((uint32_t *)0x00200000) = usd.wValue;
                        }
                        break;
 
@@ -326,8 +347,8 @@ static void HandleRxdSetupData(void)
 
                case USB_REQUEST_GET_STATUS: {
                        if(usd.bmRequestType & 0x80) {
-                               WORD w = 0;
-                               UsbSendEp0((BYTE *)&w, sizeof(w));
+                               uint16_t w = 0;
+                               UsbSendEp0((uint8_t *)&w, sizeof(w));
                        }
                        break;
                }
@@ -348,7 +369,7 @@ static void HandleRxdSetupData(void)
                        break;
 
                case USB_REQUEST_GET_INTERFACE: {
-                       BYTE b = 0;
+                       uint8_t b = 0;
                        UsbSendEp0(&b, sizeof(b));
                        break;
                }
@@ -368,7 +389,7 @@ static void HandleRxdSetupData(void)
        }
 }
 
-void UsbSendPacket(BYTE *packet, int len)
+void UsbSendPacket(uint8_t *packet, int len)
 {
        int i, thisTime;
 
@@ -380,12 +401,15 @@ void UsbSendPacket(BYTE *packet, int len)
                }
                AT91C_BASE_UDP->UDP_CSR[2] |= AT91C_UDP_TXPKTRDY;
 
-               while(!(AT91C_BASE_UDP->UDP_CSR[2] & AT91C_UDP_TXCOMP))
-                       ;
+               while(!(AT91C_BASE_UDP->UDP_CSR[2] & AT91C_UDP_TXCOMP)) {
+            WDT_HIT();
+        }
+
                AT91C_BASE_UDP->UDP_CSR[2] &= ~AT91C_UDP_TXCOMP;
 
-               while(AT91C_BASE_UDP->UDP_CSR[2] & AT91C_UDP_TXCOMP)
-                       ;
+               while(AT91C_BASE_UDP->UDP_CSR[2] & AT91C_UDP_TXCOMP) {
+            WDT_HIT();
+        }
 
                len -= thisTime;
                packet += thisTime;
@@ -405,8 +429,9 @@ static void HandleRxdData(void)
                }
 
                AT91C_BASE_UDP->UDP_CSR[1] &= ~AT91C_UDP_RX_DATA_BK0;
-               while(AT91C_BASE_UDP->UDP_CSR[1] & AT91C_UDP_RX_DATA_BK0)
-                       ;
+               while(AT91C_BASE_UDP->UDP_CSR[1] & AT91C_UDP_RX_DATA_BK0) {
+            WDT_HIT();
+        }
 
                if(UsbSoFarCount >= 64) {
                        UsbPacketReceived(UsbBuffer, UsbSoFarCount);
@@ -423,14 +448,17 @@ static void HandleRxdData(void)
                }
 
                AT91C_BASE_UDP->UDP_CSR[1] &= ~AT91C_UDP_RX_DATA_BK1;
-               while(AT91C_BASE_UDP->UDP_CSR[1] & AT91C_UDP_RX_DATA_BK1)
-                       ;
-
+               while(AT91C_BASE_UDP->UDP_CSR[1] & AT91C_UDP_RX_DATA_BK1) {
+            WDT_HIT();
+        }
+        
                if(UsbSoFarCount >= 64) {
                        UsbPacketReceived(UsbBuffer, UsbSoFarCount);
                        UsbSoFarCount = 0;
                }
        }
+    
+    WDT_HIT();
 }
 
 void UsbStart(void)
@@ -451,7 +479,7 @@ void UsbStart(void)
        }
 }
 
-BOOL UsbConnected()
+int UsbConnected()
 {
        if (AT91C_BASE_UDP->UDP_GLBSTATE & AT91C_UDP_CONFG)
                return TRUE;
@@ -459,9 +487,9 @@ BOOL UsbConnected()
                return FALSE;
 }
 
-BOOL UsbPoll(BOOL blinkLeds)
+int UsbPoll(int blinkLeds)
 {
-       BOOL ret = FALSE;
+       int ret = FALSE;
 
        if(AT91C_BASE_UDP->UDP_ISR & AT91C_UDP_ENDBUSRES) {
                AT91C_BASE_UDP->UDP_ICR = AT91C_UDP_ENDBUSRES;
Impressum, Datenschutz