]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - common/usb.c
alphabet ;)
[proxmark3-svn] / common / usb.c
index f0b9529175ad664e39d848f71c399a5008355717..2e8365e45904e257434e2e605c093152d78dab4a 100644 (file)
@@ -217,45 +217,58 @@ static void UsbSendEp0(const BYTE *data, int len)
                len -= thisTime;\r
 \r
                for(i = 0; i < thisTime; i++) {\r
-                       UDP_ENDPOINT_FIFO(0) = *data;\r
+                       AT91C_BASE_UDP->UDP_FDR[0] = *data;\r
                        data++;\r
                }\r
 \r
-               if(UDP_ENDPOINT_CSR(0) & UDP_CSR_TX_PACKET_ACKED) {\r
-                       UDP_ENDPOINT_CSR(0) &= ~UDP_CSR_TX_PACKET_ACKED;\r
-                       while(UDP_ENDPOINT_CSR(0) & UDP_CSR_TX_PACKET_ACKED)\r
+               if(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP) {\r
+                       AT91C_BASE_UDP->UDP_CSR[0] &= ~AT91C_UDP_TXCOMP;\r
+                       while(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP)\r
                                ;\r
                }\r
 \r
-               UDP_ENDPOINT_CSR(0) |= UDP_CSR_TX_PACKET;\r
+               AT91C_BASE_UDP->UDP_CSR[0] |= AT91C_UDP_TXPKTRDY;\r
 \r
                do {\r
-                       if(UDP_ENDPOINT_CSR(0) & UDP_CSR_RX_PACKET_RECEIVED_BANK_0) {\r
+                       if(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_RX_DATA_BK0) {\r
                                // This means that the host is trying to write to us, so\r
                                // abandon our write to them.\r
-                               UDP_ENDPOINT_CSR(0) &= ~UDP_CSR_RX_PACKET_RECEIVED_BANK_0;\r
+                               AT91C_BASE_UDP->UDP_CSR[0] &= ~AT91C_UDP_RX_DATA_BK0;\r
                                return;\r
                        }\r
-               } while(!(UDP_ENDPOINT_CSR(0) & UDP_CSR_TX_PACKET_ACKED));\r
+               } while(!(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP));\r
        } while(len > 0);\r
 \r
-       if(UDP_ENDPOINT_CSR(0) & UDP_CSR_TX_PACKET_ACKED) {\r
-               UDP_ENDPOINT_CSR(0) &= ~UDP_CSR_TX_PACKET_ACKED;\r
-               while(UDP_ENDPOINT_CSR(0) & UDP_CSR_TX_PACKET_ACKED)\r
+       if(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP) {\r
+               AT91C_BASE_UDP->UDP_CSR[0] &= ~AT91C_UDP_TXCOMP;\r
+               while(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP)\r
                        ;\r
        }\r
 }\r
 \r
 static void UsbSendZeroLength(void)\r
 {\r
-       UDP_ENDPOINT_CSR(0) |= UDP_CSR_TX_PACKET;\r
+       AT91C_BASE_UDP->UDP_CSR[0] |= AT91C_UDP_TXPKTRDY;\r
 \r
-       while(!(UDP_ENDPOINT_CSR(0) & UDP_CSR_TX_PACKET_ACKED))\r
+       while(!(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP))\r
                ;\r
 \r
-       UDP_ENDPOINT_CSR(0) &= ~UDP_CSR_TX_PACKET_ACKED;\r
+       AT91C_BASE_UDP->UDP_CSR[0] &= ~AT91C_UDP_TXCOMP;\r
 \r
-       while(UDP_ENDPOINT_CSR(0) & UDP_CSR_TX_PACKET_ACKED)\r
+       while(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP)\r
+               ;\r
+}\r
+\r
+static void UsbSendStall(void)\r
+{\r
+       AT91C_BASE_UDP->UDP_CSR[0] |= AT91C_UDP_FORCESTALL;\r
+\r
+       while(!(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_STALLSENT))\r
+               ;\r
+\r
+       AT91C_BASE_UDP->UDP_CSR[0] &= ~AT91C_UDP_STALLSENT;\r
+\r
+       while(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_STALLSENT)\r
                ;\r
 }\r
 \r
