X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/1f42ccddfb11c6a862c6cb058af765acae038e4d..a2d058f3aa4bde441b1a7912844d0eb6d13f455d:/common/usb_cdc.c diff --git a/common/usb_cdc.c b/common/usb_cdc.c index 5577e354..84aa2c06 100644 --- a/common/usb_cdc.c +++ b/common/usb_cdc.c @@ -36,6 +36,8 @@ #include "at91sam7s512.h" #include "config_gpio.h" + +#define AT91C_EP_CONTROL 0 #define AT91C_EP_IN_SIZE 0x40 #define AT91C_EP_OUT 1 #define AT91C_EP_OUT_SIZE 0x40 @@ -197,7 +199,30 @@ const char* getStringDescriptor(uint8_t idx) } } -/* USB standard request code */ +// Bitmap for all status bits in CSR which must be written as 1 to cause no effect +#define REG_NO_EFFECT_1_ALL AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 \ + |AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP \ + |AT91C_UDP_TXCOMP + +// Clear flags in the UDP_CSR register +#define UDP_CLEAR_EP_FLAGS(endpoint, flags) { \ + volatile unsigned int reg; \ + reg = pUdp->UDP_CSR[(endpoint)]; \ + reg |= REG_NO_EFFECT_1_ALL; \ + reg &= ~(flags); \ + pUdp->UDP_CSR[(endpoint)] = reg; \ +} + +// Set flags in the UDP_CSR register +#define UDP_SET_EP_FLAGS(endpoint, flags) { \ + volatile unsigned int reg; \ + reg = pUdp->UDP_CSR[(endpoint)]; \ + reg |= REG_NO_EFFECT_1_ALL; \ + reg |= (flags); \ + pUdp->UDP_CSR[(endpoint)] = reg; \ +} + +/* USB standard request codes */ #define STD_GET_STATUS_ZERO 0x0080 #define STD_GET_STATUS_INTERFACE 0x0081 #define STD_GET_STATUS_ENDPOINT 0x0082 @@ -305,7 +330,7 @@ bool usb_check() { // Enable the function pUdp->UDP_FADDR = AT91C_UDP_FEN; // Configure endpoint 0 - pUdp->UDP_CSR[0] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL); + pUdp->UDP_CSR[AT91C_EP_CONTROL] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL); } else if (isr & AT91C_UDP_EPINT0) { pUdp->UDP_ICR = AT91C_UDP_EPINT0; @@ -354,7 +379,7 @@ uint32_t usb_read(byte_t* data, size_t len) { len -= packetSize; while(packetSize--) data[nbBytesRcv++] = pUdp->UDP_FDR[AT91C_EP_OUT]; - pUdp->UDP_CSR[AT91C_EP_OUT] &= ~(bank); + UDP_CLEAR_EP_FLAGS(AT91C_EP_OUT, bank); if (bank == AT91C_UDP_RX_DATA_BK0) { bank = AT91C_UDP_RX_DATA_BK1; } else { @@ -383,7 +408,7 @@ uint32_t usb_write(const byte_t* data, const size_t len) { cpt = MIN(length, AT91C_EP_IN_SIZE-1); length -= cpt; while (cpt--) pUdp->UDP_FDR[AT91C_EP_IN] = *data++; - pUdp->UDP_CSR[AT91C_EP_IN] |= AT91C_UDP_TXPKTRDY; + UDP_SET_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXPKTRDY); while (length) { // Fill the second bank @@ -394,9 +419,9 @@ uint32_t usb_write(const byte_t* data, const size_t len) { while (!(pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)) { if (!usb_check()) return length; } - pUdp->UDP_CSR[AT91C_EP_IN] &= ~(AT91C_UDP_TXCOMP); + UDP_CLEAR_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXCOMP); while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP); - pUdp->UDP_CSR[AT91C_EP_IN] |= AT91C_UDP_TXPKTRDY; + UDP_SET_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXPKTRDY); } // Wait for the end of transfer @@ -404,7 +429,7 @@ uint32_t usb_write(const byte_t* data, const size_t len) { if (!usb_check()) return length; } - pUdp->UDP_CSR[AT91C_EP_IN] &= ~(AT91C_UDP_TXCOMP); + UDP_CLEAR_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXCOMP); while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP); return length; @@ -428,27 +453,27 @@ static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t leng while (cpt--) pUdp->UDP_FDR[0] = *pData++; - if (pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP) { - pUdp->UDP_CSR[0] &= ~(AT91C_UDP_TXCOMP); - while (pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP); + if (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) { + UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXCOMP); + while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP); } - pUdp->UDP_CSR[0] |= AT91C_UDP_TXPKTRDY; + UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXPKTRDY); do { - csr = pUdp->UDP_CSR[0]; + csr = pUdp->UDP_CSR[AT91C_EP_CONTROL]; // Data IN stage has been stopped by a status OUT if (csr & AT91C_UDP_RX_DATA_BK0) { - pUdp->UDP_CSR[0] &= ~(AT91C_UDP_RX_DATA_BK0); + UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_RX_DATA_BK0); return; } } while ( !(csr & AT91C_UDP_TXCOMP) ); } while (length); - if (pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP) { - pUdp->UDP_CSR[0] &= ~(AT91C_UDP_TXCOMP); - while (pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP); + if (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) { + UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXCOMP); + while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP); } } @@ -457,10 +482,10 @@ static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t leng //* \brief Send zero length packet through the control endpoint //*---------------------------------------------------------------------------- void AT91F_USB_SendZlp(AT91PS_UDP pUdp) { - pUdp->UDP_CSR[0] |= AT91C_UDP_TXPKTRDY; - while ( !(pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP) ); - pUdp->UDP_CSR[0] &= ~(AT91C_UDP_TXCOMP); - while (pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP); + UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXPKTRDY); + while ( !(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) ); + UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXCOMP); + while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP); } //*---------------------------------------------------------------------------- @@ -468,10 +493,10 @@ void AT91F_USB_SendZlp(AT91PS_UDP pUdp) { //* \brief Stall the control endpoint //*---------------------------------------------------------------------------- void AT91F_USB_SendStall(AT91PS_UDP pUdp) { - pUdp->UDP_CSR[0] |= AT91C_UDP_FORCESTALL; - while ( !(pUdp->UDP_CSR[0] & AT91C_UDP_ISOERROR) ); - pUdp->UDP_CSR[0] &= ~(AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR); - while (pUdp->UDP_CSR[0] & (AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR)); + UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_FORCESTALL); + while ( !(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_ISOERROR) ); + UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR); + while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & (AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR)); } //*---------------------------------------------------------------------------- @@ -482,7 +507,7 @@ void AT91F_CDC_Enumerate() { byte_t bmRequestType, bRequest; uint16_t wValue, wIndex, wLength, wStatus; - if ( !(pUdp->UDP_CSR[0] & AT91C_UDP_RXSETUP) ) + if ( !(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RXSETUP) ) return; bmRequestType = pUdp->UDP_FDR[0]; @@ -495,11 +520,11 @@ void AT91F_CDC_Enumerate() { wLength |= (pUdp->UDP_FDR[0] << 8); if (bmRequestType & 0x80) { - pUdp->UDP_CSR[0] |= AT91C_UDP_DIR; - while ( !(pUdp->UDP_CSR[0] & AT91C_UDP_DIR) ); + UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_DIR); + while ( !(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_DIR) ); } - pUdp->UDP_CSR[0] &= ~AT91C_UDP_RXSETUP; - while ( (pUdp->UDP_CSR[0] & AT91C_UDP_RXSETUP) ); + UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_RXSETUP); + while ( (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RXSETUP) ); // Handle supported standard device request Cf Table 9-3 in USB specification Rev 1.1 switch ((bRequest << 8) | bmRequestType) { @@ -595,8 +620,8 @@ void AT91F_CDC_Enumerate() { // handle CDC class requests case SET_LINE_CODING: - while ( !(pUdp->UDP_CSR[0] & AT91C_UDP_RX_DATA_BK0) ); - pUdp->UDP_CSR[0] &= ~(AT91C_UDP_RX_DATA_BK0); + while ( !(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RX_DATA_BK0) ); + UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_RX_DATA_BK0); AT91F_USB_SendZlp(pUdp); break; case GET_LINE_CODING: