usbd_vendor.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. #include "usbd_vendor.h"
  2. #include "usbd_desc.h"
  3. #include "usbd_ctlreq.h"
  4. #include "usbd_vendor_desc.h"
  5. static uint8_t USBD_VENDOR_Init (USBD_HandleTypeDef *pdev,
  6. uint8_t cfgidx);
  7. static uint8_t USBD_VENDOR_DeInit (USBD_HandleTypeDef *pdev,
  8. uint8_t cfgidx);
  9. static uint8_t USBD_VENDOR_Setup (USBD_HandleTypeDef *pdev,
  10. USBD_SetupReqTypedef *req);
  11. static uint8_t USBD_VENDOR_Error (USBD_HandleTypeDef *pdev,
  12. uint8_t epnum);
  13. static uint8_t USBD_VENDOR_EP0_RxReady (USBD_HandleTypeDef *pdev, size_t * );
  14. static uint8_t USBD_VENDOR_EP0_TxSent (USBD_HandleTypeDef *pdev, size_t * );
  15. /* Vendor interface class callbacks structure */
  16. const USBD_ClassTypeDef USBD_VENDOR =
  17. {
  18. .Init = USBD_VENDOR_Init,
  19. .DeInit = USBD_VENDOR_DeInit,
  20. .Error = USBD_VENDOR_Error,
  21. .Setup = USBD_VENDOR_Setup,
  22. .EP0_TxSent = USBD_VENDOR_EP0_TxSent,
  23. .EP0_RxReady = USBD_VENDOR_EP0_RxReady,
  24. .DataIn = NULL,
  25. .DataOut = NULL,
  26. .SOF = NULL,
  27. .pDesc = &USBD_VENDOR_DescriptorHandlers,
  28. };
  29. /**
  30. * @brief USBD_VENDOR_Init
  31. * Initialize the Vendor interface
  32. * @param pdev: device instance
  33. * @param cfgidx: Configuration index
  34. * @retval status
  35. */
  36. static uint8_t USBD_VENDOR_Init (USBD_HandleTypeDef *pdev,
  37. uint8_t cfgidx)
  38. {
  39. uint8_t ret = USBD_OK;
  40. USBD_VENDOR_HandleTypeDef *hvendor;
  41. pdev->pClassData = USBD_malloc(sizeof (USBD_VENDOR_HandleTypeDef));
  42. if(pdev->pClassData == NULL)
  43. {
  44. ret = USBD_FAIL;
  45. }
  46. else
  47. {
  48. hvendor = (USBD_VENDOR_HandleTypeDef*) pdev->pClassData;
  49. USBD_COMMON_ItfTypeDef * pUserIface = ((USBD_COMMON_ItfTypeDef *)pdev->pUserData);
  50. usb_create_transfer( &hvendor->ep0_tx_transfer,
  51. &hvendor->ep0_tx_buffer[0],
  52. sizeof(hvendor->ep0_tx_buffer),
  53. NULL, pdev );
  54. usb_create_transfer( &hvendor->ep0_rx_transfer,
  55. &hvendor->ep0_rx_buffer[0],
  56. sizeof(hvendor->ep0_rx_buffer),
  57. NULL, pdev );
  58. if( 0 == pUserIface->fUsbInit() )
  59. {
  60. if( USBD_STATE_DEFAULT == pdev->dev_state ) // useless
  61. {
  62. if( NULL != pUserIface->fResetEvent )
  63. {
  64. pUserIface->fResetEvent();
  65. }
  66. }
  67. if( pdev->dev_state == USBD_STATE_CONFIGURED )
  68. {
  69. if( NULL != pUserIface->fSetIface )
  70. {
  71. //--------------------------------------------------------------------------------------
  72. // Enumerate interface descriptors and call SetInterface callback
  73. uint16_t nConfDescLength = 0;
  74. const uint8_t * pDescBytePtr = NULL;
  75. #if USBD_GETDESCRIPTORS_ALTMETHOD == 0
  76. ret = USBD_CallGetDescriptorHandler( pdev,
  77. eGetDescriptorHandlerType_Config,
  78. &pbuf,
  79. &len );
  80. #else
  81. USBD_CallGetDescriptorHandlerAlt( ret, pdev, pDescBytePtr, nConfDescLength, GetConfigDescriptor );
  82. #endif
  83. if( USBD_OK == ret )
  84. {
  85. const sUSBConfigurationDescriptor_t * conf = (const sUSBConfigurationDescriptor_t *)pDescBytePtr;
  86. size_t ifidx = 0;
  87. while( nConfDescLength > 0 )
  88. {
  89. sUSBDescriptorHeader_t * pDescHeader = (sUSBDescriptorHeader_t*)pDescBytePtr;
  90. if( pDescHeader->bLength == 0 ) break;
  91. if( USB_DESC_TYPE_INTERFACE == pDescHeader->bDescriptorType )
  92. {
  93. const sUSBInterfaceDescriptor_t * ifdesc = (const sUSBInterfaceDescriptor_t *)pDescBytePtr;
  94. if( ! pUserIface->fSetIface( cfgidx, ifidx++, ifdesc ) )
  95. {
  96. ret = USBD_FAIL;
  97. break;
  98. }
  99. }
  100. nConfDescLength -= pDescHeader->bLength;
  101. pDescBytePtr += (pDescHeader->bLength);
  102. }
  103. (void)conf;
  104. }
  105. //--------------------------------------------------------------------------------------
  106. }
  107. }
  108. }
  109. else
  110. {
  111. ret = USBD_FAIL;
  112. }
  113. }
  114. return ret;
  115. }
  116. /*
  117. static const sUSBInterfaceDescriptor_t * USBD_VENDOR_SearchInterfaceDescriptor( USBD_HandleTypeDef *pdev, uint8_t cfgidx, uint8_t nIf )
  118. {
  119. //--------------------------------------------------------------------------------------
  120. // Enumerate interface descriptors and call ClearInterface callback
  121. (void)cfgidx; // to do - use cfgidx to get configuration descriptor
  122. uint16_t nConfDescLength = 0;
  123. const uint8_t * pDescBytePtr = (const uint8_t * )pdev->pClass->GetFSConfigDescriptor(&nConfDescLength);
  124. const sUSBConfigurationDescriptor_t * conf = (const sUSBConfigurationDescriptor_t *)pDescBytePtr;
  125. while( nConfDescLength > 0 )
  126. {
  127. sUSBDescriptorHeader_t * pDescHeader = (sUSBDescriptorHeader_t*)pDescBytePtr;
  128. if( pDescHeader->bLength == 0 ) break;
  129. if( USB_DESC_TYPE_INTERFACE == pDescHeader->bDescriptorType )
  130. {
  131. const sUSBInterfaceDescriptor_t * ifdesc = (const sUSBInterfaceDescriptor_t *)pDescBytePtr;
  132. if( ifdesc->bInterfaceNumber == nIf )
  133. {
  134. return ifdesc;
  135. }
  136. }
  137. nConfDescLength -= pDescHeader->bLength;
  138. pDescBytePtr += (pDescHeader->bLength);
  139. }
  140. //--------------------------------------------------------------------------------------
  141. return NULL;
  142. }
  143. */
  144. /**
  145. * @brief USBD_VENDOR_DeInit
  146. * DeInitialize the Vendor layer
  147. * @param pdev: device instance
  148. * @param cfgidx: Configuration index
  149. * @retval status
  150. */
  151. static uint8_t USBD_VENDOR_DeInit (USBD_HandleTypeDef *pdev,
  152. uint8_t cfgidx)
  153. {
  154. uint8_t ret = USBD_OK;
  155. /* DeInit physical Interface components */
  156. if(pdev->pClassData != NULL)
  157. {
  158. if( pdev->pUserData != NULL )
  159. {
  160. USBD_COMMON_ItfTypeDef * pUserIface = ((USBD_COMMON_ItfTypeDef *)pdev->pUserData);
  161. if( NULL != pUserIface->fClrIface )
  162. {
  163. if( ((pdev->dev_old_state == USBD_STATE_CONFIGURED)
  164. || (pdev->dev_old_state == USBD_STATE_SUSPENDED) )
  165. && ((pdev->dev_state != USBD_STATE_CONFIGURED)
  166. || (pdev->dev_state != USBD_STATE_SUSPENDED) ) )
  167. {
  168. //--------------------------------------------------------------------------------------
  169. // Enumerate interface descriptors and call ClearInterface callback
  170. (void)cfgidx; // to do - use cfgidx to get configuration descriptor
  171. uint16_t nConfDescLength = 0;
  172. const uint8_t * pDescBytePtr = NULL;
  173. #if USBD_GETDESCRIPTORS_ALTMETHOD == 0
  174. ret = USBD_CallGetDescriptorHandler( pdev,
  175. eGetDescriptorHandlerType_Config,
  176. &pbuf,
  177. &len );
  178. #else
  179. USBD_CallGetDescriptorHandlerAlt( ret, pdev, pDescBytePtr, nConfDescLength, GetConfigDescriptor );
  180. #endif
  181. if( USBD_OK == ret )
  182. {
  183. const sUSBConfigurationDescriptor_t * conf = (const sUSBConfigurationDescriptor_t *)pDescBytePtr;
  184. size_t ifidx = 0;
  185. while( nConfDescLength > 0 )
  186. {
  187. sUSBDescriptorHeader_t * pDescHeader = (sUSBDescriptorHeader_t*)pDescBytePtr;
  188. if( pDescHeader->bLength == 0 ) break;
  189. if( USB_DESC_TYPE_INTERFACE == pDescHeader->bDescriptorType )
  190. {
  191. const sUSBInterfaceDescriptor_t * ifdesc = (const sUSBInterfaceDescriptor_t *)pDescBytePtr;
  192. pUserIface->fClrIface( cfgidx, ifidx++, ifdesc );
  193. }
  194. nConfDescLength -= pDescHeader->bLength;
  195. pDescBytePtr += (pDescHeader->bLength);
  196. }
  197. (void)conf;
  198. }
  199. //--------------------------------------------------------------------------------------
  200. }
  201. }
  202. if( USBD_STATE_DEFAULT == pdev->dev_state )
  203. {
  204. if( NULL != pUserIface->fResetEvent )
  205. {
  206. pUserIface->fResetEvent();
  207. }
  208. }
  209. pUserIface->fUsbDeInit();
  210. USBD_free(pdev->pClassData);
  211. pdev->pClassData = NULL;
  212. }
  213. }
  214. return ret;
  215. }
  216. static uint8_t USBD_VENDOR_Error (USBD_HandleTypeDef *pdev, uint8_t epnum)
  217. {
  218. if(pdev->pClassData != NULL)
  219. {
  220. if( pdev->pUserData != NULL )
  221. {
  222. USBD_COMMON_ItfTypeDef * pUserIface = ((USBD_COMMON_ItfTypeDef *)pdev->pUserData);
  223. if( epnum == 0 )
  224. {
  225. if( NULL != pUserIface->fUsbSetup )
  226. {
  227. pUserIface->fUsbSetup( &pdev->request, false, false );
  228. }
  229. }
  230. else
  231. {
  232. if( NULL != pUserIface->fDataErrHandler )
  233. {
  234. pUserIface->fDataErrHandler( epnum, 0 );
  235. }
  236. }
  237. }
  238. }
  239. return USBD_OK;
  240. }
  241. /**
  242. * @brief USBD_VENDOR_Setup
  243. * Handle the vendor specific requests
  244. * @param pdev: instance
  245. * @param req: usb requests
  246. * @retval status
  247. */
  248. #if VENDOR_USE_COMMON_SETUP
  249. static uint8_t USBD_VENDOR_Setup (USBD_HandleTypeDef *pdev,
  250. USBD_SetupReqTypedef *req)
  251. {
  252. uint8_t ret = USBD_OK;
  253. USBD_VENDOR_HandleTypeDef *hvendor = (USBD_VENDOR_HandleTypeDef*) pdev->pClassData;
  254. USBD_COMMON_ItfTypeDef * pUserIface = ((USBD_COMMON_ItfTypeDef *)pdev->pUserData);
  255. if( NULL == pUserIface->fUsbSetup )
  256. {
  257. return USBD_FAIL; // @fUsbSetup is missing
  258. }
  259. if( NULL == hvendor)
  260. {
  261. return USBD_FAIL;
  262. }
  263. // SETUP filtering:
  264. if( ! pUserIface->fUsbSetup( req, true, false ) )
  265. {
  266. return USBD_FAIL; // do not call @fUsbSetup second time
  267. }
  268. else
  269. {
  270. // Checking request type
  271. switch (req->bmRequest & USB_REQ_TYPE_MASK)
  272. {
  273. case USB_REQ_TYPE_VENDOR : // Vendor I/O
  274. {
  275. if (req->wLength > 0)
  276. {
  277. if (req->bmRequest & 0x80)
  278. {
  279. #if RECREATE_CONTROL_TRANSFER_OBJ
  280. usb_create_transfer( &hvendor->ep0_tx_transfer,
  281. &hvendor->ep0_tx_buffer[0],
  282. sizeof(hvendor->ep0_tx_buffer),
  283. NULL, pdev );
  284. #endif
  285. ret = USBD_COMMON_Setup_DataProcess( pdev, req, &hvendor->ep0_tx_transfer );
  286. }
  287. else
  288. {
  289. #if RECREATE_CONTROL_TRANSFER_OBJ
  290. usb_create_transfer( &hvendor->ep0_rx_transfer,
  291. &hvendor->ep0_rx_buffer[0],
  292. sizeof(hvendor->ep0_rx_buffer),
  293. NULL, pdev );
  294. #endif
  295. ret = USBD_COMMON_Setup_DataProcess( pdev, req, &hvendor->ep0_rx_transfer );
  296. }
  297. }
  298. else
  299. {
  300. USBD_CtlSendStatus( pdev ); // need answer for ZeroLength requests
  301. }
  302. }
  303. break;
  304. case USB_REQ_TYPE_STANDARD:
  305. {
  306. switch (req->bRequest)
  307. {
  308. case USB_REQ_GET_INTERFACE:
  309. {
  310. if( NULL != pUserIface->fGetIface )
  311. {
  312. uint8_t nIf = req->wIndex;
  313. uint8_t ifalt = 0;
  314. if( pUserIface->fGetIface( pdev->dev_config, nIf, &ifalt ) )
  315. {
  316. USBD_CtlSendData (pdev, &ifalt, sizeof(ifalt));
  317. }
  318. else
  319. {
  320. ret = USBD_FAIL;
  321. }
  322. }
  323. else
  324. {
  325. ret = USBD_FAIL;
  326. }
  327. }
  328. break;
  329. case USB_REQ_SET_INTERFACE:
  330. break;
  331. }
  332. }
  333. break;
  334. default:
  335. break;
  336. }
  337. }
  338. if( ret != USBD_OK )
  339. {
  340. pUserIface->fUsbSetup( req, false, false );
  341. }
  342. return ret;
  343. }
  344. #else
  345. static uint8_t USBD_VENDOR_Setup (USBD_HandleTypeDef *pdev,
  346. USBD_SetupReqTypedef *req)
  347. {
  348. uint8_t ret = USBD_OK;
  349. USBD_VENDOR_HandleTypeDef *hvendor = (USBD_VENDOR_HandleTypeDef*) pdev->pClassData;
  350. USBD_COMMON_ItfTypeDef * pUserIface = ((USBD_COMMON_ItfTypeDef *)pdev->pUserData);
  351. if( NULL == pUserIface->fUsbSetup )
  352. {
  353. return USBD_FAIL; // @fUsbSetup is missing
  354. }
  355. if( NULL == hvendor)
  356. {
  357. return USBD_FAIL;
  358. }
  359. // SETUP filtering:
  360. if( ! pUserIface->fUsbSetup( req, true, false ) )
  361. {
  362. return USBD_FAIL; // do not call @fUsbSetup second time
  363. }
  364. else
  365. {
  366. switch (req->bmRequest & USB_REQ_TYPE_MASK)
  367. {
  368. case USB_REQ_TYPE_VENDOR : // Vendor I/O
  369. {
  370. if (req->wLength > 0)
  371. {
  372. if (req->bmRequest & 0x80)
  373. {
  374. /*if( req->wLength > 0 ) --- extra checking fixed 22/05/19 */
  375. {
  376. // DataIN stage preparing
  377. #if RECREATE_CONTROL_TRANSFER_OBJ
  378. usb_create_transfer( &hvendor->ep0_tx_transfer,
  379. &hvendor->ep0_tx_buffer[0],
  380. sizeof(hvendor->ep0_tx_buffer),
  381. NULL, pdev );
  382. #endif
  383. if( usb_validate_transfer( &hvendor->ep0_tx_transfer ) )
  384. {
  385. // First packet in the stage: reset the transfer
  386. usb_reset_transfer( &hvendor->ep0_tx_transfer );
  387. // Single-transfer mode only:
  388. // Whole transfer is limited by the @ep0_tx_transfer size
  389. size_t nBytesProcessed = pUserIface->fUsbCtlEpTx( (const tUSBSetupPacket_t*)req,
  390. &hvendor->ep0_tx_transfer,
  391. 0,
  392. req->wLength );
  393. // User can perform move semantic on @ep0_tx_transfer transfer
  394. // This is an error condition, let's check it:
  395. #warning Check
  396. if( usb_validate_transfer( &hvendor->ep0_tx_transfer ) )
  397. {
  398. USBD_EndpointTypeDef * pep = &pdev->ep_in[0];
  399. // If SETUP data stage assumes trasmitting more than one MAX_PACKET bytes,
  400. // this packet must include MAX_PACKET bytes (not to be "short packet")
  401. size_t min_limit = ((req->wLength > pep->maxpacket)?(pep->maxpacket-1):(0));
  402. // Actually, @ep0_tx_transfer can store more data than @nBytesProcessed.
  403. // @nBytesProcessed: bytes processed by last @fUsbCtlEpTx call.
  404. // Just check it for zero:
  405. if( nBytesProcessed > 0 )
  406. {
  407. // Check the transfer for minimal bytes limit:
  408. if( min_limit < usb_count_transfer( &hvendor->ep0_tx_transfer ) )
  409. {
  410. // truncate the transfer in case if the transfer size is
  411. // greater than requested length.
  412. usb_truncate_transfer( &hvendor->ep0_tx_transfer, req->wLength );
  413. USBD_CtlSendData (pdev,
  414. (const uint8_t *)hvendor->ep0_tx_transfer.pReadData,
  415. req->wLength );
  416. }
  417. else
  418. {
  419. ret = USBD_FAIL;
  420. }
  421. }
  422. else
  423. {
  424. ret = USBD_FAIL;
  425. }
  426. }
  427. else
  428. {
  429. ret = USBD_FAIL;
  430. }
  431. }
  432. else
  433. {
  434. ret = USBD_FAIL;
  435. }
  436. }
  437. }
  438. else
  439. {
  440. // DataOUT stage preparing
  441. if( req->wLength > 0 )
  442. {
  443. #if RECREATE_CONTROL_TRANSFER_OBJ
  444. usb_create_transfer( &hvendor->ep0_rx_transfer,
  445. &hvendor->ep0_rx_buffer[0],
  446. sizeof(hvendor->ep0_rx_buffer),
  447. NULL, pdev );
  448. #endif
  449. if( usb_validate_transfer( &hvendor->ep0_rx_transfer ) )
  450. {
  451. // Reset the RX-transfer
  452. usb_reset_transfer( &hvendor->ep0_rx_transfer );
  453. // Begin receiving first packet
  454. USBD_CtlPrepareRx (pdev,
  455. (uint8_t *)hvendor->ep0_rx_transfer.pWriteData,
  456. req->wLength );
  457. }
  458. else
  459. {
  460. ret = USBD_FAIL;
  461. }
  462. }
  463. }
  464. }
  465. else
  466. {
  467. USBD_CtlSendStatus( pdev ); // need answer for ZeroLength requests
  468. }
  469. }
  470. break;
  471. case USB_REQ_TYPE_STANDARD:
  472. {
  473. switch (req->bRequest)
  474. {
  475. case USB_REQ_GET_INTERFACE:
  476. {
  477. if( NULL != pUserIface->fGetIface )
  478. {
  479. uint8_t nIf = req->wIndex;
  480. uint8_t ifalt = 0;
  481. if( pUserIface->fGetIface( pdev->dev_config, nIf, &ifalt ) )
  482. {
  483. USBD_CtlSendData (pdev, &ifalt, sizeof(ifalt));
  484. }
  485. else
  486. {
  487. ret = USBD_FAIL;
  488. }
  489. }
  490. else
  491. {
  492. ret = USBD_FAIL;
  493. }
  494. }
  495. break;
  496. case USB_REQ_SET_INTERFACE:
  497. break;
  498. }
  499. }
  500. break;
  501. default:
  502. break;
  503. }
  504. }
  505. if( ret != USBD_OK )
  506. {
  507. pUserIface->fUsbSetup( req, false, false );
  508. }
  509. return ret;
  510. }
  511. #endif
  512. // USBD_VENDOR_EP0_RxReady()
  513. // This function is called when a packet via CONTROL protocol had been received from host
  514. // This function continues the transmitting
  515. #if VENDOR_USE_COMMON_EP0RXREADY
  516. static uint8_t USBD_VENDOR_EP0_RxReady (USBD_HandleTypeDef *pdev, size_t * pnBytesRollback )
  517. {
  518. USBD_VENDOR_HandleTypeDef *hvendor = (USBD_VENDOR_HandleTypeDef*) pdev->pClassData;
  519. return USBD_COMMON_EP0_RxReady( pdev, &hvendor->ep0_rx_transfer, pnBytesRollback );
  520. }
  521. #else
  522. static uint8_t USBD_VENDOR_EP0_RxReady (USBD_HandleTypeDef *pdev, size_t * pnBytesRollback )
  523. {
  524. USBD_VENDOR_HandleTypeDef *hvendor = (USBD_VENDOR_HandleTypeDef*) pdev->pClassData;
  525. USBD_EndpointTypeDef * pep = &pdev->ep_out[0];
  526. USBD_StatusTypeDef ret = USBD_FAIL;
  527. sUSBTransfer_t * rx = &hvendor->ep0_rx_transfer;
  528. if( pdev->pUserData != NULL && usb_validate_transfer(rx) )
  529. {
  530. USBD_COMMON_ItfTypeDef * pUserIface = ((USBD_COMMON_ItfTypeDef *)pdev->pUserData);
  531. if( NULL != pUserIface->fUsbCtlEpRx )
  532. {
  533. size_t nBytesReceived = ((pep->rem_length > pep->maxpacket)?pep->maxpacket:pep->rem_length);
  534. size_t nBytesRemaining = (pep->rem_length - nBytesReceived);
  535. // Since the core uses raw @ep0_rx_transfer->pWriteData pointer, it is required to update the transfer information.
  536. // Use 'virutal write' to shift the pointer:
  537. usb_transfer_virtual_write( rx, nBytesReceived );
  538. // Check if the trasfer have enough free space for receiving another packet?
  539. // Or maybe it is a last packet?
  540. if( (usb_space_transfer(rx) < MIN(nBytesRemaining, pep->maxpacket)) || (0 == nBytesRemaining) )
  541. {
  542. // No, there no enough free space
  543. // Need to call user handler.
  544. // Retirieve actual amount of data in transfer
  545. size_t nBytesInTransfer = usb_count_transfer(rx);
  546. // Calculate current data offset
  547. size_t nOffset = (pep->total_length - nBytesRemaining - nBytesInTransfer);
  548. // pass the data to user
  549. size_t nBytesProcessed = pUserIface->fUsbCtlEpRx( &pdev->request,
  550. rx,
  551. nOffset,
  552. nBytesRemaining + nBytesInTransfer );
  553. // user can perform move semantic on transfer @rx.
  554. // It is valid condition if no remaining bytes left only.
  555. // Let's check it:
  556. bool bValid = usb_validate_transfer( rx );
  557. #warning Check
  558. if( 0 == nBytesRemaining || bValid )
  559. {
  560. // Actually, it does not matter, what exactly the function @fUsbCtlEpRx returns.
  561. // The only that matters is the amount of data in the transfer.
  562. // Just check the return value for zero:
  563. if( nBytesProcessed > 0 )
  564. {
  565. // By default ( usb_count_transfer() is ZERO ) ==> @nCompressed equals to the number of bytes in the transfer before user handler call.
  566. size_t nCompressed = nBytesInTransfer;
  567. // It is required that the transfer be valid only if @nBytesRemaining greater than zero
  568. if( bValid )
  569. {
  570. // If the transfer still contains data, it means user didn't perform full read.
  571. if( usb_count_transfer(rx) > 0 )
  572. {
  573. // it is required to compress the data in the transfer
  574. // optimize the transfer to compress stored data
  575. nCompressed = usb_transfer_compress( rx );
  576. // @bNeedPacketSpace - true if the core deadly needs at least MAX_PACKET free bytes in transfer
  577. bool bNeedPacketSpace = false;
  578. // After compressing: check if the transfer have enough space for next packet?
  579. if( usb_space_transfer(rx) >= MIN(nBytesRemaining, pep->maxpacket) )
  580. {
  581. // yes, ok
  582. ret = USBD_OK;
  583. }
  584. else
  585. {
  586. // nope, user didn't read enough data from the transfer to free enough space for next packet
  587. // Need to free at least MAX_PACKET free bytes in transfer
  588. bNeedPacketSpace = true;
  589. }
  590. // try to call user handler again:
  591. if( bNeedPacketSpace || (0 == nBytesRemaining) )
  592. {
  593. // Here: two reasons:
  594. // 1) if it is required to free MAX_PACKET bytes in transfer for next packet
  595. // 2) or this is a last packet: last chance to retrieve data. It is required to feed all data to user... Even if he resists!!!! >:D
  596. // For USB-core it does not matter if user processed all bytes or not...
  597. // There no incoming packets expected.
  598. // The only problem is to send a confirmation for this transaction.
  599. // As soon user process all bytes from the transfer @rx, this function returns control to the
  600. // core to send a confirmation.
  601. // Ok, lets feed user..
  602. while( nBytesProcessed > 0 )
  603. {
  604. nBytesInTransfer = usb_count_transfer(rx);
  605. size_t nLeftBytes = nBytesInTransfer + ((bNeedPacketSpace)?nBytesRemaining:0);
  606. size_t nOffset = (pep->total_length - nLeftBytes);
  607. // Call user handler, again...
  608. nBytesProcessed = pUserIface->fUsbCtlEpRx( &pdev->request,
  609. rx,
  610. nOffset,
  611. nLeftBytes );
  612. // user can perform move semantic on transfer @rx.
  613. // It is valid condition if no remaining bytes left only.
  614. // Let's check it:
  615. #warning Check
  616. if( 0 == nBytesRemaining || usb_validate_transfer( rx ) )
  617. {
  618. // ~~~ do not do this here, extra compressing possible (reduces performance) ~~ /* usb_transfer_compress( rx ); */
  619. // ~~~ do not do this here, extra compressing possible (reduces performance) ~~ /* nCompressed += (nBytesInTransfer - usb_count_transfer(rx)); */
  620. // Reason 1: (only if @bNeedPacketSpace is true)
  621. // check if the transfer has enough data for next packet
  622. if( bNeedPacketSpace
  623. && (usb_space_transfer(rx) >= MIN(nBytesRemaining, pep->maxpacket)) )
  624. {
  625. // Reason 1: exit, because transfer has enough free space for next packet
  626. // User have read data portion from the transfer, need to compress it again
  627. // Need to update @nCompressed variable to take in account new pointer to the free memory.
  628. usb_transfer_compress( rx );
  629. nCompressed += (nBytesInTransfer - usb_count_transfer(rx));
  630. ret = USBD_OK;
  631. break;
  632. }
  633. else
  634. {
  635. // User have read data portion from the transfer, need to compress it again
  636. // Need to update @nCompressed variable to take in account new pointer to the free memory.
  637. usb_transfer_compress( rx );
  638. nCompressed += (nBytesInTransfer - usb_count_transfer(rx));
  639. }
  640. // check if the transfer is still not empty
  641. if( 0 == usb_count_transfer(rx) )
  642. {
  643. // Reason 2: exit if transfer is empty
  644. // Empty! => Success (nBytesProcessed>0)
  645. break; // yee, at last!
  646. }
  647. }
  648. else
  649. {
  650. // Failed!
  651. ret = USBD_FAIL;
  652. }
  653. }
  654. // Check the last user call status
  655. if( 0 == nBytesProcessed )
  656. {
  657. // Failed!
  658. ret = USBD_FAIL;
  659. }
  660. }
  661. }
  662. else
  663. {
  664. // ~~~~~~~~~~~~ ADDED 18/04/19
  665. // Check if the trasfer have enough free space for receiving another packet?
  666. // Or maybe it is a last packet?
  667. if( (usb_space_transfer(rx) < MIN(nBytesRemaining, pep->maxpacket)) )
  668. {
  669. // it is required to compress the data in the transfer
  670. // optimize the transfer to compress stored data
  671. nCompressed = usb_transfer_compress( rx );
  672. }
  673. // ~~~~~~~~~~~~
  674. ret = USBD_OK;
  675. }
  676. }
  677. else
  678. {
  679. // If there no bytes left to receive
  680. // it is normal that the transfer is invalid (user may use move semantic)
  681. if( 0 == nBytesRemaining )
  682. {
  683. ret = USBD_OK;
  684. }
  685. }
  686. // let the core know how many excaclty bytes is required to shift the pointer back.
  687. if( (USBD_OK == ret) && (NULL != pnBytesRollback) )
  688. {
  689. // pass the exact number of bytes for rollback to the core
  690. *pnBytesRollback = nCompressed;
  691. }
  692. }
  693. else
  694. {
  695. // user must not return zero to proceed
  696. usb_reset_transfer( rx );
  697. ret = USBD_FAIL;
  698. }
  699. }
  700. else
  701. {
  702. ret = USBD_FAIL;
  703. }
  704. }
  705. else
  706. {
  707. // yep, there is free space for the next packet, let's receive it
  708. ret = USBD_CONTINUE;
  709. }
  710. }
  711. }
  712. return ret;
  713. }
  714. #endif
  715. // USBD_VENDOR_EP0_TxSent()
  716. // This function is called when at least one packet via CONTROL protocol had been sent to the host
  717. // This function continues the transmitting
  718. #if VENDOR_USE_COMMON_EP0TXSENT
  719. static uint8_t USBD_VENDOR_EP0_TxSent (USBD_HandleTypeDef *pdev, size_t * pnBytesRollback )
  720. {
  721. USBD_VENDOR_HandleTypeDef *hvendor = (USBD_VENDOR_HandleTypeDef*) pdev->pClassData;
  722. return USBD_COMMON_EP0_TxSent( pdev, &hvendor->ep0_tx_transfer, pnBytesRollback );
  723. }
  724. #else
  725. static uint8_t USBD_VENDOR_EP0_TxSent (USBD_HandleTypeDef *pdev, size_t * pnBytesRollback )
  726. {
  727. USBD_VENDOR_HandleTypeDef *hvendor = (USBD_VENDOR_HandleTypeDef*) pdev->pClassData;
  728. #if CONFIG_USB_VENDOR_DUMMYREAD
  729. memset( hvendor->ep0_tx_transfer.pDefaultBuffer, 0xAA, hvendor->ep0_tx_transfer.size );
  730. return USBD_OK;
  731. #endif
  732. USBD_EndpointTypeDef * pep = &pdev->ep_in[0];
  733. sUSBTransfer_t * tx = &hvendor->ep0_tx_transfer;
  734. uint8_t ret = USBD_FAIL;
  735. if( pdev->pUserData != NULL && usb_validate_transfer(tx) )
  736. {
  737. USBD_COMMON_ItfTypeDef * pUserIface = ((USBD_COMMON_ItfTypeDef *)pdev->pUserData);
  738. if( NULL != pUserIface->fUsbCtlEpTx )
  739. {
  740. // Due to the low-level driver uses dry transfer->@pReadData pointer, the
  741. // tranfer object must be modified.
  742. size_t nBytesRequested = 0;
  743. if( 0 == pep->rem_length )
  744. {
  745. // Update transfer information:
  746. // If @pep->rem_length is zero, the last packet sent was a last packet
  747. // The size of the short packet is a remainder of the division the
  748. // full transaction size and the endpoint size.
  749. // If the result of operation is non zero, the last packet was a short packet.
  750. // If the result of operation is zero, the last packet was full packet
  751. size_t last_packet_size = (pep->total_length % pep->maxpacket);
  752. if( last_packet_size == 0 )
  753. {
  754. last_packet_size = pep->maxpacket;
  755. }
  756. usb_transfer_virtual_read( tx, last_packet_size );
  757. }
  758. else
  759. {
  760. nBytesRequested = MIN( pep->maxpacket, pep->rem_length );
  761. // Update transfer information:
  762. // If @pep->rem_length is non zero, the last packet send was a full packet.
  763. // The size of full packet is pep->maxpacket
  764. usb_transfer_virtual_read( tx, pep->maxpacket );
  765. }
  766. // // Update transfer information:
  767. // usb_transfer_virtual_read( tx,
  768. // // The variable @nBytesRequested have quite confusing name, but this argument
  769. // // requires the length of already sent data. Actually the number of bytes already sent
  770. // // is numerially equals to @nBytesRequested
  771. // nBytesRequested // for pep->rem_length >= pep->maxpacket: the pep->maxpacket bytes just have been sent
  772. // ); // for pep->rem_length < pep->maxpacket: the pep->rem_length bytes just have been sent
  773. //
  774. // If the transaction is still not completed:
  775. if( pep->rem_length == 0 )
  776. {
  777. // transfer full reset
  778. usb_reset_transfer( tx );
  779. ret = USBD_OK;
  780. }
  781. else
  782. {
  783. size_t nBytesLeft = usb_count_transfer( tx );
  784. // If the transfer has enougth data to send another packet:
  785. if( nBytesLeft >= nBytesRequested )
  786. {
  787. ret = USBD_CONTINUE; // ask the core to continue sending the transfer (without rollback)
  788. }
  789. else
  790. {
  791. if( nBytesLeft > 0 )
  792. {
  793. // optimize the transfer to compress stored data
  794. size_t nCompressed = usb_transfer_compress( tx );
  795. if( NULL != pnBytesRollback )
  796. {
  797. // pass the exact number of bytes for rollback to the core
  798. *pnBytesRollback = nCompressed;
  799. }
  800. }
  801. else
  802. {
  803. // // transfer full reset
  804. // usb_reset_transfer( tx );
  805. size_t nCompressed = usb_transfer_compress( tx );
  806. if( NULL != pnBytesRollback )
  807. {
  808. // pass the exact number of bytes for rollback to the core
  809. *pnBytesRollback = nCompressed;
  810. }
  811. }
  812. size_t nOffset = (pep->total_length - pep->rem_length + nBytesLeft);
  813. size_t nBytesProcessed = pUserIface->fUsbCtlEpTx( &pdev->request,
  814. tx,
  815. nOffset,
  816. pep->rem_length );
  817. // User can perform move semantic on the @tx transfer.
  818. // This is an error condition, let's check it:
  819. #warning Check
  820. if( usb_validate_transfer( tx ) )
  821. {
  822. // Actually, does not matter what exactly the function @fUsbCtlEpTx returns,
  823. // because only transfer size really matters ( usb_count_transfer() ).
  824. // Just check the return value for zero:
  825. if( nBytesProcessed > 0 )
  826. {
  827. // If more than one FULL packet is required to send:
  828. if( pep->rem_length >= pep->maxpacket )
  829. {
  830. // This packet must be full: transfer must contain at least @pep->maxpacket bytes
  831. if( usb_count_transfer( tx ) >= pep->maxpacket )
  832. {
  833. ret = USBD_OK;
  834. }
  835. }
  836. else
  837. // No, it is required to send "short packet"
  838. {
  839. // This packet must be short: transfer must contain exactly @pep->rem_length bytes
  840. if( usb_count_transfer( tx ) == pep->rem_length )
  841. {
  842. ret = USBD_OK;
  843. }
  844. }
  845. }
  846. else
  847. {
  848. ret = USBD_FAIL;
  849. }
  850. }
  851. else
  852. {
  853. ret = USBD_FAIL;
  854. }
  855. }
  856. }
  857. }
  858. }
  859. return ret;
  860. }
  861. #endif