My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

usbcore.cpp 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /*----------------------------------------------------------------------------
  2. * U S B - K e r n e l
  3. *----------------------------------------------------------------------------
  4. * Name: usbcore.c
  5. * Purpose: USB Core Module
  6. * Version: V1.20
  7. *----------------------------------------------------------------------------
  8. * This software is supplied "AS IS" without any warranties, express,
  9. * implied or statutory, including but not limited to the implied
  10. * warranties of fitness for purpose, satisfactory quality and
  11. * noninfringement. Keil extends you a royalty-free right to reproduce
  12. * and distribute executable files created using this software for use
  13. * on NXP Semiconductors LPC family microcontroller devices only. Nothing
  14. * else gives you the right to use this software.
  15. *
  16. * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
  17. *----------------------------------------------------------------------------
  18. * History:
  19. * V1.20 Added vendor specific requests
  20. * Changed string descriptor handling
  21. * Reworked Endpoint0
  22. * V1.00 Initial Version
  23. *----------------------------------------------------------------------------*/
  24. extern "C" {
  25. #include "lpc_types.h"
  26. }
  27. #include "usb.h"
  28. #include "usbcfg.h"
  29. #include "usbhw.h"
  30. #include "usbcore.h"
  31. #include "usbdesc.h"
  32. #include "usbuser.h"
  33. #include "msc.h"
  34. #include "mscuser.h"
  35. extern MSC_CSW CSW;
  36. #include "cdc.h"
  37. #include "cdcuser.h"
  38. #define __packed __attribute__((__packed__))
  39. uint16_t USB_DeviceStatus;
  40. uint8_t USB_DeviceAddress;
  41. uint8_t USB_Configuration;
  42. uint32_t USB_EndPointMask;
  43. uint32_t USB_EndPointHalt;
  44. uint32_t USB_EndPointStall; /* EP must stay stalled */
  45. uint8_t USB_NumInterfaces;
  46. uint8_t USB_AltSetting[USB_IF_NUM];
  47. uint8_t EP0Buf[USB_MAX_PACKET0];
  48. USB_EP_DATA EP0Data;
  49. USB_SETUP_PACKET SetupPacket;
  50. /*
  51. * Reset USB Core
  52. * Parameters: None
  53. * Return Value: None
  54. */
  55. void USB_ResetCore(void) {
  56. USB_DeviceStatus = USB_POWER;
  57. USB_DeviceAddress = 0;
  58. USB_Configuration = 0;
  59. USB_EndPointMask = 0x00010001;
  60. USB_EndPointHalt = 0x00000000;
  61. USB_EndPointStall = 0x00000000;
  62. }
  63. /*
  64. * USB Request - Setup Stage
  65. * Parameters: None (global SetupPacket)
  66. * Return Value: None
  67. */
  68. void USB_SetupStage(void) {
  69. USB_ReadEP(0x00, (uint8_t *) &SetupPacket);
  70. }
  71. /*
  72. * USB Request - Data In Stage
  73. * Parameters: None (global EP0Data)
  74. * Return Value: None
  75. */
  76. void USB_DataInStage(void) {
  77. uint32_t cnt;
  78. if (EP0Data.Count > USB_MAX_PACKET0) {
  79. cnt = USB_MAX_PACKET0;
  80. } else {
  81. cnt = EP0Data.Count;
  82. }
  83. cnt = USB_WriteEP(0x80, EP0Data.pData, cnt);
  84. EP0Data.pData += cnt;
  85. EP0Data.Count -= cnt;
  86. }
  87. /*
  88. * USB Request - Data Out Stage
  89. * Parameters: None (global EP0Data)
  90. * Return Value: None
  91. */
  92. void USB_DataOutStage(void) {
  93. uint32_t cnt;
  94. cnt = USB_ReadEP(0x00, EP0Data.pData);
  95. EP0Data.pData += cnt;
  96. EP0Data.Count -= cnt;
  97. }
  98. /*
  99. * USB Request - Status In Stage
  100. * Parameters: None
  101. * Return Value: None
  102. */
  103. void USB_StatusInStage(void) {
  104. USB_WriteEP(0x80, nullptr, 0);
  105. }
  106. /*
  107. * USB Request - Status Out Stage
  108. * Parameters: None
  109. * Return Value: None
  110. */
  111. void USB_StatusOutStage(void) {
  112. USB_ReadEP(0x00, EP0Buf);
  113. }
  114. /*
  115. * Get Status USB Request
  116. * Parameters: None (global SetupPacket)
  117. * Return Value: TRUE - Success, FALSE - Error
  118. */
  119. __inline uint32_t USB_ReqGetStatus(void) {
  120. uint32_t n, m;
  121. switch (SetupPacket.bmRequestType.BM.Recipient) {
  122. case REQUEST_TO_DEVICE:
  123. EP0Data.pData = (uint8_t *) &USB_DeviceStatus;
  124. break;
  125. case REQUEST_TO_INTERFACE:
  126. if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
  127. *((__packed uint16_t *) EP0Buf) = 0;
  128. *((uint16_t *) EP0Buf) = 0;
  129. EP0Data.pData = EP0Buf;
  130. } else {
  131. return (FALSE);
  132. }
  133. break;
  134. case REQUEST_TO_ENDPOINT:
  135. n = SetupPacket.wIndex.WB.L & 0x8F;
  136. m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
  137. if (((USB_Configuration != 0) || ((n & 0x0F) == 0)) && (USB_EndPointMask & m)) {
  138. *((__packed uint16_t *) EP0Buf) = (USB_EndPointHalt & m) ? 1 : 0;
  139. *((uint16_t *) EP0Buf) = (USB_EndPointHalt & m) ? 1 : 0;
  140. EP0Data.pData = EP0Buf;
  141. } else {
  142. return (FALSE);
  143. }
  144. break;
  145. default:
  146. return (FALSE);
  147. }
  148. return (TRUE);
  149. }
  150. /*
  151. * Set/Clear Feature USB Request
  152. * Parameters: sc: 0 - Clear, 1 - Set
  153. * (global SetupPacket)
  154. * Return Value: TRUE - Success, FALSE - Error
  155. */
  156. __inline uint32_t USB_ReqSetClrFeature(uint32_t sc) {
  157. uint32_t n, m;
  158. switch (SetupPacket.bmRequestType.BM.Recipient) {
  159. case REQUEST_TO_DEVICE:
  160. if (SetupPacket.wValue.W == USB_FEATURE_REMOTE_WAKEUP) {
  161. if (sc) {
  162. USB_WakeUpCfg(TRUE);
  163. USB_DeviceStatus |= USB_GETSTATUS_REMOTE_WAKEUP;
  164. } else {
  165. USB_WakeUpCfg(FALSE);
  166. USB_DeviceStatus &= ~USB_GETSTATUS_REMOTE_WAKEUP;
  167. }
  168. } else {
  169. return (FALSE);
  170. }
  171. break;
  172. case REQUEST_TO_INTERFACE:
  173. return (FALSE);
  174. case REQUEST_TO_ENDPOINT:
  175. n = SetupPacket.wIndex.WB.L & 0x8F;
  176. m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
  177. if ((USB_Configuration != 0) && ((n & 0x0F) != 0) && (USB_EndPointMask & m)) {
  178. if (SetupPacket.wValue.W == USB_FEATURE_ENDPOINT_STALL) {
  179. if (sc) {
  180. USB_SetStallEP(n);
  181. USB_EndPointHalt |= m;
  182. } else {
  183. if ((USB_EndPointStall & m) != 0) {
  184. return (TRUE);
  185. }
  186. USB_ClrStallEP(n);
  187. #if (USB_MSC)
  188. if ((n == MSC_EP_IN) && ((USB_EndPointHalt & m) != 0)) {
  189. /* Compliance Test: rewrite CSW after unstall */
  190. if (CSW.dSignature == MSC_CSW_Signature) {
  191. USB_WriteEP(MSC_EP_IN, (uint8_t *) &CSW, sizeof(CSW));
  192. }
  193. }
  194. #endif
  195. USB_EndPointHalt &= ~m;
  196. }
  197. } else {
  198. return (FALSE);
  199. }
  200. } else {
  201. return (FALSE);
  202. }
  203. break;
  204. default:
  205. return (FALSE);
  206. }
  207. return (TRUE);
  208. }
  209. /*
  210. * Set Address USB Request
  211. * Parameters: None (global SetupPacket)
  212. * Return Value: TRUE - Success, FALSE - Error
  213. */
  214. __inline uint32_t USB_ReqSetAddress(void) {
  215. switch (SetupPacket.bmRequestType.BM.Recipient) {
  216. case REQUEST_TO_DEVICE:
  217. USB_DeviceAddress = 0x80 | SetupPacket.wValue.WB.L;
  218. break;
  219. default:
  220. return (FALSE);
  221. }
  222. return (TRUE);
  223. }
  224. /*
  225. * Get Descriptor USB Request
  226. * Parameters: None (global SetupPacket)
  227. * Return Value: TRUE - Success, FALSE - Error
  228. */
  229. __inline uint32_t USB_ReqGetDescriptor(void) {
  230. uint8_t *pD;
  231. uint32_t len, n;
  232. switch (SetupPacket.bmRequestType.BM.Recipient) {
  233. case REQUEST_TO_DEVICE:
  234. switch (SetupPacket.wValue.WB.H) {
  235. case USB_DEVICE_DESCRIPTOR_TYPE:
  236. EP0Data.pData = (uint8_t *) USB_DeviceDescriptor;
  237. len = USB_DEVICE_DESC_SIZE;
  238. break;
  239. case USB_CONFIGURATION_DESCRIPTOR_TYPE:
  240. pD = (uint8_t *) USB_ConfigDescriptor;
  241. for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
  242. if (((USB_CONFIGURATION_DESCRIPTOR *) pD)->bLength != 0) {
  243. pD += ((USB_CONFIGURATION_DESCRIPTOR *) pD)->wTotalLength;
  244. }
  245. }
  246. if (((USB_CONFIGURATION_DESCRIPTOR *) pD)->bLength == 0) {
  247. return (FALSE);
  248. }
  249. EP0Data.pData = pD;
  250. len = ((USB_CONFIGURATION_DESCRIPTOR *) pD)->wTotalLength;
  251. break;
  252. case USB_STRING_DESCRIPTOR_TYPE:
  253. pD = (uint8_t *) USB_StringDescriptor;
  254. for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
  255. if (((USB_STRING_DESCRIPTOR *) pD)->bLength != 0) {
  256. pD += ((USB_STRING_DESCRIPTOR *) pD)->bLength;
  257. }
  258. }
  259. if (((USB_STRING_DESCRIPTOR *) pD)->bLength == 0) {
  260. return (FALSE);
  261. }
  262. EP0Data.pData = pD;
  263. len = ((USB_STRING_DESCRIPTOR *) EP0Data.pData)->bLength;
  264. break;
  265. default:
  266. return (FALSE);
  267. }
  268. break;
  269. case REQUEST_TO_INTERFACE:
  270. switch (SetupPacket.wValue.WB.H) {
  271. default:
  272. return (FALSE);
  273. }
  274. // break;
  275. default:
  276. return (FALSE);
  277. }
  278. if (EP0Data.Count > len) {
  279. EP0Data.Count = len;
  280. }
  281. return (TRUE);
  282. }
  283. /*
  284. * Get Configuration USB Request
  285. * Parameters: None (global SetupPacket)
  286. * Return Value: TRUE - Success, FALSE - Error
  287. */
  288. __inline uint32_t USB_ReqGetConfiguration(void) {
  289. switch (SetupPacket.bmRequestType.BM.Recipient) {
  290. case REQUEST_TO_DEVICE:
  291. EP0Data.pData = &USB_Configuration;
  292. break;
  293. default:
  294. return (FALSE);
  295. }
  296. return (TRUE);
  297. }
  298. /*
  299. * Set Configuration USB Request
  300. * Parameters: None (global SetupPacket)
  301. * Return Value: TRUE - Success, FALSE - Error
  302. */
  303. __inline uint32_t USB_ReqSetConfiguration(void) {
  304. USB_COMMON_DESCRIPTOR *pD;
  305. uint32_t alt = 0;
  306. uint32_t n, m;
  307. uint32_t tmp;
  308. switch (SetupPacket.bmRequestType.BM.Recipient) {
  309. case REQUEST_TO_DEVICE:
  310. if (SetupPacket.wValue.WB.L) {
  311. pD = (USB_COMMON_DESCRIPTOR *) USB_ConfigDescriptor;
  312. while (pD->bLength) {
  313. switch (pD->bDescriptorType) {
  314. case USB_CONFIGURATION_DESCRIPTOR_TYPE:
  315. if (((USB_CONFIGURATION_DESCRIPTOR *) pD)->bConfigurationValue == SetupPacket.wValue.WB.L) {
  316. USB_Configuration = SetupPacket.wValue.WB.L;
  317. USB_NumInterfaces = ((USB_CONFIGURATION_DESCRIPTOR *) pD)->bNumInterfaces;
  318. for (n = 0; n < USB_IF_NUM; n++) {
  319. USB_AltSetting[n] = 0;
  320. }
  321. for (n = 1; n < 16; n++) {
  322. if (USB_EndPointMask & (1 << n)) {
  323. USB_DisableEP(n);
  324. }
  325. if (USB_EndPointMask & ((1 << 16) << n)) {
  326. USB_DisableEP(n | 0x80);
  327. }
  328. }
  329. USB_EndPointMask = 0x00010001;
  330. USB_EndPointHalt = 0x00000000;
  331. USB_EndPointStall = 0x00000000;
  332. USB_Configure(TRUE);
  333. if (((USB_CONFIGURATION_DESCRIPTOR *) pD)->bmAttributes & USB_CONFIG_POWERED_MASK) {
  334. USB_DeviceStatus |= USB_GETSTATUS_SELF_POWERED;
  335. } else {
  336. USB_DeviceStatus &= ~(USB_GETSTATUS_SELF_POWERED);
  337. }
  338. } else {
  339. // (uint8_t *)pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
  340. tmp = (uint32_t) pD;
  341. tmp += ((USB_CONFIGURATION_DESCRIPTOR *) pD)->wTotalLength;
  342. pD = (USB_COMMON_DESCRIPTOR *) tmp;
  343. continue;
  344. }
  345. break;
  346. case USB_INTERFACE_DESCRIPTOR_TYPE:
  347. alt = ((USB_INTERFACE_DESCRIPTOR *) pD)->bAlternateSetting;
  348. break;
  349. case USB_ENDPOINT_DESCRIPTOR_TYPE:
  350. if (alt == 0) {
  351. n = ((USB_ENDPOINT_DESCRIPTOR *) pD)->bEndpointAddress & 0x8F;
  352. m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
  353. USB_EndPointMask |= m;
  354. USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *) pD);
  355. USB_EnableEP(n);
  356. USB_ResetEP(n);
  357. }
  358. break;
  359. }
  360. // (uint8_t *)pD += pD->bLength;
  361. tmp = (uint32_t) pD;
  362. tmp += pD->bLength;
  363. pD = (USB_COMMON_DESCRIPTOR *) tmp;
  364. }
  365. } else {
  366. USB_Configuration = 0;
  367. for (n = 1; n < 16; n++) {
  368. if (USB_EndPointMask & (1 << n)) {
  369. USB_DisableEP(n);
  370. }
  371. if (USB_EndPointMask & ((1 << 16) << n)) {
  372. USB_DisableEP(n | 0x80);
  373. }
  374. }
  375. USB_EndPointMask = 0x00010001;
  376. USB_EndPointHalt = 0x00000000;
  377. USB_EndPointStall = 0x00000000;
  378. USB_Configure(FALSE);
  379. }
  380. if (USB_Configuration != SetupPacket.wValue.WB.L) {
  381. return (FALSE);
  382. }
  383. break;
  384. default:
  385. return (FALSE);
  386. }
  387. return (TRUE);
  388. }
  389. /*
  390. * Get Interface USB Request
  391. * Parameters: None (global SetupPacket)
  392. * Return Value: TRUE - Success, FALSE - Error
  393. */
  394. __inline uint32_t USB_ReqGetInterface(void) {
  395. switch (SetupPacket.bmRequestType.BM.Recipient) {
  396. case REQUEST_TO_INTERFACE:
  397. if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
  398. EP0Data.pData = USB_AltSetting + SetupPacket.wIndex.WB.L;
  399. } else {
  400. return (FALSE);
  401. }
  402. break;
  403. default:
  404. return (FALSE);
  405. }
  406. return (TRUE);
  407. }
  408. /*
  409. * Set Interface USB Request
  410. * Parameters: None (global SetupPacket)
  411. * Return Value: TRUE - Success, FALSE - Error
  412. */
  413. __inline uint32_t USB_ReqSetInterface(void) {
  414. USB_COMMON_DESCRIPTOR *pD;
  415. uint32_t ifn = 0, alt = 0, old = 0, msk = 0;
  416. uint32_t n, m;
  417. uint32_t set;
  418. uint32_t tmp;
  419. switch (SetupPacket.bmRequestType.BM.Recipient) {
  420. case REQUEST_TO_INTERFACE:
  421. if (USB_Configuration == 0)
  422. return (FALSE);
  423. set = FALSE;
  424. pD = (USB_COMMON_DESCRIPTOR *) USB_ConfigDescriptor;
  425. while (pD->bLength) {
  426. switch (pD->bDescriptorType) {
  427. case USB_CONFIGURATION_DESCRIPTOR_TYPE:
  428. if (((USB_CONFIGURATION_DESCRIPTOR *) pD)->bConfigurationValue != USB_Configuration) {
  429. // (uint8_t *)pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
  430. tmp = (uint32_t) pD;
  431. tmp += ((USB_CONFIGURATION_DESCRIPTOR *) pD)->wTotalLength;
  432. pD = (USB_COMMON_DESCRIPTOR *) tmp;
  433. continue;
  434. }
  435. break;
  436. case USB_INTERFACE_DESCRIPTOR_TYPE:
  437. ifn = ((USB_INTERFACE_DESCRIPTOR *) pD)->bInterfaceNumber;
  438. alt = ((USB_INTERFACE_DESCRIPTOR *) pD)->bAlternateSetting;
  439. msk = 0;
  440. if ((ifn == SetupPacket.wIndex.WB.L) && (alt == SetupPacket.wValue.WB.L)) {
  441. set = TRUE;
  442. old = USB_AltSetting[ifn];
  443. USB_AltSetting[ifn] = (uint8_t) alt;
  444. }
  445. break;
  446. case USB_ENDPOINT_DESCRIPTOR_TYPE:
  447. if (ifn == SetupPacket.wIndex.WB.L) {
  448. n = ((USB_ENDPOINT_DESCRIPTOR *) pD)->bEndpointAddress & 0x8F;
  449. m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
  450. if (alt == SetupPacket.wValue.WB.L) {
  451. USB_EndPointMask |= m;
  452. USB_EndPointHalt &= ~m;
  453. USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *) pD);
  454. USB_EnableEP(n);
  455. USB_ResetEP(n);
  456. msk |= m;
  457. } else if ((alt == old) && ((msk & m) == 0)) {
  458. USB_EndPointMask &= ~m;
  459. USB_EndPointHalt &= ~m;
  460. USB_DisableEP(n);
  461. }
  462. }
  463. break;
  464. }
  465. // (uint8_t *)pD += pD->bLength;
  466. tmp = (uint32_t) pD;
  467. tmp += pD->bLength;
  468. pD = (USB_COMMON_DESCRIPTOR *) tmp;
  469. }
  470. break;
  471. default:
  472. return (FALSE);
  473. }
  474. return (set);
  475. }
  476. /*
  477. * USB Endpoint 0 Event Callback
  478. * Parameters: event
  479. * Return Value: none
  480. */
  481. void USB_EndPoint0(uint32_t event) {
  482. switch (event) {
  483. case USB_EVT_SETUP:
  484. USB_SetupStage();
  485. USB_DirCtrlEP(SetupPacket.bmRequestType.BM.Dir);
  486. EP0Data.Count = SetupPacket.wLength; /* Number of bytes to transfer */
  487. switch (SetupPacket.bmRequestType.BM.Type) {
  488. case REQUEST_STANDARD:
  489. switch (SetupPacket.bRequest) {
  490. case USB_REQUEST_GET_STATUS:
  491. if (!USB_ReqGetStatus()) {
  492. goto stall_i;
  493. }
  494. USB_DataInStage();
  495. break;
  496. case USB_REQUEST_CLEAR_FEATURE:
  497. if (!USB_ReqSetClrFeature(0)) {
  498. goto stall_i;
  499. }
  500. USB_StatusInStage();
  501. #if USB_FEATURE_EVENT
  502. USB_Feature_Event();
  503. #endif
  504. break;
  505. case USB_REQUEST_SET_FEATURE:
  506. if (!USB_ReqSetClrFeature(1)) {
  507. goto stall_i;
  508. }
  509. USB_StatusInStage();
  510. #if USB_FEATURE_EVENT
  511. USB_Feature_Event();
  512. #endif
  513. break;
  514. case USB_REQUEST_SET_ADDRESS:
  515. if (!USB_ReqSetAddress()) {
  516. goto stall_i;
  517. }
  518. USB_StatusInStage();
  519. break;
  520. case USB_REQUEST_GET_DESCRIPTOR:
  521. if (!USB_ReqGetDescriptor()) {
  522. goto stall_i;
  523. }
  524. USB_DataInStage();
  525. break;
  526. case USB_REQUEST_SET_DESCRIPTOR:
  527. USB_SetStallEP(0x00);
  528. EP0Data.Count = 0;
  529. break;
  530. case USB_REQUEST_GET_CONFIGURATION:
  531. if (!USB_ReqGetConfiguration()) {
  532. goto stall_i;
  533. }
  534. USB_DataInStage();
  535. break;
  536. case USB_REQUEST_SET_CONFIGURATION:
  537. if (!USB_ReqSetConfiguration()) {
  538. goto stall_i;
  539. }
  540. USB_StatusInStage();
  541. #if USB_CONFIGURE_EVENT
  542. USB_Configure_Event();
  543. #endif
  544. break;
  545. case USB_REQUEST_GET_INTERFACE:
  546. if (!USB_ReqGetInterface()) {
  547. goto stall_i;
  548. }
  549. USB_DataInStage();
  550. break;
  551. case USB_REQUEST_SET_INTERFACE:
  552. if (!USB_ReqSetInterface()) {
  553. goto stall_i;
  554. }
  555. USB_StatusInStage();
  556. #if USB_INTERFACE_EVENT
  557. USB_Interface_Event();
  558. #endif
  559. break;
  560. default:
  561. goto stall_i;
  562. }
  563. break; /* end case REQUEST_STANDARD */
  564. case REQUEST_CLASS:
  565. switch (SetupPacket.bmRequestType.BM.Recipient) {
  566. case REQUEST_TO_DEVICE:
  567. goto stall_i;
  568. /* not supported */
  569. case REQUEST_TO_INTERFACE:
  570. if (SetupPacket.wIndex.WB.L == USB_MSC_IF_NUM) { /* IF number correct? */
  571. switch (SetupPacket.bRequest) {
  572. case MSC_REQUEST_RESET:
  573. if ((SetupPacket.wValue.W == 0) && /* RESET with invalid parameters -> STALL */
  574. (SetupPacket.wLength == 0)) {
  575. if (MSC_Reset()) {
  576. USB_StatusInStage();
  577. goto setup_class_ok;
  578. }
  579. }
  580. break;
  581. case MSC_REQUEST_GET_MAX_LUN:
  582. if ((SetupPacket.wValue.W == 0) && /* GET_MAX_LUN with invalid parameters -> STALL */
  583. (SetupPacket.wLength == 1)) {
  584. if (MSC_GetMaxLUN()) {
  585. EP0Data.pData = EP0Buf;
  586. USB_DataInStage();
  587. goto setup_class_ok;
  588. }
  589. }
  590. break;
  591. }
  592. }
  593. if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM) || /* IF number correct? */
  594. (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
  595. switch (SetupPacket.bRequest) {
  596. case CDC_SEND_ENCAPSULATED_COMMAND:
  597. EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
  598. goto setup_class_ok;
  599. case CDC_GET_ENCAPSULATED_RESPONSE:
  600. if (CDC_GetEncapsulatedResponse()) {
  601. EP0Data.pData = EP0Buf; /* point to data to be sent */
  602. USB_DataInStage(); /* send requested data */
  603. goto setup_class_ok;
  604. }
  605. break;
  606. case CDC_SET_COMM_FEATURE:
  607. EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
  608. goto setup_class_ok;
  609. case CDC_GET_COMM_FEATURE:
  610. if (CDC_GetCommFeature(SetupPacket.wValue.W)) {
  611. EP0Data.pData = EP0Buf; /* point to data to be sent */
  612. USB_DataInStage(); /* send requested data */
  613. goto setup_class_ok;
  614. }
  615. break;
  616. case CDC_CLEAR_COMM_FEATURE:
  617. if (CDC_ClearCommFeature(SetupPacket.wValue.W)) {
  618. USB_StatusInStage(); /* send Acknowledge */
  619. goto setup_class_ok;
  620. }
  621. break;
  622. case CDC_SET_LINE_CODING:
  623. EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
  624. goto setup_class_ok;
  625. case CDC_GET_LINE_CODING:
  626. if (CDC_GetLineCoding()) {
  627. EP0Data.pData = EP0Buf; /* point to data to be sent */
  628. USB_DataInStage(); /* send requested data */
  629. goto setup_class_ok;
  630. }
  631. break;
  632. case CDC_SET_CONTROL_LINE_STATE:
  633. if (CDC_SetControlLineState(SetupPacket.wValue.W)) {
  634. USB_StatusInStage(); /* send Acknowledge */
  635. goto setup_class_ok;
  636. }
  637. break;
  638. case CDC_SEND_BREAK:
  639. if (CDC_SendBreak(SetupPacket.wValue.W)) {
  640. USB_StatusInStage(); /* send Acknowledge */
  641. goto setup_class_ok;
  642. }
  643. break;
  644. }
  645. }
  646. goto stall_i;
  647. /* not supported */
  648. /* end case REQUEST_TO_INTERFACE */
  649. case REQUEST_TO_ENDPOINT:
  650. goto stall_i;
  651. /* end case REQUEST_TO_ENDPOINT */
  652. default:
  653. goto stall_i;
  654. }
  655. setup_class_ok: /* request finished successfully */
  656. break; /* end case REQUEST_CLASS */
  657. default:
  658. stall_i: USB_SetStallEP(0x80);
  659. EP0Data.Count = 0;
  660. break;
  661. }
  662. break; /* end case USB_EVT_SETUP */
  663. case USB_EVT_OUT:
  664. if (SetupPacket.bmRequestType.BM.Dir == REQUEST_HOST_TO_DEVICE) {
  665. if (EP0Data.Count) { /* still data to receive ? */
  666. USB_DataOutStage(); /* receive data */
  667. if (EP0Data.Count == 0) { /* data complete ? */
  668. switch (SetupPacket.bmRequestType.BM.Type) {
  669. case REQUEST_STANDARD:
  670. goto stall_i;
  671. /* not supported */
  672. case REQUEST_CLASS:
  673. switch (SetupPacket.bmRequestType.BM.Recipient) {
  674. case REQUEST_TO_DEVICE:
  675. goto stall_i;
  676. /* not supported */
  677. case REQUEST_TO_INTERFACE:
  678. if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM) || /* IF number correct? */
  679. (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
  680. switch (SetupPacket.bRequest) {
  681. case CDC_SEND_ENCAPSULATED_COMMAND:
  682. if (CDC_SendEncapsulatedCommand()) {
  683. USB_StatusInStage(); /* send Acknowledge */
  684. goto out_class_ok;
  685. }
  686. break;
  687. case CDC_SET_COMM_FEATURE:
  688. if (CDC_SetCommFeature(SetupPacket.wValue.W)) {
  689. USB_StatusInStage(); /* send Acknowledge */
  690. goto out_class_ok;
  691. }
  692. break;
  693. case CDC_SET_LINE_CODING:
  694. if (CDC_SetLineCoding()) {
  695. USB_StatusInStage(); /* send Acknowledge */
  696. goto out_class_ok;
  697. }
  698. break;
  699. }
  700. }
  701. goto stall_i;
  702. /* end case REQUEST_TO_INTERFACE */
  703. case REQUEST_TO_ENDPOINT:
  704. goto stall_i;
  705. /* end case REQUEST_TO_ENDPOINT */
  706. default:
  707. goto stall_i;
  708. }
  709. out_class_ok: /* request finished successfully */
  710. break; /* end case REQUEST_CLASS */
  711. default:
  712. goto stall_i;
  713. }
  714. }
  715. }
  716. } else {
  717. USB_StatusOutStage(); /* receive Acknowledge */
  718. }
  719. break; /* end case USB_EVT_OUT */
  720. case USB_EVT_IN:
  721. if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
  722. USB_DataInStage(); /* send data */
  723. } else {
  724. if (USB_DeviceAddress & 0x80) {
  725. USB_DeviceAddress &= 0x7F;
  726. USB_SetAddress(USB_DeviceAddress);
  727. }
  728. }
  729. break; /* end case USB_EVT_IN */
  730. case USB_EVT_OUT_STALL:
  731. USB_ClrStallEP(0x00);
  732. break;
  733. case USB_EVT_IN_STALL:
  734. USB_ClrStallEP(0x80);
  735. break;
  736. }
  737. }