123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625 |
- #include "usbportability.h"
- #include "usbdrv.h"
- #include "oddebug.h"
- uchar usbRxBuf[2*USB_BUFSIZE];
- uchar usbInputBufOffset;
- uchar usbDeviceAddr;
- uchar usbNewDeviceAddr;
- uchar usbConfiguration;
- volatile schar usbRxLen;
- uchar usbCurrentTok;
- uchar usbRxToken;
- volatile uchar usbTxLen = USBPID_NAK;
- uchar usbTxBuf[USB_BUFSIZE];
- #if USB_COUNT_SOF
- volatile uchar usbSofCount;
- #endif
- #if USB_CFG_HAVE_INTRIN_ENDPOINT && !USB_CFG_SUPPRESS_INTR_CODE
- usbTxStatus_t usbTxStatus1;
- # if USB_CFG_HAVE_INTRIN_ENDPOINT3
- usbTxStatus_t usbTxStatus3;
- # endif
- #endif
- #if USB_CFG_CHECK_DATA_TOGGLING
- uchar usbCurrentDataToken;
- #endif
- uchar *usbMsgPtr;
- static usbMsgLen_t usbMsgLen = USB_NO_MSG;
- static uchar usbMsgFlags;
- #define USB_FLG_MSGPTR_IS_ROM (1<<6)
- #define USB_FLG_USE_USER_RW (1<<7)
- #if USB_CFG_DESCR_PROPS_STRINGS == 0
- #if USB_CFG_DESCR_PROPS_STRING_0 == 0
- #undef USB_CFG_DESCR_PROPS_STRING_0
- #define USB_CFG_DESCR_PROPS_STRING_0 sizeof(usbDescriptorString0)
- const PROGMEM char usbDescriptorString0[] = {
- 4,
- 3,
- 0x09, 0x04,
- };
- #endif
- #if USB_CFG_DESCR_PROPS_STRING_VENDOR == 0 && USB_CFG_VENDOR_NAME_LEN
- #undef USB_CFG_DESCR_PROPS_STRING_VENDOR
- #define USB_CFG_DESCR_PROPS_STRING_VENDOR sizeof(usbDescriptorStringVendor)
- const PROGMEM int usbDescriptorStringVendor[] = {
- USB_STRING_DESCRIPTOR_HEADER(USB_CFG_VENDOR_NAME_LEN),
- USB_CFG_VENDOR_NAME
- };
- #endif
- #if USB_CFG_DESCR_PROPS_STRING_PRODUCT == 0 && USB_CFG_DEVICE_NAME_LEN
- #undef USB_CFG_DESCR_PROPS_STRING_PRODUCT
- #define USB_CFG_DESCR_PROPS_STRING_PRODUCT sizeof(usbDescriptorStringDevice)
- const PROGMEM int usbDescriptorStringDevice[] = {
- USB_STRING_DESCRIPTOR_HEADER(USB_CFG_DEVICE_NAME_LEN),
- USB_CFG_DEVICE_NAME
- };
- #endif
- #if USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER == 0 && USB_CFG_SERIAL_NUMBER_LEN
- #undef USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER
- #define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER sizeof(usbDescriptorStringSerialNumber)
- PROGMEM int usbDescriptorStringSerialNumber[] = {
- USB_STRING_DESCRIPTOR_HEADER(USB_CFG_SERIAL_NUMBER_LEN),
- USB_CFG_SERIAL_NUMBER
- };
- #endif
- #endif
- #if USB_CFG_DESCR_PROPS_DEVICE == 0
- #undef USB_CFG_DESCR_PROPS_DEVICE
- #define USB_CFG_DESCR_PROPS_DEVICE sizeof(usbDescriptorDevice)
- const PROGMEM char usbDescriptorDevice[] = {
- 18,
- USBDESCR_DEVICE,
- 0x10, 0x01,
- USB_CFG_DEVICE_CLASS,
- USB_CFG_DEVICE_SUBCLASS,
- 0,
- 8,
-
- (char)USB_CFG_VENDOR_ID,
- (char)USB_CFG_DEVICE_ID,
- USB_CFG_DEVICE_VERSION,
- USB_CFG_DESCR_PROPS_STRING_VENDOR != 0 ? 1 : 0,
- USB_CFG_DESCR_PROPS_STRING_PRODUCT != 0 ? 2 : 0,
- USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER != 0 ? 3 : 0,
- 1,
- };
- #endif
- #if USB_CFG_DESCR_PROPS_HID_REPORT != 0 && USB_CFG_DESCR_PROPS_HID == 0
- #undef USB_CFG_DESCR_PROPS_HID
- #define USB_CFG_DESCR_PROPS_HID 9
- #endif
- #if USB_CFG_DESCR_PROPS_CONFIGURATION == 0
- #undef USB_CFG_DESCR_PROPS_CONFIGURATION
- #define USB_CFG_DESCR_PROPS_CONFIGURATION sizeof(usbDescriptorConfiguration)
- PROGMEM char usbDescriptorConfiguration[] = {
- 9,
- USBDESCR_CONFIG,
- 18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 +
- (USB_CFG_DESCR_PROPS_HID & 0xff), 0,
-
- 1,
- 1,
- 0,
- #if USB_CFG_IS_SELF_POWERED
- (1 << 7) | USBATTR_SELFPOWER,
- #else
- (1 << 7),
- #endif
- USB_CFG_MAX_BUS_POWER/2,
- 9,
- USBDESCR_INTERFACE,
- 0,
- 0,
- USB_CFG_HAVE_INTRIN_ENDPOINT + USB_CFG_HAVE_INTRIN_ENDPOINT3,
- USB_CFG_INTERFACE_CLASS,
- USB_CFG_INTERFACE_SUBCLASS,
- USB_CFG_INTERFACE_PROTOCOL,
- 0,
- #if (USB_CFG_DESCR_PROPS_HID & 0xff)
- 9,
- USBDESCR_HID,
- 0x01, 0x01,
- 0x00,
- 0x01,
- 0x22,
- USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH, 0,
- #endif
- #if USB_CFG_HAVE_INTRIN_ENDPOINT
- 7,
- USBDESCR_ENDPOINT,
- (char)0x81,
- 0x03,
- 8, 0,
- USB_CFG_INTR_POLL_INTERVAL,
- #endif
- #if USB_CFG_HAVE_INTRIN_ENDPOINT3
- 7,
- USBDESCR_ENDPOINT,
- (char)(0x80 | USB_CFG_EP3_NUMBER),
- 0x03,
- 8, 0,
- USB_CFG_INTR_POLL_INTERVAL,
- #endif
- };
- #endif
- static inline void usbResetDataToggling(void)
- {
- #if USB_CFG_HAVE_INTRIN_ENDPOINT && !USB_CFG_SUPPRESS_INTR_CODE
- USB_SET_DATATOKEN1(USB_INITIAL_DATATOKEN);
- # if USB_CFG_HAVE_INTRIN_ENDPOINT3
- USB_SET_DATATOKEN3(USB_INITIAL_DATATOKEN);
- # endif
- #endif
- }
- static inline void usbResetStall(void)
- {
- #if USB_CFG_IMPLEMENT_HALT && USB_CFG_HAVE_INTRIN_ENDPOINT
- usbTxLen1 = USBPID_NAK;
- #if USB_CFG_HAVE_INTRIN_ENDPOINT3
- usbTxLen3 = USBPID_NAK;
- #endif
- #endif
- }
- #if !USB_CFG_SUPPRESS_INTR_CODE
- #if USB_CFG_HAVE_INTRIN_ENDPOINT
- static void usbGenericSetInterrupt(uchar *data, uchar len, usbTxStatus_t *txStatus)
- {
- uchar *p;
- char i;
- #if USB_CFG_IMPLEMENT_HALT
- if(usbTxLen1 == USBPID_STALL)
- return;
- #endif
- if(txStatus->len & 0x10){
- txStatus->buffer[0] ^= USBPID_DATA0 ^ USBPID_DATA1;
- }else{
- txStatus->len = USBPID_NAK;
- }
- p = txStatus->buffer + 1;
- i = len;
- do{
- *p++ = *data++;
- }while(--i > 0);
- usbCrc16Append(&txStatus->buffer[1], len);
- txStatus->len = len + 4;
- DBG2(0x21 + (((int)txStatus >> 3) & 3), txStatus->buffer, len + 3);
- }
- USB_PUBLIC void usbSetInterrupt(uchar *data, uchar len)
- {
- usbGenericSetInterrupt(data, len, &usbTxStatus1);
- }
- #endif
- #if USB_CFG_HAVE_INTRIN_ENDPOINT3
- USB_PUBLIC void usbSetInterrupt3(uchar *data, uchar len)
- {
- usbGenericSetInterrupt(data, len, &usbTxStatus3);
- }
- #endif
- #endif
- #if USB_CFG_USE_SWITCH_STATEMENT
- # define SWITCH_START(cmd) switch(cmd){{
- # define SWITCH_CASE(value) }break; case (value):{
- # define SWITCH_CASE2(v1,v2) }break; case (v1): case(v2):{
- # define SWITCH_CASE3(v1,v2,v3) }break; case (v1): case(v2): case(v3):{
- # define SWITCH_DEFAULT }break; default:{
- # define SWITCH_END }}
- #else
- # define SWITCH_START(cmd) {uchar _cmd = cmd; if(0){
- # define SWITCH_CASE(value) }else if(_cmd == (value)){
- # define SWITCH_CASE2(v1,v2) }else if(_cmd == (v1) || _cmd == (v2)){
- # define SWITCH_CASE3(v1,v2,v3) }else if(_cmd == (v1) || _cmd == (v2) || (_cmd == v3)){
- # define SWITCH_DEFAULT }else{
- # define SWITCH_END }}
- #endif
- #ifndef USB_RX_USER_HOOK
- #define USB_RX_USER_HOOK(data, len)
- #endif
- #ifndef USB_SET_ADDRESS_HOOK
- #define USB_SET_ADDRESS_HOOK()
- #endif
- #define GET_DESCRIPTOR(cfgProp, staticName) \
- if(cfgProp){ \
- if((cfgProp) & USB_PROP_IS_RAM) \
- flags = 0; \
- if((cfgProp) & USB_PROP_IS_DYNAMIC){ \
- len = usbFunctionDescriptor(rq); \
- }else{ \
- len = USB_PROP_LENGTH(cfgProp); \
- usbMsgPtr = (uchar *)(staticName); \
- } \
- }
- static inline usbMsgLen_t usbDriverDescriptor(usbRequest_t *rq)
- {
- usbMsgLen_t len = 0;
- uchar flags = USB_FLG_MSGPTR_IS_ROM;
- SWITCH_START(rq->wValue.bytes[1])
- SWITCH_CASE(USBDESCR_DEVICE)
- GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_DEVICE, usbDescriptorDevice)
- SWITCH_CASE(USBDESCR_CONFIG)
- GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_CONFIGURATION, usbDescriptorConfiguration)
- SWITCH_CASE(USBDESCR_STRING)
- #if USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC
- if(USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_RAM)
- flags = 0;
- len = usbFunctionDescriptor(rq);
- #else
- SWITCH_START(rq->wValue.bytes[0])
- SWITCH_CASE(0)
- GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_0, usbDescriptorString0)
- SWITCH_CASE(1)
- GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_VENDOR, usbDescriptorStringVendor)
- SWITCH_CASE(2)
- GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_PRODUCT, usbDescriptorStringDevice)
- SWITCH_CASE(3)
- GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER, usbDescriptorStringSerialNumber)
- SWITCH_DEFAULT
- if(USB_CFG_DESCR_PROPS_UNKNOWN & USB_PROP_IS_DYNAMIC){
- len = usbFunctionDescriptor(rq);
- }
- SWITCH_END
- #endif
- #if USB_CFG_DESCR_PROPS_HID_REPORT
- SWITCH_CASE(USBDESCR_HID)
- GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID, usbDescriptorConfiguration + 18)
- SWITCH_CASE(USBDESCR_HID_REPORT)
- GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID_REPORT, usbDescriptorHidReport)
- #endif
- SWITCH_DEFAULT
- if(USB_CFG_DESCR_PROPS_UNKNOWN & USB_PROP_IS_DYNAMIC){
- len = usbFunctionDescriptor(rq);
- }
- SWITCH_END
- usbMsgFlags = flags;
- return len;
- }
- static inline usbMsgLen_t usbDriverSetup(usbRequest_t *rq)
- {
- uchar len = 0, *dataPtr = usbTxBuf + 9;
- uchar value = rq->wValue.bytes[0];
- #if USB_CFG_IMPLEMENT_HALT
- uchar index = rq->wIndex.bytes[0];
- #endif
- dataPtr[0] = 0;
- SWITCH_START(rq->bRequest)
- SWITCH_CASE(USBRQ_GET_STATUS)
- uchar recipient = rq->bmRequestType & USBRQ_RCPT_MASK;
- if(USB_CFG_IS_SELF_POWERED && recipient == USBRQ_RCPT_DEVICE)
- dataPtr[0] = USB_CFG_IS_SELF_POWERED;
- #if USB_CFG_IMPLEMENT_HALT
- if(recipient == USBRQ_RCPT_ENDPOINT && index == 0x81)
- dataPtr[0] = usbTxLen1 == USBPID_STALL;
- #endif
- dataPtr[1] = 0;
- len = 2;
- #if USB_CFG_IMPLEMENT_HALT
- SWITCH_CASE2(USBRQ_CLEAR_FEATURE, USBRQ_SET_FEATURE)
- if(value == 0 && index == 0x81){
- usbTxLen1 = rq->bRequest == USBRQ_CLEAR_FEATURE ? USBPID_NAK : USBPID_STALL;
- usbResetDataToggling();
- }
- #endif
- SWITCH_CASE(USBRQ_SET_ADDRESS)
- usbNewDeviceAddr = value;
- USB_SET_ADDRESS_HOOK();
- SWITCH_CASE(USBRQ_GET_DESCRIPTOR)
- len = usbDriverDescriptor(rq);
- goto skipMsgPtrAssignment;
- SWITCH_CASE(USBRQ_GET_CONFIGURATION)
- dataPtr = &usbConfiguration;
- len = 1;
- SWITCH_CASE(USBRQ_SET_CONFIGURATION)
- usbConfiguration = value;
- usbResetStall();
- SWITCH_CASE(USBRQ_GET_INTERFACE)
- len = 1;
- #if USB_CFG_HAVE_INTRIN_ENDPOINT && !USB_CFG_SUPPRESS_INTR_CODE
- SWITCH_CASE(USBRQ_SET_INTERFACE)
- usbResetDataToggling();
- usbResetStall();
- #endif
- SWITCH_DEFAULT
-
- SWITCH_END
- usbMsgPtr = dataPtr;
- skipMsgPtrAssignment:
- return len;
- }
- static inline void usbProcessRx(uchar *data, uchar len)
- {
- usbRequest_t *rq = (void *)data;
- DBG2(0x10 + (usbRxToken & 0xf), data, len + 2);
- USB_RX_USER_HOOK(data, len)
- #if USB_CFG_IMPLEMENT_FN_WRITEOUT
- if(usbRxToken < 0x10){
- usbFunctionWriteOut(data, len);
- return;
- }
- #endif
- if(usbRxToken == (uchar)USBPID_SETUP){
- if(len != 8)
- return;
- usbMsgLen_t replyLen;
- usbTxBuf[0] = USBPID_DATA0;
- usbTxLen = USBPID_NAK;
- usbMsgFlags = 0;
- uchar type = rq->bmRequestType & USBRQ_TYPE_MASK;
- if(type != USBRQ_TYPE_STANDARD){
- replyLen = usbFunctionSetup(data);
- }else{
- replyLen = usbDriverSetup(rq);
- }
- #if USB_CFG_IMPLEMENT_FN_READ || USB_CFG_IMPLEMENT_FN_WRITE
- if(replyLen == USB_NO_MSG){
-
- if((rq->bmRequestType & USBRQ_DIR_MASK) != USBRQ_DIR_HOST_TO_DEVICE){
- if(sizeof(replyLen) < sizeof(rq->wLength.word)){
- replyLen = rq->wLength.bytes[0];
- }else{
- replyLen = rq->wLength.word;
- }
- }
- usbMsgFlags = USB_FLG_USE_USER_RW;
- }else
- #endif
- if(sizeof(replyLen) < sizeof(rq->wLength.word)){
- if(!rq->wLength.bytes[1] && replyLen > rq->wLength.bytes[0])
- replyLen = rq->wLength.bytes[0];
- }else{
- if(replyLen > rq->wLength.word)
- replyLen = rq->wLength.word;
- }
- usbMsgLen = replyLen;
- }else{
- #if USB_CFG_IMPLEMENT_FN_WRITE
- if(usbMsgFlags & USB_FLG_USE_USER_RW){
- uchar rval = usbFunctionWrite(data, len);
- if(rval == 0xff){
- usbTxLen = USBPID_STALL;
- }else if(rval != 0){
- usbMsgLen = 0;
- }
- }
- #endif
- }
- }
- static uchar usbDeviceRead(uchar *data, uchar len)
- {
- if(len > 0){
- #if USB_CFG_IMPLEMENT_FN_READ
- if(usbMsgFlags & USB_FLG_USE_USER_RW){
- len = usbFunctionRead(data, len);
- }else
- #endif
- {
- uchar i = len, *r = usbMsgPtr;
- if(usbMsgFlags & USB_FLG_MSGPTR_IS_ROM){
- do{
- uchar c = USB_READ_FLASH(r);
- *data++ = c;
- r++;
- }while(--i);
- }else{
- do{
- *data++ = *r++;
- }while(--i);
- }
- usbMsgPtr = r;
- }
- }
- return len;
- }
- static inline void usbBuildTxBlock(void)
- {
- usbMsgLen_t wantLen;
- uchar len;
- wantLen = usbMsgLen;
- if(wantLen > 8)
- wantLen = 8;
- usbMsgLen -= wantLen;
- usbTxBuf[0] ^= USBPID_DATA0 ^ USBPID_DATA1;
- len = usbDeviceRead(usbTxBuf + 1, wantLen);
- if(len <= 8){
- usbCrc16Append(&usbTxBuf[1], len);
- len += 4;
- if(len < 12)
- usbMsgLen = USB_NO_MSG;
- }else{
- len = USBPID_STALL;
- usbMsgLen = USB_NO_MSG;
- }
- usbTxLen = len;
- DBG2(0x20, usbTxBuf, len-1);
- }
- static inline void usbHandleResetHook(uchar notResetState)
- {
- #ifdef USB_RESET_HOOK
- static uchar wasReset;
- uchar isReset = !notResetState;
- if(wasReset != isReset){
- USB_RESET_HOOK(isReset);
- wasReset = isReset;
- }
- #endif
- }
- USB_PUBLIC void usbPoll(void)
- {
- schar len;
- uchar i;
- len = usbRxLen - 3;
- if(len >= 0){
- usbProcessRx(usbRxBuf + USB_BUFSIZE + 1 - usbInputBufOffset, len);
- #if USB_CFG_HAVE_FLOWCONTROL
- if(usbRxLen > 0)
- usbRxLen = 0;
- #else
- usbRxLen = 0;
- #endif
- }
- if(usbTxLen & 0x10){
- if(usbMsgLen != USB_NO_MSG){
- usbBuildTxBlock();
- }
- }
- for(i = 20; i > 0; i--){
- uchar usbLineStatus = USBIN & USBMASK;
- if(usbLineStatus != 0)
- goto isNotReset;
- }
-
- usbNewDeviceAddr = 0;
- usbDeviceAddr = 0;
- usbResetStall();
- DBG1(0xff, 0, 0);
- isNotReset:
- usbHandleResetHook(i);
- }
- USB_PUBLIC void usbInit(void)
- {
- #if USB_INTR_CFG_SET != 0
- USB_INTR_CFG |= USB_INTR_CFG_SET;
- #endif
- #if USB_INTR_CFG_CLR != 0
- USB_INTR_CFG &= ~(USB_INTR_CFG_CLR);
- #endif
- USB_INTR_ENABLE |= (1 << USB_INTR_ENABLE_BIT);
- usbResetDataToggling();
- #if USB_CFG_HAVE_INTRIN_ENDPOINT && !USB_CFG_SUPPRESS_INTR_CODE
- usbTxLen1 = USBPID_NAK;
- #if USB_CFG_HAVE_INTRIN_ENDPOINT3
- usbTxLen3 = USBPID_NAK;
- #endif
- #endif
- }
|