Ei kuvausta
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.

usbdrv.c 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /* Name: usbdrv.c
  2. * Project: AVR USB driver
  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) or proprietary (CommercialLicense.txt)
  8. * This Revision: $Id: usbdrv.c 530 2008-02-28 15:34:04Z cs $
  9. */
  10. #include "iarcompat.h"
  11. #ifndef __IAR_SYSTEMS_ICC__
  12. # include <avr/io.h>
  13. # include <avr/pgmspace.h>
  14. #endif
  15. #include "usbdrv.h"
  16. #include "oddebug.h"
  17. /*
  18. General Description:
  19. This module implements the C-part of the USB driver. See usbdrv.h for a
  20. documentation of the entire driver.
  21. */
  22. /* ------------------------------------------------------------------------- */
  23. /* raw USB registers / interface to assembler code: */
  24. uchar usbRxBuf[2*USB_BUFSIZE]; /* raw RX buffer: PID, 8 bytes data, 2 bytes CRC */
  25. uchar usbInputBufOffset; /* offset in usbRxBuf used for low level receiving */
  26. uchar usbDeviceAddr; /* assigned during enumeration, defaults to 0 */
  27. uchar usbNewDeviceAddr; /* device ID which should be set after status phase */
  28. uchar usbConfiguration; /* currently selected configuration. Administered by driver, but not used */
  29. volatile schar usbRxLen; /* = 0; number of bytes in usbRxBuf; 0 means free, -1 for flow control */
  30. uchar usbCurrentTok; /* last token received or endpoint number for last OUT token if != 0 */
  31. uchar usbRxToken; /* token for data we received; or endpont number for last OUT */
  32. uchar usbMsgLen = 0xff; /* remaining number of bytes, no msg to send if -1 (see usbMsgPtr) */
  33. volatile uchar usbTxLen = USBPID_NAK; /* number of bytes to transmit with next IN token or handshake token */
  34. uchar usbTxBuf[USB_BUFSIZE];/* data to transmit with next IN, free if usbTxLen contains handshake token */
  35. #if USB_COUNT_SOF
  36. volatile uchar usbSofCount; /* incremented by assembler module every SOF */
  37. #endif
  38. #if USB_CFG_HAVE_INTRIN_ENDPOINT
  39. volatile uchar usbTxLen1 = USBPID_NAK; /* TX count for endpoint 1 */
  40. uchar usbTxBuf1[USB_BUFSIZE]; /* TX data for endpoint 1 */
  41. # if USB_CFG_HAVE_INTRIN_ENDPOINT3
  42. volatile uchar usbTxLen3 = USBPID_NAK; /* TX count for endpoint 3 */
  43. uchar usbTxBuf3[USB_BUFSIZE]; /* TX data for endpoint 3 */
  44. # endif
  45. #endif
  46. /* USB status registers / not shared with asm code */
  47. uchar *usbMsgPtr; /* data to transmit next -- ROM or RAM address */
  48. static uchar usbMsgFlags; /* flag values see below */
  49. #define USB_FLG_TX_PACKET (1<<0)
  50. /* Leave free 6 bits after TX_PACKET. This way we can increment usbMsgFlags to toggle TX_PACKET */
  51. #define USB_FLG_MSGPTR_IS_ROM (1<<6)
  52. #define USB_FLG_USE_DEFAULT_RW (1<<7)
  53. /*
  54. optimizing hints:
  55. - do not post/pre inc/dec integer values in operations
  56. - assign value of PRG_RDB() to register variables and don't use side effects in arg
  57. - use narrow scope for variables which should be in X/Y/Z register
  58. - assign char sized expressions to variables to force 8 bit arithmetics
  59. */
  60. /* ------------------------------------------------------------------------- */
  61. #if USB_CFG_DESCR_PROPS_STRINGS == 0
  62. #if USB_CFG_DESCR_PROPS_STRING_0 == 0
  63. #undef USB_CFG_DESCR_PROPS_STRING_0
  64. #define USB_CFG_DESCR_PROPS_STRING_0 sizeof(usbDescriptorString0)
  65. PROGMEM const char usbDescriptorString0[] = { /* language descriptor */
  66. 4, /* sizeof(usbDescriptorString0): length of descriptor in bytes */
  67. 3, /* descriptor type */
  68. 0x09, 0x04, /* language index (0x0409 = US-English) */
  69. };
  70. #endif
  71. #if USB_CFG_DESCR_PROPS_STRING_VENDOR == 0 && USB_CFG_VENDOR_NAME_LEN
  72. #undef USB_CFG_DESCR_PROPS_STRING_VENDOR
  73. #define USB_CFG_DESCR_PROPS_STRING_VENDOR sizeof(usbDescriptorStringVendor)
  74. PROGMEM const int usbDescriptorStringVendor[] = {
  75. USB_STRING_DESCRIPTOR_HEADER(USB_CFG_VENDOR_NAME_LEN),
  76. USB_CFG_VENDOR_NAME
  77. };
  78. #endif
  79. #if USB_CFG_DESCR_PROPS_STRING_PRODUCT == 0 && USB_CFG_DEVICE_NAME_LEN
  80. #undef USB_CFG_DESCR_PROPS_STRING_PRODUCT
  81. #define USB_CFG_DESCR_PROPS_STRING_PRODUCT sizeof(usbDescriptorStringDevice)
  82. PROGMEM const int usbDescriptorStringDevice[] = {
  83. USB_STRING_DESCRIPTOR_HEADER(USB_CFG_DEVICE_NAME_LEN),
  84. USB_CFG_DEVICE_NAME
  85. };
  86. #endif
  87. #if USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER == 0 && USB_CFG_SERIAL_NUMBER_LEN
  88. #undef USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER
  89. #define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER sizeof(usbDescriptorStringSerialNumber)
  90. PROGMEM const int usbDescriptorStringSerialNumber[] = {
  91. USB_STRING_DESCRIPTOR_HEADER(USB_CFG_SERIAL_NUMBER_LEN),
  92. USB_CFG_SERIAL_NUMBER
  93. };
  94. #endif
  95. #endif /* USB_CFG_DESCR_PROPS_STRINGS == 0 */
  96. #if USB_CFG_DESCR_PROPS_DEVICE == 0
  97. #undef USB_CFG_DESCR_PROPS_DEVICE
  98. #define USB_CFG_DESCR_PROPS_DEVICE sizeof(usbDescriptorDevice)
  99. PROGMEM const char usbDescriptorDevice[] = { /* USB device descriptor */
  100. 18, /* sizeof(usbDescriptorDevice): length of descriptor in bytes */
  101. USBDESCR_DEVICE, /* descriptor type */
  102. 0x10, 0x01, /* USB version supported */
  103. USB_CFG_DEVICE_CLASS,
  104. USB_CFG_DEVICE_SUBCLASS,
  105. 0, /* protocol */
  106. 8, /* max packet size */
  107. /* the following two casts affect the first byte of the constant only, but
  108. * that's sufficient to avoid a warning with the default values.
  109. */
  110. (char)USB_CFG_VENDOR_ID,/* 2 bytes */
  111. (char)USB_CFG_DEVICE_ID,/* 2 bytes */
  112. USB_CFG_DEVICE_VERSION, /* 2 bytes */
  113. USB_CFG_DESCR_PROPS_STRING_VENDOR != 0 ? 1 : 0, /* manufacturer string index */
  114. USB_CFG_DESCR_PROPS_STRING_PRODUCT != 0 ? 2 : 0, /* product string index */
  115. USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER != 0 ? 3 : 0, /* serial number string index */
  116. 1, /* number of configurations */
  117. };
  118. #endif
  119. #if USB_CFG_DESCR_PROPS_HID_REPORT != 0 && USB_CFG_DESCR_PROPS_HID == 0
  120. #undef USB_CFG_DESCR_PROPS_HID
  121. #define USB_CFG_DESCR_PROPS_HID 9 /* length of HID descriptor in config descriptor below */
  122. #endif
  123. #if USB_CFG_DESCR_PROPS_CONFIGURATION == 0
  124. #undef USB_CFG_DESCR_PROPS_CONFIGURATION
  125. #define USB_CFG_DESCR_PROPS_CONFIGURATION sizeof(usbDescriptorConfiguration)
  126. PROGMEM const char usbDescriptorConfiguration[] = { /* USB configuration descriptor */
  127. 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
  128. USBDESCR_CONFIG, /* descriptor type */
  129. 18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + (USB_CFG_DESCR_PROPS_HID & 0xff), 0,
  130. /* total length of data returned (including inlined descriptors) */
  131. 1, /* number of interfaces in this configuration */
  132. 1, /* index of this configuration */
  133. 0, /* configuration name string index */
  134. #if USB_CFG_IS_SELF_POWERED
  135. USBATTR_SELFPOWER, /* attributes */
  136. #else
  137. (char)USBATTR_BUSPOWER, /* attributes */
  138. #endif
  139. USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
  140. /* interface descriptor follows inline: */
  141. 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
  142. USBDESCR_INTERFACE, /* descriptor type */
  143. 0, /* index of this interface */
  144. 0, /* alternate setting for this interface */
  145. USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
  146. USB_CFG_INTERFACE_CLASS,
  147. USB_CFG_INTERFACE_SUBCLASS,
  148. USB_CFG_INTERFACE_PROTOCOL,
  149. 0, /* string index for interface */
  150. #if (USB_CFG_DESCR_PROPS_HID & 0xff) /* HID descriptor */
  151. 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
  152. USBDESCR_HID, /* descriptor type: HID */
  153. 0x01, 0x01, /* BCD representation of HID version */
  154. 0x00, /* target country code */
  155. 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
  156. 0x22, /* descriptor type: report */
  157. USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH, 0, /* total length of report descriptor */
  158. #endif
  159. #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
  160. 7, /* sizeof(usbDescrEndpoint) */
  161. USBDESCR_ENDPOINT, /* descriptor type = endpoint */
  162. (char)0x81, /* IN endpoint number 1 */
  163. 0x03, /* attrib: Interrupt endpoint */
  164. 8, 0, /* maximum packet size */
  165. USB_CFG_INTR_POLL_INTERVAL, /* in ms */
  166. #endif
  167. };
  168. #endif
  169. /* We don't use prog_int or prog_int16_t for compatibility with various libc
  170. * versions. Here's an other compatibility hack:
  171. */
  172. #ifndef PRG_RDB
  173. #define PRG_RDB(addr) pgm_read_byte(addr)
  174. #endif
  175. typedef union{
  176. unsigned word;
  177. uchar *ptr;
  178. uchar bytes[2];
  179. }converter_t;
  180. /* We use this union to do type conversions. This is better optimized than
  181. * type casts in gcc 3.4.3 and much better than using bit shifts to build
  182. * ints from chars. Byte ordering is not a problem on an 8 bit platform.
  183. */
  184. /* ------------------------------------------------------------------------- */
  185. static inline void usbResetDataToggling(void)
  186. {
  187. #if USB_CFG_HAVE_INTRIN_ENDPOINT
  188. USB_SET_DATATOKEN1(USB_INITIAL_DATATOKEN); /* reset data toggling for interrupt endpoint */
  189. # if USB_CFG_HAVE_INTRIN_ENDPOINT3
  190. USB_SET_DATATOKEN3(USB_INITIAL_DATATOKEN); /* reset data toggling for interrupt endpoint */
  191. # endif
  192. #endif
  193. }
  194. static inline void usbResetStall(void)
  195. {
  196. #if USB_CFG_IMPLEMENT_HALT && USB_CFG_HAVE_INTRIN_ENDPOINT
  197. usbTxLen1 = USBPID_NAK;
  198. #if USB_CFG_HAVE_INTRIN_ENDPOINT3
  199. usbTxLen3 = USBPID_NAK;
  200. #endif
  201. #endif
  202. }
  203. /* ------------------------------------------------------------------------- */
  204. #if USB_CFG_HAVE_INTRIN_ENDPOINT
  205. USB_PUBLIC void usbSetInterrupt(uchar *data, uchar len)
  206. {
  207. uchar *p, i;
  208. #if USB_CFG_IMPLEMENT_HALT
  209. if(usbTxLen1 == USBPID_STALL)
  210. return;
  211. #endif
  212. #if 0 /* No runtime checks! Caller is responsible for valid data! */
  213. if(len > 8) /* interrupt transfers are limited to 8 bytes */
  214. len = 8;
  215. #endif
  216. if(usbTxLen1 & 0x10){ /* packet buffer was empty */
  217. usbTxBuf1[0] ^= USBPID_DATA0 ^ USBPID_DATA1; /* toggle token */
  218. }else{
  219. usbTxLen1 = USBPID_NAK; /* avoid sending outdated (overwritten) interrupt data */
  220. }
  221. p = usbTxBuf1 + 1;
  222. for(i=len;i--;)
  223. *p++ = *data++;
  224. usbCrc16Append(&usbTxBuf1[1], len);
  225. usbTxLen1 = len + 4; /* len must be given including sync byte */
  226. DBG2(0x21, usbTxBuf1, len + 3);
  227. }
  228. #endif
  229. #if USB_CFG_HAVE_INTRIN_ENDPOINT3
  230. USB_PUBLIC void usbSetInterrupt3(uchar *data, uchar len)
  231. {
  232. uchar *p, i;
  233. if(usbTxLen3 & 0x10){ /* packet buffer was empty */
  234. usbTxBuf3[0] ^= USBPID_DATA0 ^ USBPID_DATA1; /* toggle token */
  235. }else{
  236. usbTxLen3 = USBPID_NAK; /* avoid sending outdated (overwritten) interrupt data */
  237. }
  238. p = usbTxBuf3 + 1;
  239. for(i=len;i--;)
  240. *p++ = *data++;
  241. usbCrc16Append(&usbTxBuf3[1], len);
  242. usbTxLen3 = len + 4; /* len must be given including sync byte */
  243. DBG2(0x23, usbTxBuf3, len + 3);
  244. }
  245. #endif
  246. static uchar usbRead(uchar *data, uchar len)
  247. {
  248. #if USB_CFG_IMPLEMENT_FN_READ
  249. if(usbMsgFlags & USB_FLG_USE_DEFAULT_RW){
  250. #endif
  251. uchar i = len, *r = usbMsgPtr;
  252. if(usbMsgFlags & USB_FLG_MSGPTR_IS_ROM){ /* ROM data */
  253. while(i--){
  254. uchar c = PRG_RDB(r); /* assign to char size variable to enforce byte ops */
  255. *data++ = c;
  256. r++;
  257. }
  258. }else{ /* RAM data */
  259. while(i--)
  260. *data++ = *r++;
  261. }
  262. usbMsgPtr = r;
  263. return len;
  264. #if USB_CFG_IMPLEMENT_FN_READ
  265. }else{
  266. if(len != 0) /* don't bother app with 0 sized reads */
  267. return usbFunctionRead(data, len);
  268. return 0;
  269. }
  270. #endif
  271. }
  272. #define GET_DESCRIPTOR(cfgProp, staticName) \
  273. if(cfgProp){ \
  274. if((cfgProp) & USB_PROP_IS_RAM) \
  275. flags &= ~USB_FLG_MSGPTR_IS_ROM; \
  276. if((cfgProp) & USB_PROP_IS_DYNAMIC){ \
  277. replyLen = usbFunctionDescriptor(rq); \
  278. }else{ \
  279. replyData = (uchar *)(staticName); \
  280. SET_REPLY_LEN((cfgProp) & 0xff); \
  281. } \
  282. }
  283. /* We use if() instead of #if in the macro above because #if can't be used
  284. * in macros and the compiler optimizes constant conditions anyway.
  285. */
  286. /* Don't make this function static to avoid inlining.
  287. * The entire function would become too large and exceed the range of
  288. * relative jumps.
  289. * 2006-02-25: Either gcc 3.4.3 is better than the gcc used when the comment
  290. * above was written, or other parts of the code have changed. We now get
  291. * better results with an inlined function. Test condition: PowerSwitch code.
  292. */
  293. static void usbProcessRx(uchar *data, uchar len)
  294. {
  295. usbRequest_t *rq = (usbRequest_t *)((void *)data);
  296. uchar replyLen = 0, flags = USB_FLG_USE_DEFAULT_RW;
  297. /* We use if() cascades because the compare is done byte-wise while switch()
  298. * is int-based. The if() cascades are therefore more efficient.
  299. */
  300. /* usbRxToken can be:
  301. * 0x2d 00101101 (USBPID_SETUP for endpoint 0)
  302. * 0xe1 11100001 (USBPID_OUT for endpoint 0)
  303. * 0xff 11111111 (USBPID_OUT for endpoint 1)
  304. */
  305. DBG2(0x10 + ((usbRxToken >> 1) & 3), data, len); /* SETUP0=12; OUT0=10; OUT1=13 */
  306. #ifdef USB_RX_USER_HOOK
  307. USB_RX_USER_HOOK(data, len)
  308. #endif
  309. #if USB_CFG_IMPLEMENT_FN_WRITEOUT
  310. if(usbRxToken < 0x10){ /* endpoint number in usbRxToken */
  311. usbFunctionWriteOut(data, len);
  312. return; /* no reply expected, hence no usbMsgPtr, usbMsgFlags, usbMsgLen set */
  313. }
  314. #endif
  315. if(usbRxToken == (uchar)USBPID_SETUP){
  316. usbTxLen = USBPID_NAK; /* abort pending transmit */
  317. if(len == 8){ /* Setup size must be always 8 bytes. Ignore otherwise. */
  318. uchar type = rq->bmRequestType & USBRQ_TYPE_MASK;
  319. if(type == USBRQ_TYPE_STANDARD){
  320. #define SET_REPLY_LEN(len) replyLen = (len); usbMsgPtr = replyData
  321. /* This macro ensures that replyLen and usbMsgPtr are always set in the same way.
  322. * That allows optimization of common code in if() branches */
  323. uchar *replyData = usbTxBuf + 9; /* there is 3 bytes free space at the end of the buffer */
  324. replyData[0] = 0; /* common to USBRQ_GET_STATUS and USBRQ_GET_INTERFACE */
  325. if(rq->bRequest == USBRQ_GET_STATUS){ /* 0 */
  326. uchar __attribute__((__unused__)) recipient = rq->bmRequestType & USBRQ_RCPT_MASK; /* assign arith ops to variables to enforce byte size */
  327. #if USB_CFG_IS_SELF_POWERED
  328. if(recipient == USBRQ_RCPT_DEVICE)
  329. replyData[0] = USB_CFG_IS_SELF_POWERED;
  330. #endif
  331. #if USB_CFG_HAVE_INTRIN_ENDPOINT && USB_CFG_IMPLEMENT_HALT
  332. if(recipient == USBRQ_RCPT_ENDPOINT && rq->wIndex.bytes[0] == 0x81) /* request status for endpoint 1 */
  333. replyData[0] = usbTxLen1 == USBPID_STALL;
  334. #endif
  335. replyData[1] = 0;
  336. SET_REPLY_LEN(2);
  337. }else if(rq->bRequest == USBRQ_SET_ADDRESS){ /* 5 */
  338. usbNewDeviceAddr = rq->wValue.bytes[0];
  339. #ifdef USB_SET_ADDRESS_HOOK
  340. USB_SET_ADDRESS_HOOK();
  341. #endif
  342. }else if(rq->bRequest == USBRQ_GET_DESCRIPTOR){ /* 6 */
  343. flags = USB_FLG_MSGPTR_IS_ROM | USB_FLG_USE_DEFAULT_RW;
  344. if(rq->wValue.bytes[1] == USBDESCR_DEVICE){ /* 1 */
  345. GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_DEVICE, usbDescriptorDevice)
  346. }else if(rq->wValue.bytes[1] == USBDESCR_CONFIG){ /* 2 */
  347. GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_CONFIGURATION, usbDescriptorConfiguration)
  348. }else if(rq->wValue.bytes[1] == USBDESCR_STRING){ /* 3 */
  349. #if USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC
  350. if(USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_RAM)
  351. flags &= ~USB_FLG_MSGPTR_IS_ROM;
  352. replyLen = usbFunctionDescriptor(rq);
  353. #else /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */
  354. if(rq->wValue.bytes[0] == 0){ /* descriptor index */
  355. GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_0, usbDescriptorString0)
  356. }else if(rq->wValue.bytes[0] == 1){
  357. GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_VENDOR, usbDescriptorStringVendor)
  358. }else if(rq->wValue.bytes[0] == 2){
  359. GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_PRODUCT, usbDescriptorStringDevice)
  360. }else if(rq->wValue.bytes[0] == 3){
  361. GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER, usbDescriptorStringSerialNumber)
  362. }else if(USB_CFG_DESCR_PROPS_UNKNOWN & USB_PROP_IS_DYNAMIC){
  363. replyLen = usbFunctionDescriptor(rq);
  364. }
  365. #endif /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */
  366. #if USB_CFG_DESCR_PROPS_HID_REPORT /* only support HID descriptors if enabled */
  367. }else if(rq->wValue.bytes[1] == USBDESCR_HID){ /* 0x21 */
  368. GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID, usbDescriptorConfiguration + 18)
  369. }else if(rq->wValue.bytes[1] == USBDESCR_HID_REPORT){ /* 0x22 */
  370. GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID_REPORT, usbDescriptorHidReport)
  371. #endif /* USB_CFG_DESCR_PROPS_HID_REPORT */
  372. }else if(USB_CFG_DESCR_PROPS_UNKNOWN & USB_PROP_IS_DYNAMIC){
  373. replyLen = usbFunctionDescriptor(rq);
  374. }
  375. }else if(rq->bRequest == USBRQ_GET_CONFIGURATION){ /* 8 */
  376. replyData = &usbConfiguration; /* send current configuration value */
  377. SET_REPLY_LEN(1);
  378. }else if(rq->bRequest == USBRQ_SET_CONFIGURATION){ /* 9 */
  379. usbConfiguration = rq->wValue.bytes[0];
  380. usbResetStall();
  381. }else if(rq->bRequest == USBRQ_GET_INTERFACE){ /* 10 */
  382. SET_REPLY_LEN(1);
  383. #if USB_CFG_HAVE_INTRIN_ENDPOINT
  384. }else if(rq->bRequest == USBRQ_SET_INTERFACE){ /* 11 */
  385. usbResetDataToggling();
  386. usbResetStall();
  387. # if USB_CFG_IMPLEMENT_HALT
  388. }else if(rq->bRequest == USBRQ_CLEAR_FEATURE || rq->bRequest == USBRQ_SET_FEATURE){ /* 1|3 */
  389. if(rq->wValue.bytes[0] == 0 && rq->wIndex.bytes[0] == 0x81){ /* feature 0 == HALT for endpoint == 1 */
  390. usbTxLen1 = rq->bRequest == USBRQ_CLEAR_FEATURE ? USBPID_NAK : USBPID_STALL;
  391. usbResetDataToggling();
  392. }
  393. # endif
  394. #endif
  395. }else{
  396. /* the following requests can be ignored, send default reply */
  397. /* 1: CLEAR_FEATURE, 3: SET_FEATURE, 7: SET_DESCRIPTOR */
  398. /* 12: SYNCH_FRAME */
  399. }
  400. #undef SET_REPLY_LEN
  401. }else{ /* not a standard request -- must be vendor or class request */
  402. replyLen = usbFunctionSetup(data);
  403. }
  404. #if USB_CFG_IMPLEMENT_FN_READ || USB_CFG_IMPLEMENT_FN_WRITE
  405. if(replyLen == 0xff){ /* use user-supplied read/write function */
  406. if((rq->bmRequestType & USBRQ_DIR_MASK) == USBRQ_DIR_DEVICE_TO_HOST){
  407. replyLen = rq->wLength.bytes[0]; /* IN transfers only */
  408. }
  409. flags &= ~USB_FLG_USE_DEFAULT_RW; /* we have no valid msg, use user supplied read/write functions */
  410. }else /* The 'else' prevents that we limit a replyLen of 0xff to the maximum transfer len. */
  411. #endif
  412. if(!rq->wLength.bytes[1] && replyLen > rq->wLength.bytes[0]) /* limit length to max */
  413. replyLen = rq->wLength.bytes[0];
  414. }
  415. /* make sure that data packets which are sent as ACK to an OUT transfer are always zero sized */
  416. }else{ /* DATA packet from out request */
  417. #if USB_CFG_IMPLEMENT_FN_WRITE
  418. if(!(usbMsgFlags & USB_FLG_USE_DEFAULT_RW)){
  419. uchar rval = usbFunctionWrite(data, len);
  420. replyLen = 0xff;
  421. if(rval == 0xff){ /* an error occurred */
  422. usbMsgLen = 0xff; /* cancel potentially pending data packet for ACK */
  423. usbTxLen = USBPID_STALL;
  424. }else if(rval != 0){ /* This was the final package */
  425. replyLen = 0; /* answer with a zero-sized data packet */
  426. }
  427. flags = 0; /* start with a DATA1 package, stay with user supplied write() function */
  428. }
  429. #endif
  430. }
  431. usbMsgFlags = flags;
  432. usbMsgLen = replyLen;
  433. }
  434. /* ------------------------------------------------------------------------- */
  435. static void usbBuildTxBlock(void)
  436. {
  437. uchar wantLen, len, txLen, token;
  438. wantLen = usbMsgLen;
  439. if(wantLen > 8)
  440. wantLen = 8;
  441. usbMsgLen -= wantLen;
  442. token = USBPID_DATA1;
  443. if(usbMsgFlags & USB_FLG_TX_PACKET)
  444. token = USBPID_DATA0;
  445. usbMsgFlags++;
  446. len = usbRead(usbTxBuf + 1, wantLen);
  447. if(len <= 8){ /* valid data packet */
  448. usbCrc16Append(&usbTxBuf[1], len);
  449. txLen = len + 4; /* length including sync byte */
  450. if(len < 8) /* a partial package identifies end of message */
  451. usbMsgLen = 0xff;
  452. }else{
  453. txLen = USBPID_STALL; /* stall the endpoint */
  454. usbMsgLen = 0xff;
  455. }
  456. usbTxBuf[0] = token;
  457. usbTxLen = txLen;
  458. DBG2(0x20, usbTxBuf, txLen-1);
  459. }
  460. /* ------------------------------------------------------------------------- */
  461. static inline uchar isNotSE0(void)
  462. {
  463. uchar rval;
  464. /* We want to do
  465. * return (USBIN & USBMASK);
  466. * here, but the compiler does int-expansion acrobatics.
  467. * We can avoid this by assigning to a char-sized variable.
  468. */
  469. rval = USBIN & USBMASK;
  470. return rval;
  471. }
  472. static inline void usbHandleResetHook(uchar notResetState)
  473. {
  474. #ifdef USB_RESET_HOOK
  475. static uchar wasReset;
  476. uchar isReset = !notResetState;
  477. if(wasReset != isReset){
  478. USB_RESET_HOOK(isReset);
  479. wasReset = isReset;
  480. }
  481. #endif
  482. }
  483. /* ------------------------------------------------------------------------- */
  484. USB_PUBLIC void usbPoll(void)
  485. {
  486. schar len;
  487. uchar i;
  488. if((len = usbRxLen) > 0){
  489. /* We could check CRC16 here -- but ACK has already been sent anyway. If you
  490. * need data integrity checks with this driver, check the CRC in your app
  491. * code and report errors back to the host. Since the ACK was already sent,
  492. * retries must be handled on application level.
  493. * unsigned crc = usbCrc16(buffer + 1, usbRxLen - 3);
  494. */
  495. usbProcessRx(usbRxBuf + USB_BUFSIZE + 1 - usbInputBufOffset, len - 3);
  496. #if USB_CFG_HAVE_FLOWCONTROL
  497. if(usbRxLen > 0) /* only mark as available if not inactivated */
  498. usbRxLen = 0;
  499. #else
  500. usbRxLen = 0; /* mark rx buffer as available */
  501. #endif
  502. }
  503. if(usbTxLen & 0x10){ /* transmit system idle */
  504. if(usbMsgLen != 0xff){ /* transmit data pending? */
  505. usbBuildTxBlock();
  506. }
  507. }
  508. for(i = 10; i > 0; i--){
  509. if(isNotSE0())
  510. break;
  511. }
  512. if(i == 0){ /* RESET condition, called multiple times during reset */
  513. usbNewDeviceAddr = 0;
  514. usbDeviceAddr = 0;
  515. usbResetStall();
  516. DBG1(0xff, 0, 0);
  517. }
  518. usbHandleResetHook(i);
  519. }
  520. /* ------------------------------------------------------------------------- */
  521. USB_PUBLIC void usbInit(void)
  522. {
  523. #if USB_INTR_CFG_SET != 0
  524. USB_INTR_CFG |= USB_INTR_CFG_SET;
  525. #endif
  526. #if USB_INTR_CFG_CLR != 0
  527. USB_INTR_CFG &= ~(USB_INTR_CFG_CLR);
  528. #endif
  529. USB_INTR_ENABLE |= (1 << USB_INTR_ENABLE_BIT);
  530. usbResetDataToggling();
  531. }
  532. /* ------------------------------------------------------------------------- */