You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /* Name: usbdrv.c
  2. * Project: V-USB, virtual USB port for Atmel's(r) AVR(r) microcontrollers
  3. * Author: Christian Starkjohann
  4. * Creation Date: 2004-12-29
  5. * Tabsize: 4
  6. * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH
  7. * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
  8. * This Revision: $Id: usbdrv.c 763 2009-08-22 10:27:24Z cs $
  9. */
  10. #include "usbportability.h"
  11. #include "usbdrv.h"
  12. #include "oddebug.h"
  13. /*
  14. General Description:
  15. This module implements the C-part of the USB driver. See usbdrv.h for a
  16. documentation of the entire driver.
  17. */
  18. /* ------------------------------------------------------------------------- */
  19. /* raw USB registers / interface to assembler code: */
  20. uchar usbRxBuf[2*USB_BUFSIZE]; /* raw RX buffer: PID, 8 bytes data, 2 bytes CRC */
  21. uchar usbInputBufOffset; /* offset in usbRxBuf used for low level receiving */
  22. uchar usbDeviceAddr; /* assigned during enumeration, defaults to 0 */
  23. uchar usbNewDeviceAddr; /* device ID which should be set after status phase */
  24. uchar usbConfiguration; /* currently selected configuration. Administered by driver, but not used */
  25. volatile schar usbRxLen; /* = 0; number of bytes in usbRxBuf; 0 means free, -1 for flow control */
  26. uchar usbCurrentTok; /* last token received or endpoint number for last OUT token if != 0 */
  27. uchar usbRxToken; /* token for data we received; or endpont number for last OUT */
  28. volatile uchar usbTxLen = USBPID_NAK; /* number of bytes to transmit with next IN token or handshake token */
  29. uchar usbTxBuf[USB_BUFSIZE];/* data to transmit with next IN, free if usbTxLen contains handshake token */
  30. #if USB_COUNT_SOF
  31. volatile uchar usbSofCount; /* incremented by assembler module every SOF */
  32. #endif
  33. #if USB_CFG_HAVE_INTRIN_ENDPOINT && !USB_CFG_SUPPRESS_INTR_CODE
  34. usbTxStatus_t usbTxStatus1;
  35. # if USB_CFG_HAVE_INTRIN_ENDPOINT3
  36. usbTxStatus_t usbTxStatus3;
  37. # endif
  38. #endif
  39. #if USB_CFG_CHECK_DATA_TOGGLING
  40. uchar usbCurrentDataToken;/* when we check data toggling to ignore duplicate packets */
  41. #endif
  42. /* USB status registers / not shared with asm code */
  43. uchar *usbMsgPtr; /* data to transmit next -- ROM or RAM address */
  44. static usbMsgLen_t usbMsgLen = USB_NO_MSG; /* remaining number of bytes */
  45. static uchar usbMsgFlags; /* flag values see below */
  46. #define USB_FLG_MSGPTR_IS_ROM (1<<6)
  47. #define USB_FLG_USE_USER_RW (1<<7)
  48. /*
  49. optimizing hints:
  50. - do not post/pre inc/dec integer values in operations
  51. - assign value of USB_READ_FLASH() to register variables and don't use side effects in arg
  52. - use narrow scope for variables which should be in X/Y/Z register
  53. - assign char sized expressions to variables to force 8 bit arithmetics
  54. */
  55. /* -------------------------- String Descriptors --------------------------- */
  56. #if USB_CFG_DESCR_PROPS_STRINGS == 0
  57. #if USB_CFG_DESCR_PROPS_STRING_0 == 0
  58. #undef USB_CFG_DESCR_PROPS_STRING_0
  59. #define USB_CFG_DESCR_PROPS_STRING_0 sizeof(usbDescriptorString0)
  60. PROGMEM char usbDescriptorString0[] = { /* language descriptor */
  61. 4, /* sizeof(usbDescriptorString0): length of descriptor in bytes */
  62. 3, /* descriptor type */
  63. 0x09, 0x04, /* language index (0x0409 = US-English) */
  64. };
  65. #endif
  66. #if USB_CFG_DESCR_PROPS_STRING_VENDOR == 0 && USB_CFG_VENDOR_NAME_LEN
  67. #undef USB_CFG_DESCR_PROPS_STRING_VENDOR
  68. #define USB_CFG_DESCR_PROPS_STRING_VENDOR sizeof(usbDescriptorStringVendor)
  69. PROGMEM int usbDescriptorStringVendor[] = {
  70. USB_STRING_DESCRIPTOR_HEADER(USB_CFG_VENDOR_NAME_LEN),
  71. USB_CFG_VENDOR_NAME
  72. };
  73. #endif
  74. #if USB_CFG_DESCR_PROPS_STRING_PRODUCT == 0 && USB_CFG_DEVICE_NAME_LEN
  75. #undef USB_CFG_DESCR_PROPS_STRING_PRODUCT
  76. #define USB_CFG_DESCR_PROPS_STRING_PRODUCT sizeof(usbDescriptorStringDevice)
  77. PROGMEM int usbDescriptorStringDevice[] = {
  78. USB_STRING_DESCRIPTOR_HEADER(USB_CFG_DEVICE_NAME_LEN),
  79. USB_CFG_DEVICE_NAME
  80. };
  81. #endif
  82. #if USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER == 0 && USB_CFG_SERIAL_NUMBER_LEN
  83. #undef USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER
  84. #define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER sizeof(usbDescriptorStringSerialNumber)
  85. PROGMEM int usbDescriptorStringSerialNumber[] = {
  86. USB_STRING_DESCRIPTOR_HEADER(USB_CFG_SERIAL_NUMBER_LEN),
  87. USB_CFG_SERIAL_NUMBER
  88. };
  89. #endif
  90. #endif /* USB_CFG_DESCR_PROPS_STRINGS == 0 */
  91. /* --------------------------- Device Descriptor --------------------------- */
  92. #if USB_CFG_DESCR_PROPS_DEVICE == 0
  93. #undef USB_CFG_DESCR_PROPS_DEVICE
  94. #define USB_CFG_DESCR_PROPS_DEVICE sizeof(usbDescriptorDevice)
  95. PROGMEM char usbDescriptorDevice[] = { /* USB device descriptor */
  96. 18, /* sizeof(usbDescriptorDevice): length of descriptor in bytes */
  97. USBDESCR_DEVICE, /* descriptor type */
  98. 0x10, 0x01, /* USB version supported */
  99. USB_CFG_DEVICE_CLASS,
  100. USB_CFG_DEVICE_SUBCLASS,
  101. 0, /* protocol */
  102. 8, /* max packet size */
  103. /* the following two casts affect the first byte of the constant only, but
  104. * that's sufficient to avoid a warning with the default values.
  105. */
  106. (char)USB_CFG_VENDOR_ID,/* 2 bytes */
  107. (char)USB_CFG_DEVICE_ID,/* 2 bytes */
  108. USB_CFG_DEVICE_VERSION, /* 2 bytes */
  109. USB_CFG_DESCR_PROPS_STRING_VENDOR != 0 ? 1 : 0, /* manufacturer string index */
  110. USB_CFG_DESCR_PROPS_STRING_PRODUCT != 0 ? 2 : 0, /* product string index */
  111. USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER != 0 ? 3 : 0, /* serial number string index */
  112. 1, /* number of configurations */
  113. };
  114. #endif
  115. /* ----------------------- Configuration Descriptor ------------------------ */
  116. #if USB_CFG_DESCR_PROPS_HID_REPORT != 0 && USB_CFG_DESCR_PROPS_HID == 0
  117. #undef USB_CFG_DESCR_PROPS_HID
  118. #define USB_CFG_DESCR_PROPS_HID 9 /* length of HID descriptor in config descriptor below */
  119. #endif
  120. #if USB_CFG_DESCR_PROPS_CONFIGURATION == 0
  121. #undef USB_CFG_DESCR_PROPS_CONFIGURATION
  122. #define USB_CFG_DESCR_PROPS_CONFIGURATION sizeof(usbDescriptorConfiguration)
  123. PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */
  124. 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
  125. USBDESCR_CONFIG, /* descriptor type */
  126. 18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 +
  127. (USB_CFG_DESCR_PROPS_HID & 0xff), 0,
  128. /* total length of data returned (including inlined descriptors) */
  129. 1, /* number of interfaces in this configuration */
  130. 1, /* index of this configuration */
  131. 0, /* configuration name string index */
  132. #if USB_CFG_IS_SELF_POWERED
  133. (1 << 7) | USBATTR_SELFPOWER, /* attributes */
  134. #else
  135. (1 << 7), /* attributes */
  136. #endif
  137. USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
  138. /* interface descriptor follows inline: */
  139. 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
  140. USBDESCR_INTERFACE, /* descriptor type */
  141. 0, /* index of this interface */
  142. 0, /* alternate setting for this interface */
  143. USB_CFG_HAVE_INTRIN_ENDPOINT + USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */
  144. USB_CFG_INTERFACE_CLASS,
  145. USB_CFG_INTERFACE_SUBCLASS,
  146. USB_CFG_INTERFACE_PROTOCOL,
  147. 0, /* string index for interface */
  148. #if (USB_CFG_DESCR_PROPS_HID & 0xff) /* HID descriptor */
  149. 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
  150. USBDESCR_HID, /* descriptor type: HID */
  151. 0x01, 0x01, /* BCD representation of HID version */
  152. 0x00, /* target country code */
  153. 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
  154. 0x22, /* descriptor type: report */
  155. USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH, 0, /* total length of report descriptor */
  156. #endif
  157. #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
  158. 7, /* sizeof(usbDescrEndpoint) */
  159. USBDESCR_ENDPOINT, /* descriptor type = endpoint */
  160. (char)0x81, /* IN endpoint number 1 */
  161. 0x03, /* attrib: Interrupt endpoint */
  162. 8, 0, /* maximum packet size */
  163. USB_CFG_INTR_POLL_INTERVAL, /* in ms */
  164. #endif
  165. #if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
  166. 7, /* sizeof(usbDescrEndpoint) */
  167. USBDESCR_ENDPOINT, /* descriptor type = endpoint */
  168. (char)0x83, /* IN endpoint number 1 */
  169. 0x03, /* attrib: Interrupt endpoint */
  170. 8, 0, /* maximum packet size */
  171. USB_CFG_INTR_POLL_INTERVAL, /* in ms */
  172. #endif
  173. };
  174. #endif
  175. /* ------------------------------------------------------------------------- */
  176. static inline void usbResetDataToggling(void)
  177. {
  178. #if USB_CFG_HAVE_INTRIN_ENDPOINT && !USB_CFG_SUPPRESS_INTR_CODE
  179. USB_SET_DATATOKEN1(USB_INITIAL_DATATOKEN); /* reset data toggling for interrupt endpoint */
  180. # if USB_CFG_HAVE_INTRIN_ENDPOINT3
  181. USB_SET_DATATOKEN3(USB_INITIAL_DATATOKEN); /* reset data toggling for interrupt endpoint */
  182. # endif
  183. #endif
  184. }
  185. static inline void usbResetStall(void)
  186. {
  187. #if USB_CFG_IMPLEMENT_HALT && USB_CFG_HAVE_INTRIN_ENDPOINT
  188. usbTxLen1 = USBPID_NAK;
  189. #if USB_CFG_HAVE_INTRIN_ENDPOINT3
  190. usbTxLen3 = USBPID_NAK;
  191. #endif
  192. #endif
  193. }
  194. /* ------------------------------------------------------------------------- */
  195. #if !USB_CFG_SUPPRESS_INTR_CODE
  196. #if USB_CFG_HAVE_INTRIN_ENDPOINT
  197. static void usbGenericSetInterrupt(uchar *data, uchar len, usbTxStatus_t *txStatus)
  198. {
  199. uchar *p;
  200. char i;
  201. #if USB_CFG_IMPLEMENT_HALT
  202. if(usbTxLen1 == USBPID_STALL)
  203. return;
  204. #endif
  205. if(txStatus->len & 0x10){ /* packet buffer was empty */
  206. txStatus->buffer[0] ^= USBPID_DATA0 ^ USBPID_DATA1; /* toggle token */
  207. }else{
  208. txStatus->len = USBPID_NAK; /* avoid sending outdated (overwritten) interrupt data */
  209. }
  210. p = txStatus->buffer + 1;
  211. i = len;
  212. do{ /* if len == 0, we still copy 1 byte, but that's no problem */
  213. *p++ = *data++;
  214. }while(--i > 0); /* loop control at the end is 2 bytes shorter than at beginning */
  215. usbCrc16Append(&txStatus->buffer[1], len);
  216. txStatus->len = len + 4; /* len must be given including sync byte */
  217. DBG2(0x21 + (((int)txStatus >> 3) & 3), txStatus->buffer, len + 3);
  218. }
  219. USB_PUBLIC void usbSetInterrupt(uchar *data, uchar len)
  220. {
  221. usbGenericSetInterrupt(data, len, &usbTxStatus1);
  222. }
  223. #endif
  224. #if USB_CFG_HAVE_INTRIN_ENDPOINT3
  225. USB_PUBLIC void usbSetInterrupt3(uchar *data, uchar len)
  226. {
  227. usbGenericSetInterrupt(data, len, &usbTxStatus3);
  228. }
  229. #endif
  230. #endif /* USB_CFG_SUPPRESS_INTR_CODE */
  231. /* ------------------ utilities for code following below ------------------- */
  232. /* Use defines for the switch statement so that we can choose between an
  233. * if()else if() and a switch/case based implementation. switch() is more
  234. * efficient for a LARGE set of sequential choices, if() is better in all other
  235. * cases.
  236. */
  237. #if USB_CFG_USE_SWITCH_STATEMENT
  238. # define SWITCH_START(cmd) switch(cmd){{
  239. # define SWITCH_CASE(value) }break; case (value):{
  240. # define SWITCH_CASE2(v1,v2) }break; case (v1): case(v2):{
  241. # define SWITCH_CASE3(v1,v2,v3) }break; case (v1): case(v2): case(v3):{
  242. # define SWITCH_DEFAULT }break; default:{
  243. # define SWITCH_END }}
  244. #else
  245. # define SWITCH_START(cmd) {uchar _cmd = cmd; if(0){
  246. # define SWITCH_CASE(value) }else if(_cmd == (value)){
  247. # define SWITCH_CASE2(v1,v2) }else if(_cmd == (v1) || _cmd == (v2)){
  248. # define SWITCH_CASE3(v1,v2,v3) }else if(_cmd == (v1) || _cmd == (v2) || (_cmd == v3)){
  249. # define SWITCH_DEFAULT }else{
  250. # define SWITCH_END }}
  251. #endif
  252. #ifndef USB_RX_USER_HOOK
  253. #define USB_RX_USER_HOOK(data, len)
  254. #endif
  255. #ifndef USB_SET_ADDRESS_HOOK
  256. #define USB_SET_ADDRESS_HOOK()
  257. #endif
  258. /* ------------------------------------------------------------------------- */
  259. /* We use if() instead of #if in the macro below because #if can't be used
  260. * in macros and the compiler optimizes constant conditions anyway.
  261. * This may cause problems with undefined symbols if compiled without
  262. * optimizing!
  263. */
  264. #define GET_DESCRIPTOR(cfgProp, staticName) \
  265. if(cfgProp){ \
  266. if((cfgProp) & USB_PROP_IS_RAM) \
  267. flags = 0; \
  268. if((cfgProp) & USB_PROP_IS_DYNAMIC){ \
  269. len = usbFunctionDescriptor(rq); \
  270. }else{ \
  271. len = USB_PROP_LENGTH(cfgProp); \
  272. usbMsgPtr = (uchar *)(staticName); \
  273. } \
  274. }
  275. /* usbDriverDescriptor() is similar to usbFunctionDescriptor(), but used
  276. * internally for all types of descriptors.
  277. */
  278. static inline usbMsgLen_t usbDriverDescriptor(usbRequest_t *rq)
  279. {
  280. usbMsgLen_t len = 0;
  281. uchar flags = USB_FLG_MSGPTR_IS_ROM;
  282. SWITCH_START(rq->wValue.bytes[1])
  283. SWITCH_CASE(USBDESCR_DEVICE) /* 1 */
  284. GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_DEVICE, usbDescriptorDevice)
  285. SWITCH_CASE(USBDESCR_CONFIG) /* 2 */
  286. GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_CONFIGURATION, usbDescriptorConfiguration)
  287. SWITCH_CASE(USBDESCR_STRING) /* 3 */
  288. #if USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC
  289. if(USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_RAM)
  290. flags = 0;
  291. len = usbFunctionDescriptor(rq);
  292. #else /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */
  293. SWITCH_START(rq->wValue.bytes[0])
  294. SWITCH_CASE(0)
  295. GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_0, usbDescriptorString0)
  296. SWITCH_CASE(1)
  297. GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_VENDOR, usbDescriptorStringVendor)
  298. SWITCH_CASE(2)
  299. GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_PRODUCT, usbDescriptorStringDevice)
  300. SWITCH_CASE(3)
  301. GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER, usbDescriptorStringSerialNumber)
  302. SWITCH_DEFAULT
  303. if(USB_CFG_DESCR_PROPS_UNKNOWN & USB_PROP_IS_DYNAMIC){
  304. len = usbFunctionDescriptor(rq);
  305. }
  306. SWITCH_END
  307. #endif /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */
  308. #if USB_CFG_DESCR_PROPS_HID_REPORT /* only support HID descriptors if enabled */
  309. SWITCH_CASE(USBDESCR_HID) /* 0x21 */
  310. GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID, usbDescriptorConfiguration + 18)
  311. SWITCH_CASE(USBDESCR_HID_REPORT)/* 0x22 */
  312. GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID_REPORT, usbDescriptorHidReport)
  313. #endif
  314. SWITCH_DEFAULT
  315. if(USB_CFG_DESCR_PROPS_UNKNOWN & USB_PROP_IS_DYNAMIC){
  316. len = usbFunctionDescriptor(rq);
  317. }
  318. SWITCH_END
  319. usbMsgFlags = flags;
  320. return len;
  321. }
  322. /* ------------------------------------------------------------------------- */
  323. /* usbDriverSetup() is similar to usbFunctionSetup(), but it's used for
  324. * standard requests instead of class and custom requests.
  325. */
  326. static inline usbMsgLen_t usbDriverSetup(usbRequest_t *rq)
  327. {
  328. uchar len = 0, *dataPtr = usbTxBuf + 9; /* there are 2 bytes free space at the end of the buffer */
  329. uchar value = rq->wValue.bytes[0];
  330. #if USB_CFG_IMPLEMENT_HALT
  331. uchar index = rq->wIndex.bytes[0];
  332. #endif
  333. dataPtr[0] = 0; /* default reply common to USBRQ_GET_STATUS and USBRQ_GET_INTERFACE */
  334. SWITCH_START(rq->bRequest)
  335. SWITCH_CASE(USBRQ_GET_STATUS) /* 0 */
  336. uchar recipient = rq->bmRequestType & USBRQ_RCPT_MASK; /* assign arith ops to variables to enforce byte size */
  337. if(USB_CFG_IS_SELF_POWERED && recipient == USBRQ_RCPT_DEVICE)
  338. dataPtr[0] = USB_CFG_IS_SELF_POWERED;
  339. #if USB_CFG_IMPLEMENT_HALT
  340. if(recipient == USBRQ_RCPT_ENDPOINT && index == 0x81) /* request status for endpoint 1 */
  341. dataPtr[0] = usbTxLen1 == USBPID_STALL;
  342. #endif
  343. dataPtr[1] = 0;
  344. len = 2;
  345. #if USB_CFG_IMPLEMENT_HALT
  346. SWITCH_CASE2(USBRQ_CLEAR_FEATURE, USBRQ_SET_FEATURE) /* 1, 3 */
  347. if(value == 0 && index == 0x81){ /* feature 0 == HALT for endpoint == 1 */
  348. usbTxLen1 = rq->bRequest == USBRQ_CLEAR_FEATURE ? USBPID_NAK : USBPID_STALL;
  349. usbResetDataToggling();
  350. }
  351. #endif
  352. SWITCH_CASE(USBRQ_SET_ADDRESS) /* 5 */
  353. usbNewDeviceAddr = value;
  354. USB_SET_ADDRESS_HOOK();
  355. SWITCH_CASE(USBRQ_GET_DESCRIPTOR) /* 6 */
  356. len = usbDriverDescriptor(rq);
  357. goto skipMsgPtrAssignment;
  358. SWITCH_CASE(USBRQ_GET_CONFIGURATION) /* 8 */
  359. dataPtr = &usbConfiguration; /* send current configuration value */
  360. len = 1;
  361. SWITCH_CASE(USBRQ_SET_CONFIGURATION) /* 9 */
  362. usbConfiguration = value;
  363. usbResetStall();
  364. SWITCH_CASE(USBRQ_GET_INTERFACE) /* 10 */
  365. len = 1;
  366. #if USB_CFG_HAVE_INTRIN_ENDPOINT && !USB_CFG_SUPPRESS_INTR_CODE
  367. SWITCH_CASE(USBRQ_SET_INTERFACE) /* 11 */
  368. usbResetDataToggling();
  369. usbResetStall();
  370. #endif
  371. SWITCH_DEFAULT /* 7=SET_DESCRIPTOR, 12=SYNC_FRAME */
  372. /* Should we add an optional hook here? */
  373. SWITCH_END
  374. usbMsgPtr = dataPtr;
  375. skipMsgPtrAssignment:
  376. return len;
  377. }
  378. /* ------------------------------------------------------------------------- */
  379. /* usbProcessRx() is called for every message received by the interrupt
  380. * routine. It distinguishes between SETUP and DATA packets and processes
  381. * them accordingly.
  382. */
  383. static inline void usbProcessRx(uchar *data, uchar len)
  384. {
  385. usbRequest_t *rq = (usbRequest_t *)((void *)data);
  386. /* usbRxToken can be:
  387. * 0x2d 00101101 (USBPID_SETUP for setup data)
  388. * 0xe1 11100001 (USBPID_OUT: data phase of setup transfer)
  389. * 0...0x0f for OUT on endpoint X
  390. */
  391. DBG2(0x10 + (usbRxToken & 0xf), data, len + 2); /* SETUP=1d, SETUP-DATA=11, OUTx=1x */
  392. USB_RX_USER_HOOK(data, len)
  393. #if USB_CFG_IMPLEMENT_FN_WRITEOUT
  394. if(usbRxToken < 0x10){ /* OUT to endpoint != 0: endpoint number in usbRxToken */
  395. usbFunctionWriteOut(data, len);
  396. return;
  397. }
  398. #endif
  399. if(usbRxToken == (uchar)USBPID_SETUP){
  400. if(len != 8) /* Setup size must be always 8 bytes. Ignore otherwise. */
  401. return;
  402. usbMsgLen_t replyLen;
  403. usbTxBuf[0] = USBPID_DATA0; /* initialize data toggling */
  404. usbTxLen = USBPID_NAK; /* abort pending transmit */
  405. usbMsgFlags = 0;
  406. uchar type = rq->bmRequestType & USBRQ_TYPE_MASK;
  407. if(type != USBRQ_TYPE_STANDARD){ /* standard requests are handled by driver */
  408. replyLen = usbFunctionSetup(data);
  409. }else{
  410. replyLen = usbDriverSetup(rq);
  411. }
  412. #if USB_CFG_IMPLEMENT_FN_READ || USB_CFG_IMPLEMENT_FN_WRITE
  413. if(replyLen == USB_NO_MSG){ /* use user-supplied read/write function */
  414. /* do some conditioning on replyLen, but on IN transfers only */
  415. if((rq->bmRequestType & USBRQ_DIR_MASK) != USBRQ_DIR_HOST_TO_DEVICE){
  416. if(sizeof(replyLen) < sizeof(rq->wLength.word)){ /* help compiler with optimizing */
  417. replyLen = rq->wLength.bytes[0];
  418. }else{
  419. replyLen = rq->wLength.word;
  420. }
  421. }
  422. usbMsgFlags = USB_FLG_USE_USER_RW;
  423. }else /* The 'else' prevents that we limit a replyLen of USB_NO_MSG to the maximum transfer len. */
  424. #endif
  425. if(sizeof(replyLen) < sizeof(rq->wLength.word)){ /* help compiler with optimizing */
  426. if(!rq->wLength.bytes[1] && replyLen > rq->wLength.bytes[0]) /* limit length to max */
  427. replyLen = rq->wLength.bytes[0];
  428. }else{
  429. if(replyLen > rq->wLength.word) /* limit length to max */
  430. replyLen = rq->wLength.word;
  431. }
  432. usbMsgLen = replyLen;
  433. }else{ /* usbRxToken must be USBPID_OUT, which means data phase of setup (control-out) */
  434. #if USB_CFG_IMPLEMENT_FN_WRITE
  435. if(usbMsgFlags & USB_FLG_USE_USER_RW){
  436. uchar rval = usbFunctionWrite(data, len);
  437. if(rval == 0xff){ /* an error occurred */
  438. usbTxLen = USBPID_STALL;
  439. }else if(rval != 0){ /* This was the final package */
  440. usbMsgLen = 0; /* answer with a zero-sized data packet */
  441. }
  442. }
  443. #endif
  444. }
  445. }
  446. /* ------------------------------------------------------------------------- */
  447. /* This function is similar to usbFunctionRead(), but it's also called for
  448. * data handled automatically by the driver (e.g. descriptor reads).
  449. */
  450. static uchar usbDeviceRead(uchar *data, uchar len)
  451. {
  452. if(len > 0){ /* don't bother app with 0 sized reads */
  453. #if USB_CFG_IMPLEMENT_FN_READ
  454. if(usbMsgFlags & USB_FLG_USE_USER_RW){
  455. len = usbFunctionRead(data, len);
  456. }else
  457. #endif
  458. {
  459. uchar i = len, *r = usbMsgPtr;
  460. if(usbMsgFlags & USB_FLG_MSGPTR_IS_ROM){ /* ROM data */
  461. do{
  462. uchar c = USB_READ_FLASH(r); /* assign to char size variable to enforce byte ops */
  463. *data++ = c;
  464. r++;
  465. }while(--i);
  466. }else{ /* RAM data */
  467. do{
  468. *data++ = *r++;
  469. }while(--i);
  470. }
  471. usbMsgPtr = r;
  472. }
  473. }
  474. return len;
  475. }
  476. /* ------------------------------------------------------------------------- */
  477. /* usbBuildTxBlock() is called when we have data to transmit and the
  478. * interrupt routine's transmit buffer is empty.
  479. */
  480. static inline void usbBuildTxBlock(void)
  481. {
  482. usbMsgLen_t wantLen;
  483. uchar len;
  484. wantLen = usbMsgLen;
  485. if(wantLen > 8)
  486. wantLen = 8;
  487. usbMsgLen -= wantLen;
  488. usbTxBuf[0] ^= USBPID_DATA0 ^ USBPID_DATA1; /* DATA toggling */
  489. len = usbDeviceRead(usbTxBuf + 1, wantLen);
  490. if(len <= 8){ /* valid data packet */
  491. usbCrc16Append(&usbTxBuf[1], len);
  492. len += 4; /* length including sync byte */
  493. if(len < 12) /* a partial package identifies end of message */
  494. usbMsgLen = USB_NO_MSG;
  495. }else{
  496. len = USBPID_STALL; /* stall the endpoint */
  497. usbMsgLen = USB_NO_MSG;
  498. }
  499. usbTxLen = len;
  500. DBG2(0x20, usbTxBuf, len-1);
  501. }
  502. /* ------------------------------------------------------------------------- */
  503. static inline void usbHandleResetHook(uchar notResetState)
  504. {
  505. #ifdef USB_RESET_HOOK
  506. static uchar wasReset;
  507. uchar isReset = !notResetState;
  508. if(wasReset != isReset){
  509. USB_RESET_HOOK(isReset);
  510. wasReset = isReset;
  511. }
  512. #endif
  513. }
  514. /* ------------------------------------------------------------------------- */
  515. USB_PUBLIC void usbPoll(void)
  516. {
  517. schar len;
  518. uchar i;
  519. len = usbRxLen - 3;
  520. if(len >= 0){
  521. /* We could check CRC16 here -- but ACK has already been sent anyway. If you
  522. * need data integrity checks with this driver, check the CRC in your app
  523. * code and report errors back to the host. Since the ACK was already sent,
  524. * retries must be handled on application level.
  525. * unsigned crc = usbCrc16(buffer + 1, usbRxLen - 3);
  526. */
  527. usbProcessRx(usbRxBuf + USB_BUFSIZE + 1 - usbInputBufOffset, len);
  528. #if USB_CFG_HAVE_FLOWCONTROL
  529. if(usbRxLen > 0) /* only mark as available if not inactivated */
  530. usbRxLen = 0;
  531. #else
  532. usbRxLen = 0; /* mark rx buffer as available */
  533. #endif
  534. }
  535. if(usbTxLen & 0x10){ /* transmit system idle */
  536. if(usbMsgLen != USB_NO_MSG){ /* transmit data pending? */
  537. usbBuildTxBlock();
  538. }
  539. }
  540. for(i = 20; i > 0; i--){
  541. uchar usbLineStatus = USBIN & USBMASK;
  542. if(usbLineStatus != 0) /* SE0 has ended */
  543. goto isNotReset;
  544. }
  545. /* RESET condition, called multiple times during reset */
  546. usbNewDeviceAddr = 0;
  547. usbDeviceAddr = 0;
  548. usbResetStall();
  549. DBG1(0xff, 0, 0);
  550. isNotReset:
  551. usbHandleResetHook(i);
  552. }
  553. /* ------------------------------------------------------------------------- */
  554. USB_PUBLIC void usbInit(void)
  555. {
  556. #if USB_INTR_CFG_SET != 0
  557. USB_INTR_CFG |= USB_INTR_CFG_SET;
  558. #endif
  559. #if USB_INTR_CFG_CLR != 0
  560. USB_INTR_CFG &= ~(USB_INTR_CFG_CLR);
  561. #endif
  562. USB_INTR_ENABLE |= (1 << USB_INTR_ENABLE_BIT);
  563. usbResetDataToggling();
  564. #if USB_CFG_HAVE_INTRIN_ENDPOINT && !USB_CFG_SUPPRESS_INTR_CODE
  565. usbTxLen1 = USBPID_NAK;
  566. #if USB_CFG_HAVE_INTRIN_ENDPOINT3
  567. usbTxLen3 = USBPID_NAK;
  568. #endif
  569. #endif
  570. }
  571. /* ------------------------------------------------------------------------- */