@@ -265,17 +278,17 @@ static void HandleRxdSetupData(void)
        UsbSetupData usd;\r
 \r
        for(i = 0; i < sizeof(usd); i++) {\r
-               ((BYTE *)&usd)[i] = UDP_ENDPOINT_FIFO(0);\r
+               ((BYTE *)&usd)[i] = AT91C_BASE_UDP->UDP_FDR[0];\r
        }\r
 \r
        if(usd.bmRequestType & 0x80) {\r
-               UDP_ENDPOINT_CSR(0) |= UDP_CSR_CONTROL_DATA_DIR;\r
-               while(!(UDP_ENDPOINT_CSR(0) & UDP_CSR_CONTROL_DATA_DIR))\r
+               AT91C_BASE_UDP->UDP_CSR[0] |= AT91C_UDP_DIR;\r
+               while(!(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_DIR))\r
                        ;\r
        }\r
 \r
-       UDP_ENDPOINT_CSR(0) &= ~UDP_CSR_RX_HAVE_READ_SETUP_DATA;\r
-       while(UDP_ENDPOINT_CSR(0) & UDP_CSR_RX_HAVE_READ_SETUP_DATA)\r
+       AT91C_BASE_UDP->UDP_CSR[0] &= ~AT91C_UDP_RXSETUP;\r
+       while(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_RXSETUP)\r
                ;\r
 \r
        switch(usd.bRequest) {\r
@@ -299,11 +312,11 @@ static void HandleRxdSetupData(void)
 \r
                case USB_REQUEST_SET_ADDRESS:\r
                        UsbSendZeroLength();\r
-                       UDP_FUNCTION_ADDR = UDP_FUNCTION_ADDR_ENABLED | usd.wValue ;\r
+                       AT91C_BASE_UDP->UDP_FADDR = AT91C_UDP_FEN | usd.wValue ;\r
                        if(usd.wValue != 0) {\r
-                               UDP_GLOBAL_STATE = UDP_GLOBAL_STATE_ADDRESSED;\r
+                               AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_FADDEN;\r
                        } else {\r
-                               UDP_GLOBAL_STATE = 0;\r
+                               AT91C_BASE_UDP->UDP_GLBSTATE = 0;\r
                        }\r
                        break;\r
 \r
@@ -321,15 +334,15 @@ static void HandleRxdSetupData(void)
                case USB_REQUEST_SET_CONFIGURATION:\r
                        CurrentConfiguration = usd.wValue;\r
                        if(CurrentConfiguration) {\r
-                               UDP_GLOBAL_STATE = UDP_GLOBAL_STATE_CONFIGURED;\r
-                               UDP_ENDPOINT_CSR(1) = UDP_CSR_ENABLE_EP |\r
-                                       UDP_CSR_EPTYPE_INTERRUPT_OUT;\r
-                               UDP_ENDPOINT_CSR(2) = UDP_CSR_ENABLE_EP |\r
-                                       UDP_CSR_EPTYPE_INTERRUPT_IN;\r
+                               AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_CONFG;\r
+                               AT91C_BASE_UDP->UDP_CSR[1] = AT91C_UDP_EPEDS |\r
+                                       AT91C_UDP_EPTYPE_INT_OUT;\r
+                               AT91C_BASE_UDP->UDP_CSR[2] = AT91C_UDP_EPEDS |\r
+                                       AT91C_UDP_EPTYPE_INT_IN;\r
                        } else {\r
-                               UDP_GLOBAL_STATE = UDP_GLOBAL_STATE_ADDRESSED;\r
-                               UDP_ENDPOINT_CSR(1) = 0;\r
-                               UDP_ENDPOINT_CSR(2) = 0;\r
+                               AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_FADDEN;\r
+                               AT91C_BASE_UDP->UDP_CSR[1] = 0;\r
+                               AT91C_BASE_UDP->UDP_CSR[2] = 0;\r
                        }\r
                        UsbSendZeroLength();\r
                        break;\r
@@ -346,6 +359,8 @@ static void HandleRxdSetupData(void)
 \r
                case USB_REQUEST_CLEAR_FEATURE:\r
                case USB_REQUEST_SET_FEATURE:\r
+                       UsbSendStall();\r
+                       break;\r
                case USB_REQUEST_SET_DESCRIPTOR:\r
                case USB_REQUEST_SYNC_FRAME:\r
                default:\r
@@ -361,15 +376,15 @@ void UsbSendPacket(BYTE *packet, int len)
                thisTime = min(len, 8);\r
 \r
                for(i = 0; i < thisTime; i++) {\r
-                       UDP_ENDPOINT_FIFO(2) = packet[i];\r
+                       AT91C_BASE_UDP->UDP_FDR[2] = packet[i];\r
                }\r
-               UDP_ENDPOINT_CSR(2) |= UDP_CSR_TX_PACKET;\r
+               AT91C_BASE_UDP->UDP_CSR[2] |= AT91C_UDP_TXPKTRDY;\r
 \r
-               while(!(UDP_ENDPOINT_CSR(2) & UDP_CSR_TX_PACKET_ACKED))\r
+               while(!(AT91C_BASE_UDP->UDP_CSR[2] & AT91C_UDP_TXCOMP))\r
                        ;\r
-               UDP_ENDPOINT_CSR(2) &= ~UDP_CSR_TX_PACKET_ACKED;\r
+               AT91C_BASE_UDP->UDP_CSR[2] &= ~AT91C_UDP_TXCOMP;\r
 \r
-               while(UDP_ENDPOINT_CSR(2) & UDP_CSR_TX_PACKET_ACKED)\r
+               while(AT91C_BASE_UDP->UDP_CSR[2] & AT91C_UDP_TXCOMP)\r
                        ;\r
 \r
                len -= thisTime;\r
@@ -381,16 +396,16 @@ static void HandleRxdData(void)
 {\r
        int i, len;\r
 \r
-       if(UDP_ENDPOINT_CSR(1) & UDP_CSR_RX_PACKET_RECEIVED_BANK_0) {\r
-               len = UDP_CSR_BYTES_RECEIVED(UDP_ENDPOINT_CSR(1));\r
+       if(AT91C_BASE_UDP->UDP_CSR[1] & AT91C_UDP_RX_DATA_BK0) {\r
+               len = UDP_CSR_BYTES_RECEIVED(AT91C_BASE_UDP->UDP_CSR[1]);\r
 \r
                for(i = 0; i < len; i++) {\r
-                       UsbBuffer[UsbSoFarCount] = UDP_ENDPOINT_FIFO(1);\r
+                       UsbBuffer[UsbSoFarCount] = AT91C_BASE_UDP->UDP_FDR[1];\r
                        UsbSoFarCount++;\r
                }\r
 \r
-               UDP_ENDPOINT_CSR(1) &= ~UDP_CSR_RX_PACKET_RECEIVED_BANK_0;\r
-               while(UDP_ENDPOINT_CSR(1) & UDP_CSR_RX_PACKET_RECEIVED_BANK_0)\r
+               AT91C_BASE_UDP->UDP_CSR[1] &= ~AT91C_UDP_RX_DATA_BK0;\r
+               while(AT91C_BASE_UDP->UDP_CSR[1] & AT91C_UDP_RX_DATA_BK0)\r
                        ;\r
 \r
                if(UsbSoFarCount >= 64) {\r
@@ -399,16 +414,16 @@ static void HandleRxdData(void)
                }\r
        }\r
 \r
-       if(UDP_ENDPOINT_CSR(1) & UDP_CSR_RX_PACKET_RECEIVED_BANK_1) {\r
-               len = UDP_CSR_BYTES_RECEIVED(UDP_ENDPOINT_CSR(1));\r
+       if(AT91C_BASE_UDP->UDP_CSR[1] & AT91C_UDP_RX_DATA_BK1) {\r
+               len = UDP_CSR_BYTES_RECEIVED(AT91C_BASE_UDP->UDP_CSR[1]);\r
 \r
                for(i = 0; i < len; i++) {\r
-                       UsbBuffer[UsbSoFarCount] = UDP_ENDPOINT_FIFO(1);\r
+                       UsbBuffer[UsbSoFarCount] = AT91C_BASE_UDP->UDP_FDR[1];\r
                        UsbSoFarCount++;\r
                }\r
 \r
-               UDP_ENDPOINT_CSR(1) &= ~UDP_CSR_RX_PACKET_RECEIVED_BANK_1;\r
-               while(UDP_ENDPOINT_CSR(1) & UDP_CSR_RX_PACKET_RECEIVED_BANK_1)\r
+               AT91C_BASE_UDP->UDP_CSR[1] &= ~AT91C_UDP_RX_DATA_BK1;\r
+               while(AT91C_BASE_UDP->UDP_CSR[1] & AT91C_UDP_RX_DATA_BK1)\r
                        ;\r
 \r
                if(UsbSoFarCount >= 64) {\r
@@ -431,39 +446,47 @@ void UsbStart(void)
 \r
        USB_D_PLUS_PULLUP_ON();\r
 \r
-       if(UDP_INTERRUPT_STATUS & UDP_INTERRUPT_END_OF_BUS_RESET) {\r
-               UDP_INTERRUPT_CLEAR = UDP_INTERRUPT_END_OF_BUS_RESET;\r
+       if(AT91C_BASE_UDP->UDP_ISR & AT91C_UDP_ENDBUSRES) {\r
+               AT91C_BASE_UDP->UDP_ICR = AT91C_UDP_ENDBUSRES;\r
        }\r
 }\r
 \r
+BOOL UsbConnected()\r
+{\r
+       if (AT91C_BASE_UDP->UDP_GLBSTATE & AT91C_UDP_CONFG)\r
+               return TRUE;\r
+       else\r
+               return FALSE;\r
+}\r
+\r
 BOOL UsbPoll(BOOL blinkLeds)\r
 {\r
        BOOL ret = FALSE;\r
 \r
-       if(UDP_INTERRUPT_STATUS & UDP_INTERRUPT_END_OF_BUS_RESET) {\r
-               UDP_INTERRUPT_CLEAR = UDP_INTERRUPT_END_OF_BUS_RESET;\r
+       if(AT91C_BASE_UDP->UDP_ISR & AT91C_UDP_ENDBUSRES) {\r
+               AT91C_BASE_UDP->UDP_ICR = AT91C_UDP_ENDBUSRES;\r
 \r
                // following a reset we should be ready to receive a setup packet\r
-               UDP_RESET_ENDPOINT = 0xf;\r
-               UDP_RESET_ENDPOINT = 0;\r
+               AT91C_BASE_UDP->UDP_RSTEP = 0xf;\r
+               AT91C_BASE_UDP->UDP_RSTEP = 0;\r
 \r
-               UDP_FUNCTION_ADDR = UDP_FUNCTION_ADDR_ENABLED;\r
+               AT91C_BASE_UDP->UDP_FADDR = AT91C_UDP_FEN;\r
 \r
-               UDP_ENDPOINT_CSR(0) = UDP_CSR_EPTYPE_CONTROL | UDP_CSR_ENABLE_EP;\r
+               AT91C_BASE_UDP->UDP_CSR[0] = AT91C_UDP_EPTYPE_CTRL | AT91C_UDP_EPEDS;\r
 \r
                CurrentConfiguration = 0;\r
 \r
                ret = TRUE;\r
        }\r
 \r
-       if(UDP_INTERRUPT_STATUS & UDP_INTERRUPT_ENDPOINT(0)) {\r
-               if(UDP_ENDPOINT_CSR(0) & UDP_CSR_RX_HAVE_READ_SETUP_DATA) {\r
+       if(AT91C_BASE_UDP->UDP_ISR & UDP_INTERRUPT_ENDPOINT(0)) {\r
+               if(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_RXSETUP) {\r
                        HandleRxdSetupData();\r
                        ret = TRUE;\r
                }\r
        }\r
 \r
-       if(UDP_INTERRUPT_STATUS & UDP_INTERRUPT_ENDPOINT(1)) {\r
+       if(AT91C_BASE_UDP->UDP_ISR & UDP_INTERRUPT_ENDPOINT(1)) {\r
                HandleRxdData();\r
                ret = TRUE;\r
        }\r
Impressum, Datenschutz