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