usbdrv.c 26 KB

